/*
- * Copyright (c) 2015 Nicira, Inc.
+ * Copyright (c) 2015, 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.
#define DB_CTL_BASE_H 1
#include "compiler.h"
-#include "dynamic-string.h"
-#include "shash.h"
+#include "openvswitch/dynamic-string.h"
+#include "openvswitch/shash.h"
struct ctl_context;
struct option;
* (structs, commands and functions). To utilize this module, user must
* define the following:
*
- * - the 'cmd_show_tables'. (See 'struct cmd_show_table' for more info).
- *
* - the command syntaxes for each command. (See 'struct ctl_command_syntax'
* for more info) and regiters them using ctl_register_commands().
*
#define ovs_fatal please_use_ctl_fatal_instead_of_ovs_fatal
struct ctl_table_class;
-void ctl_init(const struct ctl_table_class *tables,
- void (*ctl_exit_func)(int status));
+struct ovsdb_idl_class;
+struct ovsdb_idl_table_class;
+struct cmd_show_table;
+
+/* ctl_init() figures out the number of tables on its own and flags an error if
+ * 'ctl_classes' was defined with the wrong number of elements. */
+#define ctl_init(idl_class, table_classes, ctl_classes, cmd_show_table, \
+ ctl_exit_func) \
+ (BUILD_ASSERT(ARRAY_SIZE(table_classes) == ARRAY_SIZE(ctl_classes)), \
+ ctl_init__(idl_class, ctl_classes, cmd_show_table, ctl_exit_func))
+void ctl_init__(const struct ovsdb_idl_class *, const struct ctl_table_class *,
+ const struct cmd_show_table *cmd_show_tables,
+ void (*ctl_exit_func)(int status));
char *ctl_default_db(void);
+void ctl_error(struct ctl_context *, const char *, ...)
+OVS_PRINTF_FORMAT(2, 3);
OVS_NO_RETURN void ctl_fatal(const char *, ...) OVS_PRINTF_FORMAT(1, 2);
/* *ctl command syntax structure, to be defined by each command implementation.
void (*postprocess)(struct ctl_context *ctx);
/* A comma-separated list of supported options, e.g. "--a,--b", or the
- * empty string if the command does not support any options. */
+ * empty string if the command does not support any options.
+ *
+ * Arguments are determined by appending special characters to option
+ * names:
+ *
+ * - Append "=" (e.g. "--id=") for a required argument.
+ *
+ * - Append "?" (e.g. "--ovs?") for an optional argument.
+ *
+ * - Otherwise an option does not accept an argument. */
const char *options;
- enum { RO, RW } mode; /* Does this command modify the database? */
+ enum { RO, RW } mode; /* Does this command modify the database? */
};
/* A command extracted from command-line input plus the structs for
struct table *table;
};
-bool ctl_might_write_to_db(char **argv);
+bool ctl_might_write_to_db(const struct ctl_command *, size_t n);
const char *ctl_get_db_cmd_usage(void);
+
+const char *ctl_list_db_tables_usage(void);
void ctl_print_commands(void);
void ctl_print_options(const struct option *);
void ctl_add_cmd_options(struct option **, size_t *n_options_p,
size_t *allocated_options_p, int opt_val);
void ctl_register_commands(const struct ctl_command_syntax *);
-struct ctl_command *ctl_parse_commands(int argc, char *argv[],
- struct shash *local_options,
- size_t *n_commandsp);
+char * OVS_WARN_UNUSED_RESULT ctl_parse_commands(
+ int argc, char *argv[], struct shash *local_options,
+ 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'
+ * should be used and user must define all variables. */
+struct weak_ref_table {
+ const struct ovsdb_idl_table_class *table;
+ const struct ovsdb_idl_column *name_column;
+ /* This colum must be a weak reference to the owning
+ * 'struct cmd_show_table''s table row. */
+ const struct ovsdb_idl_column *wref_column;
+};
/* This struct is for organizing the 'show' command output where:
*
*
* - 'columns[]' allows user to specify the print of additional columns
* in 'table'.
+ *
+ * - if 'wref_table' is populated, print 'wref_table.name_column' for
+ * each row in table 'wref_table.table' that has a reference to 'table'
+ * in 'wref_table.wref_column'. Every field must be populated.
+ *
* */
struct cmd_show_table {
const struct ovsdb_idl_table_class *table;
const struct ovsdb_idl_column *name_column;
- const struct ovsdb_idl_column *columns[3]; /* Seems like a good number. */
+ const struct ovsdb_idl_column *columns[4]; /* Seems like a good number. */
+ const struct weak_ref_table wref_table;
};
-/* 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.
- * */
-extern struct cmd_show_table cmd_show_tables[];
-
\f
/* The base context struct for conducting the common database
* operations (commands listed in 'db_ctl_commands'). User should
struct shash options;
/* Modifiable state. */
+ char *error;
struct ds output;
struct table *table;
struct ovsdb_idl *idl;
/* For implementation with a cache of the contents of the database,
* this function will be called when the database is changed and the
* change makes the cache no longer valid. */
- void (*invalidate_cache)(struct ctl_context *);
+ void (*invalidate_cache_cb)(struct ctl_context *);
/* A command may set this member to true if some prerequisite is not met
* and the caller should wait for something to change and then retry. */
void ctl_context_done_command(struct ctl_context *, struct ctl_command *);
void ctl_context_done(struct ctl_context *, struct ctl_command *);
+/* A way to identify a particular row in the database based on a user-provided
+ * string. If all fields are NULL, the struct is ignored. Otherwise,
+ * 'name_column' designates a column whose table is searched for rows that
+ * match with the user string. If 'key' is NULL, then 'name_column' should be
+ * a string or integer-valued column; otherwise it should be a map from a
+ * string to one of those types and the value corresponding to 'key' is what is
+ * matched. If a matching row is found, then:
+ *
+ * - If 'uuid_column' is NULL, the matching row is the final row.
+ *
+ * - Otherwise 'uuid_column' must designate a UUID-typed column whose value
+ * refers to exactly one row, which is the final row.
+ */
struct ctl_row_id {
- const struct ovsdb_idl_table_class *table;
const struct ovsdb_idl_column *name_column;
+ const char *key;
const struct ovsdb_idl_column *uuid_column;
};
struct ctl_table_class {
- struct ovsdb_idl_table_class *class;
- struct ctl_row_id row_ids[2];
+ struct ctl_row_id row_ids[4];
};
-void ctl_set_column(const char *table_name,
- const struct ovsdb_idl_row *, const char *arg,
- struct ovsdb_symbol_table *);
+char *ctl_get_row(struct ctl_context *, const struct ovsdb_idl_table_class *,
+ const char *record_id, bool must_exist,
+ const struct ovsdb_idl_row **);
+
+char *ctl_set_column(const char *table_name, const struct ovsdb_idl_row *,
+ const char *arg, struct ovsdb_symbol_table *);
#endif /* db-ctl-base.h */