Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[cascardo/ovs.git] / lib / cfg.h
1 /* Copyright (c) 2008, 2009 Nicira Networks
2  * 
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  * In addition, as a special exception, Nicira Networks gives permission
17  * to link the code of its release of vswitchd with the OpenSSL project's
18  * "OpenSSL" library (or with modified versions of it that use the same
19  * license as the "OpenSSL" library), and distribute the linked
20  * executables.  You must obey the GNU General Public License in all
21  * respects for all of the code used other than "OpenSSL".  If you modify
22  * this file, you may extend this exception to your version of the file,
23  * but you are not obligated to do so.  If you do not wish to do so,
24  * delete this exception statement from your version.
25  */
26
27
28 #ifndef VSWITCHD_CFG_H
29 #define VSWITCHD_CFG_H 1
30
31 #include <stdbool.h>
32 #include <stdint.h>
33 #include <unistd.h>
34 #include "compiler.h"
35 #include "sha1.h"
36
37 struct svec;
38 struct ofpbuf;
39
40 int cfg_set_file(const char *file_name);
41 int cfg_read(void);
42 int cfg_lock(uint8_t *cookie, int timeout);
43 void cfg_unlock(void);
44 int cfg_write(void);
45 int cfg_write_data(uint8_t *data, size_t len);
46 bool cfg_is_dirty(void);
47
48 void cfg_get_all(struct svec *);
49
50 #define CFG_COOKIE_LEN SHA1HashSize
51 int cfg_get_cookie(uint8_t *cookie);
52
53 void cfg_buf_put(struct ofpbuf *buffer);
54 void cfg_get_subsections(struct svec *, const char *, ...) PRINTF_FORMAT(2, 3);
55
56 enum cfg_flags {
57     /* Types allowed. */
58     CFG_STRING = 1 << 0,        /* Arbitrary content. */
59     CFG_KEY = 1 << 0,           /* Valid key name. */
60     CFG_INT = 1 << 2,           /* Integer value. */
61     CFG_BOOL = 1 << 3,          /* Boolean. */
62     CFG_IP = 1 << 4,            /* IPv4 address. */
63     CFG_MAC = 1 << 5,           /* MAC address. */
64     CFG_VLAN = 1 << 6,          /* Integer in range 0...4095. */
65     CFG_DPID = 1 << 7,          /* 12 hexadecimal digits. */
66
67     /* Number allowed. */
68     CFG_REQUIRED = 1 << 8,      /* At least one value allowed. */
69     CFG_MULTIPLE = 1 << 9       /* More than one value allowed. */
70 };
71 void cfg_register(const char *key_spec, enum cfg_flags);
72
73 void cfg_add_entry(const char *key, ...) PRINTF_FORMAT(1, 2);
74 void cfg_del_entry(const char *key, ...) PRINTF_FORMAT(1, 2);
75 void cfg_del_section(const char *key, ...) PRINTF_FORMAT(1, 2);
76 void cfg_del_match(const char *pattern, ...) PRINTF_FORMAT(1, 2);
77 void cfg_get_section(struct svec *svec, const char *key, ...) 
78     PRINTF_FORMAT(2, 3);
79
80 bool cfg_has(const char *key, ...) PRINTF_FORMAT(1, 2);
81 bool cfg_is_valid(enum cfg_flags, const char *key, ...) PRINTF_FORMAT(2, 3);
82 bool cfg_has_section(const char *key, ...) PRINTF_FORMAT(1, 2);
83 int cfg_count(const char *key, ...) PRINTF_FORMAT(1, 2);
84
85 const char *cfg_get_string(int idx, const char *key, ...) PRINTF_FORMAT(2, 3);
86 const char *cfg_get_key(int idx, const char *key, ...) PRINTF_FORMAT(2, 3);
87 int cfg_get_int(int idx, const char *key, ...) PRINTF_FORMAT(2, 3);
88 bool cfg_get_bool(int idx, const char *key, ...) PRINTF_FORMAT(2, 3);
89 uint32_t cfg_get_ip(int idx, const char *key, ...) PRINTF_FORMAT(2, 3);
90 uint64_t cfg_get_mac(int idx, const char *key, ...) PRINTF_FORMAT(2, 3);
91 int cfg_get_vlan(int idx, const char *key, ...) PRINTF_FORMAT(2, 3);
92 uint64_t cfg_get_dpid(int idx, const char *key, ...) PRINTF_FORMAT(2, 3);
93
94 void cfg_get_all_strings(struct svec *, const char *key, ...)
95     PRINTF_FORMAT(2, 3);
96 void cfg_get_all_keys(struct svec *, const char *key, ...) PRINTF_FORMAT(2, 3);
97
98 #endif /* vswitchd/cfg.h */