Load Balancer: Use FIB 2.0
[vpp.git] / plugins / lb-plugin / lb / node.c
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 #include <lb/lb.h>
17
18 #include <vnet/gre/packet.h>
19 #include <lb/lbhash.h>
20
21 #define foreach_lb_error \
22  _(NONE, "no error") \
23  _(PROTO_NOT_SUPPORTED, "protocol not supported") \
24  _(NO_SERVER, "no configured application server")
25
26 typedef enum {
27 #define _(sym,str) LB_ERROR_##sym,
28   foreach_lb_error
29 #undef _
30     LB_N_ERROR,
31 } lb_error_t;
32
33 static char *lb_error_strings[] = {
34 #define _(sym,string) string,
35     foreach_lb_error
36 #undef _
37 };
38
39 typedef struct {
40   u32 vip_index;
41   u32 as_index;
42 } lb_trace_t;
43
44 u8 *
45 format_lb_trace (u8 * s, va_list * args)
46 {
47   lb_main_t *lbm = &lb_main;
48   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
49   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
50   lb_trace_t *t = va_arg (*args, lb_trace_t *);
51   s = format(s, "lb vip[%d]: %U\n", t->vip_index, format_lb_vip, &lbm->vips[t->vip_index]);
52   s = format(s, "lb as[%d]: %U\n", t->as_index, format_lb_as, &lbm->ass[t->as_index]);
53   return s;
54 }
55
56 lb_hash_t *lb_get_sticky_table(u32 cpu_index)
57 {
58   lb_main_t *lbm = &lb_main;
59   lb_hash_t *sticky_ht = lbm->per_cpu[cpu_index].sticky_ht;
60   //Check if size changed
61   if (PREDICT_FALSE(sticky_ht && (lbm->per_cpu_sticky_buckets != lb_hash_nbuckets(sticky_ht)))) {
62
63     //Dereference everything in there
64     lb_hash_entry_t *e;
65     lb_hash_foreach_entry(sticky_ht, e) {
66       vlib_refcount_add(&lbm->as_refcount, cpu_index, e->value, -1);
67       vlib_refcount_add(&lbm->as_refcount, cpu_index, 0, -1);
68     }
69
70     lb_hash_free(sticky_ht);
71     sticky_ht = NULL;
72   }
73
74   //Create if necessary
75   if (PREDICT_FALSE(sticky_ht == NULL)) {
76     lbm->per_cpu[cpu_index].sticky_ht = lb_hash_alloc(lbm->per_cpu_sticky_buckets, lbm->flow_timeout);
77     sticky_ht = lbm->per_cpu[cpu_index].sticky_ht;
78     clib_warning("Regenerated sticky table %p", sticky_ht);
79   }
80
81   ASSERT(sticky_ht);
82
83   //Update timeout
84   sticky_ht->timeout = lbm->flow_timeout;
85   return sticky_ht;
86 }
87
88 static_always_inline uword
89 lb_node_fn (vlib_main_t * vm,
90          vlib_node_runtime_t * node, vlib_frame_t * frame,
91          u8 is_input_v4, //Compile-time parameter stating that is input is v4 (or v6)
92          u8 is_encap_v4) //Compile-time parameter stating that is GRE encap is v4 (or v6)
93 {
94   lb_main_t *lbm = &lb_main;
95   vlib_node_runtime_t *error_node = node;
96   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
97   u32 cpu_index = os_get_cpu_number();
98   u32 lb_time = lb_hash_time_now(vm);
99
100   lb_hash_t *sticky_ht = lb_get_sticky_table(cpu_index);
101   from = vlib_frame_vector_args (frame);
102   n_left_from = frame->n_vectors;
103   next_index = node->cached_next_index;
104
105   while (n_left_from > 0)
106   {
107     vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
108     while (n_left_from > 0 && n_left_to_next > 0)
109     {
110       u32 pi0;
111       vlib_buffer_t *p0;
112       lb_vip_t *vip0;
113       lb_as_t *as0;
114       gre_header_t *gre0;
115       u16 len0;
116       u32 value0, available_index0, hash0;
117       u64 key0[5];
118       lb_error_t error0 = LB_ERROR_NONE;
119
120       if (PREDICT_TRUE(n_left_from > 1))
121       {
122         vlib_buffer_t *p2;
123         p2 = vlib_get_buffer(vm, from[1]);
124         vlib_prefetch_buffer_header(p2, STORE);
125         /* IPv4 + 8 = 28. possibly plus -40 */
126         CLIB_PREFETCH (vlib_buffer_get_current(p2) - 40, 128, STORE);
127       }
128
129       pi0 = to_next[0] = from[0];
130       from += 1;
131       n_left_from -= 1;
132       to_next += 1;
133       n_left_to_next -= 1;
134
135       p0 = vlib_get_buffer (vm, pi0);
136       vip0 = pool_elt_at_index (lbm->vips,
137                                 vnet_buffer (p0)->ip.adj_index[VLIB_TX]);
138
139       if (is_input_v4) {
140         ip4_header_t *ip40;
141         ip40 = vlib_buffer_get_current (p0);
142         len0 = clib_net_to_host_u16(ip40->length);
143         key0[0] = (u64) ip40->src_address.as_u32;
144         key0[1] = (u64) ip40->dst_address.as_u32;
145         key0[2] = 0;
146         key0[3] = 0;
147         key0[4] = ((u64)((udp_header_t *)(ip40 + 1))->src_port << 32) |
148             ((u64)((udp_header_t *)(ip40 + 1))->dst_port << 16);
149
150         hash0 = lb_hash_hash(key0);
151       } else {
152         ip6_header_t *ip60;
153         ip60 = vlib_buffer_get_current (p0);
154         len0 = clib_net_to_host_u16(ip60->payload_length) + sizeof(ip6_header_t);
155         key0[0] = ip60->src_address.as_u64[0];
156         key0[1] = ip60->src_address.as_u64[1];
157         key0[2] = ip60->dst_address.as_u64[0];
158         key0[3] = ip60->dst_address.as_u64[1];
159         key0[4] = ((u64)((udp_header_t *)(ip60 + 1))->src_port << 32) |
160             ((u64)((udp_header_t *)(ip60 + 1))->dst_port << 16);
161
162         hash0 = lb_hash_hash(key0);
163       }
164
165       //NOTE: This is an ugly trick to not include the VIP index in the hash calculation
166       //but actually use it in the key determination.
167       key0[4] |= ((vip0 - lbm->vips));
168
169       lb_hash_get(sticky_ht, key0, hash0, lb_time, &available_index0, &value0);
170       if (PREDICT_TRUE(value0 != ~0)) {
171         //Found an existing entry
172         as0 = &lbm->ass[value0];
173       } else if (PREDICT_TRUE(available_index0 != ~0)) {
174         //There is an available slot for a new flow
175         as0 = &lbm->ass[vip0->new_flow_table[hash0 & vip0->new_flow_table_mask].as_index];
176         if (PREDICT_FALSE(as0 == lbm->ass)) { //Special first element
177           error0 = LB_ERROR_NO_SERVER;
178         } else {
179           vlib_increment_simple_counter(&lbm->vip_counters[LB_VIP_COUNTER_TRACKED_SESSION],
180                                         cpu_index, vip0 - lbm->vips, 1);
181         }
182
183         //TODO: There are race conditions with as0 and vip0 manipulation.
184         //Configuration may be changed, vectors resized, etc...
185
186         //Dereference previously used
187         vlib_refcount_add(&lbm->as_refcount, cpu_index, lb_hash_available_value(sticky_ht, available_index0), -1);
188         vlib_refcount_add(&lbm->as_refcount, cpu_index, as0 - lbm->ass, 1);
189
190         //Add sticky entry
191         //Note that when there is no AS configured, an entry is configured anyway.
192         //But no configured AS is not something that should happen
193         lb_hash_put(sticky_ht, key0, as0 - lbm->ass, available_index0, lb_time);
194       } else {
195         //Could not store new entry in the table
196         as0 = &lbm->ass[vip0->new_flow_table[hash0 & vip0->new_flow_table_mask].as_index];
197         vlib_increment_simple_counter(&lbm->vip_counters[LB_VIP_COUNTER_UNTRACKED_PACKET],
198                                                 cpu_index, vip0 - lbm->vips, 1);
199       }
200
201       //Now let's encap
202       if (is_encap_v4) {
203         ip4_header_t *ip40;
204         vlib_buffer_advance(p0, - sizeof(ip4_header_t) - sizeof(gre_header_t));
205         ip40 = vlib_buffer_get_current(p0);
206         gre0 = (gre_header_t *)(ip40 + 1);
207         ip40->src_address = lbm->ip4_src_address;
208         ip40->dst_address = as0->address.ip4;
209         ip40->ip_version_and_header_length = 0x45;
210         ip40->ttl = 128;
211         ip40->length = clib_host_to_net_u16(len0 + sizeof(gre_header_t) + sizeof(ip4_header_t));
212         ip40->protocol = IP_PROTOCOL_GRE;
213         ip40->checksum = ip4_header_checksum (ip40);
214       } else {
215         ip6_header_t *ip60;
216         vlib_buffer_advance(p0, - sizeof(ip6_header_t) - sizeof(gre_header_t));
217         ip60 = vlib_buffer_get_current(p0);
218         gre0 = (gre_header_t *)(ip60 + 1);
219         ip60->dst_address = as0->address.ip6;
220         ip60->src_address = lbm->ip6_src_address;
221         ip60->hop_limit = 128;
222         ip60->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6<<28);
223         ip60->payload_length = clib_host_to_net_u16(len0 + sizeof(gre_header_t));
224         ip60->protocol = IP_PROTOCOL_GRE;
225       }
226
227       gre0->flags_and_version = 0;
228       gre0->protocol = (is_input_v4)?
229           clib_host_to_net_u16(0x0800):
230           clib_host_to_net_u16(0x86DD);
231
232       vnet_buffer (p0)->ip.adj_index[VLIB_TX] = as0->dpo.dpoi_index;
233
234       if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
235       {
236         lb_trace_t *tr = vlib_add_trace (vm, node, p0, sizeof (*tr));
237         tr->as_index = as0 - lbm->ass;
238         tr->vip_index = vip0 - lbm->vips;
239       }
240
241       p0->error = error_node->errors[error0];
242       vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
243                                        n_left_to_next, pi0,
244                                        as0->dpo.dpoi_next_node);
245     }
246     vlib_put_next_frame (vm, node, next_index, n_left_to_next);
247   }
248
249   return frame->n_vectors;
250 }
251
252 static uword
253 lb6_gre6_node_fn (vlib_main_t * vm,
254          vlib_node_runtime_t * node, vlib_frame_t * frame)
255 {
256   return lb_node_fn(vm, node, frame, 0, 0);
257 }
258
259 static uword
260 lb6_gre4_node_fn (vlib_main_t * vm,
261          vlib_node_runtime_t * node, vlib_frame_t * frame)
262 {
263   return lb_node_fn(vm, node, frame, 0, 1);
264 }
265
266 static uword
267 lb4_gre6_node_fn (vlib_main_t * vm,
268          vlib_node_runtime_t * node, vlib_frame_t * frame)
269 {
270   return lb_node_fn(vm, node, frame, 1, 0);
271 }
272
273 static uword
274 lb4_gre4_node_fn (vlib_main_t * vm,
275          vlib_node_runtime_t * node, vlib_frame_t * frame)
276 {
277   return lb_node_fn(vm, node, frame, 1, 1);
278 }
279
280 VLIB_REGISTER_NODE (lb6_gre6_node) =
281 {
282   .function = lb6_gre6_node_fn,
283   .name = "lb6-gre6",
284   .vector_size = sizeof (u32),
285   .format_trace = format_lb_trace,
286
287   .n_errors = LB_N_ERROR,
288   .error_strings = lb_error_strings,
289
290   .n_next_nodes = LB_N_NEXT,
291   .next_nodes =
292   {
293       [LB_NEXT_DROP] = "error-drop"
294   },
295 };
296
297 VLIB_REGISTER_NODE (lb6_gre4_node) =
298 {
299   .function = lb6_gre4_node_fn,
300   .name = "lb6-gre4",
301   .vector_size = sizeof (u32),
302   .format_trace = format_lb_trace,
303
304   .n_errors = LB_N_ERROR,
305   .error_strings = lb_error_strings,
306
307   .n_next_nodes = LB_N_NEXT,
308   .next_nodes =
309   {
310       [LB_NEXT_DROP] = "error-drop"
311   },
312 };
313
314 VLIB_REGISTER_NODE (lb4_gre6_node) =
315 {
316   .function = lb4_gre6_node_fn,
317   .name = "lb4-gre6",
318   .vector_size = sizeof (u32),
319   .format_trace = format_lb_trace,
320
321   .n_errors = LB_N_ERROR,
322   .error_strings = lb_error_strings,
323
324   .n_next_nodes = LB_N_NEXT,
325   .next_nodes =
326   {
327       [LB_NEXT_DROP] = "error-drop"
328   },
329 };
330
331 VLIB_REGISTER_NODE (lb4_gre4_node) =
332 {
333   .function = lb4_gre4_node_fn,
334   .name = "lb4-gre4",
335   .vector_size = sizeof (u32),
336   .format_trace = format_lb_trace,
337
338   .n_errors = LB_N_ERROR,
339   .error_strings = lb_error_strings,
340
341   .n_next_nodes = LB_N_NEXT,
342   .next_nodes =
343   {
344       [LB_NEXT_DROP] = "error-drop"
345   },
346 };
347