-
Notifications
You must be signed in to change notification settings - Fork 2
/
xdp-flowradar_kern.c
501 lines (421 loc) · 11.8 KB
/
xdp-flowradar_kern.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#include "data.h"
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <netinet/in.h>
#include <linux/in6.h>
#include <stdint.h>
#include <linux/version.h>
#include "bpf_helpers.h"
#include <linux/if_vlan.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#define NUM_HASHES 6
#define BF_BITS (UINT16_MAX + 1)
#define ELEM_SIZE sizeof(uint64_t)
#define BITS_PER_ELEM (ELEM_SIZE*8)
#define BF_SIZE (BF_BITS/BITS_PER_ELEM)
/* Disable 8021Q and 8021AD eth tags for now, since we don't really
* have a way to test them
*/
//#define ETHTAGS 1
/* Enable debug output. Note that disabling this will get rid of a bunch of
* calls to bpf_trace_printk, and might result in a lot of code being
* optimized out
*/
#define DEBUG 1
#ifdef DEBUG
/* Only use this for debug output. Notice output from bpf_trace_printk()
* end-up in /sys/kernel/debug/tracing/trace_pipe
*/
#define bpf_debug(fmt, ...) \
({ \
char ____fmt[] = fmt; \
bpf_trace_printk(____fmt, sizeof(____fmt), \
##__VA_ARGS__); \
})
#else
#define bpf_debug(fmt, ...) { } while (0)
#endif
/* vlan header. Not often used (or well tested) */
struct vlan_hdr {
__be16 h_vlan_TCI;
__be16 h_vlan_encapsulated_proto;
};
// 0
struct bpf_map_def SEC("maps") bloomfilter = {
.type = BPF_MAP_TYPE_PERCPU_ARRAY,
.key_size = sizeof(uint32_t),
.value_size = ELEM_SIZE,
.max_entries = BF_SIZE,
};
// 1
struct bpf_map_def SEC("maps") flow_info = {
.type = BPF_MAP_TYPE_PERCPU_ARRAY,
.key_size = sizeof(uint32_t),
.value_size = sizeof(struct flow_info),
.max_entries = 65536,
};
/* A bunch of maps. They are just counters for testing */
// 2
struct bpf_map_def SEC("maps") eth_proto_count = {
.type = BPF_MAP_TYPE_PERCPU_HASH,
.key_size = sizeof(uint16_t),
.value_size = sizeof(uint64_t),
.max_entries = 64,
};
// 3
struct bpf_map_def SEC("maps") ip_proto_count = {
.type = BPF_MAP_TYPE_PERCPU_HASH,
.key_size = sizeof(uint16_t),
.value_size = sizeof(uint64_t),
.max_entries = 64,
};
// 4
struct bpf_map_def SEC("maps") sport_count = {
.type = BPF_MAP_TYPE_PERCPU_HASH,
.key_size = sizeof(uint16_t),
.value_size = sizeof(uint64_t),
.max_entries = 64,
};
// 5
struct bpf_map_def SEC("maps") dport_count = {
.type = BPF_MAP_TYPE_PERCPU_HASH,
.key_size = sizeof(uint16_t),
.value_size = sizeof(uint64_t),
.max_entries = 64,
};
// 6
struct bpf_map_def SEC("maps") sip_count = {
.type = BPF_MAP_TYPE_PERCPU_HASH,
.key_size = sizeof(uint32_t),
.value_size = sizeof(uint64_t),
.max_entries = 64,
};
// 7
struct bpf_map_def SEC("maps") dip_count = {
.type = BPF_MAP_TYPE_PERCPU_HASH,
.key_size = sizeof(uint32_t),
.value_size = sizeof(uint64_t),
.max_entries = 64,
};
// 8
struct bpf_map_def SEC("maps") host_info_map = {
.type = BPF_MAP_TYPE_PERCPU_ARRAY,
.key_size = sizeof(uint32_t),
.value_size = sizeof(struct host_info),
.max_entries = 1,
};
/* A simple hash function, based on rs hash here: http://www.partow.net/programming/hashfunctions/
* Splits up ip addrs into two 16 bit uints and starts with k as a seed
* Returns a 16 bit uint
* TODO: make it better
*/
static __always_inline
uint16_t hash(uint16_t host, uint8_t k, struct five_tuple *ft) {
unsigned long i;
uint32_t a = 63689;
uint32_t b = 378551;
char *ptr = (void *)ft;
uint32_t hash = k;
#pragma unroll
for (i=0; i<sizeof(uint16_t); i++) {
ptr = (void *)&host + i;
hash = hash * a + *ptr;
a = a * b;
}
#pragma unroll
for (i=0; i<sizeof(struct five_tuple); i++) {
ptr = (void *)ft + i;
hash = hash * a + *ptr;
a = a * b;
}
// We've been doing math on a uint32, so xor the high and low bits together
uint16_t h1 = (uint16_t)(hash & 0x0000ffff);
uint16_t h2 = (uint16_t)(hash >> 16);
return h1 ^ h2;;
}
/* For when we only need a uint8 hash
*/
/*
static __always_inline
uint8_t hash8(uint16_t host, uint8_t k, struct five_tuple *ft) {
uint16_t h16 = hash(host, k, ft);
uint8_t h8 = h16 % UINT8_MAX;
return h8;
}
*/
static __always_inline
void set_bit(uint16_t bit, struct bpf_map_def *map) {
unsigned int elem = BF_SIZE - (bit/BITS_PER_ELEM + 1);
elem = elem % BF_SIZE;
uint64_t mask = (uint64_t)0x1 << (bit % BITS_PER_ELEM);
uint64_t *value;
value = bpf_map_lookup_elem(map, &elem);
if (value) {
*value |= mask;
}
}
static __always_inline
uint64_t test_bit(uint16_t bit, struct bpf_map_def *map) {
unsigned int elem = BF_SIZE - (bit/BITS_PER_ELEM + 1);
elem = elem % BF_SIZE;
uint64_t mask = (uint64_t)0x1 << (bit % BITS_PER_ELEM);
uint64_t *value;
value = bpf_map_lookup_elem(map, &elem);
if (value)
return *value & mask;
else
return 0;
}
static __always_inline
void add_flow(uint16_t index, struct five_tuple *ft, struct bpf_map_def *map) {
struct flow_info *fi;
char *ptr_old;
char *ptr_new;
fi = bpf_map_lookup_elem(map, &index);
if (!fi)
return;
#pragma unroll
for (unsigned long i=0; i<sizeof(struct five_tuple); i++) {
ptr_old = (char *)(&(fi->ft))+i;
ptr_new = (char *)(ft)+i;
*ptr_old ^= *ptr_new;
}
fi->flow_count += 1;
}
static __always_inline
void increment_packet_count(uint64_t index, struct bpf_map_def *map) {
struct flow_info *fi;
fi = bpf_map_lookup_elem(map, &index);
if (!fi)
return;
fi->packet_count += 1;
}
/* Parse ethernet headers
* Takes the header and end location, and puts the proto and l3_offset in
* the proper params. l3_offset is the location of the next header (likely ip)
*/
static __always_inline
int16_t parse_eth(struct ethhdr *eth, void *data_end, uint16_t *eth_proto,
uint64_t *l3_offset)
{
uint16_t eth_type;
uint64_t offset;
offset = sizeof(*eth);
// this test is neede to satisfy the verifier, so it knows we won't do any
// out of bounds accesses
if ((void *)eth + offset > data_end)
return -1;
eth_type = eth->h_proto;
#ifdef ETHTAGS
// ETH_P_8921Q is single tag, ETH_P_8021AD is double tag
if (eth_type == htons(ETH_P_8021Q) || eth_type == htons(ETH_P_8021AD)) {
struct vlan_hdr *vlan;
vlan = (void *)eth + offset;
offset += sizeof(*vlan);
if ((void *)eth + offset > data_end)
return -1;
eth_type = vlan->h_vlan_encapsulated_proto;
}
// a wild double tag appeared!
if (eth_type == htons(ETH_P_8021Q)) {
struct vlan_hdr *vlan2;
vlan2 = (void *)eth + offset;
offset += sizeof(*vlan2);
if ((void *)eth + offset > data_end)
return -1;
eth_type = vlan2->h_vlan_encapsulated_proto;
}
#endif
// ntohs converts network byteorder to host byteorder (for 16 bit value)
*eth_proto = ntohs(eth_type);
*l3_offset = offset;
return 0;
}
/* Parse an ipv4 header
* Takes the context and l3 offset and outputs the proto and l4_offset, and
* puts the source and dast addrs in ft
* l4_offset is the location in ctx of the next header (tcp, udp, icmp, etc)
*/
static __always_inline
int16_t parse_ipv4(struct xdp_md *ctx, uint64_t l3_offset, uint16_t *ip_proto,
uint32_t *l4_offset, struct five_tuple *ft)
{
uint64_t offset;
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct iphdr *iph = data + l3_offset;
offset = l3_offset + sizeof(*iph);
// We need to satisfy that the header start is in the packet
if (data + l3_offset > data_end)
return -1;
// We need to satisfy that the header end is in the packet
if ((void *)iph + sizeof(*iph) > data_end)
return -1;
uint8_t hdr_len = iph->ihl;
// ihl cannot be less than 5 (20 bytes)
if (hdr_len < 5)
return -1;
int32_t data_len = data_end - (void *)iph;
uint16_t tot_len = ntohs(iph->tot_len);
// The header length can't be less than 20 bytes
if (tot_len < 20)
return -1;
// Warn if the data len (calculated from the data end) is different from
// what's expected from the header
if (data_len != tot_len) {
bpf_debug("WARN: packet end does not match total length\n");
}
// Satisfy the verifier that ihl is in this range
// TODO: is this necessary?
uint8_t ihl = (iph->ihl);
if (ihl < 5)
ihl = 5;
if (ihl > 15)
ihl = 15;
offset = ihl*4 + l3_offset;
*l4_offset = offset;
*ip_proto = iph->protocol;
ft->saddr = iph->saddr;
ft->daddr = iph->daddr;
return 0;
}
/* Parse tcp and udp packets
* This was multiple functions, but the verifier didn't like it
* Takes cts, the proto, and the l4_offset, and puts the sport and dport in ft
*/
static __always_inline
int16_t parse_tcp_udp(struct xdp_md *ctx, uint16_t proto, uint32_t l4_offset, struct five_tuple *ft)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
// Satisfy that the header start in in data
if (data + l4_offset > data_end) {
bpf_debug("Packet l4_offset outside data_end\n");
return -1;
}
if (proto == IPPROTO_TCP) {
struct tcphdr *thdr = data + l4_offset;
// Satisfy that the header end is in data
if ((void *)thdr + sizeof(*thdr) > data_end) {
bpf_debug("TCP Packet header ends outside data_end\n");
return -1;
}
ft->sport = ntohs(thdr->source);
ft->dport = ntohs(thdr->dest);
return 0;
} else if (proto == IPPROTO_UDP) {
struct udphdr *uhdr = data + l4_offset;
// Satisfy that the header end is in the data
if ((void *)uhdr + sizeof(*uhdr) > data_end) {
bpf_debug("UPD Packet header ends outside data_end\n");
return -1;
}
ft->sport = ntohs(uhdr->source);
ft->dport = ntohs(uhdr->dest);
return 0;
}
return -1;
}
/* Increment a map value at key
* if the key is not in the map, initialize it to 1
*/
static __always_inline
void increment_map(struct bpf_map_def *map, void *key)
{
long *value;
value = bpf_map_lookup_elem(map, key);
if (value) {
*value += 1;
} else {
long v;
v = 1;
bpf_map_update_elem(map, key, &v, BPF_NOEXIST);
}
}
/* The main program.
* TODO: rename this?
*/
SEC("xdp")
int xdp_pass(struct xdp_md *ctx)
{
uint64_t l3_offset = 0;
uint16_t eth_proto = 0;
uint32_t l4_offset = 0;
uint16_t ip_proto = 0;
void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end;
struct ethhdr *eth = data;
if (parse_eth(eth, data_end, ð_proto, &l3_offset)) {
bpf_debug("Cannot parse eth header");
return XDP_PASS;
}
increment_map(ð_proto_count, ð_proto);
int16_t parse_ip = 0;
struct five_tuple ft;
switch (eth_proto) {
case ETH_P_IP:
parse_ip = parse_ipv4(ctx, l3_offset, &ip_proto, &l4_offset, &ft);
break;
case ETH_P_IPV6:
// TODO: IPv6 support?
case ETH_P_ARP:
default:
return XDP_PASS;
}
if (parse_ip) {
bpf_debug("ERROR: error parsing IP header\n");
return XDP_PASS;
}
increment_map(&ip_proto_count, &ip_proto);
ft.proto = ip_proto;
uint16_t parse_data = 0;
switch (ip_proto) {
case IPPROTO_TCP:
case IPPROTO_UDP:
parse_data = parse_tcp_udp(ctx, ip_proto, l4_offset, &ft);
break;
case IPPROTO_ICMP:
case IPPROTO_ICMPV6:
default:
return XDP_PASS;
}
if (parse_data) {
bpf_debug("ERROR: error parsing data header\n");
return XDP_PASS;
}
increment_map(&sport_count, &ft.sport);
increment_map(&dport_count, &ft.dport);
increment_map(&sip_count, &ft.saddr);
increment_map(&dip_count, &ft.daddr);
struct host_info *hi;
uint16_t host = 0;
int zero = 0;
hi = bpf_map_lookup_elem(&host_info_map, &zero);
if (hi)
host = *(uint16_t *)hi;
uint16_t hashes[NUM_HASHES];
int any_zero = 0;
#pragma unroll
for (int i=0; i<NUM_HASHES; i++) {
hashes[i] = hash(host, i, &ft);
if (!(test_bit(hashes[i], &bloomfilter)))
any_zero = 1;
}
if (any_zero) {
#pragma unroll
for (int i=0; i<NUM_HASHES; i++) {
set_bit(hashes[i], &bloomfilter);
add_flow(hashes[i], &ft, &flow_info);
}
}
#pragma unroll
for (int i=0; i<NUM_HASHES; i++) {
increment_packet_count(hashes[i], &flow_info);
}
return XDP_PASS;
}
char __license[] SEC("license") = "GPL";
uint32_t _version SEC("version") = LINUX_VERSION_CODE;