1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2018 NetDEF, Inc.
10 #include "lib_errors.h"
14 #include "lib/version.h"
15 #include "northbound.h"
17 #include <confd_lib.h>
18 #include <confd_cdb.h>
20 #include <confd_maapi.h>
22 DEFINE_MTYPE_STATIC(LIB
, CONFD
, "ConfD module");
24 static struct debug nb_dbg_client_confd
= {0, "Northbound client: ConfD"};
26 static struct thread_master
*master
;
27 static struct sockaddr confd_addr
;
28 static int cdb_sub_sock
, dp_ctl_sock
, dp_worker_sock
;
29 static struct thread
*t_cdb_sub
, *t_dp_ctl
, *t_dp_worker
;
30 static struct confd_daemon_ctx
*dctx
;
31 static struct confd_notification_ctx
*live_ctx
;
32 static bool confd_connected
;
33 static struct list
*confd_spoints
;
34 static struct nb_transaction
*transaction
;
36 static void frr_confd_finish_cdb(void);
37 static void frr_confd_finish_dp(void);
38 static int frr_confd_finish(void);
40 #define flog_err_confd(funcname) \
41 flog_err(EC_LIB_LIBCONFD, "%s: %s() failed: %s (%d): %s", __func__, \
42 (funcname), confd_strerror(confd_errno), confd_errno, \
46 /* ------------ Utils ------------ */
48 /* Get XPath string from ConfD hashed keypath. */
49 static void frr_confd_get_xpath(const confd_hkeypath_t
*kp
, char *xpath
,
54 confd_xpath_pp_kpath(xpath
, len
, 0, kp
);
57 * Replace double quotes by single quotes (the format accepted by the
61 while ((p
= strchr(p
, '"')) != NULL
)
65 /* Convert ConfD binary value to a string. */
66 static int frr_confd_val2str(const char *xpath
, const confd_value_t
*value
,
67 char *string
, size_t string_size
)
69 struct confd_cs_node
*csp
;
71 csp
= confd_cs_node_cd(NULL
, xpath
);
73 flog_err_confd("confd_cs_node_cd");
76 if (confd_val2str(csp
->info
.type
, value
, string
, string_size
)
78 flog_err_confd("confd_val2str");
85 /* Obtain list entry from ConfD hashed keypath. */
86 static int frr_confd_hkeypath_get_list_entry(const confd_hkeypath_t
*kp
,
87 struct nb_node
*nb_node
,
88 const void **list_entry
)
90 struct nb_node
*nb_node_list
;
97 * Count the number of YANG lists in the path, disconsidering the
100 nb_node_list
= nb_node
;
101 while (nb_node_list
->parent_list
) {
102 nb_node_list
= nb_node_list
->parent_list
;
105 if (nb_node
->snode
->nodetype
!= LYS_LIST
&& parent_lists
== 0)
108 /* Start from the beginning and move down the tree. */
109 for (int i
= kp
->len
; i
>= 0; i
--) {
110 struct yang_list_keys keys
;
112 /* Not a YANG list. */
113 if (kp
->v
[i
][0].type
!= C_BUF
)
116 /* Obtain list keys. */
117 memset(&keys
, 0, sizeof(keys
));
118 for (int j
= 0; kp
->v
[i
][j
].type
!= C_NOEXISTS
; j
++) {
119 strlcpy(keys
.key
[keys
.num
],
120 (char *)kp
->v
[i
][j
].val
.buf
.ptr
,
121 sizeof(keys
.key
[keys
.num
]));
125 /* Obtain northbound node associated to the YANG list. */
126 nb_node_list
= nb_node
;
127 for (int j
= curr_list
; j
< parent_lists
; j
++)
128 nb_node_list
= nb_node_list
->parent_list
;
130 /* Obtain list entry. */
131 if (!CHECK_FLAG(nb_node_list
->flags
, F_NB_NODE_KEYLESS_LIST
)) {
132 *list_entry
= nb_callback_lookup_entry(
133 nb_node
, *list_entry
, &keys
);
134 if (*list_entry
== NULL
)
137 unsigned long ptr_ulong
;
139 /* Retrieve list entry from pseudo-key (string). */
140 if (sscanf(keys
.key
[0], "%lu", &ptr_ulong
) != 1)
142 *list_entry
= (const void *)ptr_ulong
;
151 /* Fill the current date and time into a confd_datetime structure. */
152 static void getdatetime(struct confd_datetime
*datetime
)
157 gettimeofday(&tv
, NULL
);
158 gmtime_r(&tv
.tv_sec
, &tm
);
160 memset(datetime
, 0, sizeof(*datetime
));
161 datetime
->year
= 1900 + tm
.tm_year
;
162 datetime
->month
= tm
.tm_mon
+ 1;
163 datetime
->day
= tm
.tm_mday
;
164 datetime
->sec
= tm
.tm_sec
;
165 datetime
->micro
= tv
.tv_usec
;
166 datetime
->timezone
= 0;
167 datetime
->timezone_minutes
= 0;
168 datetime
->hour
= tm
.tm_hour
;
169 datetime
->min
= tm
.tm_min
;
172 /* ------------ CDB code ------------ */
174 struct cdb_iter_args
{
175 struct nb_config
*candidate
;
179 static enum cdb_iter_ret
180 frr_confd_cdb_diff_iter(confd_hkeypath_t
*kp
, enum cdb_iter_op cdb_op
,
181 confd_value_t
*oldv
, confd_value_t
*newv
, void *args
)
183 char xpath
[XPATH_MAXLEN
];
184 struct nb_node
*nb_node
;
185 enum nb_operation nb_op
;
186 struct cdb_iter_args
*iter_args
= args
;
187 char value_str
[YANG_VALUE_MAXLEN
];
188 struct yang_data
*data
;
192 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
195 * HACK: obtain value of leaf-list elements from the XPath due to
196 * a bug in the ConfD API.
199 sb1
= strrchr(xpath
, '[');
200 sb2
= strrchr(xpath
, ']');
201 if (sb1
&& sb2
&& !strchr(sb1
, '=')) {
203 strlcpy(value_str
, sb1
+ 1, sizeof(value_str
));
207 nb_node
= nb_node_find(xpath
);
209 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
210 "%s: unknown data path: %s", __func__
, xpath
);
211 iter_args
->error
= true;
215 /* Map operation values. */
218 nb_op
= NB_OP_CREATE
;
221 nb_op
= NB_OP_DESTROY
;
224 if (nb_operation_is_valid(NB_OP_MODIFY
, nb_node
->snode
))
225 nb_op
= NB_OP_MODIFY
;
227 /* Ignore list keys modifications. */
230 case MOP_MOVED_AFTER
:
234 /* We're not interested on this. */
237 flog_err(EC_LIB_DEVELOPMENT
,
238 "%s: unexpected operation %u [xpath %s]", __func__
,
240 iter_args
->error
= true;
244 /* Convert ConfD value to a string. */
245 if (nb_node
->snode
->nodetype
!= LYS_LEAFLIST
&& newv
246 && frr_confd_val2str(nb_node
->xpath
, newv
, value_str
,
249 flog_err(EC_LIB_CONFD_DATA_CONVERT
,
250 "%s: failed to convert ConfD value to a string",
252 iter_args
->error
= true;
256 /* Edit the candidate configuration. */
257 data
= yang_data_new(xpath
, value_str
);
258 ret
= nb_candidate_edit(iter_args
->candidate
, nb_node
, nb_op
, xpath
,
260 yang_data_free(data
);
263 EC_LIB_NB_CANDIDATE_EDIT_ERROR
,
264 "%s: failed to edit candidate configuration: operation [%s] xpath [%s]",
265 __func__
, nb_operation_name(nb_op
), xpath
);
266 iter_args
->error
= true;
273 static void frr_confd_cdb_read_cb_prepare(int fd
, int *subp
, int reslen
)
275 struct nb_context context
= {};
276 struct nb_config
*candidate
;
277 struct cdb_iter_args iter_args
;
278 char errmsg
[BUFSIZ
] = {0};
281 candidate
= nb_config_dup(running_config
);
283 /* Iterate over all configuration changes. */
284 iter_args
.candidate
= candidate
;
285 iter_args
.error
= false;
286 for (int i
= 0; i
< reslen
; i
++) {
287 if (cdb_diff_iterate(fd
, subp
[i
], frr_confd_cdb_diff_iter
,
288 ITER_WANT_PREV
, &iter_args
)
290 flog_err_confd("cdb_diff_iterate");
295 if (iter_args
.error
) {
296 nb_config_free(candidate
);
298 if (cdb_sub_abort_trans(
299 cdb_sub_sock
, CONFD_ERRCODE_APPLICATION_INTERNAL
, 0,
300 0, "Couldn't apply configuration changes")
302 flog_err_confd("cdb_sub_abort_trans");
309 * Validate the configuration changes and allocate all resources
310 * required to apply them.
313 context
.client
= NB_CLIENT_CONFD
;
314 ret
= nb_candidate_commit_prepare(context
, candidate
, NULL
,
315 &transaction
, false, false, errmsg
,
317 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
) {
318 enum confd_errcode errcode
;
322 errcode
= CONFD_ERRCODE_IN_USE
;
324 case NB_ERR_RESOURCE
:
325 errcode
= CONFD_ERRCODE_RESOURCE_DENIED
;
328 errcode
= CONFD_ERRCODE_APPLICATION
;
332 /* Reject the configuration changes. */
333 if (cdb_sub_abort_trans(cdb_sub_sock
, errcode
, 0, 0, "%s",
336 flog_err_confd("cdb_sub_abort_trans");
340 /* Acknowledge the notification. */
341 if (cdb_sync_subscription_socket(fd
, CDB_DONE_PRIORITY
)
343 flog_err_confd("cdb_sync_subscription_socket");
347 /* No configuration changes. */
349 nb_config_free(candidate
);
353 static void frr_confd_cdb_read_cb_commit(int fd
, int *subp
, int reslen
)
356 * No need to process the configuration changes again as we're already
357 * keeping track of them in the "transaction" variable.
361 /* Apply the transaction. */
363 struct nb_config
*candidate
= transaction
->config
;
364 char errmsg
[BUFSIZ
] = {0};
366 nb_candidate_commit_apply(transaction
, true, NULL
, errmsg
,
368 nb_config_free(candidate
);
371 /* Acknowledge the notification. */
372 if (cdb_sync_subscription_socket(fd
, CDB_DONE_PRIORITY
) != CONFD_OK
) {
373 flog_err_confd("cdb_sync_subscription_socket");
378 static int frr_confd_cdb_read_cb_abort(int fd
, int *subp
, int reslen
)
381 * No need to process the configuration changes again as we're already
382 * keeping track of them in the "transaction" variable.
386 /* Abort the transaction. */
388 struct nb_config
*candidate
= transaction
->config
;
389 char errmsg
[BUFSIZ
] = {0};
391 nb_candidate_commit_abort(transaction
, errmsg
, sizeof(errmsg
));
392 nb_config_free(candidate
);
395 /* Acknowledge the notification. */
396 if (cdb_sync_subscription_socket(fd
, CDB_DONE_PRIORITY
) != CONFD_OK
) {
397 flog_err_confd("cdb_sync_subscription_socket");
404 static void frr_confd_cdb_read_cb(struct thread
*thread
)
406 int fd
= THREAD_FD(thread
);
407 enum cdb_sub_notification cdb_ev
;
412 thread_add_read(master
, frr_confd_cdb_read_cb
, NULL
, fd
, &t_cdb_sub
);
414 if (cdb_read_subscription_socket2(fd
, &cdb_ev
, &flags
, &subp
, &reslen
)
416 flog_err_confd("cdb_read_subscription_socket2");
421 case CDB_SUB_PREPARE
:
422 frr_confd_cdb_read_cb_prepare(fd
, subp
, reslen
);
425 frr_confd_cdb_read_cb_commit(fd
, subp
, reslen
);
428 frr_confd_cdb_read_cb_abort(fd
, subp
, reslen
);
431 flog_err_confd("unknown CDB event");
436 /* Trigger CDB subscriptions to read the startup configuration. */
437 static void *thread_cdb_trigger_subscriptions(void *data
)
440 int *sub_points
= NULL
, len
= 0;
441 struct listnode
*node
;
445 /* Create CDB data socket. */
446 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
448 flog_err(EC_LIB_SOCKET
, "%s: failed to create socket: %s",
449 __func__
, safe_strerror(errno
));
453 if (cdb_connect(sock
, CDB_DATA_SOCKET
, &confd_addr
,
454 sizeof(struct sockaddr_in
))
456 flog_err_confd("cdb_connect");
461 * Fill array containing the subscription point of all loaded YANG
464 len
= listcount(confd_spoints
);
465 sub_points
= XCALLOC(MTYPE_CONFD
, len
* sizeof(int));
466 for (ALL_LIST_ELEMENTS_RO(confd_spoints
, node
, spoint
))
467 sub_points
[i
++] = *spoint
;
469 if (cdb_trigger_subscriptions(sock
, sub_points
, len
) != CONFD_OK
) {
470 flog_err_confd("cdb_trigger_subscriptions");
474 /* Cleanup and exit thread. */
475 XFREE(MTYPE_CONFD
, sub_points
);
481 static int frr_confd_subscribe(const struct lysc_node
*snode
, void *arg
)
483 struct yang_module
*module
= arg
;
484 struct nb_node
*nb_node
;
488 switch (snode
->nodetype
) {
495 return YANG_ITER_CONTINUE
;
498 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
499 return YANG_ITER_CONTINUE
;
501 nb_node
= snode
->priv
;
503 return YANG_ITER_CONTINUE
;
505 DEBUGD(&nb_dbg_client_confd
, "%s: subscribing to '%s'", __func__
,
508 spoint
= XMALLOC(MTYPE_CONFD
, sizeof(*spoint
));
509 ret
= cdb_subscribe2(cdb_sub_sock
, CDB_SUB_RUNNING_TWOPHASE
,
510 CDB_SUB_WANT_ABORT_ON_ABORT
, 3, spoint
,
511 module
->confd_hash
, nb_node
->xpath
);
512 if (ret
!= CONFD_OK
) {
513 flog_err_confd("cdb_subscribe2");
514 XFREE(MTYPE_CONFD
, spoint
);
515 return YANG_ITER_CONTINUE
;
518 listnode_add(confd_spoints
, spoint
);
519 return YANG_ITER_CONTINUE
;
522 static int frr_confd_init_cdb(void)
524 struct yang_module
*module
;
525 pthread_t cdb_trigger_thread
;
527 /* Create CDB subscription socket. */
528 cdb_sub_sock
= socket(PF_INET
, SOCK_STREAM
, 0);
529 if (cdb_sub_sock
< 0) {
530 flog_err(EC_LIB_SOCKET
, "%s: failed to create socket: %s",
531 __func__
, safe_strerror(errno
));
535 if (cdb_connect(cdb_sub_sock
, CDB_SUBSCRIPTION_SOCKET
, &confd_addr
,
536 sizeof(struct sockaddr_in
))
538 flog_err_confd("cdb_connect");
542 /* Subscribe to all loaded YANG data modules. */
543 confd_spoints
= list_new();
544 RB_FOREACH (module
, yang_modules
, &yang_modules
) {
545 module
->confd_hash
= confd_str2hash(module
->info
->ns
);
546 if (module
->confd_hash
== 0) {
549 "%s: failed to find hash value for namespace %s",
550 __func__
, module
->info
->ns
);
555 * The CDB API doesn't provide a mechanism to subscribe to an
556 * entire YANG module. So we have to find the top level
557 * nodes ourselves and subscribe to their paths.
559 yang_snodes_iterate(module
->info
, frr_confd_subscribe
, 0,
563 if (cdb_subscribe_done(cdb_sub_sock
) != CONFD_OK
) {
564 flog_err_confd("cdb_subscribe_done");
568 /* Create short lived pthread to trigger the CDB subscriptions. */
569 if (pthread_create(&cdb_trigger_thread
, NULL
,
570 thread_cdb_trigger_subscriptions
, NULL
)) {
571 flog_err(EC_LIB_SYSTEM_CALL
, "%s: error creating pthread: %s",
572 __func__
, safe_strerror(errno
));
575 pthread_detach(cdb_trigger_thread
);
577 thread_add_read(master
, frr_confd_cdb_read_cb
, NULL
, cdb_sub_sock
,
583 frr_confd_finish_cdb();
588 static void frr_confd_finish_cdb(void)
590 if (cdb_sub_sock
> 0) {
591 THREAD_OFF(t_cdb_sub
);
592 cdb_close(cdb_sub_sock
);
596 /* ------------ DP code ------------ */
598 static int frr_confd_transaction_init(struct confd_trans_ctx
*tctx
)
600 confd_trans_set_fd(tctx
, dp_worker_sock
);
605 #define CONFD_MAX_CHILD_NODES 32
607 static int frr_confd_data_get_elem(struct confd_trans_ctx
*tctx
,
608 confd_hkeypath_t
*kp
)
610 struct nb_node
*nb_node
;
611 char xpath
[XPATH_MAXLEN
];
612 struct yang_data
*data
;
614 const void *list_entry
= NULL
;
616 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
618 nb_node
= nb_node_find(xpath
);
620 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
621 "%s: unknown data path: %s", __func__
, xpath
);
622 confd_data_reply_not_found(tctx
);
626 if (frr_confd_hkeypath_get_list_entry(kp
, nb_node
, &list_entry
) != 0) {
627 confd_data_reply_not_found(tctx
);
631 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
634 CONFD_SET_STR(&v
, data
->value
);
635 confd_data_reply_value(tctx
, &v
);
637 confd_data_reply_found(tctx
);
638 yang_data_free(data
);
640 confd_data_reply_not_found(tctx
);
645 static int frr_confd_data_get_next(struct confd_trans_ctx
*tctx
,
646 confd_hkeypath_t
*kp
, long next
)
648 struct nb_node
*nb_node
;
649 char xpath
[XPATH_MAXLEN
];
650 struct yang_data
*data
;
651 const void *parent_list_entry
, *nb_next
;
652 confd_value_t v
[LIST_MAXKEYS
];
654 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
656 nb_node
= nb_node_find(xpath
);
658 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
659 "%s: unknown data path: %s", __func__
, xpath
);
660 confd_data_reply_next_key(tctx
, NULL
, -1, -1);
664 if (frr_confd_hkeypath_get_list_entry(kp
, nb_node
, &parent_list_entry
)
666 /* List entry doesn't exist anymore. */
667 confd_data_reply_next_key(tctx
, NULL
, -1, -1);
671 nb_next
= nb_callback_get_next(nb_node
, parent_list_entry
,
672 (next
== -1) ? NULL
: (void *)next
);
674 /* End of the list or leaf-list. */
675 confd_data_reply_next_key(tctx
, NULL
, -1, -1);
679 switch (nb_node
->snode
->nodetype
) {
681 if (!CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
)) {
682 struct yang_list_keys keys
;
684 memset(&keys
, 0, sizeof(keys
));
685 if (nb_callback_get_keys(nb_node
, nb_next
, &keys
)
687 flog_warn(EC_LIB_NB_CB_STATE
,
688 "%s: failed to get list keys",
690 confd_data_reply_next_key(tctx
, NULL
, -1, -1);
694 /* Feed keys to ConfD. */
695 for (size_t i
= 0; i
< keys
.num
; i
++)
696 CONFD_SET_STR(&v
[i
], keys
.key
[i
]);
697 confd_data_reply_next_key(tctx
, v
, keys
.num
,
700 char pointer_str
[32];
703 * ConfD 6.6 user guide, chapter 6.11 (Operational data
704 * lists without keys):
705 * "To support this without having completely separate
706 * APIs, we use a "pseudo" key in the ConfD APIs for
707 * this type of list. This key is not part of the data
708 * model, and completely hidden in the northbound agent
709 * interfaces, but is used with e.g. the get_next() and
710 * get_elem() callbacks as if it were a normal key. This
711 * "pseudo" key is always a single signed 64-bit
712 * integer, i.e. the confd_value_t type is C_INT64. The
713 * values can be chosen arbitrarily by the application,
714 * as long as a key value returned by get_next() can be
715 * used to get the data for the corresponding list entry
716 * with get_elem() or get_object() as usual. It could
717 * e.g. be an index into an array that holds the data,
718 * or even a memory address in integer form".
720 * Since we're using the CONFD_DAEMON_FLAG_STRINGSONLY
721 * option, we must convert our pseudo-key (a void
722 * pointer) to a string before sending it to confd.
724 snprintf(pointer_str
, sizeof(pointer_str
), "%lu",
725 (unsigned long)nb_next
);
726 CONFD_SET_STR(&v
[0], pointer_str
);
727 confd_data_reply_next_key(tctx
, v
, 1, (long)nb_next
);
731 data
= nb_callback_get_elem(nb_node
, xpath
, nb_next
);
734 CONFD_SET_STR(&v
[0], data
->value
);
735 confd_data_reply_next_key(tctx
, v
, 1,
738 yang_data_free(data
);
740 confd_data_reply_next_key(tctx
, NULL
, -1, -1);
750 * Optional callback - implemented for performance reasons.
752 static int frr_confd_data_get_object(struct confd_trans_ctx
*tctx
,
753 confd_hkeypath_t
*kp
)
755 struct nb_node
*nb_node
;
756 const struct lysc_node
*child
;
757 char xpath
[XPATH_MAXLEN
];
758 char xpath_child
[XPATH_MAXLEN
* 2];
759 struct list
*elements
;
760 struct yang_data
*data
;
761 const void *list_entry
;
762 confd_value_t values
[CONFD_MAX_CHILD_NODES
];
765 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
767 nb_node
= nb_node_find(xpath
);
769 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
770 "%s: unknown data path: %s", __func__
, xpath
);
771 confd_data_reply_not_found(tctx
);
775 if (frr_confd_hkeypath_get_list_entry(kp
, nb_node
, &list_entry
) != 0) {
776 confd_data_reply_not_found(tctx
);
780 elements
= yang_data_list_new();
782 /* Loop through list child nodes. */
783 LY_LIST_FOR (lysc_node_child(nb_node
->snode
), child
) {
784 struct nb_node
*nb_node_child
= child
->priv
;
787 if (nvalues
> CONFD_MAX_CHILD_NODES
)
790 v
= &values
[nvalues
++];
792 /* Non-presence containers, lists and leaf-lists. */
793 if (!nb_node_child
->cbs
.get_elem
) {
794 CONFD_SET_NOEXISTS(v
);
798 snprintf(xpath_child
, sizeof(xpath_child
), "%s/%s", xpath
,
800 data
= nb_callback_get_elem(nb_node_child
, xpath_child
,
804 CONFD_SET_STR(v
, data
->value
);
806 /* Presence containers and empty leafs. */
808 v
, nb_node_child
->confd_hash
,
809 confd_str2hash(nb_node_child
->snode
812 listnode_add(elements
, data
);
814 CONFD_SET_NOEXISTS(v
);
817 confd_data_reply_value_array(tctx
, values
, nvalues
);
819 /* Release memory. */
820 list_delete(&elements
);
826 * Optional callback - implemented for performance reasons.
828 static int frr_confd_data_get_next_object(struct confd_trans_ctx
*tctx
,
829 confd_hkeypath_t
*kp
, long next
)
831 char xpath
[XPATH_MAXLEN
];
832 struct nb_node
*nb_node
;
833 struct list
*elements
;
834 const void *parent_list_entry
;
836 #define CONFD_OBJECTS_PER_TIME 100
837 struct confd_next_object objects
[CONFD_OBJECTS_PER_TIME
+ 1];
838 char pseudo_keys
[CONFD_OBJECTS_PER_TIME
][32];
841 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
843 nb_node
= nb_node_find(xpath
);
845 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
846 "%s: unknown data path: %s", __func__
, xpath
);
847 confd_data_reply_next_object_array(tctx
, NULL
, 0, 0);
851 if (frr_confd_hkeypath_get_list_entry(kp
, nb_node
, &parent_list_entry
)
853 confd_data_reply_next_object_array(tctx
, NULL
, 0, 0);
857 elements
= yang_data_list_new();
858 nb_next
= (next
== -1) ? NULL
: (void *)next
;
860 memset(objects
, 0, sizeof(objects
));
861 for (int j
= 0; j
< CONFD_OBJECTS_PER_TIME
; j
++) {
862 struct confd_next_object
*object
;
863 const struct lysc_node
*child
;
864 struct yang_data
*data
;
867 object
= &objects
[j
];
869 nb_next
= nb_callback_get_next(nb_node
, parent_list_entry
,
872 /* End of the list. */
875 object
->next
= (long)nb_next
;
877 /* Leaf-lists require special handling. */
878 if (nb_node
->snode
->nodetype
== LYS_LEAFLIST
) {
879 object
->v
= XMALLOC(MTYPE_CONFD
, sizeof(confd_value_t
));
880 data
= nb_callback_get_elem(nb_node
, xpath
, nb_next
);
881 assert(data
&& data
->value
);
882 CONFD_SET_STR(object
->v
, data
->value
);
884 listnode_add(elements
, data
);
890 CONFD_MAX_CHILD_NODES
* sizeof(confd_value_t
));
893 * ConfD 6.6 user guide, chapter 6.11 (Operational data lists
895 * "In the response to the get_next_object() callback, the data
896 * provider is expected to provide the key values along with the
897 * other leafs in an array that is populated according to the
898 * data model. This must be done also for this type of list,
899 * even though the key isn't actually in the data model. The
900 * "pseudo" key must always be the first element in the array".
902 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
)) {
905 snprintf(pseudo_keys
[j
], sizeof(pseudo_keys
[j
]), "%lu",
906 (unsigned long)nb_next
);
908 v
= &object
->v
[nvalues
++];
909 CONFD_SET_STR(v
, pseudo_keys
[j
]);
912 /* Loop through list child nodes. */
913 LY_LIST_FOR (lysc_node_child(nb_node
->snode
), child
) {
914 struct nb_node
*nb_node_child
= child
->priv
;
915 char xpath_child
[XPATH_MAXLEN
* 2];
918 if (nvalues
> CONFD_MAX_CHILD_NODES
)
921 v
= &object
->v
[nvalues
++];
923 /* Non-presence containers, lists and leaf-lists. */
924 if (!nb_node_child
->cbs
.get_elem
) {
925 CONFD_SET_NOEXISTS(v
);
929 snprintf(xpath_child
, sizeof(xpath_child
), "%s/%s",
931 data
= nb_callback_get_elem(nb_node_child
, xpath_child
,
935 CONFD_SET_STR(v
, data
->value
);
938 * Presence containers and empty leafs.
941 v
, nb_node_child
->confd_hash
,
946 listnode_add(elements
, data
);
948 CONFD_SET_NOEXISTS(v
);
956 confd_data_reply_next_object_array(tctx
, NULL
, 0, 0);
957 list_delete(&elements
);
961 /* Detect end of the list. */
964 objects
[nobjects
].v
= NULL
;
967 /* Reply to ConfD. */
968 confd_data_reply_next_object_arrays(tctx
, objects
, nobjects
, 0);
972 /* Release memory. */
973 list_delete(&elements
);
974 for (int j
= 0; j
< nobjects
; j
++) {
975 struct confd_next_object
*object
;
977 object
= &objects
[j
];
978 XFREE(MTYPE_CONFD
, object
->v
);
984 static int frr_confd_notification_send(const char *xpath
,
985 struct list
*arguments
)
987 struct nb_node
*nb_node
;
988 struct yang_module
*module
;
989 struct confd_datetime now
;
990 confd_tag_value_t
*values
;
993 struct yang_data
*data
;
994 struct listnode
*node
;
997 nb_node
= nb_node_find(xpath
);
999 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1000 "%s: unknown data path: %s", __func__
, xpath
);
1003 module
= yang_module_find(nb_node
->snode
->module
->name
);
1008 nvalues
+= listcount(arguments
);
1010 values
= XMALLOC(MTYPE_CONFD
, nvalues
* sizeof(*values
));
1012 CONFD_SET_TAG_XMLBEGIN(&values
[i
++], nb_node
->confd_hash
,
1013 module
->confd_hash
);
1014 for (ALL_LIST_ELEMENTS_RO(arguments
, node
, data
)) {
1015 struct nb_node
*nb_node_arg
;
1017 nb_node_arg
= nb_node_find(data
->xpath
);
1019 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1020 "%s: unknown data path: %s", __func__
,
1022 XFREE(MTYPE_CONFD
, values
);
1026 CONFD_SET_TAG_STR(&values
[i
++], nb_node_arg
->confd_hash
,
1029 CONFD_SET_TAG_XMLEND(&values
[i
++], nb_node
->confd_hash
,
1030 module
->confd_hash
);
1033 ret
= confd_notification_send(live_ctx
, &now
, values
, nvalues
);
1035 /* Release memory. */
1036 XFREE(MTYPE_CONFD
, values
);
1038 /* Map ConfD return code to northbound return code. */
1047 static int frr_confd_action_init(struct confd_user_info
*uinfo
)
1049 confd_action_set_fd(uinfo
, dp_worker_sock
);
1054 static int frr_confd_action_execute(struct confd_user_info
*uinfo
,
1055 struct xml_tag
*name
, confd_hkeypath_t
*kp
,
1056 confd_tag_value_t
*params
, int nparams
)
1058 char xpath
[XPATH_MAXLEN
];
1059 struct nb_node
*nb_node
;
1061 struct list
*output
;
1062 struct yang_data
*data
;
1063 confd_tag_value_t
*reply
;
1065 char errmsg
[BUFSIZ
] = {0};
1067 /* Getting the XPath is tricky. */
1069 /* This is a YANG RPC. */
1070 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
1071 strlcat(xpath
, "/", sizeof(xpath
));
1072 strlcat(xpath
, confd_hash2str(name
->tag
), sizeof(xpath
));
1074 /* This is a YANG action. */
1075 snprintf(xpath
, sizeof(xpath
), "/%s:%s",
1076 confd_ns2prefix(name
->ns
), confd_hash2str(name
->tag
));
1079 nb_node
= nb_node_find(xpath
);
1081 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1082 "%s: unknown data path: %s", __func__
, xpath
);
1086 input
= yang_data_list_new();
1087 output
= yang_data_list_new();
1089 /* Process input nodes. */
1090 for (int i
= 0; i
< nparams
; i
++) {
1091 char xpath_input
[XPATH_MAXLEN
* 2];
1092 char value_str
[YANG_VALUE_MAXLEN
];
1094 snprintf(xpath_input
, sizeof(xpath_input
), "%s/%s", xpath
,
1095 confd_hash2str(params
[i
].tag
.tag
));
1097 if (frr_confd_val2str(xpath_input
, ¶ms
[i
].v
, value_str
,
1101 EC_LIB_CONFD_DATA_CONVERT
,
1102 "%s: failed to convert ConfD value to a string",
1108 data
= yang_data_new(xpath_input
, value_str
);
1109 listnode_add(input
, data
);
1112 /* Execute callback registered for this XPath. */
1113 if (nb_callback_rpc(nb_node
, xpath
, input
, output
, errmsg
,
1116 flog_warn(EC_LIB_NB_CB_RPC
, "%s: rpc callback failed: %s",
1122 /* Process output nodes. */
1123 if (listcount(output
) > 0) {
1124 struct listnode
*node
;
1127 reply
= XMALLOC(MTYPE_CONFD
,
1128 listcount(output
) * sizeof(*reply
));
1130 for (ALL_LIST_ELEMENTS_RO(output
, node
, data
)) {
1131 struct nb_node
*nb_node_output
;
1134 nb_node_output
= nb_node_find(data
->xpath
);
1135 if (!nb_node_output
) {
1136 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1137 "%s: unknown data path: %s", __func__
,
1142 hash
= confd_str2hash(nb_node_output
->snode
->name
);
1143 CONFD_SET_TAG_STR(&reply
[i
++], hash
, data
->value
);
1145 confd_action_reply_values(uinfo
, reply
, listcount(output
));
1146 XFREE(MTYPE_CONFD
, reply
);
1150 /* Release memory. */
1151 list_delete(&input
);
1152 list_delete(&output
);
1158 static int frr_confd_dp_read(struct confd_daemon_ctx
*dctx
, int fd
)
1162 ret
= confd_fd_ready(dctx
, fd
);
1163 if (ret
== CONFD_EOF
) {
1164 flog_err_confd("confd_fd_ready");
1167 } else if (ret
== CONFD_ERR
&& confd_errno
!= CONFD_ERR_EXTERNAL
) {
1168 flog_err_confd("confd_fd_ready");
1176 static void frr_confd_dp_ctl_read(struct thread
*thread
)
1178 struct confd_daemon_ctx
*dctx
= THREAD_ARG(thread
);
1179 int fd
= THREAD_FD(thread
);
1181 thread_add_read(master
, frr_confd_dp_ctl_read
, dctx
, fd
, &t_dp_ctl
);
1183 frr_confd_dp_read(dctx
, fd
);
1186 static void frr_confd_dp_worker_read(struct thread
*thread
)
1188 struct confd_daemon_ctx
*dctx
= THREAD_ARG(thread
);
1189 int fd
= THREAD_FD(thread
);
1191 thread_add_read(master
, frr_confd_dp_worker_read
, dctx
, fd
, &t_dp_worker
);
1193 frr_confd_dp_read(dctx
, fd
);
1196 static int frr_confd_subscribe_state(const struct lysc_node
*snode
, void *arg
)
1198 struct nb_node
*nb_node
= snode
->priv
;
1199 struct confd_data_cbs
*data_cbs
= arg
;
1201 if (!nb_node
|| !CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
1202 return YANG_ITER_CONTINUE
;
1203 /* We only need to subscribe to the root of the state subtrees. */
1204 if (snode
->parent
&& CHECK_FLAG(snode
->parent
->flags
, LYS_CONFIG_R
))
1205 return YANG_ITER_CONTINUE
;
1207 DEBUGD(&nb_dbg_client_confd
,
1208 "%s: providing data to '%s' (callpoint %s)", __func__
,
1209 nb_node
->xpath
, snode
->name
);
1211 strlcpy(data_cbs
->callpoint
, snode
->name
, sizeof(data_cbs
->callpoint
));
1212 if (confd_register_data_cb(dctx
, data_cbs
) != CONFD_OK
)
1213 flog_err_confd("confd_register_data_cb");
1215 return YANG_ITER_CONTINUE
;
1218 static int frr_confd_init_dp(const char *program_name
)
1220 struct confd_trans_cbs trans_cbs
;
1221 struct confd_data_cbs data_cbs
;
1222 struct confd_notification_stream_cbs ncbs
;
1223 struct confd_action_cbs acbs
;
1225 /* Initialize daemon context. */
1226 dctx
= confd_init_daemon(program_name
);
1228 flog_err_confd("confd_init_daemon");
1233 * Inform we want to receive YANG values as raw strings, and that we
1234 * want to provide only strings in the reply functions, regardless of
1237 confd_set_daemon_flags(dctx
, CONFD_DAEMON_FLAG_STRINGSONLY
);
1239 /* Establish a control socket. */
1240 dp_ctl_sock
= socket(PF_INET
, SOCK_STREAM
, 0);
1241 if (dp_ctl_sock
< 0) {
1242 flog_err(EC_LIB_SOCKET
, "%s: failed to create socket: %s",
1243 __func__
, safe_strerror(errno
));
1247 if (confd_connect(dctx
, dp_ctl_sock
, CONTROL_SOCKET
, &confd_addr
,
1248 sizeof(struct sockaddr_in
))
1250 flog_err_confd("confd_connect");
1255 * Establish a worker socket (only one since this plugin runs on a
1258 dp_worker_sock
= socket(PF_INET
, SOCK_STREAM
, 0);
1259 if (dp_worker_sock
< 0) {
1260 flog_err(EC_LIB_SOCKET
, "%s: failed to create socket: %s",
1261 __func__
, safe_strerror(errno
));
1264 if (confd_connect(dctx
, dp_worker_sock
, WORKER_SOCKET
, &confd_addr
,
1265 sizeof(struct sockaddr_in
))
1267 flog_err_confd("confd_connect");
1271 /* Register transaction callback functions. */
1272 memset(&trans_cbs
, 0, sizeof(trans_cbs
));
1273 trans_cbs
.init
= frr_confd_transaction_init
;
1274 confd_register_trans_cb(dctx
, &trans_cbs
);
1276 /* Register our read/write callbacks. */
1277 memset(&data_cbs
, 0, sizeof(data_cbs
));
1278 data_cbs
.get_elem
= frr_confd_data_get_elem
;
1279 data_cbs
.exists_optional
= frr_confd_data_get_elem
;
1280 data_cbs
.get_next
= frr_confd_data_get_next
;
1281 data_cbs
.get_object
= frr_confd_data_get_object
;
1282 data_cbs
.get_next_object
= frr_confd_data_get_next_object
;
1285 * Iterate over all loaded YANG modules and subscribe to the paths
1286 * referent to state data.
1288 yang_snodes_iterate(NULL
, frr_confd_subscribe_state
, 0, &data_cbs
);
1290 /* Register notification stream. */
1291 memset(&ncbs
, 0, sizeof(ncbs
));
1292 ncbs
.fd
= dp_worker_sock
;
1294 * RFC 5277 - Section 3.2.3:
1295 * A NETCONF server implementation supporting the notification
1296 * capability MUST support the "NETCONF" notification event
1297 * stream. This stream contains all NETCONF XML event notifications
1298 * supported by the NETCONF server.
1300 strlcpy(ncbs
.streamname
, "NETCONF", sizeof(ncbs
.streamname
));
1301 if (confd_register_notification_stream(dctx
, &ncbs
, &live_ctx
)
1303 flog_err_confd("confd_register_notification_stream");
1307 /* Register the action handler callback. */
1308 memset(&acbs
, 0, sizeof(acbs
));
1309 strlcpy(acbs
.actionpoint
, "actionpoint", sizeof(acbs
.actionpoint
));
1310 acbs
.init
= frr_confd_action_init
;
1311 acbs
.action
= frr_confd_action_execute
;
1312 if (confd_register_action_cbs(dctx
, &acbs
) != CONFD_OK
) {
1313 flog_err_confd("confd_register_action_cbs");
1317 /* Notify we registered all callbacks we wanted. */
1318 if (confd_register_done(dctx
) != CONFD_OK
) {
1319 flog_err_confd("confd_register_done");
1323 thread_add_read(master
, frr_confd_dp_ctl_read
, dctx
, dp_ctl_sock
,
1325 thread_add_read(master
, frr_confd_dp_worker_read
, dctx
, dp_worker_sock
,
1331 frr_confd_finish_dp();
1336 static void frr_confd_finish_dp(void)
1338 if (dp_worker_sock
> 0) {
1339 THREAD_OFF(t_dp_worker
);
1340 close(dp_worker_sock
);
1342 if (dp_ctl_sock
> 0) {
1343 THREAD_OFF(t_dp_ctl
);
1347 confd_release_daemon(dctx
);
1350 /* ------------ CLI ------------ */
1352 DEFUN (debug_nb_confd
,
1354 "[no] debug northbound client confd",
1357 "Northbound debugging\n"
1361 uint32_t mode
= DEBUG_NODE2MODE(vty
->node
);
1362 bool no
= strmatch(argv
[0]->text
, "no");
1364 DEBUG_MODE_SET(&nb_dbg_client_confd
, mode
, !no
);
1369 static int frr_confd_debug_config_write(struct vty
*vty
)
1371 if (DEBUG_MODE_CHECK(&nb_dbg_client_confd
, DEBUG_MODE_CONF
))
1372 vty_out(vty
, "debug northbound client confd\n");
1377 static int frr_confd_debug_set_all(uint32_t flags
, bool set
)
1379 DEBUG_FLAGS_SET(&nb_dbg_client_confd
, flags
, set
);
1381 /* If all modes have been turned off, don't preserve options. */
1382 if (!DEBUG_MODE_CHECK(&nb_dbg_client_confd
, DEBUG_MODE_ALL
))
1383 DEBUG_CLEAR(&nb_dbg_client_confd
);
1388 static void frr_confd_cli_init(void)
1390 hook_register(nb_client_debug_config_write
,
1391 frr_confd_debug_config_write
);
1392 hook_register(nb_client_debug_set_all
, frr_confd_debug_set_all
);
1394 install_element(ENABLE_NODE
, &debug_nb_confd_cmd
);
1395 install_element(CONFIG_NODE
, &debug_nb_confd_cmd
);
1398 /* ------------ Main ------------ */
1400 static int frr_confd_calculate_snode_hash(const struct lysc_node
*snode
,
1403 struct nb_node
*nb_node
= snode
->priv
;
1406 nb_node
->confd_hash
= confd_str2hash(snode
->name
);
1408 return YANG_ITER_CONTINUE
;
1411 static int frr_confd_init(const char *program_name
)
1413 struct sockaddr_in
*confd_addr4
= (struct sockaddr_in
*)&confd_addr
;
1414 int debuglevel
= CONFD_SILENT
;
1417 /* Initialize ConfD library. */
1418 confd_init(program_name
, stderr
, debuglevel
);
1420 confd_addr4
->sin_family
= AF_INET
;
1421 confd_addr4
->sin_addr
.s_addr
= inet_addr("127.0.0.1");
1422 confd_addr4
->sin_port
= htons(CONFD_PORT
);
1423 if (confd_load_schemas(&confd_addr
, sizeof(struct sockaddr_in
))
1425 flog_err_confd("confd_load_schemas");
1429 ret
= frr_confd_init_cdb();
1433 ret
= frr_confd_init_dp(program_name
);
1435 frr_confd_finish_cdb();
1439 yang_snodes_iterate(NULL
, frr_confd_calculate_snode_hash
, 0, NULL
);
1441 hook_register(nb_notification_send
, frr_confd_notification_send
);
1443 confd_connected
= true;
1447 confd_free_schemas();
1452 static int frr_confd_finish(void)
1454 if (!confd_connected
)
1457 frr_confd_finish_cdb();
1458 frr_confd_finish_dp();
1460 confd_free_schemas();
1462 confd_connected
= false;
1467 static int frr_confd_module_late_init(struct thread_master
*tm
)
1471 if (frr_confd_init(frr_get_progname()) < 0) {
1472 flog_err(EC_LIB_CONFD_INIT
,
1473 "failed to initialize the ConfD module");
1477 hook_register(frr_fini
, frr_confd_finish
);
1478 frr_confd_cli_init();
1483 static int frr_confd_module_init(void)
1485 hook_register(frr_late_init
, frr_confd_module_late_init
);
1490 FRR_MODULE_SETUP(.name
= "frr_confd", .version
= FRR_VERSION
,
1491 .description
= "FRR ConfD integration module",
1492 .init
= frr_confd_module_init
,