net: add rte prefix to ether structures
[dpdk.git] / drivers / net / bonding / rte_eth_bond_api.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4
5 #include <string.h>
6
7 #include <rte_mbuf.h>
8 #include <rte_malloc.h>
9 #include <rte_ethdev_driver.h>
10 #include <rte_tcp.h>
11 #include <rte_bus_vdev.h>
12 #include <rte_kvargs.h>
13
14 #include "rte_eth_bond.h"
15 #include "rte_eth_bond_private.h"
16 #include "rte_eth_bond_8023ad_private.h"
17
18 int
19 check_for_bonded_ethdev(const struct rte_eth_dev *eth_dev)
20 {
21         /* Check valid pointer */
22         if (eth_dev == NULL ||
23                 eth_dev->device == NULL ||
24                 eth_dev->device->driver == NULL ||
25                 eth_dev->device->driver->name == NULL)
26                 return -1;
27
28         /* return 0 if driver name matches */
29         return eth_dev->device->driver->name != pmd_bond_drv.driver.name;
30 }
31
32 int
33 valid_bonded_port_id(uint16_t port_id)
34 {
35         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
36         return check_for_bonded_ethdev(&rte_eth_devices[port_id]);
37 }
38
39 int
40 check_for_master_bonded_ethdev(const struct rte_eth_dev *eth_dev)
41 {
42         int i;
43         struct bond_dev_private *internals;
44
45         if (check_for_bonded_ethdev(eth_dev) != 0)
46                 return 0;
47
48         internals = eth_dev->data->dev_private;
49
50         /* Check if any of slave devices is a bonded device */
51         for (i = 0; i < internals->slave_count; i++)
52                 if (valid_bonded_port_id(internals->slaves[i].port_id) == 0)
53                         return 1;
54
55         return 0;
56 }
57
58 int
59 valid_slave_port_id(uint16_t port_id, uint8_t mode)
60 {
61         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
62
63         /* Verify that port_id refers to a non bonded port */
64         if (check_for_bonded_ethdev(&rte_eth_devices[port_id]) == 0 &&
65                         mode == BONDING_MODE_8023AD) {
66                 RTE_BOND_LOG(ERR, "Cannot add slave to bonded device in 802.3ad"
67                                 " mode as slave is also a bonded device, only "
68                                 "physical devices can be support in this mode.");
69                 return -1;
70         }
71
72         return 0;
73 }
74
75 void
76 activate_slave(struct rte_eth_dev *eth_dev, uint16_t port_id)
77 {
78         struct bond_dev_private *internals = eth_dev->data->dev_private;
79         uint16_t active_count = internals->active_slave_count;
80
81         if (internals->mode == BONDING_MODE_8023AD)
82                 bond_mode_8023ad_activate_slave(eth_dev, port_id);
83
84         if (internals->mode == BONDING_MODE_TLB
85                         || internals->mode == BONDING_MODE_ALB) {
86
87                 internals->tlb_slaves_order[active_count] = port_id;
88         }
89
90         RTE_ASSERT(internals->active_slave_count <
91                         (RTE_DIM(internals->active_slaves) - 1));
92
93         internals->active_slaves[internals->active_slave_count] = port_id;
94         internals->active_slave_count++;
95
96         if (internals->mode == BONDING_MODE_TLB)
97                 bond_tlb_activate_slave(internals);
98         if (internals->mode == BONDING_MODE_ALB)
99                 bond_mode_alb_client_list_upd(eth_dev);
100 }
101
102 void
103 deactivate_slave(struct rte_eth_dev *eth_dev, uint16_t port_id)
104 {
105         uint16_t slave_pos;
106         struct bond_dev_private *internals = eth_dev->data->dev_private;
107         uint16_t active_count = internals->active_slave_count;
108
109         if (internals->mode == BONDING_MODE_8023AD) {
110                 bond_mode_8023ad_stop(eth_dev);
111                 bond_mode_8023ad_deactivate_slave(eth_dev, port_id);
112         } else if (internals->mode == BONDING_MODE_TLB
113                         || internals->mode == BONDING_MODE_ALB)
114                 bond_tlb_disable(internals);
115
116         slave_pos = find_slave_by_id(internals->active_slaves, active_count,
117                         port_id);
118
119         /* If slave was not at the end of the list
120          * shift active slaves up active array list */
121         if (slave_pos < active_count) {
122                 active_count--;
123                 memmove(internals->active_slaves + slave_pos,
124                                 internals->active_slaves + slave_pos + 1,
125                                 (active_count - slave_pos) *
126                                         sizeof(internals->active_slaves[0]));
127         }
128
129         RTE_ASSERT(active_count < RTE_DIM(internals->active_slaves));
130         internals->active_slave_count = active_count;
131
132         /* Resetting active_slave when reaches to max
133          * no of slaves in active list
134          */
135         if (internals->active_slave >= active_count)
136                 internals->active_slave = 0;
137
138         if (eth_dev->data->dev_started) {
139                 if (internals->mode == BONDING_MODE_8023AD) {
140                         bond_mode_8023ad_start(eth_dev);
141                 } else if (internals->mode == BONDING_MODE_TLB) {
142                         bond_tlb_enable(internals);
143                 } else if (internals->mode == BONDING_MODE_ALB) {
144                         bond_tlb_enable(internals);
145                         bond_mode_alb_client_list_upd(eth_dev);
146                 }
147         }
148 }
149
150 int
151 rte_eth_bond_create(const char *name, uint8_t mode, uint8_t socket_id)
152 {
153         struct bond_dev_private *internals;
154         char devargs[52];
155         uint16_t port_id;
156         int ret;
157
158         if (name == NULL) {
159                 RTE_BOND_LOG(ERR, "Invalid name specified");
160                 return -EINVAL;
161         }
162
163         ret = snprintf(devargs, sizeof(devargs),
164                 "driver=net_bonding,mode=%d,socket_id=%d", mode, socket_id);
165         if (ret < 0 || ret >= (int)sizeof(devargs))
166                 return -ENOMEM;
167
168         ret = rte_vdev_init(name, devargs);
169         if (ret)
170                 return -ENOMEM;
171
172         ret = rte_eth_dev_get_port_by_name(name, &port_id);
173         RTE_ASSERT(!ret);
174
175         /*
176          * To make bond_ethdev_configure() happy we need to free the
177          * internals->kvlist here.
178          *
179          * Also see comment in bond_ethdev_configure().
180          */
181         internals = rte_eth_devices[port_id].data->dev_private;
182         rte_kvargs_free(internals->kvlist);
183         internals->kvlist = NULL;
184
185         return port_id;
186 }
187
188 int
189 rte_eth_bond_free(const char *name)
190 {
191         return rte_vdev_uninit(name);
192 }
193
194 static int
195 slave_vlan_filter_set(uint16_t bonded_port_id, uint16_t slave_port_id)
196 {
197         struct rte_eth_dev *bonded_eth_dev;
198         struct bond_dev_private *internals;
199         int found;
200         int res = 0;
201         uint64_t slab = 0;
202         uint32_t pos = 0;
203         uint16_t first;
204
205         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
206         if ((bonded_eth_dev->data->dev_conf.rxmode.offloads &
207                         DEV_RX_OFFLOAD_VLAN_FILTER) == 0)
208                 return 0;
209
210         internals = bonded_eth_dev->data->dev_private;
211         found = rte_bitmap_scan(internals->vlan_filter_bmp, &pos, &slab);
212         first = pos;
213
214         if (!found)
215                 return 0;
216
217         do {
218                 uint32_t i;
219                 uint64_t mask;
220
221                 for (i = 0, mask = 1;
222                      i < RTE_BITMAP_SLAB_BIT_SIZE;
223                      i ++, mask <<= 1) {
224                         if (unlikely(slab & mask)) {
225                                 uint16_t vlan_id = pos + i;
226
227                                 res = rte_eth_dev_vlan_filter(slave_port_id,
228                                                               vlan_id, 1);
229                         }
230                 }
231                 found = rte_bitmap_scan(internals->vlan_filter_bmp,
232                                         &pos, &slab);
233         } while (found && first != pos && res == 0);
234
235         return res;
236 }
237
238 static int
239 slave_rte_flow_prepare(uint16_t slave_id, struct bond_dev_private *internals)
240 {
241         struct rte_flow *flow;
242         struct rte_flow_error ferror;
243         uint16_t slave_port_id = internals->slaves[slave_id].port_id;
244
245         if (internals->flow_isolated_valid != 0) {
246                 rte_eth_dev_stop(slave_port_id);
247                 if (rte_flow_isolate(slave_port_id, internals->flow_isolated,
248                     &ferror)) {
249                         RTE_BOND_LOG(ERR, "rte_flow_isolate failed for slave"
250                                      " %d: %s", slave_id, ferror.message ?
251                                      ferror.message : "(no stated reason)");
252                         return -1;
253                 }
254         }
255         TAILQ_FOREACH(flow, &internals->flow_list, next) {
256                 flow->flows[slave_id] = rte_flow_create(slave_port_id,
257                                                         flow->rule.attr,
258                                                         flow->rule.pattern,
259                                                         flow->rule.actions,
260                                                         &ferror);
261                 if (flow->flows[slave_id] == NULL) {
262                         RTE_BOND_LOG(ERR, "Cannot create flow for slave"
263                                      " %d: %s", slave_id,
264                                      ferror.message ? ferror.message :
265                                      "(no stated reason)");
266                         /* Destroy successful bond flows from the slave */
267                         TAILQ_FOREACH(flow, &internals->flow_list, next) {
268                                 if (flow->flows[slave_id] != NULL) {
269                                         rte_flow_destroy(slave_port_id,
270                                                          flow->flows[slave_id],
271                                                          &ferror);
272                                         flow->flows[slave_id] = NULL;
273                                 }
274                         }
275                         return -1;
276                 }
277         }
278         return 0;
279 }
280
281 static void
282 eth_bond_slave_inherit_dev_info_rx_first(struct bond_dev_private *internals,
283                                          const struct rte_eth_dev_info *di)
284 {
285         struct rte_eth_rxconf *rxconf_i = &internals->default_rxconf;
286
287         internals->reta_size = di->reta_size;
288
289         /* Inherit Rx offload capabilities from the first slave device */
290         internals->rx_offload_capa = di->rx_offload_capa;
291         internals->rx_queue_offload_capa = di->rx_queue_offload_capa;
292         internals->flow_type_rss_offloads = di->flow_type_rss_offloads;
293
294         /* Inherit maximum Rx packet size from the first slave device */
295         internals->candidate_max_rx_pktlen = di->max_rx_pktlen;
296
297         /* Inherit default Rx queue settings from the first slave device */
298         memcpy(rxconf_i, &di->default_rxconf, sizeof(*rxconf_i));
299
300         /*
301          * Turn off descriptor prefetch and writeback by default for all
302          * slave devices. Applications may tweak this setting if need be.
303          */
304         rxconf_i->rx_thresh.pthresh = 0;
305         rxconf_i->rx_thresh.hthresh = 0;
306         rxconf_i->rx_thresh.wthresh = 0;
307
308         /* Setting this to zero should effectively enable default values */
309         rxconf_i->rx_free_thresh = 0;
310
311         /* Disable deferred start by default for all slave devices */
312         rxconf_i->rx_deferred_start = 0;
313 }
314
315 static void
316 eth_bond_slave_inherit_dev_info_tx_first(struct bond_dev_private *internals,
317                                          const struct rte_eth_dev_info *di)
318 {
319         struct rte_eth_txconf *txconf_i = &internals->default_txconf;
320
321         /* Inherit Tx offload capabilities from the first slave device */
322         internals->tx_offload_capa = di->tx_offload_capa;
323         internals->tx_queue_offload_capa = di->tx_queue_offload_capa;
324
325         /* Inherit default Tx queue settings from the first slave device */
326         memcpy(txconf_i, &di->default_txconf, sizeof(*txconf_i));
327
328         /*
329          * Turn off descriptor prefetch and writeback by default for all
330          * slave devices. Applications may tweak this setting if need be.
331          */
332         txconf_i->tx_thresh.pthresh = 0;
333         txconf_i->tx_thresh.hthresh = 0;
334         txconf_i->tx_thresh.wthresh = 0;
335
336         /*
337          * Setting these parameters to zero assumes that default
338          * values will be configured implicitly by slave devices.
339          */
340         txconf_i->tx_free_thresh = 0;
341         txconf_i->tx_rs_thresh = 0;
342
343         /* Disable deferred start by default for all slave devices */
344         txconf_i->tx_deferred_start = 0;
345 }
346
347 static void
348 eth_bond_slave_inherit_dev_info_rx_next(struct bond_dev_private *internals,
349                                         const struct rte_eth_dev_info *di)
350 {
351         struct rte_eth_rxconf *rxconf_i = &internals->default_rxconf;
352         const struct rte_eth_rxconf *rxconf = &di->default_rxconf;
353
354         internals->rx_offload_capa &= di->rx_offload_capa;
355         internals->rx_queue_offload_capa &= di->rx_queue_offload_capa;
356         internals->flow_type_rss_offloads &= di->flow_type_rss_offloads;
357
358         /*
359          * If at least one slave device suggests enabling this
360          * setting by default, enable it for all slave devices
361          * since disabling it may not be necessarily supported.
362          */
363         if (rxconf->rx_drop_en == 1)
364                 rxconf_i->rx_drop_en = 1;
365
366         /*
367          * Adding a new slave device may cause some of previously inherited
368          * offloads to be withdrawn from the internal rx_queue_offload_capa
369          * value. Thus, the new internal value of default Rx queue offloads
370          * has to be masked by rx_queue_offload_capa to make sure that only
371          * commonly supported offloads are preserved from both the previous
372          * value and the value being inhereted from the new slave device.
373          */
374         rxconf_i->offloads = (rxconf_i->offloads | rxconf->offloads) &
375                              internals->rx_queue_offload_capa;
376
377         /*
378          * RETA size is GCD of all slaves RETA sizes, so, if all sizes will be
379          * the power of 2, the lower one is GCD
380          */
381         if (internals->reta_size > di->reta_size)
382                 internals->reta_size = di->reta_size;
383
384         if (!internals->max_rx_pktlen &&
385             di->max_rx_pktlen < internals->candidate_max_rx_pktlen)
386                 internals->candidate_max_rx_pktlen = di->max_rx_pktlen;
387 }
388
389 static void
390 eth_bond_slave_inherit_dev_info_tx_next(struct bond_dev_private *internals,
391                                         const struct rte_eth_dev_info *di)
392 {
393         struct rte_eth_txconf *txconf_i = &internals->default_txconf;
394         const struct rte_eth_txconf *txconf = &di->default_txconf;
395
396         internals->tx_offload_capa &= di->tx_offload_capa;
397         internals->tx_queue_offload_capa &= di->tx_queue_offload_capa;
398
399         /*
400          * Adding a new slave device may cause some of previously inherited
401          * offloads to be withdrawn from the internal tx_queue_offload_capa
402          * value. Thus, the new internal value of default Tx queue offloads
403          * has to be masked by tx_queue_offload_capa to make sure that only
404          * commonly supported offloads are preserved from both the previous
405          * value and the value being inhereted from the new slave device.
406          */
407         txconf_i->offloads = (txconf_i->offloads | txconf->offloads) &
408                              internals->tx_queue_offload_capa;
409 }
410
411 static void
412 eth_bond_slave_inherit_desc_lim_first(struct rte_eth_desc_lim *bond_desc_lim,
413                 const struct rte_eth_desc_lim *slave_desc_lim)
414 {
415         memcpy(bond_desc_lim, slave_desc_lim, sizeof(*bond_desc_lim));
416 }
417
418 static int
419 eth_bond_slave_inherit_desc_lim_next(struct rte_eth_desc_lim *bond_desc_lim,
420                 const struct rte_eth_desc_lim *slave_desc_lim)
421 {
422         bond_desc_lim->nb_max = RTE_MIN(bond_desc_lim->nb_max,
423                                         slave_desc_lim->nb_max);
424         bond_desc_lim->nb_min = RTE_MAX(bond_desc_lim->nb_min,
425                                         slave_desc_lim->nb_min);
426         bond_desc_lim->nb_align = RTE_MAX(bond_desc_lim->nb_align,
427                                           slave_desc_lim->nb_align);
428
429         if (bond_desc_lim->nb_min > bond_desc_lim->nb_max ||
430             bond_desc_lim->nb_align > bond_desc_lim->nb_max) {
431                 RTE_BOND_LOG(ERR, "Failed to inherit descriptor limits");
432                 return -EINVAL;
433         }
434
435         /* Treat maximum number of segments equal to 0 as unspecified */
436         if (slave_desc_lim->nb_seg_max != 0 &&
437             (bond_desc_lim->nb_seg_max == 0 ||
438              slave_desc_lim->nb_seg_max < bond_desc_lim->nb_seg_max))
439                 bond_desc_lim->nb_seg_max = slave_desc_lim->nb_seg_max;
440         if (slave_desc_lim->nb_mtu_seg_max != 0 &&
441             (bond_desc_lim->nb_mtu_seg_max == 0 ||
442              slave_desc_lim->nb_mtu_seg_max < bond_desc_lim->nb_mtu_seg_max))
443                 bond_desc_lim->nb_mtu_seg_max = slave_desc_lim->nb_mtu_seg_max;
444
445         return 0;
446 }
447
448 static int
449 __eth_bond_slave_add_lock_free(uint16_t bonded_port_id, uint16_t slave_port_id)
450 {
451         struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
452         struct bond_dev_private *internals;
453         struct rte_eth_link link_props;
454         struct rte_eth_dev_info dev_info;
455
456         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
457         internals = bonded_eth_dev->data->dev_private;
458
459         if (valid_slave_port_id(slave_port_id, internals->mode) != 0)
460                 return -1;
461
462         slave_eth_dev = &rte_eth_devices[slave_port_id];
463         if (slave_eth_dev->data->dev_flags & RTE_ETH_DEV_BONDED_SLAVE) {
464                 RTE_BOND_LOG(ERR, "Slave device is already a slave of a bonded device");
465                 return -1;
466         }
467
468         rte_eth_dev_info_get(slave_port_id, &dev_info);
469         if (dev_info.max_rx_pktlen < internals->max_rx_pktlen) {
470                 RTE_BOND_LOG(ERR, "Slave (port %u) max_rx_pktlen too small",
471                              slave_port_id);
472                 return -1;
473         }
474
475         slave_add(internals, slave_eth_dev);
476
477         /* We need to store slaves reta_size to be able to synchronize RETA for all
478          * slave devices even if its sizes are different.
479          */
480         internals->slaves[internals->slave_count].reta_size = dev_info.reta_size;
481
482         if (internals->slave_count < 1) {
483                 /* if MAC is not user defined then use MAC of first slave add to
484                  * bonded device */
485                 if (!internals->user_defined_mac) {
486                         if (mac_address_set(bonded_eth_dev,
487                                             slave_eth_dev->data->mac_addrs)) {
488                                 RTE_BOND_LOG(ERR, "Failed to set MAC address");
489                                 return -1;
490                         }
491                 }
492
493                 /* Make primary slave */
494                 internals->primary_port = slave_port_id;
495                 internals->current_primary_port = slave_port_id;
496
497                 /* Inherit queues settings from first slave */
498                 internals->nb_rx_queues = slave_eth_dev->data->nb_rx_queues;
499                 internals->nb_tx_queues = slave_eth_dev->data->nb_tx_queues;
500
501                 eth_bond_slave_inherit_dev_info_rx_first(internals, &dev_info);
502                 eth_bond_slave_inherit_dev_info_tx_first(internals, &dev_info);
503
504                 eth_bond_slave_inherit_desc_lim_first(&internals->rx_desc_lim,
505                                                       &dev_info.rx_desc_lim);
506                 eth_bond_slave_inherit_desc_lim_first(&internals->tx_desc_lim,
507                                                       &dev_info.tx_desc_lim);
508         } else {
509                 int ret;
510
511                 eth_bond_slave_inherit_dev_info_rx_next(internals, &dev_info);
512                 eth_bond_slave_inherit_dev_info_tx_next(internals, &dev_info);
513
514                 ret = eth_bond_slave_inherit_desc_lim_next(
515                                 &internals->rx_desc_lim, &dev_info.rx_desc_lim);
516                 if (ret != 0)
517                         return ret;
518
519                 ret = eth_bond_slave_inherit_desc_lim_next(
520                                 &internals->tx_desc_lim, &dev_info.tx_desc_lim);
521                 if (ret != 0)
522                         return ret;
523         }
524
525         bonded_eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf &=
526                         internals->flow_type_rss_offloads;
527
528         if (slave_rte_flow_prepare(internals->slave_count, internals) != 0) {
529                 RTE_BOND_LOG(ERR, "Failed to prepare new slave flows: port=%d",
530                              slave_port_id);
531                 return -1;
532         }
533
534         /* Add additional MAC addresses to the slave */
535         if (slave_add_mac_addresses(bonded_eth_dev, slave_port_id) != 0) {
536                 RTE_BOND_LOG(ERR, "Failed to add mac address(es) to slave %hu",
537                                 slave_port_id);
538                 return -1;
539         }
540
541         internals->slave_count++;
542
543         if (bonded_eth_dev->data->dev_started) {
544                 if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
545                         internals->slave_count--;
546                         RTE_BOND_LOG(ERR, "rte_bond_slaves_configure: port=%d",
547                                         slave_port_id);
548                         return -1;
549                 }
550         }
551
552         /* Add slave details to bonded device */
553         slave_eth_dev->data->dev_flags |= RTE_ETH_DEV_BONDED_SLAVE;
554
555         /* Update all slave devices MACs */
556         mac_address_slaves_update(bonded_eth_dev);
557
558         /* Register link status change callback with bonded device pointer as
559          * argument*/
560         rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
561                         bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
562
563         /* If bonded device is started then we can add the slave to our active
564          * slave array */
565         if (bonded_eth_dev->data->dev_started) {
566                 rte_eth_link_get_nowait(slave_port_id, &link_props);
567
568                  if (link_props.link_status == ETH_LINK_UP) {
569                         if (internals->active_slave_count == 0 &&
570                             !internals->user_defined_primary_port)
571                                 bond_ethdev_primary_set(internals,
572                                                         slave_port_id);
573                 }
574         }
575
576         slave_vlan_filter_set(bonded_port_id, slave_port_id);
577
578         return 0;
579
580 }
581
582 int
583 rte_eth_bond_slave_add(uint16_t bonded_port_id, uint16_t slave_port_id)
584 {
585         struct rte_eth_dev *bonded_eth_dev;
586         struct bond_dev_private *internals;
587
588         int retval;
589
590         /* Verify that port id's are valid bonded and slave ports */
591         if (valid_bonded_port_id(bonded_port_id) != 0)
592                 return -1;
593
594         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
595         internals = bonded_eth_dev->data->dev_private;
596
597         rte_spinlock_lock(&internals->lock);
598
599         retval = __eth_bond_slave_add_lock_free(bonded_port_id, slave_port_id);
600
601         rte_spinlock_unlock(&internals->lock);
602
603         return retval;
604 }
605
606 static int
607 __eth_bond_slave_remove_lock_free(uint16_t bonded_port_id,
608                                    uint16_t slave_port_id)
609 {
610         struct rte_eth_dev *bonded_eth_dev;
611         struct bond_dev_private *internals;
612         struct rte_eth_dev *slave_eth_dev;
613         struct rte_flow_error flow_error;
614         struct rte_flow *flow;
615         int i, slave_idx;
616
617         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
618         internals = bonded_eth_dev->data->dev_private;
619
620         if (valid_slave_port_id(slave_port_id, internals->mode) < 0)
621                 return -1;
622
623         /* first remove from active slave list */
624         slave_idx = find_slave_by_id(internals->active_slaves,
625                 internals->active_slave_count, slave_port_id);
626
627         if (slave_idx < internals->active_slave_count)
628                 deactivate_slave(bonded_eth_dev, slave_port_id);
629
630         slave_idx = -1;
631         /* now find in slave list */
632         for (i = 0; i < internals->slave_count; i++)
633                 if (internals->slaves[i].port_id == slave_port_id) {
634                         slave_idx = i;
635                         break;
636                 }
637
638         if (slave_idx < 0) {
639                 RTE_BOND_LOG(ERR, "Couldn't find slave in port list, slave count %d",
640                                 internals->slave_count);
641                 return -1;
642         }
643
644         /* Un-register link status change callback with bonded device pointer as
645          * argument*/
646         rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
647                         bond_ethdev_lsc_event_callback,
648                         &rte_eth_devices[bonded_port_id].data->port_id);
649
650         /* Restore original MAC address of slave device */
651         rte_eth_dev_default_mac_addr_set(slave_port_id,
652                         &(internals->slaves[slave_idx].persisted_mac_addr));
653
654         /* remove additional MAC addresses from the slave */
655         slave_remove_mac_addresses(bonded_eth_dev, slave_port_id);
656
657         /*
658          * Remove bond device flows from slave device.
659          * Note: don't restore flow isolate mode.
660          */
661         TAILQ_FOREACH(flow, &internals->flow_list, next) {
662                 if (flow->flows[slave_idx] != NULL) {
663                         rte_flow_destroy(slave_port_id, flow->flows[slave_idx],
664                                          &flow_error);
665                         flow->flows[slave_idx] = NULL;
666                 }
667         }
668
669         slave_eth_dev = &rte_eth_devices[slave_port_id];
670         slave_remove(internals, slave_eth_dev);
671         slave_eth_dev->data->dev_flags &= (~RTE_ETH_DEV_BONDED_SLAVE);
672
673         /*  first slave in the active list will be the primary by default,
674          *  otherwise use first device in list */
675         if (internals->current_primary_port == slave_port_id) {
676                 if (internals->active_slave_count > 0)
677                         internals->current_primary_port = internals->active_slaves[0];
678                 else if (internals->slave_count > 0)
679                         internals->current_primary_port = internals->slaves[0].port_id;
680                 else
681                         internals->primary_port = 0;
682         }
683
684         if (internals->active_slave_count < 1) {
685                 /* if no slaves are any longer attached to bonded device and MAC is not
686                  * user defined then clear MAC of bonded device as it will be reset
687                  * when a new slave is added */
688                 if (internals->slave_count < 1 && !internals->user_defined_mac)
689                         memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
690                                         sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
691         }
692         if (internals->slave_count == 0) {
693                 internals->rx_offload_capa = 0;
694                 internals->tx_offload_capa = 0;
695                 internals->rx_queue_offload_capa = 0;
696                 internals->tx_queue_offload_capa = 0;
697                 internals->flow_type_rss_offloads = ETH_RSS_PROTO_MASK;
698                 internals->reta_size = 0;
699                 internals->candidate_max_rx_pktlen = 0;
700                 internals->max_rx_pktlen = 0;
701         }
702         return 0;
703 }
704
705 int
706 rte_eth_bond_slave_remove(uint16_t bonded_port_id, uint16_t slave_port_id)
707 {
708         struct rte_eth_dev *bonded_eth_dev;
709         struct bond_dev_private *internals;
710         int retval;
711
712         if (valid_bonded_port_id(bonded_port_id) != 0)
713                 return -1;
714
715         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
716         internals = bonded_eth_dev->data->dev_private;
717
718         rte_spinlock_lock(&internals->lock);
719
720         retval = __eth_bond_slave_remove_lock_free(bonded_port_id, slave_port_id);
721
722         rte_spinlock_unlock(&internals->lock);
723
724         return retval;
725 }
726
727 int
728 rte_eth_bond_mode_set(uint16_t bonded_port_id, uint8_t mode)
729 {
730         struct rte_eth_dev *bonded_eth_dev;
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
737         if (check_for_master_bonded_ethdev(bonded_eth_dev) != 0 &&
738                         mode == BONDING_MODE_8023AD)
739                 return -1;
740
741         return bond_ethdev_mode_set(bonded_eth_dev, mode);
742 }
743
744 int
745 rte_eth_bond_mode_get(uint16_t bonded_port_id)
746 {
747         struct bond_dev_private *internals;
748
749         if (valid_bonded_port_id(bonded_port_id) != 0)
750                 return -1;
751
752         internals = rte_eth_devices[bonded_port_id].data->dev_private;
753
754         return internals->mode;
755 }
756
757 int
758 rte_eth_bond_primary_set(uint16_t bonded_port_id, uint16_t slave_port_id)
759 {
760         struct bond_dev_private *internals;
761
762         if (valid_bonded_port_id(bonded_port_id) != 0)
763                 return -1;
764
765         internals = rte_eth_devices[bonded_port_id].data->dev_private;
766
767         if (valid_slave_port_id(slave_port_id, internals->mode) != 0)
768                 return -1;
769
770         internals->user_defined_primary_port = 1;
771         internals->primary_port = slave_port_id;
772
773         bond_ethdev_primary_set(internals, slave_port_id);
774
775         return 0;
776 }
777
778 int
779 rte_eth_bond_primary_get(uint16_t bonded_port_id)
780 {
781         struct bond_dev_private *internals;
782
783         if (valid_bonded_port_id(bonded_port_id) != 0)
784                 return -1;
785
786         internals = rte_eth_devices[bonded_port_id].data->dev_private;
787
788         if (internals->slave_count < 1)
789                 return -1;
790
791         return internals->current_primary_port;
792 }
793
794 int
795 rte_eth_bond_slaves_get(uint16_t bonded_port_id, uint16_t slaves[],
796                         uint16_t len)
797 {
798         struct bond_dev_private *internals;
799         uint16_t i;
800
801         if (valid_bonded_port_id(bonded_port_id) != 0)
802                 return -1;
803
804         if (slaves == NULL)
805                 return -1;
806
807         internals = rte_eth_devices[bonded_port_id].data->dev_private;
808
809         if (internals->slave_count > len)
810                 return -1;
811
812         for (i = 0; i < internals->slave_count; i++)
813                 slaves[i] = internals->slaves[i].port_id;
814
815         return internals->slave_count;
816 }
817
818 int
819 rte_eth_bond_active_slaves_get(uint16_t bonded_port_id, uint16_t slaves[],
820                 uint16_t len)
821 {
822         struct bond_dev_private *internals;
823
824         if (valid_bonded_port_id(bonded_port_id) != 0)
825                 return -1;
826
827         if (slaves == NULL)
828                 return -1;
829
830         internals = rte_eth_devices[bonded_port_id].data->dev_private;
831
832         if (internals->active_slave_count > len)
833                 return -1;
834
835         memcpy(slaves, internals->active_slaves,
836         internals->active_slave_count * sizeof(internals->active_slaves[0]));
837
838         return internals->active_slave_count;
839 }
840
841 int
842 rte_eth_bond_mac_address_set(uint16_t bonded_port_id,
843                 struct rte_ether_addr *mac_addr)
844 {
845         struct rte_eth_dev *bonded_eth_dev;
846         struct bond_dev_private *internals;
847
848         if (valid_bonded_port_id(bonded_port_id) != 0)
849                 return -1;
850
851         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
852         internals = bonded_eth_dev->data->dev_private;
853
854         /* Set MAC Address of Bonded Device */
855         if (mac_address_set(bonded_eth_dev, mac_addr))
856                 return -1;
857
858         internals->user_defined_mac = 1;
859
860         /* Update all slave devices MACs*/
861         if (internals->slave_count > 0)
862                 return mac_address_slaves_update(bonded_eth_dev);
863
864         return 0;
865 }
866
867 int
868 rte_eth_bond_mac_address_reset(uint16_t bonded_port_id)
869 {
870         struct rte_eth_dev *bonded_eth_dev;
871         struct bond_dev_private *internals;
872
873         if (valid_bonded_port_id(bonded_port_id) != 0)
874                 return -1;
875
876         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
877         internals = bonded_eth_dev->data->dev_private;
878
879         internals->user_defined_mac = 0;
880
881         if (internals->slave_count > 0) {
882                 int slave_port;
883                 /* Get the primary slave location based on the primary port
884                  * number as, while slave_add(), we will keep the primary
885                  * slave based on slave_count,but not based on the primary port.
886                  */
887                 for (slave_port = 0; slave_port < internals->slave_count;
888                      slave_port++) {
889                         if (internals->slaves[slave_port].port_id ==
890                             internals->primary_port)
891                                 break;
892                 }
893
894                 /* Set MAC Address of Bonded Device */
895                 if (mac_address_set(bonded_eth_dev,
896                         &internals->slaves[slave_port].persisted_mac_addr)
897                                 != 0) {
898                         RTE_BOND_LOG(ERR, "Failed to set MAC address on bonded device");
899                         return -1;
900                 }
901                 /* Update all slave devices MAC addresses */
902                 return mac_address_slaves_update(bonded_eth_dev);
903         }
904         /* No need to update anything as no slaves present */
905         return 0;
906 }
907
908 int
909 rte_eth_bond_xmit_policy_set(uint16_t bonded_port_id, uint8_t policy)
910 {
911         struct bond_dev_private *internals;
912
913         if (valid_bonded_port_id(bonded_port_id) != 0)
914                 return -1;
915
916         internals = rte_eth_devices[bonded_port_id].data->dev_private;
917
918         switch (policy) {
919         case BALANCE_XMIT_POLICY_LAYER2:
920                 internals->balance_xmit_policy = policy;
921                 internals->burst_xmit_hash = burst_xmit_l2_hash;
922                 break;
923         case BALANCE_XMIT_POLICY_LAYER23:
924                 internals->balance_xmit_policy = policy;
925                 internals->burst_xmit_hash = burst_xmit_l23_hash;
926                 break;
927         case BALANCE_XMIT_POLICY_LAYER34:
928                 internals->balance_xmit_policy = policy;
929                 internals->burst_xmit_hash = burst_xmit_l34_hash;
930                 break;
931
932         default:
933                 return -1;
934         }
935         return 0;
936 }
937
938 int
939 rte_eth_bond_xmit_policy_get(uint16_t bonded_port_id)
940 {
941         struct bond_dev_private *internals;
942
943         if (valid_bonded_port_id(bonded_port_id) != 0)
944                 return -1;
945
946         internals = rte_eth_devices[bonded_port_id].data->dev_private;
947
948         return internals->balance_xmit_policy;
949 }
950
951 int
952 rte_eth_bond_link_monitoring_set(uint16_t bonded_port_id, uint32_t internal_ms)
953 {
954         struct bond_dev_private *internals;
955
956         if (valid_bonded_port_id(bonded_port_id) != 0)
957                 return -1;
958
959         internals = rte_eth_devices[bonded_port_id].data->dev_private;
960         internals->link_status_polling_interval_ms = internal_ms;
961
962         return 0;
963 }
964
965 int
966 rte_eth_bond_link_monitoring_get(uint16_t bonded_port_id)
967 {
968         struct bond_dev_private *internals;
969
970         if (valid_bonded_port_id(bonded_port_id) != 0)
971                 return -1;
972
973         internals = rte_eth_devices[bonded_port_id].data->dev_private;
974
975         return internals->link_status_polling_interval_ms;
976 }
977
978 int
979 rte_eth_bond_link_down_prop_delay_set(uint16_t bonded_port_id,
980                                        uint32_t delay_ms)
981
982 {
983         struct bond_dev_private *internals;
984
985         if (valid_bonded_port_id(bonded_port_id) != 0)
986                 return -1;
987
988         internals = rte_eth_devices[bonded_port_id].data->dev_private;
989         internals->link_down_delay_ms = delay_ms;
990
991         return 0;
992 }
993
994 int
995 rte_eth_bond_link_down_prop_delay_get(uint16_t bonded_port_id)
996 {
997         struct bond_dev_private *internals;
998
999         if (valid_bonded_port_id(bonded_port_id) != 0)
1000                 return -1;
1001
1002         internals = rte_eth_devices[bonded_port_id].data->dev_private;
1003
1004         return internals->link_down_delay_ms;
1005 }
1006
1007 int
1008 rte_eth_bond_link_up_prop_delay_set(uint16_t bonded_port_id, uint32_t delay_ms)
1009
1010 {
1011         struct bond_dev_private *internals;
1012
1013         if (valid_bonded_port_id(bonded_port_id) != 0)
1014                 return -1;
1015
1016         internals = rte_eth_devices[bonded_port_id].data->dev_private;
1017         internals->link_up_delay_ms = delay_ms;
1018
1019         return 0;
1020 }
1021
1022 int
1023 rte_eth_bond_link_up_prop_delay_get(uint16_t bonded_port_id)
1024 {
1025         struct bond_dev_private *internals;
1026
1027         if (valid_bonded_port_id(bonded_port_id) != 0)
1028                 return -1;
1029
1030         internals = rte_eth_devices[bonded_port_id].data->dev_private;
1031
1032         return internals->link_up_delay_ms;
1033 }