test_l4lb.c 11 KB

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