Fix coverity issue for NSH plugin

Change-Id: Ie99ff9aebe3c896e3e104a5a7bebd84f7766e713
Signed-off-by: Hongjun Ni <hongjun.ni@intel.com>
diff --git a/src/plugins/nsh/nsh.c b/src/plugins/nsh/nsh.c
index 33cf552..ced898c 100644
--- a/src/plugins/nsh/nsh.c
+++ b/src/plugins/nsh/nsh.c
@@ -63,6 +63,10 @@
 #include <nsh/nsh.api.h>
 #undef vl_msg_name_crc_list
 
+/*  Dummy Eth header */
+const char dummy_dst_address[6] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
+const char dummy_src_address[6] = { 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc };
+
 /*
  * A handy macro to set up a message reply.
  * Assumes that the following variables are available:
@@ -1742,6 +1746,9 @@
 	  b0 = vlib_get_buffer (vm, bi0);
 	  b1 = vlib_get_buffer (vm, bi1);
 	  hdr0 = vlib_buffer_get_current (b0);
+	  hdr1 = vlib_buffer_get_current (b1);
+
+	  /* Process packet 0 */
 	  if (node_type == NSH_INPUT_TYPE)
 	    {
 	      nsp_nsi0 = hdr0->nsp_nsi;
@@ -1764,10 +1771,8 @@
 	  else if (node_type == NSH_AWARE_VNF_PROXY_TYPE)
 	    {
 	      /* Push dummy Eth header */
-	      memset (&dummy_eth0.dst_address[0], 0x11223344, 4);
-	      memset (&dummy_eth0.dst_address[4], 0x5566, 2);
-	      memset (&dummy_eth0.src_address[0], 0x778899aa, 4);
-	      memset (&dummy_eth0.src_address[4], 0xbbcc, 2);
+	      clib_memcpy (dummy_eth0.dst_address, dummy_dst_address, 6);
+	      clib_memcpy (dummy_eth0.src_address, dummy_src_address, 6);
 	      dummy_eth0.type = 0x0800;
 	      vlib_buffer_advance (b0, -(word) sizeof (ethernet_header_t));
 	      hdr0 = vlib_buffer_get_current (b0);
@@ -1799,65 +1804,6 @@
 	      nsp_nsi0 = proxy0->nsp_nsi;
 	    }
 
-	  hdr1 = vlib_buffer_get_current (b1);
-	  if (node_type == NSH_INPUT_TYPE)
-	    {
-	      nsp_nsi1 = hdr1->nsp_nsi;
-	      header_len1 = (hdr1->length & NSH_LEN_MASK) * 4;
-	      ttl1 = (hdr1->ver_o_c & NSH_TTL_H4_MASK) << 2 |
-		(hdr1->length & NSH_TTL_L2_MASK) >> 6;
-	      ttl1 = ttl1 - 1;
-	      if (PREDICT_FALSE (ttl1 == 0))
-		{
-		  error1 = NSH_NODE_ERROR_INVALID_TTL;
-		  goto trace1;
-		}
-	    }
-	  else if (node_type == NSH_CLASSIFIER_TYPE)
-	    {
-	      nsp_nsi1 =
-		clib_host_to_net_u32 (vnet_buffer (b1)->
-				      l2_classify.opaque_index);
-	    }
-	  else if (node_type == NSH_AWARE_VNF_PROXY_TYPE)
-	    {
-	      /* Push dummy Eth header */
-	      memset (&dummy_eth1.dst_address[0], 0x11223344, 4);
-	      memset (&dummy_eth1.dst_address[4], 0x5566, 2);
-	      memset (&dummy_eth1.src_address[0], 0x778899aa, 4);
-	      memset (&dummy_eth1.src_address[4], 0xbbcc, 2);
-	      dummy_eth1.type = 0x0800;
-	      vlib_buffer_advance (b1, -(word) sizeof (ethernet_header_t));
-	      hdr1 = vlib_buffer_get_current (b1);
-	      clib_memcpy (hdr1, &dummy_eth1,
-			   (word) sizeof (ethernet_header_t));
-
-	      sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
-	      nsp_nsi1 = nm->tunnel_index_by_sw_if_index[sw_if_index1];
-	    }
-	  else
-	    {
-	      memset (&key1, 0, sizeof (key1));
-	      key1.transport_type = NSH_NODE_NEXT_ENCAP_VXLAN4;
-	      key1.transport_index = vnet_buffer (b1)->sw_if_index[VLIB_RX];
-
-	      p1 = hash_get_mem (nm->nsh_proxy_session_by_key, &key1);
-	      if (PREDICT_FALSE (p1 == 0))
-		{
-		  error1 = NSH_NODE_ERROR_NO_PROXY;
-		  goto trace1;
-		}
-
-	      proxy1 = pool_elt_at_index (nm->nsh_proxy_sessions, p1[0]);
-	      if (PREDICT_FALSE (proxy1 == 0))
-		{
-		  error1 = NSH_NODE_ERROR_NO_PROXY;
-		  goto trace1;
-		}
-	      nsp_nsi1 = proxy1->nsp_nsi;
-	    }
-
-	  /* Process packet 0 */
 	  entry0 = hash_get_mem (nm->nsh_mapping_by_key, &nsp_nsi0);
 	  if (PREDICT_FALSE (entry0 == 0))
 	    {
@@ -1956,7 +1902,8 @@
 
 	    }
 
-	trace0:b0->error = error0 ? node->errors[error0] : 0;
+	trace0:
+	  b0->error = error0 ? node->errors[error0] : 0;
 
 	  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
 	    {
@@ -1967,6 +1914,61 @@
 	    }
 
 	  /* Process packet 1 */
+	  if (node_type == NSH_INPUT_TYPE)
+	    {
+	      nsp_nsi1 = hdr1->nsp_nsi;
+	      header_len1 = (hdr1->length & NSH_LEN_MASK) * 4;
+	      ttl1 = (hdr1->ver_o_c & NSH_TTL_H4_MASK) << 2 |
+		(hdr1->length & NSH_TTL_L2_MASK) >> 6;
+	      ttl1 = ttl1 - 1;
+	      if (PREDICT_FALSE (ttl1 == 0))
+		{
+		  error1 = NSH_NODE_ERROR_INVALID_TTL;
+		  goto trace1;
+		}
+	    }
+	  else if (node_type == NSH_CLASSIFIER_TYPE)
+	    {
+	      nsp_nsi1 =
+		clib_host_to_net_u32 (vnet_buffer (b1)->
+				      l2_classify.opaque_index);
+	    }
+	  else if (node_type == NSH_AWARE_VNF_PROXY_TYPE)
+	    {
+	      /* Push dummy Eth header */
+	      clib_memcpy (dummy_eth1.dst_address, dummy_dst_address, 6);
+	      clib_memcpy (dummy_eth1.src_address, dummy_src_address, 6);
+	      dummy_eth1.type = 0x0800;
+	      vlib_buffer_advance (b1, -(word) sizeof (ethernet_header_t));
+	      hdr1 = vlib_buffer_get_current (b1);
+	      clib_memcpy (hdr1, &dummy_eth1,
+			   (word) sizeof (ethernet_header_t));
+
+	      sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
+	      nsp_nsi1 = nm->tunnel_index_by_sw_if_index[sw_if_index1];
+	    }
+	  else
+	    {
+	      memset (&key1, 0, sizeof (key1));
+	      key1.transport_type = NSH_NODE_NEXT_ENCAP_VXLAN4;
+	      key1.transport_index = vnet_buffer (b1)->sw_if_index[VLIB_RX];
+
+	      p1 = hash_get_mem (nm->nsh_proxy_session_by_key, &key1);
+	      if (PREDICT_FALSE (p1 == 0))
+		{
+		  error1 = NSH_NODE_ERROR_NO_PROXY;
+		  goto trace1;
+		}
+
+	      proxy1 = pool_elt_at_index (nm->nsh_proxy_sessions, p1[0]);
+	      if (PREDICT_FALSE (proxy1 == 0))
+		{
+		  error1 = NSH_NODE_ERROR_NO_PROXY;
+		  goto trace1;
+		}
+	      nsp_nsi1 = proxy1->nsp_nsi;
+	    }
+
 	  entry1 = hash_get_mem (nm->nsh_mapping_by_key, &nsp_nsi1);
 	  if (PREDICT_FALSE (entry1 == 0))
 	    {
@@ -2065,7 +2067,8 @@
 
 	    }
 
-	trace1:b1->error = error1 ? node->errors[error1] : 0;
+	trace1:
+	  b1->error = error1 ? node->errors[error1] : 0;
 
 	  if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
 	    {
@@ -2135,10 +2138,8 @@
 	  else if (node_type == NSH_AWARE_VNF_PROXY_TYPE)
 	    {
 	      /* Push dummy Eth header */
-	      memset (&dummy_eth0.dst_address[0], 0x11223344, 4);
-	      memset (&dummy_eth0.dst_address[4], 0x5566, 2);
-	      memset (&dummy_eth0.src_address[0], 0x778899aa, 4);
-	      memset (&dummy_eth0.src_address[4], 0xbbcc, 2);
+	      clib_memcpy (dummy_eth0.dst_address, dummy_dst_address, 6);
+	      clib_memcpy (dummy_eth0.src_address, dummy_src_address, 6);
 	      dummy_eth0.type = 0x0800;
 	      vlib_buffer_advance (b0, -(word) sizeof (ethernet_header_t));
 	      hdr0 = vlib_buffer_get_current (b0);