Next stage of rttimer cleanup. Move the callback from the rttimer to the
rttimer_queue struct. The callback is always the same for a queue so there
is no need to define it on every call.

On top of that replace rt_timer_queue_destroy() with
rt_timer_queue_flush(). With this queues can no longer be removed but that
is not a problem. My next step is to actually replace
rt_timer_queue_create() with an initalizer.
-- 
:wq Claudio

Index: net/route.c
===================================================================
RCS file: /cvs/src/sys/net/route.c,v
retrieving revision 1.408
diff -u -p -r1.408 route.c
--- net/route.c 30 Apr 2022 07:20:35 -0000      1.408
+++ net/route.c 2 May 2022 10:16:58 -0000
@@ -1365,8 +1365,8 @@ struct mutex                      rttimer_mtx;
 LIST_HEAD(, rttimer_queue)     rttimer_queue_head;     /* [T] */
 
 #define RTTIMER_CALLOUT(r)     {                                       \
-       if (r->rtt_func != NULL) {                                      \
-               (*r->rtt_func)(r->rtt_rt, r->rtt_tableid);              \
+       if (r->rtt_queue->rtq_func != NULL) {                           \
+               (*r->rtt_queue->rtq_func)(r->rtt_rt, r->rtt_tableid);   \
        } else {                                                        \
                struct ifnet *ifp;                                      \
                                                                        \
@@ -1403,7 +1403,7 @@ rt_timer_init(void)
 }
 
 struct rttimer_queue *
-rt_timer_queue_create(int timeout)
+rt_timer_queue_create(int timeout, void (*func)(struct rtentry *, u_int))
 {
        struct rttimer_queue    *rtq;
 
@@ -1411,6 +1411,7 @@ rt_timer_queue_create(int timeout)
 
        rtq->rtq_timeout = timeout;
        rtq->rtq_count = 0;
+       rtq->rtq_func = func;
        TAILQ_INIT(&rtq->rtq_head);
 
        mtx_enter(&rttimer_mtx);
@@ -1429,7 +1430,7 @@ rt_timer_queue_change(struct rttimer_que
 }
 
 void
-rt_timer_queue_destroy(struct rttimer_queue *rtq)
+rt_timer_queue_flush(struct rttimer_queue *rtq)
 {
        struct rttimer          *r;
        TAILQ_HEAD(, rttimer)    rttlist;
@@ -1445,7 +1446,6 @@ rt_timer_queue_destroy(struct rttimer_qu
                KASSERT(rtq->rtq_count > 0);
                rtq->rtq_count--;
        }
-       LIST_REMOVE(rtq, rtq_link);
        mtx_leave(&rttimer_mtx);
 
        while ((r = TAILQ_FIRST(&rttlist)) != NULL) {
@@ -1453,7 +1453,6 @@ rt_timer_queue_destroy(struct rttimer_qu
                RTTIMER_CALLOUT(r);
                pool_put(&rttimer_pool, r);
        }
-       pool_put(&rttimer_queue_pool, rtq);
 }
 
 unsigned long
@@ -1486,8 +1485,7 @@ rt_timer_remove_all(struct rtentry *rt)
 }
 
 int
-rt_timer_add(struct rtentry *rt, void (*func)(struct rtentry *, u_int),
-     struct rttimer_queue *queue, u_int rtableid)
+rt_timer_add(struct rtentry *rt, struct rttimer_queue *queue, u_int rtableid)
 {
        struct rttimer  *r, *rnew;
        time_t           current_time;
@@ -1500,7 +1498,6 @@ rt_timer_add(struct rtentry *rt, void (*
 
        rnew->rtt_rt = rt;
        rnew->rtt_time = current_time;
-       rnew->rtt_func = func;
        rnew->rtt_queue = queue;
        rnew->rtt_tableid = rtableid;
 
@@ -1511,7 +1508,7 @@ rt_timer_add(struct rtentry *rt, void (*
         * we add a new one.
         */
        LIST_FOREACH(r, &rt->rt_timer, rtt_link) {
-               if (r->rtt_func == func) {
+               if (r->rtt_queue == queue) {
                        LIST_REMOVE(r, rtt_link);
                        TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
                        KASSERT(r->rtt_queue->rtq_count > 0);
Index: net/route.h
===================================================================
RCS file: /cvs/src/sys/net/route.h,v
retrieving revision 1.192
diff -u -p -r1.192 route.h
--- net/route.h 30 Apr 2022 07:20:35 -0000      1.192
+++ net/route.h 30 Apr 2022 08:09:54 -0000
@@ -410,8 +410,6 @@ struct rttimer {
        LIST_ENTRY(rttimer)     rtt_link;       /* [T] timers per rtentry */
        struct rttimer_queue    *rtt_queue;     /* [T] back pointer to queue */
        struct rtentry          *rtt_rt;        /* [I] back pointer to route */
-       void                    (*rtt_func)     /* [I] callback */
-                                   (struct rtentry *, u_int);
        time_t                  rtt_time;       /* [I] when timer registered */
        u_int                   rtt_tableid;    /* [I] rtable id of rtt_rt */
 };
@@ -419,6 +417,8 @@ struct rttimer {
 struct rttimer_queue {
        TAILQ_HEAD(, rttimer)           rtq_head;       /* [T] */
        LIST_ENTRY(rttimer_queue)       rtq_link;       /* [T] */
+       void                            (*rtq_func)     /* [I] callback */
+                                           (struct rtentry *, u_int);
        unsigned long                   rtq_count;      /* [T] */
        int                             rtq_timeout;    /* [T] */
 };
@@ -459,12 +459,12 @@ struct rtentry *rt_getll(struct rtentry 
 
 void                    rt_timer_init(void);
 int                     rt_timer_add(struct rtentry *,
-                           void(*)(struct rtentry *, u_int),
                            struct rttimer_queue *, u_int);
 void                    rt_timer_remove_all(struct rtentry *);
-struct rttimer_queue   *rt_timer_queue_create(int);
+struct rttimer_queue   *rt_timer_queue_create(int,
+                           void(*)(struct rtentry *, u_int));
 void                    rt_timer_queue_change(struct rttimer_queue *, int);
-void                    rt_timer_queue_destroy(struct rttimer_queue *);
+void                    rt_timer_queue_flush(struct rttimer_queue *);
 unsigned long           rt_timer_queue_count(struct rttimer_queue *);
 void                    rt_timer_timer(void *);
 
Index: netinet/ip_icmp.c
===================================================================
RCS file: /cvs/src/sys/netinet/ip_icmp.c,v
retrieving revision 1.189
diff -u -p -r1.189 ip_icmp.c
--- netinet/ip_icmp.c   30 Apr 2022 07:20:35 -0000      1.189
+++ netinet/ip_icmp.c   30 Apr 2022 07:57:32 -0000
@@ -120,6 +120,7 @@ int icmp_redirtimeout = 10 * 60;
 static int icmperrpps_count = 0;
 static struct timeval icmperrppslim_last;
 
+struct rttimer_queue *ip_mtudisc_timeout_q;
 struct rttimer_queue *icmp_redirect_timeout_q;
 struct cpumem *icmpcounters;
 
@@ -140,7 +141,10 @@ int icmp_sysctl_icmpstat(void *, size_t 
 void
 icmp_init(void)
 {
-       icmp_redirect_timeout_q = rt_timer_queue_create(icmp_redirtimeout);
+       ip_mtudisc_timeout_q = rt_timer_queue_create(ip_mtudisc_timeout,
+           &icmp_mtudisc_timeout);
+       icmp_redirect_timeout_q = rt_timer_queue_create(icmp_redirtimeout,
+           NULL);
        icmpcounters = counters_alloc(icps_ncounters);
 }
 
@@ -633,7 +637,7 @@ reflect:
                rtredirect(sintosa(&sdst), sintosa(&sgw),
                    sintosa(&ssrc), &newrt, m->m_pkthdr.ph_rtableid);
                if (newrt != NULL && icmp_redirtimeout > 0) {
-                       rt_timer_add(newrt, NULL, icmp_redirect_timeout_q,
+                       rt_timer_add(newrt, icmp_redirect_timeout_q,
                            m->m_pkthdr.ph_rtableid);
                }
                rtfree(newrt);
@@ -974,8 +978,7 @@ icmp_mtudisc_clone(struct in_addr dst, u
                rt = nrt;
                rtm_send(rt, RTM_ADD, 0, rtableid);
        }
-       error = rt_timer_add(rt, icmp_mtudisc_timeout, ip_mtudisc_timeout_q,
-           rtableid);
+       error = rt_timer_add(rt, ip_mtudisc_timeout_q, rtableid);
        if (error)
                goto bad;
 
Index: netinet/ip_input.c
===================================================================
RCS file: /cvs/src/sys/netinet/ip_input.c,v
retrieving revision 1.369
diff -u -p -r1.369 ip_input.c
--- netinet/ip_input.c  28 Apr 2022 17:27:14 -0000      1.369
+++ netinet/ip_input.c  30 Apr 2022 08:09:28 -0000
@@ -94,8 +94,6 @@ int   ip_mtudisc = 1;
 int    ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
 int    ip_directedbcast = 0;
 
-struct rttimer_queue *ip_mtudisc_timeout_q;
-
 /* Protects `ipq' and `ip_frags'. */
 struct mutex   ipq_mutex = MUTEX_INITIALIZER(IPL_SOFTNET);
 
@@ -200,7 +198,6 @@ ip_init(void)
                    pr->pr_protocol < IPPROTO_MAX)
                        ip_protox[pr->pr_protocol] = pr - inetsw;
        LIST_INIT(&ipq);
-       ip_mtudisc_timeout_q = rt_timer_queue_create(ip_mtudisc_timeout);
 
        /* Fill in list of ports not to allocate dynamically. */
        memset(&baddynamicports, 0, sizeof(baddynamicports));
@@ -224,7 +221,8 @@ ip_init(void)
        ipsec_init();
 #endif
 #ifdef MROUTING
-       ip_mrouterq = rt_timer_queue_create(MCAST_EXPIRE_FREQUENCY);
+       ip_mrouterq = rt_timer_queue_create(MCAST_EXPIRE_FREQUENCY,
+           &mfc_expire_route);
 #endif
 }
 
@@ -1656,11 +1654,8 @@ ip_sysctl(int *name, u_int namelen, void
        case IPCTL_MTUDISC:
                NET_LOCK();
                error = sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtudisc);
-               if (ip_mtudisc == 0) {
-                       rt_timer_queue_destroy(ip_mtudisc_timeout_q);
-                       ip_mtudisc_timeout_q =
-                           rt_timer_queue_create(ip_mtudisc_timeout);
-               }
+               if (ip_mtudisc == 0)
+                       rt_timer_queue_flush(ip_mtudisc_timeout_q);
                NET_UNLOCK();
                return error;
        case IPCTL_MTUDISCTIMEOUT:
Index: netinet/ip_mroute.c
===================================================================
RCS file: /cvs/src/sys/netinet/ip_mroute.c,v
retrieving revision 1.133
diff -u -p -r1.133 ip_mroute.c
--- netinet/ip_mroute.c 30 Apr 2022 07:20:35 -0000      1.133
+++ netinet/ip_mroute.c 30 Apr 2022 08:06:25 -0000
@@ -113,7 +113,6 @@ int get_version(struct mbuf *);
 int add_vif(struct socket *, struct mbuf *);
 int del_vif(struct socket *, struct mbuf *);
 void update_mfc_params(struct mfcctl2 *, int, unsigned int);
-void mfc_expire_route(struct rtentry *, u_int);
 int mfc_add(struct mfcctl2 *, struct in_addr *, struct in_addr *,
     int, unsigned int, int);
 int add_mfc(struct socket *, struct mbuf *);
@@ -793,7 +792,7 @@ mfc_expire_route(struct rtentry *rt, u_i
        /* Not expired, add it back to the queue. */
        if (mfc->mfc_expire == 0) {
                mfc->mfc_expire = 1;
-               rt_timer_add(rt, mfc_expire_route, ip_mrouterq, rtableid);
+               rt_timer_add(rt, ip_mrouterq, rtableid);
                return;
        }
 
@@ -827,7 +826,7 @@ mfc_add_route(struct ifnet *ifp, struct 
 
        rt->rt_llinfo = (caddr_t)mfc;
 
-       rt_timer_add(rt, mfc_expire_route, ip_mrouterq, rtableid);
+       rt_timer_add(rt, ip_mrouterq, rtableid);
 
        mfc->mfc_parent = mfccp->mfcc_parent;
        mfc->mfc_pkt_cnt = 0;
Index: netinet/ip_mroute.h
===================================================================
RCS file: /cvs/src/sys/netinet/ip_mroute.h,v
retrieving revision 1.29
diff -u -p -r1.29 ip_mroute.h
--- netinet/ip_mroute.h 28 Apr 2022 17:27:14 -0000      1.29
+++ netinet/ip_mroute.h 30 Apr 2022 08:07:03 -0000
@@ -176,6 +176,8 @@ struct mrtstat {
 #define MCAST_EXPIRE_FREQUENCY         30
 
 extern struct rttimer_queue *ip_mrouterq;
+void mfc_expire_route(struct rtentry *, u_int);
+
 extern int ip_mrtproto;
 
 /*
Index: netinet6/icmp6.c
===================================================================
RCS file: /cvs/src/sys/netinet6/icmp6.c,v
retrieving revision 1.240
diff -u -p -r1.240 icmp6.c
--- netinet6/icmp6.c    30 Apr 2022 07:20:35 -0000      1.240
+++ netinet6/icmp6.c    30 Apr 2022 08:04:20 -0000
@@ -143,8 +143,10 @@ void
 icmp6_init(void)
 {
        mld6_init();
-       icmp6_mtudisc_timeout_q = rt_timer_queue_create(ip6_mtudisc_timeout);
-       icmp6_redirect_timeout_q = rt_timer_queue_create(icmp6_redirtimeout);
+       icmp6_mtudisc_timeout_q = rt_timer_queue_create(ip6_mtudisc_timeout,
+           &icmp6_mtudisc_timeout);
+       icmp6_redirect_timeout_q = rt_timer_queue_create(icmp6_redirtimeout,
+           NULL);
        icmp6counters = counters_alloc(icp6s_ncounters);
 }
 
@@ -1404,7 +1406,7 @@ icmp6_redirect_input(struct mbuf *m, int
                rtredirect(sin6tosa(&sdst), sin6tosa(&sgw), sin6tosa(&ssrc),
                    &newrt, m->m_pkthdr.ph_rtableid);
                if (newrt != NULL && icmp6_redirtimeout > 0) {
-                       rt_timer_add(newrt, NULL, icmp6_redirect_timeout_q,
+                       rt_timer_add(newrt, icmp6_redirect_timeout_q,
                            m->m_pkthdr.ph_rtableid);
                }
                rtfree(newrt);
@@ -1828,8 +1830,7 @@ icmp6_mtudisc_clone(struct sockaddr_in6 
                rt = nrt;
                rtm_send(rt, RTM_ADD, 0, rtableid);
        }
-       error = rt_timer_add(rt, icmp6_mtudisc_timeout, icmp6_mtudisc_timeout_q,
-           rtableid);
+       error = rt_timer_add(rt, icmp6_mtudisc_timeout_q, rtableid);
        if (error)
                goto bad;
 
Index: netinet6/ip6_input.c
===================================================================
RCS file: /cvs/src/sys/netinet6/ip6_input.c,v
retrieving revision 1.243
diff -u -p -r1.243 ip6_input.c
--- netinet6/ip6_input.c        28 Apr 2022 17:27:14 -0000      1.243
+++ netinet6/ip6_input.c        30 Apr 2022 08:03:02 -0000
@@ -162,7 +162,8 @@ ip6_init(void)
 
        ip6counters = counters_alloc(ip6s_ncounters);
 #ifdef MROUTING
-       ip6_mrouterq = rt_timer_queue_create(MCAST_EXPIRE_TIMEOUT);
+       ip6_mrouterq = rt_timer_queue_create(MCAST_EXPIRE_TIMEOUT,
+           &mf6c_expire_route);
 #endif
 }
 
Index: netinet6/ip6_mroute.c
===================================================================
RCS file: /cvs/src/sys/netinet6/ip6_mroute.c,v
retrieving revision 1.129
diff -u -p -r1.129 ip6_mroute.c
--- netinet6/ip6_mroute.c       30 Apr 2022 07:20:35 -0000      1.129
+++ netinet6/ip6_mroute.c       30 Apr 2022 08:04:50 -0000
@@ -176,7 +176,6 @@ struct rtentry *mf6c_find(struct ifnet *
 struct rtentry *mrt6_mcast_add(struct ifnet *, struct sockaddr *,
     struct sockaddr *);
 void mrt6_mcast_del(struct rtentry *, unsigned int);
-void mf6c_expire_route(struct rtentry *, u_int);
 
 /*
  * Handle MRT setsockopt commands to modify the multicast routing tables.
@@ -677,7 +676,7 @@ mf6c_add_route(struct ifnet *ifp, struct
        }
 
        rt->rt_llinfo = (caddr_t)mf6c;
-       rt_timer_add(rt, mf6c_expire_route, ip6_mrouterq, rtableid);
+       rt_timer_add(rt, ip6_mrouterq, rtableid);
        mf6c->mf6c_parent = mf6cc->mf6cc_parent;
        rtfree(rt);
 
@@ -1004,7 +1003,7 @@ mf6c_expire_route(struct rtentry *rt, u_
 
        if (mf6c->mf6c_expire == 0) {
                mf6c->mf6c_expire = 1;
-               rt_timer_add(rt, mf6c_expire_route, ip6_mrouterq, rtableid);
+               rt_timer_add(rt, ip6_mrouterq, rtableid);
                return;
        }
 
Index: netinet6/ip6_mroute.h
===================================================================
RCS file: /cvs/src/sys/netinet6/ip6_mroute.h,v
retrieving revision 1.21
diff -u -p -r1.21 ip6_mroute.h
--- netinet6/ip6_mroute.h       28 Apr 2022 17:27:14 -0000      1.21
+++ netinet6/ip6_mroute.h       30 Apr 2022 08:05:29 -0000
@@ -195,6 +195,7 @@ struct sioc_mif_req6 {
 #define        MCAST_EXPIRE_TIMEOUT    30
 
 extern struct rttimer_queue *ip6_mrouterq;
+void   mf6c_expire_route(struct rtentry *, u_int);
 
 /*
  * The kernel's multicast-interface structure.

Reply via email to