1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bpf.h>
3 #include <bpf/bpf_helpers.h>
4 
5 struct {
6 	__uint(type, BPF_MAP_TYPE_DEVMAP);
7 	__uint(key_size, sizeof(__u32));
8 	__uint(value_size, sizeof(struct bpf_devmap_val));
9 	__uint(max_entries, 4);
10 } dm_ports SEC(".maps");
11 
12 SEC("xdp")
xdp_redir_prog(struct xdp_md * ctx)13 int xdp_redir_prog(struct xdp_md *ctx)
14 {
15 	return bpf_redirect_map(&dm_ports, 1, 0);
16 }
17 
18 /* invalid program on DEVMAP entry;
19  * SEC name means expected attach type not set
20  */
21 SEC("xdp")
xdp_dummy_prog(struct xdp_md * ctx)22 int xdp_dummy_prog(struct xdp_md *ctx)
23 {
24 	return XDP_PASS;
25 }
26 
27 /* valid program on DEVMAP entry via SEC name;
28  * has access to egress and ingress ifindex
29  */
30 SEC("xdp/devmap")
xdp_dummy_dm(struct xdp_md * ctx)31 int xdp_dummy_dm(struct xdp_md *ctx)
32 {
33 	char fmt[] = "devmap redirect: dev %u -> dev %u len %u\n";
34 	void *data_end = (void *)(long)ctx->data_end;
35 	void *data = (void *)(long)ctx->data;
36 	unsigned int len = data_end - data;
37 
38 	bpf_trace_printk(fmt, sizeof(fmt),
39 			 ctx->ingress_ifindex, ctx->egress_ifindex, len);
40 
41 	return XDP_PASS;
42 }
43 
44 SEC("xdp.frags/devmap")
xdp_dummy_dm_frags(struct xdp_md * ctx)45 int xdp_dummy_dm_frags(struct xdp_md *ctx)
46 {
47 	return XDP_PASS;
48 }
49 
50 char _license[] SEC("license") = "GPL";
51