summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgor Ryzhov <iryzhov@nfware.com>2024-02-09 23:58:49 +0100
committerIgor Ryzhov <iryzhov@nfware.com>2024-02-10 00:00:24 +0100
commitd94f80fbc4347cbbf5ee6ecbdf3682329db832dc (patch)
treedab7de197df1ae57c726701d24e8eaeb918a2a00
parentMerge pull request #15327 from routingrocks/pim_evpn_traffic_loss (diff)
downloadfrr-d94f80fbc4347cbbf5ee6ecbdf3682329db832dc.tar.xz
frr-d94f80fbc4347cbbf5ee6ecbdf3682329db832dc.zip
lib, mgmtd: fix processing of yang notifications
Current code assumes that notification is always sent in stripped JSON format and therefore notification xpath starts at the third symbol of notification data. Assuming JSON is more or less fine, because this representation is internal to FRR, but the assumption about the xpath is wrong, because it won't work for not top-level notifications. YANG allows to define notification as a child for some data node deep into the tree and in this case notification data contains not only the notification node itself, but also all its parents. To fix the issue, parse the notification data and get its xpath from its schema node. Signed-off-by: Igor Ryzhov <iryzhov@nfware.com>
-rw-r--r--lib/mgmt_be_client.c14
-rw-r--r--lib/yang.c46
-rw-r--r--lib/yang.h13
-rw-r--r--mgmtd/mgmt_be_adapter.c14
-rw-r--r--mgmtd/mgmt_testc.c18
-rw-r--r--tests/topotests/mgmt_notif/test_notif.py2
6 files changed, 98 insertions, 9 deletions
diff --git a/lib/mgmt_be_client.c b/lib/mgmt_be_client.c
index 6530022db..e2a720acb 100644
--- a/lib/mgmt_be_client.c
+++ b/lib/mgmt_be_client.c
@@ -964,13 +964,21 @@ static void be_client_handle_notify(struct mgmt_be_client *client, void *msgbuf,
{
struct mgmt_msg_notify_data *notif_msg = msgbuf;
struct mgmt_be_client_notification_cb *cb;
- const char *notif;
+ char notif[XPATH_MAXLEN];
+ struct lyd_node *dnode;
+ LY_ERR err;
uint i;
debug_be_client("Received notification for client %s", client->name);
- /* "{\"modname:notification-name\": ...}" */
- notif = (const char *)notif_msg->result + 2;
+ err = yang_parse_notification(notif_msg->result_type,
+ (char *)notif_msg->result, &dnode);
+ if (err)
+ return;
+
+ lysc_path(dnode->schema, LYSC_PATH_DATA, notif, sizeof(notif));
+
+ lyd_free_all(dnode);
for (i = 0; i < client->cbs.nnotify_cbs; i++) {
cb = &client->cbs.notify_cbs[i];
diff --git a/lib/yang.c b/lib/yang.c
index adf2ba2ab..ff7df0b37 100644
--- a/lib/yang.c
+++ b/lib/yang.c
@@ -714,6 +714,52 @@ static void ly_log_cb(LY_LOG_LEVEL level, const char *msg, const char *path)
zlog(priority, "libyang: %s", msg);
}
+LY_ERR yang_parse_notification(LYD_FORMAT format, const char *data,
+ struct lyd_node **notif)
+{
+ struct lyd_node *tree, *dnode;
+ struct ly_in *in = NULL;
+ bool found = false;
+ LY_ERR err;
+
+ err = ly_in_new_memory(data, &in);
+ if (err) {
+ zlog_err("Failed to initialize ly_in: %s", ly_last_errmsg());
+ return err;
+ }
+
+ err = lyd_parse_op(ly_native_ctx, NULL, in, format, LYD_TYPE_NOTIF_YANG,
+ &tree, NULL);
+ if (err) {
+ zlog_err("Failed to parse notification: %s", ly_last_errmsg());
+ ly_in_free(in, 0);
+ return err;
+ }
+
+ /*
+ * Notification can be a child of some data node, so traverse the tree
+ * until we find the notification.
+ */
+ LYD_TREE_DFS_BEGIN (tree, dnode) {
+ if (dnode->schema->nodetype == LYS_NOTIF) {
+ found = true;
+ break;
+ }
+ LYD_TREE_DFS_END(tree, dnode);
+ }
+
+ if (!found) {
+ zlog_err("Notification not found in the parsed tree");
+ lyd_free_all(tree);
+ ly_in_free(in, 0);
+ return LY_ENOTFOUND;
+ }
+
+ *notif = dnode;
+
+ return LY_SUCCESS;
+}
+
static ssize_t yang_print_darr(void *arg, const void *buf, size_t count)
{
uint8_t *dst = darr_append_n(*(uint8_t **)arg, count);
diff --git a/lib/yang.h b/lib/yang.h
index 4ed0a39ba..9c221445c 100644
--- a/lib/yang.h
+++ b/lib/yang.h
@@ -607,6 +607,19 @@ extern struct ly_ctx *yang_ctx_new_setup(bool embedded_modules,
*/
extern void yang_debugging_set(bool enable);
+/*
+ * Parse a YANG notification.
+ *
+ * Args:
+ * format: LYD_FORMAT of input data.
+ * data: input data.
+ * notif: pointer to the libyang data tree to store the parsed notification.
+ * If the notification is not on the top level of the yang model,
+ * the pointer to the notification node is still returned, but it's
+ * part of the full data tree with all its parents.
+ */
+extern LY_ERR yang_parse_notification(LYD_FORMAT format, const char *data,
+ struct lyd_node **notif);
/*
* "Print" the yang tree in `root` into dynamic sized array.
diff --git a/mgmtd/mgmt_be_adapter.c b/mgmtd/mgmt_be_adapter.c
index aba02e465..f4353defe 100644
--- a/mgmtd/mgmt_be_adapter.c
+++ b/mgmtd/mgmt_be_adapter.c
@@ -592,14 +592,22 @@ static void mgmt_be_adapter_send_notify(struct mgmt_msg_notify_data *msg,
{
struct mgmt_be_client_adapter *adapter;
struct mgmt_be_xpath_map *map;
- const char *notif;
+ char notif[XPATH_MAXLEN];
+ struct lyd_node *dnode;
+ LY_ERR err;
uint id;
if (!darr_len(be_notif_xpath_map))
return;
- /* "{\"modname:notification-name\": ...}" */
- notif = (const char *)msg->result + 2;
+ err = yang_parse_notification(msg->result_type, (char *)msg->result,
+ &dnode);
+ if (err)
+ return;
+
+ lysc_path(dnode->schema, LYSC_PATH_DATA, notif, sizeof(notif));
+
+ lyd_free_all(dnode);
darr_foreach_p (be_notif_xpath_map, map) {
if (strncmp(map->xpath_prefix, notif, strlen(map->xpath_prefix)))
diff --git a/mgmtd/mgmt_testc.c b/mgmtd/mgmt_testc.c
index 02a308f32..a664c9d7a 100644
--- a/mgmtd/mgmt_testc.c
+++ b/mgmtd/mgmt_testc.c
@@ -79,6 +79,20 @@ struct frr_signal_t __signals[] = {
#define MGMTD_TESTC_VTY_PORT 2624
/* clang-format off */
+static const struct frr_yang_module_info frr_ripd_info = {
+ .name = "frr-ripd",
+ .ignore_cfg_cbs = true,
+ .nodes = {
+ {
+ .xpath = NULL,
+ }
+ }
+};
+
+static const struct frr_yang_module_info *const mgmt_yang_modules[] = {
+ &frr_ripd_info,
+};
+
FRR_DAEMON_INFO(mgmtd_testc, MGMTD_TESTC,
.proghelp = "FRR Management Daemon Test Client.",
@@ -87,8 +101,8 @@ FRR_DAEMON_INFO(mgmtd_testc, MGMTD_TESTC,
.privs = &__privs,
- // .yang_modules = mgmt_yang_modules,
- // .n_yang_modules = array_size(mgmt_yang_modules),
+ .yang_modules = mgmt_yang_modules,
+ .n_yang_modules = array_size(mgmt_yang_modules),
/* avoid libfrr trying to read our config file for us */
.flags = FRR_MANUAL_VTY_START,
diff --git a/tests/topotests/mgmt_notif/test_notif.py b/tests/topotests/mgmt_notif/test_notif.py
index 2f923e398..c85e7ba79 100644
--- a/tests/topotests/mgmt_notif/test_notif.py
+++ b/tests/topotests/mgmt_notif/test_notif.py
@@ -92,7 +92,7 @@ def test_backend_notification(tgen):
pytest.skip("No mgmtd_testc")
output = r1.cmd_raises(
- be_client_path + " --timeout 20 --log file:mgmt_testc.log --listen frr-ripd"
+ be_client_path + " --timeout 20 --log file:mgmt_testc.log --listen /frr-ripd"
)
jsout = json.loads(output)