4 * Copyright(c) 2010-2014 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 <rte_malloc.h>
36 #include <rte_ethdev.h>
39 #include "rte_eth_bond.h"
40 #include "rte_eth_bond_private.h"
42 #define DEFAULT_POLLING_INTERVAL_10_MS (10)
45 valid_bonded_ethdev(struct rte_eth_dev *eth_dev)
49 /* Check valid pointer */
50 if (eth_dev->driver->pci_drv.name == NULL || driver_name == NULL)
53 /* Check string lengths are equal */
54 len = strlen(driver_name);
55 if (strlen(eth_dev->driver->pci_drv.name) != len)
59 return strncmp(eth_dev->driver->pci_drv.name, driver_name, len);
63 valid_port_id(uint8_t port_id)
65 /* Verify that port id is valid */
66 int ethdev_count = rte_eth_dev_count();
67 if (port_id >= ethdev_count) {
68 RTE_BOND_LOG(ERR, "Port Id %d is greater than rte_eth_dev_count %d",
69 port_id, ethdev_count);
77 valid_bonded_port_id(uint8_t port_id)
79 /* Verify that port id's are valid */
80 if (valid_port_id(port_id))
83 /* Verify that bonded_port_id refers to a bonded port */
84 if (valid_bonded_ethdev(&rte_eth_devices[port_id])) {
85 RTE_BOND_LOG(ERR, "Specified port Id %d is not a bonded eth_dev device",
94 valid_slave_port_id(uint8_t port_id)
96 /* Verify that port id's are valid */
97 if (valid_port_id(port_id))
100 /* Verify that port_id refers to a non bonded port */
101 if (!valid_bonded_ethdev(&rte_eth_devices[port_id]))
108 number_of_sockets(void)
112 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
114 for (i = 0; ((i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL)); i++) {
115 if (sockets < ms[i].socket_id)
116 sockets = ms[i].socket_id;
119 /* Number of sockets = maximum socket_id + 1 */
123 const char *driver_name = "Link Bonding PMD";
126 rte_eth_bond_create(const char *name, uint8_t mode, uint8_t socket_id)
128 struct rte_pci_device *pci_dev = NULL;
129 struct bond_dev_private *internals = NULL;
130 struct rte_eth_dev *eth_dev = NULL;
131 struct eth_driver *eth_drv = NULL;
132 struct rte_pci_driver *pci_drv = NULL;
133 struct rte_pci_id *pci_id_table = NULL;
134 /* now do all data allocation - for eth_dev structure, dummy pci driver
135 * and internal (private) data
139 RTE_BOND_LOG(ERR, "Invalid name specified");
143 if (socket_id >= number_of_sockets()) {
145 "Invalid socket id specified to create bonded device on.");
149 pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, socket_id);
150 if (pci_dev == NULL) {
151 RTE_BOND_LOG(ERR, "Unable to malloc pci dev on socket");
155 eth_drv = rte_zmalloc_socket(name, sizeof(*eth_drv), 0, socket_id);
156 if (eth_drv == NULL) {
157 RTE_BOND_LOG(ERR, "Unable to malloc eth_drv on socket");
161 pci_drv = rte_zmalloc_socket(name, sizeof(*pci_drv), 0, socket_id);
162 if (pci_drv == NULL) {
163 RTE_BOND_LOG(ERR, "Unable to malloc pci_drv on socket");
166 pci_id_table = rte_zmalloc_socket(name, sizeof(*pci_id_table), 0, socket_id);
167 if (pci_drv == NULL) {
168 RTE_BOND_LOG(ERR, "Unable to malloc pci_id_table on socket");
172 pci_drv->id_table = pci_id_table;
174 pci_drv->id_table->device_id = PCI_ANY_ID;
175 pci_drv->id_table->subsystem_device_id = PCI_ANY_ID;
176 pci_drv->id_table->vendor_id = PCI_ANY_ID;
177 pci_drv->id_table->subsystem_vendor_id = PCI_ANY_ID;
179 pci_drv->drv_flags = RTE_PCI_DRV_INTR_LSC;
181 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, socket_id);
182 if (internals == NULL) {
183 RTE_BOND_LOG(ERR, "Unable to malloc internals on socket");
187 /* reserve an ethdev entry */
188 eth_dev = rte_eth_dev_allocate(name);
189 if (eth_dev == NULL) {
190 RTE_BOND_LOG(ERR, "Unable to allocate rte_eth_dev");
194 pci_dev->numa_node = socket_id;
195 pci_drv->name = driver_name;
197 eth_drv->pci_drv = (struct rte_pci_driver)(*pci_drv);
198 eth_dev->driver = eth_drv;
200 eth_dev->data->dev_private = internals;
201 eth_dev->data->nb_rx_queues = (uint16_t)1;
202 eth_dev->data->nb_tx_queues = (uint16_t)1;
204 TAILQ_INIT(&(eth_dev->callbacks));
206 eth_dev->data->dev_link.link_status = 0;
208 eth_dev->data->mac_addrs = rte_zmalloc_socket(name, ETHER_ADDR_LEN, 0,
211 eth_dev->data->dev_started = 0;
212 eth_dev->data->promiscuous = 0;
213 eth_dev->data->scattered_rx = 0;
214 eth_dev->data->all_multicast = 0;
216 eth_dev->dev_ops = &default_dev_ops;
217 eth_dev->pci_dev = pci_dev;
219 if (bond_ethdev_mode_set(eth_dev, mode)) {
220 RTE_BOND_LOG(ERR, "Failed to set bonded device %d mode too %d",
221 eth_dev->data->port_id, mode);
225 rte_spinlock_init(&internals->lock);
227 internals->port_id = eth_dev->data->port_id;
228 internals->current_primary_port = 0;
229 internals->balance_xmit_policy = BALANCE_XMIT_POLICY_LAYER2;
230 internals->user_defined_mac = 0;
231 internals->link_props_set = 0;
233 internals->link_status_polling_enabled = 0;
235 internals->link_status_polling_interval_ms = DEFAULT_POLLING_INTERVAL_10_MS;
236 internals->link_down_delay_ms = 0;
237 internals->link_up_delay_ms = 0;
239 internals->slave_count = 0;
240 internals->active_slave_count = 0;
242 memset(internals->active_slaves, 0, sizeof(internals->active_slaves));
243 memset(internals->slaves, 0, sizeof(internals->slaves));
245 return eth_dev->data->port_id;
253 rte_free(pci_id_table);
262 __eth_bond_slave_add_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
264 struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
265 struct bond_dev_private *internals;
266 struct bond_dev_private *temp_internals;
267 struct rte_eth_link link_props;
271 if (valid_slave_port_id(slave_port_id) != 0)
274 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
275 internals = bonded_eth_dev->data->dev_private;
277 /* Verify that new slave device is not already a slave of another
279 for (i = rte_eth_dev_count()-1; i >= 0; i--) {
280 if (valid_bonded_ethdev(&rte_eth_devices[i]) == 0) {
281 temp_internals = rte_eth_devices[i].data->dev_private;
283 for (j = 0; j < temp_internals->slave_count; j++) {
284 /* Device already a slave of a bonded device */
285 if (temp_internals->slaves[j].port_id == slave_port_id) {
286 RTE_BOND_LOG(ERR, "Slave port %d is already a slave",
294 slave_eth_dev = &rte_eth_devices[slave_port_id];
296 /* Add slave details to bonded device */
297 slave_add(internals, slave_eth_dev);
299 if (internals->slave_count < 1) {
300 /* if MAC is not user defined then use MAC of first slave add to
302 if (!internals->user_defined_mac)
303 mac_address_set(bonded_eth_dev, slave_eth_dev->data->mac_addrs);
305 /* Inherit eth dev link properties from first slave */
306 link_properties_set(bonded_eth_dev,
307 &(slave_eth_dev->data->dev_link));
309 /* Make primary slave */
310 internals->primary_port = slave_port_id;
312 /* Check slave link properties are supported if props are set,
313 * all slaves must be the same */
314 if (internals->link_props_set) {
315 if (link_properties_valid(&(bonded_eth_dev->data->dev_link),
316 &(slave_eth_dev->data->dev_link))) {
318 "Slave port %d link speed/duplex not supported",
323 link_properties_set(bonded_eth_dev,
324 &(slave_eth_dev->data->dev_link));
328 internals->slave_count++;
330 /* Update all slave devices MACs*/
331 mac_address_slaves_update(bonded_eth_dev);
333 if (bonded_eth_dev->data->dev_started) {
334 if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
335 RTE_BOND_LOG(ERR, "rte_bond_slaves_configure: port=%d",
341 /* Register link status change callback with bonded device pointer as
343 rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
344 bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
346 /* If bonded device is started then we can add the slave to our active
348 if (bonded_eth_dev->data->dev_started) {
349 rte_eth_link_get_nowait(slave_port_id, &link_props);
351 if (link_props.link_status == 1)
352 internals->active_slaves[internals->active_slave_count++] =
361 rte_eth_bond_slave_add(uint8_t bonded_port_id, uint8_t slave_port_id)
363 struct rte_eth_dev *bonded_eth_dev;
364 struct bond_dev_private *internals;
368 /* Verify that port id's are valid bonded and slave ports */
369 if (valid_bonded_port_id(bonded_port_id) != 0)
372 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
373 internals = bonded_eth_dev->data->dev_private;
375 rte_spinlock_lock(&internals->lock);
377 retval = __eth_bond_slave_add_lock_free(bonded_port_id, slave_port_id);
379 rte_spinlock_unlock(&internals->lock);
386 __eth_bond_slave_remove_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
388 struct bond_dev_private *internals;
390 int i, slave_idx = -1;
392 if (valid_slave_port_id(slave_port_id) != 0)
395 internals = rte_eth_devices[bonded_port_id].data->dev_private;
397 /* first remove from active slave list */
398 for (i = 0; i < internals->active_slave_count; i++) {
399 if (internals->active_slaves[i] == slave_port_id)
402 /* shift active slaves up active array list */
403 if (slave_idx >= 0 && i < (internals->active_slave_count - 1))
404 internals->active_slaves[i] = internals->active_slaves[i+1];
408 internals->active_slave_count--;
411 /* now find in slave list */
412 for (i = 0; i < internals->slave_count; i++)
413 if (internals->slaves[i].port_id == slave_port_id) {
419 RTE_BOND_LOG(ERR, "Couldn't find slave in port list, slave count %d",
420 internals->slave_count);
424 /* Un-register link status change callback with bonded device pointer as
426 rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
427 bond_ethdev_lsc_event_callback,
428 &rte_eth_devices[bonded_port_id].data->port_id);
430 /* Restore original MAC address of slave device */
431 mac_address_set(&rte_eth_devices[slave_port_id],
432 &(internals->slaves[slave_idx].persisted_mac_addr));
434 slave_remove(internals, &rte_eth_devices[slave_port_id]);
436 /* first slave in the active list will be the primary by default,
437 * otherwise use first device in list */
438 if (internals->current_primary_port == slave_port_id) {
439 if (internals->active_slave_count > 0)
440 internals->current_primary_port = internals->active_slaves[0];
441 else if (internals->slave_count > 0)
442 internals->current_primary_port = internals->slaves[0].port_id;
444 internals->primary_port = 0;
447 if (internals->active_slave_count < 1) {
448 /* reset device link properties as no slaves are active */
449 link_properties_reset(&rte_eth_devices[bonded_port_id]);
451 /* if no slaves are any longer attached to bonded device and MAC is not
452 * user defined then clear MAC of bonded device as it will be reset
453 * when a new slave is added */
454 if (internals->slave_count < 1 && !internals->user_defined_mac)
455 memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
456 sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
463 rte_eth_bond_slave_remove(uint8_t bonded_port_id, uint8_t slave_port_id)
465 struct rte_eth_dev *bonded_eth_dev;
466 struct bond_dev_private *internals;
469 if (valid_bonded_port_id(bonded_port_id) != 0)
472 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
473 internals = bonded_eth_dev->data->dev_private;
475 rte_spinlock_lock(&internals->lock);
477 retval = __eth_bond_slave_remove_lock_free(bonded_port_id, slave_port_id);
479 rte_spinlock_unlock(&internals->lock);
485 rte_eth_bond_mode_set(uint8_t bonded_port_id, uint8_t mode)
487 if (valid_bonded_port_id(bonded_port_id) != 0)
490 return bond_ethdev_mode_set(&rte_eth_devices[bonded_port_id], mode);
494 rte_eth_bond_mode_get(uint8_t bonded_port_id)
496 struct bond_dev_private *internals;
498 if (valid_bonded_port_id(bonded_port_id) != 0)
501 internals = rte_eth_devices[bonded_port_id].data->dev_private;
503 return internals->mode;
507 rte_eth_bond_primary_set(uint8_t bonded_port_id, uint8_t slave_port_id)
509 struct bond_dev_private *internals;
511 if (valid_bonded_port_id(bonded_port_id) != 0)
514 if (valid_slave_port_id(slave_port_id) != 0)
517 internals = rte_eth_devices[bonded_port_id].data->dev_private;
519 internals->user_defined_primary_port = 1;
520 internals->primary_port = slave_port_id;
522 bond_ethdev_primary_set(internals, slave_port_id);
528 rte_eth_bond_primary_get(uint8_t bonded_port_id)
530 struct bond_dev_private *internals;
532 if (valid_bonded_port_id(bonded_port_id) != 0)
535 internals = rte_eth_devices[bonded_port_id].data->dev_private;
537 if (internals->slave_count < 1)
540 return internals->current_primary_port;
543 rte_eth_bond_slaves_get(uint8_t bonded_port_id, uint8_t slaves[], uint8_t len)
545 struct bond_dev_private *internals;
548 if (valid_bonded_port_id(bonded_port_id) != 0)
554 internals = rte_eth_devices[bonded_port_id].data->dev_private;
556 if (internals->slave_count > len)
559 for (i = 0; i < internals->slave_count; i++)
560 slaves[i] = internals->slaves[i].port_id;
562 return internals->slave_count;
566 rte_eth_bond_active_slaves_get(uint8_t bonded_port_id, uint8_t slaves[],
569 struct bond_dev_private *internals;
571 if (valid_bonded_port_id(bonded_port_id) != 0)
577 internals = rte_eth_devices[bonded_port_id].data->dev_private;
579 if (internals->active_slave_count > len)
582 memcpy(slaves, internals->active_slaves, internals->active_slave_count);
584 return internals->active_slave_count;
588 rte_eth_bond_mac_address_set(uint8_t bonded_port_id,
589 struct ether_addr *mac_addr)
591 struct rte_eth_dev *bonded_eth_dev;
592 struct bond_dev_private *internals;
594 if (valid_bonded_port_id(bonded_port_id) != 0)
597 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
598 internals = bonded_eth_dev->data->dev_private;
600 /* Set MAC Address of Bonded Device */
601 if (mac_address_set(bonded_eth_dev, mac_addr))
604 internals->user_defined_mac = 1;
606 /* Update all slave devices MACs*/
607 if (internals->slave_count > 0)
608 return mac_address_slaves_update(bonded_eth_dev);
614 rte_eth_bond_mac_address_reset(uint8_t bonded_port_id)
616 struct rte_eth_dev *bonded_eth_dev;
617 struct bond_dev_private *internals;
619 if (valid_bonded_port_id(bonded_port_id) != 0)
622 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
623 internals = bonded_eth_dev->data->dev_private;
625 internals->user_defined_mac = 0;
627 if (internals->slave_count > 0) {
628 /* Set MAC Address of Bonded Device */
629 if (mac_address_set(bonded_eth_dev,
630 &internals->slaves[internals->primary_port].persisted_mac_addr)
632 RTE_BOND_LOG(ERR, "Failed to set MAC address on bonded device");
635 /* Update all slave devices MAC addresses */
636 return mac_address_slaves_update(bonded_eth_dev);
638 /* No need to update anything as no slaves present */
643 rte_eth_bond_xmit_policy_set(uint8_t bonded_port_id, uint8_t policy)
645 struct bond_dev_private *internals;
647 if (valid_bonded_port_id(bonded_port_id) != 0)
650 internals = rte_eth_devices[bonded_port_id].data->dev_private;
653 case BALANCE_XMIT_POLICY_LAYER2:
654 case BALANCE_XMIT_POLICY_LAYER23:
655 case BALANCE_XMIT_POLICY_LAYER34:
656 internals->balance_xmit_policy = policy;
666 rte_eth_bond_xmit_policy_get(uint8_t bonded_port_id)
668 struct bond_dev_private *internals;
670 if (valid_bonded_port_id(bonded_port_id) != 0)
673 internals = rte_eth_devices[bonded_port_id].data->dev_private;
675 return internals->balance_xmit_policy;
680 rte_eth_bond_link_monitoring_set(uint8_t bonded_port_id, uint32_t internal_ms)
682 struct bond_dev_private *internals;
684 if (valid_bonded_port_id(bonded_port_id) != 0)
687 internals = rte_eth_devices[bonded_port_id].data->dev_private;
688 internals->link_status_polling_interval_ms = internal_ms;
694 rte_eth_bond_link_monitoring_get(uint8_t bonded_port_id)
696 struct bond_dev_private *internals;
698 if (valid_bonded_port_id(bonded_port_id) != 0)
701 internals = rte_eth_devices[bonded_port_id].data->dev_private;
703 return internals->link_status_polling_interval_ms;
707 rte_eth_bond_link_down_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
710 struct bond_dev_private *internals;
712 if (valid_bonded_port_id(bonded_port_id) != 0)
715 internals = rte_eth_devices[bonded_port_id].data->dev_private;
716 internals->link_down_delay_ms = delay_ms;
722 rte_eth_bond_link_down_prop_delay_get(uint8_t bonded_port_id)
724 struct bond_dev_private *internals;
726 if (valid_bonded_port_id(bonded_port_id) != 0)
729 internals = rte_eth_devices[bonded_port_id].data->dev_private;
731 return internals->link_down_delay_ms;
736 rte_eth_bond_link_up_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
739 struct bond_dev_private *internals;
741 if (valid_bonded_port_id(bonded_port_id) != 0)
744 internals = rte_eth_devices[bonded_port_id].data->dev_private;
745 internals->link_up_delay_ms = delay_ms;
751 rte_eth_bond_link_up_prop_delay_get(uint8_t bonded_port_id)
753 struct bond_dev_private *internals;
755 if (valid_bonded_port_id(bonded_port_id) != 0)
758 internals = rte_eth_devices[bonded_port_id].data->dev_private;
760 return internals->link_up_delay_ms;