-
Notifications
You must be signed in to change notification settings - Fork 6
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
WIP: AF_VSOCK support #8
base: main
Are you sure you want to change the base?
Conversation
On Github, maybe draft pull requests can be used for this? I see "Still in progress? [Convert to draft]".
What if somebody wants to manage many listener addresses in a Hash/BTreeSet? What is a VSOCK address? Is it some byte buffer or a string or a number? According to What is a best way to test that support locally? Is |
src/connection.rs
Outdated
@@ -34,6 +37,8 @@ impl std::fmt::Debug for Connection { | |||
ConnectionImpl::Tcp(_) => f.write_str("Connection(tcp)"), | |||
#[cfg(all(feature = "unix", unix))] | |||
ConnectionImpl::Unix(_) => f.write_str("Connection(unix)"), | |||
#[cfg(all(feature = "vsock", target_os = "linux"))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall android
be also added?
Custom kernels may enable AF_VSOCK
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[cfg(any(linux_android, apple_targets))]
used in nix
for AF_VSOCK
definition
And VsockAddr defined here https://github.com/nix-rust/nix/blob/master/src/sys/socket/addr.rs#L2049 (It have Hash, but not Ord).
If I need to have own VsockAddr struct, where better to have it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[cfg(any(linux_android, apple_targets))] used in nix for AF_VSOCK definition
That means something similar should be available in tokio-listener
.
If vsock
and tokio-vsock
supports a platform, tokio-listener
should as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nix have following aliases, https://github.com/nix-rust/nix/blob/master/build.rs#L21
And vsock exported on any(android_linux, apple_targets)
.
Should I bring similar aliases, or just use any(target_os = "android", target_os="linux", target_os="macos")
?
(that set which I have in not-pushed-yet development state at the moment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bringing in cfg_aliases!
(and build.rs
) is probably too much for this. Let's wait when such feature appears in Rust proper.
The simple (latter) one looks better.
Are there any unusual vsock-specific options that need a separate command line flags? Primary way to users to access vsock listener is expected to be the The only clap thing that comes to mind is a allowing overriding CIDs from That would somewhat deviate from TCP's obligatory address. |
It is also expected to be portable, i.e. you should be able to specify a Less portable things may be in |
@vi correct.
So I would personally prefer last one, because I like to have Regarding parsing from string -- I have use following parser in my code https://github.com/avnik/ghaf-givc/blob/avnik/listeners/src/utils/vsock.rs (not claiming as universal solution) |
Okay, I refactor |
It maybe something like that, but those aliases (
Maybe |
Documentation should be extended. Also there should be a snippet in Clap documentation comment (duplicated for positional and Speaking of the platform support: what about Android and Mac? As far as I understand, What about socket options? Are |
Documentation would be expanded as well (with mention that it works on linux/android, and probably on darwin).
working on change conditions on But I test everything only on linux now, I haven't apple/windows machines here.
Would investigate if it possibe Thanks for feedback |
Constants are platform dependent (in
Looks good for me |
Do they differ significantly (other numbers between Darwin and Linux) or just defined in a cfg-ed way? If the latter then maybe just hard code them for now. If the former then maybe we should omit aliases, as they would change meaning if one serializes+deserializes |
cfg-ed for all 3 platforms, but equal (linux have little bit more comments inside)
So we can borrow these values as pub (crate), and ensure in tests that they equal appropriate libc values |
@vi I addressed most of issues. Please re-review Last pending thing -- |
Okay, I added tonic integration, but it require
to be added into app's Cargo.toml. I didn't touch tonic010, tonic011 because I could test only 0.12 (my app use 0.12) |
I'm not sure a I'm not even sure such crates are publishable on crates.io - they should only depend on another crates.io crates. |
What Tonic has to do with Tokio support for VSOCK? Why does Maybe if Tonic over VSOCK is a popular use case then providing a helper (and reusing it for |
VsockConnectionInfo for tonic is also conditional in tonic-vsock |
One of the way is just avoid relying on |
Yep, I used it for make app buildable. |
I could borrow/inline this code if you with (adding FIXME note to remove duplication, when new version would be released) |
src/claptools.rs
Outdated
@@ -29,6 +29,9 @@ pub struct ListenerAddressPositional { | |||
/// | |||
/// * UNIX socket path like /tmp/mysock or Linux abstract address like @abstract | |||
/// | |||
/// * AF_VSOCK socket in format vsock:cid:port, like vsock:42:8080 | |||
/// Refer vsock manual for details: https://www.man7.org/linux/man-pages/man7/vsock.7.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you meant more spaces before Refer
for it to render nicely.
Here how it currently looks:
I expect you intended it to look other way.
If you run cargo doc --all-features
you also see some warning regarding the unformatted link. Note that there are currently a lot of other, unrelated doc warnings.
Note that in this case you should also care how it would look in console, so a balance between Markdown-ifying and just inhibiting relevant warnings may be needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here probably should be a dot before "refer" word, but it easily could be mistaken as part of syntax
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe that part is too lengthy for a CLI help message anyway, so it may be worth to keep it like that:
AF_VSOCK socket in format vsock:cid:port. Example: vsock:any:8080
There is no reference to e.g. unix(7)
or inet(7)
anyway nearby.
Note: currently it fails the You can use it (combined with e.g. |
/// [vsock manual]: https://www.man7.org/linux/man-pages/man7/vsock.7.html | ||
/// (Implemented only on Linux and Darwin) | ||
Vsock { | ||
cid: u32, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation detailing what specifically cid
and port
mean can be moved here. Otherwise it triggers the #[warn(missing_docs)]
lint.
Would take a look. I really tested only on linux (because haven't other platforms, but I would ask someone to try build on darwin and windows as well) |
src/some_socket_addr.rs
Outdated
@@ -27,6 +30,8 @@ impl Display for SomeSocketAddr { | |||
SomeSocketAddr::Unix(_x) => "unix".fmt(f), | |||
#[cfg(feature = "inetd")] | |||
SomeSocketAddr::Stdio => "stdio".fmt(f), | |||
#[cfg(all(any(target_os = "linux", target_os = "android", target_os = "macos"), feature = "vsock"))] | |||
SomeSocketAddr::Vsock(x) => x.fmt(f), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would produce something like cid: {} port: {}
(without any direct indication that it is VSOCK). Is it OK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done as "vsock:{cid}:{port}"
src/tonic010.rs
Outdated
@@ -31,6 +31,10 @@ impl Connected for Connection { | |||
if self.try_borrow_stdio().is_some() { | |||
return ListenerConnectInfo::Stdio; | |||
} | |||
#[cfg(feature = "vsock")] | |||
if let Some(vsock) = self.try_borrow_vsock() { | |||
return ListenerConnectInfo::Vsock(vsock.connect_info()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ListenerConnectInfo::Vsock
does not currently exist (and it fails compilation), also #[cfg]
omits platform check.
Are you sure we need to also add a special support for legacy tonic 0.10? Maybe just let it fall back to Other
below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, tonic010 was added by mistake. I plan to add support 0.12+ only (if you haven't mind). Otherwise it was too much copy-paste for inlining VsockConnectInfo
src/tonic012.rs
Outdated
use tonic_012::transport::server::{Connected, TcpConnectInfo}; | ||
|
||
#[cfg(all(any(target_os = "linux", target_os = "android", target_os = "macos"), feature = "vsock"))] | ||
use tokio_vsock::{VsockStream, VsockConnectInfo}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(see note about duplicating the VsockConnectInfo
here in the main discussion)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inlined that code as vsock_support
module at end of file
Added the comments. Also ensured
Maybe the dependency needs to be specified like this: [target."cfg(any(target_os = \"linux\", target_os = \"android\", target_os = \"macos\"))".dependencies]
tokio-vsock = {version = "0.5", optional=true} |
You can check most of compilability without a tricky setup, just by installing libstd for that target. |
Assuming "wish".
I don't understand what ecosystem fixes are expected here. Part of the reason is to avoid tricky conditional feature setup in Cargo.toml (if such setup is even possible). The more I think about that (e.g. what would happen when new features will appear in tokio-vsock proper and when new Tonic versions would appear), the more I think that native tokio-vsock's Tonic support should be avoided. I.e. AF_VSOCK worlds and Tonic worlds should be kept apart and only meet within Unfortunately, that means Tonic apps that really need to know what CID and port connects to them would need a bit deeper migration to/from |
Well, in our app we plan use this feature exactly for this -- extra authentication, some requests should be allowed only from host, or dedicated guest vm. |
Do I correctly assume that a VSOCK address is just a pair of numbers without any tricky things, so any options should be easy enough for app developers - copy those numbers from one place to another and call it a day? |
|
Signed-off-by: Alexander V. Nikolaev <[email protected]>
Signed-off-by: Alexander V. Nikolaev <[email protected]>
Signed-off-by: Alexander V. Nikolaev <[email protected]>
Signed-off-by: Alexander V. Nikolaev <[email protected]>
Signed-off-by: Alexander V. Nikolaev <[email protected]>
Cargo.toml
Outdated
@@ -35,6 +35,7 @@ tracing = "0.1.37" | |||
axum07 = { version="0.7", package="axum", optional=true } | |||
futures-util = {version="0.3", optional=true} | |||
tokio-util = {version = "0.7", optional=true, features=["net","codec"]} | |||
tokio-vsock = {version = "0.5", optional=true} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tokio-vsock
does not seem to build on Windows, which breaks --all-features
build.
Somewhere else in the comments there is a snippet about how to include it conditionally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strange, because diff started with suggested snippet in Cargo.toml
@vi updated a branch. cargo check/cargo hack check now pass for both linux and win32, vsock connection info code inlined (and slightly rewritten) |
@avnik , What is current status of the pull request? Is it marked as a draft because of you are not yet sure it is complete enough to be mergeable? |
@vi it finished, I waited until someone test it on windows. But looks like you merged it anyway, thanks |
Initial support of AF_VSOCK sockets (vsocks are sockets for virtual machines to host communication).
It basically works, so I like hear some feedback.
Some opinionated things done --
I dropped unused derived traits on ListenerAddr (.Eq
,Ord
andHash
) -- main reason was a VsockAddr,, which is not supportOrd/Hash
Some things still pending to do:
Solve import ofVsockAddr
-- importing it from nix cause version conflict with tonic, importing straight from tokio_vsock make it impossible to build without it.Feedback and suggestions welcomed