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
[MGMTD_MD5_HASH_STR_HEX_LEN
];
22 char time_str
[MGMTD_COMMIT_TIME_STR_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
= NULL
;
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 void mgmt_history_hash(const char *input_str
, char *hash
)
60 unsigned char digest
[MGMTD_MD5_HASH_LEN
];
63 memset(&ctx
, 0, sizeof(ctx
));
65 MD5Update(&ctx
, input_str
, strlen(input_str
));
66 MD5Final(digest
, &ctx
);
68 for (i
= 0; i
< MGMTD_MD5_HASH_LEN
; i
++)
69 snprintf(&hash
[i
* 2], MGMTD_MD5_HASH_STR_HEX_LEN
, "%02x",
70 (unsigned int)digest
[i
]);
73 static struct mgmt_cmt_info_t
*mgmt_history_create_cmt_rec(void)
75 struct mgmt_cmt_info_t
*new;
76 struct mgmt_cmt_info_t
*cmt_info
;
77 struct mgmt_cmt_info_t
*last_cmt_info
= NULL
;
78 struct timeval cmt_recd_tv
;
80 new = XCALLOC(MTYPE_MGMTD_CMT_INFO
, sizeof(struct mgmt_cmt_info_t
));
81 gettimeofday(&cmt_recd_tv
, NULL
);
82 mgmt_realtime_to_string(&cmt_recd_tv
, new->time_str
,
83 sizeof(new->time_str
));
84 mgmt_history_hash(new->time_str
, new->cmtid_str
);
85 snprintf(new->cmt_json_file
, sizeof(new->cmt_json_file
),
86 MGMTD_COMMIT_FILE_PATH
, new->cmtid_str
);
88 if (mgmt_cmt_infos_count(&mm
->cmts
) == MGMTD_MAX_COMMIT_LIST
) {
89 FOREACH_CMT_REC (mm
, cmt_info
)
90 last_cmt_info
= cmt_info
;
93 mgmt_history_remove_file(last_cmt_info
->cmt_json_file
);
94 mgmt_cmt_infos_del(&mm
->cmts
, last_cmt_info
);
95 XFREE(MTYPE_MGMTD_CMT_INFO
, last_cmt_info
);
99 mgmt_cmt_infos_add_head(&mm
->cmts
, new);
103 static struct mgmt_cmt_info_t
*mgmt_history_find_cmt_record(const char *cmtid_str
)
105 struct mgmt_cmt_info_t
*cmt_info
;
107 FOREACH_CMT_REC (mm
, cmt_info
) {
108 if (strncmp(cmt_info
->cmtid_str
, cmtid_str
,
109 MGMTD_MD5_HASH_STR_HEX_LEN
) == 0)
116 static bool mgmt_history_read_cmt_record_index(void)
119 struct mgmt_cmt_info_t cmt_info
;
120 struct mgmt_cmt_info_t
*new;
123 fp
= fopen(MGMTD_COMMIT_INDEX_FILE_NAME
, "rb");
125 zlog_err("Failed to open file %s rb mode",
126 MGMTD_COMMIT_INDEX_FILE_NAME
);
130 while ((fread(&cmt_info
, sizeof(cmt_info
), 1, fp
)) > 0) {
131 if (cnt
< MGMTD_MAX_COMMIT_LIST
) {
132 if (!mgmt_history_record_exists(cmt_info
.cmt_json_file
)) {
134 "Commit record present in index_file, but commit file %s missing",
135 cmt_info
.cmt_json_file
);
139 new = XCALLOC(MTYPE_MGMTD_CMT_INFO
,
140 sizeof(struct mgmt_cmt_info_t
));
141 memcpy(new, &cmt_info
, sizeof(struct mgmt_cmt_info_t
));
142 mgmt_cmt_infos_add_tail(&mm
->cmts
, new);
144 zlog_err("More records found in index file %s",
145 MGMTD_COMMIT_INDEX_FILE_NAME
);
157 static bool mgmt_history_dump_cmt_record_index(void)
161 struct mgmt_cmt_info_t
*cmt_info
;
162 struct mgmt_cmt_info_t cmt_info_set
[10];
165 mgmt_history_remove_file((char *)MGMTD_COMMIT_INDEX_FILE_NAME
);
166 fp
= fopen(MGMTD_COMMIT_INDEX_FILE_NAME
, "ab");
168 zlog_err("Failed to open file %s ab mode",
169 MGMTD_COMMIT_INDEX_FILE_NAME
);
173 FOREACH_CMT_REC (mm
, cmt_info
) {
174 memcpy(&cmt_info_set
[cnt
], cmt_info
,
175 sizeof(struct mgmt_cmt_info_t
));
184 ret
= fwrite(&cmt_info_set
, sizeof(struct mgmt_cmt_info_t
), cnt
, fp
);
187 zlog_err("Write record failed");
194 static int mgmt_history_rollback_to_cmt(struct vty
*vty
,
195 struct mgmt_cmt_info_t
*cmt_info
,
198 struct mgmt_ds_ctx
*src_ds_ctx
;
199 struct mgmt_ds_ctx
*dst_ds_ctx
;
203 vty_out(vty
, "ERROR: Rollback already in progress!\n");
207 src_ds_ctx
= mgmt_ds_get_ctx_by_id(mm
, MGMTD_DS_CANDIDATE
);
209 vty_out(vty
, "ERROR: Couldnot access Candidate datastore!\n");
214 * Note: Write lock on src_ds is not required. This is already
215 * taken in 'conf te'.
217 dst_ds_ctx
= mgmt_ds_get_ctx_by_id(mm
, MGMTD_DS_RUNNING
);
219 vty_out(vty
, "ERROR: Couldnot access Running datastore!\n");
223 ret
= mgmt_ds_write_lock(dst_ds_ctx
);
226 "Failed to lock the DS %u for rollback Reason: %s!\n",
227 MGMTD_DS_RUNNING
, strerror(ret
));
231 if (!skip_file_load
) {
232 ret
= mgmt_ds_load_config_from_file(
233 src_ds_ctx
, cmt_info
->cmt_json_file
, false);
235 mgmt_ds_unlock(dst_ds_ctx
);
237 "Error with parsing the file with error code %d\n",
243 /* Internally trigger a commit-request. */
244 ret
= mgmt_txn_rollback_trigger_cfg_apply(src_ds_ctx
, dst_ds_ctx
);
246 mgmt_ds_unlock(dst_ds_ctx
);
248 "Error with creating commit apply txn with error code %d\n",
253 mgmt_history_dump_cmt_record_index();
256 * Block the rollback command from returning till the rollback
257 * is completed. On rollback completion mgmt_history_rollback_complete()
258 * shall be called to resume the rollback command return to VTYSH.
260 vty
->mgmt_req_pending
= true;
265 void mgmt_history_rollback_complete(bool success
)
267 vty_mgmt_resume_response(rollback_vty
, success
);
271 int mgmt_history_rollback_by_id(struct vty
*vty
, const char *cmtid_str
)
274 struct mgmt_cmt_info_t
*cmt_info
;
276 if (!mgmt_cmt_infos_count(&mm
->cmts
) ||
277 !mgmt_history_find_cmt_record(cmtid_str
)) {
278 vty_out(vty
, "Invalid commit Id\n");
282 FOREACH_CMT_REC (mm
, cmt_info
) {
283 if (strncmp(cmt_info
->cmtid_str
, cmtid_str
,
284 MGMTD_MD5_HASH_STR_HEX_LEN
) == 0) {
285 ret
= mgmt_history_rollback_to_cmt(vty
, cmt_info
, false);
289 mgmt_history_remove_file(cmt_info
->cmt_json_file
);
290 mgmt_cmt_infos_del(&mm
->cmts
, cmt_info
);
291 XFREE(MTYPE_MGMTD_CMT_INFO
, cmt_info
);
297 int mgmt_history_rollback_n(struct vty
*vty
, int num_cmts
)
301 struct mgmt_cmt_info_t
*cmt_info
;
307 cmts
= mgmt_cmt_infos_count(&mm
->cmts
);
308 if ((int)cmts
< num_cmts
) {
310 "Number of commits found (%d) less than required to rollback\n",
315 if ((int)cmts
== 1 || (int)cmts
== num_cmts
) {
317 "Number of commits found (%d), Rollback of last commit is not supported\n",
322 FOREACH_CMT_REC (mm
, cmt_info
) {
323 if (cnt
== num_cmts
) {
324 ret
= mgmt_history_rollback_to_cmt(vty
, cmt_info
, false);
329 mgmt_history_remove_file(cmt_info
->cmt_json_file
);
330 mgmt_cmt_infos_del(&mm
->cmts
, cmt_info
);
331 XFREE(MTYPE_MGMTD_CMT_INFO
, cmt_info
);
334 if (!mgmt_cmt_infos_count(&mm
->cmts
)) {
335 mgmt_ds_reset_candidate();
336 ret
= mgmt_history_rollback_to_cmt(vty
, cmt_info
, true);
342 void show_mgmt_cmt_history(struct vty
*vty
)
344 struct mgmt_cmt_info_t
*cmt_info
;
347 vty_out(vty
, "Last 10 commit history:\n");
348 vty_out(vty
, " Sl.No\tCommit-ID(HEX)\t\t\t Commit-Record-Time\n");
349 FOREACH_CMT_REC (mm
, cmt_info
) {
350 vty_out(vty
, " %d\t%s %s\n", slno
, cmt_info
->cmtid_str
,
356 void mgmt_history_new_record(struct mgmt_ds_ctx
*ds_ctx
)
358 struct mgmt_cmt_info_t
*cmt_info
= mgmt_history_create_cmt_rec();
359 mgmt_ds_dump_ds_to_file(cmt_info
->cmt_json_file
, ds_ctx
);
360 mgmt_history_dump_cmt_record_index();
363 void mgmt_history_init(void)
365 /* Create commit record for previously stored commit-apply */
366 mgmt_cmt_infos_init(&mm
->cmts
);
367 mgmt_history_read_cmt_record_index();
370 void mgmt_history_destroy(void)
372 struct mgmt_cmt_info_t
*cmt_info
;
374 FOREACH_CMT_REC(mm
, cmt_info
) {
375 mgmt_cmt_infos_del(&mm
->cmts
, cmt_info
);
376 XFREE(MTYPE_MGMTD_CMT_INFO
, cmt_info
);
379 mgmt_cmt_infos_fini(&mm
->cmts
);