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 | |
| 17 | #include <sys/socket.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | // Needs to be included after sys/socket.h |
| 21 | #include <linux/vm_sockets.h> |
| 22 | |
Jiyong Park | dd4720b | 2021-06-25 13:05:50 +0900 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | #include <array> |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 25 | #include <iostream> |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 26 | #include <optional> |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 27 | |
| 28 | #include "android-base/file.h" |
| 29 | #include "android-base/logging.h" |
| 30 | #include "android-base/parseint.h" |
| 31 | #include "android-base/unique_fd.h" |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 32 | #include "android/system/virtualizationservice/VirtualMachineConfig.h" |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 33 | #include "virt/VirtualizationTest.h" |
| 34 | |
| 35 | using namespace android::base; |
Andrew Walbran | 06b5f5c | 2021-03-31 12:34:13 +0000 | [diff] [blame] | 36 | using namespace android::os; |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 37 | |
| 38 | namespace virt { |
| 39 | |
| 40 | static constexpr int kGuestPort = 45678; |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 41 | static constexpr const char kVmKernelPath[] = "/data/local/tmp/virt-test/kernel"; |
| 42 | static constexpr const char kVmInitrdPath[] = "/data/local/tmp/virt-test/initramfs"; |
| 43 | 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] | 44 | static constexpr const char kTestMessage[] = "HelloWorld"; |
| 45 | |
Jiyong Park | dd4720b | 2021-06-25 13:05:50 +0900 | [diff] [blame] | 46 | bool isVmSupported() { |
| 47 | const std::array<const char *, 4> needed_files = { |
| 48 | "/dev/kvm", |
| 49 | "/dev/vhost-vsock", |
| 50 | "/apex/com.android.virt/bin/crosvm", |
| 51 | "/apex/com.android.virt/bin/virtualizationservice", |
| 52 | }; |
| 53 | return std::all_of(needed_files.begin(), needed_files.end(), |
| 54 | [](const char *file) { return access(file, F_OK) == 0; }); |
| 55 | } |
| 56 | |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 57 | TEST_F(VirtualizationTest, TestVsock) { |
Jiyong Park | dd4720b | 2021-06-25 13:05:50 +0900 | [diff] [blame] | 58 | if (!isVmSupported()) { |
| 59 | GTEST_SKIP() << "Device doesn't support VM."; |
| 60 | } |
| 61 | |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 62 | binder::Status status; |
| 63 | |
| 64 | unique_fd server_fd(TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0))); |
| 65 | ASSERT_GE(server_fd, 0) << strerror(errno); |
| 66 | |
| 67 | struct sockaddr_vm server_sa = (struct sockaddr_vm){ |
| 68 | .svm_family = AF_VSOCK, |
| 69 | .svm_port = kGuestPort, |
| 70 | .svm_cid = VMADDR_CID_ANY, |
| 71 | }; |
| 72 | |
| 73 | int ret = TEMP_FAILURE_RETRY(bind(server_fd, (struct sockaddr *)&server_sa, sizeof(server_sa))); |
| 74 | ASSERT_EQ(ret, 0) << strerror(errno); |
| 75 | |
| 76 | LOG(INFO) << "Listening on port " << kGuestPort << "..."; |
| 77 | ret = TEMP_FAILURE_RETRY(listen(server_fd, 1)); |
| 78 | ASSERT_EQ(ret, 0) << strerror(errno); |
| 79 | |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 80 | VirtualMachineConfig config; |
| 81 | config.kernel = ParcelFileDescriptor(unique_fd(open(kVmKernelPath, O_RDONLY | O_CLOEXEC))); |
| 82 | config.initrd = ParcelFileDescriptor(unique_fd(open(kVmInitrdPath, O_RDONLY | O_CLOEXEC))); |
| 83 | config.params = String16(kVmParams); |
| 84 | |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 85 | sp<IVirtualMachine> vm; |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 86 | status = mVirtualizationService->startVm(config, std::nullopt, &vm); |
David Brazdil | 49f8a4d | 2021-03-04 09:57:33 +0000 | [diff] [blame] | 87 | ASSERT_TRUE(status.isOk()) << "Error starting VM: " << status; |
| 88 | |
| 89 | int32_t cid; |
| 90 | status = vm->getCid(&cid); |
| 91 | ASSERT_TRUE(status.isOk()) << "Error getting CID: " << status; |
| 92 | LOG(INFO) << "VM starting with CID " << cid; |
| 93 | |
| 94 | LOG(INFO) << "Accepting connection..."; |
| 95 | struct sockaddr_vm client_sa; |
| 96 | socklen_t client_sa_len = sizeof(client_sa); |
| 97 | unique_fd client_fd( |
| 98 | TEMP_FAILURE_RETRY(accept(server_fd, (struct sockaddr *)&client_sa, &client_sa_len))); |
| 99 | ASSERT_GE(client_fd, 0) << strerror(errno); |
| 100 | LOG(INFO) << "Connection from CID " << client_sa.svm_cid << " on port " << client_sa.svm_port; |
| 101 | |
| 102 | LOG(INFO) << "Reading message from the client..."; |
| 103 | std::string msg; |
| 104 | ASSERT_TRUE(ReadFdToString(client_fd, &msg)); |
| 105 | |
| 106 | LOG(INFO) << "Received message: " << msg; |
| 107 | ASSERT_EQ(msg, kTestMessage); |
| 108 | } |
| 109 | |
| 110 | } // namespace virt |