forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-ctl-base.c
2643 lines (2334 loc) · 78.8 KB
/
db-ctl-base.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) 2015, 2016, 2017 Nicira, 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 <unistd.h>
#include "db-ctl-base.h"
#include "command-line.h"
#include "compiler.h"
#include "dirs.h"
#include "openvswitch/dynamic-string.h"
#include "fatal-signal.h"
#include "hash.h"
#include "openvswitch/json.h"
#include "openvswitch/vlog.h"
#include "ovsdb-data.h"
#include "ovsdb-idl.h"
#include "ovsdb-idl-provider.h"
#include "openvswitch/shash.h"
#include "sset.h"
#include "svec.h"
#include "string.h"
#include "table.h"
#include "util.h"
VLOG_DEFINE_THIS_MODULE(db_ctl_base);
/* This array defines the 'show' command output format. User can check the
* definition in utilities/ovs-vsctl.c as reference.
*
* Particularly, if an element in 'columns[]' represents a reference to
* another table, the referred table must also be defined as an entry in
* in 'cmd_show_tables[]'.
*
* The definition must end with an all-NULL entry. It is initalized once
* when ctl_init() is called.
*
* */
static const struct cmd_show_table *cmd_show_tables;
/* ctl_exit() is called by ctl_fatal(). User can optionally supply an exit
* function ctl_exit_func() via ctl_init. If supplied, this function will
* be called by ctl_exit()
*/
static void (*ctl_exit_func)(int status) = NULL;
OVS_NO_RETURN static void ctl_exit(int status);
/* IDL class. */
static const struct ovsdb_idl_class *idl_class;
/* Two arrays with 'n_classes' elements, which represent the tables in this
* database and how the user can refer to their rows. */
static const struct ctl_table_class *ctl_classes;
static const struct ovsdb_idl_table_class *idl_classes;
static size_t n_classes;
static struct shash all_commands = SHASH_INITIALIZER(&all_commands);
static char *get_table(const char *, const struct ovsdb_idl_table_class **);
static char *set_column(const struct ovsdb_idl_table_class *,
const struct ovsdb_idl_row *, const char *,
struct ovsdb_symbol_table *);
static struct option *
find_option(const char *name, struct option *options, size_t n_options)
{
size_t i;
for (i = 0; i < n_options; i++) {
if (!strcmp(options[i].name, name)) {
return &options[i];
}
}
return NULL;
}
static struct option *
add_option(struct option **optionsp, size_t *n_optionsp,
size_t *allocated_optionsp)
{
if (*n_optionsp >= *allocated_optionsp) {
*optionsp = x2nrealloc(*optionsp, allocated_optionsp,
sizeof **optionsp);
}
return &(*optionsp)[(*n_optionsp)++];
}
/* Converts the command arguments into format that can be parsed by
* bash completion script.
*
* Therein, arguments will be attached with following prefixes:
*
* !argument :: The argument is required
* ?argument :: The argument is optional
* *argument :: The argument may appear any number (0 or more) times
* +argument :: The argument may appear one or more times
*
*/
static void
print_command_arguments(const struct ctl_command_syntax *command)
{
/*
* The argument string is parsed in reverse. We use a stack 'oew_stack' to
* keep track of nested optionals. Whenever a ']' is encountered, we push
* a bit to 'oew_stack'. The bit is set to 1 if the ']' is not nested.
* Subsequently, we pop an entry everytime '[' is met.
*
* We use 'whole_word_is_optional' value to decide whether or not a ! or +
* should be added on encountering a space: if the optional surrounds the
* whole word then it shouldn't be, but if it is only a part of the word
* (i.e. [key=]value), it should be.
*/
uint32_t oew_stack = 0;
const char *arguments = command->arguments;
int length = strlen(arguments);
if (!length) {
return;
}
/* Output buffer, written backward from end. */
char *output = xmalloc(2 * length);
char *outp = output + 2 * length;
*--outp = '\0';
bool in_repeated = false;
bool whole_word_is_optional = false;
for (const char *inp = arguments + length; inp > arguments; ) {
switch (*--inp) {
case ']':
oew_stack <<= 1;
if (inp[1] == '\0' || inp[1] == ' ' || inp[1] == '.') {
oew_stack |= 1;
}
break;
case '[':
/* Checks if the whole word is optional, and sets the
* 'whole_word_is_optional' accordingly. */
if ((inp == arguments || inp[-1] == ' ') && oew_stack & 1) {
*--outp = in_repeated ? '*' : '?';
whole_word_is_optional = true;
} else {
*--outp = '?';
whole_word_is_optional = false;
}
oew_stack >>= 1;
break;
case ' ':
if (!whole_word_is_optional) {
*--outp = in_repeated ? '+' : '!';
}
*--outp = ' ';
in_repeated = false;
whole_word_is_optional = false;
break;
case '.':
in_repeated = true;
break;
default:
*--outp = *inp;
break;
}
}
if (arguments[0] != '[' && outp != output + 2 * length - 1) {
*--outp = in_repeated ? '+' : '!';
}
printf("%s", outp);
free(output);
}
static int
to_lower_and_underscores(unsigned c)
{
return c == '-' ? '_' : tolower(c);
}
/* Returns a score representing how well 's' matches 'name'. Higher return
* values indicate a better match. The order of the arguments is important:
* 'name' is the name of an entity such as a table or a column, and 's' is user
* input. */
static unsigned int
score_partial_match(const char *name, const char *s)
{
int score;
if (!strcmp(name, s)) {
return UINT_MAX;
}
for (score = 0; ; score++, name++, s++) {
if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
break;
} else if (*name == '\0') {
return UINT_MAX - 1;
}
}
return *s == '\0' ? score : 0;
}
/* Returns NULL and sets 'symbolp' and 'newp' if symbol was created
* successfully. Otherwise returns a malloc()'ed error message on failure. */
static char * OVS_WARN_UNUSED_RESULT
create_symbol(struct ovsdb_symbol_table *symtab, const char *id,
struct ovsdb_symbol **symbolp, bool *newp)
{
struct ovsdb_symbol *symbol;
ovs_assert(symbolp);
if (id[0] != '@') {
return xasprintf("row id \"%s\" does not begin with \"@\"", id);
}
if (newp) {
*newp = ovsdb_symbol_table_get(symtab, id) == NULL;
}
symbol = ovsdb_symbol_table_insert(symtab, id);
if (symbol->created) {
return xasprintf("row id \"%s\" may only be specified on one --id "
"option", id);
}
symbol->created = true;
*symbolp = symbol;
return NULL;
}
static bool
record_id_equals(const union ovsdb_atom *name, enum ovsdb_atomic_type type,
const char *record_id)
{
if (type == OVSDB_TYPE_STRING) {
if (!strcmp(name->string, record_id)) {
return true;
}
struct uuid uuid;
size_t len = strlen(record_id);
if (len >= 4
&& uuid_from_string(&uuid, name->string)
&& !strncmp(name->string, record_id, len)) {
return true;
}
return false;
} else {
ovs_assert(type == OVSDB_TYPE_INTEGER);
return name->integer == strtoll(record_id, NULL, 10);
}
}
static const struct ovsdb_idl_row *
get_row_by_id(struct ctl_context *ctx,
const struct ovsdb_idl_table_class *table,
const struct ctl_row_id *id, const char *record_id,
bool *multiple_matches)
{
ovs_assert(multiple_matches);
*multiple_matches = false;
if (!id->name_column) {
return NULL;
}
const struct ovsdb_idl_row *referrer = NULL;
/* Figure out the 'key' and 'value' types for the column that we're going
* to look at. One of these ('name_type') is the type of the name we're
* going to compare against 'record_id'. */
enum ovsdb_atomic_type key, value, name_type;
if (!id->key) {
name_type = key = id->name_column->type.key.type;
value = OVSDB_TYPE_VOID;
} else {
key = OVSDB_TYPE_STRING;
name_type = value = id->name_column->type.value.type;
}
/* We only support integer and string names (so far). */
if (name_type == OVSDB_TYPE_INTEGER) {
if (!record_id[0] || record_id[strspn(record_id, "0123456789")]) {
return NULL;
}
} else {
ovs_assert(name_type == OVSDB_TYPE_STRING);
}
const struct ovsdb_idl_class *class = ovsdb_idl_get_class(ctx->idl);
const struct ovsdb_idl_table_class *id_table
= ovsdb_idl_table_class_from_column(class, id->name_column);
for (const struct ovsdb_idl_row *row = ovsdb_idl_first_row(ctx->idl,
id_table);
row != NULL;
row = ovsdb_idl_next_row(row)) {
/* Pick out the name column's data. */
const struct ovsdb_datum *datum = ovsdb_idl_get(
row, id->name_column, key, value);
/* Extract the name from the column. */
const union ovsdb_atom *name;
if (!id->key) {
name = datum->n == 1 ? &datum->keys[0] : NULL;
} else {
const union ovsdb_atom key_atom
= { .string = CONST_CAST(char *, id->key) };
unsigned int i = ovsdb_datum_find_key(datum, &key_atom,
OVSDB_TYPE_STRING);
name = i == UINT_MAX ? NULL : &datum->values[i];
}
if (!name) {
continue;
}
/* If the name equals 'record_id', take it. */
if (record_id_equals(name, name_type, record_id)) {
if (referrer) {
*multiple_matches = true;
return NULL;
}
referrer = row;
}
}
if (!referrer) {
return NULL;
}
const struct ovsdb_idl_row *final = referrer;
if (id->uuid_column) {
const struct ovsdb_datum *uuid;
ovsdb_idl_txn_verify(referrer, id->uuid_column);
uuid = ovsdb_idl_get(referrer, id->uuid_column,
OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
if (uuid->n == 1) {
final = ovsdb_idl_get_row_for_uuid(ctx->idl, table,
&uuid->keys[0].uuid);
} else {
final = NULL;
}
}
return final;
}
char * OVS_WARN_UNUSED_RESULT
ctl_get_row(struct ctl_context *ctx,
const struct ovsdb_idl_table_class *table, const char *record_id,
bool must_exist, const struct ovsdb_idl_row **rowp)
{
const struct ovsdb_idl_row *row = NULL;
struct uuid uuid;
ovs_assert(rowp);
if (uuid_from_string(&uuid, record_id)) {
row = ovsdb_idl_get_row_for_uuid(ctx->idl, table, &uuid);
}
if (!row) {
if (!strcmp(record_id, ".")) {
row = ovsdb_idl_first_row(ctx->idl, table);
if (row && ovsdb_idl_next_row(row)) {
row = NULL;
}
}
}
if (!row) {
const struct ctl_table_class *ctl_class
= &ctl_classes[table - idl_classes];
for (int i = 0; i < ARRAY_SIZE(ctl_class->row_ids); i++) {
const struct ctl_row_id *id = &ctl_class->row_ids[i];
bool multiple_matches;
row = get_row_by_id(ctx, table, id, record_id, &multiple_matches);
if (multiple_matches) {
const struct ovsdb_idl_class *class =
ovsdb_idl_get_class(ctx->idl);
const struct ovsdb_idl_table_class *table_class =
ovsdb_idl_table_class_from_column(class, id->name_column);
return xasprintf("multiple rows in %s match \"%s\"",
table_class->name, record_id);
}
if (row) {
break;
}
}
}
if (!row && uuid_is_partial_string(record_id) >= 4) {
for (const struct ovsdb_idl_row *r = ovsdb_idl_first_row(ctx->idl,
table);
r != NULL;
r = ovsdb_idl_next_row(r)) {
if (uuid_is_partial_match(&r->uuid, record_id)) {
if (!row) {
row = r;
} else {
return xasprintf("%s contains 2 or more rows whose UUIDs "
"begin with %s: at least "UUID_FMT" "
"and "UUID_FMT, table->name, record_id,
UUID_ARGS(&row->uuid),
UUID_ARGS(&r->uuid));
}
}
}
}
if (must_exist && !row) {
return xasprintf("no row \"%s\" in table %s", record_id, table->name);
}
*rowp = row;
return NULL;
}
static char *
get_column(const struct ovsdb_idl_table_class *table, const char *column_name,
const struct ovsdb_idl_column **columnp)
{
const struct ovsdb_idl_column *best_match = NULL;
unsigned int best_score = 0;
size_t i;
for (i = 0; i < table->n_columns; i++) {
const struct ovsdb_idl_column *column = &table->columns[i];
unsigned int score = score_partial_match(column->name, column_name);
if (score > best_score) {
best_match = column;
best_score = score;
} else if (score == best_score) {
best_match = NULL;
}
}
*columnp = best_match;
if (best_match) {
return NULL;
} else if (best_score) {
return xasprintf("%s contains more than one column whose name "
"matches \"%s\"", table->name, column_name);
} else {
return xasprintf("%s does not contain a column whose name matches "
"\"%s\"", table->name, column_name);
}
}
static char * OVS_WARN_UNUSED_RESULT
pre_get_column(struct ctl_context *ctx,
const struct ovsdb_idl_table_class *table,
const char *column_name,
const struct ovsdb_idl_column **columnp)
{
char *error = get_column(table, column_name, columnp);
if (error) {
return error;
}
ovsdb_idl_add_column(ctx->idl, *columnp);
return NULL;
}
static char * OVS_WARN_UNUSED_RESULT
pre_get_table(struct ctl_context *ctx, const char *table_name,
const struct ovsdb_idl_table_class **tablep)
{
const struct ovsdb_idl_table_class *table;
char *error = get_table(table_name, &table);
if (error) {
return error;
}
ovsdb_idl_add_table(ctx->idl, table);
const struct ctl_table_class *ctl = &ctl_classes[table - idl_classes];
for (int i = 0; i < ARRAY_SIZE(ctl->row_ids); i++) {
const struct ctl_row_id *id = &ctl->row_ids[i];
if (id->name_column) {
ovsdb_idl_add_column(ctx->idl, id->name_column);
}
if (id->uuid_column) {
ovsdb_idl_add_column(ctx->idl, id->uuid_column);
}
}
if (tablep) {
*tablep = table;
}
return NULL;
}
static char *
missing_operator_error(const char *arg, const char **allowed_operators,
size_t n_allowed)
{
struct ds s;
ds_init(&s);
ds_put_format(&s, "%s: argument does not end in ", arg);
ds_put_format(&s, "\"%s\"", allowed_operators[0]);
if (n_allowed == 2) {
ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
} else if (n_allowed > 2) {
size_t i;
for (i = 1; i < n_allowed - 1; i++) {
ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
}
ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
}
ds_put_format(&s, " followed by a value.");
return ds_steal_cstr(&s);
}
/* Breaks 'arg' apart into a number of fields in the following order:
*
* - The name of a column in 'table', stored into '*columnp'. The column
* name may be abbreviated.
*
* - Optionally ':' followed by a key string. The key is stored as a
* malloc()'d string into '*keyp', or NULL if no key is present in
* 'arg'.
*
* - If 'valuep' is nonnull, an operator followed by a value string. The
* allowed operators are the 'n_allowed' string in 'allowed_operators',
* or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
* index of the operator within 'allowed_operators' is stored into
* '*operatorp'. The value is stored as a malloc()'d string into
* '*valuep', or NULL if no value is present in 'arg'.
*
* On success, returns NULL. On failure, returned a malloc()'d string error
* message and stores NULL into all of the nonnull output arguments. */
static char * OVS_WARN_UNUSED_RESULT
parse_column_key_value(const char *arg,
const struct ovsdb_idl_table_class *table,
const struct ovsdb_idl_column **columnp, char **keyp,
int *operatorp,
const char **allowed_operators, size_t n_allowed,
char **valuep)
{
const char *p = arg;
char *column_name;
char *error;
ovs_assert(!(operatorp && !valuep));
*keyp = NULL;
if (valuep) {
*valuep = NULL;
}
/* Parse column name. */
error = ovsdb_token_parse(&p, &column_name);
if (error) {
goto error;
}
if (column_name[0] == '\0') {
free(column_name);
error = xasprintf("%s: missing column name", arg);
goto error;
}
error = get_column(table, column_name, columnp);
free(column_name);
if (error) {
goto error;
}
/* Parse key string. */
if (*p == ':') {
p++;
error = ovsdb_token_parse(&p, keyp);
if (error) {
goto error;
}
}
/* Parse value string. */
if (valuep) {
size_t best_len;
size_t i;
int best;
if (!allowed_operators) {
static const char *equals = "=";
allowed_operators = =
n_allowed = 1;
}
best = -1;
best_len = 0;
for (i = 0; i < n_allowed; i++) {
const char *op = allowed_operators[i];
size_t op_len = strlen(op);
if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
best_len = op_len;
best = i;
}
}
if (best < 0) {
error = missing_operator_error(arg, allowed_operators, n_allowed);
goto error;
}
if (operatorp) {
*operatorp = best;
}
*valuep = xstrdup(p + best_len);
} else {
if (*p != '\0') {
error = xasprintf("%s: trailing garbage \"%s\" in argument",
arg, p);
goto error;
}
}
return NULL;
error:
*columnp = NULL;
free(*keyp);
*keyp = NULL;
if (valuep) {
free(*valuep);
*valuep = NULL;
if (operatorp) {
*operatorp = -1;
}
}
return error;
}
static char * OVS_WARN_UNUSED_RESULT
pre_parse_column_key_value(struct ctl_context *ctx, const char *arg,
const struct ovsdb_idl_table_class *table)
{
const struct ovsdb_idl_column *column;
const char *p;
char *column_name = NULL;
char *error;
p = arg;
error = ovsdb_token_parse(&p, &column_name);
if (error) {
goto out;
}
if (column_name[0] == '\0') {
error = xasprintf("%s: missing column name", arg);
goto out;
}
error = pre_get_column(ctx, table, column_name, &column);
out:
free(column_name);
return error;
}
/* Checks if the 'column' is mutable. Returns NULL if it is mutable, or a
* malloc()'ed error message otherwise. */
static char * OVS_WARN_UNUSED_RESULT
check_mutable(const struct ovsdb_idl_row *row,
const struct ovsdb_idl_column *column)
{
if (!ovsdb_idl_is_mutable(row, column)) {
return xasprintf("cannot modify read-only column %s in table %s",
column->name, row->table->class_->name);
}
return NULL;
}
#define RELOPS \
RELOP(RELOP_EQ, "=") \
RELOP(RELOP_NE, "!=") \
RELOP(RELOP_LT, "<") \
RELOP(RELOP_GT, ">") \
RELOP(RELOP_LE, "<=") \
RELOP(RELOP_GE, ">=") \
RELOP(RELOP_SET_EQ, "{=}") \
RELOP(RELOP_SET_NE, "{!=}") \
RELOP(RELOP_SET_LT, "{<}") \
RELOP(RELOP_SET_GT, "{>}") \
RELOP(RELOP_SET_LE, "{<=}") \
RELOP(RELOP_SET_GE, "{>=}")
enum relop {
#define RELOP(ENUM, STRING) ENUM,
RELOPS
#undef RELOP
};
static bool
is_set_operator(enum relop op)
{
return (op == RELOP_SET_EQ || op == RELOP_SET_NE ||
op == RELOP_SET_LT || op == RELOP_SET_GT ||
op == RELOP_SET_LE || op == RELOP_SET_GE);
}
static bool
evaluate_relop(const struct ovsdb_datum *a, const struct ovsdb_datum *b,
const struct ovsdb_type *type, enum relop op)
{
switch (op) {
case RELOP_EQ:
case RELOP_SET_EQ:
return ovsdb_datum_compare_3way(a, b, type) == 0;
case RELOP_NE:
case RELOP_SET_NE:
return ovsdb_datum_compare_3way(a, b, type) != 0;
case RELOP_LT:
return ovsdb_datum_compare_3way(a, b, type) < 0;
case RELOP_GT:
return ovsdb_datum_compare_3way(a, b, type) > 0;
case RELOP_LE:
return ovsdb_datum_compare_3way(a, b, type) <= 0;
case RELOP_GE:
return ovsdb_datum_compare_3way(a, b, type) >= 0;
case RELOP_SET_LT:
return b->n > a->n && ovsdb_datum_includes_all(a, b, type);
case RELOP_SET_GT:
return a->n > b->n && ovsdb_datum_includes_all(b, a, type);
case RELOP_SET_LE:
return ovsdb_datum_includes_all(a, b, type);
case RELOP_SET_GE:
return ovsdb_datum_includes_all(b, a, type);
default:
OVS_NOT_REACHED();
}
}
/* Checks if given row satisfies the specified condition. Returns the result of
* evaluating the condition in 'satisfied' flag and NULL as a return value on
* success. On failure returns a malloc()'ed error message and 'satisfied'
* value is not modified. */
static char * OVS_WARN_UNUSED_RESULT
check_condition(const struct ovsdb_idl_table_class *table,
const struct ovsdb_idl_row *row, const char *arg,
struct ovsdb_symbol_table *symtab, bool *satisfied)
{
static const char *operators[] = {
#define RELOP(ENUM, STRING) STRING,
RELOPS
#undef RELOP
};
const struct ovsdb_idl_column *column;
const struct ovsdb_datum *have_datum;
char *key_string = NULL;
char *value_string = NULL;
struct ovsdb_type type;
int operator;
bool retval;
char *error;
ovs_assert(satisfied);
error = parse_column_key_value(arg, table, &column, &key_string,
&operator, operators, ARRAY_SIZE(operators),
&value_string);
if (error) {
goto out;
}
if (!value_string) {
error = xasprintf("%s: missing value", arg);
goto out;
}
type = column->type;
type.n_max = UINT_MAX;
have_datum = ovsdb_idl_read(row, column);
if (key_string) {
union ovsdb_atom want_key;
struct ovsdb_datum b;
unsigned int idx;
if (column->type.value.type == OVSDB_TYPE_VOID) {
error = xasprintf("cannot specify key to check for non-map column "
"%s", column->name);
goto out;
}
error = ovsdb_atom_from_string(&want_key, NULL, &column->type.key,
key_string, symtab);
if (error) {
goto out;
}
type.key = type.value;
type.value.type = OVSDB_TYPE_VOID;
error = ovsdb_datum_from_string(&b, &type, value_string, symtab);
if (error) {
goto out;
}
idx = ovsdb_datum_find_key(have_datum,
&want_key, column->type.key.type);
if (idx == UINT_MAX && !is_set_operator(operator)) {
retval = false;
} else {
struct ovsdb_datum a;
if (idx != UINT_MAX) {
a.n = 1;
a.keys = &have_datum->values[idx];
a.values = NULL;
} else {
a.n = 0;
a.keys = NULL;
a.values = NULL;
}
retval = evaluate_relop(&a, &b, &type, operator);
}
ovsdb_atom_destroy(&want_key, column->type.key.type);
ovsdb_datum_destroy(&b, &type);
} else {
struct ovsdb_datum want_datum;
error = ovsdb_datum_from_string(&want_datum, &column->type,
value_string, symtab);
if (error) {
goto out;
}
retval = evaluate_relop(have_datum, &want_datum, &type, operator);
ovsdb_datum_destroy(&want_datum, &column->type);
}
*satisfied = retval;
out:
free(key_string);
free(value_string);
return error;
}
static void
invalidate_cache(struct ctl_context *ctx)
{
if (ctx->invalidate_cache_cb) {
(ctx->invalidate_cache_cb)(ctx);
}
}
static void
pre_cmd_get(struct ctl_context *ctx)
{
const char *id = shash_find_data(&ctx->options, "--id");
const char *table_name = ctx->argv[1];
const struct ovsdb_idl_table_class *table;
int i;
/* Using "get" without --id or a column name could possibly make sense.
* Maybe, for example, a *ctl command run wants to assert that a row
* exists. But it is unlikely that an interactive user would want to do
* that, so issue a warning if we're running on a terminal. */
if (!id && ctx->argc <= 3 && isatty(STDOUT_FILENO)) {
VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
"possibly erroneous");
}
ctx->error = pre_get_table(ctx, table_name, &table);
if (ctx->error) {
return;
}
for (i = 3; i < ctx->argc; i++) {
if (!strcasecmp(ctx->argv[i], "_uuid")
|| !strcasecmp(ctx->argv[i], "-uuid")) {
continue;
}
ctx->error = pre_parse_column_key_value(ctx, ctx->argv[i], table);
if (ctx->error) {
return;
}
}
}
static void
cmd_get(struct ctl_context *ctx)
{
const char *id = shash_find_data(&ctx->options, "--id");
bool must_exist = !shash_find(&ctx->options, "--if-exists");
const char *table_name = ctx->argv[1];
const char *record_id = ctx->argv[2];
const struct ovsdb_idl_table_class *table;
const struct ovsdb_idl_row *row;
struct ds *out = &ctx->output;
int i;
if (id && !must_exist) {
ctl_error(ctx, "--if-exists and --id may not be specified together");
return;
}
ctx->error = get_table(table_name, &table);
if (ctx->error) {
return;
}
ctx->error = ctl_get_row(ctx, table, record_id, must_exist, &row);
if (ctx->error) {
return;
}
if (!row) {
return;
}
if (id) {
struct ovsdb_symbol *symbol = NULL;
bool new = false;
ctx->error = create_symbol(ctx->symtab, id, &symbol, &new);
if (ctx->error) {
return;
}
if (!new) {
ctl_error(ctx, "row id \"%s\" specified on \"get\" command was "
"used before it was defined", id);
return;
}
symbol->uuid = row->uuid;
/* This symbol refers to a row that already exists, so disable warnings
* about it being unreferenced. */
symbol->strong_ref = true;
}
for (i = 3; i < ctx->argc; i++) {
const struct ovsdb_idl_column *column;
const struct ovsdb_datum *datum;
char *key_string;
/* Special case for obtaining the UUID of a row. We can't just do this
* through parse_column_key_value() below since it returns a "struct
* ovsdb_idl_column" and the UUID column doesn't have one. */
if (!strcasecmp(ctx->argv[i], "_uuid")
|| !strcasecmp(ctx->argv[i], "-uuid")) {
ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
continue;
}
ctx->error = parse_column_key_value(ctx->argv[i], table, &column,
&key_string, NULL, NULL, 0, NULL);
if (ctx->error) {
return;
}
ovsdb_idl_txn_verify(row, column);
datum = ovsdb_idl_read(row, column);
if (key_string) {
union ovsdb_atom key;
unsigned int idx;
if (column->type.value.type == OVSDB_TYPE_VOID) {
ctl_error(ctx,
"cannot specify key to get for non-map column %s",
column->name);
free(key_string);
return;
}
ctx->error = ovsdb_atom_from_string(&key, NULL, &column->type.key,
key_string, ctx->symtab);
if (ctx->error) {
free(key_string);
return;
}
idx = ovsdb_datum_find_key(datum, &key,
column->type.key.type);
if (idx == UINT_MAX) {
if (must_exist) {
ctl_error(
ctx, "no key \"%s\" in %s record \"%s\" column %s",
key_string, table->name, record_id, column->name);
free(key_string);
return;
}
} else {
ovsdb_atom_to_string(&datum->values[idx],
column->type.value.type, out);
}
ovsdb_atom_destroy(&key, column->type.key.type);
} else {
ovsdb_datum_to_string(datum, &column->type, out);
}
ds_put_char(out, '\n');