Rework kube-proxy into LB plugin
[vpp.git] / src / plugins / 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 <vnet/util/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 #include <vppinfra/hash.h>
41 #include <vppinfra/bihash_8_8.h>
42 #include <vppinfra/bihash_24_8.h>
43 #include <lb/lbhash.h>
44
45 #define LB_DEFAULT_PER_CPU_STICKY_BUCKETS 1 << 10
46 #define LB_DEFAULT_FLOW_TIMEOUT 40
47 #define LB_MAPPING_BUCKETS  1024
48 #define LB_MAPPING_MEMORY_SIZE  64<<20
49
50 typedef enum {
51   LB_NEXT_DROP,
52   LB_N_NEXT,
53 } lb_next_t;
54
55 typedef enum {
56   LB_NAT4_IN2OUT_NEXT_DROP,
57   LB_NAT4_IN2OUT_NEXT_LOOKUP,
58   LB_NAT4_IN2OUT_N_NEXT,
59 } LB_nat4_in2out_next_t;
60
61 typedef enum {
62   LB_NAT6_IN2OUT_NEXT_DROP,
63   LB_NAT6_IN2OUT_NEXT_LOOKUP,
64   LB_NAT6_IN2OUT_N_NEXT,
65 } LB_nat6_in2out_next_t;
66
67 #define foreach_lb_nat_in2out_error                       \
68 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol")         \
69 _(IN2OUT_PACKETS, "Good in2out packets processed")      \
70 _(NO_TRANSLATION, "No translation")
71
72 typedef enum {
73 #define _(sym,str) LB_NAT_IN2OUT_ERROR_##sym,
74   foreach_lb_nat_in2out_error
75 #undef _
76   LB_NAT_IN2OUT_N_ERROR,
77 } lb_nat_in2out_error_t;
78
79 /**
80  * lb for kube-proxy supports three types of service
81  */
82 typedef enum {
83   LB_SRV_TYPE_CLUSTERIP,
84   LB_SRV_TYPE_NODEPORT,
85   LB_SRV_N_TYPES,
86 } lb_svr_type_t;
87
88 typedef enum {
89   LB4_NODEPORT_NEXT_IP4_NAT4,
90   LB4_NODEPORT_NEXT_DROP,
91   LB4_NODEPORT_N_NEXT,
92 } lb4_nodeport_next_t;
93
94 typedef enum {
95   LB6_NODEPORT_NEXT_IP6_NAT6,
96   LB6_NODEPORT_NEXT_DROP,
97   LB6_NODEPORT_N_NEXT,
98 } lb6_nodeport_next_t;
99
100 /**
101  * Each VIP is configured with a set of
102  * application server.
103  */
104 typedef struct {
105   /**
106    * Registration to FIB event.
107    */
108   fib_node_t fib_node;
109
110   /**
111    * Destination address used to tunnel traffic towards
112    * that application server.
113    * The address is also used as ID and pseudo-random
114    * seed for the load-balancing process.
115    */
116   ip46_address_t address;
117
118   /**
119    * ASs are indexed by address and VIP Index.
120    * Which means there will be duplicated if the same server
121    * address is used for multiple VIPs.
122    */
123   u32 vip_index;
124
125   /**
126    * Some per-AS flags.
127    * For now only LB_AS_FLAGS_USED is defined.
128    */
129   u8 flags;
130
131 #define LB_AS_FLAGS_USED 0x1
132
133   /**
134    * Rotating timestamp of when LB_AS_FLAGS_USED flag was last set.
135    *
136    * AS removal is based on garbage collection and reference counting.
137    * When an AS is removed, there is a race between configuration core
138    * and worker cores which may still add a reference while it should not
139    * be used. This timestamp is used to not remove the AS while a race condition
140    * may happen.
141    */
142   u32 last_used;
143
144   /**
145    * The FIB entry index for the next-hop
146    */
147   fib_node_index_t next_hop_fib_entry_index;
148
149   /**
150    * The child index on the FIB entry
151    */
152   u32 next_hop_child_index;
153
154   /**
155    * The next DPO in the graph to follow.
156    */
157   dpo_id_t dpo;
158
159 } lb_as_t;
160
161 format_function_t format_lb_as;
162
163 typedef struct {
164   u32 as_index;
165 } lb_new_flow_entry_t;
166
167 #define lb_foreach_vip_counter \
168  _(NEXT_PACKET, "packet from existing sessions", 0) \
169  _(FIRST_PACKET, "first session packet", 1) \
170  _(UNTRACKED_PACKET, "untracked packet", 2) \
171  _(NO_SERVER, "no server configured", 3)
172
173 typedef enum {
174 #define _(a,b,c) LB_VIP_COUNTER_##a = c,
175   lb_foreach_vip_counter
176 #undef _
177   LB_N_VIP_COUNTERS
178 } lb_vip_counter_t;
179
180 typedef enum {
181   LB_ENCAP_TYPE_GRE4,
182   LB_ENCAP_TYPE_GRE6,
183   LB_ENCAP_TYPE_L3DSR,
184   LB_ENCAP_TYPE_NAT4,
185   LB_ENCAP_TYPE_NAT6,
186   LB_ENCAP_N_TYPES,
187 } lb_encap_type_t;
188
189 /**
190  * The load balancer supports IPv4 and IPv6 traffic
191  * and GRE4, GRE6, L3DSR and NAT4, NAT6 encap.
192  */
193 typedef enum {
194   LB_VIP_TYPE_IP6_GRE6,
195   LB_VIP_TYPE_IP6_GRE4,
196   LB_VIP_TYPE_IP4_GRE6,
197   LB_VIP_TYPE_IP4_GRE4,
198   LB_VIP_TYPE_IP4_L3DSR,
199   LB_VIP_TYPE_IP4_NAT4,
200   LB_VIP_TYPE_IP6_NAT6,
201   LB_VIP_N_TYPES,
202 } lb_vip_type_t;
203
204 format_function_t format_lb_vip_type;
205 unformat_function_t unformat_lb_vip_type;
206
207
208 /* args for different vip encap types */
209 typedef struct {
210   union
211   {
212     struct
213     {
214       /* Service type. clusterip or nodeport */
215       u8 srv_type;
216
217       /* Service port. network byte order */
218       u16 port;
219
220       /* Pod's port corresponding to specific service. network byte order */
221       u16 target_port;
222
223       /* Node's port, can access service via NodeIP:node_port. network byte order */
224       u16 node_port;
225     };
226     /* DSCP bits for L3DSR */
227     u8 dscp;
228     u64 as_u64;
229   };
230 } lb_vip_encap_args_t;
231
232 /**
233  * Load balancing service is provided per VIP.
234  * In this data model, a VIP can be a whole prefix.
235  * But load balancing only
236  * occurs on a per-source-address/port basis. Meaning that if a given source
237  * reuses the same port for multiple destinations within the same VIP,
238  * they will be considered as a single flow.
239  */
240 typedef struct {
241
242   //Runtime
243
244   /**
245    * Vector mapping (flow-hash & new_connect_table_mask) to AS index.
246    * This is used for new flows.
247    */
248   lb_new_flow_entry_t *new_flow_table;
249
250   /**
251    * New flows table length - 1
252    * (length MUST be a power of 2)
253    */
254   u32 new_flow_table_mask;
255
256   /**
257    * Last time garbage collection was run to free the ASs.
258    */
259   u32 last_garbage_collection;
260
261   //Not runtime
262
263   /**
264    * A Virtual IP represents a given service delivered
265    * by a set of application servers. It can be a single
266    * address or a prefix.
267    * IPv4 prefixes are encoded using IPv4-in-IPv6 embedded address
268    * (i.e. ::/96 prefix).
269    */
270   ip46_address_t prefix;
271
272   /**
273    * The VIP prefix length.
274    * In case of IPv4, plen = 96 + ip4_plen.
275    */
276   u8 plen;
277
278   /**
279    * The type of traffic for this.
280    * LB_TYPE_UNDEFINED if unknown.
281    */
282   lb_vip_type_t type;
283
284   /* args for different vip encap types */
285   lb_vip_encap_args_t encap_args;
286
287   /**
288    * Flags related to this VIP.
289    * LB_VIP_FLAGS_USED means the VIP is active.
290    * When it is not set, the VIP in the process of being removed.
291    * We cannot immediately remove a VIP because the VIP index still may be stored
292    * in the adjacency index.
293    */
294   u8 flags;
295 #define LB_VIP_FLAGS_USED 0x1
296
297   /**
298    * Pool of AS indexes used for this VIP.
299    * This also includes ASs that have been removed (but are still referenced).
300    */
301   u32 *as_indexes;
302 } lb_vip_t;
303
304 #define lb_vip_is_ip4(vip) ((vip)->type == LB_VIP_TYPE_IP4_GRE6 \
305                             || (vip)->type == LB_VIP_TYPE_IP4_GRE4 \
306                             || (vip)->type == LB_VIP_TYPE_IP4_L3DSR \
307                             || (vip)->type == LB_VIP_TYPE_IP4_NAT4 )
308
309 #define lb_vip_is_gre4(vip) ((vip)->type == LB_VIP_TYPE_IP6_GRE4 \
310                              || (vip)->type == LB_VIP_TYPE_IP4_GRE4)
311
312 #define lb_vip_is_gre6(vip) ((vip)->type == LB_VIP_TYPE_IP6_GRE6 \
313                              || (vip)->type == LB_VIP_TYPE_IP4_GRE6)
314
315 #define lb_vip_is_l3dsr(vip) ((vip)->type == LB_VIP_TYPE_IP4_L3DSR)
316
317 #define lb_vip_is_nat4(vip) ((vip)->type == LB_VIP_TYPE_IP4_NAT4)
318
319 #define lb_vip_is_nat6(vip) ((vip)->type == LB_VIP_TYPE_IP6_NAT6)
320
321 #define lb_encap_is_ip4(vip) ((vip)->type == LB_VIP_TYPE_IP6_GRE4 \
322                              || (vip)->type == LB_VIP_TYPE_IP4_GRE4 \
323                              || (vip)->type == LB_VIP_TYPE_IP4_L3DSR \
324                              || (vip)->type == LB_VIP_TYPE_IP4_NAT4 )
325
326 format_function_t format_lb_vip;
327 format_function_t format_lb_vip_detailed;
328
329 #define foreach_lb_nat_protocol \
330   _(UDP, 0, udp, "udp")       \
331   _(TCP, 1, tcp, "tcp")
332
333 typedef enum {
334 #define _(N, i, n, s) LB_NAT_PROTOCOL_##N = i,
335   foreach_lb_nat_protocol
336 #undef _
337 } lb_nat_protocol_t;
338
339 always_inline u32
340 lb_ip_proto_to_nat_proto (u8 ip_proto)
341 {
342   u32 nat_proto = ~0;
343
344   nat_proto = (ip_proto == IP_PROTOCOL_UDP) ? LB_NAT_PROTOCOL_UDP : nat_proto;
345   nat_proto = (ip_proto == IP_PROTOCOL_TCP) ? LB_NAT_PROTOCOL_TCP : nat_proto;
346
347   return nat_proto;
348 }
349
350 /* Key for Pod's egress SNAT */
351 typedef struct {
352   union
353   {
354     struct
355     {
356       ip4_address_t addr;
357       u16 port;
358       u16 protocol:3,
359           fib_index:13;
360     };
361     u64 as_u64;
362   };
363 } lb_snat4_key_t;
364
365 typedef struct
366 {
367   union
368   {
369     struct
370     {
371       ip6_address_t addr;
372       u16 port;
373       u16 protocol;
374       u32 fib_index;
375     };
376     u64 as_u64[3];
377   };
378 } lb_snat6_key_t;
379
380 typedef struct {
381   /**
382    * for vip + port case, src_ip = vip;
383    * for node ip + node_port, src_ip = node_ip
384    */
385   ip46_address_t src_ip;
386   ip46_address_t as_ip;
387   u8 src_ip_is_ipv6;
388   u8 as_ip_is_ipv6;
389   /**
390    * Network byte order
391    * for vip + port case, src_port = port;
392    * for node ip + node_port, src_port = node_port
393    */
394   u16 src_port;
395   u16 target_port; /* Network byte order */
396   u32 vrf_id;
397   u32 fib_index;
398 } lb_snat_mapping_t;
399
400 typedef struct {
401   /**
402    * Each CPU has its own sticky flow hash table.
403    * One single table is used for all VIPs.
404    */
405   lb_hash_t *sticky_ht;
406 } lb_per_cpu_t;
407
408 typedef struct {
409   /**
410    * Pool of all Virtual IPs
411    */
412   lb_vip_t *vips;
413
414   /**
415    * Pool of ASs.
416    * ASs are referenced by address and vip index.
417    * The first element (index 0) is special and used only to fill
418    * new_flow_tables when no AS has been configured.
419    */
420   lb_as_t *ass;
421
422   /**
423    * Each AS has an associated reference counter.
424    * As ass[0] has a special meaning, its associated counter
425    * starts at 0 and is decremented instead. i.e. do not use it.
426    */
427   vlib_refcount_t as_refcount;
428
429   /* hash lookup vip_index by key: {u16: nodeport} */
430   uword * vip_index_by_nodeport;
431
432   /**
433    * Some global data is per-cpu
434    */
435   lb_per_cpu_t *per_cpu;
436
437   /**
438    * Node next index for IP adjacencies, for each of the traffic types.
439    */
440   u32 ip_lookup_next_index[LB_VIP_N_TYPES];
441
442   /**
443    * Source address used in IPv6 encapsulated traffic
444    */
445   ip6_address_t ip6_src_address;
446
447   /**
448    * Source address used for IPv4 encapsulated traffic
449    */
450   ip4_address_t ip4_src_address;
451
452   /**
453    * Number of buckets in the per-cpu sticky hash table.
454    */
455   u32 per_cpu_sticky_buckets;
456
457   /**
458    * Flow timeout in seconds.
459    */
460   u32 flow_timeout;
461
462   /**
463    * Per VIP counter
464    */
465   vlib_simple_counter_main_t vip_counters[LB_N_VIP_COUNTERS];
466
467   /**
468    * DPO used to send packet from IP4/6 lookup to LB node.
469    */
470   dpo_type_t dpo_gre4_type;
471   dpo_type_t dpo_gre6_type;
472   dpo_type_t dpo_l3dsr_type;
473   dpo_type_t dpo_nat4_type;
474   dpo_type_t dpo_nat6_type;
475
476   /**
477    * Node type for registering to fib changes.
478    */
479   fib_node_type_t fib_node_type;
480
481   /* Find a static mapping by AS IP : target_port */
482   clib_bihash_8_8_t mapping_by_as4;
483   clib_bihash_24_8_t mapping_by_as6;
484
485   /* Static mapping pool */
486   lb_snat_mapping_t * snat_mappings;
487
488   /**
489    * API dynamically registered base ID.
490    */
491   u16 msg_id_base;
492
493   volatile u32 *writer_lock;
494
495   /* convenience */
496   vlib_main_t *vlib_main;
497   vnet_main_t *vnet_main;
498 } lb_main_t;
499
500 /* args for different vip encap types */
501 typedef struct {
502   ip46_address_t prefix;
503   u8 plen;
504   lb_vip_type_t type;
505   u32 new_length;
506   lb_vip_encap_args_t encap_args;
507 } lb_vip_add_args_t;
508
509 extern lb_main_t lb_main;
510 extern vlib_node_registration_t lb4_node;
511 extern vlib_node_registration_t lb6_node;
512 extern vlib_node_registration_t lb4_nodeport_node;
513 extern vlib_node_registration_t lb6_nodeport_node;
514 extern vlib_node_registration_t lb_nat4_in2out_node;
515 extern vlib_node_registration_t lb_nat6_in2out_node;
516
517 /**
518  * Fix global load-balancer parameters.
519  * @param ip4_address IPv4 source address used for encapsulated traffic
520  * @param ip6_address IPv6 source address used for encapsulated traffic
521  * @return 0 on success. VNET_LB_ERR_XXX on error
522  */
523 int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address,
524             u32 sticky_buckets, u32 flow_timeout);
525
526 int lb_vip_add(lb_vip_add_args_t args, u32 *vip_index);
527
528 int lb_vip_del(u32 vip_index);
529
530 int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index);
531
532 #define lb_vip_get_by_index(index) (pool_is_free_index(lb_main.vips, index)?NULL:pool_elt_at_index(lb_main.vips, index))
533
534 int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n);
535 int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n);
536
537 u32 lb_hash_time_now(vlib_main_t * vm);
538
539 void lb_garbage_collection();
540
541 int lb_nat4_interface_add_del (u32 sw_if_index, int is_del);
542 int lb_nat6_interface_add_del (u32 sw_if_index, int is_del);
543
544 format_function_t format_lb_main;
545
546 #endif /* LB_PLUGIN_LB_LB_H_ */