blob: de1387e36c4aa619028295bf92e3e27ed9932dd7 [file] [log] [blame]
David Brazdil49f8a4d2021-03-04 09:57:33 +00001/*
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 Walbran3e73a092021-06-25 11:32:23 +000017#include <linux/kvm.h>
18#include <sys/ioctl.h>
David Brazdil49f8a4d2021-03-04 09:57:33 +000019#include <sys/socket.h>
20#include <unistd.h>
21
22// Needs to be included after sys/socket.h
23#include <linux/vm_sockets.h>
24
Jiyong Parkdd4720b2021-06-25 13:05:50 +090025#include <algorithm>
26#include <array>
David Brazdil49f8a4d2021-03-04 09:57:33 +000027#include <iostream>
Andrew Walbrana89fc132021-03-17 17:08:36 +000028#include <optional>
David Brazdil49f8a4d2021-03-04 09:57:33 +000029
30#include "android-base/file.h"
31#include "android-base/logging.h"
32#include "android-base/parseint.h"
Andrew Walbran97aade02022-02-02 16:42:44 +000033#include "android-base/properties.h"
David Brazdil49f8a4d2021-03-04 09:57:33 +000034#include "android-base/unique_fd.h"
Andrew Walbranf6bf6862021-05-21 12:41:13 +000035#include "android/system/virtualizationservice/VirtualMachineConfig.h"
Jooyung Han21e9b922021-06-26 04:14:16 +090036#include "android/system/virtualizationservice/VirtualMachineRawConfig.h"
David Brazdil49f8a4d2021-03-04 09:57:33 +000037#include "virt/VirtualizationTest.h"
38
Andrew Walbran3e73a092021-06-25 11:32:23 +000039#define KVM_CAP_ARM_PROTECTED_VM 0xffbadab1
40
David Brazdil49f8a4d2021-03-04 09:57:33 +000041using namespace android::base;
Andrew Walbran06b5f5c2021-03-31 12:34:13 +000042using namespace android::os;
David Brazdil49f8a4d2021-03-04 09:57:33 +000043
44namespace virt {
45
46static constexpr int kGuestPort = 45678;
Andrew Walbran3a5a9212021-05-04 17:09:08 +000047static constexpr const char kVmKernelPath[] = "/data/local/tmp/virt-test/kernel";
48static constexpr const char kVmInitrdPath[] = "/data/local/tmp/virt-test/initramfs";
49static constexpr const char kVmParams[] = "rdinit=/bin/init bin/vsock_client 2 45678 HelloWorld";
David Brazdil49f8a4d2021-03-04 09:57:33 +000050static constexpr const char kTestMessage[] = "HelloWorld";
51
Andrew Walbran97aade02022-02-02 16:42:44 +000052/** Returns true if the kernel supports protected VMs. */
53bool isProtectedVmSupported() {
54 return GetBoolProperty("ro.boot.hypervisor.protected_vm.supported", false);
55}
56
57/** Returns true if the kernel supports unprotected VMs. */
58bool isUnprotectedVmSupported() {
59 return GetBoolProperty("ro.boot.hypervisor.vm.supported", false);
Andrew Walbran3e73a092021-06-25 11:32:23 +000060}
Jiyong Parkdd4720b2021-06-25 13:05:50 +090061
Andrew Walbran3e73a092021-06-25 11:32:23 +000062void runTest(sp<IVirtualizationService> virtualization_service, bool protected_vm) {
Andrew Walbran97aade02022-02-02 16:42:44 +000063 if (protected_vm) {
64 if (!isProtectedVmSupported()) {
65 GTEST_SKIP() << "Skipping as protected VMs are not supported on this device.";
66 }
67 } else {
68 if (!isUnprotectedVmSupported()) {
69 GTEST_SKIP() << "Skipping as unprotected VMs are not supported on this device.";
70 }
Jiyong Parkaaf32f22021-08-30 19:11:19 +090071 }
72
David Brazdil49f8a4d2021-03-04 09:57:33 +000073 binder::Status status;
74
75 unique_fd server_fd(TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0)));
76 ASSERT_GE(server_fd, 0) << strerror(errno);
77
78 struct sockaddr_vm server_sa = (struct sockaddr_vm){
79 .svm_family = AF_VSOCK,
80 .svm_port = kGuestPort,
81 .svm_cid = VMADDR_CID_ANY,
82 };
83
84 int ret = TEMP_FAILURE_RETRY(bind(server_fd, (struct sockaddr *)&server_sa, sizeof(server_sa)));
85 ASSERT_EQ(ret, 0) << strerror(errno);
86
87 LOG(INFO) << "Listening on port " << kGuestPort << "...";
88 ret = TEMP_FAILURE_RETRY(listen(server_fd, 1));
89 ASSERT_EQ(ret, 0) << strerror(errno);
90
Jooyung Han21e9b922021-06-26 04:14:16 +090091 VirtualMachineRawConfig raw_config;
92 raw_config.kernel = ParcelFileDescriptor(unique_fd(open(kVmKernelPath, O_RDONLY | O_CLOEXEC)));
93 raw_config.initrd = ParcelFileDescriptor(unique_fd(open(kVmInitrdPath, O_RDONLY | O_CLOEXEC)));
94 raw_config.params = kVmParams;
Andrew Walbrancc045902021-07-27 16:06:17 +000095 raw_config.protectedVm = protected_vm;
Andrew Walbran3a5a9212021-05-04 17:09:08 +000096
Jooyung Han21e9b922021-06-26 04:14:16 +090097 VirtualMachineConfig config(std::move(raw_config));
David Brazdil49f8a4d2021-03-04 09:57:33 +000098 sp<IVirtualMachine> vm;
Jiyong Parkb8182bb2021-10-26 22:53:08 +090099 status = virtualization_service->createVm(config, std::nullopt, std::nullopt, &vm);
Andrew Walbranf8d94112021-09-07 11:45:36 +0000100 ASSERT_TRUE(status.isOk()) << "Error creating VM: " << status;
David Brazdil49f8a4d2021-03-04 09:57:33 +0000101
102 int32_t cid;
103 status = vm->getCid(&cid);
104 ASSERT_TRUE(status.isOk()) << "Error getting CID: " << status;
105 LOG(INFO) << "VM starting with CID " << cid;
106
Andrew Walbranf8d94112021-09-07 11:45:36 +0000107 status = vm->start();
108 ASSERT_TRUE(status.isOk()) << "Error starting VM: " << status;
109
David Brazdil49f8a4d2021-03-04 09:57:33 +0000110 LOG(INFO) << "Accepting connection...";
111 struct sockaddr_vm client_sa;
112 socklen_t client_sa_len = sizeof(client_sa);
113 unique_fd client_fd(
114 TEMP_FAILURE_RETRY(accept(server_fd, (struct sockaddr *)&client_sa, &client_sa_len)));
115 ASSERT_GE(client_fd, 0) << strerror(errno);
116 LOG(INFO) << "Connection from CID " << client_sa.svm_cid << " on port " << client_sa.svm_port;
117
118 LOG(INFO) << "Reading message from the client...";
119 std::string msg;
120 ASSERT_TRUE(ReadFdToString(client_fd, &msg));
121
122 LOG(INFO) << "Received message: " << msg;
123 ASSERT_EQ(msg, kTestMessage);
124}
125
Andrew Walbran3e73a092021-06-25 11:32:23 +0000126TEST_F(VirtualizationTest, TestVsock) {
Andrew Walbran3e73a092021-06-25 11:32:23 +0000127 runTest(mVirtualizationService, false);
128}
129
130TEST_F(VirtualizationTest, TestVsockProtected) {
Andrew Walbran3e73a092021-06-25 11:32:23 +0000131 runTest(mVirtualizationService, true);
132}
133
David Brazdil49f8a4d2021-03-04 09:57:33 +0000134} // namespace virt