if_shmem.c revision 1.6.4.3 1 /* $NetBSD: if_shmem.c,v 1.6.4.3 2009/07/23 23:32:56 jym 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.6.4.3 2009/07/23 23:32:56 jym 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 1, 1, &error);
201 if (error)
202 goto fail;
203
204 lockbus(sc);
205 if (*BUSCTRL_ATOFF(sc, IFMEM_LASTPACKET) == 0)
206 *BUSCTRL_ATOFF(sc, IFMEM_LASTPACKET) = IFMEM_DATA;
207 sc->sc_nextpacket = *BUSCTRL_ATOFF(sc, IFMEM_LASTPACKET);
208 sc->sc_prevgen = *BUSCTRL_ATOFF(sc, IFMEM_GENERATION);
209 unlockbus(sc);
210
211 sc->sc_kq = rumpuser_writewatchfile_setup(-1, sc->sc_memfd, 0, &error);
212 if (sc->sc_kq == -1)
213 goto fail;
214
215 sprintf(ifp->if_xname, "shmif%d", mynum);
216 ifp->if_softc = sc;
217 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
218 ifp->if_init = shmif_init;
219 ifp->if_ioctl = shmif_ioctl;
220 ifp->if_start = shmif_start;
221 ifp->if_stop = shmif_stop;
222 ifp->if_mtu = 1518;
223
224 if_attach(ifp);
225 ether_ifattach(ifp, enaddr);
226
227 if (ifnum)
228 *ifnum = mynum;
229 return 0;
230
231 fail:
232 panic("rump_shmemif_create: fixme");
233 }
234
235 static int
236 shmif_init(struct ifnet *ifp)
237 {
238 int error = 0;
239
240 if (rump_threads) {
241 error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
242 shmif_rcv, ifp, NULL, "shmif");
243 } else {
244 printf("WARNING: threads not enabled, shmif NOT working\n");
245 }
246
247 ifp->if_flags |= IFF_RUNNING;
248 return error;
249 }
250
251 static int
252 shmif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
253 {
254 int s, rv;
255
256 s = splnet();
257 rv = ether_ioctl(ifp, cmd, data);
258 if (rv == ENETRESET)
259 rv = 0;
260 splx(s);
261
262 return rv;
263 }
264
265 /* send everything in-context */
266 static void
267 shmif_start(struct ifnet *ifp)
268 {
269 struct shmif_sc *sc = ifp->if_softc;
270 struct mbuf *m, *m0;
271 uint32_t lastoff, dataoff, npktlenoff;
272 uint32_t pktsize = 0;
273 bool wrote = false;
274 int error;
275
276 for (;;) {
277 IF_DEQUEUE(&ifp->if_snd, m0);
278 if (m0 == NULL) {
279 break;
280 }
281
282 lockbus(sc);
283 lastoff = *BUSCTRL_ATOFF(sc, IFMEM_LASTPACKET);
284
285 npktlenoff = nextpktoff(sc, lastoff);
286 dataoff = advance(npktlenoff, 4);
287
288 for (m = m0; m != NULL; m = m->m_next) {
289 pktsize += m->m_len;
290 dataoff = buswrite(sc, dataoff, mtod(m, void *),
291 m->m_len);
292 }
293 buswrite(sc, npktlenoff, &pktsize, 4);
294 *BUSCTRL_ATOFF(sc, IFMEM_LASTPACKET) = npktlenoff;
295 unlockbus(sc);
296
297 m_freem(m0);
298 wrote = true;
299
300 DPRINTF(("shmif_start: send %d bytes at off %d\n",
301 pktsize, npktlenoff));
302 }
303 /* wakeup */
304 if (wrote)
305 rumpuser_pwrite(sc->sc_memfd, &error, 4, IFMEM_WAKEUP, &error);
306 }
307
308 static void
309 shmif_stop(struct ifnet *ifp, int disable)
310 {
311
312 panic("%s: unimpl", __func__);
313 }
314
315 static void
316 shmif_rcv(void *arg)
317 {
318 struct ifnet *ifp = arg;
319 struct shmif_sc *sc = ifp->if_softc;
320 struct mbuf *m = NULL;
321 struct ether_header *eth;
322 uint32_t nextpkt, pktlen, lastpkt, busgen, lastnext;
323 int error;
324
325 for (;;) {
326 if (m == NULL) {
327 m = m_gethdr(M_WAIT, MT_DATA);
328 MCLGET(m, M_WAIT);
329 }
330
331 DPRINTF(("waiting %d/%d\n", sc->sc_nextpacket, sc->sc_prevgen));
332
333 KASSERT(m->m_flags & M_EXT);
334 lockbus(sc);
335 lastpkt = *BUSCTRL_ATOFF(sc, IFMEM_LASTPACKET);
336 busgen = *BUSCTRL_ATOFF(sc, IFMEM_GENERATION);
337 lastnext = nextpktoff(sc, lastpkt);
338 if ((lastnext > sc->sc_nextpacket && busgen > sc->sc_prevgen)
339 || (busgen > sc->sc_prevgen+1)) {
340 nextpkt = lastpkt;
341 sc->sc_prevgen = busgen;
342 printf("DROPPING\n");
343 } else {
344 nextpkt = sc->sc_nextpacket;
345 }
346
347 /* need more data? */
348 if (lastnext == nextpkt && sc->sc_prevgen == busgen){
349 unlockbus(sc);
350 error = 0;
351 rumpuser_writewatchfile_wait(sc->sc_kq, NULL, &error);
352 if (__predict_false(error))
353 printf("shmif_rcv: wait failed %d\n", error);
354 continue;
355 }
356
357 busread(sc, &pktlen, nextpkt, 4);
358 busread(sc, mtod(m, void *), advance(nextpkt, 4), pktlen);
359
360 DPRINTF(("shmif_rcv: read packet of length %d at %d\n",
361 pktlen, nextpkt));
362
363 sc->sc_nextpacket = nextpktoff(sc, nextpkt);
364 sc->sc_prevgen = busgen;
365 unlockbus(sc);
366
367 m->m_len = m->m_pkthdr.len = pktlen;
368 m->m_pkthdr.rcvif = ifp;
369
370 /* if it's to us, don't pass up and reuse storage space */
371 eth = mtod(m, struct ether_header *);
372 if (memcmp(eth->ether_shost, sc->sc_myaddr, 6) != 0) {
373 ifp->if_input(ifp, m);
374 m = NULL;
375 }
376 }
377
378 panic("shmif_worker is a lazy boy %d\n", error);
379 }
380