|
| 1 | + |
| 2 | +/** |
| 3 | + * https://github.com/y805939188/simple-k8s-cni/blob/master/plugins/vxlan/ebpf/vxlan_ingress.c |
| 4 | + */ |
| 5 | + |
| 6 | + |
| 7 | +// /root/linux-5.10.142/include/uapi/linux/bpf.h |
| 8 | +#include <linux/bpf.h> |
| 9 | +#include <linux/if_ether.h> |
| 10 | +#include <linux/if_packet.h> |
| 11 | +#include <linux/ip.h> |
| 12 | +#include <linux/pkt_cls.h> |
| 13 | + |
| 14 | +// /root/linux-5.10.142/tools/lib/bpf/bpf_helpers.h |
| 15 | +#include <bpf/bpf_helpers.h> |
| 16 | +#include <bpf/bpf_endian.h> |
| 17 | + |
| 18 | +#ifndef __section |
| 19 | +#define __section(X) __attribute__((section(X), used)) |
| 20 | +#endif |
| 21 | +#ifndef __section_maps |
| 22 | +#define __section_maps __section("maps") |
| 23 | +#endif |
| 24 | + |
| 25 | +#define PIN_GLOBAL_NS 2 |
| 26 | +#define DEFAULT_TUNNEL_ID 13190 |
| 27 | +#define LOCAL_DEV_VXLAN 1; |
| 28 | +#define LOCAL_DEV_VETH 2; |
| 29 | + |
| 30 | +struct bpf_elf_map { |
| 31 | + __u32 type; |
| 32 | + __u32 size_key; |
| 33 | + __u32 size_value; |
| 34 | + __u32 max_elem; |
| 35 | + __u32 flags; |
| 36 | + __u32 id; |
| 37 | + __u32 pinning; |
| 38 | + __u32 inner_id; |
| 39 | + __u32 inner_idx; |
| 40 | +}; |
| 41 | + |
| 42 | +struct endpointKey { |
| 43 | + __u32 ip; |
| 44 | +}; |
| 45 | +struct endpointInfo { |
| 46 | + __u32 ifIndex; |
| 47 | + __u32 lxcIfIndex; |
| 48 | + __u8 mac[8]; |
| 49 | + __u8 nodeMac[8]; |
| 50 | +}; |
| 51 | + |
| 52 | +struct bpf_elf_map __section_maps ding_lxc = { |
| 53 | + .type = BPF_MAP_TYPE_HASH, |
| 54 | + .size_key = sizeof(struct endpointKey), |
| 55 | + .size_value = sizeof(struct endpointInfo), |
| 56 | + .pinning = PIN_GLOBAL_NS, // 设置 LIBBPF_PIN_BY_NAME 导致 pin path 为 /sys/fs/bpf/tc/424e22c8e74276a6484f398886d426f441d9b849/ |
| 57 | + .max_elem = 256, |
| 58 | +}; |
| 59 | + |
| 60 | +struct podNodeKey { |
| 61 | + __u32 ip; |
| 62 | +}; |
| 63 | + |
| 64 | +struct podNodeValue { |
| 65 | + __u32 ip; |
| 66 | +}; |
| 67 | + |
| 68 | +struct bpf_elf_map __section_maps ding_ip = { |
| 69 | + .type = BPF_MAP_TYPE_HASH, |
| 70 | + .size_key = sizeof(struct podNodeKey), |
| 71 | + .size_value = sizeof(struct podNodeValue), |
| 72 | + .pinning = PIN_GLOBAL_NS, // 设置 LIBBPF_PIN_BY_NAME 导致 pin path 为 /sys/fs/bpf/tc/424e22c8e74276a6484f398886d426f441d9b849/ |
| 73 | + .max_elem = 256, |
| 74 | +}; |
| 75 | + |
| 76 | +struct localNodeMapKey { |
| 77 | + __u32 type; |
| 78 | +}; |
| 79 | +struct localNodeMapValue { |
| 80 | + __u32 ifIndex; |
| 81 | +}; |
| 82 | +struct bpf_elf_map __section_maps ding_local = { |
| 83 | + .type = BPF_MAP_TYPE_HASH, |
| 84 | + .size_key = sizeof(struct localNodeMapKey), |
| 85 | + .size_value = sizeof(struct localNodeMapValue), |
| 86 | + .pinning = PIN_GLOBAL_NS, // 设置 LIBBPF_PIN_BY_NAME 导致 pin path 为 /sys/fs/bpf/tc/424e22c8e74276a6484f398886d426f441d9b849/ |
| 87 | + .max_elem = 256, |
| 88 | +}; |
| 89 | + |
| 90 | +SEC("veth_pair_ingress") |
| 91 | +int veth_pair_ingress(struct __sk_buff *skb) { |
| 92 | + void *data = (void *)(long)skb->data; |
| 93 | + void *data_end = (void *)(long)skb->data_end; |
| 94 | + if (data + sizeof(struct ethhdr) + sizeof(struct iphdr) > data_end) { |
| 95 | + return TC_ACT_UNSPEC; |
| 96 | + } |
| 97 | + |
| 98 | + struct ethhdr *eth = data; |
| 99 | + struct iphdr *ip = (data + sizeof(struct ethhdr)); |
| 100 | + if (eth->h_proto != bpf_htons(ETH_P_IP)) { |
| 101 | + return TC_ACT_UNSPEC; |
| 102 | + } |
| 103 | + |
| 104 | + // 在 go 那头儿往 ebpf 的 map 里存的时候我这个 arm 是按照小端序存的 |
| 105 | + // 这里给转成网络的大端序 |
| 106 | + __u32 src_ip = bpf_htonl(ip->saddr); |
| 107 | + __u32 dst_ip = bpf_htonl(ip->daddr); |
| 108 | + // 拿到 mac 地址 |
| 109 | + __u8 src_mac[ETH_ALEN]; |
| 110 | + __u8 dst_mac[ETH_ALEN]; |
| 111 | + struct endpointKey epKey = {}; |
| 112 | + epKey.ip = dst_ip; |
| 113 | + // 在 lxc 中查找 |
| 114 | + struct endpointInfo *ep = bpf_map_lookup_elem(&ding_lxc, &epKey); |
| 115 | + if (ep) { |
| 116 | + // 如果能找到说明是要发往本机其他 pod 中的 |
| 117 | + // 把 mac 地址改成目标 pod 的两对儿 veth 的 mac 地址 |
| 118 | + __builtin_memcpy(src_mac, ep->nodeMac, ETH_ALEN); |
| 119 | + __builtin_memcpy(dst_mac, ep->mac, ETH_ALEN); |
| 120 | + bpf_skb_store_bytes(skb, offsetof(struct ethhdr, h_source), dst_mac, ETH_ALEN, 0); |
| 121 | + bpf_skb_store_bytes(skb, offsetof(struct ethhdr, h_dest), src_mac, ETH_ALEN, 0); |
| 122 | + return bpf_redirect_peer(ep->lxcIfIndex, 0); |
| 123 | + } |
| 124 | + |
| 125 | + struct podNodeKey podNodeKey = { |
| 126 | + .ip = dst_ip, |
| 127 | + }; |
| 128 | + struct podNodeValue *podNode = bpf_map_lookup_elem(&ding_ip, &podNodeKey); |
| 129 | + if (podNode) { |
| 130 | + // 进到这里说明该目标 ip 是本集群内的 ip |
| 131 | + struct localNodeMapKey localKey = {}; |
| 132 | + localKey.type = LOCAL_DEV_VXLAN; |
| 133 | + struct localNodeMapValue *localValue = bpf_map_lookup_elem(&ding_local, &localKey); |
| 134 | + if (localValue) { |
| 135 | + // redirect 到 vxlan egress |
| 136 | + return bpf_redirect(localValue->ifIndex, 0); |
| 137 | + } |
| 138 | + return TC_ACT_UNSPEC; |
| 139 | + } |
| 140 | + |
| 141 | + return TC_ACT_UNSPEC; |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | +SEC("vxlan_ingress") |
| 146 | +int vxlan_ingress(struct __sk_buff *skb) { |
| 147 | + void *data = (void *)(long)skb->data; |
| 148 | + void *data_end = (void *)(long)skb->data_end; |
| 149 | + if (data + sizeof(struct ethhdr) + sizeof(struct iphdr) > data_end) { |
| 150 | + return TC_ACT_UNSPEC; |
| 151 | + } |
| 152 | + |
| 153 | + struct ethhdr *eth = data; |
| 154 | + struct iphdr *ip = (data + sizeof(struct ethhdr)); |
| 155 | + if (eth->h_proto != bpf_htons(ETH_P_IP)) { |
| 156 | + return TC_ACT_UNSPEC; |
| 157 | + } |
| 158 | + |
| 159 | + __u32 src_ip = bpf_htonl(ip->saddr); |
| 160 | + __u32 dst_ip = bpf_htonl(ip->daddr); |
| 161 | + bpf_printk("the dst_ip is: %d", dst_ip); |
| 162 | + bpf_printk("the ip->daddr is: %d", ip->daddr); |
| 163 | + |
| 164 | + struct endpointKey epKey = { |
| 165 | + .ip = dst_ip, |
| 166 | + }; |
| 167 | + struct endpointInfo *ep = bpf_map_lookup_elem(&ding_lxc, &epKey); |
| 168 | + if (!ep) { |
| 169 | + return TC_ACT_OK; |
| 170 | + } |
| 171 | + // 找到的话说明是发往本机 pod 中的流量 |
| 172 | + // 此时需要做 stc mac 和 dst mac 的更新 |
| 173 | + |
| 174 | + // 拿到 mac 地址 |
| 175 | + __u8 src_mac[ETH_ALEN]; |
| 176 | + __u8 dst_mac[ETH_ALEN]; |
| 177 | + // 将 mac 改成本机 pod 的那对儿 veth pair 的 mac |
| 178 | + __builtin_memcpy(src_mac, ep->nodeMac, ETH_ALEN); |
| 179 | + __builtin_memcpy(dst_mac, ep->mac, ETH_ALEN); |
| 180 | + // 将 mac 更新到 skb 中 |
| 181 | + bpf_skb_store_bytes(skb, offsetof(struct ethhdr, h_dest), dst_mac, ETH_ALEN, 0); |
| 182 | + bpf_skb_store_bytes(skb, offsetof(struct ethhdr, h_source), src_mac, ETH_ALEN, 0); |
| 183 | + |
| 184 | + return bpf_redirect(ep->lxcIfIndex, 0); |
| 185 | +} |
| 186 | + |
| 187 | +SEC("vxlan_egress") |
| 188 | +int vxlan_egress(struct __sk_buff *skb) { |
| 189 | + void *data = (void *)(long)skb->data; |
| 190 | + void *data_end = (void *)(long)skb->data_end; |
| 191 | + if (data + sizeof(struct ethhdr) + sizeof(struct iphdr) > data_end) { |
| 192 | + return TC_ACT_UNSPEC; |
| 193 | + } |
| 194 | + |
| 195 | + struct ethhdr *eth = data; |
| 196 | + struct iphdr *ip = (data + sizeof(struct ethhdr)); |
| 197 | + if (eth->h_proto != bpf_htons(ETH_P_IP)) { |
| 198 | + return TC_ACT_UNSPEC; |
| 199 | + } |
| 200 | + |
| 201 | + __u32 src_ip = bpf_htonl(ip->saddr); |
| 202 | + __u32 dst_ip = bpf_htonl(ip->daddr); |
| 203 | + bpf_printk("the dst_ip is: %d", dst_ip); |
| 204 | + bpf_printk("the ip->daddr is: %d", ip->daddr); |
| 205 | + |
| 206 | + // 获取目标 ip 所在的 node ip |
| 207 | + struct podNodeKey podNodeKey = {}; |
| 208 | + podNodeKey.ip = dst_ip; |
| 209 | + struct podNodeValue *podNode = bpf_map_lookup_elem(&ding_ip, &podNodeKey); |
| 210 | + if (podNode) { |
| 211 | + __u32 dst_node_ip = podNode->ip; |
| 212 | + // 准备一个 tunnel |
| 213 | + struct bpf_tunnel_key key; |
| 214 | + int ret; |
| 215 | + __builtin_memset(&key, 0x0, sizeof(key)); |
| 216 | + key.remote_ipv4 = podNode->ip; |
| 217 | + key.tunnel_id = DEFAULT_TUNNEL_ID; |
| 218 | + key.tunnel_tos = 0; |
| 219 | + key.tunnel_ttl = 64; |
| 220 | + // 添加外头的隧道 udp |
| 221 | + ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), BPF_F_ZERO_CSUM_TX); |
| 222 | + if (ret < 0) { |
| 223 | + bpf_printk("bpf_skb_set_tunnel_key failed"); |
| 224 | + return TC_ACT_SHOT; |
| 225 | + } |
| 226 | + return TC_ACT_OK; |
| 227 | + } |
| 228 | + |
| 229 | + return TC_ACT_OK; |
| 230 | +} |
| 231 | + |
| 232 | + |
| 233 | +char _license[] SEC("license") = "GPL"; |
0 commit comments