-
Notifications
You must be signed in to change notification settings - Fork 3
/
flow-leak.c
432 lines (360 loc) · 10.5 KB
/
flow-leak.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* test for rte_flow leaks
* Copyright(c) 2021 Microsoft Corporation
* All rights reserved.
*
* Creates and delete flows
*
* Usage:
* flow-leak EAL_args -- [OPTIONS] MAC...
*/
#include <stdio.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#include <getopt.h>
#include <rte_ethdev.h>
#include <rte_ether.h>
#include <rte_mbuf.h>
#include <rte_flow.h>
#include <rte_lcore.h>
#include <rte_timer.h>
#include <rte_cycles.h>
#include "rte_flow_dump.h"
#include "pkt_dump.h"
static unsigned int num_vnic;
static struct rte_ether_addr *vnic_mac;
static struct rte_flow **src_flows, **dst_flows;
static uint16_t nrxq = 236;
static uint16_t ntxq = 1;
static unsigned int repeat = 10;
#define RX_DESC_DEFAULT 256
#define TX_DESC_DEFAULT 512
#define FLOW_SRC_MODE 1
#define FLOW_DST_MODE 2
static bool flow_dump = false;
/*
* Comment from rte_flow.h on meaning of priorities:
*
* Priorities are set on a per rule based within groups.
*
* Lower values denote higher priority, the highest priority for a flow rule
* is 0, so that a flow that matches for than one rule, the rule with the
* lowest priority value will always be matched.
*
* Although optional, applications are encouraged to group similar rules as
* much as possible to fully take advantage of hardware capabilities
* (e.g. optimized matching) and work around limitations (e.g. a single
* pattern type possibly allowed in a given group). Applications should be
* aware that groups are not linked by default, and that they must be
* explicitly linked by the application using the JUMP action.
*/
#define VNIC_SRC_MAC_PRIORITY 1
#define VNIC_DST_MAC_PRIORITY 65536
static struct rte_mempool *mb_pool;
static struct rte_eth_conf port_conf = {
.rxmode = {
.mq_mode = ETH_MQ_RX_NONE,
.max_rx_pkt_len = RTE_ETHER_MAX_LEN,
.offloads = DEV_RX_OFFLOAD_VLAN_STRIP
| DEV_RX_OFFLOAD_CHECKSUM,
},
.txmode = {
.mq_mode = ETH_MQ_TX_NONE,
.offloads = DEV_TX_OFFLOAD_VLAN_INSERT
| DEV_TX_OFFLOAD_IPV4_CKSUM
| DEV_TX_OFFLOAD_TCP_CKSUM
| DEV_TX_OFFLOAD_UDP_CKSUM,
},
};
static void port_config(uint16_t portid, uint16_t ntxq, uint16_t nrxq)
{
struct rte_eth_dev_info dev_info;
struct rte_eth_txconf txq_conf;
struct rte_eth_rxconf rxq_conf;
uint16_t nb_rxd = RX_DESC_DEFAULT;
uint16_t nb_txd = TX_DESC_DEFAULT;
uint16_t firstq, q;
int r;
r = rte_eth_dev_info_get(portid, &dev_info);
if (r < 0)
rte_exit(EXIT_FAILURE,
"Could not get device information for port %u\n",
portid);
if (ntxq > dev_info.max_tx_queues)
rte_exit(EXIT_FAILURE,
"Not enough transmit queues %u > %u\n",
ntxq, dev_info.max_tx_queues);
if (nrxq > dev_info.max_rx_queues)
rte_exit(EXIT_FAILURE,
"Not enough receive queues %u > %u\n",
nrxq, dev_info.max_rx_queues);
printf("Configure %u Tx and %u Rx queues\n",
ntxq, nrxq);
r = rte_eth_dev_configure(portid, nrxq, ntxq, &port_conf);
if (r < 0)
rte_exit(EXIT_FAILURE,
"Cannot configure device: err=%d port=%u\n",
r, portid);
r = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
if (r < 0)
rte_exit(EXIT_FAILURE,
"Cannot adjust number of descriptors: err=%d, port=%d\n",
r, portid);
txq_conf = dev_info.default_txconf;
rxq_conf = dev_info.default_rxconf;
txq_conf.offloads = port_conf.txmode.offloads;
rxq_conf.offloads = port_conf.rxmode.offloads;
for (q = 0; q < ntxq; q++) {
r = rte_eth_tx_queue_setup(portid, q, nb_txd, 0, &txq_conf);
if (r < 0)
rte_exit(EXIT_FAILURE,
"rte_eth_tx_queue_setup failed %d\n",
r);
}
firstq = 1;
for (q = 0; q < nrxq; q++) {
if (q >= firstq)
rxq_conf.rx_deferred_start = 1;
r = rte_eth_rx_queue_setup(portid, q, nb_rxd, 0, &rxq_conf,
mb_pool);
if (r < 0)
rte_exit(EXIT_FAILURE,
"rte_eth_rx_queue_setup failed %d\n",
r);
}
r = rte_eth_dev_start(portid);
if (r < 0)
rte_exit(EXIT_FAILURE,
"Start failed: err=%d\n", r);
}
/* Match any level mask */
static const struct rte_flow_item_any any_mask = {
.num = UINT32_MAX,
};
/* Match encaped packets */
static const struct rte_flow_item_any inner_flow = {
.num = 4,
};
static struct rte_flow *
flow_src_mac(uint16_t port, uint32_t id,
const struct rte_ether_addr *mac,
const struct rte_flow_action actions[],
struct rte_flow_error *err)
{
struct rte_flow_attr attr = {
.group = id + 1,
.priority = VNIC_SRC_MAC_PRIORITY,
.ingress = 1,
};
static const struct rte_flow_item_eth eth_src_mask = {
.src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
};
struct rte_flow_item_eth vnic_eth_src = {
.src = *mac,
};
struct rte_flow_item patterns[3];
struct rte_flow_item *pat = patterns;
char ebuf[RTE_ETHER_ADDR_FMT_SIZE];
*pat++ = (struct rte_flow_item) {
.type = RTE_FLOW_ITEM_TYPE_ANY,
.spec = &inner_flow,
.mask = &any_mask,
};
*pat++ = (struct rte_flow_item) {
.type = RTE_FLOW_ITEM_TYPE_ETH,
.spec = &vnic_eth_src,
.mask = ð_src_mask,
};
*pat = (struct rte_flow_item){ .type = RTE_FLOW_ITEM_TYPE_END };
rte_ether_format_addr(ebuf, sizeof(ebuf), mac);
if (flow_dump)
rte_flow_dump(stdout, &attr, patterns, actions);
return rte_flow_create(port, &attr, patterns, actions, err);
}
static struct rte_flow *
flow_dst_mac(uint16_t port, uint32_t id,
const struct rte_ether_addr *mac,
const struct rte_flow_action actions[],
struct rte_flow_error *err)
{
struct rte_flow_attr attr = {
.group = id + 1,
.priority = VNIC_DST_MAC_PRIORITY,
.ingress = 1,
};
struct rte_flow_item_eth vnic_eth_dst = {
.dst = *mac,
};
static const struct rte_flow_item_eth eth_dst_mask = {
.dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
};
struct rte_flow_item patterns[3];
struct rte_flow_item *pat = patterns;
char ebuf[RTE_ETHER_ADDR_FMT_SIZE];
*pat++ = (struct rte_flow_item) {
.type = RTE_FLOW_ITEM_TYPE_ANY,
.spec = &inner_flow,
.mask = &any_mask,
};
*pat++ = (struct rte_flow_item) {
.type = RTE_FLOW_ITEM_TYPE_ETH,
.spec = &vnic_eth_dst,
.mask = ð_dst_mask,
};
*pat = (struct rte_flow_item){ .type = RTE_FLOW_ITEM_TYPE_END };
rte_ether_format_addr(ebuf, sizeof(ebuf), mac);
if (flow_dump)
rte_flow_dump(stdout, &attr, patterns, actions);
return rte_flow_create(port, &attr, patterns, actions, err);
}
static void vnic_create(uint16_t portid, uint16_t id, uint16_t q,
const struct rte_ether_addr *mac)
{
struct rte_flow_action_queue queue = {
.index = q,
};
struct rte_flow_action actions[] = {
{ .type = RTE_FLOW_ACTION_TYPE_QUEUE, .conf = &queue },
{ .type = RTE_FLOW_ACTION_TYPE_END },
};
struct rte_flow_error err;
int r;
src_flows[id] = flow_src_mac(portid, id, mac, actions, &err);
if (src_flows[id] == NULL)
rte_exit(EXIT_FAILURE,
"src mac flow create failed: %s\n error type %u %s\n",
rte_strerror(rte_errno), err.type, err.message);
dst_flows[id] = flow_dst_mac(portid, id, mac, actions, &err);
if (dst_flows[id] == NULL)
rte_exit(EXIT_FAILURE,
"dst mac flow create failed: %s\n error type %u %s\n",
rte_strerror(rte_errno), err.type, err.message);
r = rte_eth_dev_rx_queue_start(portid, q);
if (r < 0)
rte_exit(EXIT_FAILURE,
"rte_eth_dev_rx_queue_start: q=%u failed\n", q);
}
static void vnic_delete(uint16_t portid, uint16_t id, uint16_t q)
{
struct rte_flow_error err;
int r;
if (rte_flow_destroy(portid, dst_flows[id], &err))
rte_exit(EXIT_FAILURE,
"rte_flow_destroy failed: %s\n error type %u %s\n",
rte_strerror(rte_errno), err.type, err.message);
dst_flows[id] = NULL;
if (rte_flow_destroy(portid, src_flows[id], &err))
rte_exit(EXIT_FAILURE,
"rte_flow_destroy failed: %s\n error type %u %s\n",
rte_strerror(rte_errno), err.type, err.message);
src_flows[id] = NULL;
r = rte_eth_dev_rx_queue_stop(portid, q);
if (r < 0)
rte_exit(EXIT_FAILURE,
"rte_eth_dev_rx_queue_stop: q=%u failed\n", q);
}
static void print_mac(uint16_t portid)
{
struct rte_ether_addr eth_addr;
char buf[RTE_ETHER_ADDR_FMT_SIZE];
rte_eth_macaddr_get(portid, ð_addr);
rte_ether_format_addr(buf, sizeof(buf), ð_addr);
printf("Initialized port %u: MAC: %s\n", portid, buf);
}
static void usage(const char *argv0)
{
printf("Usage: %s [EAL options] -- [OPTIONS] MAC1 MAC2 ...\n"
" -c,--count N repeat count\n"
" -f,--flow flow dump\n"
" -v,--details print packet details\n",
argv0);
exit(1);
}
static const struct option longopts[] = {
{ "count", required_argument, 0, 'c' },
{ "flow", no_argument, 0, 'f' },
{ 0 }
};
static void parse_args(int argc, char **argv)
{
unsigned int i;
int opt;
while ((opt = getopt_long(argc, argv, "c:q:fr:t:",
longopts, NULL)) != EOF) {
switch (opt) {
case 'c':
repeat = atoi(optarg);
break;
case 'f':
flow_dump = true;
break;
case 'r':
nrxq = atoi(optarg);
break;
case 't':
ntxq = atoi(optarg);
break;
default:
fprintf(stderr, "Unknown option\n");
usage(argv[0]);
}
}
/* Additional arguments are MAC address of VNICs */
num_vnic = argc - optind;
vnic_mac = calloc(sizeof(struct rte_ether_addr), num_vnic);
for (i = 0; i < num_vnic; i++) {
const char *asc = argv[optind + i];
if (rte_ether_unformat_addr(asc, &vnic_mac[i]) != 0)
rte_exit(EXIT_FAILURE,
"Invalid mac address: %s\n", asc);
}
}
int main(int argc, char **argv)
{
unsigned int v, iter;
unsigned int num_mbufs, obj_size;
int r;
r = rte_eal_init(argc, argv);
if (r < 0)
rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
parse_args(argc - r, argv + r);
if (rte_eth_dev_count_avail() != 1)
rte_exit(EXIT_FAILURE,
"Expect one external port\n");
src_flows = calloc(num_vnic + 1, sizeof(struct rte_flow *));
dst_flows = calloc(num_vnic + 1, sizeof(struct rte_flow *));
num_mbufs = rte_align32pow2(nrxq * RX_DESC_DEFAULT * 3);
obj_size = rte_mempool_calc_obj_size(RTE_MBUF_DEFAULT_BUF_SIZE,
0, NULL);
printf("mbuf pool %u of %u bytes = %uMb\n",
num_mbufs, obj_size,
(num_mbufs * obj_size) / (1024 * 1024));
mb_pool = rte_pktmbuf_pool_create("mb_pool", num_mbufs, 32, 0,
RTE_MBUF_DEFAULT_BUF_SIZE, 0);
if (!mb_pool)
rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
port_config(0, ntxq, nrxq);
print_mac(0);
printf("Test started\n");
fflush(stdout);
for (iter = 0; iter < repeat; iter++) {
printf("\n%u: ", iter);
fflush(stdout);
for (v = 1; v <= num_vnic; ++v) {
printf("+"); fflush(stdout);
vnic_create(0, v, v, &vnic_mac[v - 1]);
}
sleep(1);
for (v = 1; v <= num_vnic; ++v) {
printf("-"); fflush(stdout);
vnic_delete(0, v, v);
}
}
printf("\nSUCCESS\n");
fflush(stdout);
rte_eth_dev_stop(0);
rte_eth_dev_close(0);
rte_eal_cleanup();
return 0;
}