Module Name:    src
Committed By:   rmind
Date:           Sat Jan 28 01:02:27 UTC 2012

Modified Files:
        src/sys/net: if_tun.c if_tun.h

Log Message:
Replace tun_lock with mutex(9).  XXX: too far from being MP-safe yet.


To generate a diff of this commit:
cvs rdiff -u -r1.114 -r1.115 src/sys/net/if_tun.c
cvs rdiff -u -r1.16 -r1.17 src/sys/net/if_tun.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/net/if_tun.c
diff -u src/sys/net/if_tun.c:1.114 src/sys/net/if_tun.c:1.115
--- src/sys/net/if_tun.c:1.114	Fri Oct 28 22:08:14 2011
+++ src/sys/net/if_tun.c	Sat Jan 28 01:02:27 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_tun.c,v 1.114 2011/10/28 22:08:14 dyoung Exp $	*/
+/*	$NetBSD: if_tun.c,v 1.115 2012/01/28 01:02:27 rmind Exp $	*/
 
 /*
  * Copyright (c) 1988, Julian Onions <[email protected]>
@@ -15,7 +15,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.114 2011/10/28 22:08:14 dyoung Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.115 2012/01/28 01:02:27 rmind Exp $");
 
 #include "opt_inet.h"
 
@@ -36,6 +36,7 @@ __KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1
 #include <sys/conf.h>
 #include <sys/kauth.h>
 #include <sys/simplelock.h>
+#include <sys/mutex.h>
 #include <sys/cpu.h>
 
 #include <net/if.h>
@@ -126,7 +127,7 @@ tun_find_unit(dev_t dev)
 		if (unit == tp->tun_unit)
 			break;
 	if (tp)
-		simple_lock(&tp->tun_lock);
+		mutex_enter(&tp->tun_lock);
 	simple_unlock(&tun_softc_lock);
 
 	return (tp);
@@ -167,7 +168,7 @@ tun_clone_create(struct if_clone *ifc, i
 		tp = malloc(sizeof(*tp), M_DEVBUF, M_WAITOK|M_ZERO);
 
 		tp->tun_unit = unit;
-		simple_lock_init(&tp->tun_lock);
+		mutex_init(&tp->tun_lock, MUTEX_DEFAULT, IPL_NET);
 		selinit(&tp->tun_rsel);
 		selinit(&tp->tun_wsel);
 	} else {
@@ -224,9 +225,12 @@ tun_clone_destroy(struct ifnet *ifp)
 	struct tun_softc *tp = (void *)ifp;
 	int s, zombie = 0;
 
+	IF_PURGE(&ifp->if_snd);
+	ifp->if_flags &= ~IFF_RUNNING;
+
 	s = splnet();
 	simple_lock(&tun_softc_lock);
-	simple_lock(&tp->tun_lock);
+	mutex_enter(&tp->tun_lock);
 	LIST_REMOVE(tp, tun_list);
 	if (tp->tun_flags & TUN_OPEN) {
 		/* Hang on to storage until last close */
@@ -236,16 +240,13 @@ tun_clone_destroy(struct ifnet *ifp)
 	}
 	simple_unlock(&tun_softc_lock);
 
-	IF_PURGE(&ifp->if_snd);
-	ifp->if_flags &= ~IFF_RUNNING;
-
 	if (tp->tun_flags & TUN_RWAIT) {
 		tp->tun_flags &= ~TUN_RWAIT;
 		wakeup((void *)tp);
 	}
 	selnotify(&tp->tun_rsel, 0, 0);
 
-	simple_unlock(&tp->tun_lock);
+	mutex_exit(&tp->tun_lock);
 	splx(s);
 
 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
@@ -259,6 +260,7 @@ tun_clone_destroy(struct ifnet *ifp)
 		seldestroy(&tp->tun_wsel);
 		softint_disestablish(tp->tun_osih);
 		softint_disestablish(tp->tun_isih);
+		mutex_destroy(&tp->tun_lock);
 		free(tp, M_DEVBUF);
 	}
 
@@ -302,7 +304,7 @@ tunopen(dev_t dev, int flag, int mode, s
 	tp->tun_flags |= TUN_OPEN;
 	TUNDEBUG("%s: open\n", ifp->if_xname);
 out:
-	simple_unlock(&tp->tun_lock);
+	mutex_exit(&tp->tun_lock);
 out_nolock:
 	splx(s);
 	return (error);
@@ -327,6 +329,7 @@ tunclose(dev_t dev, int flag, int mode,
 		seldestroy(&tp->tun_wsel);
 		softint_disestablish(tp->tun_osih);
 		softint_disestablish(tp->tun_isih);
+		mutex_destroy(&tp->tun_lock);
 		free(tp, M_DEVBUF);
 		goto out_nolock;
 	}
@@ -338,6 +341,12 @@ tunclose(dev_t dev, int flag, int mode,
 
 	tp->tun_flags &= ~TUN_OPEN;
 
+	tp->tun_pgid = 0;
+	selnotify(&tp->tun_rsel, 0, 0);
+
+	TUNDEBUG ("%s: closed\n", ifp->if_xname);
+	mutex_exit(&tp->tun_lock);
+
 	/*
 	 * junk all pending output
 	 */
@@ -361,11 +370,6 @@ tunclose(dev_t dev, int flag, int mode,
 			}
 		}
 	}
-	tp->tun_pgid = 0;
-	selnotify(&tp->tun_rsel, 0, 0);
-
-	TUNDEBUG ("%s: closed\n", ifp->if_xname);
-	simple_unlock(&tp->tun_lock);
 out_nolock:
 	splx(s);
 	return (0);
@@ -382,7 +386,7 @@ tuninit(struct tun_softc *tp)
 
 	TUNDEBUG("%s: tuninit\n", ifp->if_xname);
 
-	simple_lock(&tp->tun_lock);
+	mutex_enter(&tp->tun_lock);
 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
 
 	tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
@@ -420,9 +424,7 @@ tuninit(struct tun_softc *tp)
 		}
 #endif /* INET6 */
 	}
-
-	simple_unlock(&tp->tun_lock);
-	return;
+	mutex_exit(&tp->tun_lock);
 }
 
 /*
@@ -499,13 +501,12 @@ tun_output(struct ifnet *ifp, struct mbu
 	ALTQ_DECL(struct altq_pktattr pktattr;)
 
 	s = splnet();
-	simple_lock(&tp->tun_lock);
+	mutex_enter(&tp->tun_lock);
 	TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
 
 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
 			  tp->tun_flags);
-		m_freem (m0);
 		error = EHOSTDOWN;
 		goto out;
 	}
@@ -548,11 +549,10 @@ tun_output(struct ifnet *ifp, struct mbu
 			af = mtod(m0,uint32_t *);
 			*af = htonl(dst->sa_family);
 		} else {
-#ifdef INET     
+#ifdef INET
 			if (dst->sa_family != AF_INET)
 #endif
 			{
-				m_freem(m0);
 				error = EAFNOSUPPORT;
 				goto out;
 			}
@@ -563,6 +563,7 @@ tun_output(struct ifnet *ifp, struct mbu
 		if (error) {
 			ifp->if_collisions++;
 			error = EAFNOSUPPORT;
+			m0 = NULL;
 			goto out;
 		}
 		mlen = m0->m_pkthdr.len;
@@ -571,7 +572,6 @@ tun_output(struct ifnet *ifp, struct mbu
 		break;
 #endif
 	default:
-		m_freem(m0);
 		error = EAFNOSUPPORT;
 		goto out;
 	}
@@ -585,9 +585,13 @@ tun_output(struct ifnet *ifp, struct mbu
 
 	selnotify(&tp->tun_rsel, 0, 0);
 out:
-	simple_unlock(&tp->tun_lock);
+	mutex_exit(&tp->tun_lock);
 	splx(s);
-	return (0);
+
+	if (error && m0) {
+		m_freem(m0);
+	}
+	return 0;
 }
 
 static void
@@ -711,7 +715,7 @@ tunioctl(dev_t dev, u_long cmd, void *da
 	}
 
 out:
-	simple_unlock(&tp->tun_lock);
+	mutex_exit(&tp->tun_lock);
 out_nolock:
 	splx(s);
 	return (error);
@@ -758,7 +762,7 @@ tunread(dev_t dev, struct uio *uio, int 
 				goto out;
 			}
 			tp->tun_flags |= TUN_RWAIT;
-			if (ltsleep((void *)tp, PZERO|PCATCH|PNORELOCK,
+			if (mtsleep((void *)tp, PZERO|PCATCH|PNORELOCK,
 					"tunread", 0, &tp->tun_lock) != 0) {
 				error = EINTR;
 				goto out_nolock;
@@ -782,7 +786,7 @@ tunread(dev_t dev, struct uio *uio, int 
 		}
 	} while (m0 == 0);
 
-	simple_unlock(&tp->tun_lock);
+	mutex_exit(&tp->tun_lock);
 	splx(s);
 
 	/* Copy the mbuf chain */
@@ -804,7 +808,7 @@ tunread(dev_t dev, struct uio *uio, int 
 	return (error);
 
 out:
-	simple_unlock(&tp->tun_lock);
+	mutex_exit(&tp->tun_lock);
 out_nolock:
 	splx(s);
 	return (error);
@@ -834,7 +838,7 @@ tunwrite(dev_t dev, struct uio *uio, int
 	}
 
 	/* Unlock until we've got the data */
-	simple_unlock(&tp->tun_lock);
+	mutex_exit(&tp->tun_lock);
 	splx(s);
 
 	ifp = &tp->tun_if;
@@ -933,7 +937,7 @@ tunwrite(dev_t dev, struct uio *uio, int
 	bpf_mtap_af(ifp, dst.sa_family, top);
 
 	s = splnet();
-	simple_lock(&tp->tun_lock);
+	mutex_enter(&tp->tun_lock);
 	if ((tp->tun_flags & TUN_INITED) == 0) {
 		/* Interface was destroyed */
 		error = ENXIO;
@@ -942,9 +946,10 @@ tunwrite(dev_t dev, struct uio *uio, int
 	if (IF_QFULL(ifq)) {
 		IF_DROP(ifq);
 		ifp->if_collisions++;
+		mutex_exit(&tp->tun_lock);
 		m_freem(top);
 		error = ENOBUFS;
-		goto out;
+		goto out_nolock;
 	}
 
 	IF_ENQUEUE(ifq, top);
@@ -952,7 +957,7 @@ tunwrite(dev_t dev, struct uio *uio, int
 	ifp->if_ibytes += tlen;
 	schednetisr(isr);
 out:
-	simple_unlock(&tp->tun_lock);
+	mutex_exit(&tp->tun_lock);
 out_nolock:
 	splx(s);
 out0:
@@ -976,7 +981,7 @@ tunstart(struct ifnet *ifp)
 	if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd))
 		return;
 
-	simple_lock(&tp->tun_lock);
+	mutex_enter(&tp->tun_lock);
 	if (!IF_IS_EMPTY(&ifp->if_snd)) {
 		if (tp->tun_flags & TUN_RWAIT) {
 			tp->tun_flags &= ~TUN_RWAIT;
@@ -987,7 +992,7 @@ tunstart(struct ifnet *ifp)
 
 		selnotify(&tp->tun_rsel, 0, 0);
 	}
-	simple_unlock(&tp->tun_lock);
+	mutex_exit(&tp->tun_lock);
 }
 #endif /* ALTQ */
 /*
@@ -1027,7 +1032,7 @@ tunpoll(dev_t dev, int events, struct lw
 	if (events & (POLLOUT | POLLWRNORM))
 		revents |= events & (POLLOUT | POLLWRNORM);
 
-	simple_unlock(&tp->tun_lock);
+	mutex_exit(&tp->tun_lock);
 out_nolock:
 	splx(s);
 	return (revents);
@@ -1105,7 +1110,7 @@ tunkqfilter(dev_t dev, struct knote *kn)
 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
 
 out:
-	simple_unlock(&tp->tun_lock);
+	mutex_exit(&tp->tun_lock);
 out_nolock:
 	splx(s);
 	return (rv);

Index: src/sys/net/if_tun.h
diff -u src/sys/net/if_tun.h:1.16 src/sys/net/if_tun.h:1.17
--- src/sys/net/if_tun.h:1.16	Thu Apr 24 15:35:31 2008
+++ src/sys/net/if_tun.h	Sat Jan 28 01:02:27 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_tun.h,v 1.16 2008/04/24 15:35:31 ad Exp $	*/
+/*	$NetBSD: if_tun.h,v 1.17 2012/01/28 01:02:27 rmind Exp $	*/
 
 /*
  * Copyright (c) 1988, Julian Onions <[email protected]>
@@ -41,7 +41,7 @@ struct tun_softc {
 	struct	selinfo	tun_rsel;	/* read select */
 	struct	selinfo	tun_wsel;	/* write select (not used) */
 	int	tun_unit;		/* the tunnel unit number */
-	struct	simplelock tun_lock;	/* lock for this tunnel */
+	kmutex_t tun_lock;		/* lock for this tunnel */
 	LIST_ENTRY(tun_softc) tun_list;	/* list of all tuns */
 	void	*tun_osih;		/* soft interrupt handle */
 	void	*tun_isih;		/* soft interrupt handle */

Reply via email to