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