Module Name:    src
Committed By:   yamaguchi
Date:           Thu Sep 30 03:54:04 UTC 2021

Modified Files:
        src/sys/net: if_ether.h if_ethersubr.c

Log Message:
Provide a hook point called when ether_ifdetach is called


To generate a diff of this commit:
cvs rdiff -u -r1.86 -r1.87 src/sys/net/if_ether.h
cvs rdiff -u -r1.296 -r1.297 src/sys/net/if_ethersubr.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_ether.h
diff -u src/sys/net/if_ether.h:1.86 src/sys/net/if_ether.h:1.87
--- src/sys/net/if_ether.h:1.86	Sun Feb 14 19:35:37 2021
+++ src/sys/net/if_ether.h	Thu Sep 30 03:54:04 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_ether.h,v 1.86 2021/02/14 19:35:37 roy Exp $	*/
+/*	$NetBSD: if_ether.h,v 1.87 2021/09/30 03:54:04 yamaguchi Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1993
@@ -198,6 +198,8 @@ struct ethercom {
 	 * being added or removed.
 	 */
 	ether_vlancb_t				ec_vlan_cb;
+	/* Hooks called at the beginning of detach of this interface */
+	khook_list_t				*ec_ifdetach_hooks;
 	kmutex_t				*ec_lock;
 	/* Flags used only by the kernel */
 	int					ec_flags;
@@ -385,6 +387,10 @@ void	ether_ifattach(struct ifnet *, cons
 void	ether_ifdetach(struct ifnet *);
 int	ether_mediachange(struct ifnet *);
 void	ether_mediastatus(struct ifnet *, struct ifmediareq *);
+void *	ether_ifdetachhook_establish(struct ifnet *,
+	    void (*)(void *), void *arg);
+void	ether_ifdetachhook_disestablish(struct ifnet *,
+	    void *, kmutex_t *);
 
 char	*ether_sprintf(const uint8_t *);
 char	*ether_snprintf(char *, size_t, const uint8_t *);

Index: src/sys/net/if_ethersubr.c
diff -u src/sys/net/if_ethersubr.c:1.296 src/sys/net/if_ethersubr.c:1.297
--- src/sys/net/if_ethersubr.c:1.296	Thu Sep 30 03:51:05 2021
+++ src/sys/net/if_ethersubr.c	Thu Sep 30 03:54:04 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_ethersubr.c,v 1.296 2021/09/30 03:51:05 yamaguchi Exp $	*/
+/*	$NetBSD: if_ethersubr.c,v 1.297 2021/09/30 03:54:04 yamaguchi Exp $	*/
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -61,7 +61,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.296 2021/09/30 03:51:05 yamaguchi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.297 2021/09/30 03:54:04 yamaguchi Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -90,6 +90,7 @@ __KERNEL_RCSID(0, "$NetBSD: if_ethersubr
 #include <sys/rndsource.h>
 #include <sys/cpu.h>
 #include <sys/kmem.h>
+#include <sys/hook.h>
 
 #include <net/if.h>
 #include <net/netisr.h>
@@ -1012,6 +1013,7 @@ void
 ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
 {
 	struct ethercom *ec = (struct ethercom *)ifp;
+	char xnamebuf[HOOKNAMSIZ];
 
 	ifp->if_type = IFT_ETHER;
 	ifp->if_hdrlen = ETHER_HDR_LEN;
@@ -1031,6 +1033,9 @@ ether_ifattach(struct ifnet *ifp, const 
 	ec->ec_flags = 0;
 	ifp->if_broadcastaddr = etherbroadcastaddr;
 	bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
+	snprintf(xnamebuf, sizeof(xnamebuf),
+	    "%s-ether_ifdetachhooks", ifp->if_xname);
+	ec->ec_ifdetach_hooks = simplehook_create(IPL_NET, xnamebuf);
 #ifdef MBUFTRACE
 	mowner_init_owner(&ec->ec_tx_mowner, ifp->if_xname, "tx");
 	mowner_init_owner(&ec->ec_rx_mowner, ifp->if_xname, "rx");
@@ -1057,6 +1062,10 @@ ether_ifdetach(struct ifnet *ifp)
 	ifp->if_ioctl = __FPTRCAST(int (*)(struct ifnet *, u_long, void *),
 	    enxio);
 
+	simplehook_dohooks(ec->ec_ifdetach_hooks);
+	KASSERT(!simplehook_has_hooks(ec->ec_ifdetach_hooks));
+	simplehook_destroy(ec->ec_ifdetach_hooks);
+
 #if NBRIDGE > 0
 	if (ifp->if_bridge)
 		bridge_ifdetach(ifp);
@@ -1089,6 +1098,36 @@ ether_ifdetach(struct ifnet *ifp)
 	MOWNER_DETACH(&ec->ec_tx_mowner);
 }
 
+void *
+ether_ifdetachhook_establish(struct ifnet *ifp,
+    void (*fn)(void *), void *arg)
+{
+	struct ethercom *ec;
+	khook_t *hk;
+
+	if (ifp->if_type != IFT_ETHER)
+		return NULL;
+
+	ec = (struct ethercom *)ifp;
+	hk = simplehook_establish(ec->ec_ifdetach_hooks,
+	    fn, arg);
+
+	return (void *)hk;
+}
+
+void
+ether_ifdetachhook_disestablish(struct ifnet *ifp,
+    void *vhook, kmutex_t *lock)
+{
+	struct ethercom *ec;
+
+	if (vhook == NULL)
+		return;
+
+	ec = (struct ethercom *)ifp;
+	simplehook_disestablish(ec->ec_ifdetach_hooks, vhook, lock);
+}
+
 #if 0
 /*
  * This is for reference.  We have a table-driven version

Reply via email to