Skip to content

Commit

Permalink
lib: reorg native message header
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hopps <[email protected]>
  • Loading branch information
choppsv1 committed Dec 13, 2023
1 parent f7cf0e9 commit 66fec31
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 37 deletions.
8 changes: 4 additions & 4 deletions lib/mgmt_be_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions lib/mgmt_fe_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/mgmt_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

Expand Down
2 changes: 1 addition & 1 deletion lib/mgmt_msg_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
75 changes: 61 additions & 14 deletions lib/mgmt_msg_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,58 +26,105 @@ 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
*/
#define MGMT_MSG_CODE_ERROR 0
#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[];
};
_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[];
};
_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[];
};
Expand Down
8 changes: 4 additions & 4 deletions mgmtd/mgmt_be_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,18 +540,18 @@ 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;
case MGMT_MSG_CODE_TREE_DATA:
/* 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);
Expand All @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions mgmtd/mgmt_fe_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions mgmtd/mgmt_txn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 66fec31

Please sign in to comment.