tunnel: support copying TTL and flow label from inner to outer
[vpp.git] / src / vnet / ipip / ipip.c
index 089e2b6..d43bcd1 100644 (file)
@@ -93,6 +93,9 @@ ipip_build_rewrite (vnet_main_t * vnm, u32 sw_if_index,
        case VNET_LINK_IP4:
          ip4->protocol = IP_PROTOCOL_IP_IN_IP;
          break;
+       case VNET_LINK_MPLS:
+         ip4->protocol = IP_PROTOCOL_MPLS_IN_IP;
+         break;
        default:
          break;
        }
@@ -121,6 +124,9 @@ ipip_build_rewrite (vnet_main_t * vnm, u32 sw_if_index,
        case VNET_LINK_IP4:
          ip6->protocol = IP_PROTOCOL_IP_IN_IP;
          break;
+       case VNET_LINK_MPLS:
+         ip6->protocol = IP_PROTOCOL_MPLS_IN_IP;
+         break;
        default:
          break;
        }
@@ -178,7 +184,7 @@ ipip46_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b,
   ip6->payload_length =
     clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) -
                          sizeof (*ip6));
-  tunnel_encap_fixup_4o6 (flags, ((ip4_header_t *) (ip6 + 1)), ip6);
+  tunnel_encap_fixup_4o6 (flags, b, ((ip4_header_t *) (ip6 + 1)), ip6);
 }
 
 static void
@@ -201,6 +207,47 @@ ipip66_fixup (vlib_main_t * vm,
   tunnel_encap_fixup_6o6 (flags, ip6 + 1, ip6);
 }
 
+static void
+ipipm6_fixup (vlib_main_t *vm, const ip_adjacency_t *adj, vlib_buffer_t *b,
+             const void *data)
+{
+  tunnel_encap_decap_flags_t flags;
+  ip6_header_t *ip6;
+
+  flags = pointer_to_uword (data);
+
+  /* Must set locally originated otherwise we're not allowed to
+     fragment the packet later and we'll get an unwanted hop-limt
+     decrement */
+  b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
+
+  ip6 = vlib_buffer_get_current (b);
+  ip6->payload_length =
+    clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) - sizeof (*ip6));
+  tunnel_encap_fixup_mplso6 (flags, b, (mpls_unicast_header_t *) (ip6 + 1),
+                            ip6);
+}
+
+static void
+ipipm4_fixup (vlib_main_t *vm, const ip_adjacency_t *adj, vlib_buffer_t *b,
+             const void *data)
+{
+  tunnel_encap_decap_flags_t flags;
+  ip4_header_t *ip4;
+
+  flags = pointer_to_uword (data);
+
+  /* Must set locally originated otherwise we'll do a TTL decrement
+   * during ip4-rewrite */
+  b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
+
+  ip4 = vlib_buffer_get_current (b);
+  ip4->length =
+    clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) - sizeof (*ip4));
+  tunnel_encap_fixup_mplso4 (flags, (mpls_unicast_header_t *) (ip4 + 1), ip4);
+  ip4->checksum = ip4_header_checksum (ip4);
+}
+
 static void
 ipip_tunnel_stack (adj_index_t ai)
 {
@@ -265,8 +312,12 @@ ipip_get_fixup (const ipip_tunnel_t * t, vnet_link_t lt, adj_flags_t * aflags)
     return (ipip66_fixup);
   if (t->transport == IPIP_TRANSPORT_IP6 && lt == VNET_LINK_IP4)
     return (ipip46_fixup);
+  if (t->transport == IPIP_TRANSPORT_IP6 && lt == VNET_LINK_MPLS)
+    return (ipipm6_fixup);
   if (t->transport == IPIP_TRANSPORT_IP4 && lt == VNET_LINK_IP6)
     return (ipip64_fixup);
+  if (t->transport == IPIP_TRANSPORT_IP4 && lt == VNET_LINK_MPLS)
+    return (ipipm4_fixup);
   if (t->transport == IPIP_TRANSPORT_IP4 && lt == VNET_LINK_IP4)
     {
       *aflags = *aflags | ADJ_FLAG_MIDCHAIN_FIXUP_IP4O4_HDR;
@@ -288,7 +339,11 @@ ipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
   if (!t)
     return;
 
-  af = ADJ_FLAG_MIDCHAIN_IP_STACK;
+  if (t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_INNER_HASH)
+    af = ADJ_FLAG_MIDCHAIN_FIXUP_FLOW_HASH;
+  else
+    af = ADJ_FLAG_MIDCHAIN_IP_STACK;
+
   if (VNET_LINK_ETHERNET == adj_get_link_type (ai))
     af |= ADJ_FLAG_MIDCHAIN_NO_COUNT;
 
@@ -317,14 +372,18 @@ mipip_mk_complete_walk (adj_index_t ai, void *data)
   af = ADJ_FLAG_NONE;
   fixup = ipip_get_fixup (ctx->t, adj_get_link_type (ai), &af);
 
+  if (ctx->t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_INNER_HASH)
+    af = ADJ_FLAG_MIDCHAIN_FIXUP_FLOW_HASH;
+  else
+    af = ADJ_FLAG_MIDCHAIN_IP_STACK;
+
   adj_nbr_midchain_update_rewrite
     (ai, fixup,
      uword_to_pointer (ctx->t->flags, void *),
-     ADJ_FLAG_MIDCHAIN_IP_STACK, ipip_build_rewrite (vnet_get_main (),
-                                                    ctx->t->sw_if_index,
-                                                    adj_get_link_type (ai),
-                                                    &teib_entry_get_nh
-                                                    (ctx->ne)->fp_addr));
+     af, ipip_build_rewrite (vnet_get_main (),
+                            ctx->t->sw_if_index,
+                            adj_get_link_type (ai),
+                            &teib_entry_get_nh (ctx->ne)->fp_addr));
 
   teib_entry_adj_stack (ctx->ne, ai);
 
@@ -364,7 +423,8 @@ mipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
   ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
   t = pool_elt_at_index (gm->tunnels, ti);
 
-  ne = teib_entry_find (sw_if_index, &adj->sub_type.nbr.next_hop);
+  ne = teib_entry_find_46 (sw_if_index,
+                          adj->ia_nh_proto, &adj->sub_type.nbr.next_hop);
 
   if (NULL == ne)
     {
@@ -560,7 +620,7 @@ static void
 ipip_teib_entry_added (const teib_entry_t * ne)
 {
   ipip_main_t *gm = &ipip_main;
-  const ip46_address_t *nh;
+  const ip_address_t *nh;
   ipip_tunnel_key_t key;
   ipip_tunnel_t *t;
   u32 sw_if_index;
@@ -587,16 +647,17 @@ ipip_teib_entry_added (const teib_entry_t * ne)
   };
   nh = teib_entry_get_peer (ne);
   adj_nbr_walk_nh (teib_entry_get_sw_if_index (ne),
-                  (ip46_address_is_ip4 (nh) ?
+                  (AF_IP4 == ip_addr_version (nh) ?
                    FIB_PROTOCOL_IP4 :
-                   FIB_PROTOCOL_IP6), nh, mipip_mk_complete_walk, &ctx);
+                   FIB_PROTOCOL_IP6),
+                  &ip_addr_46 (nh), mipip_mk_complete_walk, &ctx);
 }
 
 static void
 ipip_teib_entry_deleted (const teib_entry_t * ne)
 {
   ipip_main_t *gm = &ipip_main;
-  const ip46_address_t *nh;
+  const ip_address_t *nh;
   ipip_tunnel_key_t key;
   ipip_tunnel_t *t;
   u32 sw_if_index;
@@ -620,9 +681,10 @@ ipip_teib_entry_deleted (const teib_entry_t * ne)
 
   /* make all the adjacencies incomplete */
   adj_nbr_walk_nh (teib_entry_get_sw_if_index (ne),
-                  (ip46_address_is_ip4 (nh) ?
+                  (AF_IP4 == ip_addr_version (nh) ?
                    FIB_PROTOCOL_IP4 :
-                   FIB_PROTOCOL_IP6), nh, mipip_mk_incomplete_walk, t);
+                   FIB_PROTOCOL_IP6),
+                  &ip_addr_46 (nh), mipip_mk_incomplete_walk, t);
 }
 
 static walk_rc_t
@@ -745,12 +807,14 @@ ipip_add_tunnel (ipip_transport_t transport,
   if (t->transport == IPIP_TRANSPORT_IP6 && !gm->ip6_protocol_registered)
     {
       ip6_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip6_input_node.index);
+      ip6_register_protocol (IP_PROTOCOL_MPLS_IN_IP, ipip6_input_node.index);
       ip6_register_protocol (IP_PROTOCOL_IPV6, ipip6_input_node.index);
       gm->ip6_protocol_registered = true;
     }
   else if (t->transport == IPIP_TRANSPORT_IP4 && !gm->ip4_protocol_registered)
     {
       ip4_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip4_input_node.index);
+      ip4_register_protocol (IP_PROTOCOL_MPLS_IN_IP, ipip4_input_node.index);
       ip4_register_protocol (IP_PROTOCOL_IPV6, ipip4_input_node.index);
       gm->ip4_protocol_registered = true;
     }