3 #include <sys/socket.h>
4 #include <netinet/in.h>
19 #include <semaphore.h>
20 #include <sys/ioctl.h>
24 #include <fwp_confdefs.h>
30 #define MAX_STREAMS 10
31 #define MIN_GRANULARITY 100
33 unsigned opt_packet_size = 800;
34 int opt_send_buf_size = -1;
35 unsigned opt_period_usec = 10*MSEC_TO_USEC;
37 unsigned opt_jitter = 0;
38 char *opt_output = "delay_stats";
39 unsigned opt_count_sec = 0;
40 unsigned opt_def_bandwidth = 50;
41 unsigned opt_def_period_msec = 0;
42 int opt_granularity_usec = MIN_GRANULARITY;
43 bool opt_wait_for_queue_is_full; /* Don't gather any statistics until any queue is full */
44 char *opt_comment = NULL;
46 bool some_queue_is_full = false;
47 struct timespec reset_timestamp;
49 bool some_contract_not_accepted = false;
51 /* Locked when some queue is full to prevent multiple resets of
53 pthread_mutex_t queue_full_mutex = PTHREAD_MUTEX_INITIALIZER;
55 int ac_sockfd[AC_NUM];
60 unsigned received, last_received;
63 struct receiver receivers[AC_NUM];
69 /* maximal traffic delay in ms - 10 s*/
70 #define MAX_DELAY_US 10000000
73 unsigned csc; /* Client-server-client delay divided by 2 */
74 unsigned cs; /* Client-server delay */
75 unsigned sc; /* Server-client delay */
78 struct delay_stat delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
79 pthread_mutex_t delay_stats_mutex = PTHREAD_MUTEX_INITIALIZER;
81 /*struct ac_stats[AC_NUM] {
82 unsigned long int min_trans_time;
83 unsigned long int sum_trans_time;
84 struct timespec recv_timestamp;
85 struct timespec send_timestamp;
89 /* Input parameters */
91 int bandwidth_bps; /* bits per second */
92 int jitter; /* percent */
93 /* Mulualy exclusive input parameters */
95 long period_usec; /* all time units are in microseconds */
99 struct sockaddr_in rem_addr;
101 fwp_contract_d_t contract_send;
102 fwp_contract_d_t contract_resp;
103 fwp_endpoint_d_t endpoint;
104 fwp_endpoint_d_t resp_endpoint;
107 struct receiver receiver;
108 long wc_delay; /* worst-case delay */
112 pthread_mutex_t mutex;
113 unsigned long long sent, really_sent, received;
116 static struct cmsg_ipi {
118 struct in_pktinfo ipi;
119 } cmsg = { {sizeof(struct cmsg_ipi), SOL_IP, IP_PKTINFO},
124 struct send_endpoint sepoint[] = {
125 { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
126 { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps = 480*Kbit },
127 { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
128 { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
129 // { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps = 675*Kbit },
133 struct stream streams[MAX_STREAMS];
135 unsigned int nr_streams = 0;
137 sem_t sem_thread_finished;
139 bool exit_flag = false;
142 #define negotiate_contract_for_stream(s) negotiate_contract_for_stream_fwp(s)
143 #define create_stream_endpoint(s) create_stream_endpoint_fwp(s)
144 #define send_packet(s, b) send_packet_fwp(s, b)
145 #define recv_packet(s, b) recv_packet_fwp(s, b)
146 #define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_fwp()
148 #define negotiate_contract_for_stream(s) 0
149 #define create_stream_endpoint(s) create_stream_endpoint_native(s)
150 #define send_packet(s, b) send_packet_native(s, b)
151 #define recv_packet(s, b) recv_packet_native(s, b)
152 #define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_native()
160 /* Interrupt all receivers */
162 for (i=0; i < nr_streams; i++) {
163 if (streams[i].receiver.valid) pthread_kill(streams[i].receiver.thread, SIGUSR1);
164 if (streams[i].contract_send) fwp_contract_cancel(streams[i].contract_send);
165 if (streams[i].contract_resp)fwp_contract_cancel(streams[i].contract_resp);
168 for (i=0; i < AC_NUM; i++) {
169 pthread_kill(receivers[i].thread, SIGUSR1);
174 void stream_to_text(char *stream_desc, size_t n, struct stream *stream, long long useconds)
180 snprintf(real, sizeof(real), "; real: %s sent %lld (%lld/s), received %lld (%lld/s)",
181 bandwidth_to_text(buf[0], (long long)stream->really_sent*stream->packet_size*8*SEC_TO_USEC/useconds),
182 stream->sent, stream->sent*SEC_TO_USEC/useconds,
183 stream->received, stream->received*SEC_TO_USEC/useconds);
188 snprintf(stream_desc, n, "%d: %s %s (%d bytes per %s +-%s, %d packets/s)%s",
189 stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
190 stream->packet_size, usec_to_text(buf[1], stream->period_usec),
191 usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
192 (int)(SEC_TO_USEC/stream->period_usec), real);
195 void save_results(int argc, char *argv[], int useconds)
198 const int mini = 3000/opt_granularity_usec;
200 unsigned send_count[AC_NUM];
202 fprintf(stderr, "Writing data to %s... ", logfname);
205 fprintf(logfd, "# Invoked as: ");
206 for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
208 fprintf(logfd, "(%s)", opt_comment);
210 fprintf(logfd, "\n");
212 if (useconds/SEC_TO_USEC != opt_count_sec) {
214 usec_to_text(buf, useconds);
215 fprintf(logfd, "# Data gathered for %s.\n", buf);
218 for (i = 0; i < nr_streams; i++) {
219 char stream_desc[200];
220 stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], useconds);
221 fprintf(logfd, "# Stream %s\n", stream_desc);
224 /* Find maximal delay */
226 for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
227 for (ac = 0; ac < AC_NUM; ac++) {
228 if ((delay_stats[ac][maxi].csc != 0) ||
229 (delay_stats[ac][maxi].cs != 0) ||
230 (delay_stats[ac][maxi].sc != 0))
233 if (!allzeros) break;
236 if (maxi < mini) maxi = mini;
238 /* Calculate total number of sent packets per AC */
239 memset(send_count, 0, sizeof(send_count));
240 for (i = 0; i < nr_streams; i++) {
242 send_count[ac] += streams[i].sent;
247 for ( i = 0 ; i < maxi; i++) {
248 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
249 for (ac = 0; ac < AC_NUM; ac++) {
251 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
252 else val = -1; /* Don't display this ac */
253 fprintf(logfd," %lf", val);
257 fprintf(logfd,"\n\n");
260 fprintf(logfd,"## Format: msec csc%% cs%% sc%%\n");
263 for (ac = 0; ac < AC_NUM; ac++) {
264 struct delay_stat integral = {0,0,0}, last = {-1,-1,-1};
266 fprintf(logfd,"%f %f %f %f\n", 0.0, 0.0, 0.0, 0.0);
268 if (send_count[ac] != 0) {
270 while ((delay_stats[ac][i].csc == 0) &&
271 (delay_stats[ac][i].cs == 0) &&
272 (delay_stats[ac][i].sc == 0)) i++;
274 for (i++; i < maxi+1; i++) {
275 if (memcmp(&last, &integral, sizeof(last))) {
277 snprintf(buf[0], sizeof(buf[0]), "%f", (double)integral.csc*100.0 / send_count[ac]);
278 snprintf(buf[1], sizeof(buf[1]), "%f", (double)integral.cs *100.0 / send_count[ac]);
279 snprintf(buf[2], sizeof(buf[2]), "%f", (double)integral.sc *100.0 / send_count[ac]);
281 fprintf(logfd,"%f %s %s %s\n", i*opt_granularity_usec/1000.0,
282 integral.csc != last.csc ? buf[0] : "-",
283 integral.cs != last.cs ? buf[1] : "-",
284 integral.sc != last.sc ? buf[2] : "-"
289 integral.csc += delay_stats[ac][i-1].csc;
290 integral.sc += delay_stats[ac][i-1].sc;
291 integral.cs += delay_stats[ac][i-1].cs;
295 fprintf(logfd,"\n\n");
298 fprintf(stderr, "finished.\n");
304 int create_ac_socket(unsigned int ac)
307 unsigned int yes=1, tos;
310 if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
312 perror("Unable to open socket");
315 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
316 perror("set non-blocking socket");
319 if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
320 perror("Unable to set socket");
324 if (opt_send_buf_size >= 0) {
325 if (setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&opt_send_buf_size,sizeof(opt_send_buf_size)) == -1) {
326 perror("Unable to set socket buffer size");
332 //tos = ((AC_NUM - ac) *2 - 1)*32;
334 if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
335 perror("Unable to set TOS");
347 void reset_statistics()
350 for (i = 0; i < nr_streams; i++) {
351 pthread_mutex_lock(&streams[i].mutex);
353 streams[i].really_sent = 0;
354 streams[i].received = 0;
355 pthread_mutex_unlock(&streams[i].mutex);
357 pthread_mutex_lock(&delay_stats_mutex);
358 clock_gettime(CLOCK_REALTIME, &reset_timestamp);
359 memset(delay_stats, 0, sizeof(delay_stats));
360 pthread_mutex_unlock(&delay_stats_mutex);
364 int recv_packet_native(unsigned ac, struct msg_t *msg)
368 struct sockaddr_in rem_addr;
369 unsigned int rem_addr_length;
372 FD_SET(ac_sockfd[ac], &fdset);
373 rem_addr_length = sizeof(rem_addr);
376 ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
378 if (errno == EINTR) continue;
379 perror("receiver select");
382 mlen = recvfrom(ac_sockfd[ac], msg, sizeof(*msg), 0,
383 (struct sockaddr*)&rem_addr, &rem_addr_length);
389 int recv_packet_fwp(struct stream *stream, struct msg_t *msg)
392 mlen = fwp_recv(stream->resp_endpoint, msg, sizeof(*msg), 0);
397 void* receiver(void* arg)
400 long long int trans_time_usec, client_to_server_usec, server_to_client_usec;
401 long long int min_trans_time;
402 struct timespec send_timestamp, server_timestamp, recv_timestamp;
413 struct stream *stream = arg;
415 mlen = recv_packet_fwp(stream, &msg);
418 mlen = recv_packet_native(ac, &msg);
421 perror("Chyba pri prijimani pozadavku");
424 clock_gettime(CLOCK_REALTIME,&recv_timestamp);
425 send_timestamp = msg.send_timestamp;
426 server_timestamp = msg.sendback_timestamp;
428 /* Check whether this message was sent after reset_statistics() */
430 if ((ret = timespec_sub_usec(&send_timestamp, &reset_timestamp)) < 0) {
431 continue; /* If so, don't count it */
434 trans_time_usec = timespec_sub_usec(&recv_timestamp ,&send_timestamp) / 2;
435 client_to_server_usec = timespec_sub_usec(&server_timestamp, &send_timestamp);
436 server_to_client_usec = timespec_sub_usec(&recv_timestamp, &server_timestamp);
438 pthread_mutex_lock(&delay_stats_mutex);
439 if (trans_time_usec < MAX_DELAY_US && trans_time_usec >= 0) {
440 delay_stats[ac][trans_time_usec/opt_granularity_usec].csc++;
442 if (client_to_server_usec < MAX_DELAY_US && client_to_server_usec >= 0) {
443 delay_stats[ac][client_to_server_usec/opt_granularity_usec].cs++;
445 if (server_to_client_usec < MAX_DELAY_US && server_to_client_usec >= 0) {
446 delay_stats[ac][server_to_client_usec/opt_granularity_usec].sc++;
448 pthread_mutex_unlock(&delay_stats_mutex);
451 if (trans_time_usec > stream->wc_delay) {
452 stream->wc_delay = trans_time_usec;
455 receivers[ac].received++;
457 pthread_mutex_lock(&streams[msg.stream].mutex);
458 streams[msg.stream].received++;
459 pthread_mutex_unlock(&streams[msg.stream].mutex);
461 /*if (trans_time_nsec < min_trans_time)
462 min_trans_time = trans_time_nsec;*/
463 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
464 "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
465 msg.seqn, msg.tos, send_timestamp.tv_sec,\
466 send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
467 recv_timestamp.tv_nsec, trans_time_msec); */
470 sem_post(&sem_thread_finished);
477 * @return -1 in case of error, 1 in case of sucessfull send and 0
478 * when all buffers are full.
482 send_packet_native(struct stream* stream, union msg_buff* buff)
488 iov.iov_len = stream->packet_size;
489 msg.msg_name = (void*)&stream->rem_addr;
490 msg.msg_namelen = sizeof(stream->rem_addr);
494 msg.msg_control = &cmsg;
495 msg.msg_controllen = cmsg_len;
499 while (sendmsg(ac_sockfd[stream->ac], &msg, 0) < 0) {
500 if (errno == EINTR) continue;
501 if (errno == EAGAIN) {
502 if (opt_wait_for_queue_is_full &&
503 !some_queue_is_full &&
504 /* We use mutex as atomic test and set */
505 (pthread_mutex_trylock(&queue_full_mutex) != EBUSY)) {
506 some_queue_is_full = true;
512 perror("Error while sending");
521 send_packet_fwp(struct stream* stream, union msg_buff* buff)
525 buff->msg.resp_port = htons(stream->resp_port);
526 ret = fwp_send(stream->endpoint, buff, stream->packet_size, 0);
533 wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
535 struct timespec time_to_wait, current_time, period, interval;
536 unsigned period_usec = stream->period_usec;
538 /* |~~~+~~~| jitter interval (width = 2*stream->jitter percentage from period)*/
539 /* |-------------| nominal period*/
540 if (stream->jitter) {
541 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-stream->jitter)/100
542 + rand() % (2*period_usec*stream->jitter/100));
544 period.tv_nsec = USEC_TO_NSEC*(period_usec);
548 timespec_add(&time_to_wait, last_send_time, &period);
549 clock_gettime(CLOCK_REALTIME,¤t_time);
550 timespec_sub(&interval,&time_to_wait,¤t_time);
551 nanosleep(&interval,NULL);
555 void* sender(void* arg)
558 unsigned long int seqn;
559 struct stream* stream = (struct stream*) arg;
560 char stream_desc[100];
563 stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
564 printf("%s\n", stream_desc);
566 if (stream->bandwidth_bps == 0)
572 set_rt_prio(90-stream->ac);
576 /* buff.msg.seqn = seqn++; */
577 /* buff.msg.tos = ac_to_tos[stream->ac]; */
578 buff.msg.stream = stream-streams;
580 clock_gettime(CLOCK_REALTIME,&buff.msg.send_timestamp);
582 ret = send_packet(stream, &buff);
587 pthread_mutex_lock(&stream->mutex);
590 stream->really_sent++;
591 pthread_mutex_unlock(&stream->mutex);
594 printf("%d", stream->ac);
598 wait_for_next_send(stream, &buff.msg.send_timestamp);
601 sem_post(&sem_thread_finished);
606 static int negotiate_contract_for_stream_fwp(struct stream *stream)
608 fwp_contract_t contract;
612 /* Contract for client->server stream */
613 memset(&contract, 0, sizeof(contract));
614 contract.budget = stream->packet_size;
615 contract.period_usec = stream->period_usec;
616 contract.deadline_usec = 3*stream->period_usec;
618 stream->contract_send = fwp_contract_create(&contract);
619 ret = fwp_contract_negotiate(stream->contract_send, &stream->vres);
621 stream->contract_send = NULL;
622 fprintf(stderr, "Send contract was not accepted\n\n\n");
626 /* Contract for server->client stream */
627 memset(&contract, 0, sizeof(contract));
628 contract.budget = stream->packet_size;
629 contract.period_usec = stream->period_usec;
630 contract.deadline_usec = 3*stream->period_usec;
632 stream->contract_resp = fwp_contract_create(&contract);
633 ret = fwp_contract_negotiate(stream->contract_resp, &vres2);
635 stream->contract_resp = NULL;
636 fprintf(stderr, "Receive contract was not accepted\n\n\n");
640 /* We don't use the vres at server, since the server doesn't
641 * know the parameters. Instread, server uses plain
649 static void create_stream_endpoint_fwp(struct stream *stream)
651 /* fwp_endpoint_attr_t attr; */
655 stream->ac = fwp_vres_get_ac(stream->vres);
657 /* fwp_endpoint_attr_init(&attr); */
658 /* fwp_endpoint_attr_setreliability(&attr, FWP_EPOINT_BESTEFFORT); */
660 ph = gethostbyname(server_addr);
661 if (ph && ph->h_addr_list[0]) {
662 struct in_addr *a = (struct in_addr *)(ph->h_addr_list[0]);
663 ret = fwp_send_endpoint_create(a->s_addr, BASE_PORT + stream->ac,
664 NULL, &stream->endpoint);
665 /* stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac); */
667 perror("fwp_send_endpoint_create");
670 ret = fwp_send_endpoint_bind(stream->endpoint, stream->vres);
672 perror("fwp_send_endpoint_bind");
675 ret = fwp_receive_endpoint_create(0, NULL, &stream->resp_endpoint);
677 perror("fwp_receive_endpoint_create");
681 fwp_endpoint_get_params(stream->resp_endpoint, NULL, &port, NULL);
682 stream->resp_port = port;
684 ret = pthread_create(&stream->receiver.thread, NULL, receiver, (void*)stream);
686 perror("Error while creating receiver\n");
689 stream->receiver.valid = true;
693 snprintf(str, sizeof(str), "gethostbyname(%s)", server_addr);
700 static void create_stream_endpoint_native(struct stream *stream)
704 memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
706 stream->rem_addr.sin_family = AF_INET;
707 ph = gethostbyname(server_addr);
709 stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
712 snprintf(str, sizeof(str), "gethostbyname(%s)", server_addr);
716 stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
721 calc_stream_params(struct stream *stream)
724 unsigned period_usec;
728 /* If some parameters are not set explicitely, use default values. */
729 if (stream->bandwidth_bps < 0) stream->bandwidth_bps = opt_def_bandwidth * Kbit;
730 if (stream->packet_size < 0) stream->packet_size = opt_packet_size;
731 if (stream->period_usec < 0) stream->period_usec = opt_def_period_msec * MSEC_TO_USEC;
733 bandwidth = stream->bandwidth_bps;
735 /* Avoid arithmetic exception. Server thread will exit if
736 stream->bandwidth_bps == 0. */
737 if (bandwidth == 0) bandwidth = 1;
739 if (stream->packet_size) {
740 packet_size = stream->packet_size;
741 period_usec = SEC_TO_USEC*packet_size*8/bandwidth;
742 if (period_usec == 0) period_usec = 1;
743 } else if (stream->period_usec) {
744 period_usec = stream->period_usec;
745 packet_size = (long long)bandwidth/8 * period_usec/SEC_TO_USEC;
748 stream_to_text(buf, sizeof(buf), stream, 0);
749 fprintf(stderr, "Neither packet size nor period was specified for a stream %s\n", buf);
753 if (packet_size < sizeof(struct msg_t)) {
754 fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
758 stream->packet_size = packet_size;
759 stream->period_usec = period_usec;
760 stream->jitter = opt_jitter;
762 ret = negotiate_contract_for_stream(stream);
764 create_stream_endpoint(stream);
767 stream_to_text(buf, sizeof(buf), stream, 0);
768 fprintf(stderr, "Contract hasn't been accepted:\n%s\n\n", buf);
769 stream->bandwidth_bps = 0;
770 some_contract_not_accepted = true;
775 * Parse -b parameter.
777 * @param params String to parse
779 * @return NULL in case of success, pointer to a problematic character
782 char* parse_bandwidths(char *params)
784 struct stream *sp = &streams[nr_streams];
786 while (*params && nr_streams < MAX_STREAMS) {
787 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
791 if (strlen(params) < 2)
793 for (i=0; i<AC_NUM; i++) {
794 if (strncmp(params, ac_ids[i], 2) == 0) {
796 params+=strlen(ac_ids[i]);
804 if (*params == ':') {
807 bw = strtol(params, &next_char, 10);
808 if (next_char == params)
814 sp->bandwidth_bps = bw*Kbit;
817 long packet_size = 0;
818 if (*params == '@') {
820 period = strtol(params, &next_char, 10);
826 if (*params == '/') {
828 packet_size = strtol(params, &next_char, 10);
829 if (packet_size == 0)
837 sp->period_usec = period*MSEC_TO_USEC;
838 sp->packet_size = packet_size;
842 if (*params != '\0' && *params != ',')
853 void wait_for_all_threads_to_finish_fwp(void)
856 /* Wait for all threads to finish */
858 /* for (i=0; i < 2*nr_streams; i++) { */
859 /* sem_wait(&sem_thread_finished); */
863 void wait_for_all_threads_to_finish_native(void)
866 /* Wait for all threads to finish */
867 for (i=0; i < nr_streams + AC_NUM; i++) {
868 sem_wait(&sem_thread_finished);
881 #define addfield(title, format, ...) \
883 x+=strlen(title)+1; \
884 if (i == 0) addstr(title); \
886 snprintf(str, sizeof(str), format, __VA_ARGS__); \
890 void print_status(int seconds)
893 char str[200], s1[20];
895 struct stream *s = NULL;
897 for (i = 0; i <= nr_streams; i++) {
898 if (i>0) s = &streams[i-1];
901 addfield("Stream", "%d", i);
902 addfield("Bandwidth", "%s", bandwidth_to_text(s1, s->bandwidth_bps));
903 addfield("Packet size", "%d bytes", s->packet_size);
904 addfield("Period ", "%s", usec_to_text(s1, s->period_usec));
905 addfield("AC ", "%s", ac_to_text[s->ac]);
906 addfield("Worst-case delay", "%s", usec_to_text(s1, s->wc_delay));
907 addfield("Received responses", "%lld", s->received);
913 void print_status(int seconds)
916 fprintf(stderr, "\r%3ds", seconds);
917 for (ac = 0; ac < AC_NUM; ac++) {
918 int delta = receivers[ac].received - receivers[ac].last_received;
919 receivers[ac].last_received = receivers[ac].received;
920 fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
926 int main(int argc, char *argv[])
934 while ((opt = getopt(argc, argv, "B:b:C:c:g:I:j:o:qQ:s:T:")) != -1) {
937 opt_def_bandwidth = atoi(optarg);
941 error = parse_bandwidths(optarg);
944 fprintf(stderr, "Bandwidth parse error - string to short\n");
946 fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
952 opt_comment = optarg;
955 opt_count_sec = atoi(optarg);
958 opt_granularity_usec = atoi(optarg);
959 if (opt_granularity_usec < MIN_GRANULARITY) {
960 fprintf(stderr, "Granulatiry too small (min %d)!\n", MIN_GRANULARITY);
965 opt_interface = optarg;
969 fprintf(stderr, "-j is not allowd when compiled with FWP\n");
972 opt_jitter = atoi(optarg);
979 opt_send_buf_size = atoi(optarg);
982 opt_wait_for_queue_is_full = true;
985 opt_packet_size = atoi(optarg);
988 opt_def_period_msec = atoi(optarg);
991 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
992 fprintf(stderr, "Options:\n");
993 fprintf(stderr, " -B default bandwidth for -b option [kbit]\n");
994 fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec> or /<bytes>][,...]\n");
995 fprintf(stderr, " -C comment (added to header)\n");
996 fprintf(stderr, " -c count (number of seconds to run)\n");
997 fprintf(stderr, " -g histogram granularity [usec]\n");
998 fprintf(stderr, " -I <interface> send packets from this interface");
999 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
1000 fprintf(stderr, " -o output filename (.dat will be appended)\n");
1001 fprintf(stderr, " -q gather statistics only after some queue becomes full\n");
1002 fprintf(stderr, " -Q <bytes> set size for socket send buffers\n");
1003 fprintf(stderr, " -s size of data payload in packets [bytes] (default: %d)\n", opt_packet_size);
1004 fprintf(stderr, " -T default period for -b option [msec]\n");
1008 if (opt_packet_size && opt_def_period_msec) {
1009 fprintf(stderr, "Error: Nonzero -T and -s can't be used together!.\n");
1013 if (optind < argc) {
1014 server_addr = argv[optind];
1016 fprintf(stderr, "Expected server address argument\n");
1020 if (nr_streams == 0)
1021 parse_bandwidths("BE");
1023 pthread_attr_init(&attr);
1025 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
1027 if ((logfd = fopen(logfname,"w+")) == NULL) {
1028 fprintf(stderr,"Can not open %s\n", logfname);
1031 if (signal(SIGTERM, stopper) == SIG_ERR) {
1032 perror("Error in signal registration");
1036 if (signal(SIGINT, stopper) == SIG_ERR) {
1037 perror("Signal handler registration error");
1041 struct sigaction sa;
1042 sa.sa_handler = empty_handler;
1043 sa.sa_flags = 0; /* don't restart syscalls */
1045 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
1046 perror("sigaction error");
1050 sem_init(&sem_thread_finished, 0, 0);
1057 fprintf(stderr, "FWP initialization failed\n");
1061 /* create four receivers each per AC */
1062 for (ac = AC_NUM - 1; ac >= 0; ac--) {
1063 ac_sockfd[ac] = create_ac_socket(ac);
1064 if (ac_sockfd[ac] < 0) {
1067 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
1069 fprintf(stderr, "Error while creating receiver %d\n",rc);
1072 receivers[ac].valid = true;
1077 if (opt_interface) {
1080 memset(&ifr, 0, sizeof(ifr));
1081 strncpy(ifr.ifr_name, opt_interface, IFNAMSIZ-1);
1082 if (ioctl(ac_sockfd[AC_VO], SIOCGIFINDEX, &ifr) < 0) {
1083 fprintf(stderr, "unknown iface %s\n", opt_interface);
1086 cmsg.ipi.ipi_ifindex = ifr.ifr_ifindex;
1087 cmsg_len = sizeof(cmsg);
1089 /* create sendpoints */
1090 for (i = 0; i < nr_streams; i++) {
1091 struct stream *s = &streams[i];
1092 pthread_mutex_init(&s->mutex, NULL);
1093 calc_stream_params(s);
1094 rc = pthread_create(&thread, &attr, sender, (void*) s);
1096 fprintf(stderr, "Error while creating sender %d\n",rc);
1101 if (some_contract_not_accepted) {
1107 while (!exit_flag) {
1110 print_status(seconds);
1111 if (seconds == opt_count_sec)
1120 fprintf(stderr, "\nWaiting for threads to finish\n");
1121 wait_for_all_threads_to_finish();
1123 struct timespec end_timestamp, measure_length;
1124 clock_gettime(CLOCK_REALTIME,&end_timestamp);
1125 timespec_sub(&measure_length, &end_timestamp, &reset_timestamp);
1127 save_results(argc, argv, timespec2usec(&measure_length));