aa01464c3fe32a944fe3a82231ea34aa9ae19e9d
[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         struct rte_pktmbuf_pool_private *mbp_priv;
140         uint16_t num_rx = 0;
141         uint16_t buf_size;
142
143         if (unlikely(pcap_q->pcap == NULL || nb_pkts == 0))
144                 return 0;
145
146         /* Reads the given number of packets from the pcap file one by one
147          * and copies the packet data into a newly allocated mbuf to return.
148          */
149         for (i = 0; i < nb_pkts; i++) {
150                 /* Get the next PCAP packet */
151                 packet = pcap_next(pcap_q->pcap, &header);
152                 if (unlikely(packet == NULL))
153                         break;
154                 else
155                         mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
156                 if (unlikely(mbuf == NULL))
157                         break;
158
159                 /* Now get the space available for data in the mbuf */
160                 mbp_priv =  rte_mempool_get_priv(pcap_q->mb_pool);
161                 buf_size = (uint16_t) (mbp_priv->mbuf_data_room_size -
162                                 RTE_PKTMBUF_HEADROOM);
163
164                 if (header.len <= buf_size) {
165                         /* pcap packet will fit in the mbuf, go ahead and copy */
166                         rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), packet,
167                                         header.len);
168                         mbuf->data_len = (uint16_t)header.len;
169                         mbuf->pkt_len = mbuf->data_len;
170                         mbuf->port = pcap_q->in_port;
171                         bufs[num_rx] = mbuf;
172                         num_rx++;
173                 } else {
174                         /* pcap packet will not fit in the mbuf, so drop packet */
175                         RTE_LOG(ERR, PMD,
176                                         "PCAP packet %d bytes will not fit in mbuf (%d bytes)\n",
177                                         header.len, buf_size);
178                         rte_pktmbuf_free(mbuf);
179                 }
180         }
181         pcap_q->rx_pkts += num_rx;
182         return num_rx;
183 }
184
185 static inline void
186 calculate_timestamp(struct timeval *ts) {
187         uint64_t cycles;
188         struct timeval cur_time;
189
190         cycles = rte_get_timer_cycles() - start_cycles;
191         cur_time.tv_sec = cycles / hz;
192         cur_time.tv_usec = (cycles % hz) * 10e6 / hz;
193         timeradd(&start_time, &cur_time, ts);
194 }
195
196 /*
197  * Callback to handle writing packets to a pcap file.
198  */
199 static uint16_t
200 eth_pcap_tx_dumper(void *queue,
201                 struct rte_mbuf **bufs,
202                 uint16_t nb_pkts)
203 {
204         unsigned i;
205         struct rte_mbuf *mbuf;
206         struct pcap_tx_queue *dumper_q = queue;
207         uint16_t num_tx = 0;
208         struct pcap_pkthdr header;
209
210         if (dumper_q->dumper == NULL || nb_pkts == 0)
211                 return 0;
212
213         /* writes the nb_pkts packets to the previously opened pcap file dumper */
214         for (i = 0; i < nb_pkts; i++) {
215                 mbuf = bufs[i];
216                 calculate_timestamp(&header.ts);
217                 header.len = mbuf->data_len;
218                 header.caplen = header.len;
219                 pcap_dump((u_char *)dumper_q->dumper, &header,
220                                 rte_pktmbuf_mtod(mbuf, void*));
221                 rte_pktmbuf_free(mbuf);
222                 num_tx++;
223         }
224
225         /*
226          * Since there's no place to hook a callback when the forwarding
227          * process stops and to make sure the pcap file is actually written,
228          * we flush the pcap dumper within each burst.
229          */
230         pcap_dump_flush(dumper_q->dumper);
231         dumper_q->tx_pkts += num_tx;
232         dumper_q->err_pkts += nb_pkts - num_tx;
233         return num_tx;
234 }
235
236 /*
237  * Callback to handle sending packets through a real NIC.
238  */
239 static uint16_t
240 eth_pcap_tx(void *queue,
241                 struct rte_mbuf **bufs,
242                 uint16_t nb_pkts)
243 {
244         unsigned i;
245         int ret;
246         struct rte_mbuf *mbuf;
247         struct pcap_tx_queue *tx_queue = queue;
248         uint16_t num_tx = 0;
249
250         if (unlikely(nb_pkts == 0 || tx_queue->pcap == NULL))
251                 return 0;
252
253         for (i = 0; i < nb_pkts; i++) {
254                 mbuf = bufs[i];
255                 ret = pcap_sendpacket(tx_queue->pcap,
256                                 rte_pktmbuf_mtod(mbuf, u_char *),
257                                 mbuf->data_len);
258                 if (unlikely(ret != 0))
259                         break;
260                 num_tx++;
261                 rte_pktmbuf_free(mbuf);
262         }
263
264         tx_queue->tx_pkts += num_tx;
265         tx_queue->err_pkts += nb_pkts - num_tx;
266         return num_tx;
267 }
268
269 static int
270 eth_dev_start(struct rte_eth_dev *dev)
271 {
272         unsigned i;
273         struct pmd_internals *internals = dev->data->dev_private;
274         struct pcap_tx_queue *tx;
275         struct pcap_rx_queue *rx;
276
277         /* Special iface case. Single pcap is open and shared between tx/rx. */
278         if (internals->single_iface) {
279                 tx = &internals->tx_queue[0];
280                 rx = &internals->rx_queue[0];
281
282                 if (!tx->pcap && strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
283                         if (open_single_iface(tx->name, &tx->pcap) < 0)
284                                 return -1;
285                         rx->pcap = tx->pcap;
286                 }
287                 goto status_up;
288         }
289
290         /* If not open already, open tx pcaps/dumpers */
291         for (i = 0; i < internals->nb_tx_queues; i++) {
292                 tx = &internals->tx_queue[i];
293
294                 if (!tx->dumper && strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
295                         if (open_single_tx_pcap(tx->name, &tx->dumper) < 0)
296                                 return -1;
297                 }
298
299                 else if (!tx->pcap && strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
300                         if (open_single_iface(tx->name, &tx->pcap) < 0)
301                                 return -1;
302                 }
303         }
304
305         /* If not open already, open rx pcaps */
306         for (i = 0; i < internals->nb_rx_queues; i++) {
307                 rx = &internals->rx_queue[i];
308
309                 if (rx->pcap != NULL)
310                         continue;
311
312                 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
313                         if (open_single_rx_pcap(rx->name, &rx->pcap) < 0)
314                                 return -1;
315                 }
316
317                 else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
318                         if (open_single_iface(rx->name, &rx->pcap) < 0)
319                                 return -1;
320                 }
321         }
322
323 status_up:
324
325         dev->data->dev_link.link_status = 1;
326         return 0;
327 }
328
329 /*
330  * This function gets called when the current port gets stopped.
331  * Is the only place for us to close all the tx streams dumpers.
332  * If not called the dumpers will be flushed within each tx burst.
333  */
334 static void
335 eth_dev_stop(struct rte_eth_dev *dev)
336 {
337         unsigned i;
338         struct pmd_internals *internals = dev->data->dev_private;
339         struct pcap_tx_queue *tx;
340         struct pcap_rx_queue *rx;
341
342         /* Special iface case. Single pcap is open and shared between tx/rx. */
343         if (internals->single_iface) {
344                 tx = &internals->tx_queue[0];
345                 rx = &internals->rx_queue[0];
346                 pcap_close(tx->pcap);
347                 tx->pcap = NULL;
348                 rx->pcap = NULL;
349                 goto status_down;
350         }
351
352         for (i = 0; i < internals->nb_tx_queues; i++) {
353                 tx = &internals->tx_queue[i];
354
355                 if (tx->dumper != NULL) {
356                         pcap_dump_close(tx->dumper);
357                         tx->dumper = NULL;
358                 }
359
360                 if (tx->pcap != NULL) {
361                         pcap_close(tx->pcap);
362                         tx->pcap = NULL;
363                 }
364         }
365
366         for (i = 0; i < internals->nb_rx_queues; i++) {
367                 rx = &internals->rx_queue[i];
368
369                 if (rx->pcap != NULL) {
370                         pcap_close(rx->pcap);
371                         rx->pcap = NULL;
372                 }
373         }
374
375 status_down:
376         dev->data->dev_link.link_status = 0;
377 }
378
379 static int
380 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
381 {
382         return 0;
383 }
384
385 static void
386 eth_dev_info(struct rte_eth_dev *dev,
387                 struct rte_eth_dev_info *dev_info)
388 {
389         struct pmd_internals *internals = dev->data->dev_private;
390         dev_info->driver_name = drivername;
391         dev_info->if_index = internals->if_index;
392         dev_info->max_mac_addrs = 1;
393         dev_info->max_rx_pktlen = (uint32_t) -1;
394         dev_info->max_rx_queues = (uint16_t)internals->nb_rx_queues;
395         dev_info->max_tx_queues = (uint16_t)internals->nb_tx_queues;
396         dev_info->min_rx_bufsize = 0;
397         dev_info->pci_dev = NULL;
398 }
399
400 static void
401 eth_stats_get(struct rte_eth_dev *dev,
402                 struct rte_eth_stats *igb_stats)
403 {
404         unsigned i;
405         unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
406         const struct pmd_internals *internal = dev->data->dev_private;
407
408         memset(igb_stats, 0, sizeof(*igb_stats));
409         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && i < internal->nb_rx_queues;
410                         i++) {
411                 igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts;
412                 rx_total += igb_stats->q_ipackets[i];
413         }
414
415         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && i < internal->nb_tx_queues;
416                         i++) {
417                 igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts;
418                 igb_stats->q_errors[i] = internal->tx_queue[i].err_pkts;
419                 tx_total += igb_stats->q_opackets[i];
420                 tx_err_total += igb_stats->q_errors[i];
421         }
422
423         igb_stats->ipackets = rx_total;
424         igb_stats->opackets = tx_total;
425         igb_stats->oerrors = tx_err_total;
426 }
427
428 static void
429 eth_stats_reset(struct rte_eth_dev *dev)
430 {
431         unsigned i;
432         struct pmd_internals *internal = dev->data->dev_private;
433         for (i = 0; i < internal->nb_rx_queues; i++)
434                 internal->rx_queue[i].rx_pkts = 0;
435         for (i = 0; i < internal->nb_tx_queues; i++) {
436                 internal->tx_queue[i].tx_pkts = 0;
437                 internal->tx_queue[i].err_pkts = 0;
438         }
439 }
440
441 static void
442 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
443 {
444 }
445
446 static void
447 eth_queue_release(void *q __rte_unused)
448 {
449 }
450
451 static int
452 eth_link_update(struct rte_eth_dev *dev __rte_unused,
453                 int wait_to_complete __rte_unused)
454 {
455         return 0;
456 }
457
458 static int
459 eth_rx_queue_setup(struct rte_eth_dev *dev,
460                 uint16_t rx_queue_id,
461                 uint16_t nb_rx_desc __rte_unused,
462                 unsigned int socket_id __rte_unused,
463                 const struct rte_eth_rxconf *rx_conf __rte_unused,
464                 struct rte_mempool *mb_pool)
465 {
466         struct pmd_internals *internals = dev->data->dev_private;
467         struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
468         pcap_q->mb_pool = mb_pool;
469         dev->data->rx_queues[rx_queue_id] = pcap_q;
470         pcap_q->in_port = dev->data->port_id;
471         return 0;
472 }
473
474 static int
475 eth_tx_queue_setup(struct rte_eth_dev *dev,
476                 uint16_t tx_queue_id,
477                 uint16_t nb_tx_desc __rte_unused,
478                 unsigned int socket_id __rte_unused,
479                 const struct rte_eth_txconf *tx_conf __rte_unused)
480 {
481
482         struct pmd_internals *internals = dev->data->dev_private;
483         dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
484         return 0;
485 }
486
487 static struct eth_dev_ops ops = {
488                 .dev_start = eth_dev_start,
489                 .dev_stop =     eth_dev_stop,
490                 .dev_close = eth_dev_close,
491                 .dev_configure = eth_dev_configure,
492                 .dev_infos_get = eth_dev_info,
493                 .rx_queue_setup = eth_rx_queue_setup,
494                 .tx_queue_setup = eth_tx_queue_setup,
495                 .rx_queue_release = eth_queue_release,
496                 .tx_queue_release = eth_queue_release,
497                 .link_update = eth_link_update,
498                 .stats_get = eth_stats_get,
499                 .stats_reset = eth_stats_reset,
500 };
501
502 /*
503  * Function handler that opens the pcap file for reading a stores a
504  * reference of it for use it later on.
505  */
506 static int
507 open_rx_pcap(const char *key, const char *value, void *extra_args)
508 {
509         unsigned i;
510         const char *pcap_filename = value;
511         struct rx_pcaps *pcaps = extra_args;
512         pcap_t *pcap = NULL;
513
514         for (i = 0; i < pcaps->num_of_rx; i++) {
515                 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
516                         return -1;
517
518                 pcaps->pcaps[i] = pcap;
519                 pcaps->names[i] = pcap_filename;
520                 pcaps->types[i] = key;
521         }
522
523         return 0;
524 }
525
526 static int
527 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
528 {
529         if ((*pcap = pcap_open_offline(pcap_filename, errbuf)) == NULL) {
530                 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename, errbuf);
531                 return -1;
532         }
533         return 0;
534 }
535
536 /*
537  * Opens a pcap file for writing and stores a reference to it
538  * for use it later on.
539  */
540 static int
541 open_tx_pcap(const char *key, const char *value, void *extra_args)
542 {
543         unsigned i;
544         const char *pcap_filename = value;
545         struct tx_pcaps *dumpers = extra_args;
546         pcap_dumper_t *dumper;
547
548         for (i = 0; i < dumpers->num_of_tx; i++) {
549                 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
550                         return -1;
551
552                 dumpers->dumpers[i] = dumper;
553                 dumpers->names[i] = pcap_filename;
554                 dumpers->types[i] = key;
555         }
556
557         return 0;
558 }
559
560 static int
561 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
562 {
563         pcap_t *tx_pcap;
564         /*
565          * We need to create a dummy empty pcap_t to use it
566          * with pcap_dump_open(). We create big enough an Ethernet
567          * pcap holder.
568          */
569
570         if ((tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN))
571                         == NULL) {
572                 RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
573                 return -1;
574         }
575
576         /* The dumper is created using the previous pcap_t reference */
577         if ((*dumper = pcap_dump_open(tx_pcap, pcap_filename)) == NULL) {
578                 RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n", pcap_filename);
579                 return -1;
580         }
581
582         return 0;
583 }
584
585 /*
586  * pcap_open_live wrapper function
587  */
588 static inline int
589 open_iface_live(const char *iface, pcap_t **pcap) {
590         *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
591                         RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
592
593         if (*pcap == NULL) {
594                 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
595                 return -1;
596         }
597         return 0;
598 }
599
600 /*
601  * Opens an interface for reading and writing
602  */
603 static inline int
604 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
605 {
606         const char *iface = value;
607         struct rx_pcaps *pcaps = extra_args;
608         pcap_t *pcap = NULL;
609
610         if (open_single_iface(iface, &pcap) < 0)
611                 return -1;
612
613         pcaps->pcaps[0] = pcap;
614         pcaps->names[0] = iface;
615         pcaps->types[0] = key;
616
617         return 0;
618 }
619
620 /*
621  * Opens a NIC for reading packets from it
622  */
623 static inline int
624 open_rx_iface(const char *key, const char *value, void *extra_args)
625 {
626         unsigned i;
627         const char *iface = value;
628         struct rx_pcaps *pcaps = extra_args;
629         pcap_t *pcap = NULL;
630
631         for (i = 0; i < pcaps->num_of_rx; i++) {
632                 if (open_single_iface(iface, &pcap) < 0)
633                         return -1;
634                 pcaps->pcaps[i] = pcap;
635                 pcaps->names[i] = iface;
636                 pcaps->types[i] = key;
637         }
638
639         return 0;
640 }
641
642 /*
643  * Opens a NIC for writing packets to it
644  */
645 static int
646 open_tx_iface(const char *key, const char *value, void *extra_args)
647 {
648         unsigned i;
649         const char *iface = value;
650         struct tx_pcaps *pcaps = extra_args;
651         pcap_t *pcap;
652
653         for (i = 0; i < pcaps->num_of_tx; i++) {
654                 if (open_single_iface(iface, &pcap) < 0)
655                         return -1;
656                 pcaps->pcaps[i] = pcap;
657                 pcaps->names[i] = iface;
658                 pcaps->types[i] = key;
659         }
660
661         return 0;
662 }
663
664 static int
665 open_single_iface(const char *iface, pcap_t **pcap)
666 {
667         if (open_iface_live(iface, pcap) < 0) {
668                 RTE_LOG(ERR, PMD, "Couldn't open interface %s\n", iface);
669                 return -1;
670         }
671
672         return 0;
673 }
674
675 static int
676 rte_pmd_init_internals(const char *name, const unsigned nb_rx_queues,
677                 const unsigned nb_tx_queues,
678                 const unsigned numa_node,
679                 struct pmd_internals **internals,
680                 struct rte_eth_dev **eth_dev,
681                 struct rte_kvargs *kvlist)
682 {
683         struct rte_eth_dev_data *data = NULL;
684         struct rte_pci_device *pci_dev = NULL;
685         unsigned k_idx;
686         struct rte_kvargs_pair *pair = NULL;
687
688         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
689                 pair = &kvlist->pairs[k_idx];
690                 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
691                         break;
692         }
693
694         RTE_LOG(INFO, PMD,
695                         "Creating pcap-backed ethdev on numa socket %u\n", numa_node);
696
697         /* now do all data allocation - for eth_dev structure, dummy pci driver
698          * and internal (private) data
699          */
700         data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
701         if (data == NULL)
702                 goto error;
703
704         pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, numa_node);
705         if (pci_dev == NULL)
706                 goto error;
707
708         *internals = rte_zmalloc_socket(name, sizeof(**internals), 0, numa_node);
709         if (*internals == NULL)
710                 goto error;
711
712         /* reserve an ethdev entry */
713         *eth_dev = rte_eth_dev_allocate(name);
714         if (*eth_dev == NULL)
715                 goto error;
716
717         /* now put it all together
718          * - store queue data in internals,
719          * - store numa_node info in pci_driver
720          * - point eth_dev_data to internals and pci_driver
721          * - and point eth_dev structure to new eth_dev_data structure
722          */
723         /* NOTE: we'll replace the data element, of originally allocated eth_dev
724          * so the rings are local per-process */
725
726         (*internals)->nb_rx_queues = nb_rx_queues;
727         (*internals)->nb_tx_queues = nb_tx_queues;
728
729         if (pair == NULL)
730                 (*internals)->if_index = 0;
731         else
732                 (*internals)->if_index = if_nametoindex(pair->value);
733
734         pci_dev->numa_node = numa_node;
735
736         data->dev_private = *internals;
737         data->port_id = (*eth_dev)->data->port_id;
738         snprintf(data->name, sizeof(data->name), "%s", (*eth_dev)->data->name);
739         data->nb_rx_queues = (uint16_t)nb_rx_queues;
740         data->nb_tx_queues = (uint16_t)nb_tx_queues;
741         data->dev_link = pmd_link;
742         data->mac_addrs = &eth_addr;
743
744         (*eth_dev)->data = data;
745         (*eth_dev)->dev_ops = &ops;
746         (*eth_dev)->pci_dev = pci_dev;
747
748         return 0;
749
750         error: if (data)
751                 rte_free(data);
752         if (pci_dev)
753                 rte_free(pci_dev);
754         if (*internals)
755                 rte_free(*internals);
756         return -1;
757 }
758
759 static int
760 rte_eth_from_pcaps_n_dumpers(const char *name,
761                 struct rx_pcaps *rx_queues,
762                 const unsigned nb_rx_queues,
763                 struct tx_pcaps *tx_queues,
764                 const unsigned nb_tx_queues,
765                 const unsigned numa_node,
766                 struct rte_kvargs *kvlist)
767 {
768         struct pmd_internals *internals = NULL;
769         struct rte_eth_dev *eth_dev = NULL;
770         unsigned i;
771
772         /* do some parameter checking */
773         if (rx_queues == NULL && nb_rx_queues > 0)
774                 return -1;
775         if (tx_queues == NULL && nb_tx_queues > 0)
776                 return -1;
777
778         if (rte_pmd_init_internals(name, nb_rx_queues, nb_tx_queues, numa_node,
779                         &internals, &eth_dev, kvlist) < 0)
780                 return -1;
781
782         for (i = 0; i < nb_rx_queues; i++) {
783                 internals->rx_queue->pcap = rx_queues->pcaps[i];
784                 internals->rx_queue->name = rx_queues->names[i];
785                 internals->rx_queue->type = rx_queues->types[i];
786         }
787         for (i = 0; i < nb_tx_queues; i++) {
788                 internals->tx_queue->dumper = tx_queues->dumpers[i];
789                 internals->tx_queue->name = tx_queues->names[i];
790                 internals->tx_queue->type = tx_queues->types[i];
791         }
792
793         /* using multiple pcaps/interfaces */
794         internals->single_iface = 0;
795
796         eth_dev->rx_pkt_burst = eth_pcap_rx;
797         eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
798
799         return 0;
800 }
801
802         struct rx_pcaps pcaps;
803 static int
804 rte_eth_from_pcaps(const char *name,
805                 struct rx_pcaps *rx_queues,
806                 const unsigned nb_rx_queues,
807                 struct tx_pcaps *tx_queues,
808                 const unsigned nb_tx_queues,
809                 const unsigned numa_node,
810                 struct rte_kvargs *kvlist,
811                 int single_iface)
812 {
813         struct pmd_internals *internals = NULL;
814         struct rte_eth_dev *eth_dev = NULL;
815         unsigned i;
816
817         /* do some parameter checking */
818         if (rx_queues == NULL && nb_rx_queues > 0)
819                 return -1;
820         if (tx_queues == NULL && nb_tx_queues > 0)
821                 return -1;
822
823         if (rte_pmd_init_internals(name, nb_rx_queues, nb_tx_queues, numa_node,
824                         &internals, &eth_dev, kvlist) < 0)
825                 return -1;
826
827         for (i = 0; i < nb_rx_queues; i++) {
828                 internals->rx_queue->pcap = rx_queues->pcaps[i];
829                 internals->rx_queue->name = rx_queues->names[i];
830                 internals->rx_queue->type = rx_queues->types[i];
831         }
832         for (i = 0; i < nb_tx_queues; i++) {
833                 internals->tx_queue->pcap = tx_queues->pcaps[i];
834                 internals->tx_queue->name = tx_queues->names[i];
835                 internals->tx_queue->type = tx_queues->types[i];
836         }
837
838         /* store wether we are using a single interface for rx/tx or not */
839         internals->single_iface = single_iface;
840
841         eth_dev->rx_pkt_burst = eth_pcap_rx;
842         eth_dev->tx_pkt_burst = eth_pcap_tx;
843
844         return 0;
845 }
846
847
848 static int
849 rte_pmd_pcap_devinit(const char *name, const char *params)
850 {
851         unsigned numa_node, using_dumpers = 0;
852         int ret;
853         struct rte_kvargs *kvlist;
854         struct rx_pcaps pcaps;
855         struct tx_pcaps dumpers;
856
857         RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
858
859         numa_node = rte_socket_id();
860
861         gettimeofday(&start_time, NULL);
862         start_cycles = rte_get_timer_cycles();
863         hz = rte_get_timer_hz();
864
865         kvlist = rte_kvargs_parse(params, valid_arguments);
866         if (kvlist == NULL)
867                 return -1;
868
869         /*
870          * If iface argument is passed we open the NICs and use them for
871          * reading / writing
872          */
873         if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
874
875                 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
876                                 &open_rx_tx_iface, &pcaps);
877                 if (ret < 0)
878                         return -1;
879                 dumpers.pcaps[0] = pcaps.pcaps[0];
880                 dumpers.names[0] = pcaps.names[0];
881                 dumpers.types[0] = pcaps.types[0];
882                 return rte_eth_from_pcaps(name, &pcaps, 1, &dumpers, 1,
883                                 numa_node, kvlist, 1);
884         }
885
886         /*
887          * We check whether we want to open a RX stream from a real NIC or a
888          * pcap file
889          */
890         if ((pcaps.num_of_rx = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG))) {
891                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
892                                 &open_rx_pcap, &pcaps);
893         } else {
894                 pcaps.num_of_rx = rte_kvargs_count(kvlist,
895                                 ETH_PCAP_RX_IFACE_ARG);
896                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
897                                 &open_rx_iface, &pcaps);
898         }
899
900         if (ret < 0)
901                 return -1;
902
903         /*
904          * We check whether we want to open a TX stream to a real NIC or a
905          * pcap file
906          */
907         if ((dumpers.num_of_tx = rte_kvargs_count(kvlist,
908                         ETH_PCAP_TX_PCAP_ARG))) {
909                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
910                                 &open_tx_pcap, &dumpers);
911                 using_dumpers = 1;
912         } else {
913                 dumpers.num_of_tx = rte_kvargs_count(kvlist,
914                                 ETH_PCAP_TX_IFACE_ARG);
915                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
916                                 &open_tx_iface, &dumpers);
917         }
918
919         if (ret < 0)
920                 return -1;
921
922         if (using_dumpers)
923                 return rte_eth_from_pcaps_n_dumpers(name, &pcaps, pcaps.num_of_rx,
924                                 &dumpers, dumpers.num_of_tx, numa_node, kvlist);
925
926         return rte_eth_from_pcaps(name, &pcaps, pcaps.num_of_rx, &dumpers,
927                         dumpers.num_of_tx, numa_node, kvlist, 0);
928
929 }
930
931 static struct rte_driver pmd_pcap_drv = {
932         .name = "eth_pcap",
933         .type = PMD_VDEV,
934         .init = rte_pmd_pcap_devinit,
935 };
936
937 PMD_REGISTER_DRIVER(pmd_pcap_drv);