Skip to content

Commit d806e1f

Browse files
fengidriPaolo Abeni
authored andcommitted
virtio_net: add the total stats field
Now, we just show the stats of every queue. But for the user, the total values of every stat may are valuable. NIC statistics: rx_packets: 373522 rx_bytes: 85919736 rx_drops: 0 rx_xdp_packets: 0 rx_xdp_tx: 0 rx_xdp_redirects: 0 rx_xdp_drops: 0 rx_kicks: 11125 rx_hw_notifications: 0 rx_hw_packets: 1325870 rx_hw_bytes: 263348963 rx_hw_interrupts: 0 rx_hw_drops: 1451 rx_hw_drop_overruns: 0 rx_hw_csum_valid: 1325870 rx_hw_needs_csum: 1325870 rx_hw_csum_none: 0 rx_hw_csum_bad: 0 rx_hw_ratelimit_packets: 0 rx_hw_ratelimit_bytes: 0 tx_packets: 10050 tx_bytes: 1230176 tx_xdp_tx: 0 tx_xdp_tx_drops: 0 tx_kicks: 10050 tx_timeouts: 0 tx_hw_notifications: 0 tx_hw_packets: 32281 tx_hw_bytes: 4315590 tx_hw_interrupts: 0 tx_hw_drops: 0 tx_hw_drop_malformed: 0 tx_hw_csum_none: 0 tx_hw_needs_csum: 32281 tx_hw_ratelimit_packets: 0 tx_hw_ratelimit_bytes: 0 rx0_packets: 373522 rx0_bytes: 85919736 rx0_drops: 0 rx0_xdp_packets: 0 rx0_xdp_tx: 0 rx0_xdp_redirects: 0 rx0_xdp_drops: 0 rx0_kicks: 11125 rx0_hw_notifications: 0 rx0_hw_packets: 1325870 rx0_hw_bytes: 263348963 rx0_hw_interrupts: 0 rx0_hw_drops: 1451 rx0_hw_drop_overruns: 0 rx0_hw_csum_valid: 1325870 rx0_hw_needs_csum: 1325870 rx0_hw_csum_none: 0 rx0_hw_csum_bad: 0 rx0_hw_ratelimit_packets: 0 rx0_hw_ratelimit_bytes: 0 tx0_packets: 10050 tx0_bytes: 1230176 tx0_xdp_tx: 0 tx0_xdp_tx_drops: 0 tx0_kicks: 10050 tx0_timeouts: 0 tx0_hw_notifications: 0 tx0_hw_packets: 32281 tx0_hw_bytes: 4315590 tx0_hw_interrupts: 0 tx0_hw_drops: 0 tx0_hw_drop_malformed: 0 tx0_hw_csum_none: 0 tx0_hw_needs_csum: 32281 tx0_hw_ratelimit_packets: 0 tx0_hw_ratelimit_bytes: 0 Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Signed-off-by: Paolo Abeni <pabeni@redhat.com>
1 parent d86769b commit d806e1f

File tree

1 file changed

+69
-12
lines changed

1 file changed

+69
-12
lines changed

drivers/net/virtio_net.c

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3383,14 +3383,15 @@ static void virtnet_stats_sprintf(u8 **p, const char *fmt, const char *noq_fmt,
33833383
}
33843384
}
33853385

3386+
/* qid == -1: for rx/tx queue total field */
33863387
static void virtnet_get_stats_string(struct virtnet_info *vi, int type, int qid, u8 **data)
33873388
{
33883389
const struct virtnet_stat_desc *desc;
33893390
const char *fmt, *noq_fmt;
33903391
u8 *p = *data;
3391-
u32 num = 0;
3392+
u32 num;
33923393

3393-
if (type == VIRTNET_Q_TYPE_CQ) {
3394+
if (type == VIRTNET_Q_TYPE_CQ && qid >= 0) {
33943395
noq_fmt = "cq_hw_%s";
33953396

33963397
if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_CVQ) {
@@ -3403,65 +3404,69 @@ static void virtnet_get_stats_string(struct virtnet_info *vi, int type, int qid,
34033404

34043405
if (type == VIRTNET_Q_TYPE_RX) {
34053406
fmt = "rx%u_%s";
3407+
noq_fmt = "rx_%s";
34063408

34073409
desc = &virtnet_rq_stats_desc[0];
34083410
num = ARRAY_SIZE(virtnet_rq_stats_desc);
34093411

3410-
virtnet_stats_sprintf(&p, fmt, NULL, num, qid, desc);
3412+
virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
34113413

34123414
fmt = "rx%u_hw_%s";
3415+
noq_fmt = "rx_hw_%s";
34133416

34143417
if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
34153418
desc = &virtnet_stats_rx_basic_desc[0];
34163419
num = ARRAY_SIZE(virtnet_stats_rx_basic_desc);
34173420

3418-
virtnet_stats_sprintf(&p, fmt, NULL, num, qid, desc);
3421+
virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
34193422
}
34203423

34213424
if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
34223425
desc = &virtnet_stats_rx_csum_desc[0];
34233426
num = ARRAY_SIZE(virtnet_stats_rx_csum_desc);
34243427

3425-
virtnet_stats_sprintf(&p, fmt, NULL, num, qid, desc);
3428+
virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
34263429
}
34273430

34283431
if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
34293432
desc = &virtnet_stats_rx_speed_desc[0];
34303433
num = ARRAY_SIZE(virtnet_stats_rx_speed_desc);
34313434

3432-
virtnet_stats_sprintf(&p, fmt, NULL, num, qid, desc);
3435+
virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
34333436
}
34343437
}
34353438

34363439
if (type == VIRTNET_Q_TYPE_TX) {
34373440
fmt = "tx%u_%s";
3441+
noq_fmt = "tx_%s";
34383442

34393443
desc = &virtnet_sq_stats_desc[0];
34403444
num = ARRAY_SIZE(virtnet_sq_stats_desc);
34413445

3442-
virtnet_stats_sprintf(&p, fmt, NULL, num, qid, desc);
3446+
virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
34433447

34443448
fmt = "tx%u_hw_%s";
3449+
noq_fmt = "tx_hw_%s";
34453450

34463451
if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
34473452
desc = &virtnet_stats_tx_basic_desc[0];
34483453
num = ARRAY_SIZE(virtnet_stats_tx_basic_desc);
34493454

3450-
virtnet_stats_sprintf(&p, fmt, NULL, num, qid, desc);
3455+
virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
34513456
}
34523457

34533458
if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
34543459
desc = &virtnet_stats_tx_gso_desc[0];
34553460
num = ARRAY_SIZE(virtnet_stats_tx_gso_desc);
34563461

3457-
virtnet_stats_sprintf(&p, fmt, NULL, num, qid, desc);
3462+
virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
34583463
}
34593464

34603465
if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
34613466
desc = &virtnet_stats_tx_speed_desc[0];
34623467
num = ARRAY_SIZE(virtnet_stats_tx_speed_desc);
34633468

3464-
virtnet_stats_sprintf(&p, fmt, NULL, num, qid, desc);
3469+
virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
34653470
}
34663471
}
34673472

@@ -3542,6 +3547,49 @@ static void virtnet_stats_ctx_init(struct virtnet_info *vi,
35423547
}
35433548
}
35443549

3550+
/* stats_sum_queue - Calculate the sum of the same fields in sq or rq.
3551+
* @sum: the position to store the sum values
3552+
* @num: field num
3553+
* @q_value: the first queue fields
3554+
* @q_num: number of the queues
3555+
*/
3556+
static void stats_sum_queue(u64 *sum, u32 num, u64 *q_value, u32 q_num)
3557+
{
3558+
u32 step = num;
3559+
int i, j;
3560+
u64 *p;
3561+
3562+
for (i = 0; i < num; ++i) {
3563+
p = sum + i;
3564+
*p = 0;
3565+
3566+
for (j = 0; j < q_num; ++j)
3567+
*p += *(q_value + i + j * step);
3568+
}
3569+
}
3570+
3571+
static void virtnet_fill_total_fields(struct virtnet_info *vi,
3572+
struct virtnet_stats_ctx *ctx)
3573+
{
3574+
u64 *data, *first_rx_q, *first_tx_q;
3575+
u32 num_cq, num_rx, num_tx;
3576+
3577+
num_cq = ctx->desc_num[VIRTNET_Q_TYPE_CQ];
3578+
num_rx = ctx->desc_num[VIRTNET_Q_TYPE_RX];
3579+
num_tx = ctx->desc_num[VIRTNET_Q_TYPE_TX];
3580+
3581+
first_rx_q = ctx->data + num_rx + num_tx + num_cq;
3582+
first_tx_q = first_rx_q + vi->curr_queue_pairs * num_rx;
3583+
3584+
data = ctx->data;
3585+
3586+
stats_sum_queue(data, num_rx, first_rx_q, vi->curr_queue_pairs);
3587+
3588+
data = ctx->data + num_rx;
3589+
3590+
stats_sum_queue(data, num_tx, first_tx_q, vi->curr_queue_pairs);
3591+
}
3592+
35453593
/* virtnet_fill_stats - copy the stats to ethtool -S
35463594
* The stats source is the device or the driver.
35473595
*
@@ -3569,7 +3617,9 @@ static void virtnet_fill_stats(struct virtnet_info *vi, u32 qid,
35693617

35703618
queue_type = vq_type(vi, qid);
35713619
bitmap = ctx->bitmap[queue_type];
3572-
offset = 0;
3620+
3621+
/* skip the total fields of pairs */
3622+
offset = num_rx + num_tx;
35733623

35743624
if (queue_type == VIRTNET_Q_TYPE_TX) {
35753625
offset += num_cq + num_rx * vi->curr_queue_pairs + num_tx * (qid / 2);
@@ -3780,6 +3830,10 @@ static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
37803830

37813831
switch (stringset) {
37823832
case ETH_SS_STATS:
3833+
/* Generate the total field names. */
3834+
virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_RX, -1, &p);
3835+
virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_TX, -1, &p);
3836+
37833837
virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_CQ, 0, &p);
37843838

37853839
for (i = 0; i < vi->curr_queue_pairs; ++i)
@@ -3803,7 +3857,8 @@ static int virtnet_get_sset_count(struct net_device *dev, int sset)
38033857

38043858
pair_count = ctx.desc_num[VIRTNET_Q_TYPE_RX] + ctx.desc_num[VIRTNET_Q_TYPE_TX];
38053859

3806-
return ctx.desc_num[VIRTNET_Q_TYPE_CQ] + vi->curr_queue_pairs * pair_count;
3860+
return pair_count + ctx.desc_num[VIRTNET_Q_TYPE_CQ] +
3861+
vi->curr_queue_pairs * pair_count;
38073862
default:
38083863
return -EOPNOTSUPP;
38093864
}
@@ -3837,6 +3892,8 @@ static void virtnet_get_ethtool_stats(struct net_device *dev,
38373892
virtnet_fill_stats(vi, i * 2 + 1, &ctx, stats_base, true, 0);
38383893
} while (u64_stats_fetch_retry(&sq->stats.syncp, start));
38393894
}
3895+
3896+
virtnet_fill_total_fields(vi, &ctx);
38403897
}
38413898

38423899
static void virtnet_get_channels(struct net_device *dev,

0 commit comments

Comments
 (0)