• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     int max_rtt = 4; /* seconds */
274     int state_transitions = 10; /* number of state transitions in iperf3 */
275     int grace_period = max_rtt * state_transitions;
276 
277     if (iperf_time_now(&now) < 0) {
278 	i_errno = IEINITTEST;
279 	return -1;
280     }
281     cd.p = test;
282     test->timer = test->stats_timer = test->reporter_timer = NULL;
283     if (test->duration != 0 ) {
284         test->done = 0;
285         test->timer = tmr_create(&now, server_timer_proc, cd, (test->duration + test->omit + grace_period) * SEC_TO_US, 0);
286         if (test->timer == NULL) {
287             i_errno = IEINITTEST;
288             return -1;
289         }
290     }
291 
292     test->stats_timer = test->reporter_timer = NULL;
293     if (test->stats_interval != 0) {
294         test->stats_timer = tmr_create(&now, server_stats_timer_proc, cd, test->stats_interval * SEC_TO_US, 1);
295         if (test->stats_timer == NULL) {
296             i_errno = IEINITTEST;
297             return -1;
298 	}
299     }
300     if (test->reporter_interval != 0) {
301         test->reporter_timer = tmr_create(&now, server_reporter_timer_proc, cd, test->reporter_interval * SEC_TO_US, 1);
302         if (test->reporter_timer == NULL) {
303             i_errno = IEINITTEST;
304             return -1;
305 	}
306     }
307     return 0;
308 }
309 
310 static void
server_omit_timer_proc(TimerClientData client_data,struct iperf_time * nowP)311 server_omit_timer_proc(TimerClientData client_data, struct iperf_time *nowP)
312 {
313     struct iperf_test *test = client_data.p;
314 
315     test->omit_timer = NULL;
316     test->omitting = 0;
317     iperf_reset_stats(test);
318     if (test->verbose && !test->json_output && test->reporter_interval == 0)
319 	iperf_printf(test, "%s", report_omit_done);
320 
321     /* Reset the timers. */
322     if (test->stats_timer != NULL)
323 	tmr_reset(nowP, test->stats_timer);
324     if (test->reporter_timer != NULL)
325 	tmr_reset(nowP, test->reporter_timer);
326 }
327 
328 static int
create_server_omit_timer(struct iperf_test * test)329 create_server_omit_timer(struct iperf_test * test)
330 {
331     struct iperf_time now;
332     TimerClientData cd;
333 
334     if (test->omit == 0) {
335 	test->omit_timer = NULL;
336 	test->omitting = 0;
337     } else {
338 	if (iperf_time_now(&now) < 0) {
339 	    i_errno = IEINITTEST;
340 	    return -1;
341 	}
342 	test->omitting = 1;
343 	cd.p = test;
344 	test->omit_timer = tmr_create(&now, server_omit_timer_proc, cd, test->omit * SEC_TO_US, 0);
345 	if (test->omit_timer == NULL) {
346 	    i_errno = IEINITTEST;
347 	    return -1;
348 	}
349     }
350 
351     return 0;
352 }
353 
354 static void
cleanup_server(struct iperf_test * test)355 cleanup_server(struct iperf_test *test)
356 {
357     /* Close open test sockets */
358     if (test->ctrl_sck) {
359 	close(test->ctrl_sck);
360     }
361     if (test->listener) {
362 	close(test->listener);
363     }
364 
365     /* Cancel any remaining timers. */
366     if (test->stats_timer != NULL) {
367 	tmr_cancel(test->stats_timer);
368 	test->stats_timer = NULL;
369     }
370     if (test->reporter_timer != NULL) {
371 	tmr_cancel(test->reporter_timer);
372 	test->reporter_timer = NULL;
373     }
374     if (test->omit_timer != NULL) {
375 	tmr_cancel(test->omit_timer);
376 	test->omit_timer = NULL;
377     }
378     if (test->congestion_used != NULL) {
379         free(test->congestion_used);
380 	test->congestion_used = NULL;
381     }
382     if (test->timer != NULL) {
383         tmr_cancel(test->timer);
384         test->timer = NULL;
385     }
386 }
387 
388 
389 int
iperf_run_server(struct iperf_test * test)390 iperf_run_server(struct iperf_test *test)
391 {
392     int result, s;
393     int send_streams_accepted, rec_streams_accepted;
394     int streams_to_send = 0, streams_to_rec = 0;
395 #if defined(HAVE_TCP_CONGESTION)
396     int saved_errno;
397 #endif /* HAVE_TCP_CONGESTION */
398     fd_set read_set, write_set;
399     struct iperf_stream *sp;
400     struct iperf_time now;
401     struct timeval* timeout;
402     int flag;
403 
404     if (test->logfile)
405         if (iperf_open_logfile(test) < 0)
406             return -1;
407 
408     if (test->affinity != -1)
409 	if (iperf_setaffinity(test, test->affinity) != 0)
410 	    return -2;
411 
412     if (test->json_output)
413 	if (iperf_json_start(test) < 0)
414 	    return -2;
415 
416     if (test->json_output) {
417 	cJSON_AddItemToObject(test->json_start, "version", cJSON_CreateString(version));
418 	cJSON_AddItemToObject(test->json_start, "system_info", cJSON_CreateString(get_system_info()));
419     } else if (test->verbose) {
420 	iperf_printf(test, "%s\n", version);
421 	iperf_printf(test, "%s", "");
422 	iperf_printf(test, "%s\n", get_system_info());
423 	iflush(test);
424     }
425 
426     // Open socket and listen
427     if (iperf_server_listen(test) < 0) {
428         return -2;
429     }
430 
431     // Begin calculating CPU utilization
432     cpu_util(NULL);
433 
434     test->state = IPERF_START;
435     send_streams_accepted = 0;
436     rec_streams_accepted = 0;
437 
438     while (test->state != IPERF_DONE) {
439 
440         memcpy(&read_set, &test->read_set, sizeof(fd_set));
441         memcpy(&write_set, &test->write_set, sizeof(fd_set));
442 
443 	iperf_time_now(&now);
444 	timeout = tmr_timeout(&now);
445         result = select(test->max_fd + 1, &read_set, &write_set, NULL, timeout);
446         if (result < 0 && errno != EINTR) {
447 	    cleanup_server(test);
448             i_errno = IESELECT;
449             return -1;
450         }
451 	if (result > 0) {
452             if (FD_ISSET(test->listener, &read_set)) {
453                 if (test->state != CREATE_STREAMS) {
454                     if (iperf_accept(test) < 0) {
455 			cleanup_server(test);
456                         return -1;
457                     }
458                     FD_CLR(test->listener, &read_set);
459 
460                     // Set streams number
461                     if (test->mode == BIDIRECTIONAL) {
462                         streams_to_send = test->num_streams;
463                         streams_to_rec = test->num_streams;
464                     } else if (test->mode == RECEIVER) {
465                         streams_to_rec = test->num_streams;
466                         streams_to_send = 0;
467                     } else {
468                         streams_to_send = test->num_streams;
469                         streams_to_rec = 0;
470                     }
471                 }
472             }
473             if (FD_ISSET(test->ctrl_sck, &read_set)) {
474                 if (iperf_handle_message_server(test) < 0) {
475 		    cleanup_server(test);
476                     return -1;
477 		}
478                 FD_CLR(test->ctrl_sck, &read_set);
479             }
480 
481             if (test->state == CREATE_STREAMS) {
482                 if (FD_ISSET(test->prot_listener, &read_set)) {
483 
484                     if ((s = test->protocol->accept(test)) < 0) {
485 			cleanup_server(test);
486                         return -1;
487 		    }
488 
489 #if defined(HAVE_TCP_CONGESTION)
490 		    if (test->protocol->id == Ptcp) {
491 			if (test->congestion) {
492 			    if (setsockopt(s, IPPROTO_TCP, TCP_CONGESTION, test->congestion, strlen(test->congestion)) < 0) {
493 				/*
494 				 * ENOENT means we tried to set the
495 				 * congestion algorithm but the algorithm
496 				 * specified doesn't exist.  This can happen
497 				 * if the client and server have different
498 				 * congestion algorithms available.  In this
499 				 * case, print a warning, but otherwise
500 				 * continue.
501 				 */
502 				if (errno == ENOENT) {
503 				    warning("TCP congestion control algorithm not supported");
504 				}
505 				else {
506 				    saved_errno = errno;
507 				    close(s);
508 				    cleanup_server(test);
509 				    errno = saved_errno;
510 				    i_errno = IESETCONGESTION;
511 				    return -1;
512 				}
513 			    }
514 			}
515 			{
516 			    socklen_t len = TCP_CA_NAME_MAX;
517 			    char ca[TCP_CA_NAME_MAX + 1];
518 			    if (getsockopt(s, IPPROTO_TCP, TCP_CONGESTION, ca, &len) < 0) {
519 				saved_errno = errno;
520 				close(s);
521 				cleanup_server(test);
522 				errno = saved_errno;
523 				i_errno = IESETCONGESTION;
524 				return -1;
525 			    }
526 			    test->congestion_used = strdup(ca);
527 			    if (test->debug) {
528 				printf("Congestion algorithm is %s\n", test->congestion_used);
529 			    }
530 			}
531 		    }
532 #endif /* HAVE_TCP_CONGESTION */
533 
534                     if (!is_closed(s)) {
535 
536                         if (rec_streams_accepted != streams_to_rec) {
537                             flag = 0;
538                             ++rec_streams_accepted;
539                         } else if (send_streams_accepted != streams_to_send) {
540                             flag = 1;
541                             ++send_streams_accepted;
542                         }
543 
544                         if (flag != -1) {
545                             sp = iperf_new_stream(test, s, flag);
546                             if (!sp) {
547                                 cleanup_server(test);
548                                 return -1;
549                             }
550 
551                             if (sp->sender)
552                                 FD_SET(s, &test->write_set);
553                             else
554                                 FD_SET(s, &test->read_set);
555 
556                             if (s > test->max_fd) test->max_fd = s;
557 
558                             /*
559                              * If the protocol isn't UDP, or even if it is but
560                              * we're the receiver, set nonblocking sockets.
561                              * We need this to allow a server receiver to
562                              * maintain interactivity with the control channel.
563                              */
564                             if (test->protocol->id != Pudp ||
565                                 !sp->sender) {
566                                 setnonblocking(s, 1);
567                             }
568 
569                             if (test->on_new_stream)
570                                 test->on_new_stream(sp);
571 
572                             flag = -1;
573                         }
574                     }
575                     FD_CLR(test->prot_listener, &read_set);
576                 }
577 
578 
579                 if (rec_streams_accepted == streams_to_rec && send_streams_accepted == streams_to_send) {
580                     if (test->protocol->id != Ptcp) {
581                         FD_CLR(test->prot_listener, &test->read_set);
582                         close(test->prot_listener);
583                     } else {
584                         if (test->no_delay || test->settings->mss || test->settings->socket_bufsize) {
585                             FD_CLR(test->listener, &test->read_set);
586                             close(test->listener);
587 			    test->listener = 0;
588                             if ((s = netannounce(test->settings->domain, Ptcp, test->bind_address, test->server_port)) < 0) {
589 				cleanup_server(test);
590                                 i_errno = IELISTEN;
591                                 return -1;
592                             }
593                             test->listener = s;
594                             FD_SET(test->listener, &test->read_set);
595 			    if (test->listener > test->max_fd) test->max_fd = test->listener;
596                         }
597                     }
598                     test->prot_listener = -1;
599 		    if (iperf_set_send_state(test, TEST_START) != 0) {
600 			cleanup_server(test);
601                         return -1;
602 		    }
603                     if (iperf_init_test(test) < 0) {
604 			cleanup_server(test);
605                         return -1;
606 		    }
607 		    if (create_server_timers(test) < 0) {
608 			cleanup_server(test);
609                         return -1;
610 		    }
611 		    if (create_server_omit_timer(test) < 0) {
612 			cleanup_server(test);
613                         return -1;
614 		    }
615 		    if (test->mode != RECEIVER)
616 			if (iperf_create_send_timers(test) < 0) {
617 			    cleanup_server(test);
618 			    return -1;
619 			}
620 		    if (iperf_set_send_state(test, TEST_RUNNING) != 0) {
621 			cleanup_server(test);
622                         return -1;
623 		    }
624                 }
625             }
626 
627             if (test->state == TEST_RUNNING) {
628                 if (test->mode == BIDIRECTIONAL) {
629                     if (iperf_recv(test, &read_set) < 0) {
630                         cleanup_server(test);
631                         return -1;
632                     }
633                     if (iperf_send(test, &write_set) < 0) {
634                         cleanup_server(test);
635                         return -1;
636                     }
637                 } else if (test->mode == SENDER) {
638                     // Reverse mode. Server sends.
639                     if (iperf_send(test, &write_set) < 0) {
640 			cleanup_server(test);
641                         return -1;
642 		    }
643                 } else {
644                     // Regular mode. Server receives.
645                     if (iperf_recv(test, &read_set) < 0) {
646 			cleanup_server(test);
647                         return -1;
648 		    }
649                 }
650             }
651         }
652 
653 	if (result == 0 ||
654 	    (timeout != NULL && timeout->tv_sec == 0 && timeout->tv_usec == 0)) {
655 	    /* Run the timers. */
656 	    iperf_time_now(&now);
657 	    tmr_run(&now);
658 	}
659     }
660 
661     cleanup_server(test);
662 
663     if (test->json_output) {
664 	if (iperf_json_finish(test) < 0)
665 	    return -1;
666     }
667 
668     iflush(test);
669 
670     if (test->server_affinity != -1)
671 	if (iperf_clearaffinity(test) != 0)
672 	    return -1;
673 
674     return 0;
675 }
676