Module Name:    src
Committed By:   christos
Date:           Sun Aug  7 17:38:34 UTC 2016

Modified Files:
        src/sys/net: if_faith.c if_gif.c if_gre.c if_loop.c if_mpls.c if_ppp.c
            if_pppoe.c if_sl.c if_srt.c if_stf.c if_strip.c if_tap.c if_tun.c
            if_vlan.c
        src/sys/net/agr: if_agr.c
Added Files:
        src/sys/net: if_module.h

Log Message:
modularize some more drivers and merge the module glue


To generate a diff of this commit:
cvs rdiff -u -r1.53 -r1.54 src/sys/net/if_faith.c
cvs rdiff -u -r1.119 -r1.120 src/sys/net/if_gif.c
cvs rdiff -u -r1.169 -r1.170 src/sys/net/if_gre.c
cvs rdiff -u -r1.89 -r1.90 src/sys/net/if_loop.c
cvs rdiff -u -r0 -r1.1 src/sys/net/if_module.h
cvs rdiff -u -r1.26 -r1.27 src/sys/net/if_mpls.c
cvs rdiff -u -r1.156 -r1.157 src/sys/net/if_ppp.c
cvs rdiff -u -r1.113 -r1.114 src/sys/net/if_pppoe.c
cvs rdiff -u -r1.125 -r1.126 src/sys/net/if_sl.c
cvs rdiff -u -r1.22 -r1.23 src/sys/net/if_srt.c
cvs rdiff -u -r1.97 -r1.98 src/sys/net/if_stf.c
cvs rdiff -u -r1.105 -r1.106 src/sys/net/if_strip.c
cvs rdiff -u -r1.84 -r1.85 src/sys/net/if_tap.c
cvs rdiff -u -r1.127 -r1.128 src/sys/net/if_tun.c
cvs rdiff -u -r1.90 -r1.91 src/sys/net/if_vlan.c
cvs rdiff -u -r1.38 -r1.39 src/sys/net/agr/if_agr.c

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_faith.c
diff -u src/sys/net/if_faith.c:1.53 src/sys/net/if_faith.c:1.54
--- src/sys/net/if_faith.c:1.53	Fri Jun 10 09:27:16 2016
+++ src/sys/net/if_faith.c	Sun Aug  7 13:38:33 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_faith.c,v 1.53 2016/06/10 13:27:16 ozaki-r Exp $	*/
+/*	$NetBSD: if_faith.c,v 1.54 2016/08/07 17:38:33 christos Exp $	*/
 /*	$KAME: if_faith.c,v 1.21 2001/02/20 07:59:26 itojun Exp $	*/
 
 /*
@@ -40,9 +40,11 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_faith.c,v 1.53 2016/06/10 13:27:16 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_faith.c,v 1.54 2016/08/07 17:38:33 christos Exp $");
 
+#ifdef _KERNEL_OPT
 #include "opt_inet.h"
+#endif
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -53,6 +55,9 @@ __KERNEL_RCSID(0, "$NetBSD: if_faith.c,v
 #include <sys/ioctl.h>
 #include <sys/time.h>
 #include <sys/queue.h>
+#include <sys/device.h>
+#include <sys/module.h>
+#include <sys/atomic.h>
 
 #include <sys/cpu.h>
 
@@ -98,15 +103,40 @@ static struct if_clone faith_cloner =
 
 #define	FAITHMTU	1500
 
+static u_int faith_count;
+
 /* ARGSUSED */
 void
 faithattach(int count)
 {
 
+	/*
+	 * Nothing to do here, initialization is handled by the
+	 * module initialization code in faithinit() below).
+	 */
+}
+
+static void
+faithinit(void)
+{
 	if_clone_attach(&faith_cloner);
 }
 
 static int
+faithdetach(void)
+{
+	int error = 0;
+
+	if (faith_count != 0)
+		error = EBUSY;
+
+	if (error == 0)
+		if_clone_detach(&faith_cloner);
+
+	return error;
+}
+
+static int
 faith_clone_create(struct if_clone *ifc, int unit)
 {
 	struct ifnet *ifp;
@@ -127,6 +157,7 @@ faith_clone_create(struct if_clone *ifc,
 	if_attach(ifp);
 	if_alloc_sadl(ifp);
 	bpf_attach(ifp, DLT_NULL, sizeof(u_int));
+	atomic_inc_uint(&faith_count);
 	return (0);
 }
 
@@ -138,6 +169,7 @@ faith_clone_destroy(struct ifnet *ifp)
 	if_detach(ifp);
 	if_free(ifp);
 
+	atomic_dec_uint(&faith_count);
 	return (0);
 }
 
@@ -294,3 +326,10 @@ faithprefix(struct in6_addr *in6)
 	return ret;
 }
 #endif
+
+/*
+ * Module infrastructure
+ */
+#include "if_module.h"
+
+IF_MODULE(MODULE_CLASS_DRIVER, faith, "")

Index: src/sys/net/if_gif.c
diff -u src/sys/net/if_gif.c:1.119 src/sys/net/if_gif.c:1.120
--- src/sys/net/if_gif.c:1.119	Mon Jul  4 00:43:46 2016
+++ src/sys/net/if_gif.c	Sun Aug  7 13:38:33 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_gif.c,v 1.119 2016/07/04 04:43:46 knakahara Exp $	*/
+/*	$NetBSD: if_gif.c,v 1.120 2016/08/07 17:38:33 christos Exp $	*/
 /*	$KAME: if_gif.c,v 1.76 2001/08/20 02:01:02 kjc Exp $	*/
 
 /*
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_gif.c,v 1.119 2016/07/04 04:43:46 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_gif.c,v 1.120 2016/08/07 17:38:33 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -55,6 +55,8 @@ __KERNEL_RCSID(0, "$NetBSD: if_gif.c,v 1
 #include <sys/kmem.h>
 #include <sys/sysctl.h>
 #include <sys/xcall.h>
+#include <sys/device.h>
+#include <sys/module.h>
 
 #include <net/if.h>
 #include <net/if_types.h>
@@ -106,8 +108,6 @@ static int	gif_set_tunnel(struct ifnet *
 			       struct sockaddr *);
 static void	gif_delete_tunnel(struct ifnet *);
 
-static void	gif_sysctl_setup(struct sysctllog **);
-
 static int	gif_clone_create(struct if_clone *, int);
 static int	gif_clone_destroy(struct ifnet *);
 static int	gif_check_nesting(struct ifnet *, struct mbuf *);
@@ -132,12 +132,15 @@ static struct if_clone gif_cloner =
 #endif
 static int max_gif_nesting = MAX_GIF_NEST;
 
+static struct sysctllog *gif_sysctl;
+
 static void
-gif_sysctl_setup(struct sysctllog **clog)
+gif_sysctl_setup(void)
 {
+	gif_sysctl = NULL;
 
 #ifdef INET
-	sysctl_createv(clog, 0, NULL, NULL,
+	sysctl_createv(&gif_sysctl, 0, NULL, NULL,
 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
 		       CTLTYPE_INT, "gifttl",
 		       SYSCTL_DESCR("Default TTL for a gif tunnel datagram"),
@@ -146,7 +149,7 @@ gif_sysctl_setup(struct sysctllog **clog
 		       IPCTL_GIF_TTL, CTL_EOL);
 #endif
 #ifdef INET6
-	sysctl_createv(clog, 0, NULL, NULL,
+	sysctl_createv(&gif_sysctl, 0, NULL, NULL,
 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
 		       CTLTYPE_INT, "gifhlim",
 		       SYSCTL_DESCR("Default hop limit for a gif tunnel datagram"),
@@ -160,11 +163,36 @@ gif_sysctl_setup(struct sysctllog **clog
 void
 gifattach(int count)
 {
+	/*
+	 * Nothing to do here, initialization is handled by the
+	 * module initialization code in gifinit() below).
+	 */
+}
+
+static void
+gifinit(void)
+{
 
 	LIST_INIT(&gif_softc_list);
 	if_clone_attach(&gif_cloner);
 
-	gif_sysctl_setup(NULL);
+	gif_sysctl_setup();
+}
+
+static int
+gifdetach(void)
+{
+	int error = 0;
+
+	if (!LIST_EMPTY(&gif_softc_list))
+		error = EBUSY;
+
+	if (error == 0) {
+		if_clone_detach(&gif_cloner);
+		sysctl_teardown(&gif_sysctl);
+	}
+
+	return error;
 }
 
 static int
@@ -1037,3 +1065,10 @@ gif_delete_tunnel(struct ifnet *ifp)
 	splx(s);
 #endif
 }
+
+/*
+ * Module infrastructure
+ */
+#include "if_module.h"
+
+IF_MODULE(MODULE_CLASS_DRIVER, gif, "")

Index: src/sys/net/if_gre.c
diff -u src/sys/net/if_gre.c:1.169 src/sys/net/if_gre.c:1.170
--- src/sys/net/if_gre.c:1.169	Fri Jun 10 09:27:16 2016
+++ src/sys/net/if_gre.c	Sun Aug  7 13:38:33 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_gre.c,v 1.169 2016/06/10 13:27:16 ozaki-r Exp $ */
+/*	$NetBSD: if_gre.c,v 1.170 2016/08/07 17:38:33 christos Exp $ */
 
 /*
  * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
@@ -45,7 +45,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.169 2016/06/10 13:27:16 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.170 2016/08/07 17:38:33 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_atalk.h"
@@ -71,6 +71,8 @@ __KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1
 #include <sys/systm.h>
 #include <sys/sysctl.h>
 #include <sys/kauth.h>
+#include <sys/device.h>
+#include <sys/module.h>
 
 #include <sys/kernel.h>
 #include <sys/mutex.h>
@@ -84,6 +86,9 @@ __KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1
 #include <net/if_types.h>
 #include <net/netisr.h>
 #include <net/route.h>
+#include <sys/device.h>
+#include <sys/module.h>
+#include <sys/atomic.h>
 
 #include <netinet/in_systm.h>
 #include <netinet/in.h>
@@ -142,6 +147,8 @@ int gre_debug = 0;
 
 int ip_gre_ttl = GRE_TTL;
 
+static u_int gre_count;
+
 static int gre_clone_create(struct if_clone *, int);
 static int gre_clone_destroy(struct ifnet *);
 
@@ -317,6 +324,7 @@ gre_clone_create(struct if_clone *ifc, i
 	if_attach(&sc->sc_if);
 	if_alloc_sadl(&sc->sc_if);
 	bpf_attach(&sc->sc_if, DLT_NULL, sizeof(uint32_t));
+	atomic_inc_uint(&gre_count);
 	return 0;
 
 fail1:	cv_destroy(&sc->sc_fp_condvar);
@@ -358,6 +366,7 @@ gre_clone_destroy(struct ifnet *ifp)
 	gre_evcnt_detach(sc);
 	free(sc, M_DEVBUF);
 
+	atomic_dec_uint(&gre_count);
 	return 0;
 }
 
@@ -1434,5 +1443,36 @@ out:
 void
 greattach(int count)
 {
+
+	/*
+	 * Nothing to do here, initialization is handled by the
+	 * module initialization code in greinit() below).
+	 */
+}
+
+static void
+greinit(void)
+{
 	if_clone_attach(&gre_cloner);
 }
+
+static int
+gredetach(void)
+{
+	int error = 0;
+
+	if (gre_count != 0)
+		error = EBUSY;
+
+	if (error == 0)
+		if_clone_detach(&gre_cloner);
+
+	return error;
+}
+
+/*
+ * Module infrastructure
+ */
+#include "if_module.h"
+
+IF_MODULE(MODULE_CLASS_DRIVER, gre, "")

Index: src/sys/net/if_loop.c
diff -u src/sys/net/if_loop.c:1.89 src/sys/net/if_loop.c:1.90
--- src/sys/net/if_loop.c:1.89	Wed Jun 22 06:44:32 2016
+++ src/sys/net/if_loop.c	Sun Aug  7 13:38:34 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_loop.c,v 1.89 2016/06/22 10:44:32 knakahara Exp $	*/
+/*	$NetBSD: if_loop.c,v 1.90 2016/08/07 17:38:34 christos Exp $	*/
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -65,7 +65,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_loop.c,v 1.89 2016/06/22 10:44:32 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_loop.c,v 1.90 2016/08/07 17:38:34 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -83,6 +83,8 @@ __KERNEL_RCSID(0, "$NetBSD: if_loop.c,v 
 #include <sys/errno.h>
 #include <sys/ioctl.h>
 #include <sys/time.h>
+#include <sys/device.h>
+#include <sys/module.h>
 
 #include <sys/cpu.h>
 
@@ -142,11 +144,28 @@ void
 loopattach(int n)
 {
 
+	/*
+	 * Nothing to do here, initialization is handled by the
+	 * module initialization code in loopnit() below).
+	 */
+}
+
+static void
+loopinit(void)
+{
+
 	(void)loop_clone_create(&loop_cloner, 0);	/* lo0 always exists */
 	if_clone_attach(&loop_cloner);
 }
 
 static int
+loopdetach(void)
+{
+	/* no detach for now; we don't allow lo0 to be deleted */
+	return EBUSY;
+}
+
+static int
 loop_clone_create(struct if_clone *ifc, int unit)
 {
 	struct ifnet *ifp;
@@ -487,3 +506,10 @@ loioctl(struct ifnet *ifp, u_long cmd, v
 	}
 	return (error);
 }
+
+/*
+ * Module infrastructure
+ */
+#include "if_module.h"
+
+IF_MODULE(MODULE_CLASS_DRIVER, loop, "")

Index: src/sys/net/if_mpls.c
diff -u src/sys/net/if_mpls.c:1.26 src/sys/net/if_mpls.c:1.27
--- src/sys/net/if_mpls.c:1.26	Thu Jul  7 02:55:43 2016
+++ src/sys/net/if_mpls.c	Sun Aug  7 13:38:34 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_mpls.c,v 1.26 2016/07/07 06:55:43 msaitoh Exp $ */
+/*	$NetBSD: if_mpls.c,v 1.27 2016/08/07 17:38:34 christos Exp $ */
 
 /*
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_mpls.c,v 1.26 2016/07/07 06:55:43 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_mpls.c,v 1.27 2016/08/07 17:38:34 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -49,6 +49,9 @@ __KERNEL_RCSID(0, "$NetBSD: if_mpls.c,v 
 #include <net/if_types.h>
 #include <net/netisr.h>
 #include <net/route.h>
+#include <sys/device.h>
+#include <sys/module.h>
+#include <sys/atomic.h>
 
 #ifdef INET
 #include <netinet/in.h>
@@ -108,21 +111,46 @@ static struct mbuf *mpls_label_inet6(str
 static struct mbuf *mpls_prepend_shim(struct mbuf *, union mpls_shim *);
 
 extern int mpls_defttl, mpls_mapttl_inet, mpls_mapttl_inet6, mpls_icmp_respond,
-	mpls_forwarding, mpls_frame_accept, mpls_mapprec_inet, mpls_mapclass_inet6,
-	mpls_rfc4182;
+    mpls_forwarding, mpls_frame_accept, mpls_mapprec_inet, mpls_mapclass_inet6,
+    mpls_rfc4182;
 
+static u_int mpls_count;
 /* ARGSUSED */
 void
-ifmplsattach(int count)
+mplsattach(int count)
+{
+	/*
+	 * Nothing to do here, initialization is handled by the
+	 * module initialization code in mplsinit() below).
+	 */
+}
+
+static void
+mplsinit(void)
 {
 	if_clone_attach(&mpls_if_cloner);
 }
 
 static int
+mplsdetach(void)
+{
+	int error = 0;
+
+	if (mpls_count != 0)
+		error = EBUSY;
+
+	if (error == 0)
+		if_clone_detach(&mpls_if_cloner);
+
+	return error;
+}
+
+static int
 mpls_clone_create(struct if_clone *ifc, int unit)
 {
 	struct mpls_softc *sc;
 
+	atomic_inc_uint(&mpls_count);
 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
 
 	if_initname(&sc->sc_if, ifc->ifc_name, unit);
@@ -155,6 +183,7 @@ mpls_clone_destroy(struct ifnet *ifp)
 	splx(s);
 
 	free(ifp->if_softc, M_DEVBUF);
+	atomic_dec_uint(&mpls_count);
 	return 0;
 }
 
@@ -659,3 +688,10 @@ mpls_prepend_shim(struct mbuf *m, union 
 
 	return m;
 }
+
+/*
+ * Module infrastructure
+ */
+#include "if_module.h"
+
+IF_MODULE(MODULE_CLASS_DRIVER, mpls, "")

Index: src/sys/net/if_ppp.c
diff -u src/sys/net/if_ppp.c:1.156 src/sys/net/if_ppp.c:1.157
--- src/sys/net/if_ppp.c:1.156	Sat Aug  6 18:54:34 2016
+++ src/sys/net/if_ppp.c	Sun Aug  7 13:38:34 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_ppp.c,v 1.156 2016/08/06 22:54:34 pgoyette Exp $	*/
+/*	$NetBSD: if_ppp.c,v 1.157 2016/08/07 17:38:34 christos Exp $	*/
 /*	Id: if_ppp.c,v 1.6 1997/03/04 03:33:00 paulus Exp 	*/
 
 /*
@@ -102,7 +102,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_ppp.c,v 1.156 2016/08/06 22:54:34 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_ppp.c,v 1.157 2016/08/07 17:38:34 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "ppp.h"
@@ -243,6 +243,8 @@ pppattach(int n __unused)
 static void
 pppinit(void)
 {
+	/* Init the compressor sub-sub-system */
+	ppp_compressor_init();
 
 	if (ttyldisc_attach(&ppp_disc) != 0)
 		panic("%s", __func__);
@@ -263,7 +265,11 @@ pppdetach(void)
 	if (error == 0)
 		error = ttyldisc_detach(&ppp_disc);
 
-	mutex_destroy(&ppp_list_lock);
+	if (error == 0) {
+		mutex_destroy(&ppp_list_lock);
+		if_clone_detach(&ppp_cloner);
+		ppp_compressor_destroy();
+	}
 
 	return error;
 }
@@ -1944,6 +1950,7 @@ ppp_unregister_compressor(struct compres
 /*
  * Module infrastructure
  */
+#include "if_module.h"
 
 #ifdef PPP_FILTER
 #define PPP_DEP "bpf_filter,"
@@ -1951,63 +1958,4 @@ ppp_unregister_compressor(struct compres
 #define PPP_DEP
 #endif
 
-MODULE(MODULE_CLASS_DRIVER, if_ppp, PPP_DEP "slcompress");
-
-#ifdef _MODULE
-CFDRIVER_DECL(ppp, DV_IFNET, NULL);
-#endif
-
-static int
-if_ppp_modcmd(modcmd_t cmd, void *arg)
-{
-	int error = 0;
-
-	switch (cmd) {
-	case MODULE_CMD_INIT:
-		/* Init the compressor sub-sub-system */
-		ppp_compressor_init();
-
-#ifdef _MODULE
-		error = config_cfdriver_attach(&ppp_cd);
-		if (error) {
-			aprint_error("%s: unable to register cfdriver for"
-			    "%s, error %d\n", __func__, ppp_cd.cd_name, error);
-			ppp_compressor_destroy();
-			break;
-		}
-
-#endif
-		/* Init the unit list and line discipline stuff */
-		pppinit();
-		break;
-
-	case MODULE_CMD_FINI:
-		/*
-		 * Make sure it's ok to detach - no units left, and
-		 * line discipline is removed
-		 */
-		error = pppdetach();
-		if (error != 0)
-			break;
-#ifdef _MODULE
-		/* Remove device from autoconf database */
-		error = config_cfdriver_detach(&ppp_cd);
-		if (error) {
-			aprint_error("%s: failed to detach %s cfdriver, "
-			    "error %d\n", __func__, ppp_cd.cd_name, error);
-			break;
-		}
-#endif
-		ppp_compressor_destroy();
-		break;
-
-	case MODULE_CMD_STAT:
-		error = ENOTTY;
-		break;
-	default:
-		error = ENOTTY;
-		break;
-	}
-
-	return error;
-}
+IF_MODULE(MODULE_CLASS_DRIVER, ppp, PPP_DEP "slcompress")

Index: src/sys/net/if_pppoe.c
diff -u src/sys/net/if_pppoe.c:1.113 src/sys/net/if_pppoe.c:1.114
--- src/sys/net/if_pppoe.c:1.113	Sat Aug  6 21:59:43 2016
+++ src/sys/net/if_pppoe.c	Sun Aug  7 13:38:34 2016
@@ -1,4 +1,4 @@
-/* $NetBSD: if_pppoe.c,v 1.113 2016/08/07 01:59:43 pgoyette Exp $ */
+/* $NetBSD: if_pppoe.c,v 1.114 2016/08/07 17:38:34 christos Exp $ */
 
 /*-
  * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_pppoe.c,v 1.113 2016/08/07 01:59:43 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_pppoe.c,v 1.114 2016/08/07 17:38:34 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "pppoe.h"
@@ -224,8 +224,8 @@ pppoeattach(int count)
 {
 
 	/*
-	 * Nothing to do here - all initialization happens as part
-	 * of module init.
+	 * Nothing to do here, initialization is handled by the
+	 * module initialization code in pppoeinit() below).
 	 */
 }
 
@@ -238,16 +238,25 @@ pppoeinit(void)
 
 	pppoe_softintr = softint_establish(SOFTINT_NET, pppoe_softintr_handler,
 	    NULL);
+	sysctl_net_pppoe_setup(&pppoe_sysctl_clog);
 }
 
 static int
 pppoedetach(void)
 {
+	int error = 0;
 
-	softint_disestablish(pppoe_softintr);
-	if_clone_detach(&pppoe_cloner);
+	if (!LIST_EMPTY(&pppoe_softc_list))
+		error = EBUSY;
 
-	return 0;
+	if (error == 0) {
+		if_clone_detach(&pppoe_cloner);
+		softint_disestablish(pppoe_softintr);
+		/* Remove our sysctl sub-tree */
+		sysctl_teardown(&pppoe_sysctl_clog);
+	}
+
+	return error;
 }
 
 static int
@@ -1658,71 +1667,6 @@ pppoedisc_input(struct ifnet *ifp, struc
 	return;
 }
 
-/*
- * Module glue
- */
-MODULE(MODULE_CLASS_DRIVER, if_pppoe, "sppp_subr");
-
-#ifdef _MODULE  
-CFDRIVER_DECL(pppoe, DV_IFNET, NULL);
-#endif
- 
-static int
-if_pppoe_modcmd(modcmd_t cmd, void *arg)
-{
-	int error = 0;
-
-	switch (cmd) {
-	case MODULE_CMD_INIT:
-#ifdef _MODULE
-		error = config_cfdriver_attach(&pppoe_cd);
-		if (error) {
-			aprint_error("%s: unable to register cfdriver for"
-			    "%s, error %d\n", __func__, pppoe_cd.cd_name,
-			    error);
-			break;
-		}
-#endif  
-		/* Init the cloner etc. */
-		pppoeinit();
-
-#ifdef _MODULE
-		/* Create our sysctl subtree */
-		sysctl_net_pppoe_setup(&pppoe_sysctl_clog);
-#endif
-		break;
-
-        case MODULE_CMD_FINI:
-		/*
-		 * Make sure it's ok to detach - no units left, and
-		 * line discipline is removed
-		 */
-		if (!LIST_EMPTY(&pppoe_softc_list)) {
-			error = EBUSY;
-			break;
-		}
-		if ((error = pppoedetach()) != 0)
-			break;
-#ifdef _MODULE
-		/* Remove device from autoconf database */
-		if ((error = config_cfdriver_detach(&pppoe_cd)) != 0) {
-			aprint_error("%s: failed to detach %s cfdriver, error "
-			    "%d\n", __func__, pppoe_cd.cd_name, error);
-			break;
-		}
-		/* Remove our sysctl sub-tree */
-		sysctl_teardown(&pppoe_sysctl_clog);
-#endif
-
-		break;
-	case MODULE_CMD_STAT:
-	case MODULE_CMD_AUTOUNLOAD:
-	default:
-		error = ENOTTY;
-	}
-	return error;
-}
-
 SYSCTL_SETUP(sysctl_net_pppoe_setup, "sysctl net.pppoe subtree setup")
 {
 	const struct sysctlnode *node = NULL;
@@ -1746,3 +1690,10 @@ SYSCTL_SETUP(sysctl_net_pppoe_setup, "sy
 	    CTL_CREATE, CTL_EOL);
 #endif
 }
+
+/*
+ * Module infrastructure
+ */
+#include "if_module.h"
+
+IF_MODULE(MODULE_CLASS_DRIVER, pppoe, "sppp_subr")

Index: src/sys/net/if_sl.c
diff -u src/sys/net/if_sl.c:1.125 src/sys/net/if_sl.c:1.126
--- src/sys/net/if_sl.c:1.125	Sat Aug  6 08:48:23 2016
+++ src/sys/net/if_sl.c	Sun Aug  7 13:38:34 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_sl.c,v 1.125 2016/08/06 12:48:23 christos Exp $	*/
+/*	$NetBSD: if_sl.c,v 1.126 2016/08/07 17:38:34 christos Exp $	*/
 
 /*
  * Copyright (c) 1987, 1989, 1992, 1993
@@ -60,7 +60,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_sl.c,v 1.125 2016/08/06 12:48:23 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_sl.c,v 1.126 2016/08/07 17:38:34 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -248,6 +248,9 @@ sldetach(void)
 	if (error == 0)
 		error = ttyldisc_detach(&slip_disc);
 
+	if (error == 0)
+		if_clone_detach(&sl_cloner);
+
 	return error;
 }
 
@@ -1068,58 +1071,6 @@ slioctl(struct ifnet *ifp, u_long cmd, v
  * Module infrastructure
  */
 
-MODULE(MODULE_CLASS_DRIVER, if_sl, "slcompress");
-
-#ifdef _MODULE
-CFDRIVER_DECL(sl, DV_IFNET, NULL);
-#endif
-
-static int
-if_sl_modcmd(modcmd_t cmd, void *arg)
-{
-	int error = 0;
-
-	switch (cmd) {
-	case MODULE_CMD_INIT:
-#ifdef _MODULE
-		error = config_cfdriver_attach(&sl_cd);
-		if (error) {
-			aprint_error("%s: unable to register cfdriver for"
-			    "%s, error %d\n", __func__, sl_cd.cd_name, error);
-			break;
-		}
-
-#endif
-		/* Init the unit list and line discipline stuff */
-		slinit();
-		break;
-
-	case MODULE_CMD_FINI:
-		/*
-		 * Make sure it's ok to detach - no units left, and
-		 * line discipline is removed
-		 */
-		error = sldetach();
-		if (error != 0)
-			break;
-#ifdef _MODULE
-		/* Remove device from autoconf database */
-		error = config_cfdriver_detach(&sl_cd);
-		if (error) {
-			aprint_error("%s: failed to detach %s cfdriver, "
-			    "error %d\n", __func__, sl_cd.cd_name, error);
-			break;
-		}
-#endif
-		break;
+#include "if_module.h"
 
-	case MODULE_CMD_STAT:
-		error = ENOTTY;
-		break;
-	default:
-		error = ENOTTY;
-		break;
-	}
-
-	return error;
-}
+IF_MODULE(MODULE_CLASS_DRIVER, sl, "slcompress");

Index: src/sys/net/if_srt.c
diff -u src/sys/net/if_srt.c:1.22 src/sys/net/if_srt.c:1.23
--- src/sys/net/if_srt.c:1.22	Mon Jun 20 02:46:37 2016
+++ src/sys/net/if_srt.c	Sun Aug  7 13:38:34 2016
@@ -1,8 +1,8 @@
-/* $NetBSD: if_srt.c,v 1.22 2016/06/20 06:46:37 knakahara Exp $ */
+/* $NetBSD: if_srt.c,v 1.23 2016/08/07 17:38:34 christos Exp $ */
 /* This file is in the public domain. */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_srt.c,v 1.22 2016/06/20 06:46:37 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_srt.c,v 1.23 2016/08/07 17:38:34 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -31,6 +31,8 @@ __KERNEL_RCSID(0, "$NetBSD: if_srt.c,v 1
 #include <sys/fcntl.h>
 #include <sys/param.h>
 #include <sys/ioctl.h>
+#include <sys/module.h>
+#include <sys/device.h>
 #include <netinet/ip.h>
 #include <netinet/ip6.h>
 #include <net/if_types.h>
@@ -51,11 +53,13 @@ struct srt_softc {
 #define SKF_CDEVOPEN 0x00000001
 };
 
-void srtattach(void);
+#include "ioconf.h"
 
 static struct srt_softc *softcv[SRT_MAXUNIT+1];
 static unsigned int global_flags;
 
+static u_int srt_count;
+
 /* Internal routines. */
 
 static unsigned int ipv4_masks[33] = {
@@ -264,6 +268,7 @@ srt_clone_create(struct if_clone *cl, in
 	bpf_attach(&sc->intf, 0, 0);
 #endif
 	softcv[unit] = sc;
+	atomic_inc_uint(&srt_count);
 	return 0;
 }
 
@@ -288,6 +293,7 @@ srt_clone_destroy(struct ifnet *ifp)
 	}
 	softcv[sc->unit] = 0;
 	free(sc,M_DEVBUF);
+	atomic_inc_uint(&srt_count);
 	return 0;
 }
 
@@ -295,16 +301,44 @@ struct if_clone srt_clone =
     IF_CLONE_INITIALIZER("srt",&srt_clone_create,&srt_clone_destroy);
 
 void
-srtattach(void)
+srtattach(int n)
+{
+
+	/*
+	 * Nothing to do here, initialization is handled by the
+	 * module initialization code in srtinit() below).
+	 */
+}
+
+static void
+srtinit(void)
 {
 	int i;
 
-	for (i=SRT_MAXUNIT;i>=0;i--)
+	for (i = SRT_MAXUNIT; i >= 0; i--)
 		softcv[i] = 0;
 	global_flags = 0;
 	if_clone_attach(&srt_clone);
 }
 
+static int
+srtdetach(void)
+{
+	int error = 0;
+	int i;
+
+	for (i = SRT_MAXUNIT; i >= 0; i--)
+		if(softcv[i]) {
+			error = EBUSY;
+			break;
+		}
+
+	if (error == 0)
+		if_clone_detach(&srt_clone);
+
+	return error;
+}
+
 /* Special-device interface. */
 
 static int
@@ -497,3 +531,10 @@ const struct cdevsw srt_cdevsw = {
 	.d_discard = nodiscard,
 	.d_flag = D_OTHER
 };
+
+/*
+ * Module infrastructure
+ */
+#include "if_module.h"
+
+IF_MODULE(MODULE_CLASS_DRIVER, srt, "")

Index: src/sys/net/if_stf.c
diff -u src/sys/net/if_stf.c:1.97 src/sys/net/if_stf.c:1.98
--- src/sys/net/if_stf.c:1.97	Sun Jul 31 23:15:30 2016
+++ src/sys/net/if_stf.c	Sun Aug  7 13:38:34 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_stf.c,v 1.97 2016/08/01 03:15:30 ozaki-r Exp $	*/
+/*	$NetBSD: if_stf.c,v 1.98 2016/08/07 17:38:34 christos Exp $	*/
 /*	$KAME: if_stf.c,v 1.62 2001/06/07 22:32:16 itojun Exp $ */
 
 /*
@@ -75,10 +75,12 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_stf.c,v 1.97 2016/08/01 03:15:30 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_stf.c,v 1.98 2016/08/07 17:38:34 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
+#include "stf.h"
+#include "gif.h"	/*XXX*/
 #endif
 
 #ifndef INET6
@@ -95,6 +97,8 @@ __KERNEL_RCSID(0, "$NetBSD: if_stf.c,v 1
 #include <sys/proc.h>
 #include <sys/queue.h>
 #include <sys/syslog.h>
+#include <sys/device.h>
+#include <sys/module.h>
 
 #include <sys/cpu.h>
 
@@ -120,9 +124,6 @@ __KERNEL_RCSID(0, "$NetBSD: if_stf.c,v 1
 
 #include <net/net_osdep.h>
 
-#include "stf.h"
-#include "gif.h"	/*XXX*/
-
 #include <net/bpf.h>
 
 #if NGIF > 0
@@ -182,11 +183,35 @@ void
 stfattach(int count)
 {
 
+	/*
+	 * Nothing to do here, initialization is handled by the
+	 * module initialization code in stfinit() below).
+	 */
+}
+
+static void
+stfinit(void)
+{
+
 	LIST_INIT(&stf_softc_list);
 	if_clone_attach(&stf_cloner);
 }
 
 static int
+stfdetach(void)
+{
+	int error = 0;
+
+	if (!LIST_EMPTY(&stf_softc_list))
+		error = EBUSY;
+
+	if (error == 0)
+		if_clone_detach(&stf_cloner);
+
+	return error;
+}
+
+static int
 stf_clone_create(struct if_clone *ifc, int unit)
 {
 	struct stf_softc *sc;
@@ -714,3 +739,10 @@ stf_ioctl(struct ifnet *ifp, u_long cmd,
 
 	return error;
 }
+
+/*
+ * Module infrastructure
+ */
+#include "if_module.h"
+
+IF_MODULE(MODULE_CLASS_DRIVER, stf, "")

Index: src/sys/net/if_strip.c
diff -u src/sys/net/if_strip.c:1.105 src/sys/net/if_strip.c:1.106
--- src/sys/net/if_strip.c:1.105	Sat Aug  6 08:48:23 2016
+++ src/sys/net/if_strip.c	Sun Aug  7 13:38:34 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_strip.c,v 1.105 2016/08/06 12:48:23 christos Exp $	*/
+/*	$NetBSD: if_strip.c,v 1.106 2016/08/07 17:38:34 christos Exp $	*/
 /*	from: NetBSD: if_sl.c,v 1.38 1996/02/13 22:00:23 christos Exp $	*/
 
 /*
@@ -87,7 +87,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_strip.c,v 1.105 2016/08/06 12:48:23 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_strip.c,v 1.106 2016/08/07 17:38:34 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -382,6 +382,9 @@ stripdetach(void)
 	if (error == 0)
 		error = ttyldisc_detach(&strip_disc);
 
+	if (error == 0)
+		if_clone_detach(&strip_cloner);
+
 	return error;
 }
 
@@ -2003,60 +2006,6 @@ RecvErr_Message(struct strip_softc *stri
 /*
  * Module infrastructure
  */
+#include "if_module.h"
 
-MODULE(MODULE_CLASS_DRIVER, if_strip, "slcompress");
-
-#ifdef _MODULE
-CFDRIVER_DECL(strip, DV_IFNET, NULL);
-#endif
-
-static int
-if_strip_modcmd(modcmd_t cmd, void *arg)
-{
-	int error = 0;
-
-	switch (cmd) {
-	case MODULE_CMD_INIT:
-#ifdef _MODULE
-		error = config_cfdriver_attach(&strip_cd);
-		if (error) {
-			aprint_error("%s: unable to register cfdriver for"
-			    "%s, error %d\n", __func__, strip_cd.cd_name,
-			    error);
-			break;
-		}
-
-#endif
-		/* Init the unit list and line discipline stuff */
-		stripinit();
-		break;
-
-	case MODULE_CMD_FINI:
-		/*
-		 * Make sure it's ok to detach - no units left, and
-		 * line discipline is removed
-		 */
-		error = stripdetach();
-		if (error != 0)
-			break;
-#ifdef _MODULE
-		/* Remove device from autoconf database */
-		error = config_cfdriver_detach(&strip_cd);
-		if (error) {
-			aprint_error("%s: failed to detach %s cfdriver, "
-			    "error %d\n", __func__, strip_cd.cd_name, error);
-			break;
-		}
-#endif
-		break;
-
-	case MODULE_CMD_STAT:
-		error = ENOTTY;
-		break;
-	default:
-		error = ENOTTY;
-		break;
-	}
-
-	return error;
-}
+IF_MODULE(MODULE_CLASS_DRIVER, strip, "slcompress");

Index: src/sys/net/if_tap.c
diff -u src/sys/net/if_tap.c:1.84 src/sys/net/if_tap.c:1.85
--- src/sys/net/if_tap.c:1.84	Fri Jun 10 09:27:16 2016
+++ src/sys/net/if_tap.c	Sun Aug  7 13:38:34 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_tap.c,v 1.84 2016/06/10 13:27:16 ozaki-r Exp $	*/
+/*	$NetBSD: if_tap.c,v 1.85 2016/08/07 17:38:34 christos Exp $	*/
 
 /*
  *  Copyright (c) 2003, 2004, 2008, 2009 The NetBSD Foundation.
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_tap.c,v 1.84 2016/06/10 13:27:16 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_tap.c,v 1.85 2016/08/07 17:38:34 christos Exp $");
 
 #if defined(_KERNEL_OPT)
 
@@ -61,6 +61,9 @@ __KERNEL_RCSID(0, "$NetBSD: if_tap.c,v 1
 #include <sys/mutex.h>
 #include <sys/intr.h>
 #include <sys/stat.h>
+#include <sys/device.h>
+#include <sys/module.h>
+#include <sys/atomic.h>
 
 #include <net/if.h>
 #include <net/if_dl.h>
@@ -234,22 +237,38 @@ struct if_clone tap_cloners = IF_CLONE_I
 static struct tap_softc *	tap_clone_creator(int);
 int	tap_clone_destroyer(device_t);
 
+static u_int tap_count;
+
 void
 tapattach(int n)
 {
-	int error;
 
-	error = config_cfattach_attach(tap_cd.cd_name, &tap_ca);
-	if (error) {
-		aprint_error("%s: unable to register cfattach\n",
-		    tap_cd.cd_name);
-		(void)config_cfdriver_detach(&tap_cd);
-		return;
-	}
+	/*
+	 * Nothing to do here, initialization is handled by the
+	 * module initialization code in tapinit() below).
+	 */
+}
 
+static void
+tapinit(void)
+{
 	if_clone_attach(&tap_cloners);
 }
 
+static int
+tapdetach(void)
+{
+	int error = 0;
+
+	if (tap_count != 0)
+		error = EBUSY;
+
+	if (error == 0)
+		if_clone_detach(&tap_cloners);
+
+	return error;
+}
+
 /* Pretty much useless for a pseudo-device */
 static int
 tap_match(device_t parent, cfdata_t cfdata, void *arg)
@@ -629,7 +648,7 @@ tap_clone_create(struct if_clone *ifc, i
                     tap_cd.cd_name, unit);
 		return (ENXIO);
 	}
-
+	atomic_inc_uint(&tap_count);
 	return (0);
 }
 
@@ -669,8 +688,11 @@ static int
 tap_clone_destroy(struct ifnet *ifp)
 {
 	struct tap_softc *sc = ifp->if_softc;
+	int error = tap_clone_destroyer(sc->sc_dev);
 
-	return tap_clone_destroyer(sc->sc_dev);
+	if (error == 0)
+		atomic_inc_uint(&tap_count);
+	return error;
 }
 
 int
@@ -1400,3 +1422,10 @@ tap_sysctl_handler(SYSCTLFN_ARGS)
 	return (error);
 }
 #endif
+
+/*
+ * Module infrastructure
+ */
+#include "if_module.h"
+
+IF_MODULE(MODULE_CLASS_DRIVER, tap, "")

Index: src/sys/net/if_tun.c
diff -u src/sys/net/if_tun.c:1.127 src/sys/net/if_tun.c:1.128
--- src/sys/net/if_tun.c:1.127	Thu Jul  7 05:32:02 2016
+++ src/sys/net/if_tun.c	Sun Aug  7 13:38:34 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_tun.c,v 1.127 2016/07/07 09:32:02 ozaki-r Exp $	*/
+/*	$NetBSD: if_tun.c,v 1.128 2016/08/07 17:38:34 christos Exp $	*/
 
 /*
  * Copyright (c) 1988, Julian Onions <j...@cs.nott.ac.uk>
@@ -15,7 +15,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.127 2016/07/07 09:32:02 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.128 2016/08/07 17:38:34 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -39,6 +39,8 @@ __KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1
 #include <sys/kauth.h>
 #include <sys/mutex.h>
 #include <sys/cpu.h>
+#include <sys/device.h>
+#include <sys/module.h>
 
 #include <net/if.h>
 #include <net/if_types.h>
@@ -81,7 +83,7 @@ static struct if_clone tun_cloner =
     IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy);
 
 static void tunattach0(struct tun_softc *);
-static void tuninit(struct tun_softc *);
+static void tuncreate(struct tun_softc *);
 static void tun_i_softintr(void *);
 static void tun_o_softintr(void *);
 #ifdef ALTQ
@@ -117,12 +119,38 @@ void
 tunattach(int unused)
 {
 
+	/*
+	 * Nothing to do here, initialization is handled by the
+	 * module initialization code in pppinit() below).
+	 */
+}
+
+static void
+tuninit(void)
+{
+
 	mutex_init(&tun_softc_lock, MUTEX_DEFAULT, IPL_NET);
 	LIST_INIT(&tun_softc_list);
 	LIST_INIT(&tunz_softc_list);
 	if_clone_attach(&tun_cloner);
 }
 
+static int
+tundetach(void)
+{
+	int error = 0;
+
+	if (!LIST_EMPTY(&tun_softc_list) || !LIST_EMPTY(&tunz_softc_list))
+		error = EBUSY;
+
+	if (error == 0) {
+		if_clone_detach(&tun_cloner);
+		mutex_destroy(&tun_softc_lock);
+	}
+
+	return error;
+}
+
 /*
  * Find driver instance from dev_t.
  * Returns with tp locked (if found).
@@ -382,12 +410,12 @@ out_nolock:
  * Call at splnet().
  */
 static void
-tuninit(struct tun_softc *tp)
+tuncreate(struct tun_softc *tp)
 {
 	struct ifnet	*ifp = &tp->tun_if;
 	struct ifaddr	*ifa;
 
-	TUNDEBUG("%s: tuninit\n", ifp->if_xname);
+	TUNDEBUG("%s: %s\n", __func__, ifp->if_xname);
 
 	mutex_enter(&tp->tun_lock);
 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
@@ -445,7 +473,7 @@ tun_ioctl(struct ifnet *ifp, u_long cmd,
 
 	switch (cmd) {
 	case SIOCINITIFADDR:
-		tuninit(tp);
+		tuncreate(tp);
 		ifa->ifa_rtrequest = p2p_rtrequest;
 		TUNDEBUG("%s: address set\n", ifp->if_xname);
 		break;
@@ -1113,3 +1141,10 @@ out_nolock:
 	splx(s);
 	return (rv);
 }
+
+/*
+ * Module infrastructure
+ */
+#include "if_module.h"
+
+IF_MODULE(MODULE_CLASS_DRIVER, tun, "")

Index: src/sys/net/if_vlan.c
diff -u src/sys/net/if_vlan.c:1.90 src/sys/net/if_vlan.c:1.91
--- src/sys/net/if_vlan.c:1.90	Wed Jun 22 06:44:32 2016
+++ src/sys/net/if_vlan.c	Sun Aug  7 13:38:34 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_vlan.c,v 1.90 2016/06/22 10:44:32 knakahara Exp $	*/
+/*	$NetBSD: if_vlan.c,v 1.91 2016/08/07 17:38:34 christos Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc.
@@ -78,7 +78,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_vlan.c,v 1.90 2016/06/22 10:44:32 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_vlan.c,v 1.91 2016/08/07 17:38:34 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -95,6 +95,8 @@ __KERNEL_RCSID(0, "$NetBSD: if_vlan.c,v 
 #include <sys/proc.h>
 #include <sys/kauth.h>
 #include <sys/mutex.h>
+#include <sys/device.h>
+#include <sys/module.h>
 
 #include <net/bpf.h>
 #include <net/if.h>
@@ -196,11 +198,37 @@ void
 vlanattach(int n)
 {
 
+	/*
+	 * Nothing to do here, initialization is handled by the
+	 * module initialization code in vlaninit() below).
+	 */
+}
+
+static void
+vlaninit(void)
+{
+
 	LIST_INIT(&ifv_list);
 	mutex_init(&ifv_mtx, MUTEX_DEFAULT, IPL_NONE);
 	if_clone_attach(&vlan_cloner);
 }
 
+static int
+vlandetach(void)
+{
+	int error = 0;
+
+	if (!LIST_EMPTY(&ifv_list))
+		error = EBUSY;
+
+	if (error == 0) {
+		if_clone_detach(&vlan_cloner);
+		mutex_destroy(&ifv_mtx);
+	}
+
+	return error;
+}
+
 static void
 vlan_reset_linkname(struct ifnet *ifp)
 {
@@ -910,3 +938,10 @@ vlan_input(struct ifnet *ifp, struct mbu
 	m->m_flags &= ~M_PROMISC;
 	if_input(&ifv->ifv_if, m);
 }
+
+/*
+ * Module infrastructure
+ */
+#include "if_module.h"
+
+IF_MODULE(MODULE_CLASS_DRIVER, vlan, "")

Index: src/sys/net/agr/if_agr.c
diff -u src/sys/net/agr/if_agr.c:1.38 src/sys/net/agr/if_agr.c:1.39
--- src/sys/net/agr/if_agr.c:1.38	Wed Jul 20 03:37:51 2016
+++ src/sys/net/agr/if_agr.c	Sun Aug  7 13:38:34 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_agr.c,v 1.38 2016/07/20 07:37:51 ozaki-r Exp $	*/
+/*	$NetBSD: if_agr.c,v 1.39 2016/08/07 17:38:34 christos Exp $	*/
 
 /*-
  * Copyright (c)2005 YAMAMOTO Takashi,
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_agr.c,v 1.38 2016/07/20 07:37:51 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_agr.c,v 1.39 2016/08/07 17:38:34 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -44,6 +44,9 @@ __KERNEL_RCSID(0, "$NetBSD: if_agr.c,v 1
 #include <sys/proc.h>	/* XXX for curproc */
 #include <sys/kauth.h>
 #include <sys/xcall.h>
+#include <sys/device.h>
+#include <sys/module.h>
+#include <sys/atomic.h>
 
 #include <net/bpf.h>
 #include <net/if.h>
@@ -96,6 +99,8 @@ static void agr_ports_exit(struct agr_so
 static struct if_clone agr_cloner =
     IF_CLONE_INITIALIZER("agr", agr_clone_create, agr_clone_destroy);
 
+static u_int agr_count;
+
 /*
  * EXPORTED FUNCTIONS
  */
@@ -108,9 +113,32 @@ void
 agrattach(int count)
 {
 
+	/*
+	 * Nothing to do here, initialization is handled by the
+	 * module initialization code in agrinit() below).
+	 */
+}
+
+static void
+agrinit(void)
+{
 	if_clone_attach(&agr_cloner);
 }
 
+static int
+agrdetach(void)
+{
+	int error = 0;
+
+	if (agr_count != 0)
+		error = EBUSY;
+
+	if (error == 0)
+		if_clone_detach(&agr_cloner);
+
+	return error;
+}
+
 /*
  * agr_input: frame collector.
  */
@@ -338,7 +366,7 @@ agr_clone_create(struct if_clone *ifc, i
 	if_attach(ifp);
 
 	agr_reset_iftype(ifp);
-
+	atomic_inc_uint(&agr_count);
 	return 0;
 }
 
@@ -375,6 +403,7 @@ agr_clone_destroy(struct ifnet *ifp)
 	cv_destroy(&sc->sc_ports_cv);
 	agr_free_softc(sc);
 
+	atomic_dec_uint(&agr_count);
 	return 0;
 }
 
@@ -1191,3 +1220,10 @@ agrport_config_promisc(struct agr_port *
 
 	return error;
 }
+
+/*
+ * Module infrastructure
+ */
+#include <net/if_module.h>
+
+IF_MODULE(MODULE_CLASS_DRIVER, agr, "")

Added files:

Index: src/sys/net/if_module.h
diff -u /dev/null src/sys/net/if_module.h:1.1
--- /dev/null	Sun Aug  7 13:38:34 2016
+++ src/sys/net/if_module.h	Sun Aug  7 13:38:34 2016
@@ -0,0 +1,89 @@
+/*	$NetBSD: if_module.h,v 1.1 2016/08/07 17:38:34 christos Exp $	*/
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * 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.
+ */
+
+#ifdef _MODULE
+# define IF_MODULE_CFDRIVER_DECL(name) CFDRIVER_DECL(name, DV_IFNET, NULL)
+# define IF_MODULE_CFDRIVER_ATTACH(name)			\
+    error = config_cfdriver_attach(& name ## _cd);		\
+    if (error) {						\
+	    aprint_error("%s: unable to register cfdriver for"	\
+		"%s, error %d\n", __func__, name ## _cd.cd_name,\
+		error);						\
+	    break;						\
+    }
+# define IF_MODULE_CFDRIVER_DETACH(name)			\
+    /* Remove device from autoconf database */			\
+    error = config_cfdriver_detach(&name ## _cd);		\
+    if (error) {						\
+	    aprint_error("%s: failed to detach %s cfdriver, "	\
+		"error %d\n", __func__, name ## _cd.cd_name,	\
+		error);						\
+	    break;						\
+    }
+#else
+# define IF_MODULE_CFDRIVER_DECL(name)
+# define IF_MODULE_CFDRIVER_ATTACH(name)
+# define IF_MODULE_CFDRIVER_DETACH(name)
+#endif
+
+#define IF_MODULE(class, name, dep)				\
+MODULE(class, if_ ## name, dep);				\
+IF_MODULE_CFDRIVER_DECL(name);					\
+static int							\
+if_ ## name ## _modcmd(modcmd_t cmd, void *arg)			\
+{								\
+	int error = 0;						\
+								\
+	switch (cmd) {						\
+	case MODULE_CMD_INIT:					\
+		IF_MODULE_CFDRIVER_ATTACH(name)			\
+		/* Init the unit list/line discipline stuff */	\
+		name ## init();					\
+		break;						\
+								\
+	case MODULE_CMD_FINI:					\
+		/*						\
+		 * Make sure it's ok to detach - no units left,	\
+		 * and line discipline is removed		\
+		 */						\
+		error = name ## detach();			\
+		if (error != 0)					\
+			break;					\
+		IF_MODULE_CFDRIVER_DETACH(name)			\
+		break;						\
+								\
+	case MODULE_CMD_STAT:					\
+		error = ENOTTY;					\
+		break;						\
+	default:						\
+		error = ENOTTY;					\
+		break;						\
+	}							\
+								\
+	return error;						\
+}

Reply via email to