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