if_virt.c revision 1.32 1 /* $NetBSD: if_virt.c,v 1.32 2013/07/03 15:06:25 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2008, 2013 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.32 2013/07/03 15:06:25 pooka Exp $");
30
31 #include <sys/param.h>
32 #include <sys/condvar.h>
33 #include <sys/fcntl.h>
34 #include <sys/kernel.h>
35 #include <sys/kmem.h>
36 #include <sys/kthread.h>
37 #include <sys/mutex.h>
38 #include <sys/poll.h>
39 #include <sys/sockio.h>
40 #include <sys/socketvar.h>
41 #include <sys/cprng.h>
42
43 #include <net/bpf.h>
44 #include <net/if.h>
45 #include <net/if_ether.h>
46 #include <net/if_tap.h>
47
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50
51 #include <rump/rump.h>
52
53 #include "rump_private.h"
54 #include "rump_net_private.h"
55
56 #include "rumpcomp_user.h"
57
58 /*
59 * Virtual interface. Uses hypercalls to shovel packets back
60 * and forth. The exact method for shoveling depends on the
61 * hypercall implementation.
62 */
63
64 #ifndef VIRTIF_BASE
65 #define VIRTIF_BASE "virt"
66 #endif
67
68 static int virtif_init(struct ifnet *);
69 static int virtif_ioctl(struct ifnet *, u_long, void *);
70 static void virtif_start(struct ifnet *);
71 static void virtif_stop(struct ifnet *, int);
72
73 struct virtif_sc {
74 struct ethercom sc_ec;
75 struct virtif_user *sc_viu;
76 bool sc_dying;
77 struct lwp *sc_l_snd, *sc_l_rcv;
78 kmutex_t sc_mtx;
79 kcondvar_t sc_cv;
80 };
81
82 static void virtif_receiver(void *);
83 static void virtif_sender(void *);
84 static int virtif_clone(struct if_clone *, int);
85 static int virtif_unclone(struct ifnet *);
86
87 struct if_clone virtif_cloner =
88 IF_CLONE_INITIALIZER(VIRTIF_BASE, virtif_clone, virtif_unclone);
89
90 int
91 rump_virtif_create(int num)
92 {
93 struct virtif_sc *sc;
94 struct virtif_user *viu;
95 struct ifnet *ifp;
96 uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
97 int error = 0;
98
99 if (num >= 0x100)
100 return E2BIG;
101
102 if ((error = rumpcomp_virtif_create(num, &viu)) != 0)
103 return error;
104
105 enaddr[2] = cprng_fast32() & 0xff;
106 enaddr[5] = num;
107
108 sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
109 sc->sc_dying = false;
110 sc->sc_viu = viu;
111
112 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
113 cv_init(&sc->sc_cv, VIRTIF_BASE "snd");
114 ifp = &sc->sc_ec.ec_if;
115 sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, num);
116 ifp->if_softc = sc;
117
118 if (rump_threads) {
119 if ((error = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL,
120 virtif_receiver, ifp, &sc->sc_l_rcv,
121 VIRTIF_BASE "ifr")) != 0)
122 goto out;
123
124 if ((error = kthread_create(PRI_NONE,
125 KTHREAD_MUSTJOIN | KTHREAD_MPSAFE, NULL,
126 virtif_sender, ifp, &sc->sc_l_snd, VIRTIF_BASE "ifs")) != 0)
127 goto out;
128 } else {
129 printf("WARNING: threads not enabled, receive NOT working\n");
130 }
131
132 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
133 ifp->if_init = virtif_init;
134 ifp->if_ioctl = virtif_ioctl;
135 ifp->if_start = virtif_start;
136 ifp->if_stop = virtif_stop;
137 IFQ_SET_READY(&ifp->if_snd);
138
139 if_attach(ifp);
140 ether_ifattach(ifp, enaddr);
141
142 out:
143 if (error) {
144 virtif_unclone(ifp);
145 }
146
147 return error;
148 }
149
150 static int
151 virtif_clone(struct if_clone *ifc, int unit)
152 {
153
154 return rump_virtif_create(unit);
155 }
156
157 static int
158 virtif_unclone(struct ifnet *ifp)
159 {
160 struct virtif_sc *sc = ifp->if_softc;
161
162 mutex_enter(&sc->sc_mtx);
163 if (sc->sc_dying) {
164 mutex_exit(&sc->sc_mtx);
165 return EINPROGRESS;
166 }
167 sc->sc_dying = true;
168 cv_broadcast(&sc->sc_cv);
169 mutex_exit(&sc->sc_mtx);
170
171 rumpcomp_virtif_dying(sc->sc_viu);
172
173 virtif_stop(ifp, 1);
174 if_down(ifp);
175
176 if (sc->sc_l_snd) {
177 kthread_join(sc->sc_l_snd);
178 sc->sc_l_snd = NULL;
179 }
180 if (sc->sc_l_rcv) {
181 kthread_join(sc->sc_l_rcv);
182 sc->sc_l_rcv = NULL;
183 }
184
185 rumpcomp_virtif_destroy(sc->sc_viu);
186
187 mutex_destroy(&sc->sc_mtx);
188 cv_destroy(&sc->sc_cv);
189 kmem_free(sc, sizeof(*sc));
190
191 ether_ifdetach(ifp);
192 if_detach(ifp);
193
194 return 0;
195 }
196
197 static int
198 virtif_init(struct ifnet *ifp)
199 {
200 struct virtif_sc *sc = ifp->if_softc;
201
202 ifp->if_flags |= IFF_RUNNING;
203
204 mutex_enter(&sc->sc_mtx);
205 cv_broadcast(&sc->sc_cv);
206 mutex_exit(&sc->sc_mtx);
207
208 return 0;
209 }
210
211 static int
212 virtif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
213 {
214 int s, rv;
215
216 s = splnet();
217 rv = ether_ioctl(ifp, cmd, data);
218 if (rv == ENETRESET)
219 rv = 0;
220 splx(s);
221
222 return rv;
223 }
224
225 static void
226 virtif_start(struct ifnet *ifp)
227 {
228 struct virtif_sc *sc = ifp->if_softc;
229
230 mutex_enter(&sc->sc_mtx);
231 ifp->if_flags |= IFF_OACTIVE;
232 cv_broadcast(&sc->sc_cv);
233 mutex_exit(&sc->sc_mtx);
234 }
235
236 static void
237 virtif_stop(struct ifnet *ifp, int disable)
238 {
239 struct virtif_sc *sc = ifp->if_softc;
240
241 ifp->if_flags &= ~IFF_RUNNING;
242
243 mutex_enter(&sc->sc_mtx);
244 cv_broadcast(&sc->sc_cv);
245 mutex_exit(&sc->sc_mtx);
246 }
247
248 #define POLLTIMO_MS 1
249 static void
250 virtif_receiver(void *arg)
251 {
252 struct ifnet *ifp = arg;
253 struct virtif_sc *sc = ifp->if_softc;
254 struct mbuf *m;
255 size_t plen = ETHER_MAX_LEN_JUMBO+1;
256 size_t n;
257 int error;
258
259 for (;;) {
260 m = m_gethdr(M_WAIT, MT_DATA);
261 MEXTMALLOC(m, plen, M_WAIT);
262
263 again:
264 if (sc->sc_dying) {
265 m_freem(m);
266 break;
267 }
268
269 error = rumpcomp_virtif_recv(sc->sc_viu,
270 mtod(m, void *), plen, &n);
271 if (error) {
272 printf("%s: read hypercall failed %d. host if down?\n",
273 ifp->if_xname, error);
274 mutex_enter(&sc->sc_mtx);
275 /* could check if need go, done soon anyway */
276 cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz);
277 mutex_exit(&sc->sc_mtx);
278 goto again;
279 }
280
281 /* tap sometimes returns EOF. don't sweat it and plow on */
282 if (__predict_false(n == 0))
283 goto again;
284
285 /* discard if we're not up */
286 if ((ifp->if_flags & IFF_RUNNING) == 0)
287 goto again;
288
289 m->m_len = m->m_pkthdr.len = n;
290 m->m_pkthdr.rcvif = ifp;
291 bpf_mtap(ifp, m);
292 ether_input(ifp, m);
293 }
294
295 kthread_exit(0);
296 }
297
298 /* lazy bum stetson-harrison magic value */
299 #define LB_SH 32
300 static void
301 virtif_sender(void *arg)
302 {
303 struct ifnet *ifp = arg;
304 struct virtif_sc *sc = ifp->if_softc;
305 struct mbuf *m, *m0;
306 struct iovec io[LB_SH];
307 int i;
308
309 mutex_enter(&sc->sc_mtx);
310 KERNEL_LOCK(1, NULL);
311 while (!sc->sc_dying) {
312 if (!(ifp->if_flags & IFF_RUNNING)) {
313 cv_wait(&sc->sc_cv, &sc->sc_mtx);
314 continue;
315 }
316 IF_DEQUEUE(&ifp->if_snd, m0);
317 if (!m0) {
318 ifp->if_flags &= ~IFF_OACTIVE;
319 cv_wait(&sc->sc_cv, &sc->sc_mtx);
320 continue;
321 }
322 mutex_exit(&sc->sc_mtx);
323
324 m = m0;
325 for (i = 0; i < LB_SH && m; i++) {
326 io[i].iov_base = mtod(m, void *);
327 io[i].iov_len = m->m_len;
328 m = m->m_next;
329 }
330 if (i == LB_SH)
331 panic("lazy bum");
332 bpf_mtap(ifp, m0);
333
334 rumpcomp_virtif_send(sc->sc_viu, io, i);
335
336 m_freem(m0);
337 mutex_enter(&sc->sc_mtx);
338 }
339 KERNEL_UNLOCK_LAST(curlwp);
340
341 mutex_exit(&sc->sc_mtx);
342
343 kthread_exit(0);
344 }
345
346 /*
347 * dummyif is a nada-interface.
348 * As it requires nothing external, it can be used for testing
349 * interface configuration.
350 */
351 static int dummyif_init(struct ifnet *);
352 static void dummyif_start(struct ifnet *);
353
354 void
355 rump_dummyif_create()
356 {
357 struct ifnet *ifp;
358 struct ethercom *ec;
359 uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
360
361 enaddr[2] = cprng_fast32() & 0xff;
362 enaddr[5] = cprng_fast32() & 0xff;
363
364 ec = kmem_zalloc(sizeof(*ec), KM_SLEEP);
365
366 ifp = &ec->ec_if;
367 strlcpy(ifp->if_xname, "dummy0", sizeof(ifp->if_xname));
368 ifp->if_softc = ifp;
369 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
370 ifp->if_init = dummyif_init;
371 ifp->if_ioctl = virtif_ioctl;
372 ifp->if_start = dummyif_start;
373
374 if_attach(ifp);
375 ether_ifattach(ifp, enaddr);
376 }
377
378 static int
379 dummyif_init(struct ifnet *ifp)
380 {
381
382 ifp->if_flags |= IFF_RUNNING;
383 return 0;
384 }
385
386 static void
387 dummyif_start(struct ifnet *ifp)
388 {
389
390 }
391