ovsctl这个应用程序主要职责是根据用户的命令和ovsdb沟通,将配置信息更新到数据库中,而vswitchd会在需要重新配置的时候和ovsdb打交道,而后和内核datapath通信执行真正的动作(通过netlink传递)。这里规定了命令的语法格式(vsctl_command_syntax )以及所支持的所有命令,这里主要看add-port相关的。OVS_VPORT_CMD_NEW到达内核后的处理流程图见 图1.
图 1
struct
vsctl_command_syntax {
const char *name; /* e.g. "add-br" */
int min_args; /* Min number of arguments following name. */
int max_args; /* Max number of arguments following name. */
void (*prerequisites)(struct vsctl_context *ctx);
//如果非空调用 ovsdb_idl_add_column() 或 ovsdb_idl_add_table() 为下面插入数据占位( ??);
const char *name; /* e.g. "add-br" */
int min_args; /* Min number of arguments following name. */
int max_args; /* Max number of arguments following name. */
void (*prerequisites)(struct vsctl_context *ctx);
//如果非空调用 ovsdb_idl_add_column() 或 ovsdb_idl_add_table() 为下面插入数据占位( ??);
void (*run)(struct vsctl_context *ctx);
//执行这个命令对应的事务,并且将命令的输出(如果有)存入ctx->output或ctx->table;如果这个命令执行的先决条件未得 //到满足,要等待时机而后重新尝试,会设置 ctx->try_again= true(现在只有 "wait-until"命令这样做)
void (*postprocess)(struct vsctl_context *ctx);
//如果非空,在事物完成后调用这个函数进行后续处理, ctx->output是从run()执行后的输出,现在只有"create" 命令有postprocessing;
const char *options; // 用逗号分开的选项,形如"--a,--b"
enum { RO, RW } mode; //标识这个命令是否修改OVSDB
};
const char *options; // 用逗号分开的选项,形如"--a,--b"
enum { RO, RW } mode; //标识这个命令是否修改OVSDB
};
static const struct vsctl_command_syntax
all_commands[] = {
/* Open vSwitch commands. */
{"init", 0, 0, NULL, cmd_init, NULL, "", RW},
{"show", 0, 0, pre_cmd_show, cmd_show, NULL, "", RO},
/* Bridge commands. */
{"add-br", 1, 3, pre_get_info, cmd_add_br, NULL, "--may-exist", RW},
{"del-br", 1, 1, pre_get_info, cmd_del_br, NULL, "--if-exists", RW},
{"list-br", 0, 0, pre_get_info, cmd_list_br, NULL, "--real,--fake", RO},
{"br-exists", 1, 1, pre_get_info, cmd_br_exists, NULL, "", RO},
{"br-to-vlan", 1, 1, pre_get_info, cmd_br_to_vlan, NULL, "", RO},
{"br-to-parent", 1, 1, pre_get_info, cmd_br_to_parent, NULL, "", RO},
{"br-set-external-id", 2, 3, pre_cmd_br_set_external_id, cmd_br_set_external_id, NULL, "", RW},
{"br-get-external-id", 1, 2, pre_cmd_br_get_external_id, cmd_br_get_external_id, NULL, "", RO},
/* Port commands. */
{"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
{"add-port", 2, INT_MAX, pre_get_info, cmd_add_port, NULL, "--may-exist", RW},
{"add-bond", 4, INT_MAX, pre_get_info, cmd_add_bond, NULL, "--may-exist,--fake-iface", RW},
{"del-port", 1, 2, pre_get_info, cmd_del_port, NULL, "--if-exists,--with-iface", RW},
{"port-to-br", 1, 1, pre_get_info, cmd_port_to_br, NULL, "", RO},
/* Interface commands. */
{"list-ifaces", 1, 1, pre_get_info, cmd_list_ifaces, NULL, "", RO},
{"iface-to-br", 1, 1, pre_get_info, cmd_iface_to_br, NULL, "", RO},
/* Controller commands. */
{"get-controller", 1, 1, pre_controller, cmd_get_controller, NULL, "", RO},
{"del-controller", 1, 1, pre_controller, cmd_del_controller, NULL, "", RW},
{"set-controller", 1, INT_MAX, pre_controller, cmd_set_controller, NULL,
"", RW},
{"get-fail-mode", 1, 1, pre_get_info, cmd_get_fail_mode, NULL, "", RO},
{"del-fail-mode", 1, 1, pre_get_info, cmd_del_fail_mode, NULL, "", RW},
{"set-fail-mode", 2, 2, pre_get_info, cmd_set_fail_mode, NULL, "", RW},
/* Manager commands. */
{"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO},
{"del-manager", 0, 0, pre_manager, cmd_del_manager, NULL, "", RW},
{"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW},
/* SSL commands. */
{"get-ssl", 0, 0, pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
{"del-ssl", 0, 0, pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
{"set-ssl", 3, 3, pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW},
/* Switch commands. */
{"emer-reset", 0, 0, pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
/* Database commands. */
{"comment", 0, INT_MAX, NULL, NULL, NULL, "", RO},
{"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
{"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL, "--columns=", RO},
{"find", 1, INT_MAX, pre_cmd_find, cmd_find, NULL, "--columns=", RO},
{"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "", RW},
{"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "", RW},
{"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "", RW},
{"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "", RW},
{"create", 2, INT_MAX, pre_create, cmd_create, post_create, "--id=", RW},
{"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL, "--if-exists,--all", RW},
{"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "", RO},
{NULL, 0, 0, NULL, NULL, NULL, NULL, RO},
/* Open vSwitch commands. */
{"init", 0, 0, NULL, cmd_init, NULL, "", RW},
{"show", 0, 0, pre_cmd_show, cmd_show, NULL, "", RO},
/* Bridge commands. */
{"add-br", 1, 3, pre_get_info, cmd_add_br, NULL, "--may-exist", RW},
{"del-br", 1, 1, pre_get_info, cmd_del_br, NULL, "--if-exists", RW},
{"list-br", 0, 0, pre_get_info, cmd_list_br, NULL, "--real,--fake", RO},
{"br-exists", 1, 1, pre_get_info, cmd_br_exists, NULL, "", RO},
{"br-to-vlan", 1, 1, pre_get_info, cmd_br_to_vlan, NULL, "", RO},
{"br-to-parent", 1, 1, pre_get_info, cmd_br_to_parent, NULL, "", RO},
{"br-set-external-id", 2, 3, pre_cmd_br_set_external_id, cmd_br_set_external_id, NULL, "", RW},
{"br-get-external-id", 1, 2, pre_cmd_br_get_external_id, cmd_br_get_external_id, NULL, "", RO},
/* Port commands. */
{"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
{"add-port", 2, INT_MAX, pre_get_info, cmd_add_port, NULL, "--may-exist", RW},
{"add-bond", 4, INT_MAX, pre_get_info, cmd_add_bond, NULL, "--may-exist,--fake-iface", RW},
{"del-port", 1, 2, pre_get_info, cmd_del_port, NULL, "--if-exists,--with-iface", RW},
{"port-to-br", 1, 1, pre_get_info, cmd_port_to_br, NULL, "", RO},
/* Interface commands. */
{"list-ifaces", 1, 1, pre_get_info, cmd_list_ifaces, NULL, "", RO},
{"iface-to-br", 1, 1, pre_get_info, cmd_iface_to_br, NULL, "", RO},
/* Controller commands. */
{"get-controller", 1, 1, pre_controller, cmd_get_controller, NULL, "", RO},
{"del-controller", 1, 1, pre_controller, cmd_del_controller, NULL, "", RW},
{"set-controller", 1, INT_MAX, pre_controller, cmd_set_controller, NULL,
"", RW},
{"get-fail-mode", 1, 1, pre_get_info, cmd_get_fail_mode, NULL, "", RO},
{"del-fail-mode", 1, 1, pre_get_info, cmd_del_fail_mode, NULL, "", RW},
{"set-fail-mode", 2, 2, pre_get_info, cmd_set_fail_mode, NULL, "", RW},
/* Manager commands. */
{"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO},
{"del-manager", 0, 0, pre_manager, cmd_del_manager, NULL, "", RW},
{"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW},
/* SSL commands. */
{"get-ssl", 0, 0, pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
{"del-ssl", 0, 0, pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
{"set-ssl", 3, 3, pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW},
/* Switch commands. */
{"emer-reset", 0, 0, pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
/* Database commands. */
{"comment", 0, INT_MAX, NULL, NULL, NULL, "", RO},
{"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
{"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL, "--columns=", RO},
{"find", 1, INT_MAX, pre_cmd_find, cmd_find, NULL, "--columns=", RO},
{"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "", RW},
{"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "", RW},
{"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "", RW},
{"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "", RW},
{"create", 2, INT_MAX, pre_create, cmd_create, post_create, "--id=", RW},
{"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL, "--if-exists,--all", RW},
{"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "", RO},
{NULL, 0, 0, NULL, NULL, NULL, NULL, RO},
};
static void
cmd_add_port(struct vsctl_context *ctx) {
bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
//看vsctl_context->options 中是否有 --may-exist选项;
add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false, &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
//看vsctl_context->options 中是否有 --may-exist选项;
add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false, &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
}
static void
add_port(struct vsctl_context *ctx, const char *br_name, const char *port_name, bool may_exist,
bool fake_iface, char *iface_names[], int n_ifaces, char *settings[], int n_settings) {
struct vsctl_port *vsctl_port;
struct vsctl_bridge *bridge;
struct ovsrec_interface **ifaces;
struct ovsrec_port *port;
size_t i;
vsctl_context_populate_cache(ctx);
if (may_exist) {
struct vsctl_port *vsctl_port;
vsctl_port = find_port(ctx, port_name, false);
if (vsctl_port) {
struct svec want_names, have_names;
svec_init(&want_names);
for (i = 0; i < n_ifaces; i++) {
svec_add(&want_names, iface_names[i]);
}
svec_sort(&want_names);
svec_init(&have_names);
for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
svec_add(&have_names, vsctl_port->port_cfg->interfaces[i]->name);
}
svec_sort(&have_names);
if (strcmp(vsctl_port->bridge->name, br_name)) {
char *command = vsctl_context_to_string(ctx);
vsctl_fatal("\"%s\" but %s is actually attached to bridge %s", command, port_name, vsctl_port->bridge->name);
}
if (!svec_equal(&want_n
struct vsctl_port *vsctl_port;
struct vsctl_bridge *bridge;
struct ovsrec_interface **ifaces;
struct ovsrec_port *port;
size_t i;
vsctl_context_populate_cache(ctx);
if (may_exist) {
struct vsctl_port *vsctl_port;
vsctl_port = find_port(ctx, port_name, false);
if (vsctl_port) {
struct svec want_names, have_names;
svec_init(&want_names);
for (i = 0; i < n_ifaces; i++) {
svec_add(&want_names, iface_names[i]);
}
svec_sort(&want_names);
svec_init(&have_names);
for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
svec_add(&have_names, vsctl_port->port_cfg->interfaces[i]->name);
}
svec_sort(&have_names);
if (strcmp(vsctl_port->bridge->name, br_name)) {
char *command = vsctl_context_to_string(ctx);
vsctl_fatal("\"%s\" but %s is actually attached to bridge %s", command, port_name, vsctl_port->bridge->name);
}
if (!svec_equal(&want_n