forked from Netronome/bpf-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
l4lb_xdp.c
221 lines (184 loc) · 5.1 KB
/
l4lb_xdp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
// Copyright (c) 2018 Netronome Systems, Inc.
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <linux/bpf.h>
#include <linux/icmp.h>
#include <linux/if_ether.h>
#include <linux/if_vlan.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include "bpf_endian.h"
#include "bpf_helpers.h"
#include "jhash.h"
#define MAX_SERVERS 512
/* 0x3FFF mask to check for fragment offset field */
#define IP_FRAGMENTED 65343
struct pkt_meta {
__be32 src;
__be32 dst;
union {
__u32 ports;
__u16 port16[2];
};
};
struct dest_info {
__u32 saddr;
__u32 daddr;
__u64 bytes;
__u64 pkts;
__u8 dmac[6];
};
struct bpf_map_def SEC("maps") servers = {
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(__u32),
.value_size = sizeof(struct dest_info),
.max_entries = MAX_SERVERS,
};
static __always_inline struct dest_info *hash_get_dest(struct pkt_meta *pkt)
{
__u32 key;
struct dest_info *tnl;
/* hash packet source ip with both ports to obtain a destination */
key = jhash_2words(pkt->src, pkt->ports, MAX_SERVERS) % MAX_SERVERS;
/* get destination's network details from map */
tnl = bpf_map_lookup_elem(&servers, &key);
if (!tnl) {
/* if entry does not exist, fallback to key 0 */
key = 0;
tnl = bpf_map_lookup_elem(&servers, &key);
}
return tnl;
}
static __always_inline bool parse_udp(void *data, __u64 off, void *data_end,
struct pkt_meta *pkt)
{
struct udphdr *udp;
udp = data + off;
if (udp + 1 > data_end)
return false;
pkt->port16[0] = udp->source;
pkt->port16[1] = udp->dest;
return true;
}
static __always_inline bool parse_tcp(void *data, __u64 off, void *data_end,
struct pkt_meta *pkt)
{
struct tcphdr *tcp;
tcp = data + off;
if (tcp + 1 > data_end)
return false;
pkt->port16[0] = tcp->source;
pkt->port16[1] = tcp->dest;
return true;
}
static __always_inline void set_ethhdr(struct ethhdr *new_eth,
const struct ethhdr *old_eth,
const struct dest_info *tnl,
__be16 h_proto)
{
memcpy(new_eth->h_source, old_eth->h_dest, sizeof(new_eth->h_source));
memcpy(new_eth->h_dest, tnl->dmac, sizeof(new_eth->h_dest));
new_eth->h_proto = h_proto;
}
static __always_inline int process_packet(struct xdp_md *ctx, __u64 off)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct pkt_meta pkt = {};
struct ethhdr *new_eth;
struct ethhdr *old_eth;
struct dest_info *tnl;
struct iphdr iph_tnl;
struct iphdr *iph;
__u16 *next_iph_u16;
__u16 pkt_size;
__u16 payload_len;
__u8 protocol;
u32 csum = 0;
iph = data + off;
if (iph + 1 > data_end)
return XDP_DROP;
if (iph->ihl != 5)
return XDP_DROP;
protocol = iph->protocol;
payload_len = bpf_ntohs(iph->tot_len);
off += sizeof(struct iphdr);
/* do not support fragmented packets as L4 headers may be missing */
if (iph->frag_off & IP_FRAGMENTED)
return XDP_DROP;
pkt.src = iph->saddr;
pkt.dst = iph->daddr;
/* obtain port numbers for UDP and TCP traffic */
if (protocol == IPPROTO_TCP) {
if (!parse_tcp(data, off, data_end, &pkt))
return XDP_DROP;
} else if (protocol == IPPROTO_UDP) {
if (!parse_udp(data, off, data_end, &pkt))
return XDP_DROP;
} else {
return XDP_PASS;
}
/* allocate a destination using packet hash and map lookup */
tnl = hash_get_dest(&pkt);
if (!tnl)
return XDP_DROP;
/* extend the packet for ip header encapsulation */
if (bpf_xdp_adjust_head(ctx, 0 - (int)sizeof(struct iphdr)))
return XDP_DROP;
data = (void *)(long)ctx->data;
data_end = (void *)(long)ctx->data_end;
/* relocate ethernet header to start of packet and set MACs */
new_eth = data;
old_eth = data + sizeof(*iph);
if (new_eth + 1 > data_end || old_eth + 1 > data_end ||
iph + 1 > data_end)
return XDP_DROP;
set_ethhdr(new_eth, old_eth, tnl, bpf_htons(ETH_P_IP));
/* create an additional ip header for encapsulation */
iph_tnl.version = 4;
iph_tnl.ihl = sizeof(*iph) >> 2;
iph_tnl.frag_off = 0;
iph_tnl.protocol = IPPROTO_IPIP;
iph_tnl.check = 0;
iph_tnl.id = 0;
iph_tnl.tos = 0;
iph_tnl.tot_len = bpf_htons(payload_len + sizeof(*iph));
iph_tnl.daddr = tnl->daddr;
iph_tnl.saddr = tnl->saddr;
iph_tnl.ttl = 8;
/* calculate ip header checksum */
next_iph_u16 = (__u16 *)&iph_tnl;
#pragma clang loop unroll(full)
for (int i = 0; i < (int)sizeof(*iph) >> 1; i++)
csum += *next_iph_u16++;
iph_tnl.check = ~((csum & 0xffff) + (csum >> 16));
iph = data + sizeof(*new_eth);
*iph = iph_tnl;
/* increment map counters */
pkt_size = (__u16)(data_end - data); /* payload size excl L2 crc */
__sync_fetch_and_add(&tnl->pkts, 1);
__sync_fetch_and_add(&tnl->bytes, pkt_size);
return XDP_TX;
}
SEC("xdp")
int loadbal(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct ethhdr *eth = data;
__u32 eth_proto;
__u32 nh_off;
nh_off = sizeof(struct ethhdr);
if (data + nh_off > data_end)
return XDP_DROP;
eth_proto = eth->h_proto;
/* demo program only accepts ipv4 packets */
if (eth_proto == bpf_htons(ETH_P_IP))
return process_packet(ctx, nh_off);
else
return XDP_PASS;
}