Patch Detail
get:
Show a patch.
patch:
Update a patch.
put:
Update a patch.
GET /api/patches/110999/?format=api
https://patches.dpdk.org/api/patches/110999/?format=api", "web_url": "https://patches.dpdk.org/project/dpdk/patch/20220510201720.1262368-2-maxime.coquelin@redhat.com/", "project": { "id": 1, "url": "https://patches.dpdk.org/api/projects/1/?format=api", "name": "DPDK", "link_name": "dpdk", "list_id": "dev.dpdk.org", "list_email": "dev@dpdk.org", "web_url": "http://core.dpdk.org", "scm_url": "git://dpdk.org/dpdk", "webscm_url": "http://git.dpdk.org/dpdk", "list_archive_url": "https://inbox.dpdk.org/dev", "list_archive_url_format": "https://inbox.dpdk.org/dev/{}", "commit_url_format": "" }, "msgid": "<20220510201720.1262368-2-maxime.coquelin@redhat.com>", "list_archive_url": "https://inbox.dpdk.org/dev/20220510201720.1262368-2-maxime.coquelin@redhat.com", "date": "2022-05-10T20:17:16", "name": "[1/5] vhost: add per-virtqueue statistics support", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": true, "hash": "868acc63533ed061a4b66271c10d9c645a7ef83d", "submitter": { "id": 512, "url": "https://patches.dpdk.org/api/people/512/?format=api", "name": "Maxime Coquelin", "email": "maxime.coquelin@redhat.com" }, "delegate": { "id": 2642, "url": "https://patches.dpdk.org/api/users/2642/?format=api", "username": "mcoquelin", "first_name": "Maxime", "last_name": "Coquelin", "email": "maxime.coquelin@redhat.com" }, "mbox": "https://patches.dpdk.org/project/dpdk/patch/20220510201720.1262368-2-maxime.coquelin@redhat.com/mbox/", "series": [ { "id": 22875, "url": "https://patches.dpdk.org/api/series/22875/?format=api", "web_url": "https://patches.dpdk.org/project/dpdk/list/?series=22875", "date": "2022-05-10T20:17:16", "name": "vhost: introduce per-virtqueue stats API", "version": 1, "mbox": "https://patches.dpdk.org/series/22875/mbox/" } ], "comments": "https://patches.dpdk.org/api/patches/110999/comments/", "check": "warning", "checks": "https://patches.dpdk.org/api/patches/110999/checks/", "tags": {}, "related": [], "headers": { "Return-Path": "<dev-bounces@dpdk.org>", "X-Original-To": "patchwork@inbox.dpdk.org", "Delivered-To": "patchwork@inbox.dpdk.org", "Received": [ "from mails.dpdk.org (mails.dpdk.org [217.70.189.124])\n\tby inbox.dpdk.org (Postfix) with ESMTP id 144D7A0093;\n\tTue, 10 May 2022 22:17:29 +0200 (CEST)", "from [217.70.189.124] (localhost [127.0.0.1])\n\tby mails.dpdk.org (Postfix) with ESMTP id A7810410F2;\n\tTue, 10 May 2022 22:17:28 +0200 (CEST)", "from us-smtp-delivery-124.mimecast.com\n (us-smtp-delivery-124.mimecast.com [170.10.129.124])\n by mails.dpdk.org (Postfix) with ESMTP id 3DA36406B4\n for <dev@dpdk.org>; Tue, 10 May 2022 22:17:27 +0200 (CEST)", "from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com\n [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS\n (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id\n us-mta-31-8B4g2QX-P_quwhy0wLZRmA-1; Tue, 10 May 2022 16:17:25 -0400", "from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com\n [10.11.54.1])\n (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n (No client certificate requested)\n by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 30EEB8002BF;\n Tue, 10 May 2022 20:17:25 +0000 (UTC)", "from max-t490s.redhat.com (unknown [10.39.208.28])\n by smtp.corp.redhat.com (Postfix) with ESMTP id 0D7CC40CFD06;\n Tue, 10 May 2022 20:17:23 +0000 (UTC)" ], "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n s=mimecast20190719; t=1652213846;\n h=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n to:to:cc:cc:mime-version:mime-version:content-type:content-type:\n content-transfer-encoding:content-transfer-encoding:\n in-reply-to:in-reply-to:references:references;\n bh=HmsXkGPExsVWeGBd4XGp3ddz+GbOlp/Yfu2oFWNC3Fw=;\n b=Dihb2BlFs8ps+k7dc704+cu5BFA/hxpB6y2IAe80NWeKDNw/RXXV/Sk+0trvk0wF99dyfm\n c9HNXg2EJg9BdxAYXRFbEJ3ScmTTZICqPIJvd1h36gaaito7DLXjw4ZmAfn6YyT+3ngWg+\n 840XYohWSrN6vsd2XDQfybzX4f9RZYY=", "X-MC-Unique": "8B4g2QX-P_quwhy0wLZRmA-1", "From": "Maxime Coquelin <maxime.coquelin@redhat.com>", "To": "dev@dpdk.org, chenbo.xia@intel.com, david.marchand@redhat.com,\n i.maximets@ovn.org", "Cc": "Maxime Coquelin <maxime.coquelin@redhat.com>", "Subject": "[PATCH 1/5] vhost: add per-virtqueue statistics support", "Date": "Tue, 10 May 2022 22:17:16 +0200", "Message-Id": "<20220510201720.1262368-2-maxime.coquelin@redhat.com>", "In-Reply-To": "<20220510201720.1262368-1-maxime.coquelin@redhat.com>", "References": "<20220510201720.1262368-1-maxime.coquelin@redhat.com>", "MIME-Version": "1.0", "X-Scanned-By": "MIMEDefang 2.84 on 10.11.54.1", "Authentication-Results": "relay.mimecast.com;\n auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=maxime.coquelin@redhat.com", "X-Mimecast-Spam-Score": "0", "X-Mimecast-Originator": "redhat.com", "Content-Transfer-Encoding": "8bit", "Content-Type": "text/plain; charset=\"US-ASCII\"; x-default=true", "X-BeenThere": "dev@dpdk.org", "X-Mailman-Version": "2.1.29", "Precedence": "list", "List-Id": "DPDK patches and discussions <dev.dpdk.org>", "List-Unsubscribe": "<https://mails.dpdk.org/options/dev>,\n <mailto:dev-request@dpdk.org?subject=unsubscribe>", "List-Archive": "<http://mails.dpdk.org/archives/dev/>", "List-Post": "<mailto:dev@dpdk.org>", "List-Help": "<mailto:dev-request@dpdk.org?subject=help>", "List-Subscribe": "<https://mails.dpdk.org/listinfo/dev>,\n <mailto:dev-request@dpdk.org?subject=subscribe>", "Errors-To": "dev-bounces@dpdk.org" }, "content": "This patch introduces new APIs for the application\nto query and reset per-virtqueue statistics. The\npatch also introduces generic counters.\n\nSigned-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>\n---\n doc/guides/prog_guide/vhost_lib.rst | 24 ++++++\n lib/vhost/rte_vhost.h | 99 ++++++++++++++++++++++++\n lib/vhost/socket.c | 4 +-\n lib/vhost/version.map | 4 +-\n lib/vhost/vhost.c | 112 +++++++++++++++++++++++++++-\n lib/vhost/vhost.h | 18 ++++-\n lib/vhost/virtio_net.c | 55 ++++++++++++++\n 7 files changed, 311 insertions(+), 5 deletions(-)", "diff": "diff --git a/doc/guides/prog_guide/vhost_lib.rst b/doc/guides/prog_guide/vhost_lib.rst\nindex f287b76ebf..0f91215a90 100644\n--- a/doc/guides/prog_guide/vhost_lib.rst\n+++ b/doc/guides/prog_guide/vhost_lib.rst\n@@ -130,6 +130,15 @@ The following is an overview of some key Vhost API functions:\n \n It is disabled by default.\n \n+ - ``RTE_VHOST_USER_NET_STATS_ENABLE``\n+\n+ Per-virtqueue statistics collection will be enabled when this flag is set.\n+ When enabled, the application may use rte_vhost_stats_get_names() and\n+ rte_vhost_stats_get() to collect statistics, and rte_vhost_stats_reset() to\n+ reset them.\n+\n+ It is disabled by default\n+\n * ``rte_vhost_driver_set_features(path, features)``\n \n This function sets the feature bits the vhost-user driver supports. The\n@@ -282,6 +291,21 @@ The following is an overview of some key Vhost API functions:\n Clear inflight packets which are submitted to DMA engine in vhost async data\n path. Completed packets are returned to applications through ``pkts``.\n \n+* ``rte_vhost_vring_stats_get_names(int vid, uint16_t queue_id, struct rte_vhost_stat_name *names, unsigned int size)``\n+\n+ This function returns the names of the queue statistics. It requires\n+ statistics collection to be enabled at registration time.\n+\n+* ``rte_vhost_vring_stats_get(int vid, uint16_t queue_id, struct rte_vhost_stat *stats, unsigned int n)``\n+\n+ This function returns the queue statistics. It requires statistics\n+ collection to be enabled at registration time.\n+\n+* ``rte_vhost_vring_stats_reset(int vid, uint16_t queue_id)``\n+\n+ This function resets the queue statistics. It requires statistics\n+ collection to be enabled at registration time.\n+\n Vhost-user Implementations\n --------------------------\n \ndiff --git a/lib/vhost/rte_vhost.h b/lib/vhost/rte_vhost.h\nindex c733f857c6..266048f3e5 100644\n--- a/lib/vhost/rte_vhost.h\n+++ b/lib/vhost/rte_vhost.h\n@@ -39,6 +39,7 @@ extern \"C\" {\n #define RTE_VHOST_USER_LINEARBUF_SUPPORT\t(1ULL << 6)\n #define RTE_VHOST_USER_ASYNC_COPY\t(1ULL << 7)\n #define RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS\t(1ULL << 8)\n+#define RTE_VHOST_USER_NET_STATS_ENABLE\t(1ULL << 9)\n \n /* Features. */\n #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE\n@@ -321,6 +322,32 @@ struct rte_vhost_power_monitor_cond {\n \tuint8_t match;\n };\n \n+/** Maximum name length for the statistics counters */\n+#define RTE_VHOST_STATS_NAME_SIZE 64\n+\n+/**\n+ * Vhost virtqueue statistics structure\n+ *\n+ * This structure is used by rte_vhost_vring_stats_get() to provide\n+ * virtqueue statistics to the calling application.\n+ * It maps a name ID, corresponding to an index in the array returned\n+ * by rte_vhost_vring_stats_get_names(), to a statistic value.\n+ */\n+struct rte_vhost_stat {\n+\tuint64_t id; /**< The index in xstats name array. */\n+\tuint64_t value; /**< The statistic counter value. */\n+};\n+\n+/**\n+ * Vhost virtqueue statistic name element\n+ *\n+ * This structure is used by rte_vhost_vring_stats_get_names() to\n+ * provide virtqueue statistics names to the calling application.\n+ */\n+struct rte_vhost_stat_name {\n+\tchar name[RTE_VHOST_STATS_NAME_SIZE]; /**< The statistic name. */\n+};\n+\n /**\n * Convert guest physical address to host virtual address\n *\n@@ -1063,6 +1090,78 @@ __rte_experimental\n int\n rte_vhost_slave_config_change(int vid, bool need_reply);\n \n+/**\n+ * Retrieve names of statistics of a Vhost virtqueue.\n+ *\n+ * There is an assumption that 'stat_names' and 'stats' arrays are matched\n+ * by array index: stats_names[i].name => stats[i].value\n+ *\n+ * @param vid\n+ * vhost device ID\n+ * @param queue_id\n+ * vhost queue index\n+ * @param name\n+ * array of at least size elements to be filled.\n+ * If set to NULL, the function returns the required number of elements.\n+ * @param size\n+ * The number of elements in stats_names array.\n+ * @return\n+ * - Success if greater than 0 and lower or equal to *size*. The return value\n+ * indicates the number of elements filled in the *names* array.\n+ * - Failure if greater than *size*. The return value indicates the number of\n+ * elements the *names* array that should be given to succeed.\n+ * - Failure if lower than 0. The device ID or queue ID is invalid or\n+ + statistics collection is not enabled.\n+ */\n+__rte_experimental\n+int\n+rte_vhost_vring_stats_get_names(int vid, uint16_t queue_id,\n+\t\tstruct rte_vhost_stat_name *name, unsigned int size);\n+\n+/**\n+ * Retrieve statistics of a Vhost virtqueue.\n+ *\n+ * There is an assumption that 'stat_names' and 'stats' arrays are matched\n+ * by array index: stats_names[i].name => stats[i].value\n+ *\n+ * @param vid\n+ * vhost device ID\n+ * @param queue_id\n+ * vhost queue index\n+ * @param stats\n+ * A pointer to a table of structure of type rte_vhost_stat to be filled with\n+ * virtqueue statistics ids and values.\n+ * @param n\n+ * The number of elements in stats array.\n+ * @return\n+ * - Success if greater than 0 and lower or equal to *n*. The return value\n+ * indicates the number of elements filled in the *stats* array.\n+ * - Failure if greater than *n*. The return value indicates the number of\n+ * elements the *stats* array that should be given to succeed.\n+ * - Failure if lower than 0. The device ID or queue ID is invalid, or\n+ * statistics collection is not enabled.\n+ */\n+__rte_experimental\n+int\n+rte_vhost_vring_stats_get(int vid, uint16_t queue_id,\n+\t\tstruct rte_vhost_stat *stats, unsigned int n);\n+\n+/**\n+ * Reset statistics of a Vhost virtqueue.\n+ *\n+ * @param vid\n+ * vhost device ID\n+ * @param queue_id\n+ * vhost queue index\n+ * @return\n+ * - Success if 0. Statistics have been reset.\n+ * - Failure if lower than 0. The device ID or queue ID is invalid, or\n+ * statistics collection is not enabled.\n+ */\n+__rte_experimental\n+int\n+rte_vhost_vring_stats_reset(int vid, uint16_t queue_id);\n+\n #ifdef __cplusplus\n }\n #endif\ndiff --git a/lib/vhost/socket.c b/lib/vhost/socket.c\nindex b304339de9..baf6c338d3 100644\n--- a/lib/vhost/socket.c\n+++ b/lib/vhost/socket.c\n@@ -42,6 +42,7 @@ struct vhost_user_socket {\n \tbool linearbuf;\n \tbool async_copy;\n \tbool net_compliant_ol_flags;\n+\tbool stats_enabled;\n \n \t/*\n \t * The \"supported_features\" indicates the feature bits the\n@@ -227,7 +228,7 @@ vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)\n \tvhost_set_ifname(vid, vsocket->path, size);\n \n \tvhost_setup_virtio_net(vid, vsocket->use_builtin_virtio_net,\n-\t\tvsocket->net_compliant_ol_flags);\n+\t\tvsocket->net_compliant_ol_flags, vsocket->stats_enabled);\n \n \tvhost_attach_vdpa_device(vid, vsocket->vdpa_dev);\n \n@@ -863,6 +864,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)\n \tvsocket->linearbuf = flags & RTE_VHOST_USER_LINEARBUF_SUPPORT;\n \tvsocket->async_copy = flags & RTE_VHOST_USER_ASYNC_COPY;\n \tvsocket->net_compliant_ol_flags = flags & RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS;\n+\tvsocket->stats_enabled = flags & RTE_VHOST_USER_NET_STATS_ENABLE;\n \n \tif (vsocket->async_copy &&\n \t\t(flags & (RTE_VHOST_USER_IOMMU_SUPPORT |\ndiff --git a/lib/vhost/version.map b/lib/vhost/version.map\nindex 5841315386..34df41556d 100644\n--- a/lib/vhost/version.map\n+++ b/lib/vhost/version.map\n@@ -90,7 +90,9 @@ EXPERIMENTAL {\n \n \t# added in 22.07\n \trte_vhost_async_get_inflight_thread_unsafe;\n-\n+\trte_vhost_vring_stats_get_names;\n+\trte_vhost_vring_stats_get;\n+\trte_vhost_vring_stats_reset;\n };\n \n INTERNAL {\ndiff --git a/lib/vhost/vhost.c b/lib/vhost/vhost.c\nindex df0bb9d043..b22f10de72 100644\n--- a/lib/vhost/vhost.c\n+++ b/lib/vhost/vhost.c\n@@ -24,6 +24,28 @@\n struct virtio_net *vhost_devices[RTE_MAX_VHOST_DEVICE];\n pthread_mutex_t vhost_dev_lock = PTHREAD_MUTEX_INITIALIZER;\n \n+struct vhost_vq_stats_name_off {\n+\tchar name[RTE_VHOST_STATS_NAME_SIZE];\n+\tunsigned int offset;\n+};\n+\n+static const struct vhost_vq_stats_name_off vhost_vq_stat_strings[] = {\n+\t{\"good_packets\", offsetof(struct vhost_virtqueue, stats.packets)},\n+\t{\"good_bytes\", offsetof(struct vhost_virtqueue, stats.bytes)},\n+\t{\"multicast_packets\", offsetof(struct vhost_virtqueue, stats.multicast)},\n+\t{\"broadcast_packets\", offsetof(struct vhost_virtqueue, stats.broadcast)},\n+\t{\"undersize_packets\", offsetof(struct vhost_virtqueue, stats.size_bins[0])},\n+\t{\"size_64_packets\", offsetof(struct vhost_virtqueue, stats.size_bins[1])},\n+\t{\"size_65_127_packets\", offsetof(struct vhost_virtqueue, stats.size_bins[2])},\n+\t{\"size_128_255_packets\", offsetof(struct vhost_virtqueue, stats.size_bins[3])},\n+\t{\"size_256_511_packets\", offsetof(struct vhost_virtqueue, stats.size_bins[4])},\n+\t{\"size_512_1023_packets\", offsetof(struct vhost_virtqueue, stats.size_bins[5])},\n+\t{\"size_1024_1518_packets\", offsetof(struct vhost_virtqueue, stats.size_bins[6])},\n+\t{\"size_1519_max_packets\", offsetof(struct vhost_virtqueue, stats.size_bins[7])},\n+};\n+\n+#define VHOST_NB_VQ_STATS RTE_DIM(vhost_vq_stat_strings)\n+\n /* Called with iotlb_lock read-locked */\n uint64_t\n __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,\n@@ -755,7 +777,7 @@ vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)\n }\n \n void\n-vhost_setup_virtio_net(int vid, bool enable, bool compliant_ol_flags)\n+vhost_setup_virtio_net(int vid, bool enable, bool compliant_ol_flags, bool stats_enabled)\n {\n \tstruct virtio_net *dev = get_device(vid);\n \n@@ -770,6 +792,10 @@ vhost_setup_virtio_net(int vid, bool enable, bool compliant_ol_flags)\n \t\tdev->flags |= VIRTIO_DEV_LEGACY_OL_FLAGS;\n \telse\n \t\tdev->flags &= ~VIRTIO_DEV_LEGACY_OL_FLAGS;\n+\tif (stats_enabled)\n+\t\tdev->flags |= VIRTIO_DEV_STATS_ENABLED;\n+\telse\n+\t\tdev->flags &= ~VIRTIO_DEV_STATS_ENABLED;\n }\n \n void\n@@ -1971,5 +1997,89 @@ rte_vhost_get_monitor_addr(int vid, uint16_t queue_id,\n \treturn 0;\n }\n \n+\n+int\n+rte_vhost_vring_stats_get_names(int vid, uint16_t queue_id,\n+\t\tstruct rte_vhost_stat_name *name, unsigned int size)\n+{\n+\tstruct virtio_net *dev = get_device(vid);\n+\tunsigned int i;\n+\n+\tif (dev == NULL)\n+\t\treturn -1;\n+\n+\tif (queue_id >= dev->nr_vring)\n+\t\treturn -1;\n+\n+\tif (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))\n+\t\treturn -1;\n+\n+\tif (name == NULL || size < VHOST_NB_VQ_STATS)\n+\t\treturn VHOST_NB_VQ_STATS;\n+\n+\tfor (i = 0; i < VHOST_NB_VQ_STATS; i++)\n+\t\tsnprintf(name[i].name, sizeof(name[i].name), \"%s_q%u_%s\",\n+\t\t\t\t(queue_id & 1) ? \"rx\" : \"tx\",\n+\t\t\t\tqueue_id / 2, vhost_vq_stat_strings[i].name);\n+\n+\treturn VHOST_NB_VQ_STATS;\n+}\n+\n+int\n+rte_vhost_vring_stats_get(int vid, uint16_t queue_id,\n+\t\tstruct rte_vhost_stat *stats, unsigned int n)\n+{\n+\tstruct virtio_net *dev = get_device(vid);\n+\tstruct vhost_virtqueue *vq;\n+\tunsigned int i;\n+\n+\tif (dev == NULL)\n+\t\treturn -1;\n+\n+\tif (queue_id >= dev->nr_vring)\n+\t\treturn -1;\n+\n+\tif (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))\n+\t\treturn -1;\n+\n+\tif (stats == NULL || n < VHOST_NB_VQ_STATS)\n+\t\treturn VHOST_NB_VQ_STATS;\n+\n+\tvq = dev->virtqueue[queue_id];\n+\n+\trte_spinlock_lock(&vq->access_lock);\n+\tfor (i = 0; i < VHOST_NB_VQ_STATS; i++) {\n+\t\tstats[i].value =\n+\t\t\t*(uint64_t *)(((char *)vq) + vhost_vq_stat_strings[i].offset);\n+\t\tstats[i].id = i;\n+\t}\n+\trte_spinlock_unlock(&vq->access_lock);\n+\n+\treturn VHOST_NB_VQ_STATS;\n+}\n+\n+int rte_vhost_vring_stats_reset(int vid, uint16_t queue_id)\n+{\n+\tstruct virtio_net *dev = get_device(vid);\n+\tstruct vhost_virtqueue *vq;\n+\n+\tif (dev == NULL)\n+\t\treturn -1;\n+\n+\tif (queue_id >= dev->nr_vring)\n+\t\treturn -1;\n+\n+\tif (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))\n+\t\treturn -1;\n+\n+\tvq = dev->virtqueue[queue_id];\n+\n+\trte_spinlock_lock(&vq->access_lock);\n+\tmemset(&vq->stats, 0, sizeof(vq->stats));\n+\trte_spinlock_unlock(&vq->access_lock);\n+\n+\treturn 0;\n+}\n+\n RTE_LOG_REGISTER_SUFFIX(vhost_config_log_level, config, INFO);\n RTE_LOG_REGISTER_SUFFIX(vhost_data_log_level, data, WARNING);\ndiff --git a/lib/vhost/vhost.h b/lib/vhost/vhost.h\nindex a9edc271aa..01b97011aa 100644\n--- a/lib/vhost/vhost.h\n+++ b/lib/vhost/vhost.h\n@@ -37,6 +37,8 @@\n #define VIRTIO_DEV_FEATURES_FAILED ((uint32_t)1 << 4)\n /* Used to indicate that the virtio_net tx code should fill TX ol_flags */\n #define VIRTIO_DEV_LEGACY_OL_FLAGS ((uint32_t)1 << 5)\n+/* Used to indicate the application has requested statistics collection */\n+#define VIRTIO_DEV_STATS_ENABLED ((uint32_t)1 << 6)\n \n /* Backend value set by guest. */\n #define VIRTIO_DEV_STOPPED -1\n@@ -121,6 +123,18 @@ struct vring_used_elem_packed {\n \tuint32_t count;\n };\n \n+/**\n+ * Virtqueue statistics\n+ */\n+struct virtqueue_stats {\n+\tuint64_t packets;\n+\tuint64_t bytes;\n+\tuint64_t multicast;\n+\tuint64_t broadcast;\n+\t/* Size bins in array as RFC 2819, undersized [0], 64 [1], etc */\n+\tuint64_t size_bins[8];\n+};\n+\n /**\n * iovec\n */\n@@ -305,6 +319,7 @@ struct vhost_virtqueue {\n #define VIRTIO_UNINITIALIZED_NOTIF\t(-1)\n \n \tstruct vhost_vring_addr ring_addrs;\n+\tstruct virtqueue_stats\tstats;\n } __rte_cache_aligned;\n \n /* Virtio device status as per Virtio specification */\n@@ -780,7 +795,7 @@ int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);\n void vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *dev);\n \n void vhost_set_ifname(int, const char *if_name, unsigned int if_len);\n-void vhost_setup_virtio_net(int vid, bool enable, bool legacy_ol_flags);\n+void vhost_setup_virtio_net(int vid, bool enable, bool legacy_ol_flags, bool stats_enabled);\n void vhost_enable_extbuf(int vid);\n void vhost_enable_linearbuf(int vid);\n int vhost_enable_guest_notification(struct virtio_net *dev,\n@@ -957,5 +972,4 @@ mbuf_is_consumed(struct rte_mbuf *m)\n \n \treturn true;\n }\n-\n #endif /* _VHOST_NET_CDEV_H_ */\ndiff --git a/lib/vhost/virtio_net.c b/lib/vhost/virtio_net.c\nindex 5f432b0d77..b1ea9fa4a5 100644\n--- a/lib/vhost/virtio_net.c\n+++ b/lib/vhost/virtio_net.c\n@@ -47,6 +47,54 @@ is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)\n \treturn (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;\n }\n \n+/*\n+ * This function must be called with virtqueue's access_lock taken.\n+ */\n+static inline void\n+vhost_queue_stats_update(struct virtio_net *dev, struct vhost_virtqueue *vq,\n+\t\tstruct rte_mbuf **pkts, uint16_t count)\n+{\n+\tstruct virtqueue_stats *stats = &vq->stats;\n+\tint i;\n+\n+\tif (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))\n+\t\treturn;\n+\n+\tfor (i = 0; i < count; i++) {\n+\t\tstruct rte_ether_addr *ea;\n+\t\tstruct rte_mbuf *pkt = pkts[i];\n+\t\tuint32_t pkt_len = rte_pktmbuf_pkt_len(pkt);\n+\n+\t\tstats->packets++;\n+\t\tstats->bytes += pkt_len;\n+\n+\t\tif (pkt_len == 64) {\n+\t\t\tstats->size_bins[1]++;\n+\t\t} else if (pkt_len > 64 && pkt_len < 1024) {\n+\t\t\tuint32_t bin;\n+\n+\t\t\t/* count zeros, and offset into correct bin */\n+\t\t\tbin = (sizeof(pkt_len) * 8) - __builtin_clz(pkt_len) - 5;\n+\t\t\tstats->size_bins[bin]++;\n+\t\t} else {\n+\t\t\tif (pkt_len < 64)\n+\t\t\t\tstats->size_bins[0]++;\n+\t\t\telse if (pkt_len < 1519)\n+\t\t\t\tstats->size_bins[6]++;\n+\t\t\telse\n+\t\t\t\tstats->size_bins[7]++;\n+\t\t}\n+\n+\t\tea = rte_pktmbuf_mtod(pkt, struct rte_ether_addr *);\n+\t\tif (rte_is_multicast_ether_addr(ea)) {\n+\t\t\tif (rte_is_broadcast_ether_addr(ea))\n+\t\t\t\tstats->broadcast++;\n+\t\t\telse\n+\t\t\t\tstats->multicast++;\n+\t\t}\n+\t}\n+}\n+\n static __rte_always_inline int64_t\n vhost_async_dma_transfer_one(struct virtio_net *dev, struct vhost_virtqueue *vq,\n \t\tint16_t dma_id, uint16_t vchan_id, uint16_t flag_idx,\n@@ -1509,6 +1557,8 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,\n \telse\n \t\tnb_tx = virtio_dev_rx_split(dev, vq, pkts, count);\n \n+\tvhost_queue_stats_update(dev, vq, pkts, nb_tx);\n+\n out:\n \tif (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))\n \t\tvhost_user_iotlb_rd_unlock(vq);\n@@ -2064,6 +2114,8 @@ rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,\n \n \tn_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count, dma_id, vchan_id);\n \n+\tvhost_queue_stats_update(dev, vq, pkts, n_pkts_cpl);\n+\n out:\n \trte_spinlock_unlock(&vq->access_lock);\n \n@@ -3113,6 +3165,7 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,\n \t\t * learning table will get updated first.\n \t\t */\n \t\tpkts[0] = rarp_mbuf;\n+\t\tvhost_queue_stats_update(dev, vq, pkts, 1);\n \t\tpkts++;\n \t\tcount -= 1;\n \t}\n@@ -3129,6 +3182,8 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,\n \t\t\tcount = virtio_dev_tx_split_compliant(dev, vq, mbuf_pool, pkts, count);\n \t}\n \n+\tvhost_queue_stats_update(dev, vq, pkts, count);\n+\n out:\n \tif (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))\n \t\tvhost_user_iotlb_rd_unlock(vq);\n", "prefixes": [ "1/5" ] }{ "id": 110999, "url": "