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