2 This file is part of GNUnet.
3 Copyright (C) 2009, 2012 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file rps/test_rps.c
22 * @brief Testcase for the random peer sampling service. Starts
23 * a peergroup with a given number of peers, then waits to
24 * receive size pushes/pulls from each peer. Expects to wait
25 * for one message from each peer.
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
31 #include "gnunet_rps_service.h"
32 #include "rps-test_util.h"
33 #include "gnunet-service-rps_sampler_elem.h"
39 * How many peers do we start?
41 static uint32_t num_peers;
44 * How long do we run the test?
46 //#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
47 static struct GNUNET_TIME_Relative timeout;
51 * Portion of malicious peers
53 static double portion = .1;
56 * Type of malicious peer to test
58 static unsigned int mal_type = 0;
61 * Handles to all of the running peers
63 static struct GNUNET_TESTBED_Peer **testbed_peers;
66 * @brief Indicates whether peer should go off- or online
68 enum PEER_ONLINE_DELTA {
70 * @brief Indicates peer going online
74 * @brief Indicates peer going offline
87 struct OpListEntry *next;
92 struct OpListEntry *prev;
95 * The testbed operation
97 struct GNUNET_TESTBED_Operation *op;
100 * Depending on whether we start or stop RPS service at the peer, set this to
101 * #PEER_GO_ONLINE (1) or #PEER_GO_OFFLINE (-1)
103 enum PEER_ONLINE_DELTA delta;
106 * Index of the regarding peer
114 static struct OpListEntry *oplist_head;
119 static struct OpListEntry *oplist_tail;
123 * A pending reply: A request was sent and the reply is pending.
130 struct PendingReply *next;
131 struct PendingReply *prev;
134 * Handle to the request we are waiting for
136 struct GNUNET_RPS_Request_Handle *req_handle;
139 * The peer that requested
141 struct RPSPeer *rps_peer;
146 * A pending request: A request was not made yet but is scheduled for later.
148 struct PendingRequest
153 struct PendingRequest *next;
154 struct PendingRequest *prev;
157 * Handle to the request we are waiting for
159 struct GNUNET_SCHEDULER_Task *request_task;
162 * The peer that requested
164 struct RPSPeer *rps_peer;
169 * Information we track for each peer.
179 * Handle for RPS connect operation.
181 struct GNUNET_TESTBED_Operation *op;
184 * Handle to RPS service.
186 struct GNUNET_RPS_Handle *rps_handle;
191 struct GNUNET_PeerIdentity *peer_id;
194 * A request handle to check for an request
196 //struct GNUNET_RPS_Request_Handle *req_handle;
199 * Peer on- or offline?
204 * Number of Peer IDs to request during the whole test
206 unsigned int num_ids_to_request;
209 * Pending requests DLL
211 struct PendingRequest *pending_req_head;
212 struct PendingRequest *pending_req_tail;
215 * Number of pending requests
217 unsigned int num_pending_reqs;
220 * Pending replies DLL
222 struct PendingReply *pending_rep_head;
223 struct PendingReply *pending_rep_tail;
226 * Number of pending replies
228 unsigned int num_pending_reps;
231 * Number of received PeerIDs
233 unsigned int num_recv_ids;
236 * Pending operation on that peer
238 const struct OpListEntry *entry_op_manage;
241 * Testbed operation to connect to statistics service
243 struct GNUNET_TESTBED_Operation *stat_op;
246 * Handle to the statistics service
248 struct GNUNET_STATISTICS_Handle *stats_h;
251 * @brief flags to indicate which statistics values have been already
252 * collected from the statistics service.
253 * Used to check whether we are able to shutdown.
255 uint32_t stat_collected_flags;
258 * @brief File name of the file the stats are finally written to
260 char *file_name_stats;
265 STAT_TYPE_ROUNDS = 0x1, /* 1 */
266 STAT_TYPE_BLOCKS = 0x2, /* 2 */
267 STAT_TYPE_BLOCKS_MANY_PUSH = 0x4, /* 3 */
268 STAT_TYPE_BLOCKS_NO_PUSH = 0x8, /* 4 */
269 STAT_TYPE_BLOCKS_NO_PULL = 0x10, /* 5 */
270 STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL = 0x20, /* 6 */
271 STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL = 0x40, /* 7 */
272 STAT_TYPE_ISSUED_PUSH_SEND = 0x80, /* 8 */
273 STAT_TYPE_ISSUED_PULL_REQ = 0x100, /* 9 */
274 STAT_TYPE_ISSUED_PULL_REP = 0x200, /* 10 */
275 STAT_TYPE_SENT_PUSH_SEND = 0x400, /* 11 */
276 STAT_TYPE_SENT_PULL_REQ = 0x800, /* 12 */
277 STAT_TYPE_SENT_PULL_REP = 0x1000, /* 13 */
278 STAT_TYPE_RECV_PUSH_SEND = 0x2000, /* 14 */
279 STAT_TYPE_RECV_PULL_REQ = 0x4000, /* 15 */
280 STAT_TYPE_RECV_PULL_REP = 0x8000, /* 16 */
281 STAT_TYPE_MAX = 0x80000000, /* 32 */
286 struct RPSPeer *rps_peer;
287 enum STAT_TYPE stat_type;
292 * Information for all the peers.
294 static struct RPSPeer *rps_peers;
297 * Peermap to get the index of a given peer ID quick.
299 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
304 static struct GNUNET_PeerIdentity *rps_peer_ids;
307 * ID of the targeted peer.
309 static struct GNUNET_PeerIdentity *target_peer;
312 * ID of the peer that requests for the evaluation.
314 static struct RPSPeer *eval_peer;
317 * Number of online peers.
319 static unsigned int num_peers_online;
322 * Return value from 'main'.
327 * Identifier for the churn task that runs periodically
329 static struct GNUNET_SCHEDULER_Task *shutdown_task;
332 * Identifier for the churn task that runs periodically
334 static struct GNUNET_SCHEDULER_Task *churn_task;
337 * Called to initialise the given RPSPeer
339 typedef void (*InitPeer) (struct RPSPeer *rps_peer);
342 * @brief Called directly after connecting to the service
344 * @param rps_peer Specific peer the function is called on
345 * @param h the handle to the rps service
347 typedef void (*PreTest) (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h);
350 * @brief Executes functions to test the api/service for a given peer
352 * Called from within #rps_connect_complete_cb ()
353 * Implemented by #churn_test_cb, #profiler_cb, #mal_cb, #single_req_cb,
354 * #delay_req_cb, #seed_big_cb, #single_peer_seed_cb, #seed_cb, #req_cancel_cb
356 * @param rps_peer the peer the task runs on
358 typedef void (*MainTest) (struct RPSPeer *rps_peer);
361 * Callback called once the requested random peers are available
363 typedef void (*ReplyHandle) (void *cls,
365 const struct GNUNET_PeerIdentity *recv_peers);
368 * Called directly before disconnecting from the service
370 typedef void (*PostTest) (struct RPSPeer *peer);
373 * Function called after disconnect to evaluate test success
375 typedef int (*EvaluationCallback) (void);
378 * @brief Do we have Churn?
382 * @brief If we have churn this is set
386 * @brief If we have no churn this is set
392 * @brief Is it ok to quit the test before the timeout?
394 enum OPTION_QUICK_QUIT {
396 * @brief It is ok for the test to quit before the timeout triggers
401 * @brief It is NOT ok for the test to quit before the timeout triggers
407 * @brief Do we collect statistics at the end?
409 enum OPTION_COLLECT_STATISTICS {
411 * @brief We collect statistics at the end
416 * @brief We do not collect statistics at the end
418 NO_COLLECT_STATISTICS,
422 * Structure to define a single test
432 * Called with a single peer in order to initialise that peer
437 * Called directly after connecting to the service
442 * Main function for each peer
447 * Callback called once the requested peers are available
449 ReplyHandle reply_handle;
452 * Called directly before disconnecting from the service
457 * Function to evaluate the test results
459 EvaluationCallback eval_cb;
464 uint32_t request_interval;
467 * Number of Requests to make.
469 uint32_t num_requests;
472 * Run with (-out) churn
474 enum OPTION_CHURN have_churn;
477 * Quit test before timeout?
479 enum OPTION_QUICK_QUIT have_quick_quit;
482 * Collect statistics at the end?
484 enum OPTION_COLLECT_STATISTICS have_collect_statistics;
487 * @brief Mark which values from the statistics service to collect at the end
490 uint32_t stat_collect_flags;
494 * Are we shutting down?
496 static int in_shutdown;
499 * Append arguments to file
502 tofile_ (const char *file_name, const char *line)
504 struct GNUNET_DISK_FileHandle *f;
505 /* char output_buffer[512]; */
510 if (NULL == (f = GNUNET_DISK_file_open (file_name,
511 GNUNET_DISK_OPEN_APPEND |
512 GNUNET_DISK_OPEN_WRITE |
513 GNUNET_DISK_OPEN_CREATE,
514 GNUNET_DISK_PERM_USER_READ |
515 GNUNET_DISK_PERM_USER_WRITE |
516 GNUNET_DISK_PERM_GROUP_READ |
517 GNUNET_DISK_PERM_OTHER_READ)))
519 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
520 "Not able to open file %s\n",
524 /* size = GNUNET_snprintf (output_buffer,
525 sizeof (output_buffer),
527 GNUNET_TIME_absolute_get ().abs_value_us,
531 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
532 "Failed to write string to buffer (size: %i)\n",
537 size = strlen (line) * sizeof (char);
539 size2 = GNUNET_DISK_file_write (f, line, size);
542 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
543 "Unable to write to file! (Size: %lu, size2: %lu)\n",
546 if (GNUNET_YES != GNUNET_DISK_file_close (f))
548 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
549 "Unable to close file\n");
554 if (GNUNET_YES != GNUNET_DISK_file_close (f))
556 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
557 "Unable to close file\n");
562 * This function is used to facilitate writing important information to disk
564 #define tofile(file_name, ...) do {\
567 size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
569 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
570 "Failed to create tmp_buf\n");\
572 tofile_(file_name,tmp_buf);\
577 * Write the ids and their according index in the given array to a file
581 ids_to_file (char *file_name,
582 struct GNUNET_PeerIdentity *peer_ids,
583 unsigned int num_peer_ids)
587 for (i=0 ; i < num_peer_ids ; i++)
592 GNUNET_i2s_full (&peer_ids[i]));
597 * Test the success of a single test
607 for (i = 0; i < num_peers; i++)
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
610 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
612 GNUNET_i2s (rps_peers[i].peer_id),
613 rps_peers[i].num_recv_ids,
614 rps_peers[i].num_ids_to_request,
615 (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
616 tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
618 return tmp_ok? 0 : 1;
623 * Creates an oplist entry and adds it to the oplist DLL
625 static struct OpListEntry *
628 struct OpListEntry *entry;
630 entry = GNUNET_new (struct OpListEntry);
631 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
637 * @brief Checks if given peer already received its statistics value from the
638 * statistics service.
640 * @param rps_peer the peer to check for
642 * @return #GNUNET_YES if so
643 * #GNUNET_NO otherwise
645 static int check_statistics_collect_completed_single_peer (
646 const struct RPSPeer *rps_peer)
648 if (cur_test_run.stat_collect_flags !=
649 (cur_test_run.stat_collect_flags &
650 rps_peer->stat_collected_flags))
657 * @brief Checks if all peers already received their statistics value from the
658 * statistics service.
660 * @return #GNUNET_YES if so
661 * #GNUNET_NO otherwise
663 static int check_statistics_collect_completed ()
667 for (i = 0; i < num_peers; i++)
669 if (GNUNET_NO == check_statistics_collect_completed_single_peer (&rps_peers[i]))
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
672 "At least Peer %" PRIu32 " did not yet receive all statistics values\n",
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
678 "All peers received their statistics values\n");
683 * Task run on timeout to shut everything down.
686 shutdown_op (void *cls)
690 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
691 "Shutdown task scheduled, going down.\n");
692 in_shutdown = GNUNET_YES;
693 if (NULL != churn_task)
695 GNUNET_SCHEDULER_cancel (churn_task);
698 for (i = 0; i < num_peers; i++)
700 if (NULL != rps_peers[i].op)
701 GNUNET_TESTBED_operation_done (rps_peers[i].op);
702 if (NULL != cur_test_run.post_test)
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing post_test for peer %u\n", i);
705 cur_test_run.post_test (&rps_peers[i]);
708 /* If we do not collect statistics, shut down directly */
709 if (NO_COLLECT_STATISTICS == cur_test_run.have_collect_statistics ||
710 GNUNET_YES == check_statistics_collect_completed())
712 GNUNET_SCHEDULER_shutdown ();
721 seed_peers (void *cls)
723 struct RPSPeer *peer = cls;
727 // TODO if malicious don't seed mal peers
728 amount = round (.5 * num_peers);
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
731 for (i = 0 ; i < amount ; i++)
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
734 GNUNET_i2s (&rps_peer_ids[i]));
736 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
744 seed_peers_big (void *cls)
746 struct RPSPeer *peer = cls;
747 unsigned int seed_msg_size;
748 uint32_t num_peers_max;
752 seed_msg_size = 8; /* sizeof (struct GNUNET_RPS_CS_SeedMessage) */
753 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE - seed_msg_size) /
754 sizeof (struct GNUNET_PeerIdentity);
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756 "Peers that fit in one seed msg; %u\n",
758 amount = num_peers_max + (0.5 * num_peers_max);
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760 "Seeding many (%u) peers:\n",
762 struct GNUNET_PeerIdentity ids_to_seed[amount];
763 for (i = 0; i < amount; i++)
765 ids_to_seed[i] = *peer->peer_id;
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
768 GNUNET_i2s (&ids_to_seed[i]));
771 GNUNET_RPS_seed_ids (peer->rps_handle, amount, ids_to_seed);
775 * Get the id of peer i.
778 info_cb (void *cb_cls,
779 struct GNUNET_TESTBED_Operation *op,
780 const struct GNUNET_TESTBED_PeerInformation *pinfo,
783 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
785 if (GNUNET_YES == in_shutdown)
790 if (NULL == pinfo || NULL != emsg)
792 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
793 GNUNET_TESTBED_operation_done (entry->op);
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 GNUNET_i2s (pinfo->result.id));
802 rps_peer_ids[entry->index] = *(pinfo->result.id);
803 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
805 GNUNET_assert (GNUNET_OK ==
806 GNUNET_CONTAINER_multipeermap_put (peer_map,
807 &rps_peer_ids[entry->index],
808 &rps_peers[entry->index],
809 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
810 tofile ("/tmp/rps/peer_ids",
813 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
815 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
816 GNUNET_TESTBED_operation_done (entry->op);
822 * Callback to be called when RPS service connect operation is completed
824 * @param cls the callback closure from functions generating an operation
825 * @param op the operation that has been finished
826 * @param ca_result the RPS service handle returned from rps_connect_adapter
827 * @param emsg error message in case the operation has failed; will be NULL if
828 * operation has executed successfully.
831 rps_connect_complete_cb (void *cls,
832 struct GNUNET_TESTBED_Operation *op,
836 struct RPSPeer *rps_peer = cls;
837 struct GNUNET_RPS_Handle *rps = ca_result;
839 if (GNUNET_YES == in_shutdown)
844 rps_peer->rps_handle = rps;
845 rps_peer->online = GNUNET_YES;
848 GNUNET_assert (op == rps_peer->op);
851 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
852 "Failed to connect to RPS service: %s\n",
855 GNUNET_SCHEDULER_shutdown ();
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
861 cur_test_run.main_test (rps_peer);
866 * Adapter function called to establish a connection to
870 * @param cfg configuration of the peer to connect to; will be available until
871 * GNUNET_TESTBED_operation_done() is called on the operation returned
872 * from GNUNET_TESTBED_service_connect()
873 * @return service handle to return in 'op_result', NULL on error
876 rps_connect_adapter (void *cls,
877 const struct GNUNET_CONFIGURATION_Handle *cfg)
879 struct GNUNET_RPS_Handle *h;
881 h = GNUNET_RPS_connect (cfg);
883 if (NULL != cur_test_run.pre_test)
884 cur_test_run.pre_test (cls, h);
890 * Called to open a connection to the peer's statistics
892 * @param cls peer context
893 * @param cfg configuration of the peer to connect to; will be available until
894 * GNUNET_TESTBED_operation_done() is called on the operation returned
895 * from GNUNET_TESTBED_service_connect()
896 * @return service handle to return in 'op_result', NULL on error
899 stat_connect_adapter (void *cls,
900 const struct GNUNET_CONFIGURATION_Handle *cfg)
902 struct RPSPeer *peer = cls;
904 peer->stats_h = GNUNET_STATISTICS_create ("rps-profiler", cfg);
905 return peer->stats_h;
909 * Called to disconnect from peer's statistics service
911 * @param cls peer context
912 * @param op_result service handle returned from the connect adapter
915 stat_disconnect_adapter (void *cls, void *op_result)
917 struct RPSPeer *peer = cls;
919 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
920 // (peer->stats_h, "core", "# peers connected",
921 // stat_iterator, peer));
922 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
923 // (peer->stats_h, "nse", "# peers connected",
924 // stat_iterator, peer));
925 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
926 peer->stats_h = NULL;
930 * Called after successfully opening a connection to a peer's statistics
931 * service; we register statistics monitoring for CORE and NSE here.
933 * @param cls the callback closure from functions generating an operation
934 * @param op the operation that has been finished
935 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
936 * @param emsg error message in case the operation has failed; will be NULL if
937 * operation has executed successfully.
940 stat_complete_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
941 void *ca_result, const char *emsg )
943 //struct GNUNET_STATISTICS_Handle *sh = ca_result;
944 //struct RPSPeer *peer = (struct RPSPeer *) cls;
951 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
952 // (sh, "core", "# peers connected",
953 // stat_iterator, peer));
954 //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
955 // (sh, "nse", "# peers connected",
956 // stat_iterator, peer));
961 * Adapter function called to destroy connection to
965 * @param op_result service handle returned from the connect adapter
968 rps_disconnect_adapter (void *cls,
971 struct RPSPeer *peer = cls;
972 struct GNUNET_RPS_Handle *h = op_result;
973 GNUNET_assert (NULL != peer);
974 GNUNET_RPS_disconnect (h);
975 peer->rps_handle = NULL;
979 /***********************************************************************
980 * Definition of tests
981 ***********************************************************************/
983 // TODO check whether tests can be stopped earlier
985 default_eval_cb (void)
997 * Initialise given RPSPeer
999 static void default_init_peer (struct RPSPeer *rps_peer)
1001 rps_peer->num_ids_to_request = 1;
1005 * Callback to call on receipt of a reply
1007 * @param cls closure
1008 * @param n number of peers
1009 * @param recv_peers the received peers
1012 default_reply_handle (void *cls,
1014 const struct GNUNET_PeerIdentity *recv_peers)
1016 struct RPSPeer *rps_peer;
1017 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1020 rps_peer = pending_rep->rps_peer;
1021 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1022 rps_peer->pending_rep_tail,
1024 rps_peer->num_pending_reps--;
1025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1026 "[%s] got %" PRIu64 " peers:\n",
1027 GNUNET_i2s (rps_peer->peer_id),
1030 for (i = 0; i < n; i++)
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1035 GNUNET_i2s (&recv_peers[i]));
1037 rps_peer->num_recv_ids++;
1040 if (0 == evaluate () && HAVE_QUICK_QUIT == cur_test_run.have_quick_quit)
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test succeeded before timeout\n");
1043 GNUNET_assert (NULL != shutdown_task);
1044 GNUNET_SCHEDULER_cancel (shutdown_task);
1045 shutdown_task = GNUNET_SCHEDULER_add_now (&shutdown_op, NULL);
1046 GNUNET_assert (NULL!= shutdown_task);
1051 * Request random peers.
1054 request_peers (void *cls)
1056 struct PendingRequest *pending_req = cls;
1057 struct RPSPeer *rps_peer;
1058 struct PendingReply *pending_rep;
1060 if (GNUNET_YES == in_shutdown)
1062 rps_peer = pending_req->rps_peer;
1063 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
1064 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1065 rps_peer->pending_req_tail,
1067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1068 "Requesting one peer\n");
1069 pending_rep = GNUNET_new (struct PendingReply);
1070 pending_rep->rps_peer = rps_peer;
1071 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
1073 cur_test_run.reply_handle,
1075 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
1076 rps_peer->pending_rep_tail,
1078 rps_peer->num_pending_reps++;
1079 rps_peer->num_pending_reqs--;
1083 cancel_pending_req (struct PendingRequest *pending_req)
1085 struct RPSPeer *rps_peer;
1087 rps_peer = pending_req->rps_peer;
1088 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1089 rps_peer->pending_req_tail,
1091 rps_peer->num_pending_reqs--;
1092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1093 "Cancelling pending request\n");
1094 GNUNET_SCHEDULER_cancel (pending_req->request_task);
1095 GNUNET_free (pending_req);
1099 cancel_request (struct PendingReply *pending_rep)
1101 struct RPSPeer *rps_peer;
1103 rps_peer = pending_rep->rps_peer;
1104 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1105 rps_peer->pending_rep_tail,
1107 rps_peer->num_pending_reps--;
1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1109 "Cancelling request\n");
1110 GNUNET_RPS_request_cancel (pending_rep->req_handle);
1111 GNUNET_free (pending_rep);
1118 cancel_request_cb (void *cls)
1120 struct RPSPeer *rps_peer = cls;
1121 struct PendingReply *pending_rep;
1123 if (GNUNET_YES == in_shutdown)
1125 pending_rep = rps_peer->pending_rep_head;
1126 GNUNET_assert (1 <= rps_peer->num_pending_reps);
1127 cancel_request (pending_rep);
1132 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
1133 * issued, nor replied
1136 schedule_missing_requests (struct RPSPeer *rps_peer)
1139 struct PendingRequest *pending_req;
1141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1142 "Scheduling %u - %u missing requests\n",
1143 rps_peer->num_ids_to_request,
1144 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
1145 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
1146 rps_peer->num_ids_to_request);
1147 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
1148 i < rps_peer->num_ids_to_request; i++)
1150 pending_req = GNUNET_new (struct PendingRequest);
1151 pending_req->rps_peer = rps_peer;
1152 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
1153 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1154 cur_test_run.request_interval * i),
1157 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
1158 rps_peer->pending_req_tail,
1160 rps_peer->num_pending_reqs++;
1165 cancel_pending_req_rep (struct RPSPeer *rps_peer)
1167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1168 "Cancelling all (pending) requests.\n");
1169 while (NULL != rps_peer->pending_req_head)
1170 cancel_pending_req (rps_peer->pending_req_head);
1171 GNUNET_assert (0 == rps_peer->num_pending_reqs);
1172 while (NULL != rps_peer->pending_rep_head)
1173 cancel_request (rps_peer->pending_rep_head);
1174 GNUNET_assert (0 == rps_peer->num_pending_reps);
1177 /***********************************
1179 ***********************************/
1182 * Initialise only non-mal RPSPeers
1184 static void mal_init_peer (struct RPSPeer *rps_peer)
1186 if (rps_peer->index >= round (portion * num_peers))
1187 rps_peer->num_ids_to_request = 1;
1192 * @brief Set peers to (non-)malicious before execution
1194 * Of signature #PreTest
1196 * @param rps_peer the peer to set (non-) malicious
1197 * @param h the handle to the service
1200 mal_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
1202 #ifdef ENABLE_MALICIOUS
1203 uint32_t num_mal_peers;
1205 GNUNET_assert ( (1 >= portion) &&
1207 num_mal_peers = round (portion * num_peers);
1209 if (rps_peer->index < num_mal_peers)
1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1212 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
1214 GNUNET_i2s (rps_peer->peer_id),
1217 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
1218 rps_peer_ids, target_peer);
1220 #endif /* ENABLE_MALICIOUS */
1224 mal_cb (struct RPSPeer *rps_peer)
1226 uint32_t num_mal_peers;
1228 if (GNUNET_YES == in_shutdown)
1233 #ifdef ENABLE_MALICIOUS
1234 GNUNET_assert ( (1 >= portion) &&
1236 num_mal_peers = round (portion * num_peers);
1238 if (rps_peer->index >= num_mal_peers)
1239 { /* It's useless to ask a malicious peer about a random sample -
1240 it's not sampling */
1241 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1242 seed_peers, rps_peer);
1243 schedule_missing_requests (rps_peer);
1245 #endif /* ENABLE_MALICIOUS */
1249 /***********************************
1251 ***********************************/
1253 single_req_cb (struct RPSPeer *rps_peer)
1255 if (GNUNET_YES == in_shutdown)
1260 schedule_missing_requests (rps_peer);
1263 /***********************************
1265 ***********************************/
1267 delay_req_cb (struct RPSPeer *rps_peer)
1269 if (GNUNET_YES == in_shutdown)
1274 schedule_missing_requests (rps_peer);
1277 /***********************************
1279 ***********************************/
1281 seed_cb (struct RPSPeer *rps_peer)
1283 if (GNUNET_YES == in_shutdown)
1288 GNUNET_SCHEDULER_add_delayed (
1289 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
1290 seed_peers, rps_peer);
1293 /***********************************
1295 ***********************************/
1297 seed_big_cb (struct RPSPeer *rps_peer)
1299 if (GNUNET_YES == in_shutdown)
1304 // TODO test seeding > GNUNET_MAX_MESSAGE_SIZE peers
1305 GNUNET_SCHEDULER_add_delayed (
1306 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1307 seed_peers_big, rps_peer);
1310 /***********************************
1312 ***********************************/
1314 single_peer_seed_cb (struct RPSPeer *rps_peer)
1319 /***********************************
1321 ***********************************/
1323 seed_req_cb (struct RPSPeer *rps_peer)
1325 if (GNUNET_YES == in_shutdown)
1330 GNUNET_SCHEDULER_add_delayed (
1331 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1332 seed_peers, rps_peer);
1333 schedule_missing_requests (rps_peer);
1336 //TODO start big mal
1338 /***********************************
1340 ***********************************/
1342 req_cancel_cb (struct RPSPeer *rps_peer)
1344 if (GNUNET_YES == in_shutdown)
1349 schedule_missing_requests (rps_peer);
1350 GNUNET_SCHEDULER_add_delayed (
1351 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1352 (cur_test_run.request_interval + 1)),
1353 cancel_request_cb, rps_peer);
1356 /***********************************
1358 ***********************************/
1364 * @brief Starts churn
1366 * Has signature of #MainTest
1368 * This is not implemented too nicely as this is called for each peer, but we
1369 * only need to call it once. (Yes we check that we only schedule the task
1372 * @param rps_peer The peer it's called for
1375 churn_test_cb (struct RPSPeer *rps_peer)
1377 if (GNUNET_YES == in_shutdown)
1383 if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
1385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1386 "Starting churn task\n");
1387 churn_task = GNUNET_SCHEDULER_add_delayed (
1388 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1393 "Not starting churn task\n");
1396 schedule_missing_requests (rps_peer);
1399 /***********************************
1401 ***********************************/
1404 * Callback to be called when RPS service is started or stopped at peers
1407 * @param op the operation handle
1408 * @param emsg NULL on success; otherwise an error description
1411 churn_cb (void *cls,
1412 struct GNUNET_TESTBED_Operation *op,
1416 struct OpListEntry *entry = cls;
1418 if (GNUNET_YES == in_shutdown)
1423 GNUNET_TESTBED_operation_done (entry->op);
1426 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1427 GNUNET_SCHEDULER_shutdown ();
1430 GNUNET_assert (0 != entry->delta);
1432 num_peers_online += entry->delta;
1434 if (PEER_GO_OFFLINE == entry->delta)
1435 { /* Peer hopefully just went offline */
1436 if (GNUNET_YES != rps_peers[entry->index].online)
1438 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1439 "peer %s was expected to go offline but is still marked as online\n",
1440 GNUNET_i2s (rps_peers[entry->index].peer_id));
1445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1446 "peer %s probably went offline as expected\n",
1447 GNUNET_i2s (rps_peers[entry->index].peer_id));
1449 rps_peers[entry->index].online = GNUNET_NO;
1452 else if (PEER_GO_ONLINE < entry->delta)
1453 { /* Peer hopefully just went online */
1454 if (GNUNET_NO != rps_peers[entry->index].online)
1456 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1457 "peer %s was expected to go online but is still marked as offline\n",
1458 GNUNET_i2s (rps_peers[entry->index].peer_id));
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1464 "peer %s probably went online as expected\n",
1465 GNUNET_i2s (rps_peers[entry->index].peer_id));
1466 if (NULL != cur_test_run.pre_test)
1468 cur_test_run.pre_test (&rps_peers[entry->index],
1469 rps_peers[entry->index].rps_handle);
1470 schedule_missing_requests (&rps_peers[entry->index]);
1473 rps_peers[entry->index].online = GNUNET_YES;
1477 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1478 "Invalid value for delta: %i\n", entry->delta);
1482 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1483 rps_peers[entry->index].entry_op_manage = NULL;
1484 GNUNET_free (entry);
1485 //if (num_peers_in_round[current_round] == peers_running)
1490 * @brief Set the rps-service up or down for a specific peer
1492 * @param i index of action
1493 * @param j index of peer
1494 * @param delta (#PEER_ONLINE_DELTA) down (-1) or up (1)
1495 * @param prob_go_on_off the probability of the action
1498 manage_service_wrapper (unsigned int i, unsigned int j,
1499 enum PEER_ONLINE_DELTA delta,
1500 double prob_go_on_off)
1502 struct OpListEntry *entry = NULL;
1505 /* make sure that management operation is not already scheduled */
1506 if (NULL != rps_peers[j].entry_op_manage)
1511 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1514 "%u. selected peer (%u: %s) is %s.\n",
1517 GNUNET_i2s (rps_peers[j].peer_id),
1518 (PEER_GO_ONLINE == delta) ? "online" : "offline");
1519 if (prob < prob_go_on_off * UINT32_MAX)
1521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1523 GNUNET_i2s (rps_peers[j].peer_id),
1524 (PEER_GO_OFFLINE == delta) ? "offline" : "online");
1526 if (PEER_GO_OFFLINE == delta)
1527 cancel_pending_req_rep (&rps_peers[j]);
1528 entry = make_oplist_entry ();
1529 entry->delta = delta;
1531 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1536 (PEER_GO_OFFLINE == delta) ? 0 : 1);
1538 rps_peers[j].entry_op_manage = entry;
1547 double portion_online;
1548 unsigned int *permut;
1549 double prob_go_offline;
1550 double portion_go_online;
1551 double portion_go_offline;
1553 if (GNUNET_YES == in_shutdown)
1557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1558 "Churn function executing\n");
1560 churn_task = NULL; /* Should be invalid by now */
1562 /* Compute the probability for an online peer to go offline
1564 portion_online = num_peers_online * 1.0 / num_peers;
1565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1566 "Portion online: %f\n",
1568 portion_go_online = ((1 - portion_online) * .5 * .66);
1569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1570 "Portion that should go online: %f\n",
1572 portion_go_offline = (portion_online + portion_go_online) - .75;
1573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1574 "Portion that probably goes offline: %f\n",
1575 portion_go_offline);
1576 prob_go_offline = portion_go_offline / (portion_online * .5);
1577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1578 "Probability of a selected online peer to go offline: %f\n",
1581 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1582 (unsigned int) num_peers);
1584 /* Go over 50% randomly chosen peers */
1585 for (i = 0; i < .5 * num_peers; i++)
1589 /* If online, shut down with certain probability */
1590 if (GNUNET_YES == rps_peers[j].online)
1592 manage_service_wrapper (i, j, -1, prob_go_offline);
1595 /* If offline, restart with certain probability */
1596 else if (GNUNET_NO == rps_peers[j].online)
1598 manage_service_wrapper (i, j, 1, 0.66);
1602 GNUNET_free (permut);
1604 churn_task = GNUNET_SCHEDULER_add_delayed (
1605 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1612 * Initialise given RPSPeer
1614 static void profiler_init_peer (struct RPSPeer *rps_peer)
1616 if (num_peers - 1 == rps_peer->index)
1617 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1622 * Callback to call on receipt of a reply
1624 * @param cls closure
1625 * @param n number of peers
1626 * @param recv_peers the received peers
1629 profiler_reply_handle (void *cls,
1631 const struct GNUNET_PeerIdentity *recv_peers)
1633 struct RPSPeer *rps_peer;
1634 struct RPSPeer *rcv_rps_peer;
1638 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1640 rps_peer = pending_rep->rps_peer;
1641 file_name = "/tmp/rps/received_ids";
1642 file_name_dh = "/tmp/rps/diehard_input";
1643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1644 "[%s] got %" PRIu64 " peers:\n",
1645 GNUNET_i2s (rps_peer->peer_id),
1647 for (i = 0; i < n; i++)
1649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1652 GNUNET_i2s (&recv_peers[i]));
1655 GNUNET_i2s_full (&recv_peers[i]));
1656 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1657 GNUNET_assert (NULL != rcv_rps_peer);
1658 tofile (file_name_dh,
1660 (uint32_t) rcv_rps_peer->index);
1662 default_reply_handle (cls, n, recv_peers);
1667 profiler_cb (struct RPSPeer *rps_peer)
1669 if (GNUNET_YES == in_shutdown)
1675 if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
1677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1678 "Starting churn task\n");
1679 churn_task = GNUNET_SCHEDULER_add_delayed (
1680 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1685 "Not starting churn task\n");
1688 /* Only request peer ids at one peer.
1689 * (It's the before-last because last one is target of the focussed attack.)
1691 if (eval_peer == rps_peer)
1692 schedule_missing_requests (rps_peer);
1696 * Function called from #profiler_eval with a filename.
1698 * @param cls closure
1699 * @param filename complete filename (absolute path)
1700 * @return #GNUNET_OK to continue to iterate,
1701 * #GNUNET_NO to stop iteration with no error,
1702 * #GNUNET_SYSERR to abort iteration with error!
1705 file_name_cb (void *cls, const char *filename)
1707 if (NULL != strstr (filename, "sampler_el"))
1709 struct RPS_SamplerElement *s_elem;
1710 struct GNUNET_CRYPTO_AuthKey auth_key;
1711 const char *key_char;
1714 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1715 tofile (filename, "--------------------------\n");
1717 auth_key = string_to_auth_key (key_char);
1718 s_elem = RPS_sampler_elem_create ();
1719 RPS_sampler_elem_set (s_elem, auth_key);
1721 for (i = 0; i < num_peers; i++)
1723 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1725 RPS_sampler_elem_destroy (s_elem);
1731 * This is run after the test finished.
1733 * Compute all perfect samples.
1736 profiler_eval (void)
1738 /* Compute perfect sample for each sampler element */
1739 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1741 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1748 * @brief Try to ensure that `/tmp/rps` exists.
1750 * @return #GNUNET_YES on success
1751 * #GNUNET_SYSERR on failure
1753 static int ensure_folder_exist (void)
1755 if (GNUNET_NO == GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
1757 GNUNET_DISK_directory_create ("/tmp/rps");
1759 if (GNUNET_YES != GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
1761 return GNUNET_SYSERR;
1767 store_stats_file_name (struct RPSPeer *rps_peer)
1769 unsigned int len_file_name;
1770 unsigned int out_size;
1773 if (GNUNET_SYSERR == ensure_folder_exist()) return;
1774 len_file_name = (14 + strlen (GNUNET_i2s_full (rps_peer->peer_id)) + 1) * sizeof (char);
1775 file_name = GNUNET_malloc (len_file_name);
1776 out_size = GNUNET_snprintf (file_name,
1779 GNUNET_i2s_full (rps_peer->peer_id));
1780 if (len_file_name < out_size ||
1783 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1784 "Failed to write string to buffer (size: %i, out_size: %i)\n",
1788 rps_peer->file_name_stats = file_name;
1792 * Continuation called by #GNUNET_STATISTICS_get() functions.
1794 * Remembers that this specific statistics value was received for this peer.
1795 * Checks whether all peers received their statistics yet.
1796 * Issues the shutdown.
1798 * @param cls closure
1799 * @param success #GNUNET_OK if statistics were
1800 * successfully obtained, #GNUNET_SYSERR if not.
1803 post_test_shutdown_ready_cb (void *cls,
1806 struct STATcls *stat_cls = (struct STATcls *) cls;
1807 struct RPSPeer *rps_peer = stat_cls->rps_peer;
1808 if (GNUNET_OK == success)
1810 /* set flag that we we got the value */
1811 rps_peer->stat_collected_flags |= stat_cls->stat_type;
1813 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1814 "Peer %u did not receive statistics value\n",
1816 GNUNET_free (stat_cls);
1820 if (NULL != rps_peer->stat_op &&
1821 GNUNET_YES == check_statistics_collect_completed_single_peer (rps_peer))
1823 GNUNET_TESTBED_operation_done (rps_peer->stat_op);
1826 if (GNUNET_YES == check_statistics_collect_completed())
1828 GNUNET_free (stat_cls);
1829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1831 GNUNET_SCHEDULER_shutdown ();
1833 GNUNET_free (stat_cls);
1838 * @brief Converts #STAT_TYPE enum to the equivalent string representation that
1839 * is stored with the statistics service.
1841 * @param stat_type #STAT_TYPE enum
1843 * @return string representation that matches statistics value
1845 char* stat_type_2_str (enum STAT_TYPE stat_type)
1849 case STAT_TYPE_ROUNDS:
1851 case STAT_TYPE_BLOCKS:
1852 return "# rounds blocked";
1853 case STAT_TYPE_BLOCKS_MANY_PUSH:
1854 return "# rounds blocked - too many pushes";
1855 case STAT_TYPE_BLOCKS_NO_PUSH:
1856 return "# rounds blocked - no pushes";
1857 case STAT_TYPE_BLOCKS_NO_PULL:
1858 return "# rounds blocked - no pull replies";
1859 case STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL:
1860 return "# rounds blocked - too many pushes, no pull replies";
1861 case STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL:
1862 return "# rounds blocked - no pushes, no pull replies";
1863 case STAT_TYPE_ISSUED_PUSH_SEND:
1864 return "# push send issued";
1865 case STAT_TYPE_ISSUED_PULL_REQ:
1866 return "# pull request send issued";
1867 case STAT_TYPE_ISSUED_PULL_REP:
1868 return "# pull reply send issued";
1869 case STAT_TYPE_SENT_PUSH_SEND:
1870 return "# pushes sent";
1871 case STAT_TYPE_SENT_PULL_REQ:
1872 return "# pull requests sent";
1873 case STAT_TYPE_SENT_PULL_REP:
1874 return "# pull replys sent";
1875 case STAT_TYPE_RECV_PUSH_SEND:
1876 return "# push message received";
1877 case STAT_TYPE_RECV_PULL_REQ:
1878 return "# pull request message received";
1879 case STAT_TYPE_RECV_PULL_REP:
1880 return "# pull reply messages received";
1889 * Callback function to process statistic values.
1891 * @param cls closure
1892 * @param subsystem name of subsystem that created the statistic
1893 * @param name the name of the datum
1894 * @param value the current value
1895 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
1896 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
1899 stat_iterator (void *cls,
1900 const char *subsystem,
1905 const struct STATcls *stat_cls = (const struct STATcls *) cls;
1906 const struct RPSPeer *rps_peer = (const struct RPSPeer *) stat_cls->rps_peer;
1907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got stat value: %s - %" PRIu64 "\n",
1908 //stat_type_2_str (stat_cls->stat_type),
1911 to_file (rps_peer->file_name_stats,
1912 "%s: %" PRIu64 "\n",
1918 void post_profiler (struct RPSPeer *rps_peer)
1920 if (COLLECT_STATISTICS != cur_test_run.have_collect_statistics)
1925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1926 "Going to request statistic values with mask 0x%" PRIx32 "\n",
1927 cur_test_run.stat_collect_flags);
1929 struct STATcls *stat_cls;
1931 for (stat_type = STAT_TYPE_ROUNDS;
1932 stat_type < STAT_TYPE_MAX;
1933 stat_type = stat_type <<1)
1935 if (stat_type & cur_test_run.stat_collect_flags)
1937 stat_cls = GNUNET_malloc (sizeof (struct STATcls));
1938 stat_cls->rps_peer = rps_peer;
1939 stat_cls->stat_type = stat_type;
1940 store_stats_file_name (rps_peer);
1941 GNUNET_STATISTICS_get (rps_peer->stats_h,
1943 stat_type_2_str (stat_type),
1944 post_test_shutdown_ready_cb,
1946 (struct STATcls *) stat_cls);
1947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1948 "Requested statistics for %s (peer %" PRIu32 ")\n",
1949 stat_type_2_str (stat_type),
1956 /***********************************************************************
1957 * /Definition of tests
1958 ***********************************************************************/
1962 * Actual "main" function for the testcase.
1964 * @param cls closure
1965 * @param h the run handle
1966 * @param n_peers number of peers in 'peers'
1967 * @param peers handle to peers run in the testbed
1968 * @param links_succeeded the number of overlay link connection attempts that
1970 * @param links_failed the number of overlay link connection attempts that
1975 struct GNUNET_TESTBED_RunHandle *h,
1976 unsigned int n_peers,
1977 struct GNUNET_TESTBED_Peer **peers,
1978 unsigned int links_succeeded,
1979 unsigned int links_failed)
1982 struct OpListEntry *entry;
1983 uint32_t num_mal_peers;
1985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
1987 /* Check whether we timed out */
1988 if (n_peers != num_peers ||
1990 0 == links_succeeded)
1992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Going down due to args (eg. timeout)\n");
1993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tn_peers: %u\n", n_peers);
1994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tnum_peers: %" PRIu32 "\n", num_peers);
1995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tpeers: %p\n", peers);
1996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tlinks_succeeded: %u\n", links_succeeded);
1997 GNUNET_SCHEDULER_shutdown ();
2002 /* Initialize peers */
2003 testbed_peers = peers;
2004 num_peers_online = 0;
2005 for (i = 0; i < num_peers; i++)
2007 entry = make_oplist_entry ();
2009 rps_peers[i].index = i;
2010 if (NULL != cur_test_run.init_peer)
2011 cur_test_run.init_peer (&rps_peers[i]);
2012 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
2013 GNUNET_TESTBED_PIT_IDENTITY,
2018 /* Bring peers up */
2019 num_mal_peers = round (portion * num_peers);
2020 GNUNET_assert (num_peers == n_peers);
2021 for (i = 0; i < n_peers; i++)
2023 rps_peers[i].index = i;
2024 if ( (rps_peers[i].num_recv_ids < rps_peers[i].num_ids_to_request) ||
2025 (i < num_mal_peers) )
2028 GNUNET_TESTBED_service_connect (&rps_peers[i],
2031 &rps_connect_complete_cb,
2033 &rps_connect_adapter,
2034 &rps_disconnect_adapter,
2037 /* Connect all peers to statistics service */
2038 if (COLLECT_STATISTICS == cur_test_run.have_collect_statistics)
2040 rps_peers[i].stat_op =
2041 GNUNET_TESTBED_service_connect (NULL,
2046 &stat_connect_adapter,
2047 &stat_disconnect_adapter,
2052 if (NULL != churn_task)
2053 GNUNET_SCHEDULER_cancel (churn_task);
2054 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
2059 * Entry point for the testcase, sets up the testbed.
2061 * @param argc unused
2062 * @param argv unused
2063 * @return 0 on success
2066 main (int argc, char *argv[])
2071 cur_test_run.name = "test-rps-default";
2072 cur_test_run.init_peer = default_init_peer;
2073 cur_test_run.pre_test = NULL;
2074 cur_test_run.reply_handle = default_reply_handle;
2075 cur_test_run.eval_cb = default_eval_cb;
2076 cur_test_run.post_test = NULL;
2077 cur_test_run.have_churn = HAVE_CHURN;
2078 cur_test_run.have_collect_statistics = NO_COLLECT_STATISTICS;
2079 cur_test_run.stat_collect_flags = 0;
2081 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
2083 if (strstr (argv[0], "malicious") != NULL)
2085 cur_test_run.pre_test = mal_pre;
2086 cur_test_run.main_test = mal_cb;
2087 cur_test_run.init_peer = mal_init_peer;
2089 if (strstr (argv[0], "_1") != NULL)
2091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
2092 cur_test_run.name = "test-rps-malicious_1";
2095 else if (strstr (argv[0], "_2") != NULL)
2097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
2098 cur_test_run.name = "test-rps-malicious_2";
2101 else if (strstr (argv[0], "_3") != NULL)
2103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
2104 cur_test_run.name = "test-rps-malicious_3";
2109 else if (strstr (argv[0], "_single_req") != NULL)
2111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
2112 cur_test_run.name = "test-rps-single-req";
2113 cur_test_run.main_test = single_req_cb;
2114 cur_test_run.have_churn = HAVE_NO_CHURN;
2117 else if (strstr (argv[0], "_delayed_reqs") != NULL)
2119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
2120 cur_test_run.name = "test-rps-delayed-reqs";
2121 cur_test_run.main_test = delay_req_cb;
2122 cur_test_run.have_churn = HAVE_NO_CHURN;
2125 else if (strstr (argv[0], "_seed_big") != NULL)
2127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_MAX_MESSAGE_SIZE)\n");
2129 cur_test_run.name = "test-rps-seed-big";
2130 cur_test_run.main_test = seed_big_cb;
2131 cur_test_run.eval_cb = no_eval;
2132 cur_test_run.have_churn = HAVE_NO_CHURN;
2133 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
2136 else if (strstr (argv[0], "_single_peer_seed") != NULL)
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
2139 cur_test_run.name = "test-rps-single-peer-seed";
2140 cur_test_run.main_test = single_peer_seed_cb;
2141 cur_test_run.have_churn = HAVE_NO_CHURN;
2144 else if (strstr (argv[0], "_seed_request") != NULL)
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
2147 cur_test_run.name = "test-rps-seed-request";
2148 cur_test_run.main_test = seed_req_cb;
2149 cur_test_run.have_churn = HAVE_NO_CHURN;
2152 else if (strstr (argv[0], "_seed") != NULL)
2154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
2155 cur_test_run.name = "test-rps-seed";
2156 cur_test_run.main_test = seed_cb;
2157 cur_test_run.eval_cb = no_eval;
2158 cur_test_run.have_churn = HAVE_NO_CHURN;
2161 else if (strstr (argv[0], "_req_cancel") != NULL)
2163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
2164 cur_test_run.name = "test-rps-req-cancel";
2166 cur_test_run.main_test = req_cancel_cb;
2167 cur_test_run.eval_cb = no_eval;
2168 cur_test_run.have_churn = HAVE_NO_CHURN;
2169 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
2172 else if (strstr (argv[0], "_churn") != NULL)
2174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test churn\n");
2175 cur_test_run.name = "test-rps-churn";
2177 cur_test_run.init_peer = default_init_peer;
2178 cur_test_run.main_test = churn_test_cb;
2179 cur_test_run.reply_handle = default_reply_handle;
2180 cur_test_run.eval_cb = default_eval_cb;
2181 cur_test_run.have_churn = HAVE_CHURN;
2182 cur_test_run.have_quick_quit = HAVE_NO_QUICK_QUIT;
2183 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
2186 else if (strstr (argv[0], "profiler") != NULL)
2188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
2189 cur_test_run.name = "test-rps-profiler";
2192 cur_test_run.init_peer = profiler_init_peer;
2193 cur_test_run.pre_test = mal_pre;
2194 cur_test_run.main_test = profiler_cb;
2195 cur_test_run.reply_handle = profiler_reply_handle;
2196 cur_test_run.eval_cb = profiler_eval;
2197 cur_test_run.post_test = post_profiler;
2198 cur_test_run.request_interval = 2;
2199 cur_test_run.num_requests = 5;
2200 cur_test_run.have_churn = HAVE_CHURN;
2201 cur_test_run.have_quick_quit = HAVE_NO_QUICK_QUIT;
2202 cur_test_run.have_collect_statistics = COLLECT_STATISTICS;
2203 cur_test_run.stat_collect_flags = STAT_TYPE_ROUNDS |
2205 STAT_TYPE_BLOCKS_MANY_PUSH |
2206 STAT_TYPE_BLOCKS_NO_PUSH |
2207 STAT_TYPE_BLOCKS_NO_PULL |
2208 STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL |
2209 STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL |
2210 STAT_TYPE_ISSUED_PUSH_SEND |
2211 STAT_TYPE_ISSUED_PULL_REQ |
2212 STAT_TYPE_ISSUED_PULL_REP |
2213 STAT_TYPE_SENT_PUSH_SEND |
2214 STAT_TYPE_SENT_PULL_REQ |
2215 STAT_TYPE_SENT_PULL_REP |
2216 STAT_TYPE_RECV_PUSH_SEND |
2217 STAT_TYPE_RECV_PULL_REQ |
2218 STAT_TYPE_RECV_PULL_REP;
2219 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300);
2221 /* 'Clean' directory */
2222 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
2223 GNUNET_DISK_directory_create ("/tmp/rps/");
2226 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
2227 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
2228 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
2229 if ( (2 == mal_type) ||
2231 target_peer = &rps_peer_ids[num_peers - 2];
2232 if (profiler_eval == cur_test_run.eval_cb)
2233 eval_peer = &rps_peers[num_peers - 1]; /* FIXME: eval_peer could be a
2234 malicious peer if not careful
2235 with the malicious portion */
2238 ret_value = GNUNET_TESTBED_test_run (cur_test_run.name,
2243 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2244 "_test_run returned.\n");
2245 if (GNUNET_OK != ret_value)
2247 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2248 "Test did not run successfully!\n");
2251 ret_value = cur_test_run.eval_cb();
2252 GNUNET_free (rps_peers);
2253 GNUNET_free (rps_peer_ids);
2254 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2258 /* end of test_rps.c */