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