drivers: copy fake PCI device info to ethdev data
[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.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 eth_dev
849          * - point eth_dev_data to internals
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         (*eth_dev)->data->dev_flags = RTE_ETH_DEV_DETACHABLE;
880         (*eth_dev)->driver = NULL;
881         (*eth_dev)->data->kdrv = RTE_KDRV_NONE;
882         (*eth_dev)->data->drv_name = drivername;
883         (*eth_dev)->data->numa_node = numa_node;
884
885         return 0;
886
887 error:
888         rte_free(data);
889         rte_free(pci_dev);
890         rte_free(*internals);
891
892         return -1;
893 }
894
895 static int
896 rte_eth_from_pcaps_n_dumpers(const char *name,
897                 struct rx_pcaps *rx_queues,
898                 const unsigned nb_rx_queues,
899                 struct tx_pcaps *tx_queues,
900                 const unsigned nb_tx_queues,
901                 const unsigned numa_node,
902                 struct rte_kvargs *kvlist)
903 {
904         struct pmd_internals *internals = NULL;
905         struct rte_eth_dev *eth_dev = NULL;
906         unsigned i;
907
908         /* do some parameter checking */
909         if (rx_queues == NULL && nb_rx_queues > 0)
910                 return -1;
911         if (tx_queues == NULL && nb_tx_queues > 0)
912                 return -1;
913
914         if (rte_pmd_init_internals(name, nb_rx_queues, nb_tx_queues, numa_node,
915                         &internals, &eth_dev, kvlist) < 0)
916                 return -1;
917
918         for (i = 0; i < nb_rx_queues; i++) {
919                 internals->rx_queue[i].pcap = rx_queues->pcaps[i];
920                 snprintf(internals->rx_queue[i].name,
921                         sizeof(internals->rx_queue[i].name), "%s",
922                         rx_queues->names[i]);
923                 snprintf(internals->rx_queue[i].type,
924                         sizeof(internals->rx_queue[i].type), "%s",
925                         rx_queues->types[i]);
926         }
927         for (i = 0; i < nb_tx_queues; i++) {
928                 internals->tx_queue[i].dumper = tx_queues->dumpers[i];
929                 snprintf(internals->tx_queue[i].name,
930                         sizeof(internals->tx_queue[i].name), "%s",
931                         tx_queues->names[i]);
932                 snprintf(internals->tx_queue[i].type,
933                         sizeof(internals->tx_queue[i].type), "%s",
934                         tx_queues->types[i]);
935         }
936
937         /* using multiple pcaps/interfaces */
938         internals->single_iface = 0;
939
940         eth_dev->rx_pkt_burst = eth_pcap_rx;
941         eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
942
943         return 0;
944 }
945
946 static int
947 rte_eth_from_pcaps(const char *name,
948                 struct rx_pcaps *rx_queues,
949                 const unsigned nb_rx_queues,
950                 struct tx_pcaps *tx_queues,
951                 const unsigned nb_tx_queues,
952                 const unsigned numa_node,
953                 struct rte_kvargs *kvlist,
954                 int single_iface)
955 {
956         struct pmd_internals *internals = NULL;
957         struct rte_eth_dev *eth_dev = NULL;
958         unsigned i;
959
960         /* do some parameter checking */
961         if (rx_queues == NULL && nb_rx_queues > 0)
962                 return -1;
963         if (tx_queues == NULL && nb_tx_queues > 0)
964                 return -1;
965
966         if (rte_pmd_init_internals(name, nb_rx_queues, nb_tx_queues, numa_node,
967                         &internals, &eth_dev, kvlist) < 0)
968                 return -1;
969
970         for (i = 0; i < nb_rx_queues; i++) {
971                 internals->rx_queue[i].pcap = rx_queues->pcaps[i];
972                 snprintf(internals->rx_queue[i].name,
973                         sizeof(internals->rx_queue[i].name), "%s",
974                         rx_queues->names[i]);
975                 snprintf(internals->rx_queue[i].type,
976                         sizeof(internals->rx_queue[i].type), "%s",
977                         rx_queues->types[i]);
978         }
979         for (i = 0; i < nb_tx_queues; i++) {
980                 internals->tx_queue[i].dumper = tx_queues->dumpers[i];
981                 snprintf(internals->tx_queue[i].name,
982                         sizeof(internals->tx_queue[i].name), "%s",
983                         tx_queues->names[i]);
984                 snprintf(internals->tx_queue[i].type,
985                         sizeof(internals->tx_queue[i].type), "%s",
986                         tx_queues->types[i]);
987         }
988
989         /* store wether we are using a single interface for rx/tx or not */
990         internals->single_iface = single_iface;
991
992         eth_dev->rx_pkt_burst = eth_pcap_rx;
993         eth_dev->tx_pkt_burst = eth_pcap_tx;
994
995         return 0;
996 }
997
998
999 static int
1000 rte_pmd_pcap_devinit(const char *name, const char *params)
1001 {
1002         unsigned numa_node, using_dumpers = 0;
1003         int ret;
1004         struct rte_kvargs *kvlist;
1005         struct rx_pcaps pcaps;
1006         struct tx_pcaps dumpers;
1007
1008         RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
1009
1010         numa_node = rte_socket_id();
1011
1012         gettimeofday(&start_time, NULL);
1013         start_cycles = rte_get_timer_cycles();
1014         hz = rte_get_timer_hz();
1015
1016         kvlist = rte_kvargs_parse(params, valid_arguments);
1017         if (kvlist == NULL)
1018                 return -1;
1019
1020         /*
1021          * If iface argument is passed we open the NICs and use them for
1022          * reading / writing
1023          */
1024         if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
1025
1026                 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
1027                                 &open_rx_tx_iface, &pcaps);
1028                 if (ret < 0)
1029                         goto free_kvlist;
1030                 dumpers.pcaps[0] = pcaps.pcaps[0];
1031                 dumpers.names[0] = pcaps.names[0];
1032                 dumpers.types[0] = pcaps.types[0];
1033                 ret = rte_eth_from_pcaps(name, &pcaps, 1, &dumpers, 1,
1034                                 numa_node, kvlist, 1);
1035                 goto free_kvlist;
1036         }
1037
1038         /*
1039          * We check whether we want to open a RX stream from a real NIC or a
1040          * pcap file
1041          */
1042         if ((pcaps.num_of_rx = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG))) {
1043                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
1044                                 &open_rx_pcap, &pcaps);
1045         } else {
1046                 pcaps.num_of_rx = rte_kvargs_count(kvlist,
1047                                 ETH_PCAP_RX_IFACE_ARG);
1048                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
1049                                 &open_rx_iface, &pcaps);
1050         }
1051
1052         if (ret < 0)
1053                 goto free_kvlist;
1054
1055         /*
1056          * We check whether we want to open a TX stream to a real NIC or a
1057          * pcap file
1058          */
1059         if ((dumpers.num_of_tx = rte_kvargs_count(kvlist,
1060                         ETH_PCAP_TX_PCAP_ARG))) {
1061                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1062                                 &open_tx_pcap, &dumpers);
1063                 using_dumpers = 1;
1064         } else {
1065                 dumpers.num_of_tx = rte_kvargs_count(kvlist,
1066                                 ETH_PCAP_TX_IFACE_ARG);
1067                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1068                                 &open_tx_iface, &dumpers);
1069         }
1070
1071         if (ret < 0)
1072                 goto free_kvlist;
1073
1074         if (using_dumpers)
1075                 ret = rte_eth_from_pcaps_n_dumpers(name, &pcaps, pcaps.num_of_rx,
1076                                 &dumpers, dumpers.num_of_tx, numa_node, kvlist);
1077         else
1078                 ret = rte_eth_from_pcaps(name, &pcaps, pcaps.num_of_rx, &dumpers,
1079                         dumpers.num_of_tx, numa_node, kvlist, 0);
1080
1081 free_kvlist:
1082         rte_kvargs_free(kvlist);
1083         return ret;
1084 }
1085
1086 static int
1087 rte_pmd_pcap_devuninit(const char *name)
1088 {
1089         struct rte_eth_dev *eth_dev = NULL;
1090
1091         RTE_LOG(INFO, PMD, "Closing pcap ethdev on numa socket %u\n",
1092                         rte_socket_id());
1093
1094         if (name == NULL)
1095                 return -1;
1096
1097         /* reserve an ethdev entry */
1098         eth_dev = rte_eth_dev_allocated(name);
1099         if (eth_dev == NULL)
1100                 return -1;
1101
1102         rte_free(eth_dev->data->dev_private);
1103         rte_free(eth_dev->data);
1104         rte_free(eth_dev->pci_dev);
1105
1106         rte_eth_dev_release_port(eth_dev);
1107
1108         return 0;
1109 }
1110
1111 static struct rte_driver pmd_pcap_drv = {
1112         .name = "eth_pcap",
1113         .type = PMD_VDEV,
1114         .init = rte_pmd_pcap_devinit,
1115         .uninit = rte_pmd_pcap_devuninit,
1116 };
1117
1118 PMD_REGISTER_DRIVER(pmd_pcap_drv);