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