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