From: Simon Horman <horms@kernel.org>
To: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Cc: netdev@vger.kernel.org, "Michael S. Tsirkin" <mst@redhat.com>,
Jason Wang <jasowang@redhat.com>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
virtualization@lists.linux.dev
Subject: Re: [PATCH net-next v3 3/6] virtio_net: support device stats
Date: Tue, 27 Feb 2024 19:19:40 +0000 [thread overview]
Message-ID: <20240227191940.GM277116@kernel.org> (raw)
In-Reply-To: <20240227080303.63894-4-xuanzhuo@linux.alibaba.com>
On Tue, Feb 27, 2024 at 04:03:00PM +0800, Xuan Zhuo wrote:
> As the spec https://github.com/oasis-tcs/virtio-spec/commit/42f389989823039724f95bbbd243291ab0064f82
>
> make virtio-net support getting the stats from the device by ethtool -S
> <eth0>.
>
> Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
...
> +static int virtnet_get_hw_stats(struct virtnet_info *vi,
> + struct virtnet_stats_ctx *ctx)
> +{
> + struct virtio_net_ctrl_queue_stats *req;
> + struct virtio_net_stats_reply_hdr *hdr;
> + struct scatterlist sgs_in, sgs_out;
> + u32 num_rx, num_tx, num_cq, offset;
> + int qnum, i, j, qid, res_size;
> + struct virtnet_stats_map *m;
> + void *reply, *p;
> + u64 bitmap;
> + int ok;
> + u64 *v;
> +
> + if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_DEVICE_STATS))
> + return 0;
> +
> + qnum = 0;
> + if (ctx->bitmap_cq)
> + qnum += 1;
> +
> + if (ctx->bitmap_rx)
> + qnum += vi->curr_queue_pairs;
> +
> + if (ctx->bitmap_tx)
> + qnum += vi->curr_queue_pairs;
> +
> + req = kcalloc(qnum, sizeof(*req), GFP_KERNEL);
> + if (!req)
> + return -ENOMEM;
> +
> + res_size = (ctx->size_rx + ctx->size_tx) * vi->curr_queue_pairs + ctx->size_cq;
> + reply = kmalloc(res_size, GFP_KERNEL);
> + if (!reply) {
> + kfree(req);
> + return -ENOMEM;
> + }
> +
> + j = 0;
> + for (i = 0; i < vi->curr_queue_pairs; ++i) {
> + if (ctx->bitmap_rx) {
> + req->stats[j].vq_index = cpu_to_le16(i * 2);
> + req->stats[j].types_bitmap[0] = cpu_to_le64(ctx->bitmap_rx);
> + ++j;
> + }
> +
> + if (ctx->bitmap_tx) {
> + req->stats[j].vq_index = cpu_to_le16(i * 2 + 1);
> + req->stats[j].types_bitmap[0] = cpu_to_le64(ctx->bitmap_tx);
> + ++j;
> + }
> + }
> +
> + if (ctx->size_cq) {
> + req->stats[j].vq_index = cpu_to_le16(vi->max_queue_pairs * 2);
> + req->stats[j].types_bitmap[0] = cpu_to_le64(ctx->bitmap_cq);
> + ++j;
> + }
> +
> + sg_init_one(&sgs_out, req, sizeof(*req) * j);
> + sg_init_one(&sgs_in, reply, res_size);
> +
> + ok = virtnet_send_command(vi, VIRTIO_NET_CTRL_STATS,
> + VIRTIO_NET_CTRL_STATS_GET,
> + &sgs_out, &sgs_in);
> + kfree(req);
> +
> + if (!ok) {
> + kfree(reply);
> + return ok;
> + }
> +
> + num_rx = VIRTNET_RQ_STATS_LEN + ctx->num_rx;
> + num_tx = VIRTNET_SQ_STATS_LEN + ctx->num_tx;
> + num_cq = ctx->num_tx;
> +
> + for (p = reply; p - reply < res_size; p += le16_to_cpu(hdr->size)) {
> + hdr = p;
> +
> + qid = le16_to_cpu(hdr->vq_index);
> +
> + if (qid == vi->max_queue_pairs * 2) {
> + offset = 0;
> + bitmap = ctx->bitmap_cq;
> + } else if (qid % 2) {
> + offset = num_cq + num_rx * vi->curr_queue_pairs + num_tx * (qid / 2);
> + offset += VIRTNET_SQ_STATS_LEN;
> + bitmap = ctx->bitmap_tx;
> + } else {
> + offset = num_cq + num_rx * (qid / 2) + VIRTNET_RQ_STATS_LEN;
> + bitmap = ctx->bitmap_rx;
> + }
> +
> + for (i = 0; i < ARRAY_SIZE(virtio_net_stats_map); ++i) {
> + m = &virtio_net_stats_map[i];
> +
> + if (m->stat_type & bitmap)
> + offset += m->num;
> +
> + if (hdr->type != m->reply_type)
> + continue;
> +
> + for (j = 0; j < m->num; ++j) {
> + v = p + m->desc[j].offset;
> + ctx->data[offset + j] = le64_to_cpu(*v);
Hi Xuan Zhuo,
Sparse complains about the line above because the type of *v is u64,
but le64_to_cpu() expects __le64.
> + }
> +
> + break;
> + }
> + }
> +
> + kfree(reply);
> + return 0;
> +}
> +
> static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
> {
> struct virtnet_info *vi = netdev_priv(dev);
...
next prev parent reply other threads:[~2024-02-27 19:19 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-27 8:02 [PATCH net-next v3 0/6] virtio-net: support device stats Xuan Zhuo
2024-02-27 8:02 ` [PATCH net-next v3 1/6] virtio_net: introduce device stats feature and structures Xuan Zhuo
2024-02-27 13:16 ` Jiri Pirko
2024-02-27 8:02 ` [PATCH net-next v3 2/6] virtio_net: virtnet_send_command supports command-specific-result Xuan Zhuo
2024-02-27 13:23 ` Jiri Pirko
2024-02-27 8:03 ` [PATCH net-next v3 3/6] virtio_net: support device stats Xuan Zhuo
2024-02-27 14:56 ` Jiri Pirko
2024-02-27 19:19 ` Simon Horman [this message]
2024-02-29 10:35 ` kernel test robot
2024-03-07 16:50 ` Jakub Kicinski
2024-03-11 10:48 ` Xuan Zhuo
2024-03-11 15:43 ` Jakub Kicinski
2024-02-27 8:03 ` [PATCH net-next v3 4/6] virtio_net: stats map include driver stats Xuan Zhuo
2024-02-27 15:05 ` Jiri Pirko
2024-02-27 8:03 ` [PATCH net-next v3 5/6] virtio_net: add the total stats field Xuan Zhuo
2024-02-27 14:54 ` Jakub Kicinski
2024-02-27 15:07 ` Jiri Pirko
2024-02-27 15:41 ` Jakub Kicinski
2024-02-27 15:59 ` Jiri Pirko
2024-03-07 9:36 ` Xuan Zhuo
2024-03-07 16:03 ` Jakub Kicinski
2024-02-27 8:03 ` [PATCH net-next v3 6/6] virtio_net: rename stat tx_timeout to timeout Xuan Zhuo
2024-02-27 15:08 ` Jiri Pirko
2024-02-27 13:14 ` [PATCH net-next v3 0/6] virtio-net: support device stats Jiri Pirko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240227191940.GM277116@kernel.org \
--to=horms@kernel.org \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=jasowang@redhat.com \
--cc=kuba@kernel.org \
--cc=mst@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=virtualization@lists.linux.dev \
--cc=xuanzhuo@linux.alibaba.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).