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.
17 * The load-balance object represents an ECMP choice. The buckets of a load
18 * balance object point to the sub-graph after the choice is made.
19 * THe load-balance object is also object type returned from a FIB table lookup.
20 * As such it needs to represent the case where there is only one coice. It may
21 * seem like overkill to use a load-balance object in this case, but the reason
22 * is for performance. If the load-balance object were not the result of the FIB
23 * lookup, then some other object would be. The case where there was ECMP
24 * this other object would need a load-balance as a parent and hence just add
25 * an unnecessary indirection.
27 * It is also the object in the DP that represents a via-fib-entry in a recursive
32 #ifndef __LOAD_BALANCE_H__
33 #define __LOAD_BALANCE_H__
35 #include <vlib/vlib.h>
36 #include <vnet/ip/lookup.h>
37 #include <vnet/dpo/dpo.h>
38 #include <vnet/fib/fib_types.h>
43 typedef struct load_balance_main_t_
45 vlib_combined_counter_main_t lbm_to_counters;
46 vlib_combined_counter_main_t lbm_via_counters;
47 } load_balance_main_t;
49 extern load_balance_main_t load_balance_main;
52 * The number of buckets that a load-balance object can have and still
53 * fit in one cache-line
55 #define LB_NUM_INLINE_BUCKETS 4
58 * @brief One path from an [EU]CMP set that the client wants to add to a
61 typedef struct load_balance_path_t_ {
63 * ID of the Data-path object.
68 * The index of the FIB path
70 fib_node_index_t path_index;
73 * weight for the path.
76 } load_balance_path_t;
79 * The FIB DPO provieds;
80 * - load-balancing over the next DPOs in the chain/graph
81 * - per-route counters
83 typedef struct load_balance_t_ {
85 * number of buckets in the load-balance. always a power of 2.
89 * number of buckets in the load-balance - 1. used in the switch path
90 * as part of the hash calculation.
92 u16 lb_n_buckets_minus_1;
95 * The protocol of packets that traverse this LB.
96 * need in combination with the flow hash config to determine how to hash.
102 * The number of locks, which is approximately the number of users,
103 * of this load-balance.
104 * Load-balance objects of via-entries are heavily shared by recursives,
105 * so the lock count is a u32.
110 * index of the load-balance map, INVALID if this LB does not use one
115 * the hash config to use when selecting a bucket. this is a u16
117 flow_hash_config_t lb_hash_config;
120 * Vector of buckets containing the next DPOs, sized as lbo_num
122 dpo_id_t *lb_buckets;
125 * The rest of the cache line is used for buckets. In the common case
126 * where there there are less than 4 buckets, then the buckets are
127 * on the same cachlie and we save ourselves a pointer dereferance in
130 dpo_id_t lb_buckets_inline[LB_NUM_INLINE_BUCKETS];
133 _Static_assert(sizeof(load_balance_t) <= CLIB_CACHE_LINE_BYTES,
134 "A load_balance object size exceeds one cachline");
137 * Flags controlling load-balance formatting/display
139 typedef enum load_balance_format_flags_t_ {
140 LOAD_BALANCE_FORMAT_NONE,
141 LOAD_BALANCE_FORMAT_DETAIL = (1 << 0),
142 } load_balance_format_flags_t;
145 * Flags controlling load-balance creation and modification
147 typedef enum load_balance_flags_t_ {
148 LOAD_BALANCE_FLAG_NONE = 0,
149 LOAD_BALANCE_FLAG_USES_MAP = (1 << 0),
150 } load_balance_flags_t;
152 extern index_t load_balance_create(u32 num_buckets,
153 dpo_proto_t lb_proto,
154 flow_hash_config_t fhc);
155 extern void load_balance_multipath_update(
157 load_balance_path_t * raw_next_hops,
158 load_balance_flags_t flags);
160 extern void load_balance_set_bucket(index_t lbi,
162 const dpo_id_t *next);
164 extern u8* format_load_balance(u8 * s, va_list * args);
166 extern const dpo_id_t *load_balance_get_bucket(index_t lbi,
168 extern int load_balance_is_drop(const dpo_id_t *dpo);
170 extern f64 load_balance_get_multipath_tolerance(void);
173 * The encapsulation breakages are for fast DP access
175 extern load_balance_t *load_balance_pool;
176 static inline load_balance_t*
177 load_balance_get (index_t lbi)
179 return (pool_elt_at_index(load_balance_pool, lbi));
182 #define LB_HAS_INLINE_BUCKETS(_lb) \
183 ((_lb)->lb_n_buckets <= LB_NUM_INLINE_BUCKETS)
185 static inline const dpo_id_t *
186 load_balance_get_bucket_i (const load_balance_t *lb,
189 ASSERT(bucket < lb->lb_n_buckets);
191 if (PREDICT_TRUE(LB_HAS_INLINE_BUCKETS(lb)))
193 return (&lb->lb_buckets_inline[bucket]);
197 return (&lb->lb_buckets[bucket]);
201 extern void load_balance_module_init(void);