ethernet: new interface registration function
[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   // Adjust requested interface id
412   if (bif->id == ~0)
413     bif->id = bif->dev_instance;
414   if (hash_get (bm->id_used, bif->id))
415     {
416       args->rv = VNET_API_ERROR_INSTANCE_IN_USE;
417       pool_put (bm->interfaces, bif);
418       return;
419     }
420   hash_set (bm->id_used, bif->id, 1);
421
422   // Special load-balance mode used for rr and bc
423   if (bif->mode == BOND_MODE_ROUND_ROBIN)
424     bif->lb = BOND_LB_RR;
425   else if (bif->mode == BOND_MODE_BROADCAST)
426     bif->lb = BOND_LB_BC;
427   else if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
428     bif->lb = BOND_LB_AB;
429
430   bif->use_custom_mac = args->hw_addr_set;
431   if (!args->hw_addr_set)
432     {
433       f64 now = vlib_time_now (vm);
434       u32 rnd;
435       rnd = (u32) (now * 1e6);
436       rnd = random_u32 (&rnd);
437
438       memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
439       args->hw_addr[0] = 2;
440       args->hw_addr[1] = 0xfe;
441     }
442   memcpy (bif->hw_address, args->hw_addr, 6);
443
444   eir.dev_class_index = bond_dev_class.index;
445   eir.dev_instance = bif->dev_instance;
446   eir.address = bif->hw_address;
447   bif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
448
449   sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
450   bif->sw_if_index = sw->sw_if_index;
451   bif->group = bif->sw_if_index;
452   bif->numa_only = args->numa_only;
453
454   /*
455    * Add GSO and Checksum offload flags if GSO is enabled on Bond
456    */
457   if (args->gso)
458     {
459       vnet_hw_if_set_caps (vnm, bif->hw_if_index,
460                            VNET_HW_IF_CAP_TCP_GSO |
461                              VNET_HW_IF_CAP_TX_TCP_CKSUM |
462                              VNET_HW_IF_CAP_TX_UDP_CKSUM);
463     }
464   if (vlib_get_thread_main ()->n_vlib_mains > 1)
465     clib_spinlock_init (&bif->lockp);
466
467   vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
468                                VNET_HW_INTERFACE_FLAG_LINK_UP);
469
470   hash_set (bm->bond_by_sw_if_index, bif->sw_if_index, bif->dev_instance);
471
472   // for return
473   args->sw_if_index = bif->sw_if_index;
474   args->rv = 0;
475 }
476
477 static clib_error_t *
478 bond_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
479                         vlib_cli_command_t * cmd)
480 {
481   unformat_input_t _line_input, *line_input = &_line_input;
482   bond_create_if_args_t args = { 0 };
483   u8 mode_is_set = 0;
484
485   /* Get a line of input. */
486   if (!unformat_user (input, unformat_line_input, line_input))
487     return clib_error_return (0, "Missing required arguments.");
488
489   args.id = ~0;
490   args.mode = -1;
491   args.lb = BOND_LB_L2;
492   args.rv = -1;
493   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
494     {
495       if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
496         mode_is_set = 1;
497       else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
498                && unformat (line_input, "load-balance %U",
499                             unformat_bond_load_balance, &args.lb))
500         ;
501       else if (unformat (line_input, "hw-addr %U",
502                          unformat_ethernet_address, args.hw_addr))
503         args.hw_addr_set = 1;
504       else if (unformat (line_input, "id %u", &args.id))
505         ;
506       else if (unformat (line_input, "gso"))
507         args.gso = 1;
508       else if (unformat (line_input, "numa-only"))
509         {
510           if (args.mode == BOND_MODE_LACP)
511             args.numa_only = 1;
512           else
513             return clib_error_return (0,
514                                       "Only lacp mode supports numa-only so far!");
515         }
516       else
517         return clib_error_return (0, "unknown input `%U'",
518                                   format_unformat_error, input);
519     }
520   unformat_free (line_input);
521
522   if (mode_is_set == 0)
523     return clib_error_return (0, "Missing bond mode");
524
525   bond_create_if (vm, &args);
526
527   if (!args.rv)
528     vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
529                      vnet_get_main (), args.sw_if_index);
530
531   return args.error;
532 }
533
534 /* *INDENT-OFF* */
535 VLIB_CLI_COMMAND (bond_create_command, static) = {
536   .path = "create bond",
537   .short_help = "create bond mode {round-robin | active-backup | broadcast | "
538     "{lacp | xor} [load-balance { l2 | l23 | l34 } [numa-only]]} "
539     "[hw-addr <mac-address>] [id <if-id>] [gso]",
540   .function = bond_create_command_fn,
541 };
542 /* *INDENT-ON* */
543
544 static clib_error_t *
545 bond_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
546                         vlib_cli_command_t * cmd)
547 {
548   unformat_input_t _line_input, *line_input = &_line_input;
549   u32 sw_if_index = ~0;
550   vnet_main_t *vnm = vnet_get_main ();
551   int rv;
552
553   /* Get a line of input. */
554   if (!unformat_user (input, unformat_line_input, line_input))
555     return clib_error_return (0, "Missing <interface>");
556
557   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
558     {
559       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
560         ;
561       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
562                          vnm, &sw_if_index))
563         ;
564       else
565         return clib_error_return (0, "unknown input `%U'",
566                                   format_unformat_error, input);
567     }
568   unformat_free (line_input);
569
570   if (sw_if_index == ~0)
571     return clib_error_return (0,
572                               "please specify interface name or sw_if_index");
573
574   rv = bond_delete_if (vm, sw_if_index);
575   if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
576     return clib_error_return (0, "not a bond interface");
577   else if (rv != 0)
578     return clib_error_return (0, "error on deleting bond interface");
579
580   return 0;
581 }
582
583 /* *INDENT-OFF* */
584 VLIB_CLI_COMMAND (bond_delete__command, static) =
585 {
586   .path = "delete bond",
587   .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
588   .function = bond_delete_command_fn,
589 };
590 /* *INDENT-ON* */
591
592 void
593 bond_add_member (vlib_main_t * vm, bond_add_member_args_t * args)
594 {
595   bond_main_t *bm = &bond_main;
596   vnet_main_t *vnm = vnet_get_main ();
597   bond_if_t *bif;
598   member_if_t *mif;
599   vnet_interface_main_t *im = &vnm->interface_main;
600   vnet_hw_interface_t *bif_hw, *mif_hw;
601   vnet_sw_interface_t *sw;
602   u32 thread_index;
603   u32 mif_if_index;
604
605   bif = bond_get_bond_if_by_sw_if_index (args->group);
606   if (!bif)
607     {
608       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
609       args->error = clib_error_return (0, "bond interface not found");
610       return;
611     }
612   // make sure the interface is not already added as member
613   if (bond_get_member_by_sw_if_index (args->member))
614     {
615       args->rv = VNET_API_ERROR_VALUE_EXIST;
616       args->error = clib_error_return
617         (0, "interface was already added as member");
618       return;
619     }
620   mif_hw = vnet_get_sup_hw_interface (vnm, args->member);
621   if (mif_hw->dev_class_index == bond_dev_class.index)
622     {
623       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
624       args->error =
625         clib_error_return (0, "bond interface cannot be added as member");
626       return;
627     }
628   if (bif->gso && !(mif_hw->caps & VNET_HW_IF_CAP_TCP_GSO))
629     {
630       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
631       args->error =
632         clib_error_return (0, "member interface is not gso capable");
633       return;
634     }
635   if (bif->mode == BOND_MODE_LACP)
636     {
637       u8 *name = format (0, "/if/lacp/%u/%u/state%c", bif->sw_if_index,
638                          args->member, 0);
639
640       vec_validate (bm->stats, bif->sw_if_index);
641       vec_validate (bm->stats[bif->sw_if_index], args->member);
642
643       args->error = stat_segment_register_state_counter
644         (name, &bm->stats[bif->sw_if_index][args->member].actor_state);
645       if (args->error != 0)
646         {
647           args->rv = VNET_API_ERROR_INVALID_INTERFACE;
648           vec_free (name);
649           return;
650         }
651
652       vec_reset_length (name);
653       name = format (0, "/if/lacp/%u/%u/partner-state%c", bif->sw_if_index,
654                      args->member, 0);
655       args->error = stat_segment_register_state_counter
656         (name, &bm->stats[bif->sw_if_index][args->member].partner_state);
657       vec_free (name);
658       if (args->error != 0)
659         {
660           args->rv = VNET_API_ERROR_INVALID_INTERFACE;
661           return;
662         }
663     }
664
665   pool_get (bm->neighbors, mif);
666   clib_memset (mif, 0, sizeof (*mif));
667   sw = pool_elt_at_index (im->sw_interfaces, args->member);
668   /* port_enabled is both admin up and hw link up */
669   mif->port_enabled = vnet_sw_interface_is_up (vnm, sw->sw_if_index);
670   mif->sw_if_index = sw->sw_if_index;
671   mif->hw_if_index = sw->hw_if_index;
672   mif->packet_template_index = (u8) ~ 0;
673   mif->is_passive = args->is_passive;
674   mif->group = args->group;
675   mif->bif_dev_instance = bif->dev_instance;
676   mif->mode = bif->mode;
677
678   mif->is_long_timeout = args->is_long_timeout;
679   if (args->is_long_timeout)
680     mif->ttl_in_seconds = LACP_LONG_TIMOUT_TIME;
681   else
682     mif->ttl_in_seconds = LACP_SHORT_TIMOUT_TIME;
683
684   vec_validate_aligned (bm->member_by_sw_if_index, mif->sw_if_index,
685                         CLIB_CACHE_LINE_BYTES);
686   /*
687    * mif - bm->neighbors may be 0
688    * Left shift it by 1 bit to distinguish the valid entry that we actually
689    * store from the null entries
690    */
691   bm->member_by_sw_if_index[mif->sw_if_index] =
692     (uword) (((mif - bm->neighbors) << 1) | 1);
693   vec_add1 (bif->members, mif->sw_if_index);
694
695   mif_hw = vnet_get_sup_hw_interface (vnm, mif->sw_if_index);
696
697   /* Save the old mac */
698   memcpy (mif->persistent_hw_address, mif_hw->hw_address, 6);
699   bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
700   if (bif->use_custom_mac)
701     {
702       vnet_hw_interface_change_mac_address (vnm, mif_hw->hw_if_index,
703                                             bif->hw_address);
704     }
705   else
706     {
707       // bond interface gets the mac address from the first member
708       if (vec_len (bif->members) == 1)
709         {
710           memcpy (bif->hw_address, mif_hw->hw_address, 6);
711           vnet_hw_interface_change_mac_address (vnm, bif_hw->hw_if_index,
712                                                 mif_hw->hw_address);
713         }
714       else
715         {
716           // subsequent members gets the mac address of the bond interface
717           vnet_hw_interface_change_mac_address (vnm, mif_hw->hw_if_index,
718                                                 bif->hw_address);
719         }
720     }
721
722   /* if there are secondary/virtual mac addrs, propagate to the member */
723   bond_member_add_del_mac_addrs (bif, mif->sw_if_index, 1 /* is_add */ );
724
725   if (bif_hw->l2_if_count)
726     ethernet_set_flags (vnm, mif_hw->hw_if_index,
727                         ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
728   else
729     ethernet_set_flags (vnm, mif_hw->hw_if_index,
730                         /*ETHERNET_INTERFACE_FLAG_DEFAULT_L3 */ 0);
731
732   if (bif->mode == BOND_MODE_LACP)
733     {
734       if (bm->lacp_enable_disable)
735         (*bm->lacp_enable_disable) (vm, bif, mif, 1);
736     }
737   else if (mif->port_enabled)
738     {
739       bond_enable_collecting_distributing (vm, mif);
740     }
741
742   vec_foreach_index (thread_index, bm->per_thread_data)
743   {
744     bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data,
745                                                     thread_index);
746
747     vec_validate_aligned (ptd->per_port_queue, vec_len (bif->members) - 1,
748                           CLIB_CACHE_LINE_BYTES);
749
750     vec_foreach_index (mif_if_index, ptd->per_port_queue)
751     {
752       ptd->per_port_queue[mif_if_index].n_buffers = 0;
753     }
754   }
755
756   args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
757                                           mif->sw_if_index, 1, 0, 0);
758
759   if (args->rv)
760     {
761       args->error =
762         clib_error_return (0,
763                            "Error encountered on input feature arc enable");
764     }
765 }
766
767 static clib_error_t *
768 add_member_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
769                                  vlib_cli_command_t * cmd)
770 {
771   bond_add_member_args_t args = { 0 };
772   unformat_input_t _line_input, *line_input = &_line_input;
773   vnet_main_t *vnm = vnet_get_main ();
774
775   /* Get a line of input. */
776   if (!unformat_user (input, unformat_line_input, line_input))
777     return clib_error_return (0, "Missing required arguments.");
778
779   args.member = ~0;
780   args.group = ~0;
781   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
782     {
783       if (unformat (line_input, "%U %U",
784                     unformat_vnet_sw_interface, vnm, &args.group,
785                     unformat_vnet_sw_interface, vnm, &args.member))
786         ;
787       else if (unformat (line_input, "passive"))
788         args.is_passive = 1;
789       else if (unformat (line_input, "long-timeout"))
790         args.is_long_timeout = 1;
791       else
792         {
793           args.error = clib_error_return (0, "unknown input `%U'",
794                                           format_unformat_error, input);
795           break;
796         }
797     }
798   unformat_free (line_input);
799
800   if (args.error)
801     return args.error;
802   if (args.group == ~0)
803     return clib_error_return (0, "Missing bond interface");
804   if (args.member == ~0)
805     return clib_error_return (0,
806                               "please specify valid member interface name");
807
808   bond_add_member (vm, &args);
809
810   return args.error;
811 }
812
813 /* *INDENT-OFF* */
814 VLIB_CLI_COMMAND (add_member_interface_command, static) = {
815   .path = "bond add",
816   .short_help = "bond add <BondEthernetx> <member-interface> "
817                 "[passive] [long-timeout]",
818   .function = add_member_interface_command_fn,
819 };
820 /* *INDENT-ON* */
821
822 void
823 bond_detach_member (vlib_main_t * vm, bond_detach_member_args_t * args)
824 {
825   bond_if_t *bif;
826   member_if_t *mif;
827
828   mif = bond_get_member_by_sw_if_index (args->member);
829   if (!mif)
830     {
831       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
832       args->error = clib_error_return (0, "interface was not a member");
833       return;
834     }
835   bif = bond_get_bond_if_by_dev_instance (mif->bif_dev_instance);
836   bond_delete_neighbor (vm, bif, mif);
837 }
838
839 static clib_error_t *
840 detach_interface_command_fn (vlib_main_t * vm, unformat_input_t * input,
841                              vlib_cli_command_t * cmd)
842 {
843   bond_detach_member_args_t args = { 0 };
844   unformat_input_t _line_input, *line_input = &_line_input;
845   vnet_main_t *vnm = vnet_get_main ();
846
847   /* Get a line of input. */
848   if (!unformat_user (input, unformat_line_input, line_input))
849     return clib_error_return (0, "Missing required arguments.");
850
851   args.member = ~0;
852   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
853     {
854       if (unformat (line_input, "%U",
855                     unformat_vnet_sw_interface, vnm, &args.member))
856         ;
857       else
858         {
859           args.error = clib_error_return (0, "unknown input `%U'",
860                                           format_unformat_error, input);
861           break;
862         }
863     }
864   unformat_free (line_input);
865
866   if (args.error)
867     return args.error;
868   if (args.member == ~0)
869     return clib_error_return (0,
870                               "please specify valid member interface name");
871
872   bond_detach_member (vm, &args);
873
874   return args.error;
875 }
876
877 /* *INDENT-OFF* */
878 VLIB_CLI_COMMAND (detach_interface_command, static) = {
879   .path = "bond del",
880   .short_help = "bond del <member-interface>",
881   .function = detach_interface_command_fn,
882 };
883 /* *INDENT-ON* */
884
885 static void
886 show_bond (vlib_main_t * vm)
887 {
888   bond_main_t *bm = &bond_main;
889   bond_if_t *bif;
890
891   vlib_cli_output (vm, "%-16s %-12s %-13s %-13s %-14s %s",
892                    "interface name", "sw_if_index", "mode",
893                    "load balance", "active members", "members");
894
895   /* *INDENT-OFF* */
896   pool_foreach (bif, bm->interfaces)
897    {
898     vlib_cli_output (vm, "%-16U %-12d %-13U %-13U %-14u %u",
899                      format_bond_interface_name, bif->dev_instance,
900                      bif->sw_if_index, format_bond_mode, bif->mode,
901                      format_bond_load_balance, bif->lb,
902                      vec_len (bif->active_members), vec_len (bif->members));
903   }
904   /* *INDENT-ON* */
905 }
906
907 static void
908 show_bond_details (vlib_main_t * vm)
909 {
910   bond_main_t *bm = &bond_main;
911   bond_if_t *bif;
912   u32 *sw_if_index;
913
914   /* *INDENT-OFF* */
915   pool_foreach (bif, bm->interfaces)
916    {
917     vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
918     vlib_cli_output (vm, "  mode: %U",
919                      format_bond_mode, bif->mode);
920     vlib_cli_output (vm, "  load balance: %U",
921                      format_bond_load_balance, bif->lb);
922     if (bif->gso)
923       vlib_cli_output (vm, "  gso enable");
924     if (bif->mode == BOND_MODE_ROUND_ROBIN)
925       vlib_cli_output (vm, "  last xmit member index: %u",
926                        bif->lb_rr_last_index);
927     vlib_cli_output (vm, "  number of active members: %d",
928                      vec_len (bif->active_members));
929     vec_foreach (sw_if_index, bif->active_members)
930       {
931         vlib_cli_output (vm, "    %U", format_vnet_sw_if_index_name,
932                          vnet_get_main (), *sw_if_index);
933         if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
934           {
935             member_if_t *mif = bond_get_member_by_sw_if_index (*sw_if_index);
936             if (mif)
937               vlib_cli_output (vm, "      weight: %u, is_local_numa: %u, "
938                                "sw_if_index: %u", mif->weight,
939                                mif->is_local_numa, mif->sw_if_index);
940           }
941       }
942     vlib_cli_output (vm, "  number of members: %d", vec_len (bif->members));
943     vec_foreach (sw_if_index, bif->members)
944       {
945         vlib_cli_output (vm, "    %U", format_vnet_sw_if_index_name,
946                          vnet_get_main (), *sw_if_index);
947       }
948     vlib_cli_output (vm, "  device instance: %d", bif->dev_instance);
949     vlib_cli_output (vm, "  interface id: %d", bif->id);
950     vlib_cli_output (vm, "  sw_if_index: %d", bif->sw_if_index);
951     vlib_cli_output (vm, "  hw_if_index: %d", bif->hw_if_index);
952   }
953   /* *INDENT-ON* */
954 }
955
956 static clib_error_t *
957 show_bond_fn (vlib_main_t * vm, unformat_input_t * input,
958               vlib_cli_command_t * cmd)
959 {
960   u8 details = 0;
961
962   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
963     {
964       if (unformat (input, "details"))
965         details = 1;
966       else
967         {
968           return clib_error_return (0, "unknown input `%U'",
969                                     format_unformat_error, input);
970         }
971     }
972
973   if (details)
974     show_bond_details (vm);
975   else
976     show_bond (vm);
977
978   return 0;
979 }
980
981 /* *INDENT-OFF* */
982 VLIB_CLI_COMMAND (show_bond_command, static) = {
983   .path = "show bond",
984   .short_help = "show bond [details]",
985   .function = show_bond_fn,
986 };
987 /* *INDENT-ON* */
988
989 void
990 bond_set_intf_weight (vlib_main_t * vm, bond_set_intf_weight_args_t * args)
991 {
992   member_if_t *mif;
993   bond_if_t *bif;
994   vnet_main_t *vnm;
995   u32 old_weight;
996
997   mif = bond_get_member_by_sw_if_index (args->sw_if_index);
998   if (!mif)
999     {
1000       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1001       args->error = clib_error_return (0, "Interface not a member");
1002       return;
1003     }
1004   bif = bond_get_bond_if_by_dev_instance (mif->bif_dev_instance);
1005   if (!bif)
1006     {
1007       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1008       args->error = clib_error_return (0, "bond interface not found");
1009       return;
1010     }
1011   if (bif->mode != BOND_MODE_ACTIVE_BACKUP)
1012     {
1013       args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
1014       args->error =
1015         clib_error_return (0, "Weight valid for active-backup only");
1016       return;
1017     }
1018
1019   old_weight = mif->weight;
1020   mif->weight = args->weight;
1021   vnm = vnet_get_main ();
1022   /*
1023    * No need to sort the list if the affected member is not up (not in active
1024    * member set), active member count is 1, or the current member is already the
1025    * primary member and new weight > old weight.
1026    */
1027   if (!vnet_sw_interface_is_up (vnm, mif->sw_if_index) ||
1028       (vec_len (bif->active_members) == 1) ||
1029       ((bif->active_members[0] == mif->sw_if_index) &&
1030        (mif->weight >= old_weight)))
1031     return;
1032
1033   bond_sort_members (bif);
1034 }
1035
1036 static clib_error_t *
1037 bond_set_intf_cmd (vlib_main_t * vm, unformat_input_t * input,
1038                    vlib_cli_command_t * cmd)
1039 {
1040   bond_set_intf_weight_args_t args = { 0 };
1041   u32 sw_if_index = (u32) ~ 0;
1042   unformat_input_t _line_input, *line_input = &_line_input;
1043   vnet_main_t *vnm = vnet_get_main ();
1044   u8 weight_enter = 0;
1045   u32 weight = 0;
1046
1047   /* Get a line of input. */
1048   if (!unformat_user (input, unformat_line_input, line_input))
1049     return clib_error_return (0, "Missing required arguments.");
1050
1051   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1052     {
1053       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1054         ;
1055       else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
1056                          &sw_if_index))
1057         ;
1058       else if (unformat (line_input, "weight %u", &weight))
1059         weight_enter = 1;
1060       else
1061         {
1062           clib_error_return (0, "unknown input `%U'", format_unformat_error,
1063                              input);
1064           break;
1065         }
1066     }
1067
1068   unformat_free (line_input);
1069   if (sw_if_index == (u32) ~ 0)
1070     {
1071       args.rv = VNET_API_ERROR_INVALID_INTERFACE;
1072       clib_error_return (0, "Interface name is invalid!");
1073     }
1074   if (weight_enter == 0)
1075     {
1076       args.rv = VNET_API_ERROR_INVALID_ARGUMENT;
1077       clib_error_return (0, "weight missing");
1078     }
1079
1080   args.sw_if_index = sw_if_index;
1081   args.weight = weight;
1082   bond_set_intf_weight (vm, &args);
1083
1084   return args.error;
1085 }
1086
1087 /* *INDENT-OFF* */
1088 VLIB_CLI_COMMAND(set_interface_bond_cmd, static) = {
1089   .path = "set interface bond",
1090   .short_help = "set interface bond <interface> | sw_if_index <idx>"
1091                 " weight <value>",
1092   .function = bond_set_intf_cmd,
1093 };
1094 /* *INDENT-ON* */
1095
1096 clib_error_t *
1097 bond_cli_init (vlib_main_t * vm)
1098 {
1099   bond_main_t *bm = &bond_main;
1100
1101   bm->vlib_main = vm;
1102   bm->vnet_main = vnet_get_main ();
1103   vec_validate_aligned (bm->member_by_sw_if_index, 1, CLIB_CACHE_LINE_BYTES);
1104   vec_validate_aligned (bm->per_thread_data,
1105                         vlib_get_thread_main ()->n_vlib_mains - 1,
1106                         CLIB_CACHE_LINE_BYTES);
1107
1108   return 0;
1109 }
1110
1111 VLIB_INIT_FUNCTION (bond_cli_init);
1112
1113 /*
1114  * fd.io coding-style-patch-verification: ON
1115  *
1116  * Local Variables:
1117  * eval: (c-set-style "gnu")
1118  * End:
1119  */