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