4 * Copyright(c) 2010-2017 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>
40 #include <rte_bus_vdev.h>
41 #include <rte_kvargs.h>
43 #include "rte_eth_bond.h"
44 #include "rte_eth_bond_private.h"
45 #include "rte_eth_bond_8023ad_private.h"
48 check_for_bonded_ethdev(const struct rte_eth_dev *eth_dev)
50 /* Check valid pointer */
51 if (eth_dev->device->driver->name == NULL)
54 /* return 0 if driver name matches */
55 return eth_dev->device->driver->name != pmd_bond_drv.driver.name;
59 valid_bonded_port_id(uint16_t port_id)
61 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
62 return check_for_bonded_ethdev(&rte_eth_devices[port_id]);
66 check_for_master_bonded_ethdev(const struct rte_eth_dev *eth_dev)
69 struct bond_dev_private *internals;
71 if (check_for_bonded_ethdev(eth_dev) != 0)
74 internals = eth_dev->data->dev_private;
76 /* Check if any of slave devices is a bonded device */
77 for (i = 0; i < internals->slave_count; i++)
78 if (valid_bonded_port_id(internals->slaves[i].port_id) == 0)
85 valid_slave_port_id(uint16_t port_id, uint8_t mode)
87 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
89 /* Verify that port_id refers to a non bonded port */
90 if (check_for_bonded_ethdev(&rte_eth_devices[port_id]) == 0 &&
91 mode == BONDING_MODE_8023AD) {
92 RTE_BOND_LOG(ERR, "Cannot add slave to bonded device in 802.3ad"
93 " mode as slave is also a bonded device, only "
94 "physical devices can be support in this mode.");
102 activate_slave(struct rte_eth_dev *eth_dev, uint16_t port_id)
104 struct bond_dev_private *internals = eth_dev->data->dev_private;
105 uint8_t active_count = internals->active_slave_count;
107 if (internals->mode == BONDING_MODE_8023AD)
108 bond_mode_8023ad_activate_slave(eth_dev, port_id);
110 if (internals->mode == BONDING_MODE_TLB
111 || internals->mode == BONDING_MODE_ALB) {
113 internals->tlb_slaves_order[active_count] = port_id;
116 RTE_ASSERT(internals->active_slave_count <
117 (RTE_DIM(internals->active_slaves) - 1));
119 internals->active_slaves[internals->active_slave_count] = port_id;
120 internals->active_slave_count++;
122 if (internals->mode == BONDING_MODE_TLB)
123 bond_tlb_activate_slave(internals);
124 if (internals->mode == BONDING_MODE_ALB)
125 bond_mode_alb_client_list_upd(eth_dev);
129 deactivate_slave(struct rte_eth_dev *eth_dev, uint16_t port_id)
132 struct bond_dev_private *internals = eth_dev->data->dev_private;
133 uint16_t active_count = internals->active_slave_count;
135 if (internals->mode == BONDING_MODE_8023AD) {
136 bond_mode_8023ad_stop(eth_dev);
137 bond_mode_8023ad_deactivate_slave(eth_dev, port_id);
138 } else if (internals->mode == BONDING_MODE_TLB
139 || internals->mode == BONDING_MODE_ALB)
140 bond_tlb_disable(internals);
142 slave_pos = find_slave_by_id(internals->active_slaves, active_count,
145 /* If slave was not at the end of the list
146 * shift active slaves up active array list */
147 if (slave_pos < active_count) {
149 memmove(internals->active_slaves + slave_pos,
150 internals->active_slaves + slave_pos + 1,
151 (active_count - slave_pos) *
152 sizeof(internals->active_slaves[0]));
155 RTE_ASSERT(active_count < RTE_DIM(internals->active_slaves));
156 internals->active_slave_count = active_count;
158 if (eth_dev->data->dev_started) {
159 if (internals->mode == BONDING_MODE_8023AD) {
160 bond_mode_8023ad_start(eth_dev);
161 } else if (internals->mode == BONDING_MODE_TLB) {
162 bond_tlb_enable(internals);
163 } else if (internals->mode == BONDING_MODE_ALB) {
164 bond_tlb_enable(internals);
165 bond_mode_alb_client_list_upd(eth_dev);
171 rte_eth_bond_create(const char *name, uint8_t mode, uint8_t socket_id)
173 struct bond_dev_private *internals;
179 RTE_BOND_LOG(ERR, "Invalid name specified");
183 ret = snprintf(devargs, sizeof(devargs),
184 "driver=net_bonding,mode=%d,socket_id=%d", mode, socket_id);
185 if (ret < 0 || ret >= (int)sizeof(devargs))
188 ret = rte_vdev_init(name, devargs);
192 ret = rte_eth_dev_get_port_by_name(name, &port_id);
196 * To make bond_ethdev_configure() happy we need to free the
197 * internals->kvlist here.
199 * Also see comment in bond_ethdev_configure().
201 internals = rte_eth_devices[port_id].data->dev_private;
202 rte_kvargs_free(internals->kvlist);
203 internals->kvlist = NULL;
209 rte_eth_bond_free(const char *name)
211 return rte_vdev_uninit(name);
215 slave_vlan_filter_set(uint16_t bonded_port_id, uint16_t slave_port_id)
217 struct rte_eth_dev *bonded_eth_dev;
218 struct bond_dev_private *internals;
225 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
226 if (bonded_eth_dev->data->dev_conf.rxmode.hw_vlan_filter == 0)
229 internals = bonded_eth_dev->data->dev_private;
230 found = rte_bitmap_scan(internals->vlan_filter_bmp, &pos, &slab);
240 for (i = 0, mask = 1;
241 i < RTE_BITMAP_SLAB_BIT_SIZE;
243 if (unlikely(slab & mask))
244 res = rte_eth_dev_vlan_filter(slave_port_id,
247 found = rte_bitmap_scan(internals->vlan_filter_bmp,
249 } while (found && first != pos && res == 0);
255 __eth_bond_slave_add_lock_free(uint16_t bonded_port_id, uint16_t slave_port_id)
257 struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
258 struct bond_dev_private *internals;
259 struct rte_eth_link link_props;
260 struct rte_eth_dev_info dev_info;
262 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
263 internals = bonded_eth_dev->data->dev_private;
265 if (valid_slave_port_id(slave_port_id, internals->mode) != 0)
268 slave_eth_dev = &rte_eth_devices[slave_port_id];
269 if (slave_eth_dev->data->dev_flags & RTE_ETH_DEV_BONDED_SLAVE) {
270 RTE_BOND_LOG(ERR, "Slave device is already a slave of a bonded device");
274 /* Add slave details to bonded device */
275 slave_eth_dev->data->dev_flags |= RTE_ETH_DEV_BONDED_SLAVE;
277 rte_eth_dev_info_get(slave_port_id, &dev_info);
278 if (dev_info.max_rx_pktlen < internals->max_rx_pktlen) {
279 RTE_BOND_LOG(ERR, "Slave (port %u) max_rx_pktlen too small",
284 slave_add(internals, slave_eth_dev);
286 /* We need to store slaves reta_size to be able to synchronize RETA for all
287 * slave devices even if its sizes are different.
289 internals->slaves[internals->slave_count].reta_size = dev_info.reta_size;
291 if (internals->slave_count < 1) {
292 /* if MAC is not user defined then use MAC of first slave add to
294 if (!internals->user_defined_mac) {
295 if (mac_address_set(bonded_eth_dev,
296 slave_eth_dev->data->mac_addrs)) {
297 RTE_BOND_LOG(ERR, "Failed to set MAC address");
302 /* Inherit eth dev link properties from first slave */
303 link_properties_set(bonded_eth_dev,
304 &(slave_eth_dev->data->dev_link));
306 /* Make primary slave */
307 internals->primary_port = slave_port_id;
308 internals->current_primary_port = slave_port_id;
310 /* Inherit queues settings from first slave */
311 internals->nb_rx_queues = slave_eth_dev->data->nb_rx_queues;
312 internals->nb_tx_queues = slave_eth_dev->data->nb_tx_queues;
314 internals->reta_size = dev_info.reta_size;
316 /* Take the first dev's offload capabilities */
317 internals->rx_offload_capa = dev_info.rx_offload_capa;
318 internals->tx_offload_capa = dev_info.tx_offload_capa;
319 internals->flow_type_rss_offloads = dev_info.flow_type_rss_offloads;
321 /* Inherit first slave's max rx packet size */
322 internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
325 internals->rx_offload_capa &= dev_info.rx_offload_capa;
326 internals->tx_offload_capa &= dev_info.tx_offload_capa;
327 internals->flow_type_rss_offloads &= dev_info.flow_type_rss_offloads;
329 if (link_properties_valid(bonded_eth_dev,
330 &slave_eth_dev->data->dev_link) != 0) {
331 RTE_BOND_LOG(ERR, "Invalid link properties for slave %d"
332 " in bonding mode %d", slave_port_id,
337 /* RETA size is GCD of all slaves RETA sizes, so, if all sizes will be
338 * the power of 2, the lower one is GCD
340 if (internals->reta_size > dev_info.reta_size)
341 internals->reta_size = dev_info.reta_size;
343 if (!internals->max_rx_pktlen &&
344 dev_info.max_rx_pktlen < internals->candidate_max_rx_pktlen)
345 internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
348 bonded_eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf &=
349 internals->flow_type_rss_offloads;
351 internals->slave_count++;
353 /* Update all slave devices MACs*/
354 mac_address_slaves_update(bonded_eth_dev);
356 if (bonded_eth_dev->data->dev_started) {
357 if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
358 slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
359 RTE_BOND_LOG(ERR, "rte_bond_slaves_configure: port=%d",
365 /* Register link status change callback with bonded device pointer as
367 rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
368 bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
370 /* If bonded device is started then we can add the slave to our active
372 if (bonded_eth_dev->data->dev_started) {
373 rte_eth_link_get_nowait(slave_port_id, &link_props);
375 if (link_props.link_status == ETH_LINK_UP) {
376 if (internals->active_slave_count == 0 &&
377 !internals->user_defined_primary_port)
378 bond_ethdev_primary_set(internals,
381 if (find_slave_by_id(internals->active_slaves,
382 internals->active_slave_count,
383 slave_port_id) == internals->active_slave_count)
384 activate_slave(bonded_eth_dev, slave_port_id);
388 slave_vlan_filter_set(bonded_port_id, slave_port_id);
395 rte_eth_bond_slave_add(uint16_t bonded_port_id, uint16_t slave_port_id)
397 struct rte_eth_dev *bonded_eth_dev;
398 struct bond_dev_private *internals;
402 /* Verify that port id's are valid bonded and slave ports */
403 if (valid_bonded_port_id(bonded_port_id) != 0)
406 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
407 internals = bonded_eth_dev->data->dev_private;
409 rte_spinlock_lock(&internals->lock);
411 retval = __eth_bond_slave_add_lock_free(bonded_port_id, slave_port_id);
413 rte_spinlock_unlock(&internals->lock);
419 __eth_bond_slave_remove_lock_free(uint16_t bonded_port_id,
420 uint16_t slave_port_id)
422 struct rte_eth_dev *bonded_eth_dev;
423 struct bond_dev_private *internals;
424 struct rte_eth_dev *slave_eth_dev;
427 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
428 internals = bonded_eth_dev->data->dev_private;
430 if (valid_slave_port_id(slave_port_id, internals->mode) < 0)
433 /* first remove from active slave list */
434 slave_idx = find_slave_by_id(internals->active_slaves,
435 internals->active_slave_count, slave_port_id);
437 if (slave_idx < internals->active_slave_count)
438 deactivate_slave(bonded_eth_dev, slave_port_id);
441 /* now find in slave list */
442 for (i = 0; i < internals->slave_count; i++)
443 if (internals->slaves[i].port_id == slave_port_id) {
449 RTE_BOND_LOG(ERR, "Couldn't find slave in port list, slave count %d",
450 internals->slave_count);
454 /* Un-register link status change callback with bonded device pointer as
456 rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
457 bond_ethdev_lsc_event_callback,
458 &rte_eth_devices[bonded_port_id].data->port_id);
460 /* Restore original MAC address of slave device */
461 rte_eth_dev_default_mac_addr_set(slave_port_id,
462 &(internals->slaves[slave_idx].persisted_mac_addr));
464 slave_eth_dev = &rte_eth_devices[slave_port_id];
465 slave_remove(internals, slave_eth_dev);
466 slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
468 /* first slave in the active list will be the primary by default,
469 * otherwise use first device in list */
470 if (internals->current_primary_port == slave_port_id) {
471 if (internals->active_slave_count > 0)
472 internals->current_primary_port = internals->active_slaves[0];
473 else if (internals->slave_count > 0)
474 internals->current_primary_port = internals->slaves[0].port_id;
476 internals->primary_port = 0;
479 if (internals->active_slave_count < 1) {
480 /* if no slaves are any longer attached to bonded device and MAC is not
481 * user defined then clear MAC of bonded device as it will be reset
482 * when a new slave is added */
483 if (internals->slave_count < 1 && !internals->user_defined_mac)
484 memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
485 sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
487 if (internals->slave_count == 0) {
488 internals->rx_offload_capa = 0;
489 internals->tx_offload_capa = 0;
490 internals->flow_type_rss_offloads = ETH_RSS_PROTO_MASK;
491 internals->reta_size = 0;
492 internals->candidate_max_rx_pktlen = 0;
493 internals->max_rx_pktlen = 0;
499 rte_eth_bond_slave_remove(uint16_t bonded_port_id, uint16_t slave_port_id)
501 struct rte_eth_dev *bonded_eth_dev;
502 struct bond_dev_private *internals;
505 if (valid_bonded_port_id(bonded_port_id) != 0)
508 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
509 internals = bonded_eth_dev->data->dev_private;
511 rte_spinlock_lock(&internals->lock);
513 retval = __eth_bond_slave_remove_lock_free(bonded_port_id, slave_port_id);
515 rte_spinlock_unlock(&internals->lock);
521 rte_eth_bond_mode_set(uint16_t bonded_port_id, uint8_t mode)
523 struct rte_eth_dev *bonded_eth_dev;
525 if (valid_bonded_port_id(bonded_port_id) != 0)
528 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
530 if (check_for_master_bonded_ethdev(bonded_eth_dev) != 0 &&
531 mode == BONDING_MODE_8023AD)
534 return bond_ethdev_mode_set(bonded_eth_dev, mode);
538 rte_eth_bond_mode_get(uint16_t bonded_port_id)
540 struct bond_dev_private *internals;
542 if (valid_bonded_port_id(bonded_port_id) != 0)
545 internals = rte_eth_devices[bonded_port_id].data->dev_private;
547 return internals->mode;
551 rte_eth_bond_primary_set(uint16_t bonded_port_id, uint16_t slave_port_id)
553 struct bond_dev_private *internals;
555 if (valid_bonded_port_id(bonded_port_id) != 0)
558 internals = rte_eth_devices[bonded_port_id].data->dev_private;
560 if (valid_slave_port_id(slave_port_id, internals->mode) != 0)
563 internals->user_defined_primary_port = 1;
564 internals->primary_port = slave_port_id;
566 bond_ethdev_primary_set(internals, slave_port_id);
572 rte_eth_bond_primary_get(uint16_t bonded_port_id)
574 struct bond_dev_private *internals;
576 if (valid_bonded_port_id(bonded_port_id) != 0)
579 internals = rte_eth_devices[bonded_port_id].data->dev_private;
581 if (internals->slave_count < 1)
584 return internals->current_primary_port;
588 rte_eth_bond_slaves_get(uint16_t bonded_port_id, uint16_t slaves[],
591 struct bond_dev_private *internals;
594 if (valid_bonded_port_id(bonded_port_id) != 0)
600 internals = rte_eth_devices[bonded_port_id].data->dev_private;
602 if (internals->slave_count > len)
605 for (i = 0; i < internals->slave_count; i++)
606 slaves[i] = internals->slaves[i].port_id;
608 return internals->slave_count;
612 rte_eth_bond_active_slaves_get(uint16_t bonded_port_id, uint16_t slaves[],
615 struct bond_dev_private *internals;
617 if (valid_bonded_port_id(bonded_port_id) != 0)
623 internals = rte_eth_devices[bonded_port_id].data->dev_private;
625 if (internals->active_slave_count > len)
628 memcpy(slaves, internals->active_slaves,
629 internals->active_slave_count * sizeof(internals->active_slaves[0]));
631 return internals->active_slave_count;
635 rte_eth_bond_mac_address_set(uint16_t bonded_port_id,
636 struct ether_addr *mac_addr)
638 struct rte_eth_dev *bonded_eth_dev;
639 struct bond_dev_private *internals;
641 if (valid_bonded_port_id(bonded_port_id) != 0)
644 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
645 internals = bonded_eth_dev->data->dev_private;
647 /* Set MAC Address of Bonded Device */
648 if (mac_address_set(bonded_eth_dev, mac_addr))
651 internals->user_defined_mac = 1;
653 /* Update all slave devices MACs*/
654 if (internals->slave_count > 0)
655 return mac_address_slaves_update(bonded_eth_dev);
661 rte_eth_bond_mac_address_reset(uint16_t bonded_port_id)
663 struct rte_eth_dev *bonded_eth_dev;
664 struct bond_dev_private *internals;
666 if (valid_bonded_port_id(bonded_port_id) != 0)
669 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
670 internals = bonded_eth_dev->data->dev_private;
672 internals->user_defined_mac = 0;
674 if (internals->slave_count > 0) {
675 /* Set MAC Address of Bonded Device */
676 if (mac_address_set(bonded_eth_dev,
677 &internals->slaves[internals->primary_port].persisted_mac_addr)
679 RTE_BOND_LOG(ERR, "Failed to set MAC address on bonded device");
682 /* Update all slave devices MAC addresses */
683 return mac_address_slaves_update(bonded_eth_dev);
685 /* No need to update anything as no slaves present */
690 rte_eth_bond_xmit_policy_set(uint16_t bonded_port_id, uint8_t policy)
692 struct bond_dev_private *internals;
694 if (valid_bonded_port_id(bonded_port_id) != 0)
697 internals = rte_eth_devices[bonded_port_id].data->dev_private;
700 case BALANCE_XMIT_POLICY_LAYER2:
701 internals->balance_xmit_policy = policy;
702 internals->xmit_hash = xmit_l2_hash;
704 case BALANCE_XMIT_POLICY_LAYER23:
705 internals->balance_xmit_policy = policy;
706 internals->xmit_hash = xmit_l23_hash;
708 case BALANCE_XMIT_POLICY_LAYER34:
709 internals->balance_xmit_policy = policy;
710 internals->xmit_hash = xmit_l34_hash;
720 rte_eth_bond_xmit_policy_get(uint16_t bonded_port_id)
722 struct bond_dev_private *internals;
724 if (valid_bonded_port_id(bonded_port_id) != 0)
727 internals = rte_eth_devices[bonded_port_id].data->dev_private;
729 return internals->balance_xmit_policy;
733 rte_eth_bond_link_monitoring_set(uint16_t bonded_port_id, uint32_t internal_ms)
735 struct bond_dev_private *internals;
737 if (valid_bonded_port_id(bonded_port_id) != 0)
740 internals = rte_eth_devices[bonded_port_id].data->dev_private;
741 internals->link_status_polling_interval_ms = internal_ms;
747 rte_eth_bond_link_monitoring_get(uint16_t bonded_port_id)
749 struct bond_dev_private *internals;
751 if (valid_bonded_port_id(bonded_port_id) != 0)
754 internals = rte_eth_devices[bonded_port_id].data->dev_private;
756 return internals->link_status_polling_interval_ms;
760 rte_eth_bond_link_down_prop_delay_set(uint16_t bonded_port_id,
764 struct bond_dev_private *internals;
766 if (valid_bonded_port_id(bonded_port_id) != 0)
769 internals = rte_eth_devices[bonded_port_id].data->dev_private;
770 internals->link_down_delay_ms = delay_ms;
776 rte_eth_bond_link_down_prop_delay_get(uint16_t bonded_port_id)
778 struct bond_dev_private *internals;
780 if (valid_bonded_port_id(bonded_port_id) != 0)
783 internals = rte_eth_devices[bonded_port_id].data->dev_private;
785 return internals->link_down_delay_ms;
789 rte_eth_bond_link_up_prop_delay_set(uint16_t bonded_port_id, uint32_t delay_ms)
792 struct bond_dev_private *internals;
794 if (valid_bonded_port_id(bonded_port_id) != 0)
797 internals = rte_eth_devices[bonded_port_id].data->dev_private;
798 internals->link_up_delay_ms = delay_ms;
804 rte_eth_bond_link_up_prop_delay_get(uint16_t bonded_port_id)
806 struct bond_dev_private *internals;
808 if (valid_bonded_port_id(bonded_port_id) != 0)
811 internals = rte_eth_devices[bonded_port_id].data->dev_private;
813 return internals->link_up_delay_ms;