Skip to content

Commit ce56a1e

Browse files
committed
confd: Refactor how sysrepo callbacks are handled
Sysrepo only care about model changes, but we want the system configuration. Therefore add a common callback for all modules and handle dependencies between the modules, if someone should be run before another for example.
1 parent 8616a27 commit ce56a1e

17 files changed

+603
-616
lines changed

src/confd/src/core.c

Lines changed: 180 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* SPDX-License-Identifier: BSD-3-Clause */
22

3+
#include <srx/srx_val.h>
34
#include <srx/common.h>
5+
#include <srx/lyx.h>
46
#include "core.h"
57

68
struct confd confd;
@@ -75,21 +77,105 @@ int core_post_hook(sr_session_ctx_t *session, uint32_t sub_id, const char *modul
7577
return SR_ERR_SYS;
7678
}
7779

78-
/* skip reload in bootstrap, implicit reload in runlevel change */
79-
if (systemf("runlevel >/dev/null 2>&1")) {
80-
/* trigger any tasks waiting for confd to have applied *-config */
81-
system("initctl -nbq cond set bootstrap");
80+
81+
return SR_ERR_OK;
82+
}
83+
84+
static int change_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *module_name,
85+
const char *xpath, sr_event_t event, uint32_t request_id, void *_confd)
86+
{
87+
struct lyd_node *diff = NULL, *config = NULL;
88+
static uint32_t last_event = -1;
89+
struct confd *confd = _confd;
90+
static uint32_t last_id = 0;
91+
confd_dependency_t result;
92+
sr_data_t *cfg = NULL;
93+
int rc = SR_ERR_OK;
94+
95+
if (request_id == last_id && last_event == event)
8296
return SR_ERR_OK;
97+
last_id = request_id;
98+
last_event = event;
99+
100+
if (event == SR_EV_CHANGE || event == SR_EV_DONE) {
101+
rc = srx_get_diff(session, &diff);
102+
if (rc != SR_ERR_OK) {
103+
ERROR("Failed to get diff: %d", rc);
104+
return rc;
105+
}
106+
rc = sr_get_data(session, "//.", 0, 0, 0, &cfg);
107+
if (rc || !cfg)
108+
goto free_diff;
109+
110+
config = cfg->tree;
111+
#if 0
112+
/* Debug: print diff to file */
113+
FILE *f = fopen("/tmp/confd-diff.json", "w");
114+
if (f) {
115+
lyd_print_file(f, diff, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
116+
fclose(f);
117+
}
118+
#endif
83119
}
84120

85-
if (systemf("initctl -b reload")) {
86-
EMERG("initctl reload: failed applying new configuration!");
87-
return SR_ERR_SYS;
88-
}
121+
/* ietf-interfaces */
122+
ietf_interfaces_change(session, config, diff, event, confd);
89123

90-
AUDIT("The new configuration has been applied.");
124+
/* infix-dhcp-client*/
125+
infix_dhcp_client_change(session, config, diff, event, confd);
91126

92-
return SR_ERR_OK;
127+
/* ietf-keystore */
128+
ietf_keystore_change(session, config, diff, event, confd);
129+
130+
/* infix-services */
131+
infix_services_change(session, config, diff, event, confd);
132+
133+
/* ietf-syslog*/
134+
ietf_syslog_change(session, config, diff, event, confd);
135+
136+
/* ietf-system */
137+
ietf_system_change(session, config, diff, event, confd);
138+
139+
/* infix-containers */
140+
infix_containers_change(session, config, diff, event, confd);
141+
142+
/* ietf-hardware */
143+
ietf_hardware_change(session, config, diff, event, confd);
144+
145+
/* ietf-routing */
146+
ietf_routing_change(session, config, diff, event, confd);
147+
148+
/* infix-dhcp-server */
149+
infix_dhcp_server_change(session, config, diff, event, confd);
150+
151+
/* infix-firewall */
152+
infix_firewall_change(session, config, diff, event, confd);
153+
154+
if (cfg)
155+
sr_release_data(cfg);
156+
157+
if (event == SR_EV_DONE) {
158+
/* skip reload in bootstrap, implicit reload in runlevel change */
159+
if (systemf("runlevel >/dev/null 2>&1")) {
160+
/* trigger any tasks waiting for confd to have applied *-config */
161+
system("initctl -nbq cond set bootstrap");
162+
return SR_ERR_OK;
163+
}
164+
165+
if (systemf("initctl -b reload")) {
166+
EMERG("initctl reload: failed applying new configuration!");
167+
return SR_ERR_SYS;
168+
}
169+
170+
AUDIT("The new configuration has been applied.");
171+
}
172+
free_diff:
173+
lyd_free_tree(diff);
174+
return rc;
175+
}
176+
177+
static int subscribe_module(char *model, struct confd *confd, int flags) {
178+
return sr_module_change_subscribe(confd->session, model, "//.", change_cb, confd, CB_PRIO_PRIMARY, SR_SUBSCR_CHANGE_ALL_MODULES | SR_SUBSCR_DEFAULT | flags, &confd->sub);
93179
}
94180

95181
int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv)
@@ -136,56 +222,119 @@ int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv)
136222
*/
137223
confd.ifquirks = json_load_file("/etc/product/interface-quirks.json", 0, NULL);
138224

139-
rc = ietf_interfaces_init(&confd);
140-
if (rc)
225+
rc = subscribe_module("ietf-interfaces", &confd, 0);
226+
if (rc) {
227+
ERROR("Failed to subscribe to ietf-interfaces");
141228
goto err;
142-
rc = ietf_keystore_init(&confd);
143-
if (rc)
229+
}
230+
rc = subscribe_module("ietf-netconf-acm", &confd, 0);
231+
if (rc) {
232+
ERROR("Failed to subscribe to ietf-netconf-acm");
144233
goto err;
145-
rc = ietf_syslog_init(&confd);
146-
if (rc)
234+
}
235+
rc = subscribe_module("infix-dhcp-client", &confd, 0);
236+
if (rc) {
237+
ERROR("Failed to subscribe to infix-dhcp-client");
147238
goto err;
148-
rc = ietf_system_init(&confd);
149-
if (rc)
239+
}
240+
rc = subscribe_module("ietf-keystore", &confd, SR_SUBSCR_UPDATE);
241+
if (rc) {
242+
ERROR("Failed to subscribe to ietf-keystore");
150243
goto err;
151-
rc = infix_containers_init(&confd);
152-
if (rc)
244+
}
245+
rc = subscribe_module("infix-services", &confd, 0);
246+
if (rc) {
247+
ERROR("Failed to subscribe to infix-services");
248+
goto err;
249+
}
250+
rc = subscribe_module("ietf-system", &confd, 0);
251+
if (rc) {
252+
ERROR("Failed to subscribe to ietf-system");
253+
goto err;
254+
}
255+
rc = subscribe_module("ieee802-dot1ab-lldp", &confd, 0);
256+
if (rc) {
257+
ERROR("Failed to subscribe to ieee802-dot1ab-lldp");
258+
goto err;
259+
}
260+
#ifdef CONTAINERS
261+
rc = subscribe_module("infix-containers", &confd, 0);
262+
if (rc) {
263+
ERROR("Failed to subscribe to infix-containers");
264+
goto err;
265+
}
266+
#endif
267+
rc = subscribe_module("infix-dhcp-server", &confd, 0);
268+
if (rc) {
269+
ERROR("Failed to subscribe to infix-dhcp-server");
270+
goto err;
271+
}
272+
rc = subscribe_module("ietf-routing", &confd, 0);
273+
if (rc) {
274+
ERROR("Failed to subscribe to ietf-routing");
275+
goto err;
276+
}
277+
rc = subscribe_module("ietf-hardware", &confd, 0);
278+
if (rc) {
279+
ERROR("Failed to subscribe to ietf-hardware");
280+
goto err;
281+
}
282+
rc = subscribe_module("infix-firewall", &confd, 0);
283+
if (rc) {
284+
ERROR("Failed to subscribe to infix-firewall");
285+
goto err;
286+
}
287+
rc = subscribe_module("infix-meta", &confd, SR_SUBSCR_UPDATE);
288+
if (rc) {
289+
ERROR("Failed to subscribe to infix-meta");
153290
goto err;
154-
rc = infix_dhcp_client_init(&confd);
291+
}
292+
293+
rc = ietf_system_rpc_init(&confd);
155294
if (rc)
156295
goto err;
157-
rc = infix_dhcp_server_init(&confd);
296+
rc = infix_containers_rpc_init(&confd);
158297
if (rc)
159298
goto err;
160-
rc = infix_factory_init(&confd);
299+
rc = infix_dhcp_server_rpc_init(&confd);
161300
if (rc)
162301
goto err;
163-
rc = ietf_factory_default_init(&confd);
302+
303+
rc = infix_factory_rpc_init(&confd);
164304
if (rc)
165305
goto err;
166-
rc = ietf_routing_init(&confd);
306+
307+
rc = ietf_factory_default_rpc_init(&confd);
167308
if (rc)
168309
goto err;
169-
rc = infix_meta_init(&confd);
310+
311+
rc = infix_firewall_rpc_init(&confd);
170312
if (rc)
171313
goto err;
172-
rc = infix_system_sw_init(&confd);
314+
315+
rc = infix_system_sw_rpc_init(&confd);
173316
if (rc)
174317
goto err;
175-
rc = infix_services_init(&confd);
318+
319+
/* Candidate infer configurations */
320+
rc = ietf_hardware_candidate_init(&confd);
176321
if (rc)
177322
goto err;
178-
rc = ietf_hardware_init(&confd);
323+
324+
rc = infix_firewall_candidate_init(&confd);
179325
if (rc)
180326
goto err;
181-
rc = infix_firewall_init(&confd);
327+
328+
rc = infix_dhcp_server_candidate_init(&confd);
182329
if (rc)
183330
goto err;
184331

332+
rc = infix_dhcp_client_candidate_init(&confd);
333+
if (rc)
334+
goto err;
185335
/* YOUR_INIT GOES HERE */
186336

187337
return SR_ERR_OK;
188-
189338
err:
190339
ERROR("init failed: %s", sr_strerror(rc));
191340
if (confd.root)

src/confd/src/core.h

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ uint32_t core_hook_prio (void);
133133
int core_pre_hook (sr_session_ctx_t *, uint32_t, const char *, const char *, sr_event_t, unsigned, void *);
134134
int core_post_hook (sr_session_ctx_t *, uint32_t, const char *, const char *, sr_event_t, unsigned, void *);
135135
int core_startup_save (sr_session_ctx_t *, uint32_t, const char *, const char *, sr_event_t, unsigned, void *);
136+
int core_wait_change (sr_session_ctx_t *, uint32_t, const char *, const char *, sr_event_t, unsigned, void *);
136137

137138
static inline int register_change(sr_session_ctx_t *session, const char *module, const char *xpath,
138139
int flags, sr_module_change_cb cb, void *arg, sr_subscription_ctx_t **sub)
@@ -206,57 +207,70 @@ static inline int register_rpc(sr_session_ctx_t *session, const char *xpath,
206207
return rc;
207208
}
208209

210+
209211
/* ietf-interfaces.c */
210-
int ietf_interfaces_init(struct confd *confd);
212+
int ietf_interfaces_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
213+
int ietf_interfaces_cand_init(struct confd *confd);
211214

212215
/* ietf-syslog.c */
213-
int ietf_syslog_init(struct confd *confd);
216+
int ietf_syslog_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
214217

215218
/* ietf-system.c */
216-
int ietf_system_init (struct confd *confd);
219+
int ietf_system_rpc_init (struct confd *confd);
217220
int hostnamefmt (struct confd *confd, const char *fmt, char *hostnm, size_t hostlen, char *domain, size_t domlen);
221+
int ietf_system_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
218222

219223
/* infix-containers.c */
220224
#ifdef CONTAINERS
221-
int infix_containers_init(struct confd *confd);
225+
int infix_containers_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
226+
int infix_containers_rpc_init(struct confd *confd);
222227
#else
223-
static inline int infix_containers_init(struct confd *confd) { return 0; }
228+
static inline int infix_containers_rpc_init(struct confd *confd) { return 0; }
229+
static inline int infix_containers_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd) { return 0; }
224230
#endif
225231

226232
/* infix-dhcp-common.c */
227233
int dhcp_option_lookup(const struct lyd_node *id);
228234

229235
/* infix-dhcp-client.c */
230-
int infix_dhcp_client_init(struct confd *confd);
236+
int infix_dhcp_client_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
237+
int infix_dhcp_client_candidate_init(struct confd *confd);
231238

232239
/* infix-dhcp-server.c */
233-
int infix_dhcp_server_init(struct confd *confd);
234-
235-
/* ietf-factory-default */
236-
int ietf_factory_default_init(struct confd *confd);
240+
int infix_dhcp_server_candidate_init(struct confd *confd);
241+
int infix_dhcp_server_rpc_init(struct confd *confd);
242+
int infix_dhcp_server_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
237243

238244
/* ietf-routing */
239-
int ietf_routing_init(struct confd *confd);
245+
int ietf_routing_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
240246

241247
/* infix-factory.c */
242-
int infix_factory_init(struct confd *confd);
248+
int infix_factory_rpc_init(struct confd *confd);
243249

244-
/* infix-factory.c */
245-
int infix_meta_init(struct confd *confd);
250+
/* ietf-factory-default */
251+
int ietf_factory_default_rpc_init(struct confd *confd);
252+
253+
/* infix-meta.c */
254+
int infix_meta_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
246255

247256
/* infix-system-software.c */
248-
int infix_system_sw_init(struct confd *confd);
257+
int infix_system_sw_rpc_init(struct confd *confd);
249258

250259
/* infix-services.c */
251-
int infix_services_init(struct confd *confd);
260+
int infix_services_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
252261

253262
/* ietf-hardware.c */
254-
int ietf_hardware_init(struct confd *confd);
263+
int ietf_hardware_candidate_init(struct confd *confd);
264+
int ietf_hardware_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
255265

256266
/* ietf-keystore.c */
257-
int ietf_keystore_init(struct confd *confd);
267+
#define SSH_HOSTKEYS "/etc/ssh/hostkeys"
268+
#define SSH_HOSTKEYS_NEXT SSH_HOSTKEYS"+"
269+
int ietf_keystore_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
258270

259271
/* infix-firewall.c */
260-
int infix_firewall_init(struct confd *confd);
272+
int infix_firewall_rpc_init(struct confd *confd);
273+
int infix_firewall_candidate_init(struct confd *confd);
274+
int infix_firewall_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
261275

262276
#endif /* CONFD_CORE_H_ */

src/confd/src/ietf-factory-default.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static int factory_reset(sr_session_ctx_t *session, uint32_t sub_id, const char
1616
return 0;
1717
}
1818

19-
int ietf_factory_default_init(struct confd *confd)
19+
int ietf_factory_default_rpc_init(struct confd *confd)
2020
{
2121
int rc;
2222

0 commit comments

Comments
 (0)