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