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_tree.h>
69 #include <yajl/yajl_gen.h>
71 /* Forward declaration */
72 typedef struct ovs_db_s ovs_db_t;
74 /* OVS DB callback type declaration */
75 typedef void (*ovs_db_init_cb_t) (ovs_db_t *pdb);
76 typedef void (*ovs_db_table_cb_t) (yajl_val jupdates);
77 typedef void (*ovs_db_result_cb_t) (yajl_val jresult, yajl_val jerror);
79 /* OVS DB structures */
80 struct ovs_db_callback_s {
81 ovs_db_init_cb_t init_cb;
82 };
83 typedef struct ovs_db_callback_s ovs_db_callback_t;
85 /* OVS DB prototypes */
87 /*
88 * NAME
89 * ovs_db_init
90 *
91 * DESCRIPTION
92 * Initialize OVS DB internal data. The `ovs_db_destroy' function
93 * shall destroy the returned object.
94 *
95 * PARAMETERS
96 * `surl' OVS DB communication URL.
97 * `cb' OVS DB callbacks.
98 *
99 * RETURN VALUE
100 * New ovs_db_t object upon success or NULL if an error occurred.
101 */
102 ovs_db_t *ovs_db_init(const char *surl, ovs_db_callback_t *cb);
104 /*
105 * NAME
106 * ovs_db_destroy
107 *
108 * DESCRIPTION
109 * Destroy OVS DB object referenced by `pdb'.
110 *
111 * PARAMETERS
112 * `pdb' Pointer to OVS DB object.
113 *
114 * RETURN VALUE
115 * Zero upon success or non-zero if an error occurred.
116 */
117 int ovs_db_destroy(ovs_db_t *pdb);
119 /*
120 * NAME
121 * ovs_db_send_request
122 *
123 * DESCRIPTION
124 * Send JSON request to OVS DB server.
125 *
126 * PARAMETERS
127 * `pdb' Pointer to OVS DB object.
128 * `method' Request method name.
129 * `params' Method params to be sent (JSON value as a string).
130 * `cb' Result callback of the request. If NULL, the request
131 * is sent asynchronously.
132 *
133 * RETURN VALUE
134 * Zero upon success or non-zero if an error occurred.
135 */
136 int ovs_db_send_request(ovs_db_t *pdb, const char *method,
137 const char *params, ovs_db_result_cb_t cb);
139 /* callback types */
140 #define OVS_DB_TABLE_CB_FLAG_INITIAL 0x01U
141 #define OVS_DB_TABLE_CB_FLAG_INSERT 0x02U
142 #define OVS_DB_TABLE_CB_FLAG_DELETE 0x04U
143 #define OVS_DB_TABLE_CB_FLAG_MODIFY 0x08U
144 #define OVS_DB_TABLE_CB_FLAG_ALL 0x0FU
146 /*
147 * NAME
148 * ovs_db_table_cb_register
149 *
150 * DESCRIPTION
151 * Subscribe a callback on OVS DB table event. It allows to
152 * receive notifications (`update_cb' callback is called) of
153 * changes to requested table.
154 *
155 * PARAMETERS
156 * `pdb' Pointer to OVS DB object.
157 * `tb_name' OVS DB Table name to be monitored.
158 * `tb_column' OVS DB Table columns to be monitored. Last
159 * element in the array should be NULL.
160 * `update_cb' Callback function that is called when
161 * requested table columns are changed.
162 * `cb' Result callback of the request. If NULL, the call
163 * becomes asynchronous.
164 * Useful, if OVS_DB_TABLE_CB_FLAG_INITIAL is set.
165 * `flags' Bit mask of:
166 * OVS_DB_TABLE_CB_FLAG_INITIAL Receive initial values in
167 * result callback.
168 * OVS_DB_TABLE_CB_FLAG_INSERT Receive table insert events.
169 * OVS_DB_TABLE_CB_FLAG_DELETE Receive table remove events.
170 * OVS_DB_TABLE_CB_FLAG_MODIFY Receive table update events.
171 * OVS_DB_TABLE_CB_FLAG_ALL Receive all events.
172 *
173 * RETURN VALUE
174 * Zero upon success or non-zero if an error occurred.
175 */
176 int ovs_db_table_cb_register(ovs_db_t *pdb, const char *tb_name,
177 const char **tb_column,
178 ovs_db_table_cb_t update_cb,
179 ovs_db_result_cb_t result_cb,
180 unsigned int flags);
182 /*
183 * OVS utils API
184 */
186 /*
187 * NAME
188 * ovs_utils_get_value_by_key
189 *
190 * DESCRIPTION
191 * Get YAJL value by object name.
192 *
193 * PARAMETERS
194 * `jval' YAJL object value.
195 * `key' Object key name.
196 *
197 * RETURN VALUE
198 * YAJL value upon success or NULL if key not found.
199 */
200 yajl_val ovs_utils_get_value_by_key(yajl_val jval, const char *key);
202 #endif