2 This file is part of GNUnet.
3 Copyright (C) 2019 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file transport/test_communicator_basic.c
23 * @brief test the communicators
24 * @author Julius Bünger
25 * @author Martin Schanzenbach
28 #include "gnunet_util_lib.h"
29 #include "transport-testing2.h"
30 #include "gnunet_ats_transport_service.h"
31 #include "gnunet_signatures.h"
32 #include "gnunet_testing_lib.h"
33 #include "transport.h"
38 #define LOG(kind, ...) GNUNET_log_from (kind, \
39 "test_transport_communicator", \
44 static struct GNUNET_SCHEDULER_Task *to_task;
46 static struct GNUNET_SCHEDULER_Task *active_task;
48 static int queue_est = GNUNET_NO;
50 static struct GNUNET_PeerIdentity peer_id[NUM_PEERS];
52 static char *communicator_binary;
55 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS];
57 static struct GNUNET_CONFIGURATION_Handle *cfg_peers[NUM_PEERS];
59 static char *cfg_peers_name[NUM_PEERS];
63 static struct GNUNET_TIME_Absolute start_short;
65 static struct GNUNET_TIME_Absolute start_long;
67 static struct GNUNET_TIME_Absolute timeout;
69 static struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *my_tc;
71 #define SHORT_MESSAGE_SIZE 128
73 #define LONG_MESSAGE_SIZE 32000
75 #define BURST_PACKETS 5000
77 #define TOTAL_ITERATIONS 5
79 static unsigned int iterations_left = TOTAL_ITERATIONS;
81 #define SHORT_BURST_WINDOW \
82 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,2)
84 #define LONG_BURST_WINDOW \
85 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,2)
95 static size_t num_sent = 0;
97 static uint32_t ack = 0;
99 static enum TestPhase phase;
101 static size_t num_received = 0;
103 static uint64_t avg_latency = 0;
105 static struct GNUNET_TIME_Relative duration;
109 communicator_available_cb (void *cls,
111 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
113 enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc,
114 char *address_prefix)
116 LOG (GNUNET_ERROR_TYPE_INFO,
117 "Communicator available. (cc: %u, prefix: %s)\n",
124 add_address_cb (void *cls,
125 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
128 struct GNUNET_TIME_Relative expiration,
130 enum GNUNET_NetworkType nt)
132 LOG (GNUNET_ERROR_TYPE_DEBUG,
133 "New address. (addr: %s, expir: %" PRIu32 ", ID: %" PRIu32 ", nt: %u\n",
135 expiration.rel_value_us,
138 // addresses[1] = GNUNET_strdup (address);
139 if ((0 == strcmp ((char*) cls, cfg_peers_name[NUM_PEERS - 1])) &&
140 (GNUNET_NO == queue_est))
142 queue_est = GNUNET_YES;
143 GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (tc_hs[0],
153 * @brief Callback that informs whether the requested queue will be
156 * Implements #GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback.
158 * @param cls Closure - unused
159 * @param tc_h Communicator handle - unused
160 * @param will_try #GNUNET_YES if queue will be established
161 * #GNUNET_NO if queue will not be established (bogous address)
164 queue_create_reply_cb (void *cls,
166 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
170 if (GNUNET_YES == will_try)
171 LOG (GNUNET_ERROR_TYPE_DEBUG,
172 "Queue will be established!\n");
174 LOG (GNUNET_ERROR_TYPE_WARNING,
175 "Queue won't be established (bougus address?)!\n");
180 make_payload (size_t payload_size)
182 struct GNUNET_TIME_Absolute ts;
183 struct GNUNET_TIME_AbsoluteNBO ts_n;
184 char *payload = GNUNET_malloc (payload_size);
186 GNUNET_assert (payload_size >= 8); // So that out timestamp fits
187 ts = GNUNET_TIME_absolute_get ();
188 ts_n = GNUNET_TIME_absolute_hton (ts);
189 memset (payload, 0, payload_size);
190 memcpy (payload, &ts_n, sizeof (struct GNUNET_TIME_AbsoluteNBO));
196 latency_timeout (void *cls)
199 if (GNUNET_TIME_absolute_get_remaining (timeout).rel_value_us > 0)
201 to_task = GNUNET_SCHEDULER_add_at (timeout,
207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
208 "Latency too high. Test failed. (Phase: %d. Sent: %lu, Received: %lu)\n",
209 phase, num_sent, num_received);
211 GNUNET_SCHEDULER_shutdown ();
216 size_test (void *cls)
221 GNUNET_assert (TP_SIZE_CHECK == phase);
223 return; /* Leave some room for our protocol, so not 2^16 exactly */
224 payload = make_payload (ack);
225 GNUNET_TRANSPORT_TESTING_transport_communicator_send (my_tc,
228 GNUNET_free (payload);
231 timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS);
233 active_task = GNUNET_SCHEDULER_add_now (&size_test,
239 long_test (void *cls)
244 payload = make_payload (LONG_MESSAGE_SIZE);
245 GNUNET_TRANSPORT_TESTING_transport_communicator_send (my_tc,
249 GNUNET_free (payload);
250 timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS);
251 if (num_sent == BURST_PACKETS)
253 active_task = GNUNET_SCHEDULER_add_now (&long_test,
259 short_test (void *cls)
264 payload = make_payload (SHORT_MESSAGE_SIZE);
265 GNUNET_TRANSPORT_TESTING_transport_communicator_send (my_tc,
269 GNUNET_free (payload);
270 timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS);
271 if (num_sent >= BURST_PACKETS)
273 active_task = GNUNET_SCHEDULER_add_now (&short_test,
279 * @brief Handle opening of queue
281 * Issues sending of test data
283 * Implements #GNUNET_TRANSPORT_TESTING_AddQueueCallback
286 * @param tc_h Communicator handle
287 * @param tc_queue Handle to newly opened queue
290 add_queue_cb (void *cls,
291 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
292 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *
295 if (0 != strcmp ((char*) cls, cfg_peers_name[0]))
297 LOG (GNUNET_ERROR_TYPE_DEBUG,
298 "Queue established, starting test...\n");
299 start_short = GNUNET_TIME_absolute_get ();
301 phase = TP_BURST_SHORT;
302 timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS);
303 GNUNET_assert (NULL == to_task);
304 to_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
307 GNUNET_assert (NULL == active_task);
308 active_task = GNUNET_SCHEDULER_add_now (&short_test,
314 update_avg_latency (const char*payload)
316 struct GNUNET_TIME_AbsoluteNBO *ts_n;
317 struct GNUNET_TIME_Absolute ts;
318 struct GNUNET_TIME_Relative latency;
320 ts_n = (struct GNUNET_TIME_AbsoluteNBO *) payload;
321 ts = GNUNET_TIME_absolute_ntoh (*ts_n);
322 latency = GNUNET_TIME_absolute_get_duration (ts);
323 if (1 >= num_received)
324 avg_latency = latency.rel_value_us;
326 avg_latency = ((avg_latency * (num_received - 1)) + latency.rel_value_us)
333 * @brief Handle an incoming message
335 * Implements #GNUNET_TRANSPORT_TESTING_IncomingMessageCallback
338 * @param tc_h Handle to the receiving communicator
339 * @param msg Received message
342 incoming_message_cb (void *cls,
343 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
348 if (0 != strcmp ((char*) cls, cfg_peers_name[NUM_PEERS - 1]))
350 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
351 "unexpected receiver...\n");
355 timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS);
360 GNUNET_assert (SHORT_MESSAGE_SIZE == payload_len);
362 duration = GNUNET_TIME_absolute_get_duration (start_short);
363 update_avg_latency (payload);
364 if (num_received == BURST_PACKETS)
366 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
367 "Short size packet test done.\n");
368 char *goodput = GNUNET_STRINGS_byte_size_fancy ((SHORT_MESSAGE_SIZE
369 * num_received * 1000 * 1000)
370 / duration.rel_value_us);
371 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
372 "%lu/%lu packets in %llu us (%s/s) -- avg latency: %llu us\n",
373 (unsigned long) num_received,
374 (unsigned long) num_sent,
375 (unsigned long long) duration.rel_value_us,
377 (unsigned long long) avg_latency);
378 GNUNET_free (goodput);
379 start_long = GNUNET_TIME_absolute_get ();
380 phase = TP_BURST_LONG;
384 active_task = GNUNET_SCHEDULER_add_now (&long_test,
391 if (LONG_MESSAGE_SIZE != payload_len)
393 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
394 "Ignoring packet with wrong length\n");
398 duration = GNUNET_TIME_absolute_get_duration (start_long);
399 update_avg_latency (payload);
400 if (num_received == BURST_PACKETS)
402 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
403 "Long size packet test done.\n");
404 char *goodput = GNUNET_STRINGS_byte_size_fancy ((LONG_MESSAGE_SIZE
405 * num_received * 1000 * 1000)
406 / duration.rel_value_us);
408 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
409 "%lu/%lu packets in %llu us (%s/s) -- avg latency: %llu us\n",
410 (unsigned long) num_received,
411 (unsigned long) num_sent,
412 (unsigned long long) duration.rel_value_us,
414 (unsigned long long) avg_latency);
415 GNUNET_free (goodput);
417 phase = TP_SIZE_CHECK;
421 active_task = GNUNET_SCHEDULER_add_now (&size_test,
429 update_avg_latency (payload);
430 if (num_received >= (64000 - 10) / 5)
432 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
433 "Size packet test done.\n");
434 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
435 "%lu/%lu packets -- avg latency: %llu us\n",
436 (unsigned long) num_received,
437 (unsigned long) num_sent,
438 (unsigned long long) avg_latency);
443 if (0 != iterations_left)
445 start_short = GNUNET_TIME_absolute_get ();
446 phase = TP_BURST_SHORT;
447 active_task = GNUNET_SCHEDULER_add_now (&short_test,
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 GNUNET_SCHEDULER_shutdown ();
462 do_shutdown (void *cls)
466 GNUNET_SCHEDULER_cancel (to_task);
469 if (NULL != active_task)
471 GNUNET_SCHEDULER_cancel (active_task);
478 * @brief Main function called by the scheduler
480 * @param cls Closure - Handle to configuration
488 for (unsigned int i = 0; i < NUM_PEERS; i++)
490 tc_hs[i] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
494 &communicator_available_cb,
496 &queue_create_reply_cb,
498 &incoming_message_cb,
499 cfg_peers_name[i]); /* cls */
501 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
510 struct GNUNET_CRYPTO_EddsaPrivateKey *private_key;
511 char *communicator_name;
517 test_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
518 communicator_name = strchr (test_name, '-');
519 communicator_name[0] = '\0';
521 test_mode = test_name;
523 GNUNET_asprintf (&communicator_binary,
524 "gnunet-communicator-%s",
527 GNUNET_log_setup ("test_communicator_basic",
531 fprintf (stderr, "Unable to setup log\n");
535 for (unsigned int i = 0; i < NUM_PEERS; i++)
537 GNUNET_asprintf ((&cfg_peer),
538 "test_communicator_%s_%s_peer%u.conf",
539 communicator_name, test_mode, i + 1);
540 cfg_peers_name[i] = cfg_peer;
541 cfg_peers[i] = GNUNET_CONFIGURATION_create ();
543 GNUNET_DISK_file_test (cfg_peers_name[i]))
546 GNUNET_CONFIGURATION_load (cfg_peers[i],
550 "Malformed configuration file `%s', exiting ...\n",
558 GNUNET_CONFIGURATION_load (cfg_peers[i],
562 "Configuration file %s does not exist, exiting ...\n",
568 GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg_peers[i]);
569 if (NULL == private_key)
571 LOG (GNUNET_ERROR_TYPE_ERROR,
572 "Unable to get peer ID\n");
575 GNUNET_CRYPTO_eddsa_key_get_public (private_key,
576 &peer_id[i].public_key);
577 GNUNET_free (private_key);
578 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
579 "Identity of peer %u is %s\n",
581 GNUNET_i2s_full (&peer_id[i]));
583 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Starting test...\n");
584 GNUNET_SCHEDULER_run (&run,