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