From efd766e4f6509a71dc88162251d17381dba22a88 Mon Sep 17 00:00:00 2001 From: Haibo Huang Date: Tue, 9 Feb 2021 23:59:55 -0800 Subject: Upgrade rust/crates/vsock to 0.2.3 Test: make Change-Id: I3dcb8b2d7eced30b99d792c42bc7ca3a375ed1d7 --- .cargo_vcs_info.json | 2 +- .github/workflows/bvt.yaml | 31 +++++++++++++++++ .github/workflows/commit-message-check.yaml | 53 +++++++++++++++++++++++++++++ .gitignore | 1 + Android.bp | 7 ++-- Cargo.toml | 14 ++++---- Cargo.toml.orig | 14 ++++---- METADATA | 10 +++--- Makefile | 19 ++++++++--- src/lib.rs | 25 +++++++++++--- tests/vsock.rs | 3 +- 11 files changed, 147 insertions(+), 32 deletions(-) create mode 100644 .github/workflows/bvt.yaml create mode 100644 .github/workflows/commit-message-check.yaml diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 6b5c25e..c3a6da8 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "7b815b3f50782ea682da1939f208d5dd8cac17bd" + "sha1": "c24bde75d75afb29563cc465f4c3e2cedfea69b9" } } diff --git a/.github/workflows/bvt.yaml b/.github/workflows/bvt.yaml new file mode 100644 index 0000000..78906ec --- /dev/null +++ b/.github/workflows/bvt.yaml @@ -0,0 +1,31 @@ +name: BVT +on: [pull_request] +jobs: + bvt: + name: BVT + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: install dependencies + run: | + rustup target add x86_64-unknown-linux-musl + rustup component add rustfmt clippy + - name: Format Check + run: make fmt + - name: Clippy Check + run: make clippy + - name: Build + run: make vsock + - name: Build echo_server + run: make echo_server + + ## Unfortunately GitHub Acions doesn't support nested virtualization at the moment, + ## And the vsock have bugs on qemu + tcg, so we can't run the unit test for now. + # - name: Install QEMU + # run: | + # sudo apt-get install qemu-system-x86 -y + # sudo modprobe vhost_vsock + # - name: Run Unit Test + # run: | + # make vm-for-action + # make check diff --git a/.github/workflows/commit-message-check.yaml b/.github/workflows/commit-message-check.yaml new file mode 100644 index 0000000..4d1c57e --- /dev/null +++ b/.github/workflows/commit-message-check.yaml @@ -0,0 +1,53 @@ +name: Commit Message Check +on: + pull_request: + types: + - opened + - reopened + - synchronize + +env: + error_msg: |+ + See the document below for help on formatting commits for the project. + + https://github.com/kata-containers/community/blob/master/CONTRIBUTING.md#patch-forma + +jobs: + commit-message-check: + runs-on: ubuntu-latest + name: Commit Message Check + steps: + - name: Get PR Commits + id: 'get-pr-commits' + uses: tim-actions/get-pr-commits@v1.0.0 + with: + token: ${{ secrets.GITHUB_TOKEN }} + + - name: DCO Check + uses: tim-actions/dco@2fd0504dc0d27b33f542867c300c60840c6dcb20 + with: + commits: ${{ steps.get-pr-commits.outputs.commits }} + + - name: Commit Body Missing Check + if: ${{ success() || failure() }} + uses: tim-actions/commit-body-check@v1.0.2 + with: + commits: ${{ steps.get-pr-commits.outputs.commits }} + + - name: Check Subject Line Length + if: ${{ success() || failure() }} + uses: tim-actions/commit-message-checker-with-regex@v0.3.1 + with: + commits: ${{ steps.get-pr-commits.outputs.commits }} + pattern: '^.{0,75}(\n.*)*$' + error: 'Subject too long (max 75)' + post_error: ${{ env.error_msg }} + + - name: Check Body Line Length + if: ${{ success() || failure() }} + uses: tim-actions/commit-message-checker-with-regex@v0.3.1 + with: + commits: ${{ steps.get-pr-commits.outputs.commits }} + pattern: '^.+(\n.{0,72})*$|^.+\n\s*[^a-zA-Z\s\n]|^.+\n\S+$' + error: 'Body line too long (max 72)' + post_error: ${{ env.error_msg }} diff --git a/.gitignore b/.gitignore index 6678b89..29db07b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ target .idea/ *.iml Cargo.lock +echo_server/Cargo.lock diff --git a/Android.bp b/Android.bp index f0e343a..dcb59a2 100644 --- a/Android.bp +++ b/Android.bp @@ -14,7 +14,6 @@ rust_library { // dependent_library ["feature_list"] // bitflags-1.2.1 "default" -// cfg-if-0.1.10 -// libc-0.2.80 "default,extra_traits,std" -// nix-0.17.0 -// void-1.0.2 "default,std" +// cfg-if-1.0.0 +// libc-0.2.86 "default,extra_traits,std" +// nix-0.19.1 diff --git a/Cargo.toml b/Cargo.toml index 3d14676..c4b49fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,19 +13,21 @@ [package] edition = "2018" name = "vsock" -version = "0.2.1" -authors = ["fsyncd"] +version = "0.2.3" +authors = ["fsyncd", "rust-vsock"] exclude = ["test_fixture"] description = "Virtio socket support for Rust" +homepage = "https://github.com/rust-vsock/vsock-rs" +readme = "README.md" license = "Apache-2.0" -repository = "https://github.com/fsyncd/vsock-rs" +repository = "https://github.com/rust-vsock/vsock-rs" [dependencies.libc] -version = "0.2.65" +version = "0.2.79" [dependencies.nix] -version = "0.17.0" +version = "0.19.1" [dev-dependencies.rand] -version = "0.7.2" +version = "0.8.3" [dev-dependencies.sha2] version = ">=0.8.0" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index d6eaa17..afe8103 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,17 +1,19 @@ [package] name = "vsock" -version = "0.2.1" -authors = ["fsyncd"] +version = "0.2.3" +authors = ["fsyncd", "rust-vsock"] description = "Virtio socket support for Rust" -repository = "https://github.com/fsyncd/vsock-rs" +repository = "https://github.com/rust-vsock/vsock-rs" +homepage = "https://github.com/rust-vsock/vsock-rs" +readme = "README.md" license = "Apache-2.0" edition = "2018" exclude = ["test_fixture"] [dependencies] -libc = "0.2.65" -nix = "0.17.0" +libc = "0.2.79" +nix = "0.19.1" [dev-dependencies] -rand = "0.7.2" +rand = "0.8.3" sha2 = ">=0.8.0" diff --git a/METADATA b/METADATA index 4e64bdb..1f3b1c3 100644 --- a/METADATA +++ b/METADATA @@ -7,13 +7,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/vsock/vsock-0.2.1.crate" + value: "https://static.crates.io/crates/vsock/vsock-0.2.3.crate" } - version: "0.2.1" + version: "0.2.3" license_type: NOTICE last_upgrade_date { - year: 2020 - month: 9 - day: 28 + year: 2021 + month: 2 + day: 9 } } diff --git a/Makefile b/Makefile index cbaf471..da5a673 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ # 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. - + TOOLCHAIN := x86_64-unknown-linux-musl VPATH = target/system target/$(TOOLCHAIN)/debug target/$(TOOLCHAIN)/release @@ -23,10 +23,16 @@ ID := $(shell date +%s) all: vsock echo_server -check: vsock echo_server +check: vsock echo_server test + +test: + cargo test --all + +fmt: cargo fmt --all -- --check + +clippy: cargo clippy --all-targets --all-features -- -D warnings - cargo test --all clean: cargo clean @@ -49,6 +55,11 @@ vm: initrd.cpio sudo qemu-system-x86_64 -kernel test_fixture/bzImage -initrd target/$(TOOLCHAIN)/debug/initrd.cpio \ -enable-kvm -m 256 -device vhost-vsock-pci,id=vhost-vsock-pci0,guest-cid=3 -nographic -append "console=ttyS0" +# Start a virtio socket enabled vm in background +vm-for-action: initrd.cpio + sudo qemu-system-x86_64 -kernel test_fixture/bzImage -initrd target/$(TOOLCHAIN)/debug/initrd.cpio \ + -m 256 -device vhost-vsock-pci,id=vhost-vsock-pci0,guest-cid=3 -display none -daemonize -append "console=ttyS0" + # Create a simple operating system image for the vm initrd.cpio: echo_server -rm -f target/$(TOOLCHAIN)/debug/initrd.cpio @@ -58,4 +69,4 @@ initrd.cpio: echo_server cp echo_server/target/$(TOOLCHAIN)/debug/echo_server /tmp/$(ID)/ (cd '/tmp/$(ID)' && find . | grep -v 'initrd.cpio' | cpio -H newc -o --append -F initrd.cpio) mv /tmp/$(ID)/initrd.cpio target/$(TOOLCHAIN)/debug/ - rm -Rf /tmp/$(ID) \ No newline at end of file + rm -Rf /tmp/$(ID) diff --git a/src/lib.rs b/src/lib.rs index 97055e7..82d5092 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,15 +18,16 @@ //! Virtio socket support for Rust. use std::io::{Error, ErrorKind, Read, Result, Write}; -use std::mem::size_of; +use std::mem::{self, size_of}; use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; use libc::*; -use nix::sys::socket::{SockAddr, VsockAddr}; use std::ffi::c_void; use std::net::Shutdown; use std::time::Duration; +pub use nix::sys::socket::{SockAddr, VsockAddr}; + fn new_socket() -> libc::c_int { unsafe { socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0) } } @@ -88,6 +89,11 @@ impl VsockListener { Ok(Self { socket }) } + /// Create a new VsockListener with specified cid and port. + pub fn bind_with_cid_port(cid: u32, port: u32) -> Result { + Self::bind(&SockAddr::Vsock(VsockAddr::new(cid, port))) + } + /// The local socket address of the listener. pub fn local_addr(&self) -> Result { let mut vsock_addr = sockaddr_vm { @@ -198,7 +204,9 @@ impl FromRawFd for VsockListener { impl IntoRawFd for VsockListener { fn into_raw_fd(self) -> RawFd { - self.socket + let fd = self.socket; + mem::forget(self); + fd } } @@ -244,6 +252,11 @@ impl VsockStream { } } + /// Open a connection to a remote host with specified cid and port. + pub fn connect_with_cid_port(cid: u32, port: u32) -> Result { + Self::connect(&SockAddr::Vsock(VsockAddr::new(cid, port))) + } + /// Virtio socket address of the remote peer associated with this connection. pub fn peer_addr(&self) -> Result { let mut vsock_addr = sockaddr_vm { @@ -393,6 +406,8 @@ impl VsockStream { )); } + // https://github.com/rust-lang/libc/issues/1848 + #[cfg_attr(target_env = "musl", allow(deprecated))] let secs = if dur.as_secs() > time_t::max_value() as u64 { time_t::max_value() } else { @@ -478,7 +493,9 @@ impl FromRawFd for VsockStream { impl IntoRawFd for VsockStream { fn into_raw_fd(self) -> RawFd { - self.socket + let fd = self.socket; + mem::forget(self); + fd } } diff --git a/tests/vsock.rs b/tests/vsock.rs index 24f4e37..d51bad0 100644 --- a/tests/vsock.rs +++ b/tests/vsock.rs @@ -14,11 +14,10 @@ * limitations under the License. */ -use nix::sys::socket::{SockAddr, VsockAddr}; use rand::RngCore; use sha2::{Digest, Sha256}; use std::io::{Read, Write}; -use vsock::VsockStream; +use vsock::{SockAddr, VsockAddr, VsockStream}; const TEST_BLOB_SIZE: usize = 1_000_000; const TEST_BLOCK_SIZE: usize = 5_000; -- cgit v1.2.3