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.
37 #include <rte_malloc.h>
38 #include <rte_ethdev.h>
41 #include "rte_eth_bond.h"
42 #include "rte_eth_bond_private.h"
43 #include "rte_eth_bond_8023ad_private.h"
45 #define DEFAULT_POLLING_INTERVAL_10_MS (10)
48 valid_bonded_ethdev(struct rte_eth_dev *eth_dev)
52 /* Check valid pointer */
53 if (eth_dev->driver->pci_drv.name == NULL || driver_name == NULL)
56 /* Check string lengths are equal */
57 len = strlen(driver_name);
58 if (strlen(eth_dev->driver->pci_drv.name) != len)
62 return strncmp(eth_dev->driver->pci_drv.name, driver_name, len);
66 valid_port_id(uint8_t port_id)
68 /* Verify that port id is valid */
69 int ethdev_count = rte_eth_dev_count();
70 if (port_id >= ethdev_count) {
71 RTE_BOND_LOG(ERR, "Port Id %d is greater than rte_eth_dev_count %d",
72 port_id, ethdev_count);
80 valid_bonded_port_id(uint8_t port_id)
82 /* Verify that port id's are valid */
83 if (valid_port_id(port_id))
86 /* Verify that bonded_port_id refers to a bonded port */
87 if (valid_bonded_ethdev(&rte_eth_devices[port_id])) {
88 RTE_BOND_LOG(ERR, "Specified port Id %d is not a bonded eth_dev device",
97 valid_slave_port_id(uint8_t port_id)
99 /* Verify that port id's are valid */
100 if (valid_port_id(port_id))
103 /* Verify that port_id refers to a non bonded port */
104 if (!valid_bonded_ethdev(&rte_eth_devices[port_id]))
111 activate_slave(struct rte_eth_dev *eth_dev, uint8_t port_id)
113 struct bond_dev_private *internals = eth_dev->data->dev_private;
115 if (internals->mode == BONDING_MODE_8023AD)
116 bond_mode_8023ad_activate_slave(eth_dev, port_id);
118 internals->active_slaves[internals->active_slave_count] = port_id;
119 internals->active_slave_count++;
123 deactivate_slave(struct rte_eth_dev *eth_dev, uint8_t port_id)
126 struct bond_dev_private *internals = eth_dev->data->dev_private;
127 uint8_t active_count = internals->active_slave_count;
129 if (internals->mode == BONDING_MODE_8023AD) {
130 bond_mode_8023ad_stop(eth_dev);
131 bond_mode_8023ad_deactivate_slave(eth_dev, port_id);
134 slave_pos = find_slave_by_id(internals->active_slaves, active_count,
137 /* If slave was not at the end of the list
138 * shift active slaves up active array list */
139 if (slave_pos < active_count) {
141 memmove(internals->active_slaves + slave_pos,
142 internals->active_slaves + slave_pos + 1,
143 (active_count - slave_pos) *
144 sizeof(internals->active_slaves[0]));
147 internals->active_slave_count = active_count;
149 if (eth_dev->data->dev_started && internals->mode == BONDING_MODE_8023AD)
150 bond_mode_8023ad_start(eth_dev);
154 number_of_sockets(void)
158 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
160 for (i = 0; ((i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL)); i++) {
161 if (sockets < ms[i].socket_id)
162 sockets = ms[i].socket_id;
165 /* Number of sockets = maximum socket_id + 1 */
169 const char *driver_name = "Link Bonding PMD";
172 rte_eth_bond_create(const char *name, uint8_t mode, uint8_t socket_id)
174 struct rte_pci_device *pci_dev = NULL;
175 struct bond_dev_private *internals = NULL;
176 struct rte_eth_dev *eth_dev = NULL;
177 struct eth_driver *eth_drv = NULL;
178 struct rte_pci_driver *pci_drv = NULL;
179 struct rte_pci_id *pci_id_table = NULL;
180 /* now do all data allocation - for eth_dev structure, dummy pci driver
181 * and internal (private) data
185 RTE_BOND_LOG(ERR, "Invalid name specified");
189 if (socket_id >= number_of_sockets()) {
191 "Invalid socket id specified to create bonded device on.");
195 pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, socket_id);
196 if (pci_dev == NULL) {
197 RTE_BOND_LOG(ERR, "Unable to malloc pci dev on socket");
201 eth_drv = rte_zmalloc_socket(name, sizeof(*eth_drv), 0, socket_id);
202 if (eth_drv == NULL) {
203 RTE_BOND_LOG(ERR, "Unable to malloc eth_drv on socket");
207 pci_drv = rte_zmalloc_socket(name, sizeof(*pci_drv), 0, socket_id);
208 if (pci_drv == NULL) {
209 RTE_BOND_LOG(ERR, "Unable to malloc pci_drv on socket");
212 pci_id_table = rte_zmalloc_socket(name, sizeof(*pci_id_table), 0, socket_id);
213 if (pci_drv == NULL) {
214 RTE_BOND_LOG(ERR, "Unable to malloc pci_id_table on socket");
218 pci_drv->id_table = pci_id_table;
220 pci_drv->id_table->device_id = PCI_ANY_ID;
221 pci_drv->id_table->subsystem_device_id = PCI_ANY_ID;
222 pci_drv->id_table->vendor_id = PCI_ANY_ID;
223 pci_drv->id_table->subsystem_vendor_id = PCI_ANY_ID;
225 pci_drv->drv_flags = RTE_PCI_DRV_INTR_LSC;
227 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, socket_id);
228 if (internals == NULL) {
229 RTE_BOND_LOG(ERR, "Unable to malloc internals on socket");
233 /* reserve an ethdev entry */
234 eth_dev = rte_eth_dev_allocate(name);
235 if (eth_dev == NULL) {
236 RTE_BOND_LOG(ERR, "Unable to allocate rte_eth_dev");
240 pci_dev->numa_node = socket_id;
241 pci_drv->name = driver_name;
243 eth_drv->pci_drv = (struct rte_pci_driver)(*pci_drv);
244 eth_dev->driver = eth_drv;
246 eth_dev->data->dev_private = internals;
247 eth_dev->data->nb_rx_queues = (uint16_t)1;
248 eth_dev->data->nb_tx_queues = (uint16_t)1;
250 TAILQ_INIT(&(eth_dev->callbacks));
252 eth_dev->data->dev_link.link_status = 0;
254 eth_dev->data->mac_addrs = rte_zmalloc_socket(name, ETHER_ADDR_LEN, 0,
257 eth_dev->data->dev_started = 0;
258 eth_dev->data->promiscuous = 0;
259 eth_dev->data->scattered_rx = 0;
260 eth_dev->data->all_multicast = 0;
262 eth_dev->dev_ops = &default_dev_ops;
263 eth_dev->pci_dev = pci_dev;
265 rte_spinlock_init(&internals->lock);
267 internals->port_id = eth_dev->data->port_id;
268 internals->mode = BONDING_MODE_INVALID;
269 internals->current_primary_port = 0;
270 internals->balance_xmit_policy = BALANCE_XMIT_POLICY_LAYER2;
271 internals->user_defined_mac = 0;
272 internals->link_props_set = 0;
274 internals->link_status_polling_enabled = 0;
276 internals->link_status_polling_interval_ms = DEFAULT_POLLING_INTERVAL_10_MS;
277 internals->link_down_delay_ms = 0;
278 internals->link_up_delay_ms = 0;
280 internals->slave_count = 0;
281 internals->active_slave_count = 0;
282 internals->rx_offload_capa = 0;
283 internals->tx_offload_capa = 0;
285 memset(internals->active_slaves, 0, sizeof(internals->active_slaves));
286 memset(internals->slaves, 0, sizeof(internals->slaves));
288 /* Set mode 4 default configuration */
289 bond_mode_8023ad_setup(eth_dev, NULL);
290 if (bond_ethdev_mode_set(eth_dev, mode)) {
291 RTE_BOND_LOG(ERR, "Failed to set bonded device %d mode too %d",
292 eth_dev->data->port_id, mode);
296 return eth_dev->data->port_id;
304 rte_free(pci_id_table);
313 __eth_bond_slave_add_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
315 struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
316 struct bond_dev_private *internals;
317 struct bond_dev_private *temp_internals;
318 struct rte_eth_link link_props;
319 struct rte_eth_dev_info dev_info;
323 if (valid_slave_port_id(slave_port_id) != 0)
326 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
327 internals = bonded_eth_dev->data->dev_private;
329 /* Verify that new slave device is not already a slave of another
331 for (i = rte_eth_dev_count()-1; i >= 0; i--) {
332 if (valid_bonded_ethdev(&rte_eth_devices[i]) == 0) {
333 temp_internals = rte_eth_devices[i].data->dev_private;
335 for (j = 0; j < temp_internals->slave_count; j++) {
336 /* Device already a slave of a bonded device */
337 if (temp_internals->slaves[j].port_id == slave_port_id) {
338 RTE_BOND_LOG(ERR, "Slave port %d is already a slave",
346 slave_eth_dev = &rte_eth_devices[slave_port_id];
348 /* Add slave details to bonded device */
349 slave_add(internals, slave_eth_dev);
351 memset(&dev_info, 0, sizeof(dev_info));
352 rte_eth_dev_info_get(slave_port_id, &dev_info);
354 if (internals->slave_count < 1) {
355 /* if MAC is not user defined then use MAC of first slave add to
357 if (!internals->user_defined_mac)
358 mac_address_set(bonded_eth_dev, slave_eth_dev->data->mac_addrs);
360 /* Inherit eth dev link properties from first slave */
361 link_properties_set(bonded_eth_dev,
362 &(slave_eth_dev->data->dev_link));
364 /* Make primary slave */
365 internals->primary_port = slave_port_id;
367 /* Take the first dev's offload capabilities */
368 internals->rx_offload_capa = dev_info.rx_offload_capa;
369 internals->tx_offload_capa = dev_info.tx_offload_capa;
372 /* Check slave link properties are supported if props are set,
373 * all slaves must be the same */
374 if (internals->link_props_set) {
375 if (link_properties_valid(&(bonded_eth_dev->data->dev_link),
376 &(slave_eth_dev->data->dev_link))) {
378 "Slave port %d link speed/duplex not supported",
383 link_properties_set(bonded_eth_dev,
384 &(slave_eth_dev->data->dev_link));
386 internals->rx_offload_capa &= dev_info.rx_offload_capa;
387 internals->tx_offload_capa &= dev_info.tx_offload_capa;
390 internals->slave_count++;
392 /* Update all slave devices MACs*/
393 mac_address_slaves_update(bonded_eth_dev);
395 if (bonded_eth_dev->data->dev_started) {
396 if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
397 RTE_BOND_LOG(ERR, "rte_bond_slaves_configure: port=%d",
403 /* Register link status change callback with bonded device pointer as
405 rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
406 bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
408 /* If bonded device is started then we can add the slave to our active
410 if (bonded_eth_dev->data->dev_started) {
411 rte_eth_link_get_nowait(slave_port_id, &link_props);
413 if (link_props.link_status == 1)
414 activate_slave(bonded_eth_dev, slave_port_id);
421 rte_eth_bond_slave_add(uint8_t bonded_port_id, uint8_t slave_port_id)
423 struct rte_eth_dev *bonded_eth_dev;
424 struct bond_dev_private *internals;
428 /* Verify that port id's are valid bonded and slave ports */
429 if (valid_bonded_port_id(bonded_port_id) != 0)
432 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
433 internals = bonded_eth_dev->data->dev_private;
435 rte_spinlock_lock(&internals->lock);
437 retval = __eth_bond_slave_add_lock_free(bonded_port_id, slave_port_id);
439 rte_spinlock_unlock(&internals->lock);
445 __eth_bond_slave_remove_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
447 struct rte_eth_dev *bonded_eth_dev;
448 struct bond_dev_private *internals;
452 if (valid_slave_port_id(slave_port_id) != 0)
455 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
456 internals = bonded_eth_dev->data->dev_private;
458 /* first remove from active slave list */
459 slave_idx = find_slave_by_id(internals->active_slaves,
460 internals->active_slave_count, slave_port_id);
462 if (slave_idx < internals->active_slave_count)
463 deactivate_slave(bonded_eth_dev, slave_port_id);
466 /* now find in slave list */
467 for (i = 0; i < internals->slave_count; i++)
468 if (internals->slaves[i].port_id == slave_port_id) {
474 RTE_BOND_LOG(ERR, "Couldn't find slave in port list, slave count %d",
475 internals->slave_count);
479 /* Un-register link status change callback with bonded device pointer as
481 rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
482 bond_ethdev_lsc_event_callback,
483 &rte_eth_devices[bonded_port_id].data->port_id);
485 /* Restore original MAC address of slave device */
486 mac_address_set(&rte_eth_devices[slave_port_id],
487 &(internals->slaves[slave_idx].persisted_mac_addr));
489 slave_remove(internals, &rte_eth_devices[slave_port_id]);
491 /* first slave in the active list will be the primary by default,
492 * otherwise use first device in list */
493 if (internals->current_primary_port == slave_port_id) {
494 if (internals->active_slave_count > 0)
495 internals->current_primary_port = internals->active_slaves[0];
496 else if (internals->slave_count > 0)
497 internals->current_primary_port = internals->slaves[0].port_id;
499 internals->primary_port = 0;
502 if (internals->active_slave_count < 1) {
503 /* reset device link properties as no slaves are active */
504 link_properties_reset(&rte_eth_devices[bonded_port_id]);
506 /* if no slaves are any longer attached to bonded device and MAC is not
507 * user defined then clear MAC of bonded device as it will be reset
508 * when a new slave is added */
509 if (internals->slave_count < 1 && !internals->user_defined_mac)
510 memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
511 sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
513 if (internals->slave_count == 0) {
514 internals->rx_offload_capa = 0;
515 internals->tx_offload_capa = 0;
521 rte_eth_bond_slave_remove(uint8_t bonded_port_id, uint8_t slave_port_id)
523 struct rte_eth_dev *bonded_eth_dev;
524 struct bond_dev_private *internals;
527 if (valid_bonded_port_id(bonded_port_id) != 0)
530 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
531 internals = bonded_eth_dev->data->dev_private;
533 rte_spinlock_lock(&internals->lock);
535 retval = __eth_bond_slave_remove_lock_free(bonded_port_id, slave_port_id);
537 rte_spinlock_unlock(&internals->lock);
543 rte_eth_bond_mode_set(uint8_t bonded_port_id, uint8_t mode)
545 if (valid_bonded_port_id(bonded_port_id) != 0)
548 return bond_ethdev_mode_set(&rte_eth_devices[bonded_port_id], mode);
552 rte_eth_bond_mode_get(uint8_t bonded_port_id)
554 struct bond_dev_private *internals;
556 if (valid_bonded_port_id(bonded_port_id) != 0)
559 internals = rte_eth_devices[bonded_port_id].data->dev_private;
561 return internals->mode;
565 rte_eth_bond_primary_set(uint8_t bonded_port_id, uint8_t slave_port_id)
567 struct bond_dev_private *internals;
569 if (valid_bonded_port_id(bonded_port_id) != 0)
572 if (valid_slave_port_id(slave_port_id) != 0)
575 internals = rte_eth_devices[bonded_port_id].data->dev_private;
577 internals->user_defined_primary_port = 1;
578 internals->primary_port = slave_port_id;
580 bond_ethdev_primary_set(internals, slave_port_id);
586 rte_eth_bond_primary_get(uint8_t bonded_port_id)
588 struct bond_dev_private *internals;
590 if (valid_bonded_port_id(bonded_port_id) != 0)
593 internals = rte_eth_devices[bonded_port_id].data->dev_private;
595 if (internals->slave_count < 1)
598 return internals->current_primary_port;
602 rte_eth_bond_slaves_get(uint8_t bonded_port_id, uint8_t slaves[], uint8_t len)
604 struct bond_dev_private *internals;
607 if (valid_bonded_port_id(bonded_port_id) != 0)
613 internals = rte_eth_devices[bonded_port_id].data->dev_private;
615 if (internals->slave_count > len)
618 for (i = 0; i < internals->slave_count; i++)
619 slaves[i] = internals->slaves[i].port_id;
621 return internals->slave_count;
625 rte_eth_bond_active_slaves_get(uint8_t bonded_port_id, uint8_t slaves[],
628 struct bond_dev_private *internals;
630 if (valid_bonded_port_id(bonded_port_id) != 0)
636 internals = rte_eth_devices[bonded_port_id].data->dev_private;
638 if (internals->active_slave_count > len)
641 memcpy(slaves, internals->active_slaves, internals->active_slave_count);
643 return internals->active_slave_count;
647 rte_eth_bond_mac_address_set(uint8_t bonded_port_id,
648 struct ether_addr *mac_addr)
650 struct rte_eth_dev *bonded_eth_dev;
651 struct bond_dev_private *internals;
653 if (valid_bonded_port_id(bonded_port_id) != 0)
656 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
657 internals = bonded_eth_dev->data->dev_private;
659 /* Set MAC Address of Bonded Device */
660 if (mac_address_set(bonded_eth_dev, mac_addr))
663 internals->user_defined_mac = 1;
665 /* Update all slave devices MACs*/
666 if (internals->slave_count > 0)
667 return mac_address_slaves_update(bonded_eth_dev);
673 rte_eth_bond_mac_address_reset(uint8_t bonded_port_id)
675 struct rte_eth_dev *bonded_eth_dev;
676 struct bond_dev_private *internals;
678 if (valid_bonded_port_id(bonded_port_id) != 0)
681 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
682 internals = bonded_eth_dev->data->dev_private;
684 internals->user_defined_mac = 0;
686 if (internals->slave_count > 0) {
687 /* Set MAC Address of Bonded Device */
688 if (mac_address_set(bonded_eth_dev,
689 &internals->slaves[internals->primary_port].persisted_mac_addr)
691 RTE_BOND_LOG(ERR, "Failed to set MAC address on bonded device");
694 /* Update all slave devices MAC addresses */
695 return mac_address_slaves_update(bonded_eth_dev);
697 /* No need to update anything as no slaves present */
702 rte_eth_bond_xmit_policy_set(uint8_t bonded_port_id, uint8_t policy)
704 struct bond_dev_private *internals;
706 if (valid_bonded_port_id(bonded_port_id) != 0)
709 internals = rte_eth_devices[bonded_port_id].data->dev_private;
712 case BALANCE_XMIT_POLICY_LAYER2:
713 case BALANCE_XMIT_POLICY_LAYER23:
714 case BALANCE_XMIT_POLICY_LAYER34:
715 internals->balance_xmit_policy = policy;
725 rte_eth_bond_xmit_policy_get(uint8_t bonded_port_id)
727 struct bond_dev_private *internals;
729 if (valid_bonded_port_id(bonded_port_id) != 0)
732 internals = rte_eth_devices[bonded_port_id].data->dev_private;
734 return internals->balance_xmit_policy;
738 rte_eth_bond_link_monitoring_set(uint8_t bonded_port_id, uint32_t internal_ms)
740 struct bond_dev_private *internals;
742 if (valid_bonded_port_id(bonded_port_id) != 0)
745 internals = rte_eth_devices[bonded_port_id].data->dev_private;
746 internals->link_status_polling_interval_ms = internal_ms;
752 rte_eth_bond_link_monitoring_get(uint8_t bonded_port_id)
754 struct bond_dev_private *internals;
756 if (valid_bonded_port_id(bonded_port_id) != 0)
759 internals = rte_eth_devices[bonded_port_id].data->dev_private;
761 return internals->link_status_polling_interval_ms;
765 rte_eth_bond_link_down_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
768 struct bond_dev_private *internals;
770 if (valid_bonded_port_id(bonded_port_id) != 0)
773 internals = rte_eth_devices[bonded_port_id].data->dev_private;
774 internals->link_down_delay_ms = delay_ms;
780 rte_eth_bond_link_down_prop_delay_get(uint8_t bonded_port_id)
782 struct bond_dev_private *internals;
784 if (valid_bonded_port_id(bonded_port_id) != 0)
787 internals = rte_eth_devices[bonded_port_id].data->dev_private;
789 return internals->link_down_delay_ms;
793 rte_eth_bond_link_up_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
796 struct bond_dev_private *internals;
798 if (valid_bonded_port_id(bonded_port_id) != 0)
801 internals = rte_eth_devices[bonded_port_id].data->dev_private;
802 internals->link_up_delay_ms = delay_ms;
808 rte_eth_bond_link_up_prop_delay_get(uint8_t bonded_port_id)
810 struct bond_dev_private *internals;
812 if (valid_bonded_port_id(bonded_port_id) != 0)
815 internals = rte_eth_devices[bonded_port_id].data->dev_private;
817 return internals->link_up_delay_ms;