xref: /dpdk/drivers/net/bonding/rte_eth_bond_api.c (revision c7f5dba7d4bb7971fac51755aad09b71b10cef90)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4 
5 #include <string.h>
6 
7 #include <rte_mbuf.h>
8 #include <rte_malloc.h>
9 #include <rte_ethdev_driver.h>
10 #include <rte_tcp.h>
11 #include <rte_bus_vdev.h>
12 #include <rte_kvargs.h>
13 
14 #include "rte_eth_bond.h"
15 #include "rte_eth_bond_private.h"
16 #include "rte_eth_bond_8023ad_private.h"
17 
18 int
19 check_for_bonded_ethdev(const struct rte_eth_dev *eth_dev)
20 {
21 	/* Check valid pointer */
22 	if (eth_dev->device->driver->name == NULL)
23 		return -1;
24 
25 	/* return 0 if driver name matches */
26 	return eth_dev->device->driver->name != pmd_bond_drv.driver.name;
27 }
28 
29 int
30 valid_bonded_port_id(uint16_t port_id)
31 {
32 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
33 	return check_for_bonded_ethdev(&rte_eth_devices[port_id]);
34 }
35 
36 int
37 check_for_master_bonded_ethdev(const struct rte_eth_dev *eth_dev)
38 {
39 	int i;
40 	struct bond_dev_private *internals;
41 
42 	if (check_for_bonded_ethdev(eth_dev) != 0)
43 		return 0;
44 
45 	internals = eth_dev->data->dev_private;
46 
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)
50 			return 1;
51 
52 	return 0;
53 }
54 
55 int
56 valid_slave_port_id(uint16_t port_id, uint8_t mode)
57 {
58 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
59 
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.");
66 		return -1;
67 	}
68 
69 	return 0;
70 }
71 
72 void
73 activate_slave(struct rte_eth_dev *eth_dev, uint16_t port_id)
74 {
75 	struct bond_dev_private *internals = eth_dev->data->dev_private;
76 	uint8_t active_count = internals->active_slave_count;
77 
78 	if (internals->mode == BONDING_MODE_8023AD)
79 		bond_mode_8023ad_activate_slave(eth_dev, port_id);
80 
81 	if (internals->mode == BONDING_MODE_TLB
82 			|| internals->mode == BONDING_MODE_ALB) {
83 
84 		internals->tlb_slaves_order[active_count] = port_id;
85 	}
86 
87 	RTE_ASSERT(internals->active_slave_count <
88 			(RTE_DIM(internals->active_slaves) - 1));
89 
90 	internals->active_slaves[internals->active_slave_count] = port_id;
91 	internals->active_slave_count++;
92 
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);
97 }
98 
99 void
100 deactivate_slave(struct rte_eth_dev *eth_dev, uint16_t port_id)
101 {
102 	uint16_t slave_pos;
103 	struct bond_dev_private *internals = eth_dev->data->dev_private;
104 	uint16_t active_count = internals->active_slave_count;
105 
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);
112 
113 	slave_pos = find_slave_by_id(internals->active_slaves, active_count,
114 			port_id);
115 
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) {
119 		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]));
124 	}
125 
126 	RTE_ASSERT(active_count < RTE_DIM(internals->active_slaves));
127 	internals->active_slave_count = active_count;
128 
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);
137 		}
138 	}
139 }
140 
141 int
142 rte_eth_bond_create(const char *name, uint8_t mode, uint8_t socket_id)
143 {
144 	struct bond_dev_private *internals;
145 	char devargs[52];
146 	uint16_t port_id;
147 	int ret;
148 
149 	if (name == NULL) {
150 		RTE_BOND_LOG(ERR, "Invalid name specified");
151 		return -EINVAL;
152 	}
153 
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))
157 		return -ENOMEM;
158 
159 	ret = rte_vdev_init(name, devargs);
160 	if (ret)
161 		return -ENOMEM;
162 
163 	ret = rte_eth_dev_get_port_by_name(name, &port_id);
164 	RTE_ASSERT(!ret);
165 
166 	/*
167 	 * To make bond_ethdev_configure() happy we need to free the
168 	 * internals->kvlist here.
169 	 *
170 	 * Also see comment in bond_ethdev_configure().
171 	 */
172 	internals = rte_eth_devices[port_id].data->dev_private;
173 	rte_kvargs_free(internals->kvlist);
174 	internals->kvlist = NULL;
175 
176 	return port_id;
177 }
178 
179 int
180 rte_eth_bond_free(const char *name)
181 {
182 	return rte_vdev_uninit(name);
183 }
184 
185 static int
186 slave_vlan_filter_set(uint16_t bonded_port_id, uint16_t slave_port_id)
187 {
188 	struct rte_eth_dev *bonded_eth_dev;
189 	struct bond_dev_private *internals;
190 	int found;
191 	int res = 0;
192 	uint64_t slab = 0;
193 	uint32_t pos = 0;
194 	uint16_t first;
195 
196 	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
197 	if ((bonded_eth_dev->data->dev_conf.rxmode.offloads &
198 			DEV_RX_OFFLOAD_VLAN_FILTER) == 0)
199 		return 0;
200 
201 	internals = bonded_eth_dev->data->dev_private;
202 	found = rte_bitmap_scan(internals->vlan_filter_bmp, &pos, &slab);
203 	first = pos;
204 
205 	if (!found)
206 		return 0;
207 
208 	do {
209 		uint32_t i;
210 		uint64_t mask;
211 
212 		for (i = 0, mask = 1;
213 		     i < RTE_BITMAP_SLAB_BIT_SIZE;
214 		     i ++, mask <<= 1) {
215 			if (unlikely(slab & mask)) {
216 				uint16_t vlan_id = pos + i;
217 
218 				res = rte_eth_dev_vlan_filter(slave_port_id,
219 							      vlan_id, 1);
220 			}
221 		}
222 		found = rte_bitmap_scan(internals->vlan_filter_bmp,
223 					&pos, &slab);
224 	} while (found && first != pos && res == 0);
225 
226 	return res;
227 }
228 
229 static int
230 slave_rte_flow_prepare(uint16_t slave_id, struct bond_dev_private *internals)
231 {
232 	struct rte_flow *flow;
233 	struct rte_flow_error ferror;
234 	uint16_t slave_port_id = internals->slaves[slave_id].port_id;
235 
236 	if (internals->flow_isolated_valid != 0) {
237 		rte_eth_dev_stop(slave_port_id);
238 		if (rte_flow_isolate(slave_port_id, internals->flow_isolated,
239 		    &ferror)) {
240 			RTE_BOND_LOG(ERR, "rte_flow_isolate failed for slave"
241 				     " %d: %s", slave_id, ferror.message ?
242 				     ferror.message : "(no stated reason)");
243 			return -1;
244 		}
245 	}
246 	TAILQ_FOREACH(flow, &internals->flow_list, next) {
247 		flow->flows[slave_id] = rte_flow_create(slave_port_id,
248 							&flow->fd->attr,
249 							flow->fd->items,
250 							flow->fd->actions,
251 							&ferror);
252 		if (flow->flows[slave_id] == NULL) {
253 			RTE_BOND_LOG(ERR, "Cannot create flow for slave"
254 				     " %d: %s", slave_id,
255 				     ferror.message ? ferror.message :
256 				     "(no stated reason)");
257 			/* Destroy successful bond flows from the slave */
258 			TAILQ_FOREACH(flow, &internals->flow_list, next) {
259 				if (flow->flows[slave_id] != NULL) {
260 					rte_flow_destroy(slave_port_id,
261 							 flow->flows[slave_id],
262 							 &ferror);
263 					flow->flows[slave_id] = NULL;
264 				}
265 			}
266 			return -1;
267 		}
268 	}
269 	return 0;
270 }
271 
272 static void
273 eth_bond_slave_inherit_dev_info_rx_first(struct bond_dev_private *internals,
274 					 const struct rte_eth_dev_info *di)
275 {
276 	struct rte_eth_rxconf *rxconf_i = &internals->default_rxconf;
277 
278 	internals->reta_size = di->reta_size;
279 
280 	/* Inherit Rx offload capabilities from the first slave device */
281 	internals->rx_offload_capa = di->rx_offload_capa;
282 	internals->rx_queue_offload_capa = di->rx_queue_offload_capa;
283 	internals->flow_type_rss_offloads = di->flow_type_rss_offloads;
284 
285 	/* Inherit maximum Rx packet size from the first slave device */
286 	internals->candidate_max_rx_pktlen = di->max_rx_pktlen;
287 
288 	/* Inherit default Rx queue settings from the first slave device */
289 	memcpy(rxconf_i, &di->default_rxconf, sizeof(*rxconf_i));
290 
291 	/*
292 	 * Turn off descriptor prefetch and writeback by default for all
293 	 * slave devices. Applications may tweak this setting if need be.
294 	 */
295 	rxconf_i->rx_thresh.pthresh = 0;
296 	rxconf_i->rx_thresh.hthresh = 0;
297 	rxconf_i->rx_thresh.wthresh = 0;
298 
299 	/* Setting this to zero should effectively enable default values */
300 	rxconf_i->rx_free_thresh = 0;
301 
302 	/* Disable deferred start by default for all slave devices */
303 	rxconf_i->rx_deferred_start = 0;
304 }
305 
306 static void
307 eth_bond_slave_inherit_dev_info_tx_first(struct bond_dev_private *internals,
308 					 const struct rte_eth_dev_info *di)
309 {
310 	struct rte_eth_txconf *txconf_i = &internals->default_txconf;
311 
312 	/* Inherit Tx offload capabilities from the first slave device */
313 	internals->tx_offload_capa = di->tx_offload_capa;
314 	internals->tx_queue_offload_capa = di->tx_queue_offload_capa;
315 
316 	/* Inherit default Tx queue settings from the first slave device */
317 	memcpy(txconf_i, &di->default_txconf, sizeof(*txconf_i));
318 
319 	/*
320 	 * Turn off descriptor prefetch and writeback by default for all
321 	 * slave devices. Applications may tweak this setting if need be.
322 	 */
323 	txconf_i->tx_thresh.pthresh = 0;
324 	txconf_i->tx_thresh.hthresh = 0;
325 	txconf_i->tx_thresh.wthresh = 0;
326 
327 	/*
328 	 * Setting these parameters to zero assumes that default
329 	 * values will be configured implicitly by slave devices.
330 	 */
331 	txconf_i->tx_free_thresh = 0;
332 	txconf_i->tx_rs_thresh = 0;
333 
334 	/* Disable deferred start by default for all slave devices */
335 	txconf_i->tx_deferred_start = 0;
336 }
337 
338 static void
339 eth_bond_slave_inherit_dev_info_rx_next(struct bond_dev_private *internals,
340 					const struct rte_eth_dev_info *di)
341 {
342 	struct rte_eth_rxconf *rxconf_i = &internals->default_rxconf;
343 	const struct rte_eth_rxconf *rxconf = &di->default_rxconf;
344 
345 	internals->rx_offload_capa &= di->rx_offload_capa;
346 	internals->rx_queue_offload_capa &= di->rx_queue_offload_capa;
347 	internals->flow_type_rss_offloads &= di->flow_type_rss_offloads;
348 
349 	/*
350 	 * If at least one slave device suggests enabling this
351 	 * setting by default, enable it for all slave devices
352 	 * since disabling it may not be necessarily supported.
353 	 */
354 	if (rxconf->rx_drop_en == 1)
355 		rxconf_i->rx_drop_en = 1;
356 
357 	/*
358 	 * Adding a new slave device may cause some of previously inherited
359 	 * offloads to be withdrawn from the internal rx_queue_offload_capa
360 	 * value. Thus, the new internal value of default Rx queue offloads
361 	 * has to be masked by rx_queue_offload_capa to make sure that only
362 	 * commonly supported offloads are preserved from both the previous
363 	 * value and the value being inhereted from the new slave device.
364 	 */
365 	rxconf_i->offloads = (rxconf_i->offloads | rxconf->offloads) &
366 			     internals->rx_queue_offload_capa;
367 
368 	/*
369 	 * RETA size is GCD of all slaves RETA sizes, so, if all sizes will be
370 	 * the power of 2, the lower one is GCD
371 	 */
372 	if (internals->reta_size > di->reta_size)
373 		internals->reta_size = di->reta_size;
374 
375 	if (!internals->max_rx_pktlen &&
376 	    di->max_rx_pktlen < internals->candidate_max_rx_pktlen)
377 		internals->candidate_max_rx_pktlen = di->max_rx_pktlen;
378 }
379 
380 static void
381 eth_bond_slave_inherit_dev_info_tx_next(struct bond_dev_private *internals,
382 					const struct rte_eth_dev_info *di)
383 {
384 	struct rte_eth_txconf *txconf_i = &internals->default_txconf;
385 	const struct rte_eth_txconf *txconf = &di->default_txconf;
386 
387 	internals->tx_offload_capa &= di->tx_offload_capa;
388 	internals->tx_queue_offload_capa &= di->tx_queue_offload_capa;
389 
390 	/*
391 	 * Adding a new slave device may cause some of previously inherited
392 	 * offloads to be withdrawn from the internal tx_queue_offload_capa
393 	 * value. Thus, the new internal value of default Tx queue offloads
394 	 * has to be masked by tx_queue_offload_capa to make sure that only
395 	 * commonly supported offloads are preserved from both the previous
396 	 * value and the value being inhereted from the new slave device.
397 	 */
398 	txconf_i->offloads = (txconf_i->offloads | txconf->offloads) &
399 			     internals->tx_queue_offload_capa;
400 }
401 
402 static void
403 eth_bond_slave_inherit_desc_lim_first(struct rte_eth_desc_lim *bond_desc_lim,
404 		const struct rte_eth_desc_lim *slave_desc_lim)
405 {
406 	memcpy(bond_desc_lim, slave_desc_lim, sizeof(*bond_desc_lim));
407 }
408 
409 static int
410 eth_bond_slave_inherit_desc_lim_next(struct rte_eth_desc_lim *bond_desc_lim,
411 		const struct rte_eth_desc_lim *slave_desc_lim)
412 {
413 	bond_desc_lim->nb_max = RTE_MIN(bond_desc_lim->nb_max,
414 					slave_desc_lim->nb_max);
415 	bond_desc_lim->nb_min = RTE_MAX(bond_desc_lim->nb_min,
416 					slave_desc_lim->nb_min);
417 	bond_desc_lim->nb_align = RTE_MAX(bond_desc_lim->nb_align,
418 					  slave_desc_lim->nb_align);
419 
420 	if (bond_desc_lim->nb_min > bond_desc_lim->nb_max ||
421 	    bond_desc_lim->nb_align > bond_desc_lim->nb_max) {
422 		RTE_BOND_LOG(ERR, "Failed to inherit descriptor limits");
423 		return -EINVAL;
424 	}
425 
426 	/* Treat maximum number of segments equal to 0 as unspecified */
427 	if (slave_desc_lim->nb_seg_max != 0 &&
428 	    (bond_desc_lim->nb_seg_max == 0 ||
429 	     slave_desc_lim->nb_seg_max < bond_desc_lim->nb_seg_max))
430 		bond_desc_lim->nb_seg_max = slave_desc_lim->nb_seg_max;
431 	if (slave_desc_lim->nb_mtu_seg_max != 0 &&
432 	    (bond_desc_lim->nb_mtu_seg_max == 0 ||
433 	     slave_desc_lim->nb_mtu_seg_max < bond_desc_lim->nb_mtu_seg_max))
434 		bond_desc_lim->nb_mtu_seg_max = slave_desc_lim->nb_mtu_seg_max;
435 
436 	return 0;
437 }
438 
439 static int
440 __eth_bond_slave_add_lock_free(uint16_t bonded_port_id, uint16_t slave_port_id)
441 {
442 	struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
443 	struct bond_dev_private *internals;
444 	struct rte_eth_link link_props;
445 	struct rte_eth_dev_info dev_info;
446 
447 	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
448 	internals = bonded_eth_dev->data->dev_private;
449 
450 	if (valid_slave_port_id(slave_port_id, internals->mode) != 0)
451 		return -1;
452 
453 	slave_eth_dev = &rte_eth_devices[slave_port_id];
454 	if (slave_eth_dev->data->dev_flags & RTE_ETH_DEV_BONDED_SLAVE) {
455 		RTE_BOND_LOG(ERR, "Slave device is already a slave of a bonded device");
456 		return -1;
457 	}
458 
459 	rte_eth_dev_info_get(slave_port_id, &dev_info);
460 	if (dev_info.max_rx_pktlen < internals->max_rx_pktlen) {
461 		RTE_BOND_LOG(ERR, "Slave (port %u) max_rx_pktlen too small",
462 			     slave_port_id);
463 		return -1;
464 	}
465 
466 	slave_add(internals, slave_eth_dev);
467 
468 	/* We need to store slaves reta_size to be able to synchronize RETA for all
469 	 * slave devices even if its sizes are different.
470 	 */
471 	internals->slaves[internals->slave_count].reta_size = dev_info.reta_size;
472 
473 	if (internals->slave_count < 1) {
474 		/* if MAC is not user defined then use MAC of first slave add to
475 		 * bonded device */
476 		if (!internals->user_defined_mac) {
477 			if (mac_address_set(bonded_eth_dev,
478 					    slave_eth_dev->data->mac_addrs)) {
479 				RTE_BOND_LOG(ERR, "Failed to set MAC address");
480 				return -1;
481 			}
482 		}
483 
484 		/* Inherit eth dev link properties from first slave */
485 		link_properties_set(bonded_eth_dev,
486 				&(slave_eth_dev->data->dev_link));
487 
488 		/* Make primary slave */
489 		internals->primary_port = slave_port_id;
490 		internals->current_primary_port = slave_port_id;
491 
492 		/* Inherit queues settings from first slave */
493 		internals->nb_rx_queues = slave_eth_dev->data->nb_rx_queues;
494 		internals->nb_tx_queues = slave_eth_dev->data->nb_tx_queues;
495 
496 		eth_bond_slave_inherit_dev_info_rx_first(internals, &dev_info);
497 		eth_bond_slave_inherit_dev_info_tx_first(internals, &dev_info);
498 
499 		eth_bond_slave_inherit_desc_lim_first(&internals->rx_desc_lim,
500 						      &dev_info.rx_desc_lim);
501 		eth_bond_slave_inherit_desc_lim_first(&internals->tx_desc_lim,
502 						      &dev_info.tx_desc_lim);
503 	} else {
504 		int ret;
505 
506 		eth_bond_slave_inherit_dev_info_rx_next(internals, &dev_info);
507 		eth_bond_slave_inherit_dev_info_tx_next(internals, &dev_info);
508 
509 		ret = eth_bond_slave_inherit_desc_lim_next(
510 				&internals->rx_desc_lim, &dev_info.rx_desc_lim);
511 		if (ret != 0)
512 			return ret;
513 
514 		ret = eth_bond_slave_inherit_desc_lim_next(
515 				&internals->tx_desc_lim, &dev_info.tx_desc_lim);
516 		if (ret != 0)
517 			return ret;
518 	}
519 
520 	bonded_eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf &=
521 			internals->flow_type_rss_offloads;
522 
523 	if (slave_rte_flow_prepare(internals->slave_count, internals) != 0) {
524 		RTE_BOND_LOG(ERR, "Failed to prepare new slave flows: port=%d",
525 			     slave_port_id);
526 		return -1;
527 	}
528 
529 	/* Add additional MAC addresses to the slave */
530 	if (slave_add_mac_addresses(bonded_eth_dev, slave_port_id) != 0) {
531 		RTE_BOND_LOG(ERR, "Failed to add mac address(es) to slave %hu",
532 				slave_port_id);
533 		return -1;
534 	}
535 
536 	internals->slave_count++;
537 
538 	if (bonded_eth_dev->data->dev_started) {
539 		if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
540 			internals->slave_count--;
541 			RTE_BOND_LOG(ERR, "rte_bond_slaves_configure: port=%d",
542 					slave_port_id);
543 			return -1;
544 		}
545 	}
546 
547 	/* Add slave details to bonded device */
548 	slave_eth_dev->data->dev_flags |= RTE_ETH_DEV_BONDED_SLAVE;
549 
550 	/* Update all slave devices MACs */
551 	mac_address_slaves_update(bonded_eth_dev);
552 
553 	/* Register link status change callback with bonded device pointer as
554 	 * argument*/
555 	rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
556 			bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
557 
558 	/* If bonded device is started then we can add the slave to our active
559 	 * slave array */
560 	if (bonded_eth_dev->data->dev_started) {
561 		rte_eth_link_get_nowait(slave_port_id, &link_props);
562 
563 		 if (link_props.link_status == ETH_LINK_UP) {
564 			if (internals->active_slave_count == 0 &&
565 			    !internals->user_defined_primary_port)
566 				bond_ethdev_primary_set(internals,
567 							slave_port_id);
568 		}
569 	}
570 
571 	slave_vlan_filter_set(bonded_port_id, slave_port_id);
572 
573 	return 0;
574 
575 }
576 
577 int
578 rte_eth_bond_slave_add(uint16_t bonded_port_id, uint16_t slave_port_id)
579 {
580 	struct rte_eth_dev *bonded_eth_dev;
581 	struct bond_dev_private *internals;
582 
583 	int retval;
584 
585 	/* Verify that port id's are valid bonded and slave ports */
586 	if (valid_bonded_port_id(bonded_port_id) != 0)
587 		return -1;
588 
589 	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
590 	internals = bonded_eth_dev->data->dev_private;
591 
592 	rte_spinlock_lock(&internals->lock);
593 
594 	retval = __eth_bond_slave_add_lock_free(bonded_port_id, slave_port_id);
595 
596 	rte_spinlock_unlock(&internals->lock);
597 
598 	return retval;
599 }
600 
601 static int
602 __eth_bond_slave_remove_lock_free(uint16_t bonded_port_id,
603 				   uint16_t slave_port_id)
604 {
605 	struct rte_eth_dev *bonded_eth_dev;
606 	struct bond_dev_private *internals;
607 	struct rte_eth_dev *slave_eth_dev;
608 	struct rte_flow_error flow_error;
609 	struct rte_flow *flow;
610 	int i, slave_idx;
611 
612 	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
613 	internals = bonded_eth_dev->data->dev_private;
614 
615 	if (valid_slave_port_id(slave_port_id, internals->mode) < 0)
616 		return -1;
617 
618 	/* first remove from active slave list */
619 	slave_idx = find_slave_by_id(internals->active_slaves,
620 		internals->active_slave_count, slave_port_id);
621 
622 	if (slave_idx < internals->active_slave_count)
623 		deactivate_slave(bonded_eth_dev, slave_port_id);
624 
625 	slave_idx = -1;
626 	/* now find in slave list */
627 	for (i = 0; i < internals->slave_count; i++)
628 		if (internals->slaves[i].port_id == slave_port_id) {
629 			slave_idx = i;
630 			break;
631 		}
632 
633 	if (slave_idx < 0) {
634 		RTE_BOND_LOG(ERR, "Couldn't find slave in port list, slave count %d",
635 				internals->slave_count);
636 		return -1;
637 	}
638 
639 	/* Un-register link status change callback with bonded device pointer as
640 	 * argument*/
641 	rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
642 			bond_ethdev_lsc_event_callback,
643 			&rte_eth_devices[bonded_port_id].data->port_id);
644 
645 	/* Restore original MAC address of slave device */
646 	rte_eth_dev_default_mac_addr_set(slave_port_id,
647 			&(internals->slaves[slave_idx].persisted_mac_addr));
648 
649 	/* remove additional MAC addresses from the slave */
650 	slave_remove_mac_addresses(bonded_eth_dev, slave_port_id);
651 
652 	/*
653 	 * Remove bond device flows from slave device.
654 	 * Note: don't restore flow isolate mode.
655 	 */
656 	TAILQ_FOREACH(flow, &internals->flow_list, next) {
657 		if (flow->flows[slave_idx] != NULL) {
658 			rte_flow_destroy(slave_port_id, flow->flows[slave_idx],
659 					 &flow_error);
660 			flow->flows[slave_idx] = NULL;
661 		}
662 	}
663 
664 	slave_eth_dev = &rte_eth_devices[slave_port_id];
665 	slave_remove(internals, slave_eth_dev);
666 	slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
667 
668 	/*  first slave in the active list will be the primary by default,
669 	 *  otherwise use first device in list */
670 	if (internals->current_primary_port == slave_port_id) {
671 		if (internals->active_slave_count > 0)
672 			internals->current_primary_port = internals->active_slaves[0];
673 		else if (internals->slave_count > 0)
674 			internals->current_primary_port = internals->slaves[0].port_id;
675 		else
676 			internals->primary_port = 0;
677 	}
678 
679 	if (internals->active_slave_count < 1) {
680 		/* if no slaves are any longer attached to bonded device and MAC is not
681 		 * user defined then clear MAC of bonded device as it will be reset
682 		 * when a new slave is added */
683 		if (internals->slave_count < 1 && !internals->user_defined_mac)
684 			memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
685 					sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
686 	}
687 	if (internals->slave_count == 0) {
688 		internals->rx_offload_capa = 0;
689 		internals->tx_offload_capa = 0;
690 		internals->rx_queue_offload_capa = 0;
691 		internals->tx_queue_offload_capa = 0;
692 		internals->flow_type_rss_offloads = ETH_RSS_PROTO_MASK;
693 		internals->reta_size = 0;
694 		internals->candidate_max_rx_pktlen = 0;
695 		internals->max_rx_pktlen = 0;
696 	}
697 	return 0;
698 }
699 
700 int
701 rte_eth_bond_slave_remove(uint16_t bonded_port_id, uint16_t slave_port_id)
702 {
703 	struct rte_eth_dev *bonded_eth_dev;
704 	struct bond_dev_private *internals;
705 	int retval;
706 
707 	if (valid_bonded_port_id(bonded_port_id) != 0)
708 		return -1;
709 
710 	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
711 	internals = bonded_eth_dev->data->dev_private;
712 
713 	rte_spinlock_lock(&internals->lock);
714 
715 	retval = __eth_bond_slave_remove_lock_free(bonded_port_id, slave_port_id);
716 
717 	rte_spinlock_unlock(&internals->lock);
718 
719 	return retval;
720 }
721 
722 int
723 rte_eth_bond_mode_set(uint16_t bonded_port_id, uint8_t mode)
724 {
725 	struct rte_eth_dev *bonded_eth_dev;
726 
727 	if (valid_bonded_port_id(bonded_port_id) != 0)
728 		return -1;
729 
730 	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
731 
732 	if (check_for_master_bonded_ethdev(bonded_eth_dev) != 0 &&
733 			mode == BONDING_MODE_8023AD)
734 		return -1;
735 
736 	return bond_ethdev_mode_set(bonded_eth_dev, mode);
737 }
738 
739 int
740 rte_eth_bond_mode_get(uint16_t bonded_port_id)
741 {
742 	struct bond_dev_private *internals;
743 
744 	if (valid_bonded_port_id(bonded_port_id) != 0)
745 		return -1;
746 
747 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
748 
749 	return internals->mode;
750 }
751 
752 int
753 rte_eth_bond_primary_set(uint16_t bonded_port_id, uint16_t slave_port_id)
754 {
755 	struct bond_dev_private *internals;
756 
757 	if (valid_bonded_port_id(bonded_port_id) != 0)
758 		return -1;
759 
760 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
761 
762 	if (valid_slave_port_id(slave_port_id, internals->mode) != 0)
763 		return -1;
764 
765 	internals->user_defined_primary_port = 1;
766 	internals->primary_port = slave_port_id;
767 
768 	bond_ethdev_primary_set(internals, slave_port_id);
769 
770 	return 0;
771 }
772 
773 int
774 rte_eth_bond_primary_get(uint16_t bonded_port_id)
775 {
776 	struct bond_dev_private *internals;
777 
778 	if (valid_bonded_port_id(bonded_port_id) != 0)
779 		return -1;
780 
781 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
782 
783 	if (internals->slave_count < 1)
784 		return -1;
785 
786 	return internals->current_primary_port;
787 }
788 
789 int
790 rte_eth_bond_slaves_get(uint16_t bonded_port_id, uint16_t slaves[],
791 			uint16_t len)
792 {
793 	struct bond_dev_private *internals;
794 	uint8_t i;
795 
796 	if (valid_bonded_port_id(bonded_port_id) != 0)
797 		return -1;
798 
799 	if (slaves == NULL)
800 		return -1;
801 
802 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
803 
804 	if (internals->slave_count > len)
805 		return -1;
806 
807 	for (i = 0; i < internals->slave_count; i++)
808 		slaves[i] = internals->slaves[i].port_id;
809 
810 	return internals->slave_count;
811 }
812 
813 int
814 rte_eth_bond_active_slaves_get(uint16_t bonded_port_id, uint16_t slaves[],
815 		uint16_t len)
816 {
817 	struct bond_dev_private *internals;
818 
819 	if (valid_bonded_port_id(bonded_port_id) != 0)
820 		return -1;
821 
822 	if (slaves == NULL)
823 		return -1;
824 
825 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
826 
827 	if (internals->active_slave_count > len)
828 		return -1;
829 
830 	memcpy(slaves, internals->active_slaves,
831 	internals->active_slave_count * sizeof(internals->active_slaves[0]));
832 
833 	return internals->active_slave_count;
834 }
835 
836 int
837 rte_eth_bond_mac_address_set(uint16_t bonded_port_id,
838 		struct ether_addr *mac_addr)
839 {
840 	struct rte_eth_dev *bonded_eth_dev;
841 	struct bond_dev_private *internals;
842 
843 	if (valid_bonded_port_id(bonded_port_id) != 0)
844 		return -1;
845 
846 	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
847 	internals = bonded_eth_dev->data->dev_private;
848 
849 	/* Set MAC Address of Bonded Device */
850 	if (mac_address_set(bonded_eth_dev, mac_addr))
851 		return -1;
852 
853 	internals->user_defined_mac = 1;
854 
855 	/* Update all slave devices MACs*/
856 	if (internals->slave_count > 0)
857 		return mac_address_slaves_update(bonded_eth_dev);
858 
859 	return 0;
860 }
861 
862 int
863 rte_eth_bond_mac_address_reset(uint16_t bonded_port_id)
864 {
865 	struct rte_eth_dev *bonded_eth_dev;
866 	struct bond_dev_private *internals;
867 
868 	if (valid_bonded_port_id(bonded_port_id) != 0)
869 		return -1;
870 
871 	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
872 	internals = bonded_eth_dev->data->dev_private;
873 
874 	internals->user_defined_mac = 0;
875 
876 	if (internals->slave_count > 0) {
877 		int slave_port;
878 		/* Get the primary slave location based on the primary port
879 		 * number as, while slave_add(), we will keep the primary
880 		 * slave based on slave_count,but not based on the primary port.
881 		 */
882 		for (slave_port = 0; slave_port < internals->slave_count;
883 		     slave_port++) {
884 			if (internals->slaves[slave_port].port_id ==
885 			    internals->primary_port)
886 				break;
887 		}
888 
889 		/* Set MAC Address of Bonded Device */
890 		if (mac_address_set(bonded_eth_dev,
891 			&internals->slaves[slave_port].persisted_mac_addr)
892 				!= 0) {
893 			RTE_BOND_LOG(ERR, "Failed to set MAC address on bonded device");
894 			return -1;
895 		}
896 		/* Update all slave devices MAC addresses */
897 		return mac_address_slaves_update(bonded_eth_dev);
898 	}
899 	/* No need to update anything as no slaves present */
900 	return 0;
901 }
902 
903 int
904 rte_eth_bond_xmit_policy_set(uint16_t bonded_port_id, uint8_t policy)
905 {
906 	struct bond_dev_private *internals;
907 
908 	if (valid_bonded_port_id(bonded_port_id) != 0)
909 		return -1;
910 
911 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
912 
913 	switch (policy) {
914 	case BALANCE_XMIT_POLICY_LAYER2:
915 		internals->balance_xmit_policy = policy;
916 		internals->burst_xmit_hash = burst_xmit_l2_hash;
917 		break;
918 	case BALANCE_XMIT_POLICY_LAYER23:
919 		internals->balance_xmit_policy = policy;
920 		internals->burst_xmit_hash = burst_xmit_l23_hash;
921 		break;
922 	case BALANCE_XMIT_POLICY_LAYER34:
923 		internals->balance_xmit_policy = policy;
924 		internals->burst_xmit_hash = burst_xmit_l34_hash;
925 		break;
926 
927 	default:
928 		return -1;
929 	}
930 	return 0;
931 }
932 
933 int
934 rte_eth_bond_xmit_policy_get(uint16_t bonded_port_id)
935 {
936 	struct bond_dev_private *internals;
937 
938 	if (valid_bonded_port_id(bonded_port_id) != 0)
939 		return -1;
940 
941 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
942 
943 	return internals->balance_xmit_policy;
944 }
945 
946 int
947 rte_eth_bond_link_monitoring_set(uint16_t bonded_port_id, uint32_t internal_ms)
948 {
949 	struct bond_dev_private *internals;
950 
951 	if (valid_bonded_port_id(bonded_port_id) != 0)
952 		return -1;
953 
954 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
955 	internals->link_status_polling_interval_ms = internal_ms;
956 
957 	return 0;
958 }
959 
960 int
961 rte_eth_bond_link_monitoring_get(uint16_t bonded_port_id)
962 {
963 	struct bond_dev_private *internals;
964 
965 	if (valid_bonded_port_id(bonded_port_id) != 0)
966 		return -1;
967 
968 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
969 
970 	return internals->link_status_polling_interval_ms;
971 }
972 
973 int
974 rte_eth_bond_link_down_prop_delay_set(uint16_t bonded_port_id,
975 				       uint32_t delay_ms)
976 
977 {
978 	struct bond_dev_private *internals;
979 
980 	if (valid_bonded_port_id(bonded_port_id) != 0)
981 		return -1;
982 
983 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
984 	internals->link_down_delay_ms = delay_ms;
985 
986 	return 0;
987 }
988 
989 int
990 rte_eth_bond_link_down_prop_delay_get(uint16_t bonded_port_id)
991 {
992 	struct bond_dev_private *internals;
993 
994 	if (valid_bonded_port_id(bonded_port_id) != 0)
995 		return -1;
996 
997 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
998 
999 	return internals->link_down_delay_ms;
1000 }
1001 
1002 int
1003 rte_eth_bond_link_up_prop_delay_set(uint16_t bonded_port_id, uint32_t delay_ms)
1004 
1005 {
1006 	struct bond_dev_private *internals;
1007 
1008 	if (valid_bonded_port_id(bonded_port_id) != 0)
1009 		return -1;
1010 
1011 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
1012 	internals->link_up_delay_ms = delay_ms;
1013 
1014 	return 0;
1015 }
1016 
1017 int
1018 rte_eth_bond_link_up_prop_delay_get(uint16_t bonded_port_id)
1019 {
1020 	struct bond_dev_private *internals;
1021 
1022 	if (valid_bonded_port_id(bonded_port_id) != 0)
1023 		return -1;
1024 
1025 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
1026 
1027 	return internals->link_up_delay_ms;
1028 }
1029