blob: b901c5beb84b6f57963ed2c80c623db223b36f8a [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15/**
16 * \brief
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.
26 *
27 * It is also the object in the DP that represents a via-fib-entry in a recursive
28 * route.
29 *
30 */
31
32#ifndef __LOAD_BALANCE_H__
33#define __LOAD_BALANCE_H__
34
35#include <vlib/vlib.h>
36#include <vnet/ip/lookup.h>
37#include <vnet/dpo/dpo.h>
38#include <vnet/fib/fib_types.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000039#include <vnet/fib/fib_entry.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010040
41/**
42 * Load-balance main
43 */
44typedef struct load_balance_main_t_
45{
46 vlib_combined_counter_main_t lbm_to_counters;
47 vlib_combined_counter_main_t lbm_via_counters;
48} load_balance_main_t;
49
50extern load_balance_main_t load_balance_main;
51
52/**
53 * The number of buckets that a load-balance object can have and still
54 * fit in one cache-line
55 */
56#define LB_NUM_INLINE_BUCKETS 4
57
58/**
59 * @brief One path from an [EU]CMP set that the client wants to add to a
60 * load-balance object
61 */
62typedef struct load_balance_path_t_ {
63 /**
64 * ID of the Data-path object.
65 */
66 dpo_id_t path_dpo;
67
68 /**
69 * The index of the FIB path
70 */
71 fib_node_index_t path_index;
72
73 /**
74 * weight for the path.
75 */
76 u32 path_weight;
77} load_balance_path_t;
78
79/**
80 * The FIB DPO provieds;
81 * - load-balancing over the next DPOs in the chain/graph
82 * - per-route counters
83 */
84typedef struct load_balance_t_ {
85 /**
86 * number of buckets in the load-balance. always a power of 2.
87 */
88 u16 lb_n_buckets;
89 /**
90 * number of buckets in the load-balance - 1. used in the switch path
91 * as part of the hash calculation.
92 */
93 u16 lb_n_buckets_minus_1;
94
95 /**
96 * The protocol of packets that traverse this LB.
97 * need in combination with the flow hash config to determine how to hash.
98 * u8.
99 */
100 dpo_proto_t lb_proto;
101
102 /**
Neale Ranns32e1c012016-11-22 17:07:28 +0000103 * Flags from the load-balance's associated fib_entry_t
104 */
105 fib_entry_flag_t lb_fib_entry_flags;
106
107 /**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100108 * The number of locks, which is approximately the number of users,
109 * of this load-balance.
110 * Load-balance objects of via-entries are heavily shared by recursives,
111 * so the lock count is a u32.
112 */
113 u32 lb_locks;
114
115 /**
116 * index of the load-balance map, INVALID if this LB does not use one
117 */
118 index_t lb_map;
119
120 /**
Neale Ranns3ee44042016-10-03 13:05:48 +0100121 * This is the index of the uRPF list for this LB
122 */
123 index_t lb_urpf;
124
125 /**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100126 * the hash config to use when selecting a bucket. this is a u16
127 */
128 flow_hash_config_t lb_hash_config;
129
130 /**
131 * Vector of buckets containing the next DPOs, sized as lbo_num
132 */
133 dpo_id_t *lb_buckets;
134
135 /**
136 * The rest of the cache line is used for buckets. In the common case
137 * where there there are less than 4 buckets, then the buckets are
138 * on the same cachlie and we save ourselves a pointer dereferance in
139 * the data-path.
140 */
141 dpo_id_t lb_buckets_inline[LB_NUM_INLINE_BUCKETS];
142} load_balance_t;
143
Damjan Marioncf478942016-11-07 14:57:50 +0100144STATIC_ASSERT(sizeof(load_balance_t) <= CLIB_CACHE_LINE_BYTES,
145 "A load_balance object size exceeds one cachline");
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100146
147/**
148 * Flags controlling load-balance formatting/display
149 */
150typedef enum load_balance_format_flags_t_ {
151 LOAD_BALANCE_FORMAT_NONE,
152 LOAD_BALANCE_FORMAT_DETAIL = (1 << 0),
153} load_balance_format_flags_t;
154
155/**
156 * Flags controlling load-balance creation and modification
157 */
158typedef enum load_balance_flags_t_ {
159 LOAD_BALANCE_FLAG_NONE = 0,
160 LOAD_BALANCE_FLAG_USES_MAP = (1 << 0),
161} load_balance_flags_t;
162
163extern index_t load_balance_create(u32 num_buckets,
164 dpo_proto_t lb_proto,
165 flow_hash_config_t fhc);
166extern void load_balance_multipath_update(
167 const dpo_id_t *dpo,
Neale Rannsc0790cf2017-01-05 01:01:47 -0800168 const load_balance_path_t * raw_next_hops,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100169 load_balance_flags_t flags);
170
171extern void load_balance_set_bucket(index_t lbi,
172 u32 bucket,
173 const dpo_id_t *next);
Neale Ranns3ee44042016-10-03 13:05:48 +0100174extern void load_balance_set_urpf(index_t lbi,
175 index_t urpf);
Neale Ranns32e1c012016-11-22 17:07:28 +0000176extern void load_balance_set_fib_entry_flags(index_t lbi,
177 fib_entry_flag_t flags);
Neale Ranns3ee44042016-10-03 13:05:48 +0100178extern index_t load_balance_get_urpf(index_t lbi);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100179
180extern u8* format_load_balance(u8 * s, va_list * args);
181
182extern const dpo_id_t *load_balance_get_bucket(index_t lbi,
183 u32 bucket);
184extern int load_balance_is_drop(const dpo_id_t *dpo);
185
186extern f64 load_balance_get_multipath_tolerance(void);
187
188/**
189 * The encapsulation breakages are for fast DP access
190 */
191extern load_balance_t *load_balance_pool;
192static inline load_balance_t*
193load_balance_get (index_t lbi)
194{
195 return (pool_elt_at_index(load_balance_pool, lbi));
196}
197
198#define LB_HAS_INLINE_BUCKETS(_lb) \
199 ((_lb)->lb_n_buckets <= LB_NUM_INLINE_BUCKETS)
200
201static inline const dpo_id_t *
202load_balance_get_bucket_i (const load_balance_t *lb,
203 u32 bucket)
204{
205 ASSERT(bucket < lb->lb_n_buckets);
206
207 if (PREDICT_TRUE(LB_HAS_INLINE_BUCKETS(lb)))
208 {
209 return (&lb->lb_buckets_inline[bucket]);
210 }
211 else
212 {
213 return (&lb->lb_buckets[bucket]);
214 }
215}
216
217extern void load_balance_module_init(void);
218
219#endif