2 * Copyright (C) 2018 NetDEF, Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "lib_errors.h"
28 #include "northbound.h"
30 #include <confd_lib.h>
31 #include <confd_cdb.h>
33 #include <confd_maapi.h>
35 DEFINE_MTYPE_STATIC(LIB
, CONFD
, "ConfD module")
37 static struct debug nb_dbg_client_confd
= {0, "Northbound client: ConfD"};
39 static struct thread_master
*master
;
40 static struct sockaddr confd_addr
;
41 static int cdb_sub_sock
, dp_ctl_sock
, dp_worker_sock
;
42 static struct thread
*t_cdb_sub
, *t_dp_ctl
, *t_dp_worker
;
43 static struct confd_daemon_ctx
*dctx
;
44 static struct confd_notification_ctx
*live_ctx
;
45 static bool confd_connected
;
46 static struct list
*confd_spoints
;
47 static struct nb_transaction
*transaction
;
49 static void frr_confd_finish_cdb(void);
50 static void frr_confd_finish_dp(void);
51 static int frr_confd_finish(void);
53 #define flog_err_confd(funcname) \
54 flog_err(EC_LIB_LIBCONFD, "%s: %s() failed: %s (%d): %s", __func__, \
55 (funcname), confd_strerror(confd_errno), confd_errno, \
59 /* ------------ Utils ------------ */
61 /* Get XPath string from ConfD hashed keypath. */
62 static void frr_confd_get_xpath(const confd_hkeypath_t
*kp
, char *xpath
,
67 confd_xpath_pp_kpath(xpath
, len
, 0, kp
);
70 * Replace double quotes by single quotes (the format accepted by the
74 while ((p
= strchr(p
, '"')) != NULL
)
78 /* Convert ConfD binary value to a string. */
79 static int frr_confd_val2str(const char *xpath
, const confd_value_t
*value
,
80 char *string
, size_t string_size
)
82 struct confd_cs_node
*csp
;
84 csp
= confd_cs_node_cd(NULL
, xpath
);
86 flog_err_confd("confd_cs_node_cd");
89 if (confd_val2str(csp
->info
.type
, value
, string
, string_size
)
91 flog_err_confd("confd_val2str");
98 /* Obtain list entry from ConfD hashed keypath. */
99 static int frr_confd_hkeypath_get_list_entry(const confd_hkeypath_t
*kp
,
100 struct nb_node
*nb_node
,
101 const void **list_entry
)
103 struct nb_node
*nb_node_list
;
104 int parent_lists
= 0;
110 * Count the number of YANG lists in the path, disconsidering the
113 nb_node_list
= nb_node
;
114 while (nb_node_list
->parent_list
) {
115 nb_node_list
= nb_node_list
->parent_list
;
118 if (nb_node
->snode
->nodetype
!= LYS_LIST
&& parent_lists
== 0)
121 /* Start from the beginning and move down the tree. */
122 for (int i
= kp
->len
; i
>= 0; i
--) {
123 struct yang_list_keys keys
;
125 /* Not a YANG list. */
126 if (kp
->v
[i
][0].type
!= C_BUF
)
129 /* Obtain list keys. */
130 memset(&keys
, 0, sizeof(keys
));
131 for (int j
= 0; kp
->v
[i
][j
].type
!= C_NOEXISTS
; j
++) {
132 strlcpy(keys
.key
[keys
.num
],
133 (char *)kp
->v
[i
][j
].val
.buf
.ptr
,
134 sizeof(keys
.key
[keys
.num
]));
138 /* Obtain northbound node associated to the YANG list. */
139 nb_node_list
= nb_node
;
140 for (int j
= curr_list
; j
< parent_lists
; j
++)
141 nb_node_list
= nb_node_list
->parent_list
;
143 /* Obtain list entry. */
144 if (!CHECK_FLAG(nb_node_list
->flags
, F_NB_NODE_KEYLESS_LIST
)) {
145 *list_entry
= nb_callback_lookup_entry(
146 nb_node
, *list_entry
, &keys
);
147 if (*list_entry
== NULL
)
150 unsigned long ptr_ulong
;
152 /* Retrieve list entry from pseudo-key (string). */
153 if (sscanf(keys
.key
[0], "%lu", &ptr_ulong
) != 1)
155 *list_entry
= (const void *)ptr_ulong
;
164 /* Fill the current date and time into a confd_datetime structure. */
165 static void getdatetime(struct confd_datetime
*datetime
)
170 gettimeofday(&tv
, NULL
);
171 gmtime_r(&tv
.tv_sec
, &tm
);
173 memset(datetime
, 0, sizeof(*datetime
));
174 datetime
->year
= 1900 + tm
.tm_year
;
175 datetime
->month
= tm
.tm_mon
+ 1;
176 datetime
->day
= tm
.tm_mday
;
177 datetime
->sec
= tm
.tm_sec
;
178 datetime
->micro
= tv
.tv_usec
;
179 datetime
->timezone
= 0;
180 datetime
->timezone_minutes
= 0;
181 datetime
->hour
= tm
.tm_hour
;
182 datetime
->min
= tm
.tm_min
;
185 /* ------------ CDB code ------------ */
187 struct cdb_iter_args
{
188 struct nb_config
*candidate
;
192 static enum cdb_iter_ret
193 frr_confd_cdb_diff_iter(confd_hkeypath_t
*kp
, enum cdb_iter_op cdb_op
,
194 confd_value_t
*oldv
, confd_value_t
*newv
, void *args
)
196 char xpath
[XPATH_MAXLEN
];
197 struct nb_node
*nb_node
;
198 enum nb_operation nb_op
;
199 struct cdb_iter_args
*iter_args
= args
;
200 char value_str
[YANG_VALUE_MAXLEN
];
201 struct yang_data
*data
;
205 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
208 * HACK: obtain value of leaf-list elements from the XPath due to
209 * a bug in the ConfD API.
212 sb1
= strrchr(xpath
, '[');
213 sb2
= strrchr(xpath
, ']');
214 if (sb1
&& sb2
&& !strchr(sb1
, '=')) {
216 strlcpy(value_str
, sb1
+ 1, sizeof(value_str
));
220 nb_node
= nb_node_find(xpath
);
222 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
223 "%s: unknown data path: %s", __func__
, xpath
);
224 iter_args
->error
= true;
228 /* Map operation values. */
231 nb_op
= NB_OP_CREATE
;
234 nb_op
= NB_OP_DESTROY
;
237 if (nb_operation_is_valid(NB_OP_MODIFY
, nb_node
->snode
))
238 nb_op
= NB_OP_MODIFY
;
240 /* Ignore list keys modifications. */
243 case MOP_MOVED_AFTER
:
247 /* We're not interested on this. */
250 flog_err(EC_LIB_DEVELOPMENT
,
251 "%s: unexpected operation %u [xpath %s]", __func__
,
253 iter_args
->error
= true;
257 /* Convert ConfD value to a string. */
258 if (nb_node
->snode
->nodetype
!= LYS_LEAFLIST
&& newv
259 && frr_confd_val2str(nb_node
->xpath
, newv
, value_str
,
262 flog_err(EC_LIB_CONFD_DATA_CONVERT
,
263 "%s: failed to convert ConfD value to a string",
265 iter_args
->error
= true;
269 /* Edit the candidate configuration. */
270 data
= yang_data_new(xpath
, value_str
);
271 ret
= nb_candidate_edit(iter_args
->candidate
, nb_node
, nb_op
, xpath
,
273 yang_data_free(data
);
276 EC_LIB_NB_CANDIDATE_EDIT_ERROR
,
277 "%s: failed to edit candidate configuration: operation [%s] xpath [%s]",
278 __func__
, nb_operation_name(nb_op
), xpath
);
279 iter_args
->error
= true;
286 static int frr_confd_cdb_read_cb_prepare(int fd
, int *subp
, int reslen
)
288 struct nb_config
*candidate
;
289 struct cdb_iter_args iter_args
;
292 candidate
= nb_config_dup(running_config
);
294 /* Iterate over all configuration changes. */
295 iter_args
.candidate
= candidate
;
296 iter_args
.error
= false;
297 for (int i
= 0; i
< reslen
; i
++) {
298 if (cdb_diff_iterate(fd
, subp
[i
], frr_confd_cdb_diff_iter
,
299 ITER_WANT_PREV
, &iter_args
)
301 flog_err_confd("cdb_diff_iterate");
306 if (iter_args
.error
) {
307 nb_config_free(candidate
);
309 if (cdb_sub_abort_trans(
310 cdb_sub_sock
, CONFD_ERRCODE_APPLICATION_INTERNAL
, 0,
311 0, "Couldn't apply configuration changes")
313 flog_err_confd("cdb_sub_abort_trans");
320 * Validate the configuration changes and allocate all resources
321 * required to apply them.
324 ret
= nb_candidate_commit_prepare(candidate
, NB_CLIENT_CONFD
, NULL
,
326 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
) {
327 enum confd_errcode errcode
;
332 errcode
= CONFD_ERRCODE_IN_USE
;
333 errmsg
= "Configuration is locked by another process";
335 case NB_ERR_RESOURCE
:
336 errcode
= CONFD_ERRCODE_RESOURCE_DENIED
;
337 errmsg
= "Failed do allocate resources";
340 errcode
= CONFD_ERRCODE_INTERNAL
;
341 errmsg
= "Internal error";
345 /* Reject the configuration changes. */
346 if (cdb_sub_abort_trans(cdb_sub_sock
, errcode
, 0, 0, "%s",
349 flog_err_confd("cdb_sub_abort_trans");
353 /* Acknowledge the notification. */
354 if (cdb_sync_subscription_socket(fd
, CDB_DONE_PRIORITY
)
356 flog_err_confd("cdb_sync_subscription_socket");
360 /* No configuration changes. */
362 nb_config_free(candidate
);
368 static int frr_confd_cdb_read_cb_commit(int fd
, int *subp
, int reslen
)
371 * No need to process the configuration changes again as we're already
372 * keeping track of them in the "transaction" variable.
376 /* Apply the transaction. */
378 struct nb_config
*candidate
= transaction
->config
;
380 nb_candidate_commit_apply(transaction
, true, NULL
);
381 nb_config_free(candidate
);
384 /* Acknowledge the notification. */
385 if (cdb_sync_subscription_socket(fd
, CDB_DONE_PRIORITY
) != CONFD_OK
) {
386 flog_err_confd("cdb_sync_subscription_socket");
393 static int frr_confd_cdb_read_cb_abort(int fd
, int *subp
, int reslen
)
396 * No need to process the configuration changes again as we're already
397 * keeping track of them in the "transaction" variable.
401 /* Abort the transaction. */
403 struct nb_config
*candidate
= transaction
->config
;
405 nb_candidate_commit_abort(transaction
);
406 nb_config_free(candidate
);
409 /* Acknowledge the notification. */
410 if (cdb_sync_subscription_socket(fd
, CDB_DONE_PRIORITY
) != CONFD_OK
) {
411 flog_err_confd("cdb_sync_subscription_socket");
418 static int frr_confd_cdb_read_cb(struct thread
*thread
)
420 int fd
= THREAD_FD(thread
);
421 enum cdb_sub_notification cdb_ev
;
427 thread_add_read(master
, frr_confd_cdb_read_cb
, NULL
, fd
, &thread
);
429 if (cdb_read_subscription_socket2(fd
, &cdb_ev
, &flags
, &subp
, &reslen
)
431 flog_err_confd("cdb_read_subscription_socket2");
436 case CDB_SUB_PREPARE
:
437 return frr_confd_cdb_read_cb_prepare(fd
, subp
, reslen
);
439 return frr_confd_cdb_read_cb_commit(fd
, subp
, reslen
);
441 return frr_confd_cdb_read_cb_abort(fd
, subp
, reslen
);
443 flog_err_confd("unknown CDB event");
448 /* Trigger CDB subscriptions to read the startup configuration. */
449 static void *thread_cdb_trigger_subscriptions(void *data
)
452 int *sub_points
= NULL
, len
= 0;
453 struct listnode
*node
;
457 /* Create CDB data socket. */
458 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
460 flog_err(EC_LIB_SOCKET
, "%s: failed to create socket: %s",
461 __func__
, safe_strerror(errno
));
465 if (cdb_connect(sock
, CDB_DATA_SOCKET
, &confd_addr
,
466 sizeof(struct sockaddr_in
))
468 flog_err_confd("cdb_connect");
473 * Fill array containing the subscription point of all loaded YANG
476 len
= listcount(confd_spoints
);
477 sub_points
= XCALLOC(MTYPE_CONFD
, len
* sizeof(int));
478 for (ALL_LIST_ELEMENTS_RO(confd_spoints
, node
, spoint
))
479 sub_points
[i
++] = *spoint
;
481 if (cdb_trigger_subscriptions(sock
, sub_points
, len
) != CONFD_OK
) {
482 flog_err_confd("cdb_trigger_subscriptions");
486 /* Cleanup and exit thread. */
487 XFREE(MTYPE_CONFD
, sub_points
);
493 static int frr_confd_init_cdb(void)
495 struct yang_module
*module
;
496 pthread_t cdb_trigger_thread
;
498 /* Create CDB subscription socket. */
499 cdb_sub_sock
= socket(PF_INET
, SOCK_STREAM
, 0);
500 if (cdb_sub_sock
< 0) {
501 flog_err(EC_LIB_SOCKET
, "%s: failed to create socket: %s",
502 __func__
, safe_strerror(errno
));
506 if (cdb_connect(cdb_sub_sock
, CDB_SUBSCRIPTION_SOCKET
, &confd_addr
,
507 sizeof(struct sockaddr_in
))
509 flog_err_confd("cdb_connect");
513 /* Subscribe to all loaded YANG data modules. */
514 confd_spoints
= list_new();
515 RB_FOREACH (module
, yang_modules
, &yang_modules
) {
516 struct lys_node
*snode
;
518 module
->confd_hash
= confd_str2hash(module
->info
->ns
);
519 if (module
->confd_hash
== 0) {
522 "%s: failed to find hash value for namespace %s",
523 __func__
, module
->info
->ns
);
528 * The CDB API doesn't provide a mechanism to subscribe to an
529 * entire YANG module. So we have to find the top level
530 * nodes ourselves and subscribe to their paths.
532 LY_TREE_FOR (module
->info
->data
, snode
) {
533 struct nb_node
*nb_node
;
537 switch (snode
->nodetype
) {
547 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
550 nb_node
= snode
->priv
;
551 DEBUGD(&nb_dbg_client_confd
, "%s: subscribing to '%s'",
552 __func__
, nb_node
->xpath
);
554 spoint
= XMALLOC(MTYPE_CONFD
, sizeof(*spoint
));
555 ret
= cdb_subscribe2(
556 cdb_sub_sock
, CDB_SUB_RUNNING_TWOPHASE
,
557 CDB_SUB_WANT_ABORT_ON_ABORT
, 3, spoint
,
558 module
->confd_hash
, nb_node
->xpath
);
559 if (ret
!= CONFD_OK
) {
560 flog_err_confd("cdb_subscribe2");
561 XFREE(MTYPE_CONFD
, spoint
);
563 listnode_add(confd_spoints
, spoint
);
567 if (cdb_subscribe_done(cdb_sub_sock
) != CONFD_OK
) {
568 flog_err_confd("cdb_subscribe_done");
572 /* Create short lived pthread to trigger the CDB subscriptions. */
573 if (pthread_create(&cdb_trigger_thread
, NULL
,
574 thread_cdb_trigger_subscriptions
, NULL
)) {
575 flog_err(EC_LIB_SYSTEM_CALL
, "%s: error creating pthread: %s",
576 __func__
, safe_strerror(errno
));
579 pthread_detach(cdb_trigger_thread
);
581 thread_add_read(master
, frr_confd_cdb_read_cb
, NULL
, cdb_sub_sock
,
587 frr_confd_finish_cdb();
592 static void frr_confd_finish_cdb(void)
594 if (cdb_sub_sock
> 0) {
595 THREAD_OFF(t_cdb_sub
);
596 cdb_close(cdb_sub_sock
);
600 /* ------------ DP code ------------ */
602 static int frr_confd_transaction_init(struct confd_trans_ctx
*tctx
)
604 confd_trans_set_fd(tctx
, dp_worker_sock
);
609 #define CONFD_MAX_CHILD_NODES 32
611 static int frr_confd_data_get_elem(struct confd_trans_ctx
*tctx
,
612 confd_hkeypath_t
*kp
)
614 struct nb_node
*nb_node
;
616 struct yang_data
*data
;
618 const void *list_entry
= NULL
;
620 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
622 nb_node
= nb_node_find(xpath
);
624 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
625 "%s: unknown data path: %s", __func__
, xpath
);
626 confd_data_reply_not_found(tctx
);
630 if (frr_confd_hkeypath_get_list_entry(kp
, nb_node
, &list_entry
) != 0) {
631 confd_data_reply_not_found(tctx
);
635 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
638 CONFD_SET_STR(&v
, data
->value
);
639 confd_data_reply_value(tctx
, &v
);
641 confd_data_reply_found(tctx
);
642 yang_data_free(data
);
644 confd_data_reply_not_found(tctx
);
649 static int frr_confd_data_get_next(struct confd_trans_ctx
*tctx
,
650 confd_hkeypath_t
*kp
, long next
)
652 struct nb_node
*nb_node
;
654 struct yang_data
*data
;
655 const void *parent_list_entry
, *nb_next
;
656 confd_value_t v
[LIST_MAXKEYS
];
658 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
660 nb_node
= nb_node_find(xpath
);
662 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
663 "%s: unknown data path: %s", __func__
, xpath
);
664 confd_data_reply_next_key(tctx
, NULL
, -1, -1);
668 if (frr_confd_hkeypath_get_list_entry(kp
, nb_node
, &parent_list_entry
)
670 /* List entry doesn't exist anymore. */
671 confd_data_reply_next_key(tctx
, NULL
, -1, -1);
675 nb_next
= nb_callback_get_next(nb_node
, parent_list_entry
,
676 (next
== -1) ? NULL
: (void *)next
);
678 /* End of the list or leaf-list. */
679 confd_data_reply_next_key(tctx
, NULL
, -1, -1);
683 switch (nb_node
->snode
->nodetype
) {
685 if (!CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
)) {
686 struct yang_list_keys keys
;
688 memset(&keys
, 0, sizeof(keys
));
689 if (nb_callback_get_keys(nb_node
, nb_next
, &keys
)
691 flog_warn(EC_LIB_NB_CB_STATE
,
692 "%s: failed to get list keys",
694 confd_data_reply_next_key(tctx
, NULL
, -1, -1);
698 /* Feed keys to ConfD. */
699 for (size_t i
= 0; i
< keys
.num
; i
++)
700 CONFD_SET_STR(&v
[i
], keys
.key
[i
]);
701 confd_data_reply_next_key(tctx
, v
, keys
.num
,
704 char pointer_str
[16];
707 * ConfD 6.6 user guide, chapter 6.11 (Operational data
708 * lists without keys):
709 * "To support this without having completely separate
710 * APIs, we use a "pseudo" key in the ConfD APIs for
711 * this type of list. This key is not part of the data
712 * model, and completely hidden in the northbound agent
713 * interfaces, but is used with e.g. the get_next() and
714 * get_elem() callbacks as if it were a normal key. This
715 * "pseudo" key is always a single signed 64-bit
716 * integer, i.e. the confd_value_t type is C_INT64. The
717 * values can be chosen arbitrarily by the application,
718 * as long as a key value returned by get_next() can be
719 * used to get the data for the corresponding list entry
720 * with get_elem() or get_object() as usual. It could
721 * e.g. be an index into an array that holds the data,
722 * or even a memory address in integer form".
724 * Since we're using the CONFD_DAEMON_FLAG_STRINGSONLY
725 * option, we must convert our pseudo-key (a void
726 * pointer) to a string before sending it to confd.
728 snprintf(pointer_str
, sizeof(pointer_str
), "%lu",
729 (unsigned long)nb_next
);
730 CONFD_SET_STR(&v
[0], pointer_str
);
731 confd_data_reply_next_key(tctx
, v
, 1, (long)nb_next
);
735 data
= nb_callback_get_elem(nb_node
, xpath
, nb_next
);
738 CONFD_SET_STR(&v
[0], data
->value
);
739 confd_data_reply_next_key(tctx
, v
, 1,
742 yang_data_free(data
);
744 confd_data_reply_next_key(tctx
, NULL
, -1, -1);
754 * Optional callback - implemented for performance reasons.
756 static int frr_confd_data_get_object(struct confd_trans_ctx
*tctx
,
757 confd_hkeypath_t
*kp
)
759 struct nb_node
*nb_node
;
760 const struct lys_node
*child
;
762 char xpath_child
[XPATH_MAXLEN
];
763 struct list
*elements
;
764 struct yang_data
*data
;
765 const void *list_entry
;
766 confd_value_t values
[CONFD_MAX_CHILD_NODES
];
769 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
771 nb_node
= nb_node_find(xpath
);
773 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
774 "%s: unknown data path: %s", __func__
, xpath
);
775 confd_data_reply_not_found(tctx
);
779 if (frr_confd_hkeypath_get_list_entry(kp
, nb_node
, &list_entry
) != 0) {
780 confd_data_reply_not_found(tctx
);
784 elements
= yang_data_list_new();
786 /* Loop through list child nodes. */
787 LY_TREE_FOR (nb_node
->snode
->child
, child
) {
788 struct nb_node
*nb_node_child
= child
->priv
;
791 if (nvalues
> CONFD_MAX_CHILD_NODES
)
794 v
= &values
[nvalues
++];
796 /* Non-presence containers, lists and leaf-lists. */
797 if (!nb_node_child
->cbs
.get_elem
) {
798 CONFD_SET_NOEXISTS(v
);
802 snprintf(xpath_child
, sizeof(xpath_child
), "%s/%s", xpath
,
804 data
= nb_callback_get_elem(nb_node_child
, xpath_child
,
808 CONFD_SET_STR(v
, data
->value
);
810 /* Presence containers and empty leafs. */
812 v
, nb_node_child
->confd_hash
,
813 confd_str2hash(nb_node_child
->snode
816 listnode_add(elements
, data
);
818 CONFD_SET_NOEXISTS(v
);
821 confd_data_reply_value_array(tctx
, values
, nvalues
);
823 /* Release memory. */
824 list_delete(&elements
);
830 * Optional callback - implemented for performance reasons.
832 static int frr_confd_data_get_next_object(struct confd_trans_ctx
*tctx
,
833 confd_hkeypath_t
*kp
, long next
)
836 struct nb_node
*nb_node
;
837 struct list
*elements
;
838 const void *parent_list_entry
;
840 #define CONFD_OBJECTS_PER_TIME 100
841 struct confd_next_object objects
[CONFD_OBJECTS_PER_TIME
+ 1];
842 char pseudo_keys
[CONFD_OBJECTS_PER_TIME
][16];
845 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
847 nb_node
= nb_node_find(xpath
);
849 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
850 "%s: unknown data path: %s", __func__
, xpath
);
851 confd_data_reply_next_object_array(tctx
, NULL
, 0, 0);
855 if (frr_confd_hkeypath_get_list_entry(kp
, nb_node
, &parent_list_entry
)
857 confd_data_reply_next_object_array(tctx
, NULL
, 0, 0);
861 elements
= yang_data_list_new();
862 nb_next
= (next
== -1) ? NULL
: (void *)next
;
864 memset(objects
, 0, sizeof(objects
));
865 for (int j
= 0; j
< CONFD_OBJECTS_PER_TIME
; j
++) {
866 struct confd_next_object
*object
;
867 struct lys_node
*child
;
868 struct yang_data
*data
;
871 object
= &objects
[j
];
873 nb_next
= nb_callback_get_next(nb_node
, parent_list_entry
,
876 /* End of the list. */
879 object
->next
= (long)nb_next
;
881 /* Leaf-lists require special handling. */
882 if (nb_node
->snode
->nodetype
== LYS_LEAFLIST
) {
883 object
->v
= XMALLOC(MTYPE_CONFD
, sizeof(confd_value_t
));
884 data
= nb_callback_get_elem(nb_node
, xpath
, nb_next
);
885 assert(data
&& data
->value
);
886 CONFD_SET_STR(object
->v
, data
->value
);
888 listnode_add(elements
, data
);
894 CONFD_MAX_CHILD_NODES
* sizeof(confd_value_t
));
897 * ConfD 6.6 user guide, chapter 6.11 (Operational data lists
899 * "In the response to the get_next_object() callback, the data
900 * provider is expected to provide the key values along with the
901 * other leafs in an array that is populated according to the
902 * data model. This must be done also for this type of list,
903 * even though the key isn't actually in the data model. The
904 * "pseudo" key must always be the first element in the array".
906 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
)) {
909 snprintf(pseudo_keys
[j
], sizeof(pseudo_keys
[j
]), "%lu",
910 (unsigned long)nb_next
);
912 v
= &object
->v
[nvalues
++];
913 CONFD_SET_STR(v
, pseudo_keys
[j
]);
916 /* Loop through list child nodes. */
917 LY_TREE_FOR (nb_node
->snode
->child
, child
) {
918 struct nb_node
*nb_node_child
= child
->priv
;
919 char xpath_child
[XPATH_MAXLEN
];
922 if (nvalues
> CONFD_MAX_CHILD_NODES
)
925 v
= &object
->v
[nvalues
++];
927 /* Non-presence containers, lists and leaf-lists. */
928 if (!nb_node_child
->cbs
.get_elem
) {
929 CONFD_SET_NOEXISTS(v
);
933 snprintf(xpath_child
, sizeof(xpath_child
), "%s/%s",
935 data
= nb_callback_get_elem(nb_node_child
, xpath_child
,
939 CONFD_SET_STR(v
, data
->value
);
942 * Presence containers and empty leafs.
945 v
, nb_node_child
->confd_hash
,
950 listnode_add(elements
, data
);
952 CONFD_SET_NOEXISTS(v
);
960 confd_data_reply_next_object_array(tctx
, NULL
, 0, 0);
961 list_delete(&elements
);
965 /* Detect end of the list. */
968 objects
[nobjects
].v
= NULL
;
971 /* Reply to ConfD. */
972 confd_data_reply_next_object_arrays(tctx
, objects
, nobjects
, 0);
976 /* Release memory. */
977 list_delete(&elements
);
978 for (int j
= 0; j
< nobjects
; j
++) {
979 struct confd_next_object
*object
;
981 object
= &objects
[j
];
982 XFREE(MTYPE_CONFD
, object
->v
);
988 static int frr_confd_notification_send(const char *xpath
,
989 struct list
*arguments
)
991 struct nb_node
*nb_node
;
992 struct yang_module
*module
;
993 struct confd_datetime now
;
994 confd_tag_value_t
*values
;
997 struct yang_data
*data
;
998 struct listnode
*node
;
1001 nb_node
= nb_node_find(xpath
);
1003 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1004 "%s: unknown data path: %s", __func__
, xpath
);
1007 module
= yang_module_find(nb_node
->snode
->module
->name
);
1012 nvalues
+= listcount(arguments
);
1014 values
= XMALLOC(MTYPE_CONFD
, nvalues
* sizeof(*values
));
1016 CONFD_SET_TAG_XMLBEGIN(&values
[i
++], nb_node
->confd_hash
,
1017 module
->confd_hash
);
1018 for (ALL_LIST_ELEMENTS_RO(arguments
, node
, data
)) {
1019 struct nb_node
*nb_node_arg
;
1021 nb_node_arg
= nb_node_find(data
->xpath
);
1023 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1024 "%s: unknown data path: %s", __func__
,
1026 XFREE(MTYPE_CONFD
, values
);
1030 CONFD_SET_TAG_STR(&values
[i
++], nb_node_arg
->confd_hash
,
1033 CONFD_SET_TAG_XMLEND(&values
[i
++], nb_node
->confd_hash
,
1034 module
->confd_hash
);
1037 ret
= confd_notification_send(live_ctx
, &now
, values
, nvalues
);
1039 /* Release memory. */
1040 XFREE(MTYPE_CONFD
, values
);
1042 /* Map ConfD return code to northbound return code. */
1051 static int frr_confd_action_init(struct confd_user_info
*uinfo
)
1053 confd_action_set_fd(uinfo
, dp_worker_sock
);
1058 static int frr_confd_action_execute(struct confd_user_info
*uinfo
,
1059 struct xml_tag
*name
, confd_hkeypath_t
*kp
,
1060 confd_tag_value_t
*params
, int nparams
)
1063 struct nb_node
*nb_node
;
1065 struct list
*output
;
1066 struct yang_data
*data
;
1067 confd_tag_value_t
*reply
;
1070 /* Getting the XPath is tricky. */
1072 /* This is a YANG RPC. */
1073 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
1074 strlcat(xpath
, "/", sizeof(xpath
));
1075 strlcat(xpath
, confd_hash2str(name
->tag
), sizeof(xpath
));
1077 /* This is a YANG action. */
1078 snprintf(xpath
, sizeof(xpath
), "/%s:%s",
1079 confd_ns2prefix(name
->ns
), confd_hash2str(name
->tag
));
1082 nb_node
= nb_node_find(xpath
);
1084 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1085 "%s: unknown data path: %s", __func__
, xpath
);
1089 input
= yang_data_list_new();
1090 output
= yang_data_list_new();
1092 /* Process input nodes. */
1093 for (int i
= 0; i
< nparams
; i
++) {
1094 char xpath_input
[BUFSIZ
];
1095 char value_str
[YANG_VALUE_MAXLEN
];
1097 snprintf(xpath_input
, sizeof(xpath_input
), "%s/%s", xpath
,
1098 confd_hash2str(params
[i
].tag
.tag
));
1100 if (frr_confd_val2str(xpath_input
, ¶ms
[i
].v
, value_str
,
1104 EC_LIB_CONFD_DATA_CONVERT
,
1105 "%s: failed to convert ConfD value to a string",
1111 data
= yang_data_new(xpath_input
, value_str
);
1112 listnode_add(input
, data
);
1115 /* Execute callback registered for this XPath. */
1116 if (nb_callback_rpc(nb_node
, xpath
, input
, output
) != NB_OK
) {
1117 flog_warn(EC_LIB_NB_CB_RPC
, "%s: rpc callback failed: %s",
1123 /* Process output nodes. */
1124 if (listcount(output
) > 0) {
1125 struct listnode
*node
;
1128 reply
= XMALLOC(MTYPE_CONFD
,
1129 listcount(output
) * sizeof(*reply
));
1131 for (ALL_LIST_ELEMENTS_RO(output
, node
, data
)) {
1132 struct nb_node
*nb_node_output
;
1135 nb_node_output
= nb_node_find(data
->xpath
);
1136 if (!nb_node_output
) {
1137 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1138 "%s: unknown data path: %s", __func__
,
1143 hash
= confd_str2hash(nb_node_output
->snode
->name
);
1144 CONFD_SET_TAG_STR(&reply
[i
++], hash
, data
->value
);
1146 confd_action_reply_values(uinfo
, reply
, listcount(output
));
1147 XFREE(MTYPE_CONFD
, reply
);
1151 /* Release memory. */
1152 list_delete(&input
);
1153 list_delete(&output
);
1159 static int frr_confd_dp_read(struct thread
*thread
)
1161 struct confd_daemon_ctx
*dctx
= THREAD_ARG(thread
);
1162 int fd
= THREAD_FD(thread
);
1166 thread_add_read(master
, frr_confd_dp_read
, dctx
, fd
, &thread
);
1168 ret
= confd_fd_ready(dctx
, fd
);
1169 if (ret
== CONFD_EOF
) {
1170 flog_err_confd("confd_fd_ready");
1173 } else if (ret
== CONFD_ERR
&& confd_errno
!= CONFD_ERR_EXTERNAL
) {
1174 flog_err_confd("confd_fd_ready");
1182 static int frr_confd_subscribe_state(const struct lys_node
*snode
, void *arg
)
1184 struct nb_node
*nb_node
= snode
->priv
;
1185 struct confd_data_cbs
*data_cbs
= arg
;
1187 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
1188 return YANG_ITER_CONTINUE
;
1189 /* We only need to subscribe to the root of the state subtrees. */
1190 if (snode
->parent
&& CHECK_FLAG(snode
->parent
->flags
, LYS_CONFIG_R
))
1191 return YANG_ITER_CONTINUE
;
1193 DEBUGD(&nb_dbg_client_confd
,
1194 "%s: providing data to '%s' (callpoint %s)", __func__
,
1195 nb_node
->xpath
, snode
->name
);
1197 strlcpy(data_cbs
->callpoint
, snode
->name
, sizeof(data_cbs
->callpoint
));
1198 if (confd_register_data_cb(dctx
, data_cbs
) != CONFD_OK
)
1199 flog_err_confd("confd_register_data_cb");
1201 return YANG_ITER_CONTINUE
;
1204 static int frr_confd_init_dp(const char *program_name
)
1206 struct confd_trans_cbs trans_cbs
;
1207 struct confd_data_cbs data_cbs
;
1208 struct confd_notification_stream_cbs ncbs
;
1209 struct confd_action_cbs acbs
;
1211 /* Initialize daemon context. */
1212 dctx
= confd_init_daemon(program_name
);
1214 flog_err_confd("confd_init_daemon");
1219 * Inform we want to receive YANG values as raw strings, and that we
1220 * want to provide only strings in the reply functions, regardless of
1223 confd_set_daemon_flags(dctx
, CONFD_DAEMON_FLAG_STRINGSONLY
);
1225 /* Establish a control socket. */
1226 dp_ctl_sock
= socket(PF_INET
, SOCK_STREAM
, 0);
1227 if (dp_ctl_sock
< 0) {
1228 flog_err(EC_LIB_SOCKET
, "%s: failed to create socket: %s",
1229 __func__
, safe_strerror(errno
));
1233 if (confd_connect(dctx
, dp_ctl_sock
, CONTROL_SOCKET
, &confd_addr
,
1234 sizeof(struct sockaddr_in
))
1236 flog_err_confd("confd_connect");
1241 * Establish a worker socket (only one since this plugin runs on a
1244 dp_worker_sock
= socket(PF_INET
, SOCK_STREAM
, 0);
1245 if (dp_worker_sock
< 0) {
1246 flog_err(EC_LIB_SOCKET
, "%s: failed to create socket: %s",
1247 __func__
, safe_strerror(errno
));
1250 if (confd_connect(dctx
, dp_worker_sock
, WORKER_SOCKET
, &confd_addr
,
1251 sizeof(struct sockaddr_in
))
1253 flog_err_confd("confd_connect");
1257 /* Register transaction callback functions. */
1258 memset(&trans_cbs
, 0, sizeof(trans_cbs
));
1259 trans_cbs
.init
= frr_confd_transaction_init
;
1260 confd_register_trans_cb(dctx
, &trans_cbs
);
1262 /* Register our read/write callbacks. */
1263 memset(&data_cbs
, 0, sizeof(data_cbs
));
1264 data_cbs
.get_elem
= frr_confd_data_get_elem
;
1265 data_cbs
.exists_optional
= frr_confd_data_get_elem
;
1266 data_cbs
.get_next
= frr_confd_data_get_next
;
1267 data_cbs
.get_object
= frr_confd_data_get_object
;
1268 data_cbs
.get_next_object
= frr_confd_data_get_next_object
;
1271 * Iterate over all loaded YANG modules and subscribe to the paths
1272 * referent to state data.
1274 yang_snodes_iterate_all(frr_confd_subscribe_state
, 0, &data_cbs
);
1276 /* Register notification stream. */
1277 memset(&ncbs
, 0, sizeof(ncbs
));
1278 ncbs
.fd
= dp_worker_sock
;
1280 * RFC 5277 - Section 3.2.3:
1281 * A NETCONF server implementation supporting the notification
1282 * capability MUST support the "NETCONF" notification event
1283 * stream. This stream contains all NETCONF XML event notifications
1284 * supported by the NETCONF server.
1286 strlcpy(ncbs
.streamname
, "NETCONF", sizeof(ncbs
.streamname
));
1287 if (confd_register_notification_stream(dctx
, &ncbs
, &live_ctx
)
1289 flog_err_confd("confd_register_notification_stream");
1293 /* Register the action handler callback. */
1294 memset(&acbs
, 0, sizeof(acbs
));
1295 strlcpy(acbs
.actionpoint
, "actionpoint", sizeof(acbs
.actionpoint
));
1296 acbs
.init
= frr_confd_action_init
;
1297 acbs
.action
= frr_confd_action_execute
;
1298 if (confd_register_action_cbs(dctx
, &acbs
) != CONFD_OK
) {
1299 flog_err_confd("confd_register_action_cbs");
1303 /* Notify we registered all callbacks we wanted. */
1304 if (confd_register_done(dctx
) != CONFD_OK
) {
1305 flog_err_confd("confd_register_done");
1309 thread_add_read(master
, frr_confd_dp_read
, dctx
, dp_ctl_sock
,
1311 thread_add_read(master
, frr_confd_dp_read
, dctx
, dp_worker_sock
,
1317 frr_confd_finish_dp();
1322 static void frr_confd_finish_dp(void)
1324 if (dp_worker_sock
> 0) {
1325 THREAD_OFF(t_dp_worker
);
1326 close(dp_worker_sock
);
1328 if (dp_ctl_sock
> 0) {
1329 THREAD_OFF(t_dp_ctl
);
1333 confd_release_daemon(dctx
);
1336 /* ------------ CLI ------------ */
1338 DEFUN (debug_nb_confd
,
1340 "[no] debug northbound client confd",
1343 "Northbound debugging\n"
1347 uint32_t mode
= DEBUG_NODE2MODE(vty
->node
);
1348 bool no
= strmatch(argv
[0]->text
, "no");
1350 DEBUG_MODE_SET(&nb_dbg_client_confd
, mode
, !no
);
1355 static int frr_confd_debug_config_write(struct vty
*vty
)
1357 if (DEBUG_MODE_CHECK(&nb_dbg_client_confd
, DEBUG_MODE_CONF
))
1358 vty_out(vty
, "debug northbound client confd\n");
1363 static int frr_confd_debug_set_all(uint32_t flags
, bool set
)
1365 DEBUG_FLAGS_SET(&nb_dbg_client_confd
, flags
, set
);
1367 /* If all modes have been turned off, don't preserve options. */
1368 if (!DEBUG_MODE_CHECK(&nb_dbg_client_confd
, DEBUG_MODE_ALL
))
1369 DEBUG_CLEAR(&nb_dbg_client_confd
);
1374 static void frr_confd_cli_init(void)
1376 hook_register(nb_client_debug_config_write
,
1377 frr_confd_debug_config_write
);
1378 hook_register(nb_client_debug_set_all
, frr_confd_debug_set_all
);
1380 install_element(ENABLE_NODE
, &debug_nb_confd_cmd
);
1381 install_element(CONFIG_NODE
, &debug_nb_confd_cmd
);
1384 /* ------------ Main ------------ */
1386 static int frr_confd_calculate_snode_hash(const struct lys_node
*snode
,
1389 struct nb_node
*nb_node
= snode
->priv
;
1391 nb_node
->confd_hash
= confd_str2hash(snode
->name
);
1393 return YANG_ITER_CONTINUE
;
1396 static int frr_confd_init(const char *program_name
)
1398 struct sockaddr_in
*confd_addr4
= (struct sockaddr_in
*)&confd_addr
;
1399 int debuglevel
= CONFD_SILENT
;
1402 /* Initialize ConfD library. */
1403 confd_init(program_name
, stderr
, debuglevel
);
1405 confd_addr4
->sin_family
= AF_INET
;
1406 confd_addr4
->sin_addr
.s_addr
= inet_addr("127.0.0.1");
1407 confd_addr4
->sin_port
= htons(CONFD_PORT
);
1408 if (confd_load_schemas(&confd_addr
, sizeof(struct sockaddr_in
))
1410 flog_err_confd("confd_load_schemas");
1414 ret
= frr_confd_init_cdb();
1418 ret
= frr_confd_init_dp(program_name
);
1420 frr_confd_finish_cdb();
1424 yang_snodes_iterate_all(frr_confd_calculate_snode_hash
, 0, NULL
);
1426 hook_register(nb_notification_send
, frr_confd_notification_send
);
1428 confd_connected
= true;
1432 confd_free_schemas();
1437 static int frr_confd_finish(void)
1439 if (!confd_connected
)
1442 frr_confd_finish_cdb();
1443 frr_confd_finish_dp();
1445 confd_free_schemas();
1447 confd_connected
= false;
1452 static int frr_confd_module_late_init(struct thread_master
*tm
)
1456 if (frr_confd_init(frr_get_progname()) < 0) {
1457 flog_err(EC_LIB_CONFD_INIT
,
1458 "failed to initialize the ConfD module");
1462 hook_register(frr_fini
, frr_confd_finish
);
1463 frr_confd_cli_init();
1468 static int frr_confd_module_init(void)
1470 hook_register(frr_late_init
, frr_confd_module_late_init
);
1475 FRR_MODULE_SETUP(.name
= "frr_confd", .version
= FRR_VERSION
,
1476 .description
= "FRR ConfD integration module",
1477 .init
= frr_confd_module_init
, )