1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation.
3 * Copyright(c) 2014 6WIND S.A.
10 #include <sys/socket.h>
11 #include <sys/ioctl.h>
14 #if defined(RTE_EXEC_ENV_FREEBSD)
15 #include <sys/sysctl.h>
16 #include <net/if_dl.h>
21 #include <rte_cycles.h>
22 #include <rte_ethdev_driver.h>
23 #include <rte_ethdev_vdev.h>
24 #include <rte_kvargs.h>
25 #include <rte_malloc.h>
27 #include <rte_bus_vdev.h>
28 #include <rte_string_fns.h>
30 #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535
31 #define RTE_ETH_PCAP_SNAPLEN RTE_ETHER_MAX_JUMBO_FRAME_LEN
32 #define RTE_ETH_PCAP_PROMISC 1
33 #define RTE_ETH_PCAP_TIMEOUT -1
35 #define ETH_PCAP_RX_PCAP_ARG "rx_pcap"
36 #define ETH_PCAP_TX_PCAP_ARG "tx_pcap"
37 #define ETH_PCAP_RX_IFACE_ARG "rx_iface"
38 #define ETH_PCAP_RX_IFACE_IN_ARG "rx_iface_in"
39 #define ETH_PCAP_TX_IFACE_ARG "tx_iface"
40 #define ETH_PCAP_IFACE_ARG "iface"
41 #define ETH_PCAP_PHY_MAC_ARG "phy_mac"
42 #define ETH_PCAP_INFINITE_RX_ARG "infinite_rx"
44 #define ETH_PCAP_ARG_MAXLEN 64
46 #define RTE_PMD_PCAP_MAX_QUEUES 16
48 static char errbuf[PCAP_ERRBUF_SIZE];
49 static unsigned char tx_pcap_data[RTE_ETH_PCAP_SNAPLEN];
50 static struct timeval start_time;
51 static uint64_t start_cycles;
53 static uint8_t iface_idx;
56 volatile unsigned long pkts;
57 volatile unsigned long bytes;
58 volatile unsigned long err_pkts;
61 struct pcap_rx_queue {
64 struct rte_mempool *mb_pool;
65 struct queue_stat rx_stat;
67 char type[ETH_PCAP_ARG_MAXLEN];
69 /* Contains pre-generated packets to be looped through */
70 struct rte_ring *pkts;
73 struct pcap_tx_queue {
76 struct queue_stat tx_stat;
78 char type[ETH_PCAP_ARG_MAXLEN];
81 struct pmd_internals {
82 struct pcap_rx_queue rx_queue[RTE_PMD_PCAP_MAX_QUEUES];
83 struct pcap_tx_queue tx_queue[RTE_PMD_PCAP_MAX_QUEUES];
84 char devargs[ETH_PCAP_ARG_MAXLEN];
85 struct rte_ether_addr eth_addr;
89 unsigned int infinite_rx;
92 struct pmd_process_private {
93 pcap_t *rx_pcap[RTE_PMD_PCAP_MAX_QUEUES];
94 pcap_t *tx_pcap[RTE_PMD_PCAP_MAX_QUEUES];
95 pcap_dumper_t *tx_dumper[RTE_PMD_PCAP_MAX_QUEUES];
99 unsigned int num_of_queue;
100 struct devargs_queue {
101 pcap_dumper_t *dumper;
105 } queue[RTE_PMD_PCAP_MAX_QUEUES];
109 struct pmd_devargs_all {
110 struct pmd_devargs rx_queues;
111 struct pmd_devargs tx_queues;
113 unsigned int is_tx_pcap;
114 unsigned int is_tx_iface;
115 unsigned int infinite_rx;
118 static const char *valid_arguments[] = {
119 ETH_PCAP_RX_PCAP_ARG,
120 ETH_PCAP_TX_PCAP_ARG,
121 ETH_PCAP_RX_IFACE_ARG,
122 ETH_PCAP_RX_IFACE_IN_ARG,
123 ETH_PCAP_TX_IFACE_ARG,
125 ETH_PCAP_PHY_MAC_ARG,
126 ETH_PCAP_INFINITE_RX_ARG,
130 static struct rte_eth_link pmd_link = {
131 .link_speed = ETH_SPEED_NUM_10G,
132 .link_duplex = ETH_LINK_FULL_DUPLEX,
133 .link_status = ETH_LINK_DOWN,
134 .link_autoneg = ETH_LINK_FIXED,
137 static int eth_pcap_logtype;
139 #define PMD_LOG(level, fmt, args...) \
140 rte_log(RTE_LOG_ ## level, eth_pcap_logtype, \
141 "%s(): " fmt "\n", __func__, ##args)
144 eth_pcap_rx_jumbo(struct rte_mempool *mb_pool, struct rte_mbuf *mbuf,
145 const u_char *data, uint16_t data_len)
147 /* Copy the first segment. */
148 uint16_t len = rte_pktmbuf_tailroom(mbuf);
149 struct rte_mbuf *m = mbuf;
151 rte_memcpy(rte_pktmbuf_append(mbuf, len), data, len);
155 while (data_len > 0) {
156 /* Allocate next mbuf and point to that. */
157 m->next = rte_pktmbuf_alloc(mb_pool);
159 if (unlikely(!m->next))
164 /* Headroom is not needed in chained mbufs. */
165 rte_pktmbuf_prepend(m, rte_pktmbuf_headroom(m));
169 /* Copy next segment. */
170 len = RTE_MIN(rte_pktmbuf_tailroom(m), data_len);
171 rte_memcpy(rte_pktmbuf_append(m, len), data, len);
178 return mbuf->nb_segs;
181 /* Copy data from mbuf chain to a buffer suitable for writing to a PCAP file. */
183 eth_pcap_gather_data(unsigned char *data, struct rte_mbuf *mbuf)
185 uint16_t data_len = 0;
188 rte_memcpy(data + data_len, rte_pktmbuf_mtod(mbuf, void *),
191 data_len += mbuf->data_len;
197 eth_pcap_rx_infinite(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
200 struct pcap_rx_queue *pcap_q = queue;
201 uint32_t rx_bytes = 0;
203 if (unlikely(nb_pkts == 0))
206 if (rte_pktmbuf_alloc_bulk(pcap_q->mb_pool, bufs, nb_pkts) != 0)
209 for (i = 0; i < nb_pkts; i++) {
210 struct rte_mbuf *pcap_buf;
211 int err = rte_ring_dequeue(pcap_q->pkts, (void **)&pcap_buf);
215 rte_memcpy(rte_pktmbuf_mtod(bufs[i], void *),
216 rte_pktmbuf_mtod(pcap_buf, void *),
218 bufs[i]->data_len = pcap_buf->data_len;
219 bufs[i]->pkt_len = pcap_buf->pkt_len;
220 bufs[i]->port = pcap_q->port_id;
221 rx_bytes += pcap_buf->data_len;
223 /* Enqueue packet back on ring to allow infinite rx. */
224 rte_ring_enqueue(pcap_q->pkts, pcap_buf);
227 pcap_q->rx_stat.pkts += i;
228 pcap_q->rx_stat.bytes += rx_bytes;
234 eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
237 struct pcap_pkthdr header;
238 struct pmd_process_private *pp;
239 const u_char *packet;
240 struct rte_mbuf *mbuf;
241 struct pcap_rx_queue *pcap_q = queue;
244 uint32_t rx_bytes = 0;
247 pp = rte_eth_devices[pcap_q->port_id].process_private;
248 pcap = pp->rx_pcap[pcap_q->queue_id];
250 if (unlikely(pcap == NULL || nb_pkts == 0))
253 /* Reads the given number of packets from the pcap file one by one
254 * and copies the packet data into a newly allocated mbuf to return.
256 for (i = 0; i < nb_pkts; i++) {
257 /* Get the next PCAP packet */
258 packet = pcap_next(pcap, &header);
259 if (unlikely(packet == NULL))
262 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
263 if (unlikely(mbuf == NULL))
266 /* Now get the space available for data in the mbuf */
267 buf_size = rte_pktmbuf_data_room_size(pcap_q->mb_pool) -
268 RTE_PKTMBUF_HEADROOM;
270 if (header.caplen <= buf_size) {
271 /* pcap packet will fit in the mbuf, can copy it */
272 rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), packet,
274 mbuf->data_len = (uint16_t)header.caplen;
276 /* Try read jumbo frame into multi mbufs. */
277 if (unlikely(eth_pcap_rx_jumbo(pcap_q->mb_pool,
280 header.caplen) == -1)) {
281 rte_pktmbuf_free(mbuf);
286 mbuf->pkt_len = (uint16_t)header.caplen;
287 mbuf->port = pcap_q->port_id;
290 rx_bytes += header.caplen;
292 pcap_q->rx_stat.pkts += num_rx;
293 pcap_q->rx_stat.bytes += rx_bytes;
299 calculate_timestamp(struct timeval *ts) {
301 struct timeval cur_time;
303 cycles = rte_get_timer_cycles() - start_cycles;
304 cur_time.tv_sec = cycles / hz;
305 cur_time.tv_usec = (cycles % hz) * 1e6 / hz;
306 timeradd(&start_time, &cur_time, ts);
310 * Callback to handle writing packets to a pcap file.
313 eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
316 struct rte_mbuf *mbuf;
317 struct pmd_process_private *pp;
318 struct pcap_tx_queue *dumper_q = queue;
320 uint32_t tx_bytes = 0;
321 struct pcap_pkthdr header;
322 pcap_dumper_t *dumper;
324 pp = rte_eth_devices[dumper_q->port_id].process_private;
325 dumper = pp->tx_dumper[dumper_q->queue_id];
327 if (dumper == NULL || nb_pkts == 0)
330 /* writes the nb_pkts packets to the previously opened pcap file
332 for (i = 0; i < nb_pkts; i++) {
334 calculate_timestamp(&header.ts);
335 header.len = mbuf->pkt_len;
336 header.caplen = header.len;
338 if (likely(mbuf->nb_segs == 1)) {
339 pcap_dump((u_char *)dumper, &header,
340 rte_pktmbuf_mtod(mbuf, void*));
342 if (mbuf->pkt_len <= RTE_ETHER_MAX_JUMBO_FRAME_LEN) {
343 eth_pcap_gather_data(tx_pcap_data, mbuf);
344 pcap_dump((u_char *)dumper, &header,
348 "Dropping PCAP packet. Size (%d) > max jumbo size (%d).",
350 RTE_ETHER_MAX_JUMBO_FRAME_LEN);
357 tx_bytes += mbuf->pkt_len;
358 rte_pktmbuf_free(mbuf);
362 * Since there's no place to hook a callback when the forwarding
363 * process stops and to make sure the pcap file is actually written,
364 * we flush the pcap dumper within each burst.
366 pcap_dump_flush(dumper);
367 dumper_q->tx_stat.pkts += num_tx;
368 dumper_q->tx_stat.bytes += tx_bytes;
369 dumper_q->tx_stat.err_pkts += nb_pkts - num_tx;
375 * Callback to handle dropping packets in the infinite rx case.
378 eth_tx_drop(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
381 uint32_t tx_bytes = 0;
382 struct pcap_tx_queue *tx_queue = queue;
384 if (unlikely(nb_pkts == 0))
387 for (i = 0; i < nb_pkts; i++) {
388 tx_bytes += bufs[i]->data_len;
389 rte_pktmbuf_free(bufs[i]);
392 tx_queue->tx_stat.pkts += nb_pkts;
393 tx_queue->tx_stat.bytes += tx_bytes;
399 * Callback to handle sending packets through a real NIC.
402 eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
406 struct rte_mbuf *mbuf;
407 struct pmd_process_private *pp;
408 struct pcap_tx_queue *tx_queue = queue;
410 uint32_t tx_bytes = 0;
413 pp = rte_eth_devices[tx_queue->port_id].process_private;
414 pcap = pp->tx_pcap[tx_queue->queue_id];
416 if (unlikely(nb_pkts == 0 || pcap == NULL))
419 for (i = 0; i < nb_pkts; i++) {
422 if (likely(mbuf->nb_segs == 1)) {
423 ret = pcap_sendpacket(pcap,
424 rte_pktmbuf_mtod(mbuf, u_char *),
427 if (mbuf->pkt_len <= RTE_ETHER_MAX_JUMBO_FRAME_LEN) {
428 eth_pcap_gather_data(tx_pcap_data, mbuf);
429 ret = pcap_sendpacket(pcap,
430 tx_pcap_data, mbuf->pkt_len);
433 "Dropping PCAP packet. Size (%d) > max jumbo size (%d).",
435 RTE_ETHER_MAX_JUMBO_FRAME_LEN);
441 if (unlikely(ret != 0))
444 tx_bytes += mbuf->pkt_len;
445 rte_pktmbuf_free(mbuf);
448 tx_queue->tx_stat.pkts += num_tx;
449 tx_queue->tx_stat.bytes += tx_bytes;
450 tx_queue->tx_stat.err_pkts += nb_pkts - num_tx;
456 * pcap_open_live wrapper function
459 open_iface_live(const char *iface, pcap_t **pcap) {
460 *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
461 RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
464 PMD_LOG(ERR, "Couldn't open %s: %s", iface, errbuf);
472 open_single_iface(const char *iface, pcap_t **pcap)
474 if (open_iface_live(iface, pcap) < 0) {
475 PMD_LOG(ERR, "Couldn't open interface %s", iface);
483 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
488 * We need to create a dummy empty pcap_t to use it
489 * with pcap_dump_open(). We create big enough an Ethernet
492 tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN);
493 if (tx_pcap == NULL) {
494 PMD_LOG(ERR, "Couldn't create dead pcap");
498 /* The dumper is created using the previous pcap_t reference */
499 *dumper = pcap_dump_open(tx_pcap, pcap_filename);
500 if (*dumper == NULL) {
502 PMD_LOG(ERR, "Couldn't open %s for writing.",
512 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
514 *pcap = pcap_open_offline(pcap_filename, errbuf);
516 PMD_LOG(ERR, "Couldn't open %s: %s", pcap_filename,
525 count_packets_in_pcap(pcap_t **pcap, struct pcap_rx_queue *pcap_q)
527 const u_char *packet;
528 struct pcap_pkthdr header;
529 uint64_t pcap_pkt_count = 0;
531 while ((packet = pcap_next(*pcap, &header)))
534 /* The pcap is reopened so it can be used as normal later. */
537 open_single_rx_pcap(pcap_q->name, pcap);
539 return pcap_pkt_count;
543 eth_dev_start(struct rte_eth_dev *dev)
546 struct pmd_internals *internals = dev->data->dev_private;
547 struct pmd_process_private *pp = dev->process_private;
548 struct pcap_tx_queue *tx;
549 struct pcap_rx_queue *rx;
551 /* Special iface case. Single pcap is open and shared between tx/rx. */
552 if (internals->single_iface) {
553 tx = &internals->tx_queue[0];
554 rx = &internals->rx_queue[0];
556 if (!pp->tx_pcap[0] &&
557 strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
558 if (open_single_iface(tx->name, &pp->tx_pcap[0]) < 0)
560 pp->rx_pcap[0] = pp->tx_pcap[0];
566 /* If not open already, open tx pcaps/dumpers */
567 for (i = 0; i < dev->data->nb_tx_queues; i++) {
568 tx = &internals->tx_queue[i];
570 if (!pp->tx_dumper[i] &&
571 strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
572 if (open_single_tx_pcap(tx->name,
573 &pp->tx_dumper[i]) < 0)
575 } else if (!pp->tx_pcap[i] &&
576 strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
577 if (open_single_iface(tx->name, &pp->tx_pcap[i]) < 0)
582 /* If not open already, open rx pcaps */
583 for (i = 0; i < dev->data->nb_rx_queues; i++) {
584 rx = &internals->rx_queue[i];
586 if (pp->rx_pcap[i] != NULL)
589 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
590 if (open_single_rx_pcap(rx->name, &pp->rx_pcap[i]) < 0)
592 } else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
593 if (open_single_iface(rx->name, &pp->rx_pcap[i]) < 0)
599 for (i = 0; i < dev->data->nb_rx_queues; i++)
600 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
602 for (i = 0; i < dev->data->nb_tx_queues; i++)
603 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
605 dev->data->dev_link.link_status = ETH_LINK_UP;
611 * This function gets called when the current port gets stopped.
612 * Is the only place for us to close all the tx streams dumpers.
613 * If not called the dumpers will be flushed within each tx burst.
616 eth_dev_stop(struct rte_eth_dev *dev)
619 struct pmd_internals *internals = dev->data->dev_private;
620 struct pmd_process_private *pp = dev->process_private;
622 /* Special iface case. Single pcap is open and shared between tx/rx. */
623 if (internals->single_iface) {
624 pcap_close(pp->tx_pcap[0]);
625 pp->tx_pcap[0] = NULL;
626 pp->rx_pcap[0] = NULL;
630 for (i = 0; i < dev->data->nb_tx_queues; i++) {
631 if (pp->tx_dumper[i] != NULL) {
632 pcap_dump_close(pp->tx_dumper[i]);
633 pp->tx_dumper[i] = NULL;
636 if (pp->tx_pcap[i] != NULL) {
637 pcap_close(pp->tx_pcap[i]);
638 pp->tx_pcap[i] = NULL;
642 for (i = 0; i < dev->data->nb_rx_queues; i++) {
643 if (pp->rx_pcap[i] != NULL) {
644 pcap_close(pp->rx_pcap[i]);
645 pp->rx_pcap[i] = NULL;
650 for (i = 0; i < dev->data->nb_rx_queues; i++)
651 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
653 for (i = 0; i < dev->data->nb_tx_queues; i++)
654 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
656 dev->data->dev_link.link_status = ETH_LINK_DOWN;
660 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
666 eth_dev_info(struct rte_eth_dev *dev,
667 struct rte_eth_dev_info *dev_info)
669 struct pmd_internals *internals = dev->data->dev_private;
671 dev_info->if_index = internals->if_index;
672 dev_info->max_mac_addrs = 1;
673 dev_info->max_rx_pktlen = (uint32_t) -1;
674 dev_info->max_rx_queues = dev->data->nb_rx_queues;
675 dev_info->max_tx_queues = dev->data->nb_tx_queues;
676 dev_info->min_rx_bufsize = 0;
680 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
683 unsigned long rx_packets_total = 0, rx_bytes_total = 0;
684 unsigned long tx_packets_total = 0, tx_bytes_total = 0;
685 unsigned long tx_packets_err_total = 0;
686 const struct pmd_internals *internal = dev->data->dev_private;
688 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
689 i < dev->data->nb_rx_queues; i++) {
690 stats->q_ipackets[i] = internal->rx_queue[i].rx_stat.pkts;
691 stats->q_ibytes[i] = internal->rx_queue[i].rx_stat.bytes;
692 rx_packets_total += stats->q_ipackets[i];
693 rx_bytes_total += stats->q_ibytes[i];
696 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
697 i < dev->data->nb_tx_queues; i++) {
698 stats->q_opackets[i] = internal->tx_queue[i].tx_stat.pkts;
699 stats->q_obytes[i] = internal->tx_queue[i].tx_stat.bytes;
700 tx_packets_total += stats->q_opackets[i];
701 tx_bytes_total += stats->q_obytes[i];
702 tx_packets_err_total += internal->tx_queue[i].tx_stat.err_pkts;
705 stats->ipackets = rx_packets_total;
706 stats->ibytes = rx_bytes_total;
707 stats->opackets = tx_packets_total;
708 stats->obytes = tx_bytes_total;
709 stats->oerrors = tx_packets_err_total;
715 eth_stats_reset(struct rte_eth_dev *dev)
718 struct pmd_internals *internal = dev->data->dev_private;
720 for (i = 0; i < dev->data->nb_rx_queues; i++) {
721 internal->rx_queue[i].rx_stat.pkts = 0;
722 internal->rx_queue[i].rx_stat.bytes = 0;
725 for (i = 0; i < dev->data->nb_tx_queues; i++) {
726 internal->tx_queue[i].tx_stat.pkts = 0;
727 internal->tx_queue[i].tx_stat.bytes = 0;
728 internal->tx_queue[i].tx_stat.err_pkts = 0;
733 eth_dev_close(struct rte_eth_dev *dev)
736 struct pmd_internals *internals = dev->data->dev_private;
738 /* Device wide flag, but cleanup must be performed per queue. */
739 if (internals->infinite_rx) {
740 for (i = 0; i < dev->data->nb_rx_queues; i++) {
741 struct pcap_rx_queue *pcap_q = &internals->rx_queue[i];
742 struct rte_mbuf *pcap_buf;
744 while (!rte_ring_dequeue(pcap_q->pkts,
746 rte_pktmbuf_free(pcap_buf);
748 rte_ring_free(pcap_q->pkts);
755 eth_queue_release(void *q __rte_unused)
760 eth_link_update(struct rte_eth_dev *dev __rte_unused,
761 int wait_to_complete __rte_unused)
767 eth_rx_queue_setup(struct rte_eth_dev *dev,
768 uint16_t rx_queue_id,
769 uint16_t nb_rx_desc __rte_unused,
770 unsigned int socket_id __rte_unused,
771 const struct rte_eth_rxconf *rx_conf __rte_unused,
772 struct rte_mempool *mb_pool)
774 struct pmd_internals *internals = dev->data->dev_private;
775 struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
777 pcap_q->mb_pool = mb_pool;
778 pcap_q->port_id = dev->data->port_id;
779 pcap_q->queue_id = rx_queue_id;
780 dev->data->rx_queues[rx_queue_id] = pcap_q;
782 if (internals->infinite_rx) {
783 struct pmd_process_private *pp;
784 char ring_name[NAME_MAX];
785 static uint32_t ring_number;
786 uint64_t pcap_pkt_count = 0;
787 struct rte_mbuf *bufs[1];
790 pp = rte_eth_devices[pcap_q->port_id].process_private;
791 pcap = &pp->rx_pcap[pcap_q->queue_id];
793 if (unlikely(*pcap == NULL))
796 pcap_pkt_count = count_packets_in_pcap(pcap, pcap_q);
798 snprintf(ring_name, sizeof(ring_name), "PCAP_RING%" PRIu16,
801 pcap_q->pkts = rte_ring_create(ring_name,
802 rte_align64pow2(pcap_pkt_count + 1), 0,
803 RING_F_SP_ENQ | RING_F_SC_DEQ);
808 /* Fill ring with packets from PCAP file one by one. */
809 while (eth_pcap_rx(pcap_q, bufs, 1)) {
810 /* Check for multiseg mbufs. */
811 if (bufs[0]->nb_segs != 1) {
812 rte_pktmbuf_free(*bufs);
814 while (!rte_ring_dequeue(pcap_q->pkts,
816 rte_pktmbuf_free(*bufs);
818 rte_ring_free(pcap_q->pkts);
819 PMD_LOG(ERR, "Multiseg mbufs are not supported in infinite_rx "
824 rte_ring_enqueue_bulk(pcap_q->pkts,
825 (void * const *)bufs, 1, NULL);
828 * Reset the stats for this queue since eth_pcap_rx calls above
829 * didn't result in the application receiving packets.
831 pcap_q->rx_stat.pkts = 0;
832 pcap_q->rx_stat.bytes = 0;
839 eth_tx_queue_setup(struct rte_eth_dev *dev,
840 uint16_t tx_queue_id,
841 uint16_t nb_tx_desc __rte_unused,
842 unsigned int socket_id __rte_unused,
843 const struct rte_eth_txconf *tx_conf __rte_unused)
845 struct pmd_internals *internals = dev->data->dev_private;
846 struct pcap_tx_queue *pcap_q = &internals->tx_queue[tx_queue_id];
848 pcap_q->port_id = dev->data->port_id;
849 pcap_q->queue_id = tx_queue_id;
850 dev->data->tx_queues[tx_queue_id] = pcap_q;
856 eth_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
858 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
864 eth_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
866 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
872 eth_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
874 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
880 eth_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
882 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
887 static const struct eth_dev_ops ops = {
888 .dev_start = eth_dev_start,
889 .dev_stop = eth_dev_stop,
890 .dev_close = eth_dev_close,
891 .dev_configure = eth_dev_configure,
892 .dev_infos_get = eth_dev_info,
893 .rx_queue_setup = eth_rx_queue_setup,
894 .tx_queue_setup = eth_tx_queue_setup,
895 .rx_queue_start = eth_rx_queue_start,
896 .tx_queue_start = eth_tx_queue_start,
897 .rx_queue_stop = eth_rx_queue_stop,
898 .tx_queue_stop = eth_tx_queue_stop,
899 .rx_queue_release = eth_queue_release,
900 .tx_queue_release = eth_queue_release,
901 .link_update = eth_link_update,
902 .stats_get = eth_stats_get,
903 .stats_reset = eth_stats_reset,
907 add_queue(struct pmd_devargs *pmd, const char *name, const char *type,
908 pcap_t *pcap, pcap_dumper_t *dumper)
910 if (pmd->num_of_queue >= RTE_PMD_PCAP_MAX_QUEUES)
913 pmd->queue[pmd->num_of_queue].pcap = pcap;
915 pmd->queue[pmd->num_of_queue].dumper = dumper;
916 pmd->queue[pmd->num_of_queue].name = name;
917 pmd->queue[pmd->num_of_queue].type = type;
923 * Function handler that opens the pcap file for reading a stores a
924 * reference of it for use it later on.
927 open_rx_pcap(const char *key, const char *value, void *extra_args)
929 const char *pcap_filename = value;
930 struct pmd_devargs *rx = extra_args;
933 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
936 if (add_queue(rx, pcap_filename, key, pcap, NULL) < 0) {
945 * Opens a pcap file for writing and stores a reference to it
946 * for use it later on.
949 open_tx_pcap(const char *key, const char *value, void *extra_args)
951 const char *pcap_filename = value;
952 struct pmd_devargs *dumpers = extra_args;
953 pcap_dumper_t *dumper;
955 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
958 if (add_queue(dumpers, pcap_filename, key, NULL, dumper) < 0) {
959 pcap_dump_close(dumper);
967 * Opens an interface for reading and writing
970 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
972 const char *iface = value;
973 struct pmd_devargs *tx = extra_args;
976 if (open_single_iface(iface, &pcap) < 0)
979 tx->queue[0].pcap = pcap;
980 tx->queue[0].name = iface;
981 tx->queue[0].type = key;
987 set_iface_direction(const char *iface, pcap_t *pcap,
988 pcap_direction_t direction)
990 const char *direction_str = (direction == PCAP_D_IN) ? "IN" : "OUT";
991 if (pcap_setdirection(pcap, direction) < 0) {
992 PMD_LOG(ERR, "Setting %s pcap direction %s failed - %s\n",
993 iface, direction_str, pcap_geterr(pcap));
996 PMD_LOG(INFO, "Setting %s pcap direction %s\n",
997 iface, direction_str);
1002 open_iface(const char *key, const char *value, void *extra_args)
1004 const char *iface = value;
1005 struct pmd_devargs *pmd = extra_args;
1006 pcap_t *pcap = NULL;
1008 if (open_single_iface(iface, &pcap) < 0)
1010 if (add_queue(pmd, iface, key, pcap, NULL) < 0) {
1019 * Opens a NIC for reading packets from it
1022 open_rx_iface(const char *key, const char *value, void *extra_args)
1024 int ret = open_iface(key, value, extra_args);
1027 if (strcmp(key, ETH_PCAP_RX_IFACE_IN_ARG) == 0) {
1028 struct pmd_devargs *pmd = extra_args;
1029 unsigned int qid = pmd->num_of_queue - 1;
1031 set_iface_direction(pmd->queue[qid].name,
1032 pmd->queue[qid].pcap,
1040 rx_iface_args_process(const char *key, const char *value, void *extra_args)
1042 if (strcmp(key, ETH_PCAP_RX_IFACE_ARG) == 0 ||
1043 strcmp(key, ETH_PCAP_RX_IFACE_IN_ARG) == 0)
1044 return open_rx_iface(key, value, extra_args);
1050 * Opens a NIC for writing packets to it
1053 open_tx_iface(const char *key, const char *value, void *extra_args)
1055 return open_iface(key, value, extra_args);
1059 select_phy_mac(const char *key __rte_unused, const char *value,
1063 const int phy_mac = atoi(value);
1064 int *enable_phy_mac = extra_args;
1067 *enable_phy_mac = 1;
1073 get_infinite_rx_arg(const char *key __rte_unused,
1074 const char *value, void *extra_args)
1077 const int infinite_rx = atoi(value);
1078 int *enable_infinite_rx = extra_args;
1080 if (infinite_rx > 0)
1081 *enable_infinite_rx = 1;
1087 pmd_init_internals(struct rte_vdev_device *vdev,
1088 const unsigned int nb_rx_queues,
1089 const unsigned int nb_tx_queues,
1090 struct pmd_internals **internals,
1091 struct rte_eth_dev **eth_dev)
1093 struct rte_eth_dev_data *data;
1094 struct pmd_process_private *pp;
1095 unsigned int numa_node = vdev->device.numa_node;
1097 PMD_LOG(INFO, "Creating pcap-backed ethdev on numa socket %d",
1100 pp = (struct pmd_process_private *)
1101 rte_zmalloc(NULL, sizeof(struct pmd_process_private),
1102 RTE_CACHE_LINE_SIZE);
1106 "Failed to allocate memory for process private");
1110 /* reserve an ethdev entry */
1111 *eth_dev = rte_eth_vdev_allocate(vdev, sizeof(**internals));
1116 (*eth_dev)->process_private = pp;
1117 /* now put it all together
1118 * - store queue data in internals,
1119 * - store numa_node info in eth_dev
1120 * - point eth_dev_data to internals
1121 * - and point eth_dev structure to new eth_dev_data structure
1123 *internals = (*eth_dev)->data->dev_private;
1125 * Interface MAC = 02:70:63:61:70:<iface_idx>
1126 * derived from: 'locally administered':'p':'c':'a':'p':'iface_idx'
1127 * where the middle 4 characters are converted to hex.
1129 (*internals)->eth_addr = (struct rte_ether_addr) {
1130 .addr_bytes = { 0x02, 0x70, 0x63, 0x61, 0x70, iface_idx++ }
1132 (*internals)->phy_mac = 0;
1133 data = (*eth_dev)->data;
1134 data->nb_rx_queues = (uint16_t)nb_rx_queues;
1135 data->nb_tx_queues = (uint16_t)nb_tx_queues;
1136 data->dev_link = pmd_link;
1137 data->mac_addrs = &(*internals)->eth_addr;
1140 * NOTE: we'll replace the data element, of originally allocated
1141 * eth_dev so the rings are local per-process
1143 (*eth_dev)->dev_ops = &ops;
1145 strlcpy((*internals)->devargs, rte_vdev_device_args(vdev),
1146 ETH_PCAP_ARG_MAXLEN);
1152 eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,
1153 const unsigned int numa_node)
1155 #if defined(RTE_EXEC_ENV_LINUX)
1158 int if_fd = socket(AF_INET, SOCK_DGRAM, 0);
1163 rte_strscpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name));
1164 if (ioctl(if_fd, SIOCGIFHWADDR, &ifr)) {
1169 mac_addrs = rte_zmalloc_socket(NULL, RTE_ETHER_ADDR_LEN, 0, numa_node);
1175 PMD_LOG(INFO, "Setting phy MAC for %s", if_name);
1176 eth_dev->data->mac_addrs = mac_addrs;
1177 rte_memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
1178 ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
1184 #elif defined(RTE_EXEC_ENV_FREEBSD)
1186 struct if_msghdr *ifm;
1187 struct sockaddr_dl *sdl;
1196 mib[4] = NET_RT_IFLIST;
1197 mib[5] = if_nametoindex(if_name);
1199 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
1205 buf = rte_malloc(NULL, len, 0);
1209 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
1213 ifm = (struct if_msghdr *)buf;
1214 sdl = (struct sockaddr_dl *)(ifm + 1);
1216 mac_addrs = rte_zmalloc_socket(NULL, RTE_ETHER_ADDR_LEN, 0, numa_node);
1222 PMD_LOG(INFO, "Setting phy MAC for %s", if_name);
1223 eth_dev->data->mac_addrs = mac_addrs;
1224 rte_memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
1225 LLADDR(sdl), RTE_ETHER_ADDR_LEN);
1236 eth_from_pcaps_common(struct rte_vdev_device *vdev,
1237 struct pmd_devargs_all *devargs_all,
1238 struct pmd_internals **internals, struct rte_eth_dev **eth_dev)
1240 struct pmd_process_private *pp;
1241 struct pmd_devargs *rx_queues = &devargs_all->rx_queues;
1242 struct pmd_devargs *tx_queues = &devargs_all->tx_queues;
1243 const unsigned int nb_rx_queues = rx_queues->num_of_queue;
1244 const unsigned int nb_tx_queues = tx_queues->num_of_queue;
1247 /* do some parameter checking */
1248 if (rx_queues == NULL && nb_rx_queues > 0)
1250 if (tx_queues == NULL && nb_tx_queues > 0)
1253 if (pmd_init_internals(vdev, nb_rx_queues, nb_tx_queues, internals,
1257 pp = (*eth_dev)->process_private;
1258 for (i = 0; i < nb_rx_queues; i++) {
1259 struct pcap_rx_queue *rx = &(*internals)->rx_queue[i];
1260 struct devargs_queue *queue = &rx_queues->queue[i];
1262 pp->rx_pcap[i] = queue->pcap;
1263 strlcpy(rx->name, queue->name, sizeof(rx->name));
1264 strlcpy(rx->type, queue->type, sizeof(rx->type));
1267 for (i = 0; i < nb_tx_queues; i++) {
1268 struct pcap_tx_queue *tx = &(*internals)->tx_queue[i];
1269 struct devargs_queue *queue = &tx_queues->queue[i];
1271 pp->tx_dumper[i] = queue->dumper;
1272 pp->tx_pcap[i] = queue->pcap;
1273 strlcpy(tx->name, queue->name, sizeof(tx->name));
1274 strlcpy(tx->type, queue->type, sizeof(tx->type));
1281 eth_from_pcaps(struct rte_vdev_device *vdev,
1282 struct pmd_devargs_all *devargs_all)
1284 struct pmd_internals *internals = NULL;
1285 struct rte_eth_dev *eth_dev = NULL;
1286 struct pmd_devargs *rx_queues = &devargs_all->rx_queues;
1287 int single_iface = devargs_all->single_iface;
1288 unsigned int infinite_rx = devargs_all->infinite_rx;
1291 ret = eth_from_pcaps_common(vdev, devargs_all, &internals, ð_dev);
1296 /* store weather we are using a single interface for rx/tx or not */
1297 internals->single_iface = single_iface;
1300 internals->if_index = if_nametoindex(rx_queues->queue[0].name);
1302 /* phy_mac arg is applied only only if "iface" devarg is provided */
1303 if (rx_queues->phy_mac) {
1304 int ret = eth_pcap_update_mac(rx_queues->queue[0].name,
1305 eth_dev, vdev->device.numa_node);
1307 internals->phy_mac = 1;
1311 internals->infinite_rx = infinite_rx;
1312 /* Assign rx ops. */
1314 eth_dev->rx_pkt_burst = eth_pcap_rx_infinite;
1316 eth_dev->rx_pkt_burst = eth_pcap_rx;
1318 /* Assign tx ops. */
1319 if (devargs_all->is_tx_pcap)
1320 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
1321 else if (devargs_all->is_tx_iface)
1322 eth_dev->tx_pkt_burst = eth_pcap_tx;
1324 eth_dev->tx_pkt_burst = eth_tx_drop;
1326 rte_eth_dev_probing_finish(eth_dev);
1331 pmd_pcap_probe(struct rte_vdev_device *dev)
1334 unsigned int is_rx_pcap = 0;
1335 struct rte_kvargs *kvlist;
1336 struct pmd_devargs pcaps = {0};
1337 struct pmd_devargs dumpers = {0};
1338 struct rte_eth_dev *eth_dev = NULL;
1339 struct pmd_internals *internal;
1342 struct pmd_devargs_all devargs_all = {
1349 name = rte_vdev_device_name(dev);
1350 PMD_LOG(INFO, "Initializing pmd_pcap for %s", name);
1352 gettimeofday(&start_time, NULL);
1353 start_cycles = rte_get_timer_cycles();
1354 hz = rte_get_timer_hz();
1356 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1357 eth_dev = rte_eth_dev_attach_secondary(name);
1359 PMD_LOG(ERR, "Failed to probe %s", name);
1363 internal = eth_dev->data->dev_private;
1365 kvlist = rte_kvargs_parse(internal->devargs, valid_arguments);
1369 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev),
1376 * If iface argument is passed we open the NICs and use them for
1379 if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
1381 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
1382 &open_rx_tx_iface, &pcaps);
1386 dumpers.queue[0] = pcaps.queue[0];
1388 ret = rte_kvargs_process(kvlist, ETH_PCAP_PHY_MAC_ARG,
1389 &select_phy_mac, &pcaps.phy_mac);
1393 dumpers.phy_mac = pcaps.phy_mac;
1395 devargs_all.single_iface = 1;
1396 pcaps.num_of_queue = 1;
1397 dumpers.num_of_queue = 1;
1403 * We check whether we want to open a RX stream from a real NIC or a
1406 is_rx_pcap = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG) ? 1 : 0;
1407 pcaps.num_of_queue = 0;
1411 * We check whether we want to infinitely rx the pcap file.
1413 unsigned int infinite_rx_arg_cnt = rte_kvargs_count(kvlist,
1414 ETH_PCAP_INFINITE_RX_ARG);
1416 if (infinite_rx_arg_cnt == 1) {
1417 ret = rte_kvargs_process(kvlist,
1418 ETH_PCAP_INFINITE_RX_ARG,
1419 &get_infinite_rx_arg,
1420 &devargs_all.infinite_rx);
1423 PMD_LOG(INFO, "infinite_rx has been %s for %s",
1424 devargs_all.infinite_rx ? "enabled" : "disabled",
1427 } else if (infinite_rx_arg_cnt > 1) {
1428 PMD_LOG(WARNING, "infinite_rx has not been enabled since the "
1429 "argument has been provided more than once "
1433 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
1434 &open_rx_pcap, &pcaps);
1436 ret = rte_kvargs_process(kvlist, NULL,
1437 &rx_iface_args_process, &pcaps);
1444 * We check whether we want to open a TX stream to a real NIC,
1445 * a pcap file, or drop packets on tx
1447 devargs_all.is_tx_pcap =
1448 rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG) ? 1 : 0;
1449 devargs_all.is_tx_iface =
1450 rte_kvargs_count(kvlist, ETH_PCAP_TX_IFACE_ARG) ? 1 : 0;
1451 dumpers.num_of_queue = 0;
1453 if (devargs_all.is_tx_pcap) {
1454 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1455 &open_tx_pcap, &dumpers);
1456 } else if (devargs_all.is_tx_iface) {
1457 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1458 &open_tx_iface, &dumpers);
1462 PMD_LOG(INFO, "Dropping packets on tx since no tx queues were provided.");
1464 /* Add 1 dummy queue per rxq which counts and drops packets. */
1465 for (i = 0; i < pcaps.num_of_queue; i++)
1466 ret = add_queue(&dumpers, "dummy", "tx_drop", NULL,
1474 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1475 struct pmd_process_private *pp;
1478 internal = eth_dev->data->dev_private;
1479 pp = (struct pmd_process_private *)
1481 sizeof(struct pmd_process_private),
1482 RTE_CACHE_LINE_SIZE);
1486 "Failed to allocate memory for process private");
1491 eth_dev->dev_ops = &ops;
1492 eth_dev->device = &dev->device;
1494 /* setup process private */
1495 for (i = 0; i < pcaps.num_of_queue; i++)
1496 pp->rx_pcap[i] = pcaps.queue[i].pcap;
1498 for (i = 0; i < dumpers.num_of_queue; i++) {
1499 pp->tx_dumper[i] = dumpers.queue[i].dumper;
1500 pp->tx_pcap[i] = dumpers.queue[i].pcap;
1503 eth_dev->process_private = pp;
1504 eth_dev->rx_pkt_burst = eth_pcap_rx;
1505 if (devargs_all.is_tx_pcap)
1506 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
1508 eth_dev->tx_pkt_burst = eth_pcap_tx;
1510 rte_eth_dev_probing_finish(eth_dev);
1514 devargs_all.rx_queues = pcaps;
1515 devargs_all.tx_queues = dumpers;
1517 ret = eth_from_pcaps(dev, &devargs_all);
1520 rte_kvargs_free(kvlist);
1526 pmd_pcap_remove(struct rte_vdev_device *dev)
1528 struct pmd_internals *internals = NULL;
1529 struct rte_eth_dev *eth_dev = NULL;
1531 PMD_LOG(INFO, "Closing pcap ethdev on numa socket %d",
1537 /* reserve an ethdev entry */
1538 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1539 if (eth_dev == NULL)
1542 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1543 internals = eth_dev->data->dev_private;
1544 if (internals != NULL && internals->phy_mac == 0)
1545 /* not dynamically allocated, must not be freed */
1546 eth_dev->data->mac_addrs = NULL;
1549 eth_dev_close(eth_dev);
1551 rte_free(eth_dev->process_private);
1552 rte_eth_dev_release_port(eth_dev);
1557 static struct rte_vdev_driver pmd_pcap_drv = {
1558 .probe = pmd_pcap_probe,
1559 .remove = pmd_pcap_remove,
1562 RTE_PMD_REGISTER_VDEV(net_pcap, pmd_pcap_drv);
1563 RTE_PMD_REGISTER_ALIAS(net_pcap, eth_pcap);
1564 RTE_PMD_REGISTER_PARAM_STRING(net_pcap,
1565 ETH_PCAP_RX_PCAP_ARG "=<string> "
1566 ETH_PCAP_TX_PCAP_ARG "=<string> "
1567 ETH_PCAP_RX_IFACE_ARG "=<ifc> "
1568 ETH_PCAP_RX_IFACE_IN_ARG "=<ifc> "
1569 ETH_PCAP_TX_IFACE_ARG "=<ifc> "
1570 ETH_PCAP_IFACE_ARG "=<ifc> "
1571 ETH_PCAP_PHY_MAC_ARG "=<int>"
1572 ETH_PCAP_INFINITE_RX_ARG "=<0|1>");
1574 RTE_INIT(eth_pcap_init_log)
1576 eth_pcap_logtype = rte_log_register("pmd.net.pcap");
1577 if (eth_pcap_logtype >= 0)
1578 rte_log_set_level(eth_pcap_logtype, RTE_LOG_NOTICE);