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_RX_IFACE_IN_ARG "rx_iface_in"
30 #define ETH_PCAP_TX_IFACE_ARG "tx_iface"
31 #define ETH_PCAP_IFACE_ARG "iface"
33 #define ETH_PCAP_ARG_MAXLEN 64
35 #define RTE_PMD_PCAP_MAX_QUEUES 16
37 static char errbuf
[PCAP_ERRBUF_SIZE
];
38 static unsigned char tx_pcap_data
[RTE_ETH_PCAP_SNAPLEN
];
39 static struct timeval start_time
;
40 static uint64_t start_cycles
;
44 volatile unsigned long pkts
;
45 volatile unsigned long bytes
;
46 volatile unsigned long err_pkts
;
49 struct pcap_rx_queue
{
52 struct rte_mempool
*mb_pool
;
53 struct queue_stat rx_stat
;
55 char type
[ETH_PCAP_ARG_MAXLEN
];
58 struct pcap_tx_queue
{
59 pcap_dumper_t
*dumper
;
61 struct queue_stat tx_stat
;
63 char type
[ETH_PCAP_ARG_MAXLEN
];
66 struct pmd_internals
{
67 struct pcap_rx_queue rx_queue
[RTE_PMD_PCAP_MAX_QUEUES
];
68 struct pcap_tx_queue tx_queue
[RTE_PMD_PCAP_MAX_QUEUES
];
74 unsigned int num_of_queue
;
75 struct devargs_queue
{
76 pcap_dumper_t
*dumper
;
80 } queue
[RTE_PMD_PCAP_MAX_QUEUES
];
83 static const char *valid_arguments
[] = {
86 ETH_PCAP_RX_IFACE_ARG
,
87 ETH_PCAP_RX_IFACE_IN_ARG
,
88 ETH_PCAP_TX_IFACE_ARG
,
93 static struct ether_addr eth_addr
= {
94 .addr_bytes
= { 0, 0, 0, 0x1, 0x2, 0x3 }
97 static struct rte_eth_link pmd_link
= {
98 .link_speed
= ETH_SPEED_NUM_10G
,
99 .link_duplex
= ETH_LINK_FULL_DUPLEX
,
100 .link_status
= ETH_LINK_DOWN
,
101 .link_autoneg
= ETH_LINK_FIXED
,
104 static int eth_pcap_logtype
;
106 #define PMD_LOG(level, fmt, args...) \
107 rte_log(RTE_LOG_ ## level, eth_pcap_logtype, \
108 "%s(): " fmt "\n", __func__, ##args)
111 eth_pcap_rx_jumbo(struct rte_mempool
*mb_pool
, struct rte_mbuf
*mbuf
,
112 const u_char
*data
, uint16_t data_len
)
114 /* Copy the first segment. */
115 uint16_t len
= rte_pktmbuf_tailroom(mbuf
);
116 struct rte_mbuf
*m
= mbuf
;
118 rte_memcpy(rte_pktmbuf_append(mbuf
, len
), data
, len
);
122 while (data_len
> 0) {
123 /* Allocate next mbuf and point to that. */
124 m
->next
= rte_pktmbuf_alloc(mb_pool
);
126 if (unlikely(!m
->next
))
131 /* Headroom is not needed in chained mbufs. */
132 rte_pktmbuf_prepend(m
, rte_pktmbuf_headroom(m
));
136 /* Copy next segment. */
137 len
= RTE_MIN(rte_pktmbuf_tailroom(m
), data_len
);
138 rte_memcpy(rte_pktmbuf_append(m
, len
), data
, len
);
145 return mbuf
->nb_segs
;
148 /* Copy data from mbuf chain to a buffer suitable for writing to a PCAP file. */
150 eth_pcap_gather_data(unsigned char *data
, struct rte_mbuf
*mbuf
)
152 uint16_t data_len
= 0;
155 rte_memcpy(data
+ data_len
, rte_pktmbuf_mtod(mbuf
, void *),
158 data_len
+= mbuf
->data_len
;
164 eth_pcap_rx(void *queue
, struct rte_mbuf
**bufs
, uint16_t nb_pkts
)
167 struct pcap_pkthdr header
;
168 const u_char
*packet
;
169 struct rte_mbuf
*mbuf
;
170 struct pcap_rx_queue
*pcap_q
= queue
;
173 uint32_t rx_bytes
= 0;
175 if (unlikely(pcap_q
->pcap
== NULL
|| nb_pkts
== 0))
178 /* Reads the given number of packets from the pcap file one by one
179 * and copies the packet data into a newly allocated mbuf to return.
181 for (i
= 0; i
< nb_pkts
; i
++) {
182 /* Get the next PCAP packet */
183 packet
= pcap_next(pcap_q
->pcap
, &header
);
184 if (unlikely(packet
== NULL
))
187 mbuf
= rte_pktmbuf_alloc(pcap_q
->mb_pool
);
188 if (unlikely(mbuf
== NULL
))
191 /* Now get the space available for data in the mbuf */
192 buf_size
= rte_pktmbuf_data_room_size(pcap_q
->mb_pool
) -
193 RTE_PKTMBUF_HEADROOM
;
195 if (header
.caplen
<= buf_size
) {
196 /* pcap packet will fit in the mbuf, can copy it */
197 rte_memcpy(rte_pktmbuf_mtod(mbuf
, void *), packet
,
199 mbuf
->data_len
= (uint16_t)header
.caplen
;
201 /* Try read jumbo frame into multi mbufs. */
202 if (unlikely(eth_pcap_rx_jumbo(pcap_q
->mb_pool
,
205 header
.caplen
) == -1)) {
206 rte_pktmbuf_free(mbuf
);
211 mbuf
->pkt_len
= (uint16_t)header
.caplen
;
212 mbuf
->port
= pcap_q
->in_port
;
215 rx_bytes
+= header
.caplen
;
217 pcap_q
->rx_stat
.pkts
+= num_rx
;
218 pcap_q
->rx_stat
.bytes
+= rx_bytes
;
224 calculate_timestamp(struct timeval
*ts
) {
226 struct timeval cur_time
;
228 cycles
= rte_get_timer_cycles() - start_cycles
;
229 cur_time
.tv_sec
= cycles
/ hz
;
230 cur_time
.tv_usec
= (cycles
% hz
) * 1e6
/ hz
;
231 timeradd(&start_time
, &cur_time
, ts
);
235 * Callback to handle writing packets to a pcap file.
238 eth_pcap_tx_dumper(void *queue
, struct rte_mbuf
**bufs
, uint16_t nb_pkts
)
241 struct rte_mbuf
*mbuf
;
242 struct pcap_tx_queue
*dumper_q
= queue
;
244 uint32_t tx_bytes
= 0;
245 struct pcap_pkthdr header
;
247 if (dumper_q
->dumper
== NULL
|| nb_pkts
== 0)
250 /* writes the nb_pkts packets to the previously opened pcap file
252 for (i
= 0; i
< nb_pkts
; i
++) {
254 calculate_timestamp(&header
.ts
);
255 header
.len
= mbuf
->pkt_len
;
256 header
.caplen
= header
.len
;
258 if (likely(mbuf
->nb_segs
== 1)) {
259 pcap_dump((u_char
*)dumper_q
->dumper
, &header
,
260 rte_pktmbuf_mtod(mbuf
, void*));
262 if (mbuf
->pkt_len
<= ETHER_MAX_JUMBO_FRAME_LEN
) {
263 eth_pcap_gather_data(tx_pcap_data
, mbuf
);
264 pcap_dump((u_char
*)dumper_q
->dumper
, &header
,
268 "Dropping PCAP packet. Size (%d) > max jumbo size (%d).",
270 ETHER_MAX_JUMBO_FRAME_LEN
);
272 rte_pktmbuf_free(mbuf
);
278 tx_bytes
+= mbuf
->pkt_len
;
279 rte_pktmbuf_free(mbuf
);
283 * Since there's no place to hook a callback when the forwarding
284 * process stops and to make sure the pcap file is actually written,
285 * we flush the pcap dumper within each burst.
287 pcap_dump_flush(dumper_q
->dumper
);
288 dumper_q
->tx_stat
.pkts
+= num_tx
;
289 dumper_q
->tx_stat
.bytes
+= tx_bytes
;
290 dumper_q
->tx_stat
.err_pkts
+= nb_pkts
- num_tx
;
296 * Callback to handle sending packets through a real NIC.
299 eth_pcap_tx(void *queue
, struct rte_mbuf
**bufs
, uint16_t nb_pkts
)
303 struct rte_mbuf
*mbuf
;
304 struct pcap_tx_queue
*tx_queue
= queue
;
306 uint32_t tx_bytes
= 0;
308 if (unlikely(nb_pkts
== 0 || tx_queue
->pcap
== NULL
))
311 for (i
= 0; i
< nb_pkts
; i
++) {
314 if (likely(mbuf
->nb_segs
== 1)) {
315 ret
= pcap_sendpacket(tx_queue
->pcap
,
316 rte_pktmbuf_mtod(mbuf
, u_char
*),
319 if (mbuf
->pkt_len
<= ETHER_MAX_JUMBO_FRAME_LEN
) {
320 eth_pcap_gather_data(tx_pcap_data
, mbuf
);
321 ret
= pcap_sendpacket(tx_queue
->pcap
,
322 tx_pcap_data
, mbuf
->pkt_len
);
325 "Dropping PCAP packet. Size (%d) > max jumbo size (%d).",
327 ETHER_MAX_JUMBO_FRAME_LEN
);
329 rte_pktmbuf_free(mbuf
);
334 if (unlikely(ret
!= 0))
337 tx_bytes
+= mbuf
->pkt_len
;
338 rte_pktmbuf_free(mbuf
);
341 tx_queue
->tx_stat
.pkts
+= num_tx
;
342 tx_queue
->tx_stat
.bytes
+= tx_bytes
;
343 tx_queue
->tx_stat
.err_pkts
+= nb_pkts
- num_tx
;
349 * pcap_open_live wrapper function
352 open_iface_live(const char *iface
, pcap_t
**pcap
) {
353 *pcap
= pcap_open_live(iface
, RTE_ETH_PCAP_SNAPLEN
,
354 RTE_ETH_PCAP_PROMISC
, RTE_ETH_PCAP_TIMEOUT
, errbuf
);
357 PMD_LOG(ERR
, "Couldn't open %s: %s", iface
, errbuf
);
365 open_single_iface(const char *iface
, pcap_t
**pcap
)
367 if (open_iface_live(iface
, pcap
) < 0) {
368 PMD_LOG(ERR
, "Couldn't open interface %s", iface
);
376 open_single_tx_pcap(const char *pcap_filename
, pcap_dumper_t
**dumper
)
381 * We need to create a dummy empty pcap_t to use it
382 * with pcap_dump_open(). We create big enough an Ethernet
385 tx_pcap
= pcap_open_dead(DLT_EN10MB
, RTE_ETH_PCAP_SNAPSHOT_LEN
);
386 if (tx_pcap
== NULL
) {
387 PMD_LOG(ERR
, "Couldn't create dead pcap");
391 /* The dumper is created using the previous pcap_t reference */
392 *dumper
= pcap_dump_open(tx_pcap
, pcap_filename
);
393 if (*dumper
== NULL
) {
395 PMD_LOG(ERR
, "Couldn't open %s for writing.",
405 open_single_rx_pcap(const char *pcap_filename
, pcap_t
**pcap
)
407 *pcap
= pcap_open_offline(pcap_filename
, errbuf
);
409 PMD_LOG(ERR
, "Couldn't open %s: %s", pcap_filename
,
418 eth_dev_start(struct rte_eth_dev
*dev
)
421 struct pmd_internals
*internals
= dev
->data
->dev_private
;
422 struct pcap_tx_queue
*tx
;
423 struct pcap_rx_queue
*rx
;
425 /* Special iface case. Single pcap is open and shared between tx/rx. */
426 if (internals
->single_iface
) {
427 tx
= &internals
->tx_queue
[0];
428 rx
= &internals
->rx_queue
[0];
430 if (!tx
->pcap
&& strcmp(tx
->type
, ETH_PCAP_IFACE_ARG
) == 0) {
431 if (open_single_iface(tx
->name
, &tx
->pcap
) < 0)
439 /* If not open already, open tx pcaps/dumpers */
440 for (i
= 0; i
< dev
->data
->nb_tx_queues
; i
++) {
441 tx
= &internals
->tx_queue
[i
];
444 strcmp(tx
->type
, ETH_PCAP_TX_PCAP_ARG
) == 0) {
445 if (open_single_tx_pcap(tx
->name
, &tx
->dumper
) < 0)
447 } else if (!tx
->pcap
&&
448 strcmp(tx
->type
, ETH_PCAP_TX_IFACE_ARG
) == 0) {
449 if (open_single_iface(tx
->name
, &tx
->pcap
) < 0)
454 /* If not open already, open rx pcaps */
455 for (i
= 0; i
< dev
->data
->nb_rx_queues
; i
++) {
456 rx
= &internals
->rx_queue
[i
];
458 if (rx
->pcap
!= NULL
)
461 if (strcmp(rx
->type
, ETH_PCAP_RX_PCAP_ARG
) == 0) {
462 if (open_single_rx_pcap(rx
->name
, &rx
->pcap
) < 0)
464 } else if (strcmp(rx
->type
, ETH_PCAP_RX_IFACE_ARG
) == 0) {
465 if (open_single_iface(rx
->name
, &rx
->pcap
) < 0)
471 for (i
= 0; i
< dev
->data
->nb_rx_queues
; i
++)
472 dev
->data
->rx_queue_state
[i
] = RTE_ETH_QUEUE_STATE_STARTED
;
474 for (i
= 0; i
< dev
->data
->nb_tx_queues
; i
++)
475 dev
->data
->tx_queue_state
[i
] = RTE_ETH_QUEUE_STATE_STARTED
;
477 dev
->data
->dev_link
.link_status
= ETH_LINK_UP
;
483 * This function gets called when the current port gets stopped.
484 * Is the only place for us to close all the tx streams dumpers.
485 * If not called the dumpers will be flushed within each tx burst.
488 eth_dev_stop(struct rte_eth_dev
*dev
)
491 struct pmd_internals
*internals
= dev
->data
->dev_private
;
492 struct pcap_tx_queue
*tx
;
493 struct pcap_rx_queue
*rx
;
495 /* Special iface case. Single pcap is open and shared between tx/rx. */
496 if (internals
->single_iface
) {
497 tx
= &internals
->tx_queue
[0];
498 rx
= &internals
->rx_queue
[0];
499 pcap_close(tx
->pcap
);
505 for (i
= 0; i
< dev
->data
->nb_tx_queues
; i
++) {
506 tx
= &internals
->tx_queue
[i
];
508 if (tx
->dumper
!= NULL
) {
509 pcap_dump_close(tx
->dumper
);
513 if (tx
->pcap
!= NULL
) {
514 pcap_close(tx
->pcap
);
519 for (i
= 0; i
< dev
->data
->nb_rx_queues
; i
++) {
520 rx
= &internals
->rx_queue
[i
];
522 if (rx
->pcap
!= NULL
) {
523 pcap_close(rx
->pcap
);
529 for (i
= 0; i
< dev
->data
->nb_rx_queues
; i
++)
530 dev
->data
->rx_queue_state
[i
] = RTE_ETH_QUEUE_STATE_STOPPED
;
532 for (i
= 0; i
< dev
->data
->nb_tx_queues
; i
++)
533 dev
->data
->tx_queue_state
[i
] = RTE_ETH_QUEUE_STATE_STOPPED
;
535 dev
->data
->dev_link
.link_status
= ETH_LINK_DOWN
;
539 eth_dev_configure(struct rte_eth_dev
*dev __rte_unused
)
545 eth_dev_info(struct rte_eth_dev
*dev
,
546 struct rte_eth_dev_info
*dev_info
)
548 struct pmd_internals
*internals
= dev
->data
->dev_private
;
550 dev_info
->if_index
= internals
->if_index
;
551 dev_info
->max_mac_addrs
= 1;
552 dev_info
->max_rx_pktlen
= (uint32_t) -1;
553 dev_info
->max_rx_queues
= dev
->data
->nb_rx_queues
;
554 dev_info
->max_tx_queues
= dev
->data
->nb_tx_queues
;
555 dev_info
->min_rx_bufsize
= 0;
556 dev_info
->rx_offload_capa
= DEV_RX_OFFLOAD_CRC_STRIP
;
560 eth_stats_get(struct rte_eth_dev
*dev
, struct rte_eth_stats
*stats
)
563 unsigned long rx_packets_total
= 0, rx_bytes_total
= 0;
564 unsigned long tx_packets_total
= 0, tx_bytes_total
= 0;
565 unsigned long tx_packets_err_total
= 0;
566 const struct pmd_internals
*internal
= dev
->data
->dev_private
;
568 for (i
= 0; i
< RTE_ETHDEV_QUEUE_STAT_CNTRS
&&
569 i
< dev
->data
->nb_rx_queues
; i
++) {
570 stats
->q_ipackets
[i
] = internal
->rx_queue
[i
].rx_stat
.pkts
;
571 stats
->q_ibytes
[i
] = internal
->rx_queue
[i
].rx_stat
.bytes
;
572 rx_packets_total
+= stats
->q_ipackets
[i
];
573 rx_bytes_total
+= stats
->q_ibytes
[i
];
576 for (i
= 0; i
< RTE_ETHDEV_QUEUE_STAT_CNTRS
&&
577 i
< dev
->data
->nb_tx_queues
; i
++) {
578 stats
->q_opackets
[i
] = internal
->tx_queue
[i
].tx_stat
.pkts
;
579 stats
->q_obytes
[i
] = internal
->tx_queue
[i
].tx_stat
.bytes
;
580 stats
->q_errors
[i
] = internal
->tx_queue
[i
].tx_stat
.err_pkts
;
581 tx_packets_total
+= stats
->q_opackets
[i
];
582 tx_bytes_total
+= stats
->q_obytes
[i
];
583 tx_packets_err_total
+= stats
->q_errors
[i
];
586 stats
->ipackets
= rx_packets_total
;
587 stats
->ibytes
= rx_bytes_total
;
588 stats
->opackets
= tx_packets_total
;
589 stats
->obytes
= tx_bytes_total
;
590 stats
->oerrors
= tx_packets_err_total
;
596 eth_stats_reset(struct rte_eth_dev
*dev
)
599 struct pmd_internals
*internal
= dev
->data
->dev_private
;
601 for (i
= 0; i
< dev
->data
->nb_rx_queues
; i
++) {
602 internal
->rx_queue
[i
].rx_stat
.pkts
= 0;
603 internal
->rx_queue
[i
].rx_stat
.bytes
= 0;
606 for (i
= 0; i
< dev
->data
->nb_tx_queues
; i
++) {
607 internal
->tx_queue
[i
].tx_stat
.pkts
= 0;
608 internal
->tx_queue
[i
].tx_stat
.bytes
= 0;
609 internal
->tx_queue
[i
].tx_stat
.err_pkts
= 0;
614 eth_dev_close(struct rte_eth_dev
*dev __rte_unused
)
619 eth_queue_release(void *q __rte_unused
)
624 eth_link_update(struct rte_eth_dev
*dev __rte_unused
,
625 int wait_to_complete __rte_unused
)
631 eth_rx_queue_setup(struct rte_eth_dev
*dev
,
632 uint16_t rx_queue_id
,
633 uint16_t nb_rx_desc __rte_unused
,
634 unsigned int socket_id __rte_unused
,
635 const struct rte_eth_rxconf
*rx_conf __rte_unused
,
636 struct rte_mempool
*mb_pool
)
638 struct pmd_internals
*internals
= dev
->data
->dev_private
;
639 struct pcap_rx_queue
*pcap_q
= &internals
->rx_queue
[rx_queue_id
];
641 pcap_q
->mb_pool
= mb_pool
;
642 dev
->data
->rx_queues
[rx_queue_id
] = pcap_q
;
643 pcap_q
->in_port
= dev
->data
->port_id
;
649 eth_tx_queue_setup(struct rte_eth_dev
*dev
,
650 uint16_t tx_queue_id
,
651 uint16_t nb_tx_desc __rte_unused
,
652 unsigned int socket_id __rte_unused
,
653 const struct rte_eth_txconf
*tx_conf __rte_unused
)
655 struct pmd_internals
*internals
= dev
->data
->dev_private
;
657 dev
->data
->tx_queues
[tx_queue_id
] = &internals
->tx_queue
[tx_queue_id
];
663 eth_rx_queue_start(struct rte_eth_dev
*dev
, uint16_t rx_queue_id
)
665 dev
->data
->rx_queue_state
[rx_queue_id
] = RTE_ETH_QUEUE_STATE_STARTED
;
671 eth_tx_queue_start(struct rte_eth_dev
*dev
, uint16_t tx_queue_id
)
673 dev
->data
->tx_queue_state
[tx_queue_id
] = RTE_ETH_QUEUE_STATE_STARTED
;
679 eth_rx_queue_stop(struct rte_eth_dev
*dev
, uint16_t rx_queue_id
)
681 dev
->data
->rx_queue_state
[rx_queue_id
] = RTE_ETH_QUEUE_STATE_STOPPED
;
687 eth_tx_queue_stop(struct rte_eth_dev
*dev
, uint16_t tx_queue_id
)
689 dev
->data
->tx_queue_state
[tx_queue_id
] = RTE_ETH_QUEUE_STATE_STOPPED
;
694 static const struct eth_dev_ops ops
= {
695 .dev_start
= eth_dev_start
,
696 .dev_stop
= eth_dev_stop
,
697 .dev_close
= eth_dev_close
,
698 .dev_configure
= eth_dev_configure
,
699 .dev_infos_get
= eth_dev_info
,
700 .rx_queue_setup
= eth_rx_queue_setup
,
701 .tx_queue_setup
= eth_tx_queue_setup
,
702 .rx_queue_start
= eth_rx_queue_start
,
703 .tx_queue_start
= eth_tx_queue_start
,
704 .rx_queue_stop
= eth_rx_queue_stop
,
705 .tx_queue_stop
= eth_tx_queue_stop
,
706 .rx_queue_release
= eth_queue_release
,
707 .tx_queue_release
= eth_queue_release
,
708 .link_update
= eth_link_update
,
709 .stats_get
= eth_stats_get
,
710 .stats_reset
= eth_stats_reset
,
714 add_queue(struct pmd_devargs
*pmd
, const char *name
, const char *type
,
715 pcap_t
*pcap
, pcap_dumper_t
*dumper
)
717 if (pmd
->num_of_queue
>= RTE_PMD_PCAP_MAX_QUEUES
)
720 pmd
->queue
[pmd
->num_of_queue
].pcap
= pcap
;
722 pmd
->queue
[pmd
->num_of_queue
].dumper
= dumper
;
723 pmd
->queue
[pmd
->num_of_queue
].name
= name
;
724 pmd
->queue
[pmd
->num_of_queue
].type
= type
;
730 * Function handler that opens the pcap file for reading a stores a
731 * reference of it for use it later on.
734 open_rx_pcap(const char *key
, const char *value
, void *extra_args
)
736 const char *pcap_filename
= value
;
737 struct pmd_devargs
*rx
= extra_args
;
740 if (open_single_rx_pcap(pcap_filename
, &pcap
) < 0)
743 if (add_queue(rx
, pcap_filename
, key
, pcap
, NULL
) < 0) {
752 * Opens a pcap file for writing and stores a reference to it
753 * for use it later on.
756 open_tx_pcap(const char *key
, const char *value
, void *extra_args
)
758 const char *pcap_filename
= value
;
759 struct pmd_devargs
*dumpers
= extra_args
;
760 pcap_dumper_t
*dumper
;
762 if (open_single_tx_pcap(pcap_filename
, &dumper
) < 0)
765 if (add_queue(dumpers
, pcap_filename
, key
, NULL
, dumper
) < 0) {
766 pcap_dump_close(dumper
);
774 * Opens an interface for reading and writing
777 open_rx_tx_iface(const char *key
, const char *value
, void *extra_args
)
779 const char *iface
= value
;
780 struct pmd_devargs
*tx
= extra_args
;
783 if (open_single_iface(iface
, &pcap
) < 0)
786 tx
->queue
[0].pcap
= pcap
;
787 tx
->queue
[0].name
= iface
;
788 tx
->queue
[0].type
= key
;
794 set_iface_direction(const char *iface
, pcap_t
*pcap
,
795 pcap_direction_t direction
)
797 const char *direction_str
= (direction
== PCAP_D_IN
) ? "IN" : "OUT";
798 if (pcap_setdirection(pcap
, direction
) < 0) {
799 PMD_LOG(ERR
, "Setting %s pcap direction %s failed - %s\n",
800 iface
, direction_str
, pcap_geterr(pcap
));
803 PMD_LOG(INFO
, "Setting %s pcap direction %s\n",
804 iface
, direction_str
);
809 open_iface(const char *key
, const char *value
, void *extra_args
)
811 const char *iface
= value
;
812 struct pmd_devargs
*pmd
= extra_args
;
815 if (open_single_iface(iface
, &pcap
) < 0)
817 if (add_queue(pmd
, iface
, key
, pcap
, NULL
) < 0) {
826 * Opens a NIC for reading packets from it
829 open_rx_iface(const char *key
, const char *value
, void *extra_args
)
831 int ret
= open_iface(key
, value
, extra_args
);
834 if (strcmp(key
, ETH_PCAP_RX_IFACE_IN_ARG
) == 0) {
835 struct pmd_devargs
*pmd
= extra_args
;
836 unsigned int qid
= pmd
->num_of_queue
- 1;
838 set_iface_direction(pmd
->queue
[qid
].name
,
839 pmd
->queue
[qid
].pcap
,
847 rx_iface_args_process(const char *key
, const char *value
, void *extra_args
)
849 if (strcmp(key
, ETH_PCAP_RX_IFACE_ARG
) == 0 ||
850 strcmp(key
, ETH_PCAP_RX_IFACE_IN_ARG
) == 0)
851 return open_rx_iface(key
, value
, extra_args
);
857 * Opens a NIC for writing packets to it
860 open_tx_iface(const char *key
, const char *value
, void *extra_args
)
862 return open_iface(key
, value
, extra_args
);
865 static struct rte_vdev_driver pmd_pcap_drv
;
868 pmd_init_internals(struct rte_vdev_device
*vdev
,
869 const unsigned int nb_rx_queues
,
870 const unsigned int nb_tx_queues
,
871 struct pmd_internals
**internals
,
872 struct rte_eth_dev
**eth_dev
)
874 struct rte_eth_dev_data
*data
;
875 unsigned int numa_node
= vdev
->device
.numa_node
;
877 PMD_LOG(INFO
, "Creating pcap-backed ethdev on numa socket %d",
880 /* reserve an ethdev entry */
881 *eth_dev
= rte_eth_vdev_allocate(vdev
, sizeof(**internals
));
885 /* now put it all together
886 * - store queue data in internals,
887 * - store numa_node info in eth_dev
888 * - point eth_dev_data to internals
889 * - and point eth_dev structure to new eth_dev_data structure
891 *internals
= (*eth_dev
)->data
->dev_private
;
892 data
= (*eth_dev
)->data
;
893 data
->nb_rx_queues
= (uint16_t)nb_rx_queues
;
894 data
->nb_tx_queues
= (uint16_t)nb_tx_queues
;
895 data
->dev_link
= pmd_link
;
896 data
->mac_addrs
= ð_addr
;
899 * NOTE: we'll replace the data element, of originally allocated
900 * eth_dev so the rings are local per-process
902 (*eth_dev
)->dev_ops
= &ops
;
908 eth_from_pcaps_common(struct rte_vdev_device
*vdev
,
909 struct pmd_devargs
*rx_queues
, const unsigned int nb_rx_queues
,
910 struct pmd_devargs
*tx_queues
, const unsigned int nb_tx_queues
,
911 struct rte_kvargs
*kvlist
, struct pmd_internals
**internals
,
912 struct rte_eth_dev
**eth_dev
)
914 struct rte_kvargs_pair
*pair
= NULL
;
918 /* do some parameter checking */
919 if (rx_queues
== NULL
&& nb_rx_queues
> 0)
921 if (tx_queues
== NULL
&& nb_tx_queues
> 0)
924 if (pmd_init_internals(vdev
, nb_rx_queues
, nb_tx_queues
, internals
,
928 for (i
= 0; i
< nb_rx_queues
; i
++) {
929 struct pcap_rx_queue
*rx
= &(*internals
)->rx_queue
[i
];
930 struct devargs_queue
*queue
= &rx_queues
->queue
[i
];
932 rx
->pcap
= queue
->pcap
;
933 snprintf(rx
->name
, sizeof(rx
->name
), "%s", queue
->name
);
934 snprintf(rx
->type
, sizeof(rx
->type
), "%s", queue
->type
);
937 for (i
= 0; i
< nb_tx_queues
; i
++) {
938 struct pcap_tx_queue
*tx
= &(*internals
)->tx_queue
[i
];
939 struct devargs_queue
*queue
= &tx_queues
->queue
[i
];
941 tx
->dumper
= queue
->dumper
;
942 tx
->pcap
= queue
->pcap
;
943 snprintf(tx
->name
, sizeof(tx
->name
), "%s", queue
->name
);
944 snprintf(tx
->type
, sizeof(tx
->type
), "%s", queue
->type
);
947 for (k_idx
= 0; k_idx
< kvlist
->count
; k_idx
++) {
948 pair
= &kvlist
->pairs
[k_idx
];
949 if (strstr(pair
->key
, ETH_PCAP_IFACE_ARG
) != NULL
)
954 (*internals
)->if_index
= 0;
956 (*internals
)->if_index
= if_nametoindex(pair
->value
);
962 eth_from_pcaps(struct rte_vdev_device
*vdev
,
963 struct pmd_devargs
*rx_queues
, const unsigned int nb_rx_queues
,
964 struct pmd_devargs
*tx_queues
, const unsigned int nb_tx_queues
,
965 struct rte_kvargs
*kvlist
, int single_iface
,
966 unsigned int using_dumpers
)
968 struct pmd_internals
*internals
= NULL
;
969 struct rte_eth_dev
*eth_dev
= NULL
;
972 ret
= eth_from_pcaps_common(vdev
, rx_queues
, nb_rx_queues
,
973 tx_queues
, nb_tx_queues
, kvlist
, &internals
, ð_dev
);
978 /* store weather we are using a single interface for rx/tx or not */
979 internals
->single_iface
= single_iface
;
981 eth_dev
->rx_pkt_burst
= eth_pcap_rx
;
984 eth_dev
->tx_pkt_burst
= eth_pcap_tx_dumper
;
986 eth_dev
->tx_pkt_burst
= eth_pcap_tx
;
988 rte_eth_dev_probing_finish(eth_dev
);
993 pmd_pcap_probe(struct rte_vdev_device
*dev
)
996 unsigned int is_rx_pcap
= 0, is_tx_pcap
= 0;
997 struct rte_kvargs
*kvlist
;
998 struct pmd_devargs pcaps
= {0};
999 struct pmd_devargs dumpers
= {0};
1000 struct rte_eth_dev
*eth_dev
;
1001 int single_iface
= 0;
1004 name
= rte_vdev_device_name(dev
);
1005 PMD_LOG(INFO
, "Initializing pmd_pcap for %s", name
);
1007 gettimeofday(&start_time
, NULL
);
1008 start_cycles
= rte_get_timer_cycles();
1009 hz
= rte_get_timer_hz();
1011 if (rte_eal_process_type() == RTE_PROC_SECONDARY
&&
1012 strlen(rte_vdev_device_args(dev
)) == 0) {
1013 eth_dev
= rte_eth_dev_attach_secondary(name
);
1015 PMD_LOG(ERR
, "Failed to probe %s", name
);
1018 /* TODO: request info from primary to set up Rx and Tx */
1019 eth_dev
->dev_ops
= &ops
;
1020 eth_dev
->device
= &dev
->device
;
1021 rte_eth_dev_probing_finish(eth_dev
);
1025 kvlist
= rte_kvargs_parse(rte_vdev_device_args(dev
), valid_arguments
);
1030 * If iface argument is passed we open the NICs and use them for
1033 if (rte_kvargs_count(kvlist
, ETH_PCAP_IFACE_ARG
) == 1) {
1035 ret
= rte_kvargs_process(kvlist
, ETH_PCAP_IFACE_ARG
,
1036 &open_rx_tx_iface
, &pcaps
);
1041 dumpers
.queue
[0] = pcaps
.queue
[0];
1044 pcaps
.num_of_queue
= 1;
1045 dumpers
.num_of_queue
= 1;
1051 * We check whether we want to open a RX stream from a real NIC or a
1054 is_rx_pcap
= rte_kvargs_count(kvlist
, ETH_PCAP_RX_PCAP_ARG
) ? 1 : 0;
1055 pcaps
.num_of_queue
= 0;
1058 ret
= rte_kvargs_process(kvlist
, ETH_PCAP_RX_PCAP_ARG
,
1059 &open_rx_pcap
, &pcaps
);
1061 ret
= rte_kvargs_process(kvlist
, NULL
,
1062 &rx_iface_args_process
, &pcaps
);
1069 * We check whether we want to open a TX stream to a real NIC or a
1072 is_tx_pcap
= rte_kvargs_count(kvlist
, ETH_PCAP_TX_PCAP_ARG
) ? 1 : 0;
1073 dumpers
.num_of_queue
= 0;
1076 ret
= rte_kvargs_process(kvlist
, ETH_PCAP_TX_PCAP_ARG
,
1077 &open_tx_pcap
, &dumpers
);
1079 ret
= rte_kvargs_process(kvlist
, ETH_PCAP_TX_IFACE_ARG
,
1080 &open_tx_iface
, &dumpers
);
1086 ret
= eth_from_pcaps(dev
, &pcaps
, pcaps
.num_of_queue
, &dumpers
,
1087 dumpers
.num_of_queue
, kvlist
, single_iface
, is_tx_pcap
);
1090 rte_kvargs_free(kvlist
);
1096 pmd_pcap_remove(struct rte_vdev_device
*dev
)
1098 struct rte_eth_dev
*eth_dev
= NULL
;
1100 PMD_LOG(INFO
, "Closing pcap ethdev on numa socket %d",
1106 /* reserve an ethdev entry */
1107 eth_dev
= rte_eth_dev_allocated(rte_vdev_device_name(dev
));
1108 if (eth_dev
== NULL
)
1111 rte_free(eth_dev
->data
->dev_private
);
1113 rte_eth_dev_release_port(eth_dev
);
1118 static struct rte_vdev_driver pmd_pcap_drv
= {
1119 .probe
= pmd_pcap_probe
,
1120 .remove
= pmd_pcap_remove
,
1123 RTE_PMD_REGISTER_VDEV(net_pcap
, pmd_pcap_drv
);
1124 RTE_PMD_REGISTER_ALIAS(net_pcap
, eth_pcap
);
1125 RTE_PMD_REGISTER_PARAM_STRING(net_pcap
,
1126 ETH_PCAP_RX_PCAP_ARG
"=<string> "
1127 ETH_PCAP_TX_PCAP_ARG
"=<string> "
1128 ETH_PCAP_RX_IFACE_ARG
"=<ifc> "
1129 ETH_PCAP_RX_IFACE_IN_ARG
"=<ifc> "
1130 ETH_PCAP_TX_IFACE_ARG
"=<ifc> "
1131 ETH_PCAP_IFACE_ARG
"=<ifc>");
1133 RTE_INIT(eth_pcap_init_log
)
1135 eth_pcap_logtype
= rte_log_register("pmd.net.pcap");
1136 if (eth_pcap_logtype
>= 0)
1137 rte_log_set_level(eth_pcap_logtype
, RTE_LOG_NOTICE
);