bfd: midchain adjacency support

Support an adjacency with lookup_next_index of IP_LOOKUP_NEXT_MIDCHAIN
so tunnel interfaces can have BFD configured on them.
Also, check if the interface a session is configured on is up
and skip assembling the packet and calculating the checksum if
the interface is down.

Change-Id: I44f76478d0fc1592e3491dd9368819a5c957e74a
Signed-off-by: Matthew Smith <mgsmith@netgate.com>
diff --git a/src/vnet/bfd/bfd_udp.c b/src/vnet/bfd/bfd_udp.c
index 147cd6e..5c0da0a 100644
--- a/src/vnet/bfd/bfd_udp.c
+++ b/src/vnet/bfd/bfd_udp.c
@@ -59,6 +59,10 @@
   u32 ip4_rewrite_idx;
   /* node index of "ip6-rewrite" node */
   u32 ip6_rewrite_idx;
+  /* node index of "ip4-midchain" node */
+  u32 ip4_midchain_idx;
+  /* node index of "ip6-midchain" node */
+  u32 ip6_midchain_idx;
   /* log class */
   vlib_log_class_t log_class;
   /* number of active udp4 sessions */
@@ -379,8 +383,14 @@
 int
 bfd_udp_calc_next_node (const struct bfd_session_s *bs, u32 * next_node)
 {
+  vnet_main_t *vnm = vnet_get_main ();
   const bfd_udp_session_t *bus = &bs->udp;
   ip_adjacency_t *adj = adj_get (bus->adj_index);
+
+  /* don't try to send the buffer if the interface is not up */
+  if (!vnet_sw_interface_is_up (vnm, bus->key.sw_if_index))
+    return 0;
+
   switch (adj->lookup_next_index)
     {
     case IP_LOOKUP_NEXT_ARP:
@@ -405,6 +415,17 @@
 	  return 1;
 	}
       break;
+    case IP_LOOKUP_NEXT_MIDCHAIN:
+      switch (bs->transport)
+	{
+	case BFD_TRANSPORT_UDP4:
+	  *next_node = bfd_udp_main.ip4_midchain_idx;
+	  return 1;
+	case BFD_TRANSPORT_UDP6:
+	  *next_node = bfd_udp_main.ip6_midchain_idx;
+	  return 1;
+	}
+      break;
     default:
       /* drop */
       break;
@@ -876,6 +897,7 @@
   BFD_UDP_INPUT_NEXT_NORMAL,
   BFD_UDP_INPUT_NEXT_REPLY_ARP,
   BFD_UDP_INPUT_NEXT_REPLY_REWRITE,
+  BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN,
   BFD_UDP_INPUT_N_NEXT,
 } bfd_udp_input_next_t;
 
@@ -902,6 +924,14 @@
     BFD_UDP_N_ERROR,
 } bfd_udp_error_t;
 
+typedef enum
+{
+  BFD_UDP_ECHO_INPUT_NEXT_NORMAL,
+  BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP,
+  BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE,
+  BFD_UDP_ECHO_INPUT_N_NEXT,
+} bfd_udp_echo_input_next_t;
+
 /* Packet counters - BFD ECHO packets */
 #define foreach_bfd_udp_echo_error(F)           \
   F (NONE, "good bfd echo packets (processed)") \
@@ -1300,6 +1330,9 @@
 		case IP_LOOKUP_NEXT_REWRITE:
 		  next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
 		  break;
+		case IP_LOOKUP_NEXT_MIDCHAIN:
+		  next0 = BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN;
+		  break;
 		default:
 		  /* drop */
 		  break;
@@ -1343,6 +1376,7 @@
               [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
               [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
               [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
+              [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip4-midchain",
       },
 };
 /* *INDENT-ON* */
@@ -1371,6 +1405,7 @@
               [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
               [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
               [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
+              [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip6-midchain",
       },
 };
 /* *INDENT-ON* */
@@ -1414,7 +1449,7 @@
       if (bfd_consume_echo_pkt (bfd_udp_main.bfd_main, b0))
 	{
 	  b0->error = rt->errors[BFD_UDP_ERROR_NONE];
-	  next0 = BFD_UDP_INPUT_NEXT_NORMAL;
+	  next0 = BFD_UDP_ECHO_INPUT_NEXT_NORMAL;
 	}
       else
 	{
@@ -1430,7 +1465,7 @@
 	      vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index,
 					   b0->error, 1);
 	    }
-	  next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
+	  next0 = BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE;
 	}
 
       bfd_unlock (bm);
@@ -1481,12 +1516,12 @@
 
   .format_trace = bfd_echo_input_format_trace,
 
-  .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
+  .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT,
   .next_nodes =
       {
-              [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
-              [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
-              [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
+              [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop",
+              [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
+              [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
       },
 };
 /* *INDENT-ON* */
@@ -1510,12 +1545,12 @@
 
   .format_trace = bfd_echo_input_format_trace,
 
-  .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
+  .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT,
   .next_nodes =
       {
-              [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
-              [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
-              [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
+              [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop",
+              [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
+              [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
       },
 };
 
@@ -1581,6 +1616,12 @@
   node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite");
   ASSERT (node);
   bfd_udp_main.ip6_rewrite_idx = node->index;
+  node = vlib_get_node_by_name (vm, (u8 *) "ip4-midchain");
+  ASSERT (node);
+  bfd_udp_main.ip4_midchain_idx = node->index;
+  node = vlib_get_node_by_name (vm, (u8 *) "ip6-midchain");
+  ASSERT (node);
+  bfd_udp_main.ip6_midchain_idx = node->index;
 
   bfd_udp_main.log_class = vlib_log_register_class ("bfd", "udp");
   vlib_log_debug (bfd_udp_main.log_class, "initialized");