-
Notifications
You must be signed in to change notification settings - Fork 6
/
server_session.c
1046 lines (873 loc) · 22 KB
/
server_session.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright Meta Platforms, Inc. and affiliates */
#define _GNU_SOURCE
#include <errno.h>
#include <pthread.h>
#include <sched.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/in6.h>
#include <linux/socket.h>
#include <linux/tcp.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sysinfo.h>
#include <ccan/array_size/array_size.h>
#include <ccan/compiler/compiler.h>
#include <ccan/daemonize/daemonize.h>
#include <ccan/err/err.h>
#include <ccan/fdpass/fdpass.h>
#include <ccan/minmax/minmax.h>
#include "proto.h"
#include "proto_dbg.h"
#include "server.h"
struct session_state {
int main_sock;
int epollfd;
int quit;
int tcp_sock;
enum kpm_rx_mode rx_mode;
enum kpm_tx_mode tx_mode;
unsigned int connection_ids;
unsigned int worker_ids;
unsigned int test_ids;
struct list_head connections;
struct list_head pworkers;
struct list_head tests;
struct session_state_devmem devmem;
};
struct connection {
unsigned int id;
int fd;
int cpu;
int worker_fd;
unsigned int tls_mask;
struct list_node connections;
};
struct pworker {
unsigned int id;
int fd;
pid_t pid;
int busy;
struct list_node pworkers;
};
struct test {
unsigned int id;
int active;
unsigned int min_worker_id;
unsigned int worker_range;
unsigned int workers_total;
unsigned int workers_done;
struct kpm_test *req, **fwd;
struct kpm_test_results **results;
struct list_node tests;
};
static struct connection *
session_find_connection_by_id(struct session_state *self, unsigned int id)
{
struct connection *conn;
list_for_each(&self->connections, conn, connections) {
if (conn->id == id)
return conn;
}
return NULL;
}
static struct pworker *
session_find_worker_by_id(struct session_state *self, unsigned int id)
{
struct pworker *pwrk;
list_for_each(&self->pworkers, pwrk, pworkers) {
if (pwrk->id == id)
return pwrk;
}
return NULL;
}
static struct test *
session_find_test_by_id(struct session_state *self, unsigned int id)
{
struct test *test;
list_for_each(&self->tests, test, tests) {
if (test->id == id)
return test;
}
return NULL;
}
static void session_new_conn(struct session_state *self, int fd)
{
struct connection *conn;
socklen_t len;
conn = malloc(sizeof(*conn));
if (!conn)
goto err_close;
memset(conn, 0, sizeof(*conn));
conn->id = ++self->connection_ids;
conn->fd = fd;
len = sizeof(conn->cpu);
if (getsockopt(fd, SOL_SOCKET, SO_INCOMING_CPU, &conn->cpu, &len) < 0) {
warn("Failed to read CPU for socket");
goto err_free;
}
if (kpm_send_conn_id(fd, conn->id, conn->cpu) < 0)
goto err_free;
list_add(&self->connections, &conn->connections);
return;
err_free:
free(conn);
err_close:
close(fd);
return;
}
static void
server_msg_tcp_acceptor(struct session_state *self, struct kpm_header *req)
{
struct epoll_event ev = {};
struct sockaddr_in6 addr;
socklen_t len;
int ret;
if (self->tcp_sock) {
kpm_reply_error(self->main_sock, req, EBUSY);
return;
}
len = sizeof(addr);
if (getsockname(self->main_sock, (void *)&addr, &len)) {
warn("Failed to get sock type for main sock");
self->quit = 1;
return;
}
addr.sin6_port = 0;
self->tcp_sock = socket(addr.sin6_family, SOCK_STREAM, 0);
if (self->tcp_sock < 0) {
warn("Failed to open socket");
self->quit = 1;
return;
}
ret = bind(self->tcp_sock, (void *)&addr, sizeof(addr));
if (ret < 0) {
warn("Failed to bind socket");
self->quit = 1;
return;
}
ret = listen(self->tcp_sock, 10);
if (ret < 0) {
warn("Failed to listen on socket");
self->quit = 1;
return;
}
len = sizeof(addr);
if (getsockname(self->tcp_sock, (void *)&addr, &len)) {
warn("Failed to get sock type for main sock");
self->quit = 1;
return;
}
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = self->tcp_sock;
if (epoll_ctl(self->epollfd, EPOLL_CTL_ADD, self->tcp_sock, &ev) < 0) {
warn("Failed to add tcp sock to epoll");
self->quit = 1;
return;
}
if (kpm_reply_acceptor(self->main_sock, req, &addr, len) < 1) {
warn("Failed reply in %s", __func__);
self->quit = 1;
return;
}
}
static void
server_msg_connect(struct session_state *self, struct kpm_header *hdr)
{
unsigned short local_port, remote_port;
struct kpm_connection_id *id;
struct sockaddr_in6 addr;
struct kpm_connect *req;
struct connection *conn;
socklen_t len;
int ret, cfd;
if (hdr->len < sizeof(struct kpm_connect)) {
warn("Invalid request in %s", __func__);
self->quit = 1;
return;
}
req = (void *)hdr;
conn = malloc(sizeof(*conn));
if (!conn) {
self->quit = 1;
return;
}
memset(conn, 0, sizeof(*conn));
cfd = socket(req->addr.sin6_family, SOCK_STREAM, 0);
if (cfd < 0) {
warn("Failed to open socket");
goto err_free;
}
if (req->mss &&
setsockopt(cfd, IPPROTO_TCP, TCP_MAXSEG,
(void *)&req->mss, sizeof(req->mss))) {
warn("Setting mss failed");
goto err_close;
}
ret = connect(cfd, (void *)&req->addr, req->len);
if (ret < 0) {
warn("Failed to connect");
goto err_close;
}
id = kpm_receive(cfd);
if (!id) {
warnx("No connection ID");
goto err_close;
}
if (id->hdr.type != KPM_MSG_TYPE_CONNECTION_ID ||
id->hdr.len != sizeof(*id)) {
warnx("Invalid connection ID %d %d", id->hdr.type, id->hdr.len);
goto err_free_id;
}
conn->id = ++self->connection_ids;
conn->fd = cfd;
len = sizeof(conn->cpu);
if (getsockopt(cfd, SOL_SOCKET, SO_INCOMING_CPU, &conn->cpu, &len) < 0) {
warn("Failed to read CPU for socket");
goto err_free_id;
}
len = sizeof(addr);
if (getsockname(cfd, &addr, &len)) {
warn("Failed to read address of socket");
goto err_free_id;
}
local_port = ntohs(addr.sin6_port);
len = sizeof(addr);
if (getpeername(cfd, &addr, &len)) {
warn("Failed to read address of socket");
goto err_free_id;
}
remote_port = ntohs(addr.sin6_port);
if (kpm_reply_connect(self->main_sock, hdr,
conn->id, conn->cpu, local_port,
id->id, id->cpu, remote_port) < 1) {
warn("Failed to reply");
goto err_free_id;
}
list_add(&self->connections, &conn->connections);
free(id);
return;
err_free_id:
free(id);
err_close:
close(cfd);
err_free:
free(conn);
self->quit = 1;
return;
}
static void
server_msg_disconnect(struct session_state *self, struct kpm_header *hdr)
{
struct __kpm_generic_u32 *req;
struct connection *conn;
if (hdr->len < sizeof(*req)) {
warn("Invalid request in %s", __func__);
goto err_quit;
}
req = (void *)hdr;
conn = session_find_connection_by_id(self, req->val);
if (!conn) {
warnx("connection not found");
kpm_reply_error(self->main_sock, hdr, ENOENT);
goto err_quit;
}
kpm_trace("close %d", conn->fd);
close(conn->fd);
list_del(&conn->connections);
free(conn);
if (kpm_reply_empty(self->main_sock, hdr) < 1) {
warnx("Reply failed");
goto err_quit;
}
return;
err_quit:
self->quit = 1;
}
static void
server_msg_tls(struct session_state *self, struct kpm_header *hdr)
{
struct connection *conn;
struct kpm_tls *req;
int one = 1;
if (hdr->len < sizeof(*req)) {
warn("Invalid request in %s", __func__);
goto err_quit;
}
req = (void *)hdr;
if (req->dir_mask & ~(KPM_TLS_ULP | KPM_TLS_TX | KPM_TLS_RX |
KPM_TLS_NOPAD)) {
warnx("unknown TLS flag");
kpm_reply_error(self->main_sock, hdr, EINVAL);
goto err_quit;
}
conn = session_find_connection_by_id(self, req->connection_id);
if (!conn) {
warnx("connection not found");
kpm_reply_error(self->main_sock, hdr, ENOENT);
goto err_quit;
}
if (conn->tls_mask & req->dir_mask) {
warnx("TLS already set");
kpm_reply_error(self->main_sock, hdr, EBUSY);
goto err_quit;
}
if (!((conn->tls_mask | req->dir_mask) & KPM_TLS_ULP)) {
warnx("TLS ULP not requested");
kpm_reply_error(self->main_sock, hdr, EINVAL);
goto err_quit;
}
if ((req->dir_mask & KPM_TLS_ULP) &&
setsockopt(conn->fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"))) {
warn("TLS ULP setup failed");
goto err_repl_errno;
}
if ((req->dir_mask & KPM_TLS_TX) &&
setsockopt(conn->fd, SOL_TLS, TLS_TX,
(void *)&req->info, req->len)) {
warn("TLS Tx setup failed");
goto err_repl_errno;
}
if ((req->dir_mask & KPM_TLS_RX) &&
setsockopt(conn->fd, SOL_TLS, TLS_RX,
(void *)&req->info, req->len)) {
warn("TLS Rx setup failed");
goto err_repl_errno;
}
if ((req->dir_mask & KPM_TLS_NOPAD) &&
setsockopt(conn->fd, SOL_TLS, TLS_RX_EXPECT_NO_PAD,
(void *)&one, sizeof(one))) {
warn("TLS nopad setup failed");
goto err_repl_errno;
}
conn->tls_mask = req->dir_mask;
if (kpm_reply_empty(self->main_sock, hdr) < 1) {
warnx("Reply failed");
goto err_quit;
}
return;
err_repl_errno:
kpm_reply_error(self->main_sock, hdr, errno);
err_quit:
self->quit = 1;
}
static void
server_msg_max_pacing(struct session_state *self, struct kpm_header *hdr)
{
struct kpm_max_pacing *req;
struct connection *conn;
if (hdr->len < sizeof(*req)) {
warn("Invalid request in %s", __func__);
goto err_quit;
}
req = (void *)hdr;
conn = session_find_connection_by_id(self, req->id);
if (!conn) {
warnx("connection not found");
kpm_reply_error(self->main_sock, hdr, ENOENT);
goto err_quit;
}
if (setsockopt(conn->fd, SOL_SOCKET, SO_MAX_PACING_RATE,
&req->max_pacing, sizeof(req->max_pacing))) {
warn("setting pacing rate failed");
goto err_repl_errno;
}
if (kpm_reply_empty(self->main_sock, hdr) < 1) {
warnx("Reply failed");
goto err_quit;
}
return;
err_repl_errno:
kpm_reply_error(self->main_sock, hdr, errno);
err_quit:
self->quit = 1;
}
static void
server_msg_tcp_cc(struct session_state *self, struct kpm_header *hdr)
{
struct connection *conn;
struct kpm_tcp_cc *req;
if (hdr->len < sizeof(*req)) {
warn("Invalid request in %s", __func__);
goto err_quit;
}
req = (void *)hdr;
conn = session_find_connection_by_id(self, req->id);
if (!conn) {
warnx("connection not found");
kpm_reply_error(self->main_sock, hdr, ENOENT);
goto err_quit;
}
if (setsockopt(conn->fd, IPPROTO_TCP, TCP_CONGESTION, &req->cc_name,
strnlen(req->cc_name, sizeof(req->cc_name)))) {
warn("setting TCP cong contorl failed");
goto err_repl_errno;
}
if (kpm_reply_empty(self->main_sock, hdr) < 1) {
warnx("Reply failed");
goto err_quit;
}
return;
err_repl_errno:
kpm_reply_error(self->main_sock, hdr, errno);
err_quit:
self->quit = 1;
}
static void
server_msg_mode(struct session_state *self, struct kpm_header *hdr)
{
struct kpm_mode *req;
int ret;
if (hdr->len < sizeof(*req)) {
warn("Invalid request in %s", __func__);
goto err_quit;
}
req = (void *)hdr;
if (self->tcp_sock && req->rx_mode == KPM_RX_MODE_DEVMEM) {
ret = devmem_setup(&self->devmem, self->tcp_sock, req->udmabuf_size_mb);
if (ret < 0) {
warn("Failed to setup devmem");
self->quit = 1;
return;
}
}
self->rx_mode = req->rx_mode;
self->tx_mode = req->tx_mode;
if (kpm_reply_empty(self->main_sock, hdr) < 1) {
warnx("Reply failed");
goto err_quit;
}
return;
err_quit:
self->quit = 1;
}
static void
server_msg_spawn_pworker(struct session_state *self, struct kpm_header *hdr)
{
struct epoll_event ev = {};
struct pworker *pwrk;
int p[2];
pwrk = malloc(sizeof(*pwrk));
if (!pwrk) {
self->quit = 1;
return;
}
memset(pwrk, 0, sizeof(*pwrk));
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, p) < 0)
goto err_free;
pwrk->pid = fork();
if (pwrk->pid < 0) {
warn("Failed to fork");
goto err_free;
}
if (!pwrk->pid) {
close(p[0]);
pworker_main(p[1], self->rx_mode, self->tx_mode);
exit(1);
}
pwrk->id = ++self->worker_ids;
pwrk->fd = p[0];
close(p[1]);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = pwrk->fd;
if (epoll_ctl(self->epollfd, EPOLL_CTL_ADD, pwrk->fd, &ev) < 0) {
warn("Failed to add worker sock to epoll");
goto err_worker_kill;
}
kpm_send_u32(pwrk->fd, KPM_MSG_WORKER_ID, pwrk->id);
if (kpm_reply_u32(self->main_sock, hdr, pwrk->id) < 1)
goto err_worker_kill;
list_add(&self->pworkers, &pwrk->pworkers);
return;
err_worker_kill:
kpm_send_empty(pwrk->fd, KPM_MSG_WORKER_KILL);
err_free:
free(pwrk);
self->quit = 1;
return;
}
static void
server_msg_pin_worker(struct session_state *self, struct kpm_header *hdr)
{
struct kpm_pin_worker *req;
struct pworker *pwrk;
cpu_set_t set;
if (hdr->len < sizeof(struct kpm_pin_worker)) {
warn("Invalid request in %s", __func__);
self->quit = 1;
return;
}
req = (void *)hdr;
pwrk = session_find_worker_by_id(self, req->worker_id);
if (!pwrk) {
kpm_reply_error(self->main_sock, hdr, ENOENT);
return;
}
CPU_ZERO(&set);
if (req->cpu == (unsigned int)-1) {
int i, n;
n = sysconf(_SC_NPROCESSORS_CONF);
if (n < 0) {
warn("Failed to get CPU count");
kpm_reply_error(self->main_sock, hdr, errno);
return;
}
for (i = 0; i < n; i++)
CPU_SET(i, &set);
} else {
CPU_SET(req->cpu, &set);
}
if (sched_setaffinity(pwrk->pid, sizeof(set), &set) < 0) {
warn("Failed to pin worker to CPU");
kpm_reply_error(self->main_sock, hdr, errno);
return;
}
if (kpm_reply_empty(self->main_sock, hdr) < 1) {
self->quit = 1;
return;
}
}
static void
server_msg_test(struct session_state *self, struct kpm_header *hdr)
{
unsigned int i, j, min_wrk, max_wrk;
struct kpm_test *req, **fwd;
unsigned int n_conns;
struct test *test;
if (hdr->len < sizeof(struct kpm_test)) {
bad_req:
warnx("Invalid request in %s: %d < %zd",
__func__, hdr->len, sizeof(*req));
self->quit = 1;
return;
}
req = (void *)hdr;
n_conns = hdr->len - sizeof(struct kpm_test);
if (n_conns % sizeof(struct kpm_test_spec))
goto bad_req;
n_conns /= sizeof(struct kpm_test_spec);
if (req->test_id || !req->time_sec || n_conns != req->n_conns)
goto bad_req;
test = malloc(sizeof(*test));
memset(test, 0, sizeof(*test));
test->id = ++self->test_ids;
test->active = req->active;
min_wrk = -1;
max_wrk = 0;
for (i = 0; i < n_conns; i++) {
min_wrk = min(min_wrk, req->specs[i].worker_id);
max_wrk = max(max_wrk, req->specs[i].worker_id);
}
test->worker_range = max_wrk - min_wrk + 1;
fwd = calloc(test->worker_range, sizeof(void *));
for (i = 0; i < n_conns; i++)
fwd[i] = calloc(1, hdr->len);
test->results = calloc(test->worker_range, sizeof(*test->results));
for (i = 0; i < n_conns; i++) {
struct kpm_test_spec *t = &req->specs[i];
struct connection *conn;
struct pworker *pwrk;
struct kpm_test *msg;
pwrk = session_find_worker_by_id(self, t->worker_id);
conn = session_find_connection_by_id(self, t->connection_id);
if (!pwrk || !conn) {
warnx("worker or connection not found");
kpm_reply_error(self->main_sock, hdr, ENOENT);
goto err_free;
}
if (pwrk->busy) {
warnx("worker is busy");
kpm_reply_error(self->main_sock, hdr, EBUSY);
goto err_free;
}
msg = fwd[t->worker_id - min_wrk];
memcpy(&msg->specs[msg->n_conns++], t, sizeof(*t));
}
for (i = 0; i < test->worker_range; i++) {
struct connection *conn;
struct pworker *pwrk;
struct kpm_test *msg;
msg = fwd[i];
if (!msg->n_conns)
continue;
msg->active = req->active;
msg->time_sec = req->time_sec;
msg->test_id = test->id;
test->workers_total++;
pwrk = session_find_worker_by_id(self, msg->specs[0].worker_id);
pwrk->busy = 1;
kpm_send(pwrk->fd, &msg->hdr,
sizeof(*msg) + sizeof(msg->specs[0]) * msg->n_conns,
KPM_MSG_WORKER_TEST);
for (j = 0; j < msg->n_conns; j++) {
conn = session_find_connection_by_id(self, msg->specs[j].connection_id);
fdpass_send(pwrk->fd, conn->fd);
}
}
test->req = kpm_msg_dup(hdr);
test->fwd = fwd;
test->min_worker_id = min_wrk;
list_add(&self->tests, &test->tests);
kpm_reply_u32(self->main_sock, hdr, test->id);
return;
err_free:
free(fwd);
self->quit = 1;
return;
}
static void
server_msg_end_test(struct session_state *self, struct kpm_header *hdr)
{
struct kpm_end_test *req;
struct test *test;
unsigned int i;
if (hdr->len < sizeof(*req)) {
warn("Invalid request in %s", __func__);
self->quit = 1;
return;
}
req = (void *)hdr;
test = session_find_test_by_id(self, req->id);
if (!test) {
warn("Failed to find test");
kpm_reply_error(self->main_sock, hdr, ENOENT);
return;
}
if (test->active && test->workers_total != test->workers_done) {
warn("Early test termination not supported");
kpm_reply_error(self->main_sock, hdr, EBUSY);
return;
}
for (i = 0; i < test->worker_range; i++) {
struct pworker *pwrk;
struct kpm_test *msg;
msg = test->fwd[i];
if (!msg->n_conns) {
warnx("no conns on %d", i);
continue;
}
kpm_trace("searching for worker %d", msg->specs[0].worker_id);
pwrk = session_find_worker_by_id(self, msg->specs[0].worker_id);
pwrk->busy = 0;
kpm_trace("Sending end test to worker");
kpm_send_u32(pwrk->fd, KPM_MSG_WORKER_END_TEST, req->id);
}
if (kpm_reply_empty(self->main_sock, hdr) < 1) {
self->quit = 1;
return;
}
}
static void session_handle_main_sock(struct session_state *self)
{
struct kpm_header *hdr;
hdr = kpm_receive(self->main_sock);
if (!hdr) {
__kpm_dbg("<<", "ctrl recv failed");
self->quit = 1;
return;
}
kpm_cmd_dbg_start(hdr);
switch (hdr->type) {
case KPM_MSG_TYPE_OPEN_TCP_ACCEPTOR:
server_msg_tcp_acceptor(self, hdr);
break;
case KPM_MSG_TYPE_CONNECT:
server_msg_connect(self, hdr);
break;
case KPM_MSG_TYPE_DISCONNECT:
server_msg_disconnect(self, hdr);
break;
case KPM_MSG_TYPE_TLS:
server_msg_tls(self, hdr);
break;
case KPM_MSG_TYPE_MAX_PACING:
server_msg_max_pacing(self, hdr);
break;
case KPM_MSG_TYPE_TCP_CC:
server_msg_tcp_cc(self, hdr);
break;
case KPM_MSG_TYPE_MODE:
server_msg_mode(self, hdr);
break;
case KPM_MSG_TYPE_SPAWN_PWORKER:
server_msg_spawn_pworker(self, hdr);
break;
case KPM_MSG_TYPE_PIN_WORKER:
server_msg_pin_worker(self, hdr);
break;
case KPM_MSG_TYPE_TEST:
server_msg_test(self, hdr);
break;
case KPM_MSG_TYPE_END_TEST:
server_msg_end_test(self, hdr);
break;
default:
warnx("Unknown message type: %d", hdr->type);
self->quit = 1;
break;
}
kpm_cmd_dbg_end(hdr);
free(hdr);
}
static void
session_results_assemble(struct session_state *self, struct test *test)
{
struct kpm_test_results *reply;
unsigned int i, j;
size_t sz;
if (!test->results[0]) {
warnx("First result slot empty!");
return;
}
sz = sizeof(*reply) + test->req->n_conns * sizeof(reply->res[0]);
reply = calloc(1, sz);
memcpy(reply, test->results[0], sizeof(*reply));
for (i = 0; i < test->req->n_conns; i++) {
struct kpm_test_result *res = NULL;
struct kpm_test_results *rmsg;
__u32 worker_id, conn_id;
worker_id = test->req->specs[i].worker_id;
conn_id = test->req->specs[i].connection_id;
rmsg = test->results[worker_id - test->min_worker_id];
if (!rmsg) {
warnx("No results for worker %d", worker_id);
return;
}
for (j = 0; j < rmsg->n_conns; j++) {
if (rmsg->res[j].connection_id == conn_id) {
res = &rmsg->res[j];
break;
}
}
if (!res) {
warnx("No results for connection %d", conn_id);
return;
}
memcpy(&reply->res[i], res, sizeof(*res));
}
kpm_dbg("Results sent");
kpm_send(self->main_sock, &reply->hdr, sz, KPM_MSG_TYPE_TEST_RESULT);
}
static void
session_wmsg_test(struct session_state *self, struct kpm_header *hdr)
{
struct kpm_test_results *msg = (void *)hdr;
__u32 worker_id = msg->res[0].worker_id;
struct test *test;
test = session_find_test_by_id(self, msg->test_id);
if (!test)
warn("Failed to find test for result");
test->workers_done++;
if (test->results[worker_id - test->min_worker_id])
warnx("Results already reported for worker %d", worker_id);
test->results[worker_id - test->min_worker_id] = kpm_msg_dup(&msg->hdr);
kpm_dbg("Results received %d/%d",
test->workers_done, test->workers_total);
if (test->workers_done == test->workers_total)
session_results_assemble(self, test);
}
static void session_handle_worker(struct session_state *self, int fd)
{
struct kpm_header *hdr;
hdr = kpm_receive(fd);
if (!hdr) {
warnx("worker recv empty");
self->quit = 1;
return;
}
__kpm_cmd_dbg_start("worker", hdr);
switch (hdr->type) {
case KPM_MSG_WORKER_TEST_RESULT:
session_wmsg_test(self, hdr);
break;
default:
warnx("Unknown worker message type: %d", hdr->type);
self->quit = 1;
break;
}
__kpm_cmd_dbg_end("worker", hdr);
free(hdr);
}
static void session_handle_accept_sock(struct session_state *self)
{
struct sockaddr_in6 sockaddr;
socklen_t addrlen;
int cfd;
__kpm_trace(">>", "accept");
addrlen = sizeof(sockaddr);
cfd = accept(self->tcp_sock, (void *)&sockaddr, &addrlen);
if (cfd < 0)
warn("Failed to accept conn");
else
session_new_conn(self, cfd);
}
static void server_session_loop(int fd)
{
struct session_state self = { .main_sock = fd, };
struct epoll_event ev = {}, events[32];
struct connection *conn, *next;
list_head_init(&self.connections);
list_head_init(&self.pworkers);
list_head_init(&self.tests);
self.epollfd = epoll_create1(0);
if (self.epollfd < 0)
err(1, "Failed to create epoll");
ev.events = EPOLLIN;
ev.data.fd = fd;
if (epoll_ctl(self.epollfd, EPOLL_CTL_ADD, fd, &ev) < 0)
err(2, "Failed to init epoll");
while (!self.quit) {
int i, nfds;
nfds = epoll_wait(self.epollfd, events, ARRAY_SIZE(events), -1);
if (nfds < 0)
err(3, "Failed to epoll");
for (i = 0; i < nfds; i++) {
struct epoll_event *e = &events[i];
if (e->data.fd == self.main_sock)
session_handle_main_sock(&self);
else if (e->data.fd == self.tcp_sock)
session_handle_accept_sock(&self);
else
session_handle_worker(&self, e->data.fd);
}