if_shmem.c revision 1.10.4.1 1 1.10.4.1 rmind /* $NetBSD: if_shmem.c,v 1.10.4.1 2011/03/05 20:56:21 rmind Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2009 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Development of this software was supported by The Nokia Foundation.
7 1.1 pooka *
8 1.1 pooka * Redistribution and use in source and binary forms, with or without
9 1.1 pooka * modification, are permitted provided that the following conditions
10 1.1 pooka * are met:
11 1.1 pooka * 1. Redistributions of source code must retain the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer.
13 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 pooka * notice, this list of conditions and the following disclaimer in the
15 1.1 pooka * documentation and/or other materials provided with the distribution.
16 1.1 pooka *
17 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 pooka * SUCH DAMAGE.
28 1.1 pooka */
29 1.1 pooka
30 1.1 pooka #include <sys/cdefs.h>
31 1.10.4.1 rmind __KERNEL_RCSID(0, "$NetBSD: if_shmem.c,v 1.10.4.1 2011/03/05 20:56:21 rmind Exp $");
32 1.1 pooka
33 1.1 pooka #include <sys/param.h>
34 1.10.4.1 rmind #include <sys/atomic.h>
35 1.1 pooka #include <sys/fcntl.h>
36 1.1 pooka #include <sys/kmem.h>
37 1.1 pooka #include <sys/kthread.h>
38 1.1 pooka #include <sys/lock.h>
39 1.10.4.1 rmind #include <sys/vmem.h>
40 1.1 pooka
41 1.1 pooka #include <net/if.h>
42 1.1 pooka #include <net/if_ether.h>
43 1.1 pooka
44 1.1 pooka #include <netinet/in.h>
45 1.1 pooka #include <netinet/in_var.h>
46 1.1 pooka
47 1.1 pooka #include <rump/rump.h>
48 1.1 pooka #include <rump/rumpuser.h>
49 1.1 pooka
50 1.1 pooka #include "rump_private.h"
51 1.9 pooka #include "rump_net_private.h"
52 1.1 pooka
53 1.10.4.1 rmind static int shmif_clone(struct if_clone *, int);
54 1.10.4.1 rmind static int shmif_unclone(struct ifnet *);
55 1.10.4.1 rmind
56 1.10.4.1 rmind struct if_clone shmif_cloner =
57 1.10.4.1 rmind IF_CLONE_INITIALIZER("shmif", shmif_clone, shmif_unclone);
58 1.10.4.1 rmind
59 1.10.4.1 rmind /*
60 1.10.4.1 rmind * Do r/w prefault for backend pages when attaching the interface.
61 1.10.4.1 rmind * At least logically thinking improves performance (although no
62 1.10.4.1 rmind * mlocking is done, so they might go away).
63 1.10.4.1 rmind */
64 1.10.4.1 rmind #define PREFAULT_RW
65 1.6 pooka
66 1.1 pooka /*
67 1.1 pooka * A virtual ethernet interface which uses shared memory from a
68 1.1 pooka * memory mapped file as the bus.
69 1.1 pooka */
70 1.1 pooka
71 1.1 pooka static int shmif_init(struct ifnet *);
72 1.1 pooka static int shmif_ioctl(struct ifnet *, u_long, void *);
73 1.1 pooka static void shmif_start(struct ifnet *);
74 1.1 pooka static void shmif_stop(struct ifnet *, int);
75 1.1 pooka
76 1.10.4.1 rmind #include "shmifvar.h"
77 1.10.4.1 rmind
78 1.1 pooka struct shmif_sc {
79 1.1 pooka struct ethercom sc_ec;
80 1.1 pooka uint8_t sc_myaddr[6];
81 1.10.4.1 rmind struct shmif_mem *sc_busmem;
82 1.1 pooka int sc_memfd;
83 1.1 pooka int sc_kq;
84 1.10.4.1 rmind int sc_unit;
85 1.10.4.1 rmind
86 1.10.4.1 rmind char *sc_backfile;
87 1.10.4.1 rmind size_t sc_backfilelen;
88 1.1 pooka
89 1.10.4.1 rmind uint64_t sc_devgen;
90 1.1 pooka uint32_t sc_nextpacket;
91 1.1 pooka
92 1.10.4.1 rmind kmutex_t sc_mtx;
93 1.10.4.1 rmind kcondvar_t sc_cv;
94 1.10.4.1 rmind
95 1.10.4.1 rmind struct lwp *sc_rcvl;
96 1.10.4.1 rmind bool sc_dying;
97 1.10.4.1 rmind };
98 1.1 pooka
99 1.10.4.1 rmind static const uint32_t busversion = SHMIF_VERSION;
100 1.1 pooka
101 1.1 pooka static void shmif_rcv(void *);
102 1.1 pooka
103 1.10.4.1 rmind #define LOCK_UNLOCKED 0
104 1.10.4.1 rmind #define LOCK_LOCKED 1
105 1.10.4.1 rmind #define LOCK_COOLDOWN 1001
106 1.10.4.1 rmind
107 1.10.4.1 rmind vmem_t *shmif_units;
108 1.1 pooka
109 1.2 pooka /*
110 1.2 pooka * This locking needs work and will misbehave severely if:
111 1.2 pooka * 1) the backing memory has to be paged in
112 1.2 pooka * 2) some lockholder exits while holding the lock
113 1.2 pooka */
114 1.1 pooka static void
115 1.10.4.1 rmind shmif_lockbus(struct shmif_mem *busmem)
116 1.1 pooka {
117 1.10.4.1 rmind int i = 0;
118 1.1 pooka
119 1.10.4.1 rmind while (__predict_false(atomic_cas_32(&busmem->shm_lock,
120 1.10.4.1 rmind LOCK_UNLOCKED, LOCK_LOCKED) == LOCK_LOCKED)) {
121 1.10.4.1 rmind if (__predict_false(++i > LOCK_COOLDOWN)) {
122 1.10.4.1 rmind uint64_t sec, nsec;
123 1.10.4.1 rmind int error;
124 1.10.4.1 rmind
125 1.10.4.1 rmind sec = 0;
126 1.10.4.1 rmind nsec = 1000*1000; /* 1ms */
127 1.10.4.1 rmind rumpuser_nanosleep(&sec, &nsec, &error);
128 1.10.4.1 rmind i = 0;
129 1.10.4.1 rmind }
130 1.10.4.1 rmind continue;
131 1.10.4.1 rmind }
132 1.10.4.1 rmind membar_enter();
133 1.1 pooka }
134 1.1 pooka
135 1.1 pooka static void
136 1.10.4.1 rmind shmif_unlockbus(struct shmif_mem *busmem)
137 1.1 pooka {
138 1.10.4.1 rmind unsigned int old;
139 1.1 pooka
140 1.10.4.1 rmind membar_exit();
141 1.10.4.1 rmind old = atomic_swap_32(&busmem->shm_lock, LOCK_UNLOCKED);
142 1.10.4.1 rmind KASSERT(old == LOCK_LOCKED);
143 1.1 pooka }
144 1.1 pooka
145 1.10.4.1 rmind static int
146 1.10.4.1 rmind allocif(int unit, struct shmif_sc **scp)
147 1.1 pooka {
148 1.10.4.1 rmind uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0xa0, 0x00, 0x00, 0x00, 0x00 };
149 1.10.4.1 rmind struct shmif_sc *sc;
150 1.10.4.1 rmind struct ifnet *ifp;
151 1.10.4.1 rmind uint32_t randnum;
152 1.10.4.1 rmind int error;
153 1.1 pooka
154 1.10.4.1 rmind randnum = arc4random();
155 1.10.4.1 rmind memcpy(&enaddr[2], &randnum, sizeof(randnum));
156 1.1 pooka
157 1.10.4.1 rmind sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
158 1.10.4.1 rmind sc->sc_memfd = -1;
159 1.10.4.1 rmind sc->sc_unit = unit;
160 1.1 pooka
161 1.10.4.1 rmind ifp = &sc->sc_ec.ec_if;
162 1.10.4.1 rmind memcpy(sc->sc_myaddr, enaddr, sizeof(enaddr));
163 1.1 pooka
164 1.10.4.1 rmind sprintf(ifp->if_xname, "shmif%d", unit);
165 1.10.4.1 rmind ifp->if_softc = sc;
166 1.10.4.1 rmind ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
167 1.10.4.1 rmind ifp->if_init = shmif_init;
168 1.10.4.1 rmind ifp->if_ioctl = shmif_ioctl;
169 1.10.4.1 rmind ifp->if_start = shmif_start;
170 1.10.4.1 rmind ifp->if_stop = shmif_stop;
171 1.10.4.1 rmind ifp->if_mtu = ETHERMTU;
172 1.1 pooka
173 1.10.4.1 rmind mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
174 1.10.4.1 rmind cv_init(&sc->sc_cv, "shmifcv");
175 1.1 pooka
176 1.10.4.1 rmind if_attach(ifp);
177 1.10.4.1 rmind ether_ifattach(ifp, enaddr);
178 1.1 pooka
179 1.10.4.1 rmind aprint_verbose("shmif%d: Ethernet address %s\n",
180 1.10.4.1 rmind unit, ether_sprintf(enaddr));
181 1.1 pooka
182 1.10.4.1 rmind if (scp)
183 1.10.4.1 rmind *scp = sc;
184 1.1 pooka
185 1.10.4.1 rmind error = 0;
186 1.10.4.1 rmind if (rump_threads) {
187 1.10.4.1 rmind error = kthread_create(PRI_NONE,
188 1.10.4.1 rmind KTHREAD_MPSAFE | KTHREAD_JOINABLE, NULL,
189 1.10.4.1 rmind shmif_rcv, ifp, &sc->sc_rcvl, "shmif");
190 1.10.4.1 rmind } else {
191 1.10.4.1 rmind printf("WARNING: threads not enabled, shmif NOT working\n");
192 1.10.4.1 rmind }
193 1.1 pooka
194 1.10.4.1 rmind if (error) {
195 1.10.4.1 rmind shmif_unclone(ifp);
196 1.10.4.1 rmind }
197 1.1 pooka
198 1.10.4.1 rmind return error;
199 1.1 pooka }
200 1.1 pooka
201 1.10.4.1 rmind static int
202 1.10.4.1 rmind initbackend(struct shmif_sc *sc, int memfd)
203 1.1 pooka {
204 1.10.4.1 rmind volatile uint8_t v;
205 1.10.4.1 rmind volatile uint8_t *p;
206 1.10.4.1 rmind int error;
207 1.10.4.1 rmind
208 1.10.4.1 rmind sc->sc_busmem = rumpuser_filemmap(memfd, 0, BUSMEM_SIZE,
209 1.10.4.1 rmind RUMPUSER_FILEMMAP_TRUNCATE | RUMPUSER_FILEMMAP_SHARED
210 1.10.4.1 rmind | RUMPUSER_FILEMMAP_READ | RUMPUSER_FILEMMAP_WRITE, &error);
211 1.10.4.1 rmind if (error)
212 1.10.4.1 rmind return error;
213 1.10.4.1 rmind
214 1.10.4.1 rmind if (sc->sc_busmem->shm_magic
215 1.10.4.1 rmind && sc->sc_busmem->shm_magic != SHMIF_MAGIC) {
216 1.10.4.1 rmind printf("bus is not magical");
217 1.10.4.1 rmind rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE);
218 1.10.4.1 rmind return ENOEXEC;
219 1.10.4.1 rmind }
220 1.10.4.1 rmind
221 1.10.4.1 rmind /* Prefault in pages to minimize runtime penalty with buslock */
222 1.10.4.1 rmind for (p = (uint8_t *)sc->sc_busmem;
223 1.10.4.1 rmind p < (uint8_t *)sc->sc_busmem + BUSMEM_SIZE;
224 1.10.4.1 rmind p += PAGE_SIZE)
225 1.10.4.1 rmind v = *p;
226 1.10.4.1 rmind
227 1.10.4.1 rmind shmif_lockbus(sc->sc_busmem);
228 1.10.4.1 rmind /* we're first? initialize bus */
229 1.10.4.1 rmind if (sc->sc_busmem->shm_magic == 0) {
230 1.10.4.1 rmind sc->sc_busmem->shm_magic = SHMIF_MAGIC;
231 1.10.4.1 rmind sc->sc_busmem->shm_first = BUSMEM_DATASIZE;
232 1.10.4.1 rmind }
233 1.10.4.1 rmind
234 1.10.4.1 rmind sc->sc_nextpacket = sc->sc_busmem->shm_last;
235 1.10.4.1 rmind sc->sc_devgen = sc->sc_busmem->shm_gen;
236 1.1 pooka
237 1.10.4.1 rmind #ifdef PREFAULT_RW
238 1.10.4.1 rmind for (p = (uint8_t *)sc->sc_busmem;
239 1.10.4.1 rmind p < (uint8_t *)sc->sc_busmem + BUSMEM_SIZE;
240 1.10.4.1 rmind p += PAGE_SIZE) {
241 1.10.4.1 rmind v = *p;
242 1.10.4.1 rmind *p = v;
243 1.10.4.1 rmind }
244 1.10.4.1 rmind #endif
245 1.10.4.1 rmind shmif_unlockbus(sc->sc_busmem);
246 1.10.4.1 rmind
247 1.10.4.1 rmind sc->sc_kq = rumpuser_writewatchfile_setup(-1, memfd, 0, &error);
248 1.10.4.1 rmind if (sc->sc_kq == -1) {
249 1.10.4.1 rmind rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE);
250 1.10.4.1 rmind return error;
251 1.10.4.1 rmind }
252 1.10.4.1 rmind
253 1.10.4.1 rmind sc->sc_memfd = memfd;
254 1.1 pooka
255 1.10.4.1 rmind return error;
256 1.1 pooka }
257 1.1 pooka
258 1.10.4.1 rmind static void
259 1.10.4.1 rmind finibackend(struct shmif_sc *sc)
260 1.1 pooka {
261 1.1 pooka
262 1.10.4.1 rmind if (sc->sc_backfile == NULL)
263 1.10.4.1 rmind return;
264 1.10.4.1 rmind
265 1.10.4.1 rmind if (sc->sc_backfile) {
266 1.10.4.1 rmind kmem_free(sc->sc_backfile, sc->sc_backfilelen);
267 1.10.4.1 rmind sc->sc_backfile = NULL;
268 1.10.4.1 rmind sc->sc_backfilelen = 0;
269 1.10.4.1 rmind }
270 1.10.4.1 rmind
271 1.10.4.1 rmind rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE);
272 1.10.4.1 rmind rumpuser_close(sc->sc_memfd, NULL);
273 1.10.4.1 rmind rumpuser_close(sc->sc_kq, NULL);
274 1.1 pooka
275 1.10.4.1 rmind sc->sc_memfd = -1;
276 1.1 pooka }
277 1.1 pooka
278 1.1 pooka int
279 1.1 pooka rump_shmif_create(const char *path, int *ifnum)
280 1.1 pooka {
281 1.1 pooka struct shmif_sc *sc;
282 1.10.4.1 rmind int unit, error;
283 1.10.4.1 rmind int memfd = -1; /* XXXgcc */
284 1.1 pooka
285 1.10.4.1 rmind if (path) {
286 1.10.4.1 rmind memfd = rumpuser_open(path, O_RDWR | O_CREAT, &error);
287 1.10.4.1 rmind if (memfd == -1)
288 1.10.4.1 rmind return error;
289 1.10.4.1 rmind }
290 1.1 pooka
291 1.10.4.1 rmind unit = vmem_xalloc(shmif_units, 1, 0, 0, 0, 0, 0,
292 1.10.4.1 rmind VM_INSTANTFIT | VM_SLEEP) - 1;
293 1.1 pooka
294 1.10.4.1 rmind if ((error = allocif(unit, &sc)) != 0) {
295 1.10.4.1 rmind if (path)
296 1.10.4.1 rmind rumpuser_close(memfd, NULL);
297 1.10.4.1 rmind return error;
298 1.10.4.1 rmind }
299 1.1 pooka
300 1.10.4.1 rmind if (!path)
301 1.10.4.1 rmind goto out;
302 1.1 pooka
303 1.10.4.1 rmind error = initbackend(sc, memfd);
304 1.10.4.1 rmind if (error) {
305 1.10.4.1 rmind shmif_unclone(&sc->sc_ec.ec_if);
306 1.10.4.1 rmind return error;
307 1.10.4.1 rmind }
308 1.1 pooka
309 1.10.4.1 rmind sc->sc_backfilelen = strlen(path)+1;
310 1.10.4.1 rmind sc->sc_backfile = kmem_alloc(sc->sc_backfilelen, KM_SLEEP);
311 1.10.4.1 rmind strcpy(sc->sc_backfile, path);
312 1.1 pooka
313 1.10.4.1 rmind out:
314 1.2 pooka if (ifnum)
315 1.10.4.1 rmind *ifnum = unit;
316 1.10.4.1 rmind
317 1.1 pooka return 0;
318 1.10.4.1 rmind }
319 1.10.4.1 rmind
320 1.10.4.1 rmind static int
321 1.10.4.1 rmind shmif_clone(struct if_clone *ifc, int unit)
322 1.10.4.1 rmind {
323 1.10.4.1 rmind int unit2;
324 1.10.4.1 rmind
325 1.10.4.1 rmind /*
326 1.10.4.1 rmind * Ok, we know the unit number, but we must still reserve it.
327 1.10.4.1 rmind * Otherwise the wildcard-side of things might get the same one.
328 1.10.4.1 rmind * This is slightly offset-happy due to vmem. First, we offset
329 1.10.4.1 rmind * the range of unit numbers by +1 since vmem cannot deal with
330 1.10.4.1 rmind * ranges starting from 0. Second, since vmem_xalloc() allocates
331 1.10.4.1 rmind * from [min,max) (half-*open* interval), we need to add one extra
332 1.10.4.1 rmind * to the one extra we add to maxaddr. Talk about uuuh.
333 1.10.4.1 rmind */
334 1.10.4.1 rmind unit2 = vmem_xalloc(shmif_units, 1, 0, 0, 0, unit+1, unit+3,
335 1.10.4.1 rmind VM_SLEEP | VM_INSTANTFIT);
336 1.10.4.1 rmind KASSERT(unit2-1 == unit);
337 1.10.4.1 rmind
338 1.10.4.1 rmind return allocif(unit, NULL);
339 1.10.4.1 rmind }
340 1.10.4.1 rmind
341 1.10.4.1 rmind static int
342 1.10.4.1 rmind shmif_unclone(struct ifnet *ifp)
343 1.10.4.1 rmind {
344 1.10.4.1 rmind struct shmif_sc *sc = ifp->if_softc;
345 1.10.4.1 rmind
346 1.10.4.1 rmind shmif_stop(ifp, 1);
347 1.10.4.1 rmind if_down(ifp);
348 1.10.4.1 rmind finibackend(sc);
349 1.10.4.1 rmind
350 1.10.4.1 rmind mutex_enter(&sc->sc_mtx);
351 1.10.4.1 rmind sc->sc_dying = true;
352 1.10.4.1 rmind cv_broadcast(&sc->sc_cv);
353 1.10.4.1 rmind mutex_exit(&sc->sc_mtx);
354 1.10.4.1 rmind
355 1.10.4.1 rmind if (sc->sc_rcvl)
356 1.10.4.1 rmind kthread_join(sc->sc_rcvl);
357 1.10.4.1 rmind sc->sc_rcvl = NULL;
358 1.10.4.1 rmind
359 1.10.4.1 rmind vmem_xfree(shmif_units, sc->sc_unit+1, 1);
360 1.10.4.1 rmind
361 1.10.4.1 rmind ether_ifdetach(ifp);
362 1.10.4.1 rmind if_detach(ifp);
363 1.10.4.1 rmind
364 1.10.4.1 rmind cv_destroy(&sc->sc_cv);
365 1.10.4.1 rmind mutex_destroy(&sc->sc_mtx);
366 1.10.4.1 rmind
367 1.10.4.1 rmind kmem_free(sc, sizeof(*sc));
368 1.1 pooka
369 1.10.4.1 rmind return 0;
370 1.1 pooka }
371 1.1 pooka
372 1.1 pooka static int
373 1.1 pooka shmif_init(struct ifnet *ifp)
374 1.1 pooka {
375 1.10.4.1 rmind struct shmif_sc *sc = ifp->if_softc;
376 1.4 pooka int error = 0;
377 1.4 pooka
378 1.10.4.1 rmind if (sc->sc_memfd == -1)
379 1.10.4.1 rmind return ENXIO;
380 1.10.4.1 rmind KASSERT(sc->sc_busmem);
381 1.1 pooka
382 1.1 pooka ifp->if_flags |= IFF_RUNNING;
383 1.10.4.1 rmind
384 1.10.4.1 rmind mutex_enter(&sc->sc_mtx);
385 1.10.4.1 rmind sc->sc_nextpacket = sc->sc_busmem->shm_last;
386 1.10.4.1 rmind sc->sc_devgen = sc->sc_busmem->shm_gen;
387 1.10.4.1 rmind
388 1.10.4.1 rmind cv_broadcast(&sc->sc_cv);
389 1.10.4.1 rmind mutex_exit(&sc->sc_mtx);
390 1.10.4.1 rmind
391 1.4 pooka return error;
392 1.1 pooka }
393 1.1 pooka
394 1.1 pooka static int
395 1.1 pooka shmif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
396 1.1 pooka {
397 1.10.4.1 rmind struct shmif_sc *sc = ifp->if_softc;
398 1.10.4.1 rmind struct ifdrv *ifd;
399 1.10.4.1 rmind char *path;
400 1.10.4.1 rmind int s, rv, memfd;
401 1.1 pooka
402 1.1 pooka s = splnet();
403 1.10.4.1 rmind switch (cmd) {
404 1.10.4.1 rmind case SIOCGLINKSTR:
405 1.10.4.1 rmind ifd = data;
406 1.10.4.1 rmind
407 1.10.4.1 rmind if (sc->sc_backfilelen == 0) {
408 1.10.4.1 rmind rv = ENOENT;
409 1.10.4.1 rmind break;
410 1.10.4.1 rmind }
411 1.10.4.1 rmind
412 1.10.4.1 rmind ifd->ifd_len = sc->sc_backfilelen;
413 1.10.4.1 rmind if (ifd->ifd_cmd == IFLINKSTR_QUERYLEN) {
414 1.10.4.1 rmind rv = 0;
415 1.10.4.1 rmind break;
416 1.10.4.1 rmind }
417 1.10.4.1 rmind
418 1.10.4.1 rmind if (ifd->ifd_cmd != 0) {
419 1.10.4.1 rmind rv = EINVAL;
420 1.10.4.1 rmind break;
421 1.10.4.1 rmind }
422 1.10.4.1 rmind
423 1.10.4.1 rmind rv = copyoutstr(sc->sc_backfile, ifd->ifd_data,
424 1.10.4.1 rmind MIN(sc->sc_backfilelen, ifd->ifd_len), NULL);
425 1.10.4.1 rmind break;
426 1.10.4.1 rmind case SIOCSLINKSTR:
427 1.10.4.1 rmind if (ifp->if_flags & IFF_UP) {
428 1.10.4.1 rmind rv = EBUSY;
429 1.10.4.1 rmind break;
430 1.10.4.1 rmind }
431 1.10.4.1 rmind
432 1.10.4.1 rmind ifd = data;
433 1.10.4.1 rmind if (ifd->ifd_cmd == IFLINKSTR_UNSET) {
434 1.10.4.1 rmind finibackend(sc);
435 1.10.4.1 rmind rv = 0;
436 1.10.4.1 rmind break;
437 1.10.4.1 rmind } else if (ifd->ifd_cmd != 0) {
438 1.10.4.1 rmind rv = EINVAL;
439 1.10.4.1 rmind break;
440 1.10.4.1 rmind } else if (sc->sc_backfile) {
441 1.10.4.1 rmind rv = EBUSY;
442 1.10.4.1 rmind break;
443 1.10.4.1 rmind }
444 1.10.4.1 rmind
445 1.10.4.1 rmind if (ifd->ifd_len > MAXPATHLEN) {
446 1.10.4.1 rmind rv = E2BIG;
447 1.10.4.1 rmind break;
448 1.10.4.1 rmind } else if (ifd->ifd_len < 1) {
449 1.10.4.1 rmind rv = EINVAL;
450 1.10.4.1 rmind break;
451 1.10.4.1 rmind }
452 1.10.4.1 rmind
453 1.10.4.1 rmind path = kmem_alloc(ifd->ifd_len, KM_SLEEP);
454 1.10.4.1 rmind rv = copyinstr(ifd->ifd_data, path, ifd->ifd_len, NULL);
455 1.10.4.1 rmind if (rv) {
456 1.10.4.1 rmind kmem_free(path, ifd->ifd_len);
457 1.10.4.1 rmind break;
458 1.10.4.1 rmind }
459 1.10.4.1 rmind memfd = rumpuser_open(path, O_RDWR | O_CREAT, &rv);
460 1.10.4.1 rmind if (memfd == -1) {
461 1.10.4.1 rmind kmem_free(path, ifd->ifd_len);
462 1.10.4.1 rmind break;
463 1.10.4.1 rmind }
464 1.10.4.1 rmind rv = initbackend(sc, memfd);
465 1.10.4.1 rmind if (rv) {
466 1.10.4.1 rmind kmem_free(path, ifd->ifd_len);
467 1.10.4.1 rmind rumpuser_close(memfd, NULL);
468 1.10.4.1 rmind break;
469 1.10.4.1 rmind }
470 1.10.4.1 rmind sc->sc_backfile = path;
471 1.10.4.1 rmind sc->sc_backfilelen = ifd->ifd_len;
472 1.10.4.1 rmind
473 1.10.4.1 rmind break;
474 1.10.4.1 rmind default:
475 1.10.4.1 rmind rv = ether_ioctl(ifp, cmd, data);
476 1.10.4.1 rmind if (rv == ENETRESET)
477 1.10.4.1 rmind rv = 0;
478 1.10.4.1 rmind break;
479 1.10.4.1 rmind }
480 1.1 pooka splx(s);
481 1.1 pooka
482 1.1 pooka return rv;
483 1.1 pooka }
484 1.1 pooka
485 1.10.4.1 rmind /* send everything in-context since it's just a matter of mem-to-mem copy */
486 1.1 pooka static void
487 1.1 pooka shmif_start(struct ifnet *ifp)
488 1.1 pooka {
489 1.1 pooka struct shmif_sc *sc = ifp->if_softc;
490 1.10.4.1 rmind struct shmif_mem *busmem = sc->sc_busmem;
491 1.1 pooka struct mbuf *m, *m0;
492 1.10.4.1 rmind uint32_t dataoff;
493 1.10.4.1 rmind uint32_t pktsize, pktwrote;
494 1.1 pooka bool wrote = false;
495 1.10.4.1 rmind bool wrap;
496 1.1 pooka int error;
497 1.1 pooka
498 1.10.4.1 rmind ifp->if_flags |= IFF_OACTIVE;
499 1.10.4.1 rmind
500 1.1 pooka for (;;) {
501 1.10.4.1 rmind struct shmif_pkthdr sp;
502 1.10.4.1 rmind struct timeval tv;
503 1.10.4.1 rmind
504 1.1 pooka IF_DEQUEUE(&ifp->if_snd, m0);
505 1.1 pooka if (m0 == NULL) {
506 1.1 pooka break;
507 1.1 pooka }
508 1.1 pooka
509 1.10.4.1 rmind pktsize = 0;
510 1.1 pooka for (m = m0; m != NULL; m = m->m_next) {
511 1.1 pooka pktsize += m->m_len;
512 1.1 pooka }
513 1.10.4.1 rmind KASSERT(pktsize <= ETHERMTU + ETHER_HDR_LEN);
514 1.10.4.1 rmind
515 1.10.4.1 rmind getmicrouptime(&tv);
516 1.10.4.1 rmind sp.sp_len = pktsize;
517 1.10.4.1 rmind sp.sp_sec = tv.tv_sec;
518 1.10.4.1 rmind sp.sp_usec = tv.tv_usec;
519 1.10.4.1 rmind
520 1.10.4.1 rmind shmif_lockbus(busmem);
521 1.10.4.1 rmind KASSERT(busmem->shm_magic == SHMIF_MAGIC);
522 1.10.4.1 rmind busmem->shm_last = shmif_nextpktoff(busmem, busmem->shm_last);
523 1.10.4.1 rmind
524 1.10.4.1 rmind wrap = false;
525 1.10.4.1 rmind dataoff = shmif_buswrite(busmem,
526 1.10.4.1 rmind busmem->shm_last, &sp, sizeof(sp), &wrap);
527 1.10.4.1 rmind pktwrote = 0;
528 1.10.4.1 rmind for (m = m0; m != NULL; m = m->m_next) {
529 1.10.4.1 rmind pktwrote += m->m_len;
530 1.10.4.1 rmind dataoff = shmif_buswrite(busmem, dataoff,
531 1.10.4.1 rmind mtod(m, void *), m->m_len, &wrap);
532 1.10.4.1 rmind }
533 1.10.4.1 rmind KASSERT(pktwrote == pktsize);
534 1.10.4.1 rmind if (wrap) {
535 1.10.4.1 rmind busmem->shm_gen++;
536 1.10.4.1 rmind DPRINTF(("bus generation now %d\n", busmem->shm_gen));
537 1.10.4.1 rmind }
538 1.10.4.1 rmind shmif_unlockbus(busmem);
539 1.1 pooka
540 1.1 pooka m_freem(m0);
541 1.1 pooka wrote = true;
542 1.1 pooka
543 1.1 pooka DPRINTF(("shmif_start: send %d bytes at off %d\n",
544 1.10.4.1 rmind pktsize, busmem->shm_last));
545 1.1 pooka }
546 1.10.4.1 rmind
547 1.10.4.1 rmind ifp->if_flags &= ~IFF_OACTIVE;
548 1.10.4.1 rmind
549 1.10.4.1 rmind /* wakeup? */
550 1.1 pooka if (wrote)
551 1.10.4.1 rmind rumpuser_pwrite(sc->sc_memfd,
552 1.10.4.1 rmind &busversion, sizeof(busversion), IFMEM_WAKEUP, &error);
553 1.1 pooka }
554 1.1 pooka
555 1.1 pooka static void
556 1.1 pooka shmif_stop(struct ifnet *ifp, int disable)
557 1.1 pooka {
558 1.10.4.1 rmind struct shmif_sc *sc = ifp->if_softc;
559 1.10.4.1 rmind
560 1.10.4.1 rmind ifp->if_flags &= ~IFF_RUNNING;
561 1.10.4.1 rmind membar_producer();
562 1.1 pooka
563 1.10.4.1 rmind /*
564 1.10.4.1 rmind * wakeup thread. this will of course wake up all bus
565 1.10.4.1 rmind * listeners, but that's life.
566 1.10.4.1 rmind */
567 1.10.4.1 rmind if (sc->sc_memfd != -1)
568 1.10.4.1 rmind rumpuser_pwrite(sc->sc_memfd,
569 1.10.4.1 rmind &busversion, sizeof(busversion), IFMEM_WAKEUP, NULL);
570 1.10.4.1 rmind }
571 1.10.4.1 rmind
572 1.10.4.1 rmind
573 1.10.4.1 rmind /*
574 1.10.4.1 rmind * Check if we have been sleeping too long. Basically,
575 1.10.4.1 rmind * our in-sc nextpkt must by first <= nextpkt <= last"+1".
576 1.10.4.1 rmind * We use the fact that first is guaranteed to never overlap
577 1.10.4.1 rmind * with the last frame in the ring.
578 1.10.4.1 rmind */
579 1.10.4.1 rmind static __inline bool
580 1.10.4.1 rmind stillvalid_p(struct shmif_sc *sc)
581 1.10.4.1 rmind {
582 1.10.4.1 rmind struct shmif_mem *busmem = sc->sc_busmem;
583 1.10.4.1 rmind unsigned gendiff = busmem->shm_gen - sc->sc_devgen;
584 1.10.4.1 rmind uint32_t lastoff, devoff;
585 1.10.4.1 rmind
586 1.10.4.1 rmind KASSERT(busmem->shm_first != busmem->shm_last);
587 1.10.4.1 rmind
588 1.10.4.1 rmind /* normalize onto a 2x busmem chunk */
589 1.10.4.1 rmind devoff = sc->sc_nextpacket;
590 1.10.4.1 rmind lastoff = shmif_nextpktoff(busmem, busmem->shm_last);
591 1.10.4.1 rmind
592 1.10.4.1 rmind /* trivial case */
593 1.10.4.1 rmind if (gendiff > 1)
594 1.10.4.1 rmind return false;
595 1.10.4.1 rmind KASSERT(gendiff <= 1);
596 1.10.4.1 rmind
597 1.10.4.1 rmind /* Normalize onto 2x busmem chunk */
598 1.10.4.1 rmind if (busmem->shm_first >= lastoff) {
599 1.10.4.1 rmind lastoff += BUSMEM_DATASIZE;
600 1.10.4.1 rmind if (gendiff == 0)
601 1.10.4.1 rmind devoff += BUSMEM_DATASIZE;
602 1.10.4.1 rmind } else {
603 1.10.4.1 rmind if (gendiff)
604 1.10.4.1 rmind return false;
605 1.10.4.1 rmind }
606 1.10.4.1 rmind
607 1.10.4.1 rmind return devoff >= busmem->shm_first && devoff <= lastoff;
608 1.1 pooka }
609 1.1 pooka
610 1.1 pooka static void
611 1.1 pooka shmif_rcv(void *arg)
612 1.1 pooka {
613 1.1 pooka struct ifnet *ifp = arg;
614 1.1 pooka struct shmif_sc *sc = ifp->if_softc;
615 1.10.4.1 rmind struct shmif_mem *busmem;
616 1.1 pooka struct mbuf *m = NULL;
617 1.1 pooka struct ether_header *eth;
618 1.10.4.1 rmind uint32_t nextpkt;
619 1.10.4.1 rmind bool wrap;
620 1.1 pooka int error;
621 1.1 pooka
622 1.10.4.1 rmind reup:
623 1.10.4.1 rmind mutex_enter(&sc->sc_mtx);
624 1.10.4.1 rmind while ((ifp->if_flags & IFF_RUNNING) == 0 && !sc->sc_dying)
625 1.10.4.1 rmind cv_wait(&sc->sc_cv, &sc->sc_mtx);
626 1.10.4.1 rmind mutex_exit(&sc->sc_mtx);
627 1.10.4.1 rmind
628 1.10.4.1 rmind busmem = sc->sc_busmem;
629 1.10.4.1 rmind
630 1.10.4.1 rmind while (ifp->if_flags & IFF_RUNNING) {
631 1.10.4.1 rmind struct shmif_pkthdr sp;
632 1.10.4.1 rmind
633 1.1 pooka if (m == NULL) {
634 1.1 pooka m = m_gethdr(M_WAIT, MT_DATA);
635 1.1 pooka MCLGET(m, M_WAIT);
636 1.1 pooka }
637 1.1 pooka
638 1.10.4.1 rmind DPRINTF(("waiting %d/%d\n", sc->sc_nextpacket, sc->sc_devgen));
639 1.1 pooka KASSERT(m->m_flags & M_EXT);
640 1.10.4.1 rmind
641 1.10.4.1 rmind shmif_lockbus(busmem);
642 1.10.4.1 rmind KASSERT(busmem->shm_magic == SHMIF_MAGIC);
643 1.10.4.1 rmind KASSERT(busmem->shm_gen >= sc->sc_devgen);
644 1.1 pooka
645 1.1 pooka /* need more data? */
646 1.10.4.1 rmind if (sc->sc_devgen == busmem->shm_gen &&
647 1.10.4.1 rmind shmif_nextpktoff(busmem, busmem->shm_last)
648 1.10.4.1 rmind == sc->sc_nextpacket) {
649 1.10.4.1 rmind shmif_unlockbus(busmem);
650 1.1 pooka error = 0;
651 1.1 pooka rumpuser_writewatchfile_wait(sc->sc_kq, NULL, &error);
652 1.1 pooka if (__predict_false(error))
653 1.1 pooka printf("shmif_rcv: wait failed %d\n", error);
654 1.10.4.1 rmind membar_consumer();
655 1.1 pooka continue;
656 1.1 pooka }
657 1.1 pooka
658 1.10.4.1 rmind if (stillvalid_p(sc)) {
659 1.10.4.1 rmind nextpkt = sc->sc_nextpacket;
660 1.10.4.1 rmind } else {
661 1.10.4.1 rmind KASSERT(busmem->shm_gen > 0);
662 1.10.4.1 rmind nextpkt = busmem->shm_first;
663 1.10.4.1 rmind if (busmem->shm_first > busmem->shm_last)
664 1.10.4.1 rmind sc->sc_devgen = busmem->shm_gen - 1;
665 1.10.4.1 rmind else
666 1.10.4.1 rmind sc->sc_devgen = busmem->shm_gen;
667 1.10.4.1 rmind DPRINTF(("dev %p overrun, new data: %d/%d\n",
668 1.10.4.1 rmind sc, nextpkt, sc->sc_devgen));
669 1.10.4.1 rmind }
670 1.10.4.1 rmind
671 1.10.4.1 rmind /*
672 1.10.4.1 rmind * If our read pointer is ahead the bus last write, our
673 1.10.4.1 rmind * generation must be one behind.
674 1.10.4.1 rmind */
675 1.10.4.1 rmind KASSERT(!(nextpkt > busmem->shm_last
676 1.10.4.1 rmind && sc->sc_devgen == busmem->shm_gen));
677 1.10.4.1 rmind
678 1.10.4.1 rmind wrap = false;
679 1.10.4.1 rmind nextpkt = shmif_busread(busmem, &sp,
680 1.10.4.1 rmind nextpkt, sizeof(sp), &wrap);
681 1.10.4.1 rmind KASSERT(sp.sp_len <= ETHERMTU + ETHER_HDR_LEN);
682 1.10.4.1 rmind nextpkt = shmif_busread(busmem, mtod(m, void *),
683 1.10.4.1 rmind nextpkt, sp.sp_len, &wrap);
684 1.1 pooka
685 1.1 pooka DPRINTF(("shmif_rcv: read packet of length %d at %d\n",
686 1.10.4.1 rmind sp.sp_len, nextpkt));
687 1.1 pooka
688 1.10.4.1 rmind sc->sc_nextpacket = nextpkt;
689 1.10.4.1 rmind shmif_unlockbus(sc->sc_busmem);
690 1.1 pooka
691 1.10.4.1 rmind if (wrap) {
692 1.10.4.1 rmind sc->sc_devgen++;
693 1.10.4.1 rmind DPRINTF(("dev %p generation now %d\n",
694 1.10.4.1 rmind sc, sc->sc_devgen));
695 1.10.4.1 rmind }
696 1.10.4.1 rmind
697 1.10.4.1 rmind m->m_len = m->m_pkthdr.len = sp.sp_len;
698 1.1 pooka m->m_pkthdr.rcvif = ifp;
699 1.1 pooka
700 1.10 pooka /* if it's from us, don't pass up and reuse storage space */
701 1.1 pooka eth = mtod(m, struct ether_header *);
702 1.1 pooka if (memcmp(eth->ether_shost, sc->sc_myaddr, 6) != 0) {
703 1.10.4.1 rmind KERNEL_LOCK(1, NULL);
704 1.1 pooka ifp->if_input(ifp, m);
705 1.10.4.1 rmind KERNEL_UNLOCK_ONE(NULL);
706 1.1 pooka m = NULL;
707 1.1 pooka }
708 1.1 pooka }
709 1.10.4.1 rmind m_freem(m);
710 1.10.4.1 rmind m = NULL;
711 1.10.4.1 rmind
712 1.10.4.1 rmind if (!sc->sc_dying)
713 1.10.4.1 rmind goto reup;
714 1.1 pooka
715 1.10.4.1 rmind kthread_exit(0);
716 1.1 pooka }
717