/
pw.c
358 lines (319 loc) · 8.01 KB
/
pw.c
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* Packet writer. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <argp.h>
#include <math.h>
#include <utils.h>
#include <seeds.h>
#include <rdist.h>
#include <strarray.h>
#include <sndpkt.h>
/* Argp's global variables. */
const char *argp_program_version = "Packet writer 1.0";
static char doc[] = "PW -- packet generator for evaluation of "
"fowarding performance of routers";
static struct argp_option options[] = {
{"prefix", 'p', "FILE", 0, "Name of prefix file"},
{"prefix-limit", 'x', "N", 0,
"Consider only the first N entries of the prefix file *after* shuffling it"},
{"zipf", 'z', "EXP", 0, "Parameter s of Zipf distribution"},
{"stack", 's', "NET", 0,
"Chose between 'ip' and 'xia' stacks"},
{"ifname", 'i', "IF", 0,
"Network interface to send packets (e.g. 'eth0')"},
{"dmac", 'm', "MAC", 0,
"Ethernet address of router (e.g. '11:22:33:44:55:66')"},
{"daddr-type", 't', "TYPE", 0,
"Type of the destination address template {'ip', 'fb0', "
"'fb1', 'fb2', 'fb3', 'via'}"},
{"pkt-len", 'l', "LEN", 0, "Packet lenght in bytes"},
{"nnodes", 'n', "COUNT", 0,
"Number of nodes (= number of ports + 1)"},
{"node-id", 'd', "ID", 0,
"ID of this packet writer [1..(N-1)]"},
{"run", 'r', "RUN", 0, "Run must be >= 1"},
{"interactive", 'v', NULL, 0,
"Allow one to interactively control the number of packets sent"
},
{ 0 }
};
struct args {
const char *prefix_filename;
uint64_t prefix_limit;
double s;
const char *stack;
const char *ifname;
unsigned char dst_mac[32];
int dst_mac_len;
const char *dst_addr_type;
int packet_len;
int nnodes;
int node_id;
int run;
int interactive;
};
/* XXX Copied from xiaconf/xip/utils.c. This function should go to
* an XIA library.
*/
static inline int hexd_to_val(int ch)
{
if ('0' <= ch && ch <= '9')
return ch - '0' + 0;
if ('A' <= ch && ch <= 'F')
return ch - 'A' + 10;
if ('a' <= ch && ch <= 'f')
return ch - 'a' + 10;
return -1;
}
/* XXX Copied from xiaconf/xip/utils.c. This function should go to
* an XIA library.
*/
static int lladdr_pton(const char *str, unsigned char *lladdr, int alen)
{
const char *p = str; /* String cursor. */
int v; /* Temporary value. */
int octet; /* The octet being unconvered. */
int digit = 0; /* State of the machine. */
int count = 0; /* Number of octets pushed into @lladdr. */
while (*p && alen > 0) {
switch (digit) {
case 0:
/* We expect a hexdigit. */
v = hexd_to_val(*p);
if (v < 0)
return -1;
octet = v;
digit++;
break;
case 1:
/* We expect a hexdigit or ':'. */
if (*p == ':') {
*(lladdr++) = octet;
alen--;
count++;
} else {
v = hexd_to_val(*p);
if (v < 0)
return -1;
octet = (octet << 4) + v;
}
digit++;
break;
case 2:
/* We expect a ':'. */
if (*p == ':') {
*(lladdr++) = octet;
alen--;
count++;
} else
return -1;
digit = 0;
break;
default:
return -1;
}
p++;
}
/* The tests read as follows:
* 1. String isn't fully parsed.
* 2. There's a more octet to add, but @lladdr is full.
* 3. No octet was found.
*/
if (*p || (digit && alen <= 0) || (!digit && !count))
return -1;
if (digit) {
assert(alen > 0);
*lladdr = octet;
count++;
}
return count;
}
static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
struct args *args = state->input;
switch (key) {
case 'p':
args->prefix_filename = arg;
break;
case 'x':
args->prefix_limit = arg_to_long(state, arg);
if (args->prefix_limit < 1)
argp_error(state, "Prefix limit must be >= 1");
break;
case 'z': {
char *end;
args->s = strtod(arg, &end);
if (!*arg || *end)
argp_error(state, "'%s' is not a float", arg);
if (args->s < 0 || args->s == NAN || args->s == INFINITY)
argp_error(state,"Zipf must be >= 0");
break;
}
case 's':
args->stack = arg;
if (strcmp(arg, "ip") && strcmp(arg, "xia"))
argp_error(state,
"Stack must be either 'ip', or 'xia'");
break;
case 'i':
args->ifname = arg;
break;
case 'm':
args->dst_mac_len = lladdr_pton(arg, args->dst_mac,
sizeof(args->dst_mac));
if (args->dst_mac_len < 1)
argp_error(state,
"'%s' is not a valid Ethernet address", arg);
break;
case 't':
args->dst_addr_type = arg;
if (strcmp(arg, "ip") && strcmp(arg, "fb0") &&
strcmp(arg, "fb1") && strcmp(arg, "fb2") &&
strcmp(arg, "fb3") && strcmp(arg, "via"))
argp_error(state, "'%s' is not a valid type", arg);
break;
case 'l':
args->packet_len = arg_to_long(state, arg);
if (args->packet_len < 1)
argp_error(state, "Packet lenght must be >= 1");
break;
case 'n':
args->nnodes = arg_to_long(state, arg);
if (args->nnodes < 2)
argp_error(state, "Number of nodes must be >= 2");
break;
case 'd':
args->node_id = arg_to_long(state, arg);
if (args->node_id < 1)
argp_error(state, "Node ID must be >= 1");
break;
case 'r':
args->run = arg_to_long(state, arg);
if (args->run < 1)
argp_error(state,"Run must be >= 1");
break;
case 'v':
args->interactive = 1;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = {options, parse_opt, NULL, doc};
static void drop_line(void)
{
int ch;
do {
ch = getchar();
if (ch == EOF)
exit(0);
} while (ch != '\n');
}
static int ask_count(void)
{
int read, n;
do {
char tail;
printf(">");
read = scanf("%i%c", &n, &tail);
if (!read || tail != '\n') {
drop_line(); /* Drop uninterpreted chars. */
printf("Please enter the number of packets to send\n");
read = 0;
} else if (n <= 0) {
printf("Please enter only positive numbers\n");
read = 0;
}
} while (!read);
return n;
}
int main(int argc, char **argv)
{
struct args args = {
/* Defaults. */
.prefix_filename = "prefix",
.prefix_limit = 0,
.s = 1.0,
.stack = "ip",
.ifname = "eth0",
.dst_mac = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
.dst_mac_len = 6,
.dst_addr_type = "ip",
.packet_len = 64,
.nnodes = 3,
.node_id = 1,
.run = 1,
.interactive = 0,
};
struct seed s1, s2, node_seed;
struct net_prefix *prefixes;
uint64_t prefixes_count;
struct zipf_cache zcache;
struct sndpkt_engine engine;
double start, diff, count, to_send;
long index;
/* Read parameters. */
argp_parse(&argp, argc, argv, 0, NULL, &args);
/* Load seeds. */
load_seeds(args.run, args.nnodes, args.node_id, &s1, &s2, &node_seed);
/*
print_seed("s1", &s1);
print_seed("s2", &s2);
print_seed("node_seed", &node_seed);
*/
/* PW does not use seed @s2. */
/* Load and shuffle destination addresses. */
prefixes = load_file_as_shuffled_addrs(args.prefix_filename,
&prefixes_count, s1.seeds, SEED_UINT32_N, 1);
if (!prefixes_count)
err(1, "Prefix file `%s' is empty", args.prefix_filename);
if (args.prefix_limit) {
if (args.prefix_limit > prefixes_count)
err(1, "Option --prefix-limit=%" PRIu64 " "
"is larger than the number of entries in "
"the prefix file `%s' (= %" PRIu64 ")",
args.prefix_limit, args.prefix_filename,
prefixes_count);
prefixes_count = args.prefix_limit;
}
/* Cache Zipf sampling. */
printf_fsh("Initializing Zipf cache... ");
init_zipf_cache(&zcache, prefixes_count * 30, args.s, prefixes_count,
node_seed.seeds, SEED_UINT32_N);
printf_fsh("DONE\n");
/*
print_zipf_cache(&zcache);
*/
/* Sample destinations and send packets out. */
init_sndpkt_engine(&engine, args.stack, args.ifname, args.packet_len,
args.dst_mac, args.dst_mac_len, args.dst_addr_type);
index = sample_zipf_cache(&zcache);
count = 0.0;
to_send = args.interactive ? ask_count() : 0.0;
start = now();
while (1) {
if (!sndpkt_send(&engine, &prefixes[index - 1].addr))
continue; /* No packet sent. */
index = sample_zipf_cache(&zcache);
count++;
if (!args.interactive) {
diff = now() - start;
if (diff >= 10.0) {
printf_fsh("%.1f pps\n", count / diff);
count = 0.0;
start = now();
}
} else {
printf("Packet %.0f sent\n", count);
if (count >= to_send)
to_send = count + ask_count();
}
}
end_sndpkt_engine(&engine);
end_zipf_cache(&zcache);
free_net_prefix(prefixes);
return 0;
}