2016-09-17 13:48:01 +00:00
|
|
|
/*-
|
2018-06-13 03:22:08 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2016-09-17 13:48:01 +00:00
|
|
|
* Copyright (c) 2016 iXsystems Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by Jakub Klama <jceel@FreeBSD.org>
|
|
|
|
* under sponsorship from iXsystems Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2017-02-14 13:35:59 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
#include <sys/capsicum.h>
|
|
|
|
#endif
|
2016-09-17 13:48:01 +00:00
|
|
|
#include <sys/linker_set.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
2019-01-16 00:39:23 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
#include <capsicum_helpers.h>
|
|
|
|
#endif
|
2017-02-14 13:35:59 +00:00
|
|
|
#include <err.h>
|
2016-09-17 13:48:01 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <libgen.h>
|
2017-02-14 13:35:59 +00:00
|
|
|
#include <sysexits.h>
|
2016-09-17 13:48:01 +00:00
|
|
|
|
|
|
|
#include "bhyverun.h"
|
2019-06-26 20:30:41 +00:00
|
|
|
#include "config.h"
|
2020-01-08 22:55:22 +00:00
|
|
|
#include "debug.h"
|
2016-09-17 13:48:01 +00:00
|
|
|
#include "pci_emul.h"
|
|
|
|
#include "virtio.h"
|
|
|
|
#include "mevent.h"
|
2016-11-24 22:16:18 +00:00
|
|
|
#include "sockstream.h"
|
2016-09-17 13:48:01 +00:00
|
|
|
|
|
|
|
#define VTCON_RINGSZ 64
|
|
|
|
#define VTCON_MAXPORTS 16
|
|
|
|
#define VTCON_MAXQ (VTCON_MAXPORTS * 2 + 2)
|
|
|
|
|
|
|
|
#define VTCON_DEVICE_READY 0
|
|
|
|
#define VTCON_DEVICE_ADD 1
|
|
|
|
#define VTCON_DEVICE_REMOVE 2
|
|
|
|
#define VTCON_PORT_READY 3
|
|
|
|
#define VTCON_CONSOLE_PORT 4
|
|
|
|
#define VTCON_CONSOLE_RESIZE 5
|
|
|
|
#define VTCON_PORT_OPEN 6
|
|
|
|
#define VTCON_PORT_NAME 7
|
|
|
|
|
|
|
|
#define VTCON_F_SIZE 0
|
|
|
|
#define VTCON_F_MULTIPORT 1
|
|
|
|
#define VTCON_F_EMERG_WRITE 2
|
|
|
|
#define VTCON_S_HOSTCAPS \
|
|
|
|
(VTCON_F_SIZE | VTCON_F_MULTIPORT | VTCON_F_EMERG_WRITE)
|
|
|
|
|
|
|
|
static int pci_vtcon_debug;
|
2020-01-08 22:55:22 +00:00
|
|
|
#define DPRINTF(params) if (pci_vtcon_debug) PRINTLN params
|
|
|
|
#define WPRINTF(params) PRINTLN params
|
2016-09-17 13:48:01 +00:00
|
|
|
|
|
|
|
struct pci_vtcon_softc;
|
|
|
|
struct pci_vtcon_port;
|
|
|
|
struct pci_vtcon_config;
|
|
|
|
typedef void (pci_vtcon_cb_t)(struct pci_vtcon_port *, void *, struct iovec *,
|
|
|
|
int);
|
|
|
|
|
|
|
|
struct pci_vtcon_port {
|
|
|
|
struct pci_vtcon_softc * vsp_sc;
|
|
|
|
int vsp_id;
|
|
|
|
const char * vsp_name;
|
|
|
|
bool vsp_enabled;
|
|
|
|
bool vsp_console;
|
|
|
|
bool vsp_rx_ready;
|
2016-11-24 21:53:42 +00:00
|
|
|
bool vsp_open;
|
2016-09-17 13:48:01 +00:00
|
|
|
int vsp_rxq;
|
|
|
|
int vsp_txq;
|
|
|
|
void * vsp_arg;
|
|
|
|
pci_vtcon_cb_t * vsp_cb;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pci_vtcon_sock
|
|
|
|
{
|
|
|
|
struct pci_vtcon_port * vss_port;
|
|
|
|
const char * vss_path;
|
|
|
|
struct mevent * vss_server_evp;
|
|
|
|
struct mevent * vss_conn_evp;
|
|
|
|
int vss_server_fd;
|
|
|
|
int vss_conn_fd;
|
|
|
|
bool vss_open;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pci_vtcon_softc {
|
|
|
|
struct virtio_softc vsc_vs;
|
|
|
|
struct vqueue_info vsc_queues[VTCON_MAXQ];
|
|
|
|
pthread_mutex_t vsc_mtx;
|
|
|
|
uint64_t vsc_cfg;
|
|
|
|
uint64_t vsc_features;
|
|
|
|
char * vsc_rootdir;
|
|
|
|
int vsc_kq;
|
2016-11-24 21:53:42 +00:00
|
|
|
bool vsc_ready;
|
2016-09-17 13:48:01 +00:00
|
|
|
struct pci_vtcon_port vsc_control_port;
|
|
|
|
struct pci_vtcon_port vsc_ports[VTCON_MAXPORTS];
|
|
|
|
struct pci_vtcon_config *vsc_config;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pci_vtcon_config {
|
|
|
|
uint16_t cols;
|
|
|
|
uint16_t rows;
|
|
|
|
uint32_t max_nr_ports;
|
|
|
|
uint32_t emerg_wr;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
struct pci_vtcon_control {
|
|
|
|
uint32_t id;
|
|
|
|
uint16_t event;
|
|
|
|
uint16_t value;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
struct pci_vtcon_console_resize {
|
|
|
|
uint16_t cols;
|
|
|
|
uint16_t rows;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
static void pci_vtcon_reset(void *);
|
|
|
|
static void pci_vtcon_notify_rx(void *, struct vqueue_info *);
|
|
|
|
static void pci_vtcon_notify_tx(void *, struct vqueue_info *);
|
|
|
|
static int pci_vtcon_cfgread(void *, int, int, uint32_t *);
|
|
|
|
static int pci_vtcon_cfgwrite(void *, int, int, uint32_t);
|
|
|
|
static void pci_vtcon_neg_features(void *, uint64_t);
|
|
|
|
static void pci_vtcon_sock_accept(int, enum ev_type, void *);
|
|
|
|
static void pci_vtcon_sock_rx(int, enum ev_type, void *);
|
|
|
|
static void pci_vtcon_sock_tx(struct pci_vtcon_port *, void *, struct iovec *,
|
|
|
|
int);
|
|
|
|
static void pci_vtcon_control_send(struct pci_vtcon_softc *,
|
|
|
|
struct pci_vtcon_control *, const void *, size_t);
|
|
|
|
static void pci_vtcon_announce_port(struct pci_vtcon_port *);
|
|
|
|
static void pci_vtcon_open_port(struct pci_vtcon_port *, bool);
|
|
|
|
|
|
|
|
static struct virtio_consts vtcon_vi_consts = {
|
|
|
|
"vtcon", /* our name */
|
|
|
|
VTCON_MAXQ, /* we support VTCON_MAXQ virtqueues */
|
|
|
|
sizeof(struct pci_vtcon_config), /* config reg size */
|
|
|
|
pci_vtcon_reset, /* reset */
|
|
|
|
NULL, /* device-wide qnotify */
|
|
|
|
pci_vtcon_cfgread, /* read virtio config */
|
|
|
|
pci_vtcon_cfgwrite, /* write virtio config */
|
|
|
|
pci_vtcon_neg_features, /* apply negotiated features */
|
|
|
|
VTCON_S_HOSTCAPS, /* our capabilities */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
pci_vtcon_reset(void *vsc)
|
|
|
|
{
|
|
|
|
struct pci_vtcon_softc *sc;
|
|
|
|
|
|
|
|
sc = vsc;
|
|
|
|
|
2020-01-08 22:55:22 +00:00
|
|
|
DPRINTF(("vtcon: device reset requested!"));
|
2016-09-17 13:48:01 +00:00
|
|
|
vi_reset_dev(&sc->vsc_vs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pci_vtcon_neg_features(void *vsc, uint64_t negotiated_features)
|
|
|
|
{
|
|
|
|
struct pci_vtcon_softc *sc = vsc;
|
|
|
|
|
|
|
|
sc->vsc_features = negotiated_features;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pci_vtcon_cfgread(void *vsc, int offset, int size, uint32_t *retval)
|
|
|
|
{
|
|
|
|
struct pci_vtcon_softc *sc = vsc;
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
ptr = (uint8_t *)sc->vsc_config + offset;
|
|
|
|
memcpy(retval, ptr, size);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pci_vtcon_cfgwrite(void *vsc, int offset, int size, uint32_t val)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct pci_vtcon_port *
|
|
|
|
pci_vtcon_vq_to_port(struct pci_vtcon_softc *sc, struct vqueue_info *vq)
|
|
|
|
{
|
|
|
|
uint16_t num = vq->vq_num;
|
|
|
|
|
|
|
|
if (num == 0 || num == 1)
|
|
|
|
return (&sc->vsc_ports[0]);
|
|
|
|
|
|
|
|
if (num == 2 || num == 3)
|
|
|
|
return (&sc->vsc_control_port);
|
|
|
|
|
|
|
|
return (&sc->vsc_ports[(num / 2) - 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct vqueue_info *
|
|
|
|
pci_vtcon_port_to_vq(struct pci_vtcon_port *port, bool tx_queue)
|
|
|
|
{
|
|
|
|
int qnum;
|
|
|
|
|
|
|
|
qnum = tx_queue ? port->vsp_txq : port->vsp_rxq;
|
|
|
|
return (&port->vsp_sc->vsc_queues[qnum]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct pci_vtcon_port *
|
2019-06-26 20:30:41 +00:00
|
|
|
pci_vtcon_port_add(struct pci_vtcon_softc *sc, int port_id, const char *name,
|
2016-09-17 13:48:01 +00:00
|
|
|
pci_vtcon_cb_t *cb, void *arg)
|
|
|
|
{
|
|
|
|
struct pci_vtcon_port *port;
|
|
|
|
|
2019-06-26 20:30:41 +00:00
|
|
|
port = &sc->vsc_ports[port_id];
|
|
|
|
if (port->vsp_enabled) {
|
2016-09-17 13:48:01 +00:00
|
|
|
errno = EBUSY;
|
|
|
|
return (NULL);
|
|
|
|
}
|
2019-06-26 20:30:41 +00:00
|
|
|
port->vsp_id = port_id;
|
2016-09-17 13:48:01 +00:00
|
|
|
port->vsp_sc = sc;
|
|
|
|
port->vsp_name = name;
|
|
|
|
port->vsp_cb = cb;
|
|
|
|
port->vsp_arg = arg;
|
|
|
|
|
|
|
|
if (port->vsp_id == 0) {
|
|
|
|
/* port0 */
|
|
|
|
port->vsp_txq = 0;
|
|
|
|
port->vsp_rxq = 1;
|
|
|
|
} else {
|
2019-06-26 20:30:41 +00:00
|
|
|
port->vsp_txq = (port_id + 1) * 2;
|
2016-09-17 13:48:01 +00:00
|
|
|
port->vsp_rxq = port->vsp_txq + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
port->vsp_enabled = true;
|
|
|
|
return (port);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-06-26 20:30:41 +00:00
|
|
|
pci_vtcon_sock_add(struct pci_vtcon_softc *sc, const char *port_name,
|
|
|
|
const nvlist_t *nvl)
|
2016-09-17 13:48:01 +00:00
|
|
|
{
|
|
|
|
struct pci_vtcon_sock *sock;
|
|
|
|
struct sockaddr_un sun;
|
2019-06-26 20:30:41 +00:00
|
|
|
const char *name, *path;
|
|
|
|
char *cp, *pathcopy;
|
|
|
|
long port;
|
2016-09-17 13:48:01 +00:00
|
|
|
int s = -1, fd = -1, error = 0;
|
2017-02-14 13:35:59 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
cap_rights_t rights;
|
|
|
|
#endif
|
2016-09-17 13:48:01 +00:00
|
|
|
|
2019-06-26 20:30:41 +00:00
|
|
|
port = strtol(port_name, &cp, 0);
|
|
|
|
if (*cp != '\0' || port < 0 || port >= VTCON_MAXPORTS) {
|
|
|
|
EPRINTLN("vtcon: Invalid port %s", port_name);
|
|
|
|
error = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = get_config_value_node(nvl, "path");
|
|
|
|
if (path == NULL) {
|
|
|
|
EPRINTLN("vtcon: required path missing for port %ld", port);
|
|
|
|
error = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-09-17 13:48:01 +00:00
|
|
|
sock = calloc(1, sizeof(struct pci_vtcon_sock));
|
|
|
|
if (sock == NULL) {
|
|
|
|
error = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (s < 0) {
|
|
|
|
error = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-09-21 13:02:43 +00:00
|
|
|
pathcopy = strdup(path);
|
|
|
|
if (pathcopy == NULL) {
|
|
|
|
error = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(dirname(pathcopy), O_RDONLY | O_DIRECTORY);
|
2016-09-17 13:48:01 +00:00
|
|
|
if (fd < 0) {
|
2016-09-21 13:02:43 +00:00
|
|
|
free(pathcopy);
|
2016-09-17 13:48:01 +00:00
|
|
|
error = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
sun.sun_family = AF_UNIX;
|
|
|
|
sun.sun_len = sizeof(struct sockaddr_un);
|
2016-09-21 13:02:43 +00:00
|
|
|
strcpy(pathcopy, path);
|
2018-05-28 03:09:09 +00:00
|
|
|
strlcpy(sun.sun_path, basename(pathcopy), sizeof(sun.sun_path));
|
2016-09-21 13:02:43 +00:00
|
|
|
free(pathcopy);
|
2016-09-17 13:48:01 +00:00
|
|
|
|
|
|
|
if (bindat(fd, s, (struct sockaddr *)&sun, sun.sun_len) < 0) {
|
|
|
|
error = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
|
|
|
|
error = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(s, 1) < 0) {
|
|
|
|
error = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-02-14 13:35:59 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
cap_rights_init(&rights, CAP_ACCEPT, CAP_EVENT, CAP_READ, CAP_WRITE);
|
2019-01-16 00:39:23 +00:00
|
|
|
if (caph_rights_limit(s, &rights) == -1)
|
2017-02-14 13:35:59 +00:00
|
|
|
errx(EX_OSERR, "Unable to apply rights for sandbox");
|
|
|
|
#endif
|
2016-09-17 13:48:01 +00:00
|
|
|
|
2019-06-26 20:30:41 +00:00
|
|
|
name = get_config_value_node(nvl, "name");
|
|
|
|
if (name == NULL) {
|
|
|
|
EPRINTLN("vtcon: required name missing for port %ld", port);
|
|
|
|
error = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
sock->vss_port = pci_vtcon_port_add(sc, port, name, pci_vtcon_sock_tx, sock);
|
2016-09-17 13:48:01 +00:00
|
|
|
if (sock->vss_port == NULL) {
|
|
|
|
error = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock->vss_open = false;
|
|
|
|
sock->vss_conn_fd = -1;
|
|
|
|
sock->vss_server_fd = s;
|
|
|
|
sock->vss_server_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_accept,
|
|
|
|
sock);
|
|
|
|
|
|
|
|
if (sock->vss_server_evp == NULL) {
|
|
|
|
error = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
|
2019-07-12 18:20:56 +00:00
|
|
|
if (error != 0) {
|
|
|
|
if (s != -1)
|
|
|
|
close(s);
|
|
|
|
free(sock);
|
|
|
|
}
|
2016-09-17 13:48:01 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pci_vtcon_sock_accept(int fd __unused, enum ev_type t __unused, void *arg)
|
|
|
|
{
|
|
|
|
struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = accept(sock->vss_server_fd, NULL, NULL);
|
|
|
|
if (s < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (sock->vss_open) {
|
|
|
|
close(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock->vss_open = true;
|
|
|
|
sock->vss_conn_fd = s;
|
|
|
|
sock->vss_conn_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_rx, sock);
|
2016-11-24 21:53:42 +00:00
|
|
|
|
2016-09-17 13:48:01 +00:00
|
|
|
pci_vtcon_open_port(sock->vss_port, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pci_vtcon_sock_rx(int fd __unused, enum ev_type t __unused, void *arg)
|
|
|
|
{
|
|
|
|
struct pci_vtcon_port *port;
|
|
|
|
struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg;
|
|
|
|
struct vqueue_info *vq;
|
2021-03-30 08:43:24 +00:00
|
|
|
struct vi_req req;
|
2016-09-17 13:48:01 +00:00
|
|
|
struct iovec iov;
|
|
|
|
static char dummybuf[2048];
|
|
|
|
int len, n;
|
|
|
|
|
|
|
|
port = sock->vss_port;
|
|
|
|
vq = pci_vtcon_port_to_vq(port, true);
|
|
|
|
|
|
|
|
if (!sock->vss_open || !port->vsp_rx_ready) {
|
|
|
|
len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf));
|
|
|
|
if (len == 0)
|
|
|
|
goto close;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!vq_has_descs(vq)) {
|
|
|
|
len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf));
|
|
|
|
vq_endchains(vq, 1);
|
|
|
|
if (len == 0)
|
|
|
|
goto close;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2021-03-30 08:43:24 +00:00
|
|
|
n = vq_getchain(vq, &iov, 1, &req);
|
2021-08-24 18:10:08 +00:00
|
|
|
assert(n == 1);
|
2016-09-17 13:48:01 +00:00
|
|
|
len = readv(sock->vss_conn_fd, &iov, n);
|
|
|
|
|
|
|
|
if (len == 0 || (len < 0 && errno == EWOULDBLOCK)) {
|
bhyve: add support for virtio-net mergeable rx buffers
Mergeable rx buffers is a virtio-net feature that allows the hypervisor
to use multiple RX descriptor chains to receive a single receive packet.
Without this feature, a TSO-enabled guest is compelled to publish only
64K (or 32K) long chains, and each of these large buffers is consumed
to receive a single packet, even a very short one. This is a waste of
memory, as a RX queue has room for 256 chains, which means up to 16MB
of buffer memory for each (single-queue) vtnet device.
With the feature on, the guest can publish 2K long chains, and the
hypervisor will merge them as needed.
This change also enables the feature in the netmap backend, which
supports virtio-net offloads. We plan to add support for the
tap backend too.
Note that differently from QEMU/KVM, here we implement one-copy receive,
while QEMU uses two copies.
Reviewed by: jhb
MFC after: 3 weeks
Differential Revision: https://reviews.freebsd.org/D21007
2019-11-08 17:57:03 +00:00
|
|
|
vq_retchains(vq, 1);
|
2016-09-17 13:48:01 +00:00
|
|
|
vq_endchains(vq, 0);
|
|
|
|
if (len == 0)
|
|
|
|
goto close;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-30 08:43:24 +00:00
|
|
|
vq_relchain(vq, req.idx, len);
|
2016-09-17 13:48:01 +00:00
|
|
|
} while (vq_has_descs(vq));
|
|
|
|
|
|
|
|
vq_endchains(vq, 1);
|
|
|
|
|
|
|
|
close:
|
|
|
|
mevent_delete_close(sock->vss_conn_evp);
|
|
|
|
sock->vss_conn_fd = -1;
|
|
|
|
sock->vss_open = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pci_vtcon_sock_tx(struct pci_vtcon_port *port, void *arg, struct iovec *iov,
|
|
|
|
int niov)
|
|
|
|
{
|
|
|
|
struct pci_vtcon_sock *sock;
|
2016-11-24 22:16:18 +00:00
|
|
|
int i, ret;
|
2016-09-17 13:48:01 +00:00
|
|
|
|
|
|
|
sock = (struct pci_vtcon_sock *)arg;
|
|
|
|
|
|
|
|
if (sock->vss_conn_fd == -1)
|
|
|
|
return;
|
|
|
|
|
2016-11-24 22:16:18 +00:00
|
|
|
for (i = 0; i < niov; i++) {
|
|
|
|
ret = stream_write(sock->vss_conn_fd, iov[i].iov_base,
|
|
|
|
iov[i].iov_len);
|
|
|
|
if (ret <= 0)
|
|
|
|
break;
|
|
|
|
}
|
2016-09-17 13:48:01 +00:00
|
|
|
|
2016-11-24 22:16:18 +00:00
|
|
|
if (ret <= 0) {
|
2016-09-17 13:48:01 +00:00
|
|
|
mevent_delete_close(sock->vss_conn_evp);
|
|
|
|
sock->vss_conn_fd = -1;
|
|
|
|
sock->vss_open = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pci_vtcon_control_tx(struct pci_vtcon_port *port, void *arg, struct iovec *iov,
|
|
|
|
int niov)
|
|
|
|
{
|
|
|
|
struct pci_vtcon_softc *sc;
|
|
|
|
struct pci_vtcon_port *tmp;
|
|
|
|
struct pci_vtcon_control resp, *ctrl;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
assert(niov == 1);
|
|
|
|
|
|
|
|
sc = port->vsp_sc;
|
|
|
|
ctrl = (struct pci_vtcon_control *)iov->iov_base;
|
|
|
|
|
|
|
|
switch (ctrl->event) {
|
|
|
|
case VTCON_DEVICE_READY:
|
2016-11-24 21:53:42 +00:00
|
|
|
sc->vsc_ready = true;
|
2016-09-17 13:48:01 +00:00
|
|
|
/* set port ready events for registered ports */
|
|
|
|
for (i = 0; i < VTCON_MAXPORTS; i++) {
|
|
|
|
tmp = &sc->vsc_ports[i];
|
|
|
|
if (tmp->vsp_enabled)
|
|
|
|
pci_vtcon_announce_port(tmp);
|
2016-11-24 21:53:42 +00:00
|
|
|
|
|
|
|
if (tmp->vsp_open)
|
|
|
|
pci_vtcon_open_port(tmp, true);
|
2016-09-17 13:48:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VTCON_PORT_READY:
|
2019-06-26 20:30:41 +00:00
|
|
|
tmp = &sc->vsc_ports[ctrl->id];
|
|
|
|
if (ctrl->id >= VTCON_MAXPORTS || !tmp->vsp_enabled) {
|
2020-01-08 22:55:22 +00:00
|
|
|
WPRINTF(("VTCON_PORT_READY event for unknown port %d",
|
2016-09-17 13:48:01 +00:00
|
|
|
ctrl->id));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp->vsp_console) {
|
|
|
|
resp.event = VTCON_CONSOLE_PORT;
|
|
|
|
resp.id = ctrl->id;
|
|
|
|
resp.value = 1;
|
|
|
|
pci_vtcon_control_send(sc, &resp, NULL, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pci_vtcon_announce_port(struct pci_vtcon_port *port)
|
|
|
|
{
|
|
|
|
struct pci_vtcon_control event;
|
|
|
|
|
|
|
|
event.id = port->vsp_id;
|
|
|
|
event.event = VTCON_DEVICE_ADD;
|
|
|
|
event.value = 1;
|
|
|
|
pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0);
|
|
|
|
|
|
|
|
event.event = VTCON_PORT_NAME;
|
|
|
|
pci_vtcon_control_send(port->vsp_sc, &event, port->vsp_name,
|
|
|
|
strlen(port->vsp_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pci_vtcon_open_port(struct pci_vtcon_port *port, bool open)
|
|
|
|
{
|
|
|
|
struct pci_vtcon_control event;
|
|
|
|
|
2016-11-24 21:53:42 +00:00
|
|
|
if (!port->vsp_sc->vsc_ready) {
|
|
|
|
port->vsp_open = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-17 13:48:01 +00:00
|
|
|
event.id = port->vsp_id;
|
|
|
|
event.event = VTCON_PORT_OPEN;
|
|
|
|
event.value = (int)open;
|
|
|
|
pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pci_vtcon_control_send(struct pci_vtcon_softc *sc,
|
|
|
|
struct pci_vtcon_control *ctrl, const void *payload, size_t len)
|
|
|
|
{
|
|
|
|
struct vqueue_info *vq;
|
2021-03-30 08:43:24 +00:00
|
|
|
struct vi_req req;
|
2016-09-17 13:48:01 +00:00
|
|
|
struct iovec iov;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
vq = pci_vtcon_port_to_vq(&sc->vsc_control_port, true);
|
|
|
|
|
|
|
|
if (!vq_has_descs(vq))
|
|
|
|
return;
|
|
|
|
|
2021-03-30 08:43:24 +00:00
|
|
|
n = vq_getchain(vq, &iov, 1, &req);
|
2016-09-17 13:48:01 +00:00
|
|
|
assert(n == 1);
|
|
|
|
|
|
|
|
memcpy(iov.iov_base, ctrl, sizeof(struct pci_vtcon_control));
|
|
|
|
if (payload != NULL && len > 0)
|
|
|
|
memcpy(iov.iov_base + sizeof(struct pci_vtcon_control),
|
|
|
|
payload, len);
|
|
|
|
|
2021-03-30 08:43:24 +00:00
|
|
|
vq_relchain(vq, req.idx, sizeof(struct pci_vtcon_control) + len);
|
2016-09-17 13:48:01 +00:00
|
|
|
vq_endchains(vq, 1);
|
|
|
|
}
|
2021-12-26 07:52:38 +00:00
|
|
|
|
2016-09-17 13:48:01 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
pci_vtcon_notify_tx(void *vsc, struct vqueue_info *vq)
|
|
|
|
{
|
|
|
|
struct pci_vtcon_softc *sc;
|
|
|
|
struct pci_vtcon_port *port;
|
|
|
|
struct iovec iov[1];
|
2021-03-30 08:43:24 +00:00
|
|
|
struct vi_req req;
|
2021-08-24 18:10:08 +00:00
|
|
|
int n;
|
2016-09-17 13:48:01 +00:00
|
|
|
|
|
|
|
sc = vsc;
|
|
|
|
port = pci_vtcon_vq_to_port(sc, vq);
|
|
|
|
|
|
|
|
while (vq_has_descs(vq)) {
|
2021-03-30 08:43:24 +00:00
|
|
|
n = vq_getchain(vq, iov, 1, &req);
|
2021-08-24 18:10:08 +00:00
|
|
|
assert(n == 1);
|
2016-09-17 13:48:01 +00:00
|
|
|
if (port != NULL)
|
|
|
|
port->vsp_cb(port, port->vsp_arg, iov, 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release this chain and handle more
|
|
|
|
*/
|
2021-03-30 08:43:24 +00:00
|
|
|
vq_relchain(vq, req.idx, 0);
|
2016-09-17 13:48:01 +00:00
|
|
|
}
|
|
|
|
vq_endchains(vq, 1); /* Generate interrupt if appropriate. */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pci_vtcon_notify_rx(void *vsc, struct vqueue_info *vq)
|
|
|
|
{
|
|
|
|
struct pci_vtcon_softc *sc;
|
|
|
|
struct pci_vtcon_port *port;
|
|
|
|
|
|
|
|
sc = vsc;
|
|
|
|
port = pci_vtcon_vq_to_port(sc, vq);
|
|
|
|
|
|
|
|
if (!port->vsp_rx_ready) {
|
|
|
|
port->vsp_rx_ready = 1;
|
2019-06-11 15:52:41 +00:00
|
|
|
vq_kick_disable(vq);
|
2016-09-17 13:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 20:30:41 +00:00
|
|
|
/*
|
|
|
|
* Each console device has a "port" node which contains nodes for
|
|
|
|
* each port. Ports are numbered starting at 0.
|
|
|
|
*/
|
2016-09-17 13:48:01 +00:00
|
|
|
static int
|
2019-06-26 20:30:41 +00:00
|
|
|
pci_vtcon_legacy_config_port(nvlist_t *nvl, int port, char *opt)
|
|
|
|
{
|
|
|
|
char *name, *path;
|
|
|
|
char node_name[sizeof("XX")];
|
|
|
|
nvlist_t *port_nvl;
|
|
|
|
|
|
|
|
name = strsep(&opt, "=");
|
|
|
|
path = opt;
|
|
|
|
if (path == NULL) {
|
|
|
|
EPRINTLN("vtcon: port %s requires a path", name);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (port >= VTCON_MAXPORTS) {
|
|
|
|
EPRINTLN("vtcon: too many ports");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
snprintf(node_name, sizeof(node_name), "%d", port);
|
|
|
|
port_nvl = create_relative_config_node(nvl, node_name);
|
|
|
|
set_config_value_node(port_nvl, "name", name);
|
|
|
|
set_config_value_node(port_nvl, "path", path);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pci_vtcon_legacy_config(nvlist_t *nvl, const char *opts)
|
|
|
|
{
|
|
|
|
char *opt, *str, *tofree;
|
|
|
|
nvlist_t *ports_nvl;
|
|
|
|
int error, port;
|
|
|
|
|
|
|
|
ports_nvl = create_relative_config_node(nvl, "port");
|
|
|
|
tofree = str = strdup(opts);
|
|
|
|
error = 0;
|
|
|
|
port = 0;
|
|
|
|
while ((opt = strsep(&str, ",")) != NULL) {
|
|
|
|
error = pci_vtcon_legacy_config_port(ports_nvl, port, opt);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
port++;
|
|
|
|
}
|
|
|
|
free(tofree);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pci_vtcon_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
|
2016-09-17 13:48:01 +00:00
|
|
|
{
|
|
|
|
struct pci_vtcon_softc *sc;
|
2019-06-26 20:30:41 +00:00
|
|
|
nvlist_t *ports_nvl;
|
|
|
|
int i;
|
2016-09-17 13:48:01 +00:00
|
|
|
|
|
|
|
sc = calloc(1, sizeof(struct pci_vtcon_softc));
|
|
|
|
sc->vsc_config = calloc(1, sizeof(struct pci_vtcon_config));
|
|
|
|
sc->vsc_config->max_nr_ports = VTCON_MAXPORTS;
|
|
|
|
sc->vsc_config->cols = 80;
|
2021-12-26 07:52:38 +00:00
|
|
|
sc->vsc_config->rows = 25;
|
2016-09-17 13:48:01 +00:00
|
|
|
|
|
|
|
vi_softc_linkup(&sc->vsc_vs, &vtcon_vi_consts, sc, pi, sc->vsc_queues);
|
|
|
|
sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
|
|
|
|
|
|
|
|
for (i = 0; i < VTCON_MAXQ; i++) {
|
|
|
|
sc->vsc_queues[i].vq_qsize = VTCON_RINGSZ;
|
|
|
|
sc->vsc_queues[i].vq_notify = i % 2 == 0
|
|
|
|
? pci_vtcon_notify_rx
|
|
|
|
: pci_vtcon_notify_tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize config space */
|
|
|
|
pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_CONSOLE);
|
|
|
|
pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
|
|
|
|
pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_SIMPLECOMM);
|
2021-03-16 11:27:38 +00:00
|
|
|
pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_ID_CONSOLE);
|
2016-09-17 13:48:01 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
/* create control port */
|
|
|
|
sc->vsc_control_port.vsp_sc = sc;
|
|
|
|
sc->vsc_control_port.vsp_txq = 2;
|
|
|
|
sc->vsc_control_port.vsp_rxq = 3;
|
|
|
|
sc->vsc_control_port.vsp_cb = pci_vtcon_control_tx;
|
|
|
|
sc->vsc_control_port.vsp_enabled = true;
|
|
|
|
|
2019-06-26 20:30:41 +00:00
|
|
|
ports_nvl = find_relative_config_node(nvl, "port");
|
|
|
|
if (ports_nvl != NULL) {
|
|
|
|
const char *name;
|
|
|
|
void *cookie;
|
|
|
|
int type;
|
|
|
|
|
|
|
|
cookie = NULL;
|
|
|
|
while ((name = nvlist_next(ports_nvl, &type, &cookie)) !=
|
|
|
|
NULL) {
|
|
|
|
if (type != NV_TYPE_NVLIST)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pci_vtcon_sock_add(sc, name,
|
|
|
|
nvlist_get_nvlist(ports_nvl, name)) < 0) {
|
|
|
|
EPRINTLN("cannot create port %s: %s",
|
|
|
|
name, strerror(errno));
|
|
|
|
return (1);
|
|
|
|
}
|
2016-09-17 13:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct pci_devemu pci_de_vcon = {
|
|
|
|
.pe_emu = "virtio-console",
|
|
|
|
.pe_init = pci_vtcon_init,
|
|
|
|
.pe_barwrite = vi_pci_write,
|
|
|
|
.pe_barread = vi_pci_read
|
|
|
|
};
|
|
|
|
PCI_EMUL_SET(pci_de_vcon);
|