Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/sys/netinet/ip_input.c,v rcsdiff: /ftp/cvs/cvsroot/src/sys/netinet/ip_input.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.254.4.4 retrieving revision 1.267 diff -u -p -r1.254.4.4 -r1.267 --- src/sys/netinet/ip_input.c 2008/02/18 21:07:08 1.254.4.4 +++ src/sys/netinet/ip_input.c 2008/04/23 06:09:04 1.267 @@ -1,4 +1,4 @@ -/* $NetBSD: ip_input.c,v 1.254.4.4 2008/02/18 21:07:08 mjf Exp $ */ +/* $NetBSD: ip_input.c,v 1.267 2008/04/23 06:09:04 thorpej Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -98,7 +98,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.254.4.4 2008/02/18 21:07:08 mjf Exp $"); +__KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.267 2008/04/23 06:09:04 thorpej Exp $"); #include "opt_inet.h" #include "opt_gateway.h" @@ -135,6 +135,7 @@ __KERNEL_RCSID(0, "$NetBSD: ip_input.c,v #include #include #include +#include #include /* just for gif_ttl */ #include @@ -148,6 +149,7 @@ __KERNEL_RCSID(0, "$NetBSD: ip_input.c,v #ifdef IPSEC #include +#include #include #endif #ifdef FAST_IPSEC @@ -230,9 +232,10 @@ u_long in_multihash; /* size of hash int in_multientries; /* total number of addrs */ struct in_multihashhead *in_multihashtbl; struct ifqueue ipintrq; -struct ipstat ipstat; uint16_t ip_id; +percpu_t *ipstat_percpu; + #ifdef PFIL_HOOKS struct pfil_head inet_pfil_hook; #endif @@ -447,6 +450,8 @@ ip_init(void) MOWNER_ATTACH(&ip_tx_mowner); MOWNER_ATTACH(&ip_rx_mowner); #endif /* MBUFTRACE */ + + ipstat_percpu = percpu_alloc(sizeof(uint64_t) * IP_NSTATS); } struct sockaddr_in ipaddr = { @@ -511,7 +516,7 @@ ip_input(struct mbuf *m) */ if (TAILQ_FIRST(&in_ifaddrhead) == 0) goto bad; - ipstat.ips_total++; + IP_STATINC(IP_STAT_TOTAL); /* * If the IP header is not aligned, slurp it up into a new * mbuf with space for link headers, in the event we forward @@ -522,28 +527,28 @@ ip_input(struct mbuf *m) if ((m = m_copyup(m, sizeof(struct ip), (max_linkhdr + 3) & ~3)) == NULL) { /* XXXJRT new stat, please */ - ipstat.ips_toosmall++; + IP_STATINC(IP_STAT_TOOSMALL); return; } } else if (__predict_false(m->m_len < sizeof (struct ip))) { if ((m = m_pullup(m, sizeof (struct ip))) == NULL) { - ipstat.ips_toosmall++; + IP_STATINC(IP_STAT_TOOSMALL); return; } } ip = mtod(m, struct ip *); if (ip->ip_v != IPVERSION) { - ipstat.ips_badvers++; + IP_STATINC(IP_STAT_BADVERS); goto bad; } hlen = ip->ip_hl << 2; if (hlen < sizeof(struct ip)) { /* minimum header length */ - ipstat.ips_badhlen++; + IP_STATINC(IP_STAT_BADHLEN); goto bad; } if (hlen > m->m_len) { if ((m = m_pullup(m, hlen)) == 0) { - ipstat.ips_badhlen++; + IP_STATINC(IP_STAT_BADHLEN); return; } ip = mtod(m, struct ip *); @@ -554,7 +559,7 @@ ip_input(struct mbuf *m) * not allowed. */ if (IN_MULTICAST(ip->ip_src.s_addr)) { - ipstat.ips_badaddr++; + IP_STATINC(IP_STAT_BADADDR); goto bad; } @@ -562,7 +567,7 @@ ip_input(struct mbuf *m) if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { - ipstat.ips_badaddr++; + IP_STATINC(IP_STAT_BADADDR); goto bad; } } @@ -600,7 +605,7 @@ ip_input(struct mbuf *m) * Check for additional length bogosity */ if (len < hlen) { - ipstat.ips_badlen++; + IP_STATINC(IP_STAT_BADLEN); goto bad; } @@ -611,7 +616,7 @@ ip_input(struct mbuf *m) * Drop packet if shorter than we expect. */ if (m->m_pkthdr.len < len) { - ipstat.ips_tooshort++; + IP_STATINC(IP_STAT_TOOSHORT); goto bad; } if (m->m_pkthdr.len > len) { @@ -780,7 +785,7 @@ ip_input(struct mbuf *m) * ip_output().) */ if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) { - ipstat.ips_cantforward++; + IP_STATINC(IP_STAT_CANTFORWARD); m_freem(m); return; } @@ -792,7 +797,7 @@ ip_input(struct mbuf *m) */ if (ip->ip_p == IPPROTO_IGMP) goto ours; - ipstat.ips_forward++; + IP_STATINC(IP_STAT_CANTFORWARD); } #endif /* @@ -801,7 +806,7 @@ ip_input(struct mbuf *m) */ IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); if (inm == NULL) { - ipstat.ips_cantforward++; + IP_STATINC(IP_STAT_CANTFORWARD); m_freem(m); return; } @@ -815,7 +820,7 @@ ip_input(struct mbuf *m) * Not for us; forward if possible and desirable. */ if (ipforwarding == 0) { - ipstat.ips_cantforward++; + IP_STATINC(IP_STAT_CANTFORWARD); m_freem(m); } else { /* @@ -826,12 +831,12 @@ ip_input(struct mbuf *m) */ if (downmatch) { icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); - ipstat.ips_cantforward++; + IP_STATINC(IP_STAT_CANTFORWARD); return; } #ifdef IPSEC if (ipsec4_in_reject(m, NULL)) { - ipsecstat.in_polvio++; + IPSEC_STATINC(IPSEC_STAT_IN_POLVIO); goto bad; } #endif @@ -859,7 +864,7 @@ ip_input(struct mbuf *m) KEY_FREESP(&sp); splx(s); if (error) { - ipstat.ips_cantforward++; + IP_STATINC(IP_STAT_CANTFORWARD); goto bad; } @@ -906,7 +911,7 @@ ours: */ off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3; if ((off > 0 ? off + hlen : len) < IP_MINFRAGSIZE - 1) { - ipstat.ips_badfrags++; + IP_STATINC(IP_STAT_BADFRAGS); goto bad; } /* @@ -925,7 +930,7 @@ ours: * fragments. */ if (ip->ip_tos != fp->ipq_tos) { - ipstat.ips_badfrags++; + IP_STATINC(IP_STAT_BADFRAGS); goto bad; } goto found; @@ -948,7 +953,7 @@ found: */ if (ntohs(ip->ip_len) == 0 || (ntohs(ip->ip_len) & 0x7) != 0) { - ipstat.ips_badfrags++; + IP_STATINC(IP_STAT_BADFRAGS); IPQ_UNLOCK(); goto bad; } @@ -961,12 +966,12 @@ found: * attempt reassembly; if it succeeds, proceed. */ if (mff || ip->ip_off != htons(0)) { - ipstat.ips_fragments++; + IP_STATINC(IP_STAT_FRAGMENTS); s = splvm(); ipqe = pool_get(&ipqent_pool, PR_NOWAIT); splx(s); if (ipqe == NULL) { - ipstat.ips_rcvmemdrop++; + IP_STATINC(IP_STAT_RCVMEMDROP); IPQ_UNLOCK(); goto bad; } @@ -978,7 +983,7 @@ found: IPQ_UNLOCK(); return; } - ipstat.ips_reassembled++; + IP_STATINC(IP_STAT_REASSEMBLED); ip = mtod(m, struct ip *); hlen = ip->ip_hl << 2; ip->ip_len = htons(ntohs(ip->ip_len) + hlen); @@ -996,7 +1001,7 @@ found: */ if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 && ipsec4_in_reject(m, NULL)) { - ipsecstat.in_polvio++; + IPSEC_STATINC(IPSEC_STAT_IN_POLVIO); goto bad; } #endif @@ -1046,7 +1051,7 @@ DPRINTF(("ip_input: no SP, packet discar if (ia && ip) ia->ia_ifa.ifa_data.ifad_inbytes += ntohs(ip->ip_len); #endif - ipstat.ips_delivered++; + IP_STATINC(IP_STAT_DELIVERED); { int off = hlen, nh = ip->ip_p; @@ -1058,7 +1063,7 @@ bad: return; badcsum: - ipstat.ips_badsum++; + IP_STATINC(IP_STAT_BADSUM); m_freem(m); } @@ -1216,7 +1221,7 @@ insert: q = TAILQ_FIRST(&fp->ipq_fragq); ip = q->ipqe_ip; if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) { - ipstat.ips_toolong++; + IP_STATINC(IP_STAT_TOOLONG); ip_freef(fp); return (0); } @@ -1266,7 +1271,7 @@ dropfrag: if (fp != 0) fp->ipq_nfrags--; ip_nfrags--; - ipstat.ips_fragdropped++; + IP_STATINC(IP_STAT_FRAGDROPPED); m_freem(m); s = splvm(); pool_put(&ipqent_pool, ipqe); @@ -1333,7 +1338,7 @@ ip_reass_ttl_decr(u_int ticks) 0 : fp->ipq_ttl - ticks); nfp = LIST_NEXT(fp, ipq_q); if (fp->ipq_ttl == 0) { - ipstat.ips_fragtimeout++; + IP_STATINC(IP_STAT_FRAGTIMEOUT); ip_freef(fp); } else { nfrags += fp->ipq_nfrags; @@ -1680,7 +1685,7 @@ ip_dooptions(struct mbuf *m) return (0); bad: icmp_error(m, type, code, 0, 0); - ipstat.ips_badoptions++; + IP_STATINC(IP_STAT_BADOPTIONS); return (1); } @@ -1853,7 +1858,7 @@ ip_forward(struct mbuf *m, int srcrt) } #endif if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { - ipstat.ips_cantforward++; + IP_STATINC(IP_STAT_CANTFORWARD); m_freem(m); return; } @@ -1917,12 +1922,15 @@ ip_forward(struct mbuf *m, int srcrt) (struct ip_moptions *)NULL, (struct socket *)NULL); if (error) - ipstat.ips_cantforward++; + IP_STATINC(IP_STAT_CANTFORWARD); else { - ipstat.ips_forward++; - if (type) - ipstat.ips_redirectsent++; - else { + uint64_t *ips = IP_STAT_GETREF(); + ips[IP_STAT_FORWARD]++; + if (type) { + ips[IP_STAT_REDIRECTSENT]++; + IP_STAT_PUTREF(); + } else { + IP_STAT_PUTREF(); if (mcopy) { #ifdef GATEWAY if (mcopy->m_flags & M_CANFASTFWD) @@ -1954,17 +1962,17 @@ ip_forward(struct mbuf *m, int srcrt) case EMSGSIZE: type = ICMP_UNREACH; code = ICMP_UNREACH_NEEDFRAG; -#if !defined(IPSEC) && !defined(FAST_IPSEC) - if ((rt = rtcache_validate(&ipforward_rt)) != NULL) - destmtu = rt->rt_ifp->if_mtu; -#else - /* - * If the packet is routed over IPsec tunnel, tell the - * originator the tunnel MTU. - * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz - * XXX quickhack!!! - */ + if ((rt = rtcache_validate(&ipforward_rt)) != NULL) { + +#if defined(IPSEC) || defined(FAST_IPSEC) + /* + * If the packet is routed over IPsec tunnel, tell the + * originator the tunnel MTU. + * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz + * XXX quickhack!!! + */ + struct secpolicy *sp; int ipsecerror; size_t ipsechdr; @@ -1973,10 +1981,11 @@ ip_forward(struct mbuf *m, int srcrt) sp = ipsec4_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND, IP_FORWARDING, &ipsecerror); +#endif - if (sp == NULL) - destmtu = rt->rt_ifp->if_mtu; - else { + destmtu = rt->rt_ifp->if_mtu; +#if defined(IPSEC) || defined(FAST_IPSEC) + if (sp != NULL) { /* count IPsec header size */ ipsechdr = ipsec4_hdrsiz(mcopy, IPSEC_DIR_OUTBOUND, NULL); @@ -2005,9 +2014,9 @@ ip_forward(struct mbuf *m, int srcrt) KEY_FREESP(&sp); #endif } +#endif /*defined(IPSEC) || defined(FAST_IPSEC)*/ } -#endif /*IPSEC*/ - ipstat.ips_cantfrag++; + IP_STATINC(IP_STAT_CANTFRAG); break; case ENOBUFS: @@ -2150,7 +2159,7 @@ sysctl_net_inet_ip_maxflows(SYSCTLFN_ARG return (s); s = splsoftnet(); - ipflow_reap(0); + ipflow_prune(); splx(s); return (0); @@ -2186,6 +2195,17 @@ sysctl_net_inet_ip_hashsize(SYSCTLFN_ARG } #endif /* GATEWAY */ +static int +sysctl_net_inet_ip_stats(SYSCTLFN_ARGS) +{ + netstat_sysctl_context ctx; + uint64_t ips[IP_NSTATS]; + + ctx.ctx_stat = ipstat_percpu; + ctx.ctx_counters = ips; + ctx.ctx_ncounters = IP_NSTATS; + return (NETSTAT_SYSCTL(&ctx)); +} SYSCTL_SETUP(sysctl_net_inet_ip_setup, "sysctl net.inet.ip subtree setup") { @@ -2390,7 +2410,15 @@ SYSCTL_SETUP(sysctl_net_inet_ip_setup, " CTLFLAG_PERMANENT, CTLTYPE_STRUCT, "stats", SYSCTL_DESCR("IP statistics"), - NULL, 0, &ipstat, sizeof(ipstat), + sysctl_net_inet_ip_stats, 0, NULL, 0, CTL_NET, PF_INET, IPPROTO_IP, IPCTL_STATS, CTL_EOL); } + +void +ip_statinc(u_int stat) +{ + + KASSERT(stat < IP_NSTATS); + IP_STATINC(stat); +}