#include <vppinfra/math.h> /* for fabs */
#include <vnet/ip/ip.h>
+#include <vnet/ip/adj_alloc.h>
static void
ip_multipath_del_adjacency (ip_lookup_main_t * lm, u32 del_adj_index);
ip_poison_adjacencies (ip_adjacency_t * adj, uword n_adj)
{
if (CLIB_DEBUG > 0)
- memset (adj, 0xfe, n_adj * sizeof (adj[0]));
+ {
+ u32 save_handle = adj->heap_handle;;
+ u32 save_n_adj = adj->n_adj;
+
+ memset (adj, 0xfe, n_adj * sizeof (adj[0]));
+
+ adj->heap_handle = save_handle;
+ adj->n_adj = save_n_adj;
+ }
+}
+
+static void
+ip_share_adjacency(ip_lookup_main_t * lm, u32 adj_index)
+{
+ ip_adjacency_t * adj = ip_get_adjacency(lm, adj_index);
+ uword * p;
+ u32 old_ai;
+ uword signature = vnet_ip_adjacency_signature (adj);
+
+ p = hash_get (lm->adj_index_by_signature, signature);
+ /* Hash collision? */
+ if (p)
+ {
+ /* Save the adj index, p[0] will be toast after the unset! */
+ old_ai = p[0];
+ hash_unset (lm->adj_index_by_signature, signature);
+ hash_set (lm->adj_index_by_signature, signature, adj_index);
+ adj->next_adj_with_signature = old_ai;
+ }
+ else
+ {
+ adj->next_adj_with_signature = 0;
+ hash_set (lm->adj_index_by_signature, signature, adj_index);
+ }
+}
+
+static void
+ip_unshare_adjacency(ip_lookup_main_t * lm, u32 adj_index)
+{
+ ip_adjacency_t * adj = ip_get_adjacency(lm, adj_index);
+ uword signature;
+ uword * p;
+ u32 this_ai;
+ ip_adjacency_t * this_adj, * prev_adj = 0;
+
+ signature = vnet_ip_adjacency_signature (adj);
+ p = hash_get (lm->adj_index_by_signature, signature);
+ if (p == 0)
+ return;
+
+ this_ai = p[0];
+ /* At the top of the signature chain (likely)? */
+ if (this_ai == adj_index)
+ {
+ if (adj->next_adj_with_signature == 0)
+ {
+ hash_unset (lm->adj_index_by_signature, signature);
+ return;
+ }
+ else
+ {
+ this_adj = ip_get_adjacency (lm, adj->next_adj_with_signature);
+ hash_unset (lm->adj_index_by_signature, signature);
+ hash_set (lm->adj_index_by_signature, signature,
+ this_adj->heap_handle);
+ }
+ }
+ else /* walk signature chain */
+ {
+ this_adj = ip_get_adjacency (lm, this_ai);
+ while (this_adj != adj)
+ {
+ prev_adj = this_adj;
+ this_adj = ip_get_adjacency
+ (lm, this_adj->next_adj_with_signature);
+ /*
+ * This can happen when creating the first multipath adj of a set
+ * We end up looking at the miss adjacency (handle==0).
+ */
+ if (this_adj->heap_handle == 0)
+ return;
+ }
+ prev_adj->next_adj_with_signature = this_adj->next_adj_with_signature;
+ }
+}
+
+int ip_register_adjacency(vlib_main_t *vm,
+ u8 is_ip4,
+ ip_adj_register_t *reg)
+{
+ ip_lookup_main_t *lm = (is_ip4)?&ip4_main.lookup_main:&ip6_main.lookup_main;
+ vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) ((is_ip4)?"ip4-lookup":"ip6-lookup"));
+ vlib_node_t *next_node = vlib_get_node_by_name(vm, (u8 *) reg->node_name);
+ *reg->next_index = vlib_node_add_next (vm, node->index, next_node->index);
+ vec_validate(lm->registered_adjacencies, *reg->next_index);
+ lm->registered_adjacencies[*reg->next_index] = *reg;
+ return 0;
+}
+
+int ip_init_registered_adjacencies(u8 is_ip4)
+{
+ vlib_main_t *vm = vlib_get_main();
+ ip_lookup_main_t *lm = (is_ip4)?&ip4_main.lookup_main:&ip6_main.lookup_main;
+ ip_adj_register_t *reg = lm->registered_adjacencies;
+ lm->registered_adjacencies = 0; //Init vector
+ int rv;
+ while (reg) {
+ if((rv = ip_register_adjacency(vm, is_ip4, reg)))
+ return rv;
+ reg = reg->next;
+ }
+ return 0;
}
/* Create new block of given number of contiguous adjacencies. */
ip_adjacency_t * adj;
u32 ai, i, handle;
- ai = heap_alloc (lm->adjacency_heap, n_adj, handle);
- adj = heap_elt_at_index (lm->adjacency_heap, ai);
+ /* See if we know enough to attempt to share an existing adjacency */
+ if (copy_adj && n_adj == 1)
+ {
+ uword signature;
+ uword * p;
+
+ switch (copy_adj->lookup_next_index)
+ {
+ case IP_LOOKUP_NEXT_DROP:
+ if (lm->drop_adj_index)
+ {
+ adj = ip_get_adjacency (lm, lm->drop_adj_index);
+ *adj_index_return = lm->drop_adj_index;
+ return (adj);
+ }
+ break;
+
+ case IP_LOOKUP_NEXT_LOCAL:
+ if (lm->local_adj_index)
+ {
+ adj = ip_get_adjacency (lm, lm->local_adj_index);
+ *adj_index_return = lm->local_adj_index;
+ return (adj);
+ }
+ default:
+ break;
+ }
+
+ signature = vnet_ip_adjacency_signature (copy_adj);
+ p = hash_get (lm->adj_index_by_signature, signature);
+ if (p)
+ {
+ adj = vec_elt_at_index (lm->adjacency_heap, p[0]);
+ while (1)
+ {
+ if (vnet_ip_adjacency_share_compare (adj, copy_adj))
+ {
+ adj->share_count++;
+ *adj_index_return = p[0];
+ return adj;
+ }
+ if (adj->next_adj_with_signature == 0)
+ break;
+ adj = vec_elt_at_index (lm->adjacency_heap,
+ adj->next_adj_with_signature);
+ }
+ }
+ }
+
+ lm->adjacency_heap = aa_alloc (lm->adjacency_heap, &adj, n_adj);
+ handle = ai = adj->heap_handle;
ip_poison_adjacencies (adj, n_adj);
adj[i].rewrite_header.sw_if_index = ~0;
adj[i].explicit_fib_index = ~0;
adj[i].mcast_group_index = ~0;
- adj[i].classify_table_index = ~0;
+ adj[i].classify.table_index = ~0;
adj[i].saved_lookup_next_index = 0;
+ adj[i].special_adjacency_format_function_index = 0;
if (copy_adj)
- adj[i] = copy_adj[i];
+ adj[i] = copy_adj[i];
adj[i].heap_handle = handle;
adj[i].n_adj = n_adj;
+ adj[i].share_count = 0;
+ adj[i].next_adj_with_signature = 0;
/* Zero possibly stale counters for re-used adjacencies. */
vlib_zero_combined_counter (&lm->adjacency_counters, ai + i);
}
+ /* Set up to share the adj later */
+ if (copy_adj && n_adj == 1)
+ ip_share_adjacency(lm, ai);
+
*adj_index_return = ai;
return adj;
}
+void
+ip_update_adjacency (ip_lookup_main_t * lm,
+ u32 adj_index,
+ ip_adjacency_t * copy_adj)
+{
+ ip_adjacency_t * adj = ip_get_adjacency(lm, adj_index);
+
+ ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 1);
+ ip_unshare_adjacency(lm, adj_index);
+
+ /* temporary redirect to drop while updating rewrite data */
+ adj->lookup_next_index = IP_LOOKUP_NEXT_ARP;
+ CLIB_MEMORY_BARRIER();
+
+ clib_memcpy (&adj->rewrite_header, ©_adj->rewrite_header,
+ VLIB_BUFFER_PRE_DATA_SIZE);
+ adj->lookup_next_index = copy_adj->lookup_next_index;
+ ip_share_adjacency(lm, adj_index);
+ ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 0);
+}
+
static void ip_del_adjacency2 (ip_lookup_main_t * lm, u32 adj_index, u32 delete_multipath_adjacency)
{
ip_adjacency_t * adj;
- uword handle;
ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 1);
adj = ip_get_adjacency (lm, adj_index);
- handle = adj->heap_handle;
+
+ /* Special-case miss, local, drop adjs */
+ if (adj_index < 3)
+ return;
+
+ if (adj->n_adj == 1)
+ {
+ if (adj->share_count > 0)
+ {
+ adj->share_count --;
+ return;
+ }
+
+ ip_unshare_adjacency(lm, adj_index);
+ }
if (delete_multipath_adjacency)
ip_multipath_del_adjacency (lm, adj_index);
ip_poison_adjacencies (adj, adj->n_adj);
- heap_dealloc (lm->adjacency_heap, handle);
+ aa_free (lm->adjacency_heap, adj);
}
void ip_del_adjacency (ip_lookup_main_t * lm, u32 adj_index)
}
else
{
- memcpy (nhs, raw_next_hops, n_nhs * sizeof (raw_next_hops[0]));
+ clib_memcpy (nhs, raw_next_hops, n_nhs * sizeof (raw_next_hops[0]));
qsort (nhs, n_nhs, sizeof (nhs[0]), (void *) next_hop_sort_by_weight);
}
madj->normalized_next_hops.heap_offset
= heap_alloc (lm->next_hop_heap, vec_len (nhs),
madj->normalized_next_hops.heap_handle);
- memcpy (lm->next_hop_heap + madj->normalized_next_hops.heap_offset,
+ clib_memcpy (lm->next_hop_heap + madj->normalized_next_hops.heap_offset,
nhs, vec_bytes (nhs));
hash_set (lm->multipath_adjacency_by_next_hops,
madj->unnormalized_next_hops.heap_offset
= heap_alloc (lm->next_hop_heap, vec_len (raw_next_hops),
madj->unnormalized_next_hops.heap_handle);
- memcpy (lm->next_hop_heap + madj->unnormalized_next_hops.heap_offset,
+ clib_memcpy (lm->next_hop_heap + madj->unnormalized_next_hops.heap_offset,
raw_next_hops, vec_bytes (raw_next_hops));
ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 0);
i_nh = 0;
nhs = 0;
+ /* If old adj is not multipath, we need to "convert" it by calling this
+ * function recursively */
+ if (old_mp_adj_index != ~0 && !ip_adjacency_is_multipath(lm, old_mp_adj_index))
+ {
+ ip_multipath_adjacency_add_del_next_hop(lm, /* is_del */ 0,
+ /* old_mp_adj_index */ ~0,
+ /* nh_adj_index */ old_mp_adj_index,
+ /* weight * */ 1,
+ &old_mp_adj_index);
+ }
+
/* If old multipath adjacency is valid, find requested next hop. */
if (old_mp_adj_index < vec_len (lm->multipath_adjacencies)
&& lm->multipath_adjacencies[old_mp_adj_index].normalized_next_hops.count > 0)
return /* no error */ 0;
}
-void serialize_vec_ip_adjacency (serialize_main_t * m, va_list * va)
-{
- ip_adjacency_t * a = va_arg (*va, ip_adjacency_t *);
- u32 n = va_arg (*va, u32);
- u32 i;
- for (i = 0; i < n; i++)
- {
- serialize_integer (m, a[i].heap_handle, sizeof (a[i].heap_handle));
- serialize_integer (m, a[i].n_adj, sizeof (a[i].n_adj));
- serialize_integer (m, a[i].lookup_next_index, sizeof (a[i].lookup_next_index_as_int));
- switch (a[i].lookup_next_index)
- {
- case IP_LOOKUP_NEXT_LOCAL:
- serialize_integer (m, a[i].if_address_index, sizeof (a[i].if_address_index));
- break;
-
- case IP_LOOKUP_NEXT_ARP:
- serialize_integer (m, a[i].if_address_index, sizeof (a[i].if_address_index));
- serialize_integer (m, a[i].rewrite_header.sw_if_index, sizeof (a[i].rewrite_header.sw_if_index));
- break;
-
- case IP_LOOKUP_NEXT_REWRITE:
- serialize (m, serialize_vnet_rewrite, &a[i].rewrite_header, sizeof (a[i].rewrite_data));
- break;
-
- default:
- /* nothing else to serialize. */
- break;
- }
- }
-}
-
-void unserialize_vec_ip_adjacency (serialize_main_t * m, va_list * va)
-{
- ip_adjacency_t * a = va_arg (*va, ip_adjacency_t *);
- u32 n = va_arg (*va, u32);
- u32 i;
- ip_poison_adjacencies (a, n);
- for (i = 0; i < n; i++)
- {
- unserialize_integer (m, &a[i].heap_handle, sizeof (a[i].heap_handle));
- unserialize_integer (m, &a[i].n_adj, sizeof (a[i].n_adj));
- unserialize_integer (m, &a[i].lookup_next_index_as_int, sizeof (a[i].lookup_next_index_as_int));
- switch (a[i].lookup_next_index)
- {
- case IP_LOOKUP_NEXT_LOCAL:
- unserialize_integer (m, &a[i].if_address_index, sizeof (a[i].if_address_index));
- break;
-
- case IP_LOOKUP_NEXT_ARP:
- unserialize_integer (m, &a[i].if_address_index, sizeof (a[i].if_address_index));
- unserialize_integer (m, &a[i].rewrite_header.sw_if_index, sizeof (a[i].rewrite_header.sw_if_index));
- break;
-
- case IP_LOOKUP_NEXT_REWRITE:
- unserialize (m, unserialize_vnet_rewrite, &a[i].rewrite_header, sizeof (a[i].rewrite_data));
- break;
-
- default:
- /* nothing else to unserialize. */
- break;
- }
- }
-}
-
-static void serialize_vec_ip_multipath_next_hop (serialize_main_t * m, va_list * va)
-{
- ip_multipath_next_hop_t * nh = va_arg (*va, ip_multipath_next_hop_t *);
- u32 n = va_arg (*va, u32);
- u32 i;
- for (i = 0; i < n; i++)
- {
- serialize_integer (m, nh[i].next_hop_adj_index, sizeof (nh[i].next_hop_adj_index));
- serialize_integer (m, nh[i].weight, sizeof (nh[i].weight));
- }
-}
-
-static void unserialize_vec_ip_multipath_next_hop (serialize_main_t * m, va_list * va)
-{
- ip_multipath_next_hop_t * nh = va_arg (*va, ip_multipath_next_hop_t *);
- u32 n = va_arg (*va, u32);
- u32 i;
- for (i = 0; i < n; i++)
- {
- unserialize_integer (m, &nh[i].next_hop_adj_index, sizeof (nh[i].next_hop_adj_index));
- unserialize_integer (m, &nh[i].weight, sizeof (nh[i].weight));
- }
-}
-
-static void serialize_vec_ip_multipath_adjacency (serialize_main_t * m, va_list * va)
-{
- ip_multipath_adjacency_t * a = va_arg (*va, ip_multipath_adjacency_t *);
- u32 n = va_arg (*va, u32);
- u32 i;
- for (i = 0; i < n; i++)
- {
-#define foreach_ip_multipath_adjacency_field \
- _ (adj_index) _ (n_adj_in_block) _ (reference_count) \
- _ (normalized_next_hops.count) \
- _ (normalized_next_hops.heap_offset) \
- _ (normalized_next_hops.heap_handle) \
- _ (unnormalized_next_hops.count) \
- _ (unnormalized_next_hops.heap_offset) \
- _ (unnormalized_next_hops.heap_handle)
-
-#define _(f) serialize_integer (m, a[i].f, sizeof (a[i].f));
- foreach_ip_multipath_adjacency_field;
-#undef _
- }
-}
-
-static void unserialize_vec_ip_multipath_adjacency (serialize_main_t * m, va_list * va)
-{
- ip_multipath_adjacency_t * a = va_arg (*va, ip_multipath_adjacency_t *);
- u32 n = va_arg (*va, u32);
- u32 i;
- for (i = 0; i < n; i++)
- {
-#define _(f) unserialize_integer (m, &a[i].f, sizeof (a[i].f));
- foreach_ip_multipath_adjacency_field;
-#undef _
- }
-}
-
-void serialize_ip_lookup_main (serialize_main_t * m, va_list * va)
-{
- ip_lookup_main_t * lm = va_arg (*va, ip_lookup_main_t *);
-
- /* If this isn't true you need to call e.g. ip4_maybe_remap_adjacencies
- to make it true. */
- ASSERT (lm->n_adjacency_remaps == 0);
-
- serialize (m, serialize_heap, lm->adjacency_heap, serialize_vec_ip_adjacency);
-
- serialize (m, serialize_heap, lm->next_hop_heap, serialize_vec_ip_multipath_next_hop);
- vec_serialize (m, lm->multipath_adjacencies, serialize_vec_ip_multipath_adjacency);
-
- /* Adjacency counters (FIXME disabled for now). */
- if (0)
- serialize (m, serialize_vlib_combined_counter_main, &lm->adjacency_counters, /* incremental */ 0);
-}
-
-void unserialize_ip_lookup_main (serialize_main_t * m, va_list * va)
+void ip_lookup_init (ip_lookup_main_t * lm, u32 is_ip6)
{
- ip_lookup_main_t * lm = va_arg (*va, ip_lookup_main_t *);
-
- unserialize (m, unserialize_heap, &lm->adjacency_heap, unserialize_vec_ip_adjacency);
- unserialize (m, unserialize_heap, &lm->next_hop_heap, unserialize_vec_ip_multipath_next_hop);
- vec_unserialize (m, &lm->multipath_adjacencies, unserialize_vec_ip_multipath_adjacency);
-
- /* Build hash table from unserialized data. */
- {
- ip_multipath_adjacency_t * a;
-
- vec_foreach (a, lm->multipath_adjacencies)
- {
- if (a->n_adj_in_block > 0 && a->reference_count > 0)
- hash_set (lm->multipath_adjacency_by_next_hops,
- ip_next_hop_hash_key_from_handle (a->normalized_next_hops.heap_handle),
- a - lm->multipath_adjacencies);
- }
- }
+ ip_adjacency_t * adj;
+ ip_adjacency_t template_adj;
- /* Validate adjacency counters. */
- vlib_validate_combined_counter (&lm->adjacency_counters,
- vec_len (lm->adjacency_heap) - 1);
+ /* ensure that adjacency is cacheline aligned and sized */
+ ASSERT(STRUCT_OFFSET_OF(ip_adjacency_t, cacheline0) == 0);
+ ASSERT(STRUCT_OFFSET_OF(ip_adjacency_t, cacheline1) == CLIB_CACHE_LINE_BYTES);
- /* Adjacency counters (FIXME disabled for now). */
- if (0)
- unserialize (m, unserialize_vlib_combined_counter_main, &lm->adjacency_counters, /* incremental */ 0);
-}
+ lm->adj_index_by_signature = hash_create (0, sizeof (uword));
+ memset (&template_adj, 0, sizeof (template_adj));
-void ip_lookup_init (ip_lookup_main_t * lm, u32 is_ip6)
-{
- ip_adjacency_t * adj;
+ /* Preallocate three "special" adjacencies */
+ lm->adjacency_heap = aa_bootstrap (0, 3 /* n=1 free items */);
/* Hand-craft special miss adjacency to use when nothing matches in the
routing table. Same for drop adjacency. */
- adj = ip_add_adjacency (lm, /* template */ 0, /* n-adj */ 1, &lm->miss_adj_index);
+ adj = ip_add_adjacency (lm, /* template */ 0, /* n-adj */ 1,
+ &lm->miss_adj_index);
adj->lookup_next_index = IP_LOOKUP_NEXT_MISS;
ASSERT (lm->miss_adj_index == IP_LOOKUP_MISS_ADJ_INDEX);
- adj = ip_add_adjacency (lm, /* template */ 0, /* n-adj */ 1, &lm->drop_adj_index);
- adj->lookup_next_index = IP_LOOKUP_NEXT_DROP;
+ /* Make the "drop" adj sharable */
+ template_adj.lookup_next_index = IP_LOOKUP_NEXT_DROP;
+ adj = ip_add_adjacency (lm, &template_adj, /* n-adj */ 1,
+ &lm->drop_adj_index);
- adj = ip_add_adjacency (lm, /* template */ 0, /* n-adj */ 1, &lm->local_adj_index);
- adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
- adj->if_address_index = ~0;
+ /* Make the "local" adj sharable */
+ template_adj.lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
+ template_adj.if_address_index = ~0;
+ adj = ip_add_adjacency (lm, &template_adj, /* n-adj */ 1,
+ &lm->local_adj_index);
if (! lm->fib_result_n_bytes)
lm->fib_result_n_bytes = sizeof (uword);
lm->local_next_by_ip_protocol[i] = IP_LOCAL_NEXT_PUNT;
lm->builtin_protocol_by_ip_protocol[i] = IP_BUILTIN_PROTOCOL_UNKNOWN;
}
-#if 0
- /* Eliot's TCP doesn't actually work */
- lm->local_next_by_ip_protocol[IP_PROTOCOL_TCP] = IP_LOCAL_NEXT_TCP_LOOKUP;
- lm->builtin_protocol_by_ip_protocol[IP_PROTOCOL_TCP] =
- IP_BUILTIN_PROTOCOL_TCP;
-#endif
lm->local_next_by_ip_protocol[IP_PROTOCOL_UDP] = IP_LOCAL_NEXT_UDP_LOOKUP;
lm->local_next_by_ip_protocol[is_ip6 ? IP_PROTOCOL_ICMP6 : IP_PROTOCOL_ICMP] = IP_LOCAL_NEXT_ICMP;
lm->builtin_protocol_by_ip_protocol[IP_PROTOCOL_UDP] = IP_BUILTIN_PROTOCOL_UDP;
lm->builtin_protocol_by_ip_protocol[is_ip6 ? IP_PROTOCOL_ICMP6 : IP_PROTOCOL_ICMP] = IP_BUILTIN_PROTOCOL_ICMP;
}
+
+ ip_init_registered_adjacencies(!is_ip6);
}
u8 * format_ip_flow_hash_config (u8 * s, va_list * args)
{
u32 flow_hash_config = va_arg (*args, u32);
-
+
#define _(n,v) if (flow_hash_config & v) s = format (s, "%s ", #n);
foreach_flow_hash_bit;
#undef _
u8 * format_ip_lookup_next (u8 * s, va_list * args)
{
- ip_lookup_next_t n = va_arg (*args, ip_lookup_next_t);
+ ip_lookup_main_t * lm = va_arg (*args, ip_lookup_main_t *);
+ ip_lookup_next_t n = va_arg (*args, u32);
+ ip_adj_register_t *reg;
+
char * t = 0;
switch (n)
{
default:
- s = format (s, "unknown %d", n);
+ vec_validate(lm->registered_adjacencies, n);
+ reg = vec_elt_at_index(lm->registered_adjacencies, n);
+ if (reg->node_name) {
+ s = format (s, "%s:", reg->node_name);
+ }
return s;
case IP_LOOKUP_NEXT_MISS: t = "miss"; break;
case IP_LOOKUP_NEXT_CLASSIFY: t = "classify"; break;
case IP_LOOKUP_NEXT_MAP: t = "map"; break;
case IP_LOOKUP_NEXT_MAP_T: t = "map-t"; break;
- case IP_LOOKUP_NEXT_SIXRD: t = "sixrd"; break;
+ case IP_LOOKUP_NEXT_INDIRECT: t="indirect"; break;
case IP_LOOKUP_NEXT_REWRITE:
break;
}
return format (s, "%U", format_ip4_address_and_length, a, ia->address_length);
}
+u32 vnet_register_special_adjacency_format_function
+(ip_lookup_main_t * lm, format_function_t * fp)
+{
+ u32 rv;
+ /*
+ * Initialize the format function registration vector
+ * Index 0 must be invalid, to avoid finding and fixing trivial bugs
+ * all over the place
+ */
+ if (vec_len (lm->special_adjacency_format_functions) == 0)
+ {
+ vec_add1 (lm->special_adjacency_format_functions,
+ (format_function_t *) 0);
+ }
+
+ rv = vec_len (lm->special_adjacency_format_functions);
+ vec_add1 (lm->special_adjacency_format_functions, fp);
+ return rv;
+}
+
+/** @brief Pretty print helper function for formatting specific adjacencies.
+ @param s - input string to format
+ @param args - other args passed to format function such as:
+ - vnet_main_t
+ - ip_lookup_main_t
+ - adj_index
+*/
u8 * format_ip_adjacency (u8 * s, va_list * args)
{
vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
ip_lookup_main_t * lm = va_arg (*args, ip_lookup_main_t *);
u32 adj_index = va_arg (*args, u32);
ip_adjacency_t * adj = ip_get_adjacency (lm, adj_index);
+ ip_adj_register_t *reg;
+ if (adj->lookup_next_index < vec_len (lm->registered_adjacencies))
+ {
+ reg = vec_elt_at_index(lm->registered_adjacencies,
+ adj->lookup_next_index);
+ if (reg->fn)
+ {
+ s = format(s, " %U", reg->fn, lm, adj);
+ goto format_done;
+ }
+ }
+
switch (adj->lookup_next_index)
{
case IP_LOOKUP_NEXT_REWRITE:
s = format (s, "%U",
format_vnet_rewrite,
- vnm->vlib_main, &adj->rewrite_header, sizeof (adj->rewrite_data));
+ vnm->vlib_main, &adj->rewrite_header,
+ sizeof (adj->rewrite_data));
break;
-
+
+ case IP_LOOKUP_NEXT_ARP:
+ if (adj->if_address_index != ~0)
+ s = format (s, " %U", format_ip_interface_address, lm,
+ adj->if_address_index);
+ if (adj->arp.next_hop.ip6.as_u64[0] || adj->arp.next_hop.ip6.as_u64[1])
+ s = format (s, " via %U", format_ip46_address,
+ &adj->arp.next_hop, IP46_TYPE_ANY);
+ break;
+ case IP_LOOKUP_NEXT_LOCAL:
+ if (adj->if_address_index != ~0)
+ s = format (s, " %U", format_ip_interface_address, lm,
+ adj->if_address_index);
+ break;
+
+ case IP_LOOKUP_NEXT_CLASSIFY:
+ s = format (s, " table %d", adj->classify.table_index);
+ break;
+ case IP_LOOKUP_NEXT_INDIRECT:
+ s = format (s, " via %U", format_ip46_address,
+ &adj->indirect.next_hop, IP46_TYPE_ANY);
+ break;
+
default:
- s = format (s, "%U", format_ip_lookup_next, adj->lookup_next_index);
- if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP)
- s = format (s, " %U",
- format_vnet_sw_interface_name,
- vnm,
- vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index));
- switch (adj->lookup_next_index)
- {
- case IP_LOOKUP_NEXT_ARP:
- case IP_LOOKUP_NEXT_LOCAL:
- if (adj->if_address_index != ~0)
- s = format (s, " %U", format_ip_interface_address, lm, adj->if_address_index);
- break;
-
- case IP_LOOKUP_NEXT_CLASSIFY:
- s = format (s, " table %d", adj->classify_table_index);
-
- default:
- break;
- }
+ s = format (s, " unknown %d", adj->lookup_next_index);
break;
}
+
+ format_done:
if (adj->explicit_fib_index != ~0 && adj->explicit_fib_index != 0)
s = format (s, " lookup fib index %d", adj->explicit_fib_index);
+ if (adj->share_count > 0)
+ s = format (s, " shared %d", adj->share_count + 1);
+ if (adj->next_adj_with_signature)
+ s = format (s, " next_adj_with_signature %d", adj->next_adj_with_signature);
return s;
}
if (unformat (input, "arp %U %U",
unformat_vnet_sw_interface, vnm, &sw_if_index,
- unformat_ip46_address, &a46, is_ip6))
+ unformat_ip46_address, &a46, is_ip6?IP46_TYPE_IP6:IP46_TYPE_IP4))
{
ip_lookup_main_t * lm = is_ip6 ? &ip6_main.lookup_main : &ip4_main.lookup_main;
ip_adjacency_t * a_adj;
if (next == IP_LOOKUP_NEXT_LOCAL)
(void) unformat (input, "%d", &adj->if_address_index);
else if (next == IP_LOOKUP_NEXT_CLASSIFY)
- if (!unformat (input, "%d", &adj->classify_table_index))
- {
- clib_warning ("classify adj must specify table index");
- return 0;
- }
+ {
+ if (!unformat (input, "%d", &adj->classify.table_index))
+ {
+ clib_warning ("classify adj must specify table index");
+ return 0;
+ }
+ }
+ else if (next == IP_LOOKUP_NEXT_DROP)
+ {
+ adj->rewrite_header.node_index = 0;
+ }
}
else if (unformat_user (input,
goto done;
}
- if (vec_len(ip4_via_next_hops))
- {
- if (sw_if_indices[0] == (u32)~0)
- {
- u32 ai;
- uword * p;
- u32 fib_index;
- ip_adjacency_t *nh_adj;
-
- p = hash_get (ip4_main.fib_index_by_table_id, table_ids[0]);
- if (p == 0)
- {
- error = clib_error_return (0, "Nonexistent FIB id %d",
- table_ids[0]);
- goto done;
- }
-
- fib_index = p[0];
-
- ai = ip4_fib_lookup_with_table (&ip4_main,
- fib_index,
- ip4_via_next_hops,
- 1 /* disable default route */);
- if (ai == 0)
- {
- error = clib_error_return (0, "next hop %U not in FIB",
- format_ip4_address,
- ip4_via_next_hops);
- goto done;
- }
- nh_adj = ip_get_adjacency (&ip4_main.lookup_main, ai);
- vec_add1 (add_adj, nh_adj[0]);
- }
- }
- if (vec_len(ip6_via_next_hops))
- {
- if (sw_if_indices[0] == (u32)~0)
- {
- u32 ai;
- uword * p;
- u32 fib_index;
- ip_adjacency_t *nh_adj;
-
- p = hash_get (ip6_main.fib_index_by_table_id, table_ids[0]);
- if (p == 0)
- {
- error = clib_error_return (0, "Nonexistent FIB id %d",
- table_ids[0]);
- goto done;
- }
-
- fib_index = p[0];
- ai = ip6_fib_lookup_with_table (&ip6_main,
- fib_index,
- ip6_via_next_hops);
- if (ai == 0)
- {
- error = clib_error_return (0, "next hop %U not in FIB",
- format_ip6_address,
- ip6_via_next_hops);
- goto done;
- }
- nh_adj = ip_get_adjacency (&ip6_main.lookup_main, ai);
- vec_add1 (add_adj, nh_adj[0]);
- }
- }
-
{
int i;
ip4_main_t * im4 = &ip4_main;
.short_help = "Internet protocol version 6 (IP6) show commands",
};
+/*?
+ * To add or delete routes, use ip route add / del
+ * @cliexpar
+ * @cliexstart{ip route}
+ * To add or delete straightforward static routes, use ip route add / del:
+ * vpp# ip route add 6.0.1.2/32 via 6.0.0.1 GigabitEthernet2/0/0
+ * vpp# ip route del 6.0.1.2/32 via 6.0.0.1 GigabitEthernet2/0/0
+ *
+ * Multiple routes
+ *
+ * Mainly for route add/del performance testing, one can add or delete
+ * multiple routes by adding 'count N' to the previous item:
+ * vpp# ip route add count 10 7.0.0.0/24 via 6.0.0.1 GigabitEthernet2/0/0
+ *
+ * Multipath
+ *
+ * Add multiple routes for the same destination to create equal-cost multipath:
+ * vpp# ip route add 7.0.0.1/32 via 6.0.0.1 GigabitEthernet2/0/0
+ * vpp# ip route add 7.0.0.1/32 via 6.0.0.2 GigabitEthernet2/0/0
+ *
+ * For unequal-cost multipath, specify the desired weights:
+ * vpp# ip route add 7.0.0.1/32 via 6.0.0.1 GigabitEthernet2/0/0 weight 1
+ * vpp# ip route add 7.0.0.1/32 via 6.0.0.2 GigabitEthernet2/0/0 weight 3
+ *
+ * This combination of weights results in 3/4 of the traffic following the second path, 1/4 following the first path.
+ * @cliexend
+ ?*/
VLIB_CLI_COMMAND (ip_route_command, static) = {
.path = "ip route",
.short_help = "Add/delete IP routes",
.function = vnet_ip_route_cmd,
+ .is_mp_safe = 1,
};
/*
default:
clib_warning ("unknown event_type %d", event_type);
}
+ vec_reset_length (event_data);
}
done:
- vec_reset_length (event_data);
if (!resolved)
return clib_error_return (0, "Resolution failed for %U",
default:
clib_warning ("unknown event_type %d", event_type);
}
+ vec_reset_length (event_data);
}
done:
.path = "ip probe-neighbor",
.function = probe_neighbor_address,
.short_help = "ip probe-neighbor <intfc> <ip4-addr> | <ip6-addr> [retry nn]",
+ .is_mp_safe = 1,
};
typedef CLIB_PACKED (struct {
u32 index : 26;
}) ip4_route_t;
+static int
+ip4_route_cmp (void * a1, void * a2)
+{
+ ip4_route_t * r1 = a1;
+ ip4_route_t * r2 = a2;
+
+ int cmp = ip4_address_compare (&r1->address, &r2->address);
+ return cmp ? cmp : ((int) r1->address_length - (int) r2->address_length);
+}
+
static clib_error_t *
ip4_show_fib (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
{
}
}
- vec_sort (routes, r1, r2,
- ({ int cmp = ip4_address_compare (&r1->address, &r2->address);
- cmp ? cmp : ((int) r1->address_length - (int) r2->address_length); }));
+ vec_sort_with_function (routes, ip4_route_cmp);
if (vec_len(routes)) {
if (include_empty_fibs == 0)
vlib_cli_output (vm, "Table %d, fib_index %d, flow hash: %U",
msg = format (msg, "%16Ld%16Ld ", sum.packets, sum.bytes);
indent = vec_len (msg);
- msg = format (msg, "weight %d, index %d\n%U%U",
- nhs[j].weight, adj_index + i,
+ msg = format (msg, "weight %d, index %d",
+ nhs[j].weight, adj_index + i);
+
+ if (ip_adjacency_is_multipath(lm, adj_index))
+ msg = format (msg, ", multipath");
+
+ msg = format (msg, "\n%U%U",
format_white_space, indent,
format_ip_adjacency,
vnm, lm, adj_index + i);
return 0;
}
+/*?
+ * Show FIB/route entries
+ *
+ * @cliexpar
+ * @cliexstart{show ip fib}
+ * Display the IPv4 FIB.
+ * This command will run for a long time when the FIBs comprise millions of entries.
+ * vpp# sh ip fib
+ * Table 0
+ * Destination Packets Bytes Adjacency
+ * 6.0.0.0/8 0 0 weight 1, index 3
+ * arp fake-eth0 6.0.0.1/8
+ * 6.0.0.1/32 0 0 weight 1, index 4
+ * local 6.0.0.1/8
+ *
+ * And so forth. Use 'show ip fib summary' for a summary:
+ *
+ * vpp# sh ip fib summary
+ * Table 0
+ * Prefix length Count
+ * 8 1
+ * 32 4
+ * @cliexend
+ ?*/
VLIB_CLI_COMMAND (ip4_show_fib_command, static) = {
.path = "show ip fib",
.short_help = "show ip fib [mtrie] [summary] [table <n>] [<ip4-addr>] [clear] [include-empty]",
ap->count_by_prefix_length[mask_width]++;
}
+static int
+ip6_route_cmp (void * a1, void * a2)
+{
+ ip6_route_t * r1 = a1;
+ ip6_route_t * r2 = a2;
+
+ int cmp = ip6_address_compare (&r1->address, &r2->address);
+ return cmp ? cmp : ((int) r1->address_length - (int) r2->address_length);
+}
static clib_error_t *
ip6_show_fib (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
BV(clib_bihash_foreach_key_value_pair)(h, add_routes_in_fib, a);
- vec_sort (routes, r1, r2,
- ({ int cmp = ip6_address_compare (&r1->address, &r2->address);
- cmp ? cmp : ((int) r1->address_length - (int) r2->address_length); }));
+ vec_sort_with_function (routes, ip6_route_cmp);
vlib_cli_output (vm, "%=45s%=16s%=16s%=16s",
"Destination", "Packets", "Bytes", "Adjacency");
msg = format (msg, "%16Ld%16Ld ", sum.packets, sum.bytes);
indent = vec_len (msg);
- msg = format (msg, "weight %d, index %d\n%U%U",
- nhs[j].weight, adj_index + i,
+ msg = format (msg, "weight %d, index %d",
+ nhs[j].weight, adj_index + i);
+
+ if (ip_adjacency_is_multipath(lm, adj_index + i))
+ msg = format (msg, ", multipath");
+
+ msg = format (msg, "\n%U%U",
format_white_space, indent,
format_ip_adjacency,
vnm, lm, adj_index + i);
return 0;
}
+/*?
+ * Show FIB6/route entries
+ *
+ * @cliexpar
+ * @cliexstart{show ip fib}
+ * Display the IPv6 FIB.
+ * This command will run for a long time when the FIBs comprise millions of entries.
+ * See 'show ip fib'
+ * @cliexend
+ ?*/
VLIB_CLI_COMMAND (ip6_show_fib_command, static) = {
.path = "show ip6 fib",
.short_help = "show ip6 fib [summary] [clear]",