X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=ovsdb%2Fjsonrpc-server.c;h=379fc524d5eec3c2519e53880552705ca8e53607;hb=b2fda3effc787f265b5ad5dfa967ac00627bd075;hp=936dd1db37263325b527b55c88769b129ae09049;hpb=23935e8bcb5be3e82ed2fb16333fdbea36eedfcd;p=sliver-openvswitch.git diff --git a/ovsdb/jsonrpc-server.c b/ovsdb/jsonrpc-server.c index 936dd1db3..379fc524d 100644 --- a/ovsdb/jsonrpc-server.c +++ b/ovsdb/jsonrpc-server.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2009, 2010 Nicira Networks +/* Copyright (c) 2009, 2010, 2011 Nicira Networks * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ #include #include +#include "bitmap.h" #include "column.h" #include "json.h" #include "jsonrpc.h" @@ -33,15 +34,15 @@ #include "timeval.h" #include "transaction.h" #include "trigger.h" - -#define THIS_MODULE VLM_ovsdb_jsonrpc_server #include "vlog.h" +VLOG_DEFINE_THIS_MODULE(ovsdb_jsonrpc_server); + struct ovsdb_jsonrpc_remote; struct ovsdb_jsonrpc_session; /* Message rate-limiting. */ -struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); +static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); /* Sessions. */ static struct ovsdb_jsonrpc_session *ovsdb_jsonrpc_session_create( @@ -49,6 +50,12 @@ static struct ovsdb_jsonrpc_session *ovsdb_jsonrpc_session_create( 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_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( + struct ovsdb_jsonrpc_remote *, const struct ovsdb_jsonrpc_options *); +static void ovsdb_jsonrpc_session_get_status( + const struct ovsdb_jsonrpc_remote *, + struct shash *); /* Triggers. */ static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *, @@ -86,8 +93,8 @@ struct ovsdb_jsonrpc_remote { struct list sessions; /* List of "struct ovsdb_jsonrpc_session"s. */ }; -static void ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *, - const char *name); +static struct ovsdb_jsonrpc_remote *ovsdb_jsonrpc_server_add_remote( + struct ovsdb_jsonrpc_server *, const char *name); static void ovsdb_jsonrpc_server_del_remote(struct shash_node *); struct ovsdb_jsonrpc_server * @@ -112,8 +119,17 @@ ovsdb_jsonrpc_server_destroy(struct ovsdb_jsonrpc_server *svr) free(svr); } -/* Sets 'svr''s current set of remotes to the names in 'new_remotes'. The data - * values in 'new_remotes' are ignored. +struct ovsdb_jsonrpc_options * +ovsdb_jsonrpc_default_options(void) +{ + struct ovsdb_jsonrpc_options *options = xzalloc(sizeof *options); + options->probe_interval = RECONNECT_DEFAULT_PROBE_INTERVAL; + options->max_backoff = RECONNECT_DEFAULT_MAX_BACKOFF; + return options; +} + +/* Sets 'svr''s current set of remotes to the names in 'new_remotes', with + * options in the struct ovsdb_jsonrpc_options supplied as the data values. * * A remote is an active or passive stream connection method, e.g. "pssl:" or * "tcp:1.2.3.4". */ @@ -129,13 +145,22 @@ ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr, } } SHASH_FOR_EACH (node, new_remotes) { - if (!shash_find(&svr->remotes, node->name)) { - ovsdb_jsonrpc_server_add_remote(svr, node->name); + const struct ovsdb_jsonrpc_options *options = node->data; + struct ovsdb_jsonrpc_remote *remote; + + remote = shash_find_data(&svr->remotes, node->name); + if (!remote) { + remote = ovsdb_jsonrpc_server_add_remote(svr, node->name); + if (!remote) { + continue; + } } + + ovsdb_jsonrpc_session_set_all_options(remote, options); } } -static void +static struct ovsdb_jsonrpc_remote * ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr, const char *name) { @@ -143,10 +168,10 @@ ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr, struct pstream *listener; int error; - error = pstream_open(name, &listener); + error = jsonrpc_pstream_open(name, &listener); if (error && error != EAFNOSUPPORT) { VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, strerror(error)); - return; + return NULL; } remote = xmalloc(sizeof *remote); @@ -158,6 +183,7 @@ ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr, if (!listener) { ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name)); } + return remote; } static void @@ -171,6 +197,35 @@ ovsdb_jsonrpc_server_del_remote(struct shash_node *node) free(remote); } +void +ovsdb_jsonrpc_server_get_remote_status(const struct ovsdb_jsonrpc_server *svr, + struct shash *statuses) +{ + struct shash_node *node; + + shash_init(statuses); + + SHASH_FOR_EACH (node, &svr->remotes) { + const struct ovsdb_jsonrpc_remote *remote = node->data; + + ovsdb_jsonrpc_session_get_status(remote, statuses); + } +} + +/* Forces all of the JSON-RPC sessions managed by 'svr' to disconnect and + * reconnect. */ +void +ovsdb_jsonrpc_server_reconnect(struct ovsdb_jsonrpc_server *svr) +{ + struct shash_node *node; + + SHASH_FOR_EACH (node, &svr->remotes) { + struct ovsdb_jsonrpc_remote *remote = node->data; + + ovsdb_jsonrpc_session_reconnect_all(remote); + } +} + void ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr) { @@ -236,6 +291,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_set_options( + struct ovsdb_jsonrpc_session *, const struct ovsdb_jsonrpc_options *); static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *, struct jsonrpc_msg *); static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *, @@ -302,13 +359,20 @@ ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s) return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT; } +static void +ovsdb_jsonrpc_session_set_options(struct ovsdb_jsonrpc_session *session, + const struct ovsdb_jsonrpc_options *options) +{ + jsonrpc_session_set_max_backoff(session->js, options->max_backoff); + jsonrpc_session_set_probe_interval(session->js, options->probe_interval); +} + static void ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *remote) { struct ovsdb_jsonrpc_session *s, *next; - LIST_FOR_EACH_SAFE (s, next, struct ovsdb_jsonrpc_session, node, - &remote->sessions) { + LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) { int error = ovsdb_jsonrpc_session_run(s); if (error) { ovsdb_jsonrpc_session_close(s); @@ -330,7 +394,7 @@ ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote) { struct ovsdb_jsonrpc_session *s; - LIST_FOR_EACH (s, struct ovsdb_jsonrpc_session, node, &remote->sessions) { + LIST_FOR_EACH (s, node, &remote->sessions) { ovsdb_jsonrpc_session_wait(s); } } @@ -340,12 +404,122 @@ ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote) { struct ovsdb_jsonrpc_session *s, *next; - LIST_FOR_EACH_SAFE (s, next, struct ovsdb_jsonrpc_session, node, - &remote->sessions) { + LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) { ovsdb_jsonrpc_session_close(s); } } +/* Forces all of the JSON-RPC sessions managed by 'remote' to disconnect and + * reconnect. */ +static void +ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *remote) +{ + struct ovsdb_jsonrpc_session *s, *next; + + LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) { + jsonrpc_session_force_reconnect(s->js); + if (!jsonrpc_session_is_alive(s->js)) { + ovsdb_jsonrpc_session_close(s); + } + } +} + +/* Sets the options for all of the JSON-RPC sessions managed by 'remote' to + * 'options'. */ +static void +ovsdb_jsonrpc_session_set_all_options( + struct ovsdb_jsonrpc_remote *remote, + const struct ovsdb_jsonrpc_options *options) +{ + struct ovsdb_jsonrpc_session *s; + + LIST_FOR_EACH (s, node, &remote->sessions) { + ovsdb_jsonrpc_session_set_options(s, options); + } +} + +static void +ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote, + struct shash *shash) +{ + const struct ovsdb_jsonrpc_session *s; + const struct jsonrpc_session *js; + const char *name; + struct ovsdb_jsonrpc_remote_status *status; + struct reconnect_stats rstats; + + /* We only look at the first session in the list. There should be only one + * node in the list for outbound connections. We don't track status for + * each individual inbound connection if someone configures the DB that + * way. Since outbound connections are the norm, this is fine. */ + if (list_is_empty(&remote->sessions)) { + return; + } + s = CONTAINER_OF(remote->sessions.next, struct ovsdb_jsonrpc_session, node); + js = s->js; + if (!js) { + return; + } + name = jsonrpc_session_get_name(js); + + status = xzalloc(sizeof *status); + shash_add(shash, name, status); + + status->is_connected = jsonrpc_session_is_connected(js); + status->last_error = jsonrpc_session_get_status(js); + + jsonrpc_session_get_reconnect_stats(js, &rstats); + status->state = rstats.state; + status->sec_since_connect = rstats.msec_since_connect == UINT_MAX + ? UINT_MAX : rstats.msec_since_connect / 1000; + status->sec_since_disconnect = rstats.msec_since_disconnect == UINT_MAX + ? UINT_MAX : rstats.msec_since_disconnect / 1000; + + return; +} + +static const char * +get_db_name(const struct ovsdb_jsonrpc_session *s) +{ + return s->remote->server->db->schema->name; +} + +static struct jsonrpc_msg * +ovsdb_jsonrpc_check_db_name(const struct ovsdb_jsonrpc_session *s, + const struct jsonrpc_msg *request) +{ + struct json_array *params; + const char *want_db_name; + const char *have_db_name; + struct ovsdb_error *error; + struct jsonrpc_msg *reply; + + params = json_array(request->params); + if (!params->n || params->elems[0]->type != JSON_STRING) { + error = ovsdb_syntax_error( + request->params, NULL, + "%s request params must begin with ", request->method); + 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)) { + error = ovsdb_syntax_error( + request->params, "unknown database", + "%s request specifies unknown database %s", + request->method, want_db_name); + goto error; + } + + return NULL; + +error: + reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id); + ovsdb_error_destroy(error); + return reply; +} + static struct jsonrpc_msg * execute_transaction(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request) @@ -364,16 +538,30 @@ ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *reply; if (!strcmp(request->method, "transact")) { - reply = execute_transaction(s, request); + reply = ovsdb_jsonrpc_check_db_name(s, request); + if (!reply) { + reply = execute_transaction(s, request); + } } else if (!strcmp(request->method, "monitor")) { - reply = jsonrpc_create_reply( - ovsdb_jsonrpc_monitor_create(s, request->params), request->id); + reply = ovsdb_jsonrpc_check_db_name(s, request); + if (!reply) { + reply = jsonrpc_create_reply( + ovsdb_jsonrpc_monitor_create(s, 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); + if (!reply) { + reply = jsonrpc_create_reply( + ovsdb_schema_to_json(s->remote->server->db->schema), + request->id); + } + } else if (!strcmp(request->method, "list_dbs")) { reply = jsonrpc_create_reply( - ovsdb_schema_to_json(s->remote->server->db->schema), request->id); + json_array_create_1(json_string_create(get_db_name(s))), + request->id); } else if (!strcmp(request->method, "echo")) { reply = jsonrpc_create_reply(json_clone(request->params), request->id); } else { @@ -466,8 +654,7 @@ ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s, { struct ovsdb_jsonrpc_trigger *t; - HMAP_FOR_EACH_WITH_HASH (t, struct ovsdb_jsonrpc_trigger, hmap_node, hash, - &s->triggers) { + HMAP_FOR_EACH_WITH_HASH (t, hmap_node, hash, &s->triggers) { if (json_equal(t->id, id)) { return t; } @@ -505,8 +692,7 @@ static void ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s) { struct ovsdb_jsonrpc_trigger *t, *next; - HMAP_FOR_EACH_SAFE (t, next, struct ovsdb_jsonrpc_trigger, hmap_node, - &s->triggers) { + HMAP_FOR_EACH_SAFE (t, next, hmap_node, &s->triggers) { ovsdb_jsonrpc_trigger_complete(t); } } @@ -531,12 +717,26 @@ enum ovsdb_jsonrpc_monitor_selection { OJMS_MODIFY = 1 << 3 /* Modified rows. */ }; +/* A particular column being monitored. */ +struct ovsdb_jsonrpc_monitor_column { + const struct ovsdb_column *column; + enum ovsdb_jsonrpc_monitor_selection select; +}; + +/* A particular table being monitored. */ struct ovsdb_jsonrpc_monitor_table { const struct ovsdb_table *table; + + /* This is the union (bitwise-OR) of the 'select' values in all of the + * members of 'columns' below. */ enum ovsdb_jsonrpc_monitor_selection select; - struct ovsdb_column_set columns; + + /* Columns being monitored. */ + struct ovsdb_jsonrpc_monitor_column *columns; + size_t n_columns; }; +/* A collection of tables being monitored. */ struct ovsdb_jsonrpc_monitor { struct ovsdb_replica replica; struct ovsdb_jsonrpc_session *session; @@ -569,8 +769,7 @@ ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s, { struct ovsdb_jsonrpc_monitor *m; - HMAP_FOR_EACH_WITH_HASH (m, struct ovsdb_jsonrpc_monitor, node, - json_hash(monitor_id, 0), &s->monitors) { + HMAP_FOR_EACH_WITH_HASH (m, node, json_hash(monitor_id, 0), &s->monitors) { if (json_equal(m->monitor_id, monitor_id)) { return m; } @@ -579,6 +778,118 @@ ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s, return NULL; } +static void +ovsdb_jsonrpc_add_monitor_column(struct ovsdb_jsonrpc_monitor_table *mt, + const struct ovsdb_column *column, + enum ovsdb_jsonrpc_monitor_selection select, + size_t *allocated_columns) +{ + struct ovsdb_jsonrpc_monitor_column *c; + + if (mt->n_columns >= *allocated_columns) { + mt->columns = x2nrealloc(mt->columns, allocated_columns, + sizeof *mt->columns); + } + + c = &mt->columns[mt->n_columns++]; + c->column = column; + c->select = select; +} + +static int +compare_ovsdb_jsonrpc_monitor_column(const void *a_, const void *b_) +{ + const struct ovsdb_jsonrpc_monitor_column *a = a_; + const struct ovsdb_jsonrpc_monitor_column *b = b_; + + return a->column < b->column ? -1 : a->column > b->column; +} + +static struct ovsdb_error * WARN_UNUSED_RESULT +ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt, + const struct json *monitor_request, + size_t *allocated_columns) +{ + const struct ovsdb_table_schema *ts = mt->table->schema; + enum ovsdb_jsonrpc_monitor_selection select; + const struct json *columns, *select_json; + struct ovsdb_parser parser; + struct ovsdb_error *error; + + ovsdb_parser_init(&parser, monitor_request, "table %s", ts->name); + columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL); + select_json = ovsdb_parser_member(&parser, "select", + OP_OBJECT | OP_OPTIONAL); + error = ovsdb_parser_finish(&parser); + if (error) { + return error; + } + + if (select_json) { + select = 0; + ovsdb_parser_init(&parser, select_json, "table %s select", ts->name); + if (parse_bool(&parser, "initial", true)) { + select |= OJMS_INITIAL; + } + if (parse_bool(&parser, "insert", true)) { + select |= OJMS_INSERT; + } + if (parse_bool(&parser, "delete", true)) { + select |= OJMS_DELETE; + } + if (parse_bool(&parser, "modify", true)) { + select |= OJMS_MODIFY; + } + error = ovsdb_parser_finish(&parser); + if (error) { + return error; + } + } else { + select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY; + } + mt->select |= select; + + if (columns) { + size_t i; + + if (columns->type != JSON_ARRAY) { + return ovsdb_syntax_error(columns, NULL, + "array of column names expected"); + } + + for (i = 0; i < columns->u.array.n; i++) { + const struct ovsdb_column *column; + const char *s; + + if (columns->u.array.elems[i]->type != JSON_STRING) { + return ovsdb_syntax_error(columns, NULL, + "array of column names expected"); + } + + s = columns->u.array.elems[i]->u.string; + column = shash_find_data(&mt->table->schema->columns, s); + if (!column) { + return ovsdb_syntax_error(columns, NULL, "%s is not a valid " + "column name", s); + } + ovsdb_jsonrpc_add_monitor_column(mt, column, select, + allocated_columns); + } + } else { + struct shash_node *node; + + SHASH_FOR_EACH (node, &ts->columns) { + const struct ovsdb_column *column = node->data; + if (column->index != OVSDB_COL_UUID) { + ovsdb_jsonrpc_add_monitor_column(mt, column, select, + allocated_columns); + } + } + } + + return NULL; +} + static struct json * ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct json *params) @@ -589,12 +900,12 @@ ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct shash_node *node; struct json *json; - if (json_array(params)->n != 2) { + if (json_array(params)->n != 3) { error = ovsdb_syntax_error(params, NULL, "invalid parameters"); goto error; } - monitor_id = params->u.array.elems[0]; - monitor_requests = params->u.array.elems[1]; + monitor_id = params->u.array.elems[1]; + monitor_requests = params->u.array.elems[2]; if (monitor_requests->type != JSON_OBJECT) { error = ovsdb_syntax_error(monitor_requests, NULL, "monitor-requests must be object"); @@ -617,8 +928,9 @@ ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, SHASH_FOR_EACH (node, json_object(monitor_requests)) { const struct ovsdb_table *table; struct ovsdb_jsonrpc_monitor_table *mt; - const struct json *columns_json, *select_json; - struct ovsdb_parser parser; + size_t allocated_columns; + const struct json *mr_value; + size_t i; table = ovsdb_get_table(s->remote->server->db, node->name); if (!table) { @@ -629,55 +941,37 @@ ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, mt = xzalloc(sizeof *mt); mt->table = table; - mt->select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY; - ovsdb_column_set_init(&mt->columns); shash_add(&m->tables, table->schema->name, mt); - ovsdb_parser_init(&parser, node->data, "table %s", node->name); - columns_json = ovsdb_parser_member(&parser, "columns", - OP_ARRAY | OP_OPTIONAL); - select_json = ovsdb_parser_member(&parser, "select", - OP_OBJECT | OP_OPTIONAL); - error = ovsdb_parser_finish(&parser); - if (error) { - goto error; - } - - if (columns_json) { - error = ovsdb_column_set_from_json(columns_json, table, - &mt->columns); - if (error) { - goto error; + /* Parse columns. */ + mr_value = node->data; + allocated_columns = 0; + if (mr_value->type == JSON_ARRAY) { + const struct json_array *array = &mr_value->u.array; + + for (i = 0; i < array->n; i++) { + error = ovsdb_jsonrpc_parse_monitor_request( + mt, array->elems[i], &allocated_columns); + if (error) { + goto error; + } } } else { - struct shash_node *node; - - SHASH_FOR_EACH (node, &table->schema->columns) { - const struct ovsdb_column *column = node->data; - if (column->index != OVSDB_COL_UUID) { - ovsdb_column_set_add(&mt->columns, column); - } + error = ovsdb_jsonrpc_parse_monitor_request( + mt, mr_value, &allocated_columns); + if (error) { + goto error; } } - if (select_json) { - mt->select = 0; - ovsdb_parser_init(&parser, select_json, "table %s select", - table->schema->name); - if (parse_bool(&parser, "initial", true)) { - mt->select |= OJMS_INITIAL; - } - if (parse_bool(&parser, "insert", true)) { - mt->select |= OJMS_INSERT; - } - if (parse_bool(&parser, "delete", true)) { - mt->select |= OJMS_DELETE; - } - if (parse_bool(&parser, "modify", true)) { - mt->select |= OJMS_MODIFY; - } - error = ovsdb_parser_finish(&parser); - if (error) { + /* Check for duplicate columns. */ + qsort(mt->columns, mt->n_columns, sizeof *mt->columns, + compare_ovsdb_jsonrpc_monitor_column); + for (i = 1; i < mt->n_columns; i++) { + if (mt->columns[i].column == mt->columns[i - 1].column) { + error = ovsdb_syntax_error(mr_value, NULL, "column %s " + "mentioned more than once", + mt->columns[i].column->name); goto error; } } @@ -722,8 +1016,7 @@ ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s) { struct ovsdb_jsonrpc_monitor *m, *next; - HMAP_FOR_EACH_SAFE (m, next, - struct ovsdb_jsonrpc_monitor, node, &s->monitors) { + HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) { ovsdb_remove_replica(s->remote->server->db, &m->replica); } } @@ -745,9 +1038,28 @@ struct ovsdb_jsonrpc_monitor_aux { 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) +{ + size_t i; + + for (i = 0; i < mt->n_columns; i++) { + const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i]; + unsigned int idx = c->column->index; + + if (c->select & OJMS_MODIFY && bitmap_is_set(changed, idx)) { + return true; + } + } + + return false; +} + static bool ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old, const struct ovsdb_row *new, + const unsigned long int *changed, void *aux_) { struct ovsdb_jsonrpc_monitor_aux *aux = aux_; @@ -757,7 +1069,6 @@ ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old, struct json *old_json, *new_json; struct json *row_json; char uuid[UUID_LEN + 1]; - int n_changed; size_t i; if (!aux->mt || table != aux->mt->table) { @@ -780,41 +1091,41 @@ ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old, return true; } + if (type == OJMS_MODIFY && !any_reportable_change(aux->mt, changed)) { + /* Nothing of interest changed. */ + return true; + } + old_json = new_json = NULL; - n_changed = 0; - for (i = 0; i < aux->mt->columns.n_columns; i++) { - const struct ovsdb_column *column = aux->mt->columns.columns[i]; - unsigned int idx = column->index; - bool changed = false; - - if (type == OJMS_MODIFY) { - changed = !ovsdb_datum_equals(&old->fields[idx], - &new->fields[idx], &column->type); - n_changed += changed; - } - if (changed || type == OJMS_DELETE) { - if (!old_json) { - old_json = json_object_create(); - } + if (type & (OJMS_DELETE | OJMS_MODIFY)) { + old_json = json_object_create(); + } + if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) { + new_json = json_object_create(); + } + 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; + + 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). */ + continue; + } + + if ((type == OJMS_MODIFY && bitmap_is_set(changed, idx)) + || type == OJMS_DELETE) { json_object_put(old_json, column->name, ovsdb_datum_to_json(&old->fields[idx], &column->type)); } if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) { - if (!new_json) { - new_json = json_object_create(); - } json_object_put(new_json, column->name, ovsdb_datum_to_json(&new->fields[idx], &column->type)); } } - if ((type == OJMS_MODIFY && !n_changed) || (!old_json && !new_json)) { - /* No reportable changes. */ - json_destroy(old_json); - json_destroy(new_json); - return true; - } /* Create JSON object for transaction overall. */ if (!aux->json) { @@ -859,7 +1170,8 @@ ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux, static struct ovsdb_error * ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica, - const struct ovsdb_txn *txn, bool durable UNUSED) + const struct ovsdb_txn *txn, + bool durable OVS_UNUSED) { struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica); struct ovsdb_jsonrpc_monitor_aux aux; @@ -892,9 +1204,8 @@ ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m) if (mt->select & OJMS_INITIAL) { struct ovsdb_row *row; - HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node, - &mt->table->rows) { - ovsdb_jsonrpc_monitor_change_cb(NULL, row, &aux); + HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) { + ovsdb_jsonrpc_monitor_change_cb(NULL, row, NULL, &aux); } } } @@ -910,7 +1221,7 @@ 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; - ovsdb_column_set_destroy(&mt->columns); + free(mt->columns); free(mt); } shash_destroy(&m->tables);