4 * Copyright(c) 2010-2016 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 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"
59 #define ETH_PCAP_ARG_MAXLEN 64
61 #define RTE_PMD_PCAP_MAX_QUEUES 16
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;
70 volatile unsigned long pkts;
71 volatile unsigned long bytes;
72 volatile unsigned long err_pkts;
75 struct pcap_rx_queue {
78 struct rte_mempool *mb_pool;
79 struct queue_stat rx_stat;
81 char type[ETH_PCAP_ARG_MAXLEN];
84 struct pcap_tx_queue {
85 pcap_dumper_t *dumper;
87 struct queue_stat tx_stat;
89 char type[ETH_PCAP_ARG_MAXLEN];
92 struct pmd_internals {
93 struct pcap_rx_queue rx_queue[RTE_PMD_PCAP_MAX_QUEUES];
94 struct pcap_tx_queue tx_queue[RTE_PMD_PCAP_MAX_QUEUES];
100 unsigned num_of_queue;
101 struct devargs_queue {
102 pcap_dumper_t *dumper;
106 } queue[RTE_PMD_PCAP_MAX_QUEUES];
109 const char *valid_arguments[] = {
110 ETH_PCAP_RX_PCAP_ARG,
111 ETH_PCAP_TX_PCAP_ARG,
112 ETH_PCAP_RX_IFACE_ARG,
113 ETH_PCAP_TX_IFACE_ARG,
118 static int open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper);
119 static int open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap);
120 static int open_single_iface(const char *iface, pcap_t **pcap);
122 static struct ether_addr eth_addr = { .addr_bytes = { 0, 0, 0, 0x1, 0x2, 0x3 } };
123 static const char *drivername = "Pcap PMD";
124 static struct rte_eth_link pmd_link = {
125 .link_speed = ETH_SPEED_NUM_10G,
126 .link_duplex = ETH_LINK_FULL_DUPLEX,
127 .link_status = ETH_LINK_DOWN,
128 .link_autoneg = ETH_LINK_SPEED_FIXED,
132 eth_pcap_rx_jumbo(struct rte_mempool *mb_pool,
133 struct rte_mbuf *mbuf,
137 struct rte_mbuf *m = mbuf;
139 /* Copy the first segment. */
140 uint16_t len = rte_pktmbuf_tailroom(mbuf);
142 rte_memcpy(rte_pktmbuf_append(mbuf, len), data, len);
146 while (data_len > 0) {
147 /* Allocate next mbuf and point to that. */
148 m->next = rte_pktmbuf_alloc(mb_pool);
150 if (unlikely(!m->next))
155 /* Headroom is not needed in chained mbufs. */
156 rte_pktmbuf_prepend(m, rte_pktmbuf_headroom(m));
160 /* Copy next segment. */
161 len = RTE_MIN(rte_pktmbuf_tailroom(m), data_len);
162 rte_memcpy(rte_pktmbuf_append(m, len), data, len);
169 return mbuf->nb_segs;
172 /* Copy data from mbuf chain to a buffer suitable for writing to a PCAP file. */
174 eth_pcap_gather_data(unsigned char *data, struct rte_mbuf *mbuf)
176 uint16_t data_len = 0;
179 rte_memcpy(data + data_len, rte_pktmbuf_mtod(mbuf, void *),
182 data_len += mbuf->data_len;
188 eth_pcap_rx(void *queue,
189 struct rte_mbuf **bufs,
193 struct pcap_pkthdr header;
194 const u_char *packet;
195 struct rte_mbuf *mbuf;
196 struct pcap_rx_queue *pcap_q = queue;
199 uint32_t rx_bytes = 0;
201 if (unlikely(pcap_q->pcap == NULL || nb_pkts == 0))
204 /* Reads the given number of packets from the pcap file one by one
205 * and copies the packet data into a newly allocated mbuf to return.
207 for (i = 0; i < nb_pkts; i++) {
208 /* Get the next PCAP packet */
209 packet = pcap_next(pcap_q->pcap, &header);
210 if (unlikely(packet == NULL))
213 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
214 if (unlikely(mbuf == NULL))
217 /* Now get the space available for data in the mbuf */
218 buf_size = (uint16_t)(rte_pktmbuf_data_room_size(pcap_q->mb_pool) -
219 RTE_PKTMBUF_HEADROOM);
221 if (header.caplen <= buf_size) {
222 /* pcap packet will fit in the mbuf, go ahead and copy */
223 rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), packet,
225 mbuf->data_len = (uint16_t)header.caplen;
227 /* Try read jumbo frame into multi mbufs. */
228 if (unlikely(eth_pcap_rx_jumbo(pcap_q->mb_pool,
231 header.caplen) == -1))
235 mbuf->pkt_len = (uint16_t)header.caplen;
236 mbuf->port = pcap_q->in_port;
239 rx_bytes += header.caplen;
241 pcap_q->rx_stat.pkts += num_rx;
242 pcap_q->rx_stat.bytes += rx_bytes;
247 calculate_timestamp(struct timeval *ts) {
249 struct timeval cur_time;
251 cycles = rte_get_timer_cycles() - start_cycles;
252 cur_time.tv_sec = cycles / hz;
253 cur_time.tv_usec = (cycles % hz) * 10e6 / hz;
254 timeradd(&start_time, &cur_time, ts);
258 * Callback to handle writing packets to a pcap file.
261 eth_pcap_tx_dumper(void *queue,
262 struct rte_mbuf **bufs,
266 struct rte_mbuf *mbuf;
267 struct pcap_tx_queue *dumper_q = queue;
269 uint32_t tx_bytes = 0;
270 struct pcap_pkthdr header;
272 if (dumper_q->dumper == NULL || nb_pkts == 0)
275 /* writes the nb_pkts packets to the previously opened pcap file dumper */
276 for (i = 0; i < nb_pkts; i++) {
278 calculate_timestamp(&header.ts);
279 header.len = mbuf->pkt_len;
280 header.caplen = header.len;
282 if (likely(mbuf->nb_segs == 1)) {
283 pcap_dump((u_char *)dumper_q->dumper, &header,
284 rte_pktmbuf_mtod(mbuf, void*));
286 if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
287 eth_pcap_gather_data(tx_pcap_data, mbuf);
288 pcap_dump((u_char *)dumper_q->dumper, &header,
292 "Dropping PCAP packet. "
293 "Size (%d) > max jumbo size (%d).\n",
295 ETHER_MAX_JUMBO_FRAME_LEN);
297 rte_pktmbuf_free(mbuf);
302 rte_pktmbuf_free(mbuf);
304 tx_bytes += mbuf->pkt_len;
308 * Since there's no place to hook a callback when the forwarding
309 * process stops and to make sure the pcap file is actually written,
310 * we flush the pcap dumper within each burst.
312 pcap_dump_flush(dumper_q->dumper);
313 dumper_q->tx_stat.pkts += num_tx;
314 dumper_q->tx_stat.bytes += tx_bytes;
315 dumper_q->tx_stat.err_pkts += nb_pkts - num_tx;
320 * Callback to handle sending packets through a real NIC.
323 eth_pcap_tx(void *queue,
324 struct rte_mbuf **bufs,
329 struct rte_mbuf *mbuf;
330 struct pcap_tx_queue *tx_queue = queue;
332 uint32_t tx_bytes = 0;
334 if (unlikely(nb_pkts == 0 || tx_queue->pcap == NULL))
337 for (i = 0; i < nb_pkts; i++) {
340 if (likely(mbuf->nb_segs == 1)) {
341 ret = pcap_sendpacket(tx_queue->pcap,
342 rte_pktmbuf_mtod(mbuf, u_char *),
345 if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
346 eth_pcap_gather_data(tx_pcap_data, mbuf);
347 ret = pcap_sendpacket(tx_queue->pcap,
352 "Dropping PCAP packet. "
353 "Size (%d) > max jumbo size (%d).\n",
355 ETHER_MAX_JUMBO_FRAME_LEN);
357 rte_pktmbuf_free(mbuf);
362 if (unlikely(ret != 0))
365 tx_bytes += mbuf->pkt_len;
366 rte_pktmbuf_free(mbuf);
369 tx_queue->tx_stat.pkts += num_tx;
370 tx_queue->tx_stat.bytes += tx_bytes;
371 tx_queue->tx_stat.err_pkts += nb_pkts - num_tx;
376 eth_dev_start(struct rte_eth_dev *dev)
379 struct pmd_internals *internals = dev->data->dev_private;
380 struct pcap_tx_queue *tx;
381 struct pcap_rx_queue *rx;
383 /* Special iface case. Single pcap is open and shared between tx/rx. */
384 if (internals->single_iface) {
385 tx = &internals->tx_queue[0];
386 rx = &internals->rx_queue[0];
388 if (!tx->pcap && strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
389 if (open_single_iface(tx->name, &tx->pcap) < 0)
396 /* If not open already, open tx pcaps/dumpers */
397 for (i = 0; i < dev->data->nb_tx_queues; i++) {
398 tx = &internals->tx_queue[i];
400 if (!tx->dumper && strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
401 if (open_single_tx_pcap(tx->name, &tx->dumper) < 0)
405 else if (!tx->pcap && strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
406 if (open_single_iface(tx->name, &tx->pcap) < 0)
411 /* If not open already, open rx pcaps */
412 for (i = 0; i < dev->data->nb_rx_queues; i++) {
413 rx = &internals->rx_queue[i];
415 if (rx->pcap != NULL)
418 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
419 if (open_single_rx_pcap(rx->name, &rx->pcap) < 0)
423 else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
424 if (open_single_iface(rx->name, &rx->pcap) < 0)
431 dev->data->dev_link.link_status = ETH_LINK_UP;
436 * This function gets called when the current port gets stopped.
437 * Is the only place for us to close all the tx streams dumpers.
438 * If not called the dumpers will be flushed within each tx burst.
441 eth_dev_stop(struct rte_eth_dev *dev)
444 struct pmd_internals *internals = dev->data->dev_private;
445 struct pcap_tx_queue *tx;
446 struct pcap_rx_queue *rx;
448 /* Special iface case. Single pcap is open and shared between tx/rx. */
449 if (internals->single_iface) {
450 tx = &internals->tx_queue[0];
451 rx = &internals->rx_queue[0];
452 pcap_close(tx->pcap);
458 for (i = 0; i < dev->data->nb_tx_queues; i++) {
459 tx = &internals->tx_queue[i];
461 if (tx->dumper != NULL) {
462 pcap_dump_close(tx->dumper);
466 if (tx->pcap != NULL) {
467 pcap_close(tx->pcap);
472 for (i = 0; i < dev->data->nb_rx_queues; i++) {
473 rx = &internals->rx_queue[i];
475 if (rx->pcap != NULL) {
476 pcap_close(rx->pcap);
482 dev->data->dev_link.link_status = ETH_LINK_DOWN;
486 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
492 eth_dev_info(struct rte_eth_dev *dev,
493 struct rte_eth_dev_info *dev_info)
495 struct pmd_internals *internals = dev->data->dev_private;
496 dev_info->driver_name = drivername;
497 dev_info->if_index = internals->if_index;
498 dev_info->max_mac_addrs = 1;
499 dev_info->max_rx_pktlen = (uint32_t) -1;
500 dev_info->max_rx_queues = dev->data->nb_rx_queues;
501 dev_info->max_tx_queues = dev->data->nb_tx_queues;
502 dev_info->min_rx_bufsize = 0;
503 dev_info->pci_dev = NULL;
507 eth_stats_get(struct rte_eth_dev *dev,
508 struct rte_eth_stats *stats)
511 unsigned long rx_packets_total = 0, rx_bytes_total = 0;
512 unsigned long tx_packets_total = 0, tx_bytes_total = 0;
513 unsigned long tx_packets_err_total = 0;
514 const struct pmd_internals *internal = dev->data->dev_private;
516 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
517 i < dev->data->nb_rx_queues; i++) {
518 stats->q_ipackets[i] = internal->rx_queue[i].rx_stat.pkts;
519 stats->q_ibytes[i] = internal->rx_queue[i].rx_stat.bytes;
520 rx_packets_total += stats->q_ipackets[i];
521 rx_bytes_total += stats->q_ibytes[i];
524 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
525 i < dev->data->nb_tx_queues; i++) {
526 stats->q_opackets[i] = internal->tx_queue[i].tx_stat.pkts;
527 stats->q_obytes[i] = internal->tx_queue[i].tx_stat.bytes;
528 stats->q_errors[i] = internal->tx_queue[i].tx_stat.err_pkts;
529 tx_packets_total += stats->q_opackets[i];
530 tx_bytes_total += stats->q_obytes[i];
531 tx_packets_err_total += stats->q_errors[i];
534 stats->ipackets = rx_packets_total;
535 stats->ibytes = rx_bytes_total;
536 stats->opackets = tx_packets_total;
537 stats->obytes = tx_bytes_total;
538 stats->oerrors = tx_packets_err_total;
542 eth_stats_reset(struct rte_eth_dev *dev)
545 struct pmd_internals *internal = dev->data->dev_private;
546 for (i = 0; i < dev->data->nb_rx_queues; i++) {
547 internal->rx_queue[i].rx_stat.pkts = 0;
548 internal->rx_queue[i].rx_stat.bytes = 0;
550 for (i = 0; i < dev->data->nb_tx_queues; i++) {
551 internal->tx_queue[i].tx_stat.pkts = 0;
552 internal->tx_queue[i].tx_stat.bytes = 0;
553 internal->tx_queue[i].tx_stat.err_pkts = 0;
558 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
563 eth_queue_release(void *q __rte_unused)
568 eth_link_update(struct rte_eth_dev *dev __rte_unused,
569 int wait_to_complete __rte_unused)
575 eth_rx_queue_setup(struct rte_eth_dev *dev,
576 uint16_t rx_queue_id,
577 uint16_t nb_rx_desc __rte_unused,
578 unsigned int socket_id __rte_unused,
579 const struct rte_eth_rxconf *rx_conf __rte_unused,
580 struct rte_mempool *mb_pool)
582 struct pmd_internals *internals = dev->data->dev_private;
583 struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
584 pcap_q->mb_pool = mb_pool;
585 dev->data->rx_queues[rx_queue_id] = pcap_q;
586 pcap_q->in_port = dev->data->port_id;
591 eth_tx_queue_setup(struct rte_eth_dev *dev,
592 uint16_t tx_queue_id,
593 uint16_t nb_tx_desc __rte_unused,
594 unsigned int socket_id __rte_unused,
595 const struct rte_eth_txconf *tx_conf __rte_unused)
598 struct pmd_internals *internals = dev->data->dev_private;
599 dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
603 static const struct eth_dev_ops ops = {
604 .dev_start = eth_dev_start,
605 .dev_stop = eth_dev_stop,
606 .dev_close = eth_dev_close,
607 .dev_configure = eth_dev_configure,
608 .dev_infos_get = eth_dev_info,
609 .rx_queue_setup = eth_rx_queue_setup,
610 .tx_queue_setup = eth_tx_queue_setup,
611 .rx_queue_release = eth_queue_release,
612 .tx_queue_release = eth_queue_release,
613 .link_update = eth_link_update,
614 .stats_get = eth_stats_get,
615 .stats_reset = eth_stats_reset,
619 * Function handler that opens the pcap file for reading a stores a
620 * reference of it for use it later on.
623 open_rx_pcap(const char *key, const char *value, void *extra_args)
626 const char *pcap_filename = value;
627 struct pmd_devargs *rx = extra_args;
630 for (i = 0; i < rx->num_of_queue; i++) {
631 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
634 rx->queue[i].pcap = pcap;
635 rx->queue[i].name = pcap_filename;
636 rx->queue[i].type = key;
643 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
645 if ((*pcap = pcap_open_offline(pcap_filename, errbuf)) == NULL) {
646 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename, errbuf);
653 * Opens a pcap file for writing and stores a reference to it
654 * for use it later on.
657 open_tx_pcap(const char *key, const char *value, void *extra_args)
660 const char *pcap_filename = value;
661 struct pmd_devargs *dumpers = extra_args;
662 pcap_dumper_t *dumper;
664 for (i = 0; i < dumpers->num_of_queue; i++) {
665 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
668 dumpers->queue[i].dumper = dumper;
669 dumpers->queue[i].name = pcap_filename;
670 dumpers->queue[i].type = key;
677 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
681 * We need to create a dummy empty pcap_t to use it
682 * with pcap_dump_open(). We create big enough an Ethernet
686 if ((tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN))
688 RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
692 /* The dumper is created using the previous pcap_t reference */
693 if ((*dumper = pcap_dump_open(tx_pcap, pcap_filename)) == NULL) {
694 RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n", pcap_filename);
702 * pcap_open_live wrapper function
705 open_iface_live(const char *iface, pcap_t **pcap) {
706 *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
707 RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
710 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
717 * Opens an interface for reading and writing
720 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
722 const char *iface = value;
723 struct pmd_devargs *tx = extra_args;
726 if (open_single_iface(iface, &pcap) < 0)
729 tx->queue[0].pcap = pcap;
730 tx->queue[0].name = iface;
731 tx->queue[0].type = key;
737 * Opens a NIC for reading packets from it
740 open_rx_iface(const char *key, const char *value, void *extra_args)
743 const char *iface = value;
744 struct pmd_devargs *rx = extra_args;
747 for (i = 0; i < rx->num_of_queue; i++) {
748 if (open_single_iface(iface, &pcap) < 0)
750 rx->queue[i].pcap = pcap;
751 rx->queue[i].name = iface;
752 rx->queue[i].type = key;
759 * Opens a NIC for writing packets to it
762 open_tx_iface(const char *key, const char *value, void *extra_args)
765 const char *iface = value;
766 struct pmd_devargs *tx = extra_args;
769 for (i = 0; i < tx->num_of_queue; i++) {
770 if (open_single_iface(iface, &pcap) < 0)
772 tx->queue[i].pcap = pcap;
773 tx->queue[i].name = iface;
774 tx->queue[i].type = key;
781 open_single_iface(const char *iface, pcap_t **pcap)
783 if (open_iface_live(iface, pcap) < 0) {
784 RTE_LOG(ERR, PMD, "Couldn't open interface %s\n", iface);
792 rte_pmd_init_internals(const char *name, const unsigned nb_rx_queues,
793 const unsigned nb_tx_queues, struct pmd_internals **internals,
794 struct rte_eth_dev **eth_dev)
796 struct rte_eth_dev_data *data = NULL;
797 unsigned int numa_node = rte_socket_id();
799 RTE_LOG(INFO, PMD, "Creating pcap-backed ethdev on numa socket %u\n",
802 /* now do all data allocation - for eth_dev structure
803 * and internal (private) data
805 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
809 *internals = rte_zmalloc_socket(name, sizeof(**internals), 0,
811 if (*internals == NULL)
814 /* reserve an ethdev entry */
815 *eth_dev = rte_eth_dev_allocate(name);
816 if (*eth_dev == NULL)
819 /* check length of device name */
820 if ((strlen((*eth_dev)->data->name) + 1) > sizeof(data->name))
823 /* now put it all together
824 * - store queue data in internals,
825 * - store numa_node info in eth_dev
826 * - point eth_dev_data to internals
827 * - and point eth_dev structure to new eth_dev_data structure
829 data->dev_private = *internals;
830 data->port_id = (*eth_dev)->data->port_id;
831 snprintf(data->name, sizeof(data->name), "%s", (*eth_dev)->data->name);
832 data->nb_rx_queues = (uint16_t)nb_rx_queues;
833 data->nb_tx_queues = (uint16_t)nb_tx_queues;
834 data->dev_link = pmd_link;
835 data->mac_addrs = ð_addr;
837 (*eth_dev)->data->name, strlen((*eth_dev)->data->name));
840 * NOTE: we'll replace the data element, of originally allocated
841 * eth_dev so the rings are local per-process
843 (*eth_dev)->data = data;
844 (*eth_dev)->dev_ops = &ops;
845 (*eth_dev)->driver = NULL;
846 data->dev_flags = RTE_ETH_DEV_DETACHABLE;
847 data->kdrv = RTE_KDRV_NONE;
848 data->drv_name = drivername;
849 data->numa_node = numa_node;
855 rte_free(*internals);
861 rte_eth_from_pcaps_common(const char *name, struct pmd_devargs *rx_queues,
862 const unsigned nb_rx_queues, struct pmd_devargs *tx_queues,
863 const unsigned nb_tx_queues, struct rte_kvargs *kvlist,
864 struct pmd_internals **internals, struct rte_eth_dev **eth_dev)
866 struct rte_kvargs_pair *pair = NULL;
870 /* do some parameter checking */
871 if (rx_queues == NULL && nb_rx_queues > 0)
873 if (tx_queues == NULL && nb_tx_queues > 0)
876 if (rte_pmd_init_internals(name, nb_rx_queues, nb_tx_queues, internals,
880 for (i = 0; i < nb_rx_queues; i++) {
881 (*internals)->rx_queue[i].pcap = rx_queues->queue[i].pcap;
882 snprintf((*internals)->rx_queue[i].name,
883 sizeof((*internals)->rx_queue[i].name), "%s",
884 rx_queues->queue[i].name);
885 snprintf((*internals)->rx_queue[i].type,
886 sizeof((*internals)->rx_queue[i].type), "%s",
887 rx_queues->queue[i].type);
889 for (i = 0; i < nb_tx_queues; i++) {
890 (*internals)->tx_queue[i].dumper = tx_queues->queue[i].dumper;
891 snprintf((*internals)->tx_queue[i].name,
892 sizeof((*internals)->tx_queue[i].name), "%s",
893 tx_queues->queue[i].name);
894 snprintf((*internals)->tx_queue[i].type,
895 sizeof((*internals)->tx_queue[i].type), "%s",
896 tx_queues->queue[i].type);
899 for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
900 pair = &kvlist->pairs[k_idx];
901 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
906 (*internals)->if_index = 0;
908 (*internals)->if_index = if_nametoindex(pair->value);
914 rte_eth_from_pcaps(const char *name, struct pmd_devargs *rx_queues,
915 const unsigned nb_rx_queues, struct pmd_devargs *tx_queues,
916 const unsigned nb_tx_queues, struct rte_kvargs *kvlist,
917 int single_iface, unsigned int using_dumpers)
919 struct pmd_internals *internals = NULL;
920 struct rte_eth_dev *eth_dev = NULL;
923 ret = rte_eth_from_pcaps_common(name, rx_queues, nb_rx_queues,
924 tx_queues, nb_tx_queues, kvlist, &internals, ð_dev);
929 /* store wether we are using a single interface for rx/tx or not */
930 internals->single_iface = single_iface;
932 eth_dev->rx_pkt_burst = eth_pcap_rx;
935 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
937 eth_dev->tx_pkt_burst = eth_pcap_tx;
944 rte_pmd_pcap_devinit(const char *name, const char *params)
946 unsigned int is_rx_pcap = 0, is_tx_pcap = 0;
947 struct rte_kvargs *kvlist;
948 struct pmd_devargs pcaps = {0};
949 struct pmd_devargs dumpers = {0};
950 int single_iface = 0;
953 RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
955 gettimeofday(&start_time, NULL);
956 start_cycles = rte_get_timer_cycles();
957 hz = rte_get_timer_hz();
959 kvlist = rte_kvargs_parse(params, valid_arguments);
964 * If iface argument is passed we open the NICs and use them for
967 if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
969 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
970 &open_rx_tx_iface, &pcaps);
974 dumpers.queue[0].pcap = pcaps.queue[0].pcap;
975 dumpers.queue[0].name = pcaps.queue[0].name;
976 dumpers.queue[0].type = pcaps.queue[0].type;
980 ret = rte_eth_from_pcaps(name, &pcaps, 1, &dumpers, 1,
981 kvlist, single_iface, is_tx_pcap);
987 * We check whether we want to open a RX stream from a real NIC or a
990 pcaps.num_of_queue = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG);
991 if (pcaps.num_of_queue)
994 pcaps.num_of_queue = rte_kvargs_count(kvlist,
995 ETH_PCAP_RX_IFACE_ARG);
997 if (pcaps.num_of_queue > RTE_PMD_PCAP_MAX_QUEUES)
998 pcaps.num_of_queue = RTE_PMD_PCAP_MAX_QUEUES;
1001 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
1002 &open_rx_pcap, &pcaps);
1004 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
1005 &open_rx_iface, &pcaps);
1011 * We check whether we want to open a TX stream to a real NIC or a
1014 dumpers.num_of_queue = rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG);
1015 if (dumpers.num_of_queue)
1018 dumpers.num_of_queue = rte_kvargs_count(kvlist,
1019 ETH_PCAP_TX_IFACE_ARG);
1021 if (dumpers.num_of_queue > RTE_PMD_PCAP_MAX_QUEUES)
1022 dumpers.num_of_queue = RTE_PMD_PCAP_MAX_QUEUES;
1025 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1026 &open_tx_pcap, &dumpers);
1028 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1029 &open_tx_iface, &dumpers);
1034 ret = rte_eth_from_pcaps(name, &pcaps, pcaps.num_of_queue, &dumpers,
1035 dumpers.num_of_queue, kvlist, single_iface, is_tx_pcap);
1038 rte_kvargs_free(kvlist);
1043 rte_pmd_pcap_devuninit(const char *name)
1045 struct rte_eth_dev *eth_dev = NULL;
1047 RTE_LOG(INFO, PMD, "Closing pcap ethdev on numa socket %u\n",
1053 /* reserve an ethdev entry */
1054 eth_dev = rte_eth_dev_allocated(name);
1055 if (eth_dev == NULL)
1058 rte_free(eth_dev->data->dev_private);
1059 rte_free(eth_dev->data);
1061 rte_eth_dev_release_port(eth_dev);
1066 static struct rte_vdev_driver pmd_pcap_drv = {
1067 .init = rte_pmd_pcap_devinit,
1068 .uninit = rte_pmd_pcap_devuninit,
1071 DRIVER_REGISTER_VDEV(net_pcap, pmd_pcap_drv);
1072 DRIVER_REGISTER_PARAM_STRING(net_pcap,
1073 ETH_PCAP_RX_PCAP_ARG "=<string> "
1074 ETH_PCAP_TX_PCAP_ARG "=<string> "
1075 ETH_PCAP_RX_IFACE_ARG "=<ifc> "
1076 ETH_PCAP_TX_IFACE_ARG "=<ifc> "
1077 ETH_PCAP_IFACE_ARG "=<ifc>");