xref: /f-stack/dpdk/drivers/net/pcap/rte_eth_pcap.c (revision 2d9fd380)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation.
3  * Copyright(c) 2014 6WIND S.A.
4  * All rights reserved.
5  */
6 
7 #include <time.h>
8 
9 #include <net/if.h>
10 #include <sys/socket.h>
11 #include <sys/ioctl.h>
12 #include <unistd.h>
13 
14 #if defined(RTE_EXEC_ENV_FREEBSD)
15 #include <sys/sysctl.h>
16 #include <net/if_dl.h>
17 #endif
18 
19 #include <pcap.h>
20 
21 #include <rte_cycles.h>
22 #include <rte_ethdev_driver.h>
23 #include <rte_ethdev_vdev.h>
24 #include <rte_kvargs.h>
25 #include <rte_malloc.h>
26 #include <rte_mbuf.h>
27 #include <rte_bus_vdev.h>
28 #include <rte_string_fns.h>
29 
30 #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535
31 #define RTE_ETH_PCAP_SNAPLEN RTE_ETHER_MAX_JUMBO_FRAME_LEN
32 #define RTE_ETH_PCAP_PROMISC 1
33 #define RTE_ETH_PCAP_TIMEOUT -1
34 
35 #define ETH_PCAP_RX_PCAP_ARG  "rx_pcap"
36 #define ETH_PCAP_TX_PCAP_ARG  "tx_pcap"
37 #define ETH_PCAP_RX_IFACE_ARG "rx_iface"
38 #define ETH_PCAP_RX_IFACE_IN_ARG "rx_iface_in"
39 #define ETH_PCAP_TX_IFACE_ARG "tx_iface"
40 #define ETH_PCAP_IFACE_ARG    "iface"
41 #define ETH_PCAP_PHY_MAC_ARG  "phy_mac"
42 #define ETH_PCAP_INFINITE_RX_ARG  "infinite_rx"
43 
44 #define ETH_PCAP_ARG_MAXLEN	64
45 
46 #define RTE_PMD_PCAP_MAX_QUEUES 16
47 
48 static char errbuf[PCAP_ERRBUF_SIZE];
49 static struct timeval start_time;
50 static uint64_t start_cycles;
51 static uint64_t hz;
52 static uint8_t iface_idx;
53 
54 static uint64_t timestamp_rx_dynflag;
55 static int timestamp_dynfield_offset = -1;
56 
57 struct queue_stat {
58 	volatile unsigned long pkts;
59 	volatile unsigned long bytes;
60 	volatile unsigned long err_pkts;
61 };
62 
63 struct pcap_rx_queue {
64 	uint16_t port_id;
65 	uint16_t queue_id;
66 	struct rte_mempool *mb_pool;
67 	struct queue_stat rx_stat;
68 	char name[PATH_MAX];
69 	char type[ETH_PCAP_ARG_MAXLEN];
70 
71 	/* Contains pre-generated packets to be looped through */
72 	struct rte_ring *pkts;
73 };
74 
75 struct pcap_tx_queue {
76 	uint16_t port_id;
77 	uint16_t queue_id;
78 	struct queue_stat tx_stat;
79 	char name[PATH_MAX];
80 	char type[ETH_PCAP_ARG_MAXLEN];
81 };
82 
83 struct pmd_internals {
84 	struct pcap_rx_queue rx_queue[RTE_PMD_PCAP_MAX_QUEUES];
85 	struct pcap_tx_queue tx_queue[RTE_PMD_PCAP_MAX_QUEUES];
86 	char devargs[ETH_PCAP_ARG_MAXLEN];
87 	struct rte_ether_addr eth_addr;
88 	int if_index;
89 	int single_iface;
90 	int phy_mac;
91 	unsigned int infinite_rx;
92 };
93 
94 struct pmd_process_private {
95 	pcap_t *rx_pcap[RTE_PMD_PCAP_MAX_QUEUES];
96 	pcap_t *tx_pcap[RTE_PMD_PCAP_MAX_QUEUES];
97 	pcap_dumper_t *tx_dumper[RTE_PMD_PCAP_MAX_QUEUES];
98 };
99 
100 struct pmd_devargs {
101 	unsigned int num_of_queue;
102 	struct devargs_queue {
103 		pcap_dumper_t *dumper;
104 		pcap_t *pcap;
105 		const char *name;
106 		const char *type;
107 	} queue[RTE_PMD_PCAP_MAX_QUEUES];
108 	int phy_mac;
109 };
110 
111 struct pmd_devargs_all {
112 	struct pmd_devargs rx_queues;
113 	struct pmd_devargs tx_queues;
114 	int single_iface;
115 	unsigned int is_tx_pcap;
116 	unsigned int is_tx_iface;
117 	unsigned int is_rx_pcap;
118 	unsigned int is_rx_iface;
119 	unsigned int infinite_rx;
120 };
121 
122 static const char *valid_arguments[] = {
123 	ETH_PCAP_RX_PCAP_ARG,
124 	ETH_PCAP_TX_PCAP_ARG,
125 	ETH_PCAP_RX_IFACE_ARG,
126 	ETH_PCAP_RX_IFACE_IN_ARG,
127 	ETH_PCAP_TX_IFACE_ARG,
128 	ETH_PCAP_IFACE_ARG,
129 	ETH_PCAP_PHY_MAC_ARG,
130 	ETH_PCAP_INFINITE_RX_ARG,
131 	NULL
132 };
133 
134 static struct rte_eth_link pmd_link = {
135 		.link_speed = ETH_SPEED_NUM_10G,
136 		.link_duplex = ETH_LINK_FULL_DUPLEX,
137 		.link_status = ETH_LINK_DOWN,
138 		.link_autoneg = ETH_LINK_FIXED,
139 };
140 
141 RTE_LOG_REGISTER(eth_pcap_logtype, pmd.net.pcap, NOTICE);
142 
143 #define PMD_LOG(level, fmt, args...) \
144 	rte_log(RTE_LOG_ ## level, eth_pcap_logtype, \
145 		"%s(): " fmt "\n", __func__, ##args)
146 
147 static int
eth_pcap_rx_jumbo(struct rte_mempool * mb_pool,struct rte_mbuf * mbuf,const u_char * data,uint16_t data_len)148 eth_pcap_rx_jumbo(struct rte_mempool *mb_pool, struct rte_mbuf *mbuf,
149 		const u_char *data, uint16_t data_len)
150 {
151 	/* Copy the first segment. */
152 	uint16_t len = rte_pktmbuf_tailroom(mbuf);
153 	struct rte_mbuf *m = mbuf;
154 
155 	rte_memcpy(rte_pktmbuf_append(mbuf, len), data, len);
156 	data_len -= len;
157 	data += len;
158 
159 	while (data_len > 0) {
160 		/* Allocate next mbuf and point to that. */
161 		m->next = rte_pktmbuf_alloc(mb_pool);
162 
163 		if (unlikely(!m->next))
164 			return -1;
165 
166 		m = m->next;
167 
168 		/* Headroom is not needed in chained mbufs. */
169 		rte_pktmbuf_prepend(m, rte_pktmbuf_headroom(m));
170 		m->pkt_len = 0;
171 		m->data_len = 0;
172 
173 		/* Copy next segment. */
174 		len = RTE_MIN(rte_pktmbuf_tailroom(m), data_len);
175 		rte_memcpy(rte_pktmbuf_append(m, len), data, len);
176 
177 		mbuf->nb_segs++;
178 		data_len -= len;
179 		data += len;
180 	}
181 
182 	return mbuf->nb_segs;
183 }
184 
185 static uint16_t
eth_pcap_rx_infinite(void * queue,struct rte_mbuf ** bufs,uint16_t nb_pkts)186 eth_pcap_rx_infinite(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
187 {
188 	int i;
189 	struct pcap_rx_queue *pcap_q = queue;
190 	uint32_t rx_bytes = 0;
191 
192 	if (unlikely(nb_pkts == 0))
193 		return 0;
194 
195 	if (rte_pktmbuf_alloc_bulk(pcap_q->mb_pool, bufs, nb_pkts) != 0)
196 		return 0;
197 
198 	for (i = 0; i < nb_pkts; i++) {
199 		struct rte_mbuf *pcap_buf;
200 		int err = rte_ring_dequeue(pcap_q->pkts, (void **)&pcap_buf);
201 		if (err)
202 			return i;
203 
204 		rte_memcpy(rte_pktmbuf_mtod(bufs[i], void *),
205 				rte_pktmbuf_mtod(pcap_buf, void *),
206 				pcap_buf->data_len);
207 		bufs[i]->data_len = pcap_buf->data_len;
208 		bufs[i]->pkt_len = pcap_buf->pkt_len;
209 		bufs[i]->port = pcap_q->port_id;
210 		rx_bytes += pcap_buf->data_len;
211 
212 		/* Enqueue packet back on ring to allow infinite rx. */
213 		rte_ring_enqueue(pcap_q->pkts, pcap_buf);
214 	}
215 
216 	pcap_q->rx_stat.pkts += i;
217 	pcap_q->rx_stat.bytes += rx_bytes;
218 
219 	return i;
220 }
221 
222 static uint16_t
eth_pcap_rx(void * queue,struct rte_mbuf ** bufs,uint16_t nb_pkts)223 eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
224 {
225 	unsigned int i;
226 	struct pcap_pkthdr header;
227 	struct pmd_process_private *pp;
228 	const u_char *packet;
229 	struct rte_mbuf *mbuf;
230 	struct pcap_rx_queue *pcap_q = queue;
231 	uint16_t num_rx = 0;
232 	uint32_t rx_bytes = 0;
233 	pcap_t *pcap;
234 
235 	pp = rte_eth_devices[pcap_q->port_id].process_private;
236 	pcap = pp->rx_pcap[pcap_q->queue_id];
237 
238 	if (unlikely(pcap == NULL || nb_pkts == 0))
239 		return 0;
240 
241 	/* Reads the given number of packets from the pcap file one by one
242 	 * and copies the packet data into a newly allocated mbuf to return.
243 	 */
244 	for (i = 0; i < nb_pkts; i++) {
245 		/* Get the next PCAP packet */
246 		packet = pcap_next(pcap, &header);
247 		if (unlikely(packet == NULL))
248 			break;
249 
250 		mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
251 		if (unlikely(mbuf == NULL))
252 			break;
253 
254 		if (header.caplen <= rte_pktmbuf_tailroom(mbuf)) {
255 			/* pcap packet will fit in the mbuf, can copy it */
256 			rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), packet,
257 					header.caplen);
258 			mbuf->data_len = (uint16_t)header.caplen;
259 		} else {
260 			/* Try read jumbo frame into multi mbufs. */
261 			if (unlikely(eth_pcap_rx_jumbo(pcap_q->mb_pool,
262 						       mbuf,
263 						       packet,
264 						       header.caplen) == -1)) {
265 				rte_pktmbuf_free(mbuf);
266 				break;
267 			}
268 		}
269 
270 		mbuf->pkt_len = (uint16_t)header.caplen;
271 		*RTE_MBUF_DYNFIELD(mbuf, timestamp_dynfield_offset,
272 			rte_mbuf_timestamp_t *) =
273 				(uint64_t)header.ts.tv_sec * 1000000 +
274 				header.ts.tv_usec;
275 		mbuf->ol_flags |= timestamp_rx_dynflag;
276 		mbuf->port = pcap_q->port_id;
277 		bufs[num_rx] = mbuf;
278 		num_rx++;
279 		rx_bytes += header.caplen;
280 	}
281 	pcap_q->rx_stat.pkts += num_rx;
282 	pcap_q->rx_stat.bytes += rx_bytes;
283 
284 	return num_rx;
285 }
286 
287 static uint16_t
eth_null_rx(void * queue __rte_unused,struct rte_mbuf ** bufs __rte_unused,uint16_t nb_pkts __rte_unused)288 eth_null_rx(void *queue __rte_unused,
289 		struct rte_mbuf **bufs __rte_unused,
290 		uint16_t nb_pkts __rte_unused)
291 {
292 	return 0;
293 }
294 
295 #define NSEC_PER_SEC	1000000000L
296 
297 static inline void
calculate_timestamp(struct timeval * ts)298 calculate_timestamp(struct timeval *ts) {
299 	uint64_t cycles;
300 	struct timeval cur_time;
301 
302 	cycles = rte_get_timer_cycles() - start_cycles;
303 	cur_time.tv_sec = cycles / hz;
304 	cur_time.tv_usec = (cycles % hz) * NSEC_PER_SEC / hz;
305 
306 	ts->tv_sec = start_time.tv_sec + cur_time.tv_sec;
307 	ts->tv_usec = start_time.tv_usec + cur_time.tv_usec;
308 	if (ts->tv_usec >= NSEC_PER_SEC) {
309 		ts->tv_usec -= NSEC_PER_SEC;
310 		ts->tv_sec += 1;
311 	}
312 }
313 
314 /*
315  * Callback to handle writing packets to a pcap file.
316  */
317 static uint16_t
eth_pcap_tx_dumper(void * queue,struct rte_mbuf ** bufs,uint16_t nb_pkts)318 eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
319 {
320 	unsigned int i;
321 	struct rte_mbuf *mbuf;
322 	struct pmd_process_private *pp;
323 	struct pcap_tx_queue *dumper_q = queue;
324 	uint16_t num_tx = 0;
325 	uint32_t tx_bytes = 0;
326 	struct pcap_pkthdr header;
327 	pcap_dumper_t *dumper;
328 	unsigned char temp_data[RTE_ETH_PCAP_SNAPLEN];
329 	size_t len, caplen;
330 
331 	pp = rte_eth_devices[dumper_q->port_id].process_private;
332 	dumper = pp->tx_dumper[dumper_q->queue_id];
333 
334 	if (dumper == NULL || nb_pkts == 0)
335 		return 0;
336 
337 	/* writes the nb_pkts packets to the previously opened pcap file
338 	 * dumper */
339 	for (i = 0; i < nb_pkts; i++) {
340 		mbuf = bufs[i];
341 		len = caplen = rte_pktmbuf_pkt_len(mbuf);
342 		if (unlikely(!rte_pktmbuf_is_contiguous(mbuf) &&
343 				len > sizeof(temp_data))) {
344 			caplen = sizeof(temp_data);
345 		}
346 
347 		calculate_timestamp(&header.ts);
348 		header.len = len;
349 		header.caplen = caplen;
350 		/* rte_pktmbuf_read() returns a pointer to the data directly
351 		 * in the mbuf (when the mbuf is contiguous) or, otherwise,
352 		 * a pointer to temp_data after copying into it.
353 		 */
354 		pcap_dump((u_char *)dumper, &header,
355 			rte_pktmbuf_read(mbuf, 0, caplen, temp_data));
356 
357 		num_tx++;
358 		tx_bytes += caplen;
359 		rte_pktmbuf_free(mbuf);
360 	}
361 
362 	/*
363 	 * Since there's no place to hook a callback when the forwarding
364 	 * process stops and to make sure the pcap file is actually written,
365 	 * we flush the pcap dumper within each burst.
366 	 */
367 	pcap_dump_flush(dumper);
368 	dumper_q->tx_stat.pkts += num_tx;
369 	dumper_q->tx_stat.bytes += tx_bytes;
370 	dumper_q->tx_stat.err_pkts += nb_pkts - num_tx;
371 
372 	return nb_pkts;
373 }
374 
375 /*
376  * Callback to handle dropping packets in the infinite rx case.
377  */
378 static uint16_t
eth_tx_drop(void * queue,struct rte_mbuf ** bufs,uint16_t nb_pkts)379 eth_tx_drop(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
380 {
381 	unsigned int i;
382 	uint32_t tx_bytes = 0;
383 	struct pcap_tx_queue *tx_queue = queue;
384 
385 	if (unlikely(nb_pkts == 0))
386 		return 0;
387 
388 	for (i = 0; i < nb_pkts; i++) {
389 		tx_bytes += bufs[i]->data_len;
390 		rte_pktmbuf_free(bufs[i]);
391 	}
392 
393 	tx_queue->tx_stat.pkts += nb_pkts;
394 	tx_queue->tx_stat.bytes += tx_bytes;
395 
396 	return i;
397 }
398 
399 /*
400  * Callback to handle sending packets through a real NIC.
401  */
402 static uint16_t
eth_pcap_tx(void * queue,struct rte_mbuf ** bufs,uint16_t nb_pkts)403 eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
404 {
405 	unsigned int i;
406 	int ret;
407 	struct rte_mbuf *mbuf;
408 	struct pmd_process_private *pp;
409 	struct pcap_tx_queue *tx_queue = queue;
410 	uint16_t num_tx = 0;
411 	uint32_t tx_bytes = 0;
412 	pcap_t *pcap;
413 	unsigned char temp_data[RTE_ETH_PCAP_SNAPLEN];
414 	size_t len;
415 
416 	pp = rte_eth_devices[tx_queue->port_id].process_private;
417 	pcap = pp->tx_pcap[tx_queue->queue_id];
418 
419 	if (unlikely(nb_pkts == 0 || pcap == NULL))
420 		return 0;
421 
422 	for (i = 0; i < nb_pkts; i++) {
423 		mbuf = bufs[i];
424 		len = rte_pktmbuf_pkt_len(mbuf);
425 		if (unlikely(!rte_pktmbuf_is_contiguous(mbuf) &&
426 				len > sizeof(temp_data))) {
427 			PMD_LOG(ERR,
428 				"Dropping multi segment PCAP packet. Size (%zd) > max size (%zd).",
429 				len, sizeof(temp_data));
430 			rte_pktmbuf_free(mbuf);
431 			continue;
432 		}
433 
434 		/* rte_pktmbuf_read() returns a pointer to the data directly
435 		 * in the mbuf (when the mbuf is contiguous) or, otherwise,
436 		 * a pointer to temp_data after copying into it.
437 		 */
438 		ret = pcap_sendpacket(pcap,
439 			rte_pktmbuf_read(mbuf, 0, len, temp_data), len);
440 		if (unlikely(ret != 0))
441 			break;
442 		num_tx++;
443 		tx_bytes += len;
444 		rte_pktmbuf_free(mbuf);
445 	}
446 
447 	tx_queue->tx_stat.pkts += num_tx;
448 	tx_queue->tx_stat.bytes += tx_bytes;
449 	tx_queue->tx_stat.err_pkts += i - num_tx;
450 
451 	return i;
452 }
453 
454 /*
455  * pcap_open_live wrapper function
456  */
457 static inline int
open_iface_live(const char * iface,pcap_t ** pcap)458 open_iface_live(const char *iface, pcap_t **pcap) {
459 	*pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
460 			RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
461 
462 	if (*pcap == NULL) {
463 		PMD_LOG(ERR, "Couldn't open %s: %s", iface, errbuf);
464 		return -1;
465 	}
466 
467 	return 0;
468 }
469 
470 static int
open_single_iface(const char * iface,pcap_t ** pcap)471 open_single_iface(const char *iface, pcap_t **pcap)
472 {
473 	if (open_iface_live(iface, pcap) < 0) {
474 		PMD_LOG(ERR, "Couldn't open interface %s", iface);
475 		return -1;
476 	}
477 
478 	return 0;
479 }
480 
481 static int
open_single_tx_pcap(const char * pcap_filename,pcap_dumper_t ** dumper)482 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
483 {
484 	pcap_t *tx_pcap;
485 
486 	/*
487 	 * We need to create a dummy empty pcap_t to use it
488 	 * with pcap_dump_open(). We create big enough an Ethernet
489 	 * pcap holder.
490 	 */
491 	tx_pcap = pcap_open_dead_with_tstamp_precision(DLT_EN10MB,
492 			RTE_ETH_PCAP_SNAPSHOT_LEN, PCAP_TSTAMP_PRECISION_NANO);
493 	if (tx_pcap == NULL) {
494 		PMD_LOG(ERR, "Couldn't create dead pcap");
495 		return -1;
496 	}
497 
498 	/* The dumper is created using the previous pcap_t reference */
499 	*dumper = pcap_dump_open(tx_pcap, pcap_filename);
500 	if (*dumper == NULL) {
501 		pcap_close(tx_pcap);
502 		PMD_LOG(ERR, "Couldn't open %s for writing.",
503 			pcap_filename);
504 		return -1;
505 	}
506 
507 	pcap_close(tx_pcap);
508 	return 0;
509 }
510 
511 static int
open_single_rx_pcap(const char * pcap_filename,pcap_t ** pcap)512 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
513 {
514 	*pcap = pcap_open_offline(pcap_filename, errbuf);
515 	if (*pcap == NULL) {
516 		PMD_LOG(ERR, "Couldn't open %s: %s", pcap_filename,
517 			errbuf);
518 		return -1;
519 	}
520 
521 	return 0;
522 }
523 
524 static uint64_t
count_packets_in_pcap(pcap_t ** pcap,struct pcap_rx_queue * pcap_q)525 count_packets_in_pcap(pcap_t **pcap, struct pcap_rx_queue *pcap_q)
526 {
527 	const u_char *packet;
528 	struct pcap_pkthdr header;
529 	uint64_t pcap_pkt_count = 0;
530 
531 	while ((packet = pcap_next(*pcap, &header)))
532 		pcap_pkt_count++;
533 
534 	/* The pcap is reopened so it can be used as normal later. */
535 	pcap_close(*pcap);
536 	*pcap = NULL;
537 	open_single_rx_pcap(pcap_q->name, pcap);
538 
539 	return pcap_pkt_count;
540 }
541 
542 static int
eth_dev_start(struct rte_eth_dev * dev)543 eth_dev_start(struct rte_eth_dev *dev)
544 {
545 	unsigned int i;
546 	struct pmd_internals *internals = dev->data->dev_private;
547 	struct pmd_process_private *pp = dev->process_private;
548 	struct pcap_tx_queue *tx;
549 	struct pcap_rx_queue *rx;
550 
551 	/* Special iface case. Single pcap is open and shared between tx/rx. */
552 	if (internals->single_iface) {
553 		tx = &internals->tx_queue[0];
554 		rx = &internals->rx_queue[0];
555 
556 		if (!pp->tx_pcap[0] &&
557 			strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
558 			if (open_single_iface(tx->name, &pp->tx_pcap[0]) < 0)
559 				return -1;
560 			pp->rx_pcap[0] = pp->tx_pcap[0];
561 		}
562 
563 		goto status_up;
564 	}
565 
566 	/* If not open already, open tx pcaps/dumpers */
567 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
568 		tx = &internals->tx_queue[i];
569 
570 		if (!pp->tx_dumper[i] &&
571 				strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
572 			if (open_single_tx_pcap(tx->name,
573 				&pp->tx_dumper[i]) < 0)
574 				return -1;
575 		} else if (!pp->tx_pcap[i] &&
576 				strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
577 			if (open_single_iface(tx->name, &pp->tx_pcap[i]) < 0)
578 				return -1;
579 		}
580 	}
581 
582 	/* If not open already, open rx pcaps */
583 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
584 		rx = &internals->rx_queue[i];
585 
586 		if (pp->rx_pcap[i] != NULL)
587 			continue;
588 
589 		if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
590 			if (open_single_rx_pcap(rx->name, &pp->rx_pcap[i]) < 0)
591 				return -1;
592 		} else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
593 			if (open_single_iface(rx->name, &pp->rx_pcap[i]) < 0)
594 				return -1;
595 		}
596 	}
597 
598 status_up:
599 	for (i = 0; i < dev->data->nb_rx_queues; i++)
600 		dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
601 
602 	for (i = 0; i < dev->data->nb_tx_queues; i++)
603 		dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
604 
605 	dev->data->dev_link.link_status = ETH_LINK_UP;
606 
607 	return 0;
608 }
609 
610 /*
611  * This function gets called when the current port gets stopped.
612  * Is the only place for us to close all the tx streams dumpers.
613  * If not called the dumpers will be flushed within each tx burst.
614  */
615 static int
eth_dev_stop(struct rte_eth_dev * dev)616 eth_dev_stop(struct rte_eth_dev *dev)
617 {
618 	unsigned int i;
619 	struct pmd_internals *internals = dev->data->dev_private;
620 	struct pmd_process_private *pp = dev->process_private;
621 
622 	/* Special iface case. Single pcap is open and shared between tx/rx. */
623 	if (internals->single_iface) {
624 		pcap_close(pp->tx_pcap[0]);
625 		pp->tx_pcap[0] = NULL;
626 		pp->rx_pcap[0] = NULL;
627 		goto status_down;
628 	}
629 
630 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
631 		if (pp->tx_dumper[i] != NULL) {
632 			pcap_dump_close(pp->tx_dumper[i]);
633 			pp->tx_dumper[i] = NULL;
634 		}
635 
636 		if (pp->tx_pcap[i] != NULL) {
637 			pcap_close(pp->tx_pcap[i]);
638 			pp->tx_pcap[i] = NULL;
639 		}
640 	}
641 
642 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
643 		if (pp->rx_pcap[i] != NULL) {
644 			pcap_close(pp->rx_pcap[i]);
645 			pp->rx_pcap[i] = NULL;
646 		}
647 	}
648 
649 status_down:
650 	for (i = 0; i < dev->data->nb_rx_queues; i++)
651 		dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
652 
653 	for (i = 0; i < dev->data->nb_tx_queues; i++)
654 		dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
655 
656 	dev->data->dev_link.link_status = ETH_LINK_DOWN;
657 
658 	return 0;
659 }
660 
661 static int
eth_dev_configure(struct rte_eth_dev * dev __rte_unused)662 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
663 {
664 	return 0;
665 }
666 
667 static int
eth_dev_info(struct rte_eth_dev * dev,struct rte_eth_dev_info * dev_info)668 eth_dev_info(struct rte_eth_dev *dev,
669 		struct rte_eth_dev_info *dev_info)
670 {
671 	struct pmd_internals *internals = dev->data->dev_private;
672 
673 	dev_info->if_index = internals->if_index;
674 	dev_info->max_mac_addrs = 1;
675 	dev_info->max_rx_pktlen = (uint32_t) -1;
676 	dev_info->max_rx_queues = dev->data->nb_rx_queues;
677 	dev_info->max_tx_queues = dev->data->nb_tx_queues;
678 	dev_info->min_rx_bufsize = 0;
679 
680 	return 0;
681 }
682 
683 static int
eth_stats_get(struct rte_eth_dev * dev,struct rte_eth_stats * stats)684 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
685 {
686 	unsigned int i;
687 	unsigned long rx_packets_total = 0, rx_bytes_total = 0;
688 	unsigned long tx_packets_total = 0, tx_bytes_total = 0;
689 	unsigned long tx_packets_err_total = 0;
690 	const struct pmd_internals *internal = dev->data->dev_private;
691 
692 	for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
693 			i < dev->data->nb_rx_queues; i++) {
694 		stats->q_ipackets[i] = internal->rx_queue[i].rx_stat.pkts;
695 		stats->q_ibytes[i] = internal->rx_queue[i].rx_stat.bytes;
696 		rx_packets_total += stats->q_ipackets[i];
697 		rx_bytes_total += stats->q_ibytes[i];
698 	}
699 
700 	for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
701 			i < dev->data->nb_tx_queues; i++) {
702 		stats->q_opackets[i] = internal->tx_queue[i].tx_stat.pkts;
703 		stats->q_obytes[i] = internal->tx_queue[i].tx_stat.bytes;
704 		tx_packets_total += stats->q_opackets[i];
705 		tx_bytes_total += stats->q_obytes[i];
706 		tx_packets_err_total += internal->tx_queue[i].tx_stat.err_pkts;
707 	}
708 
709 	stats->ipackets = rx_packets_total;
710 	stats->ibytes = rx_bytes_total;
711 	stats->opackets = tx_packets_total;
712 	stats->obytes = tx_bytes_total;
713 	stats->oerrors = tx_packets_err_total;
714 
715 	return 0;
716 }
717 
718 static int
eth_stats_reset(struct rte_eth_dev * dev)719 eth_stats_reset(struct rte_eth_dev *dev)
720 {
721 	unsigned int i;
722 	struct pmd_internals *internal = dev->data->dev_private;
723 
724 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
725 		internal->rx_queue[i].rx_stat.pkts = 0;
726 		internal->rx_queue[i].rx_stat.bytes = 0;
727 	}
728 
729 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
730 		internal->tx_queue[i].tx_stat.pkts = 0;
731 		internal->tx_queue[i].tx_stat.bytes = 0;
732 		internal->tx_queue[i].tx_stat.err_pkts = 0;
733 	}
734 
735 	return 0;
736 }
737 
738 static int
eth_dev_close(struct rte_eth_dev * dev)739 eth_dev_close(struct rte_eth_dev *dev)
740 {
741 	unsigned int i;
742 	struct pmd_internals *internals = dev->data->dev_private;
743 
744 	PMD_LOG(INFO, "Closing pcap ethdev on NUMA socket %d",
745 			rte_socket_id());
746 
747 	rte_free(dev->process_private);
748 
749 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
750 		return 0;
751 
752 	/* Device wide flag, but cleanup must be performed per queue. */
753 	if (internals->infinite_rx) {
754 		for (i = 0; i < dev->data->nb_rx_queues; i++) {
755 			struct pcap_rx_queue *pcap_q = &internals->rx_queue[i];
756 			struct rte_mbuf *pcap_buf;
757 
758 			/*
759 			 * 'pcap_q->pkts' can be NULL if 'eth_dev_close()'
760 			 * called before 'eth_rx_queue_setup()' has been called
761 			 */
762 			if (pcap_q->pkts == NULL)
763 				continue;
764 
765 			while (!rte_ring_dequeue(pcap_q->pkts,
766 					(void **)&pcap_buf))
767 				rte_pktmbuf_free(pcap_buf);
768 
769 			rte_ring_free(pcap_q->pkts);
770 		}
771 	}
772 
773 	if (internals->phy_mac == 0)
774 		/* not dynamically allocated, must not be freed */
775 		dev->data->mac_addrs = NULL;
776 
777 	return 0;
778 }
779 
780 static void
eth_queue_release(void * q __rte_unused)781 eth_queue_release(void *q __rte_unused)
782 {
783 }
784 
785 static int
eth_link_update(struct rte_eth_dev * dev __rte_unused,int wait_to_complete __rte_unused)786 eth_link_update(struct rte_eth_dev *dev __rte_unused,
787 		int wait_to_complete __rte_unused)
788 {
789 	return 0;
790 }
791 
792 static int
eth_rx_queue_setup(struct rte_eth_dev * dev,uint16_t rx_queue_id,uint16_t nb_rx_desc __rte_unused,unsigned int socket_id __rte_unused,const struct rte_eth_rxconf * rx_conf __rte_unused,struct rte_mempool * mb_pool)793 eth_rx_queue_setup(struct rte_eth_dev *dev,
794 		uint16_t rx_queue_id,
795 		uint16_t nb_rx_desc __rte_unused,
796 		unsigned int socket_id __rte_unused,
797 		const struct rte_eth_rxconf *rx_conf __rte_unused,
798 		struct rte_mempool *mb_pool)
799 {
800 	struct pmd_internals *internals = dev->data->dev_private;
801 	struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
802 
803 	pcap_q->mb_pool = mb_pool;
804 	pcap_q->port_id = dev->data->port_id;
805 	pcap_q->queue_id = rx_queue_id;
806 	dev->data->rx_queues[rx_queue_id] = pcap_q;
807 
808 	if (internals->infinite_rx) {
809 		struct pmd_process_private *pp;
810 		char ring_name[NAME_MAX];
811 		static uint32_t ring_number;
812 		uint64_t pcap_pkt_count = 0;
813 		struct rte_mbuf *bufs[1];
814 		pcap_t **pcap;
815 
816 		pp = rte_eth_devices[pcap_q->port_id].process_private;
817 		pcap = &pp->rx_pcap[pcap_q->queue_id];
818 
819 		if (unlikely(*pcap == NULL))
820 			return -ENOENT;
821 
822 		pcap_pkt_count = count_packets_in_pcap(pcap, pcap_q);
823 
824 		snprintf(ring_name, sizeof(ring_name), "PCAP_RING%" PRIu16,
825 				ring_number);
826 
827 		pcap_q->pkts = rte_ring_create(ring_name,
828 				rte_align64pow2(pcap_pkt_count + 1), 0,
829 				RING_F_SP_ENQ | RING_F_SC_DEQ);
830 		ring_number++;
831 		if (!pcap_q->pkts)
832 			return -ENOENT;
833 
834 		/* Fill ring with packets from PCAP file one by one. */
835 		while (eth_pcap_rx(pcap_q, bufs, 1)) {
836 			/* Check for multiseg mbufs. */
837 			if (bufs[0]->nb_segs != 1) {
838 				rte_pktmbuf_free(*bufs);
839 
840 				while (!rte_ring_dequeue(pcap_q->pkts,
841 						(void **)bufs))
842 					rte_pktmbuf_free(*bufs);
843 
844 				rte_ring_free(pcap_q->pkts);
845 				PMD_LOG(ERR, "Multiseg mbufs are not supported in infinite_rx "
846 						"mode.");
847 				return -EINVAL;
848 			}
849 
850 			rte_ring_enqueue_bulk(pcap_q->pkts,
851 					(void * const *)bufs, 1, NULL);
852 		}
853 		/*
854 		 * Reset the stats for this queue since eth_pcap_rx calls above
855 		 * didn't result in the application receiving packets.
856 		 */
857 		pcap_q->rx_stat.pkts = 0;
858 		pcap_q->rx_stat.bytes = 0;
859 	}
860 
861 	return 0;
862 }
863 
864 static int
eth_tx_queue_setup(struct rte_eth_dev * dev,uint16_t tx_queue_id,uint16_t nb_tx_desc __rte_unused,unsigned int socket_id __rte_unused,const struct rte_eth_txconf * tx_conf __rte_unused)865 eth_tx_queue_setup(struct rte_eth_dev *dev,
866 		uint16_t tx_queue_id,
867 		uint16_t nb_tx_desc __rte_unused,
868 		unsigned int socket_id __rte_unused,
869 		const struct rte_eth_txconf *tx_conf __rte_unused)
870 {
871 	struct pmd_internals *internals = dev->data->dev_private;
872 	struct pcap_tx_queue *pcap_q = &internals->tx_queue[tx_queue_id];
873 
874 	pcap_q->port_id = dev->data->port_id;
875 	pcap_q->queue_id = tx_queue_id;
876 	dev->data->tx_queues[tx_queue_id] = pcap_q;
877 
878 	return 0;
879 }
880 
881 static int
eth_rx_queue_start(struct rte_eth_dev * dev,uint16_t rx_queue_id)882 eth_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
883 {
884 	dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
885 
886 	return 0;
887 }
888 
889 static int
eth_tx_queue_start(struct rte_eth_dev * dev,uint16_t tx_queue_id)890 eth_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
891 {
892 	dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
893 
894 	return 0;
895 }
896 
897 static int
eth_rx_queue_stop(struct rte_eth_dev * dev,uint16_t rx_queue_id)898 eth_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
899 {
900 	dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
901 
902 	return 0;
903 }
904 
905 static int
eth_tx_queue_stop(struct rte_eth_dev * dev,uint16_t tx_queue_id)906 eth_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
907 {
908 	dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
909 
910 	return 0;
911 }
912 
913 static const struct eth_dev_ops ops = {
914 	.dev_start = eth_dev_start,
915 	.dev_stop = eth_dev_stop,
916 	.dev_close = eth_dev_close,
917 	.dev_configure = eth_dev_configure,
918 	.dev_infos_get = eth_dev_info,
919 	.rx_queue_setup = eth_rx_queue_setup,
920 	.tx_queue_setup = eth_tx_queue_setup,
921 	.rx_queue_start = eth_rx_queue_start,
922 	.tx_queue_start = eth_tx_queue_start,
923 	.rx_queue_stop = eth_rx_queue_stop,
924 	.tx_queue_stop = eth_tx_queue_stop,
925 	.rx_queue_release = eth_queue_release,
926 	.tx_queue_release = eth_queue_release,
927 	.link_update = eth_link_update,
928 	.stats_get = eth_stats_get,
929 	.stats_reset = eth_stats_reset,
930 };
931 
932 static int
add_queue(struct pmd_devargs * pmd,const char * name,const char * type,pcap_t * pcap,pcap_dumper_t * dumper)933 add_queue(struct pmd_devargs *pmd, const char *name, const char *type,
934 		pcap_t *pcap, pcap_dumper_t *dumper)
935 {
936 	if (pmd->num_of_queue >= RTE_PMD_PCAP_MAX_QUEUES)
937 		return -1;
938 	if (pcap)
939 		pmd->queue[pmd->num_of_queue].pcap = pcap;
940 	if (dumper)
941 		pmd->queue[pmd->num_of_queue].dumper = dumper;
942 	pmd->queue[pmd->num_of_queue].name = name;
943 	pmd->queue[pmd->num_of_queue].type = type;
944 	pmd->num_of_queue++;
945 	return 0;
946 }
947 
948 /*
949  * Function handler that opens the pcap file for reading a stores a
950  * reference of it for use it later on.
951  */
952 static int
open_rx_pcap(const char * key,const char * value,void * extra_args)953 open_rx_pcap(const char *key, const char *value, void *extra_args)
954 {
955 	const char *pcap_filename = value;
956 	struct pmd_devargs *rx = extra_args;
957 	pcap_t *pcap = NULL;
958 
959 	if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
960 		return -1;
961 
962 	if (add_queue(rx, pcap_filename, key, pcap, NULL) < 0) {
963 		pcap_close(pcap);
964 		return -1;
965 	}
966 
967 	return 0;
968 }
969 
970 /*
971  * Opens a pcap file for writing and stores a reference to it
972  * for use it later on.
973  */
974 static int
open_tx_pcap(const char * key,const char * value,void * extra_args)975 open_tx_pcap(const char *key, const char *value, void *extra_args)
976 {
977 	const char *pcap_filename = value;
978 	struct pmd_devargs *dumpers = extra_args;
979 	pcap_dumper_t *dumper;
980 
981 	if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
982 		return -1;
983 
984 	if (add_queue(dumpers, pcap_filename, key, NULL, dumper) < 0) {
985 		pcap_dump_close(dumper);
986 		return -1;
987 	}
988 
989 	return 0;
990 }
991 
992 /*
993  * Opens an interface for reading and writing
994  */
995 static inline int
open_rx_tx_iface(const char * key,const char * value,void * extra_args)996 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
997 {
998 	const char *iface = value;
999 	struct pmd_devargs *tx = extra_args;
1000 	pcap_t *pcap = NULL;
1001 
1002 	if (open_single_iface(iface, &pcap) < 0)
1003 		return -1;
1004 
1005 	tx->queue[0].pcap = pcap;
1006 	tx->queue[0].name = iface;
1007 	tx->queue[0].type = key;
1008 
1009 	return 0;
1010 }
1011 
1012 static inline int
set_iface_direction(const char * iface,pcap_t * pcap,pcap_direction_t direction)1013 set_iface_direction(const char *iface, pcap_t *pcap,
1014 		pcap_direction_t direction)
1015 {
1016 	const char *direction_str = (direction == PCAP_D_IN) ? "IN" : "OUT";
1017 	if (pcap_setdirection(pcap, direction) < 0) {
1018 		PMD_LOG(ERR, "Setting %s pcap direction %s failed - %s\n",
1019 				iface, direction_str, pcap_geterr(pcap));
1020 		return -1;
1021 	}
1022 	PMD_LOG(INFO, "Setting %s pcap direction %s\n",
1023 			iface, direction_str);
1024 	return 0;
1025 }
1026 
1027 static inline int
open_iface(const char * key,const char * value,void * extra_args)1028 open_iface(const char *key, const char *value, void *extra_args)
1029 {
1030 	const char *iface = value;
1031 	struct pmd_devargs *pmd = extra_args;
1032 	pcap_t *pcap = NULL;
1033 
1034 	if (open_single_iface(iface, &pcap) < 0)
1035 		return -1;
1036 	if (add_queue(pmd, iface, key, pcap, NULL) < 0) {
1037 		pcap_close(pcap);
1038 		return -1;
1039 	}
1040 
1041 	return 0;
1042 }
1043 
1044 /*
1045  * Opens a NIC for reading packets from it
1046  */
1047 static inline int
open_rx_iface(const char * key,const char * value,void * extra_args)1048 open_rx_iface(const char *key, const char *value, void *extra_args)
1049 {
1050 	int ret = open_iface(key, value, extra_args);
1051 	if (ret < 0)
1052 		return ret;
1053 	if (strcmp(key, ETH_PCAP_RX_IFACE_IN_ARG) == 0) {
1054 		struct pmd_devargs *pmd = extra_args;
1055 		unsigned int qid = pmd->num_of_queue - 1;
1056 
1057 		set_iface_direction(pmd->queue[qid].name,
1058 				pmd->queue[qid].pcap,
1059 				PCAP_D_IN);
1060 	}
1061 
1062 	return 0;
1063 }
1064 
1065 static inline int
rx_iface_args_process(const char * key,const char * value,void * extra_args)1066 rx_iface_args_process(const char *key, const char *value, void *extra_args)
1067 {
1068 	if (strcmp(key, ETH_PCAP_RX_IFACE_ARG) == 0 ||
1069 			strcmp(key, ETH_PCAP_RX_IFACE_IN_ARG) == 0)
1070 		return open_rx_iface(key, value, extra_args);
1071 
1072 	return 0;
1073 }
1074 
1075 /*
1076  * Opens a NIC for writing packets to it
1077  */
1078 static int
open_tx_iface(const char * key,const char * value,void * extra_args)1079 open_tx_iface(const char *key, const char *value, void *extra_args)
1080 {
1081 	return open_iface(key, value, extra_args);
1082 }
1083 
1084 static int
select_phy_mac(const char * key __rte_unused,const char * value,void * extra_args)1085 select_phy_mac(const char *key __rte_unused, const char *value,
1086 		void *extra_args)
1087 {
1088 	if (extra_args) {
1089 		const int phy_mac = atoi(value);
1090 		int *enable_phy_mac = extra_args;
1091 
1092 		if (phy_mac)
1093 			*enable_phy_mac = 1;
1094 	}
1095 	return 0;
1096 }
1097 
1098 static int
get_infinite_rx_arg(const char * key __rte_unused,const char * value,void * extra_args)1099 get_infinite_rx_arg(const char *key __rte_unused,
1100 		const char *value, void *extra_args)
1101 {
1102 	if (extra_args) {
1103 		const int infinite_rx = atoi(value);
1104 		int *enable_infinite_rx = extra_args;
1105 
1106 		if (infinite_rx > 0)
1107 			*enable_infinite_rx = 1;
1108 	}
1109 	return 0;
1110 }
1111 
1112 static int
pmd_init_internals(struct rte_vdev_device * vdev,const unsigned int nb_rx_queues,const unsigned int nb_tx_queues,struct pmd_internals ** internals,struct rte_eth_dev ** eth_dev)1113 pmd_init_internals(struct rte_vdev_device *vdev,
1114 		const unsigned int nb_rx_queues,
1115 		const unsigned int nb_tx_queues,
1116 		struct pmd_internals **internals,
1117 		struct rte_eth_dev **eth_dev)
1118 {
1119 	struct rte_eth_dev_data *data;
1120 	struct pmd_process_private *pp;
1121 	unsigned int numa_node = vdev->device.numa_node;
1122 
1123 	PMD_LOG(INFO, "Creating pcap-backed ethdev on numa socket %d",
1124 		numa_node);
1125 
1126 	pp = (struct pmd_process_private *)
1127 		rte_zmalloc(NULL, sizeof(struct pmd_process_private),
1128 				RTE_CACHE_LINE_SIZE);
1129 
1130 	if (pp == NULL) {
1131 		PMD_LOG(ERR,
1132 			"Failed to allocate memory for process private");
1133 		return -1;
1134 	}
1135 
1136 	/* reserve an ethdev entry */
1137 	*eth_dev = rte_eth_vdev_allocate(vdev, sizeof(**internals));
1138 	if (!(*eth_dev)) {
1139 		rte_free(pp);
1140 		return -1;
1141 	}
1142 	(*eth_dev)->process_private = pp;
1143 	/* now put it all together
1144 	 * - store queue data in internals,
1145 	 * - store numa_node info in eth_dev
1146 	 * - point eth_dev_data to internals
1147 	 * - and point eth_dev structure to new eth_dev_data structure
1148 	 */
1149 	*internals = (*eth_dev)->data->dev_private;
1150 	/*
1151 	 * Interface MAC = 02:70:63:61:70:<iface_idx>
1152 	 * derived from: 'locally administered':'p':'c':'a':'p':'iface_idx'
1153 	 * where the middle 4 characters are converted to hex.
1154 	 */
1155 	(*internals)->eth_addr = (struct rte_ether_addr) {
1156 		.addr_bytes = { 0x02, 0x70, 0x63, 0x61, 0x70, iface_idx++ }
1157 	};
1158 	(*internals)->phy_mac = 0;
1159 	data = (*eth_dev)->data;
1160 	data->nb_rx_queues = (uint16_t)nb_rx_queues;
1161 	data->nb_tx_queues = (uint16_t)nb_tx_queues;
1162 	data->dev_link = pmd_link;
1163 	data->mac_addrs = &(*internals)->eth_addr;
1164 	data->promiscuous = 1;
1165 	data->all_multicast = 1;
1166 	data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1167 
1168 	/*
1169 	 * NOTE: we'll replace the data element, of originally allocated
1170 	 * eth_dev so the rings are local per-process
1171 	 */
1172 	(*eth_dev)->dev_ops = &ops;
1173 
1174 	strlcpy((*internals)->devargs, rte_vdev_device_args(vdev),
1175 			ETH_PCAP_ARG_MAXLEN);
1176 
1177 	return 0;
1178 }
1179 
1180 static int
eth_pcap_update_mac(const char * if_name,struct rte_eth_dev * eth_dev,const unsigned int numa_node)1181 eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,
1182 		const unsigned int numa_node)
1183 {
1184 #if defined(RTE_EXEC_ENV_LINUX)
1185 	void *mac_addrs;
1186 	struct ifreq ifr;
1187 	int if_fd = socket(AF_INET, SOCK_DGRAM, 0);
1188 
1189 	if (if_fd == -1)
1190 		return -1;
1191 
1192 	rte_strscpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name));
1193 	if (ioctl(if_fd, SIOCGIFHWADDR, &ifr)) {
1194 		close(if_fd);
1195 		return -1;
1196 	}
1197 
1198 	mac_addrs = rte_zmalloc_socket(NULL, RTE_ETHER_ADDR_LEN, 0, numa_node);
1199 	if (!mac_addrs) {
1200 		close(if_fd);
1201 		return -1;
1202 	}
1203 
1204 	PMD_LOG(INFO, "Setting phy MAC for %s", if_name);
1205 	eth_dev->data->mac_addrs = mac_addrs;
1206 	rte_memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
1207 			ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
1208 
1209 	close(if_fd);
1210 
1211 	return 0;
1212 
1213 #elif defined(RTE_EXEC_ENV_FREEBSD)
1214 	void *mac_addrs;
1215 	struct if_msghdr *ifm;
1216 	struct sockaddr_dl *sdl;
1217 	int mib[6];
1218 	size_t len = 0;
1219 	char *buf;
1220 
1221 	mib[0] = CTL_NET;
1222 	mib[1] = AF_ROUTE;
1223 	mib[2] = 0;
1224 	mib[3] = AF_LINK;
1225 	mib[4] = NET_RT_IFLIST;
1226 	mib[5] = if_nametoindex(if_name);
1227 
1228 	if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
1229 		return -1;
1230 
1231 	if (len == 0)
1232 		return -1;
1233 
1234 	buf = rte_malloc(NULL, len, 0);
1235 	if (!buf)
1236 		return -1;
1237 
1238 	if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
1239 		rte_free(buf);
1240 		return -1;
1241 	}
1242 	ifm = (struct if_msghdr *)buf;
1243 	sdl = (struct sockaddr_dl *)(ifm + 1);
1244 
1245 	mac_addrs = rte_zmalloc_socket(NULL, RTE_ETHER_ADDR_LEN, 0, numa_node);
1246 	if (!mac_addrs) {
1247 		rte_free(buf);
1248 		return -1;
1249 	}
1250 
1251 	PMD_LOG(INFO, "Setting phy MAC for %s", if_name);
1252 	eth_dev->data->mac_addrs = mac_addrs;
1253 	rte_memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
1254 			LLADDR(sdl), RTE_ETHER_ADDR_LEN);
1255 
1256 	rte_free(buf);
1257 
1258 	return 0;
1259 #else
1260 	return -1;
1261 #endif
1262 }
1263 
1264 static int
eth_from_pcaps_common(struct rte_vdev_device * vdev,struct pmd_devargs_all * devargs_all,struct pmd_internals ** internals,struct rte_eth_dev ** eth_dev)1265 eth_from_pcaps_common(struct rte_vdev_device *vdev,
1266 		struct pmd_devargs_all *devargs_all,
1267 		struct pmd_internals **internals, struct rte_eth_dev **eth_dev)
1268 {
1269 	struct pmd_process_private *pp;
1270 	struct pmd_devargs *rx_queues = &devargs_all->rx_queues;
1271 	struct pmd_devargs *tx_queues = &devargs_all->tx_queues;
1272 	const unsigned int nb_rx_queues = rx_queues->num_of_queue;
1273 	const unsigned int nb_tx_queues = tx_queues->num_of_queue;
1274 	unsigned int i;
1275 
1276 	if (pmd_init_internals(vdev, nb_rx_queues, nb_tx_queues, internals,
1277 			eth_dev) < 0)
1278 		return -1;
1279 
1280 	pp = (*eth_dev)->process_private;
1281 	for (i = 0; i < nb_rx_queues; i++) {
1282 		struct pcap_rx_queue *rx = &(*internals)->rx_queue[i];
1283 		struct devargs_queue *queue = &rx_queues->queue[i];
1284 
1285 		pp->rx_pcap[i] = queue->pcap;
1286 		strlcpy(rx->name, queue->name, sizeof(rx->name));
1287 		strlcpy(rx->type, queue->type, sizeof(rx->type));
1288 	}
1289 
1290 	for (i = 0; i < nb_tx_queues; i++) {
1291 		struct pcap_tx_queue *tx = &(*internals)->tx_queue[i];
1292 		struct devargs_queue *queue = &tx_queues->queue[i];
1293 
1294 		pp->tx_dumper[i] = queue->dumper;
1295 		pp->tx_pcap[i] = queue->pcap;
1296 		strlcpy(tx->name, queue->name, sizeof(tx->name));
1297 		strlcpy(tx->type, queue->type, sizeof(tx->type));
1298 	}
1299 
1300 	return 0;
1301 }
1302 
1303 static int
eth_from_pcaps(struct rte_vdev_device * vdev,struct pmd_devargs_all * devargs_all)1304 eth_from_pcaps(struct rte_vdev_device *vdev,
1305 		struct pmd_devargs_all *devargs_all)
1306 {
1307 	struct pmd_internals *internals = NULL;
1308 	struct rte_eth_dev *eth_dev = NULL;
1309 	struct pmd_devargs *rx_queues = &devargs_all->rx_queues;
1310 	int single_iface = devargs_all->single_iface;
1311 	unsigned int infinite_rx = devargs_all->infinite_rx;
1312 	int ret;
1313 
1314 	ret = eth_from_pcaps_common(vdev, devargs_all, &internals, &eth_dev);
1315 
1316 	if (ret < 0)
1317 		return ret;
1318 
1319 	/* store weather we are using a single interface for rx/tx or not */
1320 	internals->single_iface = single_iface;
1321 
1322 	if (single_iface) {
1323 		internals->if_index = if_nametoindex(rx_queues->queue[0].name);
1324 
1325 		/* phy_mac arg is applied only only if "iface" devarg is provided */
1326 		if (rx_queues->phy_mac) {
1327 			int ret = eth_pcap_update_mac(rx_queues->queue[0].name,
1328 					eth_dev, vdev->device.numa_node);
1329 			if (ret == 0)
1330 				internals->phy_mac = 1;
1331 		}
1332 	}
1333 
1334 	internals->infinite_rx = infinite_rx;
1335 	/* Assign rx ops. */
1336 	if (infinite_rx)
1337 		eth_dev->rx_pkt_burst = eth_pcap_rx_infinite;
1338 	else if (devargs_all->is_rx_pcap || devargs_all->is_rx_iface ||
1339 			single_iface)
1340 		eth_dev->rx_pkt_burst = eth_pcap_rx;
1341 	else
1342 		eth_dev->rx_pkt_burst = eth_null_rx;
1343 
1344 	/* Assign tx ops. */
1345 	if (devargs_all->is_tx_pcap)
1346 		eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
1347 	else if (devargs_all->is_tx_iface || single_iface)
1348 		eth_dev->tx_pkt_burst = eth_pcap_tx;
1349 	else
1350 		eth_dev->tx_pkt_burst = eth_tx_drop;
1351 
1352 	rte_eth_dev_probing_finish(eth_dev);
1353 	return 0;
1354 }
1355 
1356 static int
pmd_pcap_probe(struct rte_vdev_device * dev)1357 pmd_pcap_probe(struct rte_vdev_device *dev)
1358 {
1359 	const char *name;
1360 	struct rte_kvargs *kvlist;
1361 	struct pmd_devargs pcaps = {0};
1362 	struct pmd_devargs dumpers = {0};
1363 	struct rte_eth_dev *eth_dev =  NULL;
1364 	struct pmd_internals *internal;
1365 	int ret = 0;
1366 
1367 	struct pmd_devargs_all devargs_all = {
1368 		.single_iface = 0,
1369 		.is_tx_pcap = 0,
1370 		.is_tx_iface = 0,
1371 		.infinite_rx = 0,
1372 	};
1373 
1374 	name = rte_vdev_device_name(dev);
1375 	PMD_LOG(INFO, "Initializing pmd_pcap for %s", name);
1376 
1377 	gettimeofday(&start_time, NULL);
1378 	start_cycles = rte_get_timer_cycles();
1379 	hz = rte_get_timer_hz();
1380 
1381 	ret = rte_mbuf_dyn_rx_timestamp_register(&timestamp_dynfield_offset,
1382 			&timestamp_rx_dynflag);
1383 	if (ret != 0) {
1384 		PMD_LOG(ERR, "Failed to register Rx timestamp field/flag");
1385 		return -1;
1386 	}
1387 
1388 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1389 		eth_dev = rte_eth_dev_attach_secondary(name);
1390 		if (!eth_dev) {
1391 			PMD_LOG(ERR, "Failed to probe %s", name);
1392 			return -1;
1393 		}
1394 
1395 		internal = eth_dev->data->dev_private;
1396 
1397 		kvlist = rte_kvargs_parse(internal->devargs, valid_arguments);
1398 		if (kvlist == NULL)
1399 			return -1;
1400 	} else {
1401 		kvlist = rte_kvargs_parse(rte_vdev_device_args(dev),
1402 				valid_arguments);
1403 		if (kvlist == NULL)
1404 			return -1;
1405 	}
1406 
1407 	/*
1408 	 * If iface argument is passed we open the NICs and use them for
1409 	 * reading / writing
1410 	 */
1411 	if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
1412 
1413 		ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
1414 				&open_rx_tx_iface, &pcaps);
1415 		if (ret < 0)
1416 			goto free_kvlist;
1417 
1418 		dumpers.queue[0] = pcaps.queue[0];
1419 
1420 		ret = rte_kvargs_process(kvlist, ETH_PCAP_PHY_MAC_ARG,
1421 				&select_phy_mac, &pcaps.phy_mac);
1422 		if (ret < 0)
1423 			goto free_kvlist;
1424 
1425 		dumpers.phy_mac = pcaps.phy_mac;
1426 
1427 		devargs_all.single_iface = 1;
1428 		pcaps.num_of_queue = 1;
1429 		dumpers.num_of_queue = 1;
1430 
1431 		goto create_eth;
1432 	}
1433 
1434 	/*
1435 	 * We check whether we want to open a RX stream from a real NIC, a
1436 	 * pcap file or open a dummy RX stream
1437 	 */
1438 	devargs_all.is_rx_pcap =
1439 		rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG) ? 1 : 0;
1440 	devargs_all.is_rx_iface =
1441 		(rte_kvargs_count(kvlist, ETH_PCAP_RX_IFACE_ARG) +
1442 		 rte_kvargs_count(kvlist, ETH_PCAP_RX_IFACE_IN_ARG)) ? 1 : 0;
1443 	pcaps.num_of_queue = 0;
1444 
1445 	devargs_all.is_tx_pcap =
1446 		rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG) ? 1 : 0;
1447 	devargs_all.is_tx_iface =
1448 		rte_kvargs_count(kvlist, ETH_PCAP_TX_IFACE_ARG) ? 1 : 0;
1449 	dumpers.num_of_queue = 0;
1450 
1451 	if (devargs_all.is_rx_pcap) {
1452 		/*
1453 		 * We check whether we want to infinitely rx the pcap file.
1454 		 */
1455 		unsigned int infinite_rx_arg_cnt = rte_kvargs_count(kvlist,
1456 				ETH_PCAP_INFINITE_RX_ARG);
1457 
1458 		if (infinite_rx_arg_cnt == 1) {
1459 			ret = rte_kvargs_process(kvlist,
1460 					ETH_PCAP_INFINITE_RX_ARG,
1461 					&get_infinite_rx_arg,
1462 					&devargs_all.infinite_rx);
1463 			if (ret < 0)
1464 				goto free_kvlist;
1465 			PMD_LOG(INFO, "infinite_rx has been %s for %s",
1466 					devargs_all.infinite_rx ? "enabled" : "disabled",
1467 					name);
1468 
1469 		} else if (infinite_rx_arg_cnt > 1) {
1470 			PMD_LOG(WARNING, "infinite_rx has not been enabled since the "
1471 					"argument has been provided more than once "
1472 					"for %s", name);
1473 		}
1474 
1475 		ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
1476 				&open_rx_pcap, &pcaps);
1477 	} else if (devargs_all.is_rx_iface) {
1478 		ret = rte_kvargs_process(kvlist, NULL,
1479 				&rx_iface_args_process, &pcaps);
1480 	} else if (devargs_all.is_tx_iface || devargs_all.is_tx_pcap) {
1481 		unsigned int i;
1482 
1483 		/* Count number of tx queue args passed before dummy rx queue
1484 		 * creation so a dummy rx queue can be created for each tx queue
1485 		 */
1486 		unsigned int num_tx_queues =
1487 			(rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG) +
1488 			rte_kvargs_count(kvlist, ETH_PCAP_TX_IFACE_ARG));
1489 
1490 		PMD_LOG(INFO, "Creating null rx queue since no rx queues were provided.");
1491 
1492 		/* Creating a dummy rx queue for each tx queue passed */
1493 		for (i = 0; i < num_tx_queues; i++)
1494 			ret = add_queue(&pcaps, "dummy_rx", "rx_null", NULL,
1495 					NULL);
1496 	} else {
1497 		PMD_LOG(ERR, "Error - No rx or tx queues provided");
1498 		ret = -ENOENT;
1499 	}
1500 	if (ret < 0)
1501 		goto free_kvlist;
1502 
1503 	/*
1504 	 * We check whether we want to open a TX stream to a real NIC,
1505 	 * a pcap file, or drop packets on tx
1506 	 */
1507 	if (devargs_all.is_tx_pcap) {
1508 		ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1509 				&open_tx_pcap, &dumpers);
1510 	} else if (devargs_all.is_tx_iface) {
1511 		ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1512 				&open_tx_iface, &dumpers);
1513 	} else {
1514 		unsigned int i;
1515 
1516 		PMD_LOG(INFO, "Dropping packets on tx since no tx queues were provided.");
1517 
1518 		/* Add 1 dummy queue per rxq which counts and drops packets. */
1519 		for (i = 0; i < pcaps.num_of_queue; i++)
1520 			ret = add_queue(&dumpers, "dummy_tx", "tx_drop", NULL,
1521 					NULL);
1522 	}
1523 
1524 	if (ret < 0)
1525 		goto free_kvlist;
1526 
1527 create_eth:
1528 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1529 		struct pmd_process_private *pp;
1530 		unsigned int i;
1531 
1532 		internal = eth_dev->data->dev_private;
1533 			pp = (struct pmd_process_private *)
1534 				rte_zmalloc(NULL,
1535 					sizeof(struct pmd_process_private),
1536 					RTE_CACHE_LINE_SIZE);
1537 
1538 		if (pp == NULL) {
1539 			PMD_LOG(ERR,
1540 				"Failed to allocate memory for process private");
1541 			ret = -1;
1542 			goto free_kvlist;
1543 		}
1544 
1545 		eth_dev->dev_ops = &ops;
1546 		eth_dev->device = &dev->device;
1547 
1548 		/* setup process private */
1549 		for (i = 0; i < pcaps.num_of_queue; i++)
1550 			pp->rx_pcap[i] = pcaps.queue[i].pcap;
1551 
1552 		for (i = 0; i < dumpers.num_of_queue; i++) {
1553 			pp->tx_dumper[i] = dumpers.queue[i].dumper;
1554 			pp->tx_pcap[i] = dumpers.queue[i].pcap;
1555 		}
1556 
1557 		eth_dev->process_private = pp;
1558 		eth_dev->rx_pkt_burst = eth_pcap_rx;
1559 		if (devargs_all.is_tx_pcap)
1560 			eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
1561 		else
1562 			eth_dev->tx_pkt_burst = eth_pcap_tx;
1563 
1564 		rte_eth_dev_probing_finish(eth_dev);
1565 		goto free_kvlist;
1566 	}
1567 
1568 	devargs_all.rx_queues = pcaps;
1569 	devargs_all.tx_queues = dumpers;
1570 
1571 	ret = eth_from_pcaps(dev, &devargs_all);
1572 
1573 free_kvlist:
1574 	rte_kvargs_free(kvlist);
1575 
1576 	return ret;
1577 }
1578 
1579 static int
pmd_pcap_remove(struct rte_vdev_device * dev)1580 pmd_pcap_remove(struct rte_vdev_device *dev)
1581 {
1582 	struct rte_eth_dev *eth_dev = NULL;
1583 
1584 	if (!dev)
1585 		return -1;
1586 
1587 	eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1588 	if (eth_dev == NULL)
1589 		return 0; /* port already released */
1590 
1591 	eth_dev_close(eth_dev);
1592 	rte_eth_dev_release_port(eth_dev);
1593 
1594 	return 0;
1595 }
1596 
1597 static struct rte_vdev_driver pmd_pcap_drv = {
1598 	.probe = pmd_pcap_probe,
1599 	.remove = pmd_pcap_remove,
1600 };
1601 
1602 RTE_PMD_REGISTER_VDEV(net_pcap, pmd_pcap_drv);
1603 RTE_PMD_REGISTER_ALIAS(net_pcap, eth_pcap);
1604 RTE_PMD_REGISTER_PARAM_STRING(net_pcap,
1605 	ETH_PCAP_RX_PCAP_ARG "=<string> "
1606 	ETH_PCAP_TX_PCAP_ARG "=<string> "
1607 	ETH_PCAP_RX_IFACE_ARG "=<ifc> "
1608 	ETH_PCAP_RX_IFACE_IN_ARG "=<ifc> "
1609 	ETH_PCAP_TX_IFACE_ARG "=<ifc> "
1610 	ETH_PCAP_IFACE_ARG "=<ifc> "
1611 	ETH_PCAP_PHY_MAC_ARG "=<int>"
1612 	ETH_PCAP_INFINITE_RX_ARG "=<0|1>");
1613