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