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