From: Maciej Fijalkowski Once veth will start to use __do_xdp_generic(), this will be helpful for correct stats values being incremented - otherwise we would not have a knowledge whether XDP_TX succeeded or not. With this being implemented we will only bump xdp_tx stat when actual transmission happened. The generic redirect callers in devmap and filter keep returning success. generic_xdp_tx() frees and accounts a failed transmit itself, while the caller of a failed redirect frees the skb again, and in devmap the original skb may already be consumed after skb_copy(). As with native XDP, a redirect succeeds once the packet reached the target device. Signed-off-by: Maciej Fijalkowski [sst: drop the (void) casts and explain why the redirect paths do not propagate the transmit error (Björn); rebased onto Vladimir Oltean's "net: pass the XDP action to generic_xdp_tx() for tracing" and "net: validate xmit skbs in generic_xdp_tx()": keep the act argument, and return 0 only if the driver took the skb or every segment that validation made of it] Signed-off-by: Slawomir Stepien --- include/linux/netdevice.h | 4 ++-- net/core/dev.c | 16 ++++++++++++---- net/core/filter.c | 7 +++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index f18bafb4efe2..b251c4b5c9ce 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -4357,8 +4357,8 @@ static inline void dev_consume_skb_any(struct sk_buff *skb) u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp, const struct bpf_prog *xdp_prog); -void generic_xdp_tx(struct sk_buff *skb, const struct bpf_prog *xdp_prog, - u32 act); +int generic_xdp_tx(struct sk_buff *skb, const struct bpf_prog *xdp_prog, + u32 act); int do_xdp_generic(const struct bpf_prog *xdp_prog, struct sk_buff **pskb); int netif_rx(struct sk_buff *skb); int __netif_rx(struct sk_buff *skb); diff --git a/net/core/dev.c b/net/core/dev.c index 6e330fc415cb..12595f6b9eba 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -5854,13 +5854,17 @@ static u32 netif_receive_generic_xdp(struct sk_buff **pskb, * that XDP packets are able to starve other packets going through a qdisc, * and DDOS attacks will be more effective. In-driver-XDP use dedicated TX * queues, so they do not have this starvation issue. + * + * Returns 0 if the driver took the skb, or all segments that validation made + * of it. Otherwise returns an error; the skb is consumed either way. */ -void generic_xdp_tx(struct sk_buff *skb, const struct bpf_prog *xdp_prog, - u32 act) +int generic_xdp_tx(struct sk_buff *skb, const struct bpf_prog *xdp_prog, + u32 act) { struct net_device *dev = skb->dev; enum skb_drop_reason reason; struct netdev_queue *txq; + int err = -ENETDOWN; bool again = false; int cpu, rc; @@ -5869,14 +5873,14 @@ void generic_xdp_tx(struct sk_buff *skb, const struct bpf_prog *xdp_prog, trace_xdp_exception(dev, xdp_prog, act); dev_core_stats_tx_dropped_inc(dev); kfree_skb_reason(skb, reason); - return; + return -EINVAL; } txq = netdev_core_pick_tx(dev, skb, NULL); skb = validate_xmit_skb_list(skb, dev, &again); if (!skb) { trace_xdp_exception(dev, xdp_prog, act); - return; + return -EINVAL; } cpu = smp_processor_id(); @@ -5888,6 +5892,7 @@ void generic_xdp_tx(struct sk_buff *skb, const struct bpf_prog *xdp_prog, rc = netdev_start_xmit(skb, dev, txq, false); if (!dev_xmit_complete(rc)) { skb->next = next; + err = -EBUSY; break; } skb = next; @@ -5897,7 +5902,10 @@ void generic_xdp_tx(struct sk_buff *skb, const struct bpf_prog *xdp_prog, trace_xdp_exception(dev, xdp_prog, act); dev_core_stats_tx_dropped_inc(dev); kfree_skb_list(skb); + return err; } + + return 0; } static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key); diff --git a/net/core/filter.c b/net/core/filter.c index aca8160b26a1..b7241cbfd8f1 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -4883,6 +4883,13 @@ int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb, skb->dev = fwd; _trace_xdp_redirect(dev, xdp_prog, ri->tgt_index); + /* generic_xdp_tx() consumes the skb: either the driver took it, + * or the transmit failed and generic_xdp_tx() traced the drop, + * counted it in the target's tx_dropped and freed the skb. + * Returning its error would make our caller free the skb again, + * so report success, as native XDP does once a frame has been + * handed to the target device. + */ generic_xdp_tx(skb, xdp_prog, XDP_REDIRECT); return 0; } -- 2.56.0