FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
cli.c
Go to the documentation of this file.
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
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 
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)
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;
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  {
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
123 {
124  bond_main_t *bm = &bond_main;
125  u32 old_active = bif->active_members[0];
126 
128  if (old_active != bif->active_members[0])
131 }
132 
133 void
135 {
136  bond_if_t *bif;
137  bond_main_t *bm = &bond_main;
138  vnet_main_t *vnm = vnet_get_main ();
140  int i;
141  uword p;
142 
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  {
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? */
168  else
169  bond_sort_members (bif);
170  }
171 
172 done:
174 }
175 
176 int
178 {
179  vnet_main_t *vnm = vnet_get_main ();
180  bond_main_t *bm = &bond_main;
181  bond_if_t *bif;
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
213  u32 bond_sw_if_index)
214 {
215  vnet_main_t *vnm = vnet_get_main ();
216  bond_if_t *bif;
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
264  u8 is_add)
265 {
266  vnet_main_t *vnm = vnet_get_main ();
267  ethernet_interface_t *b_ei;
268  mac_address_t *sec_mac;
269  vnet_hw_interface_t *s_hwif;
270 
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)
279  sec_mac->bytes, is_add);
280 }
281 
282 static void
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 =
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 
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 */
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  {
327  (bm->stats[bif->sw_if_index][mif->sw_if_index].actor_state);
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
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;
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 
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 */
364 
366 
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
378 {
379  bond_main_t *bm = &bond_main;
380  vnet_main_t *vnm = vnet_get_main ();
382  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);
444  (vnm, bond_dev_class.index, bif->dev_instance /* device instance */ ,
445  bif->hw_address /* ethernet address */ ,
446  &bif->hw_if_index, 0 /* flag change */ );
447 
448  if (args->error)
449  {
450  args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
451  hash_unset (bm->id_used, bif->id);
452  pool_put (bm->interfaces, bif);
453  return;
454  }
455 
456  sw = vnet_get_hw_sw_interface (vnm, bif->hw_if_index);
457  bif->sw_if_index = sw->sw_if_index;
458  bif->group = bif->sw_if_index;
459  bif->numa_only = args->numa_only;
460 
461  hw = vnet_get_hw_interface (vnm, bif->hw_if_index);
462  /*
463  * Add GSO and Checksum offload flags if GSO is enabled on Bond
464  */
465  if (args->gso)
466  {
469  }
471  clib_spinlock_init (&bif->lockp);
472 
475 
477 
478  // for return
479  args->sw_if_index = bif->sw_if_index;
480  args->rv = 0;
481 }
482 
483 static clib_error_t *
485  vlib_cli_command_t * cmd)
486 {
487  unformat_input_t _line_input, *line_input = &_line_input;
488  bond_create_if_args_t args = { 0 };
489  u8 mode_is_set = 0;
490 
491  /* Get a line of input. */
492  if (!unformat_user (input, unformat_line_input, line_input))
493  return clib_error_return (0, "Missing required arguments.");
494 
495  args.id = ~0;
496  args.mode = -1;
497  args.lb = BOND_LB_L2;
498  args.rv = -1;
499  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
500  {
501  if (unformat (line_input, "mode %U", unformat_bond_mode, &args.mode))
502  mode_is_set = 1;
503  else if (((args.mode == BOND_MODE_LACP) || (args.mode == BOND_MODE_XOR))
504  && unformat (line_input, "load-balance %U",
506  ;
507  else if (unformat (line_input, "hw-addr %U",
509  args.hw_addr_set = 1;
510  else if (unformat (line_input, "id %u", &args.id))
511  ;
512  else if (unformat (line_input, "gso"))
513  args.gso = 1;
514  else if (unformat (line_input, "numa-only"))
515  {
516  if (args.mode == BOND_MODE_LACP)
517  args.numa_only = 1;
518  else
519  return clib_error_return (0,
520  "Only lacp mode supports numa-only so far!");
521  }
522  else
523  return clib_error_return (0, "unknown input `%U'",
524  format_unformat_error, input);
525  }
526  unformat_free (line_input);
527 
528  if (mode_is_set == 0)
529  return clib_error_return (0, "Missing bond mode");
530 
531  bond_create_if (vm, &args);
532 
533  if (!args.rv)
535  vnet_get_main (), args.sw_if_index);
536 
537  return args.error;
538 }
539 
540 /* *INDENT-OFF* */
541 VLIB_CLI_COMMAND (bond_create_command, static) = {
542  .path = "create bond",
543  .short_help = "create bond mode {round-robin | active-backup | broadcast | "
544  "{lacp | xor} [load-balance { l2 | l23 | l34 } [numa-only]]} "
545  "[hw-addr <mac-address>] [id <if-id>] [gso]",
546  .function = bond_create_command_fn,
547 };
548 /* *INDENT-ON* */
549 
550 static clib_error_t *
552  vlib_cli_command_t * cmd)
553 {
554  unformat_input_t _line_input, *line_input = &_line_input;
555  u32 sw_if_index = ~0;
556  vnet_main_t *vnm = vnet_get_main ();
557  int rv;
558 
559  /* Get a line of input. */
560  if (!unformat_user (input, unformat_line_input, line_input))
561  return clib_error_return (0, "Missing <interface>");
562 
563  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
564  {
565  if (unformat (line_input, "sw_if_index %d", &sw_if_index))
566  ;
567  else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
568  vnm, &sw_if_index))
569  ;
570  else
571  return clib_error_return (0, "unknown input `%U'",
572  format_unformat_error, input);
573  }
574  unformat_free (line_input);
575 
576  if (sw_if_index == ~0)
577  return clib_error_return (0,
578  "please specify interface name or sw_if_index");
579 
580  rv = bond_delete_if (vm, sw_if_index);
581  if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
582  return clib_error_return (0, "not a bond interface");
583  else if (rv != 0)
584  return clib_error_return (0, "error on deleting bond interface");
585 
586  return 0;
587 }
588 
589 /* *INDENT-OFF* */
590 VLIB_CLI_COMMAND (bond_delete__command, static) =
591 {
592  .path = "delete bond",
593  .short_help = "delete bond {<interface> | sw_if_index <sw_idx>}",
594  .function = bond_delete_command_fn,
595 };
596 /* *INDENT-ON* */
597 
598 void
600 {
601  bond_main_t *bm = &bond_main;
602  vnet_main_t *vnm = vnet_get_main ();
603  bond_if_t *bif;
604  member_if_t *mif;
606  vnet_hw_interface_t *bif_hw, *mif_hw;
608  u32 thread_index;
609  u32 mif_if_index;
610 
612  if (!bif)
613  {
614  args->rv = VNET_API_ERROR_INVALID_INTERFACE;
615  args->error = clib_error_return (0, "bond interface not found");
616  return;
617  }
618  // make sure the interface is not already added as member
620  {
621  args->rv = VNET_API_ERROR_VALUE_EXIST;
622  args->error = clib_error_return
623  (0, "interface was already added as member");
624  return;
625  }
626  mif_hw = vnet_get_sup_hw_interface (vnm, args->member);
627  if (mif_hw->dev_class_index == bond_dev_class.index)
628  {
629  args->rv = VNET_API_ERROR_INVALID_INTERFACE;
630  args->error =
631  clib_error_return (0, "bond interface cannot be added as member");
632  return;
633  }
634  if (bif->gso && !(mif_hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO))
635  {
636  args->rv = VNET_API_ERROR_INVALID_INTERFACE;
637  args->error =
638  clib_error_return (0, "member interface is not gso capable");
639  return;
640  }
641  if (bif->mode == BOND_MODE_LACP)
642  {
643  u8 *name = format (0, "/if/lacp/%u/%u/state%c", bif->sw_if_index,
644  args->member, 0);
645 
646  vec_validate (bm->stats, bif->sw_if_index);
647  vec_validate (bm->stats[bif->sw_if_index], args->member);
648 
650  (name, &bm->stats[bif->sw_if_index][args->member].actor_state);
651  if (args->error != 0)
652  {
653  args->rv = VNET_API_ERROR_INVALID_INTERFACE;
654  vec_free (name);
655  return;
656  }
657 
658  vec_reset_length (name);
659  name = format (0, "/if/lacp/%u/%u/partner-state%c", bif->sw_if_index,
660  args->member, 0);
662  (name, &bm->stats[bif->sw_if_index][args->member].partner_state);
663  vec_free (name);
664  if (args->error != 0)
665  {
666  args->rv = VNET_API_ERROR_INVALID_INTERFACE;
667  return;
668  }
669  }
670 
671  pool_get (bm->neighbors, mif);
672  clib_memset (mif, 0, sizeof (*mif));
673  sw = pool_elt_at_index (im->sw_interfaces, args->member);
674  /* port_enabled is both admin up and hw link up */
676  mif->sw_if_index = sw->sw_if_index;
677  mif->hw_if_index = sw->hw_if_index;
678  mif->packet_template_index = (u8) ~ 0;
679  mif->is_passive = args->is_passive;
680  mif->group = args->group;
681  mif->bif_dev_instance = bif->dev_instance;
682  mif->mode = bif->mode;
683 
684  mif->is_long_timeout = args->is_long_timeout;
685  if (args->is_long_timeout)
687  else
689 
692  /*
693  * mif - bm->neighbors may be 0
694  * Left shift it by 1 bit to distinguish the valid entry that we actually
695  * store from the null entries
696  */
698  (uword) (((mif - bm->neighbors) << 1) | 1);
699  vec_add1 (bif->members, mif->sw_if_index);
700 
701  mif_hw = vnet_get_sup_hw_interface (vnm, mif->sw_if_index);
702 
703  /* Save the old mac */
704  memcpy (mif->persistent_hw_address, mif_hw->hw_address, 6);
705  bif_hw = vnet_get_sup_hw_interface (vnm, bif->sw_if_index);
706  if (bif->use_custom_mac)
707  {
709  bif->hw_address);
710  }
711  else
712  {
713  // bond interface gets the mac address from the first member
714  if (vec_len (bif->members) == 1)
715  {
716  memcpy (bif->hw_address, mif_hw->hw_address, 6);
718  mif_hw->hw_address);
719  }
720  else
721  {
722  // subsequent members gets the mac address of the bond interface
724  bif->hw_address);
725  }
726  }
727 
728  /* if there are secondary/virtual mac addrs, propagate to the member */
729  bond_member_add_del_mac_addrs (bif, mif->sw_if_index, 1 /* is_add */ );
730 
731  if (bif_hw->l2_if_count)
732  ethernet_set_flags (vnm, mif_hw->hw_if_index,
734  else
735  ethernet_set_flags (vnm, mif_hw->hw_if_index,
736  /*ETHERNET_INTERFACE_FLAG_DEFAULT_L3 */ 0);
737 
738  if (bif->mode == BOND_MODE_LACP)
739  {
740  if (bm->lacp_enable_disable)
741  (*bm->lacp_enable_disable) (vm, bif, mif, 1);
742  }
743  else if (mif->port_enabled)
744  {
746  }
747 
748  vec_foreach_index (thread_index, bm->per_thread_data)
749  {
751  thread_index);
752 
755 
756  vec_foreach_index (mif_if_index, ptd->per_port_queue)
757  {
758  ptd->per_port_queue[mif_if_index].n_buffers = 0;
759  }
760  }
761 
762  args->rv = vnet_feature_enable_disable ("device-input", "bond-input",
763  mif->sw_if_index, 1, 0, 0);
764 
765  if (args->rv)
766  {
767  args->error =
769  "Error encountered on input feature arc enable");
770  }
771 }
772 
773 static clib_error_t *
775  vlib_cli_command_t * cmd)
776 {
777  bond_add_member_args_t args = { 0 };
778  unformat_input_t _line_input, *line_input = &_line_input;
779  vnet_main_t *vnm = vnet_get_main ();
780 
781  /* Get a line of input. */
782  if (!unformat_user (input, unformat_line_input, line_input))
783  return clib_error_return (0, "Missing required arguments.");
784 
785  args.member = ~0;
786  args.group = ~0;
787  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
788  {
789  if (unformat (line_input, "%U %U",
790  unformat_vnet_sw_interface, vnm, &args.group,
791  unformat_vnet_sw_interface, vnm, &args.member))
792  ;
793  else if (unformat (line_input, "passive"))
794  args.is_passive = 1;
795  else if (unformat (line_input, "long-timeout"))
796  args.is_long_timeout = 1;
797  else
798  {
799  args.error = clib_error_return (0, "unknown input `%U'",
800  format_unformat_error, input);
801  break;
802  }
803  }
804  unformat_free (line_input);
805 
806  if (args.error)
807  return args.error;
808  if (args.group == ~0)
809  return clib_error_return (0, "Missing bond interface");
810  if (args.member == ~0)
811  return clib_error_return (0,
812  "please specify valid member interface name");
813 
814  bond_add_member (vm, &args);
815 
816  return args.error;
817 }
818 
819 /* *INDENT-OFF* */
820 VLIB_CLI_COMMAND (add_member_interface_command, static) = {
821  .path = "bond add",
822  .short_help = "bond add <BondEthernetx> <member-interface> "
823  "[passive] [long-timeout]",
825 };
826 /* *INDENT-ON* */
827 
828 void
830 {
831  bond_if_t *bif;
832  member_if_t *mif;
833 
835  if (!mif)
836  {
837  args->rv = VNET_API_ERROR_INVALID_INTERFACE;
838  args->error = clib_error_return (0, "interface was not a member");
839  return;
840  }
842  bond_delete_neighbor (vm, bif, mif);
843 }
844 
845 static clib_error_t *
847  vlib_cli_command_t * cmd)
848 {
849  bond_detach_member_args_t args = { 0 };
850  unformat_input_t _line_input, *line_input = &_line_input;
851  vnet_main_t *vnm = vnet_get_main ();
852 
853  /* Get a line of input. */
854  if (!unformat_user (input, unformat_line_input, line_input))
855  return clib_error_return (0, "Missing required arguments.");
856 
857  args.member = ~0;
858  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
859  {
860  if (unformat (line_input, "%U",
861  unformat_vnet_sw_interface, vnm, &args.member))
862  ;
863  else
864  {
865  args.error = clib_error_return (0, "unknown input `%U'",
866  format_unformat_error, input);
867  break;
868  }
869  }
870  unformat_free (line_input);
871 
872  if (args.error)
873  return args.error;
874  if (args.member == ~0)
875  return clib_error_return (0,
876  "please specify valid member interface name");
877 
878  bond_detach_member (vm, &args);
879 
880  return args.error;
881 }
882 
883 /* *INDENT-OFF* */
884 VLIB_CLI_COMMAND (detach_interface_command, static) = {
885  .path = "bond del",
886  .short_help = "bond del <member-interface>",
887  .function = detach_interface_command_fn,
888 };
889 /* *INDENT-ON* */
890 
891 static void
893 {
894  bond_main_t *bm = &bond_main;
895  bond_if_t *bif;
896 
897  vlib_cli_output (vm, "%-16s %-12s %-13s %-13s %-14s %s",
898  "interface name", "sw_if_index", "mode",
899  "load balance", "active members", "members");
900 
901  /* *INDENT-OFF* */
902  pool_foreach (bif, bm->interfaces,
903  ({
904  vlib_cli_output (vm, "%-16U %-12d %-13U %-13U %-14u %u",
905  format_bond_interface_name, bif->dev_instance,
906  bif->sw_if_index, format_bond_mode, bif->mode,
907  format_bond_load_balance, bif->lb,
908  vec_len (bif->active_members), vec_len (bif->members));
909  }));
910  /* *INDENT-ON* */
911 }
912 
913 static void
915 {
916  bond_main_t *bm = &bond_main;
917  bond_if_t *bif;
918  u32 *sw_if_index;
919 
920  /* *INDENT-OFF* */
921  pool_foreach (bif, bm->interfaces,
922  ({
923  vlib_cli_output (vm, "%U", format_bond_interface_name, bif->dev_instance);
924  vlib_cli_output (vm, " mode: %U",
925  format_bond_mode, bif->mode);
926  vlib_cli_output (vm, " load balance: %U",
927  format_bond_load_balance, bif->lb);
928  if (bif->gso)
929  vlib_cli_output (vm, " gso enable");
930  if (bif->mode == BOND_MODE_ROUND_ROBIN)
931  vlib_cli_output (vm, " last xmit member index: %u",
932  bif->lb_rr_last_index);
933  vlib_cli_output (vm, " number of active members: %d",
934  vec_len (bif->active_members));
935  vec_foreach (sw_if_index, bif->active_members)
936  {
937  vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
938  vnet_get_main (), *sw_if_index);
939  if (bif->mode == BOND_MODE_ACTIVE_BACKUP)
940  {
941  member_if_t *mif = bond_get_member_by_sw_if_index (*sw_if_index);
942  if (mif)
943  vlib_cli_output (vm, " weight: %u, is_local_numa: %u, "
944  "sw_if_index: %u", mif->weight,
945  mif->is_local_numa, mif->sw_if_index);
946  }
947  }
948  vlib_cli_output (vm, " number of members: %d", vec_len (bif->members));
949  vec_foreach (sw_if_index, bif->members)
950  {
951  vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name,
952  vnet_get_main (), *sw_if_index);
953  }
954  vlib_cli_output (vm, " device instance: %d", bif->dev_instance);
955  vlib_cli_output (vm, " interface id: %d", bif->id);
956  vlib_cli_output (vm, " sw_if_index: %d", bif->sw_if_index);
957  vlib_cli_output (vm, " hw_if_index: %d", bif->hw_if_index);
958  }));
959  /* *INDENT-ON* */
960 }
961 
962 static clib_error_t *
964  vlib_cli_command_t * cmd)
965 {
966  u8 details = 0;
967 
969  {
970  if (unformat (input, "details"))
971  details = 1;
972  else
973  {
974  return clib_error_return (0, "unknown input `%U'",
975  format_unformat_error, input);
976  }
977  }
978 
979  if (details)
980  show_bond_details (vm);
981  else
982  show_bond (vm);
983 
984  return 0;
985 }
986 
987 /* *INDENT-OFF* */
988 VLIB_CLI_COMMAND (show_bond_command, static) = {
989  .path = "show bond",
990  .short_help = "show bond [details]",
991  .function = show_bond_fn,
992 };
993 /* *INDENT-ON* */
994 
995 void
997 {
998  member_if_t *mif;
999  bond_if_t *bif;
1000  vnet_main_t *vnm;
1001  u32 old_weight;
1002 
1004  if (!mif)
1005  {
1006  args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1007  args->error = clib_error_return (0, "Interface not a member");
1008  return;
1009  }
1011  if (!bif)
1012  {
1013  args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1014  args->error = clib_error_return (0, "bond interface not found");
1015  return;
1016  }
1017  if (bif->mode != BOND_MODE_ACTIVE_BACKUP)
1018  {
1019  args->rv = VNET_API_ERROR_INVALID_ARGUMENT;
1020  args->error =
1021  clib_error_return (0, "Weight valid for active-backup only");
1022  return;
1023  }
1024 
1025  old_weight = mif->weight;
1026  mif->weight = args->weight;
1027  vnm = vnet_get_main ();
1028  /*
1029  * No need to sort the list if the affected member is not up (not in active
1030  * member set), active member count is 1, or the current member is already the
1031  * primary member and new weight > old weight.
1032  */
1033  if (!vnet_sw_interface_is_up (vnm, mif->sw_if_index) ||
1034  (vec_len (bif->active_members) == 1) ||
1035  ((bif->active_members[0] == mif->sw_if_index) &&
1036  (mif->weight >= old_weight)))
1037  return;
1038 
1039  bond_sort_members (bif);
1040 }
1041 
1042 static clib_error_t *
1044  vlib_cli_command_t * cmd)
1045 {
1046  bond_set_intf_weight_args_t args = { 0 };
1047  u32 sw_if_index = (u32) ~ 0;
1048  unformat_input_t _line_input, *line_input = &_line_input;
1049  vnet_main_t *vnm = vnet_get_main ();
1050  u8 weight_enter = 0;
1051  u32 weight = 0;
1052 
1053  /* Get a line of input. */
1054  if (!unformat_user (input, unformat_line_input, line_input))
1055  return clib_error_return (0, "Missing required arguments.");
1056 
1057  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1058  {
1059  if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1060  ;
1061  else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
1062  &sw_if_index))
1063  ;
1064  else if (unformat (line_input, "weight %u", &weight))
1065  weight_enter = 1;
1066  else
1067  {
1068  clib_error_return (0, "unknown input `%U'", format_unformat_error,
1069  input);
1070  break;
1071  }
1072  }
1073 
1074  unformat_free (line_input);
1075  if (sw_if_index == (u32) ~ 0)
1076  {
1077  args.rv = VNET_API_ERROR_INVALID_INTERFACE;
1078  clib_error_return (0, "Interface name is invalid!");
1079  }
1080  if (weight_enter == 0)
1081  {
1082  args.rv = VNET_API_ERROR_INVALID_ARGUMENT;
1083  clib_error_return (0, "weight missing");
1084  }
1085 
1086  args.sw_if_index = sw_if_index;
1087  args.weight = weight;
1088  bond_set_intf_weight (vm, &args);
1089 
1090  return args.error;
1091 }
1092 
1093 /* *INDENT-OFF* */
1094 VLIB_CLI_COMMAND(set_interface_bond_cmd, static) = {
1095  .path = "set interface bond",
1096  .short_help = "set interface bond <interface> | sw_if_index <idx>"
1097  " weight <value>",
1098  .function = bond_set_intf_cmd,
1099 };
1100 /* *INDENT-ON* */
1101 
1102 clib_error_t *
1104 {
1105  bond_main_t *bm = &bond_main;
1106 
1107  bm->vlib_main = vm;
1108  bm->vnet_main = vnet_get_main ();
1111  vlib_get_thread_main ()->n_vlib_mains - 1,
1113 
1114  return 0;
1115 }
1116 
1118 
1119 /*
1120  * fd.io coding-style-patch-verification: ON
1121  *
1122  * Local Variables:
1123  * eval: (c-set-style "gnu")
1124  * End:
1125  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
u8 is_local_numa
Definition: node.h:349
vnet_main_t * vnet_main
Definition: node.h:377
static uword vnet_sw_interface_is_up(vnet_main_t *vnm, u32 sw_if_index)
u8 gso
Definition: node.h:200
u32 dev_instance
Definition: node.h:178
u32 group
Definition: node.h:205
#define vec_foreach_index(var, v)
Iterate over vector indices.
void bond_create_if(vlib_main_t *vm, bond_create_if_args_t *args)
Definition: cli.c:377
static clib_error_t * add_member_interface_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:774
u32 hw_if_index
Definition: node.h:183
static uword unformat_bond_mode(unformat_input_t *input, va_list *args)
Definition: node.h:430
#define hash_set(h, key, value)
Definition: hash.h:255
lacp_stats_t ** stats
Definition: node.h:388
void bond_detach_member(vlib_main_t *vm, bond_detach_member_args_t *args)
Definition: cli.c:829
#define hash_unset(h, key)
Definition: hash.h:261
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface.c:378
#define ntohs(x)
Definition: af_xdp.bpf.c:29
member interface details struct
Definition: node.h:137
mac_address_t * secondary_addrs
Definition: ethernet.h:166
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
u8 lb
Definition: node.h:172
vnet_interface_main_t interface_main
Definition: vnet.h:59
clib_error_t * error
Definition: node.h:90
uword * id_used
Definition: node.h:367
clib_error_t * error
Definition: node.h:103
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
clib_error_t * error
Definition: node.h:120
u32 group
Definition: node.h:319
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:333
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:127
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
static bond_if_t * bond_get_bond_if_by_dev_instance(u32 dev_instance)
Definition: node.h:517
static bond_if_t * bond_get_bond_if_by_sw_if_index(u32 sw_if_index)
Definition: node.h:503
static clib_error_t * bond_delete_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:551
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:630
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap. ...
Definition: bitmap.h:167
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
u8 lacp_enabled
Definition: node.h:271
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
vlib_main_t * vm
Definition: in2out_ed.c:1582
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
lacp_enable_disable_func lacp_enable_disable
Definition: node.h:382
unformat_function_t unformat_vnet_sw_interface
u32 numa_node
Definition: main.h:251
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:520
u32 id
Definition: node.h:181
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:252
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
bond_per_thread_data_t * per_thread_data
Definition: node.h:386
u32 weight
Definition: node.h:248
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
static clib_error_t * bond_create_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:484
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define clib_memcpy(d, s, n)
Definition: string.h:180
u8 weight
Definition: fib_types.api:120
static void bond_sort_members(bond_if_t *bif)
Definition: cli.c:122
ethernet_main_t ethernet_main
Definition: init.c:45
vlib_node_registration_t bond_process_node
(constructor) VLIB_REGISTER_NODE (bond_process_node)
Definition: device.c:834
u8 numa_only
Definition: node.h:199
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:513
static clib_error_t * detach_interface_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:846
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
u8 use_custom_mac
Definition: node.h:207
u32 sw_if_index
Definition: node.h:184
vnet_hw_interface_flags_t flags
Definition: interface.h:537
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
void bond_add_member(vlib_main_t *vm, bond_add_member_args_t *args)
Definition: cli.c:599
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_main_t * vlib_main
Definition: node.h:376
#define ALWAYS_ASSERT(truth)
unsigned int u32
Definition: types.h:88
u8 is_long_timeout
Definition: node.h:224
#define MIN(x, y)
Definition: node.h:31
unformat_function_t unformat_line_input
Definition: format.h:283
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:63
#define LACP_LONG_TIMOUT_TIME
Definition: node.h:28
#define hash_get(h, key)
Definition: hash.h:249
vnet_device_class_t bond_dev_class
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:1015
u8 mode
Definition: node.h:328
struct _unformat_input_t unformat_input_t
static uword unformat_bond_load_balance(unformat_input_t *input, va_list *args)
Definition: node.h:462
uword * member_by_sw_if_index
Definition: node.h:384
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:302
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:875
#define LACP_SHORT_TIMOUT_TIME
Definition: node.h:26
static clib_error_t * show_bond_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:963
bond_if_t * interfaces
Definition: node.h:364
u8 is_passive
Definition: node.h:251
clib_error_t * error
Definition: node.h:111
member_if_t * neighbors
Definition: node.h:370
u32 partner_state
Definition: node.h:357
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
u8 mode
Definition: node.h:171
u32 * members
Definition: node.h:187
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
uword * bond_by_sw_if_index
Definition: node.h:373
f32 ttl_in_seconds
Definition: node.h:221
u32 * active_members
Definition: node.h:190
clib_spinlock_t lockp
Definition: node.h:210
#define ETHERNET_INTERFACE_FLAG_ACCEPT_ALL
Definition: ethernet.h:152
int bond_dump_member_ifs(member_interface_details_t **out_memberifs, u32 bond_sw_if_index)
Definition: cli.c:212
#define ARRAY_LEN(x)
Definition: clib.h:67
string name[64]
Definition: ip.api:44
static int bond_member_sort(void *a1, void *a2)
Definition: cli.c:73
BOND interface details struct.
Definition: node.h:124
u8 packet_template_index
Definition: node.h:230
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:158
clib_error_t * stat_segment_deregister_state_counter(u32 index)
Definition: stat_segment.c:856
void bond_disable_collecting_distributing(vlib_main_t *vm, member_if_t *mif)
Definition: cli.c:26
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:233
static void bond_delete_neighbor(vlib_main_t *vm, bond_if_t *bif, member_if_t *mif)
Definition: cli.c:283
#define ASSERT(truth)
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:696
u8 lacp_plugin_loaded
Definition: node.h:380
bond_main_t bond_main
Definition: node.c:25
static member_if_t * bond_get_member_by_sw_if_index(u32 sw_if_index)
Definition: node.h:525
#define vec_insert_elts(V, E, N, M)
Insert N vector elements starting at element M, insert given elements (no header, unspecified alignme...
Definition: vec.h:833
#define vec_append(v1, v2)
Append v2 after v1.
Definition: vec.h:890
#define clib_bitmap_free(v)
Free a bitmap.
Definition: bitmap.h:92
vl_api_ip4_address_t hi
Definition: arp.api:37
word n_numa_members
Definition: node.h:203
ethernet_interface_t * ethernet_get_interface(ethernet_main_t *em, u32 hw_if_index)
Definition: interface.c:967
static clib_error_t * bond_set_intf_cmd(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:1043
static void show_bond(vlib_main_t *vm)
Definition: cli.c:892
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u8 persistent_hw_address[6]
Definition: node.h:215
clib_error_t * ethernet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, const u8 *address, u32 *hw_if_index_return, ethernet_flag_change_function_t flag_change)
Definition: interface.c:331
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, vnet_hw_interface_flags_t flags)
Definition: interface.c:498
lacp_port_info_t actor_admin
Definition: node.h:259
uword * port_number_bitmap
Definition: node.h:206
u64 uword
Definition: types.h:112
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:1055
void bond_set_intf_weight(vlib_main_t *vm, bond_set_intf_weight_args_t *args)
Definition: cli.c:996
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
vnet_sw_interface_t * sw_interfaces
Definition: interface.h:872
static void show_bond_details(vlib_main_t *vm)
Definition: cli.c:914
u8 port_enabled
Definition: node.h:265
int bond_delete_if(vlib_main_t *vm, u32 sw_if_index)
Definition: cli.c:336
u32 sw_if_index
Definition: node.h:218
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, vnet_sw_interface_flags_t flags)
Definition: interface.c:507
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
#define vec_foreach(var, vec)
Vector iterator.
u32 actor_state
Definition: node.h:358
u8 hw_address[6]
Definition: node.h:208
u32 hw_if_index
Definition: node.h:245
int bond_dump_ifs(bond_interface_details_t **out_bondifs)
Definition: cli.c:177
clib_error_t * bond_cli_init(vlib_main_t *vm)
Definition: cli.c:1103
clib_error_t * vnet_hw_interface_add_del_mac_address(vnet_main_t *vnm, u32 hw_if_index, const u8 *mac_address, u8 is_add)
Definition: interface.c:1460
clib_error_t * vnet_hw_interface_change_mac_address(vnet_main_t *vnm, u32 hw_if_index, const u8 *mac_address)
Definition: interface.c:1548
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
u8 * last_marker_pkt
Definition: node.h:242
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:104
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:33
clib_error_t * stat_segment_register_state_counter(u8 *name, u32 *index)
Definition: stat_segment.c:822
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
u8 * last_rx_pkt
Definition: node.h:239
static void bond_member_add_del_mac_addrs(bond_if_t *bif, u32 mif_sw_if_index, u8 is_add)
Definition: cli.c:263
int vnet_feature_enable_disable(const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: feature.c:303
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
bond_per_port_queue_t * per_port_queue
Definition: node.h:165
u32 ethernet_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:426
u32 bif_dev_instance
Definition: node.h:323
void bond_enable_collecting_distributing(vlib_main_t *vm, member_if_t *mif)
Definition: cli.c:134