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