Add L3DSR feature in LB plugin
[vpp.git] / src / plugins / lb / lb.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 #include <vnet/plugin/plugin.h>
18 #include <vpp/app/version.h>
19 #include <vnet/api_errno.h>
20
21 //GC runs at most once every so many seconds
22 #define LB_GARBAGE_RUN 60
23
24 //After so many seconds. It is assumed that inter-core race condition will not occur.
25 #define LB_CONCURRENCY_TIMEOUT 10
26
27 lb_main_t lb_main;
28
29 #define lb_get_writer_lock() do {} while(__sync_lock_test_and_set (lb_main.writer_lock, 1))
30 #define lb_put_writer_lock() lb_main.writer_lock[0] = 0
31
32 static void lb_as_stack (lb_as_t *as);
33
34
35 const static char * const lb_dpo_gre4_ip4[] = { "lb4-gre4" , NULL };
36 const static char * const lb_dpo_gre4_ip6[] = { "lb6-gre4" , NULL };
37 const static char* const * const lb_dpo_gre4_nodes[DPO_PROTO_NUM] =
38     {
39         [DPO_PROTO_IP4]  = lb_dpo_gre4_ip4,
40         [DPO_PROTO_IP6]  = lb_dpo_gre4_ip6,
41     };
42
43 const static char * const lb_dpo_gre6_ip4[] = { "lb4-gre6" , NULL };
44 const static char * const lb_dpo_gre6_ip6[] = { "lb6-gre6" , NULL };
45 const static char* const * const lb_dpo_gre6_nodes[DPO_PROTO_NUM] =
46     {
47         [DPO_PROTO_IP4]  = lb_dpo_gre6_ip4,
48         [DPO_PROTO_IP6]  = lb_dpo_gre6_ip6,
49     };
50
51 const static char * const lb_dpo_l3dsr_ip4[] = { "lb4-l3dsr" , NULL };
52 const static char* const * const lb_dpo_l3dsr_nodes[DPO_PROTO_NUM] =
53     {
54         [DPO_PROTO_IP4]  = lb_dpo_l3dsr_ip4,
55     };
56
57 u32 lb_hash_time_now(vlib_main_t * vm)
58 {
59   return (u32) (vlib_time_now(vm) + 10000);
60 }
61
62 u8 *format_lb_main (u8 * s, va_list * args)
63 {
64   vlib_thread_main_t *tm = vlib_get_thread_main();
65   lb_main_t *lbm = &lb_main;
66   s = format(s, "lb_main");
67   s = format(s, " ip4-src-address: %U \n", format_ip4_address, &lbm->ip4_src_address);
68   s = format(s, " ip6-src-address: %U \n", format_ip6_address, &lbm->ip6_src_address);
69   s = format(s, " #vips: %u\n", pool_elts(lbm->vips));
70   s = format(s, " #ass: %u\n", pool_elts(lbm->ass) - 1);
71
72   u32 thread_index;
73   for(thread_index = 0; thread_index < tm->n_vlib_mains; thread_index++ ) {
74     lb_hash_t *h = lbm->per_cpu[thread_index].sticky_ht;
75     if (h) {
76       s = format(s, "core %d\n", thread_index);
77       s = format(s, "  timeout: %ds\n", h->timeout);
78       s = format(s, "  usage: %d / %d\n", lb_hash_elts(h, lb_hash_time_now(vlib_get_main())),  lb_hash_size(h));
79     }
80   }
81
82   return s;
83 }
84
85 static char *lb_vip_type_strings[] = {
86     [LB_VIP_TYPE_IP6_GRE6] = "ip6-gre6",
87     [LB_VIP_TYPE_IP6_GRE4] = "ip6-gre4",
88     [LB_VIP_TYPE_IP4_GRE6] = "ip4-gre6",
89     [LB_VIP_TYPE_IP4_GRE4] = "ip4-gre4",
90     [LB_VIP_TYPE_IP4_L3DSR] = "ip4-l3dsr",
91 };
92
93 u8 *format_lb_vip_type (u8 * s, va_list * args)
94 {
95   lb_vip_type_t vipt = va_arg (*args, lb_vip_type_t);
96   u32 i;
97   for (i=0; i<LB_VIP_N_TYPES; i++)
98     if (vipt == i)
99       return format(s, lb_vip_type_strings[i]);
100   return format(s, "_WRONG_TYPE_");
101 }
102
103 uword unformat_lb_vip_type (unformat_input_t * input, va_list * args)
104 {
105   lb_vip_type_t *vipt = va_arg (*args, lb_vip_type_t *);
106   u32 i;
107   for (i=0; i<LB_VIP_N_TYPES; i++)
108     if (unformat(input, lb_vip_type_strings[i])) {
109       *vipt = i;
110       return 1;
111     }
112   return 0;
113 }
114
115 u8 *format_lb_vip (u8 * s, va_list * args)
116 {
117   lb_vip_t *vip = va_arg (*args, lb_vip_t *);
118   return format(s, "%U %U new_size:%u #as:%u%s",
119              format_lb_vip_type, vip->type,
120              format_ip46_prefix, &vip->prefix, vip->plen, IP46_TYPE_ANY,
121              vip->new_flow_table_mask + 1,
122              pool_elts(vip->as_indexes),
123              (vip->flags & LB_VIP_FLAGS_USED)?"":" removed");
124 }
125
126 u8 *format_lb_as (u8 * s, va_list * args)
127 {
128   lb_as_t *as = va_arg (*args, lb_as_t *);
129   return format(s, "%U %s", format_ip46_address,
130                 &as->address, IP46_TYPE_ANY,
131                 (as->flags & LB_AS_FLAGS_USED)?"used":"removed");
132 }
133
134 u8 *format_lb_vip_detailed (u8 * s, va_list * args)
135 {
136   lb_main_t *lbm = &lb_main;
137   lb_vip_t *vip = va_arg (*args, lb_vip_t *);
138   u32 indent = format_get_indent (s);
139
140   s = format(s, "%U %U [%lu] %U%s\n"
141                    "%U  new_size:%u\n",
142                   format_white_space, indent,
143                   format_lb_vip_type, vip->type,
144                   vip - lbm->vips,
145                   format_ip46_prefix, &vip->prefix, (u32) vip->plen, IP46_TYPE_ANY,
146                   (vip->flags & LB_VIP_FLAGS_USED)?"":" removed",
147                   format_white_space, indent,
148                   vip->new_flow_table_mask + 1);
149
150   if (vip->type == LB_VIP_TYPE_IP4_L3DSR)
151     {
152       s = format(s, "%U  dscp:%u\n",
153                     format_white_space, indent,
154                     vip->dscp);
155     }
156
157   //Print counters
158   s = format(s, "%U  counters:\n",
159              format_white_space, indent);
160   u32 i;
161   for (i=0; i<LB_N_VIP_COUNTERS; i++)
162     s = format(s, "%U    %s: %d\n",
163                format_white_space, indent,
164                lbm->vip_counters[i].name,
165                vlib_get_simple_counter(&lbm->vip_counters[i], vip - lbm->vips));
166
167
168   s = format(s, "%U  #as:%u\n",
169              format_white_space, indent,
170              pool_elts(vip->as_indexes));
171
172   //Let's count the buckets for each AS
173   u32 *count = 0;
174   vec_validate(count, pool_len(lbm->ass)); //Possibly big alloc for not much...
175   lb_new_flow_entry_t *nfe;
176   vec_foreach(nfe, vip->new_flow_table)
177     count[nfe->as_index]++;
178
179   lb_as_t *as;
180   u32 *as_index;
181   pool_foreach(as_index, vip->as_indexes, {
182       as = &lbm->ass[*as_index];
183       s = format(s, "%U    %U %d buckets   %d flows  dpo:%u %s\n",
184                    format_white_space, indent,
185                    format_ip46_address, &as->address, IP46_TYPE_ANY,
186                    count[as - lbm->ass],
187                    vlib_refcount_get(&lbm->as_refcount, as - lbm->ass),
188                    as->dpo.dpoi_index,
189                    (as->flags & LB_AS_FLAGS_USED)?"used":" removed");
190   });
191
192   vec_free(count);
193
194   /*
195   s = format(s, "%U  new flows table:\n", format_white_space, indent);
196   lb_new_flow_entry_t *nfe;
197   vec_foreach(nfe, vip->new_flow_table) {
198     s = format(s, "%U    %d: %d\n", format_white_space, indent, nfe - vip->new_flow_table, nfe->as_index);
199   }
200   */
201   return s;
202 }
203
204 typedef struct {
205   u32 as_index;
206   u32 last;
207   u32 skip;
208 } lb_pseudorand_t;
209
210 static int lb_pseudorand_compare(void *a, void *b)
211 {
212   lb_as_t *asa, *asb;
213   lb_main_t *lbm = &lb_main;
214   asa = &lbm->ass[((lb_pseudorand_t *)a)->as_index];
215   asb = &lbm->ass[((lb_pseudorand_t *)b)->as_index];
216   return memcmp(&asa->address, &asb->address, sizeof(asb->address));
217 }
218
219 static void lb_vip_garbage_collection(lb_vip_t *vip)
220 {
221   lb_main_t *lbm = &lb_main;
222   ASSERT (lbm->writer_lock[0]);
223
224   u32 now = (u32) vlib_time_now(vlib_get_main());
225   if (!clib_u32_loop_gt(now, vip->last_garbage_collection + LB_GARBAGE_RUN))
226     return;
227
228   vip->last_garbage_collection = now;
229   lb_as_t *as;
230   u32 *as_index;
231   pool_foreach(as_index, vip->as_indexes, {
232       as = &lbm->ass[*as_index];
233       if (!(as->flags & LB_AS_FLAGS_USED) && //Not used
234           clib_u32_loop_gt(now, as->last_used + LB_CONCURRENCY_TIMEOUT) && //Not recently used
235           (vlib_refcount_get(&lbm->as_refcount, as - lbm->ass) == 0))
236         { //Not referenced
237           fib_entry_child_remove(as->next_hop_fib_entry_index,
238                                  as->next_hop_child_index);
239           fib_table_entry_delete_index(as->next_hop_fib_entry_index,
240                                        FIB_SOURCE_RR);
241           as->next_hop_fib_entry_index = FIB_NODE_INDEX_INVALID;
242
243           pool_put(vip->as_indexes, as_index);
244           pool_put(lbm->ass, as);
245         }
246   });
247 }
248
249 void lb_garbage_collection()
250 {
251   lb_main_t *lbm = &lb_main;
252   lb_get_writer_lock();
253   lb_vip_t *vip;
254   u32 *to_be_removed_vips = 0, *i;
255   pool_foreach(vip, lbm->vips, {
256       lb_vip_garbage_collection(vip);
257
258       if (!(vip->flags & LB_VIP_FLAGS_USED) &&
259           (pool_elts(vip->as_indexes) == 0)) {
260         vec_add1(to_be_removed_vips, vip - lbm->vips);
261       }
262   });
263
264   vec_foreach(i, to_be_removed_vips) {
265     vip = &lbm->vips[*i];
266     pool_put(lbm->vips, vip);
267     pool_free(vip->as_indexes);
268   }
269
270   vec_free(to_be_removed_vips);
271   lb_put_writer_lock();
272 }
273
274 static void lb_vip_update_new_flow_table(lb_vip_t *vip)
275 {
276   lb_main_t *lbm = &lb_main;
277   lb_new_flow_entry_t *old_table;
278   u32 i, *as_index;
279   lb_new_flow_entry_t *new_flow_table = 0;
280   lb_as_t *as;
281   lb_pseudorand_t *pr, *sort_arr = 0;
282   u32 count;
283
284   ASSERT (lbm->writer_lock[0]); //We must have the lock
285
286   //Check if some AS is configured or not
287   i = 0;
288   pool_foreach(as_index, vip->as_indexes, {
289       as = &lbm->ass[*as_index];
290       if (as->flags & LB_AS_FLAGS_USED) { //Not used anymore
291         i = 1;
292         goto out; //Not sure 'break' works in this macro-loop
293       }
294   });
295
296 out:
297   if (i == 0) {
298     //Only the default. i.e. no AS
299     vec_validate(new_flow_table, vip->new_flow_table_mask);
300     for (i=0; i<vec_len(new_flow_table); i++)
301       new_flow_table[i].as_index = 0;
302
303     goto finished;
304   }
305
306   //First, let's sort the ASs
307   sort_arr = 0;
308   vec_alloc(sort_arr, pool_elts(vip->as_indexes));
309
310   i = 0;
311   pool_foreach(as_index, vip->as_indexes, {
312       as = &lbm->ass[*as_index];
313       if (!(as->flags & LB_AS_FLAGS_USED)) //Not used anymore
314         continue;
315
316       sort_arr[i].as_index = as - lbm->ass;
317       i++;
318   });
319   _vec_len(sort_arr) = i;
320
321   vec_sort_with_function(sort_arr, lb_pseudorand_compare);
322
323   //Now let's pseudo-randomly generate permutations
324   vec_foreach(pr, sort_arr) {
325     lb_as_t *as = &lbm->ass[pr->as_index];
326
327     u64 seed = clib_xxhash(as->address.as_u64[0] ^
328                            as->address.as_u64[1]);
329     /* We have 2^n buckets.
330      * skip must be prime with 2^n.
331      * So skip must be odd.
332      * MagLev actually state that M should be prime,
333      * but this has a big computation cost (% operation).
334      * Using 2^n is more better (& operation).
335      */
336     pr->skip = ((seed & 0xffffffff) | 1) & vip->new_flow_table_mask;
337     pr->last = (seed >> 32) & vip->new_flow_table_mask;
338   }
339
340   //Let's create a new flow table
341   vec_validate(new_flow_table, vip->new_flow_table_mask);
342   for (i=0; i<vec_len(new_flow_table); i++)
343     new_flow_table[i].as_index = ~0;
344
345   u32 done = 0;
346   while (1) {
347     vec_foreach(pr, sort_arr) {
348       while (1) {
349         u32 last = pr->last;
350         pr->last = (pr->last + pr->skip) & vip->new_flow_table_mask;
351         if (new_flow_table[last].as_index == ~0) {
352           new_flow_table[last].as_index = pr->as_index;
353           break;
354         }
355       }
356       done++;
357       if (done == vec_len(new_flow_table))
358         goto finished;
359     }
360   }
361
362   vec_free(sort_arr);
363
364 finished:
365
366 //Count number of changed entries
367   count = 0;
368   for (i=0; i<vec_len(new_flow_table); i++)
369     if (vip->new_flow_table == 0 ||
370         new_flow_table[i].as_index != vip->new_flow_table[i].as_index)
371       count++;
372
373   old_table = vip->new_flow_table;
374   vip->new_flow_table = new_flow_table;
375   vec_free(old_table);
376 }
377
378 int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address,
379            u32 per_cpu_sticky_buckets, u32 flow_timeout)
380 {
381   lb_main_t *lbm = &lb_main;
382
383   if (!is_pow2(per_cpu_sticky_buckets))
384     return VNET_API_ERROR_INVALID_MEMORY_SIZE;
385
386   lb_get_writer_lock(); //Not exactly necessary but just a reminder that it exists for my future self
387   lbm->ip4_src_address = *ip4_address;
388   lbm->ip6_src_address = *ip6_address;
389   lbm->per_cpu_sticky_buckets = per_cpu_sticky_buckets;
390   lbm->flow_timeout = flow_timeout;
391   lb_put_writer_lock();
392   return 0;
393 }
394
395 static
396 int lb_vip_find_index_with_lock(ip46_address_t *prefix, u8 plen, u32 *vip_index)
397 {
398   lb_main_t *lbm = &lb_main;
399   lb_vip_t *vip;
400   ASSERT (lbm->writer_lock[0]); //This must be called with the lock owned
401   ip46_prefix_normalize(prefix, plen);
402   pool_foreach(vip, lbm->vips, {
403       if ((vip->flags & LB_AS_FLAGS_USED) &&
404           vip->plen == plen &&
405           vip->prefix.as_u64[0] == prefix->as_u64[0] &&
406           vip->prefix.as_u64[1] == prefix->as_u64[1]) {
407         *vip_index = vip - lbm->vips;
408         return 0;
409       }
410   });
411   return VNET_API_ERROR_NO_SUCH_ENTRY;
412 }
413
414 int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index)
415 {
416   int ret;
417   lb_get_writer_lock();
418   ret = lb_vip_find_index_with_lock(prefix, plen, vip_index);
419   lb_put_writer_lock();
420   return ret;
421 }
422
423 static int lb_as_find_index_vip(lb_vip_t *vip, ip46_address_t *address, u32 *as_index)
424 {
425   lb_main_t *lbm = &lb_main;
426   ASSERT (lbm->writer_lock[0]); //This must be called with the lock owned
427   lb_as_t *as;
428   u32 *asi;
429   pool_foreach(asi, vip->as_indexes, {
430       as = &lbm->ass[*asi];
431       if (as->vip_index == (vip - lbm->vips) &&
432           as->address.as_u64[0] == address->as_u64[0] &&
433           as->address.as_u64[1] == address->as_u64[1]) {
434         *as_index = as - lbm->ass;
435         return 0;
436       }
437   });
438   return -1;
439 }
440
441 int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
442 {
443   lb_main_t *lbm = &lb_main;
444   lb_get_writer_lock();
445   lb_vip_t *vip;
446   if (!(vip = lb_vip_get_by_index(vip_index))) {
447     lb_put_writer_lock();
448     return VNET_API_ERROR_NO_SUCH_ENTRY;
449   }
450
451   ip46_type_t type = lb_encap_is_ip4(vip)?IP46_TYPE_IP4:IP46_TYPE_IP6;
452   u32 *to_be_added = 0;
453   u32 *to_be_updated = 0;
454   u32 i;
455   u32 *ip;
456
457   //Sanity check
458   while (n--) {
459
460     if (!lb_as_find_index_vip(vip, &addresses[n], &i)) {
461       if (lbm->ass[i].flags & LB_AS_FLAGS_USED) {
462         vec_free(to_be_added);
463         vec_free(to_be_updated);
464         lb_put_writer_lock();
465         return VNET_API_ERROR_VALUE_EXIST;
466       }
467       vec_add1(to_be_updated, i);
468       goto next;
469     }
470
471     if (ip46_address_type(&addresses[n]) != type) {
472       vec_free(to_be_added);
473       vec_free(to_be_updated);
474       lb_put_writer_lock();
475       return VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
476     }
477
478     if (n) {
479       u32 n2 = n;
480       while(n2--) //Check for duplicates
481         if (addresses[n2].as_u64[0] == addresses[n].as_u64[0] &&
482             addresses[n2].as_u64[1] == addresses[n].as_u64[1])
483           goto next;
484     }
485
486     vec_add1(to_be_added, n);
487
488 next:
489     continue;
490   }
491
492   //Update reused ASs
493   vec_foreach(ip, to_be_updated) {
494     lbm->ass[*ip].flags = LB_AS_FLAGS_USED;
495   }
496   vec_free(to_be_updated);
497
498   //Create those who have to be created
499   vec_foreach(ip, to_be_added) {
500     lb_as_t *as;
501     u32 *as_index;
502     pool_get(lbm->ass, as);
503     as->address = addresses[*ip];
504     as->flags = LB_AS_FLAGS_USED;
505     as->vip_index = vip_index;
506     pool_get(vip->as_indexes, as_index);
507     *as_index = as - lbm->ass;
508
509     /*
510      * become a child of the FIB entry
511      * so we are informed when its forwarding changes
512      */
513     fib_prefix_t nh = {};
514     if (lb_encap_is_ip4(vip)) {
515         nh.fp_addr.ip4 = as->address.ip4;
516         nh.fp_len = 32;
517         nh.fp_proto = FIB_PROTOCOL_IP4;
518     } else {
519         nh.fp_addr.ip6 = as->address.ip6;
520         nh.fp_len = 128;
521         nh.fp_proto = FIB_PROTOCOL_IP6;
522     }
523
524     as->next_hop_fib_entry_index =
525         fib_table_entry_special_add(0,
526                                     &nh,
527                                     FIB_SOURCE_RR,
528                                     FIB_ENTRY_FLAG_NONE);
529     as->next_hop_child_index =
530         fib_entry_child_add(as->next_hop_fib_entry_index,
531                             lbm->fib_node_type,
532                             as - lbm->ass);
533
534     lb_as_stack(as);
535   }
536   vec_free(to_be_added);
537
538   //Recompute flows
539   lb_vip_update_new_flow_table(vip);
540
541   //Garbage collection maybe
542   lb_vip_garbage_collection(vip);
543
544   lb_put_writer_lock();
545   return 0;
546 }
547
548 int lb_vip_del_ass_withlock(u32 vip_index, ip46_address_t *addresses, u32 n)
549 {
550   lb_main_t *lbm = &lb_main;
551   u32 now = (u32) vlib_time_now(vlib_get_main());
552   u32 *ip = 0;
553
554   lb_vip_t *vip;
555   if (!(vip = lb_vip_get_by_index(vip_index))) {
556     return VNET_API_ERROR_NO_SUCH_ENTRY;
557   }
558
559   u32 *indexes = NULL;
560   while (n--) {
561     u32 i;
562     if (lb_as_find_index_vip(vip, &addresses[n], &i)) {
563       vec_free(indexes);
564       return VNET_API_ERROR_NO_SUCH_ENTRY;
565     }
566
567     if (n) { //Check for duplicates
568       u32 n2 = n - 1;
569       while(n2--) {
570         if (addresses[n2].as_u64[0] == addresses[n].as_u64[0] &&
571             addresses[n2].as_u64[1] == addresses[n].as_u64[1])
572           goto next;
573       }
574     }
575
576     vec_add1(indexes, i);
577 next:
578   continue;
579   }
580
581   //Garbage collection maybe
582   lb_vip_garbage_collection(vip);
583
584   if (indexes != NULL) {
585     vec_foreach(ip, indexes) {
586       lbm->ass[*ip].flags &= ~LB_AS_FLAGS_USED;
587       lbm->ass[*ip].last_used = now;
588     }
589
590     //Recompute flows
591     lb_vip_update_new_flow_table(vip);
592   }
593
594   vec_free(indexes);
595   return 0;
596 }
597
598 int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
599 {
600   lb_get_writer_lock();
601   int ret = lb_vip_del_ass_withlock(vip_index, addresses, n);
602   lb_put_writer_lock();
603   return ret;
604 }
605
606 /**
607  * Add the VIP adjacency to the ip4 or ip6 fib
608  */
609 static void lb_vip_add_adjacency(lb_main_t *lbm, lb_vip_t *vip)
610 {
611   dpo_proto_t proto = 0;
612   dpo_type_t dpo_type = 0;
613
614   dpo_id_t dpo = DPO_INVALID;
615   fib_prefix_t pfx = {};
616   if (lb_vip_is_ip4(vip)) {
617       pfx.fp_addr.ip4 = vip->prefix.ip4;
618       pfx.fp_len = vip->plen - 96;
619       pfx.fp_proto = FIB_PROTOCOL_IP4;
620       proto = DPO_PROTO_IP4;
621   } else {
622       pfx.fp_addr.ip6 = vip->prefix.ip6;
623       pfx.fp_len = vip->plen;
624       pfx.fp_proto = FIB_PROTOCOL_IP6;
625       proto = DPO_PROTO_IP6;
626   }
627
628   if(lb_vip_is_gre4(vip))
629     dpo_type = lbm->dpo_gre4_type;
630   else if (lb_vip_is_gre6(vip))
631     dpo_type = lbm->dpo_gre6_type;
632   else if (lb_vip_is_l3dsr(vip))
633     dpo_type = lbm->dpo_l3dsr_type;
634
635   dpo_set(&dpo, dpo_type, proto, vip - lbm->vips);
636   fib_table_entry_special_dpo_add(0,
637                                   &pfx,
638                                   FIB_SOURCE_PLUGIN_HI,
639                                   FIB_ENTRY_FLAG_EXCLUSIVE,
640                                   &dpo);
641   dpo_reset(&dpo);
642 }
643
644 /**
645  * Deletes the adjacency associated with the VIP
646  */
647 static void lb_vip_del_adjacency(lb_main_t *lbm, lb_vip_t *vip)
648 {
649   fib_prefix_t pfx = {};
650   if (lb_vip_is_ip4(vip)) {
651       pfx.fp_addr.ip4 = vip->prefix.ip4;
652       pfx.fp_len = vip->plen - 96;
653       pfx.fp_proto = FIB_PROTOCOL_IP4;
654   } else {
655       pfx.fp_addr.ip6 = vip->prefix.ip6;
656       pfx.fp_len = vip->plen;
657       pfx.fp_proto = FIB_PROTOCOL_IP6;
658   }
659   fib_table_entry_special_remove(0, &pfx, FIB_SOURCE_PLUGIN_HI);
660 }
661
662 int lb_vip_add(ip46_address_t *prefix, u8 plen, lb_vip_type_t type, u8 dscp,
663                u32 new_length, u32 *vip_index)
664 {
665   lb_main_t *lbm = &lb_main;
666   lb_vip_t *vip;
667
668   lb_get_writer_lock();
669   ip46_prefix_normalize(prefix, plen);
670
671   if (!lb_vip_find_index_with_lock(prefix, plen, vip_index)) {
672     lb_put_writer_lock();
673     return VNET_API_ERROR_VALUE_EXIST;
674   }
675
676   if (!is_pow2(new_length)) {
677     lb_put_writer_lock();
678     return VNET_API_ERROR_INVALID_MEMORY_SIZE;
679   }
680
681   if (ip46_prefix_is_ip4(prefix, plen) &&
682       (type != LB_VIP_TYPE_IP4_GRE4) &&
683       (type != LB_VIP_TYPE_IP4_GRE6) &&
684       (type != LB_VIP_TYPE_IP4_L3DSR))
685     return VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
686
687   if ((!ip46_prefix_is_ip4(prefix, plen)) &&
688       (type != LB_VIP_TYPE_IP6_GRE4) &&
689       (type != LB_VIP_TYPE_IP6_GRE6))
690     return VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
691
692   if ((type == LB_VIP_TYPE_IP4_L3DSR) && (dscp >= 64 ) )
693     {
694       return VNET_API_ERROR_VALUE_EXIST;
695     }
696
697   //Allocate
698   pool_get(lbm->vips, vip);
699
700   //Init
701   vip->prefix = *prefix;
702   vip->plen = plen;
703   vip->last_garbage_collection = (u32) vlib_time_now(vlib_get_main());
704   vip->type = type;
705   vip->dscp = dscp;
706   vip->flags = LB_VIP_FLAGS_USED;
707   vip->as_indexes = 0;
708
709   //Validate counters
710   u32 i;
711   for (i = 0; i < LB_N_VIP_COUNTERS; i++) {
712     vlib_validate_simple_counter(&lbm->vip_counters[i], vip - lbm->vips);
713     vlib_zero_simple_counter(&lbm->vip_counters[i], vip - lbm->vips);
714   }
715
716   //Configure new flow table
717   vip->new_flow_table_mask = new_length - 1;
718   vip->new_flow_table = 0;
719
720   //Create a new flow hash table full of the default entry
721   lb_vip_update_new_flow_table(vip);
722
723   //Create adjacency to direct traffic
724   lb_vip_add_adjacency(lbm, vip);
725
726   //Return result
727   *vip_index = vip - lbm->vips;
728
729   lb_put_writer_lock();
730   return 0;
731 }
732
733 int lb_vip_del(u32 vip_index)
734 {
735   lb_main_t *lbm = &lb_main;
736   lb_vip_t *vip;
737   lb_get_writer_lock();
738   if (!(vip = lb_vip_get_by_index(vip_index))) {
739     lb_put_writer_lock();
740     return VNET_API_ERROR_NO_SUCH_ENTRY;
741   }
742
743   //FIXME: This operation is actually not working
744   //We will need to remove state before performing this.
745
746   {
747     //Remove all ASs
748     ip46_address_t *ass = 0;
749     lb_as_t *as;
750     u32 *as_index;
751     pool_foreach(as_index, vip->as_indexes, {
752         as = &lbm->ass[*as_index];
753         vec_add1(ass, as->address);
754     });
755     if (vec_len(ass))
756       lb_vip_del_ass_withlock(vip_index, ass, vec_len(ass));
757     vec_free(ass);
758   }
759
760   //Delete adjacency
761   lb_vip_del_adjacency(lbm, vip);
762
763   //Set the VIP as unused
764   vip->flags &= ~LB_VIP_FLAGS_USED;
765
766   lb_put_writer_lock();
767   return 0;
768 }
769
770 /* *INDENT-OFF* */
771 VLIB_PLUGIN_REGISTER () = {
772     .version = VPP_BUILD_VER,
773     .description = "Load Balancer",
774 };
775 /* *INDENT-ON* */
776
777 u8 *format_lb_dpo (u8 * s, va_list * va)
778 {
779   index_t index = va_arg (*va, index_t);
780   CLIB_UNUSED(u32 indent) = va_arg (*va, u32);
781   lb_main_t *lbm = &lb_main;
782   lb_vip_t *vip = pool_elt_at_index (lbm->vips, index);
783   return format (s, "%U", format_lb_vip, vip);
784 }
785
786 static void lb_dpo_lock (dpo_id_t *dpo) {}
787 static void lb_dpo_unlock (dpo_id_t *dpo) {}
788
789 static fib_node_t *
790 lb_fib_node_get_node (fib_node_index_t index)
791 {
792   lb_main_t *lbm = &lb_main;
793   lb_as_t *as = pool_elt_at_index (lbm->ass, index);
794   return (&as->fib_node);
795 }
796
797 static void
798 lb_fib_node_last_lock_gone (fib_node_t *node)
799 {
800 }
801
802 static lb_as_t *
803 lb_as_from_fib_node (fib_node_t *node)
804 {
805   return ((lb_as_t*)(((char*)node) -
806       STRUCT_OFFSET_OF(lb_as_t, fib_node)));
807 }
808
809 static void
810 lb_as_stack (lb_as_t *as)
811 {
812   lb_main_t *lbm = &lb_main;
813   lb_vip_t *vip = &lbm->vips[as->vip_index];
814   dpo_type_t dpo_type = 0;
815
816   if(lb_vip_is_gre4(vip))
817     dpo_type = lbm->dpo_gre4_type;
818   else if (lb_vip_is_gre6(vip))
819     dpo_type = lbm->dpo_gre6_type;
820   else if (lb_vip_is_l3dsr(vip))
821     dpo_type = lbm->dpo_l3dsr_type;
822
823   dpo_stack(dpo_type,
824             lb_vip_is_ip4(vip)?DPO_PROTO_IP4:DPO_PROTO_IP6,
825             &as->dpo,
826             fib_entry_contribute_ip_forwarding(
827                 as->next_hop_fib_entry_index));
828 }
829
830 static fib_node_back_walk_rc_t
831 lb_fib_node_back_walk_notify (fib_node_t *node,
832                                fib_node_back_walk_ctx_t *ctx)
833 {
834     lb_as_stack(lb_as_from_fib_node(node));
835     return (FIB_NODE_BACK_WALK_CONTINUE);
836 }
837
838 clib_error_t *
839 lb_init (vlib_main_t * vm)
840 {
841   vlib_thread_main_t *tm = vlib_get_thread_main ();
842   lb_main_t *lbm = &lb_main;
843   lb_as_t *default_as;
844   fib_node_vft_t lb_fib_node_vft = {
845       .fnv_get = lb_fib_node_get_node,
846       .fnv_last_lock = lb_fib_node_last_lock_gone,
847       .fnv_back_walk = lb_fib_node_back_walk_notify,
848   };
849   dpo_vft_t lb_vft = {
850       .dv_lock = lb_dpo_lock,
851       .dv_unlock = lb_dpo_unlock,
852       .dv_format = format_lb_dpo,
853   };
854
855   lbm->vips = 0;
856   lbm->per_cpu = 0;
857   vec_validate(lbm->per_cpu, tm->n_vlib_mains - 1);
858   lbm->writer_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,  CLIB_CACHE_LINE_BYTES);
859   lbm->writer_lock[0] = 0;
860   lbm->per_cpu_sticky_buckets = LB_DEFAULT_PER_CPU_STICKY_BUCKETS;
861   lbm->flow_timeout = LB_DEFAULT_FLOW_TIMEOUT;
862   lbm->ip4_src_address.as_u32 = 0xffffffff;
863   lbm->ip6_src_address.as_u64[0] = 0xffffffffffffffffL;
864   lbm->ip6_src_address.as_u64[1] = 0xffffffffffffffffL;
865   lbm->dpo_gre4_type = dpo_register_new_type(&lb_vft, lb_dpo_gre4_nodes);
866   lbm->dpo_gre6_type = dpo_register_new_type(&lb_vft, lb_dpo_gre6_nodes);
867   lbm->dpo_l3dsr_type = dpo_register_new_type(&lb_vft, lb_dpo_l3dsr_nodes);
868   lbm->fib_node_type = fib_node_register_new_type(&lb_fib_node_vft);
869
870   //Init AS reference counters
871   vlib_refcount_init(&lbm->as_refcount);
872
873   //Allocate and init default AS.
874   lbm->ass = 0;
875   pool_get(lbm->ass, default_as);
876   default_as->flags = 0;
877   default_as->dpo.dpoi_next_node = LB_NEXT_DROP;
878   default_as->vip_index = ~0;
879   default_as->address.ip6.as_u64[0] = 0xffffffffffffffffL;
880   default_as->address.ip6.as_u64[1] = 0xffffffffffffffffL;
881
882 #define _(a,b,c) lbm->vip_counters[c].name = b;
883   lb_foreach_vip_counter
884 #undef _
885   return NULL;
886 }
887
888 VLIB_INIT_FUNCTION (lb_init);