4 #include <sys/socket.h>
5 #include <netinet/in.h>
20 #include <semaphore.h>
21 #include <sys/ioctl.h>
25 #include <fwp_confdefs.h>
31 #define MAX_STREAMS 10
32 #define MIN_GRANULARITY 100
34 unsigned opt_packet_size = 800;
35 int opt_send_buf_size = -1;
36 unsigned opt_period_usec = 10*MSEC_TO_USEC;
38 unsigned opt_jitter = 0;
39 char *opt_output = "delay_stats";
40 unsigned opt_count_sec = 0;
41 unsigned opt_def_bandwidth = 50;
42 unsigned opt_def_period_msec = 0;
43 int opt_granularity_usec = MIN_GRANULARITY;
44 bool opt_wait_for_queue_is_full; /* Don't gather any statistics until any queue is full */
45 char *opt_comment = NULL;
47 bool some_queue_is_full = false;
48 struct timespec reset_timestamp;
50 bool some_contract_not_accepted = false;
52 /* Locked when some queue is full to prevent multiple resets of
54 pthread_mutex_t queue_full_mutex = PTHREAD_MUTEX_INITIALIZER;
56 int ac_sockfd[AC_NUM];
61 unsigned received, last_received;
64 struct receiver receivers[AC_NUM];
70 /* maximal traffic delay in ms - 10 s*/
71 #define MAX_DELAY_US 10000000
74 unsigned csc; /* Client-server-client delay divided by 2 */
75 unsigned cs; /* Client-server delay */
76 unsigned sc; /* Server-client delay */
79 struct delay_stat delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
80 pthread_mutex_t delay_stats_mutex = PTHREAD_MUTEX_INITIALIZER;
82 /*struct ac_stats[AC_NUM] {
83 unsigned long int min_trans_time;
84 unsigned long int sum_trans_time;
85 struct timespec recv_timestamp;
86 struct timespec send_timestamp;
90 /* Input parameters */
92 int bandwidth_bps; /* bits per second */
93 int jitter; /* percent */
94 /* Mulualy exclusive input parameters */
96 long period_usec; /* all time units are in microseconds */
100 struct sockaddr_in rem_addr;
102 fwp_contract_d_t contract_send;
103 fwp_contract_d_t contract_resp;
104 fwp_endpoint_d_t endpoint;
105 fwp_endpoint_d_t resp_endpoint;
108 struct receiver receiver;
109 long wc_delay; /* worst-case delay */
113 pthread_mutex_t mutex;
114 unsigned long long sent, really_sent, received;
117 static struct cmsg_ipi {
119 struct in_pktinfo ipi;
120 } cmsg = { {sizeof(struct cmsg_ipi), SOL_IP, IP_PKTINFO},
125 struct send_endpoint sepoint[] = {
126 { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
127 { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps = 480*Kbit },
128 { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
129 { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
130 // { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps = 675*Kbit },
134 struct stream streams[MAX_STREAMS];
136 unsigned int nr_streams = 0;
138 sem_t sem_thread_finished;
140 bool exit_flag = false;
143 #define negotiate_contract_for_stream(s) negotiate_contract_for_stream_fwp(s)
144 #define create_stream_endpoint(s) create_stream_endpoint_fwp(s)
145 #define send_packet(s, b) send_packet_fwp(s, b)
146 #define recv_packet(s, b) recv_packet_fwp(s, b)
147 #define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_fwp()
149 #define negotiate_contract_for_stream(s) 0
150 #define create_stream_endpoint(s) create_stream_endpoint_native(s)
151 #define send_packet(s, b) send_packet_native(s, b)
152 #define recv_packet(s, b) recv_packet_native(s, b)
153 #define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_native()
161 /* Interrupt all receivers */
163 for (i=0; i < nr_streams; i++) {
164 if (streams[i].receiver.valid) pthread_kill(streams[i].receiver.thread, SIGUSR1);
165 if (streams[i].contract_send) fwp_contract_cancel(streams[i].contract_send);
166 if (streams[i].contract_resp)fwp_contract_cancel(streams[i].contract_resp);
169 for (i=0; i < AC_NUM; i++) {
170 pthread_kill(receivers[i].thread, SIGUSR1);
175 void stream_to_text(char *stream_desc, size_t n, struct stream *stream, long long useconds)
181 snprintf(real, sizeof(real), "; real: %s sent %lld (%lld/s), received %lld (%lld/s)",
182 bandwidth_to_text(buf[0], (long long)stream->really_sent*stream->packet_size*8*SEC_TO_USEC/useconds),
183 stream->sent, stream->sent*SEC_TO_USEC/useconds,
184 stream->received, stream->received*SEC_TO_USEC/useconds);
189 snprintf(stream_desc, n, "%d: %s %s (%d bytes per %s +-%s, %d packets/s)%s",
190 stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
191 stream->packet_size, usec_to_text(buf[1], stream->period_usec),
192 usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
193 (int)(SEC_TO_USEC/stream->period_usec), real);
196 void save_results(int argc, char *argv[], int useconds)
199 const int mini = 3000/opt_granularity_usec;
201 unsigned send_count[AC_NUM];
203 fprintf(stderr, "Writing data to %s... ", logfname);
206 fprintf(logfd, "# Invoked as: ");
207 for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
209 fprintf(logfd, "(%s)", opt_comment);
211 fprintf(logfd, "\n");
213 if (useconds/SEC_TO_USEC != opt_count_sec) {
215 usec_to_text(buf, useconds);
216 fprintf(logfd, "# Data gathered for %s.\n", buf);
219 for (i = 0; i < nr_streams; i++) {
220 char stream_desc[200];
221 stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], useconds);
222 fprintf(logfd, "# Stream %s\n", stream_desc);
225 /* Find maximal delay */
227 for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
228 for (ac = 0; ac < AC_NUM; ac++) {
229 if ((delay_stats[ac][maxi].csc != 0) ||
230 (delay_stats[ac][maxi].cs != 0) ||
231 (delay_stats[ac][maxi].sc != 0))
234 if (!allzeros) break;
237 if (maxi < mini) maxi = mini;
239 /* Calculate total number of sent packets per AC */
240 memset(send_count, 0, sizeof(send_count));
241 for (i = 0; i < nr_streams; i++) {
243 send_count[ac] += streams[i].sent;
248 for ( i = 0 ; i < maxi; i++) {
249 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
250 for (ac = 0; ac < AC_NUM; ac++) {
252 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
253 else val = -1; /* Don't display this ac */
254 fprintf(logfd," %lf", val);
258 fprintf(logfd,"\n\n");
261 fprintf(logfd,"## Format: msec csc%% cs%% sc%%\n");
264 for (ac = 0; ac < AC_NUM; ac++) {
265 struct delay_stat integral = {0,0,0}, last = {-1,-1,-1};
267 fprintf(logfd,"%f %f %f %f\n", 0.0, 0.0, 0.0, 0.0);
269 if (send_count[ac] != 0) {
271 while ((delay_stats[ac][i].csc == 0) &&
272 (delay_stats[ac][i].cs == 0) &&
273 (delay_stats[ac][i].sc == 0)) i++;
275 for (i++; i < maxi+1; i++) {
276 if (memcmp(&last, &integral, sizeof(last))) {
278 snprintf(buf[0], sizeof(buf[0]), "%f", (double)integral.csc*100.0 / send_count[ac]);
279 snprintf(buf[1], sizeof(buf[1]), "%f", (double)integral.cs *100.0 / send_count[ac]);
280 snprintf(buf[2], sizeof(buf[2]), "%f", (double)integral.sc *100.0 / send_count[ac]);
282 fprintf(logfd,"%f %s %s %s\n", i*opt_granularity_usec/1000.0,
283 integral.csc != last.csc ? buf[0] : "-",
284 integral.cs != last.cs ? buf[1] : "-",
285 integral.sc != last.sc ? buf[2] : "-"
290 integral.csc += delay_stats[ac][i-1].csc;
291 integral.sc += delay_stats[ac][i-1].sc;
292 integral.cs += delay_stats[ac][i-1].cs;
296 fprintf(logfd,"\n\n");
299 fprintf(stderr, "finished.\n");
305 int create_ac_socket(unsigned int ac)
308 unsigned int yes=1, tos;
311 if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
313 error(0, errno, "Unable to open socket");
316 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
317 error(0, errno, "set non-blocking socket");
320 if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
321 error(0, errno, "Unable to set socket");
325 if (opt_send_buf_size >= 0) {
326 if (setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&opt_send_buf_size,sizeof(opt_send_buf_size)) == -1) {
327 error(0, errno, "Unable to set socket buffer size");
333 //tos = ((AC_NUM - ac) *2 - 1)*32;
335 if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
336 error(0, errno, "Unable to set TOS");
348 void reset_statistics()
351 for (i = 0; i < nr_streams; i++) {
352 pthread_mutex_lock(&streams[i].mutex);
354 streams[i].really_sent = 0;
355 streams[i].received = 0;
356 pthread_mutex_unlock(&streams[i].mutex);
358 pthread_mutex_lock(&delay_stats_mutex);
359 clock_gettime(CLOCK_REALTIME, &reset_timestamp);
360 memset(delay_stats, 0, sizeof(delay_stats));
361 pthread_mutex_unlock(&delay_stats_mutex);
365 int recv_packet_native(unsigned ac, struct msg_t *msg)
369 struct sockaddr_in rem_addr;
370 unsigned int rem_addr_length;
373 FD_SET(ac_sockfd[ac], &fdset);
374 rem_addr_length = sizeof(rem_addr);
377 ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
379 if (errno == EINTR) continue;
380 error(0, errno, "receiver select");
383 mlen = recvfrom(ac_sockfd[ac], msg, sizeof(*msg), 0,
384 (struct sockaddr*)&rem_addr, &rem_addr_length);
390 int recv_packet_fwp(struct stream *stream, struct msg_t *msg)
393 mlen = fwp_recv(stream->resp_endpoint, msg, sizeof(*msg), 0);
398 void* receiver(void* arg)
401 long long int trans_time_usec, client_to_server_usec, server_to_client_usec;
402 long long int min_trans_time;
403 struct timespec send_timestamp, server_timestamp, recv_timestamp;
414 struct stream *stream = arg;
416 mlen = recv_packet_fwp(stream, &msg);
419 mlen = recv_packet_native(ac, &msg);
422 error(0, errno, "receive_packet error");
425 clock_gettime(CLOCK_REALTIME,&recv_timestamp);
426 send_timestamp = msg.send_timestamp;
427 server_timestamp = msg.sendback_timestamp;
429 /* Check whether this message was sent after reset_statistics() */
431 if ((ret = timespec_sub_usec(&send_timestamp, &reset_timestamp)) < 0) {
432 continue; /* If so, don't count it */
435 trans_time_usec = timespec_sub_usec(&recv_timestamp ,&send_timestamp) / 2;
436 client_to_server_usec = timespec_sub_usec(&server_timestamp, &send_timestamp);
437 server_to_client_usec = timespec_sub_usec(&recv_timestamp, &server_timestamp);
439 pthread_mutex_lock(&delay_stats_mutex);
440 if (trans_time_usec < MAX_DELAY_US && trans_time_usec >= 0) {
441 delay_stats[ac][trans_time_usec/opt_granularity_usec].csc++;
443 if (client_to_server_usec < MAX_DELAY_US && client_to_server_usec >= 0) {
444 delay_stats[ac][client_to_server_usec/opt_granularity_usec].cs++;
446 if (server_to_client_usec < MAX_DELAY_US && server_to_client_usec >= 0) {
447 delay_stats[ac][server_to_client_usec/opt_granularity_usec].sc++;
449 pthread_mutex_unlock(&delay_stats_mutex);
452 if (trans_time_usec > stream->wc_delay) {
453 stream->wc_delay = trans_time_usec;
456 receivers[ac].received++;
458 pthread_mutex_lock(&streams[msg.stream].mutex);
459 streams[msg.stream].received++;
460 pthread_mutex_unlock(&streams[msg.stream].mutex);
462 /*if (trans_time_nsec < min_trans_time)
463 min_trans_time = trans_time_nsec;*/
464 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
465 "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
466 msg.seqn, msg.tos, send_timestamp.tv_sec,\
467 send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
468 recv_timestamp.tv_nsec, trans_time_msec); */
471 sem_post(&sem_thread_finished);
478 * @return -1 in case of error, 1 in case of sucessfull send and 0
479 * when all buffers are full.
483 send_packet_native(struct stream* stream, union msg_buff* buff)
489 iov.iov_len = stream->packet_size;
490 msg.msg_name = (void*)&stream->rem_addr;
491 msg.msg_namelen = sizeof(stream->rem_addr);
495 msg.msg_control = &cmsg;
496 msg.msg_controllen = cmsg_len;
500 while (sendmsg(ac_sockfd[stream->ac], &msg, 0) < 0) {
501 if (errno == EINTR) continue;
502 if (errno == EAGAIN) {
503 if (opt_wait_for_queue_is_full &&
504 !some_queue_is_full &&
505 /* We use mutex as atomic test and set */
506 (pthread_mutex_trylock(&queue_full_mutex) != EBUSY)) {
507 some_queue_is_full = true;
513 error(0, errno, "Error while sending");
522 send_packet_fwp(struct stream* stream, union msg_buff* buff)
526 buff->msg.resp_port = htons(stream->resp_port);
527 ret = fwp_send(stream->endpoint, buff, stream->packet_size, 0);
534 wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
536 struct timespec time_to_wait, current_time, period, interval;
537 unsigned period_usec = stream->period_usec;
539 /* |~~~+~~~| jitter interval (width = 2*stream->jitter percentage from period)*/
540 /* |-------------| nominal period*/
541 if (stream->jitter) {
542 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-stream->jitter)/100
543 + rand() % (2*period_usec*stream->jitter/100));
545 period.tv_nsec = USEC_TO_NSEC*(period_usec);
549 timespec_add(&time_to_wait, last_send_time, &period);
550 clock_gettime(CLOCK_REALTIME,¤t_time);
551 timespec_sub(&interval,&time_to_wait,¤t_time);
552 nanosleep(&interval,NULL);
556 void* sender(void* arg)
559 unsigned long int seqn;
560 struct stream* stream = (struct stream*) arg;
564 char stream_desc[100];
565 stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
566 printf("%s\n", stream_desc);
568 if (stream->bandwidth_bps == 0)
574 set_rt_prio(90-stream->ac);
578 /* buff.msg.seqn = seqn++; */
579 /* buff.msg.tos = ac_to_tos[stream->ac]; */
580 buff.msg.stream = stream-streams;
582 clock_gettime(CLOCK_REALTIME,&buff.msg.send_timestamp);
584 ret = send_packet(stream, &buff);
589 pthread_mutex_lock(&stream->mutex);
592 stream->really_sent++;
593 pthread_mutex_unlock(&stream->mutex);
596 printf("%d", stream->ac);
600 wait_for_next_send(stream, &buff.msg.send_timestamp);
603 sem_post(&sem_thread_finished);
608 static int negotiate_contract_for_stream_fwp(struct stream *stream)
610 fwp_contract_t contract;
614 /* Contract for client->server stream */
615 memset(&contract, 0, sizeof(contract));
616 contract.budget = stream->packet_size;
617 contract.period_usec = stream->period_usec;
618 contract.deadline_usec = 3*stream->period_usec;
620 stream->contract_send = fwp_contract_create(&contract);
621 ret = fwp_contract_negotiate(stream->contract_send, &stream->vres);
623 stream->contract_send = NULL;
624 /* fprintf(stderr, "Send contract was not accepted\n\n\n"); */
628 /* Contract for server->client stream */
629 memset(&contract, 0, sizeof(contract));
630 contract.budget = stream->packet_size;
631 contract.period_usec = stream->period_usec;
632 contract.deadline_usec = 3*stream->period_usec;
634 stream->contract_resp = fwp_contract_create(&contract);
635 ret = fwp_contract_negotiate(stream->contract_resp, &vres2);
637 stream->contract_resp = NULL;
638 /* fprintf(stderr, "Receive contract was not accepted\n\n\n"); */
642 /* We don't use the vres at server, since the server doesn't
643 * know the parameters. Instread, server uses plain
651 static void create_stream_endpoint_fwp(struct stream *stream)
653 /* fwp_endpoint_attr_t attr; */
657 stream->ac = fwp_vres_get_ac(stream->vres);
659 /* fwp_endpoint_attr_init(&attr); */
660 /* fwp_endpoint_attr_setreliability(&attr, FWP_EPOINT_BESTEFFORT); */
662 ph = gethostbyname(server_addr);
663 if (ph && ph->h_addr_list[0]) {
664 struct in_addr *a = (struct in_addr *)(ph->h_addr_list[0]);
665 ret = fwp_send_endpoint_create(a->s_addr, BASE_PORT + stream->ac,
666 NULL, &stream->endpoint);
667 /* stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac); */
668 if (ret < 0) error(1, errno, "fwp_send_endpoint_create");
670 ret = fwp_send_endpoint_bind(stream->endpoint, stream->vres);
671 if (ret != 0) error(1, errno, "fwp_send_endpoint_bind");
673 ret = fwp_receive_endpoint_create(0, NULL, &stream->resp_endpoint);
674 if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
677 fwp_endpoint_get_params(stream->resp_endpoint, NULL, &port, NULL);
678 stream->resp_port = port;
680 ret = pthread_create(&stream->receiver.thread, NULL, receiver, (void*)stream);
681 if (ret) error(1, ret, "Error while creating receiver");
683 stream->receiver.valid = true;
686 error(1, errno, "gethostbyname(%s)", server_addr);
691 static void create_stream_endpoint_native(struct stream *stream)
695 memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
697 stream->rem_addr.sin_family = AF_INET;
698 ph = gethostbyname(server_addr);
700 stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
702 error(1, errno, "gethostbyname(%s)", server_addr);
704 stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
709 calc_stream_params(struct stream *stream)
712 unsigned period_usec;
716 /* If some parameters are not set explicitely, use default values. */
717 if (stream->bandwidth_bps < 0) stream->bandwidth_bps = opt_def_bandwidth * Kbit;
718 if (stream->packet_size < 0) stream->packet_size = opt_packet_size;
719 if (stream->period_usec < 0) stream->period_usec = opt_def_period_msec * MSEC_TO_USEC;
721 bandwidth = stream->bandwidth_bps;
723 /* Avoid arithmetic exception. Server thread will exit if
724 stream->bandwidth_bps == 0. */
725 if (bandwidth == 0) bandwidth = 1;
727 if (stream->packet_size) {
728 packet_size = stream->packet_size;
729 period_usec = SEC_TO_USEC*packet_size*8/bandwidth;
730 if (period_usec == 0) period_usec = 1;
731 } else if (stream->period_usec) {
732 period_usec = stream->period_usec;
733 packet_size = (long long)bandwidth/8 * period_usec/SEC_TO_USEC;
736 stream_to_text(buf, sizeof(buf), stream, 0);
737 error(1, 0, "Neither packet size nor period was specified for a stream %s", buf);
740 if (packet_size < sizeof(struct msg_t)) {
741 error(1, 0, "Packet size too small (min %d)", sizeof(struct msg_t));
744 stream->packet_size = packet_size;
745 stream->period_usec = period_usec;
746 stream->jitter = opt_jitter;
748 ret = negotiate_contract_for_stream(stream);
750 create_stream_endpoint(stream);
753 stream_to_text(buf, sizeof(buf), stream, 0);
754 fprintf(stderr, "Contract hasn't been accepted:\n%s\n", buf);
755 stream->bandwidth_bps = 0;
756 some_contract_not_accepted = true;
761 * Parse -b parameter.
763 * @param params String to parse
765 * @return NULL in case of success, pointer to a problematic character
768 char* parse_bandwidths(char *params)
770 struct stream *sp = &streams[nr_streams];
772 while (*params && nr_streams < MAX_STREAMS) {
773 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
777 if (strlen(params) < 2)
779 for (i=0; i<AC_NUM; i++) {
780 if (strncmp(params, ac_ids[i], 2) == 0) {
782 params+=strlen(ac_ids[i]);
790 if (*params == ':') {
793 bw = strtol(params, &next_char, 10);
794 if (next_char == params)
800 sp->bandwidth_bps = bw*Kbit;
803 long packet_size = 0;
804 if (*params == '@') {
806 period = strtol(params, &next_char, 10);
812 if (*params == '/') {
814 packet_size = strtol(params, &next_char, 10);
815 if (packet_size == 0)
823 sp->period_usec = period*MSEC_TO_USEC;
824 sp->packet_size = packet_size;
828 if (*params != '\0' && *params != ',')
839 void wait_for_all_threads_to_finish_fwp(void)
842 /* Wait for all threads to finish */
844 /* for (i=0; i < 2*nr_streams; i++) { */
845 /* sem_wait(&sem_thread_finished); */
849 void wait_for_all_threads_to_finish_native(void)
852 /* Wait for all threads to finish */
853 for (i=0; i < nr_streams + AC_NUM; i++) {
854 sem_wait(&sem_thread_finished);
867 #define addfield(title, format, ...) \
869 x+=strlen(title)+1; \
870 if (i == 0) addstr(title); \
872 snprintf(str, sizeof(str), format, __VA_ARGS__); \
876 void print_status(int seconds)
879 char str[200], s1[20];
881 struct stream *s = NULL;
883 for (i = 0; i <= nr_streams; i++) {
884 if (i>0) s = &streams[i-1];
887 addfield("Stream", "%d", i);
888 addfield("Bandwidth", "%s", bandwidth_to_text(s1, s->bandwidth_bps));
889 addfield("Packet size", "%d bytes", s->packet_size);
890 addfield("Period ", "%s", usec_to_text(s1, s->period_usec));
891 addfield("AC ", "%s", ac_to_text[s->ac]);
892 addfield("Worst-case delay", "%s", usec_to_text(s1, s->wc_delay));
893 addfield("Received responses", "%lld", s->received);
899 void print_status(int seconds)
902 fprintf(stderr, "\r%3ds", seconds);
903 for (ac = 0; ac < AC_NUM; ac++) {
904 int delta = receivers[ac].received - receivers[ac].last_received;
905 receivers[ac].last_received = receivers[ac].received;
906 fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
912 int main(int argc, char *argv[])
914 int i, rc, frames, seconds;
920 while ((opt = getopt(argc, argv, "B:b:C:c:g:I:j:o:qQ:s:T:")) != -1) {
923 opt_def_bandwidth = atoi(optarg);
927 errpos = parse_bandwidths(optarg);
928 if (errpos != NULL) {
930 error(1, 0, "Bandwidth parse error - string to short");
932 error(1, 0, "Bandwidth parse error at '%s'", errpos);
937 opt_comment = optarg;
940 opt_count_sec = atoi(optarg);
943 opt_granularity_usec = atoi(optarg);
944 if (opt_granularity_usec < MIN_GRANULARITY) {
945 error(1, 0, "Granulatiry too small (min %d)", MIN_GRANULARITY);
949 opt_interface = optarg;
953 error(1, 0, "-j is not allowd when compiled with FWP");
955 opt_jitter = atoi(optarg);
962 opt_send_buf_size = atoi(optarg);
965 opt_wait_for_queue_is_full = true;
968 opt_packet_size = atoi(optarg);
971 opt_def_period_msec = atoi(optarg);
974 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
975 fprintf(stderr, "Options:\n");
976 fprintf(stderr, " -B default bandwidth for -b option [kbit]\n");
977 fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec> or /<bytes>][,...]\n");
978 fprintf(stderr, " -C comment (added to header)\n");
979 fprintf(stderr, " -c count (number of seconds to run)\n");
980 fprintf(stderr, " -g histogram granularity [usec]\n");
981 fprintf(stderr, " -I <interface> send packets from this interface");
982 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
983 fprintf(stderr, " -o output filename (.dat will be appended)\n");
984 fprintf(stderr, " -q gather statistics only after some queue becomes full\n");
985 fprintf(stderr, " -Q <bytes> set size for socket send buffers\n");
986 fprintf(stderr, " -s size of data payload in packets [bytes] (default: %d)\n", opt_packet_size);
987 fprintf(stderr, " -T default period for -b option [msec]\n");
991 if (opt_packet_size && opt_def_period_msec) {
992 error(1, 0, "Error: Nonzero -T and -s can't be used together!");
996 server_addr = argv[optind];
998 error(1, 0, "Expected server address argument");
1001 if (nr_streams == 0)
1002 parse_bandwidths("BE");
1004 pthread_attr_init(&attr);
1006 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
1008 if ((logfd = fopen(logfname,"w+")) == NULL) {
1009 error(1, errno ,"Can not open %s", logfname);
1011 if (signal(SIGTERM, stopper) == SIG_ERR) {
1012 error(1, errno, "Error in signal registration");
1015 if (signal(SIGINT, stopper) == SIG_ERR) {
1016 error(1, errno, "Signal handler registration error");
1019 struct sigaction sa;
1020 sa.sa_handler = empty_handler;
1021 sa.sa_flags = 0; /* don't restart syscalls */
1023 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
1024 error(1, errno, "sigaction error");
1027 sem_init(&sem_thread_finished, 0, 0);
1034 error(1, errno, "FWP initialization failed");
1038 /* create four receivers each per AC */
1039 for (ac = AC_NUM - 1; ac >= 0; ac--) {
1040 ac_sockfd[ac] = create_ac_socket(ac);
1041 if (ac_sockfd[ac] < 0) {
1044 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
1046 error(1, rc, "Error while creating receiver");
1048 receivers[ac].valid = true;
1053 if (opt_interface) {
1056 memset(&ifr, 0, sizeof(ifr));
1057 strncpy(ifr.ifr_name, opt_interface, IFNAMSIZ-1);
1058 if (ioctl(ac_sockfd[AC_VO], SIOCGIFINDEX, &ifr) < 0) {
1059 error(1, 0, "unknown iface %s", opt_interface);
1061 cmsg.ipi.ipi_ifindex = ifr.ifr_ifindex;
1062 cmsg_len = sizeof(cmsg);
1064 /* create sendpoints */
1065 for (i = 0; i < nr_streams; i++) {
1066 struct stream *s = &streams[i];
1067 pthread_mutex_init(&s->mutex, NULL);
1068 calc_stream_params(s);
1069 rc = pthread_create(&thread, &attr, sender, (void*) s);
1070 if (rc) error(1, rc, "Error while creating sender");
1073 if (some_contract_not_accepted) {
1080 while (!exit_flag) {
1087 print_status(seconds);
1088 if (seconds == opt_count_sec)
1097 fprintf(stderr, "\nWaiting for threads to finish\n");
1098 wait_for_all_threads_to_finish();
1100 struct timespec end_timestamp, measure_length;
1101 clock_gettime(CLOCK_REALTIME,&end_timestamp);
1102 timespec_sub(&measure_length, &end_timestamp, &reset_timestamp);
1104 save_results(argc, argv, timespec2usec(&measure_length));