git: d8b42ddcac5c - main - rtsock: subscribe to ifnet eventhandlers instead of direct calls.

From: Alexander V. Chernikov <melifaro_at_FreeBSD.org>
Date: Thu, 11 Aug 2022 20:37:50 UTC
The branch main has been updated by melifaro:

URL: https://cgit.FreeBSD.org/src/commit/?id=d8b42ddcac5cb86af679968d09c45c9a7cc3e4fb

commit d8b42ddcac5cb86af679968d09c45c9a7cc3e4fb
Author:     Alexander V. Chernikov <melifaro@FreeBSD.org>
AuthorDate: 2022-08-11 20:09:45 +0000
Commit:     Alexander V. Chernikov <melifaro@FreeBSD.org>
CommitDate: 2022-08-11 20:36:59 +0000

    rtsock: subscribe to ifnet eventhandlers instead of direct calls.
    
    Stop treating rtsock as a "special" consumer and use already-provided
     ifaddr arrival/departure notifications.
    
    MFC after:      2 weeks
    
    Test Plan:
    ```
    21:05 [0] m@devel0 route -n monitor
    
    -> ifconfig vtnet0.2 create
    
    got message of size 24 on Tue Aug  9 21:05:44 2022
    RTM_IFANNOUNCE: interface arrival/departure: len 24, if# 3, what: arrival
    
    got message of size 168 on Tue Aug  9 21:05:54 2022
    RTM_IFINFO: iface status change: len 168, if# 3, link: up, flags:<BROADCAST,RUNNING,SIMPLEX,MULTICAST>
    
    -> ifconfig vtnet0.2 destroy
    
    got message of size 24 on Tue Aug  9 21:05:54 2022
    RTM_IFANNOUNCE: interface arrival/departure: len 24, if# 3, what: departure
    
    ```
    
    Reviewed By: glebius
    Differential Revision: https://reviews.freebsd.org/D36095
    MFC after:      2 weeks
---
 sys/net/if.c     |  9 ---------
 sys/net/route.h  |  1 -
 sys/net/rtsock.c | 17 ++++++++++++++++-
 3 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/sys/net/if.c b/sys/net/if.c
index 906f2256dd54..79995e3b9ea4 100644
--- a/sys/net/if.c
+++ b/sys/net/if.c
@@ -957,9 +957,6 @@ if_attach_internal(struct ifnet *ifp, bool vmove)
 	EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
 	if (IS_DEFAULT_VNET(curvnet))
 		devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
-
-	/* Announce the interface. */
-	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
 }
 
 static void
@@ -1210,8 +1207,6 @@ if_detach_internal(struct ifnet *ifp, bool vmove)
 #endif
 	if_purgemaddrs(ifp);
 
-	/* Announce that the interface is gone. */
-	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
 	EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
 	if (IS_DEFAULT_VNET(curvnet))
 		devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
@@ -2773,8 +2768,6 @@ ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
 		 */
 		ifp->if_flags |= IFF_RENAMING;
 		
-		/* Announce the departure of the interface. */
-		rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
 		EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
 
 		if_printf(ifp, "changing name to '%s'\n", new_name);
@@ -2804,8 +2797,6 @@ ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
 		IF_ADDR_WUNLOCK(ifp);
 
 		EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
-		/* Announce the return of the interface. */
-		rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
 
 		ifp->if_flags &= ~IFF_RENAMING;
 
diff --git a/sys/net/route.h b/sys/net/route.h
index 931b284b664d..47e4773b4700 100644
--- a/sys/net/route.h
+++ b/sys/net/route.h
@@ -415,7 +415,6 @@ struct ifmultiaddr;
 struct rib_head;
 
 void	 rt_ieee80211msg(struct ifnet *, int, void *, size_t);
-void	 rt_ifannouncemsg(struct ifnet *, int);
 void	 rt_ifmsg(struct ifnet *);
 void	 rt_missmsg(int, struct rt_addrinfo *, int, int);
 void	 rt_missmsg_fib(int, struct rt_addrinfo *, int, int, int);
diff --git a/sys/net/rtsock.c b/sys/net/rtsock.c
index c9b521eed9ca..0c6c5f067dd3 100644
--- a/sys/net/rtsock.c
+++ b/sys/net/rtsock.c
@@ -207,6 +207,7 @@ static int	sysctl_ifmalist(int af, struct walkarg *w);
 static void	rt_getmetrics(const struct rtentry *rt,
 			const struct nhop_object *nh, struct rt_metrics *out);
 static void	rt_dispatch(struct mbuf *, sa_family_t);
+static void	rt_ifannouncemsg(struct ifnet *ifp, int what);
 static int	handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
 			struct rt_msghdr *rtm, struct rib_cmd_info *rc);
 static int	update_rtm_from_rc(struct rt_addrinfo *info,
@@ -272,6 +273,20 @@ VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
     vnet_rts_uninit, 0);
 #endif
 
+static void
+rts_handle_ifnet_arrival(void *arg __unused, struct ifnet *ifp)
+{
+	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
+}
+EVENTHANDLER_DEFINE(ifnet_arrival_event, rts_handle_ifnet_arrival, NULL, 0);
+
+static void
+rts_handle_ifnet_departure(void *arg __unused, struct ifnet *ifp)
+{
+	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
+}
+EVENTHANDLER_DEFINE(ifnet_departure_event, rts_handle_ifnet_departure, NULL, 0);
+
 static void
 rts_append_data(struct socket *so, struct mbuf *m)
 {
@@ -2112,7 +2127,7 @@ rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
  * This is called to generate routing socket messages indicating
  * network interface arrival and departure.
  */
-void
+static void
 rt_ifannouncemsg(struct ifnet *ifp, int what)
 {
 	struct mbuf *m;