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