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