benchmark:Measure vsock host to VM transfer rate

Bug: 236123069
Bug: 244408006
Test: atest MicrodroidBenchmarks
Change-Id: I3731c7134efa6b0fddc24dc11b4e7a813f679be1
diff --git a/tests/benchmark/src/native/benchmarkbinary.cpp b/tests/benchmark/src/native/benchmarkbinary.cpp
index 2558a7d..28799c8 100644
--- a/tests/benchmark/src/native/benchmarkbinary.cpp
+++ b/tests/benchmark/src/native/benchmarkbinary.cpp
@@ -16,22 +16,23 @@
 
 #include <aidl/android/system/virtualmachineservice/IVirtualMachineService.h>
 #include <aidl/com/android/microdroid/testservice/BnBenchmarkService.h>
+#include <android-base/logging.h>
+#include <android-base/parseint.h>
 #include <android-base/result.h>
+#include <android-base/strings.h>
 #include <android-base/unique_fd.h>
 #include <fcntl.h>
 #include <linux/vm_sockets.h>
 #include <stdio.h>
+#include <time.h>
 #include <unistd.h>
 
 #include <binder_rpc_unstable.hpp>
-#include <chrono>
 #include <fstream>
 #include <random>
 #include <string>
 
-#include "android-base/logging.h"
-#include "android-base/parseint.h"
-#include "android-base/strings.h"
+#include "io_vsock.h"
 
 using aidl::android::system::virtualmachineservice::IVirtualMachineService;
 using android::base::ErrnoError;
@@ -74,6 +75,20 @@
         return ndk::ScopedAStatus::ok();
     }
 
+    ndk::ScopedAStatus initVsockServer(int32_t port, int32_t* out) override {
+        auto res = io_vsock::init_vsock_server(port);
+        if (res.ok()) {
+            *out = res.value();
+        }
+        return resultStatus(res);
+    }
+
+    ndk::ScopedAStatus runVsockServerAndReceiveData(int32_t server_fd,
+                                                    int32_t num_bytes_to_receive) override {
+        auto res = io_vsock::run_vsock_server_and_receive_data(server_fd, num_bytes_to_receive);
+        return resultStatus(res);
+    }
+
 private:
     /** Returns the elapsed seconds for reading the file. */
     Result<double> read_file(const std::string& filename, int64_t fileSizeBytes, bool is_rand) {
@@ -170,12 +185,10 @@
             sleep(1000);
         }
     } else if (strcmp(argv[1], "io") == 0) {
-        if (auto res = run_io_benchmark_tests(); res.ok()) {
-            return 0;
-        } else {
+        if (auto res = run_io_benchmark_tests(); !res.ok()) {
             LOG(ERROR) << "IO benchmark test failed: " << res.error() << "\n";
-            return 1;
+            return EXIT_FAILURE;
         }
     }
-    return 0;
+    return EXIT_SUCCESS;
 }
diff --git a/tests/benchmark/src/native/include/io_vsock.h b/tests/benchmark/src/native/include/io_vsock.h
new file mode 100644
index 0000000..3cd6339
--- /dev/null
+++ b/tests/benchmark/src/native/include/io_vsock.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2022 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.
+ */
+
+#pragma once
+
+#include <android-base/result.h>
+
+namespace io_vsock {
+using android::base::Result;
+Result<int> init_vsock_server(unsigned int port);
+Result<void> run_vsock_server_and_receive_data(int server_fd, int num_bytes_to_receive);
+} // namespace io_vsock
diff --git a/tests/benchmark/src/native/io_vsock.cpp b/tests/benchmark/src/native/io_vsock.cpp
new file mode 100644
index 0000000..8edc7c8
--- /dev/null
+++ b/tests/benchmark/src/native/io_vsock.cpp
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2022 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 "io_vsock.h"
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/result.h>
+#include <android-base/unique_fd.h>
+#include <linux/vm_sockets.h>
+#include <sys/socket.h>
+
+using namespace android::base;
+
+namespace io_vsock {
+Result<int> init_vsock_server(unsigned int port) {
+    int server_fd(TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0)));
+    if (server_fd < 0) {
+        return Error() << "VM:cannot create socket";
+    }
+    struct sockaddr_vm server_sa = (struct sockaddr_vm){
+            .svm_family = AF_VSOCK,
+            .svm_port = port,
+            .svm_cid = VMADDR_CID_ANY,
+    };
+    LOG(INFO) << "VM:Connecting on port " << port << "...";
+    int ret = TEMP_FAILURE_RETRY(bind(server_fd, (struct sockaddr *)&server_sa, sizeof(server_sa)));
+    if (ret < 0) {
+        return Error() << "VM:cannot bind an address with the socket";
+    }
+    ret = TEMP_FAILURE_RETRY(listen(server_fd, /*backlog=*/1));
+    if (ret < 0) {
+        return Error() << "VM:cannot listen to port";
+    }
+    LOG(INFO) << "Server now listening";
+    return server_fd;
+}
+
+Result<void> run_vsock_server_and_receive_data(int server_fd, int num_bytes_to_receive) {
+    LOG(INFO) << "Accepting connection...";
+    struct sockaddr_vm client_sa;
+    socklen_t client_sa_len = sizeof(client_sa);
+    unique_fd client_fd(TEMP_FAILURE_RETRY(
+            accept4(server_fd, (struct sockaddr *)&client_sa, &client_sa_len, SOCK_CLOEXEC)));
+    if (client_fd < 0) {
+        return Error() << "Cannot retrieve connect requests";
+    }
+    LOG(INFO) << "VM:Connection from CID " << client_sa.svm_cid << " on port "
+              << client_sa.svm_port;
+    std::string data;
+    if (!ReadFdToString(client_fd, &data)) {
+        return Error() << "Cannot get data from the host.";
+    }
+    if (data.length() != num_bytes_to_receive) {
+        return Error() << "Received data length(" << data.length() << ") is not equal to "
+                       << num_bytes_to_receive;
+    }
+    LOG(INFO) << "VM:Finished reading data.";
+    return {};
+}
+} // namespace io_vsock
\ No newline at end of file