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;
283 memset(internals->active_slaves, 0, sizeof(internals->active_slaves));
284 memset(internals->slaves, 0, sizeof(internals->slaves));
286 /* Set mode 4 default configuration */
287 bond_mode_8023ad_setup(eth_dev, NULL);
288 if (bond_ethdev_mode_set(eth_dev, mode)) {
289 RTE_BOND_LOG(ERR, "Failed to set bonded device %d mode too %d",
290 eth_dev->data->port_id, mode);
294 return eth_dev->data->port_id;
302 rte_free(pci_id_table);
311 __eth_bond_slave_add_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
313 struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
314 struct bond_dev_private *internals;
315 struct bond_dev_private *temp_internals;
316 struct rte_eth_link link_props;
320 if (valid_slave_port_id(slave_port_id) != 0)
323 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
324 internals = bonded_eth_dev->data->dev_private;
326 /* Verify that new slave device is not already a slave of another
328 for (i = rte_eth_dev_count()-1; i >= 0; i--) {
329 if (valid_bonded_ethdev(&rte_eth_devices[i]) == 0) {
330 temp_internals = rte_eth_devices[i].data->dev_private;
332 for (j = 0; j < temp_internals->slave_count; j++) {
333 /* Device already a slave of a bonded device */
334 if (temp_internals->slaves[j].port_id == slave_port_id) {
335 RTE_BOND_LOG(ERR, "Slave port %d is already a slave",
343 slave_eth_dev = &rte_eth_devices[slave_port_id];
345 /* Add slave details to bonded device */
346 slave_add(internals, slave_eth_dev);
348 if (internals->slave_count < 1) {
349 /* if MAC is not user defined then use MAC of first slave add to
351 if (!internals->user_defined_mac)
352 mac_address_set(bonded_eth_dev, slave_eth_dev->data->mac_addrs);
354 /* Inherit eth dev link properties from first slave */
355 link_properties_set(bonded_eth_dev,
356 &(slave_eth_dev->data->dev_link));
358 /* Make primary slave */
359 internals->primary_port = slave_port_id;
361 /* Check slave link properties are supported if props are set,
362 * all slaves must be the same */
363 if (internals->link_props_set) {
364 if (link_properties_valid(&(bonded_eth_dev->data->dev_link),
365 &(slave_eth_dev->data->dev_link))) {
367 "Slave port %d link speed/duplex not supported",
372 link_properties_set(bonded_eth_dev,
373 &(slave_eth_dev->data->dev_link));
377 internals->slave_count++;
379 /* Update all slave devices MACs*/
380 mac_address_slaves_update(bonded_eth_dev);
382 if (bonded_eth_dev->data->dev_started) {
383 if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
384 RTE_BOND_LOG(ERR, "rte_bond_slaves_configure: port=%d",
390 /* Register link status change callback with bonded device pointer as
392 rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
393 bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
395 /* If bonded device is started then we can add the slave to our active
397 if (bonded_eth_dev->data->dev_started) {
398 rte_eth_link_get_nowait(slave_port_id, &link_props);
400 if (link_props.link_status == 1)
401 activate_slave(bonded_eth_dev, slave_port_id);
408 rte_eth_bond_slave_add(uint8_t bonded_port_id, uint8_t slave_port_id)
410 struct rte_eth_dev *bonded_eth_dev;
411 struct bond_dev_private *internals;
415 /* Verify that port id's are valid bonded and slave ports */
416 if (valid_bonded_port_id(bonded_port_id) != 0)
419 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
420 internals = bonded_eth_dev->data->dev_private;
422 rte_spinlock_lock(&internals->lock);
424 retval = __eth_bond_slave_add_lock_free(bonded_port_id, slave_port_id);
426 rte_spinlock_unlock(&internals->lock);
432 __eth_bond_slave_remove_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
434 struct rte_eth_dev *bonded_eth_dev;
435 struct bond_dev_private *internals;
439 if (valid_slave_port_id(slave_port_id) != 0)
442 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
443 internals = bonded_eth_dev->data->dev_private;
445 /* first remove from active slave list */
446 slave_idx = find_slave_by_id(internals->active_slaves,
447 internals->active_slave_count, slave_port_id);
449 if (slave_idx < internals->active_slave_count)
450 deactivate_slave(bonded_eth_dev, slave_port_id);
453 /* now find in slave list */
454 for (i = 0; i < internals->slave_count; i++)
455 if (internals->slaves[i].port_id == slave_port_id) {
461 RTE_BOND_LOG(ERR, "Couldn't find slave in port list, slave count %d",
462 internals->slave_count);
466 /* Un-register link status change callback with bonded device pointer as
468 rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
469 bond_ethdev_lsc_event_callback,
470 &rte_eth_devices[bonded_port_id].data->port_id);
472 /* Restore original MAC address of slave device */
473 mac_address_set(&rte_eth_devices[slave_port_id],
474 &(internals->slaves[slave_idx].persisted_mac_addr));
476 slave_remove(internals, &rte_eth_devices[slave_port_id]);
478 /* first slave in the active list will be the primary by default,
479 * otherwise use first device in list */
480 if (internals->current_primary_port == slave_port_id) {
481 if (internals->active_slave_count > 0)
482 internals->current_primary_port = internals->active_slaves[0];
483 else if (internals->slave_count > 0)
484 internals->current_primary_port = internals->slaves[0].port_id;
486 internals->primary_port = 0;
489 if (internals->active_slave_count < 1) {
490 /* reset device link properties as no slaves are active */
491 link_properties_reset(&rte_eth_devices[bonded_port_id]);
493 /* if no slaves are any longer attached to bonded device and MAC is not
494 * user defined then clear MAC of bonded device as it will be reset
495 * when a new slave is added */
496 if (internals->slave_count < 1 && !internals->user_defined_mac)
497 memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
498 sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
505 rte_eth_bond_slave_remove(uint8_t bonded_port_id, uint8_t slave_port_id)
507 struct rte_eth_dev *bonded_eth_dev;
508 struct bond_dev_private *internals;
511 if (valid_bonded_port_id(bonded_port_id) != 0)
514 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
515 internals = bonded_eth_dev->data->dev_private;
517 rte_spinlock_lock(&internals->lock);
519 retval = __eth_bond_slave_remove_lock_free(bonded_port_id, slave_port_id);
521 rte_spinlock_unlock(&internals->lock);
527 rte_eth_bond_mode_set(uint8_t bonded_port_id, uint8_t mode)
529 if (valid_bonded_port_id(bonded_port_id) != 0)
532 return bond_ethdev_mode_set(&rte_eth_devices[bonded_port_id], mode);
536 rte_eth_bond_mode_get(uint8_t bonded_port_id)
538 struct bond_dev_private *internals;
540 if (valid_bonded_port_id(bonded_port_id) != 0)
543 internals = rte_eth_devices[bonded_port_id].data->dev_private;
545 return internals->mode;
549 rte_eth_bond_primary_set(uint8_t bonded_port_id, uint8_t slave_port_id)
551 struct bond_dev_private *internals;
553 if (valid_bonded_port_id(bonded_port_id) != 0)
556 if (valid_slave_port_id(slave_port_id) != 0)
559 internals = rte_eth_devices[bonded_port_id].data->dev_private;
561 internals->user_defined_primary_port = 1;
562 internals->primary_port = slave_port_id;
564 bond_ethdev_primary_set(internals, slave_port_id);
570 rte_eth_bond_primary_get(uint8_t bonded_port_id)
572 struct bond_dev_private *internals;
574 if (valid_bonded_port_id(bonded_port_id) != 0)
577 internals = rte_eth_devices[bonded_port_id].data->dev_private;
579 if (internals->slave_count < 1)
582 return internals->current_primary_port;
586 rte_eth_bond_slaves_get(uint8_t bonded_port_id, uint8_t slaves[], uint8_t len)
588 struct bond_dev_private *internals;
591 if (valid_bonded_port_id(bonded_port_id) != 0)
597 internals = rte_eth_devices[bonded_port_id].data->dev_private;
599 if (internals->slave_count > len)
602 for (i = 0; i < internals->slave_count; i++)
603 slaves[i] = internals->slaves[i].port_id;
605 return internals->slave_count;
609 rte_eth_bond_active_slaves_get(uint8_t bonded_port_id, uint8_t slaves[],
612 struct bond_dev_private *internals;
614 if (valid_bonded_port_id(bonded_port_id) != 0)
620 internals = rte_eth_devices[bonded_port_id].data->dev_private;
622 if (internals->active_slave_count > len)
625 memcpy(slaves, internals->active_slaves, internals->active_slave_count);
627 return internals->active_slave_count;
631 rte_eth_bond_mac_address_set(uint8_t bonded_port_id,
632 struct ether_addr *mac_addr)
634 struct rte_eth_dev *bonded_eth_dev;
635 struct bond_dev_private *internals;
637 if (valid_bonded_port_id(bonded_port_id) != 0)
640 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
641 internals = bonded_eth_dev->data->dev_private;
643 /* Set MAC Address of Bonded Device */
644 if (mac_address_set(bonded_eth_dev, mac_addr))
647 internals->user_defined_mac = 1;
649 /* Update all slave devices MACs*/
650 if (internals->slave_count > 0)
651 return mac_address_slaves_update(bonded_eth_dev);
657 rte_eth_bond_mac_address_reset(uint8_t bonded_port_id)
659 struct rte_eth_dev *bonded_eth_dev;
660 struct bond_dev_private *internals;
662 if (valid_bonded_port_id(bonded_port_id) != 0)
665 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
666 internals = bonded_eth_dev->data->dev_private;
668 internals->user_defined_mac = 0;
670 if (internals->slave_count > 0) {
671 /* Set MAC Address of Bonded Device */
672 if (mac_address_set(bonded_eth_dev,
673 &internals->slaves[internals->primary_port].persisted_mac_addr)
675 RTE_BOND_LOG(ERR, "Failed to set MAC address on bonded device");
678 /* Update all slave devices MAC addresses */
679 return mac_address_slaves_update(bonded_eth_dev);
681 /* No need to update anything as no slaves present */
686 rte_eth_bond_xmit_policy_set(uint8_t bonded_port_id, uint8_t policy)
688 struct bond_dev_private *internals;
690 if (valid_bonded_port_id(bonded_port_id) != 0)
693 internals = rte_eth_devices[bonded_port_id].data->dev_private;
696 case BALANCE_XMIT_POLICY_LAYER2:
697 case BALANCE_XMIT_POLICY_LAYER23:
698 case BALANCE_XMIT_POLICY_LAYER34:
699 internals->balance_xmit_policy = policy;
709 rte_eth_bond_xmit_policy_get(uint8_t bonded_port_id)
711 struct bond_dev_private *internals;
713 if (valid_bonded_port_id(bonded_port_id) != 0)
716 internals = rte_eth_devices[bonded_port_id].data->dev_private;
718 return internals->balance_xmit_policy;
722 rte_eth_bond_link_monitoring_set(uint8_t bonded_port_id, uint32_t internal_ms)
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;
730 internals->link_status_polling_interval_ms = internal_ms;
736 rte_eth_bond_link_monitoring_get(uint8_t bonded_port_id)
738 struct bond_dev_private *internals;
740 if (valid_bonded_port_id(bonded_port_id) != 0)
743 internals = rte_eth_devices[bonded_port_id].data->dev_private;
745 return internals->link_status_polling_interval_ms;
749 rte_eth_bond_link_down_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
752 struct bond_dev_private *internals;
754 if (valid_bonded_port_id(bonded_port_id) != 0)
757 internals = rte_eth_devices[bonded_port_id].data->dev_private;
758 internals->link_down_delay_ms = delay_ms;
764 rte_eth_bond_link_down_prop_delay_get(uint8_t bonded_port_id)
766 struct bond_dev_private *internals;
768 if (valid_bonded_port_id(bonded_port_id) != 0)
771 internals = rte_eth_devices[bonded_port_id].data->dev_private;
773 return internals->link_down_delay_ms;
777 rte_eth_bond_link_up_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
780 struct bond_dev_private *internals;
782 if (valid_bonded_port_id(bonded_port_id) != 0)
785 internals = rte_eth_devices[bonded_port_id].data->dev_private;
786 internals->link_up_delay_ms = delay_ms;
792 rte_eth_bond_link_up_prop_delay_get(uint8_t bonded_port_id)
794 struct bond_dev_private *internals;
796 if (valid_bonded_port_id(bonded_port_id) != 0)
799 internals = rte_eth_devices[bonded_port_id].data->dev_private;
801 return internals->link_up_delay_ms;