ixgbe: fix release queue mbufs
[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 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.len <= 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.len);
227                         mbuf->data_len = (uint16_t)header.len;
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.len) == -1))
234                                 break;
235                 }
236
237                 mbuf->pkt_len = (uint16_t)header.len;
238                 mbuf->port = pcap_q->in_port;
239                 bufs[num_rx] = mbuf;
240                 num_rx++;
241                 rx_bytes += header.len;
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 static struct eth_driver rte_pcap_pmd = {
621         .pci_drv = {
622                 .name = "rte_pcap_pmd",
623                 .drv_flags = RTE_PCI_DRV_DETACHABLE,
624         },
625 };
626
627 /*
628  * Function handler that opens the pcap file for reading a stores a
629  * reference of it for use it later on.
630  */
631 static int
632 open_rx_pcap(const char *key, const char *value, void *extra_args)
633 {
634         unsigned i;
635         const char *pcap_filename = value;
636         struct rx_pcaps *pcaps = extra_args;
637         pcap_t *pcap = NULL;
638
639         for (i = 0; i < pcaps->num_of_rx; i++) {
640                 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
641                         return -1;
642
643                 pcaps->pcaps[i] = pcap;
644                 pcaps->names[i] = pcap_filename;
645                 pcaps->types[i] = key;
646         }
647
648         return 0;
649 }
650
651 static int
652 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
653 {
654         if ((*pcap = pcap_open_offline(pcap_filename, errbuf)) == NULL) {
655                 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename, errbuf);
656                 return -1;
657         }
658         return 0;
659 }
660
661 /*
662  * Opens a pcap file for writing and stores a reference to it
663  * for use it later on.
664  */
665 static int
666 open_tx_pcap(const char *key, const char *value, void *extra_args)
667 {
668         unsigned i;
669         const char *pcap_filename = value;
670         struct tx_pcaps *dumpers = extra_args;
671         pcap_dumper_t *dumper;
672
673         for (i = 0; i < dumpers->num_of_tx; i++) {
674                 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
675                         return -1;
676
677                 dumpers->dumpers[i] = dumper;
678                 dumpers->names[i] = pcap_filename;
679                 dumpers->types[i] = key;
680         }
681
682         return 0;
683 }
684
685 static int
686 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
687 {
688         pcap_t *tx_pcap;
689         /*
690          * We need to create a dummy empty pcap_t to use it
691          * with pcap_dump_open(). We create big enough an Ethernet
692          * pcap holder.
693          */
694
695         if ((tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN))
696                         == NULL) {
697                 RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
698                 return -1;
699         }
700
701         /* The dumper is created using the previous pcap_t reference */
702         if ((*dumper = pcap_dump_open(tx_pcap, pcap_filename)) == NULL) {
703                 RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n", pcap_filename);
704                 return -1;
705         }
706
707         return 0;
708 }
709
710 /*
711  * pcap_open_live wrapper function
712  */
713 static inline int
714 open_iface_live(const char *iface, pcap_t **pcap) {
715         *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
716                         RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
717
718         if (*pcap == NULL) {
719                 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
720                 return -1;
721         }
722         return 0;
723 }
724
725 /*
726  * Opens an interface for reading and writing
727  */
728 static inline int
729 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
730 {
731         const char *iface = value;
732         struct rx_pcaps *pcaps = extra_args;
733         pcap_t *pcap = NULL;
734
735         if (open_single_iface(iface, &pcap) < 0)
736                 return -1;
737
738         pcaps->pcaps[0] = pcap;
739         pcaps->names[0] = iface;
740         pcaps->types[0] = key;
741
742         return 0;
743 }
744
745 /*
746  * Opens a NIC for reading packets from it
747  */
748 static inline int
749 open_rx_iface(const char *key, const char *value, void *extra_args)
750 {
751         unsigned i;
752         const char *iface = value;
753         struct rx_pcaps *pcaps = extra_args;
754         pcap_t *pcap = NULL;
755
756         for (i = 0; i < pcaps->num_of_rx; 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 /*
768  * Opens a NIC for writing packets to it
769  */
770 static int
771 open_tx_iface(const char *key, const char *value, void *extra_args)
772 {
773         unsigned i;
774         const char *iface = value;
775         struct tx_pcaps *pcaps = extra_args;
776         pcap_t *pcap;
777
778         for (i = 0; i < pcaps->num_of_tx; i++) {
779                 if (open_single_iface(iface, &pcap) < 0)
780                         return -1;
781                 pcaps->pcaps[i] = pcap;
782                 pcaps->names[i] = iface;
783                 pcaps->types[i] = key;
784         }
785
786         return 0;
787 }
788
789 static int
790 open_single_iface(const char *iface, pcap_t **pcap)
791 {
792         if (open_iface_live(iface, pcap) < 0) {
793                 RTE_LOG(ERR, PMD, "Couldn't open interface %s\n", iface);
794                 return -1;
795         }
796
797         return 0;
798 }
799
800 static int
801 rte_pmd_init_internals(const char *name, const unsigned nb_rx_queues,
802                 const unsigned nb_tx_queues,
803                 const unsigned numa_node,
804                 struct pmd_internals **internals,
805                 struct rte_eth_dev **eth_dev,
806                 struct rte_kvargs *kvlist)
807 {
808         struct rte_eth_dev_data *data = NULL;
809         struct rte_pci_device *pci_dev = NULL;
810         unsigned k_idx;
811         struct rte_kvargs_pair *pair = NULL;
812
813         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
814                 pair = &kvlist->pairs[k_idx];
815                 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
816                         break;
817         }
818
819         RTE_LOG(INFO, PMD,
820                         "Creating pcap-backed ethdev on numa socket %u\n", numa_node);
821
822         /* now do all data allocation - for eth_dev structure, dummy pci driver
823          * and internal (private) data
824          */
825         data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
826         if (data == NULL)
827                 goto error;
828
829         pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, numa_node);
830         if (pci_dev == NULL)
831                 goto error;
832
833         *internals = rte_zmalloc_socket(name, sizeof(**internals), 0, numa_node);
834         if (*internals == NULL)
835                 goto error;
836
837         /* reserve an ethdev entry */
838         *eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
839         if (*eth_dev == NULL)
840                 goto error;
841
842         /* check length of device name */
843         if ((strlen((*eth_dev)->data->name) + 1) > sizeof(data->name))
844                 goto error;
845
846         /* now put it all together
847          * - store queue data in internals,
848          * - store numa_node info in pci_driver
849          * - point eth_dev_data to internals and pci_driver
850          * - and point eth_dev structure to new eth_dev_data structure
851          */
852         /* NOTE: we'll replace the data element, of originally allocated eth_dev
853          * so the rings are local per-process */
854
855         (*internals)->nb_rx_queues = nb_rx_queues;
856         (*internals)->nb_tx_queues = nb_tx_queues;
857
858         if (pair == NULL)
859                 (*internals)->if_index = 0;
860         else
861                 (*internals)->if_index = if_nametoindex(pair->value);
862
863         pci_dev->numa_node = numa_node;
864
865         data->dev_private = *internals;
866         data->port_id = (*eth_dev)->data->port_id;
867         snprintf(data->name, sizeof(data->name), "%s", (*eth_dev)->data->name);
868         data->nb_rx_queues = (uint16_t)nb_rx_queues;
869         data->nb_tx_queues = (uint16_t)nb_tx_queues;
870         data->dev_link = pmd_link;
871         data->mac_addrs = &eth_addr;
872         strncpy(data->name,
873                 (*eth_dev)->data->name, strlen((*eth_dev)->data->name));
874
875         (*eth_dev)->data = data;
876         (*eth_dev)->dev_ops = &ops;
877         (*eth_dev)->pci_dev = pci_dev;
878         (*eth_dev)->driver = &rte_pcap_pmd;
879
880         return 0;
881
882 error:
883         rte_free(data);
884         rte_free(pci_dev);
885         rte_free(*internals);
886
887         return -1;
888 }
889
890 static int
891 rte_eth_from_pcaps_n_dumpers(const char *name,
892                 struct rx_pcaps *rx_queues,
893                 const unsigned nb_rx_queues,
894                 struct tx_pcaps *tx_queues,
895                 const unsigned nb_tx_queues,
896                 const unsigned numa_node,
897                 struct rte_kvargs *kvlist)
898 {
899         struct pmd_internals *internals = NULL;
900         struct rte_eth_dev *eth_dev = NULL;
901         unsigned i;
902
903         /* do some parameter checking */
904         if (rx_queues == NULL && nb_rx_queues > 0)
905                 return -1;
906         if (tx_queues == NULL && nb_tx_queues > 0)
907                 return -1;
908
909         if (rte_pmd_init_internals(name, nb_rx_queues, nb_tx_queues, numa_node,
910                         &internals, &eth_dev, kvlist) < 0)
911                 return -1;
912
913         for (i = 0; i < nb_rx_queues; i++) {
914                 internals->rx_queue[i].pcap = rx_queues->pcaps[i];
915                 snprintf(internals->rx_queue[i].name,
916                         sizeof(internals->rx_queue[i].name), "%s",
917                         rx_queues->names[i]);
918                 snprintf(internals->rx_queue[i].type,
919                         sizeof(internals->rx_queue[i].type), "%s",
920                         rx_queues->types[i]);
921         }
922         for (i = 0; i < nb_tx_queues; i++) {
923                 internals->tx_queue[i].dumper = tx_queues->dumpers[i];
924                 snprintf(internals->tx_queue[i].name,
925                         sizeof(internals->tx_queue[i].name), "%s",
926                         tx_queues->names[i]);
927                 snprintf(internals->tx_queue[i].type,
928                         sizeof(internals->tx_queue[i].type), "%s",
929                         tx_queues->types[i]);
930         }
931
932         /* using multiple pcaps/interfaces */
933         internals->single_iface = 0;
934
935         eth_dev->rx_pkt_burst = eth_pcap_rx;
936         eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
937
938         return 0;
939 }
940
941 static int
942 rte_eth_from_pcaps(const char *name,
943                 struct rx_pcaps *rx_queues,
944                 const unsigned nb_rx_queues,
945                 struct tx_pcaps *tx_queues,
946                 const unsigned nb_tx_queues,
947                 const unsigned numa_node,
948                 struct rte_kvargs *kvlist,
949                 int single_iface)
950 {
951         struct pmd_internals *internals = NULL;
952         struct rte_eth_dev *eth_dev = NULL;
953         unsigned i;
954
955         /* do some parameter checking */
956         if (rx_queues == NULL && nb_rx_queues > 0)
957                 return -1;
958         if (tx_queues == NULL && nb_tx_queues > 0)
959                 return -1;
960
961         if (rte_pmd_init_internals(name, nb_rx_queues, nb_tx_queues, numa_node,
962                         &internals, &eth_dev, kvlist) < 0)
963                 return -1;
964
965         for (i = 0; i < nb_rx_queues; i++) {
966                 internals->rx_queue[i].pcap = rx_queues->pcaps[i];
967                 snprintf(internals->rx_queue[i].name,
968                         sizeof(internals->rx_queue[i].name), "%s",
969                         rx_queues->names[i]);
970                 snprintf(internals->rx_queue[i].type,
971                         sizeof(internals->rx_queue[i].type), "%s",
972                         rx_queues->types[i]);
973         }
974         for (i = 0; i < nb_tx_queues; i++) {
975                 internals->tx_queue[i].dumper = tx_queues->dumpers[i];
976                 snprintf(internals->tx_queue[i].name,
977                         sizeof(internals->tx_queue[i].name), "%s",
978                         tx_queues->names[i]);
979                 snprintf(internals->tx_queue[i].type,
980                         sizeof(internals->tx_queue[i].type), "%s",
981                         tx_queues->types[i]);
982         }
983
984         /* store wether we are using a single interface for rx/tx or not */
985         internals->single_iface = single_iface;
986
987         eth_dev->rx_pkt_burst = eth_pcap_rx;
988         eth_dev->tx_pkt_burst = eth_pcap_tx;
989
990         return 0;
991 }
992
993
994 static int
995 rte_pmd_pcap_devinit(const char *name, const char *params)
996 {
997         unsigned numa_node, using_dumpers = 0;
998         int ret;
999         struct rte_kvargs *kvlist;
1000         struct rx_pcaps pcaps;
1001         struct tx_pcaps dumpers;
1002
1003         RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
1004
1005         numa_node = rte_socket_id();
1006
1007         gettimeofday(&start_time, NULL);
1008         start_cycles = rte_get_timer_cycles();
1009         hz = rte_get_timer_hz();
1010
1011         kvlist = rte_kvargs_parse(params, valid_arguments);
1012         if (kvlist == NULL)
1013                 return -1;
1014
1015         /*
1016          * If iface argument is passed we open the NICs and use them for
1017          * reading / writing
1018          */
1019         if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
1020
1021                 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
1022                                 &open_rx_tx_iface, &pcaps);
1023                 if (ret < 0)
1024                         goto free_kvlist;
1025                 dumpers.pcaps[0] = pcaps.pcaps[0];
1026                 dumpers.names[0] = pcaps.names[0];
1027                 dumpers.types[0] = pcaps.types[0];
1028                 ret = rte_eth_from_pcaps(name, &pcaps, 1, &dumpers, 1,
1029                                 numa_node, kvlist, 1);
1030                 goto free_kvlist;
1031         }
1032
1033         /*
1034          * We check whether we want to open a RX stream from a real NIC or a
1035          * pcap file
1036          */
1037         if ((pcaps.num_of_rx = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG))) {
1038                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
1039                                 &open_rx_pcap, &pcaps);
1040         } else {
1041                 pcaps.num_of_rx = rte_kvargs_count(kvlist,
1042                                 ETH_PCAP_RX_IFACE_ARG);
1043                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
1044                                 &open_rx_iface, &pcaps);
1045         }
1046
1047         if (ret < 0)
1048                 goto free_kvlist;
1049
1050         /*
1051          * We check whether we want to open a TX stream to a real NIC or a
1052          * pcap file
1053          */
1054         if ((dumpers.num_of_tx = rte_kvargs_count(kvlist,
1055                         ETH_PCAP_TX_PCAP_ARG))) {
1056                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1057                                 &open_tx_pcap, &dumpers);
1058                 using_dumpers = 1;
1059         } else {
1060                 dumpers.num_of_tx = rte_kvargs_count(kvlist,
1061                                 ETH_PCAP_TX_IFACE_ARG);
1062                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1063                                 &open_tx_iface, &dumpers);
1064         }
1065
1066         if (ret < 0)
1067                 goto free_kvlist;
1068
1069         if (using_dumpers)
1070                 ret = rte_eth_from_pcaps_n_dumpers(name, &pcaps, pcaps.num_of_rx,
1071                                 &dumpers, dumpers.num_of_tx, numa_node, kvlist);
1072         else
1073                 ret = rte_eth_from_pcaps(name, &pcaps, pcaps.num_of_rx, &dumpers,
1074                         dumpers.num_of_tx, numa_node, kvlist, 0);
1075
1076 free_kvlist:
1077         rte_kvargs_free(kvlist);
1078         return ret;
1079 }
1080
1081 static int
1082 rte_pmd_pcap_devuninit(const char *name)
1083 {
1084         struct rte_eth_dev *eth_dev = NULL;
1085
1086         RTE_LOG(INFO, PMD, "Closing pcap ethdev on numa socket %u\n",
1087                         rte_socket_id());
1088
1089         if (name == NULL)
1090                 return -1;
1091
1092         /* reserve an ethdev entry */
1093         eth_dev = rte_eth_dev_allocated(name);
1094         if (eth_dev == NULL)
1095                 return -1;
1096
1097         rte_free(eth_dev->data->dev_private);
1098         rte_free(eth_dev->data);
1099         rte_free(eth_dev->pci_dev);
1100
1101         rte_eth_dev_release_port(eth_dev);
1102
1103         return 0;
1104 }
1105
1106 static struct rte_driver pmd_pcap_drv = {
1107         .name = "eth_pcap",
1108         .type = PMD_VDEV,
1109         .init = rte_pmd_pcap_devinit,
1110         .uninit = rte_pmd_pcap_devuninit,
1111 };
1112
1113 PMD_REGISTER_DRIVER(pmd_pcap_drv);