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>
31 * distribution error tolerance for load-balancing
33 const f64 multipath_next_hop_error_tolerance = 0.1;
35 static const char *load_balance_attr_names[] = LOAD_BALANCE_ATTR_NAMES;
40 vlib_log_class_t load_balance_logger;
42 #define LB_DBG(_lb, _fmt, _args...) \
44 vlib_log_debug(load_balance_logger, \
46 format_load_balance, load_balance_get_index(_lb), \
47 LOAD_BALANCE_FORMAT_NONE, \
52 * Pool of all DPOs. It's not static so the DP can have fast access
54 load_balance_t *load_balance_pool;
57 * The one instance of load-balance main
59 load_balance_main_t load_balance_main = {
62 .stat_segment_name = "/net/route/to",
66 .stat_segment_name = "/net/route/via",
71 load_balance_get_multipath_tolerance (void)
73 return (multipath_next_hop_error_tolerance);
77 load_balance_get_index (const load_balance_t *lb)
79 return (lb - load_balance_pool);
82 static inline dpo_id_t*
83 load_balance_get_buckets (load_balance_t *lb)
85 if (LB_HAS_INLINE_BUCKETS(lb))
87 return (lb->lb_buckets_inline);
91 return (lb->lb_buckets);
95 static load_balance_t *
96 load_balance_alloc_i (void)
99 u8 need_barrier_sync = 0;
100 vlib_main_t *vm = vlib_get_main();
101 ASSERT (vm->thread_index == 0);
103 need_barrier_sync = pool_get_will_expand (load_balance_pool);
105 if (need_barrier_sync)
106 vlib_worker_thread_barrier_sync (vm);
108 pool_get_aligned(load_balance_pool, lb, CLIB_CACHE_LINE_BYTES);
109 clib_memset(lb, 0, sizeof(*lb));
111 lb->lb_map = INDEX_INVALID;
112 lb->lb_urpf = INDEX_INVALID;
114 if (need_barrier_sync == 0)
116 need_barrier_sync += vlib_validate_combined_counter_will_expand
117 (&(load_balance_main.lbm_to_counters),
118 load_balance_get_index(lb));
119 need_barrier_sync += vlib_validate_combined_counter_will_expand
120 (&(load_balance_main.lbm_via_counters),
121 load_balance_get_index(lb));
122 if (need_barrier_sync)
123 vlib_worker_thread_barrier_sync (vm);
126 vlib_validate_combined_counter(&(load_balance_main.lbm_to_counters),
127 load_balance_get_index(lb));
128 vlib_validate_combined_counter(&(load_balance_main.lbm_via_counters),
129 load_balance_get_index(lb));
130 vlib_zero_combined_counter(&(load_balance_main.lbm_to_counters),
131 load_balance_get_index(lb));
132 vlib_zero_combined_counter(&(load_balance_main.lbm_via_counters),
133 load_balance_get_index(lb));
135 if (need_barrier_sync)
136 vlib_worker_thread_barrier_release (vm);
142 load_balance_format (index_t lbi,
143 load_balance_format_flags_t flags,
147 vlib_counter_t to, via;
152 lb = load_balance_get(lbi);
153 vlib_get_combined_counter(&(load_balance_main.lbm_to_counters), lbi, &to);
154 vlib_get_combined_counter(&(load_balance_main.lbm_via_counters), lbi, &via);
155 buckets = load_balance_get_buckets(lb);
157 s = format(s, "%U: ", format_dpo_type, DPO_LOAD_BALANCE);
158 s = format(s, "[proto:%U ", format_dpo_proto, lb->lb_proto);
159 s = format(s, "index:%d buckets:%d ", lbi, lb->lb_n_buckets);
160 s = format(s, "uRPF:%d ", lb->lb_urpf);
163 load_balance_attr_t attr;
165 s = format(s, "flags:[");
167 FOR_EACH_LOAD_BALANCE_ATTR(attr)
169 if (lb->lb_flags & (1 << attr))
171 s = format (s, "%s", load_balance_attr_names[attr]);
176 s = format(s, "to:[%Ld:%Ld]", to.packets, to.bytes);
177 if (0 != via.packets)
179 s = format(s, " via:[%Ld:%Ld]",
180 via.packets, via.bytes);
184 if (INDEX_INVALID != lb->lb_map)
186 s = format(s, "\n%U%U",
187 format_white_space, indent+4,
188 format_load_balance_map, lb->lb_map, indent+4);
190 for (i = 0; i < lb->lb_n_buckets; i++)
192 s = format(s, "\n%U[%d] %U",
193 format_white_space, indent+2,
196 &buckets[i], indent+6);
202 format_load_balance (u8 * s, va_list * args)
204 index_t lbi = va_arg(*args, index_t);
205 load_balance_format_flags_t flags = va_arg(*args, load_balance_format_flags_t);
207 return (load_balance_format(lbi, flags, 0, s));
211 format_load_balance_dpo (u8 * s, va_list * args)
213 index_t lbi = va_arg(*args, index_t);
214 u32 indent = va_arg(*args, u32);
216 return (load_balance_format(lbi, LOAD_BALANCE_FORMAT_DETAIL, indent, s));
220 load_balance_get_default_flow_hash (dpo_proto_t lb_proto)
226 return (IP_FLOW_HASH_DEFAULT);
229 return (MPLS_FLOW_HASH_DEFAULT);
231 case DPO_PROTO_ETHERNET:
240 static load_balance_t *
241 load_balance_create_i (u32 num_buckets,
242 dpo_proto_t lb_proto,
243 flow_hash_config_t fhc)
247 ASSERT (num_buckets <= LB_MAX_BUCKETS);
249 lb = load_balance_alloc_i();
250 lb->lb_hash_config = fhc;
251 lb->lb_n_buckets = num_buckets;
252 lb->lb_n_buckets_minus_1 = num_buckets-1;
253 lb->lb_proto = lb_proto;
255 if (!LB_HAS_INLINE_BUCKETS(lb))
257 vec_validate_aligned(lb->lb_buckets,
258 lb->lb_n_buckets - 1,
259 CLIB_CACHE_LINE_BYTES);
262 LB_DBG(lb, "create");
268 load_balance_create (u32 n_buckets,
269 dpo_proto_t lb_proto,
270 flow_hash_config_t fhc)
272 return (load_balance_get_index(load_balance_create_i(n_buckets, lb_proto, fhc)));
276 load_balance_set_bucket_i (load_balance_t *lb,
279 const dpo_id_t *next)
281 dpo_stack(DPO_LOAD_BALANCE, lb->lb_proto, &buckets[bucket], next);
285 load_balance_set_bucket (index_t lbi,
287 const dpo_id_t *next)
292 lb = load_balance_get(lbi);
293 buckets = load_balance_get_buckets(lb);
295 ASSERT(bucket < lb->lb_n_buckets);
297 load_balance_set_bucket_i(lb, bucket, buckets, next);
301 load_balance_is_drop (const dpo_id_t *dpo)
305 if (DPO_LOAD_BALANCE != dpo->dpoi_type)
308 lb = load_balance_get(dpo->dpoi_index);
310 if (1 == lb->lb_n_buckets)
312 return (dpo_is_drop(load_balance_get_bucket_i(lb, 0)));
318 load_balance_n_buckets (index_t lbi)
322 lb = load_balance_get(lbi);
324 return (lb->lb_n_buckets);
328 load_balance_set_fib_entry_flags (index_t lbi,
329 fib_entry_flag_t flags)
333 lb = load_balance_get(lbi);
334 lb->lb_fib_entry_flags = flags;
339 load_balance_set_urpf (index_t lbi,
345 lb = load_balance_get(lbi);
348 * packets in flight we see this change. but it's atomic, so :P
353 fib_urpf_list_unlock(old);
354 fib_urpf_list_lock(urpf);
358 load_balance_get_urpf (index_t lbi)
362 lb = load_balance_get(lbi);
364 return (lb->lb_urpf);
368 load_balance_get_bucket (index_t lbi,
373 lb = load_balance_get(lbi);
375 return (load_balance_get_bucket_i(lb, bucket));
379 next_hop_sort_by_weight (const load_balance_path_t * n1,
380 const load_balance_path_t * n2)
382 return ((int) n1->path_weight - (int) n2->path_weight);
385 /* Given next hop vector is over-written with normalized one with sorted weights and
386 with weights corresponding to the number of adjacencies for each next hop.
387 Returns number of adjacencies in block. */
389 ip_multipath_normalize_next_hops (const load_balance_path_t * raw_next_hops,
390 load_balance_path_t ** normalized_next_hops,
392 f64 multipath_next_hop_error_tolerance)
394 load_balance_path_t * nhs;
395 uword n_nhs, n_adj, n_adj_left, i, sum_weight;
398 n_nhs = vec_len (raw_next_hops);
403 /* Allocate enough space for 2 copies; we'll use second copy to save original weights. */
404 nhs = *normalized_next_hops;
405 vec_validate (nhs, 2*n_nhs - 1);
407 /* Fast path: 1 next hop in block. */
411 nhs[0] = raw_next_hops[0];
412 nhs[0].path_weight = 1;
413 vec_set_len (nhs, 1);
420 int cmp = next_hop_sort_by_weight (&raw_next_hops[0], &raw_next_hops[1]) < 0;
423 nhs[0] = raw_next_hops[cmp];
424 nhs[1] = raw_next_hops[cmp ^ 1];
426 /* Fast path: equal cost multipath with 2 next hops. */
427 if (nhs[0].path_weight == nhs[1].path_weight)
429 nhs[0].path_weight = nhs[1].path_weight = 1;
430 vec_set_len (nhs, 2);
437 clib_memcpy_fast (nhs, raw_next_hops, n_nhs * sizeof (raw_next_hops[0]));
438 qsort (nhs, n_nhs, sizeof (nhs[0]), (void *) next_hop_sort_by_weight);
441 /* Find total weight to normalize weights. */
443 for (i = 0; i < n_nhs; i++)
444 sum_weight += nhs[i].path_weight;
446 /* In the unlikely case that all weights are given as 0, set them all to 1. */
449 for (i = 0; i < n_nhs; i++)
450 nhs[i].path_weight = 1;
454 /* Save copies of all next hop weights to avoid being overwritten in loop below. */
455 for (i = 0; i < n_nhs; i++)
456 nhs[n_nhs + i].path_weight = nhs[i].path_weight;
458 /* Try larger and larger power of 2 sized adjacency blocks until we
459 find one where traffic flows to within 1% of specified weights. */
460 for (n_adj = clib_min(max_pow2 (n_nhs), LB_MAX_BUCKETS); ; n_adj *= 2)
462 ASSERT (n_adj <= LB_MAX_BUCKETS);
465 norm = n_adj / ((f64) sum_weight);
467 for (i = 0; i < n_nhs; i++)
469 f64 nf = nhs[n_nhs + i].path_weight * norm; /* use saved weights */
470 word n = flt_round_nearest (nf);
472 n = n > n_adj_left ? n_adj_left : n;
474 error += fabs (nf - n);
475 nhs[i].path_weight = n;
477 if (0 == nhs[i].path_weight)
480 * when the weight skew is high (norm is small) and n == nf.
481 * without this correction the path with a low weight would have
482 * no representation in the load-balanace - don't want that.
483 * If the weight skew is high so the load-balance has many buckets
484 * to allow it. pays ya money takes ya choice.
491 nhs[0].path_weight += n_adj_left;
493 /* Less than 1% average error per adjacency with this size adjacency block,
494 * or did we reached the maximum number of buckets we support? */
495 if (error <= multipath_next_hop_error_tolerance*n_adj ||
496 n_adj >= LB_MAX_BUCKETS)
500 /* Truncate any next hops in excess */
501 vlib_log_err(load_balance_logger,
502 "Too many paths for load-balance, truncating %d -> %d",
504 for (int j = i; j < n_nhs; j++)
505 dpo_reset (&vec_elt(nhs, j).path_dpo);
507 vec_set_len (nhs, i);
513 /* Save vector for next call. */
514 *normalized_next_hops = nhs;
515 *sum_weight_in = sum_weight;
519 static load_balance_path_t *
520 load_balance_multipath_next_hop_fixup (const load_balance_path_t *nhs,
521 dpo_proto_t drop_proto)
523 if (0 == vec_len(nhs))
525 load_balance_path_t *new_nhs = NULL, *nh;
528 * we need something for the load-balance. so use the drop
530 vec_add2(new_nhs, nh, 1);
533 dpo_copy(&nh->path_dpo, drop_dpo_get(drop_proto));
542 * Fill in adjacencies in block based on corresponding
543 * next hop adjacencies.
546 load_balance_fill_buckets_norm (load_balance_t *lb,
547 load_balance_path_t *nhs,
551 load_balance_path_t *nh;
557 * the next-hops have normalised weights. that means their sum is the number
558 * of buckets we need to fill.
560 vec_foreach (nh, nhs)
562 for (ii = 0; ii < nh->path_weight; ii++)
564 ASSERT(bucket < n_buckets);
565 load_balance_set_bucket_i(lb, bucket++, buckets, &nh->path_dpo);
570 load_balance_fill_buckets_sticky (load_balance_t *lb,
571 load_balance_path_t *nhs,
575 load_balance_path_t *nh, *fwding_paths;
576 u16 ii, bucket, fpath;
581 vec_foreach (nh, nhs)
583 if (!dpo_is_drop(&nh->path_dpo))
585 vec_add1(fwding_paths, *nh);
588 if (vec_len(fwding_paths) == 0)
589 fwding_paths = vec_dup(nhs);
592 * the next-hops have normalised weights. that means their sum is the number
593 * of buckets we need to fill.
595 vec_foreach (nh, nhs)
597 for (ii = 0; ii < nh->path_weight; ii++)
599 ASSERT(bucket < n_buckets);
600 if (!dpo_is_drop(&nh->path_dpo))
602 load_balance_set_bucket_i(lb, bucket++, buckets, &nh->path_dpo);
606 /* fill the bucks from the next up path */
607 load_balance_set_bucket_i(lb, bucket++, buckets, &fwding_paths[fpath].path_dpo);
608 ASSERT(vec_len(fwding_paths) > 0);
609 fpath = (fpath + 1) % vec_len(fwding_paths);
614 vec_free(fwding_paths);
618 load_balance_fill_buckets (load_balance_t *lb,
619 load_balance_path_t *nhs,
622 load_balance_flags_t flags)
624 if (flags & LOAD_BALANCE_FLAG_STICKY)
626 load_balance_fill_buckets_sticky(lb, nhs, buckets, n_buckets);
630 load_balance_fill_buckets_norm(lb, nhs, buckets, n_buckets);
635 load_balance_set_n_buckets (load_balance_t *lb,
638 ASSERT (n_buckets <= LB_MAX_BUCKETS);
639 lb->lb_n_buckets = n_buckets;
640 lb->lb_n_buckets_minus_1 = n_buckets-1;
644 load_balance_multipath_update (const dpo_id_t *dpo,
645 const load_balance_path_t * raw_nhs,
646 load_balance_flags_t flags)
648 load_balance_path_t *nh, *nhs, *fixed_nhs;
649 u32 sum_of_weights, n_buckets, ii;
650 index_t lbmi, old_lbmi;
656 ASSERT(DPO_LOAD_BALANCE == dpo->dpoi_type);
657 lb = load_balance_get(dpo->dpoi_index);
658 lb->lb_flags = flags;
659 fixed_nhs = load_balance_multipath_next_hop_fixup(raw_nhs, lb->lb_proto);
661 ip_multipath_normalize_next_hops((NULL == fixed_nhs ?
666 multipath_next_hop_error_tolerance);
669 * Save the old load-balance map used, and get a new one if required.
671 old_lbmi = lb->lb_map;
672 if (flags & LOAD_BALANCE_FLAG_USES_MAP)
674 lbmi = load_balance_map_add_or_lock(n_buckets, sum_of_weights, nhs);
678 lbmi = INDEX_INVALID;
681 if (0 == lb->lb_n_buckets)
684 * first time initialisation. no packets inflight, so we can write
687 load_balance_set_n_buckets(lb, n_buckets);
689 if (!LB_HAS_INLINE_BUCKETS(lb))
690 vec_validate_aligned(lb->lb_buckets,
691 lb->lb_n_buckets - 1,
692 CLIB_CACHE_LINE_BYTES);
694 load_balance_fill_buckets(lb, nhs,
695 load_balance_get_buckets(lb),
702 * This is a modification of an existing load-balance.
703 * We need to ensure that packets inflight see a consistent state, that
704 * is the number of reported buckets the LB has (read from
705 * lb_n_buckets_minus_1) is not more than it actually has. So if the
706 * number of buckets is increasing, we must update the bucket array first,
707 * then the reported number. vice-versa if the number of buckets goes down.
709 if (n_buckets == lb->lb_n_buckets)
712 * no change in the number of buckets. we can simply fill what
713 * is new over what is old.
715 load_balance_fill_buckets(lb, nhs,
716 load_balance_get_buckets(lb),
720 else if (n_buckets > lb->lb_n_buckets)
723 * we have more buckets. the old load-balance map (if there is one)
724 * will remain valid, i.e. mapping to indices within range, so we
727 if (n_buckets > LB_NUM_INLINE_BUCKETS &&
728 lb->lb_n_buckets <= LB_NUM_INLINE_BUCKETS)
731 * the new increased number of buckets is crossing the threshold
732 * from the inline storage to out-line. Alloc the outline buckets
733 * first, then fixup the number. then reset the inlines.
735 ASSERT(NULL == lb->lb_buckets);
736 vec_validate_aligned(lb->lb_buckets,
738 CLIB_CACHE_LINE_BYTES);
740 load_balance_fill_buckets(lb, nhs,
743 CLIB_MEMORY_BARRIER();
744 load_balance_set_n_buckets(lb, n_buckets);
746 CLIB_MEMORY_BARRIER();
748 for (ii = 0; ii < LB_NUM_INLINE_BUCKETS; ii++)
750 dpo_reset(&lb->lb_buckets_inline[ii]);
755 if (n_buckets <= LB_NUM_INLINE_BUCKETS)
758 * we are not crossing the threshold and it's still inline buckets.
759 * we can write the new on the old..
761 load_balance_fill_buckets(lb, nhs,
762 load_balance_get_buckets(lb),
764 CLIB_MEMORY_BARRIER();
765 load_balance_set_n_buckets(lb, n_buckets);
770 * we are not crossing the threshold. We need a new bucket array to
771 * hold the increased number of choices.
773 dpo_id_t *new_buckets, *old_buckets, *tmp_dpo;
776 old_buckets = load_balance_get_buckets(lb);
778 vec_validate_aligned(new_buckets,
780 CLIB_CACHE_LINE_BYTES);
782 load_balance_fill_buckets(lb, nhs, new_buckets,
784 CLIB_MEMORY_BARRIER();
785 lb->lb_buckets = new_buckets;
786 CLIB_MEMORY_BARRIER();
787 load_balance_set_n_buckets(lb, n_buckets);
789 vec_foreach(tmp_dpo, old_buckets)
793 vec_free(old_buckets);
798 * buckets fixed. ready for the MAP update.
805 * bucket size shrinkage.
806 * Any map we have will be based on the old
807 * larger number of buckets, so will be translating to indices
808 * out of range. So the new MAP must be installed first.
811 CLIB_MEMORY_BARRIER();
814 if (n_buckets <= LB_NUM_INLINE_BUCKETS &&
815 lb->lb_n_buckets > LB_NUM_INLINE_BUCKETS)
818 * the new decreased number of buckets is crossing the threshold
819 * from out-line storage to inline:
820 * 1 - Fill the inline buckets,
821 * 2 - fixup the number (and this point the inline buckets are
823 * 3 - free the outline buckets
825 load_balance_fill_buckets(lb, nhs,
826 lb->lb_buckets_inline,
828 CLIB_MEMORY_BARRIER();
829 load_balance_set_n_buckets(lb, n_buckets);
830 CLIB_MEMORY_BARRIER();
832 vec_foreach(tmp_dpo, lb->lb_buckets)
836 vec_free(lb->lb_buckets);
841 * not crossing the threshold.
842 * 1 - update the number to the smaller size
843 * 2 - write the new buckets
844 * 3 - reset those no longer used.
849 old_n_buckets = lb->lb_n_buckets;
850 buckets = load_balance_get_buckets(lb);
852 load_balance_set_n_buckets(lb, n_buckets);
853 CLIB_MEMORY_BARRIER();
855 load_balance_fill_buckets(lb, nhs, buckets,
858 for (ii = n_buckets; ii < old_n_buckets; ii++)
860 dpo_reset(&buckets[ii]);
866 vec_foreach (nh, nhs)
868 dpo_reset(&nh->path_dpo);
873 load_balance_map_unlock(old_lbmi);
877 load_balance_lock (dpo_id_t *dpo)
881 lb = load_balance_get(dpo->dpoi_index);
887 load_balance_destroy (load_balance_t *lb)
892 buckets = load_balance_get_buckets(lb);
894 for (i = 0; i < lb->lb_n_buckets; i++)
896 dpo_reset(&buckets[i]);
899 LB_DBG(lb, "destroy");
900 if (!LB_HAS_INLINE_BUCKETS(lb))
902 vec_free(lb->lb_buckets);
905 fib_urpf_list_unlock(lb->lb_urpf);
906 load_balance_map_unlock(lb->lb_map);
908 pool_put(load_balance_pool, lb);
912 load_balance_unlock (dpo_id_t *dpo)
916 lb = load_balance_get(dpo->dpoi_index);
920 if (0 == lb->lb_locks)
922 load_balance_destroy(lb);
927 load_balance_mem_show (void)
929 fib_show_memory_usage("load-balance",
930 pool_elts(load_balance_pool),
931 pool_len(load_balance_pool),
932 sizeof(load_balance_t));
933 load_balance_map_show_mem();
937 load_balance_dpo_get_mtu (const dpo_id_t *dpo)
939 const dpo_id_t *buckets;
943 lb = load_balance_get(dpo->dpoi_index);
944 buckets = load_balance_get_buckets(lb);
946 for (i = 0; i < lb->lb_n_buckets; i++)
948 mtu = clib_min (mtu, dpo_get_mtu (&buckets[i]));
954 const static dpo_vft_t lb_vft = {
955 .dv_lock = load_balance_lock,
956 .dv_unlock = load_balance_unlock,
957 .dv_format = format_load_balance_dpo,
958 .dv_mem_show = load_balance_mem_show,
959 .dv_get_mtu = load_balance_dpo_get_mtu,
963 * @brief The per-protocol VLIB graph nodes that are assigned to a load-balance
966 * this means that these graph nodes are ones from which a load-balance is the
967 * parent object in the DPO-graph.
969 * We do not list all the load-balance nodes, such as the *-lookup. instead
970 * we are relying on the correct use of the .sibling_of field when setting
971 * up these sibling nodes.
973 const static char* const load_balance_ip4_nodes[] =
978 const static char* const load_balance_ip6_nodes[] =
983 const static char* const load_balance_mpls_nodes[] =
988 const static char* const load_balance_l2_nodes[] =
993 const static char* const load_balance_nsh_nodes[] =
998 const static char* const load_balance_bier_nodes[] =
1000 "bier-load-balance",
1003 const static char* const * const load_balance_nodes[DPO_PROTO_NUM] =
1005 [DPO_PROTO_IP4] = load_balance_ip4_nodes,
1006 [DPO_PROTO_IP6] = load_balance_ip6_nodes,
1007 [DPO_PROTO_MPLS] = load_balance_mpls_nodes,
1008 [DPO_PROTO_ETHERNET] = load_balance_l2_nodes,
1009 [DPO_PROTO_NSH] = load_balance_nsh_nodes,
1010 [DPO_PROTO_BIER] = load_balance_bier_nodes,
1014 load_balance_module_init (void)
1018 dpo_register(DPO_LOAD_BALANCE, &lb_vft, load_balance_nodes);
1021 * Special LB with index zero. we need to define this since the v4 mtrie
1022 * assumes an index of 0 implies the ply is empty. therefore all 'real'
1023 * adjs need a non-zero index.
1024 * This should never be used, but just in case, stack it on a drop.
1026 lbi = load_balance_create(1, DPO_PROTO_IP4, 0);
1027 load_balance_set_bucket(lbi, 0, drop_dpo_get(DPO_PROTO_IP4));
1029 load_balance_logger =
1030 vlib_log_register_class("dpo", "load-balance");
1032 load_balance_map_module_init();
1035 static clib_error_t *
1036 load_balance_show (vlib_main_t * vm,
1037 unformat_input_t * input,
1038 vlib_cli_command_t * cmd)
1040 index_t lbi = INDEX_INVALID;
1042 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1044 if (unformat (input, "%d", &lbi))
1050 if (INDEX_INVALID != lbi)
1052 if (pool_is_free_index(load_balance_pool, lbi))
1054 vlib_cli_output (vm, "no such load-balance:%d", lbi);
1058 vlib_cli_output (vm, "%U", format_load_balance, lbi,
1059 LOAD_BALANCE_FORMAT_DETAIL);
1066 pool_foreach (lb, load_balance_pool)
1068 vlib_cli_output (vm, "%U", format_load_balance,
1069 load_balance_get_index(lb),
1070 LOAD_BALANCE_FORMAT_NONE);
1077 VLIB_CLI_COMMAND (load_balance_show_command, static) = {
1078 .path = "show load-balance",
1079 .short_help = "show load-balance [<index>]",
1080 .function = load_balance_show,
1085 ip_flow_hash (void *data)
1087 ip4_header_t *iph = (ip4_header_t *) data;
1089 if ((iph->ip_version_and_header_length & 0xF0) == 0x40)
1090 return ip4_compute_flow_hash (iph, IP_FLOW_HASH_DEFAULT);
1092 return ip6_compute_flow_hash ((ip6_header_t *) iph, IP_FLOW_HASH_DEFAULT);
1098 return (*((u64 *) m) & 0xffffffffffff);
1102 l2_flow_hash (vlib_buffer_t * b0)
1104 ethernet_header_t *eh;
1106 uword is_ip, eh_size;
1109 eh = vlib_buffer_get_current (b0);
1110 eh_type = clib_net_to_host_u16 (eh->type);
1111 eh_size = ethernet_buffer_header_size (b0);
1113 is_ip = (eh_type == ETHERNET_TYPE_IP4 || eh_type == ETHERNET_TYPE_IP6);
1115 /* since we have 2 cache lines, use them */
1117 a = ip_flow_hash ((u8 *) vlib_buffer_get_current (b0) + eh_size);
1121 b = mac_to_u64 ((u8 *) eh->dst_address);
1122 c = mac_to_u64 ((u8 *) eh->src_address);
1123 hash_mix64 (a, b, c);
1128 typedef struct load_balance_trace_t_
1131 } load_balance_trace_t;
1134 load_balance_inline (vlib_main_t * vm,
1135 vlib_node_runtime_t * node,
1136 vlib_frame_t * frame,
1139 u32 n_left_from, next_index, *from, *to_next;
1141 from = vlib_frame_vector_args (frame);
1142 n_left_from = frame->n_vectors;
1144 next_index = node->cached_next_index;
1146 while (n_left_from > 0)
1150 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1152 while (n_left_from > 0 && n_left_to_next > 0)
1155 u32 bi0, lbi0, next0;
1156 const dpo_id_t *dpo0;
1157 const load_balance_t *lb0;
1164 n_left_to_next -= 1;
1166 b0 = vlib_get_buffer (vm, bi0);
1168 /* lookup dst + src mac */
1169 lbi0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
1170 lb0 = load_balance_get(lbi0);
1174 vnet_buffer(b0)->ip.flow_hash = l2_flow_hash(b0);
1179 const bier_hdr_t *bh0 = vlib_buffer_get_current(b0);
1180 vnet_buffer(b0)->ip.flow_hash = bier_compute_flow_hash(bh0);
1183 dpo0 = load_balance_get_bucket_i(lb0,
1184 vnet_buffer(b0)->ip.flow_hash &
1185 (lb0->lb_n_buckets_minus_1));
1187 next0 = dpo0->dpoi_next_node;
1188 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
1190 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1192 load_balance_trace_t *tr = vlib_add_trace (vm, node, b0,
1194 tr->lb_index = lbi0;
1196 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1197 n_left_to_next, bi0, next0);
1200 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1203 return frame->n_vectors;
1207 l2_load_balance (vlib_main_t * vm,
1208 vlib_node_runtime_t * node,
1209 vlib_frame_t * frame)
1211 return (load_balance_inline(vm, node, frame, 1));
1215 format_l2_load_balance_trace (u8 * s, va_list * args)
1217 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1218 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1219 load_balance_trace_t *t = va_arg (*args, load_balance_trace_t *);
1221 s = format (s, "L2-load-balance: index %d", t->lb_index);
1228 VLIB_REGISTER_NODE (l2_load_balance_node) = {
1229 .function = l2_load_balance,
1230 .name = "l2-load-balance",
1231 .vector_size = sizeof (u32),
1233 .format_trace = format_l2_load_balance_trace,
1241 nsh_load_balance (vlib_main_t * vm,
1242 vlib_node_runtime_t * node,
1243 vlib_frame_t * frame)
1245 u32 n_left_from, next_index, *from, *to_next;
1247 from = vlib_frame_vector_args (frame);
1248 n_left_from = frame->n_vectors;
1250 next_index = node->cached_next_index;
1252 while (n_left_from > 0)
1256 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1258 while (n_left_from > 0 && n_left_to_next > 0)
1261 u32 bi0, lbi0, next0, *nsh0;
1262 const dpo_id_t *dpo0;
1263 const load_balance_t *lb0;
1270 n_left_to_next -= 1;
1272 b0 = vlib_get_buffer (vm, bi0);
1274 lbi0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
1275 lb0 = load_balance_get(lbi0);
1277 /* SPI + SI are the second word of the NSH header */
1278 nsh0 = vlib_buffer_get_current (b0);
1279 vnet_buffer(b0)->ip.flow_hash = nsh0[1] % lb0->lb_n_buckets;
1281 dpo0 = load_balance_get_bucket_i(lb0,
1282 vnet_buffer(b0)->ip.flow_hash &
1283 (lb0->lb_n_buckets_minus_1));
1285 next0 = dpo0->dpoi_next_node;
1286 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
1288 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1290 load_balance_trace_t *tr = vlib_add_trace (vm, node, b0,
1292 tr->lb_index = lbi0;
1294 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1295 n_left_to_next, bi0, next0);
1298 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1301 return frame->n_vectors;
1305 format_nsh_load_balance_trace (u8 * s, va_list * args)
1307 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1308 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1309 load_balance_trace_t *t = va_arg (*args, load_balance_trace_t *);
1311 s = format (s, "NSH-load-balance: index %d", t->lb_index);
1318 VLIB_REGISTER_NODE (nsh_load_balance_node) = {
1319 .function = nsh_load_balance,
1320 .name = "nsh-load-balance",
1321 .vector_size = sizeof (u32),
1323 .format_trace = format_nsh_load_balance_trace,
1331 format_bier_load_balance_trace (u8 * s, va_list * args)
1333 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1334 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1335 load_balance_trace_t *t = va_arg (*args, load_balance_trace_t *);
1337 s = format (s, "BIER-load-balance: index %d", t->lb_index);
1342 bier_load_balance (vlib_main_t * vm,
1343 vlib_node_runtime_t * node,
1344 vlib_frame_t * frame)
1346 return (load_balance_inline(vm, node, frame, 0));
1352 VLIB_REGISTER_NODE (bier_load_balance_node) = {
1353 .function = bier_load_balance,
1354 .name = "bier-load-balance",
1355 .vector_size = sizeof (u32),
1357 .format_trace = format_bier_load_balance_trace,
1358 .sibling_of = "mpls-load-balance",