blob: ec779e263a853a9c6f78ce14acdb9bd91870502c [file] [log] [blame]
Steven9cd2d7a2017-12-20 12:43:01 -08001/*
2 *------------------------------------------------------------------
3 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
16 */
17
18#include <stdint.h>
19#include <vlib/vlib.h>
20#include <vlib/unix/unix.h>
21#include <vnet/ethernet/ethernet.h>
22#include <vnet/bonding/node.h>
23
24void
25bond_disable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
26{
Steven9f781d82018-06-05 11:09:32 -070027 bond_main_t *bm = &bond_main;
Steven9cd2d7a2017-12-20 12:43:01 -080028 bond_if_t *bif;
29 int i;
30 uword p;
Zhiyong Yang6865d3c2019-05-15 04:25:20 -040031 vnet_main_t *vnm = vnet_get_main ();
32 vnet_hw_interface_t *hw;
Steven9f781d82018-06-05 11:09:32 -070033 u8 switching_active = 0;
Steven9cd2d7a2017-12-20 12:43:01 -080034
35 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
Stevena005e7f2018-03-22 17:46:58 -070036 clib_spinlock_lock_if_init (&bif->lockp);
Steven9cd2d7a2017-12-20 12:43:01 -080037 vec_foreach_index (i, bif->active_slaves)
38 {
39 p = *vec_elt_at_index (bif->active_slaves, i);
40 if (p == sif->sw_if_index)
41 {
Zhiyong Yang6865d3c2019-05-15 04:25:20 -040042 if (sif->sw_if_index == bif->sw_if_index_working)
43 {
44 switching_active = 1;
45 if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
46 bif->is_local_numa = 0;
47 }
Steven9cd2d7a2017-12-20 12:43:01 -080048 vec_del1 (bif->active_slaves, i);
49 hash_unset (bif->active_slave_by_sw_if_index, sif->sw_if_index);
Zhiyong Yang751e3f32019-06-26 05:49:14 -040050 if (sif->lacp_enabled && bif->numa_only)
51 {
52 /* For lacp mode, if we check it is a slave on local numa node,
53 bif->n_numa_slaves should be decreased by 1 becasue the first
54 bif->n_numa_slaves are all slaves on local numa node */
55 if (i < bif->n_numa_slaves)
56 {
57 bif->n_numa_slaves--;
58 ASSERT (bif->n_numa_slaves >= 0);
59 }
60 }
Steven9cd2d7a2017-12-20 12:43:01 -080061 break;
62 }
63 }
Zhiyong Yang6865d3c2019-05-15 04:25:20 -040064
65 /* We get a new slave just becoming active */
66 if ((bif->mode == BOND_MODE_ACTIVE_BACKUP) && switching_active)
67 {
68 if ((vec_len (bif->active_slaves) >= 1))
69 {
70 /* scan all slaves and try to find the first slave with local numa node. */
71 vec_foreach_index (i, bif->active_slaves)
72 {
73 p = *vec_elt_at_index (bif->active_slaves, i);
74 hw = vnet_get_sup_hw_interface (vnm, p);
75 if (vm->numa_node == hw->numa_node)
76 {
77 bif->sw_if_index_working = p;
78 bif->is_local_numa = 1;
79 vlib_process_signal_event (bm->vlib_main,
80 bond_process_node.index,
81 BOND_SEND_GARP_NA,
82 bif->hw_if_index);
83 break;
84 }
85 }
86 }
87
88 /* No local numa node is found in the active slave set. Use the first slave */
89 if ((bif->is_local_numa == 0) && (vec_len (bif->active_slaves) >= 1))
90 {
91 p = *vec_elt_at_index (bif->active_slaves, 0);
92 bif->sw_if_index_working = p;
93 vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
94 BOND_SEND_GARP_NA, bif->hw_if_index);
95 }
96 }
Stevena005e7f2018-03-22 17:46:58 -070097 clib_spinlock_unlock_if_init (&bif->lockp);
Zhiyong Yang6865d3c2019-05-15 04:25:20 -040098
99 return;
Steven9cd2d7a2017-12-20 12:43:01 -0800100}
101
102void
103bond_enable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
104{
105 bond_if_t *bif;
Steven9f781d82018-06-05 11:09:32 -0700106 bond_main_t *bm = &bond_main;
Zhiyong Yang6865d3c2019-05-15 04:25:20 -0400107 vnet_main_t *vnm = vnet_get_main ();
108 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
109 int i;
110 uword p;
Steven9cd2d7a2017-12-20 12:43:01 -0800111
112 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
Stevena005e7f2018-03-22 17:46:58 -0700113 clib_spinlock_lock_if_init (&bif->lockp);
Steven9cd2d7a2017-12-20 12:43:01 -0800114 if (!hash_get (bif->active_slave_by_sw_if_index, sif->sw_if_index))
115 {
116 hash_set (bif->active_slave_by_sw_if_index, sif->sw_if_index,
117 sif->sw_if_index);
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400118
119 if ((sif->lacp_enabled && bif->numa_only)
120 && (vm->numa_node == hw->numa_node))
121 {
122 vec_insert_elts (bif->active_slaves, &sif->sw_if_index, 1,
123 bif->n_numa_slaves);
124 bif->n_numa_slaves++;
125 }
126 else
127 {
128 vec_add1 (bif->active_slaves, sif->sw_if_index);
129 }
Steven9f781d82018-06-05 11:09:32 -0700130
131 /* First slave becomes active? */
132 if ((vec_len (bif->active_slaves) == 1) &&
133 (bif->mode == BOND_MODE_ACTIVE_BACKUP))
Zhiyong Yang6865d3c2019-05-15 04:25:20 -0400134 {
135 bif->sw_if_index_working = sif->sw_if_index;
136 bif->is_local_numa = (vm->numa_node == hw->numa_node) ? 1 : 0;
137 vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
138 BOND_SEND_GARP_NA, bif->hw_if_index);
139 }
140 else if ((vec_len (bif->active_slaves) > 1)
141 && (bif->mode == BOND_MODE_ACTIVE_BACKUP)
142 && bif->is_local_numa == 0)
143 {
144 if (vm->numa_node == hw->numa_node)
145 {
146 vec_foreach_index (i, bif->active_slaves)
147 {
148 p = *vec_elt_at_index (bif->active_slaves, 0);
149 if (p == sif->sw_if_index)
150 break;
151
152 vec_del1 (bif->active_slaves, 0);
153 hash_unset (bif->active_slave_by_sw_if_index, p);
154 vec_add1 (bif->active_slaves, p);
155 hash_set (bif->active_slave_by_sw_if_index, p, p);
156 }
157 bif->sw_if_index_working = sif->sw_if_index;
158 bif->is_local_numa = 1;
159 vlib_process_signal_event (bm->vlib_main,
160 bond_process_node.index,
161 BOND_SEND_GARP_NA, bif->hw_if_index);
162
163 }
164 }
Steven9cd2d7a2017-12-20 12:43:01 -0800165 }
Stevena005e7f2018-03-22 17:46:58 -0700166 clib_spinlock_unlock_if_init (&bif->lockp);
Zhiyong Yang6865d3c2019-05-15 04:25:20 -0400167
168 return;
Steven9cd2d7a2017-12-20 12:43:01 -0800169}
170
171int
172bond_dump_ifs (bond_interface_details_t ** out_bondifs)
173{
174 vnet_main_t *vnm = vnet_get_main ();
175 bond_main_t *bm = &bond_main;
176 bond_if_t *bif;
177 vnet_hw_interface_t *hi;
178 bond_interface_details_t *r_bondifs = NULL;
179 bond_interface_details_t *bondif = NULL;
180
181 /* *INDENT-OFF* */
182 pool_foreach (bif, bm->interfaces,
183 vec_add2(r_bondifs, bondif, 1);
Dave Barachb7b92992018-10-17 10:38:51 -0400184 clib_memset (bondif, 0, sizeof (*bondif));
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500185 bondif->id = bif->id;
Steven9cd2d7a2017-12-20 12:43:01 -0800186 bondif->sw_if_index = bif->sw_if_index;
187 hi = vnet_get_hw_interface (vnm, bif->hw_if_index);
188 clib_memcpy(bondif->interface_name, hi->name,
189 MIN (ARRAY_LEN (bondif->interface_name) - 1,
190 strlen ((const char *) hi->name)));
191 bondif->mode = bif->mode;
192 bondif->lb = bif->lb;
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400193 bondif->numa_only = bif->numa_only;
Steven9cd2d7a2017-12-20 12:43:01 -0800194 bondif->active_slaves = vec_len (bif->active_slaves);
195 bondif->slaves = vec_len (bif->slaves);
196 );
197 /* *INDENT-ON* */
198
199 *out_bondifs = r_bondifs;
200
201 return 0;
202}
203
204int
205bond_dump_slave_ifs (slave_interface_details_t ** out_slaveifs,
206 u32 bond_sw_if_index)
207{
208 vnet_main_t *vnm = vnet_get_main ();
209 bond_if_t *bif;
210 vnet_hw_interface_t *hi;
211 vnet_sw_interface_t *sw;
212 slave_interface_details_t *r_slaveifs = NULL;
213 slave_interface_details_t *slaveif = NULL;
214 u32 *sw_if_index = NULL;
215 slave_if_t *sif;
216
217 bif = bond_get_master_by_sw_if_index (bond_sw_if_index);
218 if (!bif)
219 return 1;
220
221 vec_foreach (sw_if_index, bif->slaves)
222 {
223 vec_add2 (r_slaveifs, slaveif, 1);
Dave Barachb7b92992018-10-17 10:38:51 -0400224 clib_memset (slaveif, 0, sizeof (*slaveif));
Steven9cd2d7a2017-12-20 12:43:01 -0800225 sif = bond_get_slave_by_sw_if_index (*sw_if_index);
226 if (sif)
227 {
228 sw = vnet_get_sw_interface (vnm, sif->sw_if_index);
229 hi = vnet_get_hw_interface (vnm, sw->hw_if_index);
230 clib_memcpy (slaveif->interface_name, hi->name,
231 MIN (ARRAY_LEN (slaveif->interface_name) - 1,
232 strlen ((const char *) hi->name)));
233 slaveif->sw_if_index = sif->sw_if_index;
234 slaveif->is_passive = sif->is_passive;
235 slaveif->is_long_timeout = sif->is_long_timeout;
236 }
237 }
238 *out_slaveifs = r_slaveifs;
239
240 return 0;
241}
242
243static void
244bond_delete_neighbor (vlib_main_t * vm, bond_if_t * bif, slave_if_t * sif)
245{
246 bond_main_t *bm = &bond_main;
247 vnet_main_t *vnm = vnet_get_main ();
248 int i;
Stevence2db6a2018-04-23 17:06:24 -0700249 vnet_hw_interface_t *sif_hw;
Steven4f8863b2018-04-12 19:36:19 -0700250
251 sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800252
253 bif->port_number_bitmap =
254 clib_bitmap_set (bif->port_number_bitmap,
255 ntohs (sif->actor_admin.port_number) - 1, 0);
Steven0d883012018-05-11 11:06:23 -0700256 bm->slave_by_sw_if_index[sif->sw_if_index] = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800257 vec_free (sif->last_marker_pkt);
258 vec_free (sif->last_rx_pkt);
259 vec_foreach_index (i, bif->slaves)
260 {
261 uword p = *vec_elt_at_index (bif->slaves, i);
262 if (p == sif->sw_if_index)
263 {
264 vec_del1 (bif->slaves, i);
265 break;
266 }
267 }
268
269 bond_disable_collecting_distributing (vm, sif);
270
Steven5967baf2018-09-24 21:09:36 -0700271 vnet_feature_enable_disable ("device-input", "bond-input",
272 sif_hw->hw_if_index, 0, 0, 0);
273
Steven9cd2d7a2017-12-20 12:43:01 -0800274 /* Put back the old mac */
Steven4f8863b2018-04-12 19:36:19 -0700275 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
Steven9cd2d7a2017-12-20 12:43:01 -0800276 sif->persistent_hw_address);
277
Steven9cd2d7a2017-12-20 12:43:01 -0800278 if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
279 (*bm->lacp_enable_disable) (vm, bif, sif, 0);
Steven5967baf2018-09-24 21:09:36 -0700280
281 pool_put (bm->neighbors, sif);
Steven9cd2d7a2017-12-20 12:43:01 -0800282}
283
284int
285bond_delete_if (vlib_main_t * vm, u32 sw_if_index)
286{
287 bond_main_t *bm = &bond_main;
288 vnet_main_t *vnm = vnet_get_main ();
289 bond_if_t *bif;
290 slave_if_t *sif;
291 vnet_hw_interface_t *hw;
292 u32 *sif_sw_if_index;
Steven70488ab2018-03-28 17:59:00 -0700293 u32 **s_list = 0;
294 u32 i;
Steven9cd2d7a2017-12-20 12:43:01 -0800295
296 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
297 if (hw == NULL || bond_dev_class.index != hw->dev_class_index)
298 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
299
300 bif = bond_get_master_by_dev_instance (hw->dev_instance);
301
302 vec_foreach (sif_sw_if_index, bif->slaves)
303 {
Steven70488ab2018-03-28 17:59:00 -0700304 vec_add1 (s_list, sif_sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800305 }
306
Steven70488ab2018-03-28 17:59:00 -0700307 for (i = 0; i < vec_len (s_list); i++)
308 {
309 sif_sw_if_index = s_list[i];
310 sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index);
311 if (sif)
312 bond_delete_neighbor (vm, bif, sif);
313 }
314
315 if (s_list)
316 vec_free (s_list);
317
Steven9cd2d7a2017-12-20 12:43:01 -0800318 /* bring down the interface */
319 vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0);
320 vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0);
321
322 ethernet_delete_interface (vnm, bif->hw_if_index);
323
324 clib_bitmap_free (bif->port_number_bitmap);
325 hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index);
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500326 hash_unset (bm->id_used, bif->id);
Dave Barachb7b92992018-10-17 10:38:51 -0400327 clib_memset (bif, 0, sizeof (*bif));
Steven9cd2d7a2017-12-20 12:43:01 -0800328 pool_put (bm->interfaces, bif);
329
330 return 0;
331}
332
333void
334bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args)
335{
336 bond_main_t *bm = &bond_main;
337 vnet_main_t *vnm = vnet_get_main ();
338 vnet_sw_interface_t *sw;
339 bond_if_t *bif;
340
341 if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0)
342 {
343 args->rv = VNET_API_ERROR_FEATURE_DISABLED;
344 args->error = clib_error_return (0, "LACP plugin is not loaded");
345 return;
346 }
347 if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN)
348 {
349 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
350 args->error = clib_error_return (0, "Invalid mode");
351 return;
352 }
353 if (args->lb > BOND_LB_L23)
354 {
355 args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
356 args->error = clib_error_return (0, "Invalid load-balance");
357 return;
358 }
359 pool_get (bm->interfaces, bif);
Dave Barachb7b92992018-10-17 10:38:51 -0400360 clib_memset (bif, 0, sizeof (*bif));
Steven9cd2d7a2017-12-20 12:43:01 -0800361 bif->dev_instance = bif - bm->interfaces;
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500362 bif->id = args->id;
Steven9cd2d7a2017-12-20 12:43:01 -0800363 bif->lb = args->lb;
364 bif->mode = args->mode;
365
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500366 // Adjust requested interface id
367 if (bif->id == ~0)
368 bif->id = bif->dev_instance;
369 if (hash_get (bm->id_used, bif->id))
370 {
371 args->rv = VNET_API_ERROR_INSTANCE_IN_USE;
372 pool_put (bm->interfaces, bif);
373 return;
374 }
375 hash_set (bm->id_used, bif->id, 1);
376
Steven9cd2d7a2017-12-20 12:43:01 -0800377 // Special load-balance mode used for rr and bc
378 if (bif->mode == BOND_MODE_ROUND_ROBIN)
379 bif->lb = BOND_LB_RR;
380 else if (bif->mode == BOND_MODE_BROADCAST)
381 bif->lb = BOND_LB_BC;
Steven318de5d2018-10-06 22:30:50 -0700382 else if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
383 bif->lb = BOND_LB_AB;
Steven9cd2d7a2017-12-20 12:43:01 -0800384
385 bif->use_custom_mac = args->hw_addr_set;
386 if (!args->hw_addr_set)
387 {
388 f64 now = vlib_time_now (vm);
389 u32 rnd;
390 rnd = (u32) (now * 1e6);
391 rnd = random_u32 (&rnd);
392
393 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
394 args->hw_addr[0] = 2;
395 args->hw_addr[1] = 0xfe;
396 }
397 memcpy (bif->hw_address, args->hw_addr, 6);
398 args->error = ethernet_register_interface
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500399 (vnm, bond_dev_class.index, bif->dev_instance /* device instance */ ,
Steven9cd2d7a2017-12-20 12:43:01 -0800400 bif->hw_address /* ethernet address */ ,
401 &bif->hw_if_index, 0 /* flag change */ );
402
403 if (args->error)
404 {
405 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500406 hash_unset (bm->id_used, bif->id);
Steven9cd2d7a2017-12-20 12:43:01 -0800407 pool_put (bm->interfaces, bif);
408 return;
409 }
410
411 sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
412 bif->sw_if_index = sw->sw_if_index;
413 bif->group = bif->sw_if_index;
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400414 bif->numa_only = args->numa_only;
Stevena005e7f2018-03-22 17:46:58 -0700415 if (vlib_get_thread_main ()->n_vlib_mains > 1)
416 clib_spinlock_init (&bif->lockp);
Steven9cd2d7a2017-12-20 12:43:01 -0800417
418 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
419 VNET_HW_INTERFACE_FLAG_LINK_UP);
420
421 hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
422
423 // for return
424 args->sw_if_index = bif->sw_if_index;
425}
426
427static clib_error_t *
428bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
429 vlib_cli_command_t * cmd)
430{
431 unformat_input_t _line_input, *line_input = &_line_input;
432 bond_create_if_args_t args = { 0 };
433 u8 mode_is_set = 0;
434
435 /* Get a line of input. */
436 if (!unformat_user (input, unformat_line_input, line_input))
437 return clib_error_return (0, "Missing required arguments.");
438
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500439 args.id = ~0;
Steven9cd2d7a2017-12-20 12:43:01 -0800440 args.mode = -1;
441 args.lb = BOND_LB_L2;
442 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
443 {
444 if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
445 mode_is_set = 1;
446 else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
447 && unformat (line_input, "load-balance %U",
448 unformat_bond_load_balance, &args.lb))
449 ;
450 else if (unformat (line_input, "hw-addr %U",
451 unformat_ethernet_address, args.hw_addr))
452 args.hw_addr_set = 1;
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500453 else if (unformat (line_input, "id %u", &args.id))
454 ;
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400455 else if (unformat (line_input, "numa-only"))
456 {
457 if (args.mode == BOND_MODE_LACP)
458 args.numa_only = 1;
459 else
460 return clib_error_return (0,
461 "Only lacp mode supports numa-only so far!");
462 }
Steven9cd2d7a2017-12-20 12:43:01 -0800463 else
464 return clib_error_return (0, "unknown input `%U'",
465 format_unformat_error, input);
466 }
467 unformat_free (line_input);
468
469 if (mode_is_set == 0)
470 return clib_error_return (0, "Missing bond mode");
471
472 bond_create_if (vm, &args);
473
474 return args.error;
475}
476
477/* *INDENT-OFF* */
478VLIB_CLI_COMMAND (bond_create_command, static) = {
479 .path = "create bond",
480 .short_help = "create bond mode {round-robin | active-backup | broadcast | "
Zhiyong Yanga58fec12019-07-21 21:51:21 -0400481 "{lacp | xor} [load-balance { l2 | l23 | l34 } [numa-only]]} [hw-addr <mac-address>] "
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500482 "[id <if-id>]",
Steven9cd2d7a2017-12-20 12:43:01 -0800483 .function = bond_create_command_fn,
484};
485/* *INDENT-ON* */
486
487static clib_error_t *
488bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
489 vlib_cli_command_t * cmd)
490{
491 unformat_input_t _line_input, *line_input = &_line_input;
492 u32 sw_if_index = ~0;
493 vnet_main_t *vnm = vnet_get_main ();
494 int rv;
495
496 /* Get a line of input. */
497 if (!unformat_user (input, unformat_line_input, line_input))
498 return clib_error_return (0, "Missing <interface>");
499
500 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
501 {
502 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
503 ;
504 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
505 vnm, &sw_if_index))
506 ;
507 else
508 return clib_error_return (0, "unknown input `%U'",
509 format_unformat_error, input);
510 }
511 unformat_free (line_input);
512
513 if (sw_if_index == ~0)
514 return clib_error_return (0,
515 "please specify interface name or sw_if_index");
516
517 rv = bond_delete_if (vm, sw_if_index);
518 if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
519 return clib_error_return (0, "not a bond interface");
520 else if (rv != 0)
521 return clib_error_return (0, "error on deleting bond interface");
522
523 return 0;
524}
525
526/* *INDENT-OFF* */
527VLIB_CLI_COMMAND (bond_delete__command, static) =
528{
529 .path = "delete bond",
530 .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
531 .function = bond_delete_command_fn,
532};
533/* *INDENT-ON* */
534
535void
536bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args)
537{
538 bond_main_t *bm = &bond_main;
539 vnet_main_t *vnm = vnet_get_main ();
540 bond_if_t *bif;
541 slave_if_t *sif;
542 vnet_interface_main_t *im = &vnm->interface_main;
Steven4f8863b2018-04-12 19:36:19 -0700543 vnet_hw_interface_t *bif_hw, *sif_hw;
Steven9cd2d7a2017-12-20 12:43:01 -0800544 vnet_sw_interface_t *sw;
Stevenc4e99c52018-09-27 20:06:26 -0700545 u32 thread_index;
546 u32 sif_if_index;
Steven9cd2d7a2017-12-20 12:43:01 -0800547
548 bif = bond_get_master_by_sw_if_index (args->group);
549 if (!bif)
550 {
551 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
552 args->error = clib_error_return (0, "bond interface not found");
553 return;
554 }
555 // make sure the interface is not already enslaved
556 if (bond_get_slave_by_sw_if_index (args->slave))
557 {
558 args->rv = VNET_API_ERROR_VALUE_EXIST;
559 args->error = clib_error_return (0, "interface was already enslaved");
560 return;
561 }
Steven4f8863b2018-04-12 19:36:19 -0700562 sif_hw = vnet_get_sup_hw_interface (vnm, args->slave);
563 if (sif_hw->dev_class_index == bond_dev_class.index)
Steven9cd2d7a2017-12-20 12:43:01 -0800564 {
565 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
566 args->error =
567 clib_error_return (0, "bond interface cannot be enslaved");
568 return;
569 }
570 pool_get (bm->neighbors, sif);
Dave Barachb7b92992018-10-17 10:38:51 -0400571 clib_memset (sif, 0, sizeof (*sif));
Steven9cd2d7a2017-12-20 12:43:01 -0800572 sw = pool_elt_at_index (im->sw_interfaces, args->slave);
573 sif->port_enabled = sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP;
574 sif->sw_if_index = sw->sw_if_index;
575 sif->hw_if_index = sw->hw_if_index;
576 sif->packet_template_index = (u8) ~ 0;
577 sif->is_passive = args->is_passive;
578 sif->group = args->group;
579 sif->bif_dev_instance = bif->dev_instance;
580 sif->mode = bif->mode;
581
582 sif->is_long_timeout = args->is_long_timeout;
583 if (args->is_long_timeout)
584 sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
585 else
586 sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
587
Steven0d883012018-05-11 11:06:23 -0700588 vec_validate_aligned (bm->slave_by_sw_if_index, sif->sw_if_index,
589 CLIB_CACHE_LINE_BYTES);
590 /*
591 * sif - bm->neighbors may be 0
592 * Left shift it by 1 bit to distinguish the valid entry that we actually
593 * store from the null entries
594 */
595 bm->slave_by_sw_if_index[sif->sw_if_index] =
596 (uword) (((sif - bm->neighbors) << 1) | 1);
Steven9cd2d7a2017-12-20 12:43:01 -0800597 vec_add1 (bif->slaves, sif->sw_if_index);
598
Steven4f8863b2018-04-12 19:36:19 -0700599 sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
600
Steven9cd2d7a2017-12-20 12:43:01 -0800601 /* Save the old mac */
Steven4f8863b2018-04-12 19:36:19 -0700602 memcpy (sif->persistent_hw_address, sif_hw->hw_address, 6);
603 bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800604 if (bif->use_custom_mac)
605 {
Steven4f8863b2018-04-12 19:36:19 -0700606 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
Steven9cd2d7a2017-12-20 12:43:01 -0800607 bif->hw_address);
608 }
609 else
610 {
611 // bond interface gets the mac address from the first slave
612 if (vec_len (bif->slaves) == 1)
613 {
Steven4f8863b2018-04-12 19:36:19 -0700614 memcpy (bif->hw_address, sif_hw->hw_address, 6);
615 vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index,
616 sif_hw->hw_address);
Steven9cd2d7a2017-12-20 12:43:01 -0800617 }
618 else
619 {
620 // subsequent slaves gets the mac address of the bond interface
Steven4f8863b2018-04-12 19:36:19 -0700621 vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
Steven9cd2d7a2017-12-20 12:43:01 -0800622 bif->hw_address);
623 }
624 }
625
Steven4f8863b2018-04-12 19:36:19 -0700626 if (bif_hw->l2_if_count)
627 {
628 ethernet_set_flags (vnm, sif_hw->hw_if_index,
629 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
630 /* ensure all packets go to ethernet-input */
631 ethernet_set_rx_redirect (vnm, sif_hw, 1);
632 }
633
Stevene43278f2019-01-17 15:11:29 -0800634 if (bif->mode == BOND_MODE_LACP)
Steven9cd2d7a2017-12-20 12:43:01 -0800635 {
Stevene43278f2019-01-17 15:11:29 -0800636 if (bm->lacp_enable_disable)
637 (*bm->lacp_enable_disable) (vm, bif, sif, 1);
Steven9cd2d7a2017-12-20 12:43:01 -0800638 }
Stevene43278f2019-01-17 15:11:29 -0800639 else if (sif->port_enabled &&
640 (sif_hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
Steven9cd2d7a2017-12-20 12:43:01 -0800641 {
642 bond_enable_collecting_distributing (vm, sif);
643 }
644
Stevenc4e99c52018-09-27 20:06:26 -0700645 vec_foreach_index (thread_index, bm->per_thread_data)
646 {
647 bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data,
648 thread_index);
649
Damjan Marion69fdfee2018-10-06 14:33:18 +0200650 vec_validate_aligned (ptd->per_port_queue, vec_len (bif->slaves) - 1,
Stevenc4e99c52018-09-27 20:06:26 -0700651 CLIB_CACHE_LINE_BYTES);
652
653 vec_foreach_index (sif_if_index, ptd->per_port_queue)
654 {
655 ptd->per_port_queue[sif_if_index].n_buffers = 0;
656 }
657 }
658
Steven9cd2d7a2017-12-20 12:43:01 -0800659 args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
Steven4f8863b2018-04-12 19:36:19 -0700660 sif_hw->hw_if_index, 1, 0, 0);
Steven9cd2d7a2017-12-20 12:43:01 -0800661
662 if (args->rv)
663 {
664 args->error =
665 clib_error_return (0,
666 "Error encountered on input feature arc enable");
667 }
668}
669
670static clib_error_t *
671enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
672 vlib_cli_command_t * cmd)
673{
674 bond_enslave_args_t args = { 0 };
675 unformat_input_t _line_input, *line_input = &_line_input;
676 vnet_main_t *vnm = vnet_get_main ();
677
678 /* Get a line of input. */
679 if (!unformat_user (input, unformat_line_input, line_input))
680 return clib_error_return (0, "Missing required arguments.");
681
682 args.slave = ~0;
683 args.group = ~0;
684 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
685 {
Stevenb3caf552018-03-27 15:04:47 -0700686 if (unformat (line_input, "%U %U",
687 unformat_vnet_sw_interface, vnm, &args.group,
Steven9cd2d7a2017-12-20 12:43:01 -0800688 unformat_vnet_sw_interface, vnm, &args.slave))
689 ;
Steven9cd2d7a2017-12-20 12:43:01 -0800690 else if (unformat (line_input, "passive"))
691 args.is_passive = 1;
692 else if (unformat (line_input, "long-timeout"))
693 args.is_long_timeout = 1;
694 else
695 {
696 args.error = clib_error_return (0, "unknown input `%U'",
697 format_unformat_error, input);
698 break;
699 }
700 }
701 unformat_free (line_input);
702
703 if (args.error)
704 return args.error;
705 if (args.group == ~0)
706 return clib_error_return (0, "Missing bond interface");
707 if (args.slave == ~0)
Stevenb3caf552018-03-27 15:04:47 -0700708 return clib_error_return (0, "please specify valid slave interface name");
Steven9cd2d7a2017-12-20 12:43:01 -0800709
710 bond_enslave (vm, &args);
711
712 return args.error;
713}
714
715/* *INDENT-OFF* */
716VLIB_CLI_COMMAND (enslave_interface_command, static) = {
Stevenb3caf552018-03-27 15:04:47 -0700717 .path = "bond add",
718 .short_help = "bond add <BondEthernetx> <slave-interface> "
719 "[passive] [long-timeout]",
Steven9cd2d7a2017-12-20 12:43:01 -0800720 .function = enslave_interface_command_fn,
721};
722/* *INDENT-ON* */
723
724void
725bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args)
726{
727 bond_if_t *bif;
728 slave_if_t *sif;
729
730 sif = bond_get_slave_by_sw_if_index (args->slave);
731 if (!sif)
732 {
733 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
734 args->error = clib_error_return (0, "interface was not enslaved");
735 return;
736 }
737 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
738 bond_delete_neighbor (vm, bif, sif);
739}
740
741static clib_error_t *
742detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
743 vlib_cli_command_t * cmd)
744{
745 bond_detach_slave_args_t args = { 0 };
746 unformat_input_t _line_input, *line_input = &_line_input;
747 vnet_main_t *vnm = vnet_get_main ();
748
749 /* Get a line of input. */
750 if (!unformat_user (input, unformat_line_input, line_input))
751 return clib_error_return (0, "Missing required arguments.");
752
753 args.slave = ~0;
754 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
755 {
Stevenb3caf552018-03-27 15:04:47 -0700756 if (unformat (line_input, "%U",
Steven9cd2d7a2017-12-20 12:43:01 -0800757 unformat_vnet_sw_interface, vnm, &args.slave))
758 ;
759 else
760 {
761 args.error = clib_error_return (0, "unknown input `%U'",
762 format_unformat_error, input);
763 break;
764 }
765 }
766 unformat_free (line_input);
767
768 if (args.error)
769 return args.error;
770 if (args.slave == ~0)
Stevenb3caf552018-03-27 15:04:47 -0700771 return clib_error_return (0, "please specify valid slave interface name");
Steven9cd2d7a2017-12-20 12:43:01 -0800772
773 bond_detach_slave (vm, &args);
774
775 return args.error;
776}
777
778/* *INDENT-OFF* */
779VLIB_CLI_COMMAND (detach_interface_command, static) = {
Stevenb3caf552018-03-27 15:04:47 -0700780 .path = "bond del",
781 .short_help = "bond del <slave-interface>",
Steven9cd2d7a2017-12-20 12:43:01 -0800782 .function = detach_interface_command_fn,
783};
784/* *INDENT-ON* */
785
786static void
787show_bond (vlib_main_t * vm)
788{
789 bond_main_t *bm = &bond_main;
790 bond_if_t *bif;
791
Steven318de5d2018-10-06 22:30:50 -0700792 vlib_cli_output (vm, "%-16s %-12s %-13s %-13s %-14s %s",
Steven9cd2d7a2017-12-20 12:43:01 -0800793 "interface name", "sw_if_index", "mode",
794 "load balance", "active slaves", "slaves");
795
796 /* *INDENT-OFF* */
797 pool_foreach (bif, bm->interfaces,
798 ({
Steven318de5d2018-10-06 22:30:50 -0700799 vlib_cli_output (vm, "%-16U %-12d %-13U %-13U %-14u %u",
Steven9cd2d7a2017-12-20 12:43:01 -0800800 format_bond_interface_name, bif->dev_instance,
801 bif->sw_if_index, format_bond_mode, bif->mode,
802 format_bond_load_balance, bif->lb,
803 vec_len (bif->active_slaves), vec_len (bif->slaves));
804 }));
805 /* *INDENT-ON* */
806}
807
808static void
809show_bond_details (vlib_main_t * vm)
810{
811 bond_main_t *bm = &bond_main;
812 bond_if_t *bif;
813 u32 *sw_if_index;
814
815 /* *INDENT-OFF* */
816 pool_foreach (bif, bm->interfaces,
817 ({
818 vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
819 vlib_cli_output (vm, " mode: %U",
820 format_bond_mode, bif->mode);
821 vlib_cli_output (vm, " load balance: %U",
822 format_bond_load_balance, bif->lb);
823 if (bif->mode == BOND_MODE_ROUND_ROBIN)
824 vlib_cli_output (vm, " last xmit slave index: %u",
825 bif->lb_rr_last_index);
826 vlib_cli_output (vm, " number of active slaves: %d",
827 vec_len (bif->active_slaves));
828 vec_foreach (sw_if_index, bif->active_slaves)
829 {
830 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
831 vnet_get_main (), *sw_if_index);
832 }
833 vlib_cli_output (vm, " number of slaves: %d", vec_len (bif->slaves));
834 vec_foreach (sw_if_index, bif->slaves)
835 {
836 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
837 vnet_get_main (), *sw_if_index);
838 }
839 vlib_cli_output (vm, " device instance: %d", bif->dev_instance);
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500840 vlib_cli_output (vm, " interface id: %d", bif->id);
Steven9cd2d7a2017-12-20 12:43:01 -0800841 vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index);
842 vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index);
843 }));
844 /* *INDENT-ON* */
845}
846
847static clib_error_t *
848show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
849 vlib_cli_command_t * cmd)
850{
851 u8 details = 0;
852
853 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
854 {
855 if (unformat (input, "details"))
856 details = 1;
857 else
858 {
859 return clib_error_return (0, "unknown input `%U'",
860 format_unformat_error, input);
861 }
862 }
863
864 if (details)
865 show_bond_details (vm);
866 else
867 show_bond (vm);
868
869 return 0;
870}
871
872/* *INDENT-OFF* */
873VLIB_CLI_COMMAND (show_bond_command, static) = {
874 .path = "show bond",
875 .short_help = "show bond [details]",
876 .function = show_bond_fn,
877};
878/* *INDENT-ON* */
879
880clib_error_t *
881bond_cli_init (vlib_main_t * vm)
882{
883 bond_main_t *bm = &bond_main;
884
885 bm->vlib_main = vm;
886 bm->vnet_main = vnet_get_main ();
Steven0d883012018-05-11 11:06:23 -0700887 vec_validate_aligned (bm->slave_by_sw_if_index, 1, CLIB_CACHE_LINE_BYTES);
Stevenc4e99c52018-09-27 20:06:26 -0700888 vec_validate_aligned (bm->per_thread_data,
889 vlib_get_thread_main ()->n_vlib_mains - 1,
890 CLIB_CACHE_LINE_BYTES);
Steven9cd2d7a2017-12-20 12:43:01 -0800891
892 return 0;
893}
894
895VLIB_INIT_FUNCTION (bond_cli_init);
896
897/*
898 * fd.io coding-style-patch-verification: ON
899 *
900 * Local Variables:
901 * eval: (c-set-style "gnu")
902 * End:
903 */