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