Skip to content

Commit 54af769

Browse files
kkdwvdAlexei Starovoitov
authored andcommitted
samples: bpf: Convert xdp_redirect_map_kern.o to XDP samples helper
Also update it to use consistent SEC("xdp") and SEC("xdp_devmap") naming, and use global variable instead of BPF map for copying the mac address. Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20210821002010.845777-20-memxor@gmail.com
1 parent e531a22 commit 54af769

2 files changed

Lines changed: 25 additions & 70 deletions

File tree

‎samples/bpf/Makefile‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ always-y += tcp_clamp_kern.o
163163
always-y += tcp_basertt_kern.o
164164
always-y += tcp_tos_reflect_kern.o
165165
always-y += tcp_dumpstats_kern.o
166-
always-y += xdp_redirect_map_kern.o
167166
always-y += xdp_redirect_map_multi_kern.o
168167
always-y += xdp_rxq_info_kern.o
169168
always-y += xdp2skb_meta_kern.o
@@ -357,6 +356,7 @@ endef
357356
CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
358357

359358
$(obj)/xdp_redirect_cpu.bpf.o: $(obj)/xdp_sample.bpf.o
359+
$(obj)/xdp_redirect_map.bpf.o: $(obj)/xdp_sample.bpf.o
360360
$(obj)/xdp_redirect.bpf.o: $(obj)/xdp_sample.bpf.o
361361
$(obj)/xdp_monitor.bpf.o: $(obj)/xdp_sample.bpf.o
362362

@@ -368,10 +368,12 @@ $(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/x
368368
-I$(srctree)/tools/lib $(CLANG_SYS_INCLUDES) \
369369
-c $(filter %.bpf.c,$^) -o $@
370370

371-
LINKED_SKELS := xdp_redirect_cpu.skel.h xdp_redirect.skel.h xdp_monitor.skel.h
371+
LINKED_SKELS := xdp_redirect_cpu.skel.h xdp_redirect_map.skel.h \
372+
xdp_redirect.skel.h xdp_monitor.skel.h
372373
clean-files += $(LINKED_SKELS)
373374

374375
xdp_redirect_cpu.skel.h-deps := xdp_redirect_cpu.bpf.o xdp_sample.bpf.o
376+
xdp_redirect_map.skel.h-deps := xdp_redirect_map.bpf.o xdp_sample.bpf.o
375377
xdp_redirect.skel.h-deps := xdp_redirect.bpf.o xdp_sample.bpf.o
376378
xdp_monitor.skel.h-deps := xdp_monitor.bpf.o xdp_sample.bpf.o
377379

Lines changed: 21 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@
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

3228
struct {
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

7338
static __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")
10360
int 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")
10966
int 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")
11572
int 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")
13790
int xdp_redirect_dummy_prog(struct xdp_md *ctx)
13891
{
13992
return XDP_PASS;

0 commit comments

Comments
 (0)