GNU Linux-libre 4.14.266-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_break = {
30         .type = BPF_MAP_TYPE_ARRAY,
31         .key_size = sizeof(int),
32         .value_size = sizeof(int),
33         .max_entries = 20,
34 };
35
36 SEC("sk_skb2")
37 int bpf_prog2(struct __sk_buff *skb)
38 {
39         void *data_end = (void *)(long) skb->data_end;
40         void *data = (void *)(long) skb->data;
41         __u32 lport = skb->local_port;
42         __u32 rport = skb->remote_port;
43         __u8 *d = data;
44         __u8 sk, map;
45
46         if (data + 8 > data_end)
47                 return SK_DROP;
48
49         map = d[0];
50         sk = d[1];
51
52         d[0] = 0xd;
53         d[1] = 0xe;
54         d[2] = 0xa;
55         d[3] = 0xd;
56         d[4] = 0xb;
57         d[5] = 0xe;
58         d[6] = 0xe;
59         d[7] = 0xf;
60
61         bpf_printk("verdict: data[0] = redir(%u:%u)\n", map, sk);
62
63         if (!map)
64                 return bpf_sk_redirect_map(skb, &sock_map_rx, sk, 0);
65         return bpf_sk_redirect_map(skb, &sock_map_tx, sk, 0);
66 }
67
68 char _license[] SEC("license") = "GPL";