naccyde / rpms / iproute

Forked from rpms/iproute 9 months ago
Clone

Blame SOURCES/0039-ss-add-AF_VSOCK-support.patch

cd1737
From fe898bd10be2bc527f81421f06afff77e8ba42eb Mon Sep 17 00:00:00 2001
cd1737
From: Stefano Brivio <sbrivio@redhat.com>
cd1737
Date: Sun, 22 Oct 2017 21:44:27 +0200
cd1737
Subject: [PATCH] ss: add AF_VSOCK support
cd1737
cd1737
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1472759
cd1737
Upstream Status: iproute2.git commit c759116a0b2b
cd1737
cd1737
commit c759116a0b2b6da8df9687b0a40ac69050132c77
cd1737
Author: Stefan Hajnoczi <stefanha@redhat.com>
cd1737
Date:   Fri Oct 6 11:48:41 2017 -0400
cd1737
cd1737
    ss: add AF_VSOCK support
cd1737
cd1737
    The AF_VSOCK address family is a host<->guest communications channel
cd1737
    supported by VMware, KVM, and Hyper-V.  Initial VMware support was
cd1737
    released in Linux 3.9 in 2013 and transports for other hypervisors were
cd1737
    added later.
cd1737
cd1737
    AF_VSOCK addresses are <u32 cid, u32 port> tuples.  The 32-bit cid
cd1737
    integer is comparable to an IP address.  AF_VSOCK ports work like
cd1737
    TCP/UDP ports.
cd1737
cd1737
    Both SOCK_STREAM and SOCK_DGRAM socket types are available.
cd1737
cd1737
    This patch adds AF_VSOCK support to ss(8) so that sockets can be
cd1737
    observed.
cd1737
cd1737
    Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
cd1737
cd1737
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
cd1737
---
cd1737
 man/man8/ss.8 |   8 ++-
cd1737
 misc/ss.c     | 184 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
cd1737
 2 files changed, 188 insertions(+), 4 deletions(-)
cd1737
cd1737
diff --git a/man/man8/ss.8 b/man/man8/ss.8
cd1737
index 81de69d..4323eee 100644
cd1737
--- a/man/man8/ss.8
cd1737
+++ b/man/man8/ss.8
cd1737
@@ -125,14 +125,18 @@ Display Unix domain sockets (alias for -f unix).
cd1737
 .B \-S, \-\-sctp
cd1737
 Display SCTP sockets.
cd1737
 .TP
cd1737
+.B \-\-vsock
cd1737
+Display vsock sockets (alias for -f vsock).
cd1737
+.TP
cd1737
 .B \-f FAMILY, \-\-family=FAMILY
cd1737
 Display sockets of type FAMILY.
cd1737
-Currently the following families are supported: unix, inet, inet6, link, netlink.
cd1737
+Currently the following families are supported: unix, inet, inet6, link, netlink, vsock.
cd1737
 .TP
cd1737
 .B \-A QUERY, \-\-query=QUERY, \-\-socket=QUERY
cd1737
 List of socket tables to dump, separated by commas. The following identifiers
cd1737
 are understood: all, inet, tcp, udp, raw, unix, packet, netlink, unix_dgram,
cd1737
-unix_stream, unix_seqpacket, packet_raw, packet_dgram, dccp, sctp.
cd1737
+unix_stream, unix_seqpacket, packet_raw, packet_dgram, dccp, sctp,
cd1737
+vsock_stream, vsock_dgram.
cd1737
 .TP
cd1737
 .B \-D FILE, \-\-diag=FILE
cd1737
 Do not display anything, just dump raw information about TCP sockets to FILE after applying filters. If FILE is - stdout is used.
cd1737
diff --git a/misc/ss.c b/misc/ss.c
cd1737
index 0d64527..e922665 100644
cd1737
--- a/misc/ss.c
cd1737
+++ b/misc/ss.c
cd1737
@@ -44,6 +44,7 @@
cd1737
 #include <linux/packet_diag.h>
cd1737
 #include <linux/netlink_diag.h>
cd1737
 #include <linux/sctp.h>
cd1737
+#include <linux/vm_sockets_diag.h>
cd1737
 
cd1737
 #define MAGIC_SEQ 123456
cd1737
 
cd1737
@@ -126,6 +127,8 @@ enum {
cd1737
 	PACKET_R_DB,
cd1737
 	NETLINK_DB,
cd1737
 	SCTP_DB,
cd1737
+	VSOCK_ST_DB,
cd1737
+	VSOCK_DG_DB,
cd1737
 	MAX_DB
cd1737
 };
cd1737
 
cd1737
@@ -134,6 +137,7 @@ enum {
cd1737
 #define ALL_DB ((1<
cd1737
 #define INET_L4_DBM ((1<
cd1737
 #define INET_DBM (INET_L4_DBM | (1<
cd1737
+#define VSOCK_DBM ((1<
cd1737
 
cd1737
 enum {
cd1737
 	SS_UNKNOWN,
cd1737
@@ -222,6 +226,14 @@ static const struct filter default_dbs[MAX_DB] = {
cd1737
 		.states   = SS_CONN,
cd1737
 		.families = FAMILY_MASK(AF_INET) | FAMILY_MASK(AF_INET6),
cd1737
 	},
cd1737
+	[VSOCK_ST_DB] = {
cd1737
+		.states   = SS_CONN,
cd1737
+		.families = FAMILY_MASK(AF_VSOCK),
cd1737
+	},
cd1737
+	[VSOCK_DG_DB] = {
cd1737
+		.states   = SS_CONN,
cd1737
+		.families = FAMILY_MASK(AF_VSOCK),
cd1737
+	},
cd1737
 };
cd1737
 
cd1737
 static const struct filter default_afs[AF_MAX] = {
cd1737
@@ -245,6 +257,10 @@ static const struct filter default_afs[AF_MAX] = {
cd1737
 		.dbs    = (1 << NETLINK_DB),
cd1737
 		.states = (1 << SS_CLOSE),
cd1737
 	},
cd1737
+	[AF_VSOCK] = {
cd1737
+		.dbs    = VSOCK_DBM,
cd1737
+		.states = SS_CONN,
cd1737
+	},
cd1737
 };
cd1737
 
cd1737
 static int do_default = 1;
cd1737
@@ -283,6 +299,8 @@ static void filter_default_dbs(struct filter *f)
cd1737
 	filter_db_set(f, PACKET_DG_DB);
cd1737
 	filter_db_set(f, NETLINK_DB);
cd1737
 	filter_db_set(f, SCTP_DB);
cd1737
+	filter_db_set(f, VSOCK_ST_DB);
cd1737
+	filter_db_set(f, VSOCK_DG_DB);
cd1737
 }
cd1737
 
cd1737
 static void filter_states_set(struct filter *f, int states)
cd1737
@@ -792,6 +810,18 @@ static const char *proto_name(int protocol)
cd1737
 	return "???";
cd1737
 }
cd1737
 
cd1737
+static const char *vsock_netid_name(int type)
cd1737
+{
cd1737
+	switch (type) {
cd1737
+	case SOCK_STREAM:
cd1737
+		return "v_str";
cd1737
+	case SOCK_DGRAM:
cd1737
+		return "v_dgr";
cd1737
+	default:
cd1737
+		return "???";
cd1737
+	}
cd1737
+}
cd1737
+
cd1737
 static void sock_state_print(struct sockstat *s)
cd1737
 {
cd1737
 	const char *sock_name;
cd1737
@@ -824,6 +854,9 @@ static void sock_state_print(struct sockstat *s)
cd1737
 	case AF_NETLINK:
cd1737
 		sock_name = "nl";
cd1737
 		break;
cd1737
+	case AF_VSOCK:
cd1737
+		sock_name = vsock_netid_name(s->type);
cd1737
+		break;
cd1737
 	default:
cd1737
 		sock_name = "unknown";
cd1737
 	}
cd1737
@@ -1139,6 +1172,8 @@ static int run_ssfilter(struct ssfilter *f, struct sockstat *s)
cd1737
 			return s->lport == 0 && s->local.data[0] == 0;
cd1737
 		if (s->local.family == AF_NETLINK)
cd1737
 			return s->lport < 0;
cd1737
+		if (s->local.family == AF_VSOCK)
cd1737
+			return s->lport > 1023;
cd1737
 
cd1737
 		return is_ephemeral(s->lport);
cd1737
 	}
cd1737
@@ -1515,6 +1550,15 @@ void *parse_devcond(char *name)
cd1737
 	return res;
cd1737
 }
cd1737
 
cd1737
+static void vsock_set_inet_prefix(inet_prefix *a, __u32 cid)
cd1737
+{
cd1737
+	*a = (inet_prefix){
cd1737
+		.bytelen = sizeof(cid),
cd1737
+		.family = AF_VSOCK,
cd1737
+	};
cd1737
+	memcpy(a->data, &cid, sizeof(cid));
cd1737
+}
cd1737
+
cd1737
 void *parse_hostcond(char *addr, bool is_port)
cd1737
 {
cd1737
 	char *port = NULL;
cd1737
@@ -1589,6 +1633,37 @@ void *parse_hostcond(char *addr, bool is_port)
cd1737
 		goto out;
cd1737
 	}
cd1737
 
cd1737
+	if (fam == AF_VSOCK || strncmp(addr, "vsock:", 6) == 0) {
cd1737
+		__u32 cid = ~(__u32)0;
cd1737
+
cd1737
+		a.addr.family = AF_VSOCK;
cd1737
+		if (strncmp(addr, "vsock:", 6) == 0)
cd1737
+			addr += 6;
cd1737
+
cd1737
+		if (is_port)
cd1737
+			port = addr;
cd1737
+		else {
cd1737
+			port = strchr(addr, ':');
cd1737
+			if (port) {
cd1737
+				*port = '\0';
cd1737
+				port++;
cd1737
+			}
cd1737
+		}
cd1737
+
cd1737
+		if (port && strcmp(port, "*") &&
cd1737
+		    get_u32((__u32 *)&a.port, port, 0))
cd1737
+			return NULL;
cd1737
+
cd1737
+		if (addr[0] && strcmp(addr, "*")) {
cd1737
+			a.addr.bitlen = 32;
cd1737
+			if (get_u32(&cid, addr, 0))
cd1737
+				return NULL;
cd1737
+		}
cd1737
+		vsock_set_inet_prefix(&a.addr, cid);
cd1737
+		fam = AF_VSOCK;
cd1737
+		goto out;
cd1737
+	}
cd1737
+
cd1737
 	if (fam == AF_INET || !strncmp(addr, "inet:", 5)) {
cd1737
 		fam = AF_INET;
cd1737
 		if (!strncmp(addr, "inet:", 5))
cd1737
@@ -3653,6 +3728,88 @@ static int netlink_show(struct filter *f)
cd1737
 	return 0;
cd1737
 }
cd1737
 
cd1737
+static bool vsock_type_skip(struct sockstat *s, struct filter *f)
cd1737
+{
cd1737
+	if (s->type == SOCK_STREAM && !(f->dbs & (1 << VSOCK_ST_DB)))
cd1737
+		return true;
cd1737
+	if (s->type == SOCK_DGRAM && !(f->dbs & (1 << VSOCK_DG_DB)))
cd1737
+		return true;
cd1737
+	return false;
cd1737
+}
cd1737
+
cd1737
+static void vsock_addr_print(inet_prefix *a, __u32 port)
cd1737
+{
cd1737
+	char cid_str[sizeof("4294967295")];
cd1737
+	char port_str[sizeof("4294967295")];
cd1737
+	__u32 cid;
cd1737
+
cd1737
+	memcpy(&cid, a->data, sizeof(cid));
cd1737
+
cd1737
+	if (cid == ~(__u32)0)
cd1737
+		snprintf(cid_str, sizeof(cid_str), "*");
cd1737
+	else
cd1737
+		snprintf(cid_str, sizeof(cid_str), "%u", cid);
cd1737
+
cd1737
+	if (port == ~(__u32)0)
cd1737
+		snprintf(port_str, sizeof(port_str), "*");
cd1737
+	else
cd1737
+		snprintf(port_str, sizeof(port_str), "%u", port);
cd1737
+
cd1737
+	sock_addr_print(cid_str, ":", port_str, NULL);
cd1737
+}
cd1737
+
cd1737
+static void vsock_stats_print(struct sockstat *s, struct filter *f)
cd1737
+{
cd1737
+	sock_state_print(s);
cd1737
+
cd1737
+	vsock_addr_print(&s->local, s->lport);
cd1737
+	vsock_addr_print(&s->remote, s->rport);
cd1737
+
cd1737
+	proc_ctx_print(s);
cd1737
+
cd1737
+	printf("\n");
cd1737
+}
cd1737
+
cd1737
+static int vsock_show_sock(const struct sockaddr_nl *addr,
cd1737
+			   struct nlmsghdr *nlh, void *arg)
cd1737
+{
cd1737
+	struct filter *f = (struct filter *)arg;
cd1737
+	struct vsock_diag_msg *r = NLMSG_DATA(nlh);
cd1737
+	struct sockstat stat = {
cd1737
+		.type = r->vdiag_type,
cd1737
+		.lport = r->vdiag_src_port,
cd1737
+		.rport = r->vdiag_dst_port,
cd1737
+		.state = r->vdiag_state,
cd1737
+		.ino = r->vdiag_ino,
cd1737
+	};
cd1737
+
cd1737
+	vsock_set_inet_prefix(&stat.local, r->vdiag_src_cid);
cd1737
+	vsock_set_inet_prefix(&stat.remote, r->vdiag_dst_cid);
cd1737
+
cd1737
+	if (vsock_type_skip(&stat, f))
cd1737
+		return 0;
cd1737
+
cd1737
+	if (f->f && run_ssfilter(f->f, &stat) == 0)
cd1737
+		return 0;
cd1737
+
cd1737
+	vsock_stats_print(&stat, f);
cd1737
+
cd1737
+	return 0;
cd1737
+}
cd1737
+
cd1737
+static int vsock_show(struct filter *f)
cd1737
+{
cd1737
+	DIAG_REQUEST(req, struct vsock_diag_req r);
cd1737
+
cd1737
+	if (!filter_af_get(f, AF_VSOCK))
cd1737
+		return 0;
cd1737
+
cd1737
+	req.r.sdiag_family = AF_VSOCK;
cd1737
+	req.r.vdiag_states = f->states;
cd1737
+
cd1737
+	return handle_netlink_request(f, &req.nlh, sizeof(req), vsock_show_sock);
cd1737
+}
cd1737
+
cd1737
 struct sock_diag_msg {
cd1737
 	__u8 sdiag_family;
cd1737
 };
cd1737
@@ -3673,6 +3830,8 @@ static int generic_show_sock(const struct sockaddr_nl *addr,
cd1737
 		return packet_show_sock(addr, nlh, arg);
cd1737
 	case AF_NETLINK:
cd1737
 		return netlink_show_sock(addr, nlh, arg);
cd1737
+	case AF_VSOCK:
cd1737
+		return vsock_show_sock(addr, nlh, arg);
cd1737
 	default:
cd1737
 		return -1;
cd1737
 	}
cd1737
@@ -3900,14 +4059,15 @@ static void _usage(FILE *dest)
cd1737
 "   -d, --dccp          display only DCCP sockets\n"
cd1737
 "   -w, --raw           display only RAW sockets\n"
cd1737
 "   -x, --unix          display only Unix domain sockets\n"
cd1737
+"       --vsock         display only vsock sockets\n"
cd1737
 "   -f, --family=FAMILY display sockets of type FAMILY\n"
cd1737
-"       FAMILY := {inet|inet6|link|unix|netlink|help}\n"
cd1737
+"       FAMILY := {inet|inet6|link|unix|netlink|vsock|help}\n"
cd1737
 "\n"
cd1737
 "   -K, --kill          forcibly close sockets, display what was closed\n"
cd1737
 "   -H, --no-header     Suppress header line\n"
cd1737
 "\n"
cd1737
 "   -A, --query=QUERY, --socket=QUERY\n"
cd1737
-"       QUERY := {all|inet|tcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink}[,QUERY]\n"
cd1737
+"       QUERY := {all|inet|tcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink|vsock_stream|vsock_dgram}[,QUERY]\n"
cd1737
 "\n"
cd1737
 "   -D, --diag=FILE     Dump raw information about TCP sockets to FILE\n"
cd1737
 "   -F, --filter=FILE   read filter information from FILE\n"
cd1737
@@ -3980,6 +4140,9 @@ static int scan_state(const char *state)
cd1737
 	exit(-1);
cd1737
 }
cd1737
 
cd1737
+/* Values 'v' and 'V' are already used so a non-character is used */
cd1737
+#define OPT_VSOCK 256
cd1737
+
cd1737
 static const struct option long_opts[] = {
cd1737
 	{ "numeric", 0, 0, 'n' },
cd1737
 	{ "resolve", 0, 0, 'r' },
cd1737
@@ -3996,6 +4159,7 @@ static const struct option long_opts[] = {
cd1737
 	{ "udp", 0, 0, 'u' },
cd1737
 	{ "raw", 0, 0, 'w' },
cd1737
 	{ "unix", 0, 0, 'x' },
cd1737
+	{ "vsock", 0, 0, OPT_VSOCK },
cd1737
 	{ "all", 0, 0, 'a' },
cd1737
 	{ "listening", 0, 0, 'l' },
cd1737
 	{ "ipv4", 0, 0, '4' },
cd1737
@@ -4081,6 +4245,9 @@ int main(int argc, char *argv[])
cd1737
 		case 'x':
cd1737
 			filter_af_set(&current_filter, AF_UNIX);
cd1737
 			break;
cd1737
+		case OPT_VSOCK:
cd1737
+			filter_af_set(&current_filter, AF_VSOCK);
cd1737
+			break;
cd1737
 		case 'a':
cd1737
 			state_filter = SS_ALL;
cd1737
 			break;
cd1737
@@ -4107,6 +4274,8 @@ int main(int argc, char *argv[])
cd1737
 				filter_af_set(&current_filter, AF_UNIX);
cd1737
 			else if (strcmp(optarg, "netlink") == 0)
cd1737
 				filter_af_set(&current_filter, AF_NETLINK);
cd1737
+			else if (strcmp(optarg, "vsock") == 0)
cd1737
+				filter_af_set(&current_filter, AF_VSOCK);
cd1737
 			else if (strcmp(optarg, "help") == 0)
cd1737
 				help();
cd1737
 			else {
cd1737
@@ -4172,6 +4341,15 @@ int main(int argc, char *argv[])
cd1737
 					filter_db_set(&current_filter, PACKET_DG_DB);
cd1737
 				} else if (strcmp(p, "netlink") == 0) {
cd1737
 					filter_db_set(&current_filter, NETLINK_DB);
cd1737
+				} else if (strcmp(p, "vsock") == 0) {
cd1737
+					filter_db_set(&current_filter, VSOCK_ST_DB);
cd1737
+					filter_db_set(&current_filter, VSOCK_DG_DB);
cd1737
+				} else if (strcmp(p, "vsock_stream") == 0 ||
cd1737
+					   strcmp(p, "v_str") == 0) {
cd1737
+					filter_db_set(&current_filter, VSOCK_ST_DB);
cd1737
+				} else if (strcmp(p, "vsock_dgram") == 0 ||
cd1737
+					   strcmp(p, "v_dgr") == 0) {
cd1737
+					filter_db_set(&current_filter, VSOCK_DG_DB);
cd1737
 				} else {
cd1737
 					fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p);
cd1737
 					usage();
cd1737
@@ -4387,6 +4565,8 @@ int main(int argc, char *argv[])
cd1737
 		dccp_show(&current_filter);
cd1737
 	if (current_filter.dbs & (1<
cd1737
 		sctp_show(&current_filter);
cd1737
+	if (current_filter.dbs & VSOCK_DBM)
cd1737
+		vsock_show(&current_filter);
cd1737
 
cd1737
 	if (show_users || show_proc_ctx || show_sock_ctx)
cd1737
 		user_ent_destroy();
cd1737
-- 
cd1737
1.8.3.1
cd1737