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>
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 valid_slave_port_id(uint16_t port_id, uint8_t mode)
68 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
70 /* Verify that port_id refers to a non bonded port */
71 if (check_for_bonded_ethdev(&rte_eth_devices[port_id]) == 0 &&
72 mode == BONDING_MODE_8023AD) {
73 RTE_BOND_LOG(ERR, "Cannot add slave to bonded device in 802.3ad"
74 " mode as slave is also a bonded device, only "
75 "physical devices can be support in this mode.");
83 activate_slave(struct rte_eth_dev *eth_dev, uint16_t port_id)
85 struct bond_dev_private *internals = eth_dev->data->dev_private;
86 uint8_t active_count = internals->active_slave_count;
88 if (internals->mode == BONDING_MODE_8023AD)
89 bond_mode_8023ad_activate_slave(eth_dev, port_id);
91 if (internals->mode == BONDING_MODE_TLB
92 || internals->mode == BONDING_MODE_ALB) {
94 internals->tlb_slaves_order[active_count] = port_id;
97 RTE_ASSERT(internals->active_slave_count <
98 (RTE_DIM(internals->active_slaves) - 1));
100 internals->active_slaves[internals->active_slave_count] = port_id;
101 internals->active_slave_count++;
103 if (internals->mode == BONDING_MODE_TLB)
104 bond_tlb_activate_slave(internals);
105 if (internals->mode == BONDING_MODE_ALB)
106 bond_mode_alb_client_list_upd(eth_dev);
110 deactivate_slave(struct rte_eth_dev *eth_dev, uint16_t port_id)
113 struct bond_dev_private *internals = eth_dev->data->dev_private;
114 uint16_t active_count = internals->active_slave_count;
116 if (internals->mode == BONDING_MODE_8023AD) {
117 bond_mode_8023ad_stop(eth_dev);
118 bond_mode_8023ad_deactivate_slave(eth_dev, port_id);
119 } else if (internals->mode == BONDING_MODE_TLB
120 || internals->mode == BONDING_MODE_ALB)
121 bond_tlb_disable(internals);
123 slave_pos = find_slave_by_id(internals->active_slaves, active_count,
126 /* If slave was not at the end of the list
127 * shift active slaves up active array list */
128 if (slave_pos < active_count) {
130 memmove(internals->active_slaves + slave_pos,
131 internals->active_slaves + slave_pos + 1,
132 (active_count - slave_pos) *
133 sizeof(internals->active_slaves[0]));
136 RTE_ASSERT(active_count < RTE_DIM(internals->active_slaves));
137 internals->active_slave_count = active_count;
139 if (eth_dev->data->dev_started) {
140 if (internals->mode == BONDING_MODE_8023AD) {
141 bond_mode_8023ad_start(eth_dev);
142 } else if (internals->mode == BONDING_MODE_TLB) {
143 bond_tlb_enable(internals);
144 } else if (internals->mode == BONDING_MODE_ALB) {
145 bond_tlb_enable(internals);
146 bond_mode_alb_client_list_upd(eth_dev);
152 rte_eth_bond_create(const char *name, uint8_t mode, uint8_t socket_id)
154 struct bond_dev_private *internals;
160 RTE_BOND_LOG(ERR, "Invalid name specified");
164 ret = snprintf(devargs, sizeof(devargs),
165 "driver=net_bonding,mode=%d,socket_id=%d", mode, socket_id);
166 if (ret < 0 || ret >= (int)sizeof(devargs))
169 ret = rte_vdev_init(name, devargs);
173 ret = rte_eth_dev_get_port_by_name(name, &port_id);
177 * To make bond_ethdev_configure() happy we need to free the
178 * internals->kvlist here.
180 * Also see comment in bond_ethdev_configure().
182 internals = rte_eth_devices[port_id].data->dev_private;
183 rte_kvargs_free(internals->kvlist);
184 internals->kvlist = NULL;
190 rte_eth_bond_free(const char *name)
192 return rte_vdev_uninit(name);
196 slave_vlan_filter_set(uint16_t bonded_port_id, uint16_t slave_port_id)
198 struct rte_eth_dev *bonded_eth_dev;
199 struct bond_dev_private *internals;
206 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
207 if (bonded_eth_dev->data->dev_conf.rxmode.hw_vlan_filter == 0)
210 internals = bonded_eth_dev->data->dev_private;
211 found = rte_bitmap_scan(internals->vlan_filter_bmp, &pos, &slab);
221 for (i = 0, mask = 1;
222 i < RTE_BITMAP_SLAB_BIT_SIZE;
224 if (unlikely(slab & mask))
225 res = rte_eth_dev_vlan_filter(slave_port_id,
228 found = rte_bitmap_scan(internals->vlan_filter_bmp,
230 } while (found && first != pos && res == 0);
236 __eth_bond_slave_add_lock_free(uint16_t bonded_port_id, uint16_t slave_port_id)
238 struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
239 struct bond_dev_private *internals;
240 struct rte_eth_link link_props;
241 struct rte_eth_dev_info dev_info;
243 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
244 internals = bonded_eth_dev->data->dev_private;
246 if (valid_slave_port_id(slave_port_id, internals->mode) != 0)
249 slave_eth_dev = &rte_eth_devices[slave_port_id];
250 if (slave_eth_dev->data->dev_flags & RTE_ETH_DEV_BONDED_SLAVE) {
251 RTE_BOND_LOG(ERR, "Slave device is already a slave of a bonded device");
255 /* Add slave details to bonded device */
256 slave_eth_dev->data->dev_flags |= RTE_ETH_DEV_BONDED_SLAVE;
258 rte_eth_dev_info_get(slave_port_id, &dev_info);
259 if (dev_info.max_rx_pktlen < internals->max_rx_pktlen) {
260 RTE_BOND_LOG(ERR, "Slave (port %u) max_rx_pktlen too small",
265 slave_add(internals, slave_eth_dev);
267 /* We need to store slaves reta_size to be able to synchronize RETA for all
268 * slave devices even if its sizes are different.
270 internals->slaves[internals->slave_count].reta_size = dev_info.reta_size;
272 if (internals->slave_count < 1) {
273 /* if MAC is not user defined then use MAC of first slave add to
275 if (!internals->user_defined_mac)
276 mac_address_set(bonded_eth_dev, slave_eth_dev->data->mac_addrs);
278 /* Inherit eth dev link properties from first slave */
279 link_properties_set(bonded_eth_dev,
280 &(slave_eth_dev->data->dev_link));
282 /* Make primary slave */
283 internals->primary_port = slave_port_id;
284 internals->current_primary_port = slave_port_id;
286 /* Inherit queues settings from first slave */
287 internals->nb_rx_queues = slave_eth_dev->data->nb_rx_queues;
288 internals->nb_tx_queues = slave_eth_dev->data->nb_tx_queues;
290 internals->reta_size = dev_info.reta_size;
292 /* Take the first dev's offload capabilities */
293 internals->rx_offload_capa = dev_info.rx_offload_capa;
294 internals->tx_offload_capa = dev_info.tx_offload_capa;
295 internals->flow_type_rss_offloads = dev_info.flow_type_rss_offloads;
297 /* Inherit first slave's max rx packet size */
298 internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
301 internals->rx_offload_capa &= dev_info.rx_offload_capa;
302 internals->tx_offload_capa &= dev_info.tx_offload_capa;
303 internals->flow_type_rss_offloads &= dev_info.flow_type_rss_offloads;
305 if (link_properties_valid(bonded_eth_dev,
306 &slave_eth_dev->data->dev_link) != 0) {
307 RTE_BOND_LOG(ERR, "Invalid link properties for slave %d"
308 " in bonding mode %d", slave_port_id,
313 /* RETA size is GCD of all slaves RETA sizes, so, if all sizes will be
314 * the power of 2, the lower one is GCD
316 if (internals->reta_size > dev_info.reta_size)
317 internals->reta_size = dev_info.reta_size;
319 if (!internals->max_rx_pktlen &&
320 dev_info.max_rx_pktlen < internals->candidate_max_rx_pktlen)
321 internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
324 bonded_eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf &=
325 internals->flow_type_rss_offloads;
327 internals->slave_count++;
329 /* Update all slave devices MACs*/
330 mac_address_slaves_update(bonded_eth_dev);
332 if (bonded_eth_dev->data->dev_started) {
333 if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
334 slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
335 RTE_BOND_LOG(ERR, "rte_bond_slaves_configure: port=%d",
341 /* Register link status change callback with bonded device pointer as
343 rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
344 bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
346 /* If bonded device is started then we can add the slave to our active
348 if (bonded_eth_dev->data->dev_started) {
349 rte_eth_link_get_nowait(slave_port_id, &link_props);
351 if (link_props.link_status == ETH_LINK_UP) {
352 if (internals->active_slave_count == 0 &&
353 !internals->user_defined_primary_port)
354 bond_ethdev_primary_set(internals,
357 if (find_slave_by_id(internals->active_slaves,
358 internals->active_slave_count,
359 slave_port_id) == internals->active_slave_count)
360 activate_slave(bonded_eth_dev, slave_port_id);
364 slave_vlan_filter_set(bonded_port_id, slave_port_id);
371 rte_eth_bond_slave_add(uint16_t bonded_port_id, uint16_t slave_port_id)
373 struct rte_eth_dev *bonded_eth_dev;
374 struct bond_dev_private *internals;
378 /* Verify that port id's are valid bonded and slave ports */
379 if (valid_bonded_port_id(bonded_port_id) != 0)
382 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
383 internals = bonded_eth_dev->data->dev_private;
385 rte_spinlock_lock(&internals->lock);
387 retval = __eth_bond_slave_add_lock_free(bonded_port_id, slave_port_id);
389 rte_spinlock_unlock(&internals->lock);
395 __eth_bond_slave_remove_lock_free(uint16_t bonded_port_id,
396 uint16_t slave_port_id)
398 struct rte_eth_dev *bonded_eth_dev;
399 struct bond_dev_private *internals;
400 struct rte_eth_dev *slave_eth_dev;
403 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
404 internals = bonded_eth_dev->data->dev_private;
406 if (valid_slave_port_id(slave_port_id, internals->mode) < 0)
409 /* first remove from active slave list */
410 slave_idx = find_slave_by_id(internals->active_slaves,
411 internals->active_slave_count, slave_port_id);
413 if (slave_idx < internals->active_slave_count)
414 deactivate_slave(bonded_eth_dev, slave_port_id);
417 /* now find in slave list */
418 for (i = 0; i < internals->slave_count; i++)
419 if (internals->slaves[i].port_id == slave_port_id) {
425 RTE_BOND_LOG(ERR, "Couldn't find slave in port list, slave count %d",
426 internals->slave_count);
430 /* Un-register link status change callback with bonded device pointer as
432 rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
433 bond_ethdev_lsc_event_callback,
434 &rte_eth_devices[bonded_port_id].data->port_id);
436 /* Restore original MAC address of slave device */
437 mac_address_set(&rte_eth_devices[slave_port_id],
438 &(internals->slaves[slave_idx].persisted_mac_addr));
440 slave_eth_dev = &rte_eth_devices[slave_port_id];
441 slave_remove(internals, slave_eth_dev);
442 slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
444 /* first slave in the active list will be the primary by default,
445 * otherwise use first device in list */
446 if (internals->current_primary_port == slave_port_id) {
447 if (internals->active_slave_count > 0)
448 internals->current_primary_port = internals->active_slaves[0];
449 else if (internals->slave_count > 0)
450 internals->current_primary_port = internals->slaves[0].port_id;
452 internals->primary_port = 0;
455 if (internals->active_slave_count < 1) {
456 /* if no slaves are any longer attached to bonded device and MAC is not
457 * user defined then clear MAC of bonded device as it will be reset
458 * when a new slave is added */
459 if (internals->slave_count < 1 && !internals->user_defined_mac)
460 memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
461 sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
463 if (internals->slave_count == 0) {
464 internals->rx_offload_capa = 0;
465 internals->tx_offload_capa = 0;
466 internals->flow_type_rss_offloads = ETH_RSS_PROTO_MASK;
467 internals->reta_size = 0;
468 internals->candidate_max_rx_pktlen = 0;
469 internals->max_rx_pktlen = 0;
475 rte_eth_bond_slave_remove(uint16_t bonded_port_id, uint16_t slave_port_id)
477 struct rte_eth_dev *bonded_eth_dev;
478 struct bond_dev_private *internals;
481 if (valid_bonded_port_id(bonded_port_id) != 0)
484 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
485 internals = bonded_eth_dev->data->dev_private;
487 rte_spinlock_lock(&internals->lock);
489 retval = __eth_bond_slave_remove_lock_free(bonded_port_id, slave_port_id);
491 rte_spinlock_unlock(&internals->lock);
497 rte_eth_bond_mode_set(uint16_t bonded_port_id, uint8_t mode)
499 if (valid_bonded_port_id(bonded_port_id) != 0)
502 return bond_ethdev_mode_set(&rte_eth_devices[bonded_port_id], mode);
506 rte_eth_bond_mode_get(uint16_t bonded_port_id)
508 struct bond_dev_private *internals;
510 if (valid_bonded_port_id(bonded_port_id) != 0)
513 internals = rte_eth_devices[bonded_port_id].data->dev_private;
515 return internals->mode;
519 rte_eth_bond_primary_set(uint16_t bonded_port_id, uint16_t slave_port_id)
521 struct bond_dev_private *internals;
523 if (valid_bonded_port_id(bonded_port_id) != 0)
526 internals = rte_eth_devices[bonded_port_id].data->dev_private;
528 if (valid_slave_port_id(slave_port_id, internals->mode) != 0)
531 internals->user_defined_primary_port = 1;
532 internals->primary_port = slave_port_id;
534 bond_ethdev_primary_set(internals, slave_port_id);
540 rte_eth_bond_primary_get(uint16_t bonded_port_id)
542 struct bond_dev_private *internals;
544 if (valid_bonded_port_id(bonded_port_id) != 0)
547 internals = rte_eth_devices[bonded_port_id].data->dev_private;
549 if (internals->slave_count < 1)
552 return internals->current_primary_port;
556 rte_eth_bond_slaves_get(uint16_t bonded_port_id, uint16_t slaves[],
559 struct bond_dev_private *internals;
562 if (valid_bonded_port_id(bonded_port_id) != 0)
568 internals = rte_eth_devices[bonded_port_id].data->dev_private;
570 if (internals->slave_count > len)
573 for (i = 0; i < internals->slave_count; i++)
574 slaves[i] = internals->slaves[i].port_id;
576 return internals->slave_count;
580 rte_eth_bond_active_slaves_get(uint16_t bonded_port_id, uint16_t slaves[],
583 struct bond_dev_private *internals;
585 if (valid_bonded_port_id(bonded_port_id) != 0)
591 internals = rte_eth_devices[bonded_port_id].data->dev_private;
593 if (internals->active_slave_count > len)
596 memcpy(slaves, internals->active_slaves,
597 internals->active_slave_count * sizeof(internals->active_slaves[0]));
599 return internals->active_slave_count;
603 rte_eth_bond_mac_address_set(uint16_t bonded_port_id,
604 struct ether_addr *mac_addr)
606 struct rte_eth_dev *bonded_eth_dev;
607 struct bond_dev_private *internals;
609 if (valid_bonded_port_id(bonded_port_id) != 0)
612 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
613 internals = bonded_eth_dev->data->dev_private;
615 /* Set MAC Address of Bonded Device */
616 if (mac_address_set(bonded_eth_dev, mac_addr))
619 internals->user_defined_mac = 1;
621 /* Update all slave devices MACs*/
622 if (internals->slave_count > 0)
623 return mac_address_slaves_update(bonded_eth_dev);
629 rte_eth_bond_mac_address_reset(uint16_t bonded_port_id)
631 struct rte_eth_dev *bonded_eth_dev;
632 struct bond_dev_private *internals;
634 if (valid_bonded_port_id(bonded_port_id) != 0)
637 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
638 internals = bonded_eth_dev->data->dev_private;
640 internals->user_defined_mac = 0;
642 if (internals->slave_count > 0) {
643 /* Set MAC Address of Bonded Device */
644 if (mac_address_set(bonded_eth_dev,
645 &internals->slaves[internals->primary_port].persisted_mac_addr)
647 RTE_BOND_LOG(ERR, "Failed to set MAC address on bonded device");
650 /* Update all slave devices MAC addresses */
651 return mac_address_slaves_update(bonded_eth_dev);
653 /* No need to update anything as no slaves present */
658 rte_eth_bond_xmit_policy_set(uint16_t bonded_port_id, uint8_t policy)
660 struct bond_dev_private *internals;
662 if (valid_bonded_port_id(bonded_port_id) != 0)
665 internals = rte_eth_devices[bonded_port_id].data->dev_private;
668 case BALANCE_XMIT_POLICY_LAYER2:
669 internals->balance_xmit_policy = policy;
670 internals->xmit_hash = xmit_l2_hash;
672 case BALANCE_XMIT_POLICY_LAYER23:
673 internals->balance_xmit_policy = policy;
674 internals->xmit_hash = xmit_l23_hash;
676 case BALANCE_XMIT_POLICY_LAYER34:
677 internals->balance_xmit_policy = policy;
678 internals->xmit_hash = xmit_l34_hash;
688 rte_eth_bond_xmit_policy_get(uint16_t bonded_port_id)
690 struct bond_dev_private *internals;
692 if (valid_bonded_port_id(bonded_port_id) != 0)
695 internals = rte_eth_devices[bonded_port_id].data->dev_private;
697 return internals->balance_xmit_policy;
701 rte_eth_bond_link_monitoring_set(uint16_t bonded_port_id, uint32_t internal_ms)
703 struct bond_dev_private *internals;
705 if (valid_bonded_port_id(bonded_port_id) != 0)
708 internals = rte_eth_devices[bonded_port_id].data->dev_private;
709 internals->link_status_polling_interval_ms = internal_ms;
715 rte_eth_bond_link_monitoring_get(uint16_t bonded_port_id)
717 struct bond_dev_private *internals;
719 if (valid_bonded_port_id(bonded_port_id) != 0)
722 internals = rte_eth_devices[bonded_port_id].data->dev_private;
724 return internals->link_status_polling_interval_ms;
728 rte_eth_bond_link_down_prop_delay_set(uint16_t bonded_port_id,
732 struct bond_dev_private *internals;
734 if (valid_bonded_port_id(bonded_port_id) != 0)
737 internals = rte_eth_devices[bonded_port_id].data->dev_private;
738 internals->link_down_delay_ms = delay_ms;
744 rte_eth_bond_link_down_prop_delay_get(uint16_t bonded_port_id)
746 struct bond_dev_private *internals;
748 if (valid_bonded_port_id(bonded_port_id) != 0)
751 internals = rte_eth_devices[bonded_port_id].data->dev_private;
753 return internals->link_down_delay_ms;
757 rte_eth_bond_link_up_prop_delay_set(uint16_t bonded_port_id, uint32_t delay_ms)
760 struct bond_dev_private *internals;
762 if (valid_bonded_port_id(bonded_port_id) != 0)
765 internals = rte_eth_devices[bonded_port_id].data->dev_private;
766 internals->link_up_delay_ms = delay_ms;
772 rte_eth_bond_link_up_prop_delay_get(uint16_t bonded_port_id)
774 struct bond_dev_private *internals;
776 if (valid_bonded_port_id(bonded_port_id) != 0)
779 internals = rte_eth_devices[bonded_port_id].data->dev_private;
781 return internals->link_up_delay_ms;