bond: tx perf improvements
[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   sw = pool_elt_at_index (im->sw_interfaces, args->slave);
460   sif->port_enabled = sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP;
461   sif->sw_if_index = sw->sw_if_index;
462   sif->hw_if_index = sw->hw_if_index;
463   sif->packet_template_index = (u8) ~ 0;
464   sif->is_passive = args->is_passive;
465   sif->group = args->group;
466   sif->bif_dev_instance = bif->dev_instance;
467   sif->mode = bif->mode;
468
469   sif->is_long_timeout = args->is_long_timeout;
470   if (args->is_long_timeout)
471     sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
472   else
473     sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
474
475   vec_validate_aligned (bm->slave_by_sw_if_index, sif->sw_if_index,
476                         CLIB_CACHE_LINE_BYTES);
477   /*
478    * sif - bm->neighbors may be 0
479    * Left shift it by 1 bit to distinguish the valid entry that we actually
480    * store from the null entries
481    */
482   bm->slave_by_sw_if_index[sif->sw_if_index] =
483     (uword) (((sif - bm->neighbors) << 1) | 1);
484   vec_add1 (bif->slaves, sif->sw_if_index);
485
486   sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
487
488   /* Save the old mac */
489   memcpy (sif->persistent_hw_address, sif_hw->hw_address, 6);
490   bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
491   if (bif->use_custom_mac)
492     {
493       vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
494                                             bif->hw_address);
495     }
496   else
497     {
498       // bond interface gets the mac address from the first slave
499       if (vec_len (bif->slaves) == 1)
500         {
501           memcpy (bif->hw_address, sif_hw->hw_address, 6);
502           vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index,
503                                                 sif_hw->hw_address);
504         }
505       else
506         {
507           // subsequent slaves gets the mac address of the bond interface
508           vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
509                                                 bif->hw_address);
510         }
511     }
512
513   if (bif_hw->l2_if_count)
514     {
515       ethernet_set_flags (vnm, sif_hw->hw_if_index,
516                           ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
517       /* ensure all packets go to ethernet-input */
518       ethernet_set_rx_redirect (vnm, sif_hw, 1);
519     }
520
521   if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
522     {
523       (*bm->lacp_enable_disable) (vm, bif, sif, 1);
524     }
525   else
526     {
527       bond_enable_collecting_distributing (vm, sif);
528     }
529
530   args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
531                                           sif_hw->hw_if_index, 1, 0, 0);
532
533   if (args->rv)
534     {
535       args->error =
536         clib_error_return (0,
537                            "Error encountered on input feature arc enable");
538     }
539 }
540
541 static clib_error_t *
542 enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
543                               vlib_cli_command_t * cmd)
544 {
545   bond_enslave_args_t args = { 0 };
546   unformat_input_t _line_input, *line_input = &_line_input;
547   vnet_main_t *vnm = vnet_get_main ();
548
549   /* Get a line of input. */
550   if (!unformat_user (input, unformat_line_input, line_input))
551     return clib_error_return (0, "Missing required arguments.");
552
553   args.slave = ~0;
554   args.group = ~0;
555   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
556     {
557       if (unformat (line_input, "%U %U",
558                     unformat_vnet_sw_interface, vnm, &args.group,
559                     unformat_vnet_sw_interface, vnm, &args.slave))
560         ;
561       else if (unformat (line_input, "passive"))
562         args.is_passive = 1;
563       else if (unformat (line_input, "long-timeout"))
564         args.is_long_timeout = 1;
565       else
566         {
567           args.error = clib_error_return (0, "unknown input `%U'",
568                                           format_unformat_error, input);
569           break;
570         }
571     }
572   unformat_free (line_input);
573
574   if (args.error)
575     return args.error;
576   if (args.group == ~0)
577     return clib_error_return (0, "Missing bond interface");
578   if (args.slave == ~0)
579     return clib_error_return (0, "please specify valid slave interface name");
580
581   bond_enslave (vm, &args);
582
583   return args.error;
584 }
585
586 /* *INDENT-OFF* */
587 VLIB_CLI_COMMAND (enslave_interface_command, static) = {
588   .path = "bond add",
589   .short_help = "bond add <BondEthernetx> <slave-interface> "
590                 "[passive] [long-timeout]",
591   .function = enslave_interface_command_fn,
592 };
593 /* *INDENT-ON* */
594
595 void
596 bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args)
597 {
598   bond_if_t *bif;
599   slave_if_t *sif;
600
601   sif = bond_get_slave_by_sw_if_index (args->slave);
602   if (!sif)
603     {
604       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
605       args->error = clib_error_return (0, "interface was not enslaved");
606       return;
607     }
608   bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
609   bond_delete_neighbor (vm, bif, sif);
610 }
611
612 static clib_error_t *
613 detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
614                              vlib_cli_command_t * cmd)
615 {
616   bond_detach_slave_args_t args = { 0 };
617   unformat_input_t _line_input, *line_input = &_line_input;
618   vnet_main_t *vnm = vnet_get_main ();
619
620   /* Get a line of input. */
621   if (!unformat_user (input, unformat_line_input, line_input))
622     return clib_error_return (0, "Missing required arguments.");
623
624   args.slave = ~0;
625   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
626     {
627       if (unformat (line_input, "%U",
628                     unformat_vnet_sw_interface, vnm, &args.slave))
629         ;
630       else
631         {
632           args.error = clib_error_return (0, "unknown input `%U'",
633                                           format_unformat_error, input);
634           break;
635         }
636     }
637   unformat_free (line_input);
638
639   if (args.error)
640     return args.error;
641   if (args.slave == ~0)
642     return clib_error_return (0, "please specify valid slave interface name");
643
644   bond_detach_slave (vm, &args);
645
646   return args.error;
647 }
648
649 /* *INDENT-OFF* */
650 VLIB_CLI_COMMAND (detach_interface_command, static) = {
651   .path = "bond del",
652   .short_help = "bond del <slave-interface>",
653   .function = detach_interface_command_fn,
654 };
655 /* *INDENT-ON* */
656
657 static void
658 show_bond (vlib_main_t * vm)
659 {
660   bond_main_t *bm = &bond_main;
661   bond_if_t *bif;
662
663   vlib_cli_output (vm, "%-16s %-12s %-12s %-13s %-14s %s",
664                    "interface name", "sw_if_index", "mode",
665                    "load balance", "active slaves", "slaves");
666
667   /* *INDENT-OFF* */
668   pool_foreach (bif, bm->interfaces,
669   ({
670     vlib_cli_output (vm, "%-16U %-12d %-12U %-13U %-14u %u",
671                      format_bond_interface_name, bif->dev_instance,
672                      bif->sw_if_index, format_bond_mode, bif->mode,
673                      format_bond_load_balance, bif->lb,
674                      vec_len (bif->active_slaves), vec_len (bif->slaves));
675   }));
676   /* *INDENT-ON* */
677 }
678
679 static void
680 show_bond_details (vlib_main_t * vm)
681 {
682   bond_main_t *bm = &bond_main;
683   bond_if_t *bif;
684   u32 *sw_if_index;
685
686   /* *INDENT-OFF* */
687   pool_foreach (bif, bm->interfaces,
688   ({
689     vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
690     vlib_cli_output (vm, "  mode: %U",
691                      format_bond_mode, bif->mode);
692     vlib_cli_output (vm, "  load balance: %U",
693                      format_bond_load_balance, bif->lb);
694     if (bif->mode == BOND_MODE_ROUND_ROBIN)
695       vlib_cli_output (vm, "  last xmit slave index: %u",
696                        bif->lb_rr_last_index);
697     vlib_cli_output (vm, "  number of active slaves: %d",
698                      vec_len (bif->active_slaves));
699     vec_foreach (sw_if_index, bif->active_slaves)
700       {
701         vlib_cli_output (vm, "    %U", format_vnet_sw_if_index_name,
702                          vnet_get_main (), *sw_if_index);
703       }
704     vlib_cli_output (vm, "  number of slaves: %d", vec_len (bif->slaves));
705     vec_foreach (sw_if_index, bif->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, "  device instance: %d", bif->dev_instance);
711     vlib_cli_output (vm, "  sw_if_index: %d", bif->sw_if_index);
712     vlib_cli_output (vm, "  hw_if_index: %d", bif->hw_if_index);
713   }));
714   /* *INDENT-ON* */
715 }
716
717 static clib_error_t *
718 show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
719               vlib_cli_command_t * cmd)
720 {
721   u8 details = 0;
722
723   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
724     {
725       if (unformat (input, "details"))
726         details = 1;
727       else
728         {
729           return clib_error_return (0, "unknown input `%U'",
730                                     format_unformat_error, input);
731         }
732     }
733
734   if (details)
735     show_bond_details (vm);
736   else
737     show_bond (vm);
738
739   return 0;
740 }
741
742 /* *INDENT-OFF* */
743 VLIB_CLI_COMMAND (show_bond_command, static) = {
744   .path = "show bond",
745   .short_help = "show bond [details]",
746   .function = show_bond_fn,
747 };
748 /* *INDENT-ON* */
749
750 clib_error_t *
751 bond_cli_init (vlib_main_t * vm)
752 {
753   bond_main_t *bm = &bond_main;
754
755   bm->vlib_main = vm;
756   bm->vnet_main = vnet_get_main ();
757   vec_validate_aligned (bm->slave_by_sw_if_index, 1, CLIB_CACHE_LINE_BYTES);
758
759   return 0;
760 }
761
762 VLIB_INIT_FUNCTION (bond_cli_init);
763
764 /*
765  * fd.io coding-style-patch-verification: ON
766  *
767  * Local Variables:
768  * eval: (c-set-style "gnu")
769  * End:
770  */