From 1ffd1d9934998e791c90b9ab67b24a7d46cbec53 Mon Sep 17 00:00:00 2001 From: Hongyu Ning Date: Mon, 23 Oct 2023 16:49:13 +0800 Subject: [PATCH] [guest-test] Test Enhance: tab format unified across all source code all tab format unified to 2 spaces across all source code [Test Components] tdx [Test Types] func [Supported Devices] spr,emr,gnr.srf Signed-off-by: Hongyu Ning --- guest-test/qemu.config.json | 88 +++++++++--------- guest-test/qemu_get_config.py | 142 ++++++++++++++--------------- guest-test/qemu_runner.py | 12 +-- guest-test/tdx/tdx_attest_check.sh | 96 +++++++++---------- 4 files changed, 169 insertions(+), 169 deletions(-) diff --git a/guest-test/qemu.config.json b/guest-test/qemu.config.json index 565bcfb..c8fd44d 100644 --- a/guest-test/qemu.config.json +++ b/guest-test/qemu.config.json @@ -1,49 +1,49 @@ { - "common": { - "kernel_img": "/boot/vmlinuz-xxx-yyy", - "initrd_img": "/boot/initramfs-xxx-yyy", - "bios_img": "/path/to/EDKII/OVMF.fd or other virtual BIOS", - "qemu_img": "/path/to/qemu-kvm with proper capabilty of VM test", - "guest_img": "/path/to/prepared/guest_os_image, in qcow2 or raw image format", - "guest_img_format": "raw", - "boot_pattern": "*Kernel*on*an*x86_64*", - "guest_root_passwd": "123456", - "vm_type": "tdx", - "pmu": "off", - "cpus": "4", - "sockets": "1", - "mem": "16", - "cmdline": "accept_memory=lazy", - "debug": "on" - }, + "common": { + "kernel_img": "/boot/vmlinuz-xxx-yyy", + "initrd_img": "/boot/initramfs-xxx-yyy", + "bios_img": "/path/to/EDKII/OVMF.fd or other virtual BIOS", + "qemu_img": "/path/to/qemu-kvm with proper capabilty of VM test", + "guest_img": "/path/to/prepared/guest_os_image, in qcow2 or raw image format", + "guest_img_format": "raw", + "boot_pattern": "*Kernel*on*an*x86_64*", + "guest_root_passwd": "123456", + "vm_type": "tdx", + "pmu": "off", + "cpus": "4", + "sockets": "1", + "mem": "16", + "cmdline": "accept_memory=lazy", + "debug": "on" + }, - "vm": { - "cfg_1": "-accel kvm -no-reboot -nographic -vga none -device virtio-net-pci,netdev=mynet0,mac=DE:AD:BE:EF:AB:CD,romfile= ", - "cfg_2": "-chardev stdio,id=mux,mux=on,signal=off -device virtio-serial,romfile= -device virtconsole,chardev=mux ", - "cfg_3": "-serial chardev:mux -monitor chardev:mux -monitor pty -no-hpet -nodefaults ", - "cfg_var_1": "-name process=$VM_TYPEVM_$PORT,debug-threads=on ", - "cfg_var_2": "-cpu host,host-phys-bits,pmu=$PMU ", - "cfg_var_3": "-smp cpus=$VCPU,sockets=$SOCKETS ", - "cfg_var_4": "-m $MEMG ", - "cfg_var_5": "-kernel $KERNEL_IMG ", - "cfg_var_6": "-initrd $INITRD_IMG", - "cfg_var_7": "-netdev user,id=mynet0,hostfwd=tcp::$PORT-:22 ", - "cfg_var_8": "-drive file=$GUEST_IMG,if=virtio,format=$IMG_FORMAT ", - "cfg_var_9": "-append \"root=/dev/vda3 ro console=hvc0 earlyprintk=ttyS0 ignore_loglevel debug earlyprintk l1tf=off initcall_debug log_buf_len=200M nokaslr tsc=reliable efi=debug mce=off efi=debug $CMDLINE\" ", - "cfg_var_10": "-bios $BIOS_IMG " - }, + "vm": { + "cfg_1": "-accel kvm -no-reboot -nographic -vga none -device virtio-net-pci,netdev=mynet0,mac=DE:AD:BE:EF:AB:CD,romfile= ", + "cfg_2": "-chardev stdio,id=mux,mux=on,signal=off -device virtio-serial,romfile= -device virtconsole,chardev=mux ", + "cfg_3": "-serial chardev:mux -monitor chardev:mux -monitor pty -no-hpet -nodefaults ", + "cfg_var_1": "-name process=$VM_TYPEVM_$PORT,debug-threads=on ", + "cfg_var_2": "-cpu host,host-phys-bits,pmu=$PMU ", + "cfg_var_3": "-smp cpus=$VCPU,sockets=$SOCKETS ", + "cfg_var_4": "-m $MEMG ", + "cfg_var_5": "-kernel $KERNEL_IMG ", + "cfg_var_6": "-initrd $INITRD_IMG", + "cfg_var_7": "-netdev user,id=mynet0,hostfwd=tcp::$PORT-:22 ", + "cfg_var_8": "-drive file=$GUEST_IMG,if=virtio,format=$IMG_FORMAT ", + "cfg_var_9": "-append \"root=/dev/vda3 ro console=hvc0 earlyprintk=ttyS0 ignore_loglevel debug earlyprintk l1tf=off initcall_debug log_buf_len=200M nokaslr tsc=reliable efi=debug mce=off efi=debug $CMDLINE\" ", + "cfg_var_10": "-bios $BIOS_IMG " + }, - "tdx": { - "cfg_1": "-machine q35,kernel_irqchip=split,confidential-guest-support=tdx,memory-backend=ram1 ", - "cfg_var_1": "-object tdx-guest,id=tdx,debug=$DEBUG,sept-ve-disable=on,quote-generation-service=vsock:2:4050 ", - "cfg_var_2": "-object memory-backend-memfd-private,id=ram1,size=$MEMG " - }, + "tdx": { + "cfg_1": "-machine q35,kernel_irqchip=split,confidential-guest-support=tdx,memory-backend=ram1 ", + "cfg_var_1": "-object tdx-guest,id=tdx,debug=$DEBUG,sept-ve-disable=on,quote-generation-service=vsock:2:4050 ", + "cfg_var_2": "-object memory-backend-memfd-private,id=ram1,size=$MEMG " + }, - "tdxio": { - "cfg_1": "-object iommufd,id=iommufd0 ", - "cfg_2": "-device vfio-pci,host=tee_bdf1,id=hostdev2,addr=0x3,x-secure-mode=on ", - "cfg_3": "-device vfio-pci,host=tee_bdf2,id=hostdev3,addr=0x4,x-secure-mode=on ", - "cfg_4": "-device vfio-pci,host=tee_bdf3,id=hostdev4,addr=0x5,x-secure-mode=on ", - "cfg_5": "-device vfio-pci,host=tee_bdf4,id=hostdev5,addr=0x6,x-secure-mode=on " - } + "tdxio": { + "cfg_1": "-object iommufd,id=iommufd0 ", + "cfg_2": "-device vfio-pci,host=tee_bdf1,id=hostdev2,addr=0x3,x-secure-mode=on ", + "cfg_3": "-device vfio-pci,host=tee_bdf2,id=hostdev3,addr=0x4,x-secure-mode=on ", + "cfg_4": "-device vfio-pci,host=tee_bdf3,id=hostdev4,addr=0x5,x-secure-mode=on ", + "cfg_5": "-device vfio-pci,host=tee_bdf4,id=hostdev5,addr=0x6,x-secure-mode=on " + } } \ No newline at end of file diff --git a/guest-test/qemu_get_config.py b/guest-test/qemu_get_config.py index d9e906d..a3ae630 100755 --- a/guest-test/qemu_get_config.py +++ b/guest-test/qemu_get_config.py @@ -25,9 +25,9 @@ # read from qemu.config.json format for all raw qemu vm config cwd = Path(os.getcwd()) if cwd.stem == "guest-test": - raw_config = Path(f"{os.getcwd()}/qemu.config.json").read_text() + raw_config = Path(f"{os.getcwd()}/qemu.config.json").read_text() else: - exit(1) + exit(1) qemu_config = json.loads(raw_config) @@ -54,40 +54,40 @@ # O-list variables default value from qemu.config.json vm_type = qemu_config["common"]["vm_type"] if 'PMU' in dir(): - pmu = PMU + pmu = PMU else: - pmu = qemu_config["common"]["pmu"] + pmu = qemu_config["common"]["pmu"] if 'VCPU' in dir(): - cpus = VCPU + cpus = VCPU else: - cpus = qemu_config["common"]["cpus"] + cpus = qemu_config["common"]["cpus"] if 'SOCKETS' in dir(): - sockets = SOCKETS + sockets = SOCKETS else: - sockets = qemu_config["common"]["sockets"] + sockets = qemu_config["common"]["sockets"] if 'MEM' in dir(): - mem = MEM + mem = MEM else: - mem = qemu_config["common"]["mem"] + mem = qemu_config["common"]["mem"] if 'CMDLINE' in dir(): - cmdline = CMDLINE + cmdline = CMDLINE else: - cmdline = qemu_config["common"]["cmdline"] + cmdline = qemu_config["common"]["cmdline"] if 'DEBUG' in dir(): - debug = DEBUG + debug = DEBUG else: - debug = qemu_config["common"]["debug"] + debug = qemu_config["common"]["debug"] if 'TESTCASE' in dir(): - testcase = TESTCASE + testcase = TESTCASE else: - print("No TESTCASE info found, can't run any test!") - exit(1) + print("No TESTCASE info found, can't run any test!") + exit(1) # O-list variables override value handling with args passed options, not used in framework, keep it for customization params_o_list = argparse.ArgumentParser() @@ -111,21 +111,21 @@ # NOTICE!! O-list veriables' value will be override if passed through above args option if args.vmtype is not None: - vm_type = args.vmtype + vm_type = args.vmtype if args.pmu is not None: - pmu = args.pmu + pmu = args.pmu if args.cpus is not None: - cpus = args.cpus + cpus = args.cpus if args.sockets is not None: - sockets = args.sockets + sockets = args.sockets if args.mem is not None: - mem = args.mem + mem = args.mem if args.cmdline is not None: - cmdline = args.cmdline + cmdline = args.cmdline if args.debug is not None: - debug = args.debug + debug = args.debug if args.testcase is not None: - testcase = args.testcase + testcase = args.testcase # end of O-list variables handling @@ -138,18 +138,18 @@ qemu_config["vm"]["cfg_var_5"] = qemu_config["vm"]["cfg_var_5"].replace("$KERNEL_IMG", kernel_img) # bypass -initrd config option in case it's not provided if os.path.isfile(initrd_img): - qemu_config["vm"]["cfg_var_6"] = qemu_config["vm"]["cfg_var_6"].replace("$INITRD_IMG", initrd_img) + qemu_config["vm"]["cfg_var_6"] = qemu_config["vm"]["cfg_var_6"].replace("$INITRD_IMG", initrd_img) else: - qemu_config["vm"]["cfg_var_6"] = "" + qemu_config["vm"]["cfg_var_6"] = "" qemu_config["vm"]["cfg_var_7"] = qemu_config["vm"]["cfg_var_7"].replace("$PORT", str(port)) qemu_config["vm"]["cfg_var_8"] = qemu_config["vm"]["cfg_var_8"].replace("$GUEST_IMG", guest_img).replace("$IMG_FORMAT", guest_img_format) qemu_config["vm"]["cfg_var_9"] = qemu_config["vm"]["cfg_var_9"].replace("$CMDLINE", cmdline) # bypass -bios config option in case it's not provided, default seabios to use if os.path.isfile(bios_img): - qemu_config["vm"]["cfg_var_10"] = qemu_config["vm"]["cfg_var_10"].replace("$BIOS_IMG", bios_img) + qemu_config["vm"]["cfg_var_10"] = qemu_config["vm"]["cfg_var_10"].replace("$BIOS_IMG", bios_img) else: - qemu_config["vm"]["cfg_var_10"] = "" + qemu_config["vm"]["cfg_var_10"] = "" qemu_config["tdx"]["cfg_var_1"] = qemu_config["tdx"]["cfg_var_1"].replace("$DEBUG", debug) qemu_config["tdx"]["cfg_var_2"] = qemu_config["tdx"]["cfg_var_2"].replace("$MEM", str(mem)) @@ -158,31 +158,31 @@ ###################### Functions ###################### def get_sub_keys(d, key): - """ - Recursively get all 2nd-level keys in a dictionary. - """ - if isinstance(d, dict): - for k, v in d.items(): - if isinstance(v, dict): - if k == key: - for k2 in v.keys(): - yield k2 + """ + Recursively get all 2nd-level keys in a dictionary. + """ + if isinstance(d, dict): + for k, v in d.items(): + if isinstance(v, dict): + if k == key: + for k2 in v.keys(): + yield k2 def print_sub_keys(l, key): - """ - Recursively get each 2nd-level key. - """ - print("Key %s has sub-keys:" %(key)) - for i in l: - print(i) + """ + Recursively get each 2nd-level key. + """ + print("Key %s has sub-keys:" %(key)) + for i in l: + print(i) def get_sub_cfgs(l, key, result=""): - """ - Recursively collect all 2nd-level key cfg string. - """ - for i in l: - result += qemu_config[key][i] - return result + """ + Recursively collect all 2nd-level key cfg string. + """ + for i in l: + result += qemu_config[key][i] + return result ###################### Do Works ###################### #common_keys = list(get_sub_keys(qemu_config, "common")) @@ -192,30 +192,30 @@ def get_sub_cfgs(l, key, result=""): #print_sub_keys(vm_keys, "vm") if vm_type == "legacy": - vm_cfg = get_sub_cfgs(vm_keys, "vm") - print("HERE're all the vm configs to launch legacy vm:") - print("#### qemu config option, part 1 ####") - print(vm_cfg) + vm_cfg = get_sub_cfgs(vm_keys, "vm") + print("HERE're all the vm configs to launch legacy vm:") + print("#### qemu config option, part 1 ####") + print(vm_cfg) #print_sub_keys(tdx_keys, "tdx") if vm_type == "tdx": - vm_cfg = get_sub_cfgs(vm_keys, "vm") - tdx_cfg = get_sub_cfgs(tdx_keys, "tdx") - print("HERE're all the tdx configs to launch tdx vm:") - print("#### qemu config option, part 1 ####") - print(vm_cfg) - print("#### qemu config option, part 2 ####") - print(tdx_cfg) + vm_cfg = get_sub_cfgs(vm_keys, "vm") + tdx_cfg = get_sub_cfgs(tdx_keys, "tdx") + print("HERE're all the tdx configs to launch tdx vm:") + print("#### qemu config option, part 1 ####") + print(vm_cfg) + print("#### qemu config option, part 2 ####") + print(tdx_cfg) #print_sub_keys(tdxio_keys, "tdxio") if vm_type == "tdxio": - vm_cfg = get_sub_cfgs(vm_keys, "vm") - tdx_cfg = get_sub_cfgs(tdx_keys, "tdx") - tdxio_cfg = get_sub_cfgs(tdxio_keys, "tdxio") - print("HERE're all the tdx configs to launch tdxio vm:") - print("#### qemu config option, part 1 ####") - print(vm_cfg) - print("#### qemu config option, part 2 ####") - print(tdx_cfg) - print("#### qemu config option, part 3 ####") - print(tdxio_cfg) \ No newline at end of file + vm_cfg = get_sub_cfgs(vm_keys, "vm") + tdx_cfg = get_sub_cfgs(tdx_keys, "tdx") + tdxio_cfg = get_sub_cfgs(tdxio_keys, "tdxio") + print("HERE're all the tdx configs to launch tdxio vm:") + print("#### qemu config option, part 1 ####") + print(vm_cfg) + print("#### qemu config option, part 2 ####") + print(tdx_cfg) + print("#### qemu config option, part 3 ####") + print(tdxio_cfg) \ No newline at end of file diff --git a/guest-test/qemu_runner.py b/guest-test/qemu_runner.py index 6164352..4ea7d0f 100644 --- a/guest-test/qemu_runner.py +++ b/guest-test/qemu_runner.py @@ -27,15 +27,15 @@ ###################### Do Works ###################### # launch legacy common vm based on vm_type config if vm_type == "legacy": - command = '{} {}'.format(qemu_img, vm_cfg) - sp.run(command, shell=True) + command = '{} {}'.format(qemu_img, vm_cfg) + sp.run(command, shell=True) # launch tdx vm based on vm_type config if vm_type == "tdx": - command = '{} {} {}'.format(qemu_img, vm_cfg, tdx_cfg) - sp.run(command, shell=True) + command = '{} {} {}'.format(qemu_img, vm_cfg, tdx_cfg) + sp.run(command, shell=True) # launch tdxio vm based on vm_type config if vm_type == "tdxio": - command = '{} {} {} {}'.format(qemu_img, vm_cfg, tdx_cfg, tdxio_cfg) - sp.run(command, shell=True) + command = '{} {} {} {}'.format(qemu_img, vm_cfg, tdx_cfg, tdxio_cfg) + sp.run(command, shell=True) \ No newline at end of file diff --git a/guest-test/tdx/tdx_attest_check.sh b/guest-test/tdx/tdx_attest_check.sh index 0fdada8..69a91f4 100755 --- a/guest-test/tdx/tdx_attest_check.sh +++ b/guest-test/tdx/tdx_attest_check.sh @@ -29,56 +29,56 @@ done ###################### Functions ###################### attest_result() { - selftest_item=$1 - case "$selftest_item" in - global.verify_report) - selftest_num=1 - ;; - global.verify_reportmac) - selftest_num=2 - ;; - global.verify_rtmr_extend) - selftest_num=3 - ;; - global.verify_quote) - selftest_num=4 - ;; - esac - test_print_trc "TD attestation - $selftest_item start." - if [ -f "attest.log" ]; then - rm -rf attest.log - fi - ./tdx_guest_test | tee attest.log - results=$(grep "not ok $selftest_num $selftest_item" attest.log) - if [ -z "$results" ]; then - test_print_trc "TD attestation - $selftest_item PASS." - else - die "TD attestation - $selftest_item FAIL." - return 1 - fi + selftest_item=$1 + case "$selftest_item" in + global.verify_report) + selftest_num=1 + ;; + global.verify_reportmac) + selftest_num=2 + ;; + global.verify_rtmr_extend) + selftest_num=3 + ;; + global.verify_quote) + selftest_num=4 + ;; + esac + test_print_trc "TD attestation - $selftest_item start." + if [ -f "attest.log" ]; then + rm -rf attest.log + fi + ./tdx_guest_test | tee attest.log + results=$(grep "not ok $selftest_num $selftest_item" attest.log) + if [ -z "$results" ]; then + test_print_trc "TD attestation - $selftest_item PASS." + else + die "TD attestation - $selftest_item FAIL." + return 1 + fi } ###################### Do Works ###################### case "$ATTEST_CASE" in - global.verify_report) - attest_result "$ATTEST_CASE" - ;; - global.verify_reportmac) - attest_result "$ATTEST_CASE" - ;; - global.verify_rtmr_extend) - attest_result "$ATTEST_CASE" - ;; - global.verify_quote) - attest_result "$ATTEST_CASE" - ;; - :) - test_print_err "Must specify the attest case option by [-t]" - exit 1 - ;; - \?) - test_print_err "Input test case option $ATTEST_CASE is not supported" - exit 1 - ;; -esac \ No newline at end of file + global.verify_report) + attest_result "$ATTEST_CASE" + ;; + global.verify_reportmac) + attest_result "$ATTEST_CASE" + ;; + global.verify_rtmr_extend) + attest_result "$ATTEST_CASE" + ;; + global.verify_quote) + attest_result "$ATTEST_CASE" + ;; + :) + test_print_err "Must specify the attest case option by [-t]" + exit 1 + ;; + \?) + test_print_err "Input test case option $ATTEST_CASE is not supported" + exit 1 + ;; +esac \ No newline at end of file