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_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 )
321 lb_vip_is_l3dsr(const lb_vip_t *vip)
323 return vip->type == LB_VIP_TYPE_IP4_L3DSR;
326 lb_vip_is_nat4(const lb_vip_t *vip)
328 return vip->type == LB_VIP_TYPE_IP4_NAT4;
331 lb_vip_is_nat6(const lb_vip_t *vip)
333 return vip->type == LB_VIP_TYPE_IP6_NAT6;
336 format_function_t format_lb_vip;
337 format_function_t format_lb_vip_detailed;
339 #define foreach_lb_nat_protocol \
340 _(UDP, 0, udp, "udp") \
341 _(TCP, 1, tcp, "tcp")
344 #define _(N, i, n, s) LB_NAT_PROTOCOL_##N = i,
345 foreach_lb_nat_protocol
350 lb_ip_proto_to_nat_proto (u8 ip_proto)
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;
360 /* Key for Pod's egress SNAT */
392 * for vip + port case, src_ip = vip;
393 * for node ip + node_port, src_ip = node_ip
395 ip46_address_t src_ip;
396 ip46_address_t as_ip;
401 * for vip + port case, src_port = port;
402 * for node ip + node_port, src_port = node_port
405 u16 target_port; /* Network byte order */
412 * Each CPU has its own sticky flow hash table.
413 * One single table is used for all VIPs.
415 lb_hash_t *sticky_ht;
420 * Pool of all Virtual IPs
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.
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.
437 vlib_refcount_t as_refcount;
439 /* hash lookup vip_index by key: {u16: nodeport} */
440 uword * vip_index_by_nodeport;
443 * Some global data is per-cpu
445 lb_per_cpu_t *per_cpu;
448 * Node next index for IP adjacencies, for each of the traffic types.
450 u32 ip_lookup_next_index[LB_VIP_N_TYPES];
453 * Source address used in IPv6 encapsulated traffic
455 ip6_address_t ip6_src_address;
458 * Source address used for IPv4 encapsulated traffic
460 ip4_address_t ip4_src_address;
463 * Number of buckets in the per-cpu sticky hash table.
465 u32 per_cpu_sticky_buckets;
468 * Flow timeout in seconds.
475 vlib_simple_counter_main_t vip_counters[LB_N_VIP_COUNTERS];
478 * DPO used to send packet from IP4/6 lookup to LB node.
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;
487 * Node type for registering to fib changes.
489 fib_node_type_t fib_node_type;
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;
495 /* Static mapping pool */
496 lb_snat_mapping_t * snat_mappings;
499 * API dynamically registered base ID.
503 volatile u32 *writer_lock;
506 vlib_main_t *vlib_main;
507 vnet_main_t *vnet_main;
510 /* args for different vip encap types */
512 ip46_address_t prefix;
516 lb_vip_encap_args_t encap_args;
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;
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
533 int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address,
534 u32 sticky_buckets, u32 flow_timeout);
536 int lb_vip_add(lb_vip_add_args_t args, u32 *vip_index);
538 int lb_vip_del(u32 vip_index);
540 int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index);
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))
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);
547 u32 lb_hash_time_now(vlib_main_t * vm);
549 void lb_garbage_collection();
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);
554 format_function_t format_lb_main;
556 #endif /* LB_PLUGIN_LB_LB_H_ */