Skip to content

Commit e850e52

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 16d338b commit e850e52

File tree

13 files changed

+427
-492
lines changed

13 files changed

+427
-492
lines changed

src/confd/src/core.c

Lines changed: 131 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

@@ -92,6 +93,97 @@ int core_post_hook(sr_session_ctx_t *session, uint32_t sub_id, const char *modul
9293
return SR_ERR_OK;
9394
}
9495

96+
static int change_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *module_name,
97+
const char *xpath, sr_event_t event, uint32_t request_id, void *_confd)
98+
{
99+
struct confd *confd = _confd;
100+
char *output = NULL;
101+
struct lyd_node *diff = NULL, *config = NULL;
102+
sr_data_t *cfg = NULL;
103+
104+
int rc = SR_ERR_OK;
105+
static uint32_t last_id = 0;
106+
static uint32_t last_event = -1;
107+
if (request_id == last_id && last_event == event)
108+
return SR_ERR_OK;
109+
last_id = request_id;
110+
last_event = event;
111+
112+
if (event == SR_EV_CHANGE || event == SR_EV_DONE) {
113+
rc = srx_get_diff(session, &diff);
114+
if (rc != SR_ERR_OK) {
115+
ERROR("Failed to get diff: %d", rc);
116+
return rc;
117+
}
118+
rc = sr_get_data(session, "//.", 0, 0, 0, &cfg);
119+
if (rc || !cfg)
120+
goto free_diff;
121+
122+
config = cfg->tree;
123+
}
124+
125+
// For logging: Write to file instead of using ERROR() to avoid truncation
126+
if (diff) {
127+
FILE *f = fopen("/tmp/sysrepo_diff.json", "w");
128+
if (f) {
129+
lyd_print_file(f, diff, LYD_JSON,
130+
LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL);
131+
fclose(f);
132+
ERROR("req_id=%u event=%d module=%s - Full diff written to /tmp/sysrepo_diff.json",
133+
request_id, event, module_name);
134+
} else {
135+
// Fallback: print to memory but limit what we log
136+
lyd_print_mem(&output, diff, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
137+
if (output) {
138+
// Only log first 500 chars to avoid truncation
139+
ERROR("req_id=%u event=%d module=%s - Diff (truncated): %.500s...",
140+
request_id, event, module_name, output);
141+
free(output);
142+
}
143+
}
144+
}
145+
146+
/* ietf-interfaces */
147+
ietf_interfaces_change(session, config, diff, event, confd);
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_change(session, config, diff, event, confd);
157+
158+
/* ietf-syslog*/
159+
ietf_syslog_change(session, config, diff, event, confd);
160+
161+
/* ietf-system */
162+
ietf_system_change(session, config, diff, event, confd);
163+
164+
/* infix-containers */
165+
infix_containers_change(session, config, diff, event, confd);
166+
167+
/* ietf-hardware */
168+
ietf_hardware_change(session, config, diff, event, confd);
169+
170+
/* ietf-routing */
171+
ietf_routing_change(session, config, diff, event, confd);
172+
173+
/* infix-dhcp-server */
174+
infix_dhcp_server_change(session, config, diff, event, confd);
175+
176+
/* infix-firewall */
177+
infix_firewall_change(session, config, diff, event, confd);
178+
179+
if (cfg)
180+
sr_release_data(cfg);
181+
free_diff:
182+
lyd_free_tree(diff);
183+
return rc;
184+
}
185+
186+
95187
int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv)
96188
{
97189
int log_opts = LOG_PID | LOG_NDELAY;
@@ -136,57 +228,78 @@ int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv)
136228
*/
137229
confd.ifquirks = json_load_file("/etc/product/interface-quirks.json", 0, NULL);
138230

139-
rc = ietf_interfaces_init(&confd);
140-
if (rc)
141-
goto err;
142-
rc = ietf_keystore_init(&confd);
143-
if (rc)
144-
goto err;
145-
rc = ietf_syslog_init(&confd);
146-
if (rc)
231+
REGISTER_CHANGE(confd.session, "ietf-interfaces", "//.",
232+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
233+
REGISTER_CHANGE(confd.session, "infix-dhcp-client", "//.",
234+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
235+
REGISTER_CHANGE(confd.session, "ietf-keystore", "//.",
236+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
237+
REGISTER_CHANGE(confd.session, "ietf-syslog", "//.",
238+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
239+
REGISTER_CHANGE(confd.session, "infix-services", "//.",
240+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
241+
REGISTER_CHANGE(confd.session, "ietf-system", "//.",
242+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
243+
REGISTER_CHANGE(confd.session, "ieee802-dot1ab-lldp", "//.",
244+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
245+
#ifdef CONTAINERS
246+
REGISTER_CHANGE(confd.session, "infix-containers", "//.",
247+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
248+
#endif
249+
REGISTER_CHANGE(confd.session, "infix-dhcp-server", "//.",
250+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
251+
252+
REGISTER_CHANGE(confd.session, "ietf-routing", "//.",
253+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
254+
255+
REGISTER_CHANGE(confd.session, "ietf-hardware", "//.",
256+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
257+
REGISTER_CHANGE(confd.session, "infix-firewall", "//.",
258+
SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub);
259+
260+
rc = ietf_keystore_update_init(&confd);
261+
if(rc)
147262
goto err;
148263
rc = ietf_system_init(&confd);
149264
if (rc)
150265
goto err;
151266
rc = infix_containers_init(&confd);
152267
if (rc)
153268
goto err;
154-
rc = infix_dhcp_client_init(&confd);
155-
if (rc)
156-
goto err;
269+
157270
rc = infix_dhcp_server_init(&confd);
158271
if (rc)
159272
goto err;
273+
160274
rc = infix_factory_init(&confd);
161275
if (rc)
162276
goto err;
277+
163278
rc = ietf_factory_default_init(&confd);
164279
if (rc)
165280
goto err;
166-
rc = ietf_routing_init(&confd);
167-
if (rc)
168-
goto err;
281+
169282
rc = infix_meta_init(&confd);
170283
if (rc)
171284
goto err;
285+
172286
rc = infix_system_sw_init(&confd);
173287
if (rc)
174288
goto err;
175-
rc = infix_services_init(&confd);
176-
if (rc)
177-
goto err;
289+
178290
rc = ietf_hardware_init(&confd);
179291
if (rc)
180292
goto err;
293+
181294
rc = infix_firewall_init(&confd);
182295
if (rc)
183296
goto err;
184297

185298
/* YOUR_INIT GOES HERE */
186299

187300
return SR_ERR_OK;
188-
189301
err:
302+
fail:
190303
ERROR("init failed: %s", sr_strerror(rc));
191304
if (confd.root)
192305
json_decref(confd.root);

src/confd/src/core.h

Lines changed: 24 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,68 @@ 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_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
214218

215219
/* ietf-system.c */
216220
int ietf_system_init (struct confd *confd);
217221
int hostnamefmt (struct confd *confd, const char *fmt, char *hostnm, size_t hostlen, char *domain, size_t domlen);
222+
int ietf_system_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
218223

219224
/* infix-containers.c */
220225
#ifdef CONTAINERS
226+
int infix_containers_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
221227
int infix_containers_init(struct confd *confd);
222228
#else
223229
static inline int infix_containers_init(struct confd *confd) { return 0; }
230+
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; }
224231
#endif
225232

226233
/* infix-dhcp-common.c */
227234
int dhcp_option_lookup(const struct lyd_node *id);
228235

229236
/* infix-dhcp-client.c */
230-
int infix_dhcp_client_init(struct confd *confd);
237+
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);
238+
int infix_dhcp_client_cand_init(struct confd *confd);
231239

232240
/* infix-dhcp-server.c */
233241
int infix_dhcp_server_init(struct confd *confd);
234-
235-
/* ietf-factory-default */
236-
int ietf_factory_default_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 */
242248
int infix_factory_init(struct confd *confd);
243249

244-
/* infix-factory.c */
250+
/* ietf-factory-default */
251+
int ietf_factory_default_init(struct confd *confd);
252+
253+
/* infix-meta.c */
245254
int infix_meta_init(struct confd *confd);
246255

247256
/* infix-system-software.c */
248257
int infix_system_sw_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 */
254263
int ietf_hardware_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);
258-
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);
270+
int ietf_keystore_update_init(struct confd *confd);
259271
/* infix-firewall.c */
260272
int infix_firewall_init(struct confd *confd);
273+
int infix_firewall_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd);
261274

262275
#endif /* CONFD_CORE_H_ */

src/confd/src/ietf-hardware.c

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -183,26 +183,15 @@ static int hardware_cand(sr_session_ctx_t *session, uint32_t sub_id, const char
183183
return err;
184184
}
185185

186-
static int change_hardware(sr_session_ctx_t *session, uint32_t sub_id, const char *module,
187-
const char *xpath, sr_event_t event, unsigned request_id, void *priv)
186+
int ietf_hardware_change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd_node *diff, sr_event_t event, struct confd *confd)
188187
{
189-
struct lyd_node *diff, *difs = NULL, *dif = NULL, *cifs = NULL, *cif = NULL;
190-
sr_data_t *cfg;
188+
struct lyd_node *difs = NULL, *dif = NULL, *cifs = NULL, *cif = NULL;
191189
int rc = SR_ERR_OK;
192-
struct confd *confd = (struct confd *)priv;
193190

194-
if (event != SR_EV_DONE)
191+
if (event != SR_EV_DONE || !lydx_find_xpathf(diff, XPATH_BASE_))
195192
return SR_ERR_OK;
196193

197-
rc = sr_get_data(session, XPATH_BASE_ "//.", 0, 0, 0, &cfg);
198-
if (rc || !cfg)
199-
goto err;
200-
201-
rc = srx_get_diff(session, &diff);
202-
if (rc)
203-
goto err_release_data;
204-
205-
cifs = lydx_get_descendant(cfg->tree, "hardware", "component", NULL);
194+
cifs = lydx_get_descendant(config, "hardware", "component", NULL);
206195
difs = lydx_get_descendant(diff, "hardware", "component", NULL);
207196

208197
LYX_LIST_FOR_EACH(difs, dif, "component") {
@@ -216,7 +205,7 @@ static int change_hardware(sr_session_ctx_t *session, uint32_t sub_id, const cha
216205
if (op == LYDX_OP_DELETE) {
217206
if (usb_authorize(confd->root, name, 0)) {
218207
rc = SR_ERR_INTERNAL;
219-
goto err_release_diff;
208+
goto err;;
220209
}
221210
continue;
222211
}
@@ -233,23 +222,18 @@ static int change_hardware(sr_session_ctx_t *session, uint32_t sub_id, const cha
233222
admin_state = lydx_get_cattr(state, "admin-state");
234223
if (usb_authorize(confd->root, name, !strcmp(admin_state, "unlocked"))) {
235224
rc = SR_ERR_INTERNAL;
236-
goto err_release_diff;
225+
goto err;;
237226
}
238227
}
239228
}
240229

241-
err_release_diff:
242-
lyd_free_tree(diff);
243-
err_release_data:
244-
sr_release_data(cfg);
245230
err:
246231
return rc;
247232
}
248233
int ietf_hardware_init(struct confd *confd)
249234
{
250235
int rc = 0;
251236

252-
REGISTER_CHANGE(confd->session, "ietf-hardware", XPATH_BASE_, 0, change_hardware, confd, &confd->sub);
253237
REGISTER_CHANGE(confd->cand, "ietf-hardware", XPATH_BASE_,
254238
SR_SUBSCR_UPDATE, hardware_cand, confd, &confd->sub);
255239

0 commit comments

Comments
 (0)