blob: d918151282f5510f0ce45a14ff29a96fec8cbe77 [file] [log] [blame]
osdl.net!shemminger4677a542004-10-19 20:47:13 +00001/* lnstat.c: Unified linux network statistics
2 *
3 * Copyright (C) 2004 by Harald Welte <laforge@gnumonks.org>
4 *
5 * Development of this code was funded by Astaro AG, http://www.astaro.com/
6 *
7 * Based on original concept and ideas from predecessor rtstat.c:
8 *
9 * Copyright 2001 by Robert Olsson <robert.olsson@its.uu.se>
10 * Uppsala University, Sweden
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 */
18
19#include <unistd.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <dirent.h>
24#include <limits.h>
25#include <time.h>
26
27#include <sys/time.h>
28#include <sys/types.h>
29
30#include "lnstat.h"
31
32/* size of temp buffer used to read lines from procfiles */
33#define FGETS_BUF_SIZE 1024
34
35
36#define RTSTAT_COMPAT_LINE "entries in_hit in_slow_tot in_no_route in_brd in_martian_dst in_martian_src out_hit out_slow_tot out_slow_mc gc_total gc_ignored gc_goal_miss gc_dst_overflow in_hlist_search out_hlist_search\n"
37
38/* Read (and summarize for SMP) the different stats vars. */
39static int scan_lines(struct lnstat_file *lf, int i)
40{
Phil Sutterdb3ef442015-11-28 01:00:01 +010041 char buf[FGETS_BUF_SIZE];
osdl.net!shemminger4677a542004-10-19 20:47:13 +000042 int j, num_lines = 0;
43
44 for (j = 0; j < lf->num_fields; j++)
45 lf->fields[j].values[i] = 0;
46
Phil Sutterdb3ef442015-11-28 01:00:01 +010047 rewind(lf->fp);
48 /* skip first line */
49 if (!lf->compat && !fgets(buf, sizeof(buf)-1, lf->fp))
50 return -1;
51
Stephen Hemmingeracd1e432016-03-21 11:56:36 -070052 while (!feof(lf->fp) && fgets(buf, sizeof(buf)-1, lf->fp)) {
osdl.net!shemminger4677a542004-10-19 20:47:13 +000053 char *ptr = buf;
54
55 num_lines++;
56
osdl.net!shemminger4677a542004-10-19 20:47:13 +000057 gettimeofday(&lf->last_read, NULL);
58
osdl.net!shemmingerf5f1a6c2005-03-18 19:40:19 +000059 for (j = 0; j < lf->num_fields; j++) {
60 unsigned long f = strtoul(ptr, &ptr, 16);
Stephen Hemmingeracd1e432016-03-21 11:56:36 -070061
Stephen Hemmingerae665a52006-12-05 10:10:22 -080062 if (j == 0)
shemminger7339c0b2005-09-01 18:11:03 +000063 lf->fields[j].values[i] = f;
osdl.net!shemmingerf5f1a6c2005-03-18 19:40:19 +000064 else
65 lf->fields[j].values[i] += f;
66 }
osdl.net!shemminger4677a542004-10-19 20:47:13 +000067 }
68 return num_lines;
69}
70
Stephen Hemmingerae665a52006-12-05 10:10:22 -080071static int time_after(struct timeval *last,
72 struct timeval *tout,
osdl.net!shemminger4677a542004-10-19 20:47:13 +000073 struct timeval *now)
74{
75 if (now->tv_sec > last->tv_sec + tout->tv_sec)
76 return 1;
77
78 if (now->tv_sec == last->tv_sec + tout->tv_sec) {
79 if (now->tv_usec > last->tv_usec + tout->tv_usec)
80 return 1;
81 }
82
83 return 0;
84}
85
86int lnstat_update(struct lnstat_file *lnstat_files)
87{
88 struct lnstat_file *lf;
osdl.net!shemminger4677a542004-10-19 20:47:13 +000089 struct timeval tv;
90
91 gettimeofday(&tv, NULL);
92
93 for (lf = lnstat_files; lf; lf = lf->next) {
94 if (time_after(&lf->last_read, &lf->interval, &tv)) {
95 int i;
96 struct lnstat_field *lfi;
97
osdl.net!shemminger4677a542004-10-19 20:47:13 +000098 scan_lines(lf, 1);
99
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800100 for (i = 0, lfi = &lf->fields[i];
osdl.net!shemminger4677a542004-10-19 20:47:13 +0000101 i < lf->num_fields; i++, lfi = &lf->fields[i]) {
102 if (i == 0)
103 lfi->result = lfi->values[1];
104 else
105 lfi->result = (lfi->values[1]-lfi->values[0])
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700106 / lf->interval.tv_sec;
osdl.net!shemminger4677a542004-10-19 20:47:13 +0000107 }
108
osdl.net!shemminger4677a542004-10-19 20:47:13 +0000109 scan_lines(lf, 0);
110 }
111 }
112
113 return 0;
114}
115
116/* scan first template line and fill in per-field data structures */
117static int __lnstat_scan_fields(struct lnstat_file *lf, char *buf)
118{
119 char *tok;
120 int i;
121
122 tok = strtok(buf, " \t\n");
123 for (i = 0; i < LNSTAT_MAX_FIELDS_PER_LINE; i++) {
124 lf->fields[i].file = lf;
125 strncpy(lf->fields[i].name, tok, LNSTAT_MAX_FIELD_NAME_LEN);
126 /* has to be null-terminate since we initialize to zero
127 * and field size is NAME_LEN + 1 */
128 tok = strtok(NULL, " \t\n");
129 if (!tok) {
130 lf->num_fields = i+1;
131 return 0;
132 }
133 }
134 return 0;
135}
136
137static int lnstat_scan_fields(struct lnstat_file *lf)
138{
139 char buf[FGETS_BUF_SIZE];
140
141 rewind(lf->fp);
Phil Sutterd572ed42015-11-28 01:00:04 +0100142 if (!fgets(buf, sizeof(buf)-1, lf->fp))
143 return -1;
osdl.net!shemminger4677a542004-10-19 20:47:13 +0000144
145 return __lnstat_scan_fields(lf, buf);
146}
147
148/* fake function emulating lnstat_scan_fields() for old kernels */
149static int lnstat_scan_compat_rtstat_fields(struct lnstat_file *lf)
150{
151 char buf[FGETS_BUF_SIZE];
152
153 strncpy(buf, RTSTAT_COMPAT_LINE, sizeof(buf)-1);
154
155 return __lnstat_scan_fields(lf, buf);
156}
157
158/* find out whether string 'name; is in given string array */
159static int name_in_array(const int num, const char **arr, const char *name)
160{
161 int i;
Stephen Hemmingeracd1e432016-03-21 11:56:36 -0700162
osdl.net!shemminger4677a542004-10-19 20:47:13 +0000163 for (i = 0; i < num; i++) {
164 if (!strcmp(arr[i], name))
165 return 1;
166 }
167 return 0;
168}
169
170/* allocate lnstat_file and open given file */
171static struct lnstat_file *alloc_and_open(const char *path, const char *file)
172{
173 struct lnstat_file *lf;
174
175 /* allocate */
176 lf = malloc(sizeof(*lf));
Stephen Hemmingerb90b7732015-12-30 17:28:11 -0800177 if (!lf) {
178 fprintf(stderr, "out of memory\n");
osdl.net!shemminger4677a542004-10-19 20:47:13 +0000179 return NULL;
Stephen Hemmingerb90b7732015-12-30 17:28:11 -0800180 }
osdl.net!shemminger4677a542004-10-19 20:47:13 +0000181
182 /* initialize */
183 memset(lf, 0, sizeof(*lf));
184
185 /* de->d_name is guaranteed to be <= NAME_MAX */
186 strcpy(lf->basename, file);
187 strcpy(lf->path, path);
188 strcat(lf->path, "/");
189 strcat(lf->path, lf->basename);
190
191 /* initialize to default */
192 lf->interval.tv_sec = 1;
193
194 /* open */
195 lf->fp = fopen(lf->path, "r");
196 if (!lf->fp) {
Stephen Hemmingerb90b7732015-12-30 17:28:11 -0800197 perror(lf->path);
osdl.net!shemminger4677a542004-10-19 20:47:13 +0000198 free(lf);
199 return NULL;
200 }
201
202 return lf;
203}
204
205
206/* lnstat_scan_dir - find and parse all available statistics files/fields */
207struct lnstat_file *lnstat_scan_dir(const char *path, const int num_req_files,
208 const char **req_files)
209{
210 DIR *dir;
211 struct lnstat_file *lnstat_files = NULL;
212 struct dirent *de;
213
214 if (!path)
215 path = PROC_NET_STAT;
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800216
osdl.net!shemminger4677a542004-10-19 20:47:13 +0000217 dir = opendir(path);
218 if (!dir) {
219 struct lnstat_file *lf;
220 /* Old kernel, before /proc/net/stat was introduced */
221 fprintf(stderr, "Your kernel doesn't have lnstat support. ");
222
223 /* we only support rtstat, not multiple files */
224 if (num_req_files >= 2) {
225 fputc('\n', stderr);
226 return NULL;
227 }
228
229 /* we really only accept rt_cache */
230 if (num_req_files && !name_in_array(num_req_files,
231 req_files, "rt_cache")) {
232 fputc('\n', stderr);
233 return NULL;
234 }
235
236 fprintf(stderr, "Fallback to old rtstat-only operation\n");
237
238 lf = alloc_and_open("/proc/net", "rt_cache_stat");
239 if (!lf)
240 return NULL;
241 lf->compat = 1;
242 strncpy(lf->basename, "rt_cache", sizeof(lf->basename));
243
244 /* FIXME: support for old files */
245 if (lnstat_scan_compat_rtstat_fields(lf) < 0)
246 return NULL;
247
248 lf->next = lnstat_files;
249 lnstat_files = lf;
250 return lnstat_files;
251 }
252
253 while ((de = readdir(dir))) {
254 struct lnstat_file *lf;
255
256 if (de->d_type != DT_REG)
257 continue;
258
259 if (num_req_files && !name_in_array(num_req_files,
260 req_files, de->d_name))
261 continue;
262
263 lf = alloc_and_open(path, de->d_name);
Stephen Hemmingerb90b7732015-12-30 17:28:11 -0800264 if (!lf) {
265 closedir(dir);
osdl.net!shemminger4677a542004-10-19 20:47:13 +0000266 return NULL;
Stephen Hemmingerb90b7732015-12-30 17:28:11 -0800267 }
osdl.net!shemminger4677a542004-10-19 20:47:13 +0000268
269 /* fill in field structure */
Stephen Hemmingerb90b7732015-12-30 17:28:11 -0800270 if (lnstat_scan_fields(lf) < 0) {
271 closedir(dir);
osdl.net!shemminger4677a542004-10-19 20:47:13 +0000272 return NULL;
Stephen Hemmingerb90b7732015-12-30 17:28:11 -0800273 }
osdl.net!shemminger4677a542004-10-19 20:47:13 +0000274
275 /* prepend to global list */
276 lf->next = lnstat_files;
277 lnstat_files = lf;
278 }
279 closedir(dir);
280
281 return lnstat_files;
282}
283
284int lnstat_dump(FILE *outfd, struct lnstat_file *lnstat_files)
285{
286 struct lnstat_file *lf;
287
288 for (lf = lnstat_files; lf; lf = lf->next) {
289 int i;
290
291 fprintf(outfd, "%s:\n", lf->path);
292
293 for (i = 0; i < lf->num_fields; i++)
294 fprintf(outfd, "\t%2u: %s\n", i+1, lf->fields[i].name);
295
296 }
297 return 0;
298}
299
300struct lnstat_field *lnstat_find_field(struct lnstat_file *lnstat_files,
301 const char *name)
302{
303 struct lnstat_file *lf;
304 struct lnstat_field *ret = NULL;
305 const char *colon = strchr(name, ':');
306 char *file;
307 const char *field;
308
309 if (colon) {
310 file = strndup(name, colon-name);
311 field = colon+1;
312 } else {
313 file = NULL;
314 field = name;
315 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800316
osdl.net!shemminger4677a542004-10-19 20:47:13 +0000317 for (lf = lnstat_files; lf; lf = lf->next) {
318 int i;
319
320 if (file && strcmp(file, lf->basename))
321 continue;
322
323 for (i = 0; i < lf->num_fields; i++) {
324 if (!strcmp(field, lf->fields[i].name)) {
325 ret = &lf->fields[i];
326 goto out;
327 }
328 }
329 }
330out:
Stephen Hemminger92de1c22015-07-21 10:49:54 -0700331 free(file);
osdl.net!shemminger4677a542004-10-19 20:47:13 +0000332
333 return ret;
334}