diff options
author | Donald Sharp <donaldsharp72@gmail.com> | 2023-06-05 13:51:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-05 13:51:25 +0200 |
commit | 42d6f84585e4f0e9e558eeff9be404c01f1b59cc (patch) | |
tree | 273fcdc08f1b021f27369daf331f986756c8847d | |
parent | Merge pull request #13656 from sri-mohan1/srib-ldpd1 (diff) | |
parent | lib: mgmtd: remove obfuscating abstraction layer and other cleanup (diff) | |
download | frr-42d6f84585e4f0e9e558eeff9be404c01f1b59cc.tar.xz frr-42d6f84585e4f0e9e558eeff9be404c01f1b59cc.zip |
Merge pull request #13677 from LabNConsulting/chopps/bad-abstract
lib: mgmtd: remove obfuscating abstraction layer and other cleanup
-rw-r--r-- | lib/mgmt_fe_client.c | 466 | ||||
-rw-r--r-- | lib/mgmt_fe_client.h | 179 | ||||
-rw-r--r-- | lib/vty.c | 115 |
3 files changed, 292 insertions, 468 deletions
diff --git a/lib/mgmt_fe_client.c b/lib/mgmt_fe_client.c index 83f60ea58..35a6d7d90 100644 --- a/lib/mgmt_fe_client.c +++ b/lib/mgmt_fe_client.c @@ -19,14 +19,12 @@ #include "lib/mgmt_fe_client_clippy.c" -struct mgmt_fe_client_ctx; - PREDECL_LIST(mgmt_sessions); struct mgmt_fe_client_session { uint64_t client_id; /* FE client identifies itself with this ID */ uint64_t session_id; /* FE adapter identified session with this ID */ - struct mgmt_fe_client_ctx *client_ctx; + struct mgmt_fe_client *client; uintptr_t user_ctx; struct mgmt_sessions_item list_linkage; @@ -34,29 +32,31 @@ struct mgmt_fe_client_session { DECLARE_LIST(mgmt_sessions, struct mgmt_fe_client_session, list_linkage); -DEFINE_MTYPE_STATIC(LIB, MGMTD_FE_SESSION, "MGMTD Frontend session"); +DEFINE_MTYPE_STATIC(LIB, MGMTD_FE_CLIENT, "frontend client"); +DEFINE_MTYPE_STATIC(LIB, MGMTD_FE_CLIENT_NAME, "frontend client name"); +DEFINE_MTYPE_STATIC(LIB, MGMTD_FE_SESSION, "frontend session"); -struct mgmt_fe_client_ctx { +struct mgmt_fe_client { struct msg_client client; - struct mgmt_fe_client_params client_params; - struct mgmt_sessions_head client_sessions; + char *name; + struct mgmt_fe_client_cbs cbs; + uintptr_t user_data; + struct mgmt_sessions_head sessions; }; -#define FOREACH_SESSION_IN_LIST(client_ctx, session) \ - frr_each_safe (mgmt_sessions, &(client_ctx)->client_sessions, (session)) +#define FOREACH_SESSION_IN_LIST(client, session) \ + frr_each_safe (mgmt_sessions, &(client)->sessions, (session)) struct debug mgmt_dbg_fe_client = {0, "Management frontend client operations"}; -static struct mgmt_fe_client_ctx mgmt_fe_client_ctx = { - .client = {.conn = {.fd = -1}}}; static struct mgmt_fe_client_session * -mgmt_fe_find_session_by_client_id(struct mgmt_fe_client_ctx *client_ctx, +mgmt_fe_find_session_by_client_id(struct mgmt_fe_client *client, uint64_t client_id) { struct mgmt_fe_client_session *session; - FOREACH_SESSION_IN_LIST (client_ctx, session) { + FOREACH_SESSION_IN_LIST (client, session) { if (session->client_id == client_id) { MGMTD_FE_CLIENT_DBG("Found session-id %" PRIu64 " using client-id %" PRIu64, @@ -70,12 +70,12 @@ mgmt_fe_find_session_by_client_id(struct mgmt_fe_client_ctx *client_ctx, } static struct mgmt_fe_client_session * -mgmt_fe_find_session_by_session_id(struct mgmt_fe_client_ctx *client_ctx, +mgmt_fe_find_session_by_session_id(struct mgmt_fe_client *client, uint64_t session_id) { struct mgmt_fe_client_session *session; - FOREACH_SESSION_IN_LIST (client_ctx, session) { + FOREACH_SESSION_IN_LIST (client, session) { if (session->session_id == session_id) { MGMTD_FE_CLIENT_DBG( "Found session of client-id %" PRIu64 @@ -89,24 +89,24 @@ mgmt_fe_find_session_by_session_id(struct mgmt_fe_client_ctx *client_ctx, return NULL; } -static int mgmt_fe_client_send_msg(struct mgmt_fe_client_ctx *client_ctx, +static int mgmt_fe_client_send_msg(struct mgmt_fe_client *client, Mgmtd__FeMessage *fe_msg, bool short_circuit_ok) { return msg_conn_send_msg( - &client_ctx->client.conn, MGMT_MSG_VERSION_PROTOBUF, fe_msg, + &client->client.conn, MGMT_MSG_VERSION_PROTOBUF, fe_msg, mgmtd__fe_message__get_packed_size(fe_msg), (size_t(*)(void *, void *))mgmtd__fe_message__pack, short_circuit_ok); } -static int mgmt_fe_send_register_req(struct mgmt_fe_client_ctx *client_ctx) +static int mgmt_fe_send_register_req(struct mgmt_fe_client *client) { Mgmtd__FeMessage fe_msg; Mgmtd__FeRegisterReq rgstr_req; mgmtd__fe_register_req__init(&rgstr_req); - rgstr_req.client_name = client_ctx->client_params.name; + rgstr_req.client_name = client->name; mgmtd__fe_message__init(&fe_msg); fe_msg.message_case = MGMTD__FE_MESSAGE__MESSAGE_REGISTER_REQ; @@ -115,10 +115,10 @@ static int mgmt_fe_send_register_req(struct mgmt_fe_client_ctx *client_ctx) MGMTD_FE_CLIENT_DBG( "Sending REGISTER_REQ message to MGMTD Frontend server"); - return mgmt_fe_client_send_msg(client_ctx, &fe_msg, true); + return mgmt_fe_client_send_msg(client, &fe_msg, true); } -static int mgmt_fe_send_session_req(struct mgmt_fe_client_ctx *client_ctx, +static int mgmt_fe_send_session_req(struct mgmt_fe_client *client, struct mgmt_fe_client_session *session, bool create) { @@ -146,12 +146,12 @@ static int mgmt_fe_send_session_req(struct mgmt_fe_client_ctx *client_ctx, "Sending SESSION_REQ %s message for client-id %" PRIu64, create ? "create" : "destroy", session->client_id); - return mgmt_fe_client_send_msg(client_ctx, &fe_msg, scok); + return mgmt_fe_client_send_msg(client, &fe_msg, scok); } -static int mgmt_fe_send_lockds_req(struct mgmt_fe_client_ctx *client_ctx, - uint64_t session_id, bool lock, - uint64_t req_id, Mgmtd__DatastoreId ds_id) +int mgmt_fe_send_lockds_req(struct mgmt_fe_client *client, uint64_t session_id, + uint64_t req_id, Mgmtd__DatastoreId ds_id, + bool lock) { (void)req_id; Mgmtd__FeMessage fe_msg; @@ -171,15 +171,13 @@ static int mgmt_fe_send_lockds_req(struct mgmt_fe_client_ctx *client_ctx, "Sending %sLOCK_REQ message for Ds:%d session-id %" PRIu64, lock ? "" : "UN", ds_id, session_id); - return mgmt_fe_client_send_msg(client_ctx, &fe_msg, false); + return mgmt_fe_client_send_msg(client, &fe_msg, false); } -static int mgmt_fe_send_setcfg_req(struct mgmt_fe_client_ctx *client_ctx, - uint64_t session_id, uint64_t req_id, - Mgmtd__DatastoreId ds_id, - Mgmtd__YangCfgDataReq **data_req, - int num_data_reqs, bool implicit_commit, - Mgmtd__DatastoreId dst_ds_id) +int mgmt_fe_send_setcfg_req(struct mgmt_fe_client *client, uint64_t session_id, + uint64_t req_id, Mgmtd__DatastoreId ds_id, + Mgmtd__YangCfgDataReq **data_req, int num_data_reqs, + bool implicit_commit, Mgmtd__DatastoreId dst_ds_id) { (void)req_id; Mgmtd__FeMessage fe_msg; @@ -203,14 +201,14 @@ static int mgmt_fe_send_setcfg_req(struct mgmt_fe_client_ctx *client_ctx, " (#xpaths:%d)", ds_id, session_id, num_data_reqs); - return mgmt_fe_client_send_msg(client_ctx, &fe_msg, false); + return mgmt_fe_client_send_msg(client, &fe_msg, false); } -static int mgmt_fe_send_commitcfg_req(struct mgmt_fe_client_ctx *client_ctx, - uint64_t session_id, uint64_t req_id, - Mgmtd__DatastoreId src_ds_id, - Mgmtd__DatastoreId dest_ds_id, - bool validate_only, bool abort) +int mgmt_fe_send_commitcfg_req(struct mgmt_fe_client *client, + uint64_t session_id, uint64_t req_id, + Mgmtd__DatastoreId src_ds_id, + Mgmtd__DatastoreId dest_ds_id, + bool validate_only, bool abort) { (void)req_id; Mgmtd__FeMessage fe_msg; @@ -232,14 +230,13 @@ static int mgmt_fe_send_commitcfg_req(struct mgmt_fe_client_ctx *client_ctx, "Sending COMMIT_CONFIG_REQ message for Src-Ds:%d, Dst-Ds:%d session-id %" PRIu64, src_ds_id, dest_ds_id, session_id); - return mgmt_fe_client_send_msg(client_ctx, &fe_msg, false); + return mgmt_fe_client_send_msg(client, &fe_msg, false); } -static int mgmt_fe_send_getcfg_req(struct mgmt_fe_client_ctx *client_ctx, - uint64_t session_id, uint64_t req_id, - Mgmtd__DatastoreId ds_id, - Mgmtd__YangGetDataReq *data_req[], - int num_data_reqs) +int mgmt_fe_send_getcfg_req(struct mgmt_fe_client *client, uint64_t session_id, + uint64_t req_id, Mgmtd__DatastoreId ds_id, + Mgmtd__YangGetDataReq *data_req[], + int num_data_reqs) { (void)req_id; Mgmtd__FeMessage fe_msg; @@ -261,14 +258,13 @@ static int mgmt_fe_send_getcfg_req(struct mgmt_fe_client_ctx *client_ctx, " (#xpaths:%d)", ds_id, session_id, num_data_reqs); - return mgmt_fe_client_send_msg(client_ctx, &fe_msg, false); + return mgmt_fe_client_send_msg(client, &fe_msg, false); } -static int mgmt_fe_send_getdata_req(struct mgmt_fe_client_ctx *client_ctx, - uint64_t session_id, uint64_t req_id, - Mgmtd__DatastoreId ds_id, - Mgmtd__YangGetDataReq *data_req[], - int num_data_reqs) +int mgmt_fe_send_getdata_req(struct mgmt_fe_client *client, uint64_t session_id, + uint64_t req_id, Mgmtd__DatastoreId ds_id, + Mgmtd__YangGetDataReq *data_req[], + int num_data_reqs) { (void)req_id; Mgmtd__FeMessage fe_msg; @@ -290,15 +286,14 @@ static int mgmt_fe_send_getdata_req(struct mgmt_fe_client_ctx *client_ctx, " (#xpaths:%d)", ds_id, session_id, num_data_reqs); - return mgmt_fe_client_send_msg(client_ctx, &fe_msg, false); + return mgmt_fe_client_send_msg(client, &fe_msg, false); } -static int mgmt_fe_send_regnotify_req(struct mgmt_fe_client_ctx *client_ctx, - uint64_t session_id, uint64_t req_id, - Mgmtd__DatastoreId ds_id, - bool register_req, - Mgmtd__YangDataXPath *data_req[], - int num_data_reqs) +int mgmt_fe_send_regnotify_req(struct mgmt_fe_client *client, + uint64_t session_id, uint64_t req_id, + Mgmtd__DatastoreId ds_id, bool register_req, + Mgmtd__YangDataXPath *data_req[], + int num_data_reqs) { (void)req_id; Mgmtd__FeMessage fe_msg; @@ -315,10 +310,10 @@ static int mgmt_fe_send_regnotify_req(struct mgmt_fe_client_ctx *client_ctx, fe_msg.message_case = MGMTD__FE_MESSAGE__MESSAGE_REGNOTIFY_REQ; fe_msg.regnotify_req = ®ntfy_req; - return mgmt_fe_client_send_msg(client_ctx, &fe_msg, false); + return mgmt_fe_client_send_msg(client, &fe_msg, false); } -static int mgmt_fe_client_handle_msg(struct mgmt_fe_client_ctx *client_ctx, +static int mgmt_fe_client_handle_msg(struct mgmt_fe_client *client, Mgmtd__FeMessage *fe_msg) { struct mgmt_fe_client_session *session = NULL; @@ -338,8 +333,7 @@ static int mgmt_fe_client_handle_msg(struct mgmt_fe_client_ctx *client_ctx, fe_msg->session_reply->session_id); session = mgmt_fe_find_session_by_client_id( - client_ctx, - fe_msg->session_reply->client_conn_id); + client, fe_msg->session_reply->client_conn_id); if (session && fe_msg->session_reply->success) { MGMTD_FE_CLIENT_DBG( @@ -358,17 +352,14 @@ static int mgmt_fe_client_handle_msg(struct mgmt_fe_client_ctx *client_ctx, fe_msg->session_reply->session_id); session = mgmt_fe_find_session_by_session_id( - client_ctx, fe_msg->session_req->session_id); + client, fe_msg->session_req->session_id); } /* The session state may be deleted by the callback */ - if (session && session->client_ctx && - session->client_ctx->client_params.client_session_notify) - (*session->client_ctx->client_params - .client_session_notify)( - (uintptr_t)client_ctx, - client_ctx->client_params.user_data, - session->client_id, + if (session && session->client && + session->client->cbs.client_session_notify) + (*session->client->cbs.client_session_notify)( + client, client->user_data, session->client_id, fe_msg->session_reply->create, fe_msg->session_reply->success, fe_msg->session_reply->session_id, @@ -378,14 +369,12 @@ static int mgmt_fe_client_handle_msg(struct mgmt_fe_client_ctx *client_ctx, MGMTD_FE_CLIENT_DBG("Got LOCKDS_REPLY for session-id %" PRIu64, fe_msg->lockds_reply->session_id); session = mgmt_fe_find_session_by_session_id( - client_ctx, fe_msg->lockds_reply->session_id); - - if (session && session->client_ctx && - session->client_ctx->client_params.lock_ds_notify) - (*session->client_ctx->client_params.lock_ds_notify)( - (uintptr_t)client_ctx, - client_ctx->client_params.user_data, - session->client_id, + client, fe_msg->lockds_reply->session_id); + + if (session && session->client && + session->client->cbs.lock_ds_notify) + (*session->client->cbs.lock_ds_notify)( + client, client->user_data, session->client_id, fe_msg->lockds_reply->session_id, session->user_ctx, fe_msg->lockds_reply->req_id, fe_msg->lockds_reply->lock, @@ -398,14 +387,12 @@ static int mgmt_fe_client_handle_msg(struct mgmt_fe_client_ctx *client_ctx, fe_msg->setcfg_reply->session_id); session = mgmt_fe_find_session_by_session_id( - client_ctx, fe_msg->setcfg_reply->session_id); - - if (session && session->client_ctx && - session->client_ctx->client_params.set_config_notify) - (*session->client_ctx->client_params.set_config_notify)( - (uintptr_t)client_ctx, - client_ctx->client_params.user_data, - session->client_id, + client, fe_msg->setcfg_reply->session_id); + + if (session && session->client && + session->client->cbs.set_config_notify) + (*session->client->cbs.set_config_notify)( + client, client->user_data, session->client_id, fe_msg->setcfg_reply->session_id, session->user_ctx, fe_msg->setcfg_reply->req_id, fe_msg->setcfg_reply->success, @@ -417,15 +404,12 @@ static int mgmt_fe_client_handle_msg(struct mgmt_fe_client_ctx *client_ctx, fe_msg->commcfg_reply->session_id); session = mgmt_fe_find_session_by_session_id( - client_ctx, fe_msg->commcfg_reply->session_id); - - if (session && session->client_ctx && - session->client_ctx->client_params.commit_config_notify) - (*session->client_ctx->client_params - .commit_config_notify)( - (uintptr_t)client_ctx, - client_ctx->client_params.user_data, - session->client_id, + client, fe_msg->commcfg_reply->session_id); + + if (session && session->client && + session->client->cbs.commit_config_notify) + (*session->client->cbs.commit_config_notify)( + client, client->user_data, session->client_id, fe_msg->commcfg_reply->session_id, session->user_ctx, fe_msg->commcfg_reply->req_id, @@ -440,14 +424,12 @@ static int mgmt_fe_client_handle_msg(struct mgmt_fe_client_ctx *client_ctx, fe_msg->getcfg_reply->session_id); session = mgmt_fe_find_session_by_session_id( - client_ctx, fe_msg->getcfg_reply->session_id); - - if (session && session->client_ctx && - session->client_ctx->client_params.get_data_notify) - (*session->client_ctx->client_params.get_data_notify)( - (uintptr_t)client_ctx, - client_ctx->client_params.user_data, - session->client_id, + client, fe_msg->getcfg_reply->session_id); + + if (session && session->client && + session->client->cbs.get_data_notify) + (*session->client->cbs.get_data_notify)( + client, client->user_data, session->client_id, fe_msg->getcfg_reply->session_id, session->user_ctx, fe_msg->getcfg_reply->req_id, fe_msg->getcfg_reply->success, @@ -468,14 +450,12 @@ static int mgmt_fe_client_handle_msg(struct mgmt_fe_client_ctx *client_ctx, fe_msg->getdata_reply->session_id); session = mgmt_fe_find_session_by_session_id( - client_ctx, fe_msg->getdata_reply->session_id); - - if (session && session->client_ctx && - session->client_ctx->client_params.get_data_notify) - (*session->client_ctx->client_params.get_data_notify)( - (uintptr_t)client_ctx, - client_ctx->client_params.user_data, - session->client_id, + client, fe_msg->getdata_reply->session_id); + + if (session && session->client && + session->client->cbs.get_data_notify) + (*session->client->cbs.get_data_notify)( + client, client->user_data, session->client_id, fe_msg->getdata_reply->session_id, session->user_ctx, fe_msg->getdata_reply->req_id, @@ -526,12 +506,12 @@ static int mgmt_fe_client_handle_msg(struct mgmt_fe_client_ctx *client_ctx, static void mgmt_fe_client_process_msg(uint8_t version, uint8_t *data, size_t len, struct msg_conn *conn) { - struct mgmt_fe_client_ctx *client_ctx; - struct msg_client *client; + struct mgmt_fe_client *client; + struct msg_client *msg_client; Mgmtd__FeMessage *fe_msg; - client = container_of(conn, struct msg_client, conn); - client_ctx = container_of(client, struct mgmt_fe_client_ctx, client); + msg_client = container_of(conn, struct msg_client, conn); + client = container_of(msg_client, struct mgmt_fe_client, client); fe_msg = mgmtd__fe_message__unpack(NULL, len, data); if (!fe_msg) { @@ -542,41 +522,38 @@ static void mgmt_fe_client_process_msg(uint8_t version, uint8_t *data, MGMTD_FE_CLIENT_DBG( "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_ctx, fe_msg); + (void)mgmt_fe_client_handle_msg(client, fe_msg); mgmtd__fe_message__free_unpacked(fe_msg, NULL); } -static int _notify_connect_disconnect(struct msg_client *client, bool connected) +static int _notify_connect_disconnect(struct msg_client *msg_client, + bool connected) { - struct mgmt_fe_client_ctx *client_ctx = - container_of(client, struct mgmt_fe_client_ctx, client); + struct mgmt_fe_client *client = + container_of(msg_client, struct mgmt_fe_client, client); struct mgmt_fe_client_session *session; int ret; /* Send REGISTER_REQ message */ if (connected) { - if ((ret = mgmt_fe_send_register_req(client_ctx)) != 0) + if ((ret = mgmt_fe_send_register_req(client)) != 0) return ret; } /* Walk list of sessions for this FE client deleting them */ - if (!connected && mgmt_sessions_count(&client_ctx->client_sessions)) { + if (!connected && mgmt_sessions_count(&client->sessions)) { MGMTD_FE_CLIENT_DBG("Cleaning up existing sessions"); - FOREACH_SESSION_IN_LIST (client_ctx, session) { - assert(session->client_ctx); + FOREACH_SESSION_IN_LIST (client, session) { + assert(session->client); /* unlink from list first this avoids double free */ - mgmt_sessions_del(&client_ctx->client_sessions, - session); + mgmt_sessions_del(&client->sessions, session); /* notify FE client the session is being deleted */ - if (session->client_ctx->client_params - .client_session_notify) { - (*session->client_ctx->client_params - .client_session_notify)( - (uintptr_t)client_ctx, - client_ctx->client_params.user_data, + if (session->client->cbs.client_session_notify) { + (*session->client->cbs.client_session_notify)( + client, client->user_data, session->client_id, false, true, session->session_id, session->user_ctx); } @@ -586,10 +563,9 @@ static int _notify_connect_disconnect(struct msg_client *client, bool connected) } /* Notify FE client through registered callback (if any). */ - if (client_ctx->client_params.client_connect_notify) - (void)(*client_ctx->client_params.client_connect_notify)( - (uintptr_t)client_ctx, - client_ctx->client_params.user_data, connected); + if (client->cbs.client_connect_notify) + (void)(*client->cbs.client_connect_notify)( + client, client->user_data, connected); return 0; } @@ -651,26 +627,31 @@ static struct cmd_node mgmt_dbg_node = { /* * Initialize library and try connecting with MGMTD. */ -uintptr_t mgmt_fe_client_lib_init(struct mgmt_fe_client_params *params, - struct event_loop *master_thread) +struct mgmt_fe_client *mgmt_fe_client_create(const char *client_name, + struct mgmt_fe_client_cbs *cbs, + uintptr_t user_data, + struct event_loop *event_loop) { - /* Don't call twice */ - assert(!mgmt_fe_client_ctx.client.conn.loop); + struct mgmt_fe_client *client = + XCALLOC(MTYPE_MGMTD_FE_CLIENT, sizeof(*client)); - mgmt_fe_client_ctx.client_params = *params; + client->name = XSTRDUP(MTYPE_MGMTD_FE_CLIENT_NAME, client_name); + client->user_data = user_data; + if (cbs) + client->cbs = *cbs; - mgmt_sessions_init(&mgmt_fe_client_ctx.client_sessions); + mgmt_sessions_init(&client->sessions); - msg_client_init(&mgmt_fe_client_ctx.client, master_thread, - MGMTD_FE_SERVER_PATH, mgmt_fe_client_notify_connect, + msg_client_init(&client->client, event_loop, MGMTD_FE_SERVER_PATH, + mgmt_fe_client_notify_connect, mgmt_fe_client_notify_disconnect, mgmt_fe_client_process_msg, MGMTD_FE_MAX_NUM_MSG_PROC, MGMTD_FE_MAX_NUM_MSG_WRITE, MGMTD_FE_MSG_MAX_LEN, true, "FE-client", MGMTD_DBG_FE_CLIENT_CHECK()); - MGMTD_FE_CLIENT_DBG("Initialized client '%s'", params->name); + MGMTD_FE_CLIENT_DBG("Initialized client '%s'", client_name); - return (uintptr_t)&mgmt_fe_client_ctx; + return client; } void mgmt_fe_client_lib_vty_init(void) @@ -681,39 +662,31 @@ void mgmt_fe_client_lib_vty_init(void) install_element(CONFIG_NODE, &debug_mgmt_client_fe_cmd); } -uint mgmt_fe_client_session_count(uintptr_t lib_hndl) +uint mgmt_fe_client_session_count(struct mgmt_fe_client *client) { - struct mgmt_fe_client_ctx *client_ctx = - (struct mgmt_fe_client_ctx *)lib_hndl; - - return mgmt_sessions_count(&client_ctx->client_sessions); + return mgmt_sessions_count(&client->sessions); } /* * Create a new Session for a Frontend Client connection. */ -enum mgmt_result mgmt_fe_create_client_session(uintptr_t lib_hndl, +enum mgmt_result mgmt_fe_create_client_session(struct mgmt_fe_client *client, uint64_t client_id, uintptr_t user_ctx) { - struct mgmt_fe_client_ctx *client_ctx; struct mgmt_fe_client_session *session; - client_ctx = (struct mgmt_fe_client_ctx *)lib_hndl; - if (!client_ctx) - return MGMTD_INVALID_PARAM; - session = XCALLOC(MTYPE_MGMTD_FE_SESSION, sizeof(struct mgmt_fe_client_session)); assert(session); session->user_ctx = user_ctx; session->client_id = client_id; - session->client_ctx = client_ctx; + session->client = client; session->session_id = 0; - mgmt_sessions_add_tail(&client_ctx->client_sessions, session); + mgmt_sessions_add_tail(&client->sessions, session); - if (mgmt_fe_send_session_req(client_ctx, session, true) != 0) { + if (mgmt_fe_send_session_req(client, session, true) != 0) { XFREE(MTYPE_MGMTD_FE_SESSION, session); return MGMTD_INTERNAL_ERROR; } @@ -724,189 +697,42 @@ enum mgmt_result mgmt_fe_create_client_session(uintptr_t lib_hndl, /* * Delete an existing Session for a Frontend Client connection. */ -enum mgmt_result mgmt_fe_destroy_client_session(uintptr_t lib_hndl, +enum mgmt_result mgmt_fe_destroy_client_session(struct mgmt_fe_client *client, uint64_t client_id) { - struct mgmt_fe_client_ctx *client_ctx; struct mgmt_fe_client_session *session; - client_ctx = (struct mgmt_fe_client_ctx *)lib_hndl; - if (!client_ctx) - return MGMTD_INVALID_PARAM; - - session = mgmt_fe_find_session_by_client_id(client_ctx, client_id); - if (!session || session->client_ctx != client_ctx) + session = mgmt_fe_find_session_by_client_id(client, client_id); + if (!session || session->client != client) return MGMTD_INVALID_PARAM; if (session->session_id && - mgmt_fe_send_session_req(client_ctx, session, false) != 0) + 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); - mgmt_sessions_del(&client_ctx->client_sessions, session); + mgmt_sessions_del(&client->sessions, session); XFREE(MTYPE_MGMTD_FE_SESSION, session); return MGMTD_SUCCESS; } -static void mgmt_fe_destroy_client_sessions(uintptr_t lib_hndl) -{ - struct mgmt_fe_client_ctx *client_ctx; - struct mgmt_fe_client_session *session; - - client_ctx = (struct mgmt_fe_client_ctx *)lib_hndl; - if (!client_ctx) - return; - - FOREACH_SESSION_IN_LIST (client_ctx, session) - mgmt_fe_destroy_client_session(lib_hndl, session->client_id); -} - -/* - * Send UN/LOCK_DS_REQ to MGMTD for a specific Datastore DS. - */ -enum mgmt_result mgmt_fe_lock_ds(uintptr_t lib_hndl, uint64_t session_id, - uint64_t req_id, Mgmtd__DatastoreId ds_id, - bool lock_ds) -{ - struct mgmt_fe_client_ctx *client_ctx; - - client_ctx = (struct mgmt_fe_client_ctx *)lib_hndl; - if (!client_ctx) - return MGMTD_INVALID_PARAM; - - if (mgmt_fe_send_lockds_req(client_ctx, session_id, lock_ds, req_id, - ds_id) != 0) - return MGMTD_INTERNAL_ERROR; - - return MGMTD_SUCCESS; -} - -/* - * Send SET_CONFIG_REQ to MGMTD for one or more config data(s). - */ -enum mgmt_result mgmt_fe_set_config_data(uintptr_t lib_hndl, - uint64_t session_id, uint64_t req_id, - Mgmtd__DatastoreId ds_id, - Mgmtd__YangCfgDataReq **config_req, - int num_reqs, bool implicit_commit, - Mgmtd__DatastoreId dst_ds_id) -{ - struct mgmt_fe_client_ctx *client_ctx; - - client_ctx = (struct mgmt_fe_client_ctx *)lib_hndl; - if (!client_ctx) - return MGMTD_INVALID_PARAM; - - if (mgmt_fe_send_setcfg_req(client_ctx, session_id, req_id, ds_id, - config_req, num_reqs, implicit_commit, - dst_ds_id) != 0) - return MGMTD_INTERNAL_ERROR; - - return MGMTD_SUCCESS; -} - -/* - * Send SET_CONFIG_REQ to MGMTD for one or more config data(s). - */ -enum mgmt_result mgmt_fe_commit_config_data(uintptr_t lib_hndl, - uint64_t session_id, - uint64_t req_id, - Mgmtd__DatastoreId src_ds_id, - Mgmtd__DatastoreId dst_ds_id, - bool validate_only, bool abort) -{ - struct mgmt_fe_client_ctx *client_ctx; - - client_ctx = (struct mgmt_fe_client_ctx *)lib_hndl; - if (!client_ctx) - return MGMTD_INVALID_PARAM; - - if (mgmt_fe_send_commitcfg_req(client_ctx, session_id, req_id, - src_ds_id, dst_ds_id, validate_only, - abort) != 0) - return MGMTD_INTERNAL_ERROR; - - return MGMTD_SUCCESS; -} - -/* - * Send GET_CONFIG_REQ to MGMTD for one or more config data item(s). - */ -enum mgmt_result mgmt_fe_get_config_data(uintptr_t lib_hndl, - uint64_t session_id, uint64_t req_id, - Mgmtd__DatastoreId ds_id, - Mgmtd__YangGetDataReq *data_req[], - int num_reqs) -{ - struct mgmt_fe_client_ctx *client_ctx; - - client_ctx = (struct mgmt_fe_client_ctx *)lib_hndl; - if (!client_ctx) - return MGMTD_INVALID_PARAM; - - if (mgmt_fe_send_getcfg_req(client_ctx, session_id, req_id, ds_id, - data_req, num_reqs) != 0) - return MGMTD_INTERNAL_ERROR; - - return MGMTD_SUCCESS; -} - -/* - * Send GET_DATA_REQ to MGMTD for one or more config data item(s). - */ -enum mgmt_result mgmt_fe_get_data(uintptr_t lib_hndl, uint64_t session_id, - uint64_t req_id, Mgmtd__DatastoreId ds_id, - Mgmtd__YangGetDataReq *data_req[], - int num_reqs) -{ - struct mgmt_fe_client_ctx *client_ctx; - - client_ctx = (struct mgmt_fe_client_ctx *)lib_hndl; - if (!client_ctx) - return MGMTD_INVALID_PARAM; - - if (mgmt_fe_send_getdata_req(client_ctx, session_id, req_id, ds_id, - data_req, num_reqs) != 0) - return MGMTD_INTERNAL_ERROR; - - return MGMTD_SUCCESS; -} - -/* - * Send NOTIFY_REGISTER_REQ to MGMTD daemon. - */ -enum mgmt_result -mgmt_fe_register_yang_notify(uintptr_t lib_hndl, uint64_t session_id, - uint64_t req_id, Mgmtd__DatastoreId ds_id, - bool register_req, - Mgmtd__YangDataXPath *data_req[], int num_reqs) -{ - struct mgmt_fe_client_ctx *client_ctx; - - client_ctx = (struct mgmt_fe_client_ctx *)lib_hndl; - if (!client_ctx) - return MGMTD_INVALID_PARAM; - - if (mgmt_fe_send_regnotify_req(client_ctx, session_id, req_id, ds_id, - register_req, data_req, num_reqs) != 0) - return MGMTD_INTERNAL_ERROR; - - return MGMTD_SUCCESS; -} - /* * Destroy library and cleanup everything. */ -void mgmt_fe_client_lib_destroy(void) +void mgmt_fe_client_destroy(struct mgmt_fe_client *client) { - struct mgmt_fe_client_ctx *client_ctx = &mgmt_fe_client_ctx; + struct mgmt_fe_client_session *session; MGMTD_FE_CLIENT_DBG("Destroying MGMTD Frontend Client '%s'", - client_ctx->client_params.name); + client->name); + + FOREACH_SESSION_IN_LIST (client, session) + mgmt_fe_destroy_client_session(client, session->client_id); + + msg_client_cleanup(&client->client); - mgmt_fe_destroy_client_sessions((uintptr_t)client_ctx); - msg_client_cleanup(&client_ctx->client); - memset(client_ctx, 0, sizeof(*client_ctx)); + XFREE(MTYPE_MGMTD_FE_CLIENT_NAME, client->name); + XFREE(MTYPE_MGMTD_FE_CLIENT, client); } diff --git a/lib/mgmt_fe_client.h b/lib/mgmt_fe_client.h index 7ce6c5eef..edf861746 100644 --- a/lib/mgmt_fe_client.h +++ b/lib/mgmt_fe_client.h @@ -56,6 +56,9 @@ extern "C" { #define MGMTD_DS_OPERATIONAL MGMTD__DATASTORE_ID__OPERATIONAL_DS #define MGMTD_DS_MAX_ID MGMTD_DS_OPERATIONAL + 1 +struct mgmt_fe_client; + + /* * All the client specific information this library needs to * initialize itself, setup connection with MGMTD FrontEnd interface @@ -66,52 +69,52 @@ extern "C" { * to initialize the library (See mgmt_fe_client_lib_init for * more details). */ -struct mgmt_fe_client_params { - char name[MGMTD_CLIENT_NAME_MAX_LEN]; - uintptr_t user_data; - unsigned long conn_retry_intvl_sec; - - void (*client_connect_notify)(uintptr_t lib_hndl, - uintptr_t user_data, - bool connected); - - void (*client_session_notify)(uintptr_t lib_hndl, - uintptr_t user_data, - uint64_t client_id, +struct mgmt_fe_client_cbs { + void (*client_connect_notify)(struct mgmt_fe_client *client, + uintptr_t user_data, bool connected); + + void (*client_session_notify)(struct mgmt_fe_client *client, + uintptr_t user_data, uint64_t client_id, bool create, bool success, uintptr_t session_id, - uintptr_t user_session_ctx); + uintptr_t user_session_client); - void (*lock_ds_notify)(uintptr_t lib_hndl, uintptr_t user_data, - uint64_t client_id, uintptr_t session_id, - uintptr_t user_session_ctx, uint64_t req_id, + void (*lock_ds_notify)(struct mgmt_fe_client *client, + uintptr_t user_data, uint64_t client_id, + uintptr_t session_id, + uintptr_t user_session_client, uint64_t req_id, bool lock_ds, bool success, Mgmtd__DatastoreId ds_id, char *errmsg_if_any); - void (*set_config_notify)(uintptr_t lib_hndl, uintptr_t user_data, - uint64_t client_id, uintptr_t session_id, - uintptr_t user_session_ctx, uint64_t req_id, - bool success, Mgmtd__DatastoreId ds_id, + void (*set_config_notify)(struct mgmt_fe_client *client, + uintptr_t user_data, uint64_t client_id, + uintptr_t session_id, + uintptr_t user_session_client, + uint64_t req_id, bool success, + Mgmtd__DatastoreId ds_id, char *errmsg_if_any); - void (*commit_config_notify)( - uintptr_t lib_hndl, uintptr_t user_data, uint64_t client_id, - uintptr_t session_id, uintptr_t user_session_ctx, - uint64_t req_id, bool success, Mgmtd__DatastoreId src_ds_id, - Mgmtd__DatastoreId dst_ds_id, bool validate_only, - char *errmsg_if_any); - - enum mgmt_result (*get_data_notify)( - uintptr_t lib_hndl, uintptr_t user_data, uint64_t client_id, - uintptr_t session_id, uintptr_t user_session_ctx, - uint64_t req_id, bool success, Mgmtd__DatastoreId ds_id, - Mgmtd__YangData **yang_data, size_t num_data, int next_key, - char *errmsg_if_any); - - enum mgmt_result (*data_notify)( - uint64_t client_id, uint64_t session_id, uintptr_t user_data, - uint64_t req_id, Mgmtd__DatastoreId ds_id, - Mgmtd__YangData **yang_data, size_t num_data); + void (*commit_config_notify)(struct mgmt_fe_client *client, + uintptr_t user_data, uint64_t client_id, + uintptr_t session_id, + uintptr_t user_session_client, + uint64_t req_id, bool success, + Mgmtd__DatastoreId src_ds_id, + Mgmtd__DatastoreId dst_ds_id, + bool validate_only, char *errmsg_if_any); + + int (*get_data_notify)(struct mgmt_fe_client *client, + uintptr_t user_data, uint64_t client_id, + uintptr_t session_id, + uintptr_t user_session_client, uint64_t req_id, + bool success, Mgmtd__DatastoreId ds_id, + Mgmtd__YangData **yang_data, size_t num_data, + int next_key, char *errmsg_if_any); + + int (*data_notify)(uint64_t client_id, uint64_t session_id, + uintptr_t user_data, uint64_t req_id, + Mgmtd__DatastoreId ds_id, + Mgmtd__YangData **yang_data, size_t num_data); }; extern struct debug mgmt_dbg_fe_client; @@ -139,17 +142,18 @@ extern struct debug mgmt_dbg_fe_client; * Thread master. * * Returns: - * Frontend client lib handler (nothing but address of mgmt_fe_client_ctx) + * Frontend client lib handler (nothing but address of mgmt_fe_client) */ -extern uintptr_t mgmt_fe_client_lib_init(struct mgmt_fe_client_params *params, - struct event_loop *master_thread); +extern struct mgmt_fe_client * +mgmt_fe_client_create(const char *client_name, struct mgmt_fe_client_cbs *cbs, + uintptr_t user_data, struct event_loop *event_loop); /* * Initialize library vty (adds debug support). * - * This call should be added to your component when enabling other vty code to - * enable mgmtd client debugs. When adding, one needs to also add a their - * component in `xref2vtysh.py` as well. + * This call should be added to your component when enabling other vty + * code to enable mgmtd client debugs. When adding, one needs to also + * add a their component in `xref2vtysh.py` as well. */ extern void mgmt_fe_client_lib_vty_init(void); @@ -167,15 +171,15 @@ extern void mgmt_debug_fe_client_show_debug(struct vty *vty); * client_id * Unique identifier of client. * - * user_ctx + * user_client * Client context. * * Returns: * MGMTD_SUCCESS on success, MGMTD_* otherwise. */ -extern enum mgmt_result mgmt_fe_create_client_session(uintptr_t lib_hndl, - uint64_t client_id, - uintptr_t user_ctx); +extern enum mgmt_result +mgmt_fe_create_client_session(struct mgmt_fe_client *client, uint64_t client_id, + uintptr_t user_client); /* * Delete an existing Session for a Frontend Client connection. @@ -187,10 +191,11 @@ extern enum mgmt_result mgmt_fe_create_client_session(uintptr_t lib_hndl, * Unique identifier of client. * * Returns: - * MGMTD_SUCCESS on success, MGMTD_* otherwise. + * 0 on success, otherwise msg_conn_send_msg() return values. */ -extern enum mgmt_result mgmt_fe_destroy_client_session(uintptr_t lib_hndl, - uint64_t client_id); +extern enum mgmt_result +mgmt_fe_destroy_client_session(struct mgmt_fe_client *client, + uint64_t client_id); /* * Send UN/LOCK_DS_REQ to MGMTD for a specific Datastore DS. @@ -211,11 +216,11 @@ extern enum mgmt_result mgmt_fe_destroy_client_session(uintptr_t lib_hndl, * TRUE for lock request, FALSE for unlock request. * * Returns: - * MGMTD_SUCCESS on success, MGMTD_* otherwise. + * 0 on success, otherwise msg_conn_send_msg() return values. */ -extern enum mgmt_result mgmt_fe_lock_ds(uintptr_t lib_hndl, uint64_t session_id, - uint64_t req_id, - Mgmtd__DatastoreId ds_id, bool lock_ds); +extern int mgmt_fe_send_lockds_req(struct mgmt_fe_client *client, + uint64_t session_id, uint64_t req_id, + Mgmtd__DatastoreId ds_id, bool lock_ds); /* * Send SET_CONFIG_REQ to MGMTD for one or more config data(s). @@ -245,13 +250,15 @@ extern enum mgmt_result mgmt_fe_lock_ds(uintptr_t lib_hndl, uint64_t session_id, * Destination Datastore ID where data needs to be set. * * Returns: - * MGMTD_SUCCESS on success, MGMTD_* otherwise. + * 0 on success, otherwise msg_conn_send_msg() return values. */ -extern enum mgmt_result -mgmt_fe_set_config_data(uintptr_t lib_hndl, uint64_t session_id, - uint64_t req_id, Mgmtd__DatastoreId ds_id, - Mgmtd__YangCfgDataReq **config_req, int num_req, - bool implicit_commit, Mgmtd__DatastoreId dst_ds_id); + +extern int mgmt_fe_send_setcfg_req(struct mgmt_fe_client *client, + uint64_t session_id, uint64_t req_id, + Mgmtd__DatastoreId ds_id, + Mgmtd__YangCfgDataReq **config_req, + int num_req, bool implicit_commit, + Mgmtd__DatastoreId dst_ds_id); /* * Send SET_COMMMIT_REQ to MGMTD for one or more config data(s). @@ -278,13 +285,13 @@ mgmt_fe_set_config_data(uintptr_t lib_hndl, uint64_t session_id, * TRUE if need to restore Src DS back to Dest DS, FALSE otherwise. * * Returns: - * MGMTD_SUCCESS on success, MGMTD_* otherwise. + * 0 on success, otherwise msg_conn_send_msg() return values. */ -extern enum mgmt_result -mgmt_fe_commit_config_data(uintptr_t lib_hndl, uint64_t session_id, - uint64_t req_id, Mgmtd__DatastoreId src_ds_id, - Mgmtd__DatastoreId dst_ds_id, bool validate_only, - bool abort); +extern int mgmt_fe_send_commitcfg_req(struct mgmt_fe_client *client, + uint64_t session_id, uint64_t req_id, + Mgmtd__DatastoreId src_ds_id, + Mgmtd__DatastoreId dst_ds_id, + bool validate_only, bool abort); /* * Send GET_CONFIG_REQ to MGMTD for one or more config data item(s). @@ -308,12 +315,13 @@ mgmt_fe_commit_config_data(uintptr_t lib_hndl, uint64_t session_id, * Number of get config requests. * * Returns: - * MGMTD_SUCCESS on success, MGMTD_* otherwise. + * 0 on success, otherwise msg_conn_send_msg() return values. */ -extern enum mgmt_result -mgmt_fe_get_config_data(uintptr_t lib_hndl, uint64_t session_id, - uint64_t req_id, Mgmtd__DatastoreId ds_id, - Mgmtd__YangGetDataReq **data_req, int num_reqs); +extern int mgmt_fe_send_getcfg_req(struct mgmt_fe_client *client, + uint64_t session_id, uint64_t req_id, + Mgmtd__DatastoreId ds_id, + Mgmtd__YangGetDataReq **data_req, + int num_reqs); /* * Send GET_DATA_REQ to MGMTD for one or more data item(s). @@ -321,11 +329,11 @@ mgmt_fe_get_config_data(uintptr_t lib_hndl, uint64_t session_id, * Similar to get config request but supports getting data * from operational ds aka backend clients directly. */ -extern enum mgmt_result mgmt_fe_get_data(uintptr_t lib_hndl, - uint64_t session_id, uint64_t req_id, - Mgmtd__DatastoreId ds_id, - Mgmtd__YangGetDataReq **data_req, - int num_reqs); +extern int mgmt_fe_send_getdata_req(struct mgmt_fe_client *client, + uint64_t session_id, uint64_t req_id, + Mgmtd__DatastoreId ds_id, + Mgmtd__YangGetDataReq **data_req, + int num_reqs); /* * Send NOTIFY_REGISTER_REQ to MGMTD daemon. @@ -352,23 +360,24 @@ extern enum mgmt_result mgmt_fe_get_data(uintptr_t lib_hndl, * Number of data requests. * * Returns: - * MGMTD_SUCCESS on success, MGMTD_* otherwise. + * 0 on success, otherwise msg_conn_send_msg() return values. */ -extern enum mgmt_result -mgmt_fe_register_yang_notify(uintptr_t lib_hndl, uint64_t session_id, - uint64_t req_id, Mgmtd__DatastoreId ds_id, - bool register_req, Mgmtd__YangDataXPath **data_req, - int num_reqs); +extern int mgmt_fe_send_regnotify_req(struct mgmt_fe_client *client, + uint64_t session_id, uint64_t req_id, + Mgmtd__DatastoreId ds_id, + bool register_req, + Mgmtd__YangDataXPath **data_req, + int num_reqs); /* * Destroy library and cleanup everything. */ -extern void mgmt_fe_client_lib_destroy(void); +extern void mgmt_fe_client_destroy(struct mgmt_fe_client *client); /* * Get count of open sessions. */ -extern uint mgmt_fe_client_session_count(uintptr_t lib_hndl); +extern uint mgmt_fe_client_session_count(struct mgmt_fe_client *client); #ifdef __cplusplus } @@ -68,7 +68,7 @@ enum vty_event { struct nb_config *vty_mgmt_candidate_config; -static uintptr_t mgmt_lib_hndl; +static struct mgmt_fe_client *mgmt_fe_client; static bool mgmt_fe_connected; static bool mgmt_candidate_ds_wr_locked; static uint64_t mgmt_client_id_next; @@ -1640,12 +1640,12 @@ struct vty *vty_new(void) new->max = VTY_BUFSIZ; new->pass_fd = -1; - if (mgmt_lib_hndl) { + if (mgmt_fe_client) { if (!mgmt_client_id_next) mgmt_client_id_next++; new->mgmt_client_id = mgmt_client_id_next++; if (mgmt_fe_create_client_session( - mgmt_lib_hndl, new->mgmt_client_id, + mgmt_fe_client, new->mgmt_client_id, (uintptr_t) new) != MGMTD_SUCCESS) zlog_err( "Failed to open a MGMTD Frontend session for VTY session %p!!", @@ -2419,8 +2419,8 @@ void vty_close(struct vty *vty) vty->status = VTY_CLOSE; - if (mgmt_lib_hndl && vty->mgmt_session_id) { - mgmt_fe_destroy_client_session(mgmt_lib_hndl, + if (mgmt_fe_client && vty->mgmt_session_id) { + mgmt_fe_destroy_client_session(mgmt_fe_client, vty->mgmt_client_id); vty->mgmt_session_id = 0; } @@ -3391,8 +3391,8 @@ void vty_init_vtysh(void) * functionality linked into it. This design choice was taken for efficiency. */ -static void vty_mgmt_server_connected(uintptr_t lib_hndl, uintptr_t usr_data, - bool connected) +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: " : "", @@ -3403,7 +3403,7 @@ static void vty_mgmt_server_connected(uintptr_t lib_hndl, uintptr_t usr_data, * The fe client library will delete all session on disconnect before * calling us. */ - assert(mgmt_fe_client_session_count(lib_hndl) == 0); + assert(mgmt_fe_client_session_count(client) == 0); mgmt_fe_connected = connected; @@ -3417,10 +3417,10 @@ static void vty_mgmt_server_connected(uintptr_t lib_hndl, uintptr_t usr_data, /* * A session has successfully been created for a vty. */ -static void vty_mgmt_session_notify(uintptr_t lib_hndl, uintptr_t usr_data, - uint64_t client_id, bool create, - bool success, uintptr_t session_id, - uintptr_t session_ctx) +static void vty_mgmt_session_notify(struct mgmt_fe_client *client, + uintptr_t usr_data, uint64_t client_id, + bool create, bool success, + uintptr_t session_id, uintptr_t session_ctx) { struct vty *vty; @@ -3444,8 +3444,9 @@ static void vty_mgmt_session_notify(uintptr_t lib_hndl, uintptr_t usr_data, } } -static void vty_mgmt_ds_lock_notified(uintptr_t lib_hndl, uintptr_t usr_data, - uint64_t client_id, uintptr_t session_id, +static void vty_mgmt_ds_lock_notified(struct mgmt_fe_client *client, + uintptr_t usr_data, uint64_t client_id, + uintptr_t session_id, uintptr_t session_ctx, uint64_t req_id, bool lock_ds, bool success, Mgmtd__DatastoreId ds_id, @@ -3469,7 +3470,7 @@ static void vty_mgmt_ds_lock_notified(uintptr_t lib_hndl, uintptr_t usr_data, } static void vty_mgmt_set_config_result_notified( - uintptr_t lib_hndl, uintptr_t usr_data, uint64_t client_id, + struct mgmt_fe_client *client, uintptr_t usr_data, uint64_t client_id, uintptr_t session_id, uintptr_t session_ctx, uint64_t req_id, bool success, Mgmtd__DatastoreId ds_id, char *errmsg_if_any) { @@ -3493,7 +3494,7 @@ static void vty_mgmt_set_config_result_notified( } static void vty_mgmt_commit_config_result_notified( - uintptr_t lib_hndl, uintptr_t usr_data, uint64_t client_id, + struct mgmt_fe_client *client, uintptr_t usr_data, uint64_t client_id, uintptr_t session_id, uintptr_t session_ctx, uint64_t req_id, bool success, Mgmtd__DatastoreId src_ds_id, Mgmtd__DatastoreId dst_ds_id, bool validate_only, char *errmsg_if_any) @@ -3520,8 +3521,8 @@ static void vty_mgmt_commit_config_result_notified( vty_mgmt_resume_response(vty, success); } -static enum mgmt_result vty_mgmt_get_data_result_notified( - uintptr_t lib_hndl, uintptr_t usr_data, uint64_t client_id, +static int vty_mgmt_get_data_result_notified( + struct mgmt_fe_client *client, uintptr_t usr_data, uint64_t client_id, uintptr_t session_id, uintptr_t session_ctx, uint64_t req_id, bool success, Mgmtd__DatastoreId ds_id, Mgmtd__YangData **yang_data, size_t num_data, int next_key, char *errmsg_if_any) @@ -3538,7 +3539,7 @@ static enum mgmt_result vty_mgmt_get_data_result_notified( vty_out(vty, "ERROR: GET_DATA request failed, Error: %s\n", errmsg_if_any ? errmsg_if_any : "Unknown"); vty_mgmt_resume_response(vty, success); - return MGMTD_INTERNAL_ERROR; + return -1; } MGMTD_FE_CLIENT_DBG("GET_DATA request succeeded, client 0x%" PRIx64 @@ -3559,10 +3560,10 @@ static enum mgmt_result vty_mgmt_get_data_result_notified( vty_mgmt_resume_response(vty, success); } - return MGMTD_SUCCESS; + return 0; } -static struct mgmt_fe_client_params client_params = { +static struct mgmt_fe_client_cbs mgmt_cbs = { .client_connect_notify = vty_mgmt_server_connected, .client_session_notify = vty_mgmt_session_notify, .lock_ds_notify = vty_mgmt_ds_lock_notified, @@ -3573,21 +3574,19 @@ static struct mgmt_fe_client_params client_params = { void vty_init_mgmt_fe(void) { - if (!vty_master) { - zlog_err("Always call vty_mgmt_init_fe() after vty_init()!!"); - return; - } + char name[40]; - assert(!mgmt_lib_hndl); - snprintf(client_params.name, sizeof(client_params.name), "%s-%lld", - frr_get_progname(), (long long)getpid()); - mgmt_lib_hndl = mgmt_fe_client_lib_init(&client_params, vty_master); - assert(mgmt_lib_hndl); + assert(vty_master); + assert(!mgmt_fe_client); + snprintf(name, sizeof(name), "vty-%s-%ld", frr_get_progname(), + (long)getpid()); + mgmt_fe_client = mgmt_fe_client_create(name, &mgmt_cbs, 0, vty_master); + assert(mgmt_fe_client); } bool vty_mgmt_fe_enabled(void) { - return mgmt_lib_hndl && mgmt_fe_connected; + return mgmt_fe_client && mgmt_fe_connected; } bool vty_mgmt_should_process_cli_apply_changes(struct vty *vty) @@ -3598,13 +3597,11 @@ bool vty_mgmt_should_process_cli_apply_changes(struct vty *vty) int vty_mgmt_send_lockds_req(struct vty *vty, Mgmtd__DatastoreId ds_id, bool lock) { - enum mgmt_result ret; - - if (mgmt_lib_hndl && vty->mgmt_session_id) { + if (mgmt_fe_client && vty->mgmt_session_id) { vty->mgmt_req_id++; - ret = mgmt_fe_lock_ds(mgmt_lib_hndl, vty->mgmt_session_id, - vty->mgmt_req_id, ds_id, lock); - if (ret != MGMTD_SUCCESS) { + if (mgmt_fe_send_lockds_req(mgmt_fe_client, + vty->mgmt_session_id, + vty->mgmt_req_id, ds_id, lock)) { zlog_err("Failed sending %sLOCK-DS-REQ req-id %" PRIu64, lock ? "" : "UN", vty->mgmt_req_id); vty_out(vty, "Failed to send %sLOCK-DS-REQ to MGMTD!\n", @@ -3641,7 +3638,7 @@ int vty_mgmt_send_config_data(struct vty *vty) } - if (mgmt_lib_hndl && vty->mgmt_client_id && !vty->mgmt_session_id) { + if (mgmt_fe_client && vty->mgmt_client_id && !vty->mgmt_session_id) { /* * We are connected to mgmtd but we do not yet have an * established session. this means we need to send any changes @@ -3652,7 +3649,7 @@ int vty_mgmt_send_config_data(struct vty *vty) return 0; } - if (mgmt_lib_hndl && vty->mgmt_session_id) { + if (mgmt_fe_client && vty->mgmt_session_id) { cnt = 0; for (indx = 0; indx < vty->num_cfg_changes; indx++) { mgmt_yang_data_init(&cfg_data[cnt]); @@ -3701,8 +3698,8 @@ int vty_mgmt_send_config_data(struct vty *vty) vty->mgmt_req_id++; implicit_commit = vty_needs_implicit_commit(vty); - if (cnt && mgmt_fe_set_config_data( - mgmt_lib_hndl, vty->mgmt_session_id, + if (cnt && mgmt_fe_send_setcfg_req( + mgmt_fe_client, vty->mgmt_session_id, vty->mgmt_req_id, MGMTD_DS_CANDIDATE, cfgreq, cnt, implicit_commit, MGMTD_DS_RUNNING) != MGMTD_SUCCESS) { @@ -3720,15 +3717,12 @@ int vty_mgmt_send_config_data(struct vty *vty) int vty_mgmt_send_commit_config(struct vty *vty, bool validate_only, bool abort) { - enum mgmt_result ret; - - if (mgmt_lib_hndl && vty->mgmt_session_id) { + if (mgmt_fe_client && vty->mgmt_session_id) { vty->mgmt_req_id++; - ret = mgmt_fe_commit_config_data( - mgmt_lib_hndl, vty->mgmt_session_id, vty->mgmt_req_id, - MGMTD_DS_CANDIDATE, MGMTD_DS_RUNNING, validate_only, - abort); - if (ret != MGMTD_SUCCESS) { + if (mgmt_fe_send_commitcfg_req( + mgmt_fe_client, vty->mgmt_session_id, + vty->mgmt_req_id, MGMTD_DS_CANDIDATE, + MGMTD_DS_RUNNING, validate_only, abort)) { zlog_err("Failed sending COMMIT-REQ req-id %" PRIu64, vty->mgmt_req_id); vty_out(vty, "Failed to send COMMIT-REQ to MGMTD!\n"); @@ -3745,7 +3739,6 @@ int vty_mgmt_send_commit_config(struct vty *vty, bool validate_only, bool abort) int vty_mgmt_send_get_config(struct vty *vty, Mgmtd__DatastoreId datastore, const char **xpath_list, int num_req) { - enum mgmt_result ret; Mgmtd__YangData yang_data[VTY_MAXCFGCHANGES]; Mgmtd__YangGetDataReq get_req[VTY_MAXCFGCHANGES]; Mgmtd__YangGetDataReq *getreq[VTY_MAXCFGCHANGES]; @@ -3762,11 +3755,9 @@ int vty_mgmt_send_get_config(struct vty *vty, Mgmtd__DatastoreId datastore, get_req[i].data = &yang_data[i]; getreq[i] = &get_req[i]; } - ret = mgmt_fe_get_config_data(mgmt_lib_hndl, vty->mgmt_session_id, - vty->mgmt_req_id, datastore, getreq, - num_req); - - if (ret != MGMTD_SUCCESS) { + if (mgmt_fe_send_getcfg_req(mgmt_fe_client, vty->mgmt_session_id, + vty->mgmt_req_id, datastore, getreq, + num_req)) { zlog_err( "Failed to send GET-CONFIG to MGMTD for req-id %" PRIu64 ".", @@ -3783,7 +3774,6 @@ int vty_mgmt_send_get_config(struct vty *vty, Mgmtd__DatastoreId datastore, int vty_mgmt_send_get_data(struct vty *vty, Mgmtd__DatastoreId datastore, const char **xpath_list, int num_req) { - enum mgmt_result ret; Mgmtd__YangData yang_data[VTY_MAXCFGCHANGES]; Mgmtd__YangGetDataReq get_req[VTY_MAXCFGCHANGES]; Mgmtd__YangGetDataReq *getreq[VTY_MAXCFGCHANGES]; @@ -3800,10 +3790,9 @@ int vty_mgmt_send_get_data(struct vty *vty, Mgmtd__DatastoreId datastore, get_req[i].data = &yang_data[i]; getreq[i] = &get_req[i]; } - ret = mgmt_fe_get_data(mgmt_lib_hndl, vty->mgmt_session_id, - vty->mgmt_req_id, datastore, getreq, num_req); - - if (ret != MGMTD_SUCCESS) { + if (mgmt_fe_send_getdata_req(mgmt_fe_client, vty->mgmt_session_id, + vty->mgmt_req_id, datastore, getreq, + num_req)) { zlog_err("Failed to send GET-DATA to MGMTD for req-id %" PRIu64 ".", vty->mgmt_req_id); @@ -3862,9 +3851,9 @@ void vty_terminate(void) { struct vty *vty; - if (mgmt_lib_hndl) { - mgmt_fe_client_lib_destroy(); - mgmt_lib_hndl = 0; + if (mgmt_fe_client) { + mgmt_fe_client_destroy(mgmt_fe_client); + mgmt_fe_client = 0; } memset(vty_cwd, 0x00, sizeof(vty_cwd)); |