Skip to content

Commit 8a97590

Browse files
committed
Merge branch 'netdevsim-implement-rx-statistics-using-netdev_pcpu_stat_dstats'
Breno Leitao says: ==================== netdevsim: implement RX statistics using NETDEV_PCPU_STAT_DSTATS The netdevsim driver previously lacked RX statistics support, which prevented its use with the GenerateTraffic() test framework, as this framework verifies traffic flow by checking RX byte counts. This patch migrates netdevsim from its custom statistics collection to the NETDEV_PCPU_STAT_DSTATS framework, as suggested by Jakub. This change not only standardizes the statistics handling but also adds the necessary RX statistics support required by the test framework. v3: https://lore.kernel.org/[email protected] v2: https://lore.kernel.org/[email protected] v1: https://lore.kernel.org/[email protected] ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents c3ee72d + 2a68a22 commit 8a97590

File tree

3 files changed

+33
-38
lines changed

3 files changed

+33
-38
lines changed

drivers/net/netdevsim/netdev.c

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,14 @@ static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
9393
hrtimer_start(&rq->napi_timer, us_to_ktime(5), HRTIMER_MODE_REL);
9494

9595
rcu_read_unlock();
96-
u64_stats_update_begin(&ns->syncp);
97-
ns->tx_packets++;
98-
ns->tx_bytes += len;
99-
u64_stats_update_end(&ns->syncp);
96+
dev_dstats_tx_add(dev, skb->len);
10097
return NETDEV_TX_OK;
10198

10299
out_drop_free:
103100
dev_kfree_skb(skb);
104101
out_drop_cnt:
105102
rcu_read_unlock();
106-
u64_stats_update_begin(&ns->syncp);
107-
ns->tx_dropped++;
108-
u64_stats_update_end(&ns->syncp);
103+
dev_dstats_tx_dropped(dev);
109104
return NETDEV_TX_OK;
110105
}
111106

@@ -126,20 +121,6 @@ static int nsim_change_mtu(struct net_device *dev, int new_mtu)
126121
return 0;
127122
}
128123

129-
static void
130-
nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
131-
{
132-
struct netdevsim *ns = netdev_priv(dev);
133-
unsigned int start;
134-
135-
do {
136-
start = u64_stats_fetch_begin(&ns->syncp);
137-
stats->tx_bytes = ns->tx_bytes;
138-
stats->tx_packets = ns->tx_packets;
139-
stats->tx_dropped = ns->tx_dropped;
140-
} while (u64_stats_fetch_retry(&ns->syncp, start));
141-
}
142-
143124
static int
144125
nsim_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
145126
{
@@ -350,16 +331,24 @@ static int nsim_get_iflink(const struct net_device *dev)
350331

351332
static int nsim_rcv(struct nsim_rq *rq, int budget)
352333
{
334+
struct net_device *dev = rq->napi.dev;
353335
struct sk_buff *skb;
354-
int i;
336+
unsigned int skblen;
337+
int i, ret;
355338

356339
for (i = 0; i < budget; i++) {
357340
if (skb_queue_empty(&rq->skb_queue))
358341
break;
359342

360343
skb = skb_dequeue(&rq->skb_queue);
344+
/* skb might be discard at netif_receive_skb, save the len */
345+
skblen = skb->len;
361346
skb_mark_napi_id(skb, &rq->napi);
362-
netif_receive_skb(skb);
347+
ret = netif_receive_skb(skb);
348+
if (ret == NET_RX_SUCCESS)
349+
dev_dstats_rx_add(dev, skblen);
350+
else
351+
dev_dstats_rx_dropped(dev);
363352
}
364353

365354
return i;
@@ -556,7 +545,6 @@ static const struct net_device_ops nsim_netdev_ops = {
556545
.ndo_set_mac_address = eth_mac_addr,
557546
.ndo_validate_addr = eth_validate_addr,
558547
.ndo_change_mtu = nsim_change_mtu,
559-
.ndo_get_stats64 = nsim_get_stats64,
560548
.ndo_set_vf_mac = nsim_set_vf_mac,
561549
.ndo_set_vf_vlan = nsim_set_vf_vlan,
562550
.ndo_set_vf_rate = nsim_set_vf_rate,
@@ -580,7 +568,6 @@ static const struct net_device_ops nsim_vf_netdev_ops = {
580568
.ndo_set_mac_address = eth_mac_addr,
581569
.ndo_validate_addr = eth_validate_addr,
582570
.ndo_change_mtu = nsim_change_mtu,
583-
.ndo_get_stats64 = nsim_get_stats64,
584571
.ndo_setup_tc = nsim_setup_tc,
585572
.ndo_set_features = nsim_set_features,
586573
};
@@ -594,7 +581,7 @@ static void nsim_get_queue_stats_rx(struct net_device *dev, int idx,
594581
struct rtnl_link_stats64 rtstats = {};
595582

596583
if (!idx)
597-
nsim_get_stats64(dev, &rtstats);
584+
dev_get_stats(dev, &rtstats);
598585

599586
stats->packets = rtstats.rx_packets - !!rtstats.rx_packets;
600587
stats->bytes = rtstats.rx_bytes;
@@ -606,7 +593,7 @@ static void nsim_get_queue_stats_tx(struct net_device *dev, int idx,
606593
struct rtnl_link_stats64 rtstats = {};
607594

608595
if (!idx)
609-
nsim_get_stats64(dev, &rtstats);
596+
dev_get_stats(dev, &rtstats);
610597

611598
stats->packets = rtstats.tx_packets - !!rtstats.tx_packets;
612599
stats->bytes = rtstats.tx_bytes;
@@ -618,7 +605,7 @@ static void nsim_get_base_stats(struct net_device *dev,
618605
{
619606
struct rtnl_link_stats64 rtstats = {};
620607

621-
nsim_get_stats64(dev, &rtstats);
608+
dev_get_stats(dev, &rtstats);
622609

623610
rx->packets = !!rtstats.rx_packets;
624611
rx->bytes = 0;
@@ -645,9 +632,12 @@ static struct nsim_rq *nsim_queue_alloc(void)
645632
return rq;
646633
}
647634

648-
static void nsim_queue_free(struct nsim_rq *rq)
635+
static void nsim_queue_free(struct net_device *dev, struct nsim_rq *rq)
649636
{
650637
hrtimer_cancel(&rq->napi_timer);
638+
local_bh_disable();
639+
dev_dstats_rx_dropped_add(dev, rq->skb_queue.qlen);
640+
local_bh_enable();
651641
skb_queue_purge_reason(&rq->skb_queue, SKB_DROP_REASON_QUEUE_PURGE);
652642
kfree(rq);
653643
}
@@ -694,7 +684,7 @@ nsim_queue_mem_alloc(struct net_device *dev, void *per_queue_mem, int idx)
694684
return 0;
695685

696686
err_free:
697-
nsim_queue_free(qmem->rq);
687+
nsim_queue_free(dev, qmem->rq);
698688
return err;
699689
}
700690

@@ -708,7 +698,7 @@ static void nsim_queue_mem_free(struct net_device *dev, void *per_queue_mem)
708698
if (!ns->rq_reset_mode)
709699
netif_napi_del_locked(&qmem->rq->napi);
710700
page_pool_destroy(qmem->rq->page_pool);
711-
nsim_queue_free(qmem->rq);
701+
nsim_queue_free(dev, qmem->rq);
712702
}
713703
}
714704

@@ -890,6 +880,7 @@ static void nsim_setup(struct net_device *dev)
890880
NETIF_F_HW_CSUM |
891881
NETIF_F_LRO |
892882
NETIF_F_TSO;
883+
dev->pcpu_stat_type = NETDEV_PCPU_STAT_DSTATS;
893884
dev->max_mtu = ETH_MAX_MTU;
894885
dev->xdp_features = NETDEV_XDP_ACT_HW_OFFLOAD;
895886
}
@@ -925,7 +916,7 @@ static void nsim_queue_uninit(struct netdevsim *ns)
925916
int i;
926917

927918
for (i = 0; i < dev->num_rx_queues; i++)
928-
nsim_queue_free(ns->rq[i]);
919+
nsim_queue_free(dev, ns->rq[i]);
929920

930921
kfree(ns->rq);
931922
ns->rq = NULL;
@@ -1022,7 +1013,6 @@ nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port)
10221013
dev_net_set(dev, nsim_dev_net(nsim_dev));
10231014
ns = netdev_priv(dev);
10241015
ns->netdev = dev;
1025-
u64_stats_init(&ns->syncp);
10261016
ns->nsim_dev = nsim_dev;
10271017
ns->nsim_dev_port = nsim_dev_port;
10281018
ns->nsim_bus_dev = nsim_dev->nsim_bus_dev;

drivers/net/netdevsim/netdevsim.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,6 @@ struct netdevsim {
108108

109109
int rq_reset_mode;
110110

111-
u64 tx_packets;
112-
u64 tx_bytes;
113-
u64 tx_dropped;
114-
struct u64_stats_sync syncp;
115-
116111
struct nsim_bus_dev *nsim_bus_dev;
117112

118113
struct bpf_prog *bpf_offloaded;

include/linux/netdevice.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,6 +3016,16 @@ static inline void dev_dstats_rx_dropped(struct net_device *dev)
30163016
u64_stats_update_end(&dstats->syncp);
30173017
}
30183018

3019+
static inline void dev_dstats_rx_dropped_add(struct net_device *dev,
3020+
unsigned int packets)
3021+
{
3022+
struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
3023+
3024+
u64_stats_update_begin(&dstats->syncp);
3025+
u64_stats_add(&dstats->rx_drops, packets);
3026+
u64_stats_update_end(&dstats->syncp);
3027+
}
3028+
30193029
static inline void dev_dstats_tx_add(struct net_device *dev,
30203030
unsigned int len)
30213031
{

0 commit comments

Comments
 (0)