Skip to content

Commit 9247080

Browse files
John FastabendJeff Kirsher
John Fastabend
authored and
Jeff Kirsher
committed
ixgbe: add XDP support for pass and drop actions
Basic XDP drop support for ixgbe. Uses READ_ONCE/xchg semantics on XDP programs instead of RCU primitives as suggested by Daniel Borkmann and Alex Duyck. v2: fix the build issues seen w/ XDP when page sizes are larger than 4K and made minor fixes based on feedback from Jakub Kicinski Signed-off-by: John Fastabend <[email protected]> Acked-by: Alexander Duyck <[email protected]> Signed-off-by: Jeff Kirsher <[email protected]>
1 parent 6133406 commit 9247080

File tree

3 files changed

+148
-29
lines changed

3 files changed

+148
-29
lines changed

drivers/net/ethernet/intel/ixgbe/ixgbe.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ struct ixgbe_ring {
318318
struct ixgbe_ring *next; /* pointer to next ring in q_vector */
319319
struct ixgbe_q_vector *q_vector; /* backpointer to host q_vector */
320320
struct net_device *netdev; /* netdev ring belongs to */
321+
struct bpf_prog *xdp_prog;
321322
struct device *dev; /* device for DMA mapping */
322323
struct ixgbe_fwd_adapter *l2_accel_priv;
323324
void *desc; /* descriptor ring memory */
@@ -555,6 +556,7 @@ struct ixgbe_adapter {
555556
unsigned long active_vlans[BITS_TO_LONGS(VLAN_N_VID)];
556557
/* OS defined structs */
557558
struct net_device *netdev;
559+
struct bpf_prog *xdp_prog;
558560
struct pci_dev *pdev;
559561

560562
unsigned long state;
@@ -835,7 +837,7 @@ void ixgbe_down(struct ixgbe_adapter *adapter);
835837
void ixgbe_reinit_locked(struct ixgbe_adapter *adapter);
836838
void ixgbe_reset(struct ixgbe_adapter *adapter);
837839
void ixgbe_set_ethtool_ops(struct net_device *netdev);
838-
int ixgbe_setup_rx_resources(struct ixgbe_ring *);
840+
int ixgbe_setup_rx_resources(struct ixgbe_adapter *, struct ixgbe_ring *);
839841
int ixgbe_setup_tx_resources(struct ixgbe_ring *);
840842
void ixgbe_free_rx_resources(struct ixgbe_ring *);
841843
void ixgbe_free_tx_resources(struct ixgbe_ring *);

drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ static int ixgbe_set_ringparam(struct net_device *netdev,
11281128
sizeof(struct ixgbe_ring));
11291129

11301130
temp_ring[i].count = new_rx_count;
1131-
err = ixgbe_setup_rx_resources(&temp_ring[i]);
1131+
err = ixgbe_setup_rx_resources(adapter, &temp_ring[i]);
11321132
if (err) {
11331133
while (i) {
11341134
i--;
@@ -1761,7 +1761,7 @@ static int ixgbe_setup_desc_rings(struct ixgbe_adapter *adapter)
17611761
rx_ring->netdev = adapter->netdev;
17621762
rx_ring->reg_idx = adapter->rx_ring[0]->reg_idx;
17631763

1764-
err = ixgbe_setup_rx_resources(rx_ring);
1764+
err = ixgbe_setup_rx_resources(adapter, rx_ring);
17651765
if (err) {
17661766
ret_val = 4;
17671767
goto err_nomem;

drivers/net/ethernet/intel/ixgbe/ixgbe_main.c

Lines changed: 143 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
#include <linux/if_macvlan.h>
5050
#include <linux/if_bridge.h>
5151
#include <linux/prefetch.h>
52+
#include <linux/bpf.h>
53+
#include <linux/bpf_trace.h>
54+
#include <linux/atomic.h>
5255
#include <scsi/fc/fc_fcoe.h>
5356
#include <net/udp_tunnel.h>
5457
#include <net/pkt_cls.h>
@@ -1855,6 +1858,10 @@ static void ixgbe_dma_sync_frag(struct ixgbe_ring *rx_ring,
18551858
* @rx_desc: pointer to the EOP Rx descriptor
18561859
* @skb: pointer to current skb being fixed
18571860
*
1861+
* Check if the skb is valid in the XDP case it will be an error pointer.
1862+
* Return true in this case to abort processing and advance to next
1863+
* descriptor.
1864+
*
18581865
* Check for corrupted packet headers caused by senders on the local L2
18591866
* embedded NIC switch not setting up their Tx Descriptors right. These
18601867
* should be very rare.
@@ -1873,6 +1880,10 @@ static bool ixgbe_cleanup_headers(struct ixgbe_ring *rx_ring,
18731880
{
18741881
struct net_device *netdev = rx_ring->netdev;
18751882

1883+
/* XDP packets use error pointer so abort at this point */
1884+
if (IS_ERR(skb))
1885+
return true;
1886+
18761887
/* verify that the packet does not have any known errors */
18771888
if (unlikely(ixgbe_test_staterr(rx_desc,
18781889
IXGBE_RXDADV_ERR_FRAME_ERR_MASK) &&
@@ -2048,7 +2059,7 @@ static void ixgbe_put_rx_buffer(struct ixgbe_ring *rx_ring,
20482059
/* hand second half of page back to the ring */
20492060
ixgbe_reuse_rx_page(rx_ring, rx_buffer);
20502061
} else {
2051-
if (IXGBE_CB(skb)->dma == rx_buffer->dma) {
2062+
if (!IS_ERR(skb) && IXGBE_CB(skb)->dma == rx_buffer->dma) {
20522063
/* the page has been released from the ring */
20532064
IXGBE_CB(skb)->page_released = true;
20542065
} else {
@@ -2069,21 +2080,22 @@ static void ixgbe_put_rx_buffer(struct ixgbe_ring *rx_ring,
20692080

20702081
static struct sk_buff *ixgbe_construct_skb(struct ixgbe_ring *rx_ring,
20712082
struct ixgbe_rx_buffer *rx_buffer,
2072-
union ixgbe_adv_rx_desc *rx_desc,
2073-
unsigned int size)
2083+
struct xdp_buff *xdp,
2084+
union ixgbe_adv_rx_desc *rx_desc)
20742085
{
2075-
void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
2086+
unsigned int size = xdp->data_end - xdp->data;
20762087
#if (PAGE_SIZE < 8192)
20772088
unsigned int truesize = ixgbe_rx_pg_size(rx_ring) / 2;
20782089
#else
2079-
unsigned int truesize = SKB_DATA_ALIGN(size);
2090+
unsigned int truesize = SKB_DATA_ALIGN(xdp->data_end -
2091+
xdp->data_hard_start);
20802092
#endif
20812093
struct sk_buff *skb;
20822094

20832095
/* prefetch first cache line of first page */
2084-
prefetch(va);
2096+
prefetch(xdp->data);
20852097
#if L1_CACHE_BYTES < 128
2086-
prefetch(va + L1_CACHE_BYTES);
2098+
prefetch(xdp->data + L1_CACHE_BYTES);
20872099
#endif
20882100

20892101
/* allocate a skb to store the frags */
@@ -2096,15 +2108,16 @@ static struct sk_buff *ixgbe_construct_skb(struct ixgbe_ring *rx_ring,
20962108
IXGBE_CB(skb)->dma = rx_buffer->dma;
20972109

20982110
skb_add_rx_frag(skb, 0, rx_buffer->page,
2099-
rx_buffer->page_offset,
2111+
xdp->data - page_address(rx_buffer->page),
21002112
size, truesize);
21012113
#if (PAGE_SIZE < 8192)
21022114
rx_buffer->page_offset ^= truesize;
21032115
#else
21042116
rx_buffer->page_offset += truesize;
21052117
#endif
21062118
} else {
2107-
memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
2119+
memcpy(__skb_put(skb, size),
2120+
xdp->data, ALIGN(size, sizeof(long)));
21082121
rx_buffer->pagecnt_bias++;
21092122
}
21102123

@@ -2113,32 +2126,32 @@ static struct sk_buff *ixgbe_construct_skb(struct ixgbe_ring *rx_ring,
21132126

21142127
static struct sk_buff *ixgbe_build_skb(struct ixgbe_ring *rx_ring,
21152128
struct ixgbe_rx_buffer *rx_buffer,
2116-
union ixgbe_adv_rx_desc *rx_desc,
2117-
unsigned int size)
2129+
struct xdp_buff *xdp,
2130+
union ixgbe_adv_rx_desc *rx_desc)
21182131
{
2119-
void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
21202132
#if (PAGE_SIZE < 8192)
21212133
unsigned int truesize = ixgbe_rx_pg_size(rx_ring) / 2;
21222134
#else
21232135
unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
2124-
SKB_DATA_ALIGN(IXGBE_SKB_PAD + size);
2136+
SKB_DATA_ALIGN(xdp->data_end -
2137+
xdp->data_hard_start);
21252138
#endif
21262139
struct sk_buff *skb;
21272140

21282141
/* prefetch first cache line of first page */
2129-
prefetch(va);
2142+
prefetch(xdp->data);
21302143
#if L1_CACHE_BYTES < 128
2131-
prefetch(va + L1_CACHE_BYTES);
2144+
prefetch(xdp->data + L1_CACHE_BYTES);
21322145
#endif
21332146

2134-
/* build an skb around the page buffer */
2135-
skb = build_skb(va - IXGBE_SKB_PAD, truesize);
2147+
/* build an skb to around the page buffer */
2148+
skb = build_skb(xdp->data_hard_start, truesize);
21362149
if (unlikely(!skb))
21372150
return NULL;
21382151

21392152
/* update pointers within the skb to store the data */
2140-
skb_reserve(skb, IXGBE_SKB_PAD);
2141-
__skb_put(skb, size);
2153+
skb_reserve(skb, xdp->data - xdp->data_hard_start);
2154+
__skb_put(skb, xdp->data_end - xdp->data);
21422155

21432156
/* record DMA address if this is the start of a chain of buffers */
21442157
if (!ixgbe_test_staterr(rx_desc, IXGBE_RXD_STAT_EOP))
@@ -2154,6 +2167,41 @@ static struct sk_buff *ixgbe_build_skb(struct ixgbe_ring *rx_ring,
21542167
return skb;
21552168
}
21562169

2170+
#define IXGBE_XDP_PASS 0
2171+
#define IXGBE_XDP_CONSUMED 1
2172+
2173+
static struct sk_buff *ixgbe_run_xdp(struct ixgbe_ring *rx_ring,
2174+
struct xdp_buff *xdp)
2175+
{
2176+
int result = IXGBE_XDP_PASS;
2177+
struct bpf_prog *xdp_prog;
2178+
u32 act;
2179+
2180+
rcu_read_lock();
2181+
xdp_prog = READ_ONCE(rx_ring->xdp_prog);
2182+
2183+
if (!xdp_prog)
2184+
goto xdp_out;
2185+
2186+
act = bpf_prog_run_xdp(xdp_prog, xdp);
2187+
switch (act) {
2188+
case XDP_PASS:
2189+
break;
2190+
default:
2191+
bpf_warn_invalid_xdp_action(act);
2192+
case XDP_TX:
2193+
case XDP_ABORTED:
2194+
trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
2195+
/* fallthrough -- handle aborts by dropping packet */
2196+
case XDP_DROP:
2197+
result = IXGBE_XDP_CONSUMED;
2198+
break;
2199+
}
2200+
xdp_out:
2201+
rcu_read_unlock();
2202+
return ERR_PTR(-result);
2203+
}
2204+
21572205
/**
21582206
* ixgbe_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
21592207
* @q_vector: structure containing interrupt and ring information
@@ -2183,6 +2231,7 @@ static int ixgbe_clean_rx_irq(struct ixgbe_q_vector *q_vector,
21832231
union ixgbe_adv_rx_desc *rx_desc;
21842232
struct ixgbe_rx_buffer *rx_buffer;
21852233
struct sk_buff *skb;
2234+
struct xdp_buff xdp;
21862235
unsigned int size;
21872236

21882237
/* return some buffers to hardware, one at a time is too slow */
@@ -2205,14 +2254,29 @@ static int ixgbe_clean_rx_irq(struct ixgbe_q_vector *q_vector,
22052254
rx_buffer = ixgbe_get_rx_buffer(rx_ring, rx_desc, &skb, size);
22062255

22072256
/* retrieve a buffer from the ring */
2208-
if (skb)
2257+
if (!skb) {
2258+
xdp.data = page_address(rx_buffer->page) +
2259+
rx_buffer->page_offset;
2260+
xdp.data_hard_start = xdp.data -
2261+
ixgbe_rx_offset(rx_ring);
2262+
xdp.data_end = xdp.data + size;
2263+
2264+
skb = ixgbe_run_xdp(rx_ring, &xdp);
2265+
}
2266+
2267+
if (IS_ERR(skb)) {
2268+
total_rx_packets++;
2269+
total_rx_bytes += size;
2270+
rx_buffer->pagecnt_bias++;
2271+
} else if (skb) {
22092272
ixgbe_add_rx_frag(rx_ring, rx_buffer, skb, size);
2210-
else if (ring_uses_build_skb(rx_ring))
2273+
} else if (ring_uses_build_skb(rx_ring)) {
22112274
skb = ixgbe_build_skb(rx_ring, rx_buffer,
2212-
rx_desc, size);
2213-
else
2275+
&xdp, rx_desc);
2276+
} else {
22142277
skb = ixgbe_construct_skb(rx_ring, rx_buffer,
2215-
rx_desc, size);
2278+
&xdp, rx_desc);
2279+
}
22162280

22172281
/* exit if we failed to retrieve a buffer */
22182282
if (!skb) {
@@ -6073,7 +6137,8 @@ static int ixgbe_setup_all_tx_resources(struct ixgbe_adapter *adapter)
60736137
*
60746138
* Returns 0 on success, negative on failure
60756139
**/
6076-
int ixgbe_setup_rx_resources(struct ixgbe_ring *rx_ring)
6140+
int ixgbe_setup_rx_resources(struct ixgbe_adapter *adapter,
6141+
struct ixgbe_ring *rx_ring)
60776142
{
60786143
struct device *dev = rx_ring->dev;
60796144
int orig_node = dev_to_node(dev);
@@ -6112,6 +6177,8 @@ int ixgbe_setup_rx_resources(struct ixgbe_ring *rx_ring)
61126177
rx_ring->next_to_clean = 0;
61136178
rx_ring->next_to_use = 0;
61146179

6180+
rx_ring->xdp_prog = adapter->xdp_prog;
6181+
61156182
return 0;
61166183
err:
61176184
vfree(rx_ring->rx_buffer_info);
@@ -6135,7 +6202,7 @@ static int ixgbe_setup_all_rx_resources(struct ixgbe_adapter *adapter)
61356202
int i, err = 0;
61366203

61376204
for (i = 0; i < adapter->num_rx_queues; i++) {
6138-
err = ixgbe_setup_rx_resources(adapter->rx_ring[i]);
6205+
err = ixgbe_setup_rx_resources(adapter, adapter->rx_ring[i]);
61396206
if (!err)
61406207
continue;
61416208

@@ -6203,6 +6270,7 @@ void ixgbe_free_rx_resources(struct ixgbe_ring *rx_ring)
62036270
{
62046271
ixgbe_clean_rx_ring(rx_ring);
62056272

6273+
rx_ring->xdp_prog = NULL;
62066274
vfree(rx_ring->rx_buffer_info);
62076275
rx_ring->rx_buffer_info = NULL;
62086276

@@ -9468,6 +9536,54 @@ ixgbe_features_check(struct sk_buff *skb, struct net_device *dev,
94689536
return features;
94699537
}
94709538

9539+
static int ixgbe_xdp_setup(struct net_device *dev, struct bpf_prog *prog)
9540+
{
9541+
int i, frame_size = dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
9542+
struct ixgbe_adapter *adapter = netdev_priv(dev);
9543+
struct bpf_prog *old_prog;
9544+
9545+
if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
9546+
return -EINVAL;
9547+
9548+
if (adapter->flags & IXGBE_FLAG_DCB_ENABLED)
9549+
return -EINVAL;
9550+
9551+
/* verify ixgbe ring attributes are sufficient for XDP */
9552+
for (i = 0; i < adapter->num_rx_queues; i++) {
9553+
struct ixgbe_ring *ring = adapter->rx_ring[i];
9554+
9555+
if (ring_is_rsc_enabled(ring))
9556+
return -EINVAL;
9557+
9558+
if (frame_size > ixgbe_rx_bufsz(ring))
9559+
return -EINVAL;
9560+
}
9561+
9562+
old_prog = xchg(&adapter->xdp_prog, prog);
9563+
for (i = 0; i < adapter->num_rx_queues; i++)
9564+
xchg(&adapter->rx_ring[i]->xdp_prog, adapter->xdp_prog);
9565+
9566+
if (old_prog)
9567+
bpf_prog_put(old_prog);
9568+
9569+
return 0;
9570+
}
9571+
9572+
static int ixgbe_xdp(struct net_device *dev, struct netdev_xdp *xdp)
9573+
{
9574+
struct ixgbe_adapter *adapter = netdev_priv(dev);
9575+
9576+
switch (xdp->command) {
9577+
case XDP_SETUP_PROG:
9578+
return ixgbe_xdp_setup(dev, xdp->prog);
9579+
case XDP_QUERY_PROG:
9580+
xdp->prog_attached = !!(adapter->xdp_prog);
9581+
return 0;
9582+
default:
9583+
return -EINVAL;
9584+
}
9585+
}
9586+
94719587
static const struct net_device_ops ixgbe_netdev_ops = {
94729588
.ndo_open = ixgbe_open,
94739589
.ndo_stop = ixgbe_close,
@@ -9513,6 +9629,7 @@ static const struct net_device_ops ixgbe_netdev_ops = {
95139629
.ndo_udp_tunnel_add = ixgbe_add_udp_tunnel_port,
95149630
.ndo_udp_tunnel_del = ixgbe_del_udp_tunnel_port,
95159631
.ndo_features_check = ixgbe_features_check,
9632+
.ndo_xdp = ixgbe_xdp,
95169633
};
95179634

95189635
/**

0 commit comments

Comments
 (0)