GNU Linux-libre 4.19.264-gnu1
[releases.git] / tools / testing / selftests / bpf / sockmap_verdict_prog.c
1 #include <linux/bpf.h>
2 #include "bpf_helpers.h"
3 #include "bpf_util.h"
4 #include "bpf_endian.h"
5
6 int _version SEC("version") = 1;
7
8 #define bpf_printk(fmt, ...)                                    \
9 ({                                                              \
10                char ____fmt[] = fmt;                            \
11                bpf_trace_printk(____fmt, sizeof(____fmt),       \
12                                 ##__VA_ARGS__);                 \
13 })
14
15 struct bpf_map_def SEC("maps") sock_map_rx = {
16         .type = BPF_MAP_TYPE_SOCKMAP,
17         .key_size = sizeof(int),
18         .value_size = sizeof(int),
19         .max_entries = 20,
20 };
21
22 struct bpf_map_def SEC("maps") sock_map_tx = {
23         .type = BPF_MAP_TYPE_SOCKMAP,
24         .key_size = sizeof(int),
25         .value_size = sizeof(int),
26         .max_entries = 20,
27 };
28
29 struct bpf_map_def SEC("maps") sock_map_msg = {
30         .type = BPF_MAP_TYPE_SOCKMAP,
31         .key_size = sizeof(int),
32         .value_size = sizeof(int),
33         .max_entries = 20,
34 };
35
36 struct bpf_map_def SEC("maps") sock_map_break = {
37         .type = BPF_MAP_TYPE_ARRAY,
38         .key_size = sizeof(int),
39         .value_size = sizeof(int),
40         .max_entries = 20,
41 };
42
43 SEC("sk_skb2")
44 int bpf_prog2(struct __sk_buff *skb)
45 {
46         void *data_end = (void *)(long) skb->data_end;
47         void *data = (void *)(long) skb->data;
48         __u32 lport = skb->local_port;
49         __u32 rport = skb->remote_port;
50         __u8 *d = data;
51         __u8 sk, map;
52
53         if (data + 8 > data_end)
54                 return SK_DROP;
55
56         map = d[0];
57         sk = d[1];
58
59         d[0] = 0xd;
60         d[1] = 0xe;
61         d[2] = 0xa;
62         d[3] = 0xd;
63         d[4] = 0xb;
64         d[5] = 0xe;
65         d[6] = 0xe;
66         d[7] = 0xf;
67
68         if (!map)
69                 return bpf_sk_redirect_map(skb, &sock_map_rx, sk, 0);
70         return bpf_sk_redirect_map(skb, &sock_map_tx, sk, 0);
71 }
72
73 char _license[] SEC("license") = "GPL";