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 */
130 rte_eth_dev_data_alloc(void)
132 const unsigned flags = 0;
133 const struct rte_memzone *mz;
135 if (rte_eal_process_type() == RTE_PROC_PRIMARY){
136 mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA,
137 RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data),
138 rte_socket_id(), flags);
140 mz = rte_memzone_lookup(MZ_RTE_ETH_DEV_DATA);
142 rte_panic("Cannot allocate memzone for ethernet port data\n");
144 rte_eth_dev_data = mz->addr;
145 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
146 memset(rte_eth_dev_data, 0,
147 RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data));
150 static inline struct rte_eth_dev *
151 rte_eth_dev_allocate(void)
153 struct rte_eth_dev *eth_dev;
155 if (nb_ports == RTE_MAX_ETHPORTS) {
156 PMD_DEBUG_TRACE("Reached maximum number of ethernet ports\n");
160 if (rte_eth_dev_data == NULL)
161 rte_eth_dev_data_alloc();
163 eth_dev = &rte_eth_devices[nb_ports];
164 eth_dev->data = &rte_eth_dev_data[nb_ports];
165 eth_dev->data->port_id = nb_ports++;
170 rte_eth_dev_init(struct rte_pci_driver *pci_drv,
171 struct rte_pci_device *pci_dev)
173 struct eth_driver *eth_drv;
174 struct rte_eth_dev *eth_dev;
177 eth_drv = (struct eth_driver *)pci_drv;
179 eth_dev = rte_eth_dev_allocate();
183 if (rte_eal_process_type() == RTE_PROC_PRIMARY){
184 eth_dev->data->dev_private = rte_zmalloc("ethdev private structure",
185 eth_drv->dev_private_size,
187 if (eth_dev->data->dev_private == NULL)
188 rte_panic("Cannot allocate memzone for private port data\n");
190 eth_dev->pci_dev = pci_dev;
191 eth_dev->driver = eth_drv;
192 eth_dev->data->rx_mbuf_alloc_failed = 0;
194 /* init user callbacks */
195 TAILQ_INIT(&(eth_dev->callbacks));
198 * Set the default maximum frame size.
200 eth_dev->data->max_frame_size = ETHER_MAX_LEN;
202 /* Invoke PMD device initialization function */
203 diag = (*eth_drv->eth_dev_init)(eth_drv, eth_dev);
207 PMD_DEBUG_TRACE("driver %s: eth_dev_init(vendor_id=0x%u device_id=0x%x)"
208 " failed\n", pci_drv->name,
209 (unsigned) pci_dev->id.vendor_id,
210 (unsigned) pci_dev->id.device_id);
211 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
212 rte_free(eth_dev->data->dev_private);
218 * Register an Ethernet [Poll Mode] driver.
220 * Function invoked by the initialization function of an Ethernet driver
221 * to simultaneously register itself as a PCI driver and as an Ethernet
223 * Invokes the rte_eal_pci_register() function to register the *pci_drv*
224 * structure embedded in the *eth_drv* structure, after having stored the
225 * address of the rte_eth_dev_init() function in the *devinit* field of
226 * the *pci_drv* structure.
227 * During the PCI probing phase, the rte_eth_dev_init() function is
228 * invoked for each PCI [Ethernet device] matching the embedded PCI
229 * identifiers provided by the driver.
232 rte_eth_driver_register(struct eth_driver *eth_drv)
234 eth_drv->pci_drv.devinit = rte_eth_dev_init;
235 rte_eal_pci_register(ð_drv->pci_drv);
239 rte_eth_dev_count(void)
245 rte_eth_dev_configure(uint8_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
246 const struct rte_eth_conf *dev_conf)
248 struct rte_eth_dev *dev;
249 struct rte_eth_dev_info dev_info;
252 /* This function is only safe when called from the primary process
253 * in a multi-process setup*/
254 PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
256 if (port_id >= nb_ports || port_id >= RTE_MAX_ETHPORTS) {
257 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
260 dev = &rte_eth_devices[port_id];
262 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
263 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
265 if (dev->data->dev_started) {
267 "port %d must be stopped to allow configuration\n", port_id);
272 * Check that the numbers of RX and TX queues are not greater
273 * than the maximum number of RX and TX queues supported by the
276 (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
277 if (nb_rx_q > dev_info.max_rx_queues) {
278 PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_queues=%d > %d\n",
279 port_id, nb_rx_q, dev_info.max_rx_queues);
283 PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_q == 0\n", port_id);
287 if (nb_tx_q > dev_info.max_tx_queues) {
288 PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_queues=%d > %d\n",
289 port_id, nb_tx_q, dev_info.max_tx_queues);
293 PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_q == 0\n", port_id);
297 /* Copy the dev_conf parameter into the dev structure */
298 memcpy(&dev->data->dev_conf, dev_conf, sizeof(dev->data->dev_conf));
301 * If jumbo frames are enabled, check that the maximum RX packet
302 * length is supported by the configured device.
304 if (dev_conf->rxmode.jumbo_frame == 1) {
305 if (dev_conf->rxmode.max_rx_pkt_len >
306 dev_info.max_rx_pktlen) {
307 PMD_DEBUG_TRACE("ethdev port_id=%d max_rx_pkt_len %u"
308 " > max valid value %u\n",
310 (unsigned)dev_conf->rxmode.max_rx_pkt_len,
311 (unsigned)dev_info.max_rx_pktlen);
315 /* Use default value */
316 dev->data->dev_conf.rxmode.max_rx_pkt_len = ETHER_MAX_LEN;
318 /* For vmdb+dcb mode check our configuration before we go further */
319 if (dev_conf->rxmode.mq_mode == ETH_VMDQ_DCB) {
320 const struct rte_eth_vmdq_dcb_conf *conf;
322 if (nb_rx_q != ETH_VMDQ_DCB_NUM_QUEUES) {
323 PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB, nb_rx_q "
325 port_id, ETH_VMDQ_DCB_NUM_QUEUES);
328 conf = &(dev_conf->rx_adv_conf.vmdq_dcb_conf);
329 if (! (conf->nb_queue_pools == ETH_16_POOLS ||
330 conf->nb_queue_pools == ETH_32_POOLS)) {
331 PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB selected, "
332 "nb_queue_pools must be %d or %d\n",
333 port_id, ETH_16_POOLS, ETH_32_POOLS);
338 diag = (*dev->dev_ops->dev_configure)(dev, nb_rx_q, nb_tx_q);
340 rte_free(dev->data->rx_queues);
341 rte_free(dev->data->tx_queues);
347 rte_eth_dev_config_restore(uint8_t port_id)
349 struct rte_eth_dev *dev;
350 struct rte_eth_dev_info dev_info;
351 struct ether_addr addr;
354 dev = &rte_eth_devices[port_id];
356 rte_eth_dev_info_get(port_id, &dev_info);
358 /* replay MAC address configuration */
359 for (i = 0; i < dev_info.max_mac_addrs; i++) {
360 addr = dev->data->mac_addrs[i];
362 /* skip zero address */
363 if (is_zero_ether_addr(&addr))
366 /* add address to the hardware */
367 if (*dev->dev_ops->mac_addr_add)
368 (*dev->dev_ops->mac_addr_add)(dev, &addr, i, 0);
370 PMD_DEBUG_TRACE("port %d: MAC address array not supported\n",
372 /* exit the loop but not return an error */
377 /* replay promiscuous configuration */
378 if (rte_eth_promiscuous_get(port_id) == 1)
379 rte_eth_promiscuous_enable(port_id);
380 else if (rte_eth_promiscuous_get(port_id) == 0)
381 rte_eth_promiscuous_disable(port_id);
383 /* replay allmulticast configuration */
384 if (rte_eth_allmulticast_get(port_id) == 1)
385 rte_eth_allmulticast_enable(port_id);
386 else if (rte_eth_allmulticast_get(port_id) == 0)
387 rte_eth_allmulticast_disable(port_id);
391 rte_eth_dev_start(uint8_t port_id)
393 struct rte_eth_dev *dev;
396 /* This function is only safe when called from the primary process
397 * in a multi-process setup*/
398 PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
400 if (port_id >= nb_ports) {
401 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
404 dev = &rte_eth_devices[port_id];
406 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
407 diag = (*dev->dev_ops->dev_start)(dev);
409 dev->data->dev_started = 1;
413 rte_eth_dev_config_restore(port_id);
419 rte_eth_dev_stop(uint8_t port_id)
421 struct rte_eth_dev *dev;
423 /* This function is only safe when called from the primary process
424 * in a multi-process setup*/
425 PROC_PRIMARY_OR_RET();
427 if (port_id >= nb_ports) {
428 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
431 dev = &rte_eth_devices[port_id];
433 FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
434 dev->data->dev_started = 0;
435 (*dev->dev_ops->dev_stop)(dev);
439 rte_eth_dev_close(uint8_t port_id)
441 struct rte_eth_dev *dev;
443 /* This function is only safe when called from the primary process
444 * in a multi-process setup*/
445 PROC_PRIMARY_OR_RET();
447 if (port_id >= nb_ports) {
448 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
452 dev = &rte_eth_devices[port_id];
454 FUNC_PTR_OR_RET(*dev->dev_ops->dev_close);
455 dev->data->dev_started = 0;
456 (*dev->dev_ops->dev_close)(dev);
460 rte_eth_rx_queue_setup(uint8_t port_id, uint16_t rx_queue_id,
461 uint16_t nb_rx_desc, unsigned int socket_id,
462 const struct rte_eth_rxconf *rx_conf,
463 struct rte_mempool *mp)
465 struct rte_eth_dev *dev;
466 struct rte_pktmbuf_pool_private *mbp_priv;
467 struct rte_eth_dev_info dev_info;
469 /* This function is only safe when called from the primary process
470 * in a multi-process setup*/
471 PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
473 if (port_id >= nb_ports) {
474 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
477 dev = &rte_eth_devices[port_id];
478 if (rx_queue_id >= dev->data->nb_rx_queues) {
479 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
483 if (dev->data->dev_started) {
485 "port %d must be stopped to allow configuration\n", port_id);
489 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
490 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
493 * Check the size of the mbuf data buffer.
494 * This value must be provided in the private data of the memory pool.
495 * First check that the memory pool has a valid private data.
497 (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
498 if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
499 PMD_DEBUG_TRACE("%s private_data_size %d < %d\n",
500 mp->name, (int) mp->private_data_size,
501 (int) sizeof(struct rte_pktmbuf_pool_private));
504 mbp_priv = (struct rte_pktmbuf_pool_private *)
505 ((char *)mp + sizeof(struct rte_mempool));
506 if ((uint32_t) (mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM) <
507 dev_info.min_rx_bufsize) {
508 PMD_DEBUG_TRACE("%s mbuf_data_room_size %d < %d "
509 "(RTE_PKTMBUF_HEADROOM=%d + min_rx_bufsize(dev)"
512 (int)mbp_priv->mbuf_data_room_size,
513 (int)(RTE_PKTMBUF_HEADROOM +
514 dev_info.min_rx_bufsize),
515 (int)RTE_PKTMBUF_HEADROOM,
516 (int)dev_info.min_rx_bufsize);
520 return (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
521 socket_id, rx_conf, mp);
525 rte_eth_tx_queue_setup(uint8_t port_id, uint16_t tx_queue_id,
526 uint16_t nb_tx_desc, unsigned int socket_id,
527 const struct rte_eth_txconf *tx_conf)
529 struct rte_eth_dev *dev;
531 /* This function is only safe when called from the primary process
532 * in a multi-process setup*/
533 PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
535 if (port_id >= nb_ports) {
536 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
539 dev = &rte_eth_devices[port_id];
540 if (tx_queue_id >= dev->data->nb_tx_queues) {
541 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
545 if (dev->data->dev_started) {
547 "port %d must be stopped to allow configuration\n", port_id);
551 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
552 return (*dev->dev_ops->tx_queue_setup)(dev, tx_queue_id, nb_tx_desc,
557 rte_eth_promiscuous_enable(uint8_t port_id)
559 struct rte_eth_dev *dev;
561 if (port_id >= nb_ports) {
562 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
565 dev = &rte_eth_devices[port_id];
567 FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_enable);
568 (*dev->dev_ops->promiscuous_enable)(dev);
569 dev->data->promiscuous = 1;
573 rte_eth_promiscuous_disable(uint8_t port_id)
575 struct rte_eth_dev *dev;
577 if (port_id >= nb_ports) {
578 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
581 dev = &rte_eth_devices[port_id];
583 FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_disable);
584 dev->data->promiscuous = 0;
585 (*dev->dev_ops->promiscuous_disable)(dev);
589 rte_eth_promiscuous_get(uint8_t port_id)
591 struct rte_eth_dev *dev;
593 if (port_id >= nb_ports) {
594 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
598 dev = &rte_eth_devices[port_id];
599 return dev->data->promiscuous;
603 rte_eth_allmulticast_enable(uint8_t port_id)
605 struct rte_eth_dev *dev;
607 if (port_id >= nb_ports) {
608 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
611 dev = &rte_eth_devices[port_id];
613 FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_enable);
614 (*dev->dev_ops->allmulticast_enable)(dev);
615 dev->data->all_multicast = 1;
619 rte_eth_allmulticast_disable(uint8_t port_id)
621 struct rte_eth_dev *dev;
623 if (port_id >= nb_ports) {
624 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
627 dev = &rte_eth_devices[port_id];
629 FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_disable);
630 dev->data->all_multicast = 0;
631 (*dev->dev_ops->allmulticast_disable)(dev);
635 rte_eth_allmulticast_get(uint8_t port_id)
637 struct rte_eth_dev *dev;
639 if (port_id >= nb_ports) {
640 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
644 dev = &rte_eth_devices[port_id];
645 return dev->data->all_multicast;
649 rte_eth_dev_atomic_read_link_status(struct rte_eth_dev *dev,
650 struct rte_eth_link *link)
652 struct rte_eth_link *dst = link;
653 struct rte_eth_link *src = &(dev->data->dev_link);
655 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
656 *(uint64_t *)src) == 0)
663 rte_eth_link_get(uint8_t port_id, struct rte_eth_link *eth_link)
665 struct rte_eth_dev *dev;
667 if (port_id >= nb_ports) {
668 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
671 dev = &rte_eth_devices[port_id];
672 FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
674 if (dev->data->dev_conf.intr_conf.lsc != 0)
675 rte_eth_dev_atomic_read_link_status(dev, eth_link);
677 (*dev->dev_ops->link_update)(dev, 1);
678 *eth_link = dev->data->dev_link;
683 rte_eth_link_get_nowait(uint8_t port_id, struct rte_eth_link *eth_link)
685 struct rte_eth_dev *dev;
687 if (port_id >= nb_ports) {
688 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
691 dev = &rte_eth_devices[port_id];
692 FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
694 if (dev->data->dev_conf.intr_conf.lsc != 0)
695 rte_eth_dev_atomic_read_link_status(dev, eth_link);
697 (*dev->dev_ops->link_update)(dev, 0);
698 *eth_link = dev->data->dev_link;
703 rte_eth_stats_get(uint8_t port_id, struct rte_eth_stats *stats)
705 struct rte_eth_dev *dev;
707 if (port_id >= nb_ports) {
708 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
711 dev = &rte_eth_devices[port_id];
713 FUNC_PTR_OR_RET(*dev->dev_ops->stats_get);
714 (*dev->dev_ops->stats_get)(dev, stats);
715 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
719 rte_eth_stats_reset(uint8_t port_id)
721 struct rte_eth_dev *dev;
723 if (port_id >= nb_ports) {
724 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
727 dev = &rte_eth_devices[port_id];
729 FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
730 (*dev->dev_ops->stats_reset)(dev);
734 rte_eth_dev_info_get(uint8_t port_id, struct rte_eth_dev_info *dev_info)
736 struct rte_eth_dev *dev;
738 if (port_id >= nb_ports) {
739 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
742 dev = &rte_eth_devices[port_id];
744 FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
745 (*dev->dev_ops->dev_infos_get)(dev, dev_info);
746 dev_info->pci_dev = dev->pci_dev;
747 dev_info->driver_name = dev->driver->pci_drv.name;
751 rte_eth_macaddr_get(uint8_t port_id, struct ether_addr *mac_addr)
753 struct rte_eth_dev *dev;
755 if (port_id >= nb_ports) {
756 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
759 dev = &rte_eth_devices[port_id];
760 ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
764 rte_eth_dev_vlan_filter(uint8_t port_id, uint16_t vlan_id, int on)
766 struct rte_eth_dev *dev;
768 if (port_id >= nb_ports) {
769 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
772 dev = &rte_eth_devices[port_id];
773 if (! (dev->data->dev_conf.rxmode.hw_vlan_filter)) {
774 PMD_DEBUG_TRACE("port %d: vlan-filtering disabled\n", port_id);
778 if (vlan_id > 4095) {
779 PMD_DEBUG_TRACE("(port_id=%d) invalid vlan_id=%u > 4095\n",
780 port_id, (unsigned) vlan_id);
783 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
784 (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
789 rte_eth_dev_fdir_add_signature_filter(uint8_t port_id,
790 struct rte_fdir_filter *fdir_filter,
793 struct rte_eth_dev *dev;
795 if (port_id >= nb_ports) {
796 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
800 dev = &rte_eth_devices[port_id];
802 if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
803 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
804 port_id, dev->data->dev_conf.fdir_conf.mode);
808 if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
809 || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
810 && (fdir_filter->port_src || fdir_filter->port_dst)) {
811 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
812 "None l4type, source & destinations ports " \
813 "should be null!\n");
817 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_signature_filter, -ENOTSUP);
818 return (*dev->dev_ops->fdir_add_signature_filter)(dev, fdir_filter,
823 rte_eth_dev_fdir_update_signature_filter(uint8_t port_id,
824 struct rte_fdir_filter *fdir_filter,
827 struct rte_eth_dev *dev;
829 if (port_id >= nb_ports) {
830 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
834 dev = &rte_eth_devices[port_id];
836 if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
837 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
838 port_id, dev->data->dev_conf.fdir_conf.mode);
842 if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
843 || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
844 && (fdir_filter->port_src || fdir_filter->port_dst)) {
845 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
846 "None l4type, source & destinations ports " \
847 "should be null!\n");
851 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_signature_filter, -ENOTSUP);
852 return (*dev->dev_ops->fdir_update_signature_filter)(dev, fdir_filter,
858 rte_eth_dev_fdir_remove_signature_filter(uint8_t port_id,
859 struct rte_fdir_filter *fdir_filter)
861 struct rte_eth_dev *dev;
863 if (port_id >= nb_ports) {
864 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
868 dev = &rte_eth_devices[port_id];
870 if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
871 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
872 port_id, dev->data->dev_conf.fdir_conf.mode);
876 if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
877 || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
878 && (fdir_filter->port_src || fdir_filter->port_dst)) {
879 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
880 "None l4type source & destinations ports " \
881 "should be null!\n");
885 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_signature_filter, -ENOTSUP);
886 return (*dev->dev_ops->fdir_remove_signature_filter)(dev, fdir_filter);
890 rte_eth_dev_fdir_get_infos(uint8_t port_id, struct rte_eth_fdir *fdir)
892 struct rte_eth_dev *dev;
894 if (port_id >= nb_ports) {
895 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
899 dev = &rte_eth_devices[port_id];
900 if (! (dev->data->dev_conf.fdir_conf.mode)) {
901 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
905 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_infos_get, -ENOTSUP);
907 (*dev->dev_ops->fdir_infos_get)(dev, fdir);
912 rte_eth_dev_fdir_add_perfect_filter(uint8_t port_id,
913 struct rte_fdir_filter *fdir_filter,
914 uint16_t soft_id, uint8_t queue,
917 struct rte_eth_dev *dev;
919 if (port_id >= nb_ports) {
920 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
924 dev = &rte_eth_devices[port_id];
926 if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
927 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
928 port_id, dev->data->dev_conf.fdir_conf.mode);
932 if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
933 || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
934 && (fdir_filter->port_src || fdir_filter->port_dst)) {
935 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
936 "None l4type, source & destinations ports " \
937 "should be null!\n");
941 /* For now IPv6 is not supported with perfect filter */
942 if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
945 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_perfect_filter, -ENOTSUP);
946 return (*dev->dev_ops->fdir_add_perfect_filter)(dev, fdir_filter,
952 rte_eth_dev_fdir_update_perfect_filter(uint8_t port_id,
953 struct rte_fdir_filter *fdir_filter,
954 uint16_t soft_id, uint8_t queue,
957 struct rte_eth_dev *dev;
959 if (port_id >= nb_ports) {
960 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
964 dev = &rte_eth_devices[port_id];
966 if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
967 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
968 port_id, dev->data->dev_conf.fdir_conf.mode);
972 if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
973 || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
974 && (fdir_filter->port_src || fdir_filter->port_dst)) {
975 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
976 "None l4type, source & destinations ports " \
977 "should be null!\n");
981 /* For now IPv6 is not supported with perfect filter */
982 if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
985 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_perfect_filter, -ENOTSUP);
986 return (*dev->dev_ops->fdir_update_perfect_filter)(dev, fdir_filter,
987 soft_id, queue, drop);
991 rte_eth_dev_fdir_remove_perfect_filter(uint8_t port_id,
992 struct rte_fdir_filter *fdir_filter,
995 struct rte_eth_dev *dev;
997 if (port_id >= nb_ports) {
998 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1002 dev = &rte_eth_devices[port_id];
1004 if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1005 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1006 port_id, dev->data->dev_conf.fdir_conf.mode);
1010 if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1011 || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1012 && (fdir_filter->port_src || fdir_filter->port_dst)) {
1013 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1014 "None l4type, source & destinations ports " \
1015 "should be null!\n");
1019 /* For now IPv6 is not supported with perfect filter */
1020 if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1023 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_perfect_filter, -ENOTSUP);
1024 return (*dev->dev_ops->fdir_remove_perfect_filter)(dev, fdir_filter,
1029 rte_eth_dev_fdir_set_masks(uint8_t port_id, struct rte_fdir_masks *fdir_mask)
1031 struct rte_eth_dev *dev;
1033 if (port_id >= nb_ports) {
1034 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1038 dev = &rte_eth_devices[port_id];
1039 if (! (dev->data->dev_conf.fdir_conf.mode)) {
1040 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
1044 /* IPv6 mask are not supported */
1045 if (fdir_mask->src_ipv6_mask)
1048 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_set_masks, -ENOTSUP);
1049 return (*dev->dev_ops->fdir_set_masks)(dev, fdir_mask);
1053 rte_eth_dev_flow_ctrl_set(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
1055 struct rte_eth_dev *dev;
1057 if (port_id >= nb_ports) {
1058 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1062 if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
1063 PMD_DEBUG_TRACE("Invalid send_xon, only 0/1 allowed\n");
1067 dev = &rte_eth_devices[port_id];
1069 /* High water, low water validation are device specific */
1070 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
1071 if (*dev->dev_ops->flow_ctrl_set)
1072 return (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf);
1078 rte_eth_led_on(uint8_t port_id)
1080 struct rte_eth_dev *dev;
1082 if (port_id >= nb_ports) {
1083 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1087 dev = &rte_eth_devices[port_id];
1088 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
1089 return ((*dev->dev_ops->dev_led_on)(dev));
1093 rte_eth_led_off(uint8_t port_id)
1095 struct rte_eth_dev *dev;
1097 if (port_id >= nb_ports) {
1098 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1102 dev = &rte_eth_devices[port_id];
1103 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
1104 return ((*dev->dev_ops->dev_led_off)(dev));
1108 * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
1112 get_mac_addr_index(uint8_t port_id, struct ether_addr *addr)
1114 struct rte_eth_dev_info dev_info;
1115 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1118 rte_eth_dev_info_get(port_id, &dev_info);
1120 for (i = 0; i < dev_info.max_mac_addrs; i++)
1121 if (memcmp(addr, &dev->data->mac_addrs[i], ETHER_ADDR_LEN) == 0)
1127 static struct ether_addr null_mac_addr = {{0, 0, 0, 0, 0, 0}};
1130 rte_eth_dev_mac_addr_add(uint8_t port_id, struct ether_addr *addr,
1133 struct rte_eth_dev *dev;
1136 if (port_id >= nb_ports) {
1137 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1140 dev = &rte_eth_devices[port_id];
1141 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
1143 if (is_zero_ether_addr(addr)) {
1144 PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n", port_id);
1148 /* Check if it's already there, and do nothing */
1149 index = get_mac_addr_index(port_id, addr);
1153 index = get_mac_addr_index(port_id, &null_mac_addr);
1155 PMD_DEBUG_TRACE("port %d: MAC address array full\n", port_id);
1160 (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
1162 /* Update address in NIC data structure */
1163 ether_addr_copy(addr, &dev->data->mac_addrs[index]);
1169 rte_eth_dev_mac_addr_remove(uint8_t port_id, struct ether_addr *addr)
1171 struct rte_eth_dev *dev;
1174 if (port_id >= nb_ports) {
1175 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1178 dev = &rte_eth_devices[port_id];
1179 FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
1181 index = get_mac_addr_index(port_id, addr);
1183 PMD_DEBUG_TRACE("port %d: Cannot remove default MAC address\n", port_id);
1184 return (-EADDRINUSE);
1185 } else if (index < 0)
1186 return 0; /* Do nothing if address wasn't found */
1189 (*dev->dev_ops->mac_addr_remove)(dev, index);
1191 /* Update address in NIC data structure */
1192 ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
1197 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1199 rte_eth_rx_burst(uint8_t port_id, uint16_t queue_id,
1200 struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1202 struct rte_eth_dev *dev;
1204 if (port_id >= nb_ports) {
1205 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1208 dev = &rte_eth_devices[port_id];
1209 FUNC_PTR_OR_ERR_RET(*dev->rx_pkt_burst, -ENOTSUP);
1210 if (queue_id >= dev->data->nb_rx_queues) {
1211 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", queue_id);
1214 return (*dev->rx_pkt_burst)(dev->data->rx_queues[queue_id],
1219 rte_eth_tx_burst(uint8_t port_id, uint16_t queue_id,
1220 struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1222 struct rte_eth_dev *dev;
1224 if (port_id >= nb_ports) {
1225 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1228 dev = &rte_eth_devices[port_id];
1230 FUNC_PTR_OR_ERR_RET(*dev->tx_pkt_burst, -ENOTSUP);
1231 if (queue_id >= dev->data->nb_tx_queues) {
1232 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", queue_id);
1235 return (*dev->tx_pkt_burst)(dev->data->tx_queues[queue_id],
1241 rte_eth_dev_callback_register(uint8_t port_id,
1242 enum rte_eth_event_type event,
1243 rte_eth_dev_cb_fn cb_fn, void *cb_arg)
1246 struct rte_eth_dev *dev;
1247 struct rte_eth_dev_callback *user_cb = NULL;
1251 if (port_id >= nb_ports) {
1252 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1255 dev = &rte_eth_devices[port_id];
1256 rte_spinlock_lock(&rte_eth_dev_cb_lock);
1257 TAILQ_FOREACH(user_cb, &(dev->callbacks), next) {
1258 if (user_cb->cb_fn == cb_fn &&
1259 user_cb->cb_arg == cb_arg &&
1260 user_cb->event == event) {
1265 user_cb = rte_malloc("INTR_USER_CALLBACK",
1266 sizeof(struct rte_eth_dev_callback), 0);
1269 user_cb->cb_fn = cb_fn;
1270 user_cb->cb_arg = cb_arg;
1271 user_cb->event = event;
1272 TAILQ_INSERT_TAIL(&(dev->callbacks), user_cb, next);
1276 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1282 rte_eth_dev_callback_unregister(uint8_t port_id,
1283 enum rte_eth_event_type event,
1284 rte_eth_dev_cb_fn cb_fn, void *cb_arg)
1287 struct rte_eth_dev *dev;
1288 struct rte_eth_dev_callback *cb_lst = NULL;
1292 if (port_id >= nb_ports) {
1293 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1296 dev = &rte_eth_devices[port_id];
1297 rte_spinlock_lock(&rte_eth_dev_cb_lock);
1298 TAILQ_FOREACH(cb_lst, &(dev->callbacks), next) {
1299 if (cb_lst->cb_fn != cb_fn || cb_lst->event != event)
1301 if (cb_lst->cb_arg == (void *)-1 ||
1302 cb_lst->cb_arg == cb_arg) {
1303 TAILQ_REMOVE(&(dev->callbacks), cb_lst, next);
1309 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1315 _rte_eth_dev_callback_process(struct rte_eth_dev *dev, enum rte_eth_event_type event)
1317 struct rte_eth_dev_callback *cb_lst = NULL;
1318 struct rte_eth_dev_callback dev_cb;
1320 rte_spinlock_lock(&rte_eth_dev_cb_lock);
1321 TAILQ_FOREACH(cb_lst, &(dev->callbacks), next) {
1322 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1325 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1326 dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
1328 rte_spinlock_lock(&rte_eth_dev_cb_lock);
1330 rte_spinlock_unlock(&rte_eth_dev_cb_lock);