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