diff mbox series

[iproute2-next] ss: dump sockmap information

Message ID 20230313043339.395737-1-xiyou.wangcong@gmail.com (mailing list archive)
State New, archived
Delegated to: David Ahern
Headers show
Series [iproute2-next] ss: dump sockmap information | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch, async

Commit Message

Cong Wang March 13, 2023, 4:33 a.m. UTC
From: Cong Wang <cong.wang@bytedance.com>

This patch dumps sockmap ID's which a socket has been added to.

Sample output:

      # ./iproute2/misc/ss -tnaie --bpf-map
      ESTAB  0      344329     127.0.0.1:1234     127.0.0.1:40912 ino:21098 sk:5 cgroup:/user.slice/user-0.slice/session-c1.scope <-> sockmap: 1

      # bpftool map
      1: sockmap  flags 0x0
            key 4B  value 4B  max_entries 2  memlock 4096B
            pids echo-sockmap(549)
      4: array  name pid_iter.rodata  flags 0x480
            key 4B  value 4B  max_entries 1  memlock 4096B
            btf_id 10  frozen
            pids bpftool(624)

Signed-off-by: Cong Wang <cong.wang@bytedance.com>
---
 include/uapi/linux/inet_diag.h |  1 +
 include/uapi/linux/sock_diag.h |  8 ++++++++
 include/uapi/linux/unix_diag.h |  1 +
 misc/ss.c                      | 35 ++++++++++++++++++++++++++++++++++
 4 files changed, 45 insertions(+)
diff mbox series

Patch

diff --git a/include/uapi/linux/inet_diag.h b/include/uapi/linux/inet_diag.h
index d81cb69a..adadeb3c 100644
--- a/include/uapi/linux/inet_diag.h
+++ b/include/uapi/linux/inet_diag.h
@@ -161,6 +161,7 @@  enum {
 	INET_DIAG_SK_BPF_STORAGES,
 	INET_DIAG_CGROUP_ID,
 	INET_DIAG_SOCKOPT,
+	INET_DIAG_BPF_MAP,
 	__INET_DIAG_MAX,
 };
 
diff --git a/include/uapi/linux/sock_diag.h b/include/uapi/linux/sock_diag.h
index 35c0ce67..fb7c3f22 100644
--- a/include/uapi/linux/sock_diag.h
+++ b/include/uapi/linux/sock_diag.h
@@ -62,4 +62,12 @@  enum {
 
 #define SK_DIAG_BPF_STORAGE_MAX        (__SK_DIAG_BPF_STORAGE_MAX - 1)
 
+enum {
+	SK_DIAG_BPF_MAP_NONE,
+	SK_DIAG_BPF_MAP_IDS,
+	__SK_DIAG_BPF_MAP_MAX,
+};
+
+#define SK_DIAG_BPF_MAP_MAX        (__SK_DIAG_BPF_MAP_MAX - 1)
+
 #endif /* __SOCK_DIAG_H__ */
diff --git a/include/uapi/linux/unix_diag.h b/include/uapi/linux/unix_diag.h
index a1988576..b95a2b33 100644
--- a/include/uapi/linux/unix_diag.h
+++ b/include/uapi/linux/unix_diag.h
@@ -42,6 +42,7 @@  enum {
 	UNIX_DIAG_MEMINFO,
 	UNIX_DIAG_SHUTDOWN,
 	UNIX_DIAG_UID,
+	UNIX_DIAG_BPF_MAP,
 
 	__UNIX_DIAG_MAX,
 };
diff --git a/misc/ss.c b/misc/ss.c
index de02fccb..a4e72346 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -121,6 +121,7 @@  static int show_tipcinfo;
 static int show_tos;
 static int show_cgroup;
 static int show_inet_sockopt;
+static int show_bpf_map;
 int oneline;
 
 enum col_id {
@@ -3377,6 +3378,28 @@  static void parse_diag_msg(struct nlmsghdr *nlh, struct sockstat *s)
 	memcpy(s->remote.data, r->id.idiag_dst, s->local.bytelen);
 }
 
+static void print_bpf_map(struct rtattr *tb)
+{
+	if (tb) {
+		struct rtattr *sockmap[SK_DIAG_BPF_MAP_MAX + 1] = { 0 };
+
+		parse_rtattr_nested(sockmap, SK_DIAG_BPF_MAP_MAX, tb);
+		if (sockmap[SK_DIAG_BPF_MAP_IDS]) {
+			__u32 *maps = RTA_DATA(sockmap[SK_DIAG_BPF_MAP_IDS]);
+			int len = RTA_PAYLOAD(sockmap[SK_DIAG_BPF_MAP_IDS]);
+
+			out(" sockmap:");
+			out(" %d", *maps);
+			maps++;
+			for (len -= 4; len > 0; len -= 4) {
+				out(",");
+				out(" %d", *maps);
+				maps++;
+			}
+		}
+	}
+}
+
 static int inet_show_sock(struct nlmsghdr *nlh,
 			  struct sockstat *s)
 {
@@ -3470,6 +3493,9 @@  static int inet_show_sock(struct nlmsghdr *nlh,
 		}
 	}
 
+	if (show_bpf_map)
+		print_bpf_map(tb[INET_DIAG_BPF_MAP]);
+
 	if (show_mem || (show_tcpinfo && s->type != IPPROTO_UDP)) {
 		if (!oneline)
 			out("\n\t");
@@ -4153,6 +4179,9 @@  static int unix_show_sock(struct nlmsghdr *nlh, void *arg)
 		}
 	}
 
+	if (show_bpf_map)
+		print_bpf_map(tb[UNIX_DIAG_BPF_MAP]);
+
 	return 0;
 }
 
@@ -5469,6 +5498,8 @@  static int scan_state(const char *state)
 
 #define OPT_INET_SOCKOPT 262
 
+#define OPT_BPF_MAP 263
+
 static const struct option long_opts[] = {
 	{ "numeric", 0, 0, 'n' },
 	{ "resolve", 0, 0, 'r' },
@@ -5513,6 +5544,7 @@  static const struct option long_opts[] = {
 	{ "mptcp", 0, 0, 'M' },
 	{ "oneline", 0, 0, 'O' },
 	{ "inet-sockopt", 0, 0, OPT_INET_SOCKOPT },
+	{ "bpf-map", 0, 0, OPT_BPF_MAP },
 	{ 0 }
 
 };
@@ -5715,6 +5747,9 @@  int main(int argc, char *argv[])
 		case OPT_INET_SOCKOPT:
 			show_inet_sockopt = 1;
 			break;
+		case OPT_BPF_MAP:
+			show_bpf_map = 1;
+			break;
 		case 'h':
 			help();
 		case '?':