2 This file is part of GNUnet.
3 (C) 2011-2014 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file src/transport/gnunet-transport.c
23 * @brief Tool to help configure, measure and control the transport subsystem.
24 * @author Christian Grothoff
25 * @author Nathan Evans
27 * This utility can be used to test if a transport mechanism for
28 * GNUnet is properly configured.
31 #include "gnunet_util_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_transport_service.h"
37 struct Iteration *next;
38 struct Iteration *prev;
39 struct GNUNET_TIME_Absolute start;
40 struct GNUNET_TIME_Absolute end;
42 struct GNUNET_TIME_Relative dur;
44 unsigned int msgs_sent;
49 * Timeout for a connections
51 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
54 * Benchmarking block size in bye
56 #define DEFAULT_MESSAGE_SIZE 1024
59 * Benchmarking message count
61 #define DEFAULT_MESSAGE_COUNT 1024
64 * Benchmarking iteration count
66 #define DEFAULT_ITERATION_COUNT 1
71 static int benchmark_send;
76 static int benchmark_receive;
81 static unsigned int benchmark_count;
86 static unsigned int benchmark_iterations;
91 static unsigned int benchmark_size;
96 static unsigned int benchmark_running;
99 * Which peer should we connect to?
104 * Handle to transport service.
106 static struct GNUNET_TRANSPORT_Handle *handle;
109 * Configuration handle
111 static struct GNUNET_CONFIGURATION_Handle *cfg;
116 struct GNUNET_TRANSPORT_TryConnectHandle *tc_handle;
119 struct Iteration *ihead;
120 struct Iteration *itail;
123 * Global return value (0 success).
127 * Handle for current transmission request.
129 static struct GNUNET_TRANSPORT_TransmitHandle *th;
131 struct GNUNET_TRANSPORT_Blacklist *bl_handle;
134 * Identity of the peer we transmit to / connect to.
135 * (equivalent to 'cpid' string).
137 static struct GNUNET_PeerIdentity pid;
140 * Task scheduled for cleanup / termination of the process.
142 static GNUNET_SCHEDULER_TaskIdentifier end;
145 * Selected level of verbosity.
147 static int verbosity;
150 * Task run in monitor mode when the user presses CTRL-C to abort.
151 * Stops monitoring activity.
154 * @param tc scheduler context
157 shutdown_task (void *cls,
158 const struct GNUNET_SCHEDULER_TaskContext *tc)
160 struct Iteration *icur;
161 struct Iteration *inext;
163 if (NULL != tc_handle)
165 GNUNET_TRANSPORT_try_connect_cancel (tc_handle);
170 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
174 if (NULL != bl_handle )
176 GNUNET_TRANSPORT_blacklist_cancel (bl_handle);
182 GNUNET_TRANSPORT_disconnect (handle);
189 while (NULL != (icur = inext))
193 FPRINTF (stdout, _("%llu B in %llu ms == %.2f KB/s!\n"),
194 ((long long unsigned int) benchmark_count * benchmark_size),
195 ((long long unsigned int) itail->dur.rel_value_us / 1000),
196 (float) ((benchmark_count * benchmark_size) / 1024)/ ((float) itail->dur.rel_value_us / (1000 * 1000)));
198 FPRINTF (stdout, _("%u;%u"),benchmark_count, benchmark_size);
200 while (NULL != (icur = inext))
203 GNUNET_CONTAINER_DLL_remove (ihead, itail, icur);
205 FPRINTF (stdout, _(";%llu"),
206 (long long unsigned int) icur->dur.rel_value_us);
212 if (benchmark_receive)
214 duration = GNUNET_TIME_absolute_get_duration (start_time);
216 _("Received %llu bytes/s (%llu bytes in %s)\n"),
217 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
219 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
222 FPRINTF (stdout, _("\n"));
229 * Function called to notify a client about the socket
230 * begin ready to queue more data. @a buf will be
231 * NULL and @a size zero if the socket was closed for
232 * writing in the meantime.
235 * @param size number of bytes available in @a buf
236 * @param buf where the callee should write the message
237 * @return number of bytes written to @a buf
240 transmit_data (void *cls,
244 struct GNUNET_MessageHeader *m = buf;
247 if ((NULL == buf) || (0 == size))
255 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
256 GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
257 m->size = ntohs (size);
258 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
259 memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
261 if (itail->msgs_sent < benchmark_count)
263 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, benchmark_size,
264 GNUNET_TIME_UNIT_FOREVER_REL, &transmit_data, NULL );
273 if (itail->msgs_sent % 10 == 0 )
274 FPRINTF (stdout, _("."));
282 struct Iteration *icur;
287 benchmark_running = GNUNET_YES;
288 icur = GNUNET_new (struct Iteration);
289 GNUNET_CONTAINER_DLL_insert_tail (ihead, itail, icur);
290 icur->start = GNUNET_TIME_absolute_get();
294 _("\nStarting benchmark to `%s', starting to send %u messages in %u byte blocks\n"),
295 GNUNET_i2s (&pid), benchmark_count, benchmark_size);
297 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, benchmark_size,
298 GNUNET_TIME_UNIT_FOREVER_REL, &transmit_data, NULL );
308 static int it_count = 0;
311 itail->dur = GNUNET_TIME_absolute_get_duration (itail->start);
312 if (it_count == benchmark_iterations)
314 benchmark_running = GNUNET_NO;
315 if (GNUNET_SCHEDULER_NO_TASK != end)
316 GNUNET_SCHEDULER_cancel (end);
317 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
329 * Function called to notify transport users that another
330 * peer connected to us.
333 * @param peer the peer that connected
336 notify_connect (void *cls,
337 const struct GNUNET_PeerIdentity *peer)
339 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
342 _("Connected to different peer `%s'\n"), GNUNET_i2s (&pid));
348 _("Successfully connected to `%s'\n"),
351 if (NULL != tc_handle)
353 GNUNET_TRANSPORT_try_connect_cancel (tc_handle);
362 * Function called to notify transport users that another
363 * peer disconnected from us.
366 * @param peer the peer that disconnected
369 notify_disconnect (void *cls,
370 const struct GNUNET_PeerIdentity *peer)
372 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
374 if (GNUNET_YES == benchmark_running)
376 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
383 * Function called by the transport for each received message.
386 * @param peer (claimed) identity of the other peer
387 * @param message the message
390 notify_receive (void *cls,
391 const struct GNUNET_PeerIdentity *peer,
392 const struct GNUNET_MessageHeader *message)
394 if (benchmark_receive)
396 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
400 _("Received %u bytes from %s\n"),
401 (unsigned int) ntohs (message->size),
410 try_connect_cb (void *cls,
413 static int retries = 0;
415 if (GNUNET_OK == result)
425 FPRINTF (stdout, _("Retrying to connect to `%s'\n"), GNUNET_i2s (&pid));
427 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
434 _("Failed to send connect request to transport service\n"));
435 if (GNUNET_SCHEDULER_NO_TASK != end)
436 GNUNET_SCHEDULER_cancel (end);
437 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
444 blacklist_cb (void *cls, const struct GNUNET_PeerIdentity *peer)
446 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
450 _("Denying connection to `%s'\n"), GNUNET_i2s (peer));
451 return GNUNET_SYSERR;
460 * Function called with the result of the check if the 'transport'
461 * service is running.
463 * @param cls closure with our configuration
464 * @param result #GNUNET_YES if transport is running
467 testservice_task (void *cls, int result)
471 if (GNUNET_YES != result)
473 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
477 if (GNUNET_SERVER_MAX_MESSAGE_SIZE <= benchmark_size)
479 FPRINTF (stderr, _("Message size too big!\n"));
485 FPRINTF (stderr, _("No peer identity given\n"));
488 if ((GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid),
491 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
496 if (1 == benchmark_send)
500 _("Trying to send %u messages with size %u to peer `%s'\n"),
501 benchmark_count, benchmark_size, GNUNET_i2s (&pid));
503 else if (1 == benchmark_receive)
506 _("Trying to receive messages from peer `%s'\n"),
511 FPRINTF (stderr, _("No operation given\n"));
515 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
516 ¬ify_connect, ¬ify_disconnect);
520 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
525 bl_handle = GNUNET_TRANSPORT_blacklist (cfg, blacklist_cb, NULL);
526 tc_handle = GNUNET_TRANSPORT_try_connect(handle, &pid, try_connect_cb, NULL);
528 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
535 * Main function that will be run by the scheduler.
538 * @param args remaining command-line arguments
539 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
540 * @param mycfg configuration
546 const struct GNUNET_CONFIGURATION_Handle *mycfg)
548 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
549 GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
550 &testservice_task, (void *) cfg);
554 main (int argc, char * const *argv)
557 benchmark_count = DEFAULT_MESSAGE_COUNT;
558 benchmark_size = DEFAULT_MESSAGE_SIZE;
559 benchmark_iterations = DEFAULT_ITERATION_COUNT;
560 benchmark_running = GNUNET_NO;
562 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
565 gettext_noop ("send data to peer"),
566 0, &GNUNET_GETOPT_set_one, &benchmark_send},
567 { 'r', "receive", NULL, gettext_noop
568 ("receive data from peer"), 0,
569 &GNUNET_GETOPT_set_one, &benchmark_receive},
570 { 'i', "iterations", NULL, gettext_noop
572 &GNUNET_GETOPT_set_uint, &benchmark_iterations},
573 { 'n', "number", NULL, gettext_noop
574 ("number of messages to send"), 1,
575 &GNUNET_GETOPT_set_uint, &benchmark_count},
576 { 'm', "messagesize", NULL, gettext_noop
577 ("message size to use"), 1,
578 &GNUNET_GETOPT_set_uint, &benchmark_size},
579 { 'p', "peer", "PEER",
580 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
582 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
583 GNUNET_GETOPT_OPTION_END
586 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
589 res = GNUNET_PROGRAM_run (argc, argv,
591 gettext_noop ("Direct access to transport service."),
594 GNUNET_free((void *) argv);
595 if (GNUNET_OK == res)
600 /* end of gnunet-transport.c */