Hi,

I noticed for some time now a relatively high CPU usage of the
user level ppp even on slow links. On my ADSL line (768/128)
ppp consumes up to 20% CPU time (AMD K6-2 300MHz).

I trussed the process and noticed, that it calls getprotobynumber()
for each packet it receives. In most cases, the result from this call
isn't used at all - only with debugging enabled or on errors.
getprotobynumber() scans /etc/protocols - or even more expensive functions
if NSS is involved.

Below is a patch which reduces the getprotobynumber() calls, so they only
get called if there is log output at all.

With this patch, the CPU usage dropped from 20% to under 3% (on my full
blown ADSL link)


Daniel
Index: usr.sbin/ppp/ip.c
===================================================================
RCS file: /export/cvs/src/usr.sbin/ppp/ip.c,v
retrieving revision 1.100
diff -u -r1.100 ip.c
--- usr.sbin/ppp/ip.c   26 Mar 2003 02:27:32 -0000      1.100
+++ usr.sbin/ppp/ip.c   11 Dec 2003 22:20:27 -0000
@@ -161,6 +161,18 @@
   }
 }
 
+char *toprototxt(int cproto)
+{
+  static char prototxt[16];
+  struct protoent *pe;
+
+  if ((pe = getprotobynumber(cproto)) == NULL)
+    snprintf(prototxt, sizeof prototxt, "%d", cproto);
+  else
+    snprintf(prototxt, sizeof prototxt, "%s", pe->p_name);
+  return prototxt;
+}
+
 /*
  * Check a packet against the given filter
  * Returns 0 to accept the packet, non-zero to drop the packet.
@@ -187,8 +199,7 @@
   int match;                   /* true if condition matched */
   int mindata;                 /* minimum data size or zero */
   const struct filterent *fp = filter->rule;
-  char dbuff[100], dstip[16], prototxt[16];
-  struct protoent *pe;
+  char dbuff[100], dstip[16];
   struct ncpaddr srcaddr, dstaddr;
   const char *payload;         /* IP payload */
   int datalen;                 /* IP datagram length */
@@ -239,10 +250,6 @@
     cproto = pip->ip_p;
   }
 
-  if ((pe = getprotobynumber(cproto)) == NULL)
-    snprintf(prototxt, sizeof prototxt, "%d", cproto);
-  else
-    snprintf(prototxt, sizeof prototxt, "%s", pe->p_name);
   gotinfo = estab = syn = finrst = didname = 0;
   sport = dport = 0;
 
@@ -356,7 +363,7 @@
 
           if (datalen < mindata) {
             log_Printf(LogFILTER, " error: proto %s must be at least"
-                       " %d octets\n", prototxt, mindata);
+                       " %d octets\n", toprototxt(cproto), mindata);
             return 1;
           }
 
@@ -367,7 +374,8 @@
                        ", estab = %d, syn = %d, finrst = %d",
                        estab, syn, finrst);
             }
-            log_Printf(LogDEBUG, " Filter: proto = %s, %s\n", prototxt, dbuff);
+            log_Printf(LogDEBUG, " Filter: proto = %s, %s\n",
+                       toprototxt(cproto), dbuff);
           }
           gotinfo = 1;
         }
@@ -424,7 +432,8 @@
             if (log_IsKept(LogFILTER)) {
               snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
               log_Printf(LogFILTER, "%sbound rule = %d accept %s "
-                         "src = %s:%d dst = %s:%d\n", filter->name, n, prototxt,
+                         "src = %s:%d dst = %s:%d\n", filter->name, n,
+                         toprototxt(cproto),
                          ncpaddr_ntoa(&srcaddr), sport, dstip, dport);
             }
           }
@@ -434,7 +443,7 @@
             snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
             log_Printf(LogFILTER,
                        "%sbound rule = %d deny %s src = %s/%d dst = %s/%d\n",
-                       filter->name, n, prototxt,
+                       filter->name, n, toprototxt(cproto),
                        ncpaddr_ntoa(&srcaddr), sport, dstip, dport);
           }
           return 1;
@@ -450,7 +459,7 @@
     snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
     log_Printf(LogFILTER,
                "%sbound rule = implicit deny %s src = %s/%d dst = %s/%d\n",
-               filter->name, prototxt, ncpaddr_ntoa(&srcaddr), sport,
+               filter->name, toprototxt(cproto), ncpaddr_ntoa(&srcaddr), sport,
                dstip, dport);
   }
 
_______________________________________________
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[EMAIL PROTECTED]"

Reply via email to