]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ovsdb-types.c
ovsdb: Add support for weak references.
[mirror_ovs.git] / lib / ovsdb-types.c
1 /* Copyright (c) 2009, 2010 Nicira Networks
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <config.h>
17
18 #include "ovsdb-types.h"
19
20 #include <float.h>
21 #include <limits.h>
22
23 #include "dynamic-string.h"
24 #include "json.h"
25 #include "ovsdb-data.h"
26 #include "ovsdb-error.h"
27 #include "ovsdb-parser.h"
28
29 const struct ovsdb_type ovsdb_type_integer =
30 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_INTEGER_INIT);
31 const struct ovsdb_type ovsdb_type_real =
32 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_REAL_INIT);
33 const struct ovsdb_type ovsdb_type_boolean =
34 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_BOOLEAN_INIT);
35 const struct ovsdb_type ovsdb_type_string =
36 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_STRING_INIT);
37 const struct ovsdb_type ovsdb_type_uuid =
38 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_UUID_INIT);
39 \f
40 /* ovsdb_atomic_type */
41 const char *
42 ovsdb_atomic_type_to_string(enum ovsdb_atomic_type type)
43 {
44 switch (type) {
45 case OVSDB_TYPE_VOID:
46 return "void";
47
48 case OVSDB_TYPE_INTEGER:
49 return "integer";
50
51 case OVSDB_TYPE_REAL:
52 return "real";
53
54 case OVSDB_TYPE_BOOLEAN:
55 return "boolean";
56
57 case OVSDB_TYPE_STRING:
58 return "string";
59
60 case OVSDB_TYPE_UUID:
61 return "uuid";
62
63 case OVSDB_N_TYPES:
64 default:
65 return "<invalid>";
66 }
67 }
68
69 struct json *
70 ovsdb_atomic_type_to_json(enum ovsdb_atomic_type type)
71 {
72 return json_string_create(ovsdb_atomic_type_to_string(type));
73 }
74
75 bool
76 ovsdb_atomic_type_from_string(const char *string, enum ovsdb_atomic_type *type)
77 {
78 if (!strcmp(string, "integer")) {
79 *type = OVSDB_TYPE_INTEGER;
80 } else if (!strcmp(string, "real")) {
81 *type = OVSDB_TYPE_REAL;
82 } else if (!strcmp(string, "boolean")) {
83 *type = OVSDB_TYPE_BOOLEAN;
84 } else if (!strcmp(string, "string")) {
85 *type = OVSDB_TYPE_STRING;
86 } else if (!strcmp(string, "uuid")) {
87 *type = OVSDB_TYPE_UUID;
88 } else {
89 return false;
90 }
91 return true;
92 }
93
94 struct ovsdb_error *
95 ovsdb_atomic_type_from_json(enum ovsdb_atomic_type *type,
96 const struct json *json)
97 {
98 if (json->type == JSON_STRING) {
99 if (ovsdb_atomic_type_from_string(json_string(json), type)) {
100 return NULL;
101 } else {
102 *type = OVSDB_TYPE_VOID;
103 return ovsdb_syntax_error(json, NULL,
104 "\"%s\" is not an atomic-type",
105 json_string(json));
106 }
107 } else {
108 *type = OVSDB_TYPE_VOID;
109 return ovsdb_syntax_error(json, NULL, "atomic-type expected");
110 }
111 }
112 \f
113 /* ovsdb_base_type */
114
115 void
116 ovsdb_base_type_init(struct ovsdb_base_type *base, enum ovsdb_atomic_type type)
117 {
118 base->type = type;
119 base->enum_ = NULL;
120
121 switch (base->type) {
122 case OVSDB_TYPE_VOID:
123 break;
124
125 case OVSDB_TYPE_INTEGER:
126 base->u.integer.min = INT64_MIN;
127 base->u.integer.max = INT64_MAX;
128 break;
129
130 case OVSDB_TYPE_REAL:
131 base->u.real.min = -DBL_MAX;
132 base->u.real.max = DBL_MAX;
133 break;
134
135 case OVSDB_TYPE_BOOLEAN:
136 break;
137
138 case OVSDB_TYPE_STRING:
139 base->u.string.minLen = 0;
140 base->u.string.maxLen = UINT_MAX;
141 break;
142
143 case OVSDB_TYPE_UUID:
144 base->u.uuid.refTableName = NULL;
145 base->u.uuid.refTable = NULL;
146 break;
147
148 case OVSDB_N_TYPES:
149 NOT_REACHED();
150
151 default:
152 NOT_REACHED();
153 }
154 }
155
156 /* Returns the type of the 'enum_' member for an ovsdb_base_type whose 'type'
157 * is 'atomic_type'. */
158 const struct ovsdb_type *
159 ovsdb_base_type_get_enum_type(enum ovsdb_atomic_type atomic_type)
160 {
161 static struct ovsdb_type *types[OVSDB_N_TYPES];
162
163 if (!types[atomic_type]) {
164 struct ovsdb_type *type;
165
166 types[atomic_type] = type = xmalloc(sizeof *type);
167 ovsdb_base_type_init(&type->key, atomic_type);
168 ovsdb_base_type_init(&type->value, OVSDB_TYPE_VOID);
169 type->n_min = 1;
170 type->n_max = UINT_MAX;
171 }
172 return types[atomic_type];
173 }
174
175 void
176 ovsdb_base_type_clone(struct ovsdb_base_type *dst,
177 const struct ovsdb_base_type *src)
178 {
179 *dst = *src;
180
181 if (src->enum_) {
182 dst->enum_ = xmalloc(sizeof *dst->enum_);
183 ovsdb_datum_clone(dst->enum_, src->enum_,
184 ovsdb_base_type_get_enum_type(dst->type));
185 }
186
187 switch (dst->type) {
188 case OVSDB_TYPE_VOID:
189 case OVSDB_TYPE_INTEGER:
190 case OVSDB_TYPE_REAL:
191 case OVSDB_TYPE_BOOLEAN:
192 break;
193
194 case OVSDB_TYPE_STRING:
195 break;
196
197 case OVSDB_TYPE_UUID:
198 if (dst->u.uuid.refTableName) {
199 dst->u.uuid.refTableName = xstrdup(dst->u.uuid.refTableName);
200 }
201 break;
202
203 case OVSDB_N_TYPES:
204 default:
205 NOT_REACHED();
206 }
207 }
208
209 void
210 ovsdb_base_type_destroy(struct ovsdb_base_type *base)
211 {
212 if (base) {
213 if (base->enum_) {
214 ovsdb_datum_destroy(base->enum_,
215 ovsdb_base_type_get_enum_type(base->type));
216 free(base->enum_);
217 }
218
219 switch (base->type) {
220 case OVSDB_TYPE_VOID:
221 case OVSDB_TYPE_INTEGER:
222 case OVSDB_TYPE_REAL:
223 case OVSDB_TYPE_BOOLEAN:
224 break;
225
226 case OVSDB_TYPE_STRING:
227 break;
228
229 case OVSDB_TYPE_UUID:
230 free(base->u.uuid.refTableName);
231 break;
232
233 case OVSDB_N_TYPES:
234 NOT_REACHED();
235
236 default:
237 NOT_REACHED();
238 }
239 }
240 }
241
242 bool
243 ovsdb_base_type_is_valid(const struct ovsdb_base_type *base)
244 {
245 switch (base->type) {
246 case OVSDB_TYPE_VOID:
247 return true;
248
249 case OVSDB_TYPE_INTEGER:
250 return base->u.integer.min <= base->u.integer.max;
251
252 case OVSDB_TYPE_REAL:
253 return base->u.real.min <= base->u.real.max;
254
255 case OVSDB_TYPE_BOOLEAN:
256 return true;
257
258 case OVSDB_TYPE_STRING:
259 return base->u.string.minLen <= base->u.string.maxLen;
260
261 case OVSDB_TYPE_UUID:
262 return true;
263
264 case OVSDB_N_TYPES:
265 default:
266 return false;
267 }
268 }
269
270 bool
271 ovsdb_base_type_has_constraints(const struct ovsdb_base_type *base)
272 {
273 if (base->enum_) {
274 return true;
275 }
276
277 switch (base->type) {
278 case OVSDB_TYPE_VOID:
279 NOT_REACHED();
280
281 case OVSDB_TYPE_INTEGER:
282 return (base->u.integer.min != INT64_MIN
283 || base->u.integer.max != INT64_MAX);
284
285 case OVSDB_TYPE_REAL:
286 return (base->u.real.min != -DBL_MAX
287 || base->u.real.max != DBL_MAX);
288
289 case OVSDB_TYPE_BOOLEAN:
290 return false;
291
292 case OVSDB_TYPE_STRING:
293 return base->u.string.minLen != 0 || base->u.string.maxLen != UINT_MAX;
294
295 case OVSDB_TYPE_UUID:
296 return base->u.uuid.refTableName != NULL;
297
298 case OVSDB_N_TYPES:
299 NOT_REACHED();
300
301 default:
302 NOT_REACHED();
303 }
304 }
305
306 void
307 ovsdb_base_type_clear_constraints(struct ovsdb_base_type *base)
308 {
309 enum ovsdb_atomic_type type = base->type;
310 ovsdb_base_type_destroy(base);
311 ovsdb_base_type_init(base, type);
312 }
313
314 static struct ovsdb_error *
315 parse_optional_uint(struct ovsdb_parser *parser, const char *member,
316 unsigned int *uint)
317 {
318 const struct json *json;
319
320 json = ovsdb_parser_member(parser, member, OP_INTEGER | OP_OPTIONAL);
321 if (json) {
322 if (json->u.integer < 0 || json->u.integer > UINT_MAX) {
323 return ovsdb_syntax_error(json, NULL,
324 "%s out of valid range 0 to %u",
325 member, UINT_MAX);
326 }
327 *uint = json->u.integer;
328 }
329 return NULL;
330 }
331
332 struct ovsdb_error *
333 ovsdb_base_type_from_json(struct ovsdb_base_type *base,
334 const struct json *json)
335 {
336 struct ovsdb_parser parser;
337 struct ovsdb_error *error;
338 const struct json *type, *enum_;
339
340 if (json->type == JSON_STRING) {
341 error = ovsdb_atomic_type_from_json(&base->type, json);
342 if (error) {
343 return error;
344 }
345 ovsdb_base_type_init(base, base->type);
346 return NULL;
347 }
348
349 ovsdb_parser_init(&parser, json, "ovsdb type");
350 type = ovsdb_parser_member(&parser, "type", OP_STRING);
351 if (ovsdb_parser_has_error(&parser)) {
352 base->type = OVSDB_TYPE_VOID;
353 return ovsdb_parser_finish(&parser);
354 }
355
356 error = ovsdb_atomic_type_from_json(&base->type, type);
357 if (error) {
358 return error;
359 }
360
361 ovsdb_base_type_init(base, base->type);
362
363 enum_ = ovsdb_parser_member(&parser, "enum", OP_ANY | OP_OPTIONAL);
364 if (enum_) {
365 base->enum_ = xmalloc(sizeof *base->enum_);
366 error = ovsdb_datum_from_json(
367 base->enum_, ovsdb_base_type_get_enum_type(base->type),
368 enum_, NULL);
369 if (error) {
370 free(base->enum_);
371 base->enum_ = NULL;
372 }
373 } else if (base->type == OVSDB_TYPE_INTEGER) {
374 const struct json *min, *max;
375
376 min = ovsdb_parser_member(&parser, "minInteger",
377 OP_INTEGER | OP_OPTIONAL);
378 max = ovsdb_parser_member(&parser, "maxInteger",
379 OP_INTEGER | OP_OPTIONAL);
380 base->u.integer.min = min ? min->u.integer : INT64_MIN;
381 base->u.integer.max = max ? max->u.integer : INT64_MAX;
382 if (base->u.integer.min > base->u.integer.max) {
383 error = ovsdb_syntax_error(json, NULL,
384 "minInteger exceeds maxInteger");
385 }
386 } else if (base->type == OVSDB_TYPE_REAL) {
387 const struct json *min, *max;
388
389 min = ovsdb_parser_member(&parser, "minReal", OP_NUMBER | OP_OPTIONAL);
390 max = ovsdb_parser_member(&parser, "maxReal", OP_NUMBER | OP_OPTIONAL);
391 base->u.real.min = min ? json_real(min) : -DBL_MAX;
392 base->u.real.max = max ? json_real(max) : DBL_MAX;
393 if (base->u.real.min > base->u.real.max) {
394 error = ovsdb_syntax_error(json, NULL, "minReal exceeds maxReal");
395 }
396 } else if (base->type == OVSDB_TYPE_STRING) {
397 if (!error) {
398 error = parse_optional_uint(&parser, "minLength",
399 &base->u.string.minLen);
400 }
401 if (!error) {
402 error = parse_optional_uint(&parser, "maxLength",
403 &base->u.string.maxLen);
404 }
405 if (!error && base->u.string.minLen > base->u.string.maxLen) {
406 error = ovsdb_syntax_error(json, NULL,
407 "minLength exceeds maxLength");
408 }
409 } else if (base->type == OVSDB_TYPE_UUID) {
410 const struct json *refTable;
411
412 refTable = ovsdb_parser_member(&parser, "refTable",
413 OP_ID | OP_OPTIONAL);
414 if (refTable) {
415 const struct json *refType;
416
417 base->u.uuid.refTableName = xstrdup(refTable->u.string);
418
419 /* We can't set base->u.uuid.refTable here because we don't have
420 * enough context (we might not even be running in ovsdb-server).
421 * ovsdb_create() will set refTable later. */
422
423 refType = ovsdb_parser_member(&parser, "refType",
424 OP_ID | OP_OPTIONAL);
425 if (refType) {
426 const char *refType_s = json_string(refType);
427 if (!strcmp(refType_s, "strong")) {
428 base->u.uuid.refType = OVSDB_REF_STRONG;
429 } else if (!strcmp(refType_s, "weak")) {
430 base->u.uuid.refType = OVSDB_REF_WEAK;
431 } else {
432 error = ovsdb_syntax_error(json, NULL, "refType must be "
433 "\"strong\" or \"weak\" (not "
434 "\"%s\")", refType_s);
435 }
436 } else {
437 base->u.uuid.refType = OVSDB_REF_STRONG;
438 }
439 }
440 }
441
442 if (error) {
443 ovsdb_error_destroy(ovsdb_parser_finish(&parser));
444 } else {
445 error = ovsdb_parser_finish(&parser);
446 }
447 if (error) {
448 ovsdb_base_type_destroy(base);
449 base->type = OVSDB_TYPE_VOID;
450 }
451 return error;
452 }
453
454 struct json *
455 ovsdb_base_type_to_json(const struct ovsdb_base_type *base)
456 {
457 struct json *json;
458
459 if (!ovsdb_base_type_has_constraints(base)) {
460 return json_string_create(ovsdb_atomic_type_to_string(base->type));
461 }
462
463 json = json_object_create();
464 json_object_put_string(json, "type",
465 ovsdb_atomic_type_to_string(base->type));
466
467 if (base->enum_) {
468 const struct ovsdb_type *type;
469
470 type = ovsdb_base_type_get_enum_type(base->type);
471 json_object_put(json, "enum", ovsdb_datum_to_json(base->enum_, type));
472 }
473
474 switch (base->type) {
475 case OVSDB_TYPE_VOID:
476 NOT_REACHED();
477
478 case OVSDB_TYPE_INTEGER:
479 if (base->u.integer.min != INT64_MIN) {
480 json_object_put(json, "minInteger",
481 json_integer_create(base->u.integer.min));
482 }
483 if (base->u.integer.max != INT64_MAX) {
484 json_object_put(json, "maxInteger",
485 json_integer_create(base->u.integer.max));
486 }
487 break;
488
489 case OVSDB_TYPE_REAL:
490 if (base->u.real.min != -DBL_MAX) {
491 json_object_put(json, "minReal",
492 json_real_create(base->u.real.min));
493 }
494 if (base->u.real.max != DBL_MAX) {
495 json_object_put(json, "maxReal",
496 json_real_create(base->u.real.max));
497 }
498 break;
499
500 case OVSDB_TYPE_BOOLEAN:
501 break;
502
503 case OVSDB_TYPE_STRING:
504 if (base->u.string.minLen != 0) {
505 json_object_put(json, "minLength",
506 json_integer_create(base->u.string.minLen));
507 }
508 if (base->u.string.maxLen != UINT_MAX) {
509 json_object_put(json, "maxLength",
510 json_integer_create(base->u.string.maxLen));
511 }
512 break;
513
514 case OVSDB_TYPE_UUID:
515 if (base->u.uuid.refTableName) {
516 json_object_put_string(json, "refTable",
517 base->u.uuid.refTableName);
518 if (base->u.uuid.refType == OVSDB_REF_WEAK) {
519 json_object_put_string(json, "refType", "weak");
520 }
521 }
522 break;
523
524 case OVSDB_N_TYPES:
525 NOT_REACHED();
526
527 default:
528 NOT_REACHED();
529 }
530
531 return json;
532 }
533 \f
534 /* ovsdb_type */
535
536 void
537 ovsdb_type_clone(struct ovsdb_type *dst, const struct ovsdb_type *src)
538 {
539 ovsdb_base_type_clone(&dst->key, &src->key);
540 ovsdb_base_type_clone(&dst->value, &src->value);
541 dst->n_min = src->n_min;
542 dst->n_max = src->n_max;
543 }
544
545 void
546 ovsdb_type_destroy(struct ovsdb_type *type)
547 {
548 ovsdb_base_type_destroy(&type->key);
549 ovsdb_base_type_destroy(&type->value);
550 }
551
552 bool
553 ovsdb_type_is_valid(const struct ovsdb_type *type)
554 {
555 return (type->key.type != OVSDB_TYPE_VOID
556 && ovsdb_base_type_is_valid(&type->key)
557 && ovsdb_base_type_is_valid(&type->value)
558 && type->n_min <= 1
559 && type->n_min <= type->n_max
560 && type->n_max >= 1);
561 }
562
563 static struct ovsdb_error *
564 n_from_json(const struct json *json, unsigned int *n)
565 {
566 if (!json) {
567 return NULL;
568 } else if (json->type == JSON_INTEGER
569 && json->u.integer >= 0 && json->u.integer < UINT_MAX) {
570 *n = json->u.integer;
571 return NULL;
572 } else {
573 return ovsdb_syntax_error(json, NULL, "bad min or max value");
574 }
575 }
576
577 char *
578 ovsdb_type_to_english(const struct ovsdb_type *type)
579 {
580 const char *key = ovsdb_atomic_type_to_string(type->key.type);
581 const char *value = ovsdb_atomic_type_to_string(type->value.type);
582 if (ovsdb_type_is_scalar(type)) {
583 return xstrdup(key);
584 } else {
585 struct ds s = DS_EMPTY_INITIALIZER;
586 ds_put_cstr(&s, ovsdb_type_is_set(type) ? "set" : "map");
587 if (type->n_max == UINT_MAX) {
588 if (type->n_min) {
589 ds_put_format(&s, " of %u or more", type->n_min);
590 } else {
591 ds_put_cstr(&s, " of");
592 }
593 } else if (type->n_min) {
594 ds_put_format(&s, " of %u to %u", type->n_min, type->n_max);
595 } else {
596 ds_put_format(&s, " of up to %u", type->n_max);
597 }
598 if (ovsdb_type_is_set(type)) {
599 ds_put_format(&s, " %ss", key);
600 } else {
601 ds_put_format(&s, " (%s, %s) pairs", key, value);
602 }
603 return ds_cstr(&s);
604 }
605 }
606
607 struct ovsdb_error *
608 ovsdb_type_from_json(struct ovsdb_type *type, const struct json *json)
609 {
610 ovsdb_base_type_init(&type->value, OVSDB_TYPE_VOID);
611 type->n_min = 1;
612 type->n_max = 1;
613
614 if (json->type == JSON_STRING) {
615 return ovsdb_base_type_from_json(&type->key, json);
616 } else if (json->type == JSON_OBJECT) {
617 const struct json *key, *value, *min, *max;
618 struct ovsdb_error *error;
619 struct ovsdb_parser parser;
620
621 ovsdb_parser_init(&parser, json, "ovsdb type");
622 key = ovsdb_parser_member(&parser, "key", OP_STRING | OP_OBJECT);
623 value = ovsdb_parser_member(&parser, "value",
624 OP_STRING | OP_OBJECT | OP_OPTIONAL);
625 min = ovsdb_parser_member(&parser, "min", OP_INTEGER | OP_OPTIONAL);
626 max = ovsdb_parser_member(&parser, "max",
627 OP_INTEGER | OP_STRING | OP_OPTIONAL);
628 error = ovsdb_parser_finish(&parser);
629 if (error) {
630 return error;
631 }
632
633 error = ovsdb_base_type_from_json(&type->key, key);
634 if (error) {
635 return error;
636 }
637
638 if (value) {
639 error = ovsdb_base_type_from_json(&type->value, value);
640 if (error) {
641 return error;
642 }
643 }
644
645 error = n_from_json(min, &type->n_min);
646 if (error) {
647 return error;
648 }
649
650 if (max && max->type == JSON_STRING
651 && !strcmp(max->u.string, "unlimited")) {
652 type->n_max = UINT_MAX;
653 } else {
654 error = n_from_json(max, &type->n_max);
655 if (error) {
656 return error;
657 }
658 }
659
660 if (!ovsdb_type_is_valid(type)) {
661 return ovsdb_syntax_error(json, NULL,
662 "ovsdb type fails constraint checks");
663 }
664
665 return NULL;
666 } else {
667 return ovsdb_syntax_error(json, NULL, "ovsdb type expected");
668 }
669 }
670
671 struct json *
672 ovsdb_type_to_json(const struct ovsdb_type *type)
673 {
674 if (ovsdb_type_is_scalar(type)
675 && !ovsdb_base_type_has_constraints(&type->key)) {
676 return ovsdb_base_type_to_json(&type->key);
677 } else {
678 struct json *json = json_object_create();
679 json_object_put(json, "key", ovsdb_base_type_to_json(&type->key));
680 if (type->value.type != OVSDB_TYPE_VOID) {
681 json_object_put(json, "value",
682 ovsdb_base_type_to_json(&type->value));
683 }
684 if (type->n_min != 1) {
685 json_object_put(json, "min", json_integer_create(type->n_min));
686 }
687 if (type->n_max == UINT_MAX) {
688 json_object_put_string(json, "max", "unlimited");
689 } else if (type->n_max != 1) {
690 json_object_put(json, "max", json_integer_create(type->n_max));
691 }
692 return json;
693 }
694 }