Setting tag sliver-openvswitch-2.2.90-1
[sliver-openvswitch.git] / ovsdb / jsonrpc-server.c
index 0cc8bdf..cfaa656 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
+/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
 
 #include "jsonrpc-server.h"
 
-#include <assert.h>
 #include <errno.h>
 
 #include "bitmap.h"
 #include "ovsdb-error.h"
 #include "ovsdb-parser.h"
 #include "ovsdb.h"
+#include "poll-loop.h"
 #include "reconnect.h"
 #include "row.h"
 #include "server.h"
+#include "simap.h"
 #include "stream.h"
 #include "table.h"
 #include "timeval.h"
@@ -51,6 +52,8 @@ static struct ovsdb_jsonrpc_session *ovsdb_jsonrpc_session_create(
     struct ovsdb_jsonrpc_remote *, struct jsonrpc_session *);
 static void ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *);
 static void ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *);
+static void ovsdb_jsonrpc_session_get_memory_usage_all(
+    const struct ovsdb_jsonrpc_remote *, struct simap *usage);
 static void ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *);
 static void ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *);
 static void ovsdb_jsonrpc_session_set_all_options(
@@ -60,9 +63,12 @@ static bool ovsdb_jsonrpc_session_get_status(
     struct ovsdb_jsonrpc_remote_status *);
 static void ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *);
 static void ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *);
+static void ovsdb_jsonrpc_session_send(struct ovsdb_jsonrpc_session *,
+                                       struct jsonrpc_msg *);
 
 /* Triggers. */
 static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *,
+                                         struct ovsdb *,
                                          struct json *id, struct json *params);
 static struct ovsdb_jsonrpc_trigger *ovsdb_jsonrpc_trigger_find(
     struct ovsdb_jsonrpc_session *, const struct json *id, size_t hash);
@@ -73,12 +79,14 @@ static void ovsdb_jsonrpc_trigger_complete_done(
 
 /* Monitors. */
 static struct json *ovsdb_jsonrpc_monitor_create(
-    struct ovsdb_jsonrpc_session *, struct json *params);
+    struct ovsdb_jsonrpc_session *, struct ovsdb *, struct json *params);
 static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
     struct ovsdb_jsonrpc_session *,
     struct json_array *params,
     const struct json *request_id);
 static void ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *);
+static void ovsdb_jsonrpc_monitor_flush_all(struct ovsdb_jsonrpc_session *);
+static bool ovsdb_jsonrpc_monitor_needs_flush(struct ovsdb_jsonrpc_session *);
 \f
 /* JSON-RPC database server. */
 
@@ -95,6 +103,7 @@ struct ovsdb_jsonrpc_remote {
     struct ovsdb_jsonrpc_server *server;
     struct pstream *listener;   /* Listener, if passive. */
     struct list sessions;       /* List of "struct ovsdb_jsonrpc_session"s. */
+    uint8_t dscp;
 };
 
 static struct ovsdb_jsonrpc_remote *ovsdb_jsonrpc_server_add_remote(
@@ -103,16 +112,56 @@ static struct ovsdb_jsonrpc_remote *ovsdb_jsonrpc_server_add_remote(
 );
 static void ovsdb_jsonrpc_server_del_remote(struct shash_node *);
 
+/* Creates and returns a new server to provide JSON-RPC access to an OVSDB.
+ *
+ * The caller must call ovsdb_jsonrpc_server_add_db() for each database to
+ * which 'server' should provide access. */
 struct ovsdb_jsonrpc_server *
-ovsdb_jsonrpc_server_create(struct ovsdb *db)
+ovsdb_jsonrpc_server_create(void)
 {
     struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
-    ovsdb_server_init(&server->up, db);
+    ovsdb_server_init(&server->up);
     server->max_sessions = 64;
     shash_init(&server->remotes);
     return server;
 }
 
+/* Adds 'db' to the set of databases served out by 'svr'.  Returns true if
+ * successful, false if 'db''s name is the same as some database already in
+ * 'server'. */
+bool
+ovsdb_jsonrpc_server_add_db(struct ovsdb_jsonrpc_server *svr, struct ovsdb *db)
+{
+    /* The OVSDB protocol doesn't have a way to notify a client that a
+     * database has been added.  If some client tried to use the database
+     * that we're adding and failed, then forcing it to reconnect seems like
+     * a reasonable way to make it try again.
+     *
+     * If this is too big of a hammer in practice, we could be more selective,
+     * e.g. disconnect only connections that actually tried to use a database
+     * with 'db''s name. */
+    ovsdb_jsonrpc_server_reconnect(svr);
+
+    return ovsdb_server_add_db(&svr->up, db);
+}
+
+/* Removes 'db' from the set of databases served out by 'svr'.  Returns
+ * true if successful, false if there is no database associated with 'db'. */
+bool
+ovsdb_jsonrpc_server_remove_db(struct ovsdb_jsonrpc_server *svr,
+                               struct ovsdb *db)
+{
+    /* There might be pointers to 'db' from 'svr', such as monitors or
+     * outstanding transactions.  Disconnect all JSON-RPC connections to avoid
+     * accesses to freed memory.
+     *
+     * If this is too big of a hammer in practice, we could be more selective,
+     * e.g. disconnect only connections that actually reference 'db'. */
+    ovsdb_jsonrpc_server_reconnect(svr);
+
+    return ovsdb_server_remove_db(&svr->up, db);
+}
+
 void
 ovsdb_jsonrpc_server_destroy(struct ovsdb_jsonrpc_server *svr)
 {
@@ -181,7 +230,7 @@ ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
 
     error = jsonrpc_pstream_open(name, &listener, options->dscp);
     if (error && error != EAFNOSUPPORT) {
-        VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, strerror(error));
+        VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, ovs_strerror(error));
         return NULL;
     }
 
@@ -189,10 +238,11 @@ ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
     remote->server = svr;
     remote->listener = listener;
     list_init(&remote->sessions);
+    remote->dscp = options->dscp;
     shash_add(&svr->remotes, name, remote);
 
     if (!listener) {
-        ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name));
+        ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name, true));
     }
     return remote;
 }
@@ -265,12 +315,13 @@ ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
             error = pstream_accept(remote->listener, &stream);
             if (!error) {
                 struct jsonrpc_session *js;
-                js = jsonrpc_session_open_unreliably(jsonrpc_open(stream));
+                js = jsonrpc_session_open_unreliably(jsonrpc_open(stream),
+                                                     remote->dscp);
                 ovsdb_jsonrpc_session_create(remote, js);
             } else if (error != EAGAIN) {
                 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
                              pstream_get_name(remote->listener),
-                             strerror(error));
+                             ovs_strerror(error));
             }
         }
 
@@ -293,6 +344,22 @@ ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
         ovsdb_jsonrpc_session_wait_all(remote);
     }
 }
+
+/* Adds some memory usage statistics for 'svr' into 'usage', for use with
+ * memory_report(). */
+void
+ovsdb_jsonrpc_server_get_memory_usage(const struct ovsdb_jsonrpc_server *svr,
+                                      struct simap *usage)
+{
+    struct shash_node *node;
+
+    simap_increase(usage, "sessions", svr->n_sessions);
+    SHASH_FOR_EACH (node, &svr->remotes) {
+        struct ovsdb_jsonrpc_remote *remote = node->data;
+
+        ovsdb_jsonrpc_session_get_memory_usage_all(remote, usage);
+    }
+}
 \f
 /* JSON-RPC database server session. */
 
@@ -315,6 +382,8 @@ struct ovsdb_jsonrpc_session {
 static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
 static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
 static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
+static void ovsdb_jsonrpc_session_get_memory_usage(
+    const struct ovsdb_jsonrpc_session *, struct simap *usage);
 static void ovsdb_jsonrpc_session_set_options(
     struct ovsdb_jsonrpc_session *, const struct ovsdb_jsonrpc_options *);
 static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
@@ -329,7 +398,7 @@ ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
     struct ovsdb_jsonrpc_session *s;
 
     s = xzalloc(sizeof *s);
-    ovsdb_session_init(&s->up, remote->server->up.db);
+    ovsdb_session_init(&s->up, &remote->server->up);
     s->remote = remote;
     list_push_back(&remote->sessions, &s->node);
     hmap_init(&s->triggers);
@@ -347,9 +416,13 @@ ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
 {
     ovsdb_jsonrpc_monitor_remove_all(s);
     ovsdb_jsonrpc_session_unlock_all(s);
+    ovsdb_jsonrpc_trigger_complete_all(s);
+
+    hmap_destroy(&s->monitors);
+    hmap_destroy(&s->triggers);
+
     jsonrpc_session_close(s->js);
     list_remove(&s->node);
-    ovsdb_session_destroy(&s->up);
     s->remote->server->n_sessions--;
     ovsdb_session_destroy(&s->up);
     free(s);
@@ -369,7 +442,11 @@ ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
     ovsdb_jsonrpc_trigger_complete_done(s);
 
     if (!jsonrpc_session_get_backlog(s->js)) {
-        struct jsonrpc_msg *msg = jsonrpc_session_recv(s->js);
+        struct jsonrpc_msg *msg;
+
+        ovsdb_jsonrpc_monitor_flush_all(s);
+
+        msg = jsonrpc_session_recv(s->js);
         if (msg) {
             if (msg->type == JSONRPC_REQUEST) {
                 ovsdb_jsonrpc_session_got_request(s, msg);
@@ -414,7 +491,11 @@ ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
 {
     jsonrpc_session_wait(s->js);
     if (!jsonrpc_session_get_backlog(s->js)) {
-        jsonrpc_session_recv_wait(s->js);
+        if (ovsdb_jsonrpc_monitor_needs_flush(s)) {
+            poll_immediate_wake();
+        } else {
+            jsonrpc_session_recv_wait(s->js);
+        }
     }
 }
 
@@ -428,6 +509,27 @@ ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote)
     }
 }
 
+static void
+ovsdb_jsonrpc_session_get_memory_usage(const struct ovsdb_jsonrpc_session *s,
+                                       struct simap *usage)
+{
+    simap_increase(usage, "triggers", hmap_count(&s->triggers));
+    simap_increase(usage, "monitors", hmap_count(&s->monitors));
+    simap_increase(usage, "backlog", jsonrpc_session_get_backlog(s->js));
+}
+
+static void
+ovsdb_jsonrpc_session_get_memory_usage_all(
+    const struct ovsdb_jsonrpc_remote *remote,
+    struct simap *usage)
+{
+    struct ovsdb_jsonrpc_session *s;
+
+    LIST_FOR_EACH (s, node, &remote->sessions) {
+        ovsdb_jsonrpc_session_get_memory_usage(s, usage);
+    }
+}
+
 static void
 ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote)
 {
@@ -462,6 +564,22 @@ ovsdb_jsonrpc_session_set_all_options(
 {
     struct ovsdb_jsonrpc_session *s;
 
+    if (remote->listener) {
+        int error;
+
+        error = pstream_set_dscp(remote->listener, options->dscp);
+        if (error) {
+            VLOG_ERR("%s: set_dscp failed %s",
+                     pstream_get_name(remote->listener), ovs_strerror(error));
+        } else {
+            remote->dscp = options->dscp;
+        }
+        /*
+         * XXX race window between setting dscp to listening socket
+         * and accepting socket. Accepted socket may have old dscp value.
+         * Ignore this race window for now.
+         */
+    }
     LIST_FOR_EACH (s, node, &remote->sessions) {
         ovsdb_jsonrpc_session_set_options(s, options);
     }
@@ -477,6 +595,10 @@ ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote,
     struct reconnect_stats rstats;
     struct ds locks_held, locks_waiting, locks_lost;
 
+    status->bound_port = (remote->listener
+                          ? pstream_get_bound_port(remote->listener)
+                          : htons(0));
+
     if (list_is_empty(&remote->sessions)) {
         return false;
     }
@@ -516,21 +638,22 @@ ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote,
     return true;
 }
 
-static const char *
-get_db_name(const struct ovsdb_jsonrpc_session *s)
-{
-    return s->remote->server->up.db->schema->name;
-}
-
-static struct jsonrpc_msg *
-ovsdb_jsonrpc_check_db_name(const struct ovsdb_jsonrpc_session *s,
-                            const struct jsonrpc_msg *request)
+/* Examines 'request' to determine the database to which it relates, and then
+ * searches 's' to find that database:
+ *
+ *    - If successful, returns the database and sets '*replyp' to NULL.
+ *
+ *    - If no such database exists, returns NULL and sets '*replyp' to an
+ *      appropriate JSON-RPC error reply, owned by the caller. */
+static struct ovsdb *
+ovsdb_jsonrpc_lookup_db(const struct ovsdb_jsonrpc_session *s,
+                        const struct jsonrpc_msg *request,
+                        struct jsonrpc_msg **replyp)
 {
     struct json_array *params;
-    const char *want_db_name;
-    const char *have_db_name;
     struct ovsdb_error *error;
-    struct jsonrpc_msg *reply;
+    const char *db_name;
+    struct ovsdb *db;
 
     params = json_array(request->params);
     if (!params->n || params->elems[0]->type != JSON_STRING) {
@@ -540,22 +663,23 @@ ovsdb_jsonrpc_check_db_name(const struct ovsdb_jsonrpc_session *s,
         goto error;
     }
 
-    want_db_name = params->elems[0]->u.string;
-    have_db_name = get_db_name(s);
-    if (strcmp(want_db_name, have_db_name)) {
+    db_name = params->elems[0]->u.string;
+    db = shash_find_data(&s->up.server->dbs, db_name);
+    if (!db) {
         error = ovsdb_syntax_error(
             request->params, "unknown database",
             "%s request specifies unknown database %s",
-            request->method, want_db_name);
+            request->method, db_name);
         goto error;
     }
 
-    return NULL;
+    *replyp = NULL;
+    return db;
 
 error:
-    reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
+    *replyp = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
     ovsdb_error_destroy(error);
-    return reply;
+    return NULL;
 }
 
 static struct ovsdb_error *
@@ -587,7 +711,7 @@ ovsdb_jsonrpc_session_notify(struct ovsdb_session *session,
 
     s = CONTAINER_OF(session, struct ovsdb_jsonrpc_session, up);
     params = json_array_create_1(json_string_create(lock_name));
-    jsonrpc_session_send(s->js, jsonrpc_create_notify(method, params));
+    ovsdb_jsonrpc_session_send(s, jsonrpc_create_notify(method, params));
 }
 
 static struct jsonrpc_msg *
@@ -697,10 +821,10 @@ error:
 }
 
 static struct jsonrpc_msg *
-execute_transaction(struct ovsdb_jsonrpc_session *s,
+execute_transaction(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
                     struct jsonrpc_msg *request)
 {
-    ovsdb_jsonrpc_trigger_create(s, request->id, request->params);
+    ovsdb_jsonrpc_trigger_create(s, db, request->id, request->params);
     request->id = NULL;
     request->params = NULL;
     jsonrpc_msg_destroy(request);
@@ -714,30 +838,39 @@ ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
     struct jsonrpc_msg *reply;
 
     if (!strcmp(request->method, "transact")) {
-        reply = ovsdb_jsonrpc_check_db_name(s, request);
+        struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
         if (!reply) {
-            reply = execute_transaction(s, request);
+            reply = execute_transaction(s, db, request);
         }
     } else if (!strcmp(request->method, "monitor")) {
-        reply = ovsdb_jsonrpc_check_db_name(s, request);
+        struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
         if (!reply) {
             reply = jsonrpc_create_reply(
-                ovsdb_jsonrpc_monitor_create(s, request->params), request->id);
+                ovsdb_jsonrpc_monitor_create(s, db, request->params),
+                request->id);
         }
     } else if (!strcmp(request->method, "monitor_cancel")) {
         reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
                                              request->id);
     } else if (!strcmp(request->method, "get_schema")) {
-        reply = ovsdb_jsonrpc_check_db_name(s, request);
+        struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
         if (!reply) {
-            reply = jsonrpc_create_reply(
-                ovsdb_schema_to_json(s->remote->server->up.db->schema),
-                request->id);
+            reply = jsonrpc_create_reply(ovsdb_schema_to_json(db->schema),
+                                         request->id);
         }
     } else if (!strcmp(request->method, "list_dbs")) {
-        reply = jsonrpc_create_reply(
-            json_array_create_1(json_string_create(get_db_name(s))),
-            request->id);
+        size_t n_dbs = shash_count(&s->up.server->dbs);
+        struct shash_node *node;
+        struct json **dbs;
+        size_t i;
+
+        dbs = xmalloc(n_dbs * sizeof *dbs);
+        i = 0;
+        SHASH_FOR_EACH (node, &s->up.server->dbs) {
+            dbs[i++] = json_string_create(node->name);
+        }
+        reply = jsonrpc_create_reply(json_array_create(dbs, n_dbs),
+                                     request->id);
     } else if (!strcmp(request->method, "lock")) {
         reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_WAIT);
     } else if (!strcmp(request->method, "steal")) {
@@ -753,7 +886,7 @@ ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
 
     if (reply) {
         jsonrpc_msg_destroy(request);
-        jsonrpc_session_send(s->js, reply);
+        ovsdb_jsonrpc_session_send(s, reply);
     }
 }
 
@@ -781,6 +914,14 @@ ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
     }
     jsonrpc_msg_destroy(request);
 }
+
+static void
+ovsdb_jsonrpc_session_send(struct ovsdb_jsonrpc_session *s,
+                           struct jsonrpc_msg *msg)
+{
+    ovsdb_jsonrpc_monitor_flush_all(s);
+    jsonrpc_session_send(s->js, msg);
+}
 \f
 /* JSON-RPC database server triggers.
  *
@@ -794,7 +935,7 @@ struct ovsdb_jsonrpc_trigger {
 };
 
 static void
-ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s,
+ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
                              struct json *id, struct json *params)
 {
     struct ovsdb_jsonrpc_trigger *t;
@@ -808,7 +949,7 @@ ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s,
 
         msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
                                    id);
-        jsonrpc_session_send(s->js, msg);
+        ovsdb_jsonrpc_session_send(s, msg);
         json_destroy(id);
         json_destroy(params);
         return;
@@ -816,7 +957,7 @@ ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s,
 
     /* Insert into trigger table. */
     t = xmalloc(sizeof *t);
-    ovsdb_trigger_init(&s->up, &t->trigger, params, time_msec());
+    ovsdb_trigger_init(&s->up, db, &t->trigger, params, time_msec());
     t->id = id;
     hmap_insert(&s->triggers, &t->hmap_node, hash);
 
@@ -859,7 +1000,7 @@ ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
             reply = jsonrpc_create_error(json_string_create("canceled"),
                                          t->id);
         }
-        jsonrpc_session_send(s->js, reply);
+        ovsdb_jsonrpc_session_send(s, reply);
     }
 
     json_destroy(t->id);
@@ -903,6 +1044,14 @@ struct ovsdb_jsonrpc_monitor_column {
     enum ovsdb_jsonrpc_monitor_selection select;
 };
 
+/* A row that has changed in a monitored table. */
+struct ovsdb_jsonrpc_monitor_row {
+    struct hmap_node hmap_node; /* In ovsdb_jsonrpc_monitor_table.changes. */
+    struct uuid uuid;           /* UUID of row that changed. */
+    struct ovsdb_datum *old;    /* Old data, NULL for an inserted row. */
+    struct ovsdb_datum *new;    /* New data, NULL for a deleted row. */
+};
+
 /* A particular table being monitored. */
 struct ovsdb_jsonrpc_monitor_table {
     const struct ovsdb_table *table;
@@ -914,12 +1063,17 @@ struct ovsdb_jsonrpc_monitor_table {
     /* Columns being monitored. */
     struct ovsdb_jsonrpc_monitor_column *columns;
     size_t n_columns;
+
+    /* Contains 'struct ovsdb_jsonrpc_monitor_row's for rows that have been
+     * updated but not yet flushed to the jsonrpc connection. */
+    struct hmap changes;
 };
 
 /* A collection of tables being monitored. */
 struct ovsdb_jsonrpc_monitor {
     struct ovsdb_replica replica;
     struct ovsdb_jsonrpc_session *session;
+    struct ovsdb *db;
     struct hmap_node node;      /* In ovsdb_jsonrpc_session's "monitors". */
 
     struct json *monitor_id;
@@ -1071,7 +1225,7 @@ ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
 }
 
 static struct json *
-ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s,
+ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
                              struct json *params)
 {
     struct ovsdb_jsonrpc_monitor *m = NULL;
@@ -1099,8 +1253,9 @@ ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s,
 
     m = xzalloc(sizeof *m);
     ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
-    ovsdb_add_replica(s->remote->server->up.db, &m->replica);
+    ovsdb_add_replica(db, &m->replica);
     m->session = s;
+    m->db = db;
     hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
     m->monitor_id = json_clone(monitor_id);
     shash_init(&m->tables);
@@ -1112,7 +1267,7 @@ ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s,
         const struct json *mr_value;
         size_t i;
 
-        table = ovsdb_get_table(s->remote->server->up.db, node->name);
+        table = ovsdb_get_table(m->db, node->name);
         if (!table) {
             error = ovsdb_syntax_error(NULL, NULL,
                                        "no table named %s", node->name);
@@ -1121,6 +1276,7 @@ ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s,
 
         mt = xzalloc(sizeof *mt);
         mt->table = table;
+        hmap_init(&mt->changes);
         shash_add(&m->tables, table->schema->name, mt);
 
         /* Parse columns. */
@@ -1161,7 +1317,7 @@ ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s,
 
 error:
     if (m) {
-        ovsdb_remove_replica(s->remote->server->up.db, &m->replica);
+        ovsdb_remove_replica(m->db, &m->replica);
     }
 
     json = ovsdb_error_to_json(error);
@@ -1185,7 +1341,7 @@ ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
             return jsonrpc_create_error(json_string_create("unknown monitor"),
                                         request_id);
         } else {
-            ovsdb_remove_replica(s->remote->server->up.db, &m->replica);
+            ovsdb_remove_replica(m->db, &m->replica);
             return jsonrpc_create_reply(json_object_create(), request_id);
         }
     }
@@ -1197,155 +1353,359 @@ ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
     struct ovsdb_jsonrpc_monitor *m, *next;
 
     HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
-        ovsdb_remove_replica(s->remote->server->up.db, &m->replica);
+        ovsdb_remove_replica(m->db, &m->replica);
     }
 }
 
 static struct ovsdb_jsonrpc_monitor *
 ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
 {
-    assert(replica->class == &ovsdb_jsonrpc_replica_class);
+    ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
     return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
 }
 
 struct ovsdb_jsonrpc_monitor_aux {
-    bool initial;               /* Sending initial contents of table? */
     const struct ovsdb_jsonrpc_monitor *monitor;
-    struct json *json;          /* JSON for the whole transaction. */
-
-    /* Current table.  */
     struct ovsdb_jsonrpc_monitor_table *mt;
-    struct json *table_json;    /* JSON for table's transaction. */
 };
 
-static bool
-any_reportable_change(const struct ovsdb_jsonrpc_monitor_table *mt,
-                      const unsigned long int *changed)
+/* Finds and returns the ovsdb_jsonrpc_monitor_row in 'mt->changes' for the
+ * given 'uuid', or NULL if there is no such row. */
+static struct ovsdb_jsonrpc_monitor_row *
+ovsdb_jsonrpc_monitor_row_find(const struct ovsdb_jsonrpc_monitor_table *mt,
+                               const struct uuid *uuid)
 {
+    struct ovsdb_jsonrpc_monitor_row *row;
+
+    HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &mt->changes) {
+        if (uuid_equals(uuid, &row->uuid)) {
+            return row;
+        }
+    }
+    return NULL;
+}
+
+/* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
+ * copies of the data in 'row' drawn from the columns represented by
+ * mt->columns[].  Returns the array.
+ *
+ * If 'row' is NULL, returns NULL. */
+static struct ovsdb_datum *
+clone_monitor_row_data(const struct ovsdb_jsonrpc_monitor_table *mt,
+                       const struct ovsdb_row *row)
+{
+    struct ovsdb_datum *data;
     size_t i;
 
+    if (!row) {
+        return NULL;
+    }
+
+    data = xmalloc(mt->n_columns * sizeof *data);
     for (i = 0; i < mt->n_columns; i++) {
-        const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
-        unsigned int idx = c->column->index;
+        const struct ovsdb_column *c = mt->columns[i].column;
+        const struct ovsdb_datum *src = &row->fields[c->index];
+        struct ovsdb_datum *dst = &data[i];
+        const struct ovsdb_type *type = &c->type;
+
+        ovsdb_datum_clone(dst, src, type);
+    }
+    return data;
+}
+
+/* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
+ * in 'row' drawn from the columns represented by mt->columns[]. */
+static void
+update_monitor_row_data(const struct ovsdb_jsonrpc_monitor_table *mt,
+                        const struct ovsdb_row *row,
+                        struct ovsdb_datum *data)
+{
+    size_t i;
 
-        if (c->select & OJMS_MODIFY && bitmap_is_set(changed, idx)) {
-            return true;
+    for (i = 0; i < mt->n_columns; i++) {
+        const struct ovsdb_column *c = mt->columns[i].column;
+        const struct ovsdb_datum *src = &row->fields[c->index];
+        struct ovsdb_datum *dst = &data[i];
+        const struct ovsdb_type *type = &c->type;
+
+        if (!ovsdb_datum_equals(src, dst, type)) {
+            ovsdb_datum_destroy(dst, type);
+            ovsdb_datum_clone(dst, src, type);
         }
     }
+}
 
-    return false;
+/* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
+ * from mt->columns[], plus 'data' itself. */
+static void
+free_monitor_row_data(const struct ovsdb_jsonrpc_monitor_table *mt,
+                      struct ovsdb_datum *data)
+{
+    if (data) {
+        size_t i;
+
+        for (i = 0; i < mt->n_columns; i++) {
+            const struct ovsdb_column *c = mt->columns[i].column;
+
+            ovsdb_datum_destroy(&data[i], &c->type);
+        }
+        free(data);
+    }
+}
+
+/* Frees 'row', which must have been created from 'mt'. */
+static void
+ovsdb_jsonrpc_monitor_row_destroy(const struct ovsdb_jsonrpc_monitor_table *mt,
+                                  struct ovsdb_jsonrpc_monitor_row *row)
+{
+    if (row) {
+        free_monitor_row_data(mt, row->old);
+        free_monitor_row_data(mt, row->new);
+        free(row);
+    }
 }
 
 static bool
 ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
                                 const struct ovsdb_row *new,
-                                const unsigned long int *changed,
+                                const unsigned long int *changed OVS_UNUSED,
                                 void *aux_)
 {
     struct ovsdb_jsonrpc_monitor_aux *aux = aux_;
     const struct ovsdb_jsonrpc_monitor *m = aux->monitor;
     struct ovsdb_table *table = new ? new->table : old->table;
-    enum ovsdb_jsonrpc_monitor_selection type;
-    struct json *old_json, *new_json;
-    struct json *row_json;
-    char uuid[UUID_LEN + 1];
-    size_t i;
+    const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
+    struct ovsdb_jsonrpc_monitor_row *change;
+    struct ovsdb_jsonrpc_monitor_table *mt;
 
     if (!aux->mt || table != aux->mt->table) {
         aux->mt = shash_find_data(&m->tables, table->schema->name);
-        aux->table_json = NULL;
         if (!aux->mt) {
             /* We don't care about rows in this table at all.  Tell the caller
              * to skip it.  */
             return false;
         }
     }
+    mt = aux->mt;
+
+    change = ovsdb_jsonrpc_monitor_row_find(mt, uuid);
+    if (!change) {
+        change = xmalloc(sizeof *change);
+        hmap_insert(&mt->changes, &change->hmap_node, uuid_hash(uuid));
+        change->uuid = *uuid;
+        change->old = clone_monitor_row_data(mt, old);
+        change->new = clone_monitor_row_data(mt, new);
+    } else {
+        if (new) {
+            update_monitor_row_data(mt, new, change->new);
+        } else {
+            free_monitor_row_data(mt, change->new);
+            change->new = NULL;
+
+            if (!change->old) {
+                /* This row was added then deleted.  Forget about it. */
+                hmap_remove(&mt->changes, &change->hmap_node);
+                free(change);
+            }
+        }
+    }
+    return true;
+}
 
-    type = (aux->initial ? OJMS_INITIAL
-            : !old ? OJMS_INSERT
-            : !new ? OJMS_DELETE
+/* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
+ * 'mt', or NULL if no row update should be sent.
+ *
+ * The caller should specify 'initial' as true if the returned JSON is going to
+ * be used as part of the initial reply to a "monitor" request, false if it is
+ * going to be used as part of an "update" notification.
+ *
+ * 'changed' must be a scratch buffer for internal use that is at least
+ * bitmap_n_bytes(mt->n_columns) bytes long. */
+static struct json *
+ovsdb_jsonrpc_monitor_compose_row_update(
+    const struct ovsdb_jsonrpc_monitor_table *mt,
+    const struct ovsdb_jsonrpc_monitor_row *row,
+    bool initial, unsigned long int *changed)
+{
+    enum ovsdb_jsonrpc_monitor_selection type;
+    struct json *old_json, *new_json;
+    struct json *row_json;
+    size_t i;
+
+    type = (initial ? OJMS_INITIAL
+            : !row->old ? OJMS_INSERT
+            : !row->new ? OJMS_DELETE
             : OJMS_MODIFY);
-    if (!(aux->mt->select & type)) {
-        /* We don't care about this type of change (but do want to be called
-         * back for changes to other rows in the same table). */
-        return true;
+    if (!(mt->select & type)) {
+        return NULL;
     }
 
-    if (type == OJMS_MODIFY && !any_reportable_change(aux->mt, changed)) {
-        /* Nothing of interest changed. */
-        return true;
+    if (type == OJMS_MODIFY) {
+        size_t n_changes;
+
+        n_changes = 0;
+        memset(changed, 0, bitmap_n_bytes(mt->n_columns));
+        for (i = 0; i < mt->n_columns; i++) {
+            const struct ovsdb_column *c = mt->columns[i].column;
+            if (!ovsdb_datum_equals(&row->old[i], &row->new[i], &c->type)) {
+                bitmap_set1(changed, i);
+                n_changes++;
+            }
+        }
+        if (!n_changes) {
+            /* No actual changes: presumably a row changed and then
+             * changed back later. */
+            return NULL;
+        }
     }
 
+    row_json = json_object_create();
     old_json = new_json = NULL;
     if (type & (OJMS_DELETE | OJMS_MODIFY)) {
         old_json = json_object_create();
+        json_object_put(row_json, "old", old_json);
     }
     if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
         new_json = json_object_create();
+        json_object_put(row_json, "new", new_json);
     }
-    for (i = 0; i < aux->mt->n_columns; i++) {
-        const struct ovsdb_jsonrpc_monitor_column *c = &aux->mt->columns[i];
-        const struct ovsdb_column *column = c->column;
-        unsigned int idx = c->column->index;
+    for (i = 0; i < mt->n_columns; i++) {
+        const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
 
         if (!(type & c->select)) {
-            /* We don't care about this type of change for this particular
-             * column (but we will care about it for some other column). */
+            /* We don't care about this type of change for this
+             * particular column (but we will care about it for some
+             * other column). */
             continue;
         }
 
-        if ((type == OJMS_MODIFY && bitmap_is_set(changed, idx))
+        if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
             || type == OJMS_DELETE) {
-            json_object_put(old_json, column->name,
-                            ovsdb_datum_to_json(&old->fields[idx],
-                                                &column->type));
+            json_object_put(old_json, c->column->name,
+                            ovsdb_datum_to_json(&row->old[i],
+                                                &c->column->type));
         }
         if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
-            json_object_put(new_json, column->name,
-                            ovsdb_datum_to_json(&new->fields[idx],
-                                                &column->type));
+            json_object_put(new_json, c->column->name,
+                            ovsdb_datum_to_json(&row->new[i],
+                                                &c->column->type));
         }
     }
 
-    /* Create JSON object for transaction overall. */
-    if (!aux->json) {
-        aux->json = json_object_create();
-    }
+    return row_json;
+}
+
+/* Constructs and returns JSON for a <table-updates> object (as described in
+ * RFC 7047) for all the outstanding changes within 'monitor', and deletes all
+ * the outstanding changes from 'monitor'.  Returns NULL if no update needs to
+ * be sent.
+ *
+ * The caller should specify 'initial' as true if the returned JSON is going to
+ * be used as part of the initial reply to a "monitor" request, false if it is
+ * going to be used as part of an "update" notification. */
+static struct json *
+ovsdb_jsonrpc_monitor_compose_table_update(
+    const struct ovsdb_jsonrpc_monitor *monitor, bool initial)
+{
+    struct shash_node *node;
+    unsigned long int *changed;
+    struct json *json;
+    size_t max_columns;
+
+    max_columns = 0;
+    SHASH_FOR_EACH (node, &monitor->tables) {
+        struct ovsdb_jsonrpc_monitor_table *mt = node->data;
 
-    /* Create JSON object for transaction on this table. */
-    if (!aux->table_json) {
-        aux->table_json = json_object_create();
-        json_object_put(aux->json, aux->mt->table->schema->name,
-                        aux->table_json);
+        max_columns = MAX(max_columns, mt->n_columns);
     }
+    changed = xmalloc(bitmap_n_bytes(max_columns));
 
-    /* Create JSON object for transaction on this row. */
-    row_json = json_object_create();
-    if (old_json) {
-        json_object_put(row_json, "old", old_json);
+    json = NULL;
+    SHASH_FOR_EACH (node, &monitor->tables) {
+        struct ovsdb_jsonrpc_monitor_table *mt = node->data;
+        struct ovsdb_jsonrpc_monitor_row *row, *next;
+        struct json *table_json = NULL;
+
+        HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
+            struct json *row_json;
+
+            row_json = ovsdb_jsonrpc_monitor_compose_row_update(
+                mt, row, initial, changed);
+            if (row_json) {
+                char uuid[UUID_LEN + 1];
+
+                /* Create JSON object for transaction overall. */
+                if (!json) {
+                    json = json_object_create();
+                }
+
+                /* Create JSON object for transaction on this table. */
+                if (!table_json) {
+                    table_json = json_object_create();
+                    json_object_put(json, mt->table->schema->name, table_json);
+                }
+
+                /* Add JSON row to JSON table. */
+                snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(&row->uuid));
+                json_object_put(table_json, uuid, row_json);
+            }
+
+            hmap_remove(&mt->changes, &row->hmap_node);
+            ovsdb_jsonrpc_monitor_row_destroy(mt, row);
+        }
     }
-    if (new_json) {
-        json_object_put(row_json, "new", new_json);
+
+    free(changed);
+
+    return json;
+}
+
+static bool
+ovsdb_jsonrpc_monitor_needs_flush(struct ovsdb_jsonrpc_session *s)
+{
+    struct ovsdb_jsonrpc_monitor *m;
+
+    HMAP_FOR_EACH (m, node, &s->monitors) {
+        struct shash_node *node;
+
+        SHASH_FOR_EACH (node, &m->tables) {
+            struct ovsdb_jsonrpc_monitor_table *mt = node->data;
+
+            if (!hmap_is_empty(&mt->changes)) {
+                return true;
+            }
+        }
     }
 
-    /* Add JSON row to JSON table. */
-    snprintf(uuid, sizeof uuid,
-             UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
-    json_object_put(aux->table_json, uuid, row_json);
+    return false;
+}
 
-    return true;
+static void
+ovsdb_jsonrpc_monitor_flush_all(struct ovsdb_jsonrpc_session *s)
+{
+    struct ovsdb_jsonrpc_monitor *m;
+
+    HMAP_FOR_EACH (m, node, &s->monitors) {
+        struct json *json;
+
+        json = ovsdb_jsonrpc_monitor_compose_table_update(m, false);
+        if (json) {
+            struct jsonrpc_msg *msg;
+            struct json *params;
+
+            params = json_array_create_2(json_clone(m->monitor_id), json);
+            msg = jsonrpc_create_notify("update", params);
+            jsonrpc_session_send(s->js, msg);
+        }
+    }
 }
 
 static void
 ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
-                               const struct ovsdb_jsonrpc_monitor *m,
-                               bool initial)
+                               const struct ovsdb_jsonrpc_monitor *m)
 {
-    aux->initial = initial;
     aux->monitor = m;
-    aux->json = NULL;
     aux->mt = NULL;
-    aux->table_json = NULL;
 }
 
 static struct ovsdb_error *
@@ -1356,17 +1716,8 @@ ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
     struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
     struct ovsdb_jsonrpc_monitor_aux aux;
 
-    ovsdb_jsonrpc_monitor_init_aux(&aux, m, false);
+    ovsdb_jsonrpc_monitor_init_aux(&aux, m);
     ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
-    if (aux.json) {
-        struct jsonrpc_msg *msg;
-        struct json *params;
-
-        params = json_array_create_2(json_clone(aux.monitor->monitor_id),
-                                     aux.json);
-        msg = jsonrpc_create_notify("update", params);
-        jsonrpc_session_send(aux.monitor->session->js, msg);
-    }
 
     return NULL;
 }
@@ -1376,8 +1727,9 @@ ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
 {
     struct ovsdb_jsonrpc_monitor_aux aux;
     struct shash_node *node;
+    struct json *json;
 
-    ovsdb_jsonrpc_monitor_init_aux(&aux, m, true);
+    ovsdb_jsonrpc_monitor_init_aux(&aux, m);
     SHASH_FOR_EACH (node, &m->tables) {
         struct ovsdb_jsonrpc_monitor_table *mt = node->data;
 
@@ -1389,7 +1741,8 @@ ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
             }
         }
     }
-    return aux.json ? aux.json : json_object_create();
+    json = ovsdb_jsonrpc_monitor_compose_table_update(m, true);
+    return json ? json : json_object_create();
 }
 
 static void
@@ -1401,6 +1754,14 @@ ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
     json_destroy(m->monitor_id);
     SHASH_FOR_EACH (node, &m->tables) {
         struct ovsdb_jsonrpc_monitor_table *mt = node->data;
+        struct ovsdb_jsonrpc_monitor_row *row, *next;
+
+        HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
+            hmap_remove(&mt->changes, &row->hmap_node);
+            ovsdb_jsonrpc_monitor_row_destroy(mt, row);
+        }
+        hmap_destroy(&mt->changes);
+
         free(mt->columns);
         free(mt);
     }