summaryrefslogtreecommitdiffstats
path: root/src/vnet/devices/virtio/vhost_user.h
blob: 604e5571141617e990ff04192a2f66e9673ea2bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bi
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#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_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 VHOST_USER_PROTOCOL_F_MQ   0
#define VHOST_USER_PROTOCOL_F_LOG_SHMFD	1
#define VHOST_VRING_F_LOG 0

#define VHOST_USER_PROTOCOL_FEATURES   ((1ULL << VHOST_USER_PROTOCOL_F_MQ) |	\
					(1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD))

#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); \
    (__unixfd_idx != ~0) ? \
        pool_elt_at_index (file_main.file_pool, \
                           __unixfd_idx)->file_descriptor : -1; })

#define foreach_virtio_trace_flags \
  _ (SIMPLE_CHAINED, 0, "Simple descriptor chaining") \
  _ (SINGLE_DESC,  1, "Single descriptor packet") \
  _ (INDIRECT, 2, "Indirect descriptor") \
  _ (MAP_ERROR, 4, "Memory mapping error")

typedef enum
{
#define _(n,i,s) VIRTIO_TRACE_F_##n,
  foreach_virtio_trace_flags
#undef _
} virtio_trace_flag_t;

#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))

#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
{
  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,
			  vhost_user_create_if_args_t * args);
int vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
			  vhost_user_create_if_args_t * args);
int vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm,
			  u32 sw_if_index);

/* *INDENT-OFF* */
typedef struct vhost_user_memory_region
{
  u64 guest_phys_addr;
  u64 memory_size;
  u64 userspace_addr;
  u64 mmap_offset;
} __attribute ((packed)) vhost_user_memory_region_t;

typedef struct vhost_user_memory
{
  u32 nregions;
  u32 padding;
  vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS];
} __attribute ((packed)) vhost_user_memory_t;

typedef enum vhost_user_req
{
  VHOST_USER_NONE = 0,
  VHOST_USER_GET_FEATURES = 1,
  VHOST_USER_SET_FEATURES = 2,
  VHOST_USER_SET_OWNER = 3,
  VHOST_USER_RESET_OWNER = 4,
  VHOST_USER_SET_MEM_TABLE = 5,
  VHOST_USER_SET_LOG_BASE = 6,
  VHOST_USER_SET_LOG_FD = 7,
  VHOST_USER_SET_VRING_NUM = 8,
  VHOST_USER_SET_VRING_ADDR = 9,
  VHOST_USER_SET_VRING_BASE = 10,
  VHOST_USER_GET_VRING_BASE = 11,
  VHOST_USER_SET_VRING_KICK = 12,
  VHOST_USER_SET_VRING_CALL = 13,
  VHOST_USER_SET_VRING_ERR = 14,
  VHOST_USER_GET_PROTOCOL_FEATURES = 15,
  VHOST_USER_SET_PROTOCOL_FEATURES = 16,
  VHOST_USER_GET_QUEUE_NUM = 17,
  VHOST_USER_SET_VRING_ENABLE = 18,
  VHOST_USER_MAX
} vhost_user_req_t;

typedef struct vhost_user_msg {
  vhost_user_req_t request;
  u32 flags;
  u32 size;
  union
    {
      u64 u64;
      vhost_vring_state_t state;
      vhost_vring_addr_t addr;
      vhost_user_memory_t memory;
      vhost_user_log_t log;
    };
} __attribute ((packed)) vhost_user_msg_t;
/* *INDENT-ON* */

typedef struct
{
  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
  u16 qsz_mask;
  u16 last_avail_idx;
  u16 last_used_idx;
  u16 n_since_last_int;
  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;
  u32 callfd_idx;
  u32 kickfd_idx;
  u64 log_guest_addr;

  /* 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
#define VHOST_USER_EVENT_STOP_TIMER  2

typedef struct
{
  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
  u32 is_ready;
  u32 admin_up;
  u32 unix_server_index;
  u32 clib_file_index;
  char sock_filename[256];
  int sock_errno;
  uword if_index;
  u32 hw_if_index, sw_if_index;

  //Feature negotiation
  u64 features;
  u64 feature_mask;
  u64 protocol_features;

  //Memory region information
  u32 nregions;
  vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS];
  void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
  u64 region_guest_addr_lo[VHOST_MEMORY_MAX_NREGIONS];
  u64 region_guest_addr_hi[VHOST_MEMORY_MAX_NREGIONS];
  u32 region_mmap_fd[VHOST_MEMORY_MAX_NREGIONS];

  //Virtual rings
  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;

  void *log_base_addr;
  u64 log_size;

  /* Whether to use spinlock or per_cpu_tx_qid assignment */
  u8 use_tx_spinlock;
  u16 *per_cpu_tx_qid;

  u8 enable_gso;

  /* Packed ring configured */
  u8 enable_packed;

  u8 enable_event_idx;
} vhost_user_intf_t;

typedef struct
{
  uword dst;
  uword src;
  u32 len;
} vhost_copy_t;

typedef struct
{
  u16 qid; /** The interface queue index (Not the virtio vring idx) */
  u16 device_index; /** The device index */
  u32 virtio_ring_flags; /** Runtime queue flags  **/
  u16 first_desc_len; /** Length of the first data descriptor **/
  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)

typedef struct
{
  u32 rx_buffers_len;
  u32 rx_buffers[VHOST_USER_RX_BUFFERS_N];

  virtio_net_hdr_mrg_rxbuf_t tx_headers[VLIB_FRAME_SIZE];
  vhost_copy_t copy[VHOST_USER_COPY_ARRAY_N];

  /* 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
{
  mhash_t if_index_by_sock_name;
  u32 mtu_bytes;
  vhost_user_intf_t *vhost_user_interfaces;
  u32 *show_dev_instance_by_real_dev_instance;
  u32 coalesce_frames;
  f64 coalesce_time;
  int dont_dump_vhost_user_memory;

  /** Per-CPU data for vhost-user */
  vhost_cpu_t *cpus;

  /** Pseudo random iterator */
  u32 random;

  /* The number of rx interface/queue pairs in interrupt mode */
  u32 ifq_count;

  /* logging */
  vlib_log_class_t log_default;

  /* gso interface count */
  u32 gso_count;
} vhost_user_main_t;

typedef struct
{
  u8 if_name[64];
  u32 sw_if_index;
  u32 virtio_net_hdr_sz;
  u64 features;
  u8 is_server;
  u8 sock_filename[256];
  u32 num_regions;
  int sock_errno;
} vhost_user_intf_details_t;

int vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
			 vhost_user_intf_details_t ** out_vuids);

extern vlib_node_registration_t vhost_user_send_interrupt_node;
extern vnet_device_class_t vhost_user_device_class;
extern vlib_node_registration_t vhost_user_input_node;
extern vhost_user_main_t vhost_user_main;

#endif

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */