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