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