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-new.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
243 GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
244 GNUNET_TIME_UNIT_FOREVER_REL,
246 sizeof (struct test_traffic_message),
248 if (BCK != test) // Send root -> leaf
249 GNUNET_MESH_notify_transmit_ready (t_fwd, GNUNET_NO,
250 GNUNET_TIME_UNIT_FOREVER_REL,
252 sizeof (struct test_traffic_message),
257 if (*peer_number == 1)
260 target = to_send_bck;
262 else if (*peer_number == 2)
265 target = to_send_fwd;
271 msg = (struct test_traffic_message *) message;
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got data packet # %u [%u]\n",
273 ntohl (msg->data), got + 1);
277 if (to_send_bck == sent_bck && to_send_fwd == sent_fwd) {
278 end_time = GNUNET_TIME_absolute_get();
284 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
285 GNUNET_SCHEDULER_cancel (shutdown_task);
287 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &do_shutdown,
294 * Method called whenever another peer has added us to a tunnel
295 * the other peer initiated.
298 * @param tunnel new handle to the tunnel
299 * @param initiator peer that started the tunnel
300 * @param atsi performance information for the tunnel
301 * @return initial tunnel context for the tunnel (can be NULL -- that's not an error)
304 inbound_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
305 const struct GNUNET_PeerIdentity *initiator,
306 const struct GNUNET_ATS_Information *atsi)
308 unsigned int id = *(unsigned int *) cls;
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received incoming tunnel %p\n", tunnel);
314 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
315 "received incoming tunnel on peer 1\n");
316 result = GNUNET_SYSERR;
323 * Function called whenever an inbound tunnel is destroyed. Should clean up
324 * any associated state.
326 * @param cls closure (set from GNUNET_MESH_connect)
327 * @param tunnel connection to the other end (henceforth invalid)
328 * @param tunnel_ctx place where local state associated
329 * with the tunnel is stored
332 inbound_end (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
335 unsigned int id = *(unsigned int *) cls;
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "incoming tunnel closed\n");
340 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
341 "received closing tunnel on peer 1\n");
342 result = GNUNET_SYSERR;
348 * Method called whenever a peer has connected to the tunnel.
350 * @param cls Closure.
351 * @param peer Peer identity of connected peer.
354 peer_connected (void *cls, const struct GNUNET_PeerIdentity *peer,
355 const struct GNUNET_ATS_Information *atsi)
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer connected\n");
359 /* Force an inbound tunnel notification on peer 2 */
360 GNUNET_MESH_notify_transmit_ready (t_fwd, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL,
361 peer, sizeof (struct test_traffic_message),
367 * Method called whenever a peer has connected to the tunnel.
370 * @param peer peer identity the tunnel was created to, NULL on timeout
371 * @param atsi performance data for the connection
374 peer_disconnected (void *cls, const struct GNUNET_PeerIdentity *peer)
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer disconnected\n");
381 * Handler array for traffic received on peer1
383 static struct GNUNET_MESH_MessageHandler handlers[] = {
384 {&data_callback, 1, sizeof (struct test_traffic_message)},
390 * Handler array for traffic received on peer2 (none expected)
392 static struct GNUNET_MESH_MessageHandler handlers_null[] = { {NULL, 0, 0} };
396 * Initialize framework and start test
400 const struct GNUNET_CONFIGURATION_Handle *cfg,
401 struct GNUNET_TESTING_Peer *peer)
403 static const GNUNET_MESH_ApplicationType app1[] = { 0 };
404 static const GNUNET_MESH_ApplicationType app2[] = { 1, 0 };
407 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
408 (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
410 mesh_peer_1 = GNUNET_MESH_connect (cfg, /* configuration */
411 (void *) &one, /* cls */
412 NULL, /* inbound new hndlr */
413 NULL, /* inbound end hndlr */
414 /* traffic handlers */
415 test == FWD ? handlers_null : handlers,
416 app1); /* apps offered */
418 mesh_peer_2 = GNUNET_MESH_connect (cfg, /* configuration */
419 (void *) &two, /* cls */
420 &inbound_tunnel, /* inbound new hndlr */
421 &inbound_end, /* inbound end hndlr */
422 handlers, /* traffic handlers */
423 app2); /* apps offered */
424 if (NULL == mesh_peer_1 || NULL == mesh_peer_2)
426 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to mesh\n");
427 result = GNUNET_SYSERR;
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to mesh\n");
434 t_fwd = GNUNET_MESH_tunnel_create (mesh_peer_1, NULL, &peer_connected,
435 &peer_disconnected, (void *) &two);
436 GNUNET_MESH_peer_request_connect_by_type (t_fwd, 1);
444 main (int argc, char *argv[])
446 if (strstr (argv[0], "test_mesh_local_traffic_fwd") != NULL)
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FWD\n");
450 to_send_fwd = TARGET;
452 else if (strstr (argv[0], "test_mesh_local_traffic_bck") != NULL)
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BCK\n");
456 to_send_bck = TARGET;
458 else if (strstr (argv[0], "test_mesh_local_traffic_both") != NULL)
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BOTH\n");
462 to_send_bck = to_send_fwd = TARGET;
469 if (0 != GNUNET_TESTING_peer_run ("test-mesh-local-traffic",
473 if (result != GNUNET_OK)
475 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
476 "Failed.\nFWD expected: %u, Sent: %u, Got: %u\n",
477 to_send_fwd, sent_fwd, got_fwd);
478 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
479 "BCK expected: %u, Sent: %u, Got: %u\n",
480 to_send_bck, sent_bck, got_bck);
485 struct GNUNET_TIME_Relative total_time;
486 unsigned int total_traffic;
489 total_traffic = BOTH == test ? 2 * TARGET : TARGET;
493 name = "Local traffic Root to Leaf";
496 name = "Local traffic Leaf to Root";
499 name = "Local traffic bidirectional";
505 total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
506 FPRINTF (stderr, "\nTest time %llu ms\n",
507 (unsigned long long) total_time.rel_value);
508 FPRINTF (stderr, "Test payload bandwidth: %f kb/s\n",
509 4 * 1000.0 / total_time.rel_value); // 4bytes * ms
510 FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
511 total_traffic * 1000.0 / total_time.rel_value); // 1000 packets * ms
514 total_traffic * 1000.0 / total_time.rel_value,
520 /* end of test_mesh_local_traffic.c */