xref: /freebsd/usr.sbin/bhyve/pci_virtio_console.c (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
1 /*-
2  * Copyright (c) 2016 iXsystems Inc.
3  * All rights reserved.
4  *
5  * This software was developed by Jakub Klama <jceel@FreeBSD.org>
6  * under sponsorship from iXsystems Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer
13  *    in this position and unchanged.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/linker_set.h>
36 #include <sys/uio.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/un.h>
40 
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <stdbool.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <assert.h>
49 #include <pthread.h>
50 #include <libgen.h>
51 
52 #include "bhyverun.h"
53 #include "pci_emul.h"
54 #include "virtio.h"
55 #include "mevent.h"
56 #include "sockstream.h"
57 
58 #define	VTCON_RINGSZ	64
59 #define	VTCON_MAXPORTS	16
60 #define	VTCON_MAXQ	(VTCON_MAXPORTS * 2 + 2)
61 
62 #define	VTCON_DEVICE_READY	0
63 #define	VTCON_DEVICE_ADD	1
64 #define	VTCON_DEVICE_REMOVE	2
65 #define	VTCON_PORT_READY	3
66 #define	VTCON_CONSOLE_PORT	4
67 #define	VTCON_CONSOLE_RESIZE	5
68 #define	VTCON_PORT_OPEN		6
69 #define	VTCON_PORT_NAME		7
70 
71 #define	VTCON_F_SIZE		0
72 #define	VTCON_F_MULTIPORT	1
73 #define	VTCON_F_EMERG_WRITE	2
74 #define	VTCON_S_HOSTCAPS	\
75     (VTCON_F_SIZE | VTCON_F_MULTIPORT | VTCON_F_EMERG_WRITE)
76 
77 static int pci_vtcon_debug;
78 #define DPRINTF(params) if (pci_vtcon_debug) printf params
79 #define WPRINTF(params) printf params
80 
81 struct pci_vtcon_softc;
82 struct pci_vtcon_port;
83 struct pci_vtcon_config;
84 typedef void (pci_vtcon_cb_t)(struct pci_vtcon_port *, void *, struct iovec *,
85     int);
86 
87 struct pci_vtcon_port {
88 	struct pci_vtcon_softc * vsp_sc;
89 	int                      vsp_id;
90 	const char *             vsp_name;
91 	bool                     vsp_enabled;
92 	bool                     vsp_console;
93 	bool                     vsp_rx_ready;
94 	bool                     vsp_open;
95 	int                      vsp_rxq;
96 	int                      vsp_txq;
97 	void *                   vsp_arg;
98 	pci_vtcon_cb_t *         vsp_cb;
99 };
100 
101 struct pci_vtcon_sock
102 {
103 	struct pci_vtcon_port *  vss_port;
104 	const char *             vss_path;
105 	struct mevent *          vss_server_evp;
106 	struct mevent *          vss_conn_evp;
107 	int                      vss_server_fd;
108 	int                      vss_conn_fd;
109 	bool                     vss_open;
110 };
111 
112 struct pci_vtcon_softc {
113 	struct virtio_softc      vsc_vs;
114 	struct vqueue_info       vsc_queues[VTCON_MAXQ];
115 	pthread_mutex_t          vsc_mtx;
116 	uint64_t                 vsc_cfg;
117 	uint64_t                 vsc_features;
118 	char *                   vsc_rootdir;
119 	int                      vsc_kq;
120 	int                      vsc_nports;
121 	bool                     vsc_ready;
122 	struct pci_vtcon_port    vsc_control_port;
123  	struct pci_vtcon_port    vsc_ports[VTCON_MAXPORTS];
124 	struct pci_vtcon_config *vsc_config;
125 };
126 
127 struct pci_vtcon_config {
128 	uint16_t cols;
129 	uint16_t rows;
130 	uint32_t max_nr_ports;
131 	uint32_t emerg_wr;
132 } __attribute__((packed));
133 
134 struct pci_vtcon_control {
135 	uint32_t id;
136 	uint16_t event;
137 	uint16_t value;
138 } __attribute__((packed));
139 
140 struct pci_vtcon_console_resize {
141 	uint16_t cols;
142 	uint16_t rows;
143 } __attribute__((packed));
144 
145 static void pci_vtcon_reset(void *);
146 static void pci_vtcon_notify_rx(void *, struct vqueue_info *);
147 static void pci_vtcon_notify_tx(void *, struct vqueue_info *);
148 static int pci_vtcon_cfgread(void *, int, int, uint32_t *);
149 static int pci_vtcon_cfgwrite(void *, int, int, uint32_t);
150 static void pci_vtcon_neg_features(void *, uint64_t);
151 static void pci_vtcon_sock_accept(int, enum ev_type,  void *);
152 static void pci_vtcon_sock_rx(int, enum ev_type, void *);
153 static void pci_vtcon_sock_tx(struct pci_vtcon_port *, void *, struct iovec *,
154     int);
155 static void pci_vtcon_control_send(struct pci_vtcon_softc *,
156     struct pci_vtcon_control *, const void *, size_t);
157 static void pci_vtcon_announce_port(struct pci_vtcon_port *);
158 static void pci_vtcon_open_port(struct pci_vtcon_port *, bool);
159 
160 static struct virtio_consts vtcon_vi_consts = {
161 	"vtcon",		/* our name */
162 	VTCON_MAXQ,		/* we support VTCON_MAXQ virtqueues */
163 	sizeof(struct pci_vtcon_config), /* config reg size */
164 	pci_vtcon_reset,	/* reset */
165 	NULL,			/* device-wide qnotify */
166 	pci_vtcon_cfgread,	/* read virtio config */
167 	pci_vtcon_cfgwrite,	/* write virtio config */
168 	pci_vtcon_neg_features,	/* apply negotiated features */
169 	VTCON_S_HOSTCAPS,	/* our capabilities */
170 };
171 
172 
173 static void
174 pci_vtcon_reset(void *vsc)
175 {
176 	struct pci_vtcon_softc *sc;
177 
178 	sc = vsc;
179 
180 	DPRINTF(("vtcon: device reset requested!\n"));
181 	vi_reset_dev(&sc->vsc_vs);
182 }
183 
184 static void
185 pci_vtcon_neg_features(void *vsc, uint64_t negotiated_features)
186 {
187 	struct pci_vtcon_softc *sc = vsc;
188 
189 	sc->vsc_features = negotiated_features;
190 }
191 
192 static int
193 pci_vtcon_cfgread(void *vsc, int offset, int size, uint32_t *retval)
194 {
195 	struct pci_vtcon_softc *sc = vsc;
196 	void *ptr;
197 
198 	ptr = (uint8_t *)sc->vsc_config + offset;
199 	memcpy(retval, ptr, size);
200 	return (0);
201 }
202 
203 static int
204 pci_vtcon_cfgwrite(void *vsc, int offset, int size, uint32_t val)
205 {
206 
207 	return (0);
208 }
209 
210 static inline struct pci_vtcon_port *
211 pci_vtcon_vq_to_port(struct pci_vtcon_softc *sc, struct vqueue_info *vq)
212 {
213 	uint16_t num = vq->vq_num;
214 
215 	if (num == 0 || num == 1)
216 		return (&sc->vsc_ports[0]);
217 
218 	if (num == 2 || num == 3)
219 		return (&sc->vsc_control_port);
220 
221 	return (&sc->vsc_ports[(num / 2) - 1]);
222 }
223 
224 static inline struct vqueue_info *
225 pci_vtcon_port_to_vq(struct pci_vtcon_port *port, bool tx_queue)
226 {
227 	int qnum;
228 
229 	qnum = tx_queue ? port->vsp_txq : port->vsp_rxq;
230 	return (&port->vsp_sc->vsc_queues[qnum]);
231 }
232 
233 static struct pci_vtcon_port *
234 pci_vtcon_port_add(struct pci_vtcon_softc *sc, const char *name,
235     pci_vtcon_cb_t *cb, void *arg)
236 {
237 	struct pci_vtcon_port *port;
238 
239 	if (sc->vsc_nports == VTCON_MAXPORTS) {
240 		errno = EBUSY;
241 		return (NULL);
242 	}
243 
244 	port = &sc->vsc_ports[sc->vsc_nports++];
245 	port->vsp_id = sc->vsc_nports - 1;
246 	port->vsp_sc = sc;
247 	port->vsp_name = name;
248 	port->vsp_cb = cb;
249 	port->vsp_arg = arg;
250 
251 	if (port->vsp_id == 0) {
252 		/* port0 */
253 		port->vsp_txq = 0;
254 		port->vsp_rxq = 1;
255 	} else {
256 		port->vsp_txq = sc->vsc_nports * 2;
257 		port->vsp_rxq = port->vsp_txq + 1;
258 	}
259 
260 	port->vsp_enabled = true;
261 	return (port);
262 }
263 
264 static int
265 pci_vtcon_sock_add(struct pci_vtcon_softc *sc, const char *name,
266     const char *path)
267 {
268 	struct pci_vtcon_sock *sock;
269 	struct sockaddr_un sun;
270 	char *pathcopy;
271 	int s = -1, fd = -1, error = 0;
272 
273 	sock = calloc(1, sizeof(struct pci_vtcon_sock));
274 	if (sock == NULL) {
275 		error = -1;
276 		goto out;
277 	}
278 
279 	s = socket(AF_UNIX, SOCK_STREAM, 0);
280 	if (s < 0) {
281 		error = -1;
282 		goto out;
283 	}
284 
285 	pathcopy = strdup(path);
286 	if (pathcopy == NULL) {
287 		error = -1;
288 		goto out;
289 	}
290 
291 	fd = open(dirname(pathcopy), O_RDONLY | O_DIRECTORY);
292 	if (fd < 0) {
293 		free(pathcopy);
294 		error = -1;
295 		goto out;
296 	}
297 
298 	sun.sun_family = AF_UNIX;
299 	sun.sun_len = sizeof(struct sockaddr_un);
300 	strcpy(pathcopy, path);
301 	strncpy(sun.sun_path, basename(pathcopy), sizeof(sun.sun_path));
302 	free(pathcopy);
303 
304 	if (bindat(fd, s, (struct sockaddr *)&sun, sun.sun_len) < 0) {
305 		error = -1;
306 		goto out;
307 	}
308 
309 	if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
310 		error = -1;
311 		goto out;
312 	}
313 
314 	if (listen(s, 1) < 0) {
315 		error = -1;
316 		goto out;
317 	}
318 
319 
320 	sock->vss_port = pci_vtcon_port_add(sc, name, pci_vtcon_sock_tx, sock);
321 	if (sock->vss_port == NULL) {
322 		error = -1;
323 		goto out;
324 	}
325 
326 	sock->vss_open = false;
327 	sock->vss_conn_fd = -1;
328 	sock->vss_server_fd = s;
329 	sock->vss_server_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_accept,
330 	    sock);
331 
332 	if (sock->vss_server_evp == NULL) {
333 		error = -1;
334 		goto out;
335 	}
336 
337 out:
338 	if (fd != -1)
339 		close(fd);
340 
341 	if (error != 0 && s != -1)
342 		close(s);
343 
344 	return (error);
345 }
346 
347 static void
348 pci_vtcon_sock_accept(int fd __unused, enum ev_type t __unused, void *arg)
349 {
350 	struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg;
351 	int s;
352 
353 	s = accept(sock->vss_server_fd, NULL, NULL);
354 	if (s < 0)
355 		return;
356 
357 	if (sock->vss_open) {
358 		close(s);
359 		return;
360 	}
361 
362 	sock->vss_open = true;
363 	sock->vss_conn_fd = s;
364 	sock->vss_conn_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_rx, sock);
365 
366 	pci_vtcon_open_port(sock->vss_port, true);
367 }
368 
369 static void
370 pci_vtcon_sock_rx(int fd __unused, enum ev_type t __unused, void *arg)
371 {
372 	struct pci_vtcon_port *port;
373 	struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg;
374 	struct vqueue_info *vq;
375 	struct iovec iov;
376 	static char dummybuf[2048];
377 	int len, n;
378 	uint16_t idx;
379 
380 	port = sock->vss_port;
381 	vq = pci_vtcon_port_to_vq(port, true);
382 
383 	if (!sock->vss_open || !port->vsp_rx_ready) {
384 		len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf));
385 		if (len == 0)
386 			goto close;
387 
388 		return;
389 	}
390 
391 	if (!vq_has_descs(vq)) {
392 		len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf));
393 		vq_endchains(vq, 1);
394 		if (len == 0)
395 			goto close;
396 
397 		return;
398 	}
399 
400 	do {
401 		n = vq_getchain(vq, &idx, &iov, 1, NULL);
402 		len = readv(sock->vss_conn_fd, &iov, n);
403 
404 		if (len == 0 || (len < 0 && errno == EWOULDBLOCK)) {
405 			vq_retchain(vq);
406 			vq_endchains(vq, 0);
407 			if (len == 0)
408 				goto close;
409 
410 			return;
411 		}
412 
413 		vq_relchain(vq, idx, len);
414 	} while (vq_has_descs(vq));
415 
416 	vq_endchains(vq, 1);
417 
418 close:
419 	mevent_delete_close(sock->vss_conn_evp);
420 	sock->vss_conn_fd = -1;
421 	sock->vss_open = false;
422 }
423 
424 static void
425 pci_vtcon_sock_tx(struct pci_vtcon_port *port, void *arg, struct iovec *iov,
426     int niov)
427 {
428 	struct pci_vtcon_sock *sock;
429 	int i, ret;
430 
431 	sock = (struct pci_vtcon_sock *)arg;
432 
433 	if (sock->vss_conn_fd == -1)
434 		return;
435 
436 	for (i = 0; i < niov; i++) {
437 		ret = stream_write(sock->vss_conn_fd, iov[i].iov_base,
438 		    iov[i].iov_len);
439 		if (ret <= 0)
440 			break;
441 	}
442 
443 	if (ret <= 0) {
444 		mevent_delete_close(sock->vss_conn_evp);
445 		sock->vss_conn_fd = -1;
446 		sock->vss_open = false;
447 	}
448 }
449 
450 static void
451 pci_vtcon_control_tx(struct pci_vtcon_port *port, void *arg, struct iovec *iov,
452     int niov)
453 {
454 	struct pci_vtcon_softc *sc;
455 	struct pci_vtcon_port *tmp;
456 	struct pci_vtcon_control resp, *ctrl;
457 	int i;
458 
459 	assert(niov == 1);
460 
461 	sc = port->vsp_sc;
462 	ctrl = (struct pci_vtcon_control *)iov->iov_base;
463 
464 	switch (ctrl->event) {
465 	case VTCON_DEVICE_READY:
466 		sc->vsc_ready = true;
467 		/* set port ready events for registered ports */
468 		for (i = 0; i < VTCON_MAXPORTS; i++) {
469 			tmp = &sc->vsc_ports[i];
470 			if (tmp->vsp_enabled)
471 				pci_vtcon_announce_port(tmp);
472 
473 			if (tmp->vsp_open)
474 				pci_vtcon_open_port(tmp, true);
475 		}
476 		break;
477 
478 	case VTCON_PORT_READY:
479 		if (ctrl->id >= sc->vsc_nports) {
480 			WPRINTF(("VTCON_PORT_READY event for unknown port %d\n",
481 			    ctrl->id));
482 			return;
483 		}
484 
485 		tmp = &sc->vsc_ports[ctrl->id];
486 		if (tmp->vsp_console) {
487 			resp.event = VTCON_CONSOLE_PORT;
488 			resp.id = ctrl->id;
489 			resp.value = 1;
490 			pci_vtcon_control_send(sc, &resp, NULL, 0);
491 		}
492 		break;
493 	}
494 }
495 
496 static void
497 pci_vtcon_announce_port(struct pci_vtcon_port *port)
498 {
499 	struct pci_vtcon_control event;
500 
501 	event.id = port->vsp_id;
502 	event.event = VTCON_DEVICE_ADD;
503 	event.value = 1;
504 	pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0);
505 
506 	event.event = VTCON_PORT_NAME;
507 	pci_vtcon_control_send(port->vsp_sc, &event, port->vsp_name,
508 	    strlen(port->vsp_name));
509 }
510 
511 static void
512 pci_vtcon_open_port(struct pci_vtcon_port *port, bool open)
513 {
514 	struct pci_vtcon_control event;
515 
516 	if (!port->vsp_sc->vsc_ready) {
517 		port->vsp_open = true;
518 		return;
519 	}
520 
521 	event.id = port->vsp_id;
522 	event.event = VTCON_PORT_OPEN;
523 	event.value = (int)open;
524 	pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0);
525 }
526 
527 static void
528 pci_vtcon_control_send(struct pci_vtcon_softc *sc,
529     struct pci_vtcon_control *ctrl, const void *payload, size_t len)
530 {
531 	struct vqueue_info *vq;
532 	struct iovec iov;
533 	uint16_t idx;
534 	int n;
535 
536 	vq = pci_vtcon_port_to_vq(&sc->vsc_control_port, true);
537 
538 	if (!vq_has_descs(vq))
539 		return;
540 
541 	n = vq_getchain(vq, &idx, &iov, 1, NULL);
542 
543 	assert(n == 1);
544 
545 	memcpy(iov.iov_base, ctrl, sizeof(struct pci_vtcon_control));
546 	if (payload != NULL && len > 0)
547 		memcpy(iov.iov_base + sizeof(struct pci_vtcon_control),
548 		     payload, len);
549 
550 	vq_relchain(vq, idx, sizeof(struct pci_vtcon_control) + len);
551 	vq_endchains(vq, 1);
552 }
553 
554 
555 static void
556 pci_vtcon_notify_tx(void *vsc, struct vqueue_info *vq)
557 {
558 	struct pci_vtcon_softc *sc;
559 	struct pci_vtcon_port *port;
560 	struct iovec iov[1];
561 	uint16_t idx, n;
562 	uint16_t flags[8];
563 
564 	sc = vsc;
565 	port = pci_vtcon_vq_to_port(sc, vq);
566 
567 	while (vq_has_descs(vq)) {
568 		n = vq_getchain(vq, &idx, iov, 1, flags);
569 		if (port != NULL)
570 			port->vsp_cb(port, port->vsp_arg, iov, 1);
571 
572 		/*
573 		 * Release this chain and handle more
574 		 */
575 		vq_relchain(vq, idx, 0);
576 	}
577 	vq_endchains(vq, 1);	/* Generate interrupt if appropriate. */
578 }
579 
580 static void
581 pci_vtcon_notify_rx(void *vsc, struct vqueue_info *vq)
582 {
583 	struct pci_vtcon_softc *sc;
584 	struct pci_vtcon_port *port;
585 
586 	sc = vsc;
587 	port = pci_vtcon_vq_to_port(sc, vq);
588 
589 	if (!port->vsp_rx_ready) {
590 		port->vsp_rx_ready = 1;
591 		vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY;
592 	}
593 }
594 
595 static int
596 pci_vtcon_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
597 {
598 	struct pci_vtcon_softc *sc;
599 	char *portname = NULL;
600 	char *portpath = NULL;
601 	char *opt;
602 	int i;
603 
604 	sc = calloc(1, sizeof(struct pci_vtcon_softc));
605 	sc->vsc_config = calloc(1, sizeof(struct pci_vtcon_config));
606 	sc->vsc_config->max_nr_ports = VTCON_MAXPORTS;
607 	sc->vsc_config->cols = 80;
608 	sc->vsc_config->rows = 25;
609 
610 	vi_softc_linkup(&sc->vsc_vs, &vtcon_vi_consts, sc, pi, sc->vsc_queues);
611 	sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
612 
613 	for (i = 0; i < VTCON_MAXQ; i++) {
614 		sc->vsc_queues[i].vq_qsize = VTCON_RINGSZ;
615 		sc->vsc_queues[i].vq_notify = i % 2 == 0
616 		    ? pci_vtcon_notify_rx
617 		    : pci_vtcon_notify_tx;
618 	}
619 
620 	/* initialize config space */
621 	pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_CONSOLE);
622 	pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
623 	pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_SIMPLECOMM);
624 	pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_CONSOLE);
625 	pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
626 
627 	if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
628 		return (1);
629 	vi_set_io_bar(&sc->vsc_vs, 0);
630 
631 	/* create control port */
632 	sc->vsc_control_port.vsp_sc = sc;
633 	sc->vsc_control_port.vsp_txq = 2;
634 	sc->vsc_control_port.vsp_rxq = 3;
635 	sc->vsc_control_port.vsp_cb = pci_vtcon_control_tx;
636 	sc->vsc_control_port.vsp_enabled = true;
637 
638 	while ((opt = strsep(&opts, ",")) != NULL) {
639 		portname = strsep(&opt, "=");
640 		portpath = strdup(opt);
641 
642 		/* create port */
643 		if (pci_vtcon_sock_add(sc, portname, portpath) < 0) {
644 			fprintf(stderr, "cannot create port %s: %s\n",
645 			    portname, strerror(errno));
646 			return (1);
647 		}
648 	}
649 
650 	return (0);
651 }
652 
653 struct pci_devemu pci_de_vcon = {
654 	.pe_emu =	"virtio-console",
655 	.pe_init =	pci_vtcon_init,
656 	.pe_barwrite =	vi_pci_write,
657 	.pe_barread =	vi_pci_read
658 };
659 PCI_EMUL_SET(pci_de_vcon);
660