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