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);
189 if (GNUNET_YES == started)
193 GNUNET_MESH_notify_transmit_ready (t, GNUNET_NO,
194 GNUNET_TIME_UNIT_FOREVER_REL,
195 &peer_id, msize, &tmt_rdy, cls);
197 m->size = htons (msize);
199 msg->data = htonl (*sent - 1);
205 * Function is called whenever a message is received.
207 * @param cls closure (set from GNUNET_MESH_connect)
208 * @param tunnel connection to the other end
209 * @param tunnel_ctx place to store local state associated with the tunnel
210 * @param sender who sent the message
211 * @param message the actual message
212 * @param atsi performance data for the connection
213 * @return GNUNET_OK to keep the connection open,
214 * GNUNET_SYSERR to close it (signal serious error)
217 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
218 const struct GNUNET_PeerIdentity *sender,
219 const struct GNUNET_MessageHeader *message,
220 const struct GNUNET_ATS_Information *atsi)
222 struct test_traffic_message *msg;
223 unsigned int *peer_number = cls;
227 if (GNUNET_NO == started)
229 GNUNET_break (2 == *peer_number);
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got initial data packet\n");
231 started = GNUNET_YES;
232 start_time = GNUNET_TIME_absolute_get();
233 if (FWD != test) // Send leaf -> root
234 GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
235 GNUNET_TIME_UNIT_FOREVER_REL,
237 sizeof (struct test_traffic_message),
239 if (BCK != test) // Send root -> leaf
240 GNUNET_MESH_notify_transmit_ready (t, GNUNET_NO,
241 GNUNET_TIME_UNIT_FOREVER_REL,
243 sizeof (struct test_traffic_message),
248 if (*peer_number == 1)
251 target = to_send_bck;
253 else if (*peer_number == 2)
256 target = to_send_fwd;
262 msg = (struct test_traffic_message *) message;
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got data packet # %u [%u]\n",
264 ntohl (msg->data), got + 1);
268 if (to_send_bck == sent_bck && to_send_fwd == sent_fwd) {
269 end_time = GNUNET_TIME_absolute_get();
275 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
276 GNUNET_SCHEDULER_cancel (shutdown_task);
278 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &do_shutdown,
285 * Method called whenever another peer has added us to a tunnel
286 * the other peer initiated.
289 * @param tunnel new handle to the tunnel
290 * @param initiator peer that started the tunnel
291 * @param atsi performance information for the tunnel
292 * @return initial tunnel context for the tunnel (can be NULL -- that's not an error)
295 inbound_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
296 const struct GNUNET_PeerIdentity *initiator,
297 const struct GNUNET_ATS_Information *atsi)
299 unsigned int id = *(unsigned int *) cls;
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received incoming tunnel %p\n", tunnel);
304 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
305 "received incoming tunnel on peer 1\n");
306 result = GNUNET_SYSERR;
313 * Function called whenever an inbound tunnel is destroyed. Should clean up
314 * any associated state.
316 * @param cls closure (set from GNUNET_MESH_connect)
317 * @param tunnel connection to the other end (henceforth invalid)
318 * @param tunnel_ctx place where local state associated
319 * with the tunnel is stored
322 inbound_end (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
325 unsigned int id = *(unsigned int *) cls;
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "incoming tunnel closed\n");
330 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
331 "received closing tunnel on peer 1\n");
332 result = GNUNET_SYSERR;
338 * Method called whenever a peer has connected to the tunnel.
340 * @param cls Closure.
341 * @param peer Peer identity of connected peer.
344 peer_connected (void *cls, const struct GNUNET_PeerIdentity *peer,
345 const struct GNUNET_ATS_Information *atsi)
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer connected\n");
349 /* Force an inbound tunnel notification on peer 2 */
350 GNUNET_MESH_notify_transmit_ready (t, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL,
351 peer, sizeof (struct test_traffic_message),
357 * Method called whenever a peer has connected to the tunnel.
360 * @param peer peer identity the tunnel was created to, NULL on timeout
361 * @param atsi performance data for the connection
364 peer_disconnected (void *cls, const struct GNUNET_PeerIdentity *peer)
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer disconnected\n");
371 * Handler array for traffic received on peer1
373 static struct GNUNET_MESH_MessageHandler handlers[] = {
374 {&data_callback, 1, sizeof (struct test_traffic_message)},
380 * Handler array for traffic received on peer2 (none expected)
382 static struct GNUNET_MESH_MessageHandler handlers_null[] = { {NULL, 0, 0} };
386 * Initialize framework and start test
390 const struct GNUNET_CONFIGURATION_Handle *cfg,
391 struct GNUNET_TESTING_Peer *peer)
393 static const GNUNET_MESH_ApplicationType app1[] = { 0 };
394 static const GNUNET_MESH_ApplicationType app2[] = { 1, 0 };
397 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
398 (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
400 mesh_peer_1 = GNUNET_MESH_connect (cfg, /* configuration */
401 (void *) &one, /* cls */
402 NULL, /* inbound new hndlr */
403 NULL, /* inbound end hndlr */
404 /* traffic handlers */
405 test == FWD ? handlers_null : handlers,
406 app1); /* apps offered */
408 mesh_peer_2 = GNUNET_MESH_connect (cfg, /* configuration */
409 (void *) &two, /* cls */
410 &inbound_tunnel, /* inbound new hndlr */
411 &inbound_end, /* inbound end hndlr */
412 handlers, /* traffic handlers */
413 app2); /* apps offered */
414 if (NULL == mesh_peer_1 || NULL == mesh_peer_2)
416 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to mesh\n");
417 result = GNUNET_SYSERR;
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to mesh\n");
424 t = GNUNET_MESH_tunnel_create (mesh_peer_1, NULL, &peer_connected,
425 &peer_disconnected, (void *) &two);
426 GNUNET_MESH_peer_request_connect_by_type (t, 1);
434 main (int argc, char *argv[])
436 if (strstr (argv[0], "test_mesh_local_traffic_fwd") != NULL)
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FWD\n");
440 to_send_fwd = TARGET;
442 else if (strstr (argv[0], "test_mesh_local_traffic_bck") != NULL)
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BCK\n");
446 to_send_bck = TARGET;
448 else if (strstr (argv[0], "test_mesh_local_traffic_both") != NULL)
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BOTH\n");
452 to_send_bck = to_send_fwd = TARGET;
459 if (0 != GNUNET_TESTING_peer_run ("test-mesh-local-traffic",
463 if (result != GNUNET_OK)
465 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
466 "Failed.\nFWD expected: %u, Sent: %u, Got: %u\n",
467 to_send_fwd, sent_fwd, got_fwd);
468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
469 "BCK expected: %u, Sent: %u, Got: %u\n",
470 to_send_bck, sent_bck, got_bck);
475 struct GNUNET_TIME_Relative total_time;
476 unsigned int total_traffic;
479 total_traffic = BOTH == test ? 2 * TARGET : TARGET;
483 name = "Local traffic Root to Leaf";
486 name = "Local traffic Leaf to Root";
489 name = "Local traffic bidirectional";
495 total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
496 FPRINTF (stderr, "\nTest time %llu ms\n",
497 (unsigned long long) total_time.rel_value);
498 FPRINTF (stderr, "Test payload bandwidth: %f kb/s\n",
499 4 * 1000.0 / total_time.rel_value); // 4bytes * ms
500 FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
501 total_traffic * 1000.0 / total_time.rel_value); // 1000 packets * ms
504 total_traffic * 1000.0 / total_time.rel_value,
510 /* end of test_mesh_local_traffic.c */