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