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.
41 #include <rte_cycles.h>
42 #include <rte_ethdev.h>
43 #include <rte_ethdev_vdev.h>
44 #include <rte_kvargs.h>
45 #include <rte_malloc.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
54 #define ETH_PCAP_RX_PCAP_ARG "rx_pcap"
55 #define ETH_PCAP_TX_PCAP_ARG "tx_pcap"
56 #define ETH_PCAP_RX_IFACE_ARG "rx_iface"
57 #define ETH_PCAP_TX_IFACE_ARG "tx_iface"
58 #define ETH_PCAP_IFACE_ARG "iface"
60 #define ETH_PCAP_ARG_MAXLEN 64
62 #define RTE_PMD_PCAP_MAX_QUEUES 16
64 static char errbuf[PCAP_ERRBUF_SIZE];
65 static unsigned char tx_pcap_data[RTE_ETH_PCAP_SNAPLEN];
66 static struct timeval start_time;
67 static uint64_t start_cycles;
71 volatile unsigned long pkts;
72 volatile unsigned long bytes;
73 volatile unsigned long err_pkts;
76 struct pcap_rx_queue {
79 struct rte_mempool *mb_pool;
80 struct queue_stat rx_stat;
82 char type[ETH_PCAP_ARG_MAXLEN];
85 struct pcap_tx_queue {
86 pcap_dumper_t *dumper;
88 struct queue_stat tx_stat;
90 char type[ETH_PCAP_ARG_MAXLEN];
93 struct pmd_internals {
94 struct pcap_rx_queue rx_queue[RTE_PMD_PCAP_MAX_QUEUES];
95 struct pcap_tx_queue tx_queue[RTE_PMD_PCAP_MAX_QUEUES];
101 unsigned int num_of_queue;
102 struct devargs_queue {
103 pcap_dumper_t *dumper;
107 } queue[RTE_PMD_PCAP_MAX_QUEUES];
110 static const char *valid_arguments[] = {
111 ETH_PCAP_RX_PCAP_ARG,
112 ETH_PCAP_TX_PCAP_ARG,
113 ETH_PCAP_RX_IFACE_ARG,
114 ETH_PCAP_TX_IFACE_ARG,
119 static struct ether_addr eth_addr = {
120 .addr_bytes = { 0, 0, 0, 0x1, 0x2, 0x3 }
123 static struct rte_eth_link pmd_link = {
124 .link_speed = ETH_SPEED_NUM_10G,
125 .link_duplex = ETH_LINK_FULL_DUPLEX,
126 .link_status = ETH_LINK_DOWN,
127 .link_autoneg = ETH_LINK_SPEED_FIXED,
131 eth_pcap_rx_jumbo(struct rte_mempool *mb_pool, struct rte_mbuf *mbuf,
132 const u_char *data, uint16_t data_len)
134 /* Copy the first segment. */
135 uint16_t len = rte_pktmbuf_tailroom(mbuf);
136 struct rte_mbuf *m = mbuf;
138 rte_memcpy(rte_pktmbuf_append(mbuf, len), data, len);
142 while (data_len > 0) {
143 /* Allocate next mbuf and point to that. */
144 m->next = rte_pktmbuf_alloc(mb_pool);
146 if (unlikely(!m->next))
151 /* Headroom is not needed in chained mbufs. */
152 rte_pktmbuf_prepend(m, rte_pktmbuf_headroom(m));
156 /* Copy next segment. */
157 len = RTE_MIN(rte_pktmbuf_tailroom(m), data_len);
158 rte_memcpy(rte_pktmbuf_append(m, len), data, len);
165 return mbuf->nb_segs;
168 /* Copy data from mbuf chain to a buffer suitable for writing to a PCAP file. */
170 eth_pcap_gather_data(unsigned char *data, struct rte_mbuf *mbuf)
172 uint16_t data_len = 0;
175 rte_memcpy(data + data_len, rte_pktmbuf_mtod(mbuf, void *),
178 data_len += mbuf->data_len;
184 eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
187 struct pcap_pkthdr header;
188 const u_char *packet;
189 struct rte_mbuf *mbuf;
190 struct pcap_rx_queue *pcap_q = queue;
193 uint32_t rx_bytes = 0;
195 if (unlikely(pcap_q->pcap == NULL || nb_pkts == 0))
198 /* Reads the given number of packets from the pcap file one by one
199 * and copies the packet data into a newly allocated mbuf to return.
201 for (i = 0; i < nb_pkts; i++) {
202 /* Get the next PCAP packet */
203 packet = pcap_next(pcap_q->pcap, &header);
204 if (unlikely(packet == NULL))
207 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
208 if (unlikely(mbuf == NULL))
211 /* Now get the space available for data in the mbuf */
212 buf_size = rte_pktmbuf_data_room_size(pcap_q->mb_pool) -
213 RTE_PKTMBUF_HEADROOM;
215 if (header.caplen <= buf_size) {
216 /* pcap packet will fit in the mbuf, can copy it */
217 rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), packet,
219 mbuf->data_len = (uint16_t)header.caplen;
221 /* Try read jumbo frame into multi mbufs. */
222 if (unlikely(eth_pcap_rx_jumbo(pcap_q->mb_pool,
225 header.caplen) == -1)) {
226 rte_pktmbuf_free(mbuf);
231 mbuf->pkt_len = (uint16_t)header.caplen;
232 mbuf->port = pcap_q->in_port;
235 rx_bytes += header.caplen;
237 pcap_q->rx_stat.pkts += num_rx;
238 pcap_q->rx_stat.bytes += rx_bytes;
244 calculate_timestamp(struct timeval *ts) {
246 struct timeval cur_time;
248 cycles = rte_get_timer_cycles() - start_cycles;
249 cur_time.tv_sec = cycles / hz;
250 cur_time.tv_usec = (cycles % hz) * 1e6 / hz;
251 timeradd(&start_time, &cur_time, ts);
255 * Callback to handle writing packets to a pcap file.
258 eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
261 struct rte_mbuf *mbuf;
262 struct pcap_tx_queue *dumper_q = queue;
264 uint32_t tx_bytes = 0;
265 struct pcap_pkthdr header;
267 if (dumper_q->dumper == NULL || nb_pkts == 0)
270 /* writes the nb_pkts packets to the previously opened pcap file
272 for (i = 0; i < nb_pkts; i++) {
274 calculate_timestamp(&header.ts);
275 header.len = mbuf->pkt_len;
276 header.caplen = header.len;
278 if (likely(mbuf->nb_segs == 1)) {
279 pcap_dump((u_char *)dumper_q->dumper, &header,
280 rte_pktmbuf_mtod(mbuf, void*));
282 if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
283 eth_pcap_gather_data(tx_pcap_data, mbuf);
284 pcap_dump((u_char *)dumper_q->dumper, &header,
288 "Dropping PCAP packet. Size (%d) > max jumbo size (%d).\n",
290 ETHER_MAX_JUMBO_FRAME_LEN);
292 rte_pktmbuf_free(mbuf);
298 tx_bytes += mbuf->pkt_len;
299 rte_pktmbuf_free(mbuf);
303 * Since there's no place to hook a callback when the forwarding
304 * process stops and to make sure the pcap file is actually written,
305 * we flush the pcap dumper within each burst.
307 pcap_dump_flush(dumper_q->dumper);
308 dumper_q->tx_stat.pkts += num_tx;
309 dumper_q->tx_stat.bytes += tx_bytes;
310 dumper_q->tx_stat.err_pkts += nb_pkts - num_tx;
316 * Callback to handle sending packets through a real NIC.
319 eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
323 struct rte_mbuf *mbuf;
324 struct pcap_tx_queue *tx_queue = queue;
326 uint32_t tx_bytes = 0;
328 if (unlikely(nb_pkts == 0 || tx_queue->pcap == NULL))
331 for (i = 0; i < nb_pkts; i++) {
334 if (likely(mbuf->nb_segs == 1)) {
335 ret = pcap_sendpacket(tx_queue->pcap,
336 rte_pktmbuf_mtod(mbuf, u_char *),
339 if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
340 eth_pcap_gather_data(tx_pcap_data, mbuf);
341 ret = pcap_sendpacket(tx_queue->pcap,
342 tx_pcap_data, mbuf->pkt_len);
345 "Dropping PCAP packet. Size (%d) > max jumbo size (%d).\n",
347 ETHER_MAX_JUMBO_FRAME_LEN);
349 rte_pktmbuf_free(mbuf);
354 if (unlikely(ret != 0))
357 tx_bytes += mbuf->pkt_len;
358 rte_pktmbuf_free(mbuf);
361 tx_queue->tx_stat.pkts += num_tx;
362 tx_queue->tx_stat.bytes += tx_bytes;
363 tx_queue->tx_stat.err_pkts += nb_pkts - num_tx;
369 * pcap_open_live wrapper function
372 open_iface_live(const char *iface, pcap_t **pcap) {
373 *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
374 RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
377 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
385 open_single_iface(const char *iface, pcap_t **pcap)
387 if (open_iface_live(iface, pcap) < 0) {
388 RTE_LOG(ERR, PMD, "Couldn't open interface %s\n", iface);
396 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
401 * We need to create a dummy empty pcap_t to use it
402 * with pcap_dump_open(). We create big enough an Ethernet
405 tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN);
406 if (tx_pcap == NULL) {
407 RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
411 /* The dumper is created using the previous pcap_t reference */
412 *dumper = pcap_dump_open(tx_pcap, pcap_filename);
413 if (*dumper == NULL) {
414 RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n",
423 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
425 *pcap = pcap_open_offline(pcap_filename, errbuf);
427 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename,
436 eth_dev_start(struct rte_eth_dev *dev)
439 struct pmd_internals *internals = dev->data->dev_private;
440 struct pcap_tx_queue *tx;
441 struct pcap_rx_queue *rx;
443 /* Special iface case. Single pcap is open and shared between tx/rx. */
444 if (internals->single_iface) {
445 tx = &internals->tx_queue[0];
446 rx = &internals->rx_queue[0];
448 if (!tx->pcap && strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
449 if (open_single_iface(tx->name, &tx->pcap) < 0)
456 /* If not open already, open tx pcaps/dumpers */
457 for (i = 0; i < dev->data->nb_tx_queues; i++) {
458 tx = &internals->tx_queue[i];
461 strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
462 if (open_single_tx_pcap(tx->name, &tx->dumper) < 0)
464 } else if (!tx->pcap &&
465 strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
466 if (open_single_iface(tx->name, &tx->pcap) < 0)
471 /* If not open already, open rx pcaps */
472 for (i = 0; i < dev->data->nb_rx_queues; i++) {
473 rx = &internals->rx_queue[i];
475 if (rx->pcap != NULL)
478 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
479 if (open_single_rx_pcap(rx->name, &rx->pcap) < 0)
481 } else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
482 if (open_single_iface(rx->name, &rx->pcap) < 0)
488 dev->data->dev_link.link_status = ETH_LINK_UP;
494 * This function gets called when the current port gets stopped.
495 * Is the only place for us to close all the tx streams dumpers.
496 * If not called the dumpers will be flushed within each tx burst.
499 eth_dev_stop(struct rte_eth_dev *dev)
502 struct pmd_internals *internals = dev->data->dev_private;
503 struct pcap_tx_queue *tx;
504 struct pcap_rx_queue *rx;
506 /* Special iface case. Single pcap is open and shared between tx/rx. */
507 if (internals->single_iface) {
508 tx = &internals->tx_queue[0];
509 rx = &internals->rx_queue[0];
510 pcap_close(tx->pcap);
516 for (i = 0; i < dev->data->nb_tx_queues; i++) {
517 tx = &internals->tx_queue[i];
519 if (tx->dumper != NULL) {
520 pcap_dump_close(tx->dumper);
524 if (tx->pcap != NULL) {
525 pcap_close(tx->pcap);
530 for (i = 0; i < dev->data->nb_rx_queues; i++) {
531 rx = &internals->rx_queue[i];
533 if (rx->pcap != NULL) {
534 pcap_close(rx->pcap);
540 dev->data->dev_link.link_status = ETH_LINK_DOWN;
544 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
550 eth_dev_info(struct rte_eth_dev *dev,
551 struct rte_eth_dev_info *dev_info)
553 struct pmd_internals *internals = dev->data->dev_private;
555 dev_info->if_index = internals->if_index;
556 dev_info->max_mac_addrs = 1;
557 dev_info->max_rx_pktlen = (uint32_t) -1;
558 dev_info->max_rx_queues = dev->data->nb_rx_queues;
559 dev_info->max_tx_queues = dev->data->nb_tx_queues;
560 dev_info->min_rx_bufsize = 0;
564 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
567 unsigned long rx_packets_total = 0, rx_bytes_total = 0;
568 unsigned long tx_packets_total = 0, tx_bytes_total = 0;
569 unsigned long tx_packets_err_total = 0;
570 const struct pmd_internals *internal = dev->data->dev_private;
572 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
573 i < dev->data->nb_rx_queues; i++) {
574 stats->q_ipackets[i] = internal->rx_queue[i].rx_stat.pkts;
575 stats->q_ibytes[i] = internal->rx_queue[i].rx_stat.bytes;
576 rx_packets_total += stats->q_ipackets[i];
577 rx_bytes_total += stats->q_ibytes[i];
580 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
581 i < dev->data->nb_tx_queues; i++) {
582 stats->q_opackets[i] = internal->tx_queue[i].tx_stat.pkts;
583 stats->q_obytes[i] = internal->tx_queue[i].tx_stat.bytes;
584 stats->q_errors[i] = internal->tx_queue[i].tx_stat.err_pkts;
585 tx_packets_total += stats->q_opackets[i];
586 tx_bytes_total += stats->q_obytes[i];
587 tx_packets_err_total += stats->q_errors[i];
590 stats->ipackets = rx_packets_total;
591 stats->ibytes = rx_bytes_total;
592 stats->opackets = tx_packets_total;
593 stats->obytes = tx_bytes_total;
594 stats->oerrors = tx_packets_err_total;
598 eth_stats_reset(struct rte_eth_dev *dev)
601 struct pmd_internals *internal = dev->data->dev_private;
603 for (i = 0; i < dev->data->nb_rx_queues; i++) {
604 internal->rx_queue[i].rx_stat.pkts = 0;
605 internal->rx_queue[i].rx_stat.bytes = 0;
608 for (i = 0; i < dev->data->nb_tx_queues; i++) {
609 internal->tx_queue[i].tx_stat.pkts = 0;
610 internal->tx_queue[i].tx_stat.bytes = 0;
611 internal->tx_queue[i].tx_stat.err_pkts = 0;
616 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
621 eth_queue_release(void *q __rte_unused)
626 eth_link_update(struct rte_eth_dev *dev __rte_unused,
627 int wait_to_complete __rte_unused)
633 eth_rx_queue_setup(struct rte_eth_dev *dev,
634 uint16_t rx_queue_id,
635 uint16_t nb_rx_desc __rte_unused,
636 unsigned int socket_id __rte_unused,
637 const struct rte_eth_rxconf *rx_conf __rte_unused,
638 struct rte_mempool *mb_pool)
640 struct pmd_internals *internals = dev->data->dev_private;
641 struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
643 pcap_q->mb_pool = mb_pool;
644 dev->data->rx_queues[rx_queue_id] = pcap_q;
645 pcap_q->in_port = dev->data->port_id;
651 eth_tx_queue_setup(struct rte_eth_dev *dev,
652 uint16_t tx_queue_id,
653 uint16_t nb_tx_desc __rte_unused,
654 unsigned int socket_id __rte_unused,
655 const struct rte_eth_txconf *tx_conf __rte_unused)
657 struct pmd_internals *internals = dev->data->dev_private;
659 dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
664 static const struct eth_dev_ops ops = {
665 .dev_start = eth_dev_start,
666 .dev_stop = eth_dev_stop,
667 .dev_close = eth_dev_close,
668 .dev_configure = eth_dev_configure,
669 .dev_infos_get = eth_dev_info,
670 .rx_queue_setup = eth_rx_queue_setup,
671 .tx_queue_setup = eth_tx_queue_setup,
672 .rx_queue_release = eth_queue_release,
673 .tx_queue_release = eth_queue_release,
674 .link_update = eth_link_update,
675 .stats_get = eth_stats_get,
676 .stats_reset = eth_stats_reset,
680 * Function handler that opens the pcap file for reading a stores a
681 * reference of it for use it later on.
684 open_rx_pcap(const char *key, const char *value, void *extra_args)
687 const char *pcap_filename = value;
688 struct pmd_devargs *rx = extra_args;
691 for (i = 0; i < rx->num_of_queue; i++) {
692 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
695 rx->queue[i].pcap = pcap;
696 rx->queue[i].name = pcap_filename;
697 rx->queue[i].type = key;
704 * Opens a pcap file for writing and stores a reference to it
705 * for use it later on.
708 open_tx_pcap(const char *key, const char *value, void *extra_args)
711 const char *pcap_filename = value;
712 struct pmd_devargs *dumpers = extra_args;
713 pcap_dumper_t *dumper;
715 for (i = 0; i < dumpers->num_of_queue; i++) {
716 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
719 dumpers->queue[i].dumper = dumper;
720 dumpers->queue[i].name = pcap_filename;
721 dumpers->queue[i].type = key;
728 * Opens an interface for reading and writing
731 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
733 const char *iface = value;
734 struct pmd_devargs *tx = extra_args;
737 if (open_single_iface(iface, &pcap) < 0)
740 tx->queue[0].pcap = pcap;
741 tx->queue[0].name = iface;
742 tx->queue[0].type = key;
748 * Opens a NIC for reading packets from it
751 open_rx_iface(const char *key, const char *value, void *extra_args)
754 const char *iface = value;
755 struct pmd_devargs *rx = extra_args;
758 for (i = 0; i < rx->num_of_queue; i++) {
759 if (open_single_iface(iface, &pcap) < 0)
761 rx->queue[i].pcap = pcap;
762 rx->queue[i].name = iface;
763 rx->queue[i].type = key;
770 * Opens a NIC for writing packets to it
773 open_tx_iface(const char *key, const char *value, void *extra_args)
776 const char *iface = value;
777 struct pmd_devargs *tx = extra_args;
780 for (i = 0; i < tx->num_of_queue; i++) {
781 if (open_single_iface(iface, &pcap) < 0)
783 tx->queue[i].pcap = pcap;
784 tx->queue[i].name = iface;
785 tx->queue[i].type = key;
791 static struct rte_vdev_driver pmd_pcap_drv;
794 pmd_init_internals(struct rte_vdev_device *vdev,
795 const unsigned int nb_rx_queues,
796 const unsigned int nb_tx_queues,
797 struct pmd_internals **internals,
798 struct rte_eth_dev **eth_dev)
800 struct rte_eth_dev_data *data = NULL;
801 unsigned int numa_node = vdev->device.numa_node;
804 name = rte_vdev_device_name(vdev);
805 RTE_LOG(INFO, PMD, "Creating pcap-backed ethdev on numa socket %u\n",
808 /* now do all data allocation - for eth_dev structure
809 * and internal (private) data
811 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
815 /* reserve an ethdev entry */
816 *eth_dev = rte_eth_vdev_allocate(vdev, sizeof(**internals));
817 if (*eth_dev == NULL) {
822 /* now put it all together
823 * - store queue data in internals,
824 * - store numa_node info in eth_dev
825 * - point eth_dev_data to internals
826 * - and point eth_dev structure to new eth_dev_data structure
828 *internals = (*eth_dev)->data->dev_private;
829 rte_memcpy(data, (*eth_dev)->data, sizeof(*data));
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;
836 * NOTE: we'll replace the data element, of originally allocated
837 * eth_dev so the rings are local per-process
839 (*eth_dev)->data = data;
840 (*eth_dev)->dev_ops = &ops;
841 data->dev_flags = RTE_ETH_DEV_DETACHABLE;
847 eth_from_pcaps_common(struct rte_vdev_device *vdev,
848 struct pmd_devargs *rx_queues, const unsigned int nb_rx_queues,
849 struct pmd_devargs *tx_queues, const unsigned int nb_tx_queues,
850 struct rte_kvargs *kvlist, struct pmd_internals **internals,
851 struct rte_eth_dev **eth_dev)
853 struct rte_kvargs_pair *pair = NULL;
857 /* do some parameter checking */
858 if (rx_queues == NULL && nb_rx_queues > 0)
860 if (tx_queues == NULL && nb_tx_queues > 0)
863 if (pmd_init_internals(vdev, nb_rx_queues, nb_tx_queues, internals,
867 for (i = 0; i < nb_rx_queues; i++) {
868 struct pcap_rx_queue *rx = &(*internals)->rx_queue[i];
869 struct devargs_queue *queue = &rx_queues->queue[i];
871 rx->pcap = queue->pcap;
872 snprintf(rx->name, sizeof(rx->name), "%s", queue->name);
873 snprintf(rx->type, sizeof(rx->type), "%s", queue->type);
876 for (i = 0; i < nb_tx_queues; i++) {
877 struct pcap_tx_queue *tx = &(*internals)->tx_queue[i];
878 struct devargs_queue *queue = &tx_queues->queue[i];
880 tx->dumper = queue->dumper;
881 tx->pcap = queue->pcap;
882 snprintf(tx->name, sizeof(tx->name), "%s", queue->name);
883 snprintf(tx->type, sizeof(tx->type), "%s", queue->type);
886 for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
887 pair = &kvlist->pairs[k_idx];
888 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
893 (*internals)->if_index = 0;
895 (*internals)->if_index = if_nametoindex(pair->value);
901 eth_from_pcaps(struct rte_vdev_device *vdev,
902 struct pmd_devargs *rx_queues, const unsigned int nb_rx_queues,
903 struct pmd_devargs *tx_queues, const unsigned int nb_tx_queues,
904 struct rte_kvargs *kvlist, int single_iface,
905 unsigned int using_dumpers)
907 struct pmd_internals *internals = NULL;
908 struct rte_eth_dev *eth_dev = NULL;
911 ret = eth_from_pcaps_common(vdev, rx_queues, nb_rx_queues,
912 tx_queues, nb_tx_queues, kvlist, &internals, ð_dev);
917 /* store weather we are using a single interface for rx/tx or not */
918 internals->single_iface = single_iface;
920 eth_dev->rx_pkt_burst = eth_pcap_rx;
923 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
925 eth_dev->tx_pkt_burst = eth_pcap_tx;
931 pmd_pcap_probe(struct rte_vdev_device *dev)
934 unsigned int is_rx_pcap = 0, is_tx_pcap = 0;
935 struct rte_kvargs *kvlist;
936 struct pmd_devargs pcaps = {0};
937 struct pmd_devargs dumpers = {0};
938 int single_iface = 0;
941 name = rte_vdev_device_name(dev);
942 RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
944 gettimeofday(&start_time, NULL);
945 start_cycles = rte_get_timer_cycles();
946 hz = rte_get_timer_hz();
948 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
953 * If iface argument is passed we open the NICs and use them for
956 if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
958 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
959 &open_rx_tx_iface, &pcaps);
964 dumpers.queue[0] = pcaps.queue[0];
967 pcaps.num_of_queue = 1;
968 dumpers.num_of_queue = 1;
974 * We check whether we want to open a RX stream from a real NIC or a
977 pcaps.num_of_queue = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG);
978 if (pcaps.num_of_queue)
981 pcaps.num_of_queue = rte_kvargs_count(kvlist,
982 ETH_PCAP_RX_IFACE_ARG);
984 if (pcaps.num_of_queue > RTE_PMD_PCAP_MAX_QUEUES)
985 pcaps.num_of_queue = RTE_PMD_PCAP_MAX_QUEUES;
988 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
989 &open_rx_pcap, &pcaps);
991 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
992 &open_rx_iface, &pcaps);
998 * We check whether we want to open a TX stream to a real NIC or a
1001 dumpers.num_of_queue = rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG);
1002 if (dumpers.num_of_queue)
1005 dumpers.num_of_queue = rte_kvargs_count(kvlist,
1006 ETH_PCAP_TX_IFACE_ARG);
1008 if (dumpers.num_of_queue > RTE_PMD_PCAP_MAX_QUEUES)
1009 dumpers.num_of_queue = RTE_PMD_PCAP_MAX_QUEUES;
1012 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1013 &open_tx_pcap, &dumpers);
1015 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1016 &open_tx_iface, &dumpers);
1022 ret = eth_from_pcaps(dev, &pcaps, pcaps.num_of_queue, &dumpers,
1023 dumpers.num_of_queue, kvlist, single_iface, is_tx_pcap);
1026 rte_kvargs_free(kvlist);
1032 pmd_pcap_remove(struct rte_vdev_device *dev)
1034 struct rte_eth_dev *eth_dev = NULL;
1036 RTE_LOG(INFO, PMD, "Closing pcap ethdev on numa socket %u\n",
1042 /* reserve an ethdev entry */
1043 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1044 if (eth_dev == NULL)
1047 rte_free(eth_dev->data->dev_private);
1048 rte_free(eth_dev->data);
1050 rte_eth_dev_release_port(eth_dev);
1055 static struct rte_vdev_driver pmd_pcap_drv = {
1056 .probe = pmd_pcap_probe,
1057 .remove = pmd_pcap_remove,
1060 RTE_PMD_REGISTER_VDEV(net_pcap, pmd_pcap_drv);
1061 RTE_PMD_REGISTER_ALIAS(net_pcap, eth_pcap);
1062 RTE_PMD_REGISTER_PARAM_STRING(net_pcap,
1063 ETH_PCAP_RX_PCAP_ARG "=<string> "
1064 ETH_PCAP_TX_PCAP_ARG "=<string> "
1065 ETH_PCAP_RX_IFACE_ARG "=<ifc> "
1066 ETH_PCAP_TX_IFACE_ARG "=<ifc> "
1067 ETH_PCAP_IFACE_ARG "=<ifc>");