if_virt.c revision 1.19 1 /* $NetBSD: if_virt.c,v 1.19 2010/08/10 18:06:10 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.19 2010/08/10 18:06:10 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: %d\n",
114 num, error);
115 return error;
116 }
117 KASSERT(num < 0x100);
118 enaddr[2] = arc4random() & 0xff;
119 enaddr[5] = num;
120
121 sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
122 sc->sc_tapfd = fd;
123
124 ifp = &sc->sc_ec.ec_if;
125 sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, num);
126 ifp->if_softc = sc;
127 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
128 ifp->if_init = virtif_init;
129 ifp->if_ioctl = virtif_ioctl;
130 ifp->if_start = virtif_start;
131 ifp->if_stop = virtif_stop;
132
133 mutex_init(&sc->sc_sendmtx, MUTEX_DEFAULT, IPL_NONE);
134 cv_init(&sc->sc_sendcv, "virtsnd");
135
136 if_attach(ifp);
137 ether_ifattach(ifp, enaddr);
138
139 return 0;
140 }
141
142 static int
143 virtif_init(struct ifnet *ifp)
144 {
145 int rv;
146
147 if (rump_threads) {
148 rv = kthread_create(PRI_NONE, 0, NULL, virtif_worker, ifp,
149 NULL, "virtifi");
150 /* XXX: should do proper cleanup */
151 if (rv) {
152 panic("if_virt: can't create worker");
153 }
154 rv = kthread_create(PRI_NONE, 0, NULL, virtif_sender, ifp,
155 NULL, "virtifs");
156 if (rv) {
157 panic("if_virt: can't create sender");
158 }
159 } else {
160 printf("WARNING: threads not enabled, receive NOT working\n");
161 }
162 ifp->if_flags |= IFF_RUNNING;
163
164 return 0;
165 }
166
167 static int
168 virtif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
169 {
170 int s, rv;
171
172 s = splnet();
173 rv = ether_ioctl(ifp, cmd, data);
174 if (rv == ENETRESET)
175 rv = 0;
176 splx(s);
177
178 return rv;
179 }
180
181 /* just send everything in-context */
182 static void
183 virtif_start(struct ifnet *ifp)
184 {
185 struct virtif_sc *sc = ifp->if_softc;
186
187 mutex_enter(&sc->sc_sendmtx);
188 cv_signal(&sc->sc_sendcv);
189 mutex_exit(&sc->sc_sendmtx);
190 }
191
192 static void
193 virtif_stop(struct ifnet *ifp, int disable)
194 {
195
196 panic("%s: unimpl", __func__);
197 }
198
199 static void
200 virtif_worker(void *arg)
201 {
202 struct ifnet *ifp = arg;
203 struct virtif_sc *sc = ifp->if_softc;
204 struct mbuf *m;
205 size_t plen = ETHER_MAX_LEN_JUMBO+1;
206 ssize_t n;
207 int error;
208
209 for (;;) {
210 m = m_gethdr(M_WAIT, MT_DATA);
211 MEXTMALLOC(m, plen, M_WAIT);
212
213 again:
214 n = rumpuser_read(sc->sc_tapfd, mtod(m, void *), plen, &error);
215 KASSERT(n < ETHER_MAX_LEN_JUMBO);
216 if (__predict_false(n < 0)) {
217 /*
218 * work around tap bug: /dev/tap is opened in
219 * non-blocking mode if it previously was
220 * non-blocking.
221 */
222 if (n == -1 && error == EAGAIN) {
223 struct pollfd pfd;
224
225 pfd.fd = sc->sc_tapfd;
226 pfd.events = POLLIN;
227
228 rumpuser_poll(&pfd, 1, INFTIM, &error);
229 goto again;
230 }
231
232 m_freem(m);
233 break;
234 }
235
236 /* tap sometimes returns EOF. don't sweat it and plow on */
237 if (__predict_false(n == 0))
238 goto again;
239
240 m->m_len = m->m_pkthdr.len = n;
241 m->m_pkthdr.rcvif = ifp;
242 bpf_mtap(ifp, m);
243 ether_input(ifp, m);
244 }
245
246 panic("virtif_workin is a lazy boy %d\n", error);
247 }
248
249 /* lazy bum stetson-harrison magic value */
250 #define LB_SH 32
251 static void
252 virtif_sender(void *arg)
253 {
254 struct ifnet *ifp = arg;
255 struct virtif_sc *sc = ifp->if_softc;
256 struct mbuf *m, *m0;
257 struct rumpuser_iovec io[LB_SH];
258 int i, error;
259
260 mutex_enter(&sc->sc_sendmtx);
261 for (;;) {
262 IF_DEQUEUE(&ifp->if_snd, m0);
263 if (!m0) {
264 cv_wait(&sc->sc_sendcv, &sc->sc_sendmtx);
265 continue;
266 }
267 mutex_exit(&sc->sc_sendmtx);
268
269 m = m0;
270 for (i = 0; i < LB_SH && m; i++) {
271 io[i].iov_base = mtod(m, void *);
272 io[i].iov_len = m->m_len;
273 m = m->m_next;
274 }
275 if (i == LB_SH)
276 panic("lazy bum");
277 bpf_mtap(ifp, m0);
278 rumpuser_writev(sc->sc_tapfd, io, i, &error);
279 m_freem(m0);
280 mutex_enter(&sc->sc_sendmtx);
281 }
282
283 mutex_exit(softnet_lock);
284 }
285
286 /*
287 * dummyif is a nada-interface.
288 * As it requires nothing external, it can be used for testing
289 * interface configuration.
290 */
291 static int dummyif_init(struct ifnet *);
292 static void dummyif_start(struct ifnet *);
293
294 void
295 rump_dummyif_create()
296 {
297 struct ifnet *ifp;
298 struct ethercom *ec;
299 uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
300
301 enaddr[2] = arc4random() & 0xff;
302 enaddr[5] = arc4random() & 0xff;
303
304 ec = kmem_zalloc(sizeof(*ec), KM_SLEEP);
305
306 ifp = &ec->ec_if;
307 strlcpy(ifp->if_xname, "dummy0", sizeof(ifp->if_xname));
308 ifp->if_softc = ifp;
309 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
310 ifp->if_init = dummyif_init;
311 ifp->if_ioctl = virtif_ioctl;
312 ifp->if_start = dummyif_start;
313
314 if_attach(ifp);
315 ether_ifattach(ifp, enaddr);
316 }
317
318 static int
319 dummyif_init(struct ifnet *ifp)
320 {
321
322 ifp->if_flags |= IFF_RUNNING;
323 return 0;
324 }
325
326 static void
327 dummyif_start(struct ifnet *ifp)
328 {
329
330 }
331