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
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/queue.h>
34 #include <sys/sysctl.h>
36 #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 display_name[IF_NAMESIZE];
70 char dev_name[IFNAMSIZ]; /* copied from ifmibdata */
71 struct ifmibdata if_mib;
73 struct timeval tv_lastchanged;
74 uint64_t if_in_curtraffic;
75 uint64_t if_out_curtraffic;
76 uint64_t if_in_traffic_peak;
77 uint64_t if_out_traffic_peak;
78 uint64_t if_in_curpps;
79 uint64_t if_out_curpps;
80 uint64_t if_in_pps_peak;
81 uint64_t if_out_pps_peak;
82 u_int if_row; /* Index into ifmib sysctl */
83 int if_ypos; /* -1 if not being displayed */
89 extern char *matchline;
93 static int needclear = 0;
94 static bool displayall = false;
96 static void format_device_name(struct if_stat *);
97 static int getifmibdata(const int, struct ifmibdata *);
98 static void sort_interface_list(void);
99 static u_int getifnum(void);
100 static void clearifstat(void);
102 #define IFSTAT_ERR(n, s) do { \
110 " Interface Traffic Peak Total"
112 #define STARTING_ROW (TOPLINE + 1)
113 #define ROW_SPACING (3)
115 #define IN_col2 (showpps ? ifp->if_in_curpps : ifp->if_in_curtraffic)
116 #define OUT_col2 (showpps ? ifp->if_out_curpps : ifp->if_out_curtraffic)
117 #define IN_col3 (showpps ? \
118 ifp->if_in_pps_peak : ifp->if_in_traffic_peak)
119 #define OUT_col3 (showpps ? \
120 ifp->if_out_pps_peak : ifp->if_out_traffic_peak)
121 #define IN_col4 (showpps ? \
122 ifp->if_mib.ifmd_data.ifi_ipackets : ifp->if_mib.ifmd_data.ifi_ibytes)
123 #define OUT_col4 (showpps ? \
124 ifp->if_mib.ifmd_data.ifi_opackets : ifp->if_mib.ifmd_data.ifi_obytes)
126 #define EMPTY_COLUMN " "
127 #define CLEAR_COLUMN(y, x) mvprintw((y), (x), "%20s", EMPTY_COLUMN);
129 #define DOPUTRATE(c, r, d) do { \
130 CLEAR_COLUMN(r, c); \
132 mvprintw(r, (c), "%10.3f %cp%s ", \
133 convert(d##_##c, curscale), \
134 *get_string(d##_##c, curscale), \
138 mvprintw(r, (c), "%10.3f %s%s ", \
139 convert(d##_##c, curscale), \
140 get_string(d##_##c, curscale), \
145 #define DOPUTTOTAL(c, r, d) do { \
146 CLEAR_COLUMN((r), (c)); \
148 mvprintw((r), (c), "%12.3f %cp ", \
149 convert(d##_##c, SC_AUTO), \
150 *get_string(d##_##c, SC_AUTO)); \
153 mvprintw((r), (c), "%12.3f %s ", \
154 convert(d##_##c, SC_AUTO), \
155 get_string(d##_##c, SC_AUTO)); \
159 #define PUTRATE(c, r) do { \
160 DOPUTRATE(c, (r), IN); \
161 DOPUTRATE(c, (r)+1, OUT); \
164 #define PUTTOTAL(c, r) do { \
165 DOPUTTOTAL(c, (r), IN); \
166 DOPUTTOTAL(c, (r)+1, OUT); \
169 #define PUTNAME(p) do { \
170 mvprintw(p->if_ypos, 0, "%s", p->display_name); \
171 mvprintw(p->if_ypos, col2-3, "%s", (const char *)"in"); \
172 mvprintw(p->if_ypos+1, col2-3, "%s", (const char *)"out"); \
178 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
182 closeifstat(WINDOW *w)
184 struct if_stat *node = NULL;
186 while (!SLIST_EMPTY(&curlist)) {
187 node = SLIST_FIRST(&curlist);
188 SLIST_REMOVE_HEAD(&curlist, link);
205 wmove(wnd, TOPLINE, 0);
207 mvprintw(TOPLINE, 0, "%s", TOPLABEL);
215 struct if_stat *ifp = NULL;
217 SLIST_FOREACH(ifp, &curlist, link) {
218 if (ifp->if_ypos < LINES - 3 && ifp->if_ypos != -1)
219 if (!ifp->display || ifp->match == 0) {
220 wmove(wnd, ifp->if_ypos, 0);
222 wmove(wnd, ifp->if_ypos + 1, 0);
227 PUTRATE(col2, ifp->if_ypos);
228 PUTRATE(col3, ifp->if_ypos);
229 PUTTOTAL(col4, ifp->if_ypos);
239 struct if_stat *p = NULL;
246 SLIST_INIT(&curlist);
248 for (i = 0; i < n; i++) {
249 p = (struct if_stat *)calloc(1, sizeof(struct if_stat));
251 IFSTAT_ERR(1, "out of memory");
253 if (getifmibdata(p->if_row, &p->if_mib) == -1) {
257 SLIST_INSERT_HEAD(&curlist, p, link);
258 format_device_name(p);
262 * Initially, we only display interfaces that have
263 * received some traffic unless display-all is on.
265 if (displayall || p->if_mib.ifmd_data.ifi_ibytes != 0)
269 sort_interface_list();
277 struct if_stat *ifp = NULL, *temp_var;
278 struct timeval tv, new_tv, old_tv;
279 double elapsed = 0.0;
280 uint64_t new_inb, new_outb, old_inb, old_outb = 0;
281 uint64_t new_inp, new_outp, old_inp, old_outp = 0;
283 SLIST_FOREACH_SAFE(ifp, &curlist, link, temp_var) {
285 * Grab a copy of the old input/output values before we
286 * call getifmibdata().
288 old_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
289 old_outb = ifp->if_mib.ifmd_data.ifi_obytes;
290 old_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
291 old_outp = ifp->if_mib.ifmd_data.ifi_opackets;
292 ifp->tv_lastchanged = ifp->if_mib.ifmd_data.ifi_lastchange;
294 (void)gettimeofday(&new_tv, NULL);
295 if (getifmibdata(ifp->if_row, &ifp->if_mib) == -1 ) {
296 /* if a device was removed */
297 SLIST_REMOVE(&curlist, ifp, if_stat, link);
301 } else if (strcmp(ifp->dev_name, ifp->if_mib.ifmd_name) != 0 ) {
302 /* a device was removed and another one was added */
303 format_device_name(ifp);
304 /* clear to the current value for the new device */
305 old_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
306 old_outb = ifp->if_mib.ifmd_data.ifi_obytes;
307 old_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
308 old_outp = ifp->if_mib.ifmd_data.ifi_opackets;
312 new_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
313 new_outb = ifp->if_mib.ifmd_data.ifi_obytes;
314 new_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
315 new_outp = ifp->if_mib.ifmd_data.ifi_opackets;
317 /* Display interface if it's received some traffic. */
318 if (!ifp->display && new_inb > 0 && old_inb == 0) {
324 * The rest is pretty trivial. Calculate the new values
325 * for our current traffic rates, and while we're there,
326 * see if we have new peak rates.
329 timersub(&new_tv, &old_tv, &tv);
330 elapsed = tv.tv_sec + (tv.tv_usec * 1e-6);
332 ifp->if_in_curtraffic = new_inb - old_inb;
333 ifp->if_out_curtraffic = new_outb - old_outb;
335 ifp->if_in_curpps = new_inp - old_inp;
336 ifp->if_out_curpps = new_outp - old_outp;
339 * Rather than divide by the time specified on the comm-
340 * and line, we divide by ``elapsed'' as this is likely
341 * to be more accurate.
343 ifp->if_in_curtraffic /= elapsed;
344 ifp->if_out_curtraffic /= elapsed;
345 ifp->if_in_curpps /= elapsed;
346 ifp->if_out_curpps /= elapsed;
348 if (ifp->if_in_curtraffic > ifp->if_in_traffic_peak)
349 ifp->if_in_traffic_peak = ifp->if_in_curtraffic;
351 if (ifp->if_out_curtraffic > ifp->if_out_traffic_peak)
352 ifp->if_out_traffic_peak = ifp->if_out_curtraffic;
354 if (ifp->if_in_curpps > ifp->if_in_pps_peak)
355 ifp->if_in_pps_peak = ifp->if_in_curpps;
357 if (ifp->if_out_curpps > ifp->if_out_pps_peak)
358 ifp->if_out_pps_peak = ifp->if_out_curpps;
360 ifp->tv.tv_sec = new_tv.tv_sec;
361 ifp->tv.tv_usec = new_tv.tv_usec;
366 sort_interface_list();
372 * We want to right justify our interface names against the first column
373 * (first sixteen or so characters), so we need to do some alignment.
374 * We save original name so that we can find a same spot is take by a
378 format_device_name(struct if_stat *ifp)
382 snprintf(ifp->display_name, IF_NAMESIZE, "%*s", IF_NAMESIZE-1,
383 ifp->if_mib.ifmd_name);
384 strcpy(ifp->dev_name, ifp->if_mib.ifmd_name);
389 check_match(const char *ifname)
391 char *p = matchline, *c, t;
394 if (matchline == NULL)
397 /* Strip leading whitespaces */
402 while ((mlen = strcspn(c, " ;,")) != 0) {
407 if (fnmatch(c, ifname, FNM_CASEFOLD) == 0) {
412 c = p + strspn(p, " ;,");
415 c = p + strspn(p, " ;,");
423 * This function iterates through our list of interfaces, identifying
424 * those that are to be displayed (ifp->display = 1). For each interf-
425 * rface that we're displaying, we generate an appropriate position for
426 * it on the screen (ifp->if_ypos).
428 * This function is called any time a change is made to an interface's
432 sort_interface_list(void)
434 struct if_stat *ifp = NULL;
438 SLIST_FOREACH(ifp, &curlist, link) {
439 if (matchline && !check_match(ifp->if_mib.ifmd_name))
443 if (ifp->display && ifp->match) {
461 static int name[] = { CTL_NET,
467 datalen = sizeof(data);
468 if (sysctl(name, 5, (void *)&data, (size_t *)&datalen, (void *)NULL,
470 IFSTAT_ERR(1, "sysctl error");
475 getifmibdata(int row, struct ifmibdata *data)
479 static int name[] = { CTL_NET,
485 datalen = sizeof(*data);
488 ret = sysctl(name, 6, (void *)data, (size_t *)&datalen, (void *)NULL,
490 if ((ret != 0) && (errno != ENOENT))
491 IFSTAT_ERR(2, "sysctl error getting interface data");
497 cmdifstat(const char *cmd, const char *args)
501 retval = ifcmd(cmd, args);
502 /* ifcmd() returns 1 on success */
507 else if (prefix(cmd, "all")) {