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