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;
81 static unsigned int one = 1;
83 static unsigned int two = 2;
85 static int result = GNUNET_SYSERR;
87 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
89 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
91 static struct GNUNET_TIME_Absolute start_time;
93 static struct GNUNET_TIME_Absolute end_time;
95 static struct GNUNET_PeerIdentity peer_id;
102 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutdown\n");
107 GNUNET_SCHEDULER_cancel (abort_task);
111 GNUNET_MESH_tunnel_destroy(t);
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "D1\n");
114 if (NULL != mesh_peer_1)
116 GNUNET_MESH_disconnect (mesh_peer_1);
118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "D2\n");
119 if (NULL != mesh_peer_2)
121 GNUNET_MESH_disconnect (mesh_peer_2);
127 * Something went wrong and timed out. Kill everything and set error flag
130 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
132 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ABORT\n");
133 result = GNUNET_SYSERR;
135 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
137 GNUNET_SCHEDULER_cancel (shutdown_task);
138 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
140 do_shutdown (cls, tc);
146 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
147 GNUNET_SCHEDULER_cancel(shutdown_task);
148 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
154 * Transmit ready callback.
156 * @param cls Closure (peer number of peer sending the data).
157 * @param size Buffer size.
161 tmt_rdy (void *cls, size_t size, void *buf)
163 unsigned int peer_number = *(unsigned int *) cls;
164 struct GNUNET_MessageHeader *m = buf;
165 struct test_traffic_message *msg = buf;
166 size_t msize = sizeof (struct test_traffic_message);
170 if (0 == size || NULL == buf)
173 if (1 == peer_number)
176 target = to_send_fwd;
178 else if (2 == peer_number)
181 target = to_send_bck;
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending data packet # %u\n",
188 GNUNET_assert (size >= msize);
191 GNUNET_MESH_notify_transmit_ready (t, GNUNET_NO,
192 GNUNET_TIME_UNIT_FOREVER_REL,
193 &peer_id, msize, &tmt_rdy, cls);
194 m->size = htons (msize);
196 msg->data = htonl (*sent - 1);
202 * Function is called whenever a message is received.
204 * @param cls closure (set from GNUNET_MESH_connect)
205 * @param tunnel connection to the other end
206 * @param tunnel_ctx place to store local state associated with the tunnel
207 * @param sender who sent the message
208 * @param message the actual message
209 * @param atsi performance data for the connection
210 * @return GNUNET_OK to keep the connection open,
211 * GNUNET_SYSERR to close it (signal serious error)
214 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
215 const struct GNUNET_PeerIdentity *sender,
216 const struct GNUNET_MessageHeader *message,
217 const struct GNUNET_ATS_Information *atsi)
219 struct test_traffic_message *msg;
220 unsigned int *peer_number = cls;
224 if (GNUNET_NO == started)
226 GNUNET_break (2 == *peer_number);
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got initial data packet\n");
228 started = GNUNET_YES;
232 if (*peer_number == 1)
235 target = to_send_bck;
237 else if (*peer_number == 2)
240 target = to_send_fwd;
246 msg = (struct test_traffic_message *) message;
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got data packet # %u [%u]\n",
248 ntohl (msg->data), got + 1);
252 if (to_send_bck == sent_bck && to_send_fwd == sent_fwd) {
253 end_time = GNUNET_TIME_absolute_get();
259 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
260 GNUNET_SCHEDULER_cancel (shutdown_task);
262 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &do_shutdown,
269 * Method called whenever another peer has added us to a tunnel
270 * the other peer initiated.
273 * @param tunnel new handle to the tunnel
274 * @param initiator peer that started the tunnel
275 * @param atsi performance information for the tunnel
276 * @return initial tunnel context for the tunnel (can be NULL -- that's not an error)
279 inbound_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
280 const struct GNUNET_PeerIdentity *initiator,
281 const struct GNUNET_ATS_Information *atsi)
283 unsigned int id = *(unsigned int *) cls;
285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received incoming tunnel\n");
288 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
289 "received incoming tunnel on peer 1\n");
290 result = GNUNET_SYSERR;
292 start_time = GNUNET_TIME_absolute_get();
293 if (FWD != test) // Send leaf -> root
294 GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
295 GNUNET_TIME_UNIT_FOREVER_REL,
297 sizeof (struct test_traffic_message),
299 if (BCK != test) // Send root -> leaf
300 GNUNET_MESH_notify_transmit_ready (t, GNUNET_NO,
301 GNUNET_TIME_UNIT_FOREVER_REL,
303 sizeof (struct test_traffic_message),
310 * Function called whenever an inbound tunnel is destroyed. Should clean up
311 * any associated state.
313 * @param cls closure (set from GNUNET_MESH_connect)
314 * @param tunnel connection to the other end (henceforth invalid)
315 * @param tunnel_ctx place where local state associated
316 * with the tunnel is stored
319 inbound_end (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
322 unsigned int id = *(unsigned int *) cls;
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "incoming tunnel closed\n");
327 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
328 "received closing tunnel on peer 2\n");
329 result = GNUNET_SYSERR;
335 * Method called whenever a peer has connected to the tunnel.
337 * @param cls Closure.
338 * @param peer Peer identity of connected peer.
341 peer_connected (void *cls, const struct GNUNET_PeerIdentity *peer,
342 const struct GNUNET_ATS_Information *atsi)
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer connected\n");
346 /* Force an inbound tunnel notification on peer 2 */
347 GNUNET_MESH_notify_transmit_ready (t, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL,
348 peer, sizeof (struct test_traffic_message),
354 * Method called whenever a peer has connected to the tunnel.
357 * @param peer peer identity the tunnel was created to, NULL on timeout
358 * @param atsi performance data for the connection
361 peer_disconnected (void *cls, const struct GNUNET_PeerIdentity *peer)
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer disconnected\n");
368 * Handler array for traffic received on peer1
370 static struct GNUNET_MESH_MessageHandler handlers[] = {
371 {&data_callback, 1, sizeof (struct test_traffic_message)},
377 * Handler array for traffic received on peer2 (none expected)
379 static struct GNUNET_MESH_MessageHandler handlers_null[] = { {NULL, 0, 0} };
383 * Initialize framework and start test
387 const struct GNUNET_CONFIGURATION_Handle *cfg,
388 struct GNUNET_TESTING_Peer *peer)
390 static const GNUNET_MESH_ApplicationType app1[] = { 0 };
391 static const GNUNET_MESH_ApplicationType app2[] = { 1, 0 };
394 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
395 (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
397 mesh_peer_1 = GNUNET_MESH_connect (cfg, /* configuration */
398 (void *) &one, /* cls */
399 NULL, /* inbound new hndlr */
400 NULL, /* inbound end hndlr */
401 /* traffic handlers */
402 test == FWD ? handlers_null : handlers,
403 app1); /* apps offered */
405 mesh_peer_2 = GNUNET_MESH_connect (cfg, /* configuration */
406 (void *) &two, /* cls */
407 &inbound_tunnel, /* inbound new hndlr */
408 &inbound_end, /* inbound end hndlr */
409 handlers, /* traffic handlers */
410 app2); /* apps offered */
411 if (NULL == mesh_peer_1 || NULL == mesh_peer_2)
413 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to mesh\n");
414 result = GNUNET_SYSERR;
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to mesh\n");
421 t = GNUNET_MESH_tunnel_create (mesh_peer_1, NULL, &peer_connected,
422 &peer_disconnected, (void *) &two);
423 GNUNET_MESH_peer_request_connect_by_type (t, 1);
431 main (int argc, char *argv[])
433 if (strstr (argv[0], "test_mesh_local_traffic_fwd") != NULL)
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FWD\n");
437 to_send_fwd = TARGET;
439 else if (strstr (argv[0], "test_mesh_local_traffic_bck") != NULL)
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BCK\n");
443 to_send_bck = TARGET;
445 else if (strstr (argv[0], "test_mesh_local_traffic_both") != NULL)
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BOTH\n");
449 to_send_bck = to_send_fwd = TARGET;
456 if (0 != GNUNET_TESTING_peer_run ("test-mesh-local-traffic",
460 if (result != GNUNET_OK)
462 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
463 "Failed.\nFWD expected: %u, Sent: %u, Got: %u\n",
464 to_send_fwd, sent_fwd, got_fwd);
465 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
466 "BCK expected: %u, Sent: %u, Got: %u\n",
467 to_send_bck, sent_bck, got_bck);
472 struct GNUNET_TIME_Relative total_time;
473 unsigned int total_traffic;
476 total_traffic = BOTH == test ? 2 * TARGET : TARGET;
480 name = "Local traffic Root to Leaf";
483 name = "Local traffic Leaf to Root";
486 name = "Local traffic bidirectional";
492 total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
493 FPRINTF (stderr, "\nTest time %llu ms\n",
494 (unsigned long long) total_time.rel_value);
495 FPRINTF (stderr, "Test payload bandwidth: %f kb/s\n",
496 4 * 1000.0 / total_time.rel_value); // 4bytes * ms
497 FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
498 total_traffic * 1000.0 / total_time.rel_value); // 1000 packets * ms
501 total_traffic * 1000.0 / total_time.rel_value,
507 /* end of test_mesh_local_traffic.c */