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 #include <vnet/plugin/plugin.h>
18 #include <vnet/api_errno.h>
20 //GC runs at most once every so many seconds
21 #define LB_GARBAGE_RUN 60
23 //After so many seconds. It is assumed that inter-core race condition will not occur.
24 #define LB_CONCURRENCY_TIMEOUT 10
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
31 static void lb_as_stack (lb_as_t *as);
34 const static char * const lb_dpo_gre4_ip4[] = { "lb4-gre4" , NULL };
35 const static char * const lb_dpo_gre4_ip6[] = { "lb6-gre4" , NULL };
36 const static char* const * const lb_dpo_gre4_nodes[DPO_PROTO_NUM] =
38 [DPO_PROTO_IP4] = lb_dpo_gre4_ip4,
39 [DPO_PROTO_IP6] = lb_dpo_gre4_ip6,
42 const static char * const lb_dpo_gre6_ip4[] = { "lb4-gre6" , NULL };
43 const static char * const lb_dpo_gre6_ip6[] = { "lb6-gre6" , NULL };
44 const static char* const * const lb_dpo_gre6_nodes[DPO_PROTO_NUM] =
46 [DPO_PROTO_IP4] = lb_dpo_gre6_ip4,
47 [DPO_PROTO_IP6] = lb_dpo_gre6_ip6,
50 u32 lb_hash_time_now(vlib_main_t * vm)
52 return (u32) (vlib_time_now(vm) + 10000);
55 u8 *format_lb_main (u8 * s, va_list * args)
57 vlib_thread_main_t *tm = vlib_get_thread_main();
58 lb_main_t *lbm = &lb_main;
59 s = format(s, "lb_main");
60 s = format(s, " ip4-src-address: %U \n", format_ip4_address, &lbm->ip4_src_address);
61 s = format(s, " ip6-src-address: %U \n", format_ip6_address, &lbm->ip6_src_address);
62 s = format(s, " #vips: %u\n", pool_elts(lbm->vips));
63 s = format(s, " #ass: %u\n", pool_elts(lbm->ass) - 1);
66 for(cpu_index = 0; cpu_index < tm->n_vlib_mains; cpu_index++ ) {
67 lb_hash_t *h = lbm->per_cpu[cpu_index].sticky_ht;
69 s = format(s, "core %d\n", cpu_index);
70 s = format(s, " timeout: %ds\n", h->timeout);
71 s = format(s, " usage: %d / %d\n", lb_hash_elts(h, lb_hash_time_now(vlib_get_main())), lb_hash_size(h));
78 static char *lb_vip_type_strings[] = {
79 [LB_VIP_TYPE_IP6_GRE6] = "ip6-gre6",
80 [LB_VIP_TYPE_IP6_GRE4] = "ip6-gre4",
81 [LB_VIP_TYPE_IP4_GRE6] = "ip4-gre6",
82 [LB_VIP_TYPE_IP4_GRE4] = "ip4-gre4",
85 u8 *format_lb_vip_type (u8 * s, va_list * args)
87 lb_vip_type_t vipt = va_arg (*args, lb_vip_type_t);
89 for (i=0; i<LB_VIP_N_TYPES; i++)
91 return format(s, lb_vip_type_strings[i]);
92 return format(s, "_WRONG_TYPE_");
95 uword unformat_lb_vip_type (unformat_input_t * input, va_list * args)
97 lb_vip_type_t *vipt = va_arg (*args, lb_vip_type_t *);
99 for (i=0; i<LB_VIP_N_TYPES; i++)
100 if (unformat(input, lb_vip_type_strings[i])) {
107 u8 *format_lb_vip (u8 * s, va_list * args)
109 lb_vip_t *vip = va_arg (*args, lb_vip_t *);
110 return format(s, "%U %U new_size:%u #as:%u%s",
111 format_lb_vip_type, vip->type,
112 format_ip46_prefix, &vip->prefix, vip->plen, IP46_TYPE_ANY,
113 vip->new_flow_table_mask + 1,
114 pool_elts(vip->as_indexes),
115 (vip->flags & LB_VIP_FLAGS_USED)?"":" removed");
118 u8 *format_lb_as (u8 * s, va_list * args)
120 lb_as_t *as = va_arg (*args, lb_as_t *);
121 return format(s, "%U %s", format_ip46_address,
122 &as->address, IP46_TYPE_ANY,
123 (as->flags & LB_AS_FLAGS_USED)?"used":"removed");
126 u8 *format_lb_vip_detailed (u8 * s, va_list * args)
128 lb_main_t *lbm = &lb_main;
129 lb_vip_t *vip = va_arg (*args, lb_vip_t *);
130 uword indent = format_get_indent (s);
132 s = format(s, "%U %U [%u] %U%s\n"
134 format_white_space, indent,
135 format_lb_vip_type, vip->type,
136 vip - lbm->vips, format_ip46_prefix, &vip->prefix, vip->plen, IP46_TYPE_ANY,
137 (vip->flags & LB_VIP_FLAGS_USED)?"":" removed",
138 format_white_space, indent,
139 vip->new_flow_table_mask + 1);
142 s = format(s, "%U counters:\n",
143 format_white_space, indent);
145 for (i=0; i<LB_N_VIP_COUNTERS; i++)
146 s = format(s, "%U %s: %d\n",
147 format_white_space, indent,
148 lbm->vip_counters[i].name,
149 vlib_get_simple_counter(&lbm->vip_counters[i], vip - lbm->vips));
152 s = format(s, "%U #as:%u\n",
153 format_white_space, indent,
154 pool_elts(vip->as_indexes));
156 //Let's count the buckets for each AS
158 vec_validate(count, pool_len(lbm->ass)); //Possibly big alloc for not much...
159 lb_new_flow_entry_t *nfe;
160 vec_foreach(nfe, vip->new_flow_table)
161 count[nfe->as_index]++;
165 pool_foreach(as_index, vip->as_indexes, {
166 as = &lbm->ass[*as_index];
167 s = format(s, "%U %U %d buckets %d flows dpo:%u %s\n",
168 format_white_space, indent,
169 format_ip46_address, &as->address, IP46_TYPE_ANY,
170 count[as - lbm->ass],
171 vlib_refcount_get(&lbm->as_refcount, as - lbm->ass),
173 (as->flags & LB_AS_FLAGS_USED)?"used":" removed");
179 s = format(s, "%U new flows table:\n", format_white_space, indent);
180 lb_new_flow_entry_t *nfe;
181 vec_foreach(nfe, vip->new_flow_table) {
182 s = format(s, "%U %d: %d\n", format_white_space, indent, nfe - vip->new_flow_table, nfe->as_index);
194 static int lb_pseudorand_compare(void *a, void *b)
197 lb_main_t *lbm = &lb_main;
198 asa = &lbm->ass[((lb_pseudorand_t *)a)->as_index];
199 asb = &lbm->ass[((lb_pseudorand_t *)b)->as_index];
200 return memcmp(&asa->address, &asb->address, sizeof(asb->address));
203 static void lb_vip_garbage_collection(lb_vip_t *vip)
205 lb_main_t *lbm = &lb_main;
206 ASSERT (lbm->writer_lock[0]);
208 u32 now = (u32) vlib_time_now(vlib_get_main());
209 if (!clib_u32_loop_gt(now, vip->last_garbage_collection + LB_GARBAGE_RUN))
212 vip->last_garbage_collection = now;
215 pool_foreach(as_index, vip->as_indexes, {
216 as = &lbm->ass[*as_index];
217 if (!(as->flags & LB_AS_FLAGS_USED) && //Not used
218 clib_u32_loop_gt(now, as->last_used + LB_CONCURRENCY_TIMEOUT) && //Not recently used
219 (vlib_refcount_get(&lbm->as_refcount, as - lbm->ass) == 0))
221 fib_entry_child_remove(as->next_hop_fib_entry_index,
222 as->next_hop_child_index);
223 fib_table_entry_delete_index(as->next_hop_fib_entry_index,
225 as->next_hop_fib_entry_index = FIB_NODE_INDEX_INVALID;
227 pool_put(vip->as_indexes, as_index);
228 pool_put(lbm->ass, as);
233 void lb_garbage_collection()
235 lb_main_t *lbm = &lb_main;
236 lb_get_writer_lock();
238 u32 *to_be_removed_vips = 0, *i;
239 pool_foreach(vip, lbm->vips, {
240 lb_vip_garbage_collection(vip);
242 if (!(vip->flags & LB_VIP_FLAGS_USED) &&
243 (pool_elts(vip->as_indexes) == 0)) {
244 vec_add1(to_be_removed_vips, vip - lbm->vips);
248 vec_foreach(i, to_be_removed_vips) {
249 vip = &lbm->vips[*i];
250 pool_put(lbm->vips, vip);
251 pool_free(vip->as_indexes);
254 vec_free(to_be_removed_vips);
255 lb_put_writer_lock();
258 static void lb_vip_update_new_flow_table(lb_vip_t *vip)
260 lb_main_t *lbm = &lb_main;
261 lb_new_flow_entry_t *old_table;
263 lb_new_flow_entry_t *new_flow_table = 0;
265 lb_pseudorand_t *pr, *sort_arr = 0;
268 ASSERT (lbm->writer_lock[0]); //We must have the lock
270 //Check if some AS is configured or not
272 pool_foreach(as_index, vip->as_indexes, {
273 as = &lbm->ass[*as_index];
274 if (as->flags & LB_AS_FLAGS_USED) { //Not used anymore
276 goto out; //Not sure 'break' works in this macro-loop
282 //Only the default. i.e. no AS
283 vec_validate(new_flow_table, vip->new_flow_table_mask);
284 for (i=0; i<vec_len(new_flow_table); i++)
285 new_flow_table[i].as_index = 0;
290 //First, let's sort the ASs
292 vec_alloc(sort_arr, pool_elts(vip->as_indexes));
295 pool_foreach(as_index, vip->as_indexes, {
296 as = &lbm->ass[*as_index];
297 if (!(as->flags & LB_AS_FLAGS_USED)) //Not used anymore
300 sort_arr[i].as_index = as - lbm->ass;
303 _vec_len(sort_arr) = i;
305 vec_sort_with_function(sort_arr, lb_pseudorand_compare);
307 //Now let's pseudo-randomly generate permutations
308 vec_foreach(pr, sort_arr) {
309 lb_as_t *as = &lbm->ass[pr->as_index];
311 u64 seed = clib_xxhash(as->address.as_u64[0] ^
312 as->address.as_u64[1]);
313 /* We have 2^n buckets.
314 * skip must be prime with 2^n.
315 * So skip must be odd.
316 * MagLev actually state that M should be prime,
317 * but this has a big computation cost (% operation).
318 * Using 2^n is more better (& operation).
320 pr->skip = ((seed & 0xffffffff) | 1) & vip->new_flow_table_mask;
321 pr->last = (seed >> 32) & vip->new_flow_table_mask;
324 //Let's create a new flow table
325 vec_validate(new_flow_table, vip->new_flow_table_mask);
326 for (i=0; i<vec_len(new_flow_table); i++)
327 new_flow_table[i].as_index = ~0;
331 vec_foreach(pr, sort_arr) {
334 pr->last = (pr->last + pr->skip) & vip->new_flow_table_mask;
335 if (new_flow_table[last].as_index == ~0) {
336 new_flow_table[last].as_index = pr->as_index;
341 if (done == vec_len(new_flow_table))
350 //Count number of changed entries
352 for (i=0; i<vec_len(new_flow_table); i++)
353 if (vip->new_flow_table == 0 ||
354 new_flow_table[i].as_index != vip->new_flow_table[i].as_index)
357 old_table = vip->new_flow_table;
358 vip->new_flow_table = new_flow_table;
362 int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address,
363 u32 per_cpu_sticky_buckets, u32 flow_timeout)
365 lb_main_t *lbm = &lb_main;
367 if (!is_pow2(per_cpu_sticky_buckets))
368 return VNET_API_ERROR_INVALID_MEMORY_SIZE;
370 lb_get_writer_lock(); //Not exactly necessary but just a reminder that it exists for my future self
371 lbm->ip4_src_address = *ip4_address;
372 lbm->ip6_src_address = *ip6_address;
373 lbm->per_cpu_sticky_buckets = per_cpu_sticky_buckets;
374 lbm->flow_timeout = flow_timeout;
375 lb_put_writer_lock();
380 int lb_vip_find_index_with_lock(ip46_address_t *prefix, u8 plen, u32 *vip_index)
382 lb_main_t *lbm = &lb_main;
384 ASSERT (lbm->writer_lock[0]); //This must be called with the lock owned
385 ip46_prefix_normalize(prefix, plen);
386 pool_foreach(vip, lbm->vips, {
387 if ((vip->flags & LB_AS_FLAGS_USED) &&
389 vip->prefix.as_u64[0] == prefix->as_u64[0] &&
390 vip->prefix.as_u64[1] == prefix->as_u64[1]) {
391 *vip_index = vip - lbm->vips;
395 return VNET_API_ERROR_NO_SUCH_ENTRY;
398 int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index)
401 lb_get_writer_lock();
402 ret = lb_vip_find_index_with_lock(prefix, plen, vip_index);
403 lb_put_writer_lock();
407 static int lb_as_find_index_vip(lb_vip_t *vip, ip46_address_t *address, u32 *as_index)
409 lb_main_t *lbm = &lb_main;
410 ASSERT (lbm->writer_lock[0]); //This must be called with the lock owned
413 pool_foreach(asi, vip->as_indexes, {
414 as = &lbm->ass[*asi];
415 if (as->vip_index == (vip - lbm->vips) &&
416 as->address.as_u64[0] == address->as_u64[0] &&
417 as->address.as_u64[1] == address->as_u64[1]) {
418 *as_index = as - lbm->ass;
425 int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
427 lb_main_t *lbm = &lb_main;
428 lb_get_writer_lock();
430 if (!(vip = lb_vip_get_by_index(vip_index))) {
431 lb_put_writer_lock();
432 return VNET_API_ERROR_NO_SUCH_ENTRY;
435 ip46_type_t type = lb_vip_is_gre4(vip)?IP46_TYPE_IP4:IP46_TYPE_IP6;
436 u32 *to_be_added = 0;
437 u32 *to_be_updated = 0;
444 if (!lb_as_find_index_vip(vip, &addresses[n], &i)) {
445 if (lbm->ass[i].flags & LB_AS_FLAGS_USED) {
446 vec_free(to_be_added);
447 vec_free(to_be_updated);
448 lb_put_writer_lock();
449 return VNET_API_ERROR_VALUE_EXIST;
451 vec_add1(to_be_updated, i);
455 if (ip46_address_type(&addresses[n]) != type) {
456 vec_free(to_be_added);
457 vec_free(to_be_updated);
458 lb_put_writer_lock();
459 return VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
464 while(n2--) //Check for duplicates
465 if (addresses[n2].as_u64[0] == addresses[n].as_u64[0] &&
466 addresses[n2].as_u64[1] == addresses[n].as_u64[1])
470 vec_add1(to_be_added, n);
477 vec_foreach(ip, to_be_updated) {
478 lbm->ass[*ip].flags = LB_AS_FLAGS_USED;
480 vec_free(to_be_updated);
482 //Create those who have to be created
483 vec_foreach(ip, to_be_added) {
486 pool_get(lbm->ass, as);
487 as->address = addresses[*ip];
488 as->flags = LB_AS_FLAGS_USED;
489 as->vip_index = vip_index;
490 pool_get(vip->as_indexes, as_index);
491 *as_index = as - lbm->ass;
494 * become a child of the FIB entry
495 * so we are informed when its forwarding changes
497 fib_prefix_t nh = {};
498 if (lb_vip_is_gre4(vip)) {
499 nh.fp_addr.ip4 = as->address.ip4;
501 nh.fp_proto = FIB_PROTOCOL_IP4;
503 nh.fp_addr.ip6 = as->address.ip6;
505 nh.fp_proto = FIB_PROTOCOL_IP6;
508 as->next_hop_fib_entry_index =
509 fib_table_entry_special_add(0,
514 as->next_hop_child_index =
515 fib_entry_child_add(as->next_hop_fib_entry_index,
521 vec_free(to_be_added);
524 lb_vip_update_new_flow_table(vip);
526 //Garbage collection maybe
527 lb_vip_garbage_collection(vip);
529 lb_put_writer_lock();
533 int lb_vip_del_ass_withlock(u32 vip_index, ip46_address_t *addresses, u32 n)
535 lb_main_t *lbm = &lb_main;
536 u32 now = (u32) vlib_time_now(vlib_get_main());
540 if (!(vip = lb_vip_get_by_index(vip_index))) {
541 return VNET_API_ERROR_NO_SUCH_ENTRY;
547 if (lb_as_find_index_vip(vip, &addresses[n], &i)) {
549 return VNET_API_ERROR_NO_SUCH_ENTRY;
552 if (n) { //Check for duplicates
555 if (addresses[n2].as_u64[0] == addresses[n].as_u64[0] &&
556 addresses[n2].as_u64[1] == addresses[n].as_u64[1])
561 vec_add1(indexes, i);
566 //Garbage collection maybe
567 lb_vip_garbage_collection(vip);
569 if (indexes != NULL) {
570 vec_foreach(ip, indexes) {
571 lbm->ass[*ip].flags &= ~LB_AS_FLAGS_USED;
572 lbm->ass[*ip].last_used = now;
576 lb_vip_update_new_flow_table(vip);
583 int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
585 lb_get_writer_lock();
586 int ret = lb_vip_del_ass_withlock(vip_index, addresses, n);
587 lb_put_writer_lock();
592 * Add the VIP adjacency to the ip4 or ip6 fib
594 static void lb_vip_add_adjacency(lb_main_t *lbm, lb_vip_t *vip)
596 dpo_proto_t proto = 0;
597 dpo_id_t dpo = DPO_INVALID;
598 fib_prefix_t pfx = {};
599 if (lb_vip_is_ip4(vip)) {
600 pfx.fp_addr.ip4 = vip->prefix.ip4;
601 pfx.fp_len = vip->plen - 96;
602 pfx.fp_proto = FIB_PROTOCOL_IP4;
603 proto = DPO_PROTO_IP4;
605 pfx.fp_addr.ip6 = vip->prefix.ip6;
606 pfx.fp_len = vip->plen;
607 pfx.fp_proto = FIB_PROTOCOL_IP6;
608 proto = DPO_PROTO_IP6;
610 dpo_set(&dpo, lb_vip_is_gre4(vip)?lbm->dpo_gre4_type:lbm->dpo_gre6_type,
611 proto, vip - lbm->vips);
612 fib_table_entry_special_dpo_add(0,
614 FIB_SOURCE_PLUGIN_HI,
615 FIB_ENTRY_FLAG_EXCLUSIVE,
621 * Deletes the adjacency associated with the VIP
623 static void lb_vip_del_adjacency(lb_main_t *lbm, lb_vip_t *vip)
625 fib_prefix_t pfx = {};
626 if (lb_vip_is_ip4(vip)) {
627 pfx.fp_addr.ip4 = vip->prefix.ip4;
628 pfx.fp_len = vip->plen - 96;
629 pfx.fp_proto = FIB_PROTOCOL_IP4;
631 pfx.fp_addr.ip6 = vip->prefix.ip6;
632 pfx.fp_len = vip->plen;
633 pfx.fp_proto = FIB_PROTOCOL_IP6;
635 fib_table_entry_special_remove(0, &pfx, FIB_SOURCE_PLUGIN_HI);
638 int lb_vip_add(ip46_address_t *prefix, u8 plen, lb_vip_type_t type, u32 new_length, u32 *vip_index)
640 lb_main_t *lbm = &lb_main;
642 lb_get_writer_lock();
643 ip46_prefix_normalize(prefix, plen);
645 if (!lb_vip_find_index_with_lock(prefix, plen, vip_index)) {
646 lb_put_writer_lock();
647 return VNET_API_ERROR_VALUE_EXIST;
650 if (!is_pow2(new_length)) {
651 lb_put_writer_lock();
652 return VNET_API_ERROR_INVALID_MEMORY_SIZE;
655 if (ip46_prefix_is_ip4(prefix, plen) &&
656 (type != LB_VIP_TYPE_IP4_GRE4) &&
657 (type != LB_VIP_TYPE_IP4_GRE6))
658 return VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
662 pool_get(lbm->vips, vip);
665 vip->prefix = *prefix;
667 vip->last_garbage_collection = (u32) vlib_time_now(vlib_get_main());
669 vip->flags = LB_VIP_FLAGS_USED;
674 for (i = 0; i < LB_N_VIP_COUNTERS; i++) {
675 vlib_validate_simple_counter(&lbm->vip_counters[i], vip - lbm->vips);
676 vlib_zero_simple_counter(&lbm->vip_counters[i], vip - lbm->vips);
679 //Configure new flow table
680 vip->new_flow_table_mask = new_length - 1;
681 vip->new_flow_table = 0;
683 //Create a new flow hash table full of the default entry
684 lb_vip_update_new_flow_table(vip);
686 //Create adjacency to direct traffic
687 lb_vip_add_adjacency(lbm, vip);
690 *vip_index = vip - lbm->vips;
692 lb_put_writer_lock();
696 int lb_vip_del(u32 vip_index)
698 lb_main_t *lbm = &lb_main;
700 lb_get_writer_lock();
701 if (!(vip = lb_vip_get_by_index(vip_index))) {
702 lb_put_writer_lock();
703 return VNET_API_ERROR_NO_SUCH_ENTRY;
706 //FIXME: This operation is actually not working
707 //We will need to remove state before performing this.
711 ip46_address_t *ass = 0;
714 pool_foreach(as_index, vip->as_indexes, {
715 as = &lbm->ass[*as_index];
716 vec_add1(ass, as->address);
719 lb_vip_del_ass_withlock(vip_index, ass, vec_len(ass));
724 lb_vip_del_adjacency(lbm, vip);
726 //Set the VIP as unused
727 vip->flags &= ~LB_VIP_FLAGS_USED;
729 lb_put_writer_lock();
734 vlib_plugin_register (vlib_main_t * vm,
735 vnet_plugin_handoff_t * h,
738 clib_error_t *error = 0;
743 u8 *format_lb_dpo (u8 * s, va_list * va)
745 index_t index = va_arg (*va, index_t);
746 CLIB_UNUSED(u32 indent) = va_arg (*va, u32);
747 lb_main_t *lbm = &lb_main;
748 lb_vip_t *vip = pool_elt_at_index (lbm->vips, index);
749 return format (s, "%U", format_lb_vip, vip);
752 static void lb_dpo_lock (dpo_id_t *dpo) {}
753 static void lb_dpo_unlock (dpo_id_t *dpo) {}
756 lb_fib_node_get_node (fib_node_index_t index)
758 lb_main_t *lbm = &lb_main;
759 lb_as_t *as = pool_elt_at_index (lbm->ass, index);
760 return (&as->fib_node);
764 lb_fib_node_last_lock_gone (fib_node_t *node)
769 lb_as_from_fib_node (fib_node_t *node)
771 return ((lb_as_t*)(((char*)node) -
772 STRUCT_OFFSET_OF(lb_as_t, fib_node)));
776 lb_as_stack (lb_as_t *as)
778 lb_main_t *lbm = &lb_main;
779 lb_vip_t *vip = &lbm->vips[as->vip_index];
780 dpo_stack(lb_vip_is_gre4(vip)?lbm->dpo_gre4_type:lbm->dpo_gre6_type,
781 lb_vip_is_ip4(vip)?DPO_PROTO_IP4:DPO_PROTO_IP6,
783 fib_entry_contribute_ip_forwarding(
784 as->next_hop_fib_entry_index));
787 static fib_node_back_walk_rc_t
788 lb_fib_node_back_walk_notify (fib_node_t *node,
789 fib_node_back_walk_ctx_t *ctx)
791 lb_as_stack(lb_as_from_fib_node(node));
792 return (FIB_NODE_BACK_WALK_CONTINUE);
796 lb_init (vlib_main_t * vm)
798 vlib_thread_main_t *tm = vlib_get_thread_main ();
799 lb_main_t *lbm = &lb_main;
801 fib_node_vft_t lb_fib_node_vft = {
802 .fnv_get = lb_fib_node_get_node,
803 .fnv_last_lock = lb_fib_node_last_lock_gone,
804 .fnv_back_walk = lb_fib_node_back_walk_notify,
807 .dv_lock = lb_dpo_lock,
808 .dv_unlock = lb_dpo_unlock,
809 .dv_format = format_lb_dpo,
814 vec_validate(lbm->per_cpu, tm->n_vlib_mains - 1);
815 lbm->writer_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
816 lbm->writer_lock[0] = 0;
817 lbm->per_cpu_sticky_buckets = LB_DEFAULT_PER_CPU_STICKY_BUCKETS;
818 lbm->flow_timeout = LB_DEFAULT_FLOW_TIMEOUT;
819 lbm->ip4_src_address.as_u32 = 0xffffffff;
820 lbm->ip6_src_address.as_u64[0] = 0xffffffffffffffffL;
821 lbm->ip6_src_address.as_u64[1] = 0xffffffffffffffffL;
822 lbm->dpo_gre4_type = dpo_register_new_type(&lb_vft, lb_dpo_gre4_nodes);
823 lbm->dpo_gre6_type = dpo_register_new_type(&lb_vft, lb_dpo_gre6_nodes);
824 lbm->fib_node_type = fib_node_register_new_type(&lb_fib_node_vft);
826 //Init AS reference counters
827 vlib_refcount_init(&lbm->as_refcount);
829 //Allocate and init default AS.
831 pool_get(lbm->ass, default_as);
832 default_as->flags = 0;
833 default_as->dpo.dpoi_next_node = LB_NEXT_DROP;
834 default_as->vip_index = ~0;
835 default_as->address.ip6.as_u64[0] = 0xffffffffffffffffL;
836 default_as->address.ip6.as_u64[1] = 0xffffffffffffffffL;
838 #define _(a,b,c) lbm->vip_counters[c].name = b;
839 lb_foreach_vip_counter
844 VLIB_INIT_FUNCTION (lb_init);