diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cadet/Makefile.am | 8 | ||||
-rw-r--r-- | src/cadet/gnunet-cadet-profiler.c | 1157 | ||||
-rw-r--r-- | src/cadet/meson.build | 13 | ||||
-rw-r--r-- | src/cadet/test_cadet.c | 1605 | ||||
-rw-r--r-- | src/cadet/test_cadet_drop.conf | 4 | ||||
-rw-r--r-- | src/cadet/test_cadet_flow.c | 891 |
6 files changed, 14 insertions, 3664 deletions
diff --git a/src/cadet/Makefile.am b/src/cadet/Makefile.am index 9f59aba3c..3f1c79de0 100644 --- a/src/cadet/Makefile.am +++ b/src/cadet/Makefile.am | |||
@@ -75,11 +75,6 @@ gnunet_service_cadet_LDFLAGS = -lrt \ | |||
75 | endif | 75 | endif |
76 | 76 | ||
77 | 77 | ||
78 | #gnunet_cadet_profiler_SOURCES = \ | ||
79 | # gnunet-cadet-profiler.c | ||
80 | #gnunet_cadet_profiler_LDADD = $(ld_cadet_test_lib) | ||
81 | |||
82 | |||
83 | test_cadet_local_mq_SOURCES = \ | 78 | test_cadet_local_mq_SOURCES = \ |
84 | test_cadet_local_mq.c | 79 | test_cadet_local_mq.c |
85 | test_cadet_local_mq_LDADD = \ | 80 | test_cadet_local_mq_LDADD = \ |
@@ -94,5 +89,4 @@ endif | |||
94 | 89 | ||
95 | EXTRA_DIST = \ | 90 | EXTRA_DIST = \ |
96 | cadet.h cadet_protocol.h \ | 91 | cadet.h cadet_protocol.h \ |
97 | test_cadet.conf \ | 92 | test_cadet.conf |
98 | test_cadet_drop.conf | ||
diff --git a/src/cadet/gnunet-cadet-profiler.c b/src/cadet/gnunet-cadet-profiler.c deleted file mode 100644 index 22cbe55dc..000000000 --- a/src/cadet/gnunet-cadet-profiler.c +++ /dev/null | |||
@@ -1,1157 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2011 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file cadet/gnunet-cadet-profiler.c | ||
22 | * | ||
23 | * @brief Profiler for cadet experiments. | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include <stdio.h> | ||
27 | #include "cadet_test_lib.h" | ||
28 | #include "gnunet_cadet_service.h" | ||
29 | #include "gnunet_statistics_service.h" | ||
30 | |||
31 | |||
32 | #define PING 1 | ||
33 | #define PONG 2 | ||
34 | |||
35 | |||
36 | /** | ||
37 | * Paximum ping period in milliseconds. Real period = rand (0, PING_PERIOD) | ||
38 | */ | ||
39 | #define PING_PERIOD 500 | ||
40 | |||
41 | /** | ||
42 | * How long until we give up on connecting the peers? | ||
43 | */ | ||
44 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120) | ||
45 | |||
46 | /** | ||
47 | * Time to wait for stuff that should be rather fast | ||
48 | */ | ||
49 | #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300) | ||
50 | |||
51 | /** | ||
52 | * Total number of rounds. | ||
53 | */ | ||
54 | #define number_rounds sizeof(rounds) / sizeof(rounds[0]) | ||
55 | |||
56 | /** | ||
57 | * Ratio of peers active. First round always is 1.0. | ||
58 | */ | ||
59 | static float rounds[] = { 0.8, 0.6, 0.8, 0.5, 0.3, 0.8, 0.0 }; | ||
60 | |||
61 | /** | ||
62 | * Message type for pings. | ||
63 | */ | ||
64 | struct CadetPingMessage | ||
65 | { | ||
66 | /** | ||
67 | * Header. Type PING/PONG. | ||
68 | */ | ||
69 | struct GNUNET_MessageHeader header; | ||
70 | |||
71 | /** | ||
72 | * Message number. | ||
73 | */ | ||
74 | uint32_t counter; | ||
75 | |||
76 | /** | ||
77 | * Time the message was sent. | ||
78 | */ | ||
79 | struct GNUNET_TIME_AbsoluteNBO timestamp; | ||
80 | |||
81 | /** | ||
82 | * Round number. | ||
83 | */ | ||
84 | uint32_t round_number; | ||
85 | }; | ||
86 | |||
87 | /** | ||
88 | * Peer description. | ||
89 | */ | ||
90 | struct CadetPeer | ||
91 | { | ||
92 | /** | ||
93 | * Testbed Operation (to get peer id, etc). | ||
94 | */ | ||
95 | struct GNUNET_TESTBED_Operation *op; | ||
96 | |||
97 | /** | ||
98 | * Peer ID. | ||
99 | */ | ||
100 | struct GNUNET_PeerIdentity id; | ||
101 | |||
102 | /** | ||
103 | * Cadet handle for the root peer | ||
104 | */ | ||
105 | struct GNUNET_CADET_Handle *cadet; | ||
106 | |||
107 | /** | ||
108 | * Channel handle for the root peer | ||
109 | */ | ||
110 | struct GNUNET_CADET_Channel *ch; | ||
111 | |||
112 | /** | ||
113 | * Channel handle for the dest peer | ||
114 | */ | ||
115 | struct GNUNET_CADET_Channel *incoming_ch; | ||
116 | |||
117 | /** | ||
118 | * Channel handle for a warmup channel. | ||
119 | */ | ||
120 | struct GNUNET_CADET_Channel *warmup_ch; | ||
121 | |||
122 | /** | ||
123 | * Number of payload packes sent | ||
124 | */ | ||
125 | int data_sent; | ||
126 | |||
127 | /** | ||
128 | * Number of payload packets received | ||
129 | */ | ||
130 | int data_received; | ||
131 | |||
132 | /** | ||
133 | * Is peer up? | ||
134 | */ | ||
135 | int up; | ||
136 | |||
137 | /** | ||
138 | * Destinaton to ping. | ||
139 | */ | ||
140 | struct CadetPeer *dest; | ||
141 | |||
142 | /** | ||
143 | * Incoming channel for pings. | ||
144 | */ | ||
145 | struct CadetPeer *incoming; | ||
146 | |||
147 | /** | ||
148 | * Task to do the next ping. | ||
149 | */ | ||
150 | struct GNUNET_SCHEDULER_Task *ping_task; | ||
151 | |||
152 | /** | ||
153 | * NTR operation for the next ping. | ||
154 | */ | ||
155 | struct GNUNET_CADET_TransmitHandle *ping_ntr; | ||
156 | |||
157 | float mean[number_rounds]; | ||
158 | float var[number_rounds]; | ||
159 | unsigned int pongs[number_rounds]; | ||
160 | unsigned int pings[number_rounds]; | ||
161 | }; | ||
162 | |||
163 | /** | ||
164 | * Duration of each round. | ||
165 | */ | ||
166 | static struct GNUNET_TIME_Relative round_time; | ||
167 | |||
168 | /** | ||
169 | * GNUNET_PeerIdentity -> CadetPeer | ||
170 | */ | ||
171 | static struct GNUNET_CONTAINER_MultiPeerMap *ids; | ||
172 | |||
173 | /** | ||
174 | * Testbed peer handles. | ||
175 | */ | ||
176 | static struct GNUNET_TESTBED_Peer **testbed_handles; | ||
177 | |||
178 | /** | ||
179 | * Testbed Operation (to get stats). | ||
180 | */ | ||
181 | static struct GNUNET_TESTBED_Operation *stats_op; | ||
182 | |||
183 | /** | ||
184 | * Operation to get peer ids. | ||
185 | */ | ||
186 | static struct CadetPeer *peers; | ||
187 | |||
188 | /** | ||
189 | * Peer ids counter. | ||
190 | */ | ||
191 | static unsigned int p_ids; | ||
192 | |||
193 | /** | ||
194 | * Total number of peers. | ||
195 | */ | ||
196 | static unsigned long long peers_total; | ||
197 | |||
198 | /** | ||
199 | * Number of currently running peers. | ||
200 | */ | ||
201 | static unsigned long long peers_running; | ||
202 | |||
203 | /** | ||
204 | * Number of peers doing pings. | ||
205 | */ | ||
206 | static unsigned long long peers_pinging; | ||
207 | |||
208 | /** | ||
209 | * Test context (to shut down). | ||
210 | */ | ||
211 | static struct GNUNET_CADET_TEST_Context *test_ctx; | ||
212 | |||
213 | /** | ||
214 | * Task called to disconnect peers, before shutdown. | ||
215 | */ | ||
216 | static struct GNUNET_SCHEDULER_Task *disconnect_task; | ||
217 | |||
218 | /** | ||
219 | * Task to perform tests | ||
220 | */ | ||
221 | static struct GNUNET_SCHEDULER_Task *test_task; | ||
222 | |||
223 | /** | ||
224 | * Round number. | ||
225 | */ | ||
226 | static unsigned int current_round; | ||
227 | |||
228 | /** | ||
229 | * Do preconnect? (Each peer creates a tunnel to one other peer). | ||
230 | */ | ||
231 | static int do_warmup; | ||
232 | |||
233 | /** | ||
234 | * Warmup progress. | ||
235 | */ | ||
236 | static unsigned int peers_warmup; | ||
237 | |||
238 | /** | ||
239 | * Flag to notify callbacks not to generate any new traffic anymore. | ||
240 | */ | ||
241 | static int test_finished; | ||
242 | |||
243 | /** | ||
244 | * Task running each round of the benchmark. | ||
245 | */ | ||
246 | static struct GNUNET_SCHEDULER_Task *round_task; | ||
247 | |||
248 | |||
249 | /** | ||
250 | * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE CADET SERVICES. | ||
251 | * | ||
252 | * Testcase continues when the root receives confirmation of connected peers, | ||
253 | * on callback function ch. | ||
254 | * | ||
255 | * @param cls Closure (unused). | ||
256 | */ | ||
257 | static void | ||
258 | start_test (void *cls); | ||
259 | |||
260 | |||
261 | /** | ||
262 | * Calculate a random delay. | ||
263 | * | ||
264 | * @param max Exclusive maximum, in ms. | ||
265 | * | ||
266 | * @return A time between 0 a max-1 ms. | ||
267 | */ | ||
268 | static struct GNUNET_TIME_Relative | ||
269 | delay_ms_rnd (unsigned int max) | ||
270 | { | ||
271 | unsigned int rnd; | ||
272 | |||
273 | rnd = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max); | ||
274 | return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rnd); | ||
275 | } | ||
276 | |||
277 | |||
278 | /** | ||
279 | * Get the index of a peer in the peers array. | ||
280 | * | ||
281 | * @param peer Peer whose index to get. | ||
282 | * | ||
283 | * @return Index of peer in peers. | ||
284 | */ | ||
285 | static unsigned int | ||
286 | get_index (struct CadetPeer *peer) | ||
287 | { | ||
288 | return peer - peers; | ||
289 | } | ||
290 | |||
291 | |||
292 | /** | ||
293 | * Show the results of the test (banwidth achieved) and log them to GAUGER | ||
294 | */ | ||
295 | static void | ||
296 | show_end_data (void) | ||
297 | { | ||
298 | struct CadetPeer *peer; | ||
299 | unsigned int i; | ||
300 | unsigned int j; | ||
301 | |||
302 | for (i = 0; i < number_rounds; i++) | ||
303 | { | ||
304 | for (j = 0; j < peers_pinging; j++) | ||
305 | { | ||
306 | peer = &peers[j]; | ||
307 | fprintf (stdout, | ||
308 | "ROUND %3u PEER %3u: %10.2f / %10.2f, PINGS: %3u, PONGS: %3u\n", | ||
309 | i, j, peer->mean[i], sqrt (peer->var[i] / (peer->pongs[i] - 1)), | ||
310 | peer->pings[i], peer->pongs[i]); | ||
311 | } | ||
312 | } | ||
313 | } | ||
314 | |||
315 | |||
316 | /** | ||
317 | * Disconnect from cadet services af all peers, call shutdown. | ||
318 | * | ||
319 | * @param cls Closure (unused). | ||
320 | */ | ||
321 | static void | ||
322 | disconnect_cadet_peers (void *cls) | ||
323 | { | ||
324 | long line = (long) cls; | ||
325 | unsigned int i; | ||
326 | |||
327 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
328 | "disconnecting cadet service, called from line %ld\n", | ||
329 | line); | ||
330 | disconnect_task = NULL; | ||
331 | for (i = 0; i < peers_total; i++) | ||
332 | { | ||
333 | if (NULL != peers[i].op) | ||
334 | GNUNET_TESTBED_operation_done (peers[i].op); | ||
335 | |||
336 | if (peers[i].up != GNUNET_YES) | ||
337 | continue; | ||
338 | |||
339 | if (NULL != peers[i].ch) | ||
340 | { | ||
341 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
342 | "%u: channel %p\n", i, peers[i].ch); | ||
343 | GNUNET_CADET_channel_destroy (peers[i].ch); | ||
344 | } | ||
345 | if (NULL != peers[i].warmup_ch) | ||
346 | { | ||
347 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
348 | "%u: warmup channel %p\n", | ||
349 | i, peers[i].warmup_ch); | ||
350 | GNUNET_CADET_channel_destroy (peers[i].warmup_ch); | ||
351 | } | ||
352 | if (NULL != peers[i].incoming_ch) | ||
353 | { | ||
354 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
355 | "%u: incoming channel %p\n", | ||
356 | i, peers[i].incoming_ch); | ||
357 | GNUNET_CADET_channel_destroy (peers[i].incoming_ch); | ||
358 | } | ||
359 | } | ||
360 | GNUNET_CADET_TEST_cleanup (test_ctx); | ||
361 | GNUNET_SCHEDULER_shutdown (); | ||
362 | } | ||
363 | |||
364 | |||
365 | /** | ||
366 | * Shut down peergroup, clean up. | ||
367 | * | ||
368 | * @param cls Closure (unused). | ||
369 | */ | ||
370 | static void | ||
371 | shutdown_task (void *cls) | ||
372 | { | ||
373 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
374 | "Ending test.\n"); | ||
375 | if (NULL != disconnect_task) | ||
376 | { | ||
377 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
378 | disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, | ||
379 | (void *) __LINE__); | ||
380 | } | ||
381 | if (NULL != round_task) | ||
382 | { | ||
383 | GNUNET_SCHEDULER_cancel (round_task); | ||
384 | round_task = NULL; | ||
385 | } | ||
386 | if (NULL != test_task) | ||
387 | { | ||
388 | GNUNET_SCHEDULER_cancel (test_task); | ||
389 | test_task = NULL; | ||
390 | } | ||
391 | } | ||
392 | |||
393 | |||
394 | /** | ||
395 | * Finish test normally: schedule disconnect and shutdown | ||
396 | * | ||
397 | * @param line Line in the code the abort is requested from (__LINE__). | ||
398 | */ | ||
399 | static void | ||
400 | abort_test (long line) | ||
401 | { | ||
402 | if (disconnect_task != NULL) | ||
403 | { | ||
404 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
405 | disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, | ||
406 | (void *) line); | ||
407 | } | ||
408 | } | ||
409 | |||
410 | |||
411 | /** | ||
412 | * Stats callback. Finish the stats testbed operation and when all stats have | ||
413 | * been iterated, shutdown the test. | ||
414 | * | ||
415 | * @param cls closure | ||
416 | * @param op the operation that has been finished | ||
417 | * @param emsg error message in case the operation has failed; will be NULL if | ||
418 | * operation has executed successfully. | ||
419 | */ | ||
420 | static void | ||
421 | stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg) | ||
422 | { | ||
423 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n"); | ||
424 | GNUNET_TESTBED_operation_done (stats_op); | ||
425 | |||
426 | if (NULL != disconnect_task) | ||
427 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
428 | disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, | ||
429 | (void *) __LINE__); | ||
430 | } | ||
431 | |||
432 | |||
433 | /** | ||
434 | * Process statistic values. | ||
435 | * | ||
436 | * @param cls closure | ||
437 | * @param peer the peer the statistic belong to | ||
438 | * @param subsystem name of subsystem that created the statistic | ||
439 | * @param name the name of the datum | ||
440 | * @param value the current value | ||
441 | * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not | ||
442 | * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration | ||
443 | */ | ||
444 | static int | ||
445 | stats_iterator (void *cls, | ||
446 | const struct GNUNET_TESTBED_Peer *peer, | ||
447 | const char *subsystem, | ||
448 | const char *name, | ||
449 | uint64_t value, | ||
450 | int is_persistent) | ||
451 | { | ||
452 | uint32_t i; | ||
453 | |||
454 | i = GNUNET_TESTBED_get_index (peer); | ||
455 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
456 | " STATS %u - %s [%s]: %llu\n", | ||
457 | i, subsystem, name, | ||
458 | (unsigned long long) value); | ||
459 | |||
460 | return GNUNET_OK; | ||
461 | } | ||
462 | |||
463 | |||
464 | /** | ||
465 | * Task check that keepalives were sent and received. | ||
466 | * | ||
467 | * @param cls Closure (NULL). | ||
468 | */ | ||
469 | static void | ||
470 | collect_stats (void *cls) | ||
471 | { | ||
472 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
473 | "Start collecting statistics...\n"); | ||
474 | stats_op = GNUNET_TESTBED_get_statistics (peers_total, | ||
475 | testbed_handles, | ||
476 | NULL, NULL, | ||
477 | &stats_iterator, | ||
478 | &stats_cont, NULL); | ||
479 | } | ||
480 | |||
481 | |||
482 | /** | ||
483 | * @brief Finish profiler normally. Signal finish and start collecting stats. | ||
484 | * | ||
485 | * @param cls Closure (unused). | ||
486 | */ | ||
487 | static void | ||
488 | finish_profiler (void *cls) | ||
489 | { | ||
490 | test_finished = GNUNET_YES; | ||
491 | show_end_data (); | ||
492 | GNUNET_SCHEDULER_add_now (&collect_stats, NULL); | ||
493 | } | ||
494 | |||
495 | |||
496 | /** | ||
497 | * Set the total number of running peers. | ||
498 | * | ||
499 | * @param target Desired number of running peers. | ||
500 | */ | ||
501 | static void | ||
502 | adjust_running_peers (unsigned int target) | ||
503 | { | ||
504 | struct GNUNET_TESTBED_Operation *op; | ||
505 | unsigned int delta; | ||
506 | unsigned int run; | ||
507 | unsigned int i; | ||
508 | unsigned int r; | ||
509 | |||
510 | GNUNET_assert (target <= peers_total); | ||
511 | |||
512 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "adjust peers to %u\n", target); | ||
513 | if (target > peers_running) | ||
514 | { | ||
515 | delta = target - peers_running; | ||
516 | run = GNUNET_YES; | ||
517 | } | ||
518 | else | ||
519 | { | ||
520 | delta = peers_running - target; | ||
521 | run = GNUNET_NO; | ||
522 | } | ||
523 | |||
524 | for (i = 0; i < delta; i++) | ||
525 | { | ||
526 | do | ||
527 | { | ||
528 | r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, | ||
529 | peers_total - peers_pinging); | ||
530 | r += peers_pinging; | ||
531 | } | ||
532 | while (peers[r].up == run || NULL != peers[r].incoming); | ||
533 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "St%s peer %u: %s\n", | ||
534 | run ? "arting" : "opping", r, GNUNET_i2s (&peers[r].id)); | ||
535 | |||
536 | if (NULL != peers[r].ping_task) | ||
537 | { | ||
538 | GNUNET_SCHEDULER_cancel (peers[r].ping_task); | ||
539 | peers[r].ping_task = NULL; | ||
540 | } | ||
541 | if (NULL != peers[r].ping_ntr) | ||
542 | { | ||
543 | GNUNET_CADET_notify_transmit_ready_cancel (peers[r].ping_ntr); | ||
544 | peers[r].ping_ntr = NULL; | ||
545 | } | ||
546 | peers[r].up = run; | ||
547 | |||
548 | if (NULL != peers[r].ch) | ||
549 | GNUNET_CADET_channel_destroy (peers[r].ch); | ||
550 | peers[r].ch = NULL; | ||
551 | if (NULL != peers[r].dest) | ||
552 | { | ||
553 | if (NULL != peers[r].dest->incoming_ch) | ||
554 | GNUNET_CADET_channel_destroy (peers[r].dest->incoming_ch); | ||
555 | peers[r].dest->incoming_ch = NULL; | ||
556 | } | ||
557 | |||
558 | op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r], | ||
559 | "cadet", NULL, NULL, run); | ||
560 | GNUNET_break (NULL != op); | ||
561 | peers_running += run ? 1 : -1; | ||
562 | GNUNET_assert (peers_running > 0); | ||
563 | } | ||
564 | } | ||
565 | |||
566 | |||
567 | /** | ||
568 | * @brief Move to next round. | ||
569 | * | ||
570 | * @param cls Closure (round #). | ||
571 | */ | ||
572 | static void | ||
573 | next_rnd (void *cls) | ||
574 | { | ||
575 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
576 | "ROUND %u\n", | ||
577 | current_round); | ||
578 | if (0.0 == rounds[current_round]) | ||
579 | { | ||
580 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Finishing\n"); | ||
581 | GNUNET_SCHEDULER_add_now (&finish_profiler, NULL); | ||
582 | return; | ||
583 | } | ||
584 | adjust_running_peers (rounds[current_round] * peers_total); | ||
585 | current_round++; | ||
586 | |||
587 | round_task = GNUNET_SCHEDULER_add_delayed (round_time, | ||
588 | &next_rnd, | ||
589 | NULL); | ||
590 | } | ||
591 | |||
592 | |||
593 | /** | ||
594 | * Transmit ping callback. | ||
595 | * | ||
596 | * @param cls Closure (peer for PING, NULL for PONG). | ||
597 | * @param size Size of the transmit buffer. | ||
598 | * @param buf Pointer to the beginning of the buffer. | ||
599 | * | ||
600 | * @return Number of bytes written to buf. | ||
601 | */ | ||
602 | static size_t | ||
603 | tmt_rdy_ping (void *cls, size_t size, void *buf); | ||
604 | |||
605 | |||
606 | /** | ||
607 | * Transmit pong callback. | ||
608 | * | ||
609 | * @param cls Closure (copy of PING message, to be freed). | ||
610 | * @param size Size of the buffer we have. | ||
611 | * @param buf Buffer to copy data to. | ||
612 | */ | ||
613 | static size_t | ||
614 | tmt_rdy_pong (void *cls, size_t size, void *buf) | ||
615 | { | ||
616 | struct CadetPingMessage *ping = cls; | ||
617 | struct CadetPingMessage *pong; | ||
618 | |||
619 | if ((0 == size) || (NULL == buf)) | ||
620 | { | ||
621 | GNUNET_free (ping); | ||
622 | return 0; | ||
623 | } | ||
624 | pong = (struct CadetPingMessage *) buf; | ||
625 | GNUNET_memcpy (pong, ping, sizeof(*ping)); | ||
626 | pong->header.type = htons (PONG); | ||
627 | |||
628 | GNUNET_free (ping); | ||
629 | return sizeof(*ping); | ||
630 | } | ||
631 | |||
632 | |||
633 | /** | ||
634 | * @brief Send a ping to destination | ||
635 | * | ||
636 | * @param cls Closure (peer). | ||
637 | */ | ||
638 | static void | ||
639 | ping (void *cls) | ||
640 | { | ||
641 | struct CadetPeer *peer = cls; | ||
642 | |||
643 | peer->ping_task = NULL; | ||
644 | if (GNUNET_YES == test_finished) | ||
645 | return; | ||
646 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
647 | "%u -> %u (%u)\n", | ||
648 | get_index (peer), | ||
649 | get_index (peer->dest), | ||
650 | peer->data_sent); | ||
651 | peer->ping_ntr = GNUNET_CADET_notify_transmit_ready (peer->ch, GNUNET_NO, | ||
652 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
653 | sizeof(struct | ||
654 | CadetPingMessage), | ||
655 | &tmt_rdy_ping, peer); | ||
656 | } | ||
657 | |||
658 | |||
659 | /** | ||
660 | * @brief Reply with a pong to origin. | ||
661 | * | ||
662 | */ | ||
663 | static void | ||
664 | pong (struct GNUNET_CADET_Channel *channel, | ||
665 | const struct CadetPingMessage *ping) | ||
666 | { | ||
667 | struct CadetPingMessage *copy; | ||
668 | |||
669 | copy = GNUNET_new (struct CadetPingMessage); | ||
670 | *copy = *ping; | ||
671 | GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO, | ||
672 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
673 | sizeof(struct CadetPingMessage), | ||
674 | &tmt_rdy_pong, copy); | ||
675 | } | ||
676 | |||
677 | |||
678 | /** | ||
679 | * Transmit ping callback | ||
680 | * | ||
681 | * @param cls Closure (peer). | ||
682 | * @param size Size of the buffer we have. | ||
683 | * @param buf Buffer to copy data to. | ||
684 | */ | ||
685 | static size_t | ||
686 | tmt_rdy_ping (void *cls, size_t size, void *buf) | ||
687 | { | ||
688 | struct CadetPeer *peer = cls; | ||
689 | struct CadetPingMessage *msg = buf; | ||
690 | |||
691 | peer->ping_ntr = NULL; | ||
692 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
693 | "tmt_rdy called, filling buffer\n"); | ||
694 | if ((size < sizeof(struct CadetPingMessage)) || (NULL == buf)) | ||
695 | { | ||
696 | GNUNET_break (GNUNET_YES == test_finished); | ||
697 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
698 | "size %u, buf %p, data_sent %u, data_received %u\n", | ||
699 | (unsigned int) size, | ||
700 | buf, | ||
701 | peer->data_sent, | ||
702 | peer->data_received); | ||
703 | |||
704 | return 0; | ||
705 | } | ||
706 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
707 | "Sending: msg %d\n", | ||
708 | peer->data_sent); | ||
709 | msg->header.size = htons (size); | ||
710 | msg->header.type = htons (PING); | ||
711 | msg->counter = htonl (peer->data_sent++); | ||
712 | msg->round_number = htonl (current_round); | ||
713 | msg->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ()); | ||
714 | peer->pings[current_round]++; | ||
715 | peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD), | ||
716 | &ping, peer); | ||
717 | |||
718 | return sizeof(struct CadetPingMessage); | ||
719 | } | ||
720 | |||
721 | |||
722 | /** | ||
723 | * Function is called whenever a PING message is received. | ||
724 | * | ||
725 | * @param cls closure (peer #, set from GNUNET_CADET_connect) | ||
726 | * @param channel connection to the other end | ||
727 | * @param channel_ctx place to store local state associated with the channel | ||
728 | * @param message the actual message | ||
729 | * @return GNUNET_OK to keep the connection open, | ||
730 | * GNUNET_SYSERR to close it (signal serious error) | ||
731 | */ | ||
732 | int | ||
733 | ping_handler (void *cls, struct GNUNET_CADET_Channel *channel, | ||
734 | void **channel_ctx, | ||
735 | const struct GNUNET_MessageHeader *message) | ||
736 | { | ||
737 | long n = (long) cls; | ||
738 | |||
739 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
740 | "%u got PING\n", | ||
741 | (unsigned int) n); | ||
742 | GNUNET_CADET_receive_done (channel); | ||
743 | if (GNUNET_NO == test_finished) | ||
744 | pong (channel, (struct CadetPingMessage *) message); | ||
745 | |||
746 | return GNUNET_OK; | ||
747 | } | ||
748 | |||
749 | |||
750 | /** | ||
751 | * Function is called whenever a PONG message is received. | ||
752 | * | ||
753 | * @param cls closure (peer #, set from GNUNET_CADET_connect) | ||
754 | * @param channel connection to the other end | ||
755 | * @param channel_ctx place to store local state associated with the channel | ||
756 | * @param message the actual message | ||
757 | * @return GNUNET_OK to keep the connection open, | ||
758 | * GNUNET_SYSERR to close it (signal serious error) | ||
759 | */ | ||
760 | int | ||
761 | pong_handler (void *cls, struct GNUNET_CADET_Channel *channel, | ||
762 | void **channel_ctx, | ||
763 | const struct GNUNET_MessageHeader *message) | ||
764 | { | ||
765 | long n = (long) cls; | ||
766 | struct CadetPeer *peer; | ||
767 | struct CadetPingMessage *msg; | ||
768 | struct GNUNET_TIME_Absolute send_time; | ||
769 | struct GNUNET_TIME_Relative latency; | ||
770 | unsigned int r /* Ping round */; | ||
771 | float delta; | ||
772 | |||
773 | GNUNET_CADET_receive_done (channel); | ||
774 | peer = &peers[n]; | ||
775 | |||
776 | msg = (struct CadetPingMessage *) message; | ||
777 | |||
778 | send_time = GNUNET_TIME_absolute_ntoh (msg->timestamp); | ||
779 | latency = GNUNET_TIME_absolute_get_duration (send_time); | ||
780 | r = ntohl (msg->round_number); | ||
781 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u (%u) latency: %s\n", | ||
782 | get_index (peer), | ||
783 | get_index (peer->dest), | ||
784 | (uint32_t) ntohl (msg->counter), | ||
785 | GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO)); | ||
786 | |||
787 | /* Online variance calculation */ | ||
788 | peer->pongs[r]++; | ||
789 | delta = latency.rel_value_us - peer->mean[r]; | ||
790 | peer->mean[r] = peer->mean[r] + delta / peer->pongs[r]; | ||
791 | peer->var[r] += delta * (latency.rel_value_us - peer->mean[r]); | ||
792 | |||
793 | return GNUNET_OK; | ||
794 | } | ||
795 | |||
796 | |||
797 | /** | ||
798 | * Handlers, for diverse services | ||
799 | */ | ||
800 | static struct GNUNET_CADET_MessageHandler handlers[] = { | ||
801 | { &ping_handler, PING, sizeof(struct CadetPingMessage) }, | ||
802 | { &pong_handler, PONG, sizeof(struct CadetPingMessage) }, | ||
803 | { NULL, 0, 0 } | ||
804 | }; | ||
805 | |||
806 | |||
807 | /** | ||
808 | * Method called whenever another peer has added us to a channel | ||
809 | * the other peer initiated. | ||
810 | * | ||
811 | * @param cls Closure. | ||
812 | * @param channel New handle to the channel. | ||
813 | * @param initiator Peer that started the channel. | ||
814 | * @param port Port this channel is connected to. | ||
815 | * @param options channel option flags | ||
816 | * @return Initial channel context for the channel | ||
817 | * (can be NULL -- that's not an error). | ||
818 | */ | ||
819 | static void * | ||
820 | incoming_channel (void *cls, struct GNUNET_CADET_Channel *channel, | ||
821 | const struct GNUNET_PeerIdentity *initiator, | ||
822 | const struct GNUNET_HashCode *port, | ||
823 | enum GNUNET_CADET_ChannelOption options) | ||
824 | { | ||
825 | long n = (long) cls; | ||
826 | struct CadetPeer *peer; | ||
827 | |||
828 | peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator); | ||
829 | GNUNET_assert (NULL != peer); | ||
830 | if (NULL == peers[n].incoming) | ||
831 | { | ||
832 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
833 | "WARMUP %3u: %u <= %u\n", | ||
834 | peers_warmup, | ||
835 | (unsigned int) n, | ||
836 | get_index (peer)); | ||
837 | peers_warmup++; | ||
838 | if (peers_warmup < peers_total) | ||
839 | return NULL; | ||
840 | if (NULL != test_task) | ||
841 | { | ||
842 | GNUNET_SCHEDULER_cancel (test_task); | ||
843 | test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
844 | &start_test, NULL); | ||
845 | } | ||
846 | return NULL; | ||
847 | } | ||
848 | GNUNET_assert (peer == peers[n].incoming); | ||
849 | GNUNET_assert (peer->dest == &peers[n]); | ||
850 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
851 | "%u <= %u %p\n", | ||
852 | (unsigned int) n, | ||
853 | get_index (peer), | ||
854 | channel); | ||
855 | peers[n].incoming_ch = channel; | ||
856 | |||
857 | return NULL; | ||
858 | } | ||
859 | |||
860 | |||
861 | /** | ||
862 | * Function called whenever an inbound channel is destroyed. Should clean up | ||
863 | * any associated state. | ||
864 | * | ||
865 | * @param cls closure (set from GNUNET_CADET_connect) | ||
866 | * @param channel connection to the other end (henceforth invalid) | ||
867 | * @param channel_ctx place where local state associated | ||
868 | * with the channel is stored | ||
869 | */ | ||
870 | static void | ||
871 | channel_cleaner (void *cls, | ||
872 | const struct GNUNET_CADET_Channel *channel, | ||
873 | void *channel_ctx) | ||
874 | { | ||
875 | long n = (long) cls; | ||
876 | struct CadetPeer *peer = &peers[n]; | ||
877 | |||
878 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
879 | "Channel %p disconnected at peer %ld\n", channel, n); | ||
880 | if (peer->ch == channel) | ||
881 | peer->ch = NULL; | ||
882 | } | ||
883 | |||
884 | |||
885 | /** | ||
886 | * Select a random peer that has no incoming channel | ||
887 | * | ||
888 | * @param peer ID of the peer connecting. NULL if irrelevant (warmup). | ||
889 | * | ||
890 | * @return Random peer not yet connected to. | ||
891 | */ | ||
892 | static struct CadetPeer * | ||
893 | select_random_peer (struct CadetPeer *peer) | ||
894 | { | ||
895 | unsigned int r; | ||
896 | |||
897 | do | ||
898 | { | ||
899 | r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, peers_total); | ||
900 | } | ||
901 | while (NULL != peers[r].incoming); | ||
902 | peers[r].incoming = peer; | ||
903 | |||
904 | return &peers[r]; | ||
905 | } | ||
906 | |||
907 | |||
908 | /** | ||
909 | * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE CADET SERVICES. | ||
910 | * | ||
911 | * Testcase continues when the root receives confirmation of connected peers, | ||
912 | * on callback function ch. | ||
913 | * | ||
914 | * @param cls Closure (unused). | ||
915 | */ | ||
916 | static void | ||
917 | start_test (void *cls) | ||
918 | { | ||
919 | unsigned long i; | ||
920 | |||
921 | test_task = NULL; | ||
922 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n"); | ||
923 | |||
924 | |||
925 | for (i = 0; i < peers_pinging; i++) | ||
926 | { | ||
927 | peers[i].dest = select_random_peer (&peers[i]); | ||
928 | peers[i].ch = GNUNET_CADET_channel_create (peers[i].cadet, NULL, | ||
929 | &peers[i].dest->id, | ||
930 | GC_u2h (1)); | ||
931 | if (NULL == peers[i].ch) | ||
932 | { | ||
933 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Channel %lu failed\n", i); | ||
934 | GNUNET_CADET_TEST_cleanup (test_ctx); | ||
935 | return; | ||
936 | } | ||
937 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
938 | "%lu => %u %p\n", | ||
939 | i, | ||
940 | get_index (peers[i].dest), | ||
941 | peers[i].ch); | ||
942 | peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000), | ||
943 | &ping, &peers[i]); | ||
944 | } | ||
945 | peers_running = peers_total; | ||
946 | if (NULL != disconnect_task) | ||
947 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
948 | disconnect_task = | ||
949 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (round_time, | ||
950 | number_rounds | ||
951 | + 1), | ||
952 | &disconnect_cadet_peers, | ||
953 | (void *) __LINE__); | ||
954 | round_task = GNUNET_SCHEDULER_add_delayed (round_time, | ||
955 | &next_rnd, | ||
956 | NULL); | ||
957 | } | ||
958 | |||
959 | |||
960 | /** | ||
961 | * Do warmup: create some channels to spread information about the topology. | ||
962 | */ | ||
963 | static void | ||
964 | warmup (void) | ||
965 | { | ||
966 | struct CadetPeer *peer; | ||
967 | unsigned int i; | ||
968 | |||
969 | for (i = 0; i < peers_total; i++) | ||
970 | { | ||
971 | peer = select_random_peer (NULL); | ||
972 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "WARMUP %u => %u\n", | ||
973 | i, get_index (peer)); | ||
974 | peers[i].warmup_ch = | ||
975 | GNUNET_CADET_channel_create (peers[i].cadet, NULL, &peer->id, | ||
976 | GC_u2h (1)); | ||
977 | if (NULL == peers[i].warmup_ch) | ||
978 | { | ||
979 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Warmup %u failed\n", i); | ||
980 | GNUNET_CADET_TEST_cleanup (test_ctx); | ||
981 | return; | ||
982 | } | ||
983 | } | ||
984 | } | ||
985 | |||
986 | |||
987 | /** | ||
988 | * Callback to be called when the requested peer information is available | ||
989 | * | ||
990 | * @param cls the closure from GNUNET_TESTBED_peer_get_information() | ||
991 | * @param op the operation this callback corresponds to | ||
992 | * @param pinfo the result; will be NULL if the operation has failed | ||
993 | * @param emsg error message if the operation has failed; | ||
994 | * NULL if the operation is successful | ||
995 | */ | ||
996 | static void | ||
997 | peer_id_cb (void *cls, | ||
998 | struct GNUNET_TESTBED_Operation *op, | ||
999 | const struct GNUNET_TESTBED_PeerInformation *pinfo, | ||
1000 | const char *emsg) | ||
1001 | { | ||
1002 | long n = (long) cls; | ||
1003 | |||
1004 | if ((NULL == pinfo) || (NULL != emsg)) | ||
1005 | { | ||
1006 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg); | ||
1007 | abort_test (__LINE__); | ||
1008 | return; | ||
1009 | } | ||
1010 | peers[n].id = *(pinfo->result.id); | ||
1011 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1012 | "%ld id: %s\n", | ||
1013 | n, | ||
1014 | GNUNET_i2s (&peers[n].id)); | ||
1015 | GNUNET_break (GNUNET_OK == | ||
1016 | GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n], | ||
1017 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)); | ||
1018 | |||
1019 | GNUNET_TESTBED_operation_done (peers[n].op); | ||
1020 | peers[n].op = NULL; | ||
1021 | |||
1022 | p_ids++; | ||
1023 | if (p_ids < peers_total) | ||
1024 | return; | ||
1025 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n"); | ||
1026 | if (do_warmup) | ||
1027 | { | ||
1028 | struct GNUNET_TIME_Relative delay; | ||
1029 | |||
1030 | warmup (); | ||
1031 | delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, | ||
1032 | 100 * peers_total); | ||
1033 | test_task = GNUNET_SCHEDULER_add_delayed (delay, &start_test, NULL); | ||
1034 | return; /* start_test from incoming_channel */ | ||
1035 | } | ||
1036 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting in a second...\n"); | ||
1037 | test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
1038 | &start_test, NULL); | ||
1039 | } | ||
1040 | |||
1041 | |||
1042 | /** | ||
1043 | * test main: start test when all peers are connected | ||
1044 | * | ||
1045 | * @param cls Closure. | ||
1046 | * @param ctx Argument to give to GNUNET_CADET_TEST_cleanup on test end. | ||
1047 | * @param num_peers Number of peers that are running. | ||
1048 | * @param testbed_peers Array of peers. | ||
1049 | * @param cadetes Handle to each of the CADETs of the peers. | ||
1050 | */ | ||
1051 | static void | ||
1052 | tmain (void *cls, | ||
1053 | struct GNUNET_CADET_TEST_Context *ctx, | ||
1054 | unsigned int num_peers, | ||
1055 | struct GNUNET_TESTBED_Peer **testbed_peers, | ||
1056 | struct GNUNET_CADET_Handle **cadetes) | ||
1057 | { | ||
1058 | unsigned long i; | ||
1059 | |||
1060 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1061 | "test main\n"); | ||
1062 | test_ctx = ctx; | ||
1063 | GNUNET_assert (peers_total == num_peers); | ||
1064 | peers_running = num_peers; | ||
1065 | testbed_handles = testbed_peers; | ||
1066 | disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME, | ||
1067 | &disconnect_cadet_peers, | ||
1068 | (void *) __LINE__); | ||
1069 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); | ||
1070 | for (i = 0; i < peers_total; i++) | ||
1071 | { | ||
1072 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1073 | "requesting id %ld\n", | ||
1074 | i); | ||
1075 | peers[i].up = GNUNET_YES; | ||
1076 | peers[i].cadet = cadetes[i]; | ||
1077 | peers[i].op = | ||
1078 | GNUNET_TESTBED_peer_get_information (testbed_handles[i], | ||
1079 | GNUNET_TESTBED_PIT_IDENTITY, | ||
1080 | &peer_id_cb, (void *) i); | ||
1081 | } | ||
1082 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "requested peer ids\n"); | ||
1083 | /* Continues from pi_cb -> do_test */ | ||
1084 | } | ||
1085 | |||
1086 | |||
1087 | /** | ||
1088 | * Main: start profiler. | ||
1089 | */ | ||
1090 | int | ||
1091 | main (int argc, char *argv[]) | ||
1092 | { | ||
1093 | static const struct GNUNET_HashCode *ports[2]; | ||
1094 | const char *config_file; | ||
1095 | |||
1096 | config_file = ".profiler.conf"; | ||
1097 | |||
1098 | if (4 > argc) | ||
1099 | { | ||
1100 | fprintf (stderr, | ||
1101 | "usage: %s ROUND_TIME PEERS PINGS [DO_WARMUP]\n", | ||
1102 | argv[0]); | ||
1103 | fprintf (stderr, | ||
1104 | "example: %s 30s 16 1 Y\n", | ||
1105 | argv[0]); | ||
1106 | return 1; | ||
1107 | } | ||
1108 | |||
1109 | if (GNUNET_OK != | ||
1110 | GNUNET_STRINGS_fancy_time_to_relative (argv[1], | ||
1111 | &round_time)) | ||
1112 | { | ||
1113 | fprintf (stderr, | ||
1114 | "%s is not a valid time\n", | ||
1115 | argv[1]); | ||
1116 | return 1; | ||
1117 | } | ||
1118 | |||
1119 | peers_total = atoll (argv[2]); | ||
1120 | if (2 > peers_total) | ||
1121 | { | ||
1122 | fprintf (stderr, | ||
1123 | "%s peers is not valid (> 2)\n", | ||
1124 | argv[1]); | ||
1125 | return 1; | ||
1126 | } | ||
1127 | peers = GNUNET_new_array (peers_total, | ||
1128 | struct CadetPeer); | ||
1129 | peers_pinging = atoll (argv[3]); | ||
1130 | |||
1131 | if (peers_total < 2 * peers_pinging) | ||
1132 | { | ||
1133 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1134 | "not enough peers, total should be > 2 * peers_pinging\n"); | ||
1135 | return 1; | ||
1136 | } | ||
1137 | |||
1138 | do_warmup = (5 > argc || argv[4][0] != 'N'); | ||
1139 | |||
1140 | ids = GNUNET_CONTAINER_multipeermap_create (2 * peers_total, | ||
1141 | GNUNET_YES); | ||
1142 | GNUNET_assert (NULL != ids); | ||
1143 | p_ids = 0; | ||
1144 | test_finished = GNUNET_NO; | ||
1145 | ports[0] = GC_u2h (1); | ||
1146 | ports[1] = 0; | ||
1147 | GNUNET_CADET_TEST_run ("cadet-profiler", config_file, peers_total, | ||
1148 | &tmain, NULL, /* tmain cls */ | ||
1149 | &incoming_channel, &channel_cleaner, | ||
1150 | handlers, ports); | ||
1151 | GNUNET_free (peers); | ||
1152 | |||
1153 | return 0; | ||
1154 | } | ||
1155 | |||
1156 | |||
1157 | /* end of gnunet-cadet-profiler.c */ | ||
diff --git a/src/cadet/meson.build b/src/cadet/meson.build index 5684d06ea..a961c8e76 100644 --- a/src/cadet/meson.build +++ b/src/cadet/meson.build | |||
@@ -72,3 +72,16 @@ executable ('gnunet-service-cadet', | |||
72 | install: true, | 72 | install: true, |
73 | install_dir: get_option('libdir') / 'gnunet' / 'libexec') | 73 | install_dir: get_option('libdir') / 'gnunet' / 'libexec') |
74 | 74 | ||
75 | testcadetlocalmq = executable ('test_cadet_local_mq', | ||
76 | ['test_cadet_local_mq.c'], | ||
77 | dependencies: [libgnunetcadet_dep, | ||
78 | libgnunettesting_dep, | ||
79 | libgnunetutil_dep], | ||
80 | include_directories: [incdir, configuration_inc], | ||
81 | install: false) | ||
82 | configure_file(input : 'test_cadet.conf', | ||
83 | output : 'test_cadet.conf', | ||
84 | configuration : cdata, | ||
85 | install: false) | ||
86 | test('test_cadet_local_mq', testcadetlocalmq, workdir: meson.current_build_dir(), suite: 'cadet') | ||
87 | |||
diff --git a/src/cadet/test_cadet.c b/src/cadet/test_cadet.c deleted file mode 100644 index 61c09f389..000000000 --- a/src/cadet/test_cadet.c +++ /dev/null | |||
@@ -1,1605 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2011, 2017 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file cadet/test_cadet.c | ||
22 | * @author Bart Polot | ||
23 | * @author Christian Grothoff | ||
24 | * @brief Test for the cadet service using mq API. | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include <stdio.h> | ||
28 | #include "cadet.h" | ||
29 | #include "cadet_test_lib.h" | ||
30 | #include "gnunet_cadet_service.h" | ||
31 | #include "gnunet_statistics_service.h" | ||
32 | #include <gauger.h> | ||
33 | |||
34 | |||
35 | /** | ||
36 | * Ugly workaround to unify data handlers on incoming and outgoing channels. | ||
37 | */ | ||
38 | struct CadetTestChannelWrapper | ||
39 | { | ||
40 | /** | ||
41 | * Channel pointer. | ||
42 | */ | ||
43 | struct GNUNET_CADET_Channel *ch; | ||
44 | }; | ||
45 | |||
46 | /** | ||
47 | * How many messages to send by default. | ||
48 | */ | ||
49 | #define TOTAL_PACKETS 500 /* Cannot exceed 64k! */ | ||
50 | |||
51 | /** | ||
52 | * How long until we give up on connecting the peers? | ||
53 | */ | ||
54 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120) | ||
55 | |||
56 | /** | ||
57 | * Time to wait by default for stuff that should be rather fast. | ||
58 | */ | ||
59 | #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20) | ||
60 | |||
61 | /** | ||
62 | * How fast do we send messages? | ||
63 | */ | ||
64 | #define SEND_INTERVAL GNUNET_TIME_relative_multiply ( \ | ||
65 | GNUNET_TIME_UNIT_MILLISECONDS, 10) | ||
66 | |||
67 | /** | ||
68 | * DIFFERENT TESTS TO RUN | ||
69 | */ | ||
70 | #define SETUP 0 | ||
71 | #define FORWARD 1 | ||
72 | #define KEEPALIVE 2 | ||
73 | #define SPEED 3 | ||
74 | #define SPEED_ACK 4 | ||
75 | #define SPEED_REL 8 | ||
76 | #define P2P_SIGNAL 10 | ||
77 | #define REOPEN 11 | ||
78 | #define DESTROY 12 | ||
79 | |||
80 | /** | ||
81 | * Active peer listing operation. | ||
82 | */ | ||
83 | static struct GNUNET_CADET_PeersLister *plo; | ||
84 | |||
85 | /* | ||
86 | * Task called to check for existing tunnel and depending on that reopen channel | ||
87 | */ | ||
88 | static struct GNUNET_SCHEDULER_Task *get_peers_task; | ||
89 | |||
90 | /** | ||
91 | * Which test are we running? | ||
92 | */ | ||
93 | static int test; | ||
94 | |||
95 | /** | ||
96 | * String with test name | ||
97 | */ | ||
98 | static char *test_name; | ||
99 | |||
100 | /** | ||
101 | * Flag to send traffic leaf->root in speed tests to test BCK_ACK logic. | ||
102 | */ | ||
103 | static int test_backwards = GNUNET_NO; | ||
104 | |||
105 | /** | ||
106 | * How many packets to send. | ||
107 | */ | ||
108 | static unsigned int total_packets; | ||
109 | |||
110 | /** | ||
111 | * Time to wait for fast operations. | ||
112 | */ | ||
113 | static struct GNUNET_TIME_Relative short_time; | ||
114 | |||
115 | /** | ||
116 | * How many events have happened | ||
117 | */ | ||
118 | static int ok; | ||
119 | |||
120 | /** | ||
121 | * Number of events expected to conclude the test successfully. | ||
122 | */ | ||
123 | static int ok_goal; | ||
124 | |||
125 | /** | ||
126 | * Size of each test packet's payload | ||
127 | */ | ||
128 | static size_t size_payload = sizeof(uint32_t); | ||
129 | |||
130 | /** | ||
131 | * Operation to get peer ids. | ||
132 | */ | ||
133 | static struct GNUNET_TESTBED_Operation *t_op[2]; | ||
134 | |||
135 | /** | ||
136 | * Peer ids. | ||
137 | */ | ||
138 | static struct GNUNET_PeerIdentity *testpeer_id[2]; | ||
139 | |||
140 | /** | ||
141 | * Peer ids. | ||
142 | */ | ||
143 | static struct GNUNET_CONFIGURATION_Handle *p_cfg[2]; | ||
144 | |||
145 | /** | ||
146 | * Port ID | ||
147 | */ | ||
148 | static struct GNUNET_HashCode port; | ||
149 | |||
150 | /** | ||
151 | * Peer ids counter. | ||
152 | */ | ||
153 | static unsigned int peerinfo_task_cnt; | ||
154 | |||
155 | /** | ||
156 | * Is the setup initialized? | ||
157 | */ | ||
158 | static int initialized; | ||
159 | |||
160 | /** | ||
161 | * Number of payload packes sent. | ||
162 | */ | ||
163 | static int data_sent; | ||
164 | |||
165 | /** | ||
166 | * Number of payload packets received. | ||
167 | */ | ||
168 | static int data_received; | ||
169 | |||
170 | /** | ||
171 | * Number of payload packed acknowledgements sent. | ||
172 | */ | ||
173 | static int ack_sent; | ||
174 | |||
175 | /** | ||
176 | * Number of payload packed explicitly (app level) acknowledged. | ||
177 | */ | ||
178 | static int ack_received; | ||
179 | |||
180 | /** | ||
181 | * Total number of peers asked to run. | ||
182 | */ | ||
183 | static unsigned long long peers_requested; | ||
184 | |||
185 | /** | ||
186 | * Number of currently running peers (should be same as @c peers_requested). | ||
187 | */ | ||
188 | static unsigned long long peers_running; | ||
189 | |||
190 | /** | ||
191 | * Test context (to shut down). | ||
192 | */ | ||
193 | struct GNUNET_CADET_TEST_Context *test_ctx; | ||
194 | |||
195 | /** | ||
196 | * Task called to disconnect peers. | ||
197 | */ | ||
198 | static struct GNUNET_SCHEDULER_Task *disconnect_task; | ||
199 | |||
200 | /** | ||
201 | * Task called to reconnect peers. | ||
202 | */ | ||
203 | static struct GNUNET_SCHEDULER_Task *reconnect_task; | ||
204 | |||
205 | /** | ||
206 | * Task To perform tests | ||
207 | */ | ||
208 | static struct GNUNET_SCHEDULER_Task *test_task; | ||
209 | |||
210 | /** | ||
211 | * Task runnining #send_next_msg(). | ||
212 | */ | ||
213 | static struct GNUNET_SCHEDULER_Task *send_next_msg_task; | ||
214 | |||
215 | /** | ||
216 | * Channel handle for the root peer | ||
217 | */ | ||
218 | static struct GNUNET_CADET_Channel *outgoing_ch; | ||
219 | |||
220 | /** | ||
221 | * Channel handle for the dest peer | ||
222 | */ | ||
223 | static struct GNUNET_CADET_Channel *incoming_ch; | ||
224 | |||
225 | /** | ||
226 | * Time we started the data transmission (after channel has been established | ||
227 | * and initialized). | ||
228 | */ | ||
229 | static struct GNUNET_TIME_Absolute start_time; | ||
230 | |||
231 | /** | ||
232 | * Peers handle. | ||
233 | */ | ||
234 | static struct GNUNET_TESTBED_Peer **testbed_peers; | ||
235 | |||
236 | |||
237 | struct GNUNET_CADET_Handle **cadets_running; | ||
238 | |||
239 | /** | ||
240 | * Statistics operation handle. | ||
241 | */ | ||
242 | static struct GNUNET_TESTBED_Operation *stats_op; | ||
243 | |||
244 | /** | ||
245 | * Keepalives sent. | ||
246 | */ | ||
247 | static unsigned int ka_sent; | ||
248 | |||
249 | /** | ||
250 | * Keepalives received. | ||
251 | */ | ||
252 | static unsigned int ka_received; | ||
253 | |||
254 | /** | ||
255 | * How many messages were dropped by CADET because of full buffers? | ||
256 | */ | ||
257 | static unsigned int msg_dropped; | ||
258 | |||
259 | /** | ||
260 | * Drop the next cadet message of a given type.. | ||
261 | * | ||
262 | * @param mq message queue | ||
263 | * @param ccn client channel number. | ||
264 | * @param type of cadet message to be dropped. | ||
265 | */ | ||
266 | void | ||
267 | GNUNET_CADET_drop_message (struct GNUNET_MQ_Handle *mq, | ||
268 | struct GNUNET_CADET_ClientChannelNumber ccn, | ||
269 | uint16_t type); | ||
270 | |||
271 | /******************************************************************************/ | ||
272 | |||
273 | |||
274 | /******************************************************************************/ | ||
275 | |||
276 | |||
277 | /** | ||
278 | * Get the channel considered as the "target" or "receiver", depending on | ||
279 | * the test type and size. | ||
280 | * | ||
281 | * @return Channel handle of the target client, either 0 (for backward tests) | ||
282 | * or the last peer in the line (for other tests). | ||
283 | */ | ||
284 | static struct GNUNET_CADET_Channel * | ||
285 | get_target_channel () | ||
286 | { | ||
287 | if ((SPEED == test) && (GNUNET_YES == test_backwards)) | ||
288 | return outgoing_ch; | ||
289 | else | ||
290 | return incoming_ch; | ||
291 | } | ||
292 | |||
293 | |||
294 | /** | ||
295 | * Show the results of the test (banwidth achieved) and log them to GAUGER | ||
296 | */ | ||
297 | static void | ||
298 | show_end_data (void) | ||
299 | { | ||
300 | static struct GNUNET_TIME_Absolute end_time; | ||
301 | static struct GNUNET_TIME_Relative total_time; | ||
302 | |||
303 | end_time = GNUNET_TIME_absolute_get (); | ||
304 | total_time = GNUNET_TIME_absolute_get_difference (start_time, end_time); | ||
305 | fprintf (stderr, | ||
306 | "\nResults of test \"%s\"\n", | ||
307 | test_name); | ||
308 | fprintf (stderr, | ||
309 | "Test time %s\n", | ||
310 | GNUNET_STRINGS_relative_time_to_string (total_time, GNUNET_YES)); | ||
311 | fprintf (stderr, | ||
312 | "Test bandwidth: %f kb/s\n", | ||
313 | 4 * total_packets * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms | ||
314 | fprintf (stderr, | ||
315 | "Test throughput: %f packets/s\n\n", | ||
316 | total_packets * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms | ||
317 | GAUGER ("CADET", | ||
318 | test_name, | ||
319 | total_packets * 1000.0 / (total_time.rel_value_us / 1000), | ||
320 | "packets/s"); | ||
321 | } | ||
322 | |||
323 | |||
324 | /** | ||
325 | * Disconnect from cadet services af all peers, call shutdown. | ||
326 | * | ||
327 | * @param cls Closure (line number from which termination was requested). | ||
328 | * @param tc Task Context. | ||
329 | */ | ||
330 | static void | ||
331 | disconnect_cadet_peers (void *cls) | ||
332 | { | ||
333 | long line = (long) cls; | ||
334 | |||
335 | disconnect_task = NULL; | ||
336 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
337 | "disconnecting cadet service of peers, called from line %ld\n", | ||
338 | line); | ||
339 | for (unsigned int i = 0; i < 2; i++) | ||
340 | { | ||
341 | GNUNET_TESTBED_operation_done (t_op[i]); | ||
342 | } | ||
343 | if (NULL != outgoing_ch) | ||
344 | { | ||
345 | GNUNET_CADET_channel_destroy (outgoing_ch); | ||
346 | outgoing_ch = NULL; | ||
347 | } | ||
348 | if (NULL != incoming_ch) | ||
349 | { | ||
350 | GNUNET_CADET_channel_destroy (incoming_ch); | ||
351 | incoming_ch = NULL; | ||
352 | } | ||
353 | GNUNET_CADET_TEST_cleanup (test_ctx); | ||
354 | GNUNET_SCHEDULER_shutdown (); | ||
355 | } | ||
356 | |||
357 | |||
358 | /** | ||
359 | * Shut down peergroup, clean up. | ||
360 | * | ||
361 | * @param cls Closure (unused). | ||
362 | * @param tc Task Context. | ||
363 | */ | ||
364 | static void | ||
365 | shutdown_task (void *cls) | ||
366 | { | ||
367 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
368 | "Ending test.\n"); | ||
369 | if (NULL != send_next_msg_task) | ||
370 | { | ||
371 | GNUNET_SCHEDULER_cancel (send_next_msg_task); | ||
372 | send_next_msg_task = NULL; | ||
373 | } | ||
374 | if (NULL != test_task) | ||
375 | { | ||
376 | GNUNET_SCHEDULER_cancel (test_task); | ||
377 | test_task = NULL; | ||
378 | } | ||
379 | if (NULL != disconnect_task) | ||
380 | { | ||
381 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
382 | disconnect_task = | ||
383 | GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, | ||
384 | (void *) __LINE__); | ||
385 | } | ||
386 | } | ||
387 | |||
388 | |||
389 | /** | ||
390 | * Stats callback. Finish the stats testbed operation and when all stats have | ||
391 | * been iterated, shutdown the test. | ||
392 | * | ||
393 | * @param cls Closure (line number from which termination was requested). | ||
394 | * @param op the operation that has been finished | ||
395 | * @param emsg error message in case the operation has failed; will be NULL if | ||
396 | * operation has executed successfully. | ||
397 | */ | ||
398 | static void | ||
399 | stats_cont (void *cls, | ||
400 | struct GNUNET_TESTBED_Operation *op, | ||
401 | const char *emsg) | ||
402 | { | ||
403 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
404 | "KA sent: %u, KA received: %u\n", | ||
405 | ka_sent, | ||
406 | ka_received); | ||
407 | if (((KEEPALIVE == test) || (REOPEN == test)) && | ||
408 | ((ka_sent < 2) || (ka_sent > ka_received + 1))) | ||
409 | { | ||
410 | GNUNET_break (0); | ||
411 | ok--; | ||
412 | } | ||
413 | GNUNET_TESTBED_operation_done (stats_op); | ||
414 | |||
415 | if (NULL != disconnect_task) | ||
416 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
417 | disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, | ||
418 | cls); | ||
419 | } | ||
420 | |||
421 | |||
422 | /** | ||
423 | * Process statistic values. | ||
424 | * | ||
425 | * @param cls closure (line number, unused) | ||
426 | * @param peer the peer the statistic belong to | ||
427 | * @param subsystem name of subsystem that created the statistic | ||
428 | * @param name the name of the datum | ||
429 | * @param value the current value | ||
430 | * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not | ||
431 | * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration | ||
432 | */ | ||
433 | static int | ||
434 | stats_iterator (void *cls, | ||
435 | const struct GNUNET_TESTBED_Peer *peer, | ||
436 | const char *subsystem, | ||
437 | const char *name, | ||
438 | uint64_t value, | ||
439 | int is_persistent) | ||
440 | { | ||
441 | static const char *s_sent = "# keepalives sent"; | ||
442 | static const char *s_recv = "# keepalives received"; | ||
443 | static const char *rdrops = "# messages dropped due to full buffer"; | ||
444 | static const char *cdrops = "# messages dropped due to slow client"; | ||
445 | uint32_t i; | ||
446 | |||
447 | i = GNUNET_TESTBED_get_index (peer); | ||
448 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "STATS PEER %u - %s [%s]: %llu\n", i, | ||
449 | subsystem, name, (unsigned long long) value); | ||
450 | if ((0 == strncmp (s_sent, name, strlen (s_sent))) && (0 == i)) | ||
451 | ka_sent = value; | ||
452 | if ((0 == strncmp (s_recv, name, strlen (s_recv))) && (peers_requested - 1 == | ||
453 | i) ) | ||
454 | ka_received = value; | ||
455 | if (0 == strncmp (rdrops, name, strlen (rdrops))) | ||
456 | msg_dropped += value; | ||
457 | if (0 == strncmp (cdrops, name, strlen (cdrops))) | ||
458 | msg_dropped += value; | ||
459 | |||
460 | return GNUNET_OK; | ||
461 | } | ||
462 | |||
463 | |||
464 | /** | ||
465 | * Task to gather all statistics. | ||
466 | * | ||
467 | * @param cls Closure (line from which the task was scheduled). | ||
468 | */ | ||
469 | static void | ||
470 | gather_stats_and_exit (void *cls) | ||
471 | { | ||
472 | long l = (long) cls; | ||
473 | |||
474 | disconnect_task = NULL; | ||
475 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
476 | "gathering statistics from line %ld\n", | ||
477 | l); | ||
478 | if (NULL != outgoing_ch) | ||
479 | { | ||
480 | GNUNET_CADET_channel_destroy (outgoing_ch); | ||
481 | outgoing_ch = NULL; | ||
482 | } | ||
483 | stats_op = GNUNET_TESTBED_get_statistics (peers_running, | ||
484 | testbed_peers, | ||
485 | "cadet", | ||
486 | NULL, | ||
487 | &stats_iterator, | ||
488 | stats_cont, | ||
489 | cls); | ||
490 | } | ||
491 | |||
492 | |||
493 | /** | ||
494 | * Send a message on the channel with the appropriate size and payload. | ||
495 | * | ||
496 | * Update the appropriate *_sent counter. | ||
497 | * | ||
498 | * @param channel Channel to send the message on. | ||
499 | */ | ||
500 | static void | ||
501 | send_test_message (struct GNUNET_CADET_Channel *channel); | ||
502 | |||
503 | /** | ||
504 | * Check if payload is sane (size contains payload). | ||
505 | * | ||
506 | * @param cls should match #ch | ||
507 | * @param message The actual message. | ||
508 | * @return #GNUNET_OK to keep the channel open, | ||
509 | * #GNUNET_SYSERR to close it (signal serious error). | ||
510 | */ | ||
511 | static int | ||
512 | check_data (void *cls, | ||
513 | const struct GNUNET_MessageHeader *message); | ||
514 | |||
515 | /** | ||
516 | * Function is called whenever a message is received. | ||
517 | * | ||
518 | * @param cls closure (set from GNUNET_CADET_connect(), peer number) | ||
519 | * @param message the actual message | ||
520 | */ | ||
521 | static void | ||
522 | handle_data (void *cls, | ||
523 | const struct GNUNET_MessageHeader *message); | ||
524 | |||
525 | /** | ||
526 | * Function called whenever an MQ-channel is destroyed, unless the destruction | ||
527 | * was requested by #GNUNET_CADET_channel_destroy. | ||
528 | * It must NOT call #GNUNET_CADET_channel_destroy on the channel. | ||
529 | * | ||
530 | * It should clean up any associated state, including cancelling any pending | ||
531 | * transmission on this channel. | ||
532 | * | ||
533 | * @param cls Channel closure (channel wrapper). | ||
534 | * @param channel Connection to the other end (henceforth invalid). | ||
535 | */ | ||
536 | static void | ||
537 | disconnect_handler (void *cls, | ||
538 | const struct GNUNET_CADET_Channel *channel); | ||
539 | |||
540 | static struct GNUNET_PeerIdentity * | ||
541 | get_from_p_ids () | ||
542 | { | ||
543 | if (0 < GNUNET_memcmp (testpeer_id[0], testpeer_id[1])) | ||
544 | { | ||
545 | return testpeer_id[1]; | ||
546 | } | ||
547 | else | ||
548 | { | ||
549 | return testpeer_id[0]; | ||
550 | } | ||
551 | } | ||
552 | |||
553 | |||
554 | static struct GNUNET_CADET_Handle * | ||
555 | get_from_cadets () | ||
556 | { | ||
557 | |||
558 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "1\n"); | ||
559 | if (0 < GNUNET_memcmp (testpeer_id[0], testpeer_id[1])) | ||
560 | { | ||
561 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "standard peer\n"); | ||
562 | return cadets_running[0]; | ||
563 | } | ||
564 | else | ||
565 | { | ||
566 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "the other peer\n"); | ||
567 | return cadets_running[peers_running - 1]; | ||
568 | } | ||
569 | |||
570 | } | ||
571 | |||
572 | |||
573 | static unsigned int | ||
574 | get_peer_nr (int outgoing) | ||
575 | { | ||
576 | if (0 < GNUNET_memcmp (testpeer_id[0], testpeer_id[1])) | ||
577 | { | ||
578 | return GNUNET_YES == outgoing ? 0 : peers_running - 1; | ||
579 | } | ||
580 | else | ||
581 | { | ||
582 | return GNUNET_YES == outgoing ? peers_running - 1 : 0; | ||
583 | } | ||
584 | } | ||
585 | |||
586 | |||
587 | /** | ||
588 | * Task to reconnect to other peer. | ||
589 | * | ||
590 | * @param cls Closure (line from which the task was scheduled). | ||
591 | */ | ||
592 | static void | ||
593 | reconnect_op (void *cls) | ||
594 | { | ||
595 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
596 | GNUNET_MQ_hd_var_size (data, | ||
597 | GNUNET_MESSAGE_TYPE_DUMMY, | ||
598 | struct GNUNET_MessageHeader, | ||
599 | NULL), | ||
600 | GNUNET_MQ_handler_end () | ||
601 | }; | ||
602 | long l = (long) cls; | ||
603 | struct CadetTestChannelWrapper *ch; | ||
604 | static struct GNUNET_PeerIdentity *p_id; | ||
605 | static struct GNUNET_CADET_Handle *h1; | ||
606 | |||
607 | reconnect_task = NULL; | ||
608 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
609 | "reconnecting from line %ld\n", | ||
610 | l); | ||
611 | if (NULL != outgoing_ch) | ||
612 | { | ||
613 | GNUNET_CADET_channel_destroy (outgoing_ch); | ||
614 | outgoing_ch = NULL; | ||
615 | } | ||
616 | ch = GNUNET_new (struct CadetTestChannelWrapper); | ||
617 | |||
618 | p_id = get_from_p_ids (); | ||
619 | h1 = get_from_cadets (); | ||
620 | |||
621 | outgoing_ch = GNUNET_CADET_channel_create (h1, | ||
622 | ch, | ||
623 | p_id, | ||
624 | &port, | ||
625 | NULL, | ||
626 | &disconnect_handler, | ||
627 | handlers); | ||
628 | ch->ch = outgoing_ch; | ||
629 | send_test_message (outgoing_ch); | ||
630 | } | ||
631 | |||
632 | |||
633 | void | ||
634 | reopen_channel () | ||
635 | { | ||
636 | struct CadetTestChannelWrapper *ch; | ||
637 | static struct GNUNET_CADET_Handle *h1; | ||
638 | static struct GNUNET_PeerIdentity *p_id; | ||
639 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
640 | GNUNET_MQ_hd_var_size (data, | ||
641 | GNUNET_MESSAGE_TYPE_DUMMY, | ||
642 | struct GNUNET_MessageHeader, | ||
643 | NULL), | ||
644 | GNUNET_MQ_handler_end () | ||
645 | }; | ||
646 | |||
647 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "creating channel again\n"); | ||
648 | p_id = get_from_p_ids (); | ||
649 | h1 = get_from_cadets (); | ||
650 | |||
651 | ch = GNUNET_new (struct CadetTestChannelWrapper); | ||
652 | outgoing_ch = GNUNET_CADET_channel_create (h1, | ||
653 | ch, | ||
654 | p_id, | ||
655 | &port, | ||
656 | NULL, | ||
657 | &disconnect_handler, | ||
658 | handlers); | ||
659 | ch->ch = outgoing_ch; | ||
660 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
661 | "Sending second test data (after destroying the channel) on channel %p...\n", | ||
662 | outgoing_ch); | ||
663 | send_test_message (outgoing_ch); | ||
664 | } | ||
665 | |||
666 | |||
667 | static void | ||
668 | peers_callback (void *cls, const struct GNUNET_CADET_PeerListEntry *ple); | ||
669 | |||
670 | /** | ||
671 | * We ask the monitoring api for all the peers. | ||
672 | */ | ||
673 | static void | ||
674 | get_peers (void *cls) | ||
675 | { | ||
676 | |||
677 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
678 | "requesting peers info!\n"); | ||
679 | plo = GNUNET_CADET_list_peers (p_cfg[get_peer_nr (GNUNET_YES)], | ||
680 | &peers_callback, NULL); | ||
681 | |||
682 | } | ||
683 | |||
684 | |||
685 | /** | ||
686 | * Method called to retrieve information about all peers in CADET, called | ||
687 | * once per peer. | ||
688 | * | ||
689 | * After last peer has been reported, an additional call with NULL is done. | ||
690 | * | ||
691 | * We check the peer we are interested in, if we have a tunnel. If not, we | ||
692 | * reopen the channel | ||
693 | * | ||
694 | * @param cls Closure. | ||
695 | * @param ple information about peer, or NULL on "EOF". | ||
696 | */ | ||
697 | static void | ||
698 | peers_callback (void *cls, const struct GNUNET_CADET_PeerListEntry *ple) | ||
699 | { | ||
700 | |||
701 | const struct GNUNET_PeerIdentity *p_id; | ||
702 | const struct GNUNET_PeerIdentity *peer; | ||
703 | |||
704 | |||
705 | peer = &ple->peer; | ||
706 | |||
707 | if (NULL == ple) | ||
708 | { | ||
709 | plo = NULL; | ||
710 | return; | ||
711 | } | ||
712 | p_id = get_from_p_ids (); | ||
713 | |||
714 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
715 | "ple->peer %s\n", | ||
716 | GNUNET_i2s_full (&ple->peer)); | ||
717 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
718 | "p_id %s\n", | ||
719 | GNUNET_i2s_full (p_id)); | ||
720 | |||
721 | if ((0 == GNUNET_memcmp (&ple->peer, p_id)) && ple->have_tunnel) | ||
722 | { | ||
723 | |||
724 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
725 | "schedule get_peers again?\n"); | ||
726 | get_peers_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME, | ||
727 | &get_peers, | ||
728 | NULL); | ||
729 | |||
730 | } | ||
731 | else if (0 == GNUNET_memcmp (&ple->peer, p_id) ) | ||
732 | { | ||
733 | |||
734 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
735 | "reopen channel\n"); | ||
736 | |||
737 | reopen_channel (); | ||
738 | |||
739 | } | ||
740 | } | ||
741 | |||
742 | |||
743 | /** | ||
744 | * Function called whenever an MQ-channel is destroyed, unless the destruction | ||
745 | * was requested by #GNUNET_CADET_channel_destroy. | ||
746 | * It must NOT call #GNUNET_CADET_channel_destroy on the channel. | ||
747 | * | ||
748 | * It should clean up any associated state, including cancelling any pending | ||
749 | * transmission on this channel. | ||
750 | * | ||
751 | * @param cls Channel closure (channel wrapper). | ||
752 | * @param channel Connection to the other end (henceforth invalid). | ||
753 | */ | ||
754 | static void | ||
755 | disconnect_handler (void *cls, | ||
756 | const struct GNUNET_CADET_Channel *channel) | ||
757 | { | ||
758 | struct CadetTestChannelWrapper *ch_w = cls; | ||
759 | |||
760 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
761 | "Channel disconnected at ok=%d\n", | ||
762 | ok); | ||
763 | GNUNET_assert (ch_w->ch == channel); | ||
764 | |||
765 | if ((DESTROY == test) && (3 == ok)) | ||
766 | { | ||
767 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
768 | "Reopen channel task!\n"); | ||
769 | if (NULL == get_peers_task) | ||
770 | { | ||
771 | get_peers_task = GNUNET_SCHEDULER_add_now (&get_peers, | ||
772 | NULL); | ||
773 | } | ||
774 | return; | ||
775 | } | ||
776 | |||
777 | if (channel == incoming_ch) | ||
778 | { | ||
779 | ok++; | ||
780 | incoming_ch = NULL; | ||
781 | } | ||
782 | else if (outgoing_ch == channel) | ||
783 | { | ||
784 | if (P2P_SIGNAL == test) | ||
785 | { | ||
786 | ok++; | ||
787 | } | ||
788 | outgoing_ch = NULL; | ||
789 | } | ||
790 | else | ||
791 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
792 | "Unknown channel! %p\n", | ||
793 | channel); | ||
794 | if ((NULL != disconnect_task) && (REOPEN != test)) | ||
795 | { | ||
796 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
797 | disconnect_task = | ||
798 | GNUNET_SCHEDULER_add_now (&gather_stats_and_exit, | ||
799 | (void *) __LINE__); | ||
800 | } | ||
801 | else if ((NULL != reconnect_task) && (REOPEN == test)) | ||
802 | { | ||
803 | GNUNET_SCHEDULER_cancel (reconnect_task); | ||
804 | reconnect_task = | ||
805 | GNUNET_SCHEDULER_add_now (&reconnect_op, | ||
806 | (void *) __LINE__); | ||
807 | } | ||
808 | GNUNET_free (ch_w); | ||
809 | } | ||
810 | |||
811 | |||
812 | /** | ||
813 | * Abort test: schedule disconnect and shutdown immediately | ||
814 | * | ||
815 | * @param line Line in the code the abort is requested from (__LINE__). | ||
816 | */ | ||
817 | static void | ||
818 | abort_test (long line) | ||
819 | { | ||
820 | if (NULL != disconnect_task) | ||
821 | { | ||
822 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
823 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
824 | "Aborting test from %ld\n", | ||
825 | line); | ||
826 | disconnect_task = | ||
827 | GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, | ||
828 | (void *) line); | ||
829 | } | ||
830 | } | ||
831 | |||
832 | |||
833 | /** | ||
834 | * Send a message on the channel with the appropriate size and payload. | ||
835 | * | ||
836 | * Update the appropriate *_sent counter. | ||
837 | * | ||
838 | * @param channel Channel to send the message on. | ||
839 | */ | ||
840 | static void | ||
841 | send_test_message (struct GNUNET_CADET_Channel *channel) | ||
842 | { | ||
843 | struct GNUNET_MQ_Envelope *env; | ||
844 | struct GNUNET_MessageHeader *msg; | ||
845 | uint32_t *data; | ||
846 | int payload; | ||
847 | int size; | ||
848 | |||
849 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
850 | "Sending test message on channel %u\n", | ||
851 | channel->ccn.channel_of_client); | ||
852 | size = size_payload; | ||
853 | if (GNUNET_NO == initialized) | ||
854 | { | ||
855 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending INITIALIZER\n"); | ||
856 | size += 1000; | ||
857 | payload = data_sent; | ||
858 | if (SPEED_ACK == test) // FIXME unify SPEED_ACK with an initializer | ||
859 | data_sent++; | ||
860 | } | ||
861 | else if ((SPEED == test) || (SPEED_ACK == test)) | ||
862 | { | ||
863 | if (get_target_channel () == channel) | ||
864 | { | ||
865 | payload = ack_sent; | ||
866 | size += ack_sent; | ||
867 | ack_sent++; | ||
868 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
869 | "Sending ACK %u [%d bytes]\n", | ||
870 | payload, size); | ||
871 | } | ||
872 | else | ||
873 | { | ||
874 | payload = data_sent; | ||
875 | size += data_sent; | ||
876 | data_sent++; | ||
877 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
878 | "Sending DATA %u [%d bytes]\n", | ||
879 | data_sent, size); | ||
880 | } | ||
881 | } | ||
882 | else if (FORWARD == test) | ||
883 | { | ||
884 | payload = ack_sent; | ||
885 | } | ||
886 | else if (P2P_SIGNAL == test) | ||
887 | { | ||
888 | payload = data_sent; | ||
889 | } | ||
890 | else if (REOPEN == test) | ||
891 | { | ||
892 | payload = data_sent; | ||
893 | data_sent++; | ||
894 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
895 | "Sending DATA %u [%d bytes]\n", | ||
896 | data_sent, size); | ||
897 | } | ||
898 | else if (DESTROY == test) | ||
899 | { | ||
900 | payload = data_sent; | ||
901 | } | ||
902 | else | ||
903 | { | ||
904 | GNUNET_assert (0); | ||
905 | } | ||
906 | env = GNUNET_MQ_msg_extra (msg, size, GNUNET_MESSAGE_TYPE_DUMMY); | ||
907 | |||
908 | data = (uint32_t *) &msg[1]; | ||
909 | *data = htonl (payload); | ||
910 | GNUNET_MQ_send (GNUNET_CADET_get_mq (channel), env); | ||
911 | } | ||
912 | |||
913 | |||
914 | /** | ||
915 | * Task to request a new data transmission in a SPEED test, without waiting | ||
916 | * for previous messages to be sent/arrrive. | ||
917 | * | ||
918 | * @param cls Closure (unused). | ||
919 | */ | ||
920 | static void | ||
921 | send_next_msg (void *cls) | ||
922 | { | ||
923 | struct GNUNET_CADET_Channel *channel; | ||
924 | |||
925 | send_next_msg_task = NULL; | ||
926 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
927 | "Sending next message: %d\n", | ||
928 | data_sent); | ||
929 | |||
930 | channel = GNUNET_YES == test_backwards ? incoming_ch : outgoing_ch; | ||
931 | GNUNET_assert (NULL != channel); | ||
932 | GNUNET_assert (SPEED == test); | ||
933 | send_test_message (channel); | ||
934 | if (data_sent < total_packets) | ||
935 | { | ||
936 | /* SPEED test: Send all messages as soon as possible */ | ||
937 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
938 | "Scheduling message %d\n", | ||
939 | data_sent + 1); | ||
940 | send_next_msg_task = | ||
941 | GNUNET_SCHEDULER_add_delayed (SEND_INTERVAL, | ||
942 | &send_next_msg, | ||
943 | NULL); | ||
944 | } | ||
945 | } | ||
946 | |||
947 | |||
948 | /** | ||
949 | * Every few messages cancel the timeout task and re-schedule it again, to | ||
950 | * avoid timing out when traffic keeps coming. | ||
951 | * | ||
952 | * @param line Code line number to log if a timeout occurs. | ||
953 | */ | ||
954 | static void | ||
955 | reschedule_timeout_task (long line) | ||
956 | { | ||
957 | if ((ok % 10) == 0) | ||
958 | { | ||
959 | if (NULL != disconnect_task) | ||
960 | { | ||
961 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
962 | "reschedule timeout every 10 messages\n"); | ||
963 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
964 | disconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, | ||
965 | &gather_stats_and_exit, | ||
966 | (void *) line); | ||
967 | } | ||
968 | } | ||
969 | } | ||
970 | |||
971 | |||
972 | /** | ||
973 | * Check if payload is sane (size contains payload). | ||
974 | * | ||
975 | * @param cls should match #ch | ||
976 | * @param message The actual message. | ||
977 | * @return #GNUNET_OK to keep the channel open, | ||
978 | * #GNUNET_SYSERR to close it (signal serious error). | ||
979 | */ | ||
980 | static int | ||
981 | check_data (void *cls, | ||
982 | const struct GNUNET_MessageHeader *message) | ||
983 | { | ||
984 | return GNUNET_OK; /* all is well-formed */ | ||
985 | } | ||
986 | |||
987 | |||
988 | /** | ||
989 | * Function is called whenever a message is received. | ||
990 | * | ||
991 | * @param cls closure (set from GNUNET_CADET_connect(), peer number) | ||
992 | * @param message the actual message | ||
993 | */ | ||
994 | static void | ||
995 | handle_data (void *cls, | ||
996 | const struct GNUNET_MessageHeader *message) | ||
997 | { | ||
998 | struct CadetTestChannelWrapper *ch = cls; | ||
999 | struct GNUNET_CADET_Channel *channel = ch->ch; | ||
1000 | uint32_t *data; | ||
1001 | uint32_t payload; | ||
1002 | int *counter; | ||
1003 | |||
1004 | ok++; | ||
1005 | GNUNET_CADET_receive_done (channel); | ||
1006 | counter = get_target_channel () == channel ? &data_received : &ack_received; | ||
1007 | |||
1008 | reschedule_timeout_task ((long) __LINE__); | ||
1009 | |||
1010 | if (channel == outgoing_ch) | ||
1011 | { | ||
1012 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1013 | "Root client got a message.\n"); | ||
1014 | } | ||
1015 | else if (channel == incoming_ch) | ||
1016 | { | ||
1017 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1018 | "Leaf client got a message.\n"); | ||
1019 | } | ||
1020 | else | ||
1021 | { | ||
1022 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1023 | "Unknown channel %p.\n", | ||
1024 | channel); | ||
1025 | GNUNET_assert (0); | ||
1026 | } | ||
1027 | |||
1028 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1029 | "handle_data ok: (%d/%d)\n", | ||
1030 | ok, | ||
1031 | ok_goal); | ||
1032 | data = (uint32_t *) &message[1]; | ||
1033 | payload = ntohl (*data); | ||
1034 | if (payload == *counter) | ||
1035 | { | ||
1036 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1037 | " payload as expected: %u\n", | ||
1038 | payload); | ||
1039 | } | ||
1040 | else | ||
1041 | { | ||
1042 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1043 | " payload %u, expected: %u\n", | ||
1044 | payload, *counter); | ||
1045 | } | ||
1046 | |||
1047 | if (DESTROY == test) | ||
1048 | { | ||
1049 | if (2 == ok) | ||
1050 | { | ||
1051 | ok++; | ||
1052 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1053 | "dropping message ok: (%d/%d)\n", | ||
1054 | ok, | ||
1055 | ok_goal); | ||
1056 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1057 | "TEST ID 0: %s\n", | ||
1058 | GNUNET_i2s (testpeer_id[0])); | ||
1059 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1060 | "TEST ID 1: %s\n", | ||
1061 | GNUNET_i2s (testpeer_id[1])); | ||
1062 | |||
1063 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "dropping message\n"); | ||
1064 | GNUNET_CADET_drop_message (GNUNET_CADET_get_mq (outgoing_ch), | ||
1065 | outgoing_ch->ccn, | ||
1066 | GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY); | ||
1067 | if (NULL != outgoing_ch) | ||
1068 | { | ||
1069 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1070 | "Destroying channel %p...\n", | ||
1071 | outgoing_ch); | ||
1072 | GNUNET_CADET_channel_destroy (outgoing_ch); | ||
1073 | outgoing_ch = NULL; | ||
1074 | } | ||
1075 | } | ||
1076 | else if (5 == ok) | ||
1077 | { | ||
1078 | ok++; | ||
1079 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1080 | "destroy test finished ok: (%d/%d)\n", | ||
1081 | ok, | ||
1082 | ok_goal); | ||
1083 | disconnect_task = | ||
1084 | GNUNET_SCHEDULER_add_now (&gather_stats_and_exit, | ||
1085 | (void *) __LINE__); | ||
1086 | // End of DESTROY test. | ||
1087 | } | ||
1088 | } | ||
1089 | |||
1090 | if (GNUNET_NO == initialized) | ||
1091 | { | ||
1092 | initialized = GNUNET_YES; | ||
1093 | start_time = GNUNET_TIME_absolute_get (); | ||
1094 | if (SPEED == test) | ||
1095 | { | ||
1096 | GNUNET_assert (incoming_ch == channel); | ||
1097 | send_next_msg_task = GNUNET_SCHEDULER_add_now (&send_next_msg, | ||
1098 | NULL); | ||
1099 | return; | ||
1100 | } | ||
1101 | } | ||
1102 | |||
1103 | (*counter)++; | ||
1104 | if (get_target_channel () == channel) /* Got "data" */ | ||
1105 | { | ||
1106 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received data %u\n", data_received); | ||
1107 | if ((DESTROY != test) && ((SPEED != test) || ( (ok_goal - 2) == ok)) ) | ||
1108 | { | ||
1109 | /* Send ACK */ | ||
1110 | send_test_message (channel); | ||
1111 | return; | ||
1112 | } | ||
1113 | else | ||
1114 | { | ||
1115 | if (data_received < total_packets) | ||
1116 | return; | ||
1117 | } | ||
1118 | } | ||
1119 | else /* Got "ack" */ | ||
1120 | { | ||
1121 | if ((SPEED_ACK == test) || (SPEED == test) ) | ||
1122 | { | ||
1123 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received ack %u\n", ack_received); | ||
1124 | /* Send more data */ | ||
1125 | send_test_message (channel); | ||
1126 | if ((ack_received < total_packets) && (SPEED != test) ) | ||
1127 | return; | ||
1128 | if ((ok == 2) && (SPEED == test) ) | ||
1129 | return; | ||
1130 | show_end_data (); | ||
1131 | } | ||
1132 | if (test == P2P_SIGNAL) | ||
1133 | { | ||
1134 | GNUNET_CADET_channel_destroy (incoming_ch); | ||
1135 | incoming_ch = NULL; | ||
1136 | } | ||
1137 | else | ||
1138 | { | ||
1139 | GNUNET_CADET_channel_destroy (outgoing_ch); | ||
1140 | outgoing_ch = NULL; | ||
1141 | } | ||
1142 | } | ||
1143 | } | ||
1144 | |||
1145 | |||
1146 | /** | ||
1147 | * Method called whenever a peer connects to a port in MQ-based CADET. | ||
1148 | * | ||
1149 | * @param cls Closure from #GNUNET_CADET_open_port (peer # as long). | ||
1150 | * @param channel New handle to the channel. | ||
1151 | * @param source Peer that started this channel. | ||
1152 | * @return Closure for the incoming @a channel. It's given to: | ||
1153 | * - The #GNUNET_CADET_DisconnectEventHandler (given to | ||
1154 | * #GNUNET_CADET_open_port) when the channel dies. | ||
1155 | * - Each the #GNUNET_MQ_MessageCallback handlers for each message | ||
1156 | * received on the @a channel. | ||
1157 | */ | ||
1158 | static void * | ||
1159 | connect_handler (void *cls, | ||
1160 | struct GNUNET_CADET_Channel *channel, | ||
1161 | const struct GNUNET_PeerIdentity *source) | ||
1162 | { | ||
1163 | struct CadetTestChannelWrapper *ch; | ||
1164 | long peer = (long) cls; | ||
1165 | |||
1166 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1167 | "Incoming channel from %s to %ld: %p\n", | ||
1168 | GNUNET_i2s (source), | ||
1169 | peer, | ||
1170 | channel); | ||
1171 | ok++; | ||
1172 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1173 | "connect_handler ok: (%d/%d)\n", | ||
1174 | ok, | ||
1175 | ok_goal); | ||
1176 | |||
1177 | if (peer == get_peer_nr (GNUNET_NO)) | ||
1178 | { | ||
1179 | if ((DESTROY != test) && (NULL != incoming_ch)) | ||
1180 | { | ||
1181 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1182 | "Duplicate incoming channel for client %lu\n", | ||
1183 | (long) cls); | ||
1184 | GNUNET_assert (0); | ||
1185 | } | ||
1186 | incoming_ch = channel; | ||
1187 | } | ||
1188 | else | ||
1189 | { | ||
1190 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1191 | "Incoming channel for unexpected peer #%lu\n", | ||
1192 | (long) cls); | ||
1193 | GNUNET_assert (0); | ||
1194 | } | ||
1195 | if ((NULL != disconnect_task) && (REOPEN != test) && (DESTROY != test)) | ||
1196 | { | ||
1197 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
1198 | disconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, | ||
1199 | &gather_stats_and_exit, | ||
1200 | (void *) __LINE__); | ||
1201 | } | ||
1202 | else if ((NULL != disconnect_task) && (REOPEN == test)) | ||
1203 | { | ||
1204 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
1205 | disconnect_task = GNUNET_SCHEDULER_add_delayed ( | ||
1206 | GNUNET_TIME_relative_multiply (short_time, 2), | ||
1207 | &gather_stats_and_exit, | ||
1208 | (void *) __LINE__); | ||
1209 | } | ||
1210 | |||
1211 | if ((NULL != reconnect_task) && (REOPEN == test)) | ||
1212 | { | ||
1213 | GNUNET_SCHEDULER_cancel (reconnect_task); | ||
1214 | reconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, | ||
1215 | &reconnect_op, | ||
1216 | (void *) __LINE__); | ||
1217 | } | ||
1218 | |||
1219 | |||
1220 | /* TODO: cannot return channel as-is, in order to unify the data handlers */ | ||
1221 | ch = GNUNET_new (struct CadetTestChannelWrapper); | ||
1222 | ch->ch = channel; | ||
1223 | |||
1224 | return ch; | ||
1225 | } | ||
1226 | |||
1227 | |||
1228 | /** | ||
1229 | * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE CADET SERVICES. | ||
1230 | * | ||
1231 | * Testcase continues when the root receives confirmation of connected peers, | ||
1232 | * on callback function ch. | ||
1233 | * | ||
1234 | * @param cls Closure (unused). | ||
1235 | */ | ||
1236 | static void | ||
1237 | start_test (void *cls) | ||
1238 | { | ||
1239 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
1240 | GNUNET_MQ_hd_var_size (data, | ||
1241 | GNUNET_MESSAGE_TYPE_DUMMY, | ||
1242 | struct GNUNET_MessageHeader, | ||
1243 | NULL), | ||
1244 | GNUNET_MQ_handler_end () | ||
1245 | }; | ||
1246 | struct CadetTestChannelWrapper *ch; | ||
1247 | static struct GNUNET_CADET_Handle *h1; | ||
1248 | static struct GNUNET_PeerIdentity *p_id; | ||
1249 | |||
1250 | test_task = NULL; | ||
1251 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "start_test: %s\n", test_name); | ||
1252 | if (NULL != disconnect_task) | ||
1253 | { | ||
1254 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
1255 | disconnect_task = NULL; | ||
1256 | } | ||
1257 | |||
1258 | if (SPEED_REL == test) | ||
1259 | { | ||
1260 | test = SPEED; | ||
1261 | } | ||
1262 | |||
1263 | p_id = get_from_p_ids (); | ||
1264 | h1 = get_from_cadets (); | ||
1265 | |||
1266 | ch = GNUNET_new (struct CadetTestChannelWrapper); | ||
1267 | outgoing_ch = GNUNET_CADET_channel_create (h1, | ||
1268 | ch, | ||
1269 | p_id, | ||
1270 | &port, | ||
1271 | NULL, | ||
1272 | &disconnect_handler, | ||
1273 | handlers); | ||
1274 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "channel created\n"); | ||
1275 | |||
1276 | ch->ch = outgoing_ch; | ||
1277 | |||
1278 | if (DESTROY != test) | ||
1279 | disconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, | ||
1280 | &gather_stats_and_exit, | ||
1281 | (void *) __LINE__); | ||
1282 | if (KEEPALIVE == test) | ||
1283 | return; /* Don't send any data. */ | ||
1284 | |||
1285 | data_received = 0; | ||
1286 | data_sent = 0; | ||
1287 | ack_received = 0; | ||
1288 | ack_sent = 0; | ||
1289 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1290 | "Sending data initializer on channel %p...\n", | ||
1291 | outgoing_ch); | ||
1292 | send_test_message (outgoing_ch); | ||
1293 | if (REOPEN == test) | ||
1294 | { | ||
1295 | reconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, | ||
1296 | &reconnect_op, | ||
1297 | (void *) __LINE__); | ||
1298 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
1299 | disconnect_task = GNUNET_SCHEDULER_add_delayed ( | ||
1300 | GNUNET_TIME_relative_multiply (short_time, 2), | ||
1301 | &gather_stats_and_exit, | ||
1302 | (void *) __LINE__); | ||
1303 | } | ||
1304 | } | ||
1305 | |||
1306 | |||
1307 | /** | ||
1308 | * Callback to be called when the requested peer information is available | ||
1309 | * | ||
1310 | * @param cls the closure from GNUNET_TESTBED_peer_getinformation() | ||
1311 | * @param op the operation this callback corresponds to | ||
1312 | * @param pinfo the result; will be NULL if the operation has failed | ||
1313 | * @param emsg error message if the operation has failed; | ||
1314 | * NULL if the operation is successful | ||
1315 | */ | ||
1316 | static void | ||
1317 | pi_cb (void *cls, | ||
1318 | struct GNUNET_TESTBED_Operation *op, | ||
1319 | const struct GNUNET_TESTBED_PeerInformation *pinfo, | ||
1320 | const char *emsg) | ||
1321 | { | ||
1322 | long i = (long) cls; | ||
1323 | |||
1324 | if ((NULL == pinfo) || | ||
1325 | (NULL != emsg)) | ||
1326 | { | ||
1327 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1328 | "pi_cb: %s\n", | ||
1329 | emsg); | ||
1330 | abort_test (__LINE__); | ||
1331 | return; | ||
1332 | } | ||
1333 | |||
1334 | if (GNUNET_TESTBED_PIT_IDENTITY == pinfo->pit) | ||
1335 | { | ||
1336 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1337 | "ID callback for %ld\n", | ||
1338 | i); | ||
1339 | testpeer_id[i] = pinfo->result.id; | ||
1340 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1341 | "id: %s\n", | ||
1342 | GNUNET_i2s (testpeer_id[i])); | ||
1343 | } | ||
1344 | else if (GNUNET_TESTBED_PIT_CONFIGURATION == pinfo->pit) | ||
1345 | { | ||
1346 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1347 | "CFG callback for %ld\n", | ||
1348 | i); | ||
1349 | p_cfg[i] = pinfo->result.cfg; | ||
1350 | } | ||
1351 | else | ||
1352 | { | ||
1353 | GNUNET_break (0); | ||
1354 | } | ||
1355 | |||
1356 | peerinfo_task_cnt++; | ||
1357 | if (peerinfo_task_cnt < 4) | ||
1358 | return; | ||
1359 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1360 | "Got all peer information, starting test\n"); | ||
1361 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1362 | "TEST ID 0: %s\n", | ||
1363 | GNUNET_i2s (testpeer_id[0])); | ||
1364 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1365 | "TEST ID 1: %s\n", | ||
1366 | GNUNET_i2s (testpeer_id[1])); | ||
1367 | test_task = GNUNET_SCHEDULER_add_now (&start_test, NULL); | ||
1368 | } | ||
1369 | |||
1370 | |||
1371 | /** | ||
1372 | * test main: start test when all peers are connected | ||
1373 | * | ||
1374 | * @param cls Closure. | ||
1375 | * @param ctx Argument to give to GNUNET_CADET_TEST_cleanup on test end. | ||
1376 | * @param num_peers Number of peers that are running. | ||
1377 | * @param peers Array of peers. | ||
1378 | * @param cadets Handle to each of the CADETs of the peers. | ||
1379 | */ | ||
1380 | static void | ||
1381 | tmain (void *cls, | ||
1382 | struct GNUNET_CADET_TEST_Context *ctx, | ||
1383 | unsigned int num_peers, | ||
1384 | struct GNUNET_TESTBED_Peer **peers, | ||
1385 | struct GNUNET_CADET_Handle **cadets) | ||
1386 | { | ||
1387 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n"); | ||
1388 | ok = 0; | ||
1389 | test_ctx = ctx; | ||
1390 | peers_running = num_peers; | ||
1391 | GNUNET_assert (peers_running == peers_requested); | ||
1392 | testbed_peers = peers; | ||
1393 | cadets_running = cadets; | ||
1394 | |||
1395 | disconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, | ||
1396 | &disconnect_cadet_peers, | ||
1397 | (void *) __LINE__); | ||
1398 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, | ||
1399 | NULL); | ||
1400 | t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0], | ||
1401 | GNUNET_TESTBED_PIT_IDENTITY, | ||
1402 | &pi_cb, | ||
1403 | (void *) 0L); | ||
1404 | t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1], | ||
1405 | GNUNET_TESTBED_PIT_IDENTITY, | ||
1406 | &pi_cb, | ||
1407 | (void *) 1L); | ||
1408 | t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0], | ||
1409 | GNUNET_TESTBED_PIT_CONFIGURATION, | ||
1410 | &pi_cb, | ||
1411 | (void *) 0L); | ||
1412 | t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1], | ||
1413 | GNUNET_TESTBED_PIT_CONFIGURATION, | ||
1414 | &pi_cb, | ||
1415 | (void *) 1L); | ||
1416 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n"); | ||
1417 | } | ||
1418 | |||
1419 | |||
1420 | /** | ||
1421 | * Main: start test | ||
1422 | */ | ||
1423 | int | ||
1424 | main (int argc, char *argv[]) | ||
1425 | { | ||
1426 | static const struct GNUNET_HashCode *ports[2]; | ||
1427 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
1428 | GNUNET_MQ_hd_var_size (data, | ||
1429 | GNUNET_MESSAGE_TYPE_DUMMY, | ||
1430 | struct GNUNET_MessageHeader, | ||
1431 | NULL), | ||
1432 | GNUNET_MQ_handler_end () | ||
1433 | }; | ||
1434 | const char *config_file; | ||
1435 | char port_id[] = "test port"; | ||
1436 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
1437 | GNUNET_GETOPT_option_relative_time ('t', | ||
1438 | "time", | ||
1439 | "short_time", | ||
1440 | gettext_noop ("set short timeout"), | ||
1441 | &short_time), | ||
1442 | GNUNET_GETOPT_option_uint ('m', | ||
1443 | "messages", | ||
1444 | "NUM_MESSAGES", | ||
1445 | gettext_noop ("set number of messages to send"), | ||
1446 | &total_packets), | ||
1447 | |||
1448 | GNUNET_GETOPT_OPTION_END | ||
1449 | }; | ||
1450 | |||
1451 | |||
1452 | initialized = GNUNET_NO; | ||
1453 | GNUNET_log_setup ("test", "DEBUG", NULL); | ||
1454 | |||
1455 | total_packets = TOTAL_PACKETS; | ||
1456 | short_time = SHORT_TIME; | ||
1457 | if (-1 == GNUNET_GETOPT_run (argv[0], options, argc, argv)) | ||
1458 | { | ||
1459 | fprintf (stderr, "test failed: problem with CLI parameters\n"); | ||
1460 | exit (1); | ||
1461 | } | ||
1462 | |||
1463 | config_file = "test_cadet.conf"; | ||
1464 | GNUNET_CRYPTO_hash (port_id, sizeof(port_id), &port); | ||
1465 | |||
1466 | /* Find out requested size */ | ||
1467 | if (strstr (argv[0], "_2_") != NULL) | ||
1468 | { | ||
1469 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DIRECT CONNECTIONs\n"); | ||
1470 | peers_requested = 2; | ||
1471 | } | ||
1472 | else if (strstr (argv[0], "_5_") != NULL) | ||
1473 | { | ||
1474 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "5 PEER LINE\n"); | ||
1475 | peers_requested = 5; | ||
1476 | } | ||
1477 | else if (strstr (argv[0], "_6_") != NULL) | ||
1478 | { | ||
1479 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "6 PEER LINE\n"); | ||
1480 | peers_requested = 6; | ||
1481 | } | ||
1482 | else | ||
1483 | { | ||
1484 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "SIZE UNKNOWN, USING 2\n"); | ||
1485 | peers_requested = 2; | ||
1486 | } | ||
1487 | |||
1488 | /* Find out requested test */ | ||
1489 | if (strstr (argv[0], "_forward") != NULL) | ||
1490 | { | ||
1491 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n"); | ||
1492 | test = FORWARD; | ||
1493 | test_name = "unicast"; | ||
1494 | ok_goal = 4; | ||
1495 | } | ||
1496 | else if (strstr (argv[0], "_signal") != NULL) | ||
1497 | { | ||
1498 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n"); | ||
1499 | test = P2P_SIGNAL; | ||
1500 | test_name = "signal"; | ||
1501 | ok_goal = 4; | ||
1502 | } | ||
1503 | else if (strstr (argv[0], "_speed_ack") != NULL) | ||
1504 | { | ||
1505 | /* Test is supposed to generate the following callbacks: | ||
1506 | * 1 incoming channel (@dest) | ||
1507 | * total_packets received data packet (@dest) | ||
1508 | * total_packets received data packet (@orig) | ||
1509 | * 1 received channel destroy (@dest) FIXME #5818 | ||
1510 | */ok_goal = total_packets * 2 + 2; | ||
1511 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n"); | ||
1512 | test = SPEED_ACK; | ||
1513 | test_name = "speed ack"; | ||
1514 | } | ||
1515 | else if (strstr (argv[0], "_speed") != NULL) | ||
1516 | { | ||
1517 | /* Test is supposed to generate the following callbacks: | ||
1518 | * 1 incoming channel (@dest) | ||
1519 | * 1 initial packet (@dest) | ||
1520 | * total_packets received data packet (@dest) | ||
1521 | * 1 received data packet (@orig) | ||
1522 | * 1 received channel destroy (@dest) FIXME #5818 | ||
1523 | */ok_goal = total_packets + 4; | ||
1524 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n"); | ||
1525 | if (strstr (argv[0], "_reliable") != NULL) | ||
1526 | { | ||
1527 | test = SPEED_REL; | ||
1528 | test_name = "speed reliable"; | ||
1529 | config_file = "test_cadet_drop.conf"; | ||
1530 | } | ||
1531 | else | ||
1532 | { | ||
1533 | test = SPEED; | ||
1534 | test_name = "speed"; | ||
1535 | } | ||
1536 | } | ||
1537 | else if (strstr (argv[0], "_keepalive") != NULL) | ||
1538 | { | ||
1539 | test = KEEPALIVE; | ||
1540 | test_name = "keepalive"; | ||
1541 | /* Test is supposed to generate the following callbacks: | ||
1542 | * 1 incoming channel (@dest) | ||
1543 | * [wait] | ||
1544 | * 1 received channel destroy (@dest) FIXME #5818 | ||
1545 | */ok_goal = 1; | ||
1546 | } | ||
1547 | else if (strstr (argv[0], "_reopen") != NULL) | ||
1548 | { | ||
1549 | test = REOPEN; | ||
1550 | test_name = "reopen"; | ||
1551 | ///* Test is supposed to generate the following callbacks: | ||
1552 | // * 1 incoming channel (@dest) | ||
1553 | // * [wait] | ||
1554 | // * 1 received channel destroy (@dest) FIXME #5818 | ||
1555 | // */ | ||
1556 | ok_goal = 6; | ||
1557 | } | ||
1558 | else if (strstr (argv[0], "_destroy") != NULL) | ||
1559 | { | ||
1560 | test = DESTROY; | ||
1561 | test_name = "destroy"; | ||
1562 | ok_goal = 6; | ||
1563 | short_time = GNUNET_TIME_relative_multiply (short_time, 5); | ||
1564 | } | ||
1565 | else | ||
1566 | { | ||
1567 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n"); | ||
1568 | test = SETUP; | ||
1569 | ok_goal = 0; | ||
1570 | } | ||
1571 | |||
1572 | if (strstr (argv[0], "backwards") != NULL) | ||
1573 | { | ||
1574 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n"); | ||
1575 | test_backwards = GNUNET_YES; | ||
1576 | GNUNET_asprintf (&test_name, "backwards %s", test_name); | ||
1577 | } | ||
1578 | |||
1579 | peerinfo_task_cnt = 0; | ||
1580 | ports[0] = &port; | ||
1581 | ports[1] = NULL; | ||
1582 | GNUNET_CADET_TEST_ruN ("test_cadet_small", | ||
1583 | config_file, | ||
1584 | peers_requested, | ||
1585 | &tmain, | ||
1586 | NULL, /* tmain cls */ | ||
1587 | &connect_handler, | ||
1588 | NULL, | ||
1589 | &disconnect_handler, | ||
1590 | handlers, | ||
1591 | ports); | ||
1592 | if (NULL != strstr (argv[0], "_reliable")) | ||
1593 | msg_dropped = 0; /* dropped should be retransmitted */ | ||
1594 | |||
1595 | if (ok_goal > ok - msg_dropped) | ||
1596 | { | ||
1597 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "FAILED! (%d/%d)\n", ok, ok_goal); | ||
1598 | return 1; | ||
1599 | } | ||
1600 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n"); | ||
1601 | return 0; | ||
1602 | } | ||
1603 | |||
1604 | |||
1605 | /* end of test_cadet.c */ | ||
diff --git a/src/cadet/test_cadet_drop.conf b/src/cadet/test_cadet_drop.conf deleted file mode 100644 index c9bcf603b..000000000 --- a/src/cadet/test_cadet_drop.conf +++ /dev/null | |||
@@ -1,4 +0,0 @@ | |||
1 | @INLINE@ test_cadet.conf | ||
2 | |||
3 | [cadet] | ||
4 | DROP_PERCENT = 1 | ||
diff --git a/src/cadet/test_cadet_flow.c b/src/cadet/test_cadet_flow.c deleted file mode 100644 index 441599973..000000000 --- a/src/cadet/test_cadet_flow.c +++ /dev/null | |||
@@ -1,891 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2011, 2017 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file cadet/test_cadet_flow.c | ||
22 | * @author Bart Polot | ||
23 | * @author Christian Grothoff | ||
24 | * @brief Test for flow control of CADET service | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include <stdio.h> | ||
28 | #include "cadet_test_lib.h" | ||
29 | #include "gnunet_cadet_service.h" | ||
30 | #include "gnunet_statistics_service.h" | ||
31 | #include <gauger.h> | ||
32 | |||
33 | |||
34 | /** | ||
35 | * Ugly workaround to unify data handlers on incoming and outgoing channels. | ||
36 | */ | ||
37 | struct CadetTestChannelWrapper | ||
38 | { | ||
39 | /** | ||
40 | * Channel pointer. | ||
41 | */ | ||
42 | struct GNUNET_CADET_Channel *ch; | ||
43 | }; | ||
44 | |||
45 | /** | ||
46 | * How many messages to send by default. | ||
47 | */ | ||
48 | #define TOTAL_PACKETS_DEFAULT 500 | ||
49 | |||
50 | /** | ||
51 | * How long until we give up on connecting the peers? | ||
52 | */ | ||
53 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120) | ||
54 | |||
55 | /** | ||
56 | * Time to wait by default for stuff that should be rather fast. | ||
57 | */ | ||
58 | #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20) | ||
59 | |||
60 | /** | ||
61 | * How fast do we send messages? | ||
62 | */ | ||
63 | #define SEND_INTERVAL GNUNET_TIME_relative_multiply ( \ | ||
64 | GNUNET_TIME_UNIT_MILLISECONDS, 10) | ||
65 | |||
66 | |||
67 | /** | ||
68 | * How many packets to send. | ||
69 | */ | ||
70 | static unsigned int total_packets = TOTAL_PACKETS_DEFAULT; | ||
71 | |||
72 | /** | ||
73 | * Time to wait for fast operations. | ||
74 | */ | ||
75 | static struct GNUNET_TIME_Relative short_time; | ||
76 | |||
77 | /** | ||
78 | * Size of each test packet's payload | ||
79 | */ | ||
80 | static size_t size_payload = sizeof(uint32_t); | ||
81 | |||
82 | /** | ||
83 | * Operation to get peer ids. | ||
84 | */ | ||
85 | static struct GNUNET_TESTBED_Operation *t_op[2]; | ||
86 | |||
87 | /** | ||
88 | * Peer ids. | ||
89 | */ | ||
90 | static struct GNUNET_PeerIdentity *p_id[2]; | ||
91 | |||
92 | /** | ||
93 | * Port ID | ||
94 | */ | ||
95 | static struct GNUNET_HashCode port; | ||
96 | |||
97 | /** | ||
98 | * Peer ids counter. | ||
99 | */ | ||
100 | static unsigned int p_ids; | ||
101 | |||
102 | /** | ||
103 | * Is the setup initialized? | ||
104 | */ | ||
105 | static int initialized; | ||
106 | |||
107 | /** | ||
108 | * Number of payload packes sent. | ||
109 | */ | ||
110 | static int data_sent; | ||
111 | |||
112 | /** | ||
113 | * Number of payload packets received. | ||
114 | */ | ||
115 | static int data_received; | ||
116 | |||
117 | /** | ||
118 | * Number of payload packed acknowledgements sent. | ||
119 | */ | ||
120 | static int ack_sent; | ||
121 | |||
122 | /** | ||
123 | * Number of payload packed explicitly (app level) acknowledged. | ||
124 | */ | ||
125 | static int ack_received; | ||
126 | |||
127 | /** | ||
128 | * Total number of peers asked to run. | ||
129 | */ | ||
130 | static unsigned int peers_requested = 2; | ||
131 | |||
132 | /** | ||
133 | * Number of currently running peers (should be same as @c peers_requested). | ||
134 | */ | ||
135 | static unsigned int peers_running; | ||
136 | |||
137 | /** | ||
138 | * Test context (to shut down). | ||
139 | */ | ||
140 | struct GNUNET_CADET_TEST_Context *test_ctx; | ||
141 | |||
142 | /** | ||
143 | * Task called to disconnect peers. | ||
144 | */ | ||
145 | static struct GNUNET_SCHEDULER_Task *disconnect_task; | ||
146 | |||
147 | /** | ||
148 | * Task To perform tests | ||
149 | */ | ||
150 | static struct GNUNET_SCHEDULER_Task *test_task; | ||
151 | |||
152 | /** | ||
153 | * Task runnining #send_next_msg(). | ||
154 | */ | ||
155 | static struct GNUNET_SCHEDULER_Task *send_next_msg_task; | ||
156 | |||
157 | /** | ||
158 | * Cadet handle for the root peer | ||
159 | */ | ||
160 | static struct GNUNET_CADET_Handle *h1; | ||
161 | |||
162 | /** | ||
163 | * Cadet handle for the first leaf peer | ||
164 | */ | ||
165 | static struct GNUNET_CADET_Handle *h2; | ||
166 | |||
167 | /** | ||
168 | * Channel handle for the root peer | ||
169 | */ | ||
170 | static struct GNUNET_CADET_Channel *outgoing_ch; | ||
171 | |||
172 | /** | ||
173 | * Channel handle for the dest peer | ||
174 | */ | ||
175 | static struct GNUNET_CADET_Channel *incoming_ch; | ||
176 | |||
177 | /** | ||
178 | * Time we started the data transmission (after channel has been established | ||
179 | * and initialized). | ||
180 | */ | ||
181 | static struct GNUNET_TIME_Absolute start_time; | ||
182 | |||
183 | /** | ||
184 | * Peers handle. | ||
185 | */ | ||
186 | static struct GNUNET_TESTBED_Peer **testbed_peers; | ||
187 | |||
188 | /** | ||
189 | * Statistics operation handle. | ||
190 | */ | ||
191 | static struct GNUNET_TESTBED_Operation *stats_op; | ||
192 | |||
193 | /** | ||
194 | * Keepalives sent. | ||
195 | */ | ||
196 | static unsigned int ka_sent; | ||
197 | |||
198 | /** | ||
199 | * Keepalives received. | ||
200 | */ | ||
201 | static unsigned int ka_received; | ||
202 | |||
203 | /** | ||
204 | * How many messages were dropped by CADET because of full buffers? | ||
205 | */ | ||
206 | static unsigned int msg_dropped; | ||
207 | |||
208 | |||
209 | /** | ||
210 | * Show the results of the test (banwidth achieved) and log them to GAUGER | ||
211 | */ | ||
212 | static void | ||
213 | show_end_data (void) | ||
214 | { | ||
215 | static struct GNUNET_TIME_Absolute end_time; | ||
216 | static struct GNUNET_TIME_Relative total_time; | ||
217 | |||
218 | end_time = GNUNET_TIME_absolute_get (); | ||
219 | total_time = GNUNET_TIME_absolute_get_difference (start_time, end_time); | ||
220 | fprintf (stderr, | ||
221 | "\nResults of test \"%s\"\n", | ||
222 | test_name); | ||
223 | fprintf (stderr, | ||
224 | "Test time %s\n", | ||
225 | GNUNET_STRINGS_relative_time_to_string (total_time, GNUNET_YES)); | ||
226 | fprintf (stderr, | ||
227 | "Test bandwidth: %f kb/s\n", | ||
228 | 4 * total_packets * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms | ||
229 | fprintf (stderr, | ||
230 | "Test throughput: %f packets/s\n\n", | ||
231 | total_packets * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms | ||
232 | GAUGER ("CADET", | ||
233 | test_name, | ||
234 | total_packets * 1000.0 / (total_time.rel_value_us / 1000), | ||
235 | "packets/s"); | ||
236 | } | ||
237 | |||
238 | |||
239 | /** | ||
240 | * Shut down peergroup, clean up. | ||
241 | * | ||
242 | * @param cls Closure (unused). | ||
243 | * @param tc Task Context. | ||
244 | */ | ||
245 | static void | ||
246 | shutdown_task (void *cls) | ||
247 | { | ||
248 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
249 | "Ending test.\n"); | ||
250 | if (NULL != send_next_msg_task) | ||
251 | { | ||
252 | GNUNET_SCHEDULER_cancel (send_next_msg_task); | ||
253 | send_next_msg_task = NULL; | ||
254 | } | ||
255 | if (NULL != test_task) | ||
256 | { | ||
257 | GNUNET_SCHEDULER_cancel (test_task); | ||
258 | test_task = NULL; | ||
259 | } | ||
260 | for (unsigned int i = 0; i < 2; i++) | ||
261 | GNUNET_TESTBED_operation_done (t_op[i]); | ||
262 | if (NULL != outgoing_ch) | ||
263 | { | ||
264 | GNUNET_CADET_channel_destroy (outgoing_ch); | ||
265 | outgoing_ch = NULL; | ||
266 | } | ||
267 | if (NULL != incoming_ch) | ||
268 | { | ||
269 | GNUNET_CADET_channel_destroy (incoming_ch); | ||
270 | incoming_ch = NULL; | ||
271 | } | ||
272 | GNUNET_CADET_TEST_cleanup (test_ctx); | ||
273 | } | ||
274 | |||
275 | |||
276 | /** | ||
277 | * Stats callback. Finish the stats testbed operation and when all stats have | ||
278 | * been iterated, shutdown the test. | ||
279 | * | ||
280 | * @param cls Closure (line number from which termination was requested). | ||
281 | * @param op the operation that has been finished | ||
282 | * @param emsg error message in case the operation has failed; will be NULL if | ||
283 | * operation has executed successfully. | ||
284 | */ | ||
285 | static void | ||
286 | stats_cont (void *cls, | ||
287 | struct GNUNET_TESTBED_Operation *op, | ||
288 | const char *emsg) | ||
289 | { | ||
290 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
291 | "KA sent: %u, KA received: %u\n", | ||
292 | ka_sent, | ||
293 | ka_received); | ||
294 | if ((KEEPALIVE == test) && ((ka_sent < 2) || (ka_sent > ka_received + 1))) | ||
295 | { | ||
296 | GNUNET_break (0); | ||
297 | ok--; | ||
298 | } | ||
299 | GNUNET_TESTBED_operation_done (stats_op); | ||
300 | |||
301 | if (NULL != disconnect_task) | ||
302 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
303 | disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, | ||
304 | cls); | ||
305 | } | ||
306 | |||
307 | |||
308 | /** | ||
309 | * Process statistic values. | ||
310 | * | ||
311 | * @param cls closure (line number, unused) | ||
312 | * @param peer the peer the statistic belong to | ||
313 | * @param subsystem name of subsystem that created the statistic | ||
314 | * @param name the name of the datum | ||
315 | * @param value the current value | ||
316 | * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not | ||
317 | * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration | ||
318 | */ | ||
319 | static int | ||
320 | stats_iterator (void *cls, | ||
321 | const struct GNUNET_TESTBED_Peer *peer, | ||
322 | const char *subsystem, | ||
323 | const char *name, | ||
324 | uint64_t value, | ||
325 | int is_persistent) | ||
326 | { | ||
327 | static const char *s_sent = "# keepalives sent"; | ||
328 | static const char *s_recv = "# keepalives received"; | ||
329 | static const char *rdrops = "# messages dropped due to full buffer"; | ||
330 | static const char *cdrops = "# messages dropped due to slow client"; | ||
331 | uint32_t i; | ||
332 | |||
333 | i = GNUNET_TESTBED_get_index (peer); | ||
334 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "STATS PEER %u - %s [%s]: %llu\n", i, | ||
335 | subsystem, name, (unsigned long long) value); | ||
336 | if ((0 == strncmp (s_sent, name, strlen (s_sent))) && (0 == i)) | ||
337 | ka_sent = value; | ||
338 | if ((0 == strncmp (s_recv, name, strlen (s_recv))) && (peers_requested - 1 == | ||
339 | i) ) | ||
340 | ka_received = value; | ||
341 | if (0 == strncmp (rdrops, name, strlen (rdrops))) | ||
342 | msg_dropped += value; | ||
343 | if (0 == strncmp (cdrops, name, strlen (cdrops))) | ||
344 | msg_dropped += value; | ||
345 | |||
346 | return GNUNET_OK; | ||
347 | } | ||
348 | |||
349 | |||
350 | /** | ||
351 | * Task to gather all statistics. | ||
352 | * | ||
353 | * @param cls Closure (line from which the task was scheduled). | ||
354 | */ | ||
355 | static void | ||
356 | gather_stats_and_exit (void *cls) | ||
357 | { | ||
358 | long l = (long) cls; | ||
359 | |||
360 | disconnect_task = NULL; | ||
361 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
362 | "gathering statistics from line %ld\n", | ||
363 | l); | ||
364 | if (NULL != outgoing_ch) | ||
365 | { | ||
366 | GNUNET_CADET_channel_destroy (outgoing_ch); | ||
367 | outgoing_ch = NULL; | ||
368 | } | ||
369 | stats_op = GNUNET_TESTBED_get_statistics (peers_running, | ||
370 | testbed_peers, | ||
371 | "cadet", | ||
372 | NULL, | ||
373 | &stats_iterator, | ||
374 | stats_cont, | ||
375 | cls); | ||
376 | } | ||
377 | |||
378 | |||
379 | /** | ||
380 | * Abort test: schedule disconnect and shutdown immediately | ||
381 | * | ||
382 | * @param line Line in the code the abort is requested from (__LINE__). | ||
383 | */ | ||
384 | static void | ||
385 | abort_test (long line) | ||
386 | { | ||
387 | if (NULL != disconnect_task) | ||
388 | { | ||
389 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
390 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
391 | "Aborting test from %ld\n", | ||
392 | line); | ||
393 | disconnect_task = | ||
394 | GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, | ||
395 | (void *) line); | ||
396 | } | ||
397 | } | ||
398 | |||
399 | |||
400 | /** | ||
401 | * Send a message on the channel with the appropriate size and payload. | ||
402 | * | ||
403 | * Update the appropriate *_sent counter. | ||
404 | * | ||
405 | * @param channel Channel to send the message on. | ||
406 | */ | ||
407 | static void | ||
408 | send_test_message (struct GNUNET_CADET_Channel *channel) | ||
409 | { | ||
410 | struct GNUNET_MQ_Envelope *env; | ||
411 | struct GNUNET_MessageHeader *msg; | ||
412 | uint32_t *data; | ||
413 | int payload; | ||
414 | int size; | ||
415 | |||
416 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
417 | "Sending test message on channel %p\n", | ||
418 | channel); | ||
419 | size = size_payload; | ||
420 | if (GNUNET_NO == initialized) | ||
421 | { | ||
422 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending INITIALIZER\n"); | ||
423 | size += 1000; | ||
424 | payload = data_sent; | ||
425 | if (SPEED_ACK == test) // FIXME unify SPEED_ACK with an initializer | ||
426 | data_sent++; | ||
427 | } | ||
428 | else if ((SPEED == test) || (SPEED_ACK == test)) | ||
429 | { | ||
430 | if (get_target_channel () == channel) | ||
431 | { | ||
432 | payload = ack_sent; | ||
433 | size += ack_sent; | ||
434 | ack_sent++; | ||
435 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
436 | "Sending ACK %u [%d bytes]\n", | ||
437 | payload, size); | ||
438 | } | ||
439 | else | ||
440 | { | ||
441 | payload = data_sent; | ||
442 | size += data_sent; | ||
443 | data_sent++; | ||
444 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
445 | "Sending DATA %u [%d bytes]\n", | ||
446 | data_sent, size); | ||
447 | } | ||
448 | } | ||
449 | else if (FORWARD == test) | ||
450 | { | ||
451 | payload = ack_sent; | ||
452 | } | ||
453 | else if (P2P_SIGNAL == test) | ||
454 | { | ||
455 | payload = data_sent; | ||
456 | } | ||
457 | else | ||
458 | { | ||
459 | GNUNET_assert (0); | ||
460 | } | ||
461 | env = GNUNET_MQ_msg_extra (msg, size, GNUNET_MESSAGE_TYPE_DUMMY); | ||
462 | |||
463 | data = (uint32_t *) &msg[1]; | ||
464 | *data = htonl (payload); | ||
465 | GNUNET_MQ_send (GNUNET_CADET_get_mq (channel), env); | ||
466 | } | ||
467 | |||
468 | |||
469 | /** | ||
470 | * Task to request a new data transmission in a SPEED test, without waiting | ||
471 | * for previous messages to be sent/arrrive. | ||
472 | * | ||
473 | * @param cls Closure (unused). | ||
474 | */ | ||
475 | static void | ||
476 | send_next_msg (void *cls) | ||
477 | { | ||
478 | struct GNUNET_CADET_Channel *channel; | ||
479 | |||
480 | send_next_msg_task = NULL; | ||
481 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
482 | "Sending next message: %d\n", | ||
483 | data_sent); | ||
484 | |||
485 | channel = GNUNET_YES == test_backwards ? incoming_ch : outgoing_ch; | ||
486 | GNUNET_assert (NULL != channel); | ||
487 | GNUNET_assert (SPEED == test); | ||
488 | send_test_message (channel); | ||
489 | if (data_sent < total_packets) | ||
490 | { | ||
491 | /* SPEED test: Send all messages as soon as possible */ | ||
492 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
493 | "Scheduling message %d\n", | ||
494 | data_sent + 1); | ||
495 | send_next_msg_task = | ||
496 | GNUNET_SCHEDULER_add_delayed (SEND_INTERVAL, | ||
497 | &send_next_msg, | ||
498 | NULL); | ||
499 | } | ||
500 | } | ||
501 | |||
502 | |||
503 | /** | ||
504 | * Check if payload is sane (size contains payload). | ||
505 | * | ||
506 | * @param cls should match #ch | ||
507 | * @param message The actual message. | ||
508 | * @return #GNUNET_OK to keep the channel open, | ||
509 | * #GNUNET_SYSERR to close it (signal serious error). | ||
510 | */ | ||
511 | static int | ||
512 | check_data (void *cls, | ||
513 | const struct GNUNET_MessageHeader *message) | ||
514 | { | ||
515 | return GNUNET_OK; /* all is well-formed */ | ||
516 | } | ||
517 | |||
518 | |||
519 | /** | ||
520 | * Function is called whenever a message is received. | ||
521 | * | ||
522 | * @param cls closure (set from GNUNET_CADET_connect(), peer number) | ||
523 | * @param message the actual message | ||
524 | */ | ||
525 | static void | ||
526 | handle_data (void *cls, | ||
527 | const struct GNUNET_MessageHeader *message) | ||
528 | { | ||
529 | struct CadetTestChannelWrapper *ch = cls; | ||
530 | struct GNUNET_CADET_Channel *channel = ch->ch; | ||
531 | uint32_t *data; | ||
532 | uint32_t payload; | ||
533 | int *counter; | ||
534 | |||
535 | GNUNET_CADET_receive_done (channel); | ||
536 | counter = get_target_channel () == channel ? &data_received : &ack_received; | ||
537 | if (channel == outgoing_ch) | ||
538 | { | ||
539 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
540 | "Root client got a message.\n"); | ||
541 | } | ||
542 | else if (channel == incoming_ch) | ||
543 | { | ||
544 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
545 | "Leaf client got a message.\n"); | ||
546 | } | ||
547 | else | ||
548 | { | ||
549 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
550 | "Unknown channel %p.\n", | ||
551 | channel); | ||
552 | GNUNET_assert (0); | ||
553 | } | ||
554 | |||
555 | data = (uint32_t *) &message[1]; | ||
556 | payload = ntohl (*data); | ||
557 | if (payload == *counter) | ||
558 | { | ||
559 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
560 | "Payload as expected: %u\n", | ||
561 | payload); | ||
562 | } | ||
563 | else | ||
564 | { | ||
565 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
566 | "Received payload %u, expected: %u\n", | ||
567 | payload, *counter); | ||
568 | } | ||
569 | (*counter)++; | ||
570 | if (get_target_channel () == channel) /* Got "data" */ | ||
571 | { | ||
572 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
573 | " received data %u\n", | ||
574 | data_received); | ||
575 | if (data_received < total_packets) | ||
576 | return; | ||
577 | } | ||
578 | else /* Got "ack" */ | ||
579 | { | ||
580 | if ((SPEED_ACK == test) || (SPEED == test) ) | ||
581 | { | ||
582 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received ack %u\n", ack_received); | ||
583 | /* Send more data */ | ||
584 | send_test_message (channel); | ||
585 | if ((ack_received < total_packets) && (SPEED != test) ) | ||
586 | return; | ||
587 | if ((ok == 2) && (SPEED == test) ) | ||
588 | return; | ||
589 | show_end_data (); | ||
590 | } | ||
591 | if (test == P2P_SIGNAL) | ||
592 | { | ||
593 | GNUNET_CADET_channel_destroy (incoming_ch); | ||
594 | incoming_ch = NULL; | ||
595 | } | ||
596 | else | ||
597 | { | ||
598 | GNUNET_CADET_channel_destroy (outgoing_ch); | ||
599 | outgoing_ch = NULL; | ||
600 | } | ||
601 | } | ||
602 | } | ||
603 | |||
604 | |||
605 | /** | ||
606 | * Method called whenever a peer connects to a port in MQ-based CADET. | ||
607 | * | ||
608 | * @param cls Closure from #GNUNET_CADET_open_port (peer # as long). | ||
609 | * @param channel New handle to the channel. | ||
610 | * @param source Peer that started this channel. | ||
611 | * @return Closure for the incoming @a channel. It's given to: | ||
612 | * - The #GNUNET_CADET_DisconnectEventHandler (given to | ||
613 | * #GNUNET_CADET_open_port) when the channel dies. | ||
614 | * - Each the #GNUNET_MQ_MessageCallback handlers for each message | ||
615 | * received on the @a channel. | ||
616 | */ | ||
617 | static void * | ||
618 | connect_handler (void *cls, | ||
619 | struct GNUNET_CADET_Channel *channel, | ||
620 | const struct GNUNET_PeerIdentity *source) | ||
621 | { | ||
622 | struct CadetTestChannelWrapper *ch; | ||
623 | long peer = (long) cls; | ||
624 | |||
625 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
626 | "Incoming channel from %s to %ld: %p\n", | ||
627 | GNUNET_i2s (source), | ||
628 | peer, | ||
629 | channel); | ||
630 | if (peer == peers_requested - 1) | ||
631 | { | ||
632 | if (NULL != incoming_ch) | ||
633 | { | ||
634 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
635 | "Duplicate incoming channel for client %lu\n", | ||
636 | (long) cls); | ||
637 | GNUNET_assert (0); | ||
638 | } | ||
639 | incoming_ch = channel; | ||
640 | } | ||
641 | else | ||
642 | { | ||
643 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
644 | "Incoming channel for unexpected peer #%lu\n", | ||
645 | (long) cls); | ||
646 | GNUNET_assert (0); | ||
647 | } | ||
648 | ch = GNUNET_new (struct CadetTestChannelWrapper); | ||
649 | ch->ch = channel; | ||
650 | |||
651 | return ch; | ||
652 | } | ||
653 | |||
654 | |||
655 | /** | ||
656 | * Function called whenever an MQ-channel is destroyed, even if the destruction | ||
657 | * was requested by #GNUNET_CADET_channel_destroy. | ||
658 | * It must NOT call #GNUNET_CADET_channel_destroy on the channel. | ||
659 | * | ||
660 | * It should clean up any associated state, including cancelling any pending | ||
661 | * transmission on this channel. | ||
662 | * | ||
663 | * @param cls Channel closure (channel wrapper). | ||
664 | * @param channel Connection to the other end (henceforth invalid). | ||
665 | */ | ||
666 | static void | ||
667 | disconnect_handler (void *cls, | ||
668 | const struct GNUNET_CADET_Channel *channel) | ||
669 | { | ||
670 | struct CadetTestChannelWrapper *ch_w = cls; | ||
671 | |||
672 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
673 | "Channel disconnected at %d\n", | ||
674 | ok); | ||
675 | GNUNET_assert (ch_w->ch == channel); | ||
676 | if (channel == incoming_ch) | ||
677 | incoming_ch = NULL; | ||
678 | else if (outgoing_ch == channel) | ||
679 | outgoing_ch = NULL; | ||
680 | else | ||
681 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
682 | "Disconnect on unknown channel %p\n", | ||
683 | channel); | ||
684 | if (NULL != disconnect_task) | ||
685 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
686 | disconnect_task = GNUNET_SCHEDULER_add_now (&gather_stats_and_exit, | ||
687 | (void *) __LINE__); | ||
688 | GNUNET_free (ch_w); | ||
689 | } | ||
690 | |||
691 | |||
692 | /** | ||
693 | * Start the testcase, we know the peers and have handles to CADET. | ||
694 | * | ||
695 | * Testcase continues when the root receives confirmation of connected peers, | ||
696 | * on callback function ch. | ||
697 | * | ||
698 | * @param cls Closure (unused). | ||
699 | */ | ||
700 | static void | ||
701 | start_test (void *cls) | ||
702 | { | ||
703 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
704 | GNUNET_MQ_hd_var_size (data, | ||
705 | GNUNET_MESSAGE_TYPE_DUMMY, | ||
706 | struct GNUNET_MessageHeader, | ||
707 | NULL), | ||
708 | GNUNET_MQ_handler_end () | ||
709 | }; | ||
710 | struct CadetTestChannelWrapper *ch; | ||
711 | enum GNUNET_CADET_ChannelOption flags; | ||
712 | |||
713 | test_task = NULL; | ||
714 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
715 | "In start_test\n"); | ||
716 | start_time = GNUNET_TIME_absolute_get (); | ||
717 | ch = GNUNET_new (struct CadetTestChannelWrapper); | ||
718 | outgoing_ch = GNUNET_CADET_channel_create (h1, | ||
719 | ch, | ||
720 | p_id[1], | ||
721 | &port, | ||
722 | flags, | ||
723 | NULL, | ||
724 | &disconnect_handler, | ||
725 | handlers); | ||
726 | ch->ch = outgoing_ch; | ||
727 | GNUNET_assert (NULL == disconnect_task); | ||
728 | disconnect_task | ||
729 | = GNUNET_SCHEDULER_add_delayed (short_time, | ||
730 | &gather_stats_and_exit, | ||
731 | (void *) __LINE__); | ||
732 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
733 | "Sending data initializer on channel %p...\n", | ||
734 | outgoing_ch); | ||
735 | send_test_message (outgoing_ch); | ||
736 | } | ||
737 | |||
738 | |||
739 | /** | ||
740 | * Callback to be called when the requested peer information is available | ||
741 | * | ||
742 | * @param cls the closure from GNUNET_TESTBED_peer_get_information() | ||
743 | * @param op the operation this callback corresponds to | ||
744 | * @param pinfo the result; will be NULL if the operation has failed | ||
745 | * @param emsg error message if the operation has failed; | ||
746 | * NULL if the operation is successful | ||
747 | */ | ||
748 | static void | ||
749 | pi_cb (void *cls, | ||
750 | struct GNUNET_TESTBED_Operation *op, | ||
751 | const struct GNUNET_TESTBED_PeerInformation *pinfo, | ||
752 | const char *emsg) | ||
753 | { | ||
754 | long i = (long) cls; | ||
755 | |||
756 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
757 | "ID callback for %ld\n", | ||
758 | i); | ||
759 | if ((NULL == pinfo) || | ||
760 | (NULL != emsg)) | ||
761 | { | ||
762 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
763 | "pi_cb: %s\n", | ||
764 | emsg); | ||
765 | abort_test (__LINE__); | ||
766 | return; | ||
767 | } | ||
768 | p_id[i] = pinfo->result.id; | ||
769 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
770 | "id: %s\n", | ||
771 | GNUNET_i2s (p_id[i])); | ||
772 | p_ids++; | ||
773 | if (p_ids < 2) | ||
774 | return; | ||
775 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
776 | "Got all IDs, starting test\n"); | ||
777 | test_task = GNUNET_SCHEDULER_add_now (&start_test, | ||
778 | NULL); | ||
779 | } | ||
780 | |||
781 | |||
782 | /** | ||
783 | * test main: start test when all peers are connected | ||
784 | * | ||
785 | * @param cls Closure. | ||
786 | * @param ctx Argument to give to GNUNET_CADET_TEST_cleanup on test end. | ||
787 | * @param num_peers Number of peers that are running. | ||
788 | * @param peers Array of peers. | ||
789 | * @param cadets Handle to each of the CADETs of the peers. | ||
790 | */ | ||
791 | static void | ||
792 | tmain (void *cls, | ||
793 | struct GNUNET_CADET_TEST_Context *ctx, | ||
794 | unsigned int num_peers, | ||
795 | struct GNUNET_TESTBED_Peer **peers, | ||
796 | struct GNUNET_CADET_Handle **cadets) | ||
797 | { | ||
798 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
799 | "test main\n"); | ||
800 | test_ctx = ctx; | ||
801 | peers_running = num_peers; | ||
802 | GNUNET_assert (peers_running == peers_requested); | ||
803 | testbed_peers = peers; | ||
804 | h1 = cadets[0]; | ||
805 | h2 = cadets[num_peers - 1]; | ||
806 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, | ||
807 | NULL); | ||
808 | p_ids = 0; | ||
809 | t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0], | ||
810 | GNUNET_TESTBED_PIT_IDENTITY, | ||
811 | &pi_cb, | ||
812 | (void *) 0L); | ||
813 | t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1], | ||
814 | GNUNET_TESTBED_PIT_IDENTITY, | ||
815 | &pi_cb, | ||
816 | (void *) 1L); | ||
817 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
818 | "requested peer ids\n"); | ||
819 | } | ||
820 | |||
821 | |||
822 | /** | ||
823 | * Main: start test | ||
824 | */ | ||
825 | int | ||
826 | main (int argc, | ||
827 | char *argv[]) | ||
828 | { | ||
829 | static const struct GNUNET_HashCode *ports[2]; | ||
830 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
831 | GNUNET_MQ_hd_var_size (data, | ||
832 | GNUNET_MESSAGE_TYPE_DUMMY, | ||
833 | struct GNUNET_MessageHeader, | ||
834 | NULL), | ||
835 | GNUNET_MQ_handler_end () | ||
836 | }; | ||
837 | const char *config_file = "test_cadet.conf"; | ||
838 | char port_id[] = "test port"; | ||
839 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
840 | GNUNET_GETOPT_option_relative_time ('t', | ||
841 | "time", | ||
842 | "short_time", | ||
843 | gettext_noop ("set short timeout"), | ||
844 | &short_time), | ||
845 | GNUNET_GETOPT_option_uint ('m', | ||
846 | "messages", | ||
847 | "NUM_MESSAGES", | ||
848 | gettext_noop ("set number of messages to send"), | ||
849 | &total_packets), | ||
850 | GNUNET_GETOPT_option_uint ('p', | ||
851 | "peers", | ||
852 | "NUM_PEERS", | ||
853 | gettext_noop ("number of peers to launch"), | ||
854 | &peers_requested), | ||
855 | GNUNET_GETOPT_OPTION_END | ||
856 | }; | ||
857 | |||
858 | GNUNET_log_setup ("test-cadet-flow", | ||
859 | "DEBUG", | ||
860 | NULL); | ||
861 | total_packets = TOTAL_PACKETS; | ||
862 | short_time = SHORT_TIME; | ||
863 | if (-1 == GNUNET_GETOPT_run (argv[0], | ||
864 | options, | ||
865 | argc, | ||
866 | argv)) | ||
867 | { | ||
868 | fprintf (stderr, | ||
869 | "test failed: problem with CLI parameters\n"); | ||
870 | return 1; | ||
871 | } | ||
872 | GNUNET_CRYPTO_hash (port_id, | ||
873 | sizeof(port_id), | ||
874 | &port); | ||
875 | ports[0] = &port; | ||
876 | ports[1] = NULL; | ||
877 | GNUNET_CADET_TEST_ruN ("test_cadet_flow", | ||
878 | config_file, | ||
879 | peers_requested, | ||
880 | &tmain, | ||
881 | NULL, /* tmain cls */ | ||
882 | &connect_handler, | ||
883 | NULL, | ||
884 | &disconnect_handler, | ||
885 | handlers, | ||
886 | ports); | ||
887 | return 0; | ||
888 | } | ||
889 | |||
890 | |||
891 | /* end of test_cadet_flow.c */ | ||