📄 hypervisor.c
字号:
/* * Cisco 7200 (Predator) simulation platform. * Copyright (c) 2006 Christophe Fillot (cf@utc.fr) * * Hypervisor routines. *//* By default, Cygwin supports only 64 FDs with select()! */#ifdef __CYGWIN__#define FD_SETSIZE 1024#endif#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/mman.h>#include <signal.h>#include <fcntl.h>#include <errno.h>#include <assert.h>#include <stdarg.h>#include <sys/ioctl.h>#include <sys/types.h>#include <sys/socket.h>#include <arpa/inet.h>#include <pthread.h>#include "utils.h"#include "parser.h"#include "net.h"#include "registry.h"#include "mips64.h"#include "dynamips.h"#include "dev_c7200.h"#include "dev_c3600.h"#include "dev_c2691.h"#include "dev_c3725.h"#include "dev_c3745.h"#include "hypervisor.h"#include "net_io.h"#include "net_io_bridge.h"#include "frame_relay.h"#include "atm.h"#define DEBUG_TOKEN 0/* Hypervisor modules */static hypervisor_module_t *module_list = NULL;static volatile int hypervisor_running = 0;/* Hypervisor connection list */static hypervisor_conn_t *hypervisor_conn_list = NULL;/* Show hypervisor version */static int cmd_version(hypervisor_conn_t *conn,int argc,char *argv[]){ hypervisor_send_reply(conn,HSC_INFO_OK,1,"%s",sw_version); return(0);}/* Parser test */static int cmd_parser_test(hypervisor_conn_t *conn,int argc,char *argv[]){ int i; for(i=0;i<argc;i++) hypervisor_send_reply(conn,HSC_INFO_MSG,0, "arg %d (len %u): \"%s\"", i,strlen(argv[i]),argv[i]); hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); return(0);}/* Show hypervisor module list */static int cmd_mod_list(hypervisor_conn_t *conn,int argc,char *argv[]){ hypervisor_module_t *m; for(m=module_list;m;m=m->next) hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s",m->name); hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); return(0);}/* Show module command list */static int cmd_modcmd_list(hypervisor_conn_t *conn,int argc,char *argv[]){ hypervisor_module_t *m; hypervisor_cmd_t *cmd; if (!(m = hypervisor_find_module(argv[0]))) { hypervisor_send_reply(conn,HSC_ERR_UNK_MODULE,1,"unknown module '%s'", argv[0]); return(-1); } for(cmd=m->cmd_list;cmd;cmd=cmd->next) hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s (min/max args: %d/%d)", cmd->name,cmd->min_param,cmd->max_param); hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); return(0);}/* Set working directory */static int cmd_set_working_dir(hypervisor_conn_t *conn,int argc,char *argv[]){ if (chdir(argv[0]) == -1) { hypervisor_send_reply(conn,HSC_ERR_INV_PARAM,1, "chdir: %s",strerror(errno)); } else { hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); } return(0);}/* Save the hypervisor configuration in the specified file */static int cmd_save_config(hypervisor_conn_t *conn,int argc,char *argv[]){ FILE *fd; if (!(fd = fopen(argv[0],"w"))) { hypervisor_send_reply(conn,HSC_ERR_FILE,1,"fopen: %s",strerror(errno)); return(-1); } /* Save configuration for all objects */ netio_save_config_all(fd); frsw_save_config_all(fd); atmsw_save_config_all(fd); netio_bridge_save_config_all(fd); c7200_save_config_all(fd); c3600_save_config_all(fd); c2691_save_config_all(fd); c3725_save_config_all(fd); c3745_save_config_all(fd); hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); return(0);}/* Reset hypervisor (delete all objects) */static int cmd_reset(hypervisor_conn_t *conn,int argc,char *argv[]){ dynamips_reset(); hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); return(0);}/* Close connection */static int cmd_close(hypervisor_conn_t *conn,int argc,char *argv[]){ hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); conn->active = FALSE; return(0);}/* Stop hypervisor */static int cmd_stop(hypervisor_conn_t *conn,int argc,char *argv[]){ hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); hypervisor_running = FALSE; return(0);}/* Hypervisor commands */static hypervisor_cmd_t hypervisor_cmd_array[] = { { "version", 0, 0, cmd_version, NULL }, { "parser_test", 0, 10, cmd_parser_test, NULL }, { "module_list", 0, 0, cmd_mod_list, NULL }, { "cmd_list", 1, 1, cmd_modcmd_list, NULL }, { "working_dir", 1, 1, cmd_set_working_dir, NULL }, { "save_config", 1, 1, cmd_save_config, NULL }, { "reset", 0, 0, cmd_reset, NULL }, { "close", 0, 0, cmd_close, NULL }, { "stop", 0, 0, cmd_stop, NULL }, { NULL, -1, -1, NULL, NULL },};/* Send a reply */int hypervisor_send_reply(hypervisor_conn_t *conn,int code,int done, char *format,...){ va_list ap; size_t n = 0; if (conn != NULL) { va_start(ap,format); n += fprintf(conn->out,"%3d%s",code,(done)?"-":" "); n += vfprintf(conn->out,format,ap); n += fprintf(conn->out,"\r\n"); fflush(conn->out); va_end(ap); } return(n);}/* Find a module */hypervisor_module_t *hypervisor_find_module(char *name){ hypervisor_module_t *m; for(m=module_list;m;m=m->next) if (!strcmp(m->name,name)) return m; return NULL;}/* Find a command in a module */hypervisor_cmd_t *hypervisor_find_cmd(hypervisor_module_t *module,char *name){ hypervisor_cmd_t *cmd; for(cmd=module->cmd_list;cmd;cmd=cmd->next) if (!strcmp(cmd->name,name)) return cmd; return NULL;}/* Find an object in the registry */void *hypervisor_find_object(hypervisor_conn_t *conn,char *name,int obj_type){ void *p; if (!(p = registry_find(name,obj_type))) { hypervisor_send_reply(conn,HSC_ERR_UNK_OBJ,1, "unable to find object '%s'",name); return NULL; } return p;}/* Find a VM in the registry */void *hypervisor_find_vm(hypervisor_conn_t *conn,char *name,int vm_type){ vm_instance_t *vm; if (!(vm = vm_acquire(name))) { hypervisor_send_reply(conn,HSC_ERR_UNK_OBJ,1, "unable to find VM '%s'",name); return NULL; } if (vm->type != vm_type) { vm_release(vm); hypervisor_send_reply(conn,HSC_ERR_BAD_OBJ,1, "VM '%s' is not a VM type %d", name,vm_type); return NULL; } return vm;}/* Register a module */hypervisor_module_t *hypervisor_register_module(char *name){ hypervisor_module_t *m; if (hypervisor_find_module(name) != NULL) { fprintf(stderr,"Hypervisor: module '%s' already exists.\n",name); return NULL; } if (!(m = malloc(sizeof(*m)))) { fprintf(stderr,"Hypervisor: unable to register new module.\n"); return NULL; } m->name = name; m->cmd_list = NULL; m->next = module_list; module_list = m; return m;}/* Register a list of commands */int hypervisor_register_cmd_list(hypervisor_module_t *module, hypervisor_cmd_t *cmd_list){ hypervisor_cmd_t *cmd = cmd_list; while(cmd->next != NULL) cmd = cmd->next; cmd->next = module->cmd_list; module->cmd_list = cmd_list; return(0);}/* Register an array of commands */int hypervisor_register_cmd_array(hypervisor_module_t *module, hypervisor_cmd_t *cmd_array){ hypervisor_cmd_t *cmd; for(cmd=cmd_array;cmd->name!=NULL;cmd++) { cmd->next = module->cmd_list; module->cmd_list = cmd; } return(0);}/* Locate the module and execute command */static int hypervisor_exec_cmd(hypervisor_conn_t *conn, char *mod_name,char *cmd_name, int argc,char *argv[]){ hypervisor_module_t *module; hypervisor_cmd_t *cmd;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -