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