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 **/
28 #include "collectd.h"
30 #include "common.h" /* auxiliary functions */
32 #include "utils_ovs.h" /* OVS helpers */
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)
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;
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;
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;
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;
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;
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};
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 }
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 }
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);
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 if (strcmp(ifname, iface->name) == 0)
129 return (1);
131 return (0);
132 }
134 /* Get OVS DB select parameter request based on rfc7047,
135 * "Transact" & "Select" section
136 */
137 static char *ovs_events_get_select_params() {
138 int ret = 0;
139 size_t buff_size = 0;
140 size_t offset = 0;
141 char *buff = NULL;
142 char *new_buff = NULL;
143 const char params_fmt[] = "[\"Open_vSwitch\"%s]";
144 const char option_fmt[] = ",{\"op\":\"select\",\"table\":\"Interface\","
145 "\"where\":[[\"name\",\"==\",\"%s\"]],"
146 "\"columns\":[\"link_state\",\"external_ids\","
147 "\"name\",\"_uuid\"]}";
148 const char default_opt[] = ",{\"op\":\"select\",\"table\":\"Interface\","
149 "\"where\":[],\"columns\":[\"link_state\","
150 "\"external_ids\",\"name\",\"_uuid\"]}";
151 /* setup OVS DB interface condition */
152 for (ovs_events_iface_list_t *iface = ovs_events_ctx.config.ifaces; iface;
153 iface = iface->next, offset += ret) {
154 /* allocate new buffer (format size + ifname len is good enough) */
155 buff_size += (sizeof(option_fmt) + strlen(iface->name));
156 new_buff = realloc(buff, buff_size);
157 if (new_buff == NULL)
158 goto failure;
159 buff = new_buff;
160 ret = ssnprintf(buff + offset, buff_size, option_fmt, iface->name);
161 if (ret < 0)
162 goto failure;
163 }
164 /* if no interfaces are configured, use default params */
165 if (buff == NULL) {
166 buff = strdup(default_opt);
167 offset = strlen(default_opt);
168 }
170 /* allocate memory for OVS DB select params */
171 buff_size = offset + sizeof(params_fmt);
172 new_buff = malloc(buff_size);
173 if (new_buff == NULL)
174 goto failure;
176 /* create OVS DB select params */
177 if (ssnprintf(new_buff, buff_size, params_fmt, buff) < 0)
178 goto failure;
180 sfree(buff);
181 return new_buff;
183 failure:
184 sfree(new_buff);
185 sfree(buff);
186 return NULL;
187 }
189 /* Release memory allocated for configuration data */
190 static void ovs_events_config_free() {
191 ovs_events_iface_list_t *del_iface = NULL;
192 sfree(ovs_events_ctx.ovs_db_select_params);
193 while (ovs_events_ctx.config.ifaces) {
194 del_iface = ovs_events_ctx.config.ifaces;
195 ovs_events_ctx.config.ifaces = ovs_events_ctx.config.ifaces->next;
196 sfree(del_iface);
197 }
198 }
200 /* Parse plugin configuration file and store the config
201 * in allocated memory. Returns negative value in case of error.
202 */
203 static int ovs_events_plugin_config(oconfig_item_t *ci) {
204 ovs_events_iface_list_t *new_iface;
206 for (int i = 0; i < ci->children_num; i++) {
207 oconfig_item_t *child = ci->children + i;
208 if (strcasecmp("SendNotification", child->key) == 0) {
209 if (cf_util_get_boolean(child, &ovs_events_ctx.config.send_notification) <
210 0)
211 OVS_EVENTS_CONFIG_ERROR(child->key);
212 } else if (strcasecmp("OvsDbAddress", child->key) == 0) {
213 if (child->values_num < 1) {
214 ERROR(OVS_EVENTS_PLUGIN ": invalid OVS DB address specified");
215 goto failure;
216 }
217 /* check node type and get the value */
218 if (child->values[0].type != OCONFIG_TYPE_STRING) {
219 ERROR(OVS_EVENTS_PLUGIN ": OVS DB node is not a string");
220 goto failure;
221 }
222 sstrncpy(ovs_events_ctx.config.ovs_db_node, child->values[0].value.string,
223 sizeof(ovs_events_ctx.config.ovs_db_node));
224 /* get OVS DB address service name (optional) */
225 if (child->values_num > 1) {
226 if (child->values[1].type != OCONFIG_TYPE_STRING) {
227 ERROR(OVS_EVENTS_PLUGIN ": OVS DB service is not a string");
228 goto failure;
229 }
230 sstrncpy(ovs_events_ctx.config.ovs_db_serv,
231 child->values[1].value.string,
232 sizeof(ovs_events_ctx.config.ovs_db_serv));
233 }
234 } else if (strcasecmp("Interfaces", child->key) == 0) {
235 for (int j = 0; j < child->values_num; j++) {
236 /* check value type */
237 if (child->values[j].type != OCONFIG_TYPE_STRING) {
238 ERROR(OVS_EVENTS_PLUGIN
239 ": given interface name is not a string [idx=%d]",
240 j);
241 goto failure;
242 }
243 /* allocate memory for configured interface */
244 if ((new_iface = malloc(sizeof(*new_iface))) == NULL) {
245 ERROR(OVS_EVENTS_PLUGIN ": malloc () copy interface name fail");
246 goto failure;
247 } else {
248 /* store interface name */
249 sstrncpy(new_iface->name, child->values[j].value.string,
250 sizeof(new_iface->name));
251 new_iface->next = ovs_events_ctx.config.ifaces;
252 ovs_events_ctx.config.ifaces = new_iface;
253 DEBUG(OVS_EVENTS_PLUGIN ": found monitored interface \"%s\"",
254 new_iface->name);
255 }
256 }
257 } else {
258 ERROR(OVS_EVENTS_PLUGIN ": option '%s' is not allowed here", child->key);
259 goto failure;
260 }
261 }
262 return (0);
264 failure:
265 ovs_events_config_free();
266 return (-1);
267 }
269 /* Dispatch OVS interface link status event to collectd */
270 static void
271 ovs_events_dispatch_notification(const ovs_events_iface_info_t *ifinfo) {
272 const char *msg_link_status = NULL;
273 notification_t n = {
274 NOTIF_FAILURE, cdtime(), "", "", OVS_EVENTS_PLUGIN, "", "", "", NULL};
276 /* convert link status to message string */
277 switch (ifinfo->link_status) {
278 case UP:
279 msg_link_status = "UP";
280 n.severity = NOTIF_OKAY;
281 break;
282 case DOWN:
283 msg_link_status = "DOWN";
284 n.severity = NOTIF_WARNING;
285 break;
286 default:
287 ERROR(OVS_EVENTS_PLUGIN ": unknown interface link status");
288 return;
289 }
291 /* add interface metadata to the notification */
292 if (plugin_notification_meta_add_string(&n, "uuid", ifinfo->uuid) < 0) {
293 ERROR(OVS_EVENTS_PLUGIN ": add interface uuid meta data failed");
294 return;
295 }
297 if (strlen(ifinfo->ext_vm_uuid) > 0)
298 if (plugin_notification_meta_add_string(&n, "vm-uuid",
299 ifinfo->ext_vm_uuid) < 0) {
300 ERROR(OVS_EVENTS_PLUGIN ": add interface vm-uuid meta data failed");
301 return;
302 }
304 if (strlen(ifinfo->ext_iface_id) > 0)
305 if (plugin_notification_meta_add_string(&n, "iface-id",
306 ifinfo->ext_iface_id) < 0) {
307 ERROR(OVS_EVENTS_PLUGIN ": add interface iface-id meta data failed");
308 return;
309 }
311 /* fill the notification data */
312 ssnprintf(n.message, sizeof(n.message),
313 "link state of \"%s\" interface has been changed to \"%s\"",
314 ifinfo->name, msg_link_status);
315 sstrncpy(n.host, hostname_g, sizeof(n.host));
316 sstrncpy(n.plugin_instance, ifinfo->name, sizeof(n.plugin_instance));
317 sstrncpy(n.type, "gauge", sizeof(n.type));
318 sstrncpy(n.type_instance, "link_status", sizeof(n.type_instance));
319 plugin_dispatch_notification(&n);
320 }
322 /* Dispatch OVS interface link status value to collectd */
323 static void
324 ovs_events_link_status_submit(const ovs_events_iface_info_t *ifinfo) {
325 value_list_t vl = VALUE_LIST_INIT;
326 meta_data_t *meta = NULL;
328 /* add interface metadata to the submit value */
329 if ((meta = meta_data_create()) != NULL) {
330 if (meta_data_add_string(meta, "uuid", ifinfo->uuid) < 0)
331 ERROR(OVS_EVENTS_PLUGIN ": add interface uuid meta data failed");
333 if (strlen(ifinfo->ext_vm_uuid) > 0)
334 if (meta_data_add_string(meta, "vm-uuid", ifinfo->ext_vm_uuid) < 0)
335 ERROR(OVS_EVENTS_PLUGIN ": add interface vm-uuid meta data failed");
337 if (strlen(ifinfo->ext_iface_id) > 0)
338 if (meta_data_add_string(meta, "iface-id", ifinfo->ext_iface_id) < 0)
339 ERROR(OVS_EVENTS_PLUGIN ": add interface iface-id meta data failed");
340 vl.meta = meta;
341 } else
342 ERROR(OVS_EVENTS_PLUGIN ": create metadata failed");
344 vl.time = cdtime();
345 vl.values = &(value_t){.gauge = (gauge_t)ifinfo->link_status};
346 vl.values_len = 1;
347 sstrncpy(vl.plugin, OVS_EVENTS_PLUGIN, sizeof(vl.plugin));
348 sstrncpy(vl.plugin_instance, ifinfo->name, sizeof(vl.plugin_instance));
349 sstrncpy(vl.type, "gauge", sizeof(vl.type));
350 sstrncpy(vl.type_instance, "link_status", sizeof(vl.type_instance));
351 plugin_dispatch_values(&vl);
352 meta_data_destroy(meta);
353 }
355 /* Dispatch OVS DB terminate connection event to collectd */
356 static void ovs_events_dispatch_terminate_notification(const char *msg) {
357 notification_t n = {
358 NOTIF_FAILURE, cdtime(), "", "", OVS_EVENTS_PLUGIN, "", "", "", NULL};
359 sstrncpy(n.message, msg, sizeof(n.message));
360 sstrncpy(n.host, hostname_g, sizeof(n.host));
361 plugin_dispatch_notification(&n);
362 }
364 /* Get OVS DB interface information and stores it into
365 * ovs_events_iface_info_t structure */
366 static int ovs_events_get_iface_info(yajl_val jobject,
367 ovs_events_iface_info_t *ifinfo) {
368 yajl_val jexternal_ids = NULL;
369 yajl_val jvalue = NULL;
370 yajl_val juuid = NULL;
371 const char *state = NULL;
373 /* check YAJL type */
374 if (!YAJL_IS_OBJECT(jobject))
375 return (-1);
377 /* try to find external_ids, name and link_state fields */
378 jexternal_ids = ovs_utils_get_value_by_key(jobject, "external_ids");
379 if (jexternal_ids == NULL || ifinfo == NULL)
380 return (-1);
382 /* get iface-id from external_ids field */
383 jvalue = ovs_utils_get_map_value(jexternal_ids, "iface-id");
384 if (jvalue != NULL && YAJL_IS_STRING(jvalue))
385 sstrncpy(ifinfo->ext_iface_id, YAJL_GET_STRING(jvalue),
386 sizeof(ifinfo->ext_iface_id));
388 /* get vm-uuid from external_ids field */
389 jvalue = ovs_utils_get_map_value(jexternal_ids, "vm-uuid");
390 if (jvalue != NULL && YAJL_IS_STRING(jvalue))
391 sstrncpy(ifinfo->ext_vm_uuid, YAJL_GET_STRING(jvalue),
392 sizeof(ifinfo->ext_vm_uuid));
394 /* get interface uuid */
395 jvalue = ovs_utils_get_value_by_key(jobject, "_uuid");
396 if (jvalue == NULL || !YAJL_IS_ARRAY(jvalue) ||
397 YAJL_GET_ARRAY(jvalue)->len != 2)
398 return (-1);
399 juuid = YAJL_GET_ARRAY(jvalue)->values[1];
400 if (juuid == NULL || !YAJL_IS_STRING(juuid))
401 return (-1);
402 sstrncpy(ifinfo->uuid, YAJL_GET_STRING(juuid), sizeof(ifinfo->uuid));
404 /* get interface name */
405 jvalue = ovs_utils_get_value_by_key(jobject, "name");
406 if (jvalue == NULL || !YAJL_IS_STRING(jvalue))
407 return (-1);
408 sstrncpy(ifinfo->name, YAJL_GET_STRING(jvalue), sizeof(ifinfo->name));
410 /* get OVS DB interface link status */
411 jvalue = ovs_utils_get_value_by_key(jobject, "link_state");
412 if (jvalue != NULL && ((state = YAJL_GET_STRING(jvalue)) != NULL)) {
413 /* convert OVS table link state to link status */
414 if (strcmp(state, "up") == 0)
415 ifinfo->link_status = UP;
416 else if (strcmp(state, "down") == 0)
417 ifinfo->link_status = DOWN;
418 }
419 return (0);
420 }
422 /* Process OVS DB update table event. It handles link status update event(s)
423 * and dispatches the value(s) to collectd if interface name matches one of
424 * interfaces specified in configuration file.
425 */
426 static void ovs_events_table_update_cb(yajl_val jupdates) {
427 yajl_val jnew_val = NULL;
428 yajl_val jupdate = NULL;
429 yajl_val jrow_update = NULL;
430 ovs_events_iface_info_t ifinfo;
432 /* JSON "Interface" table update example:
433 * ---------------------------------
434 * {"Interface":
435 * {
436 * "9adf1db2-29ca-4140-ab22-ae347a4484de":
437 * {
438 * "new":
439 * {
440 * "name":"br0",
441 * "link_state":"up"
442 * },
443 * "old":
444 * {
445 * "link_state":"down"
446 * }
447 * }
448 * }
449 * }
450 */
451 if (!YAJL_IS_OBJECT(jupdates) || !(YAJL_GET_OBJECT(jupdates)->len > 0)) {
452 ERROR(OVS_EVENTS_PLUGIN ": unexpected OVS DB update event received");
453 return;
454 }
455 /* verify if this is a table event */
456 jupdate = YAJL_GET_OBJECT(jupdates)->values[0];
457 if (!YAJL_IS_OBJECT(jupdate)) {
458 ERROR(OVS_EVENTS_PLUGIN ": unexpected table update event received");
459 return;
460 }
461 /* go through all row updates */
462 for (int row_index = 0; row_index < YAJL_GET_OBJECT(jupdate)->len;
463 ++row_index) {
464 jrow_update = YAJL_GET_OBJECT(jupdate)->values[row_index];
466 /* check row update */
467 jnew_val = ovs_utils_get_value_by_key(jrow_update, "new");
468 if (jnew_val == NULL) {
469 ERROR(OVS_EVENTS_PLUGIN ": unexpected row update received");
470 return;
471 }
472 /* get OVS DB interface information */
473 if (ovs_events_get_iface_info(jnew_val, &ifinfo) < 0) {
474 ERROR(OVS_EVENTS_PLUGIN
475 " :unexpected interface information data received");
476 return;
477 }
478 if (ovs_events_config_iface_exists(ifinfo.name) != 0)
479 /* dispatch notification */
480 ovs_events_dispatch_notification(&ifinfo);
481 }
482 }
484 /* OVD DB reply callback. It parses reply, receives
485 * interface information and dispatches the info to
486 * collecd
487 */
488 static void ovs_events_poll_result_cb(yajl_val jresult, yajl_val jerror) {
489 yajl_val *jvalues = NULL;
490 yajl_val jvalue = NULL;
491 ovs_events_iface_info_t ifinfo;
493 if (!YAJL_IS_NULL(jerror)) {
494 ERROR(OVS_EVENTS_PLUGIN "error received by OVS DB server");
495 return;
496 }
498 /* result should be an array */
499 if (!YAJL_IS_ARRAY(jresult)) {
500 ERROR(OVS_EVENTS_PLUGIN "invalid data (array is expected)");
501 return;
502 }
504 /* go through all rows and get interface info */
505 jvalues = YAJL_GET_ARRAY(jresult)->values;
506 for (int i = 0; i < YAJL_GET_ARRAY(jresult)->len; i++) {
507 jvalue = ovs_utils_get_value_by_key(jvalues[i], "rows");
508 if (jvalue == NULL || !YAJL_IS_ARRAY(jvalue)) {
509 ERROR(OVS_EVENTS_PLUGIN "invalid data (array of rows is expected)");
510 return;
511 }
512 /* get interfaces info */
513 for (int j = 0; j < YAJL_GET_ARRAY(jvalue)->len; j++) {
514 memset(&ifinfo, 0, sizeof(ifinfo));
515 if (ovs_events_get_iface_info(YAJL_GET_ARRAY(jvalue)->values[j],
516 &ifinfo) < 0) {
517 ERROR(OVS_EVENTS_PLUGIN
518 "unexpected interface information data received");
519 return;
520 }
521 DEBUG("name=%s, uuid=%s, ext_iface_id=%s, ext_vm_uuid=%s", ifinfo.name,
522 ifinfo.uuid, ifinfo.ext_iface_id, ifinfo.ext_vm_uuid);
523 ovs_events_link_status_submit(&ifinfo);
524 }
525 }
526 }
528 /* Setup OVS DB table callback. It subscribes to OVS DB 'Interface' table
529 * to receive link status event(s).
530 */
531 static void ovs_events_conn_initialize(ovs_db_t *pdb) {
532 int ret = 0;
533 const char tb_name[] = "Interface";
534 const char *columns[] = {"_uuid", "external_ids", "name", "link_state", NULL};
536 /* register update link status event if needed */
537 if (ovs_events_ctx.config.send_notification) {
538 ret = ovs_db_table_cb_register(pdb, tb_name, columns,
539 ovs_events_table_update_cb, NULL,
540 OVS_DB_TABLE_CB_FLAG_MODIFY);
541 if (ret < 0) {
542 ERROR(OVS_EVENTS_PLUGIN ": register OVS DB update callback failed");
543 return;
544 }
545 }
546 OVS_EVENTS_CTX_LOCK { ovs_events_ctx.is_db_available = 1; }
547 DEBUG(OVS_EVENTS_PLUGIN ": OVS DB has been initialized");
548 }
550 /* OVS DB terminate connection notification callback */
551 static void ovs_events_conn_terminate() {
552 const char msg[] = "OVS DB connection has been lost";
553 if (ovs_events_ctx.config.send_notification)
554 ovs_events_dispatch_terminate_notification(msg);
555 WARNING(OVS_EVENTS_PLUGIN ": %s", msg);
556 OVS_EVENTS_CTX_LOCK { ovs_events_ctx.is_db_available = 0; }
557 }
559 /* Read OVS DB interface link status callback */
560 static int ovs_events_plugin_read(__attribute__((unused)) user_data_t *u) {
561 _Bool is_connected = 0;
562 OVS_EVENTS_CTX_LOCK { is_connected = ovs_events_ctx.is_db_available; }
563 if (is_connected)
564 if (ovs_db_send_request(ovs_events_ctx.ovs_db, "transact",
565 ovs_events_ctx.ovs_db_select_params,
566 ovs_events_poll_result_cb) < 0) {
567 ERROR(OVS_EVENTS_PLUGIN ": get interface info failed");
568 return (-1);
569 }
570 return (0);
571 }
573 /* Initialize OVS plugin */
574 static int ovs_events_plugin_init(void) {
575 ovs_db_t *ovs_db = NULL;
576 ovs_db_callback_t cb = {.post_conn_init = ovs_events_conn_initialize,
577 .post_conn_terminate = ovs_events_conn_terminate};
579 DEBUG(OVS_EVENTS_PLUGIN ": OVS DB node = %s, service=%s",
580 ovs_events_ctx.config.ovs_db_node, ovs_events_ctx.config.ovs_db_serv);
582 /* generate OVS DB select condition based on list on configured interfaces */
583 ovs_events_ctx.ovs_db_select_params = ovs_events_get_select_params();
584 if (ovs_events_ctx.ovs_db_select_params == NULL) {
585 ERROR(OVS_EVENTS_PLUGIN ": fail to get OVS DB select condition");
586 goto ovs_events_failure;
587 }
589 /* initialize OVS DB */
590 ovs_db = ovs_db_init(ovs_events_ctx.config.ovs_db_node,
591 ovs_events_ctx.config.ovs_db_serv, &cb);
592 if (ovs_db == NULL) {
593 ERROR(OVS_EVENTS_PLUGIN ": fail to connect to OVS DB server");
594 goto ovs_events_failure;
595 }
597 /* store OVS DB handler */
598 OVS_EVENTS_CTX_LOCK { ovs_events_ctx.ovs_db = ovs_db; }
600 DEBUG(OVS_EVENTS_PLUGIN ": plugin has been initialized");
601 return (0);
603 ovs_events_failure:
604 ERROR(OVS_EVENTS_PLUGIN ": plugin initialize failed");
605 /* release allocated memory */
606 ovs_events_config_free();
607 return (-1);
608 }
610 /* Shutdown OVS plugin */
611 static int ovs_events_plugin_shutdown(void) {
612 /* destroy OVS DB */
613 if (ovs_db_destroy(ovs_events_ctx.ovs_db))
614 ERROR(OVS_EVENTS_PLUGIN ": OVSDB object destroy failed");
616 /* release memory allocated for config */
617 ovs_events_config_free();
619 DEBUG(OVS_EVENTS_PLUGIN ": plugin has been destroyed");
620 return (0);
621 }
623 /* Register OVS plugin callbacks */
624 void module_register(void) {
625 plugin_register_complex_config(OVS_EVENTS_PLUGIN, ovs_events_plugin_config);
626 plugin_register_init(OVS_EVENTS_PLUGIN, ovs_events_plugin_init);
627 plugin_register_complex_read(NULL, OVS_EVENTS_PLUGIN, ovs_events_plugin_read,
628 0, NULL);
629 plugin_register_shutdown(OVS_EVENTS_PLUGIN, ovs_events_plugin_shutdown);
630 }