From 41c9d8917080328e6a9333648c879efb74d8fea3 Mon Sep 17 00:00:00 2001
From: Ming Yang <yangming73@huawei.com>
Date: Wed, 2 Dec 2020 16:42:21 +0800
Subject: [PATCH 1/2] Alter architecture: move mainloop from macro_vm to
 machine_manager

1. mainloop is supported to be archived to manager, because it provides
services for devices and vms.

Signed-off-by: Ming Yang <yangming73@huawei.com>
---
 device_model/src/lib.rs                             |  2 +-
 device_model/src/micro_vm/mod.rs                    | 13 +++++++------
 device_model/src/virtio/block.rs                    |  6 ++++--
 device_model/src/virtio/console.rs                  |  3 +--
 device_model/src/virtio/net.rs                      |  6 ++++--
 device_model/src/virtio/vhost/kernel/net.rs         |  3 +--
 device_model/src/virtio/vhost/kernel/vsock.rs       |  3 +--
 machine_manager/src/lib.rs                          |  1 +
 .../micro_vm => machine_manager/src}/main_loop.rs   |  0
 src/main.rs                                         |  7 +++++--
 10 files changed, 25 insertions(+), 19 deletions(-)
 rename {device_model/src/micro_vm => machine_manager/src}/main_loop.rs (100%)

diff --git a/device_model/src/lib.rs b/device_model/src/lib.rs
index 229a4d7..06d742e 100644
--- a/device_model/src/lib.rs
+++ b/device_model/src/lib.rs
@@ -46,7 +46,7 @@ mod mmio;
 mod virtio;
 
 pub use error_chain::*;
-pub use micro_vm::{main_loop::MainLoop, micro_syscall::register_seccomp, LightMachine};
+pub use micro_vm::{micro_syscall::register_seccomp, LightMachine};
 
 use address_space::GuestAddress;
 
diff --git a/device_model/src/micro_vm/mod.rs b/device_model/src/micro_vm/mod.rs
index 87113b4..7bf582e 100644
--- a/device_model/src/micro_vm/mod.rs
+++ b/device_model/src/micro_vm/mod.rs
@@ -32,7 +32,6 @@ extern crate boot_loader;
 extern crate machine_manager;
 extern crate util;
 
-pub mod main_loop;
 pub mod micro_syscall;
 
 use std::marker::{Send, Sync};
@@ -52,14 +51,17 @@ use vmm_sys_util::terminal::Terminal;
 use address_space::KvmIoListener;
 use address_space::{create_host_mmaps, AddressSpace, GuestAddress, KvmMemoryListener, Region};
 use boot_loader::{load_kernel, BootLoaderConfig};
-use machine_manager::config::{
-    BootSource, ConsoleConfig, DriveConfig, NetworkInterfaceConfig, SerialConfig, VmConfig,
-    VsockConfig,
-};
 use machine_manager::machine::{
     DeviceInterface, KvmVmState, MachineAddressInterface, MachineExternalInterface,
     MachineInterface, MachineLifecycle,
 };
+use machine_manager::{
+    config::{
+        BootSource, ConsoleConfig, DriveConfig, NetworkInterfaceConfig, SerialConfig, VmConfig,
+        VsockConfig,
+    },
+    main_loop::MainLoop,
+};
 #[cfg(feature = "qmp")]
 use machine_manager::{qmp, qmp::qmp_schema as schema, qmp::QmpChannel};
 #[cfg(target_arch = "aarch64")]
@@ -78,7 +80,6 @@ use crate::interrupt_controller::{InterruptController, InterruptControllerConfig
 use crate::legacy::PL031;
 #[cfg(target_arch = "aarch64")]
 use crate::mmio::DeviceResource;
-use crate::MainLoop;
 use crate::{
     legacy::Serial,
     mmio::{Bus, DeviceType, VirtioMmioDevice},
diff --git a/device_model/src/virtio/block.rs b/device_model/src/virtio/block.rs
index 7201dd5..bdcb691 100644
--- a/device_model/src/virtio/block.rs
+++ b/device_model/src/virtio/block.rs
@@ -22,7 +22,10 @@ use std::sync::mpsc::{channel, Receiver, Sender};
 use std::sync::{Arc, Mutex};
 
 use address_space::{AddressSpace, GuestAddress};
-use machine_manager::config::{ConfigCheck, DriveConfig};
+use machine_manager::{
+    config::{ConfigCheck, DriveConfig},
+    main_loop::MainLoop,
+};
 use util::aio::{Aio, AioCb, AioCompleteFunc, IoCmd, Iovec};
 use util::byte_code::ByteCode;
 use util::epoll_context::{
@@ -31,7 +34,6 @@ use util::epoll_context::{
 use util::num_ops::{read_u32, write_u32};
 use vmm_sys_util::{epoll::EventSet, eventfd::EventFd};
 
-use super::super::micro_vm::main_loop::MainLoop;
 use super::errors::{ErrorKind, Result, ResultExt};
 use super::{
     Element, Queue, VirtioDevice, VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_SEG_MAX,
diff --git a/device_model/src/virtio/console.rs b/device_model/src/virtio/console.rs
index def783b..2fb601a 100644
--- a/device_model/src/virtio/console.rs
+++ b/device_model/src/virtio/console.rs
@@ -18,7 +18,7 @@ use std::sync::atomic::{AtomicU32, Ordering};
 use std::sync::{Arc, Mutex};
 
 use address_space::AddressSpace;
-use machine_manager::config::ConsoleConfig;
+use machine_manager::{config::ConsoleConfig, main_loop::MainLoop};
 use util::byte_code::ByteCode;
 use util::epoll_context::{read_fd, EventNotifier, EventNotifierHelper, NotifierOperation};
 use util::num_ops::{read_u32, write_u32};
@@ -26,7 +26,6 @@ use util::unix::limit_permission;
 use vmm_sys_util::epoll::EventSet;
 use vmm_sys_util::eventfd::EventFd;
 
-use super::super::micro_vm::main_loop::MainLoop;
 use super::errors::{ErrorKind, Result, ResultExt};
 use super::{
     Queue, VirtioDevice, VIRTIO_CONSOLE_F_SIZE, VIRTIO_F_VERSION_1, VIRTIO_MMIO_INT_VRING,
diff --git a/device_model/src/virtio/net.rs b/device_model/src/virtio/net.rs
index 3d6dcc3..788b94b 100644
--- a/device_model/src/virtio/net.rs
+++ b/device_model/src/virtio/net.rs
@@ -18,7 +18,10 @@ use std::sync::{Arc, Mutex};
 use std::{cmp, mem};
 
 use address_space::AddressSpace;
-use machine_manager::config::{ConfigCheck, NetworkInterfaceConfig};
+use machine_manager::{
+    config::{ConfigCheck, NetworkInterfaceConfig},
+    main_loop::MainLoop,
+};
 use util::byte_code::ByteCode;
 use util::epoll_context::{
     read_fd, EventNotifier, EventNotifierHelper, NotifierCallback, NotifierOperation,
@@ -27,7 +30,6 @@ use util::num_ops::{read_u32, write_u32};
 use util::tap::{Tap, TUN_F_VIRTIO};
 use vmm_sys_util::{epoll::EventSet, eventfd::EventFd};
 
-use super::super::micro_vm::main_loop::MainLoop;
 use super::errors::{ErrorKind, Result, ResultExt};
 use super::{
     Queue, VirtioDevice, VirtioNetHdr, VIRTIO_F_VERSION_1, VIRTIO_MMIO_INT_VRING,
diff --git a/device_model/src/virtio/vhost/kernel/net.rs b/device_model/src/virtio/vhost/kernel/net.rs
index 5a51d54..b7839c9 100644
--- a/device_model/src/virtio/vhost/kernel/net.rs
+++ b/device_model/src/virtio/vhost/kernel/net.rs
@@ -18,7 +18,7 @@ use std::sync::atomic::AtomicU32;
 use std::sync::{Arc, Mutex};
 
 use address_space::AddressSpace;
-use machine_manager::config::NetworkInterfaceConfig;
+use machine_manager::{config::NetworkInterfaceConfig, main_loop::MainLoop};
 use util::byte_code::ByteCode;
 use util::epoll_context::EventNotifierHelper;
 use util::num_ops::{read_u32, write_u32};
@@ -26,7 +26,6 @@ use util::tap::Tap;
 use vmm_sys_util::eventfd::EventFd;
 use vmm_sys_util::ioctl::ioctl_with_ref;
 
-use super::super::super::super::micro_vm::main_loop::MainLoop;
 use super::super::super::errors::{ErrorKind, Result, ResultExt};
 use super::super::super::{
     net::{build_device_config_space, create_tap, VirtioNetConfig},
diff --git a/device_model/src/virtio/vhost/kernel/vsock.rs b/device_model/src/virtio/vhost/kernel/vsock.rs
index bf23f04..08b091f 100644
--- a/device_model/src/virtio/vhost/kernel/vsock.rs
+++ b/device_model/src/virtio/vhost/kernel/vsock.rs
@@ -16,13 +16,12 @@ use std::sync::{Arc, Mutex};
 
 use address_space::AddressSpace;
 use byteorder::{ByteOrder, LittleEndian};
-use machine_manager::config::VsockConfig;
+use machine_manager::{config::VsockConfig, main_loop::MainLoop};
 use util::epoll_context::EventNotifierHelper;
 use util::num_ops::{read_u32, write_u32};
 use vmm_sys_util::eventfd::EventFd;
 use vmm_sys_util::ioctl::ioctl_with_ref;
 
-use super::super::super::super::micro_vm::main_loop::MainLoop;
 use super::super::super::errors::{ErrorKind, Result, ResultExt};
 use super::super::super::{Queue, VirtioDevice, VIRTIO_TYPE_VSOCK};
 use super::super::{VhostNotify, VhostOps};
diff --git a/machine_manager/src/lib.rs b/machine_manager/src/lib.rs
index 0d8d91a..43c0c00 100644
--- a/machine_manager/src/lib.rs
+++ b/machine_manager/src/lib.rs
@@ -30,6 +30,7 @@ extern crate serde_json;
 pub mod cmdline;
 pub mod config;
 pub mod machine;
+pub mod main_loop;
 #[cfg(feature = "qmp")]
 pub mod qmp;
 pub mod socket;
diff --git a/device_model/src/micro_vm/main_loop.rs b/machine_manager/src/main_loop.rs
similarity index 100%
rename from device_model/src/micro_vm/main_loop.rs
rename to machine_manager/src/main_loop.rs
diff --git a/src/main.rs b/src/main.rs
index 0ef51f3..c0000bd 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -22,12 +22,15 @@ use std::sync::{Arc, Mutex};
 
 use vmm_sys_util::terminal::Terminal;
 
-use device_model::{register_seccomp, LightMachine, MainLoop};
-use machine_manager::cmdline::{check_api_channel, create_args_parser, create_vmconfig};
+use device_model::{register_seccomp, LightMachine};
 use machine_manager::config::VmConfig;
 #[cfg(feature = "qmp")]
 use machine_manager::qmp::QmpChannel;
 use machine_manager::socket::Socket;
+use machine_manager::{
+    cmdline::{check_api_channel, create_args_parser, create_vmconfig},
+    main_loop::MainLoop,
+};
 use util::epoll_context::EventNotifierHelper;
 use util::unix::limit_permission;
 use util::{arg_parser, daemonize::daemonize, logger};
-- 
2.27.0

