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/queue.h>
36 #include <sys/sysctl.h>
38 #include <net/if_mib.h>
53 #define C1 0 /* 0-19 */
54 #define C2 20 /* 20-39 */
55 #define C3 40 /* 40-59 */
56 #define C4 60 /* 60-80 */
57 #define C5 80 /* Used for label positioning. */
59 static const int col0 = 0;
60 static const int col1 = C1;
61 static const int col2 = C2;
62 static const int col3 = C3;
63 static const int col4 = C4;
64 static const int col5 = C5;
66 SLIST_HEAD(, if_stat) curlist;
67 SLIST_HEAD(, if_stat_disp) displist;
70 SLIST_ENTRY(if_stat) link;
71 char display_name[IF_NAMESIZE];
72 char dev_name[IFNAMSIZ]; /* copied from ifmibdata */
73 struct ifmibdata if_mib;
75 struct timeval tv_lastchanged;
76 uint64_t if_in_curtraffic;
77 uint64_t if_out_curtraffic;
78 uint64_t if_in_traffic_peak;
79 uint64_t if_out_traffic_peak;
80 uint64_t if_in_curpps;
81 uint64_t if_out_curpps;
82 uint64_t if_in_pps_peak;
83 uint64_t if_out_pps_peak;
84 u_int if_row; /* Index into ifmib sysctl */
85 int if_ypos; /* -1 if not being displayed */
91 extern char *matchline;
95 static int needclear = 0;
96 static bool displayall = false;
98 static void format_device_name(struct if_stat *);
99 static int getifmibdata(const int, struct ifmibdata *);
100 static void sort_interface_list(void);
101 static u_int getifnum(void);
102 static void clearifstat(void);
104 #define IFSTAT_ERR(n, s) do { \
112 " Interface Traffic Peak Total"
114 #define STARTING_ROW (TOPLINE + 1)
115 #define ROW_SPACING (3)
117 #define IN_col2 (showpps ? ifp->if_in_curpps : ifp->if_in_curtraffic)
118 #define OUT_col2 (showpps ? ifp->if_out_curpps : ifp->if_out_curtraffic)
119 #define IN_col3 (showpps ? \
120 ifp->if_in_pps_peak : ifp->if_in_traffic_peak)
121 #define OUT_col3 (showpps ? \
122 ifp->if_out_pps_peak : ifp->if_out_traffic_peak)
123 #define IN_col4 (showpps ? \
124 ifp->if_mib.ifmd_data.ifi_ipackets : ifp->if_mib.ifmd_data.ifi_ibytes)
125 #define OUT_col4 (showpps ? \
126 ifp->if_mib.ifmd_data.ifi_opackets : ifp->if_mib.ifmd_data.ifi_obytes)
128 #define EMPTY_COLUMN " "
129 #define CLEAR_COLUMN(y, x) mvprintw((y), (x), "%20s", EMPTY_COLUMN);
131 #define DOPUTRATE(c, r, d) do { \
132 CLEAR_COLUMN(r, c); \
134 mvprintw(r, (c), "%10.3f %cp%s ", \
135 convert(d##_##c, curscale), \
136 *get_string(d##_##c, curscale), \
140 mvprintw(r, (c), "%10.3f %s%s ", \
141 convert(d##_##c, curscale), \
142 get_string(d##_##c, curscale), \
147 #define DOPUTTOTAL(c, r, d) do { \
148 CLEAR_COLUMN((r), (c)); \
150 mvprintw((r), (c), "%12.3f %cp ", \
151 convert(d##_##c, SC_AUTO), \
152 *get_string(d##_##c, SC_AUTO)); \
155 mvprintw((r), (c), "%12.3f %s ", \
156 convert(d##_##c, SC_AUTO), \
157 get_string(d##_##c, SC_AUTO)); \
161 #define PUTRATE(c, r) do { \
162 DOPUTRATE(c, (r), IN); \
163 DOPUTRATE(c, (r)+1, OUT); \
166 #define PUTTOTAL(c, r) do { \
167 DOPUTTOTAL(c, (r), IN); \
168 DOPUTTOTAL(c, (r)+1, OUT); \
171 #define PUTNAME(p) do { \
172 mvprintw(p->if_ypos, 0, "%s", p->display_name); \
173 mvprintw(p->if_ypos, col2-3, "%s", (const char *)"in"); \
174 mvprintw(p->if_ypos+1, col2-3, "%s", (const char *)"out"); \
180 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
184 closeifstat(WINDOW *w)
186 struct if_stat *node = NULL;
188 while (!SLIST_EMPTY(&curlist)) {
189 node = SLIST_FIRST(&curlist);
190 SLIST_REMOVE_HEAD(&curlist, link);
207 wmove(wnd, TOPLINE, 0);
209 mvprintw(TOPLINE, 0, "%s", TOPLABEL);
217 struct if_stat *ifp = NULL;
219 SLIST_FOREACH(ifp, &curlist, link) {
220 if (ifp->if_ypos < LINES - 3 && ifp->if_ypos != -1)
221 if (!ifp->display || ifp->match == 0) {
222 wmove(wnd, ifp->if_ypos, 0);
224 wmove(wnd, ifp->if_ypos + 1, 0);
229 PUTRATE(col2, ifp->if_ypos);
230 PUTRATE(col3, ifp->if_ypos);
231 PUTTOTAL(col4, ifp->if_ypos);
241 struct if_stat *p = NULL;
248 SLIST_INIT(&curlist);
250 for (i = 0; i < n; i++) {
251 p = (struct if_stat *)calloc(1, sizeof(struct if_stat));
253 IFSTAT_ERR(1, "out of memory");
255 if (getifmibdata(p->if_row, &p->if_mib) == -1) {
259 SLIST_INSERT_HEAD(&curlist, p, link);
260 format_device_name(p);
264 * Initially, we only display interfaces that have
265 * received some traffic unless display-all is on.
267 if (displayall || p->if_mib.ifmd_data.ifi_ibytes != 0)
271 sort_interface_list();
279 struct if_stat *ifp = NULL, *temp_var;
280 struct timeval tv, new_tv, old_tv;
281 double elapsed = 0.0;
282 uint64_t new_inb, new_outb, old_inb, old_outb = 0;
283 uint64_t new_inp, new_outp, old_inp, old_outp = 0;
285 SLIST_FOREACH_SAFE(ifp, &curlist, link, temp_var) {
287 * Grab a copy of the old input/output values before we
288 * call getifmibdata().
290 old_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
291 old_outb = ifp->if_mib.ifmd_data.ifi_obytes;
292 old_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
293 old_outp = ifp->if_mib.ifmd_data.ifi_opackets;
294 ifp->tv_lastchanged = ifp->if_mib.ifmd_data.ifi_lastchange;
296 (void)gettimeofday(&new_tv, NULL);
297 if (getifmibdata(ifp->if_row, &ifp->if_mib) == -1 ) {
298 /* if a device was removed */
299 SLIST_REMOVE(&curlist, ifp, if_stat, link);
303 } else if (strcmp(ifp->dev_name, ifp->if_mib.ifmd_name) != 0 ) {
304 /* a device was removed and another one was added */
305 format_device_name(ifp);
306 /* clear to the current value for the new device */
307 old_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
308 old_outb = ifp->if_mib.ifmd_data.ifi_obytes;
309 old_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
310 old_outp = ifp->if_mib.ifmd_data.ifi_opackets;
314 new_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
315 new_outb = ifp->if_mib.ifmd_data.ifi_obytes;
316 new_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
317 new_outp = ifp->if_mib.ifmd_data.ifi_opackets;
319 /* Display interface if it's received some traffic. */
320 if (!ifp->display && new_inb > 0 && old_inb == 0) {
326 * The rest is pretty trivial. Calculate the new values
327 * for our current traffic rates, and while we're there,
328 * see if we have new peak rates.
331 timersub(&new_tv, &old_tv, &tv);
332 elapsed = tv.tv_sec + (tv.tv_usec * 1e-6);
334 ifp->if_in_curtraffic = new_inb - old_inb;
335 ifp->if_out_curtraffic = new_outb - old_outb;
337 ifp->if_in_curpps = new_inp - old_inp;
338 ifp->if_out_curpps = new_outp - old_outp;
341 * Rather than divide by the time specified on the comm-
342 * and line, we divide by ``elapsed'' as this is likely
343 * to be more accurate.
345 ifp->if_in_curtraffic /= elapsed;
346 ifp->if_out_curtraffic /= elapsed;
347 ifp->if_in_curpps /= elapsed;
348 ifp->if_out_curpps /= elapsed;
350 if (ifp->if_in_curtraffic > ifp->if_in_traffic_peak)
351 ifp->if_in_traffic_peak = ifp->if_in_curtraffic;
353 if (ifp->if_out_curtraffic > ifp->if_out_traffic_peak)
354 ifp->if_out_traffic_peak = ifp->if_out_curtraffic;
356 if (ifp->if_in_curpps > ifp->if_in_pps_peak)
357 ifp->if_in_pps_peak = ifp->if_in_curpps;
359 if (ifp->if_out_curpps > ifp->if_out_pps_peak)
360 ifp->if_out_pps_peak = ifp->if_out_curpps;
362 ifp->tv.tv_sec = new_tv.tv_sec;
363 ifp->tv.tv_usec = new_tv.tv_usec;
368 sort_interface_list();
374 * We want to right justify our interface names against the first column
375 * (first sixteen or so characters), so we need to do some alignment.
376 * We save original name so that we can find a same spot is take by a
380 format_device_name(struct if_stat *ifp)
384 snprintf(ifp->display_name, IF_NAMESIZE, "%*s", IF_NAMESIZE-1,
385 ifp->if_mib.ifmd_name);
386 strcpy(ifp->dev_name, ifp->if_mib.ifmd_name);
391 check_match(const char *ifname)
393 char *p = matchline, *c, t;
396 if (matchline == NULL)
399 /* Strip leading whitespaces */
404 while ((mlen = strcspn(c, " ;,")) != 0) {
409 if (fnmatch(c, ifname, FNM_CASEFOLD) == 0) {
414 c = p + strspn(p, " ;,");
417 c = p + strspn(p, " ;,");
425 * This function iterates through our list of interfaces, identifying
426 * those that are to be displayed (ifp->display = 1). For each interf-
427 * rface that we're displaying, we generate an appropriate position for
428 * it on the screen (ifp->if_ypos).
430 * This function is called any time a change is made to an interface's
434 sort_interface_list(void)
436 struct if_stat *ifp = NULL;
440 SLIST_FOREACH(ifp, &curlist, link) {
441 if (matchline && !check_match(ifp->if_mib.ifmd_name))
445 if (ifp->display && ifp->match) {
463 static int name[] = { CTL_NET,
469 datalen = sizeof(data);
470 if (sysctl(name, 5, (void *)&data, (size_t *)&datalen, (void *)NULL,
472 IFSTAT_ERR(1, "sysctl error");
477 getifmibdata(int row, struct ifmibdata *data)
481 static int name[] = { CTL_NET,
487 datalen = sizeof(*data);
490 ret = sysctl(name, 6, (void *)data, (size_t *)&datalen, (void *)NULL,
492 if ((ret != 0) && (errno != ENOENT))
493 IFSTAT_ERR(2, "sysctl error getting interface data");
499 cmdifstat(const char *cmd, const char *args)
503 retval = ifcmd(cmd, args);
504 /* ifcmd() returns 1 on success */
509 else if (prefix(cmd, "all")) {