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);
423 error(0, errno, "receive_packet error");
426 clock_gettime(CLOCK_REALTIME,&recv_timestamp);
427 send_timestamp = msg.send_timestamp;
428 server_timestamp = msg.sendback_timestamp;
430 /* Check whether this message was sent after reset_statistics() */
432 if ((ret = timespec_sub_usec(&send_timestamp, &reset_timestamp)) < 0) {
433 continue; /* If so, don't count it */
436 trans_time_usec = timespec_sub_usec(&recv_timestamp ,&send_timestamp) / 2;
437 client_to_server_usec = timespec_sub_usec(&server_timestamp, &send_timestamp);
438 server_to_client_usec = timespec_sub_usec(&recv_timestamp, &server_timestamp);
440 pthread_mutex_lock(&delay_stats_mutex);
441 if (trans_time_usec < MAX_DELAY_US && trans_time_usec >= 0) {
442 delay_stats[ac][trans_time_usec/opt_granularity_usec].csc++;
444 if (client_to_server_usec < MAX_DELAY_US && client_to_server_usec >= 0) {
445 delay_stats[ac][client_to_server_usec/opt_granularity_usec].cs++;
447 if (server_to_client_usec < MAX_DELAY_US && server_to_client_usec >= 0) {
448 delay_stats[ac][server_to_client_usec/opt_granularity_usec].sc++;
450 pthread_mutex_unlock(&delay_stats_mutex);
453 if (trans_time_usec > stream->wc_delay) {
454 stream->wc_delay = trans_time_usec;
457 receivers[ac].received++;
459 pthread_mutex_lock(&streams[msg.stream].mutex);
460 streams[msg.stream].received++;
461 pthread_mutex_unlock(&streams[msg.stream].mutex);
463 /*if (trans_time_nsec < min_trans_time)
464 min_trans_time = trans_time_nsec;*/
465 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
466 "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
467 msg.seqn, msg.tos, send_timestamp.tv_sec,\
468 send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
469 recv_timestamp.tv_nsec, trans_time_msec); */
472 sem_post(&sem_thread_finished);
479 * @return -1 in case of error, 1 in case of sucessfull send and 0
480 * when all buffers are full.
484 send_packet_native(struct stream* stream, union msg_buff* buff)
490 iov.iov_len = stream->packet_size;
491 msg.msg_name = (void*)&stream->rem_addr;
492 msg.msg_namelen = sizeof(stream->rem_addr);
496 msg.msg_control = &cmsg;
497 msg.msg_controllen = cmsg_len;
501 while (sendmsg(ac_sockfd[stream->ac], &msg, 0) < 0) {
502 if (errno == EINTR) continue;
503 if (errno == EAGAIN) {
504 if (opt_wait_for_queue_is_full &&
505 !some_queue_is_full &&
506 /* We use mutex as atomic test and set */
507 (pthread_mutex_trylock(&queue_full_mutex) != EBUSY)) {
508 some_queue_is_full = true;
514 error(0, errno, "Error while sending");
523 send_packet_fwp(struct stream* stream, union msg_buff* buff)
527 buff->msg.resp_port = htons(stream->resp_port);
528 ret = fwp_send(stream->endpoint, buff, stream->packet_size, 0);
535 wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
537 struct timespec time_to_wait, current_time, period, interval;
538 unsigned period_usec = stream->period_usec;
540 /* |~~~+~~~| jitter interval (width = 2*stream->jitter percentage from period)*/
541 /* |-------------| nominal period*/
542 if (stream->jitter) {
543 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-stream->jitter)/100
544 + rand() % (2*period_usec*stream->jitter/100));
546 period.tv_nsec = USEC_TO_NSEC*(period_usec);
550 timespec_add(&time_to_wait, last_send_time, &period);
551 clock_gettime(CLOCK_REALTIME,¤t_time);
552 timespec_sub(&interval,&time_to_wait,¤t_time);
553 nanosleep(&interval,NULL);
557 void* sender(void* arg)
560 unsigned long int seqn;
561 struct stream* stream = (struct stream*) arg;
565 char stream_desc[100];
566 stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
567 printf("%s\n", stream_desc);
569 if (stream->bandwidth_bps == 0)
575 set_rt_prio(90-stream->ac);
579 /* buff.msg.seqn = seqn++; */
580 /* buff.msg.tos = ac_to_tos[stream->ac]; */
581 buff.msg.stream = stream-streams;
583 clock_gettime(CLOCK_REALTIME,&buff.msg.send_timestamp);
585 ret = send_packet(stream, &buff);
590 pthread_mutex_lock(&stream->mutex);
593 stream->really_sent++;
594 pthread_mutex_unlock(&stream->mutex);
597 printf("%d", stream->ac);
601 wait_for_next_send(stream, &buff.msg.send_timestamp);
604 sem_post(&sem_thread_finished);
609 static int negotiate_contract_for_stream_fwp(struct stream *stream)
611 fwp_contract_t contract;
615 /* Contract for client->server stream */
616 memset(&contract, 0, sizeof(contract));
617 contract.budget = stream->packet_size;
618 contract.period_usec = stream->period_usec;
619 contract.deadline_usec = 3*stream->period_usec;
621 stream->contract_send = fwp_contract_create(&contract);
622 ret = fwp_contract_negotiate(stream->contract_send, &stream->vres);
624 stream->contract_send = NULL;
625 /* fprintf(stderr, "Send contract was not accepted\n\n\n"); */
629 /* Contract for server->client stream */
630 memset(&contract, 0, sizeof(contract));
631 contract.budget = stream->packet_size;
632 contract.period_usec = stream->period_usec;
633 contract.deadline_usec = 3*stream->period_usec;
635 stream->contract_resp = fwp_contract_create(&contract);
636 ret = fwp_contract_negotiate(stream->contract_resp, &vres2);
638 stream->contract_resp = NULL;
639 /* fprintf(stderr, "Receive contract was not accepted\n\n\n"); */
643 /* We don't use the vres at server, since the server doesn't
644 * know the parameters. Instread, server uses plain
652 static void create_stream_endpoint_fwp(struct stream *stream)
654 /* fwp_endpoint_attr_t attr; */
658 stream->ac = fwp_vres_get_ac(stream->vres);
660 /* fwp_endpoint_attr_init(&attr); */
661 /* fwp_endpoint_attr_setreliability(&attr, FWP_EPOINT_BESTEFFORT); */
663 ph = gethostbyname(server_addr);
664 if (ph && ph->h_addr_list[0]) {
665 struct in_addr *a = (struct in_addr *)(ph->h_addr_list[0]);
666 ret = fwp_send_endpoint_create(a->s_addr, BASE_PORT + stream->ac,
667 NULL, &stream->endpoint);
668 /* stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac); */
669 if (ret < 0) error(1, errno, "fwp_send_endpoint_create");
671 ret = fwp_send_endpoint_bind(stream->endpoint, stream->vres);
672 if (ret != 0) error(1, errno, "fwp_send_endpoint_bind");
674 ret = fwp_receive_endpoint_create(0, NULL, &stream->resp_endpoint);
675 if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
678 fwp_endpoint_get_params(stream->resp_endpoint, NULL, &port, NULL);
679 stream->resp_port = port;
681 ret = pthread_create(&stream->receiver.thread, NULL, receiver, (void*)stream);
682 if (ret) error(1, ret, "Error while creating receiver");
684 stream->receiver.valid = true;
687 error(1, errno, "gethostbyname(%s)", server_addr);
692 static void create_stream_endpoint_native(struct stream *stream)
696 memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
698 stream->rem_addr.sin_family = AF_INET;
699 ph = gethostbyname(server_addr);
701 stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
703 error(1, errno, "gethostbyname(%s)", server_addr);
705 stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
710 calc_stream_params(struct stream *stream)
713 unsigned period_usec;
717 /* If some parameters are not set explicitely, use default values. */
718 if (stream->bandwidth_bps < 0) stream->bandwidth_bps = opt_def_bandwidth * Kbit;
719 if (stream->packet_size < 0) stream->packet_size = opt_packet_size;
720 if (stream->period_usec < 0) stream->period_usec = opt_def_period_msec * MSEC_TO_USEC;
722 bandwidth = stream->bandwidth_bps;
724 /* Avoid arithmetic exception. Server thread will exit if
725 stream->bandwidth_bps == 0. */
726 if (bandwidth == 0) bandwidth = 1;
728 if (stream->packet_size) {
729 packet_size = stream->packet_size;
730 period_usec = SEC_TO_USEC*packet_size*8/bandwidth;
731 if (period_usec == 0) period_usec = 1;
732 } else if (stream->period_usec) {
733 period_usec = stream->period_usec;
734 packet_size = (long long)bandwidth/8 * period_usec/SEC_TO_USEC;
737 stream_to_text(buf, sizeof(buf), stream, 0);
738 error(1, 0, "Neither packet size nor period was specified for a stream %s", buf);
741 if (packet_size < sizeof(struct msg_t)) {
742 error(1, 0, "Packet size too small (min %d)", sizeof(struct msg_t));
745 stream->packet_size = packet_size;
746 stream->period_usec = period_usec;
747 stream->jitter = opt_jitter;
749 ret = negotiate_contract_for_stream(stream);
751 create_stream_endpoint(stream);
754 stream_to_text(buf, sizeof(buf), stream, 0);
755 fprintf(stderr, "Contract hasn't been accepted:\n%s\n", buf);
756 stream->bandwidth_bps = 0;
757 some_contract_not_accepted = true;
762 * Parse -b parameter.
764 * @param params String to parse
766 * @return NULL in case of success, pointer to a problematic character
769 char* parse_bandwidths(char *params)
771 struct stream *sp = &streams[nr_streams];
773 while (*params && nr_streams < MAX_STREAMS) {
774 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
778 if (strlen(params) < 2)
780 for (i=0; i<AC_NUM; i++) {
781 if (strncmp(params, ac_ids[i], 2) == 0) {
783 params+=strlen(ac_ids[i]);
791 if (*params == ':') {
794 bw = strtol(params, &next_char, 10);
795 if (next_char == params)
801 sp->bandwidth_bps = bw*Kbit;
804 long packet_size = 0;
805 if (*params == '@') {
807 period = strtol(params, &next_char, 10);
813 if (*params == '/') {
815 packet_size = strtol(params, &next_char, 10);
816 if (packet_size == 0)
824 sp->period_usec = period*MSEC_TO_USEC;
825 sp->packet_size = packet_size;
829 if (*params != '\0' && *params != ',')
840 void wait_for_all_threads_to_finish_fwp(void)
843 /* Wait for all threads to finish */
845 /* for (i=0; i < 2*nr_streams; i++) { */
846 /* sem_wait(&sem_thread_finished); */
850 void wait_for_all_threads_to_finish_native(void)
853 /* Wait for all threads to finish */
854 for (i=0; i < nr_streams + AC_NUM; i++) {
855 sem_wait(&sem_thread_finished);
868 #define addfield(title, format, ...) \
870 x+=strlen(title)+1; \
871 if (i == 0) addstr(title); \
873 snprintf(str, sizeof(str), format, __VA_ARGS__); \
877 void print_status(int seconds)
880 char str[200], s1[20];
882 struct stream *s = NULL;
884 for (i = 0; i <= nr_streams; i++) {
885 if (i>0) s = &streams[i-1];
888 addfield("Stream", "%d", i);
889 addfield("Bandwidth", "%s", bandwidth_to_text(s1, s->bandwidth_bps));
890 addfield("Packet size", "%d bytes", s->packet_size);
891 addfield("Period ", "%s", usec_to_text(s1, s->period_usec));
892 addfield("AC ", "%s", ac_to_text[s->ac]);
893 addfield("Worst-case delay", "%s", usec_to_text(s1, s->wc_delay));
894 addfield("Received responses", "%lld", s->received);
900 void print_status(int seconds)
903 fprintf(stderr, "\r%3ds", seconds);
904 for (ac = 0; ac < AC_NUM; ac++) {
905 int delta = receivers[ac].received - receivers[ac].last_received;
906 receivers[ac].last_received = receivers[ac].received;
907 fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
913 int main(int argc, char *argv[])
915 int i, rc, frames, seconds;
921 while ((opt = getopt(argc, argv, "B:b:C:c:g:I:j:o:qQ:s:T:")) != -1) {
924 opt_def_bandwidth = atoi(optarg);
928 errpos = parse_bandwidths(optarg);
929 if (errpos != NULL) {
931 error(1, 0, "Bandwidth parse error - string to short");
933 error(1, 0, "Bandwidth parse error at '%s'", errpos);
938 opt_comment = optarg;
941 opt_count_sec = atoi(optarg);
944 opt_granularity_usec = atoi(optarg);
945 if (opt_granularity_usec < MIN_GRANULARITY) {
946 error(1, 0, "Granulatiry too small (min %d)", MIN_GRANULARITY);
950 opt_interface = optarg;
954 error(1, 0, "-j is not allowd when compiled with FWP");
956 opt_jitter = atoi(optarg);
963 opt_send_buf_size = atoi(optarg);
966 opt_wait_for_queue_is_full = true;
969 opt_packet_size = atoi(optarg);
972 opt_def_period_msec = atoi(optarg);
975 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
976 fprintf(stderr, "Options:\n");
977 fprintf(stderr, " -B default bandwidth for -b option [kbit]\n");
978 fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec> or /<bytes>][,...]\n");
979 fprintf(stderr, " -C comment (added to header)\n");
980 fprintf(stderr, " -c count (number of seconds to run)\n");
981 fprintf(stderr, " -g histogram granularity [usec]\n");
982 fprintf(stderr, " -I <interface> send packets from this interface");
983 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
984 fprintf(stderr, " -o output filename (.dat will be appended)\n");
985 fprintf(stderr, " -q gather statistics only after some queue becomes full\n");
986 fprintf(stderr, " -Q <bytes> set size for socket send buffers\n");
987 fprintf(stderr, " -s size of data payload in packets [bytes] (default: %d)\n", opt_packet_size);
988 fprintf(stderr, " -T default period for -b option [msec]\n");
992 if (opt_packet_size && opt_def_period_msec) {
993 error(1, 0, "Error: Nonzero -T and -s can't be used together!");
997 server_addr = argv[optind];
999 error(1, 0, "Expected server address argument");
1002 if (nr_streams == 0)
1003 parse_bandwidths("BE");
1005 pthread_attr_init(&attr);
1007 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
1009 if ((logfd = fopen(logfname,"w+")) == NULL) {
1010 error(1, errno ,"Can not open %s", logfname);
1012 if (signal(SIGTERM, stopper) == SIG_ERR) {
1013 error(1, errno, "Error in signal registration");
1016 if (signal(SIGINT, stopper) == SIG_ERR) {
1017 error(1, errno, "Signal handler registration error");
1020 struct sigaction sa;
1021 sa.sa_handler = empty_handler;
1022 sa.sa_flags = 0; /* don't restart syscalls */
1024 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
1025 error(1, errno, "sigaction error");
1028 sem_init(&sem_thread_finished, 0, 0);
1035 error(1, errno, "FWP initialization failed");
1039 /* create four receivers each per AC */
1040 for (ac = AC_NUM - 1; ac >= 0; ac--) {
1041 ac_sockfd[ac] = create_ac_socket(ac);
1042 if (ac_sockfd[ac] < 0) {
1045 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
1047 error(1, rc, "Error while creating receiver");
1049 receivers[ac].valid = true;
1054 if (opt_interface) {
1057 memset(&ifr, 0, sizeof(ifr));
1058 strncpy(ifr.ifr_name, opt_interface, IFNAMSIZ-1);
1059 if (ioctl(ac_sockfd[AC_VO], SIOCGIFINDEX, &ifr) < 0) {
1060 error(1, 0, "unknown iface %s", opt_interface);
1062 cmsg.ipi.ipi_ifindex = ifr.ifr_ifindex;
1063 cmsg_len = sizeof(cmsg);
1065 /* create sendpoints */
1066 for (i = 0; i < nr_streams; i++) {
1067 struct stream *s = &streams[i];
1068 pthread_mutex_init(&s->mutex, NULL);
1069 calc_stream_params(s);
1070 rc = pthread_create(&thread, &attr, sender, (void*) s);
1071 if (rc) error(1, rc, "Error while creating sender");
1074 if (some_contract_not_accepted) {
1081 while (!exit_flag) {
1088 print_status(seconds);
1089 if (seconds == opt_count_sec)
1098 fprintf(stderr, "\nWaiting for threads to finish\n");
1099 wait_for_all_threads_to_finish();
1101 struct timespec end_timestamp, measure_length;
1102 clock_gettime(CLOCK_REALTIME,&end_timestamp);
1103 timespec_sub(&measure_length, &end_timestamp, &reset_timestamp);
1105 save_results(argc, argv, timespec2usec(&measure_length));