7521b280d2ec1769e538b7bc6c86dcd9e1bac0a6
[collectd.git] / src / ovs_events.c
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  **/
27
28 #include "collectd.h"
29
30 #include "common.h" /* auxiliary functions */
31
32 #include "utils_ovs.h" /* OVS helpers */
33
34 #define OVS_EVENTS_IFACE_NAME_SIZE 128
35 #define OVS_EVENTS_IFACE_UUID_SIZE 64
36 #define OVS_EVENTS_EXT_IFACE_ID_SIZE 64
37 #define OVS_EVENTS_EXT_VM_UUID_SIZE 64
38 #define OVS_EVENTS_PLUGIN "ovs_events"
39 #define OVS_EVENTS_CTX_LOCK                                                    \
40   for (int __i = ovs_events_ctx_lock(); __i != 0; __i = ovs_events_ctx_unlock())
41 #define OVS_EVENTS_CONFIG_ERROR(option)                                        \
42   do {                                                                         \
43     ERROR(OVS_EVENTS_PLUGIN ": read '%s' config option failed", option);       \
44     goto failure;                                                              \
45   } while (0)
46
47 /* Link status type */
48 enum ovs_events_link_status_e { DOWN, UP };
49 typedef enum ovs_events_link_status_e ovs_events_link_status_t;
50
51 /* Interface info */
52 struct ovs_events_iface_info_s {
53   char name[OVS_EVENTS_IFACE_NAME_SIZE];           /* interface name */
54   char uuid[OVS_EVENTS_IFACE_UUID_SIZE];           /* interface UUID */
55   char ext_iface_id[OVS_EVENTS_EXT_IFACE_ID_SIZE]; /* external interface id */
56   char ext_vm_uuid[OVS_EVENTS_EXT_VM_UUID_SIZE];   /* external VM UUID */
57   ovs_events_link_status_t link_status;            /* interface link status */
58   struct ovs_events_iface_info_s *next;            /* next interface info */
59 };
60 typedef struct ovs_events_iface_info_s ovs_events_iface_info_t;
61
62 /* Interface list */
63 struct ovs_events_iface_list_s {
64   char name[OVS_EVENTS_IFACE_NAME_SIZE]; /* interface name */
65   struct ovs_events_iface_list_s *next;  /* next interface info */
66 };
67 typedef struct ovs_events_iface_list_s ovs_events_iface_list_t;
68
69 /* OVS events configuration data */
70 struct ovs_events_config_s {
71   _Bool send_notification;                 /* sent notification to collectd? */
72   char ovs_db_node[OVS_DB_ADDR_NODE_SIZE]; /* OVS DB node */
73   char ovs_db_serv[OVS_DB_ADDR_SERVICE_SIZE]; /* OVS DB service */
74   ovs_events_iface_list_t *ifaces;            /* interface info */
75 };
76 typedef struct ovs_events_config_s ovs_events_config_t;
77
78 /* OVS events context type */
79 struct ovs_events_ctx_s {
80   pthread_mutex_t mutex;      /* mutex to lock the context */
81   ovs_db_t *ovs_db;           /* pointer to OVS DB instance */
82   ovs_events_config_t config; /* plugin config */
83   char *ovs_db_select_params; /* OVS DB select parameter request */
84   _Bool is_db_available;      /* specify whether OVS DB is available */
85 };
86 typedef struct ovs_events_ctx_s ovs_events_ctx_t;
87
88 /*
89  * Private variables
90  */
91 static ovs_events_ctx_t ovs_events_ctx = {
92     .mutex = PTHREAD_MUTEX_INITIALIZER,
93     .config = {.send_notification = 0,     /* do not send notification */
94                .ovs_db_node = "localhost", /* use default OVS DB node */
95                .ovs_db_serv = "6640",      /* use default OVS DB service */
96                .ifaces = NULL},
97     .ovs_db_select_params = NULL,
98     .is_db_available = 0,
99     .ovs_db = NULL};
100
101 /* This function is used only by "OVS_EVENTS_CTX_LOCK" define (see above).
102  * It always returns 1 when context is locked.
103  */
104 static int ovs_events_ctx_lock() {
105   pthread_mutex_lock(&ovs_events_ctx.mutex);
106   return (1);
107 }
108
109 /* This function is used only by "OVS_EVENTS_CTX_LOCK" define (see above).
110  * It always returns 0 when context is unlocked.
111  */
112 static int ovs_events_ctx_unlock() {
113   pthread_mutex_unlock(&ovs_events_ctx.mutex);
114   return (0);
115 }
116
117 /* Check if given interface name exists in configuration file. It
118  * returns 1 if exists otherwise 0. If no interfaces are configured,
119  * -1 is returned
120  */
121 static int ovs_events_config_iface_exists(const char *ifname) {
122   if (ovs_events_ctx.config.ifaces == NULL)
123     return -1;
124
125   /* check if given interface exists */
126   for (ovs_events_iface_list_t *iface = ovs_events_ctx.config.ifaces; iface;
127        iface = iface->next)
128     return (strcmp(ifname, iface->name) == 0);
129
130   return 0;
131 }
132
133 /* Get OVS DB select parameter request based on rfc7047,
134  * "Transact" & "Select" section
135  */
136 static char *ovs_events_get_select_params() {
137   int ret = 0;
138   size_t buff_size = 0;
139   size_t offset = 0;
140   char *buff = NULL;
141   char *new_buff = NULL;
142   const char params_fmt[] = "[\"Open_vSwitch\"%s]";
143   const char option_fmt[] = ",{\"op\":\"select\",\"table\":\"Interface\","
144                             "\"where\":[[\"name\",\"==\",\"%s\"]],"
145                             "\"columns\":[\"link_state\",\"external_ids\","
146                             "\"name\",\"_uuid\"]}";
147   const char default_opt[] = ",{\"op\":\"select\",\"table\":\"Interface\","
148                              "\"where\":[],\"columns\":[\"link_state\","
149                              "\"external_ids\",\"name\",\"_uuid\"]}";
150   /* setup OVS DB interface condition */
151   for (ovs_events_iface_list_t *iface = ovs_events_ctx.config.ifaces; iface;
152        iface = iface->next, offset += ret) {
153     /* allocate new buffer (format size + ifname len is good enough) */
154     buff_size += (sizeof(option_fmt) + strlen(iface->name));
155     new_buff = realloc(buff, buff_size);
156     if (new_buff == NULL)
157       goto failure;
158     buff = new_buff;
159     ret = ssnprintf(buff + offset, buff_size, option_fmt, iface->name);
160     if (ret < 0)
161       goto failure;
162   }
163   /* if no interfaces are configured, use default params */
164   if (buff == NULL) {
165     buff = strdup(default_opt);
166     offset = strlen(default_opt);
167   }
168
169   /* allocate memory for OVS DB select params */
170   buff_size = offset + sizeof(params_fmt);
171   new_buff = malloc(buff_size);
172   if (new_buff == NULL)
173     goto failure;
174
175   /* create OVS DB select params */
176   if (ssnprintf(new_buff, buff_size, params_fmt, buff) < 0)
177     goto failure;
178
179   sfree(buff);
180   return new_buff;
181
182 failure:
183   sfree(new_buff);
184   sfree(buff);
185   return NULL;
186 }
187
188 /* Release memory allocated for configuration data */
189 static void ovs_events_config_free() {
190   ovs_events_iface_list_t *del_iface = NULL;
191   sfree(ovs_events_ctx.ovs_db_select_params);
192   while (ovs_events_ctx.config.ifaces) {
193     del_iface = ovs_events_ctx.config.ifaces;
194     ovs_events_ctx.config.ifaces = ovs_events_ctx.config.ifaces->next;
195     sfree(del_iface);
196   }
197 }
198
199 /* Parse plugin configuration file and store the config
200  * in allocated memory. Returns negative value in case of error.
201  */
202 static int ovs_events_plugin_config(oconfig_item_t *ci) {
203   ovs_events_iface_list_t *new_iface;
204
205   for (int i = 0; i < ci->children_num; i++) {
206     oconfig_item_t *child = ci->children + i;
207     if (strcasecmp("SendNotification", child->key) == 0) {
208       if (cf_util_get_boolean(child, &ovs_events_ctx.config.send_notification) <
209           0)
210         OVS_EVENTS_CONFIG_ERROR(child->key);
211     } else if (strcasecmp("OvsDbAddress", child->key) == 0) {
212       if (child->values_num < 1) {
213         ERROR(OVS_EVENTS_PLUGIN ": invalid OVS DB address specified");
214         goto failure;
215       }
216       /* check node type and get the value */
217       if (child->values[0].type != OCONFIG_TYPE_STRING) {
218         ERROR(OVS_EVENTS_PLUGIN ": OVS DB node is not a string");
219         goto failure;
220       }
221       sstrncpy(ovs_events_ctx.config.ovs_db_node, child->values[0].value.string,
222                sizeof(ovs_events_ctx.config.ovs_db_node));
223       /* get OVS DB address service name (optional) */
224       if (child->values_num > 1) {
225         if (child->values[1].type != OCONFIG_TYPE_STRING) {
226           ERROR(OVS_EVENTS_PLUGIN ": OVS DB service is not a string");
227           goto failure;
228         }
229         sstrncpy(ovs_events_ctx.config.ovs_db_serv,
230                  child->values[1].value.string,
231                  sizeof(ovs_events_ctx.config.ovs_db_serv));
232       }
233     } else if (strcasecmp("Interfaces", child->key) == 0) {
234       for (int j = 0; j < child->values_num; j++) {
235         /* check value type */
236         if (child->values[j].type != OCONFIG_TYPE_STRING) {
237           ERROR(OVS_EVENTS_PLUGIN
238                 ": given interface name is not a string [idx=%d]",
239                 j);
240           goto failure;
241         }
242         /* allocate memory for configured interface */
243         if ((new_iface = malloc(sizeof(*new_iface))) == NULL) {
244           ERROR(OVS_EVENTS_PLUGIN ": malloc () copy interface name fail");
245           goto failure;
246         } else {
247           /* store interface name */
248           sstrncpy(new_iface->name, child->values[j].value.string,
249                    sizeof(new_iface->name));
250           new_iface->next = ovs_events_ctx.config.ifaces;
251           ovs_events_ctx.config.ifaces = new_iface;
252           DEBUG(OVS_EVENTS_PLUGIN ": found monitored interface \"%s\"",
253                 new_iface->name);
254         }
255       }
256     } else {
257       ERROR(OVS_EVENTS_PLUGIN ": option '%s' is not allowed here", child->key);
258       goto failure;
259     }
260   }
261   return (0);
262
263 failure:
264   ovs_events_config_free();
265   return (-1);
266 }
267
268 /* Dispatch OVS interface link status event to collectd */
269 static void
270 ovs_events_dispatch_notification(const ovs_events_iface_info_t *ifinfo) {
271   const char *msg_link_status = NULL;
272   notification_t n = {
273       NOTIF_FAILURE, cdtime(), "", "", OVS_EVENTS_PLUGIN, "", "", "", NULL};
274
275   /* convert link status to message string */
276   switch (ifinfo->link_status) {
277   case UP:
278     msg_link_status = "UP";
279     n.severity = NOTIF_OKAY;
280     break;
281   case DOWN:
282     msg_link_status = "DOWN";
283     n.severity = NOTIF_WARNING;
284     break;
285   default:
286     ERROR(OVS_EVENTS_PLUGIN ": unknown interface link status");
287     return;
288   }
289
290   /* add interface metadata to the notification */
291   if (plugin_notification_meta_add_string(&n, "uuid", ifinfo->uuid) < 0) {
292     ERROR(OVS_EVENTS_PLUGIN ": add interface uuid meta data failed");
293     return;
294   }
295
296   if (strlen(ifinfo->ext_vm_uuid) > 0)
297     if (plugin_notification_meta_add_string(&n, "vm-uuid",
298                                             ifinfo->ext_vm_uuid) < 0) {
299       ERROR(OVS_EVENTS_PLUGIN ": add interface vm-uuid meta data failed");
300       return;
301     }
302
303   if (strlen(ifinfo->ext_iface_id) > 0)
304     if (plugin_notification_meta_add_string(&n, "iface-id",
305                                             ifinfo->ext_iface_id) < 0) {
306       ERROR(OVS_EVENTS_PLUGIN ": add interface iface-id meta data failed");
307       return;
308     }
309
310   /* fill the notification data */
311   ssnprintf(n.message, sizeof(n.message),
312             "link state of \"%s\" interface has been changed to \"%s\"",
313             ifinfo->name, msg_link_status);
314   sstrncpy(n.host, hostname_g, sizeof(n.host));
315   sstrncpy(n.plugin_instance, ifinfo->name, sizeof(n.plugin_instance));
316   sstrncpy(n.type, "gauge", sizeof(n.type));
317   sstrncpy(n.type_instance, "link_status", sizeof(n.type_instance));
318   plugin_dispatch_notification(&n);
319 }
320
321 /* Dispatch OVS interface link status value to collectd */
322 static void
323 ovs_events_link_status_submit(const ovs_events_iface_info_t *ifinfo) {
324   value_list_t vl = VALUE_LIST_INIT;
325   meta_data_t *meta = NULL;
326
327   /* add interface metadata to the submit value */
328   if ((meta = meta_data_create()) != NULL) {
329     if (meta_data_add_string(meta, "uuid", ifinfo->uuid) < 0)
330       ERROR(OVS_EVENTS_PLUGIN ": add interface uuid meta data failed");
331
332     if (strlen(ifinfo->ext_vm_uuid) > 0)
333       if (meta_data_add_string(meta, "vm-uuid", ifinfo->ext_vm_uuid) < 0)
334         ERROR(OVS_EVENTS_PLUGIN ": add interface vm-uuid meta data failed");
335
336     if (strlen(ifinfo->ext_iface_id) > 0)
337       if (meta_data_add_string(meta, "iface-id", ifinfo->ext_iface_id) < 0)
338         ERROR(OVS_EVENTS_PLUGIN ": add interface iface-id meta data failed");
339     vl.meta = meta;
340   } else
341     ERROR(OVS_EVENTS_PLUGIN ": create metadata failed");
342
343   vl.time = cdtime();
344   vl.values = &(value_t){.gauge = (gauge_t)ifinfo->link_status};
345   vl.values_len = 1;
346   sstrncpy(vl.plugin, OVS_EVENTS_PLUGIN, sizeof(vl.plugin));
347   sstrncpy(vl.plugin_instance, ifinfo->name, sizeof(vl.plugin_instance));
348   sstrncpy(vl.type, "gauge", sizeof(vl.type));
349   sstrncpy(vl.type_instance, "link_status", sizeof(vl.type_instance));
350   plugin_dispatch_values(&vl);
351   meta_data_destroy(meta);
352 }
353
354 /* Dispatch OVS DB terminate connection event to collectd */
355 static void ovs_events_dispatch_terminate_notification(const char *msg) {
356   notification_t n = {
357       NOTIF_FAILURE, cdtime(), "", "", OVS_EVENTS_PLUGIN, "", "", "", NULL};
358   sstrncpy(n.message, msg, sizeof(n.message));
359   sstrncpy(n.host, hostname_g, sizeof(n.host));
360   plugin_dispatch_notification(&n);
361 }
362
363 /* Get OVS DB interface information and stores it into
364  * ovs_events_iface_info_t structure */
365 static int ovs_events_get_iface_info(yajl_val jobject,
366                                      ovs_events_iface_info_t *ifinfo) {
367   yajl_val jexternal_ids = NULL;
368   yajl_val jvalue = NULL;
369   yajl_val juuid = NULL;
370   const char *state = NULL;
371
372   /* check YAJL type */
373   if (!YAJL_IS_OBJECT(jobject))
374     return (-1);
375
376   /* try to find external_ids, name and link_state fields */
377   jexternal_ids = ovs_utils_get_value_by_key(jobject, "external_ids");
378   if (jexternal_ids == NULL || ifinfo == NULL)
379     return (-1);
380
381   /* get iface-id from external_ids field */
382   jvalue = ovs_utils_get_map_value(jexternal_ids, "iface-id");
383   if (jvalue != NULL && YAJL_IS_STRING(jvalue))
384     sstrncpy(ifinfo->ext_iface_id, YAJL_GET_STRING(jvalue),
385              sizeof(ifinfo->ext_iface_id));
386
387   /* get vm-uuid from external_ids field */
388   jvalue = ovs_utils_get_map_value(jexternal_ids, "vm-uuid");
389   if (jvalue != NULL && YAJL_IS_STRING(jvalue))
390     sstrncpy(ifinfo->ext_vm_uuid, YAJL_GET_STRING(jvalue),
391              sizeof(ifinfo->ext_vm_uuid));
392
393   /* get interface uuid */
394   jvalue = ovs_utils_get_value_by_key(jobject, "_uuid");
395   if (jvalue == NULL || !YAJL_IS_ARRAY(jvalue) ||
396       YAJL_GET_ARRAY(jvalue)->len != 2)
397     return (-1);
398   juuid = YAJL_GET_ARRAY(jvalue)->values[1];
399   if (juuid == NULL || !YAJL_IS_STRING(juuid))
400     return (-1);
401   sstrncpy(ifinfo->uuid, YAJL_GET_STRING(juuid), sizeof(ifinfo->uuid));
402
403   /* get interface name */
404   jvalue = ovs_utils_get_value_by_key(jobject, "name");
405   if (jvalue == NULL || !YAJL_IS_STRING(jvalue))
406     return (-1);
407   sstrncpy(ifinfo->name, YAJL_GET_STRING(jvalue), sizeof(ifinfo->name));
408
409   /* get OVS DB interface link status */
410   jvalue = ovs_utils_get_value_by_key(jobject, "link_state");
411   if (jvalue != NULL && ((state = YAJL_GET_STRING(jvalue)) != NULL)) {
412     /* convert OVS table link state to link status */
413     if (strcmp(state, "up") == 0)
414       ifinfo->link_status = UP;
415     else if (strcmp(state, "down") == 0)
416       ifinfo->link_status = DOWN;
417   }
418   return (0);
419 }
420
421 /* Process OVS DB update table event. It handles link status update event(s)
422  * and dispatches the value(s) to collectd if interface name matches one of
423  * interfaces specified in configuration file.
424  */
425 static void ovs_events_table_update_cb(yajl_val jupdates) {
426   yajl_val jnew_val = NULL;
427   yajl_val jupdate = NULL;
428   yajl_val jrow_update = NULL;
429   ovs_events_iface_info_t ifinfo;
430
431   /* JSON "Interface" table update example:
432    * ---------------------------------
433    * {"Interface":
434    *  {
435    *   "9adf1db2-29ca-4140-ab22-ae347a4484de":
436    *    {
437    *     "new":
438    *      {
439    *       "name":"br0",
440    *       "link_state":"up"
441    *      },
442    *     "old":
443    *      {
444    *       "link_state":"down"
445    *      }
446    *    }
447    *  }
448    * }
449    */
450   if (!YAJL_IS_OBJECT(jupdates) || !(YAJL_GET_OBJECT(jupdates)->len > 0)) {
451     ERROR(OVS_EVENTS_PLUGIN ": unexpected OVS DB update event received");
452     return;
453   }
454   /* verify if this is a table event */
455   jupdate = YAJL_GET_OBJECT(jupdates)->values[0];
456   if (!YAJL_IS_OBJECT(jupdate)) {
457     ERROR(OVS_EVENTS_PLUGIN ": unexpected table update event received");
458     return;
459   }
460   /* go through all row updates  */
461   for (int row_index = 0; row_index < YAJL_GET_OBJECT(jupdate)->len;
462        ++row_index) {
463     jrow_update = YAJL_GET_OBJECT(jupdate)->values[row_index];
464
465     /* check row update */
466     jnew_val = ovs_utils_get_value_by_key(jrow_update, "new");
467     if (jnew_val == NULL) {
468       ERROR(OVS_EVENTS_PLUGIN ": unexpected row update received");
469       return;
470     }
471     /* get OVS DB interface information */
472     if (ovs_events_get_iface_info(jnew_val, &ifinfo) < 0) {
473       ERROR(OVS_EVENTS_PLUGIN
474             " :unexpected interface information data received");
475       return;
476     }
477     if (ovs_events_config_iface_exists(ifinfo.name) != 0)
478       /* dispatch notification */
479       ovs_events_dispatch_notification(&ifinfo);
480   }
481 }
482
483 /* OVD DB reply callback. It parses reply, receives
484  * interface information and dispatches the info to
485  * collecd
486  */
487 static void ovs_events_poll_result_cb(yajl_val jresult, yajl_val jerror) {
488   yajl_val *jvalues = NULL;
489   yajl_val jvalue = NULL;
490   ovs_events_iface_info_t ifinfo;
491
492   if (!YAJL_IS_NULL(jerror)) {
493     ERROR(OVS_EVENTS_PLUGIN "error received by OVS DB server");
494     return;
495   }
496
497   /* result should be an array */
498   if (!YAJL_IS_ARRAY(jresult)) {
499     ERROR(OVS_EVENTS_PLUGIN "invalid data (array is expected)");
500     return;
501   }
502
503   /* go through all rows and get interface info */
504   jvalues = YAJL_GET_ARRAY(jresult)->values;
505   for (int i = 0; i < YAJL_GET_ARRAY(jresult)->len; i++) {
506     jvalue = ovs_utils_get_value_by_key(jvalues[i], "rows");
507     if (jvalue == NULL || !YAJL_IS_ARRAY(jvalue)) {
508       ERROR(OVS_EVENTS_PLUGIN "invalid data (array of rows is expected)");
509       return;
510     }
511     /* get interfaces info */
512     for (int j = 0; j < YAJL_GET_ARRAY(jvalue)->len; j++) {
513       memset(&ifinfo, 0, sizeof(ifinfo));
514       if (ovs_events_get_iface_info(YAJL_GET_ARRAY(jvalue)->values[j],
515                                     &ifinfo) < 0) {
516         ERROR(OVS_EVENTS_PLUGIN
517               "unexpected interface information data received");
518         return;
519       }
520       DEBUG("name=%s, uuid=%s, ext_iface_id=%s, ext_vm_uuid=%s", ifinfo.name,
521             ifinfo.uuid, ifinfo.ext_iface_id, ifinfo.ext_vm_uuid);
522       ovs_events_link_status_submit(&ifinfo);
523     }
524   }
525 }
526
527 /* Setup OVS DB table callback. It subscribes to OVS DB 'Interface' table
528  * to receive link status event(s).
529  */
530 static void ovs_events_conn_initialize(ovs_db_t *pdb) {
531   int ret = 0;
532   const char tb_name[] = "Interface";
533   const char *columns[] = {"_uuid", "external_ids", "name", "link_state", NULL};
534
535   /* register update link status event if needed */
536   if (ovs_events_ctx.config.send_notification) {
537     ret = ovs_db_table_cb_register(pdb, tb_name, columns,
538                                    ovs_events_table_update_cb, NULL,
539                                    OVS_DB_TABLE_CB_FLAG_MODIFY);
540     if (ret < 0) {
541       ERROR(OVS_EVENTS_PLUGIN ": register OVS DB update callback failed");
542       return;
543     }
544   }
545   OVS_EVENTS_CTX_LOCK { ovs_events_ctx.is_db_available = 1; }
546   DEBUG(OVS_EVENTS_PLUGIN ": OVS DB has been initialized");
547 }
548
549 /* OVS DB terminate connection notification callback */
550 static void ovs_events_conn_terminate() {
551   const char msg[] = "OVS DB connection has been lost";
552   if (ovs_events_ctx.config.send_notification)
553     ovs_events_dispatch_terminate_notification(msg);
554   WARNING(OVS_EVENTS_PLUGIN ": %s", msg);
555   OVS_EVENTS_CTX_LOCK { ovs_events_ctx.is_db_available = 0; }
556 }
557
558 /* Read OVS DB interface link status callback */
559 static int ovs_events_plugin_read(__attribute__((unused)) user_data_t *u) {
560   _Bool is_connected = 0;
561   OVS_EVENTS_CTX_LOCK { is_connected = ovs_events_ctx.is_db_available; }
562   if (is_connected)
563     if (ovs_db_send_request(ovs_events_ctx.ovs_db, "transact",
564                             ovs_events_ctx.ovs_db_select_params,
565                             ovs_events_poll_result_cb) < 0) {
566       ERROR(OVS_EVENTS_PLUGIN ": get interface info failed");
567       return (-1);
568     }
569   return (0);
570 }
571
572 /* Initialize OVS plugin */
573 static int ovs_events_plugin_init(void) {
574   ovs_db_t *ovs_db = NULL;
575   ovs_db_callback_t cb = {.post_conn_init = ovs_events_conn_initialize,
576                           .post_conn_terminate = ovs_events_conn_terminate};
577
578   DEBUG(OVS_EVENTS_PLUGIN ": OVS DB node = %s, service=%s",
579         ovs_events_ctx.config.ovs_db_node, ovs_events_ctx.config.ovs_db_serv);
580
581   /* generate OVS DB select condition based on list on configured interfaces */
582   ovs_events_ctx.ovs_db_select_params = ovs_events_get_select_params();
583   if (ovs_events_ctx.ovs_db_select_params == NULL) {
584     ERROR(OVS_EVENTS_PLUGIN ": fail to get OVS DB select condition");
585     goto ovs_events_failure;
586   }
587
588   /* initialize OVS DB */
589   ovs_db = ovs_db_init(ovs_events_ctx.config.ovs_db_node,
590                        ovs_events_ctx.config.ovs_db_serv, &cb);
591   if (ovs_db == NULL) {
592     ERROR(OVS_EVENTS_PLUGIN ": fail to connect to OVS DB server");
593     goto ovs_events_failure;
594   }
595
596   /* store OVS DB handler */
597   OVS_EVENTS_CTX_LOCK { ovs_events_ctx.ovs_db = ovs_db; }
598
599   DEBUG(OVS_EVENTS_PLUGIN ": plugin has been initialized");
600   return (0);
601
602 ovs_events_failure:
603   ERROR(OVS_EVENTS_PLUGIN ": plugin initialize failed");
604   /* release allocated memory */
605   ovs_events_config_free();
606   return (-1);
607 }
608
609 /* Shutdown OVS plugin */
610 static int ovs_events_plugin_shutdown(void) {
611   /* destroy OVS DB */
612   if (ovs_db_destroy(ovs_events_ctx.ovs_db))
613     ERROR(OVS_EVENTS_PLUGIN ": OVSDB object destroy failed");
614
615   /* release memory allocated for config */
616   ovs_events_config_free();
617
618   DEBUG(OVS_EVENTS_PLUGIN ": plugin has been destroyed");
619   return (0);
620 }
621
622 /* Register OVS plugin callbacks */
623 void module_register(void) {
624   plugin_register_complex_config(OVS_EVENTS_PLUGIN, ovs_events_plugin_config);
625   plugin_register_init(OVS_EVENTS_PLUGIN, ovs_events_plugin_init);
626   plugin_register_complex_read(NULL, OVS_EVENTS_PLUGIN, ovs_events_plugin_read,
627                                0, NULL);
628   plugin_register_shutdown(OVS_EVENTS_PLUGIN, ovs_events_plugin_shutdown);
629 }