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