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