1 /**
2 * collectd - src/ovs_events.c
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 **/
28 #include "common.h" /* auxiliary functions */
30 #include "utils_ovs.h" /* OVS helpers */
32 #define OVS_EVENTS_PLUGIN "ovs_events"
33 #define OVS_EVENTS_DEFAULT_OVS_DB_SERVER_URL "tcp:127.0.0.1:6640"
34 #define OVS_EVENTS_CTX_LOCK for (int __i = ovs_events_ctx_lock(); __i != 0 ; \
35 __i = ovs_events_ctx_unlock())
36 #define OVS_EVENTS_CONFIG_ERROR(option) do { \
37 ERROR(OVS_EVENTS_PLUGIN ": read '%s' config option failed", option); \
38 goto failure; } while (0)
40 /* Link status type */
41 enum ovs_events_link_status_e {DOWN, UP, UNKNOWN};
42 typedef enum ovs_events_link_status_e ovs_events_link_status_t;
44 /* Interface info */
45 struct ovs_events_interface_info_s {
46 char *name; /* interface name */
47 ovs_events_link_status_t link_status; /* link status */
48 struct ovs_events_interface_info_s *next; /* next interface info */
49 };
50 typedef struct ovs_events_interface_info_s ovs_events_interface_info_t;
52 /* OVS events configuration data */
53 struct ovs_events_config_s {
54 _Bool send_notification; /* sent notification to collectd? */
55 char *ovs_db_server_url; /* OVS DB server URL */
56 };
57 typedef struct ovs_events_config_s ovs_events_config_t;
59 /* OVS events context type */
60 struct ovs_events_ctx_s {
61 pthread_mutex_t mutex; /* mutex to lock the context */
62 pthread_mutexattr_t mutex_attr; /* context mutex attribute */
63 ovs_db_t *ovs_db; /* pointer to OVS DB instance */
64 ovs_events_config_t config; /* plugin config */
65 ovs_events_interface_info_t *ifaces; /* interface info */
66 };
67 typedef struct ovs_events_ctx_s ovs_events_ctx_t;
69 /*
70 * Private variables
71 */
72 static ovs_events_ctx_t ovs_events_ctx = {
73 .mutex = PTHREAD_MUTEX_INITIALIZER,
74 .config = {
75 .send_notification = 0, /* do not send notification */
76 .ovs_db_server_url = NULL}, /* use default OVS DB URL */
77 .ovs_db = NULL,
78 .ifaces = NULL};
80 /* This function is used only by "OVS_EVENTS_CTX_LOCK" define (see above).
81 * It always returns 1 when context is locked.
82 */
83 static inline int
84 ovs_events_ctx_lock()
85 {
86 pthread_mutex_lock(&ovs_events_ctx.mutex);
87 return (1);
88 }
90 /* This function is used only by "OVS_EVENTS_CTX_LOCK" define (see above).
91 * It always returns 0 when context is unlocked.
92 */
93 static inline int
94 ovs_events_ctx_unlock()
95 {
96 pthread_mutex_unlock(&ovs_events_ctx.mutex);
97 return (0);
98 }
100 /* Update link status in OVS events context (cache) */
101 static void
102 ovs_events_link_status_update(const char *name,
103 ovs_events_link_status_t status)
104 {
105 OVS_EVENTS_CTX_LOCK {
106 for (ovs_events_interface_info_t *iface = ovs_events_ctx.ifaces; iface;
107 iface = iface->next)
108 if (strcmp(iface->name, name) == 0)
109 iface->link_status = status;
110 }
111 }
113 /* Check if given interface name exists in configuration file. It
114 * returns 1 if exists otherwise 0. If no interfaces are configured,
115 * 1 is returned
116 */
117 static int
118 ovs_events_config_iface_exists(const char *ifname)
119 {
120 int rc = 0;
121 OVS_EVENTS_CTX_LOCK {
122 if (!(rc = (ovs_events_ctx.ifaces == NULL))) {
123 for (ovs_events_interface_info_t *iface = ovs_events_ctx.ifaces; iface;
124 iface = iface->next)
125 if (rc = (strcmp(ifname, iface->name) == 0))
126 break;
127 }
128 }
129 return rc;
130 }
132 /* Release memory allocated for configuration data */
133 static void
134 ovs_events_config_free()
135 {
136 ovs_events_interface_info_t *del_iface = NULL;
137 OVS_EVENTS_CTX_LOCK {
138 sfree(ovs_events_ctx.config.ovs_db_server_url);
139 while (ovs_events_ctx.ifaces) {
140 del_iface = ovs_events_ctx.ifaces;
141 ovs_events_ctx.ifaces = ovs_events_ctx.ifaces->next;
142 free(del_iface->name);
143 free(del_iface);
144 }
145 }
146 }
148 /* Parse plugin configuration file and store the config
149 * in allocated memory. Returns negative value in case of error.
150 */
151 static int
152 ovs_events_plugin_config(oconfig_item_t *ci)
153 {
154 ovs_events_interface_info_t *new_iface;
155 char *if_name;
157 for (int i = 0; i < ci->children_num; i++) {
158 oconfig_item_t *child = ci->children + i;
159 if (strcasecmp("SendNotification", child->key) == 0) {
160 if (cf_util_get_boolean(child,
161 &ovs_events_ctx.config.send_notification) < 0)
162 OVS_EVENTS_CONFIG_ERROR(child->key);
163 } else if (strcasecmp("OvsDbServerUrl", child->key) == 0) {
164 if (cf_util_get_string(child,
165 &ovs_events_ctx.config.ovs_db_server_url) < 0)
166 OVS_EVENTS_CONFIG_ERROR(child->key);
167 } else if (strcasecmp("Interfaces", child->key) == 0) {
168 for (int j = 0; j < child->values_num; j++) {
169 /* check value type */
170 if (child->values[j].type != OCONFIG_TYPE_STRING) {
171 ERROR(OVS_EVENTS_PLUGIN
172 ": given interface name is not a string [idx=%d]", j);
173 goto failure;
174 }
175 /* get value */
176 if ((if_name = strdup(child->values[j].value.string)) == NULL) {
177 ERROR(OVS_EVENTS_PLUGIN " strdup() copy interface name fail");
178 goto failure;
179 }
180 if ((new_iface = malloc(sizeof(*new_iface))) == NULL) {
181 ERROR(OVS_EVENTS_PLUGIN ": malloc () copy interface name fail");
182 goto failure;
183 } else {
184 /* store interface name */
185 new_iface->name = if_name;
186 new_iface->link_status = UNKNOWN;
187 new_iface->next = ovs_events_ctx.ifaces;
188 ovs_events_ctx.ifaces = new_iface;
189 DEBUG(OVS_EVENTS_PLUGIN ": found monitored interface \"%s\"",
190 if_name);
191 }
192 }
193 } else {
194 ERROR(OVS_EVENTS_PLUGIN ": option '%s' is not allowed here",
195 child->key);
196 goto failure;
197 }
198 }
199 return (0);
201 failure:
202 ovs_events_config_free();
203 return (-1);
204 }
206 /* Dispatch OVS interface link status event to collectd */
207 static int
208 ovs_events_dispatch_notification(const char *link_name,
209 ovs_events_link_status_t link_status)
210 {
211 const char *msg_link_status = NULL;
212 notification_t n = {NOTIF_FAILURE, cdtime(), "", "", OVS_EVENTS_PLUGIN,
213 "", "", "", NULL};
215 /* convert link status to message string */
216 switch (link_status) {
217 case UP:
218 msg_link_status = "UP";
219 n.severity = NOTIF_OKAY;
220 break;
221 case DOWN:
222 msg_link_status = "DOWN";
223 n.severity = NOTIF_WARNING;
224 break;
225 default:
226 msg_link_status = "UNKNOWN";;
227 break;
228 }
230 /* fill the notification data */
231 ssnprintf(n.message, sizeof(n.message),
232 "link state of \"%s\" interface has been changed to \"%s\"",
233 link_name, msg_link_status);
234 sstrncpy(n.host, hostname_g, sizeof(n.host));
235 sstrncpy(n.plugin_instance, link_name, sizeof(n.plugin_instance));
236 sstrncpy(n.type, "gauge", sizeof(n.type));
237 sstrncpy(n.type_instance, "link_status", sizeof(n.type_instance));
238 return plugin_dispatch_notification(&n);
239 }
241 /* Dispatch OVS interface link status value to collectd */
242 static void
243 ovs_events_link_status_submit(const char *link_name,
244 ovs_events_link_status_t link_status)
245 {
246 value_t values[1];
247 value_list_t vl = VALUE_LIST_INIT;
249 values[0].gauge = (gauge_t) link_status;
250 vl.time = cdtime();
251 vl.values = values;
252 vl.values_len = STATIC_ARRAY_SIZE(values);
253 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
254 sstrncpy(vl.plugin, OVS_EVENTS_PLUGIN, sizeof(vl.plugin));
255 sstrncpy(vl.plugin_instance, link_name, sizeof(vl.plugin_instance));
256 sstrncpy(vl.type, "gauge", sizeof(vl.type));
257 sstrncpy(vl.type_instance, "link_status", sizeof(vl.type_instance));
258 plugin_dispatch_values(&vl);
259 }
261 /* Dispatch OVS DB terminate connection event to collectd */
262 static void
263 ovs_events_dispatch_terminate_notification(const char *msg)
264 {
265 notification_t n = {NOTIF_FAILURE, cdtime(), "", "", OVS_EVENTS_PLUGIN,
266 "", "", "", NULL};
267 ssnprintf(n.message, sizeof(n.message), msg);
268 sstrncpy(n.host, hostname_g, sizeof(n.host));
269 plugin_dispatch_notification(&n);
270 }
272 /* Process OVS DB update table event. It handles link status update event(s)
273 * and dispatches the value(s) to collectd if interface name matches one of
274 * interfaces specified in configuration file.
275 */
276 static void
277 ovs_events_table_update_cb(yajl_val jupdates)
278 {
279 yajl_val jnew_val = NULL;
280 yajl_val jupdate = NULL;
281 yajl_val jrow_update = NULL;
282 yajl_val jlink_name = NULL;
283 yajl_val jlink_state = NULL;
284 const char *link_name = NULL;
285 const char *link_state = NULL;
286 ovs_events_link_status_t link_status = UNKNOWN;
288 /* JSON "Interface" table update example:
289 * ---------------------------------
290 * {"Interface":
291 * {
292 * "9adf1db2-29ca-4140-ab22-ae347a4484de":
293 * {
294 * "new":
295 * {
296 * "name":"br0",
297 * "link_state":"up"
298 * },
299 * "old":
300 * {
301 * "link_state":"down"
302 * }
303 * }
304 * }
305 * }
306 */
307 if (!YAJL_IS_OBJECT(jupdates) || !(YAJL_GET_OBJECT(jupdates)->len > 0)) {
308 ERROR(OVS_EVENTS_PLUGIN ": unexpected OVS DB update event received");
309 return;
310 }
311 /* verify if this is a table event */
312 jupdate = YAJL_GET_OBJECT(jupdates)->values[0];
313 if (!YAJL_IS_OBJECT(jupdate)) {
314 ERROR(OVS_EVENTS_PLUGIN ": unexpected table update event received");
315 return;
316 }
317 /* go through all row updates */
318 for (int row_index = 0; row_index < YAJL_GET_OBJECT(jupdate)->len;
319 ++row_index) {
320 jrow_update = YAJL_GET_OBJECT(jupdate)->values[row_index];
322 /* check row update */
323 jnew_val = ovs_utils_get_value_by_key(jrow_update, "new");
324 if (jnew_val == NULL) {
325 ERROR(OVS_EVENTS_PLUGIN ": unexpected row update received");
326 return;
327 }
328 /* get link status update */
329 jlink_name = ovs_utils_get_value_by_key(jnew_val, "name");
330 jlink_state = ovs_utils_get_value_by_key(jnew_val, "link_state");
331 if (jlink_name && jlink_state) {
332 link_name = YAJL_GET_STRING(jlink_name);
333 if (link_name && ovs_events_config_iface_exists(link_name)) {
334 /* convert OVS table link state to link status */
335 if (YAJL_IS_STRING(jlink_state)) {
336 link_state = YAJL_GET_STRING(jlink_state);
337 if (strcmp(link_state, "up") == 0)
338 link_status = UP;
339 else if (strcmp(link_state, "down") == 0)
340 link_status = DOWN;
341 }
342 /* update link status in cache */
343 ovs_events_link_status_update(link_name, link_status);
344 if (ovs_events_ctx.config.send_notification)
345 /* dispatch notification */
346 ovs_events_dispatch_notification(link_name, link_status);
347 }
348 }
349 }
350 }
352 /* Process OVS DB result table callback. It handles init link status value
353 * and dispatches the value(s) to collectd. The logic to handle init status
354 * is same as 'ovs_events_table_update_cb'.
355 */
356 static void
357 ovs_events_table_result_cb(yajl_val jresult, yajl_val jerror)
358 {
359 (void)jerror;
360 /* jerror is not used as it is the same all the time
361 (rfc7047, "Monitor" section, return value) */
362 ovs_events_table_update_cb(jresult);
363 }
365 /* Setup OVS DB table callback. It subscribes to OVS DB 'Interface' table
366 * to receive link status event(s).
367 */
368 static void
369 ovs_events_conn_initialize(ovs_db_t *pdb)
370 {
371 int ret = 0;
372 const char tb_name[] = "Interface";
373 const char *columns[] = {"name", "link_state", NULL};
375 /* register the update callback */
376 ret = ovs_db_table_cb_register(pdb, tb_name, columns,
377 ovs_events_table_update_cb,
378 ovs_events_table_result_cb,
379 OVS_DB_TABLE_CB_FLAG_MODIFY |
380 OVS_DB_TABLE_CB_FLAG_INITIAL);
381 if (ret < 0) {
382 ERROR(OVS_EVENTS_PLUGIN ": register OVS DB update callback failed");
383 return;
384 }
386 DEBUG(OVS_EVENTS_PLUGIN ": OVS DB has been initialized");
387 }
389 /* OVS DB terminate connection notification callback */
390 static void
391 ovs_events_conn_terminate()
392 {
393 const char msg[] = "OVS DB connection has been lost";
394 if (ovs_events_ctx.config.send_notification)
395 ovs_events_dispatch_terminate_notification(msg);
396 WARNING(OVS_EVENTS_PLUGIN ": %s", msg);
397 OVS_EVENTS_CTX_LOCK {
398 /* update link status to UNKNOWN */
399 for (ovs_events_interface_info_t *iface = ovs_events_ctx.ifaces; iface;
400 iface = iface->next)
401 ovs_events_link_status_update(iface->name, UNKNOWN);
402 }
403 }
405 /* Read OVS DB interface link status callback */
406 static int
407 ovs_events_plugin_read(user_data_t *ud)
408 {
409 (void)ud; /* unused argument */
410 OVS_EVENTS_CTX_LOCK {
411 for (ovs_events_interface_info_t *iface = ovs_events_ctx.ifaces; iface;
412 iface = iface->next)
413 /* submit link status value */
414 ovs_events_link_status_submit(iface->name, iface->link_status);
415 }
416 return (0);
417 }
419 /* Initialize OVS plugin */
420 static int
421 ovs_events_plugin_init(void)
422 {
423 ovs_db_t *ovs_db = NULL;
424 ovs_db_callback_t cb = {.post_conn_init = ovs_events_conn_initialize,
425 .post_conn_terminate = ovs_events_conn_terminate};
427 /* Initialize the context mutex */
428 if (pthread_mutexattr_init(&ovs_events_ctx.mutex_attr) != 0) {
429 ERROR(OVS_EVENTS_PLUGIN ": init context mutex attribute failed");
430 return (-1);
431 }
432 pthread_mutexattr_settype(&ovs_events_ctx.mutex_attr,
433 PTHREAD_MUTEX_RECURSIVE);
434 if (pthread_mutex_init(&ovs_events_ctx.mutex, &ovs_events_ctx.mutex_attr) !=
435 0) {
436 ERROR(OVS_EVENTS_PLUGIN ": init context mutex failed");
437 goto ovs_events_failure;
438 }
440 /* set default OVS DB url */
441 if (ovs_events_ctx.config.ovs_db_server_url == NULL)
442 if ((ovs_events_ctx.config.ovs_db_server_url =
443 strdup(OVS_EVENTS_DEFAULT_OVS_DB_SERVER_URL)) == NULL) {
444 ERROR(OVS_EVENTS_PLUGIN ": fail to set default OVS DB URL");
445 goto ovs_events_failure;
446 }
447 DEBUG(OVS_EVENTS_PLUGIN ": OVS DB url = %s",
448 ovs_events_ctx.config.ovs_db_server_url);
450 /* initialize OVS DB */
451 ovs_db = ovs_db_init(ovs_events_ctx.config.ovs_db_server_url, &cb);
452 if (ovs_db == NULL) {
453 ERROR(OVS_EVENTS_PLUGIN ": fail to connect to OVS DB server");
454 goto ovs_events_failure;
455 }
457 /* store OVS DB handler */
458 OVS_EVENTS_CTX_LOCK {
459 ovs_events_ctx.ovs_db = ovs_db;
460 }
462 DEBUG(OVS_EVENTS_PLUGIN ": plugin has been initialized");
463 return (0);
465 ovs_events_failure:
466 ERROR(OVS_EVENTS_PLUGIN ": plugin initialize failed");
467 /* release allocated memory */
468 ovs_events_config_free();
469 /* destroy context mutex */
470 pthread_mutexattr_destroy(&ovs_events_ctx.mutex_attr);
471 pthread_mutex_destroy(&ovs_events_ctx.mutex);
472 return (-1);
473 }
475 /* Shutdown OVS plugin */
476 static int
477 ovs_events_plugin_shutdown(void)
478 {
479 /* release memory allocated for config */
480 ovs_events_config_free();
482 /* destroy OVS DB */
483 if (ovs_db_destroy(ovs_events_ctx.ovs_db))
484 ERROR(OVS_EVENTS_PLUGIN ": OVSDB object destroy failed");
486 /* destroy context mutex */
487 pthread_mutexattr_destroy(&ovs_events_ctx.mutex_attr);
488 pthread_mutex_destroy(&ovs_events_ctx.mutex);
490 DEBUG(OVS_EVENTS_PLUGIN ": plugin has been destroyed");
491 return (0);
492 }
494 /* Register OVS plugin callbacks */
495 void
496 module_register(void)
497 {
498 plugin_register_complex_config(OVS_EVENTS_PLUGIN, ovs_events_plugin_config);
499 plugin_register_init(OVS_EVENTS_PLUGIN, ovs_events_plugin_init);
500 plugin_register_complex_read(NULL, OVS_EVENTS_PLUGIN,
501 ovs_events_plugin_read, 0, NULL);
502 plugin_register_shutdown(OVS_EVENTS_PLUGIN, ovs_events_plugin_shutdown);
503 }