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