virtio: support virtio 1.1 packed ring in vhost
[vpp.git] / src / vnet / devices / virtio / vhost_user.h
index ff06695..b86f42e 100644 (file)
 
 #define VHOST_USER_VRING_NOFD_MASK      0x100
 #define VIRTQ_DESC_F_NEXT               1
+#define VIRTQ_DESC_F_WRITE              2
 #define VIRTQ_DESC_F_INDIRECT           4
-#define VHOST_USER_REPLY_MASK       (0x1 << 2)
+
+#define VIRTQ_DESC_F_AVAIL             (1 << 7)
+#define VIRTQ_DESC_F_USED              (1 << 15)
+
+#define VRING_EVENT_F_ENABLE            0x0
+#define VRING_EVENT_F_DISABLE           0x1
+#define VRING_EVENT_F_DESC              0x2
 
 #define VHOST_USER_PROTOCOL_F_MQ   0
 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD        1
@@ -36,7 +43,7 @@
 #define VHOST_USER_PROTOCOL_FEATURES   ((1ULL << VHOST_USER_PROTOCOL_F_MQ) |   \
                                        (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD))
 
-/* If multiqueue is provided by host, then we suppport it. */
+/* If multiqueue is provided by host, then we support it. */
 #define VIRTIO_NET_CTRL_MQ   4
 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET        0
 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN        1
 #define VRING_USED_F_NO_NOTIFY  1
 #define VRING_AVAIL_F_NO_INTERRUPT 1
 
-#define DBG_SOCK(args...)                       \
-  {                                             \
-    vhost_user_main_t *_vum = &vhost_user_main; \
-    if (_vum->debug)                            \
-      clib_warning(args);                       \
-  };
-
-#define VHOST_DEBUG_VQ 0
-
-#if VHOST_DEBUG_VQ == 1
-#define DBG_VQ(args...) clib_warning(args);
-#else
-#define DBG_VQ(args...)
-#endif
+#define vu_log_debug(dev, f, ...) \
+{                                                                             \
+  vlib_log(VLIB_LOG_LEVEL_DEBUG, vhost_user_main.log_default, "%U: " f,       \
+          format_vnet_hw_if_index_name, vnet_get_main(),                     \
+          dev->hw_if_index, ##__VA_ARGS__);                                  \
+};
+
+#define vu_log_warn(dev, f, ...) \
+{                                                                             \
+  vlib_log(VLIB_LOG_LEVEL_WARNING, vhost_user_main.log_default, "%U: " f,     \
+          format_vnet_hw_if_index_name, vnet_get_main(),                     \
+          dev->hw_if_index, ##__VA_ARGS__);                                  \
+};
+#define vu_log_err(dev, f, ...) \
+{                                                                             \
+  vlib_log(VLIB_LOG_LEVEL_ERR, vhost_user_main.log_default, "%U: " f,         \
+          format_vnet_hw_if_index_name, vnet_get_main(),                     \
+          dev->hw_if_index, ##__VA_ARGS__);                                  \
+};
 
 #define UNIX_GET_FD(unixfd_idx) ({ \
     typeof(unixfd_idx) __unixfd_idx = (unixfd_idx); \
@@ -80,6 +92,14 @@ typedef enum
 } virtio_trace_flag_t;
 
 #define foreach_virtio_net_feature      \
+ _ (VIRTIO_NET_F_CSUM, 0)              \
+ _ (VIRTIO_NET_F_GUEST_CSUM, 1)                \
+ _ (VIRTIO_NET_F_GUEST_TSO4, 7)         \
+ _ (VIRTIO_NET_F_GUEST_TSO6, 8)         \
+ _ (VIRTIO_NET_F_GUEST_UFO, 10)         \
+ _ (VIRTIO_NET_F_HOST_TSO4, 11)         \
+ _ (VIRTIO_NET_F_HOST_TSO6, 12)         \
+ _ (VIRTIO_NET_F_HOST_UFO, 14)          \
  _ (VIRTIO_NET_F_MRG_RXBUF, 15)         \
  _ (VIRTIO_NET_F_CTRL_VQ, 17)           \
  _ (VIRTIO_NET_F_GUEST_ANNOUNCE, 21)    \
@@ -87,8 +107,11 @@ typedef enum
  _ (VHOST_F_LOG_ALL, 26)                \
  _ (VIRTIO_F_ANY_LAYOUT, 27)            \
  _ (VIRTIO_F_INDIRECT_DESC, 28)         \
+ _ (VIRTIO_F_EVENT_IDX, 29)            \
  _ (VHOST_USER_F_PROTOCOL_FEATURES, 30) \
- _ (VIRTIO_F_VERSION_1, 32)
+ _ (VIRTIO_F_VERSION_1, 32)             \
+ _ (VIRTIO_F_RING_PACKED, 34)           \
+ _ (VIRTIO_F_IN_ORDER, 35)
 
 typedef enum
 {
@@ -97,14 +120,32 @@ typedef enum
 #undef _
 } virtio_net_feature_t;
 
+#define FEATURE_VIRTIO_NET_F_HOST_TSO_FEATURE_BITS \
+  ((1ULL << FEAT_VIRTIO_NET_F_CSUM) |             \
+   (1ULL << FEAT_VIRTIO_NET_F_HOST_UFO) |         \
+   (1ULL << FEAT_VIRTIO_NET_F_HOST_TSO4) |        \
+   (1ULL << FEAT_VIRTIO_NET_F_HOST_TSO6))
+
+#define FEATURE_VIRTIO_NET_F_GUEST_TSO_FEATURE_BITS \
+  ((1ULL << FEAT_VIRTIO_NET_F_GUEST_CSUM) |        \
+   (1ULL << FEAT_VIRTIO_NET_F_GUEST_UFO) |         \
+   (1ULL << FEAT_VIRTIO_NET_F_GUEST_TSO4) |        \
+   (1ULL << FEAT_VIRTIO_NET_F_GUEST_TSO6))
+
+#define FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS \
+  (FEATURE_VIRTIO_NET_F_HOST_TSO_FEATURE_BITS |                 \
+   FEATURE_VIRTIO_NET_F_GUEST_TSO_FEATURE_BITS)
+
 int vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
                          const char *sock_filename, u8 is_server,
                          u32 * sw_if_index, u64 feature_mask,
-                         u8 renumber, u32 custom_dev_instance, u8 * hwaddr);
+                         u8 renumber, u32 custom_dev_instance, u8 * hwaddr,
+                         u8 enable_gso, u8 enable_packed);
 int vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
                          const char *sock_filename, u8 is_server,
                          u32 sw_if_index, u64 feature_mask,
-                         u8 renumber, u32 custom_dev_instance);
+                         u8 renumber, u32 custom_dev_instance,
+                         u8 enable_gso, u8 enable_packed);
 int vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm,
                          u32 sw_if_index);
 
@@ -192,6 +233,22 @@ typedef struct
     } ring[VHOST_VRING_MAX_SIZE];
 } __attribute ((packed)) vring_used_t;
 
+typedef CLIB_PACKED (struct
+{
+  u64 addr;                    // packet data buffer address
+  u32 len;                     // packet data buffer size
+  u16 id;                      // buffer id
+  u16 flags;                   // flags
+}) vring_packed_desc_t;
+
+STATIC_ASSERT_SIZEOF (vring_packed_desc_t, 16);
+
+typedef CLIB_PACKED (struct
+{
+  u16 off_wrap;
+  u16 flags;
+}) vring_desc_event_t;
+
 typedef struct
 {
   u8 flags;
@@ -229,9 +286,24 @@ typedef struct
   u16 last_avail_idx;
   u16 last_used_idx;
   u16 n_since_last_int;
-  vring_desc_t *desc;
-  vring_avail_t *avail;
-  vring_used_t *used;
+  union
+  {
+    vring_desc_t *desc;
+    vring_packed_desc_t *packed_desc;
+  };
+  union
+  {
+    vring_avail_t *avail;
+    vring_desc_event_t *avail_event;
+  };
+  union
+  {
+    vring_used_t *used;
+    vring_desc_event_t *used_event;
+  };
+  uword desc_user_addr;
+  uword used_user_addr;
+  uword avail_user_addr;
   f64 int_deadline;
   u8 started;
   u8 enabled;
@@ -245,6 +317,17 @@ typedef struct
 
   /* The rx queue policy (interrupt/adaptive/polling) for this queue */
   u32 mode;
+
+  /*
+   * It contains the device queue number. -1 if it does not. The idea is
+   * to not invoke vnet_hw_interface_assign_rx_thread and
+   * vnet_hw_interface_unassign_rx_thread more than once for the duration of
+   * the interface even if it is disconnected and reconnected.
+   */
+  i16 qid;
+
+  u16 used_wrap_counter;
+  u16 avail_wrap_counter;
 } vhost_user_vring_t;
 
 #define VHOST_USER_EVENT_START_TIMER 1
@@ -253,7 +336,7 @@ typedef struct
 typedef struct
 {
   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
-  u32 is_up;
+  u32 is_ready;
   u32 admin_up;
   u32 unix_server_index;
   u32 clib_file_index;
@@ -289,8 +372,11 @@ typedef struct
   u8 use_tx_spinlock;
   u16 *per_cpu_tx_qid;
 
-  /* Vector of active rx queues for this interface */
-  u16 *rx_queues;
+  u8 enable_gso;
+
+  /* Packed ring configured */
+  u8 enable_packed;
+
 } vhost_user_intf_t;
 
 typedef struct
@@ -309,7 +395,6 @@ typedef struct
   virtio_net_hdr_mrg_rxbuf_t hdr; /** Virtio header **/
 } vhost_trace_t;
 
-
 #define VHOST_USER_RX_BUFFERS_N (2 * VLIB_FRAME_SIZE + 2)
 #define VHOST_USER_COPY_ARRAY_N (4 * VLIB_FRAME_SIZE)
 
@@ -324,6 +409,9 @@ typedef struct
   /* This is here so it doesn't end-up
    * using stack or registers. */
   vhost_trace_t *current_trace;
+
+  u32 *to_next_list;
+  vlib_buffer_t **rx_buffers_pdesc;
 } vhost_cpu_t;
 
 typedef struct
@@ -345,8 +433,11 @@ typedef struct
   /* The number of rx interface/queue pairs in interrupt mode */
   u32 ifq_count;
 
-  /* debug on or off */
-  u8 debug;
+  /* logging */
+  vlib_log_class_t log_default;
+
+  /* gso interface count */
+  u32 gso_count;
 } vhost_user_main_t;
 
 typedef struct