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)
34 static bool mgmt_history_record_exists(char *file_path
)
38 exist
= access(file_path
, F_OK
);
45 static void mgmt_history_remove_file(char *name
)
47 if (remove(name
) == 0)
48 zlog_debug("Old commit info deletion succeeded");
50 zlog_err("Old commit info deletion failed");
53 static void mgmt_history_hash(const char *input_str
, char *hash
)
56 unsigned char digest
[MGMTD_MD5_HASH_LEN
];
59 memset(&ctx
, 0, sizeof(ctx
));
61 MD5Update(&ctx
, input_str
, strlen(input_str
));
62 MD5Final(digest
, &ctx
);
64 for (i
= 0; i
< MGMTD_MD5_HASH_LEN
; i
++)
65 snprintf(&hash
[i
* 2], MGMTD_MD5_HASH_STR_HEX_LEN
, "%02x",
66 (unsigned int)digest
[i
]);
69 static struct mgmt_cmt_info_t
*mgmt_history_create_cmt_rec(void)
71 struct mgmt_cmt_info_t
*new;
72 struct mgmt_cmt_info_t
*cmt_info
;
73 struct mgmt_cmt_info_t
*last_cmt_info
= NULL
;
74 struct timeval cmt_recd_tv
;
76 new = XCALLOC(MTYPE_MGMTD_CMT_INFO
, sizeof(struct mgmt_cmt_info_t
));
77 gettimeofday(&cmt_recd_tv
, NULL
);
78 mgmt_realtime_to_string(&cmt_recd_tv
, new->time_str
,
79 sizeof(new->time_str
));
80 mgmt_history_hash(new->time_str
, new->cmtid_str
);
81 snprintf(new->cmt_json_file
, sizeof(new->cmt_json_file
),
82 MGMTD_COMMIT_FILE_PATH
, new->cmtid_str
);
84 if (mgmt_cmt_infos_count(&mm
->cmts
) == MGMTD_MAX_COMMIT_LIST
) {
85 FOREACH_CMT_REC (mm
, cmt_info
)
86 last_cmt_info
= cmt_info
;
89 mgmt_history_remove_file(last_cmt_info
->cmt_json_file
);
90 mgmt_cmt_infos_del(&mm
->cmts
, last_cmt_info
);
91 XFREE(MTYPE_MGMTD_CMT_INFO
, last_cmt_info
);
95 mgmt_cmt_infos_add_head(&mm
->cmts
, new);
99 static struct mgmt_cmt_info_t
*mgmt_history_find_cmt_record(const char *cmtid_str
)
101 struct mgmt_cmt_info_t
*cmt_info
;
103 FOREACH_CMT_REC (mm
, cmt_info
) {
104 if (strncmp(cmt_info
->cmtid_str
, cmtid_str
,
105 MGMTD_MD5_HASH_STR_HEX_LEN
) == 0)
112 static bool mgmt_history_read_cmt_record_index(void)
115 struct mgmt_cmt_info_t cmt_info
;
116 struct mgmt_cmt_info_t
*new;
119 fp
= fopen(MGMTD_COMMIT_INDEX_FILE_NAME
, "rb");
121 zlog_err("Failed to open file %s rb mode",
122 MGMTD_COMMIT_INDEX_FILE_NAME
);
126 while ((fread(&cmt_info
, sizeof(cmt_info
), 1, fp
)) > 0) {
127 if (cnt
< MGMTD_MAX_COMMIT_LIST
) {
128 if (!mgmt_history_record_exists(cmt_info
.cmt_json_file
)) {
130 "Commit record present in index_file, but commit file %s missing",
131 cmt_info
.cmt_json_file
);
135 new = XCALLOC(MTYPE_MGMTD_CMT_INFO
,
136 sizeof(struct mgmt_cmt_info_t
));
137 memcpy(new, &cmt_info
, sizeof(struct mgmt_cmt_info_t
));
138 mgmt_cmt_infos_add_tail(&mm
->cmts
, new);
140 zlog_err("More records found in index file %s",
141 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
;
197 src_ds_ctx
= mgmt_ds_get_ctx_by_id(mm
, MGMTD_DS_CANDIDATE
);
199 vty_out(vty
, "ERROR: Couldnot access Candidate datastore!\n");
204 * Note: Write lock on src_ds is not required. This is already
205 * taken in 'conf te'.
207 dst_ds_ctx
= mgmt_ds_get_ctx_by_id(mm
, MGMTD_DS_RUNNING
);
209 vty_out(vty
, "ERROR: Couldnot access Running datastore!\n");
213 ret
= mgmt_ds_write_lock(dst_ds_ctx
);
216 "Failed to lock the DS %u for rollback Reason: %s!\n",
217 MGMTD_DS_RUNNING
, strerror(ret
));
221 if (!skip_file_load
) {
222 ret
= mgmt_ds_load_config_from_file(
223 src_ds_ctx
, cmt_info
->cmt_json_file
, false);
225 mgmt_ds_unlock(dst_ds_ctx
);
227 "Error with parsing the file with error code %d\n",
233 /* Internally trigger a commit-request. */
234 ret
= mgmt_txn_rollback_trigger_cfg_apply(src_ds_ctx
, dst_ds_ctx
);
236 mgmt_ds_unlock(dst_ds_ctx
);
238 "Error with creating commit apply txn with error code %d\n",
243 mgmt_history_dump_cmt_record_index();
247 int mgmt_history_rollback_by_id(struct vty
*vty
, const char *cmtid_str
)
250 struct mgmt_cmt_info_t
*cmt_info
;
252 if (!mgmt_cmt_infos_count(&mm
->cmts
) ||
253 !mgmt_history_find_cmt_record(cmtid_str
)) {
254 vty_out(vty
, "Invalid commit Id\n");
258 FOREACH_CMT_REC (mm
, cmt_info
) {
259 if (strncmp(cmt_info
->cmtid_str
, cmtid_str
,
260 MGMTD_MD5_HASH_STR_HEX_LEN
) == 0) {
261 ret
= mgmt_history_rollback_to_cmt(vty
, cmt_info
, false);
265 mgmt_history_remove_file(cmt_info
->cmt_json_file
);
266 mgmt_cmt_infos_del(&mm
->cmts
, cmt_info
);
267 XFREE(MTYPE_MGMTD_CMT_INFO
, cmt_info
);
273 int mgmt_history_rollback_n(struct vty
*vty
, int num_cmts
)
277 struct mgmt_cmt_info_t
*cmt_info
;
283 cmts
= mgmt_cmt_infos_count(&mm
->cmts
);
284 if ((int)cmts
< num_cmts
) {
286 "Number of commits found (%d) less than required to rollback\n",
291 if ((int)cmts
== 1 || (int)cmts
== num_cmts
) {
293 "Number of commits found (%d), Rollback of last commit is not supported\n",
298 FOREACH_CMT_REC (mm
, cmt_info
) {
299 if (cnt
== num_cmts
) {
300 ret
= mgmt_history_rollback_to_cmt(vty
, cmt_info
, false);
305 mgmt_history_remove_file(cmt_info
->cmt_json_file
);
306 mgmt_cmt_infos_del(&mm
->cmts
, cmt_info
);
307 XFREE(MTYPE_MGMTD_CMT_INFO
, cmt_info
);
310 if (!mgmt_cmt_infos_count(&mm
->cmts
)) {
311 mgmt_ds_reset_candidate();
312 ret
= mgmt_history_rollback_to_cmt(vty
, cmt_info
, true);
318 void show_mgmt_cmt_history(struct vty
*vty
)
320 struct mgmt_cmt_info_t
*cmt_info
;
323 vty_out(vty
, "Last 10 commit history:\n");
324 vty_out(vty
, " Sl.No\tCommit-ID(HEX)\t\t\t Commit-Record-Time\n");
325 FOREACH_CMT_REC (mm
, cmt_info
) {
326 vty_out(vty
, " %d\t%s %s\n", slno
, cmt_info
->cmtid_str
,
332 void mgmt_history_new_record(struct mgmt_ds_ctx
*ds_ctx
)
334 struct mgmt_cmt_info_t
*cmt_info
= mgmt_history_create_cmt_rec();
335 mgmt_ds_dump_ds_to_file(cmt_info
->cmt_json_file
, ds_ctx
);
336 mgmt_history_dump_cmt_record_index();
339 void mgmt_history_init(void)
341 /* Create commit record for previously stored commit-apply */
342 mgmt_cmt_infos_init(&mm
->cmts
);
343 mgmt_history_read_cmt_record_index();
346 void mgmt_history_destroy(void)
348 struct mgmt_cmt_info_t
*cmt_info
;
350 FOREACH_CMT_REC(mm
, cmt_info
) {
351 mgmt_cmt_infos_del(&mm
->cmts
, cmt_info
);
352 XFREE(MTYPE_MGMTD_CMT_INFO
, cmt_info
);
355 mgmt_cmt_infos_fini(&mm
->cmts
);