bonding: initialize backpointer from pci device to driver
[dpdk.git] / drivers / net / bonding / 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_TLB
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_TLB)
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_TLB
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_TLB) {
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 = &eth_drv->pci_drv;
234
235         pci_id_table = rte_zmalloc_socket(name, sizeof(*pci_id_table), 0, socket_id);
236         if (pci_id_table == NULL) {
237                 RTE_BOND_LOG(ERR, "Unable to malloc pci_id_table on socket");
238                 goto err;
239         }
240         pci_id_table->device_id = PCI_ANY_ID;
241         pci_id_table->subsystem_device_id = PCI_ANY_ID;
242         pci_id_table->vendor_id = PCI_ANY_ID;
243         pci_id_table->subsystem_vendor_id = PCI_ANY_ID;
244
245         pci_drv->id_table = pci_id_table;
246         pci_drv->drv_flags = RTE_PCI_DRV_INTR_LSC;
247
248         internals = rte_zmalloc_socket(name, sizeof(*internals), 0, socket_id);
249         if (internals == NULL) {
250                 RTE_BOND_LOG(ERR, "Unable to malloc internals on socket");
251                 goto err;
252         }
253
254         /* reserve an ethdev entry */
255         eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
256         if (eth_dev == NULL) {
257                 RTE_BOND_LOG(ERR, "Unable to allocate rte_eth_dev");
258                 goto err;
259         }
260
261         pci_dev->numa_node = socket_id;
262         pci_drv->name = driver_name;
263         pci_dev->driver = pci_drv;
264
265         eth_dev->driver = eth_drv;
266         eth_dev->data->dev_private = internals;
267         eth_dev->data->nb_rx_queues = (uint16_t)1;
268         eth_dev->data->nb_tx_queues = (uint16_t)1;
269
270         TAILQ_INIT(&(eth_dev->link_intr_cbs));
271
272         eth_dev->data->dev_link.link_status = 0;
273
274         eth_dev->data->mac_addrs = rte_zmalloc_socket(name, ETHER_ADDR_LEN, 0,
275                         socket_id);
276
277         eth_dev->data->dev_started = 0;
278         eth_dev->data->promiscuous = 0;
279         eth_dev->data->scattered_rx = 0;
280         eth_dev->data->all_multicast = 0;
281
282         eth_dev->dev_ops = &default_dev_ops;
283         eth_dev->pci_dev = pci_dev;
284
285         rte_spinlock_init(&internals->lock);
286
287         internals->port_id = eth_dev->data->port_id;
288         internals->mode = BONDING_MODE_INVALID;
289         internals->current_primary_port = 0;
290         internals->balance_xmit_policy = BALANCE_XMIT_POLICY_LAYER2;
291         internals->xmit_hash = xmit_l2_hash;
292         internals->user_defined_mac = 0;
293         internals->link_props_set = 0;
294
295         internals->link_status_polling_enabled = 0;
296
297         internals->link_status_polling_interval_ms = DEFAULT_POLLING_INTERVAL_10_MS;
298         internals->link_down_delay_ms = 0;
299         internals->link_up_delay_ms = 0;
300
301         internals->slave_count = 0;
302         internals->active_slave_count = 0;
303         internals->rx_offload_capa = 0;
304         internals->tx_offload_capa = 0;
305
306         memset(internals->active_slaves, 0, sizeof(internals->active_slaves));
307         memset(internals->slaves, 0, sizeof(internals->slaves));
308
309         /* Set mode 4 default configuration */
310         bond_mode_8023ad_setup(eth_dev, NULL);
311         if (bond_ethdev_mode_set(eth_dev, mode)) {
312                 RTE_BOND_LOG(ERR, "Failed to set bonded device %d mode too %d",
313                                  eth_dev->data->port_id, mode);
314                 goto err;
315         }
316
317         return eth_dev->data->port_id;
318
319 err:
320         rte_free(pci_dev);
321         rte_free(pci_id_table);
322         rte_free(eth_drv);
323         rte_free(internals);
324
325         return -1;
326 }
327
328 static int
329 __eth_bond_slave_add_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
330 {
331         struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
332         struct bond_dev_private *internals;
333         struct bond_dev_private *temp_internals;
334         struct rte_eth_link link_props;
335         struct rte_eth_dev_info dev_info;
336
337         int i, j;
338
339         if (valid_slave_port_id(slave_port_id) != 0)
340                 return -1;
341
342         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
343         internals = bonded_eth_dev->data->dev_private;
344
345         /* Verify that new slave device is not already a slave of another
346          * bonded device */
347         for (i = rte_eth_dev_count()-1; i >= 0; i--) {
348                 if (valid_bonded_ethdev(&rte_eth_devices[i]) == 0) {
349                         temp_internals = rte_eth_devices[i].data->dev_private;
350
351                         for (j = 0; j < temp_internals->slave_count; j++) {
352                                 /* Device already a slave of a bonded device */
353                                 if (temp_internals->slaves[j].port_id == slave_port_id) {
354                                         RTE_BOND_LOG(ERR, "Slave port %d is already a slave",
355                                                         slave_port_id);
356                                         return -1;
357                                 }
358                         }
359                 }
360         }
361
362         slave_eth_dev = &rte_eth_devices[slave_port_id];
363
364         /* Add slave details to bonded device */
365         slave_add(internals, slave_eth_dev);
366
367         rte_eth_dev_info_get(slave_port_id, &dev_info);
368
369         if (internals->slave_count < 1) {
370                 /* if MAC is not user defined then use MAC of first slave add to
371                  * bonded device */
372                 if (!internals->user_defined_mac)
373                         mac_address_set(bonded_eth_dev, slave_eth_dev->data->mac_addrs);
374
375                 /* Inherit eth dev link properties from first slave */
376                 link_properties_set(bonded_eth_dev,
377                                 &(slave_eth_dev->data->dev_link));
378
379                 /* Make primary slave */
380                 internals->primary_port = slave_port_id;
381
382                 /* Take the first dev's offload capabilities */
383                 internals->rx_offload_capa = dev_info.rx_offload_capa;
384                 internals->tx_offload_capa = dev_info.tx_offload_capa;
385
386         } else {
387                 /* Check slave link properties are supported if props are set,
388                  * all slaves must be the same */
389                 if (internals->link_props_set) {
390                         if (link_properties_valid(&(bonded_eth_dev->data->dev_link),
391                                                                           &(slave_eth_dev->data->dev_link))) {
392                                 RTE_BOND_LOG(ERR,
393                                                 "Slave port %d link speed/duplex not supported",
394                                                 slave_port_id);
395                                 return -1;
396                         }
397                 } else {
398                         link_properties_set(bonded_eth_dev,
399                                         &(slave_eth_dev->data->dev_link));
400                 }
401                 internals->rx_offload_capa &= dev_info.rx_offload_capa;
402                 internals->tx_offload_capa &= dev_info.tx_offload_capa;
403         }
404
405         internals->slave_count++;
406
407         /* Update all slave devices MACs*/
408         mac_address_slaves_update(bonded_eth_dev);
409
410         if (bonded_eth_dev->data->dev_started) {
411                 if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
412                         RTE_BOND_LOG(ERR, "rte_bond_slaves_configure: port=%d",
413                                         slave_port_id);
414                         return -1;
415                 }
416         }
417
418         /* Register link status change callback with bonded device pointer as
419          * argument*/
420         rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
421                         bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
422
423         /* If bonded device is started then we can add the slave to our active
424          * slave array */
425         if (bonded_eth_dev->data->dev_started) {
426                 rte_eth_link_get_nowait(slave_port_id, &link_props);
427
428                  if (link_props.link_status == 1)
429                         activate_slave(bonded_eth_dev, slave_port_id);
430         }
431         return 0;
432
433 }
434
435 int
436 rte_eth_bond_slave_add(uint8_t bonded_port_id, uint8_t slave_port_id)
437 {
438         struct rte_eth_dev *bonded_eth_dev;
439         struct bond_dev_private *internals;
440
441         int retval;
442
443         /* Verify that port id's are valid bonded and slave ports */
444         if (valid_bonded_port_id(bonded_port_id) != 0)
445                 return -1;
446
447         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
448         internals = bonded_eth_dev->data->dev_private;
449
450         rte_spinlock_lock(&internals->lock);
451
452         retval = __eth_bond_slave_add_lock_free(bonded_port_id, slave_port_id);
453
454         rte_spinlock_unlock(&internals->lock);
455
456         return retval;
457 }
458
459 static int
460 __eth_bond_slave_remove_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
461 {
462         struct rte_eth_dev *bonded_eth_dev;
463         struct bond_dev_private *internals;
464
465         int i, slave_idx;
466
467         if (valid_slave_port_id(slave_port_id) != 0)
468                 return -1;
469
470         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
471         internals = bonded_eth_dev->data->dev_private;
472
473         /* first remove from active slave list */
474         slave_idx = find_slave_by_id(internals->active_slaves,
475                 internals->active_slave_count, slave_port_id);
476
477         if (slave_idx < internals->active_slave_count)
478                 deactivate_slave(bonded_eth_dev, slave_port_id);
479
480         slave_idx = -1;
481         /* now find in slave list */
482         for (i = 0; i < internals->slave_count; i++)
483                 if (internals->slaves[i].port_id == slave_port_id) {
484                         slave_idx = i;
485                         break;
486                 }
487
488         if (slave_idx < 0) {
489                 RTE_BOND_LOG(ERR, "Couldn't find slave in port list, slave count %d",
490                                 internals->slave_count);
491                 return -1;
492         }
493
494         /* Un-register link status change callback with bonded device pointer as
495          * argument*/
496         rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
497                         bond_ethdev_lsc_event_callback,
498                         &rte_eth_devices[bonded_port_id].data->port_id);
499
500         /* Restore original MAC address of slave device */
501         mac_address_set(&rte_eth_devices[slave_port_id],
502                         &(internals->slaves[slave_idx].persisted_mac_addr));
503
504         slave_remove(internals, &rte_eth_devices[slave_port_id]);
505
506         /*  first slave in the active list will be the primary by default,
507          *  otherwise use first device in list */
508         if (internals->current_primary_port == slave_port_id) {
509                 if (internals->active_slave_count > 0)
510                         internals->current_primary_port = internals->active_slaves[0];
511                 else if (internals->slave_count > 0)
512                         internals->current_primary_port = internals->slaves[0].port_id;
513                 else
514                         internals->primary_port = 0;
515         }
516
517         if (internals->active_slave_count < 1) {
518                 /* reset device link properties as no slaves are active */
519                 link_properties_reset(&rte_eth_devices[bonded_port_id]);
520
521                 /* if no slaves are any longer attached to bonded device and MAC is not
522                  * user defined then clear MAC of bonded device as it will be reset
523                  * when a new slave is added */
524                 if (internals->slave_count < 1 && !internals->user_defined_mac)
525                         memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
526                                         sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
527         }
528         if (internals->slave_count == 0) {
529                 internals->rx_offload_capa = 0;
530                 internals->tx_offload_capa = 0;
531         }
532         return 0;
533 }
534
535 int
536 rte_eth_bond_slave_remove(uint8_t bonded_port_id, uint8_t slave_port_id)
537 {
538         struct rte_eth_dev *bonded_eth_dev;
539         struct bond_dev_private *internals;
540         int retval;
541
542         if (valid_bonded_port_id(bonded_port_id) != 0)
543                 return -1;
544
545         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
546         internals = bonded_eth_dev->data->dev_private;
547
548         rte_spinlock_lock(&internals->lock);
549
550         retval = __eth_bond_slave_remove_lock_free(bonded_port_id, slave_port_id);
551
552         rte_spinlock_unlock(&internals->lock);
553
554         return retval;
555 }
556
557 int
558 rte_eth_bond_mode_set(uint8_t bonded_port_id, uint8_t mode)
559 {
560         if (valid_bonded_port_id(bonded_port_id) != 0)
561                 return -1;
562
563         return bond_ethdev_mode_set(&rte_eth_devices[bonded_port_id], mode);
564 }
565
566 int
567 rte_eth_bond_mode_get(uint8_t bonded_port_id)
568 {
569         struct bond_dev_private *internals;
570
571         if (valid_bonded_port_id(bonded_port_id) != 0)
572                 return -1;
573
574         internals = rte_eth_devices[bonded_port_id].data->dev_private;
575
576         return internals->mode;
577 }
578
579 int
580 rte_eth_bond_primary_set(uint8_t bonded_port_id, uint8_t slave_port_id)
581 {
582         struct bond_dev_private *internals;
583
584         if (valid_bonded_port_id(bonded_port_id) != 0)
585                 return -1;
586
587         if (valid_slave_port_id(slave_port_id) != 0)
588                 return -1;
589
590         internals =  rte_eth_devices[bonded_port_id].data->dev_private;
591
592         internals->user_defined_primary_port = 1;
593         internals->primary_port = slave_port_id;
594
595         bond_ethdev_primary_set(internals, slave_port_id);
596
597         return 0;
598 }
599
600 int
601 rte_eth_bond_primary_get(uint8_t bonded_port_id)
602 {
603         struct bond_dev_private *internals;
604
605         if (valid_bonded_port_id(bonded_port_id) != 0)
606                 return -1;
607
608         internals = rte_eth_devices[bonded_port_id].data->dev_private;
609
610         if (internals->slave_count < 1)
611                 return -1;
612
613         return internals->current_primary_port;
614 }
615
616 int
617 rte_eth_bond_slaves_get(uint8_t bonded_port_id, uint8_t slaves[], uint8_t len)
618 {
619         struct bond_dev_private *internals;
620         uint8_t i;
621
622         if (valid_bonded_port_id(bonded_port_id) != 0)
623                 return -1;
624
625         if (slaves == NULL)
626                 return -1;
627
628         internals = rte_eth_devices[bonded_port_id].data->dev_private;
629
630         if (internals->slave_count > len)
631                 return -1;
632
633         for (i = 0; i < internals->slave_count; i++)
634                 slaves[i] = internals->slaves[i].port_id;
635
636         return internals->slave_count;
637 }
638
639 int
640 rte_eth_bond_active_slaves_get(uint8_t bonded_port_id, uint8_t slaves[],
641                 uint8_t len)
642 {
643         struct bond_dev_private *internals;
644
645         if (valid_bonded_port_id(bonded_port_id) != 0)
646                 return -1;
647
648         if (slaves == NULL)
649                 return -1;
650
651         internals = rte_eth_devices[bonded_port_id].data->dev_private;
652
653         if (internals->active_slave_count > len)
654                 return -1;
655
656         memcpy(slaves, internals->active_slaves, internals->active_slave_count);
657
658         return internals->active_slave_count;
659 }
660
661 int
662 rte_eth_bond_mac_address_set(uint8_t bonded_port_id,
663                 struct ether_addr *mac_addr)
664 {
665         struct rte_eth_dev *bonded_eth_dev;
666         struct bond_dev_private *internals;
667
668         if (valid_bonded_port_id(bonded_port_id) != 0)
669                 return -1;
670
671         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
672         internals = bonded_eth_dev->data->dev_private;
673
674         /* Set MAC Address of Bonded Device */
675         if (mac_address_set(bonded_eth_dev, mac_addr))
676                 return -1;
677
678         internals->user_defined_mac = 1;
679
680         /* Update all slave devices MACs*/
681         if (internals->slave_count > 0)
682                 return mac_address_slaves_update(bonded_eth_dev);
683
684         return 0;
685 }
686
687 int
688 rte_eth_bond_mac_address_reset(uint8_t bonded_port_id)
689 {
690         struct rte_eth_dev *bonded_eth_dev;
691         struct bond_dev_private *internals;
692
693         if (valid_bonded_port_id(bonded_port_id) != 0)
694                 return -1;
695
696         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
697         internals = bonded_eth_dev->data->dev_private;
698
699         internals->user_defined_mac = 0;
700
701         if (internals->slave_count > 0) {
702                 /* Set MAC Address of Bonded Device */
703                 if (mac_address_set(bonded_eth_dev,
704                                 &internals->slaves[internals->primary_port].persisted_mac_addr)
705                                 != 0) {
706                         RTE_BOND_LOG(ERR, "Failed to set MAC address on bonded device");
707                         return -1;
708                 }
709                 /* Update all slave devices MAC addresses */
710                 return mac_address_slaves_update(bonded_eth_dev);
711         }
712         /* No need to update anything as no slaves present */
713         return 0;
714 }
715
716 int
717 rte_eth_bond_xmit_policy_set(uint8_t bonded_port_id, uint8_t policy)
718 {
719         struct bond_dev_private *internals;
720
721         if (valid_bonded_port_id(bonded_port_id) != 0)
722                 return -1;
723
724         internals = rte_eth_devices[bonded_port_id].data->dev_private;
725
726         switch (policy) {
727         case BALANCE_XMIT_POLICY_LAYER2:
728                 internals->balance_xmit_policy = policy;
729                 internals->xmit_hash = xmit_l2_hash;
730                 break;
731         case BALANCE_XMIT_POLICY_LAYER23:
732                 internals->balance_xmit_policy = policy;
733                 internals->xmit_hash = xmit_l23_hash;
734                 break;
735         case BALANCE_XMIT_POLICY_LAYER34:
736                 internals->balance_xmit_policy = policy;
737                 internals->xmit_hash = xmit_l34_hash;
738                 break;
739
740         default:
741                 return -1;
742         }
743         return 0;
744 }
745
746 int
747 rte_eth_bond_xmit_policy_get(uint8_t bonded_port_id)
748 {
749         struct bond_dev_private *internals;
750
751         if (valid_bonded_port_id(bonded_port_id) != 0)
752                 return -1;
753
754         internals = rte_eth_devices[bonded_port_id].data->dev_private;
755
756         return internals->balance_xmit_policy;
757 }
758
759 int
760 rte_eth_bond_link_monitoring_set(uint8_t bonded_port_id, uint32_t internal_ms)
761 {
762         struct bond_dev_private *internals;
763
764         if (valid_bonded_port_id(bonded_port_id) != 0)
765                 return -1;
766
767         internals = rte_eth_devices[bonded_port_id].data->dev_private;
768         internals->link_status_polling_interval_ms = internal_ms;
769
770         return 0;
771 }
772
773 int
774 rte_eth_bond_link_monitoring_get(uint8_t bonded_port_id)
775 {
776         struct bond_dev_private *internals;
777
778         if (valid_bonded_port_id(bonded_port_id) != 0)
779                 return -1;
780
781         internals = rte_eth_devices[bonded_port_id].data->dev_private;
782
783         return internals->link_status_polling_interval_ms;
784 }
785
786 int
787 rte_eth_bond_link_down_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
788
789 {
790         struct bond_dev_private *internals;
791
792         if (valid_bonded_port_id(bonded_port_id) != 0)
793                 return -1;
794
795         internals = rte_eth_devices[bonded_port_id].data->dev_private;
796         internals->link_down_delay_ms = delay_ms;
797
798         return 0;
799 }
800
801 int
802 rte_eth_bond_link_down_prop_delay_get(uint8_t bonded_port_id)
803 {
804         struct bond_dev_private *internals;
805
806         if (valid_bonded_port_id(bonded_port_id) != 0)
807                 return -1;
808
809         internals = rte_eth_devices[bonded_port_id].data->dev_private;
810
811         return internals->link_down_delay_ms;
812 }
813
814 int
815 rte_eth_bond_link_up_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
816
817 {
818         struct bond_dev_private *internals;
819
820         if (valid_bonded_port_id(bonded_port_id) != 0)
821                 return -1;
822
823         internals = rte_eth_devices[bonded_port_id].data->dev_private;
824         internals->link_up_delay_ms = delay_ms;
825
826         return 0;
827 }
828
829 int
830 rte_eth_bond_link_up_prop_delay_get(uint8_t bonded_port_id)
831 {
832         struct bond_dev_private *internals;
833
834         if (valid_bonded_port_id(bonded_port_id) != 0)
835                 return -1;
836
837         internals = rte_eth_devices[bonded_port_id].data->dev_private;
838
839         return internals->link_up_delay_ms;
840 }