virtio: virtio_flags api use enumflag instead of enum
[vpp.git] / src / vnet / devices / virtio / node.c
index 9c4ea90..b58c753 100644 (file)
 #include <vlib/unix/unix.h>
 #include <vnet/ethernet/ethernet.h>
 #include <vnet/feature/feature.h>
-#include <vnet/gso/gro_func.h>
 #include <vnet/interface/rx_queue_funcs.h>
 #include <vnet/ip/ip4_packet.h>
 #include <vnet/ip/ip6_packet.h>
 #include <vnet/udp/udp_packet.h>
+#include <vnet/tcp/tcp_packet.h>
 #include <vnet/devices/virtio/virtio.h>
 #include <vnet/devices/virtio/virtio_inline.h>
 
@@ -47,7 +47,7 @@ typedef struct
   u32 hw_if_index;
   u16 ring;
   u16 len;
-  virtio_net_hdr_v1_t hdr;
+  vnet_virtio_net_hdr_v1_t hdr;
 } virtio_input_trace_t;
 
 static u8 *
@@ -69,8 +69,8 @@ format_virtio_input_trace (u8 * s, va_list * args)
 }
 
 static_always_inline void
-virtio_needs_csum (vlib_buffer_t * b0, virtio_net_hdr_v1_t * hdr,
-                  u8 * l4_proto, u8 * l4_hdr_sz, virtio_if_type_t type)
+virtio_needs_csum (vlib_buffer_t *b0, vnet_virtio_net_hdr_v1_t *hdr,
+                  u8 *l4_proto, u8 *l4_hdr_sz, virtio_if_type_t type)
 {
   if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
     {
@@ -91,8 +91,7 @@ virtio_needs_csum (vlib_buffer_t * b0, virtio_net_hdr_v1_t * hdr,
        }
       else
        {
-         ethernet_header_t *eh =
-           (ethernet_header_t *) vlib_buffer_get_current (b0);
+         ethernet_header_t *eh = (ethernet_header_t *) b0->data;
          ethertype = clib_net_to_host_u16 (eh->type);
          l2hdr_sz = sizeof (ethernet_header_t);
 
@@ -117,8 +116,7 @@ virtio_needs_csum (vlib_buffer_t * b0, virtio_net_hdr_v1_t * hdr,
 
       if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4))
        {
-         ip4_header_t *ip4 =
-           (ip4_header_t *) (vlib_buffer_get_current (b0) + l2hdr_sz);
+         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;
          oflags |= VNET_BUFFER_OFFLOAD_F_IP_CKSUM;
@@ -129,8 +127,7 @@ virtio_needs_csum (vlib_buffer_t * b0, virtio_net_hdr_v1_t * hdr,
        }
       else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6))
        {
-         ip6_header_t *ip6 =
-           (ip6_header_t *) (vlib_buffer_get_current (b0) + l2hdr_sz);
+         ip6_header_t *ip6 = (ip6_header_t *) (b0->data + l2hdr_sz);
          vnet_buffer (b0)->l4_hdr_offset = l2hdr_sz + sizeof (ip6_header_t);
          /* FIXME IPv6 EH traversal */
          *l4_proto = ip6->protocol;
@@ -142,18 +139,14 @@ virtio_needs_csum (vlib_buffer_t * b0, virtio_net_hdr_v1_t * hdr,
       if (*l4_proto == IP_PROTOCOL_TCP)
        {
          oflags |= VNET_BUFFER_OFFLOAD_F_TCP_CKSUM;
-         tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b0) +
-                                               vnet_buffer
-                                               (b0)->l4_hdr_offset);
+         tcp_header_t *tcp =
+           (tcp_header_t *) (b0->data + vnet_buffer (b0)->l4_hdr_offset);
          *l4_hdr_sz = tcp_header_bytes (tcp);
        }
       else if (*l4_proto == IP_PROTOCOL_UDP)
        {
          oflags |= VNET_BUFFER_OFFLOAD_F_UDP_CKSUM;
-         udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b0) +
-                                               vnet_buffer
-                                               (b0)->l4_hdr_offset);
-         *l4_hdr_sz = sizeof (*udp);
+         *l4_hdr_sz = sizeof (udp_header_t);
        }
       if (oflags)
        vnet_buffer_offload_flags_set (b0, oflags);
@@ -161,7 +154,7 @@ virtio_needs_csum (vlib_buffer_t * b0, virtio_net_hdr_v1_t * hdr,
 }
 
 static_always_inline void
-fill_gso_buffer_flags (vlib_buffer_t * b0, virtio_net_hdr_v1_t * hdr,
+fill_gso_buffer_flags (vlib_buffer_t *b0, vnet_virtio_net_hdr_v1_t *hdr,
                       u8 l4_proto, u8 l4_hdr_sz)
 {
   if (hdr->gso_type == VIRTIO_NET_HDR_GSO_TCPV4)
@@ -181,7 +174,7 @@ fill_gso_buffer_flags (vlib_buffer_t * b0, virtio_net_hdr_v1_t * hdr,
 }
 
 static_always_inline u16
-virtio_n_left_to_process (virtio_vring_t * vring, const int packed)
+virtio_n_left_to_process (vnet_virtio_vring_t *vring, const int packed)
 {
   if (packed)
     return vring->desc_in_use;
@@ -190,7 +183,7 @@ virtio_n_left_to_process (virtio_vring_t * vring, const int packed)
 }
 
 static_always_inline u16
-virtio_get_slot_id (virtio_vring_t * vring, const int packed, u16 last,
+virtio_get_slot_id (vnet_virtio_vring_t *vring, const int packed, u16 last,
                    u16 mask)
 {
   if (packed)
@@ -200,7 +193,7 @@ virtio_get_slot_id (virtio_vring_t * vring, const int packed, u16 last,
 }
 
 static_always_inline u16
-virtio_get_len (virtio_vring_t * vring, const int packed, const int hdr_sz,
+virtio_get_len (vnet_virtio_vring_t *vring, const int packed, const int hdr_sz,
                u16 last, u16 mask)
 {
   if (packed)
@@ -209,22 +202,47 @@ virtio_get_len (virtio_vring_t * vring, const int packed, const int hdr_sz,
     return vring->used->ring[last & mask].len - hdr_sz;
 }
 
-#define increment_last(last, packed, vring) \
-   do {                                            \
-         last++;                            \
-         if (packed && last >= vring->size) \
-           {                                \
-             last = 0;                      \
-             vring->used_wrap_counter ^= 1; \
-           }                                \
-    } while (0)
+#define increment_last(last, packed, vring)                                   \
+  do                                                                          \
+    {                                                                         \
+      last++;                                                                 \
+      if (packed && last >= vring->queue_size)                                \
+       {                                                                     \
+         last = 0;                                                           \
+         vring->used_wrap_counter ^= 1;                                      \
+       }                                                                     \
+    }                                                                         \
+  while (0)
+
+static_always_inline void
+virtio_device_input_ethernet (vlib_main_t *vm, vlib_node_runtime_t *node,
+                             const u32 next_index, const u32 sw_if_index,
+                             const u32 hw_if_index)
+{
+  vlib_next_frame_t *nf;
+  vlib_frame_t *f;
+  ethernet_input_frame_t *ef;
+
+  if (PREDICT_FALSE (VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT != next_index))
+    return;
+
+  nf = vlib_node_runtime_get_next_frame (
+    vm, node, VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT);
+  f = vlib_get_frame (vm, nf->frame);
+  f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX;
+
+  ef = vlib_frame_scalar_args (f);
+  ef->sw_if_index = sw_if_index;
+  ef->hw_if_index = hw_if_index;
+  vlib_frame_no_append (f);
+}
 
 static_always_inline uword
-virtio_device_input_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
-                               vlib_frame_t * frame, virtio_if_t * vif,
-                               virtio_vring_t * vring, virtio_if_type_t type,
-                               int gso_enabled, int checksum_offload_enabled,
-                               int packed)
+virtio_device_input_gso_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
+                               vlib_frame_t *frame, virtio_if_t *vif,
+                               vnet_virtio_vring_t *vring,
+                               virtio_if_type_t type, int gso_enabled,
+                               int checksum_offload_enabled, int packed)
 {
   vnet_main_t *vnm = vnet_get_main ();
   u32 thread_index = vm->thread_index;
@@ -234,7 +252,7 @@ virtio_device_input_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
   u32 *to_next = 0;
   u32 n_rx_packets = 0;
   u32 n_rx_bytes = 0;
-  u16 mask = vring->size - 1;
+  u16 mask = vring->queue_size - 1;
   u16 last = vring->last_used_idx;
   u16 n_left = virtio_n_left_to_process (vring, packed);
   vlib_buffer_t bt = {};
@@ -253,7 +271,7 @@ virtio_device_input_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
        next_index = vif->per_interface_next_index;
 
       /* only for l2, redirect if feature path enabled */
-      vnet_feature_start_device_input_x1 (vif->sw_if_index, &next_index, &bt);
+      vnet_feature_start_device_input (vif->sw_if_index, &next_index, &bt);
     }
 
   while (n_left)
@@ -261,13 +279,13 @@ virtio_device_input_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
       u32 n_left_to_next;
       u32 next0 = next_index;
 
-      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
+      vlib_get_new_next_frame (vm, node, next_index, to_next, n_left_to_next);
 
       while (n_left && n_left_to_next)
        {
          if (packed)
            {
-             vring_packed_desc_t *d = &vring->packed_desc[last];
+             vnet_virtio_vring_packed_desc_t *d = &vring->packed_desc[last];
              u16 flags = d->flags;
              if ((flags & VRING_DESC_F_AVAIL) !=
                  (vring->used_wrap_counter << 7)
@@ -280,13 +298,13 @@ virtio_device_input_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
            }
          u8 l4_proto = 0, l4_hdr_sz = 0;
          u16 num_buffers = 1;
-         virtio_net_hdr_v1_t *hdr;
+         vnet_virtio_net_hdr_v1_t *hdr;
          u16 slot = virtio_get_slot_id (vring, packed, last, mask);
          u16 len = virtio_get_len (vring, packed, hdr_sz, last, mask);
          u32 bi0 = vring->buffers[slot];
          vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
          hdr = vlib_buffer_get_current (b0);
-         if (hdr_sz == sizeof (virtio_net_hdr_v1_t))
+         if (hdr_sz == sizeof (vnet_virtio_net_hdr_v1_t))
            num_buffers = hdr->num_buffers;
 
          b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
@@ -391,10 +409,13 @@ virtio_device_input_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
          n_rx_packets++;
          n_rx_bytes += len;
        }
+      virtio_device_input_ethernet (vm, node, next_index, vif->sw_if_index,
+                                   vif->hw_if_index);
       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
     }
   vring->last_used_idx = last;
 
+  vring->total_packets += n_rx_packets;
   vlib_increment_combined_counter (vnm->interface_main.combined_sw_if_counters
                                   + VNET_INTERFACE_COUNTER_RX, thread_index,
                                   vif->sw_if_index, n_rx_packets,
@@ -408,23 +429,10 @@ virtio_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
                            vlib_frame_t * frame, virtio_if_t * vif, u16 qid,
                            virtio_if_type_t type)
 {
-  virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid);
+  vnet_virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid);
   const int hdr_sz = vif->virtio_net_hdr_sz;
-  u16 txq_id = vm->thread_index % vif->num_txqs;
-  virtio_vring_t *txq_vring = vec_elt_at_index (vif->txq_vrings, txq_id);
   uword rv;
 
-  if (clib_spinlock_trylock_if_init (&txq_vring->lockp))
-    {
-      if (vif->packet_coalesce)
-       vnet_gro_flow_table_schedule_node_on_dispatcher
-         (vm, txq_vring->flow_table);
-      else if (vif->packet_buffering)
-       virtio_vring_buffering_schedule_node_on_dispatcher
-         (vm, txq_vring->buffering);
-      clib_spinlock_unlock_if_init (&txq_vring->lockp);
-    }
-
   if (vif->is_packed)
     {
       if (vif->gso_enabled)