]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ovsdb-types.h
lldp: fix a buffer overflow when handling management address TLV
[mirror_ovs.git] / lib / ovsdb-types.h
CommitLineData
e0edde6f 1/* Copyright (c) 2009, 2010, 2011 Nicira, Inc.
f85f8ebb
BP
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#ifndef OVSDB_TYPES_H
17#define OVSDB_TYPES_H 1
18
bd76d25d 19#include <float.h>
f85f8ebb
BP
20#include <stdbool.h>
21#include <stdint.h>
22#include "compiler.h"
23#include "uuid.h"
24
9131abe2
YHW
25#ifdef __cplusplus
26extern "C" {
27#endif
28
f85f8ebb
BP
29struct json;
30
31/* An atomic type: one that OVSDB regards as a single unit of data. */
32enum ovsdb_atomic_type {
33 OVSDB_TYPE_VOID, /* No value. */
34 OVSDB_TYPE_INTEGER, /* Signed 64-bit integer. */
35 OVSDB_TYPE_REAL, /* IEEE 754 double-precision floating point. */
36 OVSDB_TYPE_BOOLEAN, /* True or false. */
37 OVSDB_TYPE_STRING, /* UTF-8 string. */
38 OVSDB_TYPE_UUID, /* RFC 4122 UUID referencing a table row. */
39 OVSDB_N_TYPES
40};
41
42static inline bool ovsdb_atomic_type_is_valid(enum ovsdb_atomic_type);
f85f8ebb
BP
43bool ovsdb_atomic_type_from_string(const char *, enum ovsdb_atomic_type *);
44struct ovsdb_error *ovsdb_atomic_type_from_json(enum ovsdb_atomic_type *,
45 const struct json *);
46const char *ovsdb_atomic_type_to_string(enum ovsdb_atomic_type);
47struct json *ovsdb_atomic_type_to_json(enum ovsdb_atomic_type);
48\f
bd76d25d
BP
49/* An atomic type plus optional constraints. */
50
7360012b
BP
51enum ovsdb_ref_type {
52 OVSDB_REF_STRONG, /* Target must exist. */
53 OVSDB_REF_WEAK /* Delete reference if target disappears. */
54};
55
f70ac90d
YS
56struct ovsdb_integer_constraints {
57 int64_t min; /* minInteger or INT64_MIN. */
58 int64_t max; /* maxInteger or INT64_MAX. */
59};
60
61struct ovsdb_real_constraints {
62 double min; /* minReal or -DBL_MAX. */
63 double max; /* minReal or DBL_MAX. */
64};
65
66struct ovsdb_string_constraints {
67 unsigned int minLen; /* minLength or 0. */
68 unsigned int maxLen; /* maxLength or UINT_MAX. */
69};
70
71struct ovsdb_uuid_constraints {
72 char *refTableName; /* Name of referenced table, or NULL. */
73 struct ovsdb_table *refTable; /* Referenced table, if available. */
74 enum ovsdb_ref_type refType; /* Reference type. */
75};
76
bd76d25d
BP
77struct ovsdb_base_type {
78 enum ovsdb_atomic_type type;
bfc96d9b
BP
79
80 /* If nonnull, a datum with keys of type 'type' that expresses all the
81 * valid values for this base_type. */
82 struct ovsdb_datum *enum_;
83
bd76d25d 84 union {
f70ac90d
YS
85 struct ovsdb_integer_constraints integer;
86 struct ovsdb_real_constraints real;
bd76d25d 87 /* No constraints for Boolean types. */
f70ac90d
YS
88 struct ovsdb_string_constraints string;
89 struct ovsdb_uuid_constraints uuid;
fa37affa 90 };
bd76d25d
BP
91};
92
93#define OVSDB_BASE_VOID_INIT { .type = OVSDB_TYPE_VOID }
94#define OVSDB_BASE_INTEGER_INIT { .type = OVSDB_TYPE_INTEGER, \
fa37affa 95 .integer = { INT64_MIN, INT64_MAX } }
bd76d25d 96#define OVSDB_BASE_REAL_INIT { .type = OVSDB_TYPE_REAL, \
fa37affa 97 .real = { -DBL_MAX, DBL_MAX } }
bd76d25d 98#define OVSDB_BASE_BOOLEAN_INIT { .type = OVSDB_TYPE_BOOLEAN }
bfe8e67a 99#define OVSDB_BASE_STRING_INIT { .type = OVSDB_TYPE_STRING, \
fa37affa 100 .string = { 0, UINT_MAX } }
0d0f05b9 101#define OVSDB_BASE_UUID_INIT { .type = OVSDB_TYPE_UUID, \
fa37affa 102 .uuid = { NULL, NULL, 0 } }
bd76d25d
BP
103
104void ovsdb_base_type_init(struct ovsdb_base_type *, enum ovsdb_atomic_type);
105void ovsdb_base_type_clone(struct ovsdb_base_type *,
106 const struct ovsdb_base_type *);
107void ovsdb_base_type_destroy(struct ovsdb_base_type *);
108
109bool ovsdb_base_type_is_valid(const struct ovsdb_base_type *);
110bool ovsdb_base_type_has_constraints(const struct ovsdb_base_type *);
111void ovsdb_base_type_clear_constraints(struct ovsdb_base_type *);
bfc96d9b 112const struct ovsdb_type *ovsdb_base_type_get_enum_type(enum ovsdb_atomic_type);
bd76d25d
BP
113
114struct ovsdb_error *ovsdb_base_type_from_json(struct ovsdb_base_type *,
115 const struct json *)
cab50449 116 OVS_WARN_UNUSED_RESULT;
bd76d25d 117struct json *ovsdb_base_type_to_json(const struct ovsdb_base_type *);
7360012b
BP
118
119static inline bool ovsdb_base_type_is_ref(const struct ovsdb_base_type *);
120static inline bool ovsdb_base_type_is_strong_ref(
121 const struct ovsdb_base_type *);
122static inline bool ovsdb_base_type_is_weak_ref(const struct ovsdb_base_type *);
bd76d25d 123\f
c7239c2f 124/* An OVSDB type.
f85f8ebb 125 *
c7239c2f
BP
126 * Several rules constrain the valid types. See ovsdb_type_is_valid() (in
127 * ovsdb-types.c) for details.
f85f8ebb 128 *
c7239c2f
BP
129 * If 'value_type' is OVSDB_TYPE_VOID, 'n_min' is 1, and 'n_max' is 1, then the
130 * type is a single atomic 'key_type'.
f85f8ebb 131 *
c7239c2f
BP
132 * If 'value_type' is OVSDB_TYPE_VOID and 'n_min' or 'n_max' (or both) has a
133 * value other than 1, then the type is a set of 'key_type'. If 'n_min' is 0
134 * and 'n_max' is 1, then the type can also be considered an optional
135 * 'key_type'.
136 *
137 * If 'value_type' is not OVSDB_TYPE_VOID, then the type is a map from
138 * 'key_type' to 'value_type'. If 'n_min' is 0 and 'n_max' is 1, then the type
139 * can also be considered an optional pair of 'key_type' and 'value_type'.
f85f8ebb
BP
140 */
141struct ovsdb_type {
bd76d25d
BP
142 struct ovsdb_base_type key;
143 struct ovsdb_base_type value;
f85f8ebb
BP
144 unsigned int n_min;
145 unsigned int n_max; /* UINT_MAX stands in for "unlimited". */
146};
147
bd76d25d 148#define OVSDB_TYPE_SCALAR_INITIALIZER(KEY) { KEY, OVSDB_BASE_VOID_INIT, 1, 1 }
f85f8ebb
BP
149
150extern const struct ovsdb_type ovsdb_type_integer;
151extern const struct ovsdb_type ovsdb_type_real;
152extern const struct ovsdb_type ovsdb_type_boolean;
153extern const struct ovsdb_type ovsdb_type_string;
154extern const struct ovsdb_type ovsdb_type_uuid;
155
bd76d25d
BP
156void ovsdb_type_clone(struct ovsdb_type *, const struct ovsdb_type *);
157void ovsdb_type_destroy(struct ovsdb_type *);
158
f85f8ebb
BP
159bool ovsdb_type_is_valid(const struct ovsdb_type *);
160
161static inline bool ovsdb_type_is_scalar(const struct ovsdb_type *);
162static inline bool ovsdb_type_is_optional(const struct ovsdb_type *);
09e25603
TW
163static inline bool ovsdb_type_is_optional_scalar(
164 const struct ovsdb_type *);
f85f8ebb
BP
165static inline bool ovsdb_type_is_composite(const struct ovsdb_type *);
166static inline bool ovsdb_type_is_set(const struct ovsdb_type *);
167static inline bool ovsdb_type_is_map(const struct ovsdb_type *);
168
169char *ovsdb_type_to_english(const struct ovsdb_type *);
170
171struct ovsdb_error *ovsdb_type_from_json(struct ovsdb_type *,
172 const struct json *)
cab50449 173 OVS_WARN_UNUSED_RESULT;
f85f8ebb
BP
174struct json *ovsdb_type_to_json(const struct ovsdb_type *);
175\f
176/* Inline function implementations. */
177
178static inline bool
179ovsdb_atomic_type_is_valid(enum ovsdb_atomic_type atomic_type)
180{
a6d214f0 181 return (int) atomic_type >= 0 && atomic_type < OVSDB_N_TYPES;
f85f8ebb
BP
182}
183
7360012b
BP
184static inline bool
185ovsdb_base_type_is_ref(const struct ovsdb_base_type *base)
186{
fa37affa 187 return base->type == OVSDB_TYPE_UUID && base->uuid.refTableName;
7360012b
BP
188}
189
190static inline bool
191ovsdb_base_type_is_strong_ref(const struct ovsdb_base_type *base)
192{
193 return (ovsdb_base_type_is_ref(base)
fa37affa 194 && base->uuid.refType == OVSDB_REF_STRONG);
7360012b
BP
195}
196
197static inline bool
198ovsdb_base_type_is_weak_ref(const struct ovsdb_base_type *base)
199{
200 return (ovsdb_base_type_is_ref(base)
fa37affa 201 && base->uuid.refType == OVSDB_REF_WEAK);
7360012b
BP
202}
203
f85f8ebb
BP
204static inline bool ovsdb_type_is_scalar(const struct ovsdb_type *type)
205{
bd76d25d 206 return (type->value.type == OVSDB_TYPE_VOID
f85f8ebb
BP
207 && type->n_min == 1 && type->n_max == 1);
208}
209
210static inline bool ovsdb_type_is_optional(const struct ovsdb_type *type)
211{
212 return type->n_min == 0;
213}
214
09e25603
TW
215static inline bool ovsdb_type_is_optional_scalar(
216 const struct ovsdb_type *type)
217{
218 return (type->value.type == OVSDB_TYPE_VOID
219 && type->n_min == 0 && type->n_max == 1);
220}
221
f85f8ebb
BP
222static inline bool ovsdb_type_is_composite(const struct ovsdb_type *type)
223{
224 return type->n_max > 1;
225}
226
227static inline bool ovsdb_type_is_set(const struct ovsdb_type *type)
228{
bd76d25d 229 return (type->value.type == OVSDB_TYPE_VOID
f85f8ebb
BP
230 && (type->n_min != 1 || type->n_max != 1));
231}
232
233static inline bool ovsdb_type_is_map(const struct ovsdb_type *type)
234{
bd76d25d 235 return type->value.type != OVSDB_TYPE_VOID;
f85f8ebb
BP
236}
237
9131abe2
YHW
238#ifdef __cplusplus
239}
240#endif
241
f85f8ebb 242#endif /* ovsdb-types.h */