test_l4lb_noinline.c 11 KB

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