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