1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2021 Vmware, Inc.
4 * Pushpasis Sarkar <spushpasis@vmware.com>
5 * Copyright (c) 2023, LabN Consulting, L.L.C.
13 #include "mgmt_fe_client.h"
14 #include "mgmtd/mgmt.h"
15 #include "mgmtd/mgmt_ds.h"
16 #include "mgmtd/mgmt_history.h"
18 struct mgmt_cmt_info_t
{
19 struct mgmt_cmt_infos_item cmts
;
21 char cmtid_str
[MGMT_SHORT_TIME_MAX_LEN
];
22 char time_str
[MGMT_LONG_TIME_MAX_LEN
];
23 char cmt_json_file
[PATH_MAX
];
27 DECLARE_DLIST(mgmt_cmt_infos
, struct mgmt_cmt_info_t
, cmts
);
29 #define FOREACH_CMT_REC(mm, cmt_info) \
30 frr_each_safe (mgmt_cmt_infos, &mm->cmts, cmt_info)
33 * The only instance of VTY session that has triggered an ongoing
34 * config rollback operation.
36 static struct vty
*rollback_vty
;
38 static bool mgmt_history_record_exists(char *file_path
)
42 exist
= access(file_path
, F_OK
);
49 static void mgmt_history_remove_file(char *name
)
51 if (remove(name
) == 0)
52 zlog_debug("Old commit info deletion succeeded");
54 zlog_err("Old commit info deletion failed");
57 static struct mgmt_cmt_info_t
*mgmt_history_new_cmt_info(void)
59 struct mgmt_cmt_info_t
*new;
63 new = XCALLOC(MTYPE_MGMTD_CMT_INFO
, sizeof(struct mgmt_cmt_info_t
));
65 clock_gettime(CLOCK_REALTIME
, &tv
);
66 localtime_r(&tv
.tv_sec
, &tm
);
68 mgmt_time_to_string(&tv
, true, new->time_str
, sizeof(new->time_str
));
69 mgmt_time_to_string(&tv
, false, new->cmtid_str
, sizeof(new->cmtid_str
));
70 snprintf(new->cmt_json_file
, sizeof(new->cmt_json_file
),
71 MGMTD_COMMIT_FILE_PATH
, new->cmtid_str
);
76 static struct mgmt_cmt_info_t
*mgmt_history_create_cmt_rec(void)
78 struct mgmt_cmt_info_t
*new = mgmt_history_new_cmt_info();
79 struct mgmt_cmt_info_t
*cmt_info
;
80 struct mgmt_cmt_info_t
*last_cmt_info
= NULL
;
82 if (mgmt_cmt_infos_count(&mm
->cmts
) == MGMTD_MAX_COMMIT_LIST
) {
83 FOREACH_CMT_REC (mm
, cmt_info
)
84 last_cmt_info
= cmt_info
;
87 mgmt_history_remove_file(last_cmt_info
->cmt_json_file
);
88 mgmt_cmt_infos_del(&mm
->cmts
, last_cmt_info
);
89 XFREE(MTYPE_MGMTD_CMT_INFO
, last_cmt_info
);
93 mgmt_cmt_infos_add_head(&mm
->cmts
, new);
97 static struct mgmt_cmt_info_t
*
98 mgmt_history_find_cmt_record(const char *cmtid_str
)
100 struct mgmt_cmt_info_t
*cmt_info
;
102 FOREACH_CMT_REC (mm
, cmt_info
) {
103 if (strcmp(cmt_info
->cmtid_str
, cmtid_str
) == 0)
110 static bool mgmt_history_read_cmt_record_index(void)
113 struct mgmt_cmt_info_t cmt_info
;
114 struct mgmt_cmt_info_t
*new;
117 fp
= fopen(MGMTD_COMMIT_INDEX_FILE_NAME
, "rb");
119 zlog_err("Failed to open file %s rb mode",
120 MGMTD_COMMIT_INDEX_FILE_NAME
);
124 while ((fread(&cmt_info
, sizeof(cmt_info
), 1, fp
)) > 0) {
125 if (cnt
< MGMTD_MAX_COMMIT_LIST
) {
126 if (!mgmt_history_record_exists(
127 cmt_info
.cmt_json_file
)) {
129 "Commit record present in index_file, but commit file %s missing",
130 cmt_info
.cmt_json_file
);
134 new = XCALLOC(MTYPE_MGMTD_CMT_INFO
,
135 sizeof(struct mgmt_cmt_info_t
));
136 memcpy(new, &cmt_info
, sizeof(struct mgmt_cmt_info_t
));
137 mgmt_cmt_infos_add_tail(&mm
->cmts
, new);
139 zlog_err("More records found in index file %s",
140 MGMTD_COMMIT_INDEX_FILE_NAME
);
152 static bool mgmt_history_dump_cmt_record_index(void)
156 struct mgmt_cmt_info_t
*cmt_info
;
157 struct mgmt_cmt_info_t cmt_info_set
[10];
160 mgmt_history_remove_file((char *)MGMTD_COMMIT_INDEX_FILE_NAME
);
161 fp
= fopen(MGMTD_COMMIT_INDEX_FILE_NAME
, "ab");
163 zlog_err("Failed to open file %s ab mode",
164 MGMTD_COMMIT_INDEX_FILE_NAME
);
168 FOREACH_CMT_REC (mm
, cmt_info
) {
169 memcpy(&cmt_info_set
[cnt
], cmt_info
,
170 sizeof(struct mgmt_cmt_info_t
));
179 ret
= fwrite(&cmt_info_set
, sizeof(struct mgmt_cmt_info_t
), cnt
, fp
);
182 zlog_err("Write record failed");
189 static int mgmt_history_rollback_to_cmt(struct vty
*vty
,
190 struct mgmt_cmt_info_t
*cmt_info
,
193 struct mgmt_ds_ctx
*src_ds_ctx
;
194 struct mgmt_ds_ctx
*dst_ds_ctx
;
198 vty_out(vty
, "ERROR: Rollback already in progress!\n");
202 src_ds_ctx
= mgmt_ds_get_ctx_by_id(mm
, MGMTD_DS_CANDIDATE
);
204 vty_out(vty
, "ERROR: Couldnot access Candidate datastore!\n");
209 * Note: Write lock on src_ds is not required. This is already
210 * taken in 'conf te'.
212 dst_ds_ctx
= mgmt_ds_get_ctx_by_id(mm
, MGMTD_DS_RUNNING
);
214 vty_out(vty
, "ERROR: Couldnot access Running datastore!\n");
218 ret
= mgmt_ds_write_lock(dst_ds_ctx
);
221 "Failed to lock the DS %u for rollback Reason: %s!\n",
222 MGMTD_DS_RUNNING
, strerror(ret
));
226 if (!skip_file_load
) {
227 ret
= mgmt_ds_load_config_from_file(
228 src_ds_ctx
, cmt_info
->cmt_json_file
, false);
230 mgmt_ds_unlock(dst_ds_ctx
);
232 "Error with parsing the file with error code %d\n",
238 /* Internally trigger a commit-request. */
239 ret
= mgmt_txn_rollback_trigger_cfg_apply(src_ds_ctx
, dst_ds_ctx
);
241 mgmt_ds_unlock(dst_ds_ctx
);
243 "Error with creating commit apply txn with error code %d\n",
248 mgmt_history_dump_cmt_record_index();
251 * Block the rollback command from returning till the rollback
252 * is completed. On rollback completion mgmt_history_rollback_complete()
253 * shall be called to resume the rollback command return to VTYSH.
255 vty
->mgmt_req_pending
= true;
260 void mgmt_history_rollback_complete(bool success
)
262 vty_mgmt_resume_response(rollback_vty
, success
);
266 int mgmt_history_rollback_by_id(struct vty
*vty
, const char *cmtid_str
)
269 struct mgmt_cmt_info_t
*cmt_info
;
271 if (!mgmt_cmt_infos_count(&mm
->cmts
) ||
272 !mgmt_history_find_cmt_record(cmtid_str
)) {
273 vty_out(vty
, "Invalid commit Id\n");
277 FOREACH_CMT_REC (mm
, cmt_info
) {
278 if (strcmp(cmt_info
->cmtid_str
, cmtid_str
) == 0) {
279 ret
= mgmt_history_rollback_to_cmt(vty
, cmt_info
,
284 mgmt_history_remove_file(cmt_info
->cmt_json_file
);
285 mgmt_cmt_infos_del(&mm
->cmts
, cmt_info
);
286 XFREE(MTYPE_MGMTD_CMT_INFO
, cmt_info
);
292 int mgmt_history_rollback_n(struct vty
*vty
, int num_cmts
)
296 struct mgmt_cmt_info_t
*cmt_info
;
302 cmts
= mgmt_cmt_infos_count(&mm
->cmts
);
303 if ((int)cmts
< num_cmts
) {
305 "Number of commits found (%d) less than required to rollback\n",
310 if ((int)cmts
== 1 || (int)cmts
== num_cmts
) {
312 "Number of commits found (%d), Rollback of last commit is not supported\n",
317 FOREACH_CMT_REC (mm
, cmt_info
) {
318 if (cnt
== num_cmts
) {
319 ret
= mgmt_history_rollback_to_cmt(vty
, cmt_info
,
325 mgmt_history_remove_file(cmt_info
->cmt_json_file
);
326 mgmt_cmt_infos_del(&mm
->cmts
, cmt_info
);
327 XFREE(MTYPE_MGMTD_CMT_INFO
, cmt_info
);
330 if (!mgmt_cmt_infos_count(&mm
->cmts
)) {
331 mgmt_ds_reset_candidate();
332 ret
= mgmt_history_rollback_to_cmt(vty
, cmt_info
, true);
338 void show_mgmt_cmt_history(struct vty
*vty
)
340 struct mgmt_cmt_info_t
*cmt_info
;
343 vty_out(vty
, "Last 10 commit history:\n");
344 vty_out(vty
, "Slot Commit-ID Commit-Record-Time\n");
345 FOREACH_CMT_REC (mm
, cmt_info
) {
346 vty_out(vty
, "%4d %23s %s\n", slno
, cmt_info
->cmtid_str
,
352 void mgmt_history_new_record(struct mgmt_ds_ctx
*ds_ctx
)
354 struct mgmt_cmt_info_t
*cmt_info
= mgmt_history_create_cmt_rec();
356 mgmt_ds_dump_ds_to_file(cmt_info
->cmt_json_file
, ds_ctx
);
357 mgmt_history_dump_cmt_record_index();
360 void mgmt_history_init(void)
362 /* Create commit record for previously stored commit-apply */
363 mgmt_cmt_infos_init(&mm
->cmts
);
364 mgmt_history_read_cmt_record_index();
367 void mgmt_history_destroy(void)
369 struct mgmt_cmt_info_t
*cmt_info
;
371 FOREACH_CMT_REC(mm
, cmt_info
) {
372 mgmt_cmt_infos_del(&mm
->cmts
, cmt_info
);
373 XFREE(MTYPE_MGMTD_CMT_INFO
, cmt_info
);
376 mgmt_cmt_infos_fini(&mm
->cmts
);