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