2 This file is part of GNUnet.
3 (C) 2012 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 mesh/test_mesh_local_traffic.c
23 * @brief test mesh local traffic: test of tunnels with just one peer
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
29 #include "gnunet_mesh_service.h"
30 #include "gnunet_testing_lib.h"
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
38 * DIFFERENT TESTS TO RUN
45 GNUNET_NETWORK_STRUCT_BEGIN
47 struct test_traffic_message
49 struct GNUNET_MessageHeader header;
50 uint32_t data GNUNET_PACKED;
53 GNUNET_NETWORK_STRUCT_END
56 /** Which test to run, based on executable name */
61 /** How many packets to send from root to leaf */
62 static unsigned int to_send_fwd;
64 /** How many packets to send from leaf to root */
65 static unsigned int to_send_bck;
67 static unsigned int sent_fwd = 0;
69 static unsigned int got_fwd = 0;
71 static unsigned int sent_bck = 0;
73 static unsigned int got_bck = 0;
75 static struct GNUNET_MESH_Handle *mesh_peer_1;
77 static struct GNUNET_MESH_Handle *mesh_peer_2;
79 static struct GNUNET_MESH_Tunnel *t_fwd;
81 static struct GNUNET_MESH_Tunnel *t_bck;
83 static unsigned int one = 1;
85 static unsigned int two = 2;
87 static int result = GNUNET_SYSERR;
89 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
91 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
93 static struct GNUNET_TIME_Absolute start_time;
95 static struct GNUNET_TIME_Absolute end_time;
97 static struct GNUNET_PeerIdentity peer_id;
104 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutdown\n");
109 GNUNET_SCHEDULER_cancel (abort_task);
113 GNUNET_MESH_tunnel_destroy(t_fwd);
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "D1\n");
116 if (NULL != mesh_peer_1)
118 GNUNET_MESH_disconnect (mesh_peer_1);
120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "D2\n");
121 if (NULL != mesh_peer_2)
123 GNUNET_MESH_disconnect (mesh_peer_2);
129 * Something went wrong and timed out. Kill everything and set error flag
132 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
134 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ABORT\n");
135 result = GNUNET_SYSERR;
137 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
139 GNUNET_SCHEDULER_cancel (shutdown_task);
140 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
142 do_shutdown (cls, tc);
148 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
149 GNUNET_SCHEDULER_cancel(shutdown_task);
150 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
156 * Transmit ready callback.
158 * @param cls Closure (peer number of peer sending the data).
159 * @param size Buffer size.
163 tmt_rdy (void *cls, size_t size, void *buf)
165 unsigned int peer_number = *(unsigned int *) cls;
166 struct GNUNET_MessageHeader *m = buf;
167 struct GNUNET_MESH_Tunnel *t;
168 struct test_traffic_message *msg = buf;
169 size_t msize = sizeof (struct test_traffic_message);
174 if (0 == size || NULL == buf)
177 if (1 == peer_number)
180 target = to_send_fwd;
184 else if (2 == peer_number)
187 target = to_send_bck;
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending %s data packet # %4u\n",
196 GNUNET_assert (size >= msize);
197 if (GNUNET_YES == started)
200 if (target > *sent) {
201 GNUNET_MESH_notify_transmit_ready (t, GNUNET_NO,
202 GNUNET_TIME_UNIT_FOREVER_REL,
203 &peer_id, msize, &tmt_rdy, cls);
206 m->size = htons (msize);
208 msg->data = htonl (*sent - 1);
214 * Function is called whenever a message is received.
216 * @param cls closure (set from GNUNET_MESH_connect)
217 * @param tunnel connection to the other end
218 * @param tunnel_ctx place to store local state associated with the tunnel
219 * @param sender who sent the message
220 * @param message the actual message
221 * @param atsi performance data for the connection
222 * @return GNUNET_OK to keep the connection open,
223 * GNUNET_SYSERR to close it (signal serious error)
226 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
227 const struct GNUNET_PeerIdentity *sender,
228 const struct GNUNET_MessageHeader *message,
229 const struct GNUNET_ATS_Information *atsi)
231 struct test_traffic_message *msg;
232 unsigned int *peer_number = cls;
236 if (GNUNET_NO == started)
238 GNUNET_break (2 == *peer_number);
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got initial data packet\n");
240 started = GNUNET_YES;
241 start_time = GNUNET_TIME_absolute_get();
242 if (FWD != test) // Send leaf -> root
244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending first BCK data\n");
245 GNUNET_MESH_notify_transmit_ready (t_bck, GNUNET_NO,
246 GNUNET_TIME_UNIT_FOREVER_REL,
248 sizeof (struct test_traffic_message),
251 if (BCK != test) // Send root -> leaf
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending first FWD data\n");
254 GNUNET_MESH_notify_transmit_ready (t_fwd, GNUNET_NO,
255 GNUNET_TIME_UNIT_FOREVER_REL,
257 sizeof (struct test_traffic_message),
263 if (*peer_number == 1)
266 target = to_send_bck;
268 else if (*peer_number == 2)
271 target = to_send_fwd;
277 msg = (struct test_traffic_message *) message;
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got data packet # %u [%u]\n",
279 ntohl (msg->data), *got + 1);
283 if (to_send_bck == sent_bck && to_send_fwd == sent_fwd) {
284 end_time = GNUNET_TIME_absolute_get();
290 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
291 GNUNET_SCHEDULER_cancel (shutdown_task);
293 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &do_shutdown,
300 * Method called whenever another peer has added us to a tunnel
301 * the other peer initiated.
304 * @param tunnel new handle to the tunnel
305 * @param initiator peer that started the tunnel
306 * @param atsi performance information for the tunnel
307 * @return initial tunnel context for the tunnel (can be NULL -- that's not an error)
310 inbound_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
311 const struct GNUNET_PeerIdentity *initiator,
312 const struct GNUNET_ATS_Information *atsi)
314 unsigned int id = *(unsigned int *) cls;
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received incoming tunnel %p\n", tunnel);
320 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
321 "received incoming tunnel on peer 1\n");
322 result = GNUNET_SYSERR;
329 * Function called whenever an inbound tunnel is destroyed. Should clean up
330 * any associated state.
332 * @param cls closure (set from GNUNET_MESH_connect)
333 * @param tunnel connection to the other end (henceforth invalid)
334 * @param tunnel_ctx place where local state associated
335 * with the tunnel is stored
338 inbound_end (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
341 unsigned int id = *(unsigned int *) cls;
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "incoming tunnel closed\n");
346 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
347 "received closing tunnel on peer 1\n");
348 result = GNUNET_SYSERR;
354 * Method called whenever a peer has connected to the tunnel.
356 * @param cls Closure.
357 * @param peer Peer identity of connected peer.
360 peer_connected (void *cls, const struct GNUNET_PeerIdentity *peer,
361 const struct GNUNET_ATS_Information *atsi)
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer connected\n");
365 /* Force an inbound tunnel notification on peer 2 */
366 GNUNET_MESH_notify_transmit_ready (t_fwd, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL,
367 peer, sizeof (struct test_traffic_message),
373 * Method called whenever a peer has connected to the tunnel.
376 * @param peer peer identity the tunnel was created to, NULL on timeout
377 * @param atsi performance data for the connection
380 peer_disconnected (void *cls, const struct GNUNET_PeerIdentity *peer)
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer disconnected\n");
387 * Handler array for traffic received on peer1
389 static struct GNUNET_MESH_MessageHandler handlers[] = {
390 {&data_callback, 1, sizeof (struct test_traffic_message)},
396 * Handler array for traffic received on peer2 (none expected)
398 static struct GNUNET_MESH_MessageHandler handlers_null[] = { {NULL, 0, 0} };
402 * Initialize framework and start test
406 const struct GNUNET_CONFIGURATION_Handle *cfg,
407 struct GNUNET_TESTING_Peer *peer)
409 static const GNUNET_MESH_ApplicationType app1[] = { 0 };
410 static const GNUNET_MESH_ApplicationType app2[] = { 1, 0 };
413 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
414 (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
416 mesh_peer_1 = GNUNET_MESH_connect (cfg, /* configuration */
417 (void *) &one, /* cls */
418 NULL, /* inbound new hndlr */
419 NULL, /* inbound end hndlr */
420 /* traffic handlers */
421 test == FWD ? handlers_null : handlers,
422 app1); /* apps offered */
424 mesh_peer_2 = GNUNET_MESH_connect (cfg, /* configuration */
425 (void *) &two, /* cls */
426 &inbound_tunnel, /* inbound new hndlr */
427 &inbound_end, /* inbound end hndlr */
428 handlers, /* traffic handlers */
429 app2); /* apps offered */
430 if (NULL == mesh_peer_1 || NULL == mesh_peer_2)
432 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to mesh\n");
433 result = GNUNET_SYSERR;
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to mesh\n");
440 t_fwd = GNUNET_MESH_tunnel_create (mesh_peer_1, NULL, &peer_connected,
441 &peer_disconnected, (void *) &two);
442 GNUNET_MESH_peer_request_connect_by_type (t_fwd, 1);
450 main (int argc, char *argv[])
452 if (strstr (argv[0], "test_mesh_local_traffic_fwd") != NULL)
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FWD\n");
456 to_send_fwd = TARGET;
458 else if (strstr (argv[0], "test_mesh_local_traffic_bck") != NULL)
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BCK\n");
462 to_send_bck = TARGET;
464 else if (strstr (argv[0], "test_mesh_local_traffic_both") != NULL)
466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BOTH\n");
468 to_send_bck = to_send_fwd = TARGET;
475 if (0 != GNUNET_TESTING_peer_run ("test-mesh-local-traffic",
479 if (result != GNUNET_OK)
481 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
482 "Failed.\nFWD expected: %u, Sent: %u, Got: %u\n",
483 to_send_fwd, sent_fwd, got_fwd);
484 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
485 "BCK expected: %u, Sent: %u, Got: %u\n",
486 to_send_bck, sent_bck, got_bck);
491 struct GNUNET_TIME_Relative total_time;
492 unsigned int total_traffic;
495 total_traffic = BOTH == test ? 2 * TARGET : TARGET;
499 name = "Local traffic Root to Leaf";
502 name = "Local traffic Leaf to Root";
505 name = "Local traffic bidirectional";
511 total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
512 FPRINTF (stderr, "\nTest time %llu ms\n",
513 (unsigned long long) total_time.rel_value);
514 FPRINTF (stderr, "Test payload bandwidth: %f kb/s\n",
515 total_traffic * 4.0 / total_time.rel_value); // 4bytes * kb/ms
516 FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
517 total_traffic * 1000.0 / total_time.rel_value); // 1000 packets * ms
520 total_traffic * 1000.0 / total_time.rel_value,
526 /* end of test_mesh_local_traffic.c */