From 66fec3102b2c74c1323a6c5f624e636f861b4a63 Mon Sep 17 00:00:00 2001 From: Christian Hopps Date: Wed, 13 Dec 2023 17:32:43 -0500 Subject: [PATCH] lib: reorg native message header Signed-off-by: Christian Hopps --- lib/mgmt_be_client.c | 8 ++--- lib/mgmt_fe_client.c | 14 ++++---- lib/mgmt_msg.c | 2 +- lib/mgmt_msg_native.c | 2 +- lib/mgmt_msg_native.h | 75 +++++++++++++++++++++++++++++++++-------- mgmtd/mgmt_be_adapter.c | 8 ++--- mgmtd/mgmt_fe_adapter.c | 8 ++--- mgmtd/mgmt_txn.c | 4 +-- 8 files changed, 84 insertions(+), 37 deletions(-) diff --git a/lib/mgmt_be_client.c b/lib/mgmt_be_client.c index f58dbf6f44ca..7f5073a658bf 100644 --- a/lib/mgmt_be_client.c +++ b/lib/mgmt_be_client.c @@ -799,7 +799,7 @@ static enum nb_error be_client_send_tree_data_batch(const struct lyd_node *tree, darr_append_nz(buf, offsetof(typeof(*tree_msg), result)); tree_msg = (typeof(tree_msg))buf; - tree_msg->txn_id = args->txn_id; + tree_msg->refer_id = args->txn_id; tree_msg->req_id = args->req_id; tree_msg->code = MGMT_MSG_CODE_TREE_DATA; tree_msg->result_type = args->result_type; @@ -848,7 +848,7 @@ static void be_client_handle_get_tree(struct mgmt_be_client *client, args = XMALLOC(MTYPE_MGMTD_BE_GT_CB_ARGS, sizeof(*args)); args->client = client; - args->txn_id = get_tree_msg->txn_id; + args->txn_id = get_tree_msg->refer_id; args->req_id = get_tree_msg->req_id; args->result_type = get_tree_msg->result_type; nb_op_walk(get_tree_msg->xpath, NULL, 0, true, NULL, NULL, @@ -864,7 +864,7 @@ static void be_client_handle_native_msg(struct mgmt_be_client *client, struct mgmt_msg_header *msg, size_t msg_len) { - uint64_t txn_id = msg->txn_id; + uint64_t txn_id = msg->refer_id; switch (msg->code) { case MGMT_MSG_CODE_GET_TREE: @@ -875,7 +875,7 @@ static void be_client_handle_native_msg(struct mgmt_be_client *client, " req-id %" PRIu64 " code %u to client %s", txn_id, msg->req_id, msg->code, client->name); - be_client_send_error(client, msg->txn_id, msg->req_id, false, -1, + be_client_send_error(client, msg->refer_id, msg->req_id, false, -1, "BE cilent %s recv msg unknown txn-id %" PRIu64, client->name, txn_id); break; diff --git a/lib/mgmt_fe_client.c b/lib/mgmt_fe_client.c index 549a72f47dda..dccfb1dae620 100644 --- a/lib/mgmt_fe_client.c +++ b/lib/mgmt_fe_client.c @@ -325,7 +325,7 @@ int mgmt_fe_send_get_tree_req(struct mgmt_fe_client *client, int ret; msg = XCALLOC(MTYPE_MGMTD_FE_GET_DATA_MSG, mlen); - msg->session_id = session_id; + msg->refer_id = session_id; msg->req_id = req_id; msg->code = MGMT_MSG_CODE_GET_TREE; msg->result_type = result_type; @@ -518,13 +518,13 @@ static void fe_client_handle_native_msg(struct mgmt_fe_client *client, struct mgmt_msg_error *err_msg; MGMTD_FE_CLIENT_DBG("Got GET_TREE reply for session-id %" PRIu64, - msg->session_id); + msg->refer_id); - session = mgmt_fe_find_session_by_session_id(client, msg->session_id); + session = mgmt_fe_find_session_by_session_id(client, msg->refer_id); if (!session || !session->client) { MGMTD_FE_CLIENT_ERR("No session for received native msg session-id %" PRIu64, - msg->session_id); + msg->refer_id); return; } @@ -536,7 +536,7 @@ static void fe_client_handle_native_msg(struct mgmt_fe_client *client, err_msg = (typeof(err_msg))msg; session->client->cbs.error_notify(client, client->user_data, session->client_id, - msg->session_id, + msg->refer_id, session->user_ctx, msg->req_id, err_msg->error, err_msg->errstr); @@ -548,7 +548,7 @@ static void fe_client_handle_native_msg(struct mgmt_fe_client *client, tree_msg = (typeof(tree_msg))msg; session->client->cbs.get_tree_notify(client, client->user_data, session->client_id, - msg->session_id, + msg->refer_id, session->user_ctx, msg->req_id, MGMTD_DS_OPERATIONAL, @@ -560,7 +560,7 @@ static void fe_client_handle_native_msg(struct mgmt_fe_client *client, default: MGMTD_FE_CLIENT_ERR("unknown native message session-id %" PRIu64 " req-id %" PRIu64 " code %u", - msg->session_id, msg->req_id, msg->code); + msg->refer_id, msg->req_id, msg->code); break; } } diff --git a/lib/mgmt_msg.c b/lib/mgmt_msg.c index d8a7fde435f9..b03dbe8cc3e2 100644 --- a/lib/mgmt_msg.c +++ b/lib/mgmt_msg.c @@ -348,7 +348,7 @@ int mgmt_msg_send_msg(struct mgmt_msg_state *ms, uint8_t version, void *msg, MGMT_MSG_DBG( dbgtag, "Sending native msg sess/txn-id %"PRIu64" req-id %"PRIu64" code %u", - native_msg->session_id, native_msg->req_id, native_msg->code); + native_msg->refer_id, native_msg->req_id, native_msg->code); } diff --git a/lib/mgmt_msg_native.c b/lib/mgmt_msg_native.c index 436f01998c76..61ed9f732540 100644 --- a/lib/mgmt_msg_native.c +++ b/lib/mgmt_msg_native.c @@ -22,7 +22,7 @@ int vmgmt_msg_native_send_error(struct msg_conn *conn, uint64_t sess_or_txn_id, int ret; msg = XMALLOC(MTYPE_MSG_NATIVE_ERROR, 1024); - msg->session_id = sess_or_txn_id; + msg->refer_id = sess_or_txn_id; msg->req_id = req_id; msg->code = MGMT_MSG_CODE_ERROR; msg->error = error; diff --git a/lib/mgmt_msg_native.h b/lib/mgmt_msg_native.h index a3b456efaac1..472c84dea337 100644 --- a/lib/mgmt_msg_native.h +++ b/lib/mgmt_msg_native.h @@ -26,6 +26,27 @@ extern "C" { DECLARE_MTYPE(MSG_NATIVE_MSG); DECLARE_MTYPE(MSG_NATIVE_ERROR); +/* + * Adding New Messages Types + * ------------------------- + * + * Native messages structs have 2 simple rules: + * + * 1) All fields should be naturally aligned. + * 2) Any required padding should be explicitly reserved. + * + * This is so for all intents and purposes the messages may be read and written + * direct from "the wire", easily, using common programming languages (e.g., + * C, rust, go, python, ...) + * + * Additionally by design fixed fields precede the variable length data which + * comes at the end. The zero length arrays fields are aligned such that this is so: + * + * sizeof(struct mgmt_msg_foo) == offsetof(struct mgmt_msg_foo, field) + * + * This allows things like `value = (HDR + 1)` to work. + */ + /* * Native message codes */ @@ -33,29 +54,40 @@ DECLARE_MTYPE(MSG_NATIVE_ERROR); #define MGMT_MSG_CODE_GET_TREE 1 #define MGMT_MSG_CODE_TREE_DATA 2 -/* - * A note on alignments: The zero length arrays fields are aligned such that - * this is so: - * - * sizeof(struct mgmt_msg_foo) == offsetof(struct mgmt_msg_foo, field) +/** + * struct mgmt_msg_header - Header common to all native messages. * - * This allows things like `ptr = darr_append_n(A, sizeof(*ptr))` - * to work + * @code: the actual type of the message. + * @resv: Set to zero, ignore on receive. + * @vsplit: If a variable section is split in 2, the length of first part. + * @refer_id: the session, txn, conn, etc, this message is associated with. + * @req_id: the request this message is for. */ - - struct mgmt_msg_header { - union { - uint64_t session_id; - uint64_t txn_id; - }; - uint64_t req_id; uint16_t code; + uint16_t resv; + uint32_t vsplit; + uint64_t refer_id; + uint64_t req_id; }; +_Static_assert(sizeof(struct mgmt_msg_header) == 3 * 8, "Bad padding"); +_Static_assert(sizeof(struct mgmt_msg_header) == + offsetof(struct mgmt_msg_header, req_id) + + sizeof(((struct mgmt_msg_header *)0)->req_id), + "Size mismatch"); +/** + * struct mgmt_msg_error - Common error message. + * @error: An error value. + * @errst: Description of error can be 0 length. + * + * This common error message can be used for replies for many msg requests + * (req_id). + */ struct mgmt_msg_error { struct mgmt_msg_header; int16_t error; + uint8_t resv2[6]; alignas(8) char errstr[]; }; @@ -63,9 +95,15 @@ _Static_assert(sizeof(struct mgmt_msg_error) == offsetof(struct mgmt_msg_error, errstr), "Size mismatch"); +/** + * struct mgmt_msg_get_tree - Message carrying xpath query request. + * @result_type: ``LYD_FORMAT`` for the returned result. + * @xpath: the query for the data to return. + */ struct mgmt_msg_get_tree { struct mgmt_msg_header; uint8_t result_type; + uint8_t resv2[7]; alignas(8) char xpath[]; }; @@ -73,11 +111,20 @@ _Static_assert(sizeof(struct mgmt_msg_get_tree) == offsetof(struct mgmt_msg_get_tree, xpath), "Size mismatch"); +/** + * struct mgmt_msg_tree_data - Message carrying tree data. + * @partial_error: If the full result could not be returned do to this error. + * @result_type: ``LYD_FORMAT`` for format of the @result value. + * @more: if this is a partial return and there will be more coming. + * @result: The tree data in @result_type format. + * + */ struct mgmt_msg_tree_data { struct mgmt_msg_header; int8_t partial_error; uint8_t result_type; uint8_t more; + uint8_t resv2[5]; alignas(8) uint8_t result[]; }; diff --git a/mgmtd/mgmt_be_adapter.c b/mgmtd/mgmt_be_adapter.c index 57d63551e2de..4f1c5b5b17bc 100644 --- a/mgmtd/mgmt_be_adapter.c +++ b/mgmtd/mgmt_be_adapter.c @@ -540,10 +540,10 @@ static void be_adapter_handle_native_msg(struct mgmt_be_client_adapter *adapter, case MGMT_MSG_CODE_ERROR: error_msg = (typeof(error_msg))msg; MGMTD_BE_ADAPTER_DBG("Got ERROR from '%s' txn-id %" PRIx64, - adapter->name, msg->txn_id); + adapter->name, msg->refer_id); /* Forward the reply to the txn module */ - mgmt_txn_notify_error(adapter, msg->txn_id, msg->req_id, + mgmt_txn_notify_error(adapter, msg->refer_id, msg->req_id, error_msg->error, error_msg->errstr); break; @@ -551,7 +551,7 @@ static void be_adapter_handle_native_msg(struct mgmt_be_client_adapter *adapter, /* tree data from a backend client */ tree_msg = (typeof(tree_msg))msg; MGMTD_BE_ADAPTER_DBG("Got TREE_DATA from '%s' txn-id %" PRIx64, - adapter->name, msg->txn_id); + adapter->name, msg->refer_id); /* Forward the reply to the txn module */ mgmt_txn_notify_tree_data_reply(adapter, tree_msg, msg_len); @@ -560,7 +560,7 @@ static void be_adapter_handle_native_msg(struct mgmt_be_client_adapter *adapter, MGMTD_BE_ADAPTER_ERR("unknown native message txn-id %" PRIu64 " req-id %" PRIu64 " code %u from BE client for adapter %s", - msg->txn_id, msg->req_id, msg->code, + msg->refer_id, msg->req_id, msg->code, adapter->name); break; } diff --git a/mgmtd/mgmt_fe_adapter.c b/mgmtd/mgmt_fe_adapter.c index dc9654987797..87c67491b6f4 100644 --- a/mgmtd/mgmt_fe_adapter.c +++ b/mgmtd/mgmt_fe_adapter.c @@ -1089,7 +1089,7 @@ static int fe_adapter_send_tree_data(struct mgmt_fe_session_ctx *session, darr_append_n(buf, offsetof(typeof(*msg), result)); msg = (typeof(msg))buf; - msg->session_id = session->session_id; + msg->refer_id = session->session_id; msg->req_id = req_id; msg->code = MGMT_MSG_CODE_TREE_DATA; msg->partial_error = partial_error; @@ -1200,10 +1200,10 @@ static void fe_adapter_handle_native_msg(struct mgmt_fe_client_adapter *adapter, { struct mgmt_fe_session_ctx *session; - session = mgmt_session_id2ctx(msg->session_id); + session = mgmt_session_id2ctx(msg->refer_id); if (!session) { MGMTD_FE_ADAPTER_ERR("adapter %s: recv msg unknown session-id %" PRIu64, - adapter->name, msg->session_id); + adapter->name, msg->refer_id); return; } assert(session->adapter == adapter); @@ -1216,7 +1216,7 @@ static void fe_adapter_handle_native_msg(struct mgmt_fe_client_adapter *adapter, MGMTD_FE_ADAPTER_ERR("unknown native message session-id %" PRIu64 " req-id %" PRIu64 " code %u to FE adapter %s", - msg->session_id, msg->req_id, msg->code, + msg->refer_id, msg->req_id, msg->code, adapter->name); break; } diff --git a/mgmtd/mgmt_txn.c b/mgmtd/mgmt_txn.c index b94fd3bad47b..98189317fa11 100644 --- a/mgmtd/mgmt_txn.c +++ b/mgmtd/mgmt_txn.c @@ -2381,7 +2381,7 @@ int mgmt_txn_send_get_tree_oper(uint64_t txn_id, uint64_t req_id, get_tree->xpath = XSTRDUP(MTYPE_MGMTD_XPATH, xpath); msg = XCALLOC(MTYPE_MSG_NATIVE_MSG, mlen); - msg->txn_id = txn_id; + msg->refer_id = txn_id; msg->req_id = req_id; msg->code = MGMT_MSG_CODE_GET_TREE; /* Always operate with the binary format in the backend */ @@ -2479,7 +2479,7 @@ int mgmt_txn_notify_tree_data_reply(struct mgmt_be_client_adapter *adapter, struct mgmt_msg_tree_data *data_msg, size_t msg_len) { - uint64_t txn_id = data_msg->txn_id; + uint64_t txn_id = data_msg->refer_id; uint64_t req_id = data_msg->req_id; enum mgmt_be_client_id id = adapter->id;