1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * MGMTD Frontend Client Library api interfaces
4 * Copyright (C) 2021 Vmware, Inc.
5 * Pushpasis Sarkar <spushpasis@vmware.com>
13 #include "mgmt_fe_client.h"
20 #include "lib/mgmt_fe_client_clippy.c"
22 PREDECL_LIST(mgmt_sessions
);
24 struct mgmt_fe_client_session
{
25 uint64_t client_id
; /* FE client identifies itself with this ID */
26 uint64_t session_id
; /* FE adapter identified session with this ID */
27 struct mgmt_fe_client
*client
;
30 struct mgmt_sessions_item list_linkage
;
33 DECLARE_LIST(mgmt_sessions
, struct mgmt_fe_client_session
, list_linkage
);
35 DEFINE_MTYPE_STATIC(LIB
, MGMTD_FE_CLIENT
, "frontend client");
36 DEFINE_MTYPE_STATIC(LIB
, MGMTD_FE_CLIENT_NAME
, "frontend client name");
37 DEFINE_MTYPE_STATIC(LIB
, MGMTD_FE_SESSION
, "frontend session");
39 struct mgmt_fe_client
{
40 struct msg_client client
;
42 struct mgmt_fe_client_cbs cbs
;
44 struct mgmt_sessions_head sessions
;
47 #define FOREACH_SESSION_IN_LIST(client, session) \
48 frr_each_safe (mgmt_sessions, &(client)->sessions, (session))
50 struct debug mgmt_dbg_fe_client
= {0, "Management frontend client operations"};
53 static struct mgmt_fe_client_session
*
54 mgmt_fe_find_session_by_client_id(struct mgmt_fe_client
*client
,
57 struct mgmt_fe_client_session
*session
;
59 FOREACH_SESSION_IN_LIST (client
, session
) {
60 if (session
->client_id
== client_id
) {
61 MGMTD_FE_CLIENT_DBG("Found session-id %" PRIu64
62 " using client-id %" PRIu64
,
63 session
->session_id
, client_id
);
67 MGMTD_FE_CLIENT_DBG("Session not found using client-id %" PRIu64
,
72 static struct mgmt_fe_client_session
*
73 mgmt_fe_find_session_by_session_id(struct mgmt_fe_client
*client
,
76 struct mgmt_fe_client_session
*session
;
78 FOREACH_SESSION_IN_LIST (client
, session
) {
79 if (session
->session_id
== session_id
) {
81 "Found session of client-id %" PRIu64
82 " using session-id %" PRIu64
,
83 session
->client_id
, session_id
);
87 MGMTD_FE_CLIENT_DBG("Session not found using session-id %" PRIu64
,
92 static int mgmt_fe_client_send_msg(struct mgmt_fe_client
*client
,
93 Mgmtd__FeMessage
*fe_msg
,
94 bool short_circuit_ok
)
96 return msg_conn_send_msg(
97 &client
->client
.conn
, MGMT_MSG_VERSION_PROTOBUF
, fe_msg
,
98 mgmtd__fe_message__get_packed_size(fe_msg
),
99 (size_t(*)(void *, void *))mgmtd__fe_message__pack
,
103 static int mgmt_fe_send_register_req(struct mgmt_fe_client
*client
)
105 Mgmtd__FeMessage fe_msg
;
106 Mgmtd__FeRegisterReq rgstr_req
;
108 mgmtd__fe_register_req__init(&rgstr_req
);
109 rgstr_req
.client_name
= client
->name
;
111 mgmtd__fe_message__init(&fe_msg
);
112 fe_msg
.message_case
= MGMTD__FE_MESSAGE__MESSAGE_REGISTER_REQ
;
113 fe_msg
.register_req
= &rgstr_req
;
116 "Sending REGISTER_REQ message to MGMTD Frontend server");
118 return mgmt_fe_client_send_msg(client
, &fe_msg
, true);
121 static int mgmt_fe_send_session_req(struct mgmt_fe_client
*client
,
122 struct mgmt_fe_client_session
*session
,
125 Mgmtd__FeMessage fe_msg
;
126 Mgmtd__FeSessionReq sess_req
;
129 mgmtd__fe_session_req__init(&sess_req
);
130 sess_req
.create
= create
;
132 sess_req
.id_case
= MGMTD__FE_SESSION_REQ__ID_CLIENT_CONN_ID
;
133 sess_req
.client_conn_id
= session
->client_id
;
136 sess_req
.id_case
= MGMTD__FE_SESSION_REQ__ID_SESSION_ID
;
137 sess_req
.session_id
= session
->session_id
;
141 mgmtd__fe_message__init(&fe_msg
);
142 fe_msg
.message_case
= MGMTD__FE_MESSAGE__MESSAGE_SESSION_REQ
;
143 fe_msg
.session_req
= &sess_req
;
146 "Sending SESSION_REQ %s message for client-id %" PRIu64
,
147 create
? "create" : "destroy", session
->client_id
);
149 return mgmt_fe_client_send_msg(client
, &fe_msg
, scok
);
152 int mgmt_fe_send_lockds_req(struct mgmt_fe_client
*client
, uint64_t session_id
,
153 uint64_t req_id
, Mgmtd__DatastoreId ds_id
,
157 Mgmtd__FeMessage fe_msg
;
158 Mgmtd__FeLockDsReq lockds_req
;
160 mgmtd__fe_lock_ds_req__init(&lockds_req
);
161 lockds_req
.session_id
= session_id
;
162 lockds_req
.req_id
= req_id
;
163 lockds_req
.ds_id
= ds_id
;
164 lockds_req
.lock
= lock
;
166 mgmtd__fe_message__init(&fe_msg
);
167 fe_msg
.message_case
= MGMTD__FE_MESSAGE__MESSAGE_LOCKDS_REQ
;
168 fe_msg
.lockds_req
= &lockds_req
;
171 "Sending %sLOCK_REQ message for Ds:%d session-id %" PRIu64
,
172 lock
? "" : "UN", ds_id
, session_id
);
174 return mgmt_fe_client_send_msg(client
, &fe_msg
, false);
177 int mgmt_fe_send_setcfg_req(struct mgmt_fe_client
*client
, uint64_t session_id
,
178 uint64_t req_id
, Mgmtd__DatastoreId ds_id
,
179 Mgmtd__YangCfgDataReq
**data_req
, int num_data_reqs
,
180 bool implicit_commit
, Mgmtd__DatastoreId dst_ds_id
)
183 Mgmtd__FeMessage fe_msg
;
184 Mgmtd__FeSetConfigReq setcfg_req
;
186 mgmtd__fe_set_config_req__init(&setcfg_req
);
187 setcfg_req
.session_id
= session_id
;
188 setcfg_req
.ds_id
= ds_id
;
189 setcfg_req
.req_id
= req_id
;
190 setcfg_req
.data
= data_req
;
191 setcfg_req
.n_data
= (size_t)num_data_reqs
;
192 setcfg_req
.implicit_commit
= implicit_commit
;
193 setcfg_req
.commit_ds_id
= dst_ds_id
;
195 mgmtd__fe_message__init(&fe_msg
);
196 fe_msg
.message_case
= MGMTD__FE_MESSAGE__MESSAGE_SETCFG_REQ
;
197 fe_msg
.setcfg_req
= &setcfg_req
;
200 "Sending SET_CONFIG_REQ message for Ds:%d session-id %" PRIu64
202 ds_id
, session_id
, num_data_reqs
);
204 return mgmt_fe_client_send_msg(client
, &fe_msg
, false);
207 int mgmt_fe_send_commitcfg_req(struct mgmt_fe_client
*client
,
208 uint64_t session_id
, uint64_t req_id
,
209 Mgmtd__DatastoreId src_ds_id
,
210 Mgmtd__DatastoreId dest_ds_id
,
211 bool validate_only
, bool abort
)
214 Mgmtd__FeMessage fe_msg
;
215 Mgmtd__FeCommitConfigReq commitcfg_req
;
217 mgmtd__fe_commit_config_req__init(&commitcfg_req
);
218 commitcfg_req
.session_id
= session_id
;
219 commitcfg_req
.src_ds_id
= src_ds_id
;
220 commitcfg_req
.dst_ds_id
= dest_ds_id
;
221 commitcfg_req
.req_id
= req_id
;
222 commitcfg_req
.validate_only
= validate_only
;
223 commitcfg_req
.abort
= abort
;
225 mgmtd__fe_message__init(&fe_msg
);
226 fe_msg
.message_case
= MGMTD__FE_MESSAGE__MESSAGE_COMMCFG_REQ
;
227 fe_msg
.commcfg_req
= &commitcfg_req
;
230 "Sending COMMIT_CONFIG_REQ message for Src-Ds:%d, Dst-Ds:%d session-id %" PRIu64
,
231 src_ds_id
, dest_ds_id
, session_id
);
233 return mgmt_fe_client_send_msg(client
, &fe_msg
, false);
236 int mgmt_fe_send_getcfg_req(struct mgmt_fe_client
*client
, uint64_t session_id
,
237 uint64_t req_id
, Mgmtd__DatastoreId ds_id
,
238 Mgmtd__YangGetDataReq
*data_req
[],
242 Mgmtd__FeMessage fe_msg
;
243 Mgmtd__FeGetConfigReq getcfg_req
;
245 mgmtd__fe_get_config_req__init(&getcfg_req
);
246 getcfg_req
.session_id
= session_id
;
247 getcfg_req
.ds_id
= ds_id
;
248 getcfg_req
.req_id
= req_id
;
249 getcfg_req
.data
= data_req
;
250 getcfg_req
.n_data
= (size_t)num_data_reqs
;
252 mgmtd__fe_message__init(&fe_msg
);
253 fe_msg
.message_case
= MGMTD__FE_MESSAGE__MESSAGE_GETCFG_REQ
;
254 fe_msg
.getcfg_req
= &getcfg_req
;
257 "Sending GET_CONFIG_REQ message for Ds:%d session-id %" PRIu64
259 ds_id
, session_id
, num_data_reqs
);
261 return mgmt_fe_client_send_msg(client
, &fe_msg
, false);
264 int mgmt_fe_send_getdata_req(struct mgmt_fe_client
*client
, uint64_t session_id
,
265 uint64_t req_id
, Mgmtd__DatastoreId ds_id
,
266 Mgmtd__YangGetDataReq
*data_req
[],
270 Mgmtd__FeMessage fe_msg
;
271 Mgmtd__FeGetDataReq getdata_req
;
273 mgmtd__fe_get_data_req__init(&getdata_req
);
274 getdata_req
.session_id
= session_id
;
275 getdata_req
.ds_id
= ds_id
;
276 getdata_req
.req_id
= req_id
;
277 getdata_req
.data
= data_req
;
278 getdata_req
.n_data
= (size_t)num_data_reqs
;
280 mgmtd__fe_message__init(&fe_msg
);
281 fe_msg
.message_case
= MGMTD__FE_MESSAGE__MESSAGE_GETDATA_REQ
;
282 fe_msg
.getdata_req
= &getdata_req
;
285 "Sending GET_CONFIG_REQ message for Ds:%d session-id %" PRIu64
287 ds_id
, session_id
, num_data_reqs
);
289 return mgmt_fe_client_send_msg(client
, &fe_msg
, false);
292 int mgmt_fe_send_regnotify_req(struct mgmt_fe_client
*client
,
293 uint64_t session_id
, uint64_t req_id
,
294 Mgmtd__DatastoreId ds_id
, bool register_req
,
295 Mgmtd__YangDataXPath
*data_req
[],
299 Mgmtd__FeMessage fe_msg
;
300 Mgmtd__FeRegisterNotifyReq regntfy_req
;
302 mgmtd__fe_register_notify_req__init(®ntfy_req
);
303 regntfy_req
.session_id
= session_id
;
304 regntfy_req
.ds_id
= ds_id
;
305 regntfy_req
.register_req
= register_req
;
306 regntfy_req
.data_xpath
= data_req
;
307 regntfy_req
.n_data_xpath
= (size_t)num_data_reqs
;
309 mgmtd__fe_message__init(&fe_msg
);
310 fe_msg
.message_case
= MGMTD__FE_MESSAGE__MESSAGE_REGNOTIFY_REQ
;
311 fe_msg
.regnotify_req
= ®ntfy_req
;
313 return mgmt_fe_client_send_msg(client
, &fe_msg
, false);
316 static int mgmt_fe_client_handle_msg(struct mgmt_fe_client
*client
,
317 Mgmtd__FeMessage
*fe_msg
)
319 struct mgmt_fe_client_session
*session
= NULL
;
322 * protobuf-c adds a max size enum with an internal, and changing by
323 * version, name; cast to an int to avoid unhandled enum warnings
325 switch ((int)fe_msg
->message_case
) {
326 case MGMTD__FE_MESSAGE__MESSAGE_SESSION_REPLY
:
327 if (fe_msg
->session_reply
->create
&&
328 fe_msg
->session_reply
->has_client_conn_id
) {
330 "Got SESSION_REPLY (create) for client-id %" PRIu64
331 " with session-id: %" PRIu64
,
332 fe_msg
->session_reply
->client_conn_id
,
333 fe_msg
->session_reply
->session_id
);
335 session
= mgmt_fe_find_session_by_client_id(
336 client
, fe_msg
->session_reply
->client_conn_id
);
338 if (session
&& fe_msg
->session_reply
->success
) {
340 "Session Created for client-id %" PRIu64
,
341 fe_msg
->session_reply
->client_conn_id
);
342 session
->session_id
=
343 fe_msg
->session_reply
->session_id
;
346 "Session Create failed for client-id %" PRIu64
,
347 fe_msg
->session_reply
->client_conn_id
);
349 } else if (!fe_msg
->session_reply
->create
) {
351 "Got SESSION_REPLY (destroy) for session-id %" PRIu64
,
352 fe_msg
->session_reply
->session_id
);
354 session
= mgmt_fe_find_session_by_session_id(
355 client
, fe_msg
->session_req
->session_id
);
358 /* The session state may be deleted by the callback */
359 if (session
&& session
->client
&&
360 session
->client
->cbs
.client_session_notify
)
361 (*session
->client
->cbs
.client_session_notify
)(
362 client
, client
->user_data
, session
->client_id
,
363 fe_msg
->session_reply
->create
,
364 fe_msg
->session_reply
->success
,
365 fe_msg
->session_reply
->session_id
,
368 case MGMTD__FE_MESSAGE__MESSAGE_LOCKDS_REPLY
:
369 MGMTD_FE_CLIENT_DBG("Got LOCKDS_REPLY for session-id %" PRIu64
,
370 fe_msg
->lockds_reply
->session_id
);
371 session
= mgmt_fe_find_session_by_session_id(
372 client
, fe_msg
->lockds_reply
->session_id
);
374 if (session
&& session
->client
&&
375 session
->client
->cbs
.lock_ds_notify
)
376 (*session
->client
->cbs
.lock_ds_notify
)(
377 client
, client
->user_data
, session
->client_id
,
378 fe_msg
->lockds_reply
->session_id
,
379 session
->user_ctx
, fe_msg
->lockds_reply
->req_id
,
380 fe_msg
->lockds_reply
->lock
,
381 fe_msg
->lockds_reply
->success
,
382 fe_msg
->lockds_reply
->ds_id
,
383 fe_msg
->lockds_reply
->error_if_any
);
385 case MGMTD__FE_MESSAGE__MESSAGE_SETCFG_REPLY
:
386 MGMTD_FE_CLIENT_DBG("Got SETCFG_REPLY for session-id %" PRIu64
,
387 fe_msg
->setcfg_reply
->session_id
);
389 session
= mgmt_fe_find_session_by_session_id(
390 client
, fe_msg
->setcfg_reply
->session_id
);
392 if (session
&& session
->client
&&
393 session
->client
->cbs
.set_config_notify
)
394 (*session
->client
->cbs
.set_config_notify
)(
395 client
, client
->user_data
, session
->client_id
,
396 fe_msg
->setcfg_reply
->session_id
,
397 session
->user_ctx
, fe_msg
->setcfg_reply
->req_id
,
398 fe_msg
->setcfg_reply
->success
,
399 fe_msg
->setcfg_reply
->ds_id
,
400 fe_msg
->setcfg_reply
->error_if_any
);
402 case MGMTD__FE_MESSAGE__MESSAGE_COMMCFG_REPLY
:
403 MGMTD_FE_CLIENT_DBG("Got COMMCFG_REPLY for session-id %" PRIu64
,
404 fe_msg
->commcfg_reply
->session_id
);
406 session
= mgmt_fe_find_session_by_session_id(
407 client
, fe_msg
->commcfg_reply
->session_id
);
409 if (session
&& session
->client
&&
410 session
->client
->cbs
.commit_config_notify
)
411 (*session
->client
->cbs
.commit_config_notify
)(
412 client
, client
->user_data
, session
->client_id
,
413 fe_msg
->commcfg_reply
->session_id
,
415 fe_msg
->commcfg_reply
->req_id
,
416 fe_msg
->commcfg_reply
->success
,
417 fe_msg
->commcfg_reply
->src_ds_id
,
418 fe_msg
->commcfg_reply
->dst_ds_id
,
419 fe_msg
->commcfg_reply
->validate_only
,
420 fe_msg
->commcfg_reply
->error_if_any
);
422 case MGMTD__FE_MESSAGE__MESSAGE_GETCFG_REPLY
:
423 MGMTD_FE_CLIENT_DBG("Got GETCFG_REPLY for session-id %" PRIu64
,
424 fe_msg
->getcfg_reply
->session_id
);
426 session
= mgmt_fe_find_session_by_session_id(
427 client
, fe_msg
->getcfg_reply
->session_id
);
429 if (session
&& session
->client
&&
430 session
->client
->cbs
.get_data_notify
)
431 (*session
->client
->cbs
.get_data_notify
)(
432 client
, client
->user_data
, session
->client_id
,
433 fe_msg
->getcfg_reply
->session_id
,
434 session
->user_ctx
, fe_msg
->getcfg_reply
->req_id
,
435 fe_msg
->getcfg_reply
->success
,
436 fe_msg
->getcfg_reply
->ds_id
,
437 fe_msg
->getcfg_reply
->data
438 ? fe_msg
->getcfg_reply
->data
->data
440 fe_msg
->getcfg_reply
->data
441 ? fe_msg
->getcfg_reply
->data
->n_data
443 fe_msg
->getcfg_reply
->data
444 ? fe_msg
->getcfg_reply
->data
->next_indx
446 fe_msg
->getcfg_reply
->error_if_any
);
448 case MGMTD__FE_MESSAGE__MESSAGE_GETDATA_REPLY
:
449 MGMTD_FE_CLIENT_DBG("Got GETDATA_REPLY for session-id %" PRIu64
,
450 fe_msg
->getdata_reply
->session_id
);
452 session
= mgmt_fe_find_session_by_session_id(
453 client
, fe_msg
->getdata_reply
->session_id
);
455 if (session
&& session
->client
&&
456 session
->client
->cbs
.get_data_notify
)
457 (*session
->client
->cbs
.get_data_notify
)(
458 client
, client
->user_data
, session
->client_id
,
459 fe_msg
->getdata_reply
->session_id
,
461 fe_msg
->getdata_reply
->req_id
,
462 fe_msg
->getdata_reply
->success
,
463 fe_msg
->getdata_reply
->ds_id
,
464 fe_msg
->getdata_reply
->data
465 ? fe_msg
->getdata_reply
->data
->data
467 fe_msg
->getdata_reply
->data
468 ? fe_msg
->getdata_reply
->data
->n_data
470 fe_msg
->getdata_reply
->data
471 ? fe_msg
->getdata_reply
->data
->next_indx
473 fe_msg
->getdata_reply
->error_if_any
);
475 case MGMTD__FE_MESSAGE__MESSAGE_NOTIFY_DATA_REQ
:
476 case MGMTD__FE_MESSAGE__MESSAGE_REGNOTIFY_REQ
:
478 * TODO: Add handling code in future.
482 * NOTE: The following messages are always sent from Frontend
483 * clients to MGMTd only and/or need not be handled here.
485 case MGMTD__FE_MESSAGE__MESSAGE_REGISTER_REQ
:
486 case MGMTD__FE_MESSAGE__MESSAGE_SESSION_REQ
:
487 case MGMTD__FE_MESSAGE__MESSAGE_LOCKDS_REQ
:
488 case MGMTD__FE_MESSAGE__MESSAGE_SETCFG_REQ
:
489 case MGMTD__FE_MESSAGE__MESSAGE_COMMCFG_REQ
:
490 case MGMTD__FE_MESSAGE__MESSAGE_GETCFG_REQ
:
491 case MGMTD__FE_MESSAGE__MESSAGE_GETDATA_REQ
:
492 case MGMTD__FE_MESSAGE__MESSAGE__NOT_SET
:
495 * A 'default' case is being added contrary to the
496 * FRR code guidelines to take care of build
497 * failures on certain build systems (courtesy of
498 * the proto-c package).
506 static void mgmt_fe_client_process_msg(uint8_t version
, uint8_t *data
,
507 size_t len
, struct msg_conn
*conn
)
509 struct mgmt_fe_client
*client
;
510 struct msg_client
*msg_client
;
511 Mgmtd__FeMessage
*fe_msg
;
513 msg_client
= container_of(conn
, struct msg_client
, conn
);
514 client
= container_of(msg_client
, struct mgmt_fe_client
, client
);
516 fe_msg
= mgmtd__fe_message__unpack(NULL
, len
, data
);
518 MGMTD_FE_CLIENT_DBG("Failed to decode %zu bytes from server.",
523 "Decoded %zu bytes of message(msg: %u/%u) from server", len
,
524 fe_msg
->message_case
, fe_msg
->message_case
);
525 (void)mgmt_fe_client_handle_msg(client
, fe_msg
);
526 mgmtd__fe_message__free_unpacked(fe_msg
, NULL
);
529 static int _notify_connect_disconnect(struct msg_client
*msg_client
,
532 struct mgmt_fe_client
*client
=
533 container_of(msg_client
, struct mgmt_fe_client
, client
);
534 struct mgmt_fe_client_session
*session
;
537 /* Send REGISTER_REQ message */
539 if ((ret
= mgmt_fe_send_register_req(client
)) != 0)
543 /* Walk list of sessions for this FE client deleting them */
544 if (!connected
&& mgmt_sessions_count(&client
->sessions
)) {
545 MGMTD_FE_CLIENT_DBG("Cleaning up existing sessions");
547 FOREACH_SESSION_IN_LIST (client
, session
) {
548 assert(session
->client
);
550 /* unlink from list first this avoids double free */
551 mgmt_sessions_del(&client
->sessions
, session
);
553 /* notify FE client the session is being deleted */
554 if (session
->client
->cbs
.client_session_notify
) {
555 (*session
->client
->cbs
.client_session_notify
)(
556 client
, client
->user_data
,
557 session
->client_id
, false, true,
558 session
->session_id
, session
->user_ctx
);
561 XFREE(MTYPE_MGMTD_FE_SESSION
, session
);
565 /* Notify FE client through registered callback (if any). */
566 if (client
->cbs
.client_connect_notify
)
567 (void)(*client
->cbs
.client_connect_notify
)(
568 client
, client
->user_data
, connected
);
572 static int mgmt_fe_client_notify_connect(struct msg_client
*client
)
574 return _notify_connect_disconnect(client
, true);
577 static int mgmt_fe_client_notify_disconnect(struct msg_conn
*conn
)
579 struct msg_client
*client
= container_of(conn
, struct msg_client
, conn
);
581 return _notify_connect_disconnect(client
, false);
585 DEFPY(debug_mgmt_client_fe
, debug_mgmt_client_fe_cmd
,
586 "[no] debug mgmt client frontend",
587 NO_STR DEBUG_STR MGMTD_STR
591 uint32_t mode
= DEBUG_NODE2MODE(vty
->node
);
593 DEBUG_MODE_SET(&mgmt_dbg_fe_client
, mode
, !no
);
598 static void mgmt_debug_client_fe_set_all(uint32_t flags
, bool set
)
600 DEBUG_FLAGS_SET(&mgmt_dbg_fe_client
, flags
, set
);
603 static int mgmt_debug_fe_client_config_write(struct vty
*vty
)
605 if (DEBUG_MODE_CHECK(&mgmt_dbg_fe_client
, DEBUG_MODE_CONF
))
606 vty_out(vty
, "debug mgmt client frontend\n");
611 void mgmt_debug_fe_client_show_debug(struct vty
*vty
)
613 if (MGMTD_DBG_FE_CLIENT_CHECK())
614 vty_out(vty
, "debug mgmt client frontend\n");
617 static struct debug_callbacks mgmt_dbg_fe_client_cbs
= {
618 .debug_set_all
= mgmt_debug_client_fe_set_all
};
620 static struct cmd_node mgmt_dbg_node
= {
621 .name
= "mgmt client frontend",
624 .config_write
= mgmt_debug_fe_client_config_write
,
628 * Initialize library and try connecting with MGMTD.
630 struct mgmt_fe_client
*mgmt_fe_client_create(const char *client_name
,
631 struct mgmt_fe_client_cbs
*cbs
,
633 struct event_loop
*event_loop
)
635 struct mgmt_fe_client
*client
=
636 XCALLOC(MTYPE_MGMTD_FE_CLIENT
, sizeof(*client
));
638 client
->name
= XSTRDUP(MTYPE_MGMTD_FE_CLIENT_NAME
, client_name
);
639 client
->user_data
= user_data
;
643 mgmt_sessions_init(&client
->sessions
);
645 msg_client_init(&client
->client
, event_loop
, MGMTD_FE_SERVER_PATH
,
646 mgmt_fe_client_notify_connect
,
647 mgmt_fe_client_notify_disconnect
,
648 mgmt_fe_client_process_msg
, MGMTD_FE_MAX_NUM_MSG_PROC
,
649 MGMTD_FE_MAX_NUM_MSG_WRITE
, MGMTD_FE_MSG_MAX_LEN
, true,
650 "FE-client", MGMTD_DBG_FE_CLIENT_CHECK());
652 MGMTD_FE_CLIENT_DBG("Initialized client '%s'", client_name
);
657 void mgmt_fe_client_lib_vty_init(void)
659 debug_init(&mgmt_dbg_fe_client_cbs
);
660 install_node(&mgmt_dbg_node
);
661 install_element(ENABLE_NODE
, &debug_mgmt_client_fe_cmd
);
662 install_element(CONFIG_NODE
, &debug_mgmt_client_fe_cmd
);
665 uint
mgmt_fe_client_session_count(struct mgmt_fe_client
*client
)
667 return mgmt_sessions_count(&client
->sessions
);
671 * Create a new Session for a Frontend Client connection.
673 enum mgmt_result
mgmt_fe_create_client_session(struct mgmt_fe_client
*client
,
677 struct mgmt_fe_client_session
*session
;
679 session
= XCALLOC(MTYPE_MGMTD_FE_SESSION
,
680 sizeof(struct mgmt_fe_client_session
));
682 session
->user_ctx
= user_ctx
;
683 session
->client_id
= client_id
;
684 session
->client
= client
;
685 session
->session_id
= 0;
687 mgmt_sessions_add_tail(&client
->sessions
, session
);
689 if (mgmt_fe_send_session_req(client
, session
, true) != 0) {
690 XFREE(MTYPE_MGMTD_FE_SESSION
, session
);
691 return MGMTD_INTERNAL_ERROR
;
694 return MGMTD_SUCCESS
;
698 * Delete an existing Session for a Frontend Client connection.
700 enum mgmt_result
mgmt_fe_destroy_client_session(struct mgmt_fe_client
*client
,
703 struct mgmt_fe_client_session
*session
;
705 session
= mgmt_fe_find_session_by_client_id(client
, client_id
);
706 if (!session
|| session
->client
!= client
)
707 return MGMTD_INVALID_PARAM
;
709 if (session
->session_id
&&
710 mgmt_fe_send_session_req(client
, session
, false) != 0)
712 "Failed to send session destroy request for the session-id %" PRIu64
,
713 session
->session_id
);
715 mgmt_sessions_del(&client
->sessions
, session
);
716 XFREE(MTYPE_MGMTD_FE_SESSION
, session
);
718 return MGMTD_SUCCESS
;
722 * Destroy library and cleanup everything.
724 void mgmt_fe_client_destroy(struct mgmt_fe_client
*client
)
726 struct mgmt_fe_client_session
*session
;
728 MGMTD_FE_CLIENT_DBG("Destroying MGMTD Frontend Client '%s'",
731 FOREACH_SESSION_IN_LIST (client
, session
)
732 mgmt_fe_destroy_client_session(client
, session
->client_id
);
734 msg_client_cleanup(&client
->client
);
736 XFREE(MTYPE_MGMTD_FE_CLIENT_NAME
, client
->name
);
737 XFREE(MTYPE_MGMTD_FE_CLIENT
, client
);