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