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