Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/net Added SPPP_LOG() for refactoring around log



details:   https://anonhg.NetBSD.org/src/rev/434cfb94dc02
branches:  trunk
changeset: 1021484:434cfb94dc02
user:      yamaguchi <yamaguchi%NetBSD.org@localhost>
date:      Tue Jun 01 05:08:46 2021 +0000

description:
Added SPPP_LOG() for refactoring around log

diffstat:

 sys/net/if_spppsubr.c |  824 ++++++++++++++++++++++---------------------------
 1 files changed, 372 insertions(+), 452 deletions(-)

diffs (truncated from 2058 to 300 lines):

diff -r 83b344b77c1f -r 434cfb94dc02 sys/net/if_spppsubr.c
--- a/sys/net/if_spppsubr.c     Tue Jun 01 05:04:06 2021 +0000
+++ b/sys/net/if_spppsubr.c     Tue Jun 01 05:08:46 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_spppsubr.c,v 1.253 2021/06/01 05:04:06 yamaguchi Exp $       */
+/*     $NetBSD: if_spppsubr.c,v 1.254 2021/06/01 05:08:46 yamaguchi Exp $       */
 
 /*
  * Synchronous PPP/Cisco link level subroutines.
@@ -41,7 +41,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_spppsubr.c,v 1.253 2021/06/01 05:04:06 yamaguchi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_spppsubr.c,v 1.254 2021/06/01 05:08:46 yamaguchi Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_inet.h"
@@ -319,6 +319,17 @@
 #define SPPP_DOWNGRADE(_sp)    rw_downgrade(&(_sp)->pp_lock)
 #define SPPP_WQ_SET(_wk, _func, _arg)  \
        sppp_wq_set((_wk), (_func), __UNCONST((_arg)))
+#define SPPP_LOG(_sp, _lvl, _fmt, _args...)    do {            \
+       if (__predict_true((_sp) != NULL)) {                    \
+               log((_lvl), "%s: ", (_sp)->pp_if.if_xname);     \
+       }                                                       \
+       addlog((_fmt), ##_args);                                \
+} while (0)
+#define SPPP_DLOG(_sp, _fmt, _args...) do {    \
+       if (!sppp_debug_enabled(_sp))                   \
+               break;                                  \
+       SPPP_LOG(_sp, LOG_DEBUG, _fmt, ##_args);        \
+} while (0)
 
 #ifdef INET
 #ifndef SPPPSUBR_MPSAFE
@@ -339,10 +350,8 @@
 #endif
 
 /* almost every function needs these */
-#define STDDCL                                                 \
-       struct ifnet *ifp = &sp->pp_if;                         \
-       int debug = ifp->if_flags & IFF_DEBUG
-
+
+static bool sppp_debug_enabled(struct sppp *sp);
 static int sppp_output(struct ifnet *, struct mbuf *,
                       const struct sockaddr *, const struct rtentry *);
 
@@ -580,13 +589,28 @@
        return PPP_NOPROTO;
 }
 
+static inline bool
+sppp_debug_enabled(struct sppp *sp)
+{
+
+       if (__predict_false(sp == NULL))
+               return false;
+
+       if ((sp->pp_if.if_flags & IFF_DEBUG) == 0)
+               return false;
+
+       return true;
+}
+
 static void
 sppp_change_phase(struct sppp *sp, int phase)
 {
-       STDDCL;
+       struct ifnet *ifp;
 
        KASSERT(SPPP_WLOCKED(sp));
 
+       ifp = &sp->pp_if;
+
        if (sp->pp_phase == phase)
                return;
 
@@ -597,11 +621,8 @@
        else
                if_link_state_change(ifp, LINK_STATE_DOWN);
 
-       if (debug)
-       {
-               log(LOG_INFO, "%s: phase %s\n", ifp->if_xname,
-                       sppp_phase_name(sp->pp_phase));
-       }
+       SPPP_DLOG(sp, "phase %s\n",
+           sppp_phase_name(sp->pp_phase));
 }
 
 /*
@@ -619,7 +640,6 @@
        struct ifqueue *inq = NULL;
        uint16_t protocol;
        struct sppp *sp = (struct sppp *)ifp;
-       int debug = ifp->if_flags & IFF_DEBUG;
        int isr = 0;
 
        SPPP_LOCK(sp, RW_READER);
@@ -633,10 +653,8 @@
 
        if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
                /* Too small packet, drop it. */
-               if (debug)
-                       log(LOG_DEBUG,
-                           "%s: input packet is too small, %d bytes\n",
-                           ifp->if_xname, m->m_pkthdr.len);
+               SPPP_DLOG(sp, "input packet is too small, "
+                   "%d bytes\n", m->m_pkthdr.len);
                goto drop;
        }
 
@@ -658,21 +676,15 @@
                case CISCO_MULTICAST:
                case CISCO_UNICAST:
                        /* Don't check the control field here (RFC 1547). */
-                       if (debug)
-                               log(LOG_DEBUG,
-                                   "%s: Cisco packet in PPP mode "
-                                   "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
-                                   ifp->if_xname,
-                                   h->address, h->control, ntohs(h->protocol));
+                       SPPP_DLOG(sp, "Cisco packet in PPP mode "
+                           "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
+                           h->address, h->control, ntohs(h->protocol));
                        goto drop;
                default:        /* Invalid PPP packet. */
                  invalid:
-                       if (debug)
-                               log(LOG_DEBUG,
-                                   "%s: invalid input packet "
-                                   "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
-                                   ifp->if_xname,
-                                   h->address, h->control, ntohs(h->protocol));
+                       SPPP_DLOG(sp, "invalid input packet "
+                           "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
+                           h->address, h->control, ntohs(h->protocol));
                        goto drop;
                }
                protocol = ntohs(h->protocol);
@@ -692,11 +704,8 @@
                if_statinc(ifp, if_noproto);
                goto drop;
        default:
-               if (debug) {
-                       log(LOG_DEBUG,
-                           "%s: invalid input protocol "
-                           "<proto=0x%x>\n", ifp->if_xname, protocol);
-               }
+               SPPP_DLOG(sp, "invalid input protocol "
+                   "<proto=0x%x>\n", protocol);
                goto reject_protocol;
        case PPP_LCP:
                SPPP_UNLOCK(sp);
@@ -720,9 +729,8 @@
 #ifdef INET
        case PPP_IPCP:
                if (!ISSET(sp->pp_ncpflags, SPPP_NCP_IPCP)) {
-                       log(LOG_INFO, "%s: reject IPCP packet "
-                           "because IPCP is disabled\n",
-                           ifp->if_xname);
+                       SPPP_LOG(sp, LOG_INFO, "reject IPCP packet "
+                           "because IPCP is disabled\n");
                        goto reject_protocol;
                }
                SPPP_UNLOCK(sp);
@@ -743,9 +751,8 @@
 #ifdef INET6
        case PPP_IPV6CP:
                if (!ISSET(sp->pp_ncpflags, SPPP_NCP_IPV6CP)) {
-                       log(LOG_INFO, "%s: reject IPv6CP packet "
-                           "because IPv6CP is disabled\n",
-                           ifp->if_xname);
+                       SPPP_LOG(sp, LOG_INFO, "reject IPv6CP packet "
+                           "because IPv6CP is disabled\n");
                        goto reject_protocol;
                }
                SPPP_UNLOCK(sp);
@@ -786,10 +793,7 @@
                /* Queue overflow. */
                IF_DROP(inq);
                IFQ_UNLOCK(inq);
-               if (debug)
-                       log(LOG_DEBUG, "%s: protocol queue overflow\n",
-                               ifp->if_xname);
-
+               SPPP_DLOG(sp,"protocol queue overflow\n");
                SPPP_LOCK(sp, RW_READER);
                goto drop;
        }
@@ -932,9 +936,7 @@
                 */
                M_PREPEND(m, PPP_HEADER_LEN, M_DONTWAIT);
                if (! m) {
-                       if (ifp->if_flags & IFF_DEBUG)
-                               log(LOG_DEBUG, "%s: no memory for transmit header\n",
-                                       ifp->if_xname);
+                       SPPP_DLOG(sp, "no memory for transmit header\n");
                        if_statinc(ifp, if_oerrors);
                        SPPP_UNLOCK(sp);
                        splx(s);
@@ -1015,9 +1017,7 @@
        if (sp->pp_flags & PP_NOFRAMING) {
                M_PREPEND(m, 2, M_DONTWAIT);
                if (m == NULL) {
-                       if (ifp->if_flags & IFF_DEBUG)
-                               log(LOG_DEBUG, "%s: no memory for transmit header\n",
-                                       ifp->if_xname);
+                       SPPP_DLOG(sp, "no memory for transmit header\n");
                        if_statinc(ifp, if_oerrors);
                        SPPP_UNLOCK(sp);
                        splx(s);
@@ -1309,14 +1309,9 @@
                        sp->pp_saved_mtu = ifp->if_mtu;
                        ifp->if_mtu = sp->lcp.their_mru;
 
-                       if (ifp->if_flags & IFF_DEBUG) {
-                               log(LOG_DEBUG,
-                                   "%s: setting MTU "
-                                   "from %"PRIu64" bytes "
-                                   "to %"PRIu64" bytes\n",
-                                   ifp->if_xname, sp->pp_saved_mtu,
-                                   ifp->if_mtu);
-                       }
+                       SPPP_DLOG(sp, "setting MTU "
+                           "from %"PRIu64" bytes to %"PRIu64" bytes\n",
+                           sp->pp_saved_mtu, ifp->if_mtu);
                }
                SPPP_UNLOCK(sp);
                break;
@@ -1397,13 +1392,14 @@
 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
             u_char ident, u_short len, void *data)
 {
-       STDDCL;
+       struct ifnet *ifp;
        struct lcp_header *lh;
        struct mbuf *m;
        size_t pkthdrlen;
 
        KASSERT(SPPP_WLOCKED(sp));
 
+       ifp = &sp->pp_if;
        pkthdrlen = (sp->pp_flags & PP_NOFRAMING) ? 2 : PPP_HEADER_LEN;
 
        if (len > MHLEN - pkthdrlen - LCP_HEADER_LEN)
@@ -1432,16 +1428,15 @@
        if (len)
                memcpy(lh + 1, data, len);
 
-       if (debug) {
+       if (sppp_debug_enabled(sp)) {
                char pbuf[SPPP_PROTO_NAMELEN];
                char tbuf[SPPP_CPTYPE_NAMELEN];
                const char *pname, *cpname;
 
                pname = sppp_proto_name(pbuf, sizeof(pbuf), proto);
                cpname = sppp_cp_type_name(tbuf, sizeof(tbuf), lh->type);
-               log(LOG_DEBUG, "%s: %s output <%s id=0x%x len=%d",
-                   ifp->if_xname, pname, cpname,
-                   lh->ident, ntohs(lh->len));
+               SPPP_LOG(sp, LOG_DEBUG, "%s output <%s id=0x%x len=%d",
+                   pname, cpname, lh->ident, ntohs(lh->len));
                if (len)
                        sppp_print_bytes((u_char *)(lh + 1), len);
                addlog(">\n");
@@ -1576,7 +1571,7 @@
 static void
 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
 {
-       STDDCL;
+       struct ifnet *ifp;
        struct lcp_header *h;
        struct sppp_cp *scp;
        int printlen, len = m->m_pkthdr.len;
@@ -1584,26 +1579,25 @@
        uint32_t u32;
        char tbuf[SPPP_CPTYPE_NAMELEN];
        const char *cpname;
+       bool debug;
 
        SPPP_LOCK(sp, RW_WRITER);
 
+       ifp = &sp->pp_if;
+       debug = sppp_debug_enabled(sp);
        scp = &sp->scp[cp->protoidx];
 
        if (len < 4) {
-               if (debug)
-                       log(LOG_DEBUG,
-                           "%s: %s invalid packet length: %d bytes\n",
-                           ifp->if_xname, cp->name, len);
+               SPPP_DLOG(sp, "%s invalid packet length: %d bytes\n",
+                   cp->name, len);
                goto out;
        }
        h = mtod(m, struct lcp_header *);
        if (debug) {



Home | Main Index | Thread Index | Old Index