blob: 233c6dd9a8c470870683db3180774833ae2d6455 [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"
33#include "android-base/unique_fd.h"
Andrew Walbranf6bf6862021-05-21 12:41:13 +000034#include "android/system/virtualizationservice/VirtualMachineConfig.h"
David Brazdil49f8a4d2021-03-04 09:57:33 +000035#include "virt/VirtualizationTest.h"
36
Andrew Walbran3e73a092021-06-25 11:32:23 +000037#define KVM_CAP_ARM_PROTECTED_VM 0xffbadab1
38
David Brazdil49f8a4d2021-03-04 09:57:33 +000039using namespace android::base;
Andrew Walbran06b5f5c2021-03-31 12:34:13 +000040using namespace android::os;
David Brazdil49f8a4d2021-03-04 09:57:33 +000041
42namespace virt {
43
44static constexpr int kGuestPort = 45678;
Andrew Walbran3a5a9212021-05-04 17:09:08 +000045static constexpr const char kVmKernelPath[] = "/data/local/tmp/virt-test/kernel";
46static constexpr const char kVmInitrdPath[] = "/data/local/tmp/virt-test/initramfs";
47static constexpr const char kVmParams[] = "rdinit=/bin/init bin/vsock_client 2 45678 HelloWorld";
David Brazdil49f8a4d2021-03-04 09:57:33 +000048static constexpr const char kTestMessage[] = "HelloWorld";
49
Jiyong Parkdd4720b2021-06-25 13:05:50 +090050bool isVmSupported() {
51 const std::array<const char *, 4> needed_files = {
52 "/dev/kvm",
53 "/dev/vhost-vsock",
54 "/apex/com.android.virt/bin/crosvm",
55 "/apex/com.android.virt/bin/virtualizationservice",
56 };
57 return std::all_of(needed_files.begin(), needed_files.end(),
58 [](const char *file) { return access(file, F_OK) == 0; });
59}
60
Andrew Walbran3e73a092021-06-25 11:32:23 +000061/** Returns true if the kernel supports Protected KVM. */
62bool isPkvmSupported() {
63 unique_fd kvm_fd(open("/dev/kvm", O_NONBLOCK | O_CLOEXEC));
64 return kvm_fd != 0 && ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_ARM_PROTECTED_VM) == 1;
65}
Jiyong Parkdd4720b2021-06-25 13:05:50 +090066
Andrew Walbran3e73a092021-06-25 11:32:23 +000067void runTest(sp<IVirtualizationService> virtualization_service, bool protected_vm) {
David Brazdil49f8a4d2021-03-04 09:57:33 +000068 binder::Status status;
69
70 unique_fd server_fd(TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0)));
71 ASSERT_GE(server_fd, 0) << strerror(errno);
72
73 struct sockaddr_vm server_sa = (struct sockaddr_vm){
74 .svm_family = AF_VSOCK,
75 .svm_port = kGuestPort,
76 .svm_cid = VMADDR_CID_ANY,
77 };
78
79 int ret = TEMP_FAILURE_RETRY(bind(server_fd, (struct sockaddr *)&server_sa, sizeof(server_sa)));
80 ASSERT_EQ(ret, 0) << strerror(errno);
81
82 LOG(INFO) << "Listening on port " << kGuestPort << "...";
83 ret = TEMP_FAILURE_RETRY(listen(server_fd, 1));
84 ASSERT_EQ(ret, 0) << strerror(errno);
85
Andrew Walbran3a5a9212021-05-04 17:09:08 +000086 VirtualMachineConfig config;
87 config.kernel = ParcelFileDescriptor(unique_fd(open(kVmKernelPath, O_RDONLY | O_CLOEXEC)));
88 config.initrd = ParcelFileDescriptor(unique_fd(open(kVmInitrdPath, O_RDONLY | O_CLOEXEC)));
89 config.params = String16(kVmParams);
Andrew Walbran3e73a092021-06-25 11:32:23 +000090 config.protected_vm = protected_vm;
Andrew Walbran3a5a9212021-05-04 17:09:08 +000091
David Brazdil49f8a4d2021-03-04 09:57:33 +000092 sp<IVirtualMachine> vm;
Andrew Walbran3e73a092021-06-25 11:32:23 +000093 status = virtualization_service->startVm(config, std::nullopt, &vm);
David Brazdil49f8a4d2021-03-04 09:57:33 +000094 ASSERT_TRUE(status.isOk()) << "Error starting VM: " << status;
95
96 int32_t cid;
97 status = vm->getCid(&cid);
98 ASSERT_TRUE(status.isOk()) << "Error getting CID: " << status;
99 LOG(INFO) << "VM starting with CID " << cid;
100
101 LOG(INFO) << "Accepting connection...";
102 struct sockaddr_vm client_sa;
103 socklen_t client_sa_len = sizeof(client_sa);
104 unique_fd client_fd(
105 TEMP_FAILURE_RETRY(accept(server_fd, (struct sockaddr *)&client_sa, &client_sa_len)));
106 ASSERT_GE(client_fd, 0) << strerror(errno);
107 LOG(INFO) << "Connection from CID " << client_sa.svm_cid << " on port " << client_sa.svm_port;
108
109 LOG(INFO) << "Reading message from the client...";
110 std::string msg;
111 ASSERT_TRUE(ReadFdToString(client_fd, &msg));
112
113 LOG(INFO) << "Received message: " << msg;
114 ASSERT_EQ(msg, kTestMessage);
115}
116
Andrew Walbran3e73a092021-06-25 11:32:23 +0000117TEST_F(VirtualizationTest, TestVsock) {
118 if (!isVmSupported()) {
119 GTEST_SKIP() << "Device doesn't support KVM.";
120 }
121
122 runTest(mVirtualizationService, false);
123}
124
125TEST_F(VirtualizationTest, TestVsockProtected) {
126 if (!isVmSupported()) {
127 GTEST_SKIP() << "Device doesn't support KVM.";
128 } else if (!isPkvmSupported()) {
129 GTEST_SKIP() << "Skipping as pKVM is not supported on this device.";
130 }
131
132 runTest(mVirtualizationService, true);
133}
134
David Brazdil49f8a4d2021-03-04 09:57:33 +0000135} // namespace virt