Annotation of src/usr.bin/systat/if.c, Revision 1.19
1.19 ! jasper 1: /* $OpenBSD: if.c,v 1.18 2010/07/05 14:31:44 lum Exp $ */
1.1 markus 2: /*
3: * Copyright (c) 2004 Markus Friedl <markus@openbsd.org>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
1.15 jasper 17:
1.1 markus 18: #include <sys/param.h>
19: #include <sys/types.h>
20: #include <sys/socket.h>
21: #include <sys/sysctl.h>
22: #include <net/if.h>
23: #include <net/if_dl.h>
24: #include <net/route.h>
1.13 deraadt 25: #include <sys/sockio.h>
1.15 jasper 26: #include <sys/ioctl.h>
1.1 markus 27:
28: #include <stdlib.h>
1.5 deraadt 29: #include <string.h>
1.15 jasper 30: #include <unistd.h>
1.1 markus 31:
32: #include "systat.h"
33:
34: static enum state { BOOT, TIME, RUN } state = TIME;
35:
36: struct ifstat {
37: char ifs_name[IFNAMSIZ]; /* interface name */
1.13 deraadt 38: char ifs_description[IFDESCRSIZE];
1.1 markus 39: struct ifcount ifs_cur;
40: struct ifcount ifs_old;
41: struct ifcount ifs_now;
1.17 canacar 42: char ifs_flag;
1.1 markus 43: } *ifstats;
44:
45: static int nifs = 0;
1.12 canacar 46: static int num_ifs = 0;
1.1 markus 47:
1.12 canacar 48: void print_if(void);
49: int read_if(void);
50: int select_if(void);
51: int if_keyboard_callback(int);
52:
1.16 deraadt 53: void fetchifstat(void);
1.12 canacar 54: static void showifstat(struct ifstat *);
55: static void showtotal(void);
1.18 lum 56: static void rt_getaddrinfo(struct sockaddr *, int, struct sockaddr **);
1.12 canacar 57:
58:
59: /* Define fields */
60: field_def fields_if[] = {
61: {"IFACE", 8, 16, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0},
1.13 deraadt 62: {"STATE", 4, 6, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0},
1.12 canacar 63: {"IPKTS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
64: {"IBYTES", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
65: {"IERRS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
66: {"OPKTS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
67: {"OBYTES", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
68: {"OERRS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
69: {"COLLS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
1.13 deraadt 70: {"DESC", 14, 64, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0},
1.12 canacar 71: };
72:
73:
1.19 ! jasper 74: #define FLD_IF_IFACE FIELD_ADDR(fields_if,0)
! 75: #define FLD_IF_STATE FIELD_ADDR(fields_if,1)
! 76: #define FLD_IF_IPKTS FIELD_ADDR(fields_if,2)
! 77: #define FLD_IF_IBYTES FIELD_ADDR(fields_if,3)
! 78: #define FLD_IF_IERRS FIELD_ADDR(fields_if,4)
! 79: #define FLD_IF_OPKTS FIELD_ADDR(fields_if,5)
! 80: #define FLD_IF_OBYTES FIELD_ADDR(fields_if,6)
! 81: #define FLD_IF_OERRS FIELD_ADDR(fields_if,7)
! 82: #define FLD_IF_COLLS FIELD_ADDR(fields_if,8)
! 83: #define FLD_IF_DESC FIELD_ADDR(fields_if,9)
1.12 canacar 84:
85:
86: /* Define views */
87: field_def *view_if_0[] = {
1.13 deraadt 88: FLD_IF_IFACE, FLD_IF_STATE, FLD_IF_DESC, FLD_IF_IPKTS,
89: FLD_IF_IBYTES, FLD_IF_IERRS, FLD_IF_OPKTS, FLD_IF_OBYTES,
90: FLD_IF_OERRS, FLD_IF_COLLS, NULL
1.12 canacar 91: };
92:
93: /* Define view managers */
94:
95: struct view_manager ifstat_mgr = {
96: "Ifstat", select_if, read_if, NULL, print_header,
97: print_if, if_keyboard_callback, NULL, NULL
98: };
99:
100: field_view views_if[] = {
101: {view_if_0, "ifstat", '1', &ifstat_mgr},
102: {NULL, NULL, 0, NULL}
103: };
1.7 claudio 104:
1.1 markus 105:
106: int
107: initifstat(void)
108: {
1.12 canacar 109: field_view *v;
110: read_if();
111: for (v = views_if; v->name != NULL; v++)
112: add_view(v);
1.1 markus 113:
114: return(1);
115: }
116:
117: #define UPDATE(x, y) do { \
1.2 deraadt 118: ifs->ifs_now.x = ifm.y; \
1.1 markus 119: ifs->ifs_cur.x = ifs->ifs_now.x - ifs->ifs_old.x; \
1.4 dlg 120: if (state == TIME) {\
121: ifs->ifs_old.x = ifs->ifs_now.x; \
122: ifs->ifs_cur.x /= naptime; \
123: } \
1.1 markus 124: sum.x += ifs->ifs_cur.x; \
125: } while(0)
126:
127:
128: void
129: rt_getaddrinfo(struct sockaddr *sa, int addrs, struct sockaddr **info)
130: {
131: int i;
132:
133: for (i = 0; i < RTAX_MAX; i++) {
134: if (addrs & (1 << i)) {
135: info[i] = sa;
136: sa = (struct sockaddr *) ((char *)(sa) +
137: roundup(sa->sa_len, sizeof(long)));
138: } else
139: info[i] = NULL;
140: }
141: }
142:
1.12 canacar 143:
144:
145: int
146: select_if(void)
147: {
148: num_disp = num_ifs + 1;
149: return (0);
150: }
151:
152: int
153: read_if(void)
154: {
155: fetchifstat();
156: num_disp = num_ifs + 1;
157:
158: return 0;
159: }
160:
1.1 markus 161: void
1.12 canacar 162: print_if(void)
163: {
164: int n, i, count = 0;
165:
166: for (n = 0, i = 0; n < nifs; n++) {
167: if (ifstats[n].ifs_name[0] == '\0')
168: continue;
169: if (i++ < dispstart)
170: continue;
171: if (i == num_disp)
172: break;
173: showifstat(ifstats + n);
174: if (maxprint > 0 && ++count >= maxprint)
175: return;
176: }
177: showtotal();
178: }
179:
180:
1.16 deraadt 181: void
1.1 markus 182: fetchifstat(void)
183: {
184: struct ifstat *newstats, *ifs;
1.2 deraadt 185: struct if_msghdr ifm;
1.1 markus 186: struct sockaddr *info[RTAX_MAX];
187: struct sockaddr_dl *sdl;
188: char *buf, *next, *lim;
1.17 canacar 189: int mib[6], i;
1.1 markus 190: size_t need;
191:
192: mib[0] = CTL_NET;
193: mib[1] = AF_ROUTE;
194: mib[2] = 0;
195: mib[3] = 0;
196: mib[4] = NET_RT_IFLIST;
197: mib[5] = 0;
198:
199: if (sysctl(mib, 6, NULL, &need, NULL, 0) == -1)
200: return;
201: if ((buf = malloc(need)) == NULL)
202: return;
203: if (sysctl(mib, 6, buf, &need, NULL, 0) == -1) {
204: free(buf);
205: return;
206: }
207:
208: bzero(&sum, sizeof(sum));
1.12 canacar 209: num_ifs = 0;
1.1 markus 210:
211: lim = buf + need;
1.2 deraadt 212: for (next = buf; next < lim; next += ifm.ifm_msglen) {
213: bcopy(next, &ifm, sizeof ifm);
1.14 claudio 214: if (ifm.ifm_version != RTM_VERSION ||
215: ifm.ifm_type != RTM_IFINFO ||
216: !(ifm.ifm_addrs & RTA_IFP))
1.1 markus 217: continue;
1.2 deraadt 218: if (ifm.ifm_index >= nifs) {
1.12 canacar 219: if ((newstats = realloc(ifstats, (ifm.ifm_index + 4)
220: * sizeof(struct ifstat))) == NULL)
1.1 markus 221: continue;
222: ifstats = newstats;
1.2 deraadt 223: for (; nifs < ifm.ifm_index + 4; nifs++)
1.12 canacar 224: bzero(&ifstats[nifs], sizeof(*ifstats));
1.1 markus 225: }
1.2 deraadt 226: ifs = &ifstats[ifm.ifm_index];
1.1 markus 227: if (ifs->ifs_name[0] == '\0') {
228: bzero(&info, sizeof(info));
1.2 deraadt 229: rt_getaddrinfo(
230: (struct sockaddr *)((struct if_msghdr *)next + 1),
231: ifm.ifm_addrs, info);
1.13 deraadt 232: sdl = (struct sockaddr_dl *)info[RTAX_IFP];
233:
234: if (sdl && sdl->sdl_family == AF_LINK &&
235: sdl->sdl_nlen > 0) {
236: struct ifreq ifrdesc;
237: char ifdescr[IFDESCRSIZE];
238: int s;
239:
240: bcopy(sdl->sdl_data, ifs->ifs_name,
241: sdl->sdl_nlen);
242: ifs->ifs_name[sdl->sdl_nlen] = '\0';
243:
244: /* Get the interface description */
245: memset(&ifrdesc, 0, sizeof(ifrdesc));
246: strlcpy(ifrdesc.ifr_name, ifs->ifs_name,
247: sizeof(ifrdesc.ifr_name));
248: ifrdesc.ifr_data = (caddr_t)&ifdescr;
249:
250: s = socket(AF_INET, SOCK_DGRAM, 0);
251: if (s != -1) {
252: if (ioctl(s, SIOCGIFDESCR, &ifrdesc) == 0)
253: strlcpy(ifs->ifs_description,
254: ifrdesc.ifr_data,
255: sizeof(ifs->ifs_description));
256: close(s);
1.3 dlg 257: }
1.1 markus 258: }
259: if (ifs->ifs_name[0] == '\0')
260: continue;
261: }
1.12 canacar 262: num_ifs++;
1.1 markus 263: UPDATE(ifc_ip, ifm_data.ifi_ipackets);
264: UPDATE(ifc_ib, ifm_data.ifi_ibytes);
265: UPDATE(ifc_ie, ifm_data.ifi_ierrors);
266: UPDATE(ifc_op, ifm_data.ifi_opackets);
267: UPDATE(ifc_ob, ifm_data.ifi_obytes);
268: UPDATE(ifc_oe, ifm_data.ifi_oerrors);
269: UPDATE(ifc_co, ifm_data.ifi_collisions);
1.7 claudio 270: ifs->ifs_cur.ifc_flags = ifm.ifm_flags;
271: ifs->ifs_cur.ifc_state = ifm.ifm_data.ifi_link_state;
1.17 canacar 272: ifs->ifs_flag++;
1.1 markus 273: }
1.17 canacar 274:
275: /* remove unreferenced interfaces */
276: for (i = 0; i < nifs; i++) {
277: ifs = &ifstats[i];
278: if (ifs->ifs_flag)
279: ifs->ifs_flag = 0;
280: else
281: ifs->ifs_name[0] = '\0';
282: }
283:
1.1 markus 284: free(buf);
285: }
286:
287:
1.12 canacar 288: static void
289: showifstat(struct ifstat *ifs)
1.1 markus 290: {
1.12 canacar 291: print_fld_str(FLD_IF_IFACE, ifs->ifs_name);
1.1 markus 292:
1.12 canacar 293: tb_start();
294: tbprintf("%s", ifs->ifs_cur.ifc_flags & IFF_UP ?
295: "up" : "dn");
1.7 claudio 296:
1.12 canacar 297: switch (ifs->ifs_cur.ifc_state) {
1.7 claudio 298: case LINK_STATE_UP:
1.8 reyk 299: case LINK_STATE_HALF_DUPLEX:
300: case LINK_STATE_FULL_DUPLEX:
1.12 canacar 301: tbprintf(":U");
302: break;
1.7 claudio 303: case LINK_STATE_DOWN:
1.12 canacar 304: tbprintf (":D");
305: break;
1.7 claudio 306: }
1.12 canacar 307:
308: print_fld_tb(FLD_IF_STATE);
1.13 deraadt 309:
310: print_fld_str(FLD_IF_DESC, ifs->ifs_description);
1.12 canacar 311:
312: print_fld_size(FLD_IF_IBYTES, ifs->ifs_cur.ifc_ib);
313: print_fld_size(FLD_IF_IPKTS, ifs->ifs_cur.ifc_ip);
314: print_fld_size(FLD_IF_IERRS, ifs->ifs_cur.ifc_ie);
315:
316: print_fld_size(FLD_IF_OBYTES, ifs->ifs_cur.ifc_ob);
317: print_fld_size(FLD_IF_OPKTS, ifs->ifs_cur.ifc_op);
318: print_fld_size(FLD_IF_OERRS, ifs->ifs_cur.ifc_oe);
319:
320: print_fld_size(FLD_IF_COLLS, ifs->ifs_cur.ifc_co);
321:
322: end_line();
1.7 claudio 323: }
1.6 deraadt 324:
1.12 canacar 325: static void
326: showtotal(void)
1.1 markus 327: {
1.12 canacar 328: print_fld_str(FLD_IF_IFACE, "Totals");
329:
330: print_fld_size(FLD_IF_IBYTES, sum.ifc_ib);
331: print_fld_size(FLD_IF_IPKTS, sum.ifc_ip);
332: print_fld_size(FLD_IF_IERRS, sum.ifc_ie);
333:
334: print_fld_size(FLD_IF_OBYTES, sum.ifc_ob);
335: print_fld_size(FLD_IF_OPKTS, sum.ifc_op);
336: print_fld_size(FLD_IF_OERRS, sum.ifc_oe);
337:
338: print_fld_size(FLD_IF_COLLS, sum.ifc_co);
339:
340: end_line();
1.1 markus 341:
342: }
343:
344: int
1.12 canacar 345: if_keyboard_callback(int ch)
1.1 markus 346: {
347: struct ifstat *ifs;
348:
1.12 canacar 349: switch (ch) {
350: case 'r':
351: for (ifs = ifstats; ifs < ifstats + nifs; ifs++)
352: ifs->ifs_old = ifs->ifs_now;
1.1 markus 353: state = RUN;
1.12 canacar 354: gotsig_alarm = 1;
355:
356: break;
357: case 'b':
1.1 markus 358: state = BOOT;
359: for (ifs = ifstats; ifs < ifstats + nifs; ifs++)
360: bzero(&ifs->ifs_old, sizeof(ifs->ifs_old));
1.12 canacar 361: gotsig_alarm = 1;
362: break;
363: case 't':
1.1 markus 364: state = TIME;
1.12 canacar 365: gotsig_alarm = 1;
366: break;
367: default:
368: return keyboard_callback(ch);
369: };
370:
371: return 1;
1.1 markus 372: }
1.12 canacar 373: