ioam: ip6 hop by hop ext header and SR header co-existence

1. Fix finding sr extension header
2. Fix for assert checks for space for sr header in packet headroom
3. ioam build warnings
4. Fix for SR header removal in presence of hbh ext header
   clib_memcpy with overlapping src/dst was failing

Change-Id: I8576204eb571d1d4725a4e6976e18fe61cd1cd35
Signed-off-by: shwethab <shwetha.bhandari@gmail.com>
diff --git a/src/vnet/sr/sr.h b/src/vnet/sr/sr.h
index 1545a84..5efeb4d 100755
--- a/src/vnet/sr/sr.h
+++ b/src/vnet/sr/sr.h
@@ -297,7 +297,7 @@
  * @return precomputed rewrite string for SRH insertion
  */
 static inline u8 *
-ip6_compute_rewrite_string_insert (ip6_address_t * sl)
+ip6_sr_compute_rewrite_string_insert (ip6_address_t * sl)
 {
   ip6_sr_header_t *srh;
   ip6_address_t *addrp, *this_address;
diff --git a/src/vnet/sr/sr_localsid.c b/src/vnet/sr/sr_localsid.c
index 407491c..610bb7f 100755
--- a/src/vnet/sr/sr_localsid.c
+++ b/src/vnet/sr/sr_localsid.c
@@ -827,6 +827,9 @@
 			ip6_sr_localsid_t * ls0, u32 * next0)
 {
   u32 new_l0, sr_len;
+  u64 *copy_dst0, *copy_src0;
+  u32 copy_len_u64s0 = 0;
+  int i;
 
   if (PREDICT_TRUE (sr0->type == ROUTING_HEADER_TYPE_SR))
     {
@@ -845,8 +848,25 @@
 	  vlib_buffer_advance (b0, sr_len);
 	  new_l0 = clib_net_to_host_u16 (ip0->payload_length) - sr_len;
 	  ip0->payload_length = clib_host_to_net_u16 (new_l0);
-	  clib_memcpy ((void *) ip0 + sr_len, ip0,
-		       (void *) sr0 - (void *) ip0);
+	  copy_src0 = (u64 *) ip0;
+	  copy_dst0 = copy_src0 + (sr0->length + 1);
+	  /* number of 8 octet units to copy
+	   * By default in absence of extension headers it is equal to length of ip6 header
+	   * With extension headers it number of 8 octet units of ext headers preceding
+	   * SR header
+	   */
+	  copy_len_u64s0 =
+	    (((u8 *) sr0 - (u8 *) ip0) - sizeof (ip6_header_t)) >> 3;
+	  copy_dst0[4 + copy_len_u64s0] = copy_src0[4 + copy_len_u64s0];
+	  copy_dst0[3 + copy_len_u64s0] = copy_src0[3 + copy_len_u64s0];
+	  copy_dst0[2 + copy_len_u64s0] = copy_src0[2 + copy_len_u64s0];
+	  copy_dst0[1 + copy_len_u64s0] = copy_src0[1 + copy_len_u64s0];
+	  copy_dst0[0 + copy_len_u64s0] = copy_src0[0 + copy_len_u64s0];
+
+	  for (i = copy_len_u64s0 - 1; i >= 0; i--)
+	    {
+	      copy_dst0[i] = copy_src0[i];
+	    }
 
 	  if (ls0->behavior == SR_BEHAVIOR_X)
 	    {
diff --git a/src/vnet/sr/sr_policy_rewrite.c b/src/vnet/sr/sr_policy_rewrite.c
index 1f8bdca..dfdd795 100755
--- a/src/vnet/sr/sr_policy_rewrite.c
+++ b/src/vnet/sr/sr_policy_rewrite.c
@@ -1147,14 +1147,14 @@
 	    pool_elt_at_index (sm->sid_lists,
 			       vnet_buffer (b3)->ip.adj_index[VLIB_TX]);
 
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl0->rewrite) + b0->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl1->rewrite) + b1->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl2->rewrite) + b2->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl3->rewrite) + b3->current_data));
+	  ASSERT (b0->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl0->rewrite));
+	  ASSERT (b1->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl1->rewrite));
+	  ASSERT (b2->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl2->rewrite));
+	  ASSERT (b3->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl3->rewrite));
 
 	  ip0_encap = vlib_buffer_get_current (b0);
 	  ip1_encap = vlib_buffer_get_current (b1);
@@ -1254,9 +1254,8 @@
 	  sl0 =
 	    pool_elt_at_index (sm->sid_lists,
 			       vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
-
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl0->rewrite) + b0->current_data));
+	  ASSERT (b0->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl0->rewrite));
 
 	  ip0_encap = vlib_buffer_get_current (b0);
 
@@ -1426,15 +1425,14 @@
 	  sl3 =
 	    pool_elt_at_index (sm->sid_lists,
 			       vnet_buffer (b3)->ip.adj_index[VLIB_TX]);
-
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl0->rewrite) + b0->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl1->rewrite) + b1->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl2->rewrite) + b2->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl3->rewrite) + b3->current_data));
+	  ASSERT (b0->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl0->rewrite));
+	  ASSERT (b1->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl1->rewrite));
+	  ASSERT (b2->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl2->rewrite));
+	  ASSERT (b3->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl3->rewrite));
 
 	  ip0_encap = vlib_buffer_get_current (b0);
 	  ip1_encap = vlib_buffer_get_current (b1);
@@ -1535,9 +1533,8 @@
 	  sl0 =
 	    pool_elt_at_index (sm->sid_lists,
 			       vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
-
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl0->rewrite) + b0->current_data));
+	  ASSERT (b0->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl0->rewrite));
 
 	  ip0_encap = vlib_buffer_get_current (b0);
 
@@ -1785,14 +1782,14 @@
 	    pool_elt_at_index (sm->sid_lists,
 			       vnet_buffer (b3)->ip.adj_index[VLIB_TX]);
 
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl0->rewrite) + b0->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl1->rewrite) + b1->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl2->rewrite) + b2->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl3->rewrite) + b3->current_data));
+	  ASSERT (b0->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl0->rewrite));
+	  ASSERT (b1->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl1->rewrite));
+	  ASSERT (b2->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl2->rewrite));
+	  ASSERT (b3->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl3->rewrite));
 
 	  en0 = vlib_buffer_get_current (b0);
 	  en1 = vlib_buffer_get_current (b1);
@@ -1926,9 +1923,8 @@
 	  sl0 =
 	    pool_elt_at_index (sm->sid_lists,
 			       vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
-
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl0->rewrite) + b0->current_data));
+	  ASSERT (b0->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl0->rewrite));
 
 	  en0 = vlib_buffer_get_current (b0);
 
@@ -2075,15 +2071,14 @@
 	  sl3 =
 	    pool_elt_at_index (sm->sid_lists,
 			       vnet_buffer (b3)->ip.adj_index[VLIB_TX]);
-
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl0->rewrite) + b0->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl1->rewrite) + b1->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl2->rewrite) + b2->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl3->rewrite) + b3->current_data));
+	  ASSERT (b0->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl0->rewrite));
+	  ASSERT (b1->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl1->rewrite));
+	  ASSERT (b2->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl2->rewrite));
+	  ASSERT (b3->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl3->rewrite));
 
 	  ip0 = vlib_buffer_get_current (b0);
 	  ip1 = vlib_buffer_get_current (b1);
@@ -2321,8 +2316,8 @@
 	  sl0 =
 	    pool_elt_at_index (sm->sid_lists,
 			       vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl0->rewrite) + b0->current_data));
+	  ASSERT (b0->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl0->rewrite));
 
 	  ip0 = vlib_buffer_get_current (b0);
 
@@ -2498,15 +2493,14 @@
 	  sl3 =
 	    pool_elt_at_index (sm->sid_lists,
 			       vnet_buffer (b3)->ip.adj_index[VLIB_TX]);
-
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl0->rewrite_bsid) + b0->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl1->rewrite_bsid) + b1->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl2->rewrite_bsid) + b2->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl3->rewrite_bsid) + b3->current_data));
+	  ASSERT (b0->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl0->rewrite_bsid));
+	  ASSERT (b1->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl1->rewrite_bsid));
+	  ASSERT (b2->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl2->rewrite_bsid));
+	  ASSERT (b3->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl3->rewrite_bsid));
 
 	  ip0 = vlib_buffer_get_current (b0);
 	  ip1 = vlib_buffer_get_current (b1);
@@ -2735,8 +2729,8 @@
 	  sl0 =
 	    pool_elt_at_index (sm->sid_lists,
 			       vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl0->rewrite_bsid) + b0->current_data));
+	  ASSERT (b0->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl0->rewrite_bsid));
 
 	  ip0 = vlib_buffer_get_current (b0);
 
@@ -2943,15 +2937,14 @@
 	  sl3 =
 	    pool_elt_at_index (sm->sid_lists,
 			       vnet_buffer (b3)->ip.adj_index[VLIB_TX]);
-
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl0->rewrite) + b0->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl1->rewrite) + b1->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl2->rewrite) + b2->current_data));
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl3->rewrite) + b3->current_data));
+	  ASSERT (b0->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl0->rewrite));
+	  ASSERT (b1->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl1->rewrite));
+	  ASSERT (b2->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl2->rewrite));
+	  ASSERT (b3->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl3->rewrite));
 
 	  ip0_encap = vlib_buffer_get_current (b0);
 	  ip1_encap = vlib_buffer_get_current (b1);
@@ -3067,9 +3060,8 @@
 	  sl0 =
 	    pool_elt_at_index (sm->sid_lists,
 			       vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
-
-	  ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
-		  (vec_len (sl0->rewrite) + b0->current_data));
+	  ASSERT (b0->current_data + VLIB_BUFFER_PRE_DATA_SIZE >=
+		  vec_len (sl0->rewrite));
 
 	  ip0_encap = vlib_buffer_get_current (b0);
 	  ip6_ext_header_find_t (ip0_encap, prev0, sr0,