Merge "Define skeleton code of vmnic(Virtual Machine Network Interface Creator)" into main
diff --git a/Android.bp b/Android.bp
index dcf67dd..3b6b8b5 100644
--- a/Android.bp
+++ b/Android.bp
@@ -27,6 +27,7 @@
         "release_avf_enable_dice_changes",
         "release_avf_enable_llpvm_changes",
         "release_avf_enable_multi_tenant_microdroid_vm",
+        "release_avf_enable_network",
         "release_avf_enable_remote_attestation",
         "release_avf_enable_vendor_modules",
         "release_avf_enable_virt_cpufreq",
@@ -52,6 +53,9 @@
         release_avf_enable_multi_tenant_microdroid_vm: {
             cfgs: ["multi_tenant"],
         },
+        release_avf_enable_network: {
+            cfgs: ["network"],
+        },
         release_avf_enable_remote_attestation: {
             cfgs: ["remote_attestation"],
         },
diff --git a/apex/Android.bp b/apex/Android.bp
index 48b7b1f..0eb8b9e 100644
--- a/apex/Android.bp
+++ b/apex/Android.bp
@@ -46,6 +46,7 @@
     bool_variables: [
         "release_avf_enable_device_assignment",
         "release_avf_enable_llpvm_changes",
+        "release_avf_enable_network",
         "release_avf_enable_remote_attestation",
         "release_avf_enable_vendor_modules",
         "release_avf_enable_virt_cpufreq",
@@ -190,6 +191,16 @@
         release_avf_enable_llpvm_changes: {
             androidManifest: "AndroidManifest.xml",
         },
+        release_avf_enable_network: {
+            arch: {
+                arm64: {
+                    binaries: ["vmnic"],
+                },
+                x86_64: {
+                    binaries: ["vmnic"],
+                },
+            },
+        },
         release_avf_enable_remote_attestation: {
             vintf_fragments: [
                 "virtualizationservice.xml",
diff --git a/virtualizationservice/aidl/android/system/virtualizationservice_internal/IVmnic.aidl b/virtualizationservice/aidl/android/system/virtualizationservice_internal/IVmnic.aidl
new file mode 100644
index 0000000..3796763
--- /dev/null
+++ b/virtualizationservice/aidl/android/system/virtualizationservice_internal/IVmnic.aidl
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2024 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.system.virtualizationservice_internal;
+
+interface IVmnic {
+    /**
+     * Create TAP network interface for a VM.
+     * @param CID of VM.
+     * @return file descriptor of the TAP network interface.
+     */
+    ParcelFileDescriptor createTapInterface(int cid);
+}
diff --git a/virtualizationservice/vmnic/Android.bp b/virtualizationservice/vmnic/Android.bp
new file mode 100644
index 0000000..4313a82
--- /dev/null
+++ b/virtualizationservice/vmnic/Android.bp
@@ -0,0 +1,21 @@
+package {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+rust_binary {
+    name: "vmnic",
+    crate_name: "vmnic",
+    defaults: ["avf_build_flags_rust"],
+    edition: "2021",
+    srcs: ["src/main.rs"],
+    prefer_rlib: true,
+    rustlibs: [
+        "android.system.virtualizationservice_internal-rust",
+        "libandroid_logger",
+        "libanyhow",
+        "libbinder_rs",
+        "liblog_rust",
+    ],
+    apex_available: ["com.android.virt"],
+    init_rc: ["vmnic.rc"],
+}
diff --git a/virtualizationservice/vmnic/src/aidl.rs b/virtualizationservice/vmnic/src/aidl.rs
new file mode 100644
index 0000000..26a0eff
--- /dev/null
+++ b/virtualizationservice/vmnic/src/aidl.rs
@@ -0,0 +1,37 @@
+// Copyright 2024, 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.
+
+//! Implementation of the AIDL interface of Vmnic.
+
+use anyhow::anyhow;
+use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal::IVmnic::IVmnic;
+use binder::{self, ExceptionCode, Interface, IntoBinderResult, ParcelFileDescriptor};
+
+#[derive(Debug, Default)]
+pub struct Vmnic {}
+
+impl Vmnic {
+    pub fn init() -> Vmnic {
+        Vmnic::default()
+    }
+}
+
+impl Interface for Vmnic {}
+
+impl IVmnic for Vmnic {
+    fn createTapInterface(&self, _cid: i32) -> binder::Result<ParcelFileDescriptor> {
+        Err(anyhow!("Creating TAP network interface is not supported yet"))
+            .or_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION)
+    }
+}
diff --git a/virtualizationservice/vmnic/src/main.rs b/virtualizationservice/vmnic/src/main.rs
new file mode 100644
index 0000000..8c73c40
--- /dev/null
+++ b/virtualizationservice/vmnic/src/main.rs
@@ -0,0 +1,44 @@
+// Copyright 2024 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.
+
+//! Android Vmnic (Virtual Machine Network Interface Creator)
+
+mod aidl;
+
+use crate::aidl::Vmnic;
+use android_logger::Config;
+use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal::IVmnic::{
+    BnVmnic,
+    BpVmnic,
+    IVmnic,
+};
+use binder::{register_lazy_service, BinderFeatures, ProcessState};
+use log::{info, LevelFilter};
+
+const LOG_TAG: &str = "Vmnic";
+
+fn main() {
+    android_logger::init_once(
+        Config::default()
+            .with_tag(LOG_TAG)
+            .with_max_level(LevelFilter::Info)
+            .with_log_buffer(android_logger::LogId::System),
+    );
+
+    let service = Vmnic::init();
+    let service = BnVmnic::new_binder(service, BinderFeatures::default());
+    register_lazy_service(<BpVmnic as IVmnic>::get_descriptor(), service.as_binder()).unwrap();
+    info!("Registered Binder service, joining threadpool.");
+    ProcessState::join_thread_pool();
+}
diff --git a/virtualizationservice/vmnic/vmnic.rc b/virtualizationservice/vmnic/vmnic.rc
new file mode 100644
index 0000000..486f387
--- /dev/null
+++ b/virtualizationservice/vmnic/vmnic.rc
@@ -0,0 +1,20 @@
+# Copyright (C) 2024 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.
+
+service vmnic /apex/com.android.virt/bin/vmnic
+    user system
+    group system
+    interface aidl android.system.virtualizationservice_internal.IVmnic
+    disabled
+    oneshot