vhost: Add event index for interrupt notification to driver
[vpp.git] / src / vnet / devices / virtio / vhost_user.h
index 5bc3608..604e557 100644 (file)
  */
 #ifndef __VIRTIO_VHOST_USER_H__
 #define __VIRTIO_VHOST_USER_H__
+
+#include <vnet/devices/virtio/virtio_std.h>
+#include <vnet/devices/virtio/vhost_std.h>
+
 /* vhost-user data structures */
 
 #define VHOST_MEMORY_MAX_NREGIONS       8
 #define VHOST_USER_MSG_HDR_SZ           12
-#define VHOST_VRING_MAX_SIZE            32768
-#define VHOST_VRING_MAX_N               16     //8TX + 8RX
+#define VHOST_VRING_INIT_MQ_PAIR_SZ     8      //8TX + 8RX
+
+/*
+ * qid is one byte in size in the spec. Please see VHOST_USER_SET_VRING_CALL,
+ * VHOST_USER_SET_VRING_KICK, and VHOST_USER_SET_VRING_ERR.
+ * The max number for q pair is naturally 128.
+ */
+#define VHOST_VRING_MAX_MQ_PAIR_SZ      128
 #define VHOST_VRING_IDX_RX(qid)         (2*qid)
 #define VHOST_VRING_IDX_TX(qid)         (2*qid + 1)
 
 #define VHOST_USER_VRING_NOFD_MASK      0x100
-#define VIRTQ_DESC_F_NEXT               1
-#define VIRTQ_DESC_F_INDIRECT           4
-#define VHOST_USER_REPLY_MASK       (0x1 << 2)
 
 #define VHOST_USER_PROTOCOL_F_MQ   0
 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD        1
 #define VHOST_VRING_F_LOG 0
 
-#define VHOST_USER_F_PROTOCOL_FEATURES  30
 #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. */
-#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 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX        0x8000
-
-#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); \
@@ -79,32 +81,43 @@ typedef enum
 #undef _
 } virtio_trace_flag_t;
 
-#define foreach_virtio_net_feature      \
- _ (VIRTIO_NET_F_MRG_RXBUF, 15)         \
- _ (VIRTIO_NET_F_CTRL_VQ, 17)           \
- _ (VIRTIO_NET_F_GUEST_ANNOUNCE, 21)    \
- _ (VIRTIO_NET_F_MQ, 22)                \
- _ (VHOST_F_LOG_ALL, 26)                \
- _ (VIRTIO_F_ANY_LAYOUT, 27)            \
- _ (VIRTIO_F_INDIRECT_DESC, 28)         \
- _ (VHOST_USER_F_PROTOCOL_FEATURES, 30) \
- _ (VIRTIO_F_VERSION_1, 32)
+#define FEATURE_VIRTIO_NET_F_HOST_TSO_FEATURE_BITS \
+  (VIRTIO_FEATURE (VIRTIO_NET_F_CSUM) |                   \
+   VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO) |       \
+   VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |      \
+   VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6))
 
-typedef enum
+#define FEATURE_VIRTIO_NET_F_GUEST_TSO_FEATURE_BITS \
+  (VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM) |      \
+   VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO) |       \
+   VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4) |      \
+   VIRTIO_FEATURE (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)
+
+
+typedef struct
 {
-#define _(f,n) FEAT_##f = (n),
-  foreach_virtio_net_feature
-#undef _
-} virtio_net_feature_t;
+  char *sock_filename;
+  u64 feature_mask;
+  u32 custom_dev_instance;
+  u8 hwaddr[6];
+  u8 renumber;
+  u8 is_server;
+  u8 enable_gso;
+  u8 enable_packed;
+  u8 enable_event_idx;
+
+  /* return */
+  u32 sw_if_index;
+} vhost_user_create_if_args_t;
 
 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);
+                         vhost_user_create_if_args_t * args);
 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);
+                         vhost_user_create_if_args_t * args);
 int vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm,
                          u32 sw_if_index);
 
@@ -124,23 +137,6 @@ typedef struct vhost_user_memory
   vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS];
 } __attribute ((packed)) vhost_user_memory_t;
 
-typedef struct
-{
-  u32 index, num;
-} __attribute ((packed)) vhost_vring_state_t;
-
-typedef struct
-{
-  u32 index, flags;
-  u64 desc_user_addr, used_user_addr, avail_user_addr, log_guest_addr;
-} __attribute ((packed)) vhost_vring_addr_t;
-
-typedef struct vhost_user_log
-{
-  u64 size;
-  u64 offset;
-} __attribute ((packed)) vhost_user_log_t;
-
 typedef enum vhost_user_req
 {
   VHOST_USER_NONE = 0,
@@ -165,48 +161,6 @@ typedef enum vhost_user_req
   VHOST_USER_MAX
 } vhost_user_req_t;
 
-// vring_desc I/O buffer descriptor
-typedef struct
-{
-  uint64_t addr;  // packet data buffer address
-  uint32_t len;   // packet data buffer size
-  uint16_t flags; // (see below)
-  uint16_t next;  // optional index next descriptor in chain
-} __attribute ((packed)) vring_desc_t;
-
-typedef struct
-{
-  uint16_t flags;
-  volatile uint16_t idx;
-  uint16_t ring[VHOST_VRING_MAX_SIZE];
-} __attribute ((packed)) vring_avail_t;
-
-typedef struct
-{
-  uint16_t flags;
-  uint16_t idx;
-  struct /* vring_used_elem */
-    {
-      uint32_t id;
-      uint32_t len;
-    } ring[VHOST_VRING_MAX_SIZE];
-} __attribute ((packed)) vring_used_t;
-
-typedef struct
-{
-  u8 flags;
-  u8 gso_type;
-  u16 hdr_len;
-  u16 gso_size;
-  u16 csum_start;
-  u16 csum_offset;
-} __attribute ((packed)) virtio_net_hdr_t;
-
-typedef struct  {
-  virtio_net_hdr_t hdr;
-  u16 num_buffers;
-} __attribute ((packed)) virtio_net_hdr_mrg_rxbuf_t;
-
 typedef struct vhost_user_msg {
   vhost_user_req_t request;
   u32 flags;
@@ -229,13 +183,30 @@ 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;
   u8 log_used;
+  clib_spinlock_t vring_lock;
+
   //Put non-runtime in a different cache line
     CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
   int errfd;
@@ -245,6 +216,20 @@ 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;
+
+  u16 last_kick;
+  u8 first_kick;
 } vhost_user_vring_t;
 
 #define VHOST_USER_EVENT_START_TIMER 1
@@ -276,8 +261,15 @@ typedef struct
   u32 region_mmap_fd[VHOST_MEMORY_MAX_NREGIONS];
 
   //Virtual rings
-  vhost_user_vring_t vrings[VHOST_VRING_MAX_N];
-  volatile u32 *vring_locks[VHOST_VRING_MAX_N];
+  vhost_user_vring_t *vrings;
+
+  /*
+   * vrings is a dynamic array. It may have more elements than it is
+   * currently used. num_qid indicates the current total qid's in the
+   * vrings. For example, vec_len(vrings) = 64, num_qid = 60, so the
+   * current valid/used qid is (0, 59) in the vrings array.
+   */
+  u32 num_qid;
 
   int virtio_net_hdr_sz;
   int is_any_layout;
@@ -289,8 +281,12 @@ 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;
+
+  u8 enable_event_idx;
 } vhost_user_intf_t;
 
 typedef struct
@@ -309,7 +305,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 +319,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 +343,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