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_FIXED,
102 static int eth_pcap_logtype;
104 #define PMD_LOG(level, fmt, args...) \
105 rte_log(RTE_LOG_ ## level, eth_pcap_logtype, \
106 "%s(): " fmt "\n", __func__, ##args)
109 eth_pcap_rx_jumbo(struct rte_mempool *mb_pool, struct rte_mbuf *mbuf,
110 const u_char *data, uint16_t data_len)
112 /* Copy the first segment. */
113 uint16_t len = rte_pktmbuf_tailroom(mbuf);
114 struct rte_mbuf *m = mbuf;
116 rte_memcpy(rte_pktmbuf_append(mbuf, len), data, len);
120 while (data_len > 0) {
121 /* Allocate next mbuf and point to that. */
122 m->next = rte_pktmbuf_alloc(mb_pool);
124 if (unlikely(!m->next))
129 /* Headroom is not needed in chained mbufs. */
130 rte_pktmbuf_prepend(m, rte_pktmbuf_headroom(m));
134 /* Copy next segment. */
135 len = RTE_MIN(rte_pktmbuf_tailroom(m), data_len);
136 rte_memcpy(rte_pktmbuf_append(m, len), data, len);
143 return mbuf->nb_segs;
146 /* Copy data from mbuf chain to a buffer suitable for writing to a PCAP file. */
148 eth_pcap_gather_data(unsigned char *data, struct rte_mbuf *mbuf)
150 uint16_t data_len = 0;
153 rte_memcpy(data + data_len, rte_pktmbuf_mtod(mbuf, void *),
156 data_len += mbuf->data_len;
162 eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
165 struct pcap_pkthdr header;
166 const u_char *packet;
167 struct rte_mbuf *mbuf;
168 struct pcap_rx_queue *pcap_q = queue;
171 uint32_t rx_bytes = 0;
173 if (unlikely(pcap_q->pcap == NULL || nb_pkts == 0))
176 /* Reads the given number of packets from the pcap file one by one
177 * and copies the packet data into a newly allocated mbuf to return.
179 for (i = 0; i < nb_pkts; i++) {
180 /* Get the next PCAP packet */
181 packet = pcap_next(pcap_q->pcap, &header);
182 if (unlikely(packet == NULL))
185 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
186 if (unlikely(mbuf == NULL))
189 /* Now get the space available for data in the mbuf */
190 buf_size = rte_pktmbuf_data_room_size(pcap_q->mb_pool) -
191 RTE_PKTMBUF_HEADROOM;
193 if (header.caplen <= buf_size) {
194 /* pcap packet will fit in the mbuf, can copy it */
195 rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), packet,
197 mbuf->data_len = (uint16_t)header.caplen;
199 /* Try read jumbo frame into multi mbufs. */
200 if (unlikely(eth_pcap_rx_jumbo(pcap_q->mb_pool,
203 header.caplen) == -1)) {
204 rte_pktmbuf_free(mbuf);
209 mbuf->pkt_len = (uint16_t)header.caplen;
210 mbuf->port = pcap_q->in_port;
213 rx_bytes += header.caplen;
215 pcap_q->rx_stat.pkts += num_rx;
216 pcap_q->rx_stat.bytes += rx_bytes;
222 calculate_timestamp(struct timeval *ts) {
224 struct timeval cur_time;
226 cycles = rte_get_timer_cycles() - start_cycles;
227 cur_time.tv_sec = cycles / hz;
228 cur_time.tv_usec = (cycles % hz) * 1e6 / hz;
229 timeradd(&start_time, &cur_time, ts);
233 * Callback to handle writing packets to a pcap file.
236 eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
239 struct rte_mbuf *mbuf;
240 struct pcap_tx_queue *dumper_q = queue;
242 uint32_t tx_bytes = 0;
243 struct pcap_pkthdr header;
245 if (dumper_q->dumper == NULL || nb_pkts == 0)
248 /* writes the nb_pkts packets to the previously opened pcap file
250 for (i = 0; i < nb_pkts; i++) {
252 calculate_timestamp(&header.ts);
253 header.len = mbuf->pkt_len;
254 header.caplen = header.len;
256 if (likely(mbuf->nb_segs == 1)) {
257 pcap_dump((u_char *)dumper_q->dumper, &header,
258 rte_pktmbuf_mtod(mbuf, void*));
260 if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
261 eth_pcap_gather_data(tx_pcap_data, mbuf);
262 pcap_dump((u_char *)dumper_q->dumper, &header,
266 "Dropping PCAP packet. Size (%d) > max jumbo size (%d).",
268 ETHER_MAX_JUMBO_FRAME_LEN);
270 rte_pktmbuf_free(mbuf);
276 tx_bytes += mbuf->pkt_len;
277 rte_pktmbuf_free(mbuf);
281 * Since there's no place to hook a callback when the forwarding
282 * process stops and to make sure the pcap file is actually written,
283 * we flush the pcap dumper within each burst.
285 pcap_dump_flush(dumper_q->dumper);
286 dumper_q->tx_stat.pkts += num_tx;
287 dumper_q->tx_stat.bytes += tx_bytes;
288 dumper_q->tx_stat.err_pkts += nb_pkts - num_tx;
294 * Callback to handle sending packets through a real NIC.
297 eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
301 struct rte_mbuf *mbuf;
302 struct pcap_tx_queue *tx_queue = queue;
304 uint32_t tx_bytes = 0;
306 if (unlikely(nb_pkts == 0 || tx_queue->pcap == NULL))
309 for (i = 0; i < nb_pkts; i++) {
312 if (likely(mbuf->nb_segs == 1)) {
313 ret = pcap_sendpacket(tx_queue->pcap,
314 rte_pktmbuf_mtod(mbuf, u_char *),
317 if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
318 eth_pcap_gather_data(tx_pcap_data, mbuf);
319 ret = pcap_sendpacket(tx_queue->pcap,
320 tx_pcap_data, mbuf->pkt_len);
323 "Dropping PCAP packet. Size (%d) > max jumbo size (%d).",
325 ETHER_MAX_JUMBO_FRAME_LEN);
327 rte_pktmbuf_free(mbuf);
332 if (unlikely(ret != 0))
335 tx_bytes += mbuf->pkt_len;
336 rte_pktmbuf_free(mbuf);
339 tx_queue->tx_stat.pkts += num_tx;
340 tx_queue->tx_stat.bytes += tx_bytes;
341 tx_queue->tx_stat.err_pkts += nb_pkts - num_tx;
347 * pcap_open_live wrapper function
350 open_iface_live(const char *iface, pcap_t **pcap) {
351 *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
352 RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
355 PMD_LOG(ERR, "Couldn't open %s: %s", iface, errbuf);
363 open_single_iface(const char *iface, pcap_t **pcap)
365 if (open_iface_live(iface, pcap) < 0) {
366 PMD_LOG(ERR, "Couldn't open interface %s", iface);
374 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
379 * We need to create a dummy empty pcap_t to use it
380 * with pcap_dump_open(). We create big enough an Ethernet
383 tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN);
384 if (tx_pcap == NULL) {
385 PMD_LOG(ERR, "Couldn't create dead pcap");
389 /* The dumper is created using the previous pcap_t reference */
390 *dumper = pcap_dump_open(tx_pcap, pcap_filename);
391 if (*dumper == NULL) {
393 PMD_LOG(ERR, "Couldn't open %s for writing.",
403 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
405 *pcap = pcap_open_offline(pcap_filename, errbuf);
407 PMD_LOG(ERR, "Couldn't open %s: %s", pcap_filename,
416 eth_dev_start(struct rte_eth_dev *dev)
419 struct pmd_internals *internals = dev->data->dev_private;
420 struct pcap_tx_queue *tx;
421 struct pcap_rx_queue *rx;
423 /* Special iface case. Single pcap is open and shared between tx/rx. */
424 if (internals->single_iface) {
425 tx = &internals->tx_queue[0];
426 rx = &internals->rx_queue[0];
428 if (!tx->pcap && strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
429 if (open_single_iface(tx->name, &tx->pcap) < 0)
436 /* If not open already, open tx pcaps/dumpers */
437 for (i = 0; i < dev->data->nb_tx_queues; i++) {
438 tx = &internals->tx_queue[i];
441 strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
442 if (open_single_tx_pcap(tx->name, &tx->dumper) < 0)
444 } else if (!tx->pcap &&
445 strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
446 if (open_single_iface(tx->name, &tx->pcap) < 0)
451 /* If not open already, open rx pcaps */
452 for (i = 0; i < dev->data->nb_rx_queues; i++) {
453 rx = &internals->rx_queue[i];
455 if (rx->pcap != NULL)
458 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
459 if (open_single_rx_pcap(rx->name, &rx->pcap) < 0)
461 } else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
462 if (open_single_iface(rx->name, &rx->pcap) < 0)
468 dev->data->dev_link.link_status = ETH_LINK_UP;
474 * This function gets called when the current port gets stopped.
475 * Is the only place for us to close all the tx streams dumpers.
476 * If not called the dumpers will be flushed within each tx burst.
479 eth_dev_stop(struct rte_eth_dev *dev)
482 struct pmd_internals *internals = dev->data->dev_private;
483 struct pcap_tx_queue *tx;
484 struct pcap_rx_queue *rx;
486 /* Special iface case. Single pcap is open and shared between tx/rx. */
487 if (internals->single_iface) {
488 tx = &internals->tx_queue[0];
489 rx = &internals->rx_queue[0];
490 pcap_close(tx->pcap);
496 for (i = 0; i < dev->data->nb_tx_queues; i++) {
497 tx = &internals->tx_queue[i];
499 if (tx->dumper != NULL) {
500 pcap_dump_close(tx->dumper);
504 if (tx->pcap != NULL) {
505 pcap_close(tx->pcap);
510 for (i = 0; i < dev->data->nb_rx_queues; i++) {
511 rx = &internals->rx_queue[i];
513 if (rx->pcap != NULL) {
514 pcap_close(rx->pcap);
520 dev->data->dev_link.link_status = ETH_LINK_DOWN;
524 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
530 eth_dev_info(struct rte_eth_dev *dev,
531 struct rte_eth_dev_info *dev_info)
533 struct pmd_internals *internals = dev->data->dev_private;
535 dev_info->if_index = internals->if_index;
536 dev_info->max_mac_addrs = 1;
537 dev_info->max_rx_pktlen = (uint32_t) -1;
538 dev_info->max_rx_queues = dev->data->nb_rx_queues;
539 dev_info->max_tx_queues = dev->data->nb_tx_queues;
540 dev_info->min_rx_bufsize = 0;
544 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
547 unsigned long rx_packets_total = 0, rx_bytes_total = 0;
548 unsigned long tx_packets_total = 0, tx_bytes_total = 0;
549 unsigned long tx_packets_err_total = 0;
550 const struct pmd_internals *internal = dev->data->dev_private;
552 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
553 i < dev->data->nb_rx_queues; i++) {
554 stats->q_ipackets[i] = internal->rx_queue[i].rx_stat.pkts;
555 stats->q_ibytes[i] = internal->rx_queue[i].rx_stat.bytes;
556 rx_packets_total += stats->q_ipackets[i];
557 rx_bytes_total += stats->q_ibytes[i];
560 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
561 i < dev->data->nb_tx_queues; i++) {
562 stats->q_opackets[i] = internal->tx_queue[i].tx_stat.pkts;
563 stats->q_obytes[i] = internal->tx_queue[i].tx_stat.bytes;
564 stats->q_errors[i] = internal->tx_queue[i].tx_stat.err_pkts;
565 tx_packets_total += stats->q_opackets[i];
566 tx_bytes_total += stats->q_obytes[i];
567 tx_packets_err_total += stats->q_errors[i];
570 stats->ipackets = rx_packets_total;
571 stats->ibytes = rx_bytes_total;
572 stats->opackets = tx_packets_total;
573 stats->obytes = tx_bytes_total;
574 stats->oerrors = tx_packets_err_total;
580 eth_stats_reset(struct rte_eth_dev *dev)
583 struct pmd_internals *internal = dev->data->dev_private;
585 for (i = 0; i < dev->data->nb_rx_queues; i++) {
586 internal->rx_queue[i].rx_stat.pkts = 0;
587 internal->rx_queue[i].rx_stat.bytes = 0;
590 for (i = 0; i < dev->data->nb_tx_queues; i++) {
591 internal->tx_queue[i].tx_stat.pkts = 0;
592 internal->tx_queue[i].tx_stat.bytes = 0;
593 internal->tx_queue[i].tx_stat.err_pkts = 0;
598 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
603 eth_queue_release(void *q __rte_unused)
608 eth_link_update(struct rte_eth_dev *dev __rte_unused,
609 int wait_to_complete __rte_unused)
615 eth_rx_queue_setup(struct rte_eth_dev *dev,
616 uint16_t rx_queue_id,
617 uint16_t nb_rx_desc __rte_unused,
618 unsigned int socket_id __rte_unused,
619 const struct rte_eth_rxconf *rx_conf __rte_unused,
620 struct rte_mempool *mb_pool)
622 struct pmd_internals *internals = dev->data->dev_private;
623 struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
625 pcap_q->mb_pool = mb_pool;
626 dev->data->rx_queues[rx_queue_id] = pcap_q;
627 pcap_q->in_port = dev->data->port_id;
633 eth_tx_queue_setup(struct rte_eth_dev *dev,
634 uint16_t tx_queue_id,
635 uint16_t nb_tx_desc __rte_unused,
636 unsigned int socket_id __rte_unused,
637 const struct rte_eth_txconf *tx_conf __rte_unused)
639 struct pmd_internals *internals = dev->data->dev_private;
641 dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
646 static const struct eth_dev_ops ops = {
647 .dev_start = eth_dev_start,
648 .dev_stop = eth_dev_stop,
649 .dev_close = eth_dev_close,
650 .dev_configure = eth_dev_configure,
651 .dev_infos_get = eth_dev_info,
652 .rx_queue_setup = eth_rx_queue_setup,
653 .tx_queue_setup = eth_tx_queue_setup,
654 .rx_queue_release = eth_queue_release,
655 .tx_queue_release = eth_queue_release,
656 .link_update = eth_link_update,
657 .stats_get = eth_stats_get,
658 .stats_reset = eth_stats_reset,
662 * Function handler that opens the pcap file for reading a stores a
663 * reference of it for use it later on.
666 open_rx_pcap(const char *key, const char *value, void *extra_args)
668 const char *pcap_filename = value;
669 struct pmd_devargs *rx = extra_args;
672 if (rx->num_of_queue >= RTE_PMD_PCAP_MAX_QUEUES)
674 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
677 rx->queue[rx->num_of_queue].pcap = pcap;
678 rx->queue[rx->num_of_queue].name = pcap_filename;
679 rx->queue[rx->num_of_queue].type = key;
686 * Opens a pcap file for writing and stores a reference to it
687 * for use it later on.
690 open_tx_pcap(const char *key, const char *value, void *extra_args)
692 const char *pcap_filename = value;
693 struct pmd_devargs *dumpers = extra_args;
694 pcap_dumper_t *dumper;
696 if (dumpers->num_of_queue >= RTE_PMD_PCAP_MAX_QUEUES)
698 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
701 dumpers->queue[dumpers->num_of_queue].dumper = dumper;
702 dumpers->queue[dumpers->num_of_queue].name = pcap_filename;
703 dumpers->queue[dumpers->num_of_queue].type = key;
704 dumpers->num_of_queue++;
710 * Opens an interface for reading and writing
713 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
715 const char *iface = value;
716 struct pmd_devargs *tx = extra_args;
719 if (open_single_iface(iface, &pcap) < 0)
722 tx->queue[0].pcap = pcap;
723 tx->queue[0].name = iface;
724 tx->queue[0].type = key;
730 * Opens a NIC for reading packets from it
733 open_rx_iface(const char *key, const char *value, void *extra_args)
735 const char *iface = value;
736 struct pmd_devargs *rx = extra_args;
739 if (rx->num_of_queue >= RTE_PMD_PCAP_MAX_QUEUES)
741 if (open_single_iface(iface, &pcap) < 0)
743 rx->queue[rx->num_of_queue].pcap = pcap;
744 rx->queue[rx->num_of_queue].name = iface;
745 rx->queue[rx->num_of_queue].type = key;
752 * Opens a NIC for writing packets to it
755 open_tx_iface(const char *key, const char *value, void *extra_args)
757 const char *iface = value;
758 struct pmd_devargs *tx = extra_args;
761 if (tx->num_of_queue >= RTE_PMD_PCAP_MAX_QUEUES)
763 if (open_single_iface(iface, &pcap) < 0)
765 tx->queue[tx->num_of_queue].pcap = pcap;
766 tx->queue[tx->num_of_queue].name = iface;
767 tx->queue[tx->num_of_queue].type = key;
773 static struct rte_vdev_driver pmd_pcap_drv;
776 pmd_init_internals(struct rte_vdev_device *vdev,
777 const unsigned int nb_rx_queues,
778 const unsigned int nb_tx_queues,
779 struct pmd_internals **internals,
780 struct rte_eth_dev **eth_dev)
782 struct rte_eth_dev_data *data;
783 unsigned int numa_node = vdev->device.numa_node;
785 PMD_LOG(INFO, "Creating pcap-backed ethdev on numa socket %d",
788 /* reserve an ethdev entry */
789 *eth_dev = rte_eth_vdev_allocate(vdev, sizeof(**internals));
793 /* now put it all together
794 * - store queue data in internals,
795 * - store numa_node info in eth_dev
796 * - point eth_dev_data to internals
797 * - and point eth_dev structure to new eth_dev_data structure
799 *internals = (*eth_dev)->data->dev_private;
800 data = (*eth_dev)->data;
801 data->nb_rx_queues = (uint16_t)nb_rx_queues;
802 data->nb_tx_queues = (uint16_t)nb_tx_queues;
803 data->dev_link = pmd_link;
804 data->mac_addrs = ð_addr;
807 * NOTE: we'll replace the data element, of originally allocated
808 * eth_dev so the rings are local per-process
810 (*eth_dev)->dev_ops = &ops;
816 eth_from_pcaps_common(struct rte_vdev_device *vdev,
817 struct pmd_devargs *rx_queues, const unsigned int nb_rx_queues,
818 struct pmd_devargs *tx_queues, const unsigned int nb_tx_queues,
819 struct rte_kvargs *kvlist, struct pmd_internals **internals,
820 struct rte_eth_dev **eth_dev)
822 struct rte_kvargs_pair *pair = NULL;
826 /* do some parameter checking */
827 if (rx_queues == NULL && nb_rx_queues > 0)
829 if (tx_queues == NULL && nb_tx_queues > 0)
832 if (pmd_init_internals(vdev, nb_rx_queues, nb_tx_queues, internals,
836 for (i = 0; i < nb_rx_queues; i++) {
837 struct pcap_rx_queue *rx = &(*internals)->rx_queue[i];
838 struct devargs_queue *queue = &rx_queues->queue[i];
840 rx->pcap = queue->pcap;
841 snprintf(rx->name, sizeof(rx->name), "%s", queue->name);
842 snprintf(rx->type, sizeof(rx->type), "%s", queue->type);
845 for (i = 0; i < nb_tx_queues; i++) {
846 struct pcap_tx_queue *tx = &(*internals)->tx_queue[i];
847 struct devargs_queue *queue = &tx_queues->queue[i];
849 tx->dumper = queue->dumper;
850 tx->pcap = queue->pcap;
851 snprintf(tx->name, sizeof(tx->name), "%s", queue->name);
852 snprintf(tx->type, sizeof(tx->type), "%s", queue->type);
855 for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
856 pair = &kvlist->pairs[k_idx];
857 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
862 (*internals)->if_index = 0;
864 (*internals)->if_index = if_nametoindex(pair->value);
870 eth_from_pcaps(struct rte_vdev_device *vdev,
871 struct pmd_devargs *rx_queues, const unsigned int nb_rx_queues,
872 struct pmd_devargs *tx_queues, const unsigned int nb_tx_queues,
873 struct rte_kvargs *kvlist, int single_iface,
874 unsigned int using_dumpers)
876 struct pmd_internals *internals = NULL;
877 struct rte_eth_dev *eth_dev = NULL;
880 ret = eth_from_pcaps_common(vdev, rx_queues, nb_rx_queues,
881 tx_queues, nb_tx_queues, kvlist, &internals, ð_dev);
886 /* store weather we are using a single interface for rx/tx or not */
887 internals->single_iface = single_iface;
889 eth_dev->rx_pkt_burst = eth_pcap_rx;
892 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
894 eth_dev->tx_pkt_burst = eth_pcap_tx;
896 rte_eth_dev_probing_finish(eth_dev);
901 pmd_pcap_probe(struct rte_vdev_device *dev)
904 unsigned int is_rx_pcap = 0, is_tx_pcap = 0;
905 struct rte_kvargs *kvlist;
906 struct pmd_devargs pcaps = {0};
907 struct pmd_devargs dumpers = {0};
908 struct rte_eth_dev *eth_dev;
909 int single_iface = 0;
912 name = rte_vdev_device_name(dev);
913 PMD_LOG(INFO, "Initializing pmd_pcap for %s", name);
915 gettimeofday(&start_time, NULL);
916 start_cycles = rte_get_timer_cycles();
917 hz = rte_get_timer_hz();
919 if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
920 strlen(rte_vdev_device_args(dev)) == 0) {
921 eth_dev = rte_eth_dev_attach_secondary(name);
923 PMD_LOG(ERR, "Failed to probe %s", name);
926 /* TODO: request info from primary to set up Rx and Tx */
927 eth_dev->dev_ops = &ops;
928 rte_eth_dev_probing_finish(eth_dev);
932 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
937 * If iface argument is passed we open the NICs and use them for
940 if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
942 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
943 &open_rx_tx_iface, &pcaps);
948 dumpers.queue[0] = pcaps.queue[0];
951 pcaps.num_of_queue = 1;
952 dumpers.num_of_queue = 1;
958 * We check whether we want to open a RX stream from a real NIC or a
961 is_rx_pcap = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG) ? 1 : 0;
962 pcaps.num_of_queue = 0;
965 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
966 &open_rx_pcap, &pcaps);
968 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
969 &open_rx_iface, &pcaps);
975 * We check whether we want to open a TX stream to a real NIC or a
978 is_tx_pcap = rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG) ? 1 : 0;
979 dumpers.num_of_queue = 0;
982 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
983 &open_tx_pcap, &dumpers);
985 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
986 &open_tx_iface, &dumpers);
992 ret = eth_from_pcaps(dev, &pcaps, pcaps.num_of_queue, &dumpers,
993 dumpers.num_of_queue, kvlist, single_iface, is_tx_pcap);
996 rte_kvargs_free(kvlist);
1002 pmd_pcap_remove(struct rte_vdev_device *dev)
1004 struct rte_eth_dev *eth_dev = NULL;
1006 PMD_LOG(INFO, "Closing pcap ethdev on numa socket %d",
1012 /* reserve an ethdev entry */
1013 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1014 if (eth_dev == NULL)
1017 rte_free(eth_dev->data->dev_private);
1019 rte_eth_dev_release_port(eth_dev);
1024 static struct rte_vdev_driver pmd_pcap_drv = {
1025 .probe = pmd_pcap_probe,
1026 .remove = pmd_pcap_remove,
1029 RTE_PMD_REGISTER_VDEV(net_pcap, pmd_pcap_drv);
1030 RTE_PMD_REGISTER_ALIAS(net_pcap, eth_pcap);
1031 RTE_PMD_REGISTER_PARAM_STRING(net_pcap,
1032 ETH_PCAP_RX_PCAP_ARG "=<string> "
1033 ETH_PCAP_TX_PCAP_ARG "=<string> "
1034 ETH_PCAP_RX_IFACE_ARG "=<ifc> "
1035 ETH_PCAP_TX_IFACE_ARG "=<ifc> "
1036 ETH_PCAP_IFACE_ARG "=<ifc>");
1038 RTE_INIT(eth_pcap_init_log);
1040 eth_pcap_init_log(void)
1042 eth_pcap_logtype = rte_log_register("pmd.net.pcap");
1043 if (eth_pcap_logtype >= 0)
1044 rte_log_set_level(eth_pcap_logtype, RTE_LOG_NOTICE);