bond: show trace causes a crash if the interface is deleted
[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
24 void
25 bond_disable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
26 {
27   bond_if_t *bif;
28   int i;
29   uword p;
30
31   bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
32   clib_spinlock_lock_if_init (&bif->lockp);
33   vec_foreach_index (i, bif->active_slaves)
34   {
35     p = *vec_elt_at_index (bif->active_slaves, i);
36     if (p == sif->sw_if_index)
37       {
38         vec_del1 (bif->active_slaves, i);
39         hash_unset (bif->active_slave_by_sw_if_index, sif->sw_if_index);
40         break;
41       }
42   }
43   clib_spinlock_unlock_if_init (&bif->lockp);
44 }
45
46 void
47 bond_enable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
48 {
49   bond_if_t *bif;
50
51   bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
52   clib_spinlock_lock_if_init (&bif->lockp);
53   if (!hash_get (bif->active_slave_by_sw_if_index, sif->sw_if_index))
54     {
55       hash_set (bif->active_slave_by_sw_if_index, sif->sw_if_index,
56                 sif->sw_if_index);
57       vec_add1 (bif->active_slaves, sif->sw_if_index);
58     }
59   clib_spinlock_unlock_if_init (&bif->lockp);
60 }
61
62 int
63 bond_dump_ifs (bond_interface_details_t ** out_bondifs)
64 {
65   vnet_main_t *vnm = vnet_get_main ();
66   bond_main_t *bm = &bond_main;
67   bond_if_t *bif;
68   vnet_hw_interface_t *hi;
69   bond_interface_details_t *r_bondifs = NULL;
70   bond_interface_details_t *bondif = NULL;
71
72   /* *INDENT-OFF* */
73   pool_foreach (bif, bm->interfaces,
74     vec_add2(r_bondifs, bondif, 1);
75     memset (bondif, 0, sizeof (*bondif));
76     bondif->sw_if_index = bif->sw_if_index;
77     hi = vnet_get_hw_interface (vnm, bif->hw_if_index);
78     clib_memcpy(bondif->interface_name, hi->name,
79                 MIN (ARRAY_LEN (bondif->interface_name) - 1,
80                      strlen ((const char *) hi->name)));
81     bondif->mode = bif->mode;
82     bondif->lb = bif->lb;
83     bondif->active_slaves = vec_len (bif->active_slaves);
84     bondif->slaves = vec_len (bif->slaves);
85   );
86   /* *INDENT-ON* */
87
88   *out_bondifs = r_bondifs;
89
90   return 0;
91 }
92
93 int
94 bond_dump_slave_ifs (slave_interface_details_t ** out_slaveifs,
95                      u32 bond_sw_if_index)
96 {
97   vnet_main_t *vnm = vnet_get_main ();
98   bond_if_t *bif;
99   vnet_hw_interface_t *hi;
100   vnet_sw_interface_t *sw;
101   slave_interface_details_t *r_slaveifs = NULL;
102   slave_interface_details_t *slaveif = NULL;
103   u32 *sw_if_index = NULL;
104   slave_if_t *sif;
105
106   bif = bond_get_master_by_sw_if_index (bond_sw_if_index);
107   if (!bif)
108     return 1;
109
110   vec_foreach (sw_if_index, bif->slaves)
111   {
112     vec_add2 (r_slaveifs, slaveif, 1);
113     memset (slaveif, 0, sizeof (*slaveif));
114     sif = bond_get_slave_by_sw_if_index (*sw_if_index);
115     if (sif)
116       {
117         sw = vnet_get_sw_interface (vnm, sif->sw_if_index);
118         hi = vnet_get_hw_interface (vnm, sw->hw_if_index);
119         clib_memcpy (slaveif->interface_name, hi->name,
120                      MIN (ARRAY_LEN (slaveif->interface_name) - 1,
121                           strlen ((const char *) hi->name)));
122         slaveif->sw_if_index = sif->sw_if_index;
123         slaveif->is_passive = sif->is_passive;
124         slaveif->is_long_timeout = sif->is_long_timeout;
125       }
126   }
127   *out_slaveifs = r_slaveifs;
128
129   return 0;
130 }
131
132 static void
133 bond_delete_neighbor (vlib_main_t * vm, bond_if_t * bif, slave_if_t * sif)
134 {
135   bond_main_t *bm = &bond_main;
136   vnet_main_t *vnm = vnet_get_main ();
137   int i;
138   vnet_hw_interface_t *hw;
139
140   bif->port_number_bitmap =
141     clib_bitmap_set (bif->port_number_bitmap,
142                      ntohs (sif->actor_admin.port_number) - 1, 0);
143   hash_unset (bm->neighbor_by_sw_if_index, sif->sw_if_index);
144   vec_free (sif->last_marker_pkt);
145   vec_free (sif->last_rx_pkt);
146   vec_foreach_index (i, bif->slaves)
147   {
148     uword p = *vec_elt_at_index (bif->slaves, i);
149     if (p == sif->sw_if_index)
150       {
151         vec_del1 (bif->slaves, i);
152         break;
153       }
154   }
155
156   bond_disable_collecting_distributing (vm, sif);
157
158   /* Put back the old mac */
159   hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
160   vnet_hw_interface_change_mac_address (vnm, hw->hw_if_index,
161                                         sif->persistent_hw_address);
162
163   pool_put (bm->neighbors, sif);
164
165   if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
166     (*bm->lacp_enable_disable) (vm, bif, sif, 0);
167 }
168
169 int
170 bond_delete_if (vlib_main_t * vm, u32 sw_if_index)
171 {
172   bond_main_t *bm = &bond_main;
173   vnet_main_t *vnm = vnet_get_main ();
174   bond_if_t *bif;
175   slave_if_t *sif;
176   vnet_hw_interface_t *hw;
177   u32 *sif_sw_if_index;
178   u32 thread_index;
179   u32 **s_list = 0;
180   u32 i;
181
182   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
183   if (hw == NULL || bond_dev_class.index != hw->dev_class_index)
184     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
185
186   bif = bond_get_master_by_dev_instance (hw->dev_instance);
187
188   vec_foreach (sif_sw_if_index, bif->slaves)
189   {
190     vec_add1 (s_list, sif_sw_if_index);
191   }
192
193   for (i = 0; i < vec_len (s_list); i++)
194     {
195       sif_sw_if_index = s_list[i];
196       sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index);
197       if (sif)
198         bond_delete_neighbor (vm, bif, sif);
199     }
200
201   if (s_list)
202     vec_free (s_list);
203
204   /* bring down the interface */
205   vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0);
206   vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0);
207
208   ethernet_delete_interface (vnm, bif->hw_if_index);
209
210   clib_bitmap_free (bif->port_number_bitmap);
211   hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index);
212   for (thread_index = 0; thread_index < vlib_get_thread_main ()->n_vlib_mains;
213        thread_index++)
214     {
215       vec_free (bif->per_thread_info[thread_index].frame);
216     }
217   vec_free (bif->per_thread_info);
218   memset (bif, 0, sizeof (*bif));
219   pool_put (bm->interfaces, bif);
220
221   return 0;
222 }
223
224 void
225 bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args)
226 {
227   bond_main_t *bm = &bond_main;
228   vnet_main_t *vnm = vnet_get_main ();
229   vnet_sw_interface_t *sw;
230   bond_if_t *bif;
231
232   if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0)
233     {
234       args->rv = VNET_API_ERROR_FEATURE_DISABLED;
235       args->error = clib_error_return (0, "LACP plugin is not loaded");
236       return;
237     }
238   if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN)
239     {
240       args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
241       args->error = clib_error_return (0, "Invalid mode");
242       return;
243     }
244   if (args->lb > BOND_LB_L23)
245     {
246       args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
247       args->error = clib_error_return (0, "Invalid load-balance");
248       return;
249     }
250   pool_get (bm->interfaces, bif);
251   memset (bif, 0, sizeof (*bif));
252   bif->dev_instance = bif - bm->interfaces;
253   bif->lb = args->lb;
254   bif->mode = args->mode;
255
256   // Special load-balance mode used for rr and bc
257   if (bif->mode == BOND_MODE_ROUND_ROBIN)
258     bif->lb = BOND_LB_RR;
259   else if (bif->mode == BOND_MODE_BROADCAST)
260     bif->lb = BOND_LB_BC;
261
262   bif->use_custom_mac = args->hw_addr_set;
263   if (!args->hw_addr_set)
264     {
265       f64 now = vlib_time_now (vm);
266       u32 rnd;
267       rnd = (u32) (now * 1e6);
268       rnd = random_u32 (&rnd);
269
270       memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
271       args->hw_addr[0] = 2;
272       args->hw_addr[1] = 0xfe;
273     }
274   memcpy (bif->hw_address, args->hw_addr, 6);
275   args->error = ethernet_register_interface
276     (vnm, bond_dev_class.index, bif - bm->interfaces /* device instance */ ,
277      bif->hw_address /* ethernet address */ ,
278      &bif->hw_if_index, 0 /* flag change */ );
279
280   if (args->error)
281     {
282       args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
283       pool_put (bm->interfaces, bif);
284       return;
285     }
286
287   sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
288   bif->sw_if_index = sw->sw_if_index;
289   bif->group = bif->sw_if_index;
290   vec_validate_aligned (bif->per_thread_info,
291                         vlib_get_thread_main ()->n_vlib_mains - 1,
292                         CLIB_CACHE_LINE_BYTES);
293   if (vlib_get_thread_main ()->n_vlib_mains > 1)
294     clib_spinlock_init (&bif->lockp);
295
296   vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
297                                VNET_HW_INTERFACE_FLAG_LINK_UP);
298
299   hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
300
301   // for return
302   args->sw_if_index = bif->sw_if_index;
303 }
304
305 static clib_error_t *
306 bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
307                         vlib_cli_command_t * cmd)
308 {
309   unformat_input_t _line_input, *line_input = &_line_input;
310   bond_create_if_args_t args = { 0 };
311   u8 mode_is_set = 0;
312
313   /* Get a line of input. */
314   if (!unformat_user (input, unformat_line_input, line_input))
315     return clib_error_return (0, "Missing required arguments.");
316
317   args.mode = -1;
318   args.lb = BOND_LB_L2;
319   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
320     {
321       if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
322         mode_is_set = 1;
323       else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
324                && unformat (line_input, "load-balance %U",
325                             unformat_bond_load_balance, &args.lb))
326         ;
327       else if (unformat (line_input, "hw-addr %U",
328                          unformat_ethernet_address, args.hw_addr))
329         args.hw_addr_set = 1;
330       else
331         return clib_error_return (0, "unknown input `%U'",
332                                   format_unformat_error, input);
333     }
334   unformat_free (line_input);
335
336   if (mode_is_set == 0)
337     return clib_error_return (0, "Missing bond mode");
338
339   bond_create_if (vm, &args);
340
341   return args.error;
342 }
343
344 /* *INDENT-OFF* */
345 VLIB_CLI_COMMAND (bond_create_command, static) = {
346   .path = "create bond",
347   .short_help = "create bond mode {round-robin | active-backup | broadcast | "
348     "{lacp | xor} [load-balance { l2 | l23 | l34 }]} [hw-addr <mac-address>]",
349   .function = bond_create_command_fn,
350 };
351 /* *INDENT-ON* */
352
353 static clib_error_t *
354 bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
355                         vlib_cli_command_t * cmd)
356 {
357   unformat_input_t _line_input, *line_input = &_line_input;
358   u32 sw_if_index = ~0;
359   vnet_main_t *vnm = vnet_get_main ();
360   int rv;
361
362   /* Get a line of input. */
363   if (!unformat_user (input, unformat_line_input, line_input))
364     return clib_error_return (0, "Missing <interface>");
365
366   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
367     {
368       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
369         ;
370       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
371                          vnm, &sw_if_index))
372         ;
373       else
374         return clib_error_return (0, "unknown input `%U'",
375                                   format_unformat_error, input);
376     }
377   unformat_free (line_input);
378
379   if (sw_if_index == ~0)
380     return clib_error_return (0,
381                               "please specify interface name or sw_if_index");
382
383   rv = bond_delete_if (vm, sw_if_index);
384   if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
385     return clib_error_return (0, "not a bond interface");
386   else if (rv != 0)
387     return clib_error_return (0, "error on deleting bond interface");
388
389   return 0;
390 }
391
392 /* *INDENT-OFF* */
393 VLIB_CLI_COMMAND (bond_delete__command, static) =
394 {
395   .path = "delete bond",
396   .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
397   .function = bond_delete_command_fn,
398 };
399 /* *INDENT-ON* */
400
401 void
402 bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args)
403 {
404   bond_main_t *bm = &bond_main;
405   vnet_main_t *vnm = vnet_get_main ();
406   bond_if_t *bif;
407   slave_if_t *sif;
408   vnet_interface_main_t *im = &vnm->interface_main;
409   vnet_hw_interface_t *hw, *hw2;
410   vnet_sw_interface_t *sw;
411
412   bif = bond_get_master_by_sw_if_index (args->group);
413   if (!bif)
414     {
415       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
416       args->error = clib_error_return (0, "bond interface not found");
417       return;
418     }
419   // make sure the interface is not already enslaved
420   if (bond_get_slave_by_sw_if_index (args->slave))
421     {
422       args->rv = VNET_API_ERROR_VALUE_EXIST;
423       args->error = clib_error_return (0, "interface was already enslaved");
424       return;
425     }
426   hw = vnet_get_sup_hw_interface (vnm, args->slave);
427   if (hw->dev_class_index == bond_dev_class.index)
428     {
429       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
430       args->error =
431         clib_error_return (0, "bond interface cannot be enslaved");
432       return;
433     }
434   pool_get (bm->neighbors, sif);
435   memset (sif, 0, sizeof (*sif));
436   clib_spinlock_init (&sif->lockp);
437   sw = pool_elt_at_index (im->sw_interfaces, args->slave);
438   sif->port_enabled = sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP;
439   sif->sw_if_index = sw->sw_if_index;
440   sif->hw_if_index = sw->hw_if_index;
441   sif->packet_template_index = (u8) ~ 0;
442   sif->is_passive = args->is_passive;
443   sif->group = args->group;
444   sif->bif_dev_instance = bif->dev_instance;
445   sif->mode = bif->mode;
446
447   sif->is_long_timeout = args->is_long_timeout;
448   if (args->is_long_timeout)
449     sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
450   else
451     sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
452
453   hash_set (bm->neighbor_by_sw_if_index, sif->sw_if_index,
454             sif - bm->neighbors);
455   vec_add1 (bif->slaves, sif->sw_if_index);
456
457   hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
458   /* Save the old mac */
459   memcpy (sif->persistent_hw_address, hw->hw_address, 6);
460   if (bif->use_custom_mac)
461     {
462       vnet_hw_interface_change_mac_address (vnm, hw->hw_if_index,
463                                             bif->hw_address);
464     }
465   else
466     {
467       // bond interface gets the mac address from the first slave
468       if (vec_len (bif->slaves) == 1)
469         {
470           memcpy (bif->hw_address, hw->hw_address, 6);
471           hw2 = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
472           vnet_hw_interface_change_mac_address (vnm, hw2->hw_if_index,
473                                                 hw->hw_address);
474         }
475       else
476         {
477           // subsequent slaves gets the mac address of the bond interface
478           vnet_hw_interface_change_mac_address (vnm, hw->hw_if_index,
479                                                 bif->hw_address);
480         }
481     }
482
483   if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
484     {
485       (*bm->lacp_enable_disable) (vm, bif, sif, 1);
486     }
487   else
488     {
489       bond_enable_collecting_distributing (vm, sif);
490     }
491
492   args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
493                                           hw->hw_if_index, 1, 0, 0);
494
495   if (args->rv)
496     {
497       args->error =
498         clib_error_return (0,
499                            "Error encountered on input feature arc enable");
500     }
501 }
502
503 static clib_error_t *
504 enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
505                               vlib_cli_command_t * cmd)
506 {
507   bond_enslave_args_t args = { 0 };
508   unformat_input_t _line_input, *line_input = &_line_input;
509   vnet_main_t *vnm = vnet_get_main ();
510
511   /* Get a line of input. */
512   if (!unformat_user (input, unformat_line_input, line_input))
513     return clib_error_return (0, "Missing required arguments.");
514
515   args.slave = ~0;
516   args.group = ~0;
517   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
518     {
519       if (unformat (line_input, "%U %U",
520                     unformat_vnet_sw_interface, vnm, &args.group,
521                     unformat_vnet_sw_interface, vnm, &args.slave))
522         ;
523       else if (unformat (line_input, "passive"))
524         args.is_passive = 1;
525       else if (unformat (line_input, "long-timeout"))
526         args.is_long_timeout = 1;
527       else
528         {
529           args.error = clib_error_return (0, "unknown input `%U'",
530                                           format_unformat_error, input);
531           break;
532         }
533     }
534   unformat_free (line_input);
535
536   if (args.error)
537     return args.error;
538   if (args.group == ~0)
539     return clib_error_return (0, "Missing bond interface");
540   if (args.slave == ~0)
541     return clib_error_return (0, "please specify valid slave interface name");
542
543   bond_enslave (vm, &args);
544
545   return args.error;
546 }
547
548 /* *INDENT-OFF* */
549 VLIB_CLI_COMMAND (enslave_interface_command, static) = {
550   .path = "bond add",
551   .short_help = "bond add <BondEthernetx> <slave-interface> "
552                 "[passive] [long-timeout]",
553   .function = enslave_interface_command_fn,
554 };
555 /* *INDENT-ON* */
556
557 void
558 bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args)
559 {
560   bond_if_t *bif;
561   slave_if_t *sif;
562
563   sif = bond_get_slave_by_sw_if_index (args->slave);
564   if (!sif)
565     {
566       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
567       args->error = clib_error_return (0, "interface was not enslaved");
568       return;
569     }
570   bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
571   bond_delete_neighbor (vm, bif, sif);
572 }
573
574 static clib_error_t *
575 detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
576                              vlib_cli_command_t * cmd)
577 {
578   bond_detach_slave_args_t args = { 0 };
579   unformat_input_t _line_input, *line_input = &_line_input;
580   vnet_main_t *vnm = vnet_get_main ();
581
582   /* Get a line of input. */
583   if (!unformat_user (input, unformat_line_input, line_input))
584     return clib_error_return (0, "Missing required arguments.");
585
586   args.slave = ~0;
587   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
588     {
589       if (unformat (line_input, "%U",
590                     unformat_vnet_sw_interface, vnm, &args.slave))
591         ;
592       else
593         {
594           args.error = clib_error_return (0, "unknown input `%U'",
595                                           format_unformat_error, input);
596           break;
597         }
598     }
599   unformat_free (line_input);
600
601   if (args.error)
602     return args.error;
603   if (args.slave == ~0)
604     return clib_error_return (0, "please specify valid slave interface name");
605
606   bond_detach_slave (vm, &args);
607
608   return args.error;
609 }
610
611 /* *INDENT-OFF* */
612 VLIB_CLI_COMMAND (detach_interface_command, static) = {
613   .path = "bond del",
614   .short_help = "bond del <slave-interface>",
615   .function = detach_interface_command_fn,
616 };
617 /* *INDENT-ON* */
618
619 static void
620 show_bond (vlib_main_t * vm)
621 {
622   bond_main_t *bm = &bond_main;
623   bond_if_t *bif;
624
625   vlib_cli_output (vm, "%-16s %-12s %-12s %-13s %-14s %s",
626                    "interface name", "sw_if_index", "mode",
627                    "load balance", "active slaves", "slaves");
628
629   /* *INDENT-OFF* */
630   pool_foreach (bif, bm->interfaces,
631   ({
632     vlib_cli_output (vm, "%-16U %-12d %-12U %-13U %-14u %u",
633                      format_bond_interface_name, bif->dev_instance,
634                      bif->sw_if_index, format_bond_mode, bif->mode,
635                      format_bond_load_balance, bif->lb,
636                      vec_len (bif->active_slaves), vec_len (bif->slaves));
637   }));
638   /* *INDENT-ON* */
639 }
640
641 static void
642 show_bond_details (vlib_main_t * vm)
643 {
644   bond_main_t *bm = &bond_main;
645   bond_if_t *bif;
646   u32 *sw_if_index;
647
648   /* *INDENT-OFF* */
649   pool_foreach (bif, bm->interfaces,
650   ({
651     vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
652     vlib_cli_output (vm, "  mode: %U",
653                      format_bond_mode, bif->mode);
654     vlib_cli_output (vm, "  load balance: %U",
655                      format_bond_load_balance, bif->lb);
656     if (bif->mode == BOND_MODE_ROUND_ROBIN)
657       vlib_cli_output (vm, "  last xmit slave index: %u",
658                        bif->lb_rr_last_index);
659     vlib_cli_output (vm, "  number of active slaves: %d",
660                      vec_len (bif->active_slaves));
661     vec_foreach (sw_if_index, bif->active_slaves)
662       {
663         vlib_cli_output (vm, "    %U", format_vnet_sw_if_index_name,
664                          vnet_get_main (), *sw_if_index);
665       }
666     vlib_cli_output (vm, "  number of slaves: %d", vec_len (bif->slaves));
667     vec_foreach (sw_if_index, bif->slaves)
668       {
669         vlib_cli_output (vm, "    %U", format_vnet_sw_if_index_name,
670                          vnet_get_main (), *sw_if_index);
671       }
672     vlib_cli_output (vm, "  device instance: %d", bif->dev_instance);
673     vlib_cli_output (vm, "  sw_if_index: %d", bif->sw_if_index);
674     vlib_cli_output (vm, "  hw_if_index: %d", bif->hw_if_index);
675   }));
676   /* *INDENT-ON* */
677 }
678
679 static clib_error_t *
680 show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
681               vlib_cli_command_t * cmd)
682 {
683   u8 details = 0;
684
685   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
686     {
687       if (unformat (input, "details"))
688         details = 1;
689       else
690         {
691           return clib_error_return (0, "unknown input `%U'",
692                                     format_unformat_error, input);
693         }
694     }
695
696   if (details)
697     show_bond_details (vm);
698   else
699     show_bond (vm);
700
701   return 0;
702 }
703
704 /* *INDENT-OFF* */
705 VLIB_CLI_COMMAND (show_bond_command, static) = {
706   .path = "show bond",
707   .short_help = "show bond [details]",
708   .function = show_bond_fn,
709 };
710 /* *INDENT-ON* */
711
712 clib_error_t *
713 bond_cli_init (vlib_main_t * vm)
714 {
715   bond_main_t *bm = &bond_main;
716
717   bm->vlib_main = vm;
718   bm->vnet_main = vnet_get_main ();
719   bm->neighbor_by_sw_if_index = hash_create (0, sizeof (uword));
720
721   return 0;
722 }
723
724 VLIB_INIT_FUNCTION (bond_cli_init);
725
726 /*
727  * fd.io coding-style-patch-verification: ON
728  *
729  * Local Variables:
730  * eval: (c-set-style "gnu")
731  * End:
732  */