Add support for multiple microarchitectures in single binary
[vpp.git] / vnet / vnet / ip / ip4_forward.c
index bf101bb..ae2f9ee 100644 (file)
@@ -103,6 +103,14 @@ find_ip4_fib_by_table_index_or_id (ip4_main_t * im,
   fib_index = table_index_or_id;
   if (! (flags & IP4_ROUTE_FLAG_FIB_INDEX))
     {
+      if (table_index_or_id == ~0) {
+        table_index_or_id = 0;
+        while ((p = hash_get (im->fib_index_by_table_id, table_index_or_id))) {
+          table_index_or_id++;
+        }
+        return create_fib_with_table_id (im, table_index_or_id);
+      }
+
       p = hash_get (im->fib_index_by_table_id, table_index_or_id);
       if (! p)
        return create_fib_with_table_id (im, table_index_or_id);
@@ -242,7 +250,9 @@ void ip4_add_del_route (ip4_main_t * im, ip4_add_del_route_args_t * a)
   old_adj_index = fib->old_hash_values[0];
 
   /* Avoid spurious reference count increments */
-  if (old_adj_index == adj_index && !(a->flags & IP4_ROUTE_FLAG_KEEP_OLD_ADJACENCY))
+  if (old_adj_index == adj_index
+      && adj_index != ~0
+      && !(a->flags & IP4_ROUTE_FLAG_KEEP_OLD_ADJACENCY))
     {
       ip_adjacency_t * adj = ip_get_adjacency (lm, adj_index);
       if (adj->share_count > 0)
@@ -332,10 +342,12 @@ ip4_add_del_route_next_hop (ip4_main_t * im,
                }
              else
                {
-                 vnm->api_errno = VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
-                 error = clib_error_return (0, "next-hop %U/32 not in FIB",
-                                            format_ip4_address, next_hop);
-                 goto done;
+                 /* Next hop is not known, so create indirect adj */
+                 ip_adjacency_t add_adj;
+                 add_adj.lookup_next_index = IP_LOOKUP_NEXT_INDIRECT;
+                 add_adj.indirect.next_hop.ip4.as_u32 = next_hop->as_u32;
+                 add_adj.explicit_fib_index = explicit_fib_index;
+                 ip_add_adjacency (lm, &add_adj, 1, &nh_adj_index);
                }
            }
          else
@@ -624,7 +636,8 @@ always_inline uword
 ip4_lookup_inline (vlib_main_t * vm,
                   vlib_node_runtime_t * node,
                   vlib_frame_t * frame,
-                  int lookup_for_responses_to_locally_received_packets)
+                  int lookup_for_responses_to_locally_received_packets,
+                  int is_indirect)
 {
   ip4_main_t * im = &ip4_main;
   ip_lookup_main_t * lm = &im->lookup_main;
@@ -651,6 +664,7 @@ ip4_lookup_inline (vlib_main_t * vm,
          ip_adjacency_t * adj0, * adj1;
          ip4_fib_mtrie_t * mtrie0, * mtrie1;
          ip4_fib_mtrie_leaf_t leaf0, leaf1;
+         ip4_address_t * dst_addr0, *dst_addr1;
          __attribute__((unused)) u32 pi0, fib_index0, adj_index0, is_tcp_udp0;
          __attribute__((unused)) u32 pi1, fib_index1, adj_index1, is_tcp_udp1;
           u32 flow_hash_config0, flow_hash_config1;
@@ -680,6 +694,20 @@ ip4_lookup_inline (vlib_main_t * vm,
          ip0 = vlib_buffer_get_current (p0);
          ip1 = vlib_buffer_get_current (p1);
 
+         if (is_indirect)
+           {
+             ip_adjacency_t * iadj0, * iadj1;
+             iadj0 = ip_get_adjacency (lm, vnet_buffer(p0)->ip.adj_index[VLIB_TX]);
+             iadj1 = ip_get_adjacency (lm, vnet_buffer(p1)->ip.adj_index[VLIB_TX]);
+             dst_addr0 = &iadj0->indirect.next_hop.ip4;
+             dst_addr1 = &iadj1->indirect.next_hop.ip4;
+           }
+         else
+           {
+             dst_addr0 = &ip0->dst_address;
+             dst_addr1 = &ip1->dst_address;
+           }
+
          fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
          fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
           fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
@@ -695,8 +723,8 @@ ip4_lookup_inline (vlib_main_t * vm,
 
              leaf0 = leaf1 = IP4_FIB_MTRIE_LEAF_ROOT;
 
-             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->dst_address, 0);
-             leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->dst_address, 0);
+             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 0);
+             leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 0);
            }
 
          tcp0 = (void *) (ip0 + 1);
@@ -709,20 +737,20 @@ ip4_lookup_inline (vlib_main_t * vm,
 
          if (! lookup_for_responses_to_locally_received_packets)
            {
-             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->dst_address, 1);
-             leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->dst_address, 1);
+             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 1);
+             leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 1);
            }
 
          if (! lookup_for_responses_to_locally_received_packets)
            {
-             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->dst_address, 2);
-             leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->dst_address, 2);
+             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 2);
+             leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 2);
            }
 
          if (! lookup_for_responses_to_locally_received_packets)
            {
-             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->dst_address, 3);
-             leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->dst_address, 3);
+             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 3);
+             leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 3);
            }
 
          if (lookup_for_responses_to_locally_received_packets)
@@ -741,10 +769,10 @@ ip4_lookup_inline (vlib_main_t * vm,
            }
 
          ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
-                                                          &ip0->dst_address,
+                                                          dst_addr0,
                                                           /* no_default_route */ 0));
          ASSERT (adj_index1 == ip4_fib_lookup_with_table (im, fib_index1,
-                                                          &ip1->dst_address,
+                                                          dst_addr1,
                                                           /* no_default_route */ 0));
          adj0 = ip_get_adjacency (lm, adj_index0);
          adj1 = ip_get_adjacency (lm, adj_index1);
@@ -840,6 +868,7 @@ ip4_lookup_inline (vlib_main_t * vm,
          ip_adjacency_t * adj0;
          ip4_fib_mtrie_t * mtrie0;
          ip4_fib_mtrie_leaf_t leaf0;
+         ip4_address_t * dst_addr0;
          __attribute__((unused)) u32 pi0, fib_index0, adj_index0, is_tcp_udp0;
           u32 flow_hash_config0, hash_c0;
 
@@ -850,6 +879,17 @@ ip4_lookup_inline (vlib_main_t * vm,
 
          ip0 = vlib_buffer_get_current (p0);
 
+         if (is_indirect)
+           {
+             ip_adjacency_t * iadj0;
+             iadj0 = ip_get_adjacency (lm, vnet_buffer(p0)->ip.adj_index[VLIB_TX]);
+             dst_addr0 = &iadj0->indirect.next_hop.ip4;
+           }
+         else
+           {
+             dst_addr0 = &ip0->dst_address;
+           }
+
          fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
           fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
             fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
@@ -860,7 +900,7 @@ ip4_lookup_inline (vlib_main_t * vm,
 
              leaf0 = IP4_FIB_MTRIE_LEAF_ROOT;
 
-             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->dst_address, 0);
+             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 0);
            }
 
          tcp0 = (void *) (ip0 + 1);
@@ -869,13 +909,13 @@ ip4_lookup_inline (vlib_main_t * vm,
                         || ip0->protocol == IP_PROTOCOL_UDP);
 
          if (! lookup_for_responses_to_locally_received_packets)
-           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->dst_address, 1);
+           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 1);
 
          if (! lookup_for_responses_to_locally_received_packets)
-           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->dst_address, 2);
+           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 2);
 
          if (! lookup_for_responses_to_locally_received_packets)
-           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->dst_address, 3);
+           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 3);
 
          if (lookup_for_responses_to_locally_received_packets)
            adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_RX];
@@ -887,7 +927,7 @@ ip4_lookup_inline (vlib_main_t * vm,
            }
 
          ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0,
-                                                          &ip0->dst_address,
+                                                          dst_addr0,
                                                           /* no_default_route */ 0));
 
          adj0 = ip_get_adjacency (lm, adj_index0);
@@ -945,7 +985,9 @@ ip4_lookup (vlib_main_t * vm,
            vlib_node_runtime_t * node,
            vlib_frame_t * frame)
 {
-  return ip4_lookup_inline (vm, node, frame, /* lookup_for_responses_to_locally_received_packets */ 0);
+  return ip4_lookup_inline (vm, node, frame,
+                           /* lookup_for_responses_to_locally_received_packets */ 0,
+                           /* is_indirect */ 0);
 
 }
 
@@ -1312,6 +1354,30 @@ VLIB_REGISTER_NODE (ip4_lookup_node) = {
   .next_nodes = IP4_LOOKUP_NEXT_NODES,
 };
 
+VLIB_NODE_FUNCTION_MULTIARCH (ip4_lookup_node, ip4_lookup)
+
+static uword
+ip4_indirect (vlib_main_t * vm,
+               vlib_node_runtime_t * node,
+               vlib_frame_t * frame)
+{
+  return ip4_lookup_inline (vm, node, frame,
+                           /* lookup_for_responses_to_locally_received_packets */ 0,
+                           /* is_indirect */ 1);
+}
+
+VLIB_REGISTER_NODE (ip4_indirect_node) = {
+  .function = ip4_indirect,
+  .name = "ip4-indirect",
+  .vector_size = sizeof (u32),
+
+  .n_next_nodes = IP_LOOKUP_N_NEXT,
+  .next_nodes = IP4_LOOKUP_NEXT_NODES,
+};
+
+VLIB_NODE_FUNCTION_MULTIARCH (ip4_indirect_node, ip4_indirect)
+
+
 /* Global IP4 main. */
 ip4_main_t ip4_main;
 
@@ -1550,6 +1616,8 @@ VLIB_REGISTER_NODE (ip4_drop_node,static) = {
   },
 };
 
+VLIB_NODE_FUNCTION_MULTIARCH (ip4_drop_node, ip4_drop)
+
 VLIB_REGISTER_NODE (ip4_punt_node,static) = {
   .function = ip4_punt,
   .name = "ip4-punt",
@@ -1563,6 +1631,8 @@ VLIB_REGISTER_NODE (ip4_punt_node,static) = {
   },
 };
 
+VLIB_NODE_FUNCTION_MULTIARCH (ip4_punt_node, ip4_punt)
+
 VLIB_REGISTER_NODE (ip4_miss_node,static) = {
   .function = ip4_miss,
   .name = "ip4-miss",
@@ -1576,6 +1646,8 @@ VLIB_REGISTER_NODE (ip4_miss_node,static) = {
   },
 };
 
+VLIB_NODE_FUNCTION_MULTIARCH (ip4_miss_node, ip4_miss)
+
 /* Compute TCP/UDP/ICMP4 checksum in software. */
 u16
 ip4_tcp_udp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
@@ -2014,6 +2086,8 @@ VLIB_REGISTER_NODE (ip4_local_node,static) = {
   },
 };
 
+VLIB_NODE_FUNCTION_MULTIARCH (ip4_local_node, ip4_local)
+
 void ip4_register_protocol (u32 protocol, u32 node_index)
 {
   vlib_main_t * vm = vlib_get_main();
@@ -2681,6 +2755,8 @@ VLIB_REGISTER_NODE (ip4_rewrite_node) = {
   },
 };
 
+VLIB_NODE_FUNCTION_MULTIARCH (ip4_rewrite_node, ip4_rewrite_transit)
+
 VLIB_REGISTER_NODE (ip4_rewrite_local_node,static) = {
   .function = ip4_rewrite_local,
   .name = "ip4-rewrite-local",
@@ -2697,6 +2773,8 @@ VLIB_REGISTER_NODE (ip4_rewrite_local_node,static) = {
   },
 };
 
+VLIB_NODE_FUNCTION_MULTIARCH (ip4_rewrite_local_node, ip4_rewrite_local)
+
 static clib_error_t *
 add_del_interface_table (vlib_main_t * vm,
                         unformat_input_t * input,
@@ -2970,6 +3048,8 @@ VLIB_REGISTER_NODE (ip4_lookup_multicast_node,static) = {
   .next_nodes = IP4_LOOKUP_NEXT_NODES,
 };
 
+VLIB_NODE_FUNCTION_MULTIARCH (ip4_lookup_multicast_node, ip4_lookup_multicast)
+
 VLIB_REGISTER_NODE (ip4_multicast_node,static) = {
   .function = ip4_drop,
   .name = "ip4-multicast",