aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_communicator_basic.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/test_communicator_basic.c')
-rw-r--r--src/transport/test_communicator_basic.c1203
1 files changed, 0 insertions, 1203 deletions
diff --git a/src/transport/test_communicator_basic.c b/src/transport/test_communicator_basic.c
deleted file mode 100644
index f43cdaaf3..000000000
--- a/src/transport/test_communicator_basic.c
+++ /dev/null
@@ -1,1203 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2019 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19*/
20
21/**
22* @file transport/test_communicator_basic.c
23* @brief test the communicators
24* @author Julius Bünger
25* @author Martin Schanzenbach
26*/
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "transport-testing-communicator.h"
30#include "gnunet_ats_transport_service.h"
31#include "gnunet_signatures.h"
32#include "gnunet_testing_lib.h"
33#include "transport.h"
34#include "gnunet_statistics_service.h"
35
36#include <inttypes.h>
37
38
39#define LOG(kind, ...) GNUNET_log_from (kind, \
40 "test_transport_communicator", \
41 __VA_ARGS__)
42
43#define NUM_PEERS 2
44
45static struct GNUNET_SCHEDULER_Task *to_task[NUM_PEERS];
46
47static int queue_est = GNUNET_NO;
48
49static struct GNUNET_PeerIdentity peer_id[NUM_PEERS];
50
51static char *communicator_binary;
52
53static struct
54GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS];
55
56static struct GNUNET_CONFIGURATION_Handle *cfg_peers[NUM_PEERS];
57
58static struct GNUNET_STATISTICS_Handle *stats[NUM_PEERS];
59
60static char *cfg_peers_name[NUM_PEERS];
61
62static int finished[NUM_PEERS];
63
64static int ret;
65
66static int bidirect = GNUNET_NO;
67
68static size_t long_message_size;
69
70static struct GNUNET_TIME_Absolute start_short[NUM_PEERS];
71
72static struct GNUNET_TIME_Absolute start_long[NUM_PEERS];
73
74static struct GNUNET_TIME_Absolute timeout[NUM_PEERS];
75
76// static struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *my_tc;
77
78static char *communicator_name;
79
80static char *test_name;
81
82static struct GNUNET_STATISTICS_GetHandle *box_stats[NUM_PEERS];
83
84static struct GNUNET_STATISTICS_GetHandle *rekey_stats[NUM_PEERS];
85
86#define TEST_SECTION "test-setup"
87
88#define SHORT_MESSAGE_SIZE 128
89
90#define LONG_MESSAGE_SIZE 32000 /* FIXME */
91
92#define ALLOWED_PACKET_LOSS 91
93
94#define BURST_PACKETS 5000
95
96#define TOTAL_ITERATIONS 1
97
98#define PEER_A 0
99
100#define PEER_B 1
101
102static unsigned int iterations_left[NUM_PEERS];
103
104#define TIMEOUT_MULTIPLIER 1
105
106#define DELAY \
107 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS,200)
108
109#define SHORT_BURST_WINDOW \
110 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,2)
111
112#define LONG_BURST_WINDOW \
113 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,2)
114
115enum TestPhase
116{
117 TP_INIT,
118 TP_BURST_SHORT,
119 TP_BURST_LONG,
120 TP_SIZE_CHECK
121};
122
123static unsigned int phase_short[NUM_PEERS];
124
125static unsigned int phase_long[NUM_PEERS];
126
127static unsigned int phase_size[NUM_PEERS];
128
129static long long unsigned int allowed_packet_loss_short;
130
131static long long unsigned int allowed_packet_loss_long;
132
133static long long unsigned int burst_packets_short;
134
135static long long unsigned int burst_packets_long;
136
137static long long unsigned int delay_long_value;
138
139static long long unsigned int delay_short_value;
140
141static struct GNUNET_TIME_Relative delay_short;
142
143static struct GNUNET_TIME_Relative delay_long;
144
145static size_t num_sent_short[NUM_PEERS];
146
147static size_t num_sent_long[NUM_PEERS];
148
149static size_t num_sent_size[NUM_PEERS];
150
151static uint32_t ack[NUM_PEERS];
152
153static enum TestPhase phase[NUM_PEERS];
154
155static size_t num_received_short[NUM_PEERS];
156
157static size_t num_received_long[NUM_PEERS];
158
159static size_t num_received_size[NUM_PEERS];
160
161static uint64_t avg_latency[NUM_PEERS];
162
163static void
164communicator_available_cb (
165 void *cls,
166 struct
167 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
168 enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc,
169 char *address_prefix)
170{
171 LOG (GNUNET_ERROR_TYPE_INFO,
172 "Communicator available. (cc: %u, prefix: %s)\n",
173 cc,
174 address_prefix);
175}
176
177
178static void
179open_queue (void *cls)
180{
181 const char *address = cls;
182
183 if (NULL != tc_hs[PEER_A]->c_mq)
184 {
185 queue_est = GNUNET_YES;
186 GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (tc_hs[PEER_A],
187 &peer_id[PEER_B],
188 address);
189 }
190 else
191 {
192 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
193 &open_queue,
194 (void *) address);
195 }
196}
197
198
199static void
200add_address_cb (
201 void *cls,
202 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
203 const char *address,
204 struct GNUNET_TIME_Relative expiration,
205 uint32_t aid,
206 enum GNUNET_NetworkType nt)
207{
208 LOG (GNUNET_ERROR_TYPE_DEBUG,
209 "New address. (addr: %s, expir: %s, ID: %" PRIu32 ", nt: %u\n",
210 address,
211 GNUNET_STRINGS_relative_time_to_string (expiration,
212 GNUNET_NO),
213 aid,
214 (int) nt);
215 // addresses[1] = GNUNET_strdup (address);
216 if ((0 == strcmp ((char*) cls, cfg_peers_name[PEER_B])) &&
217 (GNUNET_NO == queue_est))
218 {
219 open_queue ((void *) address);
220 }
221}
222
223
224/**
225 * @brief Callback that informs whether the requested queue will be
226 * established
227 *
228 * Implements #GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback.
229 *
230 * @param cls Closure - unused
231 * @param tc_h Communicator handle - unused
232 * @param will_try #GNUNET_YES if queue will be established
233 * #GNUNET_NO if queue will not be established (bogous address)
234 */
235static void
236queue_create_reply_cb (
237 void *cls,
238 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
239 int will_try)
240{
241 (void) cls;
242 (void) tc_h;
243 if (GNUNET_YES == will_try)
244 LOG (GNUNET_ERROR_TYPE_DEBUG,
245 "Queue will be established!\n");
246 else
247 LOG (GNUNET_ERROR_TYPE_WARNING,
248 "Queue won't be established (bougus address?)!\n");
249}
250
251
252static struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
253handle_backchannel_cb (void *cls,
254 struct GNUNET_MessageHeader *msg,
255 struct GNUNET_PeerIdentity *pid)
256{
257 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
258
259 (void) tc_h;
260 (void) msg;
261 LOG (GNUNET_ERROR_TYPE_DEBUG, "Handling BC message...\n");
262 if (0 == memcmp (&peer_id[PEER_A], pid, sizeof (*pid)))
263 return tc_hs[PEER_A];
264 else
265 return tc_hs[PEER_B];
266}
267
268
269static char*
270make_payload (size_t payload_size)
271{
272 struct GNUNET_TIME_Absolute ts;
273 struct GNUNET_TIME_AbsoluteNBO ts_n;
274 char *payload = GNUNET_malloc (payload_size);
275
276 LOG (GNUNET_ERROR_TYPE_DEBUG,
277 "Making payload of size %lu\n", payload_size);
278 GNUNET_assert (payload_size >= 8); // So that out timestamp fits
279 ts = GNUNET_TIME_absolute_get ();
280 ts_n = GNUNET_TIME_absolute_hton (ts);
281 memset (payload, 'a', payload_size);
282 memcpy (payload, &ts_n, sizeof (struct GNUNET_TIME_AbsoluteNBO));
283 return payload;
284}
285
286static struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
287get_tc_h (unsigned int peer_nr)
288{
289 LOG (GNUNET_ERROR_TYPE_DEBUG,
290 "Got peer %u\n",
291 peer_nr);
292
293 LOG (GNUNET_ERROR_TYPE_DEBUG,
294 "Handle %p peer 0\n",
295 tc_hs[0]);
296
297 LOG (GNUNET_ERROR_TYPE_DEBUG,
298 "Handle %p peer 1\n",
299 tc_hs[1]);
300
301 LOG (GNUNET_ERROR_TYPE_DEBUG,
302 "Handle %p get\n",
303 tc_hs[peer_nr]);
304
305 return tc_hs[peer_nr];
306}
307
308
309static unsigned int
310get_peer_nr_from_tc (struct
311 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
312{
313 if (tc_h == get_tc_h (0))
314 return PEER_A;
315 else
316 return PEER_B;
317}
318
319static unsigned int
320get_peer_nr (void *cls, unsigned int get_the_other_one)
321{
322 if (0 == strcmp ((char*) cls, cfg_peers_name[0]))
323 return get_the_other_one ? PEER_B : PEER_A;
324 else
325 return get_the_other_one ? PEER_A : PEER_B;
326}
327
328static void
329process_statistics_box_done (void *cls, int success)
330{
331 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
332 unsigned int peer_nr;
333
334 peer_nr = get_peer_nr_from_tc (tc_h);
335
336 if (NULL != box_stats[peer_nr])
337 box_stats[peer_nr] = NULL;
338 if (NULL == rekey_stats[peer_nr])
339 {
340 LOG (GNUNET_ERROR_TYPE_DEBUG,
341 "Finished\n");
342 GNUNET_SCHEDULER_shutdown ();
343 }
344}
345
346
347static void
348process_statistics_rekey_done (void *cls, int success)
349{
350 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
351 unsigned int peer_nr;
352
353 peer_nr = get_peer_nr_from_tc (tc_h);
354
355 if (NULL != rekey_stats[peer_nr])
356 rekey_stats[peer_nr] = NULL;
357 if (NULL == box_stats[peer_nr])
358 {
359 LOG (GNUNET_ERROR_TYPE_DEBUG,
360 "Finished\n");
361 GNUNET_SCHEDULER_shutdown ();
362 }
363}
364
365static int
366process_statistics (void *cls,
367 const char *subsystem,
368 const char *name,
369 uint64_t value,
370 int is_persistent)
371{
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "Statistic: Name %s and value %lu\n",
374 name,
375 value);
376 if ((0 == strcmp ("rekey", test_name)) && (0 == strcmp (
377 "# rekeying successful",
378 name)) && (0 == value))
379 {
380 ret = 2;
381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
382 "No successful rekeying!\n");
383 GNUNET_SCHEDULER_shutdown ();
384 }
385 if ((0 == strcmp ("backchannel", test_name)) &&
386 (0 == strcmp (
387 "# messages decrypted with BOX",
388 name))
389 && (9000 > value))
390 {
391 ret = 2;
392 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
393 "Not enough BOX messages!\n");
394 GNUNET_SCHEDULER_shutdown ();
395 }
396 if ((0 == strcmp ("rekey", test_name)) &&
397 (0 == strcmp (
398 "# messages decrypted with BOX",
399 name))
400 && (6000 > value))
401 {
402 ret = 2;
403 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
404 "Not enough BOX messages!\n");
405 GNUNET_SCHEDULER_shutdown ();
406 }
407 return GNUNET_OK;
408}
409
410static void
411short_test (void *cls);
412
413static void
414short_test_cb (void *cls)
415{
416 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
417 unsigned int peer_nr;
418 char *payload;
419
420 peer_nr = get_peer_nr_from_tc (tc_h);
421
422 LOG (GNUNET_ERROR_TYPE_DEBUG,
423 "short_test_cb %u/%u for peer %u and handle %p\n",
424 (unsigned int) num_sent_short[peer_nr],
425 (unsigned int) num_received_short[peer_nr],
426 peer_nr,
427 tc_h);
428 payload = make_payload (SHORT_MESSAGE_SIZE);
429 num_sent_short[peer_nr]++;
430 GNUNET_TRANSPORT_TESTING_transport_communicator_send (tc_h,
431 (burst_packets_short ==
432 num_sent_short[peer_nr])
433 ? NULL
434 : &short_test,
435 cls,
436 payload,
437 SHORT_MESSAGE_SIZE);
438 GNUNET_free (payload);
439 timeout[peer_nr] = GNUNET_TIME_relative_to_absolute (
440 GNUNET_TIME_relative_multiply (
441 GNUNET_TIME_UNIT_SECONDS,
442 TIMEOUT_MULTIPLIER));
443}
444
445static void
446short_test (void *cls)
447{
448 GNUNET_SCHEDULER_add_delayed (delay_short,
449 &short_test_cb,
450 cls);
451}
452
453static void
454size_test (void *cls)
455{
456 unsigned int peer_nr;
457 char *payload;
458 size_t max_size = 64000;
459 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
460
461 peer_nr = get_peer_nr_from_tc (tc_h);
462 LOG (GNUNET_ERROR_TYPE_DEBUG,
463 "size_test_cb %u\n",
464 (unsigned int) num_sent_size[peer_nr]);
465 GNUNET_assert (TP_SIZE_CHECK == phase[peer_nr]);
466 if (LONG_MESSAGE_SIZE != long_message_size)
467 max_size = long_message_size;
468 if (ack[peer_nr] + 10 > max_size)
469 return; /* Leave some room for our protocol, so not 2^16 exactly */
470 ack[peer_nr] += 10;
471 payload = make_payload (ack[peer_nr]);
472 num_sent_size[peer_nr]++;
473 GNUNET_TRANSPORT_TESTING_transport_communicator_send (tc_h,
474 (ack[peer_nr] <
475 max_size)
476 ? &size_test
477 : NULL,
478 cls,
479 payload,
480 ack[peer_nr]);
481 GNUNET_free (payload);
482 timeout[peer_nr] = GNUNET_TIME_relative_to_absolute (
483 GNUNET_TIME_relative_multiply (
484 GNUNET_TIME_UNIT_SECONDS,
485 TIMEOUT_MULTIPLIER));
486}
487
488static void
489long_test (void *cls);
490
491static void
492long_test_cb (void *cls)
493{
494 unsigned int peer_nr;
495 char *payload;
496 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
497
498 peer_nr = get_peer_nr_from_tc (tc_h);
499
500 LOG (GNUNET_ERROR_TYPE_DEBUG,
501 "long_test_cb %u/%u\n",
502 (unsigned int) num_sent_long[peer_nr],
503 (unsigned int) num_received_long[peer_nr]);
504 payload = make_payload (long_message_size);
505 num_sent_long[peer_nr]++;
506 GNUNET_TRANSPORT_TESTING_transport_communicator_send (tc_h,
507 (burst_packets_long ==
508 num_sent_long[peer_nr])
509 ? NULL
510 : &long_test,
511 cls,
512 payload,
513 long_message_size);
514 GNUNET_free (payload);
515 timeout[peer_nr] = GNUNET_TIME_relative_to_absolute (
516 GNUNET_TIME_relative_multiply (
517 GNUNET_TIME_UNIT_SECONDS,
518 TIMEOUT_MULTIPLIER));
519}
520
521
522static void
523long_test (void *cls)
524{
525 GNUNET_SCHEDULER_add_delayed (delay_long,
526 &long_test_cb,
527 cls);
528}
529
530static void
531choose_phase (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
532{
533 unsigned int peer_nr;
534
535 peer_nr = get_peer_nr_from_tc (tc_h);
536
537 if (GNUNET_YES == phase_short[peer_nr])
538 {
539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
540 "Choose phase short with peer %u and Handle %p\n",
541 peer_nr,
542 tc_h);
543 phase[peer_nr] = TP_BURST_SHORT;
544 start_short[peer_nr] = GNUNET_TIME_absolute_get ();
545 short_test (tc_h);
546 }
547 else if (GNUNET_YES == phase_long[peer_nr])
548 {
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
550 "Choose phase long with peer %u\n",
551 peer_nr);
552 phase[peer_nr] = TP_BURST_LONG;
553 start_long[peer_nr] = GNUNET_TIME_absolute_get ();
554 long_test (tc_h);
555 }
556 else if (GNUNET_YES == phase_size[peer_nr])
557 {
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559 "Choose phase size\n");
560 phase[peer_nr] = TP_SIZE_CHECK;
561 size_test (tc_h);
562 }
563 else
564 {
565 if ((0 == strcmp ("udp", communicator_name)) && ((0 == strcmp ("rekey",
566 test_name))
567 ||(0 == strcmp (
568 "backchannel",
569 test_name))) )
570 {
571 if (NULL != box_stats[peer_nr])
572 GNUNET_STATISTICS_get_cancel (box_stats[peer_nr]);
573 box_stats[peer_nr] = GNUNET_STATISTICS_get (stats[1],
574 "C-UDP",
575 "# messages decrypted with BOX",
576 process_statistics_box_done,
577 &process_statistics,
578 tc_h);
579 if (NULL != rekey_stats[peer_nr])
580 GNUNET_STATISTICS_get_cancel (rekey_stats[peer_nr]);
581 rekey_stats[peer_nr] = GNUNET_STATISTICS_get (stats[0],
582 "C-UDP",
583 "# rekeying successful",
584 process_statistics_rekey_done,
585 &process_statistics,
586 tc_h);
587 }
588 else
589 {
590 if ((GNUNET_NO == bidirect)|| (((PEER_A == peer_nr) &&
591 finished[PEER_B]) || ((PEER_B ==
592 peer_nr) &&
593 finished
594 [PEER_A])))
595 {
596 LOG (GNUNET_ERROR_TYPE_DEBUG,
597 "Finished\n");
598 GNUNET_SCHEDULER_shutdown ();
599 }
600 finished[peer_nr] = GNUNET_YES;
601 }
602 }
603}
604
605static void finish_phase_long (unsigned int peer_nr)
606{
607 static struct GNUNET_TIME_Relative duration;
608
609 duration = GNUNET_TIME_absolute_get_duration (start_long[peer_nr]);
610 LOG (GNUNET_ERROR_TYPE_MESSAGE,
611 "Long size packet test for peer %u done.\n",
612 peer_nr);
613 char *goodput = GNUNET_STRINGS_byte_size_fancy (
614 (long_message_size * num_received_long[peer_nr] * 1000 * 1000)
615 / duration.
616 rel_value_us);
617
618 LOG (GNUNET_ERROR_TYPE_MESSAGE,
619 "%lu/%lu packets in %llu us (%s/s) -- avg latency: %llu us\n",
620 (unsigned long) num_received_long[peer_nr],
621 (unsigned long) num_sent_long[peer_nr],
622 (unsigned long long) duration.rel_value_us,
623 goodput,
624 (unsigned long long) avg_latency[peer_nr]);
625 GNUNET_free (goodput);
626 ack[peer_nr] = 0;
627 // phase = TP_SIZE_CHECK;
628 // num_received = 0;
629 // num_sent_long = 0;
630 avg_latency[peer_nr] = 0;
631 // size_test (NULL);
632 phase_long[peer_nr] = GNUNET_NO;
633 choose_phase (get_tc_h (peer_nr));
634}
635
636static void
637finish_phase_short (unsigned int peer_nr)
638{
639 static struct GNUNET_TIME_Relative duration;
640
641 duration = GNUNET_TIME_absolute_get_duration (start_short[peer_nr]);
642 LOG (GNUNET_ERROR_TYPE_MESSAGE,
643 "Short size packet test for peer %u done.\n",
644 peer_nr);
645 char *goodput = GNUNET_STRINGS_byte_size_fancy (
646 (SHORT_MESSAGE_SIZE * num_received_short[peer_nr] * 1000 * 1000)
647 / duration.rel_value_us);
648 LOG (GNUNET_ERROR_TYPE_MESSAGE,
649 "%lu/%lu packets in %llu us (%s/s) -- avg latency: %llu us\n",
650 (unsigned long) num_received_short[peer_nr],
651 (unsigned long) num_sent_short[peer_nr],
652 (unsigned long long) duration.rel_value_us,
653 goodput,
654 (unsigned long long) avg_latency[peer_nr]);
655 GNUNET_free (goodput);
656 // start_long = GNUNET_TIME_absolute_get ();
657 // phase = TP_BURST_LONG;
658 // num_sent_short = 0;
659 avg_latency[peer_nr] = 0;
660 // num_received = 0;
661 phase_short[peer_nr] = GNUNET_NO;
662 choose_phase (get_tc_h (peer_nr));
663 // long_test (NULL);
664}
665
666static void
667latency_timeout (void *cls)
668{
669
670 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
671 unsigned int peer_nr;
672 size_t num_sent = 0;
673 size_t num_received = 0;
674
675 peer_nr = get_peer_nr_from_tc (tc_h);
676 to_task[peer_nr] = NULL;
677
678 switch (phase[peer_nr])
679 {
680 case TP_INIT:
681 GNUNET_assert (0);
682 break;
683 case TP_BURST_SHORT:
684 num_sent = num_sent_short[peer_nr];
685 num_received = num_received_short[peer_nr];
686 if ((num_sent_short[peer_nr] == burst_packets_short) &&
687 (num_received_short[peer_nr] >
688 burst_packets_short
689 / 100
690 *
691 allowed_packet_loss_short) )
692 {
693 finish_phase_short (peer_nr);
694 to_task[peer_nr] = GNUNET_SCHEDULER_add_at (timeout[peer_nr],
695 &latency_timeout,
696 cls);
697 return;
698 }
699 break;
700 case TP_BURST_LONG:
701 num_sent = num_sent_long[peer_nr];
702 num_received = num_received_long[peer_nr];
703 if ((num_sent_long[peer_nr] == burst_packets_long) &&
704 (num_received_long[peer_nr] >
705 burst_packets_long
706 / 100
707 *
708 allowed_packet_loss_long) )
709 {
710 finish_phase_long (peer_nr);
711 to_task[peer_nr] = GNUNET_SCHEDULER_add_at (timeout[peer_nr],
712 &latency_timeout,
713 cls);
714 return;
715 }
716 break;
717 case TP_SIZE_CHECK:
718 num_sent = num_sent_size[peer_nr];
719 num_received = num_received_size[peer_nr];
720 break;
721 }
722 if (GNUNET_TIME_absolute_get_remaining (timeout[peer_nr]).rel_value_us > 0)
723 {
724 to_task[peer_nr] = GNUNET_SCHEDULER_add_at (timeout[peer_nr],
725 &latency_timeout,
726 cls);
727 return;
728 }
729 LOG (GNUNET_ERROR_TYPE_ERROR,
730 "Latency too high. Test failed. (Phase: %d. Sent: %lu, Received: %lu)\n",
731 phase[peer_nr], num_sent, num_received);
732 ret = 2;
733 GNUNET_SCHEDULER_shutdown ();
734}
735
736/**
737 * @brief Handle opening of queue
738 *
739 * Issues sending of test data
740 *
741 * Implements #GNUNET_TRANSPORT_TESTING_AddQueueCallback
742 *
743 * @param cls Closure
744 * @param tc_h Communicator handle
745 * @param tc_queue Handle to newly opened queue
746 */
747static void
748add_queue_cb (void *cls,
749 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
750 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *
751 tc_queue,
752 size_t mtu)
753{
754
755 unsigned int peer_nr;
756
757 peer_nr = get_peer_nr (cls, GNUNET_NO);
758 LOG (GNUNET_ERROR_TYPE_DEBUG,
759 "Handle %p add %u %u\n",
760 tc_h,
761 peer_nr,
762 get_peer_nr_from_tc (tc_h));
763 if ((GNUNET_NO == bidirect)&&(0 != strcmp ((char*) cls, cfg_peers_name[0])))
764 {
765 LOG (GNUNET_ERROR_TYPE_DEBUG,
766 "Queue available at receiving peer\n");
767 return; // TODO?
768 }
769 else if (TP_INIT != phase[peer_nr])
770 return;
771 LOG (GNUNET_ERROR_TYPE_DEBUG,
772 "Queue established, starting test...\n");
773 if (UINT32_MAX != mtu) /* Message header overhead */
774 long_message_size = mtu - sizeof(struct GNUNET_TRANSPORT_SendMessageTo)
775 - sizeof(struct GNUNET_MessageHeader);
776 else
777 long_message_size = LONG_MESSAGE_SIZE;
778 timeout[peer_nr] = GNUNET_TIME_relative_to_absolute (
779 GNUNET_TIME_relative_multiply (
780 GNUNET_TIME_UNIT_SECONDS,
781 TIMEOUT_MULTIPLIER));
782 GNUNET_assert (NULL == to_task[peer_nr]);
783 to_task[peer_nr] = GNUNET_SCHEDULER_add_delayed (
784 GNUNET_TIME_relative_multiply (
785 GNUNET_TIME_UNIT_SECONDS,
786 TIMEOUT_MULTIPLIER),
787 &latency_timeout,
788 tc_h);
789 choose_phase (tc_h);
790}
791
792
793static void
794update_avg_latency (const char *payload, unsigned int peer_nr)
795{
796 struct GNUNET_TIME_AbsoluteNBO *ts_n;
797 struct GNUNET_TIME_Absolute ts;
798 struct GNUNET_TIME_Relative latency;
799 size_t num_received = 0;
800
801 ts_n = (struct GNUNET_TIME_AbsoluteNBO *) payload;
802 ts = GNUNET_TIME_absolute_ntoh (*ts_n);
803 latency = GNUNET_TIME_absolute_get_duration (ts);
804
805 switch (phase[peer_nr])
806 {
807 case TP_INIT:
808 GNUNET_assert (0);
809 break;
810 case TP_BURST_SHORT:
811 num_received = num_received_short[peer_nr];
812 break;
813 case TP_BURST_LONG:
814 num_received = num_received_long[peer_nr];
815 break;
816 case TP_SIZE_CHECK:
817 num_received = num_received_size[peer_nr];
818 break;
819 }
820 if (1 >= num_received)
821 avg_latency[peer_nr] = latency.rel_value_us;
822 else
823 avg_latency[peer_nr] = ((avg_latency[peer_nr] * (num_received - 1))
824 + latency.rel_value_us)
825 / num_received;
826 LOG (GNUNET_ERROR_TYPE_DEBUG,
827 "Latency of received packet by peer %u: %s with avg latency %lu\n",
828 peer_nr,
829 GNUNET_STRINGS_relative_time_to_string (latency,
830 GNUNET_YES),
831 avg_latency[peer_nr]);
832}
833
834
835
836
837static void
838load_phase_config ()
839{
840
841 phase_short[0] = GNUNET_CONFIGURATION_get_value_yesno (cfg_peers[0],
842 TEST_SECTION,
843 "PHASE_SHORT");
844 if (GNUNET_SYSERR == phase_short[0])
845 phase_short[0] = GNUNET_YES;
846
847 phase_short[1] = phase_short[0];
848
849 phase_long[0] = GNUNET_CONFIGURATION_get_value_yesno (cfg_peers[0],
850 TEST_SECTION,
851 "PHASE_LONG");
852
853 if (GNUNET_SYSERR == phase_long[0])
854 phase_long[0] = GNUNET_YES;
855
856 phase_long[1] = phase_long[0];
857
858 phase_size[0] = GNUNET_CONFIGURATION_get_value_yesno (cfg_peers[0],
859 TEST_SECTION,
860 "PHASE_SIZE");
861
862 if (GNUNET_SYSERR == phase_size[0])
863 phase_size[0] = GNUNET_YES;
864
865 phase_size[1] = phase_size[0];
866}
867
868
869
870/**
871 * @brief Handle an incoming message
872 *
873 * Implements #GNUNET_TRANSPORT_TESTING_IncomingMessageCallback
874
875 * @param cls Closure
876 * @param tc_h Handle to the receiving communicator
877 * @param msg Received message
878 */
879static void
880incoming_message_cb (
881 void *cls,
882 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
883 const char *payload,
884 size_t payload_len)
885{
886 unsigned int peer_nr;
887
888
889 peer_nr = get_peer_nr (cls, GNUNET_YES);
890
891 if ((GNUNET_NO == bidirect)&&(0 != strcmp ((char*) cls,
892 cfg_peers_name[NUM_PEERS - 1])))
893 {
894 LOG (GNUNET_ERROR_TYPE_WARNING,
895 "unexpected receiver...\n");
896 return;
897 }
898 /* Reset timeout */
899 timeout[peer_nr] = GNUNET_TIME_relative_to_absolute (
900 GNUNET_TIME_relative_multiply (
901 GNUNET_TIME_UNIT_SECONDS,
902 TIMEOUT_MULTIPLIER));
903 switch (phase[peer_nr])
904 {
905 case TP_INIT:
906 GNUNET_break (0);
907 break;
908 case TP_BURST_SHORT:
909 {
910 GNUNET_assert (SHORT_MESSAGE_SIZE == payload_len);
911 num_received_short[peer_nr]++;
912
913 update_avg_latency (payload, peer_nr);
914 if ((num_sent_short[peer_nr] == burst_packets_short) &&
915 (num_received_short[peer_nr] ==
916 burst_packets_short))
917 {
918 finish_phase_short (peer_nr);
919 }
920 break;
921 }
922 case TP_BURST_LONG:
923 {
924 if (long_message_size != payload_len)
925 {
926 LOG (GNUNET_ERROR_TYPE_WARNING,
927 "Ignoring packet with wrong length\n");
928 return; // Ignore
929 }
930 num_received_long[peer_nr]++;
931
932 update_avg_latency (payload, peer_nr);
933 if ((num_sent_long[peer_nr] == burst_packets_long) &&
934 (num_received_long[peer_nr] >
935 burst_packets_long))
936 {
937 finish_phase_long (peer_nr);
938 }
939 break;
940 }
941 case TP_SIZE_CHECK:
942 {
943 size_t max_size = 64000;
944
945 GNUNET_assert (TP_SIZE_CHECK == phase[peer_nr]);
946 if (LONG_MESSAGE_SIZE != long_message_size)
947 max_size = long_message_size;
948 num_received_size[peer_nr]++;
949 update_avg_latency (payload, peer_nr);
950 if ((GNUNET_YES == phase_size[peer_nr]) && (num_received_size[peer_nr] >=
951 (max_size) / 10) )
952 {
953 LOG (GNUNET_ERROR_TYPE_MESSAGE,
954 "Size packet test for peer %u done.\n",
955 peer_nr);
956 LOG (GNUNET_ERROR_TYPE_MESSAGE,
957 "%lu/%lu packets -- avg latency: %llu us\n",
958 (unsigned long) num_received_size[peer_nr],
959 (unsigned long) num_sent_size[peer_nr],
960 (unsigned long long) avg_latency[peer_nr]);
961 iterations_left[peer_nr]--;
962 phase_size[peer_nr] = GNUNET_NO;
963 if (0 != iterations_left[peer_nr])
964 {
965 // start_short = GNUNET_TIME_absolute_get ();
966 // phase = TP_BURST_SHORT;
967 num_received_size[peer_nr] = 0;
968 num_sent_size[peer_nr] = 0;
969 avg_latency[peer_nr] = 0;
970 num_sent_short[peer_nr] = 0;
971 num_sent_long[peer_nr] = 0;
972 num_received_short[peer_nr] = 0;
973 num_received_long[peer_nr] = 0;
974 // short_test (NULL);
975 if (((PEER_A == peer_nr) && finished[PEER_B]) || ((PEER_B ==
976 peer_nr) &&
977 finished[PEER_A]))
978 {
979 load_phase_config ();
980 }
981 }
982 choose_phase (get_tc_h (peer_nr));
983 }
984 break;
985 }
986 }
987}
988
989
990static void
991do_shutdown (void *cls)
992{
993 LOG (GNUNET_ERROR_TYPE_DEBUG,
994 "shutting down test.\n");
995
996 for (unsigned int i = 0; i < NUM_PEERS; i++)
997 {
998 if (NULL != box_stats[i])
999 {
1000 GNUNET_STATISTICS_get_cancel (box_stats[i]);
1001 box_stats[i] = NULL;
1002 }
1003 if (NULL != rekey_stats[i])
1004 {
1005 GNUNET_STATISTICS_get_cancel (rekey_stats[i]);
1006 rekey_stats[i] = NULL;
1007 }
1008 if (NULL != to_task[i])
1009 {
1010 GNUNET_SCHEDULER_cancel (to_task[i]);
1011 to_task[i] = NULL;
1012 }
1013 GNUNET_TRANSPORT_TESTING_transport_communicator_service_stop (tc_hs[i]);
1014 GNUNET_STATISTICS_destroy (stats[i], GNUNET_NO);
1015 }
1016}
1017
1018
1019
1020/**
1021 * @brief Main function called by the scheduler
1022 *
1023 * @param cls Closure - Handle to confiation
1024 */
1025static void
1026run (void *cls)
1027{
1028 ret = 0;
1029 // num_received = 0;
1030 // num_sent = 0;
1031 for (unsigned int i = 0; i < NUM_PEERS; i++)
1032 {
1033 tc_hs[i] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
1034 "transport",
1035 communicator_binary,
1036 cfg_peers_name[i],
1037 &peer_id[i],
1038 &communicator_available_cb,
1039 &add_address_cb,
1040 &queue_create_reply_cb,
1041 &add_queue_cb,
1042 &incoming_message_cb,
1043 &handle_backchannel_cb,
1044 cfg_peers_name[i]); /* cls */
1045
1046 if ((0 == strcmp ("udp", communicator_name)) && ((0 == strcmp ("rekey",
1047 test_name))||
1048 (0 == strcmp (
1049 "backchannel",
1050 test_name))) )
1051 {
1052 stats[i] = GNUNET_STATISTICS_create ("C-UDP",
1053 cfg_peers[i]);
1054 }
1055 else if ((0 == strcmp ("bidirect", test_name)))
1056 {
1057 bidirect = GNUNET_YES;
1058 }
1059 }
1060 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
1061 NULL);
1062}
1063
1064int
1065main (int argc,
1066 char *const *argv)
1067{
1068 struct GNUNET_CRYPTO_EddsaPrivateKey *private_key;
1069 char *test_mode;
1070 char *cfg_peer;
1071
1072 iterations_left[0] = TOTAL_ITERATIONS;
1073 iterations_left[1] = TOTAL_ITERATIONS;
1074 phase[0] = TP_INIT;
1075 phase[1] = TP_INIT;
1076 ret = 1;
1077 test_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
1078 communicator_name = strchr (test_name, '-');
1079 communicator_name[0] = '\0';
1080 communicator_name++;
1081 test_mode = test_name;
1082
1083 GNUNET_asprintf (&communicator_binary,
1084 "gnunet-communicator-%s",
1085 communicator_name);
1086
1087 if (GNUNET_OK !=
1088 GNUNET_log_setup ("test_communicator_basic",
1089 "DEBUG",
1090 NULL))
1091 {
1092 fprintf (stderr, "Unable to setup log\n");
1093 GNUNET_break (0);
1094 return 2;
1095 }
1096 for (unsigned int i = 0; i < NUM_PEERS; i++)
1097 {
1098 GNUNET_asprintf ((&cfg_peer),
1099 "test_communicator_%s_%s_peer%u.conf",
1100 communicator_name, test_mode, i + 1);
1101 cfg_peers_name[i] = cfg_peer;
1102 cfg_peers[i] = GNUNET_CONFIGURATION_create ();
1103 if (GNUNET_YES ==
1104 GNUNET_DISK_file_test (cfg_peers_name[i]))
1105 {
1106 if (GNUNET_SYSERR ==
1107 GNUNET_CONFIGURATION_load (cfg_peers[i],
1108 cfg_peers_name[i]))
1109 {
1110 fprintf (stderr,
1111 "Malformed configuration file `%s', exiting ...\n",
1112 cfg_peers_name[i]);
1113 return 1;
1114 }
1115 }
1116 else
1117 {
1118 if (GNUNET_SYSERR ==
1119 GNUNET_CONFIGURATION_load (cfg_peers[i],
1120 NULL))
1121 {
1122 fprintf (stderr,
1123 "Configuration file %s does not exist, exiting ...\n",
1124 cfg_peers_name[i]);
1125 return 1;
1126 }
1127 }
1128 private_key =
1129 GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg_peers[i]);
1130 if (NULL == private_key)
1131 {
1132 LOG (GNUNET_ERROR_TYPE_ERROR,
1133 "Unable to get peer ID\n");
1134 return 1;
1135 }
1136 GNUNET_CRYPTO_eddsa_key_get_public (private_key,
1137 &peer_id[i].public_key);
1138 GNUNET_free (private_key);
1139 LOG (GNUNET_ERROR_TYPE_INFO,
1140 "Identity of peer %u is %s\n",
1141 i,
1142 GNUNET_i2s_full (&peer_id[i]));
1143 }
1144 if (GNUNET_OK !=
1145 GNUNET_CONFIGURATION_get_value_number (cfg_peers[0],
1146 TEST_SECTION,
1147 "ALLOWED_PACKET_LOSS_SHORT",
1148 &allowed_packet_loss_short))
1149 allowed_packet_loss_short = ALLOWED_PACKET_LOSS;
1150 if (GNUNET_OK !=
1151 GNUNET_CONFIGURATION_get_value_number (cfg_peers[0],
1152 TEST_SECTION,
1153 "ALLOWED_PACKET_LOSS_LONG",
1154 &allowed_packet_loss_long))
1155 allowed_packet_loss_long = ALLOWED_PACKET_LOSS;
1156 if (GNUNET_OK !=
1157 GNUNET_CONFIGURATION_get_value_number (cfg_peers[0],
1158 TEST_SECTION,
1159 "BURST_PACKETS_SHORT",
1160 &burst_packets_short))
1161 burst_packets_short = BURST_PACKETS;
1162 if (GNUNET_OK !=
1163 GNUNET_CONFIGURATION_get_value_number (cfg_peers[0],
1164 TEST_SECTION,
1165 "BURST_ÜACKETS_LONG",
1166 &burst_packets_long))
1167 burst_packets_long = BURST_PACKETS;
1168 if (GNUNET_OK !=
1169 GNUNET_CONFIGURATION_get_value_number (cfg_peers[0],
1170 TEST_SECTION,
1171 "DELAY_SHORT",
1172 &delay_short_value))
1173 delay_short = DELAY;
1174 else
1175 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS,
1176 delay_short_value);
1177 if (GNUNET_OK !=
1178 GNUNET_CONFIGURATION_get_value_number (cfg_peers[0],
1179 TEST_SECTION,
1180 "DELAY_SHORT",
1181 &delay_long_value))
1182 delay_long = DELAY;
1183 else
1184 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS,
1185 delay_long_value);
1186 load_phase_config ();
1187 LOG (GNUNET_ERROR_TYPE_MESSAGE, "Starting test...\n");
1188 LOG (GNUNET_ERROR_TYPE_DEBUG,
1189 "argv[0]: %s\n",
1190 argv[0]);
1191 LOG (GNUNET_ERROR_TYPE_DEBUG,
1192 "test_name: %s\n",
1193 test_name);
1194 LOG (GNUNET_ERROR_TYPE_DEBUG,
1195 "communicator_name: %s\n",
1196 communicator_name);
1197 LOG (GNUNET_ERROR_TYPE_DEBUG,
1198 "communicator_binary: %s\n",
1199 communicator_binary);
1200 GNUNET_SCHEDULER_run (&run,
1201 NULL);
1202 return ret;
1203}