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