xref: /netbsd-src/sys/arch/usermode/dev/if_veth.c (revision e39ef1d61eee3ccba837ee281f1e098c864487aa)
1 /* $NetBSD: if_veth.c,v 1.4 2012/01/15 10:51:12 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 Jared D. McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: if_veth.c,v 1.4 2012/01/15 10:51:12 jmcneill Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/proc.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/mbuf.h>
37 #include <sys/socket.h>
38 #include <sys/device.h>
39 
40 #include <net/if.h>
41 #include <net/if_ether.h>
42 #include <net/if_media.h>
43 
44 #include <net/bpf.h>
45 
46 #include <machine/mainbus.h>
47 #include <machine/thunk.h>
48 
49 static int	veth_match(device_t, cfdata_t, void *);
50 static void	veth_attach(device_t, device_t, void *);
51 static bool	veth_shutdown(device_t, int);
52 
53 static int	veth_init(struct ifnet *);
54 static void	veth_start(struct ifnet *);
55 static void	veth_stop(struct ifnet *, int);
56 static void	veth_watchdog(struct ifnet *);
57 static int	veth_ioctl(struct ifnet *, u_long, void *);
58 
59 static int	veth_rx(void *);
60 static void	veth_softrx(void *);
61 static void	veth_softtx(void *);
62 
63 static int	veth_ifmedia_change(struct ifnet *);
64 static void	veth_ifmedia_status(struct ifnet *, struct ifmediareq *);
65 
66 #ifdef VETH_DEBUG
67 #define vethprintf printf
68 #else
69 static inline void vethprintf(const char *fmt, ...) { }
70 #endif
71 
72 struct veth_softc {
73 	device_t		sc_dev;
74 	struct ethercom		sc_ec;
75 	struct ifmedia		sc_ifmedia;
76 	int			sc_tapfd;
77 	uint8_t			sc_eaddr[ETHER_ADDR_LEN];
78 	uint8_t			sc_rx_buf[4096 + 65536];
79 	uint8_t			sc_tx_buf[4096 + 65536];
80 	void			*sc_rx_ih;
81 	void			*sc_rx_intr;
82 	void			*sc_tx_intr;
83 };
84 
85 CFATTACH_DECL_NEW(veth, sizeof(struct veth_softc),
86     veth_match, veth_attach, NULL, NULL);
87 
88 static int
89 veth_match(device_t parent, cfdata_t match, void *opaque)
90 {
91 	struct thunkbus_attach_args *taa = opaque;
92 
93 	if (taa->taa_type != THUNKBUS_TYPE_VETH)
94 		return 0;
95 
96 	return 1;
97 }
98 
99 static void
100 veth_attach(device_t parent, device_t self, void *opaque)
101 {
102 	struct veth_softc *sc = device_private(self);
103 	struct thunkbus_attach_args *taa = opaque;
104 	struct ifnet *ifp = &sc->sc_ec.ec_if;
105 
106 	sc->sc_dev = self;
107 
108 	pmf_device_register1(self, NULL, NULL, veth_shutdown);
109 
110 	sc->sc_tapfd = thunk_open_tap(taa->u.veth.device);
111 	if (sc->sc_tapfd == -1) {
112 		aprint_error(": couldn't open %s: %d\n",
113 		    taa->u.veth.device, thunk_geterrno());
114 		return;
115 	}
116 	if (ether_aton_r(sc->sc_eaddr, sizeof(sc->sc_eaddr),
117 	    taa->u.veth.eaddr) != 0) {
118 		aprint_error(": couldn't parse hw address '%s'\n",
119 		    taa->u.veth.eaddr);
120 		return;
121 	}
122 
123 	aprint_naive("\n");
124 	aprint_normal(": Virtual Ethernet (device = %s)\n", taa->u.veth.device);
125 
126 	aprint_normal_dev(self, "Ethernet address %s\n",
127 	    ether_sprintf(sc->sc_eaddr));
128 
129 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
130 	ifp->if_softc = sc;
131 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
132 	ifp->if_ioctl = veth_ioctl;
133 	ifp->if_watchdog = veth_watchdog;
134 	ifp->if_start = veth_start;
135 	ifp->if_init = veth_init;
136 	ifp->if_stop = veth_stop;
137 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
138 	IFQ_SET_READY(&ifq->if_snd);
139 
140 	if_attach(ifp);
141 	ether_ifattach(ifp, sc->sc_eaddr);
142 
143 	ifmedia_init(&sc->sc_ifmedia, 0,
144 	    veth_ifmedia_change,
145 	    veth_ifmedia_status);
146 	ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
147 	ifmedia_set(&sc->sc_ifmedia, IFM_ETHER|IFM_100_TX);
148 
149 	sc->sc_rx_intr = softint_establish(SOFTINT_NET, veth_softrx, sc);
150 	if (sc->sc_rx_intr == NULL)
151 		panic("couldn't establish veth rx softint");
152 	sc->sc_tx_intr = softint_establish(SOFTINT_NET, veth_softtx, sc);
153 	if (sc->sc_tx_intr == NULL)
154 		panic("couldn't establish veth tx softint");
155 
156 	thunk_setown(sc->sc_tapfd);
157 
158 	sc->sc_rx_ih = sigio_intr_establish(veth_rx, sc);
159 	if (sc->sc_rx_ih == NULL)
160 		panic("couldn't establish veth rx interrupt");
161 }
162 
163 static bool
164 veth_shutdown(device_t self, int flags)
165 {
166 	struct veth_softc *sc = device_private(self);
167 
168 	if (sc->sc_tapfd != -1)
169 		thunk_close(sc->sc_tapfd);
170 
171 	return true;
172 }
173 
174 static int
175 veth_init(struct ifnet *ifp)
176 {
177 	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
178 
179 	veth_stop(ifp, 0);
180 
181 	ifp->if_flags |= IFF_RUNNING;
182 	ifp->if_flags &= ~IFF_OACTIVE;
183 
184 	return 0;
185 }
186 
187 static int
188 veth_rx(void *priv)
189 {
190 	struct veth_softc *sc = priv;
191 
192 	curcpu()->ci_idepth++;
193 	spl_intr(IPL_NET, softint_schedule, sc->sc_rx_intr);
194 	curcpu()->ci_idepth--;
195 
196 	return 0;
197 }
198 
199 static void
200 veth_softrx(void *priv)
201 {
202 	struct veth_softc *sc = priv;
203 	struct ifnet *ifp = &sc->sc_ec.ec_if;
204 	struct mbuf *m;
205 	ssize_t len;
206 	int s, avail;
207 
208 	for (;;) {
209 		avail = thunk_pollin_tap(sc->sc_tapfd, 0);
210 		if (avail == 0)
211 			break;
212 
213 		len = thunk_read(sc->sc_tapfd, sc->sc_rx_buf,
214 		    min(avail, sizeof(sc->sc_rx_buf)));
215 		vethprintf("%s: read returned %d\n", __func__, len);
216 		if (len == -1)
217 			panic("read() from tap failed");
218 
219 		MGETHDR(m, M_DONTWAIT, MT_DATA);
220 		if (m == NULL) {
221 			vethprintf("MGETHDR failed (input error)\n");
222 			++ifp->if_ierrors;
223 			continue;
224 		}
225 		if (len > MHLEN) {
226 			MCLGET(m, M_DONTWAIT);
227 			if ((m->m_flags & M_EXT) == 0) {
228 				m_freem(m);
229 				++ifp->if_ierrors;
230 				vethprintf("M_EXT not set (input error)\n");
231 				continue;
232 			}
233 		}
234 		m->m_pkthdr.rcvif = ifp;
235 		m->m_pkthdr.len = m->m_len = len;
236 		memcpy(mtod(m, void *), sc->sc_rx_buf, len);
237 		++ifp->if_ipackets;
238 
239 		bpf_mtap(ifp, m);
240 
241 		s = splnet();
242 		ifp->if_input(ifp, m);
243 		splx(s);
244 	}
245 }
246 
247 static void
248 veth_softtx(void *priv)
249 {
250 	struct veth_softc *sc = priv;
251 	struct ifnet *ifp = &sc->sc_ec.ec_if;
252 	int s;
253 
254 	if (ifp->if_flags & IFF_OACTIVE) {
255 		if (thunk_pollout_tap(sc->sc_tapfd, 0) == 1)
256 			ifp->if_flags &= ~IFF_OACTIVE;
257 	}
258 
259 	s = splnet();
260 	veth_start(ifp);
261 	splx(s);
262 }
263 
264 static void
265 veth_start(struct ifnet *ifp)
266 {
267 	struct veth_softc *sc = ifp->if_softc;
268 	struct mbuf *m0;
269 	ssize_t len;
270 
271 	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
272 
273 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
274 		return;
275 
276 	for (;;) {
277 		IFQ_POLL(&ifp->if_snd, m0);
278 		if (m0 == NULL)
279 			break;
280 
281 		if (thunk_pollout_tap(sc->sc_tapfd, 0) != 1) {
282 			printf("queue full\n");
283 			ifp->if_flags |= IFF_OACTIVE;
284 			break;
285 		}
286 
287 		IFQ_DEQUEUE(&ifp->if_snd, m0);
288 		bpf_mtap(ifp, m0);
289 
290 		m_copydata(m0, 0, m0->m_pkthdr.len, sc->sc_tx_buf);
291 
292 		vethprintf("write %d bytes...\n", m0->m_pkthdr.len);
293 		len = thunk_write(sc->sc_tapfd, sc->sc_tx_buf,
294 		    m0->m_pkthdr.len);
295 		vethprintf("write returned %d\n", len);
296 		if (len > 0)
297 			++ifp->if_opackets;
298 		else
299 			++ifp->if_oerrors;
300 		m_freem(m0);
301 	}
302 }
303 
304 static void
305 veth_stop(struct ifnet *ifp, int disable)
306 {
307 	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
308 	ifp->if_timer = 0;
309 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
310 }
311 
312 static void
313 veth_watchdog(struct ifnet *ifp)
314 {
315 	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
316 	++ifp->if_oerrors;
317 	veth_init(ifp);
318 }
319 
320 static int
321 veth_ioctl(struct ifnet *ifp, u_long cmd, void *data)
322 {
323 	struct veth_softc *sc = ifp->if_softc;
324 	struct ifreq *ifr;
325 	int s, error;
326 
327 	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
328 
329 	s = splnet();
330 
331 	switch (cmd) {
332 	case SIOCSIFMEDIA:
333 	case SIOCGIFMEDIA:
334 		ifr = data;
335 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_ifmedia, cmd);
336 		break;
337 	case SIOCADDMULTI:
338 	case SIOCDELMULTI:
339 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
340 			if (ifp->if_flags & IFF_RUNNING) {
341 				veth_init(ifp);
342 			}
343 			error = 0;
344 		}
345 		break;
346 	default:
347 		error = ether_ioctl(ifp, cmd, data);
348 		break;
349 	}
350 
351 	splx(s);
352 	return error;
353 }
354 
355 static int
356 veth_ifmedia_change(struct ifnet *ifp)
357 {
358 	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
359 	return 0;
360 }
361 
362 static void
363 veth_ifmedia_status(struct ifnet *ifp, struct ifmediareq *req)
364 {
365 	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
366 	req->ifm_status |= IFM_ACTIVE | IFM_AVALID;
367 	req->ifm_active = IFM_100_TX | IFM_FDX | IFM_ETHER;
368 }
369