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
50 #define LB_VIP_PER_PORT_BUCKETS 1024
51 #define LB_VIP_PER_PORT_MEMORY_SIZE 64<<20
59 LB_NAT4_IN2OUT_NEXT_DROP,
60 LB_NAT4_IN2OUT_NEXT_LOOKUP,
61 LB_NAT4_IN2OUT_N_NEXT,
62 } LB_nat4_in2out_next_t;
65 LB_NAT6_IN2OUT_NEXT_DROP,
66 LB_NAT6_IN2OUT_NEXT_LOOKUP,
67 LB_NAT6_IN2OUT_N_NEXT,
68 } LB_nat6_in2out_next_t;
70 #define foreach_lb_nat_in2out_error \
71 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
72 _(IN2OUT_PACKETS, "Good in2out packets processed") \
73 _(NO_TRANSLATION, "No translation")
76 #define _(sym,str) LB_NAT_IN2OUT_ERROR_##sym,
77 foreach_lb_nat_in2out_error
79 LB_NAT_IN2OUT_N_ERROR,
80 } lb_nat_in2out_error_t;
83 * lb for kube-proxy supports three types of service
86 LB_SRV_TYPE_CLUSTERIP,
92 LB4_NODEPORT_NEXT_IP4_NAT4,
93 LB4_NODEPORT_NEXT_DROP,
95 } lb4_nodeport_next_t;
98 LB6_NODEPORT_NEXT_IP6_NAT6,
99 LB6_NODEPORT_NEXT_DROP,
101 } lb6_nodeport_next_t;
104 * Each VIP is configured with a set of
105 * application server.
109 * Registration to FIB event.
114 * Destination address used to tunnel traffic towards
115 * that application server.
116 * The address is also used as ID and pseudo-random
117 * seed for the load-balancing process.
119 ip46_address_t address;
122 * ASs are indexed by address and VIP Index.
123 * Which means there will be duplicated if the same server
124 * address is used for multiple VIPs.
130 * For now only LB_AS_FLAGS_USED is defined.
134 #define LB_AS_FLAGS_USED 0x1
137 * Rotating timestamp of when LB_AS_FLAGS_USED flag was last set.
139 * AS removal is based on garbage collection and reference counting.
140 * When an AS is removed, there is a race between configuration core
141 * and worker cores which may still add a reference while it should not
142 * be used. This timestamp is used to not remove the AS while a race condition
148 * The FIB entry index for the next-hop
150 fib_node_index_t next_hop_fib_entry_index;
153 * The child index on the FIB entry
155 u32 next_hop_child_index;
158 * The next DPO in the graph to follow.
164 format_function_t format_lb_as;
168 } lb_new_flow_entry_t;
170 #define lb_foreach_vip_counter \
171 _(NEXT_PACKET, "packet from existing sessions", 0) \
172 _(FIRST_PACKET, "first session packet", 1) \
173 _(UNTRACKED_PACKET, "untracked packet", 2) \
174 _(NO_SERVER, "no server configured", 3)
177 #define _(a,b,c) LB_VIP_COUNTER_##a = c,
178 lb_foreach_vip_counter
204 * The load balancer supports IPv4 and IPv6 traffic
205 * and GRE4, GRE6, L3DSR and NAT4, NAT6 encap.
208 LB_VIP_TYPE_IP6_GRE6,
209 LB_VIP_TYPE_IP6_GRE4,
210 LB_VIP_TYPE_IP4_GRE6,
211 LB_VIP_TYPE_IP4_GRE4,
212 LB_VIP_TYPE_IP4_L3DSR,
213 LB_VIP_TYPE_IP4_NAT4,
214 LB_VIP_TYPE_IP6_NAT6,
218 format_function_t format_lb_vip_type;
219 unformat_function_t unformat_lb_vip_type;
222 /* args for different vip encap types */
228 /* Service type. clusterip or nodeport */
231 /* Pod's port corresponding to specific service. network byte order */
234 /* DSCP bits for L3DSR */
238 } lb_vip_encap_args_t;
241 /* all fields in NET byte order */
244 u32 vip_prefix_index;
254 * Load balancing service is provided per VIP+protocol+port.
255 * In this data model, a VIP can be a whole prefix.
256 * But load balancing only
257 * occurs on a per-source-address/port basis. Meaning that if a given source
258 * reuses the same port for multiple destinations within the same VIP,
259 * they will be considered as a single flow.
266 * Vector mapping (flow-hash & new_connect_table_mask) to AS index.
267 * This is used for new flows.
269 lb_new_flow_entry_t *new_flow_table;
272 * New flows table length - 1
273 * (length MUST be a power of 2)
275 u32 new_flow_table_mask;
278 * Last time garbage collection was run to free the ASs.
280 u32 last_garbage_collection;
285 * A Virtual IP represents a given service delivered
286 * by a set of application servers. It can be a single
287 * address or a prefix.
288 * IPv4 prefixes are encoded using IPv4-in-IPv6 embedded address
289 * (i.e. ::/96 prefix).
291 ip46_address_t prefix;
294 * The VIP prefix length.
295 * In case of IPv4, plen = 96 + ip4_plen.
299 /* tcp or udp. If not per-port vip, set to ~0 */
302 /* tcp port or udp port. If not per-port vip, set to ~0 */
305 /* Valid for per-port vip */
306 u32 vip_prefix_index;
309 * The type of traffic for this.
310 * LB_TYPE_UNDEFINED if unknown.
314 /* args for different vip encap types */
315 lb_vip_encap_args_t encap_args;
318 * Flags related to this VIP.
319 * LB_VIP_FLAGS_USED means the VIP is active.
320 * When it is not set, the VIP in the process of being removed.
321 * We cannot immediately remove a VIP because the VIP index still may be stored
322 * in the adjacency index.
325 #define LB_VIP_FLAGS_USED 0x1
328 * Pool of AS indexes used for this VIP.
329 * This also includes ASs that have been removed (but are still referenced).
334 #define lb_vip_is_ip4(type) (type == LB_VIP_TYPE_IP4_GRE6 \
335 || type == LB_VIP_TYPE_IP4_GRE4 \
336 || type == LB_VIP_TYPE_IP4_L3DSR \
337 || type == LB_VIP_TYPE_IP4_NAT4 )
339 #define lb_vip_is_ip6(type) (type == LB_VIP_TYPE_IP6_GRE6 \
340 || type == LB_VIP_TYPE_IP6_GRE4 \
341 || type == LB_VIP_TYPE_IP6_NAT6 )
343 #define lb_encap_is_ip4(vip) ((vip)->type == LB_VIP_TYPE_IP6_GRE4 \
344 || (vip)->type == LB_VIP_TYPE_IP4_GRE4 \
345 || (vip)->type == LB_VIP_TYPE_IP4_L3DSR \
346 || (vip)->type == LB_VIP_TYPE_IP4_NAT4 )
348 #define lb_vip_is_gre4(vip) (((vip)->type == LB_VIP_TYPE_IP6_GRE4 \
349 || (vip)->type == LB_VIP_TYPE_IP4_GRE4) \
350 && ((vip)->port == 0))
353 #define lb_vip_is_gre6(vip) (((vip)->type == LB_VIP_TYPE_IP6_GRE6 \
354 || (vip)->type == LB_VIP_TYPE_IP4_GRE6) \
355 && ((vip)->port == 0))
357 #define lb_vip_is_gre4_port(vip) (((vip)->type == LB_VIP_TYPE_IP6_GRE4 \
358 || (vip)->type == LB_VIP_TYPE_IP4_GRE4) \
359 && ((vip)->port != 0))
361 #define lb_vip_is_gre6_port(vip) (((vip)->type == LB_VIP_TYPE_IP6_GRE6 \
362 || (vip)->type == LB_VIP_TYPE_IP4_GRE6) \
363 && ((vip)->port != 0))
366 lb_vip_is_l3dsr(const lb_vip_t *vip)
368 return (vip->type == LB_VIP_TYPE_IP4_L3DSR && vip->port ==0);
372 lb_vip_is_l3dsr_port(const lb_vip_t *vip)
374 return (vip->type == LB_VIP_TYPE_IP4_L3DSR && vip->port !=0);
377 lb_vip_is_nat4_port(const lb_vip_t *vip)
379 return (vip->type == LB_VIP_TYPE_IP4_NAT4 && vip->port !=0);
382 lb_vip_is_nat6_port(const lb_vip_t *vip)
384 return (vip->type == LB_VIP_TYPE_IP6_NAT6 && vip->port !=0);
387 format_function_t format_lb_vip;
388 format_function_t format_lb_vip_detailed;
390 #define foreach_lb_nat_protocol \
391 _(UDP, 0, udp, "udp") \
392 _(TCP, 1, tcp, "tcp")
395 #define _(N, i, n, s) LB_NAT_PROTOCOL_##N = i,
396 foreach_lb_nat_protocol
401 lb_ip_proto_to_nat_proto (u8 ip_proto)
405 nat_proto = (ip_proto == IP_PROTOCOL_UDP) ? LB_NAT_PROTOCOL_UDP : nat_proto;
406 nat_proto = (ip_proto == IP_PROTOCOL_TCP) ? LB_NAT_PROTOCOL_TCP : nat_proto;
411 /* Key for Pod's egress SNAT */
443 * for vip + port case, src_ip = vip;
444 * for node ip + node_port, src_ip = node_ip
446 ip46_address_t src_ip;
447 ip46_address_t as_ip;
452 * for vip + port case, src_port = port;
453 * for node ip + node_port, src_port = node_port
456 u16 target_port; /* Network byte order */
463 * Each CPU has its own sticky flow hash table.
464 * One single table is used for all VIPs.
466 lb_hash_t *sticky_ht;
471 * Pool of all Virtual IPs
476 * bitmap for vip prefix to support per-port vip
478 uword *vip_prefix_indexes;
482 * ASs are referenced by address and vip index.
483 * The first element (index 0) is special and used only to fill
484 * new_flow_tables when no AS has been configured.
489 * Each AS has an associated reference counter.
490 * As ass[0] has a special meaning, its associated counter
491 * starts at 0 and is decremented instead. i.e. do not use it.
493 vlib_refcount_t as_refcount;
495 /* hash lookup vip_index by key: {u16: nodeport} */
496 uword * vip_index_by_nodeport;
499 * Some global data is per-cpu
501 lb_per_cpu_t *per_cpu;
504 * Node next index for IP adjacencies, for each of the traffic types.
506 u32 ip_lookup_next_index[LB_VIP_N_TYPES];
509 * Source address used in IPv6 encapsulated traffic
511 ip6_address_t ip6_src_address;
514 * Source address used for IPv4 encapsulated traffic
516 ip4_address_t ip4_src_address;
519 * Number of buckets in the per-cpu sticky hash table.
521 u32 per_cpu_sticky_buckets;
524 * Flow timeout in seconds.
531 vlib_simple_counter_main_t vip_counters[LB_N_VIP_COUNTERS];
534 * DPO used to send packet from IP4/6 lookup to LB node.
536 dpo_type_t dpo_gre4_type;
537 dpo_type_t dpo_gre6_type;
538 dpo_type_t dpo_gre4_port_type;
539 dpo_type_t dpo_gre6_port_type;
540 dpo_type_t dpo_l3dsr_type;
541 dpo_type_t dpo_l3dsr_port_type;
542 dpo_type_t dpo_nat4_port_type;
543 dpo_type_t dpo_nat6_port_type;
545 * Node type for registering to fib changes.
547 fib_node_type_t fib_node_type;
549 /* lookup per_port vip by key */
550 clib_bihash_8_8_t vip_index_per_port;
552 /* Find a static mapping by AS IP : target_port */
553 clib_bihash_8_8_t mapping_by_as4;
554 clib_bihash_24_8_t mapping_by_as6;
556 /* Static mapping pool */
557 lb_snat_mapping_t * snat_mappings;
560 * API dynamically registered base ID.
564 volatile u32 *writer_lock;
567 vlib_main_t *vlib_main;
568 vnet_main_t *vnet_main;
571 /* args for different vip encap types */
573 ip46_address_t prefix;
579 lb_vip_encap_args_t encap_args;
582 extern lb_main_t lb_main;
583 extern vlib_node_registration_t lb4_node;
584 extern vlib_node_registration_t lb6_node;
585 extern vlib_node_registration_t lb4_nodeport_node;
586 extern vlib_node_registration_t lb6_nodeport_node;
587 extern vlib_node_registration_t lb_nat4_in2out_node;
588 extern vlib_node_registration_t lb_nat6_in2out_node;
591 * Fix global load-balancer parameters.
592 * @param ip4_address IPv4 source address used for encapsulated traffic
593 * @param ip6_address IPv6 source address used for encapsulated traffic
594 * @return 0 on success. VNET_LB_ERR_XXX on error
596 int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address,
597 u32 sticky_buckets, u32 flow_timeout);
599 int lb_vip_add(lb_vip_add_args_t args, u32 *vip_index);
601 int lb_vip_del(u32 vip_index);
603 int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u8 protocol,
604 u16 port, u32 *vip_index);
606 #define lb_vip_get_by_index(index) (pool_is_free_index(lb_main.vips, index)?NULL:pool_elt_at_index(lb_main.vips, index))
608 int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n);
609 int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n, u8 flush);
610 int lb_flush_vip_as (u32 vip_index, u32 as_index);
612 u32 lb_hash_time_now(vlib_main_t * vm);
614 void lb_garbage_collection();
616 int lb_nat4_interface_add_del (u32 sw_if_index, int is_del);
617 int lb_nat6_interface_add_del (u32 sw_if_index, int is_del);
619 format_function_t format_lb_main;
621 #endif /* LB_PLUGIN_LB_LB_H_ */