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>
52 #define C1 0 /* 0-19 */
53 #define C2 20 /* 20-39 */
54 #define C3 40 /* 40-59 */
55 #define C4 60 /* 60-80 */
56 #define C5 80 /* Used for label positioning. */
58 static const int col0 = 0;
59 static const int col1 = C1;
60 static const int col2 = C2;
61 static const int col3 = C3;
62 static const int col4 = C4;
63 static const int col5 = C5;
65 SLIST_HEAD(, if_stat) curlist;
66 SLIST_HEAD(, if_stat_disp) displist;
69 SLIST_ENTRY(if_stat) link;
70 char if_name[IF_NAMESIZE];
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;
95 static void right_align_string(struct if_stat *);
96 static void getifmibdata(const int, struct ifmibdata *);
97 static void sort_interface_list(void);
98 static u_int getifnum(void);
100 #define IFSTAT_ERR(n, s) do { \
108 " Interface Traffic Peak Total"
110 #define STARTING_ROW (TOPLINE + 1)
111 #define ROW_SPACING (3)
113 #define IN_col2 (showpps ? ifp->if_in_curpps : ifp->if_in_curtraffic)
114 #define OUT_col2 (showpps ? ifp->if_out_curpps : ifp->if_out_curtraffic)
115 #define IN_col3 (showpps ? \
116 ifp->if_in_pps_peak : ifp->if_in_traffic_peak)
117 #define OUT_col3 (showpps ? \
118 ifp->if_out_pps_peak : ifp->if_out_traffic_peak)
119 #define IN_col4 (showpps ? \
120 ifp->if_mib.ifmd_data.ifi_ipackets : ifp->if_mib.ifmd_data.ifi_ibytes)
121 #define OUT_col4 (showpps ? \
122 ifp->if_mib.ifmd_data.ifi_opackets : ifp->if_mib.ifmd_data.ifi_obytes)
124 #define EMPTY_COLUMN " "
125 #define CLEAR_COLUMN(y, x) mvprintw((y), (x), "%20s", EMPTY_COLUMN);
127 #define DOPUTRATE(c, r, d) do { \
128 CLEAR_COLUMN(r, c); \
130 mvprintw(r, (c), "%10.3f %cp%s ", \
131 convert(d##_##c, curscale), \
132 *get_string(d##_##c, curscale), \
136 mvprintw(r, (c), "%10.3f %s%s ", \
137 convert(d##_##c, curscale), \
138 get_string(d##_##c, curscale), \
143 #define DOPUTTOTAL(c, r, d) do { \
144 CLEAR_COLUMN((r), (c)); \
146 mvprintw((r), (c), "%12.3f %cp ", \
147 convert(d##_##c, SC_AUTO), \
148 *get_string(d##_##c, SC_AUTO)); \
151 mvprintw((r), (c), "%12.3f %s ", \
152 convert(d##_##c, SC_AUTO), \
153 get_string(d##_##c, SC_AUTO)); \
157 #define PUTRATE(c, r) do { \
158 DOPUTRATE(c, (r), IN); \
159 DOPUTRATE(c, (r)+1, OUT); \
162 #define PUTTOTAL(c, r) do { \
163 DOPUTTOTAL(c, (r), IN); \
164 DOPUTTOTAL(c, (r)+1, OUT); \
167 #define PUTNAME(p) do { \
168 mvprintw(p->if_ypos, 0, "%s", p->if_name); \
169 mvprintw(p->if_ypos, col2-3, "%s", (const char *)"in"); \
170 mvprintw(p->if_ypos+1, col2-3, "%s", (const char *)"out"); \
176 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
180 closeifstat(WINDOW *w)
182 struct if_stat *node = NULL;
184 while (!SLIST_EMPTY(&curlist)) {
185 node = SLIST_FIRST(&curlist);
186 SLIST_REMOVE_HEAD(&curlist, link);
203 wmove(wnd, TOPLINE, 0);
205 mvprintw(TOPLINE, 0, "%s", TOPLABEL);
213 struct if_stat *ifp = NULL;
215 SLIST_FOREACH(ifp, &curlist, link) {
216 if (ifp->if_ypos < LINES - 3 && ifp->if_ypos != -1)
217 if (ifp->display == 0 || ifp->match == 0) {
218 wmove(wnd, ifp->if_ypos, 0);
220 wmove(wnd, ifp->if_ypos + 1, 0);
225 PUTRATE(col2, ifp->if_ypos);
226 PUTRATE(col3, ifp->if_ypos);
227 PUTTOTAL(col4, ifp->if_ypos);
237 struct if_stat *p = NULL;
244 SLIST_INIT(&curlist);
246 for (i = 0; i < n; i++) {
247 p = (struct if_stat *)calloc(1, sizeof(struct if_stat));
249 IFSTAT_ERR(1, "out of memory");
250 SLIST_INSERT_HEAD(&curlist, p, link);
252 getifmibdata(p->if_row, &p->if_mib);
253 right_align_string(p);
257 * Initially, we only display interfaces that have
258 * received some traffic.
260 if (p->if_mib.ifmd_data.ifi_ibytes != 0)
264 sort_interface_list();
272 struct if_stat *ifp = NULL;
273 struct timeval tv, new_tv, old_tv;
274 double elapsed = 0.0;
275 uint64_t new_inb, new_outb, old_inb, old_outb = 0;
276 uint64_t new_inp, new_outp, old_inp, old_outp = 0;
278 SLIST_FOREACH(ifp, &curlist, link) {
280 * Grab a copy of the old input/output values before we
281 * call getifmibdata().
283 old_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
284 old_outb = ifp->if_mib.ifmd_data.ifi_obytes;
285 old_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
286 old_outp = ifp->if_mib.ifmd_data.ifi_opackets;
287 ifp->tv_lastchanged = ifp->if_mib.ifmd_data.ifi_lastchange;
289 (void)gettimeofday(&new_tv, NULL);
290 (void)getifmibdata(ifp->if_row, &ifp->if_mib);
292 new_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
293 new_outb = ifp->if_mib.ifmd_data.ifi_obytes;
294 new_inp = ifp->if_mib.ifmd_data.ifi_ipackets;
295 new_outp = ifp->if_mib.ifmd_data.ifi_opackets;
297 /* Display interface if it's received some traffic. */
298 if (new_inb > 0 && old_inb == 0) {
304 * The rest is pretty trivial. Calculate the new values
305 * for our current traffic rates, and while we're there,
306 * see if we have new peak rates.
309 timersub(&new_tv, &old_tv, &tv);
310 elapsed = tv.tv_sec + (tv.tv_usec * 1e-6);
312 ifp->if_in_curtraffic = new_inb - old_inb;
313 ifp->if_out_curtraffic = new_outb - old_outb;
315 ifp->if_in_curpps = new_inp - old_inp;
316 ifp->if_out_curpps = new_outp - old_outp;
319 * Rather than divide by the time specified on the comm-
320 * and line, we divide by ``elapsed'' as this is likely
321 * to be more accurate.
323 ifp->if_in_curtraffic /= elapsed;
324 ifp->if_out_curtraffic /= elapsed;
325 ifp->if_in_curpps /= elapsed;
326 ifp->if_out_curpps /= elapsed;
328 if (ifp->if_in_curtraffic > ifp->if_in_traffic_peak)
329 ifp->if_in_traffic_peak = ifp->if_in_curtraffic;
331 if (ifp->if_out_curtraffic > ifp->if_out_traffic_peak)
332 ifp->if_out_traffic_peak = ifp->if_out_curtraffic;
334 if (ifp->if_in_curpps > ifp->if_in_pps_peak)
335 ifp->if_in_pps_peak = ifp->if_in_curpps;
337 if (ifp->if_out_curpps > ifp->if_out_pps_peak)
338 ifp->if_out_pps_peak = ifp->if_out_curpps;
340 ifp->tv.tv_sec = new_tv.tv_sec;
341 ifp->tv.tv_usec = new_tv.tv_usec;
346 sort_interface_list();
352 * We want to right justify our interface names against the first column
353 * (first sixteen or so characters), so we need to do some alignment.
356 right_align_string(struct if_stat *ifp)
358 int str_len = 0, pad_len = 0;
359 char *newstr = NULL, *ptr = NULL;
361 if (ifp == NULL || ifp->if_mib.ifmd_name == NULL)
364 /* string length + '\0' */
365 str_len = strlen(ifp->if_mib.ifmd_name)+1;
366 pad_len = IF_NAMESIZE-(str_len);
368 newstr = ifp->if_name;
369 ptr = newstr + pad_len;
370 (void)memset((void *)newstr, (int)' ', IF_NAMESIZE);
371 (void)strncpy(ptr, (const char *)&ifp->if_mib.ifmd_name,
379 check_match(const char *ifname)
381 char *p = matchline, *c, t;
384 if (matchline == NULL)
387 /* Strip leading whitespaces */
392 while ((mlen = strcspn(c, " ;,")) != 0) {
397 if (fnmatch(c, ifname, FNM_CASEFOLD) == 0) {
402 c = p + strspn(p, " ;,");
405 c = p + strspn(p, " ;,");
413 * This function iterates through our list of interfaces, identifying
414 * those that are to be displayed (ifp->display = 1). For each interf-
415 * rface that we're displaying, we generate an appropriate position for
416 * it on the screen (ifp->if_ypos).
418 * This function is called any time a change is made to an interface's
422 sort_interface_list(void)
424 struct if_stat *ifp = NULL;
428 SLIST_FOREACH(ifp, &curlist, link) {
429 if (matchline && !check_match(ifp->if_mib.ifmd_name))
433 if (ifp->display && ifp->match) {
451 static int name[] = { CTL_NET,
457 datalen = sizeof(data);
458 if (sysctl(name, 5, (void *)&data, (size_t *)&datalen, (void *)NULL,
460 IFSTAT_ERR(1, "sysctl error");
465 getifmibdata(int row, struct ifmibdata *data)
468 static int name[] = { CTL_NET,
474 datalen = sizeof(*data);
477 if ((sysctl(name, 6, (void *)data, (size_t *)&datalen, (void *)NULL,
478 (size_t)0) != 0) && (errno != ENOENT))
479 IFSTAT_ERR(2, "sysctl error getting interface data");
483 cmdifstat(const char *cmd, const char *args)
487 retval = ifcmd(cmd, args);
488 /* ifcmd() returns 1 on success */