-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathofconfig-datastore.c
620 lines (542 loc) · 16.7 KB
/
ofconfig-datastore.c
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/* Copyright (c) 2015 Open Networking Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* TODO:
* - NACM
*/
#define _GNU_SOURCE
#include <config.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/types.h>
#include <libnetconf.h>
/* libovs */
#include <dirs.h>
#include "data.h"
#include "edit-config.c"
#define XML_READ_OPT XML_PARSE_NOBLANKS|XML_PARSE_NSCLEAN
/* daemonize flag from server.c */
extern int ofc_daemonize;
/* OVSDB socket path shared with server.c */
char *ovsdb_path = NULL;
/* The last valid running for rollback */
struct rollback_s {
xmlDocPtr doc;
NC_DATASTORE type;
};
static struct rollback_s rollback = { NULL, NC_DATASTORE_ERROR };
volatile static int rollbacking = 0;
/* local locks info */
struct {
int running;
char *running_sid;
int startup;
char *startup_sid;
int cand;
char *cand_sid;
} locks = {0, NULL, 0, NULL, 0, NULL};
/* localy maintained datastores */
xmlDocPtr gds_startup = NULL;
xmlDocPtr gds_cand = NULL;
int ofcds_deleteconfig(void *UNUSED(data), NC_DATASTORE UNUSED(target),
struct nc_err **UNUSED(error));
int
ofcds_init(void *UNUSED(data))
{
if (!ovsdb_path) {
/* default path */
asprintf(&ovsdb_path, "unix:%s/db.sock", ovs_rundir());
}
if (ofc_init(ovsdb_path) == false) {
return EXIT_FAILURE;
}
/* hack - OVS calls openlog() and rewrites the syslog settings of the
* ofc-server. So we have to rewrite syslog settings back by another
* openlog() call */
if (ofc_daemonize) {
openlog("ofc-server", LOG_PID, LOG_DAEMON);
} else {
openlog("ofc-server", LOG_PID | LOG_PERROR, LOG_DAEMON);
}
/* get startup data */
gds_startup = xmlReadFile(OFC_DATADIR "/startup.xml", NULL, XML_READ_OPT);
/* check that there are some data, if not, continue with empty startup */
if (!xmlDocGetRootElement(gds_startup)) {
xmlFreeDoc(gds_startup);
gds_startup = NULL;
}
nc_verb_verbose("OF-CONFIG datastore initialized.");
return EXIT_SUCCESS;
}
void
ofcds_free(void *UNUSED(data))
{
ofc_destroy();
/* dump startup to persistent storage */
if (gds_startup) {
xmlSaveFormatFile(OFC_DATADIR "/startup.xml", gds_startup, 1);
} else {
/* erase the file */
truncate(OFC_DATADIR "/startup.xml", 0);
}
/* cleanup locks */
free(locks.running_sid);
free(locks.startup_sid);
free(locks.cand_sid);
if (rollback.doc) {
xmlFreeDoc(rollback.doc);
rollback.doc = NULL;
}
return;
}
static void
store_rollback(const xmlDocPtr doc, NC_DATASTORE type)
{
if (rollback.doc) {
xmlFreeDoc(rollback.doc);
}
rollback.doc = doc;
rollback.type = type;
}
int
ofcds_changed(void *UNUSED(data))
{
/* always false the function is not needed now, we can implement it later
* for internal purposes, but for now the datastore content is synced
* continuously */
return (0);
}
int
ofcds_lock(void *UNUSED(data), NC_DATASTORE target, const char *session_id,
struct nc_err **error)
{
int *locked;
char **sid;
switch (target) {
case NC_DATASTORE_RUNNING:
locked = &(locks.running);
sid = &(locks.running_sid);
break;
case NC_DATASTORE_STARTUP:
locked = &(locks.startup);
sid = &(locks.startup_sid);
break;
case NC_DATASTORE_CANDIDATE:
locked = &(locks.cand);
sid = &(locks.cand_sid);
break;
default:
/* handled by libnetconf */
return EXIT_FAILURE;
}
if (*locked) {
/* datastore is already locked */
*error = nc_err_new(NC_ERR_LOCK_DENIED);
nc_err_set(*error, NC_ERR_PARAM_INFO_SID, *sid);
return EXIT_FAILURE;
} else {
/* remember the lock */
*locked = 1;
*sid = strdup(session_id);
nc_verb_verbose("OFC datastore %d locked by %s.", target, session_id);
}
return EXIT_SUCCESS;
}
int
ofcds_unlock(void *UNUSED(data), NC_DATASTORE target, const char *session_id,
struct nc_err **error)
{
int *locked;
char **sid;
switch (target) {
case NC_DATASTORE_RUNNING:
locked = &(locks.running);
sid = &(locks.running_sid);
break;
case NC_DATASTORE_STARTUP:
locked = &(locks.startup);
sid = &(locks.startup_sid);
break;
case NC_DATASTORE_CANDIDATE:
locked = &(locks.cand);
sid = &(locks.cand_sid);
break;
default:
/* handled by libnetconf */
return EXIT_FAILURE;
}
if (*locked) {
if (strcmp(*sid, session_id) == 0) {
/* correct request, unlock */
*locked = 0;
free(*sid);
*sid = NULL;
nc_verb_verbose("OFC datastore %d unlocked by %s.", target,
session_id);
} else {
/* locked by another session */
*error = nc_err_new(NC_ERR_LOCK_DENIED);
nc_err_set(*error, NC_ERR_PARAM_INFO_SID, *sid);
nc_err_set(*error, NC_ERR_PARAM_MSG,
"Target datastore is locked by another session.");
return EXIT_FAILURE;
}
} else {
/* not locked */
*error = nc_err_new(NC_ERR_OP_FAILED);
nc_err_set(*error, NC_ERR_PARAM_MSG,
"Target datastore is not locked.");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
char *
ofcds_getconfig(void *UNUSED(data), NC_DATASTORE target, struct nc_err **error)
{
xmlChar *config_data = NULL;
switch (target) {
case NC_DATASTORE_RUNNING:
/* If there is no id of the capable-switch (no configuration data were
* provided), continue as there is no OVSDB */
return ofc_get_config_data();
case NC_DATASTORE_STARTUP:
if (!gds_startup) {
config_data = xmlStrdup(BAD_CAST "");
} else {
xmlDocDumpMemory(gds_startup, &config_data, NULL);
}
break;
case NC_DATASTORE_CANDIDATE:
if (!gds_cand) {
config_data = xmlStrdup(BAD_CAST "");
} else {
xmlDocDumpMemory(gds_cand, &config_data, NULL);
}
break;
default:
nc_verb_error("Invalid <get-config> source.");
*error = nc_err_new(NC_ERR_BAD_ELEM);
nc_err_set(*error, NC_ERR_PARAM_INFO_BADELEM, "source");
}
return (char *) config_data;
}
int
ofcds_deleteconfig(void *UNUSED(data), NC_DATASTORE target,
struct nc_err **error)
{
switch (target) {
case NC_DATASTORE_RUNNING:
*error = nc_err_new(NC_ERR_OP_FAILED);
nc_err_set(*error, NC_ERR_PARAM_MSG,
"Cannot delete a running datastore.");
return EXIT_FAILURE;
case NC_DATASTORE_STARTUP:
store_rollback(gds_startup, NC_DATASTORE_STARTUP);
gds_startup = NULL;
break;
case NC_DATASTORE_CANDIDATE:
store_rollback(gds_cand, NC_DATASTORE_CANDIDATE);
gds_cand = NULL;
break;
default:
nc_verb_error("Invalid <delete-config> target.");
*error = nc_err_new(NC_ERR_BAD_ELEM);
nc_err_set(*error, NC_ERR_PARAM_INFO_BADELEM, "target");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int
ofcds_editconfig(void *UNUSED(data), const nc_rpc * UNUSED(rpc),
NC_DATASTORE target, const char *config,
NC_EDIT_DEFOP_TYPE defop,
NC_EDIT_ERROPT_TYPE UNUSED(errop), struct nc_err **error)
{
int ret = EXIT_FAILURE, running = 0;
char *aux;
int cfgds_new = 0;
xmlDocPtr cfgds = NULL, cfg = NULL, cfg_clone = NULL;
xmlNodePtr rootcfg;
if (defop == NC_EDIT_DEFOP_NOTSET) {
defop = NC_EDIT_DEFOP_MERGE;
}
cfg = xmlReadMemory(config, strlen(config), NULL, NULL, XML_READ_OPT);
rootcfg = xmlDocGetRootElement(cfg);
if (!cfg
|| (rootcfg
&& !xmlStrEqual(rootcfg->name, BAD_CAST "capable-switch"))) {
nc_verb_error("Invalid <edit-config> configuration data.");
*error = nc_err_new(NC_ERR_BAD_ELEM);
nc_err_set(*error, NC_ERR_PARAM_INFO_BADELEM, "config");
return EXIT_FAILURE;
}
switch (target) {
case NC_DATASTORE_RUNNING:
/* Make a copy of parsed config - we will find port/configuration in
* it. It is used after txn_commit(). */
cfg_clone = xmlCopyDoc(cfg, 1);
aux = ofc_get_config_data();
if (!aux) {
*error = nc_err_new(NC_ERR_OP_FAILED);
goto error_cleanup;
}
cfgds = xmlReadMemory(aux, strlen(aux), NULL, NULL, XML_READ_OPT);
free(aux);
running = 1;
break;
case NC_DATASTORE_STARTUP:
cfgds = gds_startup;
break;
case NC_DATASTORE_CANDIDATE:
cfgds = gds_cand;
break;
default:
nc_verb_error("Invalid <edit-config> target.");
*error = nc_err_new(NC_ERR_BAD_ELEM);
nc_err_set(*error, NC_ERR_PARAM_INFO_BADELEM, "target");
goto error_cleanup;
}
store_rollback(xmlCopyDoc(cfgds, 1), target);
/* check keys in config's lists */
ret = check_keys(cfg, error);
if (ret != EXIT_SUCCESS) {
goto error_cleanup;
}
/* check operations */
ret = check_edit_ops(NC_EDIT_OP_DELETE, defop, cfgds, cfg, error);
if (ret != EXIT_SUCCESS) {
goto error_cleanup;
}
ret = check_edit_ops(NC_EDIT_OP_CREATE, defop, cfgds, cfg, error);
if (ret != EXIT_SUCCESS) {
goto error_cleanup;
}
if (target == NC_DATASTORE_RUNNING) {
txn_init();
}
ret = compact_edit_operations(cfg, defop);
if (ret != EXIT_SUCCESS) {
nc_verb_error("Compacting edit-config operations failed.");
if (error != NULL) {
*error = nc_err_new(NC_ERR_OP_FAILED);
}
goto error_cleanup;
}
/* perform operations */
if (!cfgds) {
cfgds_new = 1;
cfgds = xmlNewDoc(BAD_CAST "1.0");
}
ret = edit_operations(cfgds, cfg, defop, running, error);
if (ret != EXIT_SUCCESS) {
goto error_cleanup;
}
/* with defaults capability */
if (ncdflt_get_basic_mode() == NCWD_MODE_TRIM) {
/* server work in trim basic mode and therefore all default values
* must be removed from the datastore. */
/* TODO */
}
if (target == NC_DATASTORE_RUNNING) {
ret = txn_commit(error);
if (ret == EXIT_SUCCESS) {
/* modify port/configuration of ports that were created */
ret = of_post_ports(xmlDocGetRootElement(cfg_clone), error);
}
/* config clone was used and it is not needed by now */
xmlFreeDoc(cfg_clone);
xmlFreeDoc(cfgds);
} else if (cfgds_new){
if (cfgds->children) {
/* document changed, because we started with empty document */
if (target == NC_DATASTORE_STARTUP) {
gds_startup = cfgds;
cfgds = NULL;
} else if (target == NC_DATASTORE_CANDIDATE) {
gds_cand = cfgds;
cfgds = NULL;
}
}
xmlFreeDoc(cfgds);
}
xmlFreeDoc(cfg);
return ret;
error_cleanup:
if (target == NC_DATASTORE_RUNNING) {
txn_abort();
xmlFreeDoc(cfg_clone);
xmlFreeDoc(cfgds);
}
xmlFreeDoc(cfg);
return ret;
}
int
ofcds_copyconfig(void *UNUSED(data), NC_DATASTORE target,
NC_DATASTORE source, char *config, struct nc_err **error)
{
int ret = EXIT_FAILURE;
char *s;
xmlDocPtr src_doc = NULL;
xmlDocPtr dst_doc = NULL;
xmlNodePtr root;
static const char *ds[] = {"error", "<config>", "URL", "running",
"startup", "candidate"};
nc_verb_verbose("OFC COPY-CONFIG (from %s to %s)", ds[source], ds[target]);
switch (source) {
case NC_DATASTORE_RUNNING:
s = ofcds_getconfig(NULL, NC_DATASTORE_RUNNING, error);
if (!s) {
nc_verb_error
("copy-config: unable to get running source repository");
return EXIT_FAILURE;
}
src_doc = xmlReadMemory(s, strlen(s), NULL, NULL, XML_READ_OPT);
free(s);
if (!src_doc) {
nc_verb_error("copy-config: invalid running source data");
*error = nc_err_new(NC_ERR_OP_FAILED);
nc_err_set(*error, NC_ERR_PARAM_INFO_BADELEM,
"invalid running source data");
return EXIT_FAILURE;
}
break;
case NC_DATASTORE_STARTUP:
src_doc = xmlCopyDoc(gds_startup, 1);
break;
case NC_DATASTORE_CANDIDATE:
src_doc = xmlCopyDoc(gds_cand, 1);
break;
case NC_DATASTORE_CONFIG:
if (config && strlen(config) > 0) {
src_doc = xmlReadMemory(config, strlen(config), NULL, NULL,
XML_READ_OPT);
}
if (!config || (strlen(config) > 0 && !src_doc)) {
nc_verb_error("Invalid source configuration data.");
*error = nc_err_new(NC_ERR_BAD_ELEM);
nc_err_set(*error, NC_ERR_PARAM_INFO_BADELEM, "config");
return EXIT_FAILURE;
}
break;
default:
nc_verb_error("Invalid <get-config> source.");
*error = nc_err_new(NC_ERR_BAD_ELEM);
nc_err_set(*error, NC_ERR_PARAM_INFO_BADELEM, "source");
return EXIT_FAILURE;
}
switch (target) {
case NC_DATASTORE_RUNNING:
/* apply source to OVSDB */
s = ofcds_getconfig(NULL, NC_DATASTORE_RUNNING, error);
if (!s) {
nc_verb_error("copy-config: unable to get running source data");
goto cleanup;
}
dst_doc = xmlReadMemory(s, strlen(s), NULL, NULL, XML_READ_OPT);
free(s);
root = xmlDocGetRootElement(src_doc);
if (!dst_doc) {
/* create envelope */
dst_doc = xmlNewDoc(BAD_CAST "1.0");
}
if (!rollbacking) {
store_rollback(xmlCopyDoc(dst_doc, 1), target);
}
txn_init();
if (edit_replace(dst_doc, root, 1, error)) {
txn_abort();
} else {
ret = txn_commit(error);
}
xmlFreeDoc(dst_doc);
goto cleanup;
break;
case NC_DATASTORE_STARTUP:
case NC_DATASTORE_CANDIDATE:
/* create copy */
if (src_doc) {
dst_doc = src_doc;
src_doc = NULL;
}
/* store the copy */
if (target == NC_DATASTORE_STARTUP) {
if (!rollbacking) {
store_rollback(gds_startup, target);
} else {
xmlFreeDoc(gds_startup);
}
gds_startup = dst_doc;
} else { /* NC_DATASTORE_CANDIDATE */
if (!rollbacking) {
store_rollback(gds_cand, target);
} else {
xmlFreeDoc(gds_cand);
}
gds_cand = dst_doc;
}
break;
default:
nc_verb_error("Invalid <get-config> source.");
*error = nc_err_new(NC_ERR_BAD_ELEM);
nc_err_set(*error, NC_ERR_PARAM_INFO_BADELEM, "source");
goto cleanup;
}
ret = EXIT_SUCCESS;
cleanup:
xmlFreeDoc(src_doc);
return ret;
}
int
ofcds_rollback(void *UNUSED(data))
{
xmlChar *data;
int size, ret;
struct nc_err *e;
if (rollback.type == NC_DATASTORE_ERROR) {
nc_verb_error("No data to rollback");
return EXIT_FAILURE;
}
/* dump data for copy-config */
if (rollback.doc) {
xmlDocDumpMemory(rollback.doc, &data, &size);
} else {
data = xmlStrdup(BAD_CAST "");
}
rollbacking = 1;
ret = ofcds_copyconfig(NULL, rollback.type, NC_DATASTORE_CONFIG,
(char *) data, &e);
rollbacking = 0;
if (ret) {
nc_err_free(e);
}
xmlFree(data);
return ret;
}
struct ncds_custom_funcs ofcds_funcs = {
.init = ofcds_init,
.free = ofcds_free,
.was_changed = ofcds_changed,
.rollback = ofcds_rollback,
.lock = ofcds_lock,
.unlock = ofcds_unlock,
.is_locked = NULL,
.getconfig = ofcds_getconfig,
.copyconfig = ofcds_copyconfig,
.deleteconfig = ofcds_deleteconfig,
.editconfig = ofcds_editconfig,
};