1010 * General Public License for more details.
1111 */
1212#define KBUILD_MODNAME "foo"
13- #include <uapi/linux/bpf.h>
14- #include <linux/in.h>
15- #include <linux/if_ether.h>
16- #include <linux/if_packet.h>
17- #include <linux/if_vlan.h>
18- #include <linux/ip.h>
19- #include <linux/ipv6.h>
20- #include <bpf/bpf_helpers.h>
13+
14+ #include "vmlinux.h"
15+ #include "xdp_sample.bpf.h"
16+ #include "xdp_sample_shared.h"
2117
2218/* The 2nd xdp prog on egress does not support skb mode, so we define two
2319 * maps, tx_port_general and tx_port_native.
@@ -26,114 +22,71 @@ struct {
2622 __uint (type , BPF_MAP_TYPE_DEVMAP );
2723 __uint (key_size , sizeof (int ));
2824 __uint (value_size , sizeof (int ));
29- __uint (max_entries , 100 );
25+ __uint (max_entries , 1 );
3026} tx_port_general SEC (".maps" );
3127
3228struct {
3329 __uint (type , BPF_MAP_TYPE_DEVMAP );
3430 __uint (key_size , sizeof (int ));
3531 __uint (value_size , sizeof (struct bpf_devmap_val ));
36- __uint (max_entries , 100 );
37- } tx_port_native SEC (".maps" );
38-
39- /* Count RX packets, as XDP bpf_prog doesn't get direct TX-success
40- * feedback. Redirect TX errors can be caught via a tracepoint.
41- */
42- struct {
43- __uint (type , BPF_MAP_TYPE_PERCPU_ARRAY );
44- __type (key , u32 );
45- __type (value , long);
46- __uint (max_entries , 1 );
47- } rxcnt SEC (".maps" );
48-
49- /* map to store egress interface mac address */
50- struct {
51- __uint (type , BPF_MAP_TYPE_ARRAY );
52- __type (key , u32 );
53- __type (value , __be64 );
5432 __uint (max_entries , 1 );
55- } tx_mac SEC (".maps" );
56-
57- static void swap_src_dst_mac (void * data )
58- {
59- unsigned short * p = data ;
60- unsigned short dst [3 ];
33+ } tx_port_native SEC (".maps" );
6134
62- dst [0 ] = p [0 ];
63- dst [1 ] = p [1 ];
64- dst [2 ] = p [2 ];
65- p [0 ] = p [3 ];
66- p [1 ] = p [4 ];
67- p [2 ] = p [5 ];
68- p [3 ] = dst [0 ];
69- p [4 ] = dst [1 ];
70- p [5 ] = dst [2 ];
71- }
35+ /* store egress interface mac address */
36+ const volatile char tx_mac_addr [ETH_ALEN ];
7237
7338static __always_inline int xdp_redirect_map (struct xdp_md * ctx , void * redirect_map )
7439{
7540 void * data_end = (void * )(long )ctx -> data_end ;
7641 void * data = (void * )(long )ctx -> data ;
42+ u32 key = bpf_get_smp_processor_id ();
7743 struct ethhdr * eth = data ;
78- int rc = XDP_DROP ;
79- long * value ;
80- u32 key = 0 ;
44+ struct datarec * rec ;
8145 u64 nh_off ;
82- int vport ;
8346
8447 nh_off = sizeof (* eth );
8548 if (data + nh_off > data_end )
86- return rc ;
87-
88- /* constant virtual port */
89- vport = 0 ;
90-
91- /* count packet in global counter */
92- value = bpf_map_lookup_elem (& rxcnt , & key );
93- if (value )
94- * value += 1 ;
49+ return XDP_DROP ;
9550
51+ rec = bpf_map_lookup_elem (& rx_cnt , & key );
52+ if (!rec )
53+ return XDP_PASS ;
54+ NO_TEAR_INC (rec -> processed );
9655 swap_src_dst_mac (data );
97-
98- /* send packet out physical port */
99- return bpf_redirect_map (redirect_map , vport , 0 );
56+ return bpf_redirect_map (redirect_map , 0 , 0 );
10057}
10158
102- SEC ("xdp_redirect_general " )
59+ SEC ("xdp " )
10360int xdp_redirect_map_general (struct xdp_md * ctx )
10461{
10562 return xdp_redirect_map (ctx , & tx_port_general );
10663}
10764
108- SEC ("xdp_redirect_native " )
65+ SEC ("xdp " )
10966int xdp_redirect_map_native (struct xdp_md * ctx )
11067{
11168 return xdp_redirect_map (ctx , & tx_port_native );
11269}
11370
114- SEC ("xdp_devmap/map_prog " )
71+ SEC ("xdp_devmap/egress " )
11572int xdp_redirect_map_egress (struct xdp_md * ctx )
11673{
11774 void * data_end = (void * )(long )ctx -> data_end ;
11875 void * data = (void * )(long )ctx -> data ;
11976 struct ethhdr * eth = data ;
120- __be64 * mac ;
121- u32 key = 0 ;
12277 u64 nh_off ;
12378
12479 nh_off = sizeof (* eth );
12580 if (data + nh_off > data_end )
12681 return XDP_DROP ;
12782
128- mac = bpf_map_lookup_elem (& tx_mac , & key );
129- if (mac )
130- __builtin_memcpy (eth -> h_source , mac , ETH_ALEN );
83+ __builtin_memcpy (eth -> h_source , (const char * )tx_mac_addr , ETH_ALEN );
13184
13285 return XDP_PASS ;
13386}
13487
13588/* Redirect require an XDP bpf_prog loaded on the TX device */
136- SEC ("xdp_redirect_dummy " )
89+ SEC ("xdp " )
13790int xdp_redirect_dummy_prog (struct xdp_md * ctx )
13891{
13992 return XDP_PASS ;
0 commit comments