net: remove dead driver names
[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 int 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
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 int 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
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 = 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, can copy it */
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                                 rte_pktmbuf_free(mbuf);
226                                 break;
227                         }
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
239         return num_rx;
240 }
241
242 static inline void
243 calculate_timestamp(struct timeval *ts) {
244         uint64_t cycles;
245         struct timeval cur_time;
246
247         cycles = rte_get_timer_cycles() - start_cycles;
248         cur_time.tv_sec = cycles / hz;
249         cur_time.tv_usec = (cycles % hz) * 10e6 / hz;
250         timeradd(&start_time, &cur_time, ts);
251 }
252
253 /*
254  * Callback to handle writing packets to a pcap file.
255  */
256 static uint16_t
257 eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
258 {
259         unsigned int i;
260         struct rte_mbuf *mbuf;
261         struct pcap_tx_queue *dumper_q = queue;
262         uint16_t num_tx = 0;
263         uint32_t tx_bytes = 0;
264         struct pcap_pkthdr header;
265
266         if (dumper_q->dumper == NULL || nb_pkts == 0)
267                 return 0;
268
269         /* writes the nb_pkts packets to the previously opened pcap file
270          * 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. Size (%d) > max jumbo size (%d).\n",
288                                         mbuf->pkt_len,
289                                         ETHER_MAX_JUMBO_FRAME_LEN);
290
291                                 rte_pktmbuf_free(mbuf);
292                                 break;
293                         }
294                 }
295
296                 rte_pktmbuf_free(mbuf);
297                 num_tx++;
298                 tx_bytes += mbuf->pkt_len;
299         }
300
301         /*
302          * Since there's no place to hook a callback when the forwarding
303          * process stops and to make sure the pcap file is actually written,
304          * we flush the pcap dumper within each burst.
305          */
306         pcap_dump_flush(dumper_q->dumper);
307         dumper_q->tx_stat.pkts += num_tx;
308         dumper_q->tx_stat.bytes += tx_bytes;
309         dumper_q->tx_stat.err_pkts += nb_pkts - num_tx;
310
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, struct rte_mbuf **bufs, uint16_t nb_pkts)
319 {
320         unsigned int i;
321         int ret;
322         struct rte_mbuf *mbuf;
323         struct pcap_tx_queue *tx_queue = queue;
324         uint16_t num_tx = 0;
325         uint32_t tx_bytes = 0;
326
327         if (unlikely(nb_pkts == 0 || tx_queue->pcap == NULL))
328                 return 0;
329
330         for (i = 0; i < nb_pkts; i++) {
331                 mbuf = bufs[i];
332
333                 if (likely(mbuf->nb_segs == 1)) {
334                         ret = pcap_sendpacket(tx_queue->pcap,
335                                         rte_pktmbuf_mtod(mbuf, u_char *),
336                                         mbuf->pkt_len);
337                 } else {
338                         if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
339                                 eth_pcap_gather_data(tx_pcap_data, mbuf);
340                                 ret = pcap_sendpacket(tx_queue->pcap,
341                                                 tx_pcap_data, mbuf->pkt_len);
342                         } else {
343                                 RTE_LOG(ERR, PMD,
344                                         "Dropping PCAP packet. Size (%d) > max jumbo size (%d).\n",
345                                         mbuf->pkt_len,
346                                         ETHER_MAX_JUMBO_FRAME_LEN);
347
348                                 rte_pktmbuf_free(mbuf);
349                                 break;
350                         }
351                 }
352
353                 if (unlikely(ret != 0))
354                         break;
355                 num_tx++;
356                 tx_bytes += mbuf->pkt_len;
357                 rte_pktmbuf_free(mbuf);
358         }
359
360         tx_queue->tx_stat.pkts += num_tx;
361         tx_queue->tx_stat.bytes += tx_bytes;
362         tx_queue->tx_stat.err_pkts += nb_pkts - num_tx;
363
364         return num_tx;
365 }
366
367 /*
368  * pcap_open_live wrapper function
369  */
370 static inline int
371 open_iface_live(const char *iface, pcap_t **pcap) {
372         *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
373                         RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
374
375         if (*pcap == NULL) {
376                 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
377                 return -1;
378         }
379
380         return 0;
381 }
382
383 static int
384 open_single_iface(const char *iface, pcap_t **pcap)
385 {
386         if (open_iface_live(iface, pcap) < 0) {
387                 RTE_LOG(ERR, PMD, "Couldn't open interface %s\n", iface);
388                 return -1;
389         }
390
391         return 0;
392 }
393
394 static int
395 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
396 {
397         pcap_t *tx_pcap;
398
399         /*
400          * We need to create a dummy empty pcap_t to use it
401          * with pcap_dump_open(). We create big enough an Ethernet
402          * pcap holder.
403          */
404         tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN);
405         if (tx_pcap == NULL) {
406                 RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
407                 return -1;
408         }
409
410         /* The dumper is created using the previous pcap_t reference */
411         *dumper = pcap_dump_open(tx_pcap, pcap_filename);
412         if (*dumper == NULL) {
413                 RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n",
414                         pcap_filename);
415                 return -1;
416         }
417
418         return 0;
419 }
420
421 static int
422 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
423 {
424         *pcap = pcap_open_offline(pcap_filename, errbuf);
425         if (*pcap == NULL) {
426                 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename,
427                         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 int 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 &&
460                                 strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
461                         if (open_single_tx_pcap(tx->name, &tx->dumper) < 0)
462                                 return -1;
463                 } else if (!tx->pcap &&
464                                 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                 } else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
481                         if (open_single_iface(rx->name, &rx->pcap) < 0)
482                                 return -1;
483                 }
484         }
485
486 status_up:
487         dev->data->dev_link.link_status = ETH_LINK_UP;
488
489         return 0;
490 }
491
492 /*
493  * This function gets called when the current port gets stopped.
494  * Is the only place for us to close all the tx streams dumpers.
495  * If not called the dumpers will be flushed within each tx burst.
496  */
497 static void
498 eth_dev_stop(struct rte_eth_dev *dev)
499 {
500         unsigned int i;
501         struct pmd_internals *internals = dev->data->dev_private;
502         struct pcap_tx_queue *tx;
503         struct pcap_rx_queue *rx;
504
505         /* Special iface case. Single pcap is open and shared between tx/rx. */
506         if (internals->single_iface) {
507                 tx = &internals->tx_queue[0];
508                 rx = &internals->rx_queue[0];
509                 pcap_close(tx->pcap);
510                 tx->pcap = NULL;
511                 rx->pcap = NULL;
512                 goto status_down;
513         }
514
515         for (i = 0; i < dev->data->nb_tx_queues; i++) {
516                 tx = &internals->tx_queue[i];
517
518                 if (tx->dumper != NULL) {
519                         pcap_dump_close(tx->dumper);
520                         tx->dumper = NULL;
521                 }
522
523                 if (tx->pcap != NULL) {
524                         pcap_close(tx->pcap);
525                         tx->pcap = NULL;
526                 }
527         }
528
529         for (i = 0; i < dev->data->nb_rx_queues; i++) {
530                 rx = &internals->rx_queue[i];
531
532                 if (rx->pcap != NULL) {
533                         pcap_close(rx->pcap);
534                         rx->pcap = NULL;
535                 }
536         }
537
538 status_down:
539         dev->data->dev_link.link_status = ETH_LINK_DOWN;
540 }
541
542 static int
543 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
544 {
545         return 0;
546 }
547
548 static void
549 eth_dev_info(struct rte_eth_dev *dev,
550                 struct rte_eth_dev_info *dev_info)
551 {
552         struct pmd_internals *internals = dev->data->dev_private;
553
554         dev_info->if_index = internals->if_index;
555         dev_info->max_mac_addrs = 1;
556         dev_info->max_rx_pktlen = (uint32_t) -1;
557         dev_info->max_rx_queues = dev->data->nb_rx_queues;
558         dev_info->max_tx_queues = dev->data->nb_tx_queues;
559         dev_info->min_rx_bufsize = 0;
560 }
561
562 static void
563 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
564 {
565         unsigned int i;
566         unsigned long rx_packets_total = 0, rx_bytes_total = 0;
567         unsigned long tx_packets_total = 0, tx_bytes_total = 0;
568         unsigned long tx_packets_err_total = 0;
569         const struct pmd_internals *internal = dev->data->dev_private;
570
571         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
572                         i < dev->data->nb_rx_queues; i++) {
573                 stats->q_ipackets[i] = internal->rx_queue[i].rx_stat.pkts;
574                 stats->q_ibytes[i] = internal->rx_queue[i].rx_stat.bytes;
575                 rx_packets_total += stats->q_ipackets[i];
576                 rx_bytes_total += stats->q_ibytes[i];
577         }
578
579         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
580                         i < dev->data->nb_tx_queues; i++) {
581                 stats->q_opackets[i] = internal->tx_queue[i].tx_stat.pkts;
582                 stats->q_obytes[i] = internal->tx_queue[i].tx_stat.bytes;
583                 stats->q_errors[i] = internal->tx_queue[i].tx_stat.err_pkts;
584                 tx_packets_total += stats->q_opackets[i];
585                 tx_bytes_total += stats->q_obytes[i];
586                 tx_packets_err_total += stats->q_errors[i];
587         }
588
589         stats->ipackets = rx_packets_total;
590         stats->ibytes = rx_bytes_total;
591         stats->opackets = tx_packets_total;
592         stats->obytes = tx_bytes_total;
593         stats->oerrors = tx_packets_err_total;
594 }
595
596 static void
597 eth_stats_reset(struct rte_eth_dev *dev)
598 {
599         unsigned int i;
600         struct pmd_internals *internal = dev->data->dev_private;
601
602         for (i = 0; i < dev->data->nb_rx_queues; i++) {
603                 internal->rx_queue[i].rx_stat.pkts = 0;
604                 internal->rx_queue[i].rx_stat.bytes = 0;
605         }
606
607         for (i = 0; i < dev->data->nb_tx_queues; i++) {
608                 internal->tx_queue[i].tx_stat.pkts = 0;
609                 internal->tx_queue[i].tx_stat.bytes = 0;
610                 internal->tx_queue[i].tx_stat.err_pkts = 0;
611         }
612 }
613
614 static void
615 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
616 {
617 }
618
619 static void
620 eth_queue_release(void *q __rte_unused)
621 {
622 }
623
624 static int
625 eth_link_update(struct rte_eth_dev *dev __rte_unused,
626                 int wait_to_complete __rte_unused)
627 {
628         return 0;
629 }
630
631 static int
632 eth_rx_queue_setup(struct rte_eth_dev *dev,
633                 uint16_t rx_queue_id,
634                 uint16_t nb_rx_desc __rte_unused,
635                 unsigned int socket_id __rte_unused,
636                 const struct rte_eth_rxconf *rx_conf __rte_unused,
637                 struct rte_mempool *mb_pool)
638 {
639         struct pmd_internals *internals = dev->data->dev_private;
640         struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
641
642         pcap_q->mb_pool = mb_pool;
643         dev->data->rx_queues[rx_queue_id] = pcap_q;
644         pcap_q->in_port = dev->data->port_id;
645
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         struct pmd_internals *internals = dev->data->dev_private;
657
658         dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
659
660         return 0;
661 }
662
663 static const struct eth_dev_ops ops = {
664         .dev_start = eth_dev_start,
665         .dev_stop = eth_dev_stop,
666         .dev_close = eth_dev_close,
667         .dev_configure = eth_dev_configure,
668         .dev_infos_get = eth_dev_info,
669         .rx_queue_setup = eth_rx_queue_setup,
670         .tx_queue_setup = eth_tx_queue_setup,
671         .rx_queue_release = eth_queue_release,
672         .tx_queue_release = eth_queue_release,
673         .link_update = eth_link_update,
674         .stats_get = eth_stats_get,
675         .stats_reset = eth_stats_reset,
676 };
677
678 /*
679  * Function handler that opens the pcap file for reading a stores a
680  * reference of it for use it later on.
681  */
682 static int
683 open_rx_pcap(const char *key, const char *value, void *extra_args)
684 {
685         unsigned int i;
686         const char *pcap_filename = value;
687         struct pmd_devargs *rx = extra_args;
688         pcap_t *pcap = NULL;
689
690         for (i = 0; i < rx->num_of_queue; i++) {
691                 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
692                         return -1;
693
694                 rx->queue[i].pcap = pcap;
695                 rx->queue[i].name = pcap_filename;
696                 rx->queue[i].type = key;
697         }
698
699         return 0;
700 }
701
702 /*
703  * Opens a pcap file for writing and stores a reference to it
704  * for use it later on.
705  */
706 static int
707 open_tx_pcap(const char *key, const char *value, void *extra_args)
708 {
709         unsigned int i;
710         const char *pcap_filename = value;
711         struct pmd_devargs *dumpers = extra_args;
712         pcap_dumper_t *dumper;
713
714         for (i = 0; i < dumpers->num_of_queue; i++) {
715                 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
716                         return -1;
717
718                 dumpers->queue[i].dumper = dumper;
719                 dumpers->queue[i].name = pcap_filename;
720                 dumpers->queue[i].type = key;
721         }
722
723         return 0;
724 }
725
726 /*
727  * Opens an interface for reading and writing
728  */
729 static inline int
730 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
731 {
732         const char *iface = value;
733         struct pmd_devargs *tx = extra_args;
734         pcap_t *pcap = NULL;
735
736         if (open_single_iface(iface, &pcap) < 0)
737                 return -1;
738
739         tx->queue[0].pcap = pcap;
740         tx->queue[0].name = iface;
741         tx->queue[0].type = key;
742
743         return 0;
744 }
745
746 /*
747  * Opens a NIC for reading packets from it
748  */
749 static inline int
750 open_rx_iface(const char *key, const char *value, void *extra_args)
751 {
752         unsigned int i;
753         const char *iface = value;
754         struct pmd_devargs *rx = extra_args;
755         pcap_t *pcap = NULL;
756
757         for (i = 0; i < rx->num_of_queue; i++) {
758                 if (open_single_iface(iface, &pcap) < 0)
759                         return -1;
760                 rx->queue[i].pcap = pcap;
761                 rx->queue[i].name = iface;
762                 rx->queue[i].type = key;
763         }
764
765         return 0;
766 }
767
768 /*
769  * Opens a NIC for writing packets to it
770  */
771 static int
772 open_tx_iface(const char *key, const char *value, void *extra_args)
773 {
774         unsigned int i;
775         const char *iface = value;
776         struct pmd_devargs *tx = extra_args;
777         pcap_t *pcap;
778
779         for (i = 0; i < tx->num_of_queue; i++) {
780                 if (open_single_iface(iface, &pcap) < 0)
781                         return -1;
782                 tx->queue[i].pcap = pcap;
783                 tx->queue[i].name = iface;
784                 tx->queue[i].type = key;
785         }
786
787         return 0;
788 }
789
790 static int
791 pmd_init_internals(const char *name, const unsigned int nb_rx_queues,
792                 const unsigned int nb_tx_queues,
793                 struct pmd_internals **internals,
794                 struct rte_eth_dev **eth_dev)
795 {
796         struct rte_eth_dev_data *data = NULL;
797         unsigned int numa_node = rte_socket_id();
798
799         RTE_LOG(INFO, PMD, "Creating pcap-backed ethdev on numa socket %u\n",
800                 numa_node);
801
802         /* now do all data allocation - for eth_dev structure
803          * and internal (private) data
804          */
805         data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
806         if (data == NULL)
807                 goto error;
808
809         *internals = rte_zmalloc_socket(name, sizeof(**internals), 0,
810                         numa_node);
811         if (*internals == NULL)
812                 goto error;
813
814         /* reserve an ethdev entry */
815         *eth_dev = rte_eth_dev_allocate(name);
816         if (*eth_dev == NULL)
817                 goto error;
818
819         /* now put it all together
820          * - store queue data in internals,
821          * - store numa_node info in eth_dev
822          * - point eth_dev_data to internals
823          * - and point eth_dev structure to new eth_dev_data structure
824          */
825         data->dev_private = *internals;
826         data->port_id = (*eth_dev)->data->port_id;
827         snprintf(data->name, sizeof(data->name), "%s", (*eth_dev)->data->name);
828         data->nb_rx_queues = (uint16_t)nb_rx_queues;
829         data->nb_tx_queues = (uint16_t)nb_tx_queues;
830         data->dev_link = pmd_link;
831         data->mac_addrs = &eth_addr;
832
833         /*
834          * NOTE: we'll replace the data element, of originally allocated
835          * eth_dev so the rings are local per-process
836          */
837         (*eth_dev)->data = data;
838         (*eth_dev)->dev_ops = &ops;
839         (*eth_dev)->driver = NULL;
840         data->dev_flags = RTE_ETH_DEV_DETACHABLE;
841         data->kdrv = RTE_KDRV_NONE;
842         data->drv_name = "Pcap PMD";
843         data->numa_node = numa_node;
844
845         return 0;
846
847 error:
848         rte_free(data);
849         rte_free(*internals);
850
851         return -1;
852 }
853
854 static int
855 eth_from_pcaps_common(const char *name, struct pmd_devargs *rx_queues,
856                 const unsigned int nb_rx_queues, struct pmd_devargs *tx_queues,
857                 const unsigned int nb_tx_queues, struct rte_kvargs *kvlist,
858                 struct pmd_internals **internals, struct rte_eth_dev **eth_dev)
859 {
860         struct rte_kvargs_pair *pair = NULL;
861         unsigned int k_idx;
862         unsigned int i;
863
864         /* do some parameter checking */
865         if (rx_queues == NULL && nb_rx_queues > 0)
866                 return -1;
867         if (tx_queues == NULL && nb_tx_queues > 0)
868                 return -1;
869
870         if (pmd_init_internals(name, nb_rx_queues, nb_tx_queues, internals,
871                         eth_dev) < 0)
872                 return -1;
873
874         for (i = 0; i < nb_rx_queues; i++) {
875                 struct pcap_rx_queue *rx = &(*internals)->rx_queue[i];
876                 struct devargs_queue *queue = &rx_queues->queue[i];
877
878                 rx->pcap = queue->pcap;
879                 snprintf(rx->name, sizeof(rx->name), "%s", queue->name);
880                 snprintf(rx->type, sizeof(rx->type), "%s", queue->type);
881         }
882
883         for (i = 0; i < nb_tx_queues; i++) {
884                 struct pcap_tx_queue *tx = &(*internals)->tx_queue[i];
885                 struct devargs_queue *queue = &tx_queues->queue[i];
886
887                 tx->dumper = queue->dumper;
888                 tx->pcap = queue->pcap;
889                 snprintf(tx->name, sizeof(tx->name), "%s", queue->name);
890                 snprintf(tx->type, sizeof(tx->type), "%s", queue->type);
891         }
892
893         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
894                 pair = &kvlist->pairs[k_idx];
895                 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
896                         break;
897         }
898
899         if (pair == NULL)
900                 (*internals)->if_index = 0;
901         else
902                 (*internals)->if_index = if_nametoindex(pair->value);
903
904         return 0;
905 }
906
907 static int
908 eth_from_pcaps(const char *name, struct pmd_devargs *rx_queues,
909                 const unsigned int nb_rx_queues, struct pmd_devargs *tx_queues,
910                 const unsigned int nb_tx_queues, struct rte_kvargs *kvlist,
911                 int single_iface, unsigned int using_dumpers)
912 {
913         struct pmd_internals *internals = NULL;
914         struct rte_eth_dev *eth_dev = NULL;
915         int ret;
916
917         ret = eth_from_pcaps_common(name, rx_queues, nb_rx_queues,
918                 tx_queues, nb_tx_queues, kvlist, &internals, &eth_dev);
919
920         if (ret < 0)
921                 return ret;
922
923         /* store weather we are using a single interface for rx/tx or not */
924         internals->single_iface = single_iface;
925
926         eth_dev->rx_pkt_burst = eth_pcap_rx;
927
928         if (using_dumpers)
929                 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
930         else
931                 eth_dev->tx_pkt_burst = eth_pcap_tx;
932
933         return 0;
934 }
935
936 static int
937 pmd_pcap_probe(const char *name, const char *params)
938 {
939         unsigned int is_rx_pcap = 0, is_tx_pcap = 0;
940         struct rte_kvargs *kvlist;
941         struct pmd_devargs pcaps = {0};
942         struct pmd_devargs dumpers = {0};
943         int single_iface = 0;
944         int ret;
945
946         RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
947
948         gettimeofday(&start_time, NULL);
949         start_cycles = rte_get_timer_cycles();
950         hz = rte_get_timer_hz();
951
952         kvlist = rte_kvargs_parse(params, valid_arguments);
953         if (kvlist == NULL)
954                 return -1;
955
956         /*
957          * If iface argument is passed we open the NICs and use them for
958          * reading / writing
959          */
960         if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
961
962                 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
963                                 &open_rx_tx_iface, &pcaps);
964
965                 if (ret < 0)
966                         goto free_kvlist;
967
968                 dumpers.queue[0] = pcaps.queue[0];
969
970                 single_iface = 1;
971                 pcaps.num_of_queue = 1;
972                 dumpers.num_of_queue = 1;
973
974                 goto create_eth;
975         }
976
977         /*
978          * We check whether we want to open a RX stream from a real NIC or a
979          * pcap file
980          */
981         pcaps.num_of_queue = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG);
982         if (pcaps.num_of_queue)
983                 is_rx_pcap = 1;
984         else
985                 pcaps.num_of_queue = rte_kvargs_count(kvlist,
986                                 ETH_PCAP_RX_IFACE_ARG);
987
988         if (pcaps.num_of_queue > RTE_PMD_PCAP_MAX_QUEUES)
989                 pcaps.num_of_queue = RTE_PMD_PCAP_MAX_QUEUES;
990
991         if (is_rx_pcap)
992                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
993                                 &open_rx_pcap, &pcaps);
994         else
995                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
996                                 &open_rx_iface, &pcaps);
997
998         if (ret < 0)
999                 goto free_kvlist;
1000
1001         /*
1002          * We check whether we want to open a TX stream to a real NIC or a
1003          * pcap file
1004          */
1005         dumpers.num_of_queue = rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG);
1006         if (dumpers.num_of_queue)
1007                 is_tx_pcap = 1;
1008         else
1009                 dumpers.num_of_queue = rte_kvargs_count(kvlist,
1010                                 ETH_PCAP_TX_IFACE_ARG);
1011
1012         if (dumpers.num_of_queue > RTE_PMD_PCAP_MAX_QUEUES)
1013                 dumpers.num_of_queue = RTE_PMD_PCAP_MAX_QUEUES;
1014
1015         if (is_tx_pcap)
1016                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1017                                 &open_tx_pcap, &dumpers);
1018         else
1019                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1020                                 &open_tx_iface, &dumpers);
1021
1022         if (ret < 0)
1023                 goto free_kvlist;
1024
1025 create_eth:
1026         ret = eth_from_pcaps(name, &pcaps, pcaps.num_of_queue, &dumpers,
1027                 dumpers.num_of_queue, kvlist, single_iface, is_tx_pcap);
1028
1029 free_kvlist:
1030         rte_kvargs_free(kvlist);
1031
1032         return ret;
1033 }
1034
1035 static int
1036 pmd_pcap_remove(const char *name)
1037 {
1038         struct rte_eth_dev *eth_dev = NULL;
1039
1040         RTE_LOG(INFO, PMD, "Closing pcap ethdev on numa socket %u\n",
1041                         rte_socket_id());
1042
1043         if (name == NULL)
1044                 return -1;
1045
1046         /* reserve an ethdev entry */
1047         eth_dev = rte_eth_dev_allocated(name);
1048         if (eth_dev == NULL)
1049                 return -1;
1050
1051         rte_free(eth_dev->data->dev_private);
1052         rte_free(eth_dev->data);
1053
1054         rte_eth_dev_release_port(eth_dev);
1055
1056         return 0;
1057 }
1058
1059 static struct rte_vdev_driver pmd_pcap_drv = {
1060         .probe = pmd_pcap_probe,
1061         .remove = pmd_pcap_remove,
1062 };
1063
1064 RTE_PMD_REGISTER_VDEV(net_pcap, pmd_pcap_drv);
1065 RTE_PMD_REGISTER_ALIAS(net_pcap, eth_pcap);
1066 RTE_PMD_REGISTER_PARAM_STRING(net_pcap,
1067         ETH_PCAP_RX_PCAP_ARG "=<string> "
1068         ETH_PCAP_TX_PCAP_ARG "=<string> "
1069         ETH_PCAP_RX_IFACE_ARG "=<ifc> "
1070         ETH_PCAP_TX_IFACE_ARG "=<ifc> "
1071         ETH_PCAP_IFACE_ARG "=<ifc>");