Home | History | Annotate | Line # | Download | only in dev
if_veth.c revision 1.9
      1 /* $NetBSD: if_veth.c,v 1.9 2017/10/23 09:31:17 msaitoh Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2011 Jared D. McNeill <jmcneill (at) 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.9 2017/10/23 09:31:17 msaitoh 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 	int rv;
    106 
    107 	sc->sc_dev = self;
    108 
    109 	pmf_device_register1(self, NULL, NULL, veth_shutdown);
    110 
    111 	sc->sc_tapfd = thunk_open_tap(taa->u.veth.device);
    112 	if (sc->sc_tapfd == -1) {
    113 		aprint_error(": couldn't open %s: %d\n",
    114 		    taa->u.veth.device, thunk_geterrno());
    115 		return;
    116 	}
    117 	if (ether_aton_r(sc->sc_eaddr, sizeof(sc->sc_eaddr),
    118 	    taa->u.veth.eaddr) != 0) {
    119 		aprint_error(": couldn't parse hw address '%s'\n",
    120 		    taa->u.veth.eaddr);
    121 		return;
    122 	}
    123 
    124 	aprint_naive("\n");
    125 	aprint_normal(": Virtual Ethernet (device = %s)\n", taa->u.veth.device);
    126 
    127 	aprint_normal_dev(self, "Ethernet address %s\n",
    128 	    ether_sprintf(sc->sc_eaddr));
    129 
    130 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
    131 	ifp->if_softc = sc;
    132 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    133 	ifp->if_ioctl = veth_ioctl;
    134 	ifp->if_watchdog = veth_watchdog;
    135 	ifp->if_start = veth_start;
    136 	ifp->if_init = veth_init;
    137 	ifp->if_stop = veth_stop;
    138 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
    139 	IFQ_SET_READY(&ifq->if_snd);
    140 
    141 	rv = if_initialize(ifp);
    142 	if (rv != 0) {
    143 		aprint_error_dev(self, "if_initialize failed(%d)\n", rv);
    144 		thunk_close(sc->sc_tapfd);
    145 		pmf_device_deregister(self);
    146 		return; /* Error */
    147 	}
    148 	ether_ifattach(ifp, sc->sc_eaddr);
    149 	if_register(ifp);
    150 
    151 	ifmedia_init(&sc->sc_ifmedia, 0,
    152 	    veth_ifmedia_change,
    153 	    veth_ifmedia_status);
    154 	ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
    155 	ifmedia_set(&sc->sc_ifmedia, IFM_ETHER|IFM_100_TX);
    156 
    157 	sc->sc_rx_intr = softint_establish(SOFTINT_NET, veth_softrx, sc);
    158 	if (sc->sc_rx_intr == NULL)
    159 		panic("couldn't establish veth rx softint");
    160 	sc->sc_tx_intr = softint_establish(SOFTINT_NET, veth_softtx, sc);
    161 	if (sc->sc_tx_intr == NULL)
    162 		panic("couldn't establish veth tx softint");
    163 
    164 	thunk_setown(sc->sc_tapfd);
    165 
    166 	sc->sc_rx_ih = sigio_intr_establish(veth_rx, sc);
    167 	if (sc->sc_rx_ih == NULL)
    168 		panic("couldn't establish veth rx interrupt");
    169 }
    170 
    171 static bool
    172 veth_shutdown(device_t self, int flags)
    173 {
    174 	struct veth_softc *sc = device_private(self);
    175 
    176 	if (sc->sc_tapfd != -1)
    177 		thunk_close(sc->sc_tapfd);
    178 
    179 	return true;
    180 }
    181 
    182 static int
    183 veth_init(struct ifnet *ifp)
    184 {
    185 	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
    186 
    187 	veth_stop(ifp, 0);
    188 
    189 	ifp->if_flags |= IFF_RUNNING;
    190 	ifp->if_flags &= ~IFF_OACTIVE;
    191 
    192 	return 0;
    193 }
    194 
    195 static int
    196 veth_rx(void *priv)
    197 {
    198 	struct veth_softc *sc = priv;
    199 
    200 	softint_schedule(sc->sc_rx_intr);
    201 	return 0;
    202 }
    203 
    204 static void
    205 veth_softrx(void *priv)
    206 {
    207 	struct veth_softc *sc = priv;
    208 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    209 	struct mbuf *m;
    210 	ssize_t len;
    211 	int s, avail;
    212 
    213 	for (;;) {
    214 		avail = thunk_pollin_tap(sc->sc_tapfd, 0);
    215 		if (avail == 0)
    216 			break;
    217 
    218 		len = thunk_read(sc->sc_tapfd, sc->sc_rx_buf,
    219 		    min(avail, sizeof(sc->sc_rx_buf)));
    220 		vethprintf("%s: read returned %d\n", __func__, len);
    221 		if (len == -1)
    222 			panic("read() from tap failed");
    223 
    224 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    225 		if (m == NULL) {
    226 			vethprintf("MGETHDR failed (input error)\n");
    227 			++ifp->if_ierrors;
    228 			continue;
    229 		}
    230 		if (len > MHLEN) {
    231 			MCLGET(m, M_DONTWAIT);
    232 			if ((m->m_flags & M_EXT) == 0) {
    233 				m_freem(m);
    234 				++ifp->if_ierrors;
    235 				vethprintf("M_EXT not set (input error)\n");
    236 				continue;
    237 			}
    238 		}
    239 		m_set_rcvif(m, ifp);
    240 		m->m_pkthdr.len = m->m_len = len;
    241 		memcpy(mtod(m, void *), sc->sc_rx_buf, len);
    242 
    243 		s = splnet();
    244 		if_input(ifp, m);
    245 		splx(s);
    246 	}
    247 }
    248 
    249 static void
    250 veth_softtx(void *priv)
    251 {
    252 	struct veth_softc *sc = priv;
    253 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    254 	int s;
    255 
    256 	if (ifp->if_flags & IFF_OACTIVE) {
    257 		if (thunk_pollout_tap(sc->sc_tapfd, 0) == 1)
    258 			ifp->if_flags &= ~IFF_OACTIVE;
    259 	}
    260 
    261 	s = splnet();
    262 	veth_start(ifp);
    263 	splx(s);
    264 }
    265 
    266 static void
    267 veth_start(struct ifnet *ifp)
    268 {
    269 	struct veth_softc *sc = ifp->if_softc;
    270 	struct mbuf *m0;
    271 	ssize_t len;
    272 
    273 	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
    274 
    275 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    276 		return;
    277 
    278 	for (;;) {
    279 		IFQ_POLL(&ifp->if_snd, m0);
    280 		if (m0 == NULL)
    281 			break;
    282 
    283 		if (thunk_pollout_tap(sc->sc_tapfd, 0) != 1) {
    284 			printf("queue full\n");
    285 			ifp->if_flags |= IFF_OACTIVE;
    286 			break;
    287 		}
    288 
    289 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    290 		bpf_mtap(ifp, m0);
    291 
    292 		m_copydata(m0, 0, m0->m_pkthdr.len, sc->sc_tx_buf);
    293 
    294 		vethprintf("write %d bytes...\n", m0->m_pkthdr.len);
    295 		len = thunk_write(sc->sc_tapfd, sc->sc_tx_buf,
    296 		    m0->m_pkthdr.len);
    297 		vethprintf("write returned %d\n", len);
    298 		if (len > 0)
    299 			++ifp->if_opackets;
    300 		else
    301 			++ifp->if_oerrors;
    302 		m_freem(m0);
    303 	}
    304 }
    305 
    306 static void
    307 veth_stop(struct ifnet *ifp, int disable)
    308 {
    309 	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
    310 	ifp->if_timer = 0;
    311 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    312 }
    313 
    314 static void
    315 veth_watchdog(struct ifnet *ifp)
    316 {
    317 	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
    318 	++ifp->if_oerrors;
    319 	veth_init(ifp);
    320 }
    321 
    322 static int
    323 veth_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    324 {
    325 	struct veth_softc *sc = ifp->if_softc;
    326 	struct ifreq *ifr;
    327 	int s, error;
    328 
    329 	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
    330 
    331 	s = splnet();
    332 
    333 	switch (cmd) {
    334 	case SIOCSIFMEDIA:
    335 	case SIOCGIFMEDIA:
    336 		ifr = data;
    337 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_ifmedia, cmd);
    338 		break;
    339 	case SIOCADDMULTI:
    340 	case SIOCDELMULTI:
    341 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
    342 			if (ifp->if_flags & IFF_RUNNING) {
    343 				veth_init(ifp);
    344 			}
    345 			error = 0;
    346 		}
    347 		break;
    348 	default:
    349 		error = ether_ioctl(ifp, cmd, data);
    350 		break;
    351 	}
    352 
    353 	splx(s);
    354 	return error;
    355 }
    356 
    357 static int
    358 veth_ifmedia_change(struct ifnet *ifp)
    359 {
    360 	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
    361 	return 0;
    362 }
    363 
    364 static void
    365 veth_ifmedia_status(struct ifnet *ifp, struct ifmediareq *req)
    366 {
    367 	vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
    368 	req->ifm_status |= IFM_ACTIVE | IFM_AVALID;
    369 	req->ifm_active = IFM_100_TX | IFM_FDX | IFM_ETHER;
    370 }
    371