Blame SOURCES/0011-vdpa-Enable-user-to-query-vdpa-device-config-layout.patch

7570e2
From 492d698656851b27349dc64a8f0c2f66057eea52 Mon Sep 17 00:00:00 2001
7570e2
Message-Id: <492d698656851b27349dc64a8f0c2f66057eea52.1647984433.git.aclaudi@redhat.com>
7570e2
In-Reply-To: <cef782ca658d695c5ca2d174ba1f89cba6bd84e5.1647984433.git.aclaudi@redhat.com>
7570e2
References: <cef782ca658d695c5ca2d174ba1f89cba6bd84e5.1647984433.git.aclaudi@redhat.com>
7570e2
From: Andrea Claudi <aclaudi@redhat.com>
7570e2
Date: Mon, 21 Mar 2022 16:35:15 +0100
7570e2
Subject: [PATCH] vdpa: Enable user to query vdpa device config layout
7570e2
7570e2
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2059427
7570e2
Upstream Status: iproute2-next.git commit a311f0c4
7570e2
7570e2
commit a311f0c43a67be939dfafda563453a3f9bf30e42
7570e2
Author: Parav Pandit <parav@nvidia.com>
7570e2
Date:   Fri Dec 17 10:08:25 2021 +0200
7570e2
7570e2
    vdpa: Enable user to query vdpa device config layout
7570e2
7570e2
    Query the device configuration layout whenever kernel supports it.
7570e2
7570e2
    An example of configuration layout of vdpa device of type network:
7570e2
7570e2
    $ vdpa dev add name bar mgmtdev vdpasim_net
7570e2
7570e2
    $ vdpa dev config show
7570e2
    bar: mac 00:35:09:19:48:05 link up link_announce false mtu 1500
7570e2
7570e2
    $ vdpa dev config show -jp
7570e2
    {
7570e2
        "config": {
7570e2
            "bar": {
7570e2
                "mac": "00:35:09:19:48:05",
7570e2
                "link ": "up",
7570e2
                "link_announce ": false,
7570e2
                "mtu": 1500,
7570e2
            }
7570e2
        }
7570e2
    }
7570e2
7570e2
    Signed-off-by: Parav Pandit <parav@nvidia.com>
7570e2
    Signed-off-by: David Ahern <dsahern@kernel.org>
7570e2
---
7570e2
 man/man8/vdpa-dev.8 |  21 +++++++++
7570e2
 vdpa/vdpa.c         | 110 ++++++++++++++++++++++++++++++++++++++++++++
7570e2
 2 files changed, 131 insertions(+)
7570e2
7570e2
diff --git a/man/man8/vdpa-dev.8 b/man/man8/vdpa-dev.8
7570e2
index 36433519..5d3a3f26 100644
7570e2
--- a/man/man8/vdpa-dev.8
7570e2
+++ b/man/man8/vdpa-dev.8
7570e2
@@ -36,6 +36,10 @@ vdpa-dev \- vdpa device configuration
7570e2
 .B vdpa dev del
7570e2
 .I DEV
7570e2
 
7570e2
+.ti -8
7570e2
+.B vdpa dev config show
7570e2
+.RI "[ " DEV " ]"
7570e2
+
7570e2
 .SH "DESCRIPTION"
7570e2
 .SS vdpa dev show - display vdpa device attributes
7570e2
 
7570e2
@@ -65,6 +69,18 @@ Name of the management device to use for device addition.
7570e2
 .I "DEV"
7570e2
 - specifies the vdpa device to delete.
7570e2
 
7570e2
+.SS vdpa dev config show - Show configuration of specific device or all devices.
7570e2
+
7570e2
+.PP
7570e2
+.I "DEV"
7570e2
+- specifies the vdpa device to show its configuration.
7570e2
+If this argument is omitted all devices configuration is listed.
7570e2
+
7570e2
+.in +4
7570e2
+Format is:
7570e2
+.in +2
7570e2
+VDPA_DEVICE_NAME
7570e2
+
7570e2
 .SH "EXAMPLES"
7570e2
 .PP
7570e2
 vdpa dev show
7570e2
@@ -86,6 +102,11 @@ vdpa dev del foo
7570e2
 .RS 4
7570e2
 Delete the vdpa device named foo which was previously created.
7570e2
 .RE
7570e2
+.PP
7570e2
+vdpa dev config show foo
7570e2
+.RS 4
7570e2
+Shows the vdpa device configuration of device named foo.
7570e2
+.RE
7570e2
 
7570e2
 .SH SEE ALSO
7570e2
 .BR vdpa (8),
7570e2
diff --git a/vdpa/vdpa.c b/vdpa/vdpa.c
7570e2
index 7fdb36b9..ba704254 100644
7570e2
--- a/vdpa/vdpa.c
7570e2
+++ b/vdpa/vdpa.c
7570e2
@@ -6,9 +6,11 @@
7570e2
 #include <linux/genetlink.h>
7570e2
 #include <linux/vdpa.h>
7570e2
 #include <linux/virtio_ids.h>
7570e2
+#include <linux/virtio_net.h>
7570e2
 #include <linux/netlink.h>
7570e2
 #include <libmnl/libmnl.h>
7570e2
 #include "mnl_utils.h"
7570e2
+#include <rt_names.h>
7570e2
 
7570e2
 #include "version.h"
7570e2
 #include "json_print.h"
7570e2
@@ -413,6 +415,7 @@ static void cmd_dev_help(void)
7570e2
 	fprintf(stderr, "Usage: vdpa dev show [ DEV ]\n");
7570e2
 	fprintf(stderr, "       vdpa dev add name NAME mgmtdev MANAGEMENTDEV\n");
7570e2
 	fprintf(stderr, "       vdpa dev del DEV\n");
7570e2
+	fprintf(stderr, "Usage: vdpa dev config COMMAND [ OPTIONS ]\n");
7570e2
 }
7570e2
 
7570e2
 static const char *device_type_name(uint32_t type)
7570e2
@@ -520,6 +523,111 @@ static int cmd_dev_del(struct vdpa *vdpa,  int argc, char **argv)
7570e2
 	return mnlu_gen_socket_sndrcv(&vdpa->nlg, nlh, NULL, NULL);
7570e2
 }
7570e2
 
7570e2
+static void pr_out_dev_net_config(struct nlattr **tb)
7570e2
+{
7570e2
+	SPRINT_BUF(macaddr);
7570e2
+	uint16_t val_u16;
7570e2
+
7570e2
+	if (tb[VDPA_ATTR_DEV_NET_CFG_MACADDR]) {
7570e2
+		const unsigned char *data;
7570e2
+		uint16_t len;
7570e2
+
7570e2
+		len = mnl_attr_get_payload_len(tb[VDPA_ATTR_DEV_NET_CFG_MACADDR]);
7570e2
+		data = mnl_attr_get_payload(tb[VDPA_ATTR_DEV_NET_CFG_MACADDR]);
7570e2
+
7570e2
+		print_string(PRINT_ANY, "mac", "mac %s ",
7570e2
+			     ll_addr_n2a(data, len, 0, macaddr, sizeof(macaddr)));
7570e2
+	}
7570e2
+	if (tb[VDPA_ATTR_DEV_NET_STATUS]) {
7570e2
+		val_u16 = mnl_attr_get_u16(tb[VDPA_ATTR_DEV_NET_STATUS]);
7570e2
+		print_string(PRINT_ANY, "link ", "link %s ",
7570e2
+			     (val_u16 & VIRTIO_NET_S_LINK_UP) ? "up" : "down");
7570e2
+		print_bool(PRINT_ANY, "link_announce ", "link_announce %s ",
7570e2
+			     (val_u16 & VIRTIO_NET_S_ANNOUNCE) ? true : false);
7570e2
+	}
7570e2
+	if (tb[VDPA_ATTR_DEV_NET_CFG_MAX_VQP]) {
7570e2
+		val_u16 = mnl_attr_get_u16(tb[VDPA_ATTR_DEV_NET_CFG_MAX_VQP]);
7570e2
+		print_uint(PRINT_ANY, "max_vq_pairs", "max_vq_pairs %d ",
7570e2
+			     val_u16);
7570e2
+	}
7570e2
+	if (tb[VDPA_ATTR_DEV_NET_CFG_MTU]) {
7570e2
+		val_u16 = mnl_attr_get_u16(tb[VDPA_ATTR_DEV_NET_CFG_MTU]);
7570e2
+		print_uint(PRINT_ANY, "mtu", "mtu %d ", val_u16);
7570e2
+	}
7570e2
+}
7570e2
+
7570e2
+static void pr_out_dev_config(struct vdpa *vdpa, struct nlattr **tb)
7570e2
+{
7570e2
+	uint32_t device_id = mnl_attr_get_u32(tb[VDPA_ATTR_DEV_ID]);
7570e2
+
7570e2
+	pr_out_vdev_handle_start(vdpa, tb);
7570e2
+	switch (device_id) {
7570e2
+	case VIRTIO_ID_NET:
7570e2
+		pr_out_dev_net_config(tb);
7570e2
+		break;
7570e2
+	default:
7570e2
+		break;
7570e2
+	}
7570e2
+	pr_out_vdev_handle_end(vdpa);
7570e2
+}
7570e2
+
7570e2
+static int cmd_dev_config_show_cb(const struct nlmsghdr *nlh, void *data)
7570e2
+{
7570e2
+	struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
7570e2
+	struct nlattr *tb[VDPA_ATTR_MAX + 1] = {};
7570e2
+	struct vdpa *vdpa = data;
7570e2
+
7570e2
+	mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
7570e2
+	if (!tb[VDPA_ATTR_DEV_NAME] || !tb[VDPA_ATTR_DEV_ID])
7570e2
+		return MNL_CB_ERROR;
7570e2
+	pr_out_dev_config(vdpa, tb);
7570e2
+	return MNL_CB_OK;
7570e2
+}
7570e2
+
7570e2
+static int cmd_dev_config_show(struct vdpa *vdpa, int argc, char **argv)
7570e2
+{
7570e2
+	uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
7570e2
+	struct nlmsghdr *nlh;
7570e2
+	int err;
7570e2
+
7570e2
+	if (argc <= 0)
7570e2
+		flags |= NLM_F_DUMP;
7570e2
+
7570e2
+	nlh = mnlu_gen_socket_cmd_prepare(&vdpa->nlg, VDPA_CMD_DEV_CONFIG_GET,
7570e2
+					  flags);
7570e2
+	if (argc > 0) {
7570e2
+		err = vdpa_argv_parse_put(nlh, vdpa, argc, argv,
7570e2
+					  VDPA_OPT_VDEV_HANDLE);
7570e2
+		if (err)
7570e2
+			return err;
7570e2
+	}
7570e2
+
7570e2
+	pr_out_section_start(vdpa, "config");
7570e2
+	err = mnlu_gen_socket_sndrcv(&vdpa->nlg, nlh, cmd_dev_config_show_cb, vdpa);
7570e2
+	pr_out_section_end(vdpa);
7570e2
+	return err;
7570e2
+}
7570e2
+
7570e2
+static void cmd_dev_config_help(void)
7570e2
+{
7570e2
+	fprintf(stderr, "Usage: vdpa dev config show [ DEV ]\n");
7570e2
+}
7570e2
+
7570e2
+static int cmd_dev_config(struct vdpa *vdpa, int argc, char **argv)
7570e2
+{
7570e2
+	if (!argc)
7570e2
+		return cmd_dev_config_show(vdpa, argc - 1, argv + 1);
7570e2
+
7570e2
+	if (matches(*argv, "help") == 0) {
7570e2
+		cmd_dev_config_help();
7570e2
+		return 0;
7570e2
+	} else if (matches(*argv, "show") == 0) {
7570e2
+		return cmd_dev_config_show(vdpa, argc - 1, argv + 1);
7570e2
+	}
7570e2
+	fprintf(stderr, "Command \"%s\" not found\n", *argv);
7570e2
+	return -ENOENT;
7570e2
+}
7570e2
+
7570e2
 static int cmd_dev(struct vdpa *vdpa, int argc, char **argv)
7570e2
 {
7570e2
 	if (!argc)
7570e2
@@ -535,6 +643,8 @@ static int cmd_dev(struct vdpa *vdpa, int argc, char **argv)
7570e2
 		return cmd_dev_add(vdpa, argc - 1, argv + 1);
7570e2
 	} else if (matches(*argv, "del") == 0) {
7570e2
 		return cmd_dev_del(vdpa, argc - 1, argv + 1);
7570e2
+	} else if (matches(*argv, "config") == 0) {
7570e2
+		return cmd_dev_config(vdpa, argc - 1, argv + 1);
7570e2
 	}
7570e2
 	fprintf(stderr, "Command \"%s\" not found\n", *argv);
7570e2
 	return -ENOENT;
7570e2
-- 
7570e2
2.35.1
7570e2