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 struct timeval start_time;
50 static uint64_t start_cycles;
52 static uint8_t iface_idx;
55 volatile unsigned long pkts;
56 volatile unsigned long bytes;
57 volatile unsigned long err_pkts;
60 struct pcap_rx_queue {
63 struct rte_mempool *mb_pool;
64 struct queue_stat rx_stat;
66 char type[ETH_PCAP_ARG_MAXLEN];
68 /* Contains pre-generated packets to be looped through */
69 struct rte_ring *pkts;
72 struct pcap_tx_queue {
75 struct queue_stat tx_stat;
77 char type[ETH_PCAP_ARG_MAXLEN];
80 struct pmd_internals {
81 struct pcap_rx_queue rx_queue[RTE_PMD_PCAP_MAX_QUEUES];
82 struct pcap_tx_queue tx_queue[RTE_PMD_PCAP_MAX_QUEUES];
83 char devargs[ETH_PCAP_ARG_MAXLEN];
84 struct rte_ether_addr eth_addr;
88 unsigned int infinite_rx;
91 struct pmd_process_private {
92 pcap_t *rx_pcap[RTE_PMD_PCAP_MAX_QUEUES];
93 pcap_t *tx_pcap[RTE_PMD_PCAP_MAX_QUEUES];
94 pcap_dumper_t *tx_dumper[RTE_PMD_PCAP_MAX_QUEUES];
98 unsigned int num_of_queue;
99 struct devargs_queue {
100 pcap_dumper_t *dumper;
104 } queue[RTE_PMD_PCAP_MAX_QUEUES];
108 struct pmd_devargs_all {
109 struct pmd_devargs rx_queues;
110 struct pmd_devargs tx_queues;
112 unsigned int is_tx_pcap;
113 unsigned int is_tx_iface;
114 unsigned int is_rx_pcap;
115 unsigned int is_rx_iface;
116 unsigned int infinite_rx;
119 static const char *valid_arguments[] = {
120 ETH_PCAP_RX_PCAP_ARG,
121 ETH_PCAP_TX_PCAP_ARG,
122 ETH_PCAP_RX_IFACE_ARG,
123 ETH_PCAP_RX_IFACE_IN_ARG,
124 ETH_PCAP_TX_IFACE_ARG,
126 ETH_PCAP_PHY_MAC_ARG,
127 ETH_PCAP_INFINITE_RX_ARG,
131 static struct rte_eth_link pmd_link = {
132 .link_speed = ETH_SPEED_NUM_10G,
133 .link_duplex = ETH_LINK_FULL_DUPLEX,
134 .link_status = ETH_LINK_DOWN,
135 .link_autoneg = ETH_LINK_FIXED,
138 RTE_LOG_REGISTER(eth_pcap_logtype, pmd.net.pcap, NOTICE);
140 #define PMD_LOG(level, fmt, args...) \
141 rte_log(RTE_LOG_ ## level, eth_pcap_logtype, \
142 "%s(): " fmt "\n", __func__, ##args)
145 eth_pcap_rx_jumbo(struct rte_mempool *mb_pool, struct rte_mbuf *mbuf,
146 const u_char *data, uint16_t data_len)
148 /* Copy the first segment. */
149 uint16_t len = rte_pktmbuf_tailroom(mbuf);
150 struct rte_mbuf *m = mbuf;
152 rte_memcpy(rte_pktmbuf_append(mbuf, len), data, len);
156 while (data_len > 0) {
157 /* Allocate next mbuf and point to that. */
158 m->next = rte_pktmbuf_alloc(mb_pool);
160 if (unlikely(!m->next))
165 /* Headroom is not needed in chained mbufs. */
166 rte_pktmbuf_prepend(m, rte_pktmbuf_headroom(m));
170 /* Copy next segment. */
171 len = RTE_MIN(rte_pktmbuf_tailroom(m), data_len);
172 rte_memcpy(rte_pktmbuf_append(m, len), data, len);
179 return mbuf->nb_segs;
183 eth_pcap_rx_infinite(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
186 struct pcap_rx_queue *pcap_q = queue;
187 uint32_t rx_bytes = 0;
189 if (unlikely(nb_pkts == 0))
192 if (rte_pktmbuf_alloc_bulk(pcap_q->mb_pool, bufs, nb_pkts) != 0)
195 for (i = 0; i < nb_pkts; i++) {
196 struct rte_mbuf *pcap_buf;
197 int err = rte_ring_dequeue(pcap_q->pkts, (void **)&pcap_buf);
201 rte_memcpy(rte_pktmbuf_mtod(bufs[i], void *),
202 rte_pktmbuf_mtod(pcap_buf, void *),
204 bufs[i]->data_len = pcap_buf->data_len;
205 bufs[i]->pkt_len = pcap_buf->pkt_len;
206 bufs[i]->port = pcap_q->port_id;
207 rx_bytes += pcap_buf->data_len;
209 /* Enqueue packet back on ring to allow infinite rx. */
210 rte_ring_enqueue(pcap_q->pkts, pcap_buf);
213 pcap_q->rx_stat.pkts += i;
214 pcap_q->rx_stat.bytes += rx_bytes;
220 eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
223 struct pcap_pkthdr header;
224 struct pmd_process_private *pp;
225 const u_char *packet;
226 struct rte_mbuf *mbuf;
227 struct pcap_rx_queue *pcap_q = queue;
229 uint32_t rx_bytes = 0;
232 pp = rte_eth_devices[pcap_q->port_id].process_private;
233 pcap = pp->rx_pcap[pcap_q->queue_id];
235 if (unlikely(pcap == NULL || nb_pkts == 0))
238 /* Reads the given number of packets from the pcap file one by one
239 * and copies the packet data into a newly allocated mbuf to return.
241 for (i = 0; i < nb_pkts; i++) {
242 /* Get the next PCAP packet */
243 packet = pcap_next(pcap, &header);
244 if (unlikely(packet == NULL))
247 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
248 if (unlikely(mbuf == NULL))
251 if (header.caplen <= rte_pktmbuf_tailroom(mbuf)) {
252 /* pcap packet will fit in the mbuf, can copy it */
253 rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), packet,
255 mbuf->data_len = (uint16_t)header.caplen;
257 /* Try read jumbo frame into multi mbufs. */
258 if (unlikely(eth_pcap_rx_jumbo(pcap_q->mb_pool,
261 header.caplen) == -1)) {
262 rte_pktmbuf_free(mbuf);
267 mbuf->pkt_len = (uint16_t)header.caplen;
268 mbuf->timestamp = (uint64_t)header.ts.tv_sec * 1000000
270 mbuf->ol_flags |= PKT_RX_TIMESTAMP;
271 mbuf->port = pcap_q->port_id;
274 rx_bytes += header.caplen;
276 pcap_q->rx_stat.pkts += num_rx;
277 pcap_q->rx_stat.bytes += rx_bytes;
283 eth_null_rx(void *queue __rte_unused,
284 struct rte_mbuf **bufs __rte_unused,
285 uint16_t nb_pkts __rte_unused)
290 #define NSEC_PER_SEC 1000000000L
293 calculate_timestamp(struct timeval *ts) {
295 struct timeval cur_time;
297 cycles = rte_get_timer_cycles() - start_cycles;
298 cur_time.tv_sec = cycles / hz;
299 cur_time.tv_usec = (cycles % hz) * NSEC_PER_SEC / hz;
301 ts->tv_sec = start_time.tv_sec + cur_time.tv_sec;
302 ts->tv_usec = start_time.tv_usec + cur_time.tv_usec;
303 if (ts->tv_usec >= NSEC_PER_SEC) {
304 ts->tv_usec -= NSEC_PER_SEC;
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;
323 unsigned char temp_data[RTE_ETH_PCAP_SNAPLEN];
326 pp = rte_eth_devices[dumper_q->port_id].process_private;
327 dumper = pp->tx_dumper[dumper_q->queue_id];
329 if (dumper == NULL || nb_pkts == 0)
332 /* writes the nb_pkts packets to the previously opened pcap file
334 for (i = 0; i < nb_pkts; i++) {
336 len = caplen = rte_pktmbuf_pkt_len(mbuf);
337 if (unlikely(!rte_pktmbuf_is_contiguous(mbuf) &&
338 len > sizeof(temp_data))) {
339 caplen = sizeof(temp_data);
342 calculate_timestamp(&header.ts);
344 header.caplen = caplen;
345 /* rte_pktmbuf_read() returns a pointer to the data directly
346 * in the mbuf (when the mbuf is contiguous) or, otherwise,
347 * a pointer to temp_data after copying into it.
349 pcap_dump((u_char *)dumper, &header,
350 rte_pktmbuf_read(mbuf, 0, caplen, temp_data));
354 rte_pktmbuf_free(mbuf);
358 * Since there's no place to hook a callback when the forwarding
359 * process stops and to make sure the pcap file is actually written,
360 * we flush the pcap dumper within each burst.
362 pcap_dump_flush(dumper);
363 dumper_q->tx_stat.pkts += num_tx;
364 dumper_q->tx_stat.bytes += tx_bytes;
365 dumper_q->tx_stat.err_pkts += nb_pkts - num_tx;
371 * Callback to handle dropping packets in the infinite rx case.
374 eth_tx_drop(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
377 uint32_t tx_bytes = 0;
378 struct pcap_tx_queue *tx_queue = queue;
380 if (unlikely(nb_pkts == 0))
383 for (i = 0; i < nb_pkts; i++) {
384 tx_bytes += bufs[i]->data_len;
385 rte_pktmbuf_free(bufs[i]);
388 tx_queue->tx_stat.pkts += nb_pkts;
389 tx_queue->tx_stat.bytes += tx_bytes;
395 * Callback to handle sending packets through a real NIC.
398 eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
402 struct rte_mbuf *mbuf;
403 struct pmd_process_private *pp;
404 struct pcap_tx_queue *tx_queue = queue;
406 uint32_t tx_bytes = 0;
408 unsigned char temp_data[RTE_ETH_PCAP_SNAPLEN];
411 pp = rte_eth_devices[tx_queue->port_id].process_private;
412 pcap = pp->tx_pcap[tx_queue->queue_id];
414 if (unlikely(nb_pkts == 0 || pcap == NULL))
417 for (i = 0; i < nb_pkts; i++) {
419 len = rte_pktmbuf_pkt_len(mbuf);
420 if (unlikely(!rte_pktmbuf_is_contiguous(mbuf) &&
421 len > sizeof(temp_data))) {
423 "Dropping multi segment PCAP packet. Size (%zd) > max size (%zd).",
424 len, sizeof(temp_data));
425 rte_pktmbuf_free(mbuf);
429 /* rte_pktmbuf_read() returns a pointer to the data directly
430 * in the mbuf (when the mbuf is contiguous) or, otherwise,
431 * a pointer to temp_data after copying into it.
433 ret = pcap_sendpacket(pcap,
434 rte_pktmbuf_read(mbuf, 0, len, temp_data), len);
435 if (unlikely(ret != 0))
439 rte_pktmbuf_free(mbuf);
442 tx_queue->tx_stat.pkts += num_tx;
443 tx_queue->tx_stat.bytes += tx_bytes;
444 tx_queue->tx_stat.err_pkts += i - num_tx;
450 * pcap_open_live wrapper function
453 open_iface_live(const char *iface, pcap_t **pcap) {
454 *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
455 RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
458 PMD_LOG(ERR, "Couldn't open %s: %s", iface, errbuf);
466 open_single_iface(const char *iface, pcap_t **pcap)
468 if (open_iface_live(iface, pcap) < 0) {
469 PMD_LOG(ERR, "Couldn't open interface %s", iface);
477 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
482 * We need to create a dummy empty pcap_t to use it
483 * with pcap_dump_open(). We create big enough an Ethernet
486 tx_pcap = pcap_open_dead_with_tstamp_precision(DLT_EN10MB,
487 RTE_ETH_PCAP_SNAPSHOT_LEN, PCAP_TSTAMP_PRECISION_NANO);
488 if (tx_pcap == NULL) {
489 PMD_LOG(ERR, "Couldn't create dead pcap");
493 /* The dumper is created using the previous pcap_t reference */
494 *dumper = pcap_dump_open(tx_pcap, pcap_filename);
495 if (*dumper == NULL) {
497 PMD_LOG(ERR, "Couldn't open %s for writing.",
507 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
509 *pcap = pcap_open_offline(pcap_filename, errbuf);
511 PMD_LOG(ERR, "Couldn't open %s: %s", pcap_filename,
520 count_packets_in_pcap(pcap_t **pcap, struct pcap_rx_queue *pcap_q)
522 const u_char *packet;
523 struct pcap_pkthdr header;
524 uint64_t pcap_pkt_count = 0;
526 while ((packet = pcap_next(*pcap, &header)))
529 /* The pcap is reopened so it can be used as normal later. */
532 open_single_rx_pcap(pcap_q->name, pcap);
534 return pcap_pkt_count;
538 eth_dev_start(struct rte_eth_dev *dev)
541 struct pmd_internals *internals = dev->data->dev_private;
542 struct pmd_process_private *pp = dev->process_private;
543 struct pcap_tx_queue *tx;
544 struct pcap_rx_queue *rx;
546 /* Special iface case. Single pcap is open and shared between tx/rx. */
547 if (internals->single_iface) {
548 tx = &internals->tx_queue[0];
549 rx = &internals->rx_queue[0];
551 if (!pp->tx_pcap[0] &&
552 strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
553 if (open_single_iface(tx->name, &pp->tx_pcap[0]) < 0)
555 pp->rx_pcap[0] = pp->tx_pcap[0];
561 /* If not open already, open tx pcaps/dumpers */
562 for (i = 0; i < dev->data->nb_tx_queues; i++) {
563 tx = &internals->tx_queue[i];
565 if (!pp->tx_dumper[i] &&
566 strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
567 if (open_single_tx_pcap(tx->name,
568 &pp->tx_dumper[i]) < 0)
570 } else if (!pp->tx_pcap[i] &&
571 strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
572 if (open_single_iface(tx->name, &pp->tx_pcap[i]) < 0)
577 /* If not open already, open rx pcaps */
578 for (i = 0; i < dev->data->nb_rx_queues; i++) {
579 rx = &internals->rx_queue[i];
581 if (pp->rx_pcap[i] != NULL)
584 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
585 if (open_single_rx_pcap(rx->name, &pp->rx_pcap[i]) < 0)
587 } else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
588 if (open_single_iface(rx->name, &pp->rx_pcap[i]) < 0)
594 for (i = 0; i < dev->data->nb_rx_queues; i++)
595 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
597 for (i = 0; i < dev->data->nb_tx_queues; i++)
598 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
600 dev->data->dev_link.link_status = ETH_LINK_UP;
606 * This function gets called when the current port gets stopped.
607 * Is the only place for us to close all the tx streams dumpers.
608 * If not called the dumpers will be flushed within each tx burst.
611 eth_dev_stop(struct rte_eth_dev *dev)
614 struct pmd_internals *internals = dev->data->dev_private;
615 struct pmd_process_private *pp = dev->process_private;
617 /* Special iface case. Single pcap is open and shared between tx/rx. */
618 if (internals->single_iface) {
619 pcap_close(pp->tx_pcap[0]);
620 pp->tx_pcap[0] = NULL;
621 pp->rx_pcap[0] = NULL;
625 for (i = 0; i < dev->data->nb_tx_queues; i++) {
626 if (pp->tx_dumper[i] != NULL) {
627 pcap_dump_close(pp->tx_dumper[i]);
628 pp->tx_dumper[i] = NULL;
631 if (pp->tx_pcap[i] != NULL) {
632 pcap_close(pp->tx_pcap[i]);
633 pp->tx_pcap[i] = NULL;
637 for (i = 0; i < dev->data->nb_rx_queues; i++) {
638 if (pp->rx_pcap[i] != NULL) {
639 pcap_close(pp->rx_pcap[i]);
640 pp->rx_pcap[i] = NULL;
645 for (i = 0; i < dev->data->nb_rx_queues; i++)
646 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
648 for (i = 0; i < dev->data->nb_tx_queues; i++)
649 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
651 dev->data->dev_link.link_status = ETH_LINK_DOWN;
655 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
661 eth_dev_info(struct rte_eth_dev *dev,
662 struct rte_eth_dev_info *dev_info)
664 struct pmd_internals *internals = dev->data->dev_private;
666 dev_info->if_index = internals->if_index;
667 dev_info->max_mac_addrs = 1;
668 dev_info->max_rx_pktlen = (uint32_t) -1;
669 dev_info->max_rx_queues = dev->data->nb_rx_queues;
670 dev_info->max_tx_queues = dev->data->nb_tx_queues;
671 dev_info->min_rx_bufsize = 0;
677 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
680 unsigned long rx_packets_total = 0, rx_bytes_total = 0;
681 unsigned long tx_packets_total = 0, tx_bytes_total = 0;
682 unsigned long tx_packets_err_total = 0;
683 const struct pmd_internals *internal = dev->data->dev_private;
685 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
686 i < dev->data->nb_rx_queues; i++) {
687 stats->q_ipackets[i] = internal->rx_queue[i].rx_stat.pkts;
688 stats->q_ibytes[i] = internal->rx_queue[i].rx_stat.bytes;
689 rx_packets_total += stats->q_ipackets[i];
690 rx_bytes_total += stats->q_ibytes[i];
693 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
694 i < dev->data->nb_tx_queues; i++) {
695 stats->q_opackets[i] = internal->tx_queue[i].tx_stat.pkts;
696 stats->q_obytes[i] = internal->tx_queue[i].tx_stat.bytes;
697 tx_packets_total += stats->q_opackets[i];
698 tx_bytes_total += stats->q_obytes[i];
699 tx_packets_err_total += internal->tx_queue[i].tx_stat.err_pkts;
702 stats->ipackets = rx_packets_total;
703 stats->ibytes = rx_bytes_total;
704 stats->opackets = tx_packets_total;
705 stats->obytes = tx_bytes_total;
706 stats->oerrors = tx_packets_err_total;
712 eth_stats_reset(struct rte_eth_dev *dev)
715 struct pmd_internals *internal = dev->data->dev_private;
717 for (i = 0; i < dev->data->nb_rx_queues; i++) {
718 internal->rx_queue[i].rx_stat.pkts = 0;
719 internal->rx_queue[i].rx_stat.bytes = 0;
722 for (i = 0; i < dev->data->nb_tx_queues; i++) {
723 internal->tx_queue[i].tx_stat.pkts = 0;
724 internal->tx_queue[i].tx_stat.bytes = 0;
725 internal->tx_queue[i].tx_stat.err_pkts = 0;
732 eth_dev_close(struct rte_eth_dev *dev)
735 struct pmd_internals *internals = dev->data->dev_private;
737 PMD_LOG(INFO, "Closing pcap ethdev on NUMA socket %d",
740 rte_free(dev->process_private);
742 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
745 /* Device wide flag, but cleanup must be performed per queue. */
746 if (internals->infinite_rx) {
747 for (i = 0; i < dev->data->nb_rx_queues; i++) {
748 struct pcap_rx_queue *pcap_q = &internals->rx_queue[i];
749 struct rte_mbuf *pcap_buf;
752 * 'pcap_q->pkts' can be NULL if 'eth_dev_close()'
753 * called before 'eth_rx_queue_setup()' has been called
755 if (pcap_q->pkts == NULL)
758 while (!rte_ring_dequeue(pcap_q->pkts,
760 rte_pktmbuf_free(pcap_buf);
762 rte_ring_free(pcap_q->pkts);
766 if (internals->phy_mac == 0)
767 /* not dynamically allocated, must not be freed */
768 dev->data->mac_addrs = NULL;
774 eth_queue_release(void *q __rte_unused)
779 eth_link_update(struct rte_eth_dev *dev __rte_unused,
780 int wait_to_complete __rte_unused)
786 eth_rx_queue_setup(struct rte_eth_dev *dev,
787 uint16_t rx_queue_id,
788 uint16_t nb_rx_desc __rte_unused,
789 unsigned int socket_id __rte_unused,
790 const struct rte_eth_rxconf *rx_conf __rte_unused,
791 struct rte_mempool *mb_pool)
793 struct pmd_internals *internals = dev->data->dev_private;
794 struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
796 pcap_q->mb_pool = mb_pool;
797 pcap_q->port_id = dev->data->port_id;
798 pcap_q->queue_id = rx_queue_id;
799 dev->data->rx_queues[rx_queue_id] = pcap_q;
801 if (internals->infinite_rx) {
802 struct pmd_process_private *pp;
803 char ring_name[NAME_MAX];
804 static uint32_t ring_number;
805 uint64_t pcap_pkt_count = 0;
806 struct rte_mbuf *bufs[1];
809 pp = rte_eth_devices[pcap_q->port_id].process_private;
810 pcap = &pp->rx_pcap[pcap_q->queue_id];
812 if (unlikely(*pcap == NULL))
815 pcap_pkt_count = count_packets_in_pcap(pcap, pcap_q);
817 snprintf(ring_name, sizeof(ring_name), "PCAP_RING%" PRIu16,
820 pcap_q->pkts = rte_ring_create(ring_name,
821 rte_align64pow2(pcap_pkt_count + 1), 0,
822 RING_F_SP_ENQ | RING_F_SC_DEQ);
827 /* Fill ring with packets from PCAP file one by one. */
828 while (eth_pcap_rx(pcap_q, bufs, 1)) {
829 /* Check for multiseg mbufs. */
830 if (bufs[0]->nb_segs != 1) {
831 rte_pktmbuf_free(*bufs);
833 while (!rte_ring_dequeue(pcap_q->pkts,
835 rte_pktmbuf_free(*bufs);
837 rte_ring_free(pcap_q->pkts);
838 PMD_LOG(ERR, "Multiseg mbufs are not supported in infinite_rx "
843 rte_ring_enqueue_bulk(pcap_q->pkts,
844 (void * const *)bufs, 1, NULL);
847 * Reset the stats for this queue since eth_pcap_rx calls above
848 * didn't result in the application receiving packets.
850 pcap_q->rx_stat.pkts = 0;
851 pcap_q->rx_stat.bytes = 0;
858 eth_tx_queue_setup(struct rte_eth_dev *dev,
859 uint16_t tx_queue_id,
860 uint16_t nb_tx_desc __rte_unused,
861 unsigned int socket_id __rte_unused,
862 const struct rte_eth_txconf *tx_conf __rte_unused)
864 struct pmd_internals *internals = dev->data->dev_private;
865 struct pcap_tx_queue *pcap_q = &internals->tx_queue[tx_queue_id];
867 pcap_q->port_id = dev->data->port_id;
868 pcap_q->queue_id = tx_queue_id;
869 dev->data->tx_queues[tx_queue_id] = pcap_q;
875 eth_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
877 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
883 eth_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
885 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
891 eth_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
893 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
899 eth_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
901 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
906 static const struct eth_dev_ops ops = {
907 .dev_start = eth_dev_start,
908 .dev_stop = eth_dev_stop,
909 .dev_close = eth_dev_close,
910 .dev_configure = eth_dev_configure,
911 .dev_infos_get = eth_dev_info,
912 .rx_queue_setup = eth_rx_queue_setup,
913 .tx_queue_setup = eth_tx_queue_setup,
914 .rx_queue_start = eth_rx_queue_start,
915 .tx_queue_start = eth_tx_queue_start,
916 .rx_queue_stop = eth_rx_queue_stop,
917 .tx_queue_stop = eth_tx_queue_stop,
918 .rx_queue_release = eth_queue_release,
919 .tx_queue_release = eth_queue_release,
920 .link_update = eth_link_update,
921 .stats_get = eth_stats_get,
922 .stats_reset = eth_stats_reset,
926 add_queue(struct pmd_devargs *pmd, const char *name, const char *type,
927 pcap_t *pcap, pcap_dumper_t *dumper)
929 if (pmd->num_of_queue >= RTE_PMD_PCAP_MAX_QUEUES)
932 pmd->queue[pmd->num_of_queue].pcap = pcap;
934 pmd->queue[pmd->num_of_queue].dumper = dumper;
935 pmd->queue[pmd->num_of_queue].name = name;
936 pmd->queue[pmd->num_of_queue].type = type;
942 * Function handler that opens the pcap file for reading a stores a
943 * reference of it for use it later on.
946 open_rx_pcap(const char *key, const char *value, void *extra_args)
948 const char *pcap_filename = value;
949 struct pmd_devargs *rx = extra_args;
952 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
955 if (add_queue(rx, pcap_filename, key, pcap, NULL) < 0) {
964 * Opens a pcap file for writing and stores a reference to it
965 * for use it later on.
968 open_tx_pcap(const char *key, const char *value, void *extra_args)
970 const char *pcap_filename = value;
971 struct pmd_devargs *dumpers = extra_args;
972 pcap_dumper_t *dumper;
974 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
977 if (add_queue(dumpers, pcap_filename, key, NULL, dumper) < 0) {
978 pcap_dump_close(dumper);
986 * Opens an interface for reading and writing
989 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
991 const char *iface = value;
992 struct pmd_devargs *tx = extra_args;
995 if (open_single_iface(iface, &pcap) < 0)
998 tx->queue[0].pcap = pcap;
999 tx->queue[0].name = iface;
1000 tx->queue[0].type = key;
1006 set_iface_direction(const char *iface, pcap_t *pcap,
1007 pcap_direction_t direction)
1009 const char *direction_str = (direction == PCAP_D_IN) ? "IN" : "OUT";
1010 if (pcap_setdirection(pcap, direction) < 0) {
1011 PMD_LOG(ERR, "Setting %s pcap direction %s failed - %s\n",
1012 iface, direction_str, pcap_geterr(pcap));
1015 PMD_LOG(INFO, "Setting %s pcap direction %s\n",
1016 iface, direction_str);
1021 open_iface(const char *key, const char *value, void *extra_args)
1023 const char *iface = value;
1024 struct pmd_devargs *pmd = extra_args;
1025 pcap_t *pcap = NULL;
1027 if (open_single_iface(iface, &pcap) < 0)
1029 if (add_queue(pmd, iface, key, pcap, NULL) < 0) {
1038 * Opens a NIC for reading packets from it
1041 open_rx_iface(const char *key, const char *value, void *extra_args)
1043 int ret = open_iface(key, value, extra_args);
1046 if (strcmp(key, ETH_PCAP_RX_IFACE_IN_ARG) == 0) {
1047 struct pmd_devargs *pmd = extra_args;
1048 unsigned int qid = pmd->num_of_queue - 1;
1050 set_iface_direction(pmd->queue[qid].name,
1051 pmd->queue[qid].pcap,
1059 rx_iface_args_process(const char *key, const char *value, void *extra_args)
1061 if (strcmp(key, ETH_PCAP_RX_IFACE_ARG) == 0 ||
1062 strcmp(key, ETH_PCAP_RX_IFACE_IN_ARG) == 0)
1063 return open_rx_iface(key, value, extra_args);
1069 * Opens a NIC for writing packets to it
1072 open_tx_iface(const char *key, const char *value, void *extra_args)
1074 return open_iface(key, value, extra_args);
1078 select_phy_mac(const char *key __rte_unused, const char *value,
1082 const int phy_mac = atoi(value);
1083 int *enable_phy_mac = extra_args;
1086 *enable_phy_mac = 1;
1092 get_infinite_rx_arg(const char *key __rte_unused,
1093 const char *value, void *extra_args)
1096 const int infinite_rx = atoi(value);
1097 int *enable_infinite_rx = extra_args;
1099 if (infinite_rx > 0)
1100 *enable_infinite_rx = 1;
1106 pmd_init_internals(struct rte_vdev_device *vdev,
1107 const unsigned int nb_rx_queues,
1108 const unsigned int nb_tx_queues,
1109 struct pmd_internals **internals,
1110 struct rte_eth_dev **eth_dev)
1112 struct rte_eth_dev_data *data;
1113 struct pmd_process_private *pp;
1114 unsigned int numa_node = vdev->device.numa_node;
1116 PMD_LOG(INFO, "Creating pcap-backed ethdev on numa socket %d",
1119 pp = (struct pmd_process_private *)
1120 rte_zmalloc(NULL, sizeof(struct pmd_process_private),
1121 RTE_CACHE_LINE_SIZE);
1125 "Failed to allocate memory for process private");
1129 /* reserve an ethdev entry */
1130 *eth_dev = rte_eth_vdev_allocate(vdev, sizeof(**internals));
1135 (*eth_dev)->process_private = pp;
1136 /* now put it all together
1137 * - store queue data in internals,
1138 * - store numa_node info in eth_dev
1139 * - point eth_dev_data to internals
1140 * - and point eth_dev structure to new eth_dev_data structure
1142 *internals = (*eth_dev)->data->dev_private;
1144 * Interface MAC = 02:70:63:61:70:<iface_idx>
1145 * derived from: 'locally administered':'p':'c':'a':'p':'iface_idx'
1146 * where the middle 4 characters are converted to hex.
1148 (*internals)->eth_addr = (struct rte_ether_addr) {
1149 .addr_bytes = { 0x02, 0x70, 0x63, 0x61, 0x70, iface_idx++ }
1151 (*internals)->phy_mac = 0;
1152 data = (*eth_dev)->data;
1153 data->nb_rx_queues = (uint16_t)nb_rx_queues;
1154 data->nb_tx_queues = (uint16_t)nb_tx_queues;
1155 data->dev_link = pmd_link;
1156 data->mac_addrs = &(*internals)->eth_addr;
1157 data->promiscuous = 1;
1158 data->all_multicast = 1;
1161 * NOTE: we'll replace the data element, of originally allocated
1162 * eth_dev so the rings are local per-process
1164 (*eth_dev)->dev_ops = &ops;
1166 strlcpy((*internals)->devargs, rte_vdev_device_args(vdev),
1167 ETH_PCAP_ARG_MAXLEN);
1173 eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,
1174 const unsigned int numa_node)
1176 #if defined(RTE_EXEC_ENV_LINUX)
1179 int if_fd = socket(AF_INET, SOCK_DGRAM, 0);
1184 rte_strscpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name));
1185 if (ioctl(if_fd, SIOCGIFHWADDR, &ifr)) {
1190 mac_addrs = rte_zmalloc_socket(NULL, RTE_ETHER_ADDR_LEN, 0, numa_node);
1196 PMD_LOG(INFO, "Setting phy MAC for %s", if_name);
1197 eth_dev->data->mac_addrs = mac_addrs;
1198 rte_memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
1199 ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
1205 #elif defined(RTE_EXEC_ENV_FREEBSD)
1207 struct if_msghdr *ifm;
1208 struct sockaddr_dl *sdl;
1217 mib[4] = NET_RT_IFLIST;
1218 mib[5] = if_nametoindex(if_name);
1220 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
1226 buf = rte_malloc(NULL, len, 0);
1230 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
1234 ifm = (struct if_msghdr *)buf;
1235 sdl = (struct sockaddr_dl *)(ifm + 1);
1237 mac_addrs = rte_zmalloc_socket(NULL, RTE_ETHER_ADDR_LEN, 0, numa_node);
1243 PMD_LOG(INFO, "Setting phy MAC for %s", if_name);
1244 eth_dev->data->mac_addrs = mac_addrs;
1245 rte_memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
1246 LLADDR(sdl), RTE_ETHER_ADDR_LEN);
1257 eth_from_pcaps_common(struct rte_vdev_device *vdev,
1258 struct pmd_devargs_all *devargs_all,
1259 struct pmd_internals **internals, struct rte_eth_dev **eth_dev)
1261 struct pmd_process_private *pp;
1262 struct pmd_devargs *rx_queues = &devargs_all->rx_queues;
1263 struct pmd_devargs *tx_queues = &devargs_all->tx_queues;
1264 const unsigned int nb_rx_queues = rx_queues->num_of_queue;
1265 const unsigned int nb_tx_queues = tx_queues->num_of_queue;
1268 if (pmd_init_internals(vdev, nb_rx_queues, nb_tx_queues, internals,
1272 pp = (*eth_dev)->process_private;
1273 for (i = 0; i < nb_rx_queues; i++) {
1274 struct pcap_rx_queue *rx = &(*internals)->rx_queue[i];
1275 struct devargs_queue *queue = &rx_queues->queue[i];
1277 pp->rx_pcap[i] = queue->pcap;
1278 strlcpy(rx->name, queue->name, sizeof(rx->name));
1279 strlcpy(rx->type, queue->type, sizeof(rx->type));
1282 for (i = 0; i < nb_tx_queues; i++) {
1283 struct pcap_tx_queue *tx = &(*internals)->tx_queue[i];
1284 struct devargs_queue *queue = &tx_queues->queue[i];
1286 pp->tx_dumper[i] = queue->dumper;
1287 pp->tx_pcap[i] = queue->pcap;
1288 strlcpy(tx->name, queue->name, sizeof(tx->name));
1289 strlcpy(tx->type, queue->type, sizeof(tx->type));
1296 eth_from_pcaps(struct rte_vdev_device *vdev,
1297 struct pmd_devargs_all *devargs_all)
1299 struct pmd_internals *internals = NULL;
1300 struct rte_eth_dev *eth_dev = NULL;
1301 struct pmd_devargs *rx_queues = &devargs_all->rx_queues;
1302 int single_iface = devargs_all->single_iface;
1303 unsigned int infinite_rx = devargs_all->infinite_rx;
1306 ret = eth_from_pcaps_common(vdev, devargs_all, &internals, ð_dev);
1311 /* store weather we are using a single interface for rx/tx or not */
1312 internals->single_iface = single_iface;
1315 internals->if_index = if_nametoindex(rx_queues->queue[0].name);
1317 /* phy_mac arg is applied only only if "iface" devarg is provided */
1318 if (rx_queues->phy_mac) {
1319 int ret = eth_pcap_update_mac(rx_queues->queue[0].name,
1320 eth_dev, vdev->device.numa_node);
1322 internals->phy_mac = 1;
1326 internals->infinite_rx = infinite_rx;
1327 /* Assign rx ops. */
1329 eth_dev->rx_pkt_burst = eth_pcap_rx_infinite;
1330 else if (devargs_all->is_rx_pcap || devargs_all->is_rx_iface ||
1332 eth_dev->rx_pkt_burst = eth_pcap_rx;
1334 eth_dev->rx_pkt_burst = eth_null_rx;
1336 /* Assign tx ops. */
1337 if (devargs_all->is_tx_pcap)
1338 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
1339 else if (devargs_all->is_tx_iface || single_iface)
1340 eth_dev->tx_pkt_burst = eth_pcap_tx;
1342 eth_dev->tx_pkt_burst = eth_tx_drop;
1344 rte_eth_dev_probing_finish(eth_dev);
1349 pmd_pcap_probe(struct rte_vdev_device *dev)
1352 struct rte_kvargs *kvlist;
1353 struct pmd_devargs pcaps = {0};
1354 struct pmd_devargs dumpers = {0};
1355 struct rte_eth_dev *eth_dev = NULL;
1356 struct pmd_internals *internal;
1359 struct pmd_devargs_all devargs_all = {
1366 name = rte_vdev_device_name(dev);
1367 PMD_LOG(INFO, "Initializing pmd_pcap for %s", name);
1369 gettimeofday(&start_time, NULL);
1370 start_cycles = rte_get_timer_cycles();
1371 hz = rte_get_timer_hz();
1373 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1374 eth_dev = rte_eth_dev_attach_secondary(name);
1376 PMD_LOG(ERR, "Failed to probe %s", name);
1380 internal = eth_dev->data->dev_private;
1382 kvlist = rte_kvargs_parse(internal->devargs, valid_arguments);
1386 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev),
1393 * If iface argument is passed we open the NICs and use them for
1396 if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
1398 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
1399 &open_rx_tx_iface, &pcaps);
1403 dumpers.queue[0] = pcaps.queue[0];
1405 ret = rte_kvargs_process(kvlist, ETH_PCAP_PHY_MAC_ARG,
1406 &select_phy_mac, &pcaps.phy_mac);
1410 dumpers.phy_mac = pcaps.phy_mac;
1412 devargs_all.single_iface = 1;
1413 pcaps.num_of_queue = 1;
1414 dumpers.num_of_queue = 1;
1420 * We check whether we want to open a RX stream from a real NIC, a
1421 * pcap file or open a dummy RX stream
1423 devargs_all.is_rx_pcap =
1424 rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG) ? 1 : 0;
1425 devargs_all.is_rx_iface =
1426 (rte_kvargs_count(kvlist, ETH_PCAP_RX_IFACE_ARG) +
1427 rte_kvargs_count(kvlist, ETH_PCAP_RX_IFACE_IN_ARG)) ? 1 : 0;
1428 pcaps.num_of_queue = 0;
1430 devargs_all.is_tx_pcap =
1431 rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG) ? 1 : 0;
1432 devargs_all.is_tx_iface =
1433 rte_kvargs_count(kvlist, ETH_PCAP_TX_IFACE_ARG) ? 1 : 0;
1434 dumpers.num_of_queue = 0;
1436 if (devargs_all.is_rx_pcap) {
1438 * We check whether we want to infinitely rx the pcap file.
1440 unsigned int infinite_rx_arg_cnt = rte_kvargs_count(kvlist,
1441 ETH_PCAP_INFINITE_RX_ARG);
1443 if (infinite_rx_arg_cnt == 1) {
1444 ret = rte_kvargs_process(kvlist,
1445 ETH_PCAP_INFINITE_RX_ARG,
1446 &get_infinite_rx_arg,
1447 &devargs_all.infinite_rx);
1450 PMD_LOG(INFO, "infinite_rx has been %s for %s",
1451 devargs_all.infinite_rx ? "enabled" : "disabled",
1454 } else if (infinite_rx_arg_cnt > 1) {
1455 PMD_LOG(WARNING, "infinite_rx has not been enabled since the "
1456 "argument has been provided more than once "
1460 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
1461 &open_rx_pcap, &pcaps);
1462 } else if (devargs_all.is_rx_iface) {
1463 ret = rte_kvargs_process(kvlist, NULL,
1464 &rx_iface_args_process, &pcaps);
1465 } else if (devargs_all.is_tx_iface || devargs_all.is_tx_pcap) {
1468 /* Count number of tx queue args passed before dummy rx queue
1469 * creation so a dummy rx queue can be created for each tx queue
1471 unsigned int num_tx_queues =
1472 (rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG) +
1473 rte_kvargs_count(kvlist, ETH_PCAP_TX_IFACE_ARG));
1475 PMD_LOG(INFO, "Creating null rx queue since no rx queues were provided.");
1477 /* Creating a dummy rx queue for each tx queue passed */
1478 for (i = 0; i < num_tx_queues; i++)
1479 ret = add_queue(&pcaps, "dummy_rx", "rx_null", NULL,
1482 PMD_LOG(ERR, "Error - No rx or tx queues provided");
1489 * We check whether we want to open a TX stream to a real NIC,
1490 * a pcap file, or drop packets on tx
1492 if (devargs_all.is_tx_pcap) {
1493 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1494 &open_tx_pcap, &dumpers);
1495 } else if (devargs_all.is_tx_iface) {
1496 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1497 &open_tx_iface, &dumpers);
1501 PMD_LOG(INFO, "Dropping packets on tx since no tx queues were provided.");
1503 /* Add 1 dummy queue per rxq which counts and drops packets. */
1504 for (i = 0; i < pcaps.num_of_queue; i++)
1505 ret = add_queue(&dumpers, "dummy_tx", "tx_drop", NULL,
1513 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1514 struct pmd_process_private *pp;
1517 internal = eth_dev->data->dev_private;
1518 pp = (struct pmd_process_private *)
1520 sizeof(struct pmd_process_private),
1521 RTE_CACHE_LINE_SIZE);
1525 "Failed to allocate memory for process private");
1530 eth_dev->dev_ops = &ops;
1531 eth_dev->device = &dev->device;
1533 /* setup process private */
1534 for (i = 0; i < pcaps.num_of_queue; i++)
1535 pp->rx_pcap[i] = pcaps.queue[i].pcap;
1537 for (i = 0; i < dumpers.num_of_queue; i++) {
1538 pp->tx_dumper[i] = dumpers.queue[i].dumper;
1539 pp->tx_pcap[i] = dumpers.queue[i].pcap;
1542 eth_dev->process_private = pp;
1543 eth_dev->rx_pkt_burst = eth_pcap_rx;
1544 if (devargs_all.is_tx_pcap)
1545 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
1547 eth_dev->tx_pkt_burst = eth_pcap_tx;
1549 rte_eth_dev_probing_finish(eth_dev);
1553 devargs_all.rx_queues = pcaps;
1554 devargs_all.tx_queues = dumpers;
1556 ret = eth_from_pcaps(dev, &devargs_all);
1559 rte_kvargs_free(kvlist);
1565 pmd_pcap_remove(struct rte_vdev_device *dev)
1567 struct rte_eth_dev *eth_dev = NULL;
1572 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1573 if (eth_dev == NULL)
1574 return 0; /* port already released */
1576 eth_dev_close(eth_dev);
1577 rte_eth_dev_release_port(eth_dev);
1582 static struct rte_vdev_driver pmd_pcap_drv = {
1583 .probe = pmd_pcap_probe,
1584 .remove = pmd_pcap_remove,
1587 RTE_PMD_REGISTER_VDEV(net_pcap, pmd_pcap_drv);
1588 RTE_PMD_REGISTER_ALIAS(net_pcap, eth_pcap);
1589 RTE_PMD_REGISTER_PARAM_STRING(net_pcap,
1590 ETH_PCAP_RX_PCAP_ARG "=<string> "
1591 ETH_PCAP_TX_PCAP_ARG "=<string> "
1592 ETH_PCAP_RX_IFACE_ARG "=<ifc> "
1593 ETH_PCAP_RX_IFACE_IN_ARG "=<ifc> "
1594 ETH_PCAP_TX_IFACE_ARG "=<ifc> "
1595 ETH_PCAP_IFACE_ARG "=<ifc> "
1596 ETH_PCAP_PHY_MAC_ARG "=<int>"
1597 ETH_PCAP_INFINITE_RX_ARG "=<0|1>");