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/dpo/load_balance.h>
17 #include <vnet/dpo/load_balance_map.h>
18 #include <vnet/dpo/drop_dpo.h>
19 #include <vppinfra/math.h> /* for fabs */
20 #include <vnet/adj/adj.h>
21 #include <vnet/adj/adj_internal.h>
22 #include <vnet/fib/fib_urpf_list.h>
23 #include <vnet/bier/bier_fwd.h>
24 #include <vnet/fib/mpls_fib.h>
25 #include <vnet/ip/ip4_inlines.h>
26 #include <vnet/ip/ip6_inlines.h>
29 * distribution error tolerance for load-balancing
31 const f64 multipath_next_hop_error_tolerance = 0.1;
33 static const char *load_balance_attr_names[] = LOAD_BALANCE_ATTR_NAMES;
38 vlib_log_class_t load_balance_logger;
40 #define LB_DBG(_lb, _fmt, _args...) \
42 vlib_log_debug(load_balance_logger, \
44 format_load_balance, load_balance_get_index(_lb), \
45 LOAD_BALANCE_FORMAT_NONE, \
50 * Pool of all DPOs. It's not static so the DP can have fast access
52 load_balance_t *load_balance_pool;
55 * The one instance of load-balance main
57 load_balance_main_t load_balance_main = {
60 .stat_segment_name = "/net/route/to",
64 .stat_segment_name = "/net/route/via",
69 load_balance_get_multipath_tolerance (void)
71 return (multipath_next_hop_error_tolerance);
75 load_balance_get_index (const load_balance_t *lb)
77 return (lb - load_balance_pool);
80 static inline dpo_id_t*
81 load_balance_get_buckets (load_balance_t *lb)
83 if (LB_HAS_INLINE_BUCKETS(lb))
85 return (lb->lb_buckets_inline);
89 return (lb->lb_buckets);
93 static load_balance_t *
94 load_balance_alloc_i (void)
97 u8 need_barrier_sync = 0;
98 vlib_main_t *vm = vlib_get_main();
99 ASSERT (vm->thread_index == 0);
101 pool_get_aligned_will_expand (load_balance_pool, need_barrier_sync,
102 CLIB_CACHE_LINE_BYTES);
103 if (need_barrier_sync)
104 vlib_worker_thread_barrier_sync (vm);
106 pool_get_aligned(load_balance_pool, lb, CLIB_CACHE_LINE_BYTES);
107 clib_memset(lb, 0, sizeof(*lb));
109 lb->lb_map = INDEX_INVALID;
110 lb->lb_urpf = INDEX_INVALID;
112 if (need_barrier_sync == 0)
114 need_barrier_sync += vlib_validate_combined_counter_will_expand
115 (&(load_balance_main.lbm_to_counters),
116 load_balance_get_index(lb));
117 need_barrier_sync += vlib_validate_combined_counter_will_expand
118 (&(load_balance_main.lbm_via_counters),
119 load_balance_get_index(lb));
120 if (need_barrier_sync)
121 vlib_worker_thread_barrier_sync (vm);
124 vlib_validate_combined_counter(&(load_balance_main.lbm_to_counters),
125 load_balance_get_index(lb));
126 vlib_validate_combined_counter(&(load_balance_main.lbm_via_counters),
127 load_balance_get_index(lb));
128 vlib_zero_combined_counter(&(load_balance_main.lbm_to_counters),
129 load_balance_get_index(lb));
130 vlib_zero_combined_counter(&(load_balance_main.lbm_via_counters),
131 load_balance_get_index(lb));
133 if (need_barrier_sync)
134 vlib_worker_thread_barrier_release (vm);
140 load_balance_format (index_t lbi,
141 load_balance_format_flags_t flags,
145 vlib_counter_t to, via;
150 lb = load_balance_get(lbi);
151 vlib_get_combined_counter(&(load_balance_main.lbm_to_counters), lbi, &to);
152 vlib_get_combined_counter(&(load_balance_main.lbm_via_counters), lbi, &via);
153 buckets = load_balance_get_buckets(lb);
155 s = format(s, "%U: ", format_dpo_type, DPO_LOAD_BALANCE);
156 s = format(s, "[proto:%U ", format_dpo_proto, lb->lb_proto);
157 s = format(s, "index:%d buckets:%d ", lbi, lb->lb_n_buckets);
158 s = format(s, "uRPF:%d ", lb->lb_urpf);
161 load_balance_attr_t attr;
163 s = format(s, "flags:[");
165 FOR_EACH_LOAD_BALANCE_ATTR(attr)
167 if (lb->lb_flags & (1 << attr))
169 s = format (s, "%s", load_balance_attr_names[attr]);
174 s = format(s, "to:[%Ld:%Ld]", to.packets, to.bytes);
175 if (0 != via.packets)
177 s = format(s, " via:[%Ld:%Ld]",
178 via.packets, via.bytes);
182 if (INDEX_INVALID != lb->lb_map)
184 s = format(s, "\n%U%U",
185 format_white_space, indent+4,
186 format_load_balance_map, lb->lb_map, indent+4);
188 for (i = 0; i < lb->lb_n_buckets; i++)
190 s = format(s, "\n%U[%d] %U",
191 format_white_space, indent+2,
194 &buckets[i], indent+6);
200 format_load_balance (u8 * s, va_list * args)
202 index_t lbi = va_arg(*args, index_t);
203 load_balance_format_flags_t flags = va_arg(*args, load_balance_format_flags_t);
205 return (load_balance_format(lbi, flags, 0, s));
209 format_load_balance_dpo (u8 * s, va_list * args)
211 index_t lbi = va_arg(*args, index_t);
212 u32 indent = va_arg(*args, u32);
214 return (load_balance_format(lbi, LOAD_BALANCE_FORMAT_DETAIL, indent, s));
218 load_balance_get_default_flow_hash (dpo_proto_t lb_proto)
224 return (IP_FLOW_HASH_DEFAULT);
227 return (MPLS_FLOW_HASH_DEFAULT);
229 case DPO_PROTO_ETHERNET:
238 static load_balance_t *
239 load_balance_create_i (u32 num_buckets,
240 dpo_proto_t lb_proto,
241 flow_hash_config_t fhc)
245 lb = load_balance_alloc_i();
246 lb->lb_hash_config = fhc;
247 lb->lb_n_buckets = num_buckets;
248 lb->lb_n_buckets_minus_1 = num_buckets-1;
249 lb->lb_proto = lb_proto;
251 if (!LB_HAS_INLINE_BUCKETS(lb))
253 vec_validate_aligned(lb->lb_buckets,
254 lb->lb_n_buckets - 1,
255 CLIB_CACHE_LINE_BYTES);
258 LB_DBG(lb, "create");
264 load_balance_create (u32 n_buckets,
265 dpo_proto_t lb_proto,
266 flow_hash_config_t fhc)
268 return (load_balance_get_index(load_balance_create_i(n_buckets, lb_proto, fhc)));
272 load_balance_set_bucket_i (load_balance_t *lb,
275 const dpo_id_t *next)
277 dpo_stack(DPO_LOAD_BALANCE, lb->lb_proto, &buckets[bucket], next);
281 load_balance_set_bucket (index_t lbi,
283 const dpo_id_t *next)
288 lb = load_balance_get(lbi);
289 buckets = load_balance_get_buckets(lb);
291 ASSERT(bucket < lb->lb_n_buckets);
293 load_balance_set_bucket_i(lb, bucket, buckets, next);
297 load_balance_is_drop (const dpo_id_t *dpo)
301 if (DPO_LOAD_BALANCE != dpo->dpoi_type)
304 lb = load_balance_get(dpo->dpoi_index);
306 if (1 == lb->lb_n_buckets)
308 return (dpo_is_drop(load_balance_get_bucket_i(lb, 0)));
314 load_balance_n_buckets (index_t lbi)
318 lb = load_balance_get(lbi);
320 return (lb->lb_n_buckets);
324 load_balance_set_fib_entry_flags (index_t lbi,
325 fib_entry_flag_t flags)
329 lb = load_balance_get(lbi);
330 lb->lb_fib_entry_flags = flags;
335 load_balance_set_urpf (index_t lbi,
341 lb = load_balance_get(lbi);
344 * packets in flight we see this change. but it's atomic, so :P
349 fib_urpf_list_unlock(old);
350 fib_urpf_list_lock(urpf);
354 load_balance_get_urpf (index_t lbi)
358 lb = load_balance_get(lbi);
360 return (lb->lb_urpf);
364 load_balance_get_bucket (index_t lbi,
369 lb = load_balance_get(lbi);
371 return (load_balance_get_bucket_i(lb, bucket));
375 next_hop_sort_by_weight (const load_balance_path_t * n1,
376 const load_balance_path_t * n2)
378 return ((int) n1->path_weight - (int) n2->path_weight);
381 /* Given next hop vector is over-written with normalized one with sorted weights and
382 with weights corresponding to the number of adjacencies for each next hop.
383 Returns number of adjacencies in block. */
385 ip_multipath_normalize_next_hops (const load_balance_path_t * raw_next_hops,
386 load_balance_path_t ** normalized_next_hops,
388 f64 multipath_next_hop_error_tolerance)
390 load_balance_path_t * nhs;
391 uword n_nhs, n_adj, n_adj_left, i, sum_weight;
394 n_nhs = vec_len (raw_next_hops);
399 /* Allocate enough space for 2 copies; we'll use second copy to save original weights. */
400 nhs = *normalized_next_hops;
401 vec_validate (nhs, 2*n_nhs - 1);
403 /* Fast path: 1 next hop in block. */
407 nhs[0] = raw_next_hops[0];
408 nhs[0].path_weight = 1;
416 int cmp = next_hop_sort_by_weight (&raw_next_hops[0], &raw_next_hops[1]) < 0;
419 nhs[0] = raw_next_hops[cmp];
420 nhs[1] = raw_next_hops[cmp ^ 1];
422 /* Fast path: equal cost multipath with 2 next hops. */
423 if (nhs[0].path_weight == nhs[1].path_weight)
425 nhs[0].path_weight = nhs[1].path_weight = 1;
433 clib_memcpy_fast (nhs, raw_next_hops, n_nhs * sizeof (raw_next_hops[0]));
434 qsort (nhs, n_nhs, sizeof (nhs[0]), (void *) next_hop_sort_by_weight);
437 /* Find total weight to normalize weights. */
439 for (i = 0; i < n_nhs; i++)
440 sum_weight += nhs[i].path_weight;
442 /* In the unlikely case that all weights are given as 0, set them all to 1. */
445 for (i = 0; i < n_nhs; i++)
446 nhs[i].path_weight = 1;
450 /* Save copies of all next hop weights to avoid being overwritten in loop below. */
451 for (i = 0; i < n_nhs; i++)
452 nhs[n_nhs + i].path_weight = nhs[i].path_weight;
454 /* Try larger and larger power of 2 sized adjacency blocks until we
455 find one where traffic flows to within 1% of specified weights. */
456 for (n_adj = max_pow2 (n_nhs); ; n_adj *= 2)
460 norm = n_adj / ((f64) sum_weight);
462 for (i = 0; i < n_nhs; i++)
464 f64 nf = nhs[n_nhs + i].path_weight * norm; /* use saved weights */
465 word n = flt_round_nearest (nf);
467 n = n > n_adj_left ? n_adj_left : n;
469 error += fabs (nf - n);
470 nhs[i].path_weight = n;
472 if (0 == nhs[i].path_weight)
475 * when the weight skew is high (norm is small) and n == nf.
476 * without this correction the path with a low weight would have
477 * no representation in the load-balanace - don't want that.
478 * If the weight skew is high so the load-balance has many buckets
479 * to allow it. pays ya money takes ya choice.
486 nhs[0].path_weight += n_adj_left;
488 /* Less than 5% average error per adjacency with this size adjacency block? */
489 if (error <= multipath_next_hop_error_tolerance*n_adj)
491 /* Truncate any next hops with zero weight. */
498 /* Save vector for next call. */
499 *normalized_next_hops = nhs;
500 *sum_weight_in = sum_weight;
504 static load_balance_path_t *
505 load_balance_multipath_next_hop_fixup (const load_balance_path_t *nhs,
506 dpo_proto_t drop_proto)
508 if (0 == vec_len(nhs))
510 load_balance_path_t *new_nhs = NULL, *nh;
513 * we need something for the load-balance. so use the drop
515 vec_add2(new_nhs, nh, 1);
518 dpo_copy(&nh->path_dpo, drop_dpo_get(drop_proto));
527 * Fill in adjacencies in block based on corresponding
528 * next hop adjacencies.
531 load_balance_fill_buckets_norm (load_balance_t *lb,
532 load_balance_path_t *nhs,
536 load_balance_path_t *nh;
542 * the next-hops have normalised weights. that means their sum is the number
543 * of buckets we need to fill.
545 vec_foreach (nh, nhs)
547 for (ii = 0; ii < nh->path_weight; ii++)
549 ASSERT(bucket < n_buckets);
550 load_balance_set_bucket_i(lb, bucket++, buckets, &nh->path_dpo);
555 load_balance_fill_buckets_sticky (load_balance_t *lb,
556 load_balance_path_t *nhs,
560 load_balance_path_t *nh, *fwding_paths;
561 u16 ii, bucket, fpath;
566 vec_foreach (nh, nhs)
568 if (!dpo_is_drop(&nh->path_dpo))
570 vec_add1(fwding_paths, *nh);
573 if (vec_len(fwding_paths) == 0)
574 fwding_paths = vec_dup(nhs);
577 * the next-hops have normalised weights. that means their sum is the number
578 * of buckets we need to fill.
580 vec_foreach (nh, nhs)
582 for (ii = 0; ii < nh->path_weight; ii++)
584 ASSERT(bucket < n_buckets);
585 if (!dpo_is_drop(&nh->path_dpo))
587 load_balance_set_bucket_i(lb, bucket++, buckets, &nh->path_dpo);
591 /* fill the bucks from the next up path */
592 load_balance_set_bucket_i(lb, bucket++, buckets, &fwding_paths[fpath].path_dpo);
593 fpath = (fpath + 1) % vec_len(fwding_paths);
598 vec_free(fwding_paths);
602 load_balance_fill_buckets (load_balance_t *lb,
603 load_balance_path_t *nhs,
606 load_balance_flags_t flags)
608 if (flags & LOAD_BALANCE_FLAG_STICKY)
610 load_balance_fill_buckets_sticky(lb, nhs, buckets, n_buckets);
614 load_balance_fill_buckets_norm(lb, nhs, buckets, n_buckets);
619 load_balance_set_n_buckets (load_balance_t *lb,
622 lb->lb_n_buckets = n_buckets;
623 lb->lb_n_buckets_minus_1 = n_buckets-1;
627 load_balance_multipath_update (const dpo_id_t *dpo,
628 const load_balance_path_t * raw_nhs,
629 load_balance_flags_t flags)
631 load_balance_path_t *nh, *nhs, *fixed_nhs;
632 u32 sum_of_weights, n_buckets, ii;
633 index_t lbmi, old_lbmi;
639 ASSERT(DPO_LOAD_BALANCE == dpo->dpoi_type);
640 lb = load_balance_get(dpo->dpoi_index);
641 lb->lb_flags = flags;
642 fixed_nhs = load_balance_multipath_next_hop_fixup(raw_nhs, lb->lb_proto);
644 ip_multipath_normalize_next_hops((NULL == fixed_nhs ?
649 multipath_next_hop_error_tolerance);
651 ASSERT (n_buckets >= vec_len (raw_nhs));
654 * Save the old load-balance map used, and get a new one if required.
656 old_lbmi = lb->lb_map;
657 if (flags & LOAD_BALANCE_FLAG_USES_MAP)
659 lbmi = load_balance_map_add_or_lock(n_buckets, sum_of_weights, nhs);
663 lbmi = INDEX_INVALID;
666 if (0 == lb->lb_n_buckets)
669 * first time initialisation. no packets inflight, so we can write
672 load_balance_set_n_buckets(lb, n_buckets);
674 if (!LB_HAS_INLINE_BUCKETS(lb))
675 vec_validate_aligned(lb->lb_buckets,
676 lb->lb_n_buckets - 1,
677 CLIB_CACHE_LINE_BYTES);
679 load_balance_fill_buckets(lb, nhs,
680 load_balance_get_buckets(lb),
687 * This is a modification of an existing load-balance.
688 * We need to ensure that packets inflight see a consistent state, that
689 * is the number of reported buckets the LB has (read from
690 * lb_n_buckets_minus_1) is not more than it actually has. So if the
691 * number of buckets is increasing, we must update the bucket array first,
692 * then the reported number. vice-versa if the number of buckets goes down.
694 if (n_buckets == lb->lb_n_buckets)
697 * no change in the number of buckets. we can simply fill what
698 * is new over what is old.
700 load_balance_fill_buckets(lb, nhs,
701 load_balance_get_buckets(lb),
705 else if (n_buckets > lb->lb_n_buckets)
708 * we have more buckets. the old load-balance map (if there is one)
709 * will remain valid, i.e. mapping to indices within range, so we
712 if (n_buckets > LB_NUM_INLINE_BUCKETS &&
713 lb->lb_n_buckets <= LB_NUM_INLINE_BUCKETS)
716 * the new increased number of buckets is crossing the threshold
717 * from the inline storage to out-line. Alloc the outline buckets
718 * first, then fixup the number. then reset the inlines.
720 ASSERT(NULL == lb->lb_buckets);
721 vec_validate_aligned(lb->lb_buckets,
723 CLIB_CACHE_LINE_BYTES);
725 load_balance_fill_buckets(lb, nhs,
728 CLIB_MEMORY_BARRIER();
729 load_balance_set_n_buckets(lb, n_buckets);
731 CLIB_MEMORY_BARRIER();
733 for (ii = 0; ii < LB_NUM_INLINE_BUCKETS; ii++)
735 dpo_reset(&lb->lb_buckets_inline[ii]);
740 if (n_buckets <= LB_NUM_INLINE_BUCKETS)
743 * we are not crossing the threshold and it's still inline buckets.
744 * we can write the new on the old..
746 load_balance_fill_buckets(lb, nhs,
747 load_balance_get_buckets(lb),
749 CLIB_MEMORY_BARRIER();
750 load_balance_set_n_buckets(lb, n_buckets);
755 * we are not crossing the threshold. We need a new bucket array to
756 * hold the increased number of choices.
758 dpo_id_t *new_buckets, *old_buckets, *tmp_dpo;
761 old_buckets = load_balance_get_buckets(lb);
763 vec_validate_aligned(new_buckets,
765 CLIB_CACHE_LINE_BYTES);
767 load_balance_fill_buckets(lb, nhs, new_buckets,
769 CLIB_MEMORY_BARRIER();
770 lb->lb_buckets = new_buckets;
771 CLIB_MEMORY_BARRIER();
772 load_balance_set_n_buckets(lb, n_buckets);
774 vec_foreach(tmp_dpo, old_buckets)
778 vec_free(old_buckets);
783 * buckets fixed. ready for the MAP update.
790 * bucket size shrinkage.
791 * Any map we have will be based on the old
792 * larger number of buckets, so will be translating to indices
793 * out of range. So the new MAP must be installed first.
796 CLIB_MEMORY_BARRIER();
799 if (n_buckets <= LB_NUM_INLINE_BUCKETS &&
800 lb->lb_n_buckets > LB_NUM_INLINE_BUCKETS)
803 * the new decreased number of buckets is crossing the threshold
804 * from out-line storage to inline:
805 * 1 - Fill the inline buckets,
806 * 2 - fixup the number (and this point the inline buckets are
808 * 3 - free the outline buckets
810 load_balance_fill_buckets(lb, nhs,
811 lb->lb_buckets_inline,
813 CLIB_MEMORY_BARRIER();
814 load_balance_set_n_buckets(lb, n_buckets);
815 CLIB_MEMORY_BARRIER();
817 vec_foreach(tmp_dpo, lb->lb_buckets)
821 vec_free(lb->lb_buckets);
826 * not crossing the threshold.
827 * 1 - update the number to the smaller size
828 * 2 - write the new buckets
829 * 3 - reset those no longer used.
834 old_n_buckets = lb->lb_n_buckets;
835 buckets = load_balance_get_buckets(lb);
837 load_balance_set_n_buckets(lb, n_buckets);
838 CLIB_MEMORY_BARRIER();
840 load_balance_fill_buckets(lb, nhs, buckets,
843 for (ii = n_buckets; ii < old_n_buckets; ii++)
845 dpo_reset(&buckets[ii]);
851 vec_foreach (nh, nhs)
853 dpo_reset(&nh->path_dpo);
858 load_balance_map_unlock(old_lbmi);
862 load_balance_lock (dpo_id_t *dpo)
866 lb = load_balance_get(dpo->dpoi_index);
872 load_balance_destroy (load_balance_t *lb)
877 buckets = load_balance_get_buckets(lb);
879 for (i = 0; i < lb->lb_n_buckets; i++)
881 dpo_reset(&buckets[i]);
884 LB_DBG(lb, "destroy");
885 if (!LB_HAS_INLINE_BUCKETS(lb))
887 vec_free(lb->lb_buckets);
890 fib_urpf_list_unlock(lb->lb_urpf);
891 load_balance_map_unlock(lb->lb_map);
893 pool_put(load_balance_pool, lb);
897 load_balance_unlock (dpo_id_t *dpo)
901 lb = load_balance_get(dpo->dpoi_index);
905 if (0 == lb->lb_locks)
907 load_balance_destroy(lb);
912 load_balance_mem_show (void)
914 fib_show_memory_usage("load-balance",
915 pool_elts(load_balance_pool),
916 pool_len(load_balance_pool),
917 sizeof(load_balance_t));
918 load_balance_map_show_mem();
921 const static dpo_vft_t lb_vft = {
922 .dv_lock = load_balance_lock,
923 .dv_unlock = load_balance_unlock,
924 .dv_format = format_load_balance_dpo,
925 .dv_mem_show = load_balance_mem_show,
929 * @brief The per-protocol VLIB graph nodes that are assigned to a load-balance
932 * this means that these graph nodes are ones from which a load-balance is the
933 * parent object in the DPO-graph.
935 * We do not list all the load-balance nodes, such as the *-lookup. instead
936 * we are relying on the correct use of the .sibling_of field when setting
937 * up these sibling nodes.
939 const static char* const load_balance_ip4_nodes[] =
944 const static char* const load_balance_ip6_nodes[] =
949 const static char* const load_balance_mpls_nodes[] =
954 const static char* const load_balance_l2_nodes[] =
959 const static char* const load_balance_nsh_nodes[] =
964 const static char* const load_balance_bier_nodes[] =
969 const static char* const * const load_balance_nodes[DPO_PROTO_NUM] =
971 [DPO_PROTO_IP4] = load_balance_ip4_nodes,
972 [DPO_PROTO_IP6] = load_balance_ip6_nodes,
973 [DPO_PROTO_MPLS] = load_balance_mpls_nodes,
974 [DPO_PROTO_ETHERNET] = load_balance_l2_nodes,
975 [DPO_PROTO_NSH] = load_balance_nsh_nodes,
976 [DPO_PROTO_BIER] = load_balance_bier_nodes,
980 load_balance_module_init (void)
984 dpo_register(DPO_LOAD_BALANCE, &lb_vft, load_balance_nodes);
987 * Special LB with index zero. we need to define this since the v4 mtrie
988 * assumes an index of 0 implies the ply is empty. therefore all 'real'
989 * adjs need a non-zero index.
990 * This should never be used, but just in case, stack it on a drop.
992 lbi = load_balance_create(1, DPO_PROTO_IP4, 0);
993 load_balance_set_bucket(lbi, 0, drop_dpo_get(DPO_PROTO_IP4));
995 load_balance_logger =
996 vlib_log_register_class("dpo", "load-balance");
998 load_balance_map_module_init();
1001 static clib_error_t *
1002 load_balance_show (vlib_main_t * vm,
1003 unformat_input_t * input,
1004 vlib_cli_command_t * cmd)
1006 index_t lbi = INDEX_INVALID;
1008 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1010 if (unformat (input, "%d", &lbi))
1016 if (INDEX_INVALID != lbi)
1018 if (pool_is_free_index(load_balance_pool, lbi))
1020 vlib_cli_output (vm, "no such load-balance:%d", lbi);
1024 vlib_cli_output (vm, "%U", format_load_balance, lbi,
1025 LOAD_BALANCE_FORMAT_DETAIL);
1032 pool_foreach (lb, load_balance_pool)
1034 vlib_cli_output (vm, "%U", format_load_balance,
1035 load_balance_get_index(lb),
1036 LOAD_BALANCE_FORMAT_NONE);
1043 VLIB_CLI_COMMAND (load_balance_show_command, static) = {
1044 .path = "show load-balance",
1045 .short_help = "show load-balance [<index>]",
1046 .function = load_balance_show,
1051 ip_flow_hash (void *data)
1053 ip4_header_t *iph = (ip4_header_t *) data;
1055 if ((iph->ip_version_and_header_length & 0xF0) == 0x40)
1056 return ip4_compute_flow_hash (iph, IP_FLOW_HASH_DEFAULT);
1058 return ip6_compute_flow_hash ((ip6_header_t *) iph, IP_FLOW_HASH_DEFAULT);
1064 return (*((u64 *) m) & 0xffffffffffff);
1068 l2_flow_hash (vlib_buffer_t * b0)
1070 ethernet_header_t *eh;
1072 uword is_ip, eh_size;
1075 eh = vlib_buffer_get_current (b0);
1076 eh_type = clib_net_to_host_u16 (eh->type);
1077 eh_size = ethernet_buffer_header_size (b0);
1079 is_ip = (eh_type == ETHERNET_TYPE_IP4 || eh_type == ETHERNET_TYPE_IP6);
1081 /* since we have 2 cache lines, use them */
1083 a = ip_flow_hash ((u8 *) vlib_buffer_get_current (b0) + eh_size);
1087 b = mac_to_u64 ((u8 *) eh->dst_address);
1088 c = mac_to_u64 ((u8 *) eh->src_address);
1089 hash_mix64 (a, b, c);
1094 typedef struct load_balance_trace_t_
1097 } load_balance_trace_t;
1100 load_balance_inline (vlib_main_t * vm,
1101 vlib_node_runtime_t * node,
1102 vlib_frame_t * frame,
1105 u32 n_left_from, next_index, *from, *to_next;
1107 from = vlib_frame_vector_args (frame);
1108 n_left_from = frame->n_vectors;
1110 next_index = node->cached_next_index;
1112 while (n_left_from > 0)
1116 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1118 while (n_left_from > 0 && n_left_to_next > 0)
1121 u32 bi0, lbi0, next0;
1122 const dpo_id_t *dpo0;
1123 const load_balance_t *lb0;
1130 n_left_to_next -= 1;
1132 b0 = vlib_get_buffer (vm, bi0);
1134 /* lookup dst + src mac */
1135 lbi0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
1136 lb0 = load_balance_get(lbi0);
1140 vnet_buffer(b0)->ip.flow_hash = l2_flow_hash(b0);
1145 const bier_hdr_t *bh0 = vlib_buffer_get_current(b0);
1146 vnet_buffer(b0)->ip.flow_hash = bier_compute_flow_hash(bh0);
1149 dpo0 = load_balance_get_bucket_i(lb0,
1150 vnet_buffer(b0)->ip.flow_hash &
1151 (lb0->lb_n_buckets_minus_1));
1153 next0 = dpo0->dpoi_next_node;
1154 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
1156 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1158 load_balance_trace_t *tr = vlib_add_trace (vm, node, b0,
1160 tr->lb_index = lbi0;
1162 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1163 n_left_to_next, bi0, next0);
1166 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1169 return frame->n_vectors;
1173 l2_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, 1));
1181 format_l2_load_balance_trace (u8 * s, va_list * args)
1183 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1184 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1185 load_balance_trace_t *t = va_arg (*args, load_balance_trace_t *);
1187 s = format (s, "L2-load-balance: index %d", t->lb_index);
1194 VLIB_REGISTER_NODE (l2_load_balance_node) = {
1195 .function = l2_load_balance,
1196 .name = "l2-load-balance",
1197 .vector_size = sizeof (u32),
1199 .format_trace = format_l2_load_balance_trace,
1207 nsh_load_balance (vlib_main_t * vm,
1208 vlib_node_runtime_t * node,
1209 vlib_frame_t * frame)
1211 u32 n_left_from, next_index, *from, *to_next;
1213 from = vlib_frame_vector_args (frame);
1214 n_left_from = frame->n_vectors;
1216 next_index = node->cached_next_index;
1218 while (n_left_from > 0)
1222 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1224 while (n_left_from > 0 && n_left_to_next > 0)
1227 u32 bi0, lbi0, next0, *nsh0;
1228 const dpo_id_t *dpo0;
1229 const load_balance_t *lb0;
1236 n_left_to_next -= 1;
1238 b0 = vlib_get_buffer (vm, bi0);
1240 lbi0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
1241 lb0 = load_balance_get(lbi0);
1243 /* SPI + SI are the second word of the NSH header */
1244 nsh0 = vlib_buffer_get_current (b0);
1245 vnet_buffer(b0)->ip.flow_hash = nsh0[1] % lb0->lb_n_buckets;
1247 dpo0 = load_balance_get_bucket_i(lb0,
1248 vnet_buffer(b0)->ip.flow_hash &
1249 (lb0->lb_n_buckets_minus_1));
1251 next0 = dpo0->dpoi_next_node;
1252 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
1254 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1256 load_balance_trace_t *tr = vlib_add_trace (vm, node, b0,
1258 tr->lb_index = lbi0;
1260 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1261 n_left_to_next, bi0, next0);
1264 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1267 return frame->n_vectors;
1271 format_nsh_load_balance_trace (u8 * s, va_list * args)
1273 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1274 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1275 load_balance_trace_t *t = va_arg (*args, load_balance_trace_t *);
1277 s = format (s, "NSH-load-balance: index %d", t->lb_index);
1284 VLIB_REGISTER_NODE (nsh_load_balance_node) = {
1285 .function = nsh_load_balance,
1286 .name = "nsh-load-balance",
1287 .vector_size = sizeof (u32),
1289 .format_trace = format_nsh_load_balance_trace,
1297 format_bier_load_balance_trace (u8 * s, va_list * args)
1299 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1300 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1301 load_balance_trace_t *t = va_arg (*args, load_balance_trace_t *);
1303 s = format (s, "BIER-load-balance: index %d", t->lb_index);
1308 bier_load_balance (vlib_main_t * vm,
1309 vlib_node_runtime_t * node,
1310 vlib_frame_t * frame)
1312 return (load_balance_inline(vm, node, frame, 0));
1318 VLIB_REGISTER_NODE (bier_load_balance_node) = {
1319 .function = bier_load_balance,
1320 .name = "bier-load-balance",
1321 .vector_size = sizeof (u32),
1323 .format_trace = format_bier_load_balance_trace,
1324 .sibling_of = "mpls-load-balance",