if_shmem.c revision 1.18 1 1.18 pooka /* $NetBSD: if_shmem.c,v 1.18 2010/08/12 18:39:54 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.18 pooka __KERNEL_RCSID(0, "$NetBSD: if_shmem.c,v 1.18 2010/08/12 18:39:54 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.6 pooka #if 0
54 1.8 pooka #define DPRINTF(x) rumpuser_dprintf x
55 1.6 pooka #else
56 1.6 pooka #define DPRINTF(x)
57 1.6 pooka #endif
58 1.6 pooka
59 1.1 pooka /*
60 1.1 pooka * A virtual ethernet interface which uses shared memory from a
61 1.1 pooka * memory mapped file as the bus.
62 1.1 pooka */
63 1.1 pooka
64 1.1 pooka static int shmif_init(struct ifnet *);
65 1.1 pooka static int shmif_ioctl(struct ifnet *, u_long, void *);
66 1.1 pooka static void shmif_start(struct ifnet *);
67 1.1 pooka static void shmif_stop(struct ifnet *, int);
68 1.1 pooka
69 1.16 pooka #include "shmifvar.h"
70 1.16 pooka
71 1.1 pooka struct shmif_sc {
72 1.1 pooka struct ethercom sc_ec;
73 1.1 pooka uint8_t sc_myaddr[6];
74 1.16 pooka struct shmif_mem *sc_busmem;
75 1.1 pooka int sc_memfd;
76 1.1 pooka int sc_kq;
77 1.1 pooka
78 1.1 pooka uint32_t sc_nextpacket;
79 1.1 pooka uint32_t sc_prevgen;
80 1.1 pooka };
81 1.1 pooka
82 1.18 pooka #define BUSMEM_SIZE (1024*1024)
83 1.18 pooka #define BUSMEM_DATASIZE (BUSMEM_SIZE - sizeof(struct shmif_mem))
84 1.15 pooka
85 1.17 pooka static const uint32_t busversion = SHMIF_VERSION;
86 1.1 pooka
87 1.1 pooka static void shmif_rcv(void *);
88 1.1 pooka
89 1.1 pooka static uint32_t numif;
90 1.1 pooka
91 1.13 pooka #define LOCK_UNLOCKED 0
92 1.13 pooka #define LOCK_LOCKED 1
93 1.13 pooka
94 1.2 pooka /*
95 1.2 pooka * This locking needs work and will misbehave severely if:
96 1.2 pooka * 1) the backing memory has to be paged in
97 1.2 pooka * 2) some lockholder exits while holding the lock
98 1.2 pooka */
99 1.1 pooka static void
100 1.1 pooka lockbus(struct shmif_sc *sc)
101 1.1 pooka {
102 1.1 pooka
103 1.16 pooka while (atomic_cas_32(&sc->sc_busmem->shm_lock,
104 1.13 pooka LOCK_UNLOCKED, LOCK_LOCKED) == LOCK_LOCKED)
105 1.13 pooka continue;
106 1.13 pooka membar_enter();
107 1.1 pooka }
108 1.1 pooka
109 1.1 pooka static void
110 1.1 pooka unlockbus(struct shmif_sc *sc)
111 1.1 pooka {
112 1.13 pooka unsigned int old;
113 1.1 pooka
114 1.13 pooka membar_exit();
115 1.16 pooka old = atomic_swap_32(&sc->sc_busmem->shm_lock, LOCK_UNLOCKED);
116 1.13 pooka KASSERT(old == LOCK_LOCKED);
117 1.1 pooka }
118 1.1 pooka
119 1.1 pooka static uint32_t
120 1.1 pooka busread(struct shmif_sc *sc, void *dest, uint32_t off, size_t len)
121 1.1 pooka {
122 1.1 pooka size_t chunk;
123 1.1 pooka
124 1.18 pooka KASSERT(len < (BUSMEM_DATASIZE) && off <= BUSMEM_DATASIZE);
125 1.18 pooka chunk = MIN(len, BUSMEM_DATASIZE - off);
126 1.17 pooka memcpy(dest, sc->sc_busmem->shm_data + off, chunk);
127 1.1 pooka len -= chunk;
128 1.1 pooka
129 1.1 pooka if (len == 0)
130 1.1 pooka return off + chunk;
131 1.1 pooka
132 1.1 pooka /* else, wraps around */
133 1.17 pooka off = 0;
134 1.16 pooka sc->sc_prevgen = sc->sc_busmem->shm_gen;
135 1.1 pooka
136 1.1 pooka /* finish reading */
137 1.17 pooka memcpy((uint8_t *)dest + chunk, sc->sc_busmem->shm_data + off, len);
138 1.1 pooka return off + len;
139 1.1 pooka }
140 1.1 pooka
141 1.1 pooka static uint32_t
142 1.1 pooka buswrite(struct shmif_sc *sc, uint32_t off, void *data, size_t len)
143 1.1 pooka {
144 1.1 pooka size_t chunk;
145 1.1 pooka
146 1.18 pooka KASSERT(len < (BUSMEM_DATASIZE) && off <= BUSMEM_DATASIZE);
147 1.1 pooka
148 1.18 pooka chunk = MIN(len, BUSMEM_DATASIZE - off);
149 1.17 pooka memcpy(sc->sc_busmem->shm_data + off, data, chunk);
150 1.1 pooka len -= chunk;
151 1.1 pooka
152 1.1 pooka if (len == 0)
153 1.1 pooka return off + chunk;
154 1.1 pooka
155 1.17 pooka DPRINTF(("buswrite wrap: wrote %d bytes to %d, left %d to 0",
156 1.17 pooka chunk, off, len));
157 1.1 pooka
158 1.1 pooka /* else, wraps around */
159 1.17 pooka off = 0;
160 1.16 pooka sc->sc_prevgen = sc->sc_busmem->shm_gen;
161 1.16 pooka sc->sc_busmem->shm_gen++;
162 1.1 pooka
163 1.1 pooka /* finish writing */
164 1.17 pooka memcpy(sc->sc_busmem->shm_data + off, (uint8_t *)data + chunk, len);
165 1.1 pooka return off + len;
166 1.1 pooka }
167 1.1 pooka
168 1.1 pooka static inline uint32_t
169 1.1 pooka advance(uint32_t oldoff, uint32_t delta)
170 1.1 pooka {
171 1.1 pooka uint32_t newoff;
172 1.1 pooka
173 1.1 pooka newoff = oldoff + delta;
174 1.18 pooka if (newoff >= BUSMEM_DATASIZE)
175 1.18 pooka newoff -= (BUSMEM_DATASIZE);
176 1.1 pooka return newoff;
177 1.1 pooka
178 1.1 pooka }
179 1.1 pooka
180 1.1 pooka static uint32_t
181 1.1 pooka nextpktoff(struct shmif_sc *sc, uint32_t oldoff)
182 1.1 pooka {
183 1.1 pooka uint32_t oldlen;
184 1.1 pooka
185 1.15 pooka busread(sc, &oldlen, oldoff, PKTLEN_SIZE);
186 1.18 pooka KASSERT(oldlen < BUSMEM_DATASIZE);
187 1.1 pooka
188 1.15 pooka return advance(oldoff, PKTLEN_SIZE + oldlen);
189 1.1 pooka }
190 1.1 pooka
191 1.1 pooka int
192 1.1 pooka rump_shmif_create(const char *path, int *ifnum)
193 1.1 pooka {
194 1.1 pooka struct shmif_sc *sc;
195 1.1 pooka struct ifnet *ifp;
196 1.1 pooka uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0xa0, 0x00, 0x00, 0x00, 0x00 };
197 1.1 pooka uint32_t randnum;
198 1.1 pooka unsigned mynum;
199 1.1 pooka int error;
200 1.1 pooka
201 1.1 pooka randnum = arc4random();
202 1.15 pooka memcpy(&enaddr[2], &randnum, sizeof(randnum));
203 1.1 pooka mynum = atomic_inc_uint_nv(&numif)-1;
204 1.1 pooka
205 1.1 pooka sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
206 1.1 pooka ifp = &sc->sc_ec.ec_if;
207 1.1 pooka memcpy(sc->sc_myaddr, enaddr, sizeof(enaddr));
208 1.1 pooka
209 1.1 pooka sc->sc_memfd = rumpuser_open(path, O_RDWR | O_CREAT, &error);
210 1.1 pooka if (sc->sc_memfd == -1)
211 1.1 pooka goto fail;
212 1.1 pooka sc->sc_busmem = rumpuser_filemmap(sc->sc_memfd, 0, BUSMEM_SIZE,
213 1.5 pooka RUMPUSER_FILEMMAP_TRUNCATE | RUMPUSER_FILEMMAP_SHARED
214 1.5 pooka | RUMPUSER_FILEMMAP_READ | RUMPUSER_FILEMMAP_WRITE, &error);
215 1.1 pooka if (error)
216 1.1 pooka goto fail;
217 1.1 pooka
218 1.17 pooka if (sc->sc_busmem->shm_magic && sc->sc_busmem->shm_magic != SHMIF_MAGIC)
219 1.17 pooka panic("bus is not magical");
220 1.17 pooka sc->sc_busmem->shm_magic = SHMIF_MAGIC;
221 1.17 pooka
222 1.1 pooka lockbus(sc);
223 1.16 pooka sc->sc_nextpacket = sc->sc_busmem->shm_last;
224 1.16 pooka sc->sc_prevgen = sc->sc_busmem->shm_gen;
225 1.1 pooka unlockbus(sc);
226 1.1 pooka
227 1.1 pooka sc->sc_kq = rumpuser_writewatchfile_setup(-1, sc->sc_memfd, 0, &error);
228 1.1 pooka if (sc->sc_kq == -1)
229 1.1 pooka goto fail;
230 1.1 pooka
231 1.1 pooka sprintf(ifp->if_xname, "shmif%d", mynum);
232 1.1 pooka ifp->if_softc = sc;
233 1.13 pooka ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
234 1.1 pooka ifp->if_init = shmif_init;
235 1.1 pooka ifp->if_ioctl = shmif_ioctl;
236 1.1 pooka ifp->if_start = shmif_start;
237 1.1 pooka ifp->if_stop = shmif_stop;
238 1.1 pooka ifp->if_mtu = 1518;
239 1.1 pooka
240 1.1 pooka if_attach(ifp);
241 1.1 pooka ether_ifattach(ifp, enaddr);
242 1.1 pooka
243 1.12 pooka aprint_verbose("shmif%d: bus %s\n", mynum, path);
244 1.12 pooka aprint_verbose("shmif%d: Ethernet address %s\n",
245 1.12 pooka mynum, ether_sprintf(enaddr));
246 1.12 pooka
247 1.2 pooka if (ifnum)
248 1.2 pooka *ifnum = mynum;
249 1.1 pooka return 0;
250 1.1 pooka
251 1.1 pooka fail:
252 1.1 pooka panic("rump_shmemif_create: fixme");
253 1.1 pooka }
254 1.1 pooka
255 1.1 pooka static int
256 1.1 pooka shmif_init(struct ifnet *ifp)
257 1.1 pooka {
258 1.4 pooka int error = 0;
259 1.4 pooka
260 1.4 pooka if (rump_threads) {
261 1.4 pooka error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
262 1.4 pooka shmif_rcv, ifp, NULL, "shmif");
263 1.4 pooka } else {
264 1.4 pooka printf("WARNING: threads not enabled, shmif NOT working\n");
265 1.4 pooka }
266 1.1 pooka
267 1.1 pooka ifp->if_flags |= IFF_RUNNING;
268 1.4 pooka return error;
269 1.1 pooka }
270 1.1 pooka
271 1.1 pooka static int
272 1.1 pooka shmif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
273 1.1 pooka {
274 1.1 pooka int s, rv;
275 1.1 pooka
276 1.1 pooka s = splnet();
277 1.1 pooka rv = ether_ioctl(ifp, cmd, data);
278 1.7 pooka if (rv == ENETRESET)
279 1.7 pooka rv = 0;
280 1.1 pooka splx(s);
281 1.1 pooka
282 1.1 pooka return rv;
283 1.1 pooka }
284 1.1 pooka
285 1.1 pooka /* send everything in-context */
286 1.1 pooka static void
287 1.1 pooka shmif_start(struct ifnet *ifp)
288 1.1 pooka {
289 1.1 pooka struct shmif_sc *sc = ifp->if_softc;
290 1.1 pooka struct mbuf *m, *m0;
291 1.1 pooka uint32_t lastoff, dataoff, npktlenoff;
292 1.1 pooka uint32_t pktsize = 0;
293 1.1 pooka bool wrote = false;
294 1.1 pooka int error;
295 1.1 pooka
296 1.1 pooka for (;;) {
297 1.1 pooka IF_DEQUEUE(&ifp->if_snd, m0);
298 1.1 pooka if (m0 == NULL) {
299 1.1 pooka break;
300 1.1 pooka }
301 1.1 pooka
302 1.1 pooka lockbus(sc);
303 1.16 pooka lastoff = sc->sc_busmem->shm_last;
304 1.1 pooka
305 1.1 pooka npktlenoff = nextpktoff(sc, lastoff);
306 1.15 pooka dataoff = advance(npktlenoff, PKTLEN_SIZE);
307 1.1 pooka
308 1.1 pooka for (m = m0; m != NULL; m = m->m_next) {
309 1.1 pooka pktsize += m->m_len;
310 1.1 pooka dataoff = buswrite(sc, dataoff, mtod(m, void *),
311 1.1 pooka m->m_len);
312 1.1 pooka }
313 1.15 pooka buswrite(sc, npktlenoff, &pktsize, PKTLEN_SIZE);
314 1.16 pooka sc->sc_busmem->shm_last = npktlenoff;
315 1.1 pooka unlockbus(sc);
316 1.1 pooka
317 1.1 pooka m_freem(m0);
318 1.1 pooka wrote = true;
319 1.1 pooka
320 1.1 pooka DPRINTF(("shmif_start: send %d bytes at off %d\n",
321 1.1 pooka pktsize, npktlenoff));
322 1.1 pooka }
323 1.1 pooka /* wakeup */
324 1.1 pooka if (wrote)
325 1.15 pooka rumpuser_pwrite(sc->sc_memfd,
326 1.15 pooka &busversion, sizeof(busversion), IFMEM_WAKEUP, &error);
327 1.1 pooka }
328 1.1 pooka
329 1.1 pooka static void
330 1.1 pooka shmif_stop(struct ifnet *ifp, int disable)
331 1.1 pooka {
332 1.1 pooka
333 1.1 pooka panic("%s: unimpl", __func__);
334 1.1 pooka }
335 1.1 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.1 pooka struct mbuf *m = NULL;
342 1.1 pooka struct ether_header *eth;
343 1.1 pooka uint32_t nextpkt, pktlen, lastpkt, busgen, lastnext;
344 1.1 pooka int error;
345 1.1 pooka
346 1.1 pooka for (;;) {
347 1.1 pooka if (m == NULL) {
348 1.1 pooka m = m_gethdr(M_WAIT, MT_DATA);
349 1.1 pooka MCLGET(m, M_WAIT);
350 1.1 pooka }
351 1.1 pooka
352 1.1 pooka DPRINTF(("waiting %d/%d\n", sc->sc_nextpacket, sc->sc_prevgen));
353 1.1 pooka
354 1.1 pooka KASSERT(m->m_flags & M_EXT);
355 1.1 pooka lockbus(sc);
356 1.16 pooka lastpkt = sc->sc_busmem->shm_last;
357 1.16 pooka busgen = sc->sc_busmem->shm_gen;
358 1.1 pooka lastnext = nextpktoff(sc, lastpkt);
359 1.1 pooka if ((lastnext > sc->sc_nextpacket && busgen > sc->sc_prevgen)
360 1.1 pooka || (busgen > sc->sc_prevgen+1)) {
361 1.1 pooka nextpkt = lastpkt;
362 1.1 pooka sc->sc_prevgen = busgen;
363 1.11 pooka rumpuser_dprintf("shmif_rcv: generation overrun, "
364 1.11 pooka "skipping invalid packets\n");
365 1.1 pooka } else {
366 1.1 pooka nextpkt = sc->sc_nextpacket;
367 1.1 pooka }
368 1.1 pooka
369 1.1 pooka /* need more data? */
370 1.1 pooka if (lastnext == nextpkt && sc->sc_prevgen == busgen){
371 1.1 pooka unlockbus(sc);
372 1.1 pooka error = 0;
373 1.1 pooka rumpuser_writewatchfile_wait(sc->sc_kq, NULL, &error);
374 1.1 pooka if (__predict_false(error))
375 1.1 pooka printf("shmif_rcv: wait failed %d\n", error);
376 1.1 pooka continue;
377 1.1 pooka }
378 1.1 pooka
379 1.15 pooka busread(sc, &pktlen, nextpkt, PKTLEN_SIZE);
380 1.15 pooka busread(sc, mtod(m, void *),
381 1.15 pooka advance(nextpkt, PKTLEN_SIZE), pktlen);
382 1.1 pooka
383 1.1 pooka DPRINTF(("shmif_rcv: read packet of length %d at %d\n",
384 1.1 pooka pktlen, nextpkt));
385 1.1 pooka
386 1.1 pooka sc->sc_nextpacket = nextpktoff(sc, nextpkt);
387 1.1 pooka sc->sc_prevgen = busgen;
388 1.1 pooka unlockbus(sc);
389 1.1 pooka
390 1.1 pooka m->m_len = m->m_pkthdr.len = pktlen;
391 1.1 pooka m->m_pkthdr.rcvif = ifp;
392 1.1 pooka
393 1.10 pooka /* if it's from us, don't pass up and reuse storage space */
394 1.1 pooka eth = mtod(m, struct ether_header *);
395 1.1 pooka if (memcmp(eth->ether_shost, sc->sc_myaddr, 6) != 0) {
396 1.1 pooka ifp->if_input(ifp, m);
397 1.1 pooka m = NULL;
398 1.1 pooka }
399 1.1 pooka }
400 1.1 pooka
401 1.1 pooka panic("shmif_worker is a lazy boy %d\n", error);
402 1.1 pooka }
403