summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorChristian Hopps <chopps@labn.net>2024-01-30 09:05:18 +0100
committerChristian Hopps <chopps@labn.net>2024-01-30 10:57:10 +0100
commit25bb8b203cdbf3b40f938add7352666146183c03 (patch)
tree90ba100c73a7f97401bd9362daa9a0a86a9f957c /lib
parentMerge pull request #15238 from louis-6wind/bgp-leak-network (diff)
downloadfrr-25bb8b203cdbf3b40f938add7352666146183c03.tar.xz
frr-25bb8b203cdbf3b40f938add7352666146183c03.zip
lib: mgmtd: add YANG notification support
Signed-off-by: Christian Hopps <chopps@labn.net>
Diffstat (limited to 'lib')
-rw-r--r--lib/mgmt_be_client.c90
-rw-r--r--lib/mgmt_be_client.h9
-rw-r--r--lib/mgmt_fe_client.c59
-rw-r--r--lib/mgmt_fe_client.h5
-rw-r--r--lib/mgmt_msg_native.c1
-rw-r--r--lib/mgmt_msg_native.h25
-rw-r--r--lib/northbound.c17
-rw-r--r--lib/northbound.h17
-rw-r--r--lib/yang.c28
-rw-r--r--lib/yang.h16
10 files changed, 257 insertions, 10 deletions
diff --git a/lib/mgmt_be_client.c b/lib/mgmt_be_client.c
index 463aefdf2..070d6627b 100644
--- a/lib/mgmt_be_client.c
+++ b/lib/mgmt_be_client.c
@@ -311,6 +311,90 @@ static int be_client_send_error(struct mgmt_be_client *client, uint64_t txn_id,
return ret;
}
+void mgmt_be_send_notification(struct lyd_node *tree)
+{
+ struct mgmt_be_client *client = __be_client;
+ struct mgmt_msg_notify_data *msg = NULL;
+ LYD_FORMAT format = LYD_JSON;
+ uint8_t **darrp;
+ LY_ERR err;
+
+ assert(tree);
+
+ MGMTD_BE_CLIENT_DBG("%s: sending YANG notification: %s", __func__,
+ tree->schema->name);
+ /*
+ * Allocate a message and append the data to it using `format`
+ */
+ msg = mgmt_msg_native_alloc_msg(struct mgmt_msg_notify_data, 0,
+ MTYPE_MSG_NATIVE_NOTIFY);
+ msg->code = MGMT_MSG_CODE_NOTIFY;
+ msg->result_type = format;
+
+ darrp = mgmt_msg_native_get_darrp(msg);
+ err = yang_print_tree_append(darrp, tree, format,
+ (LYD_PRINT_SHRINK | LYD_PRINT_WD_EXPLICIT |
+ LYD_PRINT_WITHSIBLINGS));
+ if (err) {
+ flog_err(EC_LIB_LIBYANG,
+ "%s: error creating notification data: %s", __func__,
+ ly_strerrcode(err));
+ goto done;
+ }
+
+ (void)be_client_send_native_msg(client, msg,
+ mgmt_msg_native_get_msg_len(msg), false);
+done:
+ mgmt_msg_native_free_msg(msg);
+ lyd_free_all(tree);
+}
+
+/*
+ * Convert old style NB notification data into new MGMTD YANG tree and send.
+ */
+static int mgmt_be_notification_send(void *arg, const char *xpath,
+ struct list *args)
+{
+ struct lyd_node *root = NULL;
+ struct lyd_node *dnode;
+ struct yang_data *data;
+ struct listnode *ln;
+ LY_ERR err;
+
+ MGMTD_BE_CLIENT_DBG("%s: sending notification: %s", __func__, xpath);
+
+ /*
+ * Convert yang data args list to a libyang data tree
+ */
+ for (ALL_LIST_ELEMENTS_RO(args, ln, data)) {
+ err = lyd_new_path(root, ly_native_ctx, data->xpath,
+ data->value, LYD_NEW_PATH_UPDATE, &dnode);
+ if (err != LY_SUCCESS) {
+lyerr:
+ flog_err(EC_LIB_LIBYANG,
+ "%s: error creating notification data: %s",
+ __func__, ly_strerrcode(err));
+ if (root)
+ lyd_free_all(root);
+ return 1;
+ }
+ if (!root) {
+ root = dnode;
+ while (root->parent)
+ root = lyd_parent(root);
+ }
+ }
+
+ if (!root) {
+ err = lyd_new_path(NULL, ly_native_ctx, xpath, "", 0, &root);
+ if (err)
+ goto lyerr;
+ }
+
+ mgmt_be_send_notification(root);
+ return 0;
+}
+
static int mgmt_be_send_txn_reply(struct mgmt_be_client *client_ctx,
uint64_t txn_id, bool create)
{
@@ -824,7 +908,7 @@ static enum nb_error be_client_send_tree_data_batch(const struct lyd_node *tree,
darrp = mgmt_msg_native_get_darrp(tree_msg);
err = yang_print_tree_append(darrp, tree, args->result_type,
- (LYD_PRINT_WD_EXPLICIT |
+ (LYD_PRINT_SHRINK | LYD_PRINT_WD_EXPLICIT |
LYD_PRINT_WITHSIBLINGS));
if (err) {
ret = NB_ERR;
@@ -1083,6 +1167,10 @@ struct mgmt_be_client *mgmt_be_client_create(const char *client_name,
MGMTD_BE_MAX_NUM_MSG_WRITE, MGMTD_BE_MAX_MSG_LEN, false,
"BE-client", MGMTD_DBG_BE_CLIENT_CHECK());
+ /* Hook to receive notifications */
+ hook_register_arg(nb_notification_send, mgmt_be_notification_send,
+ client);
+
MGMTD_BE_CLIENT_DBG("Initialized client '%s'", client_name);
return client;
diff --git a/lib/mgmt_be_client.h b/lib/mgmt_be_client.h
index 8ad482cac..930cbf0cd 100644
--- a/lib/mgmt_be_client.h
+++ b/lib/mgmt_be_client.h
@@ -136,6 +136,15 @@ extern int mgmt_be_send_subscr_req(struct mgmt_be_client *client,
bool subscr_xpaths, int num_xpaths,
char **reg_xpaths);
+/**
+ * mgmt_be_notification_send() - send a YANG notification to FE clients.
+ * @tree: libyang tree for the notification. The tree will be freed by
+ * this function.
+ *
+ */
+extern void mgmt_be_send_notification(struct lyd_node *tree);
+
+
/*
* Destroy backend client and cleanup everything.
*/
diff --git a/lib/mgmt_fe_client.c b/lib/mgmt_fe_client.c
index 92619f4f7..e94a6d291 100644
--- a/lib/mgmt_fe_client.c
+++ b/lib/mgmt_fe_client.c
@@ -507,19 +507,24 @@ static void fe_client_handle_native_msg(struct mgmt_fe_client *client,
struct mgmt_msg_header *msg,
size_t msg_len)
{
- struct mgmt_fe_client_session *session;
+ struct mgmt_fe_client_session *session = NULL;
+ struct mgmt_msg_notify_data *notify_msg;
struct mgmt_msg_tree_data *tree_msg;
struct mgmt_msg_error *err_msg;
+ char *notify_data = NULL;
- MGMTD_FE_CLIENT_DBG("Got GET_TREE reply for session-id %" PRIu64,
+ MGMTD_FE_CLIENT_DBG("Got native message for session-id %" PRIu64,
msg->refer_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->refer_id);
- return;
+ if (msg->code != MGMT_MSG_CODE_NOTIFY) {
+ 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->refer_id);
+ return;
+ }
}
switch (msg->code) {
@@ -559,6 +564,44 @@ static void fe_client_handle_native_msg(struct mgmt_fe_client *client,
msg_len - sizeof(*tree_msg),
tree_msg->partial_error);
break;
+ case MGMT_MSG_CODE_NOTIFY:
+ notify_msg = (typeof(notify_msg))msg;
+ if (msg_len < sizeof(*notify_msg)) {
+ MGMTD_FE_CLIENT_ERR("Corrupt notify-data msg recv");
+ return;
+ }
+
+ if (notify_msg->result_type != LYD_LYB &&
+ !MGMT_MSG_VALIDATE_NUL_TERM(notify_msg, msg_len)) {
+ MGMTD_FE_CLIENT_ERR("Corrupt error msg recv");
+ return;
+ }
+ if (notify_msg->result_type == LYD_JSON)
+ notify_data = (char *)notify_msg->result;
+ else
+ notify_data =
+ yang_convert_lyd_format(notify_msg->result,
+ msg_len,
+ notify_msg->result_type,
+ LYD_JSON, true);
+ if (!notify_data) {
+ MGMTD_FE_CLIENT_ERR("Can't convert format %d to JSON",
+ notify_msg->result_type);
+ return;
+ }
+ FOREACH_SESSION_IN_LIST (client, session) {
+ if (!session->client->cbs.async_notification)
+ continue;
+
+ session->client->cbs
+ .async_notification(client, client->user_data,
+ session->client_id,
+ session->user_ctx,
+ notify_data);
+ }
+ if (notify_msg->result_type != LYD_JSON)
+ darr_free(notify_data);
+ break;
default:
MGMTD_FE_CLIENT_ERR("unknown native message session-id %" PRIu64
" req-id %" PRIu64 " code %u",
diff --git a/lib/mgmt_fe_client.h b/lib/mgmt_fe_client.h
index 3abe29b1c..018f71ddf 100644
--- a/lib/mgmt_fe_client.h
+++ b/lib/mgmt_fe_client.h
@@ -114,6 +114,11 @@ struct mgmt_fe_client_cbs {
LYD_FORMAT result_type, void *result, size_t len,
int partial_error);
+ /* Called with asynchronous notifications from backends */
+ int (*async_notification)(struct mgmt_fe_client *client,
+ uintptr_t user_data, uint64_t client_id,
+ uintptr_t session_ctx, const char *result);
+
/* Called when new native error is returned */
int (*error_notify)(struct mgmt_fe_client *client, uintptr_t user_data,
uint64_t client_id, uint64_t session_id,
diff --git a/lib/mgmt_msg_native.c b/lib/mgmt_msg_native.c
index a9b26718d..d27c5d3a2 100644
--- a/lib/mgmt_msg_native.c
+++ b/lib/mgmt_msg_native.c
@@ -14,6 +14,7 @@ DEFINE_MTYPE(MSG_NATIVE, MSG_NATIVE_ERROR, "native error msg");
DEFINE_MTYPE(MSG_NATIVE, MSG_NATIVE_GET_TREE, "native get tree msg");
DEFINE_MTYPE(MSG_NATIVE, MSG_NATIVE_TREE_DATA, "native tree data msg");
DEFINE_MTYPE(MSG_NATIVE, MSG_NATIVE_GET_DATA, "native get data msg");
+DEFINE_MTYPE(MSG_NATIVE, MSG_NATIVE_NOTIFY, "native get data msg");
int vmgmt_msg_native_send_error(struct msg_conn *conn, uint64_t sess_or_txn_id,
uint64_t req_id, bool short_circuit_ok,
diff --git a/lib/mgmt_msg_native.h b/lib/mgmt_msg_native.h
index 069cb9b15..93a94fc15 100644
--- a/lib/mgmt_msg_native.h
+++ b/lib/mgmt_msg_native.h
@@ -143,6 +143,7 @@ DECLARE_MTYPE(MSG_NATIVE_ERROR);
DECLARE_MTYPE(MSG_NATIVE_GET_TREE);
DECLARE_MTYPE(MSG_NATIVE_TREE_DATA);
DECLARE_MTYPE(MSG_NATIVE_GET_DATA);
+DECLARE_MTYPE(MSG_NATIVE_NOTIFY);
/*
* Native message codes
@@ -151,6 +152,7 @@ DECLARE_MTYPE(MSG_NATIVE_GET_DATA);
#define MGMT_MSG_CODE_GET_TREE 1
#define MGMT_MSG_CODE_TREE_DATA 2
#define MGMT_MSG_CODE_GET_DATA 3
+#define MGMT_MSG_CODE_NOTIFY 4
/**
* struct mgmt_msg_header - Header common to all native messages.
@@ -257,8 +259,29 @@ _Static_assert(sizeof(struct mgmt_msg_get_data) ==
offsetof(struct mgmt_msg_get_data, xpath),
"Size mismatch");
+/**
+ * struct mgmt_msg_notify_data - Message carrying notification data.
+ *
+ * @result_type: ``LYD_FORMAT`` for format of the @result value.
+ * @result: The tree data in @result_type format.
+ *
+ */
+struct mgmt_msg_notify_data {
+ struct mgmt_msg_header;
+ uint8_t result_type;
+ uint8_t resv2[7];
+
+ alignas(8) uint8_t result[];
+};
+_Static_assert(sizeof(struct mgmt_msg_notify_data) ==
+ offsetof(struct mgmt_msg_notify_data, result),
+ "Size mismatch");
+
+/*
+ * Validate that the message ends in a NUL terminating byte
+ */
#define MGMT_MSG_VALIDATE_NUL_TERM(msgp, len) \
- ((len) >= sizeof(*msg) + 1 && ((char *)msgp)[(len)-1] == 0)
+ ((len) >= sizeof(*msgp) + 1 && ((char *)msgp)[(len)-1] == 0)
/**
diff --git a/lib/northbound.c b/lib/northbound.c
index b1da3315d..a0b1bd18c 100644
--- a/lib/northbound.c
+++ b/lib/northbound.c
@@ -2068,6 +2068,23 @@ int nb_notification_send(const char *xpath, struct list *arguments)
return ret;
}
+DEFINE_HOOK(nb_notification_tree_send, (struct lyd_node *tree), (tree));
+
+int nb_notification_tree_send(struct lyd_node *tree)
+{
+ int ret;
+
+ assert(tree);
+
+ DEBUGD(&nb_dbg_notif, "northbound tree notification: %s",
+ tree->schema->name);
+
+ ret = hook_call(nb_notification_tree_send, tree);
+ lyd_free_all(tree);
+
+ return ret;
+}
+
/* Running configuration user pointers management. */
struct nb_config_entry {
char xpath[XPATH_MAXLEN];
diff --git a/lib/northbound.h b/lib/northbound.h
index 2d9643e7b..9279122de 100644
--- a/lib/northbound.h
+++ b/lib/northbound.h
@@ -1441,6 +1441,10 @@ extern bool nb_cb_operation_is_valid(enum nb_cb_operation operation,
const struct lysc_node *snode);
/*
+ * DEPRECATED: This call and infra should no longer be used. Instead,
+ * the mgmtd supported tree based call `nb_notification_tree_send` should be
+ * used instead
+ *
* Send a YANG notification. This is a no-op unless the 'nb_notification_send'
* hook was registered by a northbound plugin.
*
@@ -1457,6 +1461,19 @@ extern bool nb_cb_operation_is_valid(enum nb_cb_operation operation,
extern int nb_notification_send(const char *xpath, struct list *arguments);
/*
+ * Send a YANG notification from a backend . This is a no-op unless th
+ * 'nb_notification_tree_send' hook was registered by a northbound plugin.
+ *
+ * tree
+ * The libyang tree for the notification. The tree will be freed by
+ * this call.
+ *
+ * Returns:
+ * NB_OK on success, NB_ERR otherwise.
+ */
+extern int nb_notification_tree_send(struct lyd_node *tree);
+
+/*
* Associate a user pointer to a configuration node.
*
* This should be called by northbound 'create' callbacks in the NB_EV_APPLY
diff --git a/lib/yang.c b/lib/yang.c
index 3dd2513a4..2b360376d 100644
--- a/lib/yang.c
+++ b/lib/yang.c
@@ -744,6 +744,34 @@ uint8_t *yang_print_tree(const struct lyd_node *root, LYD_FORMAT format,
return darr;
}
+char *yang_convert_lyd_format(const uint8_t *data, size_t data_len,
+ LYD_FORMAT in_format,
+ LYD_FORMAT out_format, bool shrink)
+{
+ struct lyd_node *tree = NULL;
+ uint8_t *result = NULL;
+ uint32_t options = LYD_PRINT_WD_EXPLICIT | LYD_PRINT_WITHSIBLINGS;
+
+ assert(out_format != LYD_LYB);
+
+ if (!MGMT_MSG_VALIDATE_NUL_TERM(data, data_len))
+ return NULL;
+
+ if (in_format == out_format)
+ return darr_strdup((const char *)data);
+
+ if (shrink)
+ options |= LYD_PRINT_SHRINK;
+
+ /* Take a guess at the initial capacity based on input data size */
+ darr_ensure_cap(result, data_len);
+ if (yang_print_tree_append(&result, tree, out_format, options)) {
+ darr_free(result);
+ return NULL;
+ }
+ return (char *)result;
+}
+
const char *yang_print_errors(struct ly_ctx *ly_ctx, char *buf, size_t buf_len)
{
struct ly_err_item *ei;
diff --git a/lib/yang.h b/lib/yang.h
index 431b2eee4..4ed0a39ba 100644
--- a/lib/yang.h
+++ b/lib/yang.h
@@ -622,6 +622,22 @@ extern void yang_debugging_set(bool enable);
extern uint8_t *yang_print_tree(const struct lyd_node *root, LYD_FORMAT format,
uint32_t options);
+
+/**
+ * yang_convert_lyd_format() - convert one libyang format to darr string.
+ * @data: data to convert.
+ * @data_len: length of the data.
+ * @in_format: format of the data.
+ * @out_format: format to return.
+ * @shrink: true to avoid pretty printing.
+ *
+ * Return:
+ * A darr based string or NULL for error.
+ */
+extern char *yang_convert_lyd_format(const uint8_t *data, size_t msg_len,
+ LYD_FORMAT in_format,
+ LYD_FORMAT out_format, bool shrink);
+
/*
* "Print" the yang tree in `root` into an existing dynamic sized array.
*