2 * collectd - src/ovs_link.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 */
29 #include "utils_ovs.h" /* OVS helpers */
31 #define OVS_LINK_PLUGIN "ovs_link"
32 #define OVS_LINK_DEFAULT_OVS_DB_SERVER_URL "tcp:127.0.0.1:6640"
33 #define OVS_LINK_CTX_LOCK for (int __i = ovs_link_ctx_lock(); __i != 0 ; \
34 __i = ovs_link_ctx_unlock())
35 #define OVS_LINK_CONFIG_ERROR(option) do { \
36 ERROR(OVS_LINK_PLUGIN ": read '%s' config option failed", option); \
37 goto failure; } while (0)
39 /* Link status type */
40 enum ovs_link_link_status_e {DOWN, UP, UNKNOWN};
41 typedef enum ovs_link_link_status_e ovs_link_link_status_t;
44 struct ovs_link_interface_info_s {
45 char *name; /* interface name */
46 ovs_link_link_status_t link_status; /* link status */
47 struct ovs_link_interface_info_s *next; /* next interface info */
49 typedef struct ovs_link_interface_info_s ovs_link_interface_info_t;
51 /* OVS link configuration data */
52 struct ovs_link_config_s {
53 _Bool send_notification; /* sent notification to collectd? */
54 char *ovs_db_server_url; /* OVS DB server URL */
56 typedef struct ovs_link_config_s ovs_link_config_t;
58 /* OVS link context type */
59 struct ovs_link_ctx_s {
60 pthread_mutex_t mutex; /* mutex to lock the context */
61 pthread_mutexattr_t mutex_attr; /* context mutex attribute */
62 ovs_db_t *ovs_db; /* pointer to OVS DB instance */
63 ovs_link_config_t config; /* plugin config */
64 ovs_link_interface_info_t *ifaces; /* interface info */
66 typedef struct ovs_link_ctx_s ovs_link_ctx_t;
71 static ovs_link_ctx_t ovs_link_ctx = {
72 .mutex = PTHREAD_MUTEX_INITIALIZER,
74 .send_notification = 0, /* do not send notification */
75 .ovs_db_server_url = NULL}, /* use default OVS DB URL */
79 /* This function is used only by "OVS_LINK_CTX_LOCK" define (see above).
80 * It always returns 1 when context is locked.
85 pthread_mutex_lock(&ovs_link_ctx.mutex);
89 /* This function is used only by "OVS_LINK_CTX_LOCK" define (see above).
90 * It always returns 0 when context is unlocked.
95 pthread_mutex_unlock(&ovs_link_ctx.mutex);
99 /* Update link status in OVS link context (cache) */
101 ovs_link_link_status_update(const char *name, ovs_link_link_status_t status)
104 for (ovs_link_interface_info_t *iface = ovs_link_ctx.ifaces; iface;
106 if (strcmp(iface->name, name) == 0)
107 iface->link_status = status;
111 /* Check if given interface name exists in configuration file. It
112 * returns 1 if exists otherwise 0. If no interfaces are configured,
116 ovs_link_config_iface_exists(const char *ifname)
120 if (!(rc = (ovs_link_ctx.ifaces == NULL))) {
121 for (ovs_link_interface_info_t *iface = ovs_link_ctx.ifaces; iface;
123 if (rc = (strcmp(ifname, iface->name) == 0))
130 /* Release memory allocated for configuration data */
132 ovs_link_config_free()
134 ovs_link_interface_info_t *del_iface = NULL;
136 sfree(ovs_link_ctx.config.ovs_db_server_url);
137 while (ovs_link_ctx.ifaces) {
138 del_iface = ovs_link_ctx.ifaces;
139 ovs_link_ctx.ifaces = ovs_link_ctx.ifaces->next;
140 free(del_iface->name);
146 /* Parse plugin configuration file and store the config
147 * in allocated memory. Returns negative value in case of error.
150 ovs_link_plugin_config(oconfig_item_t *ci)
152 ovs_link_interface_info_t *new_iface;
155 for (int i = 0; i < ci->children_num; i++) {
156 oconfig_item_t *child = ci->children + i;
157 if (strcasecmp("SendNotification", child->key) == 0) {
158 if (cf_util_get_boolean(child,
159 &ovs_link_ctx.config.send_notification) < 0)
160 OVS_LINK_CONFIG_ERROR(child->key);
161 } else if (strcasecmp("OvsDbServerUrl", child->key) == 0) {
162 if (cf_util_get_string(child,
163 &ovs_link_ctx.config.ovs_db_server_url) < 0)
164 OVS_LINK_CONFIG_ERROR(child->key);
165 } else if (strcasecmp("Interfaces", child->key) == 0) {
166 for (int j = 0; j < child->values_num; j++) {
167 /* check value type */
168 if (child->values[j].type != OCONFIG_TYPE_STRING) {
169 ERROR(OVS_LINK_PLUGIN
170 ": given interface name is not a string [idx=%d]", j);
174 if ((if_name = strdup(child->values[j].value.string)) == NULL) {
175 ERROR(OVS_LINK_PLUGIN " strdup() copy interface name fail");
178 if ((new_iface = malloc(sizeof(*new_iface))) == NULL) {
179 ERROR(OVS_LINK_PLUGIN ": malloc () copy interface name fail");
182 /* store interface name */
183 new_iface->name = if_name;
184 new_iface->link_status = UNKNOWN;
185 new_iface->next = ovs_link_ctx.ifaces;
186 ovs_link_ctx.ifaces = new_iface;
187 DEBUG(OVS_LINK_PLUGIN ": found monitored interface \"%s\"",
192 ERROR(OVS_LINK_PLUGIN ": option '%s' is not allowed here", child->key);
199 ovs_link_config_free();
203 /* Dispatch OVS interface link status event to collectd */
205 ovs_link_dispatch_notification(const char *link_name,
206 ovs_link_link_status_t link_status)
208 const char *msg_link_status = NULL;
209 notification_t n = {NOTIF_FAILURE, cdtime(), "", "", OVS_LINK_PLUGIN,
212 /* convert link status to message string */
213 switch (link_status) {
215 msg_link_status = "UP";
216 n.severity = NOTIF_OKAY;
219 msg_link_status = "DOWN";
220 n.severity = NOTIF_WARNING;
223 msg_link_status = "UNKNOWN";;
227 /* fill the notification data */
228 ssnprintf(n.message, sizeof(n.message),
229 "link state of \"%s\" interface has been changed to \"%s\"",
230 link_name, msg_link_status);
231 sstrncpy(n.host, hostname_g, sizeof(n.host));
232 sstrncpy(n.plugin_instance, link_name, sizeof(n.plugin_instance));
233 sstrncpy(n.type, "gauge", sizeof(n.type));
234 sstrncpy(n.type_instance, "link_status", sizeof(n.type_instance));
235 return plugin_dispatch_notification(&n);
238 /* Dispatch OVS interface link status value to collectd */
240 ovs_link_link_status_submit(const char *link_name,
241 ovs_link_link_status_t link_status)
244 value_list_t vl = VALUE_LIST_INIT;
246 values[0].gauge = (gauge_t) link_status;
249 vl.values_len = STATIC_ARRAY_SIZE(values);
250 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
251 sstrncpy(vl.plugin, OVS_LINK_PLUGIN, sizeof(vl.plugin));
252 sstrncpy(vl.plugin_instance, link_name, sizeof(vl.plugin_instance));
253 sstrncpy(vl.type, "gauge", sizeof(vl.type));
254 sstrncpy(vl.type_instance, "link_status", sizeof(vl.type_instance));
255 plugin_dispatch_values(&vl);
258 /* Dispatch OVS DB terminate connection event to collectd */
260 ovs_link_dispatch_terminate_notification(const char *msg)
262 notification_t n = {NOTIF_FAILURE, cdtime(), "", "", OVS_LINK_PLUGIN,
264 ssnprintf(n.message, sizeof(n.message), msg);
265 sstrncpy(n.host, hostname_g, sizeof(n.host));
266 plugin_dispatch_notification(&n);
269 /* Process OVS DB update table event. It handles link status update event(s)
270 * and dispatches the value(s) to collectd if interface name matches one of
271 * interfaces specified in configuration file.
274 ovs_link_table_update_cb(yajl_val jupdates)
276 yajl_val jnew_val = NULL;
277 yajl_val jupdate = NULL;
278 yajl_val jrow_update = NULL;
279 yajl_val jlink_name = NULL;
280 yajl_val jlink_state = NULL;
281 const char *link_name = NULL;
282 const char *link_state = NULL;
283 ovs_link_link_status_t link_status = UNKNOWN;
285 /* JSON "Interface" table update example:
286 * ---------------------------------
289 * "9adf1db2-29ca-4140-ab22-ae347a4484de":
298 * "link_state":"down"
304 if (!YAJL_IS_OBJECT(jupdates) || !(YAJL_GET_OBJECT(jupdates)->len > 0)) {
305 ERROR(OVS_LINK_PLUGIN ": unexpected OVS DB update event received");
308 /* verify if this is a table event */
309 jupdate = YAJL_GET_OBJECT(jupdates)->values[0];
310 if (!YAJL_IS_OBJECT(jupdate)) {
311 ERROR(OVS_LINK_PLUGIN ": unexpected table update event received");
314 /* go through all row updates */
315 for (int row_index = 0; row_index < YAJL_GET_OBJECT(jupdate)->len;
317 jrow_update = YAJL_GET_OBJECT(jupdate)->values[row_index];
319 /* check row update */
320 jnew_val = ovs_utils_get_value_by_key(jrow_update, "new");
321 if (jnew_val == NULL) {
322 ERROR(OVS_LINK_PLUGIN ": unexpected row update received");
325 /* get link status update */
326 jlink_name = ovs_utils_get_value_by_key(jnew_val, "name");
327 jlink_state = ovs_utils_get_value_by_key(jnew_val, "link_state");
328 if (jlink_name && jlink_state) {
329 link_name = YAJL_GET_STRING(jlink_name);
330 if (link_name && ovs_link_config_iface_exists(link_name)) {
331 /* convert OVS table link state to link status */
332 if (YAJL_IS_STRING(jlink_state)) {
333 link_state = YAJL_GET_STRING(jlink_state);
334 if (strcmp(link_state, "up") == 0)
336 else if (strcmp(link_state, "down") == 0)
339 /* update link status in cache */
340 ovs_link_link_status_update(link_name, link_status);
341 if (ovs_link_ctx.config.send_notification)
342 /* dispatch notification */
343 ovs_link_dispatch_notification(link_name, link_status);
349 /* Process OVS DB result table callback. It handles init link status value
350 * and dispatches the value(s) to collectd. The logic to handle init status
351 * is same as 'ovs_link_table_update_cb'.
354 ovs_link_table_result_cb(yajl_val jresult, yajl_val jerror)
357 /* jerror is not used as it is the same all the time
358 (rfc7047, "Monitor" section, return value) */
359 ovs_link_table_update_cb(jresult);
362 /* Setup OVS DB table callback. It subscribes to OVS DB 'Interface' table
363 * to receive link status event(s).
366 ovs_link_conn_initialize(ovs_db_t *pdb)
369 const char tb_name[] = "Interface";
370 const char *columns[] = {"name", "link_state", NULL};
372 /* register the update callback */
373 ret = ovs_db_table_cb_register(pdb, tb_name, columns,
374 ovs_link_table_update_cb,
375 ovs_link_table_result_cb,
376 OVS_DB_TABLE_CB_FLAG_MODIFY |
377 OVS_DB_TABLE_CB_FLAG_INITIAL);
379 ERROR(OVS_LINK_PLUGIN ": register OVS DB update callback failed");
383 DEBUG(OVS_LINK_PLUGIN ": OVS DB has been initialized");
386 /* OVS DB terminate connection notification callback */
388 ovs_link_conn_terminate()
390 const char msg[] = "OVS DB connection has been lost";
391 if (ovs_link_ctx.config.send_notification)
392 ovs_link_dispatch_terminate_notification(msg);
393 WARNIG(OVS_LINK_PLUGIN ": %s", msg);
395 /* update link status to UNKNOWN */
396 for (ovs_link_interface_info_t *iface = ovs_link_ctx.ifaces; iface;
398 ovs_link_link_status_update(iface->name, UNKNOWN);
402 /* Read OVS link status plugin callback */
404 ovs_link_plugin_read(user_data_t *ud)
406 (void)ud; /* unused argument */
408 for (ovs_link_interface_info_t *iface = ovs_link_ctx.ifaces; iface;
410 /* submit link status value */
411 ovs_link_link_status_submit(iface->name, iface->link_status);
416 /* Initialize OVS plugin */
418 ovs_link_plugin_init(void)
420 ovs_db_t *ovs_db = NULL;
421 ovs_db_callback_t cb = {.post_conn_init = ovs_link_conn_initialize,
422 .post_conn_terminate = ovs_link_conn_terminate};
424 /* Initialize the context mutex */
425 if (pthread_mutexattr_init(&ovs_link_ctx.mutex_attr) != 0) {
426 ERROR(OVS_LINK_PLUGIN ": init context mutex attribute failed");
429 pthread_mutexattr_settype(&ovs_link_ctx.mutex_attr,
430 PTHREAD_MUTEX_RECURSIVE);
431 if (pthread_mutex_init(&ovs_link_ctx.mutex, &ovs_link_ctx.mutex_attr) != 0) {
432 ERROR(OVS_LINK_PLUGIN ": init context mutex failed");
433 goto ovs_link_failure;
436 /* set default OVS DB url */
437 if (ovs_link_ctx.config.ovs_db_server_url == NULL)
438 if ((ovs_link_ctx.config.ovs_db_server_url =
439 strdup(OVS_LINK_DEFAULT_OVS_DB_SERVER_URL)) == NULL) {
440 ERROR(OVS_LINK_PLUGIN ": fail to set default OVS DB URL");
441 goto ovs_link_failure;
443 DEBUG(OVS_LINK_PLUGIN ": OVS DB url = %s",
444 ovs_link_ctx.config.ovs_db_server_url);
446 /* initialize OVS DB */
447 ovs_db = ovs_db_init(ovs_link_ctx.config.ovs_db_server_url, &cb);
448 if (ovs_db == NULL) {
449 ERROR(OVS_LINK_PLUGIN ": fail to connect to OVS DB server");
450 goto ovs_link_failure;
453 /* store OVS DB handler */
455 ovs_link_ctx.ovs_db = ovs_db;
458 DEBUG(OVS_LINK_PLUGIN ": plugin has been initialized");
462 ERROR(OVS_LINK_PLUGIN ": plugin initialize failed");
463 /* release allocated memory */
464 ovs_link_config_free();
465 /* destroy context mutex */
466 pthread_mutexattr_destroy(&ovs_link_ctx.mutex_attr);
467 pthread_mutex_destroy(&ovs_link_ctx.mutex);
471 /* Shutdown OVS plugin */
473 ovs_link_plugin_shutdown(void)
475 /* release memory allocated for config */
476 ovs_link_config_free();
479 if (ovs_db_destroy(ovs_link_ctx.ovs_db))
480 ERROR(OVS_LINK_PLUGIN ": OVSDB object destroy failed");
482 /* destroy context mutex */
483 pthread_mutexattr_destroy(&ovs_link_ctx.mutex_attr);
484 pthread_mutex_destroy(&ovs_link_ctx.mutex);
486 DEBUG(OVS_LINK_PLUGIN ": plugin has been destroyed");
490 /* Register OVS plugin callbacks */
492 module_register(void)
494 plugin_register_complex_config(OVS_LINK_PLUGIN, ovs_link_plugin_config);
495 plugin_register_init(OVS_LINK_PLUGIN, ovs_link_plugin_init);
496 plugin_register_complex_read(NULL, OVS_LINK_PLUGIN, ovs_link_plugin_read,
498 plugin_register_shutdown(OVS_LINK_PLUGIN, ovs_link_plugin_shutdown);