/* * Cisco router simulation platform. * Copyright (c) 2005,2006 Christophe Fillot (cf@utc.fr) * Patched by Jeremy Grossmann for the GNS3 project (www.gns3.net) * * Many thanks to Nicolas Szalay for his patch * for the command line parsing and virtual machine * settings (RAM, ROM, NVRAM, ...) */ #include #include #include #include #include #include #include #include #include #include #include #include "dynamips.h" #include "cpu.h" #include "vm.h" #ifdef USE_UNSTABLE #include "tcb.h" #endif #include "mips64_exec.h" #include "mips64_jit.h" #include "ppc32_exec.h" #include "ppc32_jit.h" #include "dev_c7200.h" #include "dev_c3600.h" #include "dev_c2691.h" #include "dev_c3725.h" #include "dev_c3745.h" #include "dev_c2600.h" #include "dev_c1700.h" #include "dev_c6msfc1.h" #include "dev_c6sup1.h" #include "ppc32_vmtest.h" #include "dev_vtty.h" #include "ptask.h" #include "timer.h" #include "plugin.h" #include "registry.h" #include "hypervisor.h" #include "net_io.h" #include "net_io_bridge.h" #include "net_io_filter.h" #include "crc.h" #include "atm.h" #include "atm_bridge.h" #include "frame_relay.h" #include "eth_switch.h" #ifdef GEN_ETH #include "gen_eth.h" #endif #ifdef PROFILE #include "profiler.h" #endif /* Default name for logfile */ #define LOGFILE_DEFAULT_NAME "dynamips_log.txt" /* Operating system name */ const char *os_name = STRINGIFY(OSNAME); /* Software version */ const char *sw_version = DYNAMIPS_VERSION"-"JIT_ARCH; /* Software version tag */ const char *sw_version_tag = "2023010200"; /* Hypervisor */ int hypervisor_mode = 0; int hypervisor_tcp_port = 0; char *hypervisor_ip_address = NULL; /* Log file */ char *log_file_name = NULL; FILE *log_file = NULL; /* VM flags */ volatile int vm_save_state = 0; /* Default platform */ static char *default_platform = "7200"; /* Binding address (NULL means any or 0.0.0.0) */ char *binding_addr = NULL; /* Console (vtty tcp) binding address (NULL means any or 0.0.0.0) */ char *console_binding_addr = NULL; /* Generic signal handler */ void signal_gen_handler(int sig) { switch(sig) { case SIGHUP: /* For future use */ break; case SIGQUIT: /* save VM context */ vm_save_state = TRUE; break; /* Handle SIGPIPE by ignoring it */ case SIGPIPE: fprintf(stderr,"Error: unwanted SIGPIPE.\n"); break; case SIGINT: /* CTRL+C has been pressed */ if (hypervisor_mode) hypervisor_stopsig(); else { /* In theory, this shouldn't happen thanks to VTTY settings */ vm_instance_t *vm; if ((vm = vm_acquire("default")) != NULL) { /* Only forward ctrl-c if user has requested local terminal */ if (vm->vtty_con_type == VTTY_TYPE_TERM) { vtty_store_ctrlc(vm->vtty_con); } else { vm_stop(vm); } vm_release(vm); } else { fprintf(stderr,"Error: Cannot acquire instance handle.\n"); } } break; default: fprintf(stderr,"Unhandled signal %d\n",sig); } } /* Setups signals */ static void setup_signals(void) { struct sigaction act; memset(&act,0,sizeof(act)); act.sa_handler = signal_gen_handler; act.sa_flags = SA_RESTART; sigaction(SIGHUP,&act,NULL); sigaction(SIGQUIT,&act,NULL); sigaction(SIGINT,&act,NULL); sigaction(SIGPIPE,&act,NULL); } /* Create general log file */ static void create_log_file(void) { /* Set the default value of the log file name */ if (!log_file_name) { if (!(log_file_name = strdup(LOGFILE_DEFAULT_NAME))) { fprintf(stderr,"Unable to set log file name.\n"); exit(EXIT_FAILURE); } } if (!(log_file = fopen(log_file_name,"w"))) { fprintf(stderr,"Unable to create log file (%s).\n",strerror(errno)); exit(EXIT_FAILURE); } } /* Close general log file */ static void close_log_file(void) { if (log_file) fclose(log_file); free(log_file_name); log_file = NULL; log_file_name = NULL; } /* Display the command line use */ static void show_usage(vm_instance_t *vm,int argc,char *argv[]) { printf("Usage: %s [options] \n\n",argv[0]); printf("Available options:\n" " -H [:] : Run in hypervisor mode\n\n" " -P : Platform to emulate (7200, 3600, " "2691, 3725, 3745, 2600 or 1700) " "(default: 7200)\n\n" " -l : Set logging file (default is %s)\n" " -j : Disable the JIT compiler, very slow\n" " --idle-pc : Set the idle PC (default: disabled)\n" " --timer-itv : Timer IRQ interval check (default: %u)\n" "\n" " -i : Set instance ID\n" " -r : Set the virtual RAM size (default: %u Mb)\n" " -o : Set the virtual ROM size (default: %u Mb)\n" " -n : Set the NVRAM size (default: %d Kb)\n" " -c : Set the configuration register " "(default: 0x%04x)\n" " -m : Set the MAC address of the chassis\n" " (default: automatically generated)\n" " -C, --startup-config : Import IOS configuration file into NVRAM\n" " --private-config : Import IOS configuration file into NVRAM\n" " -X : Do not use a file to simulate RAM (faster)\n" " -G : Use a ghost file to simulate RAM\n" " -g : Generate a ghost RAM file\n" " --sparse-mem : Use sparse memory\n" " -R : Load an alternate ROM (default: embedded)\n" " -k : Set the clock divisor (default: %d)\n" "\n" " -T : Console is on TCP \n" " -U : Console in on serial interface \n" " (default is on the terminal)\n" "\n" " -A : AUX is on TCP \n" " -B : AUX is on serial interface \n" " (default is no AUX port)\n" "\n" " --disk0 : Set PCMCIA ATA disk0: size " "(default: %u Mb)\n" " --disk1 : Set PCMCIA ATA disk1: size " "(default: %u Mb)\n" "\n" " --noctrl : Disable ctrl+] monitor console\n" " --notelnetmsg : Disable message when using tcp console/aux\n" " --filepid filename : Store dynamips pid in a file\n" " --console-binding-addr: binding address for tcp console/aux\n" "\n", LOGFILE_DEFAULT_NAME,VM_TIMER_IRQ_CHECK_ITV, vm->ram_size,vm->rom_size,vm->nvram_size,vm->conf_reg_setup, vm->clock_divisor,vm->pcmcia_disk_size[0],vm->pcmcia_disk_size[1]); if (vm->platform->cli_show_options != NULL) vm->platform->cli_show_options(vm); printf("\n" #if DEBUG_SYM_TREE " -S : Load a symbol file\n" #endif " -a : Virtual ATM switch configuration file\n" " -f : Virtual Frame-Relay switch configuration " "file\n" " -E : Virtual Ethernet switch configuration file\n" " -b : Virtual bridge configuration file\n" " -e : Show network device list of the " "host machine\n" "\n"); printf(" format:\n" " \"device{:baudrate{:databits{:parity{:stopbits{:hwflow}}}}}}\"\n" "\n"); switch(vm->slots_type) { case CISCO_CARD_TYPE_PA: printf(" format:\n" " \"slot:sub_slot:pa_driver\"\n" "\n"); printf(" format:\n" " \"slot:port:netio_type{:netio_parameters}\"\n" "\n"); break; case CISCO_CARD_TYPE_NM: printf(" format:\n" " \"slot:sub_slot:nm_driver\"\n" "\n"); printf(" format:\n" " \"slot:port:netio_type{:netio_parameters}\"\n" "\n"); break; case CISCO_CARD_TYPE_WIC: printf(" format:\n" " \"slot:wic_driver\"\n" "\n"); printf(" format:\n" " \"slot:port:netio_type{:netio_parameters}\"\n" "\n"); break; } if (vm->platform->show_spec_drivers != NULL) vm->platform->show_spec_drivers(); /* Show possible slot drivers */ vm_slot_show_drivers(vm); /* Show the possible NETIO types */ netio_show_types(); } /* Find an option in the command line */ static char *cli_find_option(int argc,char *argv[],char *opt) { int i; for(i=1;iname,instance_id))) goto exit_failure; opterr = 0; vtty_set_ctrlhandler(1); /* By default allow ctrl ] */ vtty_set_telnetmsg(1); /* By default allow telnet message */ while((option = getopt_long(argc,argv,options_list, cmd_line_lopts,NULL)) != -1) { switch(option) { /* Instance ID (already managed) */ case 'i': break; /* Platform (already managed) */ case 'P': break; /* RAM size */ case 'r': vm->ram_size = strtol(optarg, NULL, 10); printf("Virtual RAM size set to %d MB.\n",vm->ram_size); break; /* ROM size */ case 'o': vm->rom_size = strtol(optarg, NULL, 10); printf("Virtual ROM size set to %d MB.\n",vm->rom_size); break; /* NVRAM size */ case 'n': vm->nvram_size = strtol(optarg, NULL, 10); printf("NVRAM size set to %d KB.\n",vm->nvram_size); break; /* PCMCIA disk0 size */ case OPT_DISK0_SIZE: vm->pcmcia_disk_size[0] = atoi(optarg); printf("PCMCIA ATA disk0 size set to %u MB.\n", vm->pcmcia_disk_size[0]); break; /* PCMCIA disk1 size */ case OPT_DISK1_SIZE: vm->pcmcia_disk_size[1] = atoi(optarg); printf("PCMCIA ATA disk1 size set to %u MB.\n", vm->pcmcia_disk_size[1]); break; case OPT_NOCTRL: vtty_set_ctrlhandler(0); /* Ignore ctrl ] */ printf("Block ctrl+] access to monitor console.\n"); break; /* Config Register */ case 'c': vm->conf_reg_setup = strtol(optarg, NULL, 0); printf("Config. Register set to 0x%x.\n",vm->conf_reg_setup); break; /* IOS startup configuration file */ case 'C': case OPT_STARTUP_CONFIG_FILE: vm_ios_set_config(vm,optarg,vm->ios_private_config); break; /* IOS private configuration file */ case OPT_PRIVATE_CONFIG_FILE: vm_ios_set_config(vm,vm->ios_startup_config,optarg); break; /* Global console (vtty tcp) binding address */ case OPT_CONSOLE_BINDING_ADDR: console_binding_addr = strdup(optarg); printf("Console binding address set to %s\n", console_binding_addr); break; /* Use physical memory to emulate RAM (no-mapped file) */ case 'X': vm->ram_mmap = 0; break; /* Use a ghost file to simulate RAM */ case 'G': free(vm->ghost_ram_filename); vm->ghost_ram_filename = strdup(optarg); vm->ghost_status = VM_GHOST_RAM_USE; break; /* Generate a ghost RAM image */ case 'g': free(vm->ghost_ram_filename); vm->ghost_ram_filename = strdup(optarg); vm->ghost_status = VM_GHOST_RAM_GENERATE; break; /* Use sparse memory */ case OPT_SPARSE_MEM: vm->sparse_mem = TRUE; break; /* Alternate ROM */ case 'R': free(vm->rom_filename); vm->rom_filename = strdup(optarg); break; case OPT_NOTELMSG: vtty_set_telnetmsg(0); /* disable telnet greeting */ printf("Prevent telnet message on AUX/CONSOLE connecte.\n"); break; case OPT_FILEPID: if ((pid_file = fopen(optarg,"w"))) { fprintf(pid_file,"%d",getpid()); fclose(pid_file); } else { printf("Unable to save to %s.\n",optarg); } break; /* Idle PC */ case OPT_IDLE_PC: vm->idle_pc = strtoull(optarg,NULL,0); printf("Idle PC set to 0x%llx.\n",vm->idle_pc); break; /* Timer IRQ check interval */ case OPT_TIMER_ITV: vm->timer_irq_check_itv = atoi(optarg); break; /* Clock divisor */ case 'k': vm->clock_divisor = atoi(optarg); if (!vm->clock_divisor) { fprintf(stderr,"Invalid Clock Divisor specified!\n"); goto exit_failure; } printf("Using a clock divisor of %d.\n",vm->clock_divisor); break; /* Disable JIT */ case 'j': vm->jit_use = FALSE; break; /* VM debug level */ case OPT_VM_DEBUG: vm->debug_level = atoi(optarg); break; /* Log file */ case 'l': if (!(log_file_name = realloc(log_file_name, strlen(optarg)+1))) { fprintf(stderr,"Unable to set log file name.\n"); goto exit_failure; } strcpy(log_file_name, optarg); printf("Log file: writing to %s\n",log_file_name); break; #if DEBUG_SYM_TREE /* Symbol file */ case 'S': vm->sym_filename = strdup(optarg); break; #endif /* TCP server for Console Port */ case 'T': vm->vtty_con_type = VTTY_TYPE_TCP; vm->vtty_con_tcp_port = atoi(optarg); break; /* Serial interface for Console port */ case 'U': vm->vtty_con_type = VTTY_TYPE_SERIAL; if (vtty_parse_serial_option(&vm->vtty_con_serial_option,optarg)) { fprintf(stderr, "Invalid Console serial interface descriptor!\n"); goto exit_failure; } break; /* TCP server for AUX Port */ case 'A': vm->vtty_aux_type = VTTY_TYPE_TCP; vm->vtty_aux_tcp_port = atoi(optarg); break; /* Serial interface for AUX port */ case 'B': vm->vtty_aux_type = VTTY_TYPE_SERIAL; if (vtty_parse_serial_option(&vm->vtty_aux_serial_option,optarg)) { fprintf(stderr,"Invalid AUX serial interface descriptor!\n"); goto exit_failure; } break; /* Port settings */ case 'p': vm_slot_cmd_create(vm,optarg); break; /* NIO settings */ case 's': vm_slot_cmd_add_nio(vm,optarg); break; /* Virtual ATM switch */ case 'a': if (atmsw_start(optarg) == -1) goto exit_failure; break; /* Virtual ATM bridge */ case 'M': if (atm_bridge_start(optarg) == -1) goto exit_failure; break; /* Virtual Frame-Relay switch */ case 'f': if (frsw_start(optarg) == -1) goto exit_failure; break; /* Virtual Ethernet switch */ case 'E': if (ethsw_start(optarg) == -1) goto exit_failure; break; /* Virtual bridge */ case 'b': if (netio_bridge_start(optarg) == -1) goto exit_failure; break; #ifdef GEN_ETH /* Ethernet device list */ case 'e': gen_eth_show_dev_list(); goto exit_success; #endif /* Load plugin (already handled) */ case 'L': break; /* Oops ! */ case '?': show_usage(vm,argc,argv); goto exit_failure; /* Parse options specific to the platform */ default: if (vm->platform->cli_parse_options != NULL) /* If you get an option wrong, say which option is was */ /* Wont be pretty for a long option, but it will at least help */ if (vm->platform->cli_parse_options(vm,option) == -1) { printf("Flag not recognised: -%c\n",(char)option); goto exit_failure; } } } /* Last argument, this is the IOS filename */ if (optind == (argc - 1)) { /* setting IOS image file */ vm_ios_set_image(vm,argv[optind]); printf("IOS image file: %s\n\n",vm->ios_image); } else { /* IOS missing */ fprintf(stderr,"Please specify an IOS image filename\n"); show_usage(vm,argc,argv); goto exit_failure; } vm_release(vm); return(0); exit_success: if (vm) { vm_release(vm); vm_delete_instance("default"); } exit(EXIT_SUCCESS); return(-1); exit_failure: if (vm) { vm_release(vm); vm_delete_instance("default"); } exit(EXIT_FAILURE); return(-1); } /* * Run in hypervisor mode with a config file if the "-H" option * is present in command line. */ static int run_hypervisor(int argc,char *argv[]) { char *options_list = "H:l:hN:L:"; int i,option; char *index; size_t len; FILE *pid_file = NULL; // For saving the pid if requested vtty_set_ctrlhandler(1); /* By default allow ctrl ] */ vtty_set_telnetmsg(1); /* By default allow telnet message */ for(i=1;i 0) || (DEBUG_BLOCK_PERF_CNT > 0) { m_uint32_t counter,prev = 0,delta; while(vm->status == VM_STATUS_RUNNING) { counter = cpu_get_perf_counter(vm->boot_cpu); delta = counter - prev; prev = counter; printf("delta = %u\n",delta); sleep(1); } } #else /* Start instance monitoring */ vm_monitor(vm); #endif /* Free resources used by instance */ vm_release(vm); } else { hypervisor_tcp_server(hypervisor_ip_address,hypervisor_tcp_port); } dynamips_reset(); close_log_file(); return(0); }