bond: packet drops on VPP bond interface [VPP-1544]
[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)
535     {
536       if (bm->lacp_enable_disable)
537         (*bm->lacp_enable_disable) (vm, bif, sif, 1);
538     }
539   else if (sif->port_enabled &&
540            (sif_hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
541     {
542       bond_enable_collecting_distributing (vm, sif);
543     }
544
545   vec_foreach_index (thread_index, bm->per_thread_data)
546   {
547     bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data,
548                                                     thread_index);
549
550     vec_validate_aligned (ptd->per_port_queue, vec_len (bif->slaves) - 1,
551                           CLIB_CACHE_LINE_BYTES);
552
553     vec_foreach_index (sif_if_index, ptd->per_port_queue)
554     {
555       ptd->per_port_queue[sif_if_index].n_buffers = 0;
556     }
557   }
558
559   args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
560                                           sif_hw->hw_if_index, 1, 0, 0);
561
562   if (args->rv)
563     {
564       args->error =
565         clib_error_return (0,
566                            "Error encountered on input feature arc enable");
567     }
568 }
569
570 static clib_error_t *
571 enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
572                               vlib_cli_command_t * cmd)
573 {
574   bond_enslave_args_t args = { 0 };
575   unformat_input_t _line_input, *line_input = &_line_input;
576   vnet_main_t *vnm = vnet_get_main ();
577
578   /* Get a line of input. */
579   if (!unformat_user (input, unformat_line_input, line_input))
580     return clib_error_return (0, "Missing required arguments.");
581
582   args.slave = ~0;
583   args.group = ~0;
584   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
585     {
586       if (unformat (line_input, "%U %U",
587                     unformat_vnet_sw_interface, vnm, &args.group,
588                     unformat_vnet_sw_interface, vnm, &args.slave))
589         ;
590       else if (unformat (line_input, "passive"))
591         args.is_passive = 1;
592       else if (unformat (line_input, "long-timeout"))
593         args.is_long_timeout = 1;
594       else
595         {
596           args.error = clib_error_return (0, "unknown input `%U'",
597                                           format_unformat_error, input);
598           break;
599         }
600     }
601   unformat_free (line_input);
602
603   if (args.error)
604     return args.error;
605   if (args.group == ~0)
606     return clib_error_return (0, "Missing bond interface");
607   if (args.slave == ~0)
608     return clib_error_return (0, "please specify valid slave interface name");
609
610   bond_enslave (vm, &args);
611
612   return args.error;
613 }
614
615 /* *INDENT-OFF* */
616 VLIB_CLI_COMMAND (enslave_interface_command, static) = {
617   .path = "bond add",
618   .short_help = "bond add <BondEthernetx> <slave-interface> "
619                 "[passive] [long-timeout]",
620   .function = enslave_interface_command_fn,
621 };
622 /* *INDENT-ON* */
623
624 void
625 bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args)
626 {
627   bond_if_t *bif;
628   slave_if_t *sif;
629
630   sif = bond_get_slave_by_sw_if_index (args->slave);
631   if (!sif)
632     {
633       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
634       args->error = clib_error_return (0, "interface was not enslaved");
635       return;
636     }
637   bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
638   bond_delete_neighbor (vm, bif, sif);
639 }
640
641 static clib_error_t *
642 detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
643                              vlib_cli_command_t * cmd)
644 {
645   bond_detach_slave_args_t args = { 0 };
646   unformat_input_t _line_input, *line_input = &_line_input;
647   vnet_main_t *vnm = vnet_get_main ();
648
649   /* Get a line of input. */
650   if (!unformat_user (input, unformat_line_input, line_input))
651     return clib_error_return (0, "Missing required arguments.");
652
653   args.slave = ~0;
654   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
655     {
656       if (unformat (line_input, "%U",
657                     unformat_vnet_sw_interface, vnm, &args.slave))
658         ;
659       else
660         {
661           args.error = clib_error_return (0, "unknown input `%U'",
662                                           format_unformat_error, input);
663           break;
664         }
665     }
666   unformat_free (line_input);
667
668   if (args.error)
669     return args.error;
670   if (args.slave == ~0)
671     return clib_error_return (0, "please specify valid slave interface name");
672
673   bond_detach_slave (vm, &args);
674
675   return args.error;
676 }
677
678 /* *INDENT-OFF* */
679 VLIB_CLI_COMMAND (detach_interface_command, static) = {
680   .path = "bond del",
681   .short_help = "bond del <slave-interface>",
682   .function = detach_interface_command_fn,
683 };
684 /* *INDENT-ON* */
685
686 static void
687 show_bond (vlib_main_t * vm)
688 {
689   bond_main_t *bm = &bond_main;
690   bond_if_t *bif;
691
692   vlib_cli_output (vm, "%-16s %-12s %-13s %-13s %-14s %s",
693                    "interface name", "sw_if_index", "mode",
694                    "load balance", "active slaves", "slaves");
695
696   /* *INDENT-OFF* */
697   pool_foreach (bif, bm->interfaces,
698   ({
699     vlib_cli_output (vm, "%-16U %-12d %-13U %-13U %-14u %u",
700                      format_bond_interface_name, bif->dev_instance,
701                      bif->sw_if_index, format_bond_mode, bif->mode,
702                      format_bond_load_balance, bif->lb,
703                      vec_len (bif->active_slaves), vec_len (bif->slaves));
704   }));
705   /* *INDENT-ON* */
706 }
707
708 static void
709 show_bond_details (vlib_main_t * vm)
710 {
711   bond_main_t *bm = &bond_main;
712   bond_if_t *bif;
713   u32 *sw_if_index;
714
715   /* *INDENT-OFF* */
716   pool_foreach (bif, bm->interfaces,
717   ({
718     vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
719     vlib_cli_output (vm, "  mode: %U",
720                      format_bond_mode, bif->mode);
721     vlib_cli_output (vm, "  load balance: %U",
722                      format_bond_load_balance, bif->lb);
723     if (bif->mode == BOND_MODE_ROUND_ROBIN)
724       vlib_cli_output (vm, "  last xmit slave index: %u",
725                        bif->lb_rr_last_index);
726     vlib_cli_output (vm, "  number of active slaves: %d",
727                      vec_len (bif->active_slaves));
728     vec_foreach (sw_if_index, bif->active_slaves)
729       {
730         vlib_cli_output (vm, "    %U", format_vnet_sw_if_index_name,
731                          vnet_get_main (), *sw_if_index);
732       }
733     vlib_cli_output (vm, "  number of slaves: %d", vec_len (bif->slaves));
734     vec_foreach (sw_if_index, bif->slaves)
735       {
736         vlib_cli_output (vm, "    %U", format_vnet_sw_if_index_name,
737                          vnet_get_main (), *sw_if_index);
738       }
739     vlib_cli_output (vm, "  device instance: %d", bif->dev_instance);
740     vlib_cli_output (vm, "  interface id: %d", bif->id);
741     vlib_cli_output (vm, "  sw_if_index: %d", bif->sw_if_index);
742     vlib_cli_output (vm, "  hw_if_index: %d", bif->hw_if_index);
743   }));
744   /* *INDENT-ON* */
745 }
746
747 static clib_error_t *
748 show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
749               vlib_cli_command_t * cmd)
750 {
751   u8 details = 0;
752
753   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
754     {
755       if (unformat (input, "details"))
756         details = 1;
757       else
758         {
759           return clib_error_return (0, "unknown input `%U'",
760                                     format_unformat_error, input);
761         }
762     }
763
764   if (details)
765     show_bond_details (vm);
766   else
767     show_bond (vm);
768
769   return 0;
770 }
771
772 /* *INDENT-OFF* */
773 VLIB_CLI_COMMAND (show_bond_command, static) = {
774   .path = "show bond",
775   .short_help = "show bond [details]",
776   .function = show_bond_fn,
777 };
778 /* *INDENT-ON* */
779
780 clib_error_t *
781 bond_cli_init (vlib_main_t * vm)
782 {
783   bond_main_t *bm = &bond_main;
784
785   bm->vlib_main = vm;
786   bm->vnet_main = vnet_get_main ();
787   vec_validate_aligned (bm->slave_by_sw_if_index, 1, CLIB_CACHE_LINE_BYTES);
788   vec_validate_aligned (bm->per_thread_data,
789                         vlib_get_thread_main ()->n_vlib_mains - 1,
790                         CLIB_CACHE_LINE_BYTES);
791
792   return 0;
793 }
794
795 VLIB_INIT_FUNCTION (bond_cli_init);
796
797 /*
798  * fd.io coding-style-patch-verification: ON
799  *
800  * Local Variables:
801  * eval: (c-set-style "gnu")
802  * End:
803  */