samples/bpf: Add tunnel set/get tests.
[cascardo/linux.git] / samples / bpf / tcbpf2_kern.c
1 /* Copyright (c) 2016 VMware
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of version 2 of the GNU General Public
5  * License as published by the Free Software Foundation.
6  */
7 #include <uapi/linux/bpf.h>
8 #include <uapi/linux/if_ether.h>
9 #include <uapi/linux/if_packet.h>
10 #include <uapi/linux/ip.h>
11 #include <uapi/linux/in.h>
12 #include <uapi/linux/tcp.h>
13 #include <uapi/linux/filter.h>
14 #include <uapi/linux/pkt_cls.h>
15 #include "bpf_helpers.h"
16
17 #define ERROR(ret) do {\
18                 char fmt[] = "ERROR line:%d ret:%d\n";\
19                 bpf_trace_printk(fmt, sizeof(fmt), __LINE__, ret); \
20         } while(0)
21
22 struct geneve_opt {
23         __be16  opt_class;
24         u8      type;
25         u8      length:5;
26         u8      r3:1;
27         u8      r2:1;
28         u8      r1:1;
29         u8      opt_data[8]; /* hard-coded to 8 byte */
30 };
31
32 struct vxlan_metadata {
33         u32     gbp;
34 };
35
36 SEC("gre_set_tunnel")
37 int _gre_set_tunnel(struct __sk_buff *skb)
38 {
39         int ret;
40         struct bpf_tunnel_key key;
41
42         __builtin_memset(&key, 0x0, sizeof(key));
43         key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
44         key.tunnel_id = 2;
45         key.tunnel_tos = 0;
46         key.tunnel_ttl = 64;
47
48         ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), BPF_F_ZERO_CSUM_TX);
49         if (ret < 0) {
50                 ERROR(ret);
51                 return TC_ACT_SHOT;
52         }
53
54         return TC_ACT_OK;
55 }
56
57 SEC("gre_get_tunnel")
58 int _gre_get_tunnel(struct __sk_buff *skb)
59 {
60         int ret;
61         struct bpf_tunnel_key key;
62         char fmt[] = "key %d remote ip 0x%x\n";
63
64         ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
65         if (ret < 0) {
66                 ERROR(ret);
67                 return TC_ACT_SHOT;
68         }
69
70         bpf_trace_printk(fmt, sizeof(fmt), key.tunnel_id, key.remote_ipv4);
71         return TC_ACT_OK;
72 }
73
74 SEC("vxlan_set_tunnel")
75 int _vxlan_set_tunnel(struct __sk_buff *skb)
76 {
77         int ret;
78         struct bpf_tunnel_key key;
79         struct vxlan_metadata md;
80
81         __builtin_memset(&key, 0x0, sizeof(key));
82         key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
83         key.tunnel_id = 2;
84         key.tunnel_tos = 0;
85         key.tunnel_ttl = 64;
86
87         ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), BPF_F_ZERO_CSUM_TX);
88         if (ret < 0) {
89                 ERROR(ret);
90                 return TC_ACT_SHOT;
91         }
92
93         md.gbp = 0x800FF; /* Set VXLAN Group Policy extension */
94         ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
95         if (ret < 0) {
96                 ERROR(ret);
97                 return TC_ACT_SHOT;
98         }
99
100         return TC_ACT_OK;
101 }
102
103 SEC("vxlan_get_tunnel")
104 int _vxlan_get_tunnel(struct __sk_buff *skb)
105 {
106         int ret;
107         struct bpf_tunnel_key key;
108         struct vxlan_metadata md;
109         char fmt[] = "key %d remote ip 0x%x vxlan gbp 0x%x\n";
110
111         ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
112         if (ret < 0) {
113                 ERROR(ret);
114                 return TC_ACT_SHOT;
115         }
116
117         ret = bpf_skb_get_tunnel_opt(skb, &md, sizeof(md));
118         if (ret < 0) {
119                 ERROR(ret);
120                 return TC_ACT_SHOT;
121         }
122
123         bpf_trace_printk(fmt, sizeof(fmt),
124                         key.tunnel_id, key.remote_ipv4, md.gbp);
125
126         return TC_ACT_OK;
127 }
128
129 SEC("geneve_set_tunnel")
130 int _geneve_set_tunnel(struct __sk_buff *skb)
131 {
132         int ret, ret2;
133         struct bpf_tunnel_key key;
134         struct geneve_opt gopt;
135
136         __builtin_memset(&key, 0x0, sizeof(key));
137         key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
138         key.tunnel_id = 2;
139         key.tunnel_tos = 0;
140         key.tunnel_ttl = 64;
141
142         __builtin_memset(&gopt, 0x0, sizeof(gopt));
143         gopt.opt_class = 0x102; /* Open Virtual Networking (OVN) */
144         gopt.type = 0x08;
145         gopt.r1 = 1;
146         gopt.r2 = 0;
147         gopt.r3 = 1;
148         gopt.length = 2; /* 4-byte multiple */
149         *(int *) &gopt.opt_data = 0xdeadbeef;
150
151         ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), BPF_F_ZERO_CSUM_TX);
152         if (ret < 0) {
153                 ERROR(ret);
154                 return TC_ACT_SHOT;
155         }
156
157         ret = bpf_skb_set_tunnel_opt(skb, &gopt, sizeof(gopt));
158         if (ret < 0) {
159                 ERROR(ret);
160                 return TC_ACT_SHOT;
161         }
162
163         return TC_ACT_OK;
164 }
165
166 SEC("geneve_get_tunnel")
167 int _geneve_get_tunnel(struct __sk_buff *skb)
168 {
169         int ret;
170         struct bpf_tunnel_key key;
171         struct geneve_opt gopt;
172         char fmt[] = "key %d remote ip 0x%x geneve class 0x%x\n";
173
174         ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
175         if (ret < 0) {
176                 ERROR(ret);
177                 return TC_ACT_SHOT;
178         }
179
180         ret = bpf_skb_get_tunnel_opt(skb, &gopt, sizeof(gopt));
181         if (ret < 0) {
182                 ERROR(ret);
183                 return TC_ACT_SHOT;
184         }
185
186         bpf_trace_printk(fmt, sizeof(fmt),
187                         key.tunnel_id, key.remote_ipv4, gopt.opt_class);
188         return TC_ACT_OK;
189 }
190
191 char _license[] SEC("license") = "GPL";