26
26
27
27
#include <linux/prefetch.h>
28
28
#include <net/busy_poll.h>
29
+ #include <linux/bpf_trace.h>
29
30
#include "i40e.h"
30
31
#include "i40e_trace.h"
31
32
#include "i40e_prototype.h"
@@ -1195,6 +1196,7 @@ void i40e_clean_rx_ring(struct i40e_ring *rx_ring)
1195
1196
void i40e_free_rx_resources (struct i40e_ring * rx_ring )
1196
1197
{
1197
1198
i40e_clean_rx_ring (rx_ring );
1199
+ rx_ring -> xdp_prog = NULL ;
1198
1200
kfree (rx_ring -> rx_bi );
1199
1201
rx_ring -> rx_bi = NULL ;
1200
1202
@@ -1241,6 +1243,8 @@ int i40e_setup_rx_descriptors(struct i40e_ring *rx_ring)
1241
1243
rx_ring -> next_to_clean = 0 ;
1242
1244
rx_ring -> next_to_use = 0 ;
1243
1245
1246
+ rx_ring -> xdp_prog = rx_ring -> vsi -> xdp_prog ;
1247
+
1244
1248
return 0 ;
1245
1249
err :
1246
1250
kfree (rx_ring -> rx_bi );
@@ -1593,6 +1597,7 @@ void i40e_process_skb_fields(struct i40e_ring *rx_ring,
1593
1597
* i40e_cleanup_headers - Correct empty headers
1594
1598
* @rx_ring: rx descriptor ring packet is being transacted on
1595
1599
* @skb: pointer to current skb being fixed
1600
+ * @rx_desc: pointer to the EOP Rx descriptor
1596
1601
*
1597
1602
* Also address the case where we are pulling data in on pages only
1598
1603
* and as such no data is present in the skb header.
@@ -1602,8 +1607,25 @@ void i40e_process_skb_fields(struct i40e_ring *rx_ring,
1602
1607
*
1603
1608
* Returns true if an error was encountered and skb was freed.
1604
1609
**/
1605
- static bool i40e_cleanup_headers (struct i40e_ring * rx_ring , struct sk_buff * skb )
1610
+ static bool i40e_cleanup_headers (struct i40e_ring * rx_ring , struct sk_buff * skb ,
1611
+ union i40e_rx_desc * rx_desc )
1612
+
1606
1613
{
1614
+ /* XDP packets use error pointer so abort at this point */
1615
+ if (IS_ERR (skb ))
1616
+ return true;
1617
+
1618
+ /* ERR_MASK will only have valid bits if EOP set, and
1619
+ * what we are doing here is actually checking
1620
+ * I40E_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in
1621
+ * the error field
1622
+ */
1623
+ if (unlikely (i40e_test_staterr (rx_desc ,
1624
+ BIT (I40E_RXD_QW1_ERROR_SHIFT )))) {
1625
+ dev_kfree_skb_any (skb );
1626
+ return true;
1627
+ }
1628
+
1607
1629
/* if eth_skb_pad returns an error the skb was freed */
1608
1630
if (eth_skb_pad (skb ))
1609
1631
return true;
@@ -1776,17 +1798,17 @@ static struct i40e_rx_buffer *i40e_get_rx_buffer(struct i40e_ring *rx_ring,
1776
1798
* i40e_construct_skb - Allocate skb and populate it
1777
1799
* @rx_ring: rx descriptor ring to transact packets on
1778
1800
* @rx_buffer: rx buffer to pull data from
1779
- * @size: size of buffer to add to skb
1801
+ * @xdp: xdp_buff pointing to the data
1780
1802
*
1781
1803
* This function allocates an skb. It then populates it with the page
1782
1804
* data from the current receive descriptor, taking care to set up the
1783
1805
* skb correctly.
1784
1806
*/
1785
1807
static struct sk_buff * i40e_construct_skb (struct i40e_ring * rx_ring ,
1786
1808
struct i40e_rx_buffer * rx_buffer ,
1787
- unsigned int size )
1809
+ struct xdp_buff * xdp )
1788
1810
{
1789
- void * va = page_address ( rx_buffer -> page ) + rx_buffer -> page_offset ;
1811
+ unsigned int size = xdp -> data_end - xdp -> data ;
1790
1812
#if (PAGE_SIZE < 8192 )
1791
1813
unsigned int truesize = i40e_rx_pg_size (rx_ring ) / 2 ;
1792
1814
#else
@@ -1796,9 +1818,9 @@ static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
1796
1818
struct sk_buff * skb ;
1797
1819
1798
1820
/* prefetch first cache line of first page */
1799
- prefetch (va );
1821
+ prefetch (xdp -> data );
1800
1822
#if L1_CACHE_BYTES < 128
1801
- prefetch (va + L1_CACHE_BYTES );
1823
+ prefetch (xdp -> data + L1_CACHE_BYTES );
1802
1824
#endif
1803
1825
1804
1826
/* allocate a skb to store the frags */
@@ -1811,10 +1833,11 @@ static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
1811
1833
/* Determine available headroom for copy */
1812
1834
headlen = size ;
1813
1835
if (headlen > I40E_RX_HDR_SIZE )
1814
- headlen = eth_get_headlen (va , I40E_RX_HDR_SIZE );
1836
+ headlen = eth_get_headlen (xdp -> data , I40E_RX_HDR_SIZE );
1815
1837
1816
1838
/* align pull length to size of long to optimize memcpy performance */
1817
- memcpy (__skb_put (skb , headlen ), va , ALIGN (headlen , sizeof (long )));
1839
+ memcpy (__skb_put (skb , headlen ), xdp -> data ,
1840
+ ALIGN (headlen , sizeof (long )));
1818
1841
1819
1842
/* update all of the pointers */
1820
1843
size -= headlen ;
@@ -1841,16 +1864,16 @@ static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
1841
1864
* i40e_build_skb - Build skb around an existing buffer
1842
1865
* @rx_ring: Rx descriptor ring to transact packets on
1843
1866
* @rx_buffer: Rx buffer to pull data from
1844
- * @size: size of buffer to add to skb
1867
+ * @xdp: xdp_buff pointing to the data
1845
1868
*
1846
1869
* This function builds an skb around an existing Rx buffer, taking care
1847
1870
* to set up the skb correctly and avoid any memcpy overhead.
1848
1871
*/
1849
1872
static struct sk_buff * i40e_build_skb (struct i40e_ring * rx_ring ,
1850
1873
struct i40e_rx_buffer * rx_buffer ,
1851
- unsigned int size )
1874
+ struct xdp_buff * xdp )
1852
1875
{
1853
- void * va = page_address ( rx_buffer -> page ) + rx_buffer -> page_offset ;
1876
+ unsigned int size = xdp -> data_end - xdp -> data ;
1854
1877
#if (PAGE_SIZE < 8192 )
1855
1878
unsigned int truesize = i40e_rx_pg_size (rx_ring ) / 2 ;
1856
1879
#else
@@ -1860,12 +1883,12 @@ static struct sk_buff *i40e_build_skb(struct i40e_ring *rx_ring,
1860
1883
struct sk_buff * skb ;
1861
1884
1862
1885
/* prefetch first cache line of first page */
1863
- prefetch (va );
1886
+ prefetch (xdp -> data );
1864
1887
#if L1_CACHE_BYTES < 128
1865
- prefetch (va + L1_CACHE_BYTES );
1888
+ prefetch (xdp -> data + L1_CACHE_BYTES );
1866
1889
#endif
1867
1890
/* build an skb around the page buffer */
1868
- skb = build_skb (va - I40E_SKB_PAD , truesize );
1891
+ skb = build_skb (xdp -> data_hard_start , truesize );
1869
1892
if (unlikely (!skb ))
1870
1893
return NULL ;
1871
1894
@@ -1944,6 +1967,46 @@ static bool i40e_is_non_eop(struct i40e_ring *rx_ring,
1944
1967
return true;
1945
1968
}
1946
1969
1970
+ #define I40E_XDP_PASS 0
1971
+ #define I40E_XDP_CONSUMED 1
1972
+
1973
+ /**
1974
+ * i40e_run_xdp - run an XDP program
1975
+ * @rx_ring: Rx ring being processed
1976
+ * @xdp: XDP buffer containing the frame
1977
+ **/
1978
+ static struct sk_buff * i40e_run_xdp (struct i40e_ring * rx_ring ,
1979
+ struct xdp_buff * xdp )
1980
+ {
1981
+ int result = I40E_XDP_PASS ;
1982
+ struct bpf_prog * xdp_prog ;
1983
+ u32 act ;
1984
+
1985
+ rcu_read_lock ();
1986
+ xdp_prog = READ_ONCE (rx_ring -> xdp_prog );
1987
+
1988
+ if (!xdp_prog )
1989
+ goto xdp_out ;
1990
+
1991
+ act = bpf_prog_run_xdp (xdp_prog , xdp );
1992
+ switch (act ) {
1993
+ case XDP_PASS :
1994
+ break ;
1995
+ default :
1996
+ bpf_warn_invalid_xdp_action (act );
1997
+ case XDP_TX :
1998
+ case XDP_ABORTED :
1999
+ trace_xdp_exception (rx_ring -> netdev , xdp_prog , act );
2000
+ /* fallthrough -- handle aborts by dropping packet */
2001
+ case XDP_DROP :
2002
+ result = I40E_XDP_CONSUMED ;
2003
+ break ;
2004
+ }
2005
+ xdp_out :
2006
+ rcu_read_unlock ();
2007
+ return ERR_PTR (- result );
2008
+ }
2009
+
1947
2010
/**
1948
2011
* i40e_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
1949
2012
* @rx_ring: rx descriptor ring to transact packets on
@@ -1966,6 +2029,7 @@ static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
1966
2029
while (likely (total_rx_packets < budget )) {
1967
2030
struct i40e_rx_buffer * rx_buffer ;
1968
2031
union i40e_rx_desc * rx_desc ;
2032
+ struct xdp_buff xdp ;
1969
2033
unsigned int size ;
1970
2034
u16 vlan_tag ;
1971
2035
u8 rx_ptype ;
@@ -2006,12 +2070,27 @@ static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
2006
2070
rx_buffer = i40e_get_rx_buffer (rx_ring , size );
2007
2071
2008
2072
/* retrieve a buffer from the ring */
2009
- if (skb )
2073
+ if (!skb ) {
2074
+ xdp .data = page_address (rx_buffer -> page ) +
2075
+ rx_buffer -> page_offset ;
2076
+ xdp .data_hard_start = xdp .data -
2077
+ i40e_rx_offset (rx_ring );
2078
+ xdp .data_end = xdp .data + size ;
2079
+
2080
+ skb = i40e_run_xdp (rx_ring , & xdp );
2081
+ }
2082
+
2083
+ if (IS_ERR (skb )) {
2084
+ total_rx_bytes += size ;
2085
+ total_rx_packets ++ ;
2086
+ rx_buffer -> pagecnt_bias ++ ;
2087
+ } else if (skb ) {
2010
2088
i40e_add_rx_frag (rx_ring , rx_buffer , skb , size );
2011
- else if (ring_uses_build_skb (rx_ring ))
2012
- skb = i40e_build_skb (rx_ring , rx_buffer , size );
2013
- else
2014
- skb = i40e_construct_skb (rx_ring , rx_buffer , size );
2089
+ } else if (ring_uses_build_skb (rx_ring )) {
2090
+ skb = i40e_build_skb (rx_ring , rx_buffer , & xdp );
2091
+ } else {
2092
+ skb = i40e_construct_skb (rx_ring , rx_buffer , & xdp );
2093
+ }
2015
2094
2016
2095
/* exit if we failed to retrieve a buffer */
2017
2096
if (!skb ) {
@@ -2026,18 +2105,7 @@ static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
2026
2105
if (i40e_is_non_eop (rx_ring , rx_desc , skb ))
2027
2106
continue ;
2028
2107
2029
- /* ERR_MASK will only have valid bits if EOP set, and
2030
- * what we are doing here is actually checking
2031
- * I40E_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in
2032
- * the error field
2033
- */
2034
- if (unlikely (i40e_test_staterr (rx_desc , BIT (I40E_RXD_QW1_ERROR_SHIFT )))) {
2035
- dev_kfree_skb_any (skb );
2036
- skb = NULL ;
2037
- continue ;
2038
- }
2039
-
2040
- if (i40e_cleanup_headers (rx_ring , skb )) {
2108
+ if (i40e_cleanup_headers (rx_ring , skb , rx_desc )) {
2041
2109
skb = NULL ;
2042
2110
continue ;
2043
2111
}
0 commit comments