Following a UUID printed by list or find takes another database command for each hop. Make expansion opt-in so users can inspect related rows in one invocation without changing output for existing consumers.
Use the IDL schema to follow forward UUID references without bespoke print handlers. Keep the original datums and snapshot referenced rows for formatting after the transaction. Cap recursion at five hops and skip rows already on the current path, so shared children can still appear under each referring row. The RFC supports list and table output for interactive use and structured JSON for downstream consumers. Reject bare, CSV, and HTML with --all before any command in the batch can run. Reported-at: https://redhat.atlassian.net/browse/FDP-694 Assisted-by: GPT-6.2 Sol, OpenCode Signed-off-by: Ales Musil <[email protected]> --- lib/db-ctl-base.c | 210 ++++++++++++++++++++++++++++++++++++++---- lib/db-ctl-base.h | 5 + lib/table.c | 194 ++++++++++++++++++++++++++++++++++---- lib/table.h | 5 + utilities/ovs-vsctl.c | 5 +- vtep/vtep-ctl.c | 5 +- 6 files changed, 384 insertions(+), 40 deletions(-) diff --git a/lib/db-ctl-base.c b/lib/db-ctl-base.c index bd3a21dd9..9ee316178 100644 --- a/lib/db-ctl-base.c +++ b/lib/db-ctl-base.c @@ -1025,6 +1025,72 @@ cmd_get(struct ctl_context *ctx) } } +/* The root row is at depth zero. */ +#define LIST_REF_MAX_DEPTH 5 + +/* Returns the exact schema target of a UUID reference, if there is one. */ +static const struct ovsdb_idl_table_class * +list_ref_table(const struct ovsdb_base_type *base) +{ + if (ovsdb_base_type_is_ref(base)) { + for (size_t i = 0; i < n_classes; i++) { + if (!strcmp(base->uuid.refTableName, idl_classes[i].name)) { + return &idl_classes[i]; + } + } + } + return NULL; +} + +/* Monitors referenced rows' columns starting from a column at 'depth'. */ +static void +pre_list_refs(struct ovsdb_idl *idl, const struct ovsdb_type *type, + unsigned int depth, unsigned int *depths) +{ + if (depth == LIST_REF_MAX_DEPTH) { + return; + } + depth++; + + const struct ovsdb_idl_table_class *targets[] = { + list_ref_table(&type->key), + list_ref_table(&type->value), + }; + for (size_t i = 0; i < ARRAY_SIZE(targets); i++) { + const struct ovsdb_idl_table_class *table = targets[i]; + if (!table) { + continue; + } + + /* Only full-table visits count. A shallower visit can reach more + * descendants, even if all columns of this table are monitored. */ + size_t index = table - idl_classes; + if (depths[index] && depths[index] <= depth) { + continue; + } + depths[index] = depth; + ovsdb_idl_add_table(idl, table); + for (size_t j = 0; j < table->n_columns; j++) { + const struct ovsdb_idl_column *column = &table->columns[j]; + ovsdb_idl_add_column(idl, column); + pre_list_refs(idl, &column->type, depth, depths); + } + } +} + +/* Includes the synthetic _uuid column, represented by NULL, first. */ +static const struct ovsdb_idl_column ** +list_all_columns(const struct ovsdb_idl_table_class *table) +{ + const struct ovsdb_idl_column **columns = + xmalloc((table->n_columns + 1) * sizeof *columns); + columns[0] = NULL; + for (size_t i = 0; i < table->n_columns; i++) { + columns[i + 1] = &table->columns[i]; + } + return columns; +} + /* Returns NULL on success or malloc()'ed error message on failure. */ static char * OVS_WARN_UNUSED_RESULT parse_column_names(const char *column_names, @@ -1036,14 +1102,8 @@ parse_column_names(const char *column_names, size_t n_columns; if (!column_names) { - size_t i; - n_columns = table->n_columns + 1; - columns = xmalloc(n_columns * sizeof *columns); - columns[0] = NULL; - for (i = 0; i < table->n_columns; i++) { - columns[i + 1] = &table->columns[i]; - } + columns = list_all_columns(table); } else { char *s = xstrdup(column_names); size_t allocated_columns; @@ -1097,11 +1157,17 @@ pre_list_columns(struct ctl_context *ctx, if (error) { return error; } + unsigned int *depths = shash_find(&ctx->options, "--all") + ? xzalloc(n_classes * sizeof *depths) : NULL; for (i = 0; i < n_columns; i++) { if (columns[i]) { ovsdb_idl_add_column(ctx->idl, columns[i]); + if (depths) { + pre_list_refs(ctx->idl, &columns[i]->type, 0, depths); + } } } + free(depths); free(columns); return NULL; } @@ -1142,16 +1208,83 @@ list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns) return out; } +/* Tracks only the active ancestors of the row being rendered. */ +struct list_ref_path { + const struct ovsdb_idl *idl; /* The current IDL transaction view. */ + + /* Only slots through the current depth belong to the active path. */ + const struct ovsdb_idl_row *rows[LIST_REF_MAX_DEPTH + 1]; +}; + +/* Returns whether 'row' is on the current branch, including 'depth'. */ +static bool +list_ref_on_path(const struct list_ref_path *path, + const struct ovsdb_idl_row *row, unsigned int depth) +{ + for (unsigned int i = 0; i <= depth; i++) { + if (path->rows[i] == row) { + return true; + } + } + return false; +} + +static void list_record(const struct ovsdb_idl_row *, + const struct ovsdb_idl_column **, size_t n_columns, + struct table *, struct list_ref_path *, + unsigned int depth); + +/* Resolves schema references in a cell without modifying its source datum. */ +static void +list_cell_refs(struct cell *cell, const struct ovsdb_datum *datum, + struct list_ref_path *path, unsigned int depth) +{ + const struct ovsdb_idl_table_class *targets[] = { + list_ref_table(&cell->type->key), + list_ref_table(&cell->type->value), + }; + if (!targets[0] && !targets[1]) { + return; + } + const union ovsdb_atom *atoms[] = { datum->keys, datum->values }; + + for (unsigned int i = 0; i < datum->n; i++) { + for (size_t side = 0; side < ARRAY_SIZE(targets); side++) { + const struct ovsdb_idl_table_class *table = targets[side]; + if (!table) { + continue; + } + const struct uuid *uuid = &atoms[side][i].uuid; + const struct ovsdb_idl_row *row = + ovsdb_idl_get_row_for_uuid(path->idl, table, uuid); + if (!row || list_ref_on_path(path, row, depth)) { + continue; + } + + const struct ovsdb_idl_column **columns = list_all_columns(table); + size_t n_columns = table->n_columns + 1; + struct table *child = list_make_table(columns, n_columns); + list_record(row, columns, n_columns, child, path, depth + 1); + free(columns); + table_cell_add_ref(cell, i, side == 1, table->name, child); + } + } +} + +/* Collects a row's typed cells and, if 'path' is provided, expanded refs. */ static void list_record(const struct ovsdb_idl_row *row, const struct ovsdb_idl_column **columns, size_t n_columns, - struct table *out) + struct table *out, struct list_ref_path *path, unsigned int depth) { size_t i; if (!row) { return; } + if (path) { + path->rows[depth] = row; + } table_add_row(out); for (i = 0; i < n_columns; i++) { @@ -1175,6 +1308,9 @@ list_record(const struct ovsdb_idl_row *row, cell->json = ovsdb_datum_to_json(datum, &column->type); cell->type = &column->type; + if (path && depth < LIST_REF_MAX_DEPTH) { + list_cell_refs(cell, datum, path, depth); + } } } } @@ -1183,6 +1319,9 @@ static void cmd_list(struct ctl_context *ctx) { const char *column_names = shash_find_data(&ctx->options, "--columns"); + struct list_ref_path path = { .idl = ctx->idl }; + struct list_ref_path *refs = + shash_find(&ctx->options, "--all") ? &path : NULL; bool must_exist = !shash_find(&ctx->options, "--if-exists"); const struct ovsdb_idl_column **columns; const char *table_name = ctx->argv[1]; @@ -1210,14 +1349,14 @@ cmd_list(struct ctl_context *ctx) free(columns); return; } - list_record(row, columns, n_columns, out); + list_record(row, columns, n_columns, out, refs, 0); } } else { const struct ovsdb_idl_row *row; for (row = ovsdb_idl_first_row(ctx->idl, table); row != NULL; row = ovsdb_idl_next_row(row)) { - list_record(row, columns, n_columns, out); + list_record(row, columns, n_columns, out, refs, 0); } } free(columns); @@ -1289,6 +1428,9 @@ static void cmd_find(struct ctl_context *ctx) { const char *column_names = shash_find_data(&ctx->options, "--columns"); + struct list_ref_path path = { .idl = ctx->idl }; + struct list_ref_path *refs = + shash_find(&ctx->options, "--all") ? &path : NULL; const struct ovsdb_idl_column **columns; const char *table_name = ctx->argv[1]; const struct ovsdb_idl_table_class *table; @@ -1322,7 +1464,7 @@ cmd_find(struct ctl_context *ctx) goto next_row; } } - list_record(row, columns, n_columns, out); + list_record(row, columns, n_columns, out, refs, 0); next_row: ; } @@ -1951,6 +2093,7 @@ cmd_wait_until(struct ctl_context *ctx) /* Parses one command. */ static char * OVS_WARN_UNUSED_RESULT parse_command(int argc, char *argv[], struct shash *local_options, + const struct table_style *style, struct ctl_command *command) { const struct ctl_command_syntax *p; @@ -2044,6 +2187,26 @@ parse_command(int argc, char *argv[], struct shash *local_options, goto error; } + if ((p->run == cmd_list || p->run == cmd_find) + && shash_find(&command->options, "--all")) { + if (!style) { + error = xasprintf("'%s' with '--all' is not supported by " + "this client", p->name); + goto error; + } + if (style->cell_format == CF_BARE) { + error = xasprintf("'%s' with '--all' does not support bare data", + p->name); + goto error; + } + if (style->format != TF_LIST && style->format != TF_TABLE + && style->format != TF_JSON) { + error = xasprintf("'%s' with '--all' requires list, table, or " + "JSON output", p->name); + goto error; + } + } + command->syntax = p; command->argc = n_arg + 1; command->argv = &argv[i]; @@ -2437,10 +2600,14 @@ ctl_add_cmd_options(struct option **options_p, size_t *n_options_p, memset(o, 0, sizeof *o); } -/* Parses command-line input for commands. */ +/* Parses a command batch, checking the output style of list/find --all before + * any commands can be run. */ char * -ctl_parse_commands(int argc, char *argv[], struct shash *local_options, - struct ctl_command **commandsp, size_t *n_commandsp) +ctl_parse_commands_with_style(int argc, char *argv[], + struct shash *local_options, + const struct table_style *style, + struct ctl_command **commandsp, + size_t *n_commandsp) { struct ctl_command *commands; size_t n_commands, allocated_commands; @@ -2466,7 +2633,7 @@ ctl_parse_commands(int argc, char *argv[], struct shash *local_options, } } error = parse_command(i - start, &argv[start], local_options, - &commands[n_commands]); + style, &commands[n_commands]); if (error) { struct ctl_command *c; @@ -2493,6 +2660,15 @@ ctl_parse_commands(int argc, char *argv[], struct shash *local_options, return NULL; } +/* Clients without an output style cannot use list/find --all. */ +char * +ctl_parse_commands(int argc, char *argv[], struct shash *local_options, + struct ctl_command **commandsp, size_t *n_commandsp) +{ + return ctl_parse_commands_with_style(argc, argv, local_options, NULL, + commandsp, n_commandsp); +} + /* Prints all registered commands. */ void ctl_print_commands(void) @@ -2618,9 +2794,9 @@ static const struct ctl_command_syntax db_ctl_commands[] = { {"get", 2, INT_MAX, "TABLE RECORD [COLUMN[:KEY]]...",pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO}, {"list", 1, INT_MAX, "TABLE [RECORD]...", pre_cmd_list, cmd_list, NULL, - "--if-exists,--columns=", RO}, + "--if-exists,--columns=,--all", RO}, {"find", 1, INT_MAX, "TABLE [COLUMN[:KEY]=VALUE]...", pre_cmd_find, - cmd_find, NULL, "--columns=", RO}, + cmd_find, NULL, "--columns=,--all", RO}, {"set", 3, INT_MAX, "TABLE RECORD COLUMN[:KEY]=VALUE...", pre_cmd_set, cmd_set, NULL, "--if-exists", RW}, {"add", 4, INT_MAX, "TABLE RECORD COLUMN [KEY=]VALUE...", pre_cmd_add, diff --git a/lib/db-ctl-base.h b/lib/db-ctl-base.h index ea7e97b78..2a0b96481 100644 --- a/lib/db-ctl-base.h +++ b/lib/db-ctl-base.h @@ -28,6 +28,7 @@ struct ovsdb_idl_row; struct ovsdb_idl_txn; struct ovsdb_symbol_table; struct table; +struct table_style; /* This library module contains the common parts for ovsdb manipulation * (structs, commands and functions). To utilize this module, user must @@ -169,6 +170,10 @@ void ctl_register_commands(const struct ctl_command_syntax *); char * OVS_WARN_UNUSED_RESULT ctl_parse_commands( int argc, char *argv[], struct shash *local_options, struct ctl_command **commandsp, size_t *n_commandsp); +char * OVS_WARN_UNUSED_RESULT ctl_parse_commands_with_style( + int argc, char *argv[], struct shash *local_options, + const struct table_style *, struct ctl_command **commandsp, + size_t *n_commandsp); /* Sometimes, it is desirable to print the table with weak reference to * rows in a 'cmd_show_table' table. In that case, the 'weak_ref_table' diff --git a/lib/table.c b/lib/table.c index b7addbf39..db0c11018 100644 --- a/lib/table.c +++ b/lib/table.c @@ -29,6 +29,21 @@ struct column { char *heading; }; +/* A resolved reference to a single row, at a specific position in a datum. */ +struct cell_ref { + unsigned int index; /* Zero-based index in the source datum. */ + bool is_value; /* Map value rather than key or set member. */ + char *table_name; /* Name of the referenced table. */ + struct table *row; /* Snapshot of the referenced row. */ +}; + +/* The collection is allocated only for cells with expanded references. */ +struct cell_refs { + struct cell_ref *items; /* Expanded references in datum order. */ + size_t n; /* Number of expanded references. */ + size_t allocated; /* Capacity of 'items'. */ +}; + static char * cell_to_text(struct cell *cell, const struct table_style *style) { @@ -70,6 +85,17 @@ cell_destroy(struct cell *cell) { free(cell->text); json_destroy(cell->json); + struct cell_refs *refs = cell->refs; + if (refs) { + for (size_t i = 0; i < refs->n; i++) { + struct cell_ref *ref = &refs->items[i]; + free(ref->table_name); + table_destroy(ref->row); + free(ref->row); + } + free(refs->items); + free(refs); + } } /* Initializes 'table' as an empty table. @@ -211,6 +237,30 @@ table_add_cell(struct table *table) return table_cell__(table, y, x); } +/* Attaches a resolved reference at 'index' in the cell's OVSDB datum, taking + * ownership of the single-row table 'row'. The original datum is unchanged. + * 'is_value' is true for the value side of a map entry. */ +void +table_cell_add_ref(struct cell *cell, unsigned int index, bool is_value, + const char *table_name, struct table *row) +{ + ovs_assert(cell->json && cell->type && row->n_rows == 1); + struct cell_refs *refs = cell->refs; + if (!refs) { + refs = cell->refs = xzalloc(sizeof *refs); + } + if (refs->n >= refs->allocated) { + refs->items = x2nrealloc(refs->items, &refs->allocated, + sizeof *refs->items); + } + refs->items[refs->n++] = (struct cell_ref) { + .index = index, + .is_value = is_value, + .table_name = xstrdup(table_name), + .row = row, + }; +} + static void table_finish_line(struct ds *s) { @@ -238,6 +288,84 @@ table_print_timestamp__(const struct table *table, struct ds *s) static bool first_table = true; +static void table_print_list_row__(const struct table *, + const struct table_style *, size_t row, + size_t indent, struct ds *); + +/* Formats a map key or value from the cell's native JSON datum. */ +static void +table_print_atom__(const struct json *json, const struct ovsdb_base_type *base, + const struct table_style *style, struct ds *s) +{ + if (style->cell_format == CF_STRING) { + union ovsdb_atom atom; + struct ovsdb_error *error = ovsdb_atom_from_json(&atom, base, json, + NULL); + if (!error) { + ovsdb_atom_to_string(&atom, base->type, s); + ovsdb_atom_destroy(&atom, base->type); + return; + } + ovsdb_error_destroy(error); + } + json_to_ds(json, 0, s); +} + +/* Prints child rows separately from the parent cell's ordinary rendering. */ +static void +table_print_cell_refs__(const struct cell *cell, const char *heading, + const struct table_style *style, size_t indent, + struct ds *s) +{ + const struct cell_refs *refs = cell->refs; + if (!refs) { + return; + } + + for (size_t i = 0; i < refs->n; i++) { + const struct cell_ref *ref = &refs->items[i]; + ds_put_char_multiple(s, ' ', indent); + ds_put_format(s, "%s[%u].%s", heading, ref->index, + ref->is_value ? "value" : "key"); + if (ovsdb_type_is_map(cell->type)) { + /* OVSDB maps encode their entries as ["map", [[key, value], ...]]. + * Use the original entry even if the table grid truncated it. */ + const struct json *entries = json_array_at(cell->json, 1); + const struct json *entry = json_array_at(entries, ref->index); + ds_put_cstr(s, " ("); + table_print_atom__(json_array_at(entry, 0), &cell->type->key, + style, s); + ds_put_char(s, '='); + table_print_atom__(json_array_at(entry, 1), &cell->type->value, + style, s); + ds_put_char(s, ')'); + } + ds_put_format(s, " -> %s:\n", ref->table_name); + table_print_list_row__(ref->row, style, 0, indent + 2, s); + } +} + +/* Prints one row and its descendants as a list of fields. */ +static void +table_print_list_row__(const struct table *table, + const struct table_style *style, size_t row, + size_t indent, struct ds *s) +{ + for (size_t x = 0; x < table->n_columns; x++) { + struct cell *cell = table_cell__(table, row, x); + const char *heading = table->columns[x].heading; + + if (indent) { + ds_put_char_multiple(s, ' ', indent); + } + if (style->headings) { + ds_put_format(s, "%-20s: ", heading); + } + ds_put_format(s, "%s\n", cell_to_text(cell, style)); + table_print_cell_refs__(cell, heading, style, indent + 2, s); + } +} + static void table_print_table__(const struct table *table, const struct table_style *style, struct ds *s) @@ -304,14 +432,24 @@ table_print_table__(const struct table *table, const struct table_style *style, } for (y = 0; y < table->n_rows; y++) { + bool has_refs = false; for (x = 0; x < table->n_columns; x++) { - const char *text = cell_to_text(table_cell__(table, y, x), style); + struct cell *cell = table_cell__(table, y, x); + const char *text = cell_to_text(cell, style); + has_refs |= cell->refs != NULL; if (x) { ds_put_char(s, ' '); } ds_put_format(s, "%-*.*s", widths[x], widths[x], text); } table_finish_line(s); + if (has_refs) { + for (x = 0; x < table->n_columns; x++) { + table_print_cell_refs__(table_cell__(table, y, x), + table->columns[x].heading, style, + 2, s); + } + } } free(widths); @@ -321,8 +459,6 @@ static void table_print_list__(const struct table *table, const struct table_style *style, struct ds *s) { - size_t x, y; - if (first_table) { first_table = false; } else { @@ -335,17 +471,11 @@ table_print_list__(const struct table *table, const struct table_style *style, ds_put_format(s, "%s\n", table->caption); } - for (y = 0; y < table->n_rows; y++) { + for (size_t y = 0; y < table->n_rows; y++) { if (y > 0) { ds_put_char(s, '\n'); } - for (x = 0; x < table->n_columns; x++) { - const char *text = cell_to_text(table_cell__(table, y, x), style); - if (style->headings) { - ds_put_format(s, "%-20s: ", table->columns[x].heading); - } - ds_put_format(s, "%s\n", text); - } + table_print_list_row__(table, style, y, 0, s); } } @@ -510,6 +640,39 @@ table_print_csv__(const struct table *table, const struct table_style *style, } } +/* Wraps expanded cells without replacing their native OVSDB datum. */ +static struct json * +cell_to_json(const struct cell *cell) +{ + const struct cell_refs *refs = cell->refs; + if (!refs) { + if (cell->text) { + return json_string_create(cell->text); + } + return cell->json ? json_clone(cell->json) : json_null_create(); + } + + struct json *json = json_object_create(); + json_object_put(json, "value", json_clone(cell->json)); + struct json *array = json_array_create_empty(); + for (size_t i = 0; i < refs->n; i++) { + const struct cell_ref *ref = &refs->items[i]; + struct json *entry = json_object_create(); + json_object_put(entry, "index", json_integer_create(ref->index)); + json_object_put_string(entry, "side", ref->is_value ? "value" : "key"); + json_object_put_string(entry, "table", ref->table_name); + struct json *row = json_object_create(); + for (size_t x = 0; x < ref->row->n_columns; x++) { + json_object_put(row, ref->row->columns[x].heading, + cell_to_json(table_cell__(ref->row, 0, x))); + } + json_object_put(entry, "row", row); + json_array_add(array, entry); + } + json_object_put(json, "refs", array); + return json; +} + static void table_print_json__(const struct table *table, const struct table_style *style, struct ds *s) @@ -538,14 +701,7 @@ table_print_json__(const struct table *table, const struct table_style *style, for (y = 0; y < table->n_rows; y++) { struct json *row = json_array_create_empty(); for (x = 0; x < table->n_columns; x++) { - const struct cell *cell = table_cell__(table, y, x); - if (cell->text) { - json_array_add(row, json_string_create(cell->text)); - } else if (cell->json) { - json_array_add(row, json_clone(cell->json)); - } else { - json_array_add(row, json_null_create()); - } + json_array_add(row, cell_to_json(table_cell__(table, y, x))); } json_array_add(data, row); } diff --git a/lib/table.h b/lib/table.h index 33263e2a2..d26dc6a69 100644 --- a/lib/table.h +++ b/lib/table.h @@ -22,6 +22,7 @@ #include "compiler.h" #include "openvswitch/json.h" +struct cell_refs; struct ds; struct table_style; @@ -55,9 +56,13 @@ struct cell { /* JSON. */ struct json *json; const struct ovsdb_type *type; + + struct cell_refs *refs; /* Owned expansions of the typed datum. */ }; struct cell *table_add_cell(struct table *); +void table_cell_add_ref(struct cell *, unsigned int index, bool is_value, + const char *table_name, struct table *row); /* Table formatting. */ diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c index 5f7b3050b..5941aabfe 100644 --- a/utilities/ovs-vsctl.c +++ b/utilities/ovs-vsctl.c @@ -168,8 +168,9 @@ main(int argc, char *argv[]) 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); + char *error = ctl_parse_commands_with_style(argc - optind, argv + optind, + &local_options, &table_style, + &commands, &n_commands); if (error) { ctl_fatal("%s", error); } diff --git a/vtep/vtep-ctl.c b/vtep/vtep-ctl.c index 880c95f0e..e42e1f180 100644 --- a/vtep/vtep-ctl.c +++ b/vtep/vtep-ctl.c @@ -111,8 +111,9 @@ main(int argc, char *argv[]) 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); + char *error = ctl_parse_commands_with_style(argc - optind, argv + optind, + &local_options, &table_style, + &commands, &n_commands); if (error) { ctl_fatal("%s", error); } -- 2.56.0 _______________________________________________ dev mailing list [email protected] https://mail.openvswitch.org/mailman/listinfo/ovs-dev
