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