| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* lnstat.c: Unified linux network statistics |
| 3 |
* |
| 4 |
* Copyright (C) 2004 by Harald Welte <[email protected]> |
| 5 |
* |
| 6 |
* Development of this code was funded by Astaro AG, http://www.astaro.com/ |
| 7 |
* |
| 8 |
* Based on original concept and ideas from predecessor rtstat.c: |
| 9 |
* |
| 10 |
* Copyright 2001 by Robert Olsson <[email protected]> |
| 11 |
* Uppsala University, Sweden |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <unistd.h> |
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
#include <dirent.h> |
| 19 |
#include <limits.h> |
| 20 |
#include <time.h> |
| 21 |
|
| 22 |
#include <sys/time.h> |
| 23 |
#include <sys/types.h> |
| 24 |
|
| 25 |
#include "lnstat.h" |
| 26 |
|
| 27 |
/* size of temp buffer used to read lines from procfiles */ |
| 28 |
#define FGETS_BUF_SIZE 1024 |
| 29 |
|
| 30 |
|
| 31 |
#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" |
| 32 |
|
| 33 |
/* Read (and summarize for SMP) the different stats vars. */ |
| 34 |
static int scan_lines(struct lnstat_file *lf, int i) |
| 35 |
{ |
| 36 |
char buf[FGETS_BUF_SIZE]; |
| 37 |
int j, num_lines = 0; |
| 38 |
|
| 39 |
for (j = 0; j < lf->num_fields; j++) |
| 40 |
lf->fields[j].values[i] = 0; |
| 41 |
|
| 42 |
rewind(lf->fp); |
| 43 |
/* skip first line */ |
| 44 |
if (!lf->compat && !fgets(buf, sizeof(buf)-1, lf->fp)) |
| 45 |
return -1; |
| 46 |
|
| 47 |
while (!feof(lf->fp) && fgets(buf, sizeof(buf)-1, lf->fp)) { |
| 48 |
char *ptr = buf; |
| 49 |
|
| 50 |
num_lines++; |
| 51 |
|
| 52 |
gettimeofday(&lf->last_read, NULL); |
| 53 |
|
| 54 |
for (j = 0; j < lf->num_fields; j++) { |
| 55 |
unsigned long f = strtoul(ptr, &ptr, 16); |
| 56 |
|
| 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 |
|
| 66 |
static 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 |
|
| 81 |
int lnstat_update(struct lnstat_file *lnstat_files) |
| 82 |
{ |
| 83 |
struct lnstat_file *lf; |
| 84 |
struct timeval tv; |
| 85 |
|
| 86 |
gettimeofday(&tv, NULL); |
| 87 |
|
| 88 |
for (lf = lnstat_files; lf; lf = lf->next) { |
| 89 |
if (time_after(&lf->last_read, &lf->interval, &tv)) { |
| 90 |
int i; |
| 91 |
struct lnstat_field *lfi; |
| 92 |
|
| 93 |
scan_lines(lf, 1); |
| 94 |
|
| 95 |
for (i = 0, lfi = &lf->fields[i]; |
| 96 |
i < lf->num_fields; i++, lfi = &lf->fields[i]) { |
| 97 |
if (i == 0) |
| 98 |
lfi->result = lfi->values[1]; |
| 99 |
else |
| 100 |
lfi->result = (lfi->values[1]-lfi->values[0]) |
| 101 |
/ lf->interval.tv_sec; |
| 102 |
} |
| 103 |
|
| 104 |
scan_lines(lf, 0); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
/* scan first template line and fill in per-field data structures */ |
| 112 |
static int __lnstat_scan_fields(struct lnstat_file *lf, char *buf) |
| 113 |
{ |
| 114 |
char *tok; |
| 115 |
int i; |
| 116 |
|
| 117 |
tok = strtok(buf, " \t\n"); |
| 118 |
for (i = 0; i < LNSTAT_MAX_FIELDS_PER_LINE; i++) { |
| 119 |
lf->fields[i].file = lf; |
| 120 |
strncpy(lf->fields[i].name, tok, LNSTAT_MAX_FIELD_NAME_LEN); |
| 121 |
/* has to be null-terminate since we initialize to zero |
| 122 |
* and field size is NAME_LEN + 1 */ |
| 123 |
tok = strtok(NULL, " \t\n"); |
| 124 |
if (!tok) { |
| 125 |
lf->num_fields = i+1; |
| 126 |
return 0; |
| 127 |
} |
| 128 |
} |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
static int lnstat_scan_fields(struct lnstat_file *lf) |
| 133 |
{ |
| 134 |
char buf[FGETS_BUF_SIZE]; |
| 135 |
|
| 136 |
rewind(lf->fp); |
| 137 |
if (!fgets(buf, sizeof(buf)-1, lf->fp)) |
| 138 |
return -1; |
| 139 |
|
| 140 |
return __lnstat_scan_fields(lf, buf); |
| 141 |
} |
| 142 |
|
| 143 |
/* fake function emulating lnstat_scan_fields() for old kernels */ |
| 144 |
static int lnstat_scan_compat_rtstat_fields(struct lnstat_file *lf) |
| 145 |
{ |
| 146 |
char buf[FGETS_BUF_SIZE]; |
| 147 |
|
| 148 |
strncpy(buf, RTSTAT_COMPAT_LINE, sizeof(buf) - 1); |
| 149 |
buf[sizeof(buf) - 1] = '\0'; |
| 150 |
|
| 151 |
return __lnstat_scan_fields(lf, buf); |
| 152 |
} |
| 153 |
|
| 154 |
/* find out whether string 'name; is in given string array */ |
| 155 |
static int name_in_array(const int num, const char **arr, const char *name) |
| 156 |
{ |
| 157 |
int i; |
| 158 |
|
| 159 |
for (i = 0; i < num; i++) { |
| 160 |
if (!strcmp(arr[i], name)) |
| 161 |
return 1; |
| 162 |
} |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
/* allocate lnstat_file and open given file */ |
| 167 |
static struct lnstat_file *alloc_and_open(const char *path, const char *file) |
| 168 |
{ |
| 169 |
struct lnstat_file *lf; |
| 170 |
|
| 171 |
/* allocate */ |
| 172 |
lf = calloc(1, sizeof(*lf)); |
| 173 |
if (!lf) { |
| 174 |
fprintf(stderr, "out of memory\n"); |
| 175 |
return NULL; |
| 176 |
} |
| 177 |
|
| 178 |
/* initialize */ |
| 179 |
snprintf(lf->basename, sizeof(lf->basename), "%s", file); |
| 180 |
snprintf(lf->path, sizeof(lf->path), "%s/%s", path, file); |
| 181 |
|
| 182 |
/* initialize to default */ |
| 183 |
lf->interval.tv_sec = 1; |
| 184 |
|
| 185 |
/* open */ |
| 186 |
lf->fp = fopen(lf->path, "r"); |
| 187 |
if (!lf->fp) { |
| 188 |
perror(lf->path); |
| 189 |
free(lf); |
| 190 |
return NULL; |
| 191 |
} |
| 192 |
|
| 193 |
return lf; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
/* lnstat_scan_dir - find and parse all available statistics files/fields */ |
| 198 |
struct lnstat_file *lnstat_scan_dir(const char *path, const int num_req_files, |
| 199 |
const char **req_files) |
| 200 |
{ |
| 201 |
DIR *dir; |
| 202 |
struct lnstat_file *lnstat_files = NULL; |
| 203 |
struct dirent *de; |
| 204 |
|
| 205 |
if (!path) |
| 206 |
path = PROC_NET_STAT; |
| 207 |
|
| 208 |
dir = opendir(path); |
| 209 |
if (!dir) { |
| 210 |
struct lnstat_file *lf; |
| 211 |
/* Old kernel, before /proc/net/stat was introduced */ |
| 212 |
fprintf(stderr, "Your kernel doesn't have lnstat support. "); |
| 213 |
|
| 214 |
/* we only support rtstat, not multiple files */ |
| 215 |
if (num_req_files >= 2) { |
| 216 |
fputc('\n', stderr); |
| 217 |
return NULL; |
| 218 |
} |
| 219 |
|
| 220 |
/* we really only accept rt_cache */ |
| 221 |
if (num_req_files && !name_in_array(num_req_files, |
| 222 |
req_files, "rt_cache")) { |
| 223 |
fputc('\n', stderr); |
| 224 |
return NULL; |
| 225 |
} |
| 226 |
|
| 227 |
fprintf(stderr, "Fallback to old rtstat-only operation\n"); |
| 228 |
|
| 229 |
lf = alloc_and_open("/proc/net", "rt_cache_stat"); |
| 230 |
if (!lf) |
| 231 |
return NULL; |
| 232 |
lf->compat = 1; |
| 233 |
strncpy(lf->basename, "rt_cache", sizeof(lf->basename)); |
| 234 |
|
| 235 |
/* FIXME: support for old files */ |
| 236 |
if (lnstat_scan_compat_rtstat_fields(lf) < 0) |
| 237 |
return NULL; |
| 238 |
|
| 239 |
lf->next = lnstat_files; |
| 240 |
lnstat_files = lf; |
| 241 |
return lnstat_files; |
| 242 |
} |
| 243 |
|
| 244 |
while ((de = readdir(dir))) { |
| 245 |
struct lnstat_file *lf; |
| 246 |
|
| 247 |
if (de->d_type != DT_REG) |
| 248 |
continue; |
| 249 |
|
| 250 |
if (num_req_files && !name_in_array(num_req_files, |
| 251 |
req_files, de->d_name)) |
| 252 |
continue; |
| 253 |
|
| 254 |
lf = alloc_and_open(path, de->d_name); |
| 255 |
if (!lf) { |
| 256 |
closedir(dir); |
| 257 |
return NULL; |
| 258 |
} |
| 259 |
|
| 260 |
/* fill in field structure */ |
| 261 |
if (lnstat_scan_fields(lf) < 0) { |
| 262 |
closedir(dir); |
| 263 |
return NULL; |
| 264 |
} |
| 265 |
|
| 266 |
/* prepend to global list */ |
| 267 |
lf->next = lnstat_files; |
| 268 |
lnstat_files = lf; |
| 269 |
} |
| 270 |
closedir(dir); |
| 271 |
|
| 272 |
return lnstat_files; |
| 273 |
} |
| 274 |
|
| 275 |
int lnstat_dump(FILE *outfd, struct lnstat_file *lnstat_files) |
| 276 |
{ |
| 277 |
struct lnstat_file *lf; |
| 278 |
|
| 279 |
for (lf = lnstat_files; lf; lf = lf->next) { |
| 280 |
int i; |
| 281 |
|
| 282 |
fprintf(outfd, "%s:\n", lf->path); |
| 283 |
|
| 284 |
for (i = 0; i < lf->num_fields; i++) |
| 285 |
fprintf(outfd, "\t%2u: %s\n", i+1, lf->fields[i].name); |
| 286 |
|
| 287 |
} |
| 288 |
return 0; |
| 289 |
} |
| 290 |
|
| 291 |
struct lnstat_field *lnstat_find_field(struct lnstat_file *lnstat_files, |
| 292 |
const char *name) |
| 293 |
{ |
| 294 |
struct lnstat_file *lf; |
| 295 |
struct lnstat_field *ret = NULL; |
| 296 |
const char *colon = strchr(name, ':'); |
| 297 |
char *file; |
| 298 |
const char *field; |
| 299 |
|
| 300 |
if (colon) { |
| 301 |
file = strndup(name, colon-name); |
| 302 |
field = colon+1; |
| 303 |
} else { |
| 304 |
file = NULL; |
| 305 |
field = name; |
| 306 |
} |
| 307 |
|
| 308 |
for (lf = lnstat_files; lf; lf = lf->next) { |
| 309 |
int i; |
| 310 |
|
| 311 |
if (file && strcmp(file, lf->basename)) |
| 312 |
continue; |
| 313 |
|
| 314 |
for (i = 0; i < lf->num_fields; i++) { |
| 315 |
if (!strcmp(field, lf->fields[i].name)) { |
| 316 |
ret = &lf->fields[i]; |
| 317 |
goto out; |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
out: |
| 322 |
free(file); |
| 323 |
|
| 324 |
return ret; |
| 325 |
} |