forked from openvswitch/ovs
/
ovsdb-idl.c
5673 lines (5088 loc) · 201 KB
/
ovsdb-idl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
* Copyright (C) 2016 Hewlett Packard Enterprise Development LP
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include "ovsdb-idl.h"
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdlib.h>
#include "bitmap.h"
#include "coverage.h"
#include "hash.h"
#include "openvswitch/dynamic-string.h"
#include "fatal-signal.h"
#include "openvswitch/json.h"
#include "jsonrpc.h"
#include "ovsdb/ovsdb.h"
#include "ovsdb/table.h"
#include "ovsdb-data.h"
#include "ovsdb-error.h"
#include "ovsdb-idl-provider.h"
#include "ovsdb-parser.h"
#include "ovsdb-server-idl.h"
#include "ovsdb-session.h"
#include "openvswitch/poll-loop.h"
#include "openvswitch/shash.h"
#include "skiplist.h"
#include "sset.h"
#include "svec.h"
#include "util.h"
#include "uuid.h"
#include "openvswitch/vlog.h"
VLOG_DEFINE_THIS_MODULE(ovsdb_idl);
COVERAGE_DEFINE(txn_uncommitted);
COVERAGE_DEFINE(txn_unchanged);
COVERAGE_DEFINE(txn_incomplete);
COVERAGE_DEFINE(txn_aborted);
COVERAGE_DEFINE(txn_success);
COVERAGE_DEFINE(txn_try_again);
COVERAGE_DEFINE(txn_not_locked);
COVERAGE_DEFINE(txn_error);
/* An arc from one idl_row to another. When row A contains a UUID that
* references row B, this is represented by an arc from A (the source) to B
* (the destination).
*
* Arcs from a row to itself are omitted, that is, src and dst are always
* different.
*
* Arcs are never duplicated, that is, even if there are multiple references
* from A to B, there is only a single arc from A to B.
*
* Arcs are directed: an arc from A to B is the converse of an an arc from B to
* A. Both an arc and its converse may both be present, if each row refers
* to the other circularly.
*
* The source and destination row may be in the same table or in different
* tables.
*/
struct ovsdb_idl_arc {
struct ovs_list src_node; /* In src->src_arcs list. */
struct ovs_list dst_node; /* In dst->dst_arcs list. */
struct ovsdb_idl_row *src; /* Source row. */
struct ovsdb_idl_row *dst; /* Destination row. */
};
/* Connection state machine.
*
* When a JSON-RPC session connects, the IDL sends a "monitor_cond" request for
* the Database table in the _Server database and transitions to the
* IDL_S_SERVER_MONITOR_COND_REQUESTED state. If the session drops and
* reconnects, or if the IDL receives a "monitor_canceled" notification for a
* table it is monitoring, the IDL starts over again in the same way. */
#define OVSDB_IDL_STATES \
/* Waits for "get_schema" reply, then sends "monitor_cond" \
* request for the Database table in the _Server database, whose \
* details are informed by the schema, and transitions to \
* IDL_S_SERVER_MONITOR_COND_REQUESTED. */ \
OVSDB_IDL_STATE(SERVER_SCHEMA_REQUESTED) \
\
/* Waits for "monitor_cond" reply for the Database table: \
* \
* - If the reply indicates success, and the Database table has a \
* row for the IDL database: \
* \
* * If the row indicates that this is a clustered database \
* that is not connected to the cluster, closes the \
* connection. The next connection attempt has a chance at \
* picking a connected server. \
* \
* * Otherwise, sends a "monitor_cond_since" request for the IDL \
* database whose details are informed by the schema \
* (obtained from the row), and transitions to \
* IDL_S_DATA_MONITOR_COND_SINCE_REQUESTED. \
* \
* - If the reply indicates success, but the Database table does \
* not have a row for the IDL database, transitions to \
* IDL_S_ERROR. \
* \
* - If the reply indicates failure, sends a "get_schema" request \
* for the IDL database and transitions to \
* IDL_S_DATA_SCHEMA_REQUESTED. */ \
OVSDB_IDL_STATE(SERVER_MONITOR_COND_REQUESTED) \
\
/* Waits for "get_schema" reply, then sends "monitor_cond" \
* request whose details are informed by the schema, and \
* transitions to IDL_S_DATA_MONITOR_COND_REQUESTED. */ \
OVSDB_IDL_STATE(DATA_SCHEMA_REQUESTED) \
\
/* Waits for "monitor_cond_since" reply. If successful, replaces \
* the IDL contents by the data carried in the reply and \
* transitions to IDL_S_MONITORING. On failure, sends a \
* "monitor_cond" request and transitions to \
* IDL_S_DATA_MONITOR_COND_REQUESTED. */ \
OVSDB_IDL_STATE(DATA_MONITOR_COND_SINCE_REQUESTED) \
\
/* Waits for "monitor_cond" reply. If successful, replaces the \
* IDL contents by the data carried in the reply and transitions \
* to IDL_S_MONITORING. On failure, sends a "monitor" request \
* and transitions to IDL_S_DATA_MONITOR_REQUESTED. */ \
OVSDB_IDL_STATE(DATA_MONITOR_COND_REQUESTED) \
\
/* Waits for "monitor" reply. If successful, replaces the IDL \
* contents by the data carried in the reply and transitions to \
* IDL_S_MONITORING. On failure, transitions to IDL_S_ERROR. */ \
OVSDB_IDL_STATE(DATA_MONITOR_REQUESTED) \
\
/* State that processes "update", "update2" or "update3" \
* notifications for the main database (and the Database table \
* in _Server if available). \
* \
* If we're monitoring the Database table and we get notified \
* that the IDL database has been deleted, we close the \
* connection (which will restart the state machine). */ \
OVSDB_IDL_STATE(MONITORING) \
\
/* Terminal error state that indicates that nothing useful can be \
* done, for example because the database server doesn't actually \
* have the desired database. We maintain the session with the \
* database server anyway. If it starts serving the database \
* that we want, or if someone fixes and restarts the database, \
* then it will kill the session and we will automatically \
* reconnect and try again. */ \
OVSDB_IDL_STATE(ERROR) \
\
/* Terminal state that indicates we connected to a useless server \
* in a cluster, e.g. one that is partitioned from the rest of \
* the cluster. We're waiting to retry. */ \
OVSDB_IDL_STATE(RETRY)
enum ovsdb_idl_state {
#define OVSDB_IDL_STATE(NAME) IDL_S_##NAME,
OVSDB_IDL_STATES
#undef OVSDB_IDL_STATE
};
static const char *ovsdb_idl_state_to_string(enum ovsdb_idl_state);
enum ovsdb_idl_monitor_method {
OVSDB_IDL_MM_MONITOR,
OVSDB_IDL_MM_MONITOR_COND,
OVSDB_IDL_MM_MONITOR_COND_SINCE
};
enum ovsdb_idl_monitoring {
OVSDB_IDL_NOT_MONITORING, /* Database is not being monitored. */
OVSDB_IDL_MONITORING, /* Database has "monitor" outstanding. */
OVSDB_IDL_MONITORING_COND, /* Database has "monitor_cond" outstanding. */
OVSDB_IDL_MONITORING_COND_SINCE, /* Database has "monitor_cond_since"
outstanding. */
};
enum ovsdb_idl_monitor_cond_state {
OVSDB_IDL_MONITOR_COND_ACKED, /* Local conditional monitoring clauses
* have been acked by the server. */
OVSDB_IDL_MONITOR_COND_LOCAL, /* Local conditional monitoring clause
* changes have not yet been sent to the
* server. */
OVSDB_IDL_MONITOR_COND_REQUESTED, /* Local conditional monitoring clause
* changes have been sent to the server
* but have not yet been acked. */
};
struct ovsdb_idl_db {
struct ovsdb_idl *idl;
/* Data. */
const struct ovsdb_idl_class *class_;
struct shash table_by_name; /* Contains "struct ovsdb_idl_table *"s.*/
struct ovsdb_idl_table *tables; /* Array of ->class_->n_tables elements. */
struct json *monitor_id;
unsigned int change_seqno;
struct ovsdb_idl_txn *txn;
struct hmap outstanding_txns;
bool verify_write_only;
struct json *schema;
enum ovsdb_idl_monitoring monitoring;
/* Current state of the conditional monitoring clauses. */
enum ovsdb_idl_monitor_cond_state cond_state;
unsigned int cond_seqno; /* Keep track of condition clauses changes
over a single conditional monitoring session.
Reverts to zero when idl session
reconnects. */
/* Database locking. */
char *lock_name; /* Name of lock we need, NULL if none. */
bool has_lock; /* Has db server told us we have the lock? */
bool is_lock_contended; /* Has db server told us we can't get lock? */
struct json *lock_request_id; /* JSON-RPC ID of in-flight lock request. */
/* Last db txn id, used for fast resync through monitor_cond_since */
struct uuid last_id;
};
static void ovsdb_idl_db_track_clear(struct ovsdb_idl_db *);
static void ovsdb_idl_db_add_column(struct ovsdb_idl_db *,
const struct ovsdb_idl_column *);
static void ovsdb_idl_db_omit(struct ovsdb_idl_db *,
const struct ovsdb_idl_column *);
static void ovsdb_idl_db_omit_alert(struct ovsdb_idl_db *,
const struct ovsdb_idl_column *);
static unsigned int ovsdb_idl_db_set_condition(
struct ovsdb_idl_db *, const struct ovsdb_idl_table_class *,
const struct ovsdb_idl_condition *);
static void ovsdb_idl_send_schema_request(struct ovsdb_idl *,
struct ovsdb_idl_db *);
static void ovsdb_idl_send_db_change_aware(struct ovsdb_idl *);
static bool ovsdb_idl_check_server_db(struct ovsdb_idl *);
static void ovsdb_idl_send_monitor_request(struct ovsdb_idl *,
struct ovsdb_idl_db *,
enum ovsdb_idl_monitor_method);
static void ovsdb_idl_db_clear(struct ovsdb_idl_db *db);
static void ovsdb_idl_db_ack_condition(struct ovsdb_idl_db *db);
static void ovsdb_idl_db_sync_condition(struct ovsdb_idl_db *db);
static void ovsdb_idl_condition_move(struct ovsdb_idl_condition **dst,
struct ovsdb_idl_condition **src);
struct ovsdb_idl {
struct ovsdb_idl_db server;
struct ovsdb_idl_db data;
/* Session state.
*
*'state_seqno' is a snapshot of the session's sequence number as returned
* jsonrpc_session_get_seqno(session), so if it differs from the value that
* function currently returns then the session has reconnected and the
* state machine must restart. */
struct jsonrpc_session *session; /* Connection to the server. */
char *remote; /* 'session' remote name. */
enum ovsdb_idl_state state; /* Current session state. */
unsigned int state_seqno; /* See above. */
struct json *request_id; /* JSON ID for request awaiting reply. */
struct uuid cid;
uint64_t min_index;
bool leader_only;
bool shuffle_remotes;
};
static void ovsdb_idl_transition_at(struct ovsdb_idl *, enum ovsdb_idl_state,
const char *where);
#define ovsdb_idl_transition(IDL, STATE) \
ovsdb_idl_transition_at(IDL, STATE, OVS_SOURCE_LOCATOR)
static void ovsdb_idl_retry_at(struct ovsdb_idl *, const char *where);
#define ovsdb_idl_retry(IDL) ovsdb_idl_retry_at(IDL, OVS_SOURCE_LOCATOR)
struct ovsdb_idl_txn {
struct hmap_node hmap_node;
struct json *request_id;
struct ovsdb_idl_db *db;
struct hmap txn_rows;
enum ovsdb_idl_txn_status status;
char *error;
bool dry_run;
struct ds comment;
/* Increments. */
const char *inc_table;
const char *inc_column;
struct uuid inc_row;
bool inc_force;
unsigned int inc_index;
int64_t inc_new_value;
/* Inserted rows. */
struct hmap inserted_rows; /* Contains "struct ovsdb_idl_txn_insert"s. */
};
struct ovsdb_idl_txn_insert {
struct hmap_node hmap_node; /* In struct ovsdb_idl_txn's inserted_rows. */
struct uuid dummy; /* Dummy UUID used locally. */
int op_index; /* Index into transaction's operation array. */
struct uuid real; /* Real UUID used by database server. */
};
static struct vlog_rate_limit syntax_rl = VLOG_RATE_LIMIT_INIT(1, 5);
static struct vlog_rate_limit semantic_rl = VLOG_RATE_LIMIT_INIT(1, 5);
static struct vlog_rate_limit other_rl = VLOG_RATE_LIMIT_INIT(1, 5);
static void ovsdb_idl_clear(struct ovsdb_idl *);
static void ovsdb_idl_db_parse_monitor_reply(struct ovsdb_idl_db *,
const struct json *result,
enum ovsdb_idl_monitor_method method);
static bool ovsdb_idl_db_parse_update_rpc(struct ovsdb_idl_db *,
const struct jsonrpc_msg *);
static bool ovsdb_idl_handle_monitor_canceled(struct ovsdb_idl *,
struct ovsdb_idl_db *,
const struct jsonrpc_msg *);
static void ovsdb_idl_db_parse_update(struct ovsdb_idl_db *,
const struct json *table_updates,
enum ovsdb_idl_monitor_method method);
enum update_result {
OVSDB_IDL_UPDATE_DB_CHANGED,
OVSDB_IDL_UPDATE_NO_CHANGES,
OVSDB_IDL_UPDATE_INCONSISTENT,
};
static enum update_result ovsdb_idl_process_update(struct ovsdb_idl_table *,
const struct uuid *,
const struct json *old,
const struct json *new);
static enum update_result ovsdb_idl_process_update2(struct ovsdb_idl_table *,
const struct uuid *,
const char *operation,
const struct json *row);
static void ovsdb_idl_insert_row(struct ovsdb_idl_row *, const struct json *);
static void ovsdb_idl_delete_row(struct ovsdb_idl_row *);
static bool ovsdb_idl_modify_row(struct ovsdb_idl_row *, const struct json *);
static bool ovsdb_idl_modify_row_by_diff(struct ovsdb_idl_row *,
const struct json *);
static bool ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *);
static struct ovsdb_idl_row *ovsdb_idl_row_create__(
const struct ovsdb_idl_table_class *);
static struct ovsdb_idl_row *ovsdb_idl_row_create(struct ovsdb_idl_table *,
const struct uuid *);
static void ovsdb_idl_row_destroy(struct ovsdb_idl_row *);
static void ovsdb_idl_row_destroy_postprocess(struct ovsdb_idl_db *);
static void ovsdb_idl_destroy_all_map_op_lists(struct ovsdb_idl_row *);
static void ovsdb_idl_destroy_all_set_op_lists(struct ovsdb_idl_row *);
static void ovsdb_idl_row_parse(struct ovsdb_idl_row *);
static void ovsdb_idl_row_unparse(struct ovsdb_idl_row *);
static void ovsdb_idl_row_clear_old(struct ovsdb_idl_row *);
static void ovsdb_idl_row_clear_new(struct ovsdb_idl_row *);
static void ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *, bool destroy_dsts);
static void ovsdb_idl_db_txn_abort_all(struct ovsdb_idl_db *);
static void ovsdb_idl_txn_abort_all(struct ovsdb_idl *);
static bool ovsdb_idl_db_txn_process_reply(struct ovsdb_idl_db *,
const struct jsonrpc_msg *msg);
static bool ovsdb_idl_txn_extract_mutations(struct ovsdb_idl_row *,
struct json *);
static void ovsdb_idl_txn_add_map_op(struct ovsdb_idl_row *,
const struct ovsdb_idl_column *,
struct ovsdb_datum *,
enum map_op_type);
static void ovsdb_idl_txn_add_set_op(struct ovsdb_idl_row *,
const struct ovsdb_idl_column *,
struct ovsdb_datum *,
enum set_op_type);
static bool ovsdb_idl_db_process_lock_replies(struct ovsdb_idl_db *,
const struct jsonrpc_msg *);
static struct jsonrpc_msg *ovsdb_idl_db_compose_lock_request(
struct ovsdb_idl_db *);
static struct jsonrpc_msg *ovsdb_idl_db_compose_unlock_request(
struct ovsdb_idl_db *);
static void ovsdb_idl_db_parse_lock_reply(struct ovsdb_idl_db *,
const struct json *);
static bool ovsdb_idl_db_parse_lock_notify(struct ovsdb_idl_db *,
const struct json *params,
bool new_has_lock);
static struct ovsdb_idl_table *
ovsdb_idl_db_table_from_class(const struct ovsdb_idl_db *,
const struct ovsdb_idl_table_class *);
static struct ovsdb_idl_table *
ovsdb_idl_table_from_class(const struct ovsdb_idl *,
const struct ovsdb_idl_table_class *);
static bool ovsdb_idl_track_is_set(struct ovsdb_idl_table *table);
static void ovsdb_idl_send_cond_change(struct ovsdb_idl *idl);
static void ovsdb_idl_destroy_indexes(struct ovsdb_idl_table *);
static void ovsdb_idl_add_to_indexes(const struct ovsdb_idl_row *);
static void ovsdb_idl_remove_from_indexes(const struct ovsdb_idl_row *);
static int ovsdb_idl_try_commit_loop_txn(struct ovsdb_idl_loop *loop,
bool *may_need_wakeup);
static void
ovsdb_idl_db_init(struct ovsdb_idl_db *db, const struct ovsdb_idl_class *class,
struct ovsdb_idl *parent, bool monitor_everything_by_default)
{
memset(db, 0, sizeof *db);
uint8_t default_mode = (monitor_everything_by_default
? OVSDB_IDL_MONITOR | OVSDB_IDL_ALERT
: 0);
db->idl = parent;
db->class_ = class;
shash_init(&db->table_by_name);
db->tables = xmalloc(class->n_tables * sizeof *db->tables);
for (size_t i = 0; i < class->n_tables; i++) {
const struct ovsdb_idl_table_class *tc = &class->tables[i];
struct ovsdb_idl_table *table = &db->tables[i];
shash_add_assert(&db->table_by_name, tc->name, table);
table->class_ = tc;
table->modes = xmalloc(tc->n_columns);
memset(table->modes, default_mode, tc->n_columns);
table->need_table = false;
shash_init(&table->columns);
ovs_list_init(&table->indexes);
for (size_t j = 0; j < tc->n_columns; j++) {
const struct ovsdb_idl_column *column = &tc->columns[j];
shash_add_assert(&table->columns, column->name, column);
}
hmap_init(&table->rows);
ovs_list_init(&table->track_list);
table->change_seqno[OVSDB_IDL_CHANGE_INSERT]
= table->change_seqno[OVSDB_IDL_CHANGE_MODIFY]
= table->change_seqno[OVSDB_IDL_CHANGE_DELETE] = 0;
table->db = db;
table->ack_cond = NULL;
table->req_cond = NULL;
table->new_cond = xmalloc(sizeof *table->new_cond);
ovsdb_idl_condition_init(table->new_cond);
ovsdb_idl_condition_add_clause_true(table->new_cond);
}
db->monitor_id = json_array_create_2(json_string_create("monid"),
json_string_create(class->database));
hmap_init(&db->outstanding_txns);
}
/* Creates and returns a connection to database 'remote', which should be in a
* form acceptable to jsonrpc_session_open(). The connection will maintain an
* in-memory replica of the remote database whose schema is described by
* 'class'. (Ordinarily 'class' is compiled from an OVSDB schema automatically
* by ovsdb-idlc.)
*
* Passes 'retry' to jsonrpc_session_open(). See that function for
* documentation.
*
* If 'monitor_everything_by_default' is true, then everything in the remote
* database will be replicated by default. ovsdb_idl_omit() and
* ovsdb_idl_omit_alert() may be used to selectively drop some columns from
* monitoring.
*
* If 'monitor_everything_by_default' is false, then no columns or tables will
* be replicated by default. ovsdb_idl_add_column() and ovsdb_idl_add_table()
* must be used to choose some columns or tables to replicate.
*/
struct ovsdb_idl *
ovsdb_idl_create(const char *remote, const struct ovsdb_idl_class *class,
bool monitor_everything_by_default, bool retry)
{
struct ovsdb_idl *idl = ovsdb_idl_create_unconnected(
class, monitor_everything_by_default);
ovsdb_idl_set_remote(idl, remote, retry);
return idl;
}
/* Creates and returns a connection to an in-memory replica of the remote
* database whose schema is described by 'class'. (Ordinarily 'class' is
* compiled from an OVSDB schema automatically by ovsdb-idlc.)
*
* Use ovsdb_idl_set_remote() to configure the database to which to connect.
* Until a remote is configured, no data can be retrieved.
*
* If 'monitor_everything_by_default' is true, then everything in the remote
* database will be replicated by default. ovsdb_idl_omit() and
* ovsdb_idl_omit_alert() may be used to selectively drop some columns from
* monitoring.
*
* If 'monitor_everything_by_default' is false, then no columns or tables will
* be replicated by default. ovsdb_idl_add_column() and ovsdb_idl_add_table()
* must be used to choose some columns or tables to replicate.
*/
struct ovsdb_idl *
ovsdb_idl_create_unconnected(const struct ovsdb_idl_class *class,
bool monitor_everything_by_default)
{
struct ovsdb_idl *idl;
idl = xzalloc(sizeof *idl);
ovsdb_idl_db_init(&idl->server, &serverrec_idl_class, idl, true);
ovsdb_idl_db_init(&idl->data, class, idl, monitor_everything_by_default);
idl->state_seqno = UINT_MAX;
idl->request_id = NULL;
idl->leader_only = true;
idl->shuffle_remotes = true;
/* Monitor the Database table in the _Server database.
*
* We monitor only the row for 'class', or the row that has the
* desired 'cid'. */
struct ovsdb_idl_condition cond;
ovsdb_idl_condition_init(&cond);
if (!uuid_is_zero(&idl->cid)) {
serverrec_database_add_clause_cid(&cond, OVSDB_F_EQ, &idl->cid, 1);
} else {
serverrec_database_add_clause_name(&cond, OVSDB_F_EQ, class->database);
}
ovsdb_idl_db_set_condition(&idl->server, &serverrec_table_database, &cond);
ovsdb_idl_condition_destroy(&cond);
return idl;
}
/* Changes the remote and creates a new session.
*
* If 'retry' is true, the connection to the remote will automatically retry
* when it fails. If 'retry' is false, the connection is one-time. */
void
ovsdb_idl_set_remote(struct ovsdb_idl *idl, const char *remote, bool retry)
{
if (idl
&& ((remote != NULL) != (idl->remote != NULL)
|| (remote && idl->remote && strcmp(remote, idl->remote)))) {
ovs_assert(!idl->data.txn);
/* Close the old session, if any. */
if (idl->session) {
jsonrpc_session_close(idl->session);
idl->session = NULL;
free(idl->remote);
idl->remote = NULL;
}
/* Open new session, if any. */
if (remote) {
struct svec remotes = SVEC_EMPTY_INITIALIZER;
ovsdb_session_parse_remote(remote, &remotes, &idl->cid);
if (idl->shuffle_remotes) {
svec_shuffle(&remotes);
}
idl->session = jsonrpc_session_open_multiple(&remotes, retry);
svec_destroy(&remotes);
idl->state_seqno = UINT_MAX;
idl->remote = xstrdup(remote);
}
}
}
/* Set whether the order of remotes should be shuffled, when there
* are more than one remotes. The setting doesn't take effect
* until the next time when ovsdb_idl_set_remote() is called. */
void
ovsdb_idl_set_shuffle_remotes(struct ovsdb_idl *idl, bool shuffle)
{
idl->shuffle_remotes = shuffle;
}
/* Reset min_index to 0. This prevents a situation where the client
* thinks all databases have stale data, when they actually have all
* been destroyed and rebuilt from scratch.
*/
void
ovsdb_idl_reset_min_index(struct ovsdb_idl *idl)
{
idl->min_index = 0;
}
static void
ovsdb_idl_db_destroy(struct ovsdb_idl_db *db)
{
struct ovsdb_idl_condition *null_cond = NULL;
ovs_assert(!db->txn);
ovsdb_idl_db_txn_abort_all(db);
ovsdb_idl_db_clear(db);
for (size_t i = 0; i < db->class_->n_tables; i++) {
struct ovsdb_idl_table *table = &db->tables[i];
ovsdb_idl_condition_move(&table->ack_cond, &null_cond);
ovsdb_idl_condition_move(&table->req_cond, &null_cond);
ovsdb_idl_condition_move(&table->new_cond, &null_cond);
ovsdb_idl_destroy_indexes(table);
shash_destroy(&table->columns);
hmap_destroy(&table->rows);
free(table->modes);
}
shash_destroy(&db->table_by_name);
free(db->tables);
json_destroy(db->schema);
hmap_destroy(&db->outstanding_txns);
free(db->lock_name);
json_destroy(db->lock_request_id);
json_destroy(db->monitor_id);
}
/* Destroys 'idl' and all of the data structures that it manages. */
void
ovsdb_idl_destroy(struct ovsdb_idl *idl)
{
if (idl) {
ovsdb_idl_clear(idl);
jsonrpc_session_close(idl->session);
ovsdb_idl_db_destroy(&idl->server);
ovsdb_idl_db_destroy(&idl->data);
json_destroy(idl->request_id);
free(idl->remote);
free(idl);
}
}
void
ovsdb_idl_set_leader_only(struct ovsdb_idl *idl, bool leader_only)
{
idl->leader_only = leader_only;
if (leader_only && idl->server.monitoring) {
ovsdb_idl_check_server_db(idl);
}
}
static void
ovsdb_idl_db_clear(struct ovsdb_idl_db *db)
{
bool changed = false;
size_t i;
for (i = 0; i < db->class_->n_tables; i++) {
struct ovsdb_idl_table *table = &db->tables[i];
struct ovsdb_idl_row *row, *next_row;
if (hmap_is_empty(&table->rows)) {
continue;
}
changed = true;
HMAP_FOR_EACH_SAFE (row, next_row, hmap_node, &table->rows) {
struct ovsdb_idl_arc *arc, *next_arc;
if (!ovsdb_idl_row_is_orphan(row)) {
ovsdb_idl_remove_from_indexes(row);
ovsdb_idl_row_unparse(row);
}
LIST_FOR_EACH_SAFE (arc, next_arc, src_node, &row->src_arcs) {
free(arc);
}
/* No need to do anything with dst_arcs: some node has those arcs
* as forward arcs and will destroy them itself. */
ovsdb_idl_row_destroy(row);
}
}
ovsdb_idl_row_destroy_postprocess(db);
db->cond_seqno = 0;
ovsdb_idl_db_track_clear(db);
if (changed) {
db->change_seqno++;
}
}
static const char *
ovsdb_idl_state_to_string(enum ovsdb_idl_state state)
{
switch (state) {
#define OVSDB_IDL_STATE(NAME) case IDL_S_##NAME: return #NAME;
OVSDB_IDL_STATES
#undef OVSDB_IDL_STATE
default: return "<unknown>";
}
}
static void
ovsdb_idl_retry_at(struct ovsdb_idl *idl, const char *where)
{
ovsdb_idl_force_reconnect(idl);
ovsdb_idl_transition_at(idl, IDL_S_RETRY, where);
}
static void
ovsdb_idl_transition_at(struct ovsdb_idl *idl, enum ovsdb_idl_state new_state,
const char *where)
{
VLOG_DBG("%s: %s -> %s at %s",
idl->session ? jsonrpc_session_get_name(idl->session) : "void",
ovsdb_idl_state_to_string(idl->state),
ovsdb_idl_state_to_string(new_state),
where);
idl->state = new_state;
}
static void
ovsdb_idl_clear(struct ovsdb_idl *idl)
{
ovsdb_idl_db_clear(&idl->data);
}
static void
ovsdb_idl_send_request(struct ovsdb_idl *idl, struct jsonrpc_msg *request)
{
json_destroy(idl->request_id);
idl->request_id = json_clone(request->id);
if (idl->session) {
jsonrpc_session_send(idl->session, request);
}
}
static void
ovsdb_idl_restart_fsm(struct ovsdb_idl *idl)
{
/* Resync data DB table conditions to avoid missing updates due to
* conditions that were in flight or changed locally while the connection
* was down.
*/
ovsdb_idl_db_sync_condition(&idl->data);
ovsdb_idl_send_schema_request(idl, &idl->server);
ovsdb_idl_transition(idl, IDL_S_SERVER_SCHEMA_REQUESTED);
idl->data.monitoring = OVSDB_IDL_NOT_MONITORING;
idl->server.monitoring = OVSDB_IDL_NOT_MONITORING;
}
static void
ovsdb_idl_process_response(struct ovsdb_idl *idl, struct jsonrpc_msg *msg)
{
bool ok = msg->type == JSONRPC_REPLY;
if (!ok
&& idl->state != IDL_S_SERVER_SCHEMA_REQUESTED
&& idl->state != IDL_S_SERVER_MONITOR_COND_REQUESTED
&& idl->state != IDL_S_DATA_MONITOR_COND_REQUESTED
&& idl->state != IDL_S_DATA_MONITOR_COND_SINCE_REQUESTED) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
char *s = jsonrpc_msg_to_string(msg);
VLOG_INFO_RL(&rl, "%s: received unexpected %s response in "
"%s state: %s", jsonrpc_session_get_name(idl->session),
jsonrpc_msg_type_to_string(msg->type),
ovsdb_idl_state_to_string(idl->state),
s);
free(s);
ovsdb_idl_retry(idl);
return;
}
switch (idl->state) {
case IDL_S_SERVER_SCHEMA_REQUESTED:
if (ok) {
json_destroy(idl->server.schema);
idl->server.schema = json_clone(msg->result);
ovsdb_idl_send_monitor_request(idl, &idl->server,
OVSDB_IDL_MM_MONITOR_COND);
ovsdb_idl_transition(idl, IDL_S_SERVER_MONITOR_COND_REQUESTED);
} else {
ovsdb_idl_send_schema_request(idl, &idl->data);
ovsdb_idl_transition(idl, IDL_S_DATA_SCHEMA_REQUESTED);
}
break;
case IDL_S_SERVER_MONITOR_COND_REQUESTED:
if (ok) {
idl->server.monitoring = OVSDB_IDL_MONITORING_COND;
ovsdb_idl_db_parse_monitor_reply(&idl->server, msg->result,
OVSDB_IDL_MM_MONITOR_COND);
if (ovsdb_idl_check_server_db(idl)) {
ovsdb_idl_send_db_change_aware(idl);
}
} else {
ovsdb_idl_send_schema_request(idl, &idl->data);
ovsdb_idl_transition(idl, IDL_S_DATA_SCHEMA_REQUESTED);
}
break;
case IDL_S_DATA_SCHEMA_REQUESTED:
json_destroy(idl->data.schema);
idl->data.schema = json_clone(msg->result);
ovsdb_idl_send_monitor_request(idl, &idl->data,
OVSDB_IDL_MM_MONITOR_COND);
ovsdb_idl_transition(idl, IDL_S_DATA_MONITOR_COND_REQUESTED);
break;
case IDL_S_DATA_MONITOR_COND_SINCE_REQUESTED:
if (!ok) {
/* "monitor_cond_since" not supported. Try "monitor_cond". */
ovsdb_idl_send_monitor_request(idl, &idl->data,
OVSDB_IDL_MM_MONITOR_COND);
ovsdb_idl_transition(idl, IDL_S_DATA_MONITOR_COND_REQUESTED);
} else {
idl->data.monitoring = OVSDB_IDL_MONITORING_COND_SINCE;
ovsdb_idl_transition(idl, IDL_S_MONITORING);
ovsdb_idl_db_parse_monitor_reply(&idl->data, msg->result,
OVSDB_IDL_MM_MONITOR_COND_SINCE);
}
break;
case IDL_S_DATA_MONITOR_COND_REQUESTED:
if (!ok) {
/* "monitor_cond" not supported. Try "monitor". */
ovsdb_idl_send_monitor_request(idl, &idl->data,
OVSDB_IDL_MM_MONITOR);
ovsdb_idl_transition(idl, IDL_S_DATA_MONITOR_REQUESTED);
} else {
idl->data.monitoring = OVSDB_IDL_MONITORING_COND;
ovsdb_idl_transition(idl, IDL_S_MONITORING);
ovsdb_idl_db_parse_monitor_reply(&idl->data, msg->result,
OVSDB_IDL_MM_MONITOR_COND);
}
break;
case IDL_S_DATA_MONITOR_REQUESTED:
idl->data.monitoring = OVSDB_IDL_MONITORING;
ovsdb_idl_transition(idl, IDL_S_MONITORING);
ovsdb_idl_db_parse_monitor_reply(&idl->data, msg->result,
OVSDB_IDL_MM_MONITOR);
idl->data.change_seqno++;
ovsdb_idl_clear(idl);
ovsdb_idl_db_parse_update(&idl->data, msg->result,
OVSDB_IDL_MM_MONITOR);
break;
case IDL_S_MONITORING:
/* We don't normally have a request outstanding in this state. If we
* do, it's a "monitor_cond_change", which means that the conditional
* monitor clauses were updated.
*
* Mark the last requested conditions as acked and if further
* condition changes were pending, send them now. */
ovsdb_idl_db_ack_condition(&idl->data);
ovsdb_idl_send_cond_change(idl);
idl->data.cond_seqno++;
break;
case IDL_S_ERROR:
case IDL_S_RETRY:
/* Nothing to do in this state. */
break;
default:
OVS_NOT_REACHED();
}
}
static void
ovsdb_idl_process_msg(struct ovsdb_idl *idl, struct jsonrpc_msg *msg)
{
bool is_response = (msg->type == JSONRPC_REPLY ||
msg->type == JSONRPC_ERROR);
/* Process a reply to an outstanding request. */
if (is_response
&& idl->request_id && json_equal(idl->request_id, msg->id)) {
json_destroy(idl->request_id);
idl->request_id = NULL;
ovsdb_idl_process_response(idl, msg);
return;
}
/* Process database contents updates. */
if (ovsdb_idl_db_parse_update_rpc(&idl->data, msg)) {
return;
}
if (idl->server.monitoring
&& ovsdb_idl_db_parse_update_rpc(&idl->server, msg)) {
ovsdb_idl_check_server_db(idl);
return;
}
if (ovsdb_idl_handle_monitor_canceled(idl, &idl->data, msg)
|| (idl->server.monitoring
&& ovsdb_idl_handle_monitor_canceled(idl, &idl->server, msg))) {
return;
}
/* Process "lock" replies and related notifications. */
if (ovsdb_idl_db_process_lock_replies(&idl->data, msg)) {
return;
}
/* Process response to a database transaction we submitted. */
if (is_response && ovsdb_idl_db_txn_process_reply(&idl->data, msg)) {
return;
}
/* Unknown message. Log at a low level because this can happen if
* ovsdb_idl_txn_destroy() is called to destroy a transaction
* before we receive the reply.
*
* (We could sort those out from other kinds of unknown messages by
* using distinctive IDs for transactions, if it seems valuable to
* do so, and then it would be possible to use different log
* levels. XXX?) */
char *s = jsonrpc_msg_to_string(msg);
VLOG_DBG("%s: received unexpected %s message: %s",
jsonrpc_session_get_name(idl->session),
jsonrpc_msg_type_to_string(msg->type), s);
free(s);
}
/* Processes a batch of messages from the database server on 'idl'. This may
* cause the IDL's contents to change. The client may check for that with
* ovsdb_idl_get_seqno(). */
void
ovsdb_idl_run(struct ovsdb_idl *idl)
{
if (!idl->session) {
ovsdb_idl_txn_abort_all(idl);
return;
}
int i;
ovs_assert(!idl->data.txn);
ovsdb_idl_send_cond_change(idl);
jsonrpc_session_run(idl->session);
for (i = 0; jsonrpc_session_is_connected(idl->session) && i < 50; i++) {
struct jsonrpc_msg *msg;
unsigned int seqno;
seqno = jsonrpc_session_get_seqno(idl->session);
if (idl->state_seqno != seqno) {
idl->state_seqno = seqno;
ovsdb_idl_txn_abort_all(idl);
ovsdb_idl_restart_fsm(idl);
if (idl->data.lock_name) {
jsonrpc_session_send(
idl->session,
ovsdb_idl_db_compose_lock_request(&idl->data));
}
}
msg = jsonrpc_session_recv(idl->session);
if (!msg) {
break;
}
ovsdb_idl_process_msg(idl, msg);
jsonrpc_msg_destroy(msg);
}
ovsdb_idl_row_destroy_postprocess(&idl->data);
}
/* Arranges for poll_block() to wake up when ovsdb_idl_run() has something to
* do or when activity occurs on a transaction on 'idl'. */
void
ovsdb_idl_wait(struct ovsdb_idl *idl)
{
if (!idl->session) {
return;
}
jsonrpc_session_wait(idl->session);
jsonrpc_session_recv_wait(idl->session);
}
/* Returns a "sequence number" that represents the state of 'idl'. When
* ovsdb_idl_run() changes the database, the sequence number changes. The
* initial fetch of the entire contents of the remote database is considered to
* be one kind of change. Successfully acquiring a lock, if one has been
* configured with ovsdb_idl_set_lock(), is also considered to be a change.
*
* As long as the sequence number does not change, the client may continue to
* use any data structures it obtains from 'idl'. But when it changes, the
* client must not access any of these data structures again, because they
* could have freed or reused for other purposes.
*
* The sequence number can occasionally change even if the database does not.
* This happens if the connection to the database drops and reconnects, which
* causes the database contents to be reloaded even if they didn't change. (It
* could also happen if the database server sends out a "change" that reflects
* what the IDL already thought was in the database. The database server is
* not supposed to do that, but bugs could in theory cause it to do so.) */
unsigned int
ovsdb_idl_get_seqno(const struct ovsdb_idl *idl)
{
return idl->data.change_seqno;
}
/* Returns a "sequence number" that represents the number of conditional
* monitoring updates successfully received by the OVSDB server of an IDL
* connection.
*
* ovsdb_idl_set_condition() sets a new condition that is different from