1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation.
3 * Copyright(c) 2014 6WIND S.A.
13 #include <rte_cycles.h>
14 #include <rte_ethdev_driver.h>
15 #include <rte_ethdev_vdev.h>
16 #include <rte_kvargs.h>
17 #include <rte_malloc.h>
19 #include <rte_bus_vdev.h>
21 #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535
22 #define RTE_ETH_PCAP_SNAPLEN ETHER_MAX_JUMBO_FRAME_LEN
23 #define RTE_ETH_PCAP_PROMISC 1
24 #define RTE_ETH_PCAP_TIMEOUT -1
26 #define ETH_PCAP_RX_PCAP_ARG "rx_pcap"
27 #define ETH_PCAP_TX_PCAP_ARG "tx_pcap"
28 #define ETH_PCAP_RX_IFACE_ARG "rx_iface"
29 #define ETH_PCAP_TX_IFACE_ARG "tx_iface"
30 #define ETH_PCAP_IFACE_ARG "iface"
32 #define ETH_PCAP_ARG_MAXLEN 64
34 #define RTE_PMD_PCAP_MAX_QUEUES 16
36 static char errbuf[PCAP_ERRBUF_SIZE];
37 static unsigned char tx_pcap_data[RTE_ETH_PCAP_SNAPLEN];
38 static struct timeval start_time;
39 static uint64_t start_cycles;
43 volatile unsigned long pkts;
44 volatile unsigned long bytes;
45 volatile unsigned long err_pkts;
48 struct pcap_rx_queue {
51 struct rte_mempool *mb_pool;
52 struct queue_stat rx_stat;
54 char type[ETH_PCAP_ARG_MAXLEN];
57 struct pcap_tx_queue {
58 pcap_dumper_t *dumper;
60 struct queue_stat tx_stat;
62 char type[ETH_PCAP_ARG_MAXLEN];
65 struct pmd_internals {
66 struct pcap_rx_queue rx_queue[RTE_PMD_PCAP_MAX_QUEUES];
67 struct pcap_tx_queue tx_queue[RTE_PMD_PCAP_MAX_QUEUES];
73 unsigned int num_of_queue;
74 struct devargs_queue {
75 pcap_dumper_t *dumper;
79 } queue[RTE_PMD_PCAP_MAX_QUEUES];
82 static const char *valid_arguments[] = {
85 ETH_PCAP_RX_IFACE_ARG,
86 ETH_PCAP_TX_IFACE_ARG,
91 static struct ether_addr eth_addr = {
92 .addr_bytes = { 0, 0, 0, 0x1, 0x2, 0x3 }
95 static struct rte_eth_link pmd_link = {
96 .link_speed = ETH_SPEED_NUM_10G,
97 .link_duplex = ETH_LINK_FULL_DUPLEX,
98 .link_status = ETH_LINK_DOWN,
99 .link_autoneg = ETH_LINK_AUTONEG,
103 eth_pcap_rx_jumbo(struct rte_mempool *mb_pool, struct rte_mbuf *mbuf,
104 const u_char *data, uint16_t data_len)
106 /* Copy the first segment. */
107 uint16_t len = rte_pktmbuf_tailroom(mbuf);
108 struct rte_mbuf *m = mbuf;
110 rte_memcpy(rte_pktmbuf_append(mbuf, len), data, len);
114 while (data_len > 0) {
115 /* Allocate next mbuf and point to that. */
116 m->next = rte_pktmbuf_alloc(mb_pool);
118 if (unlikely(!m->next))
123 /* Headroom is not needed in chained mbufs. */
124 rte_pktmbuf_prepend(m, rte_pktmbuf_headroom(m));
128 /* Copy next segment. */
129 len = RTE_MIN(rte_pktmbuf_tailroom(m), data_len);
130 rte_memcpy(rte_pktmbuf_append(m, len), data, len);
137 return mbuf->nb_segs;
140 /* Copy data from mbuf chain to a buffer suitable for writing to a PCAP file. */
142 eth_pcap_gather_data(unsigned char *data, struct rte_mbuf *mbuf)
144 uint16_t data_len = 0;
147 rte_memcpy(data + data_len, rte_pktmbuf_mtod(mbuf, void *),
150 data_len += mbuf->data_len;
156 eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
159 struct pcap_pkthdr header;
160 const u_char *packet;
161 struct rte_mbuf *mbuf;
162 struct pcap_rx_queue *pcap_q = queue;
165 uint32_t rx_bytes = 0;
167 if (unlikely(pcap_q->pcap == NULL || nb_pkts == 0))
170 /* Reads the given number of packets from the pcap file one by one
171 * and copies the packet data into a newly allocated mbuf to return.
173 for (i = 0; i < nb_pkts; i++) {
174 /* Get the next PCAP packet */
175 packet = pcap_next(pcap_q->pcap, &header);
176 if (unlikely(packet == NULL))
179 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
180 if (unlikely(mbuf == NULL))
183 /* Now get the space available for data in the mbuf */
184 buf_size = rte_pktmbuf_data_room_size(pcap_q->mb_pool) -
185 RTE_PKTMBUF_HEADROOM;
187 if (header.caplen <= buf_size) {
188 /* pcap packet will fit in the mbuf, can copy it */
189 rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), packet,
191 mbuf->data_len = (uint16_t)header.caplen;
193 /* Try read jumbo frame into multi mbufs. */
194 if (unlikely(eth_pcap_rx_jumbo(pcap_q->mb_pool,
197 header.caplen) == -1)) {
198 rte_pktmbuf_free(mbuf);
203 mbuf->pkt_len = (uint16_t)header.caplen;
204 mbuf->port = pcap_q->in_port;
207 rx_bytes += header.caplen;
209 pcap_q->rx_stat.pkts += num_rx;
210 pcap_q->rx_stat.bytes += rx_bytes;
216 calculate_timestamp(struct timeval *ts) {
218 struct timeval cur_time;
220 cycles = rte_get_timer_cycles() - start_cycles;
221 cur_time.tv_sec = cycles / hz;
222 cur_time.tv_usec = (cycles % hz) * 1e6 / hz;
223 timeradd(&start_time, &cur_time, ts);
227 * Callback to handle writing packets to a pcap file.
230 eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
233 struct rte_mbuf *mbuf;
234 struct pcap_tx_queue *dumper_q = queue;
236 uint32_t tx_bytes = 0;
237 struct pcap_pkthdr header;
239 if (dumper_q->dumper == NULL || nb_pkts == 0)
242 /* writes the nb_pkts packets to the previously opened pcap file
244 for (i = 0; i < nb_pkts; i++) {
246 calculate_timestamp(&header.ts);
247 header.len = mbuf->pkt_len;
248 header.caplen = header.len;
250 if (likely(mbuf->nb_segs == 1)) {
251 pcap_dump((u_char *)dumper_q->dumper, &header,
252 rte_pktmbuf_mtod(mbuf, void*));
254 if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
255 eth_pcap_gather_data(tx_pcap_data, mbuf);
256 pcap_dump((u_char *)dumper_q->dumper, &header,
260 "Dropping PCAP packet. Size (%d) > max jumbo size (%d).\n",
262 ETHER_MAX_JUMBO_FRAME_LEN);
264 rte_pktmbuf_free(mbuf);
270 tx_bytes += mbuf->pkt_len;
271 rte_pktmbuf_free(mbuf);
275 * Since there's no place to hook a callback when the forwarding
276 * process stops and to make sure the pcap file is actually written,
277 * we flush the pcap dumper within each burst.
279 pcap_dump_flush(dumper_q->dumper);
280 dumper_q->tx_stat.pkts += num_tx;
281 dumper_q->tx_stat.bytes += tx_bytes;
282 dumper_q->tx_stat.err_pkts += nb_pkts - num_tx;
288 * Callback to handle sending packets through a real NIC.
291 eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
295 struct rte_mbuf *mbuf;
296 struct pcap_tx_queue *tx_queue = queue;
298 uint32_t tx_bytes = 0;
300 if (unlikely(nb_pkts == 0 || tx_queue->pcap == NULL))
303 for (i = 0; i < nb_pkts; i++) {
306 if (likely(mbuf->nb_segs == 1)) {
307 ret = pcap_sendpacket(tx_queue->pcap,
308 rte_pktmbuf_mtod(mbuf, u_char *),
311 if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
312 eth_pcap_gather_data(tx_pcap_data, mbuf);
313 ret = pcap_sendpacket(tx_queue->pcap,
314 tx_pcap_data, mbuf->pkt_len);
317 "Dropping PCAP packet. Size (%d) > max jumbo size (%d).\n",
319 ETHER_MAX_JUMBO_FRAME_LEN);
321 rte_pktmbuf_free(mbuf);
326 if (unlikely(ret != 0))
329 tx_bytes += mbuf->pkt_len;
330 rte_pktmbuf_free(mbuf);
333 tx_queue->tx_stat.pkts += num_tx;
334 tx_queue->tx_stat.bytes += tx_bytes;
335 tx_queue->tx_stat.err_pkts += nb_pkts - num_tx;
341 * pcap_open_live wrapper function
344 open_iface_live(const char *iface, pcap_t **pcap) {
345 *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
346 RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
349 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
357 open_single_iface(const char *iface, pcap_t **pcap)
359 if (open_iface_live(iface, pcap) < 0) {
360 RTE_LOG(ERR, PMD, "Couldn't open interface %s\n", iface);
368 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
373 * We need to create a dummy empty pcap_t to use it
374 * with pcap_dump_open(). We create big enough an Ethernet
377 tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN);
378 if (tx_pcap == NULL) {
379 RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
383 /* The dumper is created using the previous pcap_t reference */
384 *dumper = pcap_dump_open(tx_pcap, pcap_filename);
385 if (*dumper == NULL) {
387 RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n",
397 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
399 *pcap = pcap_open_offline(pcap_filename, errbuf);
401 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename,
410 eth_dev_start(struct rte_eth_dev *dev)
413 struct pmd_internals *internals = dev->data->dev_private;
414 struct pcap_tx_queue *tx;
415 struct pcap_rx_queue *rx;
417 /* Special iface case. Single pcap is open and shared between tx/rx. */
418 if (internals->single_iface) {
419 tx = &internals->tx_queue[0];
420 rx = &internals->rx_queue[0];
422 if (!tx->pcap && strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
423 if (open_single_iface(tx->name, &tx->pcap) < 0)
430 /* If not open already, open tx pcaps/dumpers */
431 for (i = 0; i < dev->data->nb_tx_queues; i++) {
432 tx = &internals->tx_queue[i];
435 strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
436 if (open_single_tx_pcap(tx->name, &tx->dumper) < 0)
438 } else if (!tx->pcap &&
439 strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
440 if (open_single_iface(tx->name, &tx->pcap) < 0)
445 /* If not open already, open rx pcaps */
446 for (i = 0; i < dev->data->nb_rx_queues; i++) {
447 rx = &internals->rx_queue[i];
449 if (rx->pcap != NULL)
452 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
453 if (open_single_rx_pcap(rx->name, &rx->pcap) < 0)
455 } else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
456 if (open_single_iface(rx->name, &rx->pcap) < 0)
462 dev->data->dev_link.link_status = ETH_LINK_UP;
468 * This function gets called when the current port gets stopped.
469 * Is the only place for us to close all the tx streams dumpers.
470 * If not called the dumpers will be flushed within each tx burst.
473 eth_dev_stop(struct rte_eth_dev *dev)
476 struct pmd_internals *internals = dev->data->dev_private;
477 struct pcap_tx_queue *tx;
478 struct pcap_rx_queue *rx;
480 /* Special iface case. Single pcap is open and shared between tx/rx. */
481 if (internals->single_iface) {
482 tx = &internals->tx_queue[0];
483 rx = &internals->rx_queue[0];
484 pcap_close(tx->pcap);
490 for (i = 0; i < dev->data->nb_tx_queues; i++) {
491 tx = &internals->tx_queue[i];
493 if (tx->dumper != NULL) {
494 pcap_dump_close(tx->dumper);
498 if (tx->pcap != NULL) {
499 pcap_close(tx->pcap);
504 for (i = 0; i < dev->data->nb_rx_queues; i++) {
505 rx = &internals->rx_queue[i];
507 if (rx->pcap != NULL) {
508 pcap_close(rx->pcap);
514 dev->data->dev_link.link_status = ETH_LINK_DOWN;
518 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
524 eth_dev_info(struct rte_eth_dev *dev,
525 struct rte_eth_dev_info *dev_info)
527 struct pmd_internals *internals = dev->data->dev_private;
529 dev_info->if_index = internals->if_index;
530 dev_info->max_mac_addrs = 1;
531 dev_info->max_rx_pktlen = (uint32_t) -1;
532 dev_info->max_rx_queues = dev->data->nb_rx_queues;
533 dev_info->max_tx_queues = dev->data->nb_tx_queues;
534 dev_info->min_rx_bufsize = 0;
538 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
541 unsigned long rx_packets_total = 0, rx_bytes_total = 0;
542 unsigned long tx_packets_total = 0, tx_bytes_total = 0;
543 unsigned long tx_packets_err_total = 0;
544 const struct pmd_internals *internal = dev->data->dev_private;
546 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
547 i < dev->data->nb_rx_queues; i++) {
548 stats->q_ipackets[i] = internal->rx_queue[i].rx_stat.pkts;
549 stats->q_ibytes[i] = internal->rx_queue[i].rx_stat.bytes;
550 rx_packets_total += stats->q_ipackets[i];
551 rx_bytes_total += stats->q_ibytes[i];
554 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
555 i < dev->data->nb_tx_queues; i++) {
556 stats->q_opackets[i] = internal->tx_queue[i].tx_stat.pkts;
557 stats->q_obytes[i] = internal->tx_queue[i].tx_stat.bytes;
558 stats->q_errors[i] = internal->tx_queue[i].tx_stat.err_pkts;
559 tx_packets_total += stats->q_opackets[i];
560 tx_bytes_total += stats->q_obytes[i];
561 tx_packets_err_total += stats->q_errors[i];
564 stats->ipackets = rx_packets_total;
565 stats->ibytes = rx_bytes_total;
566 stats->opackets = tx_packets_total;
567 stats->obytes = tx_bytes_total;
568 stats->oerrors = tx_packets_err_total;
574 eth_stats_reset(struct rte_eth_dev *dev)
577 struct pmd_internals *internal = dev->data->dev_private;
579 for (i = 0; i < dev->data->nb_rx_queues; i++) {
580 internal->rx_queue[i].rx_stat.pkts = 0;
581 internal->rx_queue[i].rx_stat.bytes = 0;
584 for (i = 0; i < dev->data->nb_tx_queues; i++) {
585 internal->tx_queue[i].tx_stat.pkts = 0;
586 internal->tx_queue[i].tx_stat.bytes = 0;
587 internal->tx_queue[i].tx_stat.err_pkts = 0;
592 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
597 eth_queue_release(void *q __rte_unused)
602 eth_link_update(struct rte_eth_dev *dev __rte_unused,
603 int wait_to_complete __rte_unused)
609 eth_rx_queue_setup(struct rte_eth_dev *dev,
610 uint16_t rx_queue_id,
611 uint16_t nb_rx_desc __rte_unused,
612 unsigned int socket_id __rte_unused,
613 const struct rte_eth_rxconf *rx_conf __rte_unused,
614 struct rte_mempool *mb_pool)
616 struct pmd_internals *internals = dev->data->dev_private;
617 struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
619 pcap_q->mb_pool = mb_pool;
620 dev->data->rx_queues[rx_queue_id] = pcap_q;
621 pcap_q->in_port = dev->data->port_id;
627 eth_tx_queue_setup(struct rte_eth_dev *dev,
628 uint16_t tx_queue_id,
629 uint16_t nb_tx_desc __rte_unused,
630 unsigned int socket_id __rte_unused,
631 const struct rte_eth_txconf *tx_conf __rte_unused)
633 struct pmd_internals *internals = dev->data->dev_private;
635 dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
640 static const struct eth_dev_ops ops = {
641 .dev_start = eth_dev_start,
642 .dev_stop = eth_dev_stop,
643 .dev_close = eth_dev_close,
644 .dev_configure = eth_dev_configure,
645 .dev_infos_get = eth_dev_info,
646 .rx_queue_setup = eth_rx_queue_setup,
647 .tx_queue_setup = eth_tx_queue_setup,
648 .rx_queue_release = eth_queue_release,
649 .tx_queue_release = eth_queue_release,
650 .link_update = eth_link_update,
651 .stats_get = eth_stats_get,
652 .stats_reset = eth_stats_reset,
656 * Function handler that opens the pcap file for reading a stores a
657 * reference of it for use it later on.
660 open_rx_pcap(const char *key, const char *value, void *extra_args)
663 const char *pcap_filename = value;
664 struct pmd_devargs *rx = extra_args;
667 for (i = 0; i < rx->num_of_queue; i++) {
668 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
671 rx->queue[i].pcap = pcap;
672 rx->queue[i].name = pcap_filename;
673 rx->queue[i].type = key;
680 * Opens a pcap file for writing and stores a reference to it
681 * for use it later on.
684 open_tx_pcap(const char *key, const char *value, void *extra_args)
687 const char *pcap_filename = value;
688 struct pmd_devargs *dumpers = extra_args;
689 pcap_dumper_t *dumper;
691 for (i = 0; i < dumpers->num_of_queue; i++) {
692 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
695 dumpers->queue[i].dumper = dumper;
696 dumpers->queue[i].name = pcap_filename;
697 dumpers->queue[i].type = key;
704 * Opens an interface for reading and writing
707 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
709 const char *iface = value;
710 struct pmd_devargs *tx = extra_args;
713 if (open_single_iface(iface, &pcap) < 0)
716 tx->queue[0].pcap = pcap;
717 tx->queue[0].name = iface;
718 tx->queue[0].type = key;
724 * Opens a NIC for reading packets from it
727 open_rx_iface(const char *key, const char *value, void *extra_args)
730 const char *iface = value;
731 struct pmd_devargs *rx = extra_args;
734 for (i = 0; i < rx->num_of_queue; i++) {
735 if (open_single_iface(iface, &pcap) < 0)
737 rx->queue[i].pcap = pcap;
738 rx->queue[i].name = iface;
739 rx->queue[i].type = key;
746 * Opens a NIC for writing packets to it
749 open_tx_iface(const char *key, const char *value, void *extra_args)
752 const char *iface = value;
753 struct pmd_devargs *tx = extra_args;
756 for (i = 0; i < tx->num_of_queue; i++) {
757 if (open_single_iface(iface, &pcap) < 0)
759 tx->queue[i].pcap = pcap;
760 tx->queue[i].name = iface;
761 tx->queue[i].type = key;
767 static struct rte_vdev_driver pmd_pcap_drv;
770 pmd_init_internals(struct rte_vdev_device *vdev,
771 const unsigned int nb_rx_queues,
772 const unsigned int nb_tx_queues,
773 struct pmd_internals **internals,
774 struct rte_eth_dev **eth_dev)
776 struct rte_eth_dev_data *data = NULL;
777 unsigned int numa_node = vdev->device.numa_node;
780 name = rte_vdev_device_name(vdev);
781 RTE_LOG(INFO, PMD, "Creating pcap-backed ethdev on numa socket %d\n",
784 /* now do all data allocation - for eth_dev structure
785 * and internal (private) data
787 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
791 /* reserve an ethdev entry */
792 *eth_dev = rte_eth_vdev_allocate(vdev, sizeof(**internals));
793 if (*eth_dev == NULL) {
798 /* now put it all together
799 * - store queue data in internals,
800 * - store numa_node info in eth_dev
801 * - point eth_dev_data to internals
802 * - and point eth_dev structure to new eth_dev_data structure
804 *internals = (*eth_dev)->data->dev_private;
805 rte_memcpy(data, (*eth_dev)->data, sizeof(*data));
806 data->nb_rx_queues = (uint16_t)nb_rx_queues;
807 data->nb_tx_queues = (uint16_t)nb_tx_queues;
808 data->dev_link = pmd_link;
809 data->mac_addrs = ð_addr;
812 * NOTE: we'll replace the data element, of originally allocated
813 * eth_dev so the rings are local per-process
815 (*eth_dev)->data = data;
816 (*eth_dev)->dev_ops = &ops;
822 eth_from_pcaps_common(struct rte_vdev_device *vdev,
823 struct pmd_devargs *rx_queues, const unsigned int nb_rx_queues,
824 struct pmd_devargs *tx_queues, const unsigned int nb_tx_queues,
825 struct rte_kvargs *kvlist, struct pmd_internals **internals,
826 struct rte_eth_dev **eth_dev)
828 struct rte_kvargs_pair *pair = NULL;
832 /* do some parameter checking */
833 if (rx_queues == NULL && nb_rx_queues > 0)
835 if (tx_queues == NULL && nb_tx_queues > 0)
838 if (pmd_init_internals(vdev, nb_rx_queues, nb_tx_queues, internals,
842 for (i = 0; i < nb_rx_queues; i++) {
843 struct pcap_rx_queue *rx = &(*internals)->rx_queue[i];
844 struct devargs_queue *queue = &rx_queues->queue[i];
846 rx->pcap = queue->pcap;
847 snprintf(rx->name, sizeof(rx->name), "%s", queue->name);
848 snprintf(rx->type, sizeof(rx->type), "%s", queue->type);
851 for (i = 0; i < nb_tx_queues; i++) {
852 struct pcap_tx_queue *tx = &(*internals)->tx_queue[i];
853 struct devargs_queue *queue = &tx_queues->queue[i];
855 tx->dumper = queue->dumper;
856 tx->pcap = queue->pcap;
857 snprintf(tx->name, sizeof(tx->name), "%s", queue->name);
858 snprintf(tx->type, sizeof(tx->type), "%s", queue->type);
861 for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
862 pair = &kvlist->pairs[k_idx];
863 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
868 (*internals)->if_index = 0;
870 (*internals)->if_index = if_nametoindex(pair->value);
876 eth_from_pcaps(struct rte_vdev_device *vdev,
877 struct pmd_devargs *rx_queues, const unsigned int nb_rx_queues,
878 struct pmd_devargs *tx_queues, const unsigned int nb_tx_queues,
879 struct rte_kvargs *kvlist, int single_iface,
880 unsigned int using_dumpers)
882 struct pmd_internals *internals = NULL;
883 struct rte_eth_dev *eth_dev = NULL;
886 ret = eth_from_pcaps_common(vdev, rx_queues, nb_rx_queues,
887 tx_queues, nb_tx_queues, kvlist, &internals, ð_dev);
892 /* store weather we are using a single interface for rx/tx or not */
893 internals->single_iface = single_iface;
895 eth_dev->rx_pkt_burst = eth_pcap_rx;
898 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
900 eth_dev->tx_pkt_burst = eth_pcap_tx;
906 pmd_pcap_probe(struct rte_vdev_device *dev)
909 unsigned int is_rx_pcap = 0, is_tx_pcap = 0;
910 struct rte_kvargs *kvlist;
911 struct pmd_devargs pcaps = {0};
912 struct pmd_devargs dumpers = {0};
913 int single_iface = 0;
916 name = rte_vdev_device_name(dev);
917 RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
919 gettimeofday(&start_time, NULL);
920 start_cycles = rte_get_timer_cycles();
921 hz = rte_get_timer_hz();
923 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
928 * If iface argument is passed we open the NICs and use them for
931 if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
933 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
934 &open_rx_tx_iface, &pcaps);
939 dumpers.queue[0] = pcaps.queue[0];
942 pcaps.num_of_queue = 1;
943 dumpers.num_of_queue = 1;
949 * We check whether we want to open a RX stream from a real NIC or a
952 pcaps.num_of_queue = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG);
953 if (pcaps.num_of_queue)
956 pcaps.num_of_queue = rte_kvargs_count(kvlist,
957 ETH_PCAP_RX_IFACE_ARG);
959 if (pcaps.num_of_queue > RTE_PMD_PCAP_MAX_QUEUES)
960 pcaps.num_of_queue = RTE_PMD_PCAP_MAX_QUEUES;
963 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
964 &open_rx_pcap, &pcaps);
966 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
967 &open_rx_iface, &pcaps);
973 * We check whether we want to open a TX stream to a real NIC or a
976 dumpers.num_of_queue = rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG);
977 if (dumpers.num_of_queue)
980 dumpers.num_of_queue = rte_kvargs_count(kvlist,
981 ETH_PCAP_TX_IFACE_ARG);
983 if (dumpers.num_of_queue > RTE_PMD_PCAP_MAX_QUEUES)
984 dumpers.num_of_queue = RTE_PMD_PCAP_MAX_QUEUES;
987 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
988 &open_tx_pcap, &dumpers);
990 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
991 &open_tx_iface, &dumpers);
997 ret = eth_from_pcaps(dev, &pcaps, pcaps.num_of_queue, &dumpers,
998 dumpers.num_of_queue, kvlist, single_iface, is_tx_pcap);
1001 rte_kvargs_free(kvlist);
1007 pmd_pcap_remove(struct rte_vdev_device *dev)
1009 struct rte_eth_dev *eth_dev = NULL;
1011 RTE_LOG(INFO, PMD, "Closing pcap ethdev on numa socket %d\n",
1017 /* reserve an ethdev entry */
1018 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1019 if (eth_dev == NULL)
1022 rte_free(eth_dev->data->dev_private);
1023 rte_free(eth_dev->data);
1025 rte_eth_dev_release_port(eth_dev);
1030 static struct rte_vdev_driver pmd_pcap_drv = {
1031 .probe = pmd_pcap_probe,
1032 .remove = pmd_pcap_remove,
1035 RTE_PMD_REGISTER_VDEV(net_pcap, pmd_pcap_drv);
1036 RTE_PMD_REGISTER_ALIAS(net_pcap, eth_pcap);
1037 RTE_PMD_REGISTER_PARAM_STRING(net_pcap,
1038 ETH_PCAP_RX_PCAP_ARG "=<string> "
1039 ETH_PCAP_TX_PCAP_ARG "=<string> "
1040 ETH_PCAP_RX_IFACE_ARG "=<ifc> "
1041 ETH_PCAP_TX_IFACE_ARG "=<ifc> "
1042 ETH_PCAP_IFACE_ARG "=<ifc>");