| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: if_virt.c,v 1.18 2010/04/05 16:35:30 joerg Exp $ */ | | 1 | /* $NetBSD: if_virt.c,v 1.19 2010/08/10 18:06:10 pooka Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 2008 Antti Kantee. All Rights Reserved. | | 4 | * Copyright (c) 2008 Antti Kantee. All Rights Reserved. |
5 | * | | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | | 6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions | | 7 | * modification, are permitted provided that the following conditions |
8 | * are met: | | 8 | * are met: |
9 | * 1. Redistributions of source code must retain the above copyright | | 9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. | | 10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above copyright | | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the | | 12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. | | 13 | * documentation and/or other materials provided with the distribution. |
14 | * | | 14 | * |
| @@ -16,27 +16,27 @@ | | | @@ -16,27 +16,27 @@ |
16 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | | 16 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | | 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
18 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | | 18 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | | 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
21 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 21 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 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 | | 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 | | 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 | * SUCH DAMAGE. | | 25 | * SUCH DAMAGE. |
26 | */ | | 26 | */ |
27 | | | 27 | |
28 | #include <sys/cdefs.h> | | 28 | #include <sys/cdefs.h> |
29 | __KERNEL_RCSID(0, "$NetBSD: if_virt.c,v 1.18 2010/04/05 16:35:30 joerg Exp $"); | | 29 | __KERNEL_RCSID(0, "$NetBSD: if_virt.c,v 1.19 2010/08/10 18:06:10 pooka Exp $"); |
30 | | | 30 | |
31 | #include <sys/param.h> | | 31 | #include <sys/param.h> |
32 | #include <sys/condvar.h> | | 32 | #include <sys/condvar.h> |
33 | #include <sys/fcntl.h> | | 33 | #include <sys/fcntl.h> |
34 | #include <sys/kmem.h> | | 34 | #include <sys/kmem.h> |
35 | #include <sys/kthread.h> | | 35 | #include <sys/kthread.h> |
36 | #include <sys/mutex.h> | | 36 | #include <sys/mutex.h> |
37 | #include <sys/poll.h> | | 37 | #include <sys/poll.h> |
38 | #include <sys/sockio.h> | | 38 | #include <sys/sockio.h> |
39 | #include <sys/socketvar.h> | | 39 | #include <sys/socketvar.h> |
40 | | | 40 | |
41 | #include <net/bpf.h> | | 41 | #include <net/bpf.h> |
42 | #include <net/if.h> | | 42 | #include <net/if.h> |
| @@ -100,27 +100,28 @@ configaddr(struct ifnet *ifp, struct ifa | | | @@ -100,27 +100,28 @@ configaddr(struct ifnet *ifp, struct ifa |
100 | | | 100 | |
101 | int | | 101 | int |
102 | rump_virtif_create(int num) | | 102 | rump_virtif_create(int num) |
103 | { | | 103 | { |
104 | struct virtif_sc *sc; | | 104 | struct virtif_sc *sc; |
105 | struct ifnet *ifp; | | 105 | struct ifnet *ifp; |
106 | uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 }; | | 106 | uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 }; |
107 | char tapdev[16]; | | 107 | char tapdev[16]; |
108 | int fd, error; | | 108 | int fd, error; |
109 | | | 109 | |
110 | snprintf(tapdev, sizeof(tapdev), "/dev/tap%d", num); | | 110 | snprintf(tapdev, sizeof(tapdev), "/dev/tap%d", num); |
111 | fd = rumpuser_open(tapdev, O_RDWR, &error); | | 111 | fd = rumpuser_open(tapdev, O_RDWR, &error); |
112 | if (fd == -1) { | | 112 | if (fd == -1) { |
113 | printf("virtif_create: can't open /dev/tap %d\n", error); | | 113 | printf("virtif_create: can't open /dev/tap%d: %d\n", |
| | | 114 | num, error); |
114 | return error; | | 115 | return error; |
115 | } | | 116 | } |
116 | KASSERT(num < 0x100); | | 117 | KASSERT(num < 0x100); |
117 | enaddr[2] = arc4random() & 0xff; | | 118 | enaddr[2] = arc4random() & 0xff; |
118 | enaddr[5] = num; | | 119 | enaddr[5] = num; |
119 | | | 120 | |
120 | sc = kmem_zalloc(sizeof(*sc), KM_SLEEP); | | 121 | sc = kmem_zalloc(sizeof(*sc), KM_SLEEP); |
121 | sc->sc_tapfd = fd; | | 122 | sc->sc_tapfd = fd; |
122 | | | 123 | |
123 | ifp = &sc->sc_ec.ec_if; | | 124 | ifp = &sc->sc_ec.ec_if; |
124 | sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, num); | | 125 | sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, num); |
125 | ifp->if_softc = sc; | | 126 | ifp->if_softc = sc; |
126 | ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; | | 127 | ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; |
| @@ -202,44 +203,50 @@ virtif_worker(void *arg) | | | @@ -202,44 +203,50 @@ virtif_worker(void *arg) |
202 | struct virtif_sc *sc = ifp->if_softc; | | 203 | struct virtif_sc *sc = ifp->if_softc; |
203 | struct mbuf *m; | | 204 | struct mbuf *m; |
204 | size_t plen = ETHER_MAX_LEN_JUMBO+1; | | 205 | size_t plen = ETHER_MAX_LEN_JUMBO+1; |
205 | ssize_t n; | | 206 | ssize_t n; |
206 | int error; | | 207 | int error; |
207 | | | 208 | |
208 | for (;;) { | | 209 | for (;;) { |
209 | m = m_gethdr(M_WAIT, MT_DATA); | | 210 | m = m_gethdr(M_WAIT, MT_DATA); |
210 | MEXTMALLOC(m, plen, M_WAIT); | | 211 | MEXTMALLOC(m, plen, M_WAIT); |
211 | | | 212 | |
212 | again: | | 213 | again: |
213 | n = rumpuser_read(sc->sc_tapfd, mtod(m, void *), plen, &error); | | 214 | n = rumpuser_read(sc->sc_tapfd, mtod(m, void *), plen, &error); |
214 | KASSERT(n < ETHER_MAX_LEN_JUMBO); | | 215 | KASSERT(n < ETHER_MAX_LEN_JUMBO); |
215 | if (n <= 0) { | | 216 | if (__predict_false(n < 0)) { |
216 | /* | | 217 | /* |
217 | * work around tap bug: /dev/tap is opened in | | 218 | * work around tap bug: /dev/tap is opened in |
218 | * non-blocking mode if it previously was | | 219 | * non-blocking mode if it previously was |
219 | * non-blocking. | | 220 | * non-blocking. |
220 | */ | | 221 | */ |
221 | if (n == -1 && error == EAGAIN) { | | 222 | if (n == -1 && error == EAGAIN) { |
222 | struct pollfd pfd; | | 223 | struct pollfd pfd; |
223 | | | 224 | |
224 | pfd.fd = sc->sc_tapfd; | | 225 | pfd.fd = sc->sc_tapfd; |
225 | pfd.events = POLLIN; | | 226 | pfd.events = POLLIN; |
226 | | | 227 | |
227 | rumpuser_poll(&pfd, 1, INFTIM, &error); | | 228 | rumpuser_poll(&pfd, 1, INFTIM, &error); |
228 | goto again; | | 229 | goto again; |
229 | } | | 230 | } |
| | | 231 | |
230 | m_freem(m); | | 232 | m_freem(m); |
231 | break; | | 233 | break; |
232 | } | | 234 | } |
| | | 235 | |
| | | 236 | /* tap sometimes returns EOF. don't sweat it and plow on */ |
| | | 237 | if (__predict_false(n == 0)) |
| | | 238 | goto again; |
| | | 239 | |
233 | m->m_len = m->m_pkthdr.len = n; | | 240 | m->m_len = m->m_pkthdr.len = n; |
234 | m->m_pkthdr.rcvif = ifp; | | 241 | m->m_pkthdr.rcvif = ifp; |
235 | bpf_mtap(ifp, m); | | 242 | bpf_mtap(ifp, m); |
236 | ether_input(ifp, m); | | 243 | ether_input(ifp, m); |
237 | } | | 244 | } |
238 | | | 245 | |
239 | panic("virtif_workin is a lazy boy %d\n", error); | | 246 | panic("virtif_workin is a lazy boy %d\n", error); |
240 | } | | 247 | } |
241 | | | 248 | |
242 | /* lazy bum stetson-harrison magic value */ | | 249 | /* lazy bum stetson-harrison magic value */ |
243 | #define LB_SH 32 | | 250 | #define LB_SH 32 |
244 | static void | | 251 | static void |
245 | virtif_sender(void *arg) | | 252 | virtif_sender(void *arg) |