From nobody Thu Feb 13 12:39:00 2025 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Ytvr636vBz5nR9F; Thu, 13 Feb 2025 12:39:02 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Ytvr50k2fz3fth; Thu, 13 Feb 2025 12:39:01 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1739450341; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=457nwYn5IqHF50r5MJ/Uu6RqzwSiPinoihD5g0JPb9k=; b=qG8OIBZur+eCNNMlzHiDQY07ogTZ3Urqw3Efql6TBigKY+dj2GlIjzWBXFNReyh2waJ7g6 LQOzaIc4z1NE4M65iCu6owEmU4ytNR/wsaAHeYS3g5CQ00UaSipYwHEWu3WMl6TLDc4eGR orUEKC20FdGdaV/bjH2s8Ud6gf+XHz1zA28ObCffPUhLrw02LngYvI4CIYar7QIH3hGJF6 0E9We7pt4GxPNF06VrIBNGQExER5S7lVlrH7XE6z5PYC8HBiklubPWLSYCT0QY+0phMrHo i7otNHYSARCtmrwSG1fct94pKZOLNYwDIqmGKiFJnXEYGo2kngcpUABGVaqzDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1739450341; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=457nwYn5IqHF50r5MJ/Uu6RqzwSiPinoihD5g0JPb9k=; b=yKmT9Bh7d4EiD1gJvVTSnJc+cHdQhkwlpM63bN3e2UsAvPz8e0lWvGeDgqYAjstz3QvflG 1V83Q8BmtFYpc4ZTsfCRWQZReS3O841ICNonIVS4+BSwsSLKcE8IjiJ/rkIh+oELC+QHOh max5RplVY8AqIffMhIzyEMgLbSEa4lKHjXJHQiFs1DsMY7rptwMAp7kMSvMFphqcmAWDh/ 85jHRusd4Oe86ncIiFoIJoDfFu466v7Etc4dtejkGDEf+yUg0rmPE+S+xOL/qtkCVJGSLm Z9cZjnZAq3lPn2EVomzTv+JRAKvBRU2jUiMMzkssWsciXjNA+NV1upI3kmA2SQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1739450341; a=rsa-sha256; cv=none; b=xh6okDwWFDJXr6u0Udy7DhdVpxdTYjsxJfRsWiJU0LuoZek3h7WKHnaizuYfMndqNeP1up rCtqzOKbxnPJsJs97geAPIK3q8a4bEOFA+cXW2VmXkv+d8+lIDUH4/W6BU80W8a3ZmPUEC rbz0Rvn97plJhUUi00E+SVSmFaeekbzGZ5FArj7Y5+hlds9W9Avx9KldvkdSB6bHUdmFZM XRGe1ZeHqYLAw0y0zgpaCWOZ+1/Z2jWVSxDWhIU7nbGQzl35X4iSw8Lo4f9CPQRs8CUT7B HHj2n+hBp9ett3UXbw4zO2O0RpzgBbyFAlcRn4k1VQJ0DlidSUGBFdtFG32wtA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Ytvr508kkzm0q; Thu, 13 Feb 2025 12:39:01 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 51DCd0kk075446; Thu, 13 Feb 2025 12:39:00 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 51DCd0jv075443; Thu, 13 Feb 2025 12:39:00 GMT (envelope-from git) Date: Thu, 13 Feb 2025 12:39:00 GMT Message-Id: <202502131239.51DCd0jv075443@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kristof Provost Subject: git: 7cef9d196ce9 - main - pfctl: cache name/index mappings List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 7cef9d196ce999a61ec864961a3849c9159a96a8 Auto-Submitted: auto-generated The branch main has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=7cef9d196ce999a61ec864961a3849c9159a96a8 commit 7cef9d196ce999a61ec864961a3849c9159a96a8 Author: Kristof Provost AuthorDate: 2025-02-10 09:44:06 +0000 Commit: Kristof Provost CommitDate: 2025-02-13 12:38:43 +0000 pfctl: cache name/index mappings Provide local implementations of if_nametoindex(3) and if_indextoname(3) that make use of the cache of addresses populated by the ifa_load on startup to save the trouble of calling expensive getaddrinfo(3) up to four times per rule. Performance wise this change provides a speed up factor of 20 with a 11k line ruleset on a machine with 150 VLANs and 250 IP addresses (20 seconds down to 1 in this case). "wow!" henning, ok benno, florian Obtained from: OpenBSD, mikeb , 918dda8655 Sponsored by: Rubicon Communications, LLC ("Netgate") --- sbin/pfctl/parse.y | 8 ++++---- sbin/pfctl/pfctl_parser.c | 31 +++++++++++++++++++++++++++++++ sbin/pfctl/pfctl_parser.h | 2 ++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/sbin/pfctl/parse.y b/sbin/pfctl/parse.y index efbd7cac18e8..40d116fe1a50 100644 --- a/sbin/pfctl/parse.y +++ b/sbin/pfctl/parse.y @@ -6216,9 +6216,9 @@ expand_rule(struct pfctl_rule *r, (src_host->ifindex && dst_host->ifindex && src_host->ifindex != dst_host->ifindex) || (src_host->ifindex && *interface->ifname && - src_host->ifindex != if_nametoindex(interface->ifname)) || + src_host->ifindex != ifa_nametoindex(interface->ifname)) || (dst_host->ifindex && *interface->ifname && - dst_host->ifindex != if_nametoindex(interface->ifname))) + dst_host->ifindex != ifa_nametoindex(interface->ifname))) continue; if (!r->af && src_host->af) r->af = src_host->af; @@ -6228,9 +6228,9 @@ expand_rule(struct pfctl_rule *r, if (*interface->ifname) strlcpy(r->ifname, interface->ifname, sizeof(r->ifname)); - else if (if_indextoname(src_host->ifindex, ifname)) + else if (ifa_indextoname(src_host->ifindex, ifname)) strlcpy(r->ifname, ifname, sizeof(r->ifname)); - else if (if_indextoname(dst_host->ifindex, ifname)) + else if (ifa_indextoname(dst_host->ifindex, ifname)) strlcpy(r->ifname, ifname, sizeof(r->ifname)); else memset(r->ifname, '\0', sizeof(r->ifname)); diff --git a/sbin/pfctl/pfctl_parser.c b/sbin/pfctl/pfctl_parser.c index f567cc78dd23..d1e0b4e99940 100644 --- a/sbin/pfctl/pfctl_parser.c +++ b/sbin/pfctl/pfctl_parser.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -1518,6 +1519,8 @@ ifa_load(void) ifa->ifa_addr)->sin6_scope_id; } else if (n->af == AF_LINK) { ifa_add_groups_to_map(ifa->ifa_name); + n->ifindex = ((struct sockaddr_dl *) + ifa->ifa_addr)->sdl_index; } if ((n->ifname = strdup(ifa->ifa_name)) == NULL) err(1, "ifa_load: strdup"); @@ -1584,6 +1587,34 @@ is_a_group(char *name) return (*(int *)ret_item->data); } +unsigned int +ifa_nametoindex(const char *ifa_name) +{ + struct node_host *p; + + for (p = iftab; p; p = p->next) { + if (p->af == AF_LINK && strcmp(p->ifname, ifa_name) == 0) + return (p->ifindex); + } + errno = ENXIO; + return (0); +} + +char * +ifa_indextoname(unsigned int ifindex, char *ifa_name) +{ + struct node_host *p; + + for (p = iftab; p; p = p->next) { + if (p->af == AF_LINK && ifindex == p->ifindex) { + strlcpy(ifa_name, p->ifname, IFNAMSIZ); + return (ifa_name); + } + } + errno = ENXIO; + return (NULL); +} + struct node_host * ifa_exists(char *ifa_name) { diff --git a/sbin/pfctl/pfctl_parser.h b/sbin/pfctl/pfctl_parser.h index 2b0766dfd99f..e916f5e7c100 100644 --- a/sbin/pfctl/pfctl_parser.h +++ b/sbin/pfctl/pfctl_parser.h @@ -363,6 +363,8 @@ int check_netmask(struct node_host *, sa_family_t); int unmask(struct pf_addr *, sa_family_t); struct node_host *gen_dynnode(struct node_host *, sa_family_t); void ifa_load(void); +unsigned int ifa_nametoindex(const char *); +char *ifa_indextoname(unsigned int, char *); int get_query_socket(void); struct node_host *ifa_exists(char *); struct node_host *ifa_grouplookup(char *ifa_name, int flags);