]> git.proxmox.com Git - mirror_frr.git/blame - lib/northbound_confd.c
*: convert northbound callbacks to new error handling model
[mirror_frr.git] / lib / northbound_confd.c
CommitLineData
5bce33b3
RW
1/*
2 * Copyright (C) 2018 NetDEF, Inc.
3 * Renato Westphal
4 *
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)
8 * any later version.
9 *
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
13 * more details.
14 *
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
18 */
19
20#include <zebra.h>
21
22#include "log.h"
23#include "lib_errors.h"
24#include "command.h"
9eb2c0a1 25#include "debug.h"
5bce33b3
RW
26#include "libfrr.h"
27#include "version.h"
28#include "northbound.h"
29
30#include <confd_lib.h>
31#include <confd_cdb.h>
32#include <confd_dp.h>
33#include <confd_maapi.h>
34
35DEFINE_MTYPE_STATIC(LIB, CONFD, "ConfD module")
36
9eb2c0a1
RW
37static struct debug nb_dbg_client_confd = {0, "Northbound client: ConfD"};
38
5bce33b3
RW
39static struct thread_master *master;
40static struct sockaddr confd_addr;
41static int cdb_sub_sock, dp_ctl_sock, dp_worker_sock;
42static struct thread *t_cdb_sub, *t_dp_ctl, *t_dp_worker;
43static struct confd_daemon_ctx *dctx;
44static struct confd_notification_ctx *live_ctx;
45static bool confd_connected;
46static struct list *confd_spoints;
88a7d121 47static struct nb_transaction *transaction;
5bce33b3
RW
48
49static void frr_confd_finish_cdb(void);
50static void frr_confd_finish_dp(void);
51static int frr_confd_finish(void);
52
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, \
56 confd_lasterr())
57
58
59/* ------------ Utils ------------ */
60
61/* Get XPath string from ConfD hashed keypath. */
62static void frr_confd_get_xpath(const confd_hkeypath_t *kp, char *xpath,
63 size_t len)
64{
65 char *p;
66
67 confd_xpath_pp_kpath(xpath, len, 0, kp);
68
69 /*
70 * Replace double quotes by single quotes (the format accepted by the
71 * northbound API).
72 */
73 p = xpath;
74 while ((p = strchr(p, '"')) != NULL)
75 *p++ = '\'';
76}
77
78/* Convert ConfD binary value to a string. */
79static int frr_confd_val2str(const char *xpath, const confd_value_t *value,
80 char *string, size_t string_size)
81{
82 struct confd_cs_node *csp;
83
84 csp = confd_cs_node_cd(NULL, xpath);
85 if (!csp) {
86 flog_err_confd("confd_cs_node_cd");
87 return -1;
88 }
89 if (confd_val2str(csp->info.type, value, string, string_size)
90 == CONFD_ERR) {
91 flog_err_confd("confd_val2str");
92 return -1;
93 }
94
95 return 0;
96}
97
1a4bc045
RW
98/* Obtain list entry from ConfD hashed keypath. */
99static int frr_confd_hkeypath_get_list_entry(const confd_hkeypath_t *kp,
100 struct nb_node *nb_node,
101 const void **list_entry)
5bce33b3 102{
1a4bc045
RW
103 struct nb_node *nb_node_list;
104 int parent_lists = 0;
105 int curr_list = 0;
106
107 *list_entry = NULL;
108
109 /*
110 * Count the number of YANG lists in the path, disconsidering the
111 * last element.
112 */
113 nb_node_list = nb_node;
114 while (nb_node_list->parent_list) {
115 nb_node_list = nb_node_list->parent_list;
116 parent_lists++;
117 }
118 if (nb_node->snode->nodetype != LYS_LIST && parent_lists == 0)
119 return 0;
120
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;
124
125 /* Not a YANG list. */
5bce33b3
RW
126 if (kp->v[i][0].type != C_BUF)
127 continue;
128
1a4bc045
RW
129 /* Obtain list keys. */
130 memset(&keys, 0, sizeof(keys));
5bce33b3 131 for (int j = 0; kp->v[i][j].type != C_NOEXISTS; j++) {
1a4bc045 132 strlcpy(keys.key[keys.num],
5bce33b3 133 (char *)kp->v[i][j].val.buf.ptr,
1a4bc045
RW
134 sizeof(keys.key[keys.num]));
135 keys.num++;
5bce33b3 136 }
1a4bc045
RW
137
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;
142
143 /* Obtain list entry. */
99fb518f 144 if (!CHECK_FLAG(nb_node_list->flags, F_NB_NODE_KEYLESS_LIST)) {
9eb2c0a1
RW
145 *list_entry = nb_callback_lookup_entry(
146 nb_node, *list_entry, &keys);
99fb518f
RW
147 if (*list_entry == NULL)
148 return -1;
149 } else {
150 unsigned long ptr_ulong;
151
152 /* Retrieve list entry from pseudo-key (string). */
153 if (sscanf(keys.key[0], "%lu", &ptr_ulong) != 1)
154 return -1;
155 *list_entry = (const void *)ptr_ulong;
156 }
1a4bc045
RW
157
158 curr_list++;
5bce33b3 159 }
1a4bc045
RW
160
161 return 0;
5bce33b3
RW
162}
163
164/* Fill the current date and time into a confd_datetime structure. */
165static void getdatetime(struct confd_datetime *datetime)
166{
167 struct tm tm;
168 struct timeval tv;
169
170 gettimeofday(&tv, NULL);
171 gmtime_r(&tv.tv_sec, &tm);
172
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;
183}
184
185/* ------------ CDB code ------------ */
186
187struct cdb_iter_args {
188 struct nb_config *candidate;
189 bool error;
190};
191
192static enum cdb_iter_ret
193frr_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)
195{
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;
202 char *sb1, *sb2;
203 int ret;
204
205 frr_confd_get_xpath(kp, xpath, sizeof(xpath));
206
207 /*
208 * HACK: obtain value of leaf-list elements from the XPath due to
209 * a bug in the ConfD API.
210 */
211 value_str[0] = '\0';
212 sb1 = strrchr(xpath, '[');
213 sb2 = strrchr(xpath, ']');
214 if (sb1 && sb2 && !strchr(sb1, '=')) {
215 *sb2 = '\0';
216 strlcpy(value_str, sb1 + 1, sizeof(value_str));
217 *sb1 = '\0';
218 }
219
220 nb_node = nb_node_find(xpath);
221 if (!nb_node) {
222 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
223 "%s: unknown data path: %s", __func__, xpath);
224 iter_args->error = true;
225 return ITER_STOP;
226 }
227
228 /* Map operation values. */
229 switch (cdb_op) {
230 case MOP_CREATED:
231 nb_op = NB_OP_CREATE;
232 break;
233 case MOP_DELETED:
95ce849b 234 nb_op = NB_OP_DESTROY;
5bce33b3
RW
235 break;
236 case MOP_VALUE_SET:
237 if (nb_operation_is_valid(NB_OP_MODIFY, nb_node->snode))
238 nb_op = NB_OP_MODIFY;
239 else
240 /* Ignore list keys modifications. */
241 return ITER_RECURSE;
242 break;
243 case MOP_MOVED_AFTER:
244 nb_op = NB_OP_MOVE;
245 break;
246 case MOP_MODIFIED:
247 /* We're not interested on this. */
248 return ITER_RECURSE;
249 default:
250 flog_err(EC_LIB_DEVELOPMENT,
251 "%s: unexpected operation %u [xpath %s]", __func__,
252 cdb_op, xpath);
253 iter_args->error = true;
254 return ITER_STOP;
255 }
256
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,
260 sizeof(value_str))
261 != 0) {
262 flog_err(EC_LIB_CONFD_DATA_CONVERT,
263 "%s: failed to convert ConfD value to a string",
264 __func__);
265 iter_args->error = true;
266 return ITER_STOP;
267 }
268
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,
272 NULL, data);
273 yang_data_free(data);
274 if (ret != NB_OK) {
275 flog_warn(
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;
280 return ITER_STOP;
281 }
282
283 return ITER_RECURSE;
284}
285
88a7d121 286static int frr_confd_cdb_read_cb_prepare(int fd, int *subp, int reslen)
5bce33b3 287{
13d6b9c1 288 struct nb_context context = {};
5bce33b3
RW
289 struct nb_config *candidate;
290 struct cdb_iter_args iter_args;
df5eda3d 291 char errmsg[BUFSIZ] = {0};
5bce33b3
RW
292 int ret;
293
8685be73 294 candidate = nb_config_dup(running_config);
5bce33b3
RW
295
296 /* Iterate over all configuration changes. */
297 iter_args.candidate = candidate;
298 iter_args.error = false;
299 for (int i = 0; i < reslen; i++) {
300 if (cdb_diff_iterate(fd, subp[i], frr_confd_cdb_diff_iter,
301 ITER_WANT_PREV, &iter_args)
302 != CONFD_OK) {
303 flog_err_confd("cdb_diff_iterate");
304 }
305 }
306 free(subp);
307
308 if (iter_args.error) {
309 nb_config_free(candidate);
310
311 if (cdb_sub_abort_trans(
312 cdb_sub_sock, CONFD_ERRCODE_APPLICATION_INTERNAL, 0,
313 0, "Couldn't apply configuration changes")
314 != CONFD_OK) {
315 flog_err_confd("cdb_sub_abort_trans");
316 return -1;
317 }
318 return 0;
319 }
320
88a7d121
RW
321 /*
322 * Validate the configuration changes and allocate all resources
323 * required to apply them.
324 */
325 transaction = NULL;
13d6b9c1
RW
326 context.client = NB_CLIENT_CONFD;
327 ret = nb_candidate_commit_prepare(&context, candidate, NULL,
df5eda3d 328 &transaction, errmsg, sizeof(errmsg));
5bce33b3
RW
329 if (ret != NB_OK && ret != NB_ERR_NO_CHANGES) {
330 enum confd_errcode errcode;
5bce33b3
RW
331
332 switch (ret) {
333 case NB_ERR_LOCKED:
334 errcode = CONFD_ERRCODE_IN_USE;
5bce33b3
RW
335 break;
336 case NB_ERR_RESOURCE:
337 errcode = CONFD_ERRCODE_RESOURCE_DENIED;
5bce33b3
RW
338 break;
339 default:
df5eda3d 340 errcode = CONFD_ERRCODE_APPLICATION;
5bce33b3
RW
341 break;
342 }
343
88a7d121 344 /* Reject the configuration changes. */
5bce33b3
RW
345 if (cdb_sub_abort_trans(cdb_sub_sock, errcode, 0, 0, "%s",
346 errmsg)
347 != CONFD_OK) {
348 flog_err_confd("cdb_sub_abort_trans");
349 return -1;
350 }
351 } else {
88a7d121 352 /* Acknowledge the notification. */
5bce33b3
RW
353 if (cdb_sync_subscription_socket(fd, CDB_DONE_PRIORITY)
354 != CONFD_OK) {
355 flog_err_confd("cdb_sync_subscription_socket");
356 return -1;
357 }
88a7d121
RW
358
359 /* No configuration changes. */
360 if (!transaction)
361 nb_config_free(candidate);
362 }
363
364 return 0;
365}
366
367static int frr_confd_cdb_read_cb_commit(int fd, int *subp, int reslen)
368{
369 /*
370 * No need to process the configuration changes again as we're already
371 * keeping track of them in the "transaction" variable.
372 */
373 free(subp);
374
375 /* Apply the transaction. */
376 if (transaction) {
377 struct nb_config *candidate = transaction->config;
378
379 nb_candidate_commit_apply(transaction, true, NULL);
380 nb_config_free(candidate);
381 }
382
383 /* Acknowledge the notification. */
384 if (cdb_sync_subscription_socket(fd, CDB_DONE_PRIORITY) != CONFD_OK) {
385 flog_err_confd("cdb_sync_subscription_socket");
386 return -1;
387 }
388
389 return 0;
390}
391
392static int frr_confd_cdb_read_cb_abort(int fd, int *subp, int reslen)
393{
394 /*
395 * No need to process the configuration changes again as we're already
396 * keeping track of them in the "transaction" variable.
397 */
398 free(subp);
399
400 /* Abort the transaction. */
401 if (transaction) {
402 struct nb_config *candidate = transaction->config;
403
404 nb_candidate_commit_abort(transaction);
405 nb_config_free(candidate);
406 }
407
408 /* Acknowledge the notification. */
409 if (cdb_sync_subscription_socket(fd, CDB_DONE_PRIORITY) != CONFD_OK) {
410 flog_err_confd("cdb_sync_subscription_socket");
411 return -1;
5bce33b3
RW
412 }
413
414 return 0;
415}
416
88a7d121
RW
417static int frr_confd_cdb_read_cb(struct thread *thread)
418{
419 int fd = THREAD_FD(thread);
420 enum cdb_sub_notification cdb_ev;
421 int flags;
422 int *subp = NULL;
423 int reslen = 0;
424
425 thread = NULL;
426 thread_add_read(master, frr_confd_cdb_read_cb, NULL, fd, &thread);
427
428 if (cdb_read_subscription_socket2(fd, &cdb_ev, &flags, &subp, &reslen)
429 != CONFD_OK) {
430 flog_err_confd("cdb_read_subscription_socket2");
431 return -1;
432 }
433
434 switch (cdb_ev) {
435 case CDB_SUB_PREPARE:
436 return frr_confd_cdb_read_cb_prepare(fd, subp, reslen);
437 case CDB_SUB_COMMIT:
438 return frr_confd_cdb_read_cb_commit(fd, subp, reslen);
439 case CDB_SUB_ABORT:
440 return frr_confd_cdb_read_cb_abort(fd, subp, reslen);
441 default:
442 flog_err_confd("unknown CDB event");
443 return -1;
444 }
445}
446
5bce33b3
RW
447/* Trigger CDB subscriptions to read the startup configuration. */
448static void *thread_cdb_trigger_subscriptions(void *data)
449{
450 int sock;
451 int *sub_points = NULL, len = 0;
452 struct listnode *node;
453 int *spoint;
454 int i = 0;
455
456 /* Create CDB data socket. */
457 sock = socket(PF_INET, SOCK_STREAM, 0);
458 if (sock < 0) {
459 flog_err(EC_LIB_SOCKET, "%s: failed to create socket: %s",
460 __func__, safe_strerror(errno));
461 return NULL;
462 }
463
464 if (cdb_connect(sock, CDB_DATA_SOCKET, &confd_addr,
465 sizeof(struct sockaddr_in))
466 != CONFD_OK) {
467 flog_err_confd("cdb_connect");
468 return NULL;
469 }
470
471 /*
472 * Fill array containing the subscription point of all loaded YANG
473 * modules.
474 */
475 len = listcount(confd_spoints);
476 sub_points = XCALLOC(MTYPE_CONFD, len * sizeof(int));
477 for (ALL_LIST_ELEMENTS_RO(confd_spoints, node, spoint))
478 sub_points[i++] = *spoint;
479
480 if (cdb_trigger_subscriptions(sock, sub_points, len) != CONFD_OK) {
481 flog_err_confd("cdb_trigger_subscriptions");
482 return NULL;
483 }
484
485 /* Cleanup and exit thread. */
486 XFREE(MTYPE_CONFD, sub_points);
487 cdb_close(sock);
488
489 return NULL;
490}
491
492static int frr_confd_init_cdb(void)
493{
494 struct yang_module *module;
495 pthread_t cdb_trigger_thread;
496
497 /* Create CDB subscription socket. */
498 cdb_sub_sock = socket(PF_INET, SOCK_STREAM, 0);
499 if (cdb_sub_sock < 0) {
500 flog_err(EC_LIB_SOCKET, "%s: failed to create socket: %s",
501 __func__, safe_strerror(errno));
502 return -1;
503 }
504
505 if (cdb_connect(cdb_sub_sock, CDB_SUBSCRIPTION_SOCKET, &confd_addr,
506 sizeof(struct sockaddr_in))
507 != CONFD_OK) {
508 flog_err_confd("cdb_connect");
509 goto error;
510 }
511
512 /* Subscribe to all loaded YANG data modules. */
513 confd_spoints = list_new();
514 RB_FOREACH (module, yang_modules, &yang_modules) {
515 struct lys_node *snode;
516
517 module->confd_hash = confd_str2hash(module->info->ns);
518 if (module->confd_hash == 0) {
519 flog_err(
520 EC_LIB_LIBCONFD,
521 "%s: failed to find hash value for namespace %s",
522 __func__, module->info->ns);
523 goto error;
524 }
525
526 /*
527 * The CDB API doesn't provide a mechanism to subscribe to an
528 * entire YANG module. So we have to find the top level
529 * nodes ourselves and subscribe to their paths.
530 */
531 LY_TREE_FOR (module->info->data, snode) {
532 struct nb_node *nb_node;
533 int *spoint;
534 int ret;
535
536 switch (snode->nodetype) {
537 case LYS_CONTAINER:
538 case LYS_LEAF:
539 case LYS_LEAFLIST:
540 case LYS_LIST:
541 break;
542 default:
543 continue;
544 }
545
85cd3326
RW
546 if (CHECK_FLAG(snode->flags, LYS_CONFIG_R))
547 continue;
548
5bce33b3 549 nb_node = snode->priv;
9eb2c0a1
RW
550 DEBUGD(&nb_dbg_client_confd, "%s: subscribing to '%s'",
551 __func__, nb_node->xpath);
5bce33b3
RW
552
553 spoint = XMALLOC(MTYPE_CONFD, sizeof(*spoint));
554 ret = cdb_subscribe2(
555 cdb_sub_sock, CDB_SUB_RUNNING_TWOPHASE,
556 CDB_SUB_WANT_ABORT_ON_ABORT, 3, spoint,
557 module->confd_hash, nb_node->xpath);
558 if (ret != CONFD_OK) {
559 flog_err_confd("cdb_subscribe2");
560 XFREE(MTYPE_CONFD, spoint);
561 }
562 listnode_add(confd_spoints, spoint);
563 }
564 }
565
566 if (cdb_subscribe_done(cdb_sub_sock) != CONFD_OK) {
567 flog_err_confd("cdb_subscribe_done");
568 goto error;
569 }
570
571 /* Create short lived pthread to trigger the CDB subscriptions. */
572 if (pthread_create(&cdb_trigger_thread, NULL,
573 thread_cdb_trigger_subscriptions, NULL)) {
574 flog_err(EC_LIB_SYSTEM_CALL, "%s: error creating pthread: %s",
575 __func__, safe_strerror(errno));
576 goto error;
577 }
578 pthread_detach(cdb_trigger_thread);
579
580 thread_add_read(master, frr_confd_cdb_read_cb, NULL, cdb_sub_sock,
581 &t_cdb_sub);
582
583 return 0;
584
585error:
586 frr_confd_finish_cdb();
587
588 return -1;
589}
590
591static void frr_confd_finish_cdb(void)
592{
593 if (cdb_sub_sock > 0) {
594 THREAD_OFF(t_cdb_sub);
595 cdb_close(cdb_sub_sock);
596 }
597}
598
599/* ------------ DP code ------------ */
600
601static int frr_confd_transaction_init(struct confd_trans_ctx *tctx)
602{
603 confd_trans_set_fd(tctx, dp_worker_sock);
604
605 return CONFD_OK;
606}
607
1a4bc045
RW
608#define CONFD_MAX_CHILD_NODES 32
609
5bce33b3
RW
610static int frr_confd_data_get_elem(struct confd_trans_ctx *tctx,
611 confd_hkeypath_t *kp)
612{
1a4bc045 613 struct nb_node *nb_node;
5bce33b3 614 char xpath[BUFSIZ];
5bce33b3
RW
615 struct yang_data *data;
616 confd_value_t v;
617 const void *list_entry = NULL;
618
619 frr_confd_get_xpath(kp, xpath, sizeof(xpath));
620
621 nb_node = nb_node_find(xpath);
622 if (!nb_node) {
623 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
624 "%s: unknown data path: %s", __func__, xpath);
625 confd_data_reply_not_found(tctx);
626 return CONFD_OK;
627 }
628
1a4bc045
RW
629 if (frr_confd_hkeypath_get_list_entry(kp, nb_node, &list_entry) != 0) {
630 confd_data_reply_not_found(tctx);
631 return CONFD_OK;
5bce33b3
RW
632 }
633
9eb2c0a1 634 data = nb_callback_get_elem(nb_node, xpath, list_entry);
5bce33b3
RW
635 if (data) {
636 if (data->value) {
637 CONFD_SET_STR(&v, data->value);
638 confd_data_reply_value(tctx, &v);
639 } else
640 confd_data_reply_found(tctx);
641 yang_data_free(data);
642 } else
643 confd_data_reply_not_found(tctx);
644
645 return CONFD_OK;
646}
647
648static int frr_confd_data_get_next(struct confd_trans_ctx *tctx,
649 confd_hkeypath_t *kp, long next)
650{
651 struct nb_node *nb_node;
652 char xpath[BUFSIZ];
1a4bc045
RW
653 struct yang_data *data;
654 const void *parent_list_entry, *nb_next;
5bce33b3
RW
655 confd_value_t v[LIST_MAXKEYS];
656
657 frr_confd_get_xpath(kp, xpath, sizeof(xpath));
658
659 nb_node = nb_node_find(xpath);
660 if (!nb_node) {
661 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
662 "%s: unknown data path: %s", __func__, xpath);
663 confd_data_reply_next_key(tctx, NULL, -1, -1);
664 return CONFD_OK;
665 }
666
1a4bc045
RW
667 if (frr_confd_hkeypath_get_list_entry(kp, nb_node, &parent_list_entry)
668 != 0) {
669 /* List entry doesn't exist anymore. */
5bce33b3
RW
670 confd_data_reply_next_key(tctx, NULL, -1, -1);
671 return CONFD_OK;
672 }
1a4bc045 673
9eb2c0a1
RW
674 nb_next = nb_callback_get_next(nb_node, parent_list_entry,
675 (next == -1) ? NULL : (void *)next);
1a4bc045
RW
676 if (!nb_next) {
677 /* End of the list or leaf-list. */
5bce33b3
RW
678 confd_data_reply_next_key(tctx, NULL, -1, -1);
679 return CONFD_OK;
680 }
681
1a4bc045
RW
682 switch (nb_node->snode->nodetype) {
683 case LYS_LIST:
99fb518f
RW
684 if (!CHECK_FLAG(nb_node->flags, F_NB_NODE_KEYLESS_LIST)) {
685 struct yang_list_keys keys;
686
687 memset(&keys, 0, sizeof(keys));
9eb2c0a1
RW
688 if (nb_callback_get_keys(nb_node, nb_next, &keys)
689 != NB_OK) {
99fb518f
RW
690 flog_warn(EC_LIB_NB_CB_STATE,
691 "%s: failed to get list keys",
692 __func__);
693 confd_data_reply_next_key(tctx, NULL, -1, -1);
694 return CONFD_OK;
695 }
1a4bc045 696
99fb518f
RW
697 /* Feed keys to ConfD. */
698 for (size_t i = 0; i < keys.num; i++)
699 CONFD_SET_STR(&v[i], keys.key[i]);
700 confd_data_reply_next_key(tctx, v, keys.num,
701 (long)nb_next);
702 } else {
703 char pointer_str[16];
704
705 /*
706 * ConfD 6.6 user guide, chapter 6.11 (Operational data
707 * lists without keys):
708 * "To support this without having completely separate
709 * APIs, we use a "pseudo" key in the ConfD APIs for
710 * this type of list. This key is not part of the data
711 * model, and completely hidden in the northbound agent
712 * interfaces, but is used with e.g. the get_next() and
713 * get_elem() callbacks as if it were a normal key. This
714 * "pseudo" key is always a single signed 64-bit
715 * integer, i.e. the confd_value_t type is C_INT64. The
716 * values can be chosen arbitrarily by the application,
717 * as long as a key value returned by get_next() can be
718 * used to get the data for the corresponding list entry
719 * with get_elem() or get_object() as usual. It could
720 * e.g. be an index into an array that holds the data,
721 * or even a memory address in integer form".
722 *
723 * Since we're using the CONFD_DAEMON_FLAG_STRINGSONLY
724 * option, we must convert our pseudo-key (a void
725 * pointer) to a string before sending it to confd.
726 */
727 snprintf(pointer_str, sizeof(pointer_str), "%lu",
728 (unsigned long)nb_next);
729 CONFD_SET_STR(&v[0], pointer_str);
730 confd_data_reply_next_key(tctx, v, 1, (long)nb_next);
731 }
1a4bc045
RW
732 break;
733 case LYS_LEAFLIST:
9eb2c0a1 734 data = nb_callback_get_elem(nb_node, xpath, nb_next);
1a4bc045
RW
735 if (data) {
736 if (data->value) {
737 CONFD_SET_STR(&v[0], data->value);
738 confd_data_reply_next_key(tctx, v, 1,
739 (long)nb_next);
740 }
741 yang_data_free(data);
742 } else
743 confd_data_reply_next_key(tctx, NULL, -1, -1);
744 break;
745 default:
746 break;
747 }
5bce33b3
RW
748
749 return CONFD_OK;
750}
751
752/*
753 * Optional callback - implemented for performance reasons.
754 */
755static int frr_confd_data_get_object(struct confd_trans_ctx *tctx,
756 confd_hkeypath_t *kp)
757{
758 struct nb_node *nb_node;
1a4bc045 759 const struct lys_node *child;
5bce33b3 760 char xpath[BUFSIZ];
5bce33b3 761 char xpath_child[XPATH_MAXLEN];
5bce33b3
RW
762 struct list *elements;
763 struct yang_data *data;
764 const void *list_entry;
1a4bc045
RW
765 confd_value_t values[CONFD_MAX_CHILD_NODES];
766 size_t nvalues = 0;
5bce33b3
RW
767
768 frr_confd_get_xpath(kp, xpath, sizeof(xpath));
769
770 nb_node = nb_node_find(xpath);
771 if (!nb_node) {
772 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
773 "%s: unknown data path: %s", __func__, xpath);
774 confd_data_reply_not_found(tctx);
1a4bc045 775 return CONFD_ERR;
5bce33b3
RW
776 }
777
1a4bc045 778 if (frr_confd_hkeypath_get_list_entry(kp, nb_node, &list_entry) != 0) {
5bce33b3
RW
779 confd_data_reply_not_found(tctx);
780 return CONFD_OK;
781 }
782
5bce33b3 783 elements = yang_data_list_new();
5bce33b3
RW
784
785 /* Loop through list child nodes. */
1a4bc045
RW
786 LY_TREE_FOR (nb_node->snode->child, child) {
787 struct nb_node *nb_node_child = child->priv;
788 confd_value_t *v;
789
790 if (nvalues > CONFD_MAX_CHILD_NODES)
791 break;
792
793 v = &values[nvalues++];
5bce33b3 794
1a4bc045
RW
795 /* Non-presence containers, lists and leaf-lists. */
796 if (!nb_node_child->cbs.get_elem) {
797 CONFD_SET_NOEXISTS(v);
798 continue;
799 }
5bce33b3
RW
800
801 snprintf(xpath_child, sizeof(xpath_child), "%s/%s", xpath,
802 child->name);
9eb2c0a1
RW
803 data = nb_callback_get_elem(nb_node_child, xpath_child,
804 list_entry);
5bce33b3
RW
805 if (data) {
806 if (data->value)
1a4bc045
RW
807 CONFD_SET_STR(v, data->value);
808 else {
809 /* Presence containers and empty leafs. */
810 CONFD_SET_XMLTAG(
811 v, nb_node_child->confd_hash,
812 confd_str2hash(nb_node_child->snode
813 ->module->ns));
814 }
5bce33b3
RW
815 listnode_add(elements, data);
816 } else
1a4bc045 817 CONFD_SET_NOEXISTS(v);
5bce33b3
RW
818 }
819
1a4bc045 820 confd_data_reply_value_array(tctx, values, nvalues);
5bce33b3
RW
821
822 /* Release memory. */
5bce33b3
RW
823 list_delete(&elements);
824
825 return CONFD_OK;
826}
827
828/*
829 * Optional callback - implemented for performance reasons.
830 */
831static int frr_confd_data_get_next_object(struct confd_trans_ctx *tctx,
832 confd_hkeypath_t *kp, long next)
833{
834 char xpath[BUFSIZ];
5bce33b3 835 struct nb_node *nb_node;
5bce33b3 836 struct list *elements;
1a4bc045 837 const void *parent_list_entry;
5bce33b3
RW
838 const void *nb_next;
839#define CONFD_OBJECTS_PER_TIME 100
840 struct confd_next_object objects[CONFD_OBJECTS_PER_TIME + 1];
99fb518f 841 char pseudo_keys[CONFD_OBJECTS_PER_TIME][16];
5bce33b3
RW
842 int nobjects = 0;
843
844 frr_confd_get_xpath(kp, xpath, sizeof(xpath));
845
846 nb_node = nb_node_find(xpath);
847 if (!nb_node) {
848 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
849 "%s: unknown data path: %s", __func__, xpath);
850 confd_data_reply_next_object_array(tctx, NULL, 0, 0);
851 return CONFD_OK;
852 }
853
1a4bc045
RW
854 if (frr_confd_hkeypath_get_list_entry(kp, nb_node, &parent_list_entry)
855 != 0) {
856 confd_data_reply_next_object_array(tctx, NULL, 0, 0);
857 return CONFD_OK;
5bce33b3
RW
858 }
859
860 elements = yang_data_list_new();
861 nb_next = (next == -1) ? NULL : (void *)next;
862
863 memset(objects, 0, sizeof(objects));
864 for (int j = 0; j < CONFD_OBJECTS_PER_TIME; j++) {
865 struct confd_next_object *object;
1a4bc045 866 struct lys_node *child;
5bce33b3 867 struct yang_data *data;
1a4bc045 868 size_t nvalues = 0;
5bce33b3
RW
869
870 object = &objects[j];
871
9eb2c0a1
RW
872 nb_next = nb_callback_get_next(nb_node, parent_list_entry,
873 nb_next);
5bce33b3
RW
874 if (!nb_next)
875 /* End of the list. */
876 break;
877
5bce33b3
RW
878 object->next = (long)nb_next;
879
1a4bc045
RW
880 /* Leaf-lists require special handling. */
881 if (nb_node->snode->nodetype == LYS_LEAFLIST) {
882 object->v = XMALLOC(MTYPE_CONFD, sizeof(confd_value_t));
9eb2c0a1 883 data = nb_callback_get_elem(nb_node, xpath, nb_next);
1a4bc045
RW
884 assert(data && data->value);
885 CONFD_SET_STR(object->v, data->value);
886 nvalues++;
887 listnode_add(elements, data);
888 goto next;
5bce33b3
RW
889 }
890
1a4bc045
RW
891 object->v =
892 XMALLOC(MTYPE_CONFD,
893 CONFD_MAX_CHILD_NODES * sizeof(confd_value_t));
5bce33b3 894
99fb518f
RW
895 /*
896 * ConfD 6.6 user guide, chapter 6.11 (Operational data lists
897 * without keys):
898 * "In the response to the get_next_object() callback, the data
899 * provider is expected to provide the key values along with the
900 * other leafs in an array that is populated according to the
901 * data model. This must be done also for this type of list,
902 * even though the key isn't actually in the data model. The
903 * "pseudo" key must always be the first element in the array".
904 */
905 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_KEYLESS_LIST)) {
906 confd_value_t *v;
907
908 snprintf(pseudo_keys[j], sizeof(pseudo_keys[j]), "%lu",
909 (unsigned long)nb_next);
910
911 v = &object->v[nvalues++];
912 CONFD_SET_STR(v, pseudo_keys[j]);
913 }
914
5bce33b3 915 /* Loop through list child nodes. */
1a4bc045
RW
916 LY_TREE_FOR (nb_node->snode->child, child) {
917 struct nb_node *nb_node_child = child->priv;
5bce33b3 918 char xpath_child[XPATH_MAXLEN];
1a4bc045 919 confd_value_t *v;
5bce33b3 920
1a4bc045
RW
921 if (nvalues > CONFD_MAX_CHILD_NODES)
922 break;
5bce33b3 923
1a4bc045
RW
924 v = &object->v[nvalues++];
925
926 /* Non-presence containers, lists and leaf-lists. */
927 if (!nb_node_child->cbs.get_elem) {
928 CONFD_SET_NOEXISTS(v);
929 continue;
930 }
5bce33b3
RW
931
932 snprintf(xpath_child, sizeof(xpath_child), "%s/%s",
933 xpath, child->name);
9eb2c0a1
RW
934 data = nb_callback_get_elem(nb_node_child, xpath_child,
935 nb_next);
5bce33b3
RW
936 if (data) {
937 if (data->value)
938 CONFD_SET_STR(v, data->value);
1a4bc045
RW
939 else {
940 /*
941 * Presence containers and empty leafs.
942 */
943 CONFD_SET_XMLTAG(
944 v, nb_node_child->confd_hash,
945 confd_str2hash(
946 nb_node_child->snode
947 ->module->ns));
948 }
5bce33b3
RW
949 listnode_add(elements, data);
950 } else
951 CONFD_SET_NOEXISTS(v);
952 }
1a4bc045
RW
953 next:
954 object->n = nvalues;
5bce33b3
RW
955 nobjects++;
956 }
5bce33b3
RW
957
958 if (nobjects == 0) {
959 confd_data_reply_next_object_array(tctx, NULL, 0, 0);
960 list_delete(&elements);
961 return CONFD_OK;
962 }
963
964 /* Detect end of the list. */
965 if (!nb_next) {
966 nobjects++;
967 objects[nobjects].v = NULL;
968 }
969
970 /* Reply to ConfD. */
971 confd_data_reply_next_object_arrays(tctx, objects, nobjects, 0);
972 if (!nb_next)
973 nobjects--;
974
975 /* Release memory. */
976 list_delete(&elements);
977 for (int j = 0; j < nobjects; j++) {
978 struct confd_next_object *object;
979
980 object = &objects[j];
981 XFREE(MTYPE_CONFD, object->v);
982 }
983
984 return CONFD_OK;
985}
986
987static int frr_confd_notification_send(const char *xpath,
988 struct list *arguments)
989{
990 struct nb_node *nb_node;
991 struct yang_module *module;
992 struct confd_datetime now;
993 confd_tag_value_t *values;
994 int nvalues;
995 int i = 0;
996 struct yang_data *data;
997 struct listnode *node;
998 int ret;
999
1000 nb_node = nb_node_find(xpath);
1001 if (!nb_node) {
1002 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
1003 "%s: unknown data path: %s", __func__, xpath);
1004 return -1;
1005 }
1006 module = yang_module_find(nb_node->snode->module->name);
1007 assert(module);
1008
1009 nvalues = 2;
1010 if (arguments)
1011 nvalues += listcount(arguments);
1012
1013 values = XMALLOC(MTYPE_CONFD, nvalues * sizeof(*values));
1014
1015 CONFD_SET_TAG_XMLBEGIN(&values[i++], nb_node->confd_hash,
1016 module->confd_hash);
1017 for (ALL_LIST_ELEMENTS_RO(arguments, node, data)) {
80243aef
RW
1018 struct nb_node *nb_node_arg;
1019
1020 nb_node_arg = nb_node_find(data->xpath);
1021 if (!nb_node_arg) {
1022 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
1023 "%s: unknown data path: %s", __func__,
1024 data->xpath);
1025 XFREE(MTYPE_CONFD, values);
1026 return NB_ERR;
1027 }
5bce33b3 1028
80243aef 1029 CONFD_SET_TAG_STR(&values[i++], nb_node_arg->confd_hash,
5bce33b3
RW
1030 data->value);
1031 }
1032 CONFD_SET_TAG_XMLEND(&values[i++], nb_node->confd_hash,
1033 module->confd_hash);
1034
1035 getdatetime(&now);
1036 ret = confd_notification_send(live_ctx, &now, values, nvalues);
1037
1038 /* Release memory. */
1039 XFREE(MTYPE_CONFD, values);
1040
1041 /* Map ConfD return code to northbound return code. */
1042 switch (ret) {
1043 case CONFD_OK:
1044 return NB_OK;
1045 default:
1046 return NB_ERR;
1047 }
1048}
1049
1050static int frr_confd_action_init(struct confd_user_info *uinfo)
1051{
1052 confd_action_set_fd(uinfo, dp_worker_sock);
1053
1054 return CONFD_OK;
1055}
1056
1057static int frr_confd_action_execute(struct confd_user_info *uinfo,
1058 struct xml_tag *name, confd_hkeypath_t *kp,
1059 confd_tag_value_t *params, int nparams)
1060{
1061 char xpath[BUFSIZ];
1062 struct nb_node *nb_node;
1063 struct list *input;
1064 struct list *output;
1065 struct yang_data *data;
1066 confd_tag_value_t *reply;
1067 int ret = CONFD_OK;
1068
1069 /* Getting the XPath is tricky. */
1070 if (kp) {
1071 /* This is a YANG RPC. */
1072 frr_confd_get_xpath(kp, xpath, sizeof(xpath));
1073 strlcat(xpath, "/", sizeof(xpath));
1074 strlcat(xpath, confd_hash2str(name->tag), sizeof(xpath));
1075 } else {
1076 /* This is a YANG action. */
1077 snprintf(xpath, sizeof(xpath), "/%s:%s",
1078 confd_ns2prefix(name->ns), confd_hash2str(name->tag));
1079 }
1080
1081 nb_node = nb_node_find(xpath);
1082 if (!nb_node) {
1083 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
1084 "%s: unknown data path: %s", __func__, xpath);
1085 return CONFD_ERR;
1086 }
1087
1088 input = yang_data_list_new();
1089 output = yang_data_list_new();
1090
1091 /* Process input nodes. */
1092 for (int i = 0; i < nparams; i++) {
1093 char xpath_input[BUFSIZ];
1094 char value_str[YANG_VALUE_MAXLEN];
1095
1096 snprintf(xpath_input, sizeof(xpath_input), "%s/%s", xpath,
1097 confd_hash2str(params[i].tag.tag));
1098
1099 if (frr_confd_val2str(xpath_input, &params[i].v, value_str,
1100 sizeof(value_str))
1101 != 0) {
1102 flog_err(
1103 EC_LIB_CONFD_DATA_CONVERT,
1104 "%s: failed to convert ConfD value to a string",
1105 __func__);
1106 ret = CONFD_ERR;
1107 goto exit;
1108 }
1109
1110 data = yang_data_new(xpath_input, value_str);
1111 listnode_add(input, data);
1112 }
1113
1114 /* Execute callback registered for this XPath. */
9eb2c0a1 1115 if (nb_callback_rpc(nb_node, xpath, input, output) != NB_OK) {
5bce33b3
RW
1116 flog_warn(EC_LIB_NB_CB_RPC, "%s: rpc callback failed: %s",
1117 __func__, xpath);
1118 ret = CONFD_ERR;
1119 goto exit;
1120 }
1121
1122 /* Process output nodes. */
1123 if (listcount(output) > 0) {
1124 struct listnode *node;
1125 int i = 0;
1126
1127 reply = XMALLOC(MTYPE_CONFD,
1128 listcount(output) * sizeof(*reply));
1129
1130 for (ALL_LIST_ELEMENTS_RO(output, node, data)) {
80243aef 1131 struct nb_node *nb_node_output;
5bce33b3
RW
1132 int hash;
1133
80243aef
RW
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__,
1138 data->xpath);
1139 goto exit;
1140 }
1141
1142 hash = confd_str2hash(nb_node_output->snode->name);
5bce33b3
RW
1143 CONFD_SET_TAG_STR(&reply[i++], hash, data->value);
1144 }
1145 confd_action_reply_values(uinfo, reply, listcount(output));
1146 XFREE(MTYPE_CONFD, reply);
1147 }
1148
1149exit:
1150 /* Release memory. */
1151 list_delete(&input);
1152 list_delete(&output);
1153
1154 return ret;
1155}
1156
1157
1158static int frr_confd_dp_read(struct thread *thread)
1159{
1160 struct confd_daemon_ctx *dctx = THREAD_ARG(thread);
1161 int fd = THREAD_FD(thread);
1162 int ret;
1163
1164 thread = NULL;
1165 thread_add_read(master, frr_confd_dp_read, dctx, fd, &thread);
1166
1167 ret = confd_fd_ready(dctx, fd);
1168 if (ret == CONFD_EOF) {
1169 flog_err_confd("confd_fd_ready");
a7d055e4 1170 frr_confd_finish();
5bce33b3
RW
1171 return -1;
1172 } else if (ret == CONFD_ERR && confd_errno != CONFD_ERR_EXTERNAL) {
1173 flog_err_confd("confd_fd_ready");
a7d055e4 1174 frr_confd_finish();
5bce33b3
RW
1175 return -1;
1176 }
1177
1178 return 0;
1179}
1180
e0ccfad2 1181static int frr_confd_subscribe_state(const struct lys_node *snode, void *arg)
5bce33b3
RW
1182{
1183 struct nb_node *nb_node = snode->priv;
e0ccfad2 1184 struct confd_data_cbs *data_cbs = arg;
5bce33b3 1185
db452508 1186 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_R))
e0ccfad2 1187 return YANG_ITER_CONTINUE;
5bce33b3 1188 /* We only need to subscribe to the root of the state subtrees. */
db452508 1189 if (snode->parent && CHECK_FLAG(snode->parent->flags, LYS_CONFIG_R))
e0ccfad2 1190 return YANG_ITER_CONTINUE;
5bce33b3 1191
9eb2c0a1
RW
1192 DEBUGD(&nb_dbg_client_confd,
1193 "%s: providing data to '%s' (callpoint %s)", __func__,
1194 nb_node->xpath, snode->name);
5bce33b3
RW
1195
1196 strlcpy(data_cbs->callpoint, snode->name, sizeof(data_cbs->callpoint));
1197 if (confd_register_data_cb(dctx, data_cbs) != CONFD_OK)
1198 flog_err_confd("confd_register_data_cb");
e0ccfad2
RW
1199
1200 return YANG_ITER_CONTINUE;
5bce33b3
RW
1201}
1202
1203static int frr_confd_init_dp(const char *program_name)
1204{
1205 struct confd_trans_cbs trans_cbs;
1206 struct confd_data_cbs data_cbs;
1207 struct confd_notification_stream_cbs ncbs;
1208 struct confd_action_cbs acbs;
1209
1210 /* Initialize daemon context. */
1211 dctx = confd_init_daemon(program_name);
1212 if (!dctx) {
1213 flog_err_confd("confd_init_daemon");
1214 goto error;
1215 }
1216
1217 /*
1218 * Inform we want to receive YANG values as raw strings, and that we
1219 * want to provide only strings in the reply functions, regardless of
1220 * the YANG type.
1221 */
1222 confd_set_daemon_flags(dctx, CONFD_DAEMON_FLAG_STRINGSONLY);
1223
1224 /* Establish a control socket. */
1225 dp_ctl_sock = socket(PF_INET, SOCK_STREAM, 0);
1226 if (dp_ctl_sock < 0) {
1227 flog_err(EC_LIB_SOCKET, "%s: failed to create socket: %s",
1228 __func__, safe_strerror(errno));
1229 goto error;
1230 }
1231
1232 if (confd_connect(dctx, dp_ctl_sock, CONTROL_SOCKET, &confd_addr,
1233 sizeof(struct sockaddr_in))
1234 != CONFD_OK) {
1235 flog_err_confd("confd_connect");
1236 goto error;
1237 }
1238
1239 /*
1240 * Establish a worker socket (only one since this plugin runs on a
1241 * single thread).
1242 */
1243 dp_worker_sock = socket(PF_INET, SOCK_STREAM, 0);
1244 if (dp_worker_sock < 0) {
1245 flog_err(EC_LIB_SOCKET, "%s: failed to create socket: %s",
1246 __func__, safe_strerror(errno));
1247 goto error;
1248 }
1249 if (confd_connect(dctx, dp_worker_sock, WORKER_SOCKET, &confd_addr,
1250 sizeof(struct sockaddr_in))
1251 != CONFD_OK) {
1252 flog_err_confd("confd_connect");
1253 goto error;
1254 }
1255
1256 /* Register transaction callback functions. */
1257 memset(&trans_cbs, 0, sizeof(trans_cbs));
1258 trans_cbs.init = frr_confd_transaction_init;
1259 confd_register_trans_cb(dctx, &trans_cbs);
1260
1261 /* Register our read/write callbacks. */
1262 memset(&data_cbs, 0, sizeof(data_cbs));
1263 data_cbs.get_elem = frr_confd_data_get_elem;
1264 data_cbs.exists_optional = frr_confd_data_get_elem;
1265 data_cbs.get_next = frr_confd_data_get_next;
1266 data_cbs.get_object = frr_confd_data_get_object;
1267 data_cbs.get_next_object = frr_confd_data_get_next_object;
1268
1269 /*
1270 * Iterate over all loaded YANG modules and subscribe to the paths
1271 * referent to state data.
1272 */
e0ccfad2 1273 yang_snodes_iterate_all(frr_confd_subscribe_state, 0, &data_cbs);
5bce33b3
RW
1274
1275 /* Register notification stream. */
1276 memset(&ncbs, 0, sizeof(ncbs));
1277 ncbs.fd = dp_worker_sock;
1278 /*
1279 * RFC 5277 - Section 3.2.3:
1280 * A NETCONF server implementation supporting the notification
1281 * capability MUST support the "NETCONF" notification event
1282 * stream. This stream contains all NETCONF XML event notifications
1283 * supported by the NETCONF server.
1284 */
1285 strlcpy(ncbs.streamname, "NETCONF", sizeof(ncbs.streamname));
1286 if (confd_register_notification_stream(dctx, &ncbs, &live_ctx)
1287 != CONFD_OK) {
1288 flog_err_confd("confd_register_notification_stream");
1289 goto error;
1290 }
1291
1292 /* Register the action handler callback. */
1293 memset(&acbs, 0, sizeof(acbs));
1294 strlcpy(acbs.actionpoint, "actionpoint", sizeof(acbs.actionpoint));
1295 acbs.init = frr_confd_action_init;
1296 acbs.action = frr_confd_action_execute;
1297 if (confd_register_action_cbs(dctx, &acbs) != CONFD_OK) {
1298 flog_err_confd("confd_register_action_cbs");
1299 goto error;
1300 }
1301
1302 /* Notify we registered all callbacks we wanted. */
1303 if (confd_register_done(dctx) != CONFD_OK) {
1304 flog_err_confd("confd_register_done");
1305 goto error;
1306 }
1307
1308 thread_add_read(master, frr_confd_dp_read, dctx, dp_ctl_sock,
1309 &t_dp_ctl);
1310 thread_add_read(master, frr_confd_dp_read, dctx, dp_worker_sock,
1311 &t_dp_worker);
1312
1313 return 0;
1314
1315error:
1316 frr_confd_finish_dp();
1317
1318 return -1;
1319}
1320
1321static void frr_confd_finish_dp(void)
1322{
1323 if (dp_worker_sock > 0) {
1324 THREAD_OFF(t_dp_worker);
1325 close(dp_worker_sock);
1326 }
1327 if (dp_ctl_sock > 0) {
1328 THREAD_OFF(t_dp_ctl);
1329 close(dp_ctl_sock);
1330 }
1331 if (dctx != NULL)
1332 confd_release_daemon(dctx);
1333}
1334
9eb2c0a1
RW
1335/* ------------ CLI ------------ */
1336
1337DEFUN (debug_nb_confd,
1338 debug_nb_confd_cmd,
1339 "[no] debug northbound client confd",
1340 NO_STR
1341 DEBUG_STR
1342 "Northbound debugging\n"
1343 "Client\n"
1344 "ConfD\n")
1345{
1346 uint32_t mode = DEBUG_NODE2MODE(vty->node);
1347 bool no = strmatch(argv[0]->text, "no");
1348
1349 DEBUG_MODE_SET(&nb_dbg_client_confd, mode, !no);
1350
1351 return CMD_SUCCESS;
1352}
1353
1354static int frr_confd_debug_config_write(struct vty *vty)
1355{
1356 if (DEBUG_MODE_CHECK(&nb_dbg_client_confd, DEBUG_MODE_CONF))
1357 vty_out(vty, "debug northbound client confd\n");
1358
1359 return 0;
1360}
1361
1362static int frr_confd_debug_set_all(uint32_t flags, bool set)
1363{
1364 DEBUG_FLAGS_SET(&nb_dbg_client_confd, flags, set);
1365
1366 /* If all modes have been turned off, don't preserve options. */
1367 if (!DEBUG_MODE_CHECK(&nb_dbg_client_confd, DEBUG_MODE_ALL))
1368 DEBUG_CLEAR(&nb_dbg_client_confd);
1369
1370 return 0;
1371}
1372
1373static void frr_confd_cli_init(void)
1374{
1375 hook_register(nb_client_debug_config_write,
1376 frr_confd_debug_config_write);
1377 hook_register(nb_client_debug_set_all, frr_confd_debug_set_all);
1378
1379 install_element(ENABLE_NODE, &debug_nb_confd_cmd);
1380 install_element(CONFIG_NODE, &debug_nb_confd_cmd);
1381}
1382
5bce33b3
RW
1383/* ------------ Main ------------ */
1384
e0ccfad2
RW
1385static int frr_confd_calculate_snode_hash(const struct lys_node *snode,
1386 void *arg)
5bce33b3
RW
1387{
1388 struct nb_node *nb_node = snode->priv;
1389
1390 nb_node->confd_hash = confd_str2hash(snode->name);
e0ccfad2
RW
1391
1392 return YANG_ITER_CONTINUE;
5bce33b3
RW
1393}
1394
1395static int frr_confd_init(const char *program_name)
1396{
1397 struct sockaddr_in *confd_addr4 = (struct sockaddr_in *)&confd_addr;
1398 int debuglevel = CONFD_SILENT;
1399 int ret = -1;
1400
1401 /* Initialize ConfD library. */
1402 confd_init(program_name, stderr, debuglevel);
1403
1404 confd_addr4->sin_family = AF_INET;
1405 confd_addr4->sin_addr.s_addr = inet_addr("127.0.0.1");
1406 confd_addr4->sin_port = htons(CONFD_PORT);
1407 if (confd_load_schemas(&confd_addr, sizeof(struct sockaddr_in))
1408 != CONFD_OK) {
1409 flog_err_confd("confd_load_schemas");
1410 return -1;
1411 }
1412
1413 ret = frr_confd_init_cdb();
1414 if (ret != 0)
1415 goto error;
1416
1417 ret = frr_confd_init_dp(program_name);
1418 if (ret != 0) {
1419 frr_confd_finish_cdb();
1420 goto error;
1421 }
1422
e0ccfad2 1423 yang_snodes_iterate_all(frr_confd_calculate_snode_hash, 0, NULL);
5bce33b3
RW
1424
1425 hook_register(nb_notification_send, frr_confd_notification_send);
1426
1427 confd_connected = true;
1428 return 0;
1429
1430error:
1431 confd_free_schemas();
1432
1433 return ret;
1434}
1435
1436static int frr_confd_finish(void)
1437{
d8729f8c 1438 if (!confd_connected)
5bce33b3
RW
1439 return 0;
1440
1441 frr_confd_finish_cdb();
1442 frr_confd_finish_dp();
1443
1444 confd_free_schemas();
1445
1446 confd_connected = false;
1447
1448 return 0;
1449}
1450
1451static int frr_confd_module_late_init(struct thread_master *tm)
1452{
1453 master = tm;
1454
1455 if (frr_confd_init(frr_get_progname()) < 0) {
1456 flog_err(EC_LIB_CONFD_INIT,
1457 "failed to initialize the ConfD module");
1458 return -1;
1459 }
1460
1461 hook_register(frr_fini, frr_confd_finish);
9eb2c0a1 1462 frr_confd_cli_init();
5bce33b3
RW
1463
1464 return 0;
1465}
1466
1467static int frr_confd_module_init(void)
1468{
1469 hook_register(frr_late_init, frr_confd_module_late_init);
1470
1471 return 0;
1472}
1473
1474FRR_MODULE_SETUP(.name = "frr_confd", .version = FRR_VERSION,
1475 .description = "FRR ConfD integration module",
1476 .init = frr_confd_module_init, )