bonding: adjust link state based on active slaves
[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 #include <vpp/stats/stat_segment.h>
24
25 void
26 bond_disable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
27 {
28   bond_main_t *bm = &bond_main;
29   vnet_main_t *vnm = vnet_get_main ();
30   bond_if_t *bif;
31   int i;
32   uword p;
33   u8 switching_active = 0;
34
35   bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
36   clib_spinlock_lock_if_init (&bif->lockp);
37   vec_foreach_index (i, bif->active_slaves)
38   {
39     p = *vec_elt_at_index (bif->active_slaves, i);
40     if (p == sif->sw_if_index)
41       {
42         if ((bif->mode == BOND_MODE_ACTIVE_BACKUP) && (i == 0) &&
43             (vec_len (bif->active_slaves) > 1))
44           /* deleting the active slave for active-backup */
45           switching_active = 1;
46         vec_del1 (bif->active_slaves, i);
47         if (sif->lacp_enabled && bif->numa_only)
48           {
49             /* For lacp mode, if we check it is a slave on local numa node,
50                bif->n_numa_slaves should be decreased by 1 becasue the first
51                bif->n_numa_slaves are all slaves on local numa node */
52             if (i < bif->n_numa_slaves)
53               {
54                 bif->n_numa_slaves--;
55                 ASSERT (bif->n_numa_slaves >= 0);
56               }
57           }
58         /* If that was the last active slave, set bond link state down */
59         if (!vec_len (bif->active_slaves))
60           {
61             vnet_hw_interface_flags_t flags;
62
63             flags = vnet_hw_interface_get_flags (vnm, bif->hw_if_index);
64             flags &= ~VNET_HW_INTERFACE_FLAG_LINK_UP;
65             vnet_hw_interface_set_flags (vnm, bif->hw_if_index, flags);
66           }
67         break;
68       }
69   }
70
71   /* We get a new slave just becoming active */
72   if (switching_active)
73     vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
74                                BOND_SEND_GARP_NA, bif->hw_if_index);
75   clib_spinlock_unlock_if_init (&bif->lockp);
76 }
77
78 /*
79  * return 1 if s2 is preferred.
80  * return -1 if s1 is preferred.
81  */
82 static int
83 bond_slave_sort (void *a1, void *a2)
84 {
85   u32 *s1 = a1;
86   u32 *s2 = a2;
87   slave_if_t *sif1 = bond_get_slave_by_sw_if_index (*s1);
88   slave_if_t *sif2 = bond_get_slave_by_sw_if_index (*s2);
89   bond_if_t *bif;
90
91   ALWAYS_ASSERT (sif1);
92   ALWAYS_ASSERT (sif2);
93   /*
94    * sort entries according to preference rules:
95    * 1. biggest weight
96    * 2. numa-node
97    * 3. current active slave (to prevent churning)
98    * 4. lowest sw_if_index (for deterministic behavior)
99    *
100    */
101   if (sif2->weight > sif1->weight)
102     return 1;
103   if (sif2->weight < sif1->weight)
104     return -1;
105   else
106     {
107       if (sif2->is_local_numa > sif1->is_local_numa)
108         return 1;
109       if (sif2->is_local_numa < sif1->is_local_numa)
110         return -1;
111       else
112         {
113           bif = bond_get_master_by_dev_instance (sif1->bif_dev_instance);
114           /* Favor the current active slave to avoid churning */
115           if (bif->active_slaves[0] == sif2->sw_if_index)
116             return 1;
117           if (bif->active_slaves[0] == sif1->sw_if_index)
118             return -1;
119           /* go for the tiebreaker as the last resort */
120           if (sif1->sw_if_index > sif2->sw_if_index)
121             return 1;
122           if (sif1->sw_if_index < sif2->sw_if_index)
123             return -1;
124           else
125             ASSERT (0);
126         }
127     }
128   return 0;
129 }
130
131 static void
132 bond_sort_slaves (bond_if_t * bif)
133 {
134   bond_main_t *bm = &bond_main;
135   u32 old_active = bif->active_slaves[0];
136
137   vec_sort_with_function (bif->active_slaves, bond_slave_sort);
138   if (old_active != bif->active_slaves[0])
139     vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
140                                BOND_SEND_GARP_NA, bif->hw_if_index);
141 }
142
143 void
144 bond_enable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif)
145 {
146   bond_if_t *bif;
147   bond_main_t *bm = &bond_main;
148   vnet_main_t *vnm = vnet_get_main ();
149   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
150   int i;
151   uword p;
152
153   bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
154   clib_spinlock_lock_if_init (&bif->lockp);
155   vec_foreach_index (i, bif->active_slaves)
156   {
157     p = *vec_elt_at_index (bif->active_slaves, i);
158     if (p == sif->sw_if_index)
159       goto done;
160   }
161
162   if (sif->lacp_enabled && bif->numa_only && (vm->numa_node == hw->numa_node))
163     {
164       vec_insert_elts (bif->active_slaves, &sif->sw_if_index, 1,
165                        bif->n_numa_slaves);
166       bif->n_numa_slaves++;
167     }
168   else
169     vec_add1 (bif->active_slaves, sif->sw_if_index);
170
171   /* If this was the first active slave, set bond link state up */
172   if (vec_len (bif->active_slaves) == 1)
173     {
174       vnet_hw_interface_flags_t flags;
175
176       flags = vnet_hw_interface_get_flags (vnm, bif->hw_if_index);
177       flags |= VNET_HW_INTERFACE_FLAG_LINK_UP;
178       vnet_hw_interface_set_flags (vnm, bif->hw_if_index, flags);
179     }
180
181   sif->is_local_numa = (vm->numa_node == hw->numa_node) ? 1 : 0;
182   if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
183     {
184       if (vec_len (bif->active_slaves) == 1)
185         /* First slave becomes active? */
186         vlib_process_signal_event (bm->vlib_main, bond_process_node.index,
187                                    BOND_SEND_GARP_NA, bif->hw_if_index);
188       else
189         bond_sort_slaves (bif);
190     }
191
192 done:
193   clib_spinlock_unlock_if_init (&bif->lockp);
194 }
195
196 int
197 bond_dump_ifs (bond_interface_details_t ** out_bondifs)
198 {
199   vnet_main_t *vnm = vnet_get_main ();
200   bond_main_t *bm = &bond_main;
201   bond_if_t *bif;
202   vnet_hw_interface_t *hi;
203   bond_interface_details_t *r_bondifs = NULL;
204   bond_interface_details_t *bondif = NULL;
205
206   /* *INDENT-OFF* */
207   pool_foreach (bif, bm->interfaces,
208     vec_add2(r_bondifs, bondif, 1);
209     clib_memset (bondif, 0, sizeof (*bondif));
210     bondif->id = bif->id;
211     bondif->sw_if_index = bif->sw_if_index;
212     hi = vnet_get_hw_interface (vnm, bif->hw_if_index);
213     clib_memcpy(bondif->interface_name, hi->name,
214                 MIN (ARRAY_LEN (bondif->interface_name) - 1,
215                      vec_len ((const char *) hi->name)));
216     /* enforce by memset() above */
217     ASSERT(0 == bondif->interface_name[ARRAY_LEN (bondif->interface_name) - 1]);
218     bondif->mode = bif->mode;
219     bondif->lb = bif->lb;
220     bondif->numa_only = bif->numa_only;
221     bondif->active_slaves = vec_len (bif->active_slaves);
222     bondif->slaves = vec_len (bif->slaves);
223   );
224   /* *INDENT-ON* */
225
226   *out_bondifs = r_bondifs;
227
228   return 0;
229 }
230
231 int
232 bond_dump_slave_ifs (slave_interface_details_t ** out_slaveifs,
233                      u32 bond_sw_if_index)
234 {
235   vnet_main_t *vnm = vnet_get_main ();
236   bond_if_t *bif;
237   vnet_hw_interface_t *hi;
238   vnet_sw_interface_t *sw;
239   slave_interface_details_t *r_slaveifs = NULL;
240   slave_interface_details_t *slaveif = NULL;
241   u32 *sw_if_index = NULL;
242   slave_if_t *sif;
243
244   bif = bond_get_master_by_sw_if_index (bond_sw_if_index);
245   if (!bif)
246     return 1;
247
248   vec_foreach (sw_if_index, bif->slaves)
249   {
250     vec_add2 (r_slaveifs, slaveif, 1);
251     clib_memset (slaveif, 0, sizeof (*slaveif));
252     sif = bond_get_slave_by_sw_if_index (*sw_if_index);
253     if (sif)
254       {
255         sw = vnet_get_sw_interface (vnm, sif->sw_if_index);
256         hi = vnet_get_hw_interface (vnm, sw->hw_if_index);
257         clib_memcpy (slaveif->interface_name, hi->name,
258                      MIN (ARRAY_LEN (slaveif->interface_name) - 1,
259                           vec_len ((const char *) hi->name)));
260         /* enforce by memset() above */
261         ASSERT (0 ==
262                 slaveif->interface_name[ARRAY_LEN (slaveif->interface_name) -
263                                         1]);
264         slaveif->sw_if_index = sif->sw_if_index;
265         slaveif->is_passive = sif->is_passive;
266         slaveif->is_long_timeout = sif->is_long_timeout;
267         slaveif->is_local_numa = sif->is_local_numa;
268         slaveif->weight = sif->weight;
269       }
270   }
271   *out_slaveifs = r_slaveifs;
272
273   return 0;
274 }
275
276 /*
277  * Manage secondary mac addresses when attaching/detaching a slave.
278  * If adding, copies any secondary addresses from master to slave
279  * If deleting, deletes the master's secondary addresses from the slave
280  *
281  */
282 static void
283 bond_slave_add_del_mac_addrs (bond_if_t * bif, u32 sif_sw_if_index, u8 is_add)
284 {
285   vnet_main_t *vnm = vnet_get_main ();
286   ethernet_interface_t *b_ei;
287   mac_address_t *sec_mac;
288   vnet_hw_interface_t *s_hwif;
289
290   b_ei = ethernet_get_interface (&ethernet_main, bif->hw_if_index);
291   if (!b_ei || !b_ei->secondary_addrs)
292     return;
293
294   s_hwif = vnet_get_sup_hw_interface (vnm, sif_sw_if_index);
295
296   vec_foreach (sec_mac, b_ei->secondary_addrs)
297     vnet_hw_interface_add_del_mac_address (vnm, s_hwif->hw_if_index,
298                                            sec_mac->bytes, is_add);
299 }
300
301 static void
302 bond_delete_neighbor (vlib_main_t * vm, bond_if_t * bif, slave_if_t * sif)
303 {
304   bond_main_t *bm = &bond_main;
305   vnet_main_t *vnm = vnet_get_main ();
306   int i;
307   vnet_hw_interface_t *sif_hw;
308
309   sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
310
311   bif->port_number_bitmap =
312     clib_bitmap_set (bif->port_number_bitmap,
313                      ntohs (sif->actor_admin.port_number) - 1, 0);
314   bm->slave_by_sw_if_index[sif->sw_if_index] = 0;
315   vec_free (sif->last_marker_pkt);
316   vec_free (sif->last_rx_pkt);
317   vec_foreach_index (i, bif->slaves)
318   {
319     uword p = *vec_elt_at_index (bif->slaves, i);
320     if (p == sif->sw_if_index)
321       {
322         vec_del1 (bif->slaves, i);
323         break;
324       }
325   }
326
327   bond_disable_collecting_distributing (vm, sif);
328
329   vnet_feature_enable_disable ("device-input", "bond-input",
330                                sif->sw_if_index, 0, 0, 0);
331
332   /* Put back the old mac */
333   vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
334                                         sif->persistent_hw_address);
335
336   /* delete the bond's secondary/virtual mac addrs from the slave */
337   bond_slave_add_del_mac_addrs (bif, sif->sw_if_index, 0 /* is_add */ );
338
339
340   if ((bif->mode == BOND_MODE_LACP) && bm->lacp_enable_disable)
341     (*bm->lacp_enable_disable) (vm, bif, sif, 0);
342
343   if (bif->mode == BOND_MODE_LACP)
344     {
345       stat_segment_deregister_state_counter
346         (bm->stats[bif->sw_if_index][sif->sw_if_index].actor_state);
347       stat_segment_deregister_state_counter
348         (bm->stats[bif->sw_if_index][sif->sw_if_index].partner_state);
349     }
350
351   pool_put (bm->neighbors, sif);
352 }
353
354 int
355 bond_delete_if (vlib_main_t * vm, u32 sw_if_index)
356 {
357   bond_main_t *bm = &bond_main;
358   vnet_main_t *vnm = vnet_get_main ();
359   bond_if_t *bif;
360   slave_if_t *sif;
361   vnet_hw_interface_t *hw;
362   u32 *sif_sw_if_index;
363   u32 *s_list = 0;
364
365   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
366   if (hw == NULL || bond_dev_class.index != hw->dev_class_index)
367     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
368
369   bif = bond_get_master_by_dev_instance (hw->dev_instance);
370
371   vec_append (s_list, bif->slaves);
372   vec_foreach (sif_sw_if_index, s_list)
373   {
374     sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index);
375     if (sif)
376       bond_delete_neighbor (vm, bif, sif);
377   }
378   vec_free (s_list);
379
380   /* bring down the interface */
381   vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0);
382   vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0);
383
384   ethernet_delete_interface (vnm, bif->hw_if_index);
385
386   clib_bitmap_free (bif->port_number_bitmap);
387   hash_unset (bm->bond_by_sw_if_index, bif->sw_if_index);
388   hash_unset (bm->id_used, bif->id);
389   clib_memset (bif, 0, sizeof (*bif));
390   pool_put (bm->interfaces, bif);
391
392   return 0;
393 }
394
395 void
396 bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args)
397 {
398   bond_main_t *bm = &bond_main;
399   vnet_main_t *vnm = vnet_get_main ();
400   vnet_sw_interface_t *sw;
401   bond_if_t *bif;
402   vnet_hw_interface_t *hw;
403
404   if ((args->mode == BOND_MODE_LACP) && bm->lacp_plugin_loaded == 0)
405     {
406       args->rv = VNET_API_ERROR_FEATURE_DISABLED;
407       args->error = clib_error_return (0, "LACP plugin is not loaded");
408       return;
409     }
410   if (args->mode > BOND_MODE_LACP || args->mode < BOND_MODE_ROUND_ROBIN)
411     {
412       args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
413       args->error = clib_error_return (0, "Invalid mode");
414       return;
415     }
416   if (args->lb > BOND_LB_L23)
417     {
418       args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
419       args->error = clib_error_return (0, "Invalid load-balance");
420       return;
421     }
422   pool_get (bm->interfaces, bif);
423   clib_memset (bif, 0, sizeof (*bif));
424   bif->dev_instance = bif - bm->interfaces;
425   bif->id = args->id;
426   bif->lb = args->lb;
427   bif->mode = args->mode;
428   bif->gso = args->gso;
429
430   // Adjust requested interface id
431   if (bif->id == ~0)
432     bif->id = bif->dev_instance;
433   if (hash_get (bm->id_used, bif->id))
434     {
435       args->rv = VNET_API_ERROR_INSTANCE_IN_USE;
436       pool_put (bm->interfaces, bif);
437       return;
438     }
439   hash_set (bm->id_used, bif->id, 1);
440
441   // Special load-balance mode used for rr and bc
442   if (bif->mode == BOND_MODE_ROUND_ROBIN)
443     bif->lb = BOND_LB_RR;
444   else if (bif->mode == BOND_MODE_BROADCAST)
445     bif->lb = BOND_LB_BC;
446   else if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
447     bif->lb = BOND_LB_AB;
448
449   bif->use_custom_mac = args->hw_addr_set;
450   if (!args->hw_addr_set)
451     {
452       f64 now = vlib_time_now (vm);
453       u32 rnd;
454       rnd = (u32) (now * 1e6);
455       rnd = random_u32 (&rnd);
456
457       memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
458       args->hw_addr[0] = 2;
459       args->hw_addr[1] = 0xfe;
460     }
461   memcpy (bif->hw_address, args->hw_addr, 6);
462   args->error = ethernet_register_interface
463     (vnm, bond_dev_class.index, bif->dev_instance /* device instance */ ,
464      bif->hw_address /* ethernet address */ ,
465      &bif->hw_if_index, 0 /* flag change */ );
466
467   if (args->error)
468     {
469       args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
470       hash_unset (bm->id_used, bif->id);
471       pool_put (bm->interfaces, bif);
472       return;
473     }
474
475   sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
476   bif->sw_if_index = sw->sw_if_index;
477   bif->group = bif->sw_if_index;
478   bif->numa_only = args->numa_only;
479
480   hw = vnet_get_hw_interface (vnm, bif->hw_if_index);
481   /*
482    * Add GSO and Checksum offload flags if GSO is enabled on Bond
483    */
484   if (args->gso)
485     {
486       hw->flags |= (VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
487                     VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
488     }
489   if (vlib_get_thread_main ()->n_vlib_mains > 1)
490     clib_spinlock_init (&bif->lockp);
491
492   hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
493
494   // for return
495   args->sw_if_index = bif->sw_if_index;
496   args->rv = 0;
497 }
498
499 static clib_error_t *
500 bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
501                         vlib_cli_command_t * cmd)
502 {
503   unformat_input_t _line_input, *line_input = &_line_input;
504   bond_create_if_args_t args = { 0 };
505   u8 mode_is_set = 0;
506
507   /* Get a line of input. */
508   if (!unformat_user (input, unformat_line_input, line_input))
509     return clib_error_return (0, "Missing required arguments.");
510
511   args.id = ~0;
512   args.mode = -1;
513   args.lb = BOND_LB_L2;
514   args.rv = -1;
515   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
516     {
517       if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
518         mode_is_set = 1;
519       else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
520                && unformat (line_input, "load-balance %U",
521                             unformat_bond_load_balance, &args.lb))
522         ;
523       else if (unformat (line_input, "hw-addr %U",
524                          unformat_ethernet_address, args.hw_addr))
525         args.hw_addr_set = 1;
526       else if (unformat (line_input, "id %u", &args.id))
527         ;
528       else if (unformat (line_input, "gso"))
529         args.gso = 1;
530       else if (unformat (line_input, "numa-only"))
531         {
532           if (args.mode == BOND_MODE_LACP)
533             args.numa_only = 1;
534           else
535             return clib_error_return (0,
536                                       "Only lacp mode supports numa-only so far!");
537         }
538       else
539         return clib_error_return (0, "unknown input `%U'",
540                                   format_unformat_error, input);
541     }
542   unformat_free (line_input);
543
544   if (mode_is_set == 0)
545     return clib_error_return (0, "Missing bond mode");
546
547   bond_create_if (vm, &args);
548
549   if (!args.rv)
550     vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
551                      vnet_get_main (), args.sw_if_index);
552
553   return args.error;
554 }
555
556 /* *INDENT-OFF* */
557 VLIB_CLI_COMMAND (bond_create_command, static) = {
558   .path = "create bond",
559   .short_help = "create bond mode {round-robin | active-backup | broadcast | "
560     "{lacp | xor} [load-balance { l2 | l23 | l34 } [numa-only]]} "
561     "[hw-addr <mac-address>] [id <if-id>] [gso]",
562   .function = bond_create_command_fn,
563 };
564 /* *INDENT-ON* */
565
566 static clib_error_t *
567 bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
568                         vlib_cli_command_t * cmd)
569 {
570   unformat_input_t _line_input, *line_input = &_line_input;
571   u32 sw_if_index = ~0;
572   vnet_main_t *vnm = vnet_get_main ();
573   int rv;
574
575   /* Get a line of input. */
576   if (!unformat_user (input, unformat_line_input, line_input))
577     return clib_error_return (0, "Missing <interface>");
578
579   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
580     {
581       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
582         ;
583       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
584                          vnm, &sw_if_index))
585         ;
586       else
587         return clib_error_return (0, "unknown input `%U'",
588                                   format_unformat_error, input);
589     }
590   unformat_free (line_input);
591
592   if (sw_if_index == ~0)
593     return clib_error_return (0,
594                               "please specify interface name or sw_if_index");
595
596   rv = bond_delete_if (vm, sw_if_index);
597   if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
598     return clib_error_return (0, "not a bond interface");
599   else if (rv != 0)
600     return clib_error_return (0, "error on deleting bond interface");
601
602   return 0;
603 }
604
605 /* *INDENT-OFF* */
606 VLIB_CLI_COMMAND (bond_delete__command, static) =
607 {
608   .path = "delete bond",
609   .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
610   .function = bond_delete_command_fn,
611 };
612 /* *INDENT-ON* */
613
614 void
615 bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args)
616 {
617   bond_main_t *bm = &bond_main;
618   vnet_main_t *vnm = vnet_get_main ();
619   bond_if_t *bif;
620   slave_if_t *sif;
621   vnet_interface_main_t *im = &vnm->interface_main;
622   vnet_hw_interface_t *bif_hw, *sif_hw;
623   vnet_sw_interface_t *sw;
624   u32 thread_index;
625   u32 sif_if_index;
626
627   bif = bond_get_master_by_sw_if_index (args->group);
628   if (!bif)
629     {
630       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
631       args->error = clib_error_return (0, "bond interface not found");
632       return;
633     }
634   // make sure the interface is not already enslaved
635   if (bond_get_slave_by_sw_if_index (args->slave))
636     {
637       args->rv = VNET_API_ERROR_VALUE_EXIST;
638       args->error = clib_error_return (0, "interface was already enslaved");
639       return;
640     }
641   sif_hw = vnet_get_sup_hw_interface (vnm, args->slave);
642   if (sif_hw->dev_class_index == bond_dev_class.index)
643     {
644       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
645       args->error =
646         clib_error_return (0, "bond interface cannot be enslaved");
647       return;
648     }
649   if (bif->gso && !(sif_hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO))
650     {
651       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
652       args->error =
653         clib_error_return (0, "slave interface is not gso capable");
654       return;
655     }
656   if (bif->mode == BOND_MODE_LACP)
657     {
658       u8 *name = format (0, "/if/lacp/%u/%u/state%c", bif->sw_if_index,
659                          args->slave, 0);
660
661       vec_validate (bm->stats, bif->sw_if_index);
662       vec_validate (bm->stats[bif->sw_if_index], args->slave);
663
664       args->error = stat_segment_register_state_counter
665         (name, &bm->stats[bif->sw_if_index][args->slave].actor_state);
666       if (args->error != 0)
667         {
668           args->rv = VNET_API_ERROR_INVALID_INTERFACE;
669           vec_free (name);
670           return;
671         }
672
673       vec_reset_length (name);
674       name = format (0, "/if/lacp/%u/%u/partner-state%c", bif->sw_if_index,
675                      args->slave, 0);
676       args->error = stat_segment_register_state_counter
677         (name, &bm->stats[bif->sw_if_index][args->slave].partner_state);
678       vec_free (name);
679       if (args->error != 0)
680         {
681           args->rv = VNET_API_ERROR_INVALID_INTERFACE;
682           return;
683         }
684     }
685
686   pool_get (bm->neighbors, sif);
687   clib_memset (sif, 0, sizeof (*sif));
688   sw = pool_elt_at_index (im->sw_interfaces, args->slave);
689   /* port_enabled is both admin up and hw link up */
690   sif->port_enabled = vnet_sw_interface_is_up (vnm, sw->sw_if_index);
691   sif->sw_if_index = sw->sw_if_index;
692   sif->hw_if_index = sw->hw_if_index;
693   sif->packet_template_index = (u8) ~ 0;
694   sif->is_passive = args->is_passive;
695   sif->group = args->group;
696   sif->bif_dev_instance = bif->dev_instance;
697   sif->mode = bif->mode;
698
699   sif->is_long_timeout = args->is_long_timeout;
700   if (args->is_long_timeout)
701     sif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
702   else
703     sif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
704
705   vec_validate_aligned (bm->slave_by_sw_if_index, sif->sw_if_index,
706                         CLIB_CACHE_LINE_BYTES);
707   /*
708    * sif - bm->neighbors may be 0
709    * Left shift it by 1 bit to distinguish the valid entry that we actually
710    * store from the null entries
711    */
712   bm->slave_by_sw_if_index[sif->sw_if_index] =
713     (uword) (((sif - bm->neighbors) << 1) | 1);
714   vec_add1 (bif->slaves, sif->sw_if_index);
715
716   sif_hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
717
718   /* Save the old mac */
719   memcpy (sif->persistent_hw_address, sif_hw->hw_address, 6);
720   bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
721   if (bif->use_custom_mac)
722     {
723       vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
724                                             bif->hw_address);
725     }
726   else
727     {
728       // bond interface gets the mac address from the first slave
729       if (vec_len (bif->slaves) == 1)
730         {
731           memcpy (bif->hw_address, sif_hw->hw_address, 6);
732           vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index,
733                                                 sif_hw->hw_address);
734         }
735       else
736         {
737           // subsequent slaves gets the mac address of the bond interface
738           vnet_hw_interface_change_mac_address (vnm, sif_hw->hw_if_index,
739                                                 bif->hw_address);
740         }
741     }
742
743   /* if there are secondary/virtual mac addrs, propagate to the slave */
744   bond_slave_add_del_mac_addrs (bif, sif->sw_if_index, 1 /* is_add */ );
745
746   if (bif_hw->l2_if_count)
747     ethernet_set_flags (vnm, sif_hw->hw_if_index,
748                         ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
749   else
750     ethernet_set_flags (vnm, sif_hw->hw_if_index,
751                         /*ETHERNET_INTERFACE_FLAG_DEFAULT_L3 */ 0);
752
753   if (bif->mode == BOND_MODE_LACP)
754     {
755       if (bm->lacp_enable_disable)
756         (*bm->lacp_enable_disable) (vm, bif, sif, 1);
757     }
758   else if (sif->port_enabled)
759     {
760       bond_enable_collecting_distributing (vm, sif);
761     }
762
763   vec_foreach_index (thread_index, bm->per_thread_data)
764   {
765     bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data,
766                                                     thread_index);
767
768     vec_validate_aligned (ptd->per_port_queue, vec_len (bif->slaves) - 1,
769                           CLIB_CACHE_LINE_BYTES);
770
771     vec_foreach_index (sif_if_index, ptd->per_port_queue)
772     {
773       ptd->per_port_queue[sif_if_index].n_buffers = 0;
774     }
775   }
776
777   args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
778                                           sif->sw_if_index, 1, 0, 0);
779
780   if (args->rv)
781     {
782       args->error =
783         clib_error_return (0,
784                            "Error encountered on input feature arc enable");
785     }
786 }
787
788 static clib_error_t *
789 enslave_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
790                               vlib_cli_command_t * cmd)
791 {
792   bond_enslave_args_t args = { 0 };
793   unformat_input_t _line_input, *line_input = &_line_input;
794   vnet_main_t *vnm = vnet_get_main ();
795
796   /* Get a line of input. */
797   if (!unformat_user (input, unformat_line_input, line_input))
798     return clib_error_return (0, "Missing required arguments.");
799
800   args.slave = ~0;
801   args.group = ~0;
802   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
803     {
804       if (unformat (line_input, "%U %U",
805                     unformat_vnet_sw_interface, vnm, &args.group,
806                     unformat_vnet_sw_interface, vnm, &args.slave))
807         ;
808       else if (unformat (line_input, "passive"))
809         args.is_passive = 1;
810       else if (unformat (line_input, "long-timeout"))
811         args.is_long_timeout = 1;
812       else
813         {
814           args.error = clib_error_return (0, "unknown input `%U'",
815                                           format_unformat_error, input);
816           break;
817         }
818     }
819   unformat_free (line_input);
820
821   if (args.error)
822     return args.error;
823   if (args.group == ~0)
824     return clib_error_return (0, "Missing bond interface");
825   if (args.slave == ~0)
826     return clib_error_return (0, "please specify valid slave interface name");
827
828   bond_enslave (vm, &args);
829
830   return args.error;
831 }
832
833 /* *INDENT-OFF* */
834 VLIB_CLI_COMMAND (enslave_interface_command, static) = {
835   .path = "bond add",
836   .short_help = "bond add <BondEthernetx> <slave-interface> "
837                 "[passive] [long-timeout]",
838   .function = enslave_interface_command_fn,
839 };
840 /* *INDENT-ON* */
841
842 void
843 bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args)
844 {
845   bond_if_t *bif;
846   slave_if_t *sif;
847
848   sif = bond_get_slave_by_sw_if_index (args->slave);
849   if (!sif)
850     {
851       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
852       args->error = clib_error_return (0, "interface was not enslaved");
853       return;
854     }
855   bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
856   bond_delete_neighbor (vm, bif, sif);
857 }
858
859 static clib_error_t *
860 detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
861                              vlib_cli_command_t * cmd)
862 {
863   bond_detach_slave_args_t args = { 0 };
864   unformat_input_t _line_input, *line_input = &_line_input;
865   vnet_main_t *vnm = vnet_get_main ();
866
867   /* Get a line of input. */
868   if (!unformat_user (input, unformat_line_input, line_input))
869     return clib_error_return (0, "Missing required arguments.");
870
871   args.slave = ~0;
872   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
873     {
874       if (unformat (line_input, "%U",
875                     unformat_vnet_sw_interface, vnm, &args.slave))
876         ;
877       else
878         {
879           args.error = clib_error_return (0, "unknown input `%U'",
880                                           format_unformat_error, input);
881           break;
882         }
883     }
884   unformat_free (line_input);
885
886   if (args.error)
887     return args.error;
888   if (args.slave == ~0)
889     return clib_error_return (0, "please specify valid slave interface name");
890
891   bond_detach_slave (vm, &args);
892
893   return args.error;
894 }
895
896 /* *INDENT-OFF* */
897 VLIB_CLI_COMMAND (detach_interface_command, static) = {
898   .path = "bond del",
899   .short_help = "bond del <slave-interface>",
900   .function = detach_interface_command_fn,
901 };
902 /* *INDENT-ON* */
903
904 static void
905 show_bond (vlib_main_t * vm)
906 {
907   bond_main_t *bm = &bond_main;
908   bond_if_t *bif;
909
910   vlib_cli_output (vm, "%-16s %-12s %-13s %-13s %-14s %s",
911                    "interface name", "sw_if_index", "mode",
912                    "load balance", "active slaves", "slaves");
913
914   /* *INDENT-OFF* */
915   pool_foreach (bif, bm->interfaces,
916   ({
917     vlib_cli_output (vm, "%-16U %-12d %-13U %-13U %-14u %u",
918                      format_bond_interface_name, bif->dev_instance,
919                      bif->sw_if_index, format_bond_mode, bif->mode,
920                      format_bond_load_balance, bif->lb,
921                      vec_len (bif->active_slaves), vec_len (bif->slaves));
922   }));
923   /* *INDENT-ON* */
924 }
925
926 static void
927 show_bond_details (vlib_main_t * vm)
928 {
929   bond_main_t *bm = &bond_main;
930   bond_if_t *bif;
931   u32 *sw_if_index;
932
933   /* *INDENT-OFF* */
934   pool_foreach (bif, bm->interfaces,
935   ({
936     vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
937     vlib_cli_output (vm, "  mode: %U",
938                      format_bond_mode, bif->mode);
939     vlib_cli_output (vm, "  load balance: %U",
940                      format_bond_load_balance, bif->lb);
941     if (bif->gso)
942       vlib_cli_output (vm, "  gso enable");
943     if (bif->mode == BOND_MODE_ROUND_ROBIN)
944       vlib_cli_output (vm, "  last xmit slave index: %u",
945                        bif->lb_rr_last_index);
946     vlib_cli_output (vm, "  number of active slaves: %d",
947                      vec_len (bif->active_slaves));
948     vec_foreach (sw_if_index, bif->active_slaves)
949       {
950         vlib_cli_output (vm, "    %U", format_vnet_sw_if_index_name,
951                          vnet_get_main (), *sw_if_index);
952         if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
953           {
954             slave_if_t *sif = bond_get_slave_by_sw_if_index (*sw_if_index);
955             if (sif)
956               vlib_cli_output (vm, "      weight: %u, is_local_numa: %u, "
957                                "sw_if_index: %u", sif->weight,
958                                sif->is_local_numa, sif->sw_if_index);
959           }
960       }
961     vlib_cli_output (vm, "  number of slaves: %d", vec_len (bif->slaves));
962     vec_foreach (sw_if_index, bif->slaves)
963       {
964         vlib_cli_output (vm, "    %U", format_vnet_sw_if_index_name,
965                          vnet_get_main (), *sw_if_index);
966       }
967     vlib_cli_output (vm, "  device instance: %d", bif->dev_instance);
968     vlib_cli_output (vm, "  interface id: %d", bif->id);
969     vlib_cli_output (vm, "  sw_if_index: %d", bif->sw_if_index);
970     vlib_cli_output (vm, "  hw_if_index: %d", bif->hw_if_index);
971   }));
972   /* *INDENT-ON* */
973 }
974
975 static clib_error_t *
976 show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
977               vlib_cli_command_t * cmd)
978 {
979   u8 details = 0;
980
981   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
982     {
983       if (unformat (input, "details"))
984         details = 1;
985       else
986         {
987           return clib_error_return (0, "unknown input `%U'",
988                                     format_unformat_error, input);
989         }
990     }
991
992   if (details)
993     show_bond_details (vm);
994   else
995     show_bond (vm);
996
997   return 0;
998 }
999
1000 /* *INDENT-OFF* */
1001 VLIB_CLI_COMMAND (show_bond_command, static) = {
1002   .path = "show bond",
1003   .short_help = "show bond [details]",
1004   .function = show_bond_fn,
1005 };
1006 /* *INDENT-ON* */
1007
1008 void
1009 bond_set_intf_weight (vlib_main_t * vm, bond_set_intf_weight_args_t * args)
1010 {
1011   slave_if_t *sif;
1012   bond_if_t *bif;
1013   vnet_main_t *vnm;
1014   u32 old_weight;
1015
1016   sif = bond_get_slave_by_sw_if_index (args->sw_if_index);
1017   if (!sif)
1018     {
1019       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1020       args->error = clib_error_return (0, "Interface not enslaved");
1021       return;
1022     }
1023   bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
1024   if (!bif)
1025     {
1026       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1027       args->error = clib_error_return (0, "bond interface not found");
1028       return;
1029     }
1030   if (bif->mode != BOND_MODE_ACTIVE_BACKUP)
1031     {
1032       args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
1033       args->error =
1034         clib_error_return (0, "Weight valid for active-backup only");
1035       return;
1036     }
1037
1038   old_weight = sif->weight;
1039   sif->weight = args->weight;
1040   vnm = vnet_get_main ();
1041   /*
1042    * No need to sort the list if the affected slave is not up (not in active
1043    * slave set), active slave count is 1, or the current slave is already the
1044    * primary slave and new weight > old weight.
1045    */
1046   if (!vnet_sw_interface_is_up (vnm, sif->sw_if_index) ||
1047       (vec_len (bif->active_slaves) == 1) ||
1048       ((bif->active_slaves[0] == sif->sw_if_index) &&
1049        (sif->weight >= old_weight)))
1050     return;
1051
1052   bond_sort_slaves (bif);
1053 }
1054
1055 static clib_error_t *
1056 bond_set_intf_cmd (vlib_main_t * vm, unformat_input_t * input,
1057                    vlib_cli_command_t * cmd)
1058 {
1059   bond_set_intf_weight_args_t args = { 0 };
1060   u32 sw_if_index = (u32) ~ 0;
1061   unformat_input_t _line_input, *line_input = &_line_input;
1062   vnet_main_t *vnm = vnet_get_main ();
1063   u8 weight_enter = 0;
1064   u32 weight = 0;
1065
1066   /* Get a line of input. */
1067   if (!unformat_user (input, unformat_line_input, line_input))
1068     return clib_error_return (0, "Missing required arguments.");
1069
1070   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1071     {
1072       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1073         ;
1074       else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
1075                          &sw_if_index))
1076         ;
1077       else if (unformat (line_input, "weight %u", &weight))
1078         weight_enter = 1;
1079       else
1080         {
1081           clib_error_return (0, "unknown input `%U'", format_unformat_error,
1082                              input);
1083           break;
1084         }
1085     }
1086
1087   unformat_free (line_input);
1088   if (sw_if_index == (u32) ~ 0)
1089     {
1090       args.rv = VNET_API_ERROR_INVALID_INTERFACE;
1091       clib_error_return (0, "Interface name is invalid!");
1092     }
1093   if (weight_enter == 0)
1094     {
1095       args.rv = VNET_API_ERROR_INVALID_ARGUMENT;
1096       clib_error_return (0, "weight missing");
1097     }
1098
1099   args.sw_if_index = sw_if_index;
1100   args.weight = weight;
1101   bond_set_intf_weight (vm, &args);
1102
1103   return args.error;
1104 }
1105
1106 /* *INDENT-OFF* */
1107 VLIB_CLI_COMMAND(set_interface_bond_cmd, static) = {
1108   .path = "set interface bond",
1109   .short_help = "set interface bond <interface> | sw_if_index <idx>"
1110                 " weight <value>",
1111   .function = bond_set_intf_cmd,
1112 };
1113 /* *INDENT-ON* */
1114
1115 clib_error_t *
1116 bond_cli_init (vlib_main_t * vm)
1117 {
1118   bond_main_t *bm = &bond_main;
1119
1120   bm->vlib_main = vm;
1121   bm->vnet_main = vnet_get_main ();
1122   vec_validate_aligned (bm->slave_by_sw_if_index, 1, CLIB_CACHE_LINE_BYTES);
1123   vec_validate_aligned (bm->per_thread_data,
1124                         vlib_get_thread_main ()->n_vlib_mains - 1,
1125                         CLIB_CACHE_LINE_BYTES);
1126
1127   return 0;
1128 }
1129
1130 VLIB_INIT_FUNCTION (bond_cli_init);
1131
1132 /*
1133  * fd.io coding-style-patch-verification: ON
1134  *
1135  * Local Variables:
1136  * eval: (c-set-style "gnu")
1137  * End:
1138  */