上两篇分析了ofservice的建立,请求处理的过程,本篇分析ovs-ofctl命令行是如何和 ofservice交互的。
1、mian函数
int
main(int argc, char *argv[])
{
struct ovs_cmdl_context ctx = { .argc = 0, };
set_program_name(argv[0]);
service_start(&argc, &argv);
parse_options(argc, argv);
fatal_ignore_sigpipe();
ctx.argc = argc - optind;
ctx.argv = argv + optind;
daemon_become_new_user(false);
ovs_cmdl_run_command(&ctx, get_all_commands()); //处理ovs-ofctl命令行
return 0;
}
2、ovs_cmdl_run_command函数
void
ovs_cmdl_run_command(struct ovs_cmdl_context *ctx, const struct ovs_cmdl_command commands[])
{
const struct ovs_cmdl_command *p;
if (ctx->argc < 1) {
ovs_fatal(0, "missing command name; use --help for help");
}
for (p = commands; p->name != NULL; p++) {
if (!strcmp(p->name, ctx->argv[0])) {
int n_arg = ctx->argc - 1;
if (n_arg < p->min_args) {
VLOG_FATAL( "'%s' command requires at least %d arguments",
p->name, p->min_args);
} else if (n_arg > p->max_args) {
VLOG_FATAL("'%s' command takes at most %d arguments",
p->name, p->max_args);
} else {
p->handler(ctx); //OVS注册多个command处理,以添加流表为例,实际会掉用ofctl_add_flow函数
if (ferror(stdout)) {
VLOG_FATAL("write to stdout failed");
}
if