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, 20)
48 * DIFFERENT TESTS TO RUN
59 * Which test are we running?
64 * String with test name
69 * Flag to send traffic leaf->root in speed tests to test BCK_ACK logic.
71 static int test_backwards = GNUNET_NO;
74 * How many events have happened
79 * Each peer is supposed to generate the following callbacks:
80 * 1 incoming tunnel (@dest)
81 * 1 connected peer (@orig)
82 * 1 received data packet (@dest)
83 * 1 received data packet (@orig)
84 * 1 received tunnel destroy (@dest)
85 * _________________________________
86 * 5 x ok expected per peer
92 * Size of each test packet
94 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
97 * Operation to get peer ids.
99 struct GNUNET_TESTBED_Operation *t_op[2];
104 struct GNUNET_PeerIdentity *p_id[2];
112 * Is the setup initialized?
114 static int initialized;
117 * Peers that have responded
119 static int peers_responded;
122 * Number of payload packes sent
124 static int data_sent;
127 * Number of payload packets received
129 static int data_received;
132 * Number of payload packed explicitly (app level) acknowledged
137 * Total number of currently running peers.
139 static unsigned long long peers_running;
142 * Test context (to shut down).
144 struct GNUNET_MESH_TEST_Context *test_ctx;
147 * Task called to disconnect peers.
149 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
152 * Task To perform tests
154 static GNUNET_SCHEDULER_TaskIdentifier test_task;
157 * Task called to shutdown test.
159 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
162 * Mesh handle for the root peer
164 static struct GNUNET_MESH_Handle *h1;
167 * Mesh handle for the first leaf peer
169 static struct GNUNET_MESH_Handle *h2;
172 * Tunnel handle for the root peer
174 static struct GNUNET_MESH_Tunnel *t;
177 * Tunnel handle for the first leaf peer
179 static struct GNUNET_MESH_Tunnel *incoming_t;
182 * Time we started the data transmission (after tunnel has been established
185 static struct GNUNET_TIME_Absolute start_time;
189 * Show the results of the test (banwidth acheived) and log them to GAUGER
194 static struct GNUNET_TIME_Absolute end_time;
195 static struct GNUNET_TIME_Relative total_time;
197 end_time = GNUNET_TIME_absolute_get();
198 total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
199 FPRINTF (stderr, "\nResults of test \"%s\"\n", test_name);
200 FPRINTF (stderr, "Test time %llu ms\n",
201 (unsigned long long) total_time.rel_value);
202 FPRINTF (stderr, "Test bandwidth: %f kb/s\n",
203 4 * TOTAL_PACKETS * 1.0 / total_time.rel_value); // 4bytes * ms
204 FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
205 TOTAL_PACKETS * 1000.0 / total_time.rel_value); // packets * ms
206 GAUGER ("MESH", test_name,
207 TOTAL_PACKETS * 1000.0 / total_time.rel_value,
213 * Shut down peergroup, clean up.
215 * @param cls Closure (unused).
216 * @param tc Task Context.
219 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
222 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
227 * Disconnect from mesh services af all peers, call shutdown.
229 * @param cls Closure (unused).
230 * @param tc Task Context.
233 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
235 long line = (long) cls;
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239 "disconnecting mesh service of peers, called from line %ld\n",
241 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
242 for (i = 0; i < 2; i++)
244 GNUNET_TESTBED_operation_done (t_op[i]);
248 GNUNET_MESH_tunnel_destroy (t);
251 if (NULL != incoming_t)
253 GNUNET_MESH_tunnel_destroy (incoming_t);
256 GNUNET_MESH_TEST_cleanup (test_ctx);
257 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
259 GNUNET_SCHEDULER_cancel (shutdown_handle);
261 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
266 * Abort test: schedule disconnect and shutdown immediately
268 * @param line Line in the code the abort is requested from (__LINE__).
271 abort_test (long line)
273 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
275 GNUNET_SCHEDULER_cancel (disconnect_task);
276 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
282 * Transmit ready callback.
284 * @param cls Closure (message type).
285 * @param size Size of the tranmist buffer.
286 * @param buf Pointer to the beginning of the buffer.
288 * @return Number of bytes written to buf.
291 tmt_rdy (void *cls, size_t size, void *buf);
295 * Task to schedule a new data transmission.
297 * @param cls Closure (peer #).
298 * @param tc Task Context.
301 data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
303 struct GNUNET_MESH_TransmitHandle *th;
304 struct GNUNET_MESH_Tunnel *tunnel;
306 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data task\n");
310 if (GNUNET_YES == test_backwards)
318 th = GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
319 GNUNET_TIME_UNIT_FOREVER_REL,
320 size_payload, &tmt_rdy, (void *) 1L);
323 unsigned long i = (unsigned long) cls;
325 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Retransmission\n");
328 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " in 1 ms\n");
329 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
330 &data_task, (void *)1UL);
335 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "in %u ms\n", i);
336 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
337 GNUNET_TIME_UNIT_MILLISECONDS,
339 &data_task, (void *)i);
346 * Transmit ready callback
348 * @param cls Closure (message type).
349 * @param size Size of the buffer we have.
350 * @param buf Buffer to copy data to.
353 tmt_rdy (void *cls, size_t size, void *buf)
355 struct GNUNET_MessageHeader *msg = buf;
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 " tmt_rdy called\n");
360 if (size < size_payload || NULL == buf)
363 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
364 "size %u, buf %p, data_sent %u, data_received %u\n",
371 msg->size = htons (size);
372 msg->type = htons ((long) cls);
373 data = (uint32_t *) &msg[1];
374 *data = htonl (data_sent);
375 if (SPEED == test && GNUNET_YES == initialized)
378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
379 " Sent packet %d\n", data_sent);
380 if (data_sent < TOTAL_PACKETS)
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 " Scheduling packet %d\n", data_sent + 1);
384 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 tunnel connection to the other end
396 * @param tunnel_ctx place to store local state associated with the tunnel
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_Tunnel *tunnel, void **tunnel_ctx,
403 const struct GNUNET_MessageHeader *message)
405 long client = (long) cls;
406 long expected_target_client;
411 GNUNET_MESH_receive_done (tunnel);
415 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
417 GNUNET_SCHEDULER_cancel (disconnect_task);
418 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
419 &disconnect_mesh_peers,
427 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",
440 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: (%d/%d)\n", ok, ok_goal);
441 data = (uint32_t *) &message[1];
442 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " payload: (%u)\n", ntohl (*data));
443 if (SPEED == test && GNUNET_YES == test_backwards)
445 expected_target_client = 0L;
449 expected_target_client = 4L;
452 if (GNUNET_NO == initialized)
454 initialized = GNUNET_YES;
455 start_time = GNUNET_TIME_absolute_get ();
458 GNUNET_assert (4L == client);
459 GNUNET_SCHEDULER_add_now (&data_task, NULL);
464 if (client == expected_target_client) // Normally 3 or 4
467 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
468 " received data %u\n", data_received);
469 if (SPEED != test || (ok_goal - 2) == ok)
471 GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
472 GNUNET_TIME_UNIT_FOREVER_REL,
473 size_payload, &tmt_rdy, (void *) 1L);
478 if (data_received < TOTAL_PACKETS)
484 if (test == SPEED_ACK || test == SPEED)
487 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
488 " received ack %u\n", data_ack);
489 GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
490 GNUNET_TIME_UNIT_FOREVER_REL,
491 size_payload, &tmt_rdy, (void *) 1L);
492 if (data_ack < TOTAL_PACKETS && SPEED != test)
494 if (ok == 2 && SPEED == test)
498 if (test == P2P_SIGNAL)
500 GNUNET_MESH_tunnel_destroy (incoming_t);
505 GNUNET_MESH_tunnel_destroy (t);
510 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
512 GNUNET_SCHEDULER_cancel (disconnect_task);
513 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
514 &disconnect_mesh_peers,
523 * Handlers, for diverse services
525 static struct GNUNET_MESH_MessageHandler handlers[] = {
526 {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
532 * Method called whenever another peer has added us to a tunnel
533 * the other peer initiated.
535 * @param cls Closure.
536 * @param tunnel New handle to the tunnel.
537 * @param initiator Peer that started the tunnel.
538 * @param port Port this tunnels is connected to.
539 * @return Initial tunnel context for the tunnel
540 * (can be NULL -- that's not an error).
543 incoming_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
544 const struct GNUNET_PeerIdentity *initiator,
547 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
548 "Incoming tunnel 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)
556 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
557 "Incoming tunnel 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 tunnel is destroyed. Should clean up
573 * any associated state.
575 * @param cls closure (set from GNUNET_MESH_connect)
576 * @param tunnel connection to the other end (henceforth invalid)
577 * @param tunnel_ctx place where local state associated
578 * with the tunnel is stored
581 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
586 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
587 "Incoming tunnel disconnected at peer %d\n",
594 else if (0L == i && P2P_SIGNAL == test)
599 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
600 "Unknown peer! %d\n", i);
601 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
603 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
605 GNUNET_SCHEDULER_cancel (disconnect_task);
606 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
615 * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
617 * Testcase continues when the root receives confirmation of connected peers,
618 * on callback funtion ch.
620 * @param cls Closure (unsued).
621 * @param tc Task Context.
624 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\n");
630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add peer 2\n");
632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
633 "schedule timeout in TIMEOUT\n");
634 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
636 GNUNET_SCHEDULER_cancel (disconnect_task);
638 if (SPEED_NOBUF == test)
646 if (SPEED_REL == test)
653 t = GNUNET_MESH_tunnel_create (h1, NULL, p_id[1], 1, buf, rel);
655 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
656 &disconnect_mesh_peers,
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
659 "Sending data initializer...\n");
664 GNUNET_MESH_notify_transmit_ready (t, GNUNET_NO,
665 GNUNET_TIME_UNIT_FOREVER_REL,
666 size_payload, &tmt_rdy, (void *) 1L);
670 * Callback to be called when the requested peer information is available
672 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
673 * @param op the operation this callback corresponds to
674 * @param pinfo the result; will be NULL if the operation has failed
675 * @param emsg error message if the operation has failed;
676 * NULL if the operation is successfull
680 struct GNUNET_TESTBED_Operation *op,
681 const struct GNUNET_TESTBED_PeerInformation *pinfo,
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id callback for %ld\n", i);
688 if (NULL == pinfo || NULL != emsg)
690 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
691 abort_test (__LINE__);
694 p_id[i] = pinfo->result.id;
695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id: %s\n", GNUNET_i2s (p_id[i]));
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n");
700 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
705 * test main: start test when all peers are connected
707 * @param cls Closure.
708 * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
709 * @param num_peers Number of peers that are running.
710 * @param peers Array of peers.
711 * @param meshes Handle to each of the MESHs of the peers.
715 struct GNUNET_MESH_TEST_Context *ctx,
716 unsigned int num_peers,
717 struct GNUNET_TESTBED_Peer **peers,
718 struct GNUNET_MESH_Handle **meshes)
720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
723 peers_running = num_peers;
725 h2 = meshes[num_peers - 1];
726 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
727 &disconnect_mesh_peers,
729 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
730 &shutdown_task, NULL);
731 t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
732 GNUNET_TESTBED_PIT_IDENTITY,
733 &pi_cb, (void *) 0L);
734 t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
735 GNUNET_TESTBED_PIT_IDENTITY,
736 &pi_cb, (void *) 1L);
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
745 main (int argc, char *argv[])
747 initialized = GNUNET_NO;
749 const char *config_file;
751 GNUNET_log_setup ("test", "DEBUG", NULL);
752 config_file = "test_mesh.conf";
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
755 if (strstr (argv[0], "_small_forward") != NULL)
757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
759 test_name = "unicast";
762 else if (strstr (argv[0], "_small_signal") != NULL)
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
766 test_name = "signal";
769 else if (strstr (argv[0], "_small_speed_ack") != NULL)
771 /* Each peer is supposed to generate the following callbacks:
772 * 1 incoming tunnel (@dest)
773 * TOTAL_PACKETS received data packet (@dest)
774 * TOTAL_PACKETS received data packet (@orig)
775 * 1 received tunnel destroy (@dest)
776 * _________________________________
777 * 5 x ok expected per peer
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
781 test_name = "speed ack";
782 ok_goal = TOTAL_PACKETS * 2 + 2;
784 else if (strstr (argv[0], "_small_speed") != NULL)
786 /* Each peer is supposed to generate the following callbacks:
787 * 1 incoming tunnel (@dest)
788 * 1 initial packet (@dest)
789 * TOTAL_PACKETS received data packet (@dest)
790 * 1 received data packet (@orig)
791 * 1 received tunnel destroy (@dest)
792 * _________________________________
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
795 ok_goal = TOTAL_PACKETS + 4;
796 if (strstr (argv[0], "_nobuf") != NULL)
799 test_name = "speed nobuf";
801 else if (strstr (argv[0], "_reliable") != NULL)
804 test_name = "speed reliable";
805 config_file = "test_mesh_drop.conf";
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
820 if (strstr (argv[0], "backwards") != NULL)
824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
825 test_backwards = GNUNET_YES;
826 aux = GNUNET_malloc (32);
827 sprintf (aux, "backwards %s", test_name);
834 GNUNET_MESH_TEST_run ("test_mesh_small",
838 NULL, /* tmain cls */
846 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
847 "FAILED! (%d/%d)\n", ok, ok_goal);
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
854 /* end of test_mesh_small.c */