diff options
author | Bart Polot <bart@net.in.tum.de> | 2014-04-09 11:48:18 +0000 |
---|---|---|
committer | Bart Polot <bart@net.in.tum.de> | 2014-04-09 11:48:18 +0000 |
commit | 145a584218dd0bdeaaca8587ee3e30ff092cc33b (patch) | |
tree | b10eef54be0422c346c62f5d3f12eb42e5aa5eac /src/mesh/test_mesh.c | |
parent | becf26dcd39fd0945a438cadd3da539e00caf135 (diff) | |
download | gnunet-145a584218dd0bdeaaca8587ee3e30ff092cc33b.tar.gz gnunet-145a584218dd0bdeaaca8587ee3e30ff092cc33b.zip |
- rename test file
Diffstat (limited to 'src/mesh/test_mesh.c')
-rw-r--r-- | src/mesh/test_mesh.c | 938 |
1 files changed, 938 insertions, 0 deletions
diff --git a/src/mesh/test_mesh.c b/src/mesh/test_mesh.c new file mode 100644 index 000000000..aec95aacb --- /dev/null +++ b/src/mesh/test_mesh.c | |||
@@ -0,0 +1,938 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011 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 | * @file mesh/test_mesh_small.c | ||
22 | * | ||
23 | * @brief Test for the mesh service: retransmission of traffic. | ||
24 | */ | ||
25 | #include <stdio.h> | ||
26 | #include "platform.h" | ||
27 | #include "mesh_test_lib.h" | ||
28 | #include "gnunet_mesh_service.h" | ||
29 | #include "gnunet_statistics_service.h" | ||
30 | #include <gauger.h> | ||
31 | |||
32 | |||
33 | /** | ||
34 | * How namy messages to send | ||
35 | */ | ||
36 | #define TOTAL_PACKETS 1000 | ||
37 | |||
38 | /** | ||
39 | * How long until we give up on connecting the peers? | ||
40 | */ | ||
41 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120) | ||
42 | |||
43 | /** | ||
44 | * Time to wait for stuff that should be rather fast | ||
45 | */ | ||
46 | #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60) | ||
47 | |||
48 | /** | ||
49 | * DIFFERENT TESTS TO RUN | ||
50 | */ | ||
51 | #define SETUP 0 | ||
52 | #define FORWARD 1 | ||
53 | #define KEEPALIVE 2 | ||
54 | #define SPEED 3 | ||
55 | #define SPEED_ACK 4 | ||
56 | #define SPEED_REL 8 | ||
57 | #define P2P_SIGNAL 10 | ||
58 | |||
59 | /** | ||
60 | * Which test are we running? | ||
61 | */ | ||
62 | static int test; | ||
63 | |||
64 | /** | ||
65 | * String with test name | ||
66 | */ | ||
67 | char *test_name; | ||
68 | |||
69 | /** | ||
70 | * Flag to send traffic leaf->root in speed tests to test BCK_ACK logic. | ||
71 | */ | ||
72 | static int test_backwards = GNUNET_NO; | ||
73 | |||
74 | /** | ||
75 | * How many events have happened | ||
76 | */ | ||
77 | static int ok; | ||
78 | |||
79 | /** | ||
80 | * Number of events expected to conclude the test successfully. | ||
81 | */ | ||
82 | int ok_goal; | ||
83 | |||
84 | /** | ||
85 | * Size of each test packet | ||
86 | */ | ||
87 | size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t); | ||
88 | |||
89 | /** | ||
90 | * Operation to get peer ids. | ||
91 | */ | ||
92 | struct GNUNET_TESTBED_Operation *t_op[2]; | ||
93 | |||
94 | /** | ||
95 | * Peer ids. | ||
96 | */ | ||
97 | struct GNUNET_PeerIdentity *p_id[2]; | ||
98 | |||
99 | /** | ||
100 | * Peer ids counter. | ||
101 | */ | ||
102 | unsigned int p_ids; | ||
103 | |||
104 | /** | ||
105 | * Is the setup initialized? | ||
106 | */ | ||
107 | static int initialized; | ||
108 | |||
109 | /** | ||
110 | * Number of payload packes sent | ||
111 | */ | ||
112 | static int data_sent; | ||
113 | |||
114 | /** | ||
115 | * Number of payload packets received | ||
116 | */ | ||
117 | static int data_received; | ||
118 | |||
119 | /** | ||
120 | * Number of payload packed explicitly (app level) acknowledged | ||
121 | */ | ||
122 | static int data_ack; | ||
123 | |||
124 | /** | ||
125 | * Total number of currently running peers. | ||
126 | */ | ||
127 | static unsigned long long peers_running; | ||
128 | |||
129 | /** | ||
130 | * Test context (to shut down). | ||
131 | */ | ||
132 | struct GNUNET_MESH_TEST_Context *test_ctx; | ||
133 | |||
134 | /** | ||
135 | * Task called to disconnect peers. | ||
136 | */ | ||
137 | static GNUNET_SCHEDULER_TaskIdentifier disconnect_task; | ||
138 | |||
139 | /** | ||
140 | * Task To perform tests | ||
141 | */ | ||
142 | static GNUNET_SCHEDULER_TaskIdentifier test_task; | ||
143 | |||
144 | /** | ||
145 | * Task called to shutdown test. | ||
146 | */ | ||
147 | static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle; | ||
148 | |||
149 | /** | ||
150 | * Mesh handle for the root peer | ||
151 | */ | ||
152 | static struct GNUNET_MESH_Handle *h1; | ||
153 | |||
154 | /** | ||
155 | * Mesh handle for the first leaf peer | ||
156 | */ | ||
157 | static struct GNUNET_MESH_Handle *h2; | ||
158 | |||
159 | /** | ||
160 | * Channel handle for the root peer | ||
161 | */ | ||
162 | static struct GNUNET_MESH_Channel *ch; | ||
163 | |||
164 | /** | ||
165 | * Channel handle for the dest peer | ||
166 | */ | ||
167 | static struct GNUNET_MESH_Channel *incoming_ch; | ||
168 | |||
169 | /** | ||
170 | * Time we started the data transmission (after channel has been established | ||
171 | * and initilized). | ||
172 | */ | ||
173 | static struct GNUNET_TIME_Absolute start_time; | ||
174 | |||
175 | static struct GNUNET_TESTBED_Peer **testbed_peers; | ||
176 | static struct GNUNET_TESTBED_Operation *stats_op; | ||
177 | static unsigned int ka_sent; | ||
178 | static unsigned int ka_received; | ||
179 | |||
180 | |||
181 | /** | ||
182 | * Show the results of the test (banwidth acheived) and log them to GAUGER | ||
183 | */ | ||
184 | static void | ||
185 | show_end_data (void) | ||
186 | { | ||
187 | static struct GNUNET_TIME_Absolute end_time; | ||
188 | static struct GNUNET_TIME_Relative total_time; | ||
189 | |||
190 | end_time = GNUNET_TIME_absolute_get(); | ||
191 | total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time); | ||
192 | FPRINTF (stderr, "\nResults of test \"%s\"\n", test_name); | ||
193 | FPRINTF (stderr, "Test time %s\n", | ||
194 | GNUNET_STRINGS_relative_time_to_string (total_time, | ||
195 | GNUNET_YES)); | ||
196 | FPRINTF (stderr, "Test bandwidth: %f kb/s\n", | ||
197 | 4 * TOTAL_PACKETS * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms | ||
198 | FPRINTF (stderr, "Test throughput: %f packets/s\n\n", | ||
199 | TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms | ||
200 | GAUGER ("MESH", test_name, | ||
201 | TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000), | ||
202 | "packets/s"); | ||
203 | } | ||
204 | |||
205 | |||
206 | /** | ||
207 | * Shut down peergroup, clean up. | ||
208 | * | ||
209 | * @param cls Closure (unused). | ||
210 | * @param tc Task Context. | ||
211 | */ | ||
212 | static void | ||
213 | shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
214 | { | ||
215 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n"); | ||
216 | shutdown_handle = GNUNET_SCHEDULER_NO_TASK; | ||
217 | } | ||
218 | |||
219 | |||
220 | /** | ||
221 | * Disconnect from mesh services af all peers, call shutdown. | ||
222 | * | ||
223 | * @param cls Closure (unused). | ||
224 | * @param tc Task Context. | ||
225 | */ | ||
226 | static void | ||
227 | disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
228 | { | ||
229 | long line = (long) cls; | ||
230 | unsigned int i; | ||
231 | |||
232 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
233 | "disconnecting mesh service of peers, called from line %ld\n", | ||
234 | line); | ||
235 | disconnect_task = GNUNET_SCHEDULER_NO_TASK; | ||
236 | for (i = 0; i < 2; i++) | ||
237 | { | ||
238 | GNUNET_TESTBED_operation_done (t_op[i]); | ||
239 | } | ||
240 | if (NULL != ch) | ||
241 | { | ||
242 | GNUNET_MESH_channel_destroy (ch); | ||
243 | ch = NULL; | ||
244 | } | ||
245 | if (NULL != incoming_ch) | ||
246 | { | ||
247 | GNUNET_MESH_channel_destroy (incoming_ch); | ||
248 | incoming_ch = NULL; | ||
249 | } | ||
250 | GNUNET_MESH_TEST_cleanup (test_ctx); | ||
251 | if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle) | ||
252 | { | ||
253 | GNUNET_SCHEDULER_cancel (shutdown_handle); | ||
254 | } | ||
255 | shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL); | ||
256 | } | ||
257 | |||
258 | |||
259 | /** | ||
260 | * Abort test: schedule disconnect and shutdown immediately | ||
261 | * | ||
262 | * @param line Line in the code the abort is requested from (__LINE__). | ||
263 | */ | ||
264 | static void | ||
265 | abort_test (long line) | ||
266 | { | ||
267 | if (disconnect_task != GNUNET_SCHEDULER_NO_TASK) | ||
268 | { | ||
269 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
270 | disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers, | ||
271 | (void *) line); | ||
272 | } | ||
273 | } | ||
274 | |||
275 | /** | ||
276 | * Transmit ready callback. | ||
277 | * | ||
278 | * @param cls Closure (message type). | ||
279 | * @param size Size of the tranmist buffer. | ||
280 | * @param buf Pointer to the beginning of the buffer. | ||
281 | * | ||
282 | * @return Number of bytes written to buf. | ||
283 | */ | ||
284 | static size_t | ||
285 | tmt_rdy (void *cls, size_t size, void *buf); | ||
286 | |||
287 | |||
288 | /** | ||
289 | * Task to schedule a new data transmission. | ||
290 | * | ||
291 | * @param cls Closure (peer #). | ||
292 | * @param tc Task Context. | ||
293 | */ | ||
294 | static void | ||
295 | data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
296 | { | ||
297 | struct GNUNET_MESH_TransmitHandle *th; | ||
298 | struct GNUNET_MESH_Channel *channel; | ||
299 | |||
300 | if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0) | ||
301 | return; | ||
302 | |||
303 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data task\n"); | ||
304 | if (GNUNET_YES == test_backwards) | ||
305 | { | ||
306 | channel = incoming_ch; | ||
307 | } | ||
308 | else | ||
309 | { | ||
310 | channel = ch; | ||
311 | } | ||
312 | th = GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO, | ||
313 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
314 | size_payload, &tmt_rdy, (void *) 1L); | ||
315 | if (NULL == th) | ||
316 | { | ||
317 | unsigned long i = (unsigned long) cls; | ||
318 | |||
319 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Retransmission\n"); | ||
320 | if (0 == i) | ||
321 | { | ||
322 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, " in 1 ms\n"); | ||
323 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, | ||
324 | &data_task, (void *)1UL); | ||
325 | } | ||
326 | else | ||
327 | { | ||
328 | i++; | ||
329 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "in %u ms\n", i); | ||
330 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply( | ||
331 | GNUNET_TIME_UNIT_MILLISECONDS, | ||
332 | i), | ||
333 | &data_task, (void *)i); | ||
334 | } | ||
335 | } | ||
336 | } | ||
337 | |||
338 | |||
339 | /** | ||
340 | * Transmit ready callback | ||
341 | * | ||
342 | * @param cls Closure (message type). | ||
343 | * @param size Size of the buffer we have. | ||
344 | * @param buf Buffer to copy data to. | ||
345 | */ | ||
346 | size_t | ||
347 | tmt_rdy (void *cls, size_t size, void *buf) | ||
348 | { | ||
349 | struct GNUNET_MessageHeader *msg = buf; | ||
350 | uint32_t *data; | ||
351 | |||
352 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
353 | "tmt_rdy called, filling buffer\n"); | ||
354 | if (size < size_payload || NULL == buf) | ||
355 | { | ||
356 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
357 | "size %u, buf %p, data_sent %u, data_received %u\n", | ||
358 | size, buf, data_sent, data_received); | ||
359 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok %u, ok goal %u\n", ok, ok_goal); | ||
360 | GNUNET_break (ok >= ok_goal - 2); | ||
361 | |||
362 | return 0; | ||
363 | } | ||
364 | msg->size = htons (size); | ||
365 | msg->type = htons ((long) cls); | ||
366 | data = (uint32_t *) &msg[1]; | ||
367 | *data = htonl (data_sent); | ||
368 | if (GNUNET_NO == initialized) | ||
369 | { | ||
370 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
371 | "sending initializer\n"); | ||
372 | } | ||
373 | else if (SPEED == test) | ||
374 | { | ||
375 | data_sent++; | ||
376 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
377 | " Sent packet %d\n", data_sent); | ||
378 | if (data_sent < TOTAL_PACKETS) | ||
379 | { | ||
380 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
381 | " Scheduling packet %d\n", data_sent + 1); | ||
382 | GNUNET_SCHEDULER_add_now (&data_task, NULL); | ||
383 | } | ||
384 | } | ||
385 | |||
386 | return size_payload; | ||
387 | } | ||
388 | |||
389 | |||
390 | /** | ||
391 | * Function is called whenever a message is received. | ||
392 | * | ||
393 | * @param cls closure (set from GNUNET_MESH_connect) | ||
394 | * @param channel connection to the other end | ||
395 | * @param channel_ctx place to store local state associated with the channel | ||
396 | * @param message the actual message | ||
397 | * @return GNUNET_OK to keep the connection open, | ||
398 | * GNUNET_SYSERR to close it (signal serious error) | ||
399 | */ | ||
400 | int | ||
401 | data_callback (void *cls, struct GNUNET_MESH_Channel *channel, | ||
402 | void **channel_ctx, | ||
403 | const struct GNUNET_MessageHeader *message) | ||
404 | { | ||
405 | long client = (long) cls; | ||
406 | long expected_target_client; | ||
407 | uint32_t *data; | ||
408 | |||
409 | ok++; | ||
410 | |||
411 | GNUNET_MESH_receive_done (channel); | ||
412 | |||
413 | if ((ok % 20) == 0) | ||
414 | { | ||
415 | if (GNUNET_SCHEDULER_NO_TASK != disconnect_task) | ||
416 | { | ||
417 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
418 | disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME, | ||
419 | &disconnect_mesh_peers, | ||
420 | (void *) __LINE__); | ||
421 | } | ||
422 | } | ||
423 | |||
424 | switch (client) | ||
425 | { | ||
426 | case 0L: | ||
427 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n"); | ||
428 | break; | ||
429 | case 4L: | ||
430 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
431 | "Leaf client %li got a message.\n", | ||
432 | client); | ||
433 | break; | ||
434 | default: | ||
435 | GNUNET_assert (0); | ||
436 | break; | ||
437 | } | ||
438 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: (%d/%d)\n", ok, ok_goal); | ||
439 | data = (uint32_t *) &message[1]; | ||
440 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, " payload: (%u)\n", ntohl (*data)); | ||
441 | if (SPEED == test && GNUNET_YES == test_backwards) | ||
442 | { | ||
443 | expected_target_client = 0L; | ||
444 | } | ||
445 | else | ||
446 | { | ||
447 | expected_target_client = 4L; | ||
448 | } | ||
449 | |||
450 | if (GNUNET_NO == initialized) | ||
451 | { | ||
452 | initialized = GNUNET_YES; | ||
453 | start_time = GNUNET_TIME_absolute_get (); | ||
454 | if (SPEED == test) | ||
455 | { | ||
456 | GNUNET_assert (4L == client); | ||
457 | GNUNET_SCHEDULER_add_now (&data_task, NULL); | ||
458 | return GNUNET_OK; | ||
459 | } | ||
460 | } | ||
461 | |||
462 | if (client == expected_target_client) // Normally 4 | ||
463 | { | ||
464 | data_received++; | ||
465 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received data %u\n", data_received); | ||
466 | if (SPEED != test || (ok_goal - 2) == ok) | ||
467 | { | ||
468 | GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO, | ||
469 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
470 | size_payload, &tmt_rdy, (void *) 1L); | ||
471 | return GNUNET_OK; | ||
472 | } | ||
473 | else | ||
474 | { | ||
475 | if (data_received < TOTAL_PACKETS) | ||
476 | return GNUNET_OK; | ||
477 | } | ||
478 | } | ||
479 | else // Normally 0 | ||
480 | { | ||
481 | if (test == SPEED_ACK || test == SPEED) | ||
482 | { | ||
483 | data_ack++; | ||
484 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received ack %u\n", data_ack); | ||
485 | GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO, | ||
486 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
487 | size_payload, &tmt_rdy, (void *) 1L); | ||
488 | if (data_ack < TOTAL_PACKETS && SPEED != test) | ||
489 | return GNUNET_OK; | ||
490 | if (ok == 2 && SPEED == test) | ||
491 | return GNUNET_OK; | ||
492 | show_end_data(); | ||
493 | } | ||
494 | if (test == P2P_SIGNAL) | ||
495 | { | ||
496 | GNUNET_MESH_channel_destroy (incoming_ch); | ||
497 | incoming_ch = NULL; | ||
498 | } | ||
499 | else | ||
500 | { | ||
501 | GNUNET_MESH_channel_destroy (ch); | ||
502 | ch = NULL; | ||
503 | } | ||
504 | } | ||
505 | |||
506 | if (GNUNET_SCHEDULER_NO_TASK != disconnect_task) | ||
507 | { | ||
508 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
509 | disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME, | ||
510 | &disconnect_mesh_peers, | ||
511 | (void *) __LINE__); | ||
512 | } | ||
513 | |||
514 | return GNUNET_OK; | ||
515 | } | ||
516 | |||
517 | |||
518 | /** | ||
519 | * Stats callback. Finish the stats testbed operation and when all stats have | ||
520 | * been iterated, shutdown the test. | ||
521 | * | ||
522 | * @param cls closure | ||
523 | * @param op the operation that has been finished | ||
524 | * @param emsg error message in case the operation has failed; will be NULL if | ||
525 | * operation has executed successfully. | ||
526 | */ | ||
527 | static void | ||
528 | stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg) | ||
529 | { | ||
530 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "stats_cont for peer %u\n", cls); | ||
531 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, " sent: %u, received: %u\n", | ||
532 | ka_sent, ka_received); | ||
533 | if (ka_sent < 2 || ka_sent > ka_received + 1) | ||
534 | ok--; | ||
535 | GNUNET_TESTBED_operation_done (stats_op); | ||
536 | |||
537 | if (GNUNET_SCHEDULER_NO_TASK != disconnect_task) | ||
538 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
539 | disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers, | ||
540 | (void *) __LINE__); | ||
541 | |||
542 | } | ||
543 | |||
544 | |||
545 | /** | ||
546 | * Process statistic values. | ||
547 | * | ||
548 | * @param cls closure | ||
549 | * @param peer the peer the statistic belong to | ||
550 | * @param subsystem name of subsystem that created the statistic | ||
551 | * @param name the name of the datum | ||
552 | * @param value the current value | ||
553 | * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not | ||
554 | * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration | ||
555 | */ | ||
556 | static int | ||
557 | stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer, | ||
558 | const char *subsystem, const char *name, | ||
559 | uint64_t value, int is_persistent) | ||
560 | { | ||
561 | static const char *s_sent = "# keepalives sent"; | ||
562 | static const char *s_recv = "# keepalives received"; | ||
563 | uint32_t i; | ||
564 | |||
565 | i = GNUNET_TESTBED_get_index (peer); | ||
566 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u - %s [%s]: %llu\n", | ||
567 | i, subsystem, name, value); | ||
568 | if (0 == strncmp (s_sent, name, strlen (s_sent)) && 0 == i) | ||
569 | ka_sent = value; | ||
570 | |||
571 | if (0 == strncmp(s_recv, name, strlen (s_recv)) && 4 == i) | ||
572 | ka_received = value; | ||
573 | |||
574 | return GNUNET_OK; | ||
575 | } | ||
576 | |||
577 | |||
578 | /** | ||
579 | * Task check that keepalives were sent and received. | ||
580 | * | ||
581 | * @param cls Closure (NULL). | ||
582 | * @param tc Task Context. | ||
583 | */ | ||
584 | static void | ||
585 | check_keepalives (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
586 | { | ||
587 | if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0) | ||
588 | return; | ||
589 | |||
590 | disconnect_task = GNUNET_SCHEDULER_NO_TASK; | ||
591 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "check keepalives\n"); | ||
592 | GNUNET_MESH_channel_destroy (ch); | ||
593 | stats_op = GNUNET_TESTBED_get_statistics (5, testbed_peers, | ||
594 | "mesh", NULL, | ||
595 | stats_iterator, stats_cont, NULL); | ||
596 | } | ||
597 | |||
598 | |||
599 | /** | ||
600 | * Handlers, for diverse services | ||
601 | */ | ||
602 | static struct GNUNET_MESH_MessageHandler handlers[] = { | ||
603 | {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)}, | ||
604 | {NULL, 0, 0} | ||
605 | }; | ||
606 | |||
607 | |||
608 | /** | ||
609 | * Method called whenever another peer has added us to a channel | ||
610 | * the other peer initiated. | ||
611 | * | ||
612 | * @param cls Closure. | ||
613 | * @param channel New handle to the channel. | ||
614 | * @param initiator Peer that started the channel. | ||
615 | * @param port Port this channel is connected to. | ||
616 | * @param options channel option flags | ||
617 | * @return Initial channel context for the channel | ||
618 | * (can be NULL -- that's not an error). | ||
619 | */ | ||
620 | static void * | ||
621 | incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel, | ||
622 | const struct GNUNET_PeerIdentity *initiator, | ||
623 | uint32_t port, enum GNUNET_MESH_ChannelOption options) | ||
624 | { | ||
625 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
626 | "Incoming channel from %s to peer %d\n", | ||
627 | GNUNET_i2s (initiator), (long) cls); | ||
628 | ok++; | ||
629 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok); | ||
630 | if ((long) cls == 4L) | ||
631 | incoming_ch = channel; | ||
632 | else | ||
633 | { | ||
634 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
635 | "Incoming channel for unknown client %lu\n", (long) cls); | ||
636 | GNUNET_break(0); | ||
637 | } | ||
638 | if (GNUNET_SCHEDULER_NO_TASK != disconnect_task) | ||
639 | { | ||
640 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
641 | if (KEEPALIVE == test) | ||
642 | { | ||
643 | struct GNUNET_TIME_Relative delay; | ||
644 | delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS , 5); | ||
645 | disconnect_task = | ||
646 | GNUNET_SCHEDULER_add_delayed (delay, &check_keepalives, NULL); | ||
647 | } | ||
648 | else | ||
649 | disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME, | ||
650 | &disconnect_mesh_peers, | ||
651 | (void *) __LINE__); | ||
652 | } | ||
653 | |||
654 | return NULL; | ||
655 | } | ||
656 | |||
657 | /** | ||
658 | * Function called whenever an inbound channel is destroyed. Should clean up | ||
659 | * any associated state. | ||
660 | * | ||
661 | * @param cls closure (set from GNUNET_MESH_connect) | ||
662 | * @param channel connection to the other end (henceforth invalid) | ||
663 | * @param channel_ctx place where local state associated | ||
664 | * with the channel is stored | ||
665 | */ | ||
666 | static void | ||
667 | channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel, | ||
668 | void *channel_ctx) | ||
669 | { | ||
670 | long i = (long) cls; | ||
671 | |||
672 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
673 | "Incoming channel disconnected at peer %ld\n", i); | ||
674 | if (4L == i) | ||
675 | { | ||
676 | ok++; | ||
677 | GNUNET_break (channel == incoming_ch); | ||
678 | incoming_ch = NULL; | ||
679 | } | ||
680 | else if (0L == i) | ||
681 | { | ||
682 | if (P2P_SIGNAL == test) | ||
683 | { | ||
684 | ok ++; | ||
685 | } | ||
686 | GNUNET_break (channel == ch); | ||
687 | ch = NULL; | ||
688 | } | ||
689 | else | ||
690 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
691 | "Unknown peer! %d\n", i); | ||
692 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok); | ||
693 | |||
694 | if (GNUNET_SCHEDULER_NO_TASK != disconnect_task) | ||
695 | { | ||
696 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
697 | disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers, | ||
698 | (void *) __LINE__); | ||
699 | } | ||
700 | |||
701 | return; | ||
702 | } | ||
703 | |||
704 | |||
705 | /** | ||
706 | * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES. | ||
707 | * | ||
708 | * Testcase continues when the root receives confirmation of connected peers, | ||
709 | * on callback funtion ch. | ||
710 | * | ||
711 | * @param cls Closure (unsued). | ||
712 | * @param tc Task Context. | ||
713 | */ | ||
714 | static void | ||
715 | do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
716 | { | ||
717 | enum GNUNET_MESH_ChannelOption flags; | ||
718 | |||
719 | if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0) | ||
720 | return; | ||
721 | |||
722 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\n"); | ||
723 | |||
724 | if (GNUNET_SCHEDULER_NO_TASK != disconnect_task) | ||
725 | { | ||
726 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
727 | } | ||
728 | |||
729 | flags = GNUNET_MESH_OPTION_DEFAULT; | ||
730 | if (SPEED_REL == test) | ||
731 | { | ||
732 | test = SPEED; | ||
733 | flags |= GNUNET_MESH_OPTION_RELIABLE; | ||
734 | } | ||
735 | ch = GNUNET_MESH_channel_create (h1, NULL, p_id[1], 1, flags); | ||
736 | |||
737 | disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME, | ||
738 | &disconnect_mesh_peers, | ||
739 | (void *) __LINE__); | ||
740 | if (KEEPALIVE == test) | ||
741 | return; /* Don't send any data. */ | ||
742 | |||
743 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
744 | "Sending data initializer...\n"); | ||
745 | data_ack = 0; | ||
746 | data_received = 0; | ||
747 | data_sent = 0; | ||
748 | GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO, | ||
749 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
750 | size_payload, &tmt_rdy, (void *) 1L); | ||
751 | } | ||
752 | |||
753 | /** | ||
754 | * Callback to be called when the requested peer information is available | ||
755 | * | ||
756 | * @param cls the closure from GNUNET_TESTBED_peer_get_information() | ||
757 | * @param op the operation this callback corresponds to | ||
758 | * @param pinfo the result; will be NULL if the operation has failed | ||
759 | * @param emsg error message if the operation has failed; | ||
760 | * NULL if the operation is successfull | ||
761 | */ | ||
762 | static void | ||
763 | pi_cb (void *cls, | ||
764 | struct GNUNET_TESTBED_Operation *op, | ||
765 | const struct GNUNET_TESTBED_PeerInformation *pinfo, | ||
766 | const char *emsg) | ||
767 | { | ||
768 | long i = (long) cls; | ||
769 | |||
770 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id callback for %ld\n", i); | ||
771 | |||
772 | if (NULL == pinfo || NULL != emsg) | ||
773 | { | ||
774 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg); | ||
775 | abort_test (__LINE__); | ||
776 | return; | ||
777 | } | ||
778 | p_id[i] = pinfo->result.id; | ||
779 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id: %s\n", GNUNET_i2s (p_id[i])); | ||
780 | p_ids++; | ||
781 | if (p_ids < 2) | ||
782 | return; | ||
783 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n"); | ||
784 | test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
785 | &do_test, NULL); | ||
786 | } | ||
787 | |||
788 | /** | ||
789 | * test main: start test when all peers are connected | ||
790 | * | ||
791 | * @param cls Closure. | ||
792 | * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end. | ||
793 | * @param num_peers Number of peers that are running. | ||
794 | * @param peers Array of peers. | ||
795 | * @param meshes Handle to each of the MESHs of the peers. | ||
796 | */ | ||
797 | static void | ||
798 | tmain (void *cls, | ||
799 | struct GNUNET_MESH_TEST_Context *ctx, | ||
800 | unsigned int num_peers, | ||
801 | struct GNUNET_TESTBED_Peer **peers, | ||
802 | struct GNUNET_MESH_Handle **meshes) | ||
803 | { | ||
804 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n"); | ||
805 | ok = 0; | ||
806 | test_ctx = ctx; | ||
807 | peers_running = num_peers; | ||
808 | testbed_peers = peers; | ||
809 | h1 = meshes[0]; | ||
810 | h2 = meshes[num_peers - 1]; | ||
811 | disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME, | ||
812 | &disconnect_mesh_peers, | ||
813 | (void *) __LINE__); | ||
814 | shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, | ||
815 | &shutdown_task, NULL); | ||
816 | t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0], | ||
817 | GNUNET_TESTBED_PIT_IDENTITY, | ||
818 | &pi_cb, (void *) 0L); | ||
819 | t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1], | ||
820 | GNUNET_TESTBED_PIT_IDENTITY, | ||
821 | &pi_cb, (void *) 1L); | ||
822 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n"); | ||
823 | } | ||
824 | |||
825 | |||
826 | /** | ||
827 | * Main: start test | ||
828 | */ | ||
829 | int | ||
830 | main (int argc, char *argv[]) | ||
831 | { | ||
832 | initialized = GNUNET_NO; | ||
833 | static uint32_t ports[2]; | ||
834 | const char *config_file; | ||
835 | |||
836 | GNUNET_log_setup ("test", "DEBUG", NULL); | ||
837 | config_file = "test_mesh.conf"; | ||
838 | |||
839 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n"); | ||
840 | if (strstr (argv[0], "_small_forward") != NULL) | ||
841 | { | ||
842 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n"); | ||
843 | test = FORWARD; | ||
844 | test_name = "unicast"; | ||
845 | ok_goal = 4; | ||
846 | } | ||
847 | else if (strstr (argv[0], "_small_signal") != NULL) | ||
848 | { | ||
849 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n"); | ||
850 | test = P2P_SIGNAL; | ||
851 | test_name = "signal"; | ||
852 | ok_goal = 4; | ||
853 | } | ||
854 | else if (strstr (argv[0], "_small_speed_ack") != NULL) | ||
855 | { | ||
856 | /* Test is supposed to generate the following callbacks: | ||
857 | * 1 incoming channel (@dest) | ||
858 | * TOTAL_PACKETS received data packet (@dest) | ||
859 | * TOTAL_PACKETS received data packet (@orig) | ||
860 | * 1 received channel destroy (@dest) | ||
861 | */ | ||
862 | ok_goal = TOTAL_PACKETS * 2 + 2; | ||
863 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n"); | ||
864 | test = SPEED_ACK; | ||
865 | test_name = "speed ack"; | ||
866 | } | ||
867 | else if (strstr (argv[0], "_small_speed") != NULL) | ||
868 | { | ||
869 | /* Test is supposed to generate the following callbacks: | ||
870 | * 1 incoming channel (@dest) | ||
871 | * 1 initial packet (@dest) | ||
872 | * TOTAL_PACKETS received data packet (@dest) | ||
873 | * 1 received data packet (@orig) | ||
874 | * 1 received channel destroy (@dest) | ||
875 | */ | ||
876 | ok_goal = TOTAL_PACKETS + 4; | ||
877 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n"); | ||
878 | if (strstr (argv[0], "_reliable") != NULL) | ||
879 | { | ||
880 | test = SPEED_REL; | ||
881 | test_name = "speed reliable"; | ||
882 | config_file = "test_mesh_drop.conf"; | ||
883 | } | ||
884 | else | ||
885 | { | ||
886 | test = SPEED; | ||
887 | test_name = "speed"; | ||
888 | } | ||
889 | } | ||
890 | else if (strstr (argv[0], "_keepalive") != NULL) | ||
891 | { | ||
892 | test = KEEPALIVE; | ||
893 | /* Test is supposed to generate the following callbacks: | ||
894 | * 1 incoming channel (@dest) | ||
895 | * [wait] | ||
896 | * 1 received channel destroy (@dest) | ||
897 | */ | ||
898 | ok_goal = 2; | ||
899 | } | ||
900 | else | ||
901 | { | ||
902 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n"); | ||
903 | test = SETUP; | ||
904 | ok_goal = 0; | ||
905 | } | ||
906 | |||
907 | if (strstr (argv[0], "backwards") != NULL) | ||
908 | { | ||
909 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n"); | ||
910 | test_backwards = GNUNET_YES; | ||
911 | GNUNET_asprintf (&test_name, "backwards %s", test_name); | ||
912 | } | ||
913 | |||
914 | p_ids = 0; | ||
915 | ports[0] = 1; | ||
916 | ports[1] = 0; | ||
917 | GNUNET_MESH_TEST_run ("test_mesh_small", | ||
918 | config_file, | ||
919 | 5, | ||
920 | &tmain, | ||
921 | NULL, /* tmain cls */ | ||
922 | &incoming_channel, | ||
923 | &channel_cleaner, | ||
924 | handlers, | ||
925 | ports); | ||
926 | |||
927 | if (ok_goal > ok) | ||
928 | { | ||
929 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
930 | "FAILED! (%d/%d)\n", ok, ok_goal); | ||
931 | return 1; | ||
932 | } | ||
933 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n"); | ||
934 | return 0; | ||
935 | } | ||
936 | |||
937 | /* end of test_mesh_small.c */ | ||
938 | |||