2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/ip/lookup.h>
17 #include <vnet/dpo/load_balance.h>
18 #include <vnet/dpo/load_balance_map.h>
19 #include <vnet/dpo/drop_dpo.h>
20 #include <vppinfra/math.h> /* for fabs */
21 #include <vnet/adj/adj.h>
22 #include <vnet/adj/adj_internal.h>
23 #include <vnet/fib/fib_urpf_list.h>
24 #include <vnet/bier/bier_fwd.h>
27 * distribution error tolerance for load-balancing
29 const f64 multipath_next_hop_error_tolerance = 0.1;
34 #define LB_DBG(_lb, _fmt, _args...) \
37 clib_warning("lb:[%s]:" _fmt, \
38 load_balance_format(load_balance_get_index((_lb)), \
44 #define LB_DBG(_p, _fmt, _args...)
49 * Pool of all DPOs. It's not static so the DP can have fast access
51 load_balance_t *load_balance_pool;
54 * The one instance of load-balance main
56 load_balance_main_t load_balance_main = {
59 .stat_segment_name = "/net/route/to",
63 .stat_segment_name = "/net/route/via",
68 load_balance_get_multipath_tolerance (void)
70 return (multipath_next_hop_error_tolerance);
74 load_balance_get_index (const load_balance_t *lb)
76 return (lb - load_balance_pool);
79 static inline dpo_id_t*
80 load_balance_get_buckets (load_balance_t *lb)
82 if (LB_HAS_INLINE_BUCKETS(lb))
84 return (lb->lb_buckets_inline);
88 return (lb->lb_buckets);
92 static load_balance_t *
93 load_balance_alloc_i (void)
97 pool_get_aligned(load_balance_pool, lb, CLIB_CACHE_LINE_BYTES);
98 memset(lb, 0, sizeof(*lb));
100 lb->lb_map = INDEX_INVALID;
101 lb->lb_urpf = INDEX_INVALID;
102 vlib_validate_combined_counter(&(load_balance_main.lbm_to_counters),
103 load_balance_get_index(lb));
104 vlib_validate_combined_counter(&(load_balance_main.lbm_via_counters),
105 load_balance_get_index(lb));
106 vlib_zero_combined_counter(&(load_balance_main.lbm_to_counters),
107 load_balance_get_index(lb));
108 vlib_zero_combined_counter(&(load_balance_main.lbm_via_counters),
109 load_balance_get_index(lb));
115 load_balance_format (index_t lbi,
116 load_balance_format_flags_t flags,
120 vlib_counter_t to, via;
125 lb = load_balance_get(lbi);
126 vlib_get_combined_counter(&(load_balance_main.lbm_to_counters), lbi, &to);
127 vlib_get_combined_counter(&(load_balance_main.lbm_via_counters), lbi, &via);
128 buckets = load_balance_get_buckets(lb);
130 s = format(s, "%U: ", format_dpo_type, DPO_LOAD_BALANCE);
131 s = format(s, "[proto:%U ", format_dpo_proto, lb->lb_proto);
132 s = format(s, "index:%d buckets:%d ", lbi, lb->lb_n_buckets);
133 s = format(s, "uRPF:%d ", lb->lb_urpf);
134 s = format(s, "to:[%Ld:%Ld]", to.packets, to.bytes);
135 if (0 != via.packets)
137 s = format(s, " via:[%Ld:%Ld]",
138 via.packets, via.bytes);
142 if (INDEX_INVALID != lb->lb_map)
144 s = format(s, "\n%U%U",
145 format_white_space, indent+4,
146 format_load_balance_map, lb->lb_map, indent+4);
148 for (i = 0; i < lb->lb_n_buckets; i++)
150 s = format(s, "\n%U[%d] %U",
151 format_white_space, indent+2,
154 &buckets[i], indent+6);
160 format_load_balance (u8 * s, va_list * args)
162 index_t lbi = va_arg(*args, index_t);
163 load_balance_format_flags_t flags = va_arg(*args, load_balance_format_flags_t);
165 return (load_balance_format(lbi, flags, 0, s));
168 format_load_balance_dpo (u8 * s, va_list * args)
170 index_t lbi = va_arg(*args, index_t);
171 u32 indent = va_arg(*args, u32);
173 return (load_balance_format(lbi, LOAD_BALANCE_FORMAT_DETAIL, indent, s));
177 static load_balance_t *
178 load_balance_create_i (u32 num_buckets,
179 dpo_proto_t lb_proto,
180 flow_hash_config_t fhc)
184 lb = load_balance_alloc_i();
185 lb->lb_hash_config = fhc;
186 lb->lb_n_buckets = num_buckets;
187 lb->lb_n_buckets_minus_1 = num_buckets-1;
188 lb->lb_proto = lb_proto;
190 if (!LB_HAS_INLINE_BUCKETS(lb))
192 vec_validate_aligned(lb->lb_buckets,
193 lb->lb_n_buckets - 1,
194 CLIB_CACHE_LINE_BYTES);
197 LB_DBG(lb, "create");
203 load_balance_create (u32 n_buckets,
204 dpo_proto_t lb_proto,
205 flow_hash_config_t fhc)
207 return (load_balance_get_index(load_balance_create_i(n_buckets, lb_proto, fhc)));
211 load_balance_set_bucket_i (load_balance_t *lb,
214 const dpo_id_t *next)
216 dpo_stack(DPO_LOAD_BALANCE, lb->lb_proto, &buckets[bucket], next);
220 load_balance_set_bucket (index_t lbi,
222 const dpo_id_t *next)
227 lb = load_balance_get(lbi);
228 buckets = load_balance_get_buckets(lb);
230 ASSERT(bucket < lb->lb_n_buckets);
232 load_balance_set_bucket_i(lb, bucket, buckets, next);
236 load_balance_is_drop (const dpo_id_t *dpo)
240 if (DPO_LOAD_BALANCE != dpo->dpoi_type)
243 lb = load_balance_get(dpo->dpoi_index);
245 if (1 == lb->lb_n_buckets)
247 return (dpo_is_drop(load_balance_get_bucket_i(lb, 0)));
253 load_balance_n_buckets (index_t lbi)
257 lb = load_balance_get(lbi);
259 return (lb->lb_n_buckets);
263 load_balance_set_fib_entry_flags (index_t lbi,
264 fib_entry_flag_t flags)
268 lb = load_balance_get(lbi);
269 lb->lb_fib_entry_flags = flags;
274 load_balance_set_urpf (index_t lbi,
280 lb = load_balance_get(lbi);
283 * packets in flight we see this change. but it's atomic, so :P
288 fib_urpf_list_unlock(old);
289 fib_urpf_list_lock(urpf);
293 load_balance_get_urpf (index_t lbi)
297 lb = load_balance_get(lbi);
299 return (lb->lb_urpf);
303 load_balance_get_bucket (index_t lbi,
308 lb = load_balance_get(lbi);
310 return (load_balance_get_bucket_i(lb, bucket));
314 next_hop_sort_by_weight (const load_balance_path_t * n1,
315 const load_balance_path_t * n2)
317 return ((int) n1->path_weight - (int) n2->path_weight);
320 /* Given next hop vector is over-written with normalized one with sorted weights and
321 with weights corresponding to the number of adjacencies for each next hop.
322 Returns number of adjacencies in block. */
324 ip_multipath_normalize_next_hops (const load_balance_path_t * raw_next_hops,
325 load_balance_path_t ** normalized_next_hops,
327 f64 multipath_next_hop_error_tolerance)
329 load_balance_path_t * nhs;
330 uword n_nhs, n_adj, n_adj_left, i, sum_weight;
333 n_nhs = vec_len (raw_next_hops);
338 /* Allocate enough space for 2 copies; we'll use second copy to save original weights. */
339 nhs = *normalized_next_hops;
340 vec_validate (nhs, 2*n_nhs - 1);
342 /* Fast path: 1 next hop in block. */
346 nhs[0] = raw_next_hops[0];
347 nhs[0].path_weight = 1;
355 int cmp = next_hop_sort_by_weight (&raw_next_hops[0], &raw_next_hops[1]) < 0;
358 nhs[0] = raw_next_hops[cmp];
359 nhs[1] = raw_next_hops[cmp ^ 1];
361 /* Fast path: equal cost multipath with 2 next hops. */
362 if (nhs[0].path_weight == nhs[1].path_weight)
364 nhs[0].path_weight = nhs[1].path_weight = 1;
372 clib_memcpy (nhs, raw_next_hops, n_nhs * sizeof (raw_next_hops[0]));
373 qsort (nhs, n_nhs, sizeof (nhs[0]), (void *) next_hop_sort_by_weight);
376 /* Find total weight to normalize weights. */
378 for (i = 0; i < n_nhs; i++)
379 sum_weight += nhs[i].path_weight;
381 /* In the unlikely case that all weights are given as 0, set them all to 1. */
384 for (i = 0; i < n_nhs; i++)
385 nhs[i].path_weight = 1;
389 /* Save copies of all next hop weights to avoid being overwritten in loop below. */
390 for (i = 0; i < n_nhs; i++)
391 nhs[n_nhs + i].path_weight = nhs[i].path_weight;
393 /* Try larger and larger power of 2 sized adjacency blocks until we
394 find one where traffic flows to within 1% of specified weights. */
395 for (n_adj = max_pow2 (n_nhs); ; n_adj *= 2)
399 norm = n_adj / ((f64) sum_weight);
401 for (i = 0; i < n_nhs; i++)
403 f64 nf = nhs[n_nhs + i].path_weight * norm; /* use saved weights */
404 word n = flt_round_nearest (nf);
406 n = n > n_adj_left ? n_adj_left : n;
408 error += fabs (nf - n);
409 nhs[i].path_weight = n;
411 if (0 == nhs[i].path_weight)
414 * when the weight skew is high (norm is small) and n == nf.
415 * without this correction the path with a low weight would have
416 * no represenation in the load-balanace - don't want that.
417 * If the weight skew is high so the load-balance has many buckets
418 * to allow it. pays ya money takes ya choice.
425 nhs[0].path_weight += n_adj_left;
427 /* Less than 5% average error per adjacency with this size adjacency block? */
428 if (error <= multipath_next_hop_error_tolerance*n_adj)
430 /* Truncate any next hops with zero weight. */
437 /* Save vector for next call. */
438 *normalized_next_hops = nhs;
439 *sum_weight_in = sum_weight;
443 static load_balance_path_t *
444 load_balance_multipath_next_hop_fixup (const load_balance_path_t *nhs,
445 dpo_proto_t drop_proto)
447 if (0 == vec_len(nhs))
449 load_balance_path_t *new_nhs = NULL, *nh;
452 * we need something for the load-balance. so use the drop
454 vec_add2(new_nhs, nh, 1);
457 dpo_copy(&nh->path_dpo, drop_dpo_get(drop_proto));
466 * Fill in adjacencies in block based on corresponding
467 * next hop adjacencies.
470 load_balance_fill_buckets (load_balance_t *lb,
471 load_balance_path_t *nhs,
475 load_balance_path_t * nh;
481 * the next-hops have normalised weights. that means their sum is the number
482 * of buckets we need to fill.
484 vec_foreach (nh, nhs)
486 for (ii = 0; ii < nh->path_weight; ii++)
488 ASSERT(bucket < n_buckets);
489 load_balance_set_bucket_i(lb, bucket++, buckets, &nh->path_dpo);
495 load_balance_set_n_buckets (load_balance_t *lb,
498 lb->lb_n_buckets = n_buckets;
499 lb->lb_n_buckets_minus_1 = n_buckets-1;
503 load_balance_multipath_update (const dpo_id_t *dpo,
504 const load_balance_path_t * raw_nhs,
505 load_balance_flags_t flags)
507 load_balance_path_t *nh, *nhs, *fixed_nhs;
508 u32 sum_of_weights, n_buckets, ii;
509 index_t lbmi, old_lbmi;
515 ASSERT(DPO_LOAD_BALANCE == dpo->dpoi_type);
516 lb = load_balance_get(dpo->dpoi_index);
517 fixed_nhs = load_balance_multipath_next_hop_fixup(raw_nhs, lb->lb_proto);
519 ip_multipath_normalize_next_hops((NULL == fixed_nhs ?
524 multipath_next_hop_error_tolerance);
526 ASSERT (n_buckets >= vec_len (raw_nhs));
529 * Save the old load-balance map used, and get a new one if required.
531 old_lbmi = lb->lb_map;
532 if (flags & LOAD_BALANCE_FLAG_USES_MAP)
534 lbmi = load_balance_map_add_or_lock(n_buckets, sum_of_weights, nhs);
538 lbmi = INDEX_INVALID;
541 if (0 == lb->lb_n_buckets)
544 * first time initialisation. no packets inflight, so we can write
547 load_balance_set_n_buckets(lb, n_buckets);
549 if (!LB_HAS_INLINE_BUCKETS(lb))
550 vec_validate_aligned(lb->lb_buckets,
551 lb->lb_n_buckets - 1,
552 CLIB_CACHE_LINE_BYTES);
554 load_balance_fill_buckets(lb, nhs,
555 load_balance_get_buckets(lb),
562 * This is a modification of an existing load-balance.
563 * We need to ensure that packets inflight see a consistent state, that
564 * is the number of reported buckets the LB has (read from
565 * lb_n_buckets_minus_1) is not more than it actually has. So if the
566 * number of buckets is increasing, we must update the bucket array first,
567 * then the reported number. vice-versa if the number of buckets goes down.
569 if (n_buckets == lb->lb_n_buckets)
572 * no change in the number of buckets. we can simply fill what
573 * is new over what is old.
575 load_balance_fill_buckets(lb, nhs,
576 load_balance_get_buckets(lb),
580 else if (n_buckets > lb->lb_n_buckets)
583 * we have more buckets. the old load-balance map (if there is one)
584 * will remain valid, i.e. mapping to indices within range, so we
587 if (n_buckets > LB_NUM_INLINE_BUCKETS &&
588 lb->lb_n_buckets <= LB_NUM_INLINE_BUCKETS)
591 * the new increased number of buckets is crossing the threshold
592 * from the inline storage to out-line. Alloc the outline buckets
593 * first, then fixup the number. then reset the inlines.
595 ASSERT(NULL == lb->lb_buckets);
596 vec_validate_aligned(lb->lb_buckets,
598 CLIB_CACHE_LINE_BYTES);
600 load_balance_fill_buckets(lb, nhs,
603 CLIB_MEMORY_BARRIER();
604 load_balance_set_n_buckets(lb, n_buckets);
606 CLIB_MEMORY_BARRIER();
608 for (ii = 0; ii < LB_NUM_INLINE_BUCKETS; ii++)
610 dpo_reset(&lb->lb_buckets_inline[ii]);
615 if (n_buckets <= LB_NUM_INLINE_BUCKETS)
618 * we are not crossing the threshold and it's still inline buckets.
619 * we can write the new on the old..
621 load_balance_fill_buckets(lb, nhs,
622 load_balance_get_buckets(lb),
624 CLIB_MEMORY_BARRIER();
625 load_balance_set_n_buckets(lb, n_buckets);
630 * we are not crossing the threshold. We need a new bucket array to
631 * hold the increased number of choices.
633 dpo_id_t *new_buckets, *old_buckets, *tmp_dpo;
636 old_buckets = load_balance_get_buckets(lb);
638 vec_validate_aligned(new_buckets,
640 CLIB_CACHE_LINE_BYTES);
642 load_balance_fill_buckets(lb, nhs, new_buckets, n_buckets);
643 CLIB_MEMORY_BARRIER();
644 lb->lb_buckets = new_buckets;
645 CLIB_MEMORY_BARRIER();
646 load_balance_set_n_buckets(lb, n_buckets);
648 vec_foreach(tmp_dpo, old_buckets)
652 vec_free(old_buckets);
657 * buckets fixed. ready for the MAP update.
664 * bucket size shrinkage.
665 * Any map we have will be based on the old
666 * larger number of buckets, so will be translating to indices
667 * out of range. So the new MAP must be installed first.
670 CLIB_MEMORY_BARRIER();
673 if (n_buckets <= LB_NUM_INLINE_BUCKETS &&
674 lb->lb_n_buckets > LB_NUM_INLINE_BUCKETS)
677 * the new decreased number of buckets is crossing the threshold
678 * from out-line storage to inline:
679 * 1 - Fill the inline buckets,
680 * 2 - fixup the number (and this point the inline buckets are
682 * 3 - free the outline buckets
684 load_balance_fill_buckets(lb, nhs,
685 lb->lb_buckets_inline,
687 CLIB_MEMORY_BARRIER();
688 load_balance_set_n_buckets(lb, n_buckets);
689 CLIB_MEMORY_BARRIER();
691 vec_foreach(tmp_dpo, lb->lb_buckets)
695 vec_free(lb->lb_buckets);
700 * not crossing the threshold.
701 * 1 - update the number to the smaller size
702 * 2 - write the new buckets
703 * 3 - reset those no longer used.
708 old_n_buckets = lb->lb_n_buckets;
709 buckets = load_balance_get_buckets(lb);
711 load_balance_set_n_buckets(lb, n_buckets);
712 CLIB_MEMORY_BARRIER();
714 load_balance_fill_buckets(lb, nhs,
718 for (ii = n_buckets; ii < old_n_buckets; ii++)
720 dpo_reset(&buckets[ii]);
726 vec_foreach (nh, nhs)
728 dpo_reset(&nh->path_dpo);
733 load_balance_map_unlock(old_lbmi);
737 load_balance_lock (dpo_id_t *dpo)
741 lb = load_balance_get(dpo->dpoi_index);
747 load_balance_destroy (load_balance_t *lb)
752 buckets = load_balance_get_buckets(lb);
754 for (i = 0; i < lb->lb_n_buckets; i++)
756 dpo_reset(&buckets[i]);
759 LB_DBG(lb, "destroy");
760 if (!LB_HAS_INLINE_BUCKETS(lb))
762 vec_free(lb->lb_buckets);
765 fib_urpf_list_unlock(lb->lb_urpf);
766 load_balance_map_unlock(lb->lb_map);
768 pool_put(load_balance_pool, lb);
772 load_balance_unlock (dpo_id_t *dpo)
776 lb = load_balance_get(dpo->dpoi_index);
780 if (0 == lb->lb_locks)
782 load_balance_destroy(lb);
787 load_balance_mem_show (void)
789 fib_show_memory_usage("load-balance",
790 pool_elts(load_balance_pool),
791 pool_len(load_balance_pool),
792 sizeof(load_balance_t));
793 load_balance_map_show_mem();
796 const static dpo_vft_t lb_vft = {
797 .dv_lock = load_balance_lock,
798 .dv_unlock = load_balance_unlock,
799 .dv_format = format_load_balance_dpo,
800 .dv_mem_show = load_balance_mem_show,
804 * @brief The per-protocol VLIB graph nodes that are assigned to a load-balance
807 * this means that these graph nodes are ones from which a load-balance is the
808 * parent object in the DPO-graph.
810 * We do not list all the load-balance nodes, such as the *-lookup. instead
811 * we are relying on the correct use of the .sibling_of field when setting
812 * up these sibling nodes.
814 const static char* const load_balance_ip4_nodes[] =
819 const static char* const load_balance_ip6_nodes[] =
824 const static char* const load_balance_mpls_nodes[] =
829 const static char* const load_balance_l2_nodes[] =
834 const static char* const load_balance_nsh_nodes[] =
839 const static char* const load_balance_bier_nodes[] =
844 const static char* const * const load_balance_nodes[DPO_PROTO_NUM] =
846 [DPO_PROTO_IP4] = load_balance_ip4_nodes,
847 [DPO_PROTO_IP6] = load_balance_ip6_nodes,
848 [DPO_PROTO_MPLS] = load_balance_mpls_nodes,
849 [DPO_PROTO_ETHERNET] = load_balance_l2_nodes,
850 [DPO_PROTO_NSH] = load_balance_nsh_nodes,
851 [DPO_PROTO_BIER] = load_balance_bier_nodes,
855 load_balance_module_init (void)
859 dpo_register(DPO_LOAD_BALANCE, &lb_vft, load_balance_nodes);
862 * Special LB with index zero. we need to define this since the v4 mtrie
863 * assumes an index of 0 implies the ply is empty. therefore all 'real'
864 * adjs need a non-zero index.
865 * This should never be used, but just in case, stack it on a drop.
867 lbi = load_balance_create(1, DPO_PROTO_IP4, 0);
868 load_balance_set_bucket(lbi, 0, drop_dpo_get(DPO_PROTO_IP4));
870 load_balance_map_module_init();
873 static clib_error_t *
874 load_balance_show (vlib_main_t * vm,
875 unformat_input_t * input,
876 vlib_cli_command_t * cmd)
878 index_t lbi = INDEX_INVALID;
880 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
882 if (unformat (input, "%d", &lbi))
888 if (INDEX_INVALID != lbi)
890 vlib_cli_output (vm, "%U", format_load_balance, lbi,
891 LOAD_BALANCE_FORMAT_DETAIL);
897 pool_foreach(lb, load_balance_pool,
899 vlib_cli_output (vm, "%U", format_load_balance,
900 load_balance_get_index(lb),
901 LOAD_BALANCE_FORMAT_NONE);
908 VLIB_CLI_COMMAND (load_balance_show_command, static) = {
909 .path = "show load-balance",
910 .short_help = "show load-balance [<index>]",
911 .function = load_balance_show,
916 ip_flow_hash (void *data)
918 ip4_header_t *iph = (ip4_header_t *) data;
920 if ((iph->ip_version_and_header_length & 0xF0) == 0x40)
921 return ip4_compute_flow_hash (iph, IP_FLOW_HASH_DEFAULT);
923 return ip6_compute_flow_hash ((ip6_header_t *) iph, IP_FLOW_HASH_DEFAULT);
929 return (*((u64 *) m) & 0xffffffffffff);
933 l2_flow_hash (vlib_buffer_t * b0)
935 ethernet_header_t *eh;
937 uword is_ip, eh_size;
940 eh = vlib_buffer_get_current (b0);
941 eh_type = clib_net_to_host_u16 (eh->type);
942 eh_size = ethernet_buffer_header_size (b0);
944 is_ip = (eh_type == ETHERNET_TYPE_IP4 || eh_type == ETHERNET_TYPE_IP6);
946 /* since we have 2 cache lines, use them */
948 a = ip_flow_hash ((u8 *) vlib_buffer_get_current (b0) + eh_size);
952 b = mac_to_u64 ((u8 *) eh->dst_address);
953 c = mac_to_u64 ((u8 *) eh->src_address);
954 hash_mix64 (a, b, c);
959 typedef struct load_balance_trace_t_
962 } load_balance_trace_t;
965 load_balance_inline (vlib_main_t * vm,
966 vlib_node_runtime_t * node,
967 vlib_frame_t * frame,
970 u32 n_left_from, next_index, *from, *to_next;
972 from = vlib_frame_vector_args (frame);
973 n_left_from = frame->n_vectors;
975 next_index = node->cached_next_index;
977 while (n_left_from > 0)
981 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
983 while (n_left_from > 0 && n_left_to_next > 0)
986 u32 bi0, lbi0, next0;
987 const dpo_id_t *dpo0;
988 const load_balance_t *lb0;
997 b0 = vlib_get_buffer (vm, bi0);
999 /* lookup dst + src mac */
1000 lbi0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
1001 lb0 = load_balance_get(lbi0);
1005 vnet_buffer(b0)->ip.flow_hash = l2_flow_hash(b0);
1010 const bier_hdr_t *bh0 = vlib_buffer_get_current(b0);
1011 vnet_buffer(b0)->ip.flow_hash = bier_compute_flow_hash(bh0);
1014 dpo0 = load_balance_get_bucket_i(lb0,
1015 vnet_buffer(b0)->ip.flow_hash &
1016 (lb0->lb_n_buckets_minus_1));
1018 next0 = dpo0->dpoi_next_node;
1019 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
1021 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1023 load_balance_trace_t *tr = vlib_add_trace (vm, node, b0,
1025 tr->lb_index = lbi0;
1027 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1028 n_left_to_next, bi0, next0);
1031 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1034 return frame->n_vectors;
1038 l2_load_balance (vlib_main_t * vm,
1039 vlib_node_runtime_t * node,
1040 vlib_frame_t * frame)
1042 return (load_balance_inline(vm, node, frame, 1));
1046 format_l2_load_balance_trace (u8 * s, va_list * args)
1048 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1049 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1050 load_balance_trace_t *t = va_arg (*args, load_balance_trace_t *);
1052 s = format (s, "L2-load-balance: index %d", t->lb_index);
1059 VLIB_REGISTER_NODE (l2_load_balance_node) = {
1060 .function = l2_load_balance,
1061 .name = "l2-load-balance",
1062 .vector_size = sizeof (u32),
1064 .format_trace = format_l2_load_balance_trace,
1072 nsh_load_balance (vlib_main_t * vm,
1073 vlib_node_runtime_t * node,
1074 vlib_frame_t * frame)
1076 u32 n_left_from, next_index, *from, *to_next;
1078 from = vlib_frame_vector_args (frame);
1079 n_left_from = frame->n_vectors;
1081 next_index = node->cached_next_index;
1083 while (n_left_from > 0)
1087 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1089 while (n_left_from > 0 && n_left_to_next > 0)
1092 u32 bi0, lbi0, next0, *nsh0;
1093 const dpo_id_t *dpo0;
1094 const load_balance_t *lb0;
1101 n_left_to_next -= 1;
1103 b0 = vlib_get_buffer (vm, bi0);
1105 lbi0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
1106 lb0 = load_balance_get(lbi0);
1108 /* SPI + SI are the second word of the NSH header */
1109 nsh0 = vlib_buffer_get_current (b0);
1110 vnet_buffer(b0)->ip.flow_hash = nsh0[1] % lb0->lb_n_buckets;
1112 dpo0 = load_balance_get_bucket_i(lb0,
1113 vnet_buffer(b0)->ip.flow_hash &
1114 (lb0->lb_n_buckets_minus_1));
1116 next0 = dpo0->dpoi_next_node;
1117 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
1119 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1121 load_balance_trace_t *tr = vlib_add_trace (vm, node, b0,
1123 tr->lb_index = lbi0;
1125 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1126 n_left_to_next, bi0, next0);
1129 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1132 return frame->n_vectors;
1136 format_nsh_load_balance_trace (u8 * s, va_list * args)
1138 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1139 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1140 load_balance_trace_t *t = va_arg (*args, load_balance_trace_t *);
1142 s = format (s, "NSH-load-balance: index %d", t->lb_index);
1149 VLIB_REGISTER_NODE (nsh_load_balance_node) = {
1150 .function = nsh_load_balance,
1151 .name = "nsh-load-balance",
1152 .vector_size = sizeof (u32),
1154 .format_trace = format_nsh_load_balance_trace,
1162 format_bier_load_balance_trace (u8 * s, va_list * args)
1164 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1165 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1166 load_balance_trace_t *t = va_arg (*args, load_balance_trace_t *);
1168 s = format (s, "BIER-load-balance: index %d", t->lb_index);
1173 bier_load_balance (vlib_main_t * vm,
1174 vlib_node_runtime_t * node,
1175 vlib_frame_t * frame)
1177 return (load_balance_inline(vm, node, frame, 0));
1183 VLIB_REGISTER_NODE (bier_load_balance_node) = {
1184 .function = bier_load_balance,
1185 .name = "bier-load-balance",
1186 .vector_size = sizeof (u32),
1188 .format_trace = format_bier_load_balance_trace,
1189 .sibling_of = "mpls-load-balance",