-
Notifications
You must be signed in to change notification settings - Fork 4
/
proto.c
471 lines (371 loc) · 8.7 KB
/
proto.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright Meta Platforms, Inc. and affiliates */
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sysinfo.h>
#include <ccan/err/err.h>
#include "proto.h"
static const unsigned int proto_ver =
__KPM_MSG_TOTAL << 24 |
sizeof(struct kpm_test) << 16 |
sizeof(struct kpm_test_results);
void *kpm_msg_dup(struct kpm_header *hdr)
{
char *msg;
msg = malloc(hdr->len);
memcpy(msg, hdr, hdr->len);
return msg;
}
void *kpm_receive(int fd)
{
struct kpm_header hdr;
ssize_t off, n;
char *msg;
n = recv(fd, &hdr, sizeof(hdr), MSG_PEEK | MSG_WAITALL);
if (n < (int)sizeof(hdr)) {
if (n)
warn("Failed to receive header (%zd)", n);
return NULL;
}
if (hdr.len < sizeof(hdr)) {
warnx("Invalid header length (%d)", hdr.len);
return NULL;
}
msg = malloc(hdr.len);
if (!msg)
return NULL;
off = 0;
while (hdr.len) {
n = recv(fd, msg + off, hdr.len, 0);
if (n > hdr.len) {
warnx("Oversized recv");
} else if (n <= 0) {
warnx("Short recv");
} else {
off += n;
hdr.len -= n;
continue;
}
free(msg);
return NULL;
}
return msg;
}
static int __kpm_send(int fd, struct kpm_header *msg, size_t size, int id,
enum kpm_msg_type type)
{
ssize_t n, off;
msg->type = type;
msg->id = id;
msg->len = size;
off = 0;
while (size) {
n = send(fd, (char *)msg + off, size, 0);
if (n <= 0) {
warnx("Short send");
return -1;
}
size -= n;
}
return id;
}
int kpm_send(int fd, struct kpm_header *msg, size_t size,
enum kpm_msg_type type)
{
static short int id_gen;
return __kpm_send(fd, msg, size, ++id_gen, type);
}
int kpm_send_empty(int fd, enum kpm_msg_type type)
{
struct kpm_header hdr;
return kpm_send(fd, &hdr, sizeof(hdr), type);
}
int kpm_send_u32(int fd, enum kpm_msg_type type, __u32 arg)
{
struct __kpm_generic_u32 msg;
msg.val = arg;
return kpm_send(fd, &msg.hdr, sizeof(msg), type);
}
int kpm_send_conn_id(int fd, __u32 id, __u32 cpu)
{
struct kpm_connection_id msg;
msg.id = id;
msg.cpu = cpu;
return kpm_send(fd, &msg.hdr, sizeof(msg), KPM_MSG_TYPE_CONNECTION_ID);
}
int kpm_send_connect(int fd, struct sockaddr_in6 *addr, socklen_t len,
__u32 mss)
{
struct kpm_connect msg;
if (len > sizeof(msg.addr)) {
warnx("Oversized connect arg");
return -1;
}
msg.len = len;
memcpy(&msg.addr, addr, len);
msg.mss = mss;
return kpm_send(fd, &msg.hdr, sizeof(msg), KPM_MSG_TYPE_CONNECT);
}
int
kpm_send_tls(int fd, __u32 conn_id, __u32 dir_mask, void *info, socklen_t len)
{
struct kpm_tls msg;
if (len > sizeof(msg.info)) {
warnx("Oversized TLS arg");
return -1;
}
msg.connection_id = conn_id;
msg.dir_mask = dir_mask;
msg.len = len;
memcpy(&msg.info, info, len);
return kpm_send(fd, &msg.hdr, sizeof(msg), KPM_MSG_TYPE_TLS);
}
int kpm_send_max_pacing(int fd, __u32 id, __u32 pace)
{
struct kpm_max_pacing msg;
msg.id = id;
msg.max_pacing = pace;
return kpm_send(fd, &msg.hdr, sizeof(msg), KPM_MSG_TYPE_MAX_PACING);
}
int kpm_send_tcp_cc(int fd, __u32 id, char *cc_name)
{
struct kpm_tcp_cc msg = {};
msg.id = id;
strncpy(msg.cc_name, cc_name, sizeof(msg.cc_name) - 1);
return kpm_send(fd, &msg.hdr, sizeof(msg), KPM_MSG_TYPE_TCP_CC);
}
int kpm_send_pin_worker(int fd, __u32 id, __u32 cpu)
{
struct kpm_pin_worker msg;
msg.worker_id = id;
msg.cpu = cpu;
return kpm_send(fd, &msg.hdr, sizeof(msg), KPM_MSG_TYPE_PIN_WORKER);
}
static int kpm_reply(int fd, struct kpm_header *msg, size_t size,
struct kpm_header *req)
{
return __kpm_send(fd, msg, size, req->id, req->type | KPM_MSG_REPLY);
}
void kpm_reply_error(int fd, struct kpm_header *hdr, __u16 error)
{
struct kpm_reply_error msg;
msg.type = hdr->type;
msg.error = error;
__kpm_send(fd, &msg.hdr, sizeof(msg), hdr->id, KPM_MSG_TYPE_ERROR);
}
int kpm_reply_empty(int fd, struct kpm_header *hdr)
{
struct kpm_header msg;
return kpm_reply(fd, &msg, sizeof(msg), hdr);
}
int kpm_reply_u16(int fd, struct kpm_header *hdr, __u16 arg)
{
struct __kpm_generic_u16 msg;
msg.val = arg;
memset(&msg.pad, 0, sizeof(msg.pad));
return kpm_reply(fd, &msg.hdr, sizeof(msg), hdr);
}
int kpm_reply_u32(int fd, struct kpm_header *hdr, __u32 arg)
{
struct __kpm_generic_u32 msg;
msg.val = arg;
return kpm_reply(fd, &msg.hdr, sizeof(msg), hdr);
}
int kpm_reply_acceptor(int fd, struct kpm_header *hdr,
struct sockaddr_in6 *addr, socklen_t len)
{
struct kpm_tcp_acceptor_reply msg;
memcpy(&msg.addr, addr, len);
msg.len = len;
return kpm_reply(fd, &msg.hdr, sizeof(msg), hdr);
}
int kpm_reply_connect(int fd, struct kpm_header *hdr,
__u32 local_id, __u32 local_cpu, __u16 local_port,
__u32 remote_id, __u32 remote_cpu, __u16 remote_port)
{
struct kpm_connect_reply msg = {};
msg.local.id = local_id;
msg.local.cpu = local_cpu;
msg.local.port = local_port;
msg.remote.id = remote_id;
msg.remote.cpu = remote_cpu;
msg.remote.port = remote_port;
return kpm_reply(fd, &msg.hdr, sizeof(msg), hdr);
}
int kpm_xchg_hello(int fd, unsigned int *ncpus)
{
struct kpm_hello hello;
struct kpm_hello *rcv;
hello.version = proto_ver;
hello.n_cpus = get_nprocs();
if (kpm_send(fd, &hello.hdr, sizeof(hello), KPM_MSG_TYPE_HELLO) < 0) {
warnx("Failed to send hello");
return -1;
}
rcv = kpm_receive(fd);
if (!rcv)
return -1;
if (!kpm_good_req(rcv, KPM_MSG_TYPE_HELLO)) {
warnx("Bad hello msg");
goto err_free;
}
if (rcv->version != proto_ver) {
warnx("Bad PROTO version");
goto err_free;
}
if (ncpus)
*ncpus = rcv->n_cpus;
free(rcv);
return 0;
err_free:
free(rcv);
return -1;
}
int kpm_req_tcp_sock(int fd, struct sockaddr_in6 *addr, socklen_t *len)
{
struct kpm_tcp_acceptor_reply *repl;
struct kpm_header hdr;
int id;
id = kpm_send(fd, &hdr, sizeof(hdr), KPM_MSG_TYPE_OPEN_TCP_ACCEPTOR);
if (id < 0) {
warnx("Failed to request TCP sock");
return id;
}
repl = kpm_receive(fd);
if (!repl) {
warnx("Failed to request TCP sock - no response");
return -1;
}
if (!kpm_good_reply(repl, KPM_MSG_TYPE_OPEN_TCP_ACCEPTOR, id)) {
warnx("Failed to request TCP sock - unexpected reply");
free(repl);
return -1;
}
if (*len < repl->len) {
warnx("Failed to request TCP sock - req space small");
free(repl);
return -1;
}
memcpy(addr, &repl->addr, repl->len);
*len = repl->len;
free(repl);
return 0;
}
int kpm_req_end_test(int fd, __u32 test_id)
{
struct kpm_empty *repl;
int id;
id = kpm_send_u32(fd, KPM_MSG_TYPE_END_TEST, test_id);
if (id < 0) {
warnx("Failed to end test");
return id;
}
repl = kpm_receive(fd);
if (!repl) {
warnx("Failed to end test - no response");
return -1;
}
if (!kpm_good_reply(repl, KPM_MSG_TYPE_END_TEST, id)) {
warnx("Failed to end test - bad reply");
free(repl);
return -1;
}
free(repl);
return 0;
}
int
kpm_req_tls(int fd, __u32 conn_id, __u32 dir_mask, void *info, socklen_t len)
{
struct kpm_empty *repl;
int id;
id = kpm_send_tls(fd, conn_id, dir_mask, info, len);
if (id < 0) {
warnx("Failed to start TLS");
return id;
}
repl = kpm_receive(fd);
if (!repl) {
warnx("Failed to start TLS - no response");
return -1;
}
if (!kpm_good_reply(repl, KPM_MSG_TYPE_TLS, id)) {
warnx("Failed to start TLS - bad reply");
free(repl);
return -1;
}
free(repl);
return 0;
}
int
kpm_req_pacing(int fd, __u32 conn_id, __u32 max_pace)
{
struct kpm_empty *repl;
int id;
id = kpm_send_max_pacing(fd, conn_id, max_pace);
if (id < 0) {
warnx("Failed to request pacing");
return id;
}
repl = kpm_receive(fd);
if (!repl) {
warnx("Failed to request pacing - no response");
return -1;
}
if (!kpm_good_reply(repl, KPM_MSG_TYPE_MAX_PACING, id)) {
warnx("Failed to request pacing - bad reply");
free(repl);
return -1;
}
free(repl);
return 0;
}
int
kpm_req_tcp_cc(int fd, __u32 conn_id, char *cc_name)
{
struct kpm_empty *repl;
int id;
id = kpm_send_tcp_cc(fd, conn_id, cc_name);
if (id < 0) {
warnx("Failed to request TCP cong control");
return id;
}
repl = kpm_receive(fd);
if (!repl) {
warnx("Failed to request TCP cong control - no response");
return -1;
}
if (!kpm_good_reply(repl, KPM_MSG_TYPE_TCP_CC, id)) {
warnx("Failed to request TCP cong control - bad reply");
free(repl);
return -1;
}
free(repl);
return 0;
}
int kpm_req_disconnect(int fd, __u32 connection_id)
{
struct kpm_empty *repl;
int id;
id = kpm_send_u32(fd, KPM_MSG_TYPE_DISCONNECT, connection_id);
if (id < 0) {
warnx("Failed to end connection");
return id;
}
repl = kpm_receive(fd);
if (!repl) {
warnx("Failed to end connection - no response");
return -1;
}
if (!kpm_good_reply(repl, KPM_MSG_TYPE_DISCONNECT, id)) {
warnx("Failed to end connection - bad reply");
free(repl);
return -1;
}
free(repl);
return 0;
}