1 /*
2 * iperf, Copyright (c) 2014-2018 The Regents of the University of
3 * California, through Lawrence Berkeley National Laboratory (subject
4 * to receipt of any required approvals from the U.S. Dept. of
5 * Energy). All rights reserved.
6 *
7 * If you have questions about your rights to use or distribute this
8 * software, please contact Berkeley Lab's Technology Transfer
9 * Department at TTD@lbl.gov.
10 *
11 * NOTICE. This software is owned by the U.S. Department of Energy.
12 * As such, the U.S. Government has been granted for itself and others
13 * acting on its behalf a paid-up, nonexclusive, irrevocable,
14 * worldwide license in the Software to reproduce, prepare derivative
15 * works, and perform publicly and display publicly. Beginning five
16 * (5) years after the date permission to assert copyright is obtained
17 * from the U.S. Department of Energy, and subject to any subsequent
18 * five (5) year renewals, the U.S. Government is granted for itself
19 * and others acting on its behalf a paid-up, nonexclusive,
20 * irrevocable, worldwide license in the Software to reproduce,
21 * prepare derivative works, distribute copies to the public, perform
22 * publicly and display publicly, and to permit others to do so.
23 *
24 * This code is distributed under a BSD style license, see the LICENSE
25 * file for complete information.
26 */
27 /* iperf_server_api.c: Functions to be used by an iperf server
28 */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <getopt.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <assert.h>
37 #include <fcntl.h>
38 #include <sys/socket.h>
39 #include <sys/types.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42 #include <netdb.h>
43 #ifdef HAVE_STDINT_H
44 #include <stdint.h>
45 #endif
46 #include <sys/time.h>
47 #include <sys/resource.h>
48 #include <sched.h>
49 #include <setjmp.h>
50
51 #include "iperf.h"
52 #include "iperf_api.h"
53 #include "iperf_udp.h"
54 #include "iperf_tcp.h"
55 #include "iperf_util.h"
56 #include "timer.h"
57 #include "iperf_time.h"
58 #include "net.h"
59 #include "units.h"
60 #include "iperf_util.h"
61 #include "iperf_locale.h"
62
63 #if defined(HAVE_TCP_CONGESTION)
64 #if !defined(TCP_CA_NAME_MAX)
65 #define TCP_CA_NAME_MAX 16
66 #endif /* TCP_CA_NAME_MAX */
67 #endif /* HAVE_TCP_CONGESTION */
68
69 int
iperf_server_listen(struct iperf_test * test)70 iperf_server_listen(struct iperf_test *test)
71 {
72 retry:
73 if((test->listener = netannounce(test->settings->domain, Ptcp, test->bind_address, test->server_port)) < 0) {
74 if (errno == EAFNOSUPPORT && (test->settings->domain == AF_INET6 || test->settings->domain == AF_UNSPEC)) {
75 /* If we get "Address family not supported by protocol", that
76 ** probably means we were compiled with IPv6 but the running
77 ** kernel does not actually do IPv6. This is not too unusual,
78 ** v6 support is and perhaps always will be spotty.
79 */
80 warning("this system does not seem to support IPv6 - trying IPv4");
81 test->settings->domain = AF_INET;
82 goto retry;
83 } else {
84 i_errno = IELISTEN;
85 return -1;
86 }
87 }
88
89 if (!test->json_output) {
90 iperf_printf(test, "-----------------------------------------------------------\n");
91 iperf_printf(test, "Server listening on %d\n", test->server_port);
92 iperf_printf(test, "-----------------------------------------------------------\n");
93 if (test->forceflush)
94 iflush(test);
95 }
96
97 FD_ZERO(&test->read_set);
98 FD_ZERO(&test->write_set);
99 FD_SET(test->listener, &test->read_set);
100 if (test->listener > test->max_fd) test->max_fd = test->listener;
101
102 return 0;
103 }
104
105 int
iperf_accept(struct iperf_test * test)106 iperf_accept(struct iperf_test *test)
107 {
108 int s;
109 signed char rbuf = ACCESS_DENIED;
110 socklen_t len;
111 struct sockaddr_storage addr;
112
113 len = sizeof(addr);
114 if ((s = accept(test->listener, (struct sockaddr *) &addr, &len)) < 0) {
115 i_errno = IEACCEPT;
116 return -1;
117 }
118
119 if (test->ctrl_sck == -1) {
120 /* Server free, accept new client */
121 test->ctrl_sck = s;
122 if (Nread(test->ctrl_sck, test->cookie, COOKIE_SIZE, Ptcp) < 0) {
123 i_errno = IERECVCOOKIE;
124 return -1;
125 }
126 FD_SET(test->ctrl_sck, &test->read_set);
127 if (test->ctrl_sck > test->max_fd) test->max_fd = test->ctrl_sck;
128
129 if (iperf_set_send_state(test, PARAM_EXCHANGE) != 0)
130 return -1;
131 if (iperf_exchange_parameters(test) < 0)
132 return -1;
133 if (test->server_affinity != -1)
134 if (iperf_setaffinity(test, test->server_affinity) != 0)
135 return -1;
136 if (test->on_connect)
137 test->on_connect(test);
138 } else {
139 /*
140 * Don't try to read from the socket. It could block an ongoing test.
141 * Just send ACCESS_DENIED.
142 */
143 if (Nwrite(s, (char*) &rbuf, sizeof(rbuf), Ptcp) < 0) {
144 i_errno = IESENDMESSAGE;
145 return -1;
146 }
147 close(s);
148 }
149
150 return 0;
151 }
152
153
154 /**************************************************************************/
155 int
iperf_handle_message_server(struct iperf_test * test)156 iperf_handle_message_server(struct iperf_test *test)
157 {
158 int rval;
159 struct iperf_stream *sp;
160
161 // XXX: Need to rethink how this behaves to fit API
162 if ((rval = Nread(test->ctrl_sck, (char*) &test->state, sizeof(signed char), Ptcp)) <= 0) {
163 if (rval == 0) {
164 iperf_err(test, "the client has unexpectedly closed the connection");
165 i_errno = IECTRLCLOSE;
166 test->state = IPERF_DONE;
167 return 0;
168 } else {
169 i_errno = IERECVMESSAGE;
170 return -1;
171 }
172 }
173
174 switch(test->state) {
175 case TEST_START:
176 break;
177 case TEST_END:
178 test->done = 1;
179 cpu_util(test->cpu_util);
180 test->stats_callback(test);
181 SLIST_FOREACH(sp, &test->streams, streams) {
182 FD_CLR(sp->socket, &test->read_set);
183 FD_CLR(sp->socket, &test->write_set);
184 close(sp->socket);
185 }
186 test->reporter_callback(test);
187 if (iperf_set_send_state(test, EXCHANGE_RESULTS) != 0)
188 return -1;
189 if (iperf_exchange_results(test) < 0)
190 return -1;
191 if (iperf_set_send_state(test, DISPLAY_RESULTS) != 0)
192 return -1;
193 if (test->on_test_finish)
194 test->on_test_finish(test);
195 break;
196 case IPERF_DONE:
197 break;
198 case CLIENT_TERMINATE:
199 i_errno = IECLIENTTERM;
200
201 // Temporarily be in DISPLAY_RESULTS phase so we can get
202 // ending summary statistics.
203 signed char oldstate = test->state;
204 cpu_util(test->cpu_util);
205 test->state = DISPLAY_RESULTS;
206 test->reporter_callback(test);
207 test->state = oldstate;
208
209 // XXX: Remove this line below!
210 iperf_err(test, "the client has terminated");
211 SLIST_FOREACH(sp, &test->streams, streams) {
212 FD_CLR(sp->socket, &test->read_set);
213 FD_CLR(sp->socket, &test->write_set);
214 close(sp->socket);
215 }
216 test->state = IPERF_DONE;
217 break;
218 default:
219 i_errno = IEMESSAGE;
220 return -1;
221 }
222
223 return 0;
224 }
225
226 static void
server_timer_proc(TimerClientData client_data,struct iperf_time * nowP)227 server_timer_proc(TimerClientData client_data, struct iperf_time *nowP)
228 {
229 struct iperf_test *test = client_data.p;
230 struct iperf_stream *sp;
231
232 test->timer = NULL;
233 if (test->done)
234 return;
235 test->done = 1;
236 /* Free streams */
237 while (!SLIST_EMPTY(&test->streams)) {
238 sp = SLIST_FIRST(&test->streams);
239 SLIST_REMOVE_HEAD(&test->streams, streams);
240 close(sp->socket);
241 iperf_free_stream(sp);
242 }
243 close(test->ctrl_sck);
244 }
245
246 static void
server_stats_timer_proc(TimerClientData client_data,struct iperf_time * nowP)247 server_stats_timer_proc(TimerClientData client_data, struct iperf_time *nowP)
248 {
249 struct iperf_test *test = client_data.p;
250
251 if (test->done)
252 return;
253 if (test->stats_callback)
254 test->stats_callback(test);
255 }
256
257 static void
server_reporter_timer_proc(TimerClientData client_data,struct iperf_time * nowP)258 server_reporter_timer_proc(TimerClientData client_data, struct iperf_time *nowP)
259 {
260 struct iperf_test *test = client_data.p;
261
262 if (test->done)
263 return;
264 if (test->reporter_callback)
265 test->reporter_callback(test);
266 }
267
268 static int
create_server_timers(struct iperf_test * test)269 create_server_timers(struct iperf_test * test)
270 {
271 struct iperf_time now;
272 TimerClientData cd;
273
274 if (iperf_time_now(&now) < 0) {
275 i_errno = IEINITTEST;
276 return -1;
277 }
278 cd.p = test;
279 test->timer = test->stats_timer = test->reporter_timer = NULL;
280 if (test->duration != 0 ) {
281 test->done = 0;
282 test->timer = tmr_create(&now, server_timer_proc, cd, (test->duration + test->omit + 5) * SEC_TO_US, 0);
283 if (test->timer == NULL) {
284 i_errno = IEINITTEST;
285 return -1;
286 }
287 }
288
289 test->stats_timer = test->reporter_timer = NULL;
290 if (test->stats_interval != 0) {
291 test->stats_timer = tmr_create(&now, server_stats_timer_proc, cd, test->stats_interval * SEC_TO_US, 1);
292 if (test->stats_timer == NULL) {
293 i_errno = IEINITTEST;
294 return -1;
295 }
296 }
297 if (test->reporter_interval != 0) {
298 test->reporter_timer = tmr_create(&now, server_reporter_timer_proc, cd, test->reporter_interval * SEC_TO_US, 1);
299 if (test->reporter_timer == NULL) {
300 i_errno = IEINITTEST;
301 return -1;
302 }
303 }
304 return 0;
305 }
306
307 static void
server_omit_timer_proc(TimerClientData client_data,struct iperf_time * nowP)308 server_omit_timer_proc(TimerClientData client_data, struct iperf_time *nowP)
309 {
310 struct iperf_test *test = client_data.p;
311
312 test->omit_timer = NULL;
313 test->omitting = 0;
314 iperf_reset_stats(test);
315 if (test->verbose && !test->json_output && test->reporter_interval == 0)
316 iperf_printf(test, "%s", report_omit_done);
317
318 /* Reset the timers. */
319 if (test->stats_timer != NULL)
320 tmr_reset(nowP, test->stats_timer);
321 if (test->reporter_timer != NULL)
322 tmr_reset(nowP, test->reporter_timer);
323 }
324
325 static int
create_server_omit_timer(struct iperf_test * test)326 create_server_omit_timer(struct iperf_test * test)
327 {
328 struct iperf_time now;
329 TimerClientData cd;
330
331 if (test->omit == 0) {
332 test->omit_timer = NULL;
333 test->omitting = 0;
334 } else {
335 if (iperf_time_now(&now) < 0) {
336 i_errno = IEINITTEST;
337 return -1;
338 }
339 test->omitting = 1;
340 cd.p = test;
341 test->omit_timer = tmr_create(&now, server_omit_timer_proc, cd, test->omit * SEC_TO_US, 0);
342 if (test->omit_timer == NULL) {
343 i_errno = IEINITTEST;
344 return -1;
345 }
346 }
347
348 return 0;
349 }
350
351 static void
cleanup_server(struct iperf_test * test)352 cleanup_server(struct iperf_test *test)
353 {
354 /* Close open test sockets */
355 if (test->ctrl_sck) {
356 close(test->ctrl_sck);
357 }
358 if (test->listener) {
359 close(test->listener);
360 }
361
362 /* Cancel any remaining timers. */
363 if (test->stats_timer != NULL) {
364 tmr_cancel(test->stats_timer);
365 test->stats_timer = NULL;
366 }
367 if (test->reporter_timer != NULL) {
368 tmr_cancel(test->reporter_timer);
369 test->reporter_timer = NULL;
370 }
371 if (test->omit_timer != NULL) {
372 tmr_cancel(test->omit_timer);
373 test->omit_timer = NULL;
374 }
375 if (test->congestion_used != NULL) {
376 free(test->congestion_used);
377 test->congestion_used = NULL;
378 }
379 if (test->timer != NULL) {
380 tmr_cancel(test->timer);
381 test->timer = NULL;
382 }
383 }
384
385
386 int
iperf_run_server(struct iperf_test * test)387 iperf_run_server(struct iperf_test *test)
388 {
389 int result, s;
390 int send_streams_accepted, rec_streams_accepted;
391 int streams_to_send = 0, streams_to_rec = 0;
392 #if defined(HAVE_TCP_CONGESTION)
393 int saved_errno;
394 #endif /* HAVE_TCP_CONGESTION */
395 fd_set read_set, write_set;
396 struct iperf_stream *sp;
397 struct iperf_time now;
398 struct timeval* timeout;
399 int flag;
400
401 if (test->affinity != -1)
402 if (iperf_setaffinity(test, test->affinity) != 0)
403 return -2;
404
405 if (test->json_output)
406 if (iperf_json_start(test) < 0)
407 return -2;
408
409 if (test->json_output) {
410 cJSON_AddItemToObject(test->json_start, "version", cJSON_CreateString(version));
411 cJSON_AddItemToObject(test->json_start, "system_info", cJSON_CreateString(get_system_info()));
412 } else if (test->verbose) {
413 iperf_printf(test, "%s\n", version);
414 iperf_printf(test, "%s", "");
415 iperf_printf(test, "%s\n", get_system_info());
416 iflush(test);
417 }
418
419 // Open socket and listen
420 if (iperf_server_listen(test) < 0) {
421 return -2;
422 }
423
424 // Begin calculating CPU utilization
425 cpu_util(NULL);
426
427 test->state = IPERF_START;
428 send_streams_accepted = 0;
429 rec_streams_accepted = 0;
430
431 while (test->state != IPERF_DONE) {
432
433 memcpy(&read_set, &test->read_set, sizeof(fd_set));
434 memcpy(&write_set, &test->write_set, sizeof(fd_set));
435
436 iperf_time_now(&now);
437 timeout = tmr_timeout(&now);
438 result = select(test->max_fd + 1, &read_set, &write_set, NULL, timeout);
439 if (result < 0 && errno != EINTR) {
440 cleanup_server(test);
441 i_errno = IESELECT;
442 return -1;
443 }
444 if (result > 0) {
445 if (FD_ISSET(test->listener, &read_set)) {
446 if (test->state != CREATE_STREAMS) {
447 if (iperf_accept(test) < 0) {
448 cleanup_server(test);
449 return -1;
450 }
451 FD_CLR(test->listener, &read_set);
452
453 // Set streams number
454 if (test->mode == BIDIRECTIONAL) {
455 streams_to_send = test->num_streams;
456 streams_to_rec = test->num_streams;
457 } else if (test->mode == RECEIVER) {
458 streams_to_rec = test->num_streams;
459 streams_to_send = 0;
460 } else {
461 streams_to_send = test->num_streams;
462 streams_to_rec = 0;
463 }
464 }
465 }
466 if (FD_ISSET(test->ctrl_sck, &read_set)) {
467 if (iperf_handle_message_server(test) < 0) {
468 cleanup_server(test);
469 return -1;
470 }
471 FD_CLR(test->ctrl_sck, &read_set);
472 }
473
474 if (test->state == CREATE_STREAMS) {
475 if (FD_ISSET(test->prot_listener, &read_set)) {
476
477 if ((s = test->protocol->accept(test)) < 0) {
478 cleanup_server(test);
479 return -1;
480 }
481
482 #if defined(HAVE_TCP_CONGESTION)
483 if (test->protocol->id == Ptcp) {
484 if (test->congestion) {
485 if (setsockopt(s, IPPROTO_TCP, TCP_CONGESTION, test->congestion, strlen(test->congestion)) < 0) {
486 /*
487 * ENOENT means we tried to set the
488 * congestion algorithm but the algorithm
489 * specified doesn't exist. This can happen
490 * if the client and server have different
491 * congestion algorithms available. In this
492 * case, print a warning, but otherwise
493 * continue.
494 */
495 if (errno == ENOENT) {
496 warning("TCP congestion control algorithm not supported");
497 }
498 else {
499 saved_errno = errno;
500 close(s);
501 cleanup_server(test);
502 errno = saved_errno;
503 i_errno = IESETCONGESTION;
504 return -1;
505 }
506 }
507 }
508 {
509 socklen_t len = TCP_CA_NAME_MAX;
510 char ca[TCP_CA_NAME_MAX + 1];
511 if (getsockopt(s, IPPROTO_TCP, TCP_CONGESTION, ca, &len) < 0) {
512 saved_errno = errno;
513 close(s);
514 cleanup_server(test);
515 errno = saved_errno;
516 i_errno = IESETCONGESTION;
517 return -1;
518 }
519 test->congestion_used = strdup(ca);
520 if (test->debug) {
521 printf("Congestion algorithm is %s\n", test->congestion_used);
522 }
523 }
524 }
525 #endif /* HAVE_TCP_CONGESTION */
526
527 if (!is_closed(s)) {
528
529 if (rec_streams_accepted != streams_to_rec) {
530 flag = 0;
531 ++rec_streams_accepted;
532 } else if (send_streams_accepted != streams_to_send) {
533 flag = 1;
534 ++send_streams_accepted;
535 }
536
537 if (flag != -1) {
538 sp = iperf_new_stream(test, s, flag);
539 if (!sp) {
540 cleanup_server(test);
541 return -1;
542 }
543
544 if (sp->sender)
545 FD_SET(s, &test->write_set);
546 else
547 FD_SET(s, &test->read_set);
548
549 if (s > test->max_fd) test->max_fd = s;
550
551 /*
552 * If the protocol isn't UDP, or even if it is but
553 * we're the receiver, set nonblocking sockets.
554 * We need this to allow a server receiver to
555 * maintain interactivity with the control channel.
556 */
557 if (test->protocol->id != Pudp ||
558 !sp->sender) {
559 setnonblocking(s, 1);
560 }
561
562 if (test->on_new_stream)
563 test->on_new_stream(sp);
564
565 flag = -1;
566 }
567 }
568 FD_CLR(test->prot_listener, &read_set);
569 }
570
571
572 if (rec_streams_accepted == streams_to_rec && send_streams_accepted == streams_to_send) {
573 if (test->protocol->id != Ptcp) {
574 FD_CLR(test->prot_listener, &test->read_set);
575 close(test->prot_listener);
576 } else {
577 if (test->no_delay || test->settings->mss || test->settings->socket_bufsize) {
578 FD_CLR(test->listener, &test->read_set);
579 close(test->listener);
580 test->listener = 0;
581 if ((s = netannounce(test->settings->domain, Ptcp, test->bind_address, test->server_port)) < 0) {
582 cleanup_server(test);
583 i_errno = IELISTEN;
584 return -1;
585 }
586 test->listener = s;
587 FD_SET(test->listener, &test->read_set);
588 if (test->listener > test->max_fd) test->max_fd = test->listener;
589 }
590 }
591 test->prot_listener = -1;
592 if (iperf_set_send_state(test, TEST_START) != 0) {
593 cleanup_server(test);
594 return -1;
595 }
596 if (iperf_init_test(test) < 0) {
597 cleanup_server(test);
598 return -1;
599 }
600 if (create_server_timers(test) < 0) {
601 cleanup_server(test);
602 return -1;
603 }
604 if (create_server_omit_timer(test) < 0) {
605 cleanup_server(test);
606 return -1;
607 }
608 if (test->mode != RECEIVER)
609 if (iperf_create_send_timers(test) < 0) {
610 cleanup_server(test);
611 return -1;
612 }
613 if (iperf_set_send_state(test, TEST_RUNNING) != 0) {
614 cleanup_server(test);
615 return -1;
616 }
617 }
618 }
619
620 if (test->state == TEST_RUNNING) {
621 if (test->mode == BIDIRECTIONAL) {
622 if (iperf_recv(test, &read_set) < 0) {
623 cleanup_server(test);
624 return -1;
625 }
626 if (iperf_send(test, &write_set) < 0) {
627 cleanup_server(test);
628 return -1;
629 }
630 } else if (test->mode == SENDER) {
631 // Reverse mode. Server sends.
632 if (iperf_send(test, &write_set) < 0) {
633 cleanup_server(test);
634 return -1;
635 }
636 } else {
637 // Regular mode. Server receives.
638 if (iperf_recv(test, &read_set) < 0) {
639 cleanup_server(test);
640 return -1;
641 }
642 }
643 }
644 }
645
646 if (result == 0 ||
647 (timeout != NULL && timeout->tv_sec == 0 && timeout->tv_usec == 0)) {
648 /* Run the timers. */
649 iperf_time_now(&now);
650 tmr_run(&now);
651 }
652 }
653
654 cleanup_server(test);
655
656 if (test->json_output) {
657 if (iperf_json_finish(test) < 0)
658 return -1;
659 }
660
661 iflush(test);
662
663 if (test->server_affinity != -1)
664 if (iperf_clearaffinity(test) != 0)
665 return -1;
666
667 return 0;
668 }
669