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