diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/bitfield.h | 1 | ||||
| -rw-r--r-- | lib/event.c | 5 | ||||
| -rw-r--r-- | lib/hash.h | 6 | ||||
| -rw-r--r-- | lib/libfrr.c | 2 | ||||
| -rw-r--r-- | lib/libfrr.h | 43 | ||||
| -rw-r--r-- | lib/mgmt.proto | 5 | ||||
| -rw-r--r-- | lib/mgmt_be_client.c | 314 | ||||
| -rw-r--r-- | lib/mgmt_be_client.h | 40 | ||||
| -rw-r--r-- | lib/mgmt_fe_client.c | 203 | ||||
| -rw-r--r-- | lib/mgmt_fe_client.h | 20 | ||||
| -rw-r--r-- | lib/mgmt_msg_native.c | 1 | ||||
| -rw-r--r-- | lib/mgmt_msg_native.h | 67 | ||||
| -rw-r--r-- | lib/northbound.c | 17 | ||||
| -rw-r--r-- | lib/northbound.h | 17 | ||||
| -rw-r--r-- | lib/routing_nb.h | 2 | ||||
| -rw-r--r-- | lib/routing_nb_config.c | 5 | ||||
| -rw-r--r-- | lib/vrf.c | 12 | ||||
| -rw-r--r-- | lib/vty.c | 84 | ||||
| -rw-r--r-- | lib/vty.h | 5 | ||||
| -rw-r--r-- | lib/yang.c | 28 | ||||
| -rw-r--r-- | lib/yang.h | 16 | ||||
| -rw-r--r-- | lib/zebra.h | 3 |
22 files changed, 633 insertions, 263 deletions
diff --git a/lib/bitfield.h b/lib/bitfield.h index cc8c311416..3fda627b74 100644 --- a/lib/bitfield.h +++ b/lib/bitfield.h @@ -116,6 +116,7 @@ DECLARE_MTYPE(BITFIELD); (v).m = (v).m + 1; \ (v).data = XREALLOC(MTYPE_BITFIELD, (v).data, \ (v).m * sizeof(word_t)); \ + (v).data[(v).m - 1] = 0; \ } \ } while (0) diff --git a/lib/event.c b/lib/event.c index a7851f6983..6427705e90 100644 --- a/lib/event.c +++ b/lib/event.c @@ -543,6 +543,7 @@ static void initializer(void) pthread_key_create(&thread_current, NULL); } +#define STUPIDLY_LARGE_FD_SIZE 100000 struct event_loop *event_master_create(const char *name) { struct event_loop *rv; @@ -569,6 +570,10 @@ struct event_loop *event_master_create(const char *name) rv->fd_limit = (int)limit.rlim_cur; } + if (rv->fd_limit > STUPIDLY_LARGE_FD_SIZE) + zlog_warn("FD Limit set: %u is stupidly large. Is this what you intended? Consider using --limit-fds", + rv->fd_limit); + rv->read = XCALLOC(MTYPE_EVENT_POLL, sizeof(struct event *) * rv->fd_limit); diff --git a/lib/hash.h b/lib/hash.h index 810faf9030..2d00a334be 100644 --- a/lib/hash.h +++ b/lib/hash.h @@ -28,12 +28,12 @@ struct hash_bucket { */ int len; - /* Linked list. */ - struct hash_bucket *next; - /* Hash key. */ unsigned int key; + /* Linked list. */ + struct hash_bucket *next; + /* Data. */ void *data; }; diff --git a/lib/libfrr.c b/lib/libfrr.c index d904f6f8bb..9e472054dd 100644 --- a/lib/libfrr.c +++ b/lib/libfrr.c @@ -208,7 +208,7 @@ bool frr_zclient_addr(struct sockaddr_storage *sa, socklen_t *sa_len, if (!strncmp(path, ZAPI_TCP_PATHNAME, strlen(ZAPI_TCP_PATHNAME))) { /* note: this functionality is disabled at bottom */ int af; - int port = ZEBRA_PORT; + int port = ZEBRA_TCP_PORT; char *err = NULL; struct sockaddr_in *sin = NULL; struct sockaddr_in6 *sin6 = NULL; diff --git a/lib/libfrr.h b/lib/libfrr.h index 27b877da18..ee436d9f8f 100644 --- a/lib/libfrr.h +++ b/lib/libfrr.h @@ -55,6 +55,49 @@ struct log_arg { }; DECLARE_DLIST(log_args, struct log_arg, itm); +/* Registry of daemons' port defaults. Many of these are VTY ports; some + * daemons have default ports for other features such as ospfapi, or zebra's + * FPM. + */ + +/* default zebra TCP port for zapi; this is currently disabled for security + * reasons. + */ +#define ZEBRA_TCP_PORT 2600 + +#define ZEBRA_VTY_PORT 2601 +#define RIP_VTY_PORT 2602 +#define RIPNG_VTY_PORT 2603 +#define OSPF_VTY_PORT 2604 +#define BGP_VTY_PORT 2605 +#define OSPF6_VTY_PORT 2606 + +/* Default API server port to accept connection request from client-side. + * This value could be overridden by "ospfapi" entry in "/etc/services". + */ +#define OSPF_API_SYNC_PORT 2607 + +#define ISISD_VTY_PORT 2608 +#define BABEL_VTY_PORT 2609 +#define NHRP_VTY_PORT 2610 +#define PIMD_VTY_PORT 2611 +#define LDP_VTY_PORT 2612 +#define EIGRP_VTY_PORT 2613 +#define SHARP_VTY_PORT 2614 +#define PBR_VTY_PORT 2615 +#define STATIC_VTY_PORT 2616 +#define BFDD_VTY_PORT 2617 +#define FABRICD_VTY_PORT 2618 +#define VRRP_VTY_PORT 2619 + +/* default port for FPM connections */ +#define FPM_DEFAULT_PORT 2620 + +#define PATH_VTY_PORT 2621 +#define PIM6D_VTY_PORT 2622 +#define MGMTD_VTY_PORT 2623 +/* Registry of daemons' port defaults */ + enum frr_cli_mode { FRR_CLI_CLASSIC = 0, FRR_CLI_TRANSACTIONAL, diff --git a/lib/mgmt.proto b/lib/mgmt.proto index 5d83fca347..01a99ab63b 100644 --- a/lib/mgmt.proto +++ b/lib/mgmt.proto @@ -76,8 +76,9 @@ message YangGetDataReq { // message BeSubscribeReq { required string client_name = 1; - required bool subscribe_xpaths = 2; - repeated string xpath_reg = 3; + repeated string config_xpaths = 2; + repeated string oper_xpaths = 3; + repeated string notif_xpaths = 4; } message BeSubscribeReply { diff --git a/lib/mgmt_be_client.c b/lib/mgmt_be_client.c index 463aefdf25..6530022db8 100644 --- a/lib/mgmt_be_client.c +++ b/lib/mgmt_be_client.c @@ -149,7 +149,7 @@ mgmt_be_batch_create(struct mgmt_be_txn_ctx *txn) mgmt_be_batches_add_tail(&txn->cfg_batches, batch); - MGMTD_BE_CLIENT_DBG("Added new batch to transaction"); + debug_be_client("Added new batch to transaction"); return batch; } @@ -202,8 +202,8 @@ mgmt_be_find_txn_by_id(struct mgmt_be_client *client_ctx, uint64_t txn_id, if (txn->txn_id == txn_id) return txn; if (warn) - MGMTD_BE_CLIENT_ERR("client %s unkonwn txn-id: %" PRIu64, - client_ctx->name, txn_id); + log_err_be_client("client %s unkonwn txn-id: %" PRIu64, + client_ctx->name, txn_id); return NULL; } @@ -215,8 +215,8 @@ mgmt_be_txn_create(struct mgmt_be_client *client_ctx, uint64_t txn_id) txn = mgmt_be_find_txn_by_id(client_ctx, txn_id, false); if (txn) { - MGMTD_BE_CLIENT_ERR("Can't create existing txn-id: %" PRIu64, - txn_id); + log_err_be_client("Can't create existing txn-id: %" PRIu64, + txn_id); return NULL; } @@ -227,7 +227,7 @@ mgmt_be_txn_create(struct mgmt_be_client *client_ctx, uint64_t txn_id) mgmt_be_batches_init(&txn->apply_cfgs); mgmt_be_txns_add_tail(&client_ctx->txn_head, txn); - MGMTD_BE_CLIENT_DBG("Created new txn-id: %" PRIu64, txn_id); + debug_be_client("Created new txn-id: %" PRIu64, txn_id); return txn; } @@ -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); + + debug_be_client("%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; + + debug_be_client("%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) { @@ -326,7 +410,7 @@ static int mgmt_be_send_txn_reply(struct mgmt_be_client *client_ctx, be_msg.message_case = MGMTD__BE_MESSAGE__MESSAGE_TXN_REPLY; be_msg.txn_reply = &txn_reply; - MGMTD_BE_CLIENT_DBG("Sending TXN_REPLY txn-id %" PRIu64, txn_id); + debug_be_client("Sending TXN_REPLY txn-id %" PRIu64, txn_id); return mgmt_be_client_send_msg(client_ctx, &be_msg); } @@ -337,7 +421,7 @@ static int mgmt_be_process_txn_req(struct mgmt_be_client *client_ctx, struct mgmt_be_txn_ctx *txn; if (create) { - MGMTD_BE_CLIENT_DBG("Creating new txn-id %" PRIu64, txn_id); + debug_be_client("Creating new txn-id %" PRIu64, txn_id); txn = mgmt_be_txn_create(client_ctx, txn_id); if (!txn) @@ -348,7 +432,7 @@ static int mgmt_be_process_txn_req(struct mgmt_be_client *client_ctx, client_ctx->user_data, &txn->client_data, false); } else { - MGMTD_BE_CLIENT_DBG("Deleting txn-id: %" PRIu64, txn_id); + debug_be_client("Deleting txn-id: %" PRIu64, txn_id); txn = mgmt_be_find_txn_by_id(client_ctx, txn_id, false); if (txn) mgmt_be_txn_delete(client_ctx, &txn); @@ -378,8 +462,7 @@ static int mgmt_be_send_cfgdata_create_reply(struct mgmt_be_client *client_ctx, be_msg.message_case = MGMTD__BE_MESSAGE__MESSAGE_CFG_DATA_REPLY; be_msg.cfg_data_reply = &cfgdata_reply; - MGMTD_BE_CLIENT_DBG("Sending CFGDATA_CREATE_REPLY txn-id: %" PRIu64, - txn_id); + debug_be_client("Sending CFGDATA_CREATE_REPLY txn-id: %" PRIu64, txn_id); return mgmt_be_client_send_msg(client_ctx, &be_msg); } @@ -390,9 +473,8 @@ static void mgmt_be_txn_cfg_abort(struct mgmt_be_txn_ctx *txn) assert(txn && txn->client); if (txn->nb_txn) { - MGMTD_BE_CLIENT_ERR( - "Aborting configs after prep for txn-id: %" PRIu64, - txn->txn_id); + log_err_be_client("Aborting configs after prep for txn-id: %" PRIu64, + txn->txn_id); nb_candidate_commit_abort(txn->nb_txn, errmsg, sizeof(errmsg)); txn->nb_txn = 0; } @@ -403,9 +485,8 @@ static void mgmt_be_txn_cfg_abort(struct mgmt_be_txn_ctx *txn) * This is one txn ctx but the candidate_config is per client ctx, how * does that work? */ - MGMTD_BE_CLIENT_DBG( - "Reset candidate configurations after abort of txn-id: %" PRIu64, - txn->txn_id); + debug_be_client("Reset candidate configurations after abort of txn-id: %" PRIu64, + txn->txn_id); nb_config_replace(txn->client->candidate_config, txn->client->running_config, true); } @@ -453,10 +534,9 @@ static int mgmt_be_txn_cfg_prepare(struct mgmt_be_txn_ctx *txn) NULL, true, err_buf, sizeof(err_buf), &error); if (error) { err_buf[sizeof(err_buf) - 1] = 0; - MGMTD_BE_CLIENT_ERR( - "Failed to update configs for txn-id: %" PRIu64 - " to candidate, err: '%s'", - txn->txn_id, err_buf); + log_err_be_client("Failed to update configs for txn-id: %" PRIu64 + " to candidate, err: '%s'", + txn->txn_id, err_buf); return -1; } gettimeofday(&edit_nb_cfg_end, NULL); @@ -494,21 +574,19 @@ static int mgmt_be_txn_cfg_prepare(struct mgmt_be_txn_ctx *txn) if (err != NB_OK) { err_buf[sizeof(err_buf) - 1] = 0; if (err == NB_ERR_VALIDATION) - MGMTD_BE_CLIENT_ERR( - "Failed to validate configs txn-id: %" PRIu64 - " %zu batches, err: '%s'", - txn->txn_id, num_processed, err_buf); + log_err_be_client("Failed to validate configs txn-id: %" PRIu64 + " %zu batches, err: '%s'", + txn->txn_id, num_processed, err_buf); else - MGMTD_BE_CLIENT_ERR( - "Failed to prepare configs for txn-id: %" PRIu64 - " %zu batches, err: '%s'", - txn->txn_id, num_processed, err_buf); + log_err_be_client("Failed to prepare configs for txn-id: %" PRIu64 + " %zu batches, err: '%s'", + txn->txn_id, num_processed, err_buf); error = true; SET_FLAG(txn->flags, MGMTD_BE_TXN_FLAGS_CFGPREP_FAILED); } else - MGMTD_BE_CLIENT_DBG("Prepared configs for txn-id: %" PRIu64 - " %zu batches", - txn->txn_id, num_processed); + debug_be_client("Prepared configs for txn-id: %" PRIu64 + " %zu batches", + txn->txn_id, num_processed); gettimeofday(&prep_nb_cfg_end, NULL); prep_nb_cfg_tm = timeval_elapsed(prep_nb_cfg_end, prep_nb_cfg_start); @@ -530,10 +608,9 @@ static int mgmt_be_txn_cfg_prepare(struct mgmt_be_txn_ctx *txn) mgmt_be_send_cfgdata_create_reply(client_ctx, txn->txn_id, error ? false : true, error ? err_buf : NULL); - MGMTD_BE_CLIENT_DBG( - "Avg-nb-edit-duration %lu uSec, nb-prep-duration %lu (avg: %lu) uSec, batch size %u", - client_ctx->avg_edit_nb_cfg_tm, prep_nb_cfg_tm, - client_ctx->avg_prep_nb_cfg_tm, (uint32_t)num_processed); + debug_be_client("Avg-nb-edit-duration %lu uSec, nb-prep-duration %lu (avg: %lu) uSec, batch size %u", + client_ctx->avg_edit_nb_cfg_tm, prep_nb_cfg_tm, + client_ctx->avg_prep_nb_cfg_tm, (uint32_t)num_processed); if (error) mgmt_be_txn_cfg_abort(txn); @@ -559,8 +636,9 @@ static int mgmt_be_update_setcfg_in_batch(struct mgmt_be_client *client_ctx, txn_req = &batch->txn_req; txn_req->event = MGMTD_BE_TXN_PROC_SETCFG; - MGMTD_BE_CLIENT_DBG("Created SETCFG request for txn-id: %" PRIu64 - " cfg-items:%d", txn->txn_id, num_req); + debug_be_client("Created SETCFG request for txn-id: %" PRIu64 + " cfg-items:%d", + txn->txn_id, num_req); txn_req->req.set_cfg.num_cfg_changes = num_req; for (index = 0; index < num_req; index++) { @@ -622,7 +700,7 @@ static int mgmt_be_process_cfgdata_req(struct mgmt_be_client *client_ctx, mgmt_be_update_setcfg_in_batch(client_ctx, txn, cfg_req, num_req); if (txn && end_of_data) { - MGMTD_BE_CLIENT_DBG("End of data; CFG_PREPARE_REQ processing"); + debug_be_client("End of data; CFG_PREPARE_REQ processing"); if (mgmt_be_txn_cfg_prepare(txn)) goto failed; } @@ -651,7 +729,7 @@ static int mgmt_be_send_apply_reply(struct mgmt_be_client *client_ctx, be_msg.message_case = MGMTD__BE_MESSAGE__MESSAGE_CFG_APPLY_REPLY; be_msg.cfg_apply_reply = &apply_reply; - MGMTD_BE_CLIENT_DBG("Sending CFG_APPLY_REPLY txn-id %" PRIu64, txn_id); + debug_be_client("Sending CFG_APPLY_REPLY txn-id %" PRIu64, txn_id); return mgmt_be_client_send_msg(client_ctx, &be_msg); } @@ -698,8 +776,8 @@ static int mgmt_be_txn_proc_cfgapply(struct mgmt_be_txn_ctx *txn) mgmt_be_send_apply_reply(client_ctx, txn->txn_id, true, NULL); - MGMTD_BE_CLIENT_DBG("Nb-apply-duration %lu (avg: %lu) uSec", - apply_nb_cfg_tm, client_ctx->avg_apply_nb_cfg_tm); + debug_be_client("Nb-apply-duration %lu (avg: %lu) uSec", + apply_nb_cfg_tm, client_ctx->avg_apply_nb_cfg_tm); return 0; } @@ -713,7 +791,7 @@ static int mgmt_be_process_cfg_apply(struct mgmt_be_client *client_ctx, if (!txn) goto failed; - MGMTD_BE_CLIENT_DBG("Trigger CFG_APPLY_REQ processing"); + debug_be_client("Trigger CFG_APPLY_REQ processing"); if (mgmt_be_txn_proc_cfgapply(txn)) goto failed; @@ -736,23 +814,28 @@ static int mgmt_be_client_handle_msg(struct mgmt_be_client *client_ctx, */ switch ((int)be_msg->message_case) { case MGMTD__BE_MESSAGE__MESSAGE_SUBSCR_REPLY: - MGMTD_BE_CLIENT_DBG("Got SUBSCR_REPLY success %u", - be_msg->subscr_reply->success); + debug_be_client("Got SUBSCR_REPLY success %u", + be_msg->subscr_reply->success); + + if (client_ctx->cbs.subscr_done) + (*client_ctx->cbs.subscr_done)(client_ctx, + client_ctx->user_data, + be_msg->subscr_reply + ->success); break; case MGMTD__BE_MESSAGE__MESSAGE_TXN_REQ: - MGMTD_BE_CLIENT_DBG("Got TXN_REQ %s txn-id: %" PRIu64, - be_msg->txn_req->create ? "Create" - : "Delete", - be_msg->txn_req->txn_id); + debug_be_client("Got TXN_REQ %s txn-id: %" PRIu64, + be_msg->txn_req->create ? "Create" : "Delete", + be_msg->txn_req->txn_id); mgmt_be_process_txn_req(client_ctx, be_msg->txn_req->txn_id, be_msg->txn_req->create); break; case MGMTD__BE_MESSAGE__MESSAGE_CFG_DATA_REQ: - MGMTD_BE_CLIENT_DBG("Got CFG_DATA_REQ txn-id: %" PRIu64 - " end-of-data %u", - be_msg->cfg_data_req->txn_id, - be_msg->cfg_data_req->end_of_data); + debug_be_client("Got CFG_DATA_REQ txn-id: %" PRIu64 + " end-of-data %u", + be_msg->cfg_data_req->txn_id, + be_msg->cfg_data_req->end_of_data); mgmt_be_process_cfgdata_req( client_ctx, be_msg->cfg_data_req->txn_id, be_msg->cfg_data_req->data_req, @@ -760,8 +843,8 @@ static int mgmt_be_client_handle_msg(struct mgmt_be_client *client_ctx, be_msg->cfg_data_req->end_of_data); break; case MGMTD__BE_MESSAGE__MESSAGE_CFG_APPLY_REQ: - MGMTD_BE_CLIENT_DBG("Got CFG_APPLY_REQ txn-id: %" PRIu64, - be_msg->cfg_data_req->txn_id); + debug_be_client("Got CFG_APPLY_REQ txn-id: %" PRIu64, + be_msg->cfg_data_req->txn_id); mgmt_be_process_cfg_apply( client_ctx, (uint64_t)be_msg->cfg_apply_req->txn_id); break; @@ -824,7 +907,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; @@ -856,9 +939,9 @@ static void be_client_handle_get_tree(struct mgmt_be_client *client, struct mgmt_msg_get_tree *get_tree_msg = msgbuf; struct be_client_tree_data_batch_args *args; - MGMTD_BE_CLIENT_DBG("Received get-tree request for client %s txn-id %" PRIu64 - " req-id %" PRIu64, - client->name, txn_id, get_tree_msg->req_id); + debug_be_client("Received get-tree request for client %s txn-id %" PRIu64 + " req-id %" PRIu64, + client->name, txn_id, get_tree_msg->req_id); /* NOTE: removed the translator, if put back merge with northbound_cli * code @@ -874,6 +957,31 @@ static void be_client_handle_get_tree(struct mgmt_be_client *client, } /* + * Process the notification. + */ +static void be_client_handle_notify(struct mgmt_be_client *client, void *msgbuf, + size_t msg_len) +{ + struct mgmt_msg_notify_data *notif_msg = msgbuf; + struct mgmt_be_client_notification_cb *cb; + const char *notif; + uint i; + + debug_be_client("Received notification for client %s", client->name); + + /* "{\"modname:notification-name\": ...}" */ + notif = (const char *)notif_msg->result + 2; + + for (i = 0; i < client->cbs.nnotify_cbs; i++) { + cb = &client->cbs.notify_cbs[i]; + if (strncmp(cb->xpath, notif, strlen(cb->xpath))) + continue; + cb->callback(client, client->user_data, cb, + (const char *)notif_msg->result); + } +} + +/* * Handle a native encoded message * * We don't create transactions with native messaging. @@ -888,12 +996,15 @@ static void be_client_handle_native_msg(struct mgmt_be_client *client, case MGMT_MSG_CODE_GET_TREE: be_client_handle_get_tree(client, txn_id, msg, msg_len); break; + case MGMT_MSG_CODE_NOTIFY: + be_client_handle_notify(client, msg, msg_len); + break; default: - MGMTD_BE_CLIENT_ERR("unknown native message txn-id %" PRIu64 - " req-id %" PRIu64 " code %u to client %s", - txn_id, msg->req_id, msg->code, - client->name); - be_client_send_error(client, msg->refer_id, msg->req_id, false, -1, + log_err_be_client("unknown native message txn-id %" PRIu64 + " req-id %" PRIu64 " code %u to client %s", + txn_id, msg->req_id, msg->code, client->name); + 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; @@ -916,49 +1027,61 @@ static void mgmt_be_client_process_msg(uint8_t version, uint8_t *data, if (len >= sizeof(*msg)) be_client_handle_native_msg(client_ctx, msg, len); else - MGMTD_BE_CLIENT_ERR("native message to client %s too short %zu", - client_ctx->name, len); + log_err_be_client("native message to client %s too short %zu", + client_ctx->name, len); return; } be_msg = mgmtd__be_message__unpack(NULL, len, data); if (!be_msg) { - MGMTD_BE_CLIENT_DBG("Failed to decode %zu bytes from server", - len); + debug_be_client("Failed to decode %zu bytes from server", len); return; } - MGMTD_BE_CLIENT_DBG( - "Decoded %zu bytes of message(msg: %u/%u) from server", len, - be_msg->message_case, be_msg->message_case); + debug_be_client("Decoded %zu bytes of message(msg: %u/%u) from server", + len, be_msg->message_case, be_msg->message_case); (void)mgmt_be_client_handle_msg(client_ctx, be_msg); mgmtd__be_message__free_unpacked(be_msg, NULL); } int mgmt_be_send_subscr_req(struct mgmt_be_client *client_ctx, - bool subscr_xpaths, int num_xpaths, - char **reg_xpaths) + int n_config_xpaths, char **config_xpaths, + int n_oper_xpaths, char **oper_xpaths) { Mgmtd__BeMessage be_msg; Mgmtd__BeSubscribeReq subscr_req; + const char **notif_xpaths = NULL; + int ret; mgmtd__be_subscribe_req__init(&subscr_req); subscr_req.client_name = client_ctx->name; - subscr_req.n_xpath_reg = num_xpaths; - if (num_xpaths) - subscr_req.xpath_reg = reg_xpaths; - else - subscr_req.xpath_reg = NULL; - subscr_req.subscribe_xpaths = subscr_xpaths; + subscr_req.n_config_xpaths = n_config_xpaths; + subscr_req.config_xpaths = config_xpaths; + subscr_req.n_oper_xpaths = n_oper_xpaths; + subscr_req.oper_xpaths = oper_xpaths; + + /* See if we should register for notifications */ + subscr_req.n_notif_xpaths = client_ctx->cbs.nnotify_cbs; + if (client_ctx->cbs.nnotify_cbs) { + struct mgmt_be_client_notification_cb *cb, *ecb; + + cb = client_ctx->cbs.notify_cbs; + ecb = cb + client_ctx->cbs.nnotify_cbs; + for (; cb < ecb; cb++) + *darr_append(notif_xpaths) = cb->xpath; + } + subscr_req.notif_xpaths = (char **)notif_xpaths; mgmtd__be_message__init(&be_msg); be_msg.message_case = MGMTD__BE_MESSAGE__MESSAGE_SUBSCR_REQ; be_msg.subscr_req = &subscr_req; - MGMTD_BE_CLIENT_DBG("Sending SUBSCR_REQ name: %s subscr_xpaths: %u num_xpaths: %zu", - subscr_req.client_name, subscr_req.subscribe_xpaths, - subscr_req.n_xpath_reg); + debug_be_client("Sending SUBSCR_REQ name: %s xpaths: config %zu oper: %zu notif: %zu", + subscr_req.client_name, subscr_req.n_config_xpaths, + subscr_req.n_oper_xpaths, subscr_req.n_notif_xpaths); - return mgmt_be_client_send_msg(client_ctx, &be_msg); + ret = mgmt_be_client_send_msg(client_ctx, &be_msg); + darr_free(notif_xpaths); + return ret; } static int _notify_conenct_disconnect(struct msg_client *msg_client, @@ -970,15 +1093,16 @@ static int _notify_conenct_disconnect(struct msg_client *msg_client, if (connected) { assert(msg_client->conn.fd != -1); - ret = mgmt_be_send_subscr_req(client, false, 0, NULL); + ret = mgmt_be_send_subscr_req(client, 0, NULL, 0, NULL); if (ret) return ret; } /* Notify BE client through registered callback (if any) */ if (client->cbs.client_connect_notify) - (void)(*client->cbs.client_connect_notify)( - client, client->user_data, connected); + (void)(*client->cbs.client_connect_notify)(client, + client->user_data, + connected); /* Cleanup any in-progress TXN on disconnect */ if (!connected) @@ -1016,9 +1140,8 @@ static void mgmt_debug_client_be_set(uint32_t flags, bool set) DEFPY(debug_mgmt_client_be, debug_mgmt_client_be_cmd, "[no] debug mgmt client backend", - NO_STR DEBUG_STR MGMTD_STR - "client\n" - "backend\n") + NO_STR DEBUG_STR MGMTD_STR "client\n" + "backend\n") { mgmt_debug_client_be_set(DEBUG_NODE2MODE(vty->node), !no); @@ -1035,7 +1158,7 @@ static int mgmt_debug_be_client_config_write(struct vty *vty) void mgmt_debug_be_client_show_debug(struct vty *vty) { - if (MGMTD_DBG_BE_CLIENT_CHECK()) + if (debug_check_be_client()) vty_out(vty, "debug mgmt client backend\n"); } @@ -1081,9 +1204,13 @@ struct mgmt_be_client *mgmt_be_client_create(const char *client_name, mgmt_be_client_notify_disconenct, mgmt_be_client_process_msg, MGMTD_BE_MAX_NUM_MSG_PROC, MGMTD_BE_MAX_NUM_MSG_WRITE, MGMTD_BE_MAX_MSG_LEN, false, - "BE-client", MGMTD_DBG_BE_CLIENT_CHECK()); + "BE-client", debug_check_be_client()); + + /* Hook to receive notifications */ + hook_register_arg(nb_notification_send, mgmt_be_notification_send, + client); - MGMTD_BE_CLIENT_DBG("Initialized client '%s'", client_name); + debug_be_client("Initialized client '%s'", client_name); return client; } @@ -1101,8 +1228,7 @@ void mgmt_be_client_destroy(struct mgmt_be_client *client) { assert(client == __be_client); - MGMTD_BE_CLIENT_DBG("Destroying MGMTD Backend Client '%s'", - client->name); + debug_be_client("Destroying MGMTD Backend Client '%s'", client->name); nb_oper_cancel_all_walks(); msg_client_cleanup(&client->client); diff --git a/lib/mgmt_be_client.h b/lib/mgmt_be_client.h index 8ad482cacf..d144ebc728 100644 --- a/lib/mgmt_be_client.h +++ b/lib/mgmt_be_client.h @@ -60,14 +60,29 @@ struct mgmt_be_client_txn_ctx { * Callbacks: * client_connect_notify: called when connection is made/lost to mgmtd. * txn_notify: called when a txn has been created + * notify_cbs: callbacks for notifications. + * nnotify_cbs: number of notification callbacks. + * */ struct mgmt_be_client_cbs { void (*client_connect_notify)(struct mgmt_be_client *client, uintptr_t usr_data, bool connected); - + void (*subscr_done)(struct mgmt_be_client *client, uintptr_t usr_data, + bool success); void (*txn_notify)(struct mgmt_be_client *client, uintptr_t usr_data, struct mgmt_be_client_txn_ctx *txn_ctx, bool destroyed); + + struct mgmt_be_client_notification_cb *notify_cbs; + uint nnotify_cbs; +}; + +struct mgmt_be_client_notification_cb { + const char *xpath; /* the notification */ + uint8_t format; /* currently only LYD_JSON supported */ + void (*callback)(struct mgmt_be_client *client, uintptr_t usr_data, + struct mgmt_be_client_notification_cb *this, + const char *notif_data); }; /*************************************************************** @@ -80,12 +95,12 @@ extern struct debug mgmt_dbg_be_client; * API prototypes ***************************************************************/ -#define MGMTD_BE_CLIENT_DBG(fmt, ...) \ +#define debug_be_client(fmt, ...) \ DEBUGD(&mgmt_dbg_be_client, "BE-CLIENT: %s: " fmt, __func__, \ ##__VA_ARGS__) -#define MGMTD_BE_CLIENT_ERR(fmt, ...) \ +#define log_err_be_client(fmt, ...) \ zlog_err("BE-CLIENT: %s: ERROR: " fmt, __func__, ##__VA_ARGS__) -#define MGMTD_DBG_BE_CLIENT_CHECK() \ +#define debug_check_be_client() \ DEBUG_MODE_CHECK(&mgmt_dbg_be_client, DEBUG_MODE_ALL) /** @@ -124,7 +139,7 @@ extern void mgmt_debug_be_client_show_debug(struct vty *vty); * The client object. * * reg_yang_xpaths - * Yang xpath(s) that needs to be [un]-subscribed from/to + * Yang xpath(s) that needs to be subscribed to * * num_xpaths * Number of xpaths @@ -132,9 +147,18 @@ extern void mgmt_debug_be_client_show_debug(struct vty *vty); * Returns: * MGMTD_SUCCESS on success, MGMTD_* otherwise. */ -extern int mgmt_be_send_subscr_req(struct mgmt_be_client *client, - bool subscr_xpaths, int num_xpaths, - char **reg_xpaths); +extern int mgmt_be_send_subscr_req(struct mgmt_be_client *client_ctx, + int n_config_xpaths, char **config_xpaths, + int n_oper_xpaths, char **oper_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 92619f4f7f..9f98a241fe 100644 --- a/lib/mgmt_fe_client.c +++ b/lib/mgmt_fe_client.c @@ -79,14 +79,13 @@ mgmt_fe_find_session_by_client_id(struct mgmt_fe_client *client, FOREACH_SESSION_IN_LIST (client, session) { if (session->client_id == client_id) { - MGMTD_FE_CLIENT_DBG("Found session-id %" PRIu64 - " using client-id %" PRIu64, - session->session_id, client_id); + debug_fe_client("Found session-id %" PRIu64 + " using client-id %" PRIu64, + session->session_id, client_id); return session; } } - MGMTD_FE_CLIENT_DBG("Session not found using client-id %" PRIu64, - client_id); + debug_fe_client("Session not found using client-id %" PRIu64, client_id); return NULL; } @@ -98,15 +97,14 @@ mgmt_fe_find_session_by_session_id(struct mgmt_fe_client *client, FOREACH_SESSION_IN_LIST (client, session) { if (session->session_id == session_id) { - MGMTD_FE_CLIENT_DBG( - "Found session of client-id %" PRIu64 - " using session-id %" PRIu64, - session->client_id, session_id); + debug_fe_client("Found session of client-id %" PRIu64 + " using session-id %" PRIu64, + session->client_id, session_id); return session; } } - MGMTD_FE_CLIENT_DBG("Session not found using session-id %" PRIu64, - session_id); + debug_fe_client("Session not found using session-id %" PRIu64, + session_id); return NULL; } @@ -133,8 +131,7 @@ static int mgmt_fe_send_register_req(struct mgmt_fe_client *client) fe_msg.message_case = MGMTD__FE_MESSAGE__MESSAGE_REGISTER_REQ; fe_msg.register_req = &rgstr_req; - MGMTD_FE_CLIENT_DBG( - "Sending REGISTER_REQ message to MGMTD Frontend server"); + debug_fe_client("Sending REGISTER_REQ message to MGMTD Frontend server"); return mgmt_fe_client_send_msg(client, &fe_msg, true); } @@ -160,9 +157,8 @@ static int mgmt_fe_send_session_req(struct mgmt_fe_client *client, fe_msg.message_case = MGMTD__FE_MESSAGE__MESSAGE_SESSION_REQ; fe_msg.session_req = &sess_req; - MGMTD_FE_CLIENT_DBG( - "Sending SESSION_REQ %s message for client-id %" PRIu64, - create ? "create" : "destroy", session->client_id); + debug_fe_client("Sending SESSION_REQ %s message for client-id %" PRIu64, + create ? "create" : "destroy", session->client_id); return mgmt_fe_client_send_msg(client, &fe_msg, true); } @@ -185,9 +181,8 @@ int mgmt_fe_send_lockds_req(struct mgmt_fe_client *client, uint64_t session_id, fe_msg.message_case = MGMTD__FE_MESSAGE__MESSAGE_LOCKDS_REQ; fe_msg.lockds_req = &lockds_req; - MGMTD_FE_CLIENT_DBG( - "Sending LOCKDS_REQ (%sLOCK) message for DS:%s session-id %" PRIu64, - lock ? "" : "UN", dsid2name(ds_id), session_id); + debug_fe_client("Sending LOCKDS_REQ (%sLOCK) message for DS:%s session-id %" PRIu64, + lock ? "" : "UN", dsid2name(ds_id), session_id); return mgmt_fe_client_send_msg(client, &fe_msg, scok); @@ -215,10 +210,9 @@ int mgmt_fe_send_setcfg_req(struct mgmt_fe_client *client, uint64_t session_id, fe_msg.message_case = MGMTD__FE_MESSAGE__MESSAGE_SETCFG_REQ; fe_msg.setcfg_req = &setcfg_req; - MGMTD_FE_CLIENT_DBG( - "Sending SET_CONFIG_REQ message for DS:%s session-id %" PRIu64 - " (#xpaths:%d)", - dsid2name(ds_id), session_id, num_data_reqs); + debug_fe_client("Sending SET_CONFIG_REQ message for DS:%s session-id %" PRIu64 + " (#xpaths:%d)", + dsid2name(ds_id), session_id, num_data_reqs); return mgmt_fe_client_send_msg(client, &fe_msg, false); } @@ -245,9 +239,8 @@ int mgmt_fe_send_commitcfg_req(struct mgmt_fe_client *client, fe_msg.message_case = MGMTD__FE_MESSAGE__MESSAGE_COMMCFG_REQ; fe_msg.commcfg_req = &commitcfg_req; - MGMTD_FE_CLIENT_DBG( - "Sending COMMIT_CONFIG_REQ message for Src-DS:%s, Dst-DS:%s session-id %" PRIu64, - dsid2name(src_ds_id), dsid2name(dest_ds_id), session_id); + debug_fe_client("Sending COMMIT_CONFIG_REQ message for Src-DS:%s, Dst-DS:%s session-id %" PRIu64, + dsid2name(src_ds_id), dsid2name(dest_ds_id), session_id); return mgmt_fe_client_send_msg(client, &fe_msg, false); } @@ -273,10 +266,9 @@ int mgmt_fe_send_get_req(struct mgmt_fe_client *client, uint64_t session_id, fe_msg.message_case = MGMTD__FE_MESSAGE__MESSAGE_GET_REQ; fe_msg.get_req = &getcfg_req; - MGMTD_FE_CLIENT_DBG("Sending GET_REQ (iscfg %d) message for DS:%s session-id %" PRIu64 - " (#xpaths:%d)", - is_config, dsid2name(ds_id), session_id, - num_data_reqs); + debug_fe_client("Sending GET_REQ (iscfg %d) message for DS:%s session-id %" PRIu64 + " (#xpaths:%d)", + is_config, dsid2name(ds_id), session_id, num_data_reqs); return mgmt_fe_client_send_msg(client, &fe_msg, false); } @@ -308,9 +300,10 @@ int mgmt_fe_send_regnotify_req(struct mgmt_fe_client *client, /* * Send get-data request. */ -int mgmt_fe_send_get_data_req(struct mgmt_fe_client *client, uint64_t session_id, - uint64_t req_id, LYD_FORMAT result_type, - uint8_t flags, const char *xpath) +int mgmt_fe_send_get_data_req(struct mgmt_fe_client *client, + uint64_t session_id, uint64_t req_id, + uint8_t datastore, LYD_FORMAT result_type, + uint8_t flags, uint8_t defaults, const char *xpath) { struct mgmt_msg_get_data *msg; size_t xplen = strlen(xpath); @@ -323,11 +316,13 @@ int mgmt_fe_send_get_data_req(struct mgmt_fe_client *client, uint64_t session_id msg->code = MGMT_MSG_CODE_GET_DATA; msg->result_type = result_type; msg->flags = flags; + msg->defaults = defaults; + msg->datastore = datastore; strlcpy(msg->xpath, xpath, xplen + 1); - MGMTD_FE_CLIENT_DBG("Sending GET_DATA_REQ session-id %" PRIu64 - " req-id %" PRIu64 " xpath: %s", - session_id, req_id, xpath); + debug_fe_client("Sending GET_DATA_REQ session-id %" PRIu64 + " req-id %" PRIu64 " xpath: %s", + session_id, req_id, xpath); ret = mgmt_msg_native_send_msg(&client->client.conn, msg, false); mgmt_msg_native_free_msg(msg); @@ -348,30 +343,28 @@ static int mgmt_fe_client_handle_msg(struct mgmt_fe_client *client, case MGMTD__FE_MESSAGE__MESSAGE_SESSION_REPLY: if (fe_msg->session_reply->create && fe_msg->session_reply->has_client_conn_id) { - MGMTD_FE_CLIENT_DBG( - "Got SESSION_REPLY (create) for client-id %" PRIu64 - " with session-id: %" PRIu64, - fe_msg->session_reply->client_conn_id, - fe_msg->session_reply->session_id); + debug_fe_client("Got SESSION_REPLY (create) for client-id %" PRIu64 + " with session-id: %" PRIu64, + fe_msg->session_reply->client_conn_id, + fe_msg->session_reply->session_id); session = mgmt_fe_find_session_by_client_id( client, fe_msg->session_reply->client_conn_id); if (session && fe_msg->session_reply->success) { - MGMTD_FE_CLIENT_DBG( - "Session Created for client-id %" PRIu64, - fe_msg->session_reply->client_conn_id); + debug_fe_client("Session Created for client-id %" PRIu64, + fe_msg->session_reply + ->client_conn_id); session->session_id = fe_msg->session_reply->session_id; } else { - MGMTD_FE_CLIENT_ERR( + log_err_fe_client( "Session Create failed for client-id %" PRIu64, fe_msg->session_reply->client_conn_id); } } else if (!fe_msg->session_reply->create) { - MGMTD_FE_CLIENT_DBG( - "Got SESSION_REPLY (destroy) for session-id %" PRIu64, - fe_msg->session_reply->session_id); + debug_fe_client("Got SESSION_REPLY (destroy) for session-id %" PRIu64, + fe_msg->session_reply->session_id); session = mgmt_fe_find_session_by_session_id( client, fe_msg->session_req->session_id); @@ -388,8 +381,8 @@ static int mgmt_fe_client_handle_msg(struct mgmt_fe_client *client, session->user_ctx); break; case MGMTD__FE_MESSAGE__MESSAGE_LOCKDS_REPLY: - MGMTD_FE_CLIENT_DBG("Got LOCKDS_REPLY for session-id %" PRIu64, - fe_msg->lockds_reply->session_id); + debug_fe_client("Got LOCKDS_REPLY for session-id %" PRIu64, + fe_msg->lockds_reply->session_id); session = mgmt_fe_find_session_by_session_id( client, fe_msg->lockds_reply->session_id); @@ -405,8 +398,8 @@ static int mgmt_fe_client_handle_msg(struct mgmt_fe_client *client, fe_msg->lockds_reply->error_if_any); break; case MGMTD__FE_MESSAGE__MESSAGE_SETCFG_REPLY: - MGMTD_FE_CLIENT_DBG("Got SETCFG_REPLY for session-id %" PRIu64, - fe_msg->setcfg_reply->session_id); + debug_fe_client("Got SETCFG_REPLY for session-id %" PRIu64, + fe_msg->setcfg_reply->session_id); session = mgmt_fe_find_session_by_session_id( client, fe_msg->setcfg_reply->session_id); @@ -423,8 +416,8 @@ static int mgmt_fe_client_handle_msg(struct mgmt_fe_client *client, fe_msg->setcfg_reply->error_if_any); break; case MGMTD__FE_MESSAGE__MESSAGE_COMMCFG_REPLY: - MGMTD_FE_CLIENT_DBG("Got COMMCFG_REPLY for session-id %" PRIu64, - fe_msg->commcfg_reply->session_id); + debug_fe_client("Got COMMCFG_REPLY for session-id %" PRIu64, + fe_msg->commcfg_reply->session_id); session = mgmt_fe_find_session_by_session_id( client, fe_msg->commcfg_reply->session_id); @@ -443,8 +436,8 @@ static int mgmt_fe_client_handle_msg(struct mgmt_fe_client *client, fe_msg->commcfg_reply->error_if_any); break; case MGMTD__FE_MESSAGE__MESSAGE_GET_REPLY: - MGMTD_FE_CLIENT_DBG("Got GET_REPLY for session-id %" PRIu64, - fe_msg->get_reply->session_id); + debug_fe_client("Got GET_REPLY for session-id %" PRIu64, + fe_msg->get_reply->session_id); session = mgmt_fe_find_session_by_session_id(client, @@ -507,19 +500,23 @@ 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, - msg->refer_id); + debug_fe_client("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) { + log_err_fe_client("No session for received native msg session-id %" PRIu64, + msg->refer_id); + return; + } } switch (msg->code) { @@ -529,7 +526,7 @@ static void fe_client_handle_native_msg(struct mgmt_fe_client *client, err_msg = (typeof(err_msg))msg; if (!MGMT_MSG_VALIDATE_NUL_TERM(err_msg, msg_len)) { - MGMTD_FE_CLIENT_ERR("Corrupt error msg recv"); + log_err_fe_client("Corrupt error msg recv"); return; } session->client->cbs.error_notify(client, client->user_data, @@ -545,7 +542,7 @@ static void fe_client_handle_native_msg(struct mgmt_fe_client *client, tree_msg = (typeof(tree_msg))msg; if (msg_len < sizeof(*tree_msg)) { - MGMTD_FE_CLIENT_ERR("Corrupt tree-data msg recv"); + log_err_fe_client("Corrupt tree-data msg recv"); return; } session->client->cbs.get_tree_notify(client, client->user_data, @@ -559,10 +556,48 @@ 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)) { + log_err_fe_client("Corrupt notify-data msg recv"); + return; + } + + if (notify_msg->result_type != LYD_LYB && + !MGMT_MSG_VALIDATE_NUL_TERM(notify_msg, msg_len)) { + log_err_fe_client("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) { + log_err_fe_client("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", - msg->refer_id, msg->req_id, msg->code); + log_err_fe_client("unknown native message session-id %" PRIu64 + " req-id %" PRIu64 " code %u", + msg->refer_id, msg->req_id, msg->code); break; } } @@ -583,20 +618,18 @@ static void mgmt_fe_client_process_msg(uint8_t version, uint8_t *data, if (len >= sizeof(*msg)) fe_client_handle_native_msg(client, msg, len); else - MGMTD_FE_CLIENT_ERR("native message to FE client %s too short %zu", - client->name, len); + log_err_fe_client("native message to FE client %s too short %zu", + client->name, len); return; } fe_msg = mgmtd__fe_message__unpack(NULL, len, data); if (!fe_msg) { - MGMTD_FE_CLIENT_DBG("Failed to decode %zu bytes from server.", - len); + debug_fe_client("Failed to decode %zu bytes from server.", len); return; } - MGMTD_FE_CLIENT_DBG( - "Decoded %zu bytes of message(msg: %u/%u) from server", len, - fe_msg->message_case, fe_msg->message_case); + debug_fe_client("Decoded %zu bytes of message(msg: %u/%u) from server", + len, fe_msg->message_case, fe_msg->message_case); (void)mgmt_fe_client_handle_msg(client, fe_msg); mgmtd__fe_message__free_unpacked(fe_msg, NULL); } @@ -617,7 +650,7 @@ static int _notify_connect_disconnect(struct msg_client *msg_client, /* Walk list of sessions for this FE client deleting them */ if (!connected && mgmt_sessions_count(&client->sessions)) { - MGMTD_FE_CLIENT_DBG("Cleaning up existing sessions"); + debug_fe_client("Cleaning up existing sessions"); FOREACH_SESSION_IN_LIST (client, session) { assert(session->client); @@ -688,7 +721,7 @@ static int mgmt_debug_fe_client_config_write(struct vty *vty) void mgmt_debug_fe_client_show_debug(struct vty *vty) { - if (MGMTD_DBG_FE_CLIENT_CHECK()) + if (debug_check_fe_client()) vty_out(vty, "debug mgmt client frontend\n"); } @@ -734,9 +767,9 @@ struct mgmt_fe_client *mgmt_fe_client_create(const char *client_name, mgmt_fe_client_notify_disconnect, mgmt_fe_client_process_msg, MGMTD_FE_MAX_NUM_MSG_PROC, MGMTD_FE_MAX_NUM_MSG_WRITE, MGMTD_FE_MAX_MSG_LEN, true, - "FE-client", MGMTD_DBG_FE_CLIENT_CHECK()); + "FE-client", debug_check_fe_client()); - MGMTD_FE_CLIENT_DBG("Initialized client '%s'", client_name); + debug_fe_client("Initialized client '%s'", client_name); return client; } @@ -805,9 +838,8 @@ enum mgmt_result mgmt_fe_destroy_client_session(struct mgmt_fe_client *client, if (session->session_id && mgmt_fe_send_session_req(client, session, false) != 0) - MGMTD_FE_CLIENT_ERR( - "Failed to send session destroy request for the session-id %" PRIu64, - session->session_id); + log_err_fe_client("Failed to send session destroy request for the session-id %" PRIu64, + session->session_id); mgmt_sessions_del(&client->sessions, session); XFREE(MTYPE_MGMTD_FE_SESSION, session); @@ -824,8 +856,7 @@ void mgmt_fe_client_destroy(struct mgmt_fe_client *client) assert(client == __fe_client); - MGMTD_FE_CLIENT_DBG("Destroying MGMTD Frontend Client '%s'", - client->name); + debug_fe_client("Destroying MGMTD Frontend Client '%s'", client->name); FOREACH_SESSION_IN_LIST (client, session) mgmt_fe_destroy_client_session(client, session->client_id); diff --git a/lib/mgmt_fe_client.h b/lib/mgmt_fe_client.h index 3abe29b1cf..7af1270071 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, @@ -127,12 +132,12 @@ extern struct debug mgmt_dbg_fe_client; * API prototypes ***************************************************************/ -#define MGMTD_FE_CLIENT_DBG(fmt, ...) \ +#define debug_fe_client(fmt, ...) \ DEBUGD(&mgmt_dbg_fe_client, "FE-CLIENT: %s: " fmt, __func__, \ ##__VA_ARGS__) -#define MGMTD_FE_CLIENT_ERR(fmt, ...) \ +#define log_err_fe_client(fmt, ...) \ zlog_err("FE-CLIENT: %s: ERROR: " fmt, __func__, ##__VA_ARGS__) -#define MGMTD_DBG_FE_CLIENT_CHECK() \ +#define debug_check_fe_client() \ DEBUG_MODE_CHECK(&mgmt_dbg_fe_client, DEBUG_MODE_ALL) /* @@ -379,12 +384,18 @@ extern int mgmt_fe_send_regnotify_req(struct mgmt_fe_client *client, * req_id * Client request ID. * + * datastore + * Datastore for getting data. + * * result_type * The LYD_FORMAT of the result. * * flags * Flags to control the behavior of the request. * + * defaults + * Options to control the reporting of default values. + * * xpath * the xpath to get. * @@ -393,7 +404,8 @@ extern int mgmt_fe_send_regnotify_req(struct mgmt_fe_client *client, */ extern int mgmt_fe_send_get_data_req(struct mgmt_fe_client *client, uint64_t session_id, uint64_t req_id, - LYD_FORMAT result_type, uint8_t flags, + uint8_t datastore, LYD_FORMAT result_type, + uint8_t flags, uint8_t defaults, const char *xpath); /* diff --git a/lib/mgmt_msg_native.c b/lib/mgmt_msg_native.c index a9b26718db..d27c5d3a29 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 069cb9b150..7273170a13 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,31 @@ 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 + +/* + * Datastores + */ +#define MGMT_MSG_DATASTORE_STARTUP 0 +#define MGMT_MSG_DATASTORE_CANDIDATE 1 +#define MGMT_MSG_DATASTORE_RUNNING 2 +#define MGMT_MSG_DATASTORE_OPERATIONAL 3 + +/* + * Formats + */ +#define MGMT_MSG_FORMAT_XML 1 +#define MGMT_MSG_FORMAT_JSON 2 +#define MGMT_MSG_FORMAT_BINARY 3 /* non-standard libyang internal format */ + +/* + * Now we're using LYD_FORMAT directly to avoid mapping code, but having our + * own definitions allows us to create such a mapping in the future if libyang + * makes a backwards incompatible change. + */ +_Static_assert(MGMT_MSG_FORMAT_XML == LYD_XML, "Format mismatch"); +_Static_assert(MGMT_MSG_FORMAT_JSON == LYD_JSON, "Format mismatch"); +_Static_assert(MGMT_MSG_FORMAT_BINARY == LYD_LYB, "Format mismatch"); /** * struct mgmt_msg_header - Header common to all native messages. @@ -234,22 +260,34 @@ _Static_assert(sizeof(struct mgmt_msg_tree_data) == "Size mismatch"); /* Flags for get-data request */ -#define GET_DATA_FLAG_STATE 0x01 /* get only "config false" data */ -#define GET_DATA_FLAG_CONFIG 0x02 /* get only "config true" data */ +#define GET_DATA_FLAG_STATE 0x01 /* include "config false" data */ +#define GET_DATA_FLAG_CONFIG 0x02 /* include "config true" data */ #define GET_DATA_FLAG_EXACT 0x04 /* get exact data node instead of the full tree */ +/* + * Modes of reporting default values. Non-default values are always reported. + * These options reflect "with-defaults" modes as defined in RFC 6243. + */ +#define GET_DATA_DEFAULTS_EXPLICIT 0 /* "explicit" */ +#define GET_DATA_DEFAULTS_TRIM 1 /* "trim" */ +#define GET_DATA_DEFAULTS_ALL 2 /* "report-all" */ +#define GET_DATA_DEFAULTS_ALL_ADD_TAG 3 /* "report-all-tagged" */ + /** * struct mgmt_msg_get_data - frontend get-data request. * * @result_type: ``LYD_FORMAT`` for the returned result. * @flags: combination of ``GET_DATA_FLAG_*`` flags. + * @defaults: one of ``GET_DATA_DEFAULTS_*`` values. * @xpath: the query for the data to return. */ struct mgmt_msg_get_data { struct mgmt_msg_header; uint8_t result_type; uint8_t flags; - uint8_t resv2[6]; + uint8_t defaults; + uint8_t datastore; + uint8_t resv2[4]; alignas(8) char xpath[]; }; @@ -257,8 +295,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 b1da3315d0..a0b1bd18c5 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 2d9643e7b4..9279122deb 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/routing_nb.h b/lib/routing_nb.h index c185091a4b..cc83d8469d 100644 --- a/lib/routing_nb.h +++ b/lib/routing_nb.h @@ -29,6 +29,8 @@ int routing_control_plane_protocols_control_plane_protocol_destroy( * based on the control plane protocol */ DECLARE_HOOK(routing_conf_event, (struct nb_cb_create_args *args), (args)); +DECLARE_HOOK(routing_create, (struct nb_cb_create_args *args), (args)); +DECLARE_KOOH(routing_destroy, (struct nb_cb_destroy_args *args), (args)); void routing_control_plane_protocols_register_vrf_dependency(void); diff --git a/lib/routing_nb_config.c b/lib/routing_nb_config.c index 2b20e6c14b..d532279a22 100644 --- a/lib/routing_nb_config.c +++ b/lib/routing_nb_config.c @@ -14,6 +14,8 @@ DEFINE_HOOK(routing_conf_event, (struct nb_cb_create_args *args), (args)); +DEFINE_HOOK(routing_create, (struct nb_cb_create_args *args), (args)); +DEFINE_KOOH(routing_destroy, (struct nb_cb_destroy_args *args), (args)); /* * XPath: /frr-routing:routing/control-plane-protocols/control-plane-protocol @@ -49,6 +51,7 @@ int routing_control_plane_protocols_control_plane_protocol_create( assert(vrf); nb_running_set_entry(args->dnode, vrf); } + hook_call(routing_create, args); break; }; @@ -61,6 +64,8 @@ int routing_control_plane_protocols_control_plane_protocol_destroy( if (args->event != NB_EV_APPLY) return NB_OK; + hook_call(routing_destroy, args); + /* * If dependency on VRF module is registered, then VRF * pointer was stored and must be cleared. @@ -681,18 +681,6 @@ DEFUN_YANG (no_vrf, const char *vrfname = argv[2]->arg; char xpath_list[XPATH_MAXLEN]; - struct vrf *vrfp; - - vrfp = vrf_lookup_by_name(vrfname); - - if (vrfp == NULL) - return CMD_SUCCESS; - - if (CHECK_FLAG(vrfp->status, VRF_ACTIVE)) { - vty_out(vty, "%% Only inactive VRFs can be deleted\n"); - return CMD_WARNING_CONFIG_FAILED; - } - if (vrf_get_backend() == VRF_BACKEND_VRF_LITE) { /* * Remove the VRF interface config when removing the VRF. @@ -178,10 +178,10 @@ void vty_mgmt_resume_response(struct vty *vty, int ret) return; } - MGMTD_FE_CLIENT_DBG("resuming CLI cmd after %s on vty session-id: %" PRIu64 - " with '%s'", - vty->mgmt_req_pending_cmd, vty->mgmt_session_id, - ret == CMD_SUCCESS ? "success" : "failed"); + debug_fe_client("resuming CLI cmd after %s on vty session-id: %" PRIu64 + " with '%s'", + vty->mgmt_req_pending_cmd, vty->mgmt_session_id, + ret == CMD_SUCCESS ? "success" : "failed"); vty->mgmt_req_pending_cmd = NULL; @@ -2402,10 +2402,9 @@ static void vtysh_read(struct event *thread) * we get response through callback. */ if (vty->mgmt_req_pending_cmd) { - MGMTD_FE_CLIENT_DBG( - "postpone CLI response pending mgmtd %s on vty session-id %" PRIu64, - vty->mgmt_req_pending_cmd, - vty->mgmt_session_id); + debug_fe_client("postpone CLI response pending mgmtd %s on vty session-id %" PRIu64, + vty->mgmt_req_pending_cmd, + vty->mgmt_session_id); return; } @@ -2486,14 +2485,14 @@ void vty_close(struct vty *vty) * so warn the user. */ if (vty->mgmt_num_pending_setcfg) - MGMTD_FE_CLIENT_ERR( + log_err_fe_client( "vty closed, uncommitted config will be lost."); /* Drop out of configure / transaction if needed. */ vty_config_exit(vty); if (mgmt_fe_client && vty->mgmt_session_id) { - MGMTD_FE_CLIENT_DBG("closing vty session"); + debug_fe_client("closing vty session"); mgmt_fe_destroy_client_session(mgmt_fe_client, vty->mgmt_client_id); vty->mgmt_session_id = 0; @@ -3473,9 +3472,8 @@ void vty_init_vtysh(void) static void vty_mgmt_server_connected(struct mgmt_fe_client *client, uintptr_t usr_data, bool connected) { - MGMTD_FE_CLIENT_DBG("Got %sconnected %s MGMTD Frontend Server", - !connected ? "dis: " : "", - !connected ? "from" : "to"); + debug_fe_client("Got %sconnected %s MGMTD Frontend Server", + !connected ? "dis: " : "", !connected ? "from" : "to"); /* * We should not have any sessions for connecting or disconnecting case. @@ -3511,8 +3509,8 @@ static void vty_mgmt_session_notify(struct mgmt_fe_client *client, return; } - MGMTD_FE_CLIENT_DBG("%s session for client %" PRIu64 " successfully", - create ? "Created" : "Destroyed", client_id); + debug_fe_client("%s session for client %" PRIu64 " successfully", + create ? "Created" : "Destroyed", client_id); if (create) { assert(session_id != 0); @@ -3543,8 +3541,8 @@ static void vty_mgmt_ds_lock_notified(struct mgmt_fe_client *client, zlog_err("%socking for DS %u failed, Err: '%s' vty %p", lock_ds ? "L" : "Unl", ds_id, errmsg_if_any, vty); else { - MGMTD_FE_CLIENT_DBG("%socked DS %u successfully", - lock_ds ? "L" : "Unl", ds_id); + debug_fe_client("%socked DS %u successfully", + lock_ds ? "L" : "Unl", ds_id); if (ds_id == MGMTD_DS_CANDIDATE) vty->mgmt_locked_candidate_ds = lock_ds; else @@ -3575,9 +3573,9 @@ static void vty_mgmt_set_config_result_notified( vty_out(vty, "ERROR: SET_CONFIG request failed, Error: %s\n", errmsg_if_any ? errmsg_if_any : "Unknown"); } else { - MGMTD_FE_CLIENT_DBG("SET_CONFIG request for client 0x%" PRIx64 - " req-id %" PRIu64 " was successfull", - client_id, req_id); + debug_fe_client("SET_CONFIG request for client 0x%" PRIx64 + " req-id %" PRIu64 " was successfull", + client_id, req_id); } if (implicit_commit) { @@ -3607,10 +3605,9 @@ static void vty_mgmt_commit_config_result_notified( vty_out(vty, "ERROR: COMMIT_CONFIG request failed, Error: %s\n", errmsg_if_any ? errmsg_if_any : "Unknown"); } else { - MGMTD_FE_CLIENT_DBG( - "COMMIT_CONFIG request for client 0x%" PRIx64 - " req-id %" PRIu64 " was successfull", - client_id, req_id); + debug_fe_client("COMMIT_CONFIG request for client 0x%" PRIx64 + " req-id %" PRIu64 " was successfull", + client_id, req_id); if (errmsg_if_any) vty_out(vty, "MGMTD: %s\n", errmsg_if_any); } @@ -3640,9 +3637,9 @@ static int vty_mgmt_get_data_result_notified( return -1; } - MGMTD_FE_CLIENT_DBG("GET_DATA request succeeded, client 0x%" PRIx64 - " req-id %" PRIu64, - client_id, req_id); + debug_fe_client("GET_DATA request succeeded, client 0x%" PRIx64 + " req-id %" PRIu64, + client_id, req_id); if (req_id != mgmt_last_req_id) { mgmt_last_req_id = req_id; @@ -3788,10 +3785,9 @@ static int vty_mgmt_get_tree_result_notified( vty = (struct vty *)session_ctx; - MGMTD_FE_CLIENT_DBG("GET_TREE request %ssucceeded, client 0x%" PRIx64 - " req-id %" PRIu64, - partial_error ? "partially " : "", client_id, - req_id); + debug_fe_client("GET_TREE request %ssucceeded, client 0x%" PRIx64 + " req-id %" PRIu64, + partial_error ? "partially " : "", client_id, req_id); assert(result_type == LYD_LYB || result_type == vty->mgmt_req_pending_data); @@ -3838,21 +3834,20 @@ static int vty_mgmt_error_notified(struct mgmt_fe_client *client, const char *cname = mgmt_fe_client_name(client); if (!vty->mgmt_req_pending_cmd) { - MGMTD_FE_CLIENT_DBG("Erorr with no pending command: %d returned for client %s 0x%" PRIx64 - " session-id %" PRIu64 " req-id %" PRIu64 - "error-str %s", - error, cname, client_id, session_id, req_id, - errstr); + debug_fe_client("Erorr with no pending command: %d returned for client %s 0x%" PRIx64 + " session-id %" PRIu64 " req-id %" PRIu64 + "error-str %s", + error, cname, client_id, session_id, req_id, + errstr); vty_out(vty, "%% Error %d from MGMTD for %s with no pending command: %s\n", error, cname, errstr); return CMD_WARNING; } - MGMTD_FE_CLIENT_DBG("Erorr %d returned for client %s 0x%" PRIx64 - " session-id %" PRIu64 " req-id %" PRIu64 - "error-str %s", - error, cname, client_id, session_id, req_id, errstr); + debug_fe_client("Erorr %d returned for client %s 0x%" PRIx64 + " session-id %" PRIu64 " req-id %" PRIu64 "error-str %s", + error, cname, client_id, session_id, req_id, errstr); vty_out(vty, "%% %s (for %s, client %s)\n", errstr, vty->mgmt_req_pending_cmd, cname); @@ -4101,16 +4096,17 @@ int vty_mgmt_send_get_req(struct vty *vty, bool is_config, return 0; } -int vty_mgmt_send_get_data_req(struct vty *vty, LYD_FORMAT result_type, - uint8_t flags, const char *xpath) +int vty_mgmt_send_get_data_req(struct vty *vty, uint8_t datastore, + LYD_FORMAT result_type, uint8_t flags, + uint8_t defaults, const char *xpath) { LYD_FORMAT intern_format = result_type; vty->mgmt_req_id++; if (mgmt_fe_send_get_data_req(mgmt_fe_client, vty->mgmt_session_id, - vty->mgmt_req_id, intern_format, flags, - xpath)) { + vty->mgmt_req_id, datastore, + intern_format, flags, defaults, xpath)) { zlog_err("Failed to send GET-DATA to MGMTD session-id: %" PRIu64 " req-id %" PRIu64 ".", vty->mgmt_session_id, vty->mgmt_req_id); @@ -420,8 +420,9 @@ extern int vty_mgmt_send_commit_config(struct vty *vty, bool validate_only, extern int vty_mgmt_send_get_req(struct vty *vty, bool is_config, Mgmtd__DatastoreId datastore, const char **xpath_list, int num_req); -extern int vty_mgmt_send_get_data_req(struct vty *vty, LYD_FORMAT result_type, - uint8_t flags, const char *xpath); +extern int vty_mgmt_send_get_data_req(struct vty *vty, uint8_t datastore, + LYD_FORMAT result_type, uint8_t flags, + uint8_t defaults, const char *xpath); extern int vty_mgmt_send_lockds_req(struct vty *vty, Mgmtd__DatastoreId ds_id, bool lock, bool scok); extern void vty_mgmt_resume_response(struct vty *vty, int ret); diff --git a/lib/yang.c b/lib/yang.c index 3dd2513a4b..2b360376d3 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 431b2eee48..4ed0a39ba4 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. * diff --git a/lib/zebra.h b/lib/zebra.h index 8b0800c257..15a54f6cdf 100644 --- a/lib/zebra.h +++ b/lib/zebra.h @@ -221,9 +221,6 @@ struct in_pktinfo { #define IN6_ARE_ADDR_EQUAL IN6_IS_ADDR_EQUAL #endif /* IN6_ARE_ADDR_EQUAL */ -/* default zebra TCP port for zclient */ -#define ZEBRA_PORT 2600 - /* Zebra route's types are defined in route_types.h */ #include "lib/route_types.h" |
