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