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