-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathrss_kern.c
275 lines (233 loc) · 5.82 KB
/
rss_kern.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
// Copyright (c) 2018 Netronome Systems, Inc.
#include <stdbool.h>
#include <stddef.h>
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/string.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include "bpf_endian.h"
#include "bpf_helpers.h"
#include "jhash.h"
#include "rss_common.h"
struct pkt_meta {
__be32 src;
__be32 dst;
__be32 srcv6[4];
__be32 dstv6[4];
union {
__u32 ports;
__u16 port16[2];
};
__u8 protocol;
};
struct bpf_map_def SEC("maps") ctrl_map = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(__u32),
.value_size = sizeof(struct queue_select_ctrl),
.max_entries = 1,
};
struct bpf_map_def SEC("maps") indirect_map = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(__u32),
.value_size = sizeof(struct indirect_queue),
.max_entries = MAX_RSS_QUEUES,
};
static __always_inline __u32 hash_tuples(struct pkt_meta *pkt, bool is_ip6)
{
__u32 a;
__u32 b;
/* hash packet ip and ports to obtain a key for rss indirection tbl */
if (is_ip6) {
a = jhash2(pkt->srcv6, 4, 0xc55);
b = jhash2(pkt->dstv6, 4, 0x1234);
return jhash_3words(a, b, pkt->ports, 0xeb0f);
} else {
return jhash_3words(pkt->src, pkt->dst, pkt->ports, 0xeb0f);
}
}
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 bool parse_ip4(void *data, __u64 off, void *data_end,
struct pkt_meta *pkt)
{
struct iphdr *iph;
iph = data + off;
if (iph + 1 > data_end)
return false;
if (iph->ihl != 5)
return false;
pkt->src = iph->saddr;
pkt->dst = iph->daddr;
pkt->protocol = iph->protocol;
return true;
}
static __always_inline bool parse_ip6(void *data, __u64 off, void *data_end,
struct pkt_meta *pkt)
{
struct ipv6hdr *ip6h;
ip6h = data + off;
if (ip6h + 1 > data_end)
return false;
memcpy(pkt->srcv6, ip6h->saddr.s6_addr32, 16);
memcpy(pkt->dstv6, ip6h->daddr.s6_addr32, 16);
pkt->protocol = ip6h->nexthdr;
return true;
}
static __always_inline void sort_tuple(struct pkt_meta *pkt, bool is_ip6)
{
__be32 temp_ipv6[4];
__be32 temp_ip;
__u16 temp_port;
__u64 tot_dst;
__u64 tot_src;
/* sort tuple to ensure consistency for both flow directions */
if (is_ip6) {
tot_src = pkt->srcv6[0] + pkt->srcv6[1]
+ pkt->srcv6[2] + pkt->srcv6[3];
tot_dst = pkt->dstv6[0] + pkt->dstv6[1]
+ pkt->dstv6[2] + pkt->dstv6[3];
if (tot_src < tot_dst) {
memcpy(temp_ipv6, pkt->srcv6, 16);
memcpy(pkt->srcv6, pkt->dstv6, 16);
memcpy(pkt->dstv6, temp_ipv6, 16);
}
} else {
if (pkt->src < pkt->dst) {
temp_ip = pkt->src;
pkt->src = pkt->dst;
pkt->dst = temp_ip;
}
}
if (pkt->port16[0] < pkt->port16[1]) {
temp_port = pkt->port16[0];
pkt->port16[0] = pkt->port16[1];
pkt->port16[1] = temp_port;
}
}
SEC("xdp")
int process_packet(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct queue_select_ctrl *queue_ctrl;
struct indirect_queue *dest_q;
struct ethhdr *eth = data;
struct pkt_meta pkt = {};
bool symmetric = false;
bool use_encap = false;
bool is_ip6 = false;
bool jhash = false;
__u32 eth_proto;
__u32 hash;
__u32 key;
__u32 off;
/* determine hashing mode using map lookup */
key = 0;
queue_ctrl = bpf_map_lookup_elem(&ctrl_map, &key);
if (!queue_ctrl)
return XDP_PASS;
switch (queue_ctrl->select_mode) {
case QUEUE_STATIC:
jhash = false;
key = queue_ctrl->queue_static;
break;
case QUEUE_HASH:
jhash = true;
break;
case QUEUE_SYMMETRIC:
jhash = true;
symmetric = true;
break;
case QUEUE_ENCAP:
jhash = true;
use_encap = true;
break;
default:
return XDP_PASS;
}
/* parse packet for IP Addresses and Ports */
off = sizeof(struct ethhdr);
if (data + off > data_end)
return XDP_PASS;
eth_proto = eth->h_proto;
if (eth_proto == bpf_htons(ETH_P_IP)) {
if (!parse_ip4(data, off, data_end, &pkt))
return XDP_PASS;
off += sizeof(struct iphdr);
} else if (eth_proto == bpf_htons(ETH_P_IPV6)) {
if (!parse_ip6(data, off, data_end, &pkt))
return XDP_PASS;
is_ip6 = true;
off += sizeof(struct ipv6hdr);
} else {
return XDP_PASS;
}
/* if IPinIP packet allow for second IP header */
if (pkt.protocol == IPPROTO_IPIP) {
if (use_encap) {
if (!parse_ip4(data, off, data_end, &pkt))
return XDP_PASS;
is_ip6 = false;
}
off += sizeof(struct iphdr);
} else if (pkt.protocol == IPPROTO_IPV6) {
if (use_encap) {
if (!parse_ip6(data, off, data_end, &pkt))
return XDP_PASS;
is_ip6 = true;
}
off += sizeof(struct ipv6hdr);
}
if (data + off > data_end)
return XDP_PASS;
/* obtain port numbers for UDP and TCP traffic */
if (pkt.protocol == IPPROTO_TCP) {
if (!parse_tcp(data, off, data_end, &pkt))
return XDP_PASS;
} else if (pkt.protocol == IPPROTO_UDP) {
if (!parse_udp(data, off, data_end, &pkt))
return XDP_PASS;
} else {
pkt.ports = 0;
}
if (symmetric)
sort_tuple(&pkt, is_ip6);
if (jhash) {
/* set map lookup key using 4 tuple hash */
hash = hash_tuples(&pkt, is_ip6);
key = hash % MAX_RSS_QUEUES;
}
/* HW cannot perform modulus on varying values so use indirection tbl */
dest_q = bpf_map_lookup_elem(&indirect_map, &key);
if (!dest_q)
return XDP_PASS;
ctx->rx_queue_index = dest_q->queue_num;
/* increment queue packet counter */
__sync_fetch_and_add(&dest_q->packet_cnt, 1);
return XDP_PASS;
}