Skip to content

Commit a29b3ca

Browse files
kkdwvdAlexei Starovoitov
authored andcommitted
samples: bpf: Convert xdp_redirect_map_multi_kern.o to XDP samples helper
One of the notable changes is using a BPF_MAP_TYPE_HASH instead of array map to store mac addresses of devices, as the resizing behavior was based on max_ifindex, which unecessarily maximized the capacity of map beyond what was needed. 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-22-memxor@gmail.com
1 parent bbe6586 commit a29b3ca

2 files changed

Lines changed: 26 additions & 31 deletions

File tree

‎samples/bpf/Makefile‎

Lines changed: 4 additions & 3 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_multi_kern.o
167166
always-y += xdp_rxq_info_kern.o
168167
always-y += xdp2skb_meta_kern.o
169168
always-y += syscall_tp_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_multi.bpf.o: $(obj)/xdp_sample.bpf.o
360360
$(obj)/xdp_redirect_map.bpf.o: $(obj)/xdp_sample.bpf.o
361361
$(obj)/xdp_redirect.bpf.o: $(obj)/xdp_sample.bpf.o
362362
$(obj)/xdp_monitor.bpf.o: $(obj)/xdp_sample.bpf.o
@@ -369,11 +369,12 @@ $(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/x
369369
-I$(srctree)/tools/lib $(CLANG_SYS_INCLUDES) \
370370
-c $(filter %.bpf.c,$^) -o $@
371371

372-
LINKED_SKELS := xdp_redirect_cpu.skel.h xdp_redirect_map.skel.h \
373-
xdp_redirect.skel.h xdp_monitor.skel.h
372+
LINKED_SKELS := xdp_redirect_cpu.skel.h xdp_redirect_map_multi.skel.h \
373+
xdp_redirect_map.skel.h xdp_redirect.skel.h xdp_monitor.skel.h
374374
clean-files += $(LINKED_SKELS)
375375

376376
xdp_redirect_cpu.skel.h-deps := xdp_redirect_cpu.bpf.o xdp_sample.bpf.o
377+
xdp_redirect_map_multi.skel.h-deps := xdp_redirect_map_multi.bpf.o xdp_sample.bpf.o
377378
xdp_redirect_map.skel.h-deps := xdp_redirect_map.bpf.o xdp_sample.bpf.o
378379
xdp_redirect.skel.h-deps := xdp_redirect.bpf.o xdp_sample.bpf.o
379380
xdp_monitor.skel.h-deps := xdp_monitor.bpf.o xdp_sample.bpf.o
Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#define KBUILD_MODNAME "foo"
3-
#include <uapi/linux/bpf.h>
4-
#include <linux/in.h>
5-
#include <linux/if_ether.h>
6-
#include <linux/ip.h>
7-
#include <linux/ipv6.h>
8-
#include <bpf/bpf_helpers.h>
3+
4+
#include "vmlinux.h"
5+
#include "xdp_sample.bpf.h"
6+
#include "xdp_sample_shared.h"
7+
8+
enum {
9+
BPF_F_BROADCAST = (1ULL << 3),
10+
BPF_F_EXCLUDE_INGRESS = (1ULL << 4),
11+
};
912

1013
struct {
1114
__uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
@@ -21,50 +24,41 @@ struct {
2124
__uint(max_entries, 32);
2225
} forward_map_native SEC(".maps");
2326

27+
/* map to store egress interfaces mac addresses */
2428
struct {
25-
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
26-
__type(key, u32);
27-
__type(value, long);
28-
__uint(max_entries, 1);
29-
} rxcnt SEC(".maps");
30-
31-
/* map to store egress interfaces mac addresses, set the
32-
* max_entries to 1 and extend it in user sapce prog.
33-
*/
34-
struct {
35-
__uint(type, BPF_MAP_TYPE_ARRAY);
29+
__uint(type, BPF_MAP_TYPE_HASH);
3630
__type(key, u32);
3731
__type(value, __be64);
38-
__uint(max_entries, 1);
32+
__uint(max_entries, 32);
3933
} mac_map SEC(".maps");
4034

4135
static int xdp_redirect_map(struct xdp_md *ctx, void *forward_map)
4236
{
43-
long *value;
44-
u32 key = 0;
37+
u32 key = bpf_get_smp_processor_id();
38+
struct datarec *rec;
4539

46-
/* count packet in global counter */
47-
value = bpf_map_lookup_elem(&rxcnt, &key);
48-
if (value)
49-
*value += 1;
40+
rec = bpf_map_lookup_elem(&rx_cnt, &key);
41+
if (!rec)
42+
return XDP_PASS;
43+
NO_TEAR_INC(rec->processed);
5044

51-
return bpf_redirect_map(forward_map, key,
45+
return bpf_redirect_map(forward_map, 0,
5246
BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
5347
}
5448

55-
SEC("xdp_redirect_general")
49+
SEC("xdp")
5650
int xdp_redirect_map_general(struct xdp_md *ctx)
5751
{
5852
return xdp_redirect_map(ctx, &forward_map_general);
5953
}
6054

61-
SEC("xdp_redirect_native")
55+
SEC("xdp")
6256
int xdp_redirect_map_native(struct xdp_md *ctx)
6357
{
6458
return xdp_redirect_map(ctx, &forward_map_native);
6559
}
6660

67-
SEC("xdp_devmap/map_prog")
61+
SEC("xdp_devmap/egress")
6862
int xdp_devmap_prog(struct xdp_md *ctx)
6963
{
7064
void *data_end = (void *)(long)ctx->data_end;

0 commit comments

Comments
 (0)