bonding: fix interface deletion
[vpp.git] / src / vnet / bonding / cli.c
1 /*
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 #include <vpp/stats/stat_segment.h>
24
25 void
26 bond_disable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
27 {
28   bond_main_t *bm = &bond_main;
29   bond_if_t *bif;
30   int i;
31   uword p;
32   u8 switching_active = 0;
33
34   bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
35   clib_spinlock_lock_if_init (&bif->lockp);
36   vec_foreach_index (i, bif->active_slaves)
37   {
38     p = *vec_elt_at_index (bif->active_slaves, i);
39     if (p == sif->sw_if_index)
40       {
41         if ((bif->mode == BOND_MODE_ACTIVE_BACKUP) && (i == 0) &&
42             (vec_len (bif->active_slaves) > 1))
43           /* deleting the active slave for active-backup */
44           switching_active = 1;
45         vec_del1 (bif->active_slaves, i);
46         hash_unset (bif->active_slave_by_sw_if_index, sif->sw_if_index);
47         if (sif->lacp_enabled && bif->numa_only)
48           {
49             /* For lacp mode, if we check it is a slave on local numa node,
50                bif->n_numa_slaves should be decreased by 1 becasue the first
51                bif->n_numa_slaves are all slaves on local numa node */
52             if (i < bif->n_numa_slaves)
53               {
54                 bif->n_numa_slaves--;
55                 ASSERT (bif->n_numa_slaves >= 0);
56               }
57           }
58         break;
59       }
60   }
61
62   /* We get a new slave just becoming active */
63   if (switching_active)
64     vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
65                                BOND_SEND_GARP_NA, bif->hw_if_index);
66   clib_spinlock_unlock_if_init (&bif->lockp);
67
68   if (bif->mode == BOND_MODE_LACP)
69     stat_segment_set_state_counter (bm->stats[bif->sw_if_index]
70                                     [sif->sw_if_index], sif->actor.state);
71 }
72
73 /*
74  * return 1 if s2 is preferred.
75  * return -1 if s1 is preferred.
76  */
77 static int
78 bond_slave_sort (void *a1, void *a2)
79 {
80   u32 *s1 = a1;
81   u32 *s2 = a2;
82   slave_if_t *sif1 = bond_get_slave_by_sw_if_index (*s1);
83   slave_if_t *sif2 = bond_get_slave_by_sw_if_index (*s2);
84   bond_if_t *bif;
85
86   ASSERT (sif1);
87   ASSERT (sif2);
88   /*
89    * sort entries according to preference rules:
90    * 1. biggest weight
91    * 2. numa-node
92    * 3. current active slave (to prevent churning)
93    * 4. lowest sw_if_index (for deterministic behavior)
94    *
95    */
96   if (sif2->weight > sif1->weight)
97     return 1;
98   if (sif2->weight < sif1->weight)
99     return -1;
100   else
101     {
102       if (sif2->is_local_numa > sif1->is_local_numa)
103         return 1;
104       if (sif2->is_local_numa < sif1->is_local_numa)
105         return -1;
106       else
107         {
108           bif = bond_get_master_by_dev_instance (sif1->bif_dev_instance);
109           /* Favor the current active slave to avoid churning */
110           if (bif->active_slaves[0] == sif2->sw_if_index)
111             return 1;
112           if (bif->active_slaves[0] == sif1->sw_if_index)
113             return -1;
114           /* go for the tiebreaker as the last resort */
115           if (sif1->sw_if_index > sif2->sw_if_index)
116             return 1;
117           if (sif1->sw_if_index < sif2->sw_if_index)
118             return -1;
119           else
120             ASSERT (0);
121         }
122     }
123   return 0;
124 }
125
126 static void
127 bond_sort_slaves (bond_if_t * bif)
128 {
129   bond_main_t *bm = &bond_main;
130   u32 old_active = bif->active_slaves[0];
131
132   vec_sort_with_function (bif->active_slaves, bond_slave_sort);
133   if (old_active != bif->active_slaves[0])
134     vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
135                                BOND_SEND_GARP_NA, bif->hw_if_index);
136 }
137
138 void
139 bond_enable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
140 {
141   bond_if_t *bif;
142   bond_main_t *bm = &bond_main;
143   vnet_main_t *vnm = vnet_get_main ();
144   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
145
146   bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
147   clib_spinlock_lock_if_init (&bif->lockp);
148   if (!hash_get (bif->active_slave_by_sw_if_index, sif->sw_if_index))
149     {
150       hash_set (bif->active_slave_by_sw_if_index, sif->sw_if_index,
151                 sif->sw_if_index);
152
153       if ((sif->lacp_enabled && bif->numa_only)
154           && (vm->numa_node == hw->numa_node))
155         {
156           vec_insert_elts (bif->active_slaves, &sif->sw_if_index, 1,
157                            bif->n_numa_slaves);
158           bif->n_numa_slaves++;
159         }
160       else
161         vec_add1 (bif->active_slaves, sif->sw_if_index);
162
163       sif->is_local_numa = (vm->numa_node == hw->numa_node) ? 1 : 0;
164       if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
165         {
166           if (vec_len (bif->active_slaves) == 1)
167             /* First slave becomes active? */
168             vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
169                                        BOND_SEND_GARP_NA, bif->hw_if_index);
170           else
171             bond_sort_slaves (bif);
172         }
173     }
174   clib_spinlock_unlock_if_init (&bif->lockp);
175
176   if (bif->mode == BOND_MODE_LACP)
177     stat_segment_set_state_counter (bm->stats[bif->sw_if_index]
178                                     [sif->sw_if_index], sif->actor.state);
179 }
180
181 int
182 bond_dump_ifs (bond_interface_details_t ** out_bondifs)
183 {
184   vnet_main_t *vnm = vnet_get_main ();
185   bond_main_t *bm = &bond_main;
186   bond_if_t *bif;
187   vnet_hw_interface_t *hi;
188   bond_interface_details_t *r_bondifs = NULL;
189   bond_interface_details_t *bondif = NULL;
190
191   /* *INDENT-OFF* */
192   pool_foreach (bif, bm->interfaces,
193     vec_add2(r_bondifs, bondif, 1);
194     clib_memset (bondif, 0, sizeof (*bondif));
195     bondif->id = bif->id;
196     bondif->sw_if_index = bif->sw_if_index;
197     hi = vnet_get_hw_interface (vnm, bif->hw_if_index);
198     clib_memcpy(bondif->interface_name, hi->name,
199                 MIN (ARRAY_LEN (bondif->interface_name) - 1,
200                      vec_len ((const char *) hi->name)));
201     /* enforce by memset() above */
202     ASSERT(0 == bondif->interface_name[ARRAY_LEN (bondif->interface_name) - 1]);
203     bondif->mode = bif->mode;
204     bondif->lb = bif->lb;
205     bondif->numa_only = bif->numa_only;
206     bondif->active_slaves = vec_len (bif->active_slaves);
207     bondif->slaves = vec_len (bif->slaves);
208   );
209   /* *INDENT-ON* */
210
211   *out_bondifs = r_bondifs;
212
213   return 0;
214 }
215
216 int
217 bond_dump_slave_ifs (slave_interface_details_t ** out_slaveifs,
218                      u32 bond_sw_if_index)
219 {
220   vnet_main_t *vnm = vnet_get_main ();
221   bond_if_t *bif;
222   vnet_hw_interface_t *hi;
223   vnet_sw_interface_t *sw;
224   slave_interface_details_t *r_slaveifs = NULL;
225   slave_interface_details_t *slaveif = NULL;
226   u32 *sw_if_index = NULL;
227   slave_if_t *sif;
228
229   bif = bond_get_master_by_sw_if_index (bond_sw_if_index);
230   if (!bif)
231     return 1;
232
233   vec_foreach (sw_if_index, bif->slaves)
234   {
235     vec_add2 (r_slaveifs, slaveif, 1);
236     clib_memset (slaveif, 0, sizeof (*slaveif));
237     sif = bond_get_slave_by_sw_if_index (*sw_if_index);
238     if (sif)
239       {
240         sw = vnet_get_sw_interface (vnm, sif->sw_if_index);
241         hi = vnet_get_hw_interface (vnm, sw->hw_if_index);
242         clib_memcpy (slaveif->interface_name, hi->name,
243                      MIN (ARRAY_LEN (slaveif->interface_name) - 1,
244                           vec_len ((const char *) hi->name)));
245         /* enforce by memset() above */
246         ASSERT (0 ==
247                 slaveif->interface_name[ARRAY_LEN (slaveif->interface_name) -
248                                         1]);
249         slaveif->sw_if_index = sif->sw_if_index;
250         slaveif->is_passive = sif->is_passive;
251         slaveif->is_long_timeout = sif->is_long_timeout;
252         slaveif->is_local_numa = sif->is_local_numa;
253         slaveif->weight = sif->weight;
254       }
255   }
256   *out_slaveifs = r_slaveifs;
257
258   return 0;
259 }
260
261 static void
262 bond_delete_neighbor (vlib_main_t * vm, bond_if_t * bif, slave_if_t * sif)
263 {
264   bond_main_t *bm = &bond_main;
265   vnet_main_t *vnm = vnet_get_main ();
266   int i;
267   vnet_hw_interface_t *sif_hw;
268
269   sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
270
271   bif->port_number_bitmap =
272     clib_bitmap_set (bif->port_number_bitmap,
273                      ntohs (sif->actor_admin.port_number) - 1, 0);
274   bm->slave_by_sw_if_index[sif->sw_if_index] = 0;
275   vec_free (sif->last_marker_pkt);
276   vec_free (sif->last_rx_pkt);
277   vec_foreach_index (i, bif->slaves)
278   {
279     uword p = *vec_elt_at_index (bif->slaves, i);
280     if (p == sif->sw_if_index)
281       {
282         vec_del1 (bif->slaves, i);
283         break;
284       }
285   }
286
287   bond_disable_collecting_distributing (vm, sif);
288
289   vnet_feature_enable_disable ("device-input", "bond-input",
290                                sif->sw_if_index, 0, 0, 0);
291
292   /* Put back the old mac */
293   vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
294                                         sif->persistent_hw_address);
295
296   if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
297     (*bm->lacp_enable_disable) (vm, bif, sif, 0);
298
299   if (bif->mode == BOND_MODE_LACP)
300     stat_segment_deregister_state_counter
301       (bm->stats[bif->sw_if_index][sif->sw_if_index]);
302
303   pool_put (bm->neighbors, sif);
304 }
305
306 int
307 bond_delete_if (vlib_main_t * vm, u32 sw_if_index)
308 {
309   bond_main_t *bm = &bond_main;
310   vnet_main_t *vnm = vnet_get_main ();
311   bond_if_t *bif;
312   slave_if_t *sif;
313   vnet_hw_interface_t *hw;
314   u32 *sif_sw_if_index;
315   u32 *s_list = 0;
316
317   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
318   if (hw == NULL || bond_dev_class.index != hw->dev_class_index)
319     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
320
321   bif = bond_get_master_by_dev_instance (hw->dev_instance);
322
323   vec_append (s_list, bif->slaves);
324   vec_foreach (sif_sw_if_index, s_list)
325   {
326     sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index);
327     if (sif)
328       bond_delete_neighbor (vm, bif, sif);
329   }
330   vec_free (s_list);
331
332   /* bring down the interface */
333   vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0);
334   vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0);
335
336   ethernet_delete_interface (vnm, bif->hw_if_index);
337
338   clib_bitmap_free (bif->port_number_bitmap);
339   hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index);
340   hash_unset (bm->id_used, bif->id);
341   clib_memset (bif, 0, sizeof (*bif));
342   pool_put (bm->interfaces, bif);
343
344   return 0;
345 }
346
347 void
348 bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args)
349 {
350   bond_main_t *bm = &bond_main;
351   vnet_main_t *vnm = vnet_get_main ();
352   vnet_sw_interface_t *sw;
353   bond_if_t *bif;
354
355   if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0)
356     {
357       args->rv = VNET_API_ERROR_FEATURE_DISABLED;
358       args->error = clib_error_return (0, "LACP plugin is not loaded");
359       return;
360     }
361   if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN)
362     {
363       args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
364       args->error = clib_error_return (0, "Invalid mode");
365       return;
366     }
367   if (args->lb > BOND_LB_L23)
368     {
369       args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
370       args->error = clib_error_return (0, "Invalid load-balance");
371       return;
372     }
373   pool_get (bm->interfaces, bif);
374   clib_memset (bif, 0, sizeof (*bif));
375   bif->dev_instance = bif - bm->interfaces;
376   bif->id = args->id;
377   bif->lb = args->lb;
378   bif->mode = args->mode;
379
380   // Adjust requested interface id
381   if (bif->id == ~0)
382     bif->id = bif->dev_instance;
383   if (hash_get (bm->id_used, bif->id))
384     {
385       args->rv = VNET_API_ERROR_INSTANCE_IN_USE;
386       pool_put (bm->interfaces, bif);
387       return;
388     }
389   hash_set (bm->id_used, bif->id, 1);
390
391   // Special load-balance mode used for rr and bc
392   if (bif->mode == BOND_MODE_ROUND_ROBIN)
393     bif->lb = BOND_LB_RR;
394   else if (bif->mode == BOND_MODE_BROADCAST)
395     bif->lb = BOND_LB_BC;
396   else if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
397     bif->lb = BOND_LB_AB;
398
399   bif->use_custom_mac = args->hw_addr_set;
400   if (!args->hw_addr_set)
401     {
402       f64 now = vlib_time_now (vm);
403       u32 rnd;
404       rnd = (u32) (now * 1e6);
405       rnd = random_u32 (&rnd);
406
407       memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
408       args->hw_addr[0] = 2;
409       args->hw_addr[1] = 0xfe;
410     }
411   memcpy (bif->hw_address, args->hw_addr, 6);
412   args->error = ethernet_register_interface
413     (vnm, bond_dev_class.index, bif->dev_instance /* device instance */ ,
414      bif->hw_address /* ethernet address */ ,
415      &bif->hw_if_index, 0 /* flag change */ );
416
417   if (args->error)
418     {
419       args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
420       hash_unset (bm->id_used, bif->id);
421       pool_put (bm->interfaces, bif);
422       return;
423     }
424
425   sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
426   bif->sw_if_index = sw->sw_if_index;
427   bif->group = bif->sw_if_index;
428   bif->numa_only = args->numa_only;
429   if (vlib_get_thread_main ()->n_vlib_mains > 1)
430     clib_spinlock_init (&bif->lockp);
431
432   vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
433                                VNET_HW_INTERFACE_FLAG_LINK_UP);
434
435   hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
436
437   // for return
438   args->sw_if_index = bif->sw_if_index;
439   args->rv = 0;
440 }
441
442 static clib_error_t *
443 bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
444                         vlib_cli_command_t * cmd)
445 {
446   unformat_input_t _line_input, *line_input = &_line_input;
447   bond_create_if_args_t args = { 0 };
448   u8 mode_is_set = 0;
449
450   /* Get a line of input. */
451   if (!unformat_user (input, unformat_line_input, line_input))
452     return clib_error_return (0, "Missing required arguments.");
453
454   args.id = ~0;
455   args.mode = -1;
456   args.lb = BOND_LB_L2;
457   args.rv = -1;
458   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
459     {
460       if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
461         mode_is_set = 1;
462       else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
463                && unformat (line_input, "load-balance %U",
464                             unformat_bond_load_balance, &args.lb))
465         ;
466       else if (unformat (line_input, "hw-addr %U",
467                          unformat_ethernet_address, args.hw_addr))
468         args.hw_addr_set = 1;
469       else if (unformat (line_input, "id %u", &args.id))
470         ;
471       else if (unformat (line_input, "numa-only"))
472         {
473           if (args.mode == BOND_MODE_LACP)
474             args.numa_only = 1;
475           else
476             return clib_error_return (0,
477                                       "Only lacp mode supports numa-only so far!");
478         }
479       else
480         return clib_error_return (0, "unknown input `%U'",
481                                   format_unformat_error, input);
482     }
483   unformat_free (line_input);
484
485   if (mode_is_set == 0)
486     return clib_error_return (0, "Missing bond mode");
487
488   bond_create_if (vm, &args);
489
490   if (!args.rv)
491     vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
492                      vnet_get_main (), args.sw_if_index);
493
494   return args.error;
495 }
496
497 /* *INDENT-OFF* */
498 VLIB_CLI_COMMAND (bond_create_command, static) = {
499   .path = "create bond",
500   .short_help = "create bond mode {round-robin | active-backup | broadcast | "
501     "{lacp | xor} [load-balance { l2 | l23 | l34 } [numa-only]]} [hw-addr <mac-address>] "
502     "[id <if-id>]",
503   .function = bond_create_command_fn,
504 };
505 /* *INDENT-ON* */
506
507 static clib_error_t *
508 bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
509                         vlib_cli_command_t * cmd)
510 {
511   unformat_input_t _line_input, *line_input = &_line_input;
512   u32 sw_if_index = ~0;
513   vnet_main_t *vnm = vnet_get_main ();
514   int rv;
515
516   /* Get a line of input. */
517   if (!unformat_user (input, unformat_line_input, line_input))
518     return clib_error_return (0, "Missing <interface>");
519
520   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
521     {
522       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
523         ;
524       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
525                          vnm, &sw_if_index))
526         ;
527       else
528         return clib_error_return (0, "unknown input `%U'",
529                                   format_unformat_error, input);
530     }
531   unformat_free (line_input);
532
533   if (sw_if_index == ~0)
534     return clib_error_return (0,
535                               "please specify interface name or sw_if_index");
536
537   rv = bond_delete_if (vm, sw_if_index);
538   if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
539     return clib_error_return (0, "not a bond interface");
540   else if (rv != 0)
541     return clib_error_return (0, "error on deleting bond interface");
542
543   return 0;
544 }
545
546 /* *INDENT-OFF* */
547 VLIB_CLI_COMMAND (bond_delete__command, static) =
548 {
549   .path = "delete bond",
550   .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
551   .function = bond_delete_command_fn,
552 };
553 /* *INDENT-ON* */
554
555 void
556 bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args)
557 {
558   bond_main_t *bm = &bond_main;
559   vnet_main_t *vnm = vnet_get_main ();
560   bond_if_t *bif;
561   slave_if_t *sif;
562   vnet_interface_main_t *im = &vnm->interface_main;
563   vnet_hw_interface_t *bif_hw, *sif_hw;
564   vnet_sw_interface_t *sw;
565   u32 thread_index;
566   u32 sif_if_index;
567
568   bif = bond_get_master_by_sw_if_index (args->group);
569   if (!bif)
570     {
571       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
572       args->error = clib_error_return (0, "bond interface not found");
573       return;
574     }
575   // make sure the interface is not already enslaved
576   if (bond_get_slave_by_sw_if_index (args->slave))
577     {
578       args->rv = VNET_API_ERROR_VALUE_EXIST;
579       args->error = clib_error_return (0, "interface was already enslaved");
580       return;
581     }
582   sif_hw = vnet_get_sup_hw_interface (vnm, args->slave);
583   if (sif_hw->dev_class_index == bond_dev_class.index)
584     {
585       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
586       args->error =
587         clib_error_return (0, "bond interface cannot be enslaved");
588       return;
589     }
590   if (bif->mode == BOND_MODE_LACP)
591     {
592       u8 *name = format (0, "/if/lacp/%u/%u/state", bif->sw_if_index,
593                          args->slave);
594
595       vec_validate (bm->stats, bif->sw_if_index);
596       vec_validate (bm->stats[bif->sw_if_index], args->slave);
597
598       args->error = stat_segment_register_state_counter
599         (name, &bm->stats[bif->sw_if_index][args->slave]);
600       vec_free (name);
601       if (args->error != 0)
602         {
603           args->rv = VNET_API_ERROR_INVALID_INTERFACE;
604           return;
605         }
606     }
607
608   pool_get (bm->neighbors, sif);
609   clib_memset (sif, 0, sizeof (*sif));
610   sw = pool_elt_at_index (im->sw_interfaces, args->slave);
611   /* port_enabled is both admin up and hw link up */
612   sif->port_enabled = vnet_sw_interface_is_up (vnm, sw->sw_if_index);
613   sif->sw_if_index = sw->sw_if_index;
614   sif->hw_if_index = sw->hw_if_index;
615   sif->packet_template_index = (u8) ~ 0;
616   sif->is_passive = args->is_passive;
617   sif->group = args->group;
618   sif->bif_dev_instance = bif->dev_instance;
619   sif->mode = bif->mode;
620
621   sif->is_long_timeout = args->is_long_timeout;
622   if (args->is_long_timeout)
623     sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
624   else
625     sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
626
627   vec_validate_aligned (bm->slave_by_sw_if_index, sif->sw_if_index,
628                         CLIB_CACHE_LINE_BYTES);
629   /*
630    * sif - bm->neighbors may be 0
631    * Left shift it by 1 bit to distinguish the valid entry that we actually
632    * store from the null entries
633    */
634   bm->slave_by_sw_if_index[sif->sw_if_index] =
635     (uword) (((sif - bm->neighbors) << 1) | 1);
636   vec_add1 (bif->slaves, sif->sw_if_index);
637
638   sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
639
640   /* Save the old mac */
641   memcpy (sif->persistent_hw_address, sif_hw->hw_address, 6);
642   bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
643   if (bif->use_custom_mac)
644     {
645       vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
646                                             bif->hw_address);
647     }
648   else
649     {
650       // bond interface gets the mac address from the first slave
651       if (vec_len (bif->slaves) == 1)
652         {
653           memcpy (bif->hw_address, sif_hw->hw_address, 6);
654           vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index,
655                                                 sif_hw->hw_address);
656         }
657       else
658         {
659           // subsequent slaves gets the mac address of the bond interface
660           vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
661                                                 bif->hw_address);
662         }
663     }
664
665   if (bif_hw->l2_if_count)
666     {
667       ethernet_set_flags (vnm, sif_hw->hw_if_index,
668                           ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
669       /* ensure all packets go to ethernet-input */
670       ethernet_set_rx_redirect (vnm, sif_hw, 1);
671     }
672
673   if (bif->mode == BOND_MODE_LACP)
674     {
675       if (bm->lacp_enable_disable)
676         (*bm->lacp_enable_disable) (vm, bif, sif, 1);
677     }
678   else if (sif->port_enabled)
679     {
680       bond_enable_collecting_distributing (vm, sif);
681     }
682
683   vec_foreach_index (thread_index, bm->per_thread_data)
684   {
685     bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data,
686                                                     thread_index);
687
688     vec_validate_aligned (ptd->per_port_queue, vec_len (bif->slaves) - 1,
689                           CLIB_CACHE_LINE_BYTES);
690
691     vec_foreach_index (sif_if_index, ptd->per_port_queue)
692     {
693       ptd->per_port_queue[sif_if_index].n_buffers = 0;
694     }
695   }
696
697   args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
698                                           sif->sw_if_index, 1, 0, 0);
699
700   if (args->rv)
701     {
702       args->error =
703         clib_error_return (0,
704                            "Error encountered on input feature arc enable");
705     }
706 }
707
708 static clib_error_t *
709 enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
710                               vlib_cli_command_t * cmd)
711 {
712   bond_enslave_args_t args = { 0 };
713   unformat_input_t _line_input, *line_input = &_line_input;
714   vnet_main_t *vnm = vnet_get_main ();
715
716   /* Get a line of input. */
717   if (!unformat_user (input, unformat_line_input, line_input))
718     return clib_error_return (0, "Missing required arguments.");
719
720   args.slave = ~0;
721   args.group = ~0;
722   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
723     {
724       if (unformat (line_input, "%U %U",
725                     unformat_vnet_sw_interface, vnm, &args.group,
726                     unformat_vnet_sw_interface, vnm, &args.slave))
727         ;
728       else if (unformat (line_input, "passive"))
729         args.is_passive = 1;
730       else if (unformat (line_input, "long-timeout"))
731         args.is_long_timeout = 1;
732       else
733         {
734           args.error = clib_error_return (0, "unknown input `%U'",
735                                           format_unformat_error, input);
736           break;
737         }
738     }
739   unformat_free (line_input);
740
741   if (args.error)
742     return args.error;
743   if (args.group == ~0)
744     return clib_error_return (0, "Missing bond interface");
745   if (args.slave == ~0)
746     return clib_error_return (0, "please specify valid slave interface name");
747
748   bond_enslave (vm, &args);
749
750   return args.error;
751 }
752
753 /* *INDENT-OFF* */
754 VLIB_CLI_COMMAND (enslave_interface_command, static) = {
755   .path = "bond add",
756   .short_help = "bond add <BondEthernetx> <slave-interface> "
757                 "[passive] [long-timeout]",
758   .function = enslave_interface_command_fn,
759 };
760 /* *INDENT-ON* */
761
762 void
763 bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args)
764 {
765   bond_if_t *bif;
766   slave_if_t *sif;
767
768   sif = bond_get_slave_by_sw_if_index (args->slave);
769   if (!sif)
770     {
771       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
772       args->error = clib_error_return (0, "interface was not enslaved");
773       return;
774     }
775   bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
776   bond_delete_neighbor (vm, bif, sif);
777 }
778
779 static clib_error_t *
780 detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
781                              vlib_cli_command_t * cmd)
782 {
783   bond_detach_slave_args_t args = { 0 };
784   unformat_input_t _line_input, *line_input = &_line_input;
785   vnet_main_t *vnm = vnet_get_main ();
786
787   /* Get a line of input. */
788   if (!unformat_user (input, unformat_line_input, line_input))
789     return clib_error_return (0, "Missing required arguments.");
790
791   args.slave = ~0;
792   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
793     {
794       if (unformat (line_input, "%U",
795                     unformat_vnet_sw_interface, vnm, &args.slave))
796         ;
797       else
798         {
799           args.error = clib_error_return (0, "unknown input `%U'",
800                                           format_unformat_error, input);
801           break;
802         }
803     }
804   unformat_free (line_input);
805
806   if (args.error)
807     return args.error;
808   if (args.slave == ~0)
809     return clib_error_return (0, "please specify valid slave interface name");
810
811   bond_detach_slave (vm, &args);
812
813   return args.error;
814 }
815
816 /* *INDENT-OFF* */
817 VLIB_CLI_COMMAND (detach_interface_command, static) = {
818   .path = "bond del",
819   .short_help = "bond del <slave-interface>",
820   .function = detach_interface_command_fn,
821 };
822 /* *INDENT-ON* */
823
824 static void
825 show_bond (vlib_main_t * vm)
826 {
827   bond_main_t *bm = &bond_main;
828   bond_if_t *bif;
829
830   vlib_cli_output (vm, "%-16s %-12s %-13s %-13s %-14s %s",
831                    "interface name", "sw_if_index", "mode",
832                    "load balance", "active slaves", "slaves");
833
834   /* *INDENT-OFF* */
835   pool_foreach (bif, bm->interfaces,
836   ({
837     vlib_cli_output (vm, "%-16U %-12d %-13U %-13U %-14u %u",
838                      format_bond_interface_name, bif->dev_instance,
839                      bif->sw_if_index, format_bond_mode, bif->mode,
840                      format_bond_load_balance, bif->lb,
841                      vec_len (bif->active_slaves), vec_len (bif->slaves));
842   }));
843   /* *INDENT-ON* */
844 }
845
846 static void
847 show_bond_details (vlib_main_t * vm)
848 {
849   bond_main_t *bm = &bond_main;
850   bond_if_t *bif;
851   u32 *sw_if_index;
852
853   /* *INDENT-OFF* */
854   pool_foreach (bif, bm->interfaces,
855   ({
856     vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
857     vlib_cli_output (vm, "  mode: %U",
858                      format_bond_mode, bif->mode);
859     vlib_cli_output (vm, "  load balance: %U",
860                      format_bond_load_balance, bif->lb);
861     if (bif->mode == BOND_MODE_ROUND_ROBIN)
862       vlib_cli_output (vm, "  last xmit slave index: %u",
863                        bif->lb_rr_last_index);
864     vlib_cli_output (vm, "  number of active slaves: %d",
865                      vec_len (bif->active_slaves));
866     vec_foreach (sw_if_index, bif->active_slaves)
867       {
868         vlib_cli_output (vm, "    %U", format_vnet_sw_if_index_name,
869                          vnet_get_main (), *sw_if_index);
870         if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
871           {
872             slave_if_t *sif = bond_get_slave_by_sw_if_index (*sw_if_index);
873             if (sif)
874               vlib_cli_output (vm, "      weight: %u, is_local_numa: %u, "
875                                "sw_if_index: %u", sif->weight,
876                                sif->is_local_numa, sif->sw_if_index);
877           }
878       }
879     vlib_cli_output (vm, "  number of slaves: %d", vec_len (bif->slaves));
880     vec_foreach (sw_if_index, bif->slaves)
881       {
882         vlib_cli_output (vm, "    %U", format_vnet_sw_if_index_name,
883                          vnet_get_main (), *sw_if_index);
884       }
885     vlib_cli_output (vm, "  device instance: %d", bif->dev_instance);
886     vlib_cli_output (vm, "  interface id: %d", bif->id);
887     vlib_cli_output (vm, "  sw_if_index: %d", bif->sw_if_index);
888     vlib_cli_output (vm, "  hw_if_index: %d", bif->hw_if_index);
889   }));
890   /* *INDENT-ON* */
891 }
892
893 static clib_error_t *
894 show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
895               vlib_cli_command_t * cmd)
896 {
897   u8 details = 0;
898
899   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
900     {
901       if (unformat (input, "details"))
902         details = 1;
903       else
904         {
905           return clib_error_return (0, "unknown input `%U'",
906                                     format_unformat_error, input);
907         }
908     }
909
910   if (details)
911     show_bond_details (vm);
912   else
913     show_bond (vm);
914
915   return 0;
916 }
917
918 /* *INDENT-OFF* */
919 VLIB_CLI_COMMAND (show_bond_command, static) = {
920   .path = "show bond",
921   .short_help = "show bond [details]",
922   .function = show_bond_fn,
923 };
924 /* *INDENT-ON* */
925
926 void
927 bond_set_intf_weight (vlib_main_t * vm, bond_set_intf_weight_args_t * args)
928 {
929   slave_if_t *sif;
930   bond_if_t *bif;
931   vnet_main_t *vnm;
932   u32 old_weight;
933
934   sif = bond_get_slave_by_sw_if_index (args->sw_if_index);
935   if (!sif)
936     {
937       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
938       args->error = clib_error_return (0, "Interface not enslaved");
939       return;
940     }
941   bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
942   if (!bif)
943     {
944       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
945       args->error = clib_error_return (0, "bond interface not found");
946       return;
947     }
948   if (bif->mode != BOND_MODE_ACTIVE_BACKUP)
949     {
950       args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
951       args->error =
952         clib_error_return (0, "Weight valid for active-backup only");
953       return;
954     }
955
956   old_weight = sif->weight;
957   sif->weight = args->weight;
958   vnm = vnet_get_main ();
959   /*
960    * No need to sort the list if the affected slave is not up (not in active
961    * slave set), active slave count is 1, or the current slave is already the
962    * primary slave and new weight > old weight.
963    */
964   if (!vnet_sw_interface_is_up (vnm, sif->sw_if_index) ||
965       (vec_len (bif->active_slaves) == 1) ||
966       ((bif->active_slaves[0] == sif->sw_if_index) &&
967        (sif->weight >= old_weight)))
968     return;
969
970   bond_sort_slaves (bif);
971 }
972
973 static clib_error_t *
974 bond_set_intf_cmd (vlib_main_t * vm, unformat_input_t * input,
975                    vlib_cli_command_t * cmd)
976 {
977   bond_set_intf_weight_args_t args = { 0 };
978   u32 sw_if_index = (u32) ~ 0;
979   unformat_input_t _line_input, *line_input = &_line_input;
980   vnet_main_t *vnm = vnet_get_main ();
981   u8 weight_enter = 0;
982   u32 weight = 0;
983
984   /* Get a line of input. */
985   if (!unformat_user (input, unformat_line_input, line_input))
986     return clib_error_return (0, "Missing required arguments.");
987
988   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
989     {
990       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
991         ;
992       else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
993                          &sw_if_index))
994         ;
995       else if (unformat (line_input, "weight %u", &weight))
996         weight_enter = 1;
997       else
998         {
999           clib_error_return (0, "unknown input `%U'", format_unformat_error,
1000                              input);
1001           break;
1002         }
1003     }
1004
1005   unformat_free (line_input);
1006   if (sw_if_index == (u32) ~ 0)
1007     {
1008       args.rv = VNET_API_ERROR_INVALID_INTERFACE;
1009       clib_error_return (0, "Interface name is invalid!");
1010     }
1011   if (weight_enter == 0)
1012     {
1013       args.rv = VNET_API_ERROR_INVALID_ARGUMENT;
1014       clib_error_return (0, "weight missing");
1015     }
1016
1017   args.sw_if_index = sw_if_index;
1018   args.weight = weight;
1019   bond_set_intf_weight (vm, &args);
1020
1021   return args.error;
1022 }
1023
1024 /* *INDENT-OFF* */
1025 VLIB_CLI_COMMAND(set_interface_bond_cmd, static) = {
1026   .path = "set interface bond",
1027   .short_help = "set interface bond <interface> | sw_if_index <idx>"
1028                 " weight <value>",
1029   .function = bond_set_intf_cmd,
1030 };
1031 /* *INDENT-ON* */
1032
1033 clib_error_t *
1034 bond_cli_init (vlib_main_t * vm)
1035 {
1036   bond_main_t *bm = &bond_main;
1037
1038   bm->vlib_main = vm;
1039   bm->vnet_main = vnet_get_main ();
1040   vec_validate_aligned (bm->slave_by_sw_if_index, 1, CLIB_CACHE_LINE_BYTES);
1041   vec_validate_aligned (bm->per_thread_data,
1042                         vlib_get_thread_main ()->n_vlib_mains - 1,
1043                         CLIB_CACHE_LINE_BYTES);
1044
1045   return 0;
1046 }
1047
1048 VLIB_INIT_FUNCTION (bond_cli_init);
1049
1050 /*
1051  * fd.io coding-style-patch-verification: ON
1052  *
1053  * Local Variables:
1054  * eval: (c-set-style "gnu")
1055  * End:
1056  */