Debian Codegraph @

unstable/hypervisor.c

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