FD.io VPP  v16.12-rc0-308-g931be3a
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 <vnet/api_errno.h>
19 
20 //GC runs at most once every so many seconds
21 #define LB_GARBAGE_RUN 60
22 
23 //After so many seconds. It is assumed that inter-core race condition will not occur.
24 #define LB_CONCURRENCY_TIMEOUT 10
25 
27 
28 #define lb_get_writer_lock() do {} while(__sync_lock_test_and_set (lb_main.writer_lock, 1))
29 #define lb_put_writer_lock() lb_main.writer_lock[0] = 0
30 
31 static void lb_as_stack (lb_as_t *as);
32 
33 
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] =
37  {
40  };
41 
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] =
45  {
48  };
49 
51 {
52  return (u32) (vlib_time_now(vm) + 10000);
53 }
54 
55 u8 *format_lb_main (u8 * s, va_list * args)
56 {
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);
64 
65  u32 cpu_index;
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;
68  if (h) {
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));
72  }
73  }
74 
75  return s;
76 }
77 
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",
83 };
84 
85 u8 *format_lb_vip_type (u8 * s, va_list * args)
86 {
87  lb_vip_type_t vipt = va_arg (*args, lb_vip_type_t);
88  u32 i;
89  for (i=0; i<LB_VIP_N_TYPES; i++)
90  if (vipt == i)
91  return format(s, lb_vip_type_strings[i]);
92  return format(s, "_WRONG_TYPE_");
93 }
94 
95 uword unformat_lb_vip_type (unformat_input_t * input, va_list * args)
96 {
97  lb_vip_type_t *vipt = va_arg (*args, lb_vip_type_t *);
98  u32 i;
99  for (i=0; i<LB_VIP_N_TYPES; i++)
100  if (unformat(input, lb_vip_type_strings[i])) {
101  *vipt = i;
102  return 1;
103  }
104  return 0;
105 }
106 
107 u8 *format_lb_vip (u8 * s, va_list * args)
108 {
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,
113  vip->new_flow_table_mask + 1,
114  pool_elts(vip->as_indexes),
115  (vip->flags & LB_VIP_FLAGS_USED)?"":" removed");
116 }
117 
118 u8 *format_lb_as (u8 * s, va_list * args)
119 {
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");
124 }
125 
126 u8 *format_lb_vip_detailed (u8 * s, va_list * args)
127 {
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);
131 
132  s = format(s, "%U %U [%u] %U%s\n"
133  "%U new_size:%u\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);
140 
141  //Print counters
142  s = format(s, "%U counters:\n",
143  format_white_space, indent);
144  u32 i;
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));
150 
151 
152  s = format(s, "%U #as:%u\n",
153  format_white_space, indent,
154  pool_elts(vip->as_indexes));
155 
156  //Let's count the buckets for each AS
157  u32 *count = 0;
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]++;
162 
163  lb_as_t *as;
164  u32 *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),
172  as->dpo.dpoi_index,
173  (as->flags & LB_AS_FLAGS_USED)?"used":" removed");
174  });
175 
176  vec_free(count);
177 
178  /*
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);
183  }
184  */
185  return s;
186 }
187 
188 typedef struct {
193 
194 static int lb_pseudorand_compare(void *a, void *b)
195 {
196  lb_as_t *asa, *asb;
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));
201 }
202 
204 {
205  lb_main_t *lbm = &lb_main;
206  ASSERT (lbm->writer_lock[0]);
207 
208  u32 now = (u32) vlib_time_now(vlib_get_main());
210  return;
211 
212  vip->last_garbage_collection = now;
213  lb_as_t *as;
214  u32 *as_index;
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))
220  { //Not referenced
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,
224  FIB_SOURCE_RR);
225  as->next_hop_fib_entry_index = FIB_NODE_INDEX_INVALID;
226 
227  pool_put(vip->as_indexes, as_index);
228  pool_put(lbm->ass, as);
229  }
230  });
231 }
232 
234 {
235  lb_main_t *lbm = &lb_main;
237  lb_vip_t *vip;
238  u32 *to_be_removed_vips = 0, *i;
239  pool_foreach(vip, lbm->vips, {
240  lb_vip_garbage_collection(vip);
241 
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);
245  }
246  });
247 
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);
252  }
253 
254  vec_free(to_be_removed_vips);
256 }
257 
259 {
260  lb_main_t *lbm = &lb_main;
261  lb_new_flow_entry_t *old_table;
262  u32 i, *as_index;
263  lb_new_flow_entry_t *new_flow_table = 0;
264  lb_as_t *as;
265  lb_pseudorand_t *pr, *sort_arr = 0;
266  u32 count;
267 
268  ASSERT (lbm->writer_lock[0]); //We must have the lock
269 
270  //Check if some AS is configured or not
271  i = 0;
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
275  i = 1;
276  goto out; //Not sure 'break' works in this macro-loop
277  }
278  });
279 
280 out:
281  if (i == 0) {
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;
286 
287  goto finished;
288  }
289 
290  //First, let's sort the ASs
291  sort_arr = 0;
292  vec_alloc(sort_arr, pool_elts(vip->as_indexes));
293 
294  i = 0;
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
298  continue;
299 
300  sort_arr[i].as_index = as - lbm->ass;
301  i++;
302  });
303  _vec_len(sort_arr) = i;
304 
306 
307  //Now let's pseudo-randomly generate permutations
308  vec_foreach(pr, sort_arr) {
309  lb_as_t *as = &lbm->ass[pr->as_index];
310 
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).
319  */
320  pr->skip = ((seed & 0xffffffff) | 1) & vip->new_flow_table_mask;
321  pr->last = (seed >> 32) & vip->new_flow_table_mask;
322  }
323 
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;
328 
329  u32 done = 0;
330  while (1) {
331  vec_foreach(pr, sort_arr) {
332  while (1) {
333  u32 last = pr->last;
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;
337  break;
338  }
339  }
340  done++;
341  if (done == vec_len(new_flow_table))
342  goto finished;
343  }
344  }
345 
346  vec_free(sort_arr);
347 
348 finished:
349 
350 //Count number of changed entries
351  count = 0;
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)
355  count++;
356 
357  old_table = vip->new_flow_table;
358  vip->new_flow_table = new_flow_table;
359  vec_free(old_table);
360 }
361 
362 int lb_conf(ip4_address_t *ip4_address, ip6_address_t *ip6_address,
363  u32 per_cpu_sticky_buckets, u32 flow_timeout)
364 {
365  lb_main_t *lbm = &lb_main;
366 
367  if (!is_pow2(per_cpu_sticky_buckets))
368  return VNET_API_ERROR_INVALID_MEMORY_SIZE;
369 
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;
376  return 0;
377 }
378 
379 static
380 int lb_vip_find_index_with_lock(ip46_address_t *prefix, u8 plen, u32 *vip_index)
381 {
382  lb_main_t *lbm = &lb_main;
383  lb_vip_t *vip;
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) &&
388  vip->plen == plen &&
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;
392  return 0;
393  }
394  });
395  return VNET_API_ERROR_NO_SUCH_ENTRY;
396 }
397 
398 int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index)
399 {
400  int ret;
402  ret = lb_vip_find_index_with_lock(prefix, plen, vip_index);
404  return ret;
405 }
406 
407 static int lb_as_find_index_vip(lb_vip_t *vip, ip46_address_t *address, u32 *as_index)
408 {
409  lb_main_t *lbm = &lb_main;
410  ASSERT (lbm->writer_lock[0]); //This must be called with the lock owned
411  lb_as_t *as;
412  u32 *asi;
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;
419  return 0;
420  }
421  });
422  return -1;
423 }
424 
425 int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
426 {
427  lb_main_t *lbm = &lb_main;
429  lb_vip_t *vip;
430  if (!(vip = lb_vip_get_by_index(vip_index))) {
432  return VNET_API_ERROR_NO_SUCH_ENTRY;
433  }
434 
436  u32 *to_be_added = 0;
437  u32 *to_be_updated = 0;
438  u32 i;
439  u32 *ip;
440 
441  //Sanity check
442  while (n--) {
443 
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);
449  return VNET_API_ERROR_VALUE_EXIST;
450  }
451  vec_add1(to_be_updated, i);
452  goto next;
453  }
454 
455  if (ip46_address_type(&addresses[n]) != type) {
456  vec_free(to_be_added);
457  vec_free(to_be_updated);
459  return VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
460  }
461 
462  if (n) {
463  u32 n2 = n;
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])
467  goto next;
468  }
469 
470  vec_add1(to_be_added, n);
471 
472 next:
473  continue;
474  }
475 
476  //Update reused ASs
477  vec_foreach(ip, to_be_updated) {
478  lbm->ass[*ip].flags = LB_AS_FLAGS_USED;
479  }
480  vec_free(to_be_updated);
481 
482  //Create those who have to be created
483  vec_foreach(ip, to_be_added) {
484  lb_as_t *as;
485  u32 *as_index;
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;
492 
493  /*
494  * become a child of the FIB entry
495  * so we are informed when its forwarding changes
496  */
497  fib_prefix_t nh = {};
498  if (lb_vip_is_gre4(vip)) {
499  nh.fp_addr.ip4 = as->address.ip4;
500  nh.fp_len = 32;
502  } else {
503  nh.fp_addr.ip6 = as->address.ip6;
504  nh.fp_len = 128;
506  }
507 
510  &nh,
516  lbm->fib_node_type,
517  as - lbm->ass);
518 
519  lb_as_stack(as);
520  }
521  vec_free(to_be_added);
522 
523  //Recompute flows
525 
526  //Garbage collection maybe
528 
530  return 0;
531 }
532 
533 int lb_vip_del_ass_withlock(u32 vip_index, ip46_address_t *addresses, u32 n)
534 {
535  lb_main_t *lbm = &lb_main;
536  u32 now = (u32) vlib_time_now(vlib_get_main());
537  u32 *ip = 0;
538 
539  lb_vip_t *vip;
540  if (!(vip = lb_vip_get_by_index(vip_index))) {
541  return VNET_API_ERROR_NO_SUCH_ENTRY;
542  }
543 
544  u32 *indexes = NULL;
545  while (n--) {
546  u32 i;
547  if (lb_as_find_index_vip(vip, &addresses[n], &i)) {
548  vec_free(indexes);
549  return VNET_API_ERROR_NO_SUCH_ENTRY;
550  }
551 
552  if (n) { //Check for duplicates
553  u32 n2 = n - 1;
554  while(n2--) {
555  if (addresses[n2].as_u64[0] == addresses[n].as_u64[0] &&
556  addresses[n2].as_u64[1] == addresses[n].as_u64[1])
557  goto next;
558  }
559  }
560 
561  vec_add1(indexes, i);
562 next:
563  continue;
564  }
565 
566  //Garbage collection maybe
568 
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;
573  }
574 
575  //Recompute flows
577  }
578 
579  vec_free(indexes);
580  return 0;
581 }
582 
583 int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
584 {
586  int ret = lb_vip_del_ass_withlock(vip_index, addresses, n);
588  return ret;
589 }
590 
591 /**
592  * Add the VIP adjacency to the ip4 or ip6 fib
593  */
594 static void lb_vip_add_adjacency(lb_main_t *lbm, lb_vip_t *vip)
595 {
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;
603  proto = DPO_PROTO_IP4;
604  } else {
605  pfx.fp_addr.ip6 = vip->prefix.ip6;
606  pfx.fp_len = vip->plen;
608  proto = DPO_PROTO_IP6;
609  }
610  dpo_set(&dpo, lb_vip_is_gre4(vip)?lbm->dpo_gre4_type:lbm->dpo_gre6_type,
611  proto, vip - lbm->vips);
613  &pfx,
616  &dpo);
617  dpo_reset(&dpo);
618 }
619 
620 /**
621  * Deletes the adjacency associated with the VIP
622  */
623 static void lb_vip_del_adjacency(lb_main_t *lbm, lb_vip_t *vip)
624 {
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;
630  } else {
631  pfx.fp_addr.ip6 = vip->prefix.ip6;
632  pfx.fp_len = vip->plen;
634  }
636 }
637 
638 int lb_vip_add(ip46_address_t *prefix, u8 plen, lb_vip_type_t type, u32 new_length, u32 *vip_index)
639 {
640  lb_main_t *lbm = &lb_main;
641  lb_vip_t *vip;
643  ip46_prefix_normalize(prefix, plen);
644 
645  if (!lb_vip_find_index_with_lock(prefix, plen, vip_index)) {
647  return VNET_API_ERROR_VALUE_EXIST;
648  }
649 
650  if (!is_pow2(new_length)) {
652  return VNET_API_ERROR_INVALID_MEMORY_SIZE;
653  }
654 
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;
659 
660 
661  //Allocate
662  pool_get(lbm->vips, vip);
663 
664  //Init
665  vip->prefix = *prefix;
666  vip->plen = plen;
668  vip->type = type;
669  vip->flags = LB_VIP_FLAGS_USED;
670  vip->as_indexes = 0;
671 
672  //Validate counters
673  u32 i;
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);
677  }
678 
679  //Configure new flow table
680  vip->new_flow_table_mask = new_length - 1;
681  vip->new_flow_table = 0;
682 
683  //Create a new flow hash table full of the default entry
685 
686  //Create adjacency to direct traffic
687  lb_vip_add_adjacency(lbm, vip);
688 
689  //Return result
690  *vip_index = vip - lbm->vips;
691 
693  return 0;
694 }
695 
696 int lb_vip_del(u32 vip_index)
697 {
698  lb_main_t *lbm = &lb_main;
699  lb_vip_t *vip;
701  if (!(vip = lb_vip_get_by_index(vip_index))) {
703  return VNET_API_ERROR_NO_SUCH_ENTRY;
704  }
705 
706  //FIXME: This operation is actually not working
707  //We will need to remove state before performing this.
708 
709  {
710  //Remove all ASs
711  ip46_address_t *ass = 0;
712  lb_as_t *as;
713  u32 *as_index;
714  pool_foreach(as_index, vip->as_indexes, {
715  as = &lbm->ass[*as_index];
716  vec_add1(ass, as->address);
717  });
718  if (vec_len(ass))
719  lb_vip_del_ass_withlock(vip_index, ass, vec_len(ass));
720  vec_free(ass);
721  }
722 
723  //Delete adjacency
724  lb_vip_del_adjacency(lbm, vip);
725 
726  //Set the VIP as unused
727  vip->flags &= ~LB_VIP_FLAGS_USED;
728 
730  return 0;
731 }
732 
733 clib_error_t *
736  int from_early_init)
737 {
738  clib_error_t *error = 0;
739  return error;
740 }
741 
742 
743 u8 *format_lb_dpo (u8 * s, va_list * va)
744 {
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);
750 }
751 
752 static void lb_dpo_lock (dpo_id_t *dpo) {}
753 static void lb_dpo_unlock (dpo_id_t *dpo) {}
754 
755 static fib_node_t *
757 {
758  lb_main_t *lbm = &lb_main;
759  lb_as_t *as = pool_elt_at_index (lbm->ass, index);
760  return (&as->fib_node);
761 }
762 
763 static void
765 {
766 }
767 
768 static lb_as_t *
770 {
771  return ((lb_as_t*)(((char*)node) -
772  STRUCT_OFFSET_OF(lb_as_t, fib_node)));
773 }
774 
775 static void
777 {
778  lb_main_t *lbm = &lb_main;
779  lb_vip_t *vip = &lbm->vips[as->vip_index];
782  &as->dpo,
785 }
786 
790 {
793 }
794 
795 clib_error_t *
797 {
799  lb_main_t *lbm = &lb_main;
800  lb_as_t *default_as;
801  fib_node_vft_t lb_fib_node_vft = {
803  .fnv_last_lock = lb_fib_node_last_lock_gone,
804  .fnv_back_walk = lb_fib_node_back_walk_notify,
805  };
806  dpo_vft_t lb_vft = {
807  .dv_lock = lb_dpo_lock,
808  .dv_unlock = lb_dpo_unlock,
809  .dv_format = format_lb_dpo,
810  };
811 
812  lbm->vips = 0;
813  lbm->per_cpu = 0;
814  vec_validate(lbm->per_cpu, tm->n_vlib_mains - 1);
816  lbm->writer_lock[0] = 0;
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;
824  lbm->fib_node_type = fib_node_register_new_type(&lb_fib_node_vft);
825 
826  //Init AS reference counters
828 
829  //Allocate and init default AS.
830  lbm->ass = 0;
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;
837 
838 #define _(a,b,c) lbm->vip_counters[c].name = b;
840 #undef _
841  return NULL;
842 }
843 
u32 skip
Definition: lb.c:191
format_function_t format_ip46_address
Definition: format.h:61
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:396
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:158
dpo_lock_fn_t dv_lock
A reference counting lock function.
Definition: dpo.h:318
u32 lb_hash_time_now(vlib_main_t *vm)
Definition: lb.c:50
Recursive resolution source.
Definition: fib_entry.h:104
static int lb_pseudorand_compare(void *a, void *b)
Definition: lb.c:194
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:343
#define LB_GARBAGE_RUN
Definition: lb.c:21
#define CLIB_UNUSED(x)
Definition: clib.h:79
A virtual function table regisitered for a DPO type.
Definition: dpo.h:313
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
void ip46_prefix_normalize(ip46_address_t *prefix, u8 plen)
Definition: util.c:18
a
Definition: bitmap.h:516
format_function_t format_ip6_address
Definition: format.h:94
u32 last
Definition: lb.c:190
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
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:796
clib_error_t * vlib_plugin_register(vlib_main_t *vm, vnet_plugin_handoff_t *h, int from_early_init)
Definition: lb.c:734
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:550
static void lb_fib_node_last_lock_gone(fib_node_t *node)
Definition: lb.c:764
u64 as_u64[2]
Definition: ip6_packet.h:50
static void lb_vip_update_new_flow_table(lb_vip_t *vip)
Definition: lb.c:258
static int lb_as_find_index_vip(lb_vip_t *vip, ip46_address_t *address, u32 *as_index)
Definition: lb.c:407
#define NULL
Definition: clib.h:55
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
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:515
u8 * format_ip46_prefix(u8 *s, va_list *args)
Definition: util.c:54
static void lb_vip_del_adjacency(lb_main_t *lbm, lb_vip_t *vip)
Deletes the adjacency associated with the VIP.
Definition: lb.c:623
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:362
#define lb_get_writer_lock()
Definition: lb.c:28
int lb_vip_add_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: lb.c:425
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:482
ip46_address_t prefix
A Virtual IP represents a given service delivered by a set of application servers.
Definition: lb.h:184
#define clib_u32_loop_gt(a, b)
32 bits integer comparison for running values.
Definition: util.h:38
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:60
static void lb_dpo_lock(dpo_id_t *dpo)
Definition: lb.c:752
#define STRUCT_OFFSET_OF(t, f)
Definition: clib.h:62
static void lb_as_stack(lb_as_t *as)
Definition: lb.c:776
#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:200
u32 vip_index
ASs are indexed by address and VIP Index.
Definition: lb.h:74
format_function_t format_ip4_address
Definition: format.h:78
#define vec_alloc(V, N)
Allocate space for N more elements (no header, unspecified alignment)
Definition: vec.h:239
static const char *const *const lb_dpo_gre6_nodes[DPO_PROTO_NUM]
Definition: lb.c:44
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:121
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:78
#define LB_VIP_FLAGS_USED
Definition: lb.h:206
#define ip46_address_type(ip46)
Definition: util.h:26
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:533
u32 timeout
Definition: lbhash.h:57
#define LB_AS_FLAGS_USED
Definition: lb.h:82
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:348
#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:769
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:399
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:26
static const char *const lb_dpo_gre4_ip4[]
Definition: lb.c:34
u32 flow_timeout
Flow timeout in seconds.
Definition: lb.h:277
A high priority source a plugin can use.
Definition: fib_entry.h:53
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:149
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:594
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:203
u8 * format_lb_main(u8 *s, va_list *args)
Definition: lb.c:55
u8 * format_lb_vip(u8 *s, va_list *args)
Definition: lb.c:107
enum dpo_proto_t_ dpo_proto_t
Data path protocol.
u16 fp_len
The mask length.
Definition: fib_types.h:153
#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:233
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:215
char * name
The counter collection&#39;s name.
Definition: counter.h:68
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:138
Definition: fib_entry.h:219
#define lb_vip_is_gre4(vip)
Definition: lb.h:216
static const char *const lb_dpo_gre6_ip4[]
Definition: lb.c:42
#define ADJ_INDEX_INVALID
Invalid ADJ index - used when no adj is known likewise blazoned capitals INVALID speak volumes where ...
Definition: adj_types.h:36
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
static uword format_get_indent(u8 *s)
Definition: format.h:72
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, adj_index_t adj_index)
Add a &#39;special&#39; entry to the FIB that links to the adj passed A special entry is an entry that the FI...
Definition: fib_table.c:369
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:172
int lb_vip_del(u32 vip_index)
Definition: lb.c:696
u8 * format_lb_vip_type(u8 *s, va_list *args)
Definition: lb.c:85
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:214
uword unformat_lb_vip_type(unformat_input_t *input, va_list *args)
Definition: lb.c:95
#define LB_DEFAULT_FLOW_TIMEOUT
Definition: lb.h:44
static const char *const *const lb_dpo_gre4_nodes[DPO_PROTO_NUM]
Definition: lb.c:36
An node in the FIB graph.
Definition: fib_node.h:242
Definition: lb.h:114
ip46_type_t
Definition: format.h:63
fib_node_t fib_node
Registration to FIB event.
Definition: lb.h:59
static const char *const lb_dpo_gre6_ip6[]
Definition: lb.c:43
static const dpo_vft_t lb_vft
Definition: load_balance.c:718
#define ip46_prefix_is_ip4(ip46, len)
Definition: util.h:27
#define pool_free(p)
Free a pool.
Definition: pool.h:263
int lb_vip_add(ip46_address_t *prefix, u8 plen, lb_vip_type_t type, u32 new_length, u32 *vip_index)
Definition: lb.c:638
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
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:230
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:28
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:788
int lb_vip_del_ass(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: lb.c:583
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:134
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:160
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:288
void vlib_validate_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
validate a simple counter
Definition: counter.c:98
#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
int lb_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index)
Definition: lb.c:398
u8 * format_lb_as(u8 *s, va_list *args)
Definition: lb.c:118
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:143
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:753
static uword is_pow2(uword x)
Definition: clib.h:266
u32 as_index
Definition: lb.c:189
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:35
#define DPO_PROTO_NUM
Definition: dpo.h:72
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:126
#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:756
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:920
#define DPO_INVALID
An initialiser for DPos declared on the stack.
Definition: dpo.h:164
void lb_garbage_collection()
Definition: lb.c:233
static int lb_vip_find_index_with_lock(ip46_address_t *prefix, u8 plen, u32 *vip_index)
Definition: lb.c:380
u32 next_hop_child_index
The child index on the FIB entry.
Definition: lb.h:103
static u64 vlib_get_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
Get the value of a simple counter Scrapes the entire set of mini counters.
Definition: counter.h:108
A FIB graph nodes virtual function table.
Definition: fib_node.h:229
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:117
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
void dpo_reset(dpo_id_t *dpo)
reset a DPO ID The DPO will be unlocked.
Definition: dpo.c:171
#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:150
struct _unformat_input_t unformat_input_t
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:78
u8 * format_lb_dpo(u8 *s, va_list *va)
Definition: lb.c:743
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:62
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:373
#define lb_put_writer_lock()
Definition: lb.c:29
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:203
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:109