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 col2 = C2;
60 static const int col3 = C3;
61 static const int col4 = C4;
63 static SLIST_HEAD(, if_stat) curlist;
66 SLIST_ENTRY(if_stat) link;
67 char display_name[IF_NAMESIZE];
68 char dev_name[IFNAMSIZ]; /* copied from ifmibdata */
69 struct ifmibdata if_mib;
71 struct timeval tv_lastchanged;
72 uint64_t if_in_curtraffic;
73 uint64_t if_out_curtraffic;
74 uint64_t if_in_traffic_peak;
75 uint64_t if_out_traffic_peak;
76 uint64_t if_in_curpps;
77 uint64_t if_out_curpps;
78 uint64_t if_in_pps_peak;
79 uint64_t if_out_pps_peak;
80 u_int if_row; /* Index into ifmib sysctl */
81 int if_ypos; /* -1 if not being displayed */
86 static int needclear = 0;
87 static bool displayall = false;
89 static void format_device_name(struct if_stat *);
90 static int getifmibdata(const int, struct ifmibdata *);
91 static void sort_interface_list(void);
92 static u_int getifnum(void);
93 static void clearifstat(void);
95 #define IFSTAT_ERR(n, s) do { \
103 " Interface Traffic Peak Total"
105 #define STARTING_ROW (TOPLINE + 1)
106 #define ROW_SPACING (3)
108 #define IN_col2 (showpps ? ifp->if_in_curpps : ifp->if_in_curtraffic)
109 #define OUT_col2 (showpps ? ifp->if_out_curpps : ifp->if_out_curtraffic)
110 #define IN_col3 (showpps ? \
111 ifp->if_in_pps_peak : ifp->if_in_traffic_peak)
112 #define OUT_col3 (showpps ? \
113 ifp->if_out_pps_peak : ifp->if_out_traffic_peak)
114 #define IN_col4 (showpps ? \
115 ifp->if_mib.ifmd_data.ifi_ipackets : ifp->if_mib.ifmd_data.ifi_ibytes)
116 #define OUT_col4 (showpps ? \
117 ifp->if_mib.ifmd_data.ifi_opackets : ifp->if_mib.ifmd_data.ifi_obytes)
119 #define EMPTY_COLUMN " "
120 #define CLEAR_COLUMN(y, x) mvprintw((y), (x), "%20s", EMPTY_COLUMN);
122 #define DOPUTRATE(c, r, d) do { \
123 CLEAR_COLUMN(r, c); \
125 mvprintw(r, (c), "%10.3f %cp%s ", \
126 convert(d##_##c, curscale), \
127 *get_string(d##_##c, curscale), \
131 mvprintw(r, (c), "%10.3f %s%s ", \
132 convert(d##_##c, curscale), \
133 get_string(d##_##c, curscale), \
138 #define DOPUTTOTAL(c, r, d) do { \
139 CLEAR_COLUMN((r), (c)); \
141 mvprintw((r), (c), "%12.3f %cp ", \
142 convert(d##_##c, SC_AUTO), \
143 *get_string(d##_##c, SC_AUTO)); \
146 mvprintw((r), (c), "%12.3f %s ", \
147 convert(d##_##c, SC_AUTO), \
148 get_string(d##_##c, SC_AUTO)); \
152 #define PUTRATE(c, r) do { \
153 DOPUTRATE(c, (r), IN); \
154 DOPUTRATE(c, (r)+1, OUT); \
157 #define PUTTOTAL(c, r) do { \
158 DOPUTTOTAL(c, (r), IN); \
159 DOPUTTOTAL(c, (r)+1, OUT); \
162 #define PUTNAME(p) do { \
163 mvprintw(p->if_ypos, 0, "%s", p->display_name); \
164 mvprintw(p->if_ypos, col2-3, "%s", (const char *)"in"); \
165 mvprintw(p->if_ypos+1, col2-3, "%s", (const char *)"out"); \
171 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
175 closeifstat(WINDOW *w)
177 struct if_stat *node = NULL;
179 while (!SLIST_EMPTY(&curlist)) {
180 node = SLIST_FIRST(&curlist);
181 SLIST_REMOVE_HEAD(&curlist, link);
198 wmove(wnd, TOPLINE, 0);
200 mvprintw(TOPLINE, 0, "%s", TOPLABEL);
208 struct if_stat *ifp = NULL;
210 SLIST_FOREACH(ifp, &curlist, link) {
211 if (ifp->if_ypos < LINES - 3 && ifp->if_ypos != -1) {
212 if (!ifp->display || ifp->match == 0) {
213 wmove(wnd, ifp->if_ypos, 0);
215 wmove(wnd, ifp->if_ypos + 1, 0);
219 PUTRATE(col2, ifp->if_ypos);
220 PUTRATE(col3, ifp->if_ypos);
221 PUTTOTAL(col4, ifp->if_ypos);
232 struct if_stat *p = NULL;
239 SLIST_INIT(&curlist);
241 for (i = 0; i < n; i++) {
242 p = (struct if_stat *)calloc(1, sizeof(struct if_stat));
244 IFSTAT_ERR(1, "out of memory");
246 if (getifmibdata(p->if_row, &p->if_mib) == -1) {
250 SLIST_INSERT_HEAD(&curlist, p, link);
251 format_device_name(p);
255 * Initially, we only display interfaces that have
256 * received some traffic unless display-all is on.
258 if (displayall || p->if_mib.ifmd_data.ifi_ibytes != 0)
262 sort_interface_list();
270 struct if_stat *ifp = NULL, *temp_var;
271 struct timeval tv, new_tv, old_tv;
272 double elapsed = 0.0;
273 uint64_t new_inb, new_outb, old_inb, old_outb = 0;
274 uint64_t new_inp, new_outp, old_inp, old_outp = 0;
276 SLIST_FOREACH_SAFE(ifp, &curlist, link, temp_var) {
278 * Grab a copy of the old input/output values before we
279 * call getifmibdata().
281 old_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
282 old_outb = ifp->if_mib.ifmd_data.ifi_obytes;
283 old_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
284 old_outp = ifp->if_mib.ifmd_data.ifi_opackets;
285 ifp->tv_lastchanged = ifp->if_mib.ifmd_data.ifi_lastchange;
287 (void)gettimeofday(&new_tv, NULL);
288 if (getifmibdata(ifp->if_row, &ifp->if_mib) == -1 ) {
289 /* if a device was removed */
290 SLIST_REMOVE(&curlist, ifp, if_stat, link);
294 } else if (strcmp(ifp->dev_name, ifp->if_mib.ifmd_name) != 0 ) {
295 /* a device was removed and another one was added */
296 format_device_name(ifp);
297 /* clear to the current value for the new device */
298 old_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
299 old_outb = ifp->if_mib.ifmd_data.ifi_obytes;
300 old_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
301 old_outp = ifp->if_mib.ifmd_data.ifi_opackets;
305 new_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
306 new_outb = ifp->if_mib.ifmd_data.ifi_obytes;
307 new_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
308 new_outp = ifp->if_mib.ifmd_data.ifi_opackets;
310 /* Display interface if it's received some traffic. */
311 if (!ifp->display && new_inb > 0 && old_inb == 0) {
317 * The rest is pretty trivial. Calculate the new values
318 * for our current traffic rates, and while we're there,
319 * see if we have new peak rates.
322 timersub(&new_tv, &old_tv, &tv);
323 elapsed = tv.tv_sec + (tv.tv_usec * 1e-6);
325 ifp->if_in_curtraffic = new_inb - old_inb;
326 ifp->if_out_curtraffic = new_outb - old_outb;
328 ifp->if_in_curpps = new_inp - old_inp;
329 ifp->if_out_curpps = new_outp - old_outp;
332 * Rather than divide by the time specified on the comm-
333 * and line, we divide by ``elapsed'' as this is likely
334 * to be more accurate.
336 ifp->if_in_curtraffic /= elapsed;
337 ifp->if_out_curtraffic /= elapsed;
338 ifp->if_in_curpps /= elapsed;
339 ifp->if_out_curpps /= elapsed;
341 if (ifp->if_in_curtraffic > ifp->if_in_traffic_peak)
342 ifp->if_in_traffic_peak = ifp->if_in_curtraffic;
344 if (ifp->if_out_curtraffic > ifp->if_out_traffic_peak)
345 ifp->if_out_traffic_peak = ifp->if_out_curtraffic;
347 if (ifp->if_in_curpps > ifp->if_in_pps_peak)
348 ifp->if_in_pps_peak = ifp->if_in_curpps;
350 if (ifp->if_out_curpps > ifp->if_out_pps_peak)
351 ifp->if_out_pps_peak = ifp->if_out_curpps;
353 ifp->tv.tv_sec = new_tv.tv_sec;
354 ifp->tv.tv_usec = new_tv.tv_usec;
359 sort_interface_list();
365 * We want to right justify our interface names against the first column
366 * (first sixteen or so characters), so we need to do some alignment.
367 * We save original name so that we can find a same spot is take by a
371 format_device_name(struct if_stat *ifp)
375 snprintf(ifp->display_name, IF_NAMESIZE, "%*s", IF_NAMESIZE-1,
376 ifp->if_mib.ifmd_name);
377 strcpy(ifp->dev_name, ifp->if_mib.ifmd_name);
382 check_match(const char *ifname)
384 char *p = matchline, *ch, t;
387 if (matchline == NULL)
390 /* Strip leading whitespaces */
395 while ((mlen = strcspn(ch, " ;,")) != 0) {
400 if (fnmatch(ch, ifname, FNM_CASEFOLD) == 0) {
405 ch = p + strspn(p, " ;,");
408 ch = p + strspn(p, " ;,");
416 * This function iterates through our list of interfaces, identifying
417 * those that are to be displayed (ifp->display = 1). For each interf-
418 * rface that we're displaying, we generate an appropriate position for
419 * it on the screen (ifp->if_ypos).
421 * This function is called any time a change is made to an interface's
425 sort_interface_list(void)
427 struct if_stat *ifp = NULL;
431 SLIST_FOREACH(ifp, &curlist, link) {
432 if (matchline && !check_match(ifp->if_mib.ifmd_name))
436 if (ifp->display && ifp->match) {
454 static int name[] = { CTL_NET,
460 datalen = sizeof(data);
461 if (sysctl(name, 5, (void *)&data, (size_t *)&datalen, (void *)NULL,
463 IFSTAT_ERR(1, "sysctl error");
468 getifmibdata(int row, struct ifmibdata *data)
472 static int name[] = { CTL_NET,
478 datalen = sizeof(*data);
481 ret = sysctl(name, 6, (void *)data, (size_t *)&datalen, (void *)NULL,
483 if ((ret != 0) && (errno != ENOENT))
484 IFSTAT_ERR(2, "sysctl error getting interface data");
490 cmdifstat(const char *cmd, const char *args)
494 retval = ifcmd(cmd, args);
495 /* ifcmd() returns 1 on success */
500 else if (prefix(cmd, "all")) {