pcap: revert build patches
[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 /*
221  * Callback to handle sending packets through a real NIC.
222  */
223 static uint16_t
224 eth_pcap_tx(void *queue,
225                 struct rte_mbuf **bufs,
226                 uint16_t nb_pkts)
227 {
228         unsigned i;
229         int ret;
230         struct rte_mbuf *mbuf;
231         struct pcap_tx_queue *tx_queue = queue;
232         uint16_t num_tx = 0;
233
234         if (unlikely(nb_pkts == 0 || tx_queue->pcap == NULL))
235                 return 0;
236
237         for (i = 0; i < nb_pkts; i++) {
238                 mbuf = bufs[i];
239                 ret = pcap_sendpacket(tx_queue->pcap, (u_char*) mbuf->pkt.data,
240                                 mbuf->pkt.data_len);
241                 if(likely(!ret))
242                         num_tx++;
243                 rte_pktmbuf_free(mbuf);
244         }
245
246         tx_queue->tx_pkts += num_tx;
247         tx_queue->err_pkts += nb_pkts - num_tx;
248         return num_tx;
249 }
250
251 static int
252 eth_dev_start(struct rte_eth_dev *dev)
253 {
254         dev->data->dev_link.link_status = 1;
255         return 0;
256 }
257
258 /*
259  * This function gets called when the current port gets stopped.
260  * Is the only place for us to close all the tx streams dumpers.
261  * If not called the dumpers will be flushed within each tx burst.
262  */
263 static void
264 eth_dev_stop(struct rte_eth_dev *dev)
265 {
266         unsigned i;
267         pcap_dumper_t *dumper;
268         pcap_t *pcap;
269         struct pmd_internals *internals = dev->data->dev_private;
270
271         for (i = 0; i < internals->nb_tx_queues; i++) {
272                 dumper = internals->tx_queue[i].dumper;
273                 if(dumper != NULL)
274                         pcap_dump_close(dumper);
275                 pcap = internals->tx_queue[i].pcap;
276                 if(pcap != NULL)
277                         pcap_close(pcap);
278         }
279
280         dev->data->dev_link.link_status = 0;
281 }
282
283 static int
284 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
285 {
286         return 0;
287 }
288
289 static void
290 eth_dev_info(struct rte_eth_dev *dev,
291                 struct rte_eth_dev_info *dev_info)
292 {
293         struct pmd_internals *internals = dev->data->dev_private;
294         dev_info->driver_name = drivername;
295         dev_info->if_index = internals->if_index;
296         dev_info->max_mac_addrs = 1;
297         dev_info->max_rx_pktlen = (uint32_t) -1;
298         dev_info->max_rx_queues = (uint16_t)internals->nb_rx_queues;
299         dev_info->max_tx_queues = (uint16_t)internals->nb_tx_queues;
300         dev_info->min_rx_bufsize = 0;
301         dev_info->pci_dev = NULL;
302 }
303
304 static void
305 eth_stats_get(struct rte_eth_dev *dev,
306                 struct rte_eth_stats *igb_stats)
307 {
308         unsigned i;
309         unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
310         const struct pmd_internals *internal = dev->data->dev_private;
311
312         memset(igb_stats, 0, sizeof(*igb_stats));
313         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && i < internal->nb_rx_queues;
314                         i++) {
315                 igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts;
316                 rx_total += igb_stats->q_ipackets[i];
317         }
318
319         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && i < internal->nb_tx_queues;
320                         i++) {
321                 igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts;
322                 igb_stats->q_errors[i] = internal->tx_queue[i].err_pkts;
323                 tx_total += igb_stats->q_opackets[i];
324                 tx_err_total += igb_stats->q_errors[i];
325         }
326
327         igb_stats->ipackets = rx_total;
328         igb_stats->opackets = tx_total;
329         igb_stats->oerrors = tx_err_total;
330 }
331
332 static void
333 eth_stats_reset(struct rte_eth_dev *dev)
334 {
335         unsigned i;
336         struct pmd_internals *internal = dev->data->dev_private;
337         for (i = 0; i < internal->nb_rx_queues; i++)
338                 internal->rx_queue[i].rx_pkts = 0;
339         for (i = 0; i < internal->nb_tx_queues; i++) {
340                 internal->tx_queue[i].tx_pkts = 0;
341                 internal->tx_queue[i].err_pkts = 0;
342         }
343 }
344
345 static void
346 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
347 {
348 }
349
350 static void
351 eth_queue_release(void *q __rte_unused)
352 {
353 }
354
355 static int
356 eth_link_update(struct rte_eth_dev *dev __rte_unused,
357                 int wait_to_complete __rte_unused)
358 {
359         return 0;
360 }
361
362 static int
363 eth_rx_queue_setup(struct rte_eth_dev *dev,
364                 uint16_t rx_queue_id,
365                 uint16_t nb_rx_desc __rte_unused,
366                 unsigned int socket_id __rte_unused,
367                 const struct rte_eth_rxconf *rx_conf __rte_unused,
368                 struct rte_mempool *mb_pool)
369 {
370         struct pmd_internals *internals = dev->data->dev_private;
371         struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
372         pcap_q->mb_pool = mb_pool;
373         dev->data->rx_queues[rx_queue_id] = pcap_q;
374         return 0;
375 }
376
377 static int
378 eth_tx_queue_setup(struct rte_eth_dev *dev,
379                 uint16_t tx_queue_id,
380                 uint16_t nb_tx_desc __rte_unused,
381                 unsigned int socket_id __rte_unused,
382                 const struct rte_eth_txconf *tx_conf __rte_unused)
383 {
384
385         struct pmd_internals *internals = dev->data->dev_private;
386         dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
387         return 0;
388 }
389
390 static struct eth_dev_ops ops = {
391                 .dev_start = eth_dev_start,
392                 .dev_stop =     eth_dev_stop,
393                 .dev_close = eth_dev_close,
394                 .dev_configure = eth_dev_configure,
395                 .dev_infos_get = eth_dev_info,
396                 .rx_queue_setup = eth_rx_queue_setup,
397                 .tx_queue_setup = eth_tx_queue_setup,
398                 .rx_queue_release = eth_queue_release,
399                 .tx_queue_release = eth_queue_release,
400                 .link_update = eth_link_update,
401                 .stats_get = eth_stats_get,
402                 .stats_reset = eth_stats_reset,
403 };
404
405 /*
406  * Function handler that opens the pcap file for reading a stores a
407  * reference of it for use it later on.
408  */
409 static int
410 open_rx_pcap(const char *key __rte_unused, const char *value, void *extra_args)
411 {
412         unsigned i;
413         const char *pcap_filename = value;
414         struct rx_pcaps *pcaps = extra_args;
415         pcap_t *rx_pcap;
416
417         for (i = 0; i < pcaps->num_of_rx; i++) {
418                 if ((rx_pcap = pcap_open_offline(pcap_filename, errbuf)) == NULL) {
419                         RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename, errbuf);
420                         return -1;
421                 }
422                 pcaps->pcaps[i] = rx_pcap;
423         }
424
425         return 0;
426 }
427
428 /*
429  * Opens a pcap file for writing and stores a reference to it
430  * for use it later on.
431  */
432 static int
433 open_tx_pcap(const char *key __rte_unused, const char *value, void *extra_args)
434 {
435         unsigned i;
436         const char *pcap_filename = value;
437         struct tx_pcaps *dumpers = extra_args;
438         pcap_t *tx_pcap;
439         pcap_dumper_t *dumper;
440
441         for (i = 0; i < dumpers->num_of_tx; i++) {
442                 /*
443                  * We need to create a dummy empty pcap_t to use it
444                  * with pcap_dump_open(). We create big enough an Ethernet
445                  * pcap holder.
446                  */
447                 if ((tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN))
448                                 == NULL) {
449                         RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
450                         return -1;
451                 }
452
453                 /* The dumper is created using the previous pcap_t reference */
454                 if ((dumper = pcap_dump_open(tx_pcap, pcap_filename)) == NULL) {
455                         RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n", pcap_filename);
456                         return -1;
457                 }
458                 dumpers->dumpers[i] = dumper;
459         }
460
461         return 0;
462 }
463
464 /*
465  * pcap_open_live wrapper function
466  */
467 static inline int
468 open_iface_live(const char *iface, pcap_t **pcap) {
469         *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
470                         RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
471
472         if (*pcap == NULL) {
473                 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
474                 return -1;
475         }
476         return 0;
477 }
478
479 /*
480  * Opens an interface for reading and writing
481  */
482 static inline int
483 open_rx_tx_iface(const char *key __rte_unused, const char *value, void *extra_args)
484 {
485         const char *iface = value;
486         pcap_t **pcap = extra_args;
487
488         if(open_iface_live(iface, pcap) < 0)
489                 return -1;
490         return 0;
491 }
492
493 /*
494  * Opens a NIC for reading packets from it
495  */
496 static inline int
497 open_rx_iface(const char *key __rte_unused, const char *value, void *extra_args)
498 {
499         unsigned i;
500         const char *iface = value;
501         struct rx_pcaps *pcaps = extra_args;
502         pcap_t *pcap = NULL;
503
504         for (i = 0; i < pcaps->num_of_rx; i++) {
505                 if(open_iface_live(iface, &pcap) < 0)
506                         return -1;
507                 pcaps->pcaps[i] = pcap;
508         }
509
510         return 0;
511 }
512
513 /*
514  * Opens a NIC for writing packets to it
515  */
516 static inline int
517 open_tx_iface(const char *key __rte_unused, const char *value, void *extra_args)
518 {
519         unsigned i;
520         const char *iface = value;
521         struct tx_pcaps *pcaps = extra_args;
522         pcap_t *pcap;
523
524         for (i = 0; i < pcaps->num_of_tx; i++) {
525                 if(open_iface_live(iface, &pcap) < 0)
526                         return -1;
527                 pcaps->pcaps[i] = pcap;
528         }
529
530         return 0;
531 }
532
533
534 static int
535 rte_pmd_init_internals(const unsigned nb_rx_queues,
536                 const unsigned nb_tx_queues,
537                 const unsigned numa_node,
538                 struct pmd_internals **internals,
539                 struct rte_eth_dev **eth_dev,
540                 struct rte_kvargs *kvlist)
541 {
542         struct rte_eth_dev_data *data = NULL;
543         struct rte_pci_device *pci_dev = NULL;
544         unsigned k_idx;
545         struct rte_kvargs_pair *pair = NULL;
546
547         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
548                 pair = &kvlist->pairs[k_idx];
549                 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
550                         break;
551         }
552
553         RTE_LOG(INFO, PMD,
554                         "Creating pcap-backed ethdev on numa socket %u\n", numa_node);
555
556         /* now do all data allocation - for eth_dev structure, dummy pci driver
557          * and internal (private) data
558          */
559         data = rte_zmalloc_socket(NULL, sizeof(*data), 0, numa_node);
560         if (data == NULL)
561                 goto error;
562
563         pci_dev = rte_zmalloc_socket(NULL, sizeof(*pci_dev), 0, numa_node);
564         if (pci_dev == NULL)
565                 goto error;
566
567         *internals = rte_zmalloc_socket(NULL, sizeof(**internals), 0, numa_node);
568         if (*internals == NULL)
569                 goto error;
570
571         /* reserve an ethdev entry */
572         *eth_dev = rte_eth_dev_allocate();
573         if (*eth_dev == NULL)
574                 goto error;
575
576         /* now put it all together
577          * - store queue data in internals,
578          * - store numa_node info in pci_driver
579          * - point eth_dev_data to internals and pci_driver
580          * - and point eth_dev structure to new eth_dev_data structure
581          */
582         /* NOTE: we'll replace the data element, of originally allocated eth_dev
583          * so the rings are local per-process */
584
585         (*internals)->nb_rx_queues = nb_rx_queues;
586         (*internals)->nb_tx_queues = nb_tx_queues;
587
588         if (pair == NULL)
589                 (*internals)->if_index = 0;
590         else
591                 (*internals)->if_index = if_nametoindex(pair->value);
592
593         pci_dev->numa_node = numa_node;
594
595         data->dev_private = *internals;
596         data->port_id = (*eth_dev)->data->port_id;
597         data->nb_rx_queues = (uint16_t)nb_rx_queues;
598         data->nb_tx_queues = (uint16_t)nb_tx_queues;
599         data->dev_link = pmd_link;
600         data->mac_addrs = &eth_addr;
601
602         (*eth_dev)->data = data;
603         (*eth_dev)->dev_ops = &ops;
604         (*eth_dev)->pci_dev = pci_dev;
605
606         return 0;
607
608         error: if (data)
609                 rte_free(data);
610         if (pci_dev)
611                 rte_free(pci_dev);
612         if (*internals)
613                 rte_free(*internals);
614         return -1;
615 }
616
617 static int
618 rte_eth_from_pcaps_n_dumpers(pcap_t * const rx_queues[],
619                 const unsigned nb_rx_queues,
620                 pcap_dumper_t * const tx_queues[],
621                 const unsigned nb_tx_queues,
622                 const unsigned numa_node,
623                 struct rte_kvargs *kvlist)
624 {
625         struct pmd_internals *internals = NULL;
626         struct rte_eth_dev *eth_dev = NULL;
627         unsigned i;
628
629         /* do some parameter checking */
630         if (rx_queues == NULL && nb_rx_queues > 0)
631                 return -1;
632         if (tx_queues == NULL && nb_tx_queues > 0)
633                 return -1;
634
635         if (rte_pmd_init_internals(nb_rx_queues, nb_tx_queues, numa_node,
636                         &internals, &eth_dev, kvlist) < 0)
637                 return -1;
638
639         for (i = 0; i < nb_rx_queues; i++) {
640                 internals->rx_queue->pcap = rx_queues[i];
641         }
642         for (i = 0; i < nb_tx_queues; i++) {
643                 internals->tx_queue->dumper = tx_queues[i];
644         }
645
646         eth_dev->rx_pkt_burst = eth_pcap_rx;
647         eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
648
649         return 0;
650 }
651
652 static int
653 rte_eth_from_pcaps(pcap_t * const rx_queues[],
654                 const unsigned nb_rx_queues,
655                 pcap_t * const tx_queues[],
656                 const unsigned nb_tx_queues,
657                 const unsigned numa_node,
658                 struct rte_kvargs *kvlist)
659 {
660         struct pmd_internals *internals = NULL;
661         struct rte_eth_dev *eth_dev = NULL;
662         unsigned i;
663
664         /* do some parameter checking */
665         if (rx_queues == NULL && nb_rx_queues > 0)
666                 return -1;
667         if (tx_queues == NULL && nb_tx_queues > 0)
668                 return -1;
669
670         if (rte_pmd_init_internals(nb_rx_queues, nb_tx_queues, numa_node,
671                         &internals, &eth_dev, kvlist) < 0)
672                 return -1;
673
674         for (i = 0; i < nb_rx_queues; i++) {
675                 internals->rx_queue->pcap = rx_queues[i];
676         }
677         for (i = 0; i < nb_tx_queues; i++) {
678                 internals->tx_queue->pcap = tx_queues[i];
679         }
680
681         eth_dev->rx_pkt_burst = eth_pcap_rx;
682         eth_dev->tx_pkt_burst = eth_pcap_tx;
683
684         return 0;
685 }
686
687
688 int
689 rte_pmd_pcap_init(const char *name, const char *params)
690 {
691         unsigned numa_node, using_dumpers = 0;
692         int ret;
693         struct rte_kvargs *kvlist;
694         struct rx_pcaps pcaps;
695         struct tx_pcaps dumpers;
696
697         RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
698
699         numa_node = rte_socket_id();
700
701         gettimeofday(&start_time, NULL);
702         start_cycles = rte_get_timer_cycles();
703         hz = rte_get_timer_hz();
704
705         kvlist = rte_kvargs_parse(params, valid_arguments);
706         if (kvlist == NULL)
707                 return -1;
708
709         /*
710          * If iface argument is passed we open the NICs and use them for
711          * reading / writing
712          */
713         if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
714
715                 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
716                                 &open_rx_tx_iface, &pcaps.pcaps[0]);
717                 if (ret < 0)
718                         return -1;
719
720                 return rte_eth_from_pcaps(pcaps.pcaps, 1, pcaps.pcaps, 1,
721                                 numa_node, kvlist);
722         }
723
724         /*
725          * We check whether we want to open a RX stream from a real NIC or a
726          * pcap file
727          */
728         if ((pcaps.num_of_rx = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG))) {
729                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
730                                 &open_rx_pcap, &pcaps);
731         } else {
732                 pcaps.num_of_rx = rte_kvargs_count(kvlist,
733                                 ETH_PCAP_RX_IFACE_ARG);
734                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
735                                 &open_rx_iface, &pcaps);
736         }
737
738         if (ret < 0)
739                 return -1;
740
741         /*
742          * We check whether we want to open a TX stream to a real NIC or a
743          * pcap file
744          */
745         if ((dumpers.num_of_tx = rte_kvargs_count(kvlist,
746                         ETH_PCAP_TX_PCAP_ARG))) {
747                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
748                                 &open_tx_pcap, &dumpers);
749                 using_dumpers = 1;
750         } else {
751                 dumpers.num_of_tx = rte_kvargs_count(kvlist,
752                                 ETH_PCAP_TX_IFACE_ARG);
753                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
754                                 &open_tx_iface, &dumpers);
755         }
756
757         if (ret < 0)
758                 return -1;
759
760         if (using_dumpers)
761                 return rte_eth_from_pcaps_n_dumpers(pcaps.pcaps, pcaps.num_of_rx,
762                                 dumpers.dumpers, dumpers.num_of_tx, numa_node, kvlist);
763
764         return rte_eth_from_pcaps(pcaps.pcaps, pcaps.num_of_rx, dumpers.pcaps,
765                         dumpers.num_of_tx, numa_node, kvlist);
766
767 }
768