lib: mgmtd: remove abstraction layer and other cleanup

Code is no longer using a global FE "client context", and instead
creates client objects, rename the structure and it's uses to reflect this.

Remove an obfuscating abstraction layer whose existence was entirely
based on using a uintptr_t rather than a pointer to an declared-only struct.

Change multi-duty "params" structure into a single duty callbacks one.

Remove unsupported API code.

Signed-off-by: Christian Hopps <chopps@labn.net>
This commit is contained in:
Christian Hopps 2023-06-04 15:43:22 -04:00
parent ff82184fae
commit 7aecb8639c
3 changed files with 139 additions and 344 deletions

View File

@ -28,6 +28,8 @@
#define MGMTD_DBG_BE_CLIENT_CHECK() \ #define MGMTD_DBG_BE_CLIENT_CHECK() \
DEBUG_MODE_CHECK(&mgmt_dbg_be_client, DEBUG_MODE_ALL) DEBUG_MODE_CHECK(&mgmt_dbg_be_client, DEBUG_MODE_ALL)
DEFINE_MTYPE_STATIC(LIB, MGMTD_BE_CLIENT, "backend client");
DEFINE_MTYPE_STATIC(LIB, MGMTD_BE_CLIENT_NAME, "backend client name");
DEFINE_MTYPE_STATIC(LIB, MGMTD_BE_BATCH, "backend transaction batch data"); DEFINE_MTYPE_STATIC(LIB, MGMTD_BE_BATCH, "backend transaction batch data");
DEFINE_MTYPE_STATIC(LIB, MGMTD_BE_TXN, "backend transaction data"); DEFINE_MTYPE_STATIC(LIB, MGMTD_BE_TXN, "backend transaction data");
@ -70,8 +72,6 @@ struct mgmt_be_batch_ctx {
#define MGMTD_BE_TXN_FLAGS_CFG_APPLIED (1U << 1) #define MGMTD_BE_TXN_FLAGS_CFG_APPLIED (1U << 1)
DECLARE_LIST(mgmt_be_batches, struct mgmt_be_batch_ctx, list_linkage); DECLARE_LIST(mgmt_be_batches, struct mgmt_be_batch_ctx, list_linkage);
struct mgmt_be_client_ctx;
PREDECL_LIST(mgmt_be_txns); PREDECL_LIST(mgmt_be_txns);
struct mgmt_be_txn_ctx { struct mgmt_be_txn_ctx {
/* Txn-Id as assigned by MGMTD */ /* Txn-Id as assigned by MGMTD */
@ -79,7 +79,7 @@ struct mgmt_be_txn_ctx {
uint32_t flags; uint32_t flags;
struct mgmt_be_client_txn_ctx client_data; struct mgmt_be_client_txn_ctx client_data;
struct mgmt_be_client_ctx *client_ctx; struct mgmt_be_client *client;
/* List of batches belonging to this transaction */ /* List of batches belonging to this transaction */
struct mgmt_be_batches_head cfg_batches; struct mgmt_be_batches_head cfg_batches;
@ -100,9 +100,11 @@ DECLARE_LIST(mgmt_be_txns, struct mgmt_be_txn_ctx, list_linkage);
#define FOREACH_BE_APPLY_BATCH_IN_LIST(txn, batch) \ #define FOREACH_BE_APPLY_BATCH_IN_LIST(txn, batch) \
frr_each_safe (mgmt_be_batches, &(txn)->apply_cfgs, (batch)) frr_each_safe (mgmt_be_batches, &(txn)->apply_cfgs, (batch))
struct mgmt_be_client_ctx { struct mgmt_be_client {
struct msg_client client; struct msg_client client;
char *name;
struct nb_config *candidate_config; struct nb_config *candidate_config;
struct nb_config *running_config; struct nb_config *running_config;
@ -114,7 +116,9 @@ struct mgmt_be_client_ctx {
unsigned long avg_apply_nb_cfg_tm; unsigned long avg_apply_nb_cfg_tm;
struct mgmt_be_txns_head txn_head; struct mgmt_be_txns_head txn_head;
struct mgmt_be_client_params client_params;
struct mgmt_be_client_cbs cbs;
uintptr_t user_data;
}; };
#define FOREACH_BE_TXN_IN_LIST(client_ctx, txn) \ #define FOREACH_BE_TXN_IN_LIST(client_ctx, txn) \
@ -122,9 +126,6 @@ struct mgmt_be_client_ctx {
struct debug mgmt_dbg_be_client = {0, "Management backend client operations"}; struct debug mgmt_dbg_be_client = {0, "Management backend client operations"};
static struct mgmt_be_client_ctx mgmt_be_client_ctx = {
.client = {.conn = {.fd = -1}}};
const char *mgmt_be_client_names[MGMTD_BE_CLIENT_ID_MAX + 1] = { const char *mgmt_be_client_names[MGMTD_BE_CLIENT_ID_MAX + 1] = {
#ifdef HAVE_STATICD #ifdef HAVE_STATICD
[MGMTD_BE_CLIENT_ID_STATICD] = "staticd", [MGMTD_BE_CLIENT_ID_STATICD] = "staticd",
@ -132,7 +133,7 @@ const char *mgmt_be_client_names[MGMTD_BE_CLIENT_ID_MAX + 1] = {
[MGMTD_BE_CLIENT_ID_MAX] = "Unknown/Invalid", [MGMTD_BE_CLIENT_ID_MAX] = "Unknown/Invalid",
}; };
static int mgmt_be_client_send_msg(struct mgmt_be_client_ctx *client_ctx, static int mgmt_be_client_send_msg(struct mgmt_be_client *client_ctx,
Mgmtd__BeMessage *be_msg) Mgmtd__BeMessage *be_msg)
{ {
return msg_conn_send_msg( return msg_conn_send_msg(
@ -216,8 +217,7 @@ static void mgmt_be_cleanup_all_batches(struct mgmt_be_txn_ctx *txn)
} }
static struct mgmt_be_txn_ctx * static struct mgmt_be_txn_ctx *
mgmt_be_find_txn_by_id(struct mgmt_be_client_ctx *client_ctx, mgmt_be_find_txn_by_id(struct mgmt_be_client *client_ctx, uint64_t txn_id)
uint64_t txn_id)
{ {
struct mgmt_be_txn_ctx *txn = NULL; struct mgmt_be_txn_ctx *txn = NULL;
@ -230,8 +230,7 @@ mgmt_be_find_txn_by_id(struct mgmt_be_client_ctx *client_ctx,
} }
static struct mgmt_be_txn_ctx * static struct mgmt_be_txn_ctx *
mgmt_be_txn_create(struct mgmt_be_client_ctx *client_ctx, mgmt_be_txn_create(struct mgmt_be_client *client_ctx, uint64_t txn_id)
uint64_t txn_id)
{ {
struct mgmt_be_txn_ctx *txn = NULL; struct mgmt_be_txn_ctx *txn = NULL;
@ -242,7 +241,7 @@ mgmt_be_txn_create(struct mgmt_be_client_ctx *client_ctx,
assert(txn); assert(txn);
txn->txn_id = txn_id; txn->txn_id = txn_id;
txn->client_ctx = client_ctx; txn->client = client_ctx;
mgmt_be_batches_init(&txn->cfg_batches); mgmt_be_batches_init(&txn->cfg_batches);
mgmt_be_batches_init(&txn->apply_cfgs); mgmt_be_batches_init(&txn->apply_cfgs);
mgmt_be_txns_add_tail(&client_ctx->txn_head, txn); mgmt_be_txns_add_tail(&client_ctx->txn_head, txn);
@ -253,8 +252,8 @@ mgmt_be_txn_create(struct mgmt_be_client_ctx *client_ctx,
return txn; return txn;
} }
static void mgmt_be_txn_delete(struct mgmt_be_client_ctx *client_ctx, static void mgmt_be_txn_delete(struct mgmt_be_client *client_ctx,
struct mgmt_be_txn_ctx **txn) struct mgmt_be_txn_ctx **txn)
{ {
char err_msg[] = "MGMT Transaction Delete"; char err_msg[] = "MGMT Transaction Delete";
@ -274,12 +273,10 @@ static void mgmt_be_txn_delete(struct mgmt_be_client_ctx *client_ctx,
* CFGDATA_CREATE_REQs. But first notify the client * CFGDATA_CREATE_REQs. But first notify the client
* about the transaction delete. * about the transaction delete.
*/ */
if (client_ctx->client_params.txn_notify) if (client_ctx->cbs.txn_notify)
(void)(*client_ctx->client_params (void)(*client_ctx->cbs.txn_notify)(client_ctx,
.txn_notify)( client_ctx->user_data,
(uintptr_t)client_ctx, &(*txn)->client_data, true);
client_ctx->client_params.user_data,
&(*txn)->client_data, true);
mgmt_be_cleanup_all_batches(*txn); mgmt_be_cleanup_all_batches(*txn);
if ((*txn)->nb_txn) if ((*txn)->nb_txn)
@ -290,8 +287,7 @@ static void mgmt_be_txn_delete(struct mgmt_be_client_ctx *client_ctx,
*txn = NULL; *txn = NULL;
} }
static void static void mgmt_be_cleanup_all_txns(struct mgmt_be_client *client_ctx)
mgmt_be_cleanup_all_txns(struct mgmt_be_client_ctx *client_ctx)
{ {
struct mgmt_be_txn_ctx *txn = NULL; struct mgmt_be_txn_ctx *txn = NULL;
@ -300,9 +296,8 @@ mgmt_be_cleanup_all_txns(struct mgmt_be_client_ctx *client_ctx)
} }
} }
static int mgmt_be_send_txn_reply(struct mgmt_be_client_ctx *client_ctx, static int mgmt_be_send_txn_reply(struct mgmt_be_client *client_ctx,
uint64_t txn_id, bool create, uint64_t txn_id, bool create, bool success)
bool success)
{ {
Mgmtd__BeMessage be_msg; Mgmtd__BeMessage be_msg;
Mgmtd__BeTxnReply txn_reply; Mgmtd__BeTxnReply txn_reply;
@ -321,8 +316,8 @@ static int mgmt_be_send_txn_reply(struct mgmt_be_client_ctx *client_ctx,
return mgmt_be_client_send_msg(client_ctx, &be_msg); return mgmt_be_client_send_msg(client_ctx, &be_msg);
} }
static int mgmt_be_process_txn_req(struct mgmt_be_client_ctx *client_ctx, static int mgmt_be_process_txn_req(struct mgmt_be_client *client_ctx,
uint64_t txn_id, bool create) uint64_t txn_id, bool create)
{ {
struct mgmt_be_txn_ctx *txn; struct mgmt_be_txn_ctx *txn;
@ -342,11 +337,9 @@ static int mgmt_be_process_txn_req(struct mgmt_be_client_ctx *client_ctx,
MGMTD_BE_CLIENT_DBG("Created new txn-id %" PRIu64, txn_id); MGMTD_BE_CLIENT_DBG("Created new txn-id %" PRIu64, txn_id);
txn = mgmt_be_txn_create(client_ctx, txn_id); txn = mgmt_be_txn_create(client_ctx, txn_id);
if (client_ctx->client_params.txn_notify) if (client_ctx->cbs.txn_notify)
(void)(*client_ctx->client_params (void)(*client_ctx->cbs.txn_notify)(
.txn_notify)( client_ctx, client_ctx->user_data,
(uintptr_t)client_ctx,
client_ctx->client_params.user_data,
&txn->client_data, false); &txn->client_data, false);
} else { } else {
if (!txn) { if (!txn) {
@ -368,10 +361,10 @@ static int mgmt_be_process_txn_req(struct mgmt_be_client_ctx *client_ctx,
return 0; return 0;
} }
static int static int mgmt_be_send_cfgdata_create_reply(struct mgmt_be_client *client_ctx,
mgmt_be_send_cfgdata_create_reply(struct mgmt_be_client_ctx *client_ctx, uint64_t txn_id, uint64_t batch_id,
uint64_t txn_id, uint64_t batch_id, bool success,
bool success, const char *error_if_any) const char *error_if_any)
{ {
Mgmtd__BeMessage be_msg; Mgmtd__BeMessage be_msg;
Mgmtd__BeCfgDataCreateReply cfgdata_reply; Mgmtd__BeCfgDataCreateReply cfgdata_reply;
@ -398,7 +391,7 @@ static void mgmt_be_txn_cfg_abort(struct mgmt_be_txn_ctx *txn)
{ {
char errmsg[BUFSIZ] = {0}; char errmsg[BUFSIZ] = {0};
assert(txn && txn->client_ctx); assert(txn && txn->client);
if (txn->nb_txn) { if (txn->nb_txn) {
MGMTD_BE_CLIENT_ERR( MGMTD_BE_CLIENT_ERR(
"Aborting configs after prep for txn-id: %" PRIu64, "Aborting configs after prep for txn-id: %" PRIu64,
@ -416,13 +409,13 @@ static void mgmt_be_txn_cfg_abort(struct mgmt_be_txn_ctx *txn)
MGMTD_BE_CLIENT_DBG( MGMTD_BE_CLIENT_DBG(
"Reset candidate configurations after abort of txn-id: %" PRIu64, "Reset candidate configurations after abort of txn-id: %" PRIu64,
txn->txn_id); txn->txn_id);
nb_config_replace(txn->client_ctx->candidate_config, nb_config_replace(txn->client->candidate_config,
txn->client_ctx->running_config, true); txn->client->running_config, true);
} }
static int mgmt_be_txn_cfg_prepare(struct mgmt_be_txn_ctx *txn) static int mgmt_be_txn_cfg_prepare(struct mgmt_be_txn_ctx *txn)
{ {
struct mgmt_be_client_ctx *client_ctx; struct mgmt_be_client *client_ctx;
struct mgmt_be_txn_req *txn_req = NULL; struct mgmt_be_txn_req *txn_req = NULL;
struct nb_context nb_ctx = {0}; struct nb_context nb_ctx = {0};
struct timeval edit_nb_cfg_start; struct timeval edit_nb_cfg_start;
@ -437,15 +430,15 @@ static int mgmt_be_txn_cfg_prepare(struct mgmt_be_txn_ctx *txn)
size_t num_processed; size_t num_processed;
int err; int err;
assert(txn && txn->client_ctx); assert(txn && txn->client);
client_ctx = txn->client_ctx; client_ctx = txn->client;
num_processed = 0; num_processed = 0;
FOREACH_BE_TXN_BATCH_IN_LIST (txn, batch) { FOREACH_BE_TXN_BATCH_IN_LIST (txn, batch) {
txn_req = &batch->txn_req; txn_req = &batch->txn_req;
error = false; error = false;
nb_ctx.client = NB_CLIENT_CLI; nb_ctx.client = NB_CLIENT_CLI;
nb_ctx.user = (void *)client_ctx->client_params.user_data; nb_ctx.user = (void *)client_ctx->user_data;
if (!txn->nb_txn) { if (!txn->nb_txn) {
/* /*
@ -492,7 +485,7 @@ static int mgmt_be_txn_cfg_prepare(struct mgmt_be_txn_ctx *txn)
* Now prepare all the batches we have applied in one go. * Now prepare all the batches we have applied in one go.
*/ */
nb_ctx.client = NB_CLIENT_CLI; nb_ctx.client = NB_CLIENT_CLI;
nb_ctx.user = (void *)client_ctx->client_params.user_data; nb_ctx.user = (void *)client_ctx->user_data;
gettimeofday(&prep_nb_cfg_start, NULL); gettimeofday(&prep_nb_cfg_start, NULL);
err = nb_candidate_commit_prepare(nb_ctx, client_ctx->candidate_config, err = nb_candidate_commit_prepare(nb_ctx, client_ctx->candidate_config,
@ -556,12 +549,11 @@ static int mgmt_be_txn_cfg_prepare(struct mgmt_be_txn_ctx *txn)
/* /*
* Process all CFG_DATA_REQs received so far and prepare them all in one go. * Process all CFG_DATA_REQs received so far and prepare them all in one go.
*/ */
static int static int mgmt_be_update_setcfg_in_batch(struct mgmt_be_client *client_ctx,
mgmt_be_update_setcfg_in_batch(struct mgmt_be_client_ctx *client_ctx, struct mgmt_be_txn_ctx *txn,
struct mgmt_be_txn_ctx *txn, uint64_t batch_id,
uint64_t batch_id, Mgmtd__YangCfgDataReq *cfg_req[],
Mgmtd__YangCfgDataReq * cfg_req[], int num_req)
int num_req)
{ {
struct mgmt_be_batch_ctx *batch = NULL; struct mgmt_be_batch_ctx *batch = NULL;
struct mgmt_be_txn_req *txn_req = NULL; struct mgmt_be_txn_req *txn_req = NULL;
@ -611,11 +603,10 @@ mgmt_be_update_setcfg_in_batch(struct mgmt_be_client_ctx *client_ctx,
return 0; return 0;
} }
static int static int mgmt_be_process_cfgdata_req(struct mgmt_be_client *client_ctx,
mgmt_be_process_cfgdata_req(struct mgmt_be_client_ctx *client_ctx, uint64_t txn_id, uint64_t batch_id,
uint64_t txn_id, uint64_t batch_id, Mgmtd__YangCfgDataReq *cfg_req[],
Mgmtd__YangCfgDataReq * cfg_req[], int num_req, int num_req, bool end_of_data)
bool end_of_data)
{ {
struct mgmt_be_txn_ctx *txn; struct mgmt_be_txn_ctx *txn;
@ -640,10 +631,10 @@ mgmt_be_process_cfgdata_req(struct mgmt_be_client_ctx *client_ctx,
return 0; return 0;
} }
static int mgmt_be_send_apply_reply(struct mgmt_be_client_ctx *client_ctx, static int mgmt_be_send_apply_reply(struct mgmt_be_client *client_ctx,
uint64_t txn_id, uint64_t batch_ids[], uint64_t txn_id, uint64_t batch_ids[],
size_t num_batch_ids, bool success, size_t num_batch_ids, bool success,
const char *error_if_any) const char *error_if_any)
{ {
Mgmtd__BeMessage be_msg; Mgmtd__BeMessage be_msg;
Mgmtd__BeCfgDataApplyReply apply_reply; Mgmtd__BeCfgDataApplyReply apply_reply;
@ -673,7 +664,7 @@ static int mgmt_be_send_apply_reply(struct mgmt_be_client_ctx *client_ctx,
static int mgmt_be_txn_proc_cfgapply(struct mgmt_be_txn_ctx *txn) static int mgmt_be_txn_proc_cfgapply(struct mgmt_be_txn_ctx *txn)
{ {
struct mgmt_be_client_ctx *client_ctx; struct mgmt_be_client *client_ctx;
struct timeval apply_nb_cfg_start; struct timeval apply_nb_cfg_start;
struct timeval apply_nb_cfg_end; struct timeval apply_nb_cfg_end;
unsigned long apply_nb_cfg_tm; unsigned long apply_nb_cfg_tm;
@ -682,8 +673,8 @@ static int mgmt_be_txn_proc_cfgapply(struct mgmt_be_txn_ctx *txn)
size_t num_processed; size_t num_processed;
static uint64_t batch_ids[MGMTD_BE_MAX_BATCH_IDS_IN_REQ]; static uint64_t batch_ids[MGMTD_BE_MAX_BATCH_IDS_IN_REQ];
assert(txn && txn->client_ctx); assert(txn && txn->client);
client_ctx = txn->client_ctx; client_ctx = txn->client;
assert(txn->nb_txn); assert(txn->nb_txn);
num_processed = 0; num_processed = 0;
@ -735,9 +726,8 @@ static int mgmt_be_txn_proc_cfgapply(struct mgmt_be_txn_ctx *txn)
return 0; return 0;
} }
static int static int mgmt_be_process_cfg_apply(struct mgmt_be_client *client_ctx,
mgmt_be_process_cfg_apply(struct mgmt_be_client_ctx *client_ctx, uint64_t txn_id)
uint64_t txn_id)
{ {
struct mgmt_be_txn_ctx *txn; struct mgmt_be_txn_ctx *txn;
@ -754,9 +744,8 @@ mgmt_be_process_cfg_apply(struct mgmt_be_client_ctx *client_ctx,
return 0; return 0;
} }
static int static int mgmt_be_client_handle_msg(struct mgmt_be_client *client_ctx,
mgmt_be_client_handle_msg(struct mgmt_be_client_ctx *client_ctx, Mgmtd__BeMessage *be_msg)
Mgmtd__BeMessage *be_msg)
{ {
/* /*
* protobuf-c adds a max size enum with an internal, and changing by * protobuf-c adds a max size enum with an internal, and changing by
@ -833,12 +822,12 @@ mgmt_be_client_handle_msg(struct mgmt_be_client_ctx *client_ctx,
static void mgmt_be_client_process_msg(uint8_t version, uint8_t *data, static void mgmt_be_client_process_msg(uint8_t version, uint8_t *data,
size_t len, struct msg_conn *conn) size_t len, struct msg_conn *conn)
{ {
struct mgmt_be_client_ctx *client_ctx; struct mgmt_be_client *client_ctx;
struct msg_client *client; struct msg_client *client;
Mgmtd__BeMessage *be_msg; Mgmtd__BeMessage *be_msg;
client = container_of(conn, struct msg_client, conn); client = container_of(conn, struct msg_client, conn);
client_ctx = container_of(client, struct mgmt_be_client_ctx, client); client_ctx = container_of(client, struct mgmt_be_client, client);
be_msg = mgmtd__be_message__unpack(NULL, len, data); be_msg = mgmtd__be_message__unpack(NULL, len, data);
if (!be_msg) { if (!be_msg) {
@ -853,17 +842,17 @@ static void mgmt_be_client_process_msg(uint8_t version, uint8_t *data,
mgmtd__be_message__free_unpacked(be_msg, NULL); mgmtd__be_message__free_unpacked(be_msg, NULL);
} }
static int mgmt_be_send_subscr_req(struct mgmt_be_client_ctx *client_ctx, int mgmt_be_send_subscr_req(struct mgmt_be_client *client_ctx,
bool subscr_xpaths, uint16_t num_reg_xpaths, bool subscr_xpaths, int num_xpaths,
char **reg_xpaths) char **reg_xpaths)
{ {
Mgmtd__BeMessage be_msg; Mgmtd__BeMessage be_msg;
Mgmtd__BeSubscribeReq subscr_req; Mgmtd__BeSubscribeReq subscr_req;
mgmtd__be_subscribe_req__init(&subscr_req); mgmtd__be_subscribe_req__init(&subscr_req);
subscr_req.client_name = client_ctx->client_params.name; subscr_req.client_name = client_ctx->name;
subscr_req.n_xpath_reg = num_reg_xpaths; subscr_req.n_xpath_reg = num_xpaths;
if (num_reg_xpaths) if (num_xpaths)
subscr_req.xpath_reg = reg_xpaths; subscr_req.xpath_reg = reg_xpaths;
else else
subscr_req.xpath_reg = NULL; subscr_req.xpath_reg = NULL;
@ -881,24 +870,24 @@ static int mgmt_be_send_subscr_req(struct mgmt_be_client_ctx *client_ctx,
return mgmt_be_client_send_msg(client_ctx, &be_msg); return mgmt_be_client_send_msg(client_ctx, &be_msg);
} }
static int _notify_conenct_disconnect(struct msg_client *client, bool connected) static int _notify_conenct_disconnect(struct msg_client *msg_client,
bool connected)
{ {
struct mgmt_be_client_ctx *client_ctx = struct mgmt_be_client *client =
container_of(client, struct mgmt_be_client_ctx, client); container_of(msg_client, struct mgmt_be_client, client);
int ret; int ret;
if (connected) { if (connected) {
assert(client->conn.fd != -1); assert(msg_client->conn.fd != -1);
ret = mgmt_be_send_subscr_req(client_ctx, false, 0, NULL); ret = mgmt_be_send_subscr_req(client, false, 0, NULL);
if (ret) if (ret)
return ret; return ret;
} }
/* Notify BE client through registered callback (if any) */ /* Notify BE client through registered callback (if any) */
if (client_ctx->client_params.client_connect_notify) if (client->cbs.client_connect_notify)
(void)(*client_ctx->client_params.client_connect_notify)( (void)(*client->cbs.client_connect_notify)(
(uintptr_t)client_ctx, client, client->user_data, connected);
client_ctx->client_params.user_data, connected);
return 0; return 0;
} }
@ -914,6 +903,10 @@ static int mgmt_be_client_notify_disconenct(struct msg_conn *conn)
return _notify_conenct_disconnect(client, false); return _notify_conenct_disconnect(client, false);
} }
/*
* Debug Flags
*/
DEFPY(debug_mgmt_client_be, debug_mgmt_client_be_cmd, DEFPY(debug_mgmt_client_be, debug_mgmt_client_be_cmd,
"[no] debug mgmt client backend", "[no] debug mgmt client backend",
NO_STR DEBUG_STR MGMTD_STR NO_STR DEBUG_STR MGMTD_STR
@ -956,32 +949,33 @@ static struct cmd_node mgmt_dbg_node = {
.config_write = mgmt_debug_be_client_config_write, .config_write = mgmt_debug_be_client_config_write,
}; };
/* struct mgmt_be_client *mgmt_be_client_create(const char *client_name,
* Initialize library and try connecting with MGMTD. struct mgmt_be_client_cbs *cbs,
*/ uintptr_t user_data,
uintptr_t mgmt_be_client_lib_init(struct mgmt_be_client_params *params, struct event_loop *event_loop)
struct event_loop *master_thread)
{ {
/* Don't call twice */ struct mgmt_be_client *client =
assert(!mgmt_be_client_ctx.client.conn.loop); XCALLOC(MTYPE_MGMTD_BE_CLIENT, sizeof(*client));
/* Only call after frr_init() */ /* Only call after frr_init() */
assert(running_config); assert(running_config);
mgmt_be_client_ctx.running_config = running_config; client->name = XSTRDUP(MTYPE_MGMTD_BE_CLIENT_NAME, client_name);
mgmt_be_client_ctx.candidate_config = nb_config_new(NULL); client->running_config = running_config;
mgmt_be_client_ctx.client_params = *params; client->candidate_config = nb_config_new(NULL);
mgmt_be_txns_init(&mgmt_be_client_ctx.txn_head); if (cbs)
msg_client_init(&mgmt_be_client_ctx.client, master_thread, client->cbs = *cbs;
MGMTD_BE_SERVER_PATH, mgmt_be_client_notify_conenct, mgmt_be_txns_init(&client->txn_head);
msg_client_init(&client->client, event_loop, MGMTD_BE_SERVER_PATH,
mgmt_be_client_notify_conenct,
mgmt_be_client_notify_disconenct, mgmt_be_client_notify_disconenct,
mgmt_be_client_process_msg, MGMTD_BE_MAX_NUM_MSG_PROC, mgmt_be_client_process_msg, MGMTD_BE_MAX_NUM_MSG_PROC,
MGMTD_BE_MAX_NUM_MSG_WRITE, MGMTD_BE_MSG_MAX_LEN, false, MGMTD_BE_MAX_NUM_MSG_WRITE, MGMTD_BE_MSG_MAX_LEN, false,
"BE-client", MGMTD_DBG_BE_CLIENT_CHECK()); "BE-client", MGMTD_DBG_BE_CLIENT_CHECK());
MGMTD_BE_CLIENT_DBG("Initialized client '%s'", params->name); MGMTD_BE_CLIENT_DBG("Initialized client '%s'", client_name);
return (uintptr_t)&mgmt_be_client_ctx; return client;
} }
@ -993,86 +987,16 @@ void mgmt_be_client_lib_vty_init(void)
install_element(CONFIG_NODE, &debug_mgmt_client_be_cmd); install_element(CONFIG_NODE, &debug_mgmt_client_be_cmd);
} }
void mgmt_be_client_destroy(struct mgmt_be_client *client)
/*
* Subscribe with MGMTD for one or more YANG subtree(s).
*/
enum mgmt_result mgmt_be_subscribe_yang_data(uintptr_t lib_hndl,
char *reg_yang_xpaths[],
int num_reg_xpaths)
{ {
struct mgmt_be_client_ctx *client_ctx;
if (!num_reg_xpaths)
return MGMTD_SUCCESS;
client_ctx = (struct mgmt_be_client_ctx *)lib_hndl;
if (!client_ctx)
return MGMTD_INVALID_PARAM;
if (mgmt_be_send_subscr_req(client_ctx, true, num_reg_xpaths,
reg_yang_xpaths)
!= 0)
return MGMTD_INTERNAL_ERROR;
return MGMTD_SUCCESS;
}
/*
* Unsubscribe with MGMTD for one or more YANG subtree(s).
*/
enum mgmt_result mgmt_be_unsubscribe_yang_data(uintptr_t lib_hndl,
char *reg_yang_xpaths[],
int num_reg_xpaths)
{
struct mgmt_be_client_ctx *client_ctx;
if (!num_reg_xpaths)
return MGMTD_SUCCESS;
client_ctx = (struct mgmt_be_client_ctx *)lib_hndl;
if (!client_ctx)
return MGMTD_INVALID_PARAM;
if (mgmt_be_send_subscr_req(client_ctx, false, num_reg_xpaths,
reg_yang_xpaths)
< 0)
return MGMTD_INTERNAL_ERROR;
return MGMTD_SUCCESS;
}
/*
* Send one or more YANG notifications to MGMTD daemon.
*/
enum mgmt_result mgmt_be_send_yang_notify(uintptr_t lib_hndl,
Mgmtd__YangData * data_elems[],
int num_elems)
{
struct mgmt_be_client_ctx *client_ctx;
client_ctx = (struct mgmt_be_client_ctx *)lib_hndl;
if (!client_ctx)
return MGMTD_INVALID_PARAM;
return MGMTD_SUCCESS;
}
/*
* Destroy library and cleanup everything.
*/
void mgmt_be_client_lib_destroy(void)
{
struct mgmt_be_client_ctx *client_ctx = &mgmt_be_client_ctx;
MGMTD_BE_CLIENT_DBG("Destroying MGMTD Backend Client '%s'", MGMTD_BE_CLIENT_DBG("Destroying MGMTD Backend Client '%s'",
client_ctx->client_params.name); client->name);
msg_client_cleanup(&client_ctx->client); msg_client_cleanup(&client->client);
mgmt_be_cleanup_all_txns(client_ctx); mgmt_be_cleanup_all_txns(client);
mgmt_be_txns_fini(&client_ctx->txn_head); mgmt_be_txns_fini(&client->txn_head);
nb_config_free(client_ctx->candidate_config); nb_config_free(client->candidate_config);
memset(client_ctx, 0, sizeof(*client_ctx)); XFREE(MTYPE_MGMTD_BE_CLIENT_NAME, client->name);
XFREE(MTYPE_MGMTD_BE_CLIENT, client);
} }

View File

@ -82,67 +82,26 @@ enum mgmt_be_client_id {
#define MGMTD_BE_MAX_CLIENTS_PER_XPATH_REG 32 #define MGMTD_BE_MAX_CLIENTS_PER_XPATH_REG 32
struct mgmt_be_client;
struct mgmt_be_client_txn_ctx { struct mgmt_be_client_txn_ctx {
uintptr_t *user_ctx; uintptr_t *user_ctx;
}; };
/* /**
* All the client-specific information this library needs to * Backend client callbacks.
* initialize itself, setup connection with MGMTD BackEnd interface
* and carry on all required procedures appropriately.
* *
* BackEnd clients need to initialise a instance of this structure * Callbacks:
* with appropriate data and pass it while calling the API * client_connect_notify: called when connection is made/lost to mgmtd.
* to initialize the library (See mgmt_be_client_lib_init for * txn_notify: called when a txn has been created
* more details).
*/ */
struct mgmt_be_client_params { struct mgmt_be_client_cbs {
char name[MGMTD_CLIENT_NAME_MAX_LEN]; void (*client_connect_notify)(struct mgmt_be_client *client,
uintptr_t user_data; uintptr_t usr_data, bool connected);
unsigned long conn_retry_intvl_sec;
void (*client_connect_notify)(uintptr_t lib_hndl, void (*txn_notify)(struct mgmt_be_client *client, uintptr_t usr_data,
uintptr_t usr_data, struct mgmt_be_client_txn_ctx *txn_ctx,
bool connected); bool destroyed);
void (*client_subscribe_notify)(
uintptr_t lib_hndl, uintptr_t usr_data,
struct nb_yang_xpath **xpath,
enum mgmt_result subscribe_result[], int num_paths);
void (*txn_notify)(
uintptr_t lib_hndl, uintptr_t usr_data,
struct mgmt_be_client_txn_ctx *txn_ctx, bool destroyed);
enum mgmt_result (*data_validate)(
uintptr_t lib_hndl, uintptr_t usr_data,
struct mgmt_be_client_txn_ctx *txn_ctx,
struct nb_yang_xpath *xpath, struct nb_yang_value *data,
bool delete, char *error_if_any);
enum mgmt_result (*data_apply)(
uintptr_t lib_hndl, uintptr_t usr_data,
struct mgmt_be_client_txn_ctx *txn_ctx,
struct nb_yang_xpath *xpath, struct nb_yang_value *data,
bool delete);
enum mgmt_result (*get_data_elem)(
uintptr_t lib_hndl, uintptr_t usr_data,
struct mgmt_be_client_txn_ctx *txn_ctx,
struct nb_yang_xpath *xpath, struct nb_yang_xpath_elem *elem);
enum mgmt_result (*get_data)(
uintptr_t lib_hndl, uintptr_t usr_data,
struct mgmt_be_client_txn_ctx *txn_ctx,
struct nb_yang_xpath *xpath, bool keys_only,
struct nb_yang_xpath_elem **elems, int *num_elems,
int *next_key);
enum mgmt_result (*get_next_data)(
uintptr_t lib_hndl, uintptr_t usr_data,
struct mgmt_be_client_txn_ctx *txn_ctx,
struct nb_yang_xpath *xpath, bool keys_only,
struct nb_yang_xpath_elem **elems, int *num_elems);
}; };
/*************************************************************** /***************************************************************
@ -176,20 +135,20 @@ mgmt_be_client_name2id(const char *name)
* API prototypes * API prototypes
***************************************************************/ ***************************************************************/
/* /**
* Initialize library and try connecting with MGMTD. * Create backend client and connect to MGMTD.
* *
* params * Args:
* Backend client parameters. * client_name: the name of the client
* * cbs: callbacks for various events.
* master_thread * event_loop: the main event loop.
* Thread master.
* *
* Returns: * Returns:
* Backend client lib handler (nothing but address of mgmt_be_client_ctx) * Backend client object.
*/ */
extern uintptr_t mgmt_be_client_lib_init(struct mgmt_be_client_params *params, extern struct mgmt_be_client *
struct event_loop *master_thread); mgmt_be_client_create(const char *name, struct mgmt_be_client_cbs *cbs,
uintptr_t user_data, struct event_loop *event_loop);
/* /*
* Initialize library vty (adds debug support). * Initialize library vty (adds debug support).
@ -206,13 +165,13 @@ extern void mgmt_be_client_lib_vty_init(void);
extern void mgmt_debug_be_client_show_debug(struct vty *vty); extern void mgmt_debug_be_client_show_debug(struct vty *vty);
/* /*
* Subscribe with MGMTD for one or more YANG subtree(s). * [Un]-subscribe with MGMTD for one or more YANG subtree(s).
* *
* lib_hndl * client
* Client library handler. * The client object.
* *
* reg_yang_xpaths * reg_yang_xpaths
* Yang xpath(s) that needs to be subscribed to. * Yang xpath(s) that needs to be [un]-subscribed from/to
* *
* num_xpaths * num_xpaths
* Number of xpaths * Number of xpaths
@ -220,52 +179,14 @@ extern void mgmt_debug_be_client_show_debug(struct vty *vty);
* Returns: * Returns:
* MGMTD_SUCCESS on success, MGMTD_* otherwise. * MGMTD_SUCCESS on success, MGMTD_* otherwise.
*/ */
extern enum mgmt_result mgmt_be_subscribe_yang_data(uintptr_t lib_hndl, extern int mgmt_be_send_subscr_req(struct mgmt_be_client *client,
char **reg_yang_xpaths, bool subscr_xpaths, int num_xpaths,
int num_xpaths); char **reg_xpaths);
/* /*
* Send one or more YANG notifications to MGMTD daemon. * Destroy backend client and cleanup everything.
*
* lib_hndl
* Client library handler.
*
* data_elems
* Yang data elements from data tree.
*
* num_elems
* Number of data elements.
*
* Returns:
* MGMTD_SUCCESS on success, MGMTD_* otherwise.
*/ */
extern enum mgmt_result extern void mgmt_be_client_destroy(struct mgmt_be_client *client);
mgmt_be_send_yang_notify(uintptr_t lib_hndl, Mgmtd__YangData **data_elems,
int num_elems);
/*
* Un-subscribe with MGMTD for one or more YANG subtree(s).
*
* lib_hndl
* Client library handler.
*
* reg_yang_xpaths
* Yang xpath(s) that needs to be un-subscribed from.
*
* num_reg_xpaths
* Number of subscribed xpaths
*
* Returns:
* MGMTD_SUCCESS on success, MGMTD_* otherwise.
*/
enum mgmt_result mgmt_be_unsubscribe_yang_data(uintptr_t lib_hndl,
char **reg_yang_xpaths,
int num_reg_xpaths);
/*
* Destroy library and cleanup everything.
*/
extern void mgmt_be_client_lib_destroy(void);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -53,7 +53,7 @@ struct option longopts[] = { { 0 } };
/* Master of threads. */ /* Master of threads. */
struct event_loop *master; struct event_loop *master;
uintptr_t mgmt_lib_hndl; struct mgmt_be_client *mgmt_be_client;
static struct frr_daemon_info staticd_di; static struct frr_daemon_info staticd_di;
/* SIGHUP handler. */ /* SIGHUP handler. */
@ -71,7 +71,7 @@ static void sigint(void)
/* Disable BFD events to avoid wasting processing. */ /* Disable BFD events to avoid wasting processing. */
bfd_protocol_integration_set_shutdown(true); bfd_protocol_integration_set_shutdown(true);
mgmt_be_client_lib_destroy(); mgmt_be_client_destroy(mgmt_be_client);
static_vrf_terminate(); static_vrf_terminate();
@ -106,56 +106,6 @@ struct frr_signal_t static_signals[] = {
}, },
}; };
#if 0
static void static_mgmt_be_client_connect(uintptr_t lib_hndl,
uintptr_t usr_data, bool connected)
{
(void)usr_data;
assert(lib_hndl == mgmt_lib_hndl);
zlog_debug("Got %s %s MGMTD Backend Client Server",
connected ? "connected" : "disconnected",
connected ? "to" : "from");
/* unless we are subscribing to xpaths we don't need to do this */
if (connected)
(void)mgmt_be_subscribe_yang_data(mgmt_lib_hndl, NULL, 0);
}
static void
static_mgmt_txn_notify(uintptr_t lib_hndl, uintptr_t usr_data,
struct mgmt_be_client_txn_ctx *txn_ctx,
bool destroyed)
{
zlog_debug("Got Txn %s Notify from MGMTD server",
destroyed ? "DESTROY" : "CREATE");
if (!destroyed) {
/*
* TODO: Allocate and install a private scratchpad for this
* transaction if required
*/
} else {
/*
* TODO: Uninstall and deallocate the private scratchpad for
* this transaction if installed earlier.
*/
}
}
#endif
static struct mgmt_be_client_params mgmt_params = {
.name = "staticd",
.conn_retry_intvl_sec = 3,
/*
* instead of a connect routine maybe just put xpaths to subcribe to
* here
*/
.client_connect_notify = NULL, /* static_mgmt_be_client_connect, */
.txn_notify = NULL, /* static_mgmt_txn_notify */
};
static const struct frr_yang_module_info *const staticd_yang_modules[] = { static const struct frr_yang_module_info *const staticd_yang_modules[] = {
&frr_filter_info, &frr_filter_info,
&frr_interface_info, &frr_interface_info,
@ -212,7 +162,7 @@ int main(int argc, char **argv, char **envp)
static_vty_init(); static_vty_init();
/* Initialize MGMT backend functionalities */ /* Initialize MGMT backend functionalities */
mgmt_lib_hndl = mgmt_be_client_lib_init(&mgmt_params, master); mgmt_be_client = mgmt_be_client_create("staticd", NULL, 0, master);
hook_register(routing_conf_event, hook_register(routing_conf_event,
routing_control_plane_protocols_name_validate); routing_control_plane_protocols_name_validate);