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