4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5 * Copyright(c) 2014 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <rte_ethdev.h>
38 #include <rte_malloc.h>
39 #include <rte_memcpy.h>
40 #include <rte_string_fns.h>
41 #include <rte_cycles.h>
42 #include <rte_kvargs.h>
49 #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535
50 #define RTE_ETH_PCAP_SNAPLEN ETHER_MAX_JUMBO_FRAME_LEN
51 #define RTE_ETH_PCAP_PROMISC 1
52 #define RTE_ETH_PCAP_TIMEOUT -1
53 #define ETH_PCAP_RX_PCAP_ARG "rx_pcap"
54 #define ETH_PCAP_TX_PCAP_ARG "tx_pcap"
55 #define ETH_PCAP_RX_IFACE_ARG "rx_iface"
56 #define ETH_PCAP_TX_IFACE_ARG "tx_iface"
57 #define ETH_PCAP_IFACE_ARG "iface"
59 #define ETH_PCAP_ARG_MAXLEN 64
61 static char errbuf[PCAP_ERRBUF_SIZE];
62 static unsigned char tx_pcap_data[RTE_ETH_PCAP_SNAPLEN];
63 static struct timeval start_time;
64 static uint64_t start_cycles;
67 struct pcap_rx_queue {
70 struct rte_mempool *mb_pool;
71 volatile unsigned long rx_pkts;
72 volatile unsigned long rx_bytes;
73 volatile unsigned long err_pkts;
75 char type[ETH_PCAP_ARG_MAXLEN];
78 struct pcap_tx_queue {
79 pcap_dumper_t *dumper;
81 volatile unsigned long tx_pkts;
82 volatile unsigned long tx_bytes;
83 volatile unsigned long err_pkts;
85 char type[ETH_PCAP_ARG_MAXLEN];
90 pcap_t *pcaps[RTE_PMD_RING_MAX_RX_RINGS];
91 const char *names[RTE_PMD_RING_MAX_RX_RINGS];
92 const char *types[RTE_PMD_RING_MAX_RX_RINGS];
97 pcap_dumper_t *dumpers[RTE_PMD_RING_MAX_TX_RINGS];
98 pcap_t *pcaps[RTE_PMD_RING_MAX_RX_RINGS];
99 const char *names[RTE_PMD_RING_MAX_RX_RINGS];
100 const char *types[RTE_PMD_RING_MAX_RX_RINGS];
103 struct pmd_internals {
104 struct pcap_rx_queue rx_queue[RTE_PMD_RING_MAX_RX_RINGS];
105 struct pcap_tx_queue tx_queue[RTE_PMD_RING_MAX_TX_RINGS];
110 const char *valid_arguments[] = {
111 ETH_PCAP_RX_PCAP_ARG,
112 ETH_PCAP_TX_PCAP_ARG,
113 ETH_PCAP_RX_IFACE_ARG,
114 ETH_PCAP_TX_IFACE_ARG,
119 static int open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper);
120 static int open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap);
121 static int open_single_iface(const char *iface, pcap_t **pcap);
123 static struct ether_addr eth_addr = { .addr_bytes = { 0, 0, 0, 0x1, 0x2, 0x3 } };
124 static const char *drivername = "Pcap PMD";
125 static struct rte_eth_link pmd_link = {
126 .link_speed = ETH_SPEED_NUM_10G,
127 .link_duplex = ETH_LINK_FULL_DUPLEX,
128 .link_status = ETH_LINK_DOWN,
129 .link_autoneg = ETH_LINK_SPEED_FIXED,
133 eth_pcap_rx_jumbo(struct rte_mempool *mb_pool,
134 struct rte_mbuf *mbuf,
138 struct rte_mbuf *m = mbuf;
140 /* Copy the first segment. */
141 uint16_t len = rte_pktmbuf_tailroom(mbuf);
143 rte_memcpy(rte_pktmbuf_append(mbuf, len), data, len);
147 while (data_len > 0) {
148 /* Allocate next mbuf and point to that. */
149 m->next = rte_pktmbuf_alloc(mb_pool);
151 if (unlikely(!m->next))
156 /* Headroom is not needed in chained mbufs. */
157 rte_pktmbuf_prepend(m, rte_pktmbuf_headroom(m));
161 /* Copy next segment. */
162 len = RTE_MIN(rte_pktmbuf_tailroom(m), data_len);
163 rte_memcpy(rte_pktmbuf_append(m, len), data, len);
170 return mbuf->nb_segs;
173 /* Copy data from mbuf chain to a buffer suitable for writing to a PCAP file. */
175 eth_pcap_gather_data(unsigned char *data, struct rte_mbuf *mbuf)
177 uint16_t data_len = 0;
180 rte_memcpy(data + data_len, rte_pktmbuf_mtod(mbuf, void *),
183 data_len += mbuf->data_len;
189 eth_pcap_rx(void *queue,
190 struct rte_mbuf **bufs,
194 struct pcap_pkthdr header;
195 const u_char *packet;
196 struct rte_mbuf *mbuf;
197 struct pcap_rx_queue *pcap_q = queue;
200 uint32_t rx_bytes = 0;
202 if (unlikely(pcap_q->pcap == NULL || nb_pkts == 0))
205 /* Reads the given number of packets from the pcap file one by one
206 * and copies the packet data into a newly allocated mbuf to return.
208 for (i = 0; i < nb_pkts; i++) {
209 /* Get the next PCAP packet */
210 packet = pcap_next(pcap_q->pcap, &header);
211 if (unlikely(packet == NULL))
214 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
215 if (unlikely(mbuf == NULL))
218 /* Now get the space available for data in the mbuf */
219 buf_size = (uint16_t)(rte_pktmbuf_data_room_size(pcap_q->mb_pool) -
220 RTE_PKTMBUF_HEADROOM);
222 if (header.caplen <= buf_size) {
223 /* pcap packet will fit in the mbuf, go ahead and copy */
224 rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), packet,
226 mbuf->data_len = (uint16_t)header.caplen;
228 /* Try read jumbo frame into multi mbufs. */
229 if (unlikely(eth_pcap_rx_jumbo(pcap_q->mb_pool,
232 header.caplen) == -1))
236 mbuf->pkt_len = (uint16_t)header.caplen;
237 mbuf->port = pcap_q->in_port;
240 rx_bytes += header.caplen;
242 pcap_q->rx_pkts += num_rx;
243 pcap_q->rx_bytes += rx_bytes;
248 calculate_timestamp(struct timeval *ts) {
250 struct timeval cur_time;
252 cycles = rte_get_timer_cycles() - start_cycles;
253 cur_time.tv_sec = cycles / hz;
254 cur_time.tv_usec = (cycles % hz) * 10e6 / hz;
255 timeradd(&start_time, &cur_time, ts);
259 * Callback to handle writing packets to a pcap file.
262 eth_pcap_tx_dumper(void *queue,
263 struct rte_mbuf **bufs,
267 struct rte_mbuf *mbuf;
268 struct pcap_tx_queue *dumper_q = queue;
270 uint32_t tx_bytes = 0;
271 struct pcap_pkthdr header;
273 if (dumper_q->dumper == NULL || nb_pkts == 0)
276 /* writes the nb_pkts packets to the previously opened pcap file dumper */
277 for (i = 0; i < nb_pkts; i++) {
279 calculate_timestamp(&header.ts);
280 header.len = mbuf->pkt_len;
281 header.caplen = header.len;
283 if (likely(mbuf->nb_segs == 1)) {
284 pcap_dump((u_char *)dumper_q->dumper, &header,
285 rte_pktmbuf_mtod(mbuf, void*));
287 if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
288 eth_pcap_gather_data(tx_pcap_data, mbuf);
289 pcap_dump((u_char *)dumper_q->dumper, &header,
293 "Dropping PCAP packet. "
294 "Size (%d) > max jumbo size (%d).\n",
296 ETHER_MAX_JUMBO_FRAME_LEN);
298 rte_pktmbuf_free(mbuf);
303 rte_pktmbuf_free(mbuf);
305 tx_bytes += mbuf->pkt_len;
309 * Since there's no place to hook a callback when the forwarding
310 * process stops and to make sure the pcap file is actually written,
311 * we flush the pcap dumper within each burst.
313 pcap_dump_flush(dumper_q->dumper);
314 dumper_q->tx_pkts += num_tx;
315 dumper_q->tx_bytes += tx_bytes;
316 dumper_q->err_pkts += nb_pkts - num_tx;
321 * Callback to handle sending packets through a real NIC.
324 eth_pcap_tx(void *queue,
325 struct rte_mbuf **bufs,
330 struct rte_mbuf *mbuf;
331 struct pcap_tx_queue *tx_queue = queue;
333 uint32_t tx_bytes = 0;
335 if (unlikely(nb_pkts == 0 || tx_queue->pcap == NULL))
338 for (i = 0; i < nb_pkts; i++) {
341 if (likely(mbuf->nb_segs == 1)) {
342 ret = pcap_sendpacket(tx_queue->pcap,
343 rte_pktmbuf_mtod(mbuf, u_char *),
346 if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
347 eth_pcap_gather_data(tx_pcap_data, mbuf);
348 ret = pcap_sendpacket(tx_queue->pcap,
353 "Dropping PCAP packet. "
354 "Size (%d) > max jumbo size (%d).\n",
356 ETHER_MAX_JUMBO_FRAME_LEN);
358 rte_pktmbuf_free(mbuf);
363 if (unlikely(ret != 0))
366 tx_bytes += mbuf->pkt_len;
367 rte_pktmbuf_free(mbuf);
370 tx_queue->tx_pkts += num_tx;
371 tx_queue->tx_bytes += tx_bytes;
372 tx_queue->err_pkts += nb_pkts - num_tx;
377 eth_dev_start(struct rte_eth_dev *dev)
380 struct pmd_internals *internals = dev->data->dev_private;
381 struct pcap_tx_queue *tx;
382 struct pcap_rx_queue *rx;
384 /* Special iface case. Single pcap is open and shared between tx/rx. */
385 if (internals->single_iface) {
386 tx = &internals->tx_queue[0];
387 rx = &internals->rx_queue[0];
389 if (!tx->pcap && strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
390 if (open_single_iface(tx->name, &tx->pcap) < 0)
397 /* If not open already, open tx pcaps/dumpers */
398 for (i = 0; i < dev->data->nb_tx_queues; i++) {
399 tx = &internals->tx_queue[i];
401 if (!tx->dumper && strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
402 if (open_single_tx_pcap(tx->name, &tx->dumper) < 0)
406 else if (!tx->pcap && strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
407 if (open_single_iface(tx->name, &tx->pcap) < 0)
412 /* If not open already, open rx pcaps */
413 for (i = 0; i < dev->data->nb_rx_queues; i++) {
414 rx = &internals->rx_queue[i];
416 if (rx->pcap != NULL)
419 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
420 if (open_single_rx_pcap(rx->name, &rx->pcap) < 0)
424 else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
425 if (open_single_iface(rx->name, &rx->pcap) < 0)
432 dev->data->dev_link.link_status = ETH_LINK_UP;
437 * This function gets called when the current port gets stopped.
438 * Is the only place for us to close all the tx streams dumpers.
439 * If not called the dumpers will be flushed within each tx burst.
442 eth_dev_stop(struct rte_eth_dev *dev)
445 struct pmd_internals *internals = dev->data->dev_private;
446 struct pcap_tx_queue *tx;
447 struct pcap_rx_queue *rx;
449 /* Special iface case. Single pcap is open and shared between tx/rx. */
450 if (internals->single_iface) {
451 tx = &internals->tx_queue[0];
452 rx = &internals->rx_queue[0];
453 pcap_close(tx->pcap);
459 for (i = 0; i < dev->data->nb_tx_queues; i++) {
460 tx = &internals->tx_queue[i];
462 if (tx->dumper != NULL) {
463 pcap_dump_close(tx->dumper);
467 if (tx->pcap != NULL) {
468 pcap_close(tx->pcap);
473 for (i = 0; i < dev->data->nb_rx_queues; i++) {
474 rx = &internals->rx_queue[i];
476 if (rx->pcap != NULL) {
477 pcap_close(rx->pcap);
483 dev->data->dev_link.link_status = ETH_LINK_DOWN;
487 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
493 eth_dev_info(struct rte_eth_dev *dev,
494 struct rte_eth_dev_info *dev_info)
496 struct pmd_internals *internals = dev->data->dev_private;
497 dev_info->driver_name = drivername;
498 dev_info->if_index = internals->if_index;
499 dev_info->max_mac_addrs = 1;
500 dev_info->max_rx_pktlen = (uint32_t) -1;
501 dev_info->max_rx_queues = dev->data->nb_rx_queues;
502 dev_info->max_tx_queues = dev->data->nb_tx_queues;
503 dev_info->min_rx_bufsize = 0;
504 dev_info->pci_dev = NULL;
508 eth_stats_get(struct rte_eth_dev *dev,
509 struct rte_eth_stats *igb_stats)
512 unsigned long rx_packets_total = 0, rx_bytes_total = 0;
513 unsigned long tx_packets_total = 0, tx_bytes_total = 0;
514 unsigned long tx_packets_err_total = 0;
515 const struct pmd_internals *internal = dev->data->dev_private;
517 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
518 i < dev->data->nb_rx_queues; i++) {
519 igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts;
520 igb_stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes;
521 rx_packets_total += igb_stats->q_ipackets[i];
522 rx_bytes_total += igb_stats->q_ibytes[i];
525 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
526 i < dev->data->nb_tx_queues; i++) {
527 igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts;
528 igb_stats->q_obytes[i] = internal->tx_queue[i].tx_bytes;
529 igb_stats->q_errors[i] = internal->tx_queue[i].err_pkts;
530 tx_packets_total += igb_stats->q_opackets[i];
531 tx_bytes_total += igb_stats->q_obytes[i];
532 tx_packets_err_total += igb_stats->q_errors[i];
535 igb_stats->ipackets = rx_packets_total;
536 igb_stats->ibytes = rx_bytes_total;
537 igb_stats->opackets = tx_packets_total;
538 igb_stats->obytes = tx_bytes_total;
539 igb_stats->oerrors = tx_packets_err_total;
543 eth_stats_reset(struct rte_eth_dev *dev)
546 struct pmd_internals *internal = dev->data->dev_private;
547 for (i = 0; i < dev->data->nb_rx_queues; i++) {
548 internal->rx_queue[i].rx_pkts = 0;
549 internal->rx_queue[i].rx_bytes = 0;
551 for (i = 0; i < dev->data->nb_tx_queues; i++) {
552 internal->tx_queue[i].tx_pkts = 0;
553 internal->tx_queue[i].tx_bytes = 0;
554 internal->tx_queue[i].err_pkts = 0;
559 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
564 eth_queue_release(void *q __rte_unused)
569 eth_link_update(struct rte_eth_dev *dev __rte_unused,
570 int wait_to_complete __rte_unused)
576 eth_rx_queue_setup(struct rte_eth_dev *dev,
577 uint16_t rx_queue_id,
578 uint16_t nb_rx_desc __rte_unused,
579 unsigned int socket_id __rte_unused,
580 const struct rte_eth_rxconf *rx_conf __rte_unused,
581 struct rte_mempool *mb_pool)
583 struct pmd_internals *internals = dev->data->dev_private;
584 struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
585 pcap_q->mb_pool = mb_pool;
586 dev->data->rx_queues[rx_queue_id] = pcap_q;
587 pcap_q->in_port = dev->data->port_id;
592 eth_tx_queue_setup(struct rte_eth_dev *dev,
593 uint16_t tx_queue_id,
594 uint16_t nb_tx_desc __rte_unused,
595 unsigned int socket_id __rte_unused,
596 const struct rte_eth_txconf *tx_conf __rte_unused)
599 struct pmd_internals *internals = dev->data->dev_private;
600 dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
604 static const struct eth_dev_ops ops = {
605 .dev_start = eth_dev_start,
606 .dev_stop = eth_dev_stop,
607 .dev_close = eth_dev_close,
608 .dev_configure = eth_dev_configure,
609 .dev_infos_get = eth_dev_info,
610 .rx_queue_setup = eth_rx_queue_setup,
611 .tx_queue_setup = eth_tx_queue_setup,
612 .rx_queue_release = eth_queue_release,
613 .tx_queue_release = eth_queue_release,
614 .link_update = eth_link_update,
615 .stats_get = eth_stats_get,
616 .stats_reset = eth_stats_reset,
620 * Function handler that opens the pcap file for reading a stores a
621 * reference of it for use it later on.
624 open_rx_pcap(const char *key, const char *value, void *extra_args)
627 const char *pcap_filename = value;
628 struct rx_pcaps *pcaps = extra_args;
631 for (i = 0; i < pcaps->num_of_rx; i++) {
632 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
635 pcaps->pcaps[i] = pcap;
636 pcaps->names[i] = pcap_filename;
637 pcaps->types[i] = key;
644 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
646 if ((*pcap = pcap_open_offline(pcap_filename, errbuf)) == NULL) {
647 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename, errbuf);
654 * Opens a pcap file for writing and stores a reference to it
655 * for use it later on.
658 open_tx_pcap(const char *key, const char *value, void *extra_args)
661 const char *pcap_filename = value;
662 struct tx_pcaps *dumpers = extra_args;
663 pcap_dumper_t *dumper;
665 for (i = 0; i < dumpers->num_of_tx; i++) {
666 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
669 dumpers->dumpers[i] = dumper;
670 dumpers->names[i] = pcap_filename;
671 dumpers->types[i] = key;
678 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
682 * We need to create a dummy empty pcap_t to use it
683 * with pcap_dump_open(). We create big enough an Ethernet
687 if ((tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN))
689 RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
693 /* The dumper is created using the previous pcap_t reference */
694 if ((*dumper = pcap_dump_open(tx_pcap, pcap_filename)) == NULL) {
695 RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n", pcap_filename);
703 * pcap_open_live wrapper function
706 open_iface_live(const char *iface, pcap_t **pcap) {
707 *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
708 RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
711 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
718 * Opens an interface for reading and writing
721 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
723 const char *iface = value;
724 struct rx_pcaps *pcaps = extra_args;
727 if (open_single_iface(iface, &pcap) < 0)
730 pcaps->pcaps[0] = pcap;
731 pcaps->names[0] = iface;
732 pcaps->types[0] = key;
738 * Opens a NIC for reading packets from it
741 open_rx_iface(const char *key, const char *value, void *extra_args)
744 const char *iface = value;
745 struct rx_pcaps *pcaps = extra_args;
748 for (i = 0; i < pcaps->num_of_rx; i++) {
749 if (open_single_iface(iface, &pcap) < 0)
751 pcaps->pcaps[i] = pcap;
752 pcaps->names[i] = iface;
753 pcaps->types[i] = key;
760 * Opens a NIC for writing packets to it
763 open_tx_iface(const char *key, const char *value, void *extra_args)
766 const char *iface = value;
767 struct tx_pcaps *pcaps = extra_args;
770 for (i = 0; i < pcaps->num_of_tx; i++) {
771 if (open_single_iface(iface, &pcap) < 0)
773 pcaps->pcaps[i] = pcap;
774 pcaps->names[i] = iface;
775 pcaps->types[i] = key;
782 open_single_iface(const char *iface, pcap_t **pcap)
784 if (open_iface_live(iface, pcap) < 0) {
785 RTE_LOG(ERR, PMD, "Couldn't open interface %s\n", iface);
793 rte_pmd_init_internals(const char *name, const unsigned nb_rx_queues,
794 const unsigned nb_tx_queues,
795 const unsigned numa_node,
796 struct pmd_internals **internals,
797 struct rte_eth_dev **eth_dev,
798 struct rte_kvargs *kvlist)
800 struct rte_eth_dev_data *data = NULL;
802 struct rte_kvargs_pair *pair = NULL;
804 for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
805 pair = &kvlist->pairs[k_idx];
806 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
811 "Creating pcap-backed ethdev on numa socket %u\n", numa_node);
813 /* now do all data allocation - for eth_dev structure
814 * and internal (private) data
816 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
820 *internals = rte_zmalloc_socket(name, sizeof(**internals), 0, numa_node);
821 if (*internals == NULL)
824 /* reserve an ethdev entry */
825 *eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
826 if (*eth_dev == NULL)
829 /* check length of device name */
830 if ((strlen((*eth_dev)->data->name) + 1) > sizeof(data->name))
833 /* now put it all together
834 * - store queue data in internals,
835 * - store numa_node info in eth_dev
836 * - point eth_dev_data to internals
837 * - and point eth_dev structure to new eth_dev_data structure
839 /* NOTE: we'll replace the data element, of originally allocated eth_dev
840 * so the rings are local per-process */
843 (*internals)->if_index = 0;
845 (*internals)->if_index = if_nametoindex(pair->value);
847 data->dev_private = *internals;
848 data->port_id = (*eth_dev)->data->port_id;
849 snprintf(data->name, sizeof(data->name), "%s", (*eth_dev)->data->name);
850 data->nb_rx_queues = (uint16_t)nb_rx_queues;
851 data->nb_tx_queues = (uint16_t)nb_tx_queues;
852 data->dev_link = pmd_link;
853 data->mac_addrs = ð_addr;
855 (*eth_dev)->data->name, strlen((*eth_dev)->data->name));
857 (*eth_dev)->data = data;
858 (*eth_dev)->dev_ops = &ops;
859 (*eth_dev)->driver = NULL;
860 data->dev_flags = RTE_ETH_DEV_DETACHABLE;
861 data->kdrv = RTE_KDRV_NONE;
862 data->drv_name = drivername;
863 data->numa_node = numa_node;
869 rte_free(*internals);
875 rte_eth_from_pcaps_common(const char *name, struct rx_pcaps *rx_queues,
876 const unsigned nb_rx_queues, struct tx_pcaps *tx_queues,
877 const unsigned nb_tx_queues, const unsigned numa_node,
878 struct rte_kvargs *kvlist, struct pmd_internals **internals,
879 struct rte_eth_dev **eth_dev)
883 /* do some parameter checking */
884 if (rx_queues == NULL && nb_rx_queues > 0)
886 if (tx_queues == NULL && nb_tx_queues > 0)
889 if (rte_pmd_init_internals(name, nb_rx_queues, nb_tx_queues, numa_node,
890 internals, eth_dev, kvlist) < 0)
893 for (i = 0; i < nb_rx_queues; i++) {
894 (*internals)->rx_queue[i].pcap = rx_queues->pcaps[i];
895 snprintf((*internals)->rx_queue[i].name,
896 sizeof((*internals)->rx_queue[i].name), "%s",
897 rx_queues->names[i]);
898 snprintf((*internals)->rx_queue[i].type,
899 sizeof((*internals)->rx_queue[i].type), "%s",
900 rx_queues->types[i]);
902 for (i = 0; i < nb_tx_queues; i++) {
903 (*internals)->tx_queue[i].dumper = tx_queues->dumpers[i];
904 snprintf((*internals)->tx_queue[i].name,
905 sizeof((*internals)->tx_queue[i].name), "%s",
906 tx_queues->names[i]);
907 snprintf((*internals)->tx_queue[i].type,
908 sizeof((*internals)->tx_queue[i].type), "%s",
909 tx_queues->types[i]);
916 rte_eth_from_pcaps_n_dumpers(const char *name,
917 struct rx_pcaps *rx_queues,
918 const unsigned nb_rx_queues,
919 struct tx_pcaps *tx_queues,
920 const unsigned nb_tx_queues,
921 const unsigned numa_node,
922 struct rte_kvargs *kvlist)
924 struct pmd_internals *internals = NULL;
925 struct rte_eth_dev *eth_dev = NULL;
928 ret = rte_eth_from_pcaps_common(name, rx_queues, nb_rx_queues,
929 tx_queues, nb_tx_queues, numa_node, kvlist,
930 &internals, ð_dev);
935 /* using multiple pcaps/interfaces */
936 internals->single_iface = 0;
938 eth_dev->rx_pkt_burst = eth_pcap_rx;
939 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
945 rte_eth_from_pcaps(const char *name,
946 struct rx_pcaps *rx_queues,
947 const unsigned nb_rx_queues,
948 struct tx_pcaps *tx_queues,
949 const unsigned nb_tx_queues,
950 const unsigned numa_node,
951 struct rte_kvargs *kvlist,
954 struct pmd_internals *internals = NULL;
955 struct rte_eth_dev *eth_dev = NULL;
958 ret = rte_eth_from_pcaps_common(name, rx_queues, nb_rx_queues,
959 tx_queues, nb_tx_queues, numa_node, kvlist,
960 &internals, ð_dev);
965 /* store wether we are using a single interface for rx/tx or not */
966 internals->single_iface = single_iface;
968 eth_dev->rx_pkt_burst = eth_pcap_rx;
969 eth_dev->tx_pkt_burst = eth_pcap_tx;
976 rte_pmd_pcap_devinit(const char *name, const char *params)
978 unsigned numa_node, using_dumpers = 0;
980 struct rte_kvargs *kvlist;
981 struct rx_pcaps pcaps;
982 struct tx_pcaps dumpers;
984 RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
986 numa_node = rte_socket_id();
988 gettimeofday(&start_time, NULL);
989 start_cycles = rte_get_timer_cycles();
990 hz = rte_get_timer_hz();
992 kvlist = rte_kvargs_parse(params, valid_arguments);
997 * If iface argument is passed we open the NICs and use them for
1000 if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
1002 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
1003 &open_rx_tx_iface, &pcaps);
1006 dumpers.pcaps[0] = pcaps.pcaps[0];
1007 dumpers.names[0] = pcaps.names[0];
1008 dumpers.types[0] = pcaps.types[0];
1009 ret = rte_eth_from_pcaps(name, &pcaps, 1, &dumpers, 1,
1010 numa_node, kvlist, 1);
1015 * We check whether we want to open a RX stream from a real NIC or a
1018 if ((pcaps.num_of_rx = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG))) {
1019 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
1020 &open_rx_pcap, &pcaps);
1022 pcaps.num_of_rx = rte_kvargs_count(kvlist,
1023 ETH_PCAP_RX_IFACE_ARG);
1024 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
1025 &open_rx_iface, &pcaps);
1032 * We check whether we want to open a TX stream to a real NIC or a
1035 if ((dumpers.num_of_tx = rte_kvargs_count(kvlist,
1036 ETH_PCAP_TX_PCAP_ARG))) {
1037 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1038 &open_tx_pcap, &dumpers);
1041 dumpers.num_of_tx = rte_kvargs_count(kvlist,
1042 ETH_PCAP_TX_IFACE_ARG);
1043 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1044 &open_tx_iface, &dumpers);
1051 ret = rte_eth_from_pcaps_n_dumpers(name, &pcaps, pcaps.num_of_rx,
1052 &dumpers, dumpers.num_of_tx, numa_node, kvlist);
1054 ret = rte_eth_from_pcaps(name, &pcaps, pcaps.num_of_rx, &dumpers,
1055 dumpers.num_of_tx, numa_node, kvlist, 0);
1058 rte_kvargs_free(kvlist);
1063 rte_pmd_pcap_devuninit(const char *name)
1065 struct rte_eth_dev *eth_dev = NULL;
1067 RTE_LOG(INFO, PMD, "Closing pcap ethdev on numa socket %u\n",
1073 /* reserve an ethdev entry */
1074 eth_dev = rte_eth_dev_allocated(name);
1075 if (eth_dev == NULL)
1078 rte_free(eth_dev->data->dev_private);
1079 rte_free(eth_dev->data);
1081 rte_eth_dev_release_port(eth_dev);
1086 static struct rte_driver pmd_pcap_drv = {
1089 .init = rte_pmd_pcap_devinit,
1090 .uninit = rte_pmd_pcap_devuninit,
1093 PMD_REGISTER_DRIVER(pmd_pcap_drv);