2 * Copyright (c) 2015 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.
16 * ip/ip_lookup.c: ip4/6 adjacency and lookup table managment
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include <vppinfra/math.h> /* for fabs */
41 #include <vnet/ip/ip.h>
42 #include <vnet/ip/adj_alloc.h>
45 ip_multipath_del_adjacency (ip_lookup_main_t * lm, u32 del_adj_index);
48 ip_poison_adjacencies (ip_adjacency_t * adj, uword n_adj)
52 u32 save_handle = adj->heap_handle;;
53 u32 save_n_adj = adj->n_adj;
55 memset (adj, 0xfe, n_adj * sizeof (adj[0]));
57 adj->heap_handle = save_handle;
58 adj->n_adj = save_n_adj;
63 ip_share_adjacency(ip_lookup_main_t * lm, u32 adj_index)
65 ip_adjacency_t * adj = ip_get_adjacency(lm, adj_index);
68 uword signature = vnet_ip_adjacency_signature (adj);
70 p = hash_get (lm->adj_index_by_signature, signature);
74 /* Save the adj index, p[0] will be toast after the unset! */
76 hash_unset (lm->adj_index_by_signature, signature);
77 hash_set (lm->adj_index_by_signature, signature, adj_index);
78 adj->next_adj_with_signature = old_ai;
82 adj->next_adj_with_signature = 0;
83 hash_set (lm->adj_index_by_signature, signature, adj_index);
88 ip_unshare_adjacency(ip_lookup_main_t * lm, u32 adj_index)
90 ip_adjacency_t * adj = ip_get_adjacency(lm, adj_index);
94 ip_adjacency_t * this_adj, * prev_adj = 0;
96 signature = vnet_ip_adjacency_signature (adj);
97 p = hash_get (lm->adj_index_by_signature, signature);
102 /* At the top of the signature chain (likely)? */
103 if (this_ai == adj_index)
105 if (adj->next_adj_with_signature == 0)
107 hash_unset (lm->adj_index_by_signature, signature);
112 this_adj = ip_get_adjacency (lm, adj->next_adj_with_signature);
113 hash_unset (lm->adj_index_by_signature, signature);
114 hash_set (lm->adj_index_by_signature, signature,
115 this_adj->heap_handle);
118 else /* walk signature chain */
120 this_adj = ip_get_adjacency (lm, this_ai);
121 while (this_adj != adj)
124 this_adj = ip_get_adjacency
125 (lm, this_adj->next_adj_with_signature);
127 * This can happen when creating the first multipath adj of a set
128 * We end up looking at the miss adjacency (handle==0).
130 if (this_adj->heap_handle == 0)
133 prev_adj->next_adj_with_signature = this_adj->next_adj_with_signature;
137 int ip_register_adjacency(vlib_main_t *vm,
139 ip_adj_register_t *reg)
141 ip_lookup_main_t *lm = (is_ip4)?&ip4_main.lookup_main:&ip6_main.lookup_main;
142 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) ((is_ip4)?"ip4-lookup":"ip6-lookup"));
143 vlib_node_t *next_node = vlib_get_node_by_name(vm, (u8 *) reg->node_name);
144 *reg->next_index = vlib_node_add_next (vm, node->index, next_node->index);
145 vec_validate(lm->registered_adjacencies, *reg->next_index);
146 lm->registered_adjacencies[*reg->next_index] = *reg;
150 int ip_init_registered_adjacencies(u8 is_ip4)
152 vlib_main_t *vm = vlib_get_main();
153 ip_lookup_main_t *lm = (is_ip4)?&ip4_main.lookup_main:&ip6_main.lookup_main;
154 ip_adj_register_t *reg = lm->registered_adjacencies;
155 lm->registered_adjacencies = 0; //Init vector
158 if((rv = ip_register_adjacency(vm, is_ip4, reg)))
165 /* Create new block of given number of contiguous adjacencies. */
167 ip_add_adjacency (ip_lookup_main_t * lm,
168 ip_adjacency_t * copy_adj,
170 u32 * adj_index_return)
172 ip_adjacency_t * adj;
175 /* See if we know enough to attempt to share an existing adjacency */
176 if (copy_adj && n_adj == 1)
181 switch (copy_adj->lookup_next_index)
183 case IP_LOOKUP_NEXT_DROP:
184 if (lm->drop_adj_index)
186 adj = ip_get_adjacency (lm, lm->drop_adj_index);
187 *adj_index_return = lm->drop_adj_index;
192 case IP_LOOKUP_NEXT_LOCAL:
193 if (lm->local_adj_index)
195 adj = ip_get_adjacency (lm, lm->local_adj_index);
196 *adj_index_return = lm->local_adj_index;
203 signature = vnet_ip_adjacency_signature (copy_adj);
204 p = hash_get (lm->adj_index_by_signature, signature);
207 adj = vec_elt_at_index (lm->adjacency_heap, p[0]);
210 if (vnet_ip_adjacency_share_compare (adj, copy_adj))
213 *adj_index_return = p[0];
216 if (adj->next_adj_with_signature == 0)
218 adj = vec_elt_at_index (lm->adjacency_heap,
219 adj->next_adj_with_signature);
224 lm->adjacency_heap = aa_alloc (lm->adjacency_heap, &adj, n_adj);
225 handle = ai = adj->heap_handle;
227 ip_poison_adjacencies (adj, n_adj);
229 /* Validate adjacency counters. */
230 vlib_validate_combined_counter (&lm->adjacency_counters, ai + n_adj - 1);
232 for (i = 0; i < n_adj; i++)
234 /* Make sure certain fields are always initialized. */
235 adj[i].rewrite_header.sw_if_index = ~0;
236 adj[i].explicit_fib_index = ~0;
237 adj[i].mcast_group_index = ~0;
238 adj[i].classify.table_index = ~0;
239 adj[i].saved_lookup_next_index = 0;
240 adj[i].special_adjacency_format_function_index = 0;
243 adj[i] = copy_adj[i];
245 adj[i].heap_handle = handle;
246 adj[i].n_adj = n_adj;
247 adj[i].share_count = 0;
248 adj[i].next_adj_with_signature = 0;
250 /* Zero possibly stale counters for re-used adjacencies. */
251 vlib_zero_combined_counter (&lm->adjacency_counters, ai + i);
254 /* Set up to share the adj later */
255 if (copy_adj && n_adj == 1)
256 ip_share_adjacency(lm, ai);
258 *adj_index_return = ai;
263 ip_update_adjacency (ip_lookup_main_t * lm,
265 ip_adjacency_t * copy_adj)
267 ip_adjacency_t * adj = ip_get_adjacency(lm, adj_index);
269 ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 1);
270 ip_unshare_adjacency(lm, adj_index);
272 /* temporary redirect to drop while updating rewrite data */
273 adj->lookup_next_index = IP_LOOKUP_NEXT_ARP;
274 CLIB_MEMORY_BARRIER();
276 clib_memcpy (&adj->rewrite_header, ©_adj->rewrite_header,
277 VLIB_BUFFER_PRE_DATA_SIZE);
278 adj->lookup_next_index = copy_adj->lookup_next_index;
279 ip_share_adjacency(lm, adj_index);
280 ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 0);
283 static void ip_del_adjacency2 (ip_lookup_main_t * lm, u32 adj_index, u32 delete_multipath_adjacency)
285 ip_adjacency_t * adj;
287 ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 1);
289 adj = ip_get_adjacency (lm, adj_index);
291 /* Special-case miss, local, drop adjs */
297 if (adj->share_count > 0)
303 ip_unshare_adjacency(lm, adj_index);
306 if (delete_multipath_adjacency)
307 ip_multipath_del_adjacency (lm, adj_index);
309 ip_poison_adjacencies (adj, adj->n_adj);
311 aa_free (lm->adjacency_heap, adj);
314 void ip_del_adjacency (ip_lookup_main_t * lm, u32 adj_index)
315 { ip_del_adjacency2 (lm, adj_index, /* delete_multipath_adjacency */ 1); }
318 next_hop_sort_by_weight (ip_multipath_next_hop_t * n1,
319 ip_multipath_next_hop_t * n2)
321 int cmp = (int) n1->weight - (int) n2->weight;
323 ? (int) n1->next_hop_adj_index - (int) n2->next_hop_adj_index
324 : (cmp > 0 ? +1 : -1));
327 /* Given next hop vector is over-written with normalized one with sorted weights and
328 with weights corresponding to the number of adjacencies for each next hop.
329 Returns number of adjacencies in block. */
330 static u32 ip_multipath_normalize_next_hops (ip_lookup_main_t * lm,
331 ip_multipath_next_hop_t * raw_next_hops,
332 ip_multipath_next_hop_t ** normalized_next_hops)
334 ip_multipath_next_hop_t * nhs;
335 uword n_nhs, n_adj, n_adj_left, i;
336 f64 sum_weight, norm, error;
338 n_nhs = vec_len (raw_next_hops);
343 /* Allocate enough space for 2 copies; we'll use second copy to save original weights. */
344 nhs = *normalized_next_hops;
345 vec_validate (nhs, 2*n_nhs - 1);
347 /* Fast path: 1 next hop in block. */
351 nhs[0] = raw_next_hops[0];
359 int cmp = next_hop_sort_by_weight (&raw_next_hops[0], &raw_next_hops[1]) < 0;
362 nhs[0] = raw_next_hops[cmp];
363 nhs[1] = raw_next_hops[cmp ^ 1];
365 /* Fast path: equal cost multipath with 2 next hops. */
366 if (nhs[0].weight == nhs[1].weight)
368 nhs[0].weight = nhs[1].weight = 1;
375 clib_memcpy (nhs, raw_next_hops, n_nhs * sizeof (raw_next_hops[0]));
376 qsort (nhs, n_nhs, sizeof (nhs[0]), (void *) next_hop_sort_by_weight);
379 /* Find total weight to normalize weights. */
381 for (i = 0; i < n_nhs; i++)
382 sum_weight += nhs[i].weight;
384 /* In the unlikely case that all weights are given as 0, set them all to 1. */
387 for (i = 0; i < n_nhs; i++)
392 /* Save copies of all next hop weights to avoid being overwritten in loop below. */
393 for (i = 0; i < n_nhs; i++)
394 nhs[n_nhs + i].weight = nhs[i].weight;
396 /* Try larger and larger power of 2 sized adjacency blocks until we
397 find one where traffic flows to within 1% of specified weights. */
398 for (n_adj = max_pow2 (n_nhs); ; n_adj *= 2)
402 norm = n_adj / sum_weight;
404 for (i = 0; i < n_nhs; i++)
406 f64 nf = nhs[n_nhs + i].weight * norm; /* use saved weights */
407 word n = flt_round_nearest (nf);
409 n = n > n_adj_left ? n_adj_left : n;
411 error += fabs (nf - n);
415 nhs[0].weight += n_adj_left;
417 /* Less than 5% average error per adjacency with this size adjacency block? */
418 if (error <= lm->multipath_next_hop_error_tolerance*n_adj)
420 /* Truncate any next hops with zero weight. */
427 /* Save vector for next call. */
428 *normalized_next_hops = nhs;
433 ip_next_hop_hash_key_from_handle (uword handle)
434 { return 1 + 2*handle; }
437 ip_next_hop_hash_key_is_heap_handle (uword k)
441 ip_next_hop_hash_key_get_heap_handle (uword k)
443 ASSERT (ip_next_hop_hash_key_is_heap_handle (k));
448 ip_multipath_adjacency_get (ip_lookup_main_t * lm,
449 ip_multipath_next_hop_t * raw_next_hops,
450 uword create_if_non_existent)
453 u32 i, j, n_adj, adj_index, adj_heap_handle;
454 ip_adjacency_t * adj, * copy_adj;
455 ip_multipath_next_hop_t * nh, * nhs;
456 ip_multipath_adjacency_t * madj;
458 n_adj = ip_multipath_normalize_next_hops (lm, raw_next_hops, &lm->next_hop_hash_lookup_key_normalized);
459 nhs = lm->next_hop_hash_lookup_key_normalized;
462 ASSERT (n_adj >= vec_len (raw_next_hops));
464 /* Use normalized next hops to see if we've seen a block equivalent to this one before. */
465 p = hash_get_mem (lm->multipath_adjacency_by_next_hops, nhs);
469 if (! create_if_non_existent)
472 adj = ip_add_adjacency (lm, /* copy_adj */ 0, n_adj, &adj_index);
473 adj_heap_handle = adj[0].heap_handle;
475 /* Fill in adjacencies in block based on corresponding next hop adjacencies. */
477 vec_foreach (nh, nhs)
479 copy_adj = ip_get_adjacency (lm, nh->next_hop_adj_index);
480 for (j = 0; j < nh->weight; j++)
482 adj[i] = copy_adj[0];
483 adj[i].heap_handle = adj_heap_handle;
484 adj[i].n_adj = n_adj;
489 /* All adjacencies should have been initialized. */
492 vec_validate (lm->multipath_adjacencies, adj_heap_handle);
493 madj = vec_elt_at_index (lm->multipath_adjacencies, adj_heap_handle);
495 madj->adj_index = adj_index;
496 madj->n_adj_in_block = n_adj;
497 madj->reference_count = 0; /* caller will set to one. */
499 madj->normalized_next_hops.count = vec_len (nhs);
500 madj->normalized_next_hops.heap_offset
501 = heap_alloc (lm->next_hop_heap, vec_len (nhs),
502 madj->normalized_next_hops.heap_handle);
503 clib_memcpy (lm->next_hop_heap + madj->normalized_next_hops.heap_offset,
504 nhs, vec_bytes (nhs));
506 hash_set (lm->multipath_adjacency_by_next_hops,
507 ip_next_hop_hash_key_from_handle (madj->normalized_next_hops.heap_handle),
508 madj - lm->multipath_adjacencies);
510 madj->unnormalized_next_hops.count = vec_len (raw_next_hops);
511 madj->unnormalized_next_hops.heap_offset
512 = heap_alloc (lm->next_hop_heap, vec_len (raw_next_hops),
513 madj->unnormalized_next_hops.heap_handle);
514 clib_memcpy (lm->next_hop_heap + madj->unnormalized_next_hops.heap_offset,
515 raw_next_hops, vec_bytes (raw_next_hops));
517 ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 0);
519 return adj_heap_handle;
522 /* Returns 0 for next hop not found. */
524 ip_multipath_adjacency_add_del_next_hop (ip_lookup_main_t * lm,
526 u32 old_mp_adj_index,
527 u32 next_hop_adj_index,
529 u32 * new_mp_adj_index)
531 ip_multipath_adjacency_t * mp_old, * mp_new;
532 ip_multipath_next_hop_t * nh, * nhs, * hash_nhs;
540 /* If old adj is not multipath, we need to "convert" it by calling this
541 * function recursively */
542 if (old_mp_adj_index != ~0 && !ip_adjacency_is_multipath(lm, old_mp_adj_index))
544 ip_multipath_adjacency_add_del_next_hop(lm, /* is_del */ 0,
545 /* old_mp_adj_index */ ~0,
546 /* nh_adj_index */ old_mp_adj_index,
551 /* If old multipath adjacency is valid, find requested next hop. */
552 if (old_mp_adj_index < vec_len (lm->multipath_adjacencies)
553 && lm->multipath_adjacencies[old_mp_adj_index].normalized_next_hops.count > 0)
555 mp_old = vec_elt_at_index (lm->multipath_adjacencies, old_mp_adj_index);
557 nhs = vec_elt_at_index (lm->next_hop_heap, mp_old->unnormalized_next_hops.heap_offset);
558 n_nhs = mp_old->unnormalized_next_hops.count;
560 /* Linear search: ok since n_next_hops is small. */
561 for (i_nh = 0; i_nh < n_nhs; i_nh++)
562 if (nhs[i_nh].next_hop_adj_index == next_hop_adj_index)
565 /* Given next hop not found. */
566 if (i_nh >= n_nhs && is_del)
570 hash_nhs = lm->next_hop_hash_lookup_key;
572 _vec_len (hash_nhs) = 0;
578 /* Prepare lookup key for multipath with target next hop deleted. */
580 vec_add (hash_nhs, nhs + 0, i_nh);
581 if (i_nh + 1 < n_nhs)
582 vec_add (hash_nhs, nhs + i_nh + 1, n_nhs - (i_nh + 1));
585 else /* it's an add. */
587 /* If next hop is already there with the same weight, we have nothing to do. */
588 if (i_nh < n_nhs && nhs[i_nh].weight == next_hop_weight)
590 new_mp_adj_index[0] = ~0;
594 /* Copy old next hops to lookup key vector. */
596 vec_add (hash_nhs, nhs, n_nhs);
600 /* Change weight of existing next hop. */
601 nh = vec_elt_at_index (hash_nhs, i_nh);
605 /* Add a new next hop. */
606 vec_add2 (hash_nhs, nh, 1);
607 nh->next_hop_adj_index = next_hop_adj_index;
610 /* Set weight for added or old next hop. */
611 nh->weight = next_hop_weight;
614 if (vec_len (hash_nhs) > 0)
616 u32 tmp = ip_multipath_adjacency_get (lm, hash_nhs,
617 /* create_if_non_existent */ 1);
619 mp_new = vec_elt_at_index (lm->multipath_adjacencies, tmp);
621 /* Fetch again since pool may have moved. */
623 mp_old = vec_elt_at_index (lm->multipath_adjacencies, old_mp_adj_index);
626 new_mp_adj_index[0] = mp_new ? mp_new - lm->multipath_adjacencies : ~0;
628 if (mp_new != mp_old)
632 ASSERT (mp_old->reference_count > 0);
633 mp_old->reference_count -= 1;
636 mp_new->reference_count += 1;
639 if (mp_old && mp_old->reference_count == 0)
640 ip_multipath_adjacency_free (lm, mp_old);
643 /* Save key vector next call. */
644 lm->next_hop_hash_lookup_key = hash_nhs;
650 ip_multipath_del_adjacency (ip_lookup_main_t * lm, u32 del_adj_index)
652 ip_adjacency_t * adj = ip_get_adjacency (lm, del_adj_index);
653 ip_multipath_adjacency_t * madj, * new_madj;
654 ip_multipath_next_hop_t * nhs, * hash_nhs;
655 u32 i, n_nhs, madj_index, new_madj_index;
657 if (adj->heap_handle >= vec_len (lm->multipath_adjacencies))
660 vec_validate (lm->adjacency_remap_table, vec_len (lm->adjacency_heap) - 1);
662 for (madj_index = 0; madj_index < vec_len (lm->multipath_adjacencies); madj_index++)
664 madj = vec_elt_at_index (lm->multipath_adjacencies, madj_index);
665 if (madj->n_adj_in_block == 0)
668 nhs = heap_elt_at_index (lm->next_hop_heap, madj->unnormalized_next_hops.heap_offset);
669 n_nhs = madj->unnormalized_next_hops.count;
670 for (i = 0; i < n_nhs; i++)
671 if (nhs[i].next_hop_adj_index == del_adj_index)
674 /* del_adj_index not found in unnormalized_next_hops? We're done. */
681 hash_nhs = lm->next_hop_hash_lookup_key;
683 _vec_len (hash_nhs) = 0;
685 vec_add (hash_nhs, nhs + 0, i);
687 vec_add (hash_nhs, nhs + i + 1, n_nhs - (i + 1));
689 new_madj_index = ip_multipath_adjacency_get (lm, hash_nhs, /* create_if_non_existent */ 1);
691 lm->next_hop_hash_lookup_key = hash_nhs;
693 if (new_madj_index == madj_index)
696 new_madj = vec_elt_at_index (lm->multipath_adjacencies, new_madj_index);
699 lm->adjacency_remap_table[madj->adj_index] = new_madj ? 1 + new_madj->adj_index : ~0;
700 lm->n_adjacency_remaps += 1;
701 ip_multipath_adjacency_free (lm, madj);
706 ip_multipath_adjacency_free (ip_lookup_main_t * lm,
707 ip_multipath_adjacency_t * a)
709 hash_unset (lm->multipath_adjacency_by_next_hops,
710 ip_next_hop_hash_key_from_handle (a->normalized_next_hops.heap_handle));
711 heap_dealloc (lm->next_hop_heap, a->normalized_next_hops.heap_handle);
712 heap_dealloc (lm->next_hop_heap, a->unnormalized_next_hops.heap_handle);
714 ip_del_adjacency2 (lm, a->adj_index, a->reference_count == 0);
715 memset (a, 0, sizeof (a[0]));
718 always_inline ip_multipath_next_hop_t *
719 ip_next_hop_hash_key_get_next_hops (ip_lookup_main_t * lm, uword k,
722 ip_multipath_next_hop_t * nhs;
724 if (ip_next_hop_hash_key_is_heap_handle (k))
726 uword handle = ip_next_hop_hash_key_get_heap_handle (k);
727 nhs = heap_elt_with_handle (lm->next_hop_heap, handle);
728 n_nhs = heap_len (lm->next_hop_heap, handle);
732 nhs = uword_to_pointer (k, ip_multipath_next_hop_t *);
733 n_nhs = vec_len (nhs);
735 *n_next_hops = n_nhs;
740 ip_next_hop_hash_key_sum (hash_t * h, uword key0)
742 ip_lookup_main_t * lm = uword_to_pointer (h->user, ip_lookup_main_t *);
743 ip_multipath_next_hop_t * k0;
746 k0 = ip_next_hop_hash_key_get_next_hops (lm, key0, &n0);
747 return hash_memory (k0, n0 * sizeof (k0[0]), /* seed */ n0);
751 ip_next_hop_hash_key_equal (hash_t * h, uword key0, uword key1)
753 ip_lookup_main_t * lm = uword_to_pointer (h->user, ip_lookup_main_t *);
754 ip_multipath_next_hop_t * k0, * k1;
757 k0 = ip_next_hop_hash_key_get_next_hops (lm, key0, &n0);
758 k1 = ip_next_hop_hash_key_get_next_hops (lm, key1, &n1);
760 return n0 == n1 && ! memcmp (k0, k1, n0 * sizeof (k0[0]));
764 ip_interface_address_add_del (ip_lookup_main_t * lm,
769 u32 * result_if_address_index)
771 vnet_main_t * vnm = vnet_get_main();
772 ip_interface_address_t * a, * prev, * next;
773 uword * p = mhash_get (&lm->address_to_if_address_index, addr_fib);
775 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
776 a = p ? pool_elt_at_index (lm->if_address_pool, p[0]) : 0;
778 /* Verify given length. */
779 if ((a && (address_length != a->address_length)) || (address_length == 0))
781 vnm->api_errno = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
782 return clib_error_create
783 ( "%U wrong length (expected %d) for interface %U",
784 lm->format_address_and_length, addr_fib,
785 address_length, a? a->address_length : -1,
786 format_vnet_sw_if_index_name, vnm, sw_if_index);
793 vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, sw_if_index);
794 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
795 return clib_error_create ("%U not found for interface %U",
796 lm->format_address_and_length,
797 addr_fib, address_length,
798 format_vnet_sw_interface_name, vnm, si);
801 if (a->prev_this_sw_interface != ~0)
803 prev = pool_elt_at_index (lm->if_address_pool, a->prev_this_sw_interface);
804 prev->next_this_sw_interface = a->next_this_sw_interface;
806 if (a->next_this_sw_interface != ~0)
808 next = pool_elt_at_index (lm->if_address_pool, a->next_this_sw_interface);
809 next->prev_this_sw_interface = a->prev_this_sw_interface;
811 if(a->prev_this_sw_interface == ~0)
812 lm->if_address_pool_index_by_sw_if_index[sw_if_index] = a->next_this_sw_interface;
815 if ((a->next_this_sw_interface == ~0) && (a->prev_this_sw_interface == ~0))
816 lm->if_address_pool_index_by_sw_if_index[sw_if_index] = ~0;
818 mhash_unset (&lm->address_to_if_address_index, addr_fib,
820 pool_put (lm->if_address_pool, a);
822 if (result_if_address_index)
823 *result_if_address_index = ~0;
828 u32 pi; /* previous index */
830 u32 hi; /* head index */
832 pool_get (lm->if_address_pool, a);
833 memset (a, ~0, sizeof (a[0]));
834 ai = a - lm->if_address_pool;
836 hi = pi = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
838 while (pi != (u32)~0)
840 prev = pool_elt_at_index(lm->if_address_pool, pi);
841 pi = prev->next_this_sw_interface;
843 pi = prev ? prev - lm->if_address_pool : (u32)~0;
845 a->address_key = mhash_set (&lm->address_to_if_address_index,
846 addr_fib, ai, /* old_value */ 0);
847 a->address_length = address_length;
848 a->sw_if_index = sw_if_index;
850 a->prev_this_sw_interface = pi;
851 a->next_this_sw_interface = ~0;
853 prev->next_this_sw_interface = ai;
855 lm->if_address_pool_index_by_sw_if_index[sw_if_index] =
856 (hi != ~0) ? hi : ai;
857 if (result_if_address_index)
858 *result_if_address_index = ai;
862 if (result_if_address_index)
863 *result_if_address_index = a - lm->if_address_pool;
867 return /* no error */ 0;
870 void ip_lookup_init (ip_lookup_main_t * lm, u32 is_ip6)
872 ip_adjacency_t * adj;
873 ip_adjacency_t template_adj;
875 /* ensure that adjacency is cacheline aligned and sized */
876 ASSERT(STRUCT_OFFSET_OF(ip_adjacency_t, cacheline0) == 0);
877 ASSERT(STRUCT_OFFSET_OF(ip_adjacency_t, cacheline1) == CLIB_CACHE_LINE_BYTES);
879 lm->adj_index_by_signature = hash_create (0, sizeof (uword));
880 memset (&template_adj, 0, sizeof (template_adj));
882 /* Preallocate three "special" adjacencies */
883 lm->adjacency_heap = aa_bootstrap (0, 3 /* n=1 free items */);
885 /* Hand-craft special miss adjacency to use when nothing matches in the
886 routing table. Same for drop adjacency. */
887 adj = ip_add_adjacency (lm, /* template */ 0, /* n-adj */ 1,
888 &lm->miss_adj_index);
889 adj->lookup_next_index = IP_LOOKUP_NEXT_MISS;
890 ASSERT (lm->miss_adj_index == IP_LOOKUP_MISS_ADJ_INDEX);
892 /* Make the "drop" adj sharable */
893 template_adj.lookup_next_index = IP_LOOKUP_NEXT_DROP;
894 adj = ip_add_adjacency (lm, &template_adj, /* n-adj */ 1,
895 &lm->drop_adj_index);
897 /* Make the "local" adj sharable */
898 template_adj.lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
899 template_adj.if_address_index = ~0;
900 adj = ip_add_adjacency (lm, &template_adj, /* n-adj */ 1,
901 &lm->local_adj_index);
903 if (! lm->fib_result_n_bytes)
904 lm->fib_result_n_bytes = sizeof (uword);
906 lm->multipath_adjacency_by_next_hops
907 = hash_create2 (/* elts */ 0,
908 /* user */ pointer_to_uword (lm),
909 /* value_bytes */ sizeof (uword),
910 ip_next_hop_hash_key_sum,
911 ip_next_hop_hash_key_equal,
912 /* format pair/arg */
915 /* 1% max error tolerance for multipath. */
916 lm->multipath_next_hop_error_tolerance = .01;
921 lm->format_address_and_length = format_ip6_address_and_length;
922 mhash_init (&lm->address_to_if_address_index, sizeof (uword),
923 sizeof (ip6_address_fib_t));
927 lm->format_address_and_length = format_ip4_address_and_length;
928 mhash_init (&lm->address_to_if_address_index, sizeof (uword),
929 sizeof (ip4_address_fib_t));
935 /* Setup all IP protocols to be punted and builtin-unknown. */
936 for (i = 0; i < 256; i++)
938 lm->local_next_by_ip_protocol[i] = IP_LOCAL_NEXT_PUNT;
939 lm->builtin_protocol_by_ip_protocol[i] = IP_BUILTIN_PROTOCOL_UNKNOWN;
942 lm->local_next_by_ip_protocol[IP_PROTOCOL_UDP] = IP_LOCAL_NEXT_UDP_LOOKUP;
943 lm->local_next_by_ip_protocol[is_ip6 ? IP_PROTOCOL_ICMP6 : IP_PROTOCOL_ICMP] = IP_LOCAL_NEXT_ICMP;
944 lm->builtin_protocol_by_ip_protocol[IP_PROTOCOL_UDP] = IP_BUILTIN_PROTOCOL_UDP;
945 lm->builtin_protocol_by_ip_protocol[is_ip6 ? IP_PROTOCOL_ICMP6 : IP_PROTOCOL_ICMP] = IP_BUILTIN_PROTOCOL_ICMP;
948 ip_init_registered_adjacencies(!is_ip6);
951 u8 * format_ip_flow_hash_config (u8 * s, va_list * args)
953 u32 flow_hash_config = va_arg (*args, u32);
955 #define _(n,v) if (flow_hash_config & v) s = format (s, "%s ", #n);
956 foreach_flow_hash_bit;
962 u8 * format_ip_lookup_next (u8 * s, va_list * args)
964 ip_lookup_main_t * lm = va_arg (*args, ip_lookup_main_t *);
965 ip_lookup_next_t n = va_arg (*args, u32);
966 ip_adj_register_t *reg;
973 vec_validate(lm->registered_adjacencies, n);
974 reg = vec_elt_at_index(lm->registered_adjacencies, n);
975 if (reg->node_name) {
976 s = format (s, "%s:", reg->node_name);
980 case IP_LOOKUP_NEXT_MISS: t = "miss"; break;
981 case IP_LOOKUP_NEXT_DROP: t = "drop"; break;
982 case IP_LOOKUP_NEXT_PUNT: t = "punt"; break;
983 case IP_LOOKUP_NEXT_LOCAL: t = "local"; break;
984 case IP_LOOKUP_NEXT_ARP: t = "arp"; break;
985 case IP_LOOKUP_NEXT_CLASSIFY: t = "classify"; break;
986 case IP_LOOKUP_NEXT_MAP: t = "map"; break;
987 case IP_LOOKUP_NEXT_MAP_T: t = "map-t"; break;
988 case IP_LOOKUP_NEXT_INDIRECT: t="indirect"; break;
989 case IP_LOOKUP_NEXT_REWRITE:
994 vec_add (s, t, strlen (t));
999 static u8 * format_ip_interface_address (u8 * s, va_list * args)
1001 ip_lookup_main_t * lm = va_arg (*args, ip_lookup_main_t *);
1002 u32 if_address_index = va_arg (*args, u32);
1003 ip_interface_address_t * ia = pool_elt_at_index (lm->if_address_pool, if_address_index);
1004 void * a = ip_interface_address_get_address (lm, ia);
1007 return format (s, "%U", format_ip6_address_and_length, a, ia->address_length);
1009 return format (s, "%U", format_ip4_address_and_length, a, ia->address_length);
1012 u32 vnet_register_special_adjacency_format_function
1013 (ip_lookup_main_t * lm, format_function_t * fp)
1017 * Initialize the format function registration vector
1018 * Index 0 must be invalid, to avoid finding and fixing trivial bugs
1019 * all over the place
1021 if (vec_len (lm->special_adjacency_format_functions) == 0)
1023 vec_add1 (lm->special_adjacency_format_functions,
1024 (format_function_t *) 0);
1027 rv = vec_len (lm->special_adjacency_format_functions);
1028 vec_add1 (lm->special_adjacency_format_functions, fp);
1032 /** @brief Pretty print helper function for formatting specific adjacencies.
1033 @param s - input string to format
1034 @param args - other args passed to format function such as:
1039 u8 * format_ip_adjacency (u8 * s, va_list * args)
1041 vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
1042 ip_lookup_main_t * lm = va_arg (*args, ip_lookup_main_t *);
1043 u32 adj_index = va_arg (*args, u32);
1044 ip_adjacency_t * adj = ip_get_adjacency (lm, adj_index);
1045 ip_adj_register_t *reg;
1047 if (adj->lookup_next_index < vec_len (lm->registered_adjacencies))
1049 reg = vec_elt_at_index(lm->registered_adjacencies,
1050 adj->lookup_next_index);
1054 s = reg->fn(s, lm, adj);
1059 switch (adj->lookup_next_index)
1061 case IP_LOOKUP_NEXT_REWRITE:
1062 s = format (s, "%U",
1063 format_vnet_rewrite,
1064 vnm->vlib_main, &adj->rewrite_header,
1065 sizeof (adj->rewrite_data));
1068 case IP_LOOKUP_NEXT_ARP:
1069 if (adj->if_address_index != ~0)
1070 s = format (s, " %U", format_ip_interface_address, lm,
1071 adj->if_address_index);
1072 if (adj->arp.next_hop.ip6.as_u64[0] || adj->arp.next_hop.ip6.as_u64[1])
1073 s = format (s, " via %U", format_ip46_address,
1074 &adj->arp.next_hop, IP46_TYPE_ANY);
1076 case IP_LOOKUP_NEXT_LOCAL:
1077 if (adj->if_address_index != ~0)
1078 s = format (s, " %U", format_ip_interface_address, lm,
1079 adj->if_address_index);
1082 case IP_LOOKUP_NEXT_CLASSIFY:
1083 s = format (s, " table %d", adj->classify.table_index);
1085 case IP_LOOKUP_NEXT_INDIRECT:
1086 s = format (s, " via %U", format_ip46_address,
1087 &adj->indirect.next_hop, IP46_TYPE_ANY);
1091 s = format (s, " unknown %d", adj->lookup_next_index);
1096 if (adj->explicit_fib_index != ~0 && adj->explicit_fib_index != 0)
1097 s = format (s, " lookup fib index %d", adj->explicit_fib_index);
1098 if (adj->share_count > 0)
1099 s = format (s, " shared %d", adj->share_count + 1);
1100 if (adj->next_adj_with_signature)
1101 s = format (s, " next_adj_with_signature %d", adj->next_adj_with_signature);
1106 u8 * format_ip_adjacency_packet_data (u8 * s, va_list * args)
1108 vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
1109 ip_lookup_main_t * lm = va_arg (*args, ip_lookup_main_t *);
1110 u32 adj_index = va_arg (*args, u32);
1111 u8 * packet_data = va_arg (*args, u8 *);
1112 u32 n_packet_data_bytes = va_arg (*args, u32);
1113 ip_adjacency_t * adj = ip_get_adjacency (lm, adj_index);
1115 switch (adj->lookup_next_index)
1117 case IP_LOOKUP_NEXT_REWRITE:
1118 s = format (s, "%U",
1119 format_vnet_rewrite_header,
1120 vnm->vlib_main, &adj->rewrite_header, packet_data, n_packet_data_bytes);
1130 static uword unformat_ip_lookup_next (unformat_input_t * input, va_list * args)
1132 ip_lookup_next_t * result = va_arg (*args, ip_lookup_next_t *);
1135 if (unformat (input, "drop"))
1136 n = IP_LOOKUP_NEXT_DROP;
1138 else if (unformat (input, "punt"))
1139 n = IP_LOOKUP_NEXT_PUNT;
1141 else if (unformat (input, "local"))
1142 n = IP_LOOKUP_NEXT_LOCAL;
1144 else if (unformat (input, "arp"))
1145 n = IP_LOOKUP_NEXT_ARP;
1147 else if (unformat (input, "classify"))
1148 n = IP_LOOKUP_NEXT_CLASSIFY;
1157 static uword unformat_ip_adjacency (unformat_input_t * input, va_list * args)
1159 vlib_main_t * vm = va_arg (*args, vlib_main_t *);
1160 ip_adjacency_t * adj = va_arg (*args, ip_adjacency_t *);
1161 u32 node_index = va_arg (*args, u32);
1162 vnet_main_t * vnm = vnet_get_main();
1163 u32 sw_if_index, is_ip6;
1165 ip_lookup_next_t next;
1167 is_ip6 = node_index == ip6_rewrite_node.index;
1168 adj->rewrite_header.node_index = node_index;
1169 adj->explicit_fib_index = ~0;
1171 if (unformat (input, "arp %U %U",
1172 unformat_vnet_sw_interface, vnm, &sw_if_index,
1173 unformat_ip46_address, &a46, is_ip6?IP46_TYPE_IP6:IP46_TYPE_IP4))
1175 ip_lookup_main_t * lm = is_ip6 ? &ip6_main.lookup_main : &ip4_main.lookup_main;
1176 ip_adjacency_t * a_adj;
1180 adj_index = ip6_fib_lookup (&ip6_main, sw_if_index, &a46.ip6);
1182 adj_index = ip4_fib_lookup (&ip4_main, sw_if_index, &a46.ip4);
1184 a_adj = ip_get_adjacency (lm, adj_index);
1186 if (a_adj->rewrite_header.sw_if_index != sw_if_index)
1190 ip6_adjacency_set_interface_route (vnm, adj, sw_if_index, a_adj->if_address_index);
1192 ip4_adjacency_set_interface_route (vnm, adj, sw_if_index, a_adj->if_address_index);
1195 else if (unformat_user (input, unformat_ip_lookup_next, &next))
1197 adj->lookup_next_index = next;
1198 adj->if_address_index = ~0;
1199 if (next == IP_LOOKUP_NEXT_LOCAL)
1200 (void) unformat (input, "%d", &adj->if_address_index);
1201 else if (next == IP_LOOKUP_NEXT_CLASSIFY)
1203 if (!unformat (input, "%d", &adj->classify.table_index))
1205 clib_warning ("classify adj must specify table index");
1209 else if (next == IP_LOOKUP_NEXT_DROP)
1211 adj->rewrite_header.node_index = 0;
1215 else if (unformat_user (input,
1216 unformat_vnet_rewrite,
1217 vm, &adj->rewrite_header, sizeof (adj->rewrite_data)))
1218 adj->lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
1227 vnet_ip_route_cmd (vlib_main_t * vm, unformat_input_t * main_input, vlib_cli_command_t * cmd)
1229 vnet_main_t * vnm = vnet_get_main();
1230 clib_error_t * error = 0;
1231 u32 table_id, is_del;
1232 u32 weight, * weights = 0;
1233 u32 * table_ids = 0;
1234 u32 sw_if_index, * sw_if_indices = 0;
1235 ip4_address_t ip4_addr, * ip4_dst_addresses = 0, * ip4_via_next_hops = 0;
1236 ip6_address_t ip6_addr, * ip6_dst_addresses = 0, * ip6_via_next_hops = 0;
1237 u32 dst_address_length, * dst_address_lengths = 0;
1238 ip_adjacency_t parse_adj, * add_adj = 0;
1239 unformat_input_t _line_input, * line_input = &_line_input;
1247 /* Get a line of input. */
1248 if (! unformat_user (main_input, unformat_line_input, line_input))
1251 memset(&parse_adj, 0, sizeof (parse_adj));
1253 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1255 if (unformat (line_input, "table %d", &table_id))
1257 else if (unformat (line_input, "del"))
1259 else if (unformat (line_input, "add"))
1261 else if (unformat (line_input, "count %f", &count))
1264 else if (unformat (line_input, "%U/%d",
1265 unformat_ip4_address, &ip4_addr,
1266 &dst_address_length))
1268 vec_add1 (ip4_dst_addresses, ip4_addr);
1269 vec_add1 (dst_address_lengths, dst_address_length);
1272 else if (unformat (line_input, "%U/%d",
1273 unformat_ip6_address, &ip6_addr,
1274 &dst_address_length))
1276 vec_add1 (ip6_dst_addresses, ip6_addr);
1277 vec_add1 (dst_address_lengths, dst_address_length);
1280 else if (unformat (line_input, "via %U %U weight %u",
1281 unformat_ip4_address, &ip4_addr,
1282 unformat_vnet_sw_interface, vnm, &sw_if_index,
1285 vec_add1 (ip4_via_next_hops, ip4_addr);
1286 vec_add1 (sw_if_indices, sw_if_index);
1287 vec_add1 (weights, weight);
1288 vec_add1 (table_ids, (u32)~0);
1291 else if (unformat (line_input, "via %U %U weight %u",
1292 unformat_ip6_address, &ip6_addr,
1293 unformat_vnet_sw_interface, vnm, &sw_if_index,
1296 vec_add1 (ip6_via_next_hops, ip6_addr);
1297 vec_add1 (sw_if_indices, sw_if_index);
1298 vec_add1 (weights, weight);
1299 vec_add1 (table_ids, (u32)~0);
1302 else if (unformat (line_input, "via %U %U",
1303 unformat_ip4_address, &ip4_addr,
1304 unformat_vnet_sw_interface, vnm, &sw_if_index))
1306 vec_add1 (ip4_via_next_hops, ip4_addr);
1307 vec_add1 (sw_if_indices, sw_if_index);
1308 vec_add1 (weights, 1);
1309 vec_add1 (table_ids, (u32)~0);
1312 else if (unformat (line_input, "via %U %U",
1313 unformat_ip6_address, &ip6_addr,
1314 unformat_vnet_sw_interface, vnm, &sw_if_index))
1316 vec_add1 (ip6_via_next_hops, ip6_addr);
1317 vec_add1 (sw_if_indices, sw_if_index);
1318 vec_add1 (weights, 1);
1319 vec_add1 (table_ids, (u32)~0);
1321 else if (unformat (line_input, "via %U",
1322 unformat_ip4_address, &ip4_addr))
1324 vec_add1 (ip4_via_next_hops, ip4_addr);
1325 vec_add1 (sw_if_indices, (u32)~0);
1326 vec_add1 (weights, 1);
1327 vec_add1 (table_ids, table_id);
1329 else if (unformat (line_input, "via %U",
1330 unformat_ip6_address, &ip6_addr))
1332 vec_add1 (ip6_via_next_hops, ip6_addr);
1333 vec_add1 (sw_if_indices, (u32)~0);
1334 vec_add1 (weights, 1);
1335 vec_add1 (table_ids, (u32)table_id);
1338 else if (vec_len (ip4_dst_addresses) > 0
1339 && unformat (line_input, "via %U",
1340 unformat_ip_adjacency, vm, &parse_adj, ip4_rewrite_node.index))
1341 vec_add1 (add_adj, parse_adj);
1343 else if (vec_len (ip6_dst_addresses) > 0
1344 && unformat (line_input, "via %U",
1345 unformat_ip_adjacency, vm, &parse_adj, ip6_rewrite_node.index))
1346 vec_add1 (add_adj, parse_adj);
1347 else if (unformat (line_input, "lookup in table %d", &outer_table_id))
1351 if (vec_len (ip4_dst_addresses) > 0)
1352 p = hash_get (ip4_main.fib_index_by_table_id, outer_table_id);
1354 p = hash_get (ip6_main.fib_index_by_table_id, outer_table_id);
1358 error = clib_error_return (0, "Nonexistent outer table id %d",
1363 parse_adj.lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
1364 parse_adj.explicit_fib_index = p[0];
1365 vec_add1 (add_adj, parse_adj);
1369 error = unformat_parse_error (line_input);
1374 unformat_free (line_input);
1376 if (vec_len (ip4_dst_addresses) + vec_len (ip6_dst_addresses) == 0)
1378 error = clib_error_return (0, "expected ip4/ip6 destination address/length.");
1382 if (vec_len (ip4_dst_addresses) > 0 && vec_len (ip6_dst_addresses) > 0)
1384 error = clib_error_return (0, "mixed ip4/ip6 address/length.");
1388 if (vec_len (ip4_dst_addresses) > 0 && vec_len (ip6_via_next_hops) > 0)
1390 error = clib_error_return (0, "ip4 destinations with ip6 next hops.");
1394 if (vec_len (ip6_dst_addresses) > 0 && vec_len (ip4_via_next_hops) > 0)
1396 error = clib_error_return (0, "ip6 destinations with ip4 next hops.");
1400 if (! is_del && vec_len (add_adj) + vec_len (weights) == 0)
1402 error = clib_error_return (0, "no next hops or adjacencies to add.");
1408 ip4_main_t * im4 = &ip4_main;
1409 ip6_main_t * im6 = &ip6_main;
1411 for (i = 0; i < vec_len (ip4_dst_addresses); i++)
1413 ip4_add_del_route_args_t a;
1415 memset (&a, 0, sizeof (a));
1416 a.flags = IP4_ROUTE_FLAG_TABLE_ID;
1417 a.table_index_or_table_id = table_id;
1418 a.dst_address = ip4_dst_addresses[i];
1419 a.dst_address_length = dst_address_lengths[i];
1424 if (vec_len (ip4_via_next_hops) == 0)
1426 uword * dst_hash, * dst_result;
1427 u32 dst_address_u32;
1430 fib = find_ip4_fib_by_table_index_or_id (im4, table_id,
1431 0 /* by table id */);
1433 a.flags |= IP4_ROUTE_FLAG_DEL;
1434 dst_address_u32 = a.dst_address.as_u32
1435 & im4->fib_masks[a.dst_address_length];
1438 fib->adj_index_by_dst_address[a.dst_address_length];
1439 dst_result = hash_get (dst_hash, dst_address_u32);
1441 a.adj_index = dst_result[0];
1444 clib_warning ("%U/%d not in FIB",
1445 format_ip4_address, &a.dst_address,
1446 a.dst_address_length);
1450 ip4_add_del_route (im4, &a);
1451 ip4_maybe_remap_adjacencies (im4, table_id,
1452 IP4_ROUTE_FLAG_TABLE_ID);
1456 u32 i, j, n, f, incr;
1457 ip4_address_t dst = a.dst_address;
1460 t[0] = vlib_time_now (vm);
1461 incr = 1<<(32 - a.dst_address_length);
1462 for (i = 0; i < n; i++)
1464 f = i + 1 < n ? IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP : 0;
1465 a.dst_address = dst;
1466 for (j = 0; j < vec_len (ip4_via_next_hops); j++)
1468 if (table_ids[j] != (u32)~0)
1470 uword * p = hash_get (im4->fib_index_by_table_id,
1474 clib_warning ("no such FIB table %d",
1478 table_ids[j] = p[0];
1481 ip4_add_del_route_next_hop (im4,
1482 IP4_ROUTE_FLAG_DEL | f,
1484 a.dst_address_length,
1485 &ip4_via_next_hops[j],
1487 weights[j], (u32)~0,
1488 table_ids[j] /* fib index */);
1490 dst.as_u32 = clib_host_to_net_u32 (incr + clib_net_to_host_u32 (dst.as_u32));
1492 t[1] = vlib_time_now (vm);
1494 vlib_cli_output (vm, "%.6e routes/sec", count / (t[1] - t[0]));
1499 if (vec_len (add_adj) > 0)
1501 a.flags |= IP4_ROUTE_FLAG_ADD;
1502 a.add_adj = add_adj;
1503 a.n_add_adj = vec_len (add_adj);
1505 ip4_add_del_route (im4, &a);
1507 else if (vec_len (ip4_via_next_hops) > 0)
1509 u32 i, j, n, f, incr;
1510 ip4_address_t dst = a.dst_address;
1513 t[0] = vlib_time_now (vm);
1514 incr = 1<<(32 - a.dst_address_length);
1515 for (i = 0; i < n; i++)
1517 f = i + 1 < n ? IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP : 0;
1518 a.dst_address = dst;
1519 for (j = 0; j < vec_len (ip4_via_next_hops); j++)
1521 if (table_ids[j] != (u32)~0)
1523 uword * p = hash_get (im4->fib_index_by_table_id,
1527 clib_warning ("no such FIB table %d",
1531 table_ids[j] = p[0];
1533 ip4_add_del_route_next_hop (im4,
1534 IP4_ROUTE_FLAG_ADD | f,
1536 a.dst_address_length,
1537 &ip4_via_next_hops[j],
1539 weights[j], (u32)~0,
1540 table_ids[j] /* fib index */);
1542 dst.as_u32 = clib_host_to_net_u32 (incr + clib_net_to_host_u32 (dst.as_u32));
1544 t[1] = vlib_time_now (vm);
1546 vlib_cli_output (vm, "%.6e routes/sec", count / (t[1] - t[0]));
1551 for (i = 0; i < vec_len (ip6_dst_addresses); i++)
1553 ip6_add_del_route_args_t a;
1556 memset (&a, 0, sizeof (a));
1557 a.flags = IP6_ROUTE_FLAG_TABLE_ID;
1558 a.table_index_or_table_id = table_id;
1559 a.dst_address = ip6_dst_addresses[i];
1560 a.dst_address_length = dst_address_lengths[i];
1565 if (vec_len (ip6_via_next_hops) == 0)
1567 BVT(clib_bihash_kv) kv, value;
1568 ip6_address_t dst_address;
1571 fib = find_ip6_fib_by_table_index_or_id (im6, table_id,
1572 0 /* by table id */);
1574 a.flags |= IP4_ROUTE_FLAG_DEL;
1576 dst_address = ip6_dst_addresses[i];
1578 ip6_address_mask (&dst_address,
1579 &im6->fib_masks[dst_address_length]);
1581 kv.key[0] = dst_address.as_u64[0];
1582 kv.key[1] = dst_address.as_u64[1];
1583 kv.key[2] = ((u64)(fib - im6->fibs)<<32)
1584 | a.dst_address_length;
1586 if (BV(clib_bihash_search)(&im6->ip6_lookup_table,
1588 a.adj_index = value.value;
1591 clib_warning ("%U/%d not in FIB",
1592 format_ip6_address, &a.dst_address,
1593 a.dst_address_length);
1597 a.flags |= IP6_ROUTE_FLAG_DEL;
1598 ip6_add_del_route (im6, &a);
1599 ip6_maybe_remap_adjacencies (im6, table_id,
1600 IP6_ROUTE_FLAG_TABLE_ID);
1605 for (i = 0; i < vec_len (ip6_via_next_hops); i++)
1607 ip6_add_del_route_next_hop (im6,
1610 a.dst_address_length,
1611 &ip6_via_next_hops[i],
1613 weights[i], (u32)~0,
1614 table_ids[i] /* fib index */);
1620 if (vec_len (add_adj) > 0)
1622 a.flags |= IP6_ROUTE_FLAG_ADD;
1623 a.add_adj = add_adj;
1624 a.n_add_adj = vec_len (add_adj);
1626 ip6_add_del_route (im6, &a);
1628 else if (vec_len (ip6_via_next_hops) > 0)
1631 for (i = 0; i < vec_len (ip6_via_next_hops); i++)
1633 ip6_add_del_route_next_hop (im6,
1636 a.dst_address_length,
1637 &ip6_via_next_hops[i],
1639 weights[i], (u32)~0,
1650 vec_free (dst_address_lengths);
1651 vec_free (ip4_dst_addresses);
1652 vec_free (ip6_dst_addresses);
1653 vec_free (ip4_via_next_hops);
1654 vec_free (ip6_via_next_hops);
1658 VLIB_CLI_COMMAND (vlib_cli_ip_command, static) = {
1660 .short_help = "Internet protocol (IP) commands",
1663 VLIB_CLI_COMMAND (vlib_cli_show_ip_command, static) = {
1665 .short_help = "Internet protocol (IP) show commands",
1668 VLIB_CLI_COMMAND (vlib_cli_show_ip4_command, static) = {
1670 .short_help = "Internet protocol version 4 (IP4) show commands",
1673 VLIB_CLI_COMMAND (vlib_cli_show_ip6_command, static) = {
1675 .short_help = "Internet protocol version 6 (IP6) show commands",
1678 VLIB_CLI_COMMAND (ip_route_command, static) = {
1680 .short_help = "Add/delete IP routes",
1681 .function = vnet_ip_route_cmd,
1686 * The next two routines address a longstanding script hemorrhoid.
1687 * Probing a v4 or v6 neighbor needs to appear to be synchronous,
1688 * or dependent route-adds will simply fail.
1690 static clib_error_t *
1691 ip6_probe_neighbor_wait (vlib_main_t *vm, ip6_address_t * a, u32 sw_if_index,
1694 vnet_main_t * vnm = vnet_get_main();
1699 uword *event_data = 0;
1701 ASSERT (vlib_in_process_context(vm));
1703 if (retry_count > 0)
1704 vnet_register_ip6_neighbor_resolution_event
1705 (vnm, a, vlib_get_current_process (vm)->node_runtime.node_index,
1706 1 /* event */, 0 /* data */);
1708 for (i = 0; i < retry_count; i++)
1710 /* The interface may be down, etc. */
1711 e = ip6_probe_neighbor (vm, a, sw_if_index);
1716 vlib_process_wait_for_event_or_clock (vm, 1.0);
1717 event_type = vlib_process_get_events (vm, &event_data);
1720 case 1: /* resolved... */
1721 vlib_cli_output (vm, "Resolved %U",
1722 format_ip6_address, a);
1726 case ~0: /* timeout */
1730 clib_warning ("unknown event_type %d", event_type);
1732 vec_reset_length (event_data);
1738 return clib_error_return (0, "Resolution failed for %U",
1739 format_ip6_address, a);
1743 static clib_error_t *
1744 ip4_probe_neighbor_wait (vlib_main_t *vm, ip4_address_t * a, u32 sw_if_index,
1747 vnet_main_t * vnm = vnet_get_main();
1752 uword *event_data = 0;
1754 ASSERT (vlib_in_process_context(vm));
1756 if (retry_count > 0)
1757 vnet_register_ip4_arp_resolution_event
1758 (vnm, a, vlib_get_current_process (vm)->node_runtime.node_index,
1759 1 /* event */, 0 /* data */);
1761 for (i = 0; i < retry_count; i++)
1763 /* The interface may be down, etc. */
1764 e = ip4_probe_neighbor (vm, a, sw_if_index);
1769 vlib_process_wait_for_event_or_clock (vm, 1.0);
1770 event_type = vlib_process_get_events (vm, &event_data);
1773 case 1: /* resolved... */
1774 vlib_cli_output (vm, "Resolved %U",
1775 format_ip4_address, a);
1779 case ~0: /* timeout */
1783 clib_warning ("unknown event_type %d", event_type);
1785 vec_reset_length (event_data);
1790 vec_reset_length (event_data);
1793 return clib_error_return (0, "Resolution failed for %U",
1794 format_ip4_address, a);
1798 static clib_error_t *
1799 probe_neighbor_address (vlib_main_t * vm,
1800 unformat_input_t * input,
1801 vlib_cli_command_t * cmd)
1803 vnet_main_t * vnm = vnet_get_main();
1804 unformat_input_t _line_input, * line_input = &_line_input;
1807 clib_error_t * error = 0;
1808 u32 sw_if_index = ~0;
1809 int retry_count = 3;
1811 int address_set = 0;
1813 /* Get a line of input. */
1814 if (! unformat_user (input, unformat_line_input, line_input))
1817 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1819 if (unformat_user (line_input, unformat_vnet_sw_interface, vnm,
1822 else if (unformat (line_input, "retry %d", &retry_count))
1825 else if (unformat (line_input, "%U", unformat_ip4_address, &a4))
1827 else if (unformat (line_input, "%U", unformat_ip6_address, &a6))
1833 return clib_error_return (0, "unknown input '%U'",
1834 format_unformat_error, line_input);
1837 unformat_free (line_input);
1839 if (sw_if_index == ~0)
1840 return clib_error_return (0, "Interface required, not set.");
1841 if (address_set == 0)
1842 return clib_error_return (0, "ip address required, not set.");
1843 if (address_set > 1)
1844 return clib_error_return (0, "Multiple ip addresses not supported.");
1847 error = ip4_probe_neighbor_wait (vm, &a4, sw_if_index, retry_count);
1849 error = ip6_probe_neighbor_wait (vm, &a6, sw_if_index, retry_count);
1854 VLIB_CLI_COMMAND (ip_probe_neighbor_command, static) = {
1855 .path = "ip probe-neighbor",
1856 .function = probe_neighbor_address,
1857 .short_help = "ip probe-neighbor <intfc> <ip4-addr> | <ip6-addr> [retry nn]",
1861 typedef CLIB_PACKED (struct {
1862 ip4_address_t address;
1864 u32 address_length : 6;
1870 ip4_route_cmp (void * a1, void * a2)
1872 ip4_route_t * r1 = a1;
1873 ip4_route_t * r2 = a2;
1875 int cmp = ip4_address_compare (&r1->address, &r2->address);
1876 return cmp ? cmp : ((int) r1->address_length - (int) r2->address_length);
1879 static clib_error_t *
1880 ip4_show_fib (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1882 vnet_main_t * vnm = vnet_get_main();
1883 ip4_main_t * im4 = &ip4_main;
1884 ip4_route_t * routes, * r;
1886 ip_lookup_main_t * lm = &im4->lookup_main;
1888 int verbose, matching, mtrie, include_empty_fibs;
1889 ip4_address_t matching_address;
1896 include_empty_fibs = 0;
1899 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1901 if (unformat (input, "brief") || unformat (input, "summary")
1902 || unformat (input, "sum"))
1905 else if (unformat (input, "mtrie"))
1908 else if (unformat (input, "include-empty"))
1909 include_empty_fibs = 1;
1911 else if (unformat (input, "%U", unformat_ip4_address, &matching_address))
1914 else if (unformat (input, "clear"))
1917 else if (unformat (input, "table %d", &table_id))
1923 vec_foreach (fib, im4->fibs)
1928 for (i = 0; i < ARRAY_LEN (fib->adj_index_by_dst_address); i++)
1930 uword * hash = fib->adj_index_by_dst_address[i];
1931 uword n_elts = hash_elts (hash);
1939 if (fib_not_empty == 0 && include_empty_fibs == 0)
1942 if (table_id >= 0 && table_id != (int)fib->table_id)
1945 if (include_empty_fibs)
1946 vlib_cli_output (vm, "Table %d, fib_index %d, flow hash: %U",
1947 fib->table_id, fib - im4->fibs,
1948 format_ip_flow_hash_config, fib->flow_hash_config);
1953 if (include_empty_fibs == 0)
1954 vlib_cli_output (vm, "Table %d, fib_index %d, flow hash: %U",
1955 fib->table_id, fib - im4->fibs,
1956 format_ip_flow_hash_config, fib->flow_hash_config);
1957 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
1958 for (i = 0; i < ARRAY_LEN (fib->adj_index_by_dst_address); i++)
1960 uword * hash = fib->adj_index_by_dst_address[i];
1961 uword n_elts = hash_elts (hash);
1963 vlib_cli_output (vm, "%20d%16d", i, n_elts);
1969 _vec_len (routes) = 0;
1971 _vec_len (results) = 0;
1973 for (i = 0; i < ARRAY_LEN (fib->adj_index_by_dst_address); i++)
1975 uword * hash = fib->adj_index_by_dst_address[i];
1979 x.address_length = i;
1983 x.address.as_u32 = matching_address.as_u32 & im4->fib_masks[i];
1984 p = hash_get_pair (hash, x.address.as_u32);
1987 if (lm->fib_result_n_words > 1)
1989 x.index = vec_len (results);
1990 vec_add (results, p->value, lm->fib_result_n_words);
1993 x.index = p->value[0];
1994 vec_add1 (routes, x);
1999 hash_foreach_pair (p, hash, ({
2000 x.address.data_u32 = p->key;
2001 if (lm->fib_result_n_words > 1)
2003 x.index = vec_len (results);
2004 vec_add (results, p->value, lm->fib_result_n_words);
2007 x.index = p->value[0];
2009 vec_add1 (routes, x);
2014 vec_sort_with_function (routes, ip4_route_cmp);
2015 if (vec_len(routes)) {
2016 if (include_empty_fibs == 0)
2017 vlib_cli_output (vm, "Table %d, fib_index %d, flow hash: %U",
2018 fib->table_id, fib - im4->fibs,
2019 format_ip_flow_hash_config, fib->flow_hash_config);
2021 vlib_cli_output (vm, "%U", format_ip4_fib_mtrie, &fib->mtrie);
2022 vlib_cli_output (vm, "%=20s%=16s%=16s%=16s",
2023 "Destination", "Packets", "Bytes", "Adjacency");
2025 vec_foreach (r, routes)
2027 vlib_counter_t c, sum;
2028 uword i, j, n_left, n_nhs, adj_index, * result = 0;
2029 ip_adjacency_t * adj;
2030 ip_multipath_next_hop_t * nhs, tmp_nhs[1];
2032 adj_index = r->index;
2033 if (lm->fib_result_n_words > 1)
2035 result = vec_elt_at_index (results, adj_index);
2036 adj_index = result[0];
2039 adj = ip_get_adjacency (lm, adj_index);
2040 if (adj->n_adj == 1)
2043 nhs[0].next_hop_adj_index = ~0; /* not used */
2049 ip_multipath_adjacency_t * madj;
2050 madj = vec_elt_at_index (lm->multipath_adjacencies, adj->heap_handle);
2051 nhs = heap_elt_at_index (lm->next_hop_heap, madj->normalized_next_hops.heap_offset);
2052 n_nhs = madj->normalized_next_hops.count;
2055 n_left = nhs[0].weight;
2056 vlib_counter_zero (&sum);
2057 for (i = j = 0; i < adj->n_adj; i++)
2060 vlib_get_combined_counter (&lm->adjacency_counters,
2063 vlib_zero_combined_counter (&lm->adjacency_counters,
2065 vlib_counter_add (&sum, &c);
2072 msg = format (msg, "%-20U",
2073 format_ip4_address_and_length,
2074 r->address.data, r->address_length);
2076 msg = format (msg, "%U", format_white_space, 20);
2078 msg = format (msg, "%16Ld%16Ld ", sum.packets, sum.bytes);
2080 indent = vec_len (msg);
2081 msg = format (msg, "weight %d, index %d",
2082 nhs[j].weight, adj_index + i);
2084 if (ip_adjacency_is_multipath(lm, adj_index))
2085 msg = format (msg, ", multipath");
2087 msg = format (msg, "\n%U%U",
2088 format_white_space, indent,
2089 format_ip_adjacency,
2090 vnm, lm, adj_index + i);
2092 vlib_cli_output (vm, "%v", msg);
2095 if (result && lm->format_fib_result)
2096 vlib_cli_output (vm, "%20s%U", "",
2097 lm->format_fib_result, vm, lm, result,
2098 i + 1 - nhs[j].weight,
2104 n_left = nhs[j].weight;
2105 vlib_counter_zero (&sum);
2118 VLIB_CLI_COMMAND (ip4_show_fib_command, static) = {
2119 .path = "show ip fib",
2120 .short_help = "show ip fib [mtrie] [summary] [table <n>] [<ip4-addr>] [clear] [include-empty]",
2121 .function = ip4_show_fib,
2125 ip6_address_t address;
2134 ip6_route_t ** routep;
2135 } add_routes_in_fib_arg_t;
2137 static void add_routes_in_fib (BVT(clib_bihash_kv) * kvp, void *arg)
2139 add_routes_in_fib_arg_t * ap = arg;
2141 if (kvp->key[2]>>32 == ap->fib_index)
2143 ip6_address_t *addr;
2145 addr = (ip6_address_t *) kvp;
2146 vec_add2 (*ap->routep, r, 1);
2147 r->address = addr[0];
2148 r->address_length = kvp->key[2] & 0xFF;
2149 r->index = kvp->value;
2155 u64 count_by_prefix_length[129];
2156 } count_routes_in_fib_at_prefix_length_arg_t;
2158 static void count_routes_in_fib_at_prefix_length
2159 (BVT(clib_bihash_kv) * kvp, void *arg)
2161 count_routes_in_fib_at_prefix_length_arg_t * ap = arg;
2164 if ((kvp->key[2]>>32) != ap->fib_index)
2167 mask_width = kvp->key[2] & 0xFF;
2169 ap->count_by_prefix_length[mask_width]++;
2173 ip6_route_cmp (void * a1, void * a2)
2175 ip6_route_t * r1 = a1;
2176 ip6_route_t * r2 = a2;
2178 int cmp = ip6_address_compare (&r1->address, &r2->address);
2179 return cmp ? cmp : ((int) r1->address_length - (int) r2->address_length);
2182 static clib_error_t *
2183 ip6_show_fib (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
2185 vnet_main_t * vnm = vnet_get_main();
2186 ip6_main_t * im6 = &ip6_main;
2187 ip6_route_t * routes, * r;
2189 ip_lookup_main_t * lm = &im6->lookup_main;
2192 BVT(clib_bihash) * h = &im6->ip6_lookup_table;
2193 __attribute__((unused)) u8 clear = 0;
2194 add_routes_in_fib_arg_t _a, *a=&_a;
2195 count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca;
2200 if (unformat (input, "brief") || unformat (input, "summary")
2201 || unformat (input, "sum"))
2204 if (unformat (input, "clear"))
2207 vlib_cli_output (vm, "FIB lookup table: %d buckets, %lld MB heap",
2208 im6->lookup_table_nbuckets, im6->lookup_table_size>>20);
2209 vlib_cli_output (vm, "%U", format_mheap, h->mheap, 0 /*verbose*/);
2210 vlib_cli_output (vm, " ");
2212 vec_foreach (fib, im6->fibs)
2214 vlib_cli_output (vm, "VRF %d, fib_index %d, flow hash: %U",
2215 fib->table_id, fib - im6->fibs,
2216 format_ip_flow_hash_config, fib->flow_hash_config);
2222 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
2224 memset (ca, 0, sizeof(*ca));
2225 ca->fib_index = fib - im6->fibs;
2227 BV(clib_bihash_foreach_key_value_pair)
2228 (h, count_routes_in_fib_at_prefix_length, ca);
2230 for (len = 128; len >= 0; len--)
2232 if (ca->count_by_prefix_length[len])
2233 vlib_cli_output (vm, "%=20d%=16lld",
2234 len, ca->count_by_prefix_length[len]);
2240 _vec_len (routes) = 0;
2242 _vec_len (results) = 0;
2244 a->fib_index = fib - im6->fibs;
2245 a->routep = &routes;
2247 BV(clib_bihash_foreach_key_value_pair)(h, add_routes_in_fib, a);
2249 vec_sort_with_function (routes, ip6_route_cmp);
2251 vlib_cli_output (vm, "%=45s%=16s%=16s%=16s",
2252 "Destination", "Packets", "Bytes", "Adjacency");
2253 vec_foreach (r, routes)
2255 vlib_counter_t c, sum;
2256 uword i, j, n_left, n_nhs, adj_index, * result = 0;
2257 ip_adjacency_t * adj;
2258 ip_multipath_next_hop_t * nhs, tmp_nhs[1];
2260 adj_index = r->index;
2261 if (lm->fib_result_n_words > 1)
2263 result = vec_elt_at_index (results, adj_index);
2264 adj_index = result[0];
2267 adj = ip_get_adjacency (lm, adj_index);
2268 if (adj->n_adj == 1)
2271 nhs[0].next_hop_adj_index = ~0; /* not used */
2277 ip_multipath_adjacency_t * madj;
2278 madj = vec_elt_at_index (lm->multipath_adjacencies, adj->heap_handle);
2279 nhs = heap_elt_at_index (lm->next_hop_heap, madj->normalized_next_hops.heap_offset);
2280 n_nhs = madj->normalized_next_hops.count;
2283 n_left = nhs[0].weight;
2284 vlib_counter_zero (&sum);
2285 for (i = j = 0; i < adj->n_adj; i++)
2288 vlib_get_combined_counter (&lm->adjacency_counters,
2291 vlib_zero_combined_counter (&lm->adjacency_counters,
2293 vlib_counter_add (&sum, &c);
2300 msg = format (msg, "%-45U",
2301 format_ip6_address_and_length,
2302 r->address.as_u8, r->address_length);
2304 msg = format (msg, "%U", format_white_space, 20);
2306 msg = format (msg, "%16Ld%16Ld ", sum.packets, sum.bytes);
2308 indent = vec_len (msg);
2309 msg = format (msg, "weight %d, index %d",
2310 nhs[j].weight, adj_index + i);
2312 if (ip_adjacency_is_multipath(lm, adj_index + i))
2313 msg = format (msg, ", multipath");
2315 msg = format (msg, "\n%U%U",
2316 format_white_space, indent,
2317 format_ip_adjacency,
2318 vnm, lm, adj_index + i);
2320 vlib_cli_output (vm, "%v", msg);
2326 n_left = nhs[j].weight;
2327 vlib_counter_zero (&sum);
2332 if (result && lm->format_fib_result)
2333 vlib_cli_output (vm, "%20s%U", "", lm->format_fib_result, vm, lm, result, 0);
2335 vlib_cli_output (vm, " ");
2344 VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
2345 .path = "show ip6 fib",
2346 .short_help = "show ip6 fib [summary] [clear]",
2347 .function = ip6_show_fib,