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