]> git.proxmox.com Git - mirror_iproute2.git/blame - misc/lnstat_util.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/shemminger...
[mirror_iproute2.git] / misc / lnstat_util.c
CommitLineData
4677a549
SH
1/* 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{
db3ef44c 41 char buf[FGETS_BUF_SIZE];
4677a549
SH
42 int j, num_lines = 0;
43
44 for (j = 0; j < lf->num_fields; j++)
45 lf->fields[j].values[i] = 0;
46
db3ef44c
PS
47 rewind(lf->fp);
48 /* skip first line */
49 if (!lf->compat && !fgets(buf, sizeof(buf)-1, lf->fp))
50 return -1;
51
52 while(!feof(lf->fp) && fgets(buf, sizeof(buf)-1, lf->fp)) {
4677a549
SH
53 char *ptr = buf;
54
55 num_lines++;
56
4677a549
SH
57 gettimeofday(&lf->last_read, NULL);
58
f5f1a6c5
SH
59 for (j = 0; j < lf->num_fields; j++) {
60 unsigned long f = strtoul(ptr, &ptr, 16);
ae665a52 61 if (j == 0)
7339c0bb 62 lf->fields[j].values[i] = f;
f5f1a6c5
SH
63 else
64 lf->fields[j].values[i] += f;
65 }
4677a549
SH
66 }
67 return num_lines;
68}
69
ae665a52
SH
70static int time_after(struct timeval *last,
71 struct timeval *tout,
4677a549
SH
72 struct timeval *now)
73{
74 if (now->tv_sec > last->tv_sec + tout->tv_sec)
75 return 1;
76
77 if (now->tv_sec == last->tv_sec + tout->tv_sec) {
78 if (now->tv_usec > last->tv_usec + tout->tv_usec)
79 return 1;
80 }
81
82 return 0;
83}
84
85int lnstat_update(struct lnstat_file *lnstat_files)
86{
87 struct lnstat_file *lf;
4677a549
SH
88 struct timeval tv;
89
90 gettimeofday(&tv, NULL);
91
92 for (lf = lnstat_files; lf; lf = lf->next) {
93 if (time_after(&lf->last_read, &lf->interval, &tv)) {
94 int i;
95 struct lnstat_field *lfi;
96
4677a549
SH
97 scan_lines(lf, 1);
98
ae665a52 99 for (i = 0, lfi = &lf->fields[i];
4677a549
SH
100 i < lf->num_fields; i++, lfi = &lf->fields[i]) {
101 if (i == 0)
102 lfi->result = lfi->values[1];
103 else
104 lfi->result = (lfi->values[1]-lfi->values[0])
105 / lf->interval.tv_sec;
106 }
107
4677a549
SH
108 scan_lines(lf, 0);
109 }
110 }
111
112 return 0;
113}
114
115/* scan first template line and fill in per-field data structures */
116static int __lnstat_scan_fields(struct lnstat_file *lf, char *buf)
117{
118 char *tok;
119 int i;
120
121 tok = strtok(buf, " \t\n");
122 for (i = 0; i < LNSTAT_MAX_FIELDS_PER_LINE; i++) {
123 lf->fields[i].file = lf;
124 strncpy(lf->fields[i].name, tok, LNSTAT_MAX_FIELD_NAME_LEN);
125 /* has to be null-terminate since we initialize to zero
126 * and field size is NAME_LEN + 1 */
127 tok = strtok(NULL, " \t\n");
128 if (!tok) {
129 lf->num_fields = i+1;
130 return 0;
131 }
132 }
133 return 0;
134}
135
136static int lnstat_scan_fields(struct lnstat_file *lf)
137{
138 char buf[FGETS_BUF_SIZE];
139
140 rewind(lf->fp);
d572ed4d
PS
141 if (!fgets(buf, sizeof(buf)-1, lf->fp))
142 return -1;
4677a549
SH
143
144 return __lnstat_scan_fields(lf, buf);
145}
146
147/* fake function emulating lnstat_scan_fields() for old kernels */
148static int lnstat_scan_compat_rtstat_fields(struct lnstat_file *lf)
149{
150 char buf[FGETS_BUF_SIZE];
151
152 strncpy(buf, RTSTAT_COMPAT_LINE, sizeof(buf)-1);
153
154 return __lnstat_scan_fields(lf, buf);
155}
156
157/* find out whether string 'name; is in given string array */
158static int name_in_array(const int num, const char **arr, const char *name)
159{
160 int i;
161 for (i = 0; i < num; i++) {
162 if (!strcmp(arr[i], name))
163 return 1;
164 }
165 return 0;
166}
167
168/* allocate lnstat_file and open given file */
169static struct lnstat_file *alloc_and_open(const char *path, const char *file)
170{
171 struct lnstat_file *lf;
172
173 /* allocate */
174 lf = malloc(sizeof(*lf));
b90b773c
SH
175 if (!lf) {
176 fprintf(stderr, "out of memory\n");
4677a549 177 return NULL;
b90b773c 178 }
4677a549
SH
179
180 /* initialize */
181 memset(lf, 0, sizeof(*lf));
182
183 /* de->d_name is guaranteed to be <= NAME_MAX */
184 strcpy(lf->basename, file);
185 strcpy(lf->path, path);
186 strcat(lf->path, "/");
187 strcat(lf->path, lf->basename);
188
189 /* initialize to default */
190 lf->interval.tv_sec = 1;
191
192 /* open */
193 lf->fp = fopen(lf->path, "r");
194 if (!lf->fp) {
b90b773c 195 perror(lf->path);
4677a549
SH
196 free(lf);
197 return NULL;
198 }
199
200 return lf;
201}
202
203
204/* lnstat_scan_dir - find and parse all available statistics files/fields */
205struct lnstat_file *lnstat_scan_dir(const char *path, const int num_req_files,
206 const char **req_files)
207{
208 DIR *dir;
209 struct lnstat_file *lnstat_files = NULL;
210 struct dirent *de;
211
212 if (!path)
213 path = PROC_NET_STAT;
ae665a52 214
4677a549
SH
215 dir = opendir(path);
216 if (!dir) {
217 struct lnstat_file *lf;
218 /* Old kernel, before /proc/net/stat was introduced */
219 fprintf(stderr, "Your kernel doesn't have lnstat support. ");
220
221 /* we only support rtstat, not multiple files */
222 if (num_req_files >= 2) {
223 fputc('\n', stderr);
224 return NULL;
225 }
226
227 /* we really only accept rt_cache */
228 if (num_req_files && !name_in_array(num_req_files,
229 req_files, "rt_cache")) {
230 fputc('\n', stderr);
231 return NULL;
232 }
233
234 fprintf(stderr, "Fallback to old rtstat-only operation\n");
235
236 lf = alloc_and_open("/proc/net", "rt_cache_stat");
237 if (!lf)
238 return NULL;
239 lf->compat = 1;
240 strncpy(lf->basename, "rt_cache", sizeof(lf->basename));
241
242 /* FIXME: support for old files */
243 if (lnstat_scan_compat_rtstat_fields(lf) < 0)
244 return NULL;
245
246 lf->next = lnstat_files;
247 lnstat_files = lf;
248 return lnstat_files;
249 }
250
251 while ((de = readdir(dir))) {
252 struct lnstat_file *lf;
253
254 if (de->d_type != DT_REG)
255 continue;
256
257 if (num_req_files && !name_in_array(num_req_files,
258 req_files, de->d_name))
259 continue;
260
261 lf = alloc_and_open(path, de->d_name);
b90b773c
SH
262 if (!lf) {
263 closedir(dir);
4677a549 264 return NULL;
b90b773c 265 }
4677a549
SH
266
267 /* fill in field structure */
b90b773c
SH
268 if (lnstat_scan_fields(lf) < 0) {
269 closedir(dir);
4677a549 270 return NULL;
b90b773c 271 }
4677a549
SH
272
273 /* prepend to global list */
274 lf->next = lnstat_files;
275 lnstat_files = lf;
276 }
277 closedir(dir);
278
279 return lnstat_files;
280}
281
282int lnstat_dump(FILE *outfd, struct lnstat_file *lnstat_files)
283{
284 struct lnstat_file *lf;
285
286 for (lf = lnstat_files; lf; lf = lf->next) {
287 int i;
288
289 fprintf(outfd, "%s:\n", lf->path);
290
291 for (i = 0; i < lf->num_fields; i++)
292 fprintf(outfd, "\t%2u: %s\n", i+1, lf->fields[i].name);
293
294 }
295 return 0;
296}
297
298struct lnstat_field *lnstat_find_field(struct lnstat_file *lnstat_files,
299 const char *name)
300{
301 struct lnstat_file *lf;
302 struct lnstat_field *ret = NULL;
303 const char *colon = strchr(name, ':');
304 char *file;
305 const char *field;
306
307 if (colon) {
308 file = strndup(name, colon-name);
309 field = colon+1;
310 } else {
311 file = NULL;
312 field = name;
313 }
ae665a52 314
4677a549
SH
315 for (lf = lnstat_files; lf; lf = lf->next) {
316 int i;
317
318 if (file && strcmp(file, lf->basename))
319 continue;
320
321 for (i = 0; i < lf->num_fields; i++) {
322 if (!strcmp(field, lf->fields[i].name)) {
323 ret = &lf->fields[i];
324 goto out;
325 }
326 }
327 }
328out:
92de1c2c 329 free(file);
4677a549
SH
330
331 return ret;
332}