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