forked from ovn-org/ovn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathovn-ic-sbctl.c
1025 lines (872 loc) · 31 KB
/
ovn-ic-sbctl.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) 2020 eBay Inc.
*
* 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 <ctype.h>
#include <getopt.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdlib.h>
#include "command-line.h"
#include "compiler.h"
#include "db-ctl-base.h"
#include "ovn-dbctl.h"
#include "dirs.h"
#include "fatal-signal.h"
#include "openvswitch/dynamic-string.h"
#include "openvswitch/json.h"
#include "openvswitch/shash.h"
#include "openvswitch/vconn.h"
#include "openvswitch/vlog.h"
#include "lib/ovn-ic-sb-idl.h"
#include "lib/ovn-util.h"
#include "openvswitch/poll-loop.h"
#include "process.h"
#include "sset.h"
#include "stream-ssl.h"
#include "stream.h"
#include "table.h"
#include "util.h"
#include "svec.h"
VLOG_DEFINE_THIS_MODULE(ic_sbctl);
struct ic_sbctl_context;
/* --db: The database server to contact. */
static const char *db;
/* --oneline: Write each command's output as a single line? */
static bool oneline;
/* --dry-run: Do not commit any changes. */
static bool dry_run;
/* --timeout: Time to wait for a connection to 'db'. */
static unsigned int timeout;
/* Format for table output. */
static struct table_style table_style = TABLE_STYLE_DEFAULT;
/* The IDL we're using and the current transaction, if any.
* This is for use by ic_sbctl_exit() only, to allow it to clean up.
* Other code should use its context arguments. */
static struct ovsdb_idl *the_idl;
static struct ovsdb_idl_txn *the_idl_txn;
OVS_NO_RETURN static void ic_sbctl_exit(int status);
/* --leader-only, --no-leader-only: Only accept the leader in a cluster. */
static int leader_only = true;
static void ic_sbctl_cmd_init(void);
OVS_NO_RETURN static void usage(void);
static void parse_options(int argc, char *argv[], struct shash *local_options);
static void run_prerequisites(struct ctl_command[], size_t n_commands,
struct ovsdb_idl *);
static bool do_ic_sbctl(const char *args, struct ctl_command *, size_t n,
struct ovsdb_idl *);
int
main(int argc, char *argv[])
{
struct ovsdb_idl *idl;
struct ctl_command *commands;
struct shash local_options;
unsigned int seqno;
size_t n_commands;
ovn_set_program_name(argv[0]);
fatal_ignore_sigpipe();
vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
vlog_set_levels_from_string_assert("reconnect:warn");
ic_sbctl_cmd_init();
/* Parse command line. */
char *args = process_escape_args(argv);
shash_init(&local_options);
parse_options(argc, argv, &local_options);
char *error = ctl_parse_commands(argc - optind, argv + optind,
&local_options, &commands, &n_commands);
if (error) {
ctl_fatal("%s", error);
}
VLOG(ctl_might_write_to_db(commands, n_commands) ? VLL_INFO : VLL_DBG,
"Called as %s", args);
ctl_timeout_setup(timeout);
/* Initialize IDL. */
idl = the_idl = ovsdb_idl_create_unconnected(&icsbrec_idl_class, true);
ovsdb_idl_set_remote(idl, db, false);
ovsdb_idl_set_db_change_aware(idl, false);
ovsdb_idl_set_leader_only(idl, leader_only);
/* Set reasonable high probe interval. */
set_idl_probe_interval(idl, db, DEFAULT_UTILS_PROBE_INTERVAL_MSEC);
run_prerequisites(commands, n_commands, idl);
/* Execute the commands.
*
* 'seqno' is the database sequence number for which we last tried to
* execute our transaction. There's no point in trying to commit more than
* once for any given sequence number, because if the transaction fails
* it's because the database changed and we need to obtain an up-to-date
* view of the database before we try the transaction again. */
seqno = ovsdb_idl_get_seqno(idl);
for (;;) {
ovsdb_idl_run(idl);
if (!ovsdb_idl_is_alive(idl)) {
int retval = ovsdb_idl_get_last_error(idl);
ctl_fatal("%s: database connection failed (%s)",
db, ovs_retval_to_string(retval));
}
if (seqno != ovsdb_idl_get_seqno(idl)) {
seqno = ovsdb_idl_get_seqno(idl);
if (do_ic_sbctl(args, commands, n_commands, idl)) {
free(args);
exit(EXIT_SUCCESS);
}
}
if (seqno == ovsdb_idl_get_seqno(idl)) {
ovsdb_idl_wait(idl);
poll_block();
}
}
}
static void
parse_options(int argc, char *argv[], struct shash *local_options)
{
enum {
OPT_DB = UCHAR_MAX + 1,
OPT_ONELINE,
OPT_NO_SYSLOG,
OPT_DRY_RUN,
OPT_LOCAL,
OPT_COMMANDS,
OPT_OPTIONS,
OPT_BOOTSTRAP_CA_CERT,
VLOG_OPTION_ENUMS,
TABLE_OPTION_ENUMS,
SSL_OPTION_ENUMS,
};
static const struct option global_long_options[] = {
{"db", required_argument, NULL, OPT_DB},
{"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
{"dry-run", no_argument, NULL, OPT_DRY_RUN},
{"oneline", no_argument, NULL, OPT_ONELINE},
{"timeout", required_argument, NULL, 't'},
{"help", no_argument, NULL, 'h'},
{"commands", no_argument, NULL, OPT_COMMANDS},
{"options", no_argument, NULL, OPT_OPTIONS},
{"leader-only", no_argument, &leader_only, true},
{"no-leader-only", no_argument, &leader_only, false},
{"version", no_argument, NULL, 'V'},
VLOG_LONG_OPTIONS,
STREAM_SSL_LONG_OPTIONS,
{"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
TABLE_LONG_OPTIONS,
{NULL, 0, NULL, 0},
};
const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
char *tmp, *short_options;
struct option *options;
size_t allocated_options;
size_t n_options;
size_t i;
tmp = ovs_cmdl_long_options_to_short_options(global_long_options);
short_options = xasprintf("+%s", tmp);
free(tmp);
/* We want to parse both global and command-specific options here, but
* getopt_long() isn't too convenient for the job. We copy our global
* options into a dynamic array, then append all of the command-specific
* options. */
options = xmemdup(global_long_options, sizeof global_long_options);
allocated_options = ARRAY_SIZE(global_long_options);
n_options = n_global_long_options;
ctl_add_cmd_options(&options, &n_options, &allocated_options, OPT_LOCAL);
for (;;) {
int idx;
int c;
c = getopt_long(argc, argv, short_options, options, &idx);
if (c == -1) {
break;
}
switch (c) {
case OPT_DB:
db = optarg;
break;
case OPT_ONELINE:
oneline = true;
break;
case OPT_NO_SYSLOG:
vlog_set_levels(&this_module, VLF_SYSLOG, VLL_WARN);
break;
case OPT_DRY_RUN:
dry_run = true;
break;
case OPT_LOCAL:
if (shash_find(local_options, options[idx].name)) {
ctl_fatal("'%s' option specified multiple times",
options[idx].name);
}
shash_add_nocopy(local_options,
xasprintf("--%s", options[idx].name),
nullable_xstrdup(optarg));
break;
case 'h':
usage();
case OPT_COMMANDS:
ctl_print_commands();
/* fall through */
case OPT_OPTIONS:
ctl_print_options(global_long_options);
/* fall through */
case 'V':
ovn_print_version(0, 0);
printf("DB Schema %s\n", icsbrec_get_db_version());
exit(EXIT_SUCCESS);
case 't':
if (!str_to_uint(optarg, 10, &timeout) || !timeout) {
ctl_fatal("value %s on -t or --timeout is invalid", optarg);
}
break;
VLOG_OPTION_HANDLERS
TABLE_OPTION_HANDLERS(&table_style)
STREAM_SSL_OPTION_HANDLERS
case OPT_BOOTSTRAP_CA_CERT:
stream_ssl_set_ca_cert_file(optarg, true);
break;
case '?':
exit(EXIT_FAILURE);
default:
ovs_abort(0, "Internal error when parsing option %d.", c);
case 0:
break;
}
}
free(short_options);
if (!db) {
db = default_ic_sb_db();
}
for (i = n_global_long_options; options[i].name; i++) {
free(CONST_CAST(char *, options[i].name));
}
free(options);
}
static void
usage(void)
{
printf("\
%s: OVN interconnection southbound DB management utility\n\
\n\
usage: %s [OPTIONS] COMMAND [ARG...]\n\
\n\
General commands:\n\
show print overview of database contents\n\
\n\
Connection commands:\n\
get-connection print the connections\n\
del-connection delete the connections\n\
[--inactivity-probe=MSECS]\n\
set-connection TARGET... set the list of connections to TARGET...\n\
\n\
SSL commands:\n\
get-ssl print the SSL configuration\n\
del-ssl delete the SSL configuration\n\
set-ssl PRIV-KEY CERT CA-CERT [SSL-PROTOS [SSL-CIPHERS]] \
set the SSL configuration\n\
\n\
%s\
%s\
\n\
Options:\n\
--db=DATABASE connect to DATABASE\n\
(default: %s)\n\
--no-leader-only accept any cluster member, not just the leader\n\
-t, --timeout=SECS wait at most SECS seconds\n\
--dry-run do not commit changes to database\n\
--oneline print exactly one line of output per command\n",
program_name, program_name, ctl_get_db_cmd_usage(),
ctl_list_db_tables_usage(), default_ic_sb_db());
table_usage();
vlog_usage();
printf("\
--no-syslog equivalent to --verbose=ic_sbctl:syslog:warn\n");
printf("\n\
Other options:\n\
-h, --help display this help message\n\
-V, --version display version information\n");
stream_usage("database", true, true, true);
exit(EXIT_SUCCESS);
}
/* ovs-ic_sbctl specific context. Inherits the 'struct ctl_context' as base.
* Now empty, just keep the framework for future additions. */
struct ic_sbctl_context {
struct ctl_context base;
/* A cache of the contents of the database.
*
* A command that needs to use any of this information must first call
* ic_sbctl_context_populate_cache(). A command that changes anything that
* could invalidate the cache must either call
* ic_sbctl_context_invalidate_cache() or manually update the cache to
* maintain its correctness. */
bool cache_valid;
};
static void
ic_sbctl_init(struct ctl_context *ctx OVS_UNUSED)
{
}
static char *
az_by_name_or_uuid(struct ctl_context *ctx, const char *id, bool must_exist,
const struct icsbrec_availability_zone **az_p)
{
const struct icsbrec_availability_zone *az = NULL;
*az_p = NULL;
struct uuid az_uuid;
bool is_uuid = uuid_from_string(&az_uuid, id);
if (is_uuid) {
az = icsbrec_availability_zone_get_for_uuid(ctx->idl, &az_uuid);
}
if (!az) {
const struct icsbrec_availability_zone *iter;
ICSBREC_AVAILABILITY_ZONE_FOR_EACH (iter, ctx->idl) {
if (!strcmp(iter->name, id)) {
az = iter;
break;
}
}
}
if (!az && must_exist) {
return xasprintf("%s: availability zone %s not found",
id, is_uuid ? "UUID" : "name");
}
*az_p = az;
return NULL;
}
/* Data structures for the show command, which constructs the hierarchy
* of availability_zone:gateway:port_binding. */
struct gw_data {
const struct icsbrec_gateway *isb_gw;
/* With node type struct icsbrec_port_binding.
* We don't need to search port-binding, but using shash makes
* sorting convenient. */
struct shash pbs;
};
struct az_data {
const struct icsbrec_availability_zone *isb_az;
/* With node type struct gw_data */
struct shash gws;
};
static void
init_az_info(struct ctl_context *ctx, struct shash *azs)
{
const struct icsbrec_availability_zone *az;
ICSBREC_AVAILABILITY_ZONE_FOR_EACH (az, ctx->idl) {
struct az_data *az_data = xmalloc(sizeof *az_data);
shash_init(&az_data->gws);
az_data->isb_az = az;
shash_add(azs, az->name, az_data);
}
const struct icsbrec_gateway *gw;
ICSBREC_GATEWAY_FOR_EACH (gw, ctx->idl) {
struct az_data *az_data = shash_find_data(azs,
gw->availability_zone->name);
ovs_assert(az_data);
struct gw_data *gw_data = xmalloc(sizeof *gw_data);
gw_data->isb_gw = gw;
shash_init(&gw_data->pbs);
shash_add(&az_data->gws, gw->name, gw_data);
}
const struct icsbrec_port_binding *pb;
ICSBREC_PORT_BINDING_FOR_EACH (pb, ctx->idl) {
struct az_data *az_data = shash_find_data(azs,
pb->availability_zone->name);
ovs_assert(az_data);
struct gw_data *gw_data = shash_find_data(&az_data->gws, pb->gateway);
if (!gw_data) {
continue;
}
shash_add(&gw_data->pbs, pb->logical_port, pb);
}
}
static void
destroy_az_info(struct shash *azs)
{
struct shash_node *az_node;
SHASH_FOR_EACH (az_node, azs) {
struct az_data *az_data = az_node->data;
struct shash_node *gw_node;
SHASH_FOR_EACH (gw_node, &az_data->gws) {
struct gw_data *gw_data = gw_node->data;
shash_destroy(&gw_data->pbs);
free(gw_data);
}
shash_destroy(&az_data->gws);
free(az_data);
}
shash_destroy(azs);
}
static void
print_az(struct az_data *az, struct ds *s)
{
ds_put_format(s, "availability-zone %s\n", az->isb_az->name);
const struct shash_node **nodes = shash_sort(&az->gws);
for (int g = 0; g < shash_count(&az->gws); g++) {
struct gw_data *gw = nodes[g]->data;
ds_put_format(s, " gateway %s\n", gw->isb_gw->name);
if (gw->isb_gw->hostname[0]) {
ds_put_format(s, " hostname: %s\n", gw->isb_gw->hostname);
}
for (int i = 0; i < gw->isb_gw->n_encaps; i++) {
ds_put_format(s, " type: %s\n",
gw->isb_gw->encaps[i]->type);
ds_put_format(s, " ip: %s\n",
gw->isb_gw->encaps[i]->ip);
}
const struct shash_node **pb_nodes = shash_sort(&gw->pbs);
for (int p = 0; p < shash_count(&gw->pbs); p++) {
const struct icsbrec_port_binding *pb = pb_nodes[p]->data;
ds_put_format(s, " port %s\n",
pb->logical_port);
ds_put_format(s, " transit switch: %s\n",
pb->transit_switch);
ds_put_format(s, " address: [\"%s\"]\n",
pb->address);
}
free(pb_nodes);
}
free(nodes);
}
static void
ic_sbctl_show(struct ctl_context *ctx)
{
/* Availability zones with the gateways and port-bindings data. Node
* type is struct az_data. */
struct shash azs = SHASH_INITIALIZER(&azs);
init_az_info(ctx, &azs);
const struct icsbrec_availability_zone *az;
if (ctx->argc == 2) {
char *error = az_by_name_or_uuid(ctx, ctx->argv[1], false, &az);
if (error) {
ctx->error = error;
goto error;
}
if (az) {
struct az_data *az_data = shash_find_data(&azs, az->name);
ovs_assert(az_data);
print_az(az_data, &ctx->output);
}
} else {
const struct shash_node **nodes = shash_sort(&azs);
for (int i = 0; i < shash_count(&azs); i++) {
struct az_data *az_data = nodes[i]->data;
print_az(az_data, &ctx->output);
}
free(nodes);
}
error:
destroy_az_info(&azs);
}
static void
verify_connections(struct ctl_context *ctx)
{
const struct icsbrec_ic_sb_global *ic_sb_global =
icsbrec_ic_sb_global_first(ctx->idl);
const struct icsbrec_connection *conn;
icsbrec_ic_sb_global_verify_connections(ic_sb_global);
ICSBREC_CONNECTION_FOR_EACH (conn, ctx->idl) {
icsbrec_connection_verify_target(conn);
}
}
static void
pre_connection(struct ctl_context *ctx)
{
ovsdb_idl_add_column(ctx->idl, &icsbrec_ic_sb_global_col_connections);
ovsdb_idl_add_column(ctx->idl, &icsbrec_connection_col_target);
ovsdb_idl_add_column(ctx->idl, &icsbrec_connection_col_inactivity_probe);
}
static void
cmd_get_connection(struct ctl_context *ctx)
{
const struct icsbrec_connection *conn;
struct svec targets;
size_t i;
verify_connections(ctx);
/* Print the targets in sorted order for reproducibility. */
svec_init(&targets);
ICSBREC_CONNECTION_FOR_EACH (conn, ctx->idl) {
svec_add(&targets, conn->target);
}
svec_sort_unique(&targets);
for (i = 0; i < targets.n; i++) {
ds_put_format(&ctx->output, "%s\n", targets.names[i]);
}
svec_destroy(&targets);
}
static void
delete_connections(struct ctl_context *ctx)
{
const struct icsbrec_ic_sb_global *ic_sb_global =
icsbrec_ic_sb_global_first(ctx->idl);
const struct icsbrec_connection *conn;
/* Delete Manager rows pointed to by 'connection_options' column. */
ICSBREC_CONNECTION_FOR_EACH_SAFE (conn, ctx->idl) {
icsbrec_connection_delete(conn);
}
/* Delete 'Manager' row refs in 'manager_options' column. */
icsbrec_ic_sb_global_set_connections(ic_sb_global, NULL, 0);
}
static void
cmd_del_connection(struct ctl_context *ctx)
{
verify_connections(ctx);
delete_connections(ctx);
}
static void
insert_connections(struct ctl_context *ctx, char *targets[], size_t n)
{
const struct icsbrec_ic_sb_global *ic_sb_global =
icsbrec_ic_sb_global_first(ctx->idl);
struct icsbrec_connection **connections;
size_t i, conns = 0;
const char *inactivity_probe = shash_find_data(&ctx->options,
"--inactivity-probe");
/* Insert each connection in a new row in Connection table. */
connections = xmalloc(n * sizeof *connections);
for (i = 0; i < n; i++) {
if (stream_verify_name(targets[i]) &&
pstream_verify_name(targets[i])) {
VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
}
connections[conns] = icsbrec_connection_insert(ctx->txn);
icsbrec_connection_set_target(connections[conns], targets[i]);
if (inactivity_probe) {
int64_t msecs = atoll(inactivity_probe);
icsbrec_connection_set_inactivity_probe(connections[conns],
&msecs, 1);
}
conns++;
}
/* Store uuids of new connection rows in 'connection' column. */
icsbrec_ic_sb_global_set_connections(ic_sb_global, connections, conns);
free(connections);
}
static void
cmd_set_connection(struct ctl_context *ctx)
{
const size_t n = ctx->argc - 1;
verify_connections(ctx);
delete_connections(ctx);
insert_connections(ctx, &ctx->argv[1], n);
}
static void
pre_cmd_get_ssl(struct ctl_context *ctx)
{
ovsdb_idl_add_column(ctx->idl, &icsbrec_ic_sb_global_col_ssl);
ovsdb_idl_add_column(ctx->idl, &icsbrec_ssl_col_private_key);
ovsdb_idl_add_column(ctx->idl, &icsbrec_ssl_col_certificate);
ovsdb_idl_add_column(ctx->idl, &icsbrec_ssl_col_ca_cert);
ovsdb_idl_add_column(ctx->idl, &icsbrec_ssl_col_bootstrap_ca_cert);
}
static void
cmd_get_ssl(struct ctl_context *ctx)
{
const struct icsbrec_ic_sb_global *ic_sb_global =
icsbrec_ic_sb_global_first(ctx->idl);
const struct icsbrec_ssl *ssl = icsbrec_ssl_first(ctx->idl);
icsbrec_ic_sb_global_verify_ssl(ic_sb_global);
if (ssl) {
icsbrec_ssl_verify_private_key(ssl);
icsbrec_ssl_verify_certificate(ssl);
icsbrec_ssl_verify_ca_cert(ssl);
icsbrec_ssl_verify_bootstrap_ca_cert(ssl);
ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
ds_put_format(&ctx->output, "Bootstrap: %s\n",
ssl->bootstrap_ca_cert ? "true" : "false");
}
}
static void
pre_cmd_del_ssl(struct ctl_context *ctx)
{
ovsdb_idl_add_column(ctx->idl, &icsbrec_ic_sb_global_col_ssl);
}
static void
cmd_del_ssl(struct ctl_context *ctx)
{
const struct icsbrec_ic_sb_global *ic_sb_global =
icsbrec_ic_sb_global_first(ctx->idl);
const struct icsbrec_ssl *ssl = icsbrec_ssl_first(ctx->idl);
if (ssl) {
icsbrec_ic_sb_global_verify_ssl(ic_sb_global);
icsbrec_ssl_delete(ssl);
icsbrec_ic_sb_global_set_ssl(ic_sb_global, NULL);
}
}
static void
pre_cmd_set_ssl(struct ctl_context *ctx)
{
ovsdb_idl_add_column(ctx->idl, &icsbrec_ic_sb_global_col_ssl);
}
static void
cmd_set_ssl(struct ctl_context *ctx)
{
bool bootstrap = shash_find(&ctx->options, "--bootstrap");
const struct icsbrec_ic_sb_global *ic_sb_global =
icsbrec_ic_sb_global_first(ctx->idl);
const struct icsbrec_ssl *ssl = icsbrec_ssl_first(ctx->idl);
icsbrec_ic_sb_global_verify_ssl(ic_sb_global);
if (ssl) {
icsbrec_ssl_delete(ssl);
}
ssl = icsbrec_ssl_insert(ctx->txn);
icsbrec_ssl_set_private_key(ssl, ctx->argv[1]);
icsbrec_ssl_set_certificate(ssl, ctx->argv[2]);
icsbrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
icsbrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
if (ctx->argc == 5) {
icsbrec_ssl_set_ssl_protocols(ssl, ctx->argv[4]);
} else if (ctx->argc == 6) {
icsbrec_ssl_set_ssl_protocols(ssl, ctx->argv[4]);
icsbrec_ssl_set_ssl_ciphers(ssl, ctx->argv[5]);
}
icsbrec_ic_sb_global_set_ssl(ic_sb_global, ssl);
}
static const struct ctl_table_class tables[ICSBREC_N_TABLES] = {
[ICSBREC_TABLE_AVAILABILITY_ZONE].row_ids[0] =
{&icsbrec_availability_zone_col_name, NULL, NULL},
[ICSBREC_TABLE_GATEWAY].row_ids[0] =
{&icsbrec_gateway_col_name, NULL, NULL},
[ICSBREC_TABLE_PORT_BINDING].row_ids[0] =
{&icsbrec_port_binding_col_logical_port, NULL, NULL},
[ICSBREC_TABLE_DATAPATH_BINDING].row_ids[0] =
{&icsbrec_datapath_binding_col_transit_switch, NULL, NULL},
};
static void
ic_sbctl_context_init_command(struct ic_sbctl_context *ic_sbctl_ctx,
struct ctl_command *command, bool last_command)
{
ctl_context_init_command(&ic_sbctl_ctx->base, command, last_command);
}
static void
ic_sbctl_context_init(struct ic_sbctl_context *ic_sbctl_ctx,
struct ctl_command *command, struct ovsdb_idl *idl,
struct ovsdb_idl_txn *txn,
struct ovsdb_symbol_table *symtab)
{
ctl_context_init(&ic_sbctl_ctx->base, command, idl, txn, symtab, NULL);
ic_sbctl_ctx->cache_valid = false;
}
static void
ic_sbctl_context_done_command(struct ic_sbctl_context *ic_sbctl_ctx,
struct ctl_command *command)
{
ctl_context_done_command(&ic_sbctl_ctx->base, command);
}
static void
ic_sbctl_context_done(struct ic_sbctl_context *ic_sbctl_ctx,
struct ctl_command *command)
{
ctl_context_done(&ic_sbctl_ctx->base, command);
}
static void
run_prerequisites(struct ctl_command *commands, size_t n_commands,
struct ovsdb_idl *idl)
{
ovsdb_idl_add_table(idl, &icsbrec_table_ic_sb_global);
for (struct ctl_command *c = commands; c < &commands[n_commands]; c++) {
if (c->syntax->prerequisites) {
struct ic_sbctl_context ic_sbctl_ctx;
ds_init(&c->output);
c->table = NULL;
ic_sbctl_context_init(&ic_sbctl_ctx, c, idl, NULL, NULL);
(c->syntax->prerequisites)(&ic_sbctl_ctx.base);
if (ic_sbctl_ctx.base.error) {
ctl_fatal("%s", ic_sbctl_ctx.base.error);
}
ic_sbctl_context_done(&ic_sbctl_ctx, c);
ovs_assert(!c->output.string);
ovs_assert(!c->table);
}
}
}
static bool
do_ic_sbctl(const char *args, struct ctl_command *commands, size_t n_commands,
struct ovsdb_idl *idl)
{
struct ovsdb_idl_txn *txn;
enum ovsdb_idl_txn_status status;
struct ovsdb_symbol_table *symtab;
struct ic_sbctl_context ic_sbctl_ctx;
struct ctl_command *c;
struct shash_node *node;
txn = the_idl_txn = ovsdb_idl_txn_create(idl);
if (dry_run) {
ovsdb_idl_txn_set_dry_run(txn);
}
ovsdb_idl_txn_add_comment(txn, "ovs-ic_sbctl: %s", args);
const struct icsbrec_ic_sb_global *isb = icsbrec_ic_sb_global_first(idl);
if (!isb) {
/* XXX add verification that table is empty */
icsbrec_ic_sb_global_insert(txn);
}
symtab = ovsdb_symbol_table_create();
for (c = commands; c < &commands[n_commands]; c++) {
ds_init(&c->output);
c->table = NULL;
}
ic_sbctl_context_init(&ic_sbctl_ctx, NULL, idl, txn, symtab);
for (c = commands; c < &commands[n_commands]; c++) {
ic_sbctl_context_init_command(&ic_sbctl_ctx, c,
c == &commands[n_commands - 1]);
if (c->syntax->run) {
(c->syntax->run)(&ic_sbctl_ctx.base);
}
if (ic_sbctl_ctx.base.error) {
ctl_fatal("%s", ic_sbctl_ctx.base.error);
}
ic_sbctl_context_done_command(&ic_sbctl_ctx, c);
if (ic_sbctl_ctx.base.try_again) {
ic_sbctl_context_done(&ic_sbctl_ctx, NULL);
goto try_again;
}
}
ic_sbctl_context_done(&ic_sbctl_ctx, NULL);
SHASH_FOR_EACH (node, &symtab->sh) {
struct ovsdb_symbol *symbol = node->data;
if (!symbol->created) {
ctl_fatal("row id \"%s\" is referenced but never created (e.g. "
"with \"-- --id=%s create ...\")",
node->name, node->name);
}
if (!symbol->strong_ref) {
if (!symbol->weak_ref) {
VLOG_WARN("row id \"%s\" was created but no reference to it "
"was inserted, so it will not actually appear in "
"the database", node->name);
} else {
VLOG_WARN("row id \"%s\" was created but only a weak "
"reference to it was inserted, so it will not "
"actually appear in the database", node->name);
}
}
}
status = ovsdb_idl_txn_commit_block(txn);
if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
for (c = commands; c < &commands[n_commands]; c++) {
if (c->syntax->postprocess) {
ic_sbctl_context_init(&ic_sbctl_ctx, c, idl, txn, symtab);
(c->syntax->postprocess)(&ic_sbctl_ctx.base);
if (ic_sbctl_ctx.base.error) {
ctl_fatal("%s", ic_sbctl_ctx.base.error);
}
ic_sbctl_context_done(&ic_sbctl_ctx, c);
}
}
}
switch (status) {
case TXN_UNCOMMITTED:
case TXN_INCOMPLETE:
OVS_NOT_REACHED();
case TXN_ABORTED:
/* Should not happen--we never call ovsdb_idl_txn_abort(). */
ctl_fatal("transaction aborted");
case TXN_UNCHANGED:
case TXN_SUCCESS:
break;
case TXN_TRY_AGAIN:
goto try_again;
case TXN_ERROR:
ctl_fatal("transaction error: %s", ovsdb_idl_txn_get_error(txn));
case TXN_NOT_LOCKED:
/* Should not happen--we never call ovsdb_idl_set_lock(). */
ctl_fatal("database not locked");
default:
OVS_NOT_REACHED();
}
ovsdb_symbol_table_destroy(symtab);
for (c = commands; c < &commands[n_commands]; c++) {
struct ds *ds = &c->output;
if (c->table) {
table_print(c->table, &table_style);
} else if (oneline) {
size_t j;
ds_chomp(ds, '\n');
for (j = 0; j < ds->length; j++) {
int ch = ds->string[j];
switch (ch) {
case '\n':
fputs("\\n", stdout);
break;
case '\\':
fputs("\\\\", stdout);
break;
default:
putchar(ch);
}
}
putchar('\n');
} else {
fputs(ds_cstr(ds), stdout);
}
ds_destroy(&c->output);
table_destroy(c->table);
free(c->table);
shash_destroy_free_data(&c->options);
}
free(commands);
ovsdb_idl_txn_destroy(txn);
ovsdb_idl_destroy(idl);
return true;
try_again:
/* Our transaction needs to be rerun, or a prerequisite was not met. Free
* resources and return so that the caller can try again. */
ovsdb_idl_txn_abort(txn);
ovsdb_idl_txn_destroy(txn);
the_idl_txn = NULL;
ovsdb_symbol_table_destroy(symtab);
for (c = commands; c < &commands[n_commands]; c++) {
ds_destroy(&c->output);
table_destroy(c->table);
free(c->table);
}
return false;
}
/* Frees the current transaction and the underlying IDL and then calls
* exit(status).
*
* Freeing the transaction and the IDL is not strictly necessary, but it makes
* for a clean memory leak report from valgrind in the normal case. That makes
* it easier to notice real memory leaks. */
static void
ic_sbctl_exit(int status)
{
if (the_idl_txn) {
ovsdb_idl_txn_abort(the_idl_txn);
ovsdb_idl_txn_destroy(the_idl_txn);
}
ovsdb_idl_destroy(the_idl);
exit(status);
}
static const struct ctl_command_syntax ic_sbctl_commands[] = {
{ "init", 0, 0, "", NULL, ic_sbctl_init, NULL, "", RW },
{ "show", 0, 1, "[AZ]", NULL, ic_sbctl_show, NULL, "", RO },
/* Connection commands. */