xref: /dpdk/drivers/net/bonding/rte_eth_bond_api.c (revision c0d02207)
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 int
273 __eth_bond_slave_add_lock_free(uint16_t bonded_port_id, uint16_t slave_port_id)
274 {
275 	struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
276 	struct bond_dev_private *internals;
277 	struct rte_eth_link link_props;
278 	struct rte_eth_dev_info dev_info;
279 
280 	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
281 	internals = bonded_eth_dev->data->dev_private;
282 
283 	if (valid_slave_port_id(slave_port_id, internals->mode) != 0)
284 		return -1;
285 
286 	slave_eth_dev = &rte_eth_devices[slave_port_id];
287 	if (slave_eth_dev->data->dev_flags & RTE_ETH_DEV_BONDED_SLAVE) {
288 		RTE_BOND_LOG(ERR, "Slave device is already a slave of a bonded device");
289 		return -1;
290 	}
291 
292 	rte_eth_dev_info_get(slave_port_id, &dev_info);
293 	if (dev_info.max_rx_pktlen < internals->max_rx_pktlen) {
294 		RTE_BOND_LOG(ERR, "Slave (port %u) max_rx_pktlen too small",
295 			     slave_port_id);
296 		return -1;
297 	}
298 
299 	slave_add(internals, slave_eth_dev);
300 
301 	/* We need to store slaves reta_size to be able to synchronize RETA for all
302 	 * slave devices even if its sizes are different.
303 	 */
304 	internals->slaves[internals->slave_count].reta_size = dev_info.reta_size;
305 
306 	if (internals->slave_count < 1) {
307 		/* if MAC is not user defined then use MAC of first slave add to
308 		 * bonded device */
309 		if (!internals->user_defined_mac) {
310 			if (mac_address_set(bonded_eth_dev,
311 					    slave_eth_dev->data->mac_addrs)) {
312 				RTE_BOND_LOG(ERR, "Failed to set MAC address");
313 				return -1;
314 			}
315 		}
316 
317 		/* Inherit eth dev link properties from first slave */
318 		link_properties_set(bonded_eth_dev,
319 				&(slave_eth_dev->data->dev_link));
320 
321 		/* Make primary slave */
322 		internals->primary_port = slave_port_id;
323 		internals->current_primary_port = slave_port_id;
324 
325 		/* Inherit queues settings from first slave */
326 		internals->nb_rx_queues = slave_eth_dev->data->nb_rx_queues;
327 		internals->nb_tx_queues = slave_eth_dev->data->nb_tx_queues;
328 
329 		internals->reta_size = dev_info.reta_size;
330 
331 		/* Take the first dev's offload capabilities */
332 		internals->rx_offload_capa = dev_info.rx_offload_capa;
333 		internals->tx_offload_capa = dev_info.tx_offload_capa;
334 		internals->rx_queue_offload_capa = dev_info.rx_queue_offload_capa;
335 		internals->tx_queue_offload_capa = dev_info.tx_queue_offload_capa;
336 		internals->flow_type_rss_offloads = dev_info.flow_type_rss_offloads;
337 
338 		/* Inherit first slave's max rx packet size */
339 		internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
340 
341 	} else {
342 		internals->rx_offload_capa &= dev_info.rx_offload_capa;
343 		internals->tx_offload_capa &= dev_info.tx_offload_capa;
344 		internals->rx_queue_offload_capa &= dev_info.rx_queue_offload_capa;
345 		internals->tx_queue_offload_capa &= dev_info.tx_queue_offload_capa;
346 		internals->flow_type_rss_offloads &= dev_info.flow_type_rss_offloads;
347 
348 		if (link_properties_valid(bonded_eth_dev,
349 				&slave_eth_dev->data->dev_link) != 0) {
350 			RTE_BOND_LOG(ERR, "Invalid link properties for slave %d"
351 					" in bonding mode %d", slave_port_id,
352 					internals->mode);
353 			return -1;
354 		}
355 
356 		/* RETA size is GCD of all slaves RETA sizes, so, if all sizes will be
357 		 * the power of 2, the lower one is GCD
358 		 */
359 		if (internals->reta_size > dev_info.reta_size)
360 			internals->reta_size = dev_info.reta_size;
361 
362 		if (!internals->max_rx_pktlen &&
363 		    dev_info.max_rx_pktlen < internals->candidate_max_rx_pktlen)
364 			internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
365 	}
366 
367 	bonded_eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf &=
368 			internals->flow_type_rss_offloads;
369 
370 	if (slave_rte_flow_prepare(internals->slave_count, internals) != 0) {
371 		RTE_BOND_LOG(ERR, "Failed to prepare new slave flows: port=%d",
372 			     slave_port_id);
373 		return -1;
374 	}
375 
376 	/* Add additional MAC addresses to the slave */
377 	if (slave_add_mac_addresses(bonded_eth_dev, slave_port_id) != 0) {
378 		RTE_BOND_LOG(ERR, "Failed to add mac address(es) to slave %hu",
379 				slave_port_id);
380 		return -1;
381 	}
382 
383 	internals->slave_count++;
384 
385 	if (bonded_eth_dev->data->dev_started) {
386 		if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
387 			internals->slave_count--;
388 			RTE_BOND_LOG(ERR, "rte_bond_slaves_configure: port=%d",
389 					slave_port_id);
390 			return -1;
391 		}
392 	}
393 
394 	/* Add slave details to bonded device */
395 	slave_eth_dev->data->dev_flags |= RTE_ETH_DEV_BONDED_SLAVE;
396 
397 	/* Update all slave devices MACs */
398 	mac_address_slaves_update(bonded_eth_dev);
399 
400 	/* Register link status change callback with bonded device pointer as
401 	 * argument*/
402 	rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
403 			bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
404 
405 	/* If bonded device is started then we can add the slave to our active
406 	 * slave array */
407 	if (bonded_eth_dev->data->dev_started) {
408 		rte_eth_link_get_nowait(slave_port_id, &link_props);
409 
410 		 if (link_props.link_status == ETH_LINK_UP) {
411 			if (internals->active_slave_count == 0 &&
412 			    !internals->user_defined_primary_port)
413 				bond_ethdev_primary_set(internals,
414 							slave_port_id);
415 
416 			if (find_slave_by_id(internals->active_slaves,
417 					     internals->active_slave_count,
418 					     slave_port_id) == internals->active_slave_count)
419 				activate_slave(bonded_eth_dev, slave_port_id);
420 		}
421 	}
422 
423 	slave_vlan_filter_set(bonded_port_id, slave_port_id);
424 
425 	return 0;
426 
427 }
428 
429 int
430 rte_eth_bond_slave_add(uint16_t bonded_port_id, uint16_t slave_port_id)
431 {
432 	struct rte_eth_dev *bonded_eth_dev;
433 	struct bond_dev_private *internals;
434 
435 	int retval;
436 
437 	/* Verify that port id's are valid bonded and slave ports */
438 	if (valid_bonded_port_id(bonded_port_id) != 0)
439 		return -1;
440 
441 	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
442 	internals = bonded_eth_dev->data->dev_private;
443 
444 	rte_spinlock_lock(&internals->lock);
445 
446 	retval = __eth_bond_slave_add_lock_free(bonded_port_id, slave_port_id);
447 
448 	rte_spinlock_unlock(&internals->lock);
449 
450 	return retval;
451 }
452 
453 static int
454 __eth_bond_slave_remove_lock_free(uint16_t bonded_port_id,
455 				   uint16_t slave_port_id)
456 {
457 	struct rte_eth_dev *bonded_eth_dev;
458 	struct bond_dev_private *internals;
459 	struct rte_eth_dev *slave_eth_dev;
460 	struct rte_flow_error flow_error;
461 	struct rte_flow *flow;
462 	int i, slave_idx;
463 
464 	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
465 	internals = bonded_eth_dev->data->dev_private;
466 
467 	if (valid_slave_port_id(slave_port_id, internals->mode) < 0)
468 		return -1;
469 
470 	/* first remove from active slave list */
471 	slave_idx = find_slave_by_id(internals->active_slaves,
472 		internals->active_slave_count, slave_port_id);
473 
474 	if (slave_idx < internals->active_slave_count)
475 		deactivate_slave(bonded_eth_dev, slave_port_id);
476 
477 	slave_idx = -1;
478 	/* now find in slave list */
479 	for (i = 0; i < internals->slave_count; i++)
480 		if (internals->slaves[i].port_id == slave_port_id) {
481 			slave_idx = i;
482 			break;
483 		}
484 
485 	if (slave_idx < 0) {
486 		RTE_BOND_LOG(ERR, "Couldn't find slave in port list, slave count %d",
487 				internals->slave_count);
488 		return -1;
489 	}
490 
491 	/* Un-register link status change callback with bonded device pointer as
492 	 * argument*/
493 	rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
494 			bond_ethdev_lsc_event_callback,
495 			&rte_eth_devices[bonded_port_id].data->port_id);
496 
497 	/* Restore original MAC address of slave device */
498 	rte_eth_dev_default_mac_addr_set(slave_port_id,
499 			&(internals->slaves[slave_idx].persisted_mac_addr));
500 
501 	/* remove additional MAC addresses from the slave */
502 	slave_remove_mac_addresses(bonded_eth_dev, slave_port_id);
503 
504 	/*
505 	 * Remove bond device flows from slave device.
506 	 * Note: don't restore flow isolate mode.
507 	 */
508 	TAILQ_FOREACH(flow, &internals->flow_list, next) {
509 		if (flow->flows[slave_idx] != NULL) {
510 			rte_flow_destroy(slave_port_id, flow->flows[slave_idx],
511 					 &flow_error);
512 			flow->flows[slave_idx] = NULL;
513 		}
514 	}
515 
516 	slave_eth_dev = &rte_eth_devices[slave_port_id];
517 	slave_remove(internals, slave_eth_dev);
518 	slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
519 
520 	/*  first slave in the active list will be the primary by default,
521 	 *  otherwise use first device in list */
522 	if (internals->current_primary_port == slave_port_id) {
523 		if (internals->active_slave_count > 0)
524 			internals->current_primary_port = internals->active_slaves[0];
525 		else if (internals->slave_count > 0)
526 			internals->current_primary_port = internals->slaves[0].port_id;
527 		else
528 			internals->primary_port = 0;
529 	}
530 
531 	if (internals->active_slave_count < 1) {
532 		/* if no slaves are any longer attached to bonded device and MAC is not
533 		 * user defined then clear MAC of bonded device as it will be reset
534 		 * when a new slave is added */
535 		if (internals->slave_count < 1 && !internals->user_defined_mac)
536 			memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
537 					sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
538 	}
539 	if (internals->slave_count == 0) {
540 		internals->rx_offload_capa = 0;
541 		internals->tx_offload_capa = 0;
542 		internals->rx_queue_offload_capa = 0;
543 		internals->tx_queue_offload_capa = 0;
544 		internals->flow_type_rss_offloads = ETH_RSS_PROTO_MASK;
545 		internals->reta_size = 0;
546 		internals->candidate_max_rx_pktlen = 0;
547 		internals->max_rx_pktlen = 0;
548 	}
549 	return 0;
550 }
551 
552 int
553 rte_eth_bond_slave_remove(uint16_t bonded_port_id, uint16_t slave_port_id)
554 {
555 	struct rte_eth_dev *bonded_eth_dev;
556 	struct bond_dev_private *internals;
557 	int retval;
558 
559 	if (valid_bonded_port_id(bonded_port_id) != 0)
560 		return -1;
561 
562 	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
563 	internals = bonded_eth_dev->data->dev_private;
564 
565 	rte_spinlock_lock(&internals->lock);
566 
567 	retval = __eth_bond_slave_remove_lock_free(bonded_port_id, slave_port_id);
568 
569 	rte_spinlock_unlock(&internals->lock);
570 
571 	return retval;
572 }
573 
574 int
575 rte_eth_bond_mode_set(uint16_t bonded_port_id, uint8_t mode)
576 {
577 	struct rte_eth_dev *bonded_eth_dev;
578 
579 	if (valid_bonded_port_id(bonded_port_id) != 0)
580 		return -1;
581 
582 	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
583 
584 	if (check_for_master_bonded_ethdev(bonded_eth_dev) != 0 &&
585 			mode == BONDING_MODE_8023AD)
586 		return -1;
587 
588 	return bond_ethdev_mode_set(bonded_eth_dev, mode);
589 }
590 
591 int
592 rte_eth_bond_mode_get(uint16_t bonded_port_id)
593 {
594 	struct bond_dev_private *internals;
595 
596 	if (valid_bonded_port_id(bonded_port_id) != 0)
597 		return -1;
598 
599 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
600 
601 	return internals->mode;
602 }
603 
604 int
605 rte_eth_bond_primary_set(uint16_t bonded_port_id, uint16_t slave_port_id)
606 {
607 	struct bond_dev_private *internals;
608 
609 	if (valid_bonded_port_id(bonded_port_id) != 0)
610 		return -1;
611 
612 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
613 
614 	if (valid_slave_port_id(slave_port_id, internals->mode) != 0)
615 		return -1;
616 
617 	internals->user_defined_primary_port = 1;
618 	internals->primary_port = slave_port_id;
619 
620 	bond_ethdev_primary_set(internals, slave_port_id);
621 
622 	return 0;
623 }
624 
625 int
626 rte_eth_bond_primary_get(uint16_t bonded_port_id)
627 {
628 	struct bond_dev_private *internals;
629 
630 	if (valid_bonded_port_id(bonded_port_id) != 0)
631 		return -1;
632 
633 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
634 
635 	if (internals->slave_count < 1)
636 		return -1;
637 
638 	return internals->current_primary_port;
639 }
640 
641 int
642 rte_eth_bond_slaves_get(uint16_t bonded_port_id, uint16_t slaves[],
643 			uint16_t len)
644 {
645 	struct bond_dev_private *internals;
646 	uint8_t i;
647 
648 	if (valid_bonded_port_id(bonded_port_id) != 0)
649 		return -1;
650 
651 	if (slaves == NULL)
652 		return -1;
653 
654 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
655 
656 	if (internals->slave_count > len)
657 		return -1;
658 
659 	for (i = 0; i < internals->slave_count; i++)
660 		slaves[i] = internals->slaves[i].port_id;
661 
662 	return internals->slave_count;
663 }
664 
665 int
666 rte_eth_bond_active_slaves_get(uint16_t bonded_port_id, uint16_t slaves[],
667 		uint16_t len)
668 {
669 	struct bond_dev_private *internals;
670 
671 	if (valid_bonded_port_id(bonded_port_id) != 0)
672 		return -1;
673 
674 	if (slaves == NULL)
675 		return -1;
676 
677 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
678 
679 	if (internals->active_slave_count > len)
680 		return -1;
681 
682 	memcpy(slaves, internals->active_slaves,
683 	internals->active_slave_count * sizeof(internals->active_slaves[0]));
684 
685 	return internals->active_slave_count;
686 }
687 
688 int
689 rte_eth_bond_mac_address_set(uint16_t bonded_port_id,
690 		struct ether_addr *mac_addr)
691 {
692 	struct rte_eth_dev *bonded_eth_dev;
693 	struct bond_dev_private *internals;
694 
695 	if (valid_bonded_port_id(bonded_port_id) != 0)
696 		return -1;
697 
698 	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
699 	internals = bonded_eth_dev->data->dev_private;
700 
701 	/* Set MAC Address of Bonded Device */
702 	if (mac_address_set(bonded_eth_dev, mac_addr))
703 		return -1;
704 
705 	internals->user_defined_mac = 1;
706 
707 	/* Update all slave devices MACs*/
708 	if (internals->slave_count > 0)
709 		return mac_address_slaves_update(bonded_eth_dev);
710 
711 	return 0;
712 }
713 
714 int
715 rte_eth_bond_mac_address_reset(uint16_t bonded_port_id)
716 {
717 	struct rte_eth_dev *bonded_eth_dev;
718 	struct bond_dev_private *internals;
719 
720 	if (valid_bonded_port_id(bonded_port_id) != 0)
721 		return -1;
722 
723 	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
724 	internals = bonded_eth_dev->data->dev_private;
725 
726 	internals->user_defined_mac = 0;
727 
728 	if (internals->slave_count > 0) {
729 		int slave_port;
730 		/* Get the primary slave location based on the primary port
731 		 * number as, while slave_add(), we will keep the primary
732 		 * slave based on slave_count,but not based on the primary port.
733 		 */
734 		for (slave_port = 0; slave_port < internals->slave_count;
735 		     slave_port++) {
736 			if (internals->slaves[slave_port].port_id ==
737 			    internals->primary_port)
738 				break;
739 		}
740 
741 		/* Set MAC Address of Bonded Device */
742 		if (mac_address_set(bonded_eth_dev,
743 			&internals->slaves[slave_port].persisted_mac_addr)
744 				!= 0) {
745 			RTE_BOND_LOG(ERR, "Failed to set MAC address on bonded device");
746 			return -1;
747 		}
748 		/* Update all slave devices MAC addresses */
749 		return mac_address_slaves_update(bonded_eth_dev);
750 	}
751 	/* No need to update anything as no slaves present */
752 	return 0;
753 }
754 
755 int
756 rte_eth_bond_xmit_policy_set(uint16_t bonded_port_id, uint8_t policy)
757 {
758 	struct bond_dev_private *internals;
759 
760 	if (valid_bonded_port_id(bonded_port_id) != 0)
761 		return -1;
762 
763 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
764 
765 	switch (policy) {
766 	case BALANCE_XMIT_POLICY_LAYER2:
767 		internals->balance_xmit_policy = policy;
768 		internals->burst_xmit_hash = burst_xmit_l2_hash;
769 		break;
770 	case BALANCE_XMIT_POLICY_LAYER23:
771 		internals->balance_xmit_policy = policy;
772 		internals->burst_xmit_hash = burst_xmit_l23_hash;
773 		break;
774 	case BALANCE_XMIT_POLICY_LAYER34:
775 		internals->balance_xmit_policy = policy;
776 		internals->burst_xmit_hash = burst_xmit_l34_hash;
777 		break;
778 
779 	default:
780 		return -1;
781 	}
782 	return 0;
783 }
784 
785 int
786 rte_eth_bond_xmit_policy_get(uint16_t bonded_port_id)
787 {
788 	struct bond_dev_private *internals;
789 
790 	if (valid_bonded_port_id(bonded_port_id) != 0)
791 		return -1;
792 
793 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
794 
795 	return internals->balance_xmit_policy;
796 }
797 
798 int
799 rte_eth_bond_link_monitoring_set(uint16_t bonded_port_id, uint32_t internal_ms)
800 {
801 	struct bond_dev_private *internals;
802 
803 	if (valid_bonded_port_id(bonded_port_id) != 0)
804 		return -1;
805 
806 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
807 	internals->link_status_polling_interval_ms = internal_ms;
808 
809 	return 0;
810 }
811 
812 int
813 rte_eth_bond_link_monitoring_get(uint16_t bonded_port_id)
814 {
815 	struct bond_dev_private *internals;
816 
817 	if (valid_bonded_port_id(bonded_port_id) != 0)
818 		return -1;
819 
820 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
821 
822 	return internals->link_status_polling_interval_ms;
823 }
824 
825 int
826 rte_eth_bond_link_down_prop_delay_set(uint16_t bonded_port_id,
827 				       uint32_t delay_ms)
828 
829 {
830 	struct bond_dev_private *internals;
831 
832 	if (valid_bonded_port_id(bonded_port_id) != 0)
833 		return -1;
834 
835 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
836 	internals->link_down_delay_ms = delay_ms;
837 
838 	return 0;
839 }
840 
841 int
842 rte_eth_bond_link_down_prop_delay_get(uint16_t bonded_port_id)
843 {
844 	struct bond_dev_private *internals;
845 
846 	if (valid_bonded_port_id(bonded_port_id) != 0)
847 		return -1;
848 
849 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
850 
851 	return internals->link_down_delay_ms;
852 }
853 
854 int
855 rte_eth_bond_link_up_prop_delay_set(uint16_t bonded_port_id, uint32_t delay_ms)
856 
857 {
858 	struct bond_dev_private *internals;
859 
860 	if (valid_bonded_port_id(bonded_port_id) != 0)
861 		return -1;
862 
863 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
864 	internals->link_up_delay_ms = delay_ms;
865 
866 	return 0;
867 }
868 
869 int
870 rte_eth_bond_link_up_prop_delay_get(uint16_t bonded_port_id)
871 {
872 	struct bond_dev_private *internals;
873 
874 	if (valid_bonded_port_id(bonded_port_id) != 0)
875 		return -1;
876 
877 	internals = rte_eth_devices[bonded_port_id].data->dev_private;
878 
879 	return internals->link_up_delay_ms;
880 }
881