Skip to content

Commit 66fc4ca

Browse files
kkdwvdAlexei Starovoitov
authored andcommitted
samples: bpf: Convert xdp_redirect_kern.o to XDP samples helper
We moved swap_src_dst_mac to xdp_sample.bpf.h to be shared with other potential users, so drop it while moving code to the new file. Also, consistently use SEC("xdp") naming instead. 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-16-memxor@gmail.com
1 parent 6e1051a commit 66fc4ca

3 files changed

Lines changed: 52 additions & 92 deletions

File tree

‎samples/bpf/Makefile‎

Lines changed: 3 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_kern.o
167166
always-y += xdp_redirect_map_kern.o
168167
always-y += xdp_redirect_map_multi_kern.o
169168
always-y += xdp_redirect_cpu_kern.o
@@ -356,6 +355,7 @@ endef
356355

357356
CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
358357

358+
$(obj)/xdp_redirect.bpf.o: $(obj)/xdp_sample.bpf.o
359359
$(obj)/xdp_monitor.bpf.o: $(obj)/xdp_sample.bpf.o
360360

361361
$(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/xdp_sample_shared.h
@@ -366,9 +366,10 @@ $(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/x
366366
-I$(srctree)/tools/lib $(CLANG_SYS_INCLUDES) \
367367
-c $(filter %.bpf.c,$^) -o $@
368368

369-
LINKED_SKELS := xdp_monitor.skel.h
369+
LINKED_SKELS := xdp_redirect.skel.h xdp_monitor.skel.h
370370
clean-files += $(LINKED_SKELS)
371371

372+
xdp_redirect.skel.h-deps := xdp_redirect.bpf.o xdp_sample.bpf.o
372373
xdp_monitor.skel.h-deps := xdp_monitor.bpf.o xdp_sample.bpf.o
373374

374375
LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))

‎samples/bpf/xdp_redirect.bpf.c‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2016 John Fastabend <john.r.fastabend@intel.com>
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of version 2 of the GNU General Public
6+
* License as published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful, but
9+
* WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* General Public License for more details.
12+
*/
13+
#include "vmlinux.h"
14+
#include "xdp_sample.bpf.h"
15+
#include "xdp_sample_shared.h"
16+
17+
const volatile int ifindex_out;
18+
19+
SEC("xdp")
20+
int xdp_redirect_prog(struct xdp_md *ctx)
21+
{
22+
void *data_end = (void *)(long)ctx->data_end;
23+
void *data = (void *)(long)ctx->data;
24+
u32 key = bpf_get_smp_processor_id();
25+
struct ethhdr *eth = data;
26+
struct datarec *rec;
27+
u64 nh_off;
28+
29+
nh_off = sizeof(*eth);
30+
if (data + nh_off > data_end)
31+
return XDP_DROP;
32+
33+
rec = bpf_map_lookup_elem(&rx_cnt, &key);
34+
if (!rec)
35+
return XDP_PASS;
36+
NO_TEAR_INC(rec->processed);
37+
38+
swap_src_dst_mac(data);
39+
return bpf_redirect(ifindex_out, 0);
40+
}
41+
42+
/* Redirect require an XDP bpf_prog loaded on the TX device */
43+
SEC("xdp")
44+
int xdp_redirect_dummy_prog(struct xdp_md *ctx)
45+
{
46+
return XDP_PASS;
47+
}
48+
49+
char _license[] SEC("license") = "GPL";

‎samples/bpf/xdp_redirect_kern.c‎

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)