IPx address query functions take a const pointer
[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_encap_is_ip4(vip) ((vip)->type == LB_VIP_TYPE_IP6_GRE4 \
316                              || (vip)->type == LB_VIP_TYPE_IP4_GRE4 \
317                              || (vip)->type == LB_VIP_TYPE_IP4_L3DSR \
318                              || (vip)->type == LB_VIP_TYPE_IP4_NAT4 )
319
320 always_inline bool
321 lb_vip_is_l3dsr(const lb_vip_t *vip)
322 {
323   return vip->type == LB_VIP_TYPE_IP4_L3DSR;
324 }
325 always_inline bool
326 lb_vip_is_nat4(const lb_vip_t *vip)
327 {
328   return vip->type == LB_VIP_TYPE_IP4_NAT4;
329 }
330 always_inline bool
331 lb_vip_is_nat6(const lb_vip_t *vip)
332 {
333   return vip->type == LB_VIP_TYPE_IP6_NAT6;
334 }
335
336 format_function_t format_lb_vip;
337 format_function_t format_lb_vip_detailed;
338
339 #define foreach_lb_nat_protocol \
340   _(UDP, 0, udp, "udp")       \
341   _(TCP, 1, tcp, "tcp")
342
343 typedef enum {
344 #define _(N, i, n, s) LB_NAT_PROTOCOL_##N = i,
345   foreach_lb_nat_protocol
346 #undef _
347 } lb_nat_protocol_t;
348
349 always_inline u32
350 lb_ip_proto_to_nat_proto (u8 ip_proto)
351 {
352   u32 nat_proto = ~0;
353
354   nat_proto = (ip_proto == IP_PROTOCOL_UDP) ? LB_NAT_PROTOCOL_UDP : nat_proto;
355   nat_proto = (ip_proto == IP_PROTOCOL_TCP) ? LB_NAT_PROTOCOL_TCP : nat_proto;
356
357   return nat_proto;
358 }
359
360 /* Key for Pod's egress SNAT */
361 typedef struct {
362   union
363   {
364     struct
365     {
366       ip4_address_t addr;
367       u16 port;
368       u16 protocol:3,
369           fib_index:13;
370     };
371     u64 as_u64;
372   };
373 } lb_snat4_key_t;
374
375 typedef struct
376 {
377   union
378   {
379     struct
380     {
381       ip6_address_t addr;
382       u16 port;
383       u16 protocol;
384       u32 fib_index;
385     };
386     u64 as_u64[3];
387   };
388 } lb_snat6_key_t;
389
390 typedef struct {
391   /**
392    * for vip + port case, src_ip = vip;
393    * for node ip + node_port, src_ip = node_ip
394    */
395   ip46_address_t src_ip;
396   ip46_address_t as_ip;
397   u8 src_ip_is_ipv6;
398   u8 as_ip_is_ipv6;
399   /**
400    * Network byte order
401    * for vip + port case, src_port = port;
402    * for node ip + node_port, src_port = node_port
403    */
404   u16 src_port;
405   u16 target_port; /* Network byte order */
406   u32 vrf_id;
407   u32 fib_index;
408 } lb_snat_mapping_t;
409
410 typedef struct {
411   /**
412    * Each CPU has its own sticky flow hash table.
413    * One single table is used for all VIPs.
414    */
415   lb_hash_t *sticky_ht;
416 } lb_per_cpu_t;
417
418 typedef struct {
419   /**
420    * Pool of all Virtual IPs
421    */
422   lb_vip_t *vips;
423
424   /**
425    * Pool of ASs.
426    * ASs are referenced by address and vip index.
427    * The first element (index 0) is special and used only to fill
428    * new_flow_tables when no AS has been configured.
429    */
430   lb_as_t *ass;
431
432   /**
433    * Each AS has an associated reference counter.
434    * As ass[0] has a special meaning, its associated counter
435    * starts at 0 and is decremented instead. i.e. do not use it.
436    */
437   vlib_refcount_t as_refcount;
438
439   /* hash lookup vip_index by key: {u16: nodeport} */
440   uword * vip_index_by_nodeport;
441
442   /**
443    * Some global data is per-cpu
444    */
445   lb_per_cpu_t *per_cpu;
446
447   /**
448    * Node next index for IP adjacencies, for each of the traffic types.
449    */
450   u32 ip_lookup_next_index[LB_VIP_N_TYPES];
451
452   /**
453    * Source address used in IPv6 encapsulated traffic
454    */
455   ip6_address_t ip6_src_address;
456
457   /**
458    * Source address used for IPv4 encapsulated traffic
459    */
460   ip4_address_t ip4_src_address;
461
462   /**
463    * Number of buckets in the per-cpu sticky hash table.
464    */
465   u32 per_cpu_sticky_buckets;
466
467   /**
468    * Flow timeout in seconds.
469    */
470   u32 flow_timeout;
471
472   /**
473    * Per VIP counter
474    */
475   vlib_simple_counter_main_t vip_counters[LB_N_VIP_COUNTERS];
476
477   /**
478    * DPO used to send packet from IP4/6 lookup to LB node.
479    */
480   dpo_type_t dpo_gre4_type;
481   dpo_type_t dpo_gre6_type;
482   dpo_type_t dpo_l3dsr_type;
483   dpo_type_t dpo_nat4_type;
484   dpo_type_t dpo_nat6_type;
485
486   /**
487    * Node type for registering to fib changes.
488    */
489   fib_node_type_t fib_node_type;
490
491   /* Find a static mapping by AS IP : target_port */
492   clib_bihash_8_8_t mapping_by_as4;
493   clib_bihash_24_8_t mapping_by_as6;
494
495   /* Static mapping pool */
496   lb_snat_mapping_t * snat_mappings;
497
498   /**
499    * API dynamically registered base ID.
500    */
501   u16 msg_id_base;
502
503   volatile u32 *writer_lock;
504
505   /* convenience */
506   vlib_main_t *vlib_main;
507   vnet_main_t *vnet_main;
508 } lb_main_t;
509
510 /* args for different vip encap types */
511 typedef struct {
512   ip46_address_t prefix;
513   u8 plen;
514   lb_vip_type_t type;
515   u32 new_length;
516   lb_vip_encap_args_t encap_args;
517 } lb_vip_add_args_t;
518
519 extern lb_main_t lb_main;
520 extern vlib_node_registration_t lb4_node;
521 extern vlib_node_registration_t lb6_node;
522 extern vlib_node_registration_t lb4_nodeport_node;
523 extern vlib_node_registration_t lb6_nodeport_node;
524 extern vlib_node_registration_t lb_nat4_in2out_node;
525 extern vlib_node_registration_t lb_nat6_in2out_node;
526
527 /**
528  * Fix global load-balancer parameters.
529  * @param ip4_address IPv4 source address used for encapsulated traffic
530  * @param ip6_address IPv6 source address used for encapsulated traffic
531  * @return 0 on success. VNET_LB_ERR_XXX on error
532  */
533 int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address,
534             u32 sticky_buckets, u32 flow_timeout);
535
536 int lb_vip_add(lb_vip_add_args_t args, u32 *vip_index);
537
538 int lb_vip_del(u32 vip_index);
539
540 int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index);
541
542 #define lb_vip_get_by_index(index) (pool_is_free_index(lb_main.vips, index)?NULL:pool_elt_at_index(lb_main.vips, index))
543
544 int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n);
545 int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n);
546
547 u32 lb_hash_time_now(vlib_main_t * vm);
548
549 void lb_garbage_collection();
550
551 int lb_nat4_interface_add_del (u32 sw_if_index, int is_del);
552 int lb_nat6_interface_add_del (u32 sw_if_index, int is_del);
553
554 format_function_t format_lb_main;
555
556 #endif /* LB_PLUGIN_LB_LB_H_ */