| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2006 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* Hypervisor routines. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "dynamips_common.h" |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <unistd.h> |
| 13 |
#include <string.h> |
| 14 |
#include <sys/types.h> |
| 15 |
#include <sys/stat.h> |
| 16 |
#include <signal.h> |
| 17 |
#include <fcntl.h> |
| 18 |
#include <errno.h> |
| 19 |
#include <assert.h> |
| 20 |
#include <stdarg.h> |
| 21 |
#include <sys/ioctl.h> |
| 22 |
#include <sys/types.h> |
| 23 |
#include <sys/socket.h> |
| 24 |
#include <arpa/inet.h> |
| 25 |
#include <pthread.h> |
| 26 |
|
| 27 |
#include "utils.h" |
| 28 |
#include "parser.h" |
| 29 |
#include "net.h" |
| 30 |
#include "registry.h" |
| 31 |
#include "cpu.h" |
| 32 |
#include "vm.h" |
| 33 |
#include "dynamips.h" |
| 34 |
#include "dev_c7200.h" |
| 35 |
#include "dev_c3600.h" |
| 36 |
#include "dev_c2691.h" |
| 37 |
#include "dev_c3725.h" |
| 38 |
#include "dev_c3745.h" |
| 39 |
#include "dev_c2600.h" |
| 40 |
#include "dev_c1700.h" |
| 41 |
#include "hypervisor.h" |
| 42 |
#include "net_io.h" |
| 43 |
#include "net_io_bridge.h" |
| 44 |
#include "frame_relay.h" |
| 45 |
#include "atm.h" |
| 46 |
|
| 47 |
#define DEBUG_TOKEN 0 |
| 48 |
|
| 49 |
/* Hypervisor modules */ |
| 50 |
static hypervisor_module_t *module_list = NULL; |
| 51 |
static volatile int hypervisor_running = 0; |
| 52 |
|
| 53 |
/* Hypervisor connection list */ |
| 54 |
static hypervisor_conn_t *hypervisor_conn_list = NULL; |
| 55 |
|
| 56 |
/* Show hypervisor version */ |
| 57 |
static int cmd_version(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 58 |
{ |
| 59 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"%s",sw_version); |
| 60 |
return(0); |
| 61 |
} |
| 62 |
|
| 63 |
/* Parser test */ |
| 64 |
static int cmd_parser_test(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 65 |
{ |
| 66 |
int i; |
| 67 |
|
| 68 |
for(i=0;i<argc;i++) |
| 69 |
hypervisor_send_reply(conn,HSC_INFO_MSG,0, |
| 70 |
"arg %d (len %lu): \"%s\"", |
| 71 |
i,strlen(argv[i]),argv[i]); |
| 72 |
|
| 73 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
| 74 |
return(0); |
| 75 |
} |
| 76 |
|
| 77 |
/* Show hypervisor module list */ |
| 78 |
static int cmd_mod_list(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 79 |
{ |
| 80 |
hypervisor_module_t *m; |
| 81 |
|
| 82 |
for(m=module_list;m;m=m->next) |
| 83 |
hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s",m->name); |
| 84 |
|
| 85 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
| 86 |
return(0); |
| 87 |
} |
| 88 |
|
| 89 |
/* Show module command list */ |
| 90 |
static int cmd_modcmd_list(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 91 |
{ |
| 92 |
hypervisor_module_t *m; |
| 93 |
hypervisor_cmd_t *cmd; |
| 94 |
|
| 95 |
if (!(m = hypervisor_find_module(argv[0]))) { |
| 96 |
hypervisor_send_reply(conn,HSC_ERR_UNK_MODULE,1,"unknown module '%s'", |
| 97 |
argv[0]); |
| 98 |
return(-1); |
| 99 |
} |
| 100 |
|
| 101 |
for(cmd=m->cmd_list;cmd;cmd=cmd->next) |
| 102 |
hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s (min/max args: %d/%d)", |
| 103 |
cmd->name,cmd->min_param,cmd->max_param); |
| 104 |
|
| 105 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
| 106 |
return(0); |
| 107 |
} |
| 108 |
|
| 109 |
/* Set working directory */ |
| 110 |
static int cmd_set_working_dir(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 111 |
{ |
| 112 |
if (chdir(argv[0]) == -1) { |
| 113 |
hypervisor_send_reply(conn,HSC_ERR_INV_PARAM,1, |
| 114 |
"chdir: %s",strerror(errno)); |
| 115 |
} else { |
| 116 |
m_log("GENERAL","working_dir=%s\n",argv[0]); |
| 117 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
| 118 |
} |
| 119 |
return(0); |
| 120 |
} |
| 121 |
|
| 122 |
/* Save the hypervisor configuration in the specified file */ |
| 123 |
static int cmd_save_config(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 124 |
{ |
| 125 |
FILE *fd; |
| 126 |
|
| 127 |
if (!(fd = fopen(argv[0],"w"))) { |
| 128 |
hypervisor_send_reply(conn,HSC_ERR_FILE,1,"fopen: %s",strerror(errno)); |
| 129 |
return(-1); |
| 130 |
} |
| 131 |
|
| 132 |
/* Save configuration for all objects */ |
| 133 |
netio_save_config_all(fd); |
| 134 |
frsw_save_config_all(fd); |
| 135 |
atmsw_save_config_all(fd); |
| 136 |
//atm_bridge_save_config_all(fd); |
| 137 |
netio_bridge_save_config_all(fd); |
| 138 |
vm_save_config_all(fd); |
| 139 |
|
| 140 |
fclose(fd); |
| 141 |
|
| 142 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
| 143 |
return(0); |
| 144 |
} |
| 145 |
|
| 146 |
/* Reset hypervisor (delete all objects) */ |
| 147 |
static int cmd_reset(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 148 |
{ |
| 149 |
dynamips_reset(); |
| 150 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
| 151 |
return(0); |
| 152 |
} |
| 153 |
|
| 154 |
/* Close connection */ |
| 155 |
static int cmd_close(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 156 |
{ |
| 157 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
| 158 |
conn->active = FALSE; |
| 159 |
return(0); |
| 160 |
} |
| 161 |
|
| 162 |
/* Stop hypervisor */ |
| 163 |
static int cmd_stop(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 164 |
{ |
| 165 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
| 166 |
hypervisor_running = FALSE; |
| 167 |
return(0); |
| 168 |
} |
| 169 |
|
| 170 |
/* Hypervisor commands */ |
| 171 |
static hypervisor_cmd_t hypervisor_cmd_array[] = { |
| 172 |
{ "version", 0, 0, cmd_version, NULL }, |
| 173 |
{ "parser_test", 0, 10, cmd_parser_test, NULL }, |
| 174 |
{ "module_list", 0, 0, cmd_mod_list, NULL }, |
| 175 |
{ "cmd_list", 1, 1, cmd_modcmd_list, NULL }, |
| 176 |
{ "working_dir", 1, 1, cmd_set_working_dir, NULL }, |
| 177 |
{ "save_config", 1, 1, cmd_save_config, NULL }, |
| 178 |
{ "reset", 0, 0, cmd_reset, NULL }, |
| 179 |
{ "close", 0, 0, cmd_close, NULL }, |
| 180 |
{ "stop", 0, 0, cmd_stop, NULL }, |
| 181 |
{ NULL, -1, -1, NULL, NULL }, |
| 182 |
}; |
| 183 |
|
| 184 |
/* Send a reply */ |
| 185 |
int hypervisor_send_reply(hypervisor_conn_t *conn,int code,int done, |
| 186 |
char *format,...) |
| 187 |
{ |
| 188 |
va_list ap; |
| 189 |
size_t n = 0; |
| 190 |
|
| 191 |
if (conn != NULL) { |
| 192 |
va_start(ap,format); |
| 193 |
n += fprintf(conn->out,"%3d%s",code,(done)?"-":" "); |
| 194 |
n += vfprintf(conn->out,format,ap); |
| 195 |
n += fprintf(conn->out,"\r\n"); |
| 196 |
fflush(conn->out); |
| 197 |
va_end(ap); |
| 198 |
} |
| 199 |
|
| 200 |
return(n); |
| 201 |
} |
| 202 |
|
| 203 |
/* Find a module */ |
| 204 |
hypervisor_module_t *hypervisor_find_module(char *name) |
| 205 |
{ |
| 206 |
hypervisor_module_t *m; |
| 207 |
|
| 208 |
for(m=module_list;m;m=m->next) |
| 209 |
if (!strcmp(m->name,name)) |
| 210 |
return m; |
| 211 |
|
| 212 |
return NULL; |
| 213 |
} |
| 214 |
|
| 215 |
/* Find a command in a module */ |
| 216 |
hypervisor_cmd_t *hypervisor_find_cmd(hypervisor_module_t *module,char *name) |
| 217 |
{ |
| 218 |
hypervisor_cmd_t *cmd; |
| 219 |
|
| 220 |
for(cmd=module->cmd_list;cmd;cmd=cmd->next) |
| 221 |
if (!strcmp(cmd->name,name)) |
| 222 |
return cmd; |
| 223 |
|
| 224 |
return NULL; |
| 225 |
} |
| 226 |
|
| 227 |
/* Find an object in the registry */ |
| 228 |
void *hypervisor_find_object(hypervisor_conn_t *conn,char *name,int obj_type) |
| 229 |
{ |
| 230 |
void *p; |
| 231 |
|
| 232 |
if (!(p = registry_find(name,obj_type))) { |
| 233 |
hypervisor_send_reply(conn,HSC_ERR_UNK_OBJ,1, |
| 234 |
"unable to find object '%s'",name); |
| 235 |
return NULL; |
| 236 |
} |
| 237 |
|
| 238 |
return p; |
| 239 |
} |
| 240 |
|
| 241 |
/* Find a VM in the registry */ |
| 242 |
void *hypervisor_find_vm(hypervisor_conn_t *conn,char *name) |
| 243 |
{ |
| 244 |
vm_platform_t *platform = conn->cur_module->opt; |
| 245 |
vm_instance_t *vm; |
| 246 |
|
| 247 |
if (!(vm = vm_acquire(name))) { |
| 248 |
hypervisor_send_reply(conn,HSC_ERR_UNK_OBJ,1, |
| 249 |
"unable to find VM '%s'",name); |
| 250 |
return NULL; |
| 251 |
} |
| 252 |
|
| 253 |
if (vm->platform != platform) { |
| 254 |
vm_release(vm); |
| 255 |
hypervisor_send_reply(conn,HSC_ERR_BAD_OBJ,1, |
| 256 |
"VM '%s' is not a VM type %s", |
| 257 |
name,platform->name); |
| 258 |
return NULL; |
| 259 |
} |
| 260 |
|
| 261 |
return vm; |
| 262 |
} |
| 263 |
|
| 264 |
/* Destroy module_list */ |
| 265 |
static void destroy_module_list(void) |
| 266 |
{ |
| 267 |
hypervisor_module_t *m, *next; |
| 268 |
|
| 269 |
for (m = module_list; m; m = next) { |
| 270 |
next = m->next; |
| 271 |
free(m); |
| 272 |
} |
| 273 |
module_list = NULL; |
| 274 |
} |
| 275 |
|
| 276 |
/* Register a module */ |
| 277 |
hypervisor_module_t *hypervisor_register_module(char *name,void *opt) |
| 278 |
{ |
| 279 |
hypervisor_module_t *m; |
| 280 |
|
| 281 |
if (hypervisor_find_module(name) != NULL) { |
| 282 |
fprintf(stderr,"Hypervisor: module '%s' already exists.\n",name); |
| 283 |
return NULL; |
| 284 |
} |
| 285 |
|
| 286 |
if (!(m = malloc(sizeof(*m)))) { |
| 287 |
fprintf(stderr,"Hypervisor: unable to register new module.\n"); |
| 288 |
return NULL; |
| 289 |
} |
| 290 |
|
| 291 |
if (!module_list) |
| 292 |
atexit(destroy_module_list); |
| 293 |
|
| 294 |
m->name = name; |
| 295 |
m->opt = opt; |
| 296 |
m->cmd_list = NULL; |
| 297 |
|
| 298 |
m->next = module_list; |
| 299 |
module_list = m; |
| 300 |
return m; |
| 301 |
} |
| 302 |
|
| 303 |
/* Register a list of commands */ |
| 304 |
int hypervisor_register_cmd_list(hypervisor_module_t *module, |
| 305 |
hypervisor_cmd_t *cmd_list) |
| 306 |
{ |
| 307 |
hypervisor_cmd_t *cmd = cmd_list; |
| 308 |
|
| 309 |
while(cmd->next != NULL) |
| 310 |
cmd = cmd->next; |
| 311 |
|
| 312 |
cmd->next = module->cmd_list; |
| 313 |
module->cmd_list = cmd_list; |
| 314 |
return(0); |
| 315 |
} |
| 316 |
|
| 317 |
/* Register an array of commands */ |
| 318 |
int hypervisor_register_cmd_array(hypervisor_module_t *module, |
| 319 |
hypervisor_cmd_t *cmd_array) |
| 320 |
{ |
| 321 |
hypervisor_cmd_t *cmd; |
| 322 |
|
| 323 |
for(cmd=cmd_array;cmd->name!=NULL;cmd++) { |
| 324 |
cmd->next = module->cmd_list; |
| 325 |
module->cmd_list = cmd; |
| 326 |
} |
| 327 |
|
| 328 |
return(0); |
| 329 |
} |
| 330 |
|
| 331 |
/* Locate the module and execute command */ |
| 332 |
static int hypervisor_exec_cmd(hypervisor_conn_t *conn, |
| 333 |
char *mod_name,char *cmd_name, |
| 334 |
int argc,char *argv[]) |
| 335 |
{ |
| 336 |
hypervisor_module_t *module; |
| 337 |
hypervisor_cmd_t *cmd; |
| 338 |
|
| 339 |
if (!(module = hypervisor_find_module(mod_name))) { |
| 340 |
hypervisor_send_reply(conn,HSC_ERR_UNK_MODULE,1,"Unknown module '%s'", |
| 341 |
mod_name); |
| 342 |
return(-1); |
| 343 |
} |
| 344 |
|
| 345 |
if (!(cmd = hypervisor_find_cmd(module,cmd_name))) { |
| 346 |
hypervisor_send_reply(conn,HSC_ERR_UNK_CMD,1,"Unknown command '%s'", |
| 347 |
cmd_name); |
| 348 |
return(-1); |
| 349 |
} |
| 350 |
|
| 351 |
if ((argc < cmd->min_param) || (argc > cmd->max_param)) { |
| 352 |
hypervisor_send_reply(conn,HSC_ERR_BAD_PARAM,1, |
| 353 |
"Bad number of parameters (%d with min/max=%d/%d)", |
| 354 |
argc,cmd->min_param,cmd->max_param); |
| 355 |
return(-1); |
| 356 |
} |
| 357 |
|
| 358 |
conn->cur_module = module; |
| 359 |
|
| 360 |
return(cmd->handler(conn,argc,argv)); |
| 361 |
} |
| 362 |
|
| 363 |
/* Thread for servicing connections */ |
| 364 |
static void *hypervisor_thread(void *arg) |
| 365 |
{ |
| 366 |
hypervisor_conn_t *conn = arg; |
| 367 |
char buffer[512],**tokens; |
| 368 |
parser_context_t ctx; |
| 369 |
int res; |
| 370 |
|
| 371 |
tokens = NULL; |
| 372 |
parser_context_init(&ctx); |
| 373 |
|
| 374 |
while(conn->active) { |
| 375 |
if (!fgets(buffer,sizeof(buffer),conn->in)) |
| 376 |
break; |
| 377 |
|
| 378 |
if (!*buffer) |
| 379 |
continue; |
| 380 |
|
| 381 |
/* Tokenize command line */ |
| 382 |
res = parser_scan_buffer(&ctx,buffer,strlen(buffer)); |
| 383 |
|
| 384 |
if (res != 0) { |
| 385 |
tokens = NULL; |
| 386 |
|
| 387 |
if (ctx.error != 0) { |
| 388 |
hypervisor_send_reply(conn,HSC_ERR_PARSING,1,"Parse error: %s", |
| 389 |
parser_strerror(&ctx)); |
| 390 |
goto free_tokens; |
| 391 |
} |
| 392 |
|
| 393 |
if (ctx.tok_count < 2) { |
| 394 |
hypervisor_send_reply(conn,HSC_ERR_PARSING,1, |
| 395 |
"At least a module and a command " |
| 396 |
"must be specified"); |
| 397 |
goto free_tokens; |
| 398 |
} |
| 399 |
|
| 400 |
/* Map token list to an array */ |
| 401 |
tokens = parser_map_array(&ctx); |
| 402 |
|
| 403 |
if (!tokens) { |
| 404 |
hypervisor_send_reply(conn,HSC_ERR_PARSING,1,"No memory"); |
| 405 |
goto free_tokens; |
| 406 |
} |
| 407 |
|
| 408 |
/* Execute command */ |
| 409 |
m_log("HYPERVISOR","exec_cmd: "); |
| 410 |
m_flog_str_array(log_file,ctx.tok_count,tokens); |
| 411 |
|
| 412 |
hypervisor_exec_cmd(conn,tokens[0],tokens[1],ctx. |
| 413 |
tok_count-2,&tokens[2]); |
| 414 |
|
| 415 |
free_tokens: |
| 416 |
free(tokens); |
| 417 |
tokens = NULL; |
| 418 |
parser_context_free(&ctx); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
free(tokens); |
| 423 |
parser_context_free(&ctx); |
| 424 |
return NULL; |
| 425 |
} |
| 426 |
|
| 427 |
static void sigpipe_handler(int sig) |
| 428 |
{ |
| 429 |
printf("SIGPIPE received.\n"); |
| 430 |
} |
| 431 |
|
| 432 |
/* Initialize hypervisor */ |
| 433 |
int hypervisor_init(void) |
| 434 |
{ |
| 435 |
hypervisor_module_t *module; |
| 436 |
|
| 437 |
module = hypervisor_register_module("hypervisor",NULL); |
| 438 |
assert(module != NULL); |
| 439 |
|
| 440 |
hypervisor_register_cmd_array(module,hypervisor_cmd_array); |
| 441 |
return(0); |
| 442 |
} |
| 443 |
|
| 444 |
/* Remove a connection from the list */ |
| 445 |
static void hypervisor_remove_conn(hypervisor_conn_t *conn) |
| 446 |
{ |
| 447 |
if (conn->pprev != NULL) { |
| 448 |
if (conn->next) |
| 449 |
conn->next->pprev = conn->pprev; |
| 450 |
|
| 451 |
*(conn->pprev) = conn->next; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
/* Close a connection */ |
| 456 |
static void hypervisor_close_conn(hypervisor_conn_t *conn) |
| 457 |
{ |
| 458 |
if (conn != NULL) { |
| 459 |
conn->active = FALSE; |
| 460 |
shutdown(conn->client_fd,2); |
| 461 |
pthread_join(conn->tid,NULL); |
| 462 |
|
| 463 |
fclose(conn->in); |
| 464 |
fclose(conn->out); |
| 465 |
|
| 466 |
shutdown(conn->client_fd,2); |
| 467 |
close(conn->client_fd); |
| 468 |
|
| 469 |
hypervisor_remove_conn(conn); |
| 470 |
free(conn); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
/* Close connections (dead or all) */ |
| 475 |
static void hypervisor_close_conn_list(int dead_status) |
| 476 |
{ |
| 477 |
hypervisor_conn_t *conn,*next; |
| 478 |
|
| 479 |
for(conn=hypervisor_conn_list;conn;conn=next) { |
| 480 |
next = conn->next; |
| 481 |
|
| 482 |
if (dead_status && conn->active) |
| 483 |
continue; |
| 484 |
|
| 485 |
hypervisor_close_conn(conn); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
/* Add a new connection to the list */ |
| 490 |
static void hypervisor_add_conn(hypervisor_conn_t *conn) |
| 491 |
{ |
| 492 |
conn->next = hypervisor_conn_list; |
| 493 |
conn->pprev = &hypervisor_conn_list; |
| 494 |
|
| 495 |
if (hypervisor_conn_list != NULL) |
| 496 |
hypervisor_conn_list->pprev = &conn->next; |
| 497 |
|
| 498 |
hypervisor_conn_list = conn; |
| 499 |
} |
| 500 |
|
| 501 |
/* Create a new connection */ |
| 502 |
static hypervisor_conn_t *hypervisor_create_conn(int client_fd) |
| 503 |
{ |
| 504 |
hypervisor_conn_t *conn; |
| 505 |
|
| 506 |
if (!(conn = malloc(sizeof(*conn)))) |
| 507 |
goto err_malloc; |
| 508 |
|
| 509 |
memset(conn,0,sizeof(*conn)); |
| 510 |
conn->active = TRUE; |
| 511 |
conn->client_fd = client_fd; |
| 512 |
|
| 513 |
/* Open input buffered stream */ |
| 514 |
if (!(conn->in = fdopen(client_fd,"r"))) { |
| 515 |
perror("hypervisor_create_conn: fdopen/in"); |
| 516 |
goto err_fd_in; |
| 517 |
} |
| 518 |
|
| 519 |
/* Open output buffered stream */ |
| 520 |
if (!(conn->out = fdopen(client_fd,"w"))) { |
| 521 |
perror("hypervisor_create_conn: fdopen/out"); |
| 522 |
goto err_fd_out; |
| 523 |
} |
| 524 |
|
| 525 |
/* Set line buffering */ |
| 526 |
setlinebuf(conn->in); |
| 527 |
setlinebuf(conn->out); |
| 528 |
|
| 529 |
/* Create the managing thread */ |
| 530 |
if (pthread_create(&conn->tid,NULL,hypervisor_thread,conn) != 0) |
| 531 |
goto err_thread; |
| 532 |
|
| 533 |
/* Add it to the connection list */ |
| 534 |
hypervisor_add_conn(conn); |
| 535 |
return conn; |
| 536 |
|
| 537 |
err_thread: |
| 538 |
fclose(conn->out); |
| 539 |
err_fd_out: |
| 540 |
fclose(conn->in); |
| 541 |
err_fd_in: |
| 542 |
free(conn); |
| 543 |
err_malloc: |
| 544 |
return NULL; |
| 545 |
} |
| 546 |
|
| 547 |
/* Stop hypervisor from sighandler */ |
| 548 |
int hypervisor_stopsig(void) |
| 549 |
{ |
| 550 |
hypervisor_running = FALSE; |
| 551 |
return(0); |
| 552 |
} |
| 553 |
|
| 554 |
/* Hypervisor TCP server */ |
| 555 |
int hypervisor_tcp_server(char *ip_addr,int tcp_port) |
| 556 |
{ |
| 557 |
int fd_array[HYPERVISOR_MAX_FD]; |
| 558 |
struct sockaddr_storage remote_addr; |
| 559 |
socklen_t remote_len; |
| 560 |
int i,res,clnt,fd_count,fd_max; |
| 561 |
struct timeval tv; |
| 562 |
fd_set fds; |
| 563 |
|
| 564 |
/* Initialize all hypervisor modules */ |
| 565 |
hypervisor_init(); |
| 566 |
hypervisor_nio_init(); |
| 567 |
hypervisor_nio_bridge_init(); |
| 568 |
hypervisor_frsw_init(); |
| 569 |
hypervisor_atmsw_init(); |
| 570 |
hypervisor_atm_bridge_init(); |
| 571 |
hypervisor_ethsw_init(); |
| 572 |
hypervisor_vm_init(); |
| 573 |
hypervisor_vm_debug_init(); |
| 574 |
hypervisor_store_init(); |
| 575 |
|
| 576 |
signal(SIGPIPE,sigpipe_handler); |
| 577 |
|
| 578 |
if (!tcp_port) |
| 579 |
tcp_port = HYPERVISOR_TCP_PORT; |
| 580 |
|
| 581 |
fd_count = ip_listen(ip_addr,tcp_port,SOCK_STREAM, |
| 582 |
HYPERVISOR_MAX_FD,fd_array); |
| 583 |
|
| 584 |
if (fd_count <= 0) { |
| 585 |
fprintf(stderr,"Hypervisor: unable to create TCP sockets.\n"); |
| 586 |
return(-1); |
| 587 |
} |
| 588 |
|
| 589 |
/* Start accepting connections */ |
| 590 |
m_log("HYPERVISOR","Release %s/%s (tag %s)\n", |
| 591 |
sw_version,os_name,sw_version_tag); |
| 592 |
|
| 593 |
if (ip_addr != NULL) { |
| 594 |
binding_addr = ip_addr; |
| 595 |
m_log("HYPERVISOR","Started on IP = %s, TCP port = %d.\n", ip_addr, tcp_port); |
| 596 |
printf("Hypervisor TCP control server started (IP %s port %d).\n", ip_addr, tcp_port); |
| 597 |
} |
| 598 |
else { |
| 599 |
m_log("HYPERVISOR","Started on TCP port = %d.\n",tcp_port); |
| 600 |
printf("Hypervisor TCP control server started (port %d).\n",tcp_port); |
| 601 |
} |
| 602 |
hypervisor_running = TRUE; |
| 603 |
|
| 604 |
while(hypervisor_running) { |
| 605 |
FD_ZERO(&fds); |
| 606 |
fd_max = -1; |
| 607 |
|
| 608 |
for(i=0;i<fd_count;i++) |
| 609 |
if (fd_array[i] != -1) { |
| 610 |
FD_SET(fd_array[i],&fds); |
| 611 |
if (fd_array[i] > fd_max) |
| 612 |
fd_max = fd_array[i]; |
| 613 |
} |
| 614 |
|
| 615 |
/* Wait for incoming connections */ |
| 616 |
tv.tv_sec = 0; |
| 617 |
tv.tv_usec = 500 * 1000; /* 500 ms */ |
| 618 |
res = select(fd_max+1,&fds,NULL,NULL,&tv); |
| 619 |
|
| 620 |
if (res == -1) { |
| 621 |
if (errno == EINTR) |
| 622 |
continue; |
| 623 |
else |
| 624 |
perror("hypervisor_tcp_server: select"); |
| 625 |
} |
| 626 |
|
| 627 |
/* Accept connections on signaled sockets */ |
| 628 |
for(i=0;i<fd_count;i++) { |
| 629 |
if (fd_array[i] == -1) |
| 630 |
continue; |
| 631 |
|
| 632 |
if (!FD_ISSET(fd_array[i],&fds)) |
| 633 |
continue; |
| 634 |
|
| 635 |
remote_len = sizeof(remote_addr); |
| 636 |
clnt = accept(fd_array[i],(struct sockaddr *)&remote_addr, |
| 637 |
&remote_len); |
| 638 |
|
| 639 |
if (clnt < 0) { |
| 640 |
perror("hypervisor_tcp_server: accept"); |
| 641 |
continue; |
| 642 |
} |
| 643 |
|
| 644 |
/* create a new connection and start a thread to handle it */ |
| 645 |
if (!hypervisor_create_conn(clnt)) { |
| 646 |
fprintf(stderr,"hypervisor_tcp_server: unable to create new " |
| 647 |
"connection for FD %d\n",clnt); |
| 648 |
close(clnt); |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
/* Walk through the connection list to eliminate dead connections */ |
| 653 |
hypervisor_close_conn_list(TRUE); |
| 654 |
} |
| 655 |
|
| 656 |
/* Close all control sockets */ |
| 657 |
printf("Hypervisor: closing control sockets.\n"); |
| 658 |
for(i=0;i<fd_count;i++) { |
| 659 |
if (fd_array[i] != -1) { |
| 660 |
shutdown(fd_array[i],2); |
| 661 |
close(fd_array[i]); |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
/* Close all remote client connections */ |
| 666 |
printf("Hypervisor: closing remote client connections.\n"); |
| 667 |
hypervisor_close_conn_list(FALSE); |
| 668 |
|
| 669 |
m_log("HYPERVISOR","Stopped.\n"); |
| 670 |
return(0); |
| 671 |
} |