VPP-130: MagLev-like Load Balancer
[vpp.git] / plugins / lb-plugin / lb / lb.h
1 /*
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 /**
17  * lb-plugin implements a MagLev-like load balancer.
18  * http://research.google.com/pubs/pub44824.html
19  *
20  * It hasn't been tested for interoperability with the original MagLev
21  * but intends to provide similar functionality.
22  * The load-balancer receives traffic destined to VIP (Virtual IP)
23  * addresses from one or multiple(ECMP) routers.
24  * The load-balancer tunnels the traffic toward many application servers
25  * ensuring session stickyness (i.e. that a single sessions is tunneled
26  * towards a single application server).
27  *
28  */
29
30 #ifndef LB_PLUGIN_LB_LB_H_
31 #define LB_PLUGIN_LB_LB_H_
32
33 #include <lb/util.h>
34 #include <lb/refcount.h>
35
36 #include <vnet/vnet.h>
37 #include <vnet/ip/ip.h>
38
39 #include <lb/lbhash.h>
40
41 #define LB_DEFAULT_PER_CPU_STICKY_BUCKETS 1 << 10
42 #define LB_DEFAULT_FLOW_TIMEOUT 40
43
44 /**
45  * Each VIP is configured with a set of
46  * application server.
47  */
48 typedef struct {
49   /**
50    * Destination address used to tunnel traffic towards
51    * that application server.
52    * The address is also used as ID and pseudo-random
53    * seed for the load-balancing process.
54    */
55   ip46_address_t address;
56
57   /**
58    * ASs are indexed by address and VIP Index.
59    * Which means there will be duplicated if the same server
60    * address is used for multiple VIPs.
61    */
62   u32 vip_index;
63
64   /**
65    * Some per-AS flags.
66    * For now only LB_AS_FLAGS_USED is defined.
67    */
68   u8 flags;
69
70 #define LB_AS_FLAGS_USED 0x1
71
72   /**
73    * Rotating timestamp of when LB_AS_FLAGS_USED flag was last set.
74    *
75    * AS removal is based on garbage collection and reference counting.
76    * When an AS is removed, there is a race between configuration core
77    * and worker cores which may still add a reference while it should not
78    * be used. This timestamp is used to not remove the AS while a race condition
79    * may happen.
80    */
81   u32 last_used;
82 } lb_as_t;
83
84 format_function_t format_lb_as;
85
86 typedef struct {
87   u32 as_index;
88 } lb_new_flow_entry_t;
89
90 #define lb_foreach_vip_counter \
91  _(TRACKED_SESSION, "tracked session", 0) \
92  _(UNTRACKED_PACKET, "untracked packet", 1)
93
94 typedef enum {
95 #define _(a,b,c) LB_VIP_COUNTER_##a = c,
96   lb_foreach_vip_counter
97 #undef _
98   LB_N_VIP_COUNTERS
99 } lb_vip_counter_t;
100
101 /**
102  * The load balancer supports IPv4 and IPv6 traffic
103  * and GRE4 and GRE6 encap.
104  */
105 typedef enum {
106   LB_VIP_TYPE_IP6_GRE6,
107   LB_VIP_TYPE_IP6_GRE4,
108   LB_VIP_TYPE_IP4_GRE6,
109   LB_VIP_TYPE_IP4_GRE4,
110   LB_VIP_N_TYPES,
111 } lb_vip_type_t;
112
113 format_function_t format_lb_vip_type;
114 unformat_function_t unformat_lb_vip_type;
115
116 /**
117  * Load balancing service is provided per VIP.
118  * In this data model, a VIP can be a whole prefix.
119  * But load balancing only
120  * occurs on a per-source-address/port basis. Meaning that if a given source
121  * reuses the same port for multiple destinations within the same VIP,
122  * they will be considered as a single flow.
123  */
124 typedef struct {
125
126   //Runtime
127
128   /**
129    * Vector mapping (flow-hash & new_connect_table_mask) to AS index.
130    * This is used for new flows.
131    */
132   lb_new_flow_entry_t *new_flow_table;
133
134   /**
135    * New flows table length - 1
136    * (length MUST be a power of 2)
137    */
138   u32 new_flow_table_mask;
139
140   /**
141    * Last time garbage collection was run to free the ASs.
142    */
143   u32 last_garbage_collection;
144
145   //Not runtime
146
147   /**
148    * A Virtual IP represents a given service delivered
149    * by a set of application servers. It can be a single
150    * address or a prefix.
151    * IPv4 prefixes are encoded using IPv4-in-IPv6 embedded address
152    * (i.e. ::/96 prefix).
153    */
154   ip46_address_t prefix;
155
156   /**
157    * The VIP prefix length.
158    * In case of IPv4, plen = 96 + ip4_plen.
159    */
160   u8 plen;
161
162   /**
163    * The type of traffic for this.
164    * LB_TYPE_UNDEFINED if unknown.
165    */
166   lb_vip_type_t type;
167
168   /**
169    * Flags related to this VIP.
170    * LB_VIP_FLAGS_USED means the VIP is active.
171    * When it is not set, the VIP in the process of being removed.
172    * We cannot immediately remove a VIP because the VIP index still may be stored
173    * in the adjacency index.
174    */
175   u8 flags;
176
177   /**
178    * Pool of AS indexes used for this VIP.
179    * This also includes ASs that have been removed (but are still referenced).
180    */
181   u32 *as_indexes;
182
183 #define LB_VIP_FLAGS_USED 0x1
184
185 } lb_vip_t;
186
187 #define lb_vip_is_ip4(vip) ((vip)->type == LB_VIP_TYPE_IP4_GRE6 || (vip)->type == LB_VIP_TYPE_IP4_GRE4)
188 #define lb_vip_is_gre4(vip) ((vip)->type == LB_VIP_TYPE_IP6_GRE4 || (vip)->type == LB_VIP_TYPE_IP4_GRE4)
189 format_function_t format_lb_vip;
190 format_function_t format_lb_vip_detailed;
191
192 typedef struct {
193   /**
194    * Each CPU has its own sticky flow hash table.
195    * One single table is used for all VIPs.
196    */
197   lb_hash_t *sticky_ht;
198 } lb_per_cpu_t;
199
200 typedef struct {
201   /**
202    * Pool of all Virtual IPs
203    */
204   lb_vip_t *vips;
205
206   /**
207    * Pool of ASs.
208    * ASs are referenced by address and vip index.
209    * The first element (index 0) is special and used only to fill
210    * new_flow_tables when no AS has been configured.
211    */
212   lb_as_t *ass;
213
214   /**
215    * Each AS has an associated reference counter.
216    * As ass[0] has a special meaning, its associated counter
217    * starts at 0 and is decremented instead. i.e. do not use it.
218    */
219   vlib_refcount_t as_refcount;
220
221   /**
222    * Some global data is per-cpu
223    */
224   lb_per_cpu_t *per_cpu;
225
226   /**
227    * Node next index for IP adjacencies, for each of the traffic types.
228    */
229   u32 ip_lookup_next_index[LB_VIP_N_TYPES];
230
231   /**
232    * Source address used in IPv6 encapsulated traffic
233    */
234   ip6_address_t ip6_src_address;
235
236   /**
237    * Source address used for IPv4 encapsulated traffic
238    */
239   ip4_address_t ip4_src_address;
240
241   /**
242    * Number of buckets in the per-cpu sticky hash table.
243    */
244   u32 per_cpu_sticky_buckets;
245
246   /**
247    * Flow timeout in seconds.
248    */
249   u32 flow_timeout;
250
251   /**
252    * Per VIP counter
253    */
254   vlib_simple_counter_main_t vip_counters[LB_N_VIP_COUNTERS];
255
256   /**
257    * API dynamically registered base ID.
258    */
259   u16 msg_id_base;
260
261   volatile u32 *writer_lock;
262 } lb_main_t;
263
264 /**
265  * struct stored in adj->opaque data.
266  */
267 typedef struct {
268   /**
269    * Index of the VIP associated with that IP adjacency.
270    */
271   u32 vip_index;
272 } lb_adj_data_t;
273
274 extern lb_main_t lb_main;
275 extern vlib_node_registration_t lb6_node;
276 extern vlib_node_registration_t lb4_node;
277
278 /**
279  * Fix global load-balancer parameters.
280  * @param ip4_address IPv4 source address used for encapsulated traffic
281  * @param ip6_address IPv6 source address used for encapsulated traffic
282  * @return 0 on success. VNET_LB_ERR_XXX on error
283  */
284 int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address,
285             u32 sticky_buckets, u32 flow_timeout);
286
287 int lb_vip_add(ip46_address_t *prefix, u8 plen, lb_vip_type_t type,
288                u32 new_length, u32 *vip_index);
289 int lb_vip_del(u32 vip_index);
290
291 int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index);
292
293 #define lb_vip_get_by_index(index) (pool_is_free_index(lb_main.vips, index)?NULL:pool_elt_at_index(lb_main.vips, index))
294
295 int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n);
296 int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n);
297
298 u32 lb_hash_time_now(vlib_main_t * vm);
299
300 void lb_garbage_collection();
301
302 format_function_t format_lb_main;
303
304 #endif /* LB_PLUGIN_LB_LB_H_ */