]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - wme_test/wclient.c
5d56436da7204ec2cf4c84007cd800844f2f097e
[frescor/fwp.git] / wme_test / wclient.c
1 #include <errno.h>
2 #include <error.h>
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include <netinet/in.h>
6 #include <arpa/inet.h>
7 #include <netdb.h>
8 #include <signal.h>
9 #include <sys/wait.h>
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <time.h>
14 #include <string.h>
15 #include <pthread.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <stdbool.h>
19 #include "common.h"
20 #include <semaphore.h>
21 #include <sys/ioctl.h>
22 #include <net/if.h>
23
24 #ifdef WITH_FWP
25 #include <fwp_confdefs.h>
26 #include <fwp.h>
27 #include <fwp_vres.h>
28 #include <ncurses.h>
29 #endif
30
31 #define MAX_STREAMS  10 
32 #define MIN_GRANULARITY 100
33
34 unsigned opt_packet_size = 800;
35 int opt_send_buf_size = -1;
36 unsigned opt_period_usec = 10*MSEC_TO_USEC;
37 char    *opt_interface;
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;
46
47 bool some_queue_is_full = false;
48 struct timespec reset_timestamp;
49
50 bool some_contract_not_accepted = false;
51
52 /* Locked when some queue is full to prevent multiple resets of
53    statstics. */
54 pthread_mutex_t queue_full_mutex = PTHREAD_MUTEX_INITIALIZER;
55
56 int ac_sockfd[AC_NUM];
57
58 struct receiver {
59         bool valid;
60         pthread_t thread;
61         unsigned received, last_received;
62 };
63
64 struct receiver receivers[AC_NUM];
65
66 FILE* logfd;
67 char* server_addr; 
68 char logfname[100];
69
70 /* maximal traffic delay in ms - 10 s*/
71 #define MAX_DELAY_US 10000000
72
73 struct delay_stat {
74         unsigned csc;           /* Client-server-client delay divided by 2 */
75         unsigned cs;            /* Client-server delay */
76         unsigned sc;            /* Server-client delay */
77 };
78
79 struct delay_stat delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
80 pthread_mutex_t delay_stats_mutex = PTHREAD_MUTEX_INITIALIZER;
81
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; 
87 };*/
88
89 struct stream {
90         /* Input parameters */
91         enum ac ac;             /*  */
92         int bandwidth_bps;      /* bits per second */
93         int jitter;             /* percent */
94         /* Mulualy exclusive input parameters */
95         int packet_size;
96         long period_usec;       /* all time units are in microseconds */
97
98         /* Internal fields */
99 #ifndef WITH_FWP
100         struct sockaddr_in rem_addr;
101 #else
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;
106         fwp_vres_d_t vres;
107         uint16_t resp_port;
108         struct receiver receiver;
109         long wc_delay;          /* worst-case delay  */
110 #endif
111
112         /* Statistics */
113         pthread_mutex_t mutex;
114         unsigned long long sent, really_sent, received;
115 };
116
117 static struct cmsg_ipi {
118         struct cmsghdr cm;
119         struct in_pktinfo ipi;
120 } cmsg = { {sizeof(struct cmsg_ipi), SOL_IP, IP_PKTINFO},
121            {0, }};
122 int cmsg_len = 0;
123
124 /*
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 },
131 };
132 */
133
134 struct stream streams[MAX_STREAMS];
135
136 unsigned int nr_streams = 0;
137
138 sem_t sem_thread_finished;
139
140 bool exit_flag = false;
141
142 #ifdef WITH_FWP
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()
148 #else
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()
154 #endif
155
156 void stopper()
157 {
158         int i;
159         exit_flag = true;
160
161         /* Interrupt all receivers */
162 #ifdef WITH_FWP
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);
167         }
168 #else
169         for (i=0; i < AC_NUM; i++) {
170                 pthread_kill(receivers[i].thread, SIGUSR1);
171         }       
172 #endif
173 }
174
175 void stream_to_text(char *stream_desc, size_t n, struct stream *stream, long long useconds)
176 {
177         char buf[3][12];
178         char real[100];
179
180         if (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);
185         } else {
186                 real[0]=0;
187         }
188         
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);
194 }
195
196 void save_results(int argc, char *argv[], int useconds)
197 {
198         int ac, i, maxi;
199         const int mini = 3000/opt_granularity_usec;
200         bool allzeros;
201         unsigned send_count[AC_NUM];
202
203         fprintf(stderr, "Writing data to %s... ", logfname);
204         fflush(stderr);
205
206         fprintf(logfd, "# Invoked as: ");
207         for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
208         if (opt_comment) {
209                 fprintf(logfd, "(%s)", opt_comment);
210         }
211         fprintf(logfd, "\n");
212
213         if (useconds/SEC_TO_USEC != opt_count_sec) {
214                 char buf[20];
215                 usec_to_text(buf, useconds);
216                 fprintf(logfd, "# Data gathered for %s.\n", buf);
217         }
218                 
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);
223         }
224
225         /* Find maximal delay */
226         allzeros = true;
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))
232                                 allzeros = false;
233                 }
234                 if (!allzeros) break;
235         }
236         maxi++;
237         if (maxi < mini) maxi = mini;
238
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++) {
242                 ac = streams[i].ac;
243                 send_count[ac] += streams[i].sent;
244         }
245
246 #if 0
247         /* Write pdf */
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++) { 
251                         if (sum[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);
255                 }
256         }
257         
258         fprintf(logfd,"\n\n");
259 #endif
260         
261         fprintf(logfd,"## Format: msec csc%% cs%% sc%%\n");
262
263         /* Write PDF */
264         for (ac = 0; ac < AC_NUM; ac++) {
265                 struct delay_stat integral = {0,0,0}, last = {-1,-1,-1};
266
267                 fprintf(logfd,"%f %f %f %f\n", 0.0, 0.0, 0.0, 0.0);
268
269                 if (send_count[ac] != 0) {
270                         i=0;
271                         while ((delay_stats[ac][i].csc == 0) &&
272                                (delay_stats[ac][i].cs == 0) &&
273                                (delay_stats[ac][i].sc == 0)) i++;
274                 
275                         for (i++; i < maxi+1; i++) {
276                                 if (memcmp(&last, &integral, sizeof(last))) {
277                                         char buf[3][20];
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]);
281                                                  
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] : "-"
286                                                 );
287                                         last = integral;
288                                 }
289                                 if (i>0) {
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;
293                                 }
294                         }
295                 }
296                 fprintf(logfd,"\n\n");
297         }
298         
299         fprintf(stderr, "finished.\n");
300         fclose(logfd);
301
302         exit(0);
303 }
304
305 int create_ac_socket(unsigned int ac) 
306 {
307         int sockfd;
308         unsigned int yes=1, tos;
309
310
311         if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
312         {
313                 error(0, errno, "Unable to open socket");
314                 return -1;
315         }
316         if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
317                 error(0, errno, "set non-blocking socket");
318                 return -1;
319         }
320         if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
321                 error(0, errno, "Unable to set socket");
322                 return -1;
323         }
324
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");
328                         return -1;
329                 }
330         }
331
332         
333         //tos = ((AC_NUM - ac) *2 - 1)*32;
334         tos = ac_to_tos[ac];
335         if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
336                 error(0, errno, "Unable to set TOS");
337                 close(sockfd);
338                 return -1;
339         }
340
341         return sockfd;
342 }
343
344 void empty_handler()
345 {
346 }
347
348 void reset_statistics()
349 {
350         int i;
351         for (i = 0; i < nr_streams; i++) {
352                 pthread_mutex_lock(&streams[i].mutex);
353                 streams[i].sent = 0;
354                 streams[i].really_sent = 0;
355                 streams[i].received = 0;
356                 pthread_mutex_unlock(&streams[i].mutex);
357         }
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);
362 }
363
364 #ifndef WITH_FWP
365 int recv_packet_native(unsigned ac, struct msg_t *msg)
366 {
367         int     mlen, ret;
368         fd_set fdset;
369         struct  sockaddr_in rem_addr;
370         unsigned int rem_addr_length; 
371
372         FD_ZERO(&fdset);
373         FD_SET(ac_sockfd[ac], &fdset);
374         rem_addr_length = sizeof(rem_addr);
375         mlen = -1;
376         while (!exit_flag) {
377                 ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
378                 if (ret < 0) {
379                         if (errno == EINTR) continue;
380                         error(0, errno, "receiver select");
381                         return -1;
382                 }
383                 mlen = recvfrom(ac_sockfd[ac], msg, sizeof(*msg), 0,
384                                 (struct sockaddr*)&rem_addr, &rem_addr_length);
385                 break;
386         }
387         return mlen;
388 }
389 #else
390 int recv_packet_fwp(struct stream *stream, struct msg_t *msg)
391 {
392         int     mlen;
393         mlen = fwp_recv(stream->resp_endpoint, msg, sizeof(*msg), 0);
394         return mlen;
395 }
396 #endif
397
398 void* receiver(void* arg)
399 {
400         struct msg_t    msg;
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;
404         int     mlen, ret;
405         unsigned ac;
406         
407         min_trans_time = ~0;
408         
409         block_signals();
410         set_rt_prio(99);
411
412         while (!exit_flag) {
413 #ifdef WITH_FWP
414                 struct stream *stream = arg;
415                 ac = stream->ac;
416                 mlen = recv_packet_fwp(stream, &msg);
417 #else
418                 ac = (unsigned)arg;
419                 mlen = recv_packet_native(ac, &msg);
420 #endif
421                 if (mlen < 0) {
422                         if (errno != EINTR)
423                                 error(0, errno, "receive_packet error");
424                         goto out;
425                 }       
426                 clock_gettime(CLOCK_REALTIME,&recv_timestamp);
427                 send_timestamp = msg.send_timestamp;
428                 server_timestamp = msg.sendback_timestamp;
429
430                 /* Check whether this message was sent after reset_statistics() */
431
432                 if ((ret = timespec_sub_usec(&send_timestamp, &reset_timestamp)) < 0) {
433                         continue; /* If so, don't count it */
434                 }
435
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);
439
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++;
443                 }
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++;
446                 }
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++;
449                 }
450                 pthread_mutex_unlock(&delay_stats_mutex);
451
452 #ifdef WITH_FWP
453                 if (trans_time_usec > stream->wc_delay) {
454                         stream->wc_delay = trans_time_usec;
455                 }
456 #endif
457                 receivers[ac].received++;
458                 
459                 pthread_mutex_lock(&streams[msg.stream].mutex);
460                 streams[msg.stream].received++;
461                 pthread_mutex_unlock(&streams[msg.stream].mutex);
462         
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); */
470         }
471 out:
472         sem_post(&sem_thread_finished);
473         return NULL;
474 }
475
476 /** 
477  * Send a packet.
478  * 
479  * @return -1 in case of error, 1 in case of sucessfull send and 0
480  *         when all buffers are full.
481  */
482 #ifndef WITH_FWP
483 static inline int 
484 send_packet_native(struct stream* stream, union msg_buff* buff)
485 {
486         struct iovec  iov;
487         struct msghdr msg;
488
489         iov.iov_base = buff;
490         iov.iov_len = stream->packet_size;
491         msg.msg_name = (void*)&stream->rem_addr;
492         msg.msg_namelen = sizeof(stream->rem_addr);
493         msg.msg_iov = &iov;
494         msg.msg_iovlen = 1;
495         msg.msg_flags = 0;
496         msg.msg_control = &cmsg;
497         msg.msg_controllen = cmsg_len;
498
499         int ret = 1;
500         
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;
509                                 reset_statistics();
510                         }
511                         ret = 0;
512                         break;
513                 } else {
514                         error(0, errno, "Error while sending");
515                         ret = -1;
516                         break;
517                 }
518         }
519         return ret;
520 }
521 #else
522 static inline int 
523 send_packet_fwp(struct stream* stream, union msg_buff* buff)
524 {
525         int ret;
526
527         buff->msg.resp_port = htons(stream->resp_port);
528         ret = fwp_send(stream->endpoint, buff, stream->packet_size, 0);
529         
530         return ret;
531 }
532 #endif
533
534 static inline void
535 wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
536 {
537         struct timespec time_to_wait, current_time, period, interval;
538         unsigned period_usec = stream->period_usec;
539
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));
545         } else {
546                 period.tv_nsec = USEC_TO_NSEC*(period_usec);
547         }
548         period.tv_sec = 0;
549         
550         timespec_add(&time_to_wait, last_send_time, &period);
551         clock_gettime(CLOCK_REALTIME,&current_time);
552         timespec_sub(&interval,&time_to_wait,&current_time);
553         nanosleep(&interval,NULL);
554 }
555
556
557 void* sender(void* arg)
558 {
559         union msg_buff buff;
560         unsigned long int seqn;
561         struct stream* stream = (struct stream*) arg;
562         int ret;
563
564 #ifndef WITH_FWP
565         char stream_desc[100];
566         stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
567         printf("%s\n", stream_desc);
568 #endif
569         if (stream->bandwidth_bps == 0)
570                 goto out;
571
572         seqn = 0;
573         
574         block_signals();
575         set_rt_prio(90-stream->ac);
576
577         while (!exit_flag) {
578
579 /*              buff.msg.seqn = seqn++; */
580 /*              buff.msg.tos = ac_to_tos[stream->ac]; */
581                 buff.msg.stream = stream-streams;
582                 
583                 clock_gettime(CLOCK_REALTIME,&buff.msg.send_timestamp);
584
585                 ret = send_packet(stream, &buff);
586                 if (ret < 0) {
587                         goto out;
588                 }
589
590                 pthread_mutex_lock(&stream->mutex);
591                 stream->sent++;
592                 if (ret > 0)
593                         stream->really_sent++;
594                 pthread_mutex_unlock(&stream->mutex);
595
596 #ifdef DEBUG
597                 printf("%d", stream->ac);
598                 fflush(stdout);
599 #endif
600
601                 wait_for_next_send(stream, &buff.msg.send_timestamp);
602         }
603 out:
604         sem_post(&sem_thread_finished);
605         return NULL;
606 }
607
608 #ifdef WITH_FWP
609 static int negotiate_contract_for_stream_fwp(struct stream *stream)
610 {
611         fwp_contract_t contract;
612         fwp_vres_d_t vres2;
613         int ret;
614
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;
620         
621         stream->contract_send = fwp_contract_create(&contract);
622         ret = fwp_contract_negotiate(stream->contract_send, &stream->vres);
623         if (ret != 0) {
624                 stream->contract_send = NULL;
625 /*              fprintf(stderr, "Send contract was not accepted\n\n\n"); */
626                 return ret;
627         }
628
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;
634
635         stream->contract_resp = fwp_contract_create(&contract);
636         ret = fwp_contract_negotiate(stream->contract_resp, &vres2);
637         if (ret != 0) {
638                 stream->contract_resp = NULL;
639 /*              fprintf(stderr, "Receive contract was not accepted\n\n\n"); */
640                 return ret;
641         }
642
643         /* We don't use the vres at server, since the server doesn't
644          * know the parameters. Instread, server uses plain
645          * sockets. */
646         
647         return ret;
648 }
649 #endif
650
651 #ifdef WITH_FWP
652 static void create_stream_endpoint_fwp(struct stream *stream)
653 {
654 /*      fwp_endpoint_attr_t  attr; */
655         int ret;
656         struct hostent* ph;
657
658         stream->ac = fwp_vres_get_ac(stream->vres);
659
660 /*      fwp_endpoint_attr_init(&attr); */
661 /*      fwp_endpoint_attr_setreliability(&attr, FWP_EPOINT_BESTEFFORT); */
662
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");
670                 
671                 ret = fwp_send_endpoint_bind(stream->endpoint, stream->vres);
672                 if (ret != 0) error(1, errno, "fwp_send_endpoint_bind");
673                 
674                 ret = fwp_receive_endpoint_create(0, NULL, &stream->resp_endpoint);
675                 if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
676                 
677                 unsigned int port;
678                 fwp_endpoint_get_params(stream->resp_endpoint, NULL, &port, NULL);
679                 stream->resp_port = port;
680
681                 ret = pthread_create(&stream->receiver.thread, NULL, receiver, (void*)stream);
682                 if (ret) error(1, ret, "Error while creating receiver");
683                 
684                 stream->receiver.valid = true;
685         }
686         else {
687                 error(1, errno, "gethostbyname(%s)", server_addr);
688         }
689
690 }
691 #else
692 static void create_stream_endpoint_native(struct stream *stream)
693 {
694         struct hostent* ph;
695
696         memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
697
698         stream->rem_addr.sin_family = AF_INET;
699         ph = gethostbyname(server_addr);
700         if (ph)
701                 stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
702         else {
703                 error(1, errno, "gethostbyname(%s)", server_addr);
704         }
705         stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
706 }
707 #endif
708
709 static inline void
710 calc_stream_params(struct stream *stream)
711 {
712         int packet_size;
713         unsigned period_usec;
714         int bandwidth;
715         int ret;
716
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;
721
722         bandwidth = stream->bandwidth_bps;
723
724         /* Avoid arithmetic exception. Server thread will exit if
725            stream->bandwidth_bps == 0. */
726         if (bandwidth == 0) bandwidth = 1;
727
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;
735         } else {
736                 char buf[200];
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);
739         }
740
741         if (packet_size < sizeof(struct msg_t)) {
742                 error(1, 0, "Packet size too small (min %d)", sizeof(struct msg_t));
743         }
744
745         stream->packet_size = packet_size;
746         stream->period_usec = period_usec;
747         stream->jitter = opt_jitter;
748
749         ret = negotiate_contract_for_stream(stream);
750         if (ret == 0) {
751                 create_stream_endpoint(stream);
752         } else {
753                 char buf[200];
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;
758         }
759 }
760
761 /** 
762  * Parse -b parameter.
763  * 
764  * @param params String to parse
765  * 
766  * @return NULL in case of success, pointer to a problematic character
767  *         on error.
768  */
769 char* parse_bandwidths(char *params)
770 {
771         struct stream *sp = &streams[nr_streams];
772
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" };
775                 int i;
776                 char *next_char;
777
778                 if (strlen(params) < 2)
779                         return params;
780                 for (i=0; i<AC_NUM; i++) {
781                         if (strncmp(params, ac_ids[i], 2) == 0) {
782                                 sp->ac = i;
783                                 params+=strlen(ac_ids[i]);
784                                 break;
785                         }
786                 }
787                 if (i==AC_NUM)
788                         return params;
789
790                 long bw;
791                 if (*params == ':') {
792                         params++;
793
794                         bw = strtol(params, &next_char, 10);
795                         if (next_char == params)
796                                 return params;
797                         params = next_char;
798                 } else
799                         bw = -1;
800                 
801                 sp->bandwidth_bps = bw*Kbit;
802
803                 long period = 0;
804                 long packet_size = 0;
805                 if (*params == '@') {
806                         params++;
807                         period = strtol(params, &next_char, 10);
808                         if (period == 0)
809                                 return params;
810                         params = next_char;
811                 }
812                 else {
813                         if (*params == '/') {
814                                 params++;
815                                 packet_size = strtol(params, &next_char, 10);
816                                 if (packet_size == 0)
817                                         return params;
818                                 params = next_char;
819                         } else {
820                                 packet_size = -1; 
821                                 period = -1;
822                         }
823                 }
824                 sp->period_usec = period*MSEC_TO_USEC;
825                 sp->packet_size = packet_size;
826
827                 
828
829                 if (*params != '\0' && *params != ',')
830                         return params;
831                 nr_streams++;
832                 sp++;
833                 if (*params == ',')
834                         params++;
835         }
836         return NULL;
837 }
838
839 #ifdef WITH_FWP
840 void wait_for_all_threads_to_finish_fwp(void)
841 {
842         int i;
843         /* Wait for all threads to finish */
844         /* FIXME: */
845 /*      for (i=0; i < 2*nr_streams; i++) { */
846 /*              sem_wait(&sem_thread_finished); */
847 /*      } */
848 }
849 #else
850 void wait_for_all_threads_to_finish_native(void)
851 {
852         int i;
853         /* Wait for all threads to finish */
854         for (i=0; i < nr_streams + AC_NUM; i++) {
855                 sem_wait(&sem_thread_finished);
856         }
857 }
858 #endif
859
860 #ifdef WITH_FWP
861 void init_gui()
862 {
863         initscr();
864         cbreak();
865         noecho();
866 }
867
868 #define addfield(title, format, ...)                                    \
869         move(y, x);                                                     \
870         x+=strlen(title)+1;                                             \
871         if (i == 0) addstr(title);                                      \
872         else {                                                          \
873                 snprintf(str, sizeof(str), format, __VA_ARGS__);        \
874                 addstr(str);                                            \
875         }
876
877 void print_status(int seconds)
878 {
879         int i;
880         char str[200], s1[20];
881         int x = 0, y;
882         struct stream *s = NULL;
883         
884         for (i = 0; i <= nr_streams; i++) {
885                 if (i>0) s = &streams[i-1];
886                 y=i;
887                 x=0;
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);
895         }
896         refresh();
897 }
898 #else
899 void init_gui() {}
900 void print_status(int seconds)
901 {
902         int ac;
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);
908         }
909         fflush(stderr);
910 }
911 #endif
912
913 int main(int argc, char *argv[])
914 {
915         int i, rc, frames, seconds;
916         pthread_attr_t attr;
917         pthread_t thread;
918         char opt;
919
920
921         while ((opt = getopt(argc, argv, "B:b:C:c:g:I:j:o:qQ:s:T:")) != -1) {
922                 switch (opt) {
923                         case 'B':
924                                 opt_def_bandwidth = atoi(optarg);
925                                 break;
926                         case 'b': {
927                                 char *errpos;
928                                 errpos = parse_bandwidths(optarg);
929                                 if (errpos != NULL) {
930                                         if (*errpos == '\0')
931                                                 error(1, 0, "Bandwidth parse error - string to short");
932                                         else
933                                                 error(1, 0, "Bandwidth parse error at '%s'", errpos);
934                                 }
935                                 break;
936                         }
937                         case 'C':
938                                 opt_comment = optarg;
939                                 break;
940                         case 'c':
941                                 opt_count_sec = atoi(optarg);
942                                 break;
943                         case 'g':
944                                 opt_granularity_usec = atoi(optarg);
945                                 if (opt_granularity_usec < MIN_GRANULARITY) {
946                                         error(1, 0, "Granulatiry too small (min %d)", MIN_GRANULARITY);
947                                 }
948                                 break;
949                         case 'I':
950                                 opt_interface = optarg;
951                                 break;
952                         case 'j':
953                                 #ifdef WITH_FWP
954                                 error(1, 0, "-j is not allowd when compiled with FWP");
955                                 #else
956                                 opt_jitter = atoi(optarg);
957                                 #endif
958                                 break;
959                         case 'o':
960                                 opt_output = optarg;
961                                 break;
962                         case 'Q':
963                                 opt_send_buf_size = atoi(optarg);
964                                 break;
965                         case 'q':
966                                 opt_wait_for_queue_is_full = true;
967                                 break;
968                         case 's':
969                                 opt_packet_size = atoi(optarg);
970                                 break;
971                         case 'T':
972                                 opt_def_period_msec = atoi(optarg);
973                                 break;
974                         default:
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");
989                                 exit(1);
990                 }
991         }
992         if (opt_packet_size && opt_def_period_msec) {
993                 error(1, 0, "Error: Nonzero -T and -s can't be used together!");
994         }
995
996         if (optind < argc) {
997                 server_addr = argv[optind];
998         } else {
999                 error(1, 0, "Expected server address argument");
1000         }
1001
1002         if (nr_streams == 0)
1003                 parse_bandwidths("BE");
1004                 
1005         pthread_attr_init(&attr);
1006
1007         snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
1008
1009         if ((logfd = fopen(logfname,"w+")) == NULL) {
1010                 error(1, errno ,"Can not open %s", logfname);
1011         }
1012         if (signal(SIGTERM, stopper) == SIG_ERR) {
1013                 error(1, errno, "Error in signal registration");
1014         }
1015                 
1016         if (signal(SIGINT, stopper) == SIG_ERR) {
1017                 error(1, errno, "Signal handler registration error");
1018         }
1019
1020         struct sigaction sa;
1021         sa.sa_handler = empty_handler;
1022         sa.sa_flags = 0;        /* don't restart syscalls */
1023
1024         if (sigaction(SIGUSR1, &sa, NULL) < 0) {
1025                 error(1, errno, "sigaction error");
1026         }
1027
1028         sem_init(&sem_thread_finished, 0, 0);
1029
1030         reset_statistics();
1031
1032 #ifdef WITH_FWP
1033         rc = fwp_init();
1034         if (rc != 0) {
1035                 error(1, errno, "FWP initialization failed");
1036         }
1037 #else
1038         int ac;
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) {
1043                         return 1;
1044                 }
1045                 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
1046                 if (rc) {
1047                         error(1, rc, "Error while creating receiver");
1048                 }
1049                 receivers[ac].valid = true;
1050         }               
1051 #endif
1052
1053                         
1054         if (opt_interface) {
1055                 struct ifreq ifr;
1056
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);
1061                 }
1062                 cmsg.ipi.ipi_ifindex = ifr.ifr_ifindex;
1063                 cmsg_len = sizeof(cmsg);
1064         }
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");
1072         }
1073
1074         if (some_contract_not_accepted) {
1075                 stopper();
1076         } else {
1077                 //init_gui();
1078
1079                 seconds = 1;
1080                 frames=0;
1081                 while (!exit_flag) {
1082                         usleep(40000);
1083                         frames++;
1084                         if (frames>=25) {
1085                                 seconds++;
1086                                 frames = 0;
1087                         }
1088                         print_status(seconds);
1089                         if (seconds == opt_count_sec)
1090                                 stopper();
1091                 }
1092         }
1093
1094 #ifdef WITH_FWP
1095         //endwin();
1096         fwp_done();
1097 #else
1098         fprintf(stderr, "\nWaiting for threads to finish\n");
1099         wait_for_all_threads_to_finish();
1100
1101         struct timespec end_timestamp, measure_length;
1102         clock_gettime(CLOCK_REALTIME,&end_timestamp);
1103         timespec_sub(&measure_length, &end_timestamp, &reset_timestamp);
1104
1105         save_results(argc, argv, timespec2usec(&measure_length));
1106 #endif
1107
1108         return 0;
1109 }