Move CLI examples from wiki to code - VPP-165
[vpp.git] / vnet / vnet / ip / lookup.c
index 9e3cdc0..4713807 100644 (file)
@@ -134,6 +134,34 @@ ip_unshare_adjacency(ip_lookup_main_t * lm, u32 adj_index)
     }
 }
 
+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 *
 ip_add_adjacency (ip_lookup_main_t * lm,
@@ -209,9 +237,10 @@ ip_add_adjacency (ip_lookup_main_t * lm,
       adj[i].mcast_group_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;
@@ -915,12 +944,14 @@ void ip_lookup_init (ip_lookup_main_t * lm, u32 is_ip6)
     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 _
@@ -930,13 +961,20 @@ u8 * format_ip_flow_hash_config (u8 * s, va_list * args)
 
 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;
@@ -947,7 +985,7 @@ u8 * format_ip_lookup_next (u8 * s, va_list * args)
     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;
     }
@@ -971,49 +1009,89 @@ static u8 * format_ip_interface_address (u8 * s, va_list * args)
     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:
-         if (adj->if_address_index != ~0)
-           s = format (s, " %U", format_ip_interface_address, lm, adj->if_address_index);
-         if (adj->arp.next_hop.ip4.as_u32)
-           s = format (s, " via %U", format_ip4_address, &adj->arp.next_hop.ip4.as_u32);
-         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);
-
-       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)
@@ -1091,7 +1169,7 @@ static uword unformat_ip_adjacency (unformat_input_t * input, va_list * args)
 
   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;
@@ -1596,6 +1674,33 @@ VLIB_CLI_COMMAND (vlib_cli_show_ip6_command, static) = {
   .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",
@@ -2036,6 +2141,30 @@ ip4_show_fib (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * c
   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]",
@@ -2262,6 +2391,16 @@ ip6_show_fib (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * c
   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]",