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