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