bpf_redirect_peer fixes
Subsystems (8)
Daniel Borkmann
maintainerv2 cover bpf_redirect_peer fixes
This fixes bpf_redirect_peer stats accounting for veth and netkit, and adds tstats in the first place for the latter. Utilise indirect call wrapper for bpf_redirect_peer, and improve test coverage of the latter also for netkit devices. Details in the patches, thanks!
The series was targeted at bpf originally, and is done here as well, so it can trigger BPF CI. Jakub, if you think directly going via net is better since the majority of the diff touches net anyway, that is fine, too.
Thanks!
v1 -> v2:
- Move stats allocation/freeing into net core (Jakub)
- As prepwork for the above, move vrf's dstats over into the core
- Add a check into stats alloc to enforce tstats upon implementing ndo_get_peer_dev
- Add Acks from the mailing list to unchanged patches
Daniel Borkmann (6):
net, vrf: Move dstats structure to core
net: Move {l,t,d}stats allocation to core and convert veth & vrf
netkit: Add tstats per-CPU traffic counters
bpf, netkit: Add indirect call wrapper for fetching peer dev selftests/bpf: De-veth-ize the tc_redirect test case selftests/bpf: Add netkit to tc_redirect selftest
Peilin Ye (2):
veth: Use tstats per-CPU traffic counters
bpf: Fix dev's rx stats for bpf_redirect_peer traffic
drivers/net/netkit.c | 22 +- drivers/net/veth.c | 44 +-- drivers/net/vrf.c | 38 +-- include/linux/netdevice.h | 18 + include/net/netkit.h | 6 + net/core/dev.c | 55 ++- net/core/filter.c | 19 +- .../selftests/bpf/prog_tests/tc_redirect.c | 317 +++++++++++------- 8 files changed, 324 insertions(+), 195 deletions(-)
Signature · 1 line
2.34.1
Daniel Borkmann
maintainerPATCH v2 1/8 net, vrf: Move dstats structure to core
Just move struct pcpu_dstats out of the vrf into the core, and streamline the field names slightly, so they better align with the {t,l}stats ones.
No functional change otherwise. A conversion of the u64s to u64_stats_t could be done at a separate point in future. This move is needed as we are moving the {t,l,d}stats allocation/freeing to the core.
--- drivers/net/vrf.c | 24 +++++++----------------- include/linux/netdevice.h | 10 ++++++++++ 2 files changed, 17 insertions(+), 17 deletions(-)
drivers/net/vrf.c +7 -17
@@ -121,22 +121,12 @@ struct net_vrf {int ifindex; }; -struct pcpu_dstats { -u64 tx_pkts; -u64 tx_bytes; -u64 tx_drps; -u64 rx_pkts; -u64 rx_bytes; -u64 rx_drps; -struct u64_stats_sync syncp; -}; -static void vrf_rx_stats (struct net_device *dev,int len) {struct pcpu_dstats *dstats =this_cpu_ptr (dev->dstats);u64_stats_update_begin (&dstats->syncp); - dstats->rx_pkts++; + dstats->rx_packets++; dstats->rx_bytes += len;u64_stats_update_end (&dstats->syncp); }
@@ -161,10 +151,10 @@ static void vrf_get_stats64(struct net_device *dev,do { start =u64_stats_fetch_begin (&dstats->syncp); tbytes = dstats->tx_bytes; - tpkts = dstats->tx_pkts; - tdrops = dstats->tx_drps; + tpkts = dstats->tx_packets; + tdrops = dstats->tx_drops; rbytes = dstats->rx_bytes; - rpkts = dstats->rx_pkts; + rpkts = dstats->rx_packets; }while (u64_stats_fetch_retry (&dstats->syncp, start)); stats->tx_bytes += tbytes; stats->tx_packets += tpkts;
@@ -421,7 +411,7 @@ static int vrf_local_xmit(struct sk_buff *skb, struct net_device *dev,if (likely (__netif_rx (skb) ==NET_RX_SUCCESS ))vrf_rx_stats (dev, len);else -this_cpu_inc (dev->dstats->rx_drps); +this_cpu_inc (dev->dstats->rx_drops);return NETDEV_TX_OK ; }
@@ -616,11 +606,11 @@ static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)struct pcpu_dstats *dstats =this_cpu_ptr (dev->dstats);u64_stats_update_begin (&dstats->syncp); - dstats->tx_pkts++; + dstats->tx_packets++; dstats->tx_bytes += len;u64_stats_update_end (&dstats->syncp); }else { -this_cpu_inc (dev->dstats->tx_drps); +this_cpu_inc (dev->dstats->tx_drops); }return ret;
include/linux/netdevice.h +10 -0
@@ -2755,6 +2755,16 @@ struct pcpu_sw_netstats {struct u64_stats_sync syncp; }__aligned(4 * sizeof(u64)) ; +struct pcpu_dstats { +u64 rx_packets; +u64 rx_bytes; +u64 rx_drops; +u64 tx_packets; +u64 tx_bytes; +u64 tx_drops; +struct u64_stats_sync syncp; +}__aligned(8 * sizeof(u64)) ; +struct pcpu_lstats {u64_stats_t packets;u64_stats_t bytes;
Signature · 1 line
2.34.1
Re: [PATCH bpf v2 1/8] net, vrf: Move dstats structure to core
Quoted 7 lines · Daniel Borkmann
Just move struct
pcpu_dstatsout of the vrf into the core, and streamline the field names slightly, so they better align with the {t,l}stats ones.No functional change otherwise. A conversion of the u64s to
u64_stats_tcould be done at a separate point in future. This move is needed as we are moving the {t,l,d}stats allocation/freeing to the core.
Quoted 8 lines
Signed-off-by Daniel Borkmann <[email protected]>Cc Jakub Kicinski <[email protected]>Cc David Ahern <[email protected]>--- drivers/net/vrf.c | 24 +++++++----------------- include/linux/netdevice.h | 10 ++++++++++ 2 files changed, 17 insertions(+), 17 deletions(-)
Daniel Borkmann
maintainerPATCH v2 2/8 net: Move {l,t,d}stats allocation to core and convert veth & vrf
In reply to Daniel BorkmannMove {l,t,d}stats allocation to the core and let netdevs pick the stats type they need. That way the driver doesn't have to bother with error handling (allocation failure checking, making sure free happens in the right spot, etc) - all happening in the core.
--- drivers/net/veth.c | 16 ++----------- drivers/net/vrf.c | 14 +++--------- include/linux/netdevice.h | 8 +++++++ net/core/dev.c | 47 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 59 insertions(+), 26 deletions(-)
drivers/net/veth.c +2 -14
@@ -1506,25 +1506,12 @@ static void veth_free_queues(struct net_device *dev)static int veth_dev_init (struct net_device *dev) { -int err; - - dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats); -if (!dev->lstats) -return -ENOMEM ; - - err =veth_alloc_queues (dev); -if (err) { -free_percpu (dev->lstats); -return err; - } - -return 0 ; +return veth_alloc_queues (dev); }static void veth_dev_free (struct net_device *dev) {veth_free_queues (dev); -free_percpu (dev->lstats); }#ifdef CONFIG_NET_POLL_CONTROLLER
@@ -1796,6 +1783,7 @@ static void veth_setup(struct net_device *dev) NETIF_F_HW_VLAN_STAG_RX); dev->needs_free_netdev = true; dev->priv_destructor = veth_dev_free; + dev->pcpu_stat_type =NETDEV_PCPU_STAT_LSTATS ; dev->max_mtu =ETH_MAX_MTU ; dev->hw_features =VETH_FEATURES ;
drivers/net/vrf.c +3 -11
@@ -1164,22 +1164,15 @@ static void vrf_dev_uninit(struct net_device *dev)vrf_rtable_release (dev, vrf);vrf_rt6_release (dev, vrf); - -free_percpu (dev->dstats); - dev->dstats =NULL ; }static int vrf_dev_init (struct net_device *dev) {struct net_vrf *vrf =netdev_priv (dev); - dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats); -if (!dev->dstats) - goto out_nomem; -/* create the default dst which points back to us */ if (vrf_rtable_create (dev) !=0 ) - goto out_stats; + goto out_nomem;if (vrf_rt6_create (dev) !=0 ) goto out_rth;
@@ -1193,9 +1186,6 @@ static int vrf_dev_init(struct net_device *dev) out_rth:vrf_rtable_release (dev, vrf); -out_stats: -free_percpu (dev->dstats); - dev->dstats =NULL ; out_nomem:return -ENOMEM ; }
@@ -1694,6 +1684,8 @@ static void vrf_setup(struct net_device *dev) dev->min_mtu =IPV6_MIN_MTU ; dev->max_mtu =IP6_MAX_MTU ; dev->mtu = dev->max_mtu; + + dev->pcpu_stat_type =NETDEV_PCPU_STAT_DSTATS ; } static int vrf_validate(struct nlattr *tb[], struct nlattr *data[],
include/linux/netdevice.h +8 -0
@@ -1797,6 +1797,13 @@ enum netdev_ml_priv_type { ML_PRIV_CAN, }; +enum netdev_stat_type { +NETDEV_PCPU_STAT_NONE , +NETDEV_PCPU_STAT_LSTATS ,/* struct pcpu_lstats */ +NETDEV_PCPU_STAT_TSTATS ,/* struct pcpu_sw_netstats */ +NETDEV_PCPU_STAT_DSTATS ,/* struct pcpu_dstats */ +}; +/** * struct net_device - The DEVICE structure. *
@@ -2354,6 +2361,7 @@ struct net_device {void *ml_priv;enum netdev_ml_priv_type ml_priv_type; + enum netdev_stat_type pcpu_stat_type:8;union {struct pcpu_lstats __percpu *lstats;struct pcpu_sw_netstats __percpu *tstats;
net/core/dev.c +46 -1
73 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH bpf v2 2/8] net: Move {l,t,d}stats allocation to core and convert veth & vrf
Quoted 9 lines · Daniel Borkmann
Move {l,t,d}stats allocation to the core and let netdevs pick the stats type they need. That way the driver doesn't have to bother with error handling (allocation failure checking, making sure free happens in the right spot, etc) - all happening in the core.
Co-developed-by Jakub Kicinski <[email protected]>Signed-off-by Jakub Kicinski <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>
Quoted 7 lines
--- drivers/net/veth.c | 16 ++----------- drivers/net/vrf.c | 14 +++--------- include/linux/netdevice.h | 8 +++++++ net/core/dev.c | 47 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 59 insertions(+), 26 deletions(-)
Simon Horman
reviewerRe: [PATCH bpf v2 2/8] net: Move {l,t,d}stats allocation to core and convert veth & vrf
In reply to Daniel BorkmannQuoted 9 lines · Daniel Borkmann
Move {l,t,d}stats allocation to the core and let netdevs pick the stats type they need. That way the driver doesn't have to bother with error handling (allocation failure checking, making sure free happens in the right spot, etc) - all happening in the core.
Co-developed-by Jakub Kicinski <[email protected]>Signed-off-by Jakub Kicinski <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>
...
include/linux/netdevice.h struct net_device { · line 2364
Quoted 5 lines
include/linux/netdevice.h +1
-0@@ -2354,6 +2361,7 @@ struct net_device {void *ml_priv;enum netdev_ml_priv_type ml_priv_type; + enum netdev_stat_type pcpu_stat_type:8;
Hi Daniel,
nit: Please consider adding documentation for this new field to
the kernel doc for net_device.
...
Daniel Borkmann
maintainerRe: [PATCH bpf v2 2/8] net: Move {l,t,d}stats allocation to core and convert veth & vrf
Quoted 17 lines · Simon Horman
Quoted 9 lines · Daniel Borkmann
Move {l,t,d}stats allocation to the core and let netdevs pick the stats type they need. That way the driver doesn't have to bother with error handling (allocation failure checking, making sure free happens in the right spot, etc) - all happening in the core.
Co-developed-by Jakub Kicinski <[email protected]>Signed-off-by Jakub Kicinski <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>...
Quoted 4 lines
include/linux/netdevice.h +0
-0@@ -2354,6 +2361,7 @@ struct net_device {void *ml_priv;enum netdev_ml_priv_type ml_priv_type;
Quoted 7 lines
include/linux/netdevice.h struct net_device { · line 2364
Quoted 1 line
include/linux/netdevice.h +1
-0+ enum netdev_stat_type pcpu_stat_type:8;Hi Daniel,
nit: Please consider adding documentation for this new field to
the kernel doc fornet_device.
Will add, thanks Simon!
Simon Horman
reviewerRe: [PATCH bpf v2 2/8] net: Move {l,t,d}stats allocation to core and convert veth & vrf
In reply to Daniel BorkmannQuoted 9 lines · Daniel Borkmann
Move {l,t,d}stats allocation to the core and let netdevs pick the stats type they need. That way the driver doesn't have to bother with error handling (allocation failure checking, making sure free happens in the right spot, etc) - all happening in the core.
Co-developed-by Jakub Kicinski <[email protected]>Signed-off-by Jakub Kicinski <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>
Hi Daniel,
sorry I was a bit to hasty in hitting the send button for my previous message. I have a some more minor feedback on this series.
Quoted 17 lines
net/core/dev.c +9
-0@@ -10049,6 +10049,44 @@ void netif_tx_stop_all_queues(struct net_device *dev) }EXPORT_SYMBOL (netif_tx_stop_all_queues); +static int netdev_do_alloc_pcpu_stats (struct net_device *dev) +{ +void __percpu *v; + +switch (dev->pcpu_stat_type) { +case NETDEV_PCPU_STAT_NONE : +return 0 ; +case NETDEV_PCPU_STAT_LSTATS : + v = dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
net/core/dev.c void netif_tx_stop_all_queues(struct net_device *dev) · line 10070
Quoted 10 lines
net/core/dev.c +10
-0+break ; +case NETDEV_PCPU_STAT_TSTATS : + v = dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); +break ; +case NETDEV_PCPU_STAT_DSTATS : + v = dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats); +break ; + } + +return v ?0 : -ENOMEM ;
Perhaps this cannot happen, but if none of the cases in the switch statement are met, then v will be uninitialised here.
As flagged by Smatch.
Quoted 2 lines
net/core/dev.c +2
-0+} +
...
net/core/dev.c void netdev_run_todo(void) · line 10519
Quoted 8 lines
net/core/dev.c +1
-0@@ -10469,6 +10513,7 @@ void netdev_run_todo(void)WARN_ON (rcu_access_pointer (dev->ip_ptr));WARN_ON (rcu_access_pointer (dev->ip6_ptr)); +netdev_do_free_pcpu_stats (dev);if (dev->priv_destructor) dev->priv_destructor (dev);if (dev->needs_free_netdev)
nit: the hunk above seems unnecessary; one blank line is enough.
Daniel Borkmann
maintainerRe: [PATCH bpf v2 2/8] net: Move {l,t,d}stats allocation to core and convert veth & vrf
Quoted 47 lines · Simon Horman
Quoted 9 lines · Daniel Borkmann
Move {l,t,d}stats allocation to the core and let netdevs pick the stats type they need. That way the driver doesn't have to bother with error handling (allocation failure checking, making sure free happens in the right spot, etc) - all happening in the core.
Co-developed-by Jakub Kicinski <[email protected]>Signed-off-by Jakub Kicinski <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>Hi Daniel,
sorry I was a bit to hasty in hitting the send button for my previous message. I have a some more minor feedback on this series.
Quoted 17 lines
net/core/dev.c +9
-0@@ -10049,6 +10049,44 @@ void netif_tx_stop_all_queues(struct net_device *dev) }EXPORT_SYMBOL (netif_tx_stop_all_queues); +static int netdev_do_alloc_pcpu_stats (struct net_device *dev) +{ +void __percpu *v; + +switch (dev->pcpu_stat_type) { +case NETDEV_PCPU_STAT_NONE : +return 0 ; +case NETDEV_PCPU_STAT_LSTATS : + v = dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);net/core/dev.c void netif_tx_stop_all_queues(struct net_device *dev) · line 10070
Quoted 10 lines
net/core/dev.c +10
-0+break ; +case NETDEV_PCPU_STAT_TSTATS : + v = dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); +break ; +case NETDEV_PCPU_STAT_DSTATS : + v = dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats); +break ; + } + +return v ?0 : -ENOMEM ;Perhaps this cannot happen, but if none of the cases in the switch statement are met, then v will be uninitialised here.
Quoted 1 line
As flagged by Smatch.
Good point, I'll add a guard in case someone tries to set an invalid value outside of the enum.
Quoted 9 lines
Quoted 2 lines
net/core/dev.c +2
-0+} +...
Quoted 4 lines
net/core/dev.c +0
-0@@ -10469,6 +10513,7 @@ void netdev_run_todo(void)WARN_ON (rcu_access_pointer (dev->ip_ptr));WARN_ON (rcu_access_pointer (dev->ip6_ptr));
Quoted 6 lines
net/core/dev.c void netdev_run_todo(void) · line 10519
Quoted 4 lines
net/core/dev.c +1
-0+netdev_do_free_pcpu_stats (dev);if (dev->priv_destructor) dev->priv_destructor (dev);if (dev->needs_free_netdev)nit: the hunk above seems unnecessary; one blank line is enough.
I'm not sure which one you mean?
Thanks,
Daniel
Simon Horman
reviewerRe: [PATCH bpf v2 2/8] net: Move {l,t,d}stats allocation to core and convert veth & vrf
Quoted 2 lines · Daniel Borkmann
Quoted 1 line · Simon Horman
On Sun, Nov 12, 2023 at 09:30:03PM +0100, Daniel Borkmann wrote:
...
Quoted 11 lines
Quoted 9 lines
net/core/dev.c void netdev_run_todo(void) · line 10518
Quoted 7 lines
net/core/dev.c +1
-0@@ -10469,6 +10513,7 @@ void netdev_run_todo(void)WARN_ON (rcu_access_pointer (dev->ip_ptr));WARN_ON (rcu_access_pointer (dev->ip6_ptr)); +netdev_do_free_pcpu_stats (dev);if (dev->priv_destructor) dev->priv_destructor (dev);if (dev->needs_free_netdev)nit: the hunk above seems unnecessary; one blank line is enough.
I'm not sure which one you mean?
It seems that I was confused for some reason,
please ignore my previous comment.
Daniel Borkmann
maintainerPATCH v2 3/8 netkit: Add tstats per-CPU traffic counters
In reply to Daniel BorkmannAdd 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.
--- drivers/net/netkit.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/net/netkit.c +18 -1
@@ -68,6 +68,7 @@ static netdev_tx_t netkit_xmit(struct sk_buff *skb, struct net_device *dev)netdev_tx_t ret_dev =NET_XMIT_SUCCESS ;const struct bpf_mprog_entry *entry;struct net_device *peer; +int len = skb->len;rcu_read_lock (); peer =rcu_dereference (nk->peer);
@@ -85,15 +86,22 @@ static netdev_tx_t netkit_xmit(struct sk_buff *skb, struct net_device *dev)case NETKIT_PASS : skb->protocol =eth_type_trans (skb, skb->dev);skb_postpull_rcsum (skb,eth_hdr (skb),ETH_HLEN ); -__netif_rx (skb); +if (likely (__netif_rx (skb) ==NET_RX_SUCCESS )) { +dev_sw_netstats_tx_add (dev,1 , len); +dev_sw_netstats_rx_add (peer, len); + }else { + goto drop_stats; + }break ;case NETKIT_REDIRECT : +dev_sw_netstats_tx_add (dev,1 , len);skb_do_redirect (skb);break ;case NETKIT_DROP :default : drop:kfree_skb (skb); +drop_stats:dev_core_stats_tx_dropped_inc (dev); ret_dev =NET_XMIT_DROP ;break ;
@@ -174,6 +182,13 @@ static struct net_device *netkit_peer_dev(struct net_device *dev)return rcu_dereference (netkit_priv (dev)->peer); } +static void netkit_get_stats (struct net_device *dev, +struct rtnl_link_stats64 *stats) +{ +dev_fetch_sw_netstats (stats, dev->tstats); + stats->tx_dropped =DEV_STATS_READ (dev, tx_dropped); +} +static void netkit_uninit (struct net_device *dev);static const struct net_device_ops netkit_netdev_ops = {
@@ -184,6 +199,7 @@ static const struct net_device_ops netkit_netdev_ops = { .ndo_set_rx_headroom = netkit_set_headroom, .ndo_get_iflink = netkit_get_iflink, .ndo_get_peer_dev = netkit_peer_dev, + .ndo_get_stats64 = netkit_get_stats, .ndo_uninit = netkit_uninit, .ndo_features_check = passthru_features_check, };
@@ -218,6 +234,7 @@ static void netkit_setup(struct net_device *dev)ether_setup (dev); dev->max_mtu =ETH_MAX_MTU ; + dev->pcpu_stat_type =NETDEV_PCPU_STAT_TSTATS ; dev->flags |=IFF_NOARP ; dev->priv_flags &= ~IFF_TX_SKB_SHARING ;
Signature · 1 line
2.34.1
Daniel Borkmann
maintainerPATCH v2 4/8 veth: Use tstats per-CPU traffic counters
In reply to Daniel BorkmannFrom: Peilin Ye <[email protected]>
Currently veth devices use the lstats per-CPU traffic counters, which only cover TX traffic. veth_get_stats64() actually populates RX stats of a veth device from its peer's TX counters, based on the assumption that a veth device can _only_ receive packets from its peer, which is no longer true:
For example, recent CNIs (like Cilium) can use the bpf_redirect_peer() BPF helper to redirect traffic from NIC's tc ingress to veth's tc ingress (in a different netns), skipping veth's peer device. Unfortunately, this kind of traffic isn't currently accounted for in veth's RX stats.
In preparation for the fix, use tstats (instead of lstats) to maintain both RX and TX counters for each veth device. We'll use RX counters for bpf_redirect_peer() traffic, and keep using TX counters for the usual "peer-to-peer" traffic. In veth_get_stats64(), calculate RX stats by _adding_ RX count to peer's TX count, in order to cover both kinds of traffic.
veth_stats_rx() might need a name change (perhaps to "veth_stats_xdp()") for less confusion, but let's leave it to another patch to keep the fix minimal.
--- drivers/net/veth.c | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-)
drivers/net/veth.c +11 -19
@@ -373,7 +373,7 @@ static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)skb_tx_timestamp (skb);if (likely (veth_forward_skb (rcv, skb, rq, use_napi) ==NET_RX_SUCCESS )) {if (!use_napi) -dev_lstats_add (dev, length); +dev_sw_netstats_tx_add (dev,1 , length);else __veth_xdp_flush (rq); }else {
@@ -387,14 +387,6 @@ static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)return ret; } -static u64 veth_stats_tx (struct net_device *dev,u64 *packets,u64 *bytes) -{ -struct veth_priv *priv =netdev_priv (dev); - -dev_lstats_read (dev, packets, bytes); -return atomic64_read (&priv->dropped); -} -static void veth_stats_rx (struct veth_stats *result,struct net_device *dev) {struct veth_priv *priv =netdev_priv (dev);
@@ -432,24 +424,24 @@ static void veth_get_stats64(struct net_device *dev,struct veth_priv *priv =netdev_priv (dev);struct net_device *peer;struct veth_stats rx; -u64 packets, bytes; - tot->tx_dropped =veth_stats_tx (dev, &packets, &bytes); - tot->tx_bytes = bytes; - tot->tx_packets = packets; + tot->tx_dropped =atomic64_read (&priv->dropped); +dev_fetch_sw_netstats (tot, dev->tstats);veth_stats_rx (&rx, dev); tot->tx_dropped += rx.xdp_tx_err; tot->rx_dropped = rx.rx_drops + rx.peer_tq_xdp_xmit_err; - tot->rx_bytes = rx.xdp_bytes; - tot->rx_packets = rx.xdp_packets; + tot->rx_bytes += rx.xdp_bytes; + tot->rx_packets += rx.xdp_packets;rcu_read_lock (); peer =rcu_dereference (priv->peer);if (peer) { -veth_stats_tx (peer, &packets, &bytes); - tot->rx_bytes += bytes; - tot->rx_packets += packets; +struct rtnl_link_stats64 tot_peer = {}; + +dev_fetch_sw_netstats (&tot_peer, peer->tstats); + tot->rx_bytes += tot_peer.tx_bytes; + tot->rx_packets += tot_peer.tx_packets;veth_stats_rx (&rx, peer); tot->tx_dropped += rx.peer_tq_xdp_xmit_err;
@@ -1783,7 +1775,7 @@ static void veth_setup(struct net_device *dev) NETIF_F_HW_VLAN_STAG_RX); dev->needs_free_netdev = true; dev->priv_destructor = veth_dev_free; - dev->pcpu_stat_type =NETDEV_PCPU_STAT_LSTATS ; + dev->pcpu_stat_type =NETDEV_PCPU_STAT_TSTATS ; dev->max_mtu =ETH_MAX_MTU ; dev->hw_features =VETH_FEATURES ;
Signature · 1 line
2.34.1
Re: [PATCH bpf v2 4/8] veth: Use tstats per-CPU traffic counters
Hi Daniel,
Thanks a lot for taking care of this!
Quoted 1 line · Daniel Borkmann
Signed-off-by Peilin Ye <[email protected]>
Would you like to add your Co-developed-by: here, since you've changed the code?
Quoted 1 line
Signed-off-by Daniel Borkmann <[email protected]>
Thanks,
Peilin Ye
Daniel Borkmann
maintainerRe: [PATCH bpf v2 4/8] veth: Use tstats per-CPU traffic counters
Hi Peilin,
Quoted 9 lines · Peilin Ye
Hi Daniel,
Thanks a lot for taking care of this!
Quoted 1 line · Daniel Borkmann
Signed-off-by Peilin Ye <[email protected]>Would you like to add your Co-developed-by: here, since you've changed the code?
Thanks, forgot about it, I'll just reply with the tag here:
Cheers,
Daniel
Re: [PATCH bpf v2 4/8] veth: Use tstats per-CPU traffic counters
In reply to Daniel BorkmannQuoted 23 lines · Daniel Borkmann
From: Peilin Ye <[email protected]>
Currently veth devices use the lstats per-CPU traffic counters, which only cover TX traffic.
veth_get_stats64()actually populates RX stats of a veth device from its peer's TX counters, based on the assumption that a veth device can _only_ receive packets from its peer, which is no longer true:For example, recent CNIs (like Cilium) can use the
bpf_redirect_peer()BPF helper to redirect traffic from NIC's tc ingress to veth's tc ingress (in a different netns), skipping veth's peer device. Unfortunately, this kind of traffic isn't currently accounted for in veth's RX stats.In preparation for the fix, use tstats (instead of lstats) to maintain both RX and TX counters for each veth device. We'll use RX counters for
bpf_redirect_peer()traffic, and keep using TX counters for the usual "peer-to-peer" traffic. Inveth_get_stats64(), calculate RX stats by _adding_ RX count to peer's TX count, in order to cover both kinds of traffic.
veth_stats_rx()might need a name change (perhaps to "veth_stats_xdp()") for less confusion, but let's leave it to another patch to keep the fix minimal.
Quoted 6 lines
Signed-off-by Peilin Ye <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>--- drivers/net/veth.c | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-)
Daniel Borkmann
maintainerPATCH v2 5/8 bpf: Fix dev's rx stats for bpf_redirect_peer traffic
In reply to Daniel BorkmannFrom: Peilin Ye <[email protected]>
Traffic redirected by bpf_redirect_peer() (used by recent CNIs like Cilium) is not accounted for in the RX stats of supported devices (that is, veth and netkit), confusing user space metrics collectors such as cAdvisor [0], as reported by Youlun.
Fix it by calling dev_sw_netstats_rx_add() in skb_do_redirect(), to update RX traffic counters. Devices that support ndo_get_peer_dev _must_ use the @tstats per-CPU counters (instead of @lstats, or @dstats).
To make this more fool-proof, error out when ndo_get_peer_dev is set but @tstats are not selected.
[0] Specifically, the "container_network_receive_{byte,packet}s_total" counters are affected.
--- net/core/dev.c | 8 ++++++++ net/core/filter.c | 1 + 2 files changed, 9 insertions(+)
net/core/dev.c +8 -0
@@ -10053,6 +10053,14 @@ static int netdev_do_alloc_pcpu_stats(struct net_device *dev) {void __percpu *v; +/* Drivers implementing ndo_get_peer_dev must support tstat +* accounting, so that skb_do_redirect() can bump the dev's +* RX stats upon network namespace switch. +*/ +if (dev->netdev_ops->ndo_get_peer_dev && + dev->pcpu_stat_type !=NETDEV_PCPU_STAT_TSTATS ) +return -EINVAL ; +switch (dev->pcpu_stat_type) {case NETDEV_PCPU_STAT_NONE :return 0 ;
net/core/filter.c +1 -0
@@ -2492,6 +2492,7 @@ int skb_do_redirect(struct sk_buff *skb) net_eq(net, dev_net(dev)))) goto out_drop; skb->dev = dev; +dev_sw_netstats_rx_add (dev, skb->len);return -EAGAIN ; } return flags & BPF_F_NEIGH ?
Signature · 1 line
2.34.1
Re: [PATCH bpf v2 5/8] bpf: Fix dev's rx stats for bpf_redirect_peer traffic
Quoted 22 lines · Daniel Borkmann
From: Peilin Ye <[email protected]>
Traffic redirected by
bpf_redirect_peer()(used by recent CNIs like Cilium) is not accounted for in the RX stats of supported devices (that is, veth and netkit), confusing user space metrics collectors such as cAdvisor [0], as reported by Youlun.Fix it by calling
dev_sw_netstats_rx_add()inskb_do_redirect(), to update RX traffic counters. Devices that supportndo_get_peer_dev_must_ use the @tstats per-CPU counters (instead of @lstats, or @dstats).To make this more fool-proof, error out when
ndo_get_peer_devis set but @tstats are not selected.[0] Specifically, the "container_network_receive_{byte,packet}s_total" counters are affected.
Fixes 9aa1206e8f48 ("bpf: Add redirect_peer helper")Reported-by Youlun Zhang <[email protected]>Signed-off-by Peilin Ye <[email protected]>Co-developed-by Daniel Borkmann <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>
Quoted 5 lines
--- net/core/dev.c | 8 ++++++++ net/core/filter.c | 1 + 2 files changed, 9 insertions(+)
Daniel Borkmann
maintainerPATCH v2 6/8 bpf, netkit: Add indirect call wrapper for fetching peer dev
In reply to Daniel Borkmannndo_get_peer_dev is used in tcx BPF fast path, therefore make use of indirect call wrapper and therefore optimize the bpf_redirect_peer() internal handling a bit. Add a small skb_get_peer_dev() wrapper which utilizes the INDIRECT_CALL_1() macro instead of open coding.
Future work could potentially add a peer pointer directly into struct net_device in future and convert veth and netkit over to use it so that eventually ndo_get_peer_dev can be removed.
--- drivers/net/netkit.c | 3 ++- include/net/netkit.h | 6 ++++++ net/core/filter.c | 18 +++++++++++++----- 3 files changed, 21 insertions(+), 6 deletions(-)
drivers/net/netkit.c +2 -1
@@ -7,6 +7,7 @@#include <linux/filter.h> #include <linux/netfilter_netdev.h> #include <linux/bpf_mprog.h> +#include <linux/indirect_call_wrapper.h> #include <net/netkit.h> #include <net/dst.h>
@@ -177,7 +178,7 @@ static void netkit_set_headroom(struct net_device *dev, int headroom)rcu_read_unlock (); } -static struct net_device *netkit_peer_dev (struct net_device *dev) +INDIRECT_CALLABLE_SCOPE struct net_device *netkit_peer_dev(struct net_device *dev) {return rcu_dereference (netkit_priv (dev)->peer); }
include/net/netkit.h +6 -0
@@ -10,6 +10,7 @@ int netkit_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog);int netkit_link_attach (const union bpf_attr *attr,struct bpf_prog *prog);int netkit_prog_detach (const union bpf_attr *attr,struct bpf_prog *prog);int netkit_prog_query (const union bpf_attr *attr,union bpf_attr __user *uattr); +INDIRECT_CALLABLE_DECLARE(struct net_device *netkit_peer_dev(struct net_device *dev));#else static inline int netkit_prog_attach (const union bpf_attr *attr,struct bpf_prog *prog)
@@ -34,5 +35,10 @@ static inline int netkit_prog_query(const union bpf_attr *attr, {return -EINVAL ; } + +static inline struct net_device *netkit_peer_dev (struct net_device *dev) +{ +return NULL ; +}#endif /* CONFIG_NETKIT */ #endif /* __NET_NETKIT_H */
net/core/filter.c +13 -5
@@ -81,6 +81,7 @@#include <net/xdp.h> #include <net/mptcp.h> #include <net/netfilter/nf_conntrack_bpf.h> +#include <net/netkit.h> #include <linux/un.h> #include "dev.h"
@@ -2468,6 +2469,16 @@ static const struct bpf_func_proto bpf_clone_redirect_proto = { DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);EXPORT_PER_CPU_SYMBOL_GPL (bpf_redirect_info); +static struct net_device *skb_get_peer_dev (struct net_device *dev) +{ +const struct net_device_ops *ops = dev->netdev_ops; + +if (likely (ops->ndo_get_peer_dev)) +return INDIRECT_CALL_1 (ops->ndo_get_peer_dev, + netkit_peer_dev, dev); +return NULL ; +} +int skb_do_redirect (struct sk_buff *skb) {struct bpf_redirect_info *ri =this_cpu_ptr (&bpf_redirect_info);
@@ -2481,12 +2492,9 @@ int skb_do_redirect(struct sk_buff *skb)if (unlikely (!dev)) goto out_drop;if (flags &BPF_F_PEER ) { -const struct net_device_ops *ops = dev->netdev_ops; - -if (unlikely (!ops->ndo_get_peer_dev || - !skb_at_tc_ingress (skb))) +if (unlikely (!skb_at_tc_ingress (skb))) goto out_drop; - dev = ops->ndo_get_peer_dev (dev); + dev =skb_get_peer_dev (dev);if (unlikely (!dev || !(dev->flags &IFF_UP ) ||net_eq (net,dev_net (dev))))
Signature · 1 line
2.34.1
Daniel Borkmann
maintainerPATCH v2 7/8 selftests/bpf: De-veth-ize the tc_redirect test case
In reply to Daniel BorkmannNo functional changes to the test case, but just renaming various functions, variables, etc, to remove veth part of their name for making it more generic and reusable later on (e.g. for netkit).
--- .../selftests/bpf/prog_tests/tc_redirect.c | 263 +++++++++--------- 1 file changed, 137 insertions(+), 126 deletions(-)
tools/testing/selftests/bpf/prog_tests/tc_redirect.c +137 -126
@@ -110,11 +110,16 @@ static void netns_setup_namespaces_nofail(const char *verb) } } +enum dev_mode { +MODE_VETH , +}; +struct netns_setup_result { -int ifindex_veth_src; -int ifindex_veth_src_fwd; -int ifindex_veth_dst; -int ifindex_veth_dst_fwd; +enum dev_mode dev_mode; +int ifindex_src; +int ifindex_src_fwd; +int ifindex_dst; +int ifindex_dst_fwd; };static int get_ifaddr (const char *name,char *ifaddr)
@@ -140,55 +145,59 @@ static int get_ifaddr(const char *name, char *ifaddr)static int netns_setup_links_and_routes (struct netns_setup_result *result) {struct nstoken *nstoken =NULL ; -char veth_src_fwd_addr[IFADDR_STR_LEN +1 ] = {}; +char src_fwd_addr[IFADDR_STR_LEN +1 ] = {}; -SYS (fail,"ip link add veth_src type veth peer name veth_src_fwd" ); -SYS (fail,"ip link add veth_dst type veth peer name veth_dst_fwd" ); +if (result->dev_mode ==MODE_VETH ) { +SYS (fail,"ip link add src type veth peer name src_fwd" ); +SYS (fail,"ip link add dst type veth peer name dst_fwd" ); -SYS (fail,"ip link set veth_dst_fwd address " MAC_DST_FWD ); -SYS (fail,"ip link set veth_dst address " MAC_DST ); +SYS (fail,"ip link set dst_fwd address " MAC_DST_FWD ); +SYS (fail,"ip link set dst address " MAC_DST ); + } -if (get_ifaddr ("veth_src_fwd" , veth_src_fwd_addr)) +if (get_ifaddr ("src_fwd" , src_fwd_addr)) goto fail; - result->ifindex_veth_src =if_nametoindex ("veth_src" ); -if (!ASSERT_GT (result->ifindex_veth_src,0 ,"ifindex_veth_src" )) + result->ifindex_src =if_nametoindex ("src" ); +if (!ASSERT_GT (result->ifindex_src,0 ,"ifindex_src" )) goto fail; - result->ifindex_veth_src_fwd =if_nametoindex ("veth_src_fwd" ); -if (!ASSERT_GT (result->ifindex_veth_src_fwd,0 ,"ifindex_veth_src_fwd" )) + result->ifindex_src_fwd =if_nametoindex ("src_fwd" ); +if (!ASSERT_GT (result->ifindex_src_fwd,0 ,"ifindex_src_fwd" )) goto fail; - result->ifindex_veth_dst =if_nametoindex ("veth_dst" ); -if (!ASSERT_GT (result->ifindex_veth_dst,0 ,"ifindex_veth_dst" )) + result->ifindex_dst =if_nametoindex ("dst" ); +if (!ASSERT_GT (result->ifindex_dst,0 ,"ifindex_dst" )) goto fail; - result->ifindex_veth_dst_fwd =if_nametoindex ("veth_dst_fwd" ); -if (!ASSERT_GT (result->ifindex_veth_dst_fwd,0 ,"ifindex_veth_dst_fwd" )) + result->ifindex_dst_fwd =if_nametoindex ("dst_fwd" ); +if (!ASSERT_GT (result->ifindex_dst_fwd,0 ,"ifindex_dst_fwd" )) goto fail; -SYS (fail,"ip link set veth_src netns " NS_SRC ); -SYS (fail,"ip link set veth_src_fwd netns " NS_FWD ); -SYS (fail,"ip link set veth_dst_fwd netns " NS_FWD ); -SYS (fail,"ip link set veth_dst netns " NS_DST ); +SYS (fail,"ip link set src netns " NS_SRC ); +SYS (fail,"ip link set src_fwd netns " NS_FWD ); +SYS (fail,"ip link set dst_fwd netns " NS_FWD ); +SYS (fail,"ip link set dst netns " NS_DST );/** setup in 'src' namespace */ nstoken =open_netns (NS_SRC );if (!ASSERT_OK_PTR (nstoken,"setns src" )) goto fail; -SYS (fail,"ip addr add " IP4_SRC "/32 dev veth_src" ); -SYS (fail,"ip addr add " IP6_SRC "/128 dev veth_src nodad" ); -SYS (fail,"ip link set dev veth_src up" ); +SYS (fail,"ip addr add " IP4_SRC "/32 dev src" ); +SYS (fail,"ip addr add " IP6_SRC "/128 dev src nodad" ); +SYS (fail,"ip link set dev src up" ); -SYS (fail,"ip route add " IP4_DST "/32 dev veth_src scope global" ); -SYS (fail,"ip route add " IP4_NET "/16 dev veth_src scope global" ); -SYS (fail,"ip route add " IP6_DST "/128 dev veth_src scope global" ); +SYS (fail,"ip route add " IP4_DST "/32 dev src scope global" ); +SYS (fail,"ip route add " IP4_NET "/16 dev src scope global" ); +SYS (fail,"ip route add " IP6_DST "/128 dev src scope global" ); -SYS (fail,"ip neigh add " IP4_DST " dev veth_src lladdr %s" , - veth_src_fwd_addr); -SYS (fail,"ip neigh add " IP6_DST " dev veth_src lladdr %s" , - veth_src_fwd_addr); +if (result->dev_mode ==MODE_VETH ) { +SYS (fail,"ip neigh add " IP4_DST " dev src lladdr %s" , + src_fwd_addr); +SYS (fail,"ip neigh add " IP6_DST " dev src lladdr %s" , + src_fwd_addr); + }close_netns (nstoken);
@@ -201,15 +210,15 @@ static int netns_setup_links_and_routes(struct netns_setup_result *result)* needs v4 one in order to start ARP probing. IP4_NET route is added * to the endpoints so that the ARP processing will reply. */ -SYS (fail,"ip addr add " IP4_SLL "/32 dev veth_src_fwd" ); -SYS (fail,"ip addr add " IP4_DLL "/32 dev veth_dst_fwd" ); -SYS (fail,"ip link set dev veth_src_fwd up" ); -SYS (fail,"ip link set dev veth_dst_fwd up" ); +SYS (fail,"ip addr add " IP4_SLL "/32 dev src_fwd" ); +SYS (fail,"ip addr add " IP4_DLL "/32 dev dst_fwd" ); +SYS (fail,"ip link set dev src_fwd up" ); +SYS (fail,"ip link set dev dst_fwd up" ); -SYS (fail,"ip route add " IP4_SRC "/32 dev veth_src_fwd scope global" ); -SYS (fail,"ip route add " IP6_SRC "/128 dev veth_src_fwd scope global" ); -SYS (fail,"ip route add " IP4_DST "/32 dev veth_dst_fwd scope global" ); -SYS (fail,"ip route add " IP6_DST "/128 dev veth_dst_fwd scope global" ); +SYS (fail,"ip route add " IP4_SRC "/32 dev src_fwd scope global" ); +SYS (fail,"ip route add " IP6_SRC "/128 dev src_fwd scope global" ); +SYS (fail,"ip route add " IP4_DST "/32 dev dst_fwd scope global" ); +SYS (fail,"ip route add " IP6_DST "/128 dev dst_fwd scope global" );close_netns (nstoken);
@@ -218,16 +227,18 @@ static int netns_setup_links_and_routes(struct netns_setup_result *result)if (!ASSERT_OK_PTR (nstoken,"setns dst" )) goto fail; -SYS (fail,"ip addr add " IP4_DST "/32 dev veth_dst" ); -SYS (fail,"ip addr add " IP6_DST "/128 dev veth_dst nodad" ); -SYS (fail,"ip link set dev veth_dst up" ); +SYS (fail,"ip addr add " IP4_DST "/32 dev dst" ); +SYS (fail,"ip addr add " IP6_DST "/128 dev dst nodad" ); +SYS (fail,"ip link set dev dst up" ); -SYS (fail,"ip route add " IP4_SRC "/32 dev veth_dst scope global" ); -SYS (fail,"ip route add " IP4_NET "/16 dev veth_dst scope global" ); -SYS (fail,"ip route add " IP6_SRC "/128 dev veth_dst scope global" ); +SYS (fail,"ip route add " IP4_SRC "/32 dev dst scope global" ); +SYS (fail,"ip route add " IP4_NET "/16 dev dst scope global" ); +SYS (fail,"ip route add " IP6_SRC "/128 dev dst scope global" ); -SYS (fail,"ip neigh add " IP4_SRC " dev veth_dst lladdr " MAC_DST_FWD ); -SYS (fail,"ip neigh add " IP6_SRC " dev veth_dst lladdr " MAC_DST_FWD ); +if (result->dev_mode ==MODE_VETH ) { +SYS (fail,"ip neigh add " IP4_SRC " dev dst lladdr " MAC_DST_FWD ); +SYS (fail,"ip neigh add " IP6_SRC " dev dst lladdr " MAC_DST_FWD ); + }close_netns (nstoken);
@@ -293,23 +304,23 @@ static int netns_load_bpf(const struct bpf_program *src_prog,const struct bpf_program *chk_prog, const struct netns_setup_result *setup_result) { -LIBBPF_OPTS (bpf_tc_hook ,qdisc_veth_src_fwd ); -LIBBPF_OPTS (bpf_tc_hook, qdisc_veth_dst_fwd); +LIBBPF_OPTS (bpf_tc_hook ,qdisc_src_fwd ); +LIBBPF_OPTS (bpf_tc_hook, qdisc_dst_fwd);int err; -/* tc qdisc add dev veth_src_fwd clsact */ -QDISC_CLSACT_CREATE (&qdisc_veth_src_fwd, setup_result->ifindex_veth_src_fwd); -/* tc filter add dev veth_src_fwd ingress bpf da src_prog */ -XGRESS_FILTER_ADD (&qdisc_veth_src_fwd,BPF_TC_INGRESS , src_prog,0 ); -/* tc filter add dev veth_src_fwd egress bpf da chk_prog */ -XGRESS_FILTER_ADD (&qdisc_veth_src_fwd,BPF_TC_EGRESS , chk_prog,0 ); +/* tc qdisc add dev src_fwd clsact */ +QDISC_CLSACT_CREATE (&qdisc_src_fwd, setup_result->ifindex_src_fwd); +/* tc filter add dev src_fwd ingress bpf da src_prog */ +XGRESS_FILTER_ADD (&qdisc_src_fwd,BPF_TC_INGRESS , src_prog,0 ); +/* tc filter add dev src_fwd egress bpf da chk_prog */ +XGRESS_FILTER_ADD (&qdisc_src_fwd,BPF_TC_EGRESS , chk_prog,0 ); -/* tc qdisc add dev veth_dst_fwd clsact */ -QDISC_CLSACT_CREATE (&qdisc_veth_dst_fwd, setup_result->ifindex_veth_dst_fwd); -/* tc filter add dev veth_dst_fwd ingress bpf da dst_prog */ -XGRESS_FILTER_ADD (&qdisc_veth_dst_fwd,BPF_TC_INGRESS , dst_prog,0 ); -/* tc filter add dev veth_dst_fwd egress bpf da chk_prog */ -XGRESS_FILTER_ADD (&qdisc_veth_dst_fwd,BPF_TC_EGRESS , chk_prog,0 ); +/* tc qdisc add dev dst_fwd clsact */ +QDISC_CLSACT_CREATE (&qdisc_dst_fwd, setup_result->ifindex_dst_fwd); +/* tc filter add dev dst_fwd ingress bpf da dst_prog */ +XGRESS_FILTER_ADD (&qdisc_dst_fwd,BPF_TC_INGRESS , dst_prog,0 ); +/* tc filter add dev dst_fwd egress bpf da chk_prog */ +XGRESS_FILTER_ADD (&qdisc_dst_fwd,BPF_TC_EGRESS , chk_prog,0 );return 0 ; fail:
@@ -539,10 +550,10 @@ static void test_inet_dtime(int family, int type, const char *addr, __u16 port)static int netns_load_dtime_bpf (struct test_tc_dtime *skel,const struct netns_setup_result *setup_result) { -LIBBPF_OPTS (bpf_tc_hook, qdisc_veth_src_fwd); -LIBBPF_OPTS (bpf_tc_hook, qdisc_veth_dst_fwd); -LIBBPF_OPTS (bpf_tc_hook, qdisc_veth_src); -LIBBPF_OPTS (bpf_tc_hook, qdisc_veth_dst); +LIBBPF_OPTS (bpf_tc_hook, qdisc_src_fwd); +LIBBPF_OPTS (bpf_tc_hook, qdisc_dst_fwd); +LIBBPF_OPTS (bpf_tc_hook, qdisc_src); +LIBBPF_OPTS (bpf_tc_hook, qdisc_dst);struct nstoken *nstoken;int err;
@@ -550,58 +561,58 @@ static int netns_load_dtime_bpf(struct test_tc_dtime *skel, nstoken =open_netns (NS_SRC );if (!ASSERT_OK_PTR (nstoken,"setns " NS_SRC ))return -1 ; -/* tc qdisc add dev veth_src clsact */ -QDISC_CLSACT_CREATE (&qdisc_veth_src, setup_result->ifindex_veth_src); -/* tc filter add dev veth_src ingress bpf da ingress_host */ -XGRESS_FILTER_ADD (&qdisc_veth_src,BPF_TC_INGRESS , skel->progs.ingress_host,0 ); -/* tc filter add dev veth_src egress bpf da egress_host */ -XGRESS_FILTER_ADD (&qdisc_veth_src,BPF_TC_EGRESS , skel->progs.egress_host,0 ); +/* tc qdisc add dev src clsact */ +QDISC_CLSACT_CREATE (&qdisc_src, setup_result->ifindex_src); +/* tc filter add dev src ingress bpf da ingress_host */ +XGRESS_FILTER_ADD (&qdisc_src,BPF_TC_INGRESS , skel->progs.ingress_host,0 ); +/* tc filter add dev src egress bpf da egress_host */ +XGRESS_FILTER_ADD (&qdisc_src,BPF_TC_EGRESS , skel->progs.egress_host,0 );close_netns (nstoken);/* setup ns_dst tc progs */ nstoken =open_netns (NS_DST );if (!ASSERT_OK_PTR (nstoken,"setns " NS_DST ))return -1 ; -/* tc qdisc add dev veth_dst clsact */ -QDISC_CLSACT_CREATE (&qdisc_veth_dst, setup_result->ifindex_veth_dst); -/* tc filter add dev veth_dst ingress bpf da ingress_host */ -XGRESS_FILTER_ADD (&qdisc_veth_dst,BPF_TC_INGRESS , skel->progs.ingress_host,0 ); -/* tc filter add dev veth_dst egress bpf da egress_host */ -XGRESS_FILTER_ADD (&qdisc_veth_dst,BPF_TC_EGRESS , skel->progs.egress_host,0 ); +/* tc qdisc add dev dst clsact */ +QDISC_CLSACT_CREATE (&qdisc_dst, setup_result->ifindex_dst); +/* tc filter add dev dst ingress bpf da ingress_host */ +XGRESS_FILTER_ADD (&qdisc_dst,BPF_TC_INGRESS , skel->progs.ingress_host,0 ); +/* tc filter add dev dst egress bpf da egress_host */ +XGRESS_FILTER_ADD (&qdisc_dst,BPF_TC_EGRESS , skel->progs.egress_host,0 );close_netns (nstoken);/* setup ns_fwd tc progs */ nstoken =open_netns (NS_FWD );if (!ASSERT_OK_PTR (nstoken,"setns " NS_FWD ))return -1 ; -/* tc qdisc add dev veth_dst_fwd clsact */ -QDISC_CLSACT_CREATE (&qdisc_veth_dst_fwd, setup_result->ifindex_veth_dst_fwd); -/* tc filter add dev veth_dst_fwd ingress prio 100 bpf da ingress_fwdns_prio100 */ -XGRESS_FILTER_ADD (&qdisc_veth_dst_fwd,BPF_TC_INGRESS , +/* tc qdisc add dev dst_fwd clsact */ +QDISC_CLSACT_CREATE (&qdisc_dst_fwd, setup_result->ifindex_dst_fwd); +/* tc filter add dev dst_fwd ingress prio 100 bpf da ingress_fwdns_prio100 */ +XGRESS_FILTER_ADD (&qdisc_dst_fwd,BPF_TC_INGRESS , skel->progs.ingress_fwdns_prio100,100 ); -/* tc filter add dev veth_dst_fwd ingress prio 101 bpf da ingress_fwdns_prio101 */ -XGRESS_FILTER_ADD (&qdisc_veth_dst_fwd,BPF_TC_INGRESS , +/* tc filter add dev dst_fwd ingress prio 101 bpf da ingress_fwdns_prio101 */ +XGRESS_FILTER_ADD (&qdisc_dst_fwd,BPF_TC_INGRESS , skel->progs.ingress_fwdns_prio101,101 ); -/* tc filter add dev veth_dst_fwd egress prio 100 bpf da egress_fwdns_prio100 */ -XGRESS_FILTER_ADD (&qdisc_veth_dst_fwd,BPF_TC_EGRESS , +/* tc filter add dev dst_fwd egress prio 100 bpf da egress_fwdns_prio100 */ +XGRESS_FILTER_ADD (&qdisc_dst_fwd,BPF_TC_EGRESS , skel->progs.egress_fwdns_prio100,100 ); -/* tc filter add dev veth_dst_fwd egress prio 101 bpf da egress_fwdns_prio101 */ -XGRESS_FILTER_ADD (&qdisc_veth_dst_fwd,BPF_TC_EGRESS , +/* tc filter add dev dst_fwd egress prio 101 bpf da egress_fwdns_prio101 */ +XGRESS_FILTER_ADD (&qdisc_dst_fwd,BPF_TC_EGRESS , skel->progs.egress_fwdns_prio101,101 ); -/* tc qdisc add dev veth_src_fwd clsact */ -QDISC_CLSACT_CREATE (&qdisc_veth_src_fwd, setup_result->ifindex_veth_src_fwd); -/* tc filter add dev veth_src_fwd ingress prio 100 bpf da ingress_fwdns_prio100 */ -XGRESS_FILTER_ADD (&qdisc_veth_src_fwd,BPF_TC_INGRESS , +/* tc qdisc add dev src_fwd clsact */ +QDISC_CLSACT_CREATE (&qdisc_src_fwd, setup_result->ifindex_src_fwd); +/* tc filter add dev src_fwd ingress prio 100 bpf da ingress_fwdns_prio100 */ +XGRESS_FILTER_ADD (&qdisc_src_fwd,BPF_TC_INGRESS , skel->progs.ingress_fwdns_prio100,100 ); -/* tc filter add dev veth_src_fwd ingress prio 101 bpf da ingress_fwdns_prio101 */ -XGRESS_FILTER_ADD (&qdisc_veth_src_fwd,BPF_TC_INGRESS , +/* tc filter add dev src_fwd ingress prio 101 bpf da ingress_fwdns_prio101 */ +XGRESS_FILTER_ADD (&qdisc_src_fwd,BPF_TC_INGRESS , skel->progs.ingress_fwdns_prio101,101 ); -/* tc filter add dev veth_src_fwd egress prio 100 bpf da egress_fwdns_prio100 */ -XGRESS_FILTER_ADD (&qdisc_veth_src_fwd,BPF_TC_EGRESS , +/* tc filter add dev src_fwd egress prio 100 bpf da egress_fwdns_prio100 */ +XGRESS_FILTER_ADD (&qdisc_src_fwd,BPF_TC_EGRESS , skel->progs.egress_fwdns_prio100,100 ); -/* tc filter add dev veth_src_fwd egress prio 101 bpf da egress_fwdns_prio101 */ -XGRESS_FILTER_ADD (&qdisc_veth_src_fwd,BPF_TC_EGRESS , +/* tc filter add dev src_fwd egress prio 101 bpf da egress_fwdns_prio101 */ +XGRESS_FILTER_ADD (&qdisc_src_fwd,BPF_TC_EGRESS , skel->progs.egress_fwdns_prio101,101 );close_netns (nstoken);return 0 ;
@@ -777,8 +788,8 @@ static void test_tc_redirect_dtime(struct netns_setup_result *setup_result)if (!ASSERT_OK_PTR (skel,"test_tc_dtime__open" ))return ; - skel->rodata->IFINDEX_SRC = setup_result->ifindex_veth_src_fwd; - skel->rodata->IFINDEX_DST = setup_result->ifindex_veth_dst_fwd; + skel->rodata->IFINDEX_SRC = setup_result->ifindex_src_fwd; + skel->rodata->IFINDEX_DST = setup_result->ifindex_dst_fwd; err =test_tc_dtime__load (skel);if (!ASSERT_OK (err,"test_tc_dtime__load" ))
@@ -868,8 +879,8 @@ static void test_tc_redirect_neigh(struct netns_setup_result *setup_result)if (!ASSERT_OK_PTR (skel,"test_tc_neigh__open" )) goto done; - skel->rodata->IFINDEX_SRC = setup_result->ifindex_veth_src_fwd; - skel->rodata->IFINDEX_DST = setup_result->ifindex_veth_dst_fwd; + skel->rodata->IFINDEX_SRC = setup_result->ifindex_src_fwd; + skel->rodata->IFINDEX_DST = setup_result->ifindex_dst_fwd; err =test_tc_neigh__load (skel);if (!ASSERT_OK (err,"test_tc_neigh__load" ))
@@ -904,8 +915,8 @@ static void test_tc_redirect_peer(struct netns_setup_result *setup_result)if (!ASSERT_OK_PTR (skel,"test_tc_peer__open" )) goto done; - skel->rodata->IFINDEX_SRC = setup_result->ifindex_veth_src_fwd; - skel->rodata->IFINDEX_DST = setup_result->ifindex_veth_dst_fwd; + skel->rodata->IFINDEX_SRC = setup_result->ifindex_src_fwd; + skel->rodata->IFINDEX_DST = setup_result->ifindex_dst_fwd; err =test_tc_peer__load (skel);if (!ASSERT_OK (err,"test_tc_peer__load" ))
@@ -996,7 +1007,7 @@ static int tun_relay_loop(int src_fd, int target_fd)static void test_tc_redirect_peer_l3 (struct netns_setup_result *setup_result) {LIBBPF_OPTS (bpf_tc_hook, qdisc_tun_fwd); -LIBBPF_OPTS (bpf_tc_hook, qdisc_veth_dst_fwd); +LIBBPF_OPTS (bpf_tc_hook, qdisc_dst_fwd);struct test_tc_peer *skel =NULL ;struct nstoken *nstoken =NULL ;int err;
@@ -1045,7 +1056,7 @@ static void test_tc_redirect_peer_l3(struct netns_setup_result *setup_result) goto fail; skel->rodata->IFINDEX_SRC = ifindex; - skel->rodata->IFINDEX_DST = setup_result->ifindex_veth_dst_fwd; + skel->rodata->IFINDEX_DST = setup_result->ifindex_dst_fwd; err =test_tc_peer__load (skel);if (!ASSERT_OK (err,"test_tc_peer__load" ))
@@ -1053,19 +1064,19 @@ static void test_tc_redirect_peer_l3(struct netns_setup_result *setup_result)/* Load "tc_src_l3" to the tun_fwd interface to redirect packets * towards dst, and "tc_dst" to redirect packets -* and "tc_chk" on veth_dst_fwd to drop non-redirected packets. +* and "tc_chk" on dst_fwd to drop non-redirected packets. */ /* tc qdisc add dev tun_fwd clsact */ QDISC_CLSACT_CREATE (&qdisc_tun_fwd, ifindex);/* tc filter add dev tun_fwd ingress bpf da tc_src_l3 */ XGRESS_FILTER_ADD (&qdisc_tun_fwd,BPF_TC_INGRESS , skel->progs.tc_src_l3,0 ); -/* tc qdisc add dev veth_dst_fwd clsact */ -QDISC_CLSACT_CREATE (&qdisc_veth_dst_fwd, setup_result->ifindex_veth_dst_fwd); -/* tc filter add dev veth_dst_fwd ingress bpf da tc_dst_l3 */ -XGRESS_FILTER_ADD (&qdisc_veth_dst_fwd,BPF_TC_INGRESS , skel->progs.tc_dst_l3,0 ); -/* tc filter add dev veth_dst_fwd egress bpf da tc_chk */ -XGRESS_FILTER_ADD (&qdisc_veth_dst_fwd,BPF_TC_EGRESS , skel->progs.tc_chk,0 ); +/* tc qdisc add dev dst_fwd clsact */ +QDISC_CLSACT_CREATE (&qdisc_dst_fwd, setup_result->ifindex_dst_fwd); +/* tc filter add dev dst_fwd ingress bpf da tc_dst_l3 */ +XGRESS_FILTER_ADD (&qdisc_dst_fwd,BPF_TC_INGRESS , skel->progs.tc_dst_l3,0 ); +/* tc filter add dev dst_fwd egress bpf da tc_chk */ +XGRESS_FILTER_ADD (&qdisc_dst_fwd,BPF_TC_EGRESS , skel->progs.tc_chk,0 );/* Setup route and neigh tables */ SYS (fail,"ip -netns " NS_SRC " addr add dev tun_src " IP4_TUN_SRC "/24" );
@@ -1074,17 +1085,17 @@ static void test_tc_redirect_peer_l3(struct netns_setup_result *setup_result)SYS (fail,"ip -netns " NS_SRC " addr add dev tun_src " IP6_TUN_SRC "/64 nodad" );SYS (fail,"ip -netns " NS_FWD " addr add dev tun_fwd " IP6_TUN_FWD "/64 nodad" ); -SYS (fail,"ip -netns " NS_SRC " route del " IP4_DST "/32 dev veth_src scope global" ); +SYS (fail,"ip -netns " NS_SRC " route del " IP4_DST "/32 dev src scope global" );SYS (fail,"ip -netns " NS_SRC " route add " IP4_DST "/32 via " IP4_TUN_FWD " dev tun_src scope global" ); -SYS (fail,"ip -netns " NS_DST " route add " IP4_TUN_SRC "/32 dev veth_dst scope global" ); -SYS (fail,"ip -netns " NS_SRC " route del " IP6_DST "/128 dev veth_src scope global" ); +SYS (fail,"ip -netns " NS_DST " route add " IP4_TUN_SRC "/32 dev dst scope global" ); +SYS (fail,"ip -netns " NS_SRC " route del " IP6_DST "/128 dev src scope global" );SYS (fail,"ip -netns " NS_SRC " route add " IP6_DST "/128 via " IP6_TUN_FWD " dev tun_src scope global" ); -SYS (fail,"ip -netns " NS_DST " route add " IP6_TUN_SRC "/128 dev veth_dst scope global" ); +SYS (fail,"ip -netns " NS_DST " route add " IP6_TUN_SRC "/128 dev dst scope global" ); -SYS (fail,"ip -netns " NS_DST " neigh add " IP4_TUN_SRC " dev veth_dst lladdr " MAC_DST_FWD ); -SYS (fail,"ip -netns " NS_DST " neigh add " IP6_TUN_SRC " dev veth_dst lladdr " MAC_DST_FWD ); +SYS (fail,"ip -netns " NS_DST " neigh add " IP4_TUN_SRC " dev dst lladdr " MAC_DST_FWD ); +SYS (fail,"ip -netns " NS_DST " neigh add " IP6_TUN_SRC " dev dst lladdr " MAC_DST_FWD );if (!ASSERT_OK (set_forwarding (false),"disable forwarding" )) goto fail;
@@ -1106,9 +1117,9 @@ static void test_tc_redirect_peer_l3(struct netns_setup_result *setup_result)close_netns (nstoken); } -#define RUN_TEST (name) \ +#define RUN_TEST (name, mode) \ ({ \ - struct netns_setup_result setup_result; \ + struct netns_setup_result setup_result = { .dev_mode = mode, }; \ if (test__start_subtest(#name)) \ if (ASSERT_OK(netns_setup_namespaces("add"), "setup namespaces")) { \ if (ASSERT_OK(netns_setup_links_and_routes(&setup_result), \
@@ -1122,11 +1133,11 @@ static void *test_tc_redirect_run_tests(void *arg) {netns_setup_namespaces_nofail ("delete" ); -RUN_TEST (tc_redirect_peer); -RUN_TEST (tc_redirect_peer_l3); -RUN_TEST (tc_redirect_neigh); -RUN_TEST (tc_redirect_neigh_fib); -RUN_TEST (tc_redirect_dtime); +RUN_TEST (tc_redirect_peer,MODE_VETH ); +RUN_TEST (tc_redirect_peer_l3,MODE_VETH ); +RUN_TEST (tc_redirect_neigh,MODE_VETH ); +RUN_TEST (tc_redirect_neigh_fib,MODE_VETH ); +RUN_TEST (tc_redirect_dtime,MODE_VETH );return NULL ; }
Signature · 1 line
2.34.1
Re: [PATCH bpf v2 7/8] selftests/bpf: De-veth-ize the tc_redirect test case
Quoted 10 lines · Daniel Borkmann
No functional changes to the test case, but just renaming various functions, variables, etc, to remove veth part of their name for making it more generic and reusable later on (e.g. for netkit).
Signed-off-by Daniel Borkmann <[email protected]>--- .../selftests/bpf/prog_tests/tc_redirect.c | 263 +++++++++--------- 1 file changed, 137 insertions(+), 126 deletions(-)
Daniel Borkmann
maintainerPATCH v2 8/8 selftests/bpf: Add netkit to tc_redirect selftest
In reply to Daniel BorkmannExtend the existing tc_redirect selftest to also cover netkit devices for exercising the bpf_redirect_peer() code paths, so that we have both veth as well as netkit covered, all tests still pass after this change.
--- .../selftests/bpf/prog_tests/tc_redirect.c | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+)
tools/testing/selftests/bpf/prog_tests/tc_redirect.c +52 -0
@@ -24,6 +24,7 @@#include "test_progs.h" #include "network_helpers.h" +#include "netlink_helpers.h" #include "test_tc_neigh_fib.skel.h" #include "test_tc_neigh.skel.h" #include "test_tc_peer.skel.h"
@@ -112,6 +113,7 @@ static void netns_setup_namespaces_nofail(const char *verb)enum dev_mode {MODE_VETH , +MODE_NETKIT , };struct netns_setup_result {
@@ -142,10 +144,51 @@ static int get_ifaddr(const char *name, char *ifaddr)return 0 ; } +static int create_netkit (int mode,char *prim,char *peer) +{ +struct rtattr *linkinfo, *data, *peer_info; +struct rtnl_handle rth = { .fd =-1 }; +const char *type ="netkit" ; +struct { +struct nlmsghdr n; +struct ifinfomsg i; +char buf[1024 ]; + } req = {}; +int err; + + err =rtnl_open (&rth,0 ); +if (!ASSERT_OK (err,"open_rtnetlink" )) +return err; + +memset (&req,0 ,sizeof (req)); + req.n.nlmsg_len =NLMSG_LENGTH (sizeof (struct ifinfomsg )); + req.n.nlmsg_flags =NLM_F_REQUEST |NLM_F_CREATE |NLM_F_EXCL ; + req.n.nlmsg_type =RTM_NEWLINK ; + req.i.ifi_family =AF_UNSPEC ; + +addattr_l (&req.n,sizeof (req),IFLA_IFNAME , prim,strlen (prim)); + linkinfo =addattr_nest (&req.n,sizeof (req),IFLA_LINKINFO ); +addattr_l (&req.n,sizeof (req),IFLA_INFO_KIND , type,strlen (type)); + data =addattr_nest (&req.n,sizeof (req),IFLA_INFO_DATA ); +addattr32 (&req.n,sizeof (req),IFLA_NETKIT_MODE , mode); + peer_info =addattr_nest (&req.n,sizeof (req),IFLA_NETKIT_PEER_INFO ); + req.n.nlmsg_len +=sizeof (struct ifinfomsg ); +addattr_l (&req.n,sizeof (req),IFLA_IFNAME , peer,strlen (peer)); +addattr_nest_end (&req.n, peer_info); +addattr_nest_end (&req.n, data); +addattr_nest_end (&req.n, linkinfo); + + err =rtnl_talk (&rth, &req.n,NULL ); +ASSERT_OK (err,"talk_rtnetlink" ); +rtnl_close (&rth); +return err; +} +static int netns_setup_links_and_routes (struct netns_setup_result *result) {struct nstoken *nstoken =NULL ;char src_fwd_addr[IFADDR_STR_LEN +1 ] = {}; +int err;if (result->dev_mode ==MODE_VETH ) {SYS (fail,"ip link add src type veth peer name src_fwd" );
@@ -153,6 +196,13 @@ static int netns_setup_links_and_routes(struct netns_setup_result *result)SYS (fail,"ip link set dst_fwd address " MAC_DST_FWD );SYS (fail,"ip link set dst address " MAC_DST ); + }else if (result->dev_mode ==MODE_NETKIT ) { + err =create_netkit (NETKIT_L3 ,"src" ,"src_fwd" ); +if (!ASSERT_OK (err,"create_ifindex_src" )) + goto fail; + err =create_netkit (NETKIT_L3 ,"dst" ,"dst_fwd" ); +if (!ASSERT_OK (err,"create_ifindex_dst" )) + goto fail; }if (get_ifaddr ("src_fwd" , src_fwd_addr))
@@ -1134,7 +1184,9 @@ static void *test_tc_redirect_run_tests(void *arg)netns_setup_namespaces_nofail ("delete" );RUN_TEST (tc_redirect_peer,MODE_VETH ); +RUN_TEST (tc_redirect_peer,MODE_NETKIT );RUN_TEST (tc_redirect_peer_l3,MODE_VETH ); +RUN_TEST (tc_redirect_peer_l3,MODE_NETKIT );RUN_TEST (tc_redirect_neigh,MODE_VETH );RUN_TEST (tc_redirect_neigh_fib,MODE_VETH );RUN_TEST (tc_redirect_dtime,MODE_VETH );
Signature · 1 line
2.34.1
Re: [PATCH bpf v2 8/8] selftests/bpf: Add netkit to tc_redirect selftest
Quoted 10 lines · Daniel Borkmann
Extend the existing
tc_redirectselftest to also cover netkit devices for exercising thebpf_redirect_peer()code paths, so that we have both veth as well as netkit covered, all tests still pass after this change.Signed-off-by Daniel Borkmann <[email protected]>--- .../selftests/bpf/prog_tests/tc_redirect.c | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+)