- leaks
[oweals/gnunet.git] / src / mesh / test_mesh_local_traffic.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
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
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_mesh_service.h"
30 #include "gnunet_testing_lib-new.h"
31 #include <gauger.h>
32
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
34
35 #define TARGET 1000
36
37 /**
38  * DIFFERENT TESTS TO RUN
39  */
40 #define FWD 0
41 #define BCK 1
42 #define BOTH 2
43
44
45 GNUNET_NETWORK_STRUCT_BEGIN
46
47 struct test_traffic_message
48 {
49   struct GNUNET_MessageHeader header;
50   uint32_t data GNUNET_PACKED;
51 };
52
53 GNUNET_NETWORK_STRUCT_END
54
55
56 /** Which test to run, based on executable name */
57 static int test;
58
59 static int started;
60
61 /** How many packets to send from root to leaf */
62 static unsigned int to_send_fwd;
63
64 /** How many packets to send from leaf to root */
65 static unsigned int to_send_bck;
66
67 static unsigned int sent_fwd = 0;
68
69 static unsigned int got_fwd = 0;
70
71 static unsigned int sent_bck = 0;
72
73 static unsigned int got_bck = 0;
74
75 static struct GNUNET_MESH_Handle *mesh_peer_1;
76
77 static struct GNUNET_MESH_Handle *mesh_peer_2;
78
79 static struct GNUNET_MESH_Tunnel *t_fwd;
80
81 static struct GNUNET_MESH_Tunnel *t_bck;
82
83 static unsigned int one = 1;
84
85 static unsigned int two = 2;
86
87 static int result = GNUNET_SYSERR;
88
89 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
90
91 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
92
93 static struct GNUNET_TIME_Absolute start_time;
94
95 static struct GNUNET_TIME_Absolute end_time;
96
97 static struct GNUNET_PeerIdentity peer_id;
98
99
100 /**
101  * Shutdown nicely
102  */
103 static void
104 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
105 {
106   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutdown\n");
107   if (0 != abort_task)
108   {
109     GNUNET_SCHEDULER_cancel (abort_task);
110   }
111   if (NULL != t_fwd)
112   {
113     GNUNET_MESH_tunnel_destroy(t_fwd);
114   }
115   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "D1\n");
116   if (NULL != mesh_peer_1)
117   {
118     GNUNET_MESH_disconnect (mesh_peer_1);
119   }
120   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "D2\n");
121   if (NULL != mesh_peer_2)
122   {
123     GNUNET_MESH_disconnect (mesh_peer_2);
124   }
125 }
126
127
128 /**
129  * Something went wrong and timed out. Kill everything and set error flag
130  */
131 static void
132 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
133 {
134   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ABORT\n");
135   result = GNUNET_SYSERR;
136   abort_task = 0;
137   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
138   {
139     GNUNET_SCHEDULER_cancel (shutdown_task);
140     shutdown_task = GNUNET_SCHEDULER_NO_TASK;
141   }
142   do_shutdown (cls, tc);
143 }
144
145 static void
146 finish(void)
147 {
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,
151                                                  &do_shutdown, NULL);
152 }
153
154
155 /**
156  * Transmit ready callback.
157  * 
158  * @param cls Closure (peer number of peer sending the data).
159  * @param size Buffer size.
160  * @param buf Buffer.
161  */
162 static size_t
163 tmt_rdy (void *cls, size_t size, void *buf)
164 {
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);
170   unsigned int *sent;
171   unsigned int target;
172   char *s;
173
174   if (0 == size || NULL == buf)
175     return 0;
176
177   if (1 == peer_number)
178   {
179     sent = &sent_fwd;
180     target = to_send_fwd;
181     t = t_fwd;
182     s = "FWD";
183   }
184   else if (2 == peer_number)
185   {
186     sent = &sent_bck;
187     target = to_send_bck;
188     t = t_bck;
189     s = "BCK";
190   }
191   else
192     GNUNET_abort();
193
194   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending %s data packet # %4u\n",
195               s, *sent);
196   GNUNET_assert (size >= msize);
197   if (GNUNET_YES == started)
198   {
199     (*sent)++;
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);
204     }
205   }
206   m->size = htons (msize);
207   m->type = htons (1);
208   msg->data = htonl (*sent - 1);
209   return msize;
210 }
211
212
213 /**
214  * Function is called whenever a message is received.
215  *
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)
224  */
225 static int
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)
230 {
231   struct test_traffic_message *msg;
232   unsigned int *peer_number = cls;
233   unsigned int *got;
234   unsigned int target;
235
236   if (GNUNET_NO == started)
237   {
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     {
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,
247                                         NULL,
248                                         sizeof (struct test_traffic_message),
249                                         &tmt_rdy, &two);
250     }
251     if (BCK != test) // Send root -> leaf
252     {
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,
256                                         &peer_id,
257                                         sizeof (struct test_traffic_message),
258                                         &tmt_rdy, &one);
259     }
260     return GNUNET_OK;
261   }
262
263   if (*peer_number == 1)
264   {
265     got = &got_bck;
266     target = to_send_bck;
267   }
268   else if (*peer_number == 2)
269   {
270     got = &got_fwd;
271     target = to_send_fwd;
272   }
273   else
274   {
275     GNUNET_abort();
276   }
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);
280   (*got)++;
281   if (target == *got)
282   {
283     if (to_send_bck == sent_bck && to_send_fwd == sent_fwd) {
284       end_time = GNUNET_TIME_absolute_get();
285       result = GNUNET_OK;
286       finish();
287     }
288     return GNUNET_OK;
289   }
290   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
291     GNUNET_SCHEDULER_cancel (shutdown_task);
292   shutdown_task =
293     GNUNET_SCHEDULER_add_delayed (TIMEOUT, &do_shutdown,
294                                   NULL);
295   return GNUNET_OK;
296 }
297
298
299 /**
300  * Method called whenever another peer has added us to a tunnel
301  * the other peer initiated.
302  *
303  * @param cls closure
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)
308  */
309 static void *
310 inbound_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
311                 const struct GNUNET_PeerIdentity *initiator,
312                 const struct GNUNET_ATS_Information *atsi)
313 {
314   unsigned int id = *(unsigned int *) cls;
315
316   t_bck = tunnel;
317   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received incoming tunnel %p\n", tunnel);
318   if (id != 2)
319   {
320     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
321                 "received incoming tunnel on peer 1\n");
322     result = GNUNET_SYSERR;
323   }
324   return NULL;
325 }
326
327
328 /**
329  * Function called whenever an inbound tunnel is destroyed.  Should clean up
330  * any associated state.
331  *
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
336  */
337 static void
338 inbound_end (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
339              void *tunnel_ctx)
340 {
341   unsigned int id = *(unsigned int *) cls;
342
343   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "incoming tunnel closed\n");
344   if (id != 2)
345   {
346     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
347                 "received closing tunnel on peer 1\n");
348     result = GNUNET_SYSERR;
349   }
350 }
351
352
353 /**
354  * Method called whenever a peer has connected to the tunnel.
355  *
356  * @param cls Closure.
357  * @param peer Peer identity of connected peer.
358  */
359 static void
360 peer_connected (void *cls, const struct GNUNET_PeerIdentity *peer,
361                const struct GNUNET_ATS_Information *atsi)
362 {
363   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer connected\n");
364   peer_id = *peer;
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),
368                                      &tmt_rdy, &one);
369 }
370
371
372 /**
373  * Method called whenever a peer has connected to the tunnel.
374  *
375  * @param cls closure
376  * @param peer peer identity the tunnel was created to, NULL on timeout
377  * @param atsi performance data for the connection
378  */
379 static void
380 peer_disconnected (void *cls, const struct GNUNET_PeerIdentity *peer)
381 {
382   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer disconnected\n");
383 }
384
385
386 /**
387  * Handler array for traffic received on peer1
388  */
389 static struct GNUNET_MESH_MessageHandler handlers[] = {
390   {&data_callback, 1, sizeof (struct test_traffic_message)},
391   {NULL, 0, 0}
392 };
393
394
395 /**
396  * Handler array for traffic received on peer2 (none expected)
397  */
398 static struct GNUNET_MESH_MessageHandler handlers_null[] = { {NULL, 0, 0} };
399
400
401 /**
402  * Initialize framework and start test
403  */
404 static void
405 run (void *cls, 
406      const struct GNUNET_CONFIGURATION_Handle *cfg,
407      struct GNUNET_TESTING_Peer *peer)
408 {
409   static const GNUNET_MESH_ApplicationType app1[] = { 0 };
410   static const GNUNET_MESH_ApplicationType app2[] = { 1, 0 };
411
412   abort_task =
413       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
414                                     (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
415                                     NULL);
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 */
423
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)
431   {
432     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to mesh\n");
433     result = GNUNET_SYSERR;
434     return;
435   }
436   else
437   {
438     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to mesh\n");
439   }
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);
443 }
444
445
446 /**
447  * Main
448  */
449 int
450 main (int argc, char *argv[])
451 {
452   if (strstr (argv[0], "test_mesh_local_traffic_fwd") != NULL)
453   {
454     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FWD\n");
455     test = FWD;
456     to_send_fwd = TARGET;
457   }
458   else if (strstr (argv[0], "test_mesh_local_traffic_bck") != NULL)
459   {
460     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BCK\n");
461     test = BCK;
462     to_send_bck = TARGET;
463   }
464   else if (strstr (argv[0], "test_mesh_local_traffic_both") != NULL)
465   {
466     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BOTH\n");
467     test = BOTH;
468     to_send_bck = to_send_fwd = TARGET;
469   }
470   else
471   {
472     return 1;
473   }
474
475   if (0 != GNUNET_TESTING_peer_run ("test-mesh-local-traffic",
476                                     "test_mesh.conf",
477                                     &run, NULL))
478     return 1;
479   if (result != GNUNET_OK)
480   {
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);
487     return 1;
488   }
489   else
490   {
491     struct GNUNET_TIME_Relative total_time;
492     unsigned int total_traffic;
493     char *name;
494
495     total_traffic = BOTH == test ? 2 * TARGET : TARGET;
496     switch (test)
497     {
498       case FWD:
499         name = "Local traffic Root to Leaf";
500         break;
501       case BCK:
502         name = "Local traffic Leaf to Root";
503         break;
504       case BOTH:
505         name = "Local traffic bidirectional";
506         break;
507       default:
508         GNUNET_assert (0);
509     }
510
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
518     GAUGER ("MESH",
519             name,
520             total_traffic * 1000.0 / total_time.rel_value,
521             "packets/s");
522   }
523   return 0;
524 }
525
526 /* end of test_mesh_local_traffic.c */