- use explicit tunnel
[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       GNUNET_MESH_notify_transmit_ready (t_bck, GNUNET_NO,
244                                         GNUNET_TIME_UNIT_FOREVER_REL,
245                                         NULL,
246                                         sizeof (struct test_traffic_message),
247                                         &tmt_rdy, &two);
248     if (BCK != test) // Send root -> leaf
249       GNUNET_MESH_notify_transmit_ready (t_fwd, GNUNET_NO,
250                                         GNUNET_TIME_UNIT_FOREVER_REL,
251                                         &peer_id,
252                                         sizeof (struct test_traffic_message),
253                                         &tmt_rdy, &one);
254     return GNUNET_OK;
255   }
256
257   if (*peer_number == 1)
258   {
259     got = &got_bck;
260     target = to_send_bck;
261   }
262   else if (*peer_number == 2)
263   {
264     got = &got_fwd;
265     target = to_send_fwd;
266   }
267   else
268   {
269     GNUNET_abort();
270   }
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);
274   (*got)++;
275   if (target == *got)
276   {
277     if (to_send_bck == sent_bck && to_send_fwd == sent_fwd) {
278       end_time = GNUNET_TIME_absolute_get();
279       result = GNUNET_OK;
280       finish();
281     }
282     return GNUNET_OK;
283   }
284   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
285     GNUNET_SCHEDULER_cancel (shutdown_task);
286   shutdown_task =
287     GNUNET_SCHEDULER_add_delayed (TIMEOUT, &do_shutdown,
288                                   NULL);
289   return GNUNET_OK;
290 }
291
292
293 /**
294  * Method called whenever another peer has added us to a tunnel
295  * the other peer initiated.
296  *
297  * @param cls closure
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)
302  */
303 static void *
304 inbound_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
305                 const struct GNUNET_PeerIdentity *initiator,
306                 const struct GNUNET_ATS_Information *atsi)
307 {
308   unsigned int id = *(unsigned int *) cls;
309
310   t_bck = tunnel;
311   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received incoming tunnel %p\n", tunnel);
312   if (id != 2)
313   {
314     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
315                 "received incoming tunnel on peer 1\n");
316     result = GNUNET_SYSERR;
317   }
318   return NULL;
319 }
320
321
322 /**
323  * Function called whenever an inbound tunnel is destroyed.  Should clean up
324  * any associated state.
325  *
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
330  */
331 static void
332 inbound_end (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
333              void *tunnel_ctx)
334 {
335   unsigned int id = *(unsigned int *) cls;
336
337   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "incoming tunnel closed\n");
338   if (id != 2)
339   {
340     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
341                 "received closing tunnel on peer 1\n");
342     result = GNUNET_SYSERR;
343   }
344 }
345
346
347 /**
348  * Method called whenever a peer has connected to the tunnel.
349  *
350  * @param cls Closure.
351  * @param peer Peer identity of connected peer.
352  */
353 static void
354 peer_connected (void *cls, const struct GNUNET_PeerIdentity *peer,
355                const struct GNUNET_ATS_Information *atsi)
356 {
357   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer connected\n");
358   peer_id = *peer;
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),
362                                      &tmt_rdy, &one);
363 }
364
365
366 /**
367  * Method called whenever a peer has connected to the tunnel.
368  *
369  * @param cls closure
370  * @param peer peer identity the tunnel was created to, NULL on timeout
371  * @param atsi performance data for the connection
372  */
373 static void
374 peer_disconnected (void *cls, const struct GNUNET_PeerIdentity *peer)
375 {
376   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer disconnected\n");
377 }
378
379
380 /**
381  * Handler array for traffic received on peer1
382  */
383 static struct GNUNET_MESH_MessageHandler handlers[] = {
384   {&data_callback, 1, sizeof (struct test_traffic_message)},
385   {NULL, 0, 0}
386 };
387
388
389 /**
390  * Handler array for traffic received on peer2 (none expected)
391  */
392 static struct GNUNET_MESH_MessageHandler handlers_null[] = { {NULL, 0, 0} };
393
394
395 /**
396  * Initialize framework and start test
397  */
398 static void
399 run (void *cls, 
400      const struct GNUNET_CONFIGURATION_Handle *cfg,
401      struct GNUNET_TESTING_Peer *peer)
402 {
403   static const GNUNET_MESH_ApplicationType app1[] = { 0 };
404   static const GNUNET_MESH_ApplicationType app2[] = { 1, 0 };
405
406   abort_task =
407       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
408                                     (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
409                                     NULL);
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 */
417
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)
425   {
426     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to mesh\n");
427     result = GNUNET_SYSERR;
428     return;
429   }
430   else
431   {
432     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to mesh\n");
433   }
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);
437 }
438
439
440 /**
441  * Main
442  */
443 int
444 main (int argc, char *argv[])
445 {
446   if (strstr (argv[0], "test_mesh_local_traffic_fwd") != NULL)
447   {
448     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FWD\n");
449     test = FWD;
450     to_send_fwd = TARGET;
451   }
452   else if (strstr (argv[0], "test_mesh_local_traffic_bck") != NULL)
453   {
454     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BCK\n");
455     test = BCK;
456     to_send_bck = TARGET;
457   }
458   else if (strstr (argv[0], "test_mesh_local_traffic_both") != NULL)
459   {
460     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BOTH\n");
461     test = BOTH;
462     to_send_bck = to_send_fwd = TARGET;
463   }
464   else
465   {
466     return 1;
467   }
468
469   if (0 != GNUNET_TESTING_peer_run ("test-mesh-local-traffic",
470                                     "test_mesh.conf",
471                                     &run, NULL))
472     return 1;
473   if (result != GNUNET_OK)
474   {
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);
481     return 1;
482   }
483   else
484   {
485     struct GNUNET_TIME_Relative total_time;
486     unsigned int total_traffic;
487     char *name;
488
489     total_traffic = BOTH == test ? 2 * TARGET : TARGET;
490     switch (test)
491     {
492       case FWD:
493         name = "Local traffic Root to Leaf";
494         break;
495       case BCK:
496         name = "Local traffic Leaf to Root";
497         break;
498       case BOTH:
499         name = "Local traffic bidirectional";
500         break;
501       default:
502         GNUNET_assert (0);
503     }
504
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
512     GAUGER ("MESH",
513             name,
514             total_traffic * 1000.0 / total_time.rel_value,
515             "packets/s");
516   }
517   return 0;
518 }
519
520 /* end of test_mesh_local_traffic.c */