LCOV - code coverage report
Current view: top level - ovsdb - monitor.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 565 592 95.4 %
Date: 2016-09-14 01:02:56 Functions: 58 59 98.3 %
Branches: 319 395 80.8 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 2015 Nicira, Inc.
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at:
       7                 :            :  *
       8                 :            :  *     http://www.apache.org/licenses/LICENSE-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : #include <config.h>
      18                 :            : 
      19                 :            : #include <errno.h>
      20                 :            : 
      21                 :            : #include "bitmap.h"
      22                 :            : #include "column.h"
      23                 :            : #include "openvswitch/dynamic-string.h"
      24                 :            : #include "openvswitch/json.h"
      25                 :            : #include "jsonrpc.h"
      26                 :            : #include "ovsdb-error.h"
      27                 :            : #include "ovsdb-parser.h"
      28                 :            : #include "ovsdb.h"
      29                 :            : #include "row.h"
      30                 :            : #include "condition.h"
      31                 :            : #include "simap.h"
      32                 :            : #include "hash.h"
      33                 :            : #include "table.h"
      34                 :            : #include "hash.h"
      35                 :            : #include "timeval.h"
      36                 :            : #include "transaction.h"
      37                 :            : #include "jsonrpc-server.h"
      38                 :            : #include "monitor.h"
      39                 :            : #include "util.h"
      40                 :            : #include "openvswitch/vlog.h"
      41                 :            : 
      42                 :       2594 : VLOG_DEFINE_THIS_MODULE(ovsdb_monitor);
      43                 :            : 
      44                 :            : static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
      45                 :            : static struct hmap ovsdb_monitors = HMAP_INITIALIZER(&ovsdb_monitors);
      46                 :            : 
      47                 :            : /* Keep state of session's conditions */
      48                 :            : struct ovsdb_monitor_session_condition {
      49                 :            :     bool conditional;
      50                 :            :     size_t n_true_cnd;
      51                 :            :     struct shash tables;     /* Contains
      52                 :            :                               *   "struct ovsdb_monitor_table_condition *"s. */
      53                 :            : };
      54                 :            : 
      55                 :            : /* Monitored table session's conditions */
      56                 :            : struct ovsdb_monitor_table_condition {
      57                 :            :     const struct ovsdb_table *table;
      58                 :            :     struct ovsdb_monitor_table *mt;
      59                 :            :     struct ovsdb_condition old_condition;
      60                 :            :     struct ovsdb_condition new_condition;
      61                 :            : };
      62                 :            : 
      63                 :            : /*  Backend monitor.
      64                 :            :  *
      65                 :            :  *  ovsdb_monitor keep track of the ovsdb changes.
      66                 :            :  */
      67                 :            : 
      68                 :            : /* A collection of tables being monitored. */
      69                 :            : struct ovsdb_monitor {
      70                 :            :     struct ovsdb_replica replica;
      71                 :            :     struct shash tables;     /* Holds "struct ovsdb_monitor_table"s. */
      72                 :            :     struct ovs_list jsonrpc_monitors;  /* Contains "jsonrpc_monitor_node"s. */
      73                 :            :     struct ovsdb *db;
      74                 :            :     uint64_t n_transactions;      /* Count number of committed transactions. */
      75                 :            :     struct hmap_node hmap_node;   /* Elements within ovsdb_monitors.  */
      76                 :            :     struct hmap json_cache;       /* Contains "ovsdb_monitor_json_cache_node"s.*/
      77                 :            : };
      78                 :            : 
      79                 :            : /* A json object of updates between 'from_txn' and 'dbmon->n_transactions'
      80                 :            :  * inclusive.  */
      81                 :            : struct ovsdb_monitor_json_cache_node {
      82                 :            :     struct hmap_node hmap_node;   /* Elements in json cache. */
      83                 :            :     enum ovsdb_monitor_version version;
      84                 :            :     uint64_t from_txn;
      85                 :            :     struct json *json;            /* Null, or a cloned of json */
      86                 :            : };
      87                 :            : 
      88                 :            : struct jsonrpc_monitor_node {
      89                 :            :     struct ovs_list node;
      90                 :            :     struct ovsdb_jsonrpc_monitor *jsonrpc_monitor;
      91                 :            : };
      92                 :            : 
      93                 :            : /* A particular column being monitored. */
      94                 :            : struct ovsdb_monitor_column {
      95                 :            :     const struct ovsdb_column *column;
      96                 :            :     enum ovsdb_monitor_selection select;
      97                 :            :     bool monitored;
      98                 :            : };
      99                 :            : 
     100                 :            : /* A row that has changed in a monitored table. */
     101                 :            : struct ovsdb_monitor_row {
     102                 :            :     struct hmap_node hmap_node; /* In ovsdb_jsonrpc_monitor_table.changes. */
     103                 :            :     struct uuid uuid;           /* UUID of row that changed. */
     104                 :            :     struct ovsdb_datum *old;    /* Old data, NULL for an inserted row. */
     105                 :            :     struct ovsdb_datum *new;    /* New data, NULL for a deleted row. */
     106                 :            : };
     107                 :            : 
     108                 :            : /* Contains 'struct ovsdb_monitor_row's for rows that have been
     109                 :            :  * updated but not yet flushed to all the jsonrpc connection.
     110                 :            :  *
     111                 :            :  * 'n_refs' represent the number of jsonrpc connections that have
     112                 :            :  * not received updates. Generate the update for the last jsonprc
     113                 :            :  * connection will also destroy the whole "struct ovsdb_monitor_changes"
     114                 :            :  * object.
     115                 :            :  *
     116                 :            :  * 'transaction' stores the first update's transaction id.
     117                 :            :  * */
     118                 :            : struct ovsdb_monitor_changes {
     119                 :            :     struct hmap_node hmap_node;  /* Element in ovsdb_monitor_tables' changes
     120                 :            :                                     hmap.  */
     121                 :            :     struct ovsdb_monitor_table *mt;
     122                 :            :     struct hmap rows;
     123                 :            :     int n_refs;
     124                 :            :     uint64_t transaction;
     125                 :            : };
     126                 :            : 
     127                 :            : /* A particular table being monitored. */
     128                 :            : struct ovsdb_monitor_table {
     129                 :            :     const struct ovsdb_table *table;
     130                 :            : 
     131                 :            :     /* This is the union (bitwise-OR) of the 'select' values in all of the
     132                 :            :      * members of 'columns' below. */
     133                 :            :     enum ovsdb_monitor_selection select;
     134                 :            : 
     135                 :            :     /* Columns being monitored. */
     136                 :            :     struct ovsdb_monitor_column *columns;
     137                 :            :     size_t n_columns;
     138                 :            :     size_t n_monitored_columns;
     139                 :            :     size_t allocated_columns;
     140                 :            : 
     141                 :            :     /* Columns in ovsdb_monitor_row have different indexes then in
     142                 :            :      * ovsdb_row. This field maps between column->index to the index in the
     143                 :            :      * ovsdb_monitor_row. It is used for condition evaluation. */
     144                 :            :     unsigned int *columns_index_map;
     145                 :            : 
     146                 :            :     /* Contains 'ovsdb_monitor_changes' indexed by 'transaction'. */
     147                 :            :     struct hmap changes;
     148                 :            : };
     149                 :            : 
     150                 :            : enum ovsdb_monitor_row_type {
     151                 :            :     OVSDB_ROW,
     152                 :            :     OVSDB_MONITOR_ROW
     153                 :            : };
     154                 :            : 
     155                 :            : typedef struct json *
     156                 :            : (*compose_row_update_cb_func)
     157                 :            :     (const struct ovsdb_monitor_table *mt,
     158                 :            :      const struct ovsdb_monitor_session_condition * condition,
     159                 :            :      enum ovsdb_monitor_row_type row_type,
     160                 :            :      const void *,
     161                 :            :      bool initial, unsigned long int *changed);
     162                 :            : 
     163                 :            : static void ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon);
     164                 :            : static struct ovsdb_monitor_changes * ovsdb_monitor_table_add_changes(
     165                 :            :     struct ovsdb_monitor_table *mt, uint64_t next_txn);
     166                 :            : static struct ovsdb_monitor_changes *ovsdb_monitor_table_find_changes(
     167                 :            :     struct ovsdb_monitor_table *mt, uint64_t unflushed);
     168                 :            : static void ovsdb_monitor_changes_destroy(
     169                 :            :                                   struct ovsdb_monitor_changes *changes);
     170                 :            : static void ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table *mt,
     171                 :            :                                   uint64_t unflushed);
     172                 :            : 
     173                 :            : static uint32_t
     174                 :      47348 : json_cache_hash(enum ovsdb_monitor_version version, uint64_t from_txn)
     175                 :            : {
     176                 :            :     uint32_t hash;
     177                 :            : 
     178                 :      47348 :     hash = hash_uint64(version);
     179                 :      47348 :     hash = hash_uint64_basis(from_txn, hash);
     180                 :            : 
     181                 :      47348 :     return hash;
     182                 :            : }
     183                 :            : 
     184                 :            : static struct ovsdb_monitor_json_cache_node *
     185                 :      24514 : ovsdb_monitor_json_cache_search(const struct ovsdb_monitor *dbmon,
     186                 :            :                                 enum ovsdb_monitor_version version,
     187                 :            :                                 uint64_t from_txn)
     188                 :            : {
     189                 :            :     struct ovsdb_monitor_json_cache_node *node;
     190                 :      24514 :     uint32_t hash = json_cache_hash(version, from_txn);
     191                 :            : 
     192 [ +  + ][ -  + ]:      24514 :     HMAP_FOR_EACH_WITH_HASH(node, hmap_node, hash, &dbmon->json_cache) {
     193 [ +  - ][ +  - ]:       1473 :         if (node->from_txn == from_txn && node->version == version) {
     194                 :       1473 :             return node;
     195                 :            :         }
     196                 :            :     }
     197                 :            : 
     198                 :      23041 :     return NULL;
     199                 :            : }
     200                 :            : 
     201                 :            : static void
     202                 :      22834 : ovsdb_monitor_json_cache_insert(struct ovsdb_monitor *dbmon,
     203                 :            :                                 enum ovsdb_monitor_version version,
     204                 :            :                                 uint64_t from_txn, struct json *json)
     205                 :            : {
     206                 :            :     struct ovsdb_monitor_json_cache_node *node;
     207                 :      22834 :     uint32_t hash = json_cache_hash(version, from_txn);
     208                 :            : 
     209                 :      22834 :     node = xmalloc(sizeof *node);
     210                 :            : 
     211                 :      22834 :     node->version = version;
     212                 :      22834 :     node->from_txn = from_txn;
     213         [ +  + ]:      22834 :     node->json = json ? json_clone(json) : NULL;
     214                 :            : 
     215                 :      22834 :     hmap_insert(&dbmon->json_cache, &node->hmap_node, hash);
     216                 :      22834 : }
     217                 :            : 
     218                 :            : static void
     219                 :      23206 : ovsdb_monitor_json_cache_flush(struct ovsdb_monitor *dbmon)
     220                 :            : {
     221                 :            :     struct ovsdb_monitor_json_cache_node *node;
     222                 :            : 
     223 [ +  + ][ -  + ]:      46040 :     HMAP_FOR_EACH_POP(node, hmap_node, &dbmon->json_cache) {
                 [ +  + ]
     224                 :      22834 :         json_destroy(node->json);
     225                 :      22834 :         free(node);
     226                 :            :     }
     227                 :      23206 : }
     228                 :            : 
     229                 :            : static int
     230                 :     390255 : compare_ovsdb_monitor_column(const void *a_, const void *b_)
     231                 :            : {
     232                 :     390255 :     const struct ovsdb_monitor_column *a = a_;
     233                 :     390255 :     const struct ovsdb_monitor_column *b = b_;
     234                 :            : 
     235                 :            :     /* put all monitored columns at the begining */
     236         [ -  + ]:     390255 :     if (a->monitored != b->monitored) {
     237         [ #  # ]:          0 :         return a->monitored ? -1 : 1;
     238                 :            :     }
     239                 :            : 
     240         [ +  + ]:     390255 :     return a->column < b->column ? -1 : a->column > b->column;
     241                 :            : }
     242                 :            : 
     243                 :            : static struct ovsdb_monitor *
     244                 :      17051 : ovsdb_monitor_cast(struct ovsdb_replica *replica)
     245                 :            : {
     246         [ -  + ]:      17051 :     ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
     247                 :      17051 :     return CONTAINER_OF(replica, struct ovsdb_monitor, replica);
     248                 :            : }
     249                 :            : 
     250                 :            : /* Finds and returns the ovsdb_monitor_row in 'mt->changes->rows' for the
     251                 :            :  * given 'uuid', or NULL if there is no such row. */
     252                 :            : static struct ovsdb_monitor_row *
     253                 :     150317 : ovsdb_monitor_changes_row_find(const struct ovsdb_monitor_changes *changes,
     254                 :            :                                const struct uuid *uuid)
     255                 :            : {
     256                 :            :     struct ovsdb_monitor_row *row;
     257                 :            : 
     258 [ +  + ][ -  + ]:     150317 :     HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid),
     259                 :            :                              &changes->rows) {
     260         [ +  - ]:       2484 :         if (uuid_equals(uuid, &row->uuid)) {
     261                 :       2484 :             return row;
     262                 :            :         }
     263                 :            :     }
     264                 :     147833 :     return NULL;
     265                 :            : }
     266                 :            : 
     267                 :            : /* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
     268                 :            :  * copies of the data in 'row' drawn from the columns represented by
     269                 :            :  * mt->columns[].  Returns the array.
     270                 :            :  *
     271                 :            :  * If 'row' is NULL, returns NULL. */
     272                 :            : static struct ovsdb_datum *
     273                 :     295666 : clone_monitor_row_data(const struct ovsdb_monitor_table *mt,
     274                 :            :                        const struct ovsdb_row *row)
     275                 :            : {
     276                 :            :     struct ovsdb_datum *data;
     277                 :            :     size_t i;
     278                 :            : 
     279         [ +  + ]:     295666 :     if (!row) {
     280                 :     122359 :         return NULL;
     281                 :            :     }
     282                 :            : 
     283                 :     173307 :     data = xmalloc(mt->n_columns * sizeof *data);
     284         [ +  + ]:    1835941 :     for (i = 0; i < mt->n_columns; i++) {
     285                 :    1662634 :         const struct ovsdb_column *c = mt->columns[i].column;
     286                 :    1662634 :         const struct ovsdb_datum *src = &row->fields[c->index];
     287                 :    1662634 :         struct ovsdb_datum *dst = &data[i];
     288                 :    1662634 :         const struct ovsdb_type *type = &c->type;
     289                 :            : 
     290                 :    1662634 :         ovsdb_datum_clone(dst, src, type);
     291                 :            :     }
     292                 :     173307 :     return data;
     293                 :            : }
     294                 :            : 
     295                 :            : /* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
     296                 :            :  * in 'row' drawn from the columns represented by mt->columns[]. */
     297                 :            : static void
     298                 :         48 : update_monitor_row_data(const struct ovsdb_monitor_table *mt,
     299                 :            :                         const struct ovsdb_row *row,
     300                 :            :                         struct ovsdb_datum *data)
     301                 :            : {
     302                 :            :     size_t i;
     303                 :            : 
     304         [ +  + ]:        664 :     for (i = 0; i < mt->n_columns; i++) {
     305                 :        616 :         const struct ovsdb_column *c = mt->columns[i].column;
     306                 :        616 :         const struct ovsdb_datum *src = &row->fields[c->index];
     307                 :        616 :         struct ovsdb_datum *dst = &data[i];
     308                 :        616 :         const struct ovsdb_type *type = &c->type;
     309                 :            : 
     310         [ +  + ]:        616 :         if (!ovsdb_datum_equals(src, dst, type)) {
     311                 :         83 :             ovsdb_datum_destroy(dst, type);
     312                 :         83 :             ovsdb_datum_clone(dst, src, type);
     313                 :            :         }
     314                 :            :     }
     315                 :         48 : }
     316                 :            : 
     317                 :            : /* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
     318                 :            :  * from mt->columns[], plus 'data' itself. */
     319                 :            : static void
     320                 :     293230 : free_monitor_row_data(const struct ovsdb_monitor_table *mt,
     321                 :            :                       struct ovsdb_datum *data)
     322                 :            : {
     323         [ +  + ]:     293230 :     if (data) {
     324                 :            :         size_t i;
     325                 :            : 
     326         [ +  + ]:    1835941 :         for (i = 0; i < mt->n_columns; i++) {
     327                 :    1662634 :             const struct ovsdb_column *c = mt->columns[i].column;
     328                 :            : 
     329                 :    1662634 :             ovsdb_datum_destroy(&data[i], &c->type);
     330                 :            :         }
     331                 :     173307 :         free(data);
     332                 :            :     }
     333                 :     293230 : }
     334                 :            : 
     335                 :            : /* Frees 'row', which must have been created from 'mt'. */
     336                 :            : static void
     337                 :     145397 : ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table *mt,
     338                 :            :                           struct ovsdb_monitor_row *row)
     339                 :            : {
     340         [ +  - ]:     145397 :     if (row) {
     341                 :     145397 :         free_monitor_row_data(mt, row->old);
     342                 :     145397 :         free_monitor_row_data(mt, row->new);
     343                 :     145397 :         free(row);
     344                 :            :     }
     345                 :     145397 : }
     346                 :            : 
     347                 :            : static void
     348                 :       7140 : ovsdb_monitor_columns_sort(struct ovsdb_monitor *dbmon)
     349                 :            : {
     350                 :            :     int i;
     351                 :            :     struct shash_node *node;
     352                 :            : 
     353 [ +  + ][ -  + ]:      51351 :     SHASH_FOR_EACH (node, &dbmon->tables) {
     354                 :      44211 :         struct ovsdb_monitor_table *mt = node->data;
     355                 :            : 
     356                 :      44211 :         qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
     357                 :            :               compare_ovsdb_monitor_column);
     358         [ +  + ]:     245773 :         for (i = 0; i < mt->n_columns; i++) {
     359                 :            :             /* re-set index map due to sort */
     360                 :     201562 :             mt->columns_index_map[mt->columns[i].column->index] = i;
     361                 :            :         }
     362                 :            :     }
     363                 :       7140 : }
     364                 :            : 
     365                 :            : void
     366                 :       7742 : ovsdb_monitor_add_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
     367                 :            :                                   struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
     368                 :            : {
     369                 :            :     struct jsonrpc_monitor_node *jm;
     370                 :            : 
     371                 :       7742 :     jm = xzalloc(sizeof *jm);
     372                 :       7742 :     jm->jsonrpc_monitor = jsonrpc_monitor;
     373                 :       7742 :     ovs_list_push_back(&dbmon->jsonrpc_monitors, &jm->node);
     374                 :       7742 : }
     375                 :            : 
     376                 :            : struct ovsdb_monitor *
     377                 :       7140 : ovsdb_monitor_create(struct ovsdb *db,
     378                 :            :                      struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
     379                 :            : {
     380                 :            :     struct ovsdb_monitor *dbmon;
     381                 :            : 
     382                 :       7140 :     dbmon = xzalloc(sizeof *dbmon);
     383                 :            : 
     384                 :       7140 :     ovsdb_replica_init(&dbmon->replica, &ovsdb_jsonrpc_replica_class);
     385                 :       7140 :     ovsdb_add_replica(db, &dbmon->replica);
     386                 :       7140 :     ovs_list_init(&dbmon->jsonrpc_monitors);
     387                 :       7140 :     dbmon->db = db;
     388                 :       7140 :     dbmon->n_transactions = 0;
     389                 :       7140 :     shash_init(&dbmon->tables);
     390                 :       7140 :     hmap_node_nullify(&dbmon->hmap_node);
     391                 :       7140 :     hmap_init(&dbmon->json_cache);
     392                 :            : 
     393                 :       7140 :     ovsdb_monitor_add_jsonrpc_monitor(dbmon, jsonrpc_monitor);
     394                 :       7140 :     return dbmon;
     395                 :            : }
     396                 :            : 
     397                 :            : void
     398                 :      44211 : ovsdb_monitor_add_table(struct ovsdb_monitor *m,
     399                 :            :                         const struct ovsdb_table *table)
     400                 :            : {
     401                 :            :     struct ovsdb_monitor_table *mt;
     402                 :            :     int i;
     403                 :      44211 :     size_t n_columns = shash_count(&table->schema->columns);
     404                 :            : 
     405                 :      44211 :     mt = xzalloc(sizeof *mt);
     406                 :      44211 :     mt->table = table;
     407                 :      44211 :     shash_add(&m->tables, table->schema->name, mt);
     408                 :      44211 :     hmap_init(&mt->changes);
     409                 :      44211 :     mt->columns_index_map =
     410                 :      44211 :         xmalloc(sizeof *mt->columns_index_map * n_columns);
     411         [ +  + ]:     652911 :     for (i = 0; i < n_columns; i++) {
     412                 :     608700 :         mt->columns_index_map[i] = -1;
     413                 :            :     }
     414                 :      44211 : }
     415                 :            : 
     416                 :            : const char *
     417                 :     201873 : ovsdb_monitor_add_column(struct ovsdb_monitor *dbmon,
     418                 :            :                          const struct ovsdb_table *table,
     419                 :            :                          const struct ovsdb_column *column,
     420                 :            :                          enum ovsdb_monitor_selection select,
     421                 :            :                          bool monitored)
     422                 :            : {
     423                 :            :     struct ovsdb_monitor_table *mt;
     424                 :            :     struct ovsdb_monitor_column *c;
     425                 :            : 
     426                 :     201873 :     mt = shash_find_data(&dbmon->tables, table->schema->name);
     427                 :            : 
     428                 :            :     /* Check for column duplication. Return duplicated column name. */
     429         [ +  + ]:     201873 :     if (mt->columns_index_map[column->index] != -1) {
     430                 :        237 :         return column->name;
     431                 :            :     }
     432                 :            : 
     433         [ +  + ]:     201636 :     if (mt->n_columns >= mt->allocated_columns) {
     434                 :     123947 :         mt->columns = x2nrealloc(mt->columns, &mt->allocated_columns,
     435                 :            :                                  sizeof *mt->columns);
     436                 :            :     }
     437                 :            : 
     438                 :     201636 :     mt->select |= select;
     439                 :     201636 :     mt->columns_index_map[column->index] = mt->n_columns;
     440                 :     201636 :     c = &mt->columns[mt->n_columns++];
     441                 :     201636 :     c->column = column;
     442                 :     201636 :     c->select = select;
     443                 :     201636 :     c->monitored = monitored;
     444         [ +  + ]:     201636 :     if (monitored) {
     445                 :     201562 :         mt->n_monitored_columns++;
     446                 :            :     }
     447                 :            : 
     448                 :     201636 :     return NULL;
     449                 :            : }
     450                 :            : 
     451                 :            : static void
     452                 :      44181 : ovsdb_monitor_condition_add_columns(struct ovsdb_monitor *dbmon,
     453                 :            :                                     const struct ovsdb_table *table,
     454                 :            :                                     struct ovsdb_condition *condition)
     455                 :            : {
     456                 :            :     size_t n_columns;
     457                 :            :     int i;
     458                 :      44181 :     const struct ovsdb_column **columns =
     459                 :            :         ovsdb_condition_get_columns(condition, &n_columns);
     460                 :            : 
     461         [ +  + ]:      44492 :     for (i = 0; i < n_columns; i++) {
     462                 :        311 :         ovsdb_monitor_add_column(dbmon, table, columns[i],
     463                 :            :                                  OJMS_NONE, false);
     464                 :            :     }
     465                 :            : 
     466                 :      44181 :     free(columns);
     467                 :      44181 : }
     468                 :            : 
     469                 :            : /* Bind this session's condition to ovsdb_monitor */
     470                 :            : void
     471                 :       7075 : ovsdb_monitor_condition_bind(struct ovsdb_monitor *dbmon,
     472                 :            :                           struct ovsdb_monitor_session_condition *cond)
     473                 :            : {
     474                 :            :     struct shash_node *node;
     475                 :            : 
     476 [ +  + ][ -  + ]:      51107 :     SHASH_FOR_EACH(node, &cond->tables) {
     477                 :      44032 :         struct ovsdb_monitor_table_condition *mtc = node->data;
     478                 :      44032 :         struct ovsdb_monitor_table *mt =
     479                 :      44032 :             shash_find_data(&dbmon->tables, mtc->table->schema->name);
     480                 :            : 
     481                 :      44032 :         mtc->mt = mt;
     482                 :      44032 :         ovsdb_monitor_condition_add_columns(dbmon, mtc->table,
     483                 :            :                                             &mtc->new_condition);
     484                 :            :     }
     485                 :       7075 : }
     486                 :            : 
     487                 :            : bool
     488                 :        149 : ovsdb_monitor_table_exists(struct ovsdb_monitor *m,
     489                 :            :                            const struct ovsdb_table *table)
     490                 :            : {
     491                 :        149 :     return shash_find_data(&m->tables, table->schema->name);
     492                 :            : }
     493                 :            : 
     494                 :            : static struct ovsdb_monitor_changes *
     495                 :     252022 : ovsdb_monitor_table_add_changes(struct ovsdb_monitor_table *mt,
     496                 :            :                                 uint64_t next_txn)
     497                 :            : {
     498                 :            :     struct ovsdb_monitor_changes *changes;
     499                 :            : 
     500                 :     252022 :     changes = xzalloc(sizeof *changes);
     501                 :            : 
     502                 :     252022 :     changes->transaction = next_txn;
     503                 :     252022 :     changes->mt = mt;
     504                 :     252022 :     changes->n_refs = 1;
     505                 :     252022 :     hmap_init(&changes->rows);
     506                 :     252022 :     hmap_insert(&mt->changes, &changes->hmap_node, hash_uint64(next_txn));
     507                 :            : 
     508                 :     252022 :     return changes;
     509                 :            : };
     510                 :            : 
     511                 :            : static struct ovsdb_monitor_changes *
     512                 :     767164 : ovsdb_monitor_table_find_changes(struct ovsdb_monitor_table *mt,
     513                 :            :                                  uint64_t transaction)
     514                 :            : {
     515                 :            :     struct ovsdb_monitor_changes *changes;
     516                 :     767164 :     size_t hash = hash_uint64(transaction);
     517                 :            : 
     518 [ +  + ][ -  + ]:     767164 :     HMAP_FOR_EACH_WITH_HASH(changes, hmap_node, hash, &mt->changes) {
     519         [ +  - ]:     508507 :         if (changes->transaction == transaction) {
     520                 :     508507 :             return changes;
     521                 :            :         }
     522                 :            :     }
     523                 :            : 
     524                 :     258657 :     return NULL;
     525                 :            : }
     526                 :            : 
     527                 :            : /* Stop currently tracking changes to table 'mt' since 'transaction'. */
     528                 :            : static void
     529                 :     280527 : ovsdb_monitor_table_untrack_changes(struct ovsdb_monitor_table *mt,
     530                 :            :                                     uint64_t transaction)
     531                 :            : {
     532                 :     280527 :     struct ovsdb_monitor_changes *changes =
     533                 :            :                 ovsdb_monitor_table_find_changes(mt, transaction);
     534         [ +  + ]:     280527 :     if (changes) {
     535         [ +  + ]:     273897 :         if (--changes->n_refs == 0) {
     536                 :     252022 :             hmap_remove(&mt->changes, &changes->hmap_node);
     537                 :     252022 :             ovsdb_monitor_changes_destroy(changes);
     538                 :            :         }
     539                 :            :     }
     540                 :     280527 : }
     541                 :            : 
     542                 :            : /* Start tracking changes to table 'mt' begins from 'transaction' inclusive.
     543                 :            :  */
     544                 :            : static void
     545                 :     229691 : ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table *mt,
     546                 :            :                                   uint64_t transaction)
     547                 :            : {
     548                 :            :     struct ovsdb_monitor_changes *changes;
     549                 :            : 
     550                 :     229691 :     changes = ovsdb_monitor_table_find_changes(mt, transaction);
     551         [ +  + ]:     229691 :     if (changes) {
     552                 :      21875 :         changes->n_refs++;
     553                 :            :     } else {
     554                 :     207816 :         ovsdb_monitor_table_add_changes(mt, transaction);
     555                 :            :     }
     556                 :     229691 : }
     557                 :            : 
     558                 :            : static void
     559                 :     252022 : ovsdb_monitor_changes_destroy(struct ovsdb_monitor_changes *changes)
     560                 :            : {
     561                 :            :     struct ovsdb_monitor_row *row, *next;
     562                 :            : 
     563 [ +  + ][ -  + ]:     397419 :     HMAP_FOR_EACH_SAFE (row, next, hmap_node, &changes->rows) {
                 [ +  + ]
     564                 :     145397 :         hmap_remove(&changes->rows, &row->hmap_node);
     565                 :     145397 :         ovsdb_monitor_row_destroy(changes->mt, row);
     566                 :            :     }
     567                 :     252022 :     hmap_destroy(&changes->rows);
     568                 :     252022 :     free(changes);
     569                 :     252022 : }
     570                 :            : 
     571                 :            : static enum ovsdb_monitor_selection
     572                 :     260788 : ovsdb_monitor_row_update_type(bool initial, const bool old, const bool new)
     573                 :            : {
     574 [ +  + ][ +  + ]:     260788 :     return initial ? OJMS_INITIAL
                 [ +  + ]
     575                 :     176346 :             : !old ? OJMS_INSERT
     576                 :     121601 :             : !new ? OJMS_DELETE
     577                 :            :             : OJMS_MODIFY;
     578                 :            : }
     579                 :            : 
     580                 :            : /* Set conditional monitoring mode only if we have non-empty condition in one
     581                 :            :  * of the tables at least */
     582                 :            : static inline void
     583                 :      44040 : ovsdb_monitor_session_condition_set_mode(
     584                 :            :                                   struct ovsdb_monitor_session_condition *cond)
     585                 :            : {
     586                 :      44040 :     cond->conditional = shash_count(&cond->tables) !=
     587                 :      44040 :         cond->n_true_cnd;
     588                 :      44040 : }
     589                 :            : 
     590                 :            : /* Returnes an empty allocated session's condition state holder */
     591                 :            : struct ovsdb_monitor_session_condition *
     592                 :       7075 : ovsdb_monitor_session_condition_create(void)
     593                 :            : {
     594                 :       7075 :     struct ovsdb_monitor_session_condition *condition =
     595                 :            :         xzalloc(sizeof *condition);
     596                 :            : 
     597                 :       7075 :     condition->conditional = false;
     598                 :       7075 :     shash_init(&condition->tables);
     599                 :       7075 :     return condition;
     600                 :            : }
     601                 :            : 
     602                 :            : void
     603                 :       7140 : ovsdb_monitor_session_condition_destroy(
     604                 :            :                            struct ovsdb_monitor_session_condition *condition)
     605                 :            : {
     606                 :            :     struct shash_node *node, *next;
     607                 :            : 
     608         [ +  + ]:       7140 :     if (!condition) {
     609                 :         65 :         return;
     610                 :            :     }
     611                 :            : 
     612 [ +  + ][ -  + ]:      51107 :     SHASH_FOR_EACH_SAFE (node, next, &condition->tables) {
                 [ +  + ]
     613                 :      44032 :         struct ovsdb_monitor_table_condition *mtc = node->data;
     614                 :            : 
     615                 :      44032 :         ovsdb_condition_destroy(&mtc->new_condition);
     616                 :      44032 :         ovsdb_condition_destroy(&mtc->old_condition);
     617                 :      44032 :         shash_delete(&condition->tables, node);
     618                 :      44032 :         free(mtc);
     619                 :            :     }
     620                 :       7075 :     shash_destroy(&condition->tables);
     621                 :       7075 :     free(condition);
     622                 :            : }
     623                 :            : 
     624                 :            : struct ovsdb_error *
     625                 :      44032 : ovsdb_monitor_table_condition_create(
     626                 :            :                          struct ovsdb_monitor_session_condition *condition,
     627                 :            :                          const struct ovsdb_table *table,
     628                 :            :                          const struct json *json_cnd)
     629                 :            : {
     630                 :            :     struct ovsdb_monitor_table_condition *mtc;
     631                 :            :     struct ovsdb_error *error;
     632                 :            : 
     633                 :      44032 :     mtc = xzalloc(sizeof *mtc);
     634                 :      44032 :     mtc->table = table;
     635                 :      44032 :     ovsdb_condition_init(&mtc->old_condition);
     636                 :      44032 :     ovsdb_condition_init(&mtc->new_condition);
     637                 :            : 
     638         [ +  + ]:      44032 :     if (json_cnd) {
     639                 :         79 :         error = ovsdb_condition_from_json(table->schema,
     640                 :            :                                           json_cnd,
     641                 :            :                                           NULL,
     642                 :            :                                           &mtc->old_condition);
     643         [ -  + ]:         79 :         if (error) {
     644                 :          0 :             free(mtc);
     645                 :          0 :             return error;
     646                 :            :         }
     647                 :            :     }
     648                 :            : 
     649                 :      44032 :     shash_add(&condition->tables, table->schema->name, mtc);
     650                 :            :     /* On session startup old == new condition */
     651                 :      44032 :     ovsdb_condition_clone(&mtc->new_condition, &mtc->old_condition);
     652         [ +  + ]:      44032 :     if (ovsdb_condition_is_true(&mtc->old_condition)) {
     653                 :      43955 :         condition->n_true_cnd++;
     654                 :      43955 :         ovsdb_monitor_session_condition_set_mode(condition);
     655                 :            :     }
     656                 :            : 
     657                 :      44032 :     return NULL;
     658                 :            : }
     659                 :            : 
     660                 :            : static bool
     661                 :     139786 : ovsdb_monitor_get_table_conditions(
     662                 :            :                       const struct ovsdb_monitor_table *mt,
     663                 :            :                       const struct ovsdb_monitor_session_condition *condition,
     664                 :            :                       struct ovsdb_condition **old_condition,
     665                 :            :                       struct ovsdb_condition **new_condition)
     666                 :            : {
     667         [ -  + ]:     139786 :     if (!condition) {
     668                 :          0 :         return false;
     669                 :            :     }
     670                 :            : 
     671                 :     139786 :     struct ovsdb_monitor_table_condition *mtc =
     672                 :     139786 :         shash_find_data(&condition->tables, mt->table->schema->name);
     673                 :            : 
     674         [ -  + ]:     139786 :     if (!mtc) {
     675                 :          0 :         return false;
     676                 :            :     }
     677                 :     139786 :     *old_condition = &mtc->old_condition;
     678                 :     139786 :     *new_condition = &mtc->new_condition;
     679                 :            : 
     680                 :     139786 :     return true;
     681                 :            : }
     682                 :            : 
     683                 :            : struct ovsdb_error *
     684                 :        149 : ovsdb_monitor_table_condition_update(
     685                 :            :                             struct ovsdb_monitor *dbmon,
     686                 :            :                             struct ovsdb_monitor_session_condition *condition,
     687                 :            :                             const struct ovsdb_table *table,
     688                 :            :                             const struct json *cond_json)
     689                 :            : {
     690                 :        149 :     struct ovsdb_monitor_table_condition *mtc =
     691                 :        149 :         shash_find_data(&condition->tables, table->schema->name);
     692                 :            :     struct ovsdb_error *error;
     693                 :        149 :     struct ovsdb_condition cond = OVSDB_CONDITION_INITIALIZER(&cond);
     694                 :            : 
     695         [ -  + ]:        149 :     if (!condition) {
     696                 :          0 :         return NULL;
     697                 :            :     }
     698                 :            : 
     699                 :        149 :     error = ovsdb_condition_from_json(table->schema, cond_json,
     700                 :            :                                       NULL, &cond);
     701         [ -  + ]:        149 :     if (error) {
     702                 :          0 :         return error;
     703                 :            :     }
     704                 :        149 :     ovsdb_condition_destroy(&mtc->new_condition);
     705                 :        149 :     ovsdb_condition_clone(&mtc->new_condition, &cond);
     706                 :        149 :     ovsdb_condition_destroy(&cond);
     707                 :        149 :     ovsdb_monitor_condition_add_columns(dbmon,
     708                 :            :                                         table,
     709                 :            :                                         &mtc->new_condition);
     710                 :            : 
     711                 :        149 :     return NULL;
     712                 :            : }
     713                 :            : 
     714                 :            : static void
     715                 :         85 : ovsdb_monitor_table_condition_updated(struct ovsdb_monitor_table *mt,
     716                 :            :                     struct ovsdb_monitor_session_condition *condition)
     717                 :            : {
     718                 :         85 :     struct ovsdb_monitor_table_condition *mtc =
     719                 :         85 :         shash_find_data(&condition->tables, mt->table->schema->name);
     720                 :            : 
     721         [ +  - ]:         85 :     if (mtc) {
     722                 :            :         /* If conditional monitoring - set old condition to new condition */
     723         [ +  - ]:         85 :         if (ovsdb_condition_cmp_3way(&mtc->old_condition,
     724                 :         85 :                                      &mtc->new_condition)) {
     725         [ +  + ]:         85 :             if (ovsdb_condition_is_true(&mtc->new_condition)) {
     726         [ +  - ]:         19 :                 if (!ovsdb_condition_is_true(&mtc->old_condition)) {
     727                 :         19 :                     condition->n_true_cnd++;
     728                 :            :                 }
     729                 :            :             } else {
     730         [ +  + ]:         66 :                 if (ovsdb_condition_is_true(&mtc->old_condition)) {
     731                 :          1 :                     condition->n_true_cnd--;
     732                 :            :                 }
     733                 :            :             }
     734                 :         85 :             ovsdb_condition_destroy(&mtc->old_condition);
     735                 :         85 :             ovsdb_condition_clone(&mtc->old_condition, &mtc->new_condition);
     736                 :         85 :             ovsdb_monitor_session_condition_set_mode(condition);
     737                 :            :         }
     738                 :            :     }
     739                 :         85 : }
     740                 :            : 
     741                 :            : static enum ovsdb_monitor_selection
     742                 :     139014 : ovsdb_monitor_row_update_type_condition(
     743                 :            :                       const struct ovsdb_monitor_table *mt,
     744                 :            :                       const struct ovsdb_monitor_session_condition *condition,
     745                 :            :                       bool initial,
     746                 :            :                       enum ovsdb_monitor_row_type row_type,
     747                 :            :                       const struct ovsdb_datum *old,
     748                 :            :                       const struct ovsdb_datum *new)
     749                 :            : {
     750                 :            :     struct ovsdb_condition *old_condition, *new_condition;
     751                 :     139014 :     enum ovsdb_monitor_selection type =
     752                 :     139014 :         ovsdb_monitor_row_update_type(initial, old, new);
     753                 :            : 
     754         [ +  - ]:     139014 :     if (ovsdb_monitor_get_table_conditions(mt,
     755                 :            :                                            condition,
     756                 :            :                                            &old_condition,
     757                 :            :                                            &new_condition)) {
     758                 :     139014 :         bool old_cond = !old ? false
     759 [ +  + ][ +  + ]:     139014 :             : ovsdb_condition_empty_or_match_any(old,
                 [ +  + ]
     760                 :            :                                                 old_condition,
     761                 :            :                                                 row_type == OVSDB_MONITOR_ROW ?
     762                 :            :                                                 mt->columns_index_map :
     763                 :            :                                                 NULL);
     764                 :     139014 :         bool new_cond = !new ? false
     765 [ +  + ][ +  + ]:     139014 :             : ovsdb_condition_empty_or_match_any(new,
                 [ +  + ]
     766                 :            :                                                 new_condition,
     767                 :            :                                                 row_type == OVSDB_MONITOR_ROW ?
     768                 :            :                                                 mt->columns_index_map :
     769                 :            :                                                 NULL);
     770                 :            : 
     771 [ +  + ][ +  + ]:     139014 :         if (!old_cond && !new_cond) {
     772                 :        112 :             type = OJMS_NONE;
     773                 :            :         }
     774                 :            : 
     775   [ +  +  +  +  :     139014 :         switch (type) {
                      - ]
     776                 :            :         case OJMS_INITIAL:
     777                 :            :         case OJMS_INSERT:
     778         [ -  + ]:     108847 :             if (!new_cond) {
     779                 :          0 :                 type = OJMS_NONE;
     780                 :            :             }
     781                 :     108847 :             break;
     782                 :            :         case OJMS_MODIFY:
     783 [ +  + ][ +  + ]:      25497 :             type = !old_cond ? OJMS_INSERT : !new_cond
     784                 :            :                 ? OJMS_DELETE : OJMS_MODIFY;
     785                 :      25497 :             break;
     786                 :            :         case OJMS_DELETE:
     787         [ -  + ]:       4558 :             if (!old_cond) {
     788                 :          0 :                 type = OJMS_NONE;
     789                 :            :             }
     790                 :       4558 :             break;
     791                 :            :         case OJMS_NONE:
     792                 :        112 :             break;
     793                 :            :         }
     794                 :            :     }
     795                 :     139014 :     return type;
     796                 :            : }
     797                 :            : 
     798                 :            : static bool
     799                 :     144939 : ovsdb_monitor_row_skip_update(const struct ovsdb_monitor_table *mt,
     800                 :            :                               enum ovsdb_monitor_row_type row_type,
     801                 :            :                               const struct ovsdb_datum *old,
     802                 :            :                               const struct ovsdb_datum *new,
     803                 :            :                               enum ovsdb_monitor_selection type,
     804                 :            :                               unsigned long int *changed)
     805                 :            : {
     806         [ +  + ]:     144939 :     if (!(mt->select & type)) {
     807                 :        124 :         return true;
     808                 :            :     }
     809                 :            : 
     810         [ +  + ]:     144815 :     if (type == OJMS_MODIFY) {
     811                 :            :         size_t i, n_changes;
     812                 :            : 
     813                 :      25476 :         n_changes = 0;
     814                 :      25476 :         memset(changed, 0, bitmap_n_bytes(mt->n_columns));
     815         [ +  + ]:     423412 :         for (i = 0; i < mt->n_columns; i++) {
     816                 :     397936 :             const struct ovsdb_column *c = mt->columns[i].column;
     817         [ +  + ]:     397936 :             size_t index = row_type == OVSDB_ROW ? c->index : i;
     818         [ +  + ]:     397936 :             if (!ovsdb_datum_equals(&old[index], &new[index], &c->type)) {
     819                 :      45399 :                 bitmap_set1(changed, i);
     820                 :      45399 :                 n_changes++;
     821                 :            :             }
     822                 :            :         }
     823         [ +  + ]:      25476 :         if (!n_changes) {
     824                 :            :             /* No actual changes: presumably a row changed and then
     825                 :            :              * changed back later. */
     826                 :          3 :             return true;
     827                 :            :         }
     828                 :            :     }
     829                 :            : 
     830                 :     144812 :     return false;
     831                 :            : }
     832                 :            : 
     833                 :            : /* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
     834                 :            :  * 'mt', or NULL if no row update should be sent.
     835                 :            :  *
     836                 :            :  * The caller should specify 'initial' as true if the returned JSON is going to
     837                 :            :  * be used as part of the initial reply to a "monitor" request, false if it is
     838                 :            :  * going to be used as part of an "update" notification.
     839                 :            :  *
     840                 :            :  * 'changed' must be a scratch buffer for internal use that is at least
     841                 :            :  * bitmap_n_bytes(mt->n_columns) bytes long. */
     842                 :            : static struct json *
     843                 :       5925 : ovsdb_monitor_compose_row_update(
     844                 :            :     const struct ovsdb_monitor_table *mt,
     845                 :            :     const struct ovsdb_monitor_session_condition *condition OVS_UNUSED,
     846                 :            :     enum ovsdb_monitor_row_type row_type OVS_UNUSED,
     847                 :            :     const void *_row,
     848                 :            :     bool initial, unsigned long int *changed)
     849                 :            : {
     850                 :       5925 :     const struct ovsdb_monitor_row *row = _row;
     851                 :            :     enum ovsdb_monitor_selection type;
     852                 :            :     struct json *old_json, *new_json;
     853                 :            :     struct json *row_json;
     854                 :            :     size_t i;
     855                 :            : 
     856         [ -  + ]:       5925 :     ovs_assert(row_type == OVSDB_MONITOR_ROW);
     857                 :       5925 :     type = ovsdb_monitor_row_update_type(initial, row->old, row->new);
     858         [ +  + ]:       5925 :     if (ovsdb_monitor_row_skip_update(mt, row_type, row->old,
     859                 :       5925 :                                       row->new, type, changed)) {
     860                 :         12 :         return NULL;
     861                 :            :     }
     862                 :            : 
     863                 :       5913 :     row_json = json_object_create();
     864                 :       5913 :     old_json = new_json = NULL;
     865         [ +  + ]:       5913 :     if (type & (OJMS_DELETE | OJMS_MODIFY)) {
     866                 :       2945 :         old_json = json_object_create();
     867                 :       2945 :         json_object_put(row_json, "old", old_json);
     868                 :            :     }
     869         [ +  + ]:       5913 :     if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
     870                 :       3045 :         new_json = json_object_create();
     871                 :       3045 :         json_object_put(row_json, "new", new_json);
     872                 :            :     }
     873         [ +  + ]:     163881 :     for (i = 0; i < mt->n_monitored_columns; i++) {
     874                 :     157968 :         const struct ovsdb_monitor_column *c = &mt->columns[i];
     875                 :            : 
     876 [ +  - ][ -  + ]:     157968 :         if (!c->monitored || !(type & c->select))  {
     877                 :            :             /* We don't care about this type of change for this
     878                 :            :              * particular column (but we will care about it for some
     879                 :            :              * other column). */
     880                 :          0 :             continue;
     881                 :            :         }
     882                 :            : 
     883 [ +  + ][ +  + ]:     157968 :         if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
     884         [ +  + ]:     157815 :             || type == OJMS_DELETE) {
     885                 :      78364 :             json_object_put(old_json, c->column->name,
     886                 :      78364 :                             ovsdb_datum_to_json(&row->old[i],
     887                 :      78364 :                                                 &c->column->type));
     888                 :            :         }
     889         [ +  + ]:     157968 :         if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
     890                 :      79757 :             json_object_put(new_json, c->column->name,
     891                 :      79757 :                             ovsdb_datum_to_json(&row->new[i],
     892                 :      79757 :                                                 &c->column->type));
     893                 :            :         }
     894                 :            :     }
     895                 :            : 
     896                 :       5913 :     return row_json;
     897                 :            : }
     898                 :            : 
     899                 :            : /* Returns JSON for a <row-update2> (as described in ovsdb-server(1) mapage)
     900                 :            :  * for 'row' within * 'mt', or NULL if no row update should be sent.
     901                 :            :  *
     902                 :            :  * The caller should specify 'initial' as true if the returned JSON is
     903                 :            :  * going to be used as part of the initial reply to a "monitor_cond" request,
     904                 :            :  * false if it is going to be used as part of an "update2" notification.
     905                 :            :  *
     906                 :            :  * 'changed' must be a scratch buffer for internal use that is at least
     907                 :            :  * bitmap_n_bytes(mt->n_columns) bytes long. */
     908                 :            : static struct json *
     909                 :     139014 : ovsdb_monitor_compose_row_update2(
     910                 :            :     const struct ovsdb_monitor_table *mt,
     911                 :            :     const struct ovsdb_monitor_session_condition *condition,
     912                 :            :     enum ovsdb_monitor_row_type row_type,
     913                 :            :     const void *_row,
     914                 :            :     bool initial, unsigned long int *changed)
     915                 :            : {
     916                 :            :     enum ovsdb_monitor_selection type;
     917                 :            :     struct json *row_update2, *diff_json;
     918                 :            :     const struct ovsdb_datum *old, *new;
     919                 :            :     size_t i;
     920                 :            : 
     921         [ +  + ]:     139014 :     if (row_type == OVSDB_MONITOR_ROW) {
     922                 :     138903 :         old = ((const struct ovsdb_monitor_row *)_row)->old;;
     923                 :     138903 :         new = ((const struct ovsdb_monitor_row *)_row)->new;
     924                 :            :     } else {
     925                 :        111 :         old = new = ((const struct ovsdb_row *)_row)->fields;
     926                 :            :     }
     927                 :            : 
     928                 :     139014 :     type = ovsdb_monitor_row_update_type_condition(mt, condition, initial,
     929                 :            :                                                    row_type, old, new);
     930         [ +  + ]:     139014 :     if (ovsdb_monitor_row_skip_update(mt, row_type, old, new, type, changed)) {
     931                 :        115 :         return NULL;
     932                 :            :     }
     933                 :            : 
     934                 :     138899 :     row_update2 = json_object_create();
     935         [ +  + ]:     138899 :     if (type == OJMS_DELETE) {
     936                 :       4571 :         json_object_put(row_update2, "delete", json_null_create());
     937                 :            :     } else {
     938                 :     134328 :         diff_json = json_object_create();
     939                 :            :         const char *op;
     940                 :            : 
     941         [ +  + ]:    1150190 :         for (i = 0; i < mt->n_monitored_columns; i++) {
     942                 :    1015862 :             const struct ovsdb_monitor_column *c = &mt->columns[i];
     943         [ +  + ]:    1015862 :             size_t index = row_type == OVSDB_ROW ? c->column->index : i;
     944 [ +  - ][ -  + ]:    1015862 :             if (!c->monitored || !(type & c->select))  {
     945                 :            :                 /* We don't care about this type of change for this
     946                 :            :                  * particular column (but we will care about it for some
     947                 :            :                  * other column). */
     948                 :          0 :                 continue;
     949                 :            :             }
     950                 :            : 
     951         [ +  + ]:    1015862 :             if (type == OJMS_MODIFY) {
     952                 :            :                 struct ovsdb_datum diff;
     953                 :            : 
     954         [ +  + ]:     397023 :                 if (!bitmap_is_set(changed, i)) {
     955                 :     351777 :                     continue;
     956                 :            :                 }
     957                 :            : 
     958                 :      45246 :                 ovsdb_datum_diff(&diff ,&old[index], &new[index],
     959                 :      45246 :                                         &c->column->type);
     960                 :      45246 :                 json_object_put(diff_json, c->column->name,
     961                 :      45246 :                                 ovsdb_datum_to_json(&diff, &c->column->type));
     962                 :      45246 :                 ovsdb_datum_destroy(&diff, &c->column->type);
     963                 :            :             } else {
     964         [ +  + ]:     618839 :                 if (!ovsdb_datum_is_default(&new[index], &c->column->type)) {
     965                 :     339396 :                     json_object_put(diff_json, c->column->name,
     966                 :     339396 :                                     ovsdb_datum_to_json(&new[index],
     967                 :     339396 :                                                         &c->column->type));
     968                 :            :                 }
     969                 :            :             }
     970                 :            :         }
     971                 :            : 
     972                 :     134328 :         op = type == OJMS_INITIAL ? "initial"
     973 [ +  + ][ +  + ]:     134328 :                                   : type == OJMS_MODIFY ? "modify" : "insert";
     974                 :     134328 :         json_object_put(row_update2, op, diff_json);
     975                 :            :     }
     976                 :            : 
     977                 :     138899 :     return row_update2;
     978                 :            : }
     979                 :            : 
     980                 :            : static size_t
     981                 :      23260 : ovsdb_monitor_max_columns(struct ovsdb_monitor *dbmon)
     982                 :            : {
     983                 :            :     struct shash_node *node;
     984                 :      23260 :     size_t max_columns = 0;
     985                 :            : 
     986 [ +  + ][ -  + ]:     236772 :     SHASH_FOR_EACH (node, &dbmon->tables) {
     987                 :     213512 :         struct ovsdb_monitor_table *mt = node->data;
     988                 :            : 
     989                 :     213512 :         max_columns = MAX(max_columns, mt->n_columns);
     990                 :            :     }
     991                 :            : 
     992                 :      23260 :     return max_columns;
     993                 :            : }
     994                 :            : 
     995                 :            : static void
     996                 :     144812 : ovsdb_monitor_add_json_row(struct json **json, const char *table_name,
     997                 :            :                            struct json **table_json, struct json *row_json,
     998                 :            :                            const struct uuid *row_uuid)
     999                 :            : {
    1000                 :            :     char uuid[UUID_LEN + 1];
    1001                 :            : 
    1002                 :            :     /* Create JSON object for transaction overall. */
    1003         [ +  + ]:     144812 :     if (!*json) {
    1004                 :      22304 :         *json = json_object_create();
    1005                 :            :     }
    1006                 :            : 
    1007                 :            :     /* Create JSON object for transaction on this table. */
    1008         [ +  + ]:     144812 :     if (!*table_json) {
    1009                 :      49831 :         *table_json = json_object_create();
    1010                 :      49831 :         json_object_put(*json, table_name, *table_json);
    1011                 :            :     }
    1012                 :            : 
    1013                 :            :     /* Add JSON row to JSON table. */
    1014                 :     144812 :     snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(row_uuid));
    1015                 :     144812 :     json_object_put(*table_json, uuid, row_json);
    1016                 :     144812 : }
    1017                 :            : 
    1018                 :            : /* Constructs and returns JSON for a <table-updates> object (as described in
    1019                 :            :  * RFC 7047) for all the outstanding changes within 'monitor', starting from
    1020                 :            :  * 'transaction'.  */
    1021                 :            : static struct json*
    1022                 :      23111 : ovsdb_monitor_compose_update(
    1023                 :            :                       struct ovsdb_monitor *dbmon,
    1024                 :            :                       bool initial, uint64_t transaction,
    1025                 :            :                       const struct ovsdb_monitor_session_condition *condition,
    1026                 :            :                       compose_row_update_cb_func row_update)
    1027                 :            : {
    1028                 :            :     struct shash_node *node;
    1029                 :            :     struct json *json;
    1030                 :      23111 :     size_t max_columns = ovsdb_monitor_max_columns(dbmon);
    1031                 :      23111 :     unsigned long int *changed = xmalloc(bitmap_n_bytes(max_columns));
    1032                 :            : 
    1033                 :      23111 :     json = NULL;
    1034 [ +  + ][ -  + ]:     235851 :     SHASH_FOR_EACH (node, &dbmon->tables) {
    1035                 :     212740 :         struct ovsdb_monitor_table *mt = node->data;
    1036                 :            :         struct ovsdb_monitor_row *row, *next;
    1037                 :            :         struct ovsdb_monitor_changes *changes;
    1038                 :     212740 :         struct json *table_json = NULL;
    1039                 :            : 
    1040                 :     212740 :         changes = ovsdb_monitor_table_find_changes(mt, transaction);
    1041         [ +  + ]:     212740 :         if (!changes) {
    1042                 :          5 :             continue;
    1043                 :            :         }
    1044                 :            : 
    1045 [ +  + ][ -  + ]:     357563 :         HMAP_FOR_EACH_SAFE (row, next, hmap_node, &changes->rows) {
                 [ +  + ]
    1046                 :            :             struct json *row_json;
    1047                 :     144828 :             row_json = (*row_update)(mt, condition, OVSDB_MONITOR_ROW, row,
    1048                 :            :                                      initial, changed);
    1049         [ +  + ]:     144828 :             if (row_json) {
    1050                 :     144716 :                 ovsdb_monitor_add_json_row(&json, mt->table->schema->name,
    1051                 :            :                                            &table_json, row_json,
    1052                 :     144716 :                                            &row->uuid);
    1053                 :            :             }
    1054                 :            :         }
    1055                 :            :     }
    1056                 :      23111 :     free(changed);
    1057                 :            : 
    1058                 :      23111 :     return json;
    1059                 :            : }
    1060                 :            : 
    1061                 :            : static struct json*
    1062                 :        149 : ovsdb_monitor_compose_cond_change_update(
    1063                 :            :                     struct ovsdb_monitor *dbmon,
    1064                 :            :                     struct ovsdb_monitor_session_condition *condition)
    1065                 :            : {
    1066                 :            :     struct shash_node *node;
    1067                 :        149 :     struct json *json = NULL;
    1068                 :        149 :     size_t max_columns = ovsdb_monitor_max_columns(dbmon);
    1069                 :        149 :     unsigned long int *changed = xmalloc(bitmap_n_bytes(max_columns));
    1070                 :            : 
    1071 [ +  + ][ -  + ]:        921 :     SHASH_FOR_EACH (node, &dbmon->tables) {
    1072                 :        772 :         struct ovsdb_monitor_table *mt = node->data;
    1073                 :            :         struct ovsdb_row *row;
    1074                 :        772 :         struct json *table_json = NULL;
    1075                 :            :         struct ovsdb_condition *old_condition, *new_condition;
    1076                 :            : 
    1077         [ +  - ]:        772 :         if (!ovsdb_monitor_get_table_conditions(mt,
    1078                 :            :                                                 condition,
    1079                 :            :                                                 &old_condition,
    1080         [ +  + ]:        772 :                                                 &new_condition) ||
    1081                 :        772 :             !ovsdb_condition_cmp_3way(old_condition, new_condition)) {
    1082                 :            :             /* Nothing to update on this table */
    1083                 :        687 :             continue;
    1084                 :            :         }
    1085                 :            : 
    1086                 :            :         /* Iterate over all rows in table */
    1087 [ +  + ][ -  + ]:        196 :         HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
    1088                 :            :             struct json *row_json;
    1089                 :            : 
    1090                 :        111 :             row_json = ovsdb_monitor_compose_row_update2(mt, condition,
    1091                 :            :                                                          OVSDB_ROW, row,
    1092                 :            :                                                          false, changed);
    1093         [ +  + ]:        111 :             if (row_json) {
    1094                 :         96 :                 ovsdb_monitor_add_json_row(&json, mt->table->schema->name,
    1095                 :            :                                            &table_json, row_json,
    1096                 :            :                                            ovsdb_row_get_uuid(row));
    1097                 :            :             }
    1098                 :            :         }
    1099                 :         85 :         ovsdb_monitor_table_condition_updated(mt, condition);
    1100                 :            :     }
    1101                 :        149 :     free(changed);
    1102                 :            : 
    1103                 :        149 :     return json;
    1104                 :            : }
    1105                 :            : 
    1106                 :            : /* Returns JSON for a <table-updates> object (as described in RFC 7047)
    1107                 :            :  * for all the outstanding changes within 'monitor' that starts from
    1108                 :            :  * '*unflushed'.
    1109                 :            :  * If cond_updated is true all rows in the db that match conditions will be
    1110                 :            :  * sent.
    1111                 :            :  *
    1112                 :            :  * The caller should specify 'initial' as true if the returned JSON is going to
    1113                 :            :  * be used as part of the initial reply to a "monitor" request, false if it is
    1114                 :            :  * going to be used as part of an "update" notification. */
    1115                 :            : struct json *
    1116                 :      24733 : ovsdb_monitor_get_update(
    1117                 :            :              struct ovsdb_monitor *dbmon,
    1118                 :            :              bool initial, bool cond_updated,
    1119                 :            :              uint64_t *unflushed_,
    1120                 :            :              struct ovsdb_monitor_session_condition *condition,
    1121                 :            :              enum ovsdb_monitor_version version)
    1122                 :            : {
    1123                 :      24733 :     struct ovsdb_monitor_json_cache_node *cache_node = NULL;
    1124                 :            :     struct shash_node *node;
    1125                 :            :     struct json *json;
    1126                 :      24733 :     const uint64_t unflushed = *unflushed_;
    1127                 :      24733 :     const uint64_t next_unflushed = dbmon->n_transactions + 1;
    1128                 :            : 
    1129 [ +  + ][ -  + ]:      24733 :     ovs_assert(cond_updated ? unflushed == next_unflushed : true);
    1130                 :            : 
    1131                 :            :     /* Return a clone of cached json if one exists. Otherwise,
    1132                 :            :      * generate a new one and add it to the cache.  */
    1133 [ +  + ][ +  + ]:      24733 :     if (!condition || (!condition->conditional && !cond_updated)) {
                 [ +  + ]
    1134                 :      24514 :         cache_node = ovsdb_monitor_json_cache_search(dbmon, version,
    1135                 :            :                                                      unflushed);
    1136                 :            :     }
    1137         [ +  + ]:      24733 :     if (cache_node) {
    1138         [ +  + ]:       1473 :         json = cache_node->json ? json_clone(cache_node->json) : NULL;
    1139                 :            :     } else {
    1140         [ +  + ]:      23260 :         if (version == OVSDB_MONITOR_V1) {
    1141                 :        207 :             json =
    1142                 :        207 :                ovsdb_monitor_compose_update(dbmon, initial, unflushed,
    1143                 :            :                                             condition,
    1144                 :            :                                             ovsdb_monitor_compose_row_update);
    1145                 :            :         } else {
    1146         [ -  + ]:      23053 :             ovs_assert(version == OVSDB_MONITOR_V2);
    1147         [ +  + ]:      23053 :             if (!cond_updated) {
    1148                 :      22904 :                 json = ovsdb_monitor_compose_update(dbmon, initial, unflushed,
    1149                 :            :                                             condition,
    1150                 :            :                                             ovsdb_monitor_compose_row_update2);
    1151                 :            : 
    1152 [ +  - ][ +  + ]:      22904 :                 if (!condition || !condition->conditional) {
    1153                 :      22904 :                     ovsdb_monitor_json_cache_insert(dbmon, version, unflushed,
    1154                 :            :                                                     json);
    1155                 :            :                 }
    1156                 :            :             } else {
    1157                 :            :                 /* Compose update on whole db due to condition update.
    1158                 :            :                    Session must be flushed (change list is empty)*/
    1159                 :        149 :                 json =
    1160                 :            :                     ovsdb_monitor_compose_cond_change_update(dbmon, condition);
    1161                 :            :             }
    1162                 :            :         }
    1163                 :            :     }
    1164                 :            : 
    1165                 :            :     /* Maintain transaction id of 'changes'. */
    1166 [ +  + ][ -  + ]:     254424 :     SHASH_FOR_EACH (node, &dbmon->tables) {
    1167                 :     229691 :         struct ovsdb_monitor_table *mt = node->data;
    1168                 :            : 
    1169                 :     229691 :         ovsdb_monitor_table_untrack_changes(mt, unflushed);
    1170                 :     229691 :         ovsdb_monitor_table_track_changes(mt, next_unflushed);
    1171                 :            :     }
    1172                 :      24733 :     *unflushed_ = next_unflushed;
    1173                 :            : 
    1174                 :      24733 :     return json;
    1175                 :            : }
    1176                 :            : 
    1177                 :            : bool
    1178                 :     158421 : ovsdb_monitor_needs_flush(struct ovsdb_monitor *dbmon,
    1179                 :            :                           uint64_t next_transaction)
    1180                 :            : {
    1181         [ -  + ]:     158421 :     ovs_assert(next_transaction <= dbmon->n_transactions + 1);
    1182                 :     158421 :     return (next_transaction <= dbmon->n_transactions);
    1183                 :            : }
    1184                 :            : 
    1185                 :            : void
    1186                 :      44211 : ovsdb_monitor_table_add_select(struct ovsdb_monitor *dbmon,
    1187                 :            :                                const struct ovsdb_table *table,
    1188                 :            :                                enum ovsdb_monitor_selection select)
    1189                 :            : {
    1190                 :            :     struct ovsdb_monitor_table * mt;
    1191                 :            : 
    1192                 :      44211 :     mt = shash_find_data(&dbmon->tables, table->schema->name);
    1193                 :      44211 :     mt->select |= select;
    1194                 :      44211 : }
    1195                 :            : 
    1196                 :            :  /*
    1197                 :            :  * If a row's change type (insert, delete or modify) matches that of
    1198                 :            :  * the monitor, they should be sent to the monitor's clients as updates.
    1199                 :            :  * Of cause, the monitor should also internally update with this change.
    1200                 :            :  *
    1201                 :            :  * When a change type does not require client side update, the monitor
    1202                 :            :  * may still need to keep track of certain changes in order to generate
    1203                 :            :  * correct future updates.  For example, the monitor internal state should
    1204                 :            :  * be updated whenever a new row is inserted, in order to generate the
    1205                 :            :  * correct initial state, regardless if a insert change type is being
    1206                 :            :  * monitored.
    1207                 :            :  *
    1208                 :            :  * On the other hand, if a transaction only contains changes to columns
    1209                 :            :  * that are not monitored, this transaction can be safely ignored by the
    1210                 :            :  * monitor.
    1211                 :            :  *
    1212                 :            :  * Thus, the order of the declaration is important:
    1213                 :            :  * 'OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE' always implies
    1214                 :            :  * 'OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE', but not vice versa.  */
    1215                 :            : enum ovsdb_monitor_changes_efficacy {
    1216                 :            :     OVSDB_CHANGES_NO_EFFECT,                /* Monitor does not care about this
    1217                 :            :                                                change.  */
    1218                 :            :     OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE,  /* Monitor internal updates. */
    1219                 :            :     OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE,  /* Client needs to be updated.  */
    1220                 :            : };
    1221                 :            : 
    1222                 :            : struct ovsdb_monitor_aux {
    1223                 :            :     const struct ovsdb_monitor *monitor;
    1224                 :            :     struct ovsdb_monitor_table *mt;
    1225                 :            :     enum ovsdb_monitor_changes_efficacy efficacy;
    1226                 :            : };
    1227                 :            : 
    1228                 :            : static void
    1229                 :      17051 : ovsdb_monitor_init_aux(struct ovsdb_monitor_aux *aux,
    1230                 :            :                        const struct ovsdb_monitor *m)
    1231                 :            : {
    1232                 :      17051 :     aux->monitor = m;
    1233                 :      17051 :     aux->mt = NULL;
    1234                 :      17051 :     aux->efficacy = OVSDB_CHANGES_NO_EFFECT;
    1235                 :      17051 : }
    1236                 :            : 
    1237                 :            : static void
    1238                 :     150317 : ovsdb_monitor_changes_update(const struct ovsdb_row *old,
    1239                 :            :                              const struct ovsdb_row *new,
    1240                 :            :                              const struct ovsdb_monitor_table *mt,
    1241                 :            :                              struct ovsdb_monitor_changes *changes)
    1242                 :            : {
    1243         [ +  + ]:     150317 :     const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
    1244                 :            :     struct ovsdb_monitor_row *change;
    1245                 :            : 
    1246                 :     150317 :     change = ovsdb_monitor_changes_row_find(changes, uuid);
    1247         [ +  + ]:     150317 :     if (!change) {
    1248                 :     147833 :         change = xzalloc(sizeof *change);
    1249                 :     147833 :         hmap_insert(&changes->rows, &change->hmap_node, uuid_hash(uuid));
    1250                 :     147833 :         change->uuid = *uuid;
    1251                 :     147833 :         change->old = clone_monitor_row_data(mt, old);
    1252                 :     147833 :         change->new = clone_monitor_row_data(mt, new);
    1253                 :            :     } else {
    1254         [ +  + ]:       2484 :         if (new) {
    1255                 :         48 :             update_monitor_row_data(mt, new, change->new);
    1256                 :            :         } else {
    1257                 :       2436 :             free_monitor_row_data(mt, change->new);
    1258                 :       2436 :             change->new = NULL;
    1259                 :            : 
    1260         [ +  - ]:       2436 :             if (!change->old) {
    1261                 :            :                 /* This row was added then deleted.  Forget about it. */
    1262                 :       2436 :                 hmap_remove(&changes->rows, &change->hmap_node);
    1263                 :       2436 :                 free(change);
    1264                 :            :             }
    1265                 :            :         }
    1266                 :            :     }
    1267                 :     150317 : }
    1268                 :            : 
    1269                 :            : static bool
    1270                 :      81347 : ovsdb_monitor_columns_changed(const struct ovsdb_monitor_table *mt,
    1271                 :            :                               const unsigned long int *changed)
    1272                 :            : {
    1273                 :            :     size_t i;
    1274                 :            : 
    1275         [ +  + ]:     748196 :     for (i = 0; i < mt->n_columns; i++) {
    1276                 :     692336 :         size_t column_index = mt->columns[i].column->index;
    1277                 :            : 
    1278         [ +  + ]:     692336 :         if (bitmap_is_set(changed, column_index)) {
    1279                 :      25487 :             return true;
    1280                 :            :         }
    1281                 :            :     }
    1282                 :            : 
    1283                 :      55860 :     return false;
    1284                 :            : }
    1285                 :            : 
    1286                 :            : /* Return the efficacy of a row's change to a monitor table.
    1287                 :            :  *
    1288                 :            :  * Please see the block comment above 'ovsdb_monitor_changes_efficacy'
    1289                 :            :  * definition form more information.  */
    1290                 :            : static enum ovsdb_monitor_changes_efficacy
    1291                 :     115849 : ovsdb_monitor_changes_classify(enum ovsdb_monitor_selection type,
    1292                 :            :                                const struct ovsdb_monitor_table *mt,
    1293                 :            :                                const unsigned long int *changed)
    1294                 :            : {
    1295   [ +  +  +  + ]:     197196 :     if (type == OJMS_MODIFY &&
    1296                 :      81347 :         !ovsdb_monitor_columns_changed(mt, changed)) {
    1297                 :      55860 :         return OVSDB_CHANGES_NO_EFFECT;
    1298                 :            :     }
    1299                 :            : 
    1300         [ +  + ]:      59989 :     if (type == OJMS_MODIFY) {
    1301                 :            :         /* Condition might turn a modify operation to insert or delete */
    1302                 :      25487 :         type |= OJMS_INSERT | OJMS_DELETE;
    1303                 :            :     }
    1304                 :            : 
    1305         [ +  + ]:      59989 :     return (mt->select & type)
    1306                 :            :                 ?  OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
    1307                 :            :                 :  OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE;
    1308                 :            : }
    1309                 :            : 
    1310                 :            : static bool
    1311                 :     116007 : ovsdb_monitor_change_cb(const struct ovsdb_row *old,
    1312                 :            :                         const struct ovsdb_row *new,
    1313                 :            :                         const unsigned long int *changed,
    1314                 :            :                         void *aux_)
    1315                 :            : {
    1316                 :     116007 :     struct ovsdb_monitor_aux *aux = aux_;
    1317                 :     116007 :     const struct ovsdb_monitor *m = aux->monitor;
    1318         [ +  + ]:     116007 :     struct ovsdb_table *table = new ? new->table : old->table;
    1319                 :            :     struct ovsdb_monitor_table *mt;
    1320                 :            :     struct ovsdb_monitor_changes *changes;
    1321                 :            : 
    1322 [ +  + ][ +  + ]:     116007 :     if (!aux->mt || table != aux->mt->table) {
    1323                 :      39037 :         aux->mt = shash_find_data(&m->tables, table->schema->name);
    1324         [ +  + ]:      39037 :         if (!aux->mt) {
    1325                 :            :             /* We don't care about rows in this table at all.  Tell the caller
    1326                 :            :              * to skip it.  */
    1327                 :        158 :             return false;
    1328                 :            :         }
    1329                 :            :     }
    1330                 :     115849 :     mt = aux->mt;
    1331                 :            : 
    1332                 :     115849 :     enum ovsdb_monitor_selection type =
    1333                 :     115849 :         ovsdb_monitor_row_update_type(false, old, new);
    1334                 :     115849 :     enum ovsdb_monitor_changes_efficacy efficacy =
    1335                 :            :         ovsdb_monitor_changes_classify(type, mt, changed);
    1336                 :            : 
    1337 [ +  + ][ -  + ]:     236605 :     HMAP_FOR_EACH(changes, hmap_node, &mt->changes) {
    1338         [ +  + ]:     120756 :         if (efficacy > OVSDB_CHANGES_NO_EFFECT) {
    1339                 :      64896 :             ovsdb_monitor_changes_update(old, new, mt, changes);
    1340                 :            :         }
    1341                 :            :     }
    1342         [ +  + ]:     115849 :     if (aux->efficacy < efficacy) {
    1343                 :      16073 :         aux->efficacy = efficacy;
    1344                 :            :     }
    1345                 :            : 
    1346                 :     115849 :     return true;
    1347                 :            : }
    1348                 :            : 
    1349                 :            : void
    1350                 :       7140 : ovsdb_monitor_get_initial(const struct ovsdb_monitor *dbmon)
    1351                 :            : {
    1352                 :            :     struct shash_node *node;
    1353                 :            : 
    1354 [ +  + ][ -  + ]:      51351 :     SHASH_FOR_EACH (node, &dbmon->tables) {
    1355                 :      44211 :         struct ovsdb_monitor_table *mt = node->data;
    1356                 :            : 
    1357         [ +  + ]:      44211 :         if (mt->select & OJMS_INITIAL) {
    1358                 :            :             struct ovsdb_row *row;
    1359                 :            :             struct ovsdb_monitor_changes *changes;
    1360                 :            : 
    1361                 :      44206 :             changes = ovsdb_monitor_table_find_changes(mt, 0);
    1362         [ +  - ]:      44206 :             if (!changes) {
    1363                 :      44206 :                 changes = ovsdb_monitor_table_add_changes(mt, 0);
    1364 [ +  + ][ -  + ]:     129627 :                 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
    1365                 :      85421 :                     ovsdb_monitor_changes_update(NULL, row, mt, changes);
    1366                 :            :                 }
    1367                 :            :             } else {
    1368                 :          0 :                 changes->n_refs++;
    1369                 :            :             }
    1370                 :            :         }
    1371                 :            :     }
    1372                 :       7140 : }
    1373                 :            : 
    1374                 :            : void
    1375                 :       7742 : ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
    1376                 :            :                    struct ovsdb_jsonrpc_monitor *jsonrpc_monitor,
    1377                 :            :                    uint64_t unflushed)
    1378                 :            : {
    1379                 :            :     struct jsonrpc_monitor_node *jm;
    1380                 :            : 
    1381         [ -  + ]:       7742 :     if (ovs_list_is_empty(&dbmon->jsonrpc_monitors)) {
    1382                 :          0 :         ovsdb_monitor_destroy(dbmon);
    1383                 :          0 :         return;
    1384                 :            :     }
    1385                 :            : 
    1386                 :            :     /* Find and remove the jsonrpc monitor from the list.  */
    1387         [ +  - ]:       8330 :     LIST_FOR_EACH(jm, node, &dbmon->jsonrpc_monitors) {
    1388         [ +  + ]:       8330 :         if (jm->jsonrpc_monitor == jsonrpc_monitor) {
    1389                 :            :             /* Release the tracked changes. */
    1390                 :            :             struct shash_node *node;
    1391 [ +  + ][ -  + ]:      58578 :             SHASH_FOR_EACH (node, &dbmon->tables) {
    1392                 :      50836 :                 struct ovsdb_monitor_table *mt = node->data;
    1393                 :      50836 :                 ovsdb_monitor_table_untrack_changes(mt, unflushed);
    1394                 :            :             }
    1395                 :       7742 :             ovs_list_remove(&jm->node);
    1396                 :       7742 :             free(jm);
    1397                 :            : 
    1398                 :            :             /* Destroy ovsdb monitor if this is the last user.  */
    1399         [ +  + ]:       7742 :             if (ovs_list_is_empty(&dbmon->jsonrpc_monitors)) {
    1400                 :       7140 :                 ovsdb_monitor_destroy(dbmon);
    1401                 :            :             }
    1402                 :            : 
    1403                 :       7742 :             return;
    1404                 :            :         };
    1405                 :            :     }
    1406                 :            : 
    1407                 :            :     /* Should never reach here. jsonrpc_monitor should be on the list.  */
    1408                 :          0 :     OVS_NOT_REACHED();
    1409                 :            : }
    1410                 :            : 
    1411                 :            : static bool
    1412                 :       6625 : ovsdb_monitor_table_equal(const struct ovsdb_monitor_table *a,
    1413                 :            :                           const struct ovsdb_monitor_table *b)
    1414                 :            : {
    1415                 :            :     size_t i;
    1416                 :            : 
    1417         [ -  + ]:       6625 :     ovs_assert(b->n_columns == b->n_monitored_columns);
    1418                 :            : 
    1419 [ +  - ][ +  - ]:       6625 :     if ((a->table != b->table) ||
    1420         [ -  + ]:       6625 :         (a->select != b->select) ||
    1421                 :       6625 :         (a->n_monitored_columns != b->n_monitored_columns)) {
    1422                 :          0 :         return false;
    1423                 :            :     }
    1424                 :            : 
    1425                 :            :     /* Compare only monitored columns that must be sorted already */
    1426         [ +  + ]:      42082 :     for (i = 0; i < a->n_monitored_columns; i++) {
    1427 [ +  - ][ -  + ]:      35457 :         if ((a->columns[i].column != b->columns[i].column) ||
    1428                 :      35457 :             (a->columns[i].select != b->columns[i].select)) {
    1429                 :          0 :             return false;
    1430                 :            :         }
    1431                 :            :     }
    1432                 :       6625 :     return true;
    1433                 :            : }
    1434                 :            : 
    1435                 :            : static bool
    1436                 :        602 : ovsdb_monitor_equal(const struct ovsdb_monitor *a,
    1437                 :            :                     const struct ovsdb_monitor *b)
    1438                 :            : {
    1439                 :            :     struct shash_node *node;
    1440                 :            : 
    1441         [ -  + ]:        602 :     if (shash_count(&a->tables) != shash_count(&b->tables)) {
    1442                 :          0 :         return false;
    1443                 :            :     }
    1444                 :            : 
    1445 [ +  + ][ -  + ]:       7227 :     SHASH_FOR_EACH(node, &a->tables) {
    1446                 :       6625 :         const struct ovsdb_monitor_table *mta = node->data;
    1447                 :            :         const struct ovsdb_monitor_table *mtb;
    1448                 :            : 
    1449                 :       6625 :         mtb = shash_find_data(&b->tables, node->name);
    1450         [ -  + ]:       6625 :         if (!mtb) {
    1451                 :          0 :             return false;
    1452                 :            :         }
    1453                 :            : 
    1454         [ -  + ]:       6625 :         if (!ovsdb_monitor_table_equal(mta, mtb)) {
    1455                 :          0 :             return false;
    1456                 :            :         }
    1457                 :            :     }
    1458                 :            : 
    1459                 :        602 :     return true;
    1460                 :            : }
    1461                 :            : 
    1462                 :            : static size_t
    1463                 :       7140 : ovsdb_monitor_hash(const struct ovsdb_monitor *dbmon, size_t basis)
    1464                 :            : {
    1465                 :            :     const struct shash_node **nodes;
    1466                 :            :     size_t i, j, n;
    1467                 :            : 
    1468                 :       7140 :     nodes = shash_sort(&dbmon->tables);
    1469                 :       7140 :     n = shash_count(&dbmon->tables);
    1470                 :            : 
    1471         [ +  + ]:      51351 :     for (i = 0; i < n; i++) {
    1472                 :      44211 :         struct ovsdb_monitor_table *mt = nodes[i]->data;
    1473                 :            : 
    1474                 :      44211 :         basis = hash_pointer(mt->table, basis);
    1475                 :      44211 :         basis = hash_3words(mt->select, mt->n_columns, basis);
    1476                 :            : 
    1477         [ +  + ]:     245773 :         for (j = 0; j < mt->n_columns; j++) {
    1478                 :     201562 :             basis = hash_pointer(mt->columns[j].column, basis);
    1479                 :     201562 :             basis = hash_2words(mt->columns[j].select, basis);
    1480                 :            :         }
    1481                 :            :     }
    1482                 :       7140 :     free(nodes);
    1483                 :            : 
    1484                 :       7140 :     return basis;
    1485                 :            : }
    1486                 :            : 
    1487                 :            : struct ovsdb_monitor *
    1488                 :       7140 : ovsdb_monitor_add(struct ovsdb_monitor *new_dbmon)
    1489                 :            : {
    1490                 :            :     struct ovsdb_monitor *dbmon;
    1491                 :            :     size_t hash;
    1492                 :            : 
    1493                 :            :     /* New_dbmon should be associated with only one jsonrpc
    1494                 :            :      * connections.  */
    1495         [ -  + ]:       7140 :     ovs_assert(ovs_list_is_singleton(&new_dbmon->jsonrpc_monitors));
    1496                 :            : 
    1497                 :       7140 :     ovsdb_monitor_columns_sort(new_dbmon);
    1498                 :            : 
    1499                 :       7140 :     hash = ovsdb_monitor_hash(new_dbmon, 0);
    1500 [ +  + ][ -  + ]:       7140 :     HMAP_FOR_EACH_WITH_HASH(dbmon, hmap_node, hash, &ovsdb_monitors) {
    1501         [ +  - ]:        602 :         if (ovsdb_monitor_equal(dbmon,  new_dbmon)) {
    1502                 :        602 :             return dbmon;
    1503                 :            :         }
    1504                 :            :     }
    1505                 :            : 
    1506                 :       6538 :     hmap_insert(&ovsdb_monitors, &new_dbmon->hmap_node, hash);
    1507                 :       6538 :     return new_dbmon;
    1508                 :            : }
    1509                 :            : 
    1510                 :            : static void
    1511                 :       7140 : ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon)
    1512                 :            : {
    1513                 :            :     struct shash_node *node;
    1514                 :            : 
    1515                 :       7140 :     ovs_list_remove(&dbmon->replica.node);
    1516                 :            : 
    1517         [ +  + ]:       7140 :     if (!hmap_node_is_null(&dbmon->hmap_node)) {
    1518                 :       6538 :         hmap_remove(&ovsdb_monitors, &dbmon->hmap_node);
    1519                 :            :     }
    1520                 :            : 
    1521                 :       7140 :     ovsdb_monitor_json_cache_flush(dbmon);
    1522                 :       7140 :     hmap_destroy(&dbmon->json_cache);
    1523                 :            : 
    1524 [ +  + ][ -  + ]:      51351 :     SHASH_FOR_EACH (node, &dbmon->tables) {
    1525                 :      44211 :         struct ovsdb_monitor_table *mt = node->data;
    1526                 :            :         struct ovsdb_monitor_changes *changes, *next;
    1527                 :            : 
    1528 [ +  - ][ -  + ]:      44211 :         HMAP_FOR_EACH_SAFE (changes, next, hmap_node, &mt->changes) {
                 [ -  + ]
    1529                 :          0 :             hmap_remove(&mt->changes, &changes->hmap_node);
    1530                 :          0 :             ovsdb_monitor_changes_destroy(changes);
    1531                 :            :         }
    1532                 :      44211 :         hmap_destroy(&mt->changes);
    1533                 :      44211 :         free(mt->columns);
    1534                 :      44211 :         free(mt->columns_index_map);
    1535                 :      44211 :         free(mt);
    1536                 :            :     }
    1537                 :       7140 :     shash_destroy(&dbmon->tables);
    1538                 :       7140 :     free(dbmon);
    1539                 :       7140 : }
    1540                 :            : 
    1541                 :            : static struct ovsdb_error *
    1542                 :      17051 : ovsdb_monitor_commit(struct ovsdb_replica *replica,
    1543                 :            :                      const struct ovsdb_txn *txn,
    1544                 :            :                      bool durable OVS_UNUSED)
    1545                 :            : {
    1546                 :      17051 :     struct ovsdb_monitor *m = ovsdb_monitor_cast(replica);
    1547                 :            :     struct ovsdb_monitor_aux aux;
    1548                 :            : 
    1549                 :      17051 :     ovsdb_monitor_init_aux(&aux, m);
    1550                 :            :     /* Update ovsdb_monitor's transaction number for
    1551                 :            :      * each transaction, before calling ovsdb_monitor_change_cb().  */
    1552                 :      17051 :     m->n_transactions++;
    1553                 :      17051 :     ovsdb_txn_for_each_change(txn, ovsdb_monitor_change_cb, &aux);
    1554                 :            : 
    1555   [ +  +  +  - ]:      17051 :     switch(aux.efficacy) {
    1556                 :            :     case OVSDB_CHANGES_NO_EFFECT:
    1557                 :            :         /* The transaction is ignored by the monitor.
    1558                 :            :          * Roll back the 'n_transactions' as if the transaction
    1559                 :            :          * has never happened. */
    1560                 :        978 :         m->n_transactions--;
    1561                 :        978 :         break;
    1562                 :            :     case OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE:
    1563                 :            :         /* Nothing.  */
    1564                 :          7 :         break;
    1565                 :            :     case  OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE:
    1566                 :      16066 :         ovsdb_monitor_json_cache_flush(m);
    1567                 :      16066 :         break;
    1568                 :            :     }
    1569                 :            : 
    1570                 :      17051 :     return NULL;
    1571                 :            : }
    1572                 :            : 
    1573                 :            : static void
    1574                 :          0 : ovsdb_monitor_destroy_callback(struct ovsdb_replica *replica)
    1575                 :            : {
    1576                 :          0 :     struct ovsdb_monitor *dbmon = ovsdb_monitor_cast(replica);
    1577                 :            :     struct jsonrpc_monitor_node *jm, *next;
    1578                 :            : 
    1579                 :            :     /* Delete all front end monitors. Removing the last front
    1580                 :            :      * end monitor will also destroy the corresponding 'ovsdb_monitor'.
    1581                 :            :      * ovsdb monitor will also be destroied.  */
    1582 [ #  # ][ #  # ]:          0 :     LIST_FOR_EACH_SAFE(jm, next, node, &dbmon->jsonrpc_monitors) {
    1583                 :          0 :         ovsdb_jsonrpc_monitor_destroy(jm->jsonrpc_monitor);
    1584                 :            :     }
    1585                 :          0 : }
    1586                 :            : 
    1587                 :            : /* Add some memory usage statics for monitors into 'usage', for use with
    1588                 :            :  * memory_report().  */
    1589                 :            : void
    1590                 :         72 : ovsdb_monitor_get_memory_usage(struct simap *usage)
    1591                 :            : {
    1592                 :            :     struct ovsdb_monitor *dbmon;
    1593                 :         72 :     simap_put(usage, "monitors", hmap_count(&ovsdb_monitors));
    1594                 :            : 
    1595 [ +  + ][ -  + ]:        165 :     HMAP_FOR_EACH(dbmon, hmap_node,  &ovsdb_monitors) {
    1596                 :         93 :         simap_increase(usage, "json-caches", hmap_count(&dbmon->json_cache));
    1597                 :            :     }
    1598                 :         72 : }
    1599                 :            : 
    1600                 :            : static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
    1601                 :            :     ovsdb_monitor_commit,
    1602                 :            :     ovsdb_monitor_destroy_callback,
    1603                 :            : };

Generated by: LCOV version 1.12