10
10
#include <linux/ip.h>
11
11
#include <linux/pm_runtime.h>
12
12
#include <net/pkt_sched.h>
13
+ #include <linux/bpf_trace.h>
13
14
14
15
#include <net/ipv6.h>
15
16
16
17
#include "igc.h"
17
18
#include "igc_hw.h"
18
19
#include "igc_tsn.h"
20
+ #include "igc_xdp.h"
19
21
20
22
#define DRV_SUMMARY "Intel(R) 2.5G Ethernet Linux Driver"
21
23
22
24
#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
23
25
26
+ #define IGC_XDP_PASS 0
27
+ #define IGC_XDP_CONSUMED BIT(0)
28
+
24
29
static int debug = -1 ;
25
30
26
31
MODULE_AUTHOR (
"Intel Corporation, <[email protected] >" );
@@ -375,6 +380,8 @@ static void igc_clean_rx_ring(struct igc_ring *rx_ring)
375
380
i = 0 ;
376
381
}
377
382
383
+ clear_ring_uses_large_buffer (rx_ring );
384
+
378
385
rx_ring -> next_to_alloc = 0 ;
379
386
rx_ring -> next_to_clean = 0 ;
380
387
rx_ring -> next_to_use = 0 ;
@@ -497,6 +504,11 @@ static int igc_setup_all_rx_resources(struct igc_adapter *adapter)
497
504
return err ;
498
505
}
499
506
507
+ static bool igc_xdp_is_enabled (struct igc_adapter * adapter )
508
+ {
509
+ return !!adapter -> xdp_prog ;
510
+ }
511
+
500
512
/**
501
513
* igc_configure_rx_ring - Configure a receive ring after Reset
502
514
* @adapter: board private structure
@@ -513,6 +525,9 @@ static void igc_configure_rx_ring(struct igc_adapter *adapter,
513
525
u32 srrctl = 0 , rxdctl = 0 ;
514
526
u64 rdba = ring -> dma ;
515
527
528
+ if (igc_xdp_is_enabled (adapter ))
529
+ set_ring_uses_large_buffer (ring );
530
+
516
531
/* disable the queue */
517
532
wr32 (IGC_RXDCTL (reg_idx ), 0 );
518
533
@@ -1581,12 +1596,12 @@ static struct sk_buff *igc_build_skb(struct igc_ring *rx_ring,
1581
1596
1582
1597
static struct sk_buff * igc_construct_skb (struct igc_ring * rx_ring ,
1583
1598
struct igc_rx_buffer * rx_buffer ,
1584
- unsigned int size , int pkt_offset ,
1599
+ struct xdp_buff * xdp ,
1585
1600
ktime_t timestamp )
1586
1601
{
1587
- void * va = page_address (rx_buffer -> page ) + rx_buffer -> page_offset +
1588
- pkt_offset ;
1602
+ unsigned int size = xdp -> data_end - xdp -> data ;
1589
1603
unsigned int truesize = igc_get_rx_frame_truesize (rx_ring , size );
1604
+ void * va = xdp -> data ;
1590
1605
unsigned int headlen ;
1591
1606
struct sk_buff * skb ;
1592
1607
@@ -1730,6 +1745,10 @@ static bool igc_cleanup_headers(struct igc_ring *rx_ring,
1730
1745
union igc_adv_rx_desc * rx_desc ,
1731
1746
struct sk_buff * skb )
1732
1747
{
1748
+ /* XDP packets use error pointer so abort at this point */
1749
+ if (IS_ERR (skb ))
1750
+ return true;
1751
+
1733
1752
if (unlikely (igc_test_staterr (rx_desc , IGC_RXDEXT_STATERR_RXE ))) {
1734
1753
struct net_device * netdev = rx_ring -> netdev ;
1735
1754
@@ -1769,7 +1788,14 @@ static void igc_put_rx_buffer(struct igc_ring *rx_ring,
1769
1788
1770
1789
static inline unsigned int igc_rx_offset (struct igc_ring * rx_ring )
1771
1790
{
1772
- return ring_uses_build_skb (rx_ring ) ? IGC_SKB_PAD : 0 ;
1791
+ struct igc_adapter * adapter = rx_ring -> q_vector -> adapter ;
1792
+
1793
+ if (ring_uses_build_skb (rx_ring ))
1794
+ return IGC_SKB_PAD ;
1795
+ if (igc_xdp_is_enabled (adapter ))
1796
+ return XDP_PACKET_HEADROOM ;
1797
+
1798
+ return 0 ;
1773
1799
}
1774
1800
1775
1801
static bool igc_alloc_mapped_page (struct igc_ring * rx_ring ,
@@ -1883,6 +1909,42 @@ static void igc_alloc_rx_buffers(struct igc_ring *rx_ring, u16 cleaned_count)
1883
1909
}
1884
1910
}
1885
1911
1912
+ static struct sk_buff * igc_xdp_run_prog (struct igc_adapter * adapter ,
1913
+ struct xdp_buff * xdp )
1914
+ {
1915
+ struct bpf_prog * prog ;
1916
+ int res ;
1917
+ u32 act ;
1918
+
1919
+ rcu_read_lock ();
1920
+
1921
+ prog = READ_ONCE (adapter -> xdp_prog );
1922
+ if (!prog ) {
1923
+ res = IGC_XDP_PASS ;
1924
+ goto unlock ;
1925
+ }
1926
+
1927
+ act = bpf_prog_run_xdp (prog , xdp );
1928
+ switch (act ) {
1929
+ case XDP_PASS :
1930
+ res = IGC_XDP_PASS ;
1931
+ break ;
1932
+ default :
1933
+ bpf_warn_invalid_xdp_action (act );
1934
+ fallthrough ;
1935
+ case XDP_ABORTED :
1936
+ trace_xdp_exception (adapter -> netdev , prog , act );
1937
+ fallthrough ;
1938
+ case XDP_DROP :
1939
+ res = IGC_XDP_CONSUMED ;
1940
+ break ;
1941
+ }
1942
+
1943
+ unlock :
1944
+ rcu_read_unlock ();
1945
+ return ERR_PTR (- res );
1946
+ }
1947
+
1886
1948
static int igc_clean_rx_irq (struct igc_q_vector * q_vector , const int budget )
1887
1949
{
1888
1950
unsigned int total_bytes = 0 , total_packets = 0 ;
@@ -1894,8 +1956,10 @@ static int igc_clean_rx_irq(struct igc_q_vector *q_vector, const int budget)
1894
1956
union igc_adv_rx_desc * rx_desc ;
1895
1957
struct igc_rx_buffer * rx_buffer ;
1896
1958
ktime_t timestamp = 0 ;
1959
+ struct xdp_buff xdp ;
1897
1960
int pkt_offset = 0 ;
1898
1961
unsigned int size ;
1962
+ void * pktbuf ;
1899
1963
1900
1964
/* return some buffers to hardware, one at a time is too slow */
1901
1965
if (cleaned_count >= IGC_RX_BUFFER_WRITE ) {
@@ -1916,24 +1980,38 @@ static int igc_clean_rx_irq(struct igc_q_vector *q_vector, const int budget)
1916
1980
1917
1981
rx_buffer = igc_get_rx_buffer (rx_ring , size );
1918
1982
1919
- if (igc_test_staterr (rx_desc , IGC_RXDADV_STAT_TSIP )) {
1920
- void * pktbuf = page_address (rx_buffer -> page ) +
1921
- rx_buffer -> page_offset ;
1983
+ pktbuf = page_address (rx_buffer -> page ) + rx_buffer -> page_offset ;
1922
1984
1985
+ if (igc_test_staterr (rx_desc , IGC_RXDADV_STAT_TSIP )) {
1923
1986
timestamp = igc_ptp_rx_pktstamp (q_vector -> adapter ,
1924
1987
pktbuf );
1925
1988
pkt_offset = IGC_TS_HDR_LEN ;
1926
1989
size -= IGC_TS_HDR_LEN ;
1927
1990
}
1928
1991
1929
- /* retrieve a buffer from the ring */
1930
- if (skb )
1992
+ if (!skb ) {
1993
+ struct igc_adapter * adapter = q_vector -> adapter ;
1994
+
1995
+ xdp .data = pktbuf + pkt_offset ;
1996
+ xdp .data_end = xdp .data + size ;
1997
+ xdp .data_hard_start = pktbuf - igc_rx_offset (rx_ring );
1998
+ xdp_set_data_meta_invalid (& xdp );
1999
+ xdp .frame_sz = igc_get_rx_frame_truesize (rx_ring , size );
2000
+
2001
+ skb = igc_xdp_run_prog (adapter , & xdp );
2002
+ }
2003
+
2004
+ if (IS_ERR (skb )) {
2005
+ rx_buffer -> pagecnt_bias ++ ;
2006
+ total_packets ++ ;
2007
+ total_bytes += size ;
2008
+ } else if (skb )
1931
2009
igc_add_rx_frag (rx_ring , rx_buffer , skb , size );
1932
2010
else if (ring_uses_build_skb (rx_ring ))
1933
2011
skb = igc_build_skb (rx_ring , rx_buffer , rx_desc , size );
1934
2012
else
1935
- skb = igc_construct_skb (rx_ring , rx_buffer , size ,
1936
- pkt_offset , timestamp );
2013
+ skb = igc_construct_skb (rx_ring , rx_buffer , & xdp ,
2014
+ timestamp );
1937
2015
1938
2016
/* exit if we failed to retrieve a buffer */
1939
2017
if (!skb ) {
@@ -3874,6 +3952,11 @@ static int igc_change_mtu(struct net_device *netdev, int new_mtu)
3874
3952
int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN ;
3875
3953
struct igc_adapter * adapter = netdev_priv (netdev );
3876
3954
3955
+ if (igc_xdp_is_enabled (adapter ) && new_mtu > ETH_DATA_LEN ) {
3956
+ netdev_dbg (netdev , "Jumbo frames not supported with XDP" );
3957
+ return - EINVAL ;
3958
+ }
3959
+
3877
3960
/* adjust max frame to be at least the size of a standard frame */
3878
3961
if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN ))
3879
3962
max_frame = ETH_FRAME_LEN + ETH_FCS_LEN ;
@@ -4860,6 +4943,18 @@ static int igc_setup_tc(struct net_device *dev, enum tc_setup_type type,
4860
4943
}
4861
4944
}
4862
4945
4946
+ static int igc_bpf (struct net_device * dev , struct netdev_bpf * bpf )
4947
+ {
4948
+ struct igc_adapter * adapter = netdev_priv (dev );
4949
+
4950
+ switch (bpf -> command ) {
4951
+ case XDP_SETUP_PROG :
4952
+ return igc_xdp_set_prog (adapter , bpf -> prog , bpf -> extack );
4953
+ default :
4954
+ return - EOPNOTSUPP ;
4955
+ }
4956
+ }
4957
+
4863
4958
static const struct net_device_ops igc_netdev_ops = {
4864
4959
.ndo_open = igc_open ,
4865
4960
.ndo_stop = igc_close ,
@@ -4873,6 +4968,7 @@ static const struct net_device_ops igc_netdev_ops = {
4873
4968
.ndo_features_check = igc_features_check ,
4874
4969
.ndo_do_ioctl = igc_ioctl ,
4875
4970
.ndo_setup_tc = igc_setup_tc ,
4971
+ .ndo_bpf = igc_bpf ,
4876
4972
};
4877
4973
4878
4974
/* PCIe configuration access */
0 commit comments