2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2003, Trent Nelson, <trent@arpa.com>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTIFSTAT_ERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
37 #include <net/if_mib.h>
51 #define C1 0 /* 0-19 */
52 #define C2 20 /* 20-39 */
53 #define C3 40 /* 40-59 */
54 #define C4 60 /* 60-80 */
55 #define C5 80 /* Used for label positioning. */
57 static const int col0 = 0;
58 static const int col1 = C1;
59 static const int col2 = C2;
60 static const int col3 = C3;
61 static const int col4 = C4;
62 static const int col5 = C5;
64 SLIST_HEAD(, if_stat) curlist;
65 SLIST_HEAD(, if_stat_disp) displist;
68 SLIST_ENTRY(if_stat) link;
69 char if_name[IF_NAMESIZE];
70 struct ifmibdata if_mib;
72 struct timeval tv_lastchanged;
73 uint64_t if_in_curtraffic;
74 uint64_t if_out_curtraffic;
75 uint64_t if_in_traffic_peak;
76 uint64_t if_out_traffic_peak;
77 uint64_t if_in_curpps;
78 uint64_t if_out_curpps;
79 uint64_t if_in_pps_peak;
80 uint64_t if_out_pps_peak;
81 u_int if_row; /* Index into ifmib sysctl */
82 int if_ypos; /* -1 if not being displayed */
88 extern char *matchline;
92 static int needclear = 0;
94 static void right_align_string(struct if_stat *);
95 static void getifmibdata(const int, struct ifmibdata *);
96 static void sort_interface_list(void);
97 static u_int getifnum(void);
99 #define IFSTAT_ERR(n, s) do { \
107 " Interface Traffic Peak Total"
109 #define STARTING_ROW (TOPLINE + 1)
110 #define ROW_SPACING (3)
112 #define IN_col2 (showpps ? ifp->if_in_curpps : ifp->if_in_curtraffic)
113 #define OUT_col2 (showpps ? ifp->if_out_curpps : ifp->if_out_curtraffic)
114 #define IN_col3 (showpps ? \
115 ifp->if_in_pps_peak : ifp->if_in_traffic_peak)
116 #define OUT_col3 (showpps ? \
117 ifp->if_out_pps_peak : ifp->if_out_traffic_peak)
118 #define IN_col4 (showpps ? \
119 ifp->if_mib.ifmd_data.ifi_ipackets : ifp->if_mib.ifmd_data.ifi_ibytes)
120 #define OUT_col4 (showpps ? \
121 ifp->if_mib.ifmd_data.ifi_opackets : ifp->if_mib.ifmd_data.ifi_obytes)
123 #define EMPTY_COLUMN " "
124 #define CLEAR_COLUMN(y, x) mvprintw((y), (x), "%20s", EMPTY_COLUMN);
126 #define DOPUTRATE(c, r, d) do { \
127 CLEAR_COLUMN(r, c); \
129 mvprintw(r, (c), "%10.3f %cp%s ", \
130 convert(d##_##c, curscale), \
131 *get_string(d##_##c, curscale), \
135 mvprintw(r, (c), "%10.3f %s%s ", \
136 convert(d##_##c, curscale), \
137 get_string(d##_##c, curscale), \
142 #define DOPUTTOTAL(c, r, d) do { \
143 CLEAR_COLUMN((r), (c)); \
145 mvprintw((r), (c), "%12.3f %cp ", \
146 convert(d##_##c, SC_AUTO), \
147 *get_string(d##_##c, SC_AUTO)); \
150 mvprintw((r), (c), "%12.3f %s ", \
151 convert(d##_##c, SC_AUTO), \
152 get_string(d##_##c, SC_AUTO)); \
156 #define PUTRATE(c, r) do { \
157 DOPUTRATE(c, (r), IN); \
158 DOPUTRATE(c, (r)+1, OUT); \
161 #define PUTTOTAL(c, r) do { \
162 DOPUTTOTAL(c, (r), IN); \
163 DOPUTTOTAL(c, (r)+1, OUT); \
166 #define PUTNAME(p) do { \
167 mvprintw(p->if_ypos, 0, "%s", p->if_name); \
168 mvprintw(p->if_ypos, col2-3, "%s", (const char *)"in"); \
169 mvprintw(p->if_ypos+1, col2-3, "%s", (const char *)"out"); \
175 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
179 closeifstat(WINDOW *w)
181 struct if_stat *node = NULL;
183 while (!SLIST_EMPTY(&curlist)) {
184 node = SLIST_FIRST(&curlist);
185 SLIST_REMOVE_HEAD(&curlist, link);
202 wmove(wnd, TOPLINE, 0);
204 mvprintw(TOPLINE, 0, "%s", TOPLABEL);
212 struct if_stat *ifp = NULL;
214 SLIST_FOREACH(ifp, &curlist, link) {
215 if (ifp->if_ypos < LINES - 3 && ifp->if_ypos != -1)
216 if (ifp->display == 0 || ifp->match == 0) {
217 wmove(wnd, ifp->if_ypos, 0);
219 wmove(wnd, ifp->if_ypos + 1, 0);
224 PUTRATE(col2, ifp->if_ypos);
225 PUTRATE(col3, ifp->if_ypos);
226 PUTTOTAL(col4, ifp->if_ypos);
236 struct if_stat *p = NULL;
243 SLIST_INIT(&curlist);
245 for (i = 0; i < n; i++) {
246 p = (struct if_stat *)calloc(1, sizeof(struct if_stat));
248 IFSTAT_ERR(1, "out of memory");
249 SLIST_INSERT_HEAD(&curlist, p, link);
251 getifmibdata(p->if_row, &p->if_mib);
252 right_align_string(p);
256 * Initially, we only display interfaces that have
257 * received some traffic.
259 if (p->if_mib.ifmd_data.ifi_ibytes != 0)
263 sort_interface_list();
271 struct if_stat *ifp = NULL;
272 struct timeval tv, new_tv, old_tv;
273 double elapsed = 0.0;
274 uint64_t new_inb, new_outb, old_inb, old_outb = 0;
275 uint64_t new_inp, new_outp, old_inp, old_outp = 0;
277 SLIST_FOREACH(ifp, &curlist, link) {
279 * Grab a copy of the old input/output values before we
280 * call getifmibdata().
282 old_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
283 old_outb = ifp->if_mib.ifmd_data.ifi_obytes;
284 old_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
285 old_outp = ifp->if_mib.ifmd_data.ifi_opackets;
286 ifp->tv_lastchanged = ifp->if_mib.ifmd_data.ifi_lastchange;
288 (void)gettimeofday(&new_tv, NULL);
289 (void)getifmibdata(ifp->if_row, &ifp->if_mib);
291 new_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
292 new_outb = ifp->if_mib.ifmd_data.ifi_obytes;
293 new_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
294 new_outp = ifp->if_mib.ifmd_data.ifi_opackets;
296 /* Display interface if it's received some traffic. */
297 if (new_inb > 0 && old_inb == 0) {
303 * The rest is pretty trivial. Calculate the new values
304 * for our current traffic rates, and while we're there,
305 * see if we have new peak rates.
308 timersub(&new_tv, &old_tv, &tv);
309 elapsed = tv.tv_sec + (tv.tv_usec * 1e-6);
311 ifp->if_in_curtraffic = new_inb - old_inb;
312 ifp->if_out_curtraffic = new_outb - old_outb;
314 ifp->if_in_curpps = new_inp - old_inp;
315 ifp->if_out_curpps = new_outp - old_outp;
318 * Rather than divide by the time specified on the comm-
319 * and line, we divide by ``elapsed'' as this is likely
320 * to be more accurate.
322 ifp->if_in_curtraffic /= elapsed;
323 ifp->if_out_curtraffic /= elapsed;
324 ifp->if_in_curpps /= elapsed;
325 ifp->if_out_curpps /= elapsed;
327 if (ifp->if_in_curtraffic > ifp->if_in_traffic_peak)
328 ifp->if_in_traffic_peak = ifp->if_in_curtraffic;
330 if (ifp->if_out_curtraffic > ifp->if_out_traffic_peak)
331 ifp->if_out_traffic_peak = ifp->if_out_curtraffic;
333 if (ifp->if_in_curpps > ifp->if_in_pps_peak)
334 ifp->if_in_pps_peak = ifp->if_in_curpps;
336 if (ifp->if_out_curpps > ifp->if_out_pps_peak)
337 ifp->if_out_pps_peak = ifp->if_out_curpps;
339 ifp->tv.tv_sec = new_tv.tv_sec;
340 ifp->tv.tv_usec = new_tv.tv_usec;
345 sort_interface_list();
351 * We want to right justify our interface names against the first column
352 * (first sixteen or so characters), so we need to do some alignment.
355 right_align_string(struct if_stat *ifp)
357 int str_len = 0, pad_len = 0;
358 char *newstr = NULL, *ptr = NULL;
360 if (ifp == NULL || ifp->if_mib.ifmd_name == NULL)
363 /* string length + '\0' */
364 str_len = strlen(ifp->if_mib.ifmd_name)+1;
365 pad_len = IF_NAMESIZE-(str_len);
367 newstr = ifp->if_name;
368 ptr = newstr + pad_len;
369 (void)memset((void *)newstr, (int)' ', IF_NAMESIZE);
370 (void)strncpy(ptr, (const char *)&ifp->if_mib.ifmd_name,
378 check_match(const char *ifname)
380 char *p = matchline, *c, t;
383 if (matchline == NULL)
386 /* Strip leading whitespaces */
391 while ((mlen = strcspn(c, " ;,")) != 0) {
396 if (fnmatch(c, ifname, FNM_CASEFOLD) == 0) {
401 c = p + strspn(p, " ;,");
404 c = p + strspn(p, " ;,");
412 * This function iterates through our list of interfaces, identifying
413 * those that are to be displayed (ifp->display = 1). For each interf-
414 * rface that we're displaying, we generate an appropriate position for
415 * it on the screen (ifp->if_ypos).
417 * This function is called any time a change is made to an interface's
421 sort_interface_list(void)
423 struct if_stat *ifp = NULL;
427 SLIST_FOREACH(ifp, &curlist, link) {
428 if (matchline && !check_match(ifp->if_mib.ifmd_name))
432 if (ifp->display && ifp->match) {
450 static int name[] = { CTL_NET,
456 datalen = sizeof(data);
457 if (sysctl(name, 5, (void *)&data, (size_t *)&datalen, (void *)NULL,
459 IFSTAT_ERR(1, "sysctl error");
464 getifmibdata(int row, struct ifmibdata *data)
467 static int name[] = { CTL_NET,
473 datalen = sizeof(*data);
476 if ((sysctl(name, 6, (void *)data, (size_t *)&datalen, (void *)NULL,
477 (size_t)0) != 0) && (errno != ENOENT))
478 IFSTAT_ERR(2, "sysctl error getting interface data");
482 cmdifstat(const char *cmd, const char *args)
486 retval = ifcmd(cmd, args);
487 /* ifcmd() returns 1 on success */