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