if_shmem.c revision 1.27 1 1.27 pooka /* $NetBSD: if_shmem.c,v 1.27 2010/08/17 11:35:23 pooka 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.27 pooka __KERNEL_RCSID(0, "$NetBSD: if_shmem.c,v 1.27 2010/08/17 11:35:23 pooka Exp $");
32 1.1 pooka
33 1.1 pooka #include <sys/param.h>
34 1.13 pooka #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.3 martin #include <sys/atomic.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.1 pooka /*
54 1.1 pooka * A virtual ethernet interface which uses shared memory from a
55 1.1 pooka * memory mapped file as the bus.
56 1.1 pooka */
57 1.1 pooka
58 1.1 pooka static int shmif_init(struct ifnet *);
59 1.1 pooka static int shmif_ioctl(struct ifnet *, u_long, void *);
60 1.1 pooka static void shmif_start(struct ifnet *);
61 1.1 pooka static void shmif_stop(struct ifnet *, int);
62 1.1 pooka
63 1.16 pooka #include "shmifvar.h"
64 1.16 pooka
65 1.1 pooka struct shmif_sc {
66 1.1 pooka struct ethercom sc_ec;
67 1.1 pooka uint8_t sc_myaddr[6];
68 1.16 pooka struct shmif_mem *sc_busmem;
69 1.1 pooka int sc_memfd;
70 1.1 pooka int sc_kq;
71 1.1 pooka
72 1.26 pooka uint64_t sc_devgen;
73 1.1 pooka uint32_t sc_nextpacket;
74 1.1 pooka };
75 1.1 pooka
76 1.17 pooka static const uint32_t busversion = SHMIF_VERSION;
77 1.1 pooka
78 1.1 pooka static void shmif_rcv(void *);
79 1.1 pooka
80 1.1 pooka static uint32_t numif;
81 1.1 pooka
82 1.23 pooka #define LOCK_UNLOCKED 0
83 1.23 pooka #define LOCK_LOCKED 1
84 1.23 pooka #define LOCK_COOLDOWN 1001
85 1.23 pooka
86 1.23 pooka /*
87 1.23 pooka * This locking needs work and will misbehave severely if:
88 1.23 pooka * 1) the backing memory has to be paged in
89 1.23 pooka * 2) some lockholder exits while holding the lock
90 1.23 pooka */
91 1.23 pooka static void
92 1.23 pooka shmif_lockbus(struct shmif_mem *busmem)
93 1.23 pooka {
94 1.23 pooka int i = 0;
95 1.23 pooka
96 1.23 pooka while (__predict_false(atomic_cas_32(&busmem->shm_lock,
97 1.23 pooka LOCK_UNLOCKED, LOCK_LOCKED) == LOCK_LOCKED)) {
98 1.23 pooka if (__predict_false(++i > LOCK_COOLDOWN)) {
99 1.23 pooka uint64_t sec, nsec;
100 1.23 pooka int error;
101 1.23 pooka
102 1.23 pooka sec = 0;
103 1.23 pooka nsec = 1000*1000; /* 1ms */
104 1.23 pooka rumpuser_nanosleep(&sec, &nsec, &error);
105 1.23 pooka i = 0;
106 1.23 pooka }
107 1.23 pooka continue;
108 1.23 pooka }
109 1.23 pooka membar_enter();
110 1.23 pooka }
111 1.23 pooka
112 1.23 pooka static void
113 1.23 pooka shmif_unlockbus(struct shmif_mem *busmem)
114 1.23 pooka {
115 1.23 pooka unsigned int old;
116 1.23 pooka
117 1.23 pooka membar_exit();
118 1.23 pooka old = atomic_swap_32(&busmem->shm_lock, LOCK_UNLOCKED);
119 1.23 pooka KASSERT(old == LOCK_LOCKED);
120 1.23 pooka }
121 1.23 pooka
122 1.1 pooka int
123 1.1 pooka rump_shmif_create(const char *path, int *ifnum)
124 1.1 pooka {
125 1.1 pooka struct shmif_sc *sc;
126 1.1 pooka struct ifnet *ifp;
127 1.1 pooka uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0xa0, 0x00, 0x00, 0x00, 0x00 };
128 1.1 pooka uint32_t randnum;
129 1.1 pooka unsigned mynum;
130 1.1 pooka int error;
131 1.1 pooka
132 1.1 pooka randnum = arc4random();
133 1.15 pooka memcpy(&enaddr[2], &randnum, sizeof(randnum));
134 1.1 pooka mynum = atomic_inc_uint_nv(&numif)-1;
135 1.1 pooka
136 1.1 pooka sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
137 1.1 pooka ifp = &sc->sc_ec.ec_if;
138 1.1 pooka memcpy(sc->sc_myaddr, enaddr, sizeof(enaddr));
139 1.1 pooka
140 1.1 pooka sc->sc_memfd = rumpuser_open(path, O_RDWR | O_CREAT, &error);
141 1.1 pooka if (sc->sc_memfd == -1)
142 1.1 pooka goto fail;
143 1.1 pooka sc->sc_busmem = rumpuser_filemmap(sc->sc_memfd, 0, BUSMEM_SIZE,
144 1.5 pooka RUMPUSER_FILEMMAP_TRUNCATE | RUMPUSER_FILEMMAP_SHARED
145 1.5 pooka | RUMPUSER_FILEMMAP_READ | RUMPUSER_FILEMMAP_WRITE, &error);
146 1.1 pooka if (error)
147 1.1 pooka goto fail;
148 1.1 pooka
149 1.17 pooka if (sc->sc_busmem->shm_magic && sc->sc_busmem->shm_magic != SHMIF_MAGIC)
150 1.17 pooka panic("bus is not magical");
151 1.17 pooka
152 1.19 pooka shmif_lockbus(sc->sc_busmem);
153 1.19 pooka /* we're first? initialize bus */
154 1.19 pooka if (sc->sc_busmem->shm_magic == 0) {
155 1.19 pooka sc->sc_busmem->shm_magic = SHMIF_MAGIC;
156 1.19 pooka sc->sc_busmem->shm_first = BUSMEM_DATASIZE;
157 1.19 pooka }
158 1.19 pooka
159 1.16 pooka sc->sc_nextpacket = sc->sc_busmem->shm_last;
160 1.26 pooka sc->sc_devgen = sc->sc_busmem->shm_gen;
161 1.19 pooka shmif_unlockbus(sc->sc_busmem);
162 1.1 pooka
163 1.1 pooka sc->sc_kq = rumpuser_writewatchfile_setup(-1, sc->sc_memfd, 0, &error);
164 1.1 pooka if (sc->sc_kq == -1)
165 1.1 pooka goto fail;
166 1.1 pooka
167 1.1 pooka sprintf(ifp->if_xname, "shmif%d", mynum);
168 1.1 pooka ifp->if_softc = sc;
169 1.13 pooka ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
170 1.1 pooka ifp->if_init = shmif_init;
171 1.1 pooka ifp->if_ioctl = shmif_ioctl;
172 1.1 pooka ifp->if_start = shmif_start;
173 1.1 pooka ifp->if_stop = shmif_stop;
174 1.26 pooka ifp->if_mtu = ETHERMTU;
175 1.1 pooka
176 1.1 pooka if_attach(ifp);
177 1.1 pooka ether_ifattach(ifp, enaddr);
178 1.1 pooka
179 1.12 pooka aprint_verbose("shmif%d: bus %s\n", mynum, path);
180 1.12 pooka aprint_verbose("shmif%d: Ethernet address %s\n",
181 1.12 pooka mynum, ether_sprintf(enaddr));
182 1.12 pooka
183 1.2 pooka if (ifnum)
184 1.2 pooka *ifnum = mynum;
185 1.1 pooka return 0;
186 1.1 pooka
187 1.1 pooka fail:
188 1.1 pooka panic("rump_shmemif_create: fixme");
189 1.1 pooka }
190 1.1 pooka
191 1.1 pooka static int
192 1.1 pooka shmif_init(struct ifnet *ifp)
193 1.1 pooka {
194 1.4 pooka int error = 0;
195 1.4 pooka
196 1.4 pooka if (rump_threads) {
197 1.4 pooka error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
198 1.4 pooka shmif_rcv, ifp, NULL, "shmif");
199 1.4 pooka } else {
200 1.4 pooka printf("WARNING: threads not enabled, shmif NOT working\n");
201 1.4 pooka }
202 1.1 pooka
203 1.1 pooka ifp->if_flags |= IFF_RUNNING;
204 1.4 pooka return error;
205 1.1 pooka }
206 1.1 pooka
207 1.1 pooka static int
208 1.1 pooka shmif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
209 1.1 pooka {
210 1.1 pooka int s, rv;
211 1.1 pooka
212 1.1 pooka s = splnet();
213 1.1 pooka rv = ether_ioctl(ifp, cmd, data);
214 1.7 pooka if (rv == ENETRESET)
215 1.7 pooka rv = 0;
216 1.1 pooka splx(s);
217 1.1 pooka
218 1.1 pooka return rv;
219 1.1 pooka }
220 1.1 pooka
221 1.1 pooka /* send everything in-context */
222 1.1 pooka static void
223 1.1 pooka shmif_start(struct ifnet *ifp)
224 1.1 pooka {
225 1.1 pooka struct shmif_sc *sc = ifp->if_softc;
226 1.26 pooka struct shmif_mem *busmem = sc->sc_busmem;
227 1.1 pooka struct mbuf *m, *m0;
228 1.26 pooka uint32_t dataoff;
229 1.26 pooka uint32_t pktsize, pktwrote;
230 1.1 pooka bool wrote = false;
231 1.24 pooka bool wrap;
232 1.1 pooka int error;
233 1.1 pooka
234 1.26 pooka ifp->if_flags |= IFF_OACTIVE;
235 1.26 pooka
236 1.1 pooka for (;;) {
237 1.20 pooka struct shmif_pkthdr sp;
238 1.20 pooka struct timeval tv;
239 1.20 pooka
240 1.1 pooka IF_DEQUEUE(&ifp->if_snd, m0);
241 1.1 pooka if (m0 == NULL) {
242 1.1 pooka break;
243 1.1 pooka }
244 1.1 pooka
245 1.25 pooka pktsize = 0;
246 1.19 pooka for (m = m0; m != NULL; m = m->m_next) {
247 1.19 pooka pktsize += m->m_len;
248 1.19 pooka }
249 1.26 pooka KASSERT(pktsize <= ETHERMTU + ETHER_HDR_LEN);
250 1.19 pooka
251 1.20 pooka getmicrouptime(&tv);
252 1.20 pooka sp.sp_len = pktsize;
253 1.20 pooka sp.sp_sec = tv.tv_sec;
254 1.20 pooka sp.sp_usec = tv.tv_usec;
255 1.20 pooka
256 1.26 pooka shmif_lockbus(busmem);
257 1.26 pooka KASSERT(busmem->shm_magic == SHMIF_MAGIC);
258 1.26 pooka busmem->shm_last = shmif_nextpktoff(busmem, busmem->shm_last);
259 1.21 pooka
260 1.24 pooka wrap = false;
261 1.26 pooka dataoff = shmif_buswrite(busmem,
262 1.26 pooka busmem->shm_last, &sp, sizeof(sp), &wrap);
263 1.26 pooka pktwrote = 0;
264 1.1 pooka for (m = m0; m != NULL; m = m->m_next) {
265 1.26 pooka pktwrote += m->m_len;
266 1.26 pooka dataoff = shmif_buswrite(busmem, dataoff,
267 1.19 pooka mtod(m, void *), m->m_len, &wrap);
268 1.1 pooka }
269 1.26 pooka KASSERT(pktwrote == pktsize);
270 1.27 pooka if (wrap) {
271 1.26 pooka busmem->shm_gen++;
272 1.27 pooka DPRINTF(("bus generation now %d\n", busmem->shm_gen));
273 1.27 pooka }
274 1.26 pooka shmif_unlockbus(busmem);
275 1.1 pooka
276 1.1 pooka m_freem(m0);
277 1.1 pooka wrote = true;
278 1.1 pooka
279 1.1 pooka DPRINTF(("shmif_start: send %d bytes at off %d\n",
280 1.27 pooka pktsize, busmem->shm_last));
281 1.1 pooka }
282 1.26 pooka
283 1.26 pooka ifp->if_flags &= ~IFF_OACTIVE;
284 1.26 pooka
285 1.1 pooka /* wakeup */
286 1.1 pooka if (wrote)
287 1.15 pooka rumpuser_pwrite(sc->sc_memfd,
288 1.15 pooka &busversion, sizeof(busversion), IFMEM_WAKEUP, &error);
289 1.1 pooka }
290 1.1 pooka
291 1.1 pooka static void
292 1.1 pooka shmif_stop(struct ifnet *ifp, int disable)
293 1.1 pooka {
294 1.1 pooka
295 1.1 pooka panic("%s: unimpl", __func__);
296 1.1 pooka }
297 1.1 pooka
298 1.27 pooka
299 1.27 pooka /*
300 1.27 pooka * Check if we have been sleeping too long. Basically,
301 1.27 pooka * our in-sc nextpkt must by first <= nextpkt <= last"+1".
302 1.27 pooka * We use the fact that first is guaranteed to never overlap
303 1.27 pooka * with the last frame in the ring.
304 1.27 pooka */
305 1.27 pooka static __inline bool
306 1.27 pooka stillvalid_p(struct shmif_sc *sc)
307 1.27 pooka {
308 1.27 pooka struct shmif_mem *busmem = sc->sc_busmem;
309 1.27 pooka unsigned gendiff = busmem->shm_gen - sc->sc_devgen;
310 1.27 pooka uint32_t lastoff, devoff;
311 1.27 pooka
312 1.27 pooka KASSERT(busmem->shm_first != busmem->shm_last);
313 1.27 pooka
314 1.27 pooka /* normalize onto a 2x busmem chunk */
315 1.27 pooka devoff = sc->sc_nextpacket;
316 1.27 pooka lastoff = shmif_nextpktoff(busmem, busmem->shm_last);
317 1.27 pooka
318 1.27 pooka /* trivial case */
319 1.27 pooka if (gendiff > 1)
320 1.27 pooka return false;
321 1.27 pooka KASSERT(gendiff <= 1);
322 1.27 pooka
323 1.27 pooka /* Normalize onto 2x busmem chunk */
324 1.27 pooka if (busmem->shm_first >= lastoff) {
325 1.27 pooka lastoff += BUSMEM_DATASIZE;
326 1.27 pooka if (gendiff == 0)
327 1.27 pooka devoff += BUSMEM_DATASIZE;
328 1.27 pooka } else {
329 1.27 pooka if (gendiff)
330 1.27 pooka return false;
331 1.27 pooka }
332 1.27 pooka
333 1.27 pooka return devoff >= busmem->shm_first && devoff <= lastoff;
334 1.27 pooka }
335 1.27 pooka
336 1.1 pooka static void
337 1.1 pooka shmif_rcv(void *arg)
338 1.1 pooka {
339 1.1 pooka struct ifnet *ifp = arg;
340 1.1 pooka struct shmif_sc *sc = ifp->if_softc;
341 1.26 pooka struct shmif_mem *busmem = sc->sc_busmem;
342 1.1 pooka struct mbuf *m = NULL;
343 1.1 pooka struct ether_header *eth;
344 1.27 pooka uint32_t nextpkt;
345 1.24 pooka bool wrap;
346 1.1 pooka int error;
347 1.1 pooka
348 1.1 pooka for (;;) {
349 1.20 pooka struct shmif_pkthdr sp;
350 1.20 pooka
351 1.1 pooka if (m == NULL) {
352 1.1 pooka m = m_gethdr(M_WAIT, MT_DATA);
353 1.1 pooka MCLGET(m, M_WAIT);
354 1.1 pooka }
355 1.1 pooka
356 1.26 pooka DPRINTF(("waiting %d/%d\n", sc->sc_nextpacket, sc->sc_devgen));
357 1.26 pooka KASSERT(m->m_flags & M_EXT);
358 1.1 pooka
359 1.26 pooka shmif_lockbus(busmem);
360 1.26 pooka KASSERT(busmem->shm_magic == SHMIF_MAGIC);
361 1.27 pooka KASSERT(busmem->shm_gen >= sc->sc_devgen);
362 1.1 pooka
363 1.1 pooka /* need more data? */
364 1.27 pooka if (sc->sc_devgen == busmem->shm_gen &&
365 1.26 pooka shmif_nextpktoff(busmem, busmem->shm_last)
366 1.26 pooka == sc->sc_nextpacket) {
367 1.26 pooka shmif_unlockbus(busmem);
368 1.1 pooka error = 0;
369 1.1 pooka rumpuser_writewatchfile_wait(sc->sc_kq, NULL, &error);
370 1.1 pooka if (__predict_false(error))
371 1.1 pooka printf("shmif_rcv: wait failed %d\n", error);
372 1.1 pooka continue;
373 1.1 pooka }
374 1.1 pooka
375 1.27 pooka if (stillvalid_p(sc)) {
376 1.27 pooka nextpkt = sc->sc_nextpacket;
377 1.27 pooka } else {
378 1.27 pooka KASSERT(busmem->shm_gen > 0);
379 1.26 pooka nextpkt = busmem->shm_first;
380 1.26 pooka if (busmem->shm_first > busmem->shm_last)
381 1.27 pooka sc->sc_devgen = busmem->shm_gen - 1;
382 1.26 pooka else
383 1.27 pooka sc->sc_devgen = busmem->shm_gen;
384 1.27 pooka DPRINTF(("dev %p overrun, new data: %d/%d\n",
385 1.27 pooka sc, nextpkt, sc->sc_devgen));
386 1.26 pooka }
387 1.26 pooka
388 1.26 pooka /*
389 1.26 pooka * If our read pointer is ahead the bus last write, our
390 1.26 pooka * generation must be one behind.
391 1.26 pooka */
392 1.26 pooka KASSERT(!(nextpkt > busmem->shm_last
393 1.27 pooka && sc->sc_devgen == busmem->shm_gen));
394 1.26 pooka
395 1.24 pooka wrap = false;
396 1.26 pooka nextpkt = shmif_busread(busmem, &sp,
397 1.26 pooka nextpkt, sizeof(sp), &wrap);
398 1.26 pooka KASSERT(sp.sp_len <= ETHERMTU + ETHER_HDR_LEN);
399 1.26 pooka nextpkt = shmif_busread(busmem, mtod(m, void *),
400 1.26 pooka nextpkt, sp.sp_len, &wrap);
401 1.1 pooka
402 1.1 pooka DPRINTF(("shmif_rcv: read packet of length %d at %d\n",
403 1.20 pooka sp.sp_len, nextpkt));
404 1.1 pooka
405 1.26 pooka sc->sc_nextpacket = nextpkt;
406 1.19 pooka shmif_unlockbus(sc->sc_busmem);
407 1.1 pooka
408 1.27 pooka if (wrap) {
409 1.26 pooka sc->sc_devgen++;
410 1.27 pooka DPRINTF(("dev %p generation now %d\n",
411 1.27 pooka sc, sc->sc_devgen));
412 1.27 pooka }
413 1.26 pooka
414 1.20 pooka m->m_len = m->m_pkthdr.len = sp.sp_len;
415 1.1 pooka m->m_pkthdr.rcvif = ifp;
416 1.1 pooka
417 1.10 pooka /* if it's from us, don't pass up and reuse storage space */
418 1.1 pooka eth = mtod(m, struct ether_header *);
419 1.1 pooka if (memcmp(eth->ether_shost, sc->sc_myaddr, 6) != 0) {
420 1.22 pooka KERNEL_LOCK(1, NULL);
421 1.1 pooka ifp->if_input(ifp, m);
422 1.22 pooka KERNEL_UNLOCK_ONE(NULL);
423 1.1 pooka m = NULL;
424 1.1 pooka }
425 1.1 pooka }
426 1.1 pooka
427 1.1 pooka panic("shmif_worker is a lazy boy %d\n", error);
428 1.1 pooka }
429