git: 6d3bc576abbd - main - libexec/rc: improve performance of pccard_ether script

From: Eugene Grosbein <eugen_at_FreeBSD.org>
Date: Tue, 01 Jul 2025 14:19:44 UTC
The branch main has been updated by eugen:

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

commit 6d3bc576abbd84f736d917f5bfec4e3fe7e6c125
Author:     Eugene Grosbein <eugen@FreeBSD.org>
AuthorDate: 2025-07-01 14:13:10 +0000
Commit:     Eugene Grosbein <eugen@FreeBSD.org>
CommitDate: 2025-07-01 14:13:10 +0000

    libexec/rc: improve performance of pccard_ether script
    
    Replace "ifconfig -ul" with "ifconfig -n" because netlink-enabled
    /sbin/ifconfig utility has sub-optimal performance for listing.
    
    Combined with the commit b1b17432aa1be670564161232d110461a5dde4ce,
    these changes mostly eliminate performance regression of the command
    "service devd start" for a system having hundreds of network interfaces
    created before devd starts, after FreeBSD 14+ switched
    /sbin/ifconfig to netlink(4)
    
    PR:             287872
    MFC-after:      2 weeks
---
 libexec/rc/network.subr | 20 ++++++++++++++++++++
 libexec/rc/pccard_ether | 17 +++++++++--------
 2 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/libexec/rc/network.subr b/libexec/rc/network.subr
index 931fbec19a60..2113a3f66f0f 100644
--- a/libexec/rc/network.subr
+++ b/libexec/rc/network.subr
@@ -653,6 +653,26 @@ ifexists()
 	${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
 }
 
+# ifisup if
+#	Returns 0 if the interface exists and UP,
+#	returns 1 if the interface exists and not UP,
+#	returns 2 otherwise.
+ifisup()
+{
+	local _if
+
+	[ -z "$1" ] && return 1
+	_if="$1"
+
+	set -- $(${IFCONFIG_CMD} -n ${_if} 2>/dev/null)
+	case "$1$2" in
+		${_if}:*'<UP'[,\>]*)	return 0 ;;
+		${_if}:*)	 	return 1 ;;
+	esac
+
+	return 2
+}
+
 # ipv4_up if
 #	add IPv4 addresses to the interface $if
 ipv4_up()
diff --git a/libexec/rc/pccard_ether b/libexec/rc/pccard_ether
index 7ca58f210085..957983e55a8e 100755
--- a/libexec/rc/pccard_ether
+++ b/libexec/rc/pccard_ether
@@ -69,16 +69,17 @@ checkauto()
 
 pccard_ether_start()
 {
-	ifexists $ifn || exit 1
-
-	if [ -z "$rc_force" ]; then
-		for uif in `ifconfig -ul`; do
-			if [ "${uif}" = "${ifn}" ]; then
-				# Interface is already up, so ignore it.
+	ifisup $ifn
+	case $? in
+		0)	# Interface is already up, so ignore it.
+			if [ -z "$rc_force"]; then
 				exit 0
 			fi
-		done
-	fi
+			;;
+		2)	# Interface does not exist.
+			exit 1
+			;;
+	esac
 
 	/etc/rc.d/netif quietstart $ifn