forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0005-enable-pdump-and-change-dpdk-pdump-tool-for-dpvs.patch
565 lines (546 loc) · 16.2 KB
/
0005-enable-pdump-and-change-dpdk-pdump-tool-for-dpvs.patch
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
diff --git a/app/pdump/main.c b/app/pdump/main.c
index ccf2a1d..7e72c16 100644
--- a/app/pdump/main.c
+++ b/app/pdump/main.c
@@ -26,6 +26,8 @@
#include <rte_ring.h>
#include <rte_string_fns.h>
#include <rte_pdump.h>
+#include <rte_malloc.h>
+
#define CMD_LINE_OPT_PDUMP "pdump"
#define PDUMP_PORT_ARG "port"
@@ -38,6 +40,14 @@
#define PDUMP_MSIZE_ARG "mbuf-size"
#define PDUMP_NUM_MBUFS_ARG "total-num-mbufs"
+#define PDUMP_HOST_ARG "host"
+#define PDUMP_SRC_ARG "src-host"
+#define PDUMP_DST_ARG "dst-host"
+#define PDUMP_PROTO_PORT_AGE "proto-port"
+#define PDUMP_SPORT_ARG "src-port"
+#define PDUMP_DPORT_ARG "dst-port"
+#define PDUMP_PROTO_ARG "proto"
+
#define VDEV_NAME_FMT "net_pcap_%s_%d"
#define VDEV_PCAP_ARGS_FMT "tx_pcap=%s"
#define VDEV_IFACE_ARGS_FMT "tx_iface=%s"
@@ -91,6 +101,13 @@ enum pdump_by {
PDUMP_RING_SIZE_ARG,
PDUMP_MSIZE_ARG,
PDUMP_NUM_MBUFS_ARG,
+ PDUMP_HOST_ARG,
+ PDUMP_SRC_ARG,
+ PDUMP_DST_ARG,
+ PDUMP_PROTO_PORT_AGE,
+ PDUMP_SPORT_ARG,
+ PDUMP_DPORT_ARG,
+ PDUMP_PROTO_ARG,
NULL
};
@@ -124,6 +141,7 @@ struct pdump_tuples {
enum pcap_stream rx_vdev_stream_type;
enum pcap_stream tx_vdev_stream_type;
bool single_pdump_dev;
+ struct pdump_filter *filter;
/* stats */
struct pdump_stats stats;
@@ -149,6 +167,11 @@ struct parse_val {
"(queue=<queue_id>),"
"(rx-dev=<iface or pcap file> |"
" tx-dev=<iface or pcap file>,"
+ "[host=<ipaddress> | src-host=<source ip address> |"
+ "dst-host=<destination ip address>],"
+ "[proto=<protocol type>support:tcp/udp/icmp],"
+ "[proto-port=<protocol port> |src-port=<source protocol port> |"
+ "dst-port=<destination protocol port>],"
"[ring-size=<ring size>default:16384],"
"[mbuf-size=<mbuf data size>default:2176],"
"[total-num-mbufs=<number of mbufs>default:65535]'\n",
@@ -235,6 +258,65 @@ struct parse_val {
}
static int
+parse_host(const char *key __rte_unused, const char *value, void *extra_args)
+{
+ struct pdump_tuples *pt = extra_args;
+ struct in_addr inaddr;
+ struct in6_addr inaddr6;
+ union addr addr;
+ int af = 0;
+
+ if (inet_pton(AF_INET6, value, &inaddr6) > 0) {
+ af = AF_INET6;
+ addr.in6 = inaddr6;
+ } else if (inet_pton(AF_INET, value, &inaddr) > 0){
+ af = AF_INET;
+ addr.in = inaddr;
+ } else {
+ printf("IP address invaled\n");
+ return -EINVAL;
+ }
+
+ if (pt->filter && pt->filter->af != 0 && af != pt->filter->af) {
+ printf("IPv4 and IPv6 conflict\n");
+ return -EINVAL;
+ } else {
+ pt->filter->af = af;
+ }
+
+ if (!strcmp(key, PDUMP_HOST_ARG)) {
+ rte_memcpy(&pt->filter->host_addr, &addr, sizeof(addr));
+ } else if (!strcmp(key, PDUMP_SRC_ARG)) {
+ rte_memcpy(&pt->filter->s_addr, &addr, sizeof(addr));
+ } else if (!strcmp(key, PDUMP_DST_ARG)) {
+ rte_memcpy(&pt->filter->d_addr, &addr, sizeof(addr));
+ }
+
+ return 0;
+}
+
+static int
+parse_proto(const char *key __rte_unused, const char *value, void *extra_args)
+{
+ struct pdump_tuples *pt = extra_args;
+
+ if (!strcmp(value, "tcp")) {
+ pt->filter->proto = IPPROTO_TCP;
+ } else if (!strcmp(value, "udp")) {
+ pt->filter->proto = IPPROTO_UDP;
+ } else if (!strcmp(value, "icmp")) {
+ pt->filter->proto = IPPROTO_ICMP;
+ } else {
+ printf("invalid value:\"%s\" for key:\"%s\", "
+ "value must be tcp/udp/icmp\n", value, key);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+
+static int
parse_pdump(const char *optarg)
{
struct rte_kvargs *kvlist;
@@ -361,6 +443,75 @@ struct parse_val {
} else
pt->total_num_mbufs = MBUFS_PER_POOL;
+ /* filter parsing and validation */
+ pt->filter = rte_zmalloc("pdump_filter",
+ sizeof(struct pdump_filter), 0);
+ cnt1 = rte_kvargs_count(kvlist, PDUMP_HOST_ARG);
+ if (cnt1 == 1) {
+ ret = rte_kvargs_process(kvlist, PDUMP_HOST_ARG,
+ &parse_host, pt);
+ if (ret < 0)
+ goto free_kvlist;
+ }
+
+ cnt1 = rte_kvargs_count(kvlist, PDUMP_SRC_ARG);
+ if (cnt1 == 1) {
+ ret = rte_kvargs_process(kvlist, PDUMP_SRC_ARG,
+ &parse_host, pt);
+ if (ret < 0)
+ goto free_kvlist;
+ }
+
+ cnt1 = rte_kvargs_count(kvlist, PDUMP_DST_ARG);
+ if (cnt1 == 1) {
+ ret = rte_kvargs_process(kvlist, PDUMP_DST_ARG,
+ &parse_host, pt);
+ if (ret < 0)
+ goto free_kvlist;
+ }
+
+
+ cnt1 = rte_kvargs_count(kvlist, PDUMP_PROTO_PORT_AGE);
+ if (cnt1 == 1) {
+ v.min = 1;
+ v.max = UINT16_MAX;
+ ret = rte_kvargs_process(kvlist, PDUMP_PROTO_PORT_AGE,
+ &parse_uint_value, &v);
+ if (ret < 0)
+ goto free_kvlist;
+ pt->filter->proto_port = (uint16_t) v.val;
+ }
+
+ cnt1 = rte_kvargs_count(kvlist, PDUMP_SPORT_ARG);
+ if (cnt1 == 1) {
+ v.min = 1;
+ v.max = UINT16_MAX;
+ ret = rte_kvargs_process(kvlist, PDUMP_SPORT_ARG,
+ &parse_uint_value, &v);
+ if (ret < 0)
+ goto free_kvlist;
+ pt->filter->s_port = (uint16_t) v.val;
+ }
+
+ cnt1 = rte_kvargs_count(kvlist, PDUMP_DPORT_ARG);
+ if (cnt1 == 1) {
+ v.min = 1;
+ v.max = UINT16_MAX;
+ ret = rte_kvargs_process(kvlist, PDUMP_DPORT_ARG,
+ &parse_uint_value, &v);
+ if (ret < 0)
+ goto free_kvlist;
+ pt->filter->d_port = (uint16_t) v.val;
+ }
+
+ cnt1 = rte_kvargs_count(kvlist, PDUMP_PROTO_ARG);
+ if (cnt1 == 1) {
+ ret = rte_kvargs_process(kvlist, PDUMP_PROTO_ARG,
+ &parse_proto, pt);
+ if (ret < 0)
+ goto free_kvlist;
+ }
+
num_tuples++;
free_kvlist:
@@ -486,6 +637,8 @@ struct parse_val {
rte_ring_free(pt->rx_ring);
if (pt->tx_ring)
rte_ring_free(pt->tx_ring);
+ if (pt->filter)
+ rte_free(pt->filter);
}
}
@@ -527,6 +680,7 @@ struct parse_val {
}
}
+
cleanup_rings();
}
@@ -789,20 +943,20 @@ struct parse_val {
pt->queue,
RTE_PDUMP_FLAG_RX,
pt->rx_ring,
- pt->mp, NULL);
+ pt->mp, pt->filter);
ret1 = rte_pdump_enable_by_deviceid(
pt->device_id,
pt->queue,
RTE_PDUMP_FLAG_TX,
pt->tx_ring,
- pt->mp, NULL);
+ pt->mp, pt->filter);
} else if (pt->dump_by_type == PORT_ID) {
ret = rte_pdump_enable(pt->port, pt->queue,
RTE_PDUMP_FLAG_RX,
- pt->rx_ring, pt->mp, NULL);
+ pt->rx_ring, pt->mp, pt->filter);
ret1 = rte_pdump_enable(pt->port, pt->queue,
RTE_PDUMP_FLAG_TX,
- pt->tx_ring, pt->mp, NULL);
+ pt->tx_ring, pt->mp, pt->filter);
}
} else if (pt->dir == RTE_PDUMP_FLAG_RX) {
if (pt->dump_by_type == DEVICE_ID)
@@ -810,22 +964,22 @@ struct parse_val {
pt->device_id,
pt->queue,
pt->dir, pt->rx_ring,
- pt->mp, NULL);
+ pt->mp, pt->filter);
else if (pt->dump_by_type == PORT_ID)
ret = rte_pdump_enable(pt->port, pt->queue,
pt->dir,
- pt->rx_ring, pt->mp, NULL);
+ pt->rx_ring, pt->mp, pt->filter);
} else if (pt->dir == RTE_PDUMP_FLAG_TX) {
if (pt->dump_by_type == DEVICE_ID)
ret = rte_pdump_enable_by_deviceid(
pt->device_id,
pt->queue,
pt->dir,
- pt->tx_ring, pt->mp, NULL);
+ pt->tx_ring, pt->mp, pt->filter);
else if (pt->dump_by_type == PORT_ID)
ret = rte_pdump_enable(pt->port, pt->queue,
pt->dir,
- pt->tx_ring, pt->mp, NULL);
+ pt->tx_ring, pt->mp, pt->filter);
}
if (ret < 0 || ret1 < 0) {
cleanup_pdump_resources();
diff --git a/config/common_base b/config/common_base
index d12ae98..5c15ea0 100644
--- a/config/common_base
+++ b/config/common_base
@@ -451,7 +451,7 @@ CONFIG_RTE_LIBRTE_PMD_NULL=y
#
# Compile software PMD backed by PCAP files
#
-CONFIG_RTE_LIBRTE_PMD_PCAP=n
+CONFIG_RTE_LIBRTE_PMD_PCAP=y
#
# Compile example software rings based PMD
diff --git a/lib/librte_pdump/rte_pdump.c b/lib/librte_pdump/rte_pdump.c
index 6c3a885..d9a3258 100644
--- a/lib/librte_pdump/rte_pdump.c
+++ b/lib/librte_pdump/rte_pdump.c
@@ -9,6 +9,10 @@
#include <rte_log.h>
#include <rte_errno.h>
#include <rte_string_fns.h>
+#include <rte_arp.h>
+#include <rte_ip.h>
+#include <rte_udp.h>
+#include <rte_byteorder.h>
#include "rte_pdump.h"
@@ -132,6 +136,133 @@ struct pdump_response {
return m_dup;
}
+static int
+inet_addr_equal(int af, const union addr *a1,
+ const union addr *a2)
+{
+ switch (af) {
+ case AF_INET:
+ return a1->in.s_addr == a2->in.s_addr;
+ case AF_INET6:
+ return memcmp(a1->in6.s6_addr, a2->in6.s6_addr, 16) == 0;
+ default:
+ return memcmp(a1, a2, sizeof(union addr)) == 0;
+ }
+}
+
+static int
+inet_is_addr_any(int af, const union addr *addr)
+{
+ switch (af) {
+ case AF_INET:
+ return addr->in.s_addr == htonl(INADDR_ANY);
+ case AF_INET6:
+ return IN6_ARE_ADDR_EQUAL(&addr->in6, &in6addr_any);
+ default:
+ return -1;
+ }
+
+ return -1;
+}
+static int
+pdump_filter(struct rte_mbuf *m, struct pdump_filter *filter)
+{
+ struct ether_hdr *eth_hdr;
+ struct vlan_eth_hdr *vlan_eth_hdr;
+ union addr s_addr, d_addr;
+ int prepend = 0;
+ uint16_t type = 0;
+ uint16_t iph_len = 0;
+ uint8_t proto = 0;
+
+ int af;
+
+ if (filter->af == 0 && filter->s_port == 0 &&
+ filter->d_port == 0 && filter->proto == 0 &&
+ filter->proto_port == 0)
+ return 0;
+
+ eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
+
+ if (eth_hdr->ether_type == htons(ETH_P_8021Q)) {
+ prepend += sizeof(struct vlan_eth_hdr);
+ vlan_eth_hdr = rte_pktmbuf_mtod(m, struct vlan_eth_hdr *);
+ type = vlan_eth_hdr->h_vlan_encapsulated_proto;
+ } else {
+ prepend += sizeof(struct ether_hdr);
+ eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
+ type = eth_hdr->ether_type;
+ }
+
+ if (rte_pktmbuf_adj(m, prepend) == NULL)
+ goto prepend;
+
+ if (type == rte_cpu_to_be_16(ETHER_TYPE_ARP)) {
+ struct arp_hdr *arp = rte_pktmbuf_mtod(m, struct arp_hdr *);
+ af = AF_INET;
+ s_addr.in.s_addr = arp->arp_data.arp_sip;
+ d_addr.in.s_addr = arp->arp_data.arp_tip;
+ } else if (type == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
+ struct ipv4_hdr *ip4 = rte_pktmbuf_mtod(m, struct ipv4_hdr *);
+ af = AF_INET;
+ s_addr.in.s_addr = ip4->src_addr;
+ d_addr.in.s_addr = ip4->dst_addr;
+ proto = ip4->next_proto_id;
+ iph_len = (ip4->version_ihl & 0xf) << 2;
+ } else if (type == rte_cpu_to_be_16(ETHER_TYPE_IPv6)) {
+ struct ipv6_hdr *ip6 = rte_pktmbuf_mtod(m, struct ipv6_hdr *);
+ af = AF_INET6;
+ rte_memcpy(&s_addr.in6, &ip6->src_addr, 16);
+ rte_memcpy(&d_addr.in6, &ip6->dst_addr, 16);
+ proto = ip6->proto;
+ iph_len = sizeof(struct ipv6_hdr);
+ } else {
+ goto prepend;
+ }
+
+ /*filter*/
+ if (!inet_is_addr_any(af, &filter->s_addr) &&
+ !inet_addr_equal(af, &filter->s_addr, &s_addr))
+ goto prepend;
+ if (!inet_is_addr_any(af, &filter->d_addr) &&
+ !inet_addr_equal(af, &filter->d_addr, &d_addr))
+ goto prepend;
+ if (!inet_is_addr_any(af, &filter->host_addr) &&
+ !inet_addr_equal(af, &filter->host_addr, &s_addr) &&
+ !inet_addr_equal(af, &filter->host_addr, &d_addr))
+ goto prepend;
+
+ if (filter->proto && filter->proto != proto)
+ goto prepend;
+
+ if (filter->s_port || filter->d_port || filter->proto_port) {
+ if (proto != IPPROTO_TCP && proto != IPPROTO_UDP)
+ goto prepend;
+ struct udp_hdr _uh;
+ const struct udp_hdr *uh;
+ uh = rte_pktmbuf_read(m, iph_len, sizeof(_uh), &_uh);
+ if (uh == NULL)
+ goto prepend;
+ if (filter->s_port && filter->s_port != rte_cpu_to_be_16(uh->src_port))
+ goto prepend;
+
+ if (filter->d_port && filter->d_port != rte_cpu_to_be_16(uh->dst_port))
+ goto prepend;
+
+ if (filter->proto_port &&
+ filter->proto_port != rte_cpu_to_be_16(uh->src_port) &&
+ filter->proto_port != rte_cpu_to_be_16(uh->dst_port))
+ goto prepend;
+ }
+
+ rte_pktmbuf_prepend(m, prepend);
+ return 0;
+
+prepend:
+ rte_pktmbuf_prepend(m, prepend);
+ return -1;
+}
+
static inline void
pdump_copy(struct rte_mbuf **pkts, uint16_t nb_pkts, void *user_params)
{
@@ -148,6 +279,8 @@ struct pdump_response {
ring = cbs->ring;
mp = cbs->mp;
for (i = 0; i < nb_pkts; i++) {
+ if (pdump_filter(pkts[i], cbs->filter) != 0)
+ continue;
p = pdump_pktmbuf_copy(pkts[i], mp);
if (p)
dup_bufs[d_pkts++] = p;
@@ -184,7 +317,7 @@ struct pdump_response {
static int
pdump_register_rx_callbacks(uint16_t end_q, uint16_t port, uint16_t queue,
struct rte_ring *ring, struct rte_mempool *mp,
- uint16_t operation)
+ struct pdump_filter *filter, uint16_t operation)
{
uint16_t qid;
struct pdump_rxtx_cbs *cbs = NULL;
@@ -202,6 +335,7 @@ struct pdump_response {
}
cbs->ring = ring;
cbs->mp = mp;
+ cbs->filter = filter;
cbs->cb = rte_eth_add_first_rx_callback(port, qid,
pdump_rx, cbs);
if (cbs->cb == NULL) {
@@ -238,7 +372,7 @@ struct pdump_response {
static int
pdump_register_tx_callbacks(uint16_t end_q, uint16_t port, uint16_t queue,
struct rte_ring *ring, struct rte_mempool *mp,
- uint16_t operation)
+ struct pdump_filter *filter, uint16_t operation)
{
uint16_t qid;
@@ -257,6 +391,7 @@ struct pdump_response {
}
cbs->ring = ring;
cbs->mp = mp;
+ cbs->filter = filter;
cbs->cb = rte_eth_add_tx_callback(port, qid, pdump_tx,
cbs);
if (cbs->cb == NULL) {
@@ -300,6 +435,7 @@ struct pdump_response {
uint16_t operation;
struct rte_ring *ring;
struct rte_mempool *mp;
+ struct pdump_filter *filter;
flags = p->flags;
operation = p->op;
@@ -315,6 +451,7 @@ struct pdump_response {
queue = p->data.en_v1.queue;
ring = p->data.en_v1.ring;
mp = p->data.en_v1.mp;
+ filter = p->data.en_v1.filter;
} else {
ret = rte_eth_dev_get_port_by_name(p->data.dis_v1.device,
&port);
@@ -327,6 +464,7 @@ struct pdump_response {
queue = p->data.dis_v1.queue;
ring = p->data.dis_v1.ring;
mp = p->data.dis_v1.mp;
+ filter = p->data.dis_v1.filter;
}
/* validation if packet capture is for all queues */
@@ -358,7 +496,7 @@ struct pdump_response {
if (flags & RTE_PDUMP_FLAG_RX) {
end_q = (queue == RTE_PDUMP_ALL_QUEUES) ? nb_rx_q : queue + 1;
ret = pdump_register_rx_callbacks(end_q, port, queue, ring, mp,
- operation);
+ filter, operation);
if (ret < 0)
return ret;
}
@@ -367,7 +505,7 @@ struct pdump_response {
if (flags & RTE_PDUMP_FLAG_TX) {
end_q = (queue == RTE_PDUMP_ALL_QUEUES) ? nb_tx_q : queue + 1;
ret = pdump_register_tx_callbacks(end_q, port, queue, ring, mp,
- operation);
+ filter, operation);
if (ret < 0)
return ret;
}
diff --git a/lib/librte_pdump/rte_pdump.h b/lib/librte_pdump/rte_pdump.h
index 673a2b0..633b48f 100644
--- a/lib/librte_pdump/rte_pdump.h
+++ b/lib/librte_pdump/rte_pdump.h
@@ -15,6 +15,8 @@
#include <stdint.h>
#include <rte_mempool.h>
#include <rte_ring.h>
+#include <arpa/inet.h>
+#include <linux/if_ether.h>
#ifdef __cplusplus
extern "C" {
@@ -34,6 +36,31 @@ enum rte_pdump_socktype {
RTE_PDUMP_SOCKET_CLIENT = 2
};
+union addr {
+ struct in_addr in;
+ struct in6_addr in6;
+};
+
+struct pdump_filter {
+ int af;
+ union addr s_addr;
+ union addr d_addr;
+ union addr host_addr; //s_addr or d_addr
+
+ uint8_t proto;
+ uint16_t proto_port; //s_port or d_port
+ uint16_t s_port;
+ uint16_t d_port;
+};
+
+struct vlan_eth_hdr {
+ unsigned char h_dest[ETH_ALEN];
+ unsigned char h_source[ETH_ALEN];
+ unsigned short h_vlan_proto;
+ unsigned short h_vlan_TCI;
+ unsigned short h_vlan_encapsulated_proto;
+};
+
/**
* Initialize packet capturing handling
*