summaryrefslogtreecommitdiff
path: root/usr/src/cmd/bhyve/pci_virtio_9p.c
blob: 2e169dc7cb9b27990d976a7c118009a816162d5b (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
393
394
395
396
397
398
399
/*-
 * Copyright (c) 2015 iXsystems Inc.
 * Copyright (c) 2017-2018 Jakub Klama <jceel@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer
 *    in this position and unchanged.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * VirtIO filesystem passthrough using 9p protocol.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/linker_set.h>
#include <sys/uio.h>
#ifndef WITHOUT_CAPSICUM
#include <sys/capsicum.h>
#endif

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <pthread.h>

#include <lib9p.h>
#include <backend/fs.h>

#include "bhyverun.h"
#include "config.h"
#include "debug.h"
#include "pci_emul.h"
#include "virtio.h"

#ifndef __FreeBSD__
#include "privileges.h"
#endif

#define	VT9P_MAX_IOV	128
#define VT9P_RINGSZ	256
#define	VT9P_MAXTAGSZ	256
#define	VT9P_CONFIGSPACESZ	(VT9P_MAXTAGSZ + sizeof(uint16_t))

static int pci_vt9p_debug;
#define DPRINTF(params) if (pci_vt9p_debug) printf params
#define WPRINTF(params) printf params

/*
 * Per-device softc
 */
struct pci_vt9p_softc {
	struct virtio_softc      vsc_vs;
	struct vqueue_info       vsc_vq;
	pthread_mutex_t          vsc_mtx;
	uint64_t                 vsc_cfg;
	uint64_t                 vsc_features;
	char *                   vsc_rootpath;
	struct pci_vt9p_config * vsc_config;
	struct l9p_backend *     vsc_fs_backend;
	struct l9p_server *      vsc_server;
        struct l9p_connection *  vsc_conn;
};

struct pci_vt9p_request {
	struct pci_vt9p_softc *	vsr_sc;
	struct iovec *		vsr_iov;
	size_t			vsr_niov;
	size_t			vsr_respidx;
	size_t			vsr_iolen;
	uint16_t		vsr_idx;
};

struct pci_vt9p_config {
	uint16_t tag_len;
	char tag[0];
} __attribute__((packed));

static int pci_vt9p_send(struct l9p_request *, const struct iovec *,
    const size_t, const size_t, void *);
static void pci_vt9p_drop(struct l9p_request *, const struct iovec *, size_t,
    void *);
static void pci_vt9p_reset(void *);
static void pci_vt9p_notify(void *, struct vqueue_info *);
static int pci_vt9p_cfgread(void *, int, int, uint32_t *);
static void pci_vt9p_neg_features(void *, uint64_t);

static struct virtio_consts vt9p_vi_consts = {
	"vt9p",			/* our name */
	1,			/* we support 1 virtqueue */
	VT9P_CONFIGSPACESZ,	/* config reg size */
	pci_vt9p_reset,		/* reset */
	pci_vt9p_notify,	/* device-wide qnotify */
	pci_vt9p_cfgread,	/* read virtio config */
	NULL,			/* write virtio config */
	pci_vt9p_neg_features,	/* apply negotiated features */
	(1 << 0),		/* our capabilities */
};


static void
pci_vt9p_reset(void *vsc)
{
	struct pci_vt9p_softc *sc;

	sc = vsc;

	DPRINTF(("vt9p: device reset requested !\n"));
	vi_reset_dev(&sc->vsc_vs);
}

static void
pci_vt9p_neg_features(void *vsc, uint64_t negotiated_features)
{
	struct pci_vt9p_softc *sc = vsc;

	sc->vsc_features = negotiated_features;
}

static int
pci_vt9p_cfgread(void *vsc, int offset, int size, uint32_t *retval)
{
	struct pci_vt9p_softc *sc = vsc;
	void *ptr;

	ptr = (uint8_t *)sc->vsc_config + offset;
	memcpy(retval, ptr, size);
	return (0);
}

static int
pci_vt9p_get_buffer(struct l9p_request *req, struct iovec *iov, size_t *niov,
    void *arg)
{
	struct pci_vt9p_request *preq = req->lr_aux;
	size_t n = preq->vsr_niov - preq->vsr_respidx;

	memcpy(iov, preq->vsr_iov + preq->vsr_respidx,
	    n * sizeof(struct iovec));
	*niov = n;
	return (0);
}

static int
pci_vt9p_send(struct l9p_request *req, const struct iovec *iov,
    const size_t niov, const size_t iolen, void *arg)
{
	struct pci_vt9p_request *preq = req->lr_aux;
	struct pci_vt9p_softc *sc = preq->vsr_sc;

	preq->vsr_iolen = iolen;

	pthread_mutex_lock(&sc->vsc_mtx);
	vq_relchain(&sc->vsc_vq, preq->vsr_idx, preq->vsr_iolen);
	vq_endchains(&sc->vsc_vq, 1);
	pthread_mutex_unlock(&sc->vsc_mtx);
	free(preq);
	return (0);
}

static void
pci_vt9p_drop(struct l9p_request *req, const struct iovec *iov, size_t niov,
    void *arg)
{
	struct pci_vt9p_request *preq = req->lr_aux;
	struct pci_vt9p_softc *sc = preq->vsr_sc;

	pthread_mutex_lock(&sc->vsc_mtx);
	vq_relchain(&sc->vsc_vq, preq->vsr_idx, 0);
	vq_endchains(&sc->vsc_vq, 1);
	pthread_mutex_unlock(&sc->vsc_mtx);
	free(preq);
}

static void
pci_vt9p_notify(void *vsc, struct vqueue_info *vq)
{
	struct iovec iov[VT9P_MAX_IOV];
	struct pci_vt9p_softc *sc;
	struct pci_vt9p_request *preq;
	struct vi_req req;
	int n;

	sc = vsc;

	while (vq_has_descs(vq)) {
		n = vq_getchain(vq, iov, VT9P_MAX_IOV, &req);
		assert(n >= 1 && n <= VT9P_MAX_IOV);
		preq = calloc(1, sizeof(struct pci_vt9p_request));
#ifndef __FreeBSD__
		if (preq == NULL) {
			EPRINTLN("virtio-9p: allocation failure: %s",
			    strerror(errno));
			break;
		}
#endif
		preq->vsr_sc = sc;
		preq->vsr_idx = req.idx;
		preq->vsr_iov = iov;
		preq->vsr_niov = n;
		preq->vsr_respidx = req.readable;

		for (int i = 0; i < n; i++) {
			DPRINTF(("vt9p: vt9p_notify(): desc%d base=%p, "
			    "len=%zu\r\n", i, iov[i].iov_base,
			    iov[i].iov_len));
		}

		l9p_connection_recv(sc->vsc_conn, iov, preq->vsr_respidx, preq);
	}
}

static int
pci_vt9p_legacy_config(nvlist_t *nvl, const char *opts)
{
	char *sharename = NULL, *tofree, *token, *tokens;

	if (opts == NULL)
		return (0);

	tokens = tofree = strdup(opts);
	while ((token = strsep(&tokens, ",")) != NULL) {
		if (strchr(token, '=') != NULL) {
			if (sharename != NULL) {
				EPRINTLN(
			    "virtio-9p: more than one share name given");
				return (-1);
			}

			sharename = strsep(&token, "=");
			set_config_value_node(nvl, "sharename", sharename);
			set_config_value_node(nvl, "path", token);
		} else
			set_config_bool_node(nvl, token, true);
	}
	free(tofree);

	return (0);
}

static int
pci_vt9p_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
{
	struct pci_vt9p_softc *sc;
	const char *value;
	const char *sharename;
	int rootfd;
	bool ro;
#ifndef WITHOUT_CAPSICUM
	cap_rights_t rootcap;
#endif

	ro = get_config_bool_node_default(nvl, "ro", false);

#ifndef __FreeBSD__
	illumos_priv_add_min(PRIV_FILE_DAC_READ, "vt9p");
	illumos_priv_add_min(PRIV_FILE_DAC_SEARCH, "vt9p");

	if (!ro) {
		illumos_priv_add_min(PRIV_FILE_CHOWN, "vt9p");
		illumos_priv_add_min(PRIV_FILE_CHOWN_SELF, "vt9p");
		illumos_priv_add_min(PRIV_FILE_WRITE, "vt9p");
		illumos_priv_add_min(PRIV_FILE_DAC_WRITE, "vt9p");
		illumos_priv_add_min(PRIV_FILE_OWNER, "vt9p");
		illumos_priv_add_min(PRIV_FILE_LINK_ANY, "vt9p");
	}
#endif

	value = get_config_value_node(nvl, "path");
	if (value == NULL) {
		EPRINTLN("virtio-9p: path required");
		return (1);
	}
	rootfd = open(value, O_DIRECTORY);
	if (rootfd < 0) {
		EPRINTLN("virtio-9p: failed to open '%s': %s", value,
		    strerror(errno));
		return (-1);
	}

	sharename = get_config_value_node(nvl, "sharename");
	if (sharename == NULL) {
		EPRINTLN("virtio-9p: share name required");
		return (1);
	}
	if (strlen(sharename) > VT9P_MAXTAGSZ) {
		EPRINTLN("virtio-9p: share name too long");
		return (1);
	}

	sc = calloc(1, sizeof(struct pci_vt9p_softc));
#ifndef __FreeBSD__
	if (sc == NULL) {
		EPRINTLN("virtio-9p: soft state allocation failure: %s",
		    strerror(errno));
		return (1);
	}
#endif
	sc->vsc_config = calloc(1, sizeof(struct pci_vt9p_config) +
	    VT9P_MAXTAGSZ);
#ifndef __FreeBSD__
	if (sc == NULL) {
		EPRINTLN("virtio-9p: vsc_config allocation failure: %s",
		    strerror(errno));
		return (1);
	}
#endif

	pthread_mutex_init(&sc->vsc_mtx, NULL);

#ifndef WITHOUT_CAPSICUM
	cap_rights_init(&rootcap,
	    CAP_LOOKUP, CAP_ACL_CHECK, CAP_ACL_DELETE, CAP_ACL_GET,
	    CAP_ACL_SET, CAP_READ, CAP_WRITE, CAP_SEEK, CAP_FSTAT,
	    CAP_CREATE, CAP_FCHMODAT, CAP_FCHOWNAT, CAP_FTRUNCATE,
	    CAP_LINKAT_SOURCE, CAP_LINKAT_TARGET, CAP_MKDIRAT, CAP_MKNODAT,
	    CAP_PREAD, CAP_PWRITE, CAP_RENAMEAT_SOURCE, CAP_RENAMEAT_TARGET,
	    CAP_SEEK, CAP_SYMLINKAT, CAP_UNLINKAT, CAP_EXTATTR_DELETE,
	    CAP_EXTATTR_GET, CAP_EXTATTR_LIST, CAP_EXTATTR_SET,
	    CAP_FUTIMES, CAP_FSTATFS, CAP_FSYNC, CAP_FPATHCONF);

	if (cap_rights_limit(rootfd, &rootcap) != 0)
		return (1);
#endif

	sc->vsc_config->tag_len = (uint16_t)strlen(sharename);
	memcpy(sc->vsc_config->tag, sharename, sc->vsc_config->tag_len);

	if (l9p_backend_fs_init(&sc->vsc_fs_backend, rootfd, ro) != 0) {
		errno = ENXIO;
		return (1);
	}

	if (l9p_server_init(&sc->vsc_server, sc->vsc_fs_backend) != 0) {
		errno = ENXIO;
		return (1);
	}

	if (l9p_connection_init(sc->vsc_server, &sc->vsc_conn) != 0) {
		errno = EIO;
		return (1);
	}

	sc->vsc_conn->lc_msize = L9P_MAX_IOV * PAGE_SIZE;
	sc->vsc_conn->lc_lt.lt_get_response_buffer = pci_vt9p_get_buffer;
	sc->vsc_conn->lc_lt.lt_send_response = pci_vt9p_send;
	sc->vsc_conn->lc_lt.lt_drop_response = pci_vt9p_drop;

	vi_softc_linkup(&sc->vsc_vs, &vt9p_vi_consts, sc, pi, &sc->vsc_vq);
	sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
	sc->vsc_vq.vq_qsize = VT9P_RINGSZ;

	/* initialize config space */
	pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_9P);
	pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
	pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_STORAGE);
	pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_ID_9P);
	pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);

	if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
		return (1);
	vi_set_io_bar(&sc->vsc_vs, 0);

	return (0);
}

static const struct pci_devemu pci_de_v9p = {
	.pe_emu =	"virtio-9p",
	.pe_legacy_config = pci_vt9p_legacy_config,
	.pe_init =	pci_vt9p_init,
	.pe_barwrite =	vi_pci_write,
	.pe_barread =	vi_pci_read
};
PCI_EMUL_SET(pci_de_v9p);