FD.io VPP  v16.09
Vector Packet Processing
lb.h
Go to the documentation of this file.
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  * Second ip lookup can be avoided by sending directly the packet
59  * to ip-rewrite with a configured adjacency.
60  * When set to ~0, the packets are sent to ip6-lookup.
61  */
63 
64  /**
65  * ASs are indexed by address and VIP Index.
66  * Which means there will be duplicated if the same server
67  * address is used for multiple VIPs.
68  */
70 
71  /**
72  * Some per-AS flags.
73  * For now only LB_AS_FLAGS_USED is defined.
74  */
76 
77 #define LB_AS_FLAGS_USED 0x1
78 
79  /**
80  * Rotating timestamp of when LB_AS_FLAGS_USED flag was last set.
81  *
82  * AS removal is based on garbage collection and reference counting.
83  * When an AS is removed, there is a race between configuration core
84  * and worker cores which may still add a reference while it should not
85  * be used. This timestamp is used to not remove the AS while a race condition
86  * may happen.
87  */
89 } lb_as_t;
90 
92 
93 typedef struct {
96 
97 #define lb_foreach_vip_counter \
98  _(TRACKED_SESSION, "tracked session", 0) \
99  _(UNTRACKED_PACKET, "untracked packet", 1)
100 
101 typedef enum {
102 #define _(a,b,c) LB_VIP_COUNTER_##a = c,
104 #undef _
107 
108 /**
109  * The load balancer supports IPv4 and IPv6 traffic
110  * and GRE4 and GRE6 encap.
111  */
112 typedef enum {
118 } lb_vip_type_t;
119 
122 
123 /**
124  * Load balancing service is provided per VIP.
125  * In this data model, a VIP can be a whole prefix.
126  * But load balancing only
127  * occurs on a per-source-address/port basis. Meaning that if a given source
128  * reuses the same port for multiple destinations within the same VIP,
129  * they will be considered as a single flow.
130  */
131 typedef struct {
132 
133  //Runtime
134 
135  /**
136  * Vector mapping (flow-hash & new_connect_table_mask) to AS index.
137  * This is used for new flows.
138  */
140 
141  /**
142  * New flows table length - 1
143  * (length MUST be a power of 2)
144  */
146 
147  /**
148  * Last time garbage collection was run to free the ASs.
149  */
151 
152  //Not runtime
153 
154  /**
155  * A Virtual IP represents a given service delivered
156  * by a set of application servers. It can be a single
157  * address or a prefix.
158  * IPv4 prefixes are encoded using IPv4-in-IPv6 embedded address
159  * (i.e. ::/96 prefix).
160  */
161  ip46_address_t prefix;
162 
163  /**
164  * The VIP prefix length.
165  * In case of IPv4, plen = 96 + ip4_plen.
166  */
168 
169  /**
170  * The type of traffic for this.
171  * LB_TYPE_UNDEFINED if unknown.
172  */
174 
175  /**
176  * Flags related to this VIP.
177  * LB_VIP_FLAGS_USED means the VIP is active.
178  * When it is not set, the VIP in the process of being removed.
179  * We cannot immediately remove a VIP because the VIP index still may be stored
180  * in the adjacency index.
181  */
183 
184  /**
185  * Pool of AS indexes used for this VIP.
186  * This also includes ASs that have been removed (but are still referenced).
187  */
189 
190 #define LB_VIP_FLAGS_USED 0x1
191 
192 } lb_vip_t;
193 
194 #define lb_vip_is_ip4(vip) ((vip)->type == LB_VIP_TYPE_IP4_GRE6 || (vip)->type == LB_VIP_TYPE_IP4_GRE4)
195 #define lb_vip_is_gre4(vip) ((vip)->type == LB_VIP_TYPE_IP6_GRE4 || (vip)->type == LB_VIP_TYPE_IP4_GRE4)
198 
199 typedef struct {
200  /**
201  * Each CPU has its own sticky flow hash table.
202  * One single table is used for all VIPs.
203  */
205 } lb_per_cpu_t;
206 
207 typedef struct {
208  /**
209  * Pool of all Virtual IPs
210  */
212 
213  /**
214  * Pool of ASs.
215  * ASs are referenced by address and vip index.
216  * The first element (index 0) is special and used only to fill
217  * new_flow_tables when no AS has been configured.
218  */
220 
221  /**
222  * Each AS has an associated reference counter.
223  * As ass[0] has a special meaning, its associated counter
224  * starts at 0 and is decremented instead. i.e. do not use it.
225  */
227 
228  /**
229  * Some global data is per-cpu
230  */
232 
233  /**
234  * Node next index for IP adjacencies, for each of the traffic types.
235  */
236  u32 ip_lookup_next_index[LB_VIP_N_TYPES];
237 
238  /**
239  * Source address used in IPv6 encapsulated traffic
240  */
242 
243  /**
244  * Source address used for IPv4 encapsulated traffic
245  */
247 
248  /**
249  * Number of buckets in the per-cpu sticky hash table.
250  */
252 
253  /**
254  * Flow timeout in seconds.
255  */
257 
258  /**
259  * Per VIP counter
260  */
262 
263  /**
264  * API dynamically registered base ID.
265  */
267 
268  volatile u32 *writer_lock;
269 } lb_main_t;
270 
271 /**
272  * struct stored in adj->opaque data.
273  */
274 typedef struct {
275  /**
276  * Index of the VIP associated with that IP adjacency.
277  */
279 } lb_adj_data_t;
280 
281 extern lb_main_t lb_main;
284 
285 /**
286  * Fix global load-balancer parameters.
287  * @param ip4_address IPv4 source address used for encapsulated traffic
288  * @param ip6_address IPv6 source address used for encapsulated traffic
289  * @return 0 on success. VNET_LB_ERR_XXX on error
290  */
291 int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address,
292  u32 sticky_buckets, u32 flow_timeout);
293 
294 int lb_vip_add(ip46_address_t *prefix, u8 plen, lb_vip_type_t type,
295  u32 new_length, u32 *vip_index);
296 int lb_vip_del(u32 vip_index);
297 
298 int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index);
299 
300 #define lb_vip_get_by_index(index) (pool_is_free_index(lb_main.vips, index)?NULL:pool_elt_at_index(lb_main.vips, index))
301 
302 int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n);
303 int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n);
304 
305 /**
306  * Updates the adjacency index stored in the AS such that the second
307  * IP lookup (after encap) can be bypassed.
308  */
309 int lb_as_lookup_bypass(u32 vip_index, ip46_address_t *address, u8 is_disable);
310 
312 
313 void lb_garbage_collection();
314 
316 
317 #endif /* LB_PLUGIN_LB_LB_H_ */
format_function_t format_lb_vip
Definition: lb.h:196
int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: lb.c:398
Each VIP is configured with a set of application server.
Definition: lb.h:48
uword( unformat_function_t)(unformat_input_t *input, va_list *args)
Definition: format.h:231
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
u32 per_cpu_sticky_buckets
Number of buckets in the per-cpu sticky hash table.
Definition: lb.h:251
u16 msg_id_base
API dynamically registered base ID.
Definition: lb.h:266
ip46_address_t prefix
A Virtual IP represents a given service delivered by a set of application servers.
Definition: lb.h:161
struct _vlib_node_registration vlib_node_registration_t
lb_vip_counter_t
Definition: lb.h:101
format_function_t format_lb_vip_type
Definition: lb.h:120
u32 adj_index
Second ip lookup can be avoided by sending directly the packet to ip-rewrite with a configured adjace...
Definition: lb.h:62
u32 vip_index
ASs are indexed by address and VIP Index.
Definition: lb.h:69
lb_hash_t * sticky_ht
Each CPU has its own sticky flow hash table.
Definition: lb.h:204
ip46_address_t address
Destination address used to tunnel traffic towards that application server.
Definition: lb.h:55
int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address, u32 sticky_buckets, u32 flow_timeout)
Fix global load-balancer parameters.
Definition: lb.c:335
unformat_function_t unformat_lb_vip_type
Definition: lb.h:121
u32 flow_timeout
Flow timeout in seconds.
Definition: lb.h:256
Definition: lb.h:207
vlib_refcount_t as_refcount
Each AS has an associated reference counter.
Definition: lb.h:226
format_function_t format_lb_as
Definition: lb.h:91
A collection of simple counters.
Definition: counter.h:59
lb_main_t lb_main
Definition: lb.c:26
lb_vip_t * vips
Pool of all Virtual IPs.
Definition: lb.h:211
u32 last_used
Rotating timestamp of when LB_AS_FLAGS_USED flag was last set.
Definition: lb.h:88
ip4_address_t ip4_src_address
Source address used for IPv4 encapsulated traffic.
Definition: lb.h:246
u8 plen
The VIP prefix length.
Definition: lb.h:167
format_function_t format_lb_main
Definition: lb.h:315
Definition: lb.h:93
vlib_node_registration_t lb6_node
volatile u32 * writer_lock
Definition: lb.h:268
#define lb_foreach_vip_counter
Definition: lb.h:97
u32 vip_index
Index of the VIP associated with that IP adjacency.
Definition: lb.h:278
u32 as_index
Definition: lb.h:94
u32 last_garbage_collection
Last time garbage collection was run to free the ASs.
Definition: lb.h:150
lb_as_t * ass
Pool of ASs.
Definition: lb.h:219
vlib_node_registration_t lb4_node
int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index)
Definition: lb.c:371
lb_vip_type_t type
The type of traffic for this.
Definition: lb.h:173
int lb_vip_del(u32 vip_index)
Definition: lb.c:723
unsigned int u32
Definition: types.h:88
lb_vip_type_t
The load balancer supports IPv4 and IPv6 traffic and GRE4 and GRE6 encap.
Definition: lb.h:112
struct stored in adj->opaque data.
Definition: lb.h:274
void lb_garbage_collection()
Definition: lb.c:206
u32 new_flow_table_mask
New flows table length - 1 (length MUST be a power of 2)
Definition: lb.h:145
format_function_t format_lb_vip_detailed
Definition: lb.h:197
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
lb_per_cpu_t * per_cpu
Some global data is per-cpu.
Definition: lb.h:231
int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: lb.c:530
unsigned short u16
Definition: types.h:57
ip6_address_t ip6_src_address
Source address used in IPv6 encapsulated traffic.
Definition: lb.h:241
unsigned char u8
Definition: types.h:56
int lb_as_lookup_bypass(u32 vip_index, ip46_address_t *address, u8 is_disable)
Updates the adjacency index stored in the AS such that the second IP lookup (after encap) can be bypa...
Definition: lb.c:538
u32 lb_hash_time_now(vlib_main_t *vm)
Definition: lb.c:31
u8 flags
Some per-AS flags.
Definition: lb.h:75
lb_new_flow_entry_t * new_flow_table
Vector mapping (flow-hash & new_connect_table_mask) to AS index.
Definition: lb.h:139
u8 flags
Flags related to this VIP.
Definition: lb.h:182
Load balancing service is provided per VIP.
Definition: lb.h:131
u32 * as_indexes
Pool of AS indexes used for this VIP.
Definition: lb.h:188
int lb_vip_add(ip46_address_t *prefix, u8 plen, lb_vip_type_t type, u32 new_length, u32 *vip_index)
Definition: lb.c:665