2 This file is part of GNUnet.
3 (C) 2011 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.
21 * @file mesh/test_mesh_small.c
23 * @brief Test for the mesh service: retransmission of traffic.
27 #include "mesh_test_lib.h"
28 #include "gnunet_mesh_service.h"
33 * How namy messages to send
35 #define TOTAL_PACKETS 1000
38 * How long until we give up on connecting the peers?
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
43 * Time to wait for stuff that should be rather fast
45 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
48 * DIFFERENT TESTS TO RUN
58 * Which test are we running?
63 * String with test name
68 * Flag to send traffic leaf->root in speed tests to test BCK_ACK logic.
70 static int test_backwards = GNUNET_NO;
73 * How many events have happened
78 * Each peer is supposed to generate the following callbacks:
79 * 1 incoming channel (@dest)
80 * 1 connected peer (@orig)
81 * 1 received data packet (@dest)
82 * 1 received data packet (@orig)
83 * 1 received channel destroy (@dest)
84 * _________________________________
85 * 5 x ok expected per peer
91 * Size of each test packet
93 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
96 * Operation to get peer ids.
98 struct GNUNET_TESTBED_Operation *t_op[2];
103 struct GNUNET_PeerIdentity *p_id[2];
111 * Is the setup initialized?
113 static int initialized;
116 * Number of payload packes sent
118 static int data_sent;
121 * Number of payload packets received
123 static int data_received;
126 * Number of payload packed explicitly (app level) acknowledged
131 * Total number of currently running peers.
133 static unsigned long long peers_running;
136 * Test context (to shut down).
138 struct GNUNET_MESH_TEST_Context *test_ctx;
141 * Task called to disconnect peers.
143 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
146 * Task To perform tests
148 static GNUNET_SCHEDULER_TaskIdentifier test_task;
151 * Task called to shutdown test.
153 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
156 * Mesh handle for the root peer
158 static struct GNUNET_MESH_Handle *h1;
161 * Mesh handle for the first leaf peer
163 static struct GNUNET_MESH_Handle *h2;
166 * Channel handle for the root peer
168 static struct GNUNET_MESH_Channel *ch;
171 * Channel handle for the dest peer
173 static struct GNUNET_MESH_Channel *incoming_ch;
176 * Time we started the data transmission (after channel has been established
179 static struct GNUNET_TIME_Absolute start_time;
183 * Show the results of the test (banwidth acheived) and log them to GAUGER
188 static struct GNUNET_TIME_Absolute end_time;
189 static struct GNUNET_TIME_Relative total_time;
191 end_time = GNUNET_TIME_absolute_get();
192 total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
193 FPRINTF (stderr, "\nResults of test \"%s\"\n", test_name);
194 FPRINTF (stderr, "Test time %s\n",
195 GNUNET_STRINGS_relative_time_to_string (total_time,
197 FPRINTF (stderr, "Test bandwidth: %f kb/s\n",
198 4 * TOTAL_PACKETS * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms
199 FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
200 TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms
201 GAUGER ("MESH", test_name,
202 TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000),
208 * Shut down peergroup, clean up.
210 * @param cls Closure (unused).
211 * @param tc Task Context.
214 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
217 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
222 * Disconnect from mesh services af all peers, call shutdown.
224 * @param cls Closure (unused).
225 * @param tc Task Context.
228 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
230 long line = (long) cls;
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "disconnecting mesh service of peers, called from line %ld\n",
236 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
237 for (i = 0; i < 2; i++)
239 GNUNET_TESTBED_operation_done (t_op[i]);
243 GNUNET_MESH_channel_destroy (ch);
246 if (NULL != incoming_ch)
248 GNUNET_MESH_channel_destroy (incoming_ch);
251 GNUNET_MESH_TEST_cleanup (test_ctx);
252 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
254 GNUNET_SCHEDULER_cancel (shutdown_handle);
256 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
261 * Abort test: schedule disconnect and shutdown immediately
263 * @param line Line in the code the abort is requested from (__LINE__).
266 abort_test (long line)
268 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
270 GNUNET_SCHEDULER_cancel (disconnect_task);
271 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
277 * Transmit ready callback.
279 * @param cls Closure (message type).
280 * @param size Size of the tranmist buffer.
281 * @param buf Pointer to the beginning of the buffer.
283 * @return Number of bytes written to buf.
286 tmt_rdy (void *cls, size_t size, void *buf);
290 * Task to schedule a new data transmission.
292 * @param cls Closure (peer #).
293 * @param tc Task Context.
296 data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
298 struct GNUNET_MESH_TransmitHandle *th;
299 struct GNUNET_MESH_Channel *channel;
301 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data task\n");
305 if (GNUNET_YES == test_backwards)
307 channel = incoming_ch;
313 th = GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
314 GNUNET_TIME_UNIT_FOREVER_REL,
315 size_payload, &tmt_rdy, (void *) 1L);
318 unsigned long i = (unsigned long) cls;
320 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Retransmission\n");
323 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " in 1 ms\n");
324 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
325 &data_task, (void *)1UL);
330 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "in %u ms\n", i);
331 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
332 GNUNET_TIME_UNIT_MILLISECONDS,
334 &data_task, (void *)i);
341 * Transmit ready callback
343 * @param cls Closure (message type).
344 * @param size Size of the buffer we have.
345 * @param buf Buffer to copy data to.
348 tmt_rdy (void *cls, size_t size, void *buf)
350 struct GNUNET_MessageHeader *msg = buf;
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 "tmt_rdy called, filling buffer\n");
355 if (size < size_payload || NULL == buf)
357 GNUNET_break (ok >= ok_goal - 2);
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 "size %u, buf %p, data_sent %u, data_received %u\n",
360 size, buf, data_sent, data_received);
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok %u, ok goal %u\n", ok, ok_goal);
365 msg->size = htons (size);
366 msg->type = htons ((long) cls);
367 data = (uint32_t *) &msg[1];
368 *data = htonl (data_sent);
369 if (GNUNET_NO == initialized)
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372 "sending initializer\n");
374 else if (SPEED == test)
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 " Sent packet %d\n", data_sent);
379 if (data_sent < TOTAL_PACKETS)
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 " Scheduling packet %d\n", data_sent + 1);
383 GNUNET_SCHEDULER_add_now (&data_task, NULL);
392 * Function is called whenever a message is received.
394 * @param cls closure (set from GNUNET_MESH_connect)
395 * @param channel connection to the other end
396 * @param channel_ctx place to store local state associated with the channel
397 * @param message the actual message
398 * @return GNUNET_OK to keep the connection open,
399 * GNUNET_SYSERR to close it (signal serious error)
402 data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
404 const struct GNUNET_MessageHeader *message)
406 long client = (long) cls;
407 long expected_target_client;
412 GNUNET_MESH_receive_done (channel);
416 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
418 GNUNET_SCHEDULER_cancel (disconnect_task);
419 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
420 &disconnect_mesh_peers,
428 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n");
431 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
432 "Leaf client %li got a message.\n",
439 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: (%d/%d)\n", ok, ok_goal);
440 data = (uint32_t *) &message[1];
441 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " payload: (%u)\n", ntohl (*data));
442 if (SPEED == test && GNUNET_YES == test_backwards)
444 expected_target_client = 0L;
448 expected_target_client = 4L;
451 if (GNUNET_NO == initialized)
453 initialized = GNUNET_YES;
454 start_time = GNUNET_TIME_absolute_get ();
457 GNUNET_assert (4L == client);
458 GNUNET_SCHEDULER_add_now (&data_task, NULL);
463 if (client == expected_target_client) // Normally 4
466 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
467 " received data %u\n", data_received);
468 if (SPEED != test || (ok_goal - 2) == ok)
470 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
471 GNUNET_TIME_UNIT_FOREVER_REL,
472 size_payload, &tmt_rdy, (void *) 1L);
477 if (data_received < TOTAL_PACKETS)
483 if (test == SPEED_ACK || test == SPEED)
486 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
487 " received ack %u\n", data_ack);
488 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
489 GNUNET_TIME_UNIT_FOREVER_REL,
490 size_payload, &tmt_rdy, (void *) 1L);
491 if (data_ack < TOTAL_PACKETS && SPEED != test)
493 if (ok == 2 && SPEED == test)
497 if (test == P2P_SIGNAL)
499 GNUNET_MESH_channel_destroy (incoming_ch);
504 GNUNET_MESH_channel_destroy (ch);
509 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
511 GNUNET_SCHEDULER_cancel (disconnect_task);
512 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
513 &disconnect_mesh_peers,
522 * Handlers, for diverse services
524 static struct GNUNET_MESH_MessageHandler handlers[] = {
525 {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
531 * Method called whenever another peer has added us to a channel
532 * the other peer initiated.
534 * @param cls Closure.
535 * @param channel New handle to the channel.
536 * @param initiator Peer that started the channel.
537 * @param port Port this channel is connected to.
538 * @param options channel option flags
539 * @return Initial channel context for the channel
540 * (can be NULL -- that's not an error).
543 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
544 const struct GNUNET_PeerIdentity *initiator,
545 uint32_t port, enum GNUNET_MESH_ChannelOption options)
547 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
548 "Incoming channel from %s to peer %d\n",
549 GNUNET_i2s (initiator), (long) cls);
551 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
552 if ((long) cls == 4L)
553 incoming_ch = channel;
556 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
557 "Incoming channel for unknown client %lu\n", (long) cls);
560 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
562 GNUNET_SCHEDULER_cancel (disconnect_task);
563 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
564 &disconnect_mesh_peers,
572 * Function called whenever an inbound channel is destroyed. Should clean up
573 * any associated state.
575 * @param cls closure (set from GNUNET_MESH_connect)
576 * @param channel connection to the other end (henceforth invalid)
577 * @param channel_ctx place where local state associated
578 * with the channel is stored
581 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
586 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
587 "Incoming channel disconnected at peer %d\n",
592 GNUNET_break (channel == incoming_ch);
597 if (P2P_SIGNAL == test)
601 GNUNET_break (channel == ch);
605 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
606 "Unknown peer! %d\n", i);
607 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
609 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
611 GNUNET_SCHEDULER_cancel (disconnect_task);
612 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
621 * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
623 * Testcase continues when the root receives confirmation of connected peers,
624 * on callback funtion ch.
626 * @param cls Closure (unsued).
627 * @param tc Task Context.
630 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
632 enum GNUNET_MESH_ChannelOption flags;
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\n");
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add peer 2\n");
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638 "schedule timeout in TIMEOUT\n");
639 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
641 GNUNET_SCHEDULER_cancel (disconnect_task);
644 flags = GNUNET_MESH_OPTION_DEFAULT;
645 if (SPEED_REL == test)
648 flags |= GNUNET_MESH_OPTION_RELIABLE;
650 ch = GNUNET_MESH_channel_create (h1, NULL, p_id[1], 1, flags);
652 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
653 &disconnect_mesh_peers,
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656 "Sending data initializer...\n");
660 GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
661 GNUNET_TIME_UNIT_FOREVER_REL,
662 size_payload, &tmt_rdy, (void *) 1L);
666 * Callback to be called when the requested peer information is available
668 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
669 * @param op the operation this callback corresponds to
670 * @param pinfo the result; will be NULL if the operation has failed
671 * @param emsg error message if the operation has failed;
672 * NULL if the operation is successfull
676 struct GNUNET_TESTBED_Operation *op,
677 const struct GNUNET_TESTBED_PeerInformation *pinfo,
682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id callback for %ld\n", i);
684 if (NULL == pinfo || NULL != emsg)
686 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
687 abort_test (__LINE__);
690 p_id[i] = pinfo->result.id;
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id: %s\n", GNUNET_i2s (p_id[i]));
695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n");
696 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
701 * test main: start test when all peers are connected
703 * @param cls Closure.
704 * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
705 * @param num_peers Number of peers that are running.
706 * @param peers Array of peers.
707 * @param meshes Handle to each of the MESHs of the peers.
711 struct GNUNET_MESH_TEST_Context *ctx,
712 unsigned int num_peers,
713 struct GNUNET_TESTBED_Peer **peers,
714 struct GNUNET_MESH_Handle **meshes)
716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
719 peers_running = num_peers;
721 h2 = meshes[num_peers - 1];
722 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
723 &disconnect_mesh_peers,
725 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
726 &shutdown_task, NULL);
727 t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
728 GNUNET_TESTBED_PIT_IDENTITY,
729 &pi_cb, (void *) 0L);
730 t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
731 GNUNET_TESTBED_PIT_IDENTITY,
732 &pi_cb, (void *) 1L);
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
741 main (int argc, char *argv[])
743 initialized = GNUNET_NO;
745 const char *config_file;
747 GNUNET_log_setup ("test", "DEBUG", NULL);
748 config_file = "test_mesh.conf";
750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
751 if (strstr (argv[0], "_small_forward") != NULL)
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
755 test_name = "unicast";
758 else if (strstr (argv[0], "_small_signal") != NULL)
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
762 test_name = "signal";
765 else if (strstr (argv[0], "_small_speed_ack") != NULL)
767 /* Each peer is supposed to generate the following callbacks:
768 * 1 incoming channel (@dest)
769 * TOTAL_PACKETS received data packet (@dest)
770 * TOTAL_PACKETS received data packet (@orig)
771 * 1 received channel destroy (@dest)
772 * _________________________________
773 * 5 x ok expected per peer
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
777 test_name = "speed ack";
778 ok_goal = TOTAL_PACKETS * 2 + 2;
780 else if (strstr (argv[0], "_small_speed") != NULL)
782 /* Each peer is supposed to generate the following callbacks:
783 * 1 incoming channel (@dest)
784 * 1 initial packet (@dest)
785 * TOTAL_PACKETS received data packet (@dest)
786 * 1 received data packet (@orig)
787 * 1 received channel destroy (@dest)
788 * _________________________________
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
791 ok_goal = TOTAL_PACKETS + 4;
792 if (strstr (argv[0], "_reliable") != NULL)
795 test_name = "speed reliable";
796 config_file = "test_mesh_drop.conf";
806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
811 if (strstr (argv[0], "backwards") != NULL)
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
816 test_backwards = GNUNET_YES;
817 aux = GNUNET_malloc (32);
818 sprintf (aux, "backwards %s", test_name);
825 GNUNET_MESH_TEST_run ("test_mesh_small",
829 NULL, /* tmain cls */
837 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
838 "FAILED! (%d/%d)\n", ok, ok_goal);
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
845 /* end of test_mesh_small.c */