/usr.bin/systat/ifstat.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 400 lines · 271 code · 65 blank · 64 comment · 38 complexity · 1ae97ad1ff49ddceed07cb1da9536e1d MD5 · raw file

  1. /*
  2. * Copyright (c) 2003, Trent Nelson, <trent@arpa.com>.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTIFSTAT_ERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. *
  28. * $FreeBSD$
  29. */
  30. #include <sys/types.h>
  31. #include <sys/socket.h>
  32. #include <sys/sysctl.h>
  33. #include <net/if.h>
  34. #include <net/if_mib.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <err.h>
  38. #include <errno.h>
  39. #include "systat.h"
  40. #include "extern.h"
  41. #include "convtbl.h"
  42. /* Column numbers */
  43. #define C1 0 /* 0-19 */
  44. #define C2 20 /* 20-39 */
  45. #define C3 40 /* 40-59 */
  46. #define C4 60 /* 60-80 */
  47. #define C5 80 /* Used for label positioning. */
  48. static const int col0 = 0;
  49. static const int col1 = C1;
  50. static const int col2 = C2;
  51. static const int col3 = C3;
  52. static const int col4 = C4;
  53. static const int col5 = C5;
  54. SLIST_HEAD(, if_stat) curlist;
  55. SLIST_HEAD(, if_stat_disp) displist;
  56. struct if_stat {
  57. SLIST_ENTRY(if_stat) link;
  58. char if_name[IF_NAMESIZE];
  59. struct ifmibdata if_mib;
  60. struct timeval tv;
  61. struct timeval tv_lastchanged;
  62. u_long if_in_curtraffic;
  63. u_long if_out_curtraffic;
  64. u_long if_in_traffic_peak;
  65. u_long if_out_traffic_peak;
  66. u_int if_row; /* Index into ifmib sysctl */
  67. u_int if_ypos; /* 0 if not being displayed */
  68. u_int display;
  69. };
  70. extern u_int curscale;
  71. static void right_align_string(struct if_stat *);
  72. static void getifmibdata(const int, struct ifmibdata *);
  73. static void sort_interface_list(void);
  74. static u_int getifnum(void);
  75. #define IFSTAT_ERR(n, s) do { \
  76. putchar('\014'); \
  77. closeifstat(wnd); \
  78. err((n), (s)); \
  79. } while (0)
  80. #define TOPLINE 3
  81. #define TOPLABEL \
  82. " Interface Traffic Peak Total"
  83. #define STARTING_ROW (TOPLINE + 1)
  84. #define ROW_SPACING (3)
  85. #define CLEAR_LINE(y, x) do { \
  86. wmove(wnd, y, x); \
  87. wclrtoeol(wnd); \
  88. } while (0)
  89. #define IN_col2 (ifp->if_in_curtraffic)
  90. #define OUT_col2 (ifp->if_out_curtraffic)
  91. #define IN_col3 (ifp->if_in_traffic_peak)
  92. #define OUT_col3 (ifp->if_out_traffic_peak)
  93. #define IN_col4 (ifp->if_mib.ifmd_data.ifi_ibytes)
  94. #define OUT_col4 (ifp->if_mib.ifmd_data.ifi_obytes)
  95. #define EMPTY_COLUMN " "
  96. #define CLEAR_COLUMN(y, x) mvprintw((y), (x), "%20s", EMPTY_COLUMN);
  97. #define DOPUTRATE(c, r, d) do { \
  98. CLEAR_COLUMN(r, c); \
  99. mvprintw(r, (c), "%10.3f %s%s ", \
  100. convert(d##_##c, curscale), \
  101. get_string(d##_##c, curscale), \
  102. "/s"); \
  103. } while (0)
  104. #define DOPUTTOTAL(c, r, d) do { \
  105. CLEAR_COLUMN((r), (c)); \
  106. mvprintw((r), (c), "%12.3f %s ", \
  107. convert(d##_##c, SC_AUTO), \
  108. get_string(d##_##c, SC_AUTO)); \
  109. } while (0)
  110. #define PUTRATE(c, r) do { \
  111. DOPUTRATE(c, (r), IN); \
  112. DOPUTRATE(c, (r)+1, OUT); \
  113. } while (0)
  114. #define PUTTOTAL(c, r) do { \
  115. DOPUTTOTAL(c, (r), IN); \
  116. DOPUTTOTAL(c, (r)+1, OUT); \
  117. } while (0)
  118. #define PUTNAME(p) do { \
  119. mvprintw(p->if_ypos, 0, "%s", p->if_name); \
  120. mvprintw(p->if_ypos, col2-3, "%s", (const char *)"in"); \
  121. mvprintw(p->if_ypos+1, col2-3, "%s", (const char *)"out"); \
  122. } while (0)
  123. WINDOW *
  124. openifstat(void)
  125. {
  126. return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
  127. }
  128. void
  129. closeifstat(WINDOW *w)
  130. {
  131. struct if_stat *node = NULL;
  132. while (!SLIST_EMPTY(&curlist)) {
  133. node = SLIST_FIRST(&curlist);
  134. SLIST_REMOVE_HEAD(&curlist, link);
  135. free(node);
  136. }
  137. if (w != NULL) {
  138. wclear(w);
  139. wrefresh(w);
  140. delwin(w);
  141. }
  142. return;
  143. }
  144. void
  145. labelifstat(void)
  146. {
  147. wmove(wnd, TOPLINE, 0);
  148. wclrtoeol(wnd);
  149. mvprintw(TOPLINE, 0, "%s", TOPLABEL);
  150. return;
  151. }
  152. void
  153. showifstat(void)
  154. {
  155. struct if_stat *ifp = NULL;
  156. SLIST_FOREACH(ifp, &curlist, link) {
  157. if (ifp->display == 0)
  158. continue;
  159. PUTNAME(ifp);
  160. PUTRATE(col2, ifp->if_ypos);
  161. PUTRATE(col3, ifp->if_ypos);
  162. PUTTOTAL(col4, ifp->if_ypos);
  163. }
  164. return;
  165. }
  166. int
  167. initifstat(void)
  168. {
  169. struct if_stat *p = NULL;
  170. u_int n = 0, i = 0;
  171. n = getifnum();
  172. if (n <= 0)
  173. return -1;
  174. SLIST_INIT(&curlist);
  175. for (i = 0; i < n; i++) {
  176. p = (struct if_stat *)calloc(1, sizeof(struct if_stat));
  177. if (p == NULL)
  178. IFSTAT_ERR(1, "out of memory");
  179. SLIST_INSERT_HEAD(&curlist, p, link);
  180. p->if_row = i+1;
  181. getifmibdata(p->if_row, &p->if_mib);
  182. right_align_string(p);
  183. /*
  184. * Initially, we only display interfaces that have
  185. * received some traffic.
  186. */
  187. if (p->if_mib.ifmd_data.ifi_ibytes != 0)
  188. p->display = 1;
  189. }
  190. sort_interface_list();
  191. return 1;
  192. }
  193. void
  194. fetchifstat(void)
  195. {
  196. struct if_stat *ifp = NULL;
  197. struct timeval tv, new_tv, old_tv;
  198. double elapsed = 0.0;
  199. u_int new_inb, new_outb, old_inb, old_outb = 0;
  200. u_int we_need_to_sort_interface_list = 0;
  201. SLIST_FOREACH(ifp, &curlist, link) {
  202. /*
  203. * Grab a copy of the old input/output values before we
  204. * call getifmibdata().
  205. */
  206. old_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
  207. old_outb = ifp->if_mib.ifmd_data.ifi_obytes;
  208. ifp->tv_lastchanged = ifp->if_mib.ifmd_data.ifi_lastchange;
  209. (void)gettimeofday(&new_tv, NULL);
  210. (void)getifmibdata(ifp->if_row, &ifp->if_mib);
  211. new_inb = ifp->if_mib.ifmd_data.ifi_ibytes;
  212. new_outb = ifp->if_mib.ifmd_data.ifi_obytes;
  213. /* Display interface if it's received some traffic. */
  214. if (new_inb > 0 && old_inb == 0) {
  215. ifp->display = 1;
  216. we_need_to_sort_interface_list++;
  217. }
  218. /*
  219. * The rest is pretty trivial. Calculate the new values
  220. * for our current traffic rates, and while we're there,
  221. * see if we have new peak rates.
  222. */
  223. old_tv = ifp->tv;
  224. timersub(&new_tv, &old_tv, &tv);
  225. elapsed = tv.tv_sec + (tv.tv_usec * 1e-6);
  226. ifp->if_in_curtraffic = new_inb - old_inb;
  227. ifp->if_out_curtraffic = new_outb - old_outb;
  228. /*
  229. * Rather than divide by the time specified on the comm-
  230. * and line, we divide by ``elapsed'' as this is likely
  231. * to be more accurate.
  232. */
  233. ifp->if_in_curtraffic /= elapsed;
  234. ifp->if_out_curtraffic /= elapsed;
  235. if (ifp->if_in_curtraffic > ifp->if_in_traffic_peak)
  236. ifp->if_in_traffic_peak = ifp->if_in_curtraffic;
  237. if (ifp->if_out_curtraffic > ifp->if_out_traffic_peak)
  238. ifp->if_out_traffic_peak = ifp->if_out_curtraffic;
  239. ifp->tv.tv_sec = new_tv.tv_sec;
  240. ifp->tv.tv_usec = new_tv.tv_usec;
  241. }
  242. if (we_need_to_sort_interface_list)
  243. sort_interface_list();
  244. return;
  245. }
  246. /*
  247. * We want to right justify our interface names against the first column
  248. * (first sixteen or so characters), so we need to do some alignment.
  249. */
  250. static void
  251. right_align_string(struct if_stat *ifp)
  252. {
  253. int str_len = 0, pad_len = 0;
  254. char *newstr = NULL, *ptr = NULL;
  255. if (ifp == NULL || ifp->if_mib.ifmd_name == NULL)
  256. return;
  257. else {
  258. /* string length + '\0' */
  259. str_len = strlen(ifp->if_mib.ifmd_name)+1;
  260. pad_len = IF_NAMESIZE-(str_len);
  261. newstr = ifp->if_name;
  262. ptr = newstr + pad_len;
  263. (void)memset((void *)newstr, (int)' ', IF_NAMESIZE);
  264. (void)strncpy(ptr, (const char *)&ifp->if_mib.ifmd_name,
  265. str_len);
  266. }
  267. return;
  268. }
  269. /*
  270. * This function iterates through our list of interfaces, identifying
  271. * those that are to be displayed (ifp->display = 1). For each interf-
  272. * rface that we're displaying, we generate an appropriate position for
  273. * it on the screen (ifp->if_ypos).
  274. *
  275. * This function is called any time a change is made to an interface's
  276. * ``display'' state.
  277. */
  278. void
  279. sort_interface_list(void)
  280. {
  281. struct if_stat *ifp = NULL;
  282. u_int y = 0;
  283. y = STARTING_ROW;
  284. SLIST_FOREACH(ifp, &curlist, link) {
  285. if (ifp->display) {
  286. ifp->if_ypos = y;
  287. y += ROW_SPACING;
  288. }
  289. }
  290. }
  291. static
  292. unsigned int
  293. getifnum(void)
  294. {
  295. u_int data = 0;
  296. size_t datalen = 0;
  297. static int name[] = { CTL_NET,
  298. PF_LINK,
  299. NETLINK_GENERIC,
  300. IFMIB_SYSTEM,
  301. IFMIB_IFCOUNT };
  302. datalen = sizeof(data);
  303. if (sysctl(name, 5, (void *)&data, (size_t *)&datalen, (void *)NULL,
  304. (size_t)0) != 0)
  305. IFSTAT_ERR(1, "sysctl error");
  306. return data;
  307. }
  308. static void
  309. getifmibdata(int row, struct ifmibdata *data)
  310. {
  311. size_t datalen = 0;
  312. static int name[] = { CTL_NET,
  313. PF_LINK,
  314. NETLINK_GENERIC,
  315. IFMIB_IFDATA,
  316. 0,
  317. IFDATA_GENERAL };
  318. datalen = sizeof(*data);
  319. name[4] = row;
  320. if ((sysctl(name, 6, (void *)data, (size_t *)&datalen, (void *)NULL,
  321. (size_t)0) != 0) && (errno != ENOENT))
  322. IFSTAT_ERR(2, "sysctl error getting interface data");
  323. }
  324. int
  325. cmdifstat(const char *cmd, const char *args)
  326. {
  327. int retval = 0;
  328. retval = ifcmd(cmd, args);
  329. /* ifcmd() returns 1 on success */
  330. if (retval == 1) {
  331. showifstat();
  332. refresh();
  333. }
  334. return retval;
  335. }