the pf_state_tree type represents the rb tree that pf_state_key structs
go into. currently pf_state_key is declared in pfvar_priv.h (because
it's a kernel private data structure) but pf_state_tree was left in
pfvar.h. this moves it to pfvar_priv.h, because it is also kernel
private.

while here, this moves it from the RB tree macros to RBT which shrinks
pf.o by about 13k on amd64.

ok?

Index: pf.c
===================================================================
RCS file: /cvs/src/sys/net/pf.c,v
retrieving revision 1.1165
diff -u -p -r1.1165 pf.c
--- pf.c        2 Jan 2023 05:32:40 -0000       1.1165
+++ pf.c        3 Jan 2023 04:16:25 -0000
@@ -303,8 +303,8 @@ struct pf_pool_limit pf_pool_limits[PF_L
        } while (0)
 
 static __inline int pf_src_compare(struct pf_src_node *, struct pf_src_node *);
-static __inline int pf_state_compare_key(struct pf_state_key *,
-       struct pf_state_key *);
+static inline int pf_state_compare_key(const struct pf_state_key *,
+       const struct pf_state_key *);
 static __inline int pf_state_compare_id(struct pf_state *,
        struct pf_state *);
 #ifdef INET6
@@ -319,12 +319,13 @@ struct pf_state_tree_id tree_id;
 struct pf_state_list pf_state_list = PF_STATE_LIST_INITIALIZER(pf_state_list);
 
 RB_GENERATE(pf_src_tree, pf_src_node, entry, pf_src_compare);
-RB_GENERATE(pf_state_tree, pf_state_key, sk_entry, pf_state_compare_key);
+RBT_GENERATE(pf_state_tree, pf_state_key, sk_entry, pf_state_compare_key);
 RB_GENERATE(pf_state_tree_id, pf_state,
     entry_id, pf_state_compare_id);
 
-__inline int
-pf_addr_compare(struct pf_addr *a, struct pf_addr *b, sa_family_t af)
+int
+pf_addr_compare(const struct pf_addr *a, const struct pf_addr *b,
+    sa_family_t af)
 {
        switch (af) {
        case AF_INET:
@@ -689,8 +690,9 @@ pf_state_rm_src_node(struct pf_state *s,
 
 /* state table stuff */
 
-static __inline int
-pf_state_compare_key(struct pf_state_key *a, struct pf_state_key *b)
+static inline int
+pf_state_compare_key(const struct pf_state_key *a,
+    const struct pf_state_key *b)
 {
        int     diff;
 
@@ -743,7 +745,7 @@ pf_state_key_attach(struct pf_state_key 
 
        KASSERT(s->key[idx] == NULL);
        sk->sk_removed = 0;
-       cur = RB_INSERT(pf_state_tree, &pf_statetbl, sk);
+       cur = RBT_INSERT(pf_state_tree, &pf_statetbl, sk);
        if (cur != NULL) {
                sk->sk_removed = 1;
                /* key exists. check for same kif, if none, add to key */
@@ -798,7 +800,7 @@ pf_state_key_attach(struct pf_state_key 
        if ((si = pool_get(&pf_state_item_pl, PR_NOWAIT)) == NULL) {
                if (TAILQ_EMPTY(&sk->sk_states)) {
                        KASSERT(cur == NULL);
-                       RB_REMOVE(pf_state_tree, &pf_statetbl, sk);
+                       RBT_REMOVE(pf_state_tree, &pf_statetbl, sk);
                        sk->sk_removed = 1;
                        pf_state_key_unref(sk);
                }
@@ -856,7 +858,7 @@ pf_state_key_detach(struct pf_state *s, 
        pool_put(&pf_state_item_pl, si);
 
        if (TAILQ_EMPTY(&sk->sk_states)) {
-               RB_REMOVE(pf_state_tree, &pf_statetbl, sk);
+               RBT_REMOVE(pf_state_tree, &pf_statetbl, sk);
                sk->sk_removed = 1;
                pf_state_key_unlink_reverse(sk);
                pf_state_key_unlink_inpcb(sk);
@@ -1165,7 +1167,7 @@ pf_find_state(struct pf_pdesc *pd, struc
        }
 
        if (sk == NULL) {
-               if ((sk = RB_FIND(pf_state_tree, &pf_statetbl,
+               if ((sk = RBT_FIND(pf_state_tree, &pf_statetbl,
                    (struct pf_state_key *)key)) == NULL)
                        return (PF_DROP);
                if (pd->dir == PF_OUT && pkt_sk &&
@@ -1220,7 +1222,7 @@ pf_find_state_all(struct pf_state_key_cm
 
        pf_status.fcounters[FCNT_STATE_SEARCH]++;
 
-       sk = RB_FIND(pf_state_tree, &pf_statetbl, (struct pf_state_key *)key);
+       sk = RBT_FIND(pf_state_tree, &pf_statetbl, (struct pf_state_key *)key);
 
        if (sk != NULL) {
                TAILQ_FOREACH(si, &sk->sk_states, si_entry) {
Index: pf_ioctl.c
===================================================================
RCS file: /cvs/src/sys/net/pf_ioctl.c,v
retrieving revision 1.393
diff -u -p -r1.393 pf_ioctl.c
--- pf_ioctl.c  21 Dec 2022 02:23:10 -0000      1.393
+++ pf_ioctl.c  3 Jan 2023 04:16:26 -0000
@@ -1758,7 +1758,8 @@ pfioctl(dev_t dev, u_long cmd, caddr_t a
                                key.port[sidx] = psk->psk_src.port[0];
                                key.port[didx] = psk->psk_dst.port[0];
 
-                               sk = RB_FIND(pf_state_tree, &pf_statetbl, &key);
+                               sk = RBT_FIND(pf_state_tree, &pf_statetbl,
+                                   &key);
                                if (sk == NULL)
                                        continue;
 
Index: pfvar.h
===================================================================
RCS file: /cvs/src/sys/net/pfvar.h,v
retrieving revision 1.525
diff -u -p -r1.525 pfvar.h
--- pfvar.h     22 Dec 2022 05:59:27 -0000      1.525
+++ pfvar.h     3 Jan 2023 04:16:26 -0000
@@ -1033,9 +1033,6 @@ struct pfr_ktable {
 #define pfrkt_nomatch  pfrkt_ts.pfrts_nomatch
 #define pfrkt_tzero    pfrkt_ts.pfrts_tzero
 
-RB_HEAD(pf_state_tree, pf_state_key);
-RB_PROTOTYPE(pf_state_tree, pf_state_key, sk_entry, pf_state_compare_key)
-
 RB_HEAD(pf_state_tree_ext_gwy, pf_state_key);
 RB_PROTOTYPE(pf_state_tree_ext_gwy, pf_state_key,
     entry_ext_gwy, pf_state_compare_ext_gwy)
@@ -1792,8 +1789,8 @@ void               pf_tag2tagname(u_int16_t, char *)
 void            pf_tag_ref(u_int16_t);
 void            pf_tag_unref(u_int16_t);
 void            pf_tag_packet(struct mbuf *, int, int);
-int             pf_addr_compare(struct pf_addr *, struct pf_addr *,
-                   sa_family_t);
+int             pf_addr_compare(const struct pf_addr *,
+                    const struct pf_addr *, sa_family_t);
 
 const struct pfq_ops
                *pf_queue_manager(struct pf_queuespec *);
Index: pfvar_priv.h
===================================================================
RCS file: /cvs/src/sys/net/pfvar_priv.h,v
retrieving revision 1.27
diff -u -p -r1.27 pfvar_priv.h
--- pfvar_priv.h        22 Dec 2022 05:59:27 -0000      1.27
+++ pfvar_priv.h        3 Jan 2023 04:16:26 -0000
@@ -63,6 +63,10 @@ struct pf_state_key {
        pf_refcnt_t              sk_refcnt;
        u_int8_t                 sk_removed;
 };
+
+RBT_HEAD(pf_state_tree, pf_state_key);
+RBT_PROTOTYPE(pf_state_tree, pf_state_key, sk_entry, pf_state_compare_key);
+
 #define PF_REVERSED_KEY(key, family)                           \
        ((key[PF_SK_WIRE]->af != key[PF_SK_STACK]->af) &&       \
         (key[PF_SK_WIRE]->af != (family)))

Reply via email to