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