-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpc.c
More file actions
301 lines (255 loc) · 6.23 KB
/
Copy pathpc.c
File metadata and controls
301 lines (255 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* Packet Counter. */
#include <stdio.h>
#include <stdlib.h>
#include <alloca.h>
#include <assert.h>
#include <string.h>
#include <err.h>
#include <errno.h>
#include <argp.h>
#include <net/if.h> /* if_nametoindex() */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <utils.h>
#include <ebt.h>
/* Argp's global variables. */
const char *argp_program_version = "Packet counter 1.0";
/* Arguments: network interfaces of containers from host's views
* (e.g. 'veth.test1').
*/
static char adoc[] = "IFNAME [IFNAME]";
static char doc[] = "PC -- sample number of packets and their total size "
"in bytes transmitted for a given network stack";
static struct argp_option options[] = {
{"stack", 's', "NET", 0,
"Chose between 'ip' and 'xia' stacks"},
{"add-rules", 'r', 0, 0, "Add ebtables(8) rules"},
{"ebtables", 'e', "FULL-PATH", 0,
"Fully qualified path to ebtables(8)"},
{"sleep", 't', "SECONDS", 0,
"Sleep time between samplings"},
{"parents", 'p', 0, 0,
"Make parent directories as needed"},
{"daemon", 'd', 0, 0,
"Daemonize after creating file"},
{"file", 'f', "FILENAME", 0,
"Fully qualified name of the file to save samplings"},
{ 0 }
};
struct args {
const char *stack;
int add_rules;
const char *ebtables;
int sleep;
int parents;
int daemon;
const char *file;
/* Arguments. */
int count;
int entries;
const char **ifs;
};
static void make_space(struct args *args)
{
size_t bytes;
if (args->entries > args->count)
return;
args->entries = !args->entries ? 1 : 2 * args->entries;
bytes = args->entries * sizeof(*args->ifs);
args->ifs = realloc(args->ifs, bytes);
assert(args->ifs);
}
static void end_args(struct args *args)
{
free(args->ifs);
/* Put it into a consistent state. */
args->count = 0;
args->entries = 0;
args->ifs = NULL;
}
static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
struct args *args = state->input;
switch (key) {
case 's':
args->stack = arg;
if (strcmp(arg, "ip") && strcmp(arg, "xia"))
argp_error(state,
"Stack must be either 'ip', or 'xia'");
break;
case 'r':
args->add_rules = 1;
assert(!arg);
break;
case 'e':
args->ebtables = arg;
break;
case 't':
args->sleep = arg_to_long(state, arg);
if (args->sleep < 1)
argp_error(state, "Sleep period must be >= 1");
break;
case 'p':
args->parents = 1;
assert(!arg);
break;
case 'd':
args->daemon = 1;
assert(!arg);
break;
case 'f':
args->file = arg;
break;
case ARGP_KEY_INIT:
break;
case ARGP_KEY_ARG:
if (!if_nametoindex(arg))
argp_error(state, "Invalid interface `%s'", arg);
make_space(args);
args->ifs[args->count++] = arg;
break;
case ARGP_KEY_END:
if (args->add_rules && args->count < 1)
argp_error(state, "There must be at least one "
"inteface to add");
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = {options, parse_opt, adoc, doc};
static char *skip_slash(char *p)
{
while (*p == '/')
p++;
return p;
}
static int _mkdir_parents(int dd, char *file)
{
char *p = strchr(file, '/');
int new_dd;
if (!p) {
/* There is no folder to create. */
return dd;
}
*p = '\0';
new_dd = openat(dd, file, O_DIRECTORY);
if (new_dd < 0) {
if (errno == ENOENT) {
if (mkdirat(dd, file, 0770))
err(1, "Can't create directory `%s'", file);
new_dd = openat(dd, file, O_DIRECTORY);
}
if (new_dd < 0)
err(1, "Can't open directory `%s'", file);
}
assert(!close(dd));
return _mkdir_parents(new_dd, skip_slash(p + 1));
}
/* Return the directory descriptor where @file should be created. */
static int mkdir_parents(const char *file)
{
char *copy_file;
const char *path;
int dd;
copy_file = alloca(strlen(file) + 1);
assert(copy_file);
strcpy(copy_file, file);
path = (copy_file[0] == '/') ? "/" : ".";
dd = open(path, O_DIRECTORY);
if (dd < 0)
err(1, "Can't open directory `%s'", path);
return _mkdir_parents(dd, skip_slash(copy_file));
}
int main(int argc, char **argv)
{
struct args args = {
/* Defaults. */
.stack = "ip",
.add_rules = 0,
.ebtables = "/sbin/ebtables",
.sleep = 10,
.parents = 0,
.daemon = 0,
/* This parameter expects something like
* "experiment/stack/column/run".
*/
.file = NULL,
.count = 0,
.entries = 0,
.ifs = NULL,
};
int i, sk, rcount;
FILE *f;
double start;
struct ebt_counter *cnt;
/* Read parameters. */
argp_parse(&argp, argc, argv, 0, NULL, &args);
/* Install ebtables(8) rules. */
if (args.add_rules) {
for (i = 0; i < args.count; i++)
ebt_add_rule(args.ebtables, args.stack, args.ifs[i]);
}
/* Create parent paths of @args.file. */
if (args.file && args.parents)
assert(!close(mkdir_parents(args.file)));
/* Test that only the expected ebtables(8) rules are in place.
* This is important to avoid silently wrong measurements.
*/
sk = ebt_socket();
if (sk < 0)
err(1, "Can't get a socket");
rcount = ebt_rule_count(sk, args.stack);
if (rcount != args.count)
errx(1, "There is a mismatch between the number of ebtables(8) rules installed (= %i) and the number of monitored interfaces (= %i)",
rcount, args.count);
if (!rcount)
errx(1, "There is no ebtables(8) rules to measure");
/* Create sampling file. */
if (args.file) {
f = fopen(args.file, "w");
if (!f)
err(1, "Can't open file `%s'", args.file);
ebt_add_header_to_file(sk, args.stack, f);
} else {
f = stdout;
}
/* Daemonize. */
if (args.daemon && daemon(1, 1))
err(1, "Can't daemonize");
start = now();
if (args.file) {
ebt_write_sample_to_file(sk, args.stack, f);
if (fflush(f))
err(1, "Can't save content of file `%s'", args.file);
} else {
cnt = ebt_create_cnt(sk, args.stack);
assert(cnt);
}
while (1) {
double diff = now() - start;
if (diff < args.sleep)
nsleep(args.sleep - diff);
else
warnx("Option --sleep=%i is too little; not enough time to estimate rates. Consider increasing the period",
args.sleep);
start = now();
if (args.file)
ebt_write_sample_to_file(sk, args.stack, f);
else
ebt_write_rates_to_file(sk, args.stack, f, args.sleep,
cnt);
if (fflush(f))
err(1, "Can't save content of file `%s'",
args.file ? args.file : "STDOUT");
}
ebt_free_cnt(cnt);
if (args.file)
assert(!fclose(f));
ebt_close(sk);
end_args(&args);
return 0;
}