Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vmm-sys-util #1

Merged
merged 15 commits into from
May 28, 2019
Merged

vmm-sys-util #1

merged 15 commits into from
May 28, 2019

Conversation

liujing2
Copy link
Collaborator

This includes the utilities that directly use libc from crosvm and firecracker.

Copy link
Member

@andreeaflorescu andreeaflorescu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass 1, will do another pass soonish.

src/errno.rs Outdated
// Copyright 2019 Intel Corporation. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// TODO: This module is not used now. Avoid the warning.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it shouldn't throw a warning if you are not using this module in the vmm-sys-util crate. All modules from this crate should be re-exported with pub mod in src/lib.rs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the quick reviewing! I'll fix the comments in codes.

src/lib.rs Outdated
#[macro_use]
pub mod ioctl;

mod errno;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the modules here should be re-exported with pub. These modules are not for internal use in the vmm-sys-util crate but they're going to be used by other crates.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -0,0 +1,86 @@
// Copyright 2019 Intel Corporation. All Rights Reserved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why we need this module instead of using std::io::Error?
In Firecracker we replaced it with std::io::Error because it wasn't producing human readable errors.

For example, let's say you do not have enough permissions to open /dev/kvm.

  • the error message with errno.rs: Cannot open /dev/kvm. errno: 13
  • the error message with std::io::Error: Cannot open /dev/kvm. Error: permission denied.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nice part about this error type is that it is fully compatible with os error code thrown off by system calls. If one were to put an os error code into an io::Error, it might be hard to get the os error back out in upper parts of the call stack. Using io::Error::raw_os_error returns an Option that either requires unwrap or an extra match just to extract the code. Using io::Error::kind is tricky because it has variants which are not possible to get from a syscall (UnexpectedEof), or are mismatched by the std implementation that converts error numbers to ErrorKind variants. Additionally, some error codes have no correspondence with an ErrorKind variant other than Other.

If human error messaging is all that is needed to make this module acceptable, I suggest using what we have in crosvm today for Error:

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        io::Error::from_raw_os_error(self.0).fmt(f)
    }
}

README.md Outdated
# vmm-sys-util
This crate is a collection of modules that essentially provide helpers and utilities.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: essentially is just a filler word, can you remove it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Will do it.

src/eventfd.rs Outdated

//TODO: Firecracker uses io::Error instead of sys_util::Error for Result<T, E>. Need discussion here.

#![allow(unused)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be necessary once we export the module as public in src/lib.rs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right! Will remove it

src/eventfd.rs Outdated
/// A safe wrapper around a Linux eventfd (man 2 eventfd).
///
/// An eventfd is useful because it is sendable across processes and can be used for signaling in
/// and out of the KVM API. They can also be polled like any other file descriptor.
Copy link
Member

@andreeaflorescu andreeaflorescu Mar 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit-ish: I would remove the reference to the KVM API here. It is not super relevant in the context of rust-vmm where we will also have other hypervisors. I believe the comment is a bit outdated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

src/eventfd.rs Outdated
}

impl EventFd {
/// Creates a new blocking EventFd with an initial value of 0.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this comment is accurate. We are creating the eventfd with EFD_NONBLOCK.
I noticed that in CrosVM they are opening it without the flag so maybe it would make sense to pass the additional flags as parameters.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Let me change the flag to be a parameter for new() and test the new(EFD_NONBLOCK) in #[test]. Do you think we need double test the new(0) for each #[test]?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'll look at the code. It is always nice to test all the usecases, now it depends whether it adds any value.

@andreeaflorescu
Copy link
Member

@liujing2 please also add the Travis configuration file. Here is the one from the vm-memory crate: https://github.com/rust-vmm/vm-memory/blob/master/.travis.yml

Cargo.toml Outdated
@@ -0,0 +1,9 @@
[package]
name = "libc-utils"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name should probably be vmm-sys-utils

/// A safe wrapper around a Linux timerfd (man 2 timerfd_create).
pub struct TimerFd(File);

impl TimerFd {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't check the other docs, but I think it is a good idea to have the documentation split in summary and description.

For new() it would look something like this:

/// Create a new `TimerFd`.
///
/// The timer is initially disarmed and must be armed by calling `reset()`.

Then we could also add links to the structures and methods, but I guess it is optional because it does make the code comments harder to read with the advantage of making the html docs prettier:

/// Create a new [`TimerFd`](struct.TimerFd.html).
///
/// The timer is initially disarmed and must be armed by calling [`reset`](struct.TimerFd.html#method.reset).

P.S. I haven't checked that the documentation links are correct.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me split the summary and description. The link might be optional and I found not all of the linux man is appended in original codes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't think I understand, I meant inside links to structures and functions that you are referring to in the comments.
https://rust-lang-nursery.github.io/api-guidelines/documentation.html#prose-contains-hyperlinks-to-relevant-things-c-link

Copy link
Collaborator Author

@liujing2 liujing2 Apr 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andreeaflorescu Thanks for the guide. I misunderstood. I'll update a new version today.

}

#[cfg(test)]
mod tests {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please prefix tests with test_ (test_one_shot). This is a good way to visually separate tests from helper methods.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will add the prefix for every tests in these files.

Copy link

@sboeuf sboeuf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please be more verbose in each commit message? We should be able to quickly understand what was introduced by each commit only looking at the commit messages IMO.

Cargo.lock Outdated
]

[metadata]
"checksum libc 0.2.49 (registry+https://github.com/rust-lang/crates.io-index)" = "413f3dfc802c5dc91dc570b05125b6cda9855edfaa9825c9849807876376e70e"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a discussion about whether Cargo.lock should be committed. I remember the conclusion is not to commit for library crate and only commit for binary crate.

Copy link
Collaborator Author

@liujing2 liujing2 Apr 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reviewing. Will ignore it in next update.

Cargo.toml Outdated
name = "libc-utils"
version = "0.1.0"
authors = ["Jing Liu <jing2.liu@linux.intel.com>"]
edition = "2018"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a little risky to force 2018 edition.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Remove it and add license BTW.

@@ -0,0 +1,86 @@
// Copyright 2019 Intel Corporation. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now it's suggested to dual license for Apache-2.0 and MIT. Please help to add support of MIT.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now it's suggested to dual license for Apache-2.0 and MIT. Please help to add support of MIT.

@jiangliu I just updated the codes regarding to the license and other review comments.
BTW, for the vhost utility, could you explain your request in details so I can help?

src/errno.rs Outdated
/// Returns the last `errno` as a [`Result`] that is always an error.
///
/// [`Result`]: type.Result.html
///
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove the blank lines?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think blank line is no need here. The same as others.

@liujing2
Copy link
Collaborator Author

Could you please be more verbose in each commit message? We should be able to quickly understand what was introduced by each commit only looking at the commit messages IMO.

Sure. Let me append more.

@jiangliu
Copy link
Member

jiangliu commented Apr 16, 2019 via email

@liujing2 liujing2 force-pushed the master branch 3 times, most recently from a651f7a to 8663f2e Compare April 17, 2019 03:10
@liujing2
Copy link
Collaborator Author

Codes updated with license MIT and review comments. Thanks!

.travis.yml Outdated
script:
- cargo build -- release
- cargo test
- cargo fmt --all -- --check
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check clippy result here ?

  • cargo clippy --all-targets --all-features -- -D warnings

Copy link
Collaborator Author

@liujing2 liujing2 Apr 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I just updated PR with a separate commit on top, about solving the clippy issue. Thanks.

src/errno.rs Outdated
//
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the THIRD-PARTY file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the BSD license file is missing from the crate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BSD license file is added.

jiangliu
jiangliu previously approved these changes Apr 23, 2019
sboeuf
sboeuf previously approved these changes Apr 23, 2019
Copy link

@sboeuf sboeuf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you @liujing2!

@sboeuf
Copy link

sboeuf commented Apr 23, 2019

sameo
sameo previously approved these changes May 1, 2019
Copy link
Collaborator

@sameo sameo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

LICENSE.MIT Outdated
@@ -0,0 +1,21 @@
MIT License
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What code is coming under MIT vs BSD or Apache 2.0?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems currently dual license for Apache-2.0 and MIT are supported for rust-vmm. See the comments BSD is copied from crosvm.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liujing2 I think it would be wiser to remove the MIT license for now and only keep the Apache 2.0 and the 3 clause BSD one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove MIT license.

@sameo
Copy link
Collaborator

sameo commented May 6, 2019

@liujing2 I sent you a PR for adding a few more utilities to that crate, mostly for supporting disk image format crates like e.g. QCOW.
See liujing2#1

LICENSE.BSD Outdated
@@ -0,0 +1,27 @@
// Copyright 2017 The Chromium OS Authors. All rights reserved.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we please rename that file LICENSE-BSD-3-Clause and the Apache one LICENSE-APACHE? Just to be consistent with the already existing crates?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do that.

LICENSE.MIT Outdated
@@ -0,0 +1,21 @@
MIT License
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liujing2 I think it would be wiser to remove the MIT license for now and only keep the Apache 2.0 and the 3 clause BSD one.

Jing Liu added 5 commits May 7, 2019 09:02
Add and correct basic support files for the vmm-sys-util crate including:
- files generated by Cargo
- license files
- gitignore

Signed-off-by: Jing Liu <jing2.liu@linux.intel.com>
The errno module wraps an interface to the error codes.
The eventfd module is a wraper around the Linux eventfd. Use result::Result<EventFd, io::Error>
instead of result::Result<EventFd, errno::Errno> as return type.
The terminal module is for configuring pseudo-terminals in raw mode.
The signal module is for registering signal handlers and for signalling threads.
The ioctl module is factoring out common ioctl wrappers and macros.

This is derived from crosvm code base e4ece32798ba436fbfa88466c571f46dab99e2be
and firecracker code base 1fdde1997fc763601f8a88399bc426bd5d4097cd.

Signed-off-by: Jing Liu <jing2.liu@linux.intel.com>

---
Change Log

Add license.
Append prefix test_ for each test.
Add verbose commit messages.
To use different init flag(e.g. SA_RESTART, SA_SIGINFO) for sigaction, add
a parameter to register_signal_handler and a function to set this flag.

Signed-off-by: Jing Liu <jing2.liu@linux.intel.com>
Wrapping the libc timerfd for the usage of setting timer, waiting expiring,
disarm timer.

This is derived from crosvm code base e4ece32798ba436fbfa88466c571f46dab99e2be.

Signed-off-by: Jing Liu <jing2.liu@linux.intel.com>

---
Change Log:

Add license.
Split the summary and description for method.
Add link for structures and functions.
Append prefix test_ for tests.
The poll module addis the Poller object for waiting on mutliple file
descriptors at once. Add macro handle_eintr_errno! to check the epoll_wait return value.

The syslog module is for logging, temporarily defining the SYS_getpid syscall value.

This is derived from crosvm code base e4ece32798ba436fbfa88466c571f46dab99e2be.

Signed-off-by: Jing Liu <jing2.liu@linux.intel.com>

---
Change Log
Append prefix test_ for tests.
@liujing2 liujing2 dismissed stale reviews from sameo, sboeuf, and jiangliu via 6ee1973 May 7, 2019 01:17
Samuel Ortiz and others added 3 commits May 7, 2019 07:40
This is needed for disk image format support like e.g. QCOW2.
Copied verbatim from crosvm 107edb3e.

Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
Several places are improved to suppress the clippy warnings.

The changes of type casting in timerfd module are based on crosvm latest code base
b1de6323ab8c96c52a60e0ff735e4bbb8a8464c9.

The write_zeroes and seek_hole clippy warnings are only on test code, we
can safely allow them.

Signed-off-by: Jing Liu <jing2.liu@linux.intel.com>
Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
@jiangliu
Copy link
Member

jiangliu commented May 7, 2019

Seems file_traits, wrtite_zeros, seek_hole and fallocate are related to each other, how about combine them into one file?

@jiangliu
Copy link
Member

It fails to compile with "cargo build --target x86_64-unknown-linux-musl" as below. GNU library is ok.
error[E0432]: unresolved imports libc::SEEK_DATA, libc::SEEK_HOLE
--> /usr/local/rust/git/checkouts/vmm-sys-util-5509361781321914/766db44/src/seek_hole.rs:11:28
|
11 | use libc::{lseek64, ENXIO, SEEK_DATA, SEEK_HOLE};
| ^^^^^^^^^ ^^^^^^^^^ no SEEK_HOLE in the root
| |
| no SEEK_DATA in the root. Did you mean to use SEEK_SET?

error: aborting due to previous error

For more information about this error, try rustc --explain E0432.

@liujing2
Copy link
Collaborator Author

liujing2 commented May 20, 2019

It fails to compile with "cargo build --target x86_64-unknown-linux-musl" as below. GNU library is ok.
error[E0432]: unresolved imports libc::SEEK_DATA, libc::SEEK_HOLE
--> /usr/local/rust/git/checkouts/vmm-sys-util-5509361781321914/766db44/src/seek_hole.rs:11:28
|
11 | use libc::{lseek64, ENXIO, SEEK_DATA, SEEK_HOLE};
| ^^^^^^^^^ ^^^^^^^^^ no SEEK_HOLE in the root
| |
| no SEEK_DATA in the root. Did you mean to use SEEK_SET?

@jiangliu According to the MUSL libc which is used by rust musl build, we can find that MUSL libc doesn't have SEEK_HOLE and SEEK_DATA support.

musl does not define the SEEK_HOLE and SEEK_DATA constants.
We use the libc one when using glibc and we define them when building
for musl.

Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
jiangliu
jiangliu previously approved these changes May 22, 2019
sboeuf
sboeuf previously approved these changes May 22, 2019
@sboeuf
Copy link

sboeuf commented May 22, 2019

@zachreizner @dgreid PTAL

@jiangliu
Copy link
Member

Could we also reexport libc::{EFD_SEMAPHORE, EFD_SEMAPHORE, EFD_CLOEXEC} in the eventfd.rs, so it get a little easier for use.

@sameo sameo dismissed stale reviews from sboeuf and jiangliu via b3dd077 May 28, 2019 09:24
@sameo
Copy link
Collaborator

sameo commented May 28, 2019

Testing a potential fix...

Samuel Ortiz added 2 commits May 28, 2019 11:26
Using the File wrappers leads to poisoned pointers when running under a
tmpfs based /dev mount.

Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
The vmm-sys-util test make heavy use of temporary dirs and files.
Running that under a container overlay leads to erratic behaviours and
this is fixed by using a real tmpfs mount into the container's /tmp.

Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
@sameo
Copy link
Collaborator

sameo commented May 28, 2019

Testing a potential fix...

Seems to be working.

sameo
sameo previously approved these changes May 28, 2019
@sameo
Copy link
Collaborator

sameo commented May 28, 2019

Could we also reexport libc::{EFD_SEMAPHORE, EFD_SEMAPHORE, EFD_CLOEXEC} in the eventfd.rs, so it get a little easier for use.

@jiangliu Do you mind if we merge that one first and then send that as a follow up PR?

@jiangliu
Copy link
Member

Could we also reexport libc::{EFD_SEMAPHORE, EFD_SEMAPHORE, EFD_CLOEXEC} in the eventfd.rs, so it get a little easier for use.

@jiangliu Do you mind if we merge that one first and then send that as a follow up PR?

sure, actually I have three patches upon this patch set:)

@sameo
Copy link
Collaborator

sameo commented May 28, 2019

Could we also reexport libc::{EFD_SEMAPHORE, EFD_SEMAPHORE, EFD_CLOEXEC} in the eventfd.rs, so it get a little easier for use.

@jiangliu Do you mind if we merge that one first and then send that as a follow up PR?

sure, actually I have three patches upon this patch set:)

Excellent :) Could you please give this PR another review? Adding new commits dismissed the previous reviews...

@sboeuf
Copy link

sboeuf commented May 28, 2019

Looks like this PR is ready to be merged :)

@jiangliu
Copy link
Member

Looks like this PR is ready to be merged :)

Great, the vhost crate could move on once this has been merged:)

@andreeaflorescu andreeaflorescu merged commit 60fe35b into rust-vmm:master May 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants