FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
lb.c
Go to the documentation of this file.
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 
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  {
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  {
49  };
50 
52 {
53  return (u32) (vlib_time_now(vm) + 10000);
54 }
55 
56 u8 *format_lb_main (u8 * s, va_list * args)
57 {
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);
65 
66  u32 thread_index;
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;
69  if (h) {
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));
73  }
74  }
75 
76  return s;
77 }
78 
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",
84 };
85 
86 u8 *format_lb_vip_type (u8 * s, va_list * args)
87 {
88  lb_vip_type_t vipt = va_arg (*args, lb_vip_type_t);
89  u32 i;
90  for (i=0; i<LB_VIP_N_TYPES; i++)
91  if (vipt == i)
92  return format(s, lb_vip_type_strings[i]);
93  return format(s, "_WRONG_TYPE_");
94 }
95 
96 uword unformat_lb_vip_type (unformat_input_t * input, va_list * args)
97 {
98  lb_vip_type_t *vipt = va_arg (*args, lb_vip_type_t *);
99  u32 i;
100  for (i=0; i<LB_VIP_N_TYPES; i++)
101  if (unformat(input, lb_vip_type_strings[i])) {
102  *vipt = i;
103  return 1;
104  }
105  return 0;
106 }
107 
108 u8 *format_lb_vip (u8 * s, va_list * args)
109 {
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,
114  vip->new_flow_table_mask + 1,
115  pool_elts(vip->as_indexes),
116  (vip->flags & LB_VIP_FLAGS_USED)?"":" removed");
117 }
118 
119 u8 *format_lb_as (u8 * s, va_list * args)
120 {
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");
125 }
126 
127 u8 *format_lb_vip_detailed (u8 * s, va_list * args)
128 {
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);
132 
133  s = format(s, "%U %U [%lu] %U%s\n"
134  "%U new_size:%u\n",
135  format_white_space, indent,
136  format_lb_vip_type, vip->type,
137  vip - lbm->vips,
139  (vip->flags & LB_VIP_FLAGS_USED)?"":" removed",
140  format_white_space, indent,
141  vip->new_flow_table_mask + 1);
142 
143  //Print counters
144  s = format(s, "%U counters:\n",
145  format_white_space, indent);
146  u32 i;
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));
152 
153 
154  s = format(s, "%U #as:%u\n",
155  format_white_space, indent,
156  pool_elts(vip->as_indexes));
157 
158  //Let's count the buckets for each AS
159  u32 *count = 0;
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]++;
164 
165  lb_as_t *as;
166  u32 *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),
174  as->dpo.dpoi_index,
175  (as->flags & LB_AS_FLAGS_USED)?"used":" removed");
176  });
177 
178  vec_free(count);
179 
180  /*
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);
185  }
186  */
187  return s;
188 }
189 
190 typedef struct {
195 
196 static int lb_pseudorand_compare(void *a, void *b)
197 {
198  lb_as_t *asa, *asb;
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));
203 }
204 
206 {
207  lb_main_t *lbm = &lb_main;
208  ASSERT (lbm->writer_lock[0]);
209 
210  u32 now = (u32) vlib_time_now(vlib_get_main());
212  return;
213 
214  vip->last_garbage_collection = now;
215  lb_as_t *as;
216  u32 *as_index;
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))
222  { //Not referenced
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,
226  FIB_SOURCE_RR);
227  as->next_hop_fib_entry_index = FIB_NODE_INDEX_INVALID;
228 
229  pool_put(vip->as_indexes, as_index);
230  pool_put(lbm->ass, as);
231  }
232  });
233 }
234 
236 {
237  lb_main_t *lbm = &lb_main;
239  lb_vip_t *vip;
240  u32 *to_be_removed_vips = 0, *i;
241  pool_foreach(vip, lbm->vips, {
242  lb_vip_garbage_collection(vip);
243 
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);
247  }
248  });
249 
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);
254  }
255 
256  vec_free(to_be_removed_vips);
258 }
259 
261 {
262  lb_main_t *lbm = &lb_main;
263  lb_new_flow_entry_t *old_table;
264  u32 i, *as_index;
265  lb_new_flow_entry_t *new_flow_table = 0;
266  lb_as_t *as;
267  lb_pseudorand_t *pr, *sort_arr = 0;
268  u32 count;
269 
270  ASSERT (lbm->writer_lock[0]); //We must have the lock
271 
272  //Check if some AS is configured or not
273  i = 0;
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
277  i = 1;
278  goto out; //Not sure 'break' works in this macro-loop
279  }
280  });
281 
282 out:
283  if (i == 0) {
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;
288 
289  goto finished;
290  }
291 
292  //First, let's sort the ASs
293  sort_arr = 0;
294  vec_alloc(sort_arr, pool_elts(vip->as_indexes));
295 
296  i = 0;
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
300  continue;
301 
302  sort_arr[i].as_index = as - lbm->ass;
303  i++;
304  });
305  _vec_len(sort_arr) = i;
306 
308 
309  //Now let's pseudo-randomly generate permutations
310  vec_foreach(pr, sort_arr) {
311  lb_as_t *as = &lbm->ass[pr->as_index];
312 
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).
321  */
322  pr->skip = ((seed & 0xffffffff) | 1) & vip->new_flow_table_mask;
323  pr->last = (seed >> 32) & vip->new_flow_table_mask;
324  }
325 
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;
330 
331  u32 done = 0;
332  while (1) {
333  vec_foreach(pr, sort_arr) {
334  while (1) {
335  u32 last = pr->last;
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;
339  break;
340  }
341  }
342  done++;
343  if (done == vec_len(new_flow_table))
344  goto finished;
345  }
346  }
347 
348  vec_free(sort_arr);
349 
350 finished:
351 
352 //Count number of changed entries
353  count = 0;
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)
357  count++;
358 
359  old_table = vip->new_flow_table;
360  vip->new_flow_table = new_flow_table;
361  vec_free(old_table);
362 }
363 
364 int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address,
365  u32 per_cpu_sticky_buckets, u32 flow_timeout)
366 {
367  lb_main_t *lbm = &lb_main;
368 
369  if (!is_pow2(per_cpu_sticky_buckets))
370  return VNET_API_ERROR_INVALID_MEMORY_SIZE;
371 
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;
378  return 0;
379 }
380 
381 static
382 int lb_vip_find_index_with_lock(ip46_address_t *prefix, u8 plen, u32 *vip_index)
383 {
384  lb_main_t *lbm = &lb_main;
385  lb_vip_t *vip;
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) &&
390  vip->plen == plen &&
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;
394  return 0;
395  }
396  });
397  return VNET_API_ERROR_NO_SUCH_ENTRY;
398 }
399 
400 int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index)
401 {
402  int ret;
404  ret = lb_vip_find_index_with_lock(prefix, plen, vip_index);
406  return ret;
407 }
408 
409 static int lb_as_find_index_vip(lb_vip_t *vip, ip46_address_t *address, u32 *as_index)
410 {
411  lb_main_t *lbm = &lb_main;
412  ASSERT (lbm->writer_lock[0]); //This must be called with the lock owned
413  lb_as_t *as;
414  u32 *asi;
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;
421  return 0;
422  }
423  });
424  return -1;
425 }
426 
427 int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
428 {
429  lb_main_t *lbm = &lb_main;
431  lb_vip_t *vip;
432  if (!(vip = lb_vip_get_by_index(vip_index))) {
434  return VNET_API_ERROR_NO_SUCH_ENTRY;
435  }
436 
438  u32 *to_be_added = 0;
439  u32 *to_be_updated = 0;
440  u32 i;
441  u32 *ip;
442 
443  //Sanity check
444  while (n--) {
445 
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);
451  return VNET_API_ERROR_VALUE_EXIST;
452  }
453  vec_add1(to_be_updated, i);
454  goto next;
455  }
456 
457  if (ip46_address_type(&addresses[n]) != type) {
458  vec_free(to_be_added);
459  vec_free(to_be_updated);
461  return VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
462  }
463 
464  if (n) {
465  u32 n2 = n;
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])
469  goto next;
470  }
471 
472  vec_add1(to_be_added, n);
473 
474 next:
475  continue;
476  }
477 
478  //Update reused ASs
479  vec_foreach(ip, to_be_updated) {
480  lbm->ass[*ip].flags = LB_AS_FLAGS_USED;
481  }
482  vec_free(to_be_updated);
483 
484  //Create those who have to be created
485  vec_foreach(ip, to_be_added) {
486  lb_as_t *as;
487  u32 *as_index;
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;
494 
495  /*
496  * become a child of the FIB entry
497  * so we are informed when its forwarding changes
498  */
499  fib_prefix_t nh = {};
500  if (lb_vip_is_gre4(vip)) {
501  nh.fp_addr.ip4 = as->address.ip4;
502  nh.fp_len = 32;
504  } else {
505  nh.fp_addr.ip6 = as->address.ip6;
506  nh.fp_len = 128;
508  }
509 
512  &nh,
517  lbm->fib_node_type,
518  as - lbm->ass);
519 
520  lb_as_stack(as);
521  }
522  vec_free(to_be_added);
523 
524  //Recompute flows
526 
527  //Garbage collection maybe
529 
531  return 0;
532 }
533 
534 int lb_vip_del_ass_withlock(u32 vip_index, ip46_address_t *addresses, u32 n)
535 {
536  lb_main_t *lbm = &lb_main;
537  u32 now = (u32) vlib_time_now(vlib_get_main());
538  u32 *ip = 0;
539 
540  lb_vip_t *vip;
541  if (!(vip = lb_vip_get_by_index(vip_index))) {
542  return VNET_API_ERROR_NO_SUCH_ENTRY;
543  }
544 
545  u32 *indexes = NULL;
546  while (n--) {
547  u32 i;
548  if (lb_as_find_index_vip(vip, &addresses[n], &i)) {
549  vec_free(indexes);
550  return VNET_API_ERROR_NO_SUCH_ENTRY;
551  }
552 
553  if (n) { //Check for duplicates
554  u32 n2 = n - 1;
555  while(n2--) {
556  if (addresses[n2].as_u64[0] == addresses[n].as_u64[0] &&
557  addresses[n2].as_u64[1] == addresses[n].as_u64[1])
558  goto next;
559  }
560  }
561 
562  vec_add1(indexes, i);
563 next:
564  continue;
565  }
566 
567  //Garbage collection maybe
569 
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;
574  }
575 
576  //Recompute flows
578  }
579 
580  vec_free(indexes);
581  return 0;
582 }
583 
584 int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
585 {
587  int ret = lb_vip_del_ass_withlock(vip_index, addresses, n);
589  return ret;
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  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;
604  proto = DPO_PROTO_IP4;
605  } else {
606  pfx.fp_addr.ip6 = vip->prefix.ip6;
607  pfx.fp_len = vip->plen;
609  proto = DPO_PROTO_IP6;
610  }
611  dpo_set(&dpo, lb_vip_is_gre4(vip)?lbm->dpo_gre4_type:lbm->dpo_gre6_type,
612  proto, vip - lbm->vips);
614  &pfx,
617  &dpo);
618  dpo_reset(&dpo);
619 }
620 
621 /**
622  * Deletes the adjacency associated with the VIP
623  */
624 static void lb_vip_del_adjacency(lb_main_t *lbm, lb_vip_t *vip)
625 {
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;
631  } else {
632  pfx.fp_addr.ip6 = vip->prefix.ip6;
633  pfx.fp_len = vip->plen;
635  }
637 }
638 
639 int lb_vip_add(ip46_address_t *prefix, u8 plen, lb_vip_type_t type, u32 new_length, u32 *vip_index)
640 {
641  lb_main_t *lbm = &lb_main;
642  lb_vip_t *vip;
644  ip46_prefix_normalize(prefix, plen);
645 
646  if (!lb_vip_find_index_with_lock(prefix, plen, vip_index)) {
648  return VNET_API_ERROR_VALUE_EXIST;
649  }
650 
651  if (!is_pow2(new_length)) {
653  return VNET_API_ERROR_INVALID_MEMORY_SIZE;
654  }
655 
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;
660 
661 
662  //Allocate
663  pool_get(lbm->vips, vip);
664 
665  //Init
666  vip->prefix = *prefix;
667  vip->plen = plen;
669  vip->type = type;
670  vip->flags = LB_VIP_FLAGS_USED;
671  vip->as_indexes = 0;
672 
673  //Validate counters
674  u32 i;
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);
678  }
679 
680  //Configure new flow table
681  vip->new_flow_table_mask = new_length - 1;
682  vip->new_flow_table = 0;
683 
684  //Create a new flow hash table full of the default entry
686 
687  //Create adjacency to direct traffic
688  lb_vip_add_adjacency(lbm, vip);
689 
690  //Return result
691  *vip_index = vip - lbm->vips;
692 
694  return 0;
695 }
696 
697 int lb_vip_del(u32 vip_index)
698 {
699  lb_main_t *lbm = &lb_main;
700  lb_vip_t *vip;
702  if (!(vip = lb_vip_get_by_index(vip_index))) {
704  return VNET_API_ERROR_NO_SUCH_ENTRY;
705  }
706 
707  //FIXME: This operation is actually not working
708  //We will need to remove state before performing this.
709 
710  {
711  //Remove all ASs
712  ip46_address_t *ass = 0;
713  lb_as_t *as;
714  u32 *as_index;
715  pool_foreach(as_index, vip->as_indexes, {
716  as = &lbm->ass[*as_index];
717  vec_add1(ass, as->address);
718  });
719  if (vec_len(ass))
720  lb_vip_del_ass_withlock(vip_index, ass, vec_len(ass));
721  vec_free(ass);
722  }
723 
724  //Delete adjacency
725  lb_vip_del_adjacency(lbm, vip);
726 
727  //Set the VIP as unused
728  vip->flags &= ~LB_VIP_FLAGS_USED;
729 
731  return 0;
732 }
733 
734 /* *INDENT-OFF* */
736  .version = VPP_BUILD_VER,
737  .description = "Load Balancer",
738 };
739 /* *INDENT-ON* */
740 
741 u8 *format_lb_dpo (u8 * s, va_list * va)
742 {
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);
748 }
749 
750 static void lb_dpo_lock (dpo_id_t *dpo) {}
751 static void lb_dpo_unlock (dpo_id_t *dpo) {}
752 
753 static fib_node_t *
755 {
756  lb_main_t *lbm = &lb_main;
757  lb_as_t *as = pool_elt_at_index (lbm->ass, index);
758  return (&as->fib_node);
759 }
760 
761 static void
763 {
764 }
765 
766 static lb_as_t *
768 {
769  return ((lb_as_t*)(((char*)node) -
770  STRUCT_OFFSET_OF(lb_as_t, fib_node)));
771 }
772 
773 static void
775 {
776  lb_main_t *lbm = &lb_main;
777  lb_vip_t *vip = &lbm->vips[as->vip_index];
780  &as->dpo,
783 }
784 
788 {
791 }
792 
793 clib_error_t *
795 {
797  lb_main_t *lbm = &lb_main;
798  lb_as_t *default_as;
799  fib_node_vft_t lb_fib_node_vft = {
801  .fnv_last_lock = lb_fib_node_last_lock_gone,
802  .fnv_back_walk = lb_fib_node_back_walk_notify,
803  };
804  dpo_vft_t lb_vft = {
805  .dv_lock = lb_dpo_lock,
806  .dv_unlock = lb_dpo_unlock,
807  .dv_format = format_lb_dpo,
808  };
809 
810  lbm->vips = 0;
811  lbm->per_cpu = 0;
812  vec_validate(lbm->per_cpu, tm->n_vlib_mains - 1);
814  lbm->writer_lock[0] = 0;
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;
822  lbm->fib_node_type = fib_node_register_new_type(&lb_fib_node_vft);
823 
824  //Init AS reference counters
826 
827  //Allocate and init default AS.
828  lbm->ass = 0;
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;
835 
836 #define _(a,b,c) lbm->vip_counters[c].name = b;
838 #undef _
839  return NULL;
840 }
841 
u32 skip
Definition: lb.c:193
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:432
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:181
dpo_lock_fn_t dv_lock
A reference counting lock function.
Definition: dpo.h:382
u32 lb_hash_time_now(vlib_main_t *vm)
Definition: lb.c:51
Recursive resolution source.
Definition: fib_entry.h:117
static int lb_pseudorand_compare(void *a, void *b)
Definition: lb.c:196
Each VIP is configured with a set of application server.
Definition: lb.h:55
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
u8 * format_ip46_prefix(u8 *s, va_list *args)
Definition: kp.c:71
#define LB_GARBAGE_RUN
Definition: lb.c:22
#define CLIB_UNUSED(x)
Definition: clib.h:79
A virtual function table regisitered for a DPO type.
Definition: dpo.h:377
ip46_type_t
Definition: format.h:63
a
Definition: bitmap.h:516
u32 last
Definition: lb.c:192
u32 per_cpu_sticky_buckets
Number of buckets in the per-cpu sticky hash table.
Definition: lb.h:272
clib_error_t * lb_init(vlib_main_t *vm)
Definition: lb.c:794
u64 as_u64
Definition: bihash_doc.h:63
u32 fib_entry_child_add(fib_node_index_t fib_entry_index, fib_node_type_t child_type, fib_node_index_t child_index)
Definition: fib_entry.c:503
static void lb_fib_node_last_lock_gone(fib_node_t *node)
Definition: lb.c:762
u64 as_u64[2]
Definition: ip6_packet.h:51
static void lb_vip_update_new_flow_table(lb_vip_t *vip)
Definition: lb.c:260
static int lb_as_find_index_vip(lb_vip_t *vip, ip46_address_t *address, u32 *as_index)
Definition: lb.c:409
#define NULL
Definition: clib.h:55
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:224
enum fib_node_back_walk_rc_t_ fib_node_back_walk_rc_t
Return code from a back walk function.
const dpo_id_t * fib_entry_contribute_ip_forwarding(fib_node_index_t fib_entry_index)
Definition: fib_entry.c:459
static void lb_vip_del_adjacency(lb_main_t *lbm, lb_vip_t *vip)
Deletes the adjacency associated with the VIP.
Definition: lb.c:624
int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address, u32 per_cpu_sticky_buckets, u32 flow_timeout)
Fix global load-balancer parameters.
Definition: lb.c:364
#define lb_get_writer_lock()
Definition: lb.c:29
int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: lb.c:427
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:41
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:518
ip46_address_t prefix
A Virtual IP represents a given service delivered by a set of application servers.
Definition: lb.h:184
static u64 clib_xxhash(u64 key)
Definition: xxhash.h:58
static heap_elt_t * last(heap_header_t *h)
Definition: heap.c:53
static_always_inline void vlib_refcount_init(vlib_refcount_t *r)
Definition: refcount.h:80
static void lb_dpo_lock(dpo_id_t *dpo)
Definition: lb.c:750
format_function_t format_ip46_address
Definition: format.h:61
static u32 format_get_indent(u8 *s)
Definition: format.h:72
#define STRUCT_OFFSET_OF(t, f)
Definition: clib.h:62
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static void lb_as_stack(lb_as_t *as)
Definition: lb.c:774
#define ip46_address_type(ip46)
Definition: kp.h:431
void ip46_prefix_normalize(ip46_address_t *prefix, u8 plen)
Definition: kp.c:35
#define lb_vip_get_by_index(index)
Definition: lb.h:322
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:225
u32 vip_index
ASs are indexed by address and VIP Index.
Definition: lb.h:74
#define vec_alloc(V, N)
Allocate space for N more elements (no header, unspecified alignment)
Definition: vec.h:275
static const char *const *const lb_dpo_gre6_nodes[DPO_PROTO_NUM]
Definition: lb.c:45
lb_hash_t * sticky_ht
Each CPU has its own sticky flow hash table.
Definition: lb.h:225
#define pool_len(p)
Number of elements in pool vector.
Definition: pool.h:140
fib_node_type_t fib_node_register_new_type(const fib_node_vft_t *vft)
Create a new FIB node type and Register the function table for it.
Definition: fib_node.c:80
#define LB_VIP_FLAGS_USED
Definition: lb.h:206
ip46_address_t address
Destination address used to tunnel traffic towards that application server.
Definition: lb.h:67
int lb_vip_del_ass_withlock(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: lb.c:534
u32 timeout
Definition: lbhash.h:59
static counter_t vlib_get_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
Get the value of a simple counter Scrapes the entire set of per-thread counters.
Definition: counter.h:97
format_function_t format_ip4_address
Definition: format.h:79
#define LB_AS_FLAGS_USED
Definition: lb.h:82
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:438
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
static lb_as_t * lb_as_from_fib_node(fib_node_t *node)
Definition: lb.c:767
void fib_table_entry_special_remove(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source)
Remove a &#39;special&#39; entry from the FIB.
Definition: fib_table.c:390
VLIB_PLUGIN_REGISTER()
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
#define LB_DEFAULT_PER_CPU_STICKY_BUCKETS
lb-plugin implements a MagLev-like load balancer.
Definition: lb.h:43
lb_main_t lb_main
Definition: lb.c:27
static const char *const lb_dpo_gre4_ip4[]
Definition: lb.c:35
u32 flow_timeout
Flow timeout in seconds.
Definition: lb.h:277
A high priority source a plugin can use.
Definition: fib_entry.h:62
Definition: lb.h:228
fib_node_type_t fib_node_type
Node type for registering to fib changes.
Definition: lb.h:293
dpo_type_t dpo_gre4_type
DPO used to send packet from IP4/6 lookup to LB node.
Definition: lb.h:287
Aggregrate type for a prefix.
Definition: fib_types.h:172
static void lb_vip_add_adjacency(lb_main_t *lbm, lb_vip_t *vip)
Add the VIP adjacency to the ip4 or ip6 fib.
Definition: lb.c:595
vlib_refcount_t as_refcount
Each AS has an associated reference counter.
Definition: lb.h:247
unsigned long u64
Definition: types.h:89
static void lb_vip_garbage_collection(lb_vip_t *vip)
Definition: lb.c:205
u8 * format_lb_main(u8 *s, va_list *args)
Definition: lb.c:56
u8 * format_lb_vip(u8 *s, va_list *args)
Definition: lb.c:108
enum dpo_proto_t_ dpo_proto_t
Data path protocol.
u16 fp_len
The mask length.
Definition: fib_types.h:176
#define lb_vip_is_ip4(vip)
Definition: lb.h:215
lb_vip_t * vips
Pool of all Virtual IPs.
Definition: lb.h:232
dpo_type_t dpo_register_new_type(const dpo_vft_t *vft, const char *const *const *nodes)
Create and register a new DPO type.
Definition: dpo.c:322
u32 last_used
Rotating timestamp of when LB_AS_FLAGS_USED flag was last set.
Definition: lb.h:93
ip4_address_t ip4_src_address
Source address used for IPv4 encapsulated traffic.
Definition: lb.h:267
Definition: fib_entry.h:238
char * name
The counter collection&#39;s name.
Definition: counter.h:65
u8 plen
The VIP prefix length.
Definition: lb.h:190
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:166
Definition: fib_entry.h:242
#define lb_vip_is_gre4(vip)
Definition: lb.h:216
static const char *const lb_dpo_gre6_ip4[]
Definition: lb.c:43
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:459
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:195
int lb_vip_del(u32 vip_index)
Definition: lb.c:697
u8 * format_lb_vip_type(u8 *s, va_list *args)
Definition: lb.c:86
struct _unformat_input_t unformat_input_t
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:271
uword unformat_lb_vip_type(unformat_input_t *input, va_list *args)
Definition: lb.c:96
#define LB_DEFAULT_FLOW_TIMEOUT
Definition: lb.h:44
static const char *const *const lb_dpo_gre4_nodes[DPO_PROTO_NUM]
Definition: lb.c:37
An node in the FIB graph.
Definition: fib_node.h:286
Definition: lb.h:114
#define clib_u32_loop_gt(a, b)
32 bits integer comparison for running values.
Definition: kphash.h:47
fib_node_t fib_node
Registration to FIB event.
Definition: lb.h:59
static const char *const lb_dpo_gre6_ip6[]
Definition: lb.c:44
static const dpo_vft_t lb_vft
Definition: load_balance.c:787
#define pool_free(p)
Free a pool.
Definition: pool.h:352
fib_node_index_t fib_table_entry_special_add(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source, fib_entry_flag_t flags)
Add a &#39;special&#39; entry to the FIB.
Definition: fib_table.c:371
int lb_vip_add(ip46_address_t *prefix, u8 plen, lb_vip_type_t type, u32 new_length, u32 *vip_index)
Definition: lb.c:639
format_function_t format_ip6_address
Definition: format.h:95
vlib_main_t * vm
Definition: buffer.c:283
vec_header_t h
Definition: buffer.c:282
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:336
volatile u32 * writer_lock
Definition: lb.h:300
#define lb_foreach_vip_counter
Definition: lb.h:118
fib_node_get_t fnv_get
Definition: fib_node.h:274
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:29
u32 as_index
Definition: lb.h:115
static fib_node_back_walk_rc_t lb_fib_node_back_walk_notify(fib_node_t *node, fib_node_back_walk_ctx_t *ctx)
Definition: lb.c:786
int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: lb.c:584
void dpo_set(dpo_id_t *dpo, dpo_type_t type, dpo_proto_t proto, index_t index)
Set/create a DPO ID The DPO will be locked.
Definition: dpo.c:182
dpo_type_t dpo_gre6_type
Definition: lb.h:288
u32 last_garbage_collection
Last time garbage collection was run to free the ASs.
Definition: lb.h:173
lb_as_t * ass
Pool of ASs.
Definition: lb.h:240
lb_vip_type_t type
The type of traffic for this.
Definition: lb.h:196
Context passed between object during a back walk.
Definition: fib_node.h:199
fib_node_index_t fib_table_entry_special_dpo_add(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source, fib_entry_flag_t flags, const dpo_id_t *dpo)
Add a &#39;special&#39; entry to the FIB that links to the DPO passed A special entry is an entry that the FI...
Definition: fib_table.c:290
void vlib_validate_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
validate a simple counter
Definition: counter.c:78
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
lb_vip_type_t
The load balancer supports IPv4 and IPv6 traffic and GRE4 and GRE6 encap.
Definition: lb.h:135
long ctx[MAX_CONNS]
Definition: main.c:122
int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index)
Definition: lb.c:400
u8 * format_lb_as(u8 *s, va_list *args)
Definition: lb.c:119
u32 new_flow_table_mask
New flows table length - 1 (length MUST be a power of 2)
Definition: lb.h:168
static void vlib_zero_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
Clear a simple counter Clears the set of per-thread u16 counters, and the u64 counter.
Definition: counter.h:123
size_t count
Definition: vapi.c:42
lb_per_cpu_t * per_cpu
Some global data is per-cpu.
Definition: lb.h:252
static void lb_dpo_unlock(dpo_id_t *dpo)
Definition: lb.c:751
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
static uword is_pow2(uword x)
Definition: clib.h:280
u32 as_index
Definition: lb.c:191
u64 uword
Definition: types.h:112
vlib_simple_counter_main_t vip_counters[LB_N_VIP_COUNTERS]
Per VIP counter.
Definition: lb.h:282
static const char *const lb_dpo_gre4_ip6[]
Definition: lb.c:36
#define DPO_PROTO_NUM
Definition: dpo.h:70
ip6_address_t ip6_src_address
Source address used in IPv6 encapsulated traffic.
Definition: lb.h:262
u8 * format_lb_vip_detailed(u8 *s, va_list *args)
Definition: lb.c:127
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
static fib_node_t * lb_fib_node_get_node(fib_node_index_t index)
Definition: lb.c:754
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:956
#define DPO_INVALID
An initialiser for DPOs declared on the stack.
Definition: dpo.h:193
void lb_garbage_collection()
Definition: lb.c:235
static int lb_vip_find_index_with_lock(ip46_address_t *prefix, u8 plen, u32 *vip_index)
Definition: lb.c:382
u32 next_hop_child_index
The child index on the FIB entry.
Definition: lb.h:103
A FIB graph nodes virtual function table.
Definition: fib_node.h:273
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:120
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
void dpo_reset(dpo_id_t *dpo)
reset a DPO ID The DPO will be unlocked.
Definition: dpo.c:228
#define vec_foreach(var, vec)
Vector iterator.
dpo_id_t dpo
The next DPO in the graph to follow.
Definition: lb.h:108
u8 flags
Some per-AS flags.
Definition: lb.h:80
u16 dpoi_next_node
The next VLIB node to follow.
Definition: dpo.h:178
#define ip46_prefix_is_ip4(ip46, len)
Definition: kp.h:432
lb_new_flow_entry_t * new_flow_table
Vector mapping (flow-hash & new_connect_table_mask) to AS index.
Definition: lb.h:162
u8 flags
Flags related to this VIP.
Definition: lb.h:205
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
static char * lb_vip_type_strings[]
Definition: lb.c:79
u8 * format_lb_dpo(u8 *s, va_list *va)
Definition: lb.c:741
Load balancing service is provided per VIP.
Definition: lb.h:154
u32 * as_indexes
Pool of AS indexes used for this VIP.
Definition: lb.h:212
#define lb_hash_size(h)
Definition: lbhash.h:64
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
void dpo_stack(dpo_type_t child_type, dpo_proto_t child_proto, dpo_id_t *dpo, const dpo_id_t *parent)
Stack one DPO object on another, and thus establish a child-parent relationship.
Definition: dpo.c:470
#define lb_put_writer_lock()
Definition: lb.c:30
fib_node_index_t next_hop_fib_entry_index
The FIB entry index for the next-hop.
Definition: lb.h:98
static_always_inline u32 lb_hash_elts(lb_hash_t *h, u32 time_now)
Definition: lbhash.h:205
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128