4 * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/types.h>
36 #include <sys/queue.h>
46 #include <rte_byteorder.h>
48 #include <rte_debug.h>
49 #include <rte_interrupts.h>
51 #include <rte_memory.h>
52 #include <rte_memcpy.h>
53 #include <rte_memzone.h>
54 #include <rte_launch.h>
55 #include <rte_tailq.h>
57 #include <rte_per_lcore.h>
58 #include <rte_lcore.h>
59 #include <rte_atomic.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_common.h>
63 #include <rte_mempool.h>
64 #include <rte_malloc.h>
66 #include <rte_errno.h>
67 #include <rte_spinlock.h>
69 #include "rte_ether.h"
70 #include "rte_ethdev.h"
72 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
73 #define PMD_DEBUG_TRACE(fmt, args...) do { \
74 RTE_LOG(ERR, PMD, "%s: " fmt, __func__, ## args); \
77 #define PMD_DEBUG_TRACE(fmt, args...)
80 /* Macros for checking for restricting functions to primary instance only */
81 #define PROC_PRIMARY_OR_ERR_RET(retval) do { \
82 if (rte_eal_process_type() != RTE_PROC_PRIMARY) { \
83 PMD_DEBUG_TRACE("Cannot run in secondary processes\n"); \
87 #define PROC_PRIMARY_OR_RET() do { \
88 if (rte_eal_process_type() != RTE_PROC_PRIMARY) { \
89 PMD_DEBUG_TRACE("Cannot run in secondary processes\n"); \
94 /* Macros to check for invlaid function pointers in dev_ops structure */
95 #define FUNC_PTR_OR_ERR_RET(func, retval) do { \
96 if ((func) == NULL) { \
97 PMD_DEBUG_TRACE("Function not supported\n"); \
101 #define FUNC_PTR_OR_RET(func) do { \
102 if ((func) == NULL) { \
103 PMD_DEBUG_TRACE("Function not supported\n"); \
108 static const char *MZ_RTE_ETH_DEV_DATA = "rte_eth_dev_data";
109 struct rte_eth_dev rte_eth_devices[RTE_MAX_ETHPORTS];
110 static struct rte_eth_dev_data *rte_eth_dev_data = NULL;
111 static uint8_t nb_ports = 0;
113 /* spinlock for eth device callbacks */
114 static rte_spinlock_t rte_eth_dev_cb_lock = RTE_SPINLOCK_INITIALIZER;
117 * The user application callback description.
119 * It contains callback address to be registered by user application,
120 * the pointer to the parameters for callback, and the event type.
122 struct rte_eth_dev_callback {
123 TAILQ_ENTRY(rte_eth_dev_callback) next; /**< Callbacks list */
124 rte_eth_dev_cb_fn cb_fn; /**< Callback address */
125 void *cb_arg; /**< Parameter for callback */
126 enum rte_eth_event_type event; /**< Interrupt event type */
135 rte_eth_dev_data_alloc(void)
137 const unsigned flags = 0;
138 const struct rte_memzone *mz;
140 if (rte_eal_process_type() == RTE_PROC_PRIMARY){
141 mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA,
142 RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data),
143 rte_socket_id(), flags);
145 mz = rte_memzone_lookup(MZ_RTE_ETH_DEV_DATA);
147 rte_panic("Cannot allocate memzone for ethernet port data\n");
149 rte_eth_dev_data = mz->addr;
150 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
151 memset(rte_eth_dev_data, 0,
152 RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data));
155 static inline struct rte_eth_dev *
156 rte_eth_dev_allocate(void)
158 struct rte_eth_dev *eth_dev;
160 if (nb_ports == RTE_MAX_ETHPORTS) {
161 PMD_DEBUG_TRACE("Reached maximum number of ethernet ports\n");
165 if (rte_eth_dev_data == NULL)
166 rte_eth_dev_data_alloc();
168 eth_dev = &rte_eth_devices[nb_ports];
169 eth_dev->data = &rte_eth_dev_data[nb_ports];
170 eth_dev->data->port_id = nb_ports++;
175 rte_eth_dev_init(struct rte_pci_driver *pci_drv,
176 struct rte_pci_device *pci_dev)
178 struct eth_driver *eth_drv;
179 struct rte_eth_dev *eth_dev;
182 eth_drv = (struct eth_driver *)pci_drv;
184 eth_dev = rte_eth_dev_allocate();
188 if (rte_eal_process_type() == RTE_PROC_PRIMARY){
189 eth_dev->data->dev_private = rte_zmalloc("ethdev private structure",
190 eth_drv->dev_private_size,
192 if (eth_dev->data->dev_private == NULL)
193 rte_panic("Cannot allocate memzone for private port data\n");
195 eth_dev->pci_dev = pci_dev;
196 eth_dev->driver = eth_drv;
197 eth_dev->data->rx_mbuf_alloc_failed = 0;
199 /* init user callbacks */
200 TAILQ_INIT(&(eth_dev->callbacks));
203 * Set the default maximum frame size.
205 eth_dev->data->max_frame_size = ETHER_MAX_LEN;
207 /* Invoke PMD device initialization function */
208 diag = (*eth_drv->eth_dev_init)(eth_drv, eth_dev);
212 PMD_DEBUG_TRACE("driver %s: eth_dev_init(vendor_id=0x%u device_id=0x%x)"
213 " failed\n", pci_drv->name,
214 (unsigned) pci_dev->id.vendor_id,
215 (unsigned) pci_dev->id.device_id);
216 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
217 rte_free(eth_dev->data->dev_private);
223 * Register an Ethernet [Poll Mode] driver.
225 * Function invoked by the initialization function of an Ethernet driver
226 * to simultaneously register itself as a PCI driver and as an Ethernet
228 * Invokes the rte_eal_pci_register() function to register the *pci_drv*
229 * structure embedded in the *eth_drv* structure, after having stored the
230 * address of the rte_eth_dev_init() function in the *devinit* field of
231 * the *pci_drv* structure.
232 * During the PCI probing phase, the rte_eth_dev_init() function is
233 * invoked for each PCI [Ethernet device] matching the embedded PCI
234 * identifiers provided by the driver.
237 rte_eth_driver_register(struct eth_driver *eth_drv)
239 eth_drv->pci_drv.devinit = rte_eth_dev_init;
240 rte_eal_pci_register(ð_drv->pci_drv);
244 rte_eth_dev_count(void)
250 rte_eth_dev_configure(uint8_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
251 const struct rte_eth_conf *dev_conf)
253 struct rte_eth_dev *dev;
254 struct rte_eth_dev_info dev_info;
257 /* This function is only safe when called from the primary process
258 * in a multi-process setup*/
259 PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
261 if (port_id >= nb_ports || port_id >= RTE_MAX_ETHPORTS) {
262 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
265 dev = &rte_eth_devices[port_id];
267 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
268 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
270 if (dev->data->dev_started) {
272 "port %d must be stopped to allow configuration\n", port_id);
277 * Check that the numbers of RX and TX queues are not greater
278 * than the maximum number of RX and TX queues supported by the
281 (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
282 if (nb_rx_q > dev_info.max_rx_queues) {
283 PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_queues=%d > %d\n",
284 port_id, nb_rx_q, dev_info.max_rx_queues);
288 PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_q == 0\n", port_id);
292 if (nb_tx_q > dev_info.max_tx_queues) {
293 PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_queues=%d > %d\n",
294 port_id, nb_tx_q, dev_info.max_tx_queues);
298 PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_q == 0\n", port_id);
302 /* Copy the dev_conf parameter into the dev structure */
303 memcpy(&dev->data->dev_conf, dev_conf, sizeof(dev->data->dev_conf));
306 * If jumbo frames are enabled, check that the maximum RX packet
307 * length is supported by the configured device.
309 if (dev_conf->rxmode.jumbo_frame == 1) {
310 if (dev_conf->rxmode.max_rx_pkt_len >
311 dev_info.max_rx_pktlen) {
312 PMD_DEBUG_TRACE("ethdev port_id=%d max_rx_pkt_len %u"
313 " > max valid value %u\n",
315 (unsigned)dev_conf->rxmode.max_rx_pkt_len,
316 (unsigned)dev_info.max_rx_pktlen);
320 /* Use default value */
321 dev->data->dev_conf.rxmode.max_rx_pkt_len = ETHER_MAX_LEN;
323 /* For vmdb+dcb mode check our configuration before we go further */
324 if (dev_conf->rxmode.mq_mode == ETH_VMDQ_DCB) {
325 const struct rte_eth_vmdq_dcb_conf *conf;
327 if (nb_rx_q != ETH_VMDQ_DCB_NUM_QUEUES) {
328 PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB, nb_rx_q "
330 port_id, ETH_VMDQ_DCB_NUM_QUEUES);
333 conf = &(dev_conf->rx_adv_conf.vmdq_dcb_conf);
334 if (! (conf->nb_queue_pools == ETH_16_POOLS ||
335 conf->nb_queue_pools == ETH_32_POOLS)) {
336 PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB selected, "
337 "nb_queue_pools must be %d or %d\n",
338 port_id, ETH_16_POOLS, ETH_32_POOLS);
343 diag = (*dev->dev_ops->dev_configure)(dev, nb_rx_q, nb_tx_q);
345 rte_free(dev->data->rx_queues);
346 rte_free(dev->data->tx_queues);
352 rte_eth_dev_config_restore(uint8_t port_id)
354 struct rte_eth_dev *dev;
355 struct rte_eth_dev_info dev_info;
356 struct ether_addr addr;
359 dev = &rte_eth_devices[port_id];
361 rte_eth_dev_info_get(port_id, &dev_info);
363 /* replay MAC address configuration */
364 for (i = 0; i < dev_info.max_mac_addrs; i++) {
365 addr = dev->data->mac_addrs[i];
367 /* skip zero address */
368 if (is_zero_ether_addr(&addr))
371 /* add address to the hardware */
372 if (*dev->dev_ops->mac_addr_add)
373 (*dev->dev_ops->mac_addr_add)(dev, &addr, i, 0);
375 PMD_DEBUG_TRACE("port %d: MAC address array not supported\n",
377 /* exit the loop but not return an error */
382 /* replay promiscuous configuration */
383 if (rte_eth_promiscuous_get(port_id) == 1)
384 rte_eth_promiscuous_enable(port_id);
385 else if (rte_eth_promiscuous_get(port_id) == 0)
386 rte_eth_promiscuous_disable(port_id);
388 /* replay allmulticast configuration */
389 if (rte_eth_allmulticast_get(port_id) == 1)
390 rte_eth_allmulticast_enable(port_id);
391 else if (rte_eth_allmulticast_get(port_id) == 0)
392 rte_eth_allmulticast_disable(port_id);
396 rte_eth_dev_start(uint8_t port_id)
398 struct rte_eth_dev *dev;
401 /* This function is only safe when called from the primary process
402 * in a multi-process setup*/
403 PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
405 if (port_id >= nb_ports) {
406 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
409 dev = &rte_eth_devices[port_id];
411 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
412 diag = (*dev->dev_ops->dev_start)(dev);
414 dev->data->dev_started = 1;
418 rte_eth_dev_config_restore(port_id);
424 rte_eth_dev_stop(uint8_t port_id)
426 struct rte_eth_dev *dev;
428 /* This function is only safe when called from the primary process
429 * in a multi-process setup*/
430 PROC_PRIMARY_OR_RET();
432 if (port_id >= nb_ports) {
433 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
436 dev = &rte_eth_devices[port_id];
438 FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
439 dev->data->dev_started = 0;
440 (*dev->dev_ops->dev_stop)(dev);
444 rte_eth_dev_close(uint8_t port_id)
446 struct rte_eth_dev *dev;
448 /* This function is only safe when called from the primary process
449 * in a multi-process setup*/
450 PROC_PRIMARY_OR_RET();
452 if (port_id >= nb_ports) {
453 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
457 dev = &rte_eth_devices[port_id];
459 FUNC_PTR_OR_RET(*dev->dev_ops->dev_close);
460 dev->data->dev_started = 0;
461 (*dev->dev_ops->dev_close)(dev);
465 rte_eth_rx_queue_setup(uint8_t port_id, uint16_t rx_queue_id,
466 uint16_t nb_rx_desc, unsigned int socket_id,
467 const struct rte_eth_rxconf *rx_conf,
468 struct rte_mempool *mp)
470 struct rte_eth_dev *dev;
471 struct rte_pktmbuf_pool_private *mbp_priv;
472 struct rte_eth_dev_info dev_info;
474 /* This function is only safe when called from the primary process
475 * in a multi-process setup*/
476 PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
478 if (port_id >= nb_ports) {
479 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
482 dev = &rte_eth_devices[port_id];
483 if (rx_queue_id >= dev->data->nb_rx_queues) {
484 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
488 if (dev->data->dev_started) {
490 "port %d must be stopped to allow configuration\n", port_id);
494 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
495 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
498 * Check the size of the mbuf data buffer.
499 * This value must be provided in the private data of the memory pool.
500 * First check that the memory pool has a valid private data.
502 (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
503 if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
504 PMD_DEBUG_TRACE("%s private_data_size %d < %d\n",
505 mp->name, (int) mp->private_data_size,
506 (int) sizeof(struct rte_pktmbuf_pool_private));
509 mbp_priv = (struct rte_pktmbuf_pool_private *)
510 ((char *)mp + sizeof(struct rte_mempool));
511 if ((uint32_t) (mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM) <
512 dev_info.min_rx_bufsize) {
513 PMD_DEBUG_TRACE("%s mbuf_data_room_size %d < %d "
514 "(RTE_PKTMBUF_HEADROOM=%d + min_rx_bufsize(dev)"
517 (int)mbp_priv->mbuf_data_room_size,
518 (int)(RTE_PKTMBUF_HEADROOM +
519 dev_info.min_rx_bufsize),
520 (int)RTE_PKTMBUF_HEADROOM,
521 (int)dev_info.min_rx_bufsize);
525 return (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
526 socket_id, rx_conf, mp);
530 rte_eth_tx_queue_setup(uint8_t port_id, uint16_t tx_queue_id,
531 uint16_t nb_tx_desc, unsigned int socket_id,
532 const struct rte_eth_txconf *tx_conf)
534 struct rte_eth_dev *dev;
536 /* This function is only safe when called from the primary process
537 * in a multi-process setup*/
538 PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
540 if (port_id >= nb_ports) {
541 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
544 dev = &rte_eth_devices[port_id];
545 if (tx_queue_id >= dev->data->nb_tx_queues) {
546 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
550 if (dev->data->dev_started) {
552 "port %d must be stopped to allow configuration\n", port_id);
556 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
557 return (*dev->dev_ops->tx_queue_setup)(dev, tx_queue_id, nb_tx_desc,
562 rte_eth_promiscuous_enable(uint8_t port_id)
564 struct rte_eth_dev *dev;
566 if (port_id >= nb_ports) {
567 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
570 dev = &rte_eth_devices[port_id];
572 FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_enable);
573 (*dev->dev_ops->promiscuous_enable)(dev);
574 dev->data->promiscuous = 1;
578 rte_eth_promiscuous_disable(uint8_t port_id)
580 struct rte_eth_dev *dev;
582 if (port_id >= nb_ports) {
583 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
586 dev = &rte_eth_devices[port_id];
588 FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_disable);
589 dev->data->promiscuous = 0;
590 (*dev->dev_ops->promiscuous_disable)(dev);
594 rte_eth_promiscuous_get(uint8_t port_id)
596 struct rte_eth_dev *dev;
598 if (port_id >= nb_ports) {
599 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
603 dev = &rte_eth_devices[port_id];
604 return dev->data->promiscuous;
608 rte_eth_allmulticast_enable(uint8_t port_id)
610 struct rte_eth_dev *dev;
612 if (port_id >= nb_ports) {
613 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
616 dev = &rte_eth_devices[port_id];
618 FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_enable);
619 (*dev->dev_ops->allmulticast_enable)(dev);
620 dev->data->all_multicast = 1;
624 rte_eth_allmulticast_disable(uint8_t port_id)
626 struct rte_eth_dev *dev;
628 if (port_id >= nb_ports) {
629 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
632 dev = &rte_eth_devices[port_id];
634 FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_disable);
635 dev->data->all_multicast = 0;
636 (*dev->dev_ops->allmulticast_disable)(dev);
640 rte_eth_allmulticast_get(uint8_t port_id)
642 struct rte_eth_dev *dev;
644 if (port_id >= nb_ports) {
645 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
649 dev = &rte_eth_devices[port_id];
650 return dev->data->all_multicast;
654 rte_eth_dev_atomic_read_link_status(struct rte_eth_dev *dev,
655 struct rte_eth_link *link)
657 struct rte_eth_link *dst = link;
658 struct rte_eth_link *src = &(dev->data->dev_link);
660 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
661 *(uint64_t *)src) == 0)
668 rte_eth_link_get(uint8_t port_id, struct rte_eth_link *eth_link)
670 struct rte_eth_dev *dev;
672 if (port_id >= nb_ports) {
673 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
676 dev = &rte_eth_devices[port_id];
677 FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
679 if (dev->data->dev_conf.intr_conf.lsc != 0)
680 rte_eth_dev_atomic_read_link_status(dev, eth_link);
682 (*dev->dev_ops->link_update)(dev, 1);
683 *eth_link = dev->data->dev_link;
688 rte_eth_link_get_nowait(uint8_t port_id, struct rte_eth_link *eth_link)
690 struct rte_eth_dev *dev;
692 if (port_id >= nb_ports) {
693 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
696 dev = &rte_eth_devices[port_id];
697 FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
699 if (dev->data->dev_conf.intr_conf.lsc != 0)
700 rte_eth_dev_atomic_read_link_status(dev, eth_link);
702 (*dev->dev_ops->link_update)(dev, 0);
703 *eth_link = dev->data->dev_link;
708 rte_eth_stats_get(uint8_t port_id, struct rte_eth_stats *stats)
710 struct rte_eth_dev *dev;
712 if (port_id >= nb_ports) {
713 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
716 dev = &rte_eth_devices[port_id];
718 FUNC_PTR_OR_RET(*dev->dev_ops->stats_get);
719 (*dev->dev_ops->stats_get)(dev, stats);
720 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
724 rte_eth_stats_reset(uint8_t port_id)
726 struct rte_eth_dev *dev;
728 if (port_id >= nb_ports) {
729 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
732 dev = &rte_eth_devices[port_id];
734 FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
735 (*dev->dev_ops->stats_reset)(dev);
740 set_queue_stats_mapping(uint8_t port_id, uint16_t queue_id, uint8_t stat_idx,
743 struct rte_eth_dev *dev;
745 if (port_id >= nb_ports) {
746 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
749 dev = &rte_eth_devices[port_id];
751 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_stats_mapping_set, -ENOTSUP);
752 return (*dev->dev_ops->queue_stats_mapping_set)
753 (dev, queue_id, stat_idx, is_rx);
758 rte_eth_dev_set_tx_queue_stats_mapping(uint8_t port_id, uint16_t tx_queue_id,
761 return set_queue_stats_mapping(port_id, tx_queue_id, stat_idx,
767 rte_eth_dev_set_rx_queue_stats_mapping(uint8_t port_id, uint16_t rx_queue_id,
770 return set_queue_stats_mapping(port_id, rx_queue_id, stat_idx,
776 rte_eth_dev_info_get(uint8_t port_id, struct rte_eth_dev_info *dev_info)
778 struct rte_eth_dev *dev;
780 if (port_id >= nb_ports) {
781 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
784 dev = &rte_eth_devices[port_id];
786 FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
787 (*dev->dev_ops->dev_infos_get)(dev, dev_info);
788 dev_info->pci_dev = dev->pci_dev;
789 dev_info->driver_name = dev->driver->pci_drv.name;
793 rte_eth_macaddr_get(uint8_t port_id, struct ether_addr *mac_addr)
795 struct rte_eth_dev *dev;
797 if (port_id >= nb_ports) {
798 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
801 dev = &rte_eth_devices[port_id];
802 ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
806 rte_eth_dev_vlan_filter(uint8_t port_id, uint16_t vlan_id, int on)
808 struct rte_eth_dev *dev;
810 if (port_id >= nb_ports) {
811 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
814 dev = &rte_eth_devices[port_id];
815 if (! (dev->data->dev_conf.rxmode.hw_vlan_filter)) {
816 PMD_DEBUG_TRACE("port %d: vlan-filtering disabled\n", port_id);
820 if (vlan_id > 4095) {
821 PMD_DEBUG_TRACE("(port_id=%d) invalid vlan_id=%u > 4095\n",
822 port_id, (unsigned) vlan_id);
825 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
826 (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
831 rte_eth_dev_fdir_add_signature_filter(uint8_t port_id,
832 struct rte_fdir_filter *fdir_filter,
835 struct rte_eth_dev *dev;
837 if (port_id >= nb_ports) {
838 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
842 dev = &rte_eth_devices[port_id];
844 if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
845 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
846 port_id, dev->data->dev_conf.fdir_conf.mode);
850 if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
851 || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
852 && (fdir_filter->port_src || fdir_filter->port_dst)) {
853 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
854 "None l4type, source & destinations ports " \
855 "should be null!\n");
859 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_signature_filter, -ENOTSUP);
860 return (*dev->dev_ops->fdir_add_signature_filter)(dev, fdir_filter,
865 rte_eth_dev_fdir_update_signature_filter(uint8_t port_id,
866 struct rte_fdir_filter *fdir_filter,
869 struct rte_eth_dev *dev;
871 if (port_id >= nb_ports) {
872 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
876 dev = &rte_eth_devices[port_id];
878 if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
879 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
880 port_id, dev->data->dev_conf.fdir_conf.mode);
884 if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
885 || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
886 && (fdir_filter->port_src || fdir_filter->port_dst)) {
887 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
888 "None l4type, source & destinations ports " \
889 "should be null!\n");
893 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_signature_filter, -ENOTSUP);
894 return (*dev->dev_ops->fdir_update_signature_filter)(dev, fdir_filter,
900 rte_eth_dev_fdir_remove_signature_filter(uint8_t port_id,
901 struct rte_fdir_filter *fdir_filter)
903 struct rte_eth_dev *dev;
905 if (port_id >= nb_ports) {
906 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
910 dev = &rte_eth_devices[port_id];
912 if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
913 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
914 port_id, dev->data->dev_conf.fdir_conf.mode);
918 if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
919 || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
920 && (fdir_filter->port_src || fdir_filter->port_dst)) {
921 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
922 "None l4type source & destinations ports " \
923 "should be null!\n");
927 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_signature_filter, -ENOTSUP);
928 return (*dev->dev_ops->fdir_remove_signature_filter)(dev, fdir_filter);
932 rte_eth_dev_fdir_get_infos(uint8_t port_id, struct rte_eth_fdir *fdir)
934 struct rte_eth_dev *dev;
936 if (port_id >= nb_ports) {
937 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
941 dev = &rte_eth_devices[port_id];
942 if (! (dev->data->dev_conf.fdir_conf.mode)) {
943 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
947 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_infos_get, -ENOTSUP);
949 (*dev->dev_ops->fdir_infos_get)(dev, fdir);
954 rte_eth_dev_fdir_add_perfect_filter(uint8_t port_id,
955 struct rte_fdir_filter *fdir_filter,
956 uint16_t soft_id, uint8_t queue,
959 struct rte_eth_dev *dev;
961 if (port_id >= nb_ports) {
962 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
966 dev = &rte_eth_devices[port_id];
968 if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
969 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
970 port_id, dev->data->dev_conf.fdir_conf.mode);
974 if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
975 || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
976 && (fdir_filter->port_src || fdir_filter->port_dst)) {
977 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
978 "None l4type, source & destinations ports " \
979 "should be null!\n");
983 /* For now IPv6 is not supported with perfect filter */
984 if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
987 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_perfect_filter, -ENOTSUP);
988 return (*dev->dev_ops->fdir_add_perfect_filter)(dev, fdir_filter,
994 rte_eth_dev_fdir_update_perfect_filter(uint8_t port_id,
995 struct rte_fdir_filter *fdir_filter,
996 uint16_t soft_id, uint8_t queue,
999 struct rte_eth_dev *dev;
1001 if (port_id >= nb_ports) {
1002 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1006 dev = &rte_eth_devices[port_id];
1008 if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1009 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1010 port_id, dev->data->dev_conf.fdir_conf.mode);
1014 if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1015 || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1016 && (fdir_filter->port_src || fdir_filter->port_dst)) {
1017 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1018 "None l4type, source & destinations ports " \
1019 "should be null!\n");
1023 /* For now IPv6 is not supported with perfect filter */
1024 if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1027 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_perfect_filter, -ENOTSUP);
1028 return (*dev->dev_ops->fdir_update_perfect_filter)(dev, fdir_filter,
1029 soft_id, queue, drop);
1033 rte_eth_dev_fdir_remove_perfect_filter(uint8_t port_id,
1034 struct rte_fdir_filter *fdir_filter,
1037 struct rte_eth_dev *dev;
1039 if (port_id >= nb_ports) {
1040 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1044 dev = &rte_eth_devices[port_id];
1046 if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1047 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1048 port_id, dev->data->dev_conf.fdir_conf.mode);
1052 if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1053 || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1054 && (fdir_filter->port_src || fdir_filter->port_dst)) {
1055 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1056 "None l4type, source & destinations ports " \
1057 "should be null!\n");
1061 /* For now IPv6 is not supported with perfect filter */
1062 if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1065 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_perfect_filter, -ENOTSUP);
1066 return (*dev->dev_ops->fdir_remove_perfect_filter)(dev, fdir_filter,
1071 rte_eth_dev_fdir_set_masks(uint8_t port_id, struct rte_fdir_masks *fdir_mask)
1073 struct rte_eth_dev *dev;
1075 if (port_id >= nb_ports) {
1076 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1080 dev = &rte_eth_devices[port_id];
1081 if (! (dev->data->dev_conf.fdir_conf.mode)) {
1082 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
1086 /* IPv6 mask are not supported */
1087 if (fdir_mask->src_ipv6_mask)
1090 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_set_masks, -ENOTSUP);
1091 return (*dev->dev_ops->fdir_set_masks)(dev, fdir_mask);
1095 rte_eth_dev_flow_ctrl_set(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
1097 struct rte_eth_dev *dev;
1099 if (port_id >= nb_ports) {
1100 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1104 if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
1105 PMD_DEBUG_TRACE("Invalid send_xon, only 0/1 allowed\n");
1109 dev = &rte_eth_devices[port_id];
1111 /* High water, low water validation are device specific */
1112 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
1113 if (*dev->dev_ops->flow_ctrl_set)
1114 return (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf);
1120 rte_eth_led_on(uint8_t port_id)
1122 struct rte_eth_dev *dev;
1124 if (port_id >= nb_ports) {
1125 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1129 dev = &rte_eth_devices[port_id];
1130 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
1131 return ((*dev->dev_ops->dev_led_on)(dev));
1135 rte_eth_led_off(uint8_t port_id)
1137 struct rte_eth_dev *dev;
1139 if (port_id >= nb_ports) {
1140 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1144 dev = &rte_eth_devices[port_id];
1145 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
1146 return ((*dev->dev_ops->dev_led_off)(dev));
1150 * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
1154 get_mac_addr_index(uint8_t port_id, struct ether_addr *addr)
1156 struct rte_eth_dev_info dev_info;
1157 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1160 rte_eth_dev_info_get(port_id, &dev_info);
1162 for (i = 0; i < dev_info.max_mac_addrs; i++)
1163 if (memcmp(addr, &dev->data->mac_addrs[i], ETHER_ADDR_LEN) == 0)
1169 static struct ether_addr null_mac_addr = {{0, 0, 0, 0, 0, 0}};
1172 rte_eth_dev_mac_addr_add(uint8_t port_id, struct ether_addr *addr,
1175 struct rte_eth_dev *dev;
1178 if (port_id >= nb_ports) {
1179 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1182 dev = &rte_eth_devices[port_id];
1183 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
1185 if (is_zero_ether_addr(addr)) {
1186 PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n", port_id);
1190 /* Check if it's already there, and do nothing */
1191 index = get_mac_addr_index(port_id, addr);
1195 index = get_mac_addr_index(port_id, &null_mac_addr);
1197 PMD_DEBUG_TRACE("port %d: MAC address array full\n", port_id);
1202 (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
1204 /* Update address in NIC data structure */
1205 ether_addr_copy(addr, &dev->data->mac_addrs[index]);
1211 rte_eth_dev_mac_addr_remove(uint8_t port_id, struct ether_addr *addr)
1213 struct rte_eth_dev *dev;
1216 if (port_id >= nb_ports) {
1217 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1220 dev = &rte_eth_devices[port_id];
1221 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
1223 index = get_mac_addr_index(port_id, addr);
1225 PMD_DEBUG_TRACE("port %d: Cannot remove default MAC address\n", port_id);
1226 return (-EADDRINUSE);
1227 } else if (index < 0)
1228 return 0; /* Do nothing if address wasn't found */
1231 (*dev->dev_ops->mac_addr_remove)(dev, index);
1233 /* Update address in NIC data structure */
1234 ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
1239 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1241 rte_eth_rx_burst(uint8_t port_id, uint16_t queue_id,
1242 struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1244 struct rte_eth_dev *dev;
1246 if (port_id >= nb_ports) {
1247 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1250 dev = &rte_eth_devices[port_id];
1251 FUNC_PTR_OR_ERR_RET(*dev->rx_pkt_burst, -ENOTSUP);
1252 if (queue_id >= dev->data->nb_rx_queues) {
1253 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", queue_id);
1256 return (*dev->rx_pkt_burst)(dev->data->rx_queues[queue_id],
1261 rte_eth_tx_burst(uint8_t port_id, uint16_t queue_id,
1262 struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1264 struct rte_eth_dev *dev;
1266 if (port_id >= nb_ports) {
1267 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1270 dev = &rte_eth_devices[port_id];
1272 FUNC_PTR_OR_ERR_RET(*dev->tx_pkt_burst, -ENOTSUP);
1273 if (queue_id >= dev->data->nb_tx_queues) {
1274 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", queue_id);
1277 return (*dev->tx_pkt_burst)(dev->data->tx_queues[queue_id],
1283 rte_eth_dev_callback_register(uint8_t port_id,
1284 enum rte_eth_event_type event,
1285 rte_eth_dev_cb_fn cb_fn, void *cb_arg)
1288 struct rte_eth_dev *dev;
1289 struct rte_eth_dev_callback *user_cb = NULL;
1293 if (port_id >= nb_ports) {
1294 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1297 dev = &rte_eth_devices[port_id];
1298 rte_spinlock_lock(&rte_eth_dev_cb_lock);
1299 TAILQ_FOREACH(user_cb, &(dev->callbacks), next) {
1300 if (user_cb->cb_fn == cb_fn &&
1301 user_cb->cb_arg == cb_arg &&
1302 user_cb->event == event) {
1307 user_cb = rte_malloc("INTR_USER_CALLBACK",
1308 sizeof(struct rte_eth_dev_callback), 0);
1311 user_cb->cb_fn = cb_fn;
1312 user_cb->cb_arg = cb_arg;
1313 user_cb->event = event;
1314 TAILQ_INSERT_TAIL(&(dev->callbacks), user_cb, next);
1318 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1324 rte_eth_dev_callback_unregister(uint8_t port_id,
1325 enum rte_eth_event_type event,
1326 rte_eth_dev_cb_fn cb_fn, void *cb_arg)
1329 struct rte_eth_dev *dev;
1330 struct rte_eth_dev_callback *cb_lst = NULL;
1334 if (port_id >= nb_ports) {
1335 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1338 dev = &rte_eth_devices[port_id];
1339 rte_spinlock_lock(&rte_eth_dev_cb_lock);
1340 TAILQ_FOREACH(cb_lst, &(dev->callbacks), next) {
1341 if (cb_lst->cb_fn != cb_fn || cb_lst->event != event)
1343 if (cb_lst->cb_arg == (void *)-1 ||
1344 cb_lst->cb_arg == cb_arg) {
1345 TAILQ_REMOVE(&(dev->callbacks), cb_lst, next);
1351 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1357 _rte_eth_dev_callback_process(struct rte_eth_dev *dev, enum rte_eth_event_type event)
1359 struct rte_eth_dev_callback *cb_lst = NULL;
1360 struct rte_eth_dev_callback dev_cb;
1362 rte_spinlock_lock(&rte_eth_dev_cb_lock);
1363 TAILQ_FOREACH(cb_lst, &(dev->callbacks), next) {
1364 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1367 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1368 dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
1370 rte_spinlock_lock(&rte_eth_dev_cb_lock);
1372 rte_spinlock_unlock(&rte_eth_dev_cb_lock);