Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 28 Jan 2019 20:30:05 +0000 (UTC)
From:      Patrick Kelsey <pkelsey@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r343535 - head/sbin/ifconfig
Message-ID:  <201901282030.x0SKU56I012302@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: pkelsey
Date: Mon Jan 28 20:30:04 2019
New Revision: 343535
URL: https://svnweb.freebsd.org/changeset/base/343535

Log:
  Speed up non-status operations applied to a single interface
  
  When performing a non-status operation on a single interface, it is
  not necessary for ifconfig to build a list of all addresses in the
  system, sort them, then iterate through them looking for the entry for
  the single interface of interest. Doing so becomes increasingly
  expensive as the number of interfaces in the system grows (e.g., in a
  system with 1000+ vlan(4) interfaces).
  
  Reviewed by:	ae, kp
  MFC after:	1 week
  Sponsored by:	RG Nets
  Differential Revision:	https://reviews.freebsd.org/D18919

Modified:
  head/sbin/ifconfig/ifconfig.c

Modified: head/sbin/ifconfig/ifconfig.c
==============================================================================
--- head/sbin/ifconfig/ifconfig.c	Mon Jan 28 20:26:09 2019	(r343534)
+++ head/sbin/ifconfig/ifconfig.c	Mon Jan 28 20:30:04 2019	(r343535)
@@ -111,6 +111,8 @@ static	void status(const struct afswtch *afp, const st
 static	void tunnel_status(int s);
 static _Noreturn void usage(void);
 
+static int getifflags(const char *ifname, int us);
+
 static struct afswtch *af_getbyname(const char *name);
 static struct afswtch *af_getbyfamily(int af);
 static void af_other_status(int);
@@ -369,6 +371,7 @@ main(int argc, char *argv[])
 	const char *ifname;
 	struct option *p;
 	size_t iflen;
+	int flags;
 
 	all = downonly = uponly = namesonly = noload = verbose = 0;
 	f_inet = f_inet6 = f_ether = f_addr = NULL;
@@ -526,6 +529,25 @@ main(int argc, char *argv[])
 			argc--, argv++;
 	}
 
+	/*
+	 * Check for a requested configuration action on a single interface,
+	 * which doesn't require building, sorting, and searching the entire
+	 * system address list
+	 */
+	if ((argc > 0) && (ifname != NULL)) {
+		iflen = strlcpy(name, ifname, sizeof(name));
+		if (iflen >= sizeof(name)) {
+			warnx("%s: interface name too long, skipping", ifname);
+		} else {
+			flags = getifflags(name, -1);
+			if (!(((flags & IFF_CANTCONFIG) != 0) ||
+				(downonly && (flags & IFF_UP) != 0) ||
+				(uponly && (flags & IFF_UP) == 0)))
+				ifconfig(argc, argv, 0, afp);
+		}
+		goto done;
+	}
+
 	if (getifaddrs(&ifap) != 0)
 		err(EXIT_FAILURE, "getifaddrs");
 
@@ -609,6 +631,7 @@ main(int argc, char *argv[])
 		printf("\n");
 	freeifaddrs(ifap);
 
+done:
 	freeformat();
 	exit(exit_code);
 }
@@ -1020,6 +1043,28 @@ setifdstaddr(const char *addr, int param __unused, int
 		afp->af_getaddr(addr, DSTADDR);
 }
 
+static int
+getifflags(const char *ifname, int us)
+{
+	struct ifreq my_ifr;
+	int s;
+	
+	memset(&my_ifr, 0, sizeof(my_ifr));
+	(void) strlcpy(my_ifr.ifr_name, ifname, sizeof(my_ifr.ifr_name));
+	if (us < 0) {
+		if ((s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)
+			err(1, "socket(family AF_LOCAL,SOCK_DGRAM");
+	} else
+		s = us;
+ 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
+ 		Perror("ioctl (SIOCGIFFLAGS)");
+ 		exit(1);
+ 	}
+	if (us < 0)
+		close(s);
+	return ((my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16));
+}
+
 /*
  * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
  * of the ifreq structure, which may confuse other parts of ifconfig.
@@ -1031,20 +1076,14 @@ setifflags(const char *vname, int value, int s, const 
 	struct ifreq		my_ifr;
 	int flags;
 
-	memset(&my_ifr, 0, sizeof(my_ifr));
-	(void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
-
- 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
- 		Perror("ioctl (SIOCGIFFLAGS)");
- 		exit(1);
- 	}
-	flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
-
+	flags = getifflags(name, s);
 	if (value < 0) {
 		value = -value;
 		flags &= ~value;
 	} else
 		flags |= value;
+	memset(&my_ifr, 0, sizeof(my_ifr));
+	(void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
 	my_ifr.ifr_flags = flags & 0xffff;
 	my_ifr.ifr_flagshigh = flags >> 16;
 	if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)



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