Vsock end-to-end integration test
Add VsockTest host-side test that runs a vsock server in Android and
spawns a VM with a client that connects to it. The client sends a
message to the server. The host-side driver validates that the message
received by the server matches the message given to the client via
kernel command-line arguments.
Bug: 168589743
Test: atest VirtualizationHostTestCases
Change-Id: Id6d1f1c5ff10066a73606ee5b14387ba4474a3f8
diff --git a/tests/hostside/Android.bp b/tests/hostside/Android.bp
index 0c894ac..3f04a78 100644
--- a/tests/hostside/Android.bp
+++ b/tests/hostside/Android.bp
@@ -28,6 +28,9 @@
":virt_hostside_tests_initramfs-arm64",
":virt_hostside_tests_initramfs-x86_64",
],
+ required: [
+ "virt_hostside_tests_vsock_server",
+ ],
}
// Give kernel images unique file names.
@@ -56,9 +59,11 @@
out: ["initramfs.lz4"],
srcs: [
":virt_hostside_tests_guest_init",
+ ":virt_hostside_tests_vsock_client",
],
cmd: "$(location scripts/place_files.sh) $(in) -- " +
"$(genDir)/root/init " +
+ "$(genDir)/root/bin/vsock_client " +
"&& $(location mkbootfs) $(genDir)/root | $(location lz4) -fq - $(out)",
}
diff --git a/tests/hostside/AndroidTest.xml b/tests/hostside/AndroidTest.xml
index ab77b45..1fd86ef 100644
--- a/tests/hostside/AndroidTest.xml
+++ b/tests/hostside/AndroidTest.xml
@@ -35,6 +35,7 @@
<option name="push-file" key="virt_hostside_tests_initramfs-arm64" value="/data/local/tmp/virt-test/arm64/initramfs" />
<option name="push-file" key="virt_hostside_tests_kernel-x86_64" value="/data/local/tmp/virt-test/x86_64/kernel" />
<option name="push-file" key="virt_hostside_tests_initramfs-x86_64" value="/data/local/tmp/virt-test/x86_64/initramfs" />
+ <option name="push-file" key="virt_hostside_tests_vsock_server" value="/data/local/tmp/virt-test" />
</target_preparer>
<!-- Root currently needed to run CrosVM.
diff --git a/tests/hostside/java/android/virt/test/VsockTest.java b/tests/hostside/java/android/virt/test/VsockTest.java
new file mode 100644
index 0000000..c82db77
--- /dev/null
+++ b/tests/hostside/java/android/virt/test/VsockTest.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.virt.test;
+
+import com.android.tradefed.log.LogUtil.CLog;
+import com.android.tradefed.util.CommandResult;
+import com.android.tradefed.util.CommandStatus;
+
+import org.junit.Test;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+public class VsockTest extends VirtTestCase {
+ private static final long TIMEOUT = 2L;
+ private static final TimeUnit TIMEOUT_UNIT = TimeUnit.MINUTES;
+ private static final int RETRIES = 0;
+
+ private static final Integer HOST_CID = 2;
+ private static final Integer GUEST_CID = 42;
+ private static final Integer GUEST_PORT = 45678;
+ private static final String TEST_MESSAGE = "HelloWorld";
+
+ private static final String CLIENT_PATH = "bin/vsock_client";
+ private static final String SERVER_TARGET = "virt_hostside_tests_vsock_server";
+
+ @Test
+ public void testVsockServer() throws Exception {
+ ExecutorService executor = Executors.newFixedThreadPool(2);
+
+ final String serverPath = getDevicePathForTestBinary(SERVER_TARGET);
+ final String serverCmd = createCommand(serverPath, GUEST_PORT);
+ final String clientCmd = createCommand(CLIENT_PATH, HOST_CID, GUEST_PORT, TEST_MESSAGE);
+ final String vmCmd = getVmCommand(clientCmd, GUEST_CID);
+
+ // Start server in Android that listens for vsock connections.
+ // It will receive a message from a client in the guest VM.
+ Future<?> serverTask = executor.submit(() -> {
+ CommandResult res = getDevice().executeShellV2Command(
+ serverCmd, TIMEOUT, TIMEOUT_UNIT, RETRIES);
+ assertEquals(TEST_MESSAGE, res.getStdout().trim());
+ return null;
+ });
+
+ // Run VM that will connect to the server and send a message to it.
+ Future<?> vmTask = executor.submit(() -> {
+ CommandResult res = getDevice().executeShellV2Command(
+ vmCmd, TIMEOUT, TIMEOUT_UNIT, RETRIES);
+ CLog.d(res.getStdout()); // print VMM output into host_log
+ assertEquals(CommandStatus.SUCCESS, res.getStatus());
+ return null;
+ });
+
+ // Wait for the VMM to finish sending the message.
+ try {
+ vmTask.get(TIMEOUT, TIMEOUT_UNIT);
+ } catch (Throwable ex) {
+ // The VMM either exited with a non-zero code or it timed out.
+ // Kill the server process, the test has failed.
+ // Note: executeShellV2Command cannot be interrupted. This will wait
+ // until `serverTask` times out.
+ executor.shutdownNow();
+ throw ex;
+ }
+
+ // Wait for the server to finish processing the message.
+ serverTask.get(TIMEOUT, TIMEOUT_UNIT);
+ }
+}
diff --git a/tests/hostside/native/vsock/Android.bp b/tests/hostside/native/vsock/Android.bp
new file mode 100644
index 0000000..cbee98d
--- /dev/null
+++ b/tests/hostside/native/vsock/Android.bp
@@ -0,0 +1,35 @@
+// Copyright (C) 2020 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+cc_test {
+ name: "virt_hostside_tests_vsock_server",
+ srcs: ["server.cc"],
+ static_libs: [
+ "libbase",
+ "liblog",
+ ],
+ static_executable: true,
+ test_suites: ["device-tests"],
+}
+
+cc_binary {
+ name: "virt_hostside_tests_vsock_client",
+ srcs: ["client.cc"],
+ static_libs: [
+ "libbase",
+ "liblog",
+ ],
+ static_executable: true,
+ installable: false,
+}
diff --git a/tests/hostside/native/vsock/client.cc b/tests/hostside/native/vsock/client.cc
new file mode 100644
index 0000000..7a72e11
--- /dev/null
+++ b/tests/hostside/native/vsock/client.cc
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/socket.h>
+
+// Needs to be included after sys/socket.h
+#include <linux/vm_sockets.h>
+
+#include <iostream>
+
+#include "android-base/file.h"
+#include "android-base/logging.h"
+#include "android-base/parseint.h"
+#include "android-base/unique_fd.h"
+
+using namespace android::base;
+
+int main(int argc, const char *argv[]) {
+ SetLogger(StderrLogger);
+
+ unsigned int cid, port;
+ if (argc != 4 || !ParseUint(argv[1], &cid) || !ParseUint(argv[2], &port)) {
+ LOG(ERROR) << "Usage: " << argv[0] << " <cid> <port> <msg>";
+ return EXIT_FAILURE;
+ }
+ std::string msg(argv[3]);
+
+ unique_fd fd(TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0)));
+ if (fd < 0) {
+ PLOG(ERROR) << "socket";
+ return EXIT_FAILURE;
+ }
+
+ struct sockaddr_vm sa = (struct sockaddr_vm){
+ .svm_family = AF_VSOCK,
+ .svm_port = port,
+ .svm_cid = cid,
+ };
+
+ LOG(INFO) << "Connecting to CID " << cid << " on port " << port << "...";
+ int ret = TEMP_FAILURE_RETRY(connect(fd, (struct sockaddr *)&sa, sizeof(sa)));
+ if (ret < 0) {
+ PLOG(ERROR) << "connect";
+ return EXIT_FAILURE;
+ }
+
+ LOG(INFO) << "Sending message to server...";
+ if (!WriteStringToFd(msg, fd)) {
+ PLOG(ERROR) << "WriteStringToFd";
+ return EXIT_FAILURE;
+ }
+
+ LOG(INFO) << "Exiting...";
+ return EXIT_SUCCESS;
+}
diff --git a/tests/hostside/native/vsock/server.cc b/tests/hostside/native/vsock/server.cc
new file mode 100644
index 0000000..d4a99d2
--- /dev/null
+++ b/tests/hostside/native/vsock/server.cc
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/socket.h>
+#include <unistd.h>
+
+// Needs to be included after sys/socket.h
+#include <linux/vm_sockets.h>
+
+#include <iostream>
+
+#include "android-base/file.h"
+#include "android-base/logging.h"
+#include "android-base/parseint.h"
+#include "android-base/unique_fd.h"
+
+using namespace android::base;
+
+int main(int argc, const char *argv[]) {
+ unsigned int port;
+ if (argc != 2 || !ParseUint(argv[1], &port)) {
+ LOG(ERROR) << "Usage: " << argv[0] << " <port>";
+ return EXIT_FAILURE;
+ }
+
+ unique_fd server_fd(TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0)));
+ if (server_fd < 0) {
+ PLOG(ERROR) << "socket";
+ return EXIT_FAILURE;
+ }
+
+ struct sockaddr_vm server_sa = (struct sockaddr_vm){
+ .svm_family = AF_VSOCK,
+ .svm_port = port,
+ .svm_cid = VMADDR_CID_ANY,
+ };
+
+ int ret = TEMP_FAILURE_RETRY(bind(server_fd, (struct sockaddr *)&server_sa, sizeof(server_sa)));
+ if (ret != 0) {
+ PLOG(ERROR) << "bind";
+ return EXIT_FAILURE;
+ }
+
+ LOG(INFO) << "Listening on port " << port << "...";
+ ret = TEMP_FAILURE_RETRY(listen(server_fd, 1));
+ if (ret != 0) {
+ PLOG(ERROR) << "listen";
+ return EXIT_FAILURE;
+ }
+
+ LOG(INFO) << "Accepting connection...";
+ struct sockaddr_vm client_sa;
+ socklen_t client_sa_len = sizeof(client_sa);
+ unique_fd client_fd(
+ TEMP_FAILURE_RETRY(accept(server_fd, (struct sockaddr *)&client_sa, &client_sa_len)));
+ if (client_fd < 0) {
+ PLOG(ERROR) << "accept";
+ return EXIT_FAILURE;
+ }
+ LOG(INFO) << "Connection from CID " << client_sa.svm_cid << " on port " << client_sa.svm_port;
+
+ LOG(INFO) << "Reading message from the client...";
+ std::string msg;
+ if (!ReadFdToString(client_fd, &msg)) {
+ PLOG(ERROR) << "ReadFdToString";
+ return EXIT_FAILURE;
+ }
+
+ // Print the received message to stdout.
+ std::cout << msg << std::endl;
+
+ LOG(INFO) << "Exiting...";
+ return EXIT_SUCCESS;
+}