Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch/usermode first cut at networking support for usermo...



details:   https://anonhg.NetBSD.org/src/rev/4da7d45859f8
branches:  trunk
changeset: 772246:4da7d45859f8
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Mon Dec 26 12:39:19 2011 +0000

description:
first cut at networking support for usermode, doesn't fully work yet but
enough to get an address with dhcp and answer arps

diffstat:

 sys/arch/usermode/conf/GENERIC.common |    6 +-
 sys/arch/usermode/conf/files.usermode |    6 +-
 sys/arch/usermode/dev/if_veth.c       |  348 ++++++++++++++++++++++++++++++++++
 sys/arch/usermode/dev/mainbus.c       |   13 +-
 sys/arch/usermode/include/mainbus.h   |    7 +-
 sys/arch/usermode/include/thunk.h     |    9 +-
 sys/arch/usermode/usermode/machdep.c  |   29 ++-
 sys/arch/usermode/usermode/thunk.c    |   76 +++++++-
 8 files changed, 482 insertions(+), 12 deletions(-)

diffs (truncated from 671 to 300 lines):

diff -r d97b24a81cc6 -r 4da7d45859f8 sys/arch/usermode/conf/GENERIC.common
--- a/sys/arch/usermode/conf/GENERIC.common     Mon Dec 26 12:29:38 2011 +0000
+++ b/sys/arch/usermode/conf/GENERIC.common     Mon Dec 26 12:39:19 2011 +0000
@@ -1,9 +1,9 @@
-# $NetBSD: GENERIC.common,v 1.1 2011/12/20 21:01:39 jmcneill Exp $
+# $NetBSD: GENERIC.common,v 1.2 2011/12/26 12:39:19 jmcneill Exp $
 
 include "arch/usermode/conf/std.usermode"
 
 options        INCLUDE_CONFIG_FILE
-#ident                 "GENERIC-$Revision: 1.1 $"
+#ident                 "GENERIC-$Revision: 1.2 $"
 maxusers       32
 
 makeoptions    DEBUG="-O1 -g3"
@@ -49,6 +49,7 @@
 clock0         at mainbus?
 ttycons0       at mainbus?
 ld0            at mainbus?
+veth0          at mainbus?
 
 #options               SDL
 #genfb0                at thunkbus?
@@ -56,6 +57,7 @@
 
 pseudo-device  loop
 pseudo-device  pty
+pseudo-device  bpfilter
 
 # Pull in optional local configuration
 cinclude "arch/usermode/conf/GENERIC.local"
diff -r d97b24a81cc6 -r 4da7d45859f8 sys/arch/usermode/conf/files.usermode
--- a/sys/arch/usermode/conf/files.usermode     Mon Dec 26 12:29:38 2011 +0000
+++ b/sys/arch/usermode/conf/files.usermode     Mon Dec 26 12:39:19 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: files.usermode,v 1.12 2011/12/20 21:01:39 jmcneill Exp $
+# $NetBSD: files.usermode,v 1.13 2011/12/26 12:39:19 jmcneill Exp $
 
 maxpartitions 8
 maxusers 8 16 64
@@ -27,6 +27,10 @@
 attach ttycons at thunkbus
 file   arch/usermode/dev/ttycons.c             ttycons
 
+device veth { } : arp, ether, ifnet
+attach veth at thunkbus
+file   arch/usermode/dev/if_veth.c             veth
+
 attach ld at thunkbus with ld_thunkbus
 file   arch/usermode/dev/ld_thunkbus.c         ld_thunkbus
 
diff -r d97b24a81cc6 -r 4da7d45859f8 sys/arch/usermode/dev/if_veth.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/usermode/dev/if_veth.c   Mon Dec 26 12:39:19 2011 +0000
@@ -0,0 +1,348 @@
+/* $NetBSD: if_veth.c,v 1.1 2011/12/26 12:39:19 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2011 Jared D. McNeill <jmcneill%invisible.ca@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: if_veth.c,v 1.1 2011/12/26 12:39:19 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/proc.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/mbuf.h>
+#include <sys/socket.h>
+#include <sys/device.h>
+
+#include <net/if.h>
+#include <net/if_ether.h>
+#include <net/if_media.h>
+
+#include <net/bpf.h>
+
+#include <machine/mainbus.h>
+#include <machine/thunk.h>
+
+static int     veth_match(device_t, cfdata_t, void *);
+static void    veth_attach(device_t, device_t, void *);
+
+static int     veth_init(struct ifnet *);
+static void    veth_start(struct ifnet *);
+static void    veth_stop(struct ifnet *, int);
+static void    veth_watchdog(struct ifnet *);
+static int     veth_ioctl(struct ifnet *, u_long, void *);
+
+static int     veth_rx(void *);
+static void    veth_softrx(void *);
+static void    veth_softtx(void *);
+
+static int     veth_ifmedia_change(struct ifnet *);
+static void    veth_ifmedia_status(struct ifnet *, struct ifmediareq *);
+
+#ifdef VETH_DEBUG
+#define vethprintf printf
+#else
+static inline void vethprintf(const char *fmt, ...) { }
+#endif
+
+struct veth_softc {
+       device_t                sc_dev;
+       struct ethercom         sc_ec;
+       struct ifmedia          sc_ifmedia;
+       int                     sc_tapfd;
+       uint8_t                 sc_eaddr[ETHER_ADDR_LEN];
+       uint8_t                 sc_rx_buf[4096 + 65536];
+       void                    *sc_rx_ih;
+       void                    *sc_rx_intr;
+       void                    *sc_tx_intr;
+};
+
+CFATTACH_DECL_NEW(veth, sizeof(struct veth_softc),
+    veth_match, veth_attach, NULL, NULL);
+
+static int
+veth_match(device_t parent, cfdata_t match, void *opaque)
+{
+       struct thunkbus_attach_args *taa = opaque;
+
+       if (taa->taa_type != THUNKBUS_TYPE_VETH)
+               return 0;
+
+       return 1;
+}
+
+static void
+veth_attach(device_t parent, device_t self, void *opaque)
+{
+       struct veth_softc *sc = device_private(self);
+       struct thunkbus_attach_args *taa = opaque;
+       struct ifnet *ifp = &sc->sc_ec.ec_if;
+
+       sc->sc_dev = self;
+       sc->sc_tapfd = thunk_open_tap(taa->u.veth.device);
+       if (sc->sc_tapfd == -1) {
+               aprint_error(": couldn't open %s: %d\n",
+                   taa->u.veth.device, thunk_geterrno());
+               return;
+       }
+       if (ether_aton_r(sc->sc_eaddr, sizeof(sc->sc_eaddr),
+           taa->u.veth.eaddr) != 0) {
+               aprint_error(": couldn't parse hw address '%s'\n",
+                   taa->u.veth.eaddr);
+               return;
+       }
+
+       aprint_naive("\n");
+       aprint_normal(": Virtual Ethernet (device = %s)\n", taa->u.veth.device);
+
+       aprint_normal_dev(self, "Ethernet address %s\n",
+           ether_sprintf(sc->sc_eaddr));
+
+       strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
+       ifp->if_softc = sc;
+       ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
+       ifp->if_ioctl = veth_ioctl;
+       ifp->if_watchdog = veth_watchdog;
+       ifp->if_start = veth_start;
+       ifp->if_init = veth_init;
+       ifp->if_stop = veth_stop;
+       IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
+       IFQ_SET_READY(&ifq->if_snd);
+
+       if_attach(ifp);
+       ether_ifattach(ifp, sc->sc_eaddr);
+
+       ifmedia_init(&sc->sc_ifmedia, 0,
+           veth_ifmedia_change,
+           veth_ifmedia_status);
+       ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
+       ifmedia_set(&sc->sc_ifmedia, IFM_ETHER|IFM_100_TX);
+
+       sc->sc_rx_intr = softint_establish(SOFTINT_NET, veth_softrx, sc);
+       if (sc->sc_rx_intr == NULL)
+               panic("couldn't establish veth rx softint");
+       sc->sc_tx_intr = softint_establish(SOFTINT_NET, veth_softtx, sc);
+       if (sc->sc_tx_intr == NULL)
+               panic("couldn't establish veth tx softint");
+
+       thunk_setown(sc->sc_tapfd);
+
+       sc->sc_rx_ih = sigio_intr_establish(veth_rx, sc);
+       if (sc->sc_rx_ih == NULL)
+               panic("couldn't establish veth rx interrupt");
+}
+
+static int
+veth_init(struct ifnet *ifp)
+{
+       vethprintf("%s: %s flags=%x\n", __func__, ifp->if_xname, ifp->if_flags);
+
+       veth_stop(ifp, 0);
+
+       ifp->if_flags |= IFF_RUNNING;
+       ifp->if_flags &= ~IFF_OACTIVE;
+
+       return 0;
+}
+
+static int
+veth_rx(void *priv)
+{
+       struct veth_softc *sc = priv;
+
+       softint_schedule(sc->sc_rx_intr);
+
+       return 0;
+}
+
+static void
+veth_softrx(void *priv)
+{
+       struct veth_softc *sc = priv;
+       struct ifnet *ifp = &sc->sc_ec.ec_if;
+       struct mbuf *m;
+       ssize_t len;
+       int s, avail;
+
+       for (;;) {
+               avail = thunk_pollin_tap(sc->sc_tapfd, 0);
+               if (avail == 0)
+                       break;
+
+               len = thunk_read(sc->sc_tapfd, sc->sc_rx_buf,
+                   min(avail, sizeof(sc->sc_rx_buf)));
+               vethprintf("%s: read returned %d\n", __func__, len);
+               if (len == -1)
+                       panic("read() from tap failed");
+
+               MGETHDR(m, M_DONTWAIT, MT_DATA);
+               if (m == NULL) {
+                       vethprintf("MGETHDR failed (input error)\n");
+                       ++ifp->if_ierrors;
+                       continue;
+               }
+               if (len > MHLEN) {
+                       MCLGET(m, M_DONTWAIT);
+                       if ((m->m_flags & M_EXT) == 0) {
+                               m_freem(m);
+                               ++ifp->if_ierrors;
+                               vethprintf("M_EXT not set (input error)\n");
+                               continue;
+                       }
+               }
+               m->m_pkthdr.rcvif = ifp;
+               m->m_pkthdr.len = m->m_len = len;
+               memcpy(mtod(m, void *), sc->sc_rx_buf, len);
+               ++ifp->if_ipackets;
+
+               bpf_mtap(ifp, m);
+
+               s = splnet();
+               ifp->if_input(ifp, m);
+               splx(s);
+       }
+}
+
+static void
+veth_softtx(void *priv)
+{
+       struct veth_softc *sc = priv;
+       struct ifnet *ifp = &sc->sc_ec.ec_if;
+       int s;
+
+       if (ifp->if_flags & IFF_OACTIVE) {
+               if (thunk_pollout_tap(sc->sc_tapfd, 0) == 1)
+                       ifp->if_flags &= ~IFF_OACTIVE;
+       }
+
+       s = splnet();
+       veth_start(ifp);
+       splx(s);
+}
+



Home | Main Index | Thread Index | Old Index