diff --git a/vmm/task/Cargo.lock b/vmm/task/Cargo.lock index a6c5ca6b..9a136500 100644 --- a/vmm/task/Cargo.lock +++ b/vmm/task/Cargo.lock @@ -93,7 +93,7 @@ checksum = "acee9fd5073ab6b045a275b3e709c163dd36c90685219cb21804a147b58dba43" dependencies = [ "async-trait", "axum-core", - "bitflags", + "bitflags 1.3.2", "bytes 1.4.0", "futures-util", "http", @@ -157,6 +157,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + [[package]] name = "byteorder" version = "1.4.3" @@ -197,6 +203,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cfg_aliases" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" + [[package]] name = "cgroups-rs" version = "0.2.11" @@ -231,7 +243,7 @@ dependencies = [ [[package]] name = "containerd-sandbox" version = "0.1.0" -source = "git+https://github.com/kuasar-io/rust-extensions.git#5f2edcffe9dc4fe41f1f4d9a14843456d0a835ee" +source = "git+https://github.com/kuasar-io/rust-extensions.git#b9ad8e197385b72ada6c1b8482c155606d26c803" dependencies = [ "anyhow", "async-stream", @@ -259,7 +271,7 @@ dependencies = [ [[package]] name = "containerd-shim" version = "0.3.0" -source = "git+https://github.com/kuasar-io/rust-extensions.git#5f2edcffe9dc4fe41f1f4d9a14843456d0a835ee" +source = "git+https://github.com/kuasar-io/rust-extensions.git#b9ad8e197385b72ada6c1b8482c155606d26c803" dependencies = [ "async-trait", "cgroups-rs", @@ -270,7 +282,7 @@ dependencies = [ "lazy_static", "libc", "log", - "nix 0.25.1", + "nix 0.28.0", "oci-spec", "page_size", "pin-project-lite", @@ -290,7 +302,7 @@ dependencies = [ [[package]] name = "containerd-shim-protos" version = "0.2.0" -source = "git+https://github.com/kuasar-io/rust-extensions.git#5f2edcffe9dc4fe41f1f4d9a14843456d0a835ee" +source = "git+https://github.com/kuasar-io/rust-extensions.git#b9ad8e197385b72ada6c1b8482c155606d26c803" dependencies = [ "async-trait", "protobuf 3.2.0", @@ -924,6 +936,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + [[package]] name = "mime" version = "0.3.17" @@ -975,7 +996,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea993e32c77d87f01236c38f572ecb6c311d592e56a06262a007fd2a6e31253c" dependencies = [ "anyhow", - "bitflags", + "bitflags 1.3.2", "byteorder", "libc", "netlink-packet-core", @@ -1040,7 +1061,7 @@ version = "0.22.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4916f159ed8e5de0082076562152a76b7a1f64a01fd9d1e0fea002c37624faf" dependencies = [ - "bitflags", + "bitflags 1.3.2", "cc", "cfg-if 1.0.0", "libc", @@ -1053,7 +1074,7 @@ version = "0.23.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f3790c00a0150112de0f4cd161e3d7fc4b2d8a5542ffc35f099a2562aecb35c" dependencies = [ - "bitflags", + "bitflags 1.3.2", "cc", "cfg-if 1.0.0", "libc", @@ -1066,7 +1087,7 @@ version = "0.24.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fa52e972a9a719cecb6864fb88568781eb706bac2cd1d4f04a648542dbf78069" dependencies = [ - "bitflags", + "bitflags 1.3.2", "cfg-if 1.0.0", "libc", "memoffset 0.6.5", @@ -1079,7 +1100,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f346ff70e7dbfd675fe90590b92d59ef2de15a8779ae305ebcbfd3f0caf59be4" dependencies = [ "autocfg", - "bitflags", + "bitflags 1.3.2", "cfg-if 1.0.0", "libc", "memoffset 0.6.5", @@ -1092,7 +1113,7 @@ version = "0.26.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfdda3d196821d6af13126e40375cdf7da646a96114af134d5f417a9a1dc8e1a" dependencies = [ - "bitflags", + "bitflags 1.3.2", "cfg-if 1.0.0", "libc", "memoffset 0.7.1", @@ -1100,6 +1121,19 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "nix" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4" +dependencies = [ + "bitflags 2.6.0", + "cfg-if 1.0.0", + "cfg_aliases", + "libc", + "memoffset 0.9.1", +] + [[package]] name = "num_cpus" version = "1.15.0" @@ -1548,7 +1582,7 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" dependencies = [ - "bitflags", + "bitflags 1.3.2", ] [[package]] @@ -1589,7 +1623,7 @@ dependencies = [ [[package]] name = "runc" version = "0.2.0" -source = "git+https://github.com/kuasar-io/rust-extensions.git#5f2edcffe9dc4fe41f1f4d9a14843456d0a835ee" +source = "git+https://github.com/kuasar-io/rust-extensions.git#b9ad8e197385b72ada6c1b8482c155606d26c803" dependencies = [ "async-trait", "futures", @@ -1622,7 +1656,7 @@ version = "0.37.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acf8729d8542766f1b2cf77eb034d52f40d375bb8b615d0b147089946e16613d" dependencies = [ - "bitflags", + "bitflags 1.3.2", "errno", "io-lifetimes", "libc", @@ -1982,7 +2016,7 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f873044bf02dd1e8239e9c1293ea39dad76dc594ec16185d0a1bf31d8dc8d858" dependencies = [ - "bitflags", + "bitflags 1.3.2", "bytes 1.4.0", "futures-core", "futures-util", @@ -2163,7 +2197,7 @@ dependencies = [ "netlink-packet-core", "netlink-packet-route", "netlink-sys 0.7.0", - "nix 0.25.1", + "nix 0.28.0", "oci-spec", "pin-project-lite", "rtnetlink", diff --git a/vmm/task/Cargo.toml b/vmm/task/Cargo.toml index 85fc71a4..cc21ba43 100644 --- a/vmm/task/Cargo.toml +++ b/vmm/task/Cargo.toml @@ -8,9 +8,9 @@ edition = "2021" panic = 'abort' [dependencies] -vmm-common = {path = "../common"} +vmm-common = { path = "../common" } log = "0.4" -nix = "0.25" +nix = { version = "0.28.0", features = ["sched", "term", "time", "hostname", "signal", "mount", "uio", "socket"] } libc = "0.2.95" time = { version = "=0.3.7", features = ["serde", "std"] } serde = { version = "1.0.133", features = ["derive"] } @@ -19,7 +19,7 @@ oci-spec = "0.5.4" crossbeam = "0.8.1" env_logger = "0.9.0" lazy_static = "1.4.0" -netlink-sys = { version = "0.7.0", features = ["tokio_socket"]} +netlink-sys = { version = "0.7.0", features = ["tokio_socket"] } rtnetlink = "0.12" netlink-packet-route = "0.15" netlink-packet-core = "0.5.0" @@ -27,13 +27,13 @@ ipnetwork = "0.20" anyhow = { version = "1.0.66", default-features = false, features = ["std", "backtrace"] } # Async dependencies -async-trait = { version = "0.1.51"} -tokio = { version = "1.17.0", features = ["full"]} -futures = { version = "0.3.21"} -signal-hook-tokio = {version = "0.3.1", features = ["futures-v0_3"]} +async-trait = { version = "0.1.51" } +tokio = { version = "1.17.0", features = ["full"] } +futures = { version = "0.3.21" } +signal-hook-tokio = { version = "0.3.1", features = ["futures-v0_3"] } tokio-vsock = "0.3.1" pin-project-lite = "0.2.7" ttrpc = { version = "0.7", features = ["async"] } -containerd-shim = { git="https://github.com/kuasar-io/rust-extensions.git", features=["async"] } -runc = { git="https://github.com/kuasar-io/rust-extensions.git", features=["async"] } +containerd-shim = { git = "https://github.com/kuasar-io/rust-extensions.git", features = ["async"] } +runc = { git = "https://github.com/kuasar-io/rust-extensions.git", features = ["async"] } diff --git a/vmm/task/src/debug.rs b/vmm/task/src/debug.rs index 649e95b5..965b5233 100644 --- a/vmm/task/src/debug.rs +++ b/vmm/task/src/debug.rs @@ -14,7 +14,10 @@ See the License for the specific language governing permissions and limitations under the License. */ -use std::{os::unix::prelude::FromRawFd, process::Stdio}; +use std::{ + os::{fd::IntoRawFd, unix::prelude::FromRawFd}, + process::Stdio, +}; use containerd_shim::{ io_error, @@ -47,9 +50,10 @@ pub async fn debug_console(stream: VsockStream) -> Result<()> { let pty = openpty(None, None)?; let pty_master = pty.master; let mut cmd = Command::new("/bin/bash"); - cmd.stdin(unsafe { Stdio::from_raw_fd(pty.slave) }); - cmd.stdout(unsafe { Stdio::from_raw_fd(pty.slave) }); - cmd.stderr(unsafe { Stdio::from_raw_fd(pty.slave) }); + let pty_fd = pty.slave.into_raw_fd(); + cmd.stdin(unsafe { Stdio::from_raw_fd(pty_fd) }); + cmd.stdout(unsafe { Stdio::from_raw_fd(pty_fd) }); + cmd.stderr(unsafe { Stdio::from_raw_fd(pty_fd) }); unsafe { cmd.pre_exec(move || { setsid()?; diff --git a/vmm/task/src/io.rs b/vmm/task/src/io.rs index 6401d994..3fb4a246 100644 --- a/vmm/task/src/io.rs +++ b/vmm/task/src/io.rs @@ -17,7 +17,10 @@ use std::{ io::{ErrorKind, IoSliceMut}, ops::Deref, - os::unix::prelude::{AsRawFd, FromRawFd, RawFd}, + os::{ + fd::{IntoRawFd, OwnedFd}, + unix::prelude::{AsRawFd, FromRawFd, RawFd}, + }, path::PathBuf, pin::Pin, sync::Arc, @@ -145,8 +148,8 @@ async fn copy_console

( ) -> Result { debug!("copy_console: waiting for runtime to send console fd"); let stream = console_socket.accept().await?; - let fd = asyncify(move || -> Result { receive_socket(stream.as_raw_fd()) }).await?; - let f = unsafe { File::from_raw_fd(fd) }; + let fd = asyncify(move || -> Result { receive_socket(stream.as_raw_fd()) }).await?; + let f = unsafe { File::from_raw_fd(fd.into_raw_fd()) }; if !stdio.stdin.is_empty() { debug!("copy_console: pipe stdin to console"); @@ -356,7 +359,7 @@ where } } -pub fn receive_socket(stream_fd: RawFd) -> containerd_shim::Result { +pub fn receive_socket(stream_fd: RawFd) -> Result { let mut buf = [0u8; 4096]; let mut iovec = [IoSliceMut::new(&mut buf)]; let mut space = cmsg_space!([RawFd; 2]); @@ -386,8 +389,9 @@ pub fn receive_socket(stream_fd: RawFd) -> containerd_shim::Result { "copy_console: console socket get path: {}, fd: {}", path, &fds[0] ); - tcgetattr(fds[0])?; - Ok(fds[0]) + let fd = unsafe { OwnedFd::from_raw_fd(fds[0]) }; + tcgetattr(&fd)?; + Ok(fd) } // TODO we still have to create pipes, otherwise the device maybe opened multiple times in container, diff --git a/vmm/task/src/main.rs b/vmm/task/src/main.rs index e7180060..0df94326 100644 --- a/vmm/task/src/main.rs +++ b/vmm/task/src/main.rs @@ -140,7 +140,7 @@ lazy_static! { ]); } -async fn initialize() -> anyhow::Result<()> { +async fn start_task_server() -> anyhow::Result<()> { early_init_call().await?; let config = TaskConfig::new().await?; @@ -172,26 +172,16 @@ async fn initialize() -> anyhow::Result<()> { late_init_call().await?; + start_ttrpc_server().await?.start().await?; + Ok(()) } #[tokio::main] async fn main() { - if let Err(e) = initialize().await { - error!("failed to do init call: {:?}", e); - exit(-1); - } - - // Keep server alive in main function - let mut server = match create_ttrpc_server().await { - Ok(s) => s, - Err(e) => { - error!("failed to create ttrpc server: {:?}", e); - exit(-1); - } - }; - if let Err(e) = server.start().await { - error!("failed to start ttrpc server: {:?}", e); + // start task server + if let Err(e) = start_task_server().await { + error!("failed to start task server: {:?}", e); exit(-1); } @@ -362,9 +352,9 @@ async fn mount_static_mounts(mounts: Vec) -> Result<()> { Ok(()) } -// create_ttrpc_server will create all the ttrpc service and register them to a server that +// start_ttrpc_server will create all the ttrpc service and register them to a server that // bind to vsock 1024 port. -async fn create_ttrpc_server() -> anyhow::Result { +async fn start_ttrpc_server() -> anyhow::Result { let task = create_task_service().await?; let task_service = create_task(Arc::new(Box::new(task))); diff --git a/vmm/task/src/netlink.rs b/vmm/task/src/netlink.rs index dc09f9b6..5a710768 100644 --- a/vmm/task/src/netlink.rs +++ b/vmm/task/src/netlink.rs @@ -371,7 +371,7 @@ impl Handle { } if let Err(rtnetlink::Error::NetlinkError(message)) = request.execute().await { - if Errno::from_i32(message.code.abs()) != Errno::EEXIST { + if Errno::from_raw(message.code.abs()) != Errno::EEXIST { return Err(other!( "Failed to add IP v6 route (src: {}, dst: {}, gtw: {},Err: {})", route.source, @@ -418,7 +418,7 @@ impl Handle { } if let Err(rtnetlink::Error::NetlinkError(message)) = request.execute().await { - if Errno::from_i32(message.code.abs()) != Errno::EEXIST { + if Errno::from_raw(message.code.abs()) != Errno::EEXIST { return Err(other!( "Failed to add IP v4 route (src: {}, dst: {}, gtw: {},Err: {})", route.source, diff --git a/vmm/task/src/stream.rs b/vmm/task/src/stream.rs index 4233ecba..6d84ee51 100644 --- a/vmm/task/src/stream.rs +++ b/vmm/task/src/stream.rs @@ -17,7 +17,10 @@ limitations under the License. use std::{ io::{Read, Result, Write}, mem::{forget, MaybeUninit}, - os::unix::io::{AsRawFd, IntoRawFd, RawFd}, + os::{ + fd::OwnedFd, + unix::io::{AsRawFd, IntoRawFd, RawFd}, + }, pin::Pin, task::{Context, Poll}, }; @@ -29,17 +32,17 @@ use nix::{ }; use tokio::io::{unix::AsyncFd, AsyncRead, AsyncWrite, ReadBuf}; -struct DirectFd(RawFd); +struct DirectFd(OwnedFd); impl Read for &DirectFd { fn read(&mut self, buf: &mut [u8]) -> Result { - read(self.0, buf).map_err(Errno::into) + read(self.0.as_raw_fd(), buf).map_err(Errno::into) } } impl Write for &DirectFd { fn write(&mut self, buf: &[u8]) -> Result { - write(self.0, buf).map_err(Errno::into) + write(&self.0, buf).map_err(Errno::into) } fn flush(&mut self) -> Result<()> { @@ -49,7 +52,7 @@ impl Write for &DirectFd { impl DirectFd { fn close(&mut self) -> Result<()> { - close(self.0).map_err(Errno::into) + close(self.0.as_raw_fd()).map_err(Errno::into) } } @@ -61,16 +64,16 @@ impl Drop for DirectFd { impl AsRawFd for DirectFd { fn as_raw_fd(&self) -> RawFd { - self.0 + self.0.as_raw_fd() } } pub struct RawStream(AsyncFd); impl RawStream { - pub fn new(fd: RawFd) -> Result { + pub fn new(fd: OwnedFd) -> Result { unsafe { - libc::fcntl(fd, libc::F_SETFL, libc::O_NONBLOCK); + libc::fcntl(fd.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK); } Ok(Self(AsyncFd::new(DirectFd(fd))?)) }