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