Debian Codegraph @

common/dynamips.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 * Patched by Jeremy Grossmann for the GNS3 project (www.gns3.net)
5 *
6 * Many thanks to Nicolas Szalay for his patch
7 * for the command line parsing and virtual machine
8 * settings (RAM, ROM, NVRAM, ...)
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <signal.h>
19 #include <fcntl.h>
20 #include <assert.h>
21 #include <getopt.h>
22
23 #include "dynamips.h"
24 #include "cpu.h"
25 #include "vm.h"
26
27 #ifdef USE_UNSTABLE
28 #include "tcb.h"
29 #endif
30
31 #include "mips64_exec.h"
32 #include "mips64_jit.h"
33 #include "ppc32_exec.h"
34 #include "ppc32_jit.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 "dev_c6msfc1.h"
43 #include "dev_c6sup1.h"
44 #include "ppc32_vmtest.h"
45 #include "dev_vtty.h"
46 #include "ptask.h"
47 #include "timer.h"
48 #include "plugin.h"
49 #include "registry.h"
50 #include "hypervisor.h"
51 #include "net_io.h"
52 #include "net_io_bridge.h"
53 #include "net_io_filter.h"
54 #include "crc.h"
55 #include "atm.h"
56 #include "atm_bridge.h"
57 #include "frame_relay.h"
58 #include "eth_switch.h"
59 #ifdef GEN_ETH
60 #include "gen_eth.h"
61 #endif
62 #ifdef PROFILE
63 #include "profiler.h"
64 #endif
65
66 /* Default name for logfile */
67 #define LOGFILE_DEFAULT_NAME "dynamips_log.txt"
68
69 /* Operating system name */
70 const char *os_name = STRINGIFY(OSNAME);
71
72 /* Software version */
73 const char *sw_version = DYNAMIPS_VERSION"-"JIT_ARCH;
74
75 /* Software version tag */
76 const char *sw_version_tag = "2023010200";
77
78 /* Hypervisor */
79 int hypervisor_mode = 0;
80 int hypervisor_tcp_port = 0;
81 char *hypervisor_ip_address = NULL;
82
83 /* Log file */
84 char *log_file_name = NULL;
85 FILE *log_file = NULL;
86
87 /* VM flags */
88 volatile int vm_save_state = 0;
89
90 /* Default platform */
91 static char *default_platform = "7200";
92
93 /* Binding address (NULL means any or 0.0.0.0) */
94 char *binding_addr = NULL;
95
96 /* Console (vtty tcp) binding address (NULL means any or 0.0.0.0) */
97 char *console_binding_addr = NULL;
98
99 /* Generic signal handler */
100 void signal_gen_handler(int sig)
101 {
102 switch(sig) {
103 case SIGHUP:
104 /* For future use */
105 break;
106
107 case SIGQUIT:
108 /* save VM context */
109 vm_save_state = TRUE;
110 break;
111
112 /* Handle SIGPIPE by ignoring it */
113 case SIGPIPE:
114 fprintf(stderr,"Error: unwanted SIGPIPE.\n");
115 break;
116
117 case SIGINT:
118 /* CTRL+C has been pressed */
119 if (hypervisor_mode)
120 hypervisor_stopsig();
121 else {
122 /* In theory, this shouldn't happen thanks to VTTY settings */
123 vm_instance_t *vm;
124
125 if ((vm = vm_acquire("default")) != NULL) {
126 /* Only forward ctrl-c if user has requested local terminal */
127 if (vm->vtty_con_type == VTTY_TYPE_TERM) {
128 vtty_store_ctrlc(vm->vtty_con);
129 } else {
130 vm_stop(vm);
131 }
132 vm_release(vm);
133 } else {
134 fprintf(stderr,"Error: Cannot acquire instance handle.\n");
135 }
136 }
137 break;
138
139 default:
140 fprintf(stderr,"Unhandled signal %d\n",sig);
141 }
142 }
143
144 /* Setups signals */
145 static void setup_signals(void)
146 {
147 struct sigaction act;
148
149 memset(&act,0,sizeof(act));
150 act.sa_handler = signal_gen_handler;
151 act.sa_flags = SA_RESTART;
152 sigaction(SIGHUP,&act,NULL);
153 sigaction(SIGQUIT,&act,NULL);
154 sigaction(SIGINT,&act,NULL);
155 sigaction(SIGPIPE,&act,NULL);
156 }
157
158 /* Create general log file */
159 static void create_log_file(void)
160 {
161 /* Set the default value of the log file name */
162 if (!log_file_name) {
163 if (!(log_file_name = strdup(LOGFILE_DEFAULT_NAME))) {
164 fprintf(stderr,"Unable to set log file name.\n");
165 exit(EXIT_FAILURE);
166 }
167 }
168
169 if (!(log_file = fopen(log_file_name,"w"))) {
170 fprintf(stderr,"Unable to create log file (%s).\n",strerror(errno));
171 exit(EXIT_FAILURE);
172 }
173 }
174
175 /* Close general log file */
176 static void close_log_file(void)
177 {
178 if (log_file) fclose(log_file);
179 free(log_file_name);
180
181 log_file = NULL;
182 log_file_name = NULL;
183 }
184
185 /* Display the command line use */
186 static void show_usage(vm_instance_t *vm,int argc,char *argv[])
187 {
188 printf("Usage: %s [options] <ios_image>\n\n",argv[0]);
189
190 printf("Available options:\n"
191 " -H [<ip_address>:]<tcp_port> : Run in hypervisor mode\n\n"
192 " -P <platform> : Platform to emulate (7200, 3600, "
193 "2691, 3725, 3745, 2600 or 1700) "
194 "(default: 7200)\n\n"
195 " -l <log_file> : Set logging file (default is %s)\n"
196 " -j : Disable the JIT compiler, very slow\n"
197 " --idle-pc <pc> : Set the idle PC (default: disabled)\n"
198 " --timer-itv <val> : Timer IRQ interval check (default: %u)\n"
199 "\n"
200 " -i <instance> : Set instance ID\n"
201 " -r <ram_size> : Set the virtual RAM size (default: %u Mb)\n"
202 " -o <rom_size> : Set the virtual ROM size (default: %u Mb)\n"
203 " -n <nvram_size> : Set the NVRAM size (default: %d Kb)\n"
204 " -c <conf_reg> : Set the configuration register "
205 "(default: 0x%04x)\n"
206 " -m <mac_addr> : Set the MAC address of the chassis\n"
207 " (default: automatically generated)\n"
208 " -C, --startup-config <file> : Import IOS configuration file into NVRAM\n"
209 " --private-config <file> : Import IOS configuration file into NVRAM\n"
210 " -X : Do not use a file to simulate RAM (faster)\n"
211 " -G <ghost_file> : Use a ghost file to simulate RAM\n"
212 " -g <ghost_file> : Generate a ghost RAM file\n"
213 " --sparse-mem : Use sparse memory\n"
214 " -R <rom_file> : Load an alternate ROM (default: embedded)\n"
215 " -k <clock_div> : Set the clock divisor (default: %d)\n"
216 "\n"
217 " -T <port> : Console is on TCP <port>\n"
218 " -U <si_desc> : Console in on serial interface <si_desc>\n"
219 " (default is on the terminal)\n"
220 "\n"
221 " -A <port> : AUX is on TCP <port>\n"
222 " -B <si_desc> : AUX is on serial interface <si_desc>\n"
223 " (default is no AUX port)\n"
224 "\n"
225 " --disk0 <size> : Set PCMCIA ATA disk0: size "
226 "(default: %u Mb)\n"
227 " --disk1 <size> : Set PCMCIA ATA disk1: size "
228 "(default: %u Mb)\n"
229 "\n"
230 " --noctrl : Disable ctrl+] monitor console\n"
231 " --notelnetmsg : Disable message when using tcp console/aux\n"
232 " --filepid filename : Store dynamips pid in a file\n"
233 " --console-binding-addr: binding address for tcp console/aux\n"
234 "\n",
235 LOGFILE_DEFAULT_NAME,VM_TIMER_IRQ_CHECK_ITV,
236 vm->ram_size,vm->rom_size,vm->nvram_size,vm->conf_reg_setup,
237 vm->clock_divisor,vm->pcmcia_disk_size[0],vm->pcmcia_disk_size[1]);
238
239 if (vm->platform->cli_show_options != NULL)
240 vm->platform->cli_show_options(vm);
241
242 printf("\n"
243 #if DEBUG_SYM_TREE
244 " -S <sym_file> : Load a symbol file\n"
245 #endif
246 " -a <cfg_file> : Virtual ATM switch configuration file\n"
247 " -f <cfg_file> : Virtual Frame-Relay switch configuration "
248 "file\n"
249 " -E <cfg_file> : Virtual Ethernet switch configuration file\n"
250 " -b <cfg_file> : Virtual bridge configuration file\n"
251 " -e : Show network device list of the "
252 "host machine\n"
253 "\n");
254
255 printf("<si_desc> format:\n"
256 " \"device{:baudrate{:databits{:parity{:stopbits{:hwflow}}}}}}\"\n"
257 "\n");
258
259 switch(vm->slots_type) {
260 case CISCO_CARD_TYPE_PA:
261 printf("<pa_desc> format:\n"
262 " \"slot:sub_slot:pa_driver\"\n"
263 "\n");
264
265 printf("<pa_nio> format:\n"
266 " \"slot:port:netio_type{:netio_parameters}\"\n"
267 "\n");
268 break;
269
270 case CISCO_CARD_TYPE_NM:
271 printf("<nm_desc> format:\n"
272 " \"slot:sub_slot:nm_driver\"\n"
273 "\n");
274
275 printf("<nm_nio> format:\n"
276 " \"slot:port:netio_type{:netio_parameters}\"\n"
277 "\n");
278 break;
279
280 case CISCO_CARD_TYPE_WIC:
281 printf("<wic_desc> format:\n"
282 " \"slot:wic_driver\"\n"
283 "\n");
284
285 printf("<wic_nio> format:\n"
286 " \"slot:port:netio_type{:netio_parameters}\"\n"
287 "\n");
288 break;
289 }
290
291 if (vm->platform->show_spec_drivers != NULL)
292 vm->platform->show_spec_drivers();
293
294 /* Show possible slot drivers */
295 vm_slot_show_drivers(vm);
296
297 /* Show the possible NETIO types */
298 netio_show_types();
299 }
300
301 /* Find an option in the command line */
302 static char *cli_find_option(int argc,char *argv[],char *opt)
303 {
304 int i;
305
306 for(i=1;i<argc;i++) {
307 if (!strncmp(argv[i],opt,2)) {
308 if (argv[i][2] != 0)
309 return(&argv[i][2]);
310 else {
311 if (argv[i+1] != NULL)
312 return(argv[i+1]);
313 else {
314 fprintf(stderr,"Error: option '%s': no argument specified.\n",
315 opt);
316 exit(EXIT_FAILURE);
317 }
318 }
319 }
320 }
321
322 return NULL;
323 }
324
325 /* Load plugins */
326 static void cli_load_plugins(int argc,char *argv[])
327 {
328 char *str;
329 int i;
330
331 for(i=1;i<argc;i++) {
332 if (!strncmp(argv[i],"-L",2)) {
333 if (argv[i][2] != 0)
334 str = &argv[i][2];
335 else {
336 if (argv[i+1] != NULL)
337 str = argv[i+1];
338 else {
339 fprintf(stderr,"Plugin error: no argument specified.\n");
340 exit(EXIT_FAILURE);
341 }
342 }
343
344 if (!plugin_load(str))
345 fprintf(stderr,"Unable to load plugin '%s'!\n",str);
346 }
347 }
348 }
349
350 /* Determine the platform (Cisco 3600, 7200). Default is Cisco 7200 */
351 static vm_platform_t *cli_get_platform_type(int argc,char *argv[])
352 {
353 vm_platform_t *platform;
354 char *str;
355
356 if (!(str = cli_find_option(argc,argv,"-P")))
357 str = default_platform;
358
359 if (!(platform = vm_platform_find_cli_name(str)))
360 fprintf(stderr,"Invalid platform type '%s'\n",str);
361
362 return platform;
363 }
364
365 static struct option cmd_line_lopts[] = {
366 { "disk0" , 1, NULL, OPT_DISK0_SIZE },
367 { "disk1" , 1, NULL, OPT_DISK1_SIZE },
368 { "idle-pc" , 1, NULL, OPT_IDLE_PC },
369 { "timer-itv" , 1, NULL, OPT_TIMER_ITV },
370 { "vm-debug" , 1, NULL, OPT_VM_DEBUG },
371 { "iomem-size" , 1, NULL, OPT_IOMEM_SIZE },
372 { "sparse-mem" , 0, NULL, OPT_SPARSE_MEM },
373 { "noctrl" , 0, NULL, OPT_NOCTRL },
374 { "notelnetmsg", 0, NULL, OPT_NOTELMSG },
375 { "filepid" , 1, NULL, OPT_FILEPID },
376 { "startup-config", 1, NULL, OPT_STARTUP_CONFIG_FILE },
377 { "private-config", 1, NULL, OPT_PRIVATE_CONFIG_FILE },
378 { "console-binding-addr", 1, NULL, OPT_CONSOLE_BINDING_ADDR },
379 { NULL , 0, NULL, 0 },
380 };
381
382 /* Create a router instance */
383 static vm_instance_t *cli_create_instance(char *name,char *platform_name,
384 int instance_id)
385 {
386 vm_instance_t *vm;
387
388 vm = vm_create_instance(name,instance_id,platform_name);
389
390 if (vm == NULL) {
391 fprintf(stderr,"%s: unable to create instance %s!\n",platform_name,name);
392 return NULL;
393 }
394
395 return vm;
396 }
397
398 /* Parse the command line */
399 static int parse_std_cmd_line(int argc,char *argv[])
400 {
401 char *options_list =
402 "r:o:n:c:m:l:C:i:jt:p:s:k:T:U:A:B:a:f:E:b:S:R:M:eXP:N:G:g:L:I:";
403 vm_platform_t *platform;
404 vm_instance_t *vm = NULL;
405 int instance_id;
406 int option;
407 char *str;
408 FILE *pid_file = NULL; // For saving the pid if requested
409
410 /* Get the instance ID */
411 instance_id = 0;
412
413 /* Use the old VM file naming type */
414 vm_file_naming_type = 1;
415
416 cli_load_plugins(argc,argv);
417
418 if ((str = cli_find_option(argc,argv,"-i"))) {
419 instance_id = atoi(str);
420 printf("Instance ID set to %d.\n",instance_id);
421 }
422
423 if ((str = cli_find_option(argc,argv,"-N")))
424 vm_file_naming_type = atoi(str);
425
426 /* Get the platform type */
427 if (!(platform = cli_get_platform_type(argc,argv)))
428 goto exit_failure;
429
430 /* Create the default instance */
431 if (!(vm = cli_create_instance("default",platform->name,instance_id)))
432 goto exit_failure;
433
434 opterr = 0;
435
436 vtty_set_ctrlhandler(1); /* By default allow ctrl ] */
437 vtty_set_telnetmsg(1); /* By default allow telnet message */
438
439 while((option = getopt_long(argc,argv,options_list,
440 cmd_line_lopts,NULL)) != -1)
441 {
442 switch(option)
443 {
444 /* Instance ID (already managed) */
445 case 'i':
446 break;
447
448 /* Platform (already managed) */
449 case 'P':
450 break;
451
452 /* RAM size */
453 case 'r':
454 vm->ram_size = strtol(optarg, NULL, 10);
455 printf("Virtual RAM size set to %d MB.\n",vm->ram_size);
456 break;
457
458 /* ROM size */
459 case 'o':
460 vm->rom_size = strtol(optarg, NULL, 10);
461 printf("Virtual ROM size set to %d MB.\n",vm->rom_size);
462 break;
463
464 /* NVRAM size */
465 case 'n':
466 vm->nvram_size = strtol(optarg, NULL, 10);
467 printf("NVRAM size set to %d KB.\n",vm->nvram_size);
468 break;
469
470 /* PCMCIA disk0 size */
471 case OPT_DISK0_SIZE:
472 vm->pcmcia_disk_size[0] = atoi(optarg);
473 printf("PCMCIA ATA disk0 size set to %u MB.\n",
474 vm->pcmcia_disk_size[0]);
475 break;
476
477 /* PCMCIA disk1 size */
478 case OPT_DISK1_SIZE:
479 vm->pcmcia_disk_size[1] = atoi(optarg);
480 printf("PCMCIA ATA disk1 size set to %u MB.\n",
481 vm->pcmcia_disk_size[1]);
482 break;
483
484 case OPT_NOCTRL:
485 vtty_set_ctrlhandler(0); /* Ignore ctrl ] */
486 printf("Block ctrl+] access to monitor console.\n");
487 break;
488
489 /* Config Register */
490 case 'c':
491 vm->conf_reg_setup = strtol(optarg, NULL, 0);
492 printf("Config. Register set to 0x%x.\n",vm->conf_reg_setup);
493 break;
494
495 /* IOS startup configuration file */
496 case 'C':
497 case OPT_STARTUP_CONFIG_FILE:
498 vm_ios_set_config(vm,optarg,vm->ios_private_config);
499 break;
500
501 /* IOS private configuration file */
502 case OPT_PRIVATE_CONFIG_FILE:
503 vm_ios_set_config(vm,vm->ios_startup_config,optarg);
504 break;
505
506 /* Global console (vtty tcp) binding address */
507 case OPT_CONSOLE_BINDING_ADDR:
508 console_binding_addr = strdup(optarg);
509 printf("Console binding address set to %s\n", console_binding_addr);
510 break;
511
512 /* Use physical memory to emulate RAM (no-mapped file) */
513 case 'X':
514 vm->ram_mmap = 0;
515 break;
516
517 /* Use a ghost file to simulate RAM */
518 case 'G':
519 free(vm->ghost_ram_filename);
520 vm->ghost_ram_filename = strdup(optarg);
521 vm->ghost_status = VM_GHOST_RAM_USE;
522 break;
523
524 /* Generate a ghost RAM image */
525 case 'g':
526 free(vm->ghost_ram_filename);
527 vm->ghost_ram_filename = strdup(optarg);
528 vm->ghost_status = VM_GHOST_RAM_GENERATE;
529 break;
530
531 /* Use sparse memory */
532 case OPT_SPARSE_MEM:
533 vm->sparse_mem = TRUE;
534 break;
535
536 /* Alternate ROM */
537 case 'R':
538 free(vm->rom_filename);
539 vm->rom_filename = strdup(optarg);
540 break;
541
542 case OPT_NOTELMSG:
543 vtty_set_telnetmsg(0); /* disable telnet greeting */
544 printf("Prevent telnet message on AUX/CONSOLE connecte.\n");
545 break;
546
547 case OPT_FILEPID:
548 if ((pid_file = fopen(optarg,"w"))) {
549 fprintf(pid_file,"%d",getpid());
550 fclose(pid_file);
551 } else {
552 printf("Unable to save to %s.\n",optarg);
553 }
554 break;
555
556 /* Idle PC */
557 case OPT_IDLE_PC:
558 vm->idle_pc = strtoull(optarg,NULL,0);
559 printf("Idle PC set to 0x%llx.\n",vm->idle_pc);
560 break;
561
562 /* Timer IRQ check interval */
563 case OPT_TIMER_ITV:
564 vm->timer_irq_check_itv = atoi(optarg);
565 break;
566
567 /* Clock divisor */
568 case 'k':
569 vm->clock_divisor = atoi(optarg);
570
571 if (!vm->clock_divisor) {
572 fprintf(stderr,"Invalid Clock Divisor specified!\n");
573 goto exit_failure;
574 }
575
576 printf("Using a clock divisor of %d.\n",vm->clock_divisor);
577 break;
578
579 /* Disable JIT */
580 case 'j':
581 vm->jit_use = FALSE;
582 break;
583
584 /* VM debug level */
585 case OPT_VM_DEBUG:
586 vm->debug_level = atoi(optarg);
587 break;
588
589 /* Log file */
590 case 'l':
591 if (!(log_file_name = realloc(log_file_name, strlen(optarg)+1))) {
592 fprintf(stderr,"Unable to set log file name.\n");
593 goto exit_failure;
594 }
595 strcpy(log_file_name, optarg);
596 printf("Log file: writing to %s\n",log_file_name);
597 break;
598
599 #if DEBUG_SYM_TREE
600 /* Symbol file */
601 case 'S':
602 vm->sym_filename = strdup(optarg);
603 break;
604 #endif
605
606 /* TCP server for Console Port */
607 case 'T':
608 vm->vtty_con_type = VTTY_TYPE_TCP;
609 vm->vtty_con_tcp_port = atoi(optarg);
610 break;
611
612 /* Serial interface for Console port */
613 case 'U':
614 vm->vtty_con_type = VTTY_TYPE_SERIAL;
615 if (vtty_parse_serial_option(&vm->vtty_con_serial_option,optarg)) {
616 fprintf(stderr,
617 "Invalid Console serial interface descriptor!\n");
618 goto exit_failure;
619 }
620 break;
621
622 /* TCP server for AUX Port */
623 case 'A':
624 vm->vtty_aux_type = VTTY_TYPE_TCP;
625 vm->vtty_aux_tcp_port = atoi(optarg);
626 break;
627
628 /* Serial interface for AUX port */
629 case 'B':
630 vm->vtty_aux_type = VTTY_TYPE_SERIAL;
631 if (vtty_parse_serial_option(&vm->vtty_aux_serial_option,optarg)) {
632 fprintf(stderr,"Invalid AUX serial interface descriptor!\n");
633 goto exit_failure;
634 }
635 break;
636
637 /* Port settings */
638 case 'p':
639 vm_slot_cmd_create(vm,optarg);
640 break;
641
642 /* NIO settings */
643 case 's':
644 vm_slot_cmd_add_nio(vm,optarg);
645 break;
646
647 /* Virtual ATM switch */
648 case 'a':
649 if (atmsw_start(optarg) == -1)
650 goto exit_failure;
651 break;
652
653 /* Virtual ATM bridge */
654 case 'M':
655 if (atm_bridge_start(optarg) == -1)
656 goto exit_failure;
657 break;
658
659 /* Virtual Frame-Relay switch */
660 case 'f':
661 if (frsw_start(optarg) == -1)
662 goto exit_failure;
663 break;
664
665 /* Virtual Ethernet switch */
666 case 'E':
667 if (ethsw_start(optarg) == -1)
668 goto exit_failure;
669 break;
670
671 /* Virtual bridge */
672 case 'b':
673 if (netio_bridge_start(optarg) == -1)
674 goto exit_failure;
675 break;
676
677 #ifdef GEN_ETH
678 /* Ethernet device list */
679 case 'e':
680 gen_eth_show_dev_list();
681 goto exit_success;
682 #endif
683
684 /* Load plugin (already handled) */
685 case 'L':
686 break;
687
688 /* Oops ! */
689 case '?':
690 show_usage(vm,argc,argv);
691 goto exit_failure;
692
693 /* Parse options specific to the platform */
694 default:
695 if (vm->platform->cli_parse_options != NULL)
696 /* If you get an option wrong, say which option is was */
697 /* Wont be pretty for a long option, but it will at least help */
698 if (vm->platform->cli_parse_options(vm,option) == -1) {
699 printf("Flag not recognised: -%c\n",(char)option);
700 goto exit_failure;
701 }
702 }
703 }
704
705 /* Last argument, this is the IOS filename */
706 if (optind == (argc - 1)) {
707 /* setting IOS image file */
708 vm_ios_set_image(vm,argv[optind]);
709 printf("IOS image file: %s\n\n",vm->ios_image);
710 } else {
711 /* IOS missing */
712 fprintf(stderr,"Please specify an IOS image filename\n");
713 show_usage(vm,argc,argv);
714 goto exit_failure;
715 }
716
717 vm_release(vm);
718 return(0);
719
720 exit_success:
721 if (vm) {
722 vm_release(vm);
723 vm_delete_instance("default");
724 }
725 exit(EXIT_SUCCESS);
726 return(-1);
727 exit_failure:
728 if (vm) {
729 vm_release(vm);
730 vm_delete_instance("default");
731 }
732 exit(EXIT_FAILURE);
733 return(-1);
734 }
735
736 /*
737 * Run in hypervisor mode with a config file if the "-H" option
738 * is present in command line.
739 */
740 static int run_hypervisor(int argc,char *argv[])
741 {
742 char *options_list = "H:l:hN:L:";
743 int i,option;
744 char *index;
745 size_t len;
746 FILE *pid_file = NULL; // For saving the pid if requested
747
748 vtty_set_ctrlhandler(1); /* By default allow ctrl ] */
749 vtty_set_telnetmsg(1); /* By default allow telnet message */
750
751 for(i=1;i<argc;i++)
752 if (!strcmp(argv[i],"-H")) {
753 hypervisor_mode = 1;
754 break;
755 }
756
757 /* standard mode with one instance */
758 if (!hypervisor_mode)
759 return(FALSE);
760
761 cli_load_plugins(argc,argv);
762
763 opterr = 0;
764
765 /* New long options are sometimes appropriate for hypervisor mode */
766 while((option = getopt_long(argc,argv,options_list,
767 cmd_line_lopts,NULL)) != -1) {
768 switch(option)
769 {
770 /* Hypervisor TCP port */
771 case 'H':
772 index = strrchr(optarg,':');
773
774 if (!index) {
775 hypervisor_tcp_port = atoi(optarg);
776 } else {
777 len = index - optarg;
778 hypervisor_ip_address = realloc(hypervisor_ip_address, len + 1);
779
780 if (!hypervisor_ip_address) {
781 fprintf(stderr,"Unable to set hypervisor IP address!\n");
782 exit(EXIT_FAILURE);
783 }
784
785 memcpy(hypervisor_ip_address,optarg,len);
786 hypervisor_ip_address[len] = '\0';
787 hypervisor_tcp_port = atoi(index + 1);
788 }
789 break;
790
791 /* Log file */
792 case 'l':
793 if (!(log_file_name = realloc(log_file_name, strlen(optarg)+1))) {
794 fprintf(stderr,"Unable to set log file name!\n");
795 exit(EXIT_FAILURE);
796 }
797 strcpy(log_file_name, optarg);
798 printf("Log file: writing to %s\n",log_file_name);
799 break;
800
801 /* VM file naming type */
802 case 'N':
803 vm_file_naming_type = atoi(optarg);
804 break;
805
806 /* Load plugin (already handled) */
807 case 'L':
808 break;
809
810 case OPT_NOCTRL:
811 vtty_set_ctrlhandler(0); /* Ignore ctrl ] */
812 printf("Block ctrl+] access to monitor console.\n");
813 break;
814
815 case OPT_NOTELMSG:
816 vtty_set_telnetmsg(0); /* disable telnet greeting */
817 printf("Prevent telnet message on AUX/CONSOLE connect.\n");
818 break;
819
820 case OPT_FILEPID:
821 if ((pid_file = fopen(optarg,"w"))) {
822 fprintf(pid_file,"%d",getpid());
823 fclose(pid_file);
824 } else {
825 printf("Unable to save to %s.\n",optarg);
826 }
827 break;
828
829 /* Global console (vtty tcp) binding address */
830 case OPT_CONSOLE_BINDING_ADDR:
831 console_binding_addr = strdup(optarg);
832 printf("Console binding address set to %s\n", console_binding_addr);
833 break;
834
835 /* Oops ! */
836 case '?':
837 //show_usage(argc,argv,VM_TYPE_C7200);
838 exit(EXIT_FAILURE);
839 }
840 }
841
842 return(TRUE);
843 }
844
845 /* Delete all objects */
846 void dynamips_reset(void)
847 {
848 printf("Shutdown in progress...\n");
849
850 /* Delete all virtual router instances */
851 vm_delete_all_instances();
852
853 /* Delete ATM and Frame-Relay switches + bridges */
854 netio_bridge_delete_all();
855 atmsw_delete_all();
856 atm_bridge_delete_all();
857 frsw_delete_all();
858 ethsw_delete_all();
859
860 /* Delete all NIO descriptors */
861 netio_delete_all();
862
863 m_log("GENERAL","reset done.\n");
864
865 printf("Shutdown completed.\n");
866 }
867
868 /* Default platforms */
869 static int (*platform_register[])(void) = {
870 c7200_platform_register,
871 c3600_platform_register,
872 c3725_platform_register,
873 c3745_platform_register,
874 c2691_platform_register,
875 c2600_platform_register,
876 c1700_platform_register,
877 c6sup1_platform_register,
878 c6msfc1_platform_register,
879 #ifdef USE_UNSTABLE
880 ppc32_vmtest_platform_register,
881 #endif
882 NULL,
883 };
884
885 /* Register default platforms */
886 static void register_default_platforms(void)
887 {
888 int i;
889
890 for(i=0;platform_register[i];i++)
891 platform_register[i]();
892 }
893
894 /* Destroy variables generated from the standard command line */
895 static void destroy_cmd_line_vars(void)
896 {
897 if (log_file_name) {
898 free(log_file_name);
899 log_file_name = NULL;
900 }
901 if (hypervisor_ip_address) {
902 free(hypervisor_ip_address);
903 hypervisor_ip_address = NULL;
904 }
905 if (console_binding_addr) {
906 free(console_binding_addr);
907 console_binding_addr = NULL;
908 }
909 }
910
911 int main(int argc,char *argv[])
912 {
913 vm_instance_t *vm;
914
915 #ifdef PROFILE
916 atexit(profiler_savestat);
917 #endif
918
919 #ifdef USE_UNSTABLE
920 printf("Cisco Router Simulation Platform (version %s/%s unstable)\n",
921 sw_version,os_name);
922 #else
923 printf("Cisco Router Simulation Platform (version %s/%s stable)\n",
924 sw_version,os_name);
925 #endif
926
927 printf("Copyright (c) 2005-2011 Christophe Fillot.\n");
928 printf("Build date: %s %s\n\n",__DATE__,__TIME__);
929
930 /* Register platforms */
931 register_default_platforms();
932
933 /* Initialize timers */
934 timer_init();
935
936 /* Initialize object registry */
937 registry_init();
938
939 /* Initialize ATM module (for HEC checksums) */
940 atm_init();
941
942 /* Initialize CRC functions */
943 crc_init();
944
945 /* Initialize NetIO code */
946 netio_rxl_init();
947
948 /* Initialize NetIO packet filters */
949 netio_filter_load_all();
950
951 /* Initialize VTTY code */
952 vtty_init();
953
954 /* Parse standard command line */
955 atexit(destroy_cmd_line_vars);
956 if (!run_hypervisor(argc,argv))
957 parse_std_cmd_line(argc,argv);
958
959 /* Create general log file */
960 create_log_file();
961
962 /* Periodic tasks initialization */
963 if (ptask_init(0) == -1)
964 exit(EXIT_FAILURE);
965
966 /* Create instruction lookup tables */
967 mips64_jit_create_ilt();
968 mips64_exec_create_ilt();
969 ppc32_jit_create_ilt();
970 ppc32_exec_create_ilt();
971
972 setup_signals();
973
974 if (!hypervisor_mode) {
975 /* Initialize the default instance */
976 vm = vm_acquire("default");
977 assert(vm != NULL);
978
979 if (vm_init_instance(vm) == -1) {
980 fprintf(stderr,"Unable to initialize router instance.\n");
981 exit(EXIT_FAILURE);
982 }
983
984 #if (DEBUG_INSN_PERF_CNT > 0) || (DEBUG_BLOCK_PERF_CNT > 0)
985 {
986 m_uint32_t counter,prev = 0,delta;
987 while(vm->status == VM_STATUS_RUNNING) {
988 counter = cpu_get_perf_counter(vm->boot_cpu);
989 delta = counter - prev;
990 prev = counter;
991 printf("delta = %u\n",delta);
992 sleep(1);
993 }
994 }
995 #else
996 /* Start instance monitoring */
997 vm_monitor(vm);
998 #endif
999
1000 /* Free resources used by instance */
1001 vm_release(vm);
1002 } else {
1003 hypervisor_tcp_server(hypervisor_ip_address,hypervisor_tcp_port);
1004 }
1005
1006 dynamips_reset();
1007 close_log_file();
1008 return(0);
1009 }