git: adba114dfbfb - main - netinet: use hashalloc(9) for IP address hash

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Sun, 12 Apr 2026 17:39:59 UTC
The branch main has been updated by glebius:

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

commit adba114dfbfb02c6f332237abf070cbcc480f914
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2026-04-12 17:26:03 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2026-04-12 17:26:03 +0000

    netinet: use hashalloc(9) for IP address hash
    
    While here, slightly restyle ip_vnet_init() and use sparse initializer for
    pfil_head_args.  There is no functional change wrt to pfil(9) hook
    registration.
    
    Differential Revision:  https://reviews.freebsd.org/D56175
---
 sys/netinet/ip_input.c | 43 +++++++++++++++++++++++++++----------------
 1 file changed, 27 insertions(+), 16 deletions(-)

diff --git a/sys/netinet/ip_input.c b/sys/netinet/ip_input.c
index 5800a0854ee5..8602cf0d5266 100644
--- a/sys/netinet/ip_input.c
+++ b/sys/netinet/ip_input.c
@@ -52,6 +52,7 @@
 #include <sys/sdt.h>
 #include <sys/syslog.h>
 #include <sys/sysctl.h>
+#include <sys/hash.h>
 
 #include <net/if.h>
 #include <net/if_types.h>
@@ -175,9 +176,6 @@ VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead);  /* first inet address */
 VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); /* inet addr hash table  */
 VNET_DEFINE(u_long, in_ifaddrhmask);		/* mask for hash table */
 
-/* Make sure it is safe to use hashinit(9) on CK_LIST. */
-CTASSERT(sizeof(struct in_ifaddrhashhead) == sizeof(LIST_HEAD(, in_addr)));
-
 #ifdef IPCTL_DEFMTU
 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
     &ip_mtu, 0, "Default MTU");
@@ -309,24 +307,32 @@ SYSCTL_PROC(_net_inet_ip, IPCTL_INTRDQDROPS, intr_direct_queue_drops,
 static void
 ip_vnet_init(void *arg __unused)
 {
-	struct pfil_head_args args;
-
 	CK_STAILQ_INIT(&V_in_ifaddrhead);
-	V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
+
+	struct hashalloc_args ha = {
+		.size = INADDR_NHASH,
+		.mtype = M_IFADDR,
+		.mflags = M_WAITOK,
+		.head = HASH_HEAD_CK_LIST,
+	};
+	V_in_ifaddrhashtbl = hashalloc(&ha);
+	V_in_ifaddrhmask = ha.size - 1;
 
 	/* Initialize IP reassembly queue. */
 	ipreass_vnet_init();
 
 	/* Initialize packet filter hooks. */
-	args.pa_version = PFIL_VERSION;
-	args.pa_flags = PFIL_IN | PFIL_OUT;
-	args.pa_type = PFIL_TYPE_IP4;
-	args.pa_headname = PFIL_INET_NAME;
-	V_inet_pfil_head = pfil_head_register(&args);
-
-	args.pa_flags = PFIL_OUT;
-	args.pa_headname = PFIL_INET_LOCAL_NAME;
-	V_inet_local_pfil_head = pfil_head_register(&args);
+	struct pfil_head_args pa = {
+		.pa_version = PFIL_VERSION,
+		.pa_flags = PFIL_IN | PFIL_OUT,
+		.pa_type = PFIL_TYPE_IP4,
+		.pa_headname = PFIL_INET_NAME,
+	};
+	V_inet_pfil_head = pfil_head_register(&pa);
+
+	pa.pa_flags = PFIL_OUT;
+	pa.pa_headname = PFIL_INET_LOCAL_NAME;
+	V_inet_local_pfil_head = pfil_head_register(&pa);
 
 	if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET,
 	    &V_ipsec_hhh_in[HHOOK_IPSEC_INET],
@@ -423,7 +429,12 @@ ip_destroy(void *unused __unused)
 	ipreass_destroy();
 
 	/* Cleanup in_ifaddr hash table; should be empty. */
-	hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask);
+	struct hashalloc_args ha = {
+		.mtype = M_IFADDR,
+		.head = HASH_HEAD_CK_LIST,
+		.size = V_in_ifaddrhmask + 1,
+	};
+	hashfree(V_in_ifaddrhashtbl, &ha);
 }
 
 VNET_SYSUNINIT(ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip_destroy, NULL);