-
Notifications
You must be signed in to change notification settings - Fork 126
/
host.c
314 lines (266 loc) · 6.63 KB
/
host.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
/*
* Copyright (c) 2013-2015 Erik Ekman <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose
* with or without fee is hereby granted, provided that the above copyright notice
* and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include "host.h"
#include "net.h"
#include "chunk.h"
#include <time.h>
#include <assert.h>
#ifndef CLOCK_MONOTONIC_RAW
#define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
#endif
#include <sys/param.h>
struct linked_gaicb {
struct gaicb gaicb;
struct linked_gaicb *next;
};
struct eval_host {
struct host *host;
struct timespec sendtime;
uint16_t cur_seqno;
uint16_t id;
uint8_t *payload;
size_t payload_len;
int done;
int num_tx;
int num_rx;
};
static const struct addrinfo addr_request = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_RAW,
};
int host_make_resolvlist(FILE *file, struct gaicb **list[])
{
int hosts = 0;
int i;
struct gaicb **l;
struct linked_gaicb *head = NULL;
struct linked_gaicb *tail = NULL;
struct linked_gaicb *lg;
for (;;) {
char hostname[300];
int res;
memset(hostname, 0, sizeof(hostname));
res = fscanf(file, "%256s", hostname);
if (res == EOF) break;
lg = calloc(1, sizeof(*lg));
if (!lg)
return 0;
lg->gaicb.ar_name = strndup(hostname, strlen(hostname));
lg->gaicb.ar_request = &addr_request;
if (!head) head = lg;
if (tail) tail->next = lg;
tail = lg;
hosts++;
}
l = calloc(hosts, sizeof(struct gaicb*));
if (!l)
return 0;
lg = head;
for (i = 0; i < hosts; i++) {
l[i] = &lg->gaicb;
lg = lg->next;
}
*list = l;
return hosts;
}
void host_free_resolvlist(struct gaicb *list[], int length)
{
int i;
for (i = 0; i < length; i++) {
free((char *) list[i]->ar_name);
if (list[i]->ar_result) freeaddrinfo(list[i]->ar_result);
free(list[i]);
}
free(list);
}
struct host *host_create(struct gaicb *list[], int listlength)
{
struct host *hosts = NULL;
struct host *last = NULL;
int i;
for (i = 0; i < listlength; i++) {
if (gai_error(list[i]) == 0) {
struct addrinfo *result = list[i]->ar_result;
while (result) {
struct host *h;
h = calloc(1, sizeof(struct host));
if (!h)
return NULL;
memcpy(&h->sockaddr, result->ai_addr, result->ai_addrlen);
h->sockaddr_len = result->ai_addrlen;
if (!hosts)
hosts = h;
if (last)
last->next = h;
last = h;
result = result->ai_next;
}
}
}
return hosts;
}
static uint64_t latency_sum_us;
static uint32_t latency_count;
static void diff_add(struct timespec *start, struct timespec *end)
{
uint64_t us = (end->tv_sec - start->tv_sec) * 1000000;
us -= start->tv_nsec / 1000;
us += end->tv_nsec / 1000;
latency_sum_us += us;
latency_count++;
}
struct evaldata {
struct eval_host *hosts;
int count;
};
static void eval_reply(void *userdata, struct sockaddr_storage *addr,
size_t addrlen, uint16_t id, uint16_t seqno, uint8_t **data, size_t len)
{
int i;
struct evaldata *eval = (struct evaldata *) userdata;
struct timespec recvtime;
clock_gettime(CLOCK_MONOTONIC_RAW, &recvtime);
for (i = 0; i < eval->count; i++) {
struct eval_host *eh = &eval->hosts[i];
if (addrlen == eh->host->sockaddr_len &&
memcmp(addr, &eh->host->sockaddr, addrlen) == 0 &&
eh->payload_len == len &&
memcmp(*data, eh->payload, eh->payload_len) == 0 &&
eh->id == id &&
eh->cur_seqno == seqno) {
/* Store accepted reply */
eh->num_rx++;
eh->done = 1;
net_inc_rx(eh->payload_len);
/* Use new seqno for next packet */
eh->cur_seqno++;
diff_add(&eh->sendtime, &recvtime);
break;
}
}
}
int host_evaluate(struct host **hosts, int length, int timeout)
{
int i;
int addr;
int good_hosts;
struct host *host;
struct host *prev;
struct evaldata evaldata;
uint8_t eval_payload[CHUNK_SIZE];
evaldata.count = length;
evaldata.hosts = calloc(length, sizeof(struct eval_host));
if (!evaldata.hosts)
return 0;
for (i = 0; i < sizeof(eval_payload); i++) {
eval_payload[i] = i & 0xff;
}
addr = 0;
host = *hosts;
for (i = 0; i < length; i++) {
evaldata.hosts[addr].host = host;
evaldata.hosts[addr].id = addr;
evaldata.hosts[addr].cur_seqno = addr * 2;
evaldata.hosts[addr].payload = eval_payload;
evaldata.hosts[addr].payload_len = sizeof(eval_payload);
addr++;
host = host->next;
}
printf("Evaluating %d hosts (timeout=%ds).", length, timeout);
for (i = 0; i < 5; i++) {
int h;
struct timeval tv;
printf(".");
fflush(stdout);
for (h = 0; h < length; h++) {
struct eval_host *eh = &evaldata.hosts[h];
clock_gettime(CLOCK_MONOTONIC_RAW, &eh->sendtime);
eh->done = 0;
eh->num_tx++;
net_send(eh->host, eh->id, eh->cur_seqno,
eh->payload, eh->payload_len);
}
tv.tv_sec = timeout;
tv.tv_usec = 0;
for (;;) {
int alldone = 1;
int res;
for (h = 0; h < length; h++) {
alldone &= evaldata.hosts[h].done;
}
if (alldone) /* All hosts have replied */
break;
res = net_recv(&tv, eval_reply, &evaldata);
if (!res) /* Timeout, give up */
break;
}
}
printf(" done.\n");
good_hosts = 0;
for (i = 0; i < length; i++) {
struct eval_host *eh = &evaldata.hosts[i];
/* Filter out hosts with below 100% result */
if (eh->num_tx == 0 ||
eh->num_tx != eh->num_rx) {
/* Mark host for deletion */
eh->host->sockaddr_len = 0;
} else {
good_hosts++;
}
}
host = *hosts;
prev = NULL;
while (host) {
struct host *next = host->next;
/* Remove bad hosts from list */
if (host->sockaddr_len == 0) {
if (host == *hosts)
*hosts = next;
if (prev)
prev->next = next;
free(host);
} else {
prev = host;
}
host = next;
}
free(evaldata.hosts);
printf("%d of %d hosts responded correctly to all pings", good_hosts, length);
if (good_hosts) {
printf(" (average RTT %.02f ms)",
latency_sum_us / ( latency_count * 1000.0f));
}
printf("\n");
return good_hosts;
}
static struct host *hosts_start;
static struct host *hosts_cur;
void host_use(struct host* hosts)
{
hosts_start = hosts;
}
/* Return next host.
* Emulate a cyclic list of hosts */
struct host *host_get_next()
{
struct host *h;
assert(hosts_start);
if (!hosts_cur)
hosts_cur = hosts_start;
h = hosts_cur;
hosts_cur = hosts_cur->next;
return h;
}