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