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 const static char * const lb_dpo_l3dsr_ip4[] = { "lb4-l3dsr" , NULL };
52 const static char* const * const lb_dpo_l3dsr_nodes[DPO_PROTO_NUM] =
54 [DPO_PROTO_IP4] = lb_dpo_l3dsr_ip4,
57 u32 lb_hash_time_now(vlib_main_t * vm)
59 return (u32) (vlib_time_now(vm) + 10000);
62 u8 *format_lb_main (u8 * s, va_list * args)
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);
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;
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));
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",
93 u8 *format_lb_vip_type (u8 * s, va_list * args)
95 lb_vip_type_t vipt = va_arg (*args, lb_vip_type_t);
97 for (i=0; i<LB_VIP_N_TYPES; i++)
99 return format(s, lb_vip_type_strings[i]);
100 return format(s, "_WRONG_TYPE_");
103 uword unformat_lb_vip_type (unformat_input_t * input, va_list * args)
105 lb_vip_type_t *vipt = va_arg (*args, lb_vip_type_t *);
107 for (i=0; i<LB_VIP_N_TYPES; i++)
108 if (unformat(input, lb_vip_type_strings[i])) {
115 u8 *format_lb_vip (u8 * s, va_list * args)
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");
126 u8 *format_lb_as (u8 * s, va_list * args)
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");
134 u8 *format_lb_vip_detailed (u8 * s, va_list * args)
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);
140 s = format(s, "%U %U [%lu] %U%s\n"
142 format_white_space, indent,
143 format_lb_vip_type, vip->type,
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);
150 if (vip->type == LB_VIP_TYPE_IP4_L3DSR)
152 s = format(s, "%U dscp:%u\n",
153 format_white_space, indent,
158 s = format(s, "%U counters:\n",
159 format_white_space, indent);
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));
168 s = format(s, "%U #as:%u\n",
169 format_white_space, indent,
170 pool_elts(vip->as_indexes));
172 //Let's count the buckets for each AS
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]++;
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),
189 (as->flags & LB_AS_FLAGS_USED)?"used":" removed");
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);
210 static int lb_pseudorand_compare(void *a, void *b)
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));
219 static void lb_vip_garbage_collection(lb_vip_t *vip)
221 lb_main_t *lbm = &lb_main;
222 ASSERT (lbm->writer_lock[0]);
224 u32 now = (u32) vlib_time_now(vlib_get_main());
225 if (!clib_u32_loop_gt(now, vip->last_garbage_collection + LB_GARBAGE_RUN))
228 vip->last_garbage_collection = now;
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))
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,
241 as->next_hop_fib_entry_index = FIB_NODE_INDEX_INVALID;
243 pool_put(vip->as_indexes, as_index);
244 pool_put(lbm->ass, as);
249 void lb_garbage_collection()
251 lb_main_t *lbm = &lb_main;
252 lb_get_writer_lock();
254 u32 *to_be_removed_vips = 0, *i;
255 pool_foreach(vip, lbm->vips, {
256 lb_vip_garbage_collection(vip);
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);
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);
270 vec_free(to_be_removed_vips);
271 lb_put_writer_lock();
274 static void lb_vip_update_new_flow_table(lb_vip_t *vip)
276 lb_main_t *lbm = &lb_main;
277 lb_new_flow_entry_t *old_table;
279 lb_new_flow_entry_t *new_flow_table = 0;
281 lb_pseudorand_t *pr, *sort_arr = 0;
284 ASSERT (lbm->writer_lock[0]); //We must have the lock
286 //Check if some AS is configured or not
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
292 goto out; //Not sure 'break' works in this macro-loop
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;
306 //First, let's sort the ASs
308 vec_alloc(sort_arr, pool_elts(vip->as_indexes));
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
316 sort_arr[i].as_index = as - lbm->ass;
319 _vec_len(sort_arr) = i;
321 vec_sort_with_function(sort_arr, lb_pseudorand_compare);
323 //Now let's pseudo-randomly generate permutations
324 vec_foreach(pr, sort_arr) {
325 lb_as_t *as = &lbm->ass[pr->as_index];
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).
336 pr->skip = ((seed & 0xffffffff) | 1) & vip->new_flow_table_mask;
337 pr->last = (seed >> 32) & vip->new_flow_table_mask;
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;
347 vec_foreach(pr, sort_arr) {
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;
357 if (done == vec_len(new_flow_table))
366 //Count number of changed entries
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)
373 old_table = vip->new_flow_table;
374 vip->new_flow_table = new_flow_table;
378 int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address,
379 u32 per_cpu_sticky_buckets, u32 flow_timeout)
381 lb_main_t *lbm = &lb_main;
383 if (!is_pow2(per_cpu_sticky_buckets))
384 return VNET_API_ERROR_INVALID_MEMORY_SIZE;
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();
396 int lb_vip_find_index_with_lock(ip46_address_t *prefix, u8 plen, u32 *vip_index)
398 lb_main_t *lbm = &lb_main;
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) &&
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;
411 return VNET_API_ERROR_NO_SUCH_ENTRY;
414 int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index)
417 lb_get_writer_lock();
418 ret = lb_vip_find_index_with_lock(prefix, plen, vip_index);
419 lb_put_writer_lock();
423 static int lb_as_find_index_vip(lb_vip_t *vip, ip46_address_t *address, u32 *as_index)
425 lb_main_t *lbm = &lb_main;
426 ASSERT (lbm->writer_lock[0]); //This must be called with the lock owned
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;
441 int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
443 lb_main_t *lbm = &lb_main;
444 lb_get_writer_lock();
446 if (!(vip = lb_vip_get_by_index(vip_index))) {
447 lb_put_writer_lock();
448 return VNET_API_ERROR_NO_SUCH_ENTRY;
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;
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;
467 vec_add1(to_be_updated, i);
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;
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])
486 vec_add1(to_be_added, n);
493 vec_foreach(ip, to_be_updated) {
494 lbm->ass[*ip].flags = LB_AS_FLAGS_USED;
496 vec_free(to_be_updated);
498 //Create those who have to be created
499 vec_foreach(ip, to_be_added) {
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;
510 * become a child of the FIB entry
511 * so we are informed when its forwarding changes
513 fib_prefix_t nh = {};
514 if (lb_encap_is_ip4(vip)) {
515 nh.fp_addr.ip4 = as->address.ip4;
517 nh.fp_proto = FIB_PROTOCOL_IP4;
519 nh.fp_addr.ip6 = as->address.ip6;
521 nh.fp_proto = FIB_PROTOCOL_IP6;
524 as->next_hop_fib_entry_index =
525 fib_table_entry_special_add(0,
528 FIB_ENTRY_FLAG_NONE);
529 as->next_hop_child_index =
530 fib_entry_child_add(as->next_hop_fib_entry_index,
536 vec_free(to_be_added);
539 lb_vip_update_new_flow_table(vip);
541 //Garbage collection maybe
542 lb_vip_garbage_collection(vip);
544 lb_put_writer_lock();
548 int lb_vip_del_ass_withlock(u32 vip_index, ip46_address_t *addresses, u32 n)
550 lb_main_t *lbm = &lb_main;
551 u32 now = (u32) vlib_time_now(vlib_get_main());
555 if (!(vip = lb_vip_get_by_index(vip_index))) {
556 return VNET_API_ERROR_NO_SUCH_ENTRY;
562 if (lb_as_find_index_vip(vip, &addresses[n], &i)) {
564 return VNET_API_ERROR_NO_SUCH_ENTRY;
567 if (n) { //Check for duplicates
570 if (addresses[n2].as_u64[0] == addresses[n].as_u64[0] &&
571 addresses[n2].as_u64[1] == addresses[n].as_u64[1])
576 vec_add1(indexes, i);
581 //Garbage collection maybe
582 lb_vip_garbage_collection(vip);
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;
591 lb_vip_update_new_flow_table(vip);
598 int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
600 lb_get_writer_lock();
601 int ret = lb_vip_del_ass_withlock(vip_index, addresses, n);
602 lb_put_writer_lock();
607 * Add the VIP adjacency to the ip4 or ip6 fib
609 static void lb_vip_add_adjacency(lb_main_t *lbm, lb_vip_t *vip)
611 dpo_proto_t proto = 0;
612 dpo_type_t dpo_type = 0;
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;
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;
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;
635 dpo_set(&dpo, dpo_type, proto, vip - lbm->vips);
636 fib_table_entry_special_dpo_add(0,
638 FIB_SOURCE_PLUGIN_HI,
639 FIB_ENTRY_FLAG_EXCLUSIVE,
645 * Deletes the adjacency associated with the VIP
647 static void lb_vip_del_adjacency(lb_main_t *lbm, lb_vip_t *vip)
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;
655 pfx.fp_addr.ip6 = vip->prefix.ip6;
656 pfx.fp_len = vip->plen;
657 pfx.fp_proto = FIB_PROTOCOL_IP6;
659 fib_table_entry_special_remove(0, &pfx, FIB_SOURCE_PLUGIN_HI);
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)
665 lb_main_t *lbm = &lb_main;
668 lb_get_writer_lock();
669 ip46_prefix_normalize(prefix, plen);
671 if (!lb_vip_find_index_with_lock(prefix, plen, vip_index)) {
672 lb_put_writer_lock();
673 return VNET_API_ERROR_VALUE_EXIST;
676 if (!is_pow2(new_length)) {
677 lb_put_writer_lock();
678 return VNET_API_ERROR_INVALID_MEMORY_SIZE;
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;
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;
692 if ((type == LB_VIP_TYPE_IP4_L3DSR) && (dscp >= 64 ) )
694 return VNET_API_ERROR_VALUE_EXIST;
698 pool_get(lbm->vips, vip);
701 vip->prefix = *prefix;
703 vip->last_garbage_collection = (u32) vlib_time_now(vlib_get_main());
706 vip->flags = LB_VIP_FLAGS_USED;
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);
716 //Configure new flow table
717 vip->new_flow_table_mask = new_length - 1;
718 vip->new_flow_table = 0;
720 //Create a new flow hash table full of the default entry
721 lb_vip_update_new_flow_table(vip);
723 //Create adjacency to direct traffic
724 lb_vip_add_adjacency(lbm, vip);
727 *vip_index = vip - lbm->vips;
729 lb_put_writer_lock();
733 int lb_vip_del(u32 vip_index)
735 lb_main_t *lbm = &lb_main;
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;
743 //FIXME: This operation is actually not working
744 //We will need to remove state before performing this.
748 ip46_address_t *ass = 0;
751 pool_foreach(as_index, vip->as_indexes, {
752 as = &lbm->ass[*as_index];
753 vec_add1(ass, as->address);
756 lb_vip_del_ass_withlock(vip_index, ass, vec_len(ass));
761 lb_vip_del_adjacency(lbm, vip);
763 //Set the VIP as unused
764 vip->flags &= ~LB_VIP_FLAGS_USED;
766 lb_put_writer_lock();
771 VLIB_PLUGIN_REGISTER () = {
772 .version = VPP_BUILD_VER,
773 .description = "Load Balancer",
777 u8 *format_lb_dpo (u8 * s, va_list * va)
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);
786 static void lb_dpo_lock (dpo_id_t *dpo) {}
787 static void lb_dpo_unlock (dpo_id_t *dpo) {}
790 lb_fib_node_get_node (fib_node_index_t index)
792 lb_main_t *lbm = &lb_main;
793 lb_as_t *as = pool_elt_at_index (lbm->ass, index);
794 return (&as->fib_node);
798 lb_fib_node_last_lock_gone (fib_node_t *node)
803 lb_as_from_fib_node (fib_node_t *node)
805 return ((lb_as_t*)(((char*)node) -
806 STRUCT_OFFSET_OF(lb_as_t, fib_node)));
810 lb_as_stack (lb_as_t *as)
812 lb_main_t *lbm = &lb_main;
813 lb_vip_t *vip = &lbm->vips[as->vip_index];
814 dpo_type_t dpo_type = 0;
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;
824 lb_vip_is_ip4(vip)?DPO_PROTO_IP4:DPO_PROTO_IP6,
826 fib_entry_contribute_ip_forwarding(
827 as->next_hop_fib_entry_index));
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)
834 lb_as_stack(lb_as_from_fib_node(node));
835 return (FIB_NODE_BACK_WALK_CONTINUE);
839 lb_init (vlib_main_t * vm)
841 vlib_thread_main_t *tm = vlib_get_thread_main ();
842 lb_main_t *lbm = &lb_main;
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,
850 .dv_lock = lb_dpo_lock,
851 .dv_unlock = lb_dpo_unlock,
852 .dv_format = format_lb_dpo,
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);
870 //Init AS reference counters
871 vlib_refcount_init(&lbm->as_refcount);
873 //Allocate and init default AS.
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;
882 #define _(a,b,c) lbm->vip_counters[c].name = b;
883 lb_foreach_vip_counter
888 VLIB_INIT_FUNCTION (lb_init);