xref: /freebsd/usr.sbin/bhyve/net_backend_netgraph.c (revision be74aede49fb480792448bf563c5079998de7cbd)
1*be74aedeSMark Johnston /*-
2*be74aedeSMark Johnston  * SPDX-License-Identifier: BSD-2-Clause
3*be74aedeSMark Johnston  *
4*be74aedeSMark Johnston  * Copyright (c) 2019 Vincenzo Maffione <vmaffione@FreeBSD.org>
5*be74aedeSMark Johnston  *
6*be74aedeSMark Johnston  * Redistribution and use in source and binary forms, with or without
7*be74aedeSMark Johnston  * modification, are permitted provided that the following conditions
8*be74aedeSMark Johnston  * are met:
9*be74aedeSMark Johnston  * 1. Redistributions of source code must retain the above copyright
10*be74aedeSMark Johnston  *    notice, this list of conditions and the following disclaimer.
11*be74aedeSMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
12*be74aedeSMark Johnston  *    notice, this list of conditions and the following disclaimer in the
13*be74aedeSMark Johnston  *    documentation and/or other materials provided with the distribution.
14*be74aedeSMark Johnston  *
15*be74aedeSMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
16*be74aedeSMark Johnston  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*be74aedeSMark Johnston  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18*be74aedeSMark Johnston  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
19*be74aedeSMark Johnston  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20*be74aedeSMark Johnston  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21*be74aedeSMark Johnston  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22*be74aedeSMark Johnston  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23*be74aedeSMark Johnston  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
24*be74aedeSMark Johnston  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25*be74aedeSMark Johnston  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*be74aedeSMark Johnston  */
27*be74aedeSMark Johnston 
28*be74aedeSMark Johnston #ifndef WITHOUT_CAPSICUM
29*be74aedeSMark Johnston #include <sys/capsicum.h>
30*be74aedeSMark Johnston #endif
31*be74aedeSMark Johnston #include <sys/socket.h>
32*be74aedeSMark Johnston #include <sys/sysctl.h>
33*be74aedeSMark Johnston 
34*be74aedeSMark Johnston #ifndef WITHOUT_CAPSICUM
35*be74aedeSMark Johnston #include <capsicum_helpers.h>
36*be74aedeSMark Johnston #endif
37*be74aedeSMark Johnston #include <err.h>
38*be74aedeSMark Johnston #include <netgraph.h>
39*be74aedeSMark Johnston #include <string.h>
40*be74aedeSMark Johnston #include <sysexits.h>
41*be74aedeSMark Johnston #include <unistd.h>
42*be74aedeSMark Johnston 
43*be74aedeSMark Johnston #include "config.h"
44*be74aedeSMark Johnston #include "debug.h"
45*be74aedeSMark Johnston #include "net_backends.h"
46*be74aedeSMark Johnston #include "net_backends_priv.h"
47*be74aedeSMark Johnston 
48*be74aedeSMark Johnston #define NG_SBUF_MAX_SIZE (4 * 1024 * 1024)
49*be74aedeSMark Johnston 
50*be74aedeSMark Johnston static int
ng_init(struct net_backend * be,const char * devname __unused,nvlist_t * nvl,net_be_rxeof_t cb,void * param)51*be74aedeSMark Johnston ng_init(struct net_backend *be, const char *devname __unused,
52*be74aedeSMark Johnston 	 nvlist_t *nvl, net_be_rxeof_t cb, void *param)
53*be74aedeSMark Johnston {
54*be74aedeSMark Johnston 	struct tap_priv *p = NET_BE_PRIV(be);
55*be74aedeSMark Johnston 	struct ngm_connect ngc;
56*be74aedeSMark Johnston 	const char *value, *nodename;
57*be74aedeSMark Johnston 	int sbsz;
58*be74aedeSMark Johnston 	int ctrl_sock;
59*be74aedeSMark Johnston 	int flags;
60*be74aedeSMark Johnston 	unsigned long maxsbsz;
61*be74aedeSMark Johnston 	size_t msbsz;
62*be74aedeSMark Johnston #ifndef WITHOUT_CAPSICUM
63*be74aedeSMark Johnston 	cap_rights_t rights;
64*be74aedeSMark Johnston #endif
65*be74aedeSMark Johnston 
66*be74aedeSMark Johnston 	if (cb == NULL) {
67*be74aedeSMark Johnston 		EPRINTLN("Netgraph backend requires non-NULL callback");
68*be74aedeSMark Johnston 		return (-1);
69*be74aedeSMark Johnston 	}
70*be74aedeSMark Johnston 
71*be74aedeSMark Johnston 	be->fd = -1;
72*be74aedeSMark Johnston 
73*be74aedeSMark Johnston 	memset(&ngc, 0, sizeof(ngc));
74*be74aedeSMark Johnston 
75*be74aedeSMark Johnston 	value = get_config_value_node(nvl, "path");
76*be74aedeSMark Johnston 	if (value == NULL) {
77*be74aedeSMark Johnston 		EPRINTLN("path must be provided");
78*be74aedeSMark Johnston 		return (-1);
79*be74aedeSMark Johnston 	}
80*be74aedeSMark Johnston 	strncpy(ngc.path, value, NG_PATHSIZ - 1);
81*be74aedeSMark Johnston 
82*be74aedeSMark Johnston 	value = get_config_value_node(nvl, "hook");
83*be74aedeSMark Johnston 	if (value == NULL)
84*be74aedeSMark Johnston 		value = "vmlink";
85*be74aedeSMark Johnston 	strncpy(ngc.ourhook, value, NG_HOOKSIZ - 1);
86*be74aedeSMark Johnston 
87*be74aedeSMark Johnston 	value = get_config_value_node(nvl, "peerhook");
88*be74aedeSMark Johnston 	if (value == NULL) {
89*be74aedeSMark Johnston 		EPRINTLN("peer hook must be provided");
90*be74aedeSMark Johnston 		return (-1);
91*be74aedeSMark Johnston 	}
92*be74aedeSMark Johnston 	strncpy(ngc.peerhook, value, NG_HOOKSIZ - 1);
93*be74aedeSMark Johnston 
94*be74aedeSMark Johnston 	nodename = get_config_value_node(nvl, "socket");
95*be74aedeSMark Johnston 	if (NgMkSockNode(nodename,
96*be74aedeSMark Johnston 		&ctrl_sock, &be->fd) < 0) {
97*be74aedeSMark Johnston 		EPRINTLN("can't get Netgraph sockets");
98*be74aedeSMark Johnston 		return (-1);
99*be74aedeSMark Johnston 	}
100*be74aedeSMark Johnston 
101*be74aedeSMark Johnston 	if (NgSendMsg(ctrl_sock, ".",
102*be74aedeSMark Johnston 		NGM_GENERIC_COOKIE,
103*be74aedeSMark Johnston 		NGM_CONNECT, &ngc, sizeof(ngc)) < 0) {
104*be74aedeSMark Johnston 		EPRINTLN("can't connect to node");
105*be74aedeSMark Johnston 		close(ctrl_sock);
106*be74aedeSMark Johnston 		goto error;
107*be74aedeSMark Johnston 	}
108*be74aedeSMark Johnston 
109*be74aedeSMark Johnston 	close(ctrl_sock);
110*be74aedeSMark Johnston 
111*be74aedeSMark Johnston 	flags = fcntl(be->fd, F_GETFL);
112*be74aedeSMark Johnston 
113*be74aedeSMark Johnston 	if (flags < 0) {
114*be74aedeSMark Johnston 		EPRINTLN("can't get socket flags");
115*be74aedeSMark Johnston 		goto error;
116*be74aedeSMark Johnston 	}
117*be74aedeSMark Johnston 
118*be74aedeSMark Johnston 	if (fcntl(be->fd, F_SETFL, flags | O_NONBLOCK) < 0) {
119*be74aedeSMark Johnston 		EPRINTLN("can't set O_NONBLOCK flag");
120*be74aedeSMark Johnston 		goto error;
121*be74aedeSMark Johnston 	}
122*be74aedeSMark Johnston 
123*be74aedeSMark Johnston 	/*
124*be74aedeSMark Johnston 	 * The default ng_socket(4) buffer's size is too low.
125*be74aedeSMark Johnston 	 * Calculate the minimum value between NG_SBUF_MAX_SIZE
126*be74aedeSMark Johnston 	 * and kern.ipc.maxsockbuf.
127*be74aedeSMark Johnston 	 */
128*be74aedeSMark Johnston 	msbsz = sizeof(maxsbsz);
129*be74aedeSMark Johnston 	if (sysctlbyname("kern.ipc.maxsockbuf", &maxsbsz, &msbsz,
130*be74aedeSMark Johnston 		NULL, 0) < 0) {
131*be74aedeSMark Johnston 		EPRINTLN("can't get 'kern.ipc.maxsockbuf' value");
132*be74aedeSMark Johnston 		goto error;
133*be74aedeSMark Johnston 	}
134*be74aedeSMark Johnston 
135*be74aedeSMark Johnston 	/*
136*be74aedeSMark Johnston 	 * We can't set the socket buffer size to kern.ipc.maxsockbuf value,
137*be74aedeSMark Johnston 	 * as it takes into account the mbuf(9) overhead.
138*be74aedeSMark Johnston 	 */
139*be74aedeSMark Johnston 	maxsbsz = maxsbsz * MCLBYTES / (MSIZE + MCLBYTES);
140*be74aedeSMark Johnston 
141*be74aedeSMark Johnston 	sbsz = MIN(NG_SBUF_MAX_SIZE, maxsbsz);
142*be74aedeSMark Johnston 
143*be74aedeSMark Johnston 	if (setsockopt(be->fd, SOL_SOCKET, SO_SNDBUF, &sbsz,
144*be74aedeSMark Johnston 		sizeof(sbsz)) < 0) {
145*be74aedeSMark Johnston 		EPRINTLN("can't set TX buffer size");
146*be74aedeSMark Johnston 		goto error;
147*be74aedeSMark Johnston 	}
148*be74aedeSMark Johnston 
149*be74aedeSMark Johnston 	if (setsockopt(be->fd, SOL_SOCKET, SO_RCVBUF, &sbsz,
150*be74aedeSMark Johnston 		sizeof(sbsz)) < 0) {
151*be74aedeSMark Johnston 		EPRINTLN("can't set RX buffer size");
152*be74aedeSMark Johnston 		goto error;
153*be74aedeSMark Johnston 	}
154*be74aedeSMark Johnston 
155*be74aedeSMark Johnston #ifndef WITHOUT_CAPSICUM
156*be74aedeSMark Johnston 	cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE);
157*be74aedeSMark Johnston 	if (caph_rights_limit(be->fd, &rights) == -1)
158*be74aedeSMark Johnston 		errx(EX_OSERR, "Unable to apply rights for sandbox");
159*be74aedeSMark Johnston #endif
160*be74aedeSMark Johnston 
161*be74aedeSMark Johnston 	memset(p->bbuf, 0, sizeof(p->bbuf));
162*be74aedeSMark Johnston 	p->bbuflen = 0;
163*be74aedeSMark Johnston 
164*be74aedeSMark Johnston 	p->mevp = mevent_add_disabled(be->fd, EVF_READ, cb, param);
165*be74aedeSMark Johnston 	if (p->mevp == NULL) {
166*be74aedeSMark Johnston 		EPRINTLN("Could not register event");
167*be74aedeSMark Johnston 		goto error;
168*be74aedeSMark Johnston 	}
169*be74aedeSMark Johnston 
170*be74aedeSMark Johnston 	return (0);
171*be74aedeSMark Johnston 
172*be74aedeSMark Johnston error:
173*be74aedeSMark Johnston 	tap_cleanup(be);
174*be74aedeSMark Johnston 	return (-1);
175*be74aedeSMark Johnston }
176*be74aedeSMark Johnston 
177*be74aedeSMark Johnston static struct net_backend ng_backend = {
178*be74aedeSMark Johnston 	.prefix = "netgraph",
179*be74aedeSMark Johnston 	.priv_size = sizeof(struct tap_priv),
180*be74aedeSMark Johnston 	.init = ng_init,
181*be74aedeSMark Johnston 	.cleanup = tap_cleanup,
182*be74aedeSMark Johnston 	.send = tap_send,
183*be74aedeSMark Johnston 	.peek_recvlen = tap_peek_recvlen,
184*be74aedeSMark Johnston 	.recv = tap_recv,
185*be74aedeSMark Johnston 	.recv_enable = tap_recv_enable,
186*be74aedeSMark Johnston 	.recv_disable = tap_recv_disable,
187*be74aedeSMark Johnston 	.get_cap = tap_get_cap,
188*be74aedeSMark Johnston 	.set_cap = tap_set_cap,
189*be74aedeSMark Johnston };
190*be74aedeSMark Johnston 
191*be74aedeSMark Johnston DATA_SET(net_backend_set, ng_backend);
192