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