]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - wme_test/wclient.c
0011be58dc944c423ab7b281c8a346e95383b6dd
[frescor/fwp.git] / wme_test / wclient.c
1 #include <errno.h>
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <netinet/in.h>
5 #include <arpa/inet.h>
6 #include <netdb.h>
7 #include <signal.h>
8 #include <sys/wait.h>
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <time.h>
13 #include <string.h>
14 #include <pthread.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdbool.h>
18 #include "common.h"
19 #include <semaphore.h>
20 #include <sys/ioctl.h>
21 #include <net/if.h>
22
23 #ifdef WITH_FWP
24 #include <fwp_confdefs.h>
25 #include <fwp.h>
26 #include <fwp_vres.h>
27 #endif
28
29 #define MAX_STREAMS  10 
30 #define MIN_GRANULARITY 100
31
32 unsigned opt_packet_size = 800;
33 int opt_send_buf_size = -1;
34 unsigned opt_period_usec = 10*MSEC_TO_USEC;
35 char    *opt_interface;
36 unsigned opt_jitter = 0;
37 char    *opt_output = "delay_stats";
38 unsigned opt_count_sec = 0;
39 unsigned opt_def_bandwidth = 200;
40 unsigned opt_def_period_msec = 0;
41 int opt_granularity_usec = MIN_GRANULARITY;
42 bool opt_wait_for_queue_is_full; /* Don't gather any statistics until any queue is full */
43 char *opt_comment = NULL;
44
45 bool some_queue_is_full = false;
46 struct timespec reset_timestamp;
47
48 /* Locked when some queue is full to prevent multiple resets of
49    statstics. */
50 pthread_mutex_t queue_full_mutex = PTHREAD_MUTEX_INITIALIZER;
51
52 int ac_sockfd[AC_NUM];
53
54 struct receiver {
55         pthread_t thread;
56         unsigned received, last_received;
57 };
58
59 struct receiver receivers[AC_NUM];
60
61 FILE* logfd;
62 char* server_addr; 
63 char logfname[100];
64
65 /* maximal traffic delay in ms - 10 s*/
66 #define MAX_DELAY_US 10000000
67
68 struct delay_stat {
69         unsigned csc;           /* Client-server-client delay divided by 2 */
70         unsigned cs;            /* Client-server delay */
71         unsigned sc;            /* Server-client delay */
72 };
73
74 struct delay_stat delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
75 pthread_mutex_t delay_stats_mutex = PTHREAD_MUTEX_INITIALIZER;
76
77 /*struct ac_stats[AC_NUM] {
78    unsigned long int min_trans_time;
79    unsigned long int sum_trans_time;
80    struct timespec   recv_timestamp;
81    struct timespec   send_timestamp; 
82 };*/
83
84 struct stream {
85         /* Input parameters */
86         enum ac ac;             /*  */
87         int bandwidth_bps;      /* bits per second */
88         int jitter;             /* percent */
89         /* Mulualy exclusive input parameters */
90         int packet_size;
91         long period_usec;       /* all time units are in microseconds */
92
93         /* Internal fields */
94 #ifndef WITH_FWP
95         struct sockaddr_in rem_addr;
96 #else
97         fwp_endpoint_d_t endpoint;
98         fwp_endpoint_d_t resp_endpoint;
99         fwp_vres_d_t vres;
100         uint16_t resp_port;
101         struct receiver receiver;
102 #endif
103
104         /* Statistics */
105         pthread_mutex_t mutex;
106         unsigned long long sent, really_sent, received;
107 };
108
109 static struct cmsg_ipi {
110         struct cmsghdr cm;
111         struct in_pktinfo ipi;
112 } cmsg = { {sizeof(struct cmsg_ipi), SOL_IP, IP_PKTINFO},
113            {0, }};
114 int cmsg_len = 0;
115
116 /*
117 struct send_endpoint sepoint[] = {
118         { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
119         { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps =  480*Kbit },
120         { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps =  300*Kbit },
121         { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps =  300*Kbit },
122 //      { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps =  675*Kbit },
123 };
124 */
125
126 struct stream streams[MAX_STREAMS];
127
128 unsigned int nr_streams = 0;
129
130 sem_t sem_thread_finished;
131
132 bool exit_flag = false;
133
134 #ifdef WITH_FWP
135 #define negotiate_contract_for_stream(s) negotiate_contract_for_stream_fwp(s)
136 #define create_stream_endpoint(s) create_stream_endpoint_fwp(s)
137 #define send_packet(s, b) send_packet_fwp(s, b)
138 #define recv_packet(s, b) recv_packet_fwp(s, b)
139 #define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_fwp()
140 #else
141 #define negotiate_contract_for_stream(s) 0
142 #define create_stream_endpoint(s) create_stream_endpoint_native(s)
143 #define send_packet(s, b) send_packet_native(s, b)
144 #define recv_packet(s, b) recv_packet_native(s, b)
145 #define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_native()
146 #endif
147
148 void stopper()
149 {
150         int i;
151         exit_flag = true;
152
153         /* Interrupt all receivers */
154 #ifdef WITH_FWP
155         for (i=0; i < nr_streams; i++) {
156                 pthread_kill(streams[i].receiver.thread, SIGUSR1);
157         }       
158 #else
159         for (i=0; i < AC_NUM; i++) {
160                 pthread_kill(receivers[i].thread, SIGUSR1);
161         }       
162 #endif
163 }
164
165 void stream_to_text(char *stream_desc, size_t n, struct stream *stream, long long useconds)
166 {
167         char buf[3][12];
168         char real[100];
169
170         if (useconds) {
171                 snprintf(real, sizeof(real), "; real: %s sent %lld (%lld/s), received %lld (%lld/s)",
172                          bandwidth_to_text(buf[0], (long long)stream->really_sent*stream->packet_size*8*SEC_TO_USEC/useconds),
173                          stream->sent, stream->sent*SEC_TO_USEC/useconds,
174                          stream->received, stream->received*SEC_TO_USEC/useconds);
175         } else {
176                 real[0]=0;
177         }
178         
179         snprintf(stream_desc, n, "%d: %s %s (%d bytes per %s +-%s, %d packets/s)%s",
180                  stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
181                  stream->packet_size, usec_to_text(buf[1], stream->period_usec),
182                  usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
183                  (int)(SEC_TO_USEC/stream->period_usec), real);
184 }
185
186 void save_results(int argc, char *argv[], int useconds)
187 {
188         int ac, i, maxi;
189         const int mini = 3000/opt_granularity_usec;
190         bool allzeros;
191         unsigned send_count[AC_NUM];
192
193         fprintf(stderr, "Writing data to %s... ", logfname);
194         fflush(stderr);
195
196         fprintf(logfd, "# Invoked as: ");
197         for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
198         if (opt_comment) {
199                 fprintf(logfd, "(%s)", opt_comment);
200         }
201         fprintf(logfd, "\n");
202
203         if (useconds/SEC_TO_USEC != opt_count_sec) {
204                 char buf[20];
205                 usec_to_text(buf, useconds);
206                 fprintf(logfd, "# Data gathered for %s.\n", buf);
207         }
208                 
209         for (i = 0; i < nr_streams; i++) {
210                 char stream_desc[200];
211                 stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], useconds);
212                 fprintf(logfd, "# Stream %s\n", stream_desc);
213         }
214
215         /* Find maximal delay */
216         allzeros = true;
217         for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
218                 for (ac = 0; ac < AC_NUM; ac++) {
219                         if ((delay_stats[ac][maxi].csc != 0) ||
220                             (delay_stats[ac][maxi].cs != 0) ||
221                             (delay_stats[ac][maxi].sc != 0))
222                                 allzeros = false;
223                 }
224                 if (!allzeros) break;
225         }
226         maxi++;
227         if (maxi < mini) maxi = mini;
228
229         /* Calculate total number of sent packets per AC */
230         memset(send_count, 0, sizeof(send_count));
231         for (i = 0; i < nr_streams; i++) {
232                 ac = streams[i].ac;
233                 send_count[ac] += streams[i].sent;
234         }
235
236 #if 0
237         /* Write pdf */
238         for ( i = 0 ; i < maxi; i++) {
239                 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
240                 for (ac = 0; ac < AC_NUM; ac++) { 
241                         if (sum[ac])
242                                 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
243                         else val = -1; /* Don't display this ac */
244                         fprintf(logfd," %lf", val);
245                 }
246         }
247         
248         fprintf(logfd,"\n\n");
249 #endif
250         
251         fprintf(logfd,"## Format: msec csc%% cs%% sc%%\n");
252
253         /* Write PDF */
254         for (ac = 0; ac < AC_NUM; ac++) {
255                 struct delay_stat integral = {0,0,0}, last = {-1,-1,-1};
256
257                 fprintf(logfd,"%f %f %f %f\n", 0.0, 0.0, 0.0, 0.0);
258
259                 if (send_count[ac] != 0) {
260                         i=0;
261                         while ((delay_stats[ac][i].csc == 0) &&
262                                (delay_stats[ac][i].cs == 0) &&
263                                (delay_stats[ac][i].sc == 0)) i++;
264                 
265                         for (i++; i < maxi+1; i++) {
266                                 if (memcmp(&last, &integral, sizeof(last))) {
267                                         char buf[3][20];
268                                         snprintf(buf[0], sizeof(buf[0]), "%f", (double)integral.csc*100.0 / send_count[ac]);
269                                         snprintf(buf[1], sizeof(buf[1]), "%f", (double)integral.cs *100.0 / send_count[ac]);
270                                         snprintf(buf[2], sizeof(buf[2]), "%f", (double)integral.sc *100.0 / send_count[ac]);
271                                                  
272                                         fprintf(logfd,"%f %s %s %s\n", i*opt_granularity_usec/1000.0,
273                                                 integral.csc != last.csc ? buf[0] : "-",
274                                                 integral.cs  != last.cs  ? buf[1] : "-",
275                                                 integral.sc  != last.sc  ? buf[2] : "-"
276                                                 );
277                                         last = integral;
278                                 }
279                                 if (i>0) {
280                                         integral.csc += delay_stats[ac][i-1].csc;
281                                         integral.sc  += delay_stats[ac][i-1].sc;
282                                         integral.cs  += delay_stats[ac][i-1].cs;
283                                 }
284                         }
285                 }
286                 fprintf(logfd,"\n\n");
287         }
288         
289         fprintf(stderr, "finished.\n");
290         fclose(logfd);
291
292         exit(0);
293 }
294
295 int create_ac_socket(unsigned int ac) 
296 {
297         int sockfd;
298         unsigned int yes=1, tos;
299
300
301         if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
302         {
303                 perror("Unable to open socket");
304                 return -1;
305         }
306         if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
307                 perror("set non-blocking socket");
308                 return -1;
309         }
310         if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
311                 perror("Unable to set socket");
312                 return -1;
313         }
314
315         if (opt_send_buf_size >= 0) {
316                 if (setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&opt_send_buf_size,sizeof(opt_send_buf_size)) == -1) {
317                         perror("Unable to set socket buffer size");
318                         return -1;
319                 }
320         }
321
322         
323         //tos = ((AC_NUM - ac) *2 - 1)*32;
324         tos = ac_to_tos[ac];
325         if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
326                 perror("Unable to set TOS");
327                 close(sockfd);
328                 return -1;
329         }
330
331         return sockfd;
332 }
333
334 void empty_handler()
335 {
336 }
337
338 void reset_statistics()
339 {
340         int i;
341         for (i = 0; i < nr_streams; i++) {
342                 pthread_mutex_lock(&streams[i].mutex);
343                 streams[i].sent = 0;
344                 streams[i].really_sent = 0;
345                 streams[i].received = 0;
346                 pthread_mutex_unlock(&streams[i].mutex);
347         }
348         pthread_mutex_lock(&delay_stats_mutex);
349         clock_gettime(CLOCK_REALTIME, &reset_timestamp);
350         memset(delay_stats, 0, sizeof(delay_stats));
351         pthread_mutex_unlock(&delay_stats_mutex);
352 }
353
354 #ifndef WITH_FWP
355 int recv_packet_native(unsigned ac, struct msg_t *msg)
356 {
357         int     mlen, ret;
358         fd_set fdset;
359         struct  sockaddr_in rem_addr;
360         unsigned int rem_addr_length; 
361
362         FD_ZERO(&fdset);
363         FD_SET(ac_sockfd[ac], &fdset);
364         rem_addr_length = sizeof(rem_addr);
365         mlen = -1;
366         while (!exit_flag) {
367                 ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
368                 if (ret < 0) {
369                         if (errno == EINTR) continue;
370                         perror("receiver select");
371                         return -1;
372                 }
373                 mlen = recvfrom(ac_sockfd[ac], msg, sizeof(*msg), 0,
374                                 (struct sockaddr*)&rem_addr, &rem_addr_length);
375                 break;
376         }
377         return mlen;
378 }
379 #else
380 int recv_packet_fwp(struct stream *stream, struct msg_t *msg)
381 {
382         int     mlen;
383         mlen = fwp_recv(stream->resp_endpoint, msg, sizeof(*msg), 0);
384         return mlen;
385 }
386 #endif
387
388 void* receiver(void* arg)
389 {
390         struct msg_t    msg;
391         long long int trans_time_usec, client_to_server_usec, server_to_client_usec;
392         long long int min_trans_time;
393         struct timespec send_timestamp, server_timestamp, recv_timestamp;
394         int     mlen, ret;
395         unsigned ac;
396         
397         min_trans_time = ~0;
398         
399         block_signals();
400         set_rt_prio(99);
401
402         while (!exit_flag) {
403 #ifdef WITH_FWP
404                 struct stream *stream = arg;
405                 ac = stream->ac;
406                 mlen = recv_packet_fwp(stream, &msg);
407 #else
408                 ac = (unsigned)arg;
409                 mlen = recv_packet_native(ac, &msg);
410 #endif
411                 if (mlen < 0) {
412                         perror("Chyba pri prijimani pozadavku");
413                         goto out;
414                 }       
415                 clock_gettime(CLOCK_REALTIME,&recv_timestamp);
416                 send_timestamp = msg.send_timestamp;
417                 server_timestamp = msg.sendback_timestamp;
418
419                 /* Check whether this message was sent after reset_statistics() */
420
421                 if ((ret = timespec_sub_usec(&send_timestamp, &reset_timestamp)) < 0) {
422                         continue; /* If so, don't count it */
423                 }
424
425                 trans_time_usec = timespec_sub_usec(&recv_timestamp ,&send_timestamp) / 2;
426                 client_to_server_usec = timespec_sub_usec(&server_timestamp, &send_timestamp);
427                 server_to_client_usec = timespec_sub_usec(&recv_timestamp, &server_timestamp);
428
429                 pthread_mutex_lock(&delay_stats_mutex);
430                 if (trans_time_usec < MAX_DELAY_US && trans_time_usec >= 0) {
431                         delay_stats[ac][trans_time_usec/opt_granularity_usec].csc++;
432                 }
433                 if (client_to_server_usec < MAX_DELAY_US && client_to_server_usec >= 0) {
434                         delay_stats[ac][client_to_server_usec/opt_granularity_usec].cs++;
435                 }
436                 if (server_to_client_usec < MAX_DELAY_US && server_to_client_usec >= 0) {
437                         delay_stats[ac][server_to_client_usec/opt_granularity_usec].sc++;
438                 }
439                 pthread_mutex_unlock(&delay_stats_mutex);
440
441                 receivers[ac].received++;
442                 
443                 pthread_mutex_lock(&streams[msg.stream].mutex);
444                 streams[msg.stream].received++;
445                 pthread_mutex_unlock(&streams[msg.stream].mutex);
446         
447                 /*if (trans_time_nsec < min_trans_time) 
448                         min_trans_time = trans_time_nsec;*/
449                 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
450                          "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
451                          msg.seqn, msg.tos, send_timestamp.tv_sec,\
452                          send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
453                          recv_timestamp.tv_nsec, trans_time_msec); */
454         }
455 out:
456         sem_post(&sem_thread_finished);
457         return NULL;
458 }
459
460 /** 
461  * Send a packet.
462  * 
463  * @return -1 in case of error, 1 in case of sucessfull send and 0
464  *         when all buffers are full.
465  */
466 #ifndef WITH_FWP
467 static inline int 
468 send_packet_native(struct stream* stream, union msg_buff* buff)
469 {
470         struct iovec  iov;
471         struct msghdr msg;
472
473         iov.iov_base = buff;
474         iov.iov_len = stream->packet_size;
475         msg.msg_name = (void*)&stream->rem_addr;
476         msg.msg_namelen = sizeof(stream->rem_addr);
477         msg.msg_iov = &iov;
478         msg.msg_iovlen = 1;
479         msg.msg_flags = 0;
480         msg.msg_control = &cmsg;
481         msg.msg_controllen = cmsg_len;
482
483         int ret = 1;
484         
485         while (sendmsg(ac_sockfd[stream->ac], &msg, 0) < 0) {
486                 if (errno == EINTR) continue;
487                 if (errno == EAGAIN) {
488                         if (opt_wait_for_queue_is_full && 
489                             !some_queue_is_full && 
490                             /* We use mutex as atomic test and set */
491                             (pthread_mutex_trylock(&queue_full_mutex) != EBUSY)) {
492                                 some_queue_is_full = true;
493                                 reset_statistics();
494                         }
495                         ret = 0;
496                         break;
497                 } else {
498                         perror("Error while sending");
499                         ret = -1;
500                         break;
501                 }
502         }
503         return ret;
504 }
505 #else
506 static inline int 
507 send_packet_fwp(struct stream* stream, union msg_buff* buff)
508 {
509         int ret;
510
511         buff->msg.resp_port = htons(stream->resp_port);
512         ret = fwp_send(stream->endpoint, buff, stream->packet_size, 0);
513         
514         return ret;
515 }
516 #endif
517
518 static inline void
519 wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
520 {
521         struct timespec time_to_wait, current_time, period, interval;
522         unsigned period_usec = stream->period_usec;
523
524         /*           |~~~+~~~| jitter interval (width = 2*stream->jitter percentage from period)*/
525         /* |-------------|     nominal period*/
526         if (stream->jitter) {
527                 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-stream->jitter)/100
528                                                + rand() % (2*period_usec*stream->jitter/100));
529         } else {
530                 period.tv_nsec = USEC_TO_NSEC*(period_usec);
531         }
532         period.tv_sec = 0;
533         
534         timespec_add(&time_to_wait, last_send_time, &period);
535         clock_gettime(CLOCK_REALTIME,&current_time);
536         timespec_sub(&interval,&time_to_wait,&current_time);
537         nanosleep(&interval,NULL);
538 }
539
540
541 void* sender(void* arg)
542 {
543         union msg_buff buff;
544         unsigned long int seqn;
545         struct stream* stream = (struct stream*) arg;
546         char stream_desc[100];
547         int ret;
548
549         stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
550         printf("%s\n", stream_desc);
551
552         if (stream->bandwidth_bps == 0)
553                 goto out;
554
555         seqn = 0;
556         
557         block_signals();
558         set_rt_prio(90-stream->ac);
559
560         while (!exit_flag) {
561
562 /*              buff.msg.seqn = seqn++; */
563 /*              buff.msg.tos = ac_to_tos[stream->ac]; */
564                 buff.msg.stream = stream-streams;
565                 
566                 clock_gettime(CLOCK_REALTIME,&buff.msg.send_timestamp);
567
568                 ret = send_packet(stream, &buff);
569                 if (ret < 0) {
570                         goto out;
571                 }
572
573                 pthread_mutex_lock(&stream->mutex);
574                 stream->sent++;
575                 if (ret > 0)
576                         stream->really_sent++;
577                 pthread_mutex_unlock(&stream->mutex);
578
579 #ifdef DEBUG
580                 printf("%d", stream->ac);
581                 fflush(stdout);
582 #endif
583
584                 wait_for_next_send(stream, &buff.msg.send_timestamp);
585         }
586 out:
587         sem_post(&sem_thread_finished);
588         return NULL;
589 }
590
591 #ifdef WITH_FWP
592 static int negotiate_contract_for_stream_fwp(struct stream *stream)
593 {
594         fwp_contract_t contract;
595         fwp_contract_d_t contract_d;
596         fwp_vres_d_t vres2;
597         int ret;
598
599         /* Contract for client->server stream */
600
601         contract.budget = stream->packet_size;
602         contract.period_usec = stream->period_usec;
603         
604         contract_d = fwp_contract_create(&contract);
605         ret = fwp_contract_negotiate(contract_d, &stream->vres);
606         if (ret != 0) {
607                 fprintf(stderr, "Send contract was not accepted\n");
608                 exit(1);
609         }
610
611         /* Contract for server->client stream */
612         contract.budget = stream->packet_size;
613         contract.period_usec = stream->period_usec;
614
615         contract_d = fwp_contract_create(&contract);
616         ret = fwp_contract_negotiate(contract_d, &vres2);
617         if (ret != 0) {
618                 fprintf(stderr, "Receive contract was not accepted\n");
619                 exit(1);
620         }
621
622         /* We don't use the vres at server, since the server doesn't
623          * know the parameters. Instread, server uses plain
624          * sockets. */
625         
626         return ret;
627 }
628 #endif
629
630 #ifdef WITH_FWP
631 static void create_stream_endpoint_fwp(struct stream *stream)
632 {
633 /*      fwp_endpoint_attr_t  attr; */
634         int ret;
635         struct hostent* ph;
636
637         stream->ac = fwp_vres_get_ac(stream->vres);
638
639 /*      fwp_endpoint_attr_init(&attr); */
640 /*      fwp_endpoint_attr_setreliability(&attr, FWP_EPOINT_BESTEFFORT); */
641
642         ph = gethostbyname(server_addr);
643         if (ph && ph->h_addr_list[0]) {
644                 struct in_addr *a = (struct in_addr *)(ph->h_addr_list[0]);
645                 ret = fwp_send_endpoint_create(a->s_addr, BASE_PORT + stream->ac,
646                                                NULL, &stream->endpoint);
647                 /* stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac); */
648                 if (ret < 0) {
649                         perror("fwp_send_endpoint_create");
650                         exit(1);
651                 }
652                 ret = fwp_send_endpoint_bind(stream->endpoint, stream->vres);
653                 if (ret != 0) {
654                         perror("fwp_send_endpoint_bind");
655                         exit(1);
656                 }
657                 ret = fwp_receive_endpoint_create(0, NULL, &stream->resp_endpoint);
658                 if (ret != 0) {
659                         perror("fwp_receive_endpoint_create");
660                         exit(1);
661                 }
662                 unsigned int port;
663                 fwp_endpoint_get_params(stream->resp_endpoint, NULL, &port, NULL);
664                 stream->resp_port = port;
665                 printf("resp_port = %d\n", port);
666
667                 ret = pthread_create(&stream->receiver.thread, NULL, receiver, (void*)stream);
668                 if (ret) {
669                         perror("Error while creating receiver\n");
670                         exit(1);
671                 }
672         }
673         else {
674                 char str[100];
675                 snprintf(str, sizeof(str), "gethostbyname(%s)", server_addr);
676                 perror(str);
677                 exit(1);
678         }
679
680 }
681 #else
682 static void create_stream_endpoint_native(struct stream *stream)
683 {
684         struct hostent* ph;
685
686         memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
687
688         stream->rem_addr.sin_family = AF_INET;
689         ph = gethostbyname(server_addr);
690         if (ph)
691                 stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
692         else {
693                 char str[100];
694                 snprintf(str, sizeof(str), "gethostbyname(%s)", server_addr);
695                 perror(str);
696                 exit(1);
697         }
698         stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
699 }
700 #endif
701
702 static inline void
703 calc_stream_params(struct stream *stream)
704 {
705         int packet_size;
706         unsigned period_usec;
707         int bandwidth;
708         int ret;
709
710         /* If some parameters are not set explicitely, use default values. */
711         if (stream->bandwidth_bps < 0) stream->bandwidth_bps = opt_def_bandwidth * Kbit;
712         if (stream->packet_size < 0) stream->packet_size = opt_packet_size;
713         if (stream->period_usec < 0) stream->period_usec = opt_def_period_msec * MSEC_TO_USEC;
714
715         bandwidth = stream->bandwidth_bps;
716
717         /* Avoid arithmetic exception. Server thread will exit if
718            stream->bandwidth_bps == 0. */
719         if (bandwidth == 0) bandwidth = 1;
720
721         if (stream->packet_size) {
722                 packet_size = stream->packet_size;
723                 period_usec = SEC_TO_USEC*packet_size*8/bandwidth;
724                 if (period_usec == 0) period_usec = 1;
725         } else if (stream->period_usec) {
726                 period_usec = stream->period_usec;
727                 packet_size = (long long)bandwidth/8 * period_usec/SEC_TO_USEC;
728         } else {
729                 char buf[200];
730                 stream_to_text(buf, sizeof(buf), stream, 0);
731                 fprintf(stderr, "Neither packet size nor period was specified for a stream %s\n", buf);
732                 exit(1);
733         }
734
735         if (packet_size < sizeof(struct msg_t)) {
736                 fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
737                 exit(1);
738         }
739
740         stream->packet_size = packet_size;
741         stream->period_usec = period_usec;
742         stream->jitter = opt_jitter;
743
744         ret = negotiate_contract_for_stream(stream);
745         if (ret == 0) {
746                 create_stream_endpoint(stream);
747         } else {
748                 char buf[200];
749                 stream_to_text(buf, sizeof(buf), stream, 0);
750                 fprintf(stderr, "Contract hasn't been accepted: %s\n", buf);
751                 stream->bandwidth_bps = 0;
752         }
753 }
754
755 /** 
756  * Parse -b parameter.
757  * 
758  * @param params String to parse
759  * 
760  * @return NULL in case of success, pointer to a problematic character
761  *         on error.
762  */
763 char* parse_bandwidths(char *params)
764 {
765         struct stream *sp = &streams[nr_streams];
766
767         while (*params && nr_streams < MAX_STREAMS) {
768                 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
769                 int i;
770                 char *next_char;
771
772                 if (strlen(params) < 2)
773                         return params;
774                 for (i=0; i<AC_NUM; i++) {
775                         if (strncmp(params, ac_ids[i], 2) == 0) {
776                                 sp->ac = i;
777                                 params+=strlen(ac_ids[i]);
778                                 break;
779                         }
780                 }
781                 if (i==AC_NUM)
782                         return params;
783
784                 long bw;
785                 if (*params == ':') {
786                         params++;
787
788                         bw = strtol(params, &next_char, 10);
789                         if (next_char == params)
790                                 return params;
791                         params = next_char;
792                 } else
793                         bw = -1;
794                 
795                 sp->bandwidth_bps = bw*Kbit;
796
797                 long period = 0;
798                 long packet_size = 0;
799                 if (*params == '@') {
800                         params++;
801                         period = strtol(params, &next_char, 10);
802                         if (period == 0)
803                                 return params;
804                         params = next_char;
805                 }
806                 else {
807                         if (*params == '/') {
808                                 params++;
809                                 packet_size = strtol(params, &next_char, 10);
810                                 if (packet_size == 0)
811                                         return params;
812                                 params = next_char;
813                         } else {
814                                 packet_size = -1; 
815                                 period = -1;
816                         }
817                 }
818                 sp->period_usec = period*MSEC_TO_USEC;
819                 sp->packet_size = packet_size;
820
821                 
822
823                 if (*params != '\0' && *params != ',')
824                         return params;
825                 nr_streams++;
826                 sp++;
827                 if (*params == ',')
828                         params++;
829         }
830         return NULL;
831 }
832
833 #ifdef WITH_FWP
834 void wait_for_all_threads_to_finish_fwp(void)
835 {
836         int i;
837         /* Wait for all threads to finish */
838         /* FIXME: */
839 /*      for (i=0; i < 2*nr_streams; i++) { */
840 /*              sem_wait(&sem_thread_finished); */
841 /*      } */
842 }
843 #else
844 void wait_for_all_threads_to_finish_native(void)
845 {
846         int i;
847         /* Wait for all threads to finish */
848         for (i=0; i < nr_streams + AC_NUM; i++) {
849                 sem_wait(&sem_thread_finished);
850         }
851 }
852 #endif
853
854 int main(int argc, char *argv[])
855 {
856         int ac, i, rc, seconds;
857         pthread_attr_t attr;
858         pthread_t thread;
859         char opt;
860
861
862         while ((opt = getopt(argc, argv, "B:b:C:c:g:I:j:o:qQ:s:T:")) != -1) {
863                 switch (opt) {
864                         case 'B':
865                                 opt_def_bandwidth = atoi(optarg);
866                                 break;
867                         case 'b': {
868                                 char *error;
869                                 error = parse_bandwidths(optarg);
870                                 if (error != NULL) {
871                                         if (*error == '\0')
872                                                 fprintf(stderr, "Bandwidth parse error - string to short\n");
873                                         else
874                                                 fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
875                                         exit(1);
876                                 }
877                                 break;
878                         }
879                         case 'C':
880                                 opt_comment = optarg;
881                                 break;
882                         case 'c':
883                                 opt_count_sec = atoi(optarg);
884                                 break;
885                         case 'g':
886                                 opt_granularity_usec = atoi(optarg);
887                                 if (opt_granularity_usec < MIN_GRANULARITY) {
888                                         fprintf(stderr, "Granulatiry too small (min %d)!\n", MIN_GRANULARITY);
889                                         exit(1);
890                                 }
891                                 break;
892                         case 'I':
893                                 opt_interface = optarg;
894                                 break;
895                         case 'j':
896                                 #ifdef WITH_FWP
897                                 fprintf(stderr, "-j is not allowd when compiled with FWP\n");
898                                 exit(1);
899                                 #else
900                                 opt_jitter = atoi(optarg);
901                                 #endif
902                                 break;
903                         case 'o':
904                                 opt_output = optarg;
905                                 break;
906                         case 'Q':
907                                 opt_send_buf_size = atoi(optarg);
908                                 break;
909                         case 'q':
910                                 opt_wait_for_queue_is_full = true;
911                                 break;
912                         case 's':
913                                 opt_packet_size = atoi(optarg);
914                                 break;
915                         case 'T':
916                                 opt_def_period_msec = atoi(optarg);
917                                 break;
918                         default:
919                                 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
920                                 fprintf(stderr, "Options:\n");
921                                 fprintf(stderr, "    -B  default bandwidth for -b option [kbit]\n");
922                                 fprintf(stderr, "    -b  bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec> or /<bytes>][,...]\n");
923                                 fprintf(stderr, "    -C  comment (added to header)\n");
924                                 fprintf(stderr, "    -c  count (number of seconds to run)\n");
925                                 fprintf(stderr, "    -g  histogram granularity [usec]\n");
926                                 fprintf(stderr, "    -I  <interface> send packets from this interface");
927                                 fprintf(stderr, "    -j  send jitter (0-100) [%%]\n");
928                                 fprintf(stderr, "    -o  output filename (.dat will be appended)\n");
929                                 fprintf(stderr, "    -q  gather statistics only after some queue becomes full\n");
930                                 fprintf(stderr, "    -Q  <bytes> set size for socket send buffers\n");
931                                 fprintf(stderr, "    -s  size of data payload in packets [bytes] (default: %d)\n", opt_packet_size);
932                                 fprintf(stderr, "    -T  default period for -b option [msec]\n");
933                                 exit(1);
934                 }
935         }
936         if (opt_packet_size && opt_def_period_msec) {
937                 fprintf(stderr, "Error: Nonzero -T and -s can't be used together!.\n");
938                 exit(1);
939         }
940
941         if (optind < argc) {
942                 server_addr = argv[optind];
943         } else {
944                 fprintf(stderr, "Expected server address argument\n");
945                 exit(1);
946         }
947
948         if (nr_streams == 0)
949                 parse_bandwidths("BE");
950                 
951         pthread_attr_init(&attr);
952
953         snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
954
955         if ((logfd = fopen(logfname,"w+")) == NULL) {
956                 fprintf(stderr,"Can not open %s\n", logfname);
957                 exit(1);
958         }
959         if (signal(SIGTERM, stopper) == SIG_ERR) {
960                 perror("Error in signal registration");
961                 exit(1);
962         }
963                 
964         if (signal(SIGINT, stopper) == SIG_ERR) {
965                 perror("Signal handler registration error");
966                 exit(1);
967         }
968
969         struct sigaction sa;
970         sa.sa_handler = empty_handler;
971         sa.sa_flags = 0;        /* don't restart syscalls */
972
973         if (sigaction(SIGUSR1, &sa, NULL) < 0) {
974                 perror("sigaction error");
975                 exit(1);
976         }
977
978         sem_init(&sem_thread_finished, 0, 0);
979
980         reset_statistics();
981
982 #ifdef WITH_FWP
983         rc = fwp_init();
984         if (rc != 0) {
985                 fprintf(stderr, "FWP initialization failed\n");
986                 exit(1);
987         }
988 #else
989         /* create four receivers each per AC */
990         for (ac = AC_NUM - 1; ac >= 0; ac--) {
991                 ac_sockfd[ac] = create_ac_socket(ac);
992                 if (ac_sockfd[ac] < 0) {
993                         return 1;
994                 }
995                 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
996                 if (rc) {
997                         fprintf(stderr, "Error while creating receiver %d\n",rc);
998                         return 1;
999                 }
1000         }               
1001 #endif
1002
1003                         
1004         if (opt_interface) {
1005                 struct ifreq ifr;
1006
1007                 memset(&ifr, 0, sizeof(ifr));
1008                 strncpy(ifr.ifr_name, opt_interface, IFNAMSIZ-1);
1009                 if (ioctl(ac_sockfd[AC_VO], SIOCGIFINDEX, &ifr) < 0) {
1010                         fprintf(stderr, "unknown iface %s\n", opt_interface);
1011                         exit(1);
1012                 }
1013                 cmsg.ipi.ipi_ifindex = ifr.ifr_ifindex;
1014                 cmsg_len = sizeof(cmsg);
1015         }
1016         /* create sendpoints */
1017         for (i = 0; i < nr_streams; i++) {
1018                 struct stream *s = &streams[i];
1019                 pthread_mutex_init(&s->mutex, NULL);
1020                 calc_stream_params(s);
1021                 rc = pthread_create(&thread, &attr, sender, (void*) s);
1022                 if (rc) {
1023                         fprintf(stderr, "Error while creating sender %d\n",rc);
1024                         return 1;
1025                 }
1026         }
1027         
1028         seconds = 0;
1029         while (!exit_flag) {
1030                 sleep(1);
1031                 seconds++;
1032                 fprintf(stderr, "\r%3ds", seconds);
1033                 for (ac = 0; ac < AC_NUM; ac++) {
1034                         int delta = receivers[ac].received - receivers[ac].last_received;
1035                         receivers[ac].last_received = receivers[ac].received;
1036                         fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
1037                 }
1038                 fflush(stderr);
1039                 if (seconds == opt_count_sec)
1040                         stopper();
1041         }
1042
1043         fprintf(stderr, "\nWaiting for threads to finish\n");
1044         wait_for_all_threads_to_finish();
1045
1046         struct timespec end_timestamp, measure_length;
1047         clock_gettime(CLOCK_REALTIME,&end_timestamp);
1048         timespec_sub(&measure_length, &end_timestamp, &reset_timestamp);
1049
1050         save_results(argc, argv, timespec2usec(&measure_length));
1051
1052         return 0;
1053 }