pg: add GSO support

Type: feature

Change-Id: I72676495a85fbecc946aa266a75234cce70c3a5e
Signed-off-by: Mohsin Kazmi <sykazmi@cisco.com>
diff --git a/src/vnet/pg/input.c b/src/vnet/pg/input.c
index 2d850e9..151624f 100644
--- a/src/vnet/pg/input.c
+++ b/src/vnet/pg/input.c
@@ -50,6 +50,9 @@
 #include <vnet/vnet.h>
 #include <vnet/ethernet/ethernet.h>
 #include <vnet/feature/feature.h>
+#include <vnet/ip/ip4_packet.h>
+#include <vnet/ip/ip6_packet.h>
+#include <vnet/udp/udp_packet.h>
 #include <vnet/devices/devices.h>
 
 static int
@@ -1523,6 +1526,70 @@
     }
 }
 
+static_always_inline void
+fill_gso_buffer_flags (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
+		       u32 packet_data_size)
+{
+
+  for (int i = 0; i < n_buffers; i++)
+    {
+      vlib_buffer_t *b0 = vlib_get_buffer (vm, buffers[i]);
+      u8 l4_proto = 0;
+      u8 l4_hdr_sz = 0;
+
+      ethernet_header_t *eh = (ethernet_header_t *) b0->data;
+      u16 ethertype = clib_net_to_host_u16 (eh->type);
+      u16 l2hdr_sz = sizeof (ethernet_header_t);
+
+      vnet_buffer (b0)->l2_hdr_offset = 0;
+      vnet_buffer (b0)->l3_hdr_offset = l2hdr_sz;
+      if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4))
+	{
+	  ip4_header_t *ip4 = (ip4_header_t *) (b0->data + l2hdr_sz);
+	  vnet_buffer (b0)->l4_hdr_offset = l2hdr_sz + ip4_header_bytes (ip4);
+	  l4_proto = ip4->protocol;
+	  b0->flags |=
+	    (VNET_BUFFER_F_IS_IP4 | VNET_BUFFER_F_L2_HDR_OFFSET_VALID
+	     | VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
+	     VNET_BUFFER_F_L4_HDR_OFFSET_VALID);
+	  b0->flags |= VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
+	}
+      else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6))
+	{
+	  ip6_header_t *ip6 = (ip6_header_t *) (b0->data + l2hdr_sz);
+	  /* FIXME IPv6 EH traversal */
+	  vnet_buffer (b0)->l4_hdr_offset = l2hdr_sz + sizeof (ip6_header_t);
+	  l4_proto = ip6->protocol;
+	  b0->flags |=
+	    (VNET_BUFFER_F_IS_IP6 | VNET_BUFFER_F_L2_HDR_OFFSET_VALID
+	     | VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
+	     VNET_BUFFER_F_L4_HDR_OFFSET_VALID);
+	  b0->flags |= VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
+	}
+      if (l4_proto == IP_PROTOCOL_TCP)
+	{
+	  b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
+	  tcp_header_t *tcp = (tcp_header_t *) (b0->data +
+						vnet_buffer
+						(b0)->l4_hdr_offset);
+	  l4_hdr_sz = tcp_header_bytes (tcp);
+	  tcp->checksum = 0;
+	  vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz;
+	  vnet_buffer2 (b0)->gso_size = packet_data_size;
+	  b0->flags |= VNET_BUFFER_F_GSO;
+	}
+      else if (l4_proto == IP_PROTOCOL_UDP)
+	{
+	  b0->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
+	  udp_header_t *udp = (udp_header_t *) (b0->data +
+						vnet_buffer
+						(b0)->l4_hdr_offset);
+	  vnet_buffer2 (b0)->gso_l4_hdr_sz = sizeof (*udp);
+	  udp->checksum = 0;
+	}
+    }
+}
+
 static uword
 pg_generate_packets (vlib_node_runtime_t * node,
 		     pg_main_t * pg,
@@ -1538,6 +1605,7 @@
   u8 feature_arc_index = fm->device_input_feature_arc_index;
   cm = &fm->feature_config_mains[feature_arc_index];
   u32 current_config_index = ~(u32) 0;
+  pg_interface_t *pi = pool_elt_at_index (pg->interfaces, s->pg_if_index);
   int i;
 
   bi0 = s->buffer_indices;
@@ -1564,14 +1632,12 @@
 	  vlib_next_frame_t *nf;
 	  vlib_frame_t *f;
 	  ethernet_input_frame_t *ef;
-	  pg_interface_t *pi;
 	  vlib_get_new_next_frame (vm, node, next_index, to_next, n_left);
 	  nf = vlib_node_runtime_get_next_frame (vm, node, next_index);
 	  f = vlib_get_frame (vm, nf->frame);
 	  f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX;
 
 	  ef = vlib_frame_scalar_args (f);
-	  pi = pool_elt_at_index (pg->interfaces, s->pg_if_index);
 	  ef->sw_if_index = pi->sw_if_index;
 	  ef->hw_if_index = pi->hw_if_index;
 	  vlib_frame_no_append (f);
@@ -1615,6 +1681,9 @@
 	    vnet_buffer (b)->feature_arc_index = feature_arc_index;
 	  }
 
+      if (pi->gso_enabled)
+	fill_gso_buffer_flags (vm, to_next, n_this_frame, pi->gso_size);
+
       n_trace = vlib_get_trace_count (vm, node);
       if (n_trace > 0)
 	{