update Intel copyright years to 2014
[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  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <time.h>
35 #include <rte_mbuf.h>
36 #include <rte_ethdev.h>
37 #include <rte_malloc.h>
38 #include <rte_memcpy.h>
39 #include <rte_string_fns.h>
40 #include <rte_cycles.h>
41
42 #include "rte_eth_pcap.h"
43 #include "rte_eth_pcap_arg_parser.h"
44
45 #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535
46 #define RTE_ETH_PCAP_SNAPLEN 4096
47 #define RTE_ETH_PCAP_PROMISC 1
48 #define RTE_ETH_PCAP_TIMEOUT -1
49 #define RTE_ETH_PCAP_MBUFS 64
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 = (struct rte_pktmbuf_pool_private *)
144                                 ((char *)pcap_q->mb_pool + sizeof(struct rte_mempool));
145                 buf_size = (uint16_t) (mbp_priv->mbuf_data_room_size -
146                                 RTE_PKTMBUF_HEADROOM);
147
148                 if (header.len <= buf_size) {
149                         /* pcap packet will fit in the mbuf, go ahead and copy */
150                         rte_memcpy(mbuf->pkt.data, packet, header.len);
151                         mbuf->pkt.data_len = (uint16_t)header.len;
152                         mbuf->pkt.pkt_len = mbuf->pkt.data_len;
153                         bufs[i] = mbuf;
154                         num_rx++;
155                 } else {
156                         /* pcap packet will not fit in the mbuf, so drop packet */
157                         RTE_LOG(ERR, PMD, 
158                                         "PCAP packet %d bytes will not fit in mbuf (%d bytes)\n",
159                                         header.len, buf_size);
160                         rte_pktmbuf_free(mbuf);
161                 }
162         }
163         pcap_q->rx_pkts += num_rx;
164         return num_rx;
165 }
166
167 static inline void
168 calculate_timestamp(struct timeval *ts) {
169         uint64_t cycles;
170         struct timeval cur_time;
171
172         cycles = rte_get_timer_cycles() - start_cycles;
173         cur_time.tv_sec = cycles / hz;
174         cur_time.tv_usec = (cycles % hz) * 10e6 / hz;
175         timeradd(&start_time, &cur_time, ts);
176 }
177
178 /*
179  * Callback to handle writing packets to a pcap file.
180  */
181 static uint16_t
182 eth_pcap_tx_dumper(void *queue,
183                 struct rte_mbuf **bufs,
184                 uint16_t nb_pkts)
185 {
186         unsigned i;
187         struct rte_mbuf *mbuf;
188         struct pcap_tx_queue *dumper_q = queue;
189         uint16_t num_tx = 0;
190         struct pcap_pkthdr header;
191
192         if (dumper_q->dumper == NULL || nb_pkts == 0)
193                 return 0;
194
195         /* writes the nb_pkts packets to the previously opened pcap file dumper */
196         for (i = 0; i < nb_pkts; i++) {
197                 mbuf = bufs[i];
198                 calculate_timestamp(&header.ts);
199                 header.len = mbuf->pkt.data_len;
200                 header.caplen = header.len;
201                 pcap_dump((u_char*) dumper_q->dumper, &header, mbuf->pkt.data);
202                 rte_pktmbuf_free(mbuf);
203                 num_tx++;
204         }
205
206         /*
207          * Since there's no place to hook a callback when the forwarding
208          * process stops and to make sure the pcap file is actually written,
209          * we flush the pcap dumper within each burst.
210          */
211         pcap_dump_flush(dumper_q->dumper);
212         dumper_q->tx_pkts += num_tx;
213         dumper_q->err_pkts += nb_pkts - num_tx;
214         return num_tx;
215 }
216
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
248 static int
249 eth_dev_start(struct rte_eth_dev *dev)
250 {
251         dev->data->dev_link.link_status = 1;
252         return 0;
253 }
254
255 /*
256  * This function gets called when the current port gets stopped.
257  * Is the only place for us to close all the tx streams dumpers.
258  * If not called the dumpers will be flushed within each tx burst.
259  */
260 static void
261 eth_dev_stop(struct rte_eth_dev *dev)
262 {
263         unsigned i;
264         pcap_dumper_t *dumper;
265         pcap_t *pcap;
266         struct pmd_internals *internals = dev->data->dev_private;
267
268         for (i = 0; i < internals->nb_tx_queues; i++) {
269                 dumper = internals->tx_queue[i].dumper;
270                 if(dumper != NULL)
271                         pcap_dump_close(dumper);
272                 pcap = internals->tx_queue[i].pcap;
273                 if(pcap != NULL)
274                         pcap_close(pcap);
275         }
276
277         dev->data->dev_link.link_status = 0;
278 }
279
280 static int
281 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
282 {
283         return 0;
284 }
285
286 static void
287 eth_dev_info(struct rte_eth_dev *dev,
288                 struct rte_eth_dev_info *dev_info)
289 {
290         struct pmd_internals *internals = dev->data->dev_private;
291         dev_info->driver_name = drivername;
292         dev_info->max_mac_addrs = 1;
293         dev_info->max_rx_pktlen = (uint32_t) -1;
294         dev_info->max_rx_queues = (uint16_t)internals->nb_rx_queues;
295         dev_info->max_tx_queues = (uint16_t)internals->nb_tx_queues;
296         dev_info->min_rx_bufsize = 0;
297         dev_info->pci_dev = NULL;
298 }
299
300 static void
301 eth_stats_get(struct rte_eth_dev *dev,
302                 struct rte_eth_stats *igb_stats)
303 {
304         unsigned i;
305         unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
306         const struct pmd_internals *internal = dev->data->dev_private;
307
308         memset(igb_stats, 0, sizeof(*igb_stats));
309         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && i < internal->nb_rx_queues;
310                         i++) {
311                 igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts;
312                 rx_total += igb_stats->q_ipackets[i];
313         }
314
315         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && i < internal->nb_tx_queues;
316                         i++) {
317                 igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts;
318                 igb_stats->q_errors[i] = internal->tx_queue[i].err_pkts;
319                 tx_total += igb_stats->q_opackets[i];
320                 tx_err_total += igb_stats->q_errors[i];
321         }
322
323         igb_stats->ipackets = rx_total;
324         igb_stats->opackets = tx_total;
325         igb_stats->oerrors = tx_err_total;
326 }
327
328 static void
329 eth_stats_reset(struct rte_eth_dev *dev)
330 {
331         unsigned i;
332         struct pmd_internals *internal = dev->data->dev_private;
333         for (i = 0; i < internal->nb_rx_queues; i++)
334                 internal->rx_queue[i].rx_pkts = 0;
335         for (i = 0; i < internal->nb_tx_queues; i++) {
336                 internal->tx_queue[i].tx_pkts = 0;
337                 internal->tx_queue[i].err_pkts = 0;
338         }
339 }
340
341 static void
342 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
343 {
344 }
345
346 static void
347 eth_queue_release(void *q __rte_unused)
348 {
349 }
350
351 static int
352 eth_link_update(struct rte_eth_dev *dev __rte_unused,
353                 int wait_to_complete __rte_unused)
354 {
355         return 0;
356 }
357
358 static int
359 eth_rx_queue_setup(struct rte_eth_dev *dev,
360                 uint16_t rx_queue_id,
361                 uint16_t nb_rx_desc __rte_unused,
362                 unsigned int socket_id __rte_unused,
363                 const struct rte_eth_rxconf *rx_conf __rte_unused,
364                 struct rte_mempool *mb_pool)
365 {
366         struct pmd_internals *internals = dev->data->dev_private;
367         struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
368         pcap_q->mb_pool = mb_pool;
369         dev->data->rx_queues[rx_queue_id] = pcap_q;
370         return 0;
371 }
372
373 static int
374 eth_tx_queue_setup(struct rte_eth_dev *dev,
375                 uint16_t tx_queue_id,
376                 uint16_t nb_tx_desc __rte_unused,
377                 unsigned int socket_id __rte_unused,
378                 const struct rte_eth_txconf *tx_conf __rte_unused)
379 {
380
381         struct pmd_internals *internals = dev->data->dev_private;
382         dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
383         return 0;
384 }
385
386 static struct eth_dev_ops ops = {
387                 .dev_start = eth_dev_start,
388                 .dev_stop =     eth_dev_stop,
389                 .dev_close = eth_dev_close,
390                 .dev_configure = eth_dev_configure,
391                 .dev_infos_get = eth_dev_info,
392                 .rx_queue_setup = eth_rx_queue_setup,
393                 .tx_queue_setup = eth_tx_queue_setup,
394                 .rx_queue_release = eth_queue_release,
395                 .tx_queue_release = eth_queue_release,
396                 .link_update = eth_link_update,
397                 .stats_get = eth_stats_get,
398                 .stats_reset = eth_stats_reset,
399 };
400
401 /*
402  * Function handler that opens the pcap file for reading a stores a
403  * reference of it for use it later on.
404  */
405 static int
406 open_rx_pcap(char *value, void *extra_args)
407 {
408         unsigned i;
409         char *pcap_filename = value;
410         struct rx_pcaps *pcaps = extra_args;
411         pcap_t *rx_pcap;
412
413         for (i = 0; i < pcaps->num_of_rx; i++) {
414                 if ((rx_pcap = pcap_open_offline(pcap_filename, errbuf)) == NULL) {
415                         RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename, errbuf);
416                         return -1;
417                 }
418                 pcaps->pcaps[i] = rx_pcap;
419         }
420
421         return 0;
422 }
423
424 /*
425  * Opens a pcap file for writing and stores a reference to it
426  * for use it later on.
427  */
428 static int
429 open_tx_pcap(char *value, void *extra_args)
430 {
431         unsigned i;
432         char *pcap_filename = value;
433         struct tx_pcaps *dumpers = extra_args;
434         pcap_t *tx_pcap;
435         pcap_dumper_t *dumper;
436
437         for (i = 0; i < dumpers->num_of_tx; i++) {
438                 /*
439                  * We need to create a dummy empty pcap_t to use it
440                  * with pcap_dump_open(). We create big enough an Ethernet
441                  * pcap holder.
442                  */
443                 if ((tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN))
444                                 == NULL) {
445                         RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
446                         return -1;
447                 }
448
449                 /* The dumper is created using the previous pcap_t reference */
450                 if ((dumper = pcap_dump_open(tx_pcap, pcap_filename)) == NULL) {
451                         RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n", pcap_filename);
452                         return -1;
453                 }
454                 dumpers->dumpers[i] = dumper;
455         }
456
457         return 0;
458 }
459
460 /*
461  * pcap_open_live wrapper function
462  */
463 static inline int
464 open_iface_live(const char *iface, pcap_t **pcap) {
465         *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
466                         RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
467
468         if (*pcap == NULL) {
469                 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
470                 return -1;
471         }
472         return 0;
473 }
474
475 /*
476  * Opens an interface for reading and writing
477  */
478 static inline int
479 open_rx_tx_iface(char *value, void *extra_args)
480 {
481         const char *iface = value;
482         pcap_t **pcap = extra_args;
483
484         if(open_iface_live(iface, pcap) < 0)
485                 return -1;
486         return 0;
487 }
488
489 /*
490  * Opens a NIC for reading packets from it
491  */
492 static inline int
493 open_rx_iface(char *value, void *extra_args)
494 {
495         unsigned i;
496         const char *iface = value;
497         struct rx_pcaps *pcaps = extra_args;
498         pcap_t *pcap = NULL;
499
500         for (i = 0; i < pcaps->num_of_rx; i++) {
501                 if(open_iface_live(iface, &pcap) < 0)
502                         return -1;
503                 pcaps->pcaps[i] = pcap;
504         }
505
506         return 0;
507 }
508
509 /*
510  * Opens a NIC for writing packets to it
511  */
512 static inline int
513 open_tx_iface(char *value, void *extra_args)
514 {
515         unsigned i;
516         const char *iface = value;
517         struct tx_pcaps *pcaps = extra_args;
518         pcap_t *pcap;
519
520         for (i = 0; i < pcaps->num_of_tx; i++) {
521                 if(open_iface_live(iface, &pcap) < 0)
522                         return -1;
523                 pcaps->pcaps[i] = pcap;
524         }
525
526         return 0;
527 }
528
529
530 static int
531 rte_pmd_init_internals(const unsigned nb_rx_queues,
532                 const unsigned nb_tx_queues,
533                 const unsigned numa_node,
534                 struct pmd_internals **internals,
535                 struct rte_eth_dev **eth_dev)
536 {
537         struct rte_eth_dev_data *data = NULL;
538         struct rte_pci_device *pci_dev = NULL;
539
540         RTE_LOG(INFO, PMD,
541                         "Creating pcap-backed ethdev on numa socket %u\n", numa_node);
542
543         /* now do all data allocation - for eth_dev structure, dummy pci driver
544          * and internal (private) data
545          */
546         data = rte_zmalloc_socket(NULL, sizeof(*data), 0, numa_node);
547         if (data == NULL)
548                 goto error;
549
550         pci_dev = rte_zmalloc_socket(NULL, sizeof(*pci_dev), 0, numa_node);
551         if (pci_dev == NULL)
552                 goto error;
553
554         *internals = rte_zmalloc_socket(NULL, sizeof(**internals), 0, numa_node);
555         if (*internals == NULL)
556                 goto error;
557
558         /* reserve an ethdev entry */
559         *eth_dev = rte_eth_dev_allocate();
560         if (*eth_dev == NULL)
561                 goto error;
562
563         /* now put it all together
564          * - store queue data in internals,
565          * - store numa_node info in pci_driver
566          * - point eth_dev_data to internals and pci_driver
567          * - and point eth_dev structure to new eth_dev_data structure
568          */
569         /* NOTE: we'll replace the data element, of originally allocated eth_dev
570          * so the rings are local per-process */
571
572         (*internals)->nb_rx_queues = nb_rx_queues;
573         (*internals)->nb_tx_queues = nb_tx_queues;
574
575         pci_dev->numa_node = numa_node;
576
577         data->dev_private = *internals;
578         data->port_id = (*eth_dev)->data->port_id;
579         data->nb_rx_queues = (uint16_t)nb_rx_queues;
580         data->nb_tx_queues = (uint16_t)nb_tx_queues;
581         data->dev_link = pmd_link;
582         data->mac_addrs = &eth_addr;
583
584         (*eth_dev)->data = data;
585         (*eth_dev)->dev_ops = &ops;
586         (*eth_dev)->pci_dev = pci_dev;
587
588         return 0;
589
590         error: if (data)
591                 rte_free(data);
592         if (pci_dev)
593                 rte_free(pci_dev);
594         if (*internals)
595                 rte_free(*internals);
596         return -1;
597 }
598
599 int
600 rte_eth_from_pcaps_n_dumpers(pcap_t * const rx_queues[],
601                 const unsigned nb_rx_queues,
602                 pcap_dumper_t * const tx_queues[],
603                 const unsigned nb_tx_queues,
604                 const unsigned numa_node)
605 {
606         struct pmd_internals *internals = NULL;
607         struct rte_eth_dev *eth_dev = NULL;
608         unsigned i;
609
610         /* do some parameter checking */
611         if (rx_queues == NULL && nb_rx_queues > 0)
612                 return -1;
613         if (tx_queues == NULL && nb_tx_queues > 0)
614                 return -1;
615
616         if (rte_pmd_init_internals(nb_rx_queues, nb_tx_queues, numa_node,
617                         &internals, &eth_dev) < 0)
618                 return -1;
619
620         for (i = 0; i < nb_rx_queues; i++) {
621                 internals->rx_queue->pcap = rx_queues[i];
622         }
623         for (i = 0; i < nb_tx_queues; i++) {
624                 internals->tx_queue->dumper = tx_queues[i];
625         }
626
627         eth_dev->rx_pkt_burst = eth_pcap_rx;
628         eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
629
630         return 0;
631 }
632
633 int
634 rte_eth_from_pcaps(pcap_t * const rx_queues[],
635                 const unsigned nb_rx_queues,
636                 pcap_t * const tx_queues[],
637                 const unsigned nb_tx_queues,
638                 const unsigned numa_node)
639 {
640         struct pmd_internals *internals = NULL;
641         struct rte_eth_dev *eth_dev = NULL;
642         unsigned i;
643
644         /* do some parameter checking */
645         if (rx_queues == NULL && nb_rx_queues > 0)
646                 return -1;
647         if (tx_queues == NULL && nb_tx_queues > 0)
648                 return -1;
649
650         if (rte_pmd_init_internals(nb_rx_queues, nb_tx_queues, numa_node,
651                         &internals, &eth_dev) < 0)
652                 return -1;
653
654         for (i = 0; i < nb_rx_queues; i++) {
655                 internals->rx_queue->pcap = rx_queues[i];
656         }
657         for (i = 0; i < nb_tx_queues; i++) {
658                 internals->tx_queue->pcap = tx_queues[i];
659         }
660
661         eth_dev->rx_pkt_burst = eth_pcap_rx;
662         eth_dev->tx_pkt_burst = eth_pcap_tx;
663
664         return 0;
665 }
666
667
668 int
669 rte_pmd_pcap_init(const char *name, const char *params)
670 {
671         unsigned numa_node, using_dumpers = 0;
672         int ret;
673         struct args_dict dict;
674         struct rx_pcaps pcaps;
675         struct tx_pcaps dumpers;
676
677         rte_eth_pcap_init_args_dict(&dict);
678
679         numa_node = rte_socket_id();
680
681         gettimeofday(&start_time, NULL);
682         start_cycles = rte_get_timer_cycles();
683         hz = rte_get_timer_hz();
684
685         if (rte_eth_pcap_parse_args(&dict, name, params, valid_arguments) < 0)
686                 return -1;
687
688         /*
689          * If iface argument is passed we open the NICs and use them for
690          * reading / writing
691          */
692         if (rte_eth_pcap_num_of_args(&dict, ETH_PCAP_IFACE_ARG) == 1) {
693
694                 ret = rte_eth_pcap_post_process_arguments(&dict, ETH_PCAP_IFACE_ARG,
695                                 &open_rx_tx_iface, &pcaps.pcaps[0]);
696                 if (ret < 0)
697                         return -1;
698
699                 return rte_eth_from_pcaps(pcaps.pcaps, 1, pcaps.pcaps, 1, numa_node);
700         }
701
702         /*
703          * We check whether we want to open a RX stream from a real NIC or a
704          * pcap file
705          */
706         if ((pcaps.num_of_rx = rte_eth_pcap_num_of_args(&dict, ETH_PCAP_RX_PCAP_ARG))) {
707                 ret = rte_eth_pcap_post_process_arguments(&dict, ETH_PCAP_RX_PCAP_ARG,
708                                 &open_rx_pcap, &pcaps);
709         } else {
710                 pcaps.num_of_rx = rte_eth_pcap_num_of_args(&dict,
711                                 ETH_PCAP_RX_IFACE_ARG);
712                 ret = rte_eth_pcap_post_process_arguments(&dict, ETH_PCAP_RX_IFACE_ARG,
713                                 &open_rx_iface, &pcaps);
714         }
715
716         if (ret < 0)
717                 return -1;
718
719         /*
720          * We check whether we want to open a TX stream to a real NIC or a
721          * pcap file
722          */
723         if ((dumpers.num_of_tx = rte_eth_pcap_num_of_args(&dict,
724                         ETH_PCAP_TX_PCAP_ARG))) {
725                 ret = rte_eth_pcap_post_process_arguments(&dict, ETH_PCAP_TX_PCAP_ARG,
726                                 &open_tx_pcap, &dumpers);
727                 using_dumpers = 1;
728         } else {
729                 dumpers.num_of_tx = rte_eth_pcap_num_of_args(&dict,
730                                 ETH_PCAP_TX_IFACE_ARG);
731                 ret = rte_eth_pcap_post_process_arguments(&dict, ETH_PCAP_TX_IFACE_ARG,
732                                 &open_tx_iface, &dumpers);
733         }
734
735         if (ret < 0)
736                 return -1;
737
738         if (using_dumpers)
739                 return rte_eth_from_pcaps_n_dumpers(pcaps.pcaps, pcaps.num_of_rx,
740                                 dumpers.dumpers, dumpers.num_of_tx, numa_node);
741
742         return rte_eth_from_pcaps(pcaps.pcaps, pcaps.num_of_rx, dumpers.pcaps,
743                         dumpers.num_of_tx, numa_node);
744
745 }
746