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