-
Notifications
You must be signed in to change notification settings - Fork 2
/
nio.c
709 lines (571 loc) · 13.7 KB
/
nio.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
#define _GNU_SOURCE
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/time.h>
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>
#include <fcntl.h>
#include <sched.h>
#include <errno.h>
#define MAX_CPUS 128
#define DEFAULT_PORT "7124"
#define CMD_START 1
#define CMD_ACK 2
#define CMD_STOP 3
#define CMD_DATA 4
struct nio_cmd {
uint32_t cmd;
uint32_t threads;
uint32_t seq_lo;
uint32_t seq_hi;
uint32_t recv_lo;
uint32_t recv_hi;
} __attribute__((packed));
enum states {
STATE_START,
STATE_START_SENT,
STATE_STARTED,
STATE_DYING,
};
struct thread_config {
pthread_t thread;
int running;
int fd;
int thread_num;
int cpu;
uint64_t last_seq;
uint64_t packets;
};
static struct thread_config *configs;
static int timeout;
static int polling;
static int port = 7124;
static volatile int should_stop;
static int threads = 1;
static int domain = AF_UNSPEC;
static const char *hostname = NULL;
static int pinning;
static void sig_handler(int signum)
{
fprintf(stderr, "Signal %d received - shutting down\n", signum);
should_stop = 1;
}
static void set_nonblocking(int fd)
{
int prev;
if ((prev = fcntl(fd, F_GETFL, 0)) != -1)
fcntl(fd, F_SETFL, prev | O_NONBLOCK);
}
int create_socket(int af, const char *hostname, const char *service)
{
struct addrinfo hints, *results, *rp;
int err, fd = -1;
memset(&hints, 0, sizeof(hints));
hints.ai_family = af;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
err = getaddrinfo(hostname, service, &hints, &results);
if (err) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
return -1;
}
if (results == NULL) {
fprintf(stderr, "Could not resolve host %s\n", hostname);
return -1;
}
if (af == AF_UNSPEC) {
af = AF_INET;
for (rp = results; rp != NULL; rp = rp->ai_next)
if (rp->ai_family == AF_INET6)
af = AF_INET6;
}
for (rp = results; rp != NULL; rp = rp->ai_next) {
if (rp->ai_family != af)
continue;
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (fd == -1)
continue;
if (hostname == NULL) {
if (bind(fd, rp->ai_addr, rp->ai_addrlen) == 0)
break;
} else {
if (connect(fd, rp->ai_addr, rp->ai_addrlen) != -1)
break;
}
close(fd);
fd = -1;
}
if (fd >= 0)
set_nonblocking(fd);
freeaddrinfo(results);
return fd;
}
static void bind_to_cpu(int cpu)
{
cpu_set_t *cpuset;
size_t size;
cpuset = CPU_ALLOC(MAX_CPUS);
if (cpuset == NULL) {
perror("CPU_ALLOC");
exit(EXIT_FAILURE);
}
size = CPU_ALLOC_SIZE(MAX_CPUS);
CPU_ZERO_S(size, cpuset);
CPU_SET_S(cpu, size, cpuset);
if (sched_setaffinity(0, size, cpuset))
perror("sched_setaffinity");
CPU_FREE(cpuset);
}
void *client_thread(void *data)
{
struct thread_config *cfg = data;
struct timeval tv;
fd_set wfds;
uint64_t i = 0;
int fd;
fd = cfg->fd;
if (cfg->cpu >= 0)
bind_to_cpu(cfg->cpu);
while (!should_stop) {
int ret;
if (!polling) {
tv.tv_sec = 1;
tv.tv_usec = 0;
FD_ZERO(&wfds);
FD_SET(fd, &wfds);
ret = select(fd + 1, NULL, &wfds, NULL, &tv);
if (ret != 1)
continue;
}
if (polling || FD_ISSET(fd, &wfds)) {
uint64_t seq = (i * threads) + cfg->thread_num;
ssize_t sent;
int i;
for (i = 0; i < 16384; ++i) {
sent = send(fd, &seq, sizeof(seq), 0);
if (sent < 0)
break;
if (sent == sizeof(seq)) {
i += 1;
cfg->last_seq = seq;
cfg->packets += 1;
}
}
}
}
return NULL;
}
void *server_thread(void *data)
{
struct thread_config *cfg = data;
struct timeval tv;
fd_set rfds;
int fd;
fd = cfg->fd;
if (cfg->cpu >= 0)
bind_to_cpu(cfg->cpu);
while (!should_stop) {
int ret;
if (!polling) {
tv.tv_sec = 1;
tv.tv_usec = 0;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
ret = select(fd + 1, &rfds, NULL, NULL, &tv);
if (ret != 1)
continue;
}
if (polling || FD_ISSET(fd, &rfds)) {
uint64_t seq;
ssize_t bytes;
int i;
for (i = 0; i < 16384; ++i) {
bytes = recv(fd, &seq, sizeof(seq), 0);
if (bytes < 0)
break;
if (bytes == sizeof(seq)) {
cfg->last_seq = seq;
cfg->packets += 1;
}
}
}
}
return NULL;
}
void wait_for_threads(void)
{
void *ret;
int i;
if (!configs)
return;
for (i = 0; i < threads; ++i) {
if (configs[i].running)
pthread_join(configs[i].thread, &ret);
configs[i].running = 0;
close(configs[i].fd);
}
}
int create_threads(int server)
{
int i;
configs = malloc(sizeof(struct thread_config) * threads);
if (configs == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
memset(configs, 0, sizeof(struct thread_config) * threads);
for (i = 0; i < threads; ++i) {
char srv[16];
int ret;
snprintf(srv, 16, "%d", port + 1 + i);
configs[i].fd = create_socket(domain, hostname, srv);
if (configs[i].fd == -1) {
printf("Failed to create socket\n");
return -1;
}
configs[i].thread_num = i;
configs[i].cpu = pinning ? i : -1;
if (server)
ret = pthread_create(&configs[i].thread, NULL,
server_thread, configs + i);
else
ret = pthread_create(&configs[i].thread, NULL,
client_thread, configs + i);
if (ret) {
perror("pthread_create");
return -1;
}
configs[i].running = 1;
}
return 0;
}
static uint64_t timediff(struct timeval *last, struct timeval *now)
{
uint64_t val;
val = (now->tv_sec - last->tv_sec - 1) * 1000000;
val += (1000000 - last->tv_usec) + now->tv_usec;
return val;
}
struct packet_stats {
uint64_t packets;
uint64_t seq;
};
static void fetch_stats(struct packet_stats *stats, int only_running)
{
int i;
memset(stats, 0, sizeof(*stats));
if (configs == NULL)
return;
for (i = 0; i < threads; ++i) {
if (only_running && !configs[i].running)
continue;
stats->packets += configs[i].packets;
if (stats->seq < configs[i].last_seq)
stats->seq = configs[i].last_seq;
}
return;
}
static void get_server_stats(struct nio_cmd *cmd)
{
struct packet_stats stats;
memset(cmd, 0, sizeof(*cmd));
fetch_stats(&stats, 1);
cmd->cmd = htonl(CMD_DATA);
cmd->seq_lo = htonl(stats.seq & 0xffffffffULL);
cmd->seq_hi = htonl(stats.seq >> 32);
cmd->recv_lo = htonl(stats.packets & 0xffffffffULL);
cmd->recv_hi = htonl(stats.packets >> 32);
}
void ctrl_server(int fd)
{
enum states state = STATE_START;
struct sockaddr_storage remote;
struct nio_cmd cmd, recv_cmd;
fd_set rfds, wfds;
int cmd_write = 0;
socklen_t r_len;
struct timeval tv, last, now;
while (state != STATE_DYING) {
int ret;
gettimeofday(&now, NULL);
if (state == STATE_STARTED &&
timediff(&last, &now) >= 1000000)
cmd_write = 1;
tv.tv_sec = 1;
tv.tv_usec = 0;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_SET(fd, &rfds);
if (cmd_write)
FD_SET(fd, &wfds);
ret = select(fd + 1, &rfds, &wfds, NULL, &tv);
if (ret == -1) {
if (errno == EINTR)
continue;
perror("select");
exit(EXIT_FAILURE);
}
if (should_stop)
break;
if (FD_ISSET(fd, &wfds)) {
int sent;
if (state == STATE_START) {
sent = sendto(fd, &cmd, sizeof(cmd), 0,
(struct sockaddr *)&remote, r_len);
if (sent != sizeof(cmd)) {
perror("sendto");
exit(EXIT_FAILURE);
}
cmd_write = 0;
state = STATE_STARTED;
gettimeofday(&last, NULL);
create_threads(1);
printf("Server started\n");
} else if (state == STATE_STARTED) {
get_server_stats(&cmd);
sent = sendto(fd, &cmd, sizeof(cmd), 0,
(struct sockaddr *)&remote, r_len);
if (sent != sizeof(cmd)) {
perror("sendto");
exit(EXIT_FAILURE);
}
gettimeofday(&last, NULL);
cmd_write = 0;
}
}
if (FD_ISSET(fd, &rfds)) {
ssize_t bytes;
r_len = sizeof(remote);
bytes = recvfrom(fd, &recv_cmd, sizeof(recv_cmd), 0,
(struct sockaddr *)&remote, &r_len);
if (bytes != sizeof(recv_cmd))
continue;
switch (ntohl(recv_cmd.cmd)) {
case CMD_START:
if (state != STATE_START)
break;
threads = ntohl(recv_cmd.threads);
memset(&cmd, 0, sizeof(cmd));
cmd.cmd = htonl(CMD_ACK);
cmd_write = 1;
break;
case CMD_STOP:
should_stop = 1;
wait_for_threads();
state = STATE_DYING;
break;
default:
break;
}
}
}
}
void ctrl_client(int fd)
{
uint64_t sent_packets, last_sent_packets = 0;
uint64_t packets, last_packets = 0;
enum states state = STATE_START;
struct nio_cmd cmd, recv_cmd;
struct timeval tv, last, now;
struct timeval start, end;
struct packet_stats stats;
uint64_t run_time;
fd_set rfds, wfds;
int cmd_write = 0;
int got_data = 0;
memset(&cmd, 0, sizeof(cmd));
cmd.cmd = htonl(CMD_START);
cmd.threads = htonl(threads);
cmd_write = 1;
gettimeofday(&last, NULL);
start = last;
while (state != STATE_DYING) {
int ret;
tv.tv_sec = 1;
tv.tv_usec = 0;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_SET(fd, &rfds);
if (cmd_write)
FD_SET(fd, &wfds);
if (should_stop) {
memset(&cmd, 0, sizeof(cmd));
cmd.cmd = htonl(CMD_STOP);
cmd_write = 1;
}
ret = select(fd + 1, &rfds, &wfds, NULL, &tv);
if (ret == -1) {
if (errno == EINTR)
continue;
perror("select");
exit(EXIT_FAILURE);
}
if (FD_ISSET(fd, &wfds)) {
ssize_t sent;
sent = send(fd, &cmd, sizeof(cmd), 0);
if (sent != sizeof(cmd)) {
perror("send");
exit(EXIT_FAILURE);
}
if (state == STATE_START)
state = STATE_START_SENT;
if (should_stop)
state = STATE_DYING;
cmd_write = 0;
}
if (FD_ISSET(fd, &rfds)) {
ssize_t bytes;
uint32_t cmd;
bytes = recv(fd, &recv_cmd, sizeof(recv_cmd), 0);
if (bytes != sizeof(recv_cmd)) {
perror("recv");
continue;
}
cmd = ntohl(recv_cmd.cmd);
switch (cmd) {
case CMD_ACK:
if (state == STATE_START_SENT) {
create_threads(0);
printf("Client started\n");
if (timeout > 0)
alarm(timeout);
state = STATE_STARTED;
}
break;
case CMD_DATA:
if (state != STATE_STARTED)
break;
gettimeofday(&now, NULL);
packets = ntohl(recv_cmd.recv_hi);
packets = (packets << 32) | ntohl(recv_cmd.recv_lo);
fetch_stats(&stats, 1);
sent_packets = stats.packets;
if (got_data) {
uint64_t p = packets - last_packets;
uint64_t s = sent_packets - last_sent_packets;
printf("PPS: %ju Sent: %ju\n",
(p * 1000000) / timediff(&last, &now),
(s * 1000000) / timediff(&last, &now));
}
last_sent_packets = sent_packets;
last_packets = packets;
last = now;
got_data = 1;
break;
default:
break;
}
}
}
wait_for_threads();
gettimeofday(&end, NULL);
fetch_stats(&stats, 0);
run_time = timediff(&start, &end);
printf("Ran for %ju.%07ju seconds with %d threads\n",
run_time / 1000000, run_time % 1000000, threads);
printf("Average PPS received: %ju Average PPS Sent: %ju\n",
(last_packets * 1000000) / run_time,
(stats.packets * 1000000) / run_time);
}
void usage(const char *prg)
{
printf("Usage: %s [-s] [-c server] [-p port] [-n threads] [-4] [-b] [-h]\n", prg);
printf(" -s Server Mode - Wait for incoming packets\n");
printf(" -r server Client Mode - Send packets to server\n");
printf(" -p port UDP port to bind to\n");
printf(" -n threads Number of thread to start for sending/receiving\n");
printf(" -l Polling mode - Do not use select() in worker threads\n");
printf(" -t timeout Timeout in seconds after client should stop and exit\n");
printf(" -b Bind worker threads to single CPUs\n");
printf(" -4 Force use of IPv4\n");
printf(" -6 Force use of IPv6\n");
printf(" -h Print this help message and exit\n");
}
int main(int argc, char **argv)
{
const char *service = DEFAULT_PORT;
int port = atoi(DEFAULT_PORT);
int server = 0;
int ctrl_fd;
int opt;
/* Parse options */
while (1) {
opt = getopt(argc, argv, "sr:p:t:n:h46lb");
if (opt == EOF)
break;
switch (opt) {
case 's':
server = 1;
break;
case 'r':
hostname = optarg;
break;
case 'p':
service = optarg;
port = atoi(service);
break;
case 'n':
threads = atoi(optarg);
break;
case 't':
timeout = atoi(optarg);
break;
case 'b':
pinning = 1;
break;
case '4':
domain = AF_INET;
break;
case '6':
domain = AF_INET6;
break;
case 'l':
polling = 1;
break;
default:
fprintf(stderr, "ERROR: Unknown option: %c\n", opt);
/* fall-through */
case 'h':
usage(argv[0]);
exit(EXIT_SUCCESS);
}
}
/* Some sanity checks */
if (hostname && server) {
fprintf(stderr, "Only one of -s or -r is allowed\n");
usage(argv[0]);
exit(EXIT_FAILURE);
}
if (server && port < 0) {
fprintf(stderr, "Option -s requires also -p");
usage(argv[0]);
exit(EXIT_FAILURE);
}
if (threads < 1) {
fprintf(stderr, "Invalid number of threads: %d\n", threads);
usage(argv[0]);
exit(EXIT_FAILURE);
}
/* Setup signals */
signal(SIGTERM, sig_handler);
signal(SIGINT, sig_handler);
signal(SIGALRM, sig_handler);
signal(SIGHUP, sig_handler);
signal(SIGQUIT, sig_handler);
ctrl_fd = create_socket(domain, hostname, service);
if (ctrl_fd == -1)
return EXIT_FAILURE;
if (server)
ctrl_server(ctrl_fd);
else
ctrl_client(ctrl_fd);
close(ctrl_fd);
return 0;
}