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 pthread_rwlock_rdlock(&running_config
->lock
);
294 candidate
= nb_config_dup(running_config
);
296 pthread_rwlock_unlock(&running_config
->lock
);
298 /* Iterate over all configuration changes. */
299 iter_args
.candidate
= candidate
;
300 iter_args
.error
= false;
301 for (int i
= 0; i
< reslen
; i
++) {
302 if (cdb_diff_iterate(fd
, subp
[i
], frr_confd_cdb_diff_iter
,
303 ITER_WANT_PREV
, &iter_args
)
305 flog_err_confd("cdb_diff_iterate");
310 if (iter_args
.error
) {
311 nb_config_free(candidate
);
313 if (cdb_sub_abort_trans(
314 cdb_sub_sock
, CONFD_ERRCODE_APPLICATION_INTERNAL
, 0,
315 0, "Couldn't apply configuration changes")
317 flog_err_confd("cdb_sub_abort_trans");
324 * Validate the configuration changes and allocate all resources
325 * required to apply them.
328 ret
= nb_candidate_commit_prepare(candidate
, NB_CLIENT_CONFD
, NULL
,
330 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
) {
331 enum confd_errcode errcode
;
336 errcode
= CONFD_ERRCODE_IN_USE
;
337 errmsg
= "Configuration is locked by another process";
339 case NB_ERR_RESOURCE
:
340 errcode
= CONFD_ERRCODE_RESOURCE_DENIED
;
341 errmsg
= "Failed do allocate resources";
344 errcode
= CONFD_ERRCODE_INTERNAL
;
345 errmsg
= "Internal error";
349 /* Reject the configuration changes. */
350 if (cdb_sub_abort_trans(cdb_sub_sock
, errcode
, 0, 0, "%s",
353 flog_err_confd("cdb_sub_abort_trans");
357 /* Acknowledge the notification. */
358 if (cdb_sync_subscription_socket(fd
, CDB_DONE_PRIORITY
)
360 flog_err_confd("cdb_sync_subscription_socket");
364 /* No configuration changes. */
366 nb_config_free(candidate
);
372 static int frr_confd_cdb_read_cb_commit(int fd
, int *subp
, int reslen
)
375 * No need to process the configuration changes again as we're already
376 * keeping track of them in the "transaction" variable.
380 /* Apply the transaction. */
382 struct nb_config
*candidate
= transaction
->config
;
384 nb_candidate_commit_apply(transaction
, true, NULL
);
385 nb_config_free(candidate
);
388 /* Acknowledge the notification. */
389 if (cdb_sync_subscription_socket(fd
, CDB_DONE_PRIORITY
) != CONFD_OK
) {
390 flog_err_confd("cdb_sync_subscription_socket");
397 static int frr_confd_cdb_read_cb_abort(int fd
, int *subp
, int reslen
)
400 * No need to process the configuration changes again as we're already
401 * keeping track of them in the "transaction" variable.
405 /* Abort the transaction. */
407 struct nb_config
*candidate
= transaction
->config
;
409 nb_candidate_commit_abort(transaction
);
410 nb_config_free(candidate
);
413 /* Acknowledge the notification. */
414 if (cdb_sync_subscription_socket(fd
, CDB_DONE_PRIORITY
) != CONFD_OK
) {
415 flog_err_confd("cdb_sync_subscription_socket");
422 static int frr_confd_cdb_read_cb(struct thread
*thread
)
424 int fd
= THREAD_FD(thread
);
425 enum cdb_sub_notification cdb_ev
;
431 thread_add_read(master
, frr_confd_cdb_read_cb
, NULL
, fd
, &thread
);
433 if (cdb_read_subscription_socket2(fd
, &cdb_ev
, &flags
, &subp
, &reslen
)
435 flog_err_confd("cdb_read_subscription_socket2");
440 case CDB_SUB_PREPARE
:
441 return frr_confd_cdb_read_cb_prepare(fd
, subp
, reslen
);
443 return frr_confd_cdb_read_cb_commit(fd
, subp
, reslen
);
445 return frr_confd_cdb_read_cb_abort(fd
, subp
, reslen
);
447 flog_err_confd("unknown CDB event");
452 /* Trigger CDB subscriptions to read the startup configuration. */
453 static void *thread_cdb_trigger_subscriptions(void *data
)
456 int *sub_points
= NULL
, len
= 0;
457 struct listnode
*node
;
461 /* Create CDB data socket. */
462 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
464 flog_err(EC_LIB_SOCKET
, "%s: failed to create socket: %s",
465 __func__
, safe_strerror(errno
));
469 if (cdb_connect(sock
, CDB_DATA_SOCKET
, &confd_addr
,
470 sizeof(struct sockaddr_in
))
472 flog_err_confd("cdb_connect");
477 * Fill array containing the subscription point of all loaded YANG
480 len
= listcount(confd_spoints
);
481 sub_points
= XCALLOC(MTYPE_CONFD
, len
* sizeof(int));
482 for (ALL_LIST_ELEMENTS_RO(confd_spoints
, node
, spoint
))
483 sub_points
[i
++] = *spoint
;
485 if (cdb_trigger_subscriptions(sock
, sub_points
, len
) != CONFD_OK
) {
486 flog_err_confd("cdb_trigger_subscriptions");
490 /* Cleanup and exit thread. */
491 XFREE(MTYPE_CONFD
, sub_points
);
497 static int frr_confd_init_cdb(void)
499 struct yang_module
*module
;
500 pthread_t cdb_trigger_thread
;
502 /* Create CDB subscription socket. */
503 cdb_sub_sock
= socket(PF_INET
, SOCK_STREAM
, 0);
504 if (cdb_sub_sock
< 0) {
505 flog_err(EC_LIB_SOCKET
, "%s: failed to create socket: %s",
506 __func__
, safe_strerror(errno
));
510 if (cdb_connect(cdb_sub_sock
, CDB_SUBSCRIPTION_SOCKET
, &confd_addr
,
511 sizeof(struct sockaddr_in
))
513 flog_err_confd("cdb_connect");
517 /* Subscribe to all loaded YANG data modules. */
518 confd_spoints
= list_new();
519 RB_FOREACH (module
, yang_modules
, &yang_modules
) {
520 struct lys_node
*snode
;
522 module
->confd_hash
= confd_str2hash(module
->info
->ns
);
523 if (module
->confd_hash
== 0) {
526 "%s: failed to find hash value for namespace %s",
527 __func__
, module
->info
->ns
);
532 * The CDB API doesn't provide a mechanism to subscribe to an
533 * entire YANG module. So we have to find the top level
534 * nodes ourselves and subscribe to their paths.
536 LY_TREE_FOR (module
->info
->data
, snode
) {
537 struct nb_node
*nb_node
;
541 switch (snode
->nodetype
) {
551 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
554 nb_node
= snode
->priv
;
555 DEBUGD(&nb_dbg_client_confd
, "%s: subscribing to '%s'",
556 __func__
, nb_node
->xpath
);
558 spoint
= XMALLOC(MTYPE_CONFD
, sizeof(*spoint
));
559 ret
= cdb_subscribe2(
560 cdb_sub_sock
, CDB_SUB_RUNNING_TWOPHASE
,
561 CDB_SUB_WANT_ABORT_ON_ABORT
, 3, spoint
,
562 module
->confd_hash
, nb_node
->xpath
);
563 if (ret
!= CONFD_OK
) {
564 flog_err_confd("cdb_subscribe2");
565 XFREE(MTYPE_CONFD
, spoint
);
567 listnode_add(confd_spoints
, spoint
);
571 if (cdb_subscribe_done(cdb_sub_sock
) != CONFD_OK
) {
572 flog_err_confd("cdb_subscribe_done");
576 /* Create short lived pthread to trigger the CDB subscriptions. */
577 if (pthread_create(&cdb_trigger_thread
, NULL
,
578 thread_cdb_trigger_subscriptions
, NULL
)) {
579 flog_err(EC_LIB_SYSTEM_CALL
, "%s: error creating pthread: %s",
580 __func__
, safe_strerror(errno
));
583 pthread_detach(cdb_trigger_thread
);
585 thread_add_read(master
, frr_confd_cdb_read_cb
, NULL
, cdb_sub_sock
,
591 frr_confd_finish_cdb();
596 static void frr_confd_finish_cdb(void)
598 if (cdb_sub_sock
> 0) {
599 THREAD_OFF(t_cdb_sub
);
600 cdb_close(cdb_sub_sock
);
604 /* ------------ DP code ------------ */
606 static int frr_confd_transaction_init(struct confd_trans_ctx
*tctx
)
608 confd_trans_set_fd(tctx
, dp_worker_sock
);
613 #define CONFD_MAX_CHILD_NODES 32
615 static int frr_confd_data_get_elem(struct confd_trans_ctx
*tctx
,
616 confd_hkeypath_t
*kp
)
618 struct nb_node
*nb_node
;
620 struct yang_data
*data
;
622 const void *list_entry
= NULL
;
624 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
626 nb_node
= nb_node_find(xpath
);
628 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
629 "%s: unknown data path: %s", __func__
, xpath
);
630 confd_data_reply_not_found(tctx
);
634 if (frr_confd_hkeypath_get_list_entry(kp
, nb_node
, &list_entry
) != 0) {
635 confd_data_reply_not_found(tctx
);
639 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
642 CONFD_SET_STR(&v
, data
->value
);
643 confd_data_reply_value(tctx
, &v
);
645 confd_data_reply_found(tctx
);
646 yang_data_free(data
);
648 confd_data_reply_not_found(tctx
);
653 static int frr_confd_data_get_next(struct confd_trans_ctx
*tctx
,
654 confd_hkeypath_t
*kp
, long next
)
656 struct nb_node
*nb_node
;
658 struct yang_data
*data
;
659 const void *parent_list_entry
, *nb_next
;
660 confd_value_t v
[LIST_MAXKEYS
];
662 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
664 nb_node
= nb_node_find(xpath
);
666 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
667 "%s: unknown data path: %s", __func__
, xpath
);
668 confd_data_reply_next_key(tctx
, NULL
, -1, -1);
672 if (frr_confd_hkeypath_get_list_entry(kp
, nb_node
, &parent_list_entry
)
674 /* List entry doesn't exist anymore. */
675 confd_data_reply_next_key(tctx
, NULL
, -1, -1);
679 nb_next
= nb_callback_get_next(nb_node
, parent_list_entry
,
680 (next
== -1) ? NULL
: (void *)next
);
682 /* End of the list or leaf-list. */
683 confd_data_reply_next_key(tctx
, NULL
, -1, -1);
687 switch (nb_node
->snode
->nodetype
) {
689 if (!CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
)) {
690 struct yang_list_keys keys
;
692 memset(&keys
, 0, sizeof(keys
));
693 if (nb_callback_get_keys(nb_node
, nb_next
, &keys
)
695 flog_warn(EC_LIB_NB_CB_STATE
,
696 "%s: failed to get list keys",
698 confd_data_reply_next_key(tctx
, NULL
, -1, -1);
702 /* Feed keys to ConfD. */
703 for (size_t i
= 0; i
< keys
.num
; i
++)
704 CONFD_SET_STR(&v
[i
], keys
.key
[i
]);
705 confd_data_reply_next_key(tctx
, v
, keys
.num
,
708 char pointer_str
[16];
711 * ConfD 6.6 user guide, chapter 6.11 (Operational data
712 * lists without keys):
713 * "To support this without having completely separate
714 * APIs, we use a "pseudo" key in the ConfD APIs for
715 * this type of list. This key is not part of the data
716 * model, and completely hidden in the northbound agent
717 * interfaces, but is used with e.g. the get_next() and
718 * get_elem() callbacks as if it were a normal key. This
719 * "pseudo" key is always a single signed 64-bit
720 * integer, i.e. the confd_value_t type is C_INT64. The
721 * values can be chosen arbitrarily by the application,
722 * as long as a key value returned by get_next() can be
723 * used to get the data for the corresponding list entry
724 * with get_elem() or get_object() as usual. It could
725 * e.g. be an index into an array that holds the data,
726 * or even a memory address in integer form".
728 * Since we're using the CONFD_DAEMON_FLAG_STRINGSONLY
729 * option, we must convert our pseudo-key (a void
730 * pointer) to a string before sending it to confd.
732 snprintf(pointer_str
, sizeof(pointer_str
), "%lu",
733 (unsigned long)nb_next
);
734 CONFD_SET_STR(&v
[0], pointer_str
);
735 confd_data_reply_next_key(tctx
, v
, 1, (long)nb_next
);
739 data
= nb_callback_get_elem(nb_node
, xpath
, nb_next
);
742 CONFD_SET_STR(&v
[0], data
->value
);
743 confd_data_reply_next_key(tctx
, v
, 1,
746 yang_data_free(data
);
748 confd_data_reply_next_key(tctx
, NULL
, -1, -1);
758 * Optional callback - implemented for performance reasons.
760 static int frr_confd_data_get_object(struct confd_trans_ctx
*tctx
,
761 confd_hkeypath_t
*kp
)
763 struct nb_node
*nb_node
;
764 const struct lys_node
*child
;
766 char xpath_child
[XPATH_MAXLEN
];
767 struct list
*elements
;
768 struct yang_data
*data
;
769 const void *list_entry
;
770 confd_value_t values
[CONFD_MAX_CHILD_NODES
];
773 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
775 nb_node
= nb_node_find(xpath
);
777 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
778 "%s: unknown data path: %s", __func__
, xpath
);
779 confd_data_reply_not_found(tctx
);
783 if (frr_confd_hkeypath_get_list_entry(kp
, nb_node
, &list_entry
) != 0) {
784 confd_data_reply_not_found(tctx
);
788 elements
= yang_data_list_new();
790 /* Loop through list child nodes. */
791 LY_TREE_FOR (nb_node
->snode
->child
, child
) {
792 struct nb_node
*nb_node_child
= child
->priv
;
795 if (nvalues
> CONFD_MAX_CHILD_NODES
)
798 v
= &values
[nvalues
++];
800 /* Non-presence containers, lists and leaf-lists. */
801 if (!nb_node_child
->cbs
.get_elem
) {
802 CONFD_SET_NOEXISTS(v
);
806 snprintf(xpath_child
, sizeof(xpath_child
), "%s/%s", xpath
,
808 data
= nb_callback_get_elem(nb_node_child
, xpath_child
,
812 CONFD_SET_STR(v
, data
->value
);
814 /* Presence containers and empty leafs. */
816 v
, nb_node_child
->confd_hash
,
817 confd_str2hash(nb_node_child
->snode
820 listnode_add(elements
, data
);
822 CONFD_SET_NOEXISTS(v
);
825 confd_data_reply_value_array(tctx
, values
, nvalues
);
827 /* Release memory. */
828 list_delete(&elements
);
834 * Optional callback - implemented for performance reasons.
836 static int frr_confd_data_get_next_object(struct confd_trans_ctx
*tctx
,
837 confd_hkeypath_t
*kp
, long next
)
840 struct nb_node
*nb_node
;
841 struct list
*elements
;
842 const void *parent_list_entry
;
844 #define CONFD_OBJECTS_PER_TIME 100
845 struct confd_next_object objects
[CONFD_OBJECTS_PER_TIME
+ 1];
846 char pseudo_keys
[CONFD_OBJECTS_PER_TIME
][16];
849 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
851 nb_node
= nb_node_find(xpath
);
853 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
854 "%s: unknown data path: %s", __func__
, xpath
);
855 confd_data_reply_next_object_array(tctx
, NULL
, 0, 0);
859 if (frr_confd_hkeypath_get_list_entry(kp
, nb_node
, &parent_list_entry
)
861 confd_data_reply_next_object_array(tctx
, NULL
, 0, 0);
865 elements
= yang_data_list_new();
866 nb_next
= (next
== -1) ? NULL
: (void *)next
;
868 memset(objects
, 0, sizeof(objects
));
869 for (int j
= 0; j
< CONFD_OBJECTS_PER_TIME
; j
++) {
870 struct confd_next_object
*object
;
871 struct lys_node
*child
;
872 struct yang_data
*data
;
875 object
= &objects
[j
];
877 nb_next
= nb_callback_get_next(nb_node
, parent_list_entry
,
880 /* End of the list. */
883 object
->next
= (long)nb_next
;
885 /* Leaf-lists require special handling. */
886 if (nb_node
->snode
->nodetype
== LYS_LEAFLIST
) {
887 object
->v
= XMALLOC(MTYPE_CONFD
, sizeof(confd_value_t
));
888 data
= nb_callback_get_elem(nb_node
, xpath
, nb_next
);
889 assert(data
&& data
->value
);
890 CONFD_SET_STR(object
->v
, data
->value
);
892 listnode_add(elements
, data
);
898 CONFD_MAX_CHILD_NODES
* sizeof(confd_value_t
));
901 * ConfD 6.6 user guide, chapter 6.11 (Operational data lists
903 * "In the response to the get_next_object() callback, the data
904 * provider is expected to provide the key values along with the
905 * other leafs in an array that is populated according to the
906 * data model. This must be done also for this type of list,
907 * even though the key isn't actually in the data model. The
908 * "pseudo" key must always be the first element in the array".
910 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
)) {
913 snprintf(pseudo_keys
[j
], sizeof(pseudo_keys
[j
]), "%lu",
914 (unsigned long)nb_next
);
916 v
= &object
->v
[nvalues
++];
917 CONFD_SET_STR(v
, pseudo_keys
[j
]);
920 /* Loop through list child nodes. */
921 LY_TREE_FOR (nb_node
->snode
->child
, child
) {
922 struct nb_node
*nb_node_child
= child
->priv
;
923 char xpath_child
[XPATH_MAXLEN
];
926 if (nvalues
> CONFD_MAX_CHILD_NODES
)
929 v
= &object
->v
[nvalues
++];
931 /* Non-presence containers, lists and leaf-lists. */
932 if (!nb_node_child
->cbs
.get_elem
) {
933 CONFD_SET_NOEXISTS(v
);
937 snprintf(xpath_child
, sizeof(xpath_child
), "%s/%s",
939 data
= nb_callback_get_elem(nb_node_child
, xpath_child
,
943 CONFD_SET_STR(v
, data
->value
);
946 * Presence containers and empty leafs.
949 v
, nb_node_child
->confd_hash
,
954 listnode_add(elements
, data
);
956 CONFD_SET_NOEXISTS(v
);
964 confd_data_reply_next_object_array(tctx
, NULL
, 0, 0);
965 list_delete(&elements
);
969 /* Detect end of the list. */
972 objects
[nobjects
].v
= NULL
;
975 /* Reply to ConfD. */
976 confd_data_reply_next_object_arrays(tctx
, objects
, nobjects
, 0);
980 /* Release memory. */
981 list_delete(&elements
);
982 for (int j
= 0; j
< nobjects
; j
++) {
983 struct confd_next_object
*object
;
985 object
= &objects
[j
];
986 XFREE(MTYPE_CONFD
, object
->v
);
992 static int frr_confd_notification_send(const char *xpath
,
993 struct list
*arguments
)
995 struct nb_node
*nb_node
;
996 struct yang_module
*module
;
997 struct confd_datetime now
;
998 confd_tag_value_t
*values
;
1001 struct yang_data
*data
;
1002 struct listnode
*node
;
1005 nb_node
= nb_node_find(xpath
);
1007 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1008 "%s: unknown data path: %s", __func__
, xpath
);
1011 module
= yang_module_find(nb_node
->snode
->module
->name
);
1016 nvalues
+= listcount(arguments
);
1018 values
= XMALLOC(MTYPE_CONFD
, nvalues
* sizeof(*values
));
1020 CONFD_SET_TAG_XMLBEGIN(&values
[i
++], nb_node
->confd_hash
,
1021 module
->confd_hash
);
1022 for (ALL_LIST_ELEMENTS_RO(arguments
, node
, data
)) {
1023 struct nb_node
*nb_node_arg
;
1025 nb_node_arg
= nb_node_find(data
->xpath
);
1027 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1028 "%s: unknown data path: %s", __func__
,
1030 XFREE(MTYPE_CONFD
, values
);
1034 CONFD_SET_TAG_STR(&values
[i
++], nb_node_arg
->confd_hash
,
1037 CONFD_SET_TAG_XMLEND(&values
[i
++], nb_node
->confd_hash
,
1038 module
->confd_hash
);
1041 ret
= confd_notification_send(live_ctx
, &now
, values
, nvalues
);
1043 /* Release memory. */
1044 XFREE(MTYPE_CONFD
, values
);
1046 /* Map ConfD return code to northbound return code. */
1055 static int frr_confd_action_init(struct confd_user_info
*uinfo
)
1057 confd_action_set_fd(uinfo
, dp_worker_sock
);
1062 static int frr_confd_action_execute(struct confd_user_info
*uinfo
,
1063 struct xml_tag
*name
, confd_hkeypath_t
*kp
,
1064 confd_tag_value_t
*params
, int nparams
)
1067 struct nb_node
*nb_node
;
1069 struct list
*output
;
1070 struct yang_data
*data
;
1071 confd_tag_value_t
*reply
;
1074 /* Getting the XPath is tricky. */
1076 /* This is a YANG RPC. */
1077 frr_confd_get_xpath(kp
, xpath
, sizeof(xpath
));
1078 strlcat(xpath
, "/", sizeof(xpath
));
1079 strlcat(xpath
, confd_hash2str(name
->tag
), sizeof(xpath
));
1081 /* This is a YANG action. */
1082 snprintf(xpath
, sizeof(xpath
), "/%s:%s",
1083 confd_ns2prefix(name
->ns
), confd_hash2str(name
->tag
));
1086 nb_node
= nb_node_find(xpath
);
1088 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1089 "%s: unknown data path: %s", __func__
, xpath
);
1093 input
= yang_data_list_new();
1094 output
= yang_data_list_new();
1096 /* Process input nodes. */
1097 for (int i
= 0; i
< nparams
; i
++) {
1098 char xpath_input
[BUFSIZ
];
1099 char value_str
[YANG_VALUE_MAXLEN
];
1101 snprintf(xpath_input
, sizeof(xpath_input
), "%s/%s", xpath
,
1102 confd_hash2str(params
[i
].tag
.tag
));
1104 if (frr_confd_val2str(xpath_input
, ¶ms
[i
].v
, value_str
,
1108 EC_LIB_CONFD_DATA_CONVERT
,
1109 "%s: failed to convert ConfD value to a string",
1115 data
= yang_data_new(xpath_input
, value_str
);
1116 listnode_add(input
, data
);
1119 /* Execute callback registered for this XPath. */
1120 if (nb_callback_rpc(nb_node
, xpath
, input
, output
) != NB_OK
) {
1121 flog_warn(EC_LIB_NB_CB_RPC
, "%s: rpc callback failed: %s",
1127 /* Process output nodes. */
1128 if (listcount(output
) > 0) {
1129 struct listnode
*node
;
1132 reply
= XMALLOC(MTYPE_CONFD
,
1133 listcount(output
) * sizeof(*reply
));
1135 for (ALL_LIST_ELEMENTS_RO(output
, node
, data
)) {
1136 struct nb_node
*nb_node_output
;
1139 nb_node_output
= nb_node_find(data
->xpath
);
1140 if (!nb_node_output
) {
1141 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1142 "%s: unknown data path: %s", __func__
,
1147 hash
= confd_str2hash(nb_node_output
->snode
->name
);
1148 CONFD_SET_TAG_STR(&reply
[i
++], hash
, data
->value
);
1150 confd_action_reply_values(uinfo
, reply
, listcount(output
));
1151 XFREE(MTYPE_CONFD
, reply
);
1155 /* Release memory. */
1156 list_delete(&input
);
1157 list_delete(&output
);
1163 static int frr_confd_dp_read(struct thread
*thread
)
1165 struct confd_daemon_ctx
*dctx
= THREAD_ARG(thread
);
1166 int fd
= THREAD_FD(thread
);
1170 thread_add_read(master
, frr_confd_dp_read
, dctx
, fd
, &thread
);
1172 ret
= confd_fd_ready(dctx
, fd
);
1173 if (ret
== CONFD_EOF
) {
1174 flog_err_confd("confd_fd_ready");
1177 } else if (ret
== CONFD_ERR
&& confd_errno
!= CONFD_ERR_EXTERNAL
) {
1178 flog_err_confd("confd_fd_ready");
1186 static int frr_confd_subscribe_state(const struct lys_node
*snode
, void *arg
)
1188 struct nb_node
*nb_node
= snode
->priv
;
1189 struct confd_data_cbs
*data_cbs
= arg
;
1191 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
1192 return YANG_ITER_CONTINUE
;
1193 /* We only need to subscribe to the root of the state subtrees. */
1194 if (snode
->parent
&& CHECK_FLAG(snode
->parent
->flags
, LYS_CONFIG_R
))
1195 return YANG_ITER_CONTINUE
;
1197 DEBUGD(&nb_dbg_client_confd
,
1198 "%s: providing data to '%s' (callpoint %s)", __func__
,
1199 nb_node
->xpath
, snode
->name
);
1201 strlcpy(data_cbs
->callpoint
, snode
->name
, sizeof(data_cbs
->callpoint
));
1202 if (confd_register_data_cb(dctx
, data_cbs
) != CONFD_OK
)
1203 flog_err_confd("confd_register_data_cb");
1205 return YANG_ITER_CONTINUE
;
1208 static int frr_confd_init_dp(const char *program_name
)
1210 struct confd_trans_cbs trans_cbs
;
1211 struct confd_data_cbs data_cbs
;
1212 struct confd_notification_stream_cbs ncbs
;
1213 struct confd_action_cbs acbs
;
1215 /* Initialize daemon context. */
1216 dctx
= confd_init_daemon(program_name
);
1218 flog_err_confd("confd_init_daemon");
1223 * Inform we want to receive YANG values as raw strings, and that we
1224 * want to provide only strings in the reply functions, regardless of
1227 confd_set_daemon_flags(dctx
, CONFD_DAEMON_FLAG_STRINGSONLY
);
1229 /* Establish a control socket. */
1230 dp_ctl_sock
= socket(PF_INET
, SOCK_STREAM
, 0);
1231 if (dp_ctl_sock
< 0) {
1232 flog_err(EC_LIB_SOCKET
, "%s: failed to create socket: %s",
1233 __func__
, safe_strerror(errno
));
1237 if (confd_connect(dctx
, dp_ctl_sock
, CONTROL_SOCKET
, &confd_addr
,
1238 sizeof(struct sockaddr_in
))
1240 flog_err_confd("confd_connect");
1245 * Establish a worker socket (only one since this plugin runs on a
1248 dp_worker_sock
= socket(PF_INET
, SOCK_STREAM
, 0);
1249 if (dp_worker_sock
< 0) {
1250 flog_err(EC_LIB_SOCKET
, "%s: failed to create socket: %s",
1251 __func__
, safe_strerror(errno
));
1254 if (confd_connect(dctx
, dp_worker_sock
, WORKER_SOCKET
, &confd_addr
,
1255 sizeof(struct sockaddr_in
))
1257 flog_err_confd("confd_connect");
1261 /* Register transaction callback functions. */
1262 memset(&trans_cbs
, 0, sizeof(trans_cbs
));
1263 trans_cbs
.init
= frr_confd_transaction_init
;
1264 confd_register_trans_cb(dctx
, &trans_cbs
);
1266 /* Register our read/write callbacks. */
1267 memset(&data_cbs
, 0, sizeof(data_cbs
));
1268 data_cbs
.get_elem
= frr_confd_data_get_elem
;
1269 data_cbs
.exists_optional
= frr_confd_data_get_elem
;
1270 data_cbs
.get_next
= frr_confd_data_get_next
;
1271 data_cbs
.get_object
= frr_confd_data_get_object
;
1272 data_cbs
.get_next_object
= frr_confd_data_get_next_object
;
1275 * Iterate over all loaded YANG modules and subscribe to the paths
1276 * referent to state data.
1278 yang_snodes_iterate_all(frr_confd_subscribe_state
, 0, &data_cbs
);
1280 /* Register notification stream. */
1281 memset(&ncbs
, 0, sizeof(ncbs
));
1282 ncbs
.fd
= dp_worker_sock
;
1284 * RFC 5277 - Section 3.2.3:
1285 * A NETCONF server implementation supporting the notification
1286 * capability MUST support the "NETCONF" notification event
1287 * stream. This stream contains all NETCONF XML event notifications
1288 * supported by the NETCONF server.
1290 strlcpy(ncbs
.streamname
, "NETCONF", sizeof(ncbs
.streamname
));
1291 if (confd_register_notification_stream(dctx
, &ncbs
, &live_ctx
)
1293 flog_err_confd("confd_register_notification_stream");
1297 /* Register the action handler callback. */
1298 memset(&acbs
, 0, sizeof(acbs
));
1299 strlcpy(acbs
.actionpoint
, "actionpoint", sizeof(acbs
.actionpoint
));
1300 acbs
.init
= frr_confd_action_init
;
1301 acbs
.action
= frr_confd_action_execute
;
1302 if (confd_register_action_cbs(dctx
, &acbs
) != CONFD_OK
) {
1303 flog_err_confd("confd_register_action_cbs");
1307 /* Notify we registered all callbacks we wanted. */
1308 if (confd_register_done(dctx
) != CONFD_OK
) {
1309 flog_err_confd("confd_register_done");
1313 thread_add_read(master
, frr_confd_dp_read
, dctx
, dp_ctl_sock
,
1315 thread_add_read(master
, frr_confd_dp_read
, dctx
, dp_worker_sock
,
1321 frr_confd_finish_dp();
1326 static void frr_confd_finish_dp(void)
1328 if (dp_worker_sock
> 0) {
1329 THREAD_OFF(t_dp_worker
);
1330 close(dp_worker_sock
);
1332 if (dp_ctl_sock
> 0) {
1333 THREAD_OFF(t_dp_ctl
);
1337 confd_release_daemon(dctx
);
1340 /* ------------ CLI ------------ */
1342 DEFUN (debug_nb_confd
,
1344 "[no] debug northbound client confd",
1347 "Northbound debugging\n"
1351 uint32_t mode
= DEBUG_NODE2MODE(vty
->node
);
1352 bool no
= strmatch(argv
[0]->text
, "no");
1354 DEBUG_MODE_SET(&nb_dbg_client_confd
, mode
, !no
);
1359 static int frr_confd_debug_config_write(struct vty
*vty
)
1361 if (DEBUG_MODE_CHECK(&nb_dbg_client_confd
, DEBUG_MODE_CONF
))
1362 vty_out(vty
, "debug northbound client confd\n");
1367 static int frr_confd_debug_set_all(uint32_t flags
, bool set
)
1369 DEBUG_FLAGS_SET(&nb_dbg_client_confd
, flags
, set
);
1371 /* If all modes have been turned off, don't preserve options. */
1372 if (!DEBUG_MODE_CHECK(&nb_dbg_client_confd
, DEBUG_MODE_ALL
))
1373 DEBUG_CLEAR(&nb_dbg_client_confd
);
1378 static void frr_confd_cli_init(void)
1380 hook_register(nb_client_debug_config_write
,
1381 frr_confd_debug_config_write
);
1382 hook_register(nb_client_debug_set_all
, frr_confd_debug_set_all
);
1384 install_element(ENABLE_NODE
, &debug_nb_confd_cmd
);
1385 install_element(CONFIG_NODE
, &debug_nb_confd_cmd
);
1388 /* ------------ Main ------------ */
1390 static int frr_confd_calculate_snode_hash(const struct lys_node
*snode
,
1393 struct nb_node
*nb_node
= snode
->priv
;
1395 nb_node
->confd_hash
= confd_str2hash(snode
->name
);
1397 return YANG_ITER_CONTINUE
;
1400 static int frr_confd_init(const char *program_name
)
1402 struct sockaddr_in
*confd_addr4
= (struct sockaddr_in
*)&confd_addr
;
1403 int debuglevel
= CONFD_SILENT
;
1406 /* Initialize ConfD library. */
1407 confd_init(program_name
, stderr
, debuglevel
);
1409 confd_addr4
->sin_family
= AF_INET
;
1410 confd_addr4
->sin_addr
.s_addr
= inet_addr("127.0.0.1");
1411 confd_addr4
->sin_port
= htons(CONFD_PORT
);
1412 if (confd_load_schemas(&confd_addr
, sizeof(struct sockaddr_in
))
1414 flog_err_confd("confd_load_schemas");
1418 ret
= frr_confd_init_cdb();
1422 ret
= frr_confd_init_dp(program_name
);
1424 frr_confd_finish_cdb();
1428 yang_snodes_iterate_all(frr_confd_calculate_snode_hash
, 0, NULL
);
1430 hook_register(nb_notification_send
, frr_confd_notification_send
);
1432 confd_connected
= true;
1436 confd_free_schemas();
1441 static int frr_confd_finish(void)
1443 if (!confd_connected
)
1446 frr_confd_finish_cdb();
1447 frr_confd_finish_dp();
1449 confd_free_schemas();
1451 confd_connected
= false;
1456 static int frr_confd_module_late_init(struct thread_master
*tm
)
1460 if (frr_confd_init(frr_get_progname()) < 0) {
1461 flog_err(EC_LIB_CONFD_INIT
,
1462 "failed to initialize the ConfD module");
1466 hook_register(frr_fini
, frr_confd_finish
);
1467 frr_confd_cli_init();
1472 static int frr_confd_module_init(void)
1474 hook_register(frr_late_init
, frr_confd_module_late_init
);
1479 FRR_MODULE_SETUP(.name
= "frr_confd", .version
= FRR_VERSION
,
1480 .description
= "FRR ConfD integration module",
1481 .init
= frr_confd_module_init
, )