Skip to content

Commit

Permalink
Low-level message functions added descr argument for better logging…
Browse files Browse the repository at this point in the history
… C-API change
  • Loading branch information
olofhagsand committed Sep 12, 2023
1 parent 17b64f0 commit 9091582
Show file tree
Hide file tree
Showing 16 changed files with 397 additions and 236 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ Users may have to change how they access the system
### C/CLI-API changes on existing features
Developers may need to change their code

* Low-level message functions added `descr` argument for better logging
* In this way, message debugs in level 2 are more descriptive
* The descr argument can be set to NULL for backward-compability, see the following translations:
* clicon_rpc(s, ...) --> clicon_rpc(s, NULL, ...)
* clicon_rpc1(s, ...) --> clicon_rpc1(s, NULL, ...)
* clicon_msg_send(s, ...) --> clicon_msg_send(s, NULL, ...)
* clicon_msg_send1(s, ...) --> clicon_msg_send1(s, NULL, ...)
* clicon_msg_rcv(s, ...) --> clicon_msg_rcv(s, NULL, ...)
* clicon_msg_rcv1(s, ...) --> clicon_msg_rcv1(s, NULL, ...)
* clicon_msg_notify_xml(h, s, ...) --> clicon_msg_notify_xml(h, s, NULL, ...)
* send_msg_reply(s, ...) --> send_msg_reply(s, NULL, ...)
* clixon_client_lock(s, ...) --> clixon_client_lock(s, NULL, ...)
* clixon_client_hello(s, ...) --> clixon_client_hello(s, NULL, ...)

* CLI pipe function: added arg to `pipe_tail_fn()`

### Minor features
Expand Down
96 changes: 72 additions & 24 deletions apps/backend/backend_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ ce_find_byid(struct client_entry *ce_list,

/*! Stream callback for netconf stream notification (RFC 5277)
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] op 0:event, 1:rm
* @param[in] event Event as XML
* @param[in] arg Extra argument provided in stream_ss_add
Expand All @@ -113,7 +113,7 @@ ce_event_cb(clicon_handle h,
backend_client_rm(h, ce);
break;
default:
if (send_msg_notify_xml(h, ce->ce_s, event) < 0){
if (send_msg_notify_xml(h, ce->ce_s, ce->ce_source_host, event) < 0){
if (errno == ECONNRESET || errno == EPIPE){
clicon_log(LOG_WARNING, "client %d reset", ce->ce_nr);
}
Expand All @@ -126,6 +126,44 @@ ce_event_cb(clicon_handle h,
return 0;
}

/*! Construct a client string from client_entry information for logging
*
* @param[in] ce Client entry struct
* @param[out] cbp Cligen buffer, deallocate with cbuf_free
* @retval 0 OK
* @retval -1 Error
*/
static int
ce_client_string(struct client_entry *ce,
cbuf **cbp)
{
int retval = -1;
cbuf *cb = NULL;
char *id = NULL;

if (ce == NULL || cbp == NULL){
clicon_err(OE_UNIX, EINVAL, "ce or cbp is NULL");
goto done;
}
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;

}
if (ce->ce_transport){
if (nodeid_split(ce->ce_transport, NULL, &id) < 0)
goto done;
cprintf(cb, "%s", id);
}
cprintf(cb, "%u", ce->ce_id);
*cbp = cb;
retval = 0;
done:
if (id)
free(id);
return retval;
}

/*! Unlock all db:s of a client and call user unlock calback
*
* @see xmldb_unlock_all unlocks, but does not call user callbacks which is a backend thing
Expand Down Expand Up @@ -164,7 +202,7 @@ release_all_dbs(clicon_handle h,
*
* Backend-specific netconf monitoring state is:
* sessions
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] yspec Yang spec
* @param[in] xpath XML Xpath
* @param[in] nsc XML Namespace context for xpath
Expand Down Expand Up @@ -248,7 +286,7 @@ backend_monitoring_state_get(clicon_handle h,
*
* Close down everything wrt clients (eg sockets, subscriptions)
* Finally actually remove client struct in handle
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] ce Client handle
* @retval 0 Ok
* @retval -1 Error (fatal)
Expand Down Expand Up @@ -313,7 +351,7 @@ backend_client_rm(clicon_handle h,

/*! Get clixon per datastore stats
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] dbname Datastore name
* @param[in,out] cb Cligen buf
* @retval 0 OK
Expand Down Expand Up @@ -356,7 +394,7 @@ clixon_stats_datastore_get(clicon_handle h,

/*! Get clixon per datastore stats
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] dbname Datastore name
* @param[in,out] cb Cligen buf
* @retval 0 OK
Expand Down Expand Up @@ -386,7 +424,7 @@ clixon_stats_module_get(clicon_handle h,

/*! Loads all or part of a specified configuration to target configuration
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] xn Request: <rpc><xn></rpc>
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
* @param[in] arg client-entry
Expand Down Expand Up @@ -614,7 +652,7 @@ from_client_edit_config(clicon_handle h,

/*! Create or replace an entire config with another complete config db
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] xe Request: <rpc><xn></rpc>
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
* @param[in] arg client-entry
Expand Down Expand Up @@ -688,7 +726,7 @@ from_client_copy_config(clicon_handle h,

/*! Delete a configuration datastore.
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] xe Request: <rpc><xn></rpc>
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
* @param[in] arg client-entry
Expand Down Expand Up @@ -764,7 +802,7 @@ from_client_delete_config(clicon_handle h,

/*! Lock the configuration system of a device
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] xe Request: <rpc><xn></rpc>
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
* @param[in] arg client-entry
Expand Down Expand Up @@ -851,7 +889,7 @@ from_client_lock(clicon_handle h,

/*! Release a configuration lock previously obtained with the 'lock' operation
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] xe Request: <rpc><xn></rpc>
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
* @param[in] arg client-entry
Expand Down Expand Up @@ -921,7 +959,7 @@ from_client_unlock(clicon_handle h,

/*! Request graceful termination of a NETCONF session.
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] xe Request: <rpc><xn></rpc>
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
* @param[in] arg client-entry
Expand All @@ -948,7 +986,7 @@ from_client_close_session(clicon_handle h,

/*! Internal message: Force the termination of a NETCONF session.
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] xe Request: <rpc><xn></rpc>
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
* @param[in] arg client-entry
Expand Down Expand Up @@ -1005,7 +1043,7 @@ from_client_kill_session(clicon_handle h,

/*! Create a notification subscription
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] xe Request: <rpc><xn></rpc>
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
* @param[in] arg client-entry
Expand Down Expand Up @@ -1105,7 +1143,7 @@ from_client_create_subscription(clicon_handle h,

/*! Retrieve a schema from the NETCONF server.
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] xe Request: <rpc><xn></rpc>
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
* @param[in] arg client-entry
Expand Down Expand Up @@ -1228,7 +1266,7 @@ from_client_get_schema(clicon_handle h,

/*! Set debug level.
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] xe Request: <rpc><xn></rpc>
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
* @param[in] arg client-entry
Expand Down Expand Up @@ -1266,7 +1304,7 @@ from_client_debug(clicon_handle h,

/*! Check liveness of backend daemon, just send a reply
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] xe Request: <rpc><xn></rpc>
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
* @param[in] arg client-entry
Expand All @@ -1287,7 +1325,7 @@ from_client_ping(clicon_handle h,

/*! Check liveness of backend daemon, just send a reply
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] xe Request: <rpc><xn></rpc>
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
* @param[in] arg client-entry
Expand Down Expand Up @@ -1380,7 +1418,7 @@ from_client_stats(clicon_handle h,

/*! Request restart of specific plugins
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] xe Request: <rpc><xn></rpc>
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
* @param[in] arg client-entry
Expand Down Expand Up @@ -1428,7 +1466,7 @@ from_client_restart_plugin(clicon_handle h,

/*! Control a specific process or daemon: start/stop, etc
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] xe Request: <rpc><xn></rpc>
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
* @param[in] arg client-entry
Expand Down Expand Up @@ -1510,7 +1548,7 @@ from_client_hello(clicon_handle h,

/*! An internal clixon NETCONF message has arrived from a local client. Receive and dispatch.
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @param[in] ce Client entry (from)
* @param[in] msg Incoming message
* @retval 0 OK
Expand Down Expand Up @@ -1542,6 +1580,7 @@ from_client_msg(clicon_handle h,
char *rpcprefix;
char *namespace = NULL;
int nr = 0;
cbuf *cbce = NULL;

clicon_debug(CLIXON_DBG_DETAIL, "%s", __FUNCTION__);
yspec = clicon_dbspec_yang(h);
Expand Down Expand Up @@ -1737,7 +1776,9 @@ from_client_msg(clicon_handle h,
// XXX clicon_debug(CLIXON_DBG_MSG, "Reply:%s", cbuf_get(cbret));
/* XXX problem here is that cbret has not been parsed so may contain
parse errors */
if (send_msg_reply(ce->ce_s, cbuf_get(cbret), cbuf_len(cbret)+1) < 0){
if (ce_client_string(ce, &cbce) < 0)
goto done;
if (send_msg_reply(ce->ce_s, cbuf_get(cbce), cbuf_get(cbret), cbuf_len(cbret)+1) < 0){
switch (errno){
case EPIPE:
/* man (2) write:
Expand Down Expand Up @@ -1767,6 +1808,8 @@ from_client_msg(clicon_handle h,
xml_free(xret);
if (xt)
xml_free(xt);
if (cbce)
cbuf_free(cbce);
if (cbret)
cbuf_free(cbret);
/* Sanity: log if clicon_err() is not called ! */
Expand Down Expand Up @@ -1794,13 +1837,16 @@ from_client(int s,
struct client_entry *ce = (struct client_entry *)arg;
clicon_handle h = ce->ce_handle;
int eof = 0;
cbuf *cbce = NULL;

clicon_debug(CLIXON_DBG_DETAIL, "%s", __FUNCTION__);
if (s != ce->ce_s){
clicon_err(OE_NETCONF, EINVAL, "Internal error: s != ce->ce_s");
goto done;
}
if (clicon_msg_rcv(ce->ce_s, 0, &msg, &eof) < 0)
if (ce_client_string(ce, &cbce) < 0)
goto done;
if (clicon_msg_rcv(ce->ce_s, cbuf_get(cbce), 0, &msg, &eof) < 0)
goto done;
if (eof){
backend_client_rm(h, ce);
Expand All @@ -1812,14 +1858,16 @@ from_client(int s,
retval = 0;
done:
clicon_debug(CLIXON_DBG_DETAIL, "%s retval=%d", __FUNCTION__, retval);
if (cbce)
cbuf_free(cbce);
if (msg)
free(msg);
return retval; /* -1 here terminates backend */
}

/*! Init backend rpc: Set up standard netconf rpc callbacks
*
* @param[in] h Clicon handle
* @param[in] h Clixon handle
* @retval 0 OK
* @retval -1 Error (fatal)
* @see ietf-netconf@2011-06-01.yang
Expand Down
29 changes: 18 additions & 11 deletions apps/backend/backend_startup.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@
#include "backend_startup.h"

/*! Merge db1 into db2 without commit
* @retval -1 Error
* @retval 0 Validation failed (with cbret set)
*
* @retval 1 Validation OK
* @retval 0 Validation failed (with cbret set)
* @retval -1 Error
*/
static int
db_merge(clicon_handle h,
Expand All @@ -97,12 +98,13 @@ db_merge(clicon_handle h,
}

/*! Clixon startup startup mode: Commit startup configuration into running state
*
* @param[in] h Clixon handle
* @param[in] db tmp or startup
* @param[out] cbret If status is invalid contains error message
* @retval -1 Error
* @retval 0 Validation failed
* @retval 1 OK
* @retval 0 Validation failed
* @retval -1 Error
OK:
reset
Expand Down Expand Up @@ -203,9 +205,10 @@ startup_mode_startup(clicon_handle h,
}

/*! Merge xml in filename into database
* @retval -1 Error
* @retval 0 Validation failed (with cbret set)
*
* @retval 1 Validation OK
* @retval 0 Validation failed (with cbret set)
* @retval -1 Error
*/
static int
load_extraxml(clicon_handle h,
Expand Down Expand Up @@ -257,18 +260,21 @@ load_extraxml(clicon_handle h,
}

/*! Load extra XML via file and/or reset callback, and merge with current
*
* An application can add extra XML either via the -c <file> option or
* via the .ca_reset callback. This XML is "merged" into running, that is,
* it does not trigger validation calbacks.
* The function uses an extra "tmp" database, loads the file to it, and calls
* the reset function on it.
* @param[in] h Clicon handle
* @param[in] file (Optional) extra xml file
* @param[in] h Clixon handle
* @param[in] file (Optional) extra xml file
* @param[out] status Startup status
* @param[out] cbret If status is invalid contains error message
* @retval -1 Error
* @retval 0 Validation failed
* @retval 1 OK
* @retval 0 Validation failed
* @retval -1 Error
running -----------------+----+------>
reset loadfile / merge
Expand Down Expand Up @@ -341,10 +347,11 @@ startup_extraxml(clicon_handle h,
}

/*! Init modules state of the backend (server). To compare with startup XML
*
* Set the modules state as setopt to the datastore module.
* Only if CLICON_XMLDB_MODSTATE is enabled
* @retval -1 Error
* @retval 0 OK
* @retval -1 Error
*/
int
startup_module_state(clicon_handle h,
Expand Down
Loading

0 comments on commit 9091582

Please sign in to comment.