Path: blob/main/usr.sbin/bhyve/net_backend_netgraph.c
105642 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2019 Vincenzo Maffione <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR17* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS18* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,19* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT20* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR21* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,22* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE23* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,24* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.25*/2627#ifndef WITHOUT_CAPSICUM28#include <sys/capsicum.h>29#endif30#include <sys/socket.h>31#include <sys/sysctl.h>3233#ifndef WITHOUT_CAPSICUM34#include <capsicum_helpers.h>35#endif36#include <err.h>37#include <netgraph.h>38#include <string.h>39#include <sysexits.h>40#include <unistd.h>4142#include "config.h"43#include "debug.h"44#include "net_backends.h"45#include "net_backends_priv.h"4647#define NG_SBUF_MAX_SIZE (4 * 1024 * 1024)4849static int50ng_init(struct net_backend *be, const char *devname __unused,51nvlist_t *nvl, net_be_rxeof_t cb, void *param)52{53struct tap_priv *p = NET_BE_PRIV(be);54struct ngm_connect ngc;55const char *value, *nodename;56int sbsz;57int ctrl_sock;58int flags;59unsigned long maxsbsz;60size_t msbsz;61#ifndef WITHOUT_CAPSICUM62cap_rights_t rights;63#endif6465if (cb == NULL) {66EPRINTLN("Netgraph backend requires non-NULL callback");67return (-1);68}6970be->fd = -1;7172memset(&ngc, 0, sizeof(ngc));7374value = get_config_value_node(nvl, "path");75if (value == NULL) {76EPRINTLN("path must be provided");77return (-1);78}79strncpy(ngc.path, value, NG_PATHSIZ - 1);8081value = get_config_value_node(nvl, "hook");82if (value == NULL)83value = "vmlink";84strncpy(ngc.ourhook, value, NG_HOOKSIZ - 1);8586value = get_config_value_node(nvl, "peerhook");87if (value == NULL) {88EPRINTLN("peer hook must be provided");89return (-1);90}91strncpy(ngc.peerhook, value, NG_HOOKSIZ - 1);9293nodename = get_config_value_node(nvl, "socket");94if (NgMkSockNode(nodename,95&ctrl_sock, &be->fd) < 0) {96EPRINTLN("can't get Netgraph sockets");97return (-1);98}99100if (NgSendMsg(ctrl_sock, ".",101NGM_GENERIC_COOKIE,102NGM_CONNECT, &ngc, sizeof(ngc)) < 0) {103EPRINTLN("can't connect to node");104close(ctrl_sock);105goto error;106}107108close(ctrl_sock);109110flags = fcntl(be->fd, F_GETFL);111112if (flags < 0) {113EPRINTLN("can't get socket flags");114goto error;115}116117if (fcntl(be->fd, F_SETFL, flags | O_NONBLOCK) < 0) {118EPRINTLN("can't set O_NONBLOCK flag");119goto error;120}121122/*123* The default ng_socket(4) buffer's size is too low.124* Calculate the minimum value between NG_SBUF_MAX_SIZE125* and kern.ipc.maxsockbuf.126*/127msbsz = sizeof(maxsbsz);128if (sysctlbyname("kern.ipc.maxsockbuf", &maxsbsz, &msbsz,129NULL, 0) < 0) {130EPRINTLN("can't get 'kern.ipc.maxsockbuf' value");131goto error;132}133134/*135* We can't set the socket buffer size to kern.ipc.maxsockbuf value,136* as it takes into account the mbuf(9) overhead.137*/138maxsbsz = maxsbsz * MCLBYTES / (MSIZE + MCLBYTES);139140sbsz = MIN(NG_SBUF_MAX_SIZE, maxsbsz);141142if (setsockopt(be->fd, SOL_SOCKET, SO_SNDBUF, &sbsz,143sizeof(sbsz)) < 0) {144EPRINTLN("can't set TX buffer size");145goto error;146}147148if (setsockopt(be->fd, SOL_SOCKET, SO_RCVBUF, &sbsz,149sizeof(sbsz)) < 0) {150EPRINTLN("can't set RX buffer size");151goto error;152}153154#ifndef WITHOUT_CAPSICUM155cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE);156if (caph_rights_limit(be->fd, &rights) == -1)157errx(EX_OSERR, "Unable to apply rights for sandbox");158#endif159160memset(p->bbuf, 0, sizeof(p->bbuf));161p->bbuflen = 0;162163p->mevp = mevent_add_disabled(be->fd, EVF_READ, cb, param);164if (p->mevp == NULL) {165EPRINTLN("Could not register event");166goto error;167}168169return (0);170171error:172tap_cleanup(be);173return (-1);174}175176static struct net_backend ng_backend = {177.prefix = "netgraph",178.priv_size = sizeof(struct tap_priv),179.init = ng_init,180.cleanup = tap_cleanup,181.send = tap_send,182.peek_recvlen = tap_peek_recvlen,183.recv = tap_recv,184.recv_enable = tap_recv_enable,185.recv_disable = tap_recv_disable,186.get_cap = tap_get_cap,187.set_cap = tap_set_cap,188};189190DATA_SET(net_backend_set, ng_backend);191192193