xref: /netbsd-src/sys/rump/net/lib/libvirtif/if_virt.c (revision 62a8debe1dc62962e18a1c918def78666141273b)
1 /*	$NetBSD: if_virt.c,v 1.16 2010/01/19 22:08:18 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: if_virt.c,v 1.16 2010/01/19 22:08:18 pooka Exp $");
30 
31 #include <sys/param.h>
32 #include <sys/condvar.h>
33 #include <sys/fcntl.h>
34 #include <sys/kmem.h>
35 #include <sys/kthread.h>
36 #include <sys/mutex.h>
37 #include <sys/poll.h>
38 #include <sys/sockio.h>
39 #include <sys/socketvar.h>
40 
41 #include <net/bpf.h>
42 #include <net/if.h>
43 #include <net/if_ether.h>
44 #include <net/if_tap.h>
45 
46 #include <netinet/in.h>
47 #include <netinet/in_var.h>
48 
49 #include <rump/rump.h>
50 #include <rump/rumpuser.h>
51 
52 #include "rump_private.h"
53 #include "rump_net_private.h"
54 
55 /*
56  * Virtual interface for userspace purposes.  Uses tap(4) to
57  * interface with the kernel and just simply shovels data
58  * to/from /dev/tap.
59  */
60 
61 #define VIRTIF_BASE "virt"
62 
63 static int	virtif_init(struct ifnet *);
64 static int	virtif_ioctl(struct ifnet *, u_long, void *);
65 static void	virtif_start(struct ifnet *);
66 static void	virtif_stop(struct ifnet *, int);
67 
68 struct virtif_sc {
69 	struct ethercom sc_ec;
70 	int sc_tapfd;
71 	kmutex_t sc_sendmtx;
72 	kcondvar_t sc_sendcv;
73 };
74 
75 static void virtif_worker(void *);
76 static void virtif_sender(void *);
77 
78 #if 0
79 /*
80  * Create a socket and call ifioctl() to configure the interface.
81  * This trickles down to virtif_ioctl().
82  */
83 static int
84 configaddr(struct ifnet *ifp, struct ifaliasreq *ia)
85 {
86 	struct socket *so;
87 	int error;
88 
89 	strcpy(ia->ifra_name, ifp->if_xname);
90 	error = socreate(ia->ifra_addr.sa_family, &so, SOCK_DGRAM,
91 	    0, curlwp, NULL);
92 	if (error)
93 		return error;
94 	error = ifioctl(so, SIOCAIFADDR, ia, curlwp);
95 	soclose(so);
96 
97 	return error;
98 }
99 #endif
100 
101 int
102 rump_virtif_create(int num)
103 {
104 	struct virtif_sc *sc;
105 	struct ifnet *ifp;
106 	uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
107 	char tapdev[16];
108 	int fd, error;
109 
110 	snprintf(tapdev, sizeof(tapdev), "/dev/tap%d", num);
111 	fd = rumpuser_open(tapdev, O_RDWR, &error);
112 	if (fd == -1) {
113 		printf("virtif_create: can't open /dev/tap %d\n", error);
114 		return error;
115 	}
116 	KASSERT(num < 0x100);
117 	enaddr[2] = arc4random() & 0xff;
118 	enaddr[5] = num;
119 
120 	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
121 	sc->sc_tapfd = fd;
122 
123 	ifp = &sc->sc_ec.ec_if;
124 	sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, num);
125 	ifp->if_softc = sc;
126 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
127 	ifp->if_init = virtif_init;
128 	ifp->if_ioctl = virtif_ioctl;
129 	ifp->if_start = virtif_start;
130 	ifp->if_stop = virtif_stop;
131 
132 	mutex_init(&sc->sc_sendmtx, MUTEX_DEFAULT, IPL_NONE);
133 	cv_init(&sc->sc_sendcv, "virtsnd");
134 
135 	if_attach(ifp);
136 	ether_ifattach(ifp, enaddr);
137 
138 	return 0;
139 }
140 
141 static int
142 virtif_init(struct ifnet *ifp)
143 {
144 	int rv;
145 
146 	if (rump_threads) {
147 		rv = kthread_create(PRI_NONE, 0, NULL, virtif_worker, ifp,
148 		    NULL, "virtifi");
149 		/* XXX: should do proper cleanup */
150 		if (rv) {
151 			panic("if_virt: can't create worker");
152 		}
153 		rv = kthread_create(PRI_NONE, 0, NULL, virtif_sender, ifp,
154 		    NULL, "virtifs");
155 		if (rv) {
156 			panic("if_virt: can't create sender");
157 		}
158 	} else {
159 		printf("WARNING: threads not enabled, receive NOT working\n");
160 	}
161 	ifp->if_flags |= IFF_RUNNING;
162 
163 	return 0;
164 }
165 
166 static int
167 virtif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
168 {
169 	int s, rv;
170 
171 	s = splnet();
172 	rv = ether_ioctl(ifp, cmd, data);
173 	if (rv == ENETRESET)
174 		rv = 0;
175 	splx(s);
176 
177 	return rv;
178 }
179 
180 /* just send everything in-context */
181 static void
182 virtif_start(struct ifnet *ifp)
183 {
184 	struct virtif_sc *sc = ifp->if_softc;
185 
186 	mutex_enter(&sc->sc_sendmtx);
187 	cv_signal(&sc->sc_sendcv);
188 	mutex_exit(&sc->sc_sendmtx);
189 }
190 
191 static void
192 virtif_stop(struct ifnet *ifp, int disable)
193 {
194 
195 	panic("%s: unimpl", __func__);
196 }
197 
198 static void
199 virtif_worker(void *arg)
200 {
201 	struct ifnet *ifp = arg;
202 	struct virtif_sc *sc = ifp->if_softc;
203 	struct mbuf *m;
204 	size_t plen = ETHER_MAX_LEN_JUMBO+1;
205 	ssize_t n;
206 	int error;
207 
208 	for (;;) {
209 		m = m_gethdr(M_WAIT, MT_DATA);
210 		MEXTMALLOC(m, plen, M_WAIT);
211 
212  again:
213 		n = rumpuser_read(sc->sc_tapfd, mtod(m, void *), plen, &error);
214 		KASSERT(n < ETHER_MAX_LEN_JUMBO);
215 		if (n <= 0) {
216 			/*
217 			 * work around tap bug: /dev/tap is opened in
218 			 * non-blocking mode if it previously was
219 			 * non-blocking.
220 			 */
221 			if (n == -1 && error == EAGAIN) {
222 				struct pollfd pfd;
223 
224 				pfd.fd = sc->sc_tapfd;
225 				pfd.events = POLLIN;
226 
227 				rumpuser_poll(&pfd, 1, INFTIM, &error);
228 				goto again;
229 			}
230 			m_freem(m);
231 			break;
232 		}
233 		m->m_len = m->m_pkthdr.len = n;
234 		m->m_pkthdr.rcvif = ifp;
235 		if (ifp->if_bpf) {
236 			bpf_ops->bpf_mtap(ifp->if_bpf, m);
237 		}
238 		ether_input(ifp, m);
239 	}
240 
241 	panic("virtif_workin is a lazy boy %d\n", error);
242 }
243 
244 /* lazy bum stetson-harrison magic value */
245 #define LB_SH 32
246 static void
247 virtif_sender(void *arg)
248 {
249 	struct ifnet *ifp = arg;
250 	struct virtif_sc *sc = ifp->if_softc;
251 	struct mbuf *m, *m0;
252 	struct rumpuser_iovec io[LB_SH];
253 	int i, error;
254 
255 	mutex_enter(&sc->sc_sendmtx);
256 	for (;;) {
257 		IF_DEQUEUE(&ifp->if_snd, m0);
258 		if (!m0) {
259 			cv_wait(&sc->sc_sendcv, &sc->sc_sendmtx);
260 			continue;
261 		}
262 		mutex_exit(&sc->sc_sendmtx);
263 
264 		m = m0;
265 		for (i = 0; i < LB_SH && m; i++) {
266 			io[i].iov_base = mtod(m, void *);
267 			io[i].iov_len = m->m_len;
268 			m = m->m_next;
269 		}
270 		if (i == LB_SH)
271 			panic("lazy bum");
272 		if (ifp->if_bpf) {
273 			bpf_ops->bpf_mtap(ifp->if_bpf, m0);
274 		}
275 		rumpuser_writev(sc->sc_tapfd, io, i, &error);
276 		m_freem(m0);
277 		mutex_enter(&sc->sc_sendmtx);
278 	}
279 
280 	mutex_exit(softnet_lock);
281 }
282 
283 /*
284  * dummyif is a nada-interface.
285  * As it requires nothing external, it can be used for testing
286  * interface configuration.
287  */
288 static int	dummyif_init(struct ifnet *);
289 static void	dummyif_start(struct ifnet *);
290 
291 void
292 rump_dummyif_create()
293 {
294 	struct ifnet *ifp;
295 	struct ethercom *ec;
296 	uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
297 
298 	enaddr[2] = arc4random() & 0xff;
299 	enaddr[5] = arc4random() & 0xff;
300 
301 	ec = kmem_zalloc(sizeof(*ec), KM_SLEEP);
302 
303 	ifp = &ec->ec_if;
304 	strlcpy(ifp->if_xname, "dummy0", sizeof(ifp->if_xname));
305 	ifp->if_softc = ifp;
306 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
307 	ifp->if_init = dummyif_init;
308 	ifp->if_ioctl = virtif_ioctl;
309 	ifp->if_start = dummyif_start;
310 
311 	if_attach(ifp);
312 	ether_ifattach(ifp, enaddr);
313 }
314 
315 static int
316 dummyif_init(struct ifnet *ifp)
317 {
318 
319 	ifp->if_flags |= IFF_RUNNING;
320 	return 0;
321 }
322 
323 static void
324 dummyif_start(struct ifnet *ifp)
325 {
326 
327 }
328