1/*-
2 *   BSD LICENSE
3 *
4 *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5 *   All rights reserved.
6 *
7 *   Redistribution and use in source and binary forms, with or without
8 *   modification, are permitted provided that the following conditions
9 *   are met:
10 *
11 *     * Redistributions of source code must retain the above copyright
12 *       notice, this list of conditions and the following disclaimer.
13 *     * Redistributions in binary form must reproduce the above copyright
14 *       notice, this list of conditions and the following disclaimer in
15 *       the documentation and/or other materials provided with the
16 *       distribution.
17 *     * Neither the name of Intel Corporation nor the names of its
18 *       contributors may be used to endorse or promote products derived
19 *       from this software without specific prior written permission.
20 *
21 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <string.h>
35
36#include <rte_mbuf.h>
37#include <rte_malloc.h>
38#include <rte_ethdev.h>
39#include <rte_tcp.h>
40#include <rte_vdev.h>
41
42#include "rte_eth_bond.h"
43#include "rte_eth_bond_private.h"
44#include "rte_eth_bond_8023ad_private.h"
45
46#define DEFAULT_POLLING_INTERVAL_10_MS (10)
47
48int
49check_for_bonded_ethdev(const struct rte_eth_dev *eth_dev)
50{
51	/* Check valid pointer */
52	if (eth_dev->data->drv_name == NULL)
53		return -1;
54
55	/* return 0 if driver name matches */
56	return eth_dev->data->drv_name != pmd_bond_drv.driver.name;
57}
58
59int
60valid_bonded_port_id(uint8_t port_id)
61{
62	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
63	return check_for_bonded_ethdev(&rte_eth_devices[port_id]);
64}
65
66int
67valid_slave_port_id(uint8_t port_id)
68{
69	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
70
71	/* Verify that port_id refers to a non bonded port */
72	if (check_for_bonded_ethdev(&rte_eth_devices[port_id]) == 0)
73		return -1;
74
75	return 0;
76}
77
78void
79activate_slave(struct rte_eth_dev *eth_dev, uint8_t port_id)
80{
81	struct bond_dev_private *internals = eth_dev->data->dev_private;
82	uint8_t active_count = internals->active_slave_count;
83
84	if (internals->mode == BONDING_MODE_8023AD)
85		bond_mode_8023ad_activate_slave(eth_dev, port_id);
86
87	if (internals->mode == BONDING_MODE_TLB
88			|| internals->mode == BONDING_MODE_ALB) {
89
90		internals->tlb_slaves_order[active_count] = port_id;
91	}
92
93	RTE_ASSERT(internals->active_slave_count <
94			(RTE_DIM(internals->active_slaves) - 1));
95
96	internals->active_slaves[internals->active_slave_count] = port_id;
97	internals->active_slave_count++;
98
99	if (internals->mode == BONDING_MODE_TLB)
100		bond_tlb_activate_slave(internals);
101	if (internals->mode == BONDING_MODE_ALB)
102		bond_mode_alb_client_list_upd(eth_dev);
103}
104
105void
106deactivate_slave(struct rte_eth_dev *eth_dev, uint8_t port_id)
107{
108	uint8_t slave_pos;
109	struct bond_dev_private *internals = eth_dev->data->dev_private;
110	uint8_t active_count = internals->active_slave_count;
111
112	if (internals->mode == BONDING_MODE_8023AD) {
113		bond_mode_8023ad_stop(eth_dev);
114		bond_mode_8023ad_deactivate_slave(eth_dev, port_id);
115	} else if (internals->mode == BONDING_MODE_TLB
116			|| internals->mode == BONDING_MODE_ALB)
117		bond_tlb_disable(internals);
118
119	slave_pos = find_slave_by_id(internals->active_slaves, active_count,
120			port_id);
121
122	/* If slave was not at the end of the list
123	 * shift active slaves up active array list */
124	if (slave_pos < active_count) {
125		active_count--;
126		memmove(internals->active_slaves + slave_pos,
127				internals->active_slaves + slave_pos + 1,
128				(active_count - slave_pos) *
129					sizeof(internals->active_slaves[0]));
130	}
131
132	RTE_ASSERT(active_count < RTE_DIM(internals->active_slaves));
133	internals->active_slave_count = active_count;
134
135	if (eth_dev->data->dev_started) {
136		if (internals->mode == BONDING_MODE_8023AD) {
137			bond_mode_8023ad_start(eth_dev);
138		} else if (internals->mode == BONDING_MODE_TLB) {
139			bond_tlb_enable(internals);
140		} else if (internals->mode == BONDING_MODE_ALB) {
141			bond_tlb_enable(internals);
142			bond_mode_alb_client_list_upd(eth_dev);
143		}
144	}
145}
146
147uint8_t
148number_of_sockets(void)
149{
150	int sockets = 0;
151	int i;
152	const struct rte_memseg *ms = rte_eal_get_physmem_layout();
153
154	for (i = 0; ((i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL)); i++) {
155		if (sockets < ms[i].socket_id)
156			sockets = ms[i].socket_id;
157	}
158
159	/* Number of sockets = maximum socket_id + 1 */
160	return ++sockets;
161}
162
163int
164rte_eth_bond_create(const char *name, uint8_t mode, uint8_t socket_id)
165{
166	struct bond_dev_private *internals = NULL;
167	struct rte_eth_dev *eth_dev = NULL;
168	uint32_t vlan_filter_bmp_size;
169
170	/* now do all data allocation - for eth_dev structure, dummy pci driver
171	 * and internal (private) data
172	 */
173
174	if (name == NULL) {
175		RTE_BOND_LOG(ERR, "Invalid name specified");
176		goto err;
177	}
178
179	if (socket_id >= number_of_sockets()) {
180		RTE_BOND_LOG(ERR,
181				"Invalid socket id specified to create bonded device on.");
182		goto err;
183	}
184
185	internals = rte_zmalloc_socket(name, sizeof(*internals), 0, socket_id);
186	if (internals == NULL) {
187		RTE_BOND_LOG(ERR, "Unable to malloc internals on socket");
188		goto err;
189	}
190
191	/* reserve an ethdev entry */
192	eth_dev = rte_eth_dev_allocate(name);
193	if (eth_dev == NULL) {
194		RTE_BOND_LOG(ERR, "Unable to allocate rte_eth_dev");
195		goto err;
196	}
197
198	eth_dev->data->dev_private = internals;
199	eth_dev->data->nb_rx_queues = (uint16_t)1;
200	eth_dev->data->nb_tx_queues = (uint16_t)1;
201
202	eth_dev->data->mac_addrs = rte_zmalloc_socket(name, ETHER_ADDR_LEN, 0,
203			socket_id);
204	if (eth_dev->data->mac_addrs == NULL) {
205		RTE_BOND_LOG(ERR, "Unable to malloc mac_addrs");
206		goto err;
207	}
208
209	eth_dev->dev_ops = &default_dev_ops;
210	eth_dev->data->dev_flags = RTE_ETH_DEV_INTR_LSC |
211		RTE_ETH_DEV_DETACHABLE;
212	eth_dev->driver = NULL;
213	eth_dev->data->kdrv = RTE_KDRV_NONE;
214	eth_dev->data->drv_name = pmd_bond_drv.driver.name;
215	eth_dev->data->numa_node =  socket_id;
216
217	rte_spinlock_init(&internals->lock);
218
219	internals->port_id = eth_dev->data->port_id;
220	internals->mode = BONDING_MODE_INVALID;
221	internals->current_primary_port = RTE_MAX_ETHPORTS + 1;
222	internals->balance_xmit_policy = BALANCE_XMIT_POLICY_LAYER2;
223	internals->xmit_hash = xmit_l2_hash;
224	internals->user_defined_mac = 0;
225	internals->link_props_set = 0;
226
227	internals->link_status_polling_enabled = 0;
228
229	internals->link_status_polling_interval_ms = DEFAULT_POLLING_INTERVAL_10_MS;
230	internals->link_down_delay_ms = 0;
231	internals->link_up_delay_ms = 0;
232
233	internals->slave_count = 0;
234	internals->active_slave_count = 0;
235	internals->rx_offload_capa = 0;
236	internals->tx_offload_capa = 0;
237	internals->candidate_max_rx_pktlen = 0;
238	internals->max_rx_pktlen = 0;
239
240	/* Initially allow to choose any offload type */
241	internals->flow_type_rss_offloads = ETH_RSS_PROTO_MASK;
242
243	memset(internals->active_slaves, 0, sizeof(internals->active_slaves));
244	memset(internals->slaves, 0, sizeof(internals->slaves));
245
246	/* Set mode 4 default configuration */
247	bond_mode_8023ad_setup(eth_dev, NULL);
248	if (bond_ethdev_mode_set(eth_dev, mode)) {
249		RTE_BOND_LOG(ERR, "Failed to set bonded device %d mode too %d",
250				 eth_dev->data->port_id, mode);
251		goto err;
252	}
253
254	vlan_filter_bmp_size =
255		rte_bitmap_get_memory_footprint(ETHER_MAX_VLAN_ID + 1);
256	internals->vlan_filter_bmpmem = rte_malloc(name, vlan_filter_bmp_size,
257						   RTE_CACHE_LINE_SIZE);
258	if (internals->vlan_filter_bmpmem == NULL) {
259		RTE_BOND_LOG(ERR,
260			     "Failed to allocate vlan bitmap for bonded device %u\n",
261			     eth_dev->data->port_id);
262		goto err;
263	}
264
265	internals->vlan_filter_bmp = rte_bitmap_init(ETHER_MAX_VLAN_ID + 1,
266			internals->vlan_filter_bmpmem, vlan_filter_bmp_size);
267	if (internals->vlan_filter_bmp == NULL) {
268		RTE_BOND_LOG(ERR,
269			     "Failed to init vlan bitmap for bonded device %u\n",
270			     eth_dev->data->port_id);
271		rte_free(internals->vlan_filter_bmpmem);
272		goto err;
273	}
274
275	return eth_dev->data->port_id;
276
277err:
278	rte_free(internals);
279	if (eth_dev != NULL) {
280		rte_free(eth_dev->data->mac_addrs);
281		rte_eth_dev_release_port(eth_dev);
282	}
283	return -1;
284}
285
286int
287rte_eth_bond_free(const char *name)
288{
289	struct rte_eth_dev *eth_dev = NULL;
290	struct bond_dev_private *internals;
291
292	/* now free all data allocation - for eth_dev structure,
293	 * dummy pci driver and internal (private) data
294	 */
295
296	/* find an ethdev entry */
297	eth_dev = rte_eth_dev_allocated(name);
298	if (eth_dev == NULL)
299		return -ENODEV;
300
301	internals = eth_dev->data->dev_private;
302	if (internals->slave_count != 0)
303		return -EBUSY;
304
305	if (eth_dev->data->dev_started == 1) {
306		bond_ethdev_stop(eth_dev);
307		bond_ethdev_close(eth_dev);
308	}
309
310	eth_dev->dev_ops = NULL;
311	eth_dev->rx_pkt_burst = NULL;
312	eth_dev->tx_pkt_burst = NULL;
313
314	internals = eth_dev->data->dev_private;
315	rte_bitmap_free(internals->vlan_filter_bmp);
316	rte_free(internals->vlan_filter_bmpmem);
317	rte_free(eth_dev->data->dev_private);
318	rte_free(eth_dev->data->mac_addrs);
319
320	rte_eth_dev_release_port(eth_dev);
321
322	return 0;
323}
324
325static int
326slave_vlan_filter_set(uint8_t bonded_port_id, uint8_t slave_port_id)
327{
328	struct rte_eth_dev *bonded_eth_dev;
329	struct bond_dev_private *internals;
330	int found;
331	int res = 0;
332	uint64_t slab = 0;
333	uint32_t pos = 0;
334	uint16_t first;
335
336	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
337	if (bonded_eth_dev->data->dev_conf.rxmode.hw_vlan_filter == 0)
338		return 0;
339
340	internals = bonded_eth_dev->data->dev_private;
341	found = rte_bitmap_scan(internals->vlan_filter_bmp, &pos, &slab);
342	first = pos;
343
344	if (!found)
345		return 0;
346
347	do {
348		uint32_t i;
349		uint64_t mask;
350
351		for (i = 0, mask = 1;
352		     i < RTE_BITMAP_SLAB_BIT_SIZE;
353		     i ++, mask <<= 1) {
354			if (unlikely(slab & mask))
355				res = rte_eth_dev_vlan_filter(slave_port_id,
356							      (uint16_t)pos, 1);
357		}
358		found = rte_bitmap_scan(internals->vlan_filter_bmp,
359					&pos, &slab);
360	} while (found && first != pos && res == 0);
361
362	return res;
363}
364
365static int
366__eth_bond_slave_add_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
367{
368	struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
369	struct bond_dev_private *internals;
370	struct rte_eth_link link_props;
371	struct rte_eth_dev_info dev_info;
372
373	if (valid_slave_port_id(slave_port_id) != 0)
374		return -1;
375
376	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
377	internals = bonded_eth_dev->data->dev_private;
378
379	slave_eth_dev = &rte_eth_devices[slave_port_id];
380	if (slave_eth_dev->data->dev_flags & RTE_ETH_DEV_BONDED_SLAVE) {
381		RTE_BOND_LOG(ERR, "Slave device is already a slave of a bonded device");
382		return -1;
383	}
384
385	/* Add slave details to bonded device */
386	slave_eth_dev->data->dev_flags |= RTE_ETH_DEV_BONDED_SLAVE;
387
388	rte_eth_dev_info_get(slave_port_id, &dev_info);
389	if (dev_info.max_rx_pktlen < internals->max_rx_pktlen) {
390		RTE_BOND_LOG(ERR, "Slave (port %u) max_rx_pktlen too small",
391			     slave_port_id);
392		return -1;
393	}
394
395	slave_add(internals, slave_eth_dev);
396
397	/* We need to store slaves reta_size to be able to synchronize RETA for all
398	 * slave devices even if its sizes are different.
399	 */
400	internals->slaves[internals->slave_count].reta_size = dev_info.reta_size;
401
402	if (internals->slave_count < 1) {
403		/* if MAC is not user defined then use MAC of first slave add to
404		 * bonded device */
405		if (!internals->user_defined_mac)
406			mac_address_set(bonded_eth_dev, slave_eth_dev->data->mac_addrs);
407
408		/* Inherit eth dev link properties from first slave */
409		link_properties_set(bonded_eth_dev,
410				&(slave_eth_dev->data->dev_link));
411
412		/* Make primary slave */
413		internals->primary_port = slave_port_id;
414		internals->current_primary_port = slave_port_id;
415
416		/* Inherit queues settings from first slave */
417		internals->nb_rx_queues = slave_eth_dev->data->nb_rx_queues;
418		internals->nb_tx_queues = slave_eth_dev->data->nb_tx_queues;
419
420		internals->reta_size = dev_info.reta_size;
421
422		/* Take the first dev's offload capabilities */
423		internals->rx_offload_capa = dev_info.rx_offload_capa;
424		internals->tx_offload_capa = dev_info.tx_offload_capa;
425		internals->flow_type_rss_offloads = dev_info.flow_type_rss_offloads;
426
427		/* Inherit first slave's max rx packet size */
428		internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
429
430	} else {
431		internals->rx_offload_capa &= dev_info.rx_offload_capa;
432		internals->tx_offload_capa &= dev_info.tx_offload_capa;
433		internals->flow_type_rss_offloads &= dev_info.flow_type_rss_offloads;
434
435		/* RETA size is GCD of all slaves RETA sizes, so, if all sizes will be
436		 * the power of 2, the lower one is GCD
437		 */
438		if (internals->reta_size > dev_info.reta_size)
439			internals->reta_size = dev_info.reta_size;
440
441		if (!internals->max_rx_pktlen &&
442		    dev_info.max_rx_pktlen < internals->candidate_max_rx_pktlen)
443			internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
444	}
445
446	bonded_eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf &=
447			internals->flow_type_rss_offloads;
448
449	internals->slave_count++;
450
451	/* Update all slave devices MACs*/
452	mac_address_slaves_update(bonded_eth_dev);
453
454	if (bonded_eth_dev->data->dev_started) {
455		if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
456			slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
457			RTE_BOND_LOG(ERR, "rte_bond_slaves_configure: port=%d",
458					slave_port_id);
459			return -1;
460		}
461	}
462
463	/* Register link status change callback with bonded device pointer as
464	 * argument*/
465	rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
466			bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
467
468	/* If bonded device is started then we can add the slave to our active
469	 * slave array */
470	if (bonded_eth_dev->data->dev_started) {
471		rte_eth_link_get_nowait(slave_port_id, &link_props);
472
473		 if (link_props.link_status == ETH_LINK_UP) {
474			if (internals->active_slave_count == 0 &&
475			    !internals->user_defined_primary_port)
476				bond_ethdev_primary_set(internals,
477							slave_port_id);
478
479			if (find_slave_by_id(internals->active_slaves,
480					     internals->active_slave_count,
481					     slave_port_id) == internals->active_slave_count)
482				activate_slave(bonded_eth_dev, slave_port_id);
483		}
484	}
485
486	slave_vlan_filter_set(bonded_port_id, slave_port_id);
487
488	return 0;
489
490}
491
492int
493rte_eth_bond_slave_add(uint8_t bonded_port_id, uint8_t slave_port_id)
494{
495	struct rte_eth_dev *bonded_eth_dev;
496	struct bond_dev_private *internals;
497
498	int retval;
499
500	/* Verify that port id's are valid bonded and slave ports */
501	if (valid_bonded_port_id(bonded_port_id) != 0)
502		return -1;
503
504	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
505	internals = bonded_eth_dev->data->dev_private;
506
507	rte_spinlock_lock(&internals->lock);
508
509	retval = __eth_bond_slave_add_lock_free(bonded_port_id, slave_port_id);
510
511	rte_spinlock_unlock(&internals->lock);
512
513	return retval;
514}
515
516static int
517__eth_bond_slave_remove_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
518{
519	struct rte_eth_dev *bonded_eth_dev;
520	struct bond_dev_private *internals;
521	struct rte_eth_dev *slave_eth_dev;
522	int i, slave_idx;
523
524	if (valid_slave_port_id(slave_port_id) != 0)
525		return -1;
526
527	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
528	internals = bonded_eth_dev->data->dev_private;
529
530	/* first remove from active slave list */
531	slave_idx = find_slave_by_id(internals->active_slaves,
532		internals->active_slave_count, slave_port_id);
533
534	if (slave_idx < internals->active_slave_count)
535		deactivate_slave(bonded_eth_dev, slave_port_id);
536
537	slave_idx = -1;
538	/* now find in slave list */
539	for (i = 0; i < internals->slave_count; i++)
540		if (internals->slaves[i].port_id == slave_port_id) {
541			slave_idx = i;
542			break;
543		}
544
545	if (slave_idx < 0) {
546		RTE_BOND_LOG(ERR, "Couldn't find slave in port list, slave count %d",
547				internals->slave_count);
548		return -1;
549	}
550
551	/* Un-register link status change callback with bonded device pointer as
552	 * argument*/
553	rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
554			bond_ethdev_lsc_event_callback,
555			&rte_eth_devices[bonded_port_id].data->port_id);
556
557	/* Restore original MAC address of slave device */
558	mac_address_set(&rte_eth_devices[slave_port_id],
559			&(internals->slaves[slave_idx].persisted_mac_addr));
560
561	slave_eth_dev = &rte_eth_devices[slave_port_id];
562	slave_remove(internals, slave_eth_dev);
563	slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
564
565	/*  first slave in the active list will be the primary by default,
566	 *  otherwise use first device in list */
567	if (internals->current_primary_port == slave_port_id) {
568		if (internals->active_slave_count > 0)
569			internals->current_primary_port = internals->active_slaves[0];
570		else if (internals->slave_count > 0)
571			internals->current_primary_port = internals->slaves[0].port_id;
572		else
573			internals->primary_port = 0;
574	}
575
576	if (internals->active_slave_count < 1) {
577		/* reset device link properties as no slaves are active */
578		link_properties_reset(&rte_eth_devices[bonded_port_id]);
579
580		/* if no slaves are any longer attached to bonded device and MAC is not
581		 * user defined then clear MAC of bonded device as it will be reset
582		 * when a new slave is added */
583		if (internals->slave_count < 1 && !internals->user_defined_mac)
584			memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
585					sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
586	}
587	if (internals->slave_count == 0) {
588		internals->rx_offload_capa = 0;
589		internals->tx_offload_capa = 0;
590		internals->flow_type_rss_offloads = ETH_RSS_PROTO_MASK;
591		internals->reta_size = 0;
592		internals->candidate_max_rx_pktlen = 0;
593		internals->max_rx_pktlen = 0;
594	}
595	return 0;
596}
597
598int
599rte_eth_bond_slave_remove(uint8_t bonded_port_id, uint8_t slave_port_id)
600{
601	struct rte_eth_dev *bonded_eth_dev;
602	struct bond_dev_private *internals;
603	int retval;
604
605	if (valid_bonded_port_id(bonded_port_id) != 0)
606		return -1;
607
608	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
609	internals = bonded_eth_dev->data->dev_private;
610
611	rte_spinlock_lock(&internals->lock);
612
613	retval = __eth_bond_slave_remove_lock_free(bonded_port_id, slave_port_id);
614
615	rte_spinlock_unlock(&internals->lock);
616
617	return retval;
618}
619
620int
621rte_eth_bond_mode_set(uint8_t bonded_port_id, uint8_t mode)
622{
623	if (valid_bonded_port_id(bonded_port_id) != 0)
624		return -1;
625
626	return bond_ethdev_mode_set(&rte_eth_devices[bonded_port_id], mode);
627}
628
629int
630rte_eth_bond_mode_get(uint8_t bonded_port_id)
631{
632	struct bond_dev_private *internals;
633
634	if (valid_bonded_port_id(bonded_port_id) != 0)
635		return -1;
636
637	internals = rte_eth_devices[bonded_port_id].data->dev_private;
638
639	return internals->mode;
640}
641
642int
643rte_eth_bond_primary_set(uint8_t bonded_port_id, uint8_t slave_port_id)
644{
645	struct bond_dev_private *internals;
646
647	if (valid_bonded_port_id(bonded_port_id) != 0)
648		return -1;
649
650	if (valid_slave_port_id(slave_port_id) != 0)
651		return -1;
652
653	internals =  rte_eth_devices[bonded_port_id].data->dev_private;
654
655	internals->user_defined_primary_port = 1;
656	internals->primary_port = slave_port_id;
657
658	bond_ethdev_primary_set(internals, slave_port_id);
659
660	return 0;
661}
662
663int
664rte_eth_bond_primary_get(uint8_t bonded_port_id)
665{
666	struct bond_dev_private *internals;
667
668	if (valid_bonded_port_id(bonded_port_id) != 0)
669		return -1;
670
671	internals = rte_eth_devices[bonded_port_id].data->dev_private;
672
673	if (internals->slave_count < 1)
674		return -1;
675
676	return internals->current_primary_port;
677}
678
679int
680rte_eth_bond_slaves_get(uint8_t bonded_port_id, uint8_t slaves[], uint8_t len)
681{
682	struct bond_dev_private *internals;
683	uint8_t i;
684
685	if (valid_bonded_port_id(bonded_port_id) != 0)
686		return -1;
687
688	if (slaves == NULL)
689		return -1;
690
691	internals = rte_eth_devices[bonded_port_id].data->dev_private;
692
693	if (internals->slave_count > len)
694		return -1;
695
696	for (i = 0; i < internals->slave_count; i++)
697		slaves[i] = internals->slaves[i].port_id;
698
699	return internals->slave_count;
700}
701
702int
703rte_eth_bond_active_slaves_get(uint8_t bonded_port_id, uint8_t slaves[],
704		uint8_t len)
705{
706	struct bond_dev_private *internals;
707
708	if (valid_bonded_port_id(bonded_port_id) != 0)
709		return -1;
710
711	if (slaves == NULL)
712		return -1;
713
714	internals = rte_eth_devices[bonded_port_id].data->dev_private;
715
716	if (internals->active_slave_count > len)
717		return -1;
718
719	memcpy(slaves, internals->active_slaves, internals->active_slave_count);
720
721	return internals->active_slave_count;
722}
723
724int
725rte_eth_bond_mac_address_set(uint8_t bonded_port_id,
726		struct ether_addr *mac_addr)
727{
728	struct rte_eth_dev *bonded_eth_dev;
729	struct bond_dev_private *internals;
730
731	if (valid_bonded_port_id(bonded_port_id) != 0)
732		return -1;
733
734	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
735	internals = bonded_eth_dev->data->dev_private;
736
737	/* Set MAC Address of Bonded Device */
738	if (mac_address_set(bonded_eth_dev, mac_addr))
739		return -1;
740
741	internals->user_defined_mac = 1;
742
743	/* Update all slave devices MACs*/
744	if (internals->slave_count > 0)
745		return mac_address_slaves_update(bonded_eth_dev);
746
747	return 0;
748}
749
750int
751rte_eth_bond_mac_address_reset(uint8_t bonded_port_id)
752{
753	struct rte_eth_dev *bonded_eth_dev;
754	struct bond_dev_private *internals;
755
756	if (valid_bonded_port_id(bonded_port_id) != 0)
757		return -1;
758
759	bonded_eth_dev = &rte_eth_devices[bonded_port_id];
760	internals = bonded_eth_dev->data->dev_private;
761
762	internals->user_defined_mac = 0;
763
764	if (internals->slave_count > 0) {
765		/* Set MAC Address of Bonded Device */
766		if (mac_address_set(bonded_eth_dev,
767				&internals->slaves[internals->primary_port].persisted_mac_addr)
768				!= 0) {
769			RTE_BOND_LOG(ERR, "Failed to set MAC address on bonded device");
770			return -1;
771		}
772		/* Update all slave devices MAC addresses */
773		return mac_address_slaves_update(bonded_eth_dev);
774	}
775	/* No need to update anything as no slaves present */
776	return 0;
777}
778
779int
780rte_eth_bond_xmit_policy_set(uint8_t bonded_port_id, uint8_t policy)
781{
782	struct bond_dev_private *internals;
783
784	if (valid_bonded_port_id(bonded_port_id) != 0)
785		return -1;
786
787	internals = rte_eth_devices[bonded_port_id].data->dev_private;
788
789	switch (policy) {
790	case BALANCE_XMIT_POLICY_LAYER2:
791		internals->balance_xmit_policy = policy;
792		internals->xmit_hash = xmit_l2_hash;
793		break;
794	case BALANCE_XMIT_POLICY_LAYER23:
795		internals->balance_xmit_policy = policy;
796		internals->xmit_hash = xmit_l23_hash;
797		break;
798	case BALANCE_XMIT_POLICY_LAYER34:
799		internals->balance_xmit_policy = policy;
800		internals->xmit_hash = xmit_l34_hash;
801		break;
802
803	default:
804		return -1;
805	}
806	return 0;
807}
808
809int
810rte_eth_bond_xmit_policy_get(uint8_t bonded_port_id)
811{
812	struct bond_dev_private *internals;
813
814	if (valid_bonded_port_id(bonded_port_id) != 0)
815		return -1;
816
817	internals = rte_eth_devices[bonded_port_id].data->dev_private;
818
819	return internals->balance_xmit_policy;
820}
821
822int
823rte_eth_bond_link_monitoring_set(uint8_t bonded_port_id, uint32_t internal_ms)
824{
825	struct bond_dev_private *internals;
826
827	if (valid_bonded_port_id(bonded_port_id) != 0)
828		return -1;
829
830	internals = rte_eth_devices[bonded_port_id].data->dev_private;
831	internals->link_status_polling_interval_ms = internal_ms;
832
833	return 0;
834}
835
836int
837rte_eth_bond_link_monitoring_get(uint8_t bonded_port_id)
838{
839	struct bond_dev_private *internals;
840
841	if (valid_bonded_port_id(bonded_port_id) != 0)
842		return -1;
843
844	internals = rte_eth_devices[bonded_port_id].data->dev_private;
845
846	return internals->link_status_polling_interval_ms;
847}
848
849int
850rte_eth_bond_link_down_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
851
852{
853	struct bond_dev_private *internals;
854
855	if (valid_bonded_port_id(bonded_port_id) != 0)
856		return -1;
857
858	internals = rte_eth_devices[bonded_port_id].data->dev_private;
859	internals->link_down_delay_ms = delay_ms;
860
861	return 0;
862}
863
864int
865rte_eth_bond_link_down_prop_delay_get(uint8_t bonded_port_id)
866{
867	struct bond_dev_private *internals;
868
869	if (valid_bonded_port_id(bonded_port_id) != 0)
870		return -1;
871
872	internals = rte_eth_devices[bonded_port_id].data->dev_private;
873
874	return internals->link_down_delay_ms;
875}
876
877int
878rte_eth_bond_link_up_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
879
880{
881	struct bond_dev_private *internals;
882
883	if (valid_bonded_port_id(bonded_port_id) != 0)
884		return -1;
885
886	internals = rte_eth_devices[bonded_port_id].data->dev_private;
887	internals->link_up_delay_ms = delay_ms;
888
889	return 0;
890}
891
892int
893rte_eth_bond_link_up_prop_delay_get(uint8_t bonded_port_id)
894{
895	struct bond_dev_private *internals;
896
897	if (valid_bonded_port_id(bonded_port_id) != 0)
898		return -1;
899
900	internals = rte_eth_devices[bonded_port_id].data->dev_private;
901
902	return internals->link_up_delay_ms;
903}
904