Add support for multiple microarchitectures in single binary
[vpp.git] / vnet / vnet / ip / ip4_forward.c
index ef3624a..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);
@@ -181,7 +189,7 @@ ip4_fib_set_adj_index (ip4_main_t * im,
                        fib->new_hash_values);
 
       p = hash_get (hash, dst_address_u32);
-      memcpy (p, fib->new_hash_values, vec_bytes (fib->new_hash_values));
+      clib_memcpy (p, fib->new_hash_values, vec_bytes (fib->new_hash_values));
     }
 }
 
@@ -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
@@ -545,7 +557,7 @@ void ip4_maybe_remap_adjacencies (ip4_main_t * im,
            else
              {
                /* Remap to new adjacency. */
-               memcpy (fib->old_hash_values, p->value, vec_bytes (fib->old_hash_values));
+               clib_memcpy (fib->old_hash_values, p->value, vec_bytes (fib->old_hash_values));
 
                /* Set new adjacency value. */
                fib->new_hash_values[0] = p->value[0] = m - 1;
@@ -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);
 
 }
 
@@ -974,6 +1016,7 @@ void ip4_adjacency_set_interface_route (vnet_main_t * vnm,
       node_index = ip4_arp_node.index;
       adj->if_address_index = if_address_index;
       adj->arp.next_hop.ip4.as_u32 = 0;
+      ip46_address_reset(&adj->arp.next_hop);
       packet_type = VNET_L3_PACKET_TYPE_ARP;
     }
   else
@@ -1311,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;
 
@@ -1450,7 +1517,7 @@ ip4_forward_next_trace (vlib_main_t * vm,
          t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
          t0->fib_index = vec_elt (im->fib_index_by_sw_if_index, 
                              vnet_buffer(b0)->sw_if_index[VLIB_RX]);
-         memcpy (t0->packet_data,
+         clib_memcpy (t0->packet_data,
                  vlib_buffer_get_current (b0),
                  sizeof (t0->packet_data));
        }
@@ -1461,7 +1528,7 @@ ip4_forward_next_trace (vlib_main_t * vm,
          t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
          t1->fib_index = vec_elt (im->fib_index_by_sw_if_index, 
                              vnet_buffer(b1)->sw_if_index[VLIB_RX]);
-         memcpy (t1->packet_data,
+         clib_memcpy (t1->packet_data,
                  vlib_buffer_get_current (b1),
                  sizeof (t1->packet_data));
        }
@@ -1486,7 +1553,7 @@ ip4_forward_next_trace (vlib_main_t * vm,
          t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
          t0->fib_index = vec_elt (im->fib_index_by_sw_if_index, 
                              vnet_buffer(b0)->sw_if_index[VLIB_RX]);
-         memcpy (t0->packet_data,
+         clib_memcpy (t0->packet_data,
                  vlib_buffer_get_current (b0),
                  sizeof (t0->packet_data));
        }
@@ -1549,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",
@@ -1562,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",
@@ -1575,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,
@@ -2013,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();
@@ -2197,7 +2272,7 @@ ip4_arp (vlib_main_t * vm,
            hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
 
            /* Src ethernet address in ARP header. */
-           memcpy (h0->ip4_over_ethernet[0].ethernet, hw_if0->hw_address,
+           clib_memcpy (h0->ip4_over_ethernet[0].ethernet, hw_if0->hw_address,
                    sizeof (h0->ip4_over_ethernet[0].ethernet));
 
            ip4_src_address_for_packet (im, p0, &h0->ip4_over_ethernet[0].ip4, sw_if_index0);
@@ -2310,7 +2385,7 @@ ip4_probe_neighbor (vlib_main_t * vm, ip4_address_t * dst, u32 sw_if_index)
 
   hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
 
-  memcpy (h->ip4_over_ethernet[0].ethernet, hi->hw_address, sizeof (h->ip4_over_ethernet[0].ethernet));
+  clib_memcpy (h->ip4_over_ethernet[0].ethernet, hi->hw_address, sizeof (h->ip4_over_ethernet[0].ethernet));
 
   h->ip4_over_ethernet[0].ip4 = src[0];
   h->ip4_over_ethernet[1].ip4 = dst[0];
@@ -2680,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",
@@ -2696,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,
@@ -2969,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",