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_PeerIdentity peer_id[NUM_PEERS];
46 static char *communicator_binary;
49 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS];
51 static struct GNUNET_CONFIGURATION_Handle *cfg_peers[NUM_PEERS];
53 static char **cfg_peers_name;
57 static struct GNUNET_TIME_Absolute start_short;
59 static struct GNUNET_TIME_Absolute start_long;
61 static struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *my_tc;
63 #define SHORT_MESSAGE_SIZE 128
65 #define LONG_MESSAGE_SIZE 32000
67 #define SHORT_BURST_SECONDS 2
69 #define LONG_BURST_SECONDS 2
71 #define SHORT_BURST_WINDOW \
72 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,SHORT_BURST_SECONDS)
74 #define LONG_BURST_WINDOW \
75 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,SHORT_BURST_SECONDS)
85 static int buf_len = 0;
87 static uint32_t ack = 0;
91 static size_t num_received = 0;
93 static uint64_t avg_latency = 0;
96 communicator_available_cb (void *cls,
98 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
100 enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc,
101 char *address_prefix)
103 LOG (GNUNET_ERROR_TYPE_INFO,
104 "Communicator available. (cc: %u, prefix: %s)\n",
111 add_address_cb (void *cls,
112 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
115 struct GNUNET_TIME_Relative expiration,
117 enum GNUNET_NetworkType nt)
119 LOG (GNUNET_ERROR_TYPE_DEBUG,
120 "New address. (addr: %s, expir: %" PRIu32 ", ID: %" PRIu32 ", nt: %u\n",
122 expiration.rel_value_us,
125 // addresses[1] = GNUNET_strdup (address);
126 if (0 == strcmp ((char*) cls, cfg_peers_name[NUM_PEERS - 1]))
127 GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (tc_hs[0],
136 * @brief Callback that informs whether the requested queue will be
139 * Implements #GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback.
141 * @param cls Closure - unused
142 * @param tc_h Communicator handle - unused
143 * @param will_try #GNUNET_YES if queue will be established
144 * #GNUNET_NO if queue will not be established (bogous address)
147 queue_create_reply_cb (void *cls,
149 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
153 if (GNUNET_YES == will_try)
154 LOG (GNUNET_ERROR_TYPE_DEBUG,
155 "Queue will be established!\n");
157 LOG (GNUNET_ERROR_TYPE_WARNING,
158 "Queue won't be established (bougus address?)!\n");
163 make_payload (size_t payload_size)
165 char *payload = GNUNET_malloc (payload_size);
166 struct GNUNET_TIME_Absolute ts;
167 struct GNUNET_TIME_AbsoluteNBO ts_n;
168 GNUNET_assert (payload_size >= 8); // So that out timestamp fits
169 ts = GNUNET_TIME_absolute_get ();
170 ts_n = GNUNET_TIME_absolute_hton (ts);
171 memset (payload, 0, payload_size);
172 memcpy (payload, &ts_n, sizeof (struct GNUNET_TIME_AbsoluteNBO));
178 size_test (void *cls)
183 if (ack < 64000) // Leave some room for our protocol.
185 payload = make_payload (ack);
186 GNUNET_TRANSPORT_TESTING_transport_communicator_send (my_tc,
189 GNUNET_free (payload);
192 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
193 "Size packet test done.\n");
194 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
195 "#packets: %lu -- latency: %lu\n",
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 GNUNET_SCHEDULER_shutdown ();
206 long_test (void *cls)
208 struct GNUNET_TIME_Relative duration = GNUNET_TIME_absolute_get_duration (
211 if (LONG_BURST_WINDOW.rel_value_us > duration.rel_value_us)
213 //FIXME: Not sure how aggressive we should be here, our transport does not
214 //implement congestion control or flow control... (also for the other three
215 if (buf_len < MAX_BUF_LEN)
217 payload = make_payload (LONG_MESSAGE_SIZE);
218 GNUNET_TRANSPORT_TESTING_transport_communicator_send (my_tc,
222 GNUNET_free (payload);
223 GNUNET_SCHEDULER_add_now (&long_test, NULL);
227 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
228 "Long size packet test done.\n");
229 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
230 "goodput: %lu b/s -- #packets: %lu -- latency: %lu\n",
231 (LONG_MESSAGE_SIZE * num_received) / LONG_BURST_SECONDS,
238 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &size_test, NULL);
243 short_test (void *cls)
245 struct GNUNET_TIME_Relative duration = GNUNET_TIME_absolute_get_duration (
248 if (SHORT_BURST_WINDOW.rel_value_us > duration.rel_value_us)
250 if (buf_len < MAX_BUF_LEN)
252 payload = make_payload (SHORT_MESSAGE_SIZE);
253 GNUNET_TRANSPORT_TESTING_transport_communicator_send (my_tc,
257 GNUNET_free (payload);
258 GNUNET_SCHEDULER_add_now (&short_test, NULL);
262 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
263 "Short size packet test done.\n");
264 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
265 "goodput: %lu b/s -- #packets: %lu -- latency: %lu\n",
266 (SHORT_MESSAGE_SIZE * num_received) / SHORT_BURST_SECONDS,
269 start_long = GNUNET_TIME_absolute_get ();
274 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &long_test, NULL);
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 ();
303 GNUNET_SCHEDULER_add_now (&short_test, tc_queue);
308 update_avg_latency (const char*payload)
310 struct GNUNET_TIME_AbsoluteNBO *ts_n;
311 struct GNUNET_TIME_Absolute ts;
312 struct GNUNET_TIME_Relative latency;
314 ts_n = (struct GNUNET_TIME_AbsoluteNBO *) payload;
315 ts = GNUNET_TIME_absolute_ntoh (*ts_n);
316 latency = GNUNET_TIME_absolute_get_duration (ts);
317 if (1 == num_received)
318 avg_latency = latency.rel_value_us;
320 avg_latency = ((avg_latency * (num_received - 1)) + latency.rel_value_us)
327 * @brief Handle an incoming message
329 * Implements #GNUNET_TRANSPORT_TESTING_IncomingMessageCallback
332 * @param tc_h Handle to the receiving communicator
333 * @param msg Received message
336 incoming_message_cb (void *cls,
337 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
342 if (0 != strcmp ((char*) cls, cfg_peers_name[NUM_PEERS - 1]))
344 if (phase == BURST_SHORT)
346 GNUNET_assert (SHORT_MESSAGE_SIZE == payload_len);
348 update_avg_latency (payload);
349 if (buf_len == MAX_BUF_LEN)
350 GNUNET_SCHEDULER_add_now (&short_test, NULL);
353 else if (phase == BURST_LONG)
355 if (LONG_MESSAGE_SIZE != payload_len)
358 update_avg_latency (payload);
359 if (buf_len == MAX_BUF_LEN)
360 GNUNET_SCHEDULER_add_now (&long_test, NULL);
363 else // if (phase == SIZE_CHECK) {
365 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366 // "Receiving payload with size %lu...\n", payload_len);
367 if (ack != payload_len)
369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
370 "Error receiving message, corrupted.\n");
372 GNUNET_SCHEDULER_shutdown ();
376 update_avg_latency (payload);
377 ack += 5; // Next expected message size
378 GNUNET_SCHEDULER_add_now (&size_test, NULL);
384 * @brief Main function called by the scheduler
386 * @param cls Closure - Handle to configuration
393 for (int i = 0; i < NUM_PEERS; i++)
395 tc_hs[i] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
399 &communicator_available_cb,
401 &queue_create_reply_cb,
403 &incoming_message_cb,
404 cfg_peers_name[i]); /* cls */
413 struct GNUNET_CRYPTO_EddsaPrivateKey *private_key;
414 char *communicator_name;
418 communicator_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
419 GNUNET_asprintf (&communicator_binary, "gnunet-communicator-%s",
421 cfg_peers_name = GNUNET_malloc (sizeof(char*) * NUM_PEERS);
422 if (GNUNET_OK != GNUNET_log_setup ("test_communicator_basic",
424 "test_communicator_basic.log"))
426 fprintf (stderr, "Unable to setup log\n");
430 for (int i = 0; i < NUM_PEERS; i++)
432 GNUNET_asprintf ((&cfg_peer),
433 "test_communicator_%s_peer%u.conf",
434 communicator_name, i + 1);
435 cfg_peers_name[i] = cfg_peer;
436 cfg_peers[i] = GNUNET_CONFIGURATION_create ();
438 GNUNET_DISK_file_test (cfg_peers_name[i]))
441 GNUNET_CONFIGURATION_load (cfg_peers[i],
445 "Malformed configuration file `%s', exiting ...\n",
453 GNUNET_CONFIGURATION_load (cfg_peers[i],
457 "Configuration file %s does not exist, exiting ...\n",
463 GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg_peers[i]);
464 if (NULL == private_key)
466 LOG (GNUNET_ERROR_TYPE_ERROR,
467 "Unable to get peer ID\n");
470 GNUNET_CRYPTO_eddsa_key_get_public (private_key,
471 &peer_id[i].public_key);
472 GNUNET_free (private_key);
473 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
474 "Identity of peer %u is %s\n",
475 i, GNUNET_i2s_full (&peer_id[i]));
477 fprintf (stderr, "Starting test...\n");
478 GNUNET_SCHEDULER_run (&run,