summaryrefslogtreecommitdiffstats
path: root/mgmtd/mgmt_history.c
blob: 6f9f2dd63f4e1f6f3fb2702823fb4338eeeb9f0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2021  Vmware, Inc.
 *		       Pushpasis Sarkar <spushpasis@vmware.com>
 * Copyright (c) 2023, LabN Consulting, L.L.C.
 */

#include <zebra.h>
#include "md5.h"
#include "frrevent.h"
#include "xref.h"

#include "mgmt_fe_client.h"
#include "mgmtd/mgmt.h"
#include "mgmtd/mgmt_ds.h"
#include "mgmtd/mgmt_history.h"

struct mgmt_cmt_info_t {
	struct mgmt_cmt_infos_item cmts;

	char cmtid_str[MGMTD_MD5_HASH_STR_HEX_LEN];
	char time_str[MGMTD_COMMIT_TIME_STR_LEN];
	char cmt_json_file[PATH_MAX];
};


DECLARE_DLIST(mgmt_cmt_infos, struct mgmt_cmt_info_t, cmts);

#define FOREACH_CMT_REC(mm, cmt_info)                                          \
	frr_each_safe (mgmt_cmt_infos, &mm->cmts, cmt_info)

/*
 * The only instance of VTY session that has triggered an ongoing
 * config rollback operation.
 */
static struct vty *rollback_vty = NULL;

static bool mgmt_history_record_exists(char *file_path)
{
	int exist;

	exist = access(file_path, F_OK);
	if (exist == 0)
		return true;
	else
		return false;
}

static void mgmt_history_remove_file(char *name)
{
	if (remove(name) == 0)
		zlog_debug("Old commit info deletion succeeded");
	else
		zlog_err("Old commit info deletion failed");
}

static void mgmt_history_hash(const char *input_str, char *hash)
{
	int i;
	unsigned char digest[MGMTD_MD5_HASH_LEN];
	MD5_CTX ctx;

	memset(&ctx, 0, sizeof(ctx));
	MD5Init(&ctx);
	MD5Update(&ctx, input_str, strlen(input_str));
	MD5Final(digest, &ctx);

	for (i = 0; i < MGMTD_MD5_HASH_LEN; i++)
		snprintf(&hash[i * 2], MGMTD_MD5_HASH_STR_HEX_LEN, "%02x",
			 (unsigned int)digest[i]);
}

static struct mgmt_cmt_info_t *mgmt_history_create_cmt_rec(void)
{
	struct mgmt_cmt_info_t *new;
	struct mgmt_cmt_info_t *cmt_info;
	struct mgmt_cmt_info_t *last_cmt_info = NULL;
	struct timeval cmt_recd_tv;

	new = XCALLOC(MTYPE_MGMTD_CMT_INFO, sizeof(struct mgmt_cmt_info_t));
	gettimeofday(&cmt_recd_tv, NULL);
	mgmt_realtime_to_string(&cmt_recd_tv, new->time_str,
				sizeof(new->time_str));
	mgmt_history_hash(new->time_str, new->cmtid_str);
	snprintf(new->cmt_json_file, sizeof(new->cmt_json_file),
		 MGMTD_COMMIT_FILE_PATH, new->cmtid_str);

	if (mgmt_cmt_infos_count(&mm->cmts) == MGMTD_MAX_COMMIT_LIST) {
		FOREACH_CMT_REC (mm, cmt_info)
			last_cmt_info = cmt_info;

		if (last_cmt_info) {
			mgmt_history_remove_file(last_cmt_info->cmt_json_file);
			mgmt_cmt_infos_del(&mm->cmts, last_cmt_info);
			XFREE(MTYPE_MGMTD_CMT_INFO, last_cmt_info);
		}
	}

	mgmt_cmt_infos_add_head(&mm->cmts, new);
	return new;
}

static struct mgmt_cmt_info_t *mgmt_history_find_cmt_record(const char *cmtid_str)
{
	struct mgmt_cmt_info_t *cmt_info;

	FOREACH_CMT_REC (mm, cmt_info) {
		if (strncmp(cmt_info->cmtid_str, cmtid_str,
			    MGMTD_MD5_HASH_STR_HEX_LEN) == 0)
			return cmt_info;
	}

	return NULL;
}

static bool mgmt_history_read_cmt_record_index(void)
{
	FILE *fp;
	struct mgmt_cmt_info_t cmt_info;
	struct mgmt_cmt_info_t *new;
	int cnt = 0;

	fp = fopen(MGMTD_COMMIT_INDEX_FILE_NAME, "rb");
	if (!fp) {
		zlog_err("Failed to open file %s rb mode",
			 MGMTD_COMMIT_INDEX_FILE_NAME);
		return false;
	}

	while ((fread(&cmt_info, sizeof(cmt_info), 1, fp)) > 0) {
		if (cnt < MGMTD_MAX_COMMIT_LIST) {
			if (!mgmt_history_record_exists(cmt_info.cmt_json_file)) {
				zlog_err(
					"Commit record present in index_file, but commit file %s missing",
					cmt_info.cmt_json_file);
				continue;
			}

			new = XCALLOC(MTYPE_MGMTD_CMT_INFO,
				      sizeof(struct mgmt_cmt_info_t));
			memcpy(new, &cmt_info, sizeof(struct mgmt_cmt_info_t));
			mgmt_cmt_infos_add_tail(&mm->cmts, new);
		} else {
			zlog_err("More records found in index file %s",
				 MGMTD_COMMIT_INDEX_FILE_NAME);
			fclose(fp);
			return false;
		}

		cnt++;
	}

	fclose(fp);
	return true;
}

static bool mgmt_history_dump_cmt_record_index(void)
{
	FILE *fp;
	int ret = 0;
	struct mgmt_cmt_info_t *cmt_info;
	struct mgmt_cmt_info_t cmt_info_set[10];
	int cnt = 0;

	mgmt_history_remove_file((char *)MGMTD_COMMIT_INDEX_FILE_NAME);
	fp = fopen(MGMTD_COMMIT_INDEX_FILE_NAME, "ab");
	if (!fp) {
		zlog_err("Failed to open file %s ab mode",
			 MGMTD_COMMIT_INDEX_FILE_NAME);
		return false;
	}

	FOREACH_CMT_REC (mm, cmt_info) {
		memcpy(&cmt_info_set[cnt], cmt_info,
		       sizeof(struct mgmt_cmt_info_t));
		cnt++;
	}

	if (!cnt) {
		fclose(fp);
		return false;
	}

	ret = fwrite(&cmt_info_set, sizeof(struct mgmt_cmt_info_t), cnt, fp);
	fclose(fp);
	if (ret != cnt) {
		zlog_err("Write record failed");
		return false;
	} else {
		return true;
	}
}

static int mgmt_history_rollback_to_cmt(struct vty *vty,
				   struct mgmt_cmt_info_t *cmt_info,
				   bool skip_file_load)
{
	struct mgmt_ds_ctx *src_ds_ctx;
	struct mgmt_ds_ctx *dst_ds_ctx;
	int ret = 0;

	if (rollback_vty) {
		vty_out(vty, "ERROR: Rollback already in progress!\n");
		return -1;
	}

	src_ds_ctx = mgmt_ds_get_ctx_by_id(mm, MGMTD_DS_CANDIDATE);
	if (!src_ds_ctx) {
		vty_out(vty, "ERROR: Couldnot access Candidate datastore!\n");
		return -1;
	}

	/*
	 * Note: Write lock on src_ds is not required. This is already
	 * taken in 'conf te'.
	 */
	dst_ds_ctx = mgmt_ds_get_ctx_by_id(mm, MGMTD_DS_RUNNING);
	if (!dst_ds_ctx) {
		vty_out(vty, "ERROR: Couldnot access Running datastore!\n");
		return -1;
	}

	ret = mgmt_ds_write_lock(dst_ds_ctx);
	if (ret != 0) {
		vty_out(vty,
			"Failed to lock the DS %u for rollback Reason: %s!\n",
			MGMTD_DS_RUNNING, strerror(ret));
		return -1;
	}

	if (!skip_file_load) {
		ret = mgmt_ds_load_config_from_file(
			src_ds_ctx, cmt_info->cmt_json_file, false);
		if (ret != 0) {
			mgmt_ds_unlock(dst_ds_ctx);
			vty_out(vty,
				"Error with parsing the file with error code %d\n",
				ret);
			return ret;
		}
	}

	/* Internally trigger a commit-request. */
	ret = mgmt_txn_rollback_trigger_cfg_apply(src_ds_ctx, dst_ds_ctx);
	if (ret != 0) {
		mgmt_ds_unlock(dst_ds_ctx);
		vty_out(vty,
			"Error with creating commit apply txn with error code %d\n",
			ret);
		return ret;
	}

	mgmt_history_dump_cmt_record_index();

	/*
	 * Block the rollback command from returning till the rollback
	 * is completed. On rollback completion mgmt_history_rollback_complete()
	 * shall be called to resume the rollback command return to VTYSH.
	 */
	vty->mgmt_req_pending = true;
	rollback_vty = vty;
	return 0;
}

void mgmt_history_rollback_complete(bool success)
{
	vty_mgmt_resume_response(rollback_vty, success);
	rollback_vty = NULL;
}

int mgmt_history_rollback_by_id(struct vty *vty, const char *cmtid_str)
{
	int ret = 0;
	struct mgmt_cmt_info_t *cmt_info;

	if (!mgmt_cmt_infos_count(&mm->cmts) ||
	    !mgmt_history_find_cmt_record(cmtid_str)) {
		vty_out(vty, "Invalid commit Id\n");
		return -1;
	}

	FOREACH_CMT_REC (mm, cmt_info) {
		if (strncmp(cmt_info->cmtid_str, cmtid_str,
			    MGMTD_MD5_HASH_STR_HEX_LEN) == 0) {
			ret = mgmt_history_rollback_to_cmt(vty, cmt_info, false);
			return ret;
		}

		mgmt_history_remove_file(cmt_info->cmt_json_file);
		mgmt_cmt_infos_del(&mm->cmts, cmt_info);
		XFREE(MTYPE_MGMTD_CMT_INFO, cmt_info);
	}

	return 0;
}

int mgmt_history_rollback_n(struct vty *vty, int num_cmts)
{
	int ret = 0;
	int cnt = 0;
	struct mgmt_cmt_info_t *cmt_info;
	size_t cmts;

	if (!num_cmts)
		num_cmts = 1;

	cmts = mgmt_cmt_infos_count(&mm->cmts);
	if ((int)cmts < num_cmts) {
		vty_out(vty,
			"Number of commits found (%d) less than required to rollback\n",
			(int)cmts);
		return -1;
	}

	if ((int)cmts == 1 || (int)cmts == num_cmts) {
		vty_out(vty,
			"Number of commits found (%d), Rollback of last commit is not supported\n",
			(int)cmts);
		return -1;
	}

	FOREACH_CMT_REC (mm, cmt_info) {
		if (cnt == num_cmts) {
			ret = mgmt_history_rollback_to_cmt(vty, cmt_info, false);
			return ret;
		}

		cnt++;
		mgmt_history_remove_file(cmt_info->cmt_json_file);
		mgmt_cmt_infos_del(&mm->cmts, cmt_info);
		XFREE(MTYPE_MGMTD_CMT_INFO, cmt_info);
	}

	if (!mgmt_cmt_infos_count(&mm->cmts)) {
		mgmt_ds_reset_candidate();
		ret = mgmt_history_rollback_to_cmt(vty, cmt_info, true);
	}

	return ret;
}

void show_mgmt_cmt_history(struct vty *vty)
{
	struct mgmt_cmt_info_t *cmt_info;
	int slno = 0;

	vty_out(vty, "Last 10 commit history:\n");
	vty_out(vty, "  Sl.No\tCommit-ID(HEX)\t\t\t  Commit-Record-Time\n");
	FOREACH_CMT_REC (mm, cmt_info) {
		vty_out(vty, "  %d\t%s  %s\n", slno, cmt_info->cmtid_str,
			cmt_info->time_str);
		slno++;
	}
}

void mgmt_history_new_record(struct mgmt_ds_ctx *ds_ctx)
{
	struct mgmt_cmt_info_t *cmt_info = mgmt_history_create_cmt_rec();
	mgmt_ds_dump_ds_to_file(cmt_info->cmt_json_file, ds_ctx);
	mgmt_history_dump_cmt_record_index();
}

void mgmt_history_init(void)
{
	/* Create commit record for previously stored commit-apply */
	mgmt_cmt_infos_init(&mm->cmts);
	mgmt_history_read_cmt_record_index();
}

void mgmt_history_destroy(void)
{
	struct mgmt_cmt_info_t *cmt_info;

	FOREACH_CMT_REC(mm, cmt_info) {
		mgmt_cmt_infos_del(&mm->cmts, cmt_info);
		XFREE(MTYPE_MGMTD_CMT_INFO, cmt_info);
	}

	mgmt_cmt_infos_fini(&mm->cmts);
}