| @@ -1,255 +1,302 @@ | | | @@ -1,255 +1,302 @@ |
1 | /* $NetBSD: if_virt.c,v 1.9 2009/05/26 19:03:05 pooka Exp $ */ | | 1 | /* $NetBSD: if_virt.c,v 1.10 2009/05/27 23:41:20 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 | * |
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | | 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
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.9 2009/05/26 19:03:05 pooka Exp $"); | | 29 | __KERNEL_RCSID(0, "$NetBSD: if_virt.c,v 1.10 2009/05/27 23:41:20 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/sockio.h> | | 37 | #include <sys/sockio.h> |
38 | #include <sys/socketvar.h> | | 38 | #include <sys/socketvar.h> |
39 | | | 39 | |
40 | #include <net/if.h> | | 40 | #include <net/if.h> |
41 | #include <net/if_ether.h> | | 41 | #include <net/if_ether.h> |
42 | #include <net/if_tap.h> | | 42 | #include <net/if_tap.h> |
43 | | | 43 | |
44 | #include <netinet/in.h> | | 44 | #include <netinet/in.h> |
45 | #include <netinet/in_var.h> | | 45 | #include <netinet/in_var.h> |
46 | | | 46 | |
47 | #include <rump/rump.h> | | 47 | #include <rump/rump.h> |
48 | #include <rump/rumpuser.h> | | 48 | #include <rump/rumpuser.h> |
49 | | | 49 | |
50 | #include "rump_private.h" | | 50 | #include "rump_private.h" |
| | | 51 | #include "rump_net_private.h" |
51 | | | 52 | |
52 | /* | | 53 | /* |
53 | * Virtual interface for userspace purposes. Uses tap(4) to | | 54 | * Virtual interface for userspace purposes. Uses tap(4) to |
54 | * interface with the kernel and just simply shovels data | | 55 | * interface with the kernel and just simply shovels data |
55 | * to/from /dev/tap. | | 56 | * to/from /dev/tap. |
56 | */ | | 57 | */ |
57 | | | 58 | |
58 | #define VIRTIF_BASE "virt" | | 59 | #define VIRTIF_BASE "virt" |
59 | | | 60 | |
60 | static int virtif_init(struct ifnet *); | | 61 | static int virtif_init(struct ifnet *); |
61 | static int virtif_ioctl(struct ifnet *, u_long, void *); | | 62 | static int virtif_ioctl(struct ifnet *, u_long, void *); |
62 | static void virtif_start(struct ifnet *); | | 63 | static void virtif_start(struct ifnet *); |
63 | static void virtif_stop(struct ifnet *, int); | | 64 | static void virtif_stop(struct ifnet *, int); |
64 | | | 65 | |
65 | struct virtif_sc { | | 66 | struct virtif_sc { |
66 | struct ethercom sc_ec; | | 67 | struct ethercom sc_ec; |
67 | int sc_tapfd; | | 68 | int sc_tapfd; |
68 | kmutex_t sc_sendmtx; | | 69 | kmutex_t sc_sendmtx; |
69 | kcondvar_t sc_sendcv; | | 70 | kcondvar_t sc_sendcv; |
70 | }; | | 71 | }; |
71 | | | 72 | |
72 | static void virtif_worker(void *); | | 73 | static void virtif_worker(void *); |
73 | static void virtif_sender(void *); | | 74 | static void virtif_sender(void *); |
74 | | | 75 | |
75 | #if 0 | | 76 | #if 0 |
76 | /* | | 77 | /* |
77 | * Create a socket and call ifioctl() to configure the interface. | | 78 | * Create a socket and call ifioctl() to configure the interface. |
78 | * This trickles down to virtif_ioctl(). | | 79 | * This trickles down to virtif_ioctl(). |
79 | */ | | 80 | */ |
80 | static int | | 81 | static int |
81 | configaddr(struct ifnet *ifp, struct ifaliasreq *ia) | | 82 | configaddr(struct ifnet *ifp, struct ifaliasreq *ia) |
82 | { | | 83 | { |
83 | struct socket *so; | | 84 | struct socket *so; |
84 | int error; | | 85 | int error; |
85 | | | 86 | |
86 | strcpy(ia->ifra_name, ifp->if_xname); | | 87 | strcpy(ia->ifra_name, ifp->if_xname); |
87 | error = socreate(ia->ifra_addr.sa_family, &so, SOCK_DGRAM, | | 88 | error = socreate(ia->ifra_addr.sa_family, &so, SOCK_DGRAM, |
88 | 0, curlwp, NULL); | | 89 | 0, curlwp, NULL); |
89 | if (error) | | 90 | if (error) |
90 | return error; | | 91 | return error; |
91 | error = ifioctl(so, SIOCAIFADDR, ia, curlwp); | | 92 | error = ifioctl(so, SIOCAIFADDR, ia, curlwp); |
92 | soclose(so); | | 93 | soclose(so); |
93 | | | 94 | |
94 | return error; | | 95 | return error; |
95 | } | | 96 | } |
96 | #endif | | 97 | #endif |
97 | | | 98 | |
98 | int | | 99 | int |
99 | rump_virtif_create(int num) | | 100 | rump_virtif_create(int num) |
100 | { | | 101 | { |
101 | struct virtif_sc *sc; | | 102 | struct virtif_sc *sc; |
102 | struct ifnet *ifp; | | 103 | struct ifnet *ifp; |
103 | uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 }; | | 104 | uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 }; |
104 | char tapdev[16]; | | 105 | char tapdev[16]; |
105 | int fd, error; | | 106 | int fd, error; |
106 | | | 107 | |
107 | snprintf(tapdev, sizeof(tapdev), "/dev/tap%d", num); | | 108 | snprintf(tapdev, sizeof(tapdev), "/dev/tap%d", num); |
108 | fd = rumpuser_open(tapdev, O_RDWR, &error); | | 109 | fd = rumpuser_open(tapdev, O_RDWR, &error); |
109 | if (fd == -1) { | | 110 | if (fd == -1) { |
110 | printf("virtif_create: can't open /dev/tap %d\n", error); | | 111 | printf("virtif_create: can't open /dev/tap %d\n", error); |
111 | return error; | | 112 | return error; |
112 | } | | 113 | } |
113 | KASSERT(num < 0x100); | | 114 | KASSERT(num < 0x100); |
114 | enaddr[2] = arc4random() & 0xff; | | 115 | enaddr[2] = arc4random() & 0xff; |
115 | enaddr[5] = num; | | 116 | enaddr[5] = num; |
116 | | | 117 | |
117 | sc = kmem_zalloc(sizeof(*sc), KM_SLEEP); | | 118 | sc = kmem_zalloc(sizeof(*sc), KM_SLEEP); |
118 | sc->sc_tapfd = fd; | | 119 | sc->sc_tapfd = fd; |
119 | | | 120 | |
120 | ifp = &sc->sc_ec.ec_if; | | 121 | ifp = &sc->sc_ec.ec_if; |
121 | sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, num); | | 122 | sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, num); |
122 | ifp->if_softc = sc; | | 123 | ifp->if_softc = sc; |
123 | ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; | | 124 | ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; |
124 | ifp->if_init = virtif_init; | | 125 | ifp->if_init = virtif_init; |
125 | ifp->if_ioctl = virtif_ioctl; | | 126 | ifp->if_ioctl = virtif_ioctl; |
126 | ifp->if_start = virtif_start; | | 127 | ifp->if_start = virtif_start; |
127 | ifp->if_stop = virtif_stop; | | 128 | ifp->if_stop = virtif_stop; |
128 | | | 129 | |
129 | mutex_init(&sc->sc_sendmtx, MUTEX_DEFAULT, IPL_NONE); | | 130 | mutex_init(&sc->sc_sendmtx, MUTEX_DEFAULT, IPL_NONE); |
130 | cv_init(&sc->sc_sendcv, "virtsnd"); | | 131 | cv_init(&sc->sc_sendcv, "virtsnd"); |
131 | | | 132 | |
132 | if_attach(ifp); | | 133 | if_attach(ifp); |
133 | ether_ifattach(ifp, enaddr); | | 134 | ether_ifattach(ifp, enaddr); |
134 | | | 135 | |
135 | return 0; | | 136 | return 0; |
136 | } | | 137 | } |
137 | | | 138 | |
138 | static int | | 139 | static int |
139 | virtif_init(struct ifnet *ifp) | | 140 | virtif_init(struct ifnet *ifp) |
140 | { | | 141 | { |
141 | int rv; | | 142 | int rv; |
142 | | | 143 | |
143 | if (rump_threads) { | | 144 | if (rump_threads) { |
144 | rv = kthread_create(PRI_NONE, 0, NULL, virtif_worker, ifp, | | 145 | rv = kthread_create(PRI_NONE, 0, NULL, virtif_worker, ifp, |
145 | NULL, "virtifi"); | | 146 | NULL, "virtifi"); |
146 | /* XXX: should do proper cleanup */ | | 147 | /* XXX: should do proper cleanup */ |
147 | if (rv) { | | 148 | if (rv) { |
148 | panic("if_virt: can't create worker"); | | 149 | panic("if_virt: can't create worker"); |
149 | } | | 150 | } |
150 | rv = kthread_create(PRI_NONE, 0, NULL, virtif_sender, ifp, | | 151 | rv = kthread_create(PRI_NONE, 0, NULL, virtif_sender, ifp, |
151 | NULL, "virtifs"); | | 152 | NULL, "virtifs"); |
152 | if (rv) { | | 153 | if (rv) { |
153 | panic("if_virt: can't create sender"); | | 154 | panic("if_virt: can't create sender"); |
154 | } | | 155 | } |
155 | } else { | | 156 | } else { |
156 | printf("WARNING: threads not enabled, receive NOT working\n"); | | 157 | printf("WARNING: threads not enabled, receive NOT working\n"); |
157 | } | | 158 | } |
158 | ifp->if_flags |= IFF_RUNNING; | | 159 | ifp->if_flags |= IFF_RUNNING; |
159 | | | 160 | |
160 | return 0; | | 161 | return 0; |
161 | } | | 162 | } |
162 | | | 163 | |
163 | static int | | 164 | static int |
164 | virtif_ioctl(struct ifnet *ifp, u_long cmd, void *data) | | 165 | virtif_ioctl(struct ifnet *ifp, u_long cmd, void *data) |
165 | { | | 166 | { |
166 | int s, rv; | | 167 | int s, rv; |
167 | | | 168 | |
168 | s = splnet(); | | 169 | s = splnet(); |
169 | rv = ether_ioctl(ifp, cmd, data); | | 170 | rv = ether_ioctl(ifp, cmd, data); |
170 | if (rv == ENETRESET) | | 171 | if (rv == ENETRESET) |
171 | rv = 0; | | 172 | rv = 0; |
172 | splx(s); | | 173 | splx(s); |
173 | | | 174 | |
174 | return rv; | | 175 | return rv; |
175 | } | | 176 | } |
176 | | | 177 | |
177 | /* just send everything in-context */ | | 178 | /* just send everything in-context */ |
178 | static void | | 179 | static void |
179 | virtif_start(struct ifnet *ifp) | | 180 | virtif_start(struct ifnet *ifp) |
180 | { | | 181 | { |
181 | struct virtif_sc *sc = ifp->if_softc; | | 182 | struct virtif_sc *sc = ifp->if_softc; |
182 | | | 183 | |
183 | mutex_enter(&sc->sc_sendmtx); | | 184 | mutex_enter(&sc->sc_sendmtx); |
184 | cv_signal(&sc->sc_sendcv); | | 185 | cv_signal(&sc->sc_sendcv); |
185 | mutex_exit(&sc->sc_sendmtx); | | 186 | mutex_exit(&sc->sc_sendmtx); |
186 | } | | 187 | } |
187 | | | 188 | |
188 | static void | | 189 | static void |
189 | virtif_stop(struct ifnet *ifp, int disable) | | 190 | virtif_stop(struct ifnet *ifp, int disable) |
190 | { | | 191 | { |
191 | | | 192 | |
192 | panic("%s: unimpl", __func__); | | 193 | panic("%s: unimpl", __func__); |
193 | } | | 194 | } |
194 | | | 195 | |
195 | static void | | 196 | static void |
196 | virtif_worker(void *arg) | | 197 | virtif_worker(void *arg) |
197 | { | | 198 | { |
198 | struct ifnet *ifp = arg; | | 199 | struct ifnet *ifp = arg; |
199 | struct virtif_sc *sc = ifp->if_softc; | | 200 | struct virtif_sc *sc = ifp->if_softc; |
200 | struct mbuf *m; | | 201 | struct mbuf *m; |
201 | size_t plen = ETHER_MAX_LEN_JUMBO+1; | | 202 | size_t plen = ETHER_MAX_LEN_JUMBO+1; |
202 | ssize_t n; | | 203 | ssize_t n; |
203 | int error; | | 204 | int error; |
204 | | | 205 | |
205 | for (;;) { | | 206 | for (;;) { |
206 | m = m_gethdr(M_WAIT, MT_DATA); | | 207 | m = m_gethdr(M_WAIT, MT_DATA); |
207 | MEXTMALLOC(m, plen, M_WAIT); | | 208 | MEXTMALLOC(m, plen, M_WAIT); |
208 | | | 209 | |
209 | n = rumpuser_read(sc->sc_tapfd, mtod(m, void *), plen, &error); | | 210 | n = rumpuser_read(sc->sc_tapfd, mtod(m, void *), plen, &error); |
210 | KASSERT(n < ETHER_MAX_LEN_JUMBO); | | 211 | KASSERT(n < ETHER_MAX_LEN_JUMBO); |
211 | if (n <= 0) { | | 212 | if (n <= 0) { |
212 | m_freem(m); | | 213 | m_freem(m); |
213 | break; | | 214 | break; |
214 | } | | 215 | } |
215 | m->m_len = m->m_pkthdr.len = n; | | 216 | m->m_len = m->m_pkthdr.len = n; |
216 | m->m_pkthdr.rcvif = ifp; | | 217 | m->m_pkthdr.rcvif = ifp; |
217 | ether_input(ifp, m); | | 218 | ether_input(ifp, m); |
218 | } | | 219 | } |
219 | | | 220 | |
220 | panic("virtif_workin is a lazy boy %d\n", error); | | 221 | panic("virtif_workin is a lazy boy %d\n", error); |
221 | } | | 222 | } |
222 | | | 223 | |
223 | static void | | 224 | static void |
224 | virtif_sender(void *arg) | | 225 | virtif_sender(void *arg) |
225 | { | | 226 | { |
226 | struct ifnet *ifp = arg; | | 227 | struct ifnet *ifp = arg; |
227 | struct virtif_sc *sc = ifp->if_softc; | | 228 | struct virtif_sc *sc = ifp->if_softc; |
228 | struct mbuf *m, *m0; | | 229 | struct mbuf *m, *m0; |
229 | struct rumpuser_iovec io[16]; | | 230 | struct rumpuser_iovec io[16]; |
230 | int i, error; | | 231 | int i, error; |
231 | | | 232 | |
232 | mutex_enter(&sc->sc_sendmtx); | | 233 | mutex_enter(&sc->sc_sendmtx); |
233 | for (;;) { | | 234 | for (;;) { |
234 | IF_DEQUEUE(&ifp->if_snd, m0); | | 235 | IF_DEQUEUE(&ifp->if_snd, m0); |
235 | if (!m0) { | | 236 | if (!m0) { |
236 | cv_wait(&sc->sc_sendcv, &sc->sc_sendmtx); | | 237 | cv_wait(&sc->sc_sendcv, &sc->sc_sendmtx); |
237 | continue; | | 238 | continue; |
238 | } | | 239 | } |
239 | mutex_exit(&sc->sc_sendmtx); | | 240 | mutex_exit(&sc->sc_sendmtx); |
240 | | | 241 | |
241 | m = m0; | | 242 | m = m0; |
242 | for (i = 0; i < 16 && m; i++) { | | 243 | for (i = 0; i < 16 && m; i++) { |
243 | io[i].iov_base = mtod(m, void *); | | 244 | io[i].iov_base = mtod(m, void *); |
244 | io[i].iov_len = m->m_len; | | 245 | io[i].iov_len = m->m_len; |
245 | m = m->m_next; | | 246 | m = m->m_next; |
246 | } | | 247 | } |
247 | if (i == 16) | | 248 | if (i == 16) |
248 | panic("lazy bum"); | | 249 | panic("lazy bum"); |
249 | rumpuser_writev(sc->sc_tapfd, io, i, &error); | | 250 | rumpuser_writev(sc->sc_tapfd, io, i, &error); |
250 | m_freem(m0); | | 251 | m_freem(m0); |
251 | mutex_enter(&sc->sc_sendmtx); | | 252 | mutex_enter(&sc->sc_sendmtx); |
252 | } | | 253 | } |
253 | | | 254 | |
254 | mutex_exit(softnet_lock); | | 255 | mutex_exit(softnet_lock); |
255 | } | | 256 | } |
| | | 257 | |
| | | 258 | /* |
| | | 259 | * dummyif is a nada-interface. |
| | | 260 | * As it requires nothing external, it can be used for testing |
| | | 261 | * interface configuration. |
| | | 262 | */ |
| | | 263 | static int dummyif_init(struct ifnet *); |
| | | 264 | static void dummyif_start(struct ifnet *); |
| | | 265 | |
| | | 266 | void |
| | | 267 | rump_dummyif_create() |
| | | 268 | { |
| | | 269 | struct ifnet *ifp; |
| | | 270 | struct ethercom *ec; |
| | | 271 | uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 }; |
| | | 272 | |
| | | 273 | enaddr[2] = arc4random() & 0xff; |
| | | 274 | enaddr[5] = arc4random() & 0xff; |
| | | 275 | |
| | | 276 | ec = kmem_zalloc(sizeof(*ec), KM_SLEEP); |
| | | 277 | |
| | | 278 | ifp = &ec->ec_if; |
| | | 279 | strlcpy(ifp->if_xname, "dummy0", sizeof(ifp->if_xname)); |
| | | 280 | ifp->if_softc = ifp; |
| | | 281 | ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; |
| | | 282 | ifp->if_init = dummyif_init; |
| | | 283 | ifp->if_ioctl = virtif_ioctl; |
| | | 284 | ifp->if_start = dummyif_start; |
| | | 285 | |
| | | 286 | if_attach(ifp); |
| | | 287 | ether_ifattach(ifp, enaddr); |
| | | 288 | } |
| | | 289 | |
| | | 290 | static int |
| | | 291 | dummyif_init(struct ifnet *ifp) |
| | | 292 | { |
| | | 293 | |
| | | 294 | ifp->if_flags |= IFF_RUNNING; |
| | | 295 | return 0; |
| | | 296 | } |
| | | 297 | |
| | | 298 | static void |
| | | 299 | dummyif_start(struct ifnet *ifp) |
| | | 300 | { |
| | | 301 | |
| | | 302 | } |