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>
49 #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535
50 #define RTE_ETH_PCAP_SNAPLEN 4096
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"
59 static char errbuf[PCAP_ERRBUF_SIZE];
60 static struct timeval start_time;
61 static uint64_t start_cycles;
64 struct pcap_rx_queue {
66 struct rte_mempool *mb_pool;
67 volatile unsigned long rx_pkts;
68 volatile unsigned long err_pkts;
71 struct pcap_tx_queue {
72 pcap_dumper_t *dumper;
74 volatile unsigned long tx_pkts;
75 volatile unsigned long err_pkts;
80 pcap_t *pcaps[RTE_PMD_RING_MAX_RX_RINGS];
85 pcap_dumper_t *dumpers[RTE_PMD_RING_MAX_TX_RINGS];
86 pcap_t *pcaps[RTE_PMD_RING_MAX_RX_RINGS];
89 struct pmd_internals {
90 unsigned nb_rx_queues;
91 unsigned nb_tx_queues;
95 struct pcap_rx_queue rx_queue[RTE_PMD_RING_MAX_RX_RINGS];
96 struct pcap_tx_queue tx_queue[RTE_PMD_RING_MAX_TX_RINGS];
99 const char *valid_arguments[] = {
100 ETH_PCAP_RX_PCAP_ARG,
101 ETH_PCAP_TX_PCAP_ARG,
102 ETH_PCAP_RX_IFACE_ARG,
103 ETH_PCAP_TX_IFACE_ARG,
108 static struct ether_addr eth_addr = { .addr_bytes = { 0, 0, 0, 0x1, 0x2, 0x3 } };
109 static const char *drivername = "Pcap PMD";
110 static struct rte_eth_link pmd_link = {
112 .link_duplex = ETH_LINK_FULL_DUPLEX,
118 eth_pcap_rx(void *queue,
119 struct rte_mbuf **bufs,
123 struct pcap_pkthdr header;
124 const u_char *packet;
125 struct rte_mbuf *mbuf;
126 struct pcap_rx_queue *pcap_q = queue;
127 struct rte_pktmbuf_pool_private *mbp_priv;
131 if (unlikely(pcap_q->pcap == NULL || nb_pkts == 0))
134 /* Reads the given number of packets from the pcap file one by one
135 * and copies the packet data into a newly allocated mbuf to return.
137 for (i = 0; i < nb_pkts; i++) {
138 /* Get the next PCAP packet */
139 packet = pcap_next(pcap_q->pcap, &header);
140 if (unlikely(packet == NULL))
143 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
144 if (unlikely(mbuf == NULL))
147 /* Now get the space available for data in the mbuf */
148 mbp_priv = rte_mempool_get_priv(pcap_q->mb_pool);
149 buf_size = (uint16_t) (mbp_priv->mbuf_data_room_size -
150 RTE_PKTMBUF_HEADROOM);
152 if (header.len <= buf_size) {
153 /* pcap packet will fit in the mbuf, go ahead and copy */
154 rte_memcpy(mbuf->pkt.data, packet, header.len);
155 mbuf->pkt.data_len = (uint16_t)header.len;
156 mbuf->pkt.pkt_len = mbuf->pkt.data_len;
160 /* pcap packet will not fit in the mbuf, so drop packet */
162 "PCAP packet %d bytes will not fit in mbuf (%d bytes)\n",
163 header.len, buf_size);
164 rte_pktmbuf_free(mbuf);
167 pcap_q->rx_pkts += num_rx;
172 calculate_timestamp(struct timeval *ts) {
174 struct timeval cur_time;
176 cycles = rte_get_timer_cycles() - start_cycles;
177 cur_time.tv_sec = cycles / hz;
178 cur_time.tv_usec = (cycles % hz) * 10e6 / hz;
179 timeradd(&start_time, &cur_time, ts);
183 * Callback to handle writing packets to a pcap file.
186 eth_pcap_tx_dumper(void *queue,
187 struct rte_mbuf **bufs,
191 struct rte_mbuf *mbuf;
192 struct pcap_tx_queue *dumper_q = queue;
194 struct pcap_pkthdr header;
196 if (dumper_q->dumper == NULL || nb_pkts == 0)
199 /* writes the nb_pkts packets to the previously opened pcap file dumper */
200 for (i = 0; i < nb_pkts; i++) {
202 calculate_timestamp(&header.ts);
203 header.len = mbuf->pkt.data_len;
204 header.caplen = header.len;
205 pcap_dump((u_char*) dumper_q->dumper, &header, mbuf->pkt.data);
206 rte_pktmbuf_free(mbuf);
211 * Since there's no place to hook a callback when the forwarding
212 * process stops and to make sure the pcap file is actually written,
213 * we flush the pcap dumper within each burst.
215 pcap_dump_flush(dumper_q->dumper);
216 dumper_q->tx_pkts += num_tx;
217 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,
242 if (unlikely(ret != 0))
245 rte_pktmbuf_free(mbuf);
248 tx_queue->tx_pkts += num_tx;
249 tx_queue->err_pkts += nb_pkts - num_tx;
254 eth_dev_start(struct rte_eth_dev *dev)
256 dev->data->dev_link.link_status = 1;
261 * This function gets called when the current port gets stopped.
262 * Is the only place for us to close all the tx streams dumpers.
263 * If not called the dumpers will be flushed within each tx burst.
266 eth_dev_stop(struct rte_eth_dev *dev)
269 pcap_dumper_t *dumper;
271 struct pmd_internals *internals = dev->data->dev_private;
273 for (i = 0; i < internals->nb_tx_queues; i++) {
274 dumper = internals->tx_queue[i].dumper;
276 pcap_dump_close(dumper);
277 pcap = internals->tx_queue[i].pcap;
282 dev->data->dev_link.link_status = 0;
286 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
292 eth_dev_info(struct rte_eth_dev *dev,
293 struct rte_eth_dev_info *dev_info)
295 struct pmd_internals *internals = dev->data->dev_private;
296 dev_info->driver_name = drivername;
297 dev_info->if_index = internals->if_index;
298 dev_info->max_mac_addrs = 1;
299 dev_info->max_rx_pktlen = (uint32_t) -1;
300 dev_info->max_rx_queues = (uint16_t)internals->nb_rx_queues;
301 dev_info->max_tx_queues = (uint16_t)internals->nb_tx_queues;
302 dev_info->min_rx_bufsize = 0;
303 dev_info->pci_dev = NULL;
307 eth_stats_get(struct rte_eth_dev *dev,
308 struct rte_eth_stats *igb_stats)
311 unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
312 const struct pmd_internals *internal = dev->data->dev_private;
314 memset(igb_stats, 0, sizeof(*igb_stats));
315 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && i < internal->nb_rx_queues;
317 igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts;
318 rx_total += igb_stats->q_ipackets[i];
321 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && i < internal->nb_tx_queues;
323 igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts;
324 igb_stats->q_errors[i] = internal->tx_queue[i].err_pkts;
325 tx_total += igb_stats->q_opackets[i];
326 tx_err_total += igb_stats->q_errors[i];
329 igb_stats->ipackets = rx_total;
330 igb_stats->opackets = tx_total;
331 igb_stats->oerrors = tx_err_total;
335 eth_stats_reset(struct rte_eth_dev *dev)
338 struct pmd_internals *internal = dev->data->dev_private;
339 for (i = 0; i < internal->nb_rx_queues; i++)
340 internal->rx_queue[i].rx_pkts = 0;
341 for (i = 0; i < internal->nb_tx_queues; i++) {
342 internal->tx_queue[i].tx_pkts = 0;
343 internal->tx_queue[i].err_pkts = 0;
348 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
353 eth_queue_release(void *q __rte_unused)
358 eth_link_update(struct rte_eth_dev *dev __rte_unused,
359 int wait_to_complete __rte_unused)
365 eth_rx_queue_setup(struct rte_eth_dev *dev,
366 uint16_t rx_queue_id,
367 uint16_t nb_rx_desc __rte_unused,
368 unsigned int socket_id __rte_unused,
369 const struct rte_eth_rxconf *rx_conf __rte_unused,
370 struct rte_mempool *mb_pool)
372 struct pmd_internals *internals = dev->data->dev_private;
373 struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
374 pcap_q->mb_pool = mb_pool;
375 dev->data->rx_queues[rx_queue_id] = pcap_q;
380 eth_tx_queue_setup(struct rte_eth_dev *dev,
381 uint16_t tx_queue_id,
382 uint16_t nb_tx_desc __rte_unused,
383 unsigned int socket_id __rte_unused,
384 const struct rte_eth_txconf *tx_conf __rte_unused)
387 struct pmd_internals *internals = dev->data->dev_private;
388 dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
392 static struct eth_dev_ops ops = {
393 .dev_start = eth_dev_start,
394 .dev_stop = eth_dev_stop,
395 .dev_close = eth_dev_close,
396 .dev_configure = eth_dev_configure,
397 .dev_infos_get = eth_dev_info,
398 .rx_queue_setup = eth_rx_queue_setup,
399 .tx_queue_setup = eth_tx_queue_setup,
400 .rx_queue_release = eth_queue_release,
401 .tx_queue_release = eth_queue_release,
402 .link_update = eth_link_update,
403 .stats_get = eth_stats_get,
404 .stats_reset = eth_stats_reset,
408 * Function handler that opens the pcap file for reading a stores a
409 * reference of it for use it later on.
412 open_rx_pcap(const char *key __rte_unused, const char *value, void *extra_args)
415 const char *pcap_filename = value;
416 struct rx_pcaps *pcaps = extra_args;
419 for (i = 0; i < pcaps->num_of_rx; i++) {
420 if ((rx_pcap = pcap_open_offline(pcap_filename, errbuf)) == NULL) {
421 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename, errbuf);
424 pcaps->pcaps[i] = rx_pcap;
431 * Opens a pcap file for writing and stores a reference to it
432 * for use it later on.
435 open_tx_pcap(const char *key __rte_unused, const char *value, void *extra_args)
438 const char *pcap_filename = value;
439 struct tx_pcaps *dumpers = extra_args;
441 pcap_dumper_t *dumper;
443 for (i = 0; i < dumpers->num_of_tx; i++) {
445 * We need to create a dummy empty pcap_t to use it
446 * with pcap_dump_open(). We create big enough an Ethernet
449 if ((tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN))
451 RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
455 /* The dumper is created using the previous pcap_t reference */
456 if ((dumper = pcap_dump_open(tx_pcap, pcap_filename)) == NULL) {
457 RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n", pcap_filename);
460 dumpers->dumpers[i] = dumper;
467 * pcap_open_live wrapper function
470 open_iface_live(const char *iface, pcap_t **pcap) {
471 *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
472 RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
475 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
482 * Opens an interface for reading and writing
485 open_rx_tx_iface(const char *key __rte_unused, const char *value, void *extra_args)
487 const char *iface = value;
488 pcap_t **pcap = extra_args;
490 if(open_iface_live(iface, pcap) < 0)
496 * Opens a NIC for reading packets from it
499 open_rx_iface(const char *key __rte_unused, const char *value, void *extra_args)
502 const char *iface = value;
503 struct rx_pcaps *pcaps = extra_args;
506 for (i = 0; i < pcaps->num_of_rx; i++) {
507 if(open_iface_live(iface, &pcap) < 0)
509 pcaps->pcaps[i] = pcap;
516 * Opens a NIC for writing packets to it
519 open_tx_iface(const char *key __rte_unused, const char *value, void *extra_args)
522 const char *iface = value;
523 struct tx_pcaps *pcaps = extra_args;
526 for (i = 0; i < pcaps->num_of_tx; i++) {
527 if(open_iface_live(iface, &pcap) < 0)
529 pcaps->pcaps[i] = pcap;
537 rte_pmd_init_internals(const unsigned nb_rx_queues,
538 const unsigned nb_tx_queues,
539 const unsigned numa_node,
540 struct pmd_internals **internals,
541 struct rte_eth_dev **eth_dev,
542 struct rte_kvargs *kvlist)
544 struct rte_eth_dev_data *data = NULL;
545 struct rte_pci_device *pci_dev = NULL;
547 struct rte_kvargs_pair *pair = NULL;
549 for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
550 pair = &kvlist->pairs[k_idx];
551 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
556 "Creating pcap-backed ethdev on numa socket %u\n", numa_node);
558 /* now do all data allocation - for eth_dev structure, dummy pci driver
559 * and internal (private) data
561 data = rte_zmalloc_socket(NULL, sizeof(*data), 0, numa_node);
565 pci_dev = rte_zmalloc_socket(NULL, sizeof(*pci_dev), 0, numa_node);
569 *internals = rte_zmalloc_socket(NULL, sizeof(**internals), 0, numa_node);
570 if (*internals == NULL)
573 /* reserve an ethdev entry */
574 *eth_dev = rte_eth_dev_allocate();
575 if (*eth_dev == NULL)
578 /* now put it all together
579 * - store queue data in internals,
580 * - store numa_node info in pci_driver
581 * - point eth_dev_data to internals and pci_driver
582 * - and point eth_dev structure to new eth_dev_data structure
584 /* NOTE: we'll replace the data element, of originally allocated eth_dev
585 * so the rings are local per-process */
587 (*internals)->nb_rx_queues = nb_rx_queues;
588 (*internals)->nb_tx_queues = nb_tx_queues;
591 (*internals)->if_index = 0;
593 (*internals)->if_index = if_nametoindex(pair->value);
595 pci_dev->numa_node = numa_node;
597 data->dev_private = *internals;
598 data->port_id = (*eth_dev)->data->port_id;
599 data->nb_rx_queues = (uint16_t)nb_rx_queues;
600 data->nb_tx_queues = (uint16_t)nb_tx_queues;
601 data->dev_link = pmd_link;
602 data->mac_addrs = ð_addr;
604 (*eth_dev)->data = data;
605 (*eth_dev)->dev_ops = &ops;
606 (*eth_dev)->pci_dev = pci_dev;
615 rte_free(*internals);
620 rte_eth_from_pcaps_n_dumpers(pcap_t * const rx_queues[],
621 const unsigned nb_rx_queues,
622 pcap_dumper_t * const tx_queues[],
623 const unsigned nb_tx_queues,
624 const unsigned numa_node,
625 struct rte_kvargs *kvlist)
627 struct pmd_internals *internals = NULL;
628 struct rte_eth_dev *eth_dev = NULL;
631 /* do some parameter checking */
632 if (rx_queues == NULL && nb_rx_queues > 0)
634 if (tx_queues == NULL && nb_tx_queues > 0)
637 if (rte_pmd_init_internals(nb_rx_queues, nb_tx_queues, numa_node,
638 &internals, ð_dev, kvlist) < 0)
641 for (i = 0; i < nb_rx_queues; i++) {
642 internals->rx_queue->pcap = rx_queues[i];
644 for (i = 0; i < nb_tx_queues; i++) {
645 internals->tx_queue->dumper = tx_queues[i];
648 eth_dev->rx_pkt_burst = eth_pcap_rx;
649 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
655 rte_eth_from_pcaps(pcap_t * const rx_queues[],
656 const unsigned nb_rx_queues,
657 pcap_t * const tx_queues[],
658 const unsigned nb_tx_queues,
659 const unsigned numa_node,
660 struct rte_kvargs *kvlist)
662 struct pmd_internals *internals = NULL;
663 struct rte_eth_dev *eth_dev = NULL;
666 /* do some parameter checking */
667 if (rx_queues == NULL && nb_rx_queues > 0)
669 if (tx_queues == NULL && nb_tx_queues > 0)
672 if (rte_pmd_init_internals(nb_rx_queues, nb_tx_queues, numa_node,
673 &internals, ð_dev, kvlist) < 0)
676 for (i = 0; i < nb_rx_queues; i++) {
677 internals->rx_queue->pcap = rx_queues[i];
679 for (i = 0; i < nb_tx_queues; i++) {
680 internals->tx_queue->pcap = tx_queues[i];
683 eth_dev->rx_pkt_burst = eth_pcap_rx;
684 eth_dev->tx_pkt_burst = eth_pcap_tx;
691 rte_pmd_pcap_devinit(const char *name, const char *params)
693 unsigned numa_node, using_dumpers = 0;
695 struct rte_kvargs *kvlist;
696 struct rx_pcaps pcaps;
697 struct tx_pcaps dumpers;
699 RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
701 numa_node = rte_socket_id();
703 gettimeofday(&start_time, NULL);
704 start_cycles = rte_get_timer_cycles();
705 hz = rte_get_timer_hz();
707 kvlist = rte_kvargs_parse(params, valid_arguments);
712 * If iface argument is passed we open the NICs and use them for
715 if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
717 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
718 &open_rx_tx_iface, &pcaps.pcaps[0]);
722 return rte_eth_from_pcaps(pcaps.pcaps, 1, pcaps.pcaps, 1,
727 * We check whether we want to open a RX stream from a real NIC or a
730 if ((pcaps.num_of_rx = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG))) {
731 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
732 &open_rx_pcap, &pcaps);
734 pcaps.num_of_rx = rte_kvargs_count(kvlist,
735 ETH_PCAP_RX_IFACE_ARG);
736 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
737 &open_rx_iface, &pcaps);
744 * We check whether we want to open a TX stream to a real NIC or a
747 if ((dumpers.num_of_tx = rte_kvargs_count(kvlist,
748 ETH_PCAP_TX_PCAP_ARG))) {
749 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
750 &open_tx_pcap, &dumpers);
753 dumpers.num_of_tx = rte_kvargs_count(kvlist,
754 ETH_PCAP_TX_IFACE_ARG);
755 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
756 &open_tx_iface, &dumpers);
763 return rte_eth_from_pcaps_n_dumpers(pcaps.pcaps, pcaps.num_of_rx,
764 dumpers.dumpers, dumpers.num_of_tx, numa_node, kvlist);
766 return rte_eth_from_pcaps(pcaps.pcaps, pcaps.num_of_rx, dumpers.pcaps,
767 dumpers.num_of_tx, numa_node, kvlist);
771 static struct rte_driver pmd_pcap_drv = {
774 .init = rte_pmd_pcap_devinit,
777 PMD_REGISTER_DRIVER(pmd_pcap_drv);