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