netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pascal Bouchareine <kalou@tfz.net>
To: linux-kernel@vger.kernel.org
Cc: Pascal Bouchareine <kalou@tfz.net>,
	linux-api@vger.kernel.org, netdev@vger.kernel.org,
	"David S. Miller" <davem@davemloft.net>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Alexey Dobriyan" <adobriyan@gmail.com>,
	"Al Viro" <viro@zeniv.linux.org.uk>
Subject: [PATCH 2/2] net: socket: implement SO_DESCRIPTION
Date: Sat, 15 Aug 2020 11:23:44 -0700	[thread overview]
Message-ID: <20200815182344.7469-3-kalou@tfz.net> (raw)
In-Reply-To: <20200815182344.7469-1-kalou@tfz.net>

This command attaches the zero terminated string in optval to the
socket for troubleshooting purposes. The free string is displayed in the
process fdinfo file for that fd (/proc/<pid>/fdinfo/<fd>).

One intended usage is to allow processes to self-document sockets
for netstat and friends to report

We ignore optlen and constrain the string to a static max size

Signed-off-by: Pascal Bouchareine <kalou@tfz.net>
---
 include/net/sock.h                |  4 ++++
 include/uapi/asm-generic/socket.h |  2 ++
 net/core/sock.c                   | 23 +++++++++++++++++++++++
 net/socket.c                      |  5 +++++
 4 files changed, 34 insertions(+)

diff --git a/include/net/sock.h b/include/net/sock.h
index 1183507df95b..6b4fd1383282 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -342,6 +342,7 @@ struct bpf_sk_storage;
   *	@sk_txtime_deadline_mode: set deadline mode for SO_TXTIME
   *	@sk_txtime_report_errors: set report errors mode for SO_TXTIME
   *	@sk_txtime_unused: unused txtime flags
+  *	@sk_description: user supplied with SO_DESCRIPTION
   */
 struct sock {
 	/*
@@ -519,6 +520,9 @@ struct sock {
 	struct bpf_sk_storage __rcu	*sk_bpf_storage;
 #endif
 	struct rcu_head		sk_rcu;
+
+#define	SK_MAX_DESC_SIZE	256
+	char			*sk_description;
 };
 
 enum sk_pacing {
diff --git a/include/uapi/asm-generic/socket.h b/include/uapi/asm-generic/socket.h
index 77f7c1638eb1..fb51c4bb7a12 100644
--- a/include/uapi/asm-generic/socket.h
+++ b/include/uapi/asm-generic/socket.h
@@ -119,6 +119,8 @@
 
 #define SO_DETACH_REUSEPORT_BPF 68
 
+#define SO_DESCRIPTION		69
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))
diff --git a/net/core/sock.c b/net/core/sock.c
index 2e5b7870e5d3..2cb44a0e38b7 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -828,6 +828,24 @@ void sock_set_rcvbuf(struct sock *sk, int val)
 }
 EXPORT_SYMBOL(sock_set_rcvbuf);
 
+int sock_set_description(struct sock *sk, char __user *user_desc)
+{
+	char *old, *desc;
+
+	desc = strndup_user(user_desc, SK_MAX_DESC_SIZE, GFP_KERNEL_ACCOUNT);
+	if (IS_ERR(desc))
+		return PTR_ERR(desc);
+
+	lock_sock(sk);
+	old = sk->sk_description;
+	sk->sk_description = desc;
+	release_sock(sk);
+
+	kfree(old);
+
+	return 0;
+}
+
 /*
  *	This is meant for all protocols to use and covers goings on
  *	at the socket level. Everything here is generic.
@@ -850,6 +868,9 @@ int sock_setsockopt(struct socket *sock, int level, int optname,
 	if (optname == SO_BINDTODEVICE)
 		return sock_setbindtodevice(sk, optval, optlen);
 
+	if (optname == SO_DESCRIPTION)
+		return sock_set_description(sk, optval);
+
 	if (optlen < sizeof(int))
 		return -EINVAL;
 
@@ -1792,6 +1813,8 @@ static void __sk_destruct(struct rcu_head *head)
 		RCU_INIT_POINTER(sk->sk_filter, NULL);
 	}
 
+	kfree(sk->sk_description);
+
 	sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP);
 
 #ifdef CONFIG_BPF_SYSCALL
diff --git a/net/socket.c b/net/socket.c
index 976426d03f09..4f2c1a7744b0 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -134,6 +134,11 @@ static void sock_show_fdinfo(struct seq_file *m, struct file *f)
 {
 	struct socket *sock = f->private_data;
 
+	lock_sock(sock->sk);
+	if (sock->sk->sk_description)
+		seq_printf(m, "desc:\t%s\n", sock->sk->sk_description);
+	release_sock(sock->sk);
+
 	if (sock->ops->show_fdinfo)
 		sock->ops->show_fdinfo(m, sock);
 }
-- 
2.25.1


  parent reply	other threads:[~2020-08-15 21:56 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-15 18:23 [RFC PATCH 0/2] proc,socket: attach description to sockets Pascal Bouchareine
2020-08-15 18:23 ` [PATCH 1/2] mm: add GFP mask param to strndup_user Pascal Bouchareine
2020-08-15 22:42   ` Al Viro
2020-08-16 16:10     ` Alexey Dobriyan
2020-08-15 18:23 ` Pascal Bouchareine [this message]
2020-08-16  5:02   ` [PATCH 2/2] net: socket: implement SO_DESCRIPTION kernel test robot
2020-08-16  7:33   ` kernel test robot
2020-08-16  7:33   ` [RFC PATCH] net: socket: sock_set_description() can be static kernel test robot
2020-08-16  9:00   ` [PATCH 2/2] net: socket: implement SO_DESCRIPTION kernel test robot
2020-08-16 18:15   ` Eric Dumazet
2020-08-16 19:30     ` Pascal Bouchareine
2020-08-22  3:28 ` [PATCH v2 1/2] mm: add GFP mask param to strndup_user Pascal Bouchareine
2020-08-22  3:28   ` [PATCH v2 2/2] net: socket: implement SO_DESCRIPTION Pascal Bouchareine
2020-08-22  6:57     ` kernel test robot
2020-08-22 19:36     ` David Miller
2020-08-22 19:59       ` Pascal Bouchareine
2020-08-22 20:19         ` Pascal Bouchareine
2020-08-22 20:53           ` Pascal Bouchareine
2020-08-22 21:01             ` David Miller
2020-08-23 22:28               ` Pascal Bouchareine
2020-08-22  3:51   ` [PATCH v2 1/2] mm: add GFP mask param to strndup_user Andrew Morton
2020-08-22 19:36     ` Pascal Bouchareine

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200815182344.7469-3-kalou@tfz.net \
    --to=kalou@tfz.net \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=davem@davemloft.net \
    --cc=kuba@kernel.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).