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 uint64_t reset_timestamp; /* [nsec] */
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()
352 for (i = 0; i < nr_streams; i++) {
353 pthread_mutex_lock(&streams[i].mutex);
355 streams[i].really_sent = 0;
356 streams[i].received = 0;
357 pthread_mutex_unlock(&streams[i].mutex);
359 pthread_mutex_lock(&delay_stats_mutex);
360 clock_gettime(CLOCK_REALTIME, &ts);
361 reset_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
362 memset(delay_stats, 0, sizeof(delay_stats));
363 pthread_mutex_unlock(&delay_stats_mutex);
367 int recv_packet_native(unsigned ac, struct msg_t *msg)
371 struct sockaddr_in rem_addr;
372 unsigned int rem_addr_length;
375 FD_SET(ac_sockfd[ac], &fdset);
376 rem_addr_length = sizeof(rem_addr);
379 ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
381 if (errno == EINTR) continue;
382 error(0, errno, "receiver select");
385 mlen = recvfrom(ac_sockfd[ac], msg, sizeof(*msg), 0,
386 (struct sockaddr*)&rem_addr, &rem_addr_length);
392 int recv_packet_fwp(struct stream *stream, struct msg_t *msg)
395 mlen = fwp_recv(stream->resp_endpoint, msg, sizeof(*msg), 0);
400 void* receiver(void* arg)
403 long long int trans_time_usec, client_to_server_usec, server_to_client_usec;
404 long long int min_trans_time;
406 uint64_t send_timestamp, server_timestamp, recv_timestamp;
417 struct stream *stream = arg;
419 mlen = recv_packet_fwp(stream, &msg);
422 mlen = recv_packet_native(ac, &msg);
426 error(0, errno, "receive_packet error");
429 clock_gettime(CLOCK_REALTIME,&ts);
430 recv_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
431 send_timestamp = msg.send_timestamp;
432 server_timestamp = msg.sendback_timestamp;
434 /* Check whether this message was sent after reset_statistics() */
436 if (send_timestamp < reset_timestamp) {
437 continue; /* If so, don't count it */
440 trans_time_usec = (recv_timestamp - send_timestamp) / 2 / 1000;
441 client_to_server_usec = (server_timestamp - send_timestamp) / 1000;
442 server_to_client_usec = (recv_timestamp - server_timestamp) / 1000;
444 pthread_mutex_lock(&delay_stats_mutex);
445 if (trans_time_usec < MAX_DELAY_US && trans_time_usec >= 0) {
446 delay_stats[ac][trans_time_usec/opt_granularity_usec].csc++;
448 if (client_to_server_usec < MAX_DELAY_US && client_to_server_usec >= 0) {
449 delay_stats[ac][client_to_server_usec/opt_granularity_usec].cs++;
451 if (server_to_client_usec < MAX_DELAY_US && server_to_client_usec >= 0) {
452 delay_stats[ac][server_to_client_usec/opt_granularity_usec].sc++;
454 pthread_mutex_unlock(&delay_stats_mutex);
457 if (trans_time_usec > stream->wc_delay) {
458 stream->wc_delay = trans_time_usec;
461 receivers[ac].received++;
463 pthread_mutex_lock(&streams[msg.stream].mutex);
464 streams[msg.stream].received++;
465 pthread_mutex_unlock(&streams[msg.stream].mutex);
467 /*if (trans_time_nsec < min_trans_time)
468 min_trans_time = trans_time_nsec;*/
469 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
470 "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
471 msg.seqn, msg.tos, send_timestamp.tv_sec,\
472 send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
473 recv_timestamp.tv_nsec, trans_time_msec); */
476 sem_post(&sem_thread_finished);
483 * @return -1 in case of error, 1 in case of sucessfull send and 0
484 * when all buffers are full.
488 send_packet_native(struct stream* stream, union msg_buff* buff)
494 iov.iov_len = stream->packet_size;
495 msg.msg_name = (void*)&stream->rem_addr;
496 msg.msg_namelen = sizeof(stream->rem_addr);
500 msg.msg_control = &cmsg;
501 msg.msg_controllen = cmsg_len;
505 while (sendmsg(ac_sockfd[stream->ac], &msg, 0) < 0) {
506 if (errno == EINTR) continue;
507 if (errno == EAGAIN) {
508 if (opt_wait_for_queue_is_full &&
509 !some_queue_is_full &&
510 /* We use mutex as atomic test and set */
511 (pthread_mutex_trylock(&queue_full_mutex) != EBUSY)) {
512 some_queue_is_full = true;
518 error(0, errno, "Error while sending");
527 send_packet_fwp(struct stream* stream, union msg_buff* buff)
531 buff->msg.resp_port = htons(stream->resp_port);
532 ret = fwp_send(stream->endpoint, buff, stream->packet_size, 0);
539 wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
541 struct timespec time_to_wait, current_time, period, interval;
542 unsigned period_usec = stream->period_usec;
544 /* |~~~+~~~| jitter interval (width = 2*stream->jitter percentage from period)*/
545 /* |-------------| nominal period*/
546 if (stream->jitter) {
547 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-stream->jitter)/100
548 + rand() % (2*period_usec*stream->jitter/100));
550 period.tv_nsec = USEC_TO_NSEC*(period_usec);
554 timespec_add(&time_to_wait, last_send_time, &period);
555 clock_gettime(CLOCK_REALTIME,¤t_time);
556 timespec_sub(&interval,&time_to_wait,¤t_time);
557 nanosleep(&interval,NULL);
561 void* sender(void* arg)
564 unsigned long int seqn;
565 struct stream* stream = (struct stream*) arg;
570 char stream_desc[100];
571 stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
572 printf("%s\n", stream_desc);
574 if (stream->bandwidth_bps == 0)
580 set_rt_prio(90-stream->ac);
584 /* buff.msg.seqn = seqn++; */
585 /* buff.msg.tos = ac_to_tos[stream->ac]; */
586 buff.msg.stream = stream-streams;
588 clock_gettime(CLOCK_REALTIME,&ts);
589 buff.msg.send_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
591 ret = send_packet(stream, &buff);
596 pthread_mutex_lock(&stream->mutex);
599 stream->really_sent++;
600 pthread_mutex_unlock(&stream->mutex);
603 printf("%d", stream->ac);
607 wait_for_next_send(stream, &ts);
610 sem_post(&sem_thread_finished);
615 static int negotiate_contract_for_stream_fwp(struct stream *stream)
617 fwp_contract_t contract;
621 /* Contract for client->server stream */
622 memset(&contract, 0, sizeof(contract));
623 contract.budget = stream->packet_size;
624 contract.period_usec = stream->period_usec;
625 contract.deadline_usec = 3*stream->period_usec;
627 stream->contract_send = fwp_contract_create(&contract);
628 ret = fwp_contract_negotiate(stream->contract_send, &stream->vres);
630 stream->contract_send = NULL;
631 /* fprintf(stderr, "Send contract was not accepted\n\n\n"); */
635 /* Contract for server->client stream */
636 memset(&contract, 0, sizeof(contract));
637 contract.budget = stream->packet_size;
638 contract.period_usec = stream->period_usec;
639 contract.deadline_usec = 3*stream->period_usec;
641 stream->contract_resp = fwp_contract_create(&contract);
642 ret = fwp_contract_negotiate(stream->contract_resp, &vres2);
644 stream->contract_resp = NULL;
645 /* fprintf(stderr, "Receive contract was not accepted\n\n\n"); */
649 /* We don't use the vres at server, since the server doesn't
650 * know the parameters. Instread, server uses plain
658 static void create_stream_endpoint_fwp(struct stream *stream)
660 /* fwp_endpoint_attr_t attr; */
664 stream->ac = fwp_vres_get_ac(stream->vres);
666 /* fwp_endpoint_attr_init(&attr); */
667 /* fwp_endpoint_attr_setreliability(&attr, FWP_EPOINT_BESTEFFORT); */
669 ph = gethostbyname(server_addr);
670 if (ph && ph->h_addr_list[0]) {
671 struct in_addr *a = (struct in_addr *)(ph->h_addr_list[0]);
672 ret = fwp_send_endpoint_create(a->s_addr, BASE_PORT + stream->ac,
673 NULL, &stream->endpoint);
674 /* stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac); */
675 if (ret < 0) error(1, errno, "fwp_send_endpoint_create");
677 ret = fwp_send_endpoint_bind(stream->endpoint, stream->vres);
678 if (ret != 0) error(1, errno, "fwp_send_endpoint_bind");
680 ret = fwp_receive_endpoint_create(0, NULL, &stream->resp_endpoint);
681 if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
684 fwp_endpoint_get_params(stream->resp_endpoint, NULL, &port, NULL);
685 stream->resp_port = port;
687 ret = pthread_create(&stream->receiver.thread, NULL, receiver, (void*)stream);
688 if (ret) error(1, ret, "Error while creating receiver");
690 stream->receiver.valid = true;
693 error(1, errno, "gethostbyname(%s)", server_addr);
698 static void create_stream_endpoint_native(struct stream *stream)
702 memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
704 stream->rem_addr.sin_family = AF_INET;
705 ph = gethostbyname(server_addr);
707 stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
709 error(1, errno, "gethostbyname(%s)", server_addr);
711 stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
716 calc_stream_params(struct stream *stream)
719 unsigned period_usec;
723 /* If some parameters are not set explicitely, use default values. */
724 if (stream->bandwidth_bps < 0) stream->bandwidth_bps = opt_def_bandwidth * Kbit;
725 if (stream->packet_size < 0) stream->packet_size = opt_packet_size;
726 if (stream->period_usec < 0) stream->period_usec = opt_def_period_msec * MSEC_TO_USEC;
728 bandwidth = stream->bandwidth_bps;
730 /* Avoid arithmetic exception. Server thread will exit if
731 stream->bandwidth_bps == 0. */
732 if (bandwidth == 0) bandwidth = 1;
734 if (stream->packet_size) {
735 packet_size = stream->packet_size;
736 period_usec = SEC_TO_USEC*packet_size*8/bandwidth;
737 if (period_usec == 0) period_usec = 1;
738 } else if (stream->period_usec) {
739 period_usec = stream->period_usec;
740 packet_size = (long long)bandwidth/8 * period_usec/SEC_TO_USEC;
743 stream_to_text(buf, sizeof(buf), stream, 0);
744 error(1, 0, "Neither packet size nor period was specified for a stream %s", buf);
747 if (packet_size < sizeof(struct msg_t)) {
748 error(1, 0, "Packet size too small (min %d)", sizeof(struct msg_t));
751 stream->packet_size = packet_size;
752 stream->period_usec = period_usec;
753 stream->jitter = opt_jitter;
755 ret = negotiate_contract_for_stream(stream);
757 create_stream_endpoint(stream);
760 stream_to_text(buf, sizeof(buf), stream, 0);
761 fprintf(stderr, "Contract hasn't been accepted:\n%s\n", buf);
762 stream->bandwidth_bps = 0;
763 some_contract_not_accepted = true;
768 * Parse -b parameter.
770 * @param params String to parse
772 * @return NULL in case of success, pointer to a problematic character
775 char* parse_bandwidths(char *params)
777 struct stream *sp = &streams[nr_streams];
779 while (*params && nr_streams < MAX_STREAMS) {
780 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
784 if (strlen(params) < 2)
786 for (i=0; i<AC_NUM; i++) {
787 if (strncmp(params, ac_ids[i], 2) == 0) {
789 params+=strlen(ac_ids[i]);
797 if (*params == ':') {
800 bw = strtol(params, &next_char, 10);
801 if (next_char == params)
807 sp->bandwidth_bps = bw*Kbit;
810 long packet_size = 0;
811 if (*params == '@') {
813 period = strtol(params, &next_char, 10);
819 if (*params == '/') {
821 packet_size = strtol(params, &next_char, 10);
822 if (packet_size == 0)
830 sp->period_usec = period*MSEC_TO_USEC;
831 sp->packet_size = packet_size;
835 if (*params != '\0' && *params != ',')
846 void wait_for_all_threads_to_finish_fwp(void)
849 /* Wait for all threads to finish */
851 /* for (i=0; i < 2*nr_streams; i++) { */
852 /* sem_wait(&sem_thread_finished); */
856 void wait_for_all_threads_to_finish_native(void)
859 /* Wait for all threads to finish */
860 for (i=0; i < nr_streams + AC_NUM; i++) {
861 sem_wait(&sem_thread_finished);
874 #define addfield(title, format, ...) \
876 x+=strlen(title)+1; \
877 if (i == 0) addstr(title); \
879 snprintf(str, sizeof(str), format, __VA_ARGS__); \
883 void print_status(int seconds)
886 char str[200], s1[20];
888 struct stream *s = NULL;
890 for (i = 0; i <= nr_streams; i++) {
891 if (i>0) s = &streams[i-1];
894 addfield("Stream", "%d", i);
895 addfield("Bandwidth", "%s", bandwidth_to_text(s1, s->bandwidth_bps));
896 addfield("Packet size", "%d bytes", s->packet_size);
897 addfield("Period ", "%s", usec_to_text(s1, s->period_usec));
898 addfield("AC ", "%s", ac_to_text[s->ac]);
899 addfield("Worst-case delay", "%s", usec_to_text(s1, s->wc_delay));
900 addfield("Received responses", "%lld", s->received);
906 void print_status(int seconds)
909 fprintf(stderr, "\r%3ds", seconds);
910 for (ac = 0; ac < AC_NUM; ac++) {
911 int delta = receivers[ac].received - receivers[ac].last_received;
912 receivers[ac].last_received = receivers[ac].received;
913 fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
919 int main(int argc, char *argv[])
921 int i, rc, frames, seconds;
927 while ((opt = getopt(argc, argv, "B:b:C:c:g:I:j:o:qQ:s:T:")) != -1) {
930 opt_def_bandwidth = atoi(optarg);
934 errpos = parse_bandwidths(optarg);
935 if (errpos != NULL) {
937 error(1, 0, "Bandwidth parse error - string to short");
939 error(1, 0, "Bandwidth parse error at '%s'", errpos);
944 opt_comment = optarg;
947 opt_count_sec = atoi(optarg);
950 opt_granularity_usec = atoi(optarg);
951 if (opt_granularity_usec < MIN_GRANULARITY) {
952 error(1, 0, "Granulatiry too small (min %d)", MIN_GRANULARITY);
956 opt_interface = optarg;
960 error(1, 0, "-j is not allowd when compiled with FWP");
962 opt_jitter = atoi(optarg);
969 opt_send_buf_size = atoi(optarg);
972 opt_wait_for_queue_is_full = true;
975 opt_packet_size = atoi(optarg);
978 opt_def_period_msec = atoi(optarg);
981 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
982 fprintf(stderr, "Options:\n");
983 fprintf(stderr, " -B default bandwidth for -b option [kbit]\n");
984 fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec> or /<bytes>][,...]\n");
985 fprintf(stderr, " -C comment (added to header)\n");
986 fprintf(stderr, " -c count (number of seconds to run)\n");
987 fprintf(stderr, " -g histogram granularity [usec]\n");
988 fprintf(stderr, " -I <interface> send packets from this interface");
989 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
990 fprintf(stderr, " -o output filename (.dat will be appended)\n");
991 fprintf(stderr, " -q gather statistics only after some queue becomes full\n");
992 fprintf(stderr, " -Q <bytes> set size for socket send buffers\n");
993 fprintf(stderr, " -s size of data payload in packets [bytes] (default: %d)\n", opt_packet_size);
994 fprintf(stderr, " -T default period for -b option [msec]\n");
998 if (opt_packet_size && opt_def_period_msec) {
999 error(1, 0, "Error: Nonzero -T and -s can't be used together!");
1002 if (optind < argc) {
1003 server_addr = argv[optind];
1005 error(1, 0, "Expected server address argument");
1008 if (nr_streams == 0)
1009 parse_bandwidths("BE");
1011 pthread_attr_init(&attr);
1013 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
1015 if ((logfd = fopen(logfname,"w+")) == NULL) {
1016 error(1, errno ,"Can not open %s", logfname);
1018 if (signal(SIGTERM, stopper) == SIG_ERR) {
1019 error(1, errno, "Error in signal registration");
1022 if (signal(SIGINT, stopper) == SIG_ERR) {
1023 error(1, errno, "Signal handler registration error");
1026 struct sigaction sa;
1027 sa.sa_handler = empty_handler;
1028 sa.sa_flags = 0; /* don't restart syscalls */
1030 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
1031 error(1, errno, "sigaction error");
1034 sem_init(&sem_thread_finished, 0, 0);
1041 error(1, errno, "FWP initialization failed");
1045 /* create four receivers each per AC */
1046 for (ac = AC_NUM - 1; ac >= 0; ac--) {
1047 ac_sockfd[ac] = create_ac_socket(ac);
1048 if (ac_sockfd[ac] < 0) {
1051 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
1053 error(1, rc, "Error while creating receiver");
1055 receivers[ac].valid = true;
1060 if (opt_interface) {
1063 memset(&ifr, 0, sizeof(ifr));
1064 strncpy(ifr.ifr_name, opt_interface, IFNAMSIZ-1);
1065 if (ioctl(ac_sockfd[AC_VO], SIOCGIFINDEX, &ifr) < 0) {
1066 error(1, 0, "unknown iface %s", opt_interface);
1068 cmsg.ipi.ipi_ifindex = ifr.ifr_ifindex;
1069 cmsg_len = sizeof(cmsg);
1071 /* create sendpoints */
1072 for (i = 0; i < nr_streams; i++) {
1073 struct stream *s = &streams[i];
1074 pthread_mutex_init(&s->mutex, NULL);
1075 calc_stream_params(s);
1076 rc = pthread_create(&thread, &attr, sender, (void*) s);
1077 if (rc) error(1, rc, "Error while creating sender");
1080 if (some_contract_not_accepted) {
1087 while (!exit_flag) {
1098 print_status(seconds);
1099 if (seconds == opt_count_sec)
1108 fprintf(stderr, "\nWaiting for threads to finish\n");
1109 wait_for_all_threads_to_finish();
1112 uint64_t end_timestamp, measure_length;
1113 clock_gettime(CLOCK_REALTIME,&ts);
1114 end_timestamp = ts.tv_sec*1000000000LL+ts.tv_nsec;
1115 measure_length = end_timestamp - reset_timestamp;
1117 save_results(argc, argv, measure_length/1000);