123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833 |
- // SPDX-License-Identifier: GPL-2.0
- // Copyright (c) 2017 Facebook
- #include <stddef.h>
- #include <stdbool.h>
- #include <string.h>
- #include <linux/pkt_cls.h>
- #include <linux/bpf.h>
- #include <linux/in.h>
- #include <linux/if_ether.h>
- #include <linux/ip.h>
- #include <linux/ipv6.h>
- #include <linux/icmp.h>
- #include <linux/icmpv6.h>
- #include <linux/tcp.h>
- #include <linux/udp.h>
- #include "bpf_helpers.h"
- #define bpf_printk(fmt, ...) \
- ({ \
- char ____fmt[] = fmt; \
- bpf_trace_printk(____fmt, sizeof(____fmt), \
- ##__VA_ARGS__); \
- })
- static __u32 rol32(__u32 word, unsigned int shift)
- {
- return (word << shift) | (word >> ((-shift) & 31));
- }
- /* copy paste of jhash from kernel sources to make sure llvm
- * can compile it into valid sequence of bpf instructions
- */
- #define __jhash_mix(a, b, c) \
- { \
- a -= c; a ^= rol32(c, 4); c += b; \
- b -= a; b ^= rol32(a, 6); a += c; \
- c -= b; c ^= rol32(b, 8); b += a; \
- a -= c; a ^= rol32(c, 16); c += b; \
- b -= a; b ^= rol32(a, 19); a += c; \
- c -= b; c ^= rol32(b, 4); b += a; \
- }
- #define __jhash_final(a, b, c) \
- { \
- c ^= b; c -= rol32(b, 14); \
- a ^= c; a -= rol32(c, 11); \
- b ^= a; b -= rol32(a, 25); \
- c ^= b; c -= rol32(b, 16); \
- a ^= c; a -= rol32(c, 4); \
- b ^= a; b -= rol32(a, 14); \
- c ^= b; c -= rol32(b, 24); \
- }
- #define JHASH_INITVAL 0xdeadbeef
- typedef unsigned int u32;
- static __attribute__ ((noinline))
- u32 jhash(const void *key, u32 length, u32 initval)
- {
- u32 a, b, c;
- const unsigned char *k = key;
- a = b = c = JHASH_INITVAL + length + initval;
- while (length > 12) {
- a += *(u32 *)(k);
- b += *(u32 *)(k + 4);
- c += *(u32 *)(k + 8);
- __jhash_mix(a, b, c);
- length -= 12;
- k += 12;
- }
- switch (length) {
- case 12: c += (u32)k[11]<<24;
- case 11: c += (u32)k[10]<<16;
- case 10: c += (u32)k[9]<<8;
- case 9: c += k[8];
- case 8: b += (u32)k[7]<<24;
- case 7: b += (u32)k[6]<<16;
- case 6: b += (u32)k[5]<<8;
- case 5: b += k[4];
- case 4: a += (u32)k[3]<<24;
- case 3: a += (u32)k[2]<<16;
- case 2: a += (u32)k[1]<<8;
- case 1: a += k[0];
- __jhash_final(a, b, c);
- case 0: /* Nothing left to add */
- break;
- }
- return c;
- }
- static __attribute__ ((noinline))
- u32 __jhash_nwords(u32 a, u32 b, u32 c, u32 initval)
- {
- a += initval;
- b += initval;
- c += initval;
- __jhash_final(a, b, c);
- return c;
- }
- static __attribute__ ((noinline))
- u32 jhash_2words(u32 a, u32 b, u32 initval)
- {
- return __jhash_nwords(a, b, 0, initval + JHASH_INITVAL + (2 << 2));
- }
- struct flow_key {
- union {
- __be32 src;
- __be32 srcv6[4];
- };
- union {
- __be32 dst;
- __be32 dstv6[4];
- };
- union {
- __u32 ports;
- __u16 port16[2];
- };
- __u8 proto;
- };
- struct packet_description {
- struct flow_key flow;
- __u8 flags;
- };
- struct ctl_value {
- union {
- __u64 value;
- __u32 ifindex;
- __u8 mac[6];
- };
- };
- struct vip_definition {
- union {
- __be32 vip;
- __be32 vipv6[4];
- };
- __u16 port;
- __u16 family;
- __u8 proto;
- };
- struct vip_meta {
- __u32 flags;
- __u32 vip_num;
- };
- struct real_pos_lru {
- __u32 pos;
- __u64 atime;
- };
- struct real_definition {
- union {
- __be32 dst;
- __be32 dstv6[4];
- };
- __u8 flags;
- };
- struct lb_stats {
- __u64 v2;
- __u64 v1;
- };
- struct bpf_map_def __attribute__ ((section("maps"), used)) vip_map = {
- .type = BPF_MAP_TYPE_HASH,
- .key_size = sizeof(struct vip_definition),
- .value_size = sizeof(struct vip_meta),
- .max_entries = 512,
- .map_flags = 0,
- };
- struct bpf_map_def __attribute__ ((section("maps"), used)) lru_cache = {
- .type = BPF_MAP_TYPE_LRU_HASH,
- .key_size = sizeof(struct flow_key),
- .value_size = sizeof(struct real_pos_lru),
- .max_entries = 300,
- .map_flags = 1U << 1,
- };
- struct bpf_map_def __attribute__ ((section("maps"), used)) ch_rings = {
- .type = BPF_MAP_TYPE_ARRAY,
- .key_size = sizeof(__u32),
- .value_size = sizeof(__u32),
- .max_entries = 12 * 655,
- .map_flags = 0,
- };
- struct bpf_map_def __attribute__ ((section("maps"), used)) reals = {
- .type = BPF_MAP_TYPE_ARRAY,
- .key_size = sizeof(__u32),
- .value_size = sizeof(struct real_definition),
- .max_entries = 40,
- .map_flags = 0,
- };
- struct bpf_map_def __attribute__ ((section("maps"), used)) stats = {
- .type = BPF_MAP_TYPE_PERCPU_ARRAY,
- .key_size = sizeof(__u32),
- .value_size = sizeof(struct lb_stats),
- .max_entries = 515,
- .map_flags = 0,
- };
- struct bpf_map_def __attribute__ ((section("maps"), used)) ctl_array = {
- .type = BPF_MAP_TYPE_ARRAY,
- .key_size = sizeof(__u32),
- .value_size = sizeof(struct ctl_value),
- .max_entries = 16,
- .map_flags = 0,
- };
- struct eth_hdr {
- unsigned char eth_dest[6];
- unsigned char eth_source[6];
- unsigned short eth_proto;
- };
- static inline __u64 calc_offset(bool is_ipv6, bool is_icmp)
- {
- __u64 off = sizeof(struct eth_hdr);
- if (is_ipv6) {
- off += sizeof(struct ipv6hdr);
- if (is_icmp)
- off += sizeof(struct icmp6hdr) + sizeof(struct ipv6hdr);
- } else {
- off += sizeof(struct iphdr);
- if (is_icmp)
- off += sizeof(struct icmphdr) + sizeof(struct iphdr);
- }
- return off;
- }
- static __attribute__ ((noinline))
- bool parse_udp(void *data, void *data_end,
- bool is_ipv6, struct packet_description *pckt)
- {
- bool is_icmp = !((pckt->flags & (1 << 0)) == 0);
- __u64 off = calc_offset(is_ipv6, is_icmp);
- struct udphdr *udp;
- udp = data + off;
- if (udp + 1 > data_end)
- return 0;
- if (!is_icmp) {
- pckt->flow.port16[0] = udp->source;
- pckt->flow.port16[1] = udp->dest;
- } else {
- pckt->flow.port16[0] = udp->dest;
- pckt->flow.port16[1] = udp->source;
- }
- return 1;
- }
- static __attribute__ ((noinline))
- bool parse_tcp(void *data, void *data_end,
- bool is_ipv6, struct packet_description *pckt)
- {
- bool is_icmp = !((pckt->flags & (1 << 0)) == 0);
- __u64 off = calc_offset(is_ipv6, is_icmp);
- struct tcphdr *tcp;
- tcp = data + off;
- if (tcp + 1 > data_end)
- return 0;
- if (tcp->syn)
- pckt->flags |= (1 << 1);
- if (!is_icmp) {
- pckt->flow.port16[0] = tcp->source;
- pckt->flow.port16[1] = tcp->dest;
- } else {
- pckt->flow.port16[0] = tcp->dest;
- pckt->flow.port16[1] = tcp->source;
- }
- return 1;
- }
- static __attribute__ ((noinline))
- bool encap_v6(struct xdp_md *xdp, struct ctl_value *cval,
- struct packet_description *pckt,
- struct real_definition *dst, __u32 pkt_bytes)
- {
- struct eth_hdr *new_eth;
- struct eth_hdr *old_eth;
- struct ipv6hdr *ip6h;
- __u32 ip_suffix;
- void *data_end;
- void *data;
- if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct ipv6hdr)))
- return 0;
- data = (void *)(long)xdp->data;
- data_end = (void *)(long)xdp->data_end;
- new_eth = data;
- ip6h = data + sizeof(struct eth_hdr);
- old_eth = data + sizeof(struct ipv6hdr);
- if (new_eth + 1 > data_end ||
- old_eth + 1 > data_end || ip6h + 1 > data_end)
- return 0;
- memcpy(new_eth->eth_dest, cval->mac, 6);
- memcpy(new_eth->eth_source, old_eth->eth_dest, 6);
- new_eth->eth_proto = 56710;
- ip6h->version = 6;
- ip6h->priority = 0;
- memset(ip6h->flow_lbl, 0, sizeof(ip6h->flow_lbl));
- ip6h->nexthdr = IPPROTO_IPV6;
- ip_suffix = pckt->flow.srcv6[3] ^ pckt->flow.port16[0];
- ip6h->payload_len =
- __builtin_bswap16(pkt_bytes + sizeof(struct ipv6hdr));
- ip6h->hop_limit = 4;
- ip6h->saddr.in6_u.u6_addr32[0] = 1;
- ip6h->saddr.in6_u.u6_addr32[1] = 2;
- ip6h->saddr.in6_u.u6_addr32[2] = 3;
- ip6h->saddr.in6_u.u6_addr32[3] = ip_suffix;
- memcpy(ip6h->daddr.in6_u.u6_addr32, dst->dstv6, 16);
- return 1;
- }
- static __attribute__ ((noinline))
- bool encap_v4(struct xdp_md *xdp, struct ctl_value *cval,
- struct packet_description *pckt,
- struct real_definition *dst, __u32 pkt_bytes)
- {
- __u32 ip_suffix = __builtin_bswap16(pckt->flow.port16[0]);
- struct eth_hdr *new_eth;
- struct eth_hdr *old_eth;
- __u16 *next_iph_u16;
- struct iphdr *iph;
- __u32 csum = 0;
- void *data_end;
- void *data;
- ip_suffix <<= 15;
- ip_suffix ^= pckt->flow.src;
- if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct iphdr)))
- return 0;
- data = (void *)(long)xdp->data;
- data_end = (void *)(long)xdp->data_end;
- new_eth = data;
- iph = data + sizeof(struct eth_hdr);
- old_eth = data + sizeof(struct iphdr);
- if (new_eth + 1 > data_end ||
- old_eth + 1 > data_end || iph + 1 > data_end)
- return 0;
- memcpy(new_eth->eth_dest, cval->mac, 6);
- memcpy(new_eth->eth_source, old_eth->eth_dest, 6);
- new_eth->eth_proto = 8;
- iph->version = 4;
- iph->ihl = 5;
- iph->frag_off = 0;
- iph->protocol = IPPROTO_IPIP;
- iph->check = 0;
- iph->tos = 1;
- iph->tot_len = __builtin_bswap16(pkt_bytes + sizeof(struct iphdr));
- /* don't update iph->daddr, since it will overwrite old eth_proto
- * and multiple iterations of bpf_prog_run() will fail
- */
- iph->saddr = ((0xFFFF0000 & ip_suffix) | 4268) ^ dst->dst;
- iph->ttl = 4;
- next_iph_u16 = (__u16 *) iph;
- #pragma clang loop unroll(full)
- for (int i = 0; i < sizeof(struct iphdr) >> 1; i++)
- csum += *next_iph_u16++;
- iph->check = ~((csum & 0xffff) + (csum >> 16));
- if (bpf_xdp_adjust_head(xdp, (int)sizeof(struct iphdr)))
- return 0;
- return 1;
- }
- static __attribute__ ((noinline))
- bool decap_v6(struct xdp_md *xdp, void **data, void **data_end, bool inner_v4)
- {
- struct eth_hdr *new_eth;
- struct eth_hdr *old_eth;
- old_eth = *data;
- new_eth = *data + sizeof(struct ipv6hdr);
- memcpy(new_eth->eth_source, old_eth->eth_source, 6);
- memcpy(new_eth->eth_dest, old_eth->eth_dest, 6);
- if (inner_v4)
- new_eth->eth_proto = 8;
- else
- new_eth->eth_proto = 56710;
- if (bpf_xdp_adjust_head(xdp, (int)sizeof(struct ipv6hdr)))
- return 0;
- *data = (void *)(long)xdp->data;
- *data_end = (void *)(long)xdp->data_end;
- return 1;
- }
- static __attribute__ ((noinline))
- bool decap_v4(struct xdp_md *xdp, void **data, void **data_end)
- {
- struct eth_hdr *new_eth;
- struct eth_hdr *old_eth;
- old_eth = *data;
- new_eth = *data + sizeof(struct iphdr);
- memcpy(new_eth->eth_source, old_eth->eth_source, 6);
- memcpy(new_eth->eth_dest, old_eth->eth_dest, 6);
- new_eth->eth_proto = 8;
- if (bpf_xdp_adjust_head(xdp, (int)sizeof(struct iphdr)))
- return 0;
- *data = (void *)(long)xdp->data;
- *data_end = (void *)(long)xdp->data_end;
- return 1;
- }
- static __attribute__ ((noinline))
- int swap_mac_and_send(void *data, void *data_end)
- {
- unsigned char tmp_mac[6];
- struct eth_hdr *eth;
- eth = data;
- memcpy(tmp_mac, eth->eth_source, 6);
- memcpy(eth->eth_source, eth->eth_dest, 6);
- memcpy(eth->eth_dest, tmp_mac, 6);
- return XDP_TX;
- }
- static __attribute__ ((noinline))
- int send_icmp_reply(void *data, void *data_end)
- {
- struct icmphdr *icmp_hdr;
- __u16 *next_iph_u16;
- __u32 tmp_addr = 0;
- struct iphdr *iph;
- __u32 csum1 = 0;
- __u32 csum = 0;
- __u64 off = 0;
- if (data + sizeof(struct eth_hdr)
- + sizeof(struct iphdr) + sizeof(struct icmphdr) > data_end)
- return XDP_DROP;
- off += sizeof(struct eth_hdr);
- iph = data + off;
- off += sizeof(struct iphdr);
- icmp_hdr = data + off;
- icmp_hdr->type = 0;
- icmp_hdr->checksum += 0x0007;
- iph->ttl = 4;
- tmp_addr = iph->daddr;
- iph->daddr = iph->saddr;
- iph->saddr = tmp_addr;
- iph->check = 0;
- next_iph_u16 = (__u16 *) iph;
- #pragma clang loop unroll(full)
- for (int i = 0; i < sizeof(struct iphdr) >> 1; i++)
- csum += *next_iph_u16++;
- iph->check = ~((csum & 0xffff) + (csum >> 16));
- return swap_mac_and_send(data, data_end);
- }
- static __attribute__ ((noinline))
- int send_icmp6_reply(void *data, void *data_end)
- {
- struct icmp6hdr *icmp_hdr;
- struct ipv6hdr *ip6h;
- __be32 tmp_addr[4];
- __u64 off = 0;
- if (data + sizeof(struct eth_hdr)
- + sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr) > data_end)
- return XDP_DROP;
- off += sizeof(struct eth_hdr);
- ip6h = data + off;
- off += sizeof(struct ipv6hdr);
- icmp_hdr = data + off;
- icmp_hdr->icmp6_type = 129;
- icmp_hdr->icmp6_cksum -= 0x0001;
- ip6h->hop_limit = 4;
- memcpy(tmp_addr, ip6h->saddr.in6_u.u6_addr32, 16);
- memcpy(ip6h->saddr.in6_u.u6_addr32, ip6h->daddr.in6_u.u6_addr32, 16);
- memcpy(ip6h->daddr.in6_u.u6_addr32, tmp_addr, 16);
- return swap_mac_and_send(data, data_end);
- }
- static __attribute__ ((noinline))
- int parse_icmpv6(void *data, void *data_end, __u64 off,
- struct packet_description *pckt)
- {
- struct icmp6hdr *icmp_hdr;
- struct ipv6hdr *ip6h;
- icmp_hdr = data + off;
- if (icmp_hdr + 1 > data_end)
- return XDP_DROP;
- if (icmp_hdr->icmp6_type == 128)
- return send_icmp6_reply(data, data_end);
- if (icmp_hdr->icmp6_type != 3)
- return XDP_PASS;
- off += sizeof(struct icmp6hdr);
- ip6h = data + off;
- if (ip6h + 1 > data_end)
- return XDP_DROP;
- pckt->flow.proto = ip6h->nexthdr;
- pckt->flags |= (1 << 0);
- memcpy(pckt->flow.srcv6, ip6h->daddr.in6_u.u6_addr32, 16);
- memcpy(pckt->flow.dstv6, ip6h->saddr.in6_u.u6_addr32, 16);
- return -1;
- }
- static __attribute__ ((noinline))
- int parse_icmp(void *data, void *data_end, __u64 off,
- struct packet_description *pckt)
- {
- struct icmphdr *icmp_hdr;
- struct iphdr *iph;
- icmp_hdr = data + off;
- if (icmp_hdr + 1 > data_end)
- return XDP_DROP;
- if (icmp_hdr->type == 8)
- return send_icmp_reply(data, data_end);
- if ((icmp_hdr->type != 3) || (icmp_hdr->code != 4))
- return XDP_PASS;
- off += sizeof(struct icmphdr);
- iph = data + off;
- if (iph + 1 > data_end)
- return XDP_DROP;
- if (iph->ihl != 5)
- return XDP_DROP;
- pckt->flow.proto = iph->protocol;
- pckt->flags |= (1 << 0);
- pckt->flow.src = iph->daddr;
- pckt->flow.dst = iph->saddr;
- return -1;
- }
- static __attribute__ ((noinline))
- __u32 get_packet_hash(struct packet_description *pckt,
- bool hash_16bytes)
- {
- if (hash_16bytes)
- return jhash_2words(jhash(pckt->flow.srcv6, 16, 12),
- pckt->flow.ports, 24);
- else
- return jhash_2words(pckt->flow.src, pckt->flow.ports,
- 24);
- }
- __attribute__ ((noinline))
- static bool get_packet_dst(struct real_definition **real,
- struct packet_description *pckt,
- struct vip_meta *vip_info,
- bool is_ipv6, void *lru_map)
- {
- struct real_pos_lru new_dst_lru = { };
- bool hash_16bytes = is_ipv6;
- __u32 *real_pos, hash, key;
- __u64 cur_time;
- if (vip_info->flags & (1 << 2))
- hash_16bytes = 1;
- if (vip_info->flags & (1 << 3)) {
- pckt->flow.port16[0] = pckt->flow.port16[1];
- memset(pckt->flow.srcv6, 0, 16);
- }
- hash = get_packet_hash(pckt, hash_16bytes);
- if (hash != 0x358459b7 /* jhash of ipv4 packet */ &&
- hash != 0x2f4bc6bb /* jhash of ipv6 packet */)
- return 0;
- key = 2 * vip_info->vip_num + hash % 2;
- real_pos = bpf_map_lookup_elem(&ch_rings, &key);
- if (!real_pos)
- return 0;
- key = *real_pos;
- *real = bpf_map_lookup_elem(&reals, &key);
- if (!(*real))
- return 0;
- if (!(vip_info->flags & (1 << 1))) {
- __u32 conn_rate_key = 512 + 2;
- struct lb_stats *conn_rate_stats =
- bpf_map_lookup_elem(&stats, &conn_rate_key);
- if (!conn_rate_stats)
- return 1;
- cur_time = bpf_ktime_get_ns();
- if ((cur_time - conn_rate_stats->v2) >> 32 > 0xffFFFF) {
- conn_rate_stats->v1 = 1;
- conn_rate_stats->v2 = cur_time;
- } else {
- conn_rate_stats->v1 += 1;
- if (conn_rate_stats->v1 >= 1)
- return 1;
- }
- if (pckt->flow.proto == IPPROTO_UDP)
- new_dst_lru.atime = cur_time;
- new_dst_lru.pos = key;
- bpf_map_update_elem(lru_map, &pckt->flow, &new_dst_lru, 0);
- }
- return 1;
- }
- __attribute__ ((noinline))
- static void connection_table_lookup(struct real_definition **real,
- struct packet_description *pckt,
- void *lru_map)
- {
- struct real_pos_lru *dst_lru;
- __u64 cur_time;
- __u32 key;
- dst_lru = bpf_map_lookup_elem(lru_map, &pckt->flow);
- if (!dst_lru)
- return;
- if (pckt->flow.proto == IPPROTO_UDP) {
- cur_time = bpf_ktime_get_ns();
- if (cur_time - dst_lru->atime > 300000)
- return;
- dst_lru->atime = cur_time;
- }
- key = dst_lru->pos;
- *real = bpf_map_lookup_elem(&reals, &key);
- }
- /* don't believe your eyes!
- * below function has 6 arguments whereas bpf and llvm allow maximum of 5
- * but since it's _static_ llvm can optimize one argument away
- */
- __attribute__ ((noinline))
- static int process_l3_headers_v6(struct packet_description *pckt,
- __u8 *protocol, __u64 off,
- __u16 *pkt_bytes, void *data,
- void *data_end)
- {
- struct ipv6hdr *ip6h;
- __u64 iph_len;
- int action;
- ip6h = data + off;
- if (ip6h + 1 > data_end)
- return XDP_DROP;
- iph_len = sizeof(struct ipv6hdr);
- *protocol = ip6h->nexthdr;
- pckt->flow.proto = *protocol;
- *pkt_bytes = __builtin_bswap16(ip6h->payload_len);
- off += iph_len;
- if (*protocol == 45) {
- return XDP_DROP;
- } else if (*protocol == 59) {
- action = parse_icmpv6(data, data_end, off, pckt);
- if (action >= 0)
- return action;
- } else {
- memcpy(pckt->flow.srcv6, ip6h->saddr.in6_u.u6_addr32, 16);
- memcpy(pckt->flow.dstv6, ip6h->daddr.in6_u.u6_addr32, 16);
- }
- return -1;
- }
- __attribute__ ((noinline))
- static int process_l3_headers_v4(struct packet_description *pckt,
- __u8 *protocol, __u64 off,
- __u16 *pkt_bytes, void *data,
- void *data_end)
- {
- struct iphdr *iph;
- __u64 iph_len;
- int action;
- iph = data + off;
- if (iph + 1 > data_end)
- return XDP_DROP;
- if (iph->ihl != 5)
- return XDP_DROP;
- *protocol = iph->protocol;
- pckt->flow.proto = *protocol;
- *pkt_bytes = __builtin_bswap16(iph->tot_len);
- off += 20;
- if (iph->frag_off & 65343)
- return XDP_DROP;
- if (*protocol == IPPROTO_ICMP) {
- action = parse_icmp(data, data_end, off, pckt);
- if (action >= 0)
- return action;
- } else {
- pckt->flow.src = iph->saddr;
- pckt->flow.dst = iph->daddr;
- }
- return -1;
- }
- __attribute__ ((noinline))
- static int process_packet(void *data, __u64 off, void *data_end,
- bool is_ipv6, struct xdp_md *xdp)
- {
- struct real_definition *dst = NULL;
- struct packet_description pckt = { };
- struct vip_definition vip = { };
- struct lb_stats *data_stats;
- struct eth_hdr *eth = data;
- void *lru_map = &lru_cache;
- struct vip_meta *vip_info;
- __u32 lru_stats_key = 513;
- __u32 mac_addr_pos = 0;
- __u32 stats_key = 512;
- struct ctl_value *cval;
- __u16 pkt_bytes;
- __u64 iph_len;
- __u8 protocol;
- __u32 vip_num;
- int action;
- if (is_ipv6)
- action = process_l3_headers_v6(&pckt, &protocol, off,
- &pkt_bytes, data, data_end);
- else
- action = process_l3_headers_v4(&pckt, &protocol, off,
- &pkt_bytes, data, data_end);
- if (action >= 0)
- return action;
- protocol = pckt.flow.proto;
- if (protocol == IPPROTO_TCP) {
- if (!parse_tcp(data, data_end, is_ipv6, &pckt))
- return XDP_DROP;
- } else if (protocol == IPPROTO_UDP) {
- if (!parse_udp(data, data_end, is_ipv6, &pckt))
- return XDP_DROP;
- } else {
- return XDP_TX;
- }
- if (is_ipv6)
- memcpy(vip.vipv6, pckt.flow.dstv6, 16);
- else
- vip.vip = pckt.flow.dst;
- vip.port = pckt.flow.port16[1];
- vip.proto = pckt.flow.proto;
- vip_info = bpf_map_lookup_elem(&vip_map, &vip);
- if (!vip_info) {
- vip.port = 0;
- vip_info = bpf_map_lookup_elem(&vip_map, &vip);
- if (!vip_info)
- return XDP_PASS;
- if (!(vip_info->flags & (1 << 4)))
- pckt.flow.port16[1] = 0;
- }
- if (data_end - data > 1400)
- return XDP_DROP;
- data_stats = bpf_map_lookup_elem(&stats, &stats_key);
- if (!data_stats)
- return XDP_DROP;
- data_stats->v1 += 1;
- if (!dst) {
- if (vip_info->flags & (1 << 0))
- pckt.flow.port16[0] = 0;
- if (!(pckt.flags & (1 << 1)) && !(vip_info->flags & (1 << 1)))
- connection_table_lookup(&dst, &pckt, lru_map);
- if (dst)
- goto out;
- if (pckt.flow.proto == IPPROTO_TCP) {
- struct lb_stats *lru_stats =
- bpf_map_lookup_elem(&stats, &lru_stats_key);
- if (!lru_stats)
- return XDP_DROP;
- if (pckt.flags & (1 << 1))
- lru_stats->v1 += 1;
- else
- lru_stats->v2 += 1;
- }
- if (!get_packet_dst(&dst, &pckt, vip_info, is_ipv6, lru_map))
- return XDP_DROP;
- data_stats->v2 += 1;
- }
- out:
- cval = bpf_map_lookup_elem(&ctl_array, &mac_addr_pos);
- if (!cval)
- return XDP_DROP;
- if (dst->flags & (1 << 0)) {
- if (!encap_v6(xdp, cval, &pckt, dst, pkt_bytes))
- return XDP_DROP;
- } else {
- if (!encap_v4(xdp, cval, &pckt, dst, pkt_bytes))
- return XDP_DROP;
- }
- vip_num = vip_info->vip_num;
- data_stats = bpf_map_lookup_elem(&stats, &vip_num);
- if (!data_stats)
- return XDP_DROP;
- data_stats->v1 += 1;
- data_stats->v2 += pkt_bytes;
- data = (void *)(long)xdp->data;
- data_end = (void *)(long)xdp->data_end;
- if (data + 4 > data_end)
- return XDP_DROP;
- *(u32 *)data = dst->dst;
- return XDP_DROP;
- }
- __attribute__ ((section("xdp-test"), used))
- int balancer_ingress(struct xdp_md *ctx)
- {
- void *data = (void *)(long)ctx->data;
- void *data_end = (void *)(long)ctx->data_end;
- struct eth_hdr *eth = data;
- __u32 eth_proto;
- __u32 nh_off;
- nh_off = sizeof(struct eth_hdr);
- if (data + nh_off > data_end)
- return XDP_DROP;
- eth_proto = eth->eth_proto;
- if (eth_proto == 8)
- return process_packet(data, nh_off, data_end, 0, ctx);
- else if (eth_proto == 56710)
- return process_packet(data, nh_off, data_end, 1, ctx);
- else
- return XDP_DROP;
- }
- char _license[] __attribute__ ((section("license"), used)) = "GPL";
- int _version __attribute__ ((section("version"), used)) = 1;
|