Initial commit of vpp code.

Change-Id: Ib246f1fbfce93274020ee93ce461e3d8bd8b9f17
Signed-off-by: Ed Warnicke <eaw@cisco.com>
diff --git a/build-root/emacs-lisp/all-skel.el b/build-root/emacs-lisp/all-skel.el
new file mode 100644
index 0000000..afc2d37
--- /dev/null
+++ b/build-root/emacs-lisp/all-skel.el
@@ -0,0 +1,11 @@
+;; list of clib / vlib / vnet / vpp skeleton files
+
+(load-file "./cli-cmd-skel.el")
+(load-file "./pipe-skel.el")
+(load-file "./dual-loop-skel.el")
+(load-file "./periodic-skel.el")
+(load-file "./config-skel.el")
+(load-file "./tunnel-c-skel.el")
+(load-file "./tunnel-h-skel.el")
+(load-file "./tunnel-encap-skel.el")
+(load-file "./tunnel-decap-skel.el")
diff --git a/build-root/emacs-lisp/cli-cmd-skel.el b/build-root/emacs-lisp/cli-cmd-skel.el
new file mode 100644
index 0000000..8a83cd1
--- /dev/null
+++ b/build-root/emacs-lisp/cli-cmd-skel.el
@@ -0,0 +1,32 @@
+;;; cli-cmd-skel.el - cli command skeleton
+
+(require 'skeleton)
+
+(define-skeleton cli-cmd-skel
+"Insert a CLI command "
+nil
+'(setq cmd-name (skeleton-read "Command Name: "))
+'(setq path (skeleton-read "Path: "))
+
+"
+static clib_error_t *
+" cmd-name "_command_fn (vlib_main_t * vm,
+		 unformat_input_t * input,
+		 vlib_cli_command_t * cmd)
+{
+    while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
+	if (unformat (input, \"whatever %d\", &whatever))
+	    ;
+	else
+	    return clib_error_return (0, \"unknown input `%U'\",
+				      format_unformat_error, input);
+    }
+    return 0;
+}
+
+VLIB_CLI_COMMAND (" cmd-name "_command, static) = {
+    .path = \"" path "\",
+    .short_help = \"" path "\",
+    .function = " cmd-name "_command_fn,
+};
+")
diff --git a/build-root/emacs-lisp/config-skel.el b/build-root/emacs-lisp/config-skel.el
new file mode 100644
index 0000000..c9a1eb6
--- /dev/null
+++ b/build-root/emacs-lisp/config-skel.el
@@ -0,0 +1,28 @@
+;;; config-skel.el - config function command skeleton
+
+(require 'skeleton)
+
+(define-skeleton config-skel
+"Insert a vlib config skeleton "
+nil
+'(setq cfg-name (skeleton-read "Config Class Name: "))
+
+"
+static clib_error_t *
+" cfg-name "_config (vlib_main_t * vm, unformat_input_t * input)
+{
+    u32 whatever;
+
+    while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
+	if (unformat (input, \"whatever %d\", &whatever))
+	    ;
+	else
+	    return clib_error_return (0, \"unknown input `%U'\",
+				      format_unformat_error, input);
+    }
+
+    return 0;
+}
+
+VLIB_CONFIG_FUNCTION (" cfg-name "_config, \"" cfg-name "\");
+")
diff --git a/build-root/emacs-lisp/dual-loop-skel.el b/build-root/emacs-lisp/dual-loop-skel.el
new file mode 100644
index 0000000..fafd21a
--- /dev/null
+++ b/build-root/emacs-lisp/dual-loop-skel.el
@@ -0,0 +1,290 @@
+;;; dual-loop-skel.el - Eliotic dual-loop node skeleton
+
+(require 'skeleton)
+
+(define-skeleton dual-loop-skel
+"Insert a skeleton dual-loop graph node"
+nil
+'(setq node-name (skeleton-read "Node Name: "))
+'(setq uc-node-name (upcase node-name))
+"
+#include <vlib/vlib.h>
+#include <vnet/vnet.h>
+#include <vnet/pg/pg.h>
+#include <clib/error.h>
+
+#include <vnet/ip/ip.h>
+#include <vnet/ethernet/ethernet.h>
+
+#include <clib/hash.h>
+#include <clib/error.h>
+#include <clib/elog.h>
+
+typedef struct {
+  /* convenience */
+  vlib_main_t * vlib_main;
+  vnet_main_t * vnet_main;
+  ethernet_main_t * ethernet_main;
+} " node-name "_main_t;
+
+" node-name "_main_t " node-name "_main;
+
+vlib_node_registration_t " node-name "_node;
+
+typedef struct {
+  u32 next_index;
+  u32 sw_if_index;
+} " node-name "_trace_t;
+
+/* packet trace format function */
+static u8 * format_" node-name "_trace (u8 * s, va_list * args)
+{
+  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
+  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
+  " node-name "_trace_t * t = va_arg (*args, " node-name "_trace_t *);
+  
+  s = format (s, \"" uc-node-name ": sw_if_index %d, next index %d\",
+              t->sw_if_index, t->next_index);
+  return s;
+}
+
+vlib_node_registration_t " node-name "_node;
+
+#define foreach_" node-name "_error \\
+_(SWAPPED, \"Mac swap packets processed\")
+
+typedef enum {
+#define _(sym,str) " uc-node-name "_ERROR_##sym,
+  foreach_" node-name "_error
+#undef _
+  " uc-node-name "_N_ERROR,
+} " node-name "_error_t;
+
+static char * " node-name "_error_strings[] = {
+#define _(sym,string) string,
+  foreach_" node-name "_error
+#undef _
+};
+
+typedef enum {
+  " uc-node-name "_NEXT_INTERFACE_OUTPUT,
+  " uc-node-name "_N_NEXT,
+} " node-name "_next_t;
+
+#define foreach_mac_address_offset              \\
+_(0)                                            \\
+_(1)                                            \\
+_(2)                                            \\
+_(3)                                            \\
+_(4)                                            \\
+_(5)
+
+static uword
+" node-name "_node_fn (vlib_main_t * vm,
+		  vlib_node_runtime_t * node,
+		  vlib_frame_t * frame)
+{
+  u32 n_left_from, * from, * to_next;
+  " node-name "_next_t next_index;
+  u32 pkts_swapped = 0;
+
+  from = vlib_frame_vector_args (frame);
+  n_left_from = frame->n_vectors;
+  next_index = node->cached_next_index;
+
+  while (n_left_from > 0)
+    {
+      u32 n_left_to_next;
+
+      vlib_get_next_frame (vm, node, next_index,
+			   to_next, n_left_to_next);
+
+      while (n_left_from >= 4 && n_left_to_next >= 2)
+	{
+          u32 next0 = " uc-node-name "_NEXT_INTERFACE_OUTPUT;
+          u32 next1 = " uc-node-name "_NEXT_INTERFACE_OUTPUT;
+          u32 sw_if_index0, sw_if_index1;
+          u8 tmp0[6], tmp1[6];
+          ethernet_header_t *en0, *en1;
+          u32 bi0, bi1;
+	  vlib_buffer_t * b0, * b1;
+          
+	  /* Prefetch next iteration. */
+	  {
+	    vlib_buffer_t * p2, * p3;
+            
+	    p2 = vlib_get_buffer (vm, from[2]);
+	    p3 = vlib_get_buffer (vm, from[3]);
+            
+	    vlib_prefetch_buffer_header (p2, LOAD);
+	    vlib_prefetch_buffer_header (p3, LOAD);
+
+	    CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
+	    CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
+	  }
+
+          /* speculatively enqueue b0 and b1 to the current next frame */
+	  to_next[0] = bi0 = from[0];
+	  to_next[1] = bi1 = from[1];
+	  from += 2;
+	  to_next += 2;
+	  n_left_from -= 2;
+	  n_left_to_next -= 2;
+
+	  b0 = vlib_get_buffer (vm, bi0);
+	  b1 = vlib_get_buffer (vm, bi1);
+
+          /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
+          ASSERT (b0->current_data == 0);
+          ASSERT (b1->current_data == 0);
+          
+          en0 = vlib_buffer_get_current (b0);
+          en1 = vlib_buffer_get_current (b1);
+
+          /* This is not the fastest way to swap src + dst mac addresses */
+#define _(a) tmp0[a] = en0->src_address[a];
+          foreach_mac_address_offset;
+#undef _
+#define _(a) en0->src_address[a] = en0->dst_address[a];
+          foreach_mac_address_offset;
+#undef _
+#define _(a) en0->dst_address[a] = tmp0[a];
+          foreach_mac_address_offset;
+#undef _
+
+#define _(a) tmp1[a] = en1->src_address[a];
+          foreach_mac_address_offset;
+#undef _
+#define _(a) en1->src_address[a] = en1->dst_address[a];
+          foreach_mac_address_offset;
+#undef _
+#define _(a) en1->dst_address[a] = tmp1[a];
+          foreach_mac_address_offset;
+#undef _
+
+          sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
+          sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
+
+          /* Send pkt back out the RX interface */
+          vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
+          vnet_buffer(b1)->sw_if_index[VLIB_TX] = sw_if_index1;
+
+          pkts_swapped += 2;
+          /* $$$$$ End of processing 2 x packets $$$$$ */
+
+          if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
+            {
+              if (b0->flags & VLIB_BUFFER_IS_TRACED) 
+                {
+                    " node-name "_trace_t *t = 
+                      vlib_add_trace (vm, node, b0, sizeof (*t));
+                    t->sw_if_index = sw_if_index0;
+                    t->next_index = next0;
+                  }
+                if (b1->flags & VLIB_BUFFER_IS_TRACED) 
+                  {
+                    " node-name "_trace_t *t = 
+                      vlib_add_trace (vm, node, b1, sizeof (*t));
+                    t->sw_if_index = sw_if_index1;
+                    t->next_index = next1;
+                  }
+              }
+            
+            /* verify speculative enqueues, maybe switch current next frame */
+            vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
+                                             to_next, n_left_to_next,
+                                             bi0, bi1, next0, next1);
+        }
+
+      while (n_left_from > 0 && n_left_to_next > 0)
+	{
+          u32 bi0;
+	  vlib_buffer_t * b0;
+          u32 next0 = " uc-node-name "_NEXT_INTERFACE_OUTPUT;
+          u32 sw_if_index0;
+          u8 tmp0[6];
+          ethernet_header_t *en0;
+
+          /* speculatively enqueue b0 to the current next frame */
+	  bi0 = from[0];
+	  to_next[0] = bi0;
+	  from += 1;
+	  to_next += 1;
+	  n_left_from -= 1;
+	  n_left_to_next -= 1;
+
+	  b0 = vlib_get_buffer (vm, bi0);
+
+          /* $$$$$ Single loop: process 1  packet here $$$$$ */
+
+          /* 
+           * Direct from the driver, we should be at offset 0
+           * aka at &b0->data[0]
+           */
+          ASSERT (b0->current_data == 0);
+
+          en0 = vlib_buffer_get_current (b0);
+
+          /* This is not the fastest way to swap src + dst mac addresses */
+#define _(a) tmp0[a] = en0->src_address[a];
+          foreach_mac_address_offset;
+#undef _
+#define _(a) en0->src_address[a] = en0->dst_address[a];
+          foreach_mac_address_offset;
+#undef _
+#define _(a) en0->dst_address[a] = tmp0[a];
+          foreach_mac_address_offset;
+#undef _
+
+          sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
+
+          /* Send pkt back out the RX interface */
+          vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
+
+          if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) 
+                            && (b0->flags & VLIB_BUFFER_IS_TRACED))) 
+            {
+              " node-name "_trace_t *t = 
+                 vlib_add_trace (vm, node, b0, sizeof (*t));
+              t->sw_if_index = sw_if_index0;
+              t->next_index = next0;
+            }
+            
+          pkts_swapped += 1;
+
+          /* $$$$$ Done processing 1 packet here $$$$$ */
+
+          /* verify speculative enqueue, maybe switch current next frame */
+	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
+					   to_next, n_left_to_next,
+					   bi0, next0);
+	}
+
+      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
+    }
+
+  vlib_node_increment_counter (vm, " node-name "_node.index, 
+                               " uc-node-name "_ERROR_SWAPPED, pkts_swapped);
+  return frame->n_vectors;
+}
+
+VLIB_REGISTER_NODE (" node-name "_node) = {
+  .function = " node-name "_node_fn,
+  .name = \"" node-name "\",
+  .vector_size = sizeof (u32),
+  .format_trace = format_" node-name "_trace,
+  .type = VLIB_NODE_TYPE_INTERNAL,
+  
+  .n_errors = ARRAY_LEN(" node-name "_error_strings),
+  .error_strings = " node-name "_error_strings,
+
+  .n_next_nodes = " uc-node-name "_N_NEXT,
+
+  /* edit / add dispositions here */
+  .next_nodes = {
+        [" uc-node-name "_NEXT_INTERFACE_OUTPUT] = \"interface-output\",
+  },
+};
+
+")
+
diff --git a/build-root/emacs-lisp/periodic-skel.el b/build-root/emacs-lisp/periodic-skel.el
new file mode 100644
index 0000000..37c2085
--- /dev/null
+++ b/build-root/emacs-lisp/periodic-skel.el
@@ -0,0 +1,86 @@
+;;; pipe-skel.el - pipelined graph node skeleton
+
+(require 'skeleton)
+
+(define-skeleton periodic-skel
+"Insert a skeleton periodic process node"
+nil
+'(setq node-name (skeleton-read "Name: "))
+'(setq uc-node-name (upcase node-name))
+'(setq poll-period (skeleton-read "Poll period (f64 seconds, e.g. 10.0): "))
+
+"
+#define " uc-node-name "_POLL_PERIOD " poll-period "
+
+static uword
+" node-name "_process (vlib_main_t * vm,
+                       vlib_node_runtime_t * rt,
+                       vlib_frame_t * f)
+{
+    f64 poll_time_remaining;
+    uword event_type, * event_data = 0;
+
+    poll_time_remaining = " uc-node-name "_POLL_PERIOD;
+    while (1) {
+        int i;
+
+        /* 
+         * Sleep until next periodic call due, or until we receive event(s) 
+         */
+        poll_time_remaining = 
+            vlib_process_wait_for_event_or_clock (vm, poll_time_remaining);
+        
+        event_type = vlib_process_get_events (vm, &event_data);
+        switch (event_type) {
+        case ~0:                /* no events => timeout */
+            break;
+
+        /* 
+         * $$$$ FIXME: add cases / handlers for each event type 
+         */
+        case EVENT1:
+            for (i = 0; i < vec_len (event_data); i++) 
+                handle_event1 (mm, event_data[i]);
+            break;
+
+        case EVENT2:
+            for (i = 0; i < vec_len (event_data); i++) 
+                handle_event2 (vm, event_data[i]);
+	    break;
+
+        /* ... and so forth for each event type */
+
+        default:
+            /* This should never happen... */
+            clib_warning (\"BUG: unhandled event type %d\", event_type);
+            break;
+        }
+        if (event_data)
+            _vec_len (event_data) = 0;
+
+        /* Timer expired, call periodic function */
+        if (vlib_process_suspend_time_is_zero (poll_time_remaining)) {
+            " node-name "_periodic (vm);
+            poll_time_remaining = " uc-node-name "_POLL_PERIOD;
+        }
+    }
+
+    return 0;
+}
+
+/*
+ * " node-name " periodic node declaration 
+ */
+static VLIB_REGISTER_NODE (" node-name "_node) = {
+    .function = " node-name "_process,
+    .type = VLIB_NODE_TYPE_PROCESS,
+    .name = \"" node-name "-process\",
+};
+
+/*
+ * To signal an event:
+ *
+ * vlib_process_signal_event (vm, " node-name "_node.index, EVENTn, datum);
+ *
+ */
+")
diff --git a/build-root/emacs-lisp/pipe-skel.el b/build-root/emacs-lisp/pipe-skel.el
new file mode 100644
index 0000000..8ad25c0
--- /dev/null
+++ b/build-root/emacs-lisp/pipe-skel.el
@@ -0,0 +1,132 @@
+;;; pipe-skel.el - pipelined graph node skeleton
+
+(require 'skeleton)
+
+(define-skeleton pipeline-node-skel
+"Insert a skeleton pipelined graph node"
+nil
+'(setq node-name (skeleton-read "Node Name: "))
+'(setq uc-node-name (upcase node-name))
+'(setq nstages (skeleton-read "Number of pipeline stages: "))
+"
+#include <vlib/vlib.h>
+#include <clib/error.h>
+
+/*
+ * Dump these counters via the \"show error\" CLI command 
+ * FIXME: Add packet counter / error strings as desired
+ */
+
+#define foreach_" node-name "_error \\
+_(ERROR1, \"sample counter/ error string\")
+
+static char * " node-name "_error_strings[] = {
+#define _(sym,string) string,
+  foreach_" node-name "_error
+#undef _
+};
+
+/*
+ * packet error / counter enumeration
+ *
+ * To count and drop a vlib_buffer_t *b:
+ *
+ *     Set b->error = node->errors[" uc-node-name "_ERROR_xxx];
+ *     last_stage returns a disposition index bound to \"error-drop\"
+ * 
+ * To manually increment the specific counter " uc-node-name "_ERROR1
+ *
+ *  vlib_node_t *n = vlib_get_node (vm, " node-name ".index);
+ *  u32 node_counter_base_index = n->error_heap_index;
+ *  vlib_error_main_t * em = &vm->error_main;
+ *  em->counters[node_counter_base_index + " uc-node-name "_ERROR1] += 1;
+ * 
+ */
+
+typedef enum {
+#define _(sym,str) " uc-node-name "_ERROR_##sym,
+    foreach_" node-name "_error
+#undef _
+    " uc-node-name "_N_ERROR,
+} " node-name "_error_t;
+
+/*
+ * enumeration of per-packet dispositions
+ * FIXME: add dispositions as desired
+ */
+
+typedef enum { \n"
+"    " uc-node-name "_NEXT_NORMAL,\n"
+"    " uc-node-name "_N_NEXT,
+} " node-name "_next_t;
+
+#define NSTAGES " nstages "
+
+/* 
+ * Use the generic buffer metadata + first line of packet data prefetch
+ * stage function from <api/pipeline.h>. This is usually a Good Idea.
+ */
+#define stage0 generic_stage0
+
+/* 
+ * FIXME: add stage functions. Here is the function prototype:
+ * 
+ * static inline void stageN (vlib_main_t * vm,
+ *                            vlib_node_runtime_t * node,
+ *                            u32 buffer_index)
+ */
+
+/*
+ * FIXME: the last pipeline stage returns the desired pkt next node index,
+ * from the " node-name "_next_t enum above
+ */
+static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
+                              u32 bi)
+{
+    vlib_buffer_t *b = vlib_get_buffer (vm, bi);
+
+    b->error = node->errors[EXAMPLE_ERROR_ERROR1];
+
+    return " uc-node-name "_NEXT_NORMAL;
+}
+
+#include <api/pipeline.h>
+
+static uword " node-name "_node_fn (vlib_main_t * vm,
+                              vlib_node_runtime_t * node,
+                              vlib_frame_t * frame)
+{
+    return dispatch_pipeline (vm, node, frame);
+}
+
+static VLIB_REGISTER_NODE (example_node) = {
+  .function = " node-name "_node_fn,
+  .name = \"" node-name "-node\",
+  .vector_size = sizeof (u32),
+  .type = VLIB_NODE_TYPE_INTERNAL,
+  
+  .n_errors = ARRAY_LEN(" node-name "_error_strings),
+  .error_strings = " node-name "_error_strings,
+
+  .n_next_nodes = " uc-node-name "_N_NEXT,
+
+  /* edit / add dispositions here */
+  .next_nodes = {
+        [" uc-node-name "_NEXT_NORMAL] = \"error-drop\",
+  },
+};
+
+/* 
+ * packet generator definition to push superframes of data into the
+ * new graph node. Cut and paste into <file>, then
+ * \"exec <file>\", \"pa enable test\" at the QVNET prompt...
+ * 
+packet-generator new {
+  name test
+  limit 100
+  node " node-name "-node
+  size 374-374
+  data { hex 0x02b46b96000100096978676265000500bf436973636f20494f5320536f6674776172652c2043333735304520536f66747761726520284333373530452d554e4956455253414c2d4d292c2056657273696f6e2031322e32283335295345352c2052454c4541534520534f4654574152452028666331290a436f707972696768742028632920313938362d3230303720627920436973636f2053797374656d732c20496e632e0a436f6d70696c6564205468752031392d4a756c2d30372031363a3137206279206e616368656e00060018636973636f2057532d4333373530452d3234544400020011000000010101cc0004000000000003001b54656e4769676162697445746865726e6574312f302f3100040008000000280008002400000c011200000000ffffffff010221ff000000000000001e7a50f000ff000000090004000a00060001000b0005010012000500001300050000160011000000010101cc000400000000001a00100000000100000000ffffffff }
+}
+ */
+")
diff --git a/build-root/emacs-lisp/tunnel-c-skel.el b/build-root/emacs-lisp/tunnel-c-skel.el
new file mode 100644
index 0000000..6f1096c
--- /dev/null
+++ b/build-root/emacs-lisp/tunnel-c-skel.el
@@ -0,0 +1,441 @@
+;;; tunnel-c-skel.el - tunnel encap cli / api
+
+(require 'skeleton)
+
+(define-skeleton tunnel-c-skel
+"Insert a tunnel cli/api implementation"
+nil
+'(setq encap_stack (skeleton-read "encap_stack (e.g ip4_udp_lisp): "))
+'(setq ENCAP_STACK (upcase encap_stack))
+'(setq encap-stack (replace-regexp-in-string "_" "-" encap_stack))
+"
+#include <vnet/" encap-stack "/" encap_stack" .h>
+
+" encap_stack "_main_t " encap_stack "_main;
+
+static u8 * format_decap_next (u8 * s, va_list * args)
+{
+  u32 next_index = va_arg (*args, u32);
+
+  switch (next_index)
+    {
+    case " ENCAP_STACK "_INPUT_NEXT_DROP:
+      return format (s, \"drop\");
+    case " ENCAP_STACK "_INPUT_NEXT_IP4_INPUT:
+      return format (s, \"ip4\");
+    case " ENCAP_STACK "_INPUT_NEXT_IP6_INPUT:
+      return format (s, \"ip6\");
+    case " ENCAP_STACK "_INPUT_NEXT_" ENCAP_STACK "_ENCAP:
+      return format (s, \"" encap-stack "\");
+    default:
+      return format (s, \"unknown %d\", next_index);
+    }
+  return s;
+}
+
+u8 * format_" encap_stack "_tunnel (u8 * s, va_list * args)
+{
+  " encap_stack "_tunnel_t * t = va_arg (*args, " encap_stack "_tunnel_t *);
+  " encap_stack "_main_t * ngm = &" encap_stack "_main;
+
+  s = format (s, 
+              \"[%d] %U (src) %U (dst) fibs: encap %d, decap %d\",
+              t - ngm->tunnels,
+              format_ip4_address, &t->src,
+              format_ip4_address, &t->dst,
+              t->encap_fib_index,
+              t->decap_fib_index);
+
+  s = format (s, \" decap next %U\\n\", format_decap_next, t->decap_next_index);
+  /* FIXME: add protocol details */
+  return s;
+}
+
+static u8 * format_" encap_stack "_name (u8 * s, va_list * args)
+{
+  u32 dev_instance = va_arg (*args, u32);
+  return format (s, \"" encap_stack "_tunnel%d\", dev_instance);
+}
+
+static uword dummy_interface_tx (vlib_main_t * vm,
+                                 vlib_node_runtime_t * node,
+                                 vlib_frame_t * frame)
+{
+  clib_warning (\"you shouldn't be here, leaking buffers...\");
+  return frame->n_vectors;
+}
+
+VNET_DEVICE_CLASS (" encap_stack "_device_class,static) = {
+  .name = "" ENCAP_STACK "",
+  .format_device_name = format_" encap_stack "_name,
+  .format_tx_trace = format_" encap_stack "_encap_trace,
+  .tx_function = dummy_interface_tx,
+};
+
+static uword dummy_set_rewrite (vnet_main_t * vnm,
+                                u32 sw_if_index,
+                                u32 l3_type,
+                                void * dst_address,
+                                void * rewrite,
+                                uword max_rewrite_bytes)
+{
+  return 0;
+}
+
+u8 * format_" encap_stack "_header_with_length (u8 * s, va_list * args)
+{
+  " encap_stack "_header_t * h = va_arg (*args, " encap_stack "_header_t *);
+  u32 max_header_bytes = va_arg (*args, u32);
+  u32 header_bytes;
+
+  header_bytes = sizeof (h[0]);
+  if (max_header_bytes != 0 && header_bytes > max_header_bytes)
+    return format (s, \"" encap-stack "header truncated\");
+
+  /* FIXME: pretty-print an " encap_stack " header */
+
+  return s;
+}
+
+VNET_HW_INTERFACE_CLASS (" encap_stack "_hw_class) = {
+  .name = \"" ENCAP_STACK "\",
+  .format_header = format_" encap_stack "_header_with_length,
+  .set_rewrite = dummy_set_rewrite,
+};
+
+#define foreach_copy_field                      \
+_(src.as_u32)                                   \
+_(dst.as_u32)                                   \
+_(encap_fib_index)                              \
+_(decap_fib_index)                              \
+_(decap_next_index)                             \
+_(FIXME_ADD_ALL_COPY_FIELDS )
+
+static int " encap_stack "_rewrite (" encap_stack "_tunnel_t * t)
+{
+  u8 *rw = 0;
+  ip4_header_t * ip0;
+  " encap_stack "_header_t * h0;
+  int len;
+
+  len = sizeof (*h0);
+
+  vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
+
+  h0 = (ip4_udp_" encap_stack "_header_t *) rw;
+
+  /* FIXME: build the actual header here... */
+
+  /* Fixed portion of the (outer) ip4 header */
+  ip0 = &h0->ip4;
+  ip0->ip_version_and_header_length = 0x45;
+  ip0->ttl = 254;
+  ip0->protocol = IP_PROTOCOL_UDP;
+
+  /* we'll fix up the ip4 header length and checksum after-the-fact */
+  ip0->src_address.as_u32 = t->src.as_u32;
+  ip0->dst_address.as_u32 = t->dst.as_u32;
+  ip0->checksum = ip4_header_checksum (ip0);
+
+  /* UDP header, randomize src port on something, maybe? */
+  h0->udp.src_port = clib_host_to_net_u16 (4341);
+  h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_" encap_stack ");
+
+  /* $$$ build a <mumble> tunnel header here */
+  
+  t->rewrite = rw;
+  return (0);
+}
+
+int vnet_" encap_stack "_add_del_tunnel 
+(vnet_" encap_stack "_add_del_tunnel_args_t *a, u32 * hw_if_indexp)
+{
+  " encap_stack "_main_t * ngm = &" encap_stack "_main;
+  " encap_stack "_tunnel_t *t = 0;
+  vnet_main_t * vnm = ngm->vnet_main;
+  vnet_hw_interface_t * hi;
+  uword * p;
+  u32 hw_if_index = ~0;
+  int rv;
+  " encap_stack "_tunnel_key_t key, *key_copy;
+  hash_pair_t *hp;
+  
+  key.FIXME = clib_host_to_net_XXX(FIXME);
+
+  p = hash_get_mem (ngm->" encap_stack "_tunnel_by_key, &key);
+  
+  if (a->is_add)
+    {
+      /* adding a tunnel: tunnel must not already exist */
+      if (p) 
+        return VNET_API_ERROR_INVALID_VALUE;
+      
+      pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
+      memset (t, 0, sizeof (*t));
+      
+      /* copy from arg structure */
+#define _(x) t->x = a->x;
+      foreach_copy_field;
+#undef _
+      
+      rv = " encap_stack "_rewrite (t);
+
+      if (rv)
+        {
+          pool_put (ngm->tunnels, t);
+          return rv;
+        }
+
+      /* $$$$ use a simple hash if you can ... */
+      key_copy = clib_mem_alloc (sizeof (*key_copy));
+      memcpy (key_copy, &key, sizeof (*key_copy));
+
+      hash_set_mem (ngm->" encap_stack "_tunnel_by_key, key_copy, 
+                    t - ngm->tunnels);
+      
+      /* 
+       * interface freelist / recycle shtik
+       * This simple implementation rapidly reuses freed tunnel interfaces.
+       * Consider whether to refcount, etc. etc.
+       */ 
+      if (vec_len (ngm->free_" encap_stack "_tunnel_hw_if_indices) > 0)
+        {
+          hw_if_index = ngm->free_" encap_stack "_tunnel_hw_if_indices
+            [vec_len (ngm->free_" encap_stack "_tunnel_hw_if_indices)-1];
+          _vec_len (ngm->free_" encap_stack "_tunnel_hw_if_indices) -= 1;
+          
+          hi = vnet_get_hw_interface (vnm, hw_if_index);
+          hi->dev_instance = t - ngm->tunnels;
+          hi->hw_instance = hi->dev_instance;
+        }
+      else 
+        {
+          hw_if_index = vnet_register_interface
+            (vnm, " encap_stack "_device_class.index, t - ngm->tunnels,
+             " encap_stack "_hw_class.index, t - ngm->tunnels);
+          hi = vnet_get_hw_interface (vnm, hw_if_index);
+          hi->output_node_index = " encap_stack "_encap_node.index;
+        }
+      
+      t->hw_if_index = hw_if_index;
+      
+      vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 
+                                   VNET_SW_INTERFACE_FLAG_ADMIN_UP);
+    }
+  else
+    {
+      /* deleting a tunnel: tunnel must exist */
+      if (!p) 
+        return VNET_API_ERROR_NO_SUCH_ENTRY;
+
+      t = pool_elt_at_index (ngm->tunnels, p[0]);
+
+      vnet_sw_interface_set_flags (vnm, t->hw_if_index, 0 /* down */);
+      vec_add1 (ngm->free_" encap_stack "_tunnel_hw_if_indices, t->hw_if_index);
+
+      hp = hash_get_pair (ngm->" encap_stack "_tunnel_by_key, &key);
+      key_copy = (void *)(hp->key);
+      hash_unset_mem (ngm->" encap_stack "_tunnel_by_key, &key);
+      clib_mem_free (key_copy);
+
+      vec_free (t->rewrite);
+      pool_put (ngm->tunnels, t);
+    }
+
+  if (hw_if_indexp)
+      *hw_if_indexp = hw_if_index;
+
+  return 0;
+}
+
+static u32 fib_index_from_fib_id (u32 fib_id)
+{
+  ip4_main_t * im = &ip4_main;
+  uword * p;
+
+  p = hash_get (im->fib_index_by_table_id, fib_id);
+  if (!p)
+    return ~0;
+
+  return p[0];
+}
+
+static uword unformat_decap_next (unformat_input_t * input, va_list * args)
+{
+  u32 * result = va_arg (*args, u32 *);
+  u32 tmp;
+  
+  if (unformat (input, \"drop\"))
+    *result = " ENCAP_STACK "_INPUT_NEXT_DROP;
+  else if (unformat (input, \"ip4\"))
+    *result = " ENCAP_STACK "_INPUT_NEXT_IP4_INPUT;
+  else if (unformat (input, \"ip6\"))
+    *result = " ENCAP_STACK "_INPUT_NEXT_IP6_INPUT;
+  else if (unformat (input, \"ethernet\"))
+    *result = " ENCAP_STACK "_INPUT_NEXT_IP6_INPUT;
+  else if (unformat (input, \"" encap-stack "\"))
+    *result = " ENCAP_STACK "_INPUT_NEXT_" ENCAP_STACK "_ENCAP;
+  else if (unformat (input, \"%d\", &tmp))
+    *result = tmp;
+  else
+    return 0;
+  return 1;
+}
+
+static clib_error_t *
+" encap_stack "_add_del_tunnel_command_fn (vlib_main_t * vm,
+                                   unformat_input_t * input,
+                                   vlib_cli_command_t * cmd)
+{
+  unformat_input_t _line_input, * line_input = &_line_input;
+  ip4_address_t src, dst;
+  u8 is_add = 1;
+  u8 src_set = 0;
+  u8 dst_set = 0;
+  u32 encap_fib_index = 0;
+  u32 decap_fib_index = 0;
+  u8 next_protocol = " ENCAP_STACK "_NEXT_PROTOCOL_IP4;
+  u32 decap_next_index = " ENCAP_STACK "_INPUT_NEXT_IP4_INPUT;
+  u8 flags = " ENCAP_STACK "_FLAGS_P;
+  u8 ver_res = 0;
+  u8 res = 0;
+  u32 iid = 0;
+  u8 iid_set = 0;
+  u32 tmp;
+  int rv;
+  vnet_" encap_stack "_add_del_tunnel_args_t _a, * a = &_a;
+  
+  /* Get a line of input. */
+  if (! unformat_user (input, unformat_line_input, line_input))
+    return 0;
+
+  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
+    if (unformat (line_input, \"del\"))
+      is_add = 0;
+    else if (unformat (line_input, \"src %U\", 
+                       unformat_ip4_address, &src))
+      src_set = 1;
+    else if (unformat (line_input, \"dst %U\",
+                       unformat_ip4_address, &dst))
+      dst_set = 1;
+    else if (unformat (line_input, \"encap-vrf-id %d\", &tmp))
+      {
+        encap_fib_index = fib_index_from_fib_id (tmp);
+        if (encap_fib_index == ~0)
+          return clib_error_return (0, \"nonexistent encap fib id %d\", tmp);
+      }
+    else if (unformat (line_input, \"decap-vrf-id %d\", &tmp))
+      {
+        decap_fib_index = fib_index_from_fib_id (tmp);
+        if (decap_fib_index == ~0)
+          return clib_error_return (0, \"nonexistent decap fib id %d\", tmp);
+      }
+    else if (unformat (line_input, \"decap-next %U\", unformat_decap_next, 
+                       &decap_next_index))
+      ;
+    else if (unformat(line_input, \"next-ip4\"))
+      next_protocol = 1;
+    else if (unformat(line_input, \"next-ip6\"))
+      next_protocol = 2;
+    else if (unformat(line_input, \"next-ethernet\"))
+      next_protocol = 3;
+    else if (unformat(line_input, \"next-nsh\"))
+      next_protocol = 4;
+    /* 
+     * $$$ allow the user to specify anything they want 
+     * in the " ENCAP_STACK " header
+     */
+    else 
+      return clib_error_return (0, \"parse error: '%U'\", 
+                                format_unformat_error, line_input);
+  }
+
+  unformat_free (line_input);
+
+  if (src_set == 0)
+    return clib_error_return (0, \"tunnel src address not specified\");
+
+  if (dst_set == 0)
+    return clib_error_return (0, \"tunnel dst address not specified\");
+
+  memset (a, 0, sizeof (*a));
+
+  a->is_add = is_add;
+
+#define _(x) a->x = x;
+  foreach_copy_field;
+#undef _
+  
+  rv = vnet_" encap_stack "_add_del_tunnel (a, 0 /* hw_if_indexp */);
+
+  switch(rv)
+    {
+    case 0:
+      break;
+    case VNET_API_ERROR_INVALID_VALUE:
+      return clib_error_return (0, \"tunnel already exists...\");
+
+    case VNET_API_ERROR_NO_SUCH_ENTRY:
+      return clib_error_return (0, \"tunnel does not exist...\");
+
+    default:
+      return clib_error_return 
+        (0, \"vnet_" encap_stack "_add_del_tunnel returned %d\", rv);
+    }
+
+  return 0;
+}
+
+VLIB_CLI_COMMAND (create_" encap_stack "_tunnel_command, static) = {
+  .path = \"lisp gpe tunnel\",
+  .short_help = 
+  \"<mumble> tunnel src <ip4-addr> dst <ip4-addr>\\n\"
+  \"    [encap-fib-id <nn>] [decap-fib-id <nn>]\\n\"
+  \"    [decap-next [ip4|ip6|ethernet|nsh-encap|<nn>]][del]\\n\",
+  .function = " encap_stack "_add_del_tunnel_command_fn,
+};
+
+static clib_error_t *
+show_" encap_stack "_tunnel_command_fn (vlib_main_t * vm,
+                                unformat_input_t * input,
+                                vlib_cli_command_t * cmd)
+{
+  " encap_stack "_main_t * ngm = &" encap_stack "_main;
+  " encap_stack "_tunnel_t * t;
+  
+  if (pool_elts (ngm->tunnels) == 0)
+    vlib_cli_output (vm, \"No lisp-gpe tunnels configured...\");
+
+  pool_foreach (t, ngm->tunnels,
+  ({
+    vlib_cli_output (vm, \"%U\", format_" encap_stack "_tunnel);
+  }));
+  
+  return 0;
+}
+
+VLIB_CLI_COMMAND (show_" encap_stack "_tunnel_command, static) = {
+    .path = \"show lisp gpe tunnel\",
+    .function = show_" encap_stack "_tunnel_command_fn,
+};
+
+clib_error_t *" encap_stack "_init (vlib_main_t *vm)
+{
+  " encap_stack "_main_t *ngm = &" encap_stack "_main;
+  
+  ngm->vnet_main = vnet_get_main();
+  ngm->vlib_main = vm;
+  
+  ngm->" encap_stack "_tunnel_by_key 
+    = hash_create_mem (0, sizeof(" encap_stack "_tunnel_key_t), sizeof (uword));
+
+  /* YMMV, register with the local netstack */
+  udp_register_dst_port (vm, UDP_DST_PORT_" encap_stack ", 
+                         " encap_stack "_input_node.index, 1 /* is_ip4 */);
+  return 0;
+}
+
+VLIB_INIT_FUNCTION(" encap_stack "_init);
+
+")
+  
diff --git a/build-root/emacs-lisp/tunnel-decap-skel.el b/build-root/emacs-lisp/tunnel-decap-skel.el
new file mode 100644
index 0000000..fc6f3a1
--- /dev/null
+++ b/build-root/emacs-lisp/tunnel-decap-skel.el
@@ -0,0 +1,299 @@
+;;; tunnel-decap-skel.el - tunnel decapsulation skeleton
+
+(require 'skeleton)
+
+(define-skeleton tunnel-decap-skel
+"Insert a tunnel decap implementation"
+nil
+'(setq encap_stack (skeleton-read "encap_stack (e.g ip4_udp_lisp): "))
+'(setq ENCAP_STACK (upcase encap_stack))
+'(setq encap-stack (replace-regexp-in-string "_" "-" encap_stack))
+'(setq ENCAP-STACK (upcase encap-stack))
+"
+#include <vlib/vlib.h>
+#include <vnet/pg/pg.h>
+#include <vnet/" encap-stack "/" encap_stack ".h>
+
+typedef struct {
+  u32 next_index;
+  u32 tunnel_index;
+  u32 error;
+  " encap_stack "_header_t h;
+} " encap_stack "_rx_trace_t;
+
+static u8 * format_" encap_stack "_rx_trace (u8 * s, va_list * args)
+{
+  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
+  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
+  " encap_stack "_rx_trace_t * t = va_arg (*args, " encap_stack "_rx_trace_t *);
+
+  if (t->tunnel_index != ~0)
+    {
+      s = format (s, \"" ENCAP-STACK ": tunnel %d next %d error %d\", 
+                  t->tunnel_index, t->next_index, t->error);
+    }
+  else
+    {
+      s = format (s, \"" ENCAP-STACK ": no tunnel next %d error %d\\n\", 
+                  t->next_index, t->error);
+    }
+  s = format (s, \"\\n  %U\", format_" encap_stack "_header_with_length, &t->h, 
+              (u32) sizeof (t->h) /* max size */);
+  return s;
+}
+
+static uword
+" encap_stack "_input (vlib_main_t * vm,
+                     vlib_node_runtime_t * node,
+                     vlib_frame_t * from_frame)
+{
+  u32 n_left_from, next_index, * from, * to_next;
+  " encap_stack "_main_t * ngm = &" encap_stack "_main;
+  u32 last_tunnel_index = ~0;
+  " encap_stack "_tunnel_key_t last_key;
+  u32 pkts_decapsulated = 0;
+
+  memset (&last_key, 0xff, sizeof (last_key));
+
+  from = vlib_frame_vector_args (from_frame);
+  n_left_from = from_frame->n_vectors;
+
+  next_index = node->cached_next_index;
+
+  while (n_left_from > 0)
+    {
+      u32 n_left_to_next;
+
+      vlib_get_next_frame (vm, node, next_index,
+			   to_next, n_left_to_next);
+
+#if 0 /* $$$ dual loop when the single loop works */
+      while (n_left_from >= 4 && n_left_to_next >= 2)
+	{
+	  u32 bi0, bi1;
+	  vlib_buffer_t * b0, * b1;
+	  nsh_unicast_header_t * h0, * h1;
+          u32 label0, label1;
+	  u32 next0, next1;
+	  uword * p0, * p1;
+
+	  /* Prefetch next iteration. */
+	  {
+	    vlib_buffer_t * p2, * p3;
+
+	    p2 = vlib_get_buffer (vm, from[2]);
+	    p3 = vlib_get_buffer (vm, from[3]);
+
+	    vlib_prefetch_buffer_header (p2, LOAD);
+	    vlib_prefetch_buffer_header (p3, LOAD);
+
+	    CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
+	    CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
+	  }
+
+	  bi0 = from[0];
+	  bi1 = from[1];
+	  to_next[0] = bi0;
+	  to_next[1] = bi1;
+	  from += 2;
+	  to_next += 2;
+	  n_left_to_next -= 2;
+	  n_left_from -= 2;
+
+	  b0 = vlib_get_buffer (vm, bi0);
+	  b1 = vlib_get_buffer (vm, bi1);
+
+          h0 = vlib_buffer_get_current (b0);
+          h1 = vlib_buffer_get_current (b1);
+          
+          next0 = next1 = " ENCAP_STACK "_INPUT_NEXT_IP4_INPUT;
+
+          label0 = clib_net_to_host_u32 (h0->label_exp_s_ttl);
+          label1 = clib_net_to_host_u32 (h1->label_exp_s_ttl);
+
+	  /* 
+	   * Translate label contents into a fib index.
+	   * This is a decent sanity check, and guarantees
+	   * a sane FIB for the downstream lookup
+	   */
+          label0 = vnet_nsh_uc_get_label (label0);
+          label1 = vnet_nsh_uc_get_label (label1);
+
+          /* If 2xlabels match, and match the 1-wide cache, use it */
+          if (label0 == label1 && rt->last_label == label0)
+            {
+              vnet_buffer(b0)->sw_if_index[VLIB_TX] = rt->last_fib_index;
+              vnet_buffer(b1)->sw_if_index[VLIB_TX] = rt->last_fib_index;
+            }
+          else
+            {
+              p0 = hash_get (rt->mm->fib_index_by_nsh_label, label0);
+              if (PREDICT_FALSE (p0 == 0))
+                {
+                  next0 = " ENCAP_STACK "_INPUT_NEXT_DROP;
+                  b0->error = node->errors[NSH_ERROR_BAD_LABEL];
+                }
+              else
+                vnet_buffer(b0)->sw_if_index[VLIB_TX] = p0[0];
+              
+              p1 = hash_get (rt->mm->fib_index_by_nsh_label, label1);
+              if (PREDICT_FALSE (p1 == 0))
+                {
+                  next1 = " ENCAP_STACK "_INPUT_NEXT_DROP;
+                  b1->error = node->errors[NSH_ERROR_BAD_LABEL];
+                }
+              else
+                {
+                  vnet_buffer(b1)->sw_if_index[VLIB_TX] = p1[0];
+                  rt->last_fib_index = p1[0];
+                  rt->last_label = label1;
+                }
+            }
+
+          if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
+            {
+              nsh_rx_trace_t *tr = vlib_add_trace (vm, node, 
+                                                   b0, sizeof (*tr));
+              tr->label_exp_s_ttl = label0;
+            }
+          if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) 
+            {
+              nsh_rx_trace_t *tr = vlib_add_trace (vm, node, 
+                                                   b1, sizeof (*tr));
+              tr->label_exp_s_ttl = label1;
+            }
+
+          vlib_buffer_advance (b0, sizeof (*h0));
+          vlib_buffer_advance (b1, sizeof (*h1));
+
+	  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
+					   to_next, n_left_to_next,
+					   bi0, bi1, next0, next1);
+	}
+#endif 
+    
+      while (n_left_from > 0 && n_left_to_next > 0)
+	{
+	  u32 bi0;
+	  vlib_buffer_t * b0;
+	  u32 next0;
+          " encap_stack "_header_t * iuX0;
+	  uword * p0;
+          u32 tunnel_index0;
+          " encap_stack "_tunnel_t * t0;
+          " encap_stack "_tunnel_key_t key0;
+          u32 error0;
+
+	  bi0 = from[0];
+	  to_next[0] = bi0;
+	  from += 1;
+	  to_next += 1;
+	  n_left_from -= 1;
+	  n_left_to_next -= 1;
+
+	  b0 = vlib_get_buffer (vm, bi0);
+
+          /* 
+           * udp leaves current_data pointing at the tunnel header
+           * $$$$ FIXME
+           */
+          vlib_buffer_advance 
+            (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
+
+          iuX0 = vlib_buffer_get_current (b0);
+
+          /* pop (ip, udp, lisp-gpe) */
+          vlib_buffer_advance (b0, sizeof (*iuX0));
+
+          tunnel_index0 = ~0;
+          error0 = 0;
+          next0 = " ENCAP_STACK "_INPUT_NEXT_DROP;
+
+          key0.src = iuX0->ip4.src_address.as_u32;
+          key0.iid = iuX0->lisp.iid;
+
+          /* $$$ validate key comparison */
+          if (PREDICT_FALSE ((key0.as_u64[0] != last_key.as_u64[0])))
+            {
+              p0 = hash_get_mem (ngm->" encap_stack "_tunnel_by_key, &key0);
+
+              if (p0 == 0)
+                {
+                  error0 = " ENCAP_STACK "_ERROR_NO_SUCH_TUNNEL;
+                  goto trace0;
+                }
+
+              last_key.as_u64[0] = key0.as_u64[0];
+              tunnel_index0 = last_tunnel_index = p0[0];
+            }
+          else
+            tunnel_index0 = last_tunnel_index;
+
+          t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0);
+
+          next0 = t0->decap_next_index;
+
+          /* Required to make the l2 tag push / pop code work on l2 subifs */
+          vnet_update_l2_len (b0);
+
+          /* 
+           * ip[46] lookup in the configured FIB
+           * " encap-stack ", here's the encap tunnel sw_if_index
+           */
+          vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
+          pkts_decapsulated ++;
+
+        trace0:
+          b0->error = error0 ? node->errors[error0] : 0;
+
+          if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
+            {
+              " encap_stack "_rx_trace_t *tr 
+                = vlib_add_trace (vm, node, b0, sizeof (*tr));
+              tr->next_index = next0;
+              tr->error = error0;
+              tr->tunnel_index = tunnel_index0;
+              tr->h = iuX0->lisp;
+            }
+	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
+					   to_next, n_left_to_next,
+					   bi0, next0);
+	}
+
+      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
+    }
+  vlib_node_increment_counter (vm, " encap_stack "_input_node.index,
+                               " ENCAP_STACK "_ERROR_DECAPSULATED, 
+                               pkts_decapsulated);
+  return from_frame->n_vectors;
+}
+
+static char * " encap_stack "_error_strings[] = {
+#define " encap_stack "_error(n,s) s,
+#include <vnet/" encap-stack "/" encap_stack "_error.def>
+#undef " encap_stack "_error
+#undef _
+};
+
+VLIB_REGISTER_NODE (" encap_stack "_input_node) = {
+  .function = \"" encap_stack "_input\",
+  .name = \"" encap-stack "-input\",
+  /* Takes a vector of packets. */
+  .vector_size = sizeof (u32),
+
+  .n_errors = " ENCAP_STACK "_N_ERROR,
+  .error_strings = " encap_stack "_error_strings,
+
+  .n_next_nodes = " ENCAP_STACK "_INPUT_N_NEXT,
+  .next_nodes = {
+#define _(s,n) [" ENCAP_STACK "_INPUT_NEXT_##s] = n,
+    foreach_" encap_stack "_input_next
+#undef _
+  },
+
+  .format_buffer = format_" encap_stack "_header_with_length,
+  .format_trace = format_" encap_stack "_rx_trace,
+  // $$$$ .unformat_buffer = unformat_" encap_stack "_header,
+};
+
+")
diff --git a/build-root/emacs-lisp/tunnel-encap-skel.el b/build-root/emacs-lisp/tunnel-encap-skel.el
new file mode 100644
index 0000000..5933153
--- /dev/null
+++ b/build-root/emacs-lisp/tunnel-encap-skel.el
@@ -0,0 +1,245 @@
+;;; tunnel-encap-skel.el - tunnel interface output skeleton
+
+(require 'skeleton)
+
+(define-skeleton tunnel-encap-skel
+"Insert a tunnel encap implementation"
+nil
+'(setq encap_stack (skeleton-read "encap_stack (e.g ip4_udp_lisp): "))
+'(setq ENCAP_STACK (upcase encap_stack))
+'(setq encap-stack (replace-regexp-in-string "_" "-" encap_stack))
+'(setq ENCAP-STACK (upcase encap-stack))
+"
+#include <clib/error.h>
+#include <clib/hash.h>
+#include <vnet/vnet.h>
+#include <vnet/ip/ip.h>
+#include <vnet/ethernet/ethernet.h>
+#include <vnet/" encap-stack "/" encap_stack ".h>
+
+/* Statistics (not really errors) */
+#define foreach_" encap_stack "_encap_error    \\
+_(ENCAPSULATED, \"good packets encapsulated\")
+
+static char * " encap_stack "_encap_error_strings[] = {
+#define _(sym,string) string,
+  foreach_" encap_stack "_encap_error
+#undef _
+};
+
+typedef enum {
+#define _(sym,str) " ENCAP_STACK "_ENCAP_ERROR_##sym,
+    foreach_" encap_stack "_encap_error
+#undef _
+    " ENCAP_STACK "_ENCAP_N_ERROR,
+} " encap_stack "_encap_error_t;
+
+typedef enum {
+    " ENCAP_STACK "_ENCAP_NEXT_IP4_LOOKUP,
+    " ENCAP_STACK "_ENCAP_NEXT_DROP,
+    " ENCAP_STACK "_ENCAP_N_NEXT,
+} " encap_stack "_encap_next_t;
+
+typedef struct {
+  u32 tunnel_index;
+} " encap_stack "_encap_trace_t;
+
+u8 * format_" encap_stack "_encap_trace (u8 * s, va_list * args)
+{
+  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
+  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
+  " encap_stack "_encap_trace_t * t 
+      = va_arg (*args, " encap_stack "_encap_trace_t *);
+
+  s = format (s, \"" ENCAP-STACK ": tunnel %d\", t->tunnel_index);
+  return s;
+}
+
+/* $$$$ FIXME adjust to match the rewrite string */
+#define foreach_fixed_header_offset             \\
+_(0) _(1) _(2) _(3) _(FIXME)
+
+static uword
+" encap_stack "_encap (vlib_main_t * vm,
+               vlib_node_runtime_t * node,
+               vlib_frame_t * from_frame)
+{
+  u32 n_left_from, next_index, * from, * to_next;
+  " encap_stack "_main_t * ngm = &" encap_stack "_main;
+  vnet_main_t * vnm = ngm->vnet_main;
+  u32 pkts_encapsulated = 0;
+  u16 old_l0 = 0, old_l1 = 0;
+
+  from = vlib_frame_vector_args (from_frame);
+  n_left_from = from_frame->n_vectors;
+
+  next_index = node->cached_next_index;
+
+  while (n_left_from > 0)
+    {
+      u32 n_left_to_next;
+
+      vlib_get_next_frame (vm, node, next_index,
+			   to_next, n_left_to_next);
+
+#if 0 /* $$$ dual loop when the single loop works */
+      while (n_left_from >= 4 && n_left_to_next >= 2)
+	{
+	  u32 bi0, bi1;
+	  vlib_buffer_t * b0, * b1;
+	  nsh_unicast_header_t * h0, * h1;
+          u32 label0, label1;
+	  u32 next0, next1;
+	  uword * p0, * p1;
+
+	  /* Prefetch next iteration. */
+	  {
+	    vlib_buffer_t * p2, * p3;
+
+	    p2 = vlib_get_buffer (vm, from[2]);
+	    p3 = vlib_get_buffer (vm, from[3]);
+
+	    vlib_prefetch_buffer_header (p2, LOAD);
+	    vlib_prefetch_buffer_header (p3, LOAD);
+
+	    CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
+	    CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
+	  }
+
+	  bi0 = from[0];
+	  bi1 = from[1];
+	  to_next[0] = bi0;
+	  to_next[1] = bi1;
+	  from += 2;
+	  to_next += 2;
+	  n_left_to_next -= 2;
+	  n_left_from -= 2;
+
+	  b0 = vlib_get_buffer (vm, bi0);
+	  b1 = vlib_get_buffer (vm, bi1);
+
+          h0 = vlib_buffer_get_current (b0);
+          h1 = vlib_buffer_get_current (b1);
+          
+          next0 = next1 = " ENCAP_STACK "_ENCAP_NEXT_IP4_LOOKUP;
+
+          vlib_buffer_advance (b0, sizeof (*h0));
+          vlib_buffer_advance (b1, sizeof (*h1));
+
+	  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
+					   to_next, n_left_to_next,
+					   bi0, bi1, next0, next1);
+	}
+#endif 
+    
+      while (n_left_from > 0 && n_left_to_next > 0)
+	{
+	  u32 bi0;
+	  vlib_buffer_t * b0;
+	  u32 next0 = " ENCAP_STACK "_ENCAP_NEXT_IP4_LOOKUP;
+          vnet_hw_interface_t * hi0;
+          ip4_header_t * ip0;
+          udp_header_t * udp0;
+          u64 * copy_src0, * copy_dst0;
+          u32 * copy_src_last0, * copy_dst_last0;
+          " encap_stack "_tunnel_t * t0;
+          u16 new_l0;
+          ip_csum_t sum0;
+
+	  bi0 = from[0];
+	  to_next[0] = bi0;
+	  from += 1;
+	  to_next += 1;
+	  n_left_from -= 1;
+	  n_left_to_next -= 1;
+
+	  b0 = vlib_get_buffer (vm, bi0);
+
+          /* 1-wide cache? */
+          hi0 = vnet_get_sup_hw_interface 
+            (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);
+
+          t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
+
+          ASSERT(vec_len(t0->rewrite) >= 24);
+
+          /* Apply the rewrite string. $$$$ vnet_rewrite? */
+          vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
+
+          ip0 = vlib_buffer_get_current(b0);
+          /* Copy the fixed header */
+          copy_dst0 = (u64 *) ip0;
+          copy_src0 = (u64 *) t0->rewrite;
+
+          ASSERT (sizeof (ip4_udp_" encap_stack "_header_t) == FIXME);
+
+          /* Copy first N octets 8-bytes at a time */
+#define _(offs) copy_dst0[offs] = copy_src0[offs];
+          foreach_fixed_header_offset;
+#undef _
+#if 0 /* needed if encap not a multiple of 8 bytes */
+          /* Last 4 octets. Hopefully gcc will be our friend */
+          copy_dst_last0 = (u32 *)(&copy_dst0[FIXME]);
+          copy_src_last0 = (u32 *)(&copy_src0[FIXME]);
+          copy_dst_last0[0] = copy_src_last0[0];
+
+#endif
+          /* fix the <bleep>ing outer-IP checksum */
+          sum0 = ip0->checksum;
+          /* old_l0 always 0, see the rewrite setup */
+          new_l0 = 
+            clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
+          
+          sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
+                                 length /* changed member */);
+          ip0->checksum = ip_csum_fold (sum0);
+          ip0->length = new_l0;
+          
+          /* Fix UDP length */
+          udp0 = (udp_header_t *)(ip0+1);
+          new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
+                                         - sizeof (*ip0));
+          
+          udp0->length = new_l0;
+
+          /* Reset to look up tunnel partner in the configured FIB */
+          vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
+          pkts_encapsulated ++;
+
+          if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
+            {
+              " encap_stack "_encap_trace_t *tr = 
+                vlib_add_trace (vm, node, b0, sizeof (*tr));
+              tr->tunnel_index = t0 - ngm->tunnels;
+            }
+	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
+					   to_next, n_left_to_next,
+					   bi0, next0);
+	}
+
+      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
+    }
+  vlib_node_increment_counter (vm, node->node_index, 
+                               " ENCAP_STACK "_ENCAP_ERROR_ENCAPSULATED, 
+                               pkts_encapsulated);
+  return from_frame->n_vectors;
+}
+
+VLIB_REGISTER_NODE (" encap_stack "_encap_node) = {
+  .function = " encap_stack "_encap,
+  .name = \"" encap-stack "-encap\",
+  .vector_size = sizeof (u32),
+  .format_trace = format_" encap_stack "_encap_trace,
+  .type = VLIB_NODE_TYPE_INTERNAL,
+
+  .n_errors = ARRAY_LEN(" encap_stack "_encap_error_strings),
+  .error_strings = " encap_stack "_encap_error_strings,
+
+  .n_next_nodes = " ENCAP_STACK "_ENCAP_N_NEXT,
+
+  .next_nodes = {
+        [" ENCAP_STACK "_ENCAP_NEXT_IP4_LOOKUP] = \"ip4-lookup\",
+        [" ENCAP_STACK "_ENCAP_NEXT_DROP] = \"error-drop\",
+  },
+};
+")
diff --git a/build-root/emacs-lisp/tunnel-h-skel.el b/build-root/emacs-lisp/tunnel-h-skel.el
new file mode 100644
index 0000000..7e33309
--- /dev/null
+++ b/build-root/emacs-lisp/tunnel-h-skel.el
@@ -0,0 +1,128 @@
+;;; tunnel-h-skel.el - tunnel encap header file skeleton
+
+(require 'skeleton)
+
+(define-skeleton tunnel-h-skel
+"Insert a tunnel encap header file"
+nil
+'(setq encap_stack (skeleton-read "encap_stack (e.g ip4_udp_lisp): "))
+'(setq ENCAP_STACK (upcase encap_stack))
+'(setq encap-stack (replace-regexp-in-string "_" "-" encap_stack))
+"
+#ifndef included_vnet_" encap_stack "_h
+#define included_vnet_" encap_stack "_h
+
+#include <clib/error.h>
+#include <clib/hash.h>
+#include <vnet/vnet.h>
+#include <vnet/ip/ip.h>
+#include <vnet/l2/l2_input.h>
+#include <vnet/ethernet/ethernet.h>
+#include <vnet/" encap-stack "/" encap_stack "_packet.h>
+#include <vnet/ip/ip4_packet.h>
+#include <vnet/ip/udp.h>
+
+/* Encap stack built in encap.c */
+typedef CLIB_PACKED (struct {
+  ip4_header_t ip4;             /* 20 bytes */
+  udp_header_t udp;             /* 8 bytes */
+  " encap_stack "_header_t lisp;       /* 8 bytes */
+}) " encap_stack "_header_t;
+
+typedef CLIB_PACKED(struct {
+  /* 
+   * Key fields: 
+   * all fields in NET byte order
+   */
+  union {
+    struct {
+      u32 FIXME_NET_BYTE_ORDER;
+    };
+    u64 as_u64[1];
+  };
+}) " encap_stack "_tunnel_key_t;
+
+typedef struct {
+  /* Rewrite string. $$$$ maybe: embed vnet_rewrite header */
+  u8 * rewrite;
+
+  /* decap next index */
+  u32 decap_next_index;
+
+  /* tunnel src and dst addresses */
+  ip4_address_t src;
+  ip4_address_t dst;
+
+  /* FIB indices */
+  u32 encap_fib_index;          /* tunnel partner lookup here */
+  u32 decap_fib_index;          /* inner IP lookup here */
+
+  /* vnet intfc hw/sw_if_index */
+  u32 hw_if_index;
+
+  /* encap header fields in HOST byte order */
+  u32 FIXME;
+} " encap_stack "_tunnel_t;
+
+#define foreach_" encap_stack "_input_next        \\
+_(DROP, \"error-drop\")                           \\
+_(IP4_INPUT, \"ip4-input\")                       \\
+_(IP6_INPUT, \"ip6-input\")                       \\
+_(ETHERNET_INPUT, \"ethernet-input\")             \\
+_(" ENCAP_STACK "_ENCAP, \"" encap-stack "-encap\")
+
+typedef enum {
+#define _(s,n) " ENCAP_STACK "_INPUT_NEXT_##s,
+  foreach_" encap_stack "_input_next
+#undef _
+  " ENCAP_STACK "_INPUT_N_NEXT,
+} " encap_stack "_input_next_t;
+
+typedef enum {
+#define " encap_stack "_error(n,s) " ENCAP_STACK "_ERROR_##n,
+#include <vnet/" encap-stack "/" encap_stack "_error.def>
+#undef " encap_stack "_error
+  " ENCAP_STACK "_N_ERROR,
+} " encap_stack "_input_error_t;
+
+typedef struct {
+  /* vector of encap tunnel instances */
+  " encap_stack "_tunnel_t *tunnels;
+
+  /* lookup tunnel by key */
+  uword * " encap_stack "_tunnel_by_key;
+
+  /* Free vlib hw_if_indices */
+  u32 * free_" encap_stack "_tunnel_hw_if_indices;
+
+  /* convenience */
+  vlib_main_t * vlib_main;
+  vnet_main_t * vnet_main;
+} " encap_stack "_main_t;
+
+" encap_stack "_main_t " encap_stack "_main;
+
+vlib_node_registration_t " encap_stack "_input_node;
+vlib_node_registration_t " encap_stack "_encap_node;
+
+u8 * format_" encap_stack "_encap_trace (u8 * s, va_list * args);
+u8 * format_" encap_stack "_header_with_length (u8 * s, va_list * args);
+
+typedef struct {
+  u8 is_add;
+  ip4_address_t src, dst;
+  u32 encap_fib_index;
+  u32 decap_fib_index;
+  u32 decap_next_index;
+  /* encap fields in HOST byte order */
+  u8 FIXME_HOST_BYTE_ORDER;
+} vnet_" encap_stack "_add_del_tunnel_args_t;
+
+int vnet_" encap_stack "_add_del_tunnel 
+(vnet_" encap_stack "_add_del_tunnel_args_t *a, u32 * hw_if_indexp);
+
+u8 * format_" encap_stack "_header_with_length (u8 * s, va_list * args);
+
+#endif /* included_vnet_" encap_stack "_h */
+
+")