]> git.proxmox.com Git - mirror_frr.git/blob - mgmtd/mgmt_txn.c
a666422b7dbb79c0d959b08a1ab3111f17f44f0b
[mirror_frr.git] / mgmtd / mgmt_txn.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * MGMTD Transactions
4 *
5 * Copyright (C) 2021 Vmware, Inc.
6 * Pushpasis Sarkar <spushpasis@vmware.com>
7 */
8
9 #include <zebra.h>
10 #include "hash.h"
11 #include "jhash.h"
12 #include "libfrr.h"
13 #include "mgmtd/mgmt.h"
14 #include "mgmtd/mgmt_memory.h"
15 #include "mgmtd/mgmt_txn.h"
16
17 #define MGMTD_TXN_DBG(fmt, ...) \
18 DEBUGD(&mgmt_debug_txn, "%s:" fmt, __func__, ##__VA_ARGS__)
19 #define MGMTD_TXN_ERR(fmt, ...) \
20 zlog_err("%s: ERROR: " fmt, __func__, ##__VA_ARGS__)
21
22 #define MGMTD_TXN_LOCK(txn) mgmt_txn_lock(txn, __FILE__, __LINE__)
23 #define MGMTD_TXN_UNLOCK(txn) mgmt_txn_unlock(txn, __FILE__, __LINE__)
24
25 enum mgmt_txn_event {
26 MGMTD_TXN_PROC_SETCFG = 1,
27 MGMTD_TXN_PROC_COMMITCFG,
28 MGMTD_TXN_PROC_GETCFG,
29 MGMTD_TXN_PROC_GETDATA,
30 MGMTD_TXN_COMMITCFG_TIMEOUT,
31 MGMTD_TXN_CLEANUP
32 };
33
34 PREDECL_LIST(mgmt_txn_reqs);
35
36 struct mgmt_set_cfg_req {
37 Mgmtd__DatastoreId ds_id;
38 struct mgmt_ds_ctx *ds_ctx;
39 struct nb_cfg_change cfg_changes[MGMTD_MAX_CFG_CHANGES_IN_BATCH];
40 uint16_t num_cfg_changes;
41 bool implicit_commit;
42 Mgmtd__DatastoreId dst_ds_id;
43 struct mgmt_ds_ctx *dst_ds_ctx;
44 struct mgmt_setcfg_stats *setcfg_stats;
45 };
46
47 enum mgmt_commit_phase {
48 MGMTD_COMMIT_PHASE_PREPARE_CFG = 0,
49 MGMTD_COMMIT_PHASE_TXN_CREATE,
50 MGMTD_COMMIT_PHASE_SEND_CFG,
51 MGMTD_COMMIT_PHASE_APPLY_CFG,
52 MGMTD_COMMIT_PHASE_TXN_DELETE,
53 MGMTD_COMMIT_PHASE_MAX
54 };
55
56 static inline const char *
57 mgmt_commit_phase2str(enum mgmt_commit_phase cmt_phase)
58 {
59 switch (cmt_phase) {
60 case MGMTD_COMMIT_PHASE_PREPARE_CFG:
61 return "PREP-CFG";
62 case MGMTD_COMMIT_PHASE_TXN_CREATE:
63 return "CREATE-TXN";
64 case MGMTD_COMMIT_PHASE_SEND_CFG:
65 return "SEND-CFG";
66 case MGMTD_COMMIT_PHASE_APPLY_CFG:
67 return "APPLY-CFG";
68 case MGMTD_COMMIT_PHASE_TXN_DELETE:
69 return "DELETE-TXN";
70 case MGMTD_COMMIT_PHASE_MAX:
71 return "Invalid/Unknown";
72 }
73
74 return "Invalid/Unknown";
75 }
76
77 PREDECL_LIST(mgmt_txn_batches);
78
79 struct mgmt_txn_be_cfg_batch {
80 struct mgmt_txn_ctx *txn;
81 uint64_t batch_id;
82 enum mgmt_be_client_id be_id;
83 struct mgmt_be_client_adapter *be_adapter;
84 uint xp_subscr[MGMTD_MAX_CFG_CHANGES_IN_BATCH];
85 Mgmtd__YangCfgDataReq cfg_data[MGMTD_MAX_CFG_CHANGES_IN_BATCH];
86 Mgmtd__YangCfgDataReq * cfg_datap[MGMTD_MAX_CFG_CHANGES_IN_BATCH];
87 Mgmtd__YangData data[MGMTD_MAX_CFG_CHANGES_IN_BATCH];
88 Mgmtd__YangDataValue value[MGMTD_MAX_CFG_CHANGES_IN_BATCH];
89 size_t num_cfg_data;
90 int buf_space_left;
91 enum mgmt_commit_phase comm_phase;
92 struct mgmt_txn_batches_item list_linkage;
93 };
94
95 DECLARE_LIST(mgmt_txn_batches, struct mgmt_txn_be_cfg_batch, list_linkage);
96
97 #define FOREACH_TXN_CFG_BATCH_IN_LIST(list, batch) \
98 frr_each_safe (mgmt_txn_batches, list, batch)
99
100 struct mgmt_commit_cfg_req {
101 Mgmtd__DatastoreId src_ds_id;
102 struct mgmt_ds_ctx *src_ds_ctx;
103 Mgmtd__DatastoreId dst_ds_id;
104 struct mgmt_ds_ctx *dst_ds_ctx;
105 uint32_t nb_txn_id;
106 uint8_t validate_only : 1;
107 uint8_t abort : 1;
108 uint8_t implicit : 1;
109 uint8_t rollback : 1;
110
111 /* Track commit phases */
112 enum mgmt_commit_phase curr_phase;
113 enum mgmt_commit_phase next_phase;
114
115 /*
116 * Set of config changes to commit. This is used only
117 * when changes are NOT to be determined by comparing
118 * candidate and running DSs. This is typically used
119 * for downloading all relevant configs for a new backend
120 * client that has recently come up and connected with
121 * MGMTD.
122 */
123 struct nb_config_cbs *cfg_chgs;
124
125 /*
126 * Details on all the Backend Clients associated with
127 * this commit.
128 */
129 struct mgmt_be_client_subscr_info subscr_info;
130
131 /*
132 * List of backend batches for this commit to be validated
133 * and applied at the backend.
134 *
135 * FIXME: Need to re-think this design for the case set of
136 * validators for a given YANG data item is different from
137 * the set of notifiers for the same. We may need to have
138 * separate list of batches for VALIDATE and APPLY.
139 */
140 struct mgmt_txn_batches_head curr_batches[MGMTD_BE_CLIENT_ID_MAX];
141 struct mgmt_txn_batches_head next_batches[MGMTD_BE_CLIENT_ID_MAX];
142 /*
143 * The last batch added for any backend client. This is always on
144 * 'curr_batches'
145 */
146 struct mgmt_txn_be_cfg_batch
147 *last_be_cfg_batch[MGMTD_BE_CLIENT_ID_MAX];
148 struct hash *batches;
149 uint64_t next_batch_id;
150
151 struct mgmt_commit_stats *cmt_stats;
152 };
153
154 struct mgmt_get_data_reply {
155 /* Buffer space for preparing data reply */
156 int num_reply;
157 int last_batch;
158 Mgmtd__YangDataReply data_reply;
159 Mgmtd__YangData reply_data[MGMTD_MAX_NUM_DATA_REPLY_IN_BATCH];
160 Mgmtd__YangData * reply_datap[MGMTD_MAX_NUM_DATA_REPLY_IN_BATCH];
161 Mgmtd__YangDataValue reply_value[MGMTD_MAX_NUM_DATA_REPLY_IN_BATCH];
162 char *reply_xpathp[MGMTD_MAX_NUM_DATA_REPLY_IN_BATCH];
163 };
164
165 struct mgmt_get_data_req {
166 Mgmtd__DatastoreId ds_id;
167 struct mgmt_ds_ctx *ds_ctx;
168 char *xpaths[MGMTD_MAX_NUM_DATA_REQ_IN_BATCH];
169 int num_xpaths;
170
171 /*
172 * Buffer space for preparing reply.
173 * NOTE: Should only be malloc-ed on demand to reduce
174 * memory footprint. Freed up via mgmt_trx_req_free()
175 */
176 struct mgmt_get_data_reply *reply;
177
178 int total_reply;
179 };
180
181 struct mgmt_txn_req {
182 struct mgmt_txn_ctx *txn;
183 enum mgmt_txn_event req_event;
184 uint64_t req_id;
185 union {
186 struct mgmt_set_cfg_req *set_cfg;
187 struct mgmt_get_data_req *get_data;
188 struct mgmt_commit_cfg_req commit_cfg;
189 } req;
190
191 bool pending_be_proc;
192 struct mgmt_txn_reqs_item list_linkage;
193 };
194
195 DECLARE_LIST(mgmt_txn_reqs, struct mgmt_txn_req, list_linkage);
196
197 #define FOREACH_TXN_REQ_IN_LIST(list, req) \
198 frr_each_safe (mgmt_txn_reqs, list, req)
199
200 struct mgmt_txn_ctx {
201 uint64_t session_id; /* One transaction per client session */
202 uint64_t txn_id;
203 enum mgmt_txn_type type;
204
205 /* struct mgmt_master *mm; */
206
207 struct event *proc_set_cfg;
208 struct event *proc_comm_cfg;
209 struct event *proc_get_cfg;
210 struct event *proc_get_data;
211 struct event *comm_cfg_timeout;
212 struct event *clnup;
213
214 /* List of backend adapters involved in this transaction */
215 struct mgmt_txn_badapters_head be_adapters;
216
217 int refcount;
218
219 struct mgmt_txns_item list_linkage;
220
221 /*
222 * List of pending set-config requests for a given
223 * transaction/session. Just one list for requests
224 * not processed at all. There's no backend interaction
225 * involved.
226 */
227 struct mgmt_txn_reqs_head set_cfg_reqs;
228 /*
229 * List of pending get-config requests for a given
230 * transaction/session. Just one list for requests
231 * not processed at all. There's no backend interaction
232 * involved.
233 */
234 struct mgmt_txn_reqs_head get_cfg_reqs;
235 /*
236 * List of pending get-data requests for a given
237 * transaction/session Two lists, one for requests
238 * not processed at all, and one for requests that
239 * has been sent to backend for processing.
240 */
241 struct mgmt_txn_reqs_head get_data_reqs;
242 struct mgmt_txn_reqs_head pending_get_datas;
243 /*
244 * There will always be one commit-config allowed for a given
245 * transaction/session. No need to maintain lists for it.
246 */
247 struct mgmt_txn_req *commit_cfg_req;
248 };
249
250 DECLARE_LIST(mgmt_txns, struct mgmt_txn_ctx, list_linkage);
251
252 #define FOREACH_TXN_IN_LIST(mm, txn) \
253 frr_each_safe (mgmt_txns, &(mm)->txn_list, (txn))
254
255 static int mgmt_txn_send_commit_cfg_reply(struct mgmt_txn_ctx *txn,
256 enum mgmt_result result,
257 const char *error_if_any);
258
259 static inline const char *
260 mgmt_txn_commit_phase_str(struct mgmt_txn_ctx *txn, bool curr)
261 {
262 if (!txn->commit_cfg_req)
263 return "None";
264
265 return (mgmt_commit_phase2str(
266 curr ? txn->commit_cfg_req->req.commit_cfg.curr_phase
267 : txn->commit_cfg_req->req.commit_cfg.next_phase));
268 }
269
270 static void mgmt_txn_lock(struct mgmt_txn_ctx *txn, const char *file,
271 int line);
272 static void mgmt_txn_unlock(struct mgmt_txn_ctx **txn, const char *file,
273 int line);
274 static int
275 mgmt_txn_send_be_txn_delete(struct mgmt_txn_ctx *txn,
276 struct mgmt_be_client_adapter *adapter);
277
278 static struct event_loop *mgmt_txn_tm;
279 static struct mgmt_master *mgmt_txn_mm;
280
281 static void mgmt_txn_register_event(struct mgmt_txn_ctx *txn,
282 enum mgmt_txn_event event);
283
284 static int
285 mgmt_move_be_commit_to_next_phase(struct mgmt_txn_ctx *txn,
286 struct mgmt_be_client_adapter *adapter);
287
288 static struct mgmt_txn_be_cfg_batch *
289 mgmt_txn_cfg_batch_alloc(struct mgmt_txn_ctx *txn,
290 enum mgmt_be_client_id id,
291 struct mgmt_be_client_adapter *be_adapter)
292 {
293 struct mgmt_txn_be_cfg_batch *cfg_btch;
294
295 cfg_btch = XCALLOC(MTYPE_MGMTD_TXN_CFG_BATCH,
296 sizeof(struct mgmt_txn_be_cfg_batch));
297 assert(cfg_btch);
298 cfg_btch->be_id = id;
299
300 cfg_btch->txn = txn;
301 MGMTD_TXN_LOCK(txn);
302 assert(txn->commit_cfg_req);
303 mgmt_txn_batches_add_tail(
304 &txn->commit_cfg_req->req.commit_cfg.curr_batches[id],
305 cfg_btch);
306 cfg_btch->be_adapter = be_adapter;
307 cfg_btch->buf_space_left = MGMTD_BE_CFGDATA_MAX_MSG_LEN;
308 if (be_adapter)
309 mgmt_be_adapter_lock(be_adapter);
310
311 txn->commit_cfg_req->req.commit_cfg.last_be_cfg_batch[id] =
312 cfg_btch;
313 if (!txn->commit_cfg_req->req.commit_cfg.next_batch_id)
314 txn->commit_cfg_req->req.commit_cfg.next_batch_id++;
315 cfg_btch->batch_id =
316 txn->commit_cfg_req->req.commit_cfg.next_batch_id++;
317 hash_get(txn->commit_cfg_req->req.commit_cfg.batches, cfg_btch,
318 hash_alloc_intern);
319
320 return cfg_btch;
321 }
322
323 static void
324 mgmt_txn_cfg_batch_free(struct mgmt_txn_be_cfg_batch **cfg_btch)
325 {
326 size_t indx;
327 struct mgmt_commit_cfg_req *cmtcfg_req;
328
329 MGMTD_TXN_DBG(" freeing batch-id: %" PRIu64 " txn-id %" PRIu64,
330 (*cfg_btch)->batch_id, (*cfg_btch)->txn->txn_id);
331
332 assert((*cfg_btch)->txn
333 && (*cfg_btch)->txn->type == MGMTD_TXN_TYPE_CONFIG);
334
335 cmtcfg_req = &(*cfg_btch)->txn->commit_cfg_req->req.commit_cfg;
336 hash_release(cmtcfg_req->batches, *cfg_btch);
337 mgmt_txn_batches_del(&cmtcfg_req->curr_batches[(*cfg_btch)->be_id],
338 *cfg_btch);
339 mgmt_txn_batches_del(&cmtcfg_req->next_batches[(*cfg_btch)->be_id],
340 *cfg_btch);
341
342 if ((*cfg_btch)->be_adapter)
343 mgmt_be_adapter_unlock(&(*cfg_btch)->be_adapter);
344
345 for (indx = 0; indx < (*cfg_btch)->num_cfg_data; indx++) {
346 if ((*cfg_btch)->data[indx].xpath) {
347 free((*cfg_btch)->data[indx].xpath);
348 (*cfg_btch)->data[indx].xpath = NULL;
349 }
350 }
351
352 MGMTD_TXN_UNLOCK(&(*cfg_btch)->txn);
353
354 XFREE(MTYPE_MGMTD_TXN_CFG_BATCH, *cfg_btch);
355 *cfg_btch = NULL;
356 }
357
358 static unsigned int mgmt_txn_cfgbatch_hash_key(const void *data)
359 {
360 const struct mgmt_txn_be_cfg_batch *batch = data;
361
362 return jhash2((uint32_t *) &batch->batch_id,
363 sizeof(batch->batch_id) / sizeof(uint32_t), 0);
364 }
365
366 static bool mgmt_txn_cfgbatch_hash_cmp(const void *d1, const void *d2)
367 {
368 const struct mgmt_txn_be_cfg_batch *batch1 = d1;
369 const struct mgmt_txn_be_cfg_batch *batch2 = d2;
370
371 return (batch1->batch_id == batch2->batch_id);
372 }
373
374 static void mgmt_txn_cfgbatch_hash_free(void *data)
375 {
376 struct mgmt_txn_be_cfg_batch *batch = data;
377
378 mgmt_txn_cfg_batch_free(&batch);
379 }
380
381 static inline struct mgmt_txn_be_cfg_batch *
382 mgmt_txn_cfgbatch_id2ctx(struct mgmt_txn_ctx *txn, uint64_t batch_id)
383 {
384 struct mgmt_txn_be_cfg_batch key = {0};
385 struct mgmt_txn_be_cfg_batch *batch;
386
387 if (!txn->commit_cfg_req)
388 return NULL;
389
390 key.batch_id = batch_id;
391 batch = hash_lookup(txn->commit_cfg_req->req.commit_cfg.batches,
392 &key);
393
394 return batch;
395 }
396
397 static void mgmt_txn_cleanup_be_cfg_batches(struct mgmt_txn_ctx *txn,
398 enum mgmt_be_client_id id)
399 {
400 struct mgmt_txn_be_cfg_batch *cfg_btch;
401 struct mgmt_txn_batches_head *list;
402
403 list = &txn->commit_cfg_req->req.commit_cfg.curr_batches[id];
404 FOREACH_TXN_CFG_BATCH_IN_LIST (list, cfg_btch)
405 mgmt_txn_cfg_batch_free(&cfg_btch);
406
407 mgmt_txn_batches_fini(list);
408
409 list = &txn->commit_cfg_req->req.commit_cfg.next_batches[id];
410 FOREACH_TXN_CFG_BATCH_IN_LIST (list, cfg_btch)
411 mgmt_txn_cfg_batch_free(&cfg_btch);
412
413 mgmt_txn_batches_fini(list);
414
415 txn->commit_cfg_req->req.commit_cfg.last_be_cfg_batch[id] = NULL;
416 }
417
418 static struct mgmt_txn_req *mgmt_txn_req_alloc(struct mgmt_txn_ctx *txn,
419 uint64_t req_id,
420 enum mgmt_txn_event req_event)
421 {
422 struct mgmt_txn_req *txn_req;
423 enum mgmt_be_client_id id;
424
425 txn_req = XCALLOC(MTYPE_MGMTD_TXN_REQ, sizeof(struct mgmt_txn_req));
426 assert(txn_req);
427 txn_req->txn = txn;
428 txn_req->req_id = req_id;
429 txn_req->req_event = req_event;
430 txn_req->pending_be_proc = false;
431
432 switch (txn_req->req_event) {
433 case MGMTD_TXN_PROC_SETCFG:
434 txn_req->req.set_cfg = XCALLOC(MTYPE_MGMTD_TXN_SETCFG_REQ,
435 sizeof(struct mgmt_set_cfg_req));
436 assert(txn_req->req.set_cfg);
437 mgmt_txn_reqs_add_tail(&txn->set_cfg_reqs, txn_req);
438 MGMTD_TXN_DBG("Added a new SETCFG req-id: %" PRIu64
439 " txn-id: %" PRIu64 ", session-id: %" PRIu64,
440 txn_req->req_id, txn->txn_id, txn->session_id);
441 break;
442 case MGMTD_TXN_PROC_COMMITCFG:
443 txn->commit_cfg_req = txn_req;
444 MGMTD_TXN_DBG("Added a new COMMITCFG req-id: %" PRIu64
445 " txn-id: %" PRIu64 " session-id: %" PRIu64,
446 txn_req->req_id, txn->txn_id, txn->session_id);
447
448 FOREACH_MGMTD_BE_CLIENT_ID (id) {
449 mgmt_txn_batches_init(
450 &txn_req->req.commit_cfg.curr_batches[id]);
451 mgmt_txn_batches_init(
452 &txn_req->req.commit_cfg.next_batches[id]);
453 }
454
455 txn_req->req.commit_cfg.batches =
456 hash_create(mgmt_txn_cfgbatch_hash_key,
457 mgmt_txn_cfgbatch_hash_cmp,
458 "MGMT Config Batches");
459 break;
460 case MGMTD_TXN_PROC_GETCFG:
461 txn_req->req.get_data =
462 XCALLOC(MTYPE_MGMTD_TXN_GETDATA_REQ,
463 sizeof(struct mgmt_get_data_req));
464 assert(txn_req->req.get_data);
465 mgmt_txn_reqs_add_tail(&txn->get_cfg_reqs, txn_req);
466 MGMTD_TXN_DBG("Added a new GETCFG req-id: %" PRIu64
467 " txn-id: %" PRIu64 " session-id: %" PRIu64,
468 txn_req->req_id, txn->txn_id, txn->session_id);
469 break;
470 case MGMTD_TXN_PROC_GETDATA:
471 txn_req->req.get_data =
472 XCALLOC(MTYPE_MGMTD_TXN_GETDATA_REQ,
473 sizeof(struct mgmt_get_data_req));
474 assert(txn_req->req.get_data);
475 mgmt_txn_reqs_add_tail(&txn->get_data_reqs, txn_req);
476 MGMTD_TXN_DBG("Added a new GETDATA req-id: %" PRIu64
477 " txn-id: %" PRIu64 " session-id: %" PRIu64,
478 txn_req->req_id, txn->txn_id, txn->session_id);
479 break;
480 case MGMTD_TXN_COMMITCFG_TIMEOUT:
481 case MGMTD_TXN_CLEANUP:
482 break;
483 }
484
485 MGMTD_TXN_LOCK(txn);
486
487 return txn_req;
488 }
489
490 static void mgmt_txn_req_free(struct mgmt_txn_req **txn_req)
491 {
492 int indx;
493 struct mgmt_txn_reqs_head *req_list = NULL;
494 struct mgmt_txn_reqs_head *pending_list = NULL;
495 enum mgmt_be_client_id id;
496 struct mgmt_be_client_adapter *adapter;
497
498 switch ((*txn_req)->req_event) {
499 case MGMTD_TXN_PROC_SETCFG:
500 for (indx = 0; indx < (*txn_req)->req.set_cfg->num_cfg_changes;
501 indx++) {
502 if ((*txn_req)->req.set_cfg->cfg_changes[indx].value) {
503 MGMTD_TXN_DBG(
504 "Freeing value for %s at %p ==> '%s'",
505 (*txn_req)
506 ->req.set_cfg->cfg_changes[indx]
507 .xpath,
508 (*txn_req)
509 ->req.set_cfg->cfg_changes[indx]
510 .value,
511 (*txn_req)
512 ->req.set_cfg->cfg_changes[indx]
513 .value);
514 free((void *)(*txn_req)
515 ->req.set_cfg->cfg_changes[indx]
516 .value);
517 }
518 }
519 req_list = &(*txn_req)->txn->set_cfg_reqs;
520 MGMTD_TXN_DBG("Deleting SETCFG req-id: %" PRIu64
521 " txn-id: %" PRIu64,
522 (*txn_req)->req_id, (*txn_req)->txn->txn_id);
523 XFREE(MTYPE_MGMTD_TXN_SETCFG_REQ, (*txn_req)->req.set_cfg);
524 break;
525 case MGMTD_TXN_PROC_COMMITCFG:
526 MGMTD_TXN_DBG("Deleting COMMITCFG req-id: %" PRIu64
527 " txn-id: %" PRIu64,
528 (*txn_req)->req_id, (*txn_req)->txn->txn_id);
529 FOREACH_MGMTD_BE_CLIENT_ID (id) {
530 /*
531 * Send TXN_DELETE to cleanup state for this
532 * transaction on backend
533 */
534 if ((*txn_req)->req.commit_cfg.curr_phase >=
535 MGMTD_COMMIT_PHASE_TXN_CREATE &&
536 (*txn_req)->req.commit_cfg.curr_phase <
537 MGMTD_COMMIT_PHASE_TXN_DELETE &&
538 (*txn_req)
539 ->req.commit_cfg.subscr_info
540 .xpath_subscr[id]) {
541 adapter = mgmt_be_get_adapter_by_id(id);
542 if (adapter)
543 mgmt_txn_send_be_txn_delete(
544 (*txn_req)->txn, adapter);
545 }
546
547 mgmt_txn_cleanup_be_cfg_batches((*txn_req)->txn,
548 id);
549 if ((*txn_req)->req.commit_cfg.batches) {
550 hash_clean((*txn_req)->req.commit_cfg.batches,
551 mgmt_txn_cfgbatch_hash_free);
552 hash_free((*txn_req)->req.commit_cfg.batches);
553 (*txn_req)->req.commit_cfg.batches = NULL;
554 }
555 }
556 break;
557 case MGMTD_TXN_PROC_GETCFG:
558 for (indx = 0; indx < (*txn_req)->req.get_data->num_xpaths;
559 indx++) {
560 if ((*txn_req)->req.get_data->xpaths[indx])
561 free((void *)(*txn_req)
562 ->req.get_data->xpaths[indx]);
563 }
564 req_list = &(*txn_req)->txn->get_cfg_reqs;
565 MGMTD_TXN_DBG("Deleting GETCFG req-id: %" PRIu64
566 " txn-id: %" PRIu64,
567 (*txn_req)->req_id, (*txn_req)->txn->txn_id);
568 if ((*txn_req)->req.get_data->reply)
569 XFREE(MTYPE_MGMTD_TXN_GETDATA_REPLY,
570 (*txn_req)->req.get_data->reply);
571 XFREE(MTYPE_MGMTD_TXN_GETDATA_REQ, (*txn_req)->req.get_data);
572 break;
573 case MGMTD_TXN_PROC_GETDATA:
574 for (indx = 0; indx < (*txn_req)->req.get_data->num_xpaths;
575 indx++) {
576 if ((*txn_req)->req.get_data->xpaths[indx])
577 free((void *)(*txn_req)
578 ->req.get_data->xpaths[indx]);
579 }
580 pending_list = &(*txn_req)->txn->pending_get_datas;
581 req_list = &(*txn_req)->txn->get_data_reqs;
582 MGMTD_TXN_DBG("Deleting GETDATA req-id: %" PRIu64
583 " txn-id: %" PRIu64,
584 (*txn_req)->req_id, (*txn_req)->txn->txn_id);
585 if ((*txn_req)->req.get_data->reply)
586 XFREE(MTYPE_MGMTD_TXN_GETDATA_REPLY,
587 (*txn_req)->req.get_data->reply);
588 XFREE(MTYPE_MGMTD_TXN_GETDATA_REQ, (*txn_req)->req.get_data);
589 break;
590 case MGMTD_TXN_COMMITCFG_TIMEOUT:
591 case MGMTD_TXN_CLEANUP:
592 break;
593 }
594
595 if ((*txn_req)->pending_be_proc && pending_list) {
596 mgmt_txn_reqs_del(pending_list, *txn_req);
597 MGMTD_TXN_DBG("Removed req-id: %" PRIu64
598 " from pending-list (left:%zu)",
599 (*txn_req)->req_id,
600 mgmt_txn_reqs_count(pending_list));
601 } else if (req_list) {
602 mgmt_txn_reqs_del(req_list, *txn_req);
603 MGMTD_TXN_DBG("Removed req-id: %" PRIu64
604 " from request-list (left:%zu)",
605 (*txn_req)->req_id,
606 mgmt_txn_reqs_count(req_list));
607 }
608
609 (*txn_req)->pending_be_proc = false;
610 MGMTD_TXN_UNLOCK(&(*txn_req)->txn);
611 XFREE(MTYPE_MGMTD_TXN_REQ, (*txn_req));
612 *txn_req = NULL;
613 }
614
615 static void mgmt_txn_process_set_cfg(struct event *thread)
616 {
617 struct mgmt_txn_ctx *txn;
618 struct mgmt_txn_req *txn_req;
619 struct mgmt_ds_ctx *ds_ctx;
620 struct nb_config *nb_config;
621 char err_buf[1024];
622 bool error;
623 int num_processed = 0;
624 size_t left;
625 struct mgmt_commit_stats *cmt_stats;
626 int ret = 0;
627
628 txn = (struct mgmt_txn_ctx *)EVENT_ARG(thread);
629 assert(txn);
630 cmt_stats = mgmt_fe_get_session_commit_stats(txn->session_id);
631
632 MGMTD_TXN_DBG("Processing %zu SET_CONFIG requests txn-id:%" PRIu64
633 " session-id: %" PRIu64,
634 mgmt_txn_reqs_count(&txn->set_cfg_reqs), txn->txn_id,
635 txn->session_id);
636
637 FOREACH_TXN_REQ_IN_LIST (&txn->set_cfg_reqs, txn_req) {
638 error = false;
639 assert(txn_req->req_event == MGMTD_TXN_PROC_SETCFG);
640 ds_ctx = txn_req->req.set_cfg->ds_ctx;
641 if (!ds_ctx) {
642 mgmt_fe_send_set_cfg_reply(
643 txn->session_id, txn->txn_id,
644 txn_req->req.set_cfg->ds_id, txn_req->req_id,
645 MGMTD_INTERNAL_ERROR, "No such datastore!",
646 txn_req->req.set_cfg->implicit_commit);
647 error = true;
648 goto mgmt_txn_process_set_cfg_done;
649 }
650
651 nb_config = mgmt_ds_get_nb_config(ds_ctx);
652 if (!nb_config) {
653 mgmt_fe_send_set_cfg_reply(
654 txn->session_id, txn->txn_id,
655 txn_req->req.set_cfg->ds_id, txn_req->req_id,
656 MGMTD_INTERNAL_ERROR,
657 "Unable to retrieve DS Config Tree!",
658 txn_req->req.set_cfg->implicit_commit);
659 error = true;
660 goto mgmt_txn_process_set_cfg_done;
661 }
662
663 error = false;
664 nb_candidate_edit_config_changes(
665 nb_config, txn_req->req.set_cfg->cfg_changes,
666 (size_t)txn_req->req.set_cfg->num_cfg_changes, NULL,
667 NULL, 0, err_buf, sizeof(err_buf), &error);
668 if (error) {
669 mgmt_fe_send_set_cfg_reply(
670 txn->session_id, txn->txn_id,
671 txn_req->req.set_cfg->ds_id, txn_req->req_id,
672 MGMTD_INTERNAL_ERROR, err_buf,
673 txn_req->req.set_cfg->implicit_commit);
674 goto mgmt_txn_process_set_cfg_done;
675 }
676
677 if (txn_req->req.set_cfg->implicit_commit) {
678 assert(mgmt_txn_reqs_count(&txn->set_cfg_reqs) == 1);
679 assert(txn_req->req.set_cfg->dst_ds_ctx);
680
681 ret = mgmt_ds_write_lock(
682 txn_req->req.set_cfg->dst_ds_ctx);
683 if (ret != 0) {
684 MGMTD_TXN_ERR(
685 "Failed to lock DS %u txn-id: %" PRIu64
686 " session-id: %" PRIu64 " err: %s",
687 txn_req->req.set_cfg->dst_ds_id,
688 txn->txn_id, txn->session_id,
689 strerror(ret));
690 mgmt_txn_send_commit_cfg_reply(
691 txn, MGMTD_DS_LOCK_FAILED,
692 "Lock running DS before implicit commit failed!");
693 goto mgmt_txn_process_set_cfg_done;
694 }
695
696 mgmt_txn_send_commit_config_req(
697 txn->txn_id, txn_req->req_id,
698 txn_req->req.set_cfg->ds_id,
699 txn_req->req.set_cfg->ds_ctx,
700 txn_req->req.set_cfg->dst_ds_id,
701 txn_req->req.set_cfg->dst_ds_ctx, false,
702 false, true);
703
704 if (mm->perf_stats_en)
705 gettimeofday(&cmt_stats->last_start, NULL);
706 cmt_stats->commit_cnt++;
707 } else if (mgmt_fe_send_set_cfg_reply(
708 txn->session_id, txn->txn_id,
709 txn_req->req.set_cfg->ds_id,
710 txn_req->req_id, MGMTD_SUCCESS, NULL, false)
711 != 0) {
712 MGMTD_TXN_ERR(
713 "Failed to send SET_CONFIG_REPLY txn-id %" PRIu64
714 " session-id: %" PRIu64,
715 txn->txn_id, txn->session_id);
716 error = true;
717 }
718
719 mgmt_txn_process_set_cfg_done:
720
721 /*
722 * Note: The following will remove it from the list as well.
723 */
724 mgmt_txn_req_free(&txn_req);
725
726 num_processed++;
727 if (num_processed == MGMTD_TXN_MAX_NUM_SETCFG_PROC)
728 break;
729 }
730
731 left = mgmt_txn_reqs_count(&txn->set_cfg_reqs);
732 if (left) {
733 MGMTD_TXN_DBG(
734 "Processed maximum number of Set-Config requests (%d/%d/%d). Rescheduling for rest.",
735 num_processed, MGMTD_TXN_MAX_NUM_SETCFG_PROC,
736 (int)left);
737 mgmt_txn_register_event(txn, MGMTD_TXN_PROC_SETCFG);
738 }
739 }
740
741 static int mgmt_txn_send_commit_cfg_reply(struct mgmt_txn_ctx *txn,
742 enum mgmt_result result,
743 const char *error_if_any)
744 {
745 int ret = 0;
746 bool success, create_cmt_info_rec;
747
748 if (!txn->commit_cfg_req)
749 return -1;
750
751 success = (result == MGMTD_SUCCESS || result == MGMTD_NO_CFG_CHANGES);
752
753 if (!txn->commit_cfg_req->req.commit_cfg.implicit && txn->session_id
754 && mgmt_fe_send_commit_cfg_reply(
755 txn->session_id, txn->txn_id,
756 txn->commit_cfg_req->req.commit_cfg.src_ds_id,
757 txn->commit_cfg_req->req.commit_cfg.dst_ds_id,
758 txn->commit_cfg_req->req_id,
759 txn->commit_cfg_req->req.commit_cfg.validate_only,
760 result, error_if_any)
761 != 0) {
762 MGMTD_TXN_ERR(
763 "Failed to send COMMIT-CONFIG-REPLY txn-id: %" PRIu64
764 " session-id: %" PRIu64,
765 txn->txn_id, txn->session_id);
766 }
767
768 if (txn->commit_cfg_req->req.commit_cfg.implicit && txn->session_id
769 && mgmt_fe_send_set_cfg_reply(
770 txn->session_id, txn->txn_id,
771 txn->commit_cfg_req->req.commit_cfg.src_ds_id,
772 txn->commit_cfg_req->req_id,
773 success ? MGMTD_SUCCESS : MGMTD_INTERNAL_ERROR,
774 error_if_any, true)
775 != 0) {
776 MGMTD_TXN_ERR("Failed to send SET-CONFIG-REPLY txn-id: %" PRIu64
777 " session-id: %" PRIu64,
778 txn->txn_id, txn->session_id);
779 }
780
781 if (success) {
782 /* Stop the commit-timeout timer */
783 EVENT_OFF(txn->comm_cfg_timeout);
784
785 create_cmt_info_rec =
786 (result != MGMTD_NO_CFG_CHANGES &&
787 !txn->commit_cfg_req->req.commit_cfg.rollback);
788
789 /*
790 * Successful commit: Merge Src DS into Dst DS if and only if
791 * this was not a validate-only or abort request.
792 */
793 if ((txn->session_id
794 && !txn->commit_cfg_req->req.commit_cfg.validate_only
795 && !txn->commit_cfg_req->req.commit_cfg.abort)
796 || txn->commit_cfg_req->req.commit_cfg.rollback) {
797 mgmt_ds_copy_dss(txn->commit_cfg_req->req.commit_cfg
798 .src_ds_ctx,
799 txn->commit_cfg_req->req.commit_cfg
800 .dst_ds_ctx,
801 create_cmt_info_rec);
802 }
803
804 /*
805 * Restore Src DS back to Dest DS only through a commit abort
806 * request.
807 */
808 if (txn->session_id
809 && txn->commit_cfg_req->req.commit_cfg.abort)
810 mgmt_ds_copy_dss(txn->commit_cfg_req->req.commit_cfg
811 .dst_ds_ctx,
812 txn->commit_cfg_req->req.commit_cfg
813 .src_ds_ctx,
814 false);
815 } else {
816 /*
817 * The commit has failied. For implicit commit requests restore
818 * back the contents of the candidate DS.
819 */
820 if (txn->commit_cfg_req->req.commit_cfg.implicit)
821 mgmt_ds_copy_dss(txn->commit_cfg_req->req.commit_cfg
822 .dst_ds_ctx,
823 txn->commit_cfg_req->req.commit_cfg
824 .src_ds_ctx,
825 false);
826 }
827
828 if (txn->commit_cfg_req->req.commit_cfg.rollback) {
829 ret = mgmt_ds_unlock(
830 txn->commit_cfg_req->req.commit_cfg.dst_ds_ctx);
831 if (ret != 0)
832 MGMTD_TXN_ERR(
833 "Failed to unlock the dst DS during rollback : %s",
834 strerror(ret));
835
836 /*
837 * Resume processing the rollback command.
838 */
839 mgmt_history_rollback_complete(success);
840 }
841
842 if (txn->commit_cfg_req->req.commit_cfg.implicit)
843 if (mgmt_ds_unlock(
844 txn->commit_cfg_req->req.commit_cfg.dst_ds_ctx)
845 != 0)
846 MGMTD_TXN_ERR(
847 "Failed to unlock the dst DS during implicit : %s",
848 strerror(ret));
849
850 txn->commit_cfg_req->req.commit_cfg.cmt_stats = NULL;
851 mgmt_txn_req_free(&txn->commit_cfg_req);
852
853 /*
854 * The CONFIG Transaction should be destroyed from Frontend-adapter.
855 * But in case the transaction is not triggered from a front-end session
856 * we need to cleanup by itself.
857 */
858 if (!txn->session_id)
859 mgmt_txn_register_event(txn, MGMTD_TXN_CLEANUP);
860
861 return 0;
862 }
863
864 static void
865 mgmt_move_txn_cfg_batch_to_next(struct mgmt_commit_cfg_req *cmtcfg_req,
866 struct mgmt_txn_be_cfg_batch *cfg_btch,
867 struct mgmt_txn_batches_head *src_list,
868 struct mgmt_txn_batches_head *dst_list,
869 bool update_commit_phase,
870 enum mgmt_commit_phase to_phase)
871 {
872 mgmt_txn_batches_del(src_list, cfg_btch);
873
874 if (update_commit_phase) {
875 MGMTD_TXN_DBG("Move txn-id %" PRIu64 " batch-id: %" PRIu64
876 " from '%s' --> '%s'",
877 cfg_btch->txn->txn_id, cfg_btch->batch_id,
878 mgmt_commit_phase2str(cfg_btch->comm_phase),
879 mgmt_txn_commit_phase_str(cfg_btch->txn, false));
880 cfg_btch->comm_phase = to_phase;
881 }
882
883 mgmt_txn_batches_add_tail(dst_list, cfg_btch);
884 }
885
886 static void mgmt_move_txn_cfg_batches(struct mgmt_txn_ctx *txn,
887 struct mgmt_commit_cfg_req *cmtcfg_req,
888 struct mgmt_txn_batches_head *src_list,
889 struct mgmt_txn_batches_head *dst_list,
890 bool update_commit_phase,
891 enum mgmt_commit_phase to_phase)
892 {
893 struct mgmt_txn_be_cfg_batch *cfg_btch;
894
895 FOREACH_TXN_CFG_BATCH_IN_LIST (src_list, cfg_btch) {
896 mgmt_move_txn_cfg_batch_to_next(cmtcfg_req, cfg_btch, src_list,
897 dst_list, update_commit_phase,
898 to_phase);
899 }
900 }
901
902 static int
903 mgmt_try_move_commit_to_next_phase(struct mgmt_txn_ctx *txn,
904 struct mgmt_commit_cfg_req *cmtcfg_req)
905 {
906 struct mgmt_txn_batches_head *curr_list, *next_list;
907 enum mgmt_be_client_id id;
908
909 MGMTD_TXN_DBG("txn-id: %" PRIu64 ", Phase(current:'%s' next:'%s')",
910 txn->txn_id, mgmt_txn_commit_phase_str(txn, true),
911 mgmt_txn_commit_phase_str(txn, false));
912
913 /*
914 * Check if all clients has moved to next phase or not.
915 */
916 FOREACH_MGMTD_BE_CLIENT_ID (id) {
917 if (cmtcfg_req->subscr_info.xpath_subscr[id] &&
918 mgmt_txn_batches_count(&cmtcfg_req->curr_batches[id])) {
919 /*
920 * There's atleast once client who hasn't moved to
921 * next phase.
922 *
923 * TODO: Need to re-think this design for the case
924 * set of validators for a given YANG data item is
925 * different from the set of notifiers for the same.
926 */
927 return -1;
928 }
929 }
930
931 MGMTD_TXN_DBG("Move entire txn-id: %" PRIu64 " from '%s' to '%s'",
932 txn->txn_id, mgmt_txn_commit_phase_str(txn, true),
933 mgmt_txn_commit_phase_str(txn, false));
934
935 /*
936 * If we are here, it means all the clients has moved to next phase.
937 * So we can move the whole commit to next phase.
938 */
939 cmtcfg_req->curr_phase = cmtcfg_req->next_phase;
940 cmtcfg_req->next_phase++;
941 MGMTD_TXN_DBG("Move back all config batches for txn-id: %" PRIu64
942 " from next to current branch",
943 txn->txn_id);
944 FOREACH_MGMTD_BE_CLIENT_ID (id) {
945 curr_list = &cmtcfg_req->curr_batches[id];
946 next_list = &cmtcfg_req->next_batches[id];
947 mgmt_move_txn_cfg_batches(txn, cmtcfg_req, next_list,
948 curr_list, false, 0);
949 }
950
951 mgmt_txn_register_event(txn, MGMTD_TXN_PROC_COMMITCFG);
952
953 return 0;
954 }
955
956 static int
957 mgmt_move_be_commit_to_next_phase(struct mgmt_txn_ctx *txn,
958 struct mgmt_be_client_adapter *adapter)
959 {
960 struct mgmt_commit_cfg_req *cmtcfg_req;
961 struct mgmt_txn_batches_head *curr_list, *next_list;
962
963 if (txn->type != MGMTD_TXN_TYPE_CONFIG || !txn->commit_cfg_req)
964 return -1;
965
966 cmtcfg_req = &txn->commit_cfg_req->req.commit_cfg;
967
968 MGMTD_TXN_DBG("Move txn-id: %" PRIu64
969 " for '%s' Phase(current: '%s' next:'%s')",
970 txn->txn_id, adapter->name,
971 mgmt_txn_commit_phase_str(txn, true),
972 mgmt_txn_commit_phase_str(txn, false));
973
974 MGMTD_TXN_DBG(
975 "Move all config batches for '%s' from current to next list",
976 adapter->name);
977 curr_list = &cmtcfg_req->curr_batches[adapter->id];
978 next_list = &cmtcfg_req->next_batches[adapter->id];
979 mgmt_move_txn_cfg_batches(txn, cmtcfg_req, curr_list, next_list, true,
980 cmtcfg_req->next_phase);
981
982 MGMTD_TXN_DBG("txn-id: %" PRIu64 ", Phase(current:'%s' next:'%s')",
983 txn->txn_id, mgmt_txn_commit_phase_str(txn, true),
984 mgmt_txn_commit_phase_str(txn, false));
985
986 /*
987 * Check if all clients has moved to next phase or not.
988 */
989 mgmt_try_move_commit_to_next_phase(txn, cmtcfg_req);
990
991 return 0;
992 }
993
994 static int mgmt_txn_create_config_batches(struct mgmt_txn_req *txn_req,
995 struct nb_config_cbs *changes)
996 {
997 struct nb_config_cb *cb, *nxt;
998 struct nb_config_change *chg;
999 struct mgmt_txn_be_cfg_batch *cfg_btch;
1000 struct mgmt_be_client_subscr_info subscr_info;
1001 char *xpath = NULL, *value = NULL;
1002 char err_buf[1024];
1003 enum mgmt_be_client_id id;
1004 struct mgmt_be_client_adapter *adapter;
1005 struct mgmt_commit_cfg_req *cmtcfg_req;
1006 bool found_validator;
1007 int num_chgs = 0;
1008 int xpath_len, value_len;
1009
1010 cmtcfg_req = &txn_req->req.commit_cfg;
1011
1012 RB_FOREACH_SAFE (cb, nb_config_cbs, changes, nxt) {
1013 chg = (struct nb_config_change *)cb;
1014
1015 /*
1016 * Could have directly pointed to xpath in nb_node.
1017 * But dont want to mess with it now.
1018 * xpath = chg->cb.nb_node->xpath;
1019 */
1020 xpath = lyd_path(chg->cb.dnode, LYD_PATH_STD, NULL, 0);
1021 if (!xpath) {
1022 (void)mgmt_txn_send_commit_cfg_reply(
1023 txn_req->txn, MGMTD_INTERNAL_ERROR,
1024 "Internal error! Could not get Xpath from Ds node!");
1025 return -1;
1026 }
1027
1028 value = (char *)lyd_get_value(chg->cb.dnode);
1029 if (!value)
1030 value = (char *)MGMTD_BE_CONTAINER_NODE_VAL;
1031
1032 MGMTD_TXN_DBG("XPATH: %s, Value: '%s'", xpath,
1033 value ? value : "NIL");
1034
1035 mgmt_be_get_subscr_info_for_xpath(xpath, &subscr_info);
1036
1037 xpath_len = strlen(xpath) + 1;
1038 value_len = strlen(value) + 1;
1039 found_validator = false;
1040 FOREACH_MGMTD_BE_CLIENT_ID (id) {
1041 if (!(subscr_info.xpath_subscr[id] &
1042 (MGMT_SUBSCR_VALIDATE_CFG |
1043 MGMT_SUBSCR_NOTIFY_CFG)))
1044 continue;
1045
1046 adapter = mgmt_be_get_adapter_by_id(id);
1047 if (!adapter)
1048 continue;
1049
1050 cfg_btch = cmtcfg_req->last_be_cfg_batch[id];
1051 if (!cfg_btch
1052 || (cfg_btch->num_cfg_data
1053 == MGMTD_MAX_CFG_CHANGES_IN_BATCH)
1054 || (cfg_btch->buf_space_left
1055 < (xpath_len + value_len))) {
1056 /* Allocate a new config batch */
1057 cfg_btch = mgmt_txn_cfg_batch_alloc(
1058 txn_req->txn, id, adapter);
1059 }
1060
1061 cfg_btch->buf_space_left -= (xpath_len + value_len);
1062 memcpy(&cfg_btch->xp_subscr[cfg_btch->num_cfg_data],
1063 &subscr_info.xpath_subscr[id],
1064 sizeof(cfg_btch->xp_subscr[0]));
1065
1066 mgmt_yang_cfg_data_req_init(
1067 &cfg_btch->cfg_data[cfg_btch->num_cfg_data]);
1068 cfg_btch->cfg_datap[cfg_btch->num_cfg_data] =
1069 &cfg_btch->cfg_data[cfg_btch->num_cfg_data];
1070
1071 if (chg->cb.operation == NB_OP_DESTROY)
1072 cfg_btch->cfg_data[cfg_btch->num_cfg_data]
1073 .req_type =
1074 MGMTD__CFG_DATA_REQ_TYPE__DELETE_DATA;
1075 else
1076 cfg_btch->cfg_data[cfg_btch->num_cfg_data]
1077 .req_type =
1078 MGMTD__CFG_DATA_REQ_TYPE__SET_DATA;
1079
1080 mgmt_yang_data_init(
1081 &cfg_btch->data[cfg_btch->num_cfg_data]);
1082 cfg_btch->cfg_data[cfg_btch->num_cfg_data].data =
1083 &cfg_btch->data[cfg_btch->num_cfg_data];
1084 cfg_btch->data[cfg_btch->num_cfg_data].xpath =
1085 strdup(xpath);
1086
1087 mgmt_yang_data_value_init(
1088 &cfg_btch->value[cfg_btch->num_cfg_data]);
1089 cfg_btch->data[cfg_btch->num_cfg_data].value =
1090 &cfg_btch->value[cfg_btch->num_cfg_data];
1091 cfg_btch->value[cfg_btch->num_cfg_data].value_case =
1092 MGMTD__YANG_DATA_VALUE__VALUE_ENCODED_STR_VAL;
1093 cfg_btch->value[cfg_btch->num_cfg_data]
1094 .encoded_str_val = value;
1095 value = NULL;
1096
1097 if (subscr_info.xpath_subscr[id] &
1098 MGMT_SUBSCR_VALIDATE_CFG)
1099 found_validator = true;
1100
1101 cmtcfg_req->subscr_info.xpath_subscr[id] |=
1102 subscr_info.xpath_subscr[id];
1103 MGMTD_TXN_DBG(" -- %s, {V:%d, N:%d}, batch-id: %" PRIu64
1104 " item:%d",
1105 adapter->name,
1106 (subscr_info.xpath_subscr[id] &
1107 MGMT_SUBSCR_VALIDATE_CFG) != 0,
1108 (subscr_info.xpath_subscr[id] &
1109 MGMT_SUBSCR_NOTIFY_CFG) != 0,
1110 cfg_btch->batch_id,
1111 (int)cfg_btch->num_cfg_data);
1112
1113 cfg_btch->num_cfg_data++;
1114 num_chgs++;
1115 }
1116
1117 if (!found_validator) {
1118 snprintf(err_buf, sizeof(err_buf),
1119 "No validator module found for XPATH: '%s",
1120 xpath);
1121 MGMTD_TXN_ERR("***** %s", err_buf);
1122 }
1123
1124 free(xpath);
1125 }
1126
1127 cmtcfg_req->cmt_stats->last_batch_cnt = num_chgs;
1128 if (!num_chgs) {
1129 (void)mgmt_txn_send_commit_cfg_reply(
1130 txn_req->txn, MGMTD_NO_CFG_CHANGES,
1131 "No changes found to commit!");
1132 return -1;
1133 }
1134
1135 cmtcfg_req->next_phase = MGMTD_COMMIT_PHASE_TXN_CREATE;
1136 return 0;
1137 }
1138
1139 static int mgmt_txn_prepare_config(struct mgmt_txn_ctx *txn)
1140 {
1141 struct nb_context nb_ctx;
1142 struct nb_config *nb_config;
1143 struct nb_config_cbs changes;
1144 struct nb_config_cbs *cfg_chgs = NULL;
1145 int ret;
1146 bool del_cfg_chgs = false;
1147
1148 ret = 0;
1149 memset(&nb_ctx, 0, sizeof(nb_ctx));
1150 memset(&changes, 0, sizeof(changes));
1151 if (txn->commit_cfg_req->req.commit_cfg.cfg_chgs) {
1152 cfg_chgs = txn->commit_cfg_req->req.commit_cfg.cfg_chgs;
1153 del_cfg_chgs = true;
1154 goto mgmt_txn_prep_config_validation_done;
1155 }
1156
1157 if (txn->commit_cfg_req->req.commit_cfg.src_ds_id
1158 != MGMTD_DS_CANDIDATE) {
1159 (void)mgmt_txn_send_commit_cfg_reply(
1160 txn, MGMTD_INVALID_PARAM,
1161 "Source DS cannot be any other than CANDIDATE!");
1162 ret = -1;
1163 goto mgmt_txn_prepare_config_done;
1164 }
1165
1166 if (txn->commit_cfg_req->req.commit_cfg.dst_ds_id
1167 != MGMTD_DS_RUNNING) {
1168 (void)mgmt_txn_send_commit_cfg_reply(
1169 txn, MGMTD_INVALID_PARAM,
1170 "Destination DS cannot be any other than RUNNING!");
1171 ret = -1;
1172 goto mgmt_txn_prepare_config_done;
1173 }
1174
1175 if (!txn->commit_cfg_req->req.commit_cfg.src_ds_ctx) {
1176 (void)mgmt_txn_send_commit_cfg_reply(
1177 txn, MGMTD_INVALID_PARAM, "No such source datastore!");
1178 ret = -1;
1179 goto mgmt_txn_prepare_config_done;
1180 }
1181
1182 if (!txn->commit_cfg_req->req.commit_cfg.dst_ds_ctx) {
1183 (void)mgmt_txn_send_commit_cfg_reply(
1184 txn, MGMTD_INVALID_PARAM,
1185 "No such destination datastore!");
1186 ret = -1;
1187 goto mgmt_txn_prepare_config_done;
1188 }
1189
1190 if (txn->commit_cfg_req->req.commit_cfg.abort) {
1191 /*
1192 * This is a commit abort request. Return back success.
1193 * That should trigger a restore of Candidate datastore to
1194 * Running.
1195 */
1196 (void)mgmt_txn_send_commit_cfg_reply(txn, MGMTD_SUCCESS,
1197 NULL);
1198 goto mgmt_txn_prepare_config_done;
1199 }
1200
1201 nb_config = mgmt_ds_get_nb_config(
1202 txn->commit_cfg_req->req.commit_cfg.src_ds_ctx);
1203 if (!nb_config) {
1204 (void)mgmt_txn_send_commit_cfg_reply(
1205 txn, MGMTD_INTERNAL_ERROR,
1206 "Unable to retrieve Commit DS Config Tree!");
1207 ret = -1;
1208 goto mgmt_txn_prepare_config_done;
1209 }
1210
1211 /*
1212 * Check for diffs from scratch buffer. If found empty
1213 * get the diff from Candidate DS itself.
1214 */
1215 cfg_chgs = &nb_config->cfg_chgs;
1216 if (RB_EMPTY(nb_config_cbs, cfg_chgs)) {
1217 /*
1218 * This could be the case when the config is directly
1219 * loaded onto the candidate DS from a file. Get the
1220 * diff from a full comparison of the candidate and
1221 * running DSs.
1222 */
1223 nb_config_diff(
1224 mgmt_ds_get_nb_config(txn->commit_cfg_req->req
1225 .commit_cfg.dst_ds_ctx),
1226 nb_config, &changes);
1227 cfg_chgs = &changes;
1228 del_cfg_chgs = true;
1229 }
1230
1231 if (RB_EMPTY(nb_config_cbs, cfg_chgs)) {
1232 /*
1233 * This means there's no changes to commit whatsoever
1234 * is the source of the changes in config.
1235 */
1236 (void)mgmt_txn_send_commit_cfg_reply(
1237 txn, MGMTD_NO_CFG_CHANGES,
1238 "No changes found to be committed!");
1239 ret = -1;
1240 goto mgmt_txn_prepare_config_done;
1241 }
1242
1243 #ifdef MGMTD_LOCAL_VALIDATIONS_ENABLED
1244 if (mm->perf_stats_en)
1245 gettimeofday(&txn->commit_cfg_req->req.commit_cfg.cmt_stats
1246 ->validate_start,
1247 NULL);
1248 /*
1249 * Validate YANG contents of the source DS and get the diff
1250 * between source and destination DS contents.
1251 */
1252 char err_buf[1024] = {0};
1253 nb_ctx.client = NB_CLIENT_MGMTD_SERVER;
1254 nb_ctx.user = (void *)txn;
1255
1256 ret = nb_candidate_validate_yang(nb_config, false, err_buf,
1257 sizeof(err_buf) - 1);
1258 if (ret != NB_OK) {
1259 if (strncmp(err_buf, " ", strlen(err_buf)) == 0)
1260 strlcpy(err_buf, "Validation failed", sizeof(err_buf));
1261 (void)mgmt_txn_send_commit_cfg_reply(txn, MGMTD_INVALID_PARAM,
1262 err_buf);
1263 ret = -1;
1264 goto mgmt_txn_prepare_config_done;
1265 }
1266 /*
1267 * Perform application level validations locally on the MGMTD
1268 * process by calling application specific validation routines
1269 * loaded onto MGMTD process using libraries.
1270 */
1271 ret = nb_candidate_validate_code(&nb_ctx, nb_config, &changes, err_buf,
1272 sizeof(err_buf) - 1);
1273 if (ret != NB_OK) {
1274 if (strncmp(err_buf, " ", strlen(err_buf)) == 0)
1275 strlcpy(err_buf, "Validation failed", sizeof(err_buf));
1276 (void)mgmt_txn_send_commit_cfg_reply(txn, MGMTD_INVALID_PARAM,
1277 err_buf);
1278 ret = -1;
1279 goto mgmt_txn_prepare_config_done;
1280 }
1281
1282 if (txn->commit_cfg_req->req.commit_cfg.validate_only) {
1283 /*
1284 * This was a validate-only COMMIT request return success.
1285 */
1286 (void)mgmt_txn_send_commit_cfg_reply(txn, MGMTD_SUCCESS,
1287 NULL);
1288 goto mgmt_txn_prepare_config_done;
1289 }
1290 #endif /* ifdef MGMTD_LOCAL_VALIDATIONS_ENABLED */
1291
1292 mgmt_txn_prep_config_validation_done:
1293
1294 if (mm->perf_stats_en)
1295 gettimeofday(&txn->commit_cfg_req->req.commit_cfg.cmt_stats
1296 ->prep_cfg_start,
1297 NULL);
1298
1299 /*
1300 * Iterate over the diffs and create ordered batches of config
1301 * commands to be validated.
1302 */
1303 ret = mgmt_txn_create_config_batches(txn->commit_cfg_req, cfg_chgs);
1304 if (ret != 0) {
1305 ret = -1;
1306 goto mgmt_txn_prepare_config_done;
1307 }
1308
1309 /* Move to the Transaction Create Phase */
1310 txn->commit_cfg_req->req.commit_cfg.curr_phase =
1311 MGMTD_COMMIT_PHASE_TXN_CREATE;
1312 mgmt_txn_register_event(txn, MGMTD_TXN_PROC_COMMITCFG);
1313
1314 /*
1315 * Start the COMMIT Timeout Timer to abort Txn if things get stuck at
1316 * backend.
1317 */
1318 mgmt_txn_register_event(txn, MGMTD_TXN_COMMITCFG_TIMEOUT);
1319 mgmt_txn_prepare_config_done:
1320
1321 if (cfg_chgs && del_cfg_chgs)
1322 nb_config_diff_del_changes(cfg_chgs);
1323
1324 return ret;
1325 }
1326
1327 static int mgmt_txn_send_be_txn_create(struct mgmt_txn_ctx *txn)
1328 {
1329 enum mgmt_be_client_id id;
1330 struct mgmt_be_client_adapter *adapter;
1331 struct mgmt_commit_cfg_req *cmtcfg_req;
1332 struct mgmt_txn_be_cfg_batch *cfg_btch;
1333
1334 assert(txn->type == MGMTD_TXN_TYPE_CONFIG && txn->commit_cfg_req);
1335
1336 cmtcfg_req = &txn->commit_cfg_req->req.commit_cfg;
1337 FOREACH_MGMTD_BE_CLIENT_ID (id) {
1338 if (cmtcfg_req->subscr_info.xpath_subscr[id]) {
1339 adapter = mgmt_be_get_adapter_by_id(id);
1340 if (mgmt_be_create_txn(adapter, txn->txn_id)
1341 != 0) {
1342 (void)mgmt_txn_send_commit_cfg_reply(
1343 txn, MGMTD_INTERNAL_ERROR,
1344 "Could not send TXN_CREATE to backend adapter");
1345 return -1;
1346 }
1347
1348 FOREACH_TXN_CFG_BATCH_IN_LIST (
1349 &txn->commit_cfg_req->req.commit_cfg
1350 .curr_batches[id],
1351 cfg_btch)
1352 cfg_btch->comm_phase =
1353 MGMTD_COMMIT_PHASE_TXN_CREATE;
1354 }
1355 }
1356
1357 txn->commit_cfg_req->req.commit_cfg.next_phase =
1358 MGMTD_COMMIT_PHASE_SEND_CFG;
1359
1360 /*
1361 * Dont move the commit to next phase yet. Wait for the TXN_REPLY to
1362 * come back.
1363 */
1364
1365 MGMTD_TXN_DBG("txn-id: %" PRIu64 " session-id: %" PRIu64
1366 " Phase(Current:'%s', Next: '%s')",
1367 txn->txn_id, txn->session_id,
1368 mgmt_txn_commit_phase_str(txn, true),
1369 mgmt_txn_commit_phase_str(txn, false));
1370
1371 return 0;
1372 }
1373
1374 static int
1375 mgmt_txn_send_be_cfg_data(struct mgmt_txn_ctx *txn,
1376 struct mgmt_be_client_adapter *adapter)
1377 {
1378 struct mgmt_commit_cfg_req *cmtcfg_req;
1379 struct mgmt_txn_be_cfg_batch *cfg_btch;
1380 struct mgmt_be_cfgreq cfg_req = {0};
1381 size_t num_batches, indx;
1382
1383 assert(txn->type == MGMTD_TXN_TYPE_CONFIG && txn->commit_cfg_req);
1384
1385 cmtcfg_req = &txn->commit_cfg_req->req.commit_cfg;
1386 assert(cmtcfg_req->subscr_info.xpath_subscr[adapter->id]);
1387
1388 indx = 0;
1389 num_batches =
1390 mgmt_txn_batches_count(&cmtcfg_req->curr_batches[adapter->id]);
1391 FOREACH_TXN_CFG_BATCH_IN_LIST (&cmtcfg_req->curr_batches[adapter->id],
1392 cfg_btch) {
1393 assert(cmtcfg_req->next_phase == MGMTD_COMMIT_PHASE_SEND_CFG);
1394
1395 cfg_req.cfgdata_reqs = cfg_btch->cfg_datap;
1396 cfg_req.num_reqs = cfg_btch->num_cfg_data;
1397 indx++;
1398 if (mgmt_be_send_cfg_data_create_req(
1399 adapter, txn->txn_id, cfg_btch->batch_id, &cfg_req,
1400 indx == num_batches ? true : false)
1401 != 0) {
1402 (void)mgmt_txn_send_commit_cfg_reply(
1403 txn, MGMTD_INTERNAL_ERROR,
1404 "Internal Error! Could not send config data to backend!");
1405 MGMTD_TXN_ERR(
1406 "Could not send CFGDATA_CREATE txn-id: %" PRIu64
1407 " batch-id: %" PRIu64 " to client '%s",
1408 txn->txn_id, cfg_btch->batch_id, adapter->name);
1409 return -1;
1410 }
1411
1412 cmtcfg_req->cmt_stats->last_num_cfgdata_reqs++;
1413 mgmt_move_txn_cfg_batch_to_next(
1414 cmtcfg_req, cfg_btch,
1415 &cmtcfg_req->curr_batches[adapter->id],
1416 &cmtcfg_req->next_batches[adapter->id], true,
1417 MGMTD_COMMIT_PHASE_SEND_CFG);
1418 }
1419
1420 /*
1421 * This could ne the last Backend Client to send CFGDATA_CREATE_REQ to.
1422 * Try moving the commit to next phase.
1423 */
1424 mgmt_try_move_commit_to_next_phase(txn, cmtcfg_req);
1425
1426 return 0;
1427 }
1428
1429 static int
1430 mgmt_txn_send_be_txn_delete(struct mgmt_txn_ctx *txn,
1431 struct mgmt_be_client_adapter *adapter)
1432 {
1433 struct mgmt_commit_cfg_req *cmtcfg_req;
1434 struct mgmt_txn_be_cfg_batch *cfg_btch;
1435
1436 assert(txn->type == MGMTD_TXN_TYPE_CONFIG && txn->commit_cfg_req);
1437
1438 cmtcfg_req = &txn->commit_cfg_req->req.commit_cfg;
1439 if (cmtcfg_req->subscr_info.xpath_subscr[adapter->id]) {
1440 adapter = mgmt_be_get_adapter_by_id(adapter->id);
1441 (void)mgmt_be_destroy_txn(adapter, txn->txn_id);
1442
1443 FOREACH_TXN_CFG_BATCH_IN_LIST (
1444 &txn->commit_cfg_req->req.commit_cfg
1445 .curr_batches[adapter->id],
1446 cfg_btch)
1447 cfg_btch->comm_phase = MGMTD_COMMIT_PHASE_TXN_DELETE;
1448 }
1449
1450 return 0;
1451 }
1452
1453 static void mgmt_txn_cfg_commit_timedout(struct event *thread)
1454 {
1455 struct mgmt_txn_ctx *txn;
1456
1457 txn = (struct mgmt_txn_ctx *)EVENT_ARG(thread);
1458 assert(txn);
1459
1460 assert(txn->type == MGMTD_TXN_TYPE_CONFIG);
1461
1462 if (!txn->commit_cfg_req)
1463 return;
1464
1465 MGMTD_TXN_ERR("Backend timeout txn-id: %" PRIu64 " aborting commit",
1466 txn->txn_id);
1467
1468 /*
1469 * Send a COMMIT_CONFIG_REPLY with failure.
1470 * NOTE: The transaction cleanup will be triggered from Front-end
1471 * adapter.
1472 */
1473 mgmt_txn_send_commit_cfg_reply(
1474 txn, MGMTD_INTERNAL_ERROR,
1475 "Operation on the backend timed-out. Aborting commit!");
1476 }
1477
1478 /*
1479 * Send CFG_APPLY_REQs to all the backend client.
1480 *
1481 * NOTE: This is always dispatched when all CFGDATA_CREATE_REQs
1482 * for all backend clients has been generated. Please see
1483 * mgmt_txn_register_event() and mgmt_txn_process_commit_cfg()
1484 * for details.
1485 */
1486 static int mgmt_txn_send_be_cfg_apply(struct mgmt_txn_ctx *txn)
1487 {
1488 enum mgmt_be_client_id id;
1489 struct mgmt_be_client_adapter *adapter;
1490 struct mgmt_commit_cfg_req *cmtcfg_req;
1491 struct mgmt_txn_batches_head *btch_list;
1492 struct mgmt_txn_be_cfg_batch *cfg_btch;
1493
1494 assert(txn->type == MGMTD_TXN_TYPE_CONFIG && txn->commit_cfg_req);
1495
1496 cmtcfg_req = &txn->commit_cfg_req->req.commit_cfg;
1497 if (cmtcfg_req->validate_only) {
1498 /*
1499 * If this was a validate-only COMMIT request return success.
1500 */
1501 (void)mgmt_txn_send_commit_cfg_reply(txn, MGMTD_SUCCESS,
1502 NULL);
1503 return 0;
1504 }
1505
1506 FOREACH_MGMTD_BE_CLIENT_ID (id) {
1507 if (cmtcfg_req->subscr_info.xpath_subscr[id] &
1508 MGMT_SUBSCR_NOTIFY_CFG) {
1509 adapter = mgmt_be_get_adapter_by_id(id);
1510 if (!adapter)
1511 return -1;
1512
1513 btch_list = &cmtcfg_req->curr_batches[id];
1514 if (mgmt_be_send_cfg_apply_req(adapter, txn->txn_id)
1515 != 0) {
1516 (void)mgmt_txn_send_commit_cfg_reply(
1517 txn, MGMTD_INTERNAL_ERROR,
1518 "Could not send CFG_APPLY_REQ to backend adapter");
1519 return -1;
1520 }
1521 cmtcfg_req->cmt_stats->last_num_apply_reqs++;
1522
1523 UNSET_FLAG(adapter->flags,
1524 MGMTD_BE_ADAPTER_FLAGS_CFG_SYNCED);
1525
1526 FOREACH_TXN_CFG_BATCH_IN_LIST (btch_list, cfg_btch)
1527 cfg_btch->comm_phase =
1528 MGMTD_COMMIT_PHASE_APPLY_CFG;
1529 }
1530 }
1531
1532 txn->commit_cfg_req->req.commit_cfg.next_phase =
1533 MGMTD_COMMIT_PHASE_TXN_DELETE;
1534
1535 /*
1536 * Dont move the commit to next phase yet. Wait for all VALIDATE_REPLIES
1537 * to come back.
1538 */
1539
1540 return 0;
1541 }
1542
1543 static void mgmt_txn_process_commit_cfg(struct event *thread)
1544 {
1545 struct mgmt_txn_ctx *txn;
1546 struct mgmt_commit_cfg_req *cmtcfg_req;
1547
1548 txn = (struct mgmt_txn_ctx *)EVENT_ARG(thread);
1549 assert(txn);
1550
1551 MGMTD_TXN_DBG("Processing COMMIT_CONFIG for txn-id: %" PRIu64
1552 " session-id: %" PRIu64
1553 " Phase(Current:'%s', Next: '%s')",
1554 txn->txn_id, txn->session_id,
1555 mgmt_txn_commit_phase_str(txn, true),
1556 mgmt_txn_commit_phase_str(txn, false));
1557
1558 assert(txn->commit_cfg_req);
1559 cmtcfg_req = &txn->commit_cfg_req->req.commit_cfg;
1560 switch (cmtcfg_req->curr_phase) {
1561 case MGMTD_COMMIT_PHASE_PREPARE_CFG:
1562 mgmt_txn_prepare_config(txn);
1563 break;
1564 case MGMTD_COMMIT_PHASE_TXN_CREATE:
1565 if (mm->perf_stats_en)
1566 gettimeofday(&cmtcfg_req->cmt_stats->txn_create_start,
1567 NULL);
1568 /*
1569 * Send TXN_CREATE_REQ to all Backend now.
1570 */
1571 mgmt_txn_send_be_txn_create(txn);
1572 break;
1573 case MGMTD_COMMIT_PHASE_SEND_CFG:
1574 if (mm->perf_stats_en)
1575 gettimeofday(&cmtcfg_req->cmt_stats->send_cfg_start,
1576 NULL);
1577 /*
1578 * All CFGDATA_CREATE_REQ should have been sent to
1579 * Backend by now.
1580 */
1581 #ifndef MGMTD_LOCAL_VALIDATIONS_ENABLED
1582 assert(cmtcfg_req->next_phase == MGMTD_COMMIT_PHASE_APPLY_CFG);
1583 MGMTD_TXN_DBG(
1584 "txn-id: %" PRIu64 " session-id: %" PRIu64
1585 " trigger sending CFG_VALIDATE_REQ to all backend clients",
1586 txn->txn_id, txn->session_id);
1587 #else /* ifndef MGMTD_LOCAL_VALIDATIONS_ENABLED */
1588 assert(cmtcfg_req->next_phase == MGMTD_COMMIT_PHASE_APPLY_CFG);
1589 MGMTD_TXN_DBG(
1590 "txn-id: %" PRIu64 " session-id: %" PRIu64
1591 " trigger sending CFG_APPLY_REQ to all backend clients",
1592 txn->txn_id, txn->session_id);
1593 #endif /* ifndef MGMTD_LOCAL_VALIDATIONS_ENABLED */
1594 break;
1595 case MGMTD_COMMIT_PHASE_APPLY_CFG:
1596 if (mm->perf_stats_en)
1597 gettimeofday(&cmtcfg_req->cmt_stats->apply_cfg_start,
1598 NULL);
1599 /*
1600 * We should have received successful CFG_VALIDATE_REPLY from
1601 * all concerned Backend Clients by now. Send out the
1602 * CFG_APPLY_REQs now.
1603 */
1604 mgmt_txn_send_be_cfg_apply(txn);
1605 break;
1606 case MGMTD_COMMIT_PHASE_TXN_DELETE:
1607 if (mm->perf_stats_en)
1608 gettimeofday(&cmtcfg_req->cmt_stats->txn_del_start,
1609 NULL);
1610 /*
1611 * We would have sent TXN_DELETE_REQ to all backend by now.
1612 * Send a successful CONFIG_COMMIT_REPLY back to front-end.
1613 * NOTE: This should also trigger DS merge/unlock and Txn
1614 * cleanup. Please see mgmt_fe_send_commit_cfg_reply() for
1615 * more details.
1616 */
1617 EVENT_OFF(txn->comm_cfg_timeout);
1618 mgmt_txn_send_commit_cfg_reply(txn, MGMTD_SUCCESS, NULL);
1619 break;
1620 case MGMTD_COMMIT_PHASE_MAX:
1621 break;
1622 }
1623
1624 MGMTD_TXN_DBG("txn-id:%" PRIu64 " session-id: %" PRIu64
1625 " phase updated to (current:'%s', next: '%s')",
1626 txn->txn_id, txn->session_id,
1627 mgmt_txn_commit_phase_str(txn, true),
1628 mgmt_txn_commit_phase_str(txn, false));
1629 }
1630
1631 static void mgmt_init_get_data_reply(struct mgmt_get_data_reply *get_reply)
1632 {
1633 size_t indx;
1634
1635 for (indx = 0; indx < array_size(get_reply->reply_data); indx++)
1636 get_reply->reply_datap[indx] = &get_reply->reply_data[indx];
1637 }
1638
1639 static void mgmt_reset_get_data_reply(struct mgmt_get_data_reply *get_reply)
1640 {
1641 int indx;
1642
1643 for (indx = 0; indx < get_reply->num_reply; indx++) {
1644 if (get_reply->reply_xpathp[indx]) {
1645 free(get_reply->reply_xpathp[indx]);
1646 get_reply->reply_xpathp[indx] = 0;
1647 }
1648 if (get_reply->reply_data[indx].xpath) {
1649 zlog_debug("%s free xpath %p", __func__,
1650 get_reply->reply_data[indx].xpath);
1651 free(get_reply->reply_data[indx].xpath);
1652 get_reply->reply_data[indx].xpath = 0;
1653 }
1654 }
1655
1656 get_reply->num_reply = 0;
1657 memset(&get_reply->data_reply, 0, sizeof(get_reply->data_reply));
1658 memset(&get_reply->reply_data, 0, sizeof(get_reply->reply_data));
1659 memset(&get_reply->reply_datap, 0, sizeof(get_reply->reply_datap));
1660
1661 memset(&get_reply->reply_value, 0, sizeof(get_reply->reply_value));
1662
1663 mgmt_init_get_data_reply(get_reply);
1664 }
1665
1666 static void mgmt_reset_get_data_reply_buf(struct mgmt_get_data_req *get_data)
1667 {
1668 if (get_data->reply)
1669 mgmt_reset_get_data_reply(get_data->reply);
1670 }
1671
1672 static void mgmt_txn_send_getcfg_reply_data(struct mgmt_txn_req *txn_req,
1673 struct mgmt_get_data_req *get_req)
1674 {
1675 struct mgmt_get_data_reply *get_reply;
1676 Mgmtd__YangDataReply *data_reply;
1677
1678 get_reply = get_req->reply;
1679 if (!get_reply)
1680 return;
1681
1682 data_reply = &get_reply->data_reply;
1683 mgmt_yang_data_reply_init(data_reply);
1684 data_reply->n_data = get_reply->num_reply;
1685 data_reply->data = get_reply->reply_datap;
1686 data_reply->next_indx =
1687 (!get_reply->last_batch ? get_req->total_reply : -1);
1688
1689 MGMTD_TXN_DBG("Sending %zu Get-Config/Data replies next-index:%" PRId64,
1690 data_reply->n_data, data_reply->next_indx);
1691
1692 switch (txn_req->req_event) {
1693 case MGMTD_TXN_PROC_GETCFG:
1694 if (mgmt_fe_send_get_cfg_reply(
1695 txn_req->txn->session_id, txn_req->txn->txn_id,
1696 get_req->ds_id, txn_req->req_id, MGMTD_SUCCESS,
1697 data_reply, NULL)
1698 != 0) {
1699 MGMTD_TXN_ERR(
1700 "Failed to send GET-CONFIG-REPLY txn-id: %" PRIu64
1701 " session-id: %" PRIu64 " req-id: %" PRIu64,
1702 txn_req->txn->txn_id, txn_req->txn->session_id,
1703 txn_req->req_id);
1704 }
1705 break;
1706 case MGMTD_TXN_PROC_GETDATA:
1707 if (mgmt_fe_send_get_data_reply(
1708 txn_req->txn->session_id, txn_req->txn->txn_id,
1709 get_req->ds_id, txn_req->req_id, MGMTD_SUCCESS,
1710 data_reply, NULL)
1711 != 0) {
1712 MGMTD_TXN_ERR(
1713 "Failed to send GET-DATA-REPLY txn-id: %" PRIu64
1714 " session-id: %" PRIu64 " req-id: %" PRIu64,
1715 txn_req->txn->txn_id, txn_req->txn->session_id,
1716 txn_req->req_id);
1717 }
1718 break;
1719 case MGMTD_TXN_PROC_SETCFG:
1720 case MGMTD_TXN_PROC_COMMITCFG:
1721 case MGMTD_TXN_COMMITCFG_TIMEOUT:
1722 case MGMTD_TXN_CLEANUP:
1723 MGMTD_TXN_ERR("Invalid Txn-Req-Event %u",
1724 txn_req->req_event);
1725 break;
1726 }
1727
1728 /*
1729 * Reset reply buffer for next reply.
1730 */
1731 mgmt_reset_get_data_reply_buf(get_req);
1732 }
1733
1734 static void mgmt_txn_iter_and_send_get_cfg_reply(struct mgmt_ds_ctx *ds_ctx,
1735 const char *xpath,
1736 struct lyd_node *node,
1737 struct nb_node *nb_node,
1738 void *ctx)
1739 {
1740 struct mgmt_txn_req *txn_req;
1741 struct mgmt_get_data_req *get_req;
1742 struct mgmt_get_data_reply *get_reply;
1743 Mgmtd__YangData *data;
1744 Mgmtd__YangDataValue *data_value;
1745
1746 txn_req = (struct mgmt_txn_req *)ctx;
1747 if (!txn_req)
1748 return;
1749
1750 if (!(node->schema->nodetype & LYD_NODE_TERM))
1751 return;
1752
1753 assert(txn_req->req_event == MGMTD_TXN_PROC_GETCFG
1754 || txn_req->req_event == MGMTD_TXN_PROC_GETDATA);
1755
1756 get_req = txn_req->req.get_data;
1757 assert(get_req);
1758 get_reply = get_req->reply;
1759 data = &get_reply->reply_data[get_reply->num_reply];
1760 data_value = &get_reply->reply_value[get_reply->num_reply];
1761
1762 mgmt_yang_data_init(data);
1763 data->xpath = strdup(xpath);
1764 mgmt_yang_data_value_init(data_value);
1765 data_value->value_case = MGMTD__YANG_DATA_VALUE__VALUE_ENCODED_STR_VAL;
1766 data_value->encoded_str_val = (char *)lyd_get_value(node);
1767 data->value = data_value;
1768
1769 get_reply->num_reply++;
1770 get_req->total_reply++;
1771 MGMTD_TXN_DBG(" [%d] XPATH: '%s', Value: '%s'", get_req->total_reply,
1772 data->xpath, data_value->encoded_str_val);
1773
1774 if (get_reply->num_reply == MGMTD_MAX_NUM_DATA_REPLY_IN_BATCH)
1775 mgmt_txn_send_getcfg_reply_data(txn_req, get_req);
1776 }
1777
1778 static int mgmt_txn_get_config(struct mgmt_txn_ctx *txn,
1779 struct mgmt_txn_req *txn_req,
1780 struct mgmt_ds_ctx *ds_ctx)
1781 {
1782 int indx;
1783 struct mgmt_get_data_req *get_data;
1784 struct mgmt_get_data_reply *get_reply;
1785
1786 get_data = txn_req->req.get_data;
1787
1788 if (!get_data->reply) {
1789 get_data->reply = XCALLOC(MTYPE_MGMTD_TXN_GETDATA_REPLY,
1790 sizeof(struct mgmt_get_data_reply));
1791 if (!get_data->reply) {
1792 mgmt_fe_send_get_cfg_reply(
1793 txn->session_id, txn->txn_id,
1794 get_data->ds_id, txn_req->req_id,
1795 MGMTD_INTERNAL_ERROR, NULL,
1796 "Internal error: Unable to allocate reply buffers!");
1797 goto mgmt_txn_get_config_failed;
1798 }
1799 }
1800
1801 /*
1802 * Read data contents from the DS and respond back directly.
1803 * No need to go to backend for getting data.
1804 */
1805 get_reply = get_data->reply;
1806 for (indx = 0; indx < get_data->num_xpaths; indx++) {
1807 MGMTD_TXN_DBG("Trying to get all data under '%s'",
1808 get_data->xpaths[indx]);
1809 mgmt_init_get_data_reply(get_reply);
1810 /*
1811 * mgmt_ds_iter_data works on path prefixes, but the user may
1812 * want to also use an xpath regexp we need to add this
1813 * functionality.
1814 */
1815 if (mgmt_ds_iter_data(get_data->ds_ctx, get_data->xpaths[indx],
1816 mgmt_txn_iter_and_send_get_cfg_reply,
1817 (void *)txn_req) == -1) {
1818 MGMTD_TXN_DBG("Invalid Xpath '%s",
1819 get_data->xpaths[indx]);
1820 mgmt_fe_send_get_cfg_reply(
1821 txn->session_id, txn->txn_id,
1822 get_data->ds_id, txn_req->req_id,
1823 MGMTD_INTERNAL_ERROR, NULL, "Invalid xpath");
1824 goto mgmt_txn_get_config_failed;
1825 }
1826 MGMTD_TXN_DBG("Got %d remaining data-replies for xpath '%s'",
1827 get_reply->num_reply, get_data->xpaths[indx]);
1828 get_reply->last_batch = true;
1829 mgmt_txn_send_getcfg_reply_data(txn_req, get_data);
1830 }
1831
1832 mgmt_txn_get_config_failed:
1833
1834 /*
1835 * Delete the txn request. It will also remove it from request
1836 * list.
1837 */
1838 mgmt_txn_req_free(&txn_req);
1839
1840 return 0;
1841 }
1842
1843 static void mgmt_txn_process_get_cfg(struct event *thread)
1844 {
1845 struct mgmt_txn_ctx *txn;
1846 struct mgmt_txn_req *txn_req;
1847 struct mgmt_ds_ctx *ds_ctx;
1848 int num_processed = 0;
1849 bool error;
1850
1851 txn = (struct mgmt_txn_ctx *)EVENT_ARG(thread);
1852 assert(txn);
1853
1854 MGMTD_TXN_DBG("Processing %zu GET_CONFIG requests txn-id: %" PRIu64
1855 " session-id: %" PRIu64,
1856 mgmt_txn_reqs_count(&txn->get_cfg_reqs), txn->txn_id,
1857 txn->session_id);
1858
1859 FOREACH_TXN_REQ_IN_LIST (&txn->get_cfg_reqs, txn_req) {
1860 error = false;
1861 assert(txn_req->req_event == MGMTD_TXN_PROC_GETCFG);
1862 ds_ctx = txn_req->req.get_data->ds_ctx;
1863 if (!ds_ctx) {
1864 mgmt_fe_send_get_cfg_reply(
1865 txn->session_id, txn->txn_id,
1866 txn_req->req.get_data->ds_id, txn_req->req_id,
1867 MGMTD_INTERNAL_ERROR, NULL,
1868 "No such datastore!");
1869 error = true;
1870 goto mgmt_txn_process_get_cfg_done;
1871 }
1872
1873 if (mgmt_txn_get_config(txn, txn_req, ds_ctx) != 0) {
1874 MGMTD_TXN_ERR(
1875 "Unable to retrieve config from DS %d txn-id: %" PRIu64
1876 " session-id: %" PRIu64 " req-id: %" PRIu64,
1877 txn_req->req.get_data->ds_id, txn->txn_id,
1878 txn->session_id, txn_req->req_id);
1879 error = true;
1880 }
1881
1882 mgmt_txn_process_get_cfg_done:
1883
1884 if (error) {
1885 /*
1886 * Delete the txn request.
1887 * Note: The following will remove it from the list
1888 * as well.
1889 */
1890 mgmt_txn_req_free(&txn_req);
1891 }
1892
1893 /*
1894 * Else the transaction would have been already deleted or
1895 * moved to corresponding pending list. No need to delete it.
1896 */
1897 num_processed++;
1898 if (num_processed == MGMTD_TXN_MAX_NUM_GETCFG_PROC)
1899 break;
1900 }
1901
1902 if (mgmt_txn_reqs_count(&txn->get_cfg_reqs)) {
1903 MGMTD_TXN_DBG(
1904 "Processed maximum number of Get-Config requests (%d/%d). Rescheduling for rest.",
1905 num_processed, MGMTD_TXN_MAX_NUM_GETCFG_PROC);
1906 mgmt_txn_register_event(txn, MGMTD_TXN_PROC_GETCFG);
1907 }
1908 }
1909
1910 static void mgmt_txn_process_get_data(struct event *thread)
1911 {
1912 struct mgmt_txn_ctx *txn;
1913 struct mgmt_txn_req *txn_req;
1914 struct mgmt_ds_ctx *ds_ctx;
1915 int num_processed = 0;
1916 bool error;
1917
1918 txn = (struct mgmt_txn_ctx *)EVENT_ARG(thread);
1919 assert(txn);
1920
1921 MGMTD_TXN_DBG("Processing %zu GET_DATA requests txn-id: %" PRIu64
1922 " session-id: %" PRIu64,
1923 mgmt_txn_reqs_count(&txn->get_data_reqs), txn->txn_id,
1924 txn->session_id);
1925
1926 FOREACH_TXN_REQ_IN_LIST (&txn->get_data_reqs, txn_req) {
1927 error = false;
1928 assert(txn_req->req_event == MGMTD_TXN_PROC_GETDATA);
1929 ds_ctx = txn_req->req.get_data->ds_ctx;
1930 if (!ds_ctx) {
1931 mgmt_fe_send_get_data_reply(
1932 txn->session_id, txn->txn_id,
1933 txn_req->req.get_data->ds_id, txn_req->req_id,
1934 MGMTD_INTERNAL_ERROR, NULL,
1935 "No such datastore!");
1936 error = true;
1937 goto mgmt_txn_process_get_data_done;
1938 }
1939
1940 if (mgmt_ds_is_config(ds_ctx)) {
1941 if (mgmt_txn_get_config(txn, txn_req, ds_ctx)
1942 != 0) {
1943 MGMTD_TXN_ERR(
1944 "Unable to retrieve config from DS %d txn-id %" PRIu64
1945 " session-id: %" PRIu64
1946 " req-id: %" PRIu64,
1947 txn_req->req.get_data->ds_id,
1948 txn->txn_id, txn->session_id,
1949 txn_req->req_id);
1950 error = true;
1951 }
1952 } else {
1953 /*
1954 * TODO: Trigger GET procedures for Backend
1955 * For now return back error.
1956 */
1957 mgmt_fe_send_get_data_reply(
1958 txn->session_id, txn->txn_id,
1959 txn_req->req.get_data->ds_id, txn_req->req_id,
1960 MGMTD_INTERNAL_ERROR, NULL,
1961 "GET-DATA on Oper DS is not supported yet!");
1962 error = true;
1963 }
1964
1965 mgmt_txn_process_get_data_done:
1966
1967 if (error) {
1968 /*
1969 * Delete the txn request.
1970 * Note: The following will remove it from the list
1971 * as well.
1972 */
1973 mgmt_txn_req_free(&txn_req);
1974 }
1975
1976 /*
1977 * Else the transaction would have been already deleted or
1978 * moved to corresponding pending list. No need to delete it.
1979 */
1980 num_processed++;
1981 if (num_processed == MGMTD_TXN_MAX_NUM_GETDATA_PROC)
1982 break;
1983 }
1984
1985 if (mgmt_txn_reqs_count(&txn->get_data_reqs)) {
1986 MGMTD_TXN_DBG(
1987 "Processed maximum number of Get-Data requests (%d/%d). Rescheduling for rest.",
1988 num_processed, MGMTD_TXN_MAX_NUM_GETDATA_PROC);
1989 mgmt_txn_register_event(txn, MGMTD_TXN_PROC_GETDATA);
1990 }
1991 }
1992
1993 static struct mgmt_txn_ctx *
1994 mgmt_fe_find_txn_by_session_id(struct mgmt_master *cm, uint64_t session_id,
1995 enum mgmt_txn_type type)
1996 {
1997 struct mgmt_txn_ctx *txn;
1998
1999 FOREACH_TXN_IN_LIST (cm, txn) {
2000 if (txn->session_id == session_id && txn->type == type)
2001 return txn;
2002 }
2003
2004 return NULL;
2005 }
2006
2007 static struct mgmt_txn_ctx *mgmt_txn_create_new(uint64_t session_id,
2008 enum mgmt_txn_type type)
2009 {
2010 struct mgmt_txn_ctx *txn = NULL;
2011
2012 /*
2013 * For 'CONFIG' transaction check if one is already created
2014 * or not.
2015 */
2016 if (type == MGMTD_TXN_TYPE_CONFIG && mgmt_txn_mm->cfg_txn) {
2017 if (mgmt_config_txn_in_progress() == session_id)
2018 txn = mgmt_txn_mm->cfg_txn;
2019 goto mgmt_create_txn_done;
2020 }
2021
2022 txn = mgmt_fe_find_txn_by_session_id(mgmt_txn_mm, session_id,
2023 type);
2024 if (!txn) {
2025 txn = XCALLOC(MTYPE_MGMTD_TXN, sizeof(struct mgmt_txn_ctx));
2026 assert(txn);
2027
2028 txn->session_id = session_id;
2029 txn->type = type;
2030 mgmt_txns_add_tail(&mgmt_txn_mm->txn_list, txn);
2031 mgmt_txn_reqs_init(&txn->set_cfg_reqs);
2032 mgmt_txn_reqs_init(&txn->get_cfg_reqs);
2033 mgmt_txn_reqs_init(&txn->get_data_reqs);
2034 mgmt_txn_reqs_init(&txn->pending_get_datas);
2035 txn->commit_cfg_req = NULL;
2036 txn->refcount = 0;
2037 if (!mgmt_txn_mm->next_txn_id)
2038 mgmt_txn_mm->next_txn_id++;
2039 txn->txn_id = mgmt_txn_mm->next_txn_id++;
2040 hash_get(mgmt_txn_mm->txn_hash, txn, hash_alloc_intern);
2041
2042 MGMTD_TXN_DBG("Added new '%s' txn-id: %" PRIu64,
2043 mgmt_txn_type2str(type), txn->txn_id);
2044
2045 if (type == MGMTD_TXN_TYPE_CONFIG)
2046 mgmt_txn_mm->cfg_txn = txn;
2047
2048 MGMTD_TXN_LOCK(txn);
2049 }
2050
2051 mgmt_create_txn_done:
2052 return txn;
2053 }
2054
2055 static void mgmt_txn_delete(struct mgmt_txn_ctx **txn)
2056 {
2057 MGMTD_TXN_UNLOCK(txn);
2058 }
2059
2060 static unsigned int mgmt_txn_hash_key(const void *data)
2061 {
2062 const struct mgmt_txn_ctx *txn = data;
2063
2064 return jhash2((uint32_t *) &txn->txn_id,
2065 sizeof(txn->txn_id) / sizeof(uint32_t), 0);
2066 }
2067
2068 static bool mgmt_txn_hash_cmp(const void *d1, const void *d2)
2069 {
2070 const struct mgmt_txn_ctx *txn1 = d1;
2071 const struct mgmt_txn_ctx *txn2 = d2;
2072
2073 return (txn1->txn_id == txn2->txn_id);
2074 }
2075
2076 static void mgmt_txn_hash_free(void *data)
2077 {
2078 struct mgmt_txn_ctx *txn = data;
2079
2080 mgmt_txn_delete(&txn);
2081 }
2082
2083 static void mgmt_txn_hash_init(void)
2084 {
2085 if (!mgmt_txn_mm || mgmt_txn_mm->txn_hash)
2086 return;
2087
2088 mgmt_txn_mm->txn_hash = hash_create(mgmt_txn_hash_key,
2089 mgmt_txn_hash_cmp,
2090 "MGMT Transactions");
2091 }
2092
2093 static void mgmt_txn_hash_destroy(void)
2094 {
2095 if (!mgmt_txn_mm || !mgmt_txn_mm->txn_hash)
2096 return;
2097
2098 hash_clean(mgmt_txn_mm->txn_hash,
2099 mgmt_txn_hash_free);
2100 hash_free(mgmt_txn_mm->txn_hash);
2101 mgmt_txn_mm->txn_hash = NULL;
2102 }
2103
2104 static inline struct mgmt_txn_ctx *
2105 mgmt_txn_id2ctx(uint64_t txn_id)
2106 {
2107 struct mgmt_txn_ctx key = {0};
2108 struct mgmt_txn_ctx *txn;
2109
2110 if (!mgmt_txn_mm || !mgmt_txn_mm->txn_hash)
2111 return NULL;
2112
2113 key.txn_id = txn_id;
2114 txn = hash_lookup(mgmt_txn_mm->txn_hash, &key);
2115
2116 return txn;
2117 }
2118
2119 static void mgmt_txn_lock(struct mgmt_txn_ctx *txn, const char *file,
2120 int line)
2121 {
2122 txn->refcount++;
2123 MGMTD_TXN_DBG("%s:%d --> Lock %s txn-id: %" PRIu64 " refcnt: %d", file,
2124 line, mgmt_txn_type2str(txn->type), txn->txn_id,
2125 txn->refcount);
2126 }
2127
2128 static void mgmt_txn_unlock(struct mgmt_txn_ctx **txn, const char *file,
2129 int line)
2130 {
2131 assert(*txn && (*txn)->refcount);
2132
2133 (*txn)->refcount--;
2134 MGMTD_TXN_DBG("%s:%d --> Unlock %s txn-id: %" PRIu64 " refcnt: %d",
2135 file, line, mgmt_txn_type2str((*txn)->type),
2136 (*txn)->txn_id, (*txn)->refcount);
2137 if (!(*txn)->refcount) {
2138 if ((*txn)->type == MGMTD_TXN_TYPE_CONFIG)
2139 if (mgmt_txn_mm->cfg_txn == *txn)
2140 mgmt_txn_mm->cfg_txn = NULL;
2141 EVENT_OFF((*txn)->proc_get_cfg);
2142 EVENT_OFF((*txn)->proc_get_data);
2143 EVENT_OFF((*txn)->proc_comm_cfg);
2144 EVENT_OFF((*txn)->comm_cfg_timeout);
2145 hash_release(mgmt_txn_mm->txn_hash, *txn);
2146 mgmt_txns_del(&mgmt_txn_mm->txn_list, *txn);
2147
2148 MGMTD_TXN_DBG("Deleted %s txn-id: %" PRIu64
2149 " session-id: %" PRIu64,
2150 mgmt_txn_type2str((*txn)->type), (*txn)->txn_id,
2151 (*txn)->session_id);
2152
2153 XFREE(MTYPE_MGMTD_TXN, *txn);
2154 }
2155
2156 *txn = NULL;
2157 }
2158
2159 static void mgmt_txn_cleanup_txn(struct mgmt_txn_ctx **txn)
2160 {
2161 /* TODO: Any other cleanup applicable */
2162
2163 mgmt_txn_delete(txn);
2164 }
2165
2166 static void
2167 mgmt_txn_cleanup_all_txns(void)
2168 {
2169 struct mgmt_txn_ctx *txn;
2170
2171 if (!mgmt_txn_mm || !mgmt_txn_mm->txn_hash)
2172 return;
2173
2174 FOREACH_TXN_IN_LIST (mgmt_txn_mm, txn)
2175 mgmt_txn_cleanup_txn(&txn);
2176 }
2177
2178 static void mgmt_txn_cleanup(struct event *thread)
2179 {
2180 struct mgmt_txn_ctx *txn;
2181
2182 txn = (struct mgmt_txn_ctx *)EVENT_ARG(thread);
2183 assert(txn);
2184
2185 mgmt_txn_cleanup_txn(&txn);
2186 }
2187
2188 static void mgmt_txn_register_event(struct mgmt_txn_ctx *txn,
2189 enum mgmt_txn_event event)
2190 {
2191 struct timeval tv = {.tv_sec = 0,
2192 .tv_usec = MGMTD_TXN_PROC_DELAY_USEC};
2193
2194 assert(mgmt_txn_mm && mgmt_txn_tm);
2195
2196 switch (event) {
2197 case MGMTD_TXN_PROC_SETCFG:
2198 event_add_timer_tv(mgmt_txn_tm, mgmt_txn_process_set_cfg,
2199 txn, &tv, &txn->proc_set_cfg);
2200 break;
2201 case MGMTD_TXN_PROC_COMMITCFG:
2202 event_add_timer_tv(mgmt_txn_tm, mgmt_txn_process_commit_cfg,
2203 txn, &tv, &txn->proc_comm_cfg);
2204 break;
2205 case MGMTD_TXN_PROC_GETCFG:
2206 event_add_timer_tv(mgmt_txn_tm, mgmt_txn_process_get_cfg,
2207 txn, &tv, &txn->proc_get_cfg);
2208 break;
2209 case MGMTD_TXN_PROC_GETDATA:
2210 event_add_timer_tv(mgmt_txn_tm, mgmt_txn_process_get_data,
2211 txn, &tv, &txn->proc_get_data);
2212 break;
2213 case MGMTD_TXN_COMMITCFG_TIMEOUT:
2214 event_add_timer_msec(mgmt_txn_tm,
2215 mgmt_txn_cfg_commit_timedout, txn,
2216 MGMTD_TXN_CFG_COMMIT_MAX_DELAY_MSEC,
2217 &txn->comm_cfg_timeout);
2218 break;
2219 case MGMTD_TXN_CLEANUP:
2220 tv.tv_usec = MGMTD_TXN_CLEANUP_DELAY_USEC;
2221 event_add_timer_tv(mgmt_txn_tm, mgmt_txn_cleanup, txn, &tv,
2222 &txn->clnup);
2223 }
2224 }
2225
2226 int mgmt_txn_init(struct mgmt_master *mm, struct event_loop *tm)
2227 {
2228 if (mgmt_txn_mm || mgmt_txn_tm)
2229 assert(!"MGMTD TXN: Call txn_init() only once");
2230
2231 mgmt_txn_mm = mm;
2232 mgmt_txn_tm = tm;
2233 mgmt_txns_init(&mm->txn_list);
2234 mgmt_txn_hash_init();
2235 assert(!mm->cfg_txn);
2236 mm->cfg_txn = NULL;
2237
2238 return 0;
2239 }
2240
2241 void mgmt_txn_destroy(void)
2242 {
2243 mgmt_txn_cleanup_all_txns();
2244 mgmt_txn_hash_destroy();
2245 }
2246
2247 uint64_t mgmt_config_txn_in_progress(void)
2248 {
2249 if (mgmt_txn_mm && mgmt_txn_mm->cfg_txn)
2250 return mgmt_txn_mm->cfg_txn->session_id;
2251
2252 return MGMTD_SESSION_ID_NONE;
2253 }
2254
2255 uint64_t mgmt_create_txn(uint64_t session_id, enum mgmt_txn_type type)
2256 {
2257 struct mgmt_txn_ctx *txn;
2258
2259 txn = mgmt_txn_create_new(session_id, type);
2260 return txn ? txn->txn_id : MGMTD_TXN_ID_NONE;
2261 }
2262
2263 bool mgmt_txn_id_is_valid(uint64_t txn_id)
2264 {
2265 return mgmt_txn_id2ctx(txn_id) ? true : false;
2266 }
2267
2268 void mgmt_destroy_txn(uint64_t *txn_id)
2269 {
2270 struct mgmt_txn_ctx *txn;
2271
2272 txn = mgmt_txn_id2ctx(*txn_id);
2273 if (!txn)
2274 return;
2275
2276 mgmt_txn_delete(&txn);
2277 *txn_id = MGMTD_TXN_ID_NONE;
2278 }
2279
2280 enum mgmt_txn_type mgmt_get_txn_type(uint64_t txn_id)
2281 {
2282 struct mgmt_txn_ctx *txn;
2283
2284 txn = mgmt_txn_id2ctx(txn_id);
2285 if (!txn)
2286 return MGMTD_TXN_TYPE_NONE;
2287
2288 return txn->type;
2289 }
2290
2291 int mgmt_txn_send_set_config_req(uint64_t txn_id, uint64_t req_id,
2292 Mgmtd__DatastoreId ds_id,
2293 struct mgmt_ds_ctx *ds_ctx,
2294 Mgmtd__YangCfgDataReq **cfg_req,
2295 size_t num_req, bool implicit_commit,
2296 Mgmtd__DatastoreId dst_ds_id,
2297 struct mgmt_ds_ctx *dst_ds_ctx)
2298 {
2299 struct mgmt_txn_ctx *txn;
2300 struct mgmt_txn_req *txn_req;
2301 size_t indx;
2302 uint16_t *num_chgs;
2303 struct nb_cfg_change *cfg_chg;
2304
2305 txn = mgmt_txn_id2ctx(txn_id);
2306 if (!txn)
2307 return -1;
2308
2309 if (implicit_commit && mgmt_txn_reqs_count(&txn->set_cfg_reqs)) {
2310 MGMTD_TXN_ERR(
2311 "For implicit commit config only one SETCFG-REQ can be allowed!");
2312 return -1;
2313 }
2314
2315 txn_req = mgmt_txn_req_alloc(txn, req_id, MGMTD_TXN_PROC_SETCFG);
2316 txn_req->req.set_cfg->ds_id = ds_id;
2317 txn_req->req.set_cfg->ds_ctx = ds_ctx;
2318 num_chgs = &txn_req->req.set_cfg->num_cfg_changes;
2319 for (indx = 0; indx < num_req; indx++) {
2320 cfg_chg = &txn_req->req.set_cfg->cfg_changes[*num_chgs];
2321
2322 if (cfg_req[indx]->req_type
2323 == MGMTD__CFG_DATA_REQ_TYPE__DELETE_DATA)
2324 cfg_chg->operation = NB_OP_DESTROY;
2325 else if (cfg_req[indx]->req_type
2326 == MGMTD__CFG_DATA_REQ_TYPE__SET_DATA)
2327 cfg_chg->operation =
2328 mgmt_ds_find_data_node_by_xpath(
2329 ds_ctx, cfg_req[indx]->data->xpath)
2330 ? NB_OP_MODIFY
2331 : NB_OP_CREATE;
2332 else
2333 continue;
2334
2335 MGMTD_TXN_DBG(
2336 "XPath: '%s', Value: '%s'", cfg_req[indx]->data->xpath,
2337 (cfg_req[indx]->data->value
2338 && cfg_req[indx]
2339 ->data->value
2340 ->encoded_str_val
2341 ? cfg_req[indx]->data->value->encoded_str_val
2342 : "NULL"));
2343 strlcpy(cfg_chg->xpath, cfg_req[indx]->data->xpath,
2344 sizeof(cfg_chg->xpath));
2345 cfg_chg->value = (cfg_req[indx]->data->value
2346 && cfg_req[indx]
2347 ->data->value
2348 ->encoded_str_val
2349 ? strdup(cfg_req[indx]
2350 ->data->value
2351 ->encoded_str_val)
2352 : NULL);
2353 if (cfg_chg->value)
2354 MGMTD_TXN_DBG("Allocated value at %p ==> '%s'",
2355 cfg_chg->value, cfg_chg->value);
2356
2357 (*num_chgs)++;
2358 }
2359 txn_req->req.set_cfg->implicit_commit = implicit_commit;
2360 txn_req->req.set_cfg->dst_ds_id = dst_ds_id;
2361 txn_req->req.set_cfg->dst_ds_ctx = dst_ds_ctx;
2362 txn_req->req.set_cfg->setcfg_stats =
2363 mgmt_fe_get_session_setcfg_stats(txn->session_id);
2364 mgmt_txn_register_event(txn, MGMTD_TXN_PROC_SETCFG);
2365
2366 return 0;
2367 }
2368
2369 int mgmt_txn_send_commit_config_req(uint64_t txn_id, uint64_t req_id,
2370 Mgmtd__DatastoreId src_ds_id,
2371 struct mgmt_ds_ctx *src_ds_ctx,
2372 Mgmtd__DatastoreId dst_ds_id,
2373 struct mgmt_ds_ctx *dst_ds_ctx,
2374 bool validate_only, bool abort,
2375 bool implicit)
2376 {
2377 struct mgmt_txn_ctx *txn;
2378 struct mgmt_txn_req *txn_req;
2379
2380 txn = mgmt_txn_id2ctx(txn_id);
2381 if (!txn)
2382 return -1;
2383
2384 if (txn->commit_cfg_req) {
2385 MGMTD_TXN_ERR("Commit already in-progress txn-id: %" PRIu64
2386 " session-id: %" PRIu64 ". Cannot start another",
2387 txn->txn_id, txn->session_id);
2388 return -1;
2389 }
2390
2391 txn_req = mgmt_txn_req_alloc(txn, req_id, MGMTD_TXN_PROC_COMMITCFG);
2392 txn_req->req.commit_cfg.src_ds_id = src_ds_id;
2393 txn_req->req.commit_cfg.src_ds_ctx = src_ds_ctx;
2394 txn_req->req.commit_cfg.dst_ds_id = dst_ds_id;
2395 txn_req->req.commit_cfg.dst_ds_ctx = dst_ds_ctx;
2396 txn_req->req.commit_cfg.validate_only = validate_only;
2397 txn_req->req.commit_cfg.abort = abort;
2398 txn_req->req.commit_cfg.implicit = implicit;
2399 txn_req->req.commit_cfg.cmt_stats =
2400 mgmt_fe_get_session_commit_stats(txn->session_id);
2401
2402 /*
2403 * Trigger a COMMIT-CONFIG process.
2404 */
2405 mgmt_txn_register_event(txn, MGMTD_TXN_PROC_COMMITCFG);
2406 return 0;
2407 }
2408
2409 int mgmt_txn_notify_be_adapter_conn(struct mgmt_be_client_adapter *adapter,
2410 bool connect)
2411 {
2412 struct mgmt_txn_ctx *txn;
2413 struct mgmt_txn_req *txn_req;
2414 struct mgmt_commit_cfg_req *cmtcfg_req;
2415 static struct mgmt_commit_stats dummy_stats;
2416 struct nb_config_cbs *adapter_cfgs = NULL;
2417
2418 memset(&dummy_stats, 0, sizeof(dummy_stats));
2419 if (connect) {
2420 /* Get config for this single backend client */
2421 mgmt_be_get_adapter_config(adapter, mm->running_ds,
2422 &adapter_cfgs);
2423
2424 if (!adapter_cfgs || RB_EMPTY(nb_config_cbs, adapter_cfgs)) {
2425 SET_FLAG(adapter->flags,
2426 MGMTD_BE_ADAPTER_FLAGS_CFG_SYNCED);
2427 return 0;
2428 }
2429
2430 /*
2431 * Create a CONFIG transaction to push the config changes
2432 * provided to the backend client.
2433 */
2434 txn = mgmt_txn_create_new(0, MGMTD_TXN_TYPE_CONFIG);
2435 if (!txn) {
2436 MGMTD_TXN_ERR(
2437 "Failed to create CONFIG Transaction for downloading CONFIGs for client '%s'",
2438 adapter->name);
2439 return -1;
2440 }
2441
2442 MGMTD_TXN_DBG("Created initial txn-id: %" PRIu64
2443 " for BE client '%s'",
2444 txn->txn_id, adapter->name);
2445 /*
2446 * Set the changeset for transaction to commit and trigger the
2447 * commit request.
2448 */
2449 txn_req =
2450 mgmt_txn_req_alloc(txn, 0, MGMTD_TXN_PROC_COMMITCFG);
2451 txn_req->req.commit_cfg.src_ds_id = MGMTD_DS_NONE;
2452 txn_req->req.commit_cfg.src_ds_ctx = 0;
2453 txn_req->req.commit_cfg.dst_ds_id = MGMTD_DS_NONE;
2454 txn_req->req.commit_cfg.dst_ds_ctx = 0;
2455 txn_req->req.commit_cfg.validate_only = false;
2456 txn_req->req.commit_cfg.abort = false;
2457 txn_req->req.commit_cfg.cmt_stats = &dummy_stats;
2458 txn_req->req.commit_cfg.cfg_chgs = adapter_cfgs;
2459
2460 /*
2461 * Trigger a COMMIT-CONFIG process.
2462 */
2463 mgmt_txn_register_event(txn, MGMTD_TXN_PROC_COMMITCFG);
2464
2465 } else {
2466 /*
2467 * Check if any transaction is currently on-going that
2468 * involves this backend client. If so, report the transaction
2469 * has failed.
2470 */
2471 FOREACH_TXN_IN_LIST (mgmt_txn_mm, txn) {
2472 /* TODO: update with operational state when that is
2473 * completed */
2474 if (txn->type == MGMTD_TXN_TYPE_CONFIG) {
2475 cmtcfg_req = txn->commit_cfg_req
2476 ? &txn->commit_cfg_req
2477 ->req.commit_cfg
2478 : NULL;
2479 if (cmtcfg_req &&
2480 cmtcfg_req->subscr_info
2481 .xpath_subscr[adapter->id]) {
2482 mgmt_txn_send_commit_cfg_reply(
2483 txn, MGMTD_INTERNAL_ERROR,
2484 "Backend daemon disconnected while processing commit!");
2485 }
2486 }
2487 }
2488 }
2489
2490 return 0;
2491 }
2492
2493 int mgmt_txn_notify_be_txn_reply(uint64_t txn_id, bool create,
2494 bool success,
2495 struct mgmt_be_client_adapter *adapter)
2496 {
2497 struct mgmt_txn_ctx *txn;
2498 struct mgmt_commit_cfg_req *cmtcfg_req = NULL;
2499
2500 txn = mgmt_txn_id2ctx(txn_id);
2501 if (!txn || txn->type != MGMTD_TXN_TYPE_CONFIG)
2502 return -1;
2503
2504 if (!create && !txn->commit_cfg_req)
2505 return 0;
2506
2507 assert(txn->commit_cfg_req);
2508 cmtcfg_req = &txn->commit_cfg_req->req.commit_cfg;
2509 if (create) {
2510 if (success) {
2511 /*
2512 * Done with TXN_CREATE. Move the backend client to
2513 * next phase.
2514 */
2515 assert(cmtcfg_req->curr_phase
2516 == MGMTD_COMMIT_PHASE_TXN_CREATE);
2517
2518 /*
2519 * Send CFGDATA_CREATE-REQs to the backend immediately.
2520 */
2521 mgmt_txn_send_be_cfg_data(txn, adapter);
2522 } else {
2523 mgmt_txn_send_commit_cfg_reply(
2524 txn, MGMTD_INTERNAL_ERROR,
2525 "Internal error! Failed to initiate transaction at backend!");
2526 }
2527 } else {
2528 /*
2529 * Done with TXN_DELETE. Move the backend client to next phase.
2530 */
2531 if (false)
2532 mgmt_move_be_commit_to_next_phase(txn, adapter);
2533 }
2534
2535 return 0;
2536 }
2537
2538 int mgmt_txn_notify_be_cfgdata_reply(
2539 uint64_t txn_id, uint64_t batch_id, bool success, char *error_if_any,
2540 struct mgmt_be_client_adapter *adapter)
2541 {
2542 struct mgmt_txn_ctx *txn;
2543 struct mgmt_txn_be_cfg_batch *cfg_btch;
2544 struct mgmt_commit_cfg_req *cmtcfg_req;
2545
2546 txn = mgmt_txn_id2ctx(txn_id);
2547 if (!txn || txn->type != MGMTD_TXN_TYPE_CONFIG)
2548 return -1;
2549
2550 if (!txn->commit_cfg_req)
2551 return -1;
2552 cmtcfg_req = &txn->commit_cfg_req->req.commit_cfg;
2553
2554 cfg_btch = mgmt_txn_cfgbatch_id2ctx(txn, batch_id);
2555 if (!cfg_btch || cfg_btch->txn != txn)
2556 return -1;
2557
2558 if (!success) {
2559 MGMTD_TXN_ERR(
2560 "CFGDATA_CREATE_REQ sent to '%s' failed txn-id: %" PRIu64
2561 " batch-id %" PRIu64 " err: %s",
2562 adapter->name, txn->txn_id, cfg_btch->batch_id,
2563 error_if_any ? error_if_any : "None");
2564 mgmt_txn_send_commit_cfg_reply(
2565 txn, MGMTD_INTERNAL_ERROR,
2566 error_if_any ? error_if_any :
2567 "Internal error! Failed to download config data to backend!");
2568 return 0;
2569 }
2570
2571 MGMTD_TXN_DBG(
2572 "CFGDATA_CREATE_REQ sent to '%s' was successful txn-id: %" PRIu64
2573 " batch-id %" PRIu64 " err: %s",
2574 adapter->name, txn->txn_id, cfg_btch->batch_id,
2575 error_if_any ? error_if_any : "None");
2576 mgmt_move_txn_cfg_batch_to_next(
2577 cmtcfg_req, cfg_btch, &cmtcfg_req->curr_batches[adapter->id],
2578 &cmtcfg_req->next_batches[adapter->id], true,
2579 MGMTD_COMMIT_PHASE_APPLY_CFG);
2580
2581 mgmt_try_move_commit_to_next_phase(txn, cmtcfg_req);
2582
2583 return 0;
2584 }
2585
2586 int mgmt_txn_notify_be_cfg_apply_reply(uint64_t txn_id, bool success,
2587 uint64_t batch_ids[],
2588 size_t num_batch_ids, char *error_if_any,
2589 struct mgmt_be_client_adapter *adapter)
2590 {
2591 struct mgmt_txn_ctx *txn;
2592 struct mgmt_txn_be_cfg_batch *cfg_btch;
2593 struct mgmt_commit_cfg_req *cmtcfg_req = NULL;
2594 size_t indx;
2595
2596 txn = mgmt_txn_id2ctx(txn_id);
2597 if (!txn || txn->type != MGMTD_TXN_TYPE_CONFIG
2598 || !txn->commit_cfg_req)
2599 return -1;
2600
2601 cmtcfg_req = &txn->commit_cfg_req->req.commit_cfg;
2602
2603 if (!success) {
2604 MGMTD_TXN_ERR(
2605 "CFGDATA_APPLY_REQ sent to '%s' failed txn-id: %" PRIu64
2606 " batch ids %" PRIu64 " - %" PRIu64 " err: %s",
2607 adapter->name, txn->txn_id, batch_ids[0],
2608 batch_ids[num_batch_ids - 1],
2609 error_if_any ? error_if_any : "None");
2610 mgmt_txn_send_commit_cfg_reply(
2611 txn, MGMTD_INTERNAL_ERROR,
2612 error_if_any ? error_if_any :
2613 "Internal error! Failed to apply config data on backend!");
2614 return 0;
2615 }
2616
2617 for (indx = 0; indx < num_batch_ids; indx++) {
2618 cfg_btch = mgmt_txn_cfgbatch_id2ctx(txn, batch_ids[indx]);
2619 if (cfg_btch->txn != txn)
2620 return -1;
2621 mgmt_move_txn_cfg_batch_to_next(
2622 cmtcfg_req, cfg_btch,
2623 &cmtcfg_req->curr_batches[adapter->id],
2624 &cmtcfg_req->next_batches[adapter->id], true,
2625 MGMTD_COMMIT_PHASE_TXN_DELETE);
2626 }
2627
2628 if (!mgmt_txn_batches_count(&cmtcfg_req->curr_batches[adapter->id])) {
2629 /*
2630 * All configuration for the specific backend has been applied.
2631 * Send TXN-DELETE to wrap up the transaction for this backend.
2632 */
2633 SET_FLAG(adapter->flags, MGMTD_BE_ADAPTER_FLAGS_CFG_SYNCED);
2634 mgmt_txn_send_be_txn_delete(txn, adapter);
2635 }
2636
2637 mgmt_try_move_commit_to_next_phase(txn, cmtcfg_req);
2638 if (mm->perf_stats_en)
2639 gettimeofday(&cmtcfg_req->cmt_stats->apply_cfg_end, NULL);
2640
2641 return 0;
2642 }
2643
2644 int mgmt_txn_send_commit_config_reply(uint64_t txn_id,
2645 enum mgmt_result result,
2646 const char *error_if_any)
2647 {
2648 struct mgmt_txn_ctx *txn;
2649
2650 txn = mgmt_txn_id2ctx(txn_id);
2651 if (!txn)
2652 return -1;
2653
2654 if (!txn->commit_cfg_req) {
2655 MGMTD_TXN_ERR("NO commit in-progress txn-id: %" PRIu64
2656 " session-id: %" PRIu64,
2657 txn->txn_id, txn->session_id);
2658 return -1;
2659 }
2660
2661 return mgmt_txn_send_commit_cfg_reply(txn, result, error_if_any);
2662 }
2663
2664 int mgmt_txn_send_get_config_req(uint64_t txn_id, uint64_t req_id,
2665 Mgmtd__DatastoreId ds_id,
2666 struct mgmt_ds_ctx *ds_ctx,
2667 Mgmtd__YangGetDataReq **data_req,
2668 size_t num_reqs)
2669 {
2670 struct mgmt_txn_ctx *txn;
2671 struct mgmt_txn_req *txn_req;
2672 size_t indx;
2673
2674 txn = mgmt_txn_id2ctx(txn_id);
2675 if (!txn)
2676 return -1;
2677
2678 txn_req = mgmt_txn_req_alloc(txn, req_id, MGMTD_TXN_PROC_GETCFG);
2679 txn_req->req.get_data->ds_id = ds_id;
2680 txn_req->req.get_data->ds_ctx = ds_ctx;
2681 for (indx = 0;
2682 indx < num_reqs && indx < MGMTD_MAX_NUM_DATA_REPLY_IN_BATCH;
2683 indx++) {
2684 MGMTD_TXN_DBG("XPath: '%s'", data_req[indx]->data->xpath);
2685 txn_req->req.get_data->xpaths[indx] =
2686 strdup(data_req[indx]->data->xpath);
2687 txn_req->req.get_data->num_xpaths++;
2688 }
2689
2690 mgmt_txn_register_event(txn, MGMTD_TXN_PROC_GETCFG);
2691
2692 return 0;
2693 }
2694
2695 int mgmt_txn_send_get_data_req(uint64_t txn_id, uint64_t req_id,
2696 Mgmtd__DatastoreId ds_id,
2697 struct mgmt_ds_ctx *ds_ctx,
2698 Mgmtd__YangGetDataReq **data_req,
2699 size_t num_reqs)
2700 {
2701 struct mgmt_txn_ctx *txn;
2702 struct mgmt_txn_req *txn_req;
2703 size_t indx;
2704
2705 txn = mgmt_txn_id2ctx(txn_id);
2706 if (!txn)
2707 return -1;
2708
2709 txn_req = mgmt_txn_req_alloc(txn, req_id, MGMTD_TXN_PROC_GETDATA);
2710 txn_req->req.get_data->ds_id = ds_id;
2711 txn_req->req.get_data->ds_ctx = ds_ctx;
2712 for (indx = 0;
2713 indx < num_reqs && indx < MGMTD_MAX_NUM_DATA_REPLY_IN_BATCH;
2714 indx++) {
2715 MGMTD_TXN_DBG("XPath: '%s'", data_req[indx]->data->xpath);
2716 txn_req->req.get_data->xpaths[indx] =
2717 strdup(data_req[indx]->data->xpath);
2718 txn_req->req.get_data->num_xpaths++;
2719 }
2720
2721 mgmt_txn_register_event(txn, MGMTD_TXN_PROC_GETDATA);
2722
2723 return 0;
2724 }
2725
2726 void mgmt_txn_status_write(struct vty *vty)
2727 {
2728 struct mgmt_txn_ctx *txn;
2729
2730 vty_out(vty, "MGMTD Transactions\n");
2731
2732 FOREACH_TXN_IN_LIST (mgmt_txn_mm, txn) {
2733 vty_out(vty, " Txn: \t\t\t0x%p\n", txn);
2734 vty_out(vty, " Txn-Id: \t\t\t%" PRIu64 "\n", txn->txn_id);
2735 vty_out(vty, " Session-Id: \t\t%" PRIu64 "\n",
2736 txn->session_id);
2737 vty_out(vty, " Type: \t\t\t%s\n",
2738 mgmt_txn_type2str(txn->type));
2739 vty_out(vty, " Ref-Count: \t\t\t%d\n", txn->refcount);
2740 }
2741 vty_out(vty, " Total: %d\n",
2742 (int)mgmt_txns_count(&mgmt_txn_mm->txn_list));
2743 }
2744
2745 int mgmt_txn_rollback_trigger_cfg_apply(struct mgmt_ds_ctx *src_ds_ctx,
2746 struct mgmt_ds_ctx *dst_ds_ctx)
2747 {
2748 static struct nb_config_cbs changes;
2749 struct nb_config_cbs *cfg_chgs = NULL;
2750 struct mgmt_txn_ctx *txn;
2751 struct mgmt_txn_req *txn_req;
2752 static struct mgmt_commit_stats dummy_stats;
2753
2754 memset(&changes, 0, sizeof(changes));
2755 memset(&dummy_stats, 0, sizeof(dummy_stats));
2756 /*
2757 * This could be the case when the config is directly
2758 * loaded onto the candidate DS from a file. Get the
2759 * diff from a full comparison of the candidate and
2760 * running DSs.
2761 */
2762 nb_config_diff(mgmt_ds_get_nb_config(dst_ds_ctx),
2763 mgmt_ds_get_nb_config(src_ds_ctx), &changes);
2764 cfg_chgs = &changes;
2765
2766 if (RB_EMPTY(nb_config_cbs, cfg_chgs)) {
2767 /*
2768 * This means there's no changes to commit whatsoever
2769 * is the source of the changes in config.
2770 */
2771 return -1;
2772 }
2773
2774 /*
2775 * Create a CONFIG transaction to push the config changes
2776 * provided to the backend client.
2777 */
2778 txn = mgmt_txn_create_new(0, MGMTD_TXN_TYPE_CONFIG);
2779 if (!txn) {
2780 MGMTD_TXN_ERR(
2781 "Failed to create CONFIG Transaction for downloading CONFIGs");
2782 return -1;
2783 }
2784
2785 MGMTD_TXN_DBG("Created rollback txn-id: %" PRIu64, txn->txn_id);
2786
2787 /*
2788 * Set the changeset for transaction to commit and trigger the commit
2789 * request.
2790 */
2791 txn_req = mgmt_txn_req_alloc(txn, 0, MGMTD_TXN_PROC_COMMITCFG);
2792 txn_req->req.commit_cfg.src_ds_id = MGMTD_DS_CANDIDATE;
2793 txn_req->req.commit_cfg.src_ds_ctx = src_ds_ctx;
2794 txn_req->req.commit_cfg.dst_ds_id = MGMTD_DS_RUNNING;
2795 txn_req->req.commit_cfg.dst_ds_ctx = dst_ds_ctx;
2796 txn_req->req.commit_cfg.validate_only = false;
2797 txn_req->req.commit_cfg.abort = false;
2798 txn_req->req.commit_cfg.rollback = true;
2799 txn_req->req.commit_cfg.cmt_stats = &dummy_stats;
2800 txn_req->req.commit_cfg.cfg_chgs = cfg_chgs;
2801
2802 /*
2803 * Trigger a COMMIT-CONFIG process.
2804 */
2805 mgmt_txn_register_event(txn, MGMTD_TXN_PROC_COMMITCFG);
2806 return 0;
2807 }