Skip to content

Commit ae16582

Browse files
borkmannMartin KaFai Lau
authored and
Martin KaFai Lau
committed
netkit: Add tstats per-CPU traffic counters
Add dev->tstats traffic accounting to netkit. The latter contains per-CPU RX and TX counters. The dev's TX counters are bumped upon pass/unspec as well as redirect verdicts, in other words, on everything except for drops. The dev's RX counters are bumped upon successful __netif_rx(), as well as from skb_do_redirect() (not part of this commit here). Using dev->lstats with having just a single packets/bytes counter and inferring one another's RX counters from the peer dev's lstats is not possible given skb_do_redirect() can also bump the device's stats. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Nikolay Aleksandrov <razor@blackwall.org> Link: https://lore.kernel.org/r/20231114004220.6495-4-daniel@iogearbox.net Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
1 parent 34d21de commit ae16582

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

drivers/net/netkit.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ static netdev_tx_t netkit_xmit(struct sk_buff *skb, struct net_device *dev)
6868
netdev_tx_t ret_dev = NET_XMIT_SUCCESS;
6969
const struct bpf_mprog_entry *entry;
7070
struct net_device *peer;
71+
int len = skb->len;
7172

7273
rcu_read_lock();
7374
peer = rcu_dereference(nk->peer);
@@ -85,15 +86,22 @@ static netdev_tx_t netkit_xmit(struct sk_buff *skb, struct net_device *dev)
8586
case NETKIT_PASS:
8687
skb->protocol = eth_type_trans(skb, skb->dev);
8788
skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
88-
__netif_rx(skb);
89+
if (likely(__netif_rx(skb) == NET_RX_SUCCESS)) {
90+
dev_sw_netstats_tx_add(dev, 1, len);
91+
dev_sw_netstats_rx_add(peer, len);
92+
} else {
93+
goto drop_stats;
94+
}
8995
break;
9096
case NETKIT_REDIRECT:
97+
dev_sw_netstats_tx_add(dev, 1, len);
9198
skb_do_redirect(skb);
9299
break;
93100
case NETKIT_DROP:
94101
default:
95102
drop:
96103
kfree_skb(skb);
104+
drop_stats:
97105
dev_core_stats_tx_dropped_inc(dev);
98106
ret_dev = NET_XMIT_DROP;
99107
break;
@@ -174,6 +182,13 @@ static struct net_device *netkit_peer_dev(struct net_device *dev)
174182
return rcu_dereference(netkit_priv(dev)->peer);
175183
}
176184

185+
static void netkit_get_stats(struct net_device *dev,
186+
struct rtnl_link_stats64 *stats)
187+
{
188+
dev_fetch_sw_netstats(stats, dev->tstats);
189+
stats->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
190+
}
191+
177192
static void netkit_uninit(struct net_device *dev);
178193

179194
static const struct net_device_ops netkit_netdev_ops = {
@@ -184,6 +199,7 @@ static const struct net_device_ops netkit_netdev_ops = {
184199
.ndo_set_rx_headroom = netkit_set_headroom,
185200
.ndo_get_iflink = netkit_get_iflink,
186201
.ndo_get_peer_dev = netkit_peer_dev,
202+
.ndo_get_stats64 = netkit_get_stats,
187203
.ndo_uninit = netkit_uninit,
188204
.ndo_features_check = passthru_features_check,
189205
};
@@ -218,6 +234,7 @@ static void netkit_setup(struct net_device *dev)
218234

219235
ether_setup(dev);
220236
dev->max_mtu = ETH_MAX_MTU;
237+
dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
221238

222239
dev->flags |= IFF_NOARP;
223240
dev->priv_flags &= ~IFF_TX_SKB_SHARING;

0 commit comments

Comments
 (0)