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