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 uint16_t vlan_id = pos + i;
246 res = rte_eth_dev_vlan_filter(slave_port_id,
250 found = rte_bitmap_scan(internals->vlan_filter_bmp,
252 } while (found && first != pos && res == 0);
258 __eth_bond_slave_add_lock_free(uint16_t bonded_port_id, uint16_t slave_port_id)
260 struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
261 struct bond_dev_private *internals;
262 struct rte_eth_link link_props;
263 struct rte_eth_dev_info dev_info;
265 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
266 internals = bonded_eth_dev->data->dev_private;
268 if (valid_slave_port_id(slave_port_id, internals->mode) != 0)
271 slave_eth_dev = &rte_eth_devices[slave_port_id];
272 if (slave_eth_dev->data->dev_flags & RTE_ETH_DEV_BONDED_SLAVE) {
273 RTE_BOND_LOG(ERR, "Slave device is already a slave of a bonded device");
277 /* Add slave details to bonded device */
278 slave_eth_dev->data->dev_flags |= RTE_ETH_DEV_BONDED_SLAVE;
280 rte_eth_dev_info_get(slave_port_id, &dev_info);
281 if (dev_info.max_rx_pktlen < internals->max_rx_pktlen) {
282 RTE_BOND_LOG(ERR, "Slave (port %u) max_rx_pktlen too small",
287 slave_add(internals, slave_eth_dev);
289 /* We need to store slaves reta_size to be able to synchronize RETA for all
290 * slave devices even if its sizes are different.
292 internals->slaves[internals->slave_count].reta_size = dev_info.reta_size;
294 if (internals->slave_count < 1) {
295 /* if MAC is not user defined then use MAC of first slave add to
297 if (!internals->user_defined_mac) {
298 if (mac_address_set(bonded_eth_dev,
299 slave_eth_dev->data->mac_addrs)) {
300 RTE_BOND_LOG(ERR, "Failed to set MAC address");
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;
311 internals->current_primary_port = slave_port_id;
313 /* Inherit queues settings from first slave */
314 internals->nb_rx_queues = slave_eth_dev->data->nb_rx_queues;
315 internals->nb_tx_queues = slave_eth_dev->data->nb_tx_queues;
317 internals->reta_size = dev_info.reta_size;
319 /* Take the first dev's offload capabilities */
320 internals->rx_offload_capa = dev_info.rx_offload_capa;
321 internals->tx_offload_capa = dev_info.tx_offload_capa;
322 internals->flow_type_rss_offloads = dev_info.flow_type_rss_offloads;
324 /* Inherit first slave's max rx packet size */
325 internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
328 internals->rx_offload_capa &= dev_info.rx_offload_capa;
329 internals->tx_offload_capa &= dev_info.tx_offload_capa;
330 internals->flow_type_rss_offloads &= dev_info.flow_type_rss_offloads;
332 if (link_properties_valid(bonded_eth_dev,
333 &slave_eth_dev->data->dev_link) != 0) {
334 RTE_BOND_LOG(ERR, "Invalid link properties for slave %d"
335 " in bonding mode %d", slave_port_id,
340 /* RETA size is GCD of all slaves RETA sizes, so, if all sizes will be
341 * the power of 2, the lower one is GCD
343 if (internals->reta_size > dev_info.reta_size)
344 internals->reta_size = dev_info.reta_size;
346 if (!internals->max_rx_pktlen &&
347 dev_info.max_rx_pktlen < internals->candidate_max_rx_pktlen)
348 internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
351 bonded_eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf &=
352 internals->flow_type_rss_offloads;
354 internals->slave_count++;
356 /* Update all slave devices MACs*/
357 mac_address_slaves_update(bonded_eth_dev);
359 if (bonded_eth_dev->data->dev_started) {
360 if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
361 slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
362 RTE_BOND_LOG(ERR, "rte_bond_slaves_configure: port=%d",
368 /* Register link status change callback with bonded device pointer as
370 rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
371 bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
373 /* If bonded device is started then we can add the slave to our active
375 if (bonded_eth_dev->data->dev_started) {
376 rte_eth_link_get_nowait(slave_port_id, &link_props);
378 if (link_props.link_status == ETH_LINK_UP) {
379 if (internals->active_slave_count == 0 &&
380 !internals->user_defined_primary_port)
381 bond_ethdev_primary_set(internals,
384 if (find_slave_by_id(internals->active_slaves,
385 internals->active_slave_count,
386 slave_port_id) == internals->active_slave_count)
387 activate_slave(bonded_eth_dev, slave_port_id);
391 slave_vlan_filter_set(bonded_port_id, slave_port_id);
398 rte_eth_bond_slave_add(uint16_t bonded_port_id, uint16_t slave_port_id)
400 struct rte_eth_dev *bonded_eth_dev;
401 struct bond_dev_private *internals;
405 /* Verify that port id's are valid bonded and slave ports */
406 if (valid_bonded_port_id(bonded_port_id) != 0)
409 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
410 internals = bonded_eth_dev->data->dev_private;
412 rte_spinlock_lock(&internals->lock);
414 retval = __eth_bond_slave_add_lock_free(bonded_port_id, slave_port_id);
416 rte_spinlock_unlock(&internals->lock);
422 __eth_bond_slave_remove_lock_free(uint16_t bonded_port_id,
423 uint16_t slave_port_id)
425 struct rte_eth_dev *bonded_eth_dev;
426 struct bond_dev_private *internals;
427 struct rte_eth_dev *slave_eth_dev;
430 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
431 internals = bonded_eth_dev->data->dev_private;
433 if (valid_slave_port_id(slave_port_id, internals->mode) < 0)
436 /* first remove from active slave list */
437 slave_idx = find_slave_by_id(internals->active_slaves,
438 internals->active_slave_count, slave_port_id);
440 if (slave_idx < internals->active_slave_count)
441 deactivate_slave(bonded_eth_dev, slave_port_id);
444 /* now find in slave list */
445 for (i = 0; i < internals->slave_count; i++)
446 if (internals->slaves[i].port_id == slave_port_id) {
452 RTE_BOND_LOG(ERR, "Couldn't find slave in port list, slave count %d",
453 internals->slave_count);
457 /* Un-register link status change callback with bonded device pointer as
459 rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
460 bond_ethdev_lsc_event_callback,
461 &rte_eth_devices[bonded_port_id].data->port_id);
463 /* Restore original MAC address of slave device */
464 rte_eth_dev_default_mac_addr_set(slave_port_id,
465 &(internals->slaves[slave_idx].persisted_mac_addr));
467 slave_eth_dev = &rte_eth_devices[slave_port_id];
468 slave_remove(internals, slave_eth_dev);
469 slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
471 /* first slave in the active list will be the primary by default,
472 * otherwise use first device in list */
473 if (internals->current_primary_port == slave_port_id) {
474 if (internals->active_slave_count > 0)
475 internals->current_primary_port = internals->active_slaves[0];
476 else if (internals->slave_count > 0)
477 internals->current_primary_port = internals->slaves[0].port_id;
479 internals->primary_port = 0;
482 if (internals->active_slave_count < 1) {
483 /* if no slaves are any longer attached to bonded device and MAC is not
484 * user defined then clear MAC of bonded device as it will be reset
485 * when a new slave is added */
486 if (internals->slave_count < 1 && !internals->user_defined_mac)
487 memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
488 sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
490 if (internals->slave_count == 0) {
491 internals->rx_offload_capa = 0;
492 internals->tx_offload_capa = 0;
493 internals->flow_type_rss_offloads = ETH_RSS_PROTO_MASK;
494 internals->reta_size = 0;
495 internals->candidate_max_rx_pktlen = 0;
496 internals->max_rx_pktlen = 0;
502 rte_eth_bond_slave_remove(uint16_t bonded_port_id, uint16_t slave_port_id)
504 struct rte_eth_dev *bonded_eth_dev;
505 struct bond_dev_private *internals;
508 if (valid_bonded_port_id(bonded_port_id) != 0)
511 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
512 internals = bonded_eth_dev->data->dev_private;
514 rte_spinlock_lock(&internals->lock);
516 retval = __eth_bond_slave_remove_lock_free(bonded_port_id, slave_port_id);
518 rte_spinlock_unlock(&internals->lock);
524 rte_eth_bond_mode_set(uint16_t bonded_port_id, uint8_t mode)
526 struct rte_eth_dev *bonded_eth_dev;
528 if (valid_bonded_port_id(bonded_port_id) != 0)
531 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
533 if (check_for_master_bonded_ethdev(bonded_eth_dev) != 0 &&
534 mode == BONDING_MODE_8023AD)
537 return bond_ethdev_mode_set(bonded_eth_dev, mode);
541 rte_eth_bond_mode_get(uint16_t bonded_port_id)
543 struct bond_dev_private *internals;
545 if (valid_bonded_port_id(bonded_port_id) != 0)
548 internals = rte_eth_devices[bonded_port_id].data->dev_private;
550 return internals->mode;
554 rte_eth_bond_primary_set(uint16_t bonded_port_id, uint16_t slave_port_id)
556 struct bond_dev_private *internals;
558 if (valid_bonded_port_id(bonded_port_id) != 0)
561 internals = rte_eth_devices[bonded_port_id].data->dev_private;
563 if (valid_slave_port_id(slave_port_id, internals->mode) != 0)
566 internals->user_defined_primary_port = 1;
567 internals->primary_port = slave_port_id;
569 bond_ethdev_primary_set(internals, slave_port_id);
575 rte_eth_bond_primary_get(uint16_t bonded_port_id)
577 struct bond_dev_private *internals;
579 if (valid_bonded_port_id(bonded_port_id) != 0)
582 internals = rte_eth_devices[bonded_port_id].data->dev_private;
584 if (internals->slave_count < 1)
587 return internals->current_primary_port;
591 rte_eth_bond_slaves_get(uint16_t bonded_port_id, uint16_t slaves[],
594 struct bond_dev_private *internals;
597 if (valid_bonded_port_id(bonded_port_id) != 0)
603 internals = rte_eth_devices[bonded_port_id].data->dev_private;
605 if (internals->slave_count > len)
608 for (i = 0; i < internals->slave_count; i++)
609 slaves[i] = internals->slaves[i].port_id;
611 return internals->slave_count;
615 rte_eth_bond_active_slaves_get(uint16_t bonded_port_id, uint16_t slaves[],
618 struct bond_dev_private *internals;
620 if (valid_bonded_port_id(bonded_port_id) != 0)
626 internals = rte_eth_devices[bonded_port_id].data->dev_private;
628 if (internals->active_slave_count > len)
631 memcpy(slaves, internals->active_slaves,
632 internals->active_slave_count * sizeof(internals->active_slaves[0]));
634 return internals->active_slave_count;
638 rte_eth_bond_mac_address_set(uint16_t bonded_port_id,
639 struct ether_addr *mac_addr)
641 struct rte_eth_dev *bonded_eth_dev;
642 struct bond_dev_private *internals;
644 if (valid_bonded_port_id(bonded_port_id) != 0)
647 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
648 internals = bonded_eth_dev->data->dev_private;
650 /* Set MAC Address of Bonded Device */
651 if (mac_address_set(bonded_eth_dev, mac_addr))
654 internals->user_defined_mac = 1;
656 /* Update all slave devices MACs*/
657 if (internals->slave_count > 0)
658 return mac_address_slaves_update(bonded_eth_dev);
664 rte_eth_bond_mac_address_reset(uint16_t bonded_port_id)
666 struct rte_eth_dev *bonded_eth_dev;
667 struct bond_dev_private *internals;
669 if (valid_bonded_port_id(bonded_port_id) != 0)
672 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
673 internals = bonded_eth_dev->data->dev_private;
675 internals->user_defined_mac = 0;
677 if (internals->slave_count > 0) {
679 /* Get the primary slave location based on the primary port
680 * number as, while slave_add(), we will keep the primary
681 * slave based on slave_count,but not based on the primary port.
683 for (slave_port = 0; slave_port < internals->slave_count;
685 if (internals->slaves[slave_port].port_id ==
686 internals->primary_port)
690 /* Set MAC Address of Bonded Device */
691 if (mac_address_set(bonded_eth_dev,
692 &internals->slaves[slave_port].persisted_mac_addr)
694 RTE_BOND_LOG(ERR, "Failed to set MAC address on bonded device");
697 /* Update all slave devices MAC addresses */
698 return mac_address_slaves_update(bonded_eth_dev);
700 /* No need to update anything as no slaves present */
705 rte_eth_bond_xmit_policy_set(uint16_t bonded_port_id, uint8_t policy)
707 struct bond_dev_private *internals;
709 if (valid_bonded_port_id(bonded_port_id) != 0)
712 internals = rte_eth_devices[bonded_port_id].data->dev_private;
715 case BALANCE_XMIT_POLICY_LAYER2:
716 internals->balance_xmit_policy = policy;
717 internals->xmit_hash = xmit_l2_hash;
719 case BALANCE_XMIT_POLICY_LAYER23:
720 internals->balance_xmit_policy = policy;
721 internals->xmit_hash = xmit_l23_hash;
723 case BALANCE_XMIT_POLICY_LAYER34:
724 internals->balance_xmit_policy = policy;
725 internals->xmit_hash = xmit_l34_hash;
735 rte_eth_bond_xmit_policy_get(uint16_t bonded_port_id)
737 struct bond_dev_private *internals;
739 if (valid_bonded_port_id(bonded_port_id) != 0)
742 internals = rte_eth_devices[bonded_port_id].data->dev_private;
744 return internals->balance_xmit_policy;
748 rte_eth_bond_link_monitoring_set(uint16_t bonded_port_id, uint32_t internal_ms)
750 struct bond_dev_private *internals;
752 if (valid_bonded_port_id(bonded_port_id) != 0)
755 internals = rte_eth_devices[bonded_port_id].data->dev_private;
756 internals->link_status_polling_interval_ms = internal_ms;
762 rte_eth_bond_link_monitoring_get(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;
771 return internals->link_status_polling_interval_ms;
775 rte_eth_bond_link_down_prop_delay_set(uint16_t bonded_port_id,
779 struct bond_dev_private *internals;
781 if (valid_bonded_port_id(bonded_port_id) != 0)
784 internals = rte_eth_devices[bonded_port_id].data->dev_private;
785 internals->link_down_delay_ms = delay_ms;
791 rte_eth_bond_link_down_prop_delay_get(uint16_t bonded_port_id)
793 struct bond_dev_private *internals;
795 if (valid_bonded_port_id(bonded_port_id) != 0)
798 internals = rte_eth_devices[bonded_port_id].data->dev_private;
800 return internals->link_down_delay_ms;
804 rte_eth_bond_link_up_prop_delay_set(uint16_t bonded_port_id, uint32_t delay_ms)
807 struct bond_dev_private *internals;
809 if (valid_bonded_port_id(bonded_port_id) != 0)
812 internals = rte_eth_devices[bonded_port_id].data->dev_private;
813 internals->link_up_delay_ms = delay_ms;
819 rte_eth_bond_link_up_prop_delay_get(uint16_t bonded_port_id)
821 struct bond_dev_private *internals;
823 if (valid_bonded_port_id(bonded_port_id) != 0)
826 internals = rte_eth_devices[bonded_port_id].data->dev_private;
828 return internals->link_up_delay_ms;