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 <vpp/app/version.h>
19 #include <vnet/api_errno.h>
21 //GC runs at most once every so many seconds
22 #define LB_GARBAGE_RUN 60
24 //After so many seconds. It is assumed that inter-core race condition will not occur.
25 #define LB_CONCURRENCY_TIMEOUT 10
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
32 static void lb_as_stack (lb_as_t *as);
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] =
39 [DPO_PROTO_IP4] = lb_dpo_gre4_ip4,
40 [DPO_PROTO_IP6] = lb_dpo_gre4_ip6,
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] =
47 [DPO_PROTO_IP4] = lb_dpo_gre6_ip4,
48 [DPO_PROTO_IP6] = lb_dpo_gre6_ip6,
51 u32 lb_hash_time_now(vlib_main_t * vm)
53 return (u32) (vlib_time_now(vm) + 10000);
56 u8 *format_lb_main (u8 * s, va_list * args)
58 vlib_thread_main_t *tm = vlib_get_thread_main();
59 lb_main_t *lbm = &lb_main;
60 s = format(s, "lb_main");
61 s = format(s, " ip4-src-address: %U \n", format_ip4_address, &lbm->ip4_src_address);
62 s = format(s, " ip6-src-address: %U \n", format_ip6_address, &lbm->ip6_src_address);
63 s = format(s, " #vips: %u\n", pool_elts(lbm->vips));
64 s = format(s, " #ass: %u\n", pool_elts(lbm->ass) - 1);
67 for(thread_index = 0; thread_index < tm->n_vlib_mains; thread_index++ ) {
68 lb_hash_t *h = lbm->per_cpu[thread_index].sticky_ht;
70 s = format(s, "core %d\n", thread_index);
71 s = format(s, " timeout: %ds\n", h->timeout);
72 s = format(s, " usage: %d / %d\n", lb_hash_elts(h, lb_hash_time_now(vlib_get_main())), lb_hash_size(h));
79 static char *lb_vip_type_strings[] = {
80 [LB_VIP_TYPE_IP6_GRE6] = "ip6-gre6",
81 [LB_VIP_TYPE_IP6_GRE4] = "ip6-gre4",
82 [LB_VIP_TYPE_IP4_GRE6] = "ip4-gre6",
83 [LB_VIP_TYPE_IP4_GRE4] = "ip4-gre4",
86 u8 *format_lb_vip_type (u8 * s, va_list * args)
88 lb_vip_type_t vipt = va_arg (*args, lb_vip_type_t);
90 for (i=0; i<LB_VIP_N_TYPES; i++)
92 return format(s, lb_vip_type_strings[i]);
93 return format(s, "_WRONG_TYPE_");
96 uword unformat_lb_vip_type (unformat_input_t * input, va_list * args)
98 lb_vip_type_t *vipt = va_arg (*args, lb_vip_type_t *);
100 for (i=0; i<LB_VIP_N_TYPES; i++)
101 if (unformat(input, lb_vip_type_strings[i])) {
108 u8 *format_lb_vip (u8 * s, va_list * args)
110 lb_vip_t *vip = va_arg (*args, lb_vip_t *);
111 return format(s, "%U %U new_size:%u #as:%u%s",
112 format_lb_vip_type, vip->type,
113 format_ip46_prefix, &vip->prefix, vip->plen, IP46_TYPE_ANY,
114 vip->new_flow_table_mask + 1,
115 pool_elts(vip->as_indexes),
116 (vip->flags & LB_VIP_FLAGS_USED)?"":" removed");
119 u8 *format_lb_as (u8 * s, va_list * args)
121 lb_as_t *as = va_arg (*args, lb_as_t *);
122 return format(s, "%U %s", format_ip46_address,
123 &as->address, IP46_TYPE_ANY,
124 (as->flags & LB_AS_FLAGS_USED)?"used":"removed");
127 u8 *format_lb_vip_detailed (u8 * s, va_list * args)
129 lb_main_t *lbm = &lb_main;
130 lb_vip_t *vip = va_arg (*args, lb_vip_t *);
131 u32 indent = format_get_indent (s);
133 s = format(s, "%U %U [%lu] %U%s\n"
135 format_white_space, indent,
136 format_lb_vip_type, vip->type,
138 format_ip46_prefix, &vip->prefix, (u32) vip->plen, IP46_TYPE_ANY,
139 (vip->flags & LB_VIP_FLAGS_USED)?"":" removed",
140 format_white_space, indent,
141 vip->new_flow_table_mask + 1);
144 s = format(s, "%U counters:\n",
145 format_white_space, indent);
147 for (i=0; i<LB_N_VIP_COUNTERS; i++)
148 s = format(s, "%U %s: %d\n",
149 format_white_space, indent,
150 lbm->vip_counters[i].name,
151 vlib_get_simple_counter(&lbm->vip_counters[i], vip - lbm->vips));
154 s = format(s, "%U #as:%u\n",
155 format_white_space, indent,
156 pool_elts(vip->as_indexes));
158 //Let's count the buckets for each AS
160 vec_validate(count, pool_len(lbm->ass)); //Possibly big alloc for not much...
161 lb_new_flow_entry_t *nfe;
162 vec_foreach(nfe, vip->new_flow_table)
163 count[nfe->as_index]++;
167 pool_foreach(as_index, vip->as_indexes, {
168 as = &lbm->ass[*as_index];
169 s = format(s, "%U %U %d buckets %d flows dpo:%u %s\n",
170 format_white_space, indent,
171 format_ip46_address, &as->address, IP46_TYPE_ANY,
172 count[as - lbm->ass],
173 vlib_refcount_get(&lbm->as_refcount, as - lbm->ass),
175 (as->flags & LB_AS_FLAGS_USED)?"used":" removed");
181 s = format(s, "%U new flows table:\n", format_white_space, indent);
182 lb_new_flow_entry_t *nfe;
183 vec_foreach(nfe, vip->new_flow_table) {
184 s = format(s, "%U %d: %d\n", format_white_space, indent, nfe - vip->new_flow_table, nfe->as_index);
196 static int lb_pseudorand_compare(void *a, void *b)
199 lb_main_t *lbm = &lb_main;
200 asa = &lbm->ass[((lb_pseudorand_t *)a)->as_index];
201 asb = &lbm->ass[((lb_pseudorand_t *)b)->as_index];
202 return memcmp(&asa->address, &asb->address, sizeof(asb->address));
205 static void lb_vip_garbage_collection(lb_vip_t *vip)
207 lb_main_t *lbm = &lb_main;
208 ASSERT (lbm->writer_lock[0]);
210 u32 now = (u32) vlib_time_now(vlib_get_main());
211 if (!clib_u32_loop_gt(now, vip->last_garbage_collection + LB_GARBAGE_RUN))
214 vip->last_garbage_collection = now;
217 pool_foreach(as_index, vip->as_indexes, {
218 as = &lbm->ass[*as_index];
219 if (!(as->flags & LB_AS_FLAGS_USED) && //Not used
220 clib_u32_loop_gt(now, as->last_used + LB_CONCURRENCY_TIMEOUT) && //Not recently used
221 (vlib_refcount_get(&lbm->as_refcount, as - lbm->ass) == 0))
223 fib_entry_child_remove(as->next_hop_fib_entry_index,
224 as->next_hop_child_index);
225 fib_table_entry_delete_index(as->next_hop_fib_entry_index,
227 as->next_hop_fib_entry_index = FIB_NODE_INDEX_INVALID;
229 pool_put(vip->as_indexes, as_index);
230 pool_put(lbm->ass, as);
235 void lb_garbage_collection()
237 lb_main_t *lbm = &lb_main;
238 lb_get_writer_lock();
240 u32 *to_be_removed_vips = 0, *i;
241 pool_foreach(vip, lbm->vips, {
242 lb_vip_garbage_collection(vip);
244 if (!(vip->flags & LB_VIP_FLAGS_USED) &&
245 (pool_elts(vip->as_indexes) == 0)) {
246 vec_add1(to_be_removed_vips, vip - lbm->vips);
250 vec_foreach(i, to_be_removed_vips) {
251 vip = &lbm->vips[*i];
252 pool_put(lbm->vips, vip);
253 pool_free(vip->as_indexes);
256 vec_free(to_be_removed_vips);
257 lb_put_writer_lock();
260 static void lb_vip_update_new_flow_table(lb_vip_t *vip)
262 lb_main_t *lbm = &lb_main;
263 lb_new_flow_entry_t *old_table;
265 lb_new_flow_entry_t *new_flow_table = 0;
267 lb_pseudorand_t *pr, *sort_arr = 0;
270 ASSERT (lbm->writer_lock[0]); //We must have the lock
272 //Check if some AS is configured or not
274 pool_foreach(as_index, vip->as_indexes, {
275 as = &lbm->ass[*as_index];
276 if (as->flags & LB_AS_FLAGS_USED) { //Not used anymore
278 goto out; //Not sure 'break' works in this macro-loop
284 //Only the default. i.e. no AS
285 vec_validate(new_flow_table, vip->new_flow_table_mask);
286 for (i=0; i<vec_len(new_flow_table); i++)
287 new_flow_table[i].as_index = 0;
292 //First, let's sort the ASs
294 vec_alloc(sort_arr, pool_elts(vip->as_indexes));
297 pool_foreach(as_index, vip->as_indexes, {
298 as = &lbm->ass[*as_index];
299 if (!(as->flags & LB_AS_FLAGS_USED)) //Not used anymore
302 sort_arr[i].as_index = as - lbm->ass;
305 _vec_len(sort_arr) = i;
307 vec_sort_with_function(sort_arr, lb_pseudorand_compare);
309 //Now let's pseudo-randomly generate permutations
310 vec_foreach(pr, sort_arr) {
311 lb_as_t *as = &lbm->ass[pr->as_index];
313 u64 seed = clib_xxhash(as->address.as_u64[0] ^
314 as->address.as_u64[1]);
315 /* We have 2^n buckets.
316 * skip must be prime with 2^n.
317 * So skip must be odd.
318 * MagLev actually state that M should be prime,
319 * but this has a big computation cost (% operation).
320 * Using 2^n is more better (& operation).
322 pr->skip = ((seed & 0xffffffff) | 1) & vip->new_flow_table_mask;
323 pr->last = (seed >> 32) & vip->new_flow_table_mask;
326 //Let's create a new flow table
327 vec_validate(new_flow_table, vip->new_flow_table_mask);
328 for (i=0; i<vec_len(new_flow_table); i++)
329 new_flow_table[i].as_index = ~0;
333 vec_foreach(pr, sort_arr) {
336 pr->last = (pr->last + pr->skip) & vip->new_flow_table_mask;
337 if (new_flow_table[last].as_index == ~0) {
338 new_flow_table[last].as_index = pr->as_index;
343 if (done == vec_len(new_flow_table))
352 //Count number of changed entries
354 for (i=0; i<vec_len(new_flow_table); i++)
355 if (vip->new_flow_table == 0 ||
356 new_flow_table[i].as_index != vip->new_flow_table[i].as_index)
359 old_table = vip->new_flow_table;
360 vip->new_flow_table = new_flow_table;
364 int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address,
365 u32 per_cpu_sticky_buckets, u32 flow_timeout)
367 lb_main_t *lbm = &lb_main;
369 if (!is_pow2(per_cpu_sticky_buckets))
370 return VNET_API_ERROR_INVALID_MEMORY_SIZE;
372 lb_get_writer_lock(); //Not exactly necessary but just a reminder that it exists for my future self
373 lbm->ip4_src_address = *ip4_address;
374 lbm->ip6_src_address = *ip6_address;
375 lbm->per_cpu_sticky_buckets = per_cpu_sticky_buckets;
376 lbm->flow_timeout = flow_timeout;
377 lb_put_writer_lock();
382 int lb_vip_find_index_with_lock(ip46_address_t *prefix, u8 plen, u32 *vip_index)
384 lb_main_t *lbm = &lb_main;
386 ASSERT (lbm->writer_lock[0]); //This must be called with the lock owned
387 ip46_prefix_normalize(prefix, plen);
388 pool_foreach(vip, lbm->vips, {
389 if ((vip->flags & LB_AS_FLAGS_USED) &&
391 vip->prefix.as_u64[0] == prefix->as_u64[0] &&
392 vip->prefix.as_u64[1] == prefix->as_u64[1]) {
393 *vip_index = vip - lbm->vips;
397 return VNET_API_ERROR_NO_SUCH_ENTRY;
400 int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index)
403 lb_get_writer_lock();
404 ret = lb_vip_find_index_with_lock(prefix, plen, vip_index);
405 lb_put_writer_lock();
409 static int lb_as_find_index_vip(lb_vip_t *vip, ip46_address_t *address, u32 *as_index)
411 lb_main_t *lbm = &lb_main;
412 ASSERT (lbm->writer_lock[0]); //This must be called with the lock owned
415 pool_foreach(asi, vip->as_indexes, {
416 as = &lbm->ass[*asi];
417 if (as->vip_index == (vip - lbm->vips) &&
418 as->address.as_u64[0] == address->as_u64[0] &&
419 as->address.as_u64[1] == address->as_u64[1]) {
420 *as_index = as - lbm->ass;
427 int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
429 lb_main_t *lbm = &lb_main;
430 lb_get_writer_lock();
432 if (!(vip = lb_vip_get_by_index(vip_index))) {
433 lb_put_writer_lock();
434 return VNET_API_ERROR_NO_SUCH_ENTRY;
437 ip46_type_t type = lb_vip_is_gre4(vip)?IP46_TYPE_IP4:IP46_TYPE_IP6;
438 u32 *to_be_added = 0;
439 u32 *to_be_updated = 0;
446 if (!lb_as_find_index_vip(vip, &addresses[n], &i)) {
447 if (lbm->ass[i].flags & LB_AS_FLAGS_USED) {
448 vec_free(to_be_added);
449 vec_free(to_be_updated);
450 lb_put_writer_lock();
451 return VNET_API_ERROR_VALUE_EXIST;
453 vec_add1(to_be_updated, i);
457 if (ip46_address_type(&addresses[n]) != type) {
458 vec_free(to_be_added);
459 vec_free(to_be_updated);
460 lb_put_writer_lock();
461 return VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
466 while(n2--) //Check for duplicates
467 if (addresses[n2].as_u64[0] == addresses[n].as_u64[0] &&
468 addresses[n2].as_u64[1] == addresses[n].as_u64[1])
472 vec_add1(to_be_added, n);
479 vec_foreach(ip, to_be_updated) {
480 lbm->ass[*ip].flags = LB_AS_FLAGS_USED;
482 vec_free(to_be_updated);
484 //Create those who have to be created
485 vec_foreach(ip, to_be_added) {
488 pool_get(lbm->ass, as);
489 as->address = addresses[*ip];
490 as->flags = LB_AS_FLAGS_USED;
491 as->vip_index = vip_index;
492 pool_get(vip->as_indexes, as_index);
493 *as_index = as - lbm->ass;
496 * become a child of the FIB entry
497 * so we are informed when its forwarding changes
499 fib_prefix_t nh = {};
500 if (lb_vip_is_gre4(vip)) {
501 nh.fp_addr.ip4 = as->address.ip4;
503 nh.fp_proto = FIB_PROTOCOL_IP4;
505 nh.fp_addr.ip6 = as->address.ip6;
507 nh.fp_proto = FIB_PROTOCOL_IP6;
510 as->next_hop_fib_entry_index =
511 fib_table_entry_special_add(0,
514 FIB_ENTRY_FLAG_NONE);
515 as->next_hop_child_index =
516 fib_entry_child_add(as->next_hop_fib_entry_index,
522 vec_free(to_be_added);
525 lb_vip_update_new_flow_table(vip);
527 //Garbage collection maybe
528 lb_vip_garbage_collection(vip);
530 lb_put_writer_lock();
534 int lb_vip_del_ass_withlock(u32 vip_index, ip46_address_t *addresses, u32 n)
536 lb_main_t *lbm = &lb_main;
537 u32 now = (u32) vlib_time_now(vlib_get_main());
541 if (!(vip = lb_vip_get_by_index(vip_index))) {
542 return VNET_API_ERROR_NO_SUCH_ENTRY;
548 if (lb_as_find_index_vip(vip, &addresses[n], &i)) {
550 return VNET_API_ERROR_NO_SUCH_ENTRY;
553 if (n) { //Check for duplicates
556 if (addresses[n2].as_u64[0] == addresses[n].as_u64[0] &&
557 addresses[n2].as_u64[1] == addresses[n].as_u64[1])
562 vec_add1(indexes, i);
567 //Garbage collection maybe
568 lb_vip_garbage_collection(vip);
570 if (indexes != NULL) {
571 vec_foreach(ip, indexes) {
572 lbm->ass[*ip].flags &= ~LB_AS_FLAGS_USED;
573 lbm->ass[*ip].last_used = now;
577 lb_vip_update_new_flow_table(vip);
584 int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
586 lb_get_writer_lock();
587 int ret = lb_vip_del_ass_withlock(vip_index, addresses, n);
588 lb_put_writer_lock();
593 * Add the VIP adjacency to the ip4 or ip6 fib
595 static void lb_vip_add_adjacency(lb_main_t *lbm, lb_vip_t *vip)
597 dpo_proto_t proto = 0;
598 dpo_id_t dpo = DPO_INVALID;
599 fib_prefix_t pfx = {};
600 if (lb_vip_is_ip4(vip)) {
601 pfx.fp_addr.ip4 = vip->prefix.ip4;
602 pfx.fp_len = vip->plen - 96;
603 pfx.fp_proto = FIB_PROTOCOL_IP4;
604 proto = DPO_PROTO_IP4;
606 pfx.fp_addr.ip6 = vip->prefix.ip6;
607 pfx.fp_len = vip->plen;
608 pfx.fp_proto = FIB_PROTOCOL_IP6;
609 proto = DPO_PROTO_IP6;
611 dpo_set(&dpo, lb_vip_is_gre4(vip)?lbm->dpo_gre4_type:lbm->dpo_gre6_type,
612 proto, vip - lbm->vips);
613 fib_table_entry_special_dpo_add(0,
615 FIB_SOURCE_PLUGIN_HI,
616 FIB_ENTRY_FLAG_EXCLUSIVE,
622 * Deletes the adjacency associated with the VIP
624 static void lb_vip_del_adjacency(lb_main_t *lbm, lb_vip_t *vip)
626 fib_prefix_t pfx = {};
627 if (lb_vip_is_ip4(vip)) {
628 pfx.fp_addr.ip4 = vip->prefix.ip4;
629 pfx.fp_len = vip->plen - 96;
630 pfx.fp_proto = FIB_PROTOCOL_IP4;
632 pfx.fp_addr.ip6 = vip->prefix.ip6;
633 pfx.fp_len = vip->plen;
634 pfx.fp_proto = FIB_PROTOCOL_IP6;
636 fib_table_entry_special_remove(0, &pfx, FIB_SOURCE_PLUGIN_HI);
639 int lb_vip_add(ip46_address_t *prefix, u8 plen, lb_vip_type_t type, u32 new_length, u32 *vip_index)
641 lb_main_t *lbm = &lb_main;
643 lb_get_writer_lock();
644 ip46_prefix_normalize(prefix, plen);
646 if (!lb_vip_find_index_with_lock(prefix, plen, vip_index)) {
647 lb_put_writer_lock();
648 return VNET_API_ERROR_VALUE_EXIST;
651 if (!is_pow2(new_length)) {
652 lb_put_writer_lock();
653 return VNET_API_ERROR_INVALID_MEMORY_SIZE;
656 if (ip46_prefix_is_ip4(prefix, plen) &&
657 (type != LB_VIP_TYPE_IP4_GRE4) &&
658 (type != LB_VIP_TYPE_IP4_GRE6))
659 return VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
663 pool_get(lbm->vips, vip);
666 vip->prefix = *prefix;
668 vip->last_garbage_collection = (u32) vlib_time_now(vlib_get_main());
670 vip->flags = LB_VIP_FLAGS_USED;
675 for (i = 0; i < LB_N_VIP_COUNTERS; i++) {
676 vlib_validate_simple_counter(&lbm->vip_counters[i], vip - lbm->vips);
677 vlib_zero_simple_counter(&lbm->vip_counters[i], vip - lbm->vips);
680 //Configure new flow table
681 vip->new_flow_table_mask = new_length - 1;
682 vip->new_flow_table = 0;
684 //Create a new flow hash table full of the default entry
685 lb_vip_update_new_flow_table(vip);
687 //Create adjacency to direct traffic
688 lb_vip_add_adjacency(lbm, vip);
691 *vip_index = vip - lbm->vips;
693 lb_put_writer_lock();
697 int lb_vip_del(u32 vip_index)
699 lb_main_t *lbm = &lb_main;
701 lb_get_writer_lock();
702 if (!(vip = lb_vip_get_by_index(vip_index))) {
703 lb_put_writer_lock();
704 return VNET_API_ERROR_NO_SUCH_ENTRY;
707 //FIXME: This operation is actually not working
708 //We will need to remove state before performing this.
712 ip46_address_t *ass = 0;
715 pool_foreach(as_index, vip->as_indexes, {
716 as = &lbm->ass[*as_index];
717 vec_add1(ass, as->address);
720 lb_vip_del_ass_withlock(vip_index, ass, vec_len(ass));
725 lb_vip_del_adjacency(lbm, vip);
727 //Set the VIP as unused
728 vip->flags &= ~LB_VIP_FLAGS_USED;
730 lb_put_writer_lock();
735 VLIB_PLUGIN_REGISTER () = {
736 .version = VPP_BUILD_VER,
737 .description = "Load Balancer",
741 u8 *format_lb_dpo (u8 * s, va_list * va)
743 index_t index = va_arg (*va, index_t);
744 CLIB_UNUSED(u32 indent) = va_arg (*va, u32);
745 lb_main_t *lbm = &lb_main;
746 lb_vip_t *vip = pool_elt_at_index (lbm->vips, index);
747 return format (s, "%U", format_lb_vip, vip);
750 static void lb_dpo_lock (dpo_id_t *dpo) {}
751 static void lb_dpo_unlock (dpo_id_t *dpo) {}
754 lb_fib_node_get_node (fib_node_index_t index)
756 lb_main_t *lbm = &lb_main;
757 lb_as_t *as = pool_elt_at_index (lbm->ass, index);
758 return (&as->fib_node);
762 lb_fib_node_last_lock_gone (fib_node_t *node)
767 lb_as_from_fib_node (fib_node_t *node)
769 return ((lb_as_t*)(((char*)node) -
770 STRUCT_OFFSET_OF(lb_as_t, fib_node)));
774 lb_as_stack (lb_as_t *as)
776 lb_main_t *lbm = &lb_main;
777 lb_vip_t *vip = &lbm->vips[as->vip_index];
778 dpo_stack(lb_vip_is_gre4(vip)?lbm->dpo_gre4_type:lbm->dpo_gre6_type,
779 lb_vip_is_ip4(vip)?DPO_PROTO_IP4:DPO_PROTO_IP6,
781 fib_entry_contribute_ip_forwarding(
782 as->next_hop_fib_entry_index));
785 static fib_node_back_walk_rc_t
786 lb_fib_node_back_walk_notify (fib_node_t *node,
787 fib_node_back_walk_ctx_t *ctx)
789 lb_as_stack(lb_as_from_fib_node(node));
790 return (FIB_NODE_BACK_WALK_CONTINUE);
794 lb_init (vlib_main_t * vm)
796 vlib_thread_main_t *tm = vlib_get_thread_main ();
797 lb_main_t *lbm = &lb_main;
799 fib_node_vft_t lb_fib_node_vft = {
800 .fnv_get = lb_fib_node_get_node,
801 .fnv_last_lock = lb_fib_node_last_lock_gone,
802 .fnv_back_walk = lb_fib_node_back_walk_notify,
805 .dv_lock = lb_dpo_lock,
806 .dv_unlock = lb_dpo_unlock,
807 .dv_format = format_lb_dpo,
812 vec_validate(lbm->per_cpu, tm->n_vlib_mains - 1);
813 lbm->writer_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
814 lbm->writer_lock[0] = 0;
815 lbm->per_cpu_sticky_buckets = LB_DEFAULT_PER_CPU_STICKY_BUCKETS;
816 lbm->flow_timeout = LB_DEFAULT_FLOW_TIMEOUT;
817 lbm->ip4_src_address.as_u32 = 0xffffffff;
818 lbm->ip6_src_address.as_u64[0] = 0xffffffffffffffffL;
819 lbm->ip6_src_address.as_u64[1] = 0xffffffffffffffffL;
820 lbm->dpo_gre4_type = dpo_register_new_type(&lb_vft, lb_dpo_gre4_nodes);
821 lbm->dpo_gre6_type = dpo_register_new_type(&lb_vft, lb_dpo_gre6_nodes);
822 lbm->fib_node_type = fib_node_register_new_type(&lb_fib_node_vft);
824 //Init AS reference counters
825 vlib_refcount_init(&lbm->as_refcount);
827 //Allocate and init default AS.
829 pool_get(lbm->ass, default_as);
830 default_as->flags = 0;
831 default_as->dpo.dpoi_next_node = LB_NEXT_DROP;
832 default_as->vip_index = ~0;
833 default_as->address.ip6.as_u64[0] = 0xffffffffffffffffL;
834 default_as->address.ip6.as_u64[1] = 0xffffffffffffffffL;
836 #define _(a,b,c) lbm->vip_counters[c].name = b;
837 lb_foreach_vip_counter
842 VLIB_INIT_FUNCTION (lb_init);