Skip to content

Commit 998b2e7

Browse files
committed
Initial prototype for confd refactor
1 parent 0fb8433 commit 998b2e7

File tree

13 files changed

+433
-492
lines changed

13 files changed

+433
-492
lines changed

src/confd/src/core.c

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

3+
#include <srx/srx_val.h>
34
#include <srx/common.h>
45
#include "core.h"
56

@@ -88,6 +89,118 @@ int core_post_hook(sr_session_ctx_t *session, uint32_t sub_id, const char *modul
8889
return SR_ERR_OK;
8990
}
9091

92+
static int change_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *module_name,
93+
const char *xpath, sr_event_t event, uint32_t request_id, void *_confd)
94+
{
95+
struct confd *confd = _confd;
96+
char *output = NULL;
97+
struct lyd_node *diff = NULL, *config = NULL;
98+
sr_data_t *cfg = NULL;
99+
100+
int rc = SR_ERR_OK;
101+
static uint32_t last_id = 0;
102+
static uint32_t last_event = -1;
103+
if (request_id == last_id && last_event == event)
104+
return SR_ERR_OK;
105+
last_id = request_id;
106+
last_event = event;
107+
// Free previous diff if it exists
108+
// Get the new diff
109+
110+
if (event == SR_EV_CHANGE || event == SR_EV_DONE) {
111+
rc = srx_get_diff(session, &diff);
112+
if (rc != SR_ERR_OK) {
113+
ERROR("Failed to get diff: %d", rc);
114+
return rc;
115+
}
116+
rc = sr_get_data(session, "//.", 0, 0, 0, &cfg);
117+
if (rc || !cfg)
118+
goto free_diff;
119+
120+
config = cfg->tree;
121+
}
122+
123+
// For logging: Write to file instead of using ERROR() to avoid truncation
124+
if (diff) {
125+
FILE *f = fopen("/tmp/sysrepo_diff.json", "w");
126+
if (f) {
127+
lyd_print_file(f, diff, LYD_JSON,
128+
LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL);
129+
fclose(f);
130+
ERROR("req_id=%u event=%d module=%s - Full diff written to /tmp/sysrepo_diff.json",
131+
request_id, event, module_name);
132+
} else {
133+
// Fallback: print to memory but limit what we log
134+
lyd_print_mem(&output, diff, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
135+
if (output) {
136+
// Only log first 500 chars to avoid truncation
137+
ERROR("req_id=%u event=%d module=%s - Diff (truncated): %.500s...",
138+
request_id, event, module_name, output);
139+
free(output);
140+
}
141+
}
142+
}
143+
/* Prepare diff */
144+
// Iterera igenom alla tills ingen lägger till något mer
145+
146+
/* ietf-interfaces */
147+
ietf_interfaces_change(session, config, diff, event, confd); /* TODO: WiFi depends on keystore */
148+
149+
/* infix-dhcp-client*/
150+
infix_dhcp_client_change(session, config, diff, event, confd);
151+
152+
/* ietf-keystore */
153+
ietf_keystore_change(session, config, diff, event, confd);
154+
155+
/* infix-services */
156+
infix_services_lldp_change(session, config, diff, event, confd);
157+
infix_services_mdns_change(session, config, diff, event, confd); /* TODO: Depends on hostname changes */
158+
infix_services_ssh_change(session, config, diff, event, confd); /* TODO: Depends on keystore changes*/
159+
infix_services_web_change(session, config, diff, event, confd);
160+
infix_services_ttyd_change(session, config, diff, event, confd);
161+
infix_services_restconf_change(session, config, diff, event, confd);
162+
infix_services_netbrowse_change(session, config, diff, event, confd);
163+
164+
/* ietf-syslog*/
165+
ietf_syslog_file_change(session, config, diff, event, confd);
166+
ietf_syslog_remote_change(session, config, diff, event, confd);
167+
ietf_syslog_rotate_change(session, config, diff, event, confd);
168+
ietf_syslog_server_change(session, config, diff, event, confd); /* TODO: Depends on hostname changes? */
169+
170+
/* ietf-system */
171+
ietf_system_change_auth(session, config, diff, event, confd);
172+
ietf_system_change_ntp(session, config, diff, event, confd);
173+
ietf_system_change_dns(session, config, diff, event, confd);
174+
ietf_system_change_editor(session,config, diff, event, confd);
175+
ietf_system_change_clock(session, config, diff, event, confd);
176+
ietf_system_change_hostname(session, config, diff, event, confd);
177+
ietf_system_change_motd(session, config, diff, event, confd);
178+
ietf_system_change_motd_banner(session, config, diff, event, confd);
179+
ietf_system_change_nacm(session, config, diff, event, confd); /* Must be called after ietf_system_change_auth, which create the users */
180+
181+
/* infix-containers */
182+
infix_containers_change(session, config, diff, event, confd);
183+
184+
/* ietf-hardware */
185+
ietf_hardware_change(session, config, diff, event, confd);
186+
187+
/* ietf-routing */
188+
ietf_routing_change(session, config, diff, event, confd);
189+
190+
/* infix-dhcp-server */
191+
infix_dhcp_server_change(session, config, diff, event, confd); /* TODO: Depends on hostname? Line: 350*/
192+
193+
/* infix-firewall */
194+
infix_firewall_change(session, config, diff, event, confd);
195+
196+
if (cfg)
197+
sr_release_data(cfg);
198+
free_diff:
199+
lyd_free_tree(diff);
200+
return rc;
201+
}
202+
203+
91204
int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv)
92205
{
93206
int log_opts = LOG_PID | LOG_NDELAY;
@@ -132,57 +245,78 @@ int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv)
132245
*/
133246
confd.ifquirks = json_load_file("/etc/product/interface-quirks.json", 0, NULL);
134247

135-
rc = ietf_interfaces_init(&confd);
136-
if (rc)
137-
goto err;
138-
rc = ietf_keystore_init(&confd);
139-
if (rc)
140-
goto err;
141-
rc = ietf_syslog_init(&confd);
142-
if (rc)
248+
REGISTER_CHANGE(confd.session, "ietf-interfaces", "//.",
249+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
250+
REGISTER_CHANGE(confd.session, "infix-dhcp-client", "//.",
251+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
252+
REGISTER_CHANGE(confd.session, "ietf-keystore", "//.",
253+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
254+
REGISTER_CHANGE(confd.session, "ietf-syslog", "//.",
255+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
256+
REGISTER_CHANGE(confd.session, "infix-services", "//.",
257+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
258+
REGISTER_CHANGE(confd.session, "ietf-system", "//.",
259+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
260+
REGISTER_CHANGE(confd.session, "ieee802-dot1ab-lldp", "//.",
261+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
262+
#ifdef CONTAINERS
263+
REGISTER_CHANGE(confd.session, "infix-containers", "//.",
264+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
265+
#endif
266+
REGISTER_CHANGE(confd.session, "infix-dhcp-server", "//.",
267+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
268+
269+
REGISTER_CHANGE(confd.session, "ietf-routing", "//.",
270+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
271+
272+
REGISTER_CHANGE(confd.session, "ietf-hardware", "//",
273+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
274+
REGISTER_CHANGE(confd.session, "infix-firewall", "//.",
275+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
276+
277+
rc = ietf_keystore_update_init(&confd);
278+
if(rc)
143279
goto err;
144280
rc = ietf_system_init(&confd);
145281
if (rc)
146282
goto err;
147283
rc = infix_containers_init(&confd);
148284
if (rc)
149285
goto err;
150-
rc = infix_dhcp_client_init(&confd);
151-
if (rc)
152-
goto err;
286+
153287
rc = infix_dhcp_server_init(&confd);
154288
if (rc)
155289
goto err;
290+
156291
rc = infix_factory_init(&confd);
157292
if (rc)
158293
goto err;
294+
159295
rc = ietf_factory_default_init(&confd);
160296
if (rc)
161297
goto err;
162-
rc = ietf_routing_init(&confd);
163-
if (rc)
164-
goto err;
298+
165299
rc = infix_meta_init(&confd);
166300
if (rc)
167301
goto err;
302+
168303
rc = infix_system_sw_init(&confd);
169304
if (rc)
170305
goto err;
171-
rc = infix_services_init(&confd);
172-
if (rc)
173-
goto err;
306+
174307
rc = ietf_hardware_init(&confd);
175308
if (rc)
176309
goto err;
310+
177311
rc = infix_firewall_init(&confd);
178312
if (rc)
179313
goto err;
180314

181315
/* YOUR_INIT GOES HERE */
182316

183317
return SR_ERR_OK;
184-
185318
err:
319+
fail:
186320
ERROR("init failed: %s", sr_strerror(rc));
187321
if (confd.root)
188322
json_decref(confd.root);

src/confd/src/core.h

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,15 @@ struct confd {
126126
sr_subscription_ctx_t *fsub; /* factory-default sub */
127127
json_t *root;
128128
json_t *ifquirks;
129+
struct lyd_node *change;
129130
struct dagger netdag;
130131
};
131132

132133
uint32_t core_hook_prio (void);
133134
int core_pre_hook (sr_session_ctx_t *, uint32_t, const char *, const char *, sr_event_t, unsigned, void *);
134135
int core_post_hook (sr_session_ctx_t *, uint32_t, const char *, const char *, sr_event_t, unsigned, void *);
135136
int core_startup_save (sr_session_ctx_t *, uint32_t, const char *, const char *, sr_event_t, unsigned, void *);
137+
int core_wait_change (sr_session_ctx_t *, uint32_t, const char *, const char *, sr_event_t, unsigned, void *);
136138

137139
static inline int register_change(sr_session_ctx_t *session, const char *module, const char *xpath,
138140
int flags, sr_module_change_cb cb, void *arg, sr_subscription_ctx_t **sub)
@@ -206,57 +208,87 @@ static inline int register_rpc(sr_session_ctx_t *session, const char *xpath,
206208
return rc;
207209
}
208210

211+
209212
/* ietf-interfaces.c */
210-
int ietf_interfaces_init(struct confd *confd);
213+
int ietf_interfaces_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
214+
int ietf_interfaces_cand_init(struct confd *confd);
211215

212216
/* ietf-syslog.c */
213-
int ietf_syslog_init(struct confd *confd);
217+
int ietf_syslog_file_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
218+
int ietf_syslog_remote_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
219+
int ietf_syslog_rotate_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
220+
int ietf_syslog_server_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
214221

215222
/* ietf-system.c */
216223
int ietf_system_init (struct confd *confd);
217224
int hostnamefmt (struct confd *confd, const char *fmt, char *hostnm, size_t hostlen, char *domain, size_t domlen);
225+
int ietf_system_change_hostname(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
226+
int ietf_system_change_editor(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
227+
int ietf_system_change_motd_banner(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
228+
int ietf_system_change_clock(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
229+
int ietf_system_change_motd(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
230+
int ietf_system_change_nacm(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
231+
int ietf_system_change_auth(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
232+
int ietf_system_change_ntp(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
233+
int ietf_system_change_dns(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
218234

219235
/* infix-containers.c */
220236
#ifdef CONTAINERS
237+
int infix_containers_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
221238
int infix_containers_init(struct confd *confd);
222239
#else
223240
static inline int infix_containers_init(struct confd *confd) { return 0; }
241+
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; }
224242
#endif
225243

226244
/* infix-dhcp-common.c */
227245
int dhcp_option_lookup(const struct lyd_node *id);
228246

229247
/* infix-dhcp-client.c */
230-
int infix_dhcp_client_init(struct confd *confd);
248+
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);
249+
int infix_dhcp_client_cand_init(struct confd *confd);
231250

232251
/* infix-dhcp-server.c */
233252
int infix_dhcp_server_init(struct confd *confd);
234-
235-
/* ietf-factory-default */
236-
int ietf_factory_default_init(struct confd *confd);
253+
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);
237254

238255
/* ietf-routing */
239-
int ietf_routing_init(struct confd *confd);
256+
int ietf_routing_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
240257

241258
/* infix-factory.c */
242259
int infix_factory_init(struct confd *confd);
243260

244-
/* infix-factory.c */
261+
/* ietf-factory-default */
262+
int ietf_factory_default_init(struct confd *confd);
263+
264+
/* infix-meta.c */
245265
int infix_meta_init(struct confd *confd);
246266

247267
/* infix-system-software.c */
248268
int infix_system_sw_init(struct confd *confd);
249269

250270
/* infix-services.c */
251-
int infix_services_init(struct confd *confd);
271+
int infix_services_web_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
272+
int infix_services_ssh_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
273+
int infix_services_restconf_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
274+
int infix_services_netbrowse_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
275+
int infix_services_ttyd_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
276+
int infix_services_lldp_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
277+
int infix_services_mdns_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
278+
279+
252280

253281
/* ietf-hardware.c */
254282
int ietf_hardware_init(struct confd *confd);
283+
int ietf_hardware_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
255284

256285
/* ietf-keystore.c */
257-
int ietf_keystore_init(struct confd *confd);
258-
286+
#define SSH_HOSTKEYS "/etc/ssh/hostkeys"
287+
#define SSH_HOSTKEYS_NEXT SSH_HOSTKEYS"+"
288+
int ietf_keystore_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
289+
int ietf_keystore_update_init(struct confd *confd);
259290
/* infix-firewall.c */
260291
int infix_firewall_init(struct confd *confd);
292+
int infix_firewall_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
261293

262294
#endif /* CONFD_CORE_H_ */

0 commit comments

Comments
 (0)