xref: /linux/tools/testing/selftests/bpf/progs/test_l4lb.c (revision 31d166642c7c601c65eccf0ff2e0afe9a0538be2)
1 /* Copyright (c) 2017 Facebook
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 <stddef.h>
8 #include <stdbool.h>
9 #include <string.h>
10 #include <linux/pkt_cls.h>
11 #include <linux/bpf.h>
12 #include <linux/in.h>
13 #include <linux/if_ether.h>
14 #include <linux/ip.h>
15 #include <linux/ipv6.h>
16 #include <linux/icmp.h>
17 #include <linux/icmpv6.h>
18 #include <linux/tcp.h>
19 #include <linux/udp.h>
20 #include "bpf_helpers.h"
21 #include "test_iptunnel_common.h"
22 #include "bpf_endian.h"
23 
24 int _version SEC("version") = 1;
25 
26 static inline __u32 rol32(__u32 word, unsigned int shift)
27 {
28 	return (word << shift) | (word >> ((-shift) & 31));
29 }
30 
31 /* copy paste of jhash from kernel sources to make sure llvm
32  * can compile it into valid sequence of bpf instructions
33  */
34 #define __jhash_mix(a, b, c)			\
35 {						\
36 	a -= c;  a ^= rol32(c, 4);  c += b;	\
37 	b -= a;  b ^= rol32(a, 6);  a += c;	\
38 	c -= b;  c ^= rol32(b, 8);  b += a;	\
39 	a -= c;  a ^= rol32(c, 16); c += b;	\
40 	b -= a;  b ^= rol32(a, 19); a += c;	\
41 	c -= b;  c ^= rol32(b, 4);  b += a;	\
42 }
43 
44 #define __jhash_final(a, b, c)			\
45 {						\
46 	c ^= b; c -= rol32(b, 14);		\
47 	a ^= c; a -= rol32(c, 11);		\
48 	b ^= a; b -= rol32(a, 25);		\
49 	c ^= b; c -= rol32(b, 16);		\
50 	a ^= c; a -= rol32(c, 4);		\
51 	b ^= a; b -= rol32(a, 14);		\
52 	c ^= b; c -= rol32(b, 24);		\
53 }
54 
55 #define JHASH_INITVAL		0xdeadbeef
56 
57 typedef unsigned int u32;
58 
59 static inline u32 jhash(const void *key, u32 length, u32 initval)
60 {
61 	u32 a, b, c;
62 	const unsigned char *k = key;
63 
64 	a = b = c = JHASH_INITVAL + length + initval;
65 
66 	while (length > 12) {
67 		a += *(u32 *)(k);
68 		b += *(u32 *)(k + 4);
69 		c += *(u32 *)(k + 8);
70 		__jhash_mix(a, b, c);
71 		length -= 12;
72 		k += 12;
73 	}
74 	switch (length) {
75 	case 12: c += (u32)k[11]<<24;
76 	case 11: c += (u32)k[10]<<16;
77 	case 10: c += (u32)k[9]<<8;
78 	case 9:  c += k[8];
79 	case 8:  b += (u32)k[7]<<24;
80 	case 7:  b += (u32)k[6]<<16;
81 	case 6:  b += (u32)k[5]<<8;
82 	case 5:  b += k[4];
83 	case 4:  a += (u32)k[3]<<24;
84 	case 3:  a += (u32)k[2]<<16;
85 	case 2:  a += (u32)k[1]<<8;
86 	case 1:  a += k[0];
87 		 __jhash_final(a, b, c);
88 	case 0: /* Nothing left to add */
89 		break;
90 	}
91 
92 	return c;
93 }
94 
95 static inline u32 __jhash_nwords(u32 a, u32 b, u32 c, u32 initval)
96 {
97 	a += initval;
98 	b += initval;
99 	c += initval;
100 	__jhash_final(a, b, c);
101 	return c;
102 }
103 
104 static inline u32 jhash_2words(u32 a, u32 b, u32 initval)
105 {
106 	return __jhash_nwords(a, b, 0, initval + JHASH_INITVAL + (2 << 2));
107 }
108 
109 #define PCKT_FRAGMENTED 65343
110 #define IPV4_HDR_LEN_NO_OPT 20
111 #define IPV4_PLUS_ICMP_HDR 28
112 #define IPV6_PLUS_ICMP_HDR 48
113 #define RING_SIZE 2
114 #define MAX_VIPS 12
115 #define MAX_REALS 5
116 #define CTL_MAP_SIZE 16
117 #define CH_RINGS_SIZE (MAX_VIPS * RING_SIZE)
118 #define F_IPV6 (1 << 0)
119 #define F_HASH_NO_SRC_PORT (1 << 0)
120 #define F_ICMP (1 << 0)
121 #define F_SYN_SET (1 << 1)
122 
123 struct packet_description {
124 	union {
125 		__be32 src;
126 		__be32 srcv6[4];
127 	};
128 	union {
129 		__be32 dst;
130 		__be32 dstv6[4];
131 	};
132 	union {
133 		__u32 ports;
134 		__u16 port16[2];
135 	};
136 	__u8 proto;
137 	__u8 flags;
138 };
139 
140 struct ctl_value {
141 	union {
142 		__u64 value;
143 		__u32 ifindex;
144 		__u8 mac[6];
145 	};
146 };
147 
148 struct vip_meta {
149 	__u32 flags;
150 	__u32 vip_num;
151 };
152 
153 struct real_definition {
154 	union {
155 		__be32 dst;
156 		__be32 dstv6[4];
157 	};
158 	__u8 flags;
159 };
160 
161 struct vip_stats {
162 	__u64 bytes;
163 	__u64 pkts;
164 };
165 
166 struct eth_hdr {
167 	unsigned char eth_dest[ETH_ALEN];
168 	unsigned char eth_source[ETH_ALEN];
169 	unsigned short eth_proto;
170 };
171 
172 struct {
173 	__u32 type;
174 	__u32 max_entries;
175 	struct vip *key;
176 	struct vip_meta *value;
177 } vip_map SEC(".maps") = {
178 	.type = BPF_MAP_TYPE_HASH,
179 	.max_entries = MAX_VIPS,
180 };
181 
182 struct {
183 	__u32 type;
184 	__u32 max_entries;
185 	__u32 *key;
186 	__u32 *value;
187 } ch_rings SEC(".maps") = {
188 	.type = BPF_MAP_TYPE_ARRAY,
189 	.max_entries = CH_RINGS_SIZE,
190 };
191 
192 struct {
193 	__u32 type;
194 	__u32 max_entries;
195 	__u32 *key;
196 	struct real_definition *value;
197 } reals SEC(".maps") = {
198 	.type = BPF_MAP_TYPE_ARRAY,
199 	.max_entries = MAX_REALS,
200 };
201 
202 struct {
203 	__u32 type;
204 	__u32 max_entries;
205 	__u32 *key;
206 	struct vip_stats *value;
207 } stats SEC(".maps") = {
208 	.type = BPF_MAP_TYPE_PERCPU_ARRAY,
209 	.max_entries = MAX_VIPS,
210 };
211 
212 struct {
213 	__u32 type;
214 	__u32 max_entries;
215 	__u32 *key;
216 	struct ctl_value *value;
217 } ctl_array SEC(".maps") = {
218 	.type = BPF_MAP_TYPE_ARRAY,
219 	.max_entries = CTL_MAP_SIZE,
220 };
221 
222 static __always_inline __u32 get_packet_hash(struct packet_description *pckt,
223 					     bool ipv6)
224 {
225 	if (ipv6)
226 		return jhash_2words(jhash(pckt->srcv6, 16, MAX_VIPS),
227 				    pckt->ports, CH_RINGS_SIZE);
228 	else
229 		return jhash_2words(pckt->src, pckt->ports, CH_RINGS_SIZE);
230 }
231 
232 static __always_inline bool get_packet_dst(struct real_definition **real,
233 					   struct packet_description *pckt,
234 					   struct vip_meta *vip_info,
235 					   bool is_ipv6)
236 {
237 	__u32 hash = get_packet_hash(pckt, is_ipv6) % RING_SIZE;
238 	__u32 key = RING_SIZE * vip_info->vip_num + hash;
239 	__u32 *real_pos;
240 
241 	real_pos = bpf_map_lookup_elem(&ch_rings, &key);
242 	if (!real_pos)
243 		return false;
244 	key = *real_pos;
245 	*real = bpf_map_lookup_elem(&reals, &key);
246 	if (!(*real))
247 		return false;
248 	return true;
249 }
250 
251 static __always_inline int parse_icmpv6(void *data, void *data_end, __u64 off,
252 					struct packet_description *pckt)
253 {
254 	struct icmp6hdr *icmp_hdr;
255 	struct ipv6hdr *ip6h;
256 
257 	icmp_hdr = data + off;
258 	if (icmp_hdr + 1 > data_end)
259 		return TC_ACT_SHOT;
260 	if (icmp_hdr->icmp6_type != ICMPV6_PKT_TOOBIG)
261 		return TC_ACT_OK;
262 	off += sizeof(struct icmp6hdr);
263 	ip6h = data + off;
264 	if (ip6h + 1 > data_end)
265 		return TC_ACT_SHOT;
266 	pckt->proto = ip6h->nexthdr;
267 	pckt->flags |= F_ICMP;
268 	memcpy(pckt->srcv6, ip6h->daddr.s6_addr32, 16);
269 	memcpy(pckt->dstv6, ip6h->saddr.s6_addr32, 16);
270 	return TC_ACT_UNSPEC;
271 }
272 
273 static __always_inline int parse_icmp(void *data, void *data_end, __u64 off,
274 				      struct packet_description *pckt)
275 {
276 	struct icmphdr *icmp_hdr;
277 	struct iphdr *iph;
278 
279 	icmp_hdr = data + off;
280 	if (icmp_hdr + 1 > data_end)
281 		return TC_ACT_SHOT;
282 	if (icmp_hdr->type != ICMP_DEST_UNREACH ||
283 	    icmp_hdr->code != ICMP_FRAG_NEEDED)
284 		return TC_ACT_OK;
285 	off += sizeof(struct icmphdr);
286 	iph = data + off;
287 	if (iph + 1 > data_end)
288 		return TC_ACT_SHOT;
289 	if (iph->ihl != 5)
290 		return TC_ACT_SHOT;
291 	pckt->proto = iph->protocol;
292 	pckt->flags |= F_ICMP;
293 	pckt->src = iph->daddr;
294 	pckt->dst = iph->saddr;
295 	return TC_ACT_UNSPEC;
296 }
297 
298 static __always_inline bool parse_udp(void *data, __u64 off, void *data_end,
299 				      struct packet_description *pckt)
300 {
301 	struct udphdr *udp;
302 	udp = data + off;
303 
304 	if (udp + 1 > data_end)
305 		return false;
306 
307 	if (!(pckt->flags & F_ICMP)) {
308 		pckt->port16[0] = udp->source;
309 		pckt->port16[1] = udp->dest;
310 	} else {
311 		pckt->port16[0] = udp->dest;
312 		pckt->port16[1] = udp->source;
313 	}
314 	return true;
315 }
316 
317 static __always_inline bool parse_tcp(void *data, __u64 off, void *data_end,
318 				      struct packet_description *pckt)
319 {
320 	struct tcphdr *tcp;
321 
322 	tcp = data + off;
323 	if (tcp + 1 > data_end)
324 		return false;
325 
326 	if (tcp->syn)
327 		pckt->flags |= F_SYN_SET;
328 
329 	if (!(pckt->flags & F_ICMP)) {
330 		pckt->port16[0] = tcp->source;
331 		pckt->port16[1] = tcp->dest;
332 	} else {
333 		pckt->port16[0] = tcp->dest;
334 		pckt->port16[1] = tcp->source;
335 	}
336 	return true;
337 }
338 
339 static __always_inline int process_packet(void *data, __u64 off, void *data_end,
340 					  bool is_ipv6, struct __sk_buff *skb)
341 {
342 	void *pkt_start = (void *)(long)skb->data;
343 	struct packet_description pckt = {};
344 	struct eth_hdr *eth = pkt_start;
345 	struct bpf_tunnel_key tkey = {};
346 	struct vip_stats *data_stats;
347 	struct real_definition *dst;
348 	struct vip_meta *vip_info;
349 	struct ctl_value *cval;
350 	__u32 v4_intf_pos = 1;
351 	__u32 v6_intf_pos = 2;
352 	struct ipv6hdr *ip6h;
353 	struct vip vip = {};
354 	struct iphdr *iph;
355 	int tun_flag = 0;
356 	__u16 pkt_bytes;
357 	__u64 iph_len;
358 	__u32 ifindex;
359 	__u8 protocol;
360 	__u32 vip_num;
361 	int action;
362 
363 	tkey.tunnel_ttl = 64;
364 	if (is_ipv6) {
365 		ip6h = data + off;
366 		if (ip6h + 1 > data_end)
367 			return TC_ACT_SHOT;
368 
369 		iph_len = sizeof(struct ipv6hdr);
370 		protocol = ip6h->nexthdr;
371 		pckt.proto = protocol;
372 		pkt_bytes = bpf_ntohs(ip6h->payload_len);
373 		off += iph_len;
374 		if (protocol == IPPROTO_FRAGMENT) {
375 			return TC_ACT_SHOT;
376 		} else if (protocol == IPPROTO_ICMPV6) {
377 			action = parse_icmpv6(data, data_end, off, &pckt);
378 			if (action >= 0)
379 				return action;
380 			off += IPV6_PLUS_ICMP_HDR;
381 		} else {
382 			memcpy(pckt.srcv6, ip6h->saddr.s6_addr32, 16);
383 			memcpy(pckt.dstv6, ip6h->daddr.s6_addr32, 16);
384 		}
385 	} else {
386 		iph = data + off;
387 		if (iph + 1 > data_end)
388 			return TC_ACT_SHOT;
389 		if (iph->ihl != 5)
390 			return TC_ACT_SHOT;
391 
392 		protocol = iph->protocol;
393 		pckt.proto = protocol;
394 		pkt_bytes = bpf_ntohs(iph->tot_len);
395 		off += IPV4_HDR_LEN_NO_OPT;
396 
397 		if (iph->frag_off & PCKT_FRAGMENTED)
398 			return TC_ACT_SHOT;
399 		if (protocol == IPPROTO_ICMP) {
400 			action = parse_icmp(data, data_end, off, &pckt);
401 			if (action >= 0)
402 				return action;
403 			off += IPV4_PLUS_ICMP_HDR;
404 		} else {
405 			pckt.src = iph->saddr;
406 			pckt.dst = iph->daddr;
407 		}
408 	}
409 	protocol = pckt.proto;
410 
411 	if (protocol == IPPROTO_TCP) {
412 		if (!parse_tcp(data, off, data_end, &pckt))
413 			return TC_ACT_SHOT;
414 	} else if (protocol == IPPROTO_UDP) {
415 		if (!parse_udp(data, off, data_end, &pckt))
416 			return TC_ACT_SHOT;
417 	} else {
418 		return TC_ACT_SHOT;
419 	}
420 
421 	if (is_ipv6)
422 		memcpy(vip.daddr.v6, pckt.dstv6, 16);
423 	else
424 		vip.daddr.v4 = pckt.dst;
425 
426 	vip.dport = pckt.port16[1];
427 	vip.protocol = pckt.proto;
428 	vip_info = bpf_map_lookup_elem(&vip_map, &vip);
429 	if (!vip_info) {
430 		vip.dport = 0;
431 		vip_info = bpf_map_lookup_elem(&vip_map, &vip);
432 		if (!vip_info)
433 			return TC_ACT_SHOT;
434 		pckt.port16[1] = 0;
435 	}
436 
437 	if (vip_info->flags & F_HASH_NO_SRC_PORT)
438 		pckt.port16[0] = 0;
439 
440 	if (!get_packet_dst(&dst, &pckt, vip_info, is_ipv6))
441 		return TC_ACT_SHOT;
442 
443 	if (dst->flags & F_IPV6) {
444 		cval = bpf_map_lookup_elem(&ctl_array, &v6_intf_pos);
445 		if (!cval)
446 			return TC_ACT_SHOT;
447 		ifindex = cval->ifindex;
448 		memcpy(tkey.remote_ipv6, dst->dstv6, 16);
449 		tun_flag = BPF_F_TUNINFO_IPV6;
450 	} else {
451 		cval = bpf_map_lookup_elem(&ctl_array, &v4_intf_pos);
452 		if (!cval)
453 			return TC_ACT_SHOT;
454 		ifindex = cval->ifindex;
455 		tkey.remote_ipv4 = dst->dst;
456 	}
457 	vip_num = vip_info->vip_num;
458 	data_stats = bpf_map_lookup_elem(&stats, &vip_num);
459 	if (!data_stats)
460 		return TC_ACT_SHOT;
461 	data_stats->pkts++;
462 	data_stats->bytes += pkt_bytes;
463 	bpf_skb_set_tunnel_key(skb, &tkey, sizeof(tkey), tun_flag);
464 	*(u32 *)eth->eth_dest = tkey.remote_ipv4;
465 	return bpf_redirect(ifindex, 0);
466 }
467 
468 SEC("l4lb-demo")
469 int balancer_ingress(struct __sk_buff *ctx)
470 {
471 	void *data_end = (void *)(long)ctx->data_end;
472 	void *data = (void *)(long)ctx->data;
473 	struct eth_hdr *eth = data;
474 	__u32 eth_proto;
475 	__u32 nh_off;
476 
477 	nh_off = sizeof(struct eth_hdr);
478 	if (data + nh_off > data_end)
479 		return TC_ACT_SHOT;
480 	eth_proto = eth->eth_proto;
481 	if (eth_proto == bpf_htons(ETH_P_IP))
482 		return process_packet(data, nh_off, data_end, false, ctx);
483 	else if (eth_proto == bpf_htons(ETH_P_IPV6))
484 		return process_packet(data, nh_off, data_end, true, ctx);
485 	else
486 		return TC_ACT_SHOT;
487 }
488 char _license[] SEC("license") = "GPL";
489