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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * lb-plugin implements a MagLev-like load balancer.
18 * http://research.google.com/pubs/pub44824.html
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).
30 #ifndef LB_PLUGIN_LB_LB_H_
31 #define LB_PLUGIN_LB_LB_H_
34 #include <vnet/util/refcount.h>
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>
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
56 LB_NAT4_IN2OUT_NEXT_DROP,
57 LB_NAT4_IN2OUT_NEXT_LOOKUP,
58 LB_NAT4_IN2OUT_N_NEXT,
59 } LB_nat4_in2out_next_t;
62 LB_NAT6_IN2OUT_NEXT_DROP,
63 LB_NAT6_IN2OUT_NEXT_LOOKUP,
64 LB_NAT6_IN2OUT_N_NEXT,
65 } LB_nat6_in2out_next_t;
67 #define foreach_lb_nat_in2out_error \
68 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
69 _(IN2OUT_PACKETS, "Good in2out packets processed") \
70 _(NO_TRANSLATION, "No translation")
73 #define _(sym,str) LB_NAT_IN2OUT_ERROR_##sym,
74 foreach_lb_nat_in2out_error
76 LB_NAT_IN2OUT_N_ERROR,
77 } lb_nat_in2out_error_t;
80 * lb for kube-proxy supports three types of service
83 LB_SRV_TYPE_CLUSTERIP,
89 LB4_NODEPORT_NEXT_IP4_NAT4,
90 LB4_NODEPORT_NEXT_DROP,
92 } lb4_nodeport_next_t;
95 LB6_NODEPORT_NEXT_IP6_NAT6,
96 LB6_NODEPORT_NEXT_DROP,
98 } lb6_nodeport_next_t;
101 * Each VIP is configured with a set of
102 * application server.
106 * Registration to FIB event.
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.
116 ip46_address_t address;
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.
127 * For now only LB_AS_FLAGS_USED is defined.
131 #define LB_AS_FLAGS_USED 0x1
134 * Rotating timestamp of when LB_AS_FLAGS_USED flag was last set.
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
145 * The FIB entry index for the next-hop
147 fib_node_index_t next_hop_fib_entry_index;
150 * The child index on the FIB entry
152 u32 next_hop_child_index;
155 * The next DPO in the graph to follow.
161 format_function_t format_lb_as;
165 } lb_new_flow_entry_t;
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)
174 #define _(a,b,c) LB_VIP_COUNTER_##a = c,
175 lb_foreach_vip_counter
190 * The load balancer supports IPv4 and IPv6 traffic
191 * and GRE4, GRE6, L3DSR and NAT4, NAT6 encap.
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,
204 format_function_t format_lb_vip_type;
205 unformat_function_t unformat_lb_vip_type;
208 /* args for different vip encap types */
214 /* Service type. clusterip or nodeport */
217 /* Service port. network byte order */
220 /* Pod's port corresponding to specific service. network byte order */
223 /* Node's port, can access service via NodeIP:node_port. network byte order */
226 /* DSCP bits for L3DSR */
230 } lb_vip_encap_args_t;
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.
245 * Vector mapping (flow-hash & new_connect_table_mask) to AS index.
246 * This is used for new flows.
248 lb_new_flow_entry_t *new_flow_table;
251 * New flows table length - 1
252 * (length MUST be a power of 2)
254 u32 new_flow_table_mask;
257 * Last time garbage collection was run to free the ASs.
259 u32 last_garbage_collection;
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).
270 ip46_address_t prefix;
273 * The VIP prefix length.
274 * In case of IPv4, plen = 96 + ip4_plen.
279 * The type of traffic for this.
280 * LB_TYPE_UNDEFINED if unknown.
284 /* args for different vip encap types */
285 lb_vip_encap_args_t encap_args;
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.
295 #define LB_VIP_FLAGS_USED 0x1
298 * Pool of AS indexes used for this VIP.
299 * This also includes ASs that have been removed (but are still referenced).
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 )
309 #define lb_vip_is_gre4(vip) ((vip)->type == LB_VIP_TYPE_IP6_GRE4 \
310 || (vip)->type == LB_VIP_TYPE_IP4_GRE4)
312 #define lb_vip_is_gre6(vip) ((vip)->type == LB_VIP_TYPE_IP6_GRE6 \
313 || (vip)->type == LB_VIP_TYPE_IP4_GRE6)
315 #define lb_vip_is_l3dsr(vip) ((vip)->type == LB_VIP_TYPE_IP4_L3DSR)
317 #define lb_vip_is_nat4(vip) ((vip)->type == LB_VIP_TYPE_IP4_NAT4)
319 #define lb_vip_is_nat6(vip) ((vip)->type == LB_VIP_TYPE_IP6_NAT6)
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 )
326 format_function_t format_lb_vip;
327 format_function_t format_lb_vip_detailed;
329 #define foreach_lb_nat_protocol \
330 _(UDP, 0, udp, "udp") \
331 _(TCP, 1, tcp, "tcp")
334 #define _(N, i, n, s) LB_NAT_PROTOCOL_##N = i,
335 foreach_lb_nat_protocol
340 lb_ip_proto_to_nat_proto (u8 ip_proto)
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;
350 /* Key for Pod's egress SNAT */
382 * for vip + port case, src_ip = vip;
383 * for node ip + node_port, src_ip = node_ip
385 ip46_address_t src_ip;
386 ip46_address_t as_ip;
391 * for vip + port case, src_port = port;
392 * for node ip + node_port, src_port = node_port
395 u16 target_port; /* Network byte order */
402 * Each CPU has its own sticky flow hash table.
403 * One single table is used for all VIPs.
405 lb_hash_t *sticky_ht;
410 * Pool of all Virtual IPs
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.
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.
427 vlib_refcount_t as_refcount;
429 /* hash lookup vip_index by key: {u16: nodeport} */
430 uword * vip_index_by_nodeport;
433 * Some global data is per-cpu
435 lb_per_cpu_t *per_cpu;
438 * Node next index for IP adjacencies, for each of the traffic types.
440 u32 ip_lookup_next_index[LB_VIP_N_TYPES];
443 * Source address used in IPv6 encapsulated traffic
445 ip6_address_t ip6_src_address;
448 * Source address used for IPv4 encapsulated traffic
450 ip4_address_t ip4_src_address;
453 * Number of buckets in the per-cpu sticky hash table.
455 u32 per_cpu_sticky_buckets;
458 * Flow timeout in seconds.
465 vlib_simple_counter_main_t vip_counters[LB_N_VIP_COUNTERS];
468 * DPO used to send packet from IP4/6 lookup to LB node.
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;
477 * Node type for registering to fib changes.
479 fib_node_type_t fib_node_type;
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;
485 /* Static mapping pool */
486 lb_snat_mapping_t * snat_mappings;
489 * API dynamically registered base ID.
493 volatile u32 *writer_lock;
496 vlib_main_t *vlib_main;
497 vnet_main_t *vnet_main;
500 /* args for different vip encap types */
502 ip46_address_t prefix;
506 lb_vip_encap_args_t encap_args;
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;
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
523 int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address,
524 u32 sticky_buckets, u32 flow_timeout);
526 int lb_vip_add(lb_vip_add_args_t args, u32 *vip_index);
528 int lb_vip_del(u32 vip_index);
530 int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index);
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))
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);
537 u32 lb_hash_time_now(vlib_main_t * vm);
539 void lb_garbage_collection();
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);
544 format_function_t format_lb_main;
546 #endif /* LB_PLUGIN_LB_LB_H_ */