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