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{
41	int j, num_lines = 0;
42
43	for (j = 0; j < lf->num_fields; j++)
44		lf->fields[j].values[i] = 0;
45
46	while(!feof(lf->fp)) {
47		char buf[FGETS_BUF_SIZE];
48		char *ptr = buf;
49
50		num_lines++;
51
52		fgets(buf, sizeof(buf)-1, lf->fp);
53		gettimeofday(&lf->last_read, NULL);
54
55		for (j = 0; j < lf->num_fields; j++) {
56			unsigned long f = strtoul(ptr, &ptr, 16);
57			if (j == 0)
58				lf->fields[j].values[i] = f;
59			else
60				lf->fields[j].values[i] += f;
61		}
62	}
63	return num_lines;
64}
65
66static int time_after(struct timeval *last,
67		      struct timeval *tout,
68		      struct timeval *now)
69{
70	if (now->tv_sec > last->tv_sec + tout->tv_sec)
71		return 1;
72
73	if (now->tv_sec == last->tv_sec + tout->tv_sec) {
74		if (now->tv_usec > last->tv_usec + tout->tv_usec)
75			return 1;
76	}
77
78	return 0;
79}
80
81int lnstat_update(struct lnstat_file *lnstat_files)
82{
83	struct lnstat_file *lf;
84	char buf[FGETS_BUF_SIZE];
85	struct timeval tv;
86
87	gettimeofday(&tv, NULL);
88
89	for (lf = lnstat_files; lf; lf = lf->next) {
90		if (time_after(&lf->last_read, &lf->interval, &tv)) {
91			int i;
92			struct lnstat_field *lfi;
93
94			rewind(lf->fp);
95			if (!lf->compat) {
96				/* skip first line */
97				fgets(buf, sizeof(buf)-1, lf->fp);
98			}
99			scan_lines(lf, 1);
100
101			for (i = 0, lfi = &lf->fields[i];
102			     i < lf->num_fields; i++, lfi = &lf->fields[i]) {
103				if (i == 0)
104					lfi->result = lfi->values[1];
105				else
106					lfi->result = (lfi->values[1]-lfi->values[0])
107				    			/ lf->interval.tv_sec;
108			}
109
110			rewind(lf->fp);
111			fgets(buf, sizeof(buf)-1, lf->fp);
112			scan_lines(lf, 0);
113		}
114	}
115
116	return 0;
117}
118
119/* scan first template line and fill in per-field data structures */
120static int __lnstat_scan_fields(struct lnstat_file *lf, char *buf)
121{
122	char *tok;
123	int i;
124
125	tok = strtok(buf, " \t\n");
126	for (i = 0; i < LNSTAT_MAX_FIELDS_PER_LINE; i++) {
127		lf->fields[i].file = lf;
128		strncpy(lf->fields[i].name, tok, LNSTAT_MAX_FIELD_NAME_LEN);
129		/* has to be null-terminate since we initialize to zero
130		 * and field size is NAME_LEN + 1 */
131		tok = strtok(NULL, " \t\n");
132		if (!tok) {
133			lf->num_fields = i+1;
134			return 0;
135		}
136	}
137	return 0;
138}
139
140static int lnstat_scan_fields(struct lnstat_file *lf)
141{
142	char buf[FGETS_BUF_SIZE];
143
144	rewind(lf->fp);
145	fgets(buf, sizeof(buf)-1, lf->fp);
146
147	return __lnstat_scan_fields(lf, buf);
148}
149
150/* fake function emulating lnstat_scan_fields() for old kernels */
151static int lnstat_scan_compat_rtstat_fields(struct lnstat_file *lf)
152{
153	char buf[FGETS_BUF_SIZE];
154
155	strncpy(buf, RTSTAT_COMPAT_LINE, sizeof(buf)-1);
156
157	return __lnstat_scan_fields(lf, buf);
158}
159
160/* find out whether string 'name; is in given string array */
161static int name_in_array(const int num, const char **arr, const char *name)
162{
163	int i;
164	for (i = 0; i < num; i++) {
165		if (!strcmp(arr[i], name))
166			return 1;
167	}
168	return 0;
169}
170
171/* allocate lnstat_file and open given file */
172static struct lnstat_file *alloc_and_open(const char *path, const char *file)
173{
174	struct lnstat_file *lf;
175
176	/* allocate */
177	lf = malloc(sizeof(*lf));
178	if (!lf)
179		return NULL;
180
181	/* initialize */
182	memset(lf, 0, sizeof(*lf));
183
184	/* de->d_name is guaranteed to be <= NAME_MAX */
185	strcpy(lf->basename, file);
186	strcpy(lf->path, path);
187	strcat(lf->path, "/");
188	strcat(lf->path, lf->basename);
189
190	/* initialize to default */
191	lf->interval.tv_sec = 1;
192
193	/* open */
194	lf->fp = fopen(lf->path, "r");
195	if (!lf->fp) {
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;
214
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);
262		if (!lf)
263			return NULL;
264
265		/* fill in field structure */
266		if (lnstat_scan_fields(lf) < 0)
267			return NULL;
268
269		/* prepend to global list */
270		lf->next = lnstat_files;
271		lnstat_files = lf;
272	}
273	closedir(dir);
274
275	return lnstat_files;
276}
277
278int lnstat_dump(FILE *outfd, struct lnstat_file *lnstat_files)
279{
280	struct lnstat_file *lf;
281
282	for (lf = lnstat_files; lf; lf = lf->next) {
283		int i;
284
285		fprintf(outfd, "%s:\n", lf->path);
286
287		for (i = 0; i < lf->num_fields; i++)
288			fprintf(outfd, "\t%2u: %s\n", i+1, lf->fields[i].name);
289
290	}
291	return 0;
292}
293
294struct lnstat_field *lnstat_find_field(struct lnstat_file *lnstat_files,
295				       const char *name)
296{
297	struct lnstat_file *lf;
298	struct lnstat_field *ret = NULL;
299	const char *colon = strchr(name, ':');
300	char *file;
301	const char *field;
302
303	if (colon) {
304		file = strndup(name, colon-name);
305		field = colon+1;
306	} else {
307		file = NULL;
308		field = name;
309	}
310
311	for (lf = lnstat_files; lf; lf = lf->next) {
312		int i;
313
314		if (file && strcmp(file, lf->basename))
315			continue;
316
317		for (i = 0; i < lf->num_fields; i++) {
318			if (!strcmp(field, lf->fields[i].name)) {
319				ret = &lf->fields[i];
320				goto out;
321			}
322		}
323	}
324out:
325	if (file)
326		free(file);
327
328	return ret;
329}
330