Author: pkelsey
Date: Mon Feb 11 23:35:34 2019
New Revision: 344030
URL: https://svnweb.freebsd.org/changeset/base/344030

Log:
  MFC r343535:
  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
  Sponsored by: RG Nets
  Differential Revision:        https://reviews.freebsd.org/D18919

Modified:
  stable/12/sbin/ifconfig/ifconfig.c
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/sbin/ifconfig/ifconfig.c
==============================================================================
--- stable/12/sbin/ifconfig/ifconfig.c  Mon Feb 11 23:33:16 2019        
(r344029)
+++ stable/12/sbin/ifconfig/ifconfig.c  Mon Feb 11 23:35:34 2019        
(r344030)
@@ -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)
_______________________________________________
[email protected] mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "[email protected]"

Reply via email to