fib: fix crash when create vxlan/vxlan-gpe/geneve/gtpu tunnel.
[vpp.git] / src / vnet / fib / fib_path.c
index ef5d58c..8d160d9 100644 (file)
@@ -106,17 +106,8 @@ typedef enum fib_path_type_t_ {
      * via a DVR.
      */
     FIB_PATH_TYPE_DVR,
-    /**
-     * Marker. Add new types before this one, then update it.
-     */
-    FIB_PATH_TYPE_LAST = FIB_PATH_TYPE_BIER_FMASK,
 } __attribute__ ((packed)) fib_path_type_t;
 
-/**
- * The maximum number of path_types
- */
-#define FIB_PATH_TYPE_MAX (FIB_PATH_TYPE_LAST + 1)
-
 #define FIB_PATH_TYPES {                                       \
     [FIB_PATH_TYPE_ATTACHED_NEXT_HOP] = "attached-nexthop",    \
     [FIB_PATH_TYPE_ATTACHED]          = "attached",            \
@@ -133,11 +124,6 @@ typedef enum fib_path_type_t_ {
     [FIB_PATH_TYPE_DVR]               = "dvr",                 \
 }
 
-#define FOR_EACH_FIB_PATH_TYPE(_item)           \
-    for (_item = FIB_PATH_TYPE_FIRST;           \
-         _item <= FIB_PATH_TYPE_LAST;           \
-         _item++)
-
 /**
  * Enurmeration of path operational (i.e. derived) attributes
  */
@@ -154,10 +140,6 @@ typedef enum fib_path_oper_attribute_t_ {
      * The path is resolved
      */
     FIB_PATH_OPER_ATTRIBUTE_RESOLVED,
-    /**
-     * The path is attached, despite what the next-hop may say.
-     */
-    FIB_PATH_OPER_ATTRIBUTE_ATTACHED,
     /**
      * The path has become a permanent drop.
      */
@@ -192,7 +174,6 @@ typedef enum fib_path_oper_flags_t_ {
     FIB_PATH_OPER_FLAG_RECURSIVE_LOOP = (1 << FIB_PATH_OPER_ATTRIBUTE_RECURSIVE_LOOP),
     FIB_PATH_OPER_FLAG_DROP = (1 << FIB_PATH_OPER_ATTRIBUTE_DROP),
     FIB_PATH_OPER_FLAG_RESOLVED = (1 << FIB_PATH_OPER_ATTRIBUTE_RESOLVED),
-    FIB_PATH_OPER_FLAG_ATTACHED = (1 << FIB_PATH_OPER_ATTRIBUTE_ATTACHED),
 } __attribute__ ((packed)) fib_path_oper_flags_t;
 
 /**
@@ -259,6 +240,10 @@ typedef struct fib_path_t_ {
            u32 fp_interface;
        } attached_next_hop;
        struct {
+           /**
+            * The Connected local address
+            */
+           fib_prefix_t fp_connected;
            /**
             * The interface
             */
@@ -282,16 +267,10 @@ typedef struct fib_path_t_ {
                     mpls_eos_bit_t fp_eos;
                 };
            } fp_nh;
-            union {
-                /**
-                 * The FIB table index in which to find the next-hop.
-                 */
-                fib_node_index_t fp_tbl_id;
-                /**
-                 * The BIER FIB the fmask is in
-                 */
-                index_t fp_bier_fib;
-            };
+            /**
+             * The FIB table index in which to find the next-hop.
+             */
+            fib_node_index_t fp_tbl_id;
        } recursive;
        struct {
             /**
@@ -369,7 +348,7 @@ typedef struct fib_path_t_ {
     STRUCT_MARK(path_hash_end);
 
     /**
-     * Memebers in this last section represent information that is
+     * Members in this last section represent information that is
      * dervied during resolution. It should not be copied to new paths
      * nor compared.
      */
@@ -645,14 +624,16 @@ fib_path_last_lock_gone (fib_node_t *node)
     ASSERT(0);
 }
 
-static void
+static fib_path_t*
 fib_path_attached_next_hop_get_adj (fib_path_t *path,
                                    vnet_link_t link,
                                     dpo_id_t *dpo)
 {
+    fib_node_index_t fib_path_index;
     fib_protocol_t nh_proto;
     adj_index_t ai;
 
+    fib_path_index = fib_path_get_index(path);
     nh_proto = dpo_proto_to_fib(path->fp_nh_proto);
 
     if (vnet_sw_interface_is_p2p(vnet_get_main(),
@@ -676,19 +657,25 @@ fib_path_attached_next_hop_get_adj (fib_path_t *path,
 
     dpo_set(dpo, DPO_ADJACENCY, vnet_link_to_dpo_proto(link), ai);
     adj_unlock(ai);
+
+    return (fib_path_get(fib_path_index));
 }
 
 static void
 fib_path_attached_next_hop_set (fib_path_t *path)
 {
+    dpo_id_t tmp = DPO_INVALID;
+
     /*
      * resolve directly via the adjacency discribed by the
      * interface and next-hop
      */
-    fib_path_attached_next_hop_get_adj(path,
-                                       dpo_proto_to_link(path->fp_nh_proto),
-                                       &path->fp_dpo);
-
+    dpo_copy (&tmp, &path->fp_dpo);
+    path = fib_path_attached_next_hop_get_adj(path,
+                                              dpo_proto_to_link(path->fp_nh_proto),
+                                              &tmp);
+    dpo_copy(&path->fp_dpo, &tmp);
+    dpo_reset(&tmp);
     ASSERT(dpo_is_adj(&path->fp_dpo));
 
     /*
@@ -742,7 +729,7 @@ fib_path_attached_get_adj (fib_path_t *path,
 
         ai = adj_glean_add_or_lock(nh_proto, link,
                                    path->attached.fp_interface,
-                                   NULL);
+                                   &path->attached.fp_connected);
         dpo_set(dpo, DPO_ADJACENCY_GLEAN, vnet_link_to_dpo_proto(link), ai);
         adj_unlock(ai);
     }
@@ -1016,6 +1003,7 @@ fib_path_back_walk_notify (fib_node_t *node,
                &path->fp_dpo);
        }
        if ((FIB_NODE_BW_REASON_FLAG_ADJ_UPDATE & ctx->fnbw_reason) ||
+            (FIB_NODE_BW_REASON_FLAG_ADJ_MTU    & ctx->fnbw_reason) ||
             (FIB_NODE_BW_REASON_FLAG_ADJ_DOWN   & ctx->fnbw_reason))
        {
            /*
@@ -1105,16 +1093,20 @@ FIXME comment
             /*
              * restack the DPO to pick up the correct DPO sub-type
              */
+            dpo_id_t tmp = DPO_INVALID;
             uword if_is_up;
 
             if_is_up = vnet_sw_interface_is_up(
                            vnet_get_main(),
                            path->attached_next_hop.fp_interface);
 
-            fib_path_attached_next_hop_get_adj(
+            dpo_copy (&tmp, &path->fp_dpo);
+            path = fib_path_attached_next_hop_get_adj(
                 path,
                 dpo_proto_to_link(path->fp_nh_proto),
-                &path->fp_dpo);
+                &tmp);
+            dpo_copy(&path->fp_dpo, &tmp);
+            dpo_reset(&tmp);
 
             path->fp_oper_flags &= ~FIB_PATH_OPER_FLAG_RESOLVED;
             if (if_is_up && adj_is_up(path->fp_dpo.dpoi_index))
@@ -1169,6 +1161,11 @@ FIXME comment
            fib_path_unresolve(path);
            path->fp_oper_flags |= FIB_PATH_OPER_FLAG_DROP;
        }
+       if (FIB_NODE_BW_REASON_FLAG_INTERFACE_BIND & ctx->fnbw_reason)
+       {
+            /* bind walks should appear here and pass silently up to
+             * to the fib_entry */
+       }
        break;
     case FIB_PATH_TYPE_UDP_ENCAP:
     {
@@ -1272,6 +1269,8 @@ fib_path_route_flags_to_cfg_flags (const fib_route_path_t *rpath)
        cfg_flags |= FIB_PATH_CFG_FLAG_ICMP_UNREACH;
     if (rpath->frp_flags & FIB_ROUTE_PATH_ICMP_PROHIBIT)
        cfg_flags |= FIB_PATH_CFG_FLAG_ICMP_PROHIBIT;
+    if (rpath->frp_flags & FIB_ROUTE_PATH_GLEAN)
+       cfg_flags |= FIB_PATH_CFG_FLAG_GLEAN;
 
     return (cfg_flags);
 }
@@ -1375,6 +1374,12 @@ fib_path_create (fib_node_index_t pl_index,
         path->fp_type = FIB_PATH_TYPE_SPECIAL;
         path->classify.fp_classify_table_id = rpath->frp_classify_table_id;
     }
+    else if (path->fp_cfg_flags & FIB_PATH_CFG_FLAG_GLEAN)
+    {
+        path->fp_type = FIB_PATH_TYPE_ATTACHED;
+        path->attached.fp_interface = rpath->frp_sw_if_index;
+        path->attached.fp_connected = rpath->frp_connected;
+    }
     else if (~0 != rpath->frp_sw_if_index)
     {
         if (ip46_address_is_zero(&rpath->frp_addr))
@@ -1488,7 +1493,7 @@ fib_path_copy (fib_node_index_t path_index,
     orig_path = fib_path_get(path_index);
     ASSERT(NULL != orig_path);
 
-    memcpy(path, orig_path, sizeof(*path));
+    clib_memcpy(path, orig_path, sizeof(*path));
 
     FIB_PATH_DBG(path, "create-copy:%d", path_index);
 
@@ -1502,6 +1507,12 @@ fib_path_copy (fib_node_index_t path_index,
     clib_memset(&path->fp_dpo, 0, sizeof(path->fp_dpo));
     dpo_reset(&path->fp_dpo);
 
+    if (path->fp_type == FIB_PATH_TYPE_EXCLUSIVE)
+    {
+       clib_memset(&path->exclusive.fp_ex_dpo, 0, sizeof(dpo_id_t));
+       dpo_copy(&path->exclusive.fp_ex_dpo, &orig_path->exclusive.fp_ex_dpo);
+    }
+
     return (fib_path_get_index(path));
 }
 
@@ -1870,7 +1881,8 @@ fib_path_recursive_loop_detect (fib_node_index_t path_index,
     }
     case FIB_PATH_TYPE_ATTACHED_NEXT_HOP:
     case FIB_PATH_TYPE_ATTACHED:
-       if (adj_recursive_loop_detect(path->fp_dpo.dpoi_index,
+       if (dpo_is_adj(&path->fp_dpo) &&
+            adj_recursive_loop_detect(path->fp_dpo.dpoi_index,
                                       entry_indicies))
        {
            FIB_PATH_DBG(path, "recursive loop formed");
@@ -1986,7 +1998,11 @@ fib_path_resolve (fib_node_index_t path_index)
        }
        else
        {
-           fib_prefix_from_ip46_addr(&path->recursive.fp_nh.fp_ip, &pfx);
+           ASSERT(!ip46_address_is_zero(&path->recursive.fp_nh.fp_ip));
+
+           fib_protocol_t fp = (ip46_address_is_ip4(&path->recursive.fp_nh.fp_ip) ?
+                                        FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
+           fib_prefix_from_ip46_addr(fp, &path->recursive.fp_nh.fp_ip, &pfx);
        }
 
         fib_table_lock(path->recursive.fp_tbl_id,
@@ -2114,7 +2130,7 @@ fib_path_resolve (fib_node_index_t path_index)
         break;
     }
     case FIB_PATH_TYPE_DVR:
-        dvr_dpo_add_or_lock(path->attached.fp_interface,
+        dvr_dpo_add_or_lock(path->dvr.fp_interface,
                             path->fp_nh_proto,
                             &path->fp_dpo);
         break;
@@ -2335,10 +2351,12 @@ fib_path_contribute_urpf (fib_node_index_t path_index,
     case FIB_PATH_TYPE_DVR:
        fib_urpf_list_append(urpf, path->dvr.fp_interface);
        break;
+    case FIB_PATH_TYPE_UDP_ENCAP:
+        fib_urpf_list_append(urpf, path->udp_encap.fp_udp_encap_id);
+       break;
     case FIB_PATH_TYPE_DEAG:
     case FIB_PATH_TYPE_RECEIVE:
     case FIB_PATH_TYPE_INTF_RX:
-    case FIB_PATH_TYPE_UDP_ENCAP:
     case FIB_PATH_TYPE_BIER_FMASK:
     case FIB_PATH_TYPE_BIER_TABLE:
     case FIB_PATH_TYPE_BIER_IMP:
@@ -2413,6 +2431,7 @@ fib_path_stack_mpls_disp (fib_node_index_t path_index,
 void
 fib_path_contribute_forwarding (fib_node_index_t path_index,
                                fib_forward_chain_type_t fct,
+                                dpo_proto_t payload_proto,
                                dpo_id_t *dpo)
 {
     fib_path_t *path;
@@ -2420,7 +2439,6 @@ fib_path_contribute_forwarding (fib_node_index_t path_index,
     path = fib_path_get(path_index);
 
     ASSERT(path);
-    ASSERT(FIB_FORW_CHAIN_TYPE_MPLS_EOS != fct);
 
     /*
      * The DPO stored in the path was created when the path was resolved.
@@ -2438,19 +2456,35 @@ fib_path_contribute_forwarding (fib_node_index_t path_index,
        case FIB_PATH_TYPE_ATTACHED_NEXT_HOP:
            switch (fct)
            {
+           case FIB_FORW_CHAIN_TYPE_MPLS_EOS: {
+                    dpo_id_t tmp = DPO_INVALID;
+                    dpo_copy (&tmp, dpo);
+                    path = fib_path_attached_next_hop_get_adj(
+                           path,
+                           dpo_proto_to_link(payload_proto),
+                           &tmp);
+                    dpo_copy (dpo, &tmp);
+                    dpo_reset(&tmp);
+                    break;
+            }
            case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
            case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
-           case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
            case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
            case FIB_FORW_CHAIN_TYPE_ETHERNET:
            case FIB_FORW_CHAIN_TYPE_NSH:
            case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
            case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
-               fib_path_attached_next_hop_get_adj(
-                        path,
-                        fib_forw_chain_type_to_link_type(fct),
-                         dpo);
-               break;
+                {
+                    dpo_id_t tmp = DPO_INVALID;
+                    dpo_copy (&tmp, dpo);
+                    path = fib_path_attached_next_hop_get_adj(
+                           path,
+                           fib_forw_chain_type_to_link_type(fct),
+                           &tmp);
+                    dpo_copy (dpo, &tmp);
+                    dpo_reset(&tmp);
+                    break;
+                }
            case FIB_FORW_CHAIN_TYPE_BIER:
                break;
            }
@@ -2546,10 +2580,25 @@ fib_path_contribute_forwarding (fib_node_index_t path_index,
         case FIB_PATH_TYPE_ATTACHED:
            switch (fct)
            {
+           case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
+                /*
+                 * End of stack traffic via an attacehd path (a glean)
+                 * must forace an IP lookup so that the IP packet can
+                 * match against any installed adj-fibs
+                 */
+                lookup_dpo_add_or_lock_w_fib_index(
+                    fib_table_get_index_for_sw_if_index(
+                        dpo_proto_to_fib(payload_proto),
+                        path->attached.fp_interface),
+                    payload_proto,
+                    LOOKUP_UNICAST,
+                    LOOKUP_INPUT_DST_ADDR,
+                    LOOKUP_TABLE_FROM_CONFIG,
+                    dpo);
+                break;
            case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
            case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
            case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
-           case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
            case FIB_FORW_CHAIN_TYPE_ETHERNET:
            case FIB_FORW_CHAIN_TYPE_NSH:
             case FIB_FORW_CHAIN_TYPE_BIER:
@@ -2595,8 +2644,8 @@ fib_path_contribute_forwarding (fib_node_index_t path_index,
             /*
              * Create the adj needed for sending IP multicast traffic
              */
-            interface_rx_dpo_add_or_lock(fib_forw_chain_type_to_dpo_proto(fct),
-                                         path->attached.fp_interface,
+            interface_rx_dpo_add_or_lock(payload_proto,
+                                         path->intf_rx.fp_interface,
                                          dpo);
             break;
         case FIB_PATH_TYPE_UDP_ENCAP:
@@ -2616,6 +2665,7 @@ fib_path_contribute_forwarding (fib_node_index_t path_index,
 load_balance_path_t *
 fib_path_append_nh_for_multipath_hash (fib_node_index_t path_index,
                                       fib_forward_chain_type_t fct,
+                                       dpo_proto_t payload_proto,
                                       load_balance_path_t *hash_key)
 {
     load_balance_path_t *mnh;
@@ -2632,7 +2682,7 @@ fib_path_append_nh_for_multipath_hash (fib_node_index_t path_index,
 
     if (fib_path_is_resolved(path_index))
     {
-        fib_path_contribute_forwarding(path_index, fct, &mnh->path_dpo);
+        fib_path_contribute_forwarding(path_index, fct, payload_proto, &mnh->path_dpo);
     }
     else
     {
@@ -2818,7 +2868,7 @@ show_fib_path_command (vlib_main_t * vm,
                            FIB_PATH_FORMAT_FLAGS_NONE);
            s = format(s, "\n  children:");
            s = fib_node_children_format(path->fp_node.fn_children, s);
-           vlib_cli_output (vm, "%s", s);
+           vlib_cli_output (vm, "%v", s);
            vec_free(s);
        }
        else
@@ -2829,11 +2879,11 @@ show_fib_path_command (vlib_main_t * vm,
     else
     {
        vlib_cli_output (vm, "FIB Paths");
-       pool_foreach_index (pi, fib_path_pool,
-       ({
+       pool_foreach_index (pi, fib_path_pool)
+        {
            vlib_cli_output (vm, "%U", format_fib_path, pi, 0,
                              FIB_PATH_FORMAT_FLAGS_NONE);
-       }));
+       }
     }
 
     return (NULL);