1/* $NetBSD: if_veth.c,v 1.16 2022/09/18 13:36:53 thorpej 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.16 2022/09/18 13:36:53 thorpej 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
49static int	veth_match(device_t, cfdata_t, void *);
50static void	veth_attach(device_t, device_t, void *);
51static bool	veth_shutdown(device_t, int);
52
53static int	veth_init(struct ifnet *);
54static void	veth_start(struct ifnet *);
55static void	veth_stop(struct ifnet *, int);
56static void	veth_watchdog(struct ifnet *);
57static int	veth_ioctl(struct ifnet *, u_long, void *);
58
59static int	veth_rx(void *);
60static void	veth_softrx(void *);
61static void	veth_softtx(void *);
62
63static int	veth_ifmedia_change(struct ifnet *);
64static void	veth_ifmedia_status(struct ifnet *, struct ifmediareq *);
65
66#ifdef VETH_DEBUG
67#define vethprintf printf
68#else
69static inline void vethprintf(const char *fmt, ...) { }
70#endif
71
72struct 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
85CFATTACH_DECL_NEW(veth, sizeof(struct veth_softc),
86    veth_match, veth_attach, NULL, NULL);
87
88static int
89veth_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
99static void
100veth_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_initialize(ifp);
141	ether_ifattach(ifp, sc->sc_eaddr);
142	if_register(ifp);
143
144	/* Initialize ifmedia structures. */
145	sc->sc_ec.ec_ifmedia = &sc->sc_ifmedia;
146	ifmedia_init(&sc->sc_ifmedia, 0,
147	    veth_ifmedia_change, veth_ifmedia_status);
148	ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_100_TX, 0, NULL);
149	ifmedia_set(&sc->sc_ifmedia, IFM_ETHER | IFM_100_TX);
150
151	sc->sc_rx_intr = softint_establish(SOFTINT_NET, veth_softrx, sc);
152	if (sc->sc_rx_intr == NULL)
153		panic("couldn't establish veth rx softint");
154	sc->sc_tx_intr = softint_establish(SOFTINT_NET, veth_softtx, sc);
155	if (sc->sc_tx_intr == NULL)
156		panic("couldn't establish veth tx softint");
157
158	thunk_setown(sc->sc_tapfd);
159
160	sc->sc_rx_ih = sigio_intr_establish(veth_rx, sc);
161	if (sc->sc_rx_ih == NULL)
162		panic("couldn't establish veth rx interrupt");
163}
164
165static bool
166veth_shutdown(device_t self, int flags)
167{
168	struct veth_softc *sc = device_private(self);
169
170	if (sc->sc_tapfd != -1)
171		thunk_close(sc->sc_tapfd);
172
173	return true;
174}
175
176static int
177veth_init(struct ifnet *ifp)
178{
179	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
180
181	veth_stop(ifp, 0);
182
183	ifp->if_flags |= IFF_RUNNING;
184
185	return 0;
186}
187
188static int
189veth_rx(void *priv)
190{
191	struct veth_softc *sc = priv;
192
193	softint_schedule(sc->sc_rx_intr);
194	return 0;
195}
196
197static void
198veth_softrx(void *priv)
199{
200	struct veth_softc *sc = priv;
201	struct ifnet *ifp = &sc->sc_ec.ec_if;
202	struct mbuf *m;
203	ssize_t len;
204	int s, avail;
205
206	for (;;) {
207		avail = thunk_pollin_tap(sc->sc_tapfd, 0);
208		if (avail == 0)
209			break;
210
211		len = thunk_read(sc->sc_tapfd, sc->sc_rx_buf,
212		    uimin(avail, sizeof(sc->sc_rx_buf)));
213		vethprintf("%s: read returned %d\n", __func__, len);
214		if (len == -1)
215			panic("read() from tap failed");
216
217		MGETHDR(m, M_DONTWAIT, MT_DATA);
218		if (m == NULL) {
219			vethprintf("MGETHDR failed (input error)\n");
220			if_statinc(ifp, if_ierrors);
221			continue;
222		}
223		if (len > MHLEN) {
224			MCLGET(m, M_DONTWAIT);
225			if ((m->m_flags & M_EXT) == 0) {
226				m_freem(m);
227				if_statinc(ifp, if_ierrors);
228				vethprintf("M_EXT not set (input error)\n");
229				continue;
230			}
231		}
232		m_set_rcvif(m, ifp);
233		m->m_pkthdr.len = m->m_len = len;
234		memcpy(mtod(m, void *), sc->sc_rx_buf, len);
235
236		s = splnet();
237		if_input(ifp, m);
238		splx(s);
239	}
240}
241
242static void
243veth_softtx(void *priv)
244{
245	struct veth_softc *sc = priv;
246	struct ifnet *ifp = &sc->sc_ec.ec_if;
247	int s;
248
249	s = splnet();
250	veth_start(ifp);
251	splx(s);
252}
253
254static void
255veth_start(struct ifnet *ifp)
256{
257	struct veth_softc *sc = ifp->if_softc;
258	struct mbuf *m0;
259	ssize_t len;
260
261	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
262
263	if ((ifp->if_flags & IFF_RUNNING) == 0)
264		return;
265
266	for (;;) {
267		IFQ_POLL(&ifp->if_snd, m0);
268		if (m0 == NULL)
269			break;
270
271		if (thunk_pollout_tap(sc->sc_tapfd, 0) != 1) {
272			printf("queue full\n");
273			break;
274		}
275
276		IFQ_DEQUEUE(&ifp->if_snd, m0);
277		bpf_mtap(ifp, m0, BPF_D_OUT);
278
279		m_copydata(m0, 0, m0->m_pkthdr.len, sc->sc_tx_buf);
280
281		vethprintf("write %d bytes...\n", m0->m_pkthdr.len);
282		len = thunk_write(sc->sc_tapfd, sc->sc_tx_buf,
283		    m0->m_pkthdr.len);
284		vethprintf("write returned %d\n", len);
285		if (len > 0)
286			if_statinc(ifp, if_opackets);
287		else
288			if_statinc(ifp, if_oerrors);
289		m_freem(m0);
290	}
291}
292
293static void
294veth_stop(struct ifnet *ifp, int disable)
295{
296	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
297	ifp->if_timer = 0;
298	ifp->if_flags &= ~IFF_RUNNING;
299}
300
301static void
302veth_watchdog(struct ifnet *ifp)
303{
304	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
305	if_statinc(ifp, if_oerrors);
306	veth_init(ifp);
307}
308
309static int
310veth_ioctl(struct ifnet *ifp, u_long cmd, void *data)
311{
312	int s, error;
313
314	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
315
316	s = splnet();
317
318	switch (cmd) {
319	case SIOCADDMULTI:
320	case SIOCDELMULTI:
321		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
322			if (ifp->if_flags & IFF_RUNNING) {
323				veth_init(ifp);
324			}
325			error = 0;
326		}
327		break;
328	default:
329		error = ether_ioctl(ifp, cmd, data);
330		break;
331	}
332
333	splx(s);
334	return error;
335}
336
337static int
338veth_ifmedia_change(struct ifnet *ifp)
339{
340	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
341	return 0;
342}
343
344static void
345veth_ifmedia_status(struct ifnet *ifp, struct ifmediareq *req)
346{
347	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
348	req->ifm_status |= IFM_ACTIVE | IFM_AVALID;
349	req->ifm_active = IFM_100_TX | IFM_FDX | IFM_ETHER;
350}
351