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