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