David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Walbran | 9d39f30 | 2022-02-02 17:51:43 +0000 | [diff] [blame^] | 17 | #include <android/sysprop/HypervisorProperties.sysprop.h> |
Andrew Walbran | 3e73a09 | 2021-06-25 11:32:23 +0000 | [diff] [blame] | 18 | #include <linux/kvm.h> |
| 19 | #include <sys/ioctl.h> |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 20 | #include <sys/socket.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | // Needs to be included after sys/socket.h |
| 24 | #include <linux/vm_sockets.h> |
| 25 | |
Jiyong Park | dd4720b | 2021-06-25 13:05:50 +0900 | [diff] [blame] | 26 | #include <algorithm> |
| 27 | #include <array> |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 28 | #include <iostream> |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 29 | #include <optional> |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 30 | |
| 31 | #include "android-base/file.h" |
| 32 | #include "android-base/logging.h" |
| 33 | #include "android-base/parseint.h" |
| 34 | #include "android-base/unique_fd.h" |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 35 | #include "android/system/virtualizationservice/VirtualMachineConfig.h" |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 36 | #include "android/system/virtualizationservice/VirtualMachineRawConfig.h" |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 37 | #include "virt/VirtualizationTest.h" |
| 38 | |
Andrew Walbran | 3e73a09 | 2021-06-25 11:32:23 +0000 | [diff] [blame] | 39 | #define KVM_CAP_ARM_PROTECTED_VM 0xffbadab1 |
| 40 | |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 41 | using namespace android::base; |
Andrew Walbran | 06b5f5c | 2021-03-31 12:34:13 +0000 | [diff] [blame] | 42 | using namespace android::os; |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 43 | |
| 44 | namespace virt { |
| 45 | |
| 46 | static constexpr int kGuestPort = 45678; |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 47 | static constexpr const char kVmKernelPath[] = "/data/local/tmp/virt-test/kernel"; |
| 48 | static constexpr const char kVmInitrdPath[] = "/data/local/tmp/virt-test/initramfs"; |
| 49 | static constexpr const char kVmParams[] = "rdinit=/bin/init bin/vsock_client 2 45678 HelloWorld"; |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 50 | static constexpr const char kTestMessage[] = "HelloWorld"; |
| 51 | |
Andrew Walbran | 97aade0 | 2022-02-02 16:42:44 +0000 | [diff] [blame] | 52 | /** Returns true if the kernel supports protected VMs. */ |
| 53 | bool isProtectedVmSupported() { |
Andrew Walbran | 9d39f30 | 2022-02-02 17:51:43 +0000 | [diff] [blame^] | 54 | return android::sysprop::HypervisorProperties::hypervisor_protected_vm_supported().value_or( |
| 55 | false); |
Andrew Walbran | 97aade0 | 2022-02-02 16:42:44 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** Returns true if the kernel supports unprotected VMs. */ |
| 59 | bool isUnprotectedVmSupported() { |
Andrew Walbran | 9d39f30 | 2022-02-02 17:51:43 +0000 | [diff] [blame^] | 60 | return android::sysprop::HypervisorProperties::hypervisor_vm_supported().value_or(false); |
Andrew Walbran | 3e73a09 | 2021-06-25 11:32:23 +0000 | [diff] [blame] | 61 | } |
Jiyong Park | dd4720b | 2021-06-25 13:05:50 +0900 | [diff] [blame] | 62 | |
Andrew Walbran | 3e73a09 | 2021-06-25 11:32:23 +0000 | [diff] [blame] | 63 | void runTest(sp<IVirtualizationService> virtualization_service, bool protected_vm) { |
Andrew Walbran | 97aade0 | 2022-02-02 16:42:44 +0000 | [diff] [blame] | 64 | if (protected_vm) { |
| 65 | if (!isProtectedVmSupported()) { |
| 66 | GTEST_SKIP() << "Skipping as protected VMs are not supported on this device."; |
| 67 | } |
| 68 | } else { |
| 69 | if (!isUnprotectedVmSupported()) { |
| 70 | GTEST_SKIP() << "Skipping as unprotected VMs are not supported on this device."; |
| 71 | } |
Jiyong Park | aaf32f2 | 2021-08-30 19:11:19 +0900 | [diff] [blame] | 72 | } |
| 73 | |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 74 | binder::Status status; |
| 75 | |
| 76 | unique_fd server_fd(TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0))); |
| 77 | ASSERT_GE(server_fd, 0) << strerror(errno); |
| 78 | |
| 79 | struct sockaddr_vm server_sa = (struct sockaddr_vm){ |
| 80 | .svm_family = AF_VSOCK, |
| 81 | .svm_port = kGuestPort, |
| 82 | .svm_cid = VMADDR_CID_ANY, |
| 83 | }; |
| 84 | |
| 85 | int ret = TEMP_FAILURE_RETRY(bind(server_fd, (struct sockaddr *)&server_sa, sizeof(server_sa))); |
| 86 | ASSERT_EQ(ret, 0) << strerror(errno); |
| 87 | |
| 88 | LOG(INFO) << "Listening on port " << kGuestPort << "..."; |
| 89 | ret = TEMP_FAILURE_RETRY(listen(server_fd, 1)); |
| 90 | ASSERT_EQ(ret, 0) << strerror(errno); |
| 91 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 92 | VirtualMachineRawConfig raw_config; |
| 93 | raw_config.kernel = ParcelFileDescriptor(unique_fd(open(kVmKernelPath, O_RDONLY | O_CLOEXEC))); |
| 94 | raw_config.initrd = ParcelFileDescriptor(unique_fd(open(kVmInitrdPath, O_RDONLY | O_CLOEXEC))); |
| 95 | raw_config.params = kVmParams; |
Andrew Walbran | cc04590 | 2021-07-27 16:06:17 +0000 | [diff] [blame] | 96 | raw_config.protectedVm = protected_vm; |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 97 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 98 | VirtualMachineConfig config(std::move(raw_config)); |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 99 | sp<IVirtualMachine> vm; |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 100 | status = virtualization_service->createVm(config, std::nullopt, std::nullopt, &vm); |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 101 | ASSERT_TRUE(status.isOk()) << "Error creating VM: " << status; |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 102 | |
| 103 | int32_t cid; |
| 104 | status = vm->getCid(&cid); |
| 105 | ASSERT_TRUE(status.isOk()) << "Error getting CID: " << status; |
| 106 | LOG(INFO) << "VM starting with CID " << cid; |
| 107 | |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 108 | status = vm->start(); |
| 109 | ASSERT_TRUE(status.isOk()) << "Error starting VM: " << status; |
| 110 | |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 111 | LOG(INFO) << "Accepting connection..."; |
| 112 | struct sockaddr_vm client_sa; |
| 113 | socklen_t client_sa_len = sizeof(client_sa); |
| 114 | unique_fd client_fd( |
| 115 | TEMP_FAILURE_RETRY(accept(server_fd, (struct sockaddr *)&client_sa, &client_sa_len))); |
| 116 | ASSERT_GE(client_fd, 0) << strerror(errno); |
| 117 | LOG(INFO) << "Connection from CID " << client_sa.svm_cid << " on port " << client_sa.svm_port; |
| 118 | |
| 119 | LOG(INFO) << "Reading message from the client..."; |
| 120 | std::string msg; |
| 121 | ASSERT_TRUE(ReadFdToString(client_fd, &msg)); |
| 122 | |
| 123 | LOG(INFO) << "Received message: " << msg; |
| 124 | ASSERT_EQ(msg, kTestMessage); |
| 125 | } |
| 126 | |
Andrew Walbran | 3e73a09 | 2021-06-25 11:32:23 +0000 | [diff] [blame] | 127 | TEST_F(VirtualizationTest, TestVsock) { |
Andrew Walbran | 3e73a09 | 2021-06-25 11:32:23 +0000 | [diff] [blame] | 128 | runTest(mVirtualizationService, false); |
| 129 | } |
| 130 | |
| 131 | TEST_F(VirtualizationTest, TestVsockProtected) { |
Andrew Walbran | 3e73a09 | 2021-06-25 11:32:23 +0000 | [diff] [blame] | 132 | runTest(mVirtualizationService, true); |
| 133 | } |
| 134 | |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 135 | } // namespace virt |