4 * Copyright(c) 2010-2016 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->data->drv_name == NULL)
54 /* return 0 if driver name matches */
55 return eth_dev->data->drv_name != pmd_bond_drv.driver.name;
59 valid_bonded_port_id(uint8_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(uint8_t port_id)
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)
78 activate_slave(struct rte_eth_dev *eth_dev, uint8_t port_id)
80 struct bond_dev_private *internals = eth_dev->data->dev_private;
81 uint8_t active_count = internals->active_slave_count;
83 if (internals->mode == BONDING_MODE_8023AD)
84 bond_mode_8023ad_activate_slave(eth_dev, port_id);
86 if (internals->mode == BONDING_MODE_TLB
87 || internals->mode == BONDING_MODE_ALB) {
89 internals->tlb_slaves_order[active_count] = port_id;
92 RTE_ASSERT(internals->active_slave_count <
93 (RTE_DIM(internals->active_slaves) - 1));
95 internals->active_slaves[internals->active_slave_count] = port_id;
96 internals->active_slave_count++;
98 if (internals->mode == BONDING_MODE_TLB)
99 bond_tlb_activate_slave(internals);
100 if (internals->mode == BONDING_MODE_ALB)
101 bond_mode_alb_client_list_upd(eth_dev);
105 deactivate_slave(struct rte_eth_dev *eth_dev, uint8_t port_id)
108 struct bond_dev_private *internals = eth_dev->data->dev_private;
109 uint8_t active_count = internals->active_slave_count;
111 if (internals->mode == BONDING_MODE_8023AD) {
112 bond_mode_8023ad_stop(eth_dev);
113 bond_mode_8023ad_deactivate_slave(eth_dev, port_id);
114 } else if (internals->mode == BONDING_MODE_TLB
115 || internals->mode == BONDING_MODE_ALB)
116 bond_tlb_disable(internals);
118 slave_pos = find_slave_by_id(internals->active_slaves, active_count,
121 /* If slave was not at the end of the list
122 * shift active slaves up active array list */
123 if (slave_pos < active_count) {
125 memmove(internals->active_slaves + slave_pos,
126 internals->active_slaves + slave_pos + 1,
127 (active_count - slave_pos) *
128 sizeof(internals->active_slaves[0]));
131 RTE_ASSERT(active_count < RTE_DIM(internals->active_slaves));
132 internals->active_slave_count = active_count;
134 if (eth_dev->data->dev_started) {
135 if (internals->mode == BONDING_MODE_8023AD) {
136 bond_mode_8023ad_start(eth_dev);
137 } else if (internals->mode == BONDING_MODE_TLB) {
138 bond_tlb_enable(internals);
139 } else if (internals->mode == BONDING_MODE_ALB) {
140 bond_tlb_enable(internals);
141 bond_mode_alb_client_list_upd(eth_dev);
147 number_of_sockets(void)
151 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
153 for (i = 0; ((i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL)); i++) {
154 if (sockets < ms[i].socket_id)
155 sockets = ms[i].socket_id;
158 /* Number of sockets = maximum socket_id + 1 */
163 rte_eth_bond_create(const char *name, uint8_t mode, uint8_t socket_id)
165 struct bond_dev_private *internals;
171 RTE_BOND_LOG(ERR, "Invalid name specified");
175 ret = snprintf(devargs, sizeof(devargs),
176 "driver=net_bonding,mode=%d,socket_id=%d", mode, socket_id);
177 if (ret < 0 || ret >= (int)sizeof(devargs))
180 ret = rte_eal_vdev_init(name, devargs);
184 ret = rte_eth_dev_get_port_by_name(name, &port_id);
188 * To make bond_ethdev_configure() happy we need to free the
189 * internals->kvlist here.
191 * Also see comment in bond_ethdev_configure().
193 internals = rte_eth_devices[port_id].data->dev_private;
194 rte_kvargs_free(internals->kvlist);
195 internals->kvlist = NULL;
201 rte_eth_bond_free(const char *name)
203 return rte_eal_vdev_uninit(name);
207 slave_vlan_filter_set(uint8_t bonded_port_id, uint8_t slave_port_id)
209 struct rte_eth_dev *bonded_eth_dev;
210 struct bond_dev_private *internals;
217 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
218 if (bonded_eth_dev->data->dev_conf.rxmode.hw_vlan_filter == 0)
221 internals = bonded_eth_dev->data->dev_private;
222 found = rte_bitmap_scan(internals->vlan_filter_bmp, &pos, &slab);
232 for (i = 0, mask = 1;
233 i < RTE_BITMAP_SLAB_BIT_SIZE;
235 if (unlikely(slab & mask))
236 res = rte_eth_dev_vlan_filter(slave_port_id,
239 found = rte_bitmap_scan(internals->vlan_filter_bmp,
241 } while (found && first != pos && res == 0);
247 __eth_bond_slave_add_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
249 struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
250 struct bond_dev_private *internals;
251 struct rte_eth_link link_props;
252 struct rte_eth_dev_info dev_info;
254 if (valid_slave_port_id(slave_port_id) != 0)
257 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
258 internals = bonded_eth_dev->data->dev_private;
260 slave_eth_dev = &rte_eth_devices[slave_port_id];
261 if (slave_eth_dev->data->dev_flags & RTE_ETH_DEV_BONDED_SLAVE) {
262 RTE_BOND_LOG(ERR, "Slave device is already a slave of a bonded device");
266 /* Add slave details to bonded device */
267 slave_eth_dev->data->dev_flags |= RTE_ETH_DEV_BONDED_SLAVE;
269 rte_eth_dev_info_get(slave_port_id, &dev_info);
270 if (dev_info.max_rx_pktlen < internals->max_rx_pktlen) {
271 RTE_BOND_LOG(ERR, "Slave (port %u) max_rx_pktlen too small",
276 slave_add(internals, slave_eth_dev);
278 /* We need to store slaves reta_size to be able to synchronize RETA for all
279 * slave devices even if its sizes are different.
281 internals->slaves[internals->slave_count].reta_size = dev_info.reta_size;
283 if (internals->slave_count < 1) {
284 /* if MAC is not user defined then use MAC of first slave add to
286 if (!internals->user_defined_mac)
287 mac_address_set(bonded_eth_dev, slave_eth_dev->data->mac_addrs);
289 /* Inherit eth dev link properties from first slave */
290 link_properties_set(bonded_eth_dev,
291 &(slave_eth_dev->data->dev_link));
293 /* Make primary slave */
294 internals->primary_port = slave_port_id;
295 internals->current_primary_port = slave_port_id;
297 /* Inherit queues settings from first slave */
298 internals->nb_rx_queues = slave_eth_dev->data->nb_rx_queues;
299 internals->nb_tx_queues = slave_eth_dev->data->nb_tx_queues;
301 internals->reta_size = dev_info.reta_size;
303 /* Take the first dev's offload capabilities */
304 internals->rx_offload_capa = dev_info.rx_offload_capa;
305 internals->tx_offload_capa = dev_info.tx_offload_capa;
306 internals->flow_type_rss_offloads = dev_info.flow_type_rss_offloads;
308 /* Inherit first slave's max rx packet size */
309 internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
312 internals->rx_offload_capa &= dev_info.rx_offload_capa;
313 internals->tx_offload_capa &= dev_info.tx_offload_capa;
314 internals->flow_type_rss_offloads &= dev_info.flow_type_rss_offloads;
316 /* RETA size is GCD of all slaves RETA sizes, so, if all sizes will be
317 * the power of 2, the lower one is GCD
319 if (internals->reta_size > dev_info.reta_size)
320 internals->reta_size = dev_info.reta_size;
322 if (!internals->max_rx_pktlen &&
323 dev_info.max_rx_pktlen < internals->candidate_max_rx_pktlen)
324 internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
327 bonded_eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf &=
328 internals->flow_type_rss_offloads;
330 internals->slave_count++;
332 /* Update all slave devices MACs*/
333 mac_address_slaves_update(bonded_eth_dev);
335 if (bonded_eth_dev->data->dev_started) {
336 if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
337 slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
338 RTE_BOND_LOG(ERR, "rte_bond_slaves_configure: port=%d",
344 /* Register link status change callback with bonded device pointer as
346 rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
347 bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
349 /* If bonded device is started then we can add the slave to our active
351 if (bonded_eth_dev->data->dev_started) {
352 rte_eth_link_get_nowait(slave_port_id, &link_props);
354 if (link_props.link_status == ETH_LINK_UP) {
355 if (internals->active_slave_count == 0 &&
356 !internals->user_defined_primary_port)
357 bond_ethdev_primary_set(internals,
360 if (find_slave_by_id(internals->active_slaves,
361 internals->active_slave_count,
362 slave_port_id) == internals->active_slave_count)
363 activate_slave(bonded_eth_dev, slave_port_id);
367 slave_vlan_filter_set(bonded_port_id, slave_port_id);
374 rte_eth_bond_slave_add(uint8_t bonded_port_id, uint8_t slave_port_id)
376 struct rte_eth_dev *bonded_eth_dev;
377 struct bond_dev_private *internals;
381 /* Verify that port id's are valid bonded and slave ports */
382 if (valid_bonded_port_id(bonded_port_id) != 0)
385 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
386 internals = bonded_eth_dev->data->dev_private;
388 rte_spinlock_lock(&internals->lock);
390 retval = __eth_bond_slave_add_lock_free(bonded_port_id, slave_port_id);
392 rte_spinlock_unlock(&internals->lock);
398 __eth_bond_slave_remove_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
400 struct rte_eth_dev *bonded_eth_dev;
401 struct bond_dev_private *internals;
402 struct rte_eth_dev *slave_eth_dev;
405 if (valid_slave_port_id(slave_port_id) != 0)
408 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
409 internals = bonded_eth_dev->data->dev_private;
411 /* first remove from active slave list */
412 slave_idx = find_slave_by_id(internals->active_slaves,
413 internals->active_slave_count, slave_port_id);
415 if (slave_idx < internals->active_slave_count)
416 deactivate_slave(bonded_eth_dev, slave_port_id);
419 /* now find in slave list */
420 for (i = 0; i < internals->slave_count; i++)
421 if (internals->slaves[i].port_id == slave_port_id) {
427 RTE_BOND_LOG(ERR, "Couldn't find slave in port list, slave count %d",
428 internals->slave_count);
432 /* Un-register link status change callback with bonded device pointer as
434 rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
435 bond_ethdev_lsc_event_callback,
436 &rte_eth_devices[bonded_port_id].data->port_id);
438 /* Restore original MAC address of slave device */
439 mac_address_set(&rte_eth_devices[slave_port_id],
440 &(internals->slaves[slave_idx].persisted_mac_addr));
442 slave_eth_dev = &rte_eth_devices[slave_port_id];
443 slave_remove(internals, slave_eth_dev);
444 slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
446 /* first slave in the active list will be the primary by default,
447 * otherwise use first device in list */
448 if (internals->current_primary_port == slave_port_id) {
449 if (internals->active_slave_count > 0)
450 internals->current_primary_port = internals->active_slaves[0];
451 else if (internals->slave_count > 0)
452 internals->current_primary_port = internals->slaves[0].port_id;
454 internals->primary_port = 0;
457 if (internals->active_slave_count < 1) {
458 /* reset device link properties as no slaves are active */
459 link_properties_reset(&rte_eth_devices[bonded_port_id]);
461 /* if no slaves are any longer attached to bonded device and MAC is not
462 * user defined then clear MAC of bonded device as it will be reset
463 * when a new slave is added */
464 if (internals->slave_count < 1 && !internals->user_defined_mac)
465 memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
466 sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
468 if (internals->slave_count == 0) {
469 internals->rx_offload_capa = 0;
470 internals->tx_offload_capa = 0;
471 internals->flow_type_rss_offloads = ETH_RSS_PROTO_MASK;
472 internals->reta_size = 0;
473 internals->candidate_max_rx_pktlen = 0;
474 internals->max_rx_pktlen = 0;
480 rte_eth_bond_slave_remove(uint8_t bonded_port_id, uint8_t slave_port_id)
482 struct rte_eth_dev *bonded_eth_dev;
483 struct bond_dev_private *internals;
486 if (valid_bonded_port_id(bonded_port_id) != 0)
489 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
490 internals = bonded_eth_dev->data->dev_private;
492 rte_spinlock_lock(&internals->lock);
494 retval = __eth_bond_slave_remove_lock_free(bonded_port_id, slave_port_id);
496 rte_spinlock_unlock(&internals->lock);
502 rte_eth_bond_mode_set(uint8_t bonded_port_id, uint8_t mode)
504 if (valid_bonded_port_id(bonded_port_id) != 0)
507 return bond_ethdev_mode_set(&rte_eth_devices[bonded_port_id], mode);
511 rte_eth_bond_mode_get(uint8_t bonded_port_id)
513 struct bond_dev_private *internals;
515 if (valid_bonded_port_id(bonded_port_id) != 0)
518 internals = rte_eth_devices[bonded_port_id].data->dev_private;
520 return internals->mode;
524 rte_eth_bond_primary_set(uint8_t bonded_port_id, uint8_t slave_port_id)
526 struct bond_dev_private *internals;
528 if (valid_bonded_port_id(bonded_port_id) != 0)
531 if (valid_slave_port_id(slave_port_id) != 0)
534 internals = rte_eth_devices[bonded_port_id].data->dev_private;
536 internals->user_defined_primary_port = 1;
537 internals->primary_port = slave_port_id;
539 bond_ethdev_primary_set(internals, slave_port_id);
545 rte_eth_bond_primary_get(uint8_t bonded_port_id)
547 struct bond_dev_private *internals;
549 if (valid_bonded_port_id(bonded_port_id) != 0)
552 internals = rte_eth_devices[bonded_port_id].data->dev_private;
554 if (internals->slave_count < 1)
557 return internals->current_primary_port;
561 rte_eth_bond_slaves_get(uint8_t bonded_port_id, uint8_t slaves[], uint8_t len)
563 struct bond_dev_private *internals;
566 if (valid_bonded_port_id(bonded_port_id) != 0)
572 internals = rte_eth_devices[bonded_port_id].data->dev_private;
574 if (internals->slave_count > len)
577 for (i = 0; i < internals->slave_count; i++)
578 slaves[i] = internals->slaves[i].port_id;
580 return internals->slave_count;
584 rte_eth_bond_active_slaves_get(uint8_t bonded_port_id, uint8_t slaves[],
587 struct bond_dev_private *internals;
589 if (valid_bonded_port_id(bonded_port_id) != 0)
595 internals = rte_eth_devices[bonded_port_id].data->dev_private;
597 if (internals->active_slave_count > len)
600 memcpy(slaves, internals->active_slaves, internals->active_slave_count);
602 return internals->active_slave_count;
606 rte_eth_bond_mac_address_set(uint8_t bonded_port_id,
607 struct ether_addr *mac_addr)
609 struct rte_eth_dev *bonded_eth_dev;
610 struct bond_dev_private *internals;
612 if (valid_bonded_port_id(bonded_port_id) != 0)
615 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
616 internals = bonded_eth_dev->data->dev_private;
618 /* Set MAC Address of Bonded Device */
619 if (mac_address_set(bonded_eth_dev, mac_addr))
622 internals->user_defined_mac = 1;
624 /* Update all slave devices MACs*/
625 if (internals->slave_count > 0)
626 return mac_address_slaves_update(bonded_eth_dev);
632 rte_eth_bond_mac_address_reset(uint8_t bonded_port_id)
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 internals->user_defined_mac = 0;
645 if (internals->slave_count > 0) {
646 /* Set MAC Address of Bonded Device */
647 if (mac_address_set(bonded_eth_dev,
648 &internals->slaves[internals->primary_port].persisted_mac_addr)
650 RTE_BOND_LOG(ERR, "Failed to set MAC address on bonded device");
653 /* Update all slave devices MAC addresses */
654 return mac_address_slaves_update(bonded_eth_dev);
656 /* No need to update anything as no slaves present */
661 rte_eth_bond_xmit_policy_set(uint8_t bonded_port_id, uint8_t policy)
663 struct bond_dev_private *internals;
665 if (valid_bonded_port_id(bonded_port_id) != 0)
668 internals = rte_eth_devices[bonded_port_id].data->dev_private;
671 case BALANCE_XMIT_POLICY_LAYER2:
672 internals->balance_xmit_policy = policy;
673 internals->xmit_hash = xmit_l2_hash;
675 case BALANCE_XMIT_POLICY_LAYER23:
676 internals->balance_xmit_policy = policy;
677 internals->xmit_hash = xmit_l23_hash;
679 case BALANCE_XMIT_POLICY_LAYER34:
680 internals->balance_xmit_policy = policy;
681 internals->xmit_hash = xmit_l34_hash;
691 rte_eth_bond_xmit_policy_get(uint8_t bonded_port_id)
693 struct bond_dev_private *internals;
695 if (valid_bonded_port_id(bonded_port_id) != 0)
698 internals = rte_eth_devices[bonded_port_id].data->dev_private;
700 return internals->balance_xmit_policy;
704 rte_eth_bond_link_monitoring_set(uint8_t bonded_port_id, uint32_t internal_ms)
706 struct bond_dev_private *internals;
708 if (valid_bonded_port_id(bonded_port_id) != 0)
711 internals = rte_eth_devices[bonded_port_id].data->dev_private;
712 internals->link_status_polling_interval_ms = internal_ms;
718 rte_eth_bond_link_monitoring_get(uint8_t bonded_port_id)
720 struct bond_dev_private *internals;
722 if (valid_bonded_port_id(bonded_port_id) != 0)
725 internals = rte_eth_devices[bonded_port_id].data->dev_private;
727 return internals->link_status_polling_interval_ms;
731 rte_eth_bond_link_down_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
734 struct bond_dev_private *internals;
736 if (valid_bonded_port_id(bonded_port_id) != 0)
739 internals = rte_eth_devices[bonded_port_id].data->dev_private;
740 internals->link_down_delay_ms = delay_ms;
746 rte_eth_bond_link_down_prop_delay_get(uint8_t bonded_port_id)
748 struct bond_dev_private *internals;
750 if (valid_bonded_port_id(bonded_port_id) != 0)
753 internals = rte_eth_devices[bonded_port_id].data->dev_private;
755 return internals->link_down_delay_ms;
759 rte_eth_bond_link_up_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
762 struct bond_dev_private *internals;
764 if (valid_bonded_port_id(bonded_port_id) != 0)
767 internals = rte_eth_devices[bonded_port_id].data->dev_private;
768 internals->link_up_delay_ms = delay_ms;
774 rte_eth_bond_link_up_prop_delay_get(uint8_t bonded_port_id)
776 struct bond_dev_private *internals;
778 if (valid_bonded_port_id(bonded_port_id) != 0)
781 internals = rte_eth_devices[bonded_port_id].data->dev_private;
783 return internals->link_up_delay_ms;