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