pmd: remove useless null checks before rte_free
[dpdk.git] / lib / librte_pmd_bond / rte_eth_bond_api.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <string.h>
35
36 #include <rte_mbuf.h>
37 #include <rte_malloc.h>
38 #include <rte_ethdev.h>
39 #include <rte_tcp.h>
40
41 #include "rte_eth_bond.h"
42 #include "rte_eth_bond_private.h"
43 #include "rte_eth_bond_8023ad_private.h"
44
45 #define DEFAULT_POLLING_INTERVAL_10_MS (10)
46
47 int
48 valid_bonded_ethdev(struct rte_eth_dev *eth_dev)
49 {
50         size_t len;
51
52         /* Check valid pointer */
53         if (eth_dev->driver->pci_drv.name == NULL || driver_name == NULL)
54                 return -1;
55
56         /* Check string lengths are equal */
57         len = strlen(driver_name);
58         if (strlen(eth_dev->driver->pci_drv.name) != len)
59                 return -1;
60
61         /* Compare strings */
62         return strncmp(eth_dev->driver->pci_drv.name, driver_name, len);
63 }
64
65 int
66 valid_port_id(uint8_t port_id)
67 {
68         /* Verify that port id is valid */
69         int ethdev_count = rte_eth_dev_count();
70         if (port_id >= ethdev_count) {
71                 RTE_BOND_LOG(ERR, "Port Id %d is greater than rte_eth_dev_count %d",
72                                 port_id, ethdev_count);
73                 return -1;
74         }
75
76         return 0;
77 }
78
79 int
80 valid_bonded_port_id(uint8_t port_id)
81 {
82         /* Verify that port id's are valid */
83         if (valid_port_id(port_id))
84                 return -1;
85
86         /* Verify that bonded_port_id refers to a bonded port */
87         if (valid_bonded_ethdev(&rte_eth_devices[port_id])) {
88                 RTE_BOND_LOG(ERR, "Specified port Id %d is not a bonded eth_dev device",
89                                 port_id);
90                 return -1;
91         }
92
93         return 0;
94 }
95
96 int
97 valid_slave_port_id(uint8_t port_id)
98 {
99         /* Verify that port id's are valid */
100         if (valid_port_id(port_id))
101                 return -1;
102
103         /* Verify that port_id refers to a non bonded port */
104         if (!valid_bonded_ethdev(&rte_eth_devices[port_id]))
105                 return -1;
106
107         return 0;
108 }
109
110 void
111 activate_slave(struct rte_eth_dev *eth_dev, uint8_t port_id)
112 {
113         struct bond_dev_private *internals = eth_dev->data->dev_private;
114         uint8_t active_count = internals->active_slave_count;
115
116         if (internals->mode == BONDING_MODE_8023AD)
117                 bond_mode_8023ad_activate_slave(eth_dev, port_id);
118
119         if (internals->mode == BONDING_MODE_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
241         pci_drv->id_table = pci_id_table;
242
243         pci_drv->id_table->device_id = PCI_ANY_ID;
244         pci_drv->id_table->subsystem_device_id = PCI_ANY_ID;
245         pci_drv->id_table->vendor_id = PCI_ANY_ID;
246         pci_drv->id_table->subsystem_vendor_id = PCI_ANY_ID;
247
248         pci_drv->drv_flags = RTE_PCI_DRV_INTR_LSC;
249
250         internals = rte_zmalloc_socket(name, sizeof(*internals), 0, socket_id);
251         if (internals == NULL) {
252                 RTE_BOND_LOG(ERR, "Unable to malloc internals on socket");
253                 goto err;
254         }
255
256         /* reserve an ethdev entry */
257         eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
258         if (eth_dev == NULL) {
259                 RTE_BOND_LOG(ERR, "Unable to allocate rte_eth_dev");
260                 goto err;
261         }
262
263         pci_dev->numa_node = socket_id;
264         pci_drv->name = driver_name;
265
266         eth_dev->driver = eth_drv;
267         eth_dev->data->dev_private = internals;
268         eth_dev->data->nb_rx_queues = (uint16_t)1;
269         eth_dev->data->nb_tx_queues = (uint16_t)1;
270
271         TAILQ_INIT(&(eth_dev->link_intr_cbs));
272
273         eth_dev->data->dev_link.link_status = 0;
274
275         eth_dev->data->mac_addrs = rte_zmalloc_socket(name, ETHER_ADDR_LEN, 0,
276                         socket_id);
277
278         eth_dev->data->dev_started = 0;
279         eth_dev->data->promiscuous = 0;
280         eth_dev->data->scattered_rx = 0;
281         eth_dev->data->all_multicast = 0;
282
283         eth_dev->dev_ops = &default_dev_ops;
284         eth_dev->pci_dev = pci_dev;
285
286         rte_spinlock_init(&internals->lock);
287
288         internals->port_id = eth_dev->data->port_id;
289         internals->mode = BONDING_MODE_INVALID;
290         internals->current_primary_port = 0;
291         internals->balance_xmit_policy = BALANCE_XMIT_POLICY_LAYER2;
292         internals->xmit_hash = xmit_l2_hash;
293         internals->user_defined_mac = 0;
294         internals->link_props_set = 0;
295
296         internals->link_status_polling_enabled = 0;
297
298         internals->link_status_polling_interval_ms = DEFAULT_POLLING_INTERVAL_10_MS;
299         internals->link_down_delay_ms = 0;
300         internals->link_up_delay_ms = 0;
301
302         internals->slave_count = 0;
303         internals->active_slave_count = 0;
304         internals->rx_offload_capa = 0;
305         internals->tx_offload_capa = 0;
306
307         memset(internals->active_slaves, 0, sizeof(internals->active_slaves));
308         memset(internals->slaves, 0, sizeof(internals->slaves));
309
310         /* Set mode 4 default configuration */
311         bond_mode_8023ad_setup(eth_dev, NULL);
312         if (bond_ethdev_mode_set(eth_dev, mode)) {
313                 RTE_BOND_LOG(ERR, "Failed to set bonded device %d mode too %d",
314                                  eth_dev->data->port_id, mode);
315                 goto err;
316         }
317
318         return eth_dev->data->port_id;
319
320 err:
321         rte_free(pci_dev);
322         rte_free(pci_id_table);
323         rte_free(eth_drv);
324         rte_free(internals);
325
326         return -1;
327 }
328
329 static int
330 __eth_bond_slave_add_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
331 {
332         struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
333         struct bond_dev_private *internals;
334         struct bond_dev_private *temp_internals;
335         struct rte_eth_link link_props;
336         struct rte_eth_dev_info dev_info;
337
338         int i, j;
339
340         if (valid_slave_port_id(slave_port_id) != 0)
341                 return -1;
342
343         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
344         internals = bonded_eth_dev->data->dev_private;
345
346         /* Verify that new slave device is not already a slave of another
347          * bonded device */
348         for (i = rte_eth_dev_count()-1; i >= 0; i--) {
349                 if (valid_bonded_ethdev(&rte_eth_devices[i]) == 0) {
350                         temp_internals = rte_eth_devices[i].data->dev_private;
351
352                         for (j = 0; j < temp_internals->slave_count; j++) {
353                                 /* Device already a slave of a bonded device */
354                                 if (temp_internals->slaves[j].port_id == slave_port_id) {
355                                         RTE_BOND_LOG(ERR, "Slave port %d is already a slave",
356                                                         slave_port_id);
357                                         return -1;
358                                 }
359                         }
360                 }
361         }
362
363         slave_eth_dev = &rte_eth_devices[slave_port_id];
364
365         /* Add slave details to bonded device */
366         slave_add(internals, slave_eth_dev);
367
368         memset(&dev_info, 0, sizeof(dev_info));
369         rte_eth_dev_info_get(slave_port_id, &dev_info);
370
371         if (internals->slave_count < 1) {
372                 /* if MAC is not user defined then use MAC of first slave add to
373                  * bonded device */
374                 if (!internals->user_defined_mac)
375                         mac_address_set(bonded_eth_dev, slave_eth_dev->data->mac_addrs);
376
377                 /* Inherit eth dev link properties from first slave */
378                 link_properties_set(bonded_eth_dev,
379                                 &(slave_eth_dev->data->dev_link));
380
381                 /* Make primary slave */
382                 internals->primary_port = slave_port_id;
383
384                 /* Take the first dev's offload capabilities */
385                 internals->rx_offload_capa = dev_info.rx_offload_capa;
386                 internals->tx_offload_capa = dev_info.tx_offload_capa;
387
388         } else {
389                 /* Check slave link properties are supported if props are set,
390                  * all slaves must be the same */
391                 if (internals->link_props_set) {
392                         if (link_properties_valid(&(bonded_eth_dev->data->dev_link),
393                                                                           &(slave_eth_dev->data->dev_link))) {
394                                 RTE_BOND_LOG(ERR,
395                                                 "Slave port %d link speed/duplex not supported",
396                                                 slave_port_id);
397                                 return -1;
398                         }
399                 } else {
400                         link_properties_set(bonded_eth_dev,
401                                         &(slave_eth_dev->data->dev_link));
402                 }
403                 internals->rx_offload_capa &= dev_info.rx_offload_capa;
404                 internals->tx_offload_capa &= dev_info.tx_offload_capa;
405         }
406
407         internals->slave_count++;
408
409         /* Update all slave devices MACs*/
410         mac_address_slaves_update(bonded_eth_dev);
411
412         if (bonded_eth_dev->data->dev_started) {
413                 if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
414                         RTE_BOND_LOG(ERR, "rte_bond_slaves_configure: port=%d",
415                                         slave_port_id);
416                         return -1;
417                 }
418         }
419
420         /* Register link status change callback with bonded device pointer as
421          * argument*/
422         rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
423                         bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
424
425         /* If bonded device is started then we can add the slave to our active
426          * slave array */
427         if (bonded_eth_dev->data->dev_started) {
428                 rte_eth_link_get_nowait(slave_port_id, &link_props);
429
430                  if (link_props.link_status == 1)
431                         activate_slave(bonded_eth_dev, slave_port_id);
432         }
433         return 0;
434
435 }
436
437 int
438 rte_eth_bond_slave_add(uint8_t bonded_port_id, uint8_t slave_port_id)
439 {
440         struct rte_eth_dev *bonded_eth_dev;
441         struct bond_dev_private *internals;
442
443         int retval;
444
445         /* Verify that port id's are valid bonded and slave ports */
446         if (valid_bonded_port_id(bonded_port_id) != 0)
447                 return -1;
448
449         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
450         internals = bonded_eth_dev->data->dev_private;
451
452         rte_spinlock_lock(&internals->lock);
453
454         retval = __eth_bond_slave_add_lock_free(bonded_port_id, slave_port_id);
455
456         rte_spinlock_unlock(&internals->lock);
457
458         return retval;
459 }
460
461 static int
462 __eth_bond_slave_remove_lock_free(uint8_t bonded_port_id, uint8_t slave_port_id)
463 {
464         struct rte_eth_dev *bonded_eth_dev;
465         struct bond_dev_private *internals;
466
467         int i, slave_idx;
468
469         if (valid_slave_port_id(slave_port_id) != 0)
470                 return -1;
471
472         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
473         internals = bonded_eth_dev->data->dev_private;
474
475         /* first remove from active slave list */
476         slave_idx = find_slave_by_id(internals->active_slaves,
477                 internals->active_slave_count, slave_port_id);
478
479         if (slave_idx < internals->active_slave_count)
480                 deactivate_slave(bonded_eth_dev, slave_port_id);
481
482         slave_idx = -1;
483         /* now find in slave list */
484         for (i = 0; i < internals->slave_count; i++)
485                 if (internals->slaves[i].port_id == slave_port_id) {
486                         slave_idx = i;
487                         break;
488                 }
489
490         if (slave_idx < 0) {
491                 RTE_BOND_LOG(ERR, "Couldn't find slave in port list, slave count %d",
492                                 internals->slave_count);
493                 return -1;
494         }
495
496         /* Un-register link status change callback with bonded device pointer as
497          * argument*/
498         rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
499                         bond_ethdev_lsc_event_callback,
500                         &rte_eth_devices[bonded_port_id].data->port_id);
501
502         /* Restore original MAC address of slave device */
503         mac_address_set(&rte_eth_devices[slave_port_id],
504                         &(internals->slaves[slave_idx].persisted_mac_addr));
505
506         slave_remove(internals, &rte_eth_devices[slave_port_id]);
507
508         /*  first slave in the active list will be the primary by default,
509          *  otherwise use first device in list */
510         if (internals->current_primary_port == slave_port_id) {
511                 if (internals->active_slave_count > 0)
512                         internals->current_primary_port = internals->active_slaves[0];
513                 else if (internals->slave_count > 0)
514                         internals->current_primary_port = internals->slaves[0].port_id;
515                 else
516                         internals->primary_port = 0;
517         }
518
519         if (internals->active_slave_count < 1) {
520                 /* reset device link properties as no slaves are active */
521                 link_properties_reset(&rte_eth_devices[bonded_port_id]);
522
523                 /* if no slaves are any longer attached to bonded device and MAC is not
524                  * user defined then clear MAC of bonded device as it will be reset
525                  * when a new slave is added */
526                 if (internals->slave_count < 1 && !internals->user_defined_mac)
527                         memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
528                                         sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
529         }
530         if (internals->slave_count == 0) {
531                 internals->rx_offload_capa = 0;
532                 internals->tx_offload_capa = 0;
533         }
534         return 0;
535 }
536
537 int
538 rte_eth_bond_slave_remove(uint8_t bonded_port_id, uint8_t slave_port_id)
539 {
540         struct rte_eth_dev *bonded_eth_dev;
541         struct bond_dev_private *internals;
542         int retval;
543
544         if (valid_bonded_port_id(bonded_port_id) != 0)
545                 return -1;
546
547         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
548         internals = bonded_eth_dev->data->dev_private;
549
550         rte_spinlock_lock(&internals->lock);
551
552         retval = __eth_bond_slave_remove_lock_free(bonded_port_id, slave_port_id);
553
554         rte_spinlock_unlock(&internals->lock);
555
556         return retval;
557 }
558
559 int
560 rte_eth_bond_mode_set(uint8_t bonded_port_id, uint8_t mode)
561 {
562         if (valid_bonded_port_id(bonded_port_id) != 0)
563                 return -1;
564
565         return bond_ethdev_mode_set(&rte_eth_devices[bonded_port_id], mode);
566 }
567
568 int
569 rte_eth_bond_mode_get(uint8_t bonded_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         internals = rte_eth_devices[bonded_port_id].data->dev_private;
577
578         return internals->mode;
579 }
580
581 int
582 rte_eth_bond_primary_set(uint8_t bonded_port_id, uint8_t slave_port_id)
583 {
584         struct bond_dev_private *internals;
585
586         if (valid_bonded_port_id(bonded_port_id) != 0)
587                 return -1;
588
589         if (valid_slave_port_id(slave_port_id) != 0)
590                 return -1;
591
592         internals =  rte_eth_devices[bonded_port_id].data->dev_private;
593
594         internals->user_defined_primary_port = 1;
595         internals->primary_port = slave_port_id;
596
597         bond_ethdev_primary_set(internals, slave_port_id);
598
599         return 0;
600 }
601
602 int
603 rte_eth_bond_primary_get(uint8_t bonded_port_id)
604 {
605         struct bond_dev_private *internals;
606
607         if (valid_bonded_port_id(bonded_port_id) != 0)
608                 return -1;
609
610         internals = rte_eth_devices[bonded_port_id].data->dev_private;
611
612         if (internals->slave_count < 1)
613                 return -1;
614
615         return internals->current_primary_port;
616 }
617
618 int
619 rte_eth_bond_slaves_get(uint8_t bonded_port_id, uint8_t slaves[], uint8_t len)
620 {
621         struct bond_dev_private *internals;
622         uint8_t i;
623
624         if (valid_bonded_port_id(bonded_port_id) != 0)
625                 return -1;
626
627         if (slaves == NULL)
628                 return -1;
629
630         internals = rte_eth_devices[bonded_port_id].data->dev_private;
631
632         if (internals->slave_count > len)
633                 return -1;
634
635         for (i = 0; i < internals->slave_count; i++)
636                 slaves[i] = internals->slaves[i].port_id;
637
638         return internals->slave_count;
639 }
640
641 int
642 rte_eth_bond_active_slaves_get(uint8_t bonded_port_id, uint8_t slaves[],
643                 uint8_t len)
644 {
645         struct bond_dev_private *internals;
646
647         if (valid_bonded_port_id(bonded_port_id) != 0)
648                 return -1;
649
650         if (slaves == NULL)
651                 return -1;
652
653         internals = rte_eth_devices[bonded_port_id].data->dev_private;
654
655         if (internals->active_slave_count > len)
656                 return -1;
657
658         memcpy(slaves, internals->active_slaves, internals->active_slave_count);
659
660         return internals->active_slave_count;
661 }
662
663 int
664 rte_eth_bond_mac_address_set(uint8_t bonded_port_id,
665                 struct ether_addr *mac_addr)
666 {
667         struct rte_eth_dev *bonded_eth_dev;
668         struct bond_dev_private *internals;
669
670         if (valid_bonded_port_id(bonded_port_id) != 0)
671                 return -1;
672
673         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
674         internals = bonded_eth_dev->data->dev_private;
675
676         /* Set MAC Address of Bonded Device */
677         if (mac_address_set(bonded_eth_dev, mac_addr))
678                 return -1;
679
680         internals->user_defined_mac = 1;
681
682         /* Update all slave devices MACs*/
683         if (internals->slave_count > 0)
684                 return mac_address_slaves_update(bonded_eth_dev);
685
686         return 0;
687 }
688
689 int
690 rte_eth_bond_mac_address_reset(uint8_t bonded_port_id)
691 {
692         struct rte_eth_dev *bonded_eth_dev;
693         struct bond_dev_private *internals;
694
695         if (valid_bonded_port_id(bonded_port_id) != 0)
696                 return -1;
697
698         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
699         internals = bonded_eth_dev->data->dev_private;
700
701         internals->user_defined_mac = 0;
702
703         if (internals->slave_count > 0) {
704                 /* Set MAC Address of Bonded Device */
705                 if (mac_address_set(bonded_eth_dev,
706                                 &internals->slaves[internals->primary_port].persisted_mac_addr)
707                                 != 0) {
708                         RTE_BOND_LOG(ERR, "Failed to set MAC address on bonded device");
709                         return -1;
710                 }
711                 /* Update all slave devices MAC addresses */
712                 return mac_address_slaves_update(bonded_eth_dev);
713         }
714         /* No need to update anything as no slaves present */
715         return 0;
716 }
717
718 int
719 rte_eth_bond_xmit_policy_set(uint8_t bonded_port_id, uint8_t policy)
720 {
721         struct bond_dev_private *internals;
722
723         if (valid_bonded_port_id(bonded_port_id) != 0)
724                 return -1;
725
726         internals = rte_eth_devices[bonded_port_id].data->dev_private;
727
728         switch (policy) {
729         case BALANCE_XMIT_POLICY_LAYER2:
730                 internals->balance_xmit_policy = policy;
731                 internals->xmit_hash = xmit_l2_hash;
732                 break;
733         case BALANCE_XMIT_POLICY_LAYER23:
734                 internals->balance_xmit_policy = policy;
735                 internals->xmit_hash = xmit_l23_hash;
736                 break;
737         case BALANCE_XMIT_POLICY_LAYER34:
738                 internals->balance_xmit_policy = policy;
739                 internals->xmit_hash = xmit_l34_hash;
740                 break;
741
742         default:
743                 return -1;
744         }
745         return 0;
746 }
747
748 int
749 rte_eth_bond_xmit_policy_get(uint8_t bonded_port_id)
750 {
751         struct bond_dev_private *internals;
752
753         if (valid_bonded_port_id(bonded_port_id) != 0)
754                 return -1;
755
756         internals = rte_eth_devices[bonded_port_id].data->dev_private;
757
758         return internals->balance_xmit_policy;
759 }
760
761 int
762 rte_eth_bond_link_monitoring_set(uint8_t bonded_port_id, uint32_t internal_ms)
763 {
764         struct bond_dev_private *internals;
765
766         if (valid_bonded_port_id(bonded_port_id) != 0)
767                 return -1;
768
769         internals = rte_eth_devices[bonded_port_id].data->dev_private;
770         internals->link_status_polling_interval_ms = internal_ms;
771
772         return 0;
773 }
774
775 int
776 rte_eth_bond_link_monitoring_get(uint8_t bonded_port_id)
777 {
778         struct bond_dev_private *internals;
779
780         if (valid_bonded_port_id(bonded_port_id) != 0)
781                 return -1;
782
783         internals = rte_eth_devices[bonded_port_id].data->dev_private;
784
785         return internals->link_status_polling_interval_ms;
786 }
787
788 int
789 rte_eth_bond_link_down_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
790
791 {
792         struct bond_dev_private *internals;
793
794         if (valid_bonded_port_id(bonded_port_id) != 0)
795                 return -1;
796
797         internals = rte_eth_devices[bonded_port_id].data->dev_private;
798         internals->link_down_delay_ms = delay_ms;
799
800         return 0;
801 }
802
803 int
804 rte_eth_bond_link_down_prop_delay_get(uint8_t bonded_port_id)
805 {
806         struct bond_dev_private *internals;
807
808         if (valid_bonded_port_id(bonded_port_id) != 0)
809                 return -1;
810
811         internals = rte_eth_devices[bonded_port_id].data->dev_private;
812
813         return internals->link_down_delay_ms;
814 }
815
816 int
817 rte_eth_bond_link_up_prop_delay_set(uint8_t bonded_port_id, uint32_t delay_ms)
818
819 {
820         struct bond_dev_private *internals;
821
822         if (valid_bonded_port_id(bonded_port_id) != 0)
823                 return -1;
824
825         internals = rte_eth_devices[bonded_port_id].data->dev_private;
826         internals->link_up_delay_ms = delay_ms;
827
828         return 0;
829 }
830
831 int
832 rte_eth_bond_link_up_prop_delay_get(uint8_t bonded_port_id)
833 {
834         struct bond_dev_private *internals;
835
836         if (valid_bonded_port_id(bonded_port_id) != 0)
837                 return -1;
838
839         internals = rte_eth_devices[bonded_port_id].data->dev_private;
840
841         return internals->link_up_delay_ms;
842 }