/[dynamips]/upstream/dynamips-0.2.6-RC2/hv_vm.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /upstream/dynamips-0.2.6-RC2/hv_vm.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Sat Oct 6 16:01:44 2007 UTC (16 years, 5 months ago) by dpavlin
Original Path: upstream/dynamips-0.2.5/hv_vm.c
File MIME type: text/plain
File size: 11698 byte(s)
import 0.2.5 from upstream

1 /*
2 * Cisco 7200 (Predator) simulation platform.
3 * Copyright (c) 2006 Christophe Fillot (cf@utc.fr)
4 *
5 * Hypervisor generic VM routines.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <sys/mman.h>
15 #include <signal.h>
16 #include <fcntl.h>
17 #include <errno.h>
18 #include <assert.h>
19 #include <stdarg.h>
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <arpa/inet.h>
24 #include <pthread.h>
25
26 #include "mips64.h"
27 #include "cp0.h"
28 #include "dynamips.h"
29 #include "device.h"
30 #include "dev_c7200.h"
31 #include "dev_vtty.h"
32 #include "utils.h"
33 #include "base64.h"
34 #include "net.h"
35 #include "atm.h"
36 #include "frame_relay.h"
37 #include "crc.h"
38 #include "net_io.h"
39 #include "net_io_bridge.h"
40 #ifdef GEN_ETH
41 #include "gen_eth.h"
42 #endif
43 #include "registry.h"
44 #include "hypervisor.h"
45
46 /* Set debugging level */
47 static int cmd_set_debug_level(hypervisor_conn_t *conn,int argc,char *argv[])
48 {
49 vm_instance_t *vm;
50
51 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
52 return(-1);
53
54 vm->debug_level = atoi(argv[1]);
55
56 vm_release(vm);
57 hypervisor_send_reply(conn,HSC_INFO_OK,1,"O",argv[0]);
58 return(0);
59 }
60
61 /* Set IOS image filename */
62 static int cmd_set_ios(hypervisor_conn_t *conn,int argc,char *argv[])
63 {
64 vm_instance_t *vm;
65
66 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
67 return(-1);
68
69 if (vm_ios_set_image(vm,argv[1]) == -1) {
70 vm_release(vm);
71 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
72 "unable to store IOS image name for router '%s'",
73 argv[0]);
74 return(-1);
75 }
76
77 vm_release(vm);
78 hypervisor_send_reply(conn,HSC_INFO_OK,1,"IOS image set for '%s'",argv[0]);
79 return(0);
80 }
81
82 /* Set IOS configuration filename to load at startup */
83 static int cmd_set_config(hypervisor_conn_t *conn,int argc,char *argv[])
84 {
85 vm_instance_t *vm;
86
87 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
88 return(-1);
89
90 if (vm_ios_set_config(vm,argv[1]) == -1) {
91 vm_release(vm);
92 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
93 "unable to store IOS config for router '%s'",
94 argv[0]);
95 return(-1);
96 }
97
98 vm_release(vm);
99 hypervisor_send_reply(conn,HSC_INFO_OK,1,"IOS config file set for '%s'",
100 argv[0]);
101 return(0);
102 }
103
104 /* Set RAM size */
105 static int cmd_set_ram(hypervisor_conn_t *conn,int argc,char *argv[])
106 {
107 vm_instance_t *vm;
108
109 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
110 return(-1);
111
112 vm->ram_size = atoi(argv[1]);
113
114 vm_release(vm);
115 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
116 return(0);
117 }
118
119 /* Set NVRAM size */
120 static int cmd_set_nvram(hypervisor_conn_t *conn,int argc,char *argv[])
121 {
122 vm_instance_t *vm;
123
124 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
125 return(-1);
126
127 vm->nvram_size = atoi(argv[1]);
128
129 vm_release(vm);
130 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
131 return(0);
132 }
133
134 /* Enable/disable use of a memory-mapped file to simulate RAM */
135 static int cmd_set_ram_mmap(hypervisor_conn_t *conn,int argc,char *argv[])
136 {
137 vm_instance_t *vm;
138
139 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
140 return(-1);
141
142 vm->ram_mmap = atoi(argv[1]);
143
144 vm_release(vm);
145 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
146 return(0);
147 }
148
149 /* Set the clock divisor */
150 static int cmd_set_clock_divisor(hypervisor_conn_t *conn,int argc,char *argv[])
151 {
152 vm_instance_t *vm;
153 u_int clock_div;
154
155 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
156 return(-1);
157
158 if ((clock_div = atoi(argv[1])) != 0)
159 vm->clock_divisor = clock_div;
160
161 vm_release(vm);
162 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
163 return(0);
164 }
165
166 /* Set the idle PC */
167 static int cmd_set_idle_pc(hypervisor_conn_t *conn,int argc,char *argv[])
168 {
169 vm_instance_t *vm;
170
171 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
172 return(-1);
173
174 vm->idle_pc = strtoull(argv[1],NULL,0);
175
176 vm_release(vm);
177 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
178 return(0);
179 }
180
181 /* Set the exec area size */
182 static int cmd_set_exec_area(hypervisor_conn_t *conn,int argc,char *argv[])
183 {
184 vm_instance_t *vm;
185
186 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
187 return(-1);
188
189 vm->exec_area_size = atoi(argv[1]);
190
191 vm_release(vm);
192 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
193 return(0);
194 }
195
196 /* Set PCMCIA ATA disk0 size */
197 static int cmd_set_disk0(hypervisor_conn_t *conn,int argc,char *argv[])
198 {
199 vm_instance_t *vm;
200
201 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
202 return(-1);
203
204 vm->pcmcia_disk_size[0] = atoi(argv[1]);
205 vm_release(vm);
206 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
207 return(0);
208 }
209
210 /* Set PCMCIA ATA disk1 size */
211 static int cmd_set_disk1(hypervisor_conn_t *conn,int argc,char *argv[])
212 {
213 vm_instance_t *vm;
214
215 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
216 return(-1);
217
218 vm->pcmcia_disk_size[1] = atoi(argv[1]);
219 vm_release(vm);
220 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
221 return(0);
222 }
223
224 /* Set the config register used at startup */
225 static int cmd_set_conf_reg(hypervisor_conn_t *conn,int argc,char *argv[])
226 {
227 vm_instance_t *vm;
228
229 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
230 return(-1);
231
232 vm->conf_reg_setup = strtol(argv[1],NULL,0);
233 vm_release(vm);
234 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
235 return(0);
236 }
237
238 /* Set TCP port for console */
239 static int cmd_set_con_tcp_port(hypervisor_conn_t *conn,int argc,char *argv[])
240 {
241 vm_instance_t *vm;
242
243 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
244 return(-1);
245
246 vm->vtty_con_type = VTTY_TYPE_TCP;
247 vm->vtty_con_tcp_port = atoi(argv[1]);
248
249 vm_release(vm);
250 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
251 return(0);
252 }
253
254 /* Set TCP port for AUX port */
255 static int cmd_set_aux_tcp_port(hypervisor_conn_t *conn,int argc,char *argv[])
256 {
257 vm_instance_t *vm;
258
259 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
260 return(-1);
261
262 vm->vtty_aux_type = VTTY_TYPE_TCP;
263 vm->vtty_aux_tcp_port = atoi(argv[1]);
264
265 vm_release(vm);
266 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
267 return(0);
268 }
269
270 /* Read an IOS configuration file from a given router */
271 static int cmd_extract_config(hypervisor_conn_t *conn,int argc,char *argv[])
272 {
273 vm_instance_t *vm;
274 char *cfg_buffer,*cfg_base64;
275 ssize_t cfg_len;
276
277 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
278 return(-1);
279
280 if (!vm->nvram_extract_config)
281 goto err_no_extract_method;
282
283 /* Extract the IOS configuration */
284 if (((cfg_len = vm->nvram_extract_config(vm,&cfg_buffer)) < 0) ||
285 (cfg_buffer == NULL))
286 goto err_nvram_extract;
287
288 /*
289 * Convert config to base64. base64 is about 1/3 larger than input,
290 * let's be on the safe side with twice longer.
291 */
292 if (!(cfg_base64 = malloc(cfg_len * 2)))
293 goto err_alloc_base64;
294
295 base64_encode(cfg_base64,cfg_buffer,cfg_len);
296
297 vm_release(vm);
298 hypervisor_send_reply(conn,HSC_INFO_OK,1,"conf '%s' %s",argv[0],cfg_base64);
299
300 free(cfg_buffer);
301 free(cfg_base64);
302 return(0);
303
304 err_alloc_base64:
305 free(cfg_buffer);
306 err_nvram_extract:
307 err_no_extract_method:
308 vm_release(vm);
309 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
310 "unable to extract config of VM '%s'",argv[0]);
311 return(-1);
312 }
313
314 /* Push an IOS configuration file */
315 static int cmd_push_config(hypervisor_conn_t *conn,int argc,char *argv[])
316 {
317 vm_instance_t *vm;
318 char *cfg_buffer;
319 ssize_t len;
320
321 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
322 return(-1);
323
324 if (!vm->nvram_push_config)
325 goto err_no_push_method;
326
327 /* Convert base64 input to standard text */
328 if (!(cfg_buffer = malloc(3 * strlen(argv[1]))))
329 goto err_alloc_base64;
330
331 if ((len = base64_decode(cfg_buffer,argv[1],0)) < 0)
332 goto err_decode_base64;
333
334 /* Push configuration */
335 if (vm->nvram_push_config(vm,cfg_buffer,len) < 0)
336 goto err_nvram_push;
337
338 free(cfg_buffer);
339 vm_release(vm);
340 hypervisor_send_reply(conn,HSC_INFO_OK,1,
341 "IOS config file pushed tm VM '%s'",
342 argv[0]);
343 return(0);
344
345 err_nvram_push:
346 err_decode_base64:
347 free(cfg_buffer);
348 err_alloc_base64:
349 err_no_push_method:
350 vm_release(vm);
351 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
352 "unable to push IOS config for VM '%s'",
353 argv[0]);
354 return(-1);
355 }
356
357 /* Show info about the specified CPU */
358 static int cmd_show_cpu_info(hypervisor_conn_t *conn,int argc,char *argv[])
359 {
360 vm_instance_t *vm;
361 cpu_mips_t *cpu;
362
363 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
364 return(-1);
365
366 cpu = cpu_group_find_id(vm->cpu_group,atoi(argv[1]));
367
368 if (cpu) {
369 mips64_dump_regs(cpu);
370 tlb_dump(cpu);
371 }
372
373 vm_release(vm);
374 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
375 return(0);
376 }
377
378 /* Suspend a VM instance */
379 static int cmd_suspend(hypervisor_conn_t *conn,int argc,char *argv[])
380 {
381 vm_instance_t *vm;
382
383 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
384 return(-1);
385
386 vm_suspend(vm);
387
388 vm_release(vm);
389 hypervisor_send_reply(conn,HSC_INFO_OK,1,"VM '%s' suspended",argv[0]);
390 return(0);
391 }
392
393 /* Resume a VM instance */
394 static int cmd_resume(hypervisor_conn_t *conn,int argc,char *argv[])
395 {
396 vm_instance_t *vm;
397
398 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
399 return(-1);
400
401 vm_resume(vm);
402
403 vm_release(vm);
404 hypervisor_send_reply(conn,HSC_INFO_OK,1,"VM '%s' resumed",argv[0]);
405 return(0);
406 }
407
408 /* Show info about VM object */
409 static void cmd_show_vm_list(registry_entry_t *entry,void *opt,int *err)
410 {
411 hypervisor_conn_t *conn = opt;
412 vm_instance_t *vm = entry->data;
413
414 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s (%s)",
415 entry->name,vm_get_type(vm));
416 }
417
418 /* VM List */
419 static int cmd_vm_list(hypervisor_conn_t *conn,int argc,char *argv[])
420 {
421 int err = 0;
422 registry_foreach_type(OBJ_TYPE_VM,cmd_show_vm_list,conn,&err);
423 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
424 return(0);
425 }
426
427 /* VM commands */
428 static hypervisor_cmd_t vm_cmd_array[] = {
429 { "set_debug_level", 2, 2, cmd_set_debug_level, NULL },
430 { "set_ios", 2, 2, cmd_set_ios, NULL },
431 { "set_config", 2, 2, cmd_set_config, NULL },
432 { "set_ram", 2, 2, cmd_set_ram, NULL },
433 { "set_nvram", 2, 2, cmd_set_nvram, NULL },
434 { "set_ram_mmap", 2, 2, cmd_set_ram_mmap, NULL },
435 { "set_clock_divisor", 2, 2, cmd_set_clock_divisor, NULL },
436 { "set_exec_area", 2, 2, cmd_set_exec_area, NULL },
437 { "set_disk0", 2, 2, cmd_set_disk0, NULL },
438 { "set_disk1", 2, 2, cmd_set_disk1, NULL },
439 { "set_conf_reg", 2, 2, cmd_set_conf_reg, NULL },
440 { "set_idle_pc", 2, 2, cmd_set_idle_pc, NULL },
441 { "set_con_tcp_port", 2, 2, cmd_set_con_tcp_port, NULL },
442 { "set_aux_tcp_port", 2, 2, cmd_set_aux_tcp_port, NULL },
443 { "extract_config", 1, 1, cmd_extract_config, NULL },
444 { "push_config", 2, 2, cmd_push_config, NULL },
445 { "cpu_info", 2, 2, cmd_show_cpu_info, NULL },
446 { "suspend", 1, 1, cmd_suspend, NULL },
447 { "resume", 1, 1, cmd_resume, NULL },
448 { "list", 0, 0, cmd_vm_list, NULL },
449 { NULL, -1, -1, NULL, NULL },
450 };
451
452 /* Hypervisor VM initialization */
453 int hypervisor_vm_init(void)
454 {
455 hypervisor_module_t *module;
456
457 module = hypervisor_register_module("vm");
458 assert(module != NULL);
459
460 hypervisor_register_cmd_array(module,vm_cmd_array);
461 return(0);
462 }

  ViewVC Help
Powered by ViewVC 1.1.26