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