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