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