From ba4d141be09651b452270fc21f062d50bd3f738a Mon Sep 17 00:00:00 2001 From: t3sserakt Date: Tue, 13 Dec 2022 13:08:17 +0100 Subject: - refactored barrier header, fixed messages for barrier signaling --- src/testing/gnunet-cmds-helper.c | 8 +- src/testing/testing.h | 185 +++++++++++++++++++++ src/testing/testing_api_cmd_barrier.c | 20 +-- src/testing/testing_api_cmd_barrier_reached.c | 6 +- .../testing_api_cmd_netjail_start_testsystem.c | 25 +-- src/testing/testing_api_loop.c | 52 +++--- 6 files changed, 244 insertions(+), 52 deletions(-) (limited to 'src/testing') diff --git a/src/testing/gnunet-cmds-helper.c b/src/testing/gnunet-cmds-helper.c index 9c16038f0..22cf5a428 100644 --- a/src/testing/gnunet-cmds-helper.c +++ b/src/testing/gnunet-cmds-helper.c @@ -67,6 +67,8 @@ struct GNUNET_SCHEDULER_Task *finished_task; +struct GNUNET_TESTING_Interpreter *is; + /** * Struct with information about a specific node and the whole network namespace setup. * @@ -392,7 +394,7 @@ tokenizer_cb (void *cls, const struct GNUNET_MessageHeader *message) } strcat (node_ip, plugin->m); - plugin->api->start_testcase (&write_message, router_ip, node_ip, plugin->m, + is = plugin->api->start_testcase (&write_message, router_ip, node_ip, plugin->m, plugin->n, plugin->local_m, ni->topology_data, ni->read_file, &finished_cb); @@ -413,9 +415,11 @@ tokenizer_cb (void *cls, const struct GNUNET_MessageHeader *message) else if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_BARRIER_ADVANCED == ntohs ( message->type)) { + const char *barrier_name; struct GNUNET_TESTING_CommandBarrierAdvanced *adm = (struct GNUNET_TESTING_CommandBarrierAdvanced *) message; - plugin->api->barrier_advanced (adm->barrier_name); + barrier_name = (const char *) &adm[1]; + GNUNET_TESTING_finish_attached_cmds (is, barrier_name); return GNUNET_OK; } else if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_ALL_PEERS_STARTED == ntohs ( diff --git a/src/testing/testing.h b/src/testing/testing.h index 848533251..382879706 100644 --- a/src/testing/testing.h +++ b/src/testing/testing.h @@ -24,7 +24,70 @@ #ifndef TESTING_H #define TESTING_H #include "gnunet_util_lib.h" +#include "gnunet_testing_plugin.h" +GNUNET_NETWORK_STRUCT_BEGIN + +/** + * Message send to a child loop to inform the child loop about a barrier being advanced. + * FIXME: This is not packed and contains a char*... no payload documentation. + */ +struct GNUNET_TESTING_CommandBarrierAdvanced +{ + /** + * Type is GNUNET_MESSAGE_TYPE_CMDS_HELPER_BARRIER_ADVANCED + */ + struct GNUNET_MessageHeader header; + + /* followed by 0-terminated barrier name */ +}; + +/** + * Message send by a child loop to inform the master loop how much + * GNUNET_CMDS_BARRIER_REACHED messages the child will send. + * FIXME: Not packed and contains char*; int in NBO? bitlength undefined. + */ +struct GNUNET_TESTING_CommandBarrierAttached +{ + /** + * Type is GNUNET_MESSAGE_TYPE_CMDS_HELPER_BARRIER_ATTACHED + */ + struct GNUNET_MessageHeader header; + + /** + * How often the child loop will reach the barrier. + */ + uint32_t expected_reaches GNUNET_PACKED; + + /** + * The number of the node the barrier is running on. + */ + uint32_t node_number GNUNET_PACKED; + + /* followed by 0-terminated barrier name */ +}; + +struct GNUNET_TESTING_CommandBarrierReached +{ + /** + * Type is GNUNET_MESSAGE_TYPE_CMDS_HELPER_BARRIER_REACHED + */ + struct GNUNET_MessageHeader header; + + /** + * The number of the node the barrier is reached. + */ + uint32_t node_number GNUNET_PACKED; + + /** + * The number of reach messages which most likely will send. + */ + uint32_t expected_number_of_reached_messages GNUNET_PACKED; + + /* followed by 0-terminated barrier name */ +}; + +GNUNET_NETWORK_STRUCT_END /** * Handle for a plugin. @@ -153,6 +216,7 @@ struct GNUNET_TESTING_Barrier unsigned int shadow; }; + /** * Advance internal pointer to next command. * @@ -196,6 +260,127 @@ GNUNET_TESTING_cmd_batch_set_current_ (const struct GNUNET_TESTING_Command *cmd, unsigned int new_ip); +// Wait for barrier to be reached by all; +// async version implies reached but does not +// wait on other peers to reach it. +/** + * FIXME: Documentation + * Create command. + * + * @param label name for command. + * @param barrier_label The name of the barrier we wait for and which will be reached. + * @param asynchronous_finish If GNUNET_YES this command will not block. Can be NULL. + * @param asynchronous_finish If GNUNET_YES this command will not block. Can be NULL. + * @param node_number The global numer of the node the cmd runs on. + * @param running_on_master Is this cmd running on the master loop. + * @param write_message Callback to write messages to the master loop. + * @return command. + */ +struct GNUNET_TESTING_Command +GNUNET_TESTING_cmd_barrier_reached ( + const char *label, + const char *barrier_label, + unsigned int asynchronous_finish, + unsigned int node_number, + unsigned int running_on_master, + GNUNET_TESTING_cmd_helper_write_cb write_message); + + +/** + * FIXME: Return type + * FIXME: Documentation + * Can we advance the barrier? + * + * @param barrier The barrier in question. + * @return GNUNET_YES if we can advance the barrier, GNUNET_NO if not. + */ +unsigned int +GNUNET_TESTING_can_barrier_advance (struct GNUNET_TESTING_Barrier *barrier); + + +/** + * FIXME: Naming + * Send Message to netjail nodes that a barrier can be advanced. + * + * @param is The interpreter loop. + * @param barrier_name The name of the barrier to advance. + * @param global_node_number The global number of the node to inform. + */ +void +GNUNET_TESTING_send_barrier_advance (struct GNUNET_TESTING_Interpreter *is, + const char *barrier_name, + unsigned int global_node_number); + + +/** + * Finish all "barrier reached" comands attached to this barrier. + * + * @param barrier The barrier in question. + */ +void +GNUNET_TESTING_finish_attached_cmds (struct GNUNET_TESTING_Interpreter *is, + const char *barrier_name); + + +/** + * Send Message to master loop that cmds being attached to a barrier. + * + * @param is The interpreter loop. + * @param barrier_name The name of the barrier to advance. + * @param subnet_number The number of the subnet. + * @param node_number The node to inform. + * @param write_message Callback to write messages to the master loop. + */ +void +GNUNET_TESTING_send_barrier_attach (struct GNUNET_TESTING_Interpreter *is, + char *barrier_name, + unsigned int global_node_number, + unsigned int expected_reaches, + GNUNET_TESTING_cmd_helper_write_cb write_message); + + +/** + * Getting a node from a map by global node number. + * + * @param nodes The map. + * @param node_number The global node number. + * @return The node. + */ +struct GNUNET_TESTING_NetjailNode * +GNUNET_TESTING_barrier_get_node (struct GNUNET_CONTAINER_MultiShortmap *nodes, + unsigned int node_number); + + +/** + * Deleting all barriers create in the context of this interpreter. + * + * @param is The interpreter. + */ +void +GNUNET_TESTING_delete_barriers (struct GNUNET_TESTING_Interpreter *is); + + +/** + * Getting a barrier from the interpreter. + * + * @param is The interpreter. + * @param barrier_name The name of the barrier. + * @return The barrier. + */ +struct GNUNET_TESTING_Barrier * +GNUNET_TESTING_get_barrier (struct GNUNET_TESTING_Interpreter *is, + const char *barrier_name); + + +/** + * Add a barrier to the loop. + * + * @param is The interpreter. + * @param barrier The barrier to add. + */ +void +GNUNET_TESTING_interpreter_add_barrier (struct GNUNET_TESTING_Interpreter *is, + struct GNUNET_TESTING_Barrier *barrier); #endif diff --git a/src/testing/testing_api_cmd_barrier.c b/src/testing/testing_api_cmd_barrier.c index 64cefd235..d27ce9d94 100644 --- a/src/testing/testing_api_cmd_barrier.c +++ b/src/testing/testing_api_cmd_barrier.c @@ -62,12 +62,14 @@ GNUNET_TESTING_send_barrier_attach (struct GNUNET_TESTING_Interpreter *is, { struct GNUNET_TESTING_CommandBarrierAttached *atm = GNUNET_new (struct GNUNET_TESTING_CommandBarrierAttached); size_t msg_length = sizeof(struct GNUNET_TESTING_CommandBarrierAttached); + size_t name_len; + name_len = strlen (barrier_name) + 1; atm->header.type = htons (GNUNET_MESSAGE_TYPE_CMDS_HELPER_BARRIER_ATTACHED); atm->header.size = htons ((uint16_t) msg_length); - atm->barrier_name = barrier_name; atm->expected_reaches = expected_reaches; atm->node_number = global_node_number; + memcpy (&atm[1], barrier_name, name_len); write_message ((struct GNUNET_MessageHeader *) atm, msg_length); GNUNET_free (atm); @@ -88,10 +90,12 @@ GNUNET_TESTING_send_barrier_advance (struct GNUNET_TESTING_Interpreter *is, { struct GNUNET_TESTING_CommandBarrierAdvanced *adm = GNUNET_new (struct GNUNET_TESTING_CommandBarrierAdvanced); size_t msg_length = sizeof(struct GNUNET_TESTING_CommandBarrierAdvanced); + size_t name_len; + name_len = strlen (barrier_name) + 1; adm->header.type = htons (GNUNET_MESSAGE_TYPE_CMDS_HELPER_BARRIER_ADVANCED); adm->header.size = htons ((uint16_t) msg_length); - adm->barrier_name = barrier_name; + memcpy (&adm[1], barrier_name, name_len); GNUNET_TESTING_send_message_to_netjail (is, global_node_number, &adm->header); @@ -225,15 +229,3 @@ GNUNET_TESTING_cmd_barrier_create (const char *label, &barrier_traits, NULL); } - -/** - * FIXME: Not sure if this is correct here - */ -struct GNUNET_TESTING_Barrier* -GNUNET_TESTING_barrier_new (const char* testcase_name) -{ - struct GNUNET_TESTING_Barrier *barr; - barr = GNUNET_new (struct GNUNET_TESTING_Barrier); - barr->name = GNUNET_strdup (testcase_name); - return barr; -} diff --git a/src/testing/testing_api_cmd_barrier_reached.c b/src/testing/testing_api_cmd_barrier_reached.c index 58f420214..eca422b1d 100644 --- a/src/testing/testing_api_cmd_barrier_reached.c +++ b/src/testing/testing_api_cmd_barrier_reached.c @@ -91,6 +91,7 @@ barrier_reached_run (void *cls, struct CommandListEntry *cle; size_t msg_length; struct GNUNET_TESTING_CommandBarrierReached *msg; + size_t name_len; barrier = GNUNET_TESTING_get_barrier (is, brs->barrier_name); if (NULL == barrier) @@ -106,7 +107,7 @@ barrier_reached_run (void *cls, //FIXME cmd uninitialized GNUNET_assert (NULL != cmd); cmd->asynchronous_finish = GNUNET_YES; - GNUNET_TESTING_finish_attached_cmds (is, barrier); + GNUNET_TESTING_finish_attached_cmds (is, barrier->name); } else if (GNUNET_NO == brs->asynchronous_finish) { @@ -126,11 +127,12 @@ barrier_reached_run (void *cls, } if (GNUNET_NO == brs->running_on_master) { + name_len = strlen (barrier->name) + 1; msg_length = sizeof(struct GNUNET_TESTING_CommandBarrierReached); msg = GNUNET_new (struct GNUNET_TESTING_CommandBarrierReached); msg->header.size = htons ((uint16_t) msg_length); msg->header.type = htons (GNUNET_MESSAGE_TYPE_CMDS_HELPER_BARRIER_REACHED); - msg->barrier_name = barrier->name; + memcpy (&msg[1], barrier->name, name_len); msg->node_number = brs->node_number; brs->write_message ((struct GNUNET_MessageHeader *) msg, msg_length); } diff --git a/src/testing/testing_api_cmd_netjail_start_testsystem.c b/src/testing/testing_api_cmd_netjail_start_testsystem.c index a1712c7b5..09f767b7a 100644 --- a/src/testing/testing_api_cmd_netjail_start_testsystem.c +++ b/src/testing/testing_api_cmd_netjail_start_testsystem.c @@ -350,9 +350,11 @@ barrier_attached (struct NetJailState *ns, const struct struct GNUNET_TESTING_Barrier *barrier; struct GNUNET_ShortHashCode key; struct GNUNET_HashCode hc; + const char *barrier_name; am = (struct GNUNET_TESTING_CommandBarrierAttached *) message; - barrier = GNUNET_TESTING_get_barrier (ns->is, am->barrier_name); + barrier_name = (const char *) &am[1]; + barrier = GNUNET_TESTING_get_barrier (ns->is, barrier_name); GNUNET_assert (NULL != barrier && GNUNET_NO == barrier->shadow); node = GNUNET_TESTING_barrier_get_node (barrier->nodes, am->node_number); if (NULL == node) @@ -376,18 +378,19 @@ void barrier_reached (struct NetJailState *ns, const struct GNUNET_MessageHeader *message) { + struct GNUNET_TESTING_Barrier *barrier; + const char *barrier_name; struct GNUNET_TESTING_CommandBarrierReached *rm = (struct GNUNET_TESTING_CommandBarrierReached *) message; - struct GNUNET_TESTING_Barrier *barrier = GNUNET_TESTING_get_barrier (ns->is, - rm-> - barrier_name); + barrier_name = (const char *) &rm[1]; + barrier = GNUNET_TESTING_get_barrier (ns->is, barrier_name); GNUNET_assert (NULL != barrier && GNUNET_NO == barrier->shadow); barrier->reached++; if (GNUNET_TESTING_can_barrier_advance (barrier)) { - GNUNET_TESTING_finish_attached_cmds (ns->is, barrier); + GNUNET_TESTING_finish_attached_cmds (ns->is, barrier->name); } } @@ -699,19 +702,21 @@ start_helper (struct NetJailState *ns, for (pos = barriers->head; NULL != pos; pos = pos->next) { - barrier = GNUNET_TESTING_get_barrier (ns->is, pos->barrier->name); + barrier = GNUNET_TESTING_get_barrier (ns->is, pos->barrier_name); if (NULL == barrier || GNUNET_YES == barrier->shadow) { - GNUNET_TESTING_interpreter_add_barrier (ns->is, pos->barrier); - barrier = pos->barrier; + barrier = GNUNET_new (struct GNUNET_TESTING_Barrier); + barrier->name = pos->barrier_name; + GNUNET_TESTING_interpreter_add_barrier (ns->is, barrier); + barrier->nodes = GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO); } GNUNET_assert (NULL != node); barrier_node = GNUNET_new (struct GNUNET_TESTING_NetjailNode); barrier_node->node_number = node->node_number; - barrier_node->expected_reaches = pos->barrier->expected_reaches; + barrier_node->expected_reaches = pos->expected_reaches; barrier->expected_reaches = barrier->expected_reaches - + pos->barrier->expected_reaches; + + pos->expected_reaches; GNUNET_CRYPTO_hash (&(node->node_number), sizeof(node->node_number), &hc); memcpy (&key, &hc, sizeof (key)); GNUNET_CONTAINER_multishortmap_put (barrier->nodes, diff --git a/src/testing/testing_api_loop.c b/src/testing/testing_api_loop.c index e8680fc13..d7456b91f 100644 --- a/src/testing/testing_api_loop.c +++ b/src/testing/testing_api_loop.c @@ -687,6 +687,31 @@ free_barrier_node_cb (void *cls, } +/** + * Getting a barrier from the interpreter. + * + * @param is The interpreter. + * @param barrier_name The name of the barrier. + * @return The barrier. + */ +struct GNUNET_TESTING_Barrier * +GNUNET_TESTING_get_barrier (struct GNUNET_TESTING_Interpreter *is, + const char *barrier_name) +{ + struct GNUNET_HashCode hc; + struct GNUNET_ShortHashCode create_key; + struct GNUNET_TESTING_Barrier *barrier; + + GNUNET_CRYPTO_hash (barrier_name, strlen (barrier_name), &hc); + memcpy (&create_key, + &hc, + sizeof (create_key)); + barrier = GNUNET_CONTAINER_multishortmap_get (is->barriers, &create_key); + // GNUNET_free (create_key); + return barrier; +} + + /** * Finish all "barrier reached" comands attached to this barrier. * @@ -694,10 +719,12 @@ free_barrier_node_cb (void *cls, */ void GNUNET_TESTING_finish_attached_cmds (struct GNUNET_TESTING_Interpreter *is, - struct GNUNET_TESTING_Barrier *barrier) + const char *barrier_name) { struct CommandListEntry *pos; struct FreeBarrierNodeCbCls *free_barrier_node_cb_cls; + struct GNUNET_TESTING_Barrier *barrier = GNUNET_TESTING_get_barrier (is, + barrier_name); while (NULL != (pos = barrier->cmds_head)) { @@ -771,29 +798,6 @@ GNUNET_TESTING_delete_barriers (struct GNUNET_TESTING_Interpreter *is) GNUNET_CONTAINER_multishortmap_destroy (is->barriers); } -/** - * Getting a barrier from the interpreter. - * - * @param is The interpreter. - * @param barrier_name The name of the barrier. - * @return The barrier. - */ -struct GNUNET_TESTING_Barrier * -GNUNET_TESTING_get_barrier (struct GNUNET_TESTING_Interpreter *is, - const char *barrier_name) -{ - struct GNUNET_HashCode hc; - struct GNUNET_ShortHashCode create_key; - struct GNUNET_TESTING_Barrier *barrier; - - GNUNET_CRYPTO_hash (barrier_name, strlen (barrier_name), &hc); - memcpy (&create_key, - &hc, - sizeof (create_key)); - barrier = GNUNET_CONTAINER_multishortmap_get (is->barriers, &create_key); - // GNUNET_free (create_key); - return barrier; -} /** * Add a barrier to the loop. -- cgit v1.2.3