1 /**
2 * collectd - src/utils_ovs.h
3 *
4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * Authors:
25 * Volodymyr Mytnyk <volodymyrx.mytnyk@intel.com>
26 *
27 * Description:
28 * The OVS util module provides the following features:
29 * - Implements the OVS DB communication transport specified
30 * by RFC7047:
31 * * Connect/disconnect to OVS DB;
32 * * Recovery mechanism in case of OVS DB connection lost;
33 * * Subscription mechanism to OVS DB table update events
34 * (insert/modify/delete);
35 * * Send custom JSON request to OVS DB (poll table data, etc.)
36 * * Handling of echo request from OVS DB server to verify the
37 * liveness of the connection.
38 * - Provides YAJL helpers functions.
39 *
40 * OVS DB API User Guide:
41 * All OVS DB function/structure names begins from 'ovs_db_*' prefix. To
42 * start using OVS DB API, client (plugin) should initialize the OVS DB
43 * object (`ovs_db_t') by calling `ovs_db_init' function. It initializes
44 * internal data and creates two main workers (threads). The result of the
45 * function is a pointer to new OVS DB object which can be used by other
46 * OVS DB API later and must be released by `ovs_db_destroy' function if
47 * the object isn't needed anymore.
48 * Once OVS DB API is initialized, the `init_cb' callback is called if
49 * the connection to OVS DB has been established. This callback is called
50 * every time the OVS DB is reconnected. So, if the client registers table
51 * update event callbacks or does any other OVS DB setup that can be lost
52 * after OVS DB reconnecting, it should be done in `init_cb' callback.
53 * The `ovs_db_table_cb_register` function is used to register OVS DB
54 * table update event callback and receive the table update notification
55 * when requested event occurs (registered callback is called). See
56 * function API for more info.
57 * To send custom JSON-RPC request to OVS DB, the `ovs_db_send_request'
58 * function is used. Please note, that connection to OVS DB should be
59 * established otherwise the function will return error.
60 * To verify the liveness of established connection, the OVS DB server
61 * sends echo request to the client with a given interval. The OVS utils
62 * takes care about this request and handles it properly.
63 **/
65 #ifndef UTILS_OVS_H
66 #define UTILS_OVS_H
68 #include <yajl/yajl_gen.h>
69 #include <yajl/yajl_tree.h>
71 /* Forward declaration */
72 typedef struct ovs_db_s ovs_db_t;
74 /* OVS DB callback type declaration */
75 typedef void (*ovs_db_table_cb_t)(yajl_val jupdates);
76 typedef void (*ovs_db_result_cb_t)(yajl_val jresult, yajl_val jerror);
78 /* OVS DB structures */
79 struct ovs_db_callback_s {
80 /*
81 * This callback is called when OVS DB connection
82 * has been established and ready to use. Client
83 * can use this callback to configure OVS DB, e.g.
84 * to subscribe to table update notification or poll
85 * some OVS DB data. This field can be NULL.
86 */
87 void (*post_conn_init)(ovs_db_t *pdb);
88 /*
89 * This callback is called when OVD DB connection
90 * has been lost. This field can be NULL.
91 */
92 void (*post_conn_terminate)(void);
93 };
94 typedef struct ovs_db_callback_s ovs_db_callback_t;
96 /* OVS DB prototypes */
98 /*
99 * NAME
100 * ovs_db_init
101 *
102 * DESCRIPTION
103 * Initialize OVS DB internal data. The `ovs_db_destroy' function
104 * shall destroy the returned object.
105 *
106 * PARAMETERS
107 * `surl' OVS DB communication URL.
108 * `cb' OVS DB callbacks.
109 *
110 * RETURN VALUE
111 * New ovs_db_t object upon success or NULL if an error occurred.
112 */
113 ovs_db_t *ovs_db_init(const char *surl, ovs_db_callback_t *cb);
115 /*
116 * NAME
117 * ovs_db_destroy
118 *
119 * DESCRIPTION
120 * Destroy OVS DB object referenced by `pdb'.
121 *
122 * PARAMETERS
123 * `pdb' Pointer to OVS DB object.
124 *
125 * RETURN VALUE
126 * Zero upon success or non-zero if an error occurred.
127 */
128 int ovs_db_destroy(ovs_db_t *pdb);
130 /*
131 * NAME
132 * ovs_db_send_request
133 *
134 * DESCRIPTION
135 * Send JSON request to OVS DB server.
136 *
137 * PARAMETERS
138 * `pdb' Pointer to OVS DB object.
139 * `method' Request method name.
140 * `params' Method params to be sent (JSON value as a string).
141 * `cb' Result callback of the request. If NULL, the request
142 * is sent asynchronously.
143 *
144 * RETURN VALUE
145 * Zero upon success or non-zero if an error occurred.
146 */
147 int ovs_db_send_request(ovs_db_t *pdb, const char *method, const char *params,
148 ovs_db_result_cb_t cb);
150 /* callback types */
151 #define OVS_DB_TABLE_CB_FLAG_INITIAL 0x01U
152 #define OVS_DB_TABLE_CB_FLAG_INSERT 0x02U
153 #define OVS_DB_TABLE_CB_FLAG_DELETE 0x04U
154 #define OVS_DB_TABLE_CB_FLAG_MODIFY 0x08U
155 #define OVS_DB_TABLE_CB_FLAG_ALL 0x0FU
157 /*
158 * NAME
159 * ovs_db_table_cb_register
160 *
161 * DESCRIPTION
162 * Subscribe a callback on OVS DB table event. It allows to
163 * receive notifications (`update_cb' callback is called) of
164 * changes to requested table.
165 *
166 * PARAMETERS
167 * `pdb' Pointer to OVS DB object.
168 * `tb_name' OVS DB Table name to be monitored.
169 * `tb_column' OVS DB Table columns to be monitored. Last
170 * element in the array should be NULL.
171 * `update_cb' Callback function that is called when
172 * requested table columns are changed.
173 * `cb' Result callback of the request. If NULL, the call
174 * becomes asynchronous.
175 * Useful, if OVS_DB_TABLE_CB_FLAG_INITIAL is set.
176 * `flags' Bit mask of:
177 * OVS_DB_TABLE_CB_FLAG_INITIAL Receive initial values in
178 * result callback.
179 * OVS_DB_TABLE_CB_FLAG_INSERT Receive table insert events.
180 * OVS_DB_TABLE_CB_FLAG_DELETE Receive table remove events.
181 * OVS_DB_TABLE_CB_FLAG_MODIFY Receive table update events.
182 * OVS_DB_TABLE_CB_FLAG_ALL Receive all events.
183 *
184 * RETURN VALUE
185 * Zero upon success or non-zero if an error occurred.
186 */
187 int ovs_db_table_cb_register(ovs_db_t *pdb, const char *tb_name,
188 const char **tb_column,
189 ovs_db_table_cb_t update_cb,
190 ovs_db_result_cb_t result_cb, unsigned int flags);
192 /*
193 * OVS utils API
194 */
196 /*
197 * NAME
198 * ovs_utils_get_value_by_key
199 *
200 * DESCRIPTION
201 * Get YAJL value by object name.
202 *
203 * PARAMETERS
204 * `jval' YAJL object value.
205 * `key' Object key name.
206 *
207 * RETURN VALUE
208 * YAJL value upon success or NULL if key not found.
209 */
210 yajl_val ovs_utils_get_value_by_key(yajl_val jval, const char *key);
212 /*
213 * NAME
214 * ovs_utils_get_map_value
215 *
216 * DESCRIPTION
217 * Get OVS DB map value by given map key (rfc7047, "Notation" section).
218 *
219 * PARAMETERS
220 * `jval' A 2-element YAJL array that represents a OVS DB map value.
221 * `key' OVS DB map key name.
222 *
223 * RETURN VALUE
224 * YAJL value upon success or NULL if key not found.
225 */
226 yajl_val ovs_utils_get_map_value(yajl_val jval, const char *key);
228 #endif