4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 * Copyright(c) 2014 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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.
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>
46 #include "rte_eth_pcap.h"
48 #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535
49 #define RTE_ETH_PCAP_SNAPLEN 4096
50 #define RTE_ETH_PCAP_PROMISC 1
51 #define RTE_ETH_PCAP_TIMEOUT -1
52 #define ETH_PCAP_RX_PCAP_ARG "rx_pcap"
53 #define ETH_PCAP_TX_PCAP_ARG "tx_pcap"
54 #define ETH_PCAP_RX_IFACE_ARG "rx_iface"
55 #define ETH_PCAP_TX_IFACE_ARG "tx_iface"
56 #define ETH_PCAP_IFACE_ARG "iface"
58 static char errbuf[PCAP_ERRBUF_SIZE];
59 static struct timeval start_time;
60 static uint64_t start_cycles;
63 struct pcap_rx_queue {
65 struct rte_mempool *mb_pool;
66 volatile unsigned long rx_pkts;
67 volatile unsigned long err_pkts;
70 struct pcap_tx_queue {
71 pcap_dumper_t *dumper;
73 volatile unsigned long tx_pkts;
74 volatile unsigned long err_pkts;
79 pcap_t *pcaps[RTE_PMD_RING_MAX_RX_RINGS];
84 pcap_dumper_t *dumpers[RTE_PMD_RING_MAX_TX_RINGS];
85 pcap_t *pcaps[RTE_PMD_RING_MAX_RX_RINGS];
88 struct pmd_internals {
89 unsigned nb_rx_queues;
90 unsigned nb_tx_queues;
94 struct pcap_rx_queue rx_queue[RTE_PMD_RING_MAX_RX_RINGS];
95 struct pcap_tx_queue tx_queue[RTE_PMD_RING_MAX_TX_RINGS];
98 const char *valid_arguments[] = {
100 ETH_PCAP_TX_PCAP_ARG,
101 ETH_PCAP_RX_IFACE_ARG,
102 ETH_PCAP_TX_IFACE_ARG,
107 static struct ether_addr eth_addr = { .addr_bytes = { 0, 0, 0, 0x1, 0x2, 0x3 } };
108 static const char *drivername = "Pcap PMD";
109 static struct rte_eth_link pmd_link = {
111 .link_duplex = ETH_LINK_FULL_DUPLEX,
117 eth_pcap_rx(void *queue,
118 struct rte_mbuf **bufs,
122 struct pcap_pkthdr header;
123 const u_char *packet;
124 struct rte_mbuf *mbuf;
125 struct pcap_rx_queue *pcap_q = queue;
126 struct rte_pktmbuf_pool_private *mbp_priv;
130 if (unlikely(pcap_q->pcap == NULL || nb_pkts == 0))
133 /* Reads the given number of packets from the pcap file one by one
134 * and copies the packet data into a newly allocated mbuf to return.
136 for (i = 0; i < nb_pkts; i++) {
137 /* Get the next PCAP packet */
138 packet = pcap_next(pcap_q->pcap, &header);
139 if (unlikely(packet == NULL))
142 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
143 if (unlikely(mbuf == NULL))
146 /* Now get the space available for data in the mbuf */
147 mbp_priv = rte_mempool_get_priv(pcap_q->mb_pool);
148 buf_size = (uint16_t) (mbp_priv->mbuf_data_room_size -
149 RTE_PKTMBUF_HEADROOM);
151 if (header.len <= buf_size) {
152 /* pcap packet will fit in the mbuf, go ahead and copy */
153 rte_memcpy(mbuf->pkt.data, packet, header.len);
154 mbuf->pkt.data_len = (uint16_t)header.len;
155 mbuf->pkt.pkt_len = mbuf->pkt.data_len;
159 /* pcap packet will not fit in the mbuf, so drop packet */
161 "PCAP packet %d bytes will not fit in mbuf (%d bytes)\n",
162 header.len, buf_size);
163 rte_pktmbuf_free(mbuf);
166 pcap_q->rx_pkts += num_rx;
171 calculate_timestamp(struct timeval *ts) {
173 struct timeval cur_time;
175 cycles = rte_get_timer_cycles() - start_cycles;
176 cur_time.tv_sec = cycles / hz;
177 cur_time.tv_usec = (cycles % hz) * 10e6 / hz;
178 timeradd(&start_time, &cur_time, ts);
182 * Callback to handle writing packets to a pcap file.
185 eth_pcap_tx_dumper(void *queue,
186 struct rte_mbuf **bufs,
190 struct rte_mbuf *mbuf;
191 struct pcap_tx_queue *dumper_q = queue;
193 struct pcap_pkthdr header;
195 if (dumper_q->dumper == NULL || nb_pkts == 0)
198 /* writes the nb_pkts packets to the previously opened pcap file dumper */
199 for (i = 0; i < nb_pkts; i++) {
201 calculate_timestamp(&header.ts);
202 header.len = mbuf->pkt.data_len;
203 header.caplen = header.len;
204 pcap_dump((u_char*) dumper_q->dumper, &header, mbuf->pkt.data);
205 rte_pktmbuf_free(mbuf);
210 * Since there's no place to hook a callback when the forwarding
211 * process stops and to make sure the pcap file is actually written,
212 * we flush the pcap dumper within each burst.
214 pcap_dump_flush(dumper_q->dumper);
215 dumper_q->tx_pkts += num_tx;
216 dumper_q->err_pkts += nb_pkts - num_tx;
222 * Callback to handle sending packets through a real NIC.
225 eth_pcap_tx(void *queue,
226 struct rte_mbuf **bufs,
231 struct rte_mbuf *mbuf;
232 struct pcap_tx_queue *tx_queue = queue;
235 if (unlikely(nb_pkts == 0 || tx_queue->pcap == NULL))
238 for (i = 0; i < nb_pkts; i++) {
240 ret = pcap_sendpacket(tx_queue->pcap, (u_char*) mbuf->pkt.data,
244 rte_pktmbuf_free(mbuf);
247 tx_queue->tx_pkts += num_tx;
248 tx_queue->err_pkts += nb_pkts - num_tx;
253 eth_pcap_tx(__rte_unused void *queue,
254 __rte_unused struct rte_mbuf **bufs,
255 __rte_unused uint16_t nb_pkts)
257 RTE_LOG(ERR, PMD, "pcap library cannot send packets, please rebuild "
258 "with a more up to date libpcap\n");
264 eth_dev_start(struct rte_eth_dev *dev)
266 dev->data->dev_link.link_status = 1;
271 * This function gets called when the current port gets stopped.
272 * Is the only place for us to close all the tx streams dumpers.
273 * If not called the dumpers will be flushed within each tx burst.
276 eth_dev_stop(struct rte_eth_dev *dev)
279 pcap_dumper_t *dumper;
281 struct pmd_internals *internals = dev->data->dev_private;
283 for (i = 0; i < internals->nb_tx_queues; i++) {
284 dumper = internals->tx_queue[i].dumper;
286 pcap_dump_close(dumper);
287 pcap = internals->tx_queue[i].pcap;
292 dev->data->dev_link.link_status = 0;
296 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
302 eth_dev_info(struct rte_eth_dev *dev,
303 struct rte_eth_dev_info *dev_info)
305 struct pmd_internals *internals = dev->data->dev_private;
306 dev_info->driver_name = drivername;
307 dev_info->if_index = internals->if_index;
308 dev_info->max_mac_addrs = 1;
309 dev_info->max_rx_pktlen = (uint32_t) -1;
310 dev_info->max_rx_queues = (uint16_t)internals->nb_rx_queues;
311 dev_info->max_tx_queues = (uint16_t)internals->nb_tx_queues;
312 dev_info->min_rx_bufsize = 0;
313 dev_info->pci_dev = NULL;
317 eth_stats_get(struct rte_eth_dev *dev,
318 struct rte_eth_stats *igb_stats)
321 unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
322 const struct pmd_internals *internal = dev->data->dev_private;
324 memset(igb_stats, 0, sizeof(*igb_stats));
325 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && i < internal->nb_rx_queues;
327 igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts;
328 rx_total += igb_stats->q_ipackets[i];
331 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && i < internal->nb_tx_queues;
333 igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts;
334 igb_stats->q_errors[i] = internal->tx_queue[i].err_pkts;
335 tx_total += igb_stats->q_opackets[i];
336 tx_err_total += igb_stats->q_errors[i];
339 igb_stats->ipackets = rx_total;
340 igb_stats->opackets = tx_total;
341 igb_stats->oerrors = tx_err_total;
345 eth_stats_reset(struct rte_eth_dev *dev)
348 struct pmd_internals *internal = dev->data->dev_private;
349 for (i = 0; i < internal->nb_rx_queues; i++)
350 internal->rx_queue[i].rx_pkts = 0;
351 for (i = 0; i < internal->nb_tx_queues; i++) {
352 internal->tx_queue[i].tx_pkts = 0;
353 internal->tx_queue[i].err_pkts = 0;
358 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
363 eth_queue_release(void *q __rte_unused)
368 eth_link_update(struct rte_eth_dev *dev __rte_unused,
369 int wait_to_complete __rte_unused)
375 eth_rx_queue_setup(struct rte_eth_dev *dev,
376 uint16_t rx_queue_id,
377 uint16_t nb_rx_desc __rte_unused,
378 unsigned int socket_id __rte_unused,
379 const struct rte_eth_rxconf *rx_conf __rte_unused,
380 struct rte_mempool *mb_pool)
382 struct pmd_internals *internals = dev->data->dev_private;
383 struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
384 pcap_q->mb_pool = mb_pool;
385 dev->data->rx_queues[rx_queue_id] = pcap_q;
390 eth_tx_queue_setup(struct rte_eth_dev *dev,
391 uint16_t tx_queue_id,
392 uint16_t nb_tx_desc __rte_unused,
393 unsigned int socket_id __rte_unused,
394 const struct rte_eth_txconf *tx_conf __rte_unused)
397 struct pmd_internals *internals = dev->data->dev_private;
398 dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
402 static struct eth_dev_ops ops = {
403 .dev_start = eth_dev_start,
404 .dev_stop = eth_dev_stop,
405 .dev_close = eth_dev_close,
406 .dev_configure = eth_dev_configure,
407 .dev_infos_get = eth_dev_info,
408 .rx_queue_setup = eth_rx_queue_setup,
409 .tx_queue_setup = eth_tx_queue_setup,
410 .rx_queue_release = eth_queue_release,
411 .tx_queue_release = eth_queue_release,
412 .link_update = eth_link_update,
413 .stats_get = eth_stats_get,
414 .stats_reset = eth_stats_reset,
418 * Function handler that opens the pcap file for reading a stores a
419 * reference of it for use it later on.
422 open_rx_pcap(const char *key __rte_unused, const char *value, void *extra_args)
425 const char *pcap_filename = value;
426 struct rx_pcaps *pcaps = extra_args;
429 for (i = 0; i < pcaps->num_of_rx; i++) {
430 if ((rx_pcap = pcap_open_offline(pcap_filename, errbuf)) == NULL) {
431 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename, errbuf);
434 pcaps->pcaps[i] = rx_pcap;
441 * Opens a pcap file for writing and stores a reference to it
442 * for use it later on.
445 open_tx_pcap(const char *key __rte_unused, const char *value, void *extra_args)
448 const char *pcap_filename = value;
449 struct tx_pcaps *dumpers = extra_args;
451 pcap_dumper_t *dumper;
453 for (i = 0; i < dumpers->num_of_tx; i++) {
455 * We need to create a dummy empty pcap_t to use it
456 * with pcap_dump_open(). We create big enough an Ethernet
459 if ((tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN))
461 RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
465 /* The dumper is created using the previous pcap_t reference */
466 if ((dumper = pcap_dump_open(tx_pcap, pcap_filename)) == NULL) {
467 RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n", pcap_filename);
470 dumpers->dumpers[i] = dumper;
477 * pcap_open_live wrapper function
480 open_iface_live(const char *iface, pcap_t **pcap) {
481 *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
482 RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
485 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
492 * Opens an interface for reading and writing
495 open_rx_tx_iface(const char *key __rte_unused, const char *value, void *extra_args)
497 const char *iface = value;
498 pcap_t **pcap = extra_args;
500 if(open_iface_live(iface, pcap) < 0)
506 * Opens a NIC for reading packets from it
509 open_rx_iface(const char *key __rte_unused, const char *value, void *extra_args)
512 const char *iface = value;
513 struct rx_pcaps *pcaps = extra_args;
516 for (i = 0; i < pcaps->num_of_rx; i++) {
517 if(open_iface_live(iface, &pcap) < 0)
519 pcaps->pcaps[i] = pcap;
526 * Opens a NIC for writing packets to it
529 open_tx_iface(const char *key __rte_unused, const char *value, void *extra_args)
532 const char *iface = value;
533 struct tx_pcaps *pcaps = extra_args;
536 for (i = 0; i < pcaps->num_of_tx; i++) {
537 if(open_iface_live(iface, &pcap) < 0)
539 pcaps->pcaps[i] = pcap;
547 rte_pmd_init_internals(const unsigned nb_rx_queues,
548 const unsigned nb_tx_queues,
549 const unsigned numa_node,
550 struct pmd_internals **internals,
551 struct rte_eth_dev **eth_dev,
552 struct rte_kvargs *kvlist)
554 struct rte_eth_dev_data *data = NULL;
555 struct rte_pci_device *pci_dev = NULL;
557 struct rte_kvargs_pair *pair = NULL;
559 for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
560 pair = &kvlist->pairs[k_idx];
561 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
566 "Creating pcap-backed ethdev on numa socket %u\n", numa_node);
568 /* now do all data allocation - for eth_dev structure, dummy pci driver
569 * and internal (private) data
571 data = rte_zmalloc_socket(NULL, sizeof(*data), 0, numa_node);
575 pci_dev = rte_zmalloc_socket(NULL, sizeof(*pci_dev), 0, numa_node);
579 *internals = rte_zmalloc_socket(NULL, sizeof(**internals), 0, numa_node);
580 if (*internals == NULL)
583 /* reserve an ethdev entry */
584 *eth_dev = rte_eth_dev_allocate();
585 if (*eth_dev == NULL)
588 /* now put it all together
589 * - store queue data in internals,
590 * - store numa_node info in pci_driver
591 * - point eth_dev_data to internals and pci_driver
592 * - and point eth_dev structure to new eth_dev_data structure
594 /* NOTE: we'll replace the data element, of originally allocated eth_dev
595 * so the rings are local per-process */
597 (*internals)->nb_rx_queues = nb_rx_queues;
598 (*internals)->nb_tx_queues = nb_tx_queues;
601 (*internals)->if_index = 0;
603 (*internals)->if_index = if_nametoindex(pair->value);
605 pci_dev->numa_node = numa_node;
607 data->dev_private = *internals;
608 data->port_id = (*eth_dev)->data->port_id;
609 data->nb_rx_queues = (uint16_t)nb_rx_queues;
610 data->nb_tx_queues = (uint16_t)nb_tx_queues;
611 data->dev_link = pmd_link;
612 data->mac_addrs = ð_addr;
614 (*eth_dev)->data = data;
615 (*eth_dev)->dev_ops = &ops;
616 (*eth_dev)->pci_dev = pci_dev;
625 rte_free(*internals);
630 rte_eth_from_pcaps_n_dumpers(pcap_t * const rx_queues[],
631 const unsigned nb_rx_queues,
632 pcap_dumper_t * const tx_queues[],
633 const unsigned nb_tx_queues,
634 const unsigned numa_node,
635 struct rte_kvargs *kvlist)
637 struct pmd_internals *internals = NULL;
638 struct rte_eth_dev *eth_dev = NULL;
641 /* do some parameter checking */
642 if (rx_queues == NULL && nb_rx_queues > 0)
644 if (tx_queues == NULL && nb_tx_queues > 0)
647 if (rte_pmd_init_internals(nb_rx_queues, nb_tx_queues, numa_node,
648 &internals, ð_dev, kvlist) < 0)
651 for (i = 0; i < nb_rx_queues; i++) {
652 internals->rx_queue->pcap = rx_queues[i];
654 for (i = 0; i < nb_tx_queues; i++) {
655 internals->tx_queue->dumper = tx_queues[i];
658 eth_dev->rx_pkt_burst = eth_pcap_rx;
659 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
665 rte_eth_from_pcaps(pcap_t * const rx_queues[],
666 const unsigned nb_rx_queues,
667 pcap_t * const tx_queues[],
668 const unsigned nb_tx_queues,
669 const unsigned numa_node,
670 struct rte_kvargs *kvlist)
672 struct pmd_internals *internals = NULL;
673 struct rte_eth_dev *eth_dev = NULL;
676 /* do some parameter checking */
677 if (rx_queues == NULL && nb_rx_queues > 0)
679 if (tx_queues == NULL && nb_tx_queues > 0)
682 if (rte_pmd_init_internals(nb_rx_queues, nb_tx_queues, numa_node,
683 &internals, ð_dev, kvlist) < 0)
686 for (i = 0; i < nb_rx_queues; i++) {
687 internals->rx_queue->pcap = rx_queues[i];
689 for (i = 0; i < nb_tx_queues; i++) {
690 internals->tx_queue->pcap = tx_queues[i];
693 eth_dev->rx_pkt_burst = eth_pcap_rx;
694 eth_dev->tx_pkt_burst = eth_pcap_tx;
701 rte_pmd_pcap_init(const char *name, const char *params)
703 unsigned numa_node, using_dumpers = 0;
705 struct rte_kvargs *kvlist;
706 struct rx_pcaps pcaps;
707 struct tx_pcaps dumpers;
709 RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
711 numa_node = rte_socket_id();
713 gettimeofday(&start_time, NULL);
714 start_cycles = rte_get_timer_cycles();
715 hz = rte_get_timer_hz();
717 kvlist = rte_kvargs_parse(params, valid_arguments);
722 * If iface argument is passed we open the NICs and use them for
725 if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
727 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
728 &open_rx_tx_iface, &pcaps.pcaps[0]);
732 return rte_eth_from_pcaps(pcaps.pcaps, 1, pcaps.pcaps, 1,
737 * We check whether we want to open a RX stream from a real NIC or a
740 if ((pcaps.num_of_rx = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG))) {
741 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
742 &open_rx_pcap, &pcaps);
744 pcaps.num_of_rx = rte_kvargs_count(kvlist,
745 ETH_PCAP_RX_IFACE_ARG);
746 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
747 &open_rx_iface, &pcaps);
754 * We check whether we want to open a TX stream to a real NIC or a
757 if ((dumpers.num_of_tx = rte_kvargs_count(kvlist,
758 ETH_PCAP_TX_PCAP_ARG))) {
759 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
760 &open_tx_pcap, &dumpers);
763 dumpers.num_of_tx = rte_kvargs_count(kvlist,
764 ETH_PCAP_TX_IFACE_ARG);
765 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
766 &open_tx_iface, &dumpers);
773 return rte_eth_from_pcaps_n_dumpers(pcaps.pcaps, pcaps.num_of_rx,
774 dumpers.dumpers, dumpers.num_of_tx, numa_node, kvlist);
776 return rte_eth_from_pcaps(pcaps.pcaps, pcaps.num_of_rx, dumpers.pcaps,
777 dumpers.num_of_tx, numa_node, kvlist);