Annotation of src/usr.bin/systat/if.c, Revision 1.18
1.18 ! lum 1: /* $OpenBSD: if.c,v 1.17 2009/11/23 01:51:41 canacar 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:
74: #define FIELD_ADDR(x) (&fields_if[x])
75:
76: #define FLD_IF_IFACE FIELD_ADDR(0)
77: #define FLD_IF_STATE FIELD_ADDR(1)
78: #define FLD_IF_IPKTS FIELD_ADDR(2)
79: #define FLD_IF_IBYTES FIELD_ADDR(3)
80: #define FLD_IF_IERRS FIELD_ADDR(4)
81: #define FLD_IF_OPKTS FIELD_ADDR(5)
82: #define FLD_IF_OBYTES FIELD_ADDR(6)
83: #define FLD_IF_OERRS FIELD_ADDR(7)
84: #define FLD_IF_COLLS FIELD_ADDR(8)
1.13 deraadt 85: #define FLD_IF_DESC FIELD_ADDR(9)
1.12 canacar 86:
87:
88: /* Define views */
89: field_def *view_if_0[] = {
1.13 deraadt 90: FLD_IF_IFACE, FLD_IF_STATE, FLD_IF_DESC, FLD_IF_IPKTS,
91: FLD_IF_IBYTES, FLD_IF_IERRS, FLD_IF_OPKTS, FLD_IF_OBYTES,
92: FLD_IF_OERRS, FLD_IF_COLLS, NULL
1.12 canacar 93: };
94:
95: /* Define view managers */
96:
97: struct view_manager ifstat_mgr = {
98: "Ifstat", select_if, read_if, NULL, print_header,
99: print_if, if_keyboard_callback, NULL, NULL
100: };
101:
102: field_view views_if[] = {
103: {view_if_0, "ifstat", '1', &ifstat_mgr},
104: {NULL, NULL, 0, NULL}
105: };
1.7 claudio 106:
1.1 markus 107:
108: int
109: initifstat(void)
110: {
1.12 canacar 111: field_view *v;
112: read_if();
113: for (v = views_if; v->name != NULL; v++)
114: add_view(v);
1.1 markus 115:
116: return(1);
117: }
118:
119: #define UPDATE(x, y) do { \
1.2 deraadt 120: ifs->ifs_now.x = ifm.y; \
1.1 markus 121: ifs->ifs_cur.x = ifs->ifs_now.x - ifs->ifs_old.x; \
1.4 dlg 122: if (state == TIME) {\
123: ifs->ifs_old.x = ifs->ifs_now.x; \
124: ifs->ifs_cur.x /= naptime; \
125: } \
1.1 markus 126: sum.x += ifs->ifs_cur.x; \
127: } while(0)
128:
129:
130: void
131: rt_getaddrinfo(struct sockaddr *sa, int addrs, struct sockaddr **info)
132: {
133: int i;
134:
135: for (i = 0; i < RTAX_MAX; i++) {
136: if (addrs & (1 << i)) {
137: info[i] = sa;
138: sa = (struct sockaddr *) ((char *)(sa) +
139: roundup(sa->sa_len, sizeof(long)));
140: } else
141: info[i] = NULL;
142: }
143: }
144:
1.12 canacar 145:
146:
147: int
148: select_if(void)
149: {
150: num_disp = num_ifs + 1;
151: return (0);
152: }
153:
154: int
155: read_if(void)
156: {
157: fetchifstat();
158: num_disp = num_ifs + 1;
159:
160: return 0;
161: }
162:
1.1 markus 163: void
1.12 canacar 164: print_if(void)
165: {
166: int n, i, count = 0;
167:
168: for (n = 0, i = 0; n < nifs; n++) {
169: if (ifstats[n].ifs_name[0] == '\0')
170: continue;
171: if (i++ < dispstart)
172: continue;
173: if (i == num_disp)
174: break;
175: showifstat(ifstats + n);
176: if (maxprint > 0 && ++count >= maxprint)
177: return;
178: }
179: showtotal();
180: }
181:
182:
1.16 deraadt 183: void
1.1 markus 184: fetchifstat(void)
185: {
186: struct ifstat *newstats, *ifs;
1.2 deraadt 187: struct if_msghdr ifm;
1.1 markus 188: struct sockaddr *info[RTAX_MAX];
189: struct sockaddr_dl *sdl;
190: char *buf, *next, *lim;
1.17 canacar 191: int mib[6], i;
1.1 markus 192: size_t need;
193:
194: mib[0] = CTL_NET;
195: mib[1] = AF_ROUTE;
196: mib[2] = 0;
197: mib[3] = 0;
198: mib[4] = NET_RT_IFLIST;
199: mib[5] = 0;
200:
201: if (sysctl(mib, 6, NULL, &need, NULL, 0) == -1)
202: return;
203: if ((buf = malloc(need)) == NULL)
204: return;
205: if (sysctl(mib, 6, buf, &need, NULL, 0) == -1) {
206: free(buf);
207: return;
208: }
209:
210: bzero(&sum, sizeof(sum));
1.12 canacar 211: num_ifs = 0;
1.1 markus 212:
213: lim = buf + need;
1.2 deraadt 214: for (next = buf; next < lim; next += ifm.ifm_msglen) {
215: bcopy(next, &ifm, sizeof ifm);
1.14 claudio 216: if (ifm.ifm_version != RTM_VERSION ||
217: ifm.ifm_type != RTM_IFINFO ||
218: !(ifm.ifm_addrs & RTA_IFP))
1.1 markus 219: continue;
1.2 deraadt 220: if (ifm.ifm_index >= nifs) {
1.12 canacar 221: if ((newstats = realloc(ifstats, (ifm.ifm_index + 4)
222: * sizeof(struct ifstat))) == NULL)
1.1 markus 223: continue;
224: ifstats = newstats;
1.2 deraadt 225: for (; nifs < ifm.ifm_index + 4; nifs++)
1.12 canacar 226: bzero(&ifstats[nifs], sizeof(*ifstats));
1.1 markus 227: }
1.2 deraadt 228: ifs = &ifstats[ifm.ifm_index];
1.1 markus 229: if (ifs->ifs_name[0] == '\0') {
230: bzero(&info, sizeof(info));
1.2 deraadt 231: rt_getaddrinfo(
232: (struct sockaddr *)((struct if_msghdr *)next + 1),
233: ifm.ifm_addrs, info);
1.13 deraadt 234: sdl = (struct sockaddr_dl *)info[RTAX_IFP];
235:
236: if (sdl && sdl->sdl_family == AF_LINK &&
237: sdl->sdl_nlen > 0) {
238: struct ifreq ifrdesc;
239: char ifdescr[IFDESCRSIZE];
240: int s;
241:
242: bcopy(sdl->sdl_data, ifs->ifs_name,
243: sdl->sdl_nlen);
244: ifs->ifs_name[sdl->sdl_nlen] = '\0';
245:
246: /* Get the interface description */
247: memset(&ifrdesc, 0, sizeof(ifrdesc));
248: strlcpy(ifrdesc.ifr_name, ifs->ifs_name,
249: sizeof(ifrdesc.ifr_name));
250: ifrdesc.ifr_data = (caddr_t)&ifdescr;
251:
252: s = socket(AF_INET, SOCK_DGRAM, 0);
253: if (s != -1) {
254: if (ioctl(s, SIOCGIFDESCR, &ifrdesc) == 0)
255: strlcpy(ifs->ifs_description,
256: ifrdesc.ifr_data,
257: sizeof(ifs->ifs_description));
258: close(s);
1.3 dlg 259: }
1.1 markus 260: }
261: if (ifs->ifs_name[0] == '\0')
262: continue;
263: }
1.12 canacar 264: num_ifs++;
1.1 markus 265: UPDATE(ifc_ip, ifm_data.ifi_ipackets);
266: UPDATE(ifc_ib, ifm_data.ifi_ibytes);
267: UPDATE(ifc_ie, ifm_data.ifi_ierrors);
268: UPDATE(ifc_op, ifm_data.ifi_opackets);
269: UPDATE(ifc_ob, ifm_data.ifi_obytes);
270: UPDATE(ifc_oe, ifm_data.ifi_oerrors);
271: UPDATE(ifc_co, ifm_data.ifi_collisions);
1.7 claudio 272: ifs->ifs_cur.ifc_flags = ifm.ifm_flags;
273: ifs->ifs_cur.ifc_state = ifm.ifm_data.ifi_link_state;
1.17 canacar 274: ifs->ifs_flag++;
1.1 markus 275: }
1.17 canacar 276:
277: /* remove unreferenced interfaces */
278: for (i = 0; i < nifs; i++) {
279: ifs = &ifstats[i];
280: if (ifs->ifs_flag)
281: ifs->ifs_flag = 0;
282: else
283: ifs->ifs_name[0] = '\0';
284: }
285:
1.1 markus 286: free(buf);
287: }
288:
289:
1.12 canacar 290: static void
291: showifstat(struct ifstat *ifs)
1.1 markus 292: {
1.12 canacar 293: print_fld_str(FLD_IF_IFACE, ifs->ifs_name);
1.1 markus 294:
1.12 canacar 295: tb_start();
296: tbprintf("%s", ifs->ifs_cur.ifc_flags & IFF_UP ?
297: "up" : "dn");
1.7 claudio 298:
1.12 canacar 299: switch (ifs->ifs_cur.ifc_state) {
1.7 claudio 300: case LINK_STATE_UP:
1.8 reyk 301: case LINK_STATE_HALF_DUPLEX:
302: case LINK_STATE_FULL_DUPLEX:
1.12 canacar 303: tbprintf(":U");
304: break;
1.7 claudio 305: case LINK_STATE_DOWN:
1.12 canacar 306: tbprintf (":D");
307: break;
1.7 claudio 308: }
1.12 canacar 309:
310: print_fld_tb(FLD_IF_STATE);
1.13 deraadt 311:
312: print_fld_str(FLD_IF_DESC, ifs->ifs_description);
1.12 canacar 313:
314: print_fld_size(FLD_IF_IBYTES, ifs->ifs_cur.ifc_ib);
315: print_fld_size(FLD_IF_IPKTS, ifs->ifs_cur.ifc_ip);
316: print_fld_size(FLD_IF_IERRS, ifs->ifs_cur.ifc_ie);
317:
318: print_fld_size(FLD_IF_OBYTES, ifs->ifs_cur.ifc_ob);
319: print_fld_size(FLD_IF_OPKTS, ifs->ifs_cur.ifc_op);
320: print_fld_size(FLD_IF_OERRS, ifs->ifs_cur.ifc_oe);
321:
322: print_fld_size(FLD_IF_COLLS, ifs->ifs_cur.ifc_co);
323:
324: end_line();
1.7 claudio 325: }
1.6 deraadt 326:
1.12 canacar 327: static void
328: showtotal(void)
1.1 markus 329: {
1.12 canacar 330: print_fld_str(FLD_IF_IFACE, "Totals");
331:
332: print_fld_size(FLD_IF_IBYTES, sum.ifc_ib);
333: print_fld_size(FLD_IF_IPKTS, sum.ifc_ip);
334: print_fld_size(FLD_IF_IERRS, sum.ifc_ie);
335:
336: print_fld_size(FLD_IF_OBYTES, sum.ifc_ob);
337: print_fld_size(FLD_IF_OPKTS, sum.ifc_op);
338: print_fld_size(FLD_IF_OERRS, sum.ifc_oe);
339:
340: print_fld_size(FLD_IF_COLLS, sum.ifc_co);
341:
342: end_line();
1.1 markus 343:
344: }
345:
346: int
1.12 canacar 347: if_keyboard_callback(int ch)
1.1 markus 348: {
349: struct ifstat *ifs;
350:
1.12 canacar 351: switch (ch) {
352: case 'r':
353: for (ifs = ifstats; ifs < ifstats + nifs; ifs++)
354: ifs->ifs_old = ifs->ifs_now;
1.1 markus 355: state = RUN;
1.12 canacar 356: gotsig_alarm = 1;
357:
358: break;
359: case 'b':
1.1 markus 360: state = BOOT;
361: for (ifs = ifstats; ifs < ifstats + nifs; ifs++)
362: bzero(&ifs->ifs_old, sizeof(ifs->ifs_old));
1.12 canacar 363: gotsig_alarm = 1;
364: break;
365: case 't':
1.1 markus 366: state = TIME;
1.12 canacar 367: gotsig_alarm = 1;
368: break;
369: default:
370: return keyboard_callback(ch);
371: };
372:
373: return 1;
1.1 markus 374: }
1.12 canacar 375: