Blame SOURCES/0015-vdpa-Allow-for-printing-negotiated-features-of-a-dev.patch

7570e2
From 5a42cc6a5ec79fd0f93904f19b8954c772d931eb Mon Sep 17 00:00:00 2001
7570e2
Message-Id: <5a42cc6a5ec79fd0f93904f19b8954c772d931eb.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:16 +0100
7570e2
Subject: [PATCH] vdpa: Allow for printing negotiated features of a device
7570e2
7570e2
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2059427
7570e2
Upstream Status: iproute2-next.git commit bd91c764
7570e2
7570e2
commit bd91c764718997adaa1d86eee5c585e67ca85356
7570e2
Author: Eli Cohen <elic@nvidia.com>
7570e2
Date:   Sun Mar 13 19:12:17 2022 +0200
7570e2
7570e2
    vdpa: Allow for printing negotiated features of a device
7570e2
7570e2
    When reading the configuration of a vdpa device, check if the
7570e2
    VDPA_ATTR_DEV_NEGOTIATED_FEATURES is available. If it is, parse the
7570e2
    feature bits and print a string representation of each of the feature
7570e2
    bits.
7570e2
7570e2
    We keep the strings in two different arrays. One for net device related
7570e2
    devices and one for generic feature bits.
7570e2
7570e2
    In this patch we parse only net device specific features. Support for
7570e2
    other devices can be added later. If the device queried is not a net
7570e2
    device, we print its bit number only.
7570e2
7570e2
    Examples:
7570e2
    1. Standard presentation
7570e2
    $ vdpa dev config show vdpa-a
7570e2
    vdpa-a: mac 00:00:00:00:88:88 link up link_announce false max_vq_pairs 2 mtu 9000
7570e2
      negotiated_features CSUM GUEST_CSUM MTU MAC HOST_TSO4 HOST_TSO6 STATUS \
7570e2
    CTRL_VQ MQ CTRL_MAC_ADDR VERSION_1 ACCESS_PLATFORM
7570e2
7570e2
    2. json output
7570e2
    $ vdpa -j dev config show vdpa-a
7570e2
    {"config":{"vdpa-a":{"mac":"00:00:00:00:88:88","link":"up","link_announce":false,\
7570e2
    "max_vq_pairs":2,"mtu":9000,"negotiated_features":["CSUM","GUEST_CSUM",\
7570e2
    "MTU","MAC","HOST_TSO4","HOST_TSO6","STATUS","CTRL_VQ","MQ","CTRL_MAC_ADDR",\
7570e2
    "VERSION_1","ACCESS_PLATFORM"]}}}
7570e2
7570e2
    3. Pretty json
7570e2
    $ vdpa -jp dev config show vdpa-a
7570e2
    {
7570e2
        "config": {
7570e2
            "vdpa-a": {
7570e2
                "mac": "00:00:00:00:88:88",
7570e2
                "link ": "up",
7570e2
                "link_announce ": false,
7570e2
                "max_vq_pairs": 2,
7570e2
                "mtu": 9000,
7570e2
                "negotiated_features": [
7570e2
    "CSUM","GUEST_CSUM","MTU","MAC","HOST_TSO4","HOST_TSO6","STATUS","CTRL_VQ",\
7570e2
    "MQ","CTRL_MAC_ADDR","VERSION_1","ACCESS_PLATFORM" ]
7570e2
            }
7570e2
        }
7570e2
    }
7570e2
7570e2
    Reviewed-by: Si-Wei Liu<si-wei.liu@oracle.com>
7570e2
    Acked-by: Jason Wang <jasowang@redhat.com>
7570e2
    Signed-off-by: Eli Cohen <elic@nvidia.com>
7570e2
    Signed-off-by: David Ahern <dsahern@kernel.org>
7570e2
---
7570e2
 vdpa/vdpa.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++-
7570e2
 1 file changed, 103 insertions(+), 2 deletions(-)
7570e2
7570e2
diff --git a/vdpa/vdpa.c b/vdpa/vdpa.c
7570e2
index 4ccb5648..40078b1c 100644
7570e2
--- a/vdpa/vdpa.c
7570e2
+++ b/vdpa/vdpa.c
7570e2
@@ -10,6 +10,8 @@
7570e2
 #include <linux/virtio_net.h>
7570e2
 #include <linux/netlink.h>
7570e2
 #include <libmnl/libmnl.h>
7570e2
+#include <linux/virtio_ring.h>
7570e2
+#include <linux/virtio_config.h>
7570e2
 #include "mnl_utils.h"
7570e2
 #include <rt_names.h>
7570e2
 
7570e2
@@ -78,6 +80,7 @@ static const enum mnl_attr_data_type vdpa_policy[VDPA_ATTR_MAX + 1] = {
7570e2
 	[VDPA_ATTR_DEV_VENDOR_ID] = MNL_TYPE_U32,
7570e2
 	[VDPA_ATTR_DEV_MAX_VQS] = MNL_TYPE_U32,
7570e2
 	[VDPA_ATTR_DEV_MAX_VQ_SIZE] = MNL_TYPE_U16,
7570e2
+	[VDPA_ATTR_DEV_NEGOTIATED_FEATURES] = MNL_TYPE_U64,
7570e2
 };
7570e2
 
7570e2
 static int attr_cb(const struct nlattr *attr, void *data)
7570e2
@@ -385,6 +388,94 @@ static const char *parse_class(int num)
7570e2
 	return class ? class : "< unknown class >";
7570e2
 }
7570e2
 
7570e2
+static const char * const net_feature_strs[64] = {
7570e2
+	[VIRTIO_NET_F_CSUM] = "CSUM",
7570e2
+	[VIRTIO_NET_F_GUEST_CSUM] = "GUEST_CSUM",
7570e2
+	[VIRTIO_NET_F_CTRL_GUEST_OFFLOADS] = "CTRL_GUEST_OFFLOADS",
7570e2
+	[VIRTIO_NET_F_MTU] = "MTU",
7570e2
+	[VIRTIO_NET_F_MAC] = "MAC",
7570e2
+	[VIRTIO_NET_F_GUEST_TSO4] = "GUEST_TSO4",
7570e2
+	[VIRTIO_NET_F_GUEST_TSO6] = "GUEST_TSO6",
7570e2
+	[VIRTIO_NET_F_GUEST_ECN] = "GUEST_ECN",
7570e2
+	[VIRTIO_NET_F_GUEST_UFO] = "GUEST_UFO",
7570e2
+	[VIRTIO_NET_F_HOST_TSO4] = "HOST_TSO4",
7570e2
+	[VIRTIO_NET_F_HOST_TSO6] = "HOST_TSO6",
7570e2
+	[VIRTIO_NET_F_HOST_ECN] = "HOST_ECN",
7570e2
+	[VIRTIO_NET_F_HOST_UFO] = "HOST_UFO",
7570e2
+	[VIRTIO_NET_F_MRG_RXBUF] = "MRG_RXBUF",
7570e2
+	[VIRTIO_NET_F_STATUS] = "STATUS",
7570e2
+	[VIRTIO_NET_F_CTRL_VQ] = "CTRL_VQ",
7570e2
+	[VIRTIO_NET_F_CTRL_RX] = "CTRL_RX",
7570e2
+	[VIRTIO_NET_F_CTRL_VLAN] = "CTRL_VLAN",
7570e2
+	[VIRTIO_NET_F_CTRL_RX_EXTRA] = "CTRL_RX_EXTRA",
7570e2
+	[VIRTIO_NET_F_GUEST_ANNOUNCE] = "GUEST_ANNOUNCE",
7570e2
+	[VIRTIO_NET_F_MQ] = "MQ",
7570e2
+	[VIRTIO_F_NOTIFY_ON_EMPTY] = "NOTIFY_ON_EMPTY",
7570e2
+	[VIRTIO_NET_F_CTRL_MAC_ADDR] = "CTRL_MAC_ADDR",
7570e2
+	[VIRTIO_F_ANY_LAYOUT] = "ANY_LAYOUT",
7570e2
+	[VIRTIO_NET_F_RSC_EXT] = "RSC_EXT",
7570e2
+	[VIRTIO_NET_F_HASH_REPORT] = "HASH_REPORT",
7570e2
+	[VIRTIO_NET_F_RSS] = "RSS",
7570e2
+	[VIRTIO_NET_F_STANDBY] = "STANDBY",
7570e2
+	[VIRTIO_NET_F_SPEED_DUPLEX] = "SPEED_DUPLEX",
7570e2
+};
7570e2
+
7570e2
+#define VIRTIO_F_IN_ORDER 35
7570e2
+#define VIRTIO_F_NOTIFICATION_DATA 38
7570e2
+#define VDPA_EXT_FEATURES_SZ (VIRTIO_TRANSPORT_F_END - \
7570e2
+			      VIRTIO_TRANSPORT_F_START + 1)
7570e2
+
7570e2
+static const char * const ext_feature_strs[VDPA_EXT_FEATURES_SZ] = {
7570e2
+	[VIRTIO_RING_F_INDIRECT_DESC - VIRTIO_TRANSPORT_F_START] = "RING_INDIRECT_DESC",
7570e2
+	[VIRTIO_RING_F_EVENT_IDX - VIRTIO_TRANSPORT_F_START] = "RING_EVENT_IDX",
7570e2
+	[VIRTIO_F_VERSION_1 - VIRTIO_TRANSPORT_F_START] = "VERSION_1",
7570e2
+	[VIRTIO_F_ACCESS_PLATFORM - VIRTIO_TRANSPORT_F_START] = "ACCESS_PLATFORM",
7570e2
+	[VIRTIO_F_RING_PACKED - VIRTIO_TRANSPORT_F_START] = "RING_PACKED",
7570e2
+	[VIRTIO_F_IN_ORDER - VIRTIO_TRANSPORT_F_START] = "IN_ORDER",
7570e2
+	[VIRTIO_F_ORDER_PLATFORM - VIRTIO_TRANSPORT_F_START] = "ORDER_PLATFORM",
7570e2
+	[VIRTIO_F_SR_IOV - VIRTIO_TRANSPORT_F_START] = "SR_IOV",
7570e2
+	[VIRTIO_F_NOTIFICATION_DATA - VIRTIO_TRANSPORT_F_START] = "NOTIFICATION_DATA",
7570e2
+};
7570e2
+
7570e2
+static const char * const *dev_to_feature_str[] = {
7570e2
+	[VIRTIO_ID_NET] = net_feature_strs,
7570e2
+};
7570e2
+
7570e2
+#define NUM_FEATURE_BITS 64
7570e2
+
7570e2
+static void print_features(struct vdpa *vdpa, uint64_t features, bool mgmtdevf,
7570e2
+			   uint16_t dev_id)
7570e2
+{
7570e2
+	const char * const *feature_strs = NULL;
7570e2
+	const char *s;
7570e2
+	int i;
7570e2
+
7570e2
+	if (dev_id < ARRAY_SIZE(dev_to_feature_str))
7570e2
+		feature_strs = dev_to_feature_str[dev_id];
7570e2
+
7570e2
+	if (mgmtdevf)
7570e2
+		pr_out_array_start(vdpa, "dev_features");
7570e2
+	else
7570e2
+		pr_out_array_start(vdpa, "negotiated_features");
7570e2
+
7570e2
+	for (i = 0; i < NUM_FEATURE_BITS; i++) {
7570e2
+		if (!(features & (1ULL << i)))
7570e2
+			continue;
7570e2
+
7570e2
+		if (i < VIRTIO_TRANSPORT_F_START || i > VIRTIO_TRANSPORT_F_END)
7570e2
+			s = feature_strs ? feature_strs[i] : NULL;
7570e2
+		else
7570e2
+			s = ext_feature_strs[i - VIRTIO_TRANSPORT_F_START];
7570e2
+
7570e2
+		if (!s)
7570e2
+			print_uint(PRINT_ANY, NULL, " bit_%d", i);
7570e2
+		else
7570e2
+			print_string(PRINT_ANY, NULL, " %s", s);
7570e2
+	}
7570e2
+
7570e2
+	pr_out_array_end(vdpa);
7570e2
+}
7570e2
+
7570e2
 static void pr_out_mgmtdev_show(struct vdpa *vdpa, const struct nlmsghdr *nlh,
7570e2
 				struct nlattr **tb)
7570e2
 {
7570e2
@@ -579,9 +670,10 @@ 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
+static void pr_out_dev_net_config(struct vdpa *vdpa, struct nlattr **tb)
7570e2
 {
7570e2
 	SPRINT_BUF(macaddr);
7570e2
+	uint64_t val_u64;
7570e2
 	uint16_t val_u16;
7570e2
 
7570e2
 	if (tb[VDPA_ATTR_DEV_NET_CFG_MACADDR]) {
7570e2
@@ -610,6 +702,15 @@ static void pr_out_dev_net_config(struct nlattr **tb)
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
+	if (tb[VDPA_ATTR_DEV_NEGOTIATED_FEATURES]) {
7570e2
+		uint16_t dev_id = 0;
7570e2
+
7570e2
+		if (tb[VDPA_ATTR_DEV_ID])
7570e2
+			dev_id = mnl_attr_get_u32(tb[VDPA_ATTR_DEV_ID]);
7570e2
+
7570e2
+		val_u64 = mnl_attr_get_u64(tb[VDPA_ATTR_DEV_NEGOTIATED_FEATURES]);
7570e2
+		print_features(vdpa, val_u64, false, dev_id);
7570e2
+	}
7570e2
 }
7570e2
 
7570e2
 static void pr_out_dev_config(struct vdpa *vdpa, struct nlattr **tb)
7570e2
@@ -619,7 +720,7 @@ static void pr_out_dev_config(struct vdpa *vdpa, struct nlattr **tb)
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
+		pr_out_dev_net_config(vdpa, tb);
7570e2
 		break;
7570e2
 	default:
7570e2
 		break;
7570e2
-- 
7570e2
2.35.1
7570e2