815d4ca0a17c8ef5d26c593c4fece31f86acb3ae
[dpdk.git] / lib / librte_pmd_pcap / rte_eth_pcap.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   Copyright(c) 2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <time.h>
36 #include <rte_mbuf.h>
37 #include <rte_ethdev.h>
38 #include <rte_malloc.h>
39 #include <rte_memcpy.h>
40 #include <rte_string_fns.h>
41 #include <rte_cycles.h>
42 #include <rte_kvargs.h>
43 #include <rte_dev.h>
44
45 #include <net/if.h>
46
47 #include <pcap.h>
48
49 #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535
50 #define RTE_ETH_PCAP_SNAPLEN 4096
51 #define RTE_ETH_PCAP_PROMISC 1
52 #define RTE_ETH_PCAP_TIMEOUT -1
53 #define ETH_PCAP_RX_PCAP_ARG  "rx_pcap"
54 #define ETH_PCAP_TX_PCAP_ARG  "tx_pcap"
55 #define ETH_PCAP_RX_IFACE_ARG "rx_iface"
56 #define ETH_PCAP_TX_IFACE_ARG "tx_iface"
57 #define ETH_PCAP_IFACE_ARG    "iface"
58
59 static char errbuf[PCAP_ERRBUF_SIZE];
60 static struct timeval start_time;
61 static uint64_t start_cycles;
62 static uint64_t hz;
63
64 struct pcap_rx_queue {
65         pcap_t *pcap;
66         uint8_t in_port;
67         struct rte_mempool *mb_pool;
68         volatile unsigned long rx_pkts;
69         volatile unsigned long err_pkts;
70         const char *name;
71         const char *type;
72 };
73
74 struct pcap_tx_queue {
75         pcap_dumper_t *dumper;
76         pcap_t *pcap;
77         volatile unsigned long tx_pkts;
78         volatile unsigned long err_pkts;
79         const char *name;
80         const char *type;
81 };
82
83 struct rx_pcaps {
84         unsigned num_of_rx;
85         pcap_t *pcaps[RTE_PMD_RING_MAX_RX_RINGS];
86         const char *names[RTE_PMD_RING_MAX_RX_RINGS];
87         const char *types[RTE_PMD_RING_MAX_RX_RINGS];
88 };
89
90 struct tx_pcaps {
91         unsigned num_of_tx;
92         pcap_dumper_t *dumpers[RTE_PMD_RING_MAX_TX_RINGS];
93         pcap_t *pcaps[RTE_PMD_RING_MAX_RX_RINGS];
94         const char *names[RTE_PMD_RING_MAX_RX_RINGS];
95         const char *types[RTE_PMD_RING_MAX_RX_RINGS];
96 };
97
98 struct pmd_internals {
99         struct pcap_rx_queue rx_queue[RTE_PMD_RING_MAX_RX_RINGS];
100         struct pcap_tx_queue tx_queue[RTE_PMD_RING_MAX_TX_RINGS];
101         unsigned nb_rx_queues;
102         unsigned nb_tx_queues;
103         int if_index;
104         int single_iface;
105 };
106
107 const char *valid_arguments[] = {
108         ETH_PCAP_RX_PCAP_ARG,
109         ETH_PCAP_TX_PCAP_ARG,
110         ETH_PCAP_RX_IFACE_ARG,
111         ETH_PCAP_TX_IFACE_ARG,
112         ETH_PCAP_IFACE_ARG,
113         NULL
114 };
115
116 static int open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper);
117 static int open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap);
118 static int open_single_iface(const char *iface, pcap_t **pcap);
119
120 static struct ether_addr eth_addr = { .addr_bytes = { 0, 0, 0, 0x1, 0x2, 0x3 } };
121 static const char *drivername = "Pcap PMD";
122 static struct rte_eth_link pmd_link = {
123                 .link_speed = 10000,
124                 .link_duplex = ETH_LINK_FULL_DUPLEX,
125                 .link_status = 0
126 };
127
128
129 static uint16_t
130 eth_pcap_rx(void *queue,
131                 struct rte_mbuf **bufs,
132                 uint16_t nb_pkts)
133 {
134         unsigned i;
135         struct pcap_pkthdr header;
136         const u_char *packet;
137         struct rte_mbuf *mbuf;
138         struct pcap_rx_queue *pcap_q = queue;
139         uint16_t num_rx = 0;
140         uint16_t buf_size;
141
142         if (unlikely(pcap_q->pcap == NULL || nb_pkts == 0))
143                 return 0;
144
145         /* Reads the given number of packets from the pcap file one by one
146          * and copies the packet data into a newly allocated mbuf to return.
147          */
148         for (i = 0; i < nb_pkts; i++) {
149                 /* Get the next PCAP packet */
150                 packet = pcap_next(pcap_q->pcap, &header);
151                 if (unlikely(packet == NULL))
152                         break;
153                 else
154                         mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
155                 if (unlikely(mbuf == NULL))
156                         break;
157
158                 /* Now get the space available for data in the mbuf */
159                 buf_size = (uint16_t)(rte_pktmbuf_data_room_size(pcap_q->mb_pool) -
160                                 RTE_PKTMBUF_HEADROOM);
161
162                 if (header.len <= buf_size) {
163                         /* pcap packet will fit in the mbuf, go ahead and copy */
164                         rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), packet,
165                                         header.len);
166                         mbuf->data_len = (uint16_t)header.len;
167                         mbuf->pkt_len = mbuf->data_len;
168                         mbuf->port = pcap_q->in_port;
169                         bufs[num_rx] = mbuf;
170                         num_rx++;
171                 } else {
172                         /* pcap packet will not fit in the mbuf, so drop packet */
173                         RTE_LOG(ERR, PMD,
174                                         "PCAP packet %d bytes will not fit in mbuf (%d bytes)\n",
175                                         header.len, buf_size);
176                         rte_pktmbuf_free(mbuf);
177                 }
178         }
179         pcap_q->rx_pkts += num_rx;
180         return num_rx;
181 }
182
183 static inline void
184 calculate_timestamp(struct timeval *ts) {
185         uint64_t cycles;
186         struct timeval cur_time;
187
188         cycles = rte_get_timer_cycles() - start_cycles;
189         cur_time.tv_sec = cycles / hz;
190         cur_time.tv_usec = (cycles % hz) * 10e6 / hz;
191         timeradd(&start_time, &cur_time, ts);
192 }
193
194 /*
195  * Callback to handle writing packets to a pcap file.
196  */
197 static uint16_t
198 eth_pcap_tx_dumper(void *queue,
199                 struct rte_mbuf **bufs,
200                 uint16_t nb_pkts)
201 {
202         unsigned i;
203         struct rte_mbuf *mbuf;
204         struct pcap_tx_queue *dumper_q = queue;
205         uint16_t num_tx = 0;
206         struct pcap_pkthdr header;
207
208         if (dumper_q->dumper == NULL || nb_pkts == 0)
209                 return 0;
210
211         /* writes the nb_pkts packets to the previously opened pcap file dumper */
212         for (i = 0; i < nb_pkts; i++) {
213                 mbuf = bufs[i];
214                 calculate_timestamp(&header.ts);
215                 header.len = mbuf->data_len;
216                 header.caplen = header.len;
217                 pcap_dump((u_char *)dumper_q->dumper, &header,
218                                 rte_pktmbuf_mtod(mbuf, void*));
219                 rte_pktmbuf_free(mbuf);
220                 num_tx++;
221         }
222
223         /*
224          * Since there's no place to hook a callback when the forwarding
225          * process stops and to make sure the pcap file is actually written,
226          * we flush the pcap dumper within each burst.
227          */
228         pcap_dump_flush(dumper_q->dumper);
229         dumper_q->tx_pkts += num_tx;
230         dumper_q->err_pkts += nb_pkts - num_tx;
231         return num_tx;
232 }
233
234 /*
235  * Callback to handle sending packets through a real NIC.
236  */
237 static uint16_t
238 eth_pcap_tx(void *queue,
239                 struct rte_mbuf **bufs,
240                 uint16_t nb_pkts)
241 {
242         unsigned i;
243         int ret;
244         struct rte_mbuf *mbuf;
245         struct pcap_tx_queue *tx_queue = queue;
246         uint16_t num_tx = 0;
247
248         if (unlikely(nb_pkts == 0 || tx_queue->pcap == NULL))
249                 return 0;
250
251         for (i = 0; i < nb_pkts; i++) {
252                 mbuf = bufs[i];
253                 ret = pcap_sendpacket(tx_queue->pcap,
254                                 rte_pktmbuf_mtod(mbuf, u_char *),
255                                 mbuf->data_len);
256                 if (unlikely(ret != 0))
257                         break;
258                 num_tx++;
259                 rte_pktmbuf_free(mbuf);
260         }
261
262         tx_queue->tx_pkts += num_tx;
263         tx_queue->err_pkts += nb_pkts - num_tx;
264         return num_tx;
265 }
266
267 static int
268 eth_dev_start(struct rte_eth_dev *dev)
269 {
270         unsigned i;
271         struct pmd_internals *internals = dev->data->dev_private;
272         struct pcap_tx_queue *tx;
273         struct pcap_rx_queue *rx;
274
275         /* Special iface case. Single pcap is open and shared between tx/rx. */
276         if (internals->single_iface) {
277                 tx = &internals->tx_queue[0];
278                 rx = &internals->rx_queue[0];
279
280                 if (!tx->pcap && strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
281                         if (open_single_iface(tx->name, &tx->pcap) < 0)
282                                 return -1;
283                         rx->pcap = tx->pcap;
284                 }
285                 goto status_up;
286         }
287
288         /* If not open already, open tx pcaps/dumpers */
289         for (i = 0; i < internals->nb_tx_queues; i++) {
290                 tx = &internals->tx_queue[i];
291
292                 if (!tx->dumper && strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
293                         if (open_single_tx_pcap(tx->name, &tx->dumper) < 0)
294                                 return -1;
295                 }
296
297                 else if (!tx->pcap && strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
298                         if (open_single_iface(tx->name, &tx->pcap) < 0)
299                                 return -1;
300                 }
301         }
302
303         /* If not open already, open rx pcaps */
304         for (i = 0; i < internals->nb_rx_queues; i++) {
305                 rx = &internals->rx_queue[i];
306
307                 if (rx->pcap != NULL)
308                         continue;
309
310                 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
311                         if (open_single_rx_pcap(rx->name, &rx->pcap) < 0)
312                                 return -1;
313                 }
314
315                 else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
316                         if (open_single_iface(rx->name, &rx->pcap) < 0)
317                                 return -1;
318                 }
319         }
320
321 status_up:
322
323         dev->data->dev_link.link_status = 1;
324         return 0;
325 }
326
327 /*
328  * This function gets called when the current port gets stopped.
329  * Is the only place for us to close all the tx streams dumpers.
330  * If not called the dumpers will be flushed within each tx burst.
331  */
332 static void
333 eth_dev_stop(struct rte_eth_dev *dev)
334 {
335         unsigned i;
336         struct pmd_internals *internals = dev->data->dev_private;
337         struct pcap_tx_queue *tx;
338         struct pcap_rx_queue *rx;
339
340         /* Special iface case. Single pcap is open and shared between tx/rx. */
341         if (internals->single_iface) {
342                 tx = &internals->tx_queue[0];
343                 rx = &internals->rx_queue[0];
344                 pcap_close(tx->pcap);
345                 tx->pcap = NULL;
346                 rx->pcap = NULL;
347                 goto status_down;
348         }
349
350         for (i = 0; i < internals->nb_tx_queues; i++) {
351                 tx = &internals->tx_queue[i];
352
353                 if (tx->dumper != NULL) {
354                         pcap_dump_close(tx->dumper);
355                         tx->dumper = NULL;
356                 }
357
358                 if (tx->pcap != NULL) {
359                         pcap_close(tx->pcap);
360                         tx->pcap = NULL;
361                 }
362         }
363
364         for (i = 0; i < internals->nb_rx_queues; i++) {
365                 rx = &internals->rx_queue[i];
366
367                 if (rx->pcap != NULL) {
368                         pcap_close(rx->pcap);
369                         rx->pcap = NULL;
370                 }
371         }
372
373 status_down:
374         dev->data->dev_link.link_status = 0;
375 }
376
377 static int
378 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
379 {
380         return 0;
381 }
382
383 static void
384 eth_dev_info(struct rte_eth_dev *dev,
385                 struct rte_eth_dev_info *dev_info)
386 {
387         struct pmd_internals *internals = dev->data->dev_private;
388         dev_info->driver_name = drivername;
389         dev_info->if_index = internals->if_index;
390         dev_info->max_mac_addrs = 1;
391         dev_info->max_rx_pktlen = (uint32_t) -1;
392         dev_info->max_rx_queues = (uint16_t)internals->nb_rx_queues;
393         dev_info->max_tx_queues = (uint16_t)internals->nb_tx_queues;
394         dev_info->min_rx_bufsize = 0;
395         dev_info->pci_dev = NULL;
396 }
397
398 static void
399 eth_stats_get(struct rte_eth_dev *dev,
400                 struct rte_eth_stats *igb_stats)
401 {
402         unsigned i;
403         unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
404         const struct pmd_internals *internal = dev->data->dev_private;
405
406         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && i < internal->nb_rx_queues;
407                         i++) {
408                 igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts;
409                 rx_total += igb_stats->q_ipackets[i];
410         }
411
412         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && i < internal->nb_tx_queues;
413                         i++) {
414                 igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts;
415                 igb_stats->q_errors[i] = internal->tx_queue[i].err_pkts;
416                 tx_total += igb_stats->q_opackets[i];
417                 tx_err_total += igb_stats->q_errors[i];
418         }
419
420         igb_stats->ipackets = rx_total;
421         igb_stats->opackets = tx_total;
422         igb_stats->oerrors = tx_err_total;
423 }
424
425 static void
426 eth_stats_reset(struct rte_eth_dev *dev)
427 {
428         unsigned i;
429         struct pmd_internals *internal = dev->data->dev_private;
430         for (i = 0; i < internal->nb_rx_queues; i++)
431                 internal->rx_queue[i].rx_pkts = 0;
432         for (i = 0; i < internal->nb_tx_queues; i++) {
433                 internal->tx_queue[i].tx_pkts = 0;
434                 internal->tx_queue[i].err_pkts = 0;
435         }
436 }
437
438 static void
439 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
440 {
441 }
442
443 static void
444 eth_queue_release(void *q __rte_unused)
445 {
446 }
447
448 static int
449 eth_link_update(struct rte_eth_dev *dev __rte_unused,
450                 int wait_to_complete __rte_unused)
451 {
452         return 0;
453 }
454
455 static int
456 eth_rx_queue_setup(struct rte_eth_dev *dev,
457                 uint16_t rx_queue_id,
458                 uint16_t nb_rx_desc __rte_unused,
459                 unsigned int socket_id __rte_unused,
460                 const struct rte_eth_rxconf *rx_conf __rte_unused,
461                 struct rte_mempool *mb_pool)
462 {
463         struct pmd_internals *internals = dev->data->dev_private;
464         struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
465         pcap_q->mb_pool = mb_pool;
466         dev->data->rx_queues[rx_queue_id] = pcap_q;
467         pcap_q->in_port = dev->data->port_id;
468         return 0;
469 }
470
471 static int
472 eth_tx_queue_setup(struct rte_eth_dev *dev,
473                 uint16_t tx_queue_id,
474                 uint16_t nb_tx_desc __rte_unused,
475                 unsigned int socket_id __rte_unused,
476                 const struct rte_eth_txconf *tx_conf __rte_unused)
477 {
478
479         struct pmd_internals *internals = dev->data->dev_private;
480         dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
481         return 0;
482 }
483
484 static const struct eth_dev_ops ops = {
485         .dev_start = eth_dev_start,
486         .dev_stop =     eth_dev_stop,
487         .dev_close = eth_dev_close,
488         .dev_configure = eth_dev_configure,
489         .dev_infos_get = eth_dev_info,
490         .rx_queue_setup = eth_rx_queue_setup,
491         .tx_queue_setup = eth_tx_queue_setup,
492         .rx_queue_release = eth_queue_release,
493         .tx_queue_release = eth_queue_release,
494         .link_update = eth_link_update,
495         .stats_get = eth_stats_get,
496         .stats_reset = eth_stats_reset,
497 };
498
499 static struct eth_driver rte_pcap_pmd = {
500         .pci_drv = {
501                 .name = "rte_pcap_pmd",
502                 .drv_flags = RTE_PCI_DRV_DETACHABLE,
503         },
504 };
505
506 /*
507  * Function handler that opens the pcap file for reading a stores a
508  * reference of it for use it later on.
509  */
510 static int
511 open_rx_pcap(const char *key, const char *value, void *extra_args)
512 {
513         unsigned i;
514         const char *pcap_filename = value;
515         struct rx_pcaps *pcaps = extra_args;
516         pcap_t *pcap = NULL;
517
518         for (i = 0; i < pcaps->num_of_rx; i++) {
519                 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
520                         return -1;
521
522                 pcaps->pcaps[i] = pcap;
523                 pcaps->names[i] = pcap_filename;
524                 pcaps->types[i] = key;
525         }
526
527         return 0;
528 }
529
530 static int
531 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
532 {
533         if ((*pcap = pcap_open_offline(pcap_filename, errbuf)) == NULL) {
534                 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename, errbuf);
535                 return -1;
536         }
537         return 0;
538 }
539
540 /*
541  * Opens a pcap file for writing and stores a reference to it
542  * for use it later on.
543  */
544 static int
545 open_tx_pcap(const char *key, const char *value, void *extra_args)
546 {
547         unsigned i;
548         const char *pcap_filename = value;
549         struct tx_pcaps *dumpers = extra_args;
550         pcap_dumper_t *dumper;
551
552         for (i = 0; i < dumpers->num_of_tx; i++) {
553                 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
554                         return -1;
555
556                 dumpers->dumpers[i] = dumper;
557                 dumpers->names[i] = pcap_filename;
558                 dumpers->types[i] = key;
559         }
560
561         return 0;
562 }
563
564 static int
565 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
566 {
567         pcap_t *tx_pcap;
568         /*
569          * We need to create a dummy empty pcap_t to use it
570          * with pcap_dump_open(). We create big enough an Ethernet
571          * pcap holder.
572          */
573
574         if ((tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN))
575                         == NULL) {
576                 RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
577                 return -1;
578         }
579
580         /* The dumper is created using the previous pcap_t reference */
581         if ((*dumper = pcap_dump_open(tx_pcap, pcap_filename)) == NULL) {
582                 RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n", pcap_filename);
583                 return -1;
584         }
585
586         return 0;
587 }
588
589 /*
590  * pcap_open_live wrapper function
591  */
592 static inline int
593 open_iface_live(const char *iface, pcap_t **pcap) {
594         *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
595                         RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
596
597         if (*pcap == NULL) {
598                 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
599                 return -1;
600         }
601         return 0;
602 }
603
604 /*
605  * Opens an interface for reading and writing
606  */
607 static inline int
608 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
609 {
610         const char *iface = value;
611         struct rx_pcaps *pcaps = extra_args;
612         pcap_t *pcap = NULL;
613
614         if (open_single_iface(iface, &pcap) < 0)
615                 return -1;
616
617         pcaps->pcaps[0] = pcap;
618         pcaps->names[0] = iface;
619         pcaps->types[0] = key;
620
621         return 0;
622 }
623
624 /*
625  * Opens a NIC for reading packets from it
626  */
627 static inline int
628 open_rx_iface(const char *key, const char *value, void *extra_args)
629 {
630         unsigned i;
631         const char *iface = value;
632         struct rx_pcaps *pcaps = extra_args;
633         pcap_t *pcap = NULL;
634
635         for (i = 0; i < pcaps->num_of_rx; i++) {
636                 if (open_single_iface(iface, &pcap) < 0)
637                         return -1;
638                 pcaps->pcaps[i] = pcap;
639                 pcaps->names[i] = iface;
640                 pcaps->types[i] = key;
641         }
642
643         return 0;
644 }
645
646 /*
647  * Opens a NIC for writing packets to it
648  */
649 static int
650 open_tx_iface(const char *key, const char *value, void *extra_args)
651 {
652         unsigned i;
653         const char *iface = value;
654         struct tx_pcaps *pcaps = extra_args;
655         pcap_t *pcap;
656
657         for (i = 0; i < pcaps->num_of_tx; i++) {
658                 if (open_single_iface(iface, &pcap) < 0)
659                         return -1;
660                 pcaps->pcaps[i] = pcap;
661                 pcaps->names[i] = iface;
662                 pcaps->types[i] = key;
663         }
664
665         return 0;
666 }
667
668 static int
669 open_single_iface(const char *iface, pcap_t **pcap)
670 {
671         if (open_iface_live(iface, pcap) < 0) {
672                 RTE_LOG(ERR, PMD, "Couldn't open interface %s\n", iface);
673                 return -1;
674         }
675
676         return 0;
677 }
678
679 static int
680 rte_pmd_init_internals(const char *name, const unsigned nb_rx_queues,
681                 const unsigned nb_tx_queues,
682                 const unsigned numa_node,
683                 struct pmd_internals **internals,
684                 struct rte_eth_dev **eth_dev,
685                 struct rte_kvargs *kvlist)
686 {
687         struct rte_eth_dev_data *data = NULL;
688         struct rte_pci_device *pci_dev = NULL;
689         unsigned k_idx;
690         struct rte_kvargs_pair *pair = NULL;
691
692         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
693                 pair = &kvlist->pairs[k_idx];
694                 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
695                         break;
696         }
697
698         RTE_LOG(INFO, PMD,
699                         "Creating pcap-backed ethdev on numa socket %u\n", numa_node);
700
701         /* now do all data allocation - for eth_dev structure, dummy pci driver
702          * and internal (private) data
703          */
704         data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
705         if (data == NULL)
706                 goto error;
707
708         pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, numa_node);
709         if (pci_dev == NULL)
710                 goto error;
711
712         *internals = rte_zmalloc_socket(name, sizeof(**internals), 0, numa_node);
713         if (*internals == NULL)
714                 goto error;
715
716         /* reserve an ethdev entry */
717         *eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
718         if (*eth_dev == NULL)
719                 goto error;
720
721         /* check length of device name */
722         if ((strlen((*eth_dev)->data->name) + 1) > sizeof(data->name))
723                 goto error;
724
725         /* now put it all together
726          * - store queue data in internals,
727          * - store numa_node info in pci_driver
728          * - point eth_dev_data to internals and pci_driver
729          * - and point eth_dev structure to new eth_dev_data structure
730          */
731         /* NOTE: we'll replace the data element, of originally allocated eth_dev
732          * so the rings are local per-process */
733
734         (*internals)->nb_rx_queues = nb_rx_queues;
735         (*internals)->nb_tx_queues = nb_tx_queues;
736
737         if (pair == NULL)
738                 (*internals)->if_index = 0;
739         else
740                 (*internals)->if_index = if_nametoindex(pair->value);
741
742         pci_dev->numa_node = numa_node;
743
744         data->dev_private = *internals;
745         data->port_id = (*eth_dev)->data->port_id;
746         snprintf(data->name, sizeof(data->name), "%s", (*eth_dev)->data->name);
747         data->nb_rx_queues = (uint16_t)nb_rx_queues;
748         data->nb_tx_queues = (uint16_t)nb_tx_queues;
749         data->dev_link = pmd_link;
750         data->mac_addrs = &eth_addr;
751         strncpy(data->name,
752                 (*eth_dev)->data->name, strlen((*eth_dev)->data->name));
753
754         (*eth_dev)->data = data;
755         (*eth_dev)->dev_ops = &ops;
756         (*eth_dev)->pci_dev = pci_dev;
757         (*eth_dev)->driver = &rte_pcap_pmd;
758
759         return 0;
760
761 error: 
762         rte_free(data);
763         rte_free(pci_dev);
764         rte_free(*internals);
765
766         return -1;
767 }
768
769 static int
770 rte_eth_from_pcaps_n_dumpers(const char *name,
771                 struct rx_pcaps *rx_queues,
772                 const unsigned nb_rx_queues,
773                 struct tx_pcaps *tx_queues,
774                 const unsigned nb_tx_queues,
775                 const unsigned numa_node,
776                 struct rte_kvargs *kvlist)
777 {
778         struct pmd_internals *internals = NULL;
779         struct rte_eth_dev *eth_dev = NULL;
780         unsigned i;
781
782         /* do some parameter checking */
783         if (rx_queues == NULL && nb_rx_queues > 0)
784                 return -1;
785         if (tx_queues == NULL && nb_tx_queues > 0)
786                 return -1;
787
788         if (rte_pmd_init_internals(name, nb_rx_queues, nb_tx_queues, numa_node,
789                         &internals, &eth_dev, kvlist) < 0)
790                 return -1;
791
792         for (i = 0; i < nb_rx_queues; i++) {
793                 internals->rx_queue->pcap = rx_queues->pcaps[i];
794                 internals->rx_queue->name = rx_queues->names[i];
795                 internals->rx_queue->type = rx_queues->types[i];
796         }
797         for (i = 0; i < nb_tx_queues; i++) {
798                 internals->tx_queue->dumper = tx_queues->dumpers[i];
799                 internals->tx_queue->name = tx_queues->names[i];
800                 internals->tx_queue->type = tx_queues->types[i];
801         }
802
803         /* using multiple pcaps/interfaces */
804         internals->single_iface = 0;
805
806         eth_dev->rx_pkt_burst = eth_pcap_rx;
807         eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
808
809         return 0;
810 }
811
812         struct rx_pcaps pcaps;
813 static int
814 rte_eth_from_pcaps(const char *name,
815                 struct rx_pcaps *rx_queues,
816                 const unsigned nb_rx_queues,
817                 struct tx_pcaps *tx_queues,
818                 const unsigned nb_tx_queues,
819                 const unsigned numa_node,
820                 struct rte_kvargs *kvlist,
821                 int single_iface)
822 {
823         struct pmd_internals *internals = NULL;
824         struct rte_eth_dev *eth_dev = NULL;
825         unsigned i;
826
827         /* do some parameter checking */
828         if (rx_queues == NULL && nb_rx_queues > 0)
829                 return -1;
830         if (tx_queues == NULL && nb_tx_queues > 0)
831                 return -1;
832
833         if (rte_pmd_init_internals(name, nb_rx_queues, nb_tx_queues, numa_node,
834                         &internals, &eth_dev, kvlist) < 0)
835                 return -1;
836
837         for (i = 0; i < nb_rx_queues; i++) {
838                 internals->rx_queue->pcap = rx_queues->pcaps[i];
839                 internals->rx_queue->name = rx_queues->names[i];
840                 internals->rx_queue->type = rx_queues->types[i];
841         }
842         for (i = 0; i < nb_tx_queues; i++) {
843                 internals->tx_queue->pcap = tx_queues->pcaps[i];
844                 internals->tx_queue->name = tx_queues->names[i];
845                 internals->tx_queue->type = tx_queues->types[i];
846         }
847
848         /* store wether we are using a single interface for rx/tx or not */
849         internals->single_iface = single_iface;
850
851         eth_dev->rx_pkt_burst = eth_pcap_rx;
852         eth_dev->tx_pkt_burst = eth_pcap_tx;
853
854         return 0;
855 }
856
857
858 static int
859 rte_pmd_pcap_devinit(const char *name, const char *params)
860 {
861         unsigned numa_node, using_dumpers = 0;
862         int ret;
863         struct rte_kvargs *kvlist;
864         struct rx_pcaps pcaps;
865         struct tx_pcaps dumpers;
866
867         RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
868
869         numa_node = rte_socket_id();
870
871         gettimeofday(&start_time, NULL);
872         start_cycles = rte_get_timer_cycles();
873         hz = rte_get_timer_hz();
874
875         kvlist = rte_kvargs_parse(params, valid_arguments);
876         if (kvlist == NULL)
877                 return -1;
878
879         /*
880          * If iface argument is passed we open the NICs and use them for
881          * reading / writing
882          */
883         if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
884
885                 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
886                                 &open_rx_tx_iface, &pcaps);
887                 if (ret < 0)
888                         goto free_kvlist;
889                 dumpers.pcaps[0] = pcaps.pcaps[0];
890                 dumpers.names[0] = pcaps.names[0];
891                 dumpers.types[0] = pcaps.types[0];
892                 ret = rte_eth_from_pcaps(name, &pcaps, 1, &dumpers, 1,
893                                 numa_node, kvlist, 1);
894                 goto free_kvlist;
895         }
896
897         /*
898          * We check whether we want to open a RX stream from a real NIC or a
899          * pcap file
900          */
901         if ((pcaps.num_of_rx = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG))) {
902                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
903                                 &open_rx_pcap, &pcaps);
904         } else {
905                 pcaps.num_of_rx = rte_kvargs_count(kvlist,
906                                 ETH_PCAP_RX_IFACE_ARG);
907                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
908                                 &open_rx_iface, &pcaps);
909         }
910
911         if (ret < 0)
912                 goto free_kvlist;
913
914         /*
915          * We check whether we want to open a TX stream to a real NIC or a
916          * pcap file
917          */
918         if ((dumpers.num_of_tx = rte_kvargs_count(kvlist,
919                         ETH_PCAP_TX_PCAP_ARG))) {
920                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
921                                 &open_tx_pcap, &dumpers);
922                 using_dumpers = 1;
923         } else {
924                 dumpers.num_of_tx = rte_kvargs_count(kvlist,
925                                 ETH_PCAP_TX_IFACE_ARG);
926                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
927                                 &open_tx_iface, &dumpers);
928         }
929
930         if (ret < 0)
931                 goto free_kvlist;
932
933         if (using_dumpers)
934                 ret = rte_eth_from_pcaps_n_dumpers(name, &pcaps, pcaps.num_of_rx,
935                                 &dumpers, dumpers.num_of_tx, numa_node, kvlist);
936         else
937                 ret = rte_eth_from_pcaps(name, &pcaps, pcaps.num_of_rx, &dumpers,
938                         dumpers.num_of_tx, numa_node, kvlist, 0);
939
940 free_kvlist:
941         rte_kvargs_free(kvlist);
942         return ret;
943 }
944
945 static int
946 rte_pmd_pcap_devuninit(const char *name)
947 {
948         struct rte_eth_dev *eth_dev = NULL;
949
950         RTE_LOG(INFO, PMD, "Closing pcap ethdev on numa socket %u\n",
951                         rte_socket_id());
952
953         if (name == NULL)
954                 return -1;
955
956         /* reserve an ethdev entry */
957         eth_dev = rte_eth_dev_allocated(name);
958         if (eth_dev == NULL)
959                 return -1;
960
961         rte_free(eth_dev->data->dev_private);
962         rte_free(eth_dev->data);
963         rte_free(eth_dev->pci_dev);
964
965         rte_eth_dev_release_port(eth_dev);
966
967         return 0;
968 }
969
970 static struct rte_driver pmd_pcap_drv = {
971         .name = "eth_pcap",
972         .type = PMD_VDEV,
973         .init = rte_pmd_pcap_devinit,
974         .uninit = rte_pmd_pcap_devuninit,
975 };
976
977 PMD_REGISTER_DRIVER(pmd_pcap_drv);