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