2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2016 iXsystems Inc.
7 * This software was developed by Jakub Klama <jceel@FreeBSD.org>
8 * under sponsorship from iXsystems Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer
15 * in this position and unchanged.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #ifndef WITHOUT_CAPSICUM
38 #include <sys/capsicum.h>
40 #include <sys/linker_set.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
46 #ifndef WITHOUT_CAPSICUM
47 #include <capsicum_helpers.h>
66 #include "sockstream.h"
68 #define VTCON_RINGSZ 64
69 #define VTCON_MAXPORTS 16
70 #define VTCON_MAXQ (VTCON_MAXPORTS * 2 + 2)
72 #define VTCON_DEVICE_READY 0
73 #define VTCON_DEVICE_ADD 1
74 #define VTCON_DEVICE_REMOVE 2
75 #define VTCON_PORT_READY 3
76 #define VTCON_CONSOLE_PORT 4
77 #define VTCON_CONSOLE_RESIZE 5
78 #define VTCON_PORT_OPEN 6
79 #define VTCON_PORT_NAME 7
81 #define VTCON_F_SIZE 0
82 #define VTCON_F_MULTIPORT 1
83 #define VTCON_F_EMERG_WRITE 2
84 #define VTCON_S_HOSTCAPS \
85 (VTCON_F_SIZE | VTCON_F_MULTIPORT | VTCON_F_EMERG_WRITE)
87 static int pci_vtcon_debug;
88 #define DPRINTF(params) if (pci_vtcon_debug) printf params
89 #define WPRINTF(params) printf params
91 struct pci_vtcon_softc;
92 struct pci_vtcon_port;
93 struct pci_vtcon_config;
94 typedef void (pci_vtcon_cb_t)(struct pci_vtcon_port *, void *, struct iovec *,
97 struct pci_vtcon_port {
98 struct pci_vtcon_softc * vsp_sc;
100 const char * vsp_name;
108 pci_vtcon_cb_t * vsp_cb;
111 struct pci_vtcon_sock
113 struct pci_vtcon_port * vss_port;
114 const char * vss_path;
115 struct mevent * vss_server_evp;
116 struct mevent * vss_conn_evp;
122 struct pci_vtcon_softc {
123 struct virtio_softc vsc_vs;
124 struct vqueue_info vsc_queues[VTCON_MAXQ];
125 pthread_mutex_t vsc_mtx;
127 uint64_t vsc_features;
132 struct pci_vtcon_port vsc_control_port;
133 struct pci_vtcon_port vsc_ports[VTCON_MAXPORTS];
134 struct pci_vtcon_config *vsc_config;
137 struct pci_vtcon_config {
140 uint32_t max_nr_ports;
142 } __attribute__((packed));
144 struct pci_vtcon_control {
148 } __attribute__((packed));
150 struct pci_vtcon_console_resize {
153 } __attribute__((packed));
155 static void pci_vtcon_reset(void *);
156 static void pci_vtcon_notify_rx(void *, struct vqueue_info *);
157 static void pci_vtcon_notify_tx(void *, struct vqueue_info *);
158 static int pci_vtcon_cfgread(void *, int, int, uint32_t *);
159 static int pci_vtcon_cfgwrite(void *, int, int, uint32_t);
160 static void pci_vtcon_neg_features(void *, uint64_t);
161 static void pci_vtcon_sock_accept(int, enum ev_type, void *);
162 static void pci_vtcon_sock_rx(int, enum ev_type, void *);
163 static void pci_vtcon_sock_tx(struct pci_vtcon_port *, void *, struct iovec *,
165 static void pci_vtcon_control_send(struct pci_vtcon_softc *,
166 struct pci_vtcon_control *, const void *, size_t);
167 static void pci_vtcon_announce_port(struct pci_vtcon_port *);
168 static void pci_vtcon_open_port(struct pci_vtcon_port *, bool);
170 static struct virtio_consts vtcon_vi_consts = {
171 "vtcon", /* our name */
172 VTCON_MAXQ, /* we support VTCON_MAXQ virtqueues */
173 sizeof(struct pci_vtcon_config), /* config reg size */
174 pci_vtcon_reset, /* reset */
175 NULL, /* device-wide qnotify */
176 pci_vtcon_cfgread, /* read virtio config */
177 pci_vtcon_cfgwrite, /* write virtio config */
178 pci_vtcon_neg_features, /* apply negotiated features */
179 VTCON_S_HOSTCAPS, /* our capabilities */
184 pci_vtcon_reset(void *vsc)
186 struct pci_vtcon_softc *sc;
190 DPRINTF(("vtcon: device reset requested!\n"));
191 vi_reset_dev(&sc->vsc_vs);
195 pci_vtcon_neg_features(void *vsc, uint64_t negotiated_features)
197 struct pci_vtcon_softc *sc = vsc;
199 sc->vsc_features = negotiated_features;
203 pci_vtcon_cfgread(void *vsc, int offset, int size, uint32_t *retval)
205 struct pci_vtcon_softc *sc = vsc;
208 ptr = (uint8_t *)sc->vsc_config + offset;
209 memcpy(retval, ptr, size);
214 pci_vtcon_cfgwrite(void *vsc, int offset, int size, uint32_t val)
220 static inline struct pci_vtcon_port *
221 pci_vtcon_vq_to_port(struct pci_vtcon_softc *sc, struct vqueue_info *vq)
223 uint16_t num = vq->vq_num;
225 if (num == 0 || num == 1)
226 return (&sc->vsc_ports[0]);
228 if (num == 2 || num == 3)
229 return (&sc->vsc_control_port);
231 return (&sc->vsc_ports[(num / 2) - 1]);
234 static inline struct vqueue_info *
235 pci_vtcon_port_to_vq(struct pci_vtcon_port *port, bool tx_queue)
239 qnum = tx_queue ? port->vsp_txq : port->vsp_rxq;
240 return (&port->vsp_sc->vsc_queues[qnum]);
243 static struct pci_vtcon_port *
244 pci_vtcon_port_add(struct pci_vtcon_softc *sc, const char *name,
245 pci_vtcon_cb_t *cb, void *arg)
247 struct pci_vtcon_port *port;
249 if (sc->vsc_nports == VTCON_MAXPORTS) {
254 port = &sc->vsc_ports[sc->vsc_nports++];
255 port->vsp_id = sc->vsc_nports - 1;
257 port->vsp_name = name;
261 if (port->vsp_id == 0) {
266 port->vsp_txq = sc->vsc_nports * 2;
267 port->vsp_rxq = port->vsp_txq + 1;
270 port->vsp_enabled = true;
275 pci_vtcon_sock_add(struct pci_vtcon_softc *sc, const char *name,
278 struct pci_vtcon_sock *sock;
279 struct sockaddr_un sun;
281 int s = -1, fd = -1, error = 0;
282 #ifndef WITHOUT_CAPSICUM
286 sock = calloc(1, sizeof(struct pci_vtcon_sock));
292 s = socket(AF_UNIX, SOCK_STREAM, 0);
298 pathcopy = strdup(path);
299 if (pathcopy == NULL) {
304 fd = open(dirname(pathcopy), O_RDONLY | O_DIRECTORY);
311 sun.sun_family = AF_UNIX;
312 sun.sun_len = sizeof(struct sockaddr_un);
313 strcpy(pathcopy, path);
314 strlcpy(sun.sun_path, basename(pathcopy), sizeof(sun.sun_path));
317 if (bindat(fd, s, (struct sockaddr *)&sun, sun.sun_len) < 0) {
322 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
327 if (listen(s, 1) < 0) {
332 #ifndef WITHOUT_CAPSICUM
333 cap_rights_init(&rights, CAP_ACCEPT, CAP_EVENT, CAP_READ, CAP_WRITE);
334 if (caph_rights_limit(s, &rights) == -1)
335 errx(EX_OSERR, "Unable to apply rights for sandbox");
338 sock->vss_port = pci_vtcon_port_add(sc, name, pci_vtcon_sock_tx, sock);
339 if (sock->vss_port == NULL) {
344 sock->vss_open = false;
345 sock->vss_conn_fd = -1;
346 sock->vss_server_fd = s;
347 sock->vss_server_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_accept,
350 if (sock->vss_server_evp == NULL) {
359 if (error != 0 && s != -1)
366 pci_vtcon_sock_accept(int fd __unused, enum ev_type t __unused, void *arg)
368 struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg;
371 s = accept(sock->vss_server_fd, NULL, NULL);
375 if (sock->vss_open) {
380 sock->vss_open = true;
381 sock->vss_conn_fd = s;
382 sock->vss_conn_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_rx, sock);
384 pci_vtcon_open_port(sock->vss_port, true);
388 pci_vtcon_sock_rx(int fd __unused, enum ev_type t __unused, void *arg)
390 struct pci_vtcon_port *port;
391 struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg;
392 struct vqueue_info *vq;
394 static char dummybuf[2048];
398 port = sock->vss_port;
399 vq = pci_vtcon_port_to_vq(port, true);
401 if (!sock->vss_open || !port->vsp_rx_ready) {
402 len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf));
409 if (!vq_has_descs(vq)) {
410 len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf));
419 n = vq_getchain(vq, &idx, &iov, 1, NULL);
420 len = readv(sock->vss_conn_fd, &iov, n);
422 if (len == 0 || (len < 0 && errno == EWOULDBLOCK)) {
431 vq_relchain(vq, idx, len);
432 } while (vq_has_descs(vq));
437 mevent_delete_close(sock->vss_conn_evp);
438 sock->vss_conn_fd = -1;
439 sock->vss_open = false;
443 pci_vtcon_sock_tx(struct pci_vtcon_port *port, void *arg, struct iovec *iov,
446 struct pci_vtcon_sock *sock;
449 sock = (struct pci_vtcon_sock *)arg;
451 if (sock->vss_conn_fd == -1)
454 for (i = 0; i < niov; i++) {
455 ret = stream_write(sock->vss_conn_fd, iov[i].iov_base,
462 mevent_delete_close(sock->vss_conn_evp);
463 sock->vss_conn_fd = -1;
464 sock->vss_open = false;
469 pci_vtcon_control_tx(struct pci_vtcon_port *port, void *arg, struct iovec *iov,
472 struct pci_vtcon_softc *sc;
473 struct pci_vtcon_port *tmp;
474 struct pci_vtcon_control resp, *ctrl;
480 ctrl = (struct pci_vtcon_control *)iov->iov_base;
482 switch (ctrl->event) {
483 case VTCON_DEVICE_READY:
484 sc->vsc_ready = true;
485 /* set port ready events for registered ports */
486 for (i = 0; i < VTCON_MAXPORTS; i++) {
487 tmp = &sc->vsc_ports[i];
488 if (tmp->vsp_enabled)
489 pci_vtcon_announce_port(tmp);
492 pci_vtcon_open_port(tmp, true);
496 case VTCON_PORT_READY:
497 if (ctrl->id >= sc->vsc_nports) {
498 WPRINTF(("VTCON_PORT_READY event for unknown port %d\n",
503 tmp = &sc->vsc_ports[ctrl->id];
504 if (tmp->vsp_console) {
505 resp.event = VTCON_CONSOLE_PORT;
508 pci_vtcon_control_send(sc, &resp, NULL, 0);
515 pci_vtcon_announce_port(struct pci_vtcon_port *port)
517 struct pci_vtcon_control event;
519 event.id = port->vsp_id;
520 event.event = VTCON_DEVICE_ADD;
522 pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0);
524 event.event = VTCON_PORT_NAME;
525 pci_vtcon_control_send(port->vsp_sc, &event, port->vsp_name,
526 strlen(port->vsp_name));
530 pci_vtcon_open_port(struct pci_vtcon_port *port, bool open)
532 struct pci_vtcon_control event;
534 if (!port->vsp_sc->vsc_ready) {
535 port->vsp_open = true;
539 event.id = port->vsp_id;
540 event.event = VTCON_PORT_OPEN;
541 event.value = (int)open;
542 pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0);
546 pci_vtcon_control_send(struct pci_vtcon_softc *sc,
547 struct pci_vtcon_control *ctrl, const void *payload, size_t len)
549 struct vqueue_info *vq;
554 vq = pci_vtcon_port_to_vq(&sc->vsc_control_port, true);
556 if (!vq_has_descs(vq))
559 n = vq_getchain(vq, &idx, &iov, 1, NULL);
563 memcpy(iov.iov_base, ctrl, sizeof(struct pci_vtcon_control));
564 if (payload != NULL && len > 0)
565 memcpy(iov.iov_base + sizeof(struct pci_vtcon_control),
568 vq_relchain(vq, idx, sizeof(struct pci_vtcon_control) + len);
574 pci_vtcon_notify_tx(void *vsc, struct vqueue_info *vq)
576 struct pci_vtcon_softc *sc;
577 struct pci_vtcon_port *port;
583 port = pci_vtcon_vq_to_port(sc, vq);
585 while (vq_has_descs(vq)) {
586 n = vq_getchain(vq, &idx, iov, 1, flags);
589 port->vsp_cb(port, port->vsp_arg, iov, 1);
592 * Release this chain and handle more
594 vq_relchain(vq, idx, 0);
596 vq_endchains(vq, 1); /* Generate interrupt if appropriate. */
600 pci_vtcon_notify_rx(void *vsc, struct vqueue_info *vq)
602 struct pci_vtcon_softc *sc;
603 struct pci_vtcon_port *port;
606 port = pci_vtcon_vq_to_port(sc, vq);
608 if (!port->vsp_rx_ready) {
609 port->vsp_rx_ready = 1;
610 vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY;
615 pci_vtcon_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
617 struct pci_vtcon_softc *sc;
618 char *portname = NULL;
619 char *portpath = NULL;
623 sc = calloc(1, sizeof(struct pci_vtcon_softc));
624 sc->vsc_config = calloc(1, sizeof(struct pci_vtcon_config));
625 sc->vsc_config->max_nr_ports = VTCON_MAXPORTS;
626 sc->vsc_config->cols = 80;
627 sc->vsc_config->rows = 25;
629 vi_softc_linkup(&sc->vsc_vs, &vtcon_vi_consts, sc, pi, sc->vsc_queues);
630 sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
632 for (i = 0; i < VTCON_MAXQ; i++) {
633 sc->vsc_queues[i].vq_qsize = VTCON_RINGSZ;
634 sc->vsc_queues[i].vq_notify = i % 2 == 0
635 ? pci_vtcon_notify_rx
636 : pci_vtcon_notify_tx;
639 /* initialize config space */
640 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_CONSOLE);
641 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
642 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_SIMPLECOMM);
643 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_CONSOLE);
644 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
646 if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
648 vi_set_io_bar(&sc->vsc_vs, 0);
650 /* create control port */
651 sc->vsc_control_port.vsp_sc = sc;
652 sc->vsc_control_port.vsp_txq = 2;
653 sc->vsc_control_port.vsp_rxq = 3;
654 sc->vsc_control_port.vsp_cb = pci_vtcon_control_tx;
655 sc->vsc_control_port.vsp_enabled = true;
657 while ((opt = strsep(&opts, ",")) != NULL) {
658 portname = strsep(&opt, "=");
662 if (pci_vtcon_sock_add(sc, portname, portpath) < 0) {
663 fprintf(stderr, "cannot create port %s: %s\n",
664 portname, strerror(errno));
672 struct pci_devemu pci_de_vcon = {
673 .pe_emu = "virtio-console",
674 .pe_init = pci_vtcon_init,
675 .pe_barwrite = vi_pci_write,
676 .pe_barread = vi_pci_read
678 PCI_EMUL_SET(pci_de_vcon);