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->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(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 rte_eth_bond_create(const char *name, uint8_t mode, uint8_t socket_id)
149 struct bond_dev_private *internals;
155 RTE_BOND_LOG(ERR, "Invalid name specified");
159 ret = snprintf(devargs, sizeof(devargs),
160 "driver=net_bonding,mode=%d,socket_id=%d", mode, socket_id);
161 if (ret < 0 || ret >= (int)sizeof(devargs))
164 ret = rte_vdev_init(name, devargs);
168 ret = rte_eth_dev_get_port_by_name(name, &port_id);
172 * To make bond_ethdev_configure() happy we need to free the
173 * internals->kvlist here.
175 * Also see comment in bond_ethdev_configure().
177 internals = rte_eth_devices[port_id].data->dev_private;
178 rte_kvargs_free(internals->kvlist);
179 internals->kvlist = NULL;
185 rte_eth_bond_free(const char *name)
187 return rte_vdev_uninit(name);
191 slave_vlan_filter_set(uint8_t bonded_port_id, uint8_t slave_port_id)
193 struct rte_eth_dev *bonded_eth_dev;
194 struct bond_dev_private *internals;
201 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
202 if (bonded_eth_dev->data->dev_conf.rxmode.hw_vlan_filter == 0)
205 internals = bonded_eth_dev->data->dev_private;
206 found = rte_bitmap_scan(internals->vlan_filter_bmp, &pos, &slab);
216 for (i = 0, mask = 1;
217 i < RTE_BITMAP_SLAB_BIT_SIZE;
219 if (unlikely(slab & mask))
220 res = rte_eth_dev_vlan_filter(slave_port_id,
223 found = rte_bitmap_scan(internals->vlan_filter_bmp,
225 } while (found && first != pos && res == 0);
231 __eth_bond_slave_add_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
233 struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
234 struct bond_dev_private *internals;
235 struct rte_eth_link link_props;
236 struct rte_eth_dev_info dev_info;
238 if (valid_slave_port_id(slave_port_id) != 0)
241 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
242 internals = bonded_eth_dev->data->dev_private;
244 slave_eth_dev = &rte_eth_devices[slave_port_id];
245 if (slave_eth_dev->data->dev_flags & RTE_ETH_DEV_BONDED_SLAVE) {
246 RTE_BOND_LOG(ERR, "Slave device is already a slave of a bonded device");
250 /* Add slave details to bonded device */
251 slave_eth_dev->data->dev_flags |= RTE_ETH_DEV_BONDED_SLAVE;
253 rte_eth_dev_info_get(slave_port_id, &dev_info);
254 if (dev_info.max_rx_pktlen < internals->max_rx_pktlen) {
255 RTE_BOND_LOG(ERR, "Slave (port %u) max_rx_pktlen too small",
260 slave_add(internals, slave_eth_dev);
262 /* We need to store slaves reta_size to be able to synchronize RETA for all
263 * slave devices even if its sizes are different.
265 internals->slaves[internals->slave_count].reta_size = dev_info.reta_size;
267 if (internals->slave_count < 1) {
268 /* if MAC is not user defined then use MAC of first slave add to
270 if (!internals->user_defined_mac)
271 mac_address_set(bonded_eth_dev, slave_eth_dev->data->mac_addrs);
273 /* Inherit eth dev link properties from first slave */
274 link_properties_set(bonded_eth_dev,
275 &(slave_eth_dev->data->dev_link));
277 /* Make primary slave */
278 internals->primary_port = slave_port_id;
279 internals->current_primary_port = slave_port_id;
281 /* Inherit queues settings from first slave */
282 internals->nb_rx_queues = slave_eth_dev->data->nb_rx_queues;
283 internals->nb_tx_queues = slave_eth_dev->data->nb_tx_queues;
285 internals->reta_size = dev_info.reta_size;
287 /* Take the first dev's offload capabilities */
288 internals->rx_offload_capa = dev_info.rx_offload_capa;
289 internals->tx_offload_capa = dev_info.tx_offload_capa;
290 internals->flow_type_rss_offloads = dev_info.flow_type_rss_offloads;
292 /* Inherit first slave's max rx packet size */
293 internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
296 internals->rx_offload_capa &= dev_info.rx_offload_capa;
297 internals->tx_offload_capa &= dev_info.tx_offload_capa;
298 internals->flow_type_rss_offloads &= dev_info.flow_type_rss_offloads;
300 /* RETA size is GCD of all slaves RETA sizes, so, if all sizes will be
301 * the power of 2, the lower one is GCD
303 if (internals->reta_size > dev_info.reta_size)
304 internals->reta_size = dev_info.reta_size;
306 if (!internals->max_rx_pktlen &&
307 dev_info.max_rx_pktlen < internals->candidate_max_rx_pktlen)
308 internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
311 bonded_eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf &=
312 internals->flow_type_rss_offloads;
314 internals->slave_count++;
316 /* Update all slave devices MACs*/
317 mac_address_slaves_update(bonded_eth_dev);
319 if (bonded_eth_dev->data->dev_started) {
320 if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
321 slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
322 RTE_BOND_LOG(ERR, "rte_bond_slaves_configure: port=%d",
328 /* Register link status change callback with bonded device pointer as
330 rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
331 bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
333 /* If bonded device is started then we can add the slave to our active
335 if (bonded_eth_dev->data->dev_started) {
336 rte_eth_link_get_nowait(slave_port_id, &link_props);
338 if (link_props.link_status == ETH_LINK_UP) {
339 if (internals->active_slave_count == 0 &&
340 !internals->user_defined_primary_port)
341 bond_ethdev_primary_set(internals,
344 if (find_slave_by_id(internals->active_slaves,
345 internals->active_slave_count,
346 slave_port_id) == internals->active_slave_count)
347 activate_slave(bonded_eth_dev, slave_port_id);
351 slave_vlan_filter_set(bonded_port_id, slave_port_id);
358 rte_eth_bond_slave_add(uint8_t bonded_port_id, uint8_t slave_port_id)
360 struct rte_eth_dev *bonded_eth_dev;
361 struct bond_dev_private *internals;
365 /* Verify that port id's are valid bonded and slave ports */
366 if (valid_bonded_port_id(bonded_port_id) != 0)
369 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
370 internals = bonded_eth_dev->data->dev_private;
372 rte_spinlock_lock(&internals->lock);
374 retval = __eth_bond_slave_add_lock_free(bonded_port_id, slave_port_id);
376 rte_spinlock_unlock(&internals->lock);
382 __eth_bond_slave_remove_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
384 struct rte_eth_dev *bonded_eth_dev;
385 struct bond_dev_private *internals;
386 struct rte_eth_dev *slave_eth_dev;
389 if (valid_slave_port_id(slave_port_id) != 0)
392 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
393 internals = bonded_eth_dev->data->dev_private;
395 /* first remove from active slave list */
396 slave_idx = find_slave_by_id(internals->active_slaves,
397 internals->active_slave_count, slave_port_id);
399 if (slave_idx < internals->active_slave_count)
400 deactivate_slave(bonded_eth_dev, slave_port_id);
403 /* now find in slave list */
404 for (i = 0; i < internals->slave_count; i++)
405 if (internals->slaves[i].port_id == slave_port_id) {
411 RTE_BOND_LOG(ERR, "Couldn't find slave in port list, slave count %d",
412 internals->slave_count);
416 /* Un-register link status change callback with bonded device pointer as
418 rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
419 bond_ethdev_lsc_event_callback,
420 &rte_eth_devices[bonded_port_id].data->port_id);
422 /* Restore original MAC address of slave device */
423 mac_address_set(&rte_eth_devices[slave_port_id],
424 &(internals->slaves[slave_idx].persisted_mac_addr));
426 slave_eth_dev = &rte_eth_devices[slave_port_id];
427 slave_remove(internals, slave_eth_dev);
428 slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
430 /* first slave in the active list will be the primary by default,
431 * otherwise use first device in list */
432 if (internals->current_primary_port == slave_port_id) {
433 if (internals->active_slave_count > 0)
434 internals->current_primary_port = internals->active_slaves[0];
435 else if (internals->slave_count > 0)
436 internals->current_primary_port = internals->slaves[0].port_id;
438 internals->primary_port = 0;
441 if (internals->active_slave_count < 1) {
442 /* reset device link properties as no slaves are active */
443 link_properties_reset(&rte_eth_devices[bonded_port_id]);
445 /* if no slaves are any longer attached to bonded device and MAC is not
446 * user defined then clear MAC of bonded device as it will be reset
447 * when a new slave is added */
448 if (internals->slave_count < 1 && !internals->user_defined_mac)
449 memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
450 sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
452 if (internals->slave_count == 0) {
453 internals->rx_offload_capa = 0;
454 internals->tx_offload_capa = 0;
455 internals->flow_type_rss_offloads = ETH_RSS_PROTO_MASK;
456 internals->reta_size = 0;
457 internals->candidate_max_rx_pktlen = 0;
458 internals->max_rx_pktlen = 0;
464 rte_eth_bond_slave_remove(uint8_t bonded_port_id, uint8_t slave_port_id)
466 struct rte_eth_dev *bonded_eth_dev;
467 struct bond_dev_private *internals;
470 if (valid_bonded_port_id(bonded_port_id) != 0)
473 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
474 internals = bonded_eth_dev->data->dev_private;
476 rte_spinlock_lock(&internals->lock);
478 retval = __eth_bond_slave_remove_lock_free(bonded_port_id, slave_port_id);
480 rte_spinlock_unlock(&internals->lock);
486 rte_eth_bond_mode_set(uint8_t bonded_port_id, uint8_t mode)
488 if (valid_bonded_port_id(bonded_port_id) != 0)
491 return bond_ethdev_mode_set(&rte_eth_devices[bonded_port_id], mode);
495 rte_eth_bond_mode_get(uint8_t bonded_port_id)
497 struct bond_dev_private *internals;
499 if (valid_bonded_port_id(bonded_port_id) != 0)
502 internals = rte_eth_devices[bonded_port_id].data->dev_private;
504 return internals->mode;
508 rte_eth_bond_primary_set(uint8_t bonded_port_id, uint8_t slave_port_id)
510 struct bond_dev_private *internals;
512 if (valid_bonded_port_id(bonded_port_id) != 0)
515 if (valid_slave_port_id(slave_port_id) != 0)
518 internals = rte_eth_devices[bonded_port_id].data->dev_private;
520 internals->user_defined_primary_port = 1;
521 internals->primary_port = slave_port_id;
523 bond_ethdev_primary_set(internals, slave_port_id);
529 rte_eth_bond_primary_get(uint8_t bonded_port_id)
531 struct bond_dev_private *internals;
533 if (valid_bonded_port_id(bonded_port_id) != 0)
536 internals = rte_eth_devices[bonded_port_id].data->dev_private;
538 if (internals->slave_count < 1)
541 return internals->current_primary_port;
545 rte_eth_bond_slaves_get(uint8_t bonded_port_id, uint8_t slaves[], uint8_t len)
547 struct bond_dev_private *internals;
550 if (valid_bonded_port_id(bonded_port_id) != 0)
556 internals = rte_eth_devices[bonded_port_id].data->dev_private;
558 if (internals->slave_count > len)
561 for (i = 0; i < internals->slave_count; i++)
562 slaves[i] = internals->slaves[i].port_id;
564 return internals->slave_count;
568 rte_eth_bond_active_slaves_get(uint8_t bonded_port_id, uint8_t slaves[],
571 struct bond_dev_private *internals;
573 if (valid_bonded_port_id(bonded_port_id) != 0)
579 internals = rte_eth_devices[bonded_port_id].data->dev_private;
581 if (internals->active_slave_count > len)
584 memcpy(slaves, internals->active_slaves, internals->active_slave_count);
586 return internals->active_slave_count;
590 rte_eth_bond_mac_address_set(uint8_t bonded_port_id,
591 struct ether_addr *mac_addr)
593 struct rte_eth_dev *bonded_eth_dev;
594 struct bond_dev_private *internals;
596 if (valid_bonded_port_id(bonded_port_id) != 0)
599 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
600 internals = bonded_eth_dev->data->dev_private;
602 /* Set MAC Address of Bonded Device */
603 if (mac_address_set(bonded_eth_dev, mac_addr))
606 internals->user_defined_mac = 1;
608 /* Update all slave devices MACs*/
609 if (internals->slave_count > 0)
610 return mac_address_slaves_update(bonded_eth_dev);
616 rte_eth_bond_mac_address_reset(uint8_t bonded_port_id)
618 struct rte_eth_dev *bonded_eth_dev;
619 struct bond_dev_private *internals;
621 if (valid_bonded_port_id(bonded_port_id) != 0)
624 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
625 internals = bonded_eth_dev->data->dev_private;
627 internals->user_defined_mac = 0;
629 if (internals->slave_count > 0) {
630 /* Set MAC Address of Bonded Device */
631 if (mac_address_set(bonded_eth_dev,
632 &internals->slaves[internals->primary_port].persisted_mac_addr)
634 RTE_BOND_LOG(ERR, "Failed to set MAC address on bonded device");
637 /* Update all slave devices MAC addresses */
638 return mac_address_slaves_update(bonded_eth_dev);
640 /* No need to update anything as no slaves present */
645 rte_eth_bond_xmit_policy_set(uint8_t bonded_port_id, uint8_t policy)
647 struct bond_dev_private *internals;
649 if (valid_bonded_port_id(bonded_port_id) != 0)
652 internals = rte_eth_devices[bonded_port_id].data->dev_private;
655 case BALANCE_XMIT_POLICY_LAYER2:
656 internals->balance_xmit_policy = policy;
657 internals->xmit_hash = xmit_l2_hash;
659 case BALANCE_XMIT_POLICY_LAYER23:
660 internals->balance_xmit_policy = policy;
661 internals->xmit_hash = xmit_l23_hash;
663 case BALANCE_XMIT_POLICY_LAYER34:
664 internals->balance_xmit_policy = policy;
665 internals->xmit_hash = xmit_l34_hash;
675 rte_eth_bond_xmit_policy_get(uint8_t bonded_port_id)
677 struct bond_dev_private *internals;
679 if (valid_bonded_port_id(bonded_port_id) != 0)
682 internals = rte_eth_devices[bonded_port_id].data->dev_private;
684 return internals->balance_xmit_policy;
688 rte_eth_bond_link_monitoring_set(uint8_t bonded_port_id, uint32_t internal_ms)
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;
696 internals->link_status_polling_interval_ms = internal_ms;
702 rte_eth_bond_link_monitoring_get(uint8_t bonded_port_id)
704 struct bond_dev_private *internals;
706 if (valid_bonded_port_id(bonded_port_id) != 0)
709 internals = rte_eth_devices[bonded_port_id].data->dev_private;
711 return internals->link_status_polling_interval_ms;
715 rte_eth_bond_link_down_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
718 struct bond_dev_private *internals;
720 if (valid_bonded_port_id(bonded_port_id) != 0)
723 internals = rte_eth_devices[bonded_port_id].data->dev_private;
724 internals->link_down_delay_ms = delay_ms;
730 rte_eth_bond_link_down_prop_delay_get(uint8_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;
739 return internals->link_down_delay_ms;
743 rte_eth_bond_link_up_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
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;
752 internals->link_up_delay_ms = delay_ms;
758 rte_eth_bond_link_up_prop_delay_get(uint8_t bonded_port_id)
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;
767 return internals->link_up_delay_ms;