BIER; bi-dir to/from underlay

set and check a special RX interface value as the packet enters and
exits a BIER domain

Change-Id: I5ff2f0e2d1b3ce0f3598b935f518fc11eb0896ee
Signed-off-by: Neale Ranns <neale.ranns@cisco.com>
diff --git a/src/vnet/bier/bier_disp_dispatch_node.c b/src/vnet/bier/bier_disp_dispatch_node.c
index 40d0a29..0dbcada 100644
--- a/src/vnet/bier/bier_disp_dispatch_node.c
+++ b/src/vnet/bier/bier_disp_dispatch_node.c
@@ -72,6 +72,7 @@
             bdei0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
             hdr0 = vlib_buffer_get_current(b0);
             bde0 = bier_disp_entry_get(bdei0);
+            vnet_buffer(b0)->ip.adj_index[VLIB_RX] = BIER_RX_ITF;
 
             /*
              * header is in network order - flip it, we are about to
diff --git a/src/vnet/bier/bier_drop.c b/src/vnet/bier/bier_drop.c
index eb873ac..da6d566 100644
--- a/src/vnet/bier/bier_drop.c
+++ b/src/vnet/bier/bier_drop.c
@@ -87,11 +87,10 @@
   return s;
 }
 
-/* *INDENT-OFF* */
 VLIB_REGISTER_NODE (bier_drop_node, static) =
 {
-    .function = bier_drop,.
-    name = "bier-drop",
+    .function = bier_drop,
+    .name = "bier-drop",
     .vector_size = sizeof (u32),
     .format_trace = format_bier_drop_trace,
     .n_next_nodes = 1,
diff --git a/src/vnet/bier/bier_hdr_inlines.h b/src/vnet/bier/bier_hdr_inlines.h
index 4437dc8..d58926e 100644
--- a/src/vnet/bier/bier_hdr_inlines.h
+++ b/src/vnet/bier/bier_hdr_inlines.h
@@ -27,6 +27,11 @@
 #include <vnet/ip/ip6_packet.h>
 
 /**
+ * Special Value of the BIER RX interface
+ */
+#define BIER_RX_ITF (~0 - 1)
+
+/**
  * Mask and shift values for the fields incorporated
  * into the header's first word
  */
diff --git a/src/vnet/bier/bier_imp.c b/src/vnet/bier/bier_imp.c
index 0c87d17..87a8f90 100644
--- a/src/vnet/bier/bier_imp.c
+++ b/src/vnet/bier/bier_imp.c
@@ -68,7 +68,7 @@
     pool_get_aligned(bier_imp_pool, bi, CLIB_CACHE_LINE_BYTES);
 
     bi->bi_tbl = *bti;
-    btii = bier_table_add_or_lock(bti, MPLS_LABEL_INVALID);
+    btii = bier_table_lock(bti);
 
     /*
      * init the BIER header we will paint on in the data plane
diff --git a/src/vnet/bier/bier_imp_node.c b/src/vnet/bier/bier_imp_node.c
index 9c09d67..0f1820b 100644
--- a/src/vnet/bier/bier_imp_node.c
+++ b/src/vnet/bier/bier_imp_node.c
@@ -120,30 +120,39 @@
             vlib_buffer_advance(b0, -(sizeof(bier_hdr_t) +
                                       bier_hdr_len_id_to_num_bytes(bimp0->bi_tbl.bti_hdr_len)));
             hdr0 = vlib_buffer_get_current(b0);
-            clib_memcpy(hdr0, &bimp0->bi_hdr,
-                        (sizeof(bier_hdr_t) +
-                         bier_hdr_len_id_to_num_bytes(bimp0->bi_tbl.bti_hdr_len)));
-            /*
-             * Fixup the entropy and protocol, both of which have a
-             * zero value post the paint job
-             */
-            hdr0->bh_oam_dscp_proto |=
-                clib_host_to_net_u16(bproto << BIER_HDR_PROTO_FIELD_SHIFT);
-            hdr0->bh_first_word |=
-                clib_host_to_net_u32((vnet_buffer(b0)->ip.flow_hash &
-                                      BIER_HDR_ENTROPY_FIELD_MASK) <<
-                                     BIER_HDR_ENTROPY_FIELD_SHIFT);
 
-            /*
-             * use TTL 64 for the post enacp MPLS label/BIFT-ID
-             * this we be decremeted in bier_output node.
-             */
-            vnet_buffer(b0)->mpls.ttl = 65;
+            /* RPF check */
+            if (PREDICT_FALSE(BIER_RX_ITF == vnet_buffer(b0)->ip.adj_index[VLIB_RX]))
+            {
+                next0 = 0;
+            }
+            else
+            {
+                clib_memcpy(hdr0, &bimp0->bi_hdr,
+                            (sizeof(bier_hdr_t) +
+                             bier_hdr_len_id_to_num_bytes(bimp0->bi_tbl.bti_hdr_len)));
+                /*
+                 * Fixup the entropy and protocol, both of which have a
+                 * zero value post the paint job
+                 */
+                hdr0->bh_oam_dscp_proto |=
+                    clib_host_to_net_u16(bproto << BIER_HDR_PROTO_FIELD_SHIFT);
+                hdr0->bh_first_word |=
+                    clib_host_to_net_u32((vnet_buffer(b0)->ip.flow_hash &
+                                          BIER_HDR_ENTROPY_FIELD_MASK) <<
+                                         BIER_HDR_ENTROPY_FIELD_SHIFT);
 
-            /* next node */
-            next0 = bimp0->bi_dpo[fproto].dpoi_next_node;
-            vnet_buffer(b0)->ip.adj_index[VLIB_TX] =
-                bimp0->bi_dpo[fproto].dpoi_index;
+                /*
+                 * use TTL 64 for the post enacp MPLS label/BIFT-ID
+                 * this we be decremeted in bier_output node.
+                 */
+                vnet_buffer(b0)->mpls.ttl = 65;
+
+                /* next node */
+                next0 = bimp0->bi_dpo[fproto].dpoi_next_node;
+                vnet_buffer(b0)->ip.adj_index[VLIB_TX] =
+                    bimp0->bi_dpo[fproto].dpoi_index;
+            }
 
             if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
             {
@@ -194,7 +203,7 @@
     .format_trace = format_bier_imp_trace,
     .n_next_nodes = 1,
     .next_nodes = {
-        [0] = "error-drop",
+        [0] = "bier-drop",
     }
 };
 VLIB_NODE_FUNCTION_MULTIARCH (bier_imp_ip4_node, bier_imp_ip4)
diff --git a/src/vnet/bier/bier_table.c b/src/vnet/bier/bier_table.c
index 3ecda10..edfb754 100644
--- a/src/vnet/bier/bier_table.c
+++ b/src/vnet/bier/bier_table.c
@@ -329,6 +329,70 @@
     return (bt);
 }
 
+
+static index_t
+bier_table_create (const bier_table_id_t *btid,
+                   mpls_label_t local_label)
+{
+    /*
+     * add a new table
+     */
+    bier_table_t *bt;
+    index_t bti;
+    u32 key;
+
+    key = bier_table_mk_key(btid);
+
+    pool_get_aligned(bier_table_pool, bt, CLIB_CACHE_LINE_BYTES);
+    bier_table_init(bt, btid, local_label);
+
+    hash_set(bier_tables_by_key, key, bier_table_get_index(bt));
+    bti = bier_table_get_index(bt);
+
+    if (bier_table_is_main(bt))
+    {
+        bt = bier_table_mk_ecmp(bti);
+
+        /*
+         * add whichever mpls-fib or bift we need
+         */
+        if (local_label != MPLS_LABEL_INVALID)
+        {
+            bt->bt_ll = local_label;
+            bier_table_mk_lfib(bt);
+        }
+        else
+        {
+            bier_table_mk_bift(bt);
+        }
+    }
+
+    return (bti);
+}
+
+index_t
+bier_table_lock (const bier_table_id_t *btid)
+{
+    bier_table_t *bt;
+    index_t bti;
+
+    bt = bier_table_find(btid);
+
+    if (NULL == bt)
+    {
+        bti = bier_table_create(btid, MPLS_LABEL_INVALID);
+        bt = bier_table_get(bti);
+    }
+    else
+    {
+        bti = bier_table_get_index(bt);
+    }
+
+    bier_table_lock_i(bt);
+
+    return (bti);
+}
+
 index_t
 bier_table_add_or_lock (const bier_table_id_t *btid,
                         mpls_label_t local_label)
@@ -379,36 +443,8 @@
     }
     else
     {
-        /*
-         * add a new table
-         */
-        u32 key;
-
-        key = bier_table_mk_key(btid);
-
-        pool_get_aligned(bier_table_pool, bt, CLIB_CACHE_LINE_BYTES);
-        bier_table_init(bt, btid, local_label);
-
-        hash_set(bier_tables_by_key, key, bier_table_get_index(bt));
-        bti = bier_table_get_index(bt);
-
-        if (bier_table_is_main(bt))
-        {
-            bt = bier_table_mk_ecmp(bti);
-
-            /*
-             * add whichever mpls-fib or bift we need
-             */
-            if (local_label != MPLS_LABEL_INVALID)
-            {
-                bt->bt_ll = local_label;
-                bier_table_mk_lfib(bt);
-            }
-            else
-            {
-                bier_table_mk_bift(bt);
-            }
-        }
+        bti = bier_table_create(btid, local_label);
+        bt = bier_table_get(bti);
     }
 
     bier_table_lock_i(bt);
diff --git a/src/vnet/bier/bier_table.h b/src/vnet/bier/bier_table.h
index 9cd9937..5264e4c 100644
--- a/src/vnet/bier/bier_table.h
+++ b/src/vnet/bier/bier_table.h
@@ -91,6 +91,7 @@
 
 extern index_t bier_table_add_or_lock(const bier_table_id_t *id,
                                       mpls_label_t ll);
+extern index_t bier_table_lock(const bier_table_id_t *id);
 extern void bier_table_unlock(const bier_table_id_t *id);
 
 extern void bier_table_route_path_add(const bier_table_id_t *bti,
diff --git a/test/test_bier.py b/test/test_bier.py
index b719f0c..cc4c9b3 100644
--- a/test/test_bier.py
+++ b/test/test_bier.py
@@ -482,6 +482,37 @@
         #
         self.send_and_expect(self.pg0, [p], self.pg1)
 
+        #
+        # A multicast route to forward post BIER disposition that needs
+        # a check against sending back into the BIER core
+        #
+        bi = VppBierImp(self, bti, 333, chr(0x3) * 32)
+        bi.add_vpp_config()
+
+        route_eg_232_1_1_2 = VppIpMRoute(
+            self,
+            "0.0.0.0",
+            "232.1.1.2", 32,
+            MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
+            paths=[VppMRoutePath(0xffffffff,
+                                 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
+                                 proto=DpoProto.DPO_PROTO_BIER,
+                                 bier_imp=bi.bi_index),
+                   VppMRoutePath(self.pg1.sw_if_index,
+                                 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
+        route_eg_232_1_1_2.add_vpp_config()
+        route_eg_232_1_1_2.update_rpf_id(8192)
+
+        p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
+             MPLS(label=77, ttl=255) /
+             BIER(length=BIERLength.BIER_LEN_256,
+                  BitString=chr(255)*32,
+                  BFRID=77) /
+             IP(src="1.1.1.1", dst="232.1.1.2") /
+             UDP(sport=1234, dport=1234) /
+             Raw())
+        self.send_and_expect(self.pg0, [p], self.pg1)
+
     def bier_e2e(self, hdr_len_id, n_bytes, max_bp):
         """ BIER end-to-end"""