Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 20 Jan 2021 14:45:01 GMT
From:      Kristof Provost <kp@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: ce26f0988baf - stable/12 - pf: Allocate and free pfi_kkif in separate functions
Message-ID:  <202101201445.10KEj1OL019475@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/12 has been updated by kp:

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

commit ce26f0988baff60505bfd4eba006abe25ee722bf
Author:     Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2020-12-13 10:36:54 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2021-01-20 14:16:05 +0000

    pf: Allocate and free pfi_kkif in separate functions
    
    Factor out allocating and freeing pfi_kkif structures. This will be
    useful when we change the counters to be counter_u64, so we don't have
    to deal with that complexity in the multiple locations where we allocate
    pfi_kkif structures.
    
    No functional change.
    
    MFC after:      2 weeks
    Sponsored by:   Orange Business Services
    Differential Revision:  https://reviews.freebsd.org/D27762
    
    (cherry picked from commit 26c841e2a402ed43896313d1b3f00facaa9d839f)
---
 sys/net/pfvar.h           |  3 +++
 sys/netpfil/pf/pf_if.c    | 37 ++++++++++++++++++++++++++++---------
 sys/netpfil/pf/pf_ioctl.c | 16 ++++++++--------
 3 files changed, 39 insertions(+), 17 deletions(-)

diff --git a/sys/net/pfvar.h b/sys/net/pfvar.h
index a58da4e4cc46..a28483fa9814 100644
--- a/sys/net/pfvar.h
+++ b/sys/net/pfvar.h
@@ -1663,6 +1663,9 @@ struct pf_krule		*pf_get_translation(struct pf_pdesc *, struct mbuf *,
 struct pf_state_key	*pf_state_key_setup(struct pf_pdesc *, struct pf_addr *,
 			    struct pf_addr *, u_int16_t, u_int16_t);
 struct pf_state_key	*pf_state_key_clone(struct pf_state_key *);
+
+struct pfi_kkif		*pf_kkif_create(int);
+void			 pf_kkif_free(struct pfi_kkif *);
 #endif /* _KERNEL */
 
 #endif /* _NET_PFVAR_H_ */
diff --git a/sys/netpfil/pf/pf_if.c b/sys/netpfil/pf/pf_if.c
index a0148395340f..3230a3e424f6 100644
--- a/sys/netpfil/pf/pf_if.c
+++ b/sys/netpfil/pf/pf_if.c
@@ -122,7 +122,7 @@ pfi_initialize_vnet(void)
 	V_pfi_buffer = malloc(V_pfi_buffer_max * sizeof(*V_pfi_buffer),
 	    PFI_MTYPE, M_WAITOK);
 
-	kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
+	kif = pf_kkif_create(M_WAITOK);
 	PF_RULES_WLOCK();
 	V_pfi_all = pfi_kkif_attach(kif, IFG_ALL);
 	PF_RULES_WUNLOCK();
@@ -169,13 +169,13 @@ pfi_cleanup_vnet(void)
 			if_rele(kif->pfik_ifp);
 			kif->pfik_ifp->if_pf_kif = NULL;
 		}
-		free(kif, PFI_MTYPE);
+		pf_kkif_free(kif);
 	}
 
 	mtx_lock(&pfi_unlnkdkifs_mtx);
 	while ((kif = LIST_FIRST(&V_pfi_unlinked_kifs))) {
 		LIST_REMOVE(kif, pfik_list);
-		free(kif, PFI_MTYPE);
+		pf_kkif_free(kif);
 	}
 	mtx_unlock(&pfi_unlnkdkifs_mtx);
 
@@ -194,6 +194,25 @@ pfi_cleanup(void)
 	EVENTHANDLER_DEREGISTER(ifaddr_event, pfi_ifaddr_event_cookie);
 }
 
+struct pfi_kkif*
+pf_kkif_create(int flags)
+{
+	struct pfi_kkif *kif;
+
+	kif = malloc(sizeof(*kif), PFI_MTYPE, flags);
+
+	return (kif);
+}
+
+void
+pf_kkif_free(struct pfi_kkif *kif)
+{
+	if (! kif)
+		return;
+
+	free(kif, PFI_MTYPE);
+}
+
 struct pfi_kkif *
 pfi_kkif_find(const char *kif_name)
 {
@@ -217,7 +236,7 @@ pfi_kkif_attach(struct pfi_kkif *kif, const char *kif_name)
 
 	kif1 = pfi_kkif_find(kif_name);
 	if (kif1 != NULL) {
-		free(kif, PFI_MTYPE);
+		pf_kkif_free(kif);
 		return (kif1);
 	}
 
@@ -286,7 +305,7 @@ pfi_kkif_purge(void)
 	LIST_FOREACH_SAFE(kif, &V_pfi_unlinked_kifs, pfik_list, kif1) {
 		if (!(kif->pfik_flags & PFI_IFLAG_REFS)) {
 			LIST_REMOVE(kif, pfik_list);
-			free(kif, PFI_MTYPE);
+			pf_kkif_free(kif);
 		} else
 			kif->pfik_flags &= ~PFI_IFLAG_REFS;
 	}
@@ -403,7 +422,7 @@ pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af)
 	if ((dyn = malloc(sizeof(*dyn), PFI_MTYPE, M_NOWAIT | M_ZERO)) == NULL)
 		return (ENOMEM);
 
-	if ((kif = malloc(sizeof(*kif), PFI_MTYPE, M_NOWAIT)) == NULL) {
+	if ((kif = pf_kkif_create(M_NOWAIT)) == NULL) {
 		free(dyn, PFI_MTYPE);
 		return (ENOMEM);
 	}
@@ -811,7 +830,7 @@ pfi_set_flags(const char *name, int flags)
 {
 	struct pfi_kkif	*p, *kif;
 
-	kif = malloc(sizeof(*kif), PFI_MTYPE, M_NOWAIT);
+	kif = pf_kkif_create(M_NOWAIT);
 	if (kif == NULL)
 		return (ENOMEM);
 
@@ -839,7 +858,7 @@ pfi_clear_flags(const char *name, int flags)
 		    p->pfik_flags == 0 && p->pfik_rulerefs == 0) {
 			/* Delete this kif. */
 			RB_REMOVE(pfi_ifhead, &V_pfi_ifs, p);
-			free(p, PFI_MTYPE);
+			pf_kkif_free(p);
 		}
 	}
 	return (0);
@@ -933,7 +952,7 @@ pfi_change_group_event(void *arg __unused, char *gname)
 		return;
 	}
 
-	kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
+	kif = pf_kkif_create(M_WAITOK);
 	PF_RULES_WLOCK();
 	V_pfi_update++;
 	kif = pfi_kkif_attach(kif, gname);
diff --git a/sys/netpfil/pf/pf_ioctl.c b/sys/netpfil/pf/pf_ioctl.c
index f152202c2515..866c4ebfaa3d 100644
--- a/sys/netpfil/pf/pf_ioctl.c
+++ b/sys/netpfil/pf/pf_ioctl.c
@@ -1838,7 +1838,7 @@ pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td
 		pf_rule_to_krule(&pr->rule, rule);
 
 		if (rule->ifname[0])
-			kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
+			kif = pf_kkif_create(M_WAITOK);
 		rule->evaluations = counter_u64_alloc(M_WAITOK);
 		for (int i = 0; i < 2; i++) {
 			rule->packets[i] = counter_u64_alloc(M_WAITOK);
@@ -1981,7 +1981,7 @@ DIOCADDRULE_error:
 		counter_u64_free(rule->src_nodes);
 		free(rule, M_PFRULE);
 		if (kif)
-			free(kif, PFI_MTYPE);
+			pf_kkif_free(kif);
 		break;
 	}
 
@@ -2108,7 +2108,7 @@ DIOCADDRULE_error:
 			pf_rule_to_krule(&pcr->rule, newrule);
 
 			if (newrule->ifname[0])
-				kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
+				kif = pf_kkif_create(M_WAITOK);
 			newrule->evaluations = counter_u64_alloc(M_WAITOK);
 			for (int i = 0; i < 2; i++) {
 				newrule->packets[i] =
@@ -2299,7 +2299,7 @@ DIOCCHANGERULE_error:
 			free(newrule, M_PFRULE);
 		}
 		if (kif != NULL)
-			free(kif, PFI_MTYPE);
+			pf_kkif_free(kif);
 		break;
 	}
 
@@ -2936,12 +2936,12 @@ DIOCGETSTATES_full:
 		pa = malloc(sizeof(*pa), M_PFRULE, M_WAITOK);
 		pf_pooladdr_to_kpooladdr(&pp->addr, pa);
 		if (pa->ifname[0])
-			kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
+			kif = pf_kkif_create(M_WAITOK);
 		PF_RULES_WLOCK();
 		if (pp->ticket != V_ticket_pabuf) {
 			PF_RULES_WUNLOCK();
 			if (pa->ifname[0])
-				free(kif, PFI_MTYPE);
+				pf_kkif_free(kif);
 			free(pa, M_PFRULE);
 			error = EBUSY;
 			break;
@@ -3053,7 +3053,7 @@ DIOCGETSTATES_full:
 			newpa = malloc(sizeof(*newpa), M_PFRULE, M_WAITOK);
 			bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
 			if (newpa->ifname[0])
-				kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
+				kif = pf_kkif_create(M_WAITOK);
 			newpa->kif = NULL;
 		}
 
@@ -3145,7 +3145,7 @@ DIOCCHANGEADDR_error:
 		}
 		PF_RULES_WUNLOCK();
 		if (kif != NULL)
-			free(kif, PFI_MTYPE);
+			pf_kkif_free(kif);
 		break;
 	}
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202101201445.10KEj1OL019475>