udp: fix tx handling of non-connected sessions

Type: fix

Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: I659b9914fcfa4619a68e9807ef241f88c96b3bd0
diff --git a/src/vnet/udp/udp.c b/src/vnet/udp/udp.c
index 710d1ac..e095a66 100644
--- a/src/vnet/udp/udp.c
+++ b/src/vnet/udp/udp.c
@@ -223,9 +223,27 @@
   vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port,
 			udp_csum_offload (uc));
   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
+
+  /* Handle ip header now as session layer overwrite connection details for
+   * non-connected udp. */
+  if (uc->c_is_ip4)
+    vlib_buffer_push_ip4_custom (vm, b, &uc->c_lcl_ip4, &uc->c_rmt_ip4,
+				 IP_PROTOCOL_UDP, udp_csum_offload (uc),
+				 0 /* is_df */, uc->c_dscp);
+  else
+    vlib_buffer_push_ip6 (vm, b, &uc->c_lcl_ip6, &uc->c_rmt_ip6,
+			  IP_PROTOCOL_UDP);
+
   /* reuse tcp medatada for now */
   vnet_buffer (b)->tcp.connection_index = uc->c_c_index;
 
+  /* Not connected udp session. Mark buffer for custom handling in
+   * udp_output */
+  if (PREDICT_FALSE (!(uc->flags & UDP_CONN_F_CONNECTED)))
+    vnet_buffer (b)->tcp.flags |= UDP_CONN_F_LISTEN;
+  else
+    vnet_buffer (b)->tcp.flags = 0;
+
   return 0;
 }
 
diff --git a/src/vnet/udp/udp_output.c b/src/vnet/udp/udp_output.c
index 3ab21d3..bcebf5a 100644
--- a/src/vnet/udp/udp_output.c
+++ b/src/vnet/udp/udp_output.c
@@ -76,21 +76,6 @@
 }
 
 always_inline void
-udp_output_push_ip (vlib_main_t *vm, vlib_buffer_t *b, udp_connection_t *uc,
-		    u8 is_ip4)
-{
-  if (uc->c_is_ip4)
-    vlib_buffer_push_ip4_custom (vm, b, &uc->c_lcl_ip4, &uc->c_rmt_ip4,
-				 IP_PROTOCOL_UDP, udp_csum_offload (uc),
-				 0 /* is_df */, uc->c_dscp);
-  else
-    vlib_buffer_push_ip6 (vm, b, &uc->c_lcl_ip6, &uc->c_rmt_ip6,
-			  IP_PROTOCOL_UDP);
-  vnet_buffer (b)->sw_if_index[VLIB_RX] = uc->sw_if_index;
-  vnet_buffer (b)->sw_if_index[VLIB_TX] = uc->c_fib_index;
-}
-
-always_inline void
 udp_output_handle_packet (udp_connection_t *uc0, vlib_buffer_t *b0,
 			  vlib_node_runtime_t *error_node, u16 *next0,
 			  u8 is_ip4)
@@ -110,6 +95,16 @@
   vnet_buffer (b0)->sw_if_index[VLIB_RX] = uc0->sw_if_index;
 }
 
+always_inline udp_connection_t *
+udp_output_get_connection (vlib_buffer_t *b, u32 thread_index)
+{
+  if (PREDICT_FALSE (vnet_buffer (b)->tcp.flags & UDP_CONN_F_LISTEN))
+    return udp_listener_get (vnet_buffer (b)->tcp.connection_index);
+
+  return udp_connection_get (vnet_buffer (b)->tcp.connection_index,
+			     thread_index);
+}
+
 always_inline uword
 udp46_output_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
 		     vlib_frame_t *frame, int is_ip4)
@@ -138,16 +133,11 @@
       vlib_prefetch_buffer_header (b[3], STORE);
       CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, STORE);
 
-      uc0 = udp_connection_get (vnet_buffer (b[0])->tcp.connection_index,
-				thread_index);
-      uc1 = udp_connection_get (vnet_buffer (b[1])->tcp.connection_index,
-				thread_index);
+      uc0 = udp_output_get_connection (b[0], thread_index);
+      uc1 = udp_output_get_connection (b[1], thread_index);
 
       if (PREDICT_TRUE (!uc0 + !uc1 == 0))
 	{
-	  udp_output_push_ip (vm, b[0], uc0, is_ip4);
-	  udp_output_push_ip (vm, b[1], uc1, is_ip4);
-
 	  udp_output_handle_packet (uc0, b[0], node, &next[0], is_ip4);
 	  udp_output_handle_packet (uc1, b[1], node, &next[1], is_ip4);
 	}
@@ -155,7 +145,6 @@
 	{
 	  if (uc0 != 0)
 	    {
-	      udp_output_push_ip (vm, b[0], uc0, is_ip4);
 	      udp_output_handle_packet (uc0, b[0], node, &next[0], is_ip4);
 	    }
 	  else
@@ -165,7 +154,6 @@
 	    }
 	  if (uc1 != 0)
 	    {
-	      udp_output_push_ip (vm, b[1], uc1, is_ip4);
 	      udp_output_handle_packet (uc1, b[1], node, &next[1], is_ip4);
 	    }
 	  else
@@ -189,12 +177,10 @@
 	  CLIB_PREFETCH (b[1]->data, 2 * CLIB_CACHE_LINE_BYTES, STORE);
 	}
 
-      uc0 = udp_connection_get (vnet_buffer (b[0])->tcp.connection_index,
-				thread_index);
+      uc0 = udp_output_get_connection (b[0], thread_index);
 
       if (PREDICT_TRUE (uc0 != 0))
 	{
-	  udp_output_push_ip (vm, b[0], uc0, is_ip4);
 	  udp_output_handle_packet (uc0, b[0], node, &next[0], is_ip4);
 	}
       else