Imported Upstream version 16.07-rc1
[deb_dpdk.git] / drivers / net / bonding / rte_eth_bond_api.c
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
41 #include "rte_eth_bond.h"
42 #include "rte_eth_bond_private.h"
43 #include "rte_eth_bond_8023ad_private.h"
44
45 #define DEFAULT_POLLING_INTERVAL_10_MS (10)
46
47 const char pmd_bond_driver_name[] = "rte_bond_pmd";
48
49 int
50 check_for_bonded_ethdev(const struct rte_eth_dev *eth_dev)
51 {
52         /* Check valid pointer */
53         if (eth_dev->data->drv_name == NULL)
54                 return -1;
55
56         /* return 0 if driver name matches */
57         return eth_dev->data->drv_name != pmd_bond_driver_name;
58 }
59
60 int
61 valid_bonded_port_id(uint8_t port_id)
62 {
63         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
64         return check_for_bonded_ethdev(&rte_eth_devices[port_id]);
65 }
66
67 int
68 valid_slave_port_id(uint8_t port_id)
69 {
70         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
71
72         /* Verify that port_id refers to a non bonded port */
73         if (check_for_bonded_ethdev(&rte_eth_devices[port_id]) == 0)
74                 return -1;
75
76         return 0;
77 }
78
79 void
80 activate_slave(struct rte_eth_dev *eth_dev, uint8_t port_id)
81 {
82         struct bond_dev_private *internals = eth_dev->data->dev_private;
83         uint8_t active_count = internals->active_slave_count;
84
85         if (internals->mode == BONDING_MODE_8023AD)
86                 bond_mode_8023ad_activate_slave(eth_dev, port_id);
87
88         if (internals->mode == BONDING_MODE_TLB
89                         || internals->mode == BONDING_MODE_ALB) {
90
91                 internals->tlb_slaves_order[active_count] = port_id;
92         }
93
94         RTE_ASSERT(internals->active_slave_count <
95                         (RTE_DIM(internals->active_slaves) - 1));
96
97         internals->active_slaves[internals->active_slave_count] = port_id;
98         internals->active_slave_count++;
99
100         if (internals->mode == BONDING_MODE_TLB)
101                 bond_tlb_activate_slave(internals);
102         if (internals->mode == BONDING_MODE_ALB)
103                 bond_mode_alb_client_list_upd(eth_dev);
104 }
105
106 void
107 deactivate_slave(struct rte_eth_dev *eth_dev, uint8_t port_id)
108 {
109         uint8_t slave_pos;
110         struct bond_dev_private *internals = eth_dev->data->dev_private;
111         uint8_t active_count = internals->active_slave_count;
112
113         if (internals->mode == BONDING_MODE_8023AD) {
114                 bond_mode_8023ad_stop(eth_dev);
115                 bond_mode_8023ad_deactivate_slave(eth_dev, port_id);
116         } else if (internals->mode == BONDING_MODE_TLB
117                         || internals->mode == BONDING_MODE_ALB)
118                 bond_tlb_disable(internals);
119
120         slave_pos = find_slave_by_id(internals->active_slaves, active_count,
121                         port_id);
122
123         /* If slave was not at the end of the list
124          * shift active slaves up active array list */
125         if (slave_pos < active_count) {
126                 active_count--;
127                 memmove(internals->active_slaves + slave_pos,
128                                 internals->active_slaves + slave_pos + 1,
129                                 (active_count - slave_pos) *
130                                         sizeof(internals->active_slaves[0]));
131         }
132
133         RTE_ASSERT(active_count < RTE_DIM(internals->active_slaves));
134         internals->active_slave_count = active_count;
135
136         if (eth_dev->data->dev_started) {
137                 if (internals->mode == BONDING_MODE_8023AD) {
138                         bond_mode_8023ad_start(eth_dev);
139                 } else if (internals->mode == BONDING_MODE_TLB) {
140                         bond_tlb_enable(internals);
141                 } else if (internals->mode == BONDING_MODE_ALB) {
142                         bond_tlb_enable(internals);
143                         bond_mode_alb_client_list_upd(eth_dev);
144                 }
145         }
146 }
147
148 uint8_t
149 number_of_sockets(void)
150 {
151         int sockets = 0;
152         int i;
153         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
154
155         for (i = 0; ((i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL)); i++) {
156                 if (sockets < ms[i].socket_id)
157                         sockets = ms[i].socket_id;
158         }
159
160         /* Number of sockets = maximum socket_id + 1 */
161         return ++sockets;
162 }
163
164 int
165 rte_eth_bond_create(const char *name, uint8_t mode, uint8_t socket_id)
166 {
167         struct bond_dev_private *internals = NULL;
168         struct rte_eth_dev *eth_dev = NULL;
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, RTE_ETH_DEV_VIRTUAL);
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         TAILQ_INIT(&(eth_dev->link_intr_cbs));
203
204         eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
205
206         eth_dev->data->mac_addrs = rte_zmalloc_socket(name, ETHER_ADDR_LEN, 0,
207                         socket_id);
208         if (eth_dev->data->mac_addrs == NULL) {
209                 RTE_BOND_LOG(ERR, "Unable to malloc mac_addrs");
210                 goto err;
211         }
212
213         eth_dev->data->dev_started = 0;
214         eth_dev->data->promiscuous = 0;
215         eth_dev->data->scattered_rx = 0;
216         eth_dev->data->all_multicast = 0;
217
218         eth_dev->dev_ops = &default_dev_ops;
219         eth_dev->data->dev_flags = RTE_ETH_DEV_INTR_LSC |
220                 RTE_ETH_DEV_DETACHABLE;
221         eth_dev->driver = NULL;
222         eth_dev->data->kdrv = RTE_KDRV_NONE;
223         eth_dev->data->drv_name = pmd_bond_driver_name;
224         eth_dev->data->numa_node =  socket_id;
225
226         rte_spinlock_init(&internals->lock);
227
228         internals->port_id = eth_dev->data->port_id;
229         internals->mode = BONDING_MODE_INVALID;
230         internals->current_primary_port = RTE_MAX_ETHPORTS + 1;
231         internals->balance_xmit_policy = BALANCE_XMIT_POLICY_LAYER2;
232         internals->xmit_hash = xmit_l2_hash;
233         internals->user_defined_mac = 0;
234         internals->link_props_set = 0;
235
236         internals->link_status_polling_enabled = 0;
237
238         internals->link_status_polling_interval_ms = DEFAULT_POLLING_INTERVAL_10_MS;
239         internals->link_down_delay_ms = 0;
240         internals->link_up_delay_ms = 0;
241
242         internals->slave_count = 0;
243         internals->active_slave_count = 0;
244         internals->rx_offload_capa = 0;
245         internals->tx_offload_capa = 0;
246         internals->candidate_max_rx_pktlen = 0;
247         internals->max_rx_pktlen = 0;
248
249         /* Initially allow to choose any offload type */
250         internals->flow_type_rss_offloads = ETH_RSS_PROTO_MASK;
251
252         memset(internals->active_slaves, 0, sizeof(internals->active_slaves));
253         memset(internals->slaves, 0, sizeof(internals->slaves));
254
255         /* Set mode 4 default configuration */
256         bond_mode_8023ad_setup(eth_dev, NULL);
257         if (bond_ethdev_mode_set(eth_dev, mode)) {
258                 RTE_BOND_LOG(ERR, "Failed to set bonded device %d mode too %d",
259                                  eth_dev->data->port_id, mode);
260                 goto err;
261         }
262
263         return eth_dev->data->port_id;
264
265 err:
266         rte_free(internals);
267         if (eth_dev != NULL) {
268                 rte_free(eth_dev->data->mac_addrs);
269                 rte_eth_dev_release_port(eth_dev);
270         }
271         return -1;
272 }
273
274 int
275 rte_eth_bond_free(const char *name)
276 {
277         struct rte_eth_dev *eth_dev = NULL;
278         struct bond_dev_private *internals;
279
280         /* now free all data allocation - for eth_dev structure,
281          * dummy pci driver and internal (private) data
282          */
283
284         /* find an ethdev entry */
285         eth_dev = rte_eth_dev_allocated(name);
286         if (eth_dev == NULL)
287                 return -ENODEV;
288
289         internals = eth_dev->data->dev_private;
290         if (internals->slave_count != 0)
291                 return -EBUSY;
292
293         if (eth_dev->data->dev_started == 1) {
294                 bond_ethdev_stop(eth_dev);
295                 bond_ethdev_close(eth_dev);
296         }
297
298         eth_dev->dev_ops = NULL;
299         eth_dev->rx_pkt_burst = NULL;
300         eth_dev->tx_pkt_burst = NULL;
301
302         rte_free(eth_dev->data->dev_private);
303         rte_free(eth_dev->data->mac_addrs);
304
305         rte_eth_dev_release_port(eth_dev);
306
307         return 0;
308 }
309
310 static int
311 __eth_bond_slave_add_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
312 {
313         struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
314         struct bond_dev_private *internals;
315         struct rte_eth_link link_props;
316         struct rte_eth_dev_info dev_info;
317
318         if (valid_slave_port_id(slave_port_id) != 0)
319                 return -1;
320
321         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
322         internals = bonded_eth_dev->data->dev_private;
323
324         slave_eth_dev = &rte_eth_devices[slave_port_id];
325         if (slave_eth_dev->data->dev_flags & RTE_ETH_DEV_BONDED_SLAVE) {
326                 RTE_BOND_LOG(ERR, "Slave device is already a slave of a bonded device");
327                 return -1;
328         }
329
330         /* Add slave details to bonded device */
331         slave_eth_dev->data->dev_flags |= RTE_ETH_DEV_BONDED_SLAVE;
332
333         rte_eth_dev_info_get(slave_port_id, &dev_info);
334         if (dev_info.max_rx_pktlen < internals->max_rx_pktlen) {
335                 RTE_BOND_LOG(ERR, "Slave (port %u) max_rx_pktlen too small",
336                              slave_port_id);
337                 return -1;
338         }
339
340         slave_add(internals, slave_eth_dev);
341
342         /* We need to store slaves reta_size to be able to synchronize RETA for all
343          * slave devices even if its sizes are different.
344          */
345         internals->slaves[internals->slave_count].reta_size = dev_info.reta_size;
346
347         if (internals->slave_count < 1) {
348                 /* if MAC is not user defined then use MAC of first slave add to
349                  * bonded device */
350                 if (!internals->user_defined_mac)
351                         mac_address_set(bonded_eth_dev, slave_eth_dev->data->mac_addrs);
352
353                 /* Inherit eth dev link properties from first slave */
354                 link_properties_set(bonded_eth_dev,
355                                 &(slave_eth_dev->data->dev_link));
356
357                 /* Make primary slave */
358                 internals->primary_port = slave_port_id;
359                 internals->current_primary_port = slave_port_id;
360
361                 /* Inherit queues settings from first slave */
362                 internals->nb_rx_queues = slave_eth_dev->data->nb_rx_queues;
363                 internals->nb_tx_queues = slave_eth_dev->data->nb_tx_queues;
364
365                 internals->reta_size = dev_info.reta_size;
366
367                 /* Take the first dev's offload capabilities */
368                 internals->rx_offload_capa = dev_info.rx_offload_capa;
369                 internals->tx_offload_capa = dev_info.tx_offload_capa;
370                 internals->flow_type_rss_offloads = dev_info.flow_type_rss_offloads;
371
372                 /* Inherit first slave's max rx packet size */
373                 internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
374
375         } else {
376                 /* Check slave link properties are supported if props are set,
377                  * all slaves must be the same */
378                 if (internals->link_props_set) {
379                         if (link_properties_valid(&(bonded_eth_dev->data->dev_link),
380                                                                           &(slave_eth_dev->data->dev_link))) {
381                                 slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
382                                 RTE_BOND_LOG(ERR,
383                                                 "Slave port %d link speed/duplex not supported",
384                                                 slave_port_id);
385                                 return -1;
386                         }
387                 } else {
388                         link_properties_set(bonded_eth_dev,
389                                         &(slave_eth_dev->data->dev_link));
390                 }
391                 internals->rx_offload_capa &= dev_info.rx_offload_capa;
392                 internals->tx_offload_capa &= dev_info.tx_offload_capa;
393                 internals->flow_type_rss_offloads &= dev_info.flow_type_rss_offloads;
394
395                 /* RETA size is GCD of all slaves RETA sizes, so, if all sizes will be
396                  * the power of 2, the lower one is GCD
397                  */
398                 if (internals->reta_size > dev_info.reta_size)
399                         internals->reta_size = dev_info.reta_size;
400
401                 if (!internals->max_rx_pktlen &&
402                     dev_info.max_rx_pktlen < internals->candidate_max_rx_pktlen)
403                         internals->candidate_max_rx_pktlen = dev_info.max_rx_pktlen;
404         }
405
406         bonded_eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf &=
407                         internals->flow_type_rss_offloads;
408
409         internals->slave_count++;
410
411         /* Update all slave devices MACs*/
412         mac_address_slaves_update(bonded_eth_dev);
413
414         if (bonded_eth_dev->data->dev_started) {
415                 if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
416                         slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
417                         RTE_BOND_LOG(ERR, "rte_bond_slaves_configure: port=%d",
418                                         slave_port_id);
419                         return -1;
420                 }
421         }
422
423         /* Register link status change callback with bonded device pointer as
424          * argument*/
425         rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
426                         bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
427
428         /* If bonded device is started then we can add the slave to our active
429          * slave array */
430         if (bonded_eth_dev->data->dev_started) {
431                 rte_eth_link_get_nowait(slave_port_id, &link_props);
432
433                  if (link_props.link_status == ETH_LINK_UP) {
434                         if (internals->active_slave_count == 0 &&
435                             !internals->user_defined_primary_port)
436                                 bond_ethdev_primary_set(internals,
437                                                         slave_port_id);
438
439                         if (find_slave_by_id(internals->active_slaves,
440                                              internals->active_slave_count,
441                                              slave_port_id) == internals->active_slave_count)
442                                 activate_slave(bonded_eth_dev, slave_port_id);
443                 }
444         }
445         return 0;
446
447 }
448
449 int
450 rte_eth_bond_slave_add(uint8_t bonded_port_id, uint8_t slave_port_id)
451 {
452         struct rte_eth_dev *bonded_eth_dev;
453         struct bond_dev_private *internals;
454
455         int retval;
456
457         /* Verify that port id's are valid bonded and slave ports */
458         if (valid_bonded_port_id(bonded_port_id) != 0)
459                 return -1;
460
461         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
462         internals = bonded_eth_dev->data->dev_private;
463
464         rte_spinlock_lock(&internals->lock);
465
466         retval = __eth_bond_slave_add_lock_free(bonded_port_id, slave_port_id);
467
468         rte_spinlock_unlock(&internals->lock);
469
470         return retval;
471 }
472
473 static int
474 __eth_bond_slave_remove_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
475 {
476         struct rte_eth_dev *bonded_eth_dev;
477         struct bond_dev_private *internals;
478         struct rte_eth_dev *slave_eth_dev;
479         int i, slave_idx;
480
481         if (valid_slave_port_id(slave_port_id) != 0)
482                 return -1;
483
484         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
485         internals = bonded_eth_dev->data->dev_private;
486
487         /* first remove from active slave list */
488         slave_idx = find_slave_by_id(internals->active_slaves,
489                 internals->active_slave_count, slave_port_id);
490
491         if (slave_idx < internals->active_slave_count)
492                 deactivate_slave(bonded_eth_dev, slave_port_id);
493
494         slave_idx = -1;
495         /* now find in slave list */
496         for (i = 0; i < internals->slave_count; i++)
497                 if (internals->slaves[i].port_id == slave_port_id) {
498                         slave_idx = i;
499                         break;
500                 }
501
502         if (slave_idx < 0) {
503                 RTE_BOND_LOG(ERR, "Couldn't find slave in port list, slave count %d",
504                                 internals->slave_count);
505                 return -1;
506         }
507
508         /* Un-register link status change callback with bonded device pointer as
509          * argument*/
510         rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
511                         bond_ethdev_lsc_event_callback,
512                         &rte_eth_devices[bonded_port_id].data->port_id);
513
514         /* Restore original MAC address of slave device */
515         mac_address_set(&rte_eth_devices[slave_port_id],
516                         &(internals->slaves[slave_idx].persisted_mac_addr));
517
518         slave_eth_dev = &rte_eth_devices[slave_port_id];
519         slave_remove(internals, slave_eth_dev);
520         slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
521
522         /*  first slave in the active list will be the primary by default,
523          *  otherwise use first device in list */
524         if (internals->current_primary_port == slave_port_id) {
525                 if (internals->active_slave_count > 0)
526                         internals->current_primary_port = internals->active_slaves[0];
527                 else if (internals->slave_count > 0)
528                         internals->current_primary_port = internals->slaves[0].port_id;
529                 else
530                         internals->primary_port = 0;
531         }
532
533         if (internals->active_slave_count < 1) {
534                 /* reset device link properties as no slaves are active */
535                 link_properties_reset(&rte_eth_devices[bonded_port_id]);
536
537                 /* if no slaves are any longer attached to bonded device and MAC is not
538                  * user defined then clear MAC of bonded device as it will be reset
539                  * when a new slave is added */
540                 if (internals->slave_count < 1 && !internals->user_defined_mac)
541                         memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
542                                         sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
543         }
544         if (internals->slave_count == 0) {
545                 internals->rx_offload_capa = 0;
546                 internals->tx_offload_capa = 0;
547                 internals->flow_type_rss_offloads = ETH_RSS_PROTO_MASK;
548                 internals->reta_size = 0;
549                 internals->candidate_max_rx_pktlen = 0;
550                 internals->max_rx_pktlen = 0;
551         }
552         return 0;
553 }
554
555 int
556 rte_eth_bond_slave_remove(uint8_t bonded_port_id, uint8_t slave_port_id)
557 {
558         struct rte_eth_dev *bonded_eth_dev;
559         struct bond_dev_private *internals;
560         int retval;
561
562         if (valid_bonded_port_id(bonded_port_id) != 0)
563                 return -1;
564
565         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
566         internals = bonded_eth_dev->data->dev_private;
567
568         rte_spinlock_lock(&internals->lock);
569
570         retval = __eth_bond_slave_remove_lock_free(bonded_port_id, slave_port_id);
571
572         rte_spinlock_unlock(&internals->lock);
573
574         return retval;
575 }
576
577 int
578 rte_eth_bond_mode_set(uint8_t bonded_port_id, uint8_t mode)
579 {
580         if (valid_bonded_port_id(bonded_port_id) != 0)
581                 return -1;
582
583         return bond_ethdev_mode_set(&rte_eth_devices[bonded_port_id], mode);
584 }
585
586 int
587 rte_eth_bond_mode_get(uint8_t bonded_port_id)
588 {
589         struct bond_dev_private *internals;
590
591         if (valid_bonded_port_id(bonded_port_id) != 0)
592                 return -1;
593
594         internals = rte_eth_devices[bonded_port_id].data->dev_private;
595
596         return internals->mode;
597 }
598
599 int
600 rte_eth_bond_primary_set(uint8_t bonded_port_id, uint8_t slave_port_id)
601 {
602         struct bond_dev_private *internals;
603
604         if (valid_bonded_port_id(bonded_port_id) != 0)
605                 return -1;
606
607         if (valid_slave_port_id(slave_port_id) != 0)
608                 return -1;
609
610         internals =  rte_eth_devices[bonded_port_id].data->dev_private;
611
612         internals->user_defined_primary_port = 1;
613         internals->primary_port = slave_port_id;
614
615         bond_ethdev_primary_set(internals, slave_port_id);
616
617         return 0;
618 }
619
620 int
621 rte_eth_bond_primary_get(uint8_t bonded_port_id)
622 {
623         struct bond_dev_private *internals;
624
625         if (valid_bonded_port_id(bonded_port_id) != 0)
626                 return -1;
627
628         internals = rte_eth_devices[bonded_port_id].data->dev_private;
629
630         if (internals->slave_count < 1)
631                 return -1;
632
633         return internals->current_primary_port;
634 }
635
636 int
637 rte_eth_bond_slaves_get(uint8_t bonded_port_id, uint8_t slaves[], uint8_t len)
638 {
639         struct bond_dev_private *internals;
640         uint8_t i;
641
642         if (valid_bonded_port_id(bonded_port_id) != 0)
643                 return -1;
644
645         if (slaves == NULL)
646                 return -1;
647
648         internals = rte_eth_devices[bonded_port_id].data->dev_private;
649
650         if (internals->slave_count > len)
651                 return -1;
652
653         for (i = 0; i < internals->slave_count; i++)
654                 slaves[i] = internals->slaves[i].port_id;
655
656         return internals->slave_count;
657 }
658
659 int
660 rte_eth_bond_active_slaves_get(uint8_t bonded_port_id, uint8_t slaves[],
661                 uint8_t len)
662 {
663         struct bond_dev_private *internals;
664
665         if (valid_bonded_port_id(bonded_port_id) != 0)
666                 return -1;
667
668         if (slaves == NULL)
669                 return -1;
670
671         internals = rte_eth_devices[bonded_port_id].data->dev_private;
672
673         if (internals->active_slave_count > len)
674                 return -1;
675
676         memcpy(slaves, internals->active_slaves, internals->active_slave_count);
677
678         return internals->active_slave_count;
679 }
680
681 int
682 rte_eth_bond_mac_address_set(uint8_t bonded_port_id,
683                 struct ether_addr *mac_addr)
684 {
685         struct rte_eth_dev *bonded_eth_dev;
686         struct bond_dev_private *internals;
687
688         if (valid_bonded_port_id(bonded_port_id) != 0)
689                 return -1;
690
691         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
692         internals = bonded_eth_dev->data->dev_private;
693
694         /* Set MAC Address of Bonded Device */
695         if (mac_address_set(bonded_eth_dev, mac_addr))
696                 return -1;
697
698         internals->user_defined_mac = 1;
699
700         /* Update all slave devices MACs*/
701         if (internals->slave_count > 0)
702                 return mac_address_slaves_update(bonded_eth_dev);
703
704         return 0;
705 }
706
707 int
708 rte_eth_bond_mac_address_reset(uint8_t bonded_port_id)
709 {
710         struct rte_eth_dev *bonded_eth_dev;
711         struct bond_dev_private *internals;
712
713         if (valid_bonded_port_id(bonded_port_id) != 0)
714                 return -1;
715
716         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
717         internals = bonded_eth_dev->data->dev_private;
718
719         internals->user_defined_mac = 0;
720
721         if (internals->slave_count > 0) {
722                 /* Set MAC Address of Bonded Device */
723                 if (mac_address_set(bonded_eth_dev,
724                                 &internals->slaves[internals->primary_port].persisted_mac_addr)
725                                 != 0) {
726                         RTE_BOND_LOG(ERR, "Failed to set MAC address on bonded device");
727                         return -1;
728                 }
729                 /* Update all slave devices MAC addresses */
730                 return mac_address_slaves_update(bonded_eth_dev);
731         }
732         /* No need to update anything as no slaves present */
733         return 0;
734 }
735
736 int
737 rte_eth_bond_xmit_policy_set(uint8_t bonded_port_id, uint8_t policy)
738 {
739         struct bond_dev_private *internals;
740
741         if (valid_bonded_port_id(bonded_port_id) != 0)
742                 return -1;
743
744         internals = rte_eth_devices[bonded_port_id].data->dev_private;
745
746         switch (policy) {
747         case BALANCE_XMIT_POLICY_LAYER2:
748                 internals->balance_xmit_policy = policy;
749                 internals->xmit_hash = xmit_l2_hash;
750                 break;
751         case BALANCE_XMIT_POLICY_LAYER23:
752                 internals->balance_xmit_policy = policy;
753                 internals->xmit_hash = xmit_l23_hash;
754                 break;
755         case BALANCE_XMIT_POLICY_LAYER34:
756                 internals->balance_xmit_policy = policy;
757                 internals->xmit_hash = xmit_l34_hash;
758                 break;
759
760         default:
761                 return -1;
762         }
763         return 0;
764 }
765
766 int
767 rte_eth_bond_xmit_policy_get(uint8_t bonded_port_id)
768 {
769         struct bond_dev_private *internals;
770
771         if (valid_bonded_port_id(bonded_port_id) != 0)
772                 return -1;
773
774         internals = rte_eth_devices[bonded_port_id].data->dev_private;
775
776         return internals->balance_xmit_policy;
777 }
778
779 int
780 rte_eth_bond_link_monitoring_set(uint8_t bonded_port_id, uint32_t internal_ms)
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         internals->link_status_polling_interval_ms = internal_ms;
789
790         return 0;
791 }
792
793 int
794 rte_eth_bond_link_monitoring_get(uint8_t bonded_port_id)
795 {
796         struct bond_dev_private *internals;
797
798         if (valid_bonded_port_id(bonded_port_id) != 0)
799                 return -1;
800
801         internals = rte_eth_devices[bonded_port_id].data->dev_private;
802
803         return internals->link_status_polling_interval_ms;
804 }
805
806 int
807 rte_eth_bond_link_down_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
808
809 {
810         struct bond_dev_private *internals;
811
812         if (valid_bonded_port_id(bonded_port_id) != 0)
813                 return -1;
814
815         internals = rte_eth_devices[bonded_port_id].data->dev_private;
816         internals->link_down_delay_ms = delay_ms;
817
818         return 0;
819 }
820
821 int
822 rte_eth_bond_link_down_prop_delay_get(uint8_t bonded_port_id)
823 {
824         struct bond_dev_private *internals;
825
826         if (valid_bonded_port_id(bonded_port_id) != 0)
827                 return -1;
828
829         internals = rte_eth_devices[bonded_port_id].data->dev_private;
830
831         return internals->link_down_delay_ms;
832 }
833
834 int
835 rte_eth_bond_link_up_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
836
837 {
838         struct bond_dev_private *internals;
839
840         if (valid_bonded_port_id(bonded_port_id) != 0)
841                 return -1;
842
843         internals = rte_eth_devices[bonded_port_id].data->dev_private;
844         internals->link_up_delay_ms = delay_ms;
845
846         return 0;
847 }
848
849 int
850 rte_eth_bond_link_up_prop_delay_get(uint8_t bonded_port_id)
851 {
852         struct bond_dev_private *internals;
853
854         if (valid_bonded_port_id(bonded_port_id) != 0)
855                 return -1;
856
857         internals = rte_eth_devices[bonded_port_id].data->dev_private;
858
859         return internals->link_up_delay_ms;
860 }