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;
69 struct pcap_rx_queue {
72 struct rte_mempool *mb_pool;
73 volatile unsigned long rx_pkts;
74 volatile unsigned long rx_bytes;
75 volatile unsigned long err_pkts;
77 char type[ETH_PCAP_ARG_MAXLEN];
80 struct pcap_tx_queue {
81 pcap_dumper_t *dumper;
83 volatile unsigned long tx_pkts;
84 volatile unsigned long tx_bytes;
85 volatile unsigned long err_pkts;
87 char type[ETH_PCAP_ARG_MAXLEN];
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];
98 unsigned num_of_queue;
99 struct devargs_queue {
100 pcap_dumper_t *dumper;
104 } queue[RTE_PMD_PCAP_MAX_QUEUES];
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,
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);
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,
130 eth_pcap_rx_jumbo(struct rte_mempool *mb_pool,
131 struct rte_mbuf *mbuf,
135 struct rte_mbuf *m = mbuf;
137 /* Copy the first segment. */
138 uint16_t len = rte_pktmbuf_tailroom(mbuf);
140 rte_memcpy(rte_pktmbuf_append(mbuf, len), data, len);
144 while (data_len > 0) {
145 /* Allocate next mbuf and point to that. */
146 m->next = rte_pktmbuf_alloc(mb_pool);
148 if (unlikely(!m->next))
153 /* Headroom is not needed in chained mbufs. */
154 rte_pktmbuf_prepend(m, rte_pktmbuf_headroom(m));
158 /* Copy next segment. */
159 len = RTE_MIN(rte_pktmbuf_tailroom(m), data_len);
160 rte_memcpy(rte_pktmbuf_append(m, len), data, len);
167 return mbuf->nb_segs;
170 /* Copy data from mbuf chain to a buffer suitable for writing to a PCAP file. */
172 eth_pcap_gather_data(unsigned char *data, struct rte_mbuf *mbuf)
174 uint16_t data_len = 0;
177 rte_memcpy(data + data_len, rte_pktmbuf_mtod(mbuf, void *),
180 data_len += mbuf->data_len;
186 eth_pcap_rx(void *queue,
187 struct rte_mbuf **bufs,
191 struct pcap_pkthdr header;
192 const u_char *packet;
193 struct rte_mbuf *mbuf;
194 struct pcap_rx_queue *pcap_q = queue;
197 uint32_t rx_bytes = 0;
199 if (unlikely(pcap_q->pcap == NULL || nb_pkts == 0))
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.
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))
211 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
212 if (unlikely(mbuf == NULL))
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);
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,
223 mbuf->data_len = (uint16_t)header.caplen;
225 /* Try read jumbo frame into multi mbufs. */
226 if (unlikely(eth_pcap_rx_jumbo(pcap_q->mb_pool,
229 header.caplen) == -1))
233 mbuf->pkt_len = (uint16_t)header.caplen;
234 mbuf->port = pcap_q->in_port;
237 rx_bytes += header.caplen;
239 pcap_q->rx_pkts += num_rx;
240 pcap_q->rx_bytes += rx_bytes;
245 calculate_timestamp(struct timeval *ts) {
247 struct timeval cur_time;
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);
256 * Callback to handle writing packets to a pcap file.
259 eth_pcap_tx_dumper(void *queue,
260 struct rte_mbuf **bufs,
264 struct rte_mbuf *mbuf;
265 struct pcap_tx_queue *dumper_q = queue;
267 uint32_t tx_bytes = 0;
268 struct pcap_pkthdr header;
270 if (dumper_q->dumper == NULL || nb_pkts == 0)
273 /* writes the nb_pkts packets to the previously opened pcap file dumper */
274 for (i = 0; i < nb_pkts; i++) {
276 calculate_timestamp(&header.ts);
277 header.len = mbuf->pkt_len;
278 header.caplen = header.len;
280 if (likely(mbuf->nb_segs == 1)) {
281 pcap_dump((u_char *)dumper_q->dumper, &header,
282 rte_pktmbuf_mtod(mbuf, void*));
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,
290 "Dropping PCAP packet. "
291 "Size (%d) > max jumbo size (%d).\n",
293 ETHER_MAX_JUMBO_FRAME_LEN);
295 rte_pktmbuf_free(mbuf);
300 rte_pktmbuf_free(mbuf);
302 tx_bytes += mbuf->pkt_len;
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.
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;
318 * Callback to handle sending packets through a real NIC.
321 eth_pcap_tx(void *queue,
322 struct rte_mbuf **bufs,
327 struct rte_mbuf *mbuf;
328 struct pcap_tx_queue *tx_queue = queue;
330 uint32_t tx_bytes = 0;
332 if (unlikely(nb_pkts == 0 || tx_queue->pcap == NULL))
335 for (i = 0; i < nb_pkts; i++) {
338 if (likely(mbuf->nb_segs == 1)) {
339 ret = pcap_sendpacket(tx_queue->pcap,
340 rte_pktmbuf_mtod(mbuf, u_char *),
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,
350 "Dropping PCAP packet. "
351 "Size (%d) > max jumbo size (%d).\n",
353 ETHER_MAX_JUMBO_FRAME_LEN);
355 rte_pktmbuf_free(mbuf);
360 if (unlikely(ret != 0))
363 tx_bytes += mbuf->pkt_len;
364 rte_pktmbuf_free(mbuf);
367 tx_queue->tx_pkts += num_tx;
368 tx_queue->tx_bytes += tx_bytes;
369 tx_queue->err_pkts += nb_pkts - num_tx;
374 eth_dev_start(struct rte_eth_dev *dev)
377 struct pmd_internals *internals = dev->data->dev_private;
378 struct pcap_tx_queue *tx;
379 struct pcap_rx_queue *rx;
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];
386 if (!tx->pcap && strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
387 if (open_single_iface(tx->name, &tx->pcap) < 0)
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];
398 if (!tx->dumper && strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
399 if (open_single_tx_pcap(tx->name, &tx->dumper) < 0)
403 else if (!tx->pcap && strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
404 if (open_single_iface(tx->name, &tx->pcap) < 0)
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];
413 if (rx->pcap != NULL)
416 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
417 if (open_single_rx_pcap(rx->name, &rx->pcap) < 0)
421 else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
422 if (open_single_iface(rx->name, &rx->pcap) < 0)
429 dev->data->dev_link.link_status = ETH_LINK_UP;
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.
439 eth_dev_stop(struct rte_eth_dev *dev)
442 struct pmd_internals *internals = dev->data->dev_private;
443 struct pcap_tx_queue *tx;
444 struct pcap_rx_queue *rx;
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);
456 for (i = 0; i < dev->data->nb_tx_queues; i++) {
457 tx = &internals->tx_queue[i];
459 if (tx->dumper != NULL) {
460 pcap_dump_close(tx->dumper);
464 if (tx->pcap != NULL) {
465 pcap_close(tx->pcap);
470 for (i = 0; i < dev->data->nb_rx_queues; i++) {
471 rx = &internals->rx_queue[i];
473 if (rx->pcap != NULL) {
474 pcap_close(rx->pcap);
480 dev->data->dev_link.link_status = ETH_LINK_DOWN;
484 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
490 eth_dev_info(struct rte_eth_dev *dev,
491 struct rte_eth_dev_info *dev_info)
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;
505 eth_stats_get(struct rte_eth_dev *dev,
506 struct rte_eth_stats *igb_stats)
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;
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];
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];
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;
540 eth_stats_reset(struct rte_eth_dev *dev)
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;
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;
556 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
561 eth_queue_release(void *q __rte_unused)
566 eth_link_update(struct rte_eth_dev *dev __rte_unused,
567 int wait_to_complete __rte_unused)
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)
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;
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)
596 struct pmd_internals *internals = dev->data->dev_private;
597 dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
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,
617 * Function handler that opens the pcap file for reading a stores a
618 * reference of it for use it later on.
621 open_rx_pcap(const char *key, const char *value, void *extra_args)
624 const char *pcap_filename = value;
625 struct pmd_devargs *rx = extra_args;
628 for (i = 0; i < rx->num_of_queue; i++) {
629 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
632 rx->queue[i].pcap = pcap;
633 rx->queue[i].name = pcap_filename;
634 rx->queue[i].type = key;
641 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
643 if ((*pcap = pcap_open_offline(pcap_filename, errbuf)) == NULL) {
644 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename, errbuf);
651 * Opens a pcap file for writing and stores a reference to it
652 * for use it later on.
655 open_tx_pcap(const char *key, const char *value, void *extra_args)
658 const char *pcap_filename = value;
659 struct pmd_devargs *dumpers = extra_args;
660 pcap_dumper_t *dumper;
662 for (i = 0; i < dumpers->num_of_queue; i++) {
663 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
666 dumpers->queue[i].dumper = dumper;
667 dumpers->queue[i].name = pcap_filename;
668 dumpers->queue[i].type = key;
675 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
679 * We need to create a dummy empty pcap_t to use it
680 * with pcap_dump_open(). We create big enough an Ethernet
684 if ((tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN))
686 RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
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);
700 * pcap_open_live wrapper function
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);
708 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
715 * Opens an interface for reading and writing
718 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
720 const char *iface = value;
721 struct pmd_devargs *tx = extra_args;
724 if (open_single_iface(iface, &pcap) < 0)
727 tx->queue[0].pcap = pcap;
728 tx->queue[0].name = iface;
729 tx->queue[0].type = key;
735 * Opens a NIC for reading packets from it
738 open_rx_iface(const char *key, const char *value, void *extra_args)
741 const char *iface = value;
742 struct pmd_devargs *rx = extra_args;
745 for (i = 0; i < rx->num_of_queue; i++) {
746 if (open_single_iface(iface, &pcap) < 0)
748 rx->queue[i].pcap = pcap;
749 rx->queue[i].name = iface;
750 rx->queue[i].type = key;
757 * Opens a NIC for writing packets to it
760 open_tx_iface(const char *key, const char *value, void *extra_args)
763 const char *iface = value;
764 struct pmd_devargs *tx = extra_args;
767 for (i = 0; i < tx->num_of_queue; i++) {
768 if (open_single_iface(iface, &pcap) < 0)
770 tx->queue[i].pcap = pcap;
771 tx->queue[i].name = iface;
772 tx->queue[i].type = key;
779 open_single_iface(const char *iface, pcap_t **pcap)
781 if (open_iface_live(iface, pcap) < 0) {
782 RTE_LOG(ERR, PMD, "Couldn't open interface %s\n", iface);
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)
794 struct rte_eth_dev_data *data = NULL;
795 unsigned int numa_node = rte_socket_id();
797 RTE_LOG(INFO, PMD, "Creating pcap-backed ethdev on numa socket %u\n",
800 /* now do all data allocation - for eth_dev structure
801 * and internal (private) data
803 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
807 *internals = rte_zmalloc_socket(name, sizeof(**internals), 0,
809 if (*internals == NULL)
812 /* reserve an ethdev entry */
813 *eth_dev = rte_eth_dev_allocate(name);
814 if (*eth_dev == NULL)
817 /* check length of device name */
818 if ((strlen((*eth_dev)->data->name) + 1) > sizeof(data->name))
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
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 = ð_addr;
835 (*eth_dev)->data->name, strlen((*eth_dev)->data->name));
838 * NOTE: we'll replace the data element, of originally allocated
839 * eth_dev so the rings are local per-process
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;
853 rte_free(*internals);
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)
864 struct rte_kvargs_pair *pair = NULL;
868 /* do some parameter checking */
869 if (rx_queues == NULL && nb_rx_queues > 0)
871 if (tx_queues == NULL && nb_tx_queues > 0)
874 if (rte_pmd_init_internals(name, nb_rx_queues, nb_tx_queues, internals,
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);
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);
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)
904 (*internals)->if_index = 0;
906 (*internals)->if_index = if_nametoindex(pair->value);
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)
917 struct pmd_internals *internals = NULL;
918 struct rte_eth_dev *eth_dev = NULL;
921 ret = rte_eth_from_pcaps_common(name, rx_queues, nb_rx_queues,
922 tx_queues, nb_tx_queues, kvlist, &internals, ð_dev);
927 /* store wether we are using a single interface for rx/tx or not */
928 internals->single_iface = single_iface;
930 eth_dev->rx_pkt_burst = eth_pcap_rx;
933 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
935 eth_dev->tx_pkt_burst = eth_pcap_tx;
942 rte_pmd_pcap_devinit(const char *name, const char *params)
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 single_iface = 0;
951 RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
953 gettimeofday(&start_time, NULL);
954 start_cycles = rte_get_timer_cycles();
955 hz = rte_get_timer_hz();
957 kvlist = rte_kvargs_parse(params, valid_arguments);
962 * If iface argument is passed we open the NICs and use them for
965 if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
967 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
968 &open_rx_tx_iface, &pcaps);
972 dumpers.queue[0].pcap = pcaps.queue[0].pcap;
973 dumpers.queue[0].name = pcaps.queue[0].name;
974 dumpers.queue[0].type = pcaps.queue[0].type;
978 ret = rte_eth_from_pcaps(name, &pcaps, 1, &dumpers, 1,
979 kvlist, single_iface, is_tx_pcap);
985 * We check whether we want to open a RX stream from a real NIC or a
988 pcaps.num_of_queue = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG);
989 if (pcaps.num_of_queue)
992 pcaps.num_of_queue = rte_kvargs_count(kvlist,
993 ETH_PCAP_RX_IFACE_ARG);
995 if (pcaps.num_of_queue > RTE_PMD_PCAP_MAX_QUEUES)
996 pcaps.num_of_queue = RTE_PMD_PCAP_MAX_QUEUES;
999 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
1000 &open_rx_pcap, &pcaps);
1002 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
1003 &open_rx_iface, &pcaps);
1009 * We check whether we want to open a TX stream to a real NIC or a
1012 dumpers.num_of_queue = rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG);
1013 if (dumpers.num_of_queue)
1016 dumpers.num_of_queue = rte_kvargs_count(kvlist,
1017 ETH_PCAP_TX_IFACE_ARG);
1019 if (dumpers.num_of_queue > RTE_PMD_PCAP_MAX_QUEUES)
1020 dumpers.num_of_queue = RTE_PMD_PCAP_MAX_QUEUES;
1023 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1024 &open_tx_pcap, &dumpers);
1026 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1027 &open_tx_iface, &dumpers);
1032 ret = rte_eth_from_pcaps(name, &pcaps, pcaps.num_of_queue, &dumpers,
1033 dumpers.num_of_queue, kvlist, single_iface, is_tx_pcap);
1036 rte_kvargs_free(kvlist);
1041 rte_pmd_pcap_devuninit(const char *name)
1043 struct rte_eth_dev *eth_dev = NULL;
1045 RTE_LOG(INFO, PMD, "Closing pcap ethdev on numa socket %u\n",
1051 /* reserve an ethdev entry */
1052 eth_dev = rte_eth_dev_allocated(name);
1053 if (eth_dev == NULL)
1056 rte_free(eth_dev->data->dev_private);
1057 rte_free(eth_dev->data);
1059 rte_eth_dev_release_port(eth_dev);
1064 static struct rte_vdev_driver pmd_pcap_drv = {
1065 .init = rte_pmd_pcap_devinit,
1066 .uninit = rte_pmd_pcap_devuninit,
1069 DRIVER_REGISTER_VDEV(net_pcap, pmd_pcap_drv);
1070 DRIVER_REGISTER_PARAM_STRING(net_pcap,
1071 ETH_PCAP_RX_PCAP_ARG "=<string> "
1072 ETH_PCAP_TX_PCAP_ARG "=<string> "
1073 ETH_PCAP_RX_IFACE_ARG "=<ifc> "
1074 ETH_PCAP_TX_IFACE_ARG "=<ifc> "
1075 ETH_PCAP_IFACE_ARG "=<ifc>");