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