1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
8 #include <rte_malloc.h>
9 #include <rte_ethdev.h>
11 #include <rte_bus_vdev.h>
12 #include <rte_kvargs.h>
14 #include "rte_eth_bond.h"
15 #include "rte_eth_bond_private.h"
16 #include "rte_eth_bond_8023ad_private.h"
19 check_for_bonded_ethdev(const struct rte_eth_dev *eth_dev)
21 /* Check valid pointer */
22 if (eth_dev->device->driver->name == NULL)
25 /* return 0 if driver name matches */
26 return eth_dev->device->driver->name != pmd_bond_drv.driver.name;
30 valid_bonded_port_id(uint16_t port_id)
32 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
33 return check_for_bonded_ethdev(&rte_eth_devices[port_id]);
37 check_for_master_bonded_ethdev(const struct rte_eth_dev *eth_dev)
40 struct bond_dev_private *internals;
42 if (check_for_bonded_ethdev(eth_dev) != 0)
45 internals = eth_dev->data->dev_private;
47 /* Check if any of slave devices is a bonded device */
48 for (i = 0; i < internals->slave_count; i++)
49 if (valid_bonded_port_id(internals->slaves[i].port_id) == 0)
56 valid_slave_port_id(uint16_t port_id, uint8_t mode)
58 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
60 /* Verify that port_id refers to a non bonded port */
61 if (check_for_bonded_ethdev(&rte_eth_devices[port_id]) == 0 &&
62 mode == BONDING_MODE_8023AD) {
63 RTE_BOND_LOG(ERR, "Cannot add slave to bonded device in 802.3ad"
64 " mode as slave is also a bonded device, only "
65 "physical devices can be support in this mode.");
73 activate_slave(struct rte_eth_dev *eth_dev, uint16_t port_id)
75 struct bond_dev_private *internals = eth_dev->data->dev_private;
76 uint8_t active_count = internals->active_slave_count;
78 if (internals->mode == BONDING_MODE_8023AD)
79 bond_mode_8023ad_activate_slave(eth_dev, port_id);
81 if (internals->mode == BONDING_MODE_TLB
82 || internals->mode == BONDING_MODE_ALB) {
84 internals->tlb_slaves_order[active_count] = port_id;
87 RTE_ASSERT(internals->active_slave_count <
88 (RTE_DIM(internals->active_slaves) - 1));
90 internals->active_slaves[internals->active_slave_count] = port_id;
91 internals->active_slave_count++;
93 if (internals->mode == BONDING_MODE_TLB)
94 bond_tlb_activate_slave(internals);
95 if (internals->mode == BONDING_MODE_ALB)
96 bond_mode_alb_client_list_upd(eth_dev);
100 deactivate_slave(struct rte_eth_dev *eth_dev, uint16_t port_id)
103 struct bond_dev_private *internals = eth_dev->data->dev_private;
104 uint16_t active_count = internals->active_slave_count;
106 if (internals->mode == BONDING_MODE_8023AD) {
107 bond_mode_8023ad_stop(eth_dev);
108 bond_mode_8023ad_deactivate_slave(eth_dev, port_id);
109 } else if (internals->mode == BONDING_MODE_TLB
110 || internals->mode == BONDING_MODE_ALB)
111 bond_tlb_disable(internals);
113 slave_pos = find_slave_by_id(internals->active_slaves, active_count,
116 /* If slave was not at the end of the list
117 * shift active slaves up active array list */
118 if (slave_pos < active_count) {
120 memmove(internals->active_slaves + slave_pos,
121 internals->active_slaves + slave_pos + 1,
122 (active_count - slave_pos) *
123 sizeof(internals->active_slaves[0]));
126 RTE_ASSERT(active_count < RTE_DIM(internals->active_slaves));
127 internals->active_slave_count = active_count;
129 if (eth_dev->data->dev_started) {
130 if (internals->mode == BONDING_MODE_8023AD) {
131 bond_mode_8023ad_start(eth_dev);
132 } else if (internals->mode == BONDING_MODE_TLB) {
133 bond_tlb_enable(internals);
134 } else if (internals->mode == BONDING_MODE_ALB) {
135 bond_tlb_enable(internals);
136 bond_mode_alb_client_list_upd(eth_dev);
142 rte_eth_bond_create(const char *name, uint8_t mode, uint8_t socket_id)
144 struct bond_dev_private *internals;
150 RTE_BOND_LOG(ERR, "Invalid name specified");
154 ret = snprintf(devargs, sizeof(devargs),
155 "driver=net_bonding,mode=%d,socket_id=%d", mode, socket_id);
156 if (ret < 0 || ret >= (int)sizeof(devargs))
159 ret = rte_vdev_init(name, devargs);
163 ret = rte_eth_dev_get_port_by_name(name, &port_id);
167 * To make bond_ethdev_configure() happy we need to free the
168 * internals->kvlist here.
170 * Also see comment in bond_ethdev_configure().
172 internals = rte_eth_devices[port_id].data->dev_private;
173 rte_kvargs_free(internals->kvlist);
174 internals->kvlist = NULL;
180 rte_eth_bond_free(const char *name)
182 return rte_vdev_uninit(name);
186 slave_vlan_filter_set(uint16_t bonded_port_id, uint16_t slave_port_id)
188 struct rte_eth_dev *bonded_eth_dev;
189 struct bond_dev_private *internals;
196 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
197 if (bonded_eth_dev->data->dev_conf.rxmode.hw_vlan_filter == 0)
200 internals = bonded_eth_dev->data->dev_private;
201 found = rte_bitmap_scan(internals->vlan_filter_bmp, &pos, &slab);
211 for (i = 0, mask = 1;
212 i < RTE_BITMAP_SLAB_BIT_SIZE;
214 if (unlikely(slab & mask))
215 res = rte_eth_dev_vlan_filter(slave_port_id,
218 found = rte_bitmap_scan(internals->vlan_filter_bmp,
220 } while (found && first != pos && res == 0);
226 __eth_bond_slave_add_lock_free(uint16_t bonded_port_id, uint16_t slave_port_id)
228 struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
229 struct bond_dev_private *internals;
230 struct rte_eth_link link_props;
231 struct rte_eth_dev_info dev_info;
233 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
234 internals = bonded_eth_dev->data->dev_private;
236 if (valid_slave_port_id(slave_port_id, internals->mode) != 0)
239 slave_eth_dev = &rte_eth_devices[slave_port_id];
240 if (slave_eth_dev->data->dev_flags & RTE_ETH_DEV_BONDED_SLAVE) {
241 RTE_BOND_LOG(ERR, "Slave device is already a slave of a bonded device");
245 /* Add slave details to bonded device */
246 slave_eth_dev->data->dev_flags |= RTE_ETH_DEV_BONDED_SLAVE;
248 rte_eth_dev_info_get(slave_port_id, &dev_info);
249 if (dev_info.max_rx_pktlen < internals->max_rx_pktlen) {
250 RTE_BOND_LOG(ERR, "Slave (port %u) max_rx_pktlen too small",
255 slave_add(internals, slave_eth_dev);
257 /* We need to store slaves reta_size to be able to synchronize RETA for all
258 * slave devices even if its sizes are different.
260 internals->slaves[internals->slave_count].reta_size = dev_info.reta_size;
262 if (internals->slave_count < 1) {
263 /* if MAC is not user defined then use MAC of first slave add to
265 if (!internals->user_defined_mac)
266 mac_address_set(bonded_eth_dev, slave_eth_dev->data->mac_addrs);
268 /* Inherit eth dev link properties from first slave */
269 link_properties_set(bonded_eth_dev,
270 &(slave_eth_dev->data->dev_link));
272 /* Make primary slave */
273 internals->primary_port = slave_port_id;
274 internals->current_primary_port = slave_port_id;
276 /* Inherit queues settings from first slave */
277 internals->nb_rx_queues = slave_eth_dev->data->nb_rx_queues;
278 internals->nb_tx_queues = slave_eth_dev->data->nb_tx_queues;
280 internals->reta_size = dev_info.reta_size;
282 /* Take the first dev's offload capabilities */
283 internals->rx_offload_capa = dev_info.rx_offload_capa;
284 internals->tx_offload_capa = dev_info.tx_offload_capa;
285 internals->flow_type_rss_offloads = dev_info.flow_type_rss_offloads;
287 /* Inherit first slave's max rx packet size */
288 internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
291 internals->rx_offload_capa &= dev_info.rx_offload_capa;
292 internals->tx_offload_capa &= dev_info.tx_offload_capa;
293 internals->flow_type_rss_offloads &= dev_info.flow_type_rss_offloads;
295 if (link_properties_valid(bonded_eth_dev,
296 &slave_eth_dev->data->dev_link) != 0) {
297 RTE_BOND_LOG(ERR, "Invalid link properties for slave %d"
298 " in bonding mode %d", slave_port_id,
303 /* RETA size is GCD of all slaves RETA sizes, so, if all sizes will be
304 * the power of 2, the lower one is GCD
306 if (internals->reta_size > dev_info.reta_size)
307 internals->reta_size = dev_info.reta_size;
309 if (!internals->max_rx_pktlen &&
310 dev_info.max_rx_pktlen < internals->candidate_max_rx_pktlen)
311 internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
314 bonded_eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf &=
315 internals->flow_type_rss_offloads;
317 internals->slave_count++;
319 /* Update all slave devices MACs*/
320 mac_address_slaves_update(bonded_eth_dev);
322 if (bonded_eth_dev->data->dev_started) {
323 if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
324 slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
325 RTE_BOND_LOG(ERR, "rte_bond_slaves_configure: port=%d",
331 /* Register link status change callback with bonded device pointer as
333 rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
334 bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
336 /* If bonded device is started then we can add the slave to our active
338 if (bonded_eth_dev->data->dev_started) {
339 rte_eth_link_get_nowait(slave_port_id, &link_props);
341 if (link_props.link_status == ETH_LINK_UP) {
342 if (internals->active_slave_count == 0 &&
343 !internals->user_defined_primary_port)
344 bond_ethdev_primary_set(internals,
347 if (find_slave_by_id(internals->active_slaves,
348 internals->active_slave_count,
349 slave_port_id) == internals->active_slave_count)
350 activate_slave(bonded_eth_dev, slave_port_id);
354 slave_vlan_filter_set(bonded_port_id, slave_port_id);
361 rte_eth_bond_slave_add(uint16_t bonded_port_id, uint16_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);
385 __eth_bond_slave_remove_lock_free(uint16_t bonded_port_id,
386 uint16_t slave_port_id)
388 struct rte_eth_dev *bonded_eth_dev;
389 struct bond_dev_private *internals;
390 struct rte_eth_dev *slave_eth_dev;
393 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
394 internals = bonded_eth_dev->data->dev_private;
396 if (valid_slave_port_id(slave_port_id, internals->mode) < 0)
399 /* first remove from active slave list */
400 slave_idx = find_slave_by_id(internals->active_slaves,
401 internals->active_slave_count, slave_port_id);
403 if (slave_idx < internals->active_slave_count)
404 deactivate_slave(bonded_eth_dev, slave_port_id);
407 /* now find in slave list */
408 for (i = 0; i < internals->slave_count; i++)
409 if (internals->slaves[i].port_id == slave_port_id) {
415 RTE_BOND_LOG(ERR, "Couldn't find slave in port list, slave count %d",
416 internals->slave_count);
420 /* Un-register link status change callback with bonded device pointer as
422 rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
423 bond_ethdev_lsc_event_callback,
424 &rte_eth_devices[bonded_port_id].data->port_id);
426 /* Restore original MAC address of slave device */
427 rte_eth_dev_default_mac_addr_set(slave_port_id,
428 &(internals->slaves[slave_idx].persisted_mac_addr));
430 slave_eth_dev = &rte_eth_devices[slave_port_id];
431 slave_remove(internals, slave_eth_dev);
432 slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
434 /* first slave in the active list will be the primary by default,
435 * otherwise use first device in list */
436 if (internals->current_primary_port == slave_port_id) {
437 if (internals->active_slave_count > 0)
438 internals->current_primary_port = internals->active_slaves[0];
439 else if (internals->slave_count > 0)
440 internals->current_primary_port = internals->slaves[0].port_id;
442 internals->primary_port = 0;
445 if (internals->active_slave_count < 1) {
446 /* if no slaves are any longer attached to bonded device and MAC is not
447 * user defined then clear MAC of bonded device as it will be reset
448 * when a new slave is added */
449 if (internals->slave_count < 1 && !internals->user_defined_mac)
450 memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
451 sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
453 if (internals->slave_count == 0) {
454 internals->rx_offload_capa = 0;
455 internals->tx_offload_capa = 0;
456 internals->flow_type_rss_offloads = ETH_RSS_PROTO_MASK;
457 internals->reta_size = 0;
458 internals->candidate_max_rx_pktlen = 0;
459 internals->max_rx_pktlen = 0;
465 rte_eth_bond_slave_remove(uint16_t bonded_port_id, uint16_t slave_port_id)
467 struct rte_eth_dev *bonded_eth_dev;
468 struct bond_dev_private *internals;
471 if (valid_bonded_port_id(bonded_port_id) != 0)
474 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
475 internals = bonded_eth_dev->data->dev_private;
477 rte_spinlock_lock(&internals->lock);
479 retval = __eth_bond_slave_remove_lock_free(bonded_port_id, slave_port_id);
481 rte_spinlock_unlock(&internals->lock);
487 rte_eth_bond_mode_set(uint16_t bonded_port_id, uint8_t mode)
489 struct rte_eth_dev *bonded_eth_dev;
491 if (valid_bonded_port_id(bonded_port_id) != 0)
494 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
496 if (check_for_master_bonded_ethdev(bonded_eth_dev) != 0 &&
497 mode == BONDING_MODE_8023AD)
500 return bond_ethdev_mode_set(bonded_eth_dev, mode);
504 rte_eth_bond_mode_get(uint16_t bonded_port_id)
506 struct bond_dev_private *internals;
508 if (valid_bonded_port_id(bonded_port_id) != 0)
511 internals = rte_eth_devices[bonded_port_id].data->dev_private;
513 return internals->mode;
517 rte_eth_bond_primary_set(uint16_t bonded_port_id, uint16_t slave_port_id)
519 struct bond_dev_private *internals;
521 if (valid_bonded_port_id(bonded_port_id) != 0)
524 internals = rte_eth_devices[bonded_port_id].data->dev_private;
526 if (valid_slave_port_id(slave_port_id, internals->mode) != 0)
529 internals->user_defined_primary_port = 1;
530 internals->primary_port = slave_port_id;
532 bond_ethdev_primary_set(internals, slave_port_id);
538 rte_eth_bond_primary_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 if (internals->slave_count < 1)
550 return internals->current_primary_port;
554 rte_eth_bond_slaves_get(uint16_t bonded_port_id, uint16_t slaves[],
557 struct bond_dev_private *internals;
560 if (valid_bonded_port_id(bonded_port_id) != 0)
566 internals = rte_eth_devices[bonded_port_id].data->dev_private;
568 if (internals->slave_count > len)
571 for (i = 0; i < internals->slave_count; i++)
572 slaves[i] = internals->slaves[i].port_id;
574 return internals->slave_count;
578 rte_eth_bond_active_slaves_get(uint16_t bonded_port_id, uint16_t slaves[],
581 struct bond_dev_private *internals;
583 if (valid_bonded_port_id(bonded_port_id) != 0)
589 internals = rte_eth_devices[bonded_port_id].data->dev_private;
591 if (internals->active_slave_count > len)
594 memcpy(slaves, internals->active_slaves,
595 internals->active_slave_count * sizeof(internals->active_slaves[0]));
597 return internals->active_slave_count;
601 rte_eth_bond_mac_address_set(uint16_t bonded_port_id,
602 struct ether_addr *mac_addr)
604 struct rte_eth_dev *bonded_eth_dev;
605 struct bond_dev_private *internals;
607 if (valid_bonded_port_id(bonded_port_id) != 0)
610 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
611 internals = bonded_eth_dev->data->dev_private;
613 /* Set MAC Address of Bonded Device */
614 if (mac_address_set(bonded_eth_dev, mac_addr))
617 internals->user_defined_mac = 1;
619 /* Update all slave devices MACs*/
620 if (internals->slave_count > 0)
621 return mac_address_slaves_update(bonded_eth_dev);
627 rte_eth_bond_mac_address_reset(uint16_t bonded_port_id)
629 struct rte_eth_dev *bonded_eth_dev;
630 struct bond_dev_private *internals;
632 if (valid_bonded_port_id(bonded_port_id) != 0)
635 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
636 internals = bonded_eth_dev->data->dev_private;
638 internals->user_defined_mac = 0;
640 if (internals->slave_count > 0) {
641 /* Set MAC Address of Bonded Device */
642 if (mac_address_set(bonded_eth_dev,
643 &internals->slaves[internals->primary_port].persisted_mac_addr)
645 RTE_BOND_LOG(ERR, "Failed to set MAC address on bonded device");
648 /* Update all slave devices MAC addresses */
649 return mac_address_slaves_update(bonded_eth_dev);
651 /* No need to update anything as no slaves present */
656 rte_eth_bond_xmit_policy_set(uint16_t bonded_port_id, uint8_t policy)
658 struct bond_dev_private *internals;
660 if (valid_bonded_port_id(bonded_port_id) != 0)
663 internals = rte_eth_devices[bonded_port_id].data->dev_private;
666 case BALANCE_XMIT_POLICY_LAYER2:
667 internals->balance_xmit_policy = policy;
668 internals->xmit_hash = xmit_l2_hash;
670 case BALANCE_XMIT_POLICY_LAYER23:
671 internals->balance_xmit_policy = policy;
672 internals->xmit_hash = xmit_l23_hash;
674 case BALANCE_XMIT_POLICY_LAYER34:
675 internals->balance_xmit_policy = policy;
676 internals->xmit_hash = xmit_l34_hash;
686 rte_eth_bond_xmit_policy_get(uint16_t bonded_port_id)
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;
695 return internals->balance_xmit_policy;
699 rte_eth_bond_link_monitoring_set(uint16_t bonded_port_id, uint32_t internal_ms)
701 struct bond_dev_private *internals;
703 if (valid_bonded_port_id(bonded_port_id) != 0)
706 internals = rte_eth_devices[bonded_port_id].data->dev_private;
707 internals->link_status_polling_interval_ms = internal_ms;
713 rte_eth_bond_link_monitoring_get(uint16_t bonded_port_id)
715 struct bond_dev_private *internals;
717 if (valid_bonded_port_id(bonded_port_id) != 0)
720 internals = rte_eth_devices[bonded_port_id].data->dev_private;
722 return internals->link_status_polling_interval_ms;
726 rte_eth_bond_link_down_prop_delay_set(uint16_t bonded_port_id,
730 struct bond_dev_private *internals;
732 if (valid_bonded_port_id(bonded_port_id) != 0)
735 internals = rte_eth_devices[bonded_port_id].data->dev_private;
736 internals->link_down_delay_ms = delay_ms;
742 rte_eth_bond_link_down_prop_delay_get(uint16_t bonded_port_id)
744 struct bond_dev_private *internals;
746 if (valid_bonded_port_id(bonded_port_id) != 0)
749 internals = rte_eth_devices[bonded_port_id].data->dev_private;
751 return internals->link_down_delay_ms;
755 rte_eth_bond_link_up_prop_delay_set(uint16_t bonded_port_id, uint32_t delay_ms)
758 struct bond_dev_private *internals;
760 if (valid_bonded_port_id(bonded_port_id) != 0)
763 internals = rte_eth_devices[bonded_port_id].data->dev_private;
764 internals->link_up_delay_ms = delay_ms;
770 rte_eth_bond_link_up_prop_delay_get(uint16_t bonded_port_id)
772 struct bond_dev_private *internals;
774 if (valid_bonded_port_id(bonded_port_id) != 0)
777 internals = rte_eth_devices[bonded_port_id].data->dev_private;
779 return internals->link_up_delay_ms;