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 /* Create new block of given number of contiguous adjacencies. */
139 ip_add_adjacency (ip_lookup_main_t * lm,
140 ip_adjacency_t * copy_adj,
142 u32 * adj_index_return)
144 ip_adjacency_t * adj;
147 /* See if we know enough to attempt to share an existing adjacency */
148 if (copy_adj && n_adj == 1)
153 switch (copy_adj->lookup_next_index)
155 case IP_LOOKUP_NEXT_DROP:
156 if (lm->drop_adj_index)
158 adj = ip_get_adjacency (lm, lm->drop_adj_index);
159 *adj_index_return = lm->drop_adj_index;
164 case IP_LOOKUP_NEXT_LOCAL:
165 if (lm->local_adj_index)
167 adj = ip_get_adjacency (lm, lm->local_adj_index);
168 *adj_index_return = lm->local_adj_index;
175 signature = vnet_ip_adjacency_signature (copy_adj);
176 p = hash_get (lm->adj_index_by_signature, signature);
179 adj = vec_elt_at_index (lm->adjacency_heap, p[0]);
182 if (vnet_ip_adjacency_share_compare (adj, copy_adj))
185 *adj_index_return = p[0];
188 if (adj->next_adj_with_signature == 0)
190 adj = vec_elt_at_index (lm->adjacency_heap,
191 adj->next_adj_with_signature);
196 lm->adjacency_heap = aa_alloc (lm->adjacency_heap, &adj, n_adj);
197 handle = ai = adj->heap_handle;
199 ip_poison_adjacencies (adj, n_adj);
201 /* Validate adjacency counters. */
202 vlib_validate_combined_counter (&lm->adjacency_counters, ai + n_adj - 1);
204 for (i = 0; i < n_adj; i++)
206 /* Make sure certain fields are always initialized. */
207 adj[i].rewrite_header.sw_if_index = ~0;
208 adj[i].explicit_fib_index = ~0;
209 adj[i].mcast_group_index = ~0;
210 adj[i].classify.table_index = ~0;
211 adj[i].saved_lookup_next_index = 0;
214 adj[i] = copy_adj[i];
216 adj[i].heap_handle = handle;
217 adj[i].n_adj = n_adj;
218 adj[i].share_count = 0;
219 adj[i].next_adj_with_signature = 0;
221 /* Zero possibly stale counters for re-used adjacencies. */
222 vlib_zero_combined_counter (&lm->adjacency_counters, ai + i);
225 /* Set up to share the adj later */
226 if (copy_adj && n_adj == 1)
227 ip_share_adjacency(lm, ai);
229 *adj_index_return = ai;
234 ip_update_adjacency (ip_lookup_main_t * lm,
236 ip_adjacency_t * copy_adj)
238 ip_adjacency_t * adj = ip_get_adjacency(lm, adj_index);
240 ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 1);
241 ip_unshare_adjacency(lm, adj_index);
243 /* temporary redirect to drop while updating rewrite data */
244 adj->lookup_next_index = IP_LOOKUP_NEXT_ARP;
245 CLIB_MEMORY_BARRIER();
247 clib_memcpy (&adj->rewrite_header, ©_adj->rewrite_header,
248 VLIB_BUFFER_PRE_DATA_SIZE);
249 adj->lookup_next_index = copy_adj->lookup_next_index;
250 ip_share_adjacency(lm, adj_index);
251 ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 0);
254 static void ip_del_adjacency2 (ip_lookup_main_t * lm, u32 adj_index, u32 delete_multipath_adjacency)
256 ip_adjacency_t * adj;
258 ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 1);
260 adj = ip_get_adjacency (lm, adj_index);
262 /* Special-case miss, local, drop adjs */
268 if (adj->share_count > 0)
274 ip_unshare_adjacency(lm, adj_index);
277 if (delete_multipath_adjacency)
278 ip_multipath_del_adjacency (lm, adj_index);
280 ip_poison_adjacencies (adj, adj->n_adj);
282 aa_free (lm->adjacency_heap, adj);
285 void ip_del_adjacency (ip_lookup_main_t * lm, u32 adj_index)
286 { ip_del_adjacency2 (lm, adj_index, /* delete_multipath_adjacency */ 1); }
289 next_hop_sort_by_weight (ip_multipath_next_hop_t * n1,
290 ip_multipath_next_hop_t * n2)
292 int cmp = (int) n1->weight - (int) n2->weight;
294 ? (int) n1->next_hop_adj_index - (int) n2->next_hop_adj_index
295 : (cmp > 0 ? +1 : -1));
298 /* Given next hop vector is over-written with normalized one with sorted weights and
299 with weights corresponding to the number of adjacencies for each next hop.
300 Returns number of adjacencies in block. */
301 static u32 ip_multipath_normalize_next_hops (ip_lookup_main_t * lm,
302 ip_multipath_next_hop_t * raw_next_hops,
303 ip_multipath_next_hop_t ** normalized_next_hops)
305 ip_multipath_next_hop_t * nhs;
306 uword n_nhs, n_adj, n_adj_left, i;
307 f64 sum_weight, norm, error;
309 n_nhs = vec_len (raw_next_hops);
314 /* Allocate enough space for 2 copies; we'll use second copy to save original weights. */
315 nhs = *normalized_next_hops;
316 vec_validate (nhs, 2*n_nhs - 1);
318 /* Fast path: 1 next hop in block. */
322 nhs[0] = raw_next_hops[0];
330 int cmp = next_hop_sort_by_weight (&raw_next_hops[0], &raw_next_hops[1]) < 0;
333 nhs[0] = raw_next_hops[cmp];
334 nhs[1] = raw_next_hops[cmp ^ 1];
336 /* Fast path: equal cost multipath with 2 next hops. */
337 if (nhs[0].weight == nhs[1].weight)
339 nhs[0].weight = nhs[1].weight = 1;
346 clib_memcpy (nhs, raw_next_hops, n_nhs * sizeof (raw_next_hops[0]));
347 qsort (nhs, n_nhs, sizeof (nhs[0]), (void *) next_hop_sort_by_weight);
350 /* Find total weight to normalize weights. */
352 for (i = 0; i < n_nhs; i++)
353 sum_weight += nhs[i].weight;
355 /* In the unlikely case that all weights are given as 0, set them all to 1. */
358 for (i = 0; i < n_nhs; i++)
363 /* Save copies of all next hop weights to avoid being overwritten in loop below. */
364 for (i = 0; i < n_nhs; i++)
365 nhs[n_nhs + i].weight = nhs[i].weight;
367 /* Try larger and larger power of 2 sized adjacency blocks until we
368 find one where traffic flows to within 1% of specified weights. */
369 for (n_adj = max_pow2 (n_nhs); ; n_adj *= 2)
373 norm = n_adj / sum_weight;
375 for (i = 0; i < n_nhs; i++)
377 f64 nf = nhs[n_nhs + i].weight * norm; /* use saved weights */
378 word n = flt_round_nearest (nf);
380 n = n > n_adj_left ? n_adj_left : n;
382 error += fabs (nf - n);
386 nhs[0].weight += n_adj_left;
388 /* Less than 5% average error per adjacency with this size adjacency block? */
389 if (error <= lm->multipath_next_hop_error_tolerance*n_adj)
391 /* Truncate any next hops with zero weight. */
398 /* Save vector for next call. */
399 *normalized_next_hops = nhs;
404 ip_next_hop_hash_key_from_handle (uword handle)
405 { return 1 + 2*handle; }
408 ip_next_hop_hash_key_is_heap_handle (uword k)
412 ip_next_hop_hash_key_get_heap_handle (uword k)
414 ASSERT (ip_next_hop_hash_key_is_heap_handle (k));
419 ip_multipath_adjacency_get (ip_lookup_main_t * lm,
420 ip_multipath_next_hop_t * raw_next_hops,
421 uword create_if_non_existent)
424 u32 i, j, n_adj, adj_index, adj_heap_handle;
425 ip_adjacency_t * adj, * copy_adj;
426 ip_multipath_next_hop_t * nh, * nhs;
427 ip_multipath_adjacency_t * madj;
429 n_adj = ip_multipath_normalize_next_hops (lm, raw_next_hops, &lm->next_hop_hash_lookup_key_normalized);
430 nhs = lm->next_hop_hash_lookup_key_normalized;
433 ASSERT (n_adj >= vec_len (raw_next_hops));
435 /* Use normalized next hops to see if we've seen a block equivalent to this one before. */
436 p = hash_get_mem (lm->multipath_adjacency_by_next_hops, nhs);
440 if (! create_if_non_existent)
443 adj = ip_add_adjacency (lm, /* copy_adj */ 0, n_adj, &adj_index);
444 adj_heap_handle = adj[0].heap_handle;
446 /* Fill in adjacencies in block based on corresponding next hop adjacencies. */
448 vec_foreach (nh, nhs)
450 copy_adj = ip_get_adjacency (lm, nh->next_hop_adj_index);
451 for (j = 0; j < nh->weight; j++)
453 adj[i] = copy_adj[0];
454 adj[i].heap_handle = adj_heap_handle;
455 adj[i].n_adj = n_adj;
460 /* All adjacencies should have been initialized. */
463 vec_validate (lm->multipath_adjacencies, adj_heap_handle);
464 madj = vec_elt_at_index (lm->multipath_adjacencies, adj_heap_handle);
466 madj->adj_index = adj_index;
467 madj->n_adj_in_block = n_adj;
468 madj->reference_count = 0; /* caller will set to one. */
470 madj->normalized_next_hops.count = vec_len (nhs);
471 madj->normalized_next_hops.heap_offset
472 = heap_alloc (lm->next_hop_heap, vec_len (nhs),
473 madj->normalized_next_hops.heap_handle);
474 clib_memcpy (lm->next_hop_heap + madj->normalized_next_hops.heap_offset,
475 nhs, vec_bytes (nhs));
477 hash_set (lm->multipath_adjacency_by_next_hops,
478 ip_next_hop_hash_key_from_handle (madj->normalized_next_hops.heap_handle),
479 madj - lm->multipath_adjacencies);
481 madj->unnormalized_next_hops.count = vec_len (raw_next_hops);
482 madj->unnormalized_next_hops.heap_offset
483 = heap_alloc (lm->next_hop_heap, vec_len (raw_next_hops),
484 madj->unnormalized_next_hops.heap_handle);
485 clib_memcpy (lm->next_hop_heap + madj->unnormalized_next_hops.heap_offset,
486 raw_next_hops, vec_bytes (raw_next_hops));
488 ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 0);
490 return adj_heap_handle;
493 /* Returns 0 for next hop not found. */
495 ip_multipath_adjacency_add_del_next_hop (ip_lookup_main_t * lm,
497 u32 old_mp_adj_index,
498 u32 next_hop_adj_index,
500 u32 * new_mp_adj_index)
502 ip_multipath_adjacency_t * mp_old, * mp_new;
503 ip_multipath_next_hop_t * nh, * nhs, * hash_nhs;
511 /* If old adj is not multipath, we need to "convert" it by calling this
512 * function recursively */
513 if (old_mp_adj_index != ~0 && !ip_adjacency_is_multipath(lm, old_mp_adj_index))
515 ip_multipath_adjacency_add_del_next_hop(lm, /* is_del */ 0,
516 /* old_mp_adj_index */ ~0,
517 /* nh_adj_index */ old_mp_adj_index,
522 /* If old multipath adjacency is valid, find requested next hop. */
523 if (old_mp_adj_index < vec_len (lm->multipath_adjacencies)
524 && lm->multipath_adjacencies[old_mp_adj_index].normalized_next_hops.count > 0)
526 mp_old = vec_elt_at_index (lm->multipath_adjacencies, old_mp_adj_index);
528 nhs = vec_elt_at_index (lm->next_hop_heap, mp_old->unnormalized_next_hops.heap_offset);
529 n_nhs = mp_old->unnormalized_next_hops.count;
531 /* Linear search: ok since n_next_hops is small. */
532 for (i_nh = 0; i_nh < n_nhs; i_nh++)
533 if (nhs[i_nh].next_hop_adj_index == next_hop_adj_index)
536 /* Given next hop not found. */
537 if (i_nh >= n_nhs && is_del)
541 hash_nhs = lm->next_hop_hash_lookup_key;
543 _vec_len (hash_nhs) = 0;
549 /* Prepare lookup key for multipath with target next hop deleted. */
551 vec_add (hash_nhs, nhs + 0, i_nh);
552 if (i_nh + 1 < n_nhs)
553 vec_add (hash_nhs, nhs + i_nh + 1, n_nhs - (i_nh + 1));
556 else /* it's an add. */
558 /* If next hop is already there with the same weight, we have nothing to do. */
559 if (i_nh < n_nhs && nhs[i_nh].weight == next_hop_weight)
561 new_mp_adj_index[0] = ~0;
565 /* Copy old next hops to lookup key vector. */
567 vec_add (hash_nhs, nhs, n_nhs);
571 /* Change weight of existing next hop. */
572 nh = vec_elt_at_index (hash_nhs, i_nh);
576 /* Add a new next hop. */
577 vec_add2 (hash_nhs, nh, 1);
578 nh->next_hop_adj_index = next_hop_adj_index;
581 /* Set weight for added or old next hop. */
582 nh->weight = next_hop_weight;
585 if (vec_len (hash_nhs) > 0)
587 u32 tmp = ip_multipath_adjacency_get (lm, hash_nhs,
588 /* create_if_non_existent */ 1);
590 mp_new = vec_elt_at_index (lm->multipath_adjacencies, tmp);
592 /* Fetch again since pool may have moved. */
594 mp_old = vec_elt_at_index (lm->multipath_adjacencies, old_mp_adj_index);
597 new_mp_adj_index[0] = mp_new ? mp_new - lm->multipath_adjacencies : ~0;
599 if (mp_new != mp_old)
603 ASSERT (mp_old->reference_count > 0);
604 mp_old->reference_count -= 1;
607 mp_new->reference_count += 1;
610 if (mp_old && mp_old->reference_count == 0)
611 ip_multipath_adjacency_free (lm, mp_old);
614 /* Save key vector next call. */
615 lm->next_hop_hash_lookup_key = hash_nhs;
621 ip_multipath_del_adjacency (ip_lookup_main_t * lm, u32 del_adj_index)
623 ip_adjacency_t * adj = ip_get_adjacency (lm, del_adj_index);
624 ip_multipath_adjacency_t * madj, * new_madj;
625 ip_multipath_next_hop_t * nhs, * hash_nhs;
626 u32 i, n_nhs, madj_index, new_madj_index;
628 if (adj->heap_handle >= vec_len (lm->multipath_adjacencies))
631 vec_validate (lm->adjacency_remap_table, vec_len (lm->adjacency_heap) - 1);
633 for (madj_index = 0; madj_index < vec_len (lm->multipath_adjacencies); madj_index++)
635 madj = vec_elt_at_index (lm->multipath_adjacencies, madj_index);
636 if (madj->n_adj_in_block == 0)
639 nhs = heap_elt_at_index (lm->next_hop_heap, madj->unnormalized_next_hops.heap_offset);
640 n_nhs = madj->unnormalized_next_hops.count;
641 for (i = 0; i < n_nhs; i++)
642 if (nhs[i].next_hop_adj_index == del_adj_index)
645 /* del_adj_index not found in unnormalized_next_hops? We're done. */
652 hash_nhs = lm->next_hop_hash_lookup_key;
654 _vec_len (hash_nhs) = 0;
656 vec_add (hash_nhs, nhs + 0, i);
658 vec_add (hash_nhs, nhs + i + 1, n_nhs - (i + 1));
660 new_madj_index = ip_multipath_adjacency_get (lm, hash_nhs, /* create_if_non_existent */ 1);
662 lm->next_hop_hash_lookup_key = hash_nhs;
664 if (new_madj_index == madj_index)
667 new_madj = vec_elt_at_index (lm->multipath_adjacencies, new_madj_index);
670 lm->adjacency_remap_table[madj->adj_index] = new_madj ? 1 + new_madj->adj_index : ~0;
671 lm->n_adjacency_remaps += 1;
672 ip_multipath_adjacency_free (lm, madj);
677 ip_multipath_adjacency_free (ip_lookup_main_t * lm,
678 ip_multipath_adjacency_t * a)
680 hash_unset (lm->multipath_adjacency_by_next_hops,
681 ip_next_hop_hash_key_from_handle (a->normalized_next_hops.heap_handle));
682 heap_dealloc (lm->next_hop_heap, a->normalized_next_hops.heap_handle);
683 heap_dealloc (lm->next_hop_heap, a->unnormalized_next_hops.heap_handle);
685 ip_del_adjacency2 (lm, a->adj_index, a->reference_count == 0);
686 memset (a, 0, sizeof (a[0]));
689 always_inline ip_multipath_next_hop_t *
690 ip_next_hop_hash_key_get_next_hops (ip_lookup_main_t * lm, uword k,
693 ip_multipath_next_hop_t * nhs;
695 if (ip_next_hop_hash_key_is_heap_handle (k))
697 uword handle = ip_next_hop_hash_key_get_heap_handle (k);
698 nhs = heap_elt_with_handle (lm->next_hop_heap, handle);
699 n_nhs = heap_len (lm->next_hop_heap, handle);
703 nhs = uword_to_pointer (k, ip_multipath_next_hop_t *);
704 n_nhs = vec_len (nhs);
706 *n_next_hops = n_nhs;
711 ip_next_hop_hash_key_sum (hash_t * h, uword key0)
713 ip_lookup_main_t * lm = uword_to_pointer (h->user, ip_lookup_main_t *);
714 ip_multipath_next_hop_t * k0;
717 k0 = ip_next_hop_hash_key_get_next_hops (lm, key0, &n0);
718 return hash_memory (k0, n0 * sizeof (k0[0]), /* seed */ n0);
722 ip_next_hop_hash_key_equal (hash_t * h, uword key0, uword key1)
724 ip_lookup_main_t * lm = uword_to_pointer (h->user, ip_lookup_main_t *);
725 ip_multipath_next_hop_t * k0, * k1;
728 k0 = ip_next_hop_hash_key_get_next_hops (lm, key0, &n0);
729 k1 = ip_next_hop_hash_key_get_next_hops (lm, key1, &n1);
731 return n0 == n1 && ! memcmp (k0, k1, n0 * sizeof (k0[0]));
735 ip_interface_address_add_del (ip_lookup_main_t * lm,
740 u32 * result_if_address_index)
742 vnet_main_t * vnm = vnet_get_main();
743 ip_interface_address_t * a, * prev, * next;
744 uword * p = mhash_get (&lm->address_to_if_address_index, addr_fib);
746 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
747 a = p ? pool_elt_at_index (lm->if_address_pool, p[0]) : 0;
749 /* Verify given length. */
750 if ((a && (address_length != a->address_length)) || (address_length == 0))
752 vnm->api_errno = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
753 return clib_error_create
754 ( "%U wrong length (expected %d) for interface %U",
755 lm->format_address_and_length, addr_fib,
756 address_length, a? a->address_length : -1,
757 format_vnet_sw_if_index_name, vnm, sw_if_index);
764 vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, sw_if_index);
765 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
766 return clib_error_create ("%U not found for interface %U",
767 lm->format_address_and_length,
768 addr_fib, address_length,
769 format_vnet_sw_interface_name, vnm, si);
772 if (a->prev_this_sw_interface != ~0)
774 prev = pool_elt_at_index (lm->if_address_pool, a->prev_this_sw_interface);
775 prev->next_this_sw_interface = a->next_this_sw_interface;
777 if (a->next_this_sw_interface != ~0)
779 next = pool_elt_at_index (lm->if_address_pool, a->next_this_sw_interface);
780 next->prev_this_sw_interface = a->prev_this_sw_interface;
782 if(a->prev_this_sw_interface == ~0)
783 lm->if_address_pool_index_by_sw_if_index[sw_if_index] = a->next_this_sw_interface;
786 if ((a->next_this_sw_interface == ~0) && (a->prev_this_sw_interface == ~0))
787 lm->if_address_pool_index_by_sw_if_index[sw_if_index] = ~0;
789 mhash_unset (&lm->address_to_if_address_index, addr_fib,
791 pool_put (lm->if_address_pool, a);
793 if (result_if_address_index)
794 *result_if_address_index = ~0;
799 u32 pi; /* previous index */
801 u32 hi; /* head index */
803 pool_get (lm->if_address_pool, a);
804 memset (a, ~0, sizeof (a[0]));
805 ai = a - lm->if_address_pool;
807 hi = pi = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
809 while (pi != (u32)~0)
811 prev = pool_elt_at_index(lm->if_address_pool, pi);
812 pi = prev->next_this_sw_interface;
814 pi = prev ? prev - lm->if_address_pool : (u32)~0;
816 a->address_key = mhash_set (&lm->address_to_if_address_index,
817 addr_fib, ai, /* old_value */ 0);
818 a->address_length = address_length;
819 a->sw_if_index = sw_if_index;
821 a->prev_this_sw_interface = pi;
822 a->next_this_sw_interface = ~0;
824 prev->next_this_sw_interface = ai;
826 lm->if_address_pool_index_by_sw_if_index[sw_if_index] =
827 (hi != ~0) ? hi : ai;
828 if (result_if_address_index)
829 *result_if_address_index = ai;
833 if (result_if_address_index)
834 *result_if_address_index = a - lm->if_address_pool;
838 return /* no error */ 0;
841 void ip_lookup_init (ip_lookup_main_t * lm, u32 is_ip6)
843 ip_adjacency_t * adj;
844 ip_adjacency_t template_adj;
846 /* ensure that adjacency is cacheline aligned and sized */
847 ASSERT(STRUCT_OFFSET_OF(ip_adjacency_t, cacheline0) == 0);
848 ASSERT(STRUCT_OFFSET_OF(ip_adjacency_t, cacheline1) == CLIB_CACHE_LINE_BYTES);
850 lm->adj_index_by_signature = hash_create (0, sizeof (uword));
851 memset (&template_adj, 0, sizeof (template_adj));
853 /* Preallocate three "special" adjacencies */
854 lm->adjacency_heap = aa_bootstrap (0, 3 /* n=1 free items */);
856 /* Hand-craft special miss adjacency to use when nothing matches in the
857 routing table. Same for drop adjacency. */
858 adj = ip_add_adjacency (lm, /* template */ 0, /* n-adj */ 1,
859 &lm->miss_adj_index);
860 adj->lookup_next_index = IP_LOOKUP_NEXT_MISS;
861 ASSERT (lm->miss_adj_index == IP_LOOKUP_MISS_ADJ_INDEX);
863 /* Make the "drop" adj sharable */
864 template_adj.lookup_next_index = IP_LOOKUP_NEXT_DROP;
865 adj = ip_add_adjacency (lm, &template_adj, /* n-adj */ 1,
866 &lm->drop_adj_index);
868 /* Make the "local" adj sharable */
869 template_adj.lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
870 template_adj.if_address_index = ~0;
871 adj = ip_add_adjacency (lm, &template_adj, /* n-adj */ 1,
872 &lm->local_adj_index);
874 if (! lm->fib_result_n_bytes)
875 lm->fib_result_n_bytes = sizeof (uword);
877 lm->multipath_adjacency_by_next_hops
878 = hash_create2 (/* elts */ 0,
879 /* user */ pointer_to_uword (lm),
880 /* value_bytes */ sizeof (uword),
881 ip_next_hop_hash_key_sum,
882 ip_next_hop_hash_key_equal,
883 /* format pair/arg */
886 /* 1% max error tolerance for multipath. */
887 lm->multipath_next_hop_error_tolerance = .01;
892 lm->format_address_and_length = format_ip6_address_and_length;
893 mhash_init (&lm->address_to_if_address_index, sizeof (uword),
894 sizeof (ip6_address_fib_t));
898 lm->format_address_and_length = format_ip4_address_and_length;
899 mhash_init (&lm->address_to_if_address_index, sizeof (uword),
900 sizeof (ip4_address_fib_t));
906 /* Setup all IP protocols to be punted and builtin-unknown. */
907 for (i = 0; i < 256; i++)
909 lm->local_next_by_ip_protocol[i] = IP_LOCAL_NEXT_PUNT;
910 lm->builtin_protocol_by_ip_protocol[i] = IP_BUILTIN_PROTOCOL_UNKNOWN;
913 lm->local_next_by_ip_protocol[IP_PROTOCOL_UDP] = IP_LOCAL_NEXT_UDP_LOOKUP;
914 lm->local_next_by_ip_protocol[is_ip6 ? IP_PROTOCOL_ICMP6 : IP_PROTOCOL_ICMP] = IP_LOCAL_NEXT_ICMP;
915 lm->builtin_protocol_by_ip_protocol[IP_PROTOCOL_UDP] = IP_BUILTIN_PROTOCOL_UDP;
916 lm->builtin_protocol_by_ip_protocol[is_ip6 ? IP_PROTOCOL_ICMP6 : IP_PROTOCOL_ICMP] = IP_BUILTIN_PROTOCOL_ICMP;
920 u8 * format_ip_flow_hash_config (u8 * s, va_list * args)
922 u32 flow_hash_config = va_arg (*args, u32);
924 #define _(n,v) if (flow_hash_config & v) s = format (s, "%s ", #n);
925 foreach_flow_hash_bit;
931 u8 * format_ip_lookup_next (u8 * s, va_list * args)
933 ip_lookup_next_t n = va_arg (*args, ip_lookup_next_t);
939 s = format (s, "unknown %d", n);
942 case IP_LOOKUP_NEXT_MISS: t = "miss"; break;
943 case IP_LOOKUP_NEXT_DROP: t = "drop"; break;
944 case IP_LOOKUP_NEXT_PUNT: t = "punt"; break;
945 case IP_LOOKUP_NEXT_LOCAL: t = "local"; break;
946 case IP_LOOKUP_NEXT_ARP: t = "arp"; break;
947 case IP_LOOKUP_NEXT_CLASSIFY: t = "classify"; break;
948 case IP_LOOKUP_NEXT_MAP: t = "map"; break;
949 case IP_LOOKUP_NEXT_MAP_T: t = "map-t"; break;
950 case IP_LOOKUP_NEXT_INDIRECT: t="indirect"; break;
951 case IP_LOOKUP_NEXT_REWRITE:
956 vec_add (s, t, strlen (t));
961 static u8 * format_ip_interface_address (u8 * s, va_list * args)
963 ip_lookup_main_t * lm = va_arg (*args, ip_lookup_main_t *);
964 u32 if_address_index = va_arg (*args, u32);
965 ip_interface_address_t * ia = pool_elt_at_index (lm->if_address_pool, if_address_index);
966 void * a = ip_interface_address_get_address (lm, ia);
969 return format (s, "%U", format_ip6_address_and_length, a, ia->address_length);
971 return format (s, "%U", format_ip4_address_and_length, a, ia->address_length);
974 u8 * format_ip_adjacency (u8 * s, va_list * args)
976 vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
977 ip_lookup_main_t * lm = va_arg (*args, ip_lookup_main_t *);
978 u32 adj_index = va_arg (*args, u32);
979 ip_adjacency_t * adj = ip_get_adjacency (lm, adj_index);
981 switch (adj->lookup_next_index)
983 case IP_LOOKUP_NEXT_REWRITE:
986 vnm->vlib_main, &adj->rewrite_header, sizeof (adj->rewrite_data));
990 s = format (s, "%U", format_ip_lookup_next, adj->lookup_next_index);
991 if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP)
992 s = format (s, " %U",
993 format_vnet_sw_interface_name,
995 vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index));
996 switch (adj->lookup_next_index)
998 case IP_LOOKUP_NEXT_ARP:
999 if (adj->if_address_index != ~0)
1000 s = format (s, " %U", format_ip_interface_address, lm, adj->if_address_index);
1001 if (adj->arp.next_hop.ip6.as_u64[0] || adj->arp.next_hop.ip6.as_u64[1])
1002 s = format (s, " via %U", format_ip46_address,
1003 &adj->arp.next_hop, IP46_TYPE_ANY);
1005 case IP_LOOKUP_NEXT_LOCAL:
1006 if (adj->if_address_index != ~0)
1007 s = format (s, " %U", format_ip_interface_address, lm, adj->if_address_index);
1010 case IP_LOOKUP_NEXT_CLASSIFY:
1011 s = format (s, " table %d", adj->classify.table_index);
1013 case IP_LOOKUP_NEXT_INDIRECT:
1014 s = format (s, " via %U", format_ip46_address,
1015 &adj->indirect.next_hop, IP46_TYPE_ANY);
1021 if (adj->explicit_fib_index != ~0 && adj->explicit_fib_index != 0)
1022 s = format (s, " lookup fib index %d", adj->explicit_fib_index);
1023 if (adj->share_count > 0)
1024 s = format (s, " shared %d", adj->share_count + 1);
1025 if (adj->next_adj_with_signature)
1026 s = format (s, " next_adj_with_signature %d", adj->next_adj_with_signature);
1031 u8 * format_ip_adjacency_packet_data (u8 * s, va_list * args)
1033 vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
1034 ip_lookup_main_t * lm = va_arg (*args, ip_lookup_main_t *);
1035 u32 adj_index = va_arg (*args, u32);
1036 u8 * packet_data = va_arg (*args, u8 *);
1037 u32 n_packet_data_bytes = va_arg (*args, u32);
1038 ip_adjacency_t * adj = ip_get_adjacency (lm, adj_index);
1040 switch (adj->lookup_next_index)
1042 case IP_LOOKUP_NEXT_REWRITE:
1043 s = format (s, "%U",
1044 format_vnet_rewrite_header,
1045 vnm->vlib_main, &adj->rewrite_header, packet_data, n_packet_data_bytes);
1055 static uword unformat_ip_lookup_next (unformat_input_t * input, va_list * args)
1057 ip_lookup_next_t * result = va_arg (*args, ip_lookup_next_t *);
1060 if (unformat (input, "drop"))
1061 n = IP_LOOKUP_NEXT_DROP;
1063 else if (unformat (input, "punt"))
1064 n = IP_LOOKUP_NEXT_PUNT;
1066 else if (unformat (input, "local"))
1067 n = IP_LOOKUP_NEXT_LOCAL;
1069 else if (unformat (input, "arp"))
1070 n = IP_LOOKUP_NEXT_ARP;
1072 else if (unformat (input, "classify"))
1073 n = IP_LOOKUP_NEXT_CLASSIFY;
1082 static uword unformat_ip_adjacency (unformat_input_t * input, va_list * args)
1084 vlib_main_t * vm = va_arg (*args, vlib_main_t *);
1085 ip_adjacency_t * adj = va_arg (*args, ip_adjacency_t *);
1086 u32 node_index = va_arg (*args, u32);
1087 vnet_main_t * vnm = vnet_get_main();
1088 u32 sw_if_index, is_ip6;
1090 ip_lookup_next_t next;
1092 is_ip6 = node_index == ip6_rewrite_node.index;
1093 adj->rewrite_header.node_index = node_index;
1094 adj->explicit_fib_index = ~0;
1096 if (unformat (input, "arp %U %U",
1097 unformat_vnet_sw_interface, vnm, &sw_if_index,
1098 unformat_ip46_address, &a46, is_ip6?IP46_TYPE_IP6:IP46_TYPE_IP4))
1100 ip_lookup_main_t * lm = is_ip6 ? &ip6_main.lookup_main : &ip4_main.lookup_main;
1101 ip_adjacency_t * a_adj;
1105 adj_index = ip6_fib_lookup (&ip6_main, sw_if_index, &a46.ip6);
1107 adj_index = ip4_fib_lookup (&ip4_main, sw_if_index, &a46.ip4);
1109 a_adj = ip_get_adjacency (lm, adj_index);
1111 if (a_adj->rewrite_header.sw_if_index != sw_if_index)
1115 ip6_adjacency_set_interface_route (vnm, adj, sw_if_index, a_adj->if_address_index);
1117 ip4_adjacency_set_interface_route (vnm, adj, sw_if_index, a_adj->if_address_index);
1120 else if (unformat_user (input, unformat_ip_lookup_next, &next))
1122 adj->lookup_next_index = next;
1123 adj->if_address_index = ~0;
1124 if (next == IP_LOOKUP_NEXT_LOCAL)
1125 (void) unformat (input, "%d", &adj->if_address_index);
1126 else if (next == IP_LOOKUP_NEXT_CLASSIFY)
1128 if (!unformat (input, "%d", &adj->classify.table_index))
1130 clib_warning ("classify adj must specify table index");
1134 else if (next == IP_LOOKUP_NEXT_DROP)
1136 adj->rewrite_header.node_index = 0;
1140 else if (unformat_user (input,
1141 unformat_vnet_rewrite,
1142 vm, &adj->rewrite_header, sizeof (adj->rewrite_data)))
1143 adj->lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
1152 vnet_ip_route_cmd (vlib_main_t * vm, unformat_input_t * main_input, vlib_cli_command_t * cmd)
1154 vnet_main_t * vnm = vnet_get_main();
1155 clib_error_t * error = 0;
1156 u32 table_id, is_del;
1157 u32 weight, * weights = 0;
1158 u32 * table_ids = 0;
1159 u32 sw_if_index, * sw_if_indices = 0;
1160 ip4_address_t ip4_addr, * ip4_dst_addresses = 0, * ip4_via_next_hops = 0;
1161 ip6_address_t ip6_addr, * ip6_dst_addresses = 0, * ip6_via_next_hops = 0;
1162 u32 dst_address_length, * dst_address_lengths = 0;
1163 ip_adjacency_t parse_adj, * add_adj = 0;
1164 unformat_input_t _line_input, * line_input = &_line_input;
1172 /* Get a line of input. */
1173 if (! unformat_user (main_input, unformat_line_input, line_input))
1176 memset(&parse_adj, 0, sizeof (parse_adj));
1178 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1180 if (unformat (line_input, "table %d", &table_id))
1182 else if (unformat (line_input, "del"))
1184 else if (unformat (line_input, "add"))
1186 else if (unformat (line_input, "count %f", &count))
1189 else if (unformat (line_input, "%U/%d",
1190 unformat_ip4_address, &ip4_addr,
1191 &dst_address_length))
1193 vec_add1 (ip4_dst_addresses, ip4_addr);
1194 vec_add1 (dst_address_lengths, dst_address_length);
1197 else if (unformat (line_input, "%U/%d",
1198 unformat_ip6_address, &ip6_addr,
1199 &dst_address_length))
1201 vec_add1 (ip6_dst_addresses, ip6_addr);
1202 vec_add1 (dst_address_lengths, dst_address_length);
1205 else if (unformat (line_input, "via %U %U weight %u",
1206 unformat_ip4_address, &ip4_addr,
1207 unformat_vnet_sw_interface, vnm, &sw_if_index,
1210 vec_add1 (ip4_via_next_hops, ip4_addr);
1211 vec_add1 (sw_if_indices, sw_if_index);
1212 vec_add1 (weights, weight);
1213 vec_add1 (table_ids, (u32)~0);
1216 else if (unformat (line_input, "via %U %U weight %u",
1217 unformat_ip6_address, &ip6_addr,
1218 unformat_vnet_sw_interface, vnm, &sw_if_index,
1221 vec_add1 (ip6_via_next_hops, ip6_addr);
1222 vec_add1 (sw_if_indices, sw_if_index);
1223 vec_add1 (weights, weight);
1224 vec_add1 (table_ids, (u32)~0);
1227 else if (unformat (line_input, "via %U %U",
1228 unformat_ip4_address, &ip4_addr,
1229 unformat_vnet_sw_interface, vnm, &sw_if_index))
1231 vec_add1 (ip4_via_next_hops, ip4_addr);
1232 vec_add1 (sw_if_indices, sw_if_index);
1233 vec_add1 (weights, 1);
1234 vec_add1 (table_ids, (u32)~0);
1237 else if (unformat (line_input, "via %U %U",
1238 unformat_ip6_address, &ip6_addr,
1239 unformat_vnet_sw_interface, vnm, &sw_if_index))
1241 vec_add1 (ip6_via_next_hops, ip6_addr);
1242 vec_add1 (sw_if_indices, sw_if_index);
1243 vec_add1 (weights, 1);
1244 vec_add1 (table_ids, (u32)~0);
1246 else if (unformat (line_input, "via %U",
1247 unformat_ip4_address, &ip4_addr))
1249 vec_add1 (ip4_via_next_hops, ip4_addr);
1250 vec_add1 (sw_if_indices, (u32)~0);
1251 vec_add1 (weights, 1);
1252 vec_add1 (table_ids, table_id);
1254 else if (unformat (line_input, "via %U",
1255 unformat_ip6_address, &ip6_addr))
1257 vec_add1 (ip6_via_next_hops, ip6_addr);
1258 vec_add1 (sw_if_indices, (u32)~0);
1259 vec_add1 (weights, 1);
1260 vec_add1 (table_ids, (u32)table_id);
1263 else if (vec_len (ip4_dst_addresses) > 0
1264 && unformat (line_input, "via %U",
1265 unformat_ip_adjacency, vm, &parse_adj, ip4_rewrite_node.index))
1266 vec_add1 (add_adj, parse_adj);
1268 else if (vec_len (ip6_dst_addresses) > 0
1269 && unformat (line_input, "via %U",
1270 unformat_ip_adjacency, vm, &parse_adj, ip6_rewrite_node.index))
1271 vec_add1 (add_adj, parse_adj);
1272 else if (unformat (line_input, "lookup in table %d", &outer_table_id))
1276 if (vec_len (ip4_dst_addresses) > 0)
1277 p = hash_get (ip4_main.fib_index_by_table_id, outer_table_id);
1279 p = hash_get (ip6_main.fib_index_by_table_id, outer_table_id);
1283 error = clib_error_return (0, "Nonexistent outer table id %d",
1288 parse_adj.lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
1289 parse_adj.explicit_fib_index = p[0];
1290 vec_add1 (add_adj, parse_adj);
1294 error = unformat_parse_error (line_input);
1299 unformat_free (line_input);
1301 if (vec_len (ip4_dst_addresses) + vec_len (ip6_dst_addresses) == 0)
1303 error = clib_error_return (0, "expected ip4/ip6 destination address/length.");
1307 if (vec_len (ip4_dst_addresses) > 0 && vec_len (ip6_dst_addresses) > 0)
1309 error = clib_error_return (0, "mixed ip4/ip6 address/length.");
1313 if (vec_len (ip4_dst_addresses) > 0 && vec_len (ip6_via_next_hops) > 0)
1315 error = clib_error_return (0, "ip4 destinations with ip6 next hops.");
1319 if (vec_len (ip6_dst_addresses) > 0 && vec_len (ip4_via_next_hops) > 0)
1321 error = clib_error_return (0, "ip6 destinations with ip4 next hops.");
1325 if (! is_del && vec_len (add_adj) + vec_len (weights) == 0)
1327 error = clib_error_return (0, "no next hops or adjacencies to add.");
1333 ip4_main_t * im4 = &ip4_main;
1334 ip6_main_t * im6 = &ip6_main;
1336 for (i = 0; i < vec_len (ip4_dst_addresses); i++)
1338 ip4_add_del_route_args_t a;
1340 memset (&a, 0, sizeof (a));
1341 a.flags = IP4_ROUTE_FLAG_TABLE_ID;
1342 a.table_index_or_table_id = table_id;
1343 a.dst_address = ip4_dst_addresses[i];
1344 a.dst_address_length = dst_address_lengths[i];
1349 if (vec_len (ip4_via_next_hops) == 0)
1351 uword * dst_hash, * dst_result;
1352 u32 dst_address_u32;
1355 fib = find_ip4_fib_by_table_index_or_id (im4, table_id,
1356 0 /* by table id */);
1358 a.flags |= IP4_ROUTE_FLAG_DEL;
1359 dst_address_u32 = a.dst_address.as_u32
1360 & im4->fib_masks[a.dst_address_length];
1363 fib->adj_index_by_dst_address[a.dst_address_length];
1364 dst_result = hash_get (dst_hash, dst_address_u32);
1366 a.adj_index = dst_result[0];
1369 clib_warning ("%U/%d not in FIB",
1370 format_ip4_address, &a.dst_address,
1371 a.dst_address_length);
1375 ip4_add_del_route (im4, &a);
1376 ip4_maybe_remap_adjacencies (im4, table_id,
1377 IP4_ROUTE_FLAG_TABLE_ID);
1381 u32 i, j, n, f, incr;
1382 ip4_address_t dst = a.dst_address;
1385 t[0] = vlib_time_now (vm);
1386 incr = 1<<(32 - a.dst_address_length);
1387 for (i = 0; i < n; i++)
1389 f = i + 1 < n ? IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP : 0;
1390 a.dst_address = dst;
1391 for (j = 0; j < vec_len (ip4_via_next_hops); j++)
1393 if (table_ids[j] != (u32)~0)
1395 uword * p = hash_get (im4->fib_index_by_table_id,
1399 clib_warning ("no such FIB table %d",
1403 table_ids[j] = p[0];
1406 ip4_add_del_route_next_hop (im4,
1407 IP4_ROUTE_FLAG_DEL | f,
1409 a.dst_address_length,
1410 &ip4_via_next_hops[j],
1412 weights[j], (u32)~0,
1413 table_ids[j] /* fib index */);
1415 dst.as_u32 = clib_host_to_net_u32 (incr + clib_net_to_host_u32 (dst.as_u32));
1417 t[1] = vlib_time_now (vm);
1419 vlib_cli_output (vm, "%.6e routes/sec", count / (t[1] - t[0]));
1424 if (vec_len (add_adj) > 0)
1426 a.flags |= IP4_ROUTE_FLAG_ADD;
1427 a.add_adj = add_adj;
1428 a.n_add_adj = vec_len (add_adj);
1430 ip4_add_del_route (im4, &a);
1432 else if (vec_len (ip4_via_next_hops) > 0)
1434 u32 i, j, n, f, incr;
1435 ip4_address_t dst = a.dst_address;
1438 t[0] = vlib_time_now (vm);
1439 incr = 1<<(32 - a.dst_address_length);
1440 for (i = 0; i < n; i++)
1442 f = i + 1 < n ? IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP : 0;
1443 a.dst_address = dst;
1444 for (j = 0; j < vec_len (ip4_via_next_hops); j++)
1446 if (table_ids[j] != (u32)~0)
1448 uword * p = hash_get (im4->fib_index_by_table_id,
1452 clib_warning ("no such FIB table %d",
1456 table_ids[j] = p[0];
1458 ip4_add_del_route_next_hop (im4,
1459 IP4_ROUTE_FLAG_ADD | f,
1461 a.dst_address_length,
1462 &ip4_via_next_hops[j],
1464 weights[j], (u32)~0,
1465 table_ids[j] /* fib index */);
1467 dst.as_u32 = clib_host_to_net_u32 (incr + clib_net_to_host_u32 (dst.as_u32));
1469 t[1] = vlib_time_now (vm);
1471 vlib_cli_output (vm, "%.6e routes/sec", count / (t[1] - t[0]));
1476 for (i = 0; i < vec_len (ip6_dst_addresses); i++)
1478 ip6_add_del_route_args_t a;
1481 memset (&a, 0, sizeof (a));
1482 a.flags = IP6_ROUTE_FLAG_TABLE_ID;
1483 a.table_index_or_table_id = table_id;
1484 a.dst_address = ip6_dst_addresses[i];
1485 a.dst_address_length = dst_address_lengths[i];
1490 if (vec_len (ip6_via_next_hops) == 0)
1492 BVT(clib_bihash_kv) kv, value;
1493 ip6_address_t dst_address;
1496 fib = find_ip6_fib_by_table_index_or_id (im6, table_id,
1497 0 /* by table id */);
1499 a.flags |= IP4_ROUTE_FLAG_DEL;
1501 dst_address = ip6_dst_addresses[i];
1503 ip6_address_mask (&dst_address,
1504 &im6->fib_masks[dst_address_length]);
1506 kv.key[0] = dst_address.as_u64[0];
1507 kv.key[1] = dst_address.as_u64[1];
1508 kv.key[2] = ((u64)(fib - im6->fibs)<<32)
1509 | a.dst_address_length;
1511 if (BV(clib_bihash_search)(&im6->ip6_lookup_table,
1513 a.adj_index = value.value;
1516 clib_warning ("%U/%d not in FIB",
1517 format_ip6_address, &a.dst_address,
1518 a.dst_address_length);
1522 a.flags |= IP6_ROUTE_FLAG_DEL;
1523 ip6_add_del_route (im6, &a);
1524 ip6_maybe_remap_adjacencies (im6, table_id,
1525 IP6_ROUTE_FLAG_TABLE_ID);
1530 for (i = 0; i < vec_len (ip6_via_next_hops); i++)
1532 ip6_add_del_route_next_hop (im6,
1535 a.dst_address_length,
1536 &ip6_via_next_hops[i],
1538 weights[i], (u32)~0,
1539 table_ids[i] /* fib index */);
1545 if (vec_len (add_adj) > 0)
1547 a.flags |= IP6_ROUTE_FLAG_ADD;
1548 a.add_adj = add_adj;
1549 a.n_add_adj = vec_len (add_adj);
1551 ip6_add_del_route (im6, &a);
1553 else if (vec_len (ip6_via_next_hops) > 0)
1556 for (i = 0; i < vec_len (ip6_via_next_hops); i++)
1558 ip6_add_del_route_next_hop (im6,
1561 a.dst_address_length,
1562 &ip6_via_next_hops[i],
1564 weights[i], (u32)~0,
1575 vec_free (dst_address_lengths);
1576 vec_free (ip4_dst_addresses);
1577 vec_free (ip6_dst_addresses);
1578 vec_free (ip4_via_next_hops);
1579 vec_free (ip6_via_next_hops);
1583 VLIB_CLI_COMMAND (vlib_cli_ip_command, static) = {
1585 .short_help = "Internet protocol (IP) commands",
1588 VLIB_CLI_COMMAND (vlib_cli_show_ip_command, static) = {
1590 .short_help = "Internet protocol (IP) show commands",
1593 VLIB_CLI_COMMAND (vlib_cli_show_ip4_command, static) = {
1595 .short_help = "Internet protocol version 4 (IP4) show commands",
1598 VLIB_CLI_COMMAND (vlib_cli_show_ip6_command, static) = {
1600 .short_help = "Internet protocol version 6 (IP6) show commands",
1603 VLIB_CLI_COMMAND (ip_route_command, static) = {
1605 .short_help = "Add/delete IP routes",
1606 .function = vnet_ip_route_cmd,
1611 * The next two routines address a longstanding script hemorrhoid.
1612 * Probing a v4 or v6 neighbor needs to appear to be synchronous,
1613 * or dependent route-adds will simply fail.
1615 static clib_error_t *
1616 ip6_probe_neighbor_wait (vlib_main_t *vm, ip6_address_t * a, u32 sw_if_index,
1619 vnet_main_t * vnm = vnet_get_main();
1624 uword *event_data = 0;
1626 ASSERT (vlib_in_process_context(vm));
1628 if (retry_count > 0)
1629 vnet_register_ip6_neighbor_resolution_event
1630 (vnm, a, vlib_get_current_process (vm)->node_runtime.node_index,
1631 1 /* event */, 0 /* data */);
1633 for (i = 0; i < retry_count; i++)
1635 /* The interface may be down, etc. */
1636 e = ip6_probe_neighbor (vm, a, sw_if_index);
1641 vlib_process_wait_for_event_or_clock (vm, 1.0);
1642 event_type = vlib_process_get_events (vm, &event_data);
1645 case 1: /* resolved... */
1646 vlib_cli_output (vm, "Resolved %U",
1647 format_ip6_address, a);
1651 case ~0: /* timeout */
1655 clib_warning ("unknown event_type %d", event_type);
1657 vec_reset_length (event_data);
1663 return clib_error_return (0, "Resolution failed for %U",
1664 format_ip6_address, a);
1668 static clib_error_t *
1669 ip4_probe_neighbor_wait (vlib_main_t *vm, ip4_address_t * a, u32 sw_if_index,
1672 vnet_main_t * vnm = vnet_get_main();
1677 uword *event_data = 0;
1679 ASSERT (vlib_in_process_context(vm));
1681 if (retry_count > 0)
1682 vnet_register_ip4_arp_resolution_event
1683 (vnm, a, vlib_get_current_process (vm)->node_runtime.node_index,
1684 1 /* event */, 0 /* data */);
1686 for (i = 0; i < retry_count; i++)
1688 /* The interface may be down, etc. */
1689 e = ip4_probe_neighbor (vm, a, sw_if_index);
1694 vlib_process_wait_for_event_or_clock (vm, 1.0);
1695 event_type = vlib_process_get_events (vm, &event_data);
1698 case 1: /* resolved... */
1699 vlib_cli_output (vm, "Resolved %U",
1700 format_ip4_address, a);
1704 case ~0: /* timeout */
1708 clib_warning ("unknown event_type %d", event_type);
1710 vec_reset_length (event_data);
1715 vec_reset_length (event_data);
1718 return clib_error_return (0, "Resolution failed for %U",
1719 format_ip4_address, a);
1723 static clib_error_t *
1724 probe_neighbor_address (vlib_main_t * vm,
1725 unformat_input_t * input,
1726 vlib_cli_command_t * cmd)
1728 vnet_main_t * vnm = vnet_get_main();
1729 unformat_input_t _line_input, * line_input = &_line_input;
1732 clib_error_t * error = 0;
1733 u32 sw_if_index = ~0;
1734 int retry_count = 3;
1736 int address_set = 0;
1738 /* Get a line of input. */
1739 if (! unformat_user (input, unformat_line_input, line_input))
1742 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1744 if (unformat_user (line_input, unformat_vnet_sw_interface, vnm,
1747 else if (unformat (line_input, "retry %d", &retry_count))
1750 else if (unformat (line_input, "%U", unformat_ip4_address, &a4))
1752 else if (unformat (line_input, "%U", unformat_ip6_address, &a6))
1758 return clib_error_return (0, "unknown input '%U'",
1759 format_unformat_error, line_input);
1762 unformat_free (line_input);
1764 if (sw_if_index == ~0)
1765 return clib_error_return (0, "Interface required, not set.");
1766 if (address_set == 0)
1767 return clib_error_return (0, "ip address required, not set.");
1768 if (address_set > 1)
1769 return clib_error_return (0, "Multiple ip addresses not supported.");
1772 error = ip4_probe_neighbor_wait (vm, &a4, sw_if_index, retry_count);
1774 error = ip6_probe_neighbor_wait (vm, &a6, sw_if_index, retry_count);
1779 VLIB_CLI_COMMAND (ip_probe_neighbor_command, static) = {
1780 .path = "ip probe-neighbor",
1781 .function = probe_neighbor_address,
1782 .short_help = "ip probe-neighbor <intfc> <ip4-addr> | <ip6-addr> [retry nn]",
1786 typedef CLIB_PACKED (struct {
1787 ip4_address_t address;
1789 u32 address_length : 6;
1795 ip4_route_cmp (void * a1, void * a2)
1797 ip4_route_t * r1 = a1;
1798 ip4_route_t * r2 = a2;
1800 int cmp = ip4_address_compare (&r1->address, &r2->address);
1801 return cmp ? cmp : ((int) r1->address_length - (int) r2->address_length);
1804 static clib_error_t *
1805 ip4_show_fib (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1807 vnet_main_t * vnm = vnet_get_main();
1808 ip4_main_t * im4 = &ip4_main;
1809 ip4_route_t * routes, * r;
1811 ip_lookup_main_t * lm = &im4->lookup_main;
1813 int verbose, matching, mtrie, include_empty_fibs;
1814 ip4_address_t matching_address;
1821 include_empty_fibs = 0;
1824 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1826 if (unformat (input, "brief") || unformat (input, "summary")
1827 || unformat (input, "sum"))
1830 else if (unformat (input, "mtrie"))
1833 else if (unformat (input, "include-empty"))
1834 include_empty_fibs = 1;
1836 else if (unformat (input, "%U", unformat_ip4_address, &matching_address))
1839 else if (unformat (input, "clear"))
1842 else if (unformat (input, "table %d", &table_id))
1848 vec_foreach (fib, im4->fibs)
1853 for (i = 0; i < ARRAY_LEN (fib->adj_index_by_dst_address); i++)
1855 uword * hash = fib->adj_index_by_dst_address[i];
1856 uword n_elts = hash_elts (hash);
1864 if (fib_not_empty == 0 && include_empty_fibs == 0)
1867 if (table_id >= 0 && table_id != (int)fib->table_id)
1870 if (include_empty_fibs)
1871 vlib_cli_output (vm, "Table %d, fib_index %d, flow hash: %U",
1872 fib->table_id, fib - im4->fibs,
1873 format_ip_flow_hash_config, fib->flow_hash_config);
1878 if (include_empty_fibs == 0)
1879 vlib_cli_output (vm, "Table %d, fib_index %d, flow hash: %U",
1880 fib->table_id, fib - im4->fibs,
1881 format_ip_flow_hash_config, fib->flow_hash_config);
1882 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
1883 for (i = 0; i < ARRAY_LEN (fib->adj_index_by_dst_address); i++)
1885 uword * hash = fib->adj_index_by_dst_address[i];
1886 uword n_elts = hash_elts (hash);
1888 vlib_cli_output (vm, "%20d%16d", i, n_elts);
1894 _vec_len (routes) = 0;
1896 _vec_len (results) = 0;
1898 for (i = 0; i < ARRAY_LEN (fib->adj_index_by_dst_address); i++)
1900 uword * hash = fib->adj_index_by_dst_address[i];
1904 x.address_length = i;
1908 x.address.as_u32 = matching_address.as_u32 & im4->fib_masks[i];
1909 p = hash_get_pair (hash, x.address.as_u32);
1912 if (lm->fib_result_n_words > 1)
1914 x.index = vec_len (results);
1915 vec_add (results, p->value, lm->fib_result_n_words);
1918 x.index = p->value[0];
1919 vec_add1 (routes, x);
1924 hash_foreach_pair (p, hash, ({
1925 x.address.data_u32 = p->key;
1926 if (lm->fib_result_n_words > 1)
1928 x.index = vec_len (results);
1929 vec_add (results, p->value, lm->fib_result_n_words);
1932 x.index = p->value[0];
1934 vec_add1 (routes, x);
1939 vec_sort_with_function (routes, ip4_route_cmp);
1940 if (vec_len(routes)) {
1941 if (include_empty_fibs == 0)
1942 vlib_cli_output (vm, "Table %d, fib_index %d, flow hash: %U",
1943 fib->table_id, fib - im4->fibs,
1944 format_ip_flow_hash_config, fib->flow_hash_config);
1946 vlib_cli_output (vm, "%U", format_ip4_fib_mtrie, &fib->mtrie);
1947 vlib_cli_output (vm, "%=20s%=16s%=16s%=16s",
1948 "Destination", "Packets", "Bytes", "Adjacency");
1950 vec_foreach (r, routes)
1952 vlib_counter_t c, sum;
1953 uword i, j, n_left, n_nhs, adj_index, * result = 0;
1954 ip_adjacency_t * adj;
1955 ip_multipath_next_hop_t * nhs, tmp_nhs[1];
1957 adj_index = r->index;
1958 if (lm->fib_result_n_words > 1)
1960 result = vec_elt_at_index (results, adj_index);
1961 adj_index = result[0];
1964 adj = ip_get_adjacency (lm, adj_index);
1965 if (adj->n_adj == 1)
1968 nhs[0].next_hop_adj_index = ~0; /* not used */
1974 ip_multipath_adjacency_t * madj;
1975 madj = vec_elt_at_index (lm->multipath_adjacencies, adj->heap_handle);
1976 nhs = heap_elt_at_index (lm->next_hop_heap, madj->normalized_next_hops.heap_offset);
1977 n_nhs = madj->normalized_next_hops.count;
1980 n_left = nhs[0].weight;
1981 vlib_counter_zero (&sum);
1982 for (i = j = 0; i < adj->n_adj; i++)
1985 vlib_get_combined_counter (&lm->adjacency_counters,
1988 vlib_zero_combined_counter (&lm->adjacency_counters,
1990 vlib_counter_add (&sum, &c);
1997 msg = format (msg, "%-20U",
1998 format_ip4_address_and_length,
1999 r->address.data, r->address_length);
2001 msg = format (msg, "%U", format_white_space, 20);
2003 msg = format (msg, "%16Ld%16Ld ", sum.packets, sum.bytes);
2005 indent = vec_len (msg);
2006 msg = format (msg, "weight %d, index %d",
2007 nhs[j].weight, adj_index + i);
2009 if (ip_adjacency_is_multipath(lm, adj_index))
2010 msg = format (msg, ", multipath");
2012 msg = format (msg, "\n%U%U",
2013 format_white_space, indent,
2014 format_ip_adjacency,
2015 vnm, lm, adj_index + i);
2017 vlib_cli_output (vm, "%v", msg);
2020 if (result && lm->format_fib_result)
2021 vlib_cli_output (vm, "%20s%U", "",
2022 lm->format_fib_result, vm, lm, result,
2023 i + 1 - nhs[j].weight,
2029 n_left = nhs[j].weight;
2030 vlib_counter_zero (&sum);
2043 VLIB_CLI_COMMAND (ip4_show_fib_command, static) = {
2044 .path = "show ip fib",
2045 .short_help = "show ip fib [mtrie] [summary] [table <n>] [<ip4-addr>] [clear] [include-empty]",
2046 .function = ip4_show_fib,
2050 ip6_address_t address;
2059 ip6_route_t ** routep;
2060 } add_routes_in_fib_arg_t;
2062 static void add_routes_in_fib (BVT(clib_bihash_kv) * kvp, void *arg)
2064 add_routes_in_fib_arg_t * ap = arg;
2066 if (kvp->key[2]>>32 == ap->fib_index)
2068 ip6_address_t *addr;
2070 addr = (ip6_address_t *) kvp;
2071 vec_add2 (*ap->routep, r, 1);
2072 r->address = addr[0];
2073 r->address_length = kvp->key[2] & 0xFF;
2074 r->index = kvp->value;
2080 u64 count_by_prefix_length[129];
2081 } count_routes_in_fib_at_prefix_length_arg_t;
2083 static void count_routes_in_fib_at_prefix_length
2084 (BVT(clib_bihash_kv) * kvp, void *arg)
2086 count_routes_in_fib_at_prefix_length_arg_t * ap = arg;
2089 if ((kvp->key[2]>>32) != ap->fib_index)
2092 mask_width = kvp->key[2] & 0xFF;
2094 ap->count_by_prefix_length[mask_width]++;
2098 ip6_route_cmp (void * a1, void * a2)
2100 ip6_route_t * r1 = a1;
2101 ip6_route_t * r2 = a2;
2103 int cmp = ip6_address_compare (&r1->address, &r2->address);
2104 return cmp ? cmp : ((int) r1->address_length - (int) r2->address_length);
2107 static clib_error_t *
2108 ip6_show_fib (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
2110 vnet_main_t * vnm = vnet_get_main();
2111 ip6_main_t * im6 = &ip6_main;
2112 ip6_route_t * routes, * r;
2114 ip_lookup_main_t * lm = &im6->lookup_main;
2117 BVT(clib_bihash) * h = &im6->ip6_lookup_table;
2118 __attribute__((unused)) u8 clear = 0;
2119 add_routes_in_fib_arg_t _a, *a=&_a;
2120 count_routes_in_fib_at_prefix_length_arg_t _ca, *ca = &_ca;
2125 if (unformat (input, "brief") || unformat (input, "summary")
2126 || unformat (input, "sum"))
2129 if (unformat (input, "clear"))
2132 vlib_cli_output (vm, "FIB lookup table: %d buckets, %lld MB heap",
2133 im6->lookup_table_nbuckets, im6->lookup_table_size>>20);
2134 vlib_cli_output (vm, "%U", format_mheap, h->mheap, 0 /*verbose*/);
2135 vlib_cli_output (vm, " ");
2137 vec_foreach (fib, im6->fibs)
2139 vlib_cli_output (vm, "VRF %d, fib_index %d, flow hash: %U",
2140 fib->table_id, fib - im6->fibs,
2141 format_ip_flow_hash_config, fib->flow_hash_config);
2147 vlib_cli_output (vm, "%=20s%=16s", "Prefix length", "Count");
2149 memset (ca, 0, sizeof(*ca));
2150 ca->fib_index = fib - im6->fibs;
2152 BV(clib_bihash_foreach_key_value_pair)
2153 (h, count_routes_in_fib_at_prefix_length, ca);
2155 for (len = 128; len >= 0; len--)
2157 if (ca->count_by_prefix_length[len])
2158 vlib_cli_output (vm, "%=20d%=16lld",
2159 len, ca->count_by_prefix_length[len]);
2165 _vec_len (routes) = 0;
2167 _vec_len (results) = 0;
2169 a->fib_index = fib - im6->fibs;
2170 a->routep = &routes;
2172 BV(clib_bihash_foreach_key_value_pair)(h, add_routes_in_fib, a);
2174 vec_sort_with_function (routes, ip6_route_cmp);
2176 vlib_cli_output (vm, "%=45s%=16s%=16s%=16s",
2177 "Destination", "Packets", "Bytes", "Adjacency");
2178 vec_foreach (r, routes)
2180 vlib_counter_t c, sum;
2181 uword i, j, n_left, n_nhs, adj_index, * result = 0;
2182 ip_adjacency_t * adj;
2183 ip_multipath_next_hop_t * nhs, tmp_nhs[1];
2185 adj_index = r->index;
2186 if (lm->fib_result_n_words > 1)
2188 result = vec_elt_at_index (results, adj_index);
2189 adj_index = result[0];
2192 adj = ip_get_adjacency (lm, adj_index);
2193 if (adj->n_adj == 1)
2196 nhs[0].next_hop_adj_index = ~0; /* not used */
2202 ip_multipath_adjacency_t * madj;
2203 madj = vec_elt_at_index (lm->multipath_adjacencies, adj->heap_handle);
2204 nhs = heap_elt_at_index (lm->next_hop_heap, madj->normalized_next_hops.heap_offset);
2205 n_nhs = madj->normalized_next_hops.count;
2208 n_left = nhs[0].weight;
2209 vlib_counter_zero (&sum);
2210 for (i = j = 0; i < adj->n_adj; i++)
2213 vlib_get_combined_counter (&lm->adjacency_counters,
2216 vlib_zero_combined_counter (&lm->adjacency_counters,
2218 vlib_counter_add (&sum, &c);
2225 msg = format (msg, "%-45U",
2226 format_ip6_address_and_length,
2227 r->address.as_u8, r->address_length);
2229 msg = format (msg, "%U", format_white_space, 20);
2231 msg = format (msg, "%16Ld%16Ld ", sum.packets, sum.bytes);
2233 indent = vec_len (msg);
2234 msg = format (msg, "weight %d, index %d",
2235 nhs[j].weight, adj_index + i);
2237 if (ip_adjacency_is_multipath(lm, adj_index + i))
2238 msg = format (msg, ", multipath");
2240 msg = format (msg, "\n%U%U",
2241 format_white_space, indent,
2242 format_ip_adjacency,
2243 vnm, lm, adj_index + i);
2245 vlib_cli_output (vm, "%v", msg);
2251 n_left = nhs[j].weight;
2252 vlib_counter_zero (&sum);
2257 if (result && lm->format_fib_result)
2258 vlib_cli_output (vm, "%20s%U", "", lm->format_fib_result, vm, lm, result, 0);
2260 vlib_cli_output (vm, " ");
2269 VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
2270 .path = "show ip6 fib",
2271 .short_help = "show ip6 fib [summary] [clear]",
2272 .function = ip6_show_fib,