-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvm.py
1292 lines (1110 loc) · 42.1 KB
/
vm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from copy import deepcopy
from dataclasses import dataclass
from typing import Any, Optional, List
from pathlib import Path
import shlex
from invoke import task
from config import BUILD_DIR, PROJECT_ROOT, LINUX_DIR, SSH_PORT
from qemu import spawn_qemu, QemuVm
@dataclass
class NodeInfo:
cpus: str
mem: int
dist: [int]
@dataclass
class VMResource:
cpu: int
memory: int # GB
pin_base: int
numa_node: [int] = None
vnuma: Optional[NodeInfo] = None
@dataclass
class VMConfig:
qemu: Path
image: Path
ovmf: Path
kernel: Optional[Path]
initrd: Optional[Path]
cmdline: Optional[str]
VMRESOURCES = {}
# AMD servers
VMRESOURCES["vislor"] = {}
VMRESOURCES["vislor"]["small"] = VMResource(cpu=1, memory=8, numa_node=[0], pin_base=8)
VMRESOURCES["vislor"]["medium"] = VMResource(
cpu=8, memory=64, numa_node=[0], pin_base=8
)
VMRESOURCES["vislor"]["large"] = VMResource(
cpu=32, memory=256, numa_node=[0], pin_base=0
)
VMRESOURCES["vislor"]["numa"] = VMResource(
cpu=64, memory=512, numa_node=[0, 1], pin_base=0
)
VMRESOURCES["vislor"]["boot-mem8"] = VMResource(
cpu=8, memory=8, numa_node=[0], pin_base=8
)
VMRESOURCES["vislor"]["boot-mem16"] = VMResource(
cpu=8, memory=16, numa_node=[0], pin_base=8
)
VMRESOURCES["vislor"]["boot-mem32"] = VMResource(
cpu=8, memory=32, numa_node=[0], pin_base=8
)
VMRESOURCES["vislor"]["boot-mem64"] = VMResource(
cpu=8, memory=64, numa_node=[0], pin_base=8
)
VMRESOURCES["vislor"]["boot-mem128"] = VMResource(
cpu=8, memory=128, numa_node=[0], pin_base=8
)
VMRESOURCES["vislor"]["boot-mem256"] = VMResource(
cpu=8, memory=256, numa_node=[0], pin_base=8
)
VMRESOURCES["vislor"]["boot-cpu1"] = VMResource(
cpu=1, memory=8, numa_node=[0], pin_base=8
)
VMRESOURCES["vislor"]["boot-cpu8"] = VMResource(
cpu=8, memory=8, numa_node=[0], pin_base=8
)
VMRESOURCES["vislor"]["boot-cpu16"] = VMResource(
cpu=16, memory=8, numa_node=[0], pin_base=8
)
VMRESOURCES["vislor"]["boot-cpu28"] = VMResource(
cpu=28, memory=8, numa_node=[0], pin_base=0
)
VMRESOURCES["vislor"]["boot-cpu32"] = VMResource(
cpu=32, memory=8, numa_node=[0], pin_base=0
)
VMRESOURCES["vislor"]["boot-cpu56"] = VMResource(
cpu=56, memory=8, numa_node=[0, 1], pin_base=0
)
VMRESOURCES["vislor"]["boot-cpu64"] = VMResource(
cpu=64, memory=8, numa_node=[0, 1], pin_base=0
)
VMRESOURCES["irene"] = deepcopy(VMRESOURCES["vislor"])
VMRESOURCES["irene"]["large"] = VMResource(
cpu=32, memory=256, numa_node=[0], pin_base=8
)
VMRESOURCES["irene"]["xlarge"] = VMResource(
cpu=64, memory=512, numa_node=[0], pin_base=8
)
del VMRESOURCES["irene"]["numa"]
# Intel servers
VMRESOURCES["ian"] = {}
VMRESOURCES["ian"]["small"] = VMResource(cpu=1, memory=8, numa_node=[0], pin_base=8)
VMRESOURCES["ian"]["medium"] = VMResource(cpu=8, memory=64, numa_node=[0], pin_base=8)
VMRESOURCES["ian"]["large"] = VMResource(cpu=32, memory=128, numa_node=[0], pin_base=0)
VMRESOURCES["ian"]["numa"] = VMResource(
cpu=64, memory=256, numa_node=[0, 1], pin_base=0
)
# configuration when SNC (Sub Numa Clustering) enabled
# VMRESOURCES["sdp"]["large"] = VMResource(
# cpu=28, memory=128, numa_node=[1], pin_base=28
# )
# VMRESOURCES["sdp"]["numa"] = VMResource(
# cpu=56, memory=256, numa_node=[0, 1], pin_base=0
# )
# VMRESOURCES["sdp"]["vnuma"] = VMResource(
# cpu=56,
# memory=256,
# numa_node=[0, 1],
# pin_base=0,
# vnuma=[
# NodeInfo(cpus="0-27", mem=128, dist=[12]),
# NodeInfo(cpus="28-55", mem=128, dist=[]),
# ],
# )
VMRESOURCES["ian"]["boot-mem8"] = VMResource(cpu=8, memory=8, numa_node=[0], pin_base=8)
VMRESOURCES["ian"]["boot-mem16"] = VMResource(
cpu=8, memory=16, numa_node=[0], pin_base=8
)
VMRESOURCES["ian"]["boot-mem32"] = VMResource(
cpu=8, memory=32, numa_node=[0], pin_base=8
)
VMRESOURCES["ian"]["boot-mem64"] = VMResource(
cpu=8, memory=64, numa_node=[0], pin_base=8
)
VMRESOURCES["ian"]["boot-mem128"] = VMResource(
cpu=8, memory=128, numa_node=[0], pin_base=8
)
VMRESOURCES["ian"]["boot-mem256"] = VMResource(
cpu=8, memory=256, numa_node=[0], pin_base=8
)
# VMRESOURCES["ian"]["boot-mem256"] = VMResource(
# cpu=8, memory=256, numa_node=[0, 1], pin_base=8
# )
VMRESOURCES["ian"]["boot-cpu1"] = VMResource(cpu=1, memory=8, numa_node=[0], pin_base=8)
VMRESOURCES["ian"]["boot-cpu8"] = VMResource(cpu=8, memory=8, numa_node=[0], pin_base=8)
VMRESOURCES["ian"]["boot-cpu16"] = VMResource(
cpu=16, memory=8, numa_node=[0], pin_base=0
)
VMRESOURCES["ian"]["boot-cpu32"] = VMResource(
cpu=32, memory=8, numa_node=[0], pin_base=0
)
VMRESOURCES["ian"]["boot-cpu64"] = VMResource(
cpu=64, memory=8, numa_node=[0, 1], pin_base=0
)
VMRESOURCES["sdp"] = deepcopy(VMRESOURCES["ian"])
VMRESOURCES["sdp"]["large"] = VMResource(cpu=28, memory=128, numa_node=[0], pin_base=28)
VMRESOURCES["sdp"]["xlarge"] = VMResource(cpu=56, memory=256, numa_node=[0], pin_base=0)
VMRESOURCES["sdp"]["numa"] = VMResource(
cpu=112, memory=512, numa_node=[0, 1], pin_base=0
)
VMRESOURCES["sdp"]["boot-cpu28"] = VMResource(
cpu=28, memory=8, numa_node=[0], pin_base=0
)
VMRESOURCES["sdp"]["boot-cpu56"] = VMResource(
cpu=56, memory=8, numa_node=[0, 1], pin_base=0
)
def get_vm_resource(hostname: str, name: str) -> VMResource:
return VMRESOURCES[hostname][name]
def get_vm_config(name: str) -> VMConfig:
if name == "amd":
# use kernel same for the "snp"
return VMConfig(
qemu=BUILD_DIR / "qemu-amd-sev-snp/bin/qemu-system-x86_64",
image=BUILD_DIR / "image/snp-guest-image.qcow2",
ovmf=BUILD_DIR / "ovmf-amd-sev-snp-fd/FV/OVMF.fd",
kernel=None,
initrd=None,
cmdline=None,
)
if name == "amd-normal":
return VMConfig(
qemu=BUILD_DIR / "qemu-amd-sev-snp/bin/qemu-system-x86_64",
image=BUILD_DIR / "image/normal-guest-image.qcow2",
ovmf=BUILD_DIR / "ovmf-amd-sev-snp-fd/FV/OVMF.fd",
kernel=None,
initrd=None,
cmdline=None,
)
if name == "amd-direct":
return VMConfig(
qemu=BUILD_DIR / "qemu-amd-sev-snp/bin/qemu-system-x86_64",
image=BUILD_DIR / "image/guest-fs.qcow2",
ovmf=BUILD_DIR / "ovmf-amd-sev-snp-fd/FV/OVMF.fd",
kernel=LINUX_DIR / "arch/x86/boot/bzImage",
initrd=None,
cmdline="root=/dev/vda console=hvc0",
)
if name == "snp":
return VMConfig(
qemu=BUILD_DIR / "qemu-amd-sev-snp/bin/qemu-system-x86_64",
image=BUILD_DIR / "image/snp-guest-image.qcow2",
ovmf=BUILD_DIR / "ovmf-amd-sev-snp-fd/FV/OVMF.fd",
kernel=None,
initrd=None,
cmdline=None,
)
if name == "snp-direct":
return VMConfig(
qemu=BUILD_DIR / "qemu-amd-sev-snp/bin/qemu-system-x86_64",
image=BUILD_DIR / "image/guest-fs.qcow2",
ovmf=BUILD_DIR / "ovmf-amd-sev-snp-fd/FV/OVMF.fd",
kernel=LINUX_DIR / "arch/x86/boot/bzImage",
initrd=None,
cmdline="root=/dev/vda console=hvc0",
)
if name == "intel":
# use kernel same for the "tdx"
return VMConfig(
# qemu="/usr/bin/qemu-system-x86_64",
# ovmf="/usr/share/ovmf/OVMF.fd",
qemu=BUILD_DIR / "qemu-tdx/bin/qemu-system-x86_64",
image=BUILD_DIR / "image/tdx-guest-image.qcow2",
ovmf=BUILD_DIR / "ovmf-tdx-fd/FV/OVMF.fd",
kernel=None,
initrd=None,
cmdline=None,
)
if name == "intel-normal":
return VMConfig(
qemu="/usr/bin/qemu-system-x86_64",
image=BUILD_DIR / "image/normal-guest-image.qcow2",
ovmf="/usr/share/ovmf/OVMF.fd",
kernel=None,
initrd=None,
cmdline=None,
)
if name == "intel-direct":
return VMConfig(
# qemu="/usr/bin/qemu-system-x86_64",
qemu=BUILD_DIR / "qemu-tdx/bin/qemu-system-x86_64",
image=BUILD_DIR / "image/guest-fs.qcow2",
# ovmf="/usr/share/ovmf/OVMF.fd",
ovmf=BUILD_DIR / "ovmf-tdx-fd/FV/OVMF.fd",
kernel=LINUX_DIR / "arch/x86/boot/bzImage",
initrd=None,
cmdline="root=/dev/vda console=hvc0",
)
if name == "intel-ubuntu":
return VMConfig(
qemu="/usr/bin/qemu-system-x86_64",
image=BUILD_DIR / "image/tdx-guest-ubuntu-24.04-generic.qcow2",
ovmf="/usr/share/ovmf/OVMF.fd",
kernel=None,
initrd=None,
cmdline=None,
)
if name == "tdx":
return VMConfig(
# qemu="/usr/bin/qemu-system-x86_64",
# ovmf="/usr/share/ovmf/OVMF.fd",
qemu=BUILD_DIR / "qemu-tdx/bin/qemu-system-x86_64",
image=BUILD_DIR / "image/tdx-guest-image.qcow2",
ovmf=BUILD_DIR / "ovmf-tdx-fd/FV/OVMF.fd",
kernel=None,
initrd=None,
cmdline=None,
)
if name == "tdx-direct":
return VMConfig(
# qemu="/usr/bin/qemu-system-x86_64",
qemu=BUILD_DIR / "qemu-tdx/bin/qemu-system-x86_64",
image=BUILD_DIR / "image/guest-fs.qcow2",
# ovmf="/usr/share/ovmf/OVMF.fd",
ovmf=BUILD_DIR / "ovmf-tdx-fd/FV/OVMF.fd",
kernel=LINUX_DIR / "arch/x86/boot/bzImage",
initrd=None,
cmdline="root=/dev/vda console=hvc0",
)
if name == "tdx-ubuntu":
return VMConfig(
qemu="/usr/bin/qemu-system-x86_64",
image=BUILD_DIR / "image/tdx-guest-ubuntu-24.04-generic.qcow2",
ovmf="/usr/share/ovmf/OVMF.fd",
kernel=None,
initrd=None,
cmdline=None,
)
raise ValueError(f"Unknown VM image: {name}")
def get_amd_vm_qemu_cmd(resource: VMResource, config: dict) -> List[str]:
vmconfig: VMConfig = get_vm_config("amd")
ssh_port = config["ssh_port"]
qemu_cmd = f"""
{vmconfig.qemu}
-enable-kvm
-cpu host
-smp {resource.cpu}
-m {resource.memory}G
-machine q35
-blockdev qcow2,node-name=q2,file.driver=file,file.filename={vmconfig.image}
-device virtio-blk-pci,drive=q2,bootindex=0
-device virtio-net-pci,netdev=net0
-netdev user,id=net0,hostfwd=tcp::{ssh_port}-:22
-virtfs local,path={PROJECT_ROOT},security_model=none,mount_tag=share
-drive if=pflash,format=raw,unit=0,file={vmconfig.ovmf},readonly=on
-nographic
"""
return shlex.split(qemu_cmd)
def get_amd_vm_direct_qemu_cmd(resource: VMResource, config: dict) -> List[str]:
vmconfig: VMConfig = get_vm_config("amd-direct")
ssh_port = config.get("ssh_port", SSH_PORT)
extra_cmdline = config.get("extra_cmdline", "")
qemu_cmd = f"""
{vmconfig.qemu}
-cpu host
-enable-kvm
-smp {resource.cpu}
-m {resource.memory}G
-machine q35
-kernel {vmconfig.kernel}
-append "{vmconfig.cmdline} {extra_cmdline}"
-blockdev qcow2,node-name=q2,file.driver=file,file.filename={vmconfig.image}
-device virtio-blk-pci,drive=q2,
-device virtio-net-pci,netdev=net0
-netdev user,id=net0,hostfwd=tcp::{ssh_port}-:22
-virtfs local,path={PROJECT_ROOT},security_model=none,mount_tag=share
-drive if=pflash,format=raw,unit=0,file={vmconfig.ovmf},readonly=on
-nographic
-serial null
-device virtio-serial
-chardev stdio,mux=on,id=char0,signal=off
-mon chardev=char0,mode=readline
-device virtconsole,chardev=char0,id=vc0,nr=0
"""
return shlex.split(qemu_cmd)
def get_snp_qemu_cmd(resource: VMResource, config: dict) -> List[str]:
vmconfig: VMConfig = get_vm_config("snp")
ssh_port = config["ssh_port"]
if config["boot_prealloc"]:
prealloc = "on"
else:
prealloc = "off"
qemu_cmd = f"""
{vmconfig.qemu}
-enable-kvm
-cpu EPYC-v4,host-phys-bits=true
-smp {resource.cpu}
-m {resource.memory}G
-machine q35,memory-backend=ram1,memory-encryption=sev0,vmport=off
-object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,policy=0x30000
-object memory-backend-memfd,id=ram1,size={resource.memory}G,share=true,prealloc={prealloc}
-blockdev qcow2,node-name=q2,file.driver=file,file.filename={vmconfig.image}
-device virtio-blk-pci,drive=q2,bootindex=0
-device virtio-net-pci,netdev=net0
-netdev user,id=net0,hostfwd=tcp::{ssh_port}-:22
-virtfs local,path={PROJECT_ROOT},security_model=none,mount_tag=share
-bios {vmconfig.ovmf}
-nographic
"""
return shlex.split(qemu_cmd)
def get_snp_direct_qemu_cmd(resource: VMResource, config: dict) -> List[str]:
vmconfig: VMConfig = get_vm_config("amd-direct")
ssh_port = config.get("ssh_port", SSH_PORT)
extra_cmdline = config.get("extra_cmdline", "")
if config["boot_prealloc"]:
prealloc = "on"
else:
prealloc = "off"
qemu_cmd = f"""
{vmconfig.qemu}
-enable-kvm
-cpu EPYC-v4,host-phys-bits=true,+avx512f,+avx512dq,+avx512cd,+avx512bw,+avx512vl,+avx512ifma,+avx512vbmi,+avx512vbmi2,+avx512vnni,+avx512bitalg
-smp {resource.cpu}
-m {resource.memory}G
-machine q35,memory-backend=ram1,memory-encryption=sev0,vmport=off
-object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,policy=0x30000
-object memory-backend-memfd,id=ram1,size={resource.memory}G,share=true,prealloc={prealloc}
-kernel {vmconfig.kernel}
-append "{vmconfig.cmdline} {extra_cmdline}"
-blockdev qcow2,node-name=q2,file.driver=file,file.filename={vmconfig.image}
-device virtio-blk-pci,drive=q2,
-device virtio-net-pci,netdev=net0
-netdev user,id=net0,hostfwd=tcp::{ssh_port}-:22
-virtfs local,path={PROJECT_ROOT},security_model=none,mount_tag=share
-bios {vmconfig.ovmf}
-nographic
-serial null
-device virtio-serial
-chardev stdio,mux=on,id=char0,signal=off
-mon chardev=char0,mode=readline
-device virtconsole,chardev=char0,id=vc0,nr=0
"""
return shlex.split(qemu_cmd)
def get_intel_qemu_cmd(type: str, resource: VMResource, config: dict) -> List[str]:
vmconfig: VMConfig = get_vm_config(type)
ssh_port = config["ssh_port"]
qemu_cmd = f"""
{vmconfig.qemu}
-enable-kvm
-cpu host,pmu=off
-smp {resource.cpu}
-m {resource.memory}G
-machine q35,kernel_irqchip=split,hpet=off
-bios {vmconfig.ovmf}
-nographic
-nodefaults
-serial stdio
-blockdev qcow2,node-name=q2,file.driver=file,file.filename={vmconfig.image}
-device virtio-blk-pci,drive=q2,bootindex=0
-device virtio-net-pci,netdev=net0
-netdev user,id=net0,hostfwd=tcp::{ssh_port}-:22
-virtfs local,path={PROJECT_ROOT},security_model=none,mount_tag=share
"""
return shlex.split(qemu_cmd)
def get_intel_direct_qemu_cmd(resource: VMResource, config: dict) -> List[str]:
vmconfig: VMConfig = get_vm_config("intel-direct")
ssh_port = config["ssh_port"]
extra_cmdline = config.get("extra_cmdline", "")
if resource.vnuma is not None:
# FIXME: the current vnuma config is static
numa_config = f"""
-object memory-backend-ram,size=128G,prealloc=yes,host-nodes=0,policy=bind,id=node0
-numa node,nodeid=0,cpus=0-27,memdev=node0
-object memory-backend-ram,size=128G,prealloc=yes,host-nodes=1,policy=bind,id=node1
-numa node,nodeid=1,cpus=28-55,memdev=node1
-numa dist,src=0,dst=1,val=12
"""
else:
numa_config = ""
qemu_cmd = f"""
{vmconfig.qemu}
-enable-kvm
-cpu host,pmu=off
-smp {resource.cpu}
-m {resource.memory}G
-machine q35,kernel_irqchip=split,hpet=off
{numa_config}
-kernel {vmconfig.kernel}
-append "{vmconfig.cmdline} {extra_cmdline}"
-bios {vmconfig.ovmf}
-nographic
-nodefaults
-blockdev qcow2,node-name=q2,file.driver=file,file.filename={vmconfig.image}
-device virtio-blk-pci,drive=q2
-device virtio-net-pci,netdev=net0
-netdev user,id=net0,hostfwd=tcp::{ssh_port}-:22
-virtfs local,path={PROJECT_ROOT},security_model=none,mount_tag=share
-serial null
-device virtio-serial
-chardev stdio,mux=on,id=char0,signal=off
-mon chardev=char0,mode=readline
-device virtconsole,chardev=char0,id=vc0,nr=0
"""
return shlex.split(qemu_cmd)
def get_tdx_qemu_cmd(type, resource: VMResource, config: dict) -> List[str]:
vmconfig: VMConfig = get_vm_config(type)
ssh_port = config["ssh_port"]
guest_cid = config["guest_cid"]
if config["boot_prealloc"]:
prealloc = "on"
else:
prealloc = "off"
qemu_cmd = f"""
{vmconfig.qemu}
-enable-kvm
-cpu host,pmu=off
-smp {resource.cpu}
-m {resource.memory}G
-machine q35,hpet=off,kernel_irqchip=split,confidential-guest-support=tdx,memory-backend=ram1
-object tdx-guest,id=tdx
-object memory-backend-ram,id=ram1,size={resource.memory}G,prealloc={prealloc}
-bios {vmconfig.ovmf}
-nographic
-nodefaults
-serial stdio
-blockdev qcow2,node-name=q2,file.driver=file,file.filename={vmconfig.image}
-device virtio-blk-pci,drive=q2,bootindex=0
-device virtio-net-pci,netdev=net0
-netdev user,id=net0,hostfwd=tcp::{ssh_port}-:22
-virtfs local,path={PROJECT_ROOT},security_model=none,mount_tag=share
-device vhost-vsock-pci,guest-cid={guest_cid}
"""
return shlex.split(qemu_cmd)
def get_tdx_direct_qemu_cmd(resource: VMResource, config: dict) -> List[str]:
vmconfig: VMConfig = get_vm_config("tdx-direct")
ssh_port = config["ssh_port"]
guest_cid = config["guest_cid"]
extra_cmdline = config.get("extra_cmdline", "")
if config["boot_prealloc"]:
prealloc = "on"
else:
prealloc = "off"
if resource.vnuma is not None:
memory = f"""
-object memory-backend-ram,size={resource.memory//2}G,prealloc={prealloc},host-nodes=0,policy=bind,id=node0
-numa node,nodeid=0,cpus=0-27,memdev=node0
-object memory-backend-ram,size={resource.memory//2}G,prealloc={prealloc},host-nodes=1,policy=bind,id=node1
-numa node,nodeid=1,cpus=28-55,memdev=node1
-numa dist,src=0,dst=1,val=12
"""
else:
memory = f"-object memory-backend-ram,id=node0,size={resource.memory}G,prealloc={prealloc}"
qemu_cmd = f"""
{vmconfig.qemu}
-enable-kvm
-cpu host,pmu=off
-smp {resource.cpu}
-m {resource.memory}G
-machine q35,hpet=off,kernel_irqchip=split,confidential-guest-support=tdx
-object tdx-guest,id=tdx
{memory}
-kernel {vmconfig.kernel}
-append "{vmconfig.cmdline} {extra_cmdline}"
-bios {vmconfig.ovmf}
-nographic
-nodefaults
-blockdev qcow2,node-name=q2,file.driver=file,file.filename={vmconfig.image}
-device virtio-blk-pci,drive=q2
-device virtio-net-pci,netdev=net0
-netdev user,id=net0,hostfwd=tcp::{ssh_port}-:22
-virtfs local,path={PROJECT_ROOT},security_model=none,mount_tag=share
-serial null
-device virtio-serial
-chardev stdio,mux=on,id=char0,signal=off
-mon chardev=char0,mode=readline
-device virtconsole,chardev=char0,id=vc0,nr=0
-device vhost-vsock-pci,guest-cid={guest_cid}
"""
return shlex.split(qemu_cmd)
def qemu_option_virtio_blk(
file: Path, # file or block device to be used as a backend of virtio-blk
aio: str = "native", # either of threads, native (POSIX AIO), io_uring
direct: bool = True, # if True, QEMU uses O_DIRECT to open the file
iothread: bool = True, # if True, use QEMU iothread
iommu_option: bool = False, # if True, enable VIRTIO_F_ACCESS_PLATFORM (VIRTIO_F_IOMMU_PLATFORM) feature bit
# (this is necessary to force bounce buffers in a normal VM for testing)
) -> List[str]:
# QEMU options (https://www.qemu.org/docs/master/system/qemu-manpage.html)
# -drive cache=
#
# | | cache.writeback | cache.direct | cache.no-flush |
# |--------------|-----------------|--------------|----------------|
# | writeback | on | off | off |
# | none | on | on | off |
# | writethrough | off | off | off |
# | directsync | off | on | off |
# | unsafe | on | off | on |
#
# NOTE:
# - cache.writeback=on by default
# - aio=native requires cache.direct=on (open file with O_DIRECT)
# - by default, we use the same configuration as the "cache=none"
#
# - aio=threads vs native: https://bugzilla.redhat.com/show_bug.cgi?id=1545721
# > With aio=native, IO submissions on the host by Qemu are limited to 1
# > cpu, where as io=threads is multi-cpu. io=native provides higher
# > efficiency (less cpu overhead), but cannot scale to the levels io=threads
# > does. However, io=threads can consume more cpu as similar IO levels. If
# > there is ample CPU on the host, then io=threads will scale better.
if file.is_block_device():
driver = "host_device"
else:
driver = "file"
if direct:
cache_direct = "on"
else:
cache_direct = "off"
if iommu_option:
iommu = ",iommu_platform=on,disable-modern=off,disable-legacy=on"
else:
iommu = ""
if iothread:
option = f"""
-blockdev node-name=q1,driver=raw,file.driver={driver},file.filename={file},file.aio={aio},cache.direct={cache_direct},cache.no-flush=off
-device virtio-blk-pci,drive=q1,iothread=iothread0{iommu}
-object iothread,id=iothread0
"""
else:
option = f"""
-blockdev node-name=q1,driver=raw,file.driver={driver},file.filename={file},file.aio={aio},cache.direct={cache_direct},cache.no-flush=off
-device virtio-blk-pci,drive=q1{iommu}
"""
return shlex.split(option)
def qemu_option_virtio_nic(
tap="tap0", mtap="mtap0", vhost=False, mq=False, config={}
) -> List[str]:
"""Qreate a virtio-nic with a tap interface.
If mq is True, then create multiple queues as many as the number of CPUs.
See justfile for the bridge configuration.
"""
resource: VMResource = config["resource"]
iommu_option = config.get("virtio_iommu", False)
num_cpus = resource.cpu
if vhost:
vhost_option = "on"
else:
vhost_option = "off"
if iommu_option:
iommu = ",iommu_platform=on,disable-modern=off,disable-legacy=on"
else:
iommu = ""
if mq:
option = f"""
-netdev tap,id=en0,ifname={mtap},script=no,downscript=no,vhost={vhost_option},queues={num_cpus}
-device virtio-net-pci,netdev=en0,mq=on,vectors=18{iommu}
"""
else:
option = f"""
-netdev tap,id=en0,ifname={tap},script=no,downscript=no,vhost={vhost_option}
-device virtio-net-pci,netdev=en0,mq=off,vectors=18{iommu}
"""
# option = f"""
# -netdev bridge,id=en0,br={bridge}
# -device virtio-net-pci,netdev=en0
# """
return shlex.split(option)
def start_and_attach(qemu_cmd: List[str], pin: bool, **kargs: Any) -> None:
"""Start a VM and attach to the console (tmux session) to interact with the VM.
Note 1: The VM automatically terminates when the tmux session is closed.
Note 2: Ctrl-C goes to the tmux session, not the VM, killing the entier session with the VM.
"""
resource: VMResource = kargs["config"]["resource"]
pin_base: int = kargs["config"].get("pin_base", resource.pin_base)
vm: QemuVM
with spawn_qemu(qemu_cmd, numa_node=resource.numa_node) as vm:
if pin:
vm.pin_vcpu(pin_base)
vm.attach()
vm.shutdown()
def ipython(qemu_cmd: List[str], pin: bool, **kargs: Any) -> None:
"""Start a VM and then start an ipython shell
Example usage:
```
# Check QEMU's PID
In [1]: vm.pid
Out[1]: 823611
# Send a command to the VM
In [2]: vm.ssh_cmd(["echo", "ok"])
$ ssh -i /scratch/masa/CVM_eval/nix/ssh_key -p 2225 -oBatchMode=yes -oStrictHostKeyChecking=no -oConnectTimeout=5 -oUserKnownHostsFile=/dev/null root@localhost -- echo ok
Warning: Permanently added '[localhost]:2225' (ED25519) to the list of known hosts.
Out[2]: CompletedProcess(args=['ssh', '-i', '/scratch/masa/CVM_eval/nix/ssh_key', '-p', '2225', '-oBatchMode=yes', '-oStrictHostKeyChecking=no', '-oConnectTimeout=5', '-oUserKnownHostsFile=/dev/null', 'root@localhost', '--', 'echo ok'], returncode=0, stdout='ok\n')
```
Note that the VM automatically terminates when the ipython session is closed.
"""
resource: VMResource = kargs["config"]["resource"]
pin_base: int = kargs["config"].get("pin_base", resource.pin_base)
vm: QemuVM
with spawn_qemu(
qemu_cmd, numa_node=resource.numa_node, config=kargs["config"]
) as vm:
if pin:
vm.pin_vcpu(pin_base)
from IPython import embed
embed()
def ssh_cmd(qemu_cmd: List[str], pin: bool, **kargs: Any) -> None:
"""Start a VM and then send cmd via ssh
Example:
# we can have multiple ssh commands
inv vm.start --type intel --ssh-cmd "echo hi" --ssh-cmd "ls /" --action ssh-cmd
"""
resource: VMResource = kargs["config"]["resource"]
pin_base: int = kargs["config"].get("pin_base", resource.pin_base)
cmds: [str] = kargs["config"]["ssh_cmd"]
vm: QemuVM
with spawn_qemu(
qemu_cmd, numa_node=resource.numa_node, config=kargs["config"]
) as vm:
if pin:
vm.pin_vcpu(pin_base)
vm.wait_for_ssh()
for cmd in cmds:
cmd_ = shlex.split(cmd)
vm.ssh_cmd(cmd_)
vm.shutdown()
def boottime(name: str, qemu_cmd: List[str], pin: bool, **kargs: Any) -> None:
"""Measure the boot time of a VM"""
import boottime
type: str = kargs["config"]["type"]
kargs["config"]["vmconfig"] = get_vm_config(f"{type}-direct")
boottime.run_boot_test(name, qemu_cmd, pin, **kargs)
def prepare(name: str, qemu_cmd: List[str], pin: bool, **kargs: Any) -> None:
prepare_phoronix(name, qemu_cmd, pin, **kargs)
prepare_app(name, qemu_cmd, pin, **kargs)
def prepare_phoronix(name: str, qemu_cmd: List[str], pin: bool, **kargs: Any) -> None:
resource: VMResource = kargs["config"]["resource"]
pin_base: int = kargs["config"].get("pin_base", resource.pin_base)
vm: QemuVM
with spawn_qemu(
qemu_cmd, numa_node=resource.numa_node, config=kargs["config"]
) as vm:
if pin:
vm.pin_vcpu(pin_base)
vm.wait_for_ssh()
from phoronix import install_bench
install_bench("pts/memory", vm)
install_bench("pts/npb", vm)
vm.shutdown()
def prepare_app(name: str, qemu_cmd: List[str], pin: bool, **kargs: Any) -> None:
resource: VMResource = kargs["config"]["resource"]
pin_base: int = kargs["config"].get("pin_base", resource.pin_base)
vm: QemuVM
with spawn_qemu(
qemu_cmd, numa_node=resource.numa_node, config=kargs["config"]
) as vm:
if pin:
vm.pin_vcpu(pin_base)
vm.wait_for_ssh()
from application import prepare
prepare(vm)
vm.shutdown()
def run_phoronix(name: str, qemu_cmd: List[str], pin: bool, **kargs: Any) -> None:
bench_name = kargs["config"]["phoronix_bench_name"]
if not bench_name:
print(
"Please specify the benchmark name using --phoronix-bench-name (e.g., --phoronix-bench-name memory)"
)
return
resource: VMResource = kargs["config"]["resource"]
pin_base: int = kargs["config"].get("pin_base", resource.pin_base)
vm: QemuVM
with spawn_qemu(
qemu_cmd, numa_node=resource.numa_node, config=kargs["config"]
) as vm:
if pin:
vm.pin_vcpu(pin_base)
vm.wait_for_ssh()
import phoronix
phoronix.run_phoronix(name, f"{bench_name}", f"pts/{bench_name}", vm)
vm.shutdown()
def run_mlc(name: str, qemu_cmd: List[str], pin: bool, **kargs: Any) -> None:
resource: VMResource = kargs["config"]["resource"]
pin_base: int = kargs["config"].get("pin_base", resource.pin_base)
vm: QemuVM
with spawn_qemu(
qemu_cmd, numa_node=resource.numa_node, config=kargs["config"]
) as vm:
if pin:
vm.pin_vcpu(pin_base)
vm.wait_for_ssh()
import memory
memory.run_mlc(name, vm)
vm.shutdown()
def run_blender(name: str, qemu_cmd: List[str], pin: bool, **kargs: Any) -> None:
repeat: int = kargs["config"].get("repeat", 1)
resource: VMResource = kargs["config"]["resource"]
pin_base: int = kargs["config"].get("pin_base", resource.pin_base)
vm: QemuVM
with spawn_qemu(
qemu_cmd, numa_node=resource.numa_node, config=kargs["config"]
) as vm:
if pin:
vm.pin_vcpu(pin_base)
vm.wait_for_ssh()
from application import run_blender
run_blender(name, vm, repeat=repeat)
vm.shutdown()
def run_iperf(
name: str, qemu_cmd: List[str], pin: bool, udp: bool = False, **kargs: Any
):
resource: VMResource = kargs["config"]["resource"]
pin_base: int = kargs["config"].get("pin_base", resource.pin_base)
vm: QemuVM
with spawn_qemu(
qemu_cmd, numa_node=resource.numa_node, config=kargs["config"]
) as vm:
if pin:
vm.pin_vcpu(pin_base)
vm.wait_for_ssh()
from network import run_iperf
if kargs["config"]["virtio_nic_vhost"]:
name += f"-vhost"
if kargs["config"]["virtio_nic_mq"]:
name += f"-mq"
if (
kargs["config"]["virtio_iommu"]
and "swiotlb" in kargs["config"]["extra_cmdline"]
):
name += f"-swiotlb"
run_iperf(name, vm, udp=udp)
vm.shutdown()
def run_memtier(
name: str, qemu_cmd: List[str], pin: bool, server: str = "redis", **kargs: Any
):
tls: bool = kargs["config"].get("tls", False)
resource: VMResource = kargs["config"]["resource"]
pin_base: int = kargs["config"].get("pin_base", resource.pin_base)
vm: QemuVM
with spawn_qemu(
qemu_cmd, numa_node=resource.numa_node, config=kargs["config"]
) as vm:
if pin:
vm.pin_vcpu(pin_base)
vm.wait_for_ssh()
from network import run_memtier
if kargs["config"]["virtio_nic_vhost"]:
name += f"-vhost"
if kargs["config"]["virtio_nic_mq"]:
name += f"-mq"
if (
kargs["config"]["virtio_iommu"]
and "swiotlb" in kargs["config"]["extra_cmdline"]
):
name += f"-swiotlb"
run_memtier(name, vm, server=server, tls=tls)
vm.shutdown()
def run_nginx(name: str, qemu_cmd: List[str], pin: bool, **kargs: Any):
resource: VMResource = kargs["config"]["resource"]
pin_base: int = kargs["config"].get("pin_base", resource.pin_base)
vm: QemuVM
with spawn_qemu(
qemu_cmd, numa_node=resource.numa_node, config=kargs["config"]
) as vm:
if pin:
vm.pin_vcpu(pin_base)
vm.wait_for_ssh()
from network import run_nginx
if kargs["config"]["virtio_nic_vhost"]:
name += f"-vhost"
if kargs["config"]["virtio_nic_mq"]:
name += f"-mq"
if (
kargs["config"]["virtio_iommu"]
and "swiotlb" in kargs["config"]["extra_cmdline"]
):
name += f"-swiotlb"
run_nginx(name, vm)
vm.shutdown()
def run_ping(name: str, qemu_cmd: List[str], pin: bool, **kargs: Any):
resource: VMResource = kargs["config"]["resource"]
pin_base: int = kargs["config"].get("pin_base", resource.pin_base)
vm: QemuVM
with spawn_qemu(
qemu_cmd, numa_node=resource.numa_node, config=kargs["config"]
) as vm:
if pin:
vm.pin_vcpu(pin_base)
vm.wait_for_ssh()
from network import run_ping
if kargs["config"]["virtio_nic_vhost"]:
name += f"-vhost"
if kargs["config"]["virtio_nic_mq"]:
name += f"-mq"
if (
kargs["config"]["virtio_iommu"]
and "swiotlb" in kargs["config"]["extra_cmdline"]
):
name += f"-swiotlb"
run_ping(name, vm)
vm.shutdown()
def run_tensorflow(name: str, qemu_cmd: List[str], pin: bool, **kargs: Any) -> None:
repeat: int = kargs["config"].get("repeat", 1)
resource: VMResource = kargs["config"]["resource"]
pin_base: int = kargs["config"].get("pin_base", resource.pin_base)
vm: QemuVM
with spawn_qemu(
qemu_cmd, numa_node=resource.numa_node, config=kargs["config"]
) as vm:
if pin:
vm.pin_vcpu(pin_base)
vm.wait_for_ssh()