4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <rte_ethdev.h>
37 #include <rte_malloc.h>
38 #include <rte_memcpy.h>
39 #include <rte_string_fns.h>
40 #include <rte_cycles.h>
42 #include "rte_eth_pcap.h"
43 #include "rte_eth_pcap_arg_parser.h"
45 #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535
46 #define RTE_ETH_PCAP_SNAPLEN 4096
47 #define RTE_ETH_PCAP_PROMISC 1
48 #define RTE_ETH_PCAP_TIMEOUT -1
49 #define RTE_ETH_PCAP_MBUFS 64
50 #define ETH_PCAP_RX_PCAP_ARG "rx_pcap"
51 #define ETH_PCAP_TX_PCAP_ARG "tx_pcap"
52 #define ETH_PCAP_RX_IFACE_ARG "rx_iface"
53 #define ETH_PCAP_TX_IFACE_ARG "tx_iface"
54 #define ETH_PCAP_IFACE_ARG "iface"
56 static char errbuf[PCAP_ERRBUF_SIZE];
57 static struct timeval start_time;
58 static uint64_t start_cycles;
61 struct pcap_rx_queue {
63 struct rte_mempool *mb_pool;
64 volatile unsigned long rx_pkts;
65 volatile unsigned long err_pkts;
68 struct pcap_tx_queue {
69 pcap_dumper_t *dumper;
71 volatile unsigned long tx_pkts;
72 volatile unsigned long err_pkts;
77 pcap_t *pcaps[RTE_PMD_RING_MAX_RX_RINGS];
82 pcap_dumper_t *dumpers[RTE_PMD_RING_MAX_TX_RINGS];
83 pcap_t *pcaps[RTE_PMD_RING_MAX_RX_RINGS];
86 struct pmd_internals {
87 unsigned nb_rx_queues;
88 unsigned nb_tx_queues;
90 struct pcap_rx_queue rx_queue[RTE_PMD_RING_MAX_RX_RINGS];
91 struct pcap_tx_queue tx_queue[RTE_PMD_RING_MAX_TX_RINGS];
94 const char *valid_arguments[] = {
97 ETH_PCAP_RX_IFACE_ARG,
98 ETH_PCAP_TX_IFACE_ARG,
103 static struct ether_addr eth_addr = { .addr_bytes = { 0, 0, 0, 0x1, 0x2, 0x3 } };
104 static const char *drivername = "Pcap PMD";
105 static struct rte_eth_link pmd_link = {
107 .link_duplex = ETH_LINK_FULL_DUPLEX,
113 eth_pcap_rx(void *queue,
114 struct rte_mbuf **bufs,
118 struct pcap_pkthdr header;
119 const u_char *packet;
120 struct rte_mbuf *mbuf;
121 struct pcap_rx_queue *pcap_q = queue;
122 struct rte_pktmbuf_pool_private *mbp_priv;
126 if (unlikely(pcap_q->pcap == NULL || nb_pkts == 0))
129 /* Reads the given number of packets from the pcap file one by one
130 * and copies the packet data into a newly allocated mbuf to return.
132 for (i = 0; i < nb_pkts; i++) {
133 /* Get the next PCAP packet */
134 packet = pcap_next(pcap_q->pcap, &header);
135 if (unlikely(packet == NULL))
138 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
139 if (unlikely(mbuf == NULL))
142 /* Now get the space available for data in the mbuf */
143 mbp_priv = rte_mempool_get_priv(pcap_q->mb_pool);
144 buf_size = (uint16_t) (mbp_priv->mbuf_data_room_size -
145 RTE_PKTMBUF_HEADROOM);
147 if (header.len <= buf_size) {
148 /* pcap packet will fit in the mbuf, go ahead and copy */
149 rte_memcpy(mbuf->pkt.data, packet, header.len);
150 mbuf->pkt.data_len = (uint16_t)header.len;
151 mbuf->pkt.pkt_len = mbuf->pkt.data_len;
155 /* pcap packet will not fit in the mbuf, so drop packet */
157 "PCAP packet %d bytes will not fit in mbuf (%d bytes)\n",
158 header.len, buf_size);
159 rte_pktmbuf_free(mbuf);
162 pcap_q->rx_pkts += num_rx;
167 calculate_timestamp(struct timeval *ts) {
169 struct timeval cur_time;
171 cycles = rte_get_timer_cycles() - start_cycles;
172 cur_time.tv_sec = cycles / hz;
173 cur_time.tv_usec = (cycles % hz) * 10e6 / hz;
174 timeradd(&start_time, &cur_time, ts);
178 * Callback to handle writing packets to a pcap file.
181 eth_pcap_tx_dumper(void *queue,
182 struct rte_mbuf **bufs,
186 struct rte_mbuf *mbuf;
187 struct pcap_tx_queue *dumper_q = queue;
189 struct pcap_pkthdr header;
191 if (dumper_q->dumper == NULL || nb_pkts == 0)
194 /* writes the nb_pkts packets to the previously opened pcap file dumper */
195 for (i = 0; i < nb_pkts; i++) {
197 calculate_timestamp(&header.ts);
198 header.len = mbuf->pkt.data_len;
199 header.caplen = header.len;
200 pcap_dump((u_char*) dumper_q->dumper, &header, mbuf->pkt.data);
201 rte_pktmbuf_free(mbuf);
206 * Since there's no place to hook a callback when the forwarding
207 * process stops and to make sure the pcap file is actually written,
208 * we flush the pcap dumper within each burst.
210 pcap_dump_flush(dumper_q->dumper);
211 dumper_q->tx_pkts += num_tx;
212 dumper_q->err_pkts += nb_pkts - num_tx;
217 * Callback to handle sending packets through a real NIC.
220 eth_pcap_tx(void *queue,
221 struct rte_mbuf **bufs,
226 struct rte_mbuf *mbuf;
227 struct pcap_tx_queue *tx_queue = queue;
230 if (unlikely(nb_pkts == 0 || tx_queue->pcap == NULL))
233 for (i = 0; i < nb_pkts; i++) {
235 ret = pcap_sendpacket(tx_queue->pcap, (u_char*) mbuf->pkt.data,
239 rte_pktmbuf_free(mbuf);
242 tx_queue->tx_pkts += num_tx;
243 tx_queue->err_pkts += nb_pkts - num_tx;
248 eth_dev_start(struct rte_eth_dev *dev)
250 dev->data->dev_link.link_status = 1;
255 * This function gets called when the current port gets stopped.
256 * Is the only place for us to close all the tx streams dumpers.
257 * If not called the dumpers will be flushed within each tx burst.
260 eth_dev_stop(struct rte_eth_dev *dev)
263 pcap_dumper_t *dumper;
265 struct pmd_internals *internals = dev->data->dev_private;
267 for (i = 0; i < internals->nb_tx_queues; i++) {
268 dumper = internals->tx_queue[i].dumper;
270 pcap_dump_close(dumper);
271 pcap = internals->tx_queue[i].pcap;
276 dev->data->dev_link.link_status = 0;
280 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
286 eth_dev_info(struct rte_eth_dev *dev,
287 struct rte_eth_dev_info *dev_info)
289 struct pmd_internals *internals = dev->data->dev_private;
290 dev_info->driver_name = drivername;
291 dev_info->max_mac_addrs = 1;
292 dev_info->max_rx_pktlen = (uint32_t) -1;
293 dev_info->max_rx_queues = (uint16_t)internals->nb_rx_queues;
294 dev_info->max_tx_queues = (uint16_t)internals->nb_tx_queues;
295 dev_info->min_rx_bufsize = 0;
296 dev_info->pci_dev = NULL;
300 eth_stats_get(struct rte_eth_dev *dev,
301 struct rte_eth_stats *igb_stats)
304 unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
305 const struct pmd_internals *internal = dev->data->dev_private;
307 memset(igb_stats, 0, sizeof(*igb_stats));
308 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && i < internal->nb_rx_queues;
310 igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts;
311 rx_total += igb_stats->q_ipackets[i];
314 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && i < internal->nb_tx_queues;
316 igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts;
317 igb_stats->q_errors[i] = internal->tx_queue[i].err_pkts;
318 tx_total += igb_stats->q_opackets[i];
319 tx_err_total += igb_stats->q_errors[i];
322 igb_stats->ipackets = rx_total;
323 igb_stats->opackets = tx_total;
324 igb_stats->oerrors = tx_err_total;
328 eth_stats_reset(struct rte_eth_dev *dev)
331 struct pmd_internals *internal = dev->data->dev_private;
332 for (i = 0; i < internal->nb_rx_queues; i++)
333 internal->rx_queue[i].rx_pkts = 0;
334 for (i = 0; i < internal->nb_tx_queues; i++) {
335 internal->tx_queue[i].tx_pkts = 0;
336 internal->tx_queue[i].err_pkts = 0;
341 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
346 eth_queue_release(void *q __rte_unused)
351 eth_link_update(struct rte_eth_dev *dev __rte_unused,
352 int wait_to_complete __rte_unused)
358 eth_rx_queue_setup(struct rte_eth_dev *dev,
359 uint16_t rx_queue_id,
360 uint16_t nb_rx_desc __rte_unused,
361 unsigned int socket_id __rte_unused,
362 const struct rte_eth_rxconf *rx_conf __rte_unused,
363 struct rte_mempool *mb_pool)
365 struct pmd_internals *internals = dev->data->dev_private;
366 struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
367 pcap_q->mb_pool = mb_pool;
368 dev->data->rx_queues[rx_queue_id] = pcap_q;
373 eth_tx_queue_setup(struct rte_eth_dev *dev,
374 uint16_t tx_queue_id,
375 uint16_t nb_tx_desc __rte_unused,
376 unsigned int socket_id __rte_unused,
377 const struct rte_eth_txconf *tx_conf __rte_unused)
380 struct pmd_internals *internals = dev->data->dev_private;
381 dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
385 static struct eth_dev_ops ops = {
386 .dev_start = eth_dev_start,
387 .dev_stop = eth_dev_stop,
388 .dev_close = eth_dev_close,
389 .dev_configure = eth_dev_configure,
390 .dev_infos_get = eth_dev_info,
391 .rx_queue_setup = eth_rx_queue_setup,
392 .tx_queue_setup = eth_tx_queue_setup,
393 .rx_queue_release = eth_queue_release,
394 .tx_queue_release = eth_queue_release,
395 .link_update = eth_link_update,
396 .stats_get = eth_stats_get,
397 .stats_reset = eth_stats_reset,
401 * Function handler that opens the pcap file for reading a stores a
402 * reference of it for use it later on.
405 open_rx_pcap(char *value, void *extra_args)
408 char *pcap_filename = value;
409 struct rx_pcaps *pcaps = extra_args;
412 for (i = 0; i < pcaps->num_of_rx; i++) {
413 if ((rx_pcap = pcap_open_offline(pcap_filename, errbuf)) == NULL) {
414 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename, errbuf);
417 pcaps->pcaps[i] = rx_pcap;
424 * Opens a pcap file for writing and stores a reference to it
425 * for use it later on.
428 open_tx_pcap(char *value, void *extra_args)
431 char *pcap_filename = value;
432 struct tx_pcaps *dumpers = extra_args;
434 pcap_dumper_t *dumper;
436 for (i = 0; i < dumpers->num_of_tx; i++) {
438 * We need to create a dummy empty pcap_t to use it
439 * with pcap_dump_open(). We create big enough an Ethernet
442 if ((tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN))
444 RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
448 /* The dumper is created using the previous pcap_t reference */
449 if ((dumper = pcap_dump_open(tx_pcap, pcap_filename)) == NULL) {
450 RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n", pcap_filename);
453 dumpers->dumpers[i] = dumper;
460 * pcap_open_live wrapper function
463 open_iface_live(const char *iface, pcap_t **pcap) {
464 *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
465 RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
468 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
475 * Opens an interface for reading and writing
478 open_rx_tx_iface(char *value, void *extra_args)
480 const char *iface = value;
481 pcap_t **pcap = extra_args;
483 if(open_iface_live(iface, pcap) < 0)
489 * Opens a NIC for reading packets from it
492 open_rx_iface(char *value, void *extra_args)
495 const char *iface = value;
496 struct rx_pcaps *pcaps = extra_args;
499 for (i = 0; i < pcaps->num_of_rx; i++) {
500 if(open_iface_live(iface, &pcap) < 0)
502 pcaps->pcaps[i] = pcap;
509 * Opens a NIC for writing packets to it
512 open_tx_iface(char *value, void *extra_args)
515 const char *iface = value;
516 struct tx_pcaps *pcaps = extra_args;
519 for (i = 0; i < pcaps->num_of_tx; i++) {
520 if(open_iface_live(iface, &pcap) < 0)
522 pcaps->pcaps[i] = pcap;
530 rte_pmd_init_internals(const unsigned nb_rx_queues,
531 const unsigned nb_tx_queues,
532 const unsigned numa_node,
533 struct pmd_internals **internals,
534 struct rte_eth_dev **eth_dev)
536 struct rte_eth_dev_data *data = NULL;
537 struct rte_pci_device *pci_dev = NULL;
540 "Creating pcap-backed ethdev on numa socket %u\n", numa_node);
542 /* now do all data allocation - for eth_dev structure, dummy pci driver
543 * and internal (private) data
545 data = rte_zmalloc_socket(NULL, sizeof(*data), 0, numa_node);
549 pci_dev = rte_zmalloc_socket(NULL, sizeof(*pci_dev), 0, numa_node);
553 *internals = rte_zmalloc_socket(NULL, sizeof(**internals), 0, numa_node);
554 if (*internals == NULL)
557 /* reserve an ethdev entry */
558 *eth_dev = rte_eth_dev_allocate();
559 if (*eth_dev == NULL)
562 /* now put it all together
563 * - store queue data in internals,
564 * - store numa_node info in pci_driver
565 * - point eth_dev_data to internals and pci_driver
566 * - and point eth_dev structure to new eth_dev_data structure
568 /* NOTE: we'll replace the data element, of originally allocated eth_dev
569 * so the rings are local per-process */
571 (*internals)->nb_rx_queues = nb_rx_queues;
572 (*internals)->nb_tx_queues = nb_tx_queues;
574 pci_dev->numa_node = numa_node;
576 data->dev_private = *internals;
577 data->port_id = (*eth_dev)->data->port_id;
578 data->nb_rx_queues = (uint16_t)nb_rx_queues;
579 data->nb_tx_queues = (uint16_t)nb_tx_queues;
580 data->dev_link = pmd_link;
581 data->mac_addrs = ð_addr;
583 (*eth_dev)->data = data;
584 (*eth_dev)->dev_ops = &ops;
585 (*eth_dev)->pci_dev = pci_dev;
594 rte_free(*internals);
599 rte_eth_from_pcaps_n_dumpers(pcap_t * const rx_queues[],
600 const unsigned nb_rx_queues,
601 pcap_dumper_t * const tx_queues[],
602 const unsigned nb_tx_queues,
603 const unsigned numa_node)
605 struct pmd_internals *internals = NULL;
606 struct rte_eth_dev *eth_dev = NULL;
609 /* do some parameter checking */
610 if (rx_queues == NULL && nb_rx_queues > 0)
612 if (tx_queues == NULL && nb_tx_queues > 0)
615 if (rte_pmd_init_internals(nb_rx_queues, nb_tx_queues, numa_node,
616 &internals, ð_dev) < 0)
619 for (i = 0; i < nb_rx_queues; i++) {
620 internals->rx_queue->pcap = rx_queues[i];
622 for (i = 0; i < nb_tx_queues; i++) {
623 internals->tx_queue->dumper = tx_queues[i];
626 eth_dev->rx_pkt_burst = eth_pcap_rx;
627 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
633 rte_eth_from_pcaps(pcap_t * const rx_queues[],
634 const unsigned nb_rx_queues,
635 pcap_t * const tx_queues[],
636 const unsigned nb_tx_queues,
637 const unsigned numa_node)
639 struct pmd_internals *internals = NULL;
640 struct rte_eth_dev *eth_dev = NULL;
643 /* do some parameter checking */
644 if (rx_queues == NULL && nb_rx_queues > 0)
646 if (tx_queues == NULL && nb_tx_queues > 0)
649 if (rte_pmd_init_internals(nb_rx_queues, nb_tx_queues, numa_node,
650 &internals, ð_dev) < 0)
653 for (i = 0; i < nb_rx_queues; i++) {
654 internals->rx_queue->pcap = rx_queues[i];
656 for (i = 0; i < nb_tx_queues; i++) {
657 internals->tx_queue->pcap = tx_queues[i];
660 eth_dev->rx_pkt_burst = eth_pcap_rx;
661 eth_dev->tx_pkt_burst = eth_pcap_tx;
668 rte_pmd_pcap_init(const char *name, const char *params)
670 unsigned numa_node, using_dumpers = 0;
672 struct args_dict dict;
673 struct rx_pcaps pcaps;
674 struct tx_pcaps dumpers;
676 rte_eth_pcap_init_args_dict(&dict);
678 numa_node = rte_socket_id();
680 gettimeofday(&start_time, NULL);
681 start_cycles = rte_get_timer_cycles();
682 hz = rte_get_timer_hz();
684 if (rte_eth_pcap_parse_args(&dict, name, params, valid_arguments) < 0)
688 * If iface argument is passed we open the NICs and use them for
691 if (rte_eth_pcap_num_of_args(&dict, ETH_PCAP_IFACE_ARG) == 1) {
693 ret = rte_eth_pcap_post_process_arguments(&dict, ETH_PCAP_IFACE_ARG,
694 &open_rx_tx_iface, &pcaps.pcaps[0]);
698 return rte_eth_from_pcaps(pcaps.pcaps, 1, pcaps.pcaps, 1, numa_node);
702 * We check whether we want to open a RX stream from a real NIC or a
705 if ((pcaps.num_of_rx = rte_eth_pcap_num_of_args(&dict, ETH_PCAP_RX_PCAP_ARG))) {
706 ret = rte_eth_pcap_post_process_arguments(&dict, ETH_PCAP_RX_PCAP_ARG,
707 &open_rx_pcap, &pcaps);
709 pcaps.num_of_rx = rte_eth_pcap_num_of_args(&dict,
710 ETH_PCAP_RX_IFACE_ARG);
711 ret = rte_eth_pcap_post_process_arguments(&dict, ETH_PCAP_RX_IFACE_ARG,
712 &open_rx_iface, &pcaps);
719 * We check whether we want to open a TX stream to a real NIC or a
722 if ((dumpers.num_of_tx = rte_eth_pcap_num_of_args(&dict,
723 ETH_PCAP_TX_PCAP_ARG))) {
724 ret = rte_eth_pcap_post_process_arguments(&dict, ETH_PCAP_TX_PCAP_ARG,
725 &open_tx_pcap, &dumpers);
728 dumpers.num_of_tx = rte_eth_pcap_num_of_args(&dict,
729 ETH_PCAP_TX_IFACE_ARG);
730 ret = rte_eth_pcap_post_process_arguments(&dict, ETH_PCAP_TX_IFACE_ARG,
731 &open_tx_iface, &dumpers);
738 return rte_eth_from_pcaps_n_dumpers(pcaps.pcaps, pcaps.num_of_rx,
739 dumpers.dumpers, dumpers.num_of_tx, numa_node);
741 return rte_eth_from_pcaps(pcaps.pcaps, pcaps.num_of_rx, dumpers.pcaps,
742 dumpers.num_of_tx, numa_node);