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