Source-Changes-HG archive

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

[src/trunk]: src/sys/net Make MP-safe and use kmem(9)



details:   https://anonhg.NetBSD.org/src/rev/de80f4dc2928
branches:  trunk
changeset: 821166:de80f4dc2928
user:      skrll <skrll%NetBSD.org@localhost>
date:      Thu Jan 26 21:13:19 2017 +0000

description:
Make MP-safe and use kmem(9)

Mostly from rmind-smpnet

diffstat:

 sys/net/if_tun.c |  178 ++++++++++++++++++++----------------------------------
 sys/net/if_tun.h |    3 +-
 2 files changed, 68 insertions(+), 113 deletions(-)

diffs (truncated from 553 to 300 lines):

diff -r a9df11ea8097 -r de80f4dc2928 sys/net/if_tun.c
--- a/sys/net/if_tun.c  Thu Jan 26 20:15:44 2017 +0000
+++ b/sys/net/if_tun.c  Thu Jan 26 21:13:19 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_tun.c,v 1.135 2017/01/23 15:32:04 skrll Exp $       */
+/*     $NetBSD: if_tun.c,v 1.136 2017/01/26 21:13:19 skrll Exp $       */
 
 /*
  * Copyright (c) 1988, Julian Onions <jpo%cs.nott.ac.uk@localhost>
@@ -14,33 +14,35 @@
  * operation though.
  */
 
+/*
+ * tun - tunnel software network interface.
+ */
+
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.135 2017/01/23 15:32:04 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.136 2017/01/26 21:13:19 skrll Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
 #endif
 
 #include <sys/param.h>
+
 #include <sys/buf.h>
 #include <sys/conf.h>
 #include <sys/cpu.h>
 #include <sys/device.h>
-#include <sys/errno.h>
 #include <sys/file.h>
 #include <sys/ioctl.h>
 #include <sys/kauth.h>
+#include <sys/kmem.h>
+#include <sys/lwp.h>
 #include <sys/mbuf.h>
 #include <sys/module.h>
 #include <sys/mutex.h>
 #include <sys/poll.h>
-#include <sys/proc.h>
 #include <sys/select.h>
 #include <sys/signalvar.h>
 #include <sys/socket.h>
-#include <sys/syslog.h>
-#include <sys/systm.h>
-#include <sys/time.h>
 
 #include <net/bpf.h>
 #include <net/if.h>
@@ -107,7 +109,7 @@
        .d_mmap = nommap,
        .d_kqfilter = tunkqfilter,
        .d_discard = nodiscard,
-       .d_flag = D_OTHER
+       .d_flag = D_OTHER | D_MPSAFE
 };
 
 #ifdef _MODULE
@@ -194,10 +196,8 @@
        if (tp)
                LIST_REMOVE(tp, tun_list);
        mutex_exit(&tun_softc_lock);
-#ifdef DIAGNOSTIC
-       if (tp != NULL && (tp->tun_flags & (TUN_INITED|TUN_OPEN)) != TUN_OPEN)
-               printf("tun%d: inconsistent flags: %x\n", unit, tp->tun_flags);
-#endif
+       KASSERTMSG(!tp || (tp->tun_flags & (TUN_INITED|TUN_OPEN)) != TUN_OPEN,
+           "tun%d: inconsistent flags: %x", unit, tp->tun_flags);
 
        return tp;
 }
@@ -208,11 +208,11 @@
        struct tun_softc *tp;
 
        if ((tp = tun_find_zunit(unit)) == NULL) {
-               /* Allocate a new instance */
-               tp = malloc(sizeof(*tp), M_DEVBUF, M_WAITOK|M_ZERO);
+               tp = kmem_zalloc(sizeof(*tp), KM_SLEEP);
 
                tp->tun_unit = unit;
                mutex_init(&tp->tun_lock, MUTEX_DEFAULT, IPL_NET);
+               cv_init(&tp->tun_cv, "tunread");
                selinit(&tp->tun_rsel);
                selinit(&tp->tun_wsel);
        } else {
@@ -267,7 +267,7 @@
 tun_clone_destroy(struct ifnet *ifp)
 {
        struct tun_softc *tp = (void *)ifp;
-       int zombie = 0;
+       bool zombie = false;
 
        IF_PURGE(&ifp->if_snd);
        ifp->if_flags &= ~IFF_RUNNING;
@@ -276,16 +276,16 @@
        mutex_enter(&tp->tun_lock);
        LIST_REMOVE(tp, tun_list);
        if (tp->tun_flags & TUN_OPEN) {
-               /* Hang on to storage until last close */
-               zombie = 1;
+               /* Hang on to storage until last close. */
                tp->tun_flags &= ~TUN_INITED;
                LIST_INSERT_HEAD(&tunz_softc_list, tp, tun_list);
+               zombie = true;
        }
        mutex_exit(&tun_softc_lock);
 
        if (tp->tun_flags & TUN_RWAIT) {
                tp->tun_flags &= ~TUN_RWAIT;
-               wakeup((void *)tp);
+               cv_broadcast(&tp->tun_cv);
        }
        selnotify(&tp->tun_rsel, 0, 0);
 
@@ -303,7 +303,7 @@
                softint_disestablish(tp->tun_osih);
                softint_disestablish(tp->tun_isih);
                mutex_destroy(&tp->tun_lock);
-               free(tp, M_DEVBUF);
+               kmem_free(tp, sizeof(*tp));
        }
 
        return 0;
@@ -331,22 +331,21 @@
                (void)tun_clone_create(&tun_cloner, minor(dev));
                tp = tun_find_unit(dev);
                if (tp == NULL) {
-                       error = ENXIO;
-                       goto out_nolock;
+                       return ENXIO;
                }
        }
 
        if (tp->tun_flags & TUN_OPEN) {
-               error = EBUSY;
-               goto out;
+               mutex_exit(&tp->tun_lock);
+               return EBUSY;
        }
 
        ifp = &tp->tun_if;
        tp->tun_flags |= TUN_OPEN;
        TUNDEBUG("%s: open\n", ifp->if_xname);
-out:
+
        mutex_exit(&tp->tun_lock);
-out_nolock:
+
        return error;
 }
 
@@ -368,8 +367,8 @@
                softint_disestablish(tp->tun_osih);
                softint_disestablish(tp->tun_isih);
                mutex_destroy(&tp->tun_lock);
-               free(tp, M_DEVBUF);
-               goto out_nolock;
+               kmem_free(tp, sizeof(*tp));
+               return 0;
        }
 
        if ((tp = tun_find_unit(dev)) == NULL)
@@ -412,9 +411,6 @@
        return 0;
 }
 
-/*
- * Call at splnet().
- */
 static void
 tun_enable(struct tun_softc *tp, const struct ifaddr *ifa)
 {
@@ -473,12 +469,10 @@
 static int
 tun_ioctl(struct ifnet *ifp, u_long cmd, void *data)
 {
-       int             error = 0, s;
        struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc);
        struct ifreq *ifr = (struct ifreq *)data;
        struct ifaddr *ifa = (struct ifaddr *)data;
-
-       s = splnet();
+       int error = 0;
 
        switch (cmd) {
        case SIOCINITIFADDR:
@@ -522,7 +516,6 @@
                error = ifioctl_common(ifp, cmd, data);
        }
 
-       splx(s);
        return error;
 }
 
@@ -534,14 +527,12 @@
     const struct rtentry *rt)
 {
        struct tun_softc *tp = ifp->if_softc;
-       int             s;
        int             error;
 #if defined(INET) || defined(INET6)
        int             mlen;
        uint32_t        *af;
 #endif
 
-       s = splnet();
        mutex_enter(&tp->tun_lock);
        TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
 
@@ -551,6 +542,8 @@
                error = EHOSTDOWN;
                goto out;
        }
+       // XXXrmind
+       mutex_exit(&tp->tun_lock);
 
        /*
         * if the queueing discipline needs packet classification,
@@ -576,7 +569,7 @@
                                error = ENOBUFS;
                                goto out;
                        }
-                       bcopy(dst, mtod(m0, char *), dst->sa_len);
+                       memcpy(mtod(m0, char *), dst, dst->sa_len);
                }
 
                if (tp->tun_flags & TUN_IFHEAD) {
@@ -617,9 +610,10 @@
                goto out;
        }
 
+       mutex_enter(&tp->tun_lock);
        if (tp->tun_flags & TUN_RWAIT) {
                tp->tun_flags &= ~TUN_RWAIT;
-               wakeup((void *)tp);
+               cv_broadcast(&tp->tun_cv);
        }
        if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
                softint_schedule(tp->tun_isih);
@@ -627,7 +621,6 @@
        selnotify(&tp->tun_rsel, 0, 0);
 out:
        mutex_exit(&tp->tun_lock);
-       splx(s);
 
        if (error && m0) {
                m_freem(m0);
@@ -662,15 +655,13 @@
 tunioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
 {
        struct tun_softc *tp;
-       int s, error = 0;
+       int error = 0;
 
-       s = splnet();
        tp = tun_find_unit(dev);
 
        /* interface was "destroyed" already */
        if (tp == NULL) {
-               error = ENXIO;
-               goto out_nolock;
+               return ENXIO;
        }
 
        switch (cmd) {
@@ -757,8 +748,7 @@
 
 out:
        mutex_exit(&tp->tun_lock);
-out_nolock:
-       splx(s);
+
        return error;
 }
 
@@ -772,18 +762,15 @@
        struct tun_softc *tp;
        struct ifnet    *ifp;
        struct mbuf     *m, *m0;
-       int             error = 0, len, s, index;
+       int             error = 0, len;
 
-       s = splnet();
        tp = tun_find_unit(dev);
 
        /* interface was "destroyed" already */
        if (tp == NULL) {
-               error = ENXIO;
-               goto out_nolock;
+               return ENXIO;
        }
 
-       index = tp->tun_if.if_index;
        ifp = &tp->tun_if;
 
        TUNDEBUG ("%s: read\n", ifp->if_xname);
@@ -803,32 +790,14 @@
                                goto out;



Home | Main Index | Thread Index | Old Index