127a9178c5fdf95da45386f2d34ab8a38cc2cbd9
[collectd.git] / src / utils / ovs / ovs.c
1 /**
2  * collectd - src/utils_ovs.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
7  *of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11  * of the Software, and to permit persons to whom the Software is furnished to
12  *do
13  * so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  *all
17  * copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  *
27  * Authors:
28  *   Volodymyr Mytnyk <volodymyrx.mytnyk@intel.com>
29  **/
30
31 /* clang-format off */
32 /*
33  *                         OVS DB API internal architecture diagram
34  * +------------------------------------------------------------------------------+
35  * |OVS plugin      |OVS utils                                                    |
36  * |                |     +------------------------+                              |
37  * |                |     |      echo handler      |                JSON request/ |
38  * |                |  +--+ (ovs_db_table_echo_cb) +<---+---------+ update event/ |
39  * |                |  |  |                        |    |         | result        |
40  * |                |  |  +------------------------+    |         |               |
41  * |                |  |                                |    +----+---+--------+  |
42  * |  +----------+  |  |  +------------------------+    |    |        |        |  |
43  * |  |  update  |  |  |  |     update handler     |    |    |  YAJL  |  JSON  |  |
44  * |  | callback +<-------+(ovs_db_table_update_cp)+<---+    | parser | reader |  |
45  * |  +----------+  |  |  |                        |    |    |        |        |  |
46  * |                |  |  +------------------------+    |    +--------+---+----+  |
47  * |                |  |                                |                 ^       |
48  * |  +----------+  |  |  +------------------------+    |                 |       |
49  * |  |  result  |  |  |  |     result handler     |    |                 |       |
50  * |  | callback +<-------+   (ovs_db_result_cb)   +<---+        JSON raw |       |
51  * |  +----------+  |  |  |                        |               data   |       |
52  * |                |  |  +------------------------+                      |       |
53  * |                |  |                                                  |       |
54  * |                |  |    +------------------+             +------------+----+  |
55  * |  +----------+  |  |    |thread|           |             |thread|          |  |
56  * |  |   init   |  |  |    |                  |  reconnect  |                 |  |
57  * |  | callback +<---------+   EVENT WORKER   +<------------+   POLL WORKER   |  |
58  * |  +----------+  |  |    +------------------+             +--------+--------+  |
59  * |                |  |                                              ^           |
60  * +----------------+-------------------------------------------------------------+
61  *                     |                                              |
62  *                 JSON|echo reply                                 raw|data
63  *                     v                                              v
64  * +-------------------+----------------------------------------------+-----------+
65  * |                                 TCP/UNIX socket                              |
66  * +-------------------------------------------------------------------------------
67  */
68 /* clang-format on */
69
70 /* collectd headers */
71 #include "collectd.h"
72
73 #include "utils/common/common.h"
74
75 /* private headers */
76 #include "utils/ovs/ovs.h"
77
78 /* system libraries */
79 #if HAVE_NETDB_H
80 #include <netdb.h>
81 #endif
82 #if HAVE_ARPA_INET_H
83 #include <arpa/inet.h>
84 #endif
85 #if HAVE_POLL_H
86 #include <poll.h>
87 #endif
88 #if HAVE_SYS_UN_H
89 #include <sys/un.h>
90 #endif
91
92 #include <semaphore.h>
93
94 #define OVS_ERROR(fmt, ...)                                                    \
95   do {                                                                         \
96     ERROR("ovs_utils: " fmt, ##__VA_ARGS__);                                   \
97   } while (0)
98 #define OVS_DEBUG(fmt, ...)                                                    \
99   do {                                                                         \
100     DEBUG("%s:%d:%s(): " fmt, __FILE__, __LINE__, __FUNCTION__,                \
101           ##__VA_ARGS__);                                                      \
102   } while (0)
103
104 #define OVS_DB_POLL_TIMEOUT 1           /* poll receive timeout (sec) */
105 #define OVS_DB_POLL_READ_BLOCK_SIZE 512 /* read block size (bytes) */
106 #define OVS_DB_DEFAULT_DB_NAME "Open_vSwitch"
107
108 #define OVS_DB_EVENT_NONE 0
109 #define OVS_DB_EVENT_TIMEOUT 5 /* event thread timeout (sec) */
110 #define OVS_DB_EVENT_TERMINATE 1
111 #define OVS_DB_EVENT_CONN_ESTABLISHED 2
112 #define OVS_DB_EVENT_CONN_TERMINATED 3
113
114 #define OVS_DB_POLL_STATE_RUNNING 1
115 #define OVS_DB_POLL_STATE_EXITING 2
116
117 #define OVS_DB_SEND_REQ_TIMEOUT 5 /* send request timeout (sec) */
118
119 #define OVS_YAJL_CALL(func, ...)                                               \
120   do {                                                                         \
121     yajl_gen_ret = yajl_gen_status_ok;                                         \
122     if ((yajl_gen_ret = func(__VA_ARGS__)) != yajl_gen_status_ok)              \
123       goto yajl_gen_failure;                                                   \
124   } while (0)
125 #define OVS_YAJL_ERROR_BUFFER_SIZE 1024
126 #define OVS_ERROR_BUFF_SIZE 512
127 #define OVS_UID_STR_SIZE 17 /* 64-bit HEX string len + '\0' */
128
129 /* JSON reader internal data */
130 struct ovs_json_reader_s {
131   char *buff_ptr;
132   size_t buff_size;
133   size_t buff_offset;
134   size_t json_offset;
135 };
136 typedef struct ovs_json_reader_s ovs_json_reader_t;
137
138 /* Result callback declaration */
139 struct ovs_result_cb_s {
140   sem_t sync;
141   ovs_db_result_cb_t call;
142 };
143 typedef struct ovs_result_cb_s ovs_result_cb_t;
144
145 /* Table callback declaration */
146 struct ovs_table_cb_s {
147   ovs_db_table_cb_t call;
148 };
149 typedef struct ovs_table_cb_s ovs_table_cb_t;
150
151 /* Callback declaration */
152 struct ovs_callback_s {
153   uint64_t uid;
154   union {
155     ovs_result_cb_t result;
156     ovs_table_cb_t table;
157   };
158   struct ovs_callback_s *next;
159   struct ovs_callback_s *prev;
160 };
161 typedef struct ovs_callback_s ovs_callback_t;
162
163 /* Event thread data declaration */
164 struct ovs_event_thread_s {
165   pthread_t tid;
166   pthread_mutex_t mutex;
167   pthread_cond_t cond;
168   int value;
169 };
170 typedef struct ovs_event_thread_s ovs_event_thread_t;
171
172 /* Poll thread data declaration */
173 struct ovs_poll_thread_s {
174   pthread_t tid;
175   pthread_mutex_t mutex;
176   int state;
177 };
178 typedef struct ovs_poll_thread_s ovs_poll_thread_t;
179
180 /* OVS DB internal data declaration */
181 struct ovs_db_s {
182   ovs_poll_thread_t poll_thread;
183   ovs_event_thread_t event_thread;
184   pthread_mutex_t mutex;
185   ovs_callback_t *remote_cb;
186   ovs_db_callback_t cb;
187   char service[OVS_DB_ADDR_SERVICE_SIZE];
188   char node[OVS_DB_ADDR_NODE_SIZE];
189   char unix_path[OVS_DB_ADDR_NODE_SIZE];
190   int sock;
191 };
192
193 /* Global variables */
194 static uint64_t ovs_uid;
195 static pthread_mutex_t ovs_uid_mutex = PTHREAD_MUTEX_INITIALIZER;
196
197 /* Post an event to event thread.
198  * Possible events are:
199  *  OVS_DB_EVENT_TERMINATE
200  *  OVS_DB_EVENT_CONN_ESTABLISHED
201  *  OVS_DB_EVENT_CONN_TERMINATED
202  */
203 static void ovs_db_event_post(ovs_db_t *pdb, int event) {
204   pthread_mutex_lock(&pdb->event_thread.mutex);
205   pdb->event_thread.value = event;
206   pthread_mutex_unlock(&pdb->event_thread.mutex);
207   pthread_cond_signal(&pdb->event_thread.cond);
208 }
209
210 /* Check if POLL thread is still running. Returns
211  * 1 if running otherwise 0 is returned */
212 static bool ovs_db_poll_is_running(ovs_db_t *pdb) {
213   int state = 0;
214   pthread_mutex_lock(&pdb->poll_thread.mutex);
215   state = pdb->poll_thread.state;
216   pthread_mutex_unlock(&pdb->poll_thread.mutex);
217   return state == OVS_DB_POLL_STATE_RUNNING;
218 }
219
220 /* Generate unique identifier (UID). It is used by OVS DB API
221  * to set "id" field for any OVS DB JSON request. */
222 static uint64_t ovs_uid_generate() {
223   uint64_t new_uid;
224   pthread_mutex_lock(&ovs_uid_mutex);
225   new_uid = ++ovs_uid;
226   pthread_mutex_unlock(&ovs_uid_mutex);
227   return new_uid;
228 }
229
230 /*
231  * Callback API. These function are used to store
232  * registered callbacks in OVS DB API.
233  */
234
235 /* Add new callback into OVS DB object */
236 static void ovs_db_callback_add(ovs_db_t *pdb, ovs_callback_t *new_cb) {
237   pthread_mutex_lock(&pdb->mutex);
238   if (pdb->remote_cb)
239     pdb->remote_cb->prev = new_cb;
240   new_cb->next = pdb->remote_cb;
241   new_cb->prev = NULL;
242   pdb->remote_cb = new_cb;
243   pthread_mutex_unlock(&pdb->mutex);
244 }
245
246 /* Remove callback from OVS DB object */
247 static void ovs_db_callback_remove(ovs_db_t *pdb, ovs_callback_t *del_cb) {
248   pthread_mutex_lock(&pdb->mutex);
249   ovs_callback_t *pre_cb = del_cb->prev;
250   ovs_callback_t *next_cb = del_cb->next;
251
252   if (next_cb)
253     next_cb->prev = del_cb->prev;
254
255   if (pre_cb)
256     pre_cb->next = del_cb->next;
257   else
258     pdb->remote_cb = del_cb->next;
259
260   free(del_cb);
261   pthread_mutex_unlock(&pdb->mutex);
262 }
263
264 /* Remove all callbacks form OVS DB object */
265 static void ovs_db_callback_remove_all(ovs_db_t *pdb) {
266   pthread_mutex_lock(&pdb->mutex);
267   while (pdb->remote_cb != NULL) {
268     ovs_callback_t *del_cb = pdb->remote_cb;
269     pdb->remote_cb = del_cb->next;
270     sfree(del_cb);
271   }
272   pthread_mutex_unlock(&pdb->mutex);
273 }
274
275 /* Get/find callback in OVS DB object by UID. Returns pointer
276  * to requested callback otherwise NULL is returned.
277  *
278  * IMPORTANT NOTE:
279  *   The OVS DB mutex MUST be locked by the caller
280  *   to make sure that returned callback is still valid.
281  */
282 static ovs_callback_t *ovs_db_callback_get(ovs_db_t *pdb, uint64_t uid) {
283   for (ovs_callback_t *cb = pdb->remote_cb; cb != NULL; cb = cb->next)
284     if (cb->uid == uid)
285       return cb;
286   return NULL;
287 }
288
289 /* Send all requested data to the socket. Returns 0 if
290  * ALL request data has been sent otherwise negative value
291  * is returned */
292 static int ovs_db_data_send(const ovs_db_t *pdb, const char *data, size_t len) {
293   ssize_t nbytes = 0;
294   size_t rem = len;
295   size_t off = 0;
296
297   while (rem > 0) {
298     if ((nbytes = send(pdb->sock, data + off, rem, 0)) <= 0)
299       return -1;
300     rem -= (size_t)nbytes;
301     off += (size_t)nbytes;
302   }
303   return 0;
304 }
305
306 /*
307  * YAJL (Yet Another JSON Library) helper functions
308  * Documentation (https://lloyd.github.io/yajl/)
309  */
310
311 /* Add null-terminated string into YAJL generator handle (JSON object).
312  * Similar function to yajl_gen_string() but takes null-terminated string
313  * instead of string and its length.
314  *
315  * jgen   - YAJL generator handle allocated by yajl_gen_alloc()
316  * string - Null-terminated string
317  */
318 static yajl_gen_status ovs_yajl_gen_tstring(yajl_gen hander,
319                                             const char *string) {
320   return yajl_gen_string(hander, (const unsigned char *)string, strlen(string));
321 }
322
323 /* Add YAJL value into YAJL generator handle (JSON object)
324  *
325  * jgen - YAJL generator handle allocated by yajl_gen_alloc()
326  * jval - YAJL value usually returned by yajl_tree_get()
327  */
328 static yajl_gen_status ovs_yajl_gen_val(yajl_gen jgen, yajl_val jval) {
329   size_t array_len = 0;
330   yajl_val *jvalues = NULL;
331   yajl_val jobj_value = NULL;
332   const char *obj_key = NULL;
333   size_t obj_len = 0;
334   yajl_gen_status yajl_gen_ret = yajl_gen_status_ok;
335
336   if (jval == NULL)
337     return yajl_gen_generation_complete;
338
339   if (YAJL_IS_STRING(jval))
340     OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, YAJL_GET_STRING(jval));
341   else if (YAJL_IS_DOUBLE(jval))
342     OVS_YAJL_CALL(yajl_gen_double, jgen, YAJL_GET_DOUBLE(jval));
343   else if (YAJL_IS_INTEGER(jval))
344     OVS_YAJL_CALL(yajl_gen_double, jgen, YAJL_GET_INTEGER(jval));
345   else if (YAJL_IS_TRUE(jval))
346     OVS_YAJL_CALL(yajl_gen_bool, jgen, 1);
347   else if (YAJL_IS_FALSE(jval))
348     OVS_YAJL_CALL(yajl_gen_bool, jgen, 0);
349   else if (YAJL_IS_NULL(jval))
350     OVS_YAJL_CALL(yajl_gen_null, jgen);
351   else if (YAJL_IS_ARRAY(jval)) {
352     /* create new array and add all elements into the array */
353     array_len = YAJL_GET_ARRAY(jval)->len;
354     jvalues = YAJL_GET_ARRAY(jval)->values;
355     OVS_YAJL_CALL(yajl_gen_array_open, jgen);
356     for (size_t i = 0; i < array_len; i++)
357       OVS_YAJL_CALL(ovs_yajl_gen_val, jgen, jvalues[i]);
358     OVS_YAJL_CALL(yajl_gen_array_close, jgen);
359   } else if (YAJL_IS_OBJECT(jval)) {
360     /* create new object and add all elements into the object */
361     OVS_YAJL_CALL(yajl_gen_map_open, jgen);
362     obj_len = YAJL_GET_OBJECT(jval)->len;
363     for (size_t i = 0; i < obj_len; i++) {
364       obj_key = YAJL_GET_OBJECT(jval)->keys[i];
365       jobj_value = YAJL_GET_OBJECT(jval)->values[i];
366       OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, obj_key);
367       OVS_YAJL_CALL(ovs_yajl_gen_val, jgen, jobj_value);
368     }
369     OVS_YAJL_CALL(yajl_gen_map_close, jgen);
370   } else {
371     OVS_ERROR("%s() unsupported value type %d (skip)", __FUNCTION__,
372               (int)(jval)->type);
373     goto yajl_gen_failure;
374   }
375   return yajl_gen_status_ok;
376
377 yajl_gen_failure:
378   OVS_ERROR("%s() error to generate value", __FUNCTION__);
379   return yajl_gen_ret;
380 }
381
382 /* OVS DB echo request handler. When OVS DB sends
383  * "echo" request to the client, client should generate
384  * "echo" replay with the same content received in the
385  * request */
386 static int ovs_db_table_echo_cb(const ovs_db_t *pdb, yajl_val jnode) {
387   yajl_val jparams;
388   yajl_val jid;
389   yajl_gen jgen;
390   size_t resp_len = 0;
391   const char *resp = NULL;
392   const char *params_path[] = {"params", NULL};
393   const char *id_path[] = {"id", NULL};
394   yajl_gen_status yajl_gen_ret;
395
396   if ((jgen = yajl_gen_alloc(NULL)) == NULL)
397     return -1;
398
399   /* check & get request attributes */
400   if ((jparams = yajl_tree_get(jnode, params_path, yajl_t_array)) == NULL ||
401       ((jid = yajl_tree_get(jnode, id_path, yajl_t_any)) == NULL)) {
402     OVS_ERROR("parse echo request failed");
403     goto yajl_gen_failure;
404   }
405
406   /* generate JSON echo response */
407   OVS_YAJL_CALL(yajl_gen_map_open, jgen);
408
409   OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "result");
410   OVS_YAJL_CALL(ovs_yajl_gen_val, jgen, jparams);
411
412   OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "error");
413   OVS_YAJL_CALL(yajl_gen_null, jgen);
414
415   OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "id");
416   OVS_YAJL_CALL(ovs_yajl_gen_val, jgen, jid);
417
418   OVS_YAJL_CALL(yajl_gen_map_close, jgen);
419   OVS_YAJL_CALL(yajl_gen_get_buf, jgen, (const unsigned char **)&resp,
420                 &resp_len);
421
422   /* send the response */
423   OVS_DEBUG("response: %s", resp);
424   if (ovs_db_data_send(pdb, resp, resp_len) < 0) {
425     OVS_ERROR("send echo reply failed");
426     goto yajl_gen_failure;
427   }
428   /* clean up and return success */
429   yajl_gen_clear(jgen);
430   return 0;
431
432 yajl_gen_failure:
433   /* release memory */
434   yajl_gen_clear(jgen);
435   return -1;
436 }
437
438 /* Get OVS DB registered callback by YAJL val. The YAJL
439  * value should be YAJL string (UID). Returns NULL if
440  * callback hasn't been found. See also ovs_db_callback_get()
441  * description for addition info.
442  */
443 static ovs_callback_t *ovs_db_table_callback_get(ovs_db_t *pdb, yajl_val jid) {
444   char *endptr = NULL;
445   const char *suid = NULL;
446   uint64_t uid;
447
448   if (jid && YAJL_IS_STRING(jid)) {
449     suid = YAJL_GET_STRING(jid);
450     uid = (uint64_t)strtoul(suid, &endptr, 16);
451     if (*endptr == '\0' && uid)
452       return ovs_db_callback_get(pdb, uid);
453   }
454
455   return NULL;
456 }
457
458 /* OVS DB table update event handler.
459  * This callback is called by POLL thread if OVS DB
460  * table update callback is received from the DB
461  * server. Once registered callback found, it's called
462  * by this handler. */
463 static int ovs_db_table_update_cb(ovs_db_t *pdb, yajl_val jnode) {
464   ovs_callback_t *cb = NULL;
465   yajl_val jvalue;
466   yajl_val jparams;
467   yajl_val jtable_updates;
468   const char *params_path[] = {"params", NULL};
469   const char *id_path[] = {"id", NULL};
470
471   /* check & get request attributes */
472   if ((jparams = yajl_tree_get(jnode, params_path, yajl_t_array)) == NULL ||
473       (yajl_tree_get(jnode, id_path, yajl_t_null) == NULL)) {
474     OVS_ERROR("invalid OVS DB request received");
475     return -1;
476   }
477
478   /* check array length: [<json-value>, <table-updates>] */
479   if ((YAJL_GET_ARRAY(jparams) == NULL) ||
480       (YAJL_GET_ARRAY(jparams)->len != 2)) {
481     OVS_ERROR("invalid OVS DB request received");
482     return -1;
483   }
484
485   jvalue = YAJL_GET_ARRAY(jparams)->values[0];
486   jtable_updates = YAJL_GET_ARRAY(jparams)->values[1];
487   if ((!YAJL_IS_OBJECT(jtable_updates)) || (!YAJL_IS_STRING(jvalue))) {
488     OVS_ERROR("invalid OVS DB request id or table update received");
489     return -1;
490   }
491
492   /* find registered callback based on <json-value> */
493   pthread_mutex_lock(&pdb->mutex);
494   cb = ovs_db_table_callback_get(pdb, jvalue);
495   if (cb == NULL || cb->table.call == NULL) {
496     OVS_ERROR("No OVS DB table update callback found");
497     pthread_mutex_unlock(&pdb->mutex);
498     return -1;
499   }
500
501   /* call registered callback */
502   cb->table.call(jtable_updates);
503   pthread_mutex_unlock(&pdb->mutex);
504   return 0;
505 }
506
507 /* OVS DB result request handler.
508  * This callback is called by POLL thread if OVS DB
509  * result reply is received from the DB server.
510  * Once registered callback found, it's called
511  * by this handler. */
512 static int ovs_db_result_cb(ovs_db_t *pdb, yajl_val jnode) {
513   ovs_callback_t *cb = NULL;
514   yajl_val jresult;
515   yajl_val jerror;
516   yajl_val jid;
517   const char *result_path[] = {"result", NULL};
518   const char *error_path[] = {"error", NULL};
519   const char *id_path[] = {"id", NULL};
520
521   jresult = yajl_tree_get(jnode, result_path, yajl_t_any);
522   jerror = yajl_tree_get(jnode, error_path, yajl_t_any);
523   jid = yajl_tree_get(jnode, id_path, yajl_t_string);
524
525   /* check & get result attributes */
526   if (!jresult || !jerror || !jid)
527     return -1;
528
529   /* try to find registered callback */
530   pthread_mutex_lock(&pdb->mutex);
531   cb = ovs_db_table_callback_get(pdb, jid);
532   if (cb != NULL && cb->result.call != NULL) {
533     /* call registered callback */
534     cb->result.call(jresult, jerror);
535     /* unlock owner of the reply */
536     sem_post(&cb->result.sync);
537   }
538
539   pthread_mutex_unlock(&pdb->mutex);
540   return 0;
541 }
542
543 /* Handle JSON data (one request) and call
544  * appropriate event OVS DB handler. Currently,
545  * update callback 'ovs_db_table_update_cb' and
546  * result callback 'ovs_db_result_cb' is supported.
547  */
548 static int ovs_db_json_data_process(ovs_db_t *pdb, const char *data,
549                                     size_t len) {
550   const char *method = NULL;
551   char yajl_errbuf[OVS_YAJL_ERROR_BUFFER_SIZE];
552   const char *method_path[] = {"method", NULL};
553   const char *result_path[] = {"result", NULL};
554   char *sjson = NULL;
555   yajl_val jnode, jval;
556
557   /* duplicate the data to make null-terminated string
558    * required for yajl_tree_parse() */
559   if ((sjson = calloc(1, len + 1)) == NULL)
560     return -1;
561
562   sstrncpy(sjson, data, len + 1);
563   OVS_DEBUG("[len=%" PRIsz "] %s", len, sjson);
564
565   /* parse json data */
566   jnode = yajl_tree_parse(sjson, yajl_errbuf, sizeof(yajl_errbuf));
567   if (jnode == NULL) {
568     OVS_ERROR("yajl_tree_parse() %s", yajl_errbuf);
569     sfree(sjson);
570     return -1;
571   }
572
573   /* get method name */
574   if ((jval = yajl_tree_get(jnode, method_path, yajl_t_string)) != NULL) {
575     if ((method = YAJL_GET_STRING(jval)) == NULL) {
576       yajl_tree_free(jnode);
577       sfree(sjson);
578       return -1;
579     }
580     if (strcmp("echo", method) == 0) {
581       /* echo request from the server */
582       if (ovs_db_table_echo_cb(pdb, jnode) < 0)
583         OVS_ERROR("handle echo request failed");
584     } else if (strcmp("update", method) == 0) {
585       /* update notification */
586       if (ovs_db_table_update_cb(pdb, jnode) < 0)
587         OVS_ERROR("handle update notification failed");
588     }
589   } else if ((jval = yajl_tree_get(jnode, result_path, yajl_t_any)) != NULL) {
590     /* result notification */
591     if (ovs_db_result_cb(pdb, jnode) < 0)
592       OVS_ERROR("handle result reply failed");
593   } else
594     OVS_ERROR("connot find method or result failed");
595
596   /* release memory */
597   yajl_tree_free(jnode);
598   sfree(sjson);
599   return 0;
600 }
601
602 /*
603  * JSON reader implementation.
604  *
605  * This module process raw JSON data (byte stream) and
606  * returns fully-fledged JSON data which can be processed
607  * (parsed) by YAJL later.
608  */
609
610 /* Allocate JSON reader instance */
611 static ovs_json_reader_t *ovs_json_reader_alloc() {
612   ovs_json_reader_t *jreader = calloc(1, sizeof(*jreader));
613   if (jreader == NULL)
614     return NULL;
615
616   return jreader;
617 }
618
619 /* Push raw data into into the JSON reader for processing */
620 static int ovs_json_reader_push_data(ovs_json_reader_t *jreader,
621                                      const char *data, size_t data_len) {
622   char *new_buff = NULL;
623   size_t available = jreader->buff_size - jreader->buff_offset;
624
625   /* check/update required memory space */
626   if (available < data_len) {
627     OVS_DEBUG("Reallocate buffer [size=%d, available=%d required=%d]",
628               (int)jreader->buff_size, (int)available, (int)data_len);
629
630     /* allocate new chunk of memory */
631     new_buff = realloc(jreader->buff_ptr, (jreader->buff_size + data_len));
632     if (new_buff == NULL)
633       return -1;
634
635     /* point to new allocated memory */
636     jreader->buff_ptr = new_buff;
637     jreader->buff_size += data_len;
638   }
639
640   /* store input data */
641   memcpy(jreader->buff_ptr + jreader->buff_offset, data, data_len);
642   jreader->buff_offset += data_len;
643   return 0;
644 }
645
646 /* Pop one fully-fledged JSON if already exists. Returns 0 if
647  * completed JSON already exists otherwise negative value is
648  * returned */
649 static int ovs_json_reader_pop(ovs_json_reader_t *jreader,
650                                const char **json_ptr, size_t *json_len_ptr) {
651   size_t nbraces = 0;
652   size_t json_len = 0;
653   char *json = NULL;
654
655   /* search open/close brace */
656   for (size_t i = jreader->json_offset; i < jreader->buff_offset; i++) {
657     if (jreader->buff_ptr[i] == '{') {
658       nbraces++;
659     } else if (jreader->buff_ptr[i] == '}')
660       if (nbraces)
661         if (!(--nbraces)) {
662           /* JSON data */
663           *json_ptr = jreader->buff_ptr + jreader->json_offset;
664           *json_len_ptr = json_len + 1;
665           jreader->json_offset = i + 1;
666           return 0;
667         }
668
669     /* increase JSON data length */
670     if (nbraces)
671       json_len++;
672   }
673
674   if (jreader->json_offset) {
675     if (jreader->json_offset < jreader->buff_offset) {
676       /* shift data to the beginning of the buffer
677        * and zero rest of the buffer data */
678       json = &jreader->buff_ptr[jreader->json_offset];
679       json_len = jreader->buff_offset - jreader->json_offset;
680       for (size_t i = 0; i < jreader->buff_size; i++)
681         jreader->buff_ptr[i] = ((i < json_len) ? (json[i]) : (0));
682       jreader->buff_offset = json_len;
683     } else
684       /* reset the buffer */
685       jreader->buff_offset = 0;
686
687     /* data is at the beginning of the buffer */
688     jreader->json_offset = 0;
689   }
690
691   return -1;
692 }
693
694 /* Reset JSON reader. It is useful when start processing
695  * new raw data. E.g.: in case of lost stream connection.
696  */
697 static void ovs_json_reader_reset(ovs_json_reader_t *jreader) {
698   if (jreader) {
699     jreader->buff_offset = 0;
700     jreader->json_offset = 0;
701   }
702 }
703
704 /* Release internal data allocated for JSON reader */
705 static void ovs_json_reader_free(ovs_json_reader_t *jreader) {
706   if (jreader) {
707     free(jreader->buff_ptr);
708     free(jreader);
709   }
710 }
711
712 /* Reconnect to OVS DB and call the OVS DB post connection init callback
713  * if connection has been established.
714  */
715 static void ovs_db_reconnect(ovs_db_t *pdb) {
716   const char *node_info = pdb->node;
717   struct addrinfo *result;
718
719   if (pdb->unix_path[0] != '\0') {
720     /* use UNIX socket instead of INET address */
721     node_info = pdb->unix_path;
722
723     struct sockaddr_un *sa_unix = calloc(1, sizeof(*sa_unix));
724     if (sa_unix == NULL)
725       return;
726
727     result = calloc(1, sizeof(*result));
728     if (result == NULL) {
729       free(sa_unix);
730       return;
731     }
732
733     result->ai_family = AF_UNIX;
734     result->ai_socktype = SOCK_STREAM;
735     result->ai_addrlen = sizeof(*sa_unix);
736     result->ai_addr = (struct sockaddr *)sa_unix;
737     sa_unix->sun_family = result->ai_family;
738     sstrncpy(sa_unix->sun_path, pdb->unix_path, sizeof(sa_unix->sun_path));
739   } else {
740     /* inet socket address */
741     struct addrinfo hints;
742
743     /* setup criteria for selecting the socket address */
744     memset(&hints, 0, sizeof(hints));
745     hints.ai_family = AF_UNSPEC;
746     hints.ai_socktype = SOCK_STREAM;
747
748     /* get socket addresses */
749     int ret = getaddrinfo(pdb->node, pdb->service, &hints, &result);
750     if (ret != 0) {
751       OVS_ERROR("getaddrinfo(): %s", gai_strerror(ret));
752       return;
753     }
754   }
755   /* try to connect to the server */
756   for (struct addrinfo *rp = result; rp != NULL; rp = rp->ai_next) {
757     int sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
758     if (sock < 0) {
759       OVS_DEBUG("socket(): %s", STRERRNO);
760       continue;
761     }
762     if (connect(sock, rp->ai_addr, rp->ai_addrlen) < 0) {
763       close(sock);
764       OVS_DEBUG("connect(): %s [family=%d]", STRERRNO, rp->ai_family);
765     } else {
766       /* send notification to event thread */
767       pdb->sock = sock;
768       ovs_db_event_post(pdb, OVS_DB_EVENT_CONN_ESTABLISHED);
769       break;
770     }
771   }
772
773   if (pdb->sock < 0)
774     OVS_ERROR("connect to \"%s\" failed", node_info);
775
776   freeaddrinfo(result);
777 }
778
779 /* POLL worker thread.
780  * It listens on OVS DB connection for incoming
781  * requests/reply/events etc. Also, it reconnects to OVS DB
782  * if connection has been lost.
783  */
784 static void *ovs_poll_worker(void *arg) {
785   ovs_db_t *pdb = (ovs_db_t *)arg; /* pointer to OVS DB */
786   ovs_json_reader_t *jreader = NULL;
787   struct pollfd poll_fd = {
788       .fd = pdb->sock, .events = POLLIN | POLLPRI, .revents = 0,
789   };
790
791   /* create JSON reader instance */
792   if ((jreader = ovs_json_reader_alloc()) == NULL) {
793     OVS_ERROR("initialize json reader failed");
794     return NULL;
795   }
796
797   /* poll data */
798   while (ovs_db_poll_is_running(pdb)) {
799     poll_fd.fd = pdb->sock;
800     int poll_ret = poll(&poll_fd, 1, /* ms */ OVS_DB_POLL_TIMEOUT * 1000);
801     if (poll_ret < 0) {
802       OVS_ERROR("poll(): %s", STRERRNO);
803       break;
804     } else if (poll_ret == 0) {
805       OVS_DEBUG("poll(): timeout");
806       if (pdb->sock < 0)
807         /* invalid fd, so try to reconnect */
808         ovs_db_reconnect(pdb);
809       continue;
810     }
811     if (poll_fd.revents & POLLNVAL) {
812       /* invalid file descriptor, clean-up */
813       ovs_db_callback_remove_all(pdb);
814       ovs_json_reader_reset(jreader);
815       /* setting poll FD to -1 tells poll() call to ignore this FD.
816        * In that case poll() call will return timeout all the time */
817       pdb->sock = (-1);
818     } else if ((poll_fd.revents & POLLERR) || (poll_fd.revents & POLLHUP)) {
819       /* connection is broken */
820       close(poll_fd.fd);
821       ovs_db_event_post(pdb, OVS_DB_EVENT_CONN_TERMINATED);
822       OVS_ERROR("poll() peer closed its end of the channel");
823     } else if ((poll_fd.revents & POLLIN) || (poll_fd.revents & POLLPRI)) {
824       /* read incoming data */
825       char buff[OVS_DB_POLL_READ_BLOCK_SIZE];
826       ssize_t nbytes = recv(poll_fd.fd, buff, sizeof(buff), 0);
827       if (nbytes < 0) {
828         OVS_ERROR("recv(): %s", STRERRNO);
829         /* read error? Try to reconnect */
830         close(poll_fd.fd);
831         continue;
832       } else if (nbytes == 0) {
833         close(poll_fd.fd);
834         ovs_db_event_post(pdb, OVS_DB_EVENT_CONN_TERMINATED);
835         OVS_ERROR("recv() peer has performed an orderly shutdown");
836         continue;
837       }
838       /* read incoming data */
839       size_t json_len = 0;
840       const char *json = NULL;
841       OVS_DEBUG("recv(): received %zd bytes of data", nbytes);
842       ovs_json_reader_push_data(jreader, buff, nbytes);
843       while (!ovs_json_reader_pop(jreader, &json, &json_len))
844         /* process JSON data */
845         ovs_db_json_data_process(pdb, json, json_len);
846     }
847   }
848
849   OVS_DEBUG("poll thread has been completed");
850   ovs_json_reader_free(jreader);
851   return NULL;
852 }
853
854 /* EVENT worker thread.
855  * Perform task based on incoming events. This
856  * task can be done asynchronously which allows to
857  * handle OVS DB callback like 'init_cb'.
858  */
859 static void *ovs_event_worker(void *arg) {
860   ovs_db_t *pdb = (ovs_db_t *)arg;
861
862   while (pdb->event_thread.value != OVS_DB_EVENT_TERMINATE) {
863     /* wait for an event */
864     struct timespec ts;
865     clock_gettime(CLOCK_REALTIME, &ts);
866     ts.tv_sec += (OVS_DB_EVENT_TIMEOUT);
867     int ret = pthread_cond_timedwait(&pdb->event_thread.cond,
868                                      &pdb->event_thread.mutex, &ts);
869     if (!ret || ret == ETIMEDOUT) {
870       /* handle the event */
871       OVS_DEBUG("handle event %d", pdb->event_thread.value);
872       switch (pdb->event_thread.value) {
873       case OVS_DB_EVENT_CONN_ESTABLISHED:
874         if (pdb->cb.post_conn_init)
875           pdb->cb.post_conn_init(pdb);
876         /* reset event */
877         pdb->event_thread.value = OVS_DB_EVENT_NONE;
878         break;
879       case OVS_DB_EVENT_CONN_TERMINATED:
880         if (pdb->cb.post_conn_terminate)
881           pdb->cb.post_conn_terminate();
882         /* reset event */
883         pdb->event_thread.value = OVS_DB_EVENT_NONE;
884         break;
885       case OVS_DB_EVENT_NONE:
886         /* wait timeout */
887         OVS_DEBUG("no event received (timeout)");
888         break;
889       default:
890         OVS_DEBUG("unknown event received");
891         break;
892       }
893     } else {
894       /* unexpected error */
895       OVS_ERROR("pthread_cond_timedwait() failed");
896       break;
897     }
898   }
899
900   OVS_DEBUG("event thread has been completed");
901   return NULL;
902 }
903
904 /* Initialize EVENT thread */
905 static int ovs_db_event_thread_init(ovs_db_t *pdb) {
906   pdb->event_thread.tid = (pthread_t){0};
907   /* init event thread condition variable */
908   if (pthread_cond_init(&pdb->event_thread.cond, NULL)) {
909     return -1;
910   }
911   /* init event thread mutex */
912   if (pthread_mutex_init(&pdb->event_thread.mutex, NULL)) {
913     pthread_cond_destroy(&pdb->event_thread.cond);
914     return -1;
915   }
916   /* Hold the event thread mutex. It ensures that no events
917    * will be lost while thread is still starting. Once event
918    * thread is started and ready to accept events, it will release
919    * the mutex */
920   if (pthread_mutex_lock(&pdb->event_thread.mutex)) {
921     pthread_mutex_destroy(&pdb->event_thread.mutex);
922     pthread_cond_destroy(&pdb->event_thread.cond);
923     return -1;
924   }
925   /* start event thread */
926   pthread_t tid;
927   if (plugin_thread_create(&tid, NULL, ovs_event_worker, pdb,
928                            "utils_ovs:event") != 0) {
929     pthread_mutex_unlock(&pdb->event_thread.mutex);
930     pthread_mutex_destroy(&pdb->event_thread.mutex);
931     pthread_cond_destroy(&pdb->event_thread.cond);
932     return -1;
933   }
934   pdb->event_thread.tid = tid;
935   return 0;
936 }
937
938 /* Terminate EVENT thread */
939 static int ovs_db_event_thread_terminate(ovs_db_t *pdb) {
940   if (pthread_equal(pdb->event_thread.tid, (pthread_t){0})) {
941     /* already terminated */
942     return 0;
943   }
944   ovs_db_event_post(pdb, OVS_DB_EVENT_TERMINATE);
945   if (pthread_join(pdb->event_thread.tid, NULL) != 0)
946     return -1;
947   /* Event thread always holds the thread mutex when
948    * performs some task (handles event) and releases it when
949    * while sleeping. Thus, if event thread exits, the mutex
950    * remains locked */
951   pdb->event_thread.tid = (pthread_t){0};
952   pthread_mutex_unlock(&pdb->event_thread.mutex);
953   return 0;
954 }
955
956 /* Destroy EVENT thread private data */
957 static void ovs_db_event_thread_data_destroy(ovs_db_t *pdb) {
958   /* destroy mutex */
959   pthread_mutex_destroy(&pdb->event_thread.mutex);
960   pthread_cond_destroy(&pdb->event_thread.cond);
961 }
962
963 /* Initialize POLL thread */
964 static int ovs_db_poll_thread_init(ovs_db_t *pdb) {
965   pdb->poll_thread.tid = (pthread_t){0};
966   /* init event thread mutex */
967   if (pthread_mutex_init(&pdb->poll_thread.mutex, NULL)) {
968     return -1;
969   }
970   /* start poll thread */
971   pthread_t tid;
972   pdb->poll_thread.state = OVS_DB_POLL_STATE_RUNNING;
973   if (plugin_thread_create(&tid, NULL, ovs_poll_worker, pdb,
974                            "utils_ovs:poll") != 0) {
975     pthread_mutex_destroy(&pdb->poll_thread.mutex);
976     return -1;
977   }
978   pdb->poll_thread.tid = tid;
979   return 0;
980 }
981
982 /* Destroy POLL thread */
983 /* XXX: Must hold pdb->mutex when calling! */
984 static int ovs_db_poll_thread_destroy(ovs_db_t *pdb) {
985   if (pthread_equal(pdb->poll_thread.tid, (pthread_t){0})) {
986     /* already destroyed */
987     return 0;
988   }
989   /* change thread state */
990   pthread_mutex_lock(&pdb->poll_thread.mutex);
991   pdb->poll_thread.state = OVS_DB_POLL_STATE_EXITING;
992   pthread_mutex_unlock(&pdb->poll_thread.mutex);
993   /* join the thread */
994   if (pthread_join(pdb->poll_thread.tid, NULL) != 0)
995     return -1;
996   pthread_mutex_destroy(&pdb->poll_thread.mutex);
997   pdb->poll_thread.tid = (pthread_t){0};
998   return 0;
999 }
1000
1001 /*
1002  * Public OVS DB API implementation
1003  */
1004
1005 ovs_db_t *ovs_db_init(const char *node, const char *service,
1006                       const char *unix_path, ovs_db_callback_t *cb) {
1007   int ret;
1008
1009   /* sanity check */
1010   if (node == NULL || service == NULL || unix_path == NULL)
1011     return NULL;
1012
1013   /* allocate db data & fill it */
1014   ovs_db_t *pdb = calloc(1, sizeof(*pdb));
1015   if (pdb == NULL)
1016     return NULL;
1017   pdb->sock = -1;
1018
1019   /* store the OVS DB address */
1020   sstrncpy(pdb->node, node, sizeof(pdb->node));
1021   sstrncpy(pdb->service, service, sizeof(pdb->service));
1022   sstrncpy(pdb->unix_path, unix_path, sizeof(pdb->unix_path));
1023
1024   /* setup OVS DB callbacks */
1025   if (cb)
1026     pdb->cb = *cb;
1027
1028   /* init OVS DB mutex attributes */
1029   pthread_mutexattr_t mutex_attr;
1030   if (pthread_mutexattr_init(&mutex_attr)) {
1031     OVS_ERROR("OVS DB mutex attribute init failed");
1032     sfree(pdb);
1033     return NULL;
1034   }
1035   /* set OVS DB mutex as recursive */
1036   if (pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE)) {
1037     OVS_ERROR("Failed to set OVS DB mutex as recursive");
1038     pthread_mutexattr_destroy(&mutex_attr);
1039     sfree(pdb);
1040     return NULL;
1041   }
1042   /* init OVS DB mutex */
1043   if (pthread_mutex_init(&pdb->mutex, &mutex_attr)) {
1044     OVS_ERROR("OVS DB mutex init failed");
1045     pthread_mutexattr_destroy(&mutex_attr);
1046     sfree(pdb);
1047     return NULL;
1048   }
1049   /* destroy mutex attributes */
1050   pthread_mutexattr_destroy(&mutex_attr);
1051
1052   /* init event thread */
1053   if (ovs_db_event_thread_init(pdb) < 0) {
1054     ret = ovs_db_destroy(pdb);
1055     if (ret > 0)
1056       goto failure;
1057     else
1058       return NULL;
1059   }
1060
1061   /* init polling thread */
1062   if (ovs_db_poll_thread_init(pdb) < 0) {
1063     ret = ovs_db_destroy(pdb);
1064     if (ret > 0) {
1065       ovs_db_event_thread_data_destroy(pdb);
1066       goto failure;
1067     } else {
1068       return NULL;
1069     }
1070   }
1071   return pdb;
1072
1073 failure:
1074   pthread_mutex_destroy(&pdb->mutex);
1075   sfree(pdb);
1076   return NULL;
1077 }
1078
1079 int ovs_db_send_request(ovs_db_t *pdb, const char *method, const char *params,
1080                         ovs_db_result_cb_t cb) {
1081   int ret = 0;
1082   yajl_gen_status yajl_gen_ret;
1083   yajl_val jparams;
1084   yajl_gen jgen;
1085   ovs_callback_t *new_cb = NULL;
1086   uint64_t uid;
1087   char uid_buff[OVS_UID_STR_SIZE];
1088   const char *req = NULL;
1089   size_t req_len = 0;
1090   struct timespec ts;
1091
1092   /* sanity check */
1093   if (!pdb || !method || !params)
1094     return -1;
1095
1096   if ((jgen = yajl_gen_alloc(NULL)) == NULL)
1097     return -1;
1098
1099   /* try to parse params */
1100   if ((jparams = yajl_tree_parse(params, NULL, 0)) == NULL) {
1101     OVS_ERROR("params is not a JSON string");
1102     yajl_gen_clear(jgen);
1103     return -1;
1104   }
1105
1106   /* generate method field */
1107   OVS_YAJL_CALL(yajl_gen_map_open, jgen);
1108
1109   OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "method");
1110   OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, method);
1111
1112   /* generate params field */
1113   OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "params");
1114   OVS_YAJL_CALL(ovs_yajl_gen_val, jgen, jparams);
1115   yajl_tree_free(jparams);
1116
1117   /* generate id field */
1118   OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "id");
1119   uid = ovs_uid_generate();
1120   ssnprintf(uid_buff, sizeof(uid_buff), "%" PRIX64, uid);
1121   OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, uid_buff);
1122
1123   OVS_YAJL_CALL(yajl_gen_map_close, jgen);
1124
1125   if (cb) {
1126     /* register result callback */
1127     if ((new_cb = calloc(1, sizeof(*new_cb))) == NULL)
1128       goto yajl_gen_failure;
1129
1130     /* add new callback to front */
1131     sem_init(&new_cb->result.sync, 0, 0);
1132     new_cb->result.call = cb;
1133     new_cb->uid = uid;
1134     ovs_db_callback_add(pdb, new_cb);
1135   }
1136
1137   /* send the request */
1138   OVS_YAJL_CALL(yajl_gen_get_buf, jgen, (const unsigned char **)&req, &req_len);
1139   OVS_DEBUG("%s", req);
1140   if (!ovs_db_data_send(pdb, req, req_len)) {
1141     if (cb) {
1142       /* wait for result */
1143       clock_gettime(CLOCK_REALTIME, &ts);
1144       ts.tv_sec += OVS_DB_SEND_REQ_TIMEOUT;
1145       if (sem_timedwait(&new_cb->result.sync, &ts) < 0) {
1146         OVS_ERROR("%s() no replay received within %d sec", __FUNCTION__,
1147                   OVS_DB_SEND_REQ_TIMEOUT);
1148         ret = (-1);
1149       }
1150     }
1151   } else {
1152     OVS_ERROR("ovs_db_data_send() failed");
1153     ret = (-1);
1154   }
1155
1156 yajl_gen_failure:
1157   if (new_cb) {
1158     /* destroy callback */
1159     sem_destroy(&new_cb->result.sync);
1160     ovs_db_callback_remove(pdb, new_cb);
1161   }
1162
1163   /* release memory */
1164   yajl_gen_clear(jgen);
1165   return (yajl_gen_ret != yajl_gen_status_ok) ? (-1) : ret;
1166 }
1167
1168 int ovs_db_table_cb_register(ovs_db_t *pdb, const char *tb_name,
1169                              const char **tb_column,
1170                              ovs_db_table_cb_t update_cb,
1171                              ovs_db_result_cb_t result_cb, unsigned int flags) {
1172   yajl_gen jgen;
1173   yajl_gen_status yajl_gen_ret;
1174   ovs_callback_t *new_cb = NULL;
1175   char uid_str[OVS_UID_STR_SIZE];
1176   char *params;
1177   size_t params_len;
1178   int ovs_db_ret = 0;
1179
1180   /* sanity check */
1181   if (pdb == NULL || tb_name == NULL || update_cb == NULL)
1182     return -1;
1183
1184   /* allocate new update callback */
1185   if ((new_cb = calloc(1, sizeof(*new_cb))) == NULL)
1186     return -1;
1187
1188   /* init YAJL generator */
1189   if ((jgen = yajl_gen_alloc(NULL)) == NULL) {
1190     sfree(new_cb);
1191     return -1;
1192   }
1193
1194   /* add new callback to front */
1195   new_cb->table.call = update_cb;
1196   new_cb->uid = ovs_uid_generate();
1197   ovs_db_callback_add(pdb, new_cb);
1198
1199   /* make update notification request
1200    * [<db-name>, <json-value>, <monitor-requests>] */
1201   OVS_YAJL_CALL(yajl_gen_array_open, jgen);
1202   {
1203     OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, OVS_DB_DEFAULT_DB_NAME);
1204
1205     /* uid string <json-value> */
1206     ssnprintf(uid_str, sizeof(uid_str), "%" PRIX64, new_cb->uid);
1207     OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, uid_str);
1208
1209     /* <monitor-requests> */
1210     OVS_YAJL_CALL(yajl_gen_map_open, jgen);
1211     {
1212       OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, tb_name);
1213       OVS_YAJL_CALL(yajl_gen_array_open, jgen);
1214       {
1215         /* <monitor-request> */
1216         OVS_YAJL_CALL(yajl_gen_map_open, jgen);
1217         {
1218           if (tb_column) {
1219             /* columns within the table to be monitored */
1220             OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "columns");
1221             OVS_YAJL_CALL(yajl_gen_array_open, jgen);
1222             for (; *tb_column; tb_column++)
1223               OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, *tb_column);
1224             OVS_YAJL_CALL(yajl_gen_array_close, jgen);
1225           }
1226           /* specify select option */
1227           OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "select");
1228           {
1229             OVS_YAJL_CALL(yajl_gen_map_open, jgen);
1230             {
1231               OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "initial");
1232               OVS_YAJL_CALL(yajl_gen_bool, jgen,
1233                             flags & OVS_DB_TABLE_CB_FLAG_INITIAL);
1234               OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "insert");
1235               OVS_YAJL_CALL(yajl_gen_bool, jgen,
1236                             flags & OVS_DB_TABLE_CB_FLAG_INSERT);
1237               OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "delete");
1238               OVS_YAJL_CALL(yajl_gen_bool, jgen,
1239                             flags & OVS_DB_TABLE_CB_FLAG_DELETE);
1240               OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, "modify");
1241               OVS_YAJL_CALL(yajl_gen_bool, jgen,
1242                             flags & OVS_DB_TABLE_CB_FLAG_MODIFY);
1243             }
1244             OVS_YAJL_CALL(yajl_gen_map_close, jgen);
1245           }
1246         }
1247         OVS_YAJL_CALL(yajl_gen_map_close, jgen);
1248       }
1249       OVS_YAJL_CALL(yajl_gen_array_close, jgen);
1250     }
1251     OVS_YAJL_CALL(yajl_gen_map_close, jgen);
1252   }
1253   OVS_YAJL_CALL(yajl_gen_array_close, jgen);
1254
1255   /* make a request to subscribe to given table */
1256   OVS_YAJL_CALL(yajl_gen_get_buf, jgen, (const unsigned char **)&params,
1257                 &params_len);
1258   if (ovs_db_send_request(pdb, "monitor", params, result_cb) < 0) {
1259     OVS_ERROR("Failed to subscribe to \"%s\" table", tb_name);
1260     ovs_db_ret = (-1);
1261   }
1262
1263 yajl_gen_failure:
1264   /* release memory */
1265   yajl_gen_clear(jgen);
1266   return ovs_db_ret;
1267 }
1268
1269 int ovs_db_destroy(ovs_db_t *pdb) {
1270   int ovs_db_ret = 0;
1271   int ret = 0;
1272
1273   /* sanity check */
1274   if (pdb == NULL)
1275     return -1;
1276
1277   /* stop event thread */
1278   if (ovs_db_event_thread_terminate(pdb) < 0) {
1279     OVS_ERROR("stop event thread failed");
1280     ovs_db_ret = -1;
1281   }
1282
1283   /* try to lock the structure before releasing */
1284   if ((ret = pthread_mutex_lock(&pdb->mutex))) {
1285     OVS_ERROR("pthread_mutex_lock() DB mutex lock failed (%d)", ret);
1286     return ret;
1287   }
1288
1289   /* stop poll thread and destroy thread's private data */
1290   if (ovs_db_poll_thread_destroy(pdb) < 0) {
1291     OVS_ERROR("destroy poll thread failed");
1292     ovs_db_ret = -1;
1293   }
1294
1295   /* destroy event thread private data */
1296   ovs_db_event_thread_data_destroy(pdb);
1297
1298   pthread_mutex_unlock(&pdb->mutex);
1299
1300   /* unsubscribe callbacks */
1301   ovs_db_callback_remove_all(pdb);
1302
1303   /* close connection */
1304   if (pdb->sock >= 0)
1305     close(pdb->sock);
1306
1307   /* release DB handler */
1308   pthread_mutex_destroy(&pdb->mutex);
1309   sfree(pdb);
1310   return ovs_db_ret;
1311 }
1312
1313 /*
1314  * Public OVS utils API implementation
1315  */
1316
1317 /* Get YAJL value by key from YAJL dictionary
1318  *
1319  * EXAMPLE:
1320  *  {
1321  *    "key_a" : <YAJL return value>
1322  *    "key_b" : <YAJL return value>
1323  *  }
1324  */
1325 yajl_val ovs_utils_get_value_by_key(yajl_val jval, const char *key) {
1326   const char *obj_key = NULL;
1327
1328   /* check params */
1329   if (!YAJL_IS_OBJECT(jval) || (key == NULL))
1330     return NULL;
1331
1332   /* find a value by key */
1333   for (size_t i = 0; i < YAJL_GET_OBJECT(jval)->len; i++) {
1334     obj_key = YAJL_GET_OBJECT(jval)->keys[i];
1335     if (strcmp(obj_key, key) == 0)
1336       return YAJL_GET_OBJECT(jval)->values[i];
1337   }
1338
1339   return NULL;
1340 }
1341
1342 /* Get OVS DB map value by given map key
1343  *
1344  * FROM RFC7047:
1345  *
1346  *   <pair>
1347  *     A 2-element JSON array that represents a pair within a database
1348  *     map.  The first element is an <atom> that represents the key, and
1349  *     the second element is an <atom> that represents the value.
1350  *
1351  *   <map>
1352  *     A 2-element JSON array that represents a database map value.  The
1353  *     first element of the array must be the string "map", and the
1354  *     second element must be an array of zero or more <pair>s giving the
1355  *     values in the map.  All of the <pair>s must have the same key and
1356  *     value types.
1357  *
1358  * EXAMPLE:
1359  *  [
1360  *    "map", [
1361  *             [ "key_a", <YAJL value>], [ "key_b", <YAJL value>], ...
1362  *           ]
1363  *  ]
1364  */
1365 yajl_val ovs_utils_get_map_value(yajl_val jval, const char *key) {
1366   size_t map_len = 0;
1367   size_t array_len = 0;
1368   yajl_val *map_values = NULL;
1369   yajl_val *array_values = NULL;
1370   const char *str_val = NULL;
1371
1372   /* check YAJL array */
1373   if (!YAJL_IS_ARRAY(jval) || (key == NULL))
1374     return NULL;
1375
1376   /* check a database map value (2-element, first one should be a string */
1377   array_len = YAJL_GET_ARRAY(jval)->len;
1378   array_values = YAJL_GET_ARRAY(jval)->values;
1379   if ((array_len != 2) || (!YAJL_IS_STRING(array_values[0])) ||
1380       (!YAJL_IS_ARRAY(array_values[1])))
1381     return NULL;
1382
1383   /* check first element of the array */
1384   str_val = YAJL_GET_STRING(array_values[0]);
1385   if (str_val == NULL || strcmp("map", str_val) != 0)
1386     return NULL;
1387
1388   /* try to find map value by map key */
1389   if (YAJL_GET_ARRAY(array_values[1]) == NULL)
1390     return NULL;
1391
1392   map_len = YAJL_GET_ARRAY(array_values[1])->len;
1393   map_values = YAJL_GET_ARRAY(array_values[1])->values;
1394   for (size_t i = 0; i < map_len; i++) {
1395     /* check YAJL array */
1396     if (!YAJL_IS_ARRAY(map_values[i]) || YAJL_GET_ARRAY(map_values[i]) == NULL)
1397       break;
1398
1399     /* check a database pair value (2-element, first one represents a key
1400      * and it should be a string in our case */
1401     array_len = YAJL_GET_ARRAY(map_values[i])->len;
1402     array_values = YAJL_GET_ARRAY(map_values[i])->values;
1403     if ((array_len != 2) || (!YAJL_IS_STRING(array_values[0])))
1404       break;
1405
1406     /* return map value if given key equals map key */
1407     str_val = YAJL_GET_STRING(array_values[0]);
1408     if (str_val != NULL && strcmp(key, str_val) == 0)
1409       return array_values[1];
1410   }
1411   return NULL;
1412 }