summaryrefslogtreecommitdiff
path: root/usr.sbin/vmd/virtio.h
blob: c293743050c128e12e9e0b28cdd8ab262f27ab12 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*	$OpenBSD: virtio.h,v 1.52 2024/07/10 09:27:33 dv Exp $	*/

/*
 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>

#include <dev/pv/virtioreg.h>
#include <net/if_tun.h>

#include <event.h>

#include "vmd.h"

#ifndef _VIRTIO_H_
#define _VIRTIO_H_

#define VIRTQUEUE_ALIGN(n)	(((n)+(VIRTIO_PAGE_SIZE-1))&    \
				    ~(VIRTIO_PAGE_SIZE-1))
#define ALIGNSZ(sz, align)	((sz + align - 1) & ~(align - 1))
#define MIN(a,b)		(((a)<(b))?(a):(b))

/* Queue sizes must be power of two and less than IOV_MAX (1024). */
#define VIORND_QUEUE_SIZE	64
#define VIORND_QUEUE_MASK	(VIORND_QUEUE_SIZE - 1)

#define VIOBLK_QUEUE_SIZE	128
#define VIOBLK_QUEUE_MASK	(VIOBLK_QUEUE_SIZE - 1)
#define VIOBLK_SEG_MAX		(VIOBLK_QUEUE_SIZE - 2)

#define VIOSCSI_QUEUE_SIZE	128
#define VIOSCSI_QUEUE_MASK	(VIOSCSI_QUEUE_SIZE - 1)

#define VIONET_QUEUE_SIZE	256
#define VIONET_QUEUE_MASK	(VIONET_QUEUE_SIZE - 1)

/* Virtio network device is backed by tap(4), so inherit limits */
#define VIONET_HARD_MTU		TUNMRU
#define VIONET_MIN_TXLEN	ETHER_HDR_LEN
#define VIONET_MAX_TXLEN	VIONET_HARD_MTU + ETHER_HDR_LEN

/* VMM Control Interface shutdown timeout (in seconds) */
#define VMMCI_TIMEOUT		3
#define VMMCI_SHUTDOWN_TIMEOUT	120

/* All the devices we support have either 1, 2 or 3 queues */
/* viornd - 1 queue
 * vioblk - 1 queue
 * vionet - 2 queues
 * vioscsi - 3 queues
 */
#define VIRTIO_MAX_QUEUES	3

#define MAXPHYS	(64 * 1024)	/* max raw I/O transfer size */

/*
 * Rename the address config register to be more descriptive.
 */
#define VIRTIO_CONFIG_QUEUE_PFN	VIRTIO_CONFIG_QUEUE_ADDRESS
#define DEVICE_NEEDS_RESET	VIRTIO_CONFIG_DEVICE_STATUS_DEVICE_NEEDS_RESET
#define DESC_WRITABLE(/* struct vring_desc */ x)	\
	(((x)->flags & VRING_DESC_F_WRITE) ? 1 : 0)


/*
 * VM <-> Device messaging.
 */
struct viodev_msg {
	uint8_t type;
#define VIODEV_MSG_INVALID	0
#define VIODEV_MSG_READY	1
#define VIODEV_MSG_ERROR	2
#define VIODEV_MSG_KICK		3
#define VIODEV_MSG_IO_READ	4
#define VIODEV_MSG_IO_WRITE	5
#define VIODEV_MSG_DUMP		6
#define VIODEV_MSG_SHUTDOWN	7

	uint16_t reg;		/* VirtIO register */
	uint8_t io_sz;		/* IO instruction size */
	uint8_t vcpu;		/* VCPU id */
	uint8_t irq;		/* IRQ number */

	int8_t state;		/* Interrupt state toggle (if any) */
#define INTR_STATE_ASSERT	 1
#define INTR_STATE_NOOP		 0
#define INTR_STATE_DEASSERT	-1

	uint32_t data;		/* Data (if any) */
	uint8_t data_valid;	/* 1 if data field is populated. */
} __packed;

/*
 * This struct stores notifications from a virtio driver. There is
 * one such struct per virtio device.
 */
struct virtio_io_cfg {
	uint32_t device_feature;
	uint32_t guest_feature;
	uint32_t queue_pfn;
	uint16_t queue_size;
	uint16_t queue_select;
	uint16_t queue_notify;
	uint8_t device_status;
	uint8_t isr_status;
};

struct virtio_backing {
	void  *p;
	ssize_t (*pread)(void *, char *, size_t, off_t);
	ssize_t (*preadv)(void *, struct iovec *, int, off_t);
	ssize_t (*pwrite)(void *, char *, size_t, off_t);
	ssize_t (*pwritev)(void *, struct iovec *, int, off_t);
	void (*close)(void *, int);
};

/*
 * A virtio device can have several virtqs. For example, vionet has one virtq
 * each for transmitting and receiving packets. This struct describes the state
 * of one virtq, such as their address in memory, size, offsets of rings, etc.
 * There is one virtio_vq_info per virtq.
 */
struct virtio_vq_info {
	/* Guest physical address of virtq */
	uint64_t q_gpa;

	/* Host virtual address of virtq */
	void *q_hva;

	/* Queue size: number of queue entries in virtq */
	uint32_t qs;

	/*
	 * The offset of the 'available' ring within the virtq located at
	 * guest physical address qa above
	 */
	uint32_t vq_availoffset;

	/*
	 * The offset of the 'used' ring within the virtq located at guest
	 * physical address qa above
	 */
	uint32_t vq_usedoffset;

	/*
	 * The index into a slot of the 'available' ring that a virtio device
	 * can consume next
	 */
	uint16_t last_avail;

	/*
	 * The most recent index into the 'available' ring that a virtio
	 * driver notified to the host.
	 */
	uint16_t notified_avail;
};

/*
 * Each virtio driver has a notifyq method where one or more messages
 * are ready to be processed on a given virtq.  As such, various
 * pieces of information are needed to provide ring accounting while
 * processing a given message such as virtq indexes, vring pointers, and
 * vring descriptors.
 */
struct virtio_vq_acct {

	/* index of previous avail vring message */
	uint16_t idx;

	/* index of current message containing the request */
	uint16_t req_idx;

	/* index of current message containing the response */
	uint16_t resp_idx;

	/* vring descriptor pointer */
	struct vring_desc *desc;

	/* vring descriptor pointer for request header and data */
	struct vring_desc *req_desc;

	/* vring descriptor pointer for response header and data */
	struct vring_desc *resp_desc;

	/* pointer to the available vring */
	struct vring_avail *avail;

	/* pointer to the used vring */
	struct vring_used *used;
};

struct viornd_dev {
	struct virtio_io_cfg cfg;

	struct virtio_vq_info vq[VIRTIO_MAX_QUEUES];

	uint8_t pci_id;
	int irq;
	uint32_t vm_id;
};

struct vioblk_dev {
	struct virtio_io_cfg cfg;
	struct virtio_vq_info vq[VIRTIO_MAX_QUEUES];
	struct virtio_backing file;

	int disk_fd[VM_MAX_BASE_PER_DISK];	/* fds for disk image(s) */
	uint8_t ndisk_fd;	/* number of valid disk fds */
	uint64_t capacity;	/* size in 512 byte sectors */
	uint32_t seg_max;	/* maximum number of segments */

	unsigned int idx;
};

/* vioscsi will use at least 3 queues - 5.6.2 Virtqueues
 * Current implementation will use 3
 * 0 - control
 * 1 - event
 * 2 - requests
 */
struct vioscsi_dev {
	struct virtio_io_cfg cfg;

	struct virtio_vq_info vq[VIRTIO_MAX_QUEUES];

	struct virtio_backing file;

	/* is the device locked */
	int locked;
	/* size of iso file in bytes */
	uint64_t sz;
	/* last block address read */
	uint64_t lba;
	/* number of blocks represented in iso */
	uint64_t n_blocks;
	uint32_t max_xfer;

	uint8_t pci_id;
	uint32_t vm_id;
	int irq;
};

struct vionet_dev {
	struct virtio_io_cfg cfg;
	struct virtio_vq_info vq[VIRTIO_MAX_QUEUES];

	int data_fd;		/* fd for our tap device */

	uint8_t mac[6];
	uint8_t hostmac[6];
	int lockedmac;
	int local;
	int pxeboot;
	struct local_prefix local_prefix;

	unsigned int idx;
};

struct virtio_dev {
	union {
		struct vioblk_dev vioblk;
		struct vionet_dev vionet;
	};

	struct imsgev async_iev;
	struct imsgev sync_iev;

	int sync_fd;		/* fd for synchronous channel */
	int async_fd;		/* fd for async channel */

	uint8_t pci_id;
	uint32_t vm_id;
	uint32_t vm_vmid;
	int irq;

	pid_t dev_pid;
	char dev_type;
	SLIST_ENTRY(virtio_dev) dev_next;
};

struct virtio_net_hdr {
	uint8_t flags;
	uint8_t gso_type;
	uint16_t hdr_len;
	uint16_t gso_size;
	uint16_t csum_start;
	uint16_t csum_offset;

	/*
	 * num_buffers is only used if VIRTIO_NET_F_MRG_RXBUF is negotiated.
	 * vmd(8) doesn't negotiate that, but the field is listed here
	 * for completeness sake.
	 */
/*	uint16_t num_buffers; */
};

enum vmmci_cmd {
	VMMCI_NONE = 0,
	VMMCI_SHUTDOWN,
	VMMCI_REBOOT,
	VMMCI_SYNCRTC,
};

struct vmmci_dev {
	struct virtio_io_cfg cfg;
	struct event timeout;
	struct timeval time;
	enum vmmci_cmd cmd;
	uint32_t vm_id;
	int irq;

	uint8_t pci_id;
};

/* XXX to be removed once vioscsi is adapted to vectorized io. */
struct ioinfo {
	uint8_t *buf;
	ssize_t len;
	off_t offset;
};

/* virtio.c */
void virtio_init(struct vmd_vm *, int, int[][VM_MAX_BASE_PER_DISK], int *);
void virtio_broadcast_imsg(struct vmd_vm *, uint16_t, void *, uint16_t);
void virtio_stop(struct vmd_vm *);
void virtio_start(struct vmd_vm *);
void virtio_shutdown(struct vmd_vm *);
int virtio_dump(int);
int virtio_restore(int, struct vmd_vm *, int, int[][VM_MAX_BASE_PER_DISK],
    int *);
const char *virtio_reg_name(uint8_t);
uint32_t vring_size(uint32_t);
int vm_device_pipe(struct virtio_dev *, void (*)(int, short, void *),
    struct event_base *);
int virtio_pci_io(int, uint16_t, uint32_t *, uint8_t *, void *, uint8_t);
void virtio_assert_irq(struct virtio_dev *, int);
void virtio_deassert_irq(struct virtio_dev *, int);

int virtio_rnd_io(int, uint16_t, uint32_t *, uint8_t *, void *, uint8_t);
int viornd_dump(int);
int viornd_restore(int, struct vmd_vm *);
void viornd_update_qs(void);
void viornd_update_qa(void);
int viornd_notifyq(void);

ssize_t virtio_qcow2_get_base(int, char *, size_t, const char *);
int virtio_qcow2_create(const char *, const char *, uint64_t);
int virtio_qcow2_init(struct virtio_backing *, off_t *, int*, size_t);
int virtio_raw_create(const char *, uint64_t);
int virtio_raw_init(struct virtio_backing *, off_t *, int*, size_t);

int vioblk_dump(int);
int vioblk_restore(int, struct vmd_vm *, int[][VM_MAX_BASE_PER_DISK]);

int vionet_dump(int);
int vionet_restore(int, struct vmd_vm *, int *);
void vionet_set_hostmac(struct vmd_vm *, unsigned int, uint8_t *);

int vmmci_io(int, uint16_t, uint32_t *, uint8_t *, void *, uint8_t);
int vmmci_dump(int);
int vmmci_restore(int, uint32_t);
int vmmci_ctl(unsigned int);
void vmmci_ack(unsigned int);
void vmmci_timeout(int, short, void *);

const char *vioblk_cmd_name(uint32_t);
int vioscsi_dump(int);
int vioscsi_restore(int, struct vmd_vm *, int);

/* dhcp.c */
ssize_t dhcp_request(struct virtio_dev *, char *, size_t, char **);

/* vioscsi.c */
int vioscsi_io(int, uint16_t, uint32_t *, uint8_t *, void *, uint8_t);
void vioscsi_update_qs(struct vioscsi_dev *);
void vioscsi_update_qa(struct vioscsi_dev *);
int vioscsi_notifyq(struct vioscsi_dev *);

#endif /* _VIRTIO_H_ */