2 * collectd - src/ovs_events.c
4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
25 * Volodymyr Mytnyk <volodymyrx.mytnyk@intel.com>
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;
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 */
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 */
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 */
67 typedef struct ovs_events_ctx_s ovs_events_ctx_t;
72 static ovs_events_ctx_t ovs_events_ctx = {
73 .mutex = PTHREAD_MUTEX_INITIALIZER,
75 .send_notification = 0, /* do not send notification */
76 .ovs_db_server_url = NULL}, /* use default OVS DB URL */
80 /* This function is used only by "OVS_EVENTS_CTX_LOCK" define (see above).
81 * It always returns 1 when context is locked.
86 pthread_mutex_lock(&ovs_events_ctx.mutex);
90 /* This function is used only by "OVS_EVENTS_CTX_LOCK" define (see above).
91 * It always returns 0 when context is unlocked.
94 ovs_events_ctx_unlock()
96 pthread_mutex_unlock(&ovs_events_ctx.mutex);
100 /* Update link status in OVS events context (cache) */
102 ovs_events_link_status_update(const char *name,
103 ovs_events_link_status_t status)
105 OVS_EVENTS_CTX_LOCK {
106 for (ovs_events_interface_info_t *iface = ovs_events_ctx.ifaces; iface;
108 if (strcmp(iface->name, name) == 0)
109 iface->link_status = status;
113 /* Check if given interface name exists in configuration file. It
114 * returns 1 if exists otherwise 0. If no interfaces are configured,
118 ovs_events_config_iface_exists(const char *ifname)
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;
125 if (rc = (strcmp(ifname, iface->name) == 0))
132 /* Release memory allocated for configuration data */
134 ovs_events_config_free()
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);
148 /* Parse plugin configuration file and store the config
149 * in allocated memory. Returns negative value in case of error.
152 ovs_events_plugin_config(oconfig_item_t *ci)
154 ovs_events_interface_info_t *new_iface;
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);
176 if ((if_name = strdup(child->values[j].value.string)) == NULL) {
177 ERROR(OVS_EVENTS_PLUGIN " strdup() copy interface name fail");
180 if ((new_iface = malloc(sizeof(*new_iface))) == NULL) {
181 ERROR(OVS_EVENTS_PLUGIN ": malloc () copy interface name fail");
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\"",
194 ERROR(OVS_EVENTS_PLUGIN ": option '%s' is not allowed here",
202 ovs_events_config_free();
206 /* Dispatch OVS interface link status event to collectd */
208 ovs_events_dispatch_notification(const char *link_name,
209 ovs_events_link_status_t link_status)
211 const char *msg_link_status = NULL;
212 notification_t n = {NOTIF_FAILURE, cdtime(), "", "", OVS_EVENTS_PLUGIN,
215 /* convert link status to message string */
216 switch (link_status) {
218 msg_link_status = "UP";
219 n.severity = NOTIF_OKAY;
222 msg_link_status = "DOWN";
223 n.severity = NOTIF_WARNING;
226 msg_link_status = "UNKNOWN";;
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);
241 /* Dispatch OVS interface link status value to collectd */
243 ovs_events_link_status_submit(const char *link_name,
244 ovs_events_link_status_t link_status)
247 value_list_t vl = VALUE_LIST_INIT;
249 values[0].gauge = (gauge_t) link_status;
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);
261 /* Dispatch OVS DB terminate connection event to collectd */
263 ovs_events_dispatch_terminate_notification(const char *msg)
265 notification_t n = {NOTIF_FAILURE, cdtime(), "", "", OVS_EVENTS_PLUGIN,
267 ssnprintf(n.message, sizeof(n.message), msg);
268 sstrncpy(n.host, hostname_g, sizeof(n.host));
269 plugin_dispatch_notification(&n);
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.
277 ovs_events_table_update_cb(yajl_val jupdates)
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 * ---------------------------------
292 * "9adf1db2-29ca-4140-ab22-ae347a4484de":
301 * "link_state":"down"
307 if (!YAJL_IS_OBJECT(jupdates) || !(YAJL_GET_OBJECT(jupdates)->len > 0)) {
308 ERROR(OVS_EVENTS_PLUGIN ": unexpected OVS DB update event received");
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");
317 /* go through all row updates */
318 for (int row_index = 0; row_index < YAJL_GET_OBJECT(jupdate)->len;
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");
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)
339 else if (strcmp(link_state, "down") == 0)
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);
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'.
357 ovs_events_table_result_cb(yajl_val jresult, yajl_val 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);
365 /* Setup OVS DB table callback. It subscribes to OVS DB 'Interface' table
366 * to receive link status event(s).
369 ovs_events_conn_initialize(ovs_db_t *pdb)
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);
382 ERROR(OVS_EVENTS_PLUGIN ": register OVS DB update callback failed");
386 DEBUG(OVS_EVENTS_PLUGIN ": OVS DB has been initialized");
389 /* OVS DB terminate connection notification callback */
391 ovs_events_conn_terminate()
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;
401 ovs_events_link_status_update(iface->name, UNKNOWN);
405 /* Read OVS DB interface link status callback */
407 ovs_events_plugin_read(user_data_t *ud)
409 (void)ud; /* unused argument */
410 OVS_EVENTS_CTX_LOCK {
411 for (ovs_events_interface_info_t *iface = ovs_events_ctx.ifaces; iface;
413 /* submit link status value */
414 ovs_events_link_status_submit(iface->name, iface->link_status);
419 /* Initialize OVS plugin */
421 ovs_events_plugin_init(void)
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");
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) !=
436 ERROR(OVS_EVENTS_PLUGIN ": init context mutex failed");
437 goto ovs_events_failure;
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;
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;
457 /* store OVS DB handler */
458 OVS_EVENTS_CTX_LOCK {
459 ovs_events_ctx.ovs_db = ovs_db;
462 DEBUG(OVS_EVENTS_PLUGIN ": plugin has been initialized");
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);
475 /* Shutdown OVS plugin */
477 ovs_events_plugin_shutdown(void)
479 /* release memory allocated for config */
480 ovs_events_config_free();
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");
494 /* Register OVS plugin callbacks */
496 module_register(void)
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);