Debian Codegraph @

stable/hv_vm.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2006 Christophe Fillot ([email protected])
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 <signal.h>
15 #include <fcntl.h>
16 #include <errno.h>
17 #include <assert.h>
18 #include <stdarg.h>
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <arpa/inet.h>
23 #include <pthread.h>
24 #include <glob.h>
25
26 #include "cpu.h"
27 #include "vm.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 #include "get_cpu_time.h"
46
47 /* Find the specified CPU */
48 static cpu_gen_t *find_cpu(hypervisor_conn_t *conn,vm_instance_t *vm,
49 u_int cpu_id)
50 {
51 cpu_gen_t *cpu;
52
53 cpu = cpu_group_find_id(vm->cpu_group,cpu_id);
54
55 if (!cpu) {
56 vm_release(vm);
57 hypervisor_send_reply(conn,HSC_ERR_BAD_OBJ,1,"Bad CPU specified");
58 return NULL;
59 }
60
61 return cpu;
62 }
63
64 /* Create a VM instance */
65 static int cmd_create(hypervisor_conn_t *conn,int argc,char *argv[])
66 {
67 vm_instance_t *vm;
68
69 if (!(vm = vm_create_instance(argv[0],atoi(argv[1]),argv[2]))) {
70 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
71 "unable to create VM instance '%s'",
72 argv[0]);
73 return(-1);
74 }
75
76 vm->vtty_con_type = VTTY_TYPE_NONE;
77 vm->vtty_aux_type = VTTY_TYPE_NONE;
78
79 vm_release(vm);
80 hypervisor_send_reply(conn,HSC_INFO_OK,1,"VM '%s' created",argv[0]);
81 return(0);
82 }
83
84 /* Rename a VM instance */
85 static int cmd_rename(hypervisor_conn_t *conn,int argc,char *argv[])
86 {
87 vm_instance_t *vm;
88
89 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
90 return(-1);
91
92 if (registry_exists(argv[1],OBJ_TYPE_VM)) {
93 vm_release(vm);
94 hypervisor_send_reply(conn,HSC_ERR_RENAME,1,
95 "unable to rename VM instance '%s', '%s' already exists",
96 argv[0],argv[1]);
97 return(-1);
98 }
99
100 if (vm_rename_instance(vm,argv[1])) {
101 hypervisor_send_reply(conn,HSC_ERR_RENAME,1,
102 "unable to rename VM instance '%s'",
103 argv[0]);
104 return(-1);
105 }
106
107 vm_release(vm);
108 hypervisor_send_reply(conn,HSC_INFO_OK,1,"VM '%s' renamed to '%s'",argv[0],argv[1]);
109 return(0);
110 }
111
112 /* Delete a VM instance */
113 static int cmd_delete(hypervisor_conn_t *conn,int argc,char *argv[])
114 {
115 int res;
116
117 res = vm_delete_instance(argv[0]);
118
119 if (res == 1) {
120 hypervisor_send_reply(conn,HSC_INFO_OK,1,"VM '%s' deleted",argv[0]);
121 } else {
122 hypervisor_send_reply(conn,HSC_ERR_DELETE,1,
123 "unable to delete VM '%s'",argv[0]);
124 }
125
126 return(res);
127 }
128
129 /* Delete a VM instance and related files */
130 static int cmd_clean_delete(hypervisor_conn_t *conn,int argc,char *argv[])
131 {
132 int res, i;
133 glob_t globbuf;
134 char *pattern;
135 vm_instance_t *vm;
136
137 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
138 return(-1);
139
140 pattern = vm_build_filename(vm, "*");
141 vm_release(vm);
142 res = vm_delete_instance(argv[0]);
143
144 if (res == 1) {
145 /* delete related files (best effort) */
146 if (pattern != NULL && glob(pattern, GLOB_NOSORT, NULL, &globbuf) == 0) {
147 for (i = 0; i < globbuf.gl_pathc; i++) {
148 remove(globbuf.gl_pathv[i]);
149 }
150
151 globfree(&globbuf);
152 }
153
154 hypervisor_send_reply(conn,HSC_INFO_OK,1,"VM '%s' and related files deleted",argv[0]);
155 } else {
156 hypervisor_send_reply(conn,HSC_ERR_DELETE,1,
157 "unable to delete VM '%s'",argv[0]);
158 }
159
160 free(pattern);
161 return(res);
162 }
163
164 /* Start a VM instance */
165 static int cmd_start(hypervisor_conn_t *conn,int argc,char *argv[])
166 {
167 vm_instance_t *vm;
168
169 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
170 return(-1);
171
172 if (vm->vtty_con_type == VTTY_TYPE_NONE) {
173 hypervisor_send_reply(conn,HSC_INFO_MSG,0,
174 "Warning: no console port defined for "
175 "VM '%s'",argv[0]);
176 }
177
178 if (vm_init_instance(vm) == -1) {
179 vm_release(vm);
180 hypervisor_send_reply(conn,HSC_ERR_START,1,
181 "unable to start VM instance '%s'",
182 argv[0]);
183 return(-1);
184 }
185
186 vm_release(vm);
187 hypervisor_send_reply(conn,HSC_INFO_OK,1,"VM '%s' started",argv[0]);
188 return(0);
189 }
190
191 /* Stop a VM instance */
192 static int cmd_stop(hypervisor_conn_t *conn,int argc,char *argv[])
193 {
194 vm_instance_t *vm;
195
196 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
197 return(-1);
198
199 if (vm_stop_instance(vm) == -1) {
200 vm_release(vm);
201 hypervisor_send_reply(conn,HSC_ERR_STOP,1,
202 "unable to stop VM instance '%s'",
203 argv[0]);
204 return(-1);
205 }
206
207 vm_release(vm);
208 hypervisor_send_reply(conn,HSC_INFO_OK,1,"VM '%s' stopped",argv[0]);
209 return(0);
210 }
211
212 /* Get the status of a VM instance */
213 static int cmd_get_status(hypervisor_conn_t *conn,int argc,char *argv[])
214 {
215 vm_instance_t *vm;
216 int status;
217
218 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
219 return(-1);
220
221 status = vm->status;
222
223 vm_release(vm);
224 hypervisor_send_reply(conn,HSC_INFO_OK,1,"%d",status);
225 return(0);
226 }
227
228 /* Set debugging level */
229 static int cmd_set_debug_level(hypervisor_conn_t *conn,int argc,char *argv[])
230 {
231 vm_instance_t *vm;
232
233 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
234 return(-1);
235
236 vm->debug_level = atoi(argv[1]);
237
238 vm_release(vm);
239 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
240 return(0);
241 }
242
243 /* Set IOS image filename */
244 static int cmd_set_ios(hypervisor_conn_t *conn,int argc,char *argv[])
245 {
246 vm_instance_t *vm;
247
248 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
249 return(-1);
250
251 if (vm_ios_set_image(vm,argv[1]) == -1) {
252 vm_release(vm);
253 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
254 "unable to store IOS image name for router '%s'",
255 argv[0]);
256 return(-1);
257 }
258
259 vm_release(vm);
260 hypervisor_send_reply(conn,HSC_INFO_OK,1,"IOS image set for '%s'",argv[0]);
261 return(0);
262 }
263
264 /* Set IOS configuration filename to load at startup */
265 static int cmd_set_config(hypervisor_conn_t *conn,int argc,char *argv[])
266 {
267 vm_instance_t *vm;
268 const char *startup_filename = argv[1];
269 const char *private_filename = ((argc > 2) ? argv[2] : "");
270
271 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
272 return(-1);
273
274 if (0 == strlen(startup_filename))
275 startup_filename = NULL; // keep existing data
276
277 if (0 == strlen(private_filename))
278 private_filename = NULL; // keep existing data
279
280 if (vm_ios_set_config(vm,startup_filename,private_filename) == -1) {
281 vm_release(vm);
282 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
283 "unable to store IOS config for router '%s'",
284 argv[0]);
285 return(-1);
286 }
287
288 vm_release(vm);
289 hypervisor_send_reply(conn,HSC_INFO_OK,1,"IOS config file set for '%s'",
290 argv[0]);
291 return(0);
292 }
293
294 /* Set RAM size */
295 static int cmd_set_ram(hypervisor_conn_t *conn,int argc,char *argv[])
296 {
297 vm_instance_t *vm;
298
299 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
300 return(-1);
301
302 vm->ram_size = atoi(argv[1]);
303
304 /* XXX npe-400 can split excess ram into iomem, adjusting ram_size */
305 if (vm->elf_machine_id == C7200_ELF_MACHINE_ID)
306 VM_C7200(vm)->npe400_ram_size = vm->ram_size;
307
308 vm_release(vm);
309 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
310 return(0);
311 }
312
313 /* Set NVRAM size */
314 static int cmd_set_nvram(hypervisor_conn_t *conn,int argc,char *argv[])
315 {
316 vm_instance_t *vm;
317
318 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
319 return(-1);
320
321 vm->nvram_size = atoi(argv[1]);
322
323 vm_release(vm);
324 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
325 return(0);
326 }
327
328 /* Enable/disable use of a memory-mapped file to simulate RAM */
329 static int cmd_set_ram_mmap(hypervisor_conn_t *conn,int argc,char *argv[])
330 {
331 vm_instance_t *vm;
332
333 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
334 return(-1);
335
336 vm->ram_mmap = atoi(argv[1]);
337
338 vm_release(vm);
339 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
340 return(0);
341 }
342
343 /* Enable/disable use of sparse memory */
344 static int cmd_set_sparse_mem(hypervisor_conn_t *conn,int argc,char *argv[])
345 {
346 vm_instance_t *vm;
347
348 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
349 return(-1);
350
351 vm->sparse_mem = atoi(argv[1]);
352
353 vm_release(vm);
354 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
355 return(0);
356 }
357
358 /* Set the clock divisor */
359 static int cmd_set_clock_divisor(hypervisor_conn_t *conn,int argc,char *argv[])
360 {
361 vm_instance_t *vm;
362 u_int clock_div;
363
364 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
365 return(-1);
366
367 if ((clock_div = atoi(argv[1])) != 0)
368 vm->clock_divisor = clock_div;
369
370 vm_release(vm);
371 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
372 return(0);
373 }
374
375 /* Enable/disable use of block direct jump (compatibility option) */
376 static int cmd_set_blk_direct_jump(hypervisor_conn_t *conn,
377 int argc,char *argv[])
378 {
379 vm_instance_t *vm;
380
381 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
382 return(-1);
383
384 vm->exec_blk_direct_jump = atoi(argv[1]);
385
386 vm_release(vm);
387 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
388 return(0);
389 }
390
391 /* Set the idle PC */
392 static int cmd_set_idle_pc(hypervisor_conn_t *conn,int argc,char *argv[])
393 {
394 vm_instance_t *vm;
395
396 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
397 return(-1);
398
399 vm->idle_pc = strtoull(argv[1],NULL,0);
400
401 vm_release(vm);
402 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
403 return(0);
404 }
405
406 /* Set the idle PC value when the CPU is online */
407 static int cmd_set_idle_pc_online(hypervisor_conn_t *conn,
408 int argc,char *argv[])
409 {
410 vm_instance_t *vm;
411 cpu_gen_t *cpu;
412
413 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
414 return(-1);
415
416 if (!(cpu = find_cpu(conn,vm,atoi(argv[1]))))
417 return(-1);
418
419 cpu->set_idle_pc(cpu,strtoull(argv[2],NULL,0));
420
421 vm_release(vm);
422 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
423 return(0);
424 }
425
426 /* Get the idle PC proposals */
427 static int cmd_get_idle_pc_prop(hypervisor_conn_t *conn,int argc,char *argv[])
428 {
429 vm_instance_t *vm;
430 cpu_gen_t *cpu;
431 int i;
432
433 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
434 return(-1);
435
436 if (!(cpu = find_cpu(conn,vm,atoi(argv[1]))))
437 return(-1);
438
439 cpu->get_idling_pc(cpu);
440
441 for(i=0;i<cpu->idle_pc_prop_count;i++) {
442 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"0x%llx [%d]",
443 cpu->idle_pc_prop[i].pc,
444 cpu->idle_pc_prop[i].count);
445 }
446
447 vm_release(vm);
448 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
449 return(0);
450 }
451
452 /* Dump the idle PC proposals */
453 static int cmd_show_idle_pc_prop(hypervisor_conn_t *conn,int argc,char *argv[])
454 {
455 vm_instance_t *vm;
456 cpu_gen_t *cpu;
457 int i;
458
459 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
460 return(-1);
461
462 if (!(cpu = find_cpu(conn,vm,atoi(argv[1]))))
463 return(-1);
464
465 for(i=0;i<cpu->idle_pc_prop_count;i++) {
466 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"0x%llx [%d]",
467 cpu->idle_pc_prop[i].pc,
468 cpu->idle_pc_prop[i].count);
469 }
470
471 vm_release(vm);
472 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
473 return(0);
474 }
475
476 /* Set CPU idle max value */
477 static int cmd_set_idle_max(hypervisor_conn_t *conn,int argc,char *argv[])
478 {
479 vm_instance_t *vm;
480 cpu_gen_t *cpu;
481
482 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
483 return(-1);
484
485 if (!(cpu = find_cpu(conn,vm,atoi(argv[1]))))
486 return(-1);
487
488 cpu->idle_max = atoi(argv[2]);
489
490 vm_release(vm);
491 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
492 return(0);
493 }
494
495 /* Set CPU idle sleep time value */
496 static int cmd_set_idle_sleep_time(hypervisor_conn_t *conn,
497 int argc,char *argv[])
498 {
499 vm_instance_t *vm;
500 cpu_gen_t *cpu;
501
502 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
503 return(-1);
504
505 if (!(cpu = find_cpu(conn,vm,atoi(argv[1]))))
506 return(-1);
507
508 cpu->idle_sleep_time = atoi(argv[2]);
509
510 vm_release(vm);
511 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
512 return(0);
513 }
514
515 /* Show info about potential timer drift */
516 static int cmd_show_timer_drift(hypervisor_conn_t *conn,
517 int argc,char *argv[])
518 {
519 vm_instance_t *vm;
520 cpu_gen_t *cpu;
521
522 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
523 return(-1);
524
525 if (!(cpu = find_cpu(conn,vm,atoi(argv[1]))))
526 return(-1);
527
528 switch(cpu->type) {
529 case CPU_TYPE_MIPS64:
530 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"Timer Drift: %u",
531 CPU_MIPS64(cpu)->timer_drift);
532
533 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"Pending Timer IRQ: %u",
534 CPU_MIPS64(cpu)->timer_irq_pending);
535 break;
536
537 case CPU_TYPE_PPC32:
538 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"Timer Drift: %u",
539 CPU_PPC32(cpu)->timer_drift);
540
541 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"Pending Timer IRQ: %u",
542 CPU_PPC32(cpu)->timer_irq_pending);
543 break;
544 }
545
546 vm_release(vm);
547 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
548 return(0);
549 }
550
551 /* Set the exec area size */
552 static int cmd_set_exec_area(hypervisor_conn_t *conn,int argc,char *argv[])
553 {
554 vm_instance_t *vm;
555
556 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
557 return(-1);
558
559 vm->exec_area_size = atoi(argv[1]);
560
561 vm_release(vm);
562 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
563 return(0);
564 }
565
566 /* Set ghost RAM file */
567 static int cmd_set_ghost_file(hypervisor_conn_t *conn,int argc,char *argv[])
568 {
569 vm_instance_t *vm;
570
571 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
572 return(-1);
573
574 free(vm->ghost_ram_filename);
575 vm->ghost_ram_filename = strdup(argv[1]);
576
577 vm_release(vm);
578 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
579 return(0);
580 }
581
582 /* Set ghost RAM status */
583 static int cmd_set_ghost_status(hypervisor_conn_t *conn,int argc,char *argv[])
584 {
585 vm_instance_t *vm;
586
587 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
588 return(-1);
589
590 vm->ghost_status = atoi(argv[1]);
591
592 vm_release(vm);
593 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
594 return(0);
595 }
596
597
598 /* Set PCMCIA ATA disk0 size */
599 static int cmd_set_disk0(hypervisor_conn_t *conn,int argc,char *argv[])
600 {
601 vm_instance_t *vm;
602
603 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
604 return(-1);
605
606 vm->pcmcia_disk_size[0] = atoi(argv[1]);
607 vm_release(vm);
608 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
609 return(0);
610 }
611
612 /* Set PCMCIA ATA disk1 size */
613 static int cmd_set_disk1(hypervisor_conn_t *conn,int argc,char *argv[])
614 {
615 vm_instance_t *vm;
616
617 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
618 return(-1);
619
620 vm->pcmcia_disk_size[1] = atoi(argv[1]);
621 vm_release(vm);
622 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
623 return(0);
624 }
625
626 /* Set the config register used at startup */
627 static int cmd_set_conf_reg(hypervisor_conn_t *conn,int argc,char *argv[])
628 {
629 vm_instance_t *vm;
630
631 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
632 return(-1);
633
634 vm->conf_reg_setup = strtol(argv[1],NULL,0);
635 vm_release(vm);
636 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
637 return(0);
638 }
639
640 /* Set TCP port for console */
641 static int cmd_set_con_tcp_port(hypervisor_conn_t *conn,int argc,char *argv[])
642 {
643 vm_instance_t *vm;
644
645 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
646 return(-1);
647
648 vm->vtty_con_type = VTTY_TYPE_TCP;
649 vm->vtty_con_tcp_port = atoi(argv[1]);
650
651 vm_release(vm);
652 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
653 return(0);
654 }
655
656 /* Set TCP port for AUX port */
657 static int cmd_set_aux_tcp_port(hypervisor_conn_t *conn,int argc,char *argv[])
658 {
659 vm_instance_t *vm;
660
661 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
662 return(-1);
663
664 vm->vtty_aux_type = VTTY_TYPE_TCP;
665 vm->vtty_aux_tcp_port = atoi(argv[1]);
666
667 vm_release(vm);
668 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
669 return(0);
670 }
671
672 /* Read IOS configuration files from a given router */
673 static int cmd_extract_config(hypervisor_conn_t *conn,int argc,char *argv[])
674 {
675 vm_instance_t *vm;
676 u_char *startup_config = NULL;
677 u_char *private_config = NULL;
678 size_t startup_len;
679 size_t private_len;
680 u_char *startup_base64 = NULL;
681 u_char *private_base64 = NULL;
682
683 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
684 return(-1);
685
686 if (!vm->platform->nvram_extract_config)
687 goto err_no_extract_method;
688
689 /* Extract the IOS configuration */
690 if ((vm->platform->nvram_extract_config(vm,&startup_config,&startup_len,&private_config,&private_len)))
691 goto err_nvram_extract;
692
693 /*
694 * Convert config to base64. base64 generates 4 bytes for each group of 3 bytes.
695 */
696 if (!(startup_base64 = malloc(1 + (startup_len + 2) / 3 * 4)) ||
697 !(private_base64 = malloc(1 + (private_len + 2) / 3 * 4)))
698 goto err_alloc_base64;
699
700 base64_encode(startup_base64,startup_config,startup_len);
701 base64_encode(private_base64,private_config,private_len);
702
703 vm_release(vm);
704 hypervisor_send_reply(conn,HSC_INFO_OK,1,"conf '%s' '%s' '%s'",argv[0],startup_base64,private_base64);
705
706 free(private_base64);
707 free(startup_base64);
708 free(private_config);
709 free(startup_config);
710 return(0);
711
712 err_alloc_base64:
713 free(private_base64);
714 free(startup_base64);
715 free(private_config);
716 free(startup_config);
717 err_nvram_extract:
718 err_no_extract_method:
719 vm_release(vm);
720 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
721 "unable to extract config of VM '%s'",argv[0]);
722 return(-1);
723 }
724
725 /* Push IOS configuration to a given router */
726 static int cmd_push_config(hypervisor_conn_t *conn,int argc,char *argv[])
727 {
728 vm_instance_t *vm;
729 u_char *startup_config = NULL;
730 u_char *private_config = NULL;
731 int startup_len = 0;
732 int private_len = 0;
733
734 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
735 return(-1);
736
737 if (!vm->platform->nvram_push_config)
738 goto err_no_push_method;
739
740 /*
741 * Convert base64 input to standard text. base64 uses 4 bytes for each group of 3 bytes.
742 */
743 if (strcmp(argv[1],"(keep)") != 0) {
744 startup_len = (strlen(argv[1]) + 3) / 4 * 3;
745 if (!(startup_config = malloc(1 + startup_len)))
746 goto err_alloc_base64;
747
748 if ((startup_len = base64_decode(startup_config,(u_char *)argv[1],startup_len)) < 0)
749 goto err_decode_base64;
750
751 startup_config[startup_len] = '\0';
752 }
753
754 if (argc > 2 && strcmp(argv[2],"(keep)") != 0) {
755 private_len = (strlen(argv[2]) + 3) / 4 * 3;
756 if (!(private_config = malloc(1 + private_len)))
757 goto err_alloc_base64;
758
759 if ((private_len = base64_decode(private_config,(u_char *)argv[2],private_len)) < 0)
760 goto err_decode_base64;
761
762 private_config[private_len] = '\0';
763 }
764
765 /* Push configuration */
766 if (vm->platform->nvram_push_config(vm,startup_config,(size_t)startup_len,private_config,(size_t)private_len) < 0)
767 goto err_nvram_push;
768
769 free(private_config);
770 free(startup_config);
771 vm_release(vm);
772 hypervisor_send_reply(conn,HSC_INFO_OK,1,
773 "IOS config file pushed tm VM '%s'",
774 argv[0]);
775 return(0);
776
777 err_nvram_push:
778 err_decode_base64:
779 err_alloc_base64:
780 free(private_config);
781 free(startup_config);
782 err_no_push_method:
783 vm_release(vm);
784 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
785 "unable to push IOS config for VM '%s'",
786 argv[0]);
787 return(-1);
788 }
789
790 /* Show info about the specified CPU */
791 static int cmd_show_cpu_info(hypervisor_conn_t *conn,int argc,char *argv[])
792 {
793 vm_instance_t *vm;
794 cpu_gen_t *cpu;
795
796 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
797 return(-1);
798
799 cpu = cpu_group_find_id(vm->cpu_group,atoi(argv[1]));
800
801 if (cpu) {
802 cpu->reg_dump(cpu);
803 cpu->mmu_dump(cpu);
804 }
805
806 vm_release(vm);
807 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
808 return(0);
809 }
810
811 /* Show CPU usage - experimental */
812 static int cmd_show_cpu_usage(hypervisor_conn_t *conn,int argc,char *argv[])
813 {
814 vm_instance_t *vm;
815 double usage;
816
817 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
818 return(-1);
819
820 usage = get_cpu_time();
821 if (usage == -1)
822 return(-1);
823 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%lu", (unsigned long)usage);
824
825 vm_release(vm);
826 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
827 return(0);
828 }
829
830 /* Suspend a VM instance */
831 static int cmd_suspend(hypervisor_conn_t *conn,int argc,char *argv[])
832 {
833 vm_instance_t *vm;
834
835 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
836 return(-1);
837
838 vm_suspend(vm);
839
840 vm_release(vm);
841 hypervisor_send_reply(conn,HSC_INFO_OK,1,"VM '%s' suspended",argv[0]);
842 return(0);
843 }
844
845 /* Resume a VM instance */
846 static int cmd_resume(hypervisor_conn_t *conn,int argc,char *argv[])
847 {
848 vm_instance_t *vm;
849
850 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
851 return(-1);
852
853 vm_resume(vm);
854
855 vm_release(vm);
856 hypervisor_send_reply(conn,HSC_INFO_OK,1,"VM '%s' resumed",argv[0]);
857 return(0);
858 }
859
860 /* Send a message on the console */
861 static int cmd_send_con_msg(hypervisor_conn_t *conn,int argc,char *argv[])
862 {
863 vm_instance_t *vm = NULL;
864 const char *format = NULL;
865 char *data = NULL;
866 int len,written;
867
868 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
869 return(-1);
870
871 format = (argc > 2)? argv[2]: "plain";
872 len = (int)strlen(argv[1]);
873 written = -1;
874
875 if (strcmp(format,"plain") == 0 && len >= 0) {
876 written = vtty_store_data(vm->vtty_con,argv[1],len);
877 }
878 else if (strcmp(format,"base64") == 0 && len >= 0) {
879 data = malloc(len+1);
880 len = base64_decode((unsigned char*)data,(const unsigned char *)argv[1],len);
881 written = vtty_store_data(vm->vtty_con,data,len);
882 free(data);
883 data = NULL;
884 }
885
886 vm_release(vm);
887 if (written < 0) {
888 hypervisor_send_reply(conn,HSC_ERR_INV_PARAM,1,"Invalid parameter");
889 return(-1);
890 }
891 else {
892 hypervisor_send_reply(conn,HSC_INFO_OK,1,"%d byte(s) written",written);
893 return(0);
894 }
895 }
896
897 /* Send a message on the AUX port */
898 static int cmd_send_aux_msg(hypervisor_conn_t *conn,int argc,char *argv[])
899 {
900 vm_instance_t *vm = NULL;
901 const char *format = NULL;
902 char *data = NULL;
903 int len,written;
904
905 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
906 return(-1);
907
908 format = (argc > 2)? argv[2]: "plain";
909 len = (int)strlen(argv[1]);
910 written = -1;
911
912 if (strcmp(format,"plain") == 0 && len >= 0) {
913 written = vtty_store_data(vm->vtty_aux,argv[1],len);
914 }
915 else if (strcmp(format,"base64") == 0 && len >= 0) {
916 data = malloc(len+1);
917 len = base64_decode((unsigned char*)data,(const unsigned char *)argv[1],len);
918 written = vtty_store_data(vm->vtty_aux,data,len);
919 free(data);
920 data = NULL;
921 }
922
923 vm_release(vm);
924 if (written < 0) {
925 hypervisor_send_reply(conn,HSC_ERR_INV_PARAM,1,"Invalid parameter");
926 return(-1);
927 }
928 else {
929 hypervisor_send_reply(conn,HSC_INFO_OK,1,"%d byte(s) written",written);
930 return(0);
931 }
932 }
933
934
935 /* Show slot bindings */
936 static int cmd_slot_bindings(hypervisor_conn_t *conn,int argc,char *argv[])
937 {
938 struct cisco_card *card,*sc;
939 vm_instance_t *vm;
940 int i,j;
941
942 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
943 return(-1);
944
945 for(i=0;i<vm->nr_slots;i++) {
946 if (!(card = vm_slot_get_card_ptr(vm,i)))
947 continue;
948
949 /* main module */
950 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%u/%u: %s",
951 card->slot_id,card->subslot_id,card->dev_type);
952
953 /* sub-slots */
954 for(j=0;j<CISCO_CARD_MAX_SUBSLOTS;j++) {
955 if (!(sc = card->sub_slots[j]))
956 continue;
957
958 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%u/%u: %s",
959 card->slot_id,card->subslot_id,card->dev_type);
960 }
961 }
962
963 vm_release(vm);
964 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
965 return(0);
966 }
967
968 /* Show NIO bindings for the specified slot */
969 static int cmd_slot_nio_bindings(hypervisor_conn_t *conn,int argc,char *argv[])
970 {
971 struct cisco_nio_binding *nb;
972 struct cisco_card *card,*sc;
973 vm_instance_t *vm;
974 u_int i,slot;
975
976 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
977 return(-1);
978
979 slot = atoi(argv[1]);
980
981 if ((card = vm_slot_get_card_ptr(vm,slot)))
982 {
983 /* main module */
984 for(nb=card->nio_list;nb;nb=nb->next) {
985 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%u: %s",
986 nb->port_id,nb->nio->name);
987 }
988
989 /* sub-slots */
990 for(i=0;i<CISCO_CARD_MAX_SUBSLOTS;i++) {
991 if (!(sc = card->sub_slots[i]))
992 continue;
993
994 for(nb=sc->nio_list;nb;nb=nb->next) {
995 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%u: %s",
996 nb->port_id,nb->nio->name);
997 }
998 }
999 }
1000
1001 vm_release(vm);
1002 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
1003 return(0);
1004 }
1005
1006 /* Add a slot binding */
1007 static int cmd_slot_add_binding(hypervisor_conn_t *conn,int argc,char *argv[])
1008 {
1009 vm_instance_t *vm;
1010 u_int slot,port;
1011
1012 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
1013 return(-1);
1014
1015 slot = atoi(argv[1]);
1016 port = atoi(argv[2]);
1017
1018 if (vm_slot_add_binding(vm,argv[3],slot,port) == -1) {
1019 vm_release(vm);
1020 hypervisor_send_reply(conn,HSC_ERR_BINDING,1,
1021 "VM %s: unable to add binding for slot %u/%u",
1022 argv[0],slot,port);
1023 return(-1);
1024 }
1025
1026 vm_release(vm);
1027 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
1028 return(0);
1029 }
1030
1031 /* Remove a slot binding */
1032 static int cmd_slot_remove_binding(hypervisor_conn_t *conn,
1033 int argc,char *argv[])
1034 {
1035 vm_instance_t *vm;
1036 u_int slot,port;
1037
1038 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
1039 return(-1);
1040
1041 slot = atoi(argv[1]);
1042 port = atoi(argv[2]);
1043
1044 if (vm_slot_remove_binding(vm,slot,port) == -1) {
1045 vm_release(vm);
1046 hypervisor_send_reply(conn,HSC_ERR_BINDING,1,
1047 "VM %s: unable to remove binding for slot %u/%u",
1048 argv[0],slot,port);
1049 return(-1);
1050 }
1051
1052 vm_release(vm);
1053 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
1054 return(0);
1055 }
1056
1057 /* Add a NIO binding for a slot/port */
1058 static int cmd_slot_add_nio_binding(hypervisor_conn_t *conn,
1059 int argc,char *argv[])
1060 {
1061 vm_instance_t *vm;
1062 u_int slot,port;
1063
1064 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
1065 return(-1);
1066
1067 slot = atoi(argv[1]);
1068 port = atoi(argv[2]);
1069
1070 if (vm_slot_add_nio_binding(vm,slot,port,argv[3]) == -1) {
1071 vm_release(vm);
1072 hypervisor_send_reply(conn,HSC_ERR_BINDING,1,
1073 "VM %s: unable to add binding "
1074 "for slot %u/%u",argv[0],slot,port);
1075 return(-1);
1076 }
1077
1078 vm_release(vm);
1079 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
1080 return(0);
1081 }
1082
1083 /* Remove a NIO binding for a slot/port */
1084 static int cmd_slot_remove_nio_binding(hypervisor_conn_t *conn,
1085 int argc,char *argv[])
1086 {
1087 vm_instance_t *vm;
1088 u_int slot,port;
1089
1090 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
1091 return(-1);
1092
1093 slot = atoi(argv[1]);
1094 port = atoi(argv[2]);
1095
1096 if (vm_slot_remove_nio_binding(vm,slot,port) == -1) {
1097 vm_release(vm);
1098 hypervisor_send_reply(conn,HSC_ERR_BINDING,1,
1099 "VM %s: unable to remove NIO binding "
1100 "for slot %u/%u",argv[0],slot,port);
1101 return(-1);
1102 }
1103
1104 vm_release(vm);
1105 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
1106 return(0);
1107 }
1108
1109 /* Enable NIO of the specified slot/port */
1110 static int cmd_slot_enable_nio(hypervisor_conn_t *conn,int argc,char *argv[])
1111 {
1112 vm_instance_t *vm;
1113 u_int slot,port;
1114
1115 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
1116 return(-1);
1117
1118 slot = atoi(argv[1]);
1119 port = atoi(argv[2]);
1120
1121 if (vm_slot_enable_nio(vm,slot,port) == -1) {
1122 vm_release(vm);
1123 hypervisor_send_reply(conn,HSC_ERR_BINDING,1,
1124 "VM %s: unable to enable NIO for slot %u/%u",
1125 argv[0],slot,port);
1126 return(-1);
1127 }
1128
1129 vm_release(vm);
1130 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
1131 return(0);
1132 }
1133
1134 /* Disable NIO of the specified slot/port */
1135 static int cmd_slot_disable_nio(hypervisor_conn_t *conn,int argc,char *argv[])
1136 {
1137 vm_instance_t *vm;
1138 u_int slot,port;
1139
1140 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
1141 return(-1);
1142
1143 slot = atoi(argv[1]);
1144 port = atoi(argv[2]);
1145
1146 if (vm_slot_disable_nio(vm,slot,port) == -1) {
1147 vm_release(vm);
1148 hypervisor_send_reply(conn,HSC_ERR_BINDING,1,
1149 "VM %s: unable to disable NIO for slot %u/%u",
1150 argv[0],slot,port);
1151 return(-1);
1152 }
1153
1154 vm_release(vm);
1155 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
1156 return(0);
1157 }
1158
1159 /* OIR to start a slot/subslot */
1160 static int cmd_slot_oir_start(hypervisor_conn_t *conn,int argc,char *argv[])
1161 {
1162 vm_instance_t *vm;
1163 u_int slot,subslot;
1164
1165 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
1166 return(-1);
1167
1168 slot = atoi(argv[1]);
1169 subslot = atoi(argv[2]);
1170
1171 if (vm_oir_start(vm,slot,subslot) == -1) {
1172 vm_release(vm);
1173 hypervisor_send_reply(conn,HSC_ERR_START,1,
1174 "VM %s: unable to engage OIR for slot %u/%u",
1175 argv[0],slot,subslot);
1176 return(-1);
1177 }
1178
1179 vm_release(vm);
1180 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
1181 return(0);
1182 }
1183
1184 /* OIR to stop a slot/subslot */
1185 static int cmd_slot_oir_stop(hypervisor_conn_t *conn,int argc,char *argv[])
1186 {
1187 vm_instance_t *vm;
1188 u_int slot,subslot;
1189
1190 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
1191 return(-1);
1192
1193 slot = atoi(argv[1]);
1194 subslot = atoi(argv[2]);
1195
1196 if (vm_oir_stop(vm,slot,subslot) == -1) {
1197 vm_release(vm);
1198 hypervisor_send_reply(conn,HSC_ERR_STOP,1,
1199 "VM %s: unable to engage OIR for slot %u/%u",
1200 argv[0],slot,subslot);
1201 return(-1);
1202 }
1203
1204 vm_release(vm);
1205 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
1206 return(0);
1207 }
1208
1209 /* Show info about VM object */
1210 static void cmd_show_vm_list(registry_entry_t *entry,void *opt,int *err)
1211 {
1212 hypervisor_conn_t *conn = opt;
1213 vm_instance_t *vm = entry->data;
1214
1215 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s (%s)",
1216 entry->name,vm_get_type(vm));
1217 }
1218
1219 /* VM List */
1220 static int cmd_vm_list(hypervisor_conn_t *conn,int argc,char *argv[])
1221 {
1222 int err = 0;
1223 registry_foreach_type(OBJ_TYPE_VM,cmd_show_vm_list,conn,&err);
1224 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
1225 return(0);
1226 }
1227
1228 /* Show console TCP port info about VM object */
1229 static void cmd_show_vm_list_con_ports(registry_entry_t *entry,void *opt,
1230 int *err)
1231 {
1232 hypervisor_conn_t *conn = opt;
1233 vm_instance_t *vm = entry->data;
1234
1235 if (vm->vtty_con_type == VTTY_TYPE_TCP)
1236 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s (%d)",
1237 vm->name,vm->vtty_con_tcp_port);
1238 }
1239
1240 /* VM console TCP port list */
1241 static int cmd_vm_list_con_ports(hypervisor_conn_t *conn,int argc,char *argv[])
1242 {
1243 int err = 0;
1244 registry_foreach_type(OBJ_TYPE_VM,cmd_show_vm_list_con_ports,conn,&err);
1245 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
1246 return(0);
1247 }
1248
1249 /* VM commands */
1250 static hypervisor_cmd_t vm_cmd_array[] = {
1251 { "create", 3, 3, cmd_create, NULL },
1252 { "rename", 2, 2, cmd_rename, NULL },
1253 { "delete", 1, 1, cmd_delete, NULL },
1254 { "clean_delete", 1, 1, cmd_clean_delete, NULL, },
1255 { "start", 1, 1, cmd_start, NULL },
1256 { "stop", 1, 1, cmd_stop, NULL },
1257 { "get_status", 1, 1, cmd_get_status, NULL },
1258 { "set_debug_level", 2, 2, cmd_set_debug_level, NULL },
1259 { "set_ios", 2, 2, cmd_set_ios, NULL },
1260 { "set_config", 2, 3, cmd_set_config, NULL },
1261 { "set_ram", 2, 2, cmd_set_ram, NULL },
1262 { "set_nvram", 2, 2, cmd_set_nvram, NULL },
1263 { "set_ram_mmap", 2, 2, cmd_set_ram_mmap, NULL },
1264 { "set_sparse_mem", 2, 2, cmd_set_sparse_mem, NULL },
1265 { "set_clock_divisor", 2, 2, cmd_set_clock_divisor, NULL },
1266 { "set_blk_direct_jump", 2, 2, cmd_set_blk_direct_jump, NULL },
1267 { "set_exec_area", 2, 2, cmd_set_exec_area, NULL },
1268 { "set_disk0", 2, 2, cmd_set_disk0, NULL },
1269 { "set_disk1", 2, 2, cmd_set_disk1, NULL },
1270 { "set_conf_reg", 2, 2, cmd_set_conf_reg, NULL },
1271 { "set_idle_pc", 2, 2, cmd_set_idle_pc, NULL },
1272 { "set_idle_pc_online", 3, 3, cmd_set_idle_pc_online, NULL },
1273 { "get_idle_pc_prop", 2, 2, cmd_get_idle_pc_prop, NULL },
1274 { "show_idle_pc_prop", 2, 2, cmd_show_idle_pc_prop, NULL },
1275 { "set_idle_max", 3, 3, cmd_set_idle_max, NULL },
1276 { "set_idle_sleep_time", 3, 3, cmd_set_idle_sleep_time, NULL },
1277 { "show_timer_drift", 2, 2, cmd_show_timer_drift, NULL },
1278 { "set_ghost_file", 2, 2, cmd_set_ghost_file, NULL },
1279 { "set_ghost_status", 2, 2, cmd_set_ghost_status, NULL },
1280 { "set_con_tcp_port", 2, 2, cmd_set_con_tcp_port, NULL },
1281 { "set_aux_tcp_port", 2, 2, cmd_set_aux_tcp_port, NULL },
1282 { "extract_config", 1, 1, cmd_extract_config, NULL },
1283 { "push_config", 2, 3, cmd_push_config, NULL },
1284 { "cpu_info", 2, 2, cmd_show_cpu_info, NULL },
1285 { "cpu_usage", 2, 2, cmd_show_cpu_usage, NULL },
1286 { "suspend", 1, 1, cmd_suspend, NULL },
1287 { "resume", 1, 1, cmd_resume, NULL },
1288 { "send_con_msg", 2, 3, cmd_send_con_msg, NULL },
1289 { "send_aux_msg", 2, 3, cmd_send_aux_msg, NULL },
1290 { "slot_bindings", 1, 1, cmd_slot_bindings, NULL },
1291 { "slot_nio_bindings", 2, 2, cmd_slot_nio_bindings, NULL },
1292 { "slot_add_binding", 4, 4, cmd_slot_add_binding, NULL },
1293 { "slot_remove_binding", 3, 3, cmd_slot_remove_binding, NULL },
1294 { "slot_add_nio_binding", 4, 4, cmd_slot_add_nio_binding, NULL },
1295 { "slot_remove_nio_binding", 3, 3, cmd_slot_remove_nio_binding, NULL },
1296 { "slot_enable_nio", 3, 3, cmd_slot_enable_nio, NULL },
1297 { "slot_disable_nio", 3, 3, cmd_slot_disable_nio, NULL },
1298 { "slot_oir_start", 3, 3, cmd_slot_oir_start, NULL },
1299 { "slot_oir_stop", 3, 3, cmd_slot_oir_stop, NULL },
1300 { "list", 0, 0, cmd_vm_list, NULL },
1301 { "list_con_ports", 0, 0, cmd_vm_list_con_ports, NULL },
1302 { NULL, -1, -1, NULL, NULL },
1303 };
1304
1305 /* Hypervisor VM initialization */
1306 int hypervisor_vm_init(void)
1307 {
1308 hypervisor_module_t *module;
1309
1310 module = hypervisor_register_module("vm",NULL);
1311 assert(module != NULL);
1312
1313 hypervisor_register_cmd_array(module,vm_cmd_array);
1314 return(0);
1315 }