1 /*- 2 * Copyright (c) 1980, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)mbufs.c 8.1 (Berkeley) 6/6/93 30 * $FreeBSD: src/usr.bin/systat/ip.c,v 1.3.2.1 2001/04/25 12:42:18 ru Exp $ 31 * $DragonFly: src/usr.bin/systat/ip.c,v 1.5 2004/05/03 15:18:25 hmp Exp $ 32 */ 33 34 #include <sys/param.h> 35 #include <sys/types.h> 36 #include <sys/socket.h> 37 #include <sys/sysctl.h> 38 39 #include <netinet/in.h> 40 #include <netinet/in_systm.h> 41 #include <netinet/ip.h> 42 #include <netinet/ip_var.h> 43 #include <netinet/udp.h> 44 #include <netinet/udp_var.h> 45 46 #include <stdlib.h> 47 #include <string.h> 48 #include <paths.h> 49 #include "systat.h" 50 #include "extern.h" 51 #include "mode.h" 52 53 struct stat { 54 struct ip_stats i; 55 struct udpstat u; 56 }; 57 58 static struct stat curstat, initstat, oldstat; 59 60 /*- 61 --0 1 2 3 4 5 6 7 62 --0123456789012345678901234567890123456789012345678901234567890123456789012345 63 01 IP Input IP Output 64 02999999999 total packets received 999999999 total packets sent 65 03999999999 - with bad checksums 999999999 - generated locally 66 04999999999 - too short for header 999999999 - output drops 67 05999999999 - too short for data 999999999 output fragments generated 68 06999999999 - with invalid hlen 999999999 - fragmentation failed 69 07999999999 - with invalid length 999999999 destinations unreachable 70 08999999999 - with invalid version 999999999 packets output via raw IP 71 09999999999 - jumbograms 72 10999999999 total fragments received UDP Statistics 73 11999999999 - fragments dropped 999999999 total input packets 74 12999999999 - fragments timed out 999999999 - too short for header 75 13999999999 - packets reassembled ok 999999999 - invalid checksum 76 14999999999 packets forwarded 999999999 - no checksum 77 15999999999 - unreachable dests 999999999 - invalid length 78 16999999999 - redirects generated 999999999 - no socket for dest port 79 17999999999 option errors 999999999 - no socket for broadcast 80 18999999999 unwanted multicasts 999999999 - socket buffer full 81 19999999999 delivered to upper layer 999999999 total output packets 82 --0123456789012345678901234567890123456789012345678901234567890123456789012345 83 --0 1 2 3 4 5 6 7 84 */ 85 86 WINDOW * 87 openip(void) 88 { 89 return (subwin(stdscr, LINES-4-1, 0, 4, 0)); 90 } 91 92 void 93 closeip(WINDOW *w) 94 { 95 if (w == NULL) 96 return; 97 wclear(w); 98 wrefresh(w); 99 delwin(w); 100 } 101 102 void 103 labelip(void) 104 { 105 wmove(wnd, 0, 0); wclrtoeol(wnd); 106 #define L(row, str) mvwprintw(wnd, row, 10, str) 107 #define R(row, str) mvwprintw(wnd, row, 45, str); 108 L(1, "IP Input"); R(1, "IP Output"); 109 L(2, "total packets received"); R(2, "total packets sent"); 110 L(3, "- with bad checksums"); R(3, "- generated locally"); 111 L(4, "- too short for header"); R(4, "- output drops"); 112 L(5, "- too short for data"); R(5, "output fragments generated"); 113 L(6, "- with invalid hlen"); R(6, "- fragmentation failed"); 114 L(7, "- with invalid length"); R(7, "destinations unreachable"); 115 L(8, "- with invalid version"); R(8, "packets output via raw IP"); 116 L(9, "- jumbograms"); 117 L(10, "total fragments received"); R(10, "UDP Statistics"); 118 L(11, "- fragments dropped"); R(11, "total input packets"); 119 L(12, "- fragments timed out"); R(12, "- too short for header"); 120 L(13, "- packets reassembled ok"); R(13, "- invalid checksum"); 121 L(14, "packets forwarded"); R(14, "- no checksum"); 122 L(15, "- unreachable dests"); R(15, "- invalid length"); 123 L(16, "- redirects generated"); R(16, "- no socket for dest port"); 124 L(17, "option errors"); R(17, "- no socket for broadcast"); 125 L(18, "unwanted multicasts"); R(18, "- socket buffer full"); 126 L(19, "delivered to upper layer"); R(19, "total output packets"); 127 #undef L 128 #undef R 129 } 130 131 static void 132 domode(struct stat *ret) 133 { 134 const struct stat *sub; 135 double divisor = 1.0; 136 137 switch(currentmode) { 138 case display_RATE: 139 sub = &oldstat; 140 divisor = naptime; 141 break; 142 case display_DELTA: 143 sub = &oldstat; 144 break; 145 case display_SINCE: 146 sub = &initstat; 147 break; 148 default: 149 *ret = curstat; 150 return; 151 } 152 #define DO(stat) ret->stat = (double)(curstat.stat - sub->stat) / divisor 153 DO(i.ips_total); 154 DO(i.ips_badsum); 155 DO(i.ips_tooshort); 156 DO(i.ips_toosmall); 157 DO(i.ips_badhlen); 158 DO(i.ips_badlen); 159 DO(i.ips_fragments); 160 DO(i.ips_fragdropped); 161 DO(i.ips_fragtimeout); 162 DO(i.ips_forward); 163 DO(i.ips_cantforward); 164 DO(i.ips_redirectsent); 165 DO(i.ips_noproto); 166 DO(i.ips_delivered); 167 DO(i.ips_localout); 168 DO(i.ips_odropped); 169 DO(i.ips_reassembled); 170 DO(i.ips_fragmented); 171 DO(i.ips_ofragments); 172 DO(i.ips_cantfrag); 173 DO(i.ips_badoptions); 174 DO(i.ips_noroute); 175 DO(i.ips_badvers); 176 DO(i.ips_rawout); 177 DO(i.ips_toolong); 178 DO(i.ips_notmember); 179 DO(u.udps_ipackets); 180 DO(u.udps_hdrops); 181 DO(u.udps_badsum); 182 DO(u.udps_nosum); 183 DO(u.udps_badlen); 184 DO(u.udps_noport); 185 DO(u.udps_noportbcast); 186 DO(u.udps_fullsock); 187 DO(u.udps_opackets); 188 #undef DO 189 } 190 191 void 192 showip(void) 193 { 194 struct stat stats; 195 u_long totalout; 196 197 domode(&stats); 198 totalout = stats.i.ips_forward + stats.i.ips_localout; 199 200 #define DO(stat, row, col) \ 201 mvwprintw(wnd, row, col, "%9lu", stats.stat) 202 203 DO(i.ips_total, 2, 0); 204 mvwprintw(wnd, 2, 35, "%9lu", totalout); 205 DO(i.ips_badsum, 3, 0); 206 DO(i.ips_localout, 3, 35); 207 DO(i.ips_tooshort, 4, 0); 208 DO(i.ips_odropped, 4, 35); 209 DO(i.ips_toosmall, 5, 0); 210 DO(i.ips_ofragments, 5, 35); 211 DO(i.ips_badhlen, 6, 0); 212 DO(i.ips_cantfrag, 6, 35); 213 DO(i.ips_badlen, 7, 0); 214 DO(i.ips_noroute, 7, 35); 215 DO(i.ips_badvers, 8, 0); 216 DO(i.ips_rawout, 8, 35); 217 DO(i.ips_toolong, 9, 0); 218 DO(i.ips_fragments, 10, 0); 219 DO(i.ips_fragdropped, 11, 0); 220 DO(u.udps_ipackets, 11, 35); 221 DO(i.ips_fragtimeout, 12, 0); 222 DO(u.udps_hdrops, 12, 35); 223 DO(i.ips_reassembled, 13, 0); 224 DO(u.udps_badsum, 13, 35); 225 DO(i.ips_forward, 14, 0); 226 DO(u.udps_nosum, 14, 35); 227 DO(i.ips_cantforward, 15, 0); 228 DO(u.udps_badlen, 15, 35); 229 DO(i.ips_redirectsent, 16, 0); 230 DO(u.udps_noport, 16, 35); 231 DO(i.ips_badoptions, 17, 0); 232 DO(u.udps_noportbcast, 17, 35); 233 DO(i.ips_notmember, 18, 0); 234 DO(u.udps_fullsock, 18, 35); 235 DO(i.ips_delivered, 19, 0); 236 DO(u.udps_opackets, 19, 35); 237 #undef DO 238 } 239 240 #define CPU_STATS_FUNC(proto,type) \ 241 static void \ 242 proto ##_stats_agg(type *ary, type *ttl, int cpucnt) \ 243 { \ 244 int i, off, siz; \ 245 siz = sizeof(type); \ 246 \ 247 if (!ary && !ttl) \ 248 return; \ 249 \ 250 bzero(ttl, siz); \ 251 if (cpucnt == 1) { \ 252 *ttl = ary[0]; \ 253 } else { \ 254 for (i = 0; i < cpucnt; ++i) { \ 255 for (off = 0; off < siz; off += sizeof(u_long)) { \ 256 *(u_long *)((char *)(*(&ttl)) + off) += \ 257 *(u_long *)((char *)&ary[i] + off); \ 258 } \ 259 } \ 260 } \ 261 } 262 CPU_STATS_FUNC(ip, struct ip_stats); 263 264 static int 265 fetch_ipstats(struct ip_stats *st) 266 { 267 struct ip_stats stattmp[SMP_MAXCPU]; 268 size_t len; 269 int name[4], cpucnt; 270 271 name[0] = CTL_NET; 272 name[1] = PF_INET; 273 name[2] = IPPROTO_IP; 274 name[3] = IPCTL_STATS; 275 276 len = sizeof(struct ip_stats) * SMP_MAXCPU; 277 if (sysctl(name, 4, stattmp, &len, NULL, 0) < 0) { 278 error("sysctl getting ip_stats failed"); 279 return -1; 280 } 281 cpucnt = len / sizeof(struct ip_stats); 282 ip_stats_agg(stattmp, st, cpucnt); 283 284 return 0; 285 } 286 287 int 288 initip(void) 289 { 290 size_t len; 291 int name[4]; 292 293 if (fetch_ipstats(&initstat.i) < 0) 294 return 0; 295 296 name[0] = CTL_NET; 297 name[1] = PF_INET; 298 name[2] = IPPROTO_UDP; 299 name[3] = UDPCTL_STATS; 300 301 len = 0; 302 if (sysctl(name, 4, 0, &len, 0, 0) < 0) { 303 error("sysctl getting udpstat size failed"); 304 return 0; 305 } 306 if (len > sizeof curstat.u) { 307 error("ip_stats structure has grown--recompile systat!"); 308 return 0; 309 } 310 if (sysctl(name, 4, &initstat.u, &len, 0, 0) < 0) { 311 error("sysctl getting udpstat failed"); 312 return 0; 313 } 314 oldstat = initstat; 315 return 1; 316 } 317 318 void 319 resetip(void) 320 { 321 size_t len; 322 int name[4]; 323 324 fetch_ipstats(&initstat.i); 325 326 name[0] = CTL_NET; 327 name[1] = PF_INET; 328 name[2] = IPPROTO_UDP; 329 name[3] = UDPCTL_STATS; 330 331 len = sizeof initstat.u; 332 if (sysctl(name, 4, &initstat.u, &len, 0, 0) < 0) { 333 error("sysctl getting udpstat failed"); 334 } 335 oldstat = initstat; 336 } 337 338 void 339 fetchip(void) 340 { 341 int name[4]; 342 size_t len; 343 344 oldstat = curstat; 345 346 if (fetch_ipstats(&curstat.i) < 0) 347 return; 348 349 name[0] = CTL_NET; 350 name[1] = PF_INET; 351 name[2] = IPPROTO_UDP; 352 name[3] = UDPCTL_STATS; 353 len = sizeof curstat.u; 354 355 if (sysctl(name, 4, &curstat.u, &len, 0, 0) < 0) 356 return; 357 } 358