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