Rust Socket abstractions
Subsystems (3)
cover Rust Socket abstractions
This patch series is intended to create Rust abstractions for Sockets and other fundamental network entities.
Specifically, it was added:
- Ip address and Socket address wrappers (for in_addr, in6_addr, sockaddr_in, sockaddr_in6, sockaddr_storage).
- Socket wrapper.
- Socket flags and options enums.
- TCP and UDP specific abstractions over the Rust Socket structure.
This series is a RFC because I would appreciate some feedback about:
- The structure of the module: is the division of the files and modules appropriate or should it be more or less fine-grained? Also, should the net module export all the structures of its submodules? I noticed that it is done in the standard library.
- Whether the documentation is comprehensive enough.
- A few other specific questions, written in the individual patches.
I would greatly appreciate any kind of feedback or opinion. I am pretty new to the patch/mailing list world, so please point out any mistake I might make.
The changes in this patch series are based on top of the latest commit of rust-next in the Rust git tree:19cd7b5d229c ("btf, scripts: rust: drop is_rust_module.sh")
Michele Dalle Rive (7):
rust: net: add net module files and shared enums.
rust: net: add ip and socket address bindings.
rust: net: add socket-related flags and flagset.
rust: net: add socket wrapper.
rust: net: implement socket options API.
rust: net: add socket TCP wrappers.
rust: net: add socket UDP wrappers.
rust/bindings/bindings_helper.h | 3 + rust/kernel/lib.rs | 2 + rust/kernel/net.rs | 185 +++++ rust/kernel/net/addr.rs | 1215 ++++++++++++++++++++++++++++++ rust/kernel/net/ip.rs | 73 ++ rust/kernel/net/socket.rs | 641 ++++++++++++++++ rust/kernel/net/socket/flags.rs | 467 ++++++++++++ rust/kernel/net/socket/opts.rs | 1222 +++++++++++++++++++++++++++++++ rust/kernel/net/tcp.rs | 252 +++++++ rust/kernel/net/udp.rs | 182 +++++ 10 files changed, 4242 insertions(+) create mode 100644 rust/kernel/net.rs create mode 100644 rust/kernel/net/addr.rs create mode 100644 rust/kernel/net/ip.rs create mode 100644 rust/kernel/net/socket.rs create mode 100644 rust/kernel/net/socket/flags.rs create mode 100644 rust/kernel/net/socket/opts.rs create mode 100644 rust/kernel/net/tcp.rs create mode 100644 rust/kernel/net/udp.rs
Signature · 1 line
2.41.0
RFC PATCH 1/7 rust: net: add net module files and shared enums.
Create net module files and network headers in bindings_helper.h. Add IpProtocol, AddressFamily and Namespace.
The wrappers added with this patch are shared across the whole network subsystem. For this reason, they are placed in the net.rs module file.
The enum IpProtocol, however, is placed in an individual ip.rs submodule, allowing to place together all the ip-related structures, such as wrappers for iphdr, ip_auth_hdr, etc.
--- rust/bindings/bindings_helper.h | 3 + rust/kernel/lib.rs | 2 + rust/kernel/net.rs | 180 ++++++++++++++++++++++++++++++++ rust/kernel/net/ip.rs | 73 +++++++++++++ 4 files changed, 258 insertions(+) create mode 100644 rust/kernel/net.rs create mode 100644 rust/kernel/net/ip.rs
rust/bindings/bindings_helper.h +3 -0
@@ -7,6 +7,9 @@*/ #include <linux/errname.h> +#include <linux/netfilter.h> +#include <linux/inet.h> +#include <linux/tcp.h> #include <linux/slab.h> #include <linux/refcount.h> #include <linux/wait.h>
rust/kernel/lib.rs +2 -0
@@ -34,6 +34,8 @@pub mod error;pub mod init;pub mod ioctl; +#[cfg( CONFIG_NET )] +pub mod net;pub mod prelude;pub mod print;mod static_assert;
rust/kernel/net.rs new file +180 -0
180 lines not on this page · raw message
rust/kernel/net/ip.rs new file +73 -0
73 lines not on this page · raw message
Signature · 1 line
2.41.0
RFC PATCH 2/7 rust: net: add ip and socket address bindings.
In reply to Michele Dalle RiveCreate structures to handle addresses: Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4 and SocketAddrV6.
These structures are meant to be as similar as possible to the ones in Rust std::net, while, at the same time, providing functionalities available in the kernel.
Some extra structures are added, compared to std:
- SocketAddrStorage: wraps struct sockaddr_storage and is used to interact with the kernel functions when the type of socket address is unknown. Since it is only used for FFI, it is crate-public.
- GenericSocketAddr: trait that defines shared functions and traits amont all socket addresses.
---
A few questions here:
- Should SocketAddrStorage be crate-public or public? My reasoning is that modules should be using SocketAddr for an "unspecified" socket address type; however, having SocketAddrStorage available could give the freedom of eventually using bindings directly to do more complex tasks.
- Is GenericSocketAddr useful? I thought it could be convenient to have a socket address trait, as a sort of "superclass" for all socket addresses. However, I think it has no particular use, since there is SocketAddr.
- Should the addresses be divided in two files, one for ip addresses and one for socket addresses? Or everything together works?
rust/kernel/net.rs | 2 + rust/kernel/net/addr.rs | 1215 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 1217 insertions(+) create mode 100644 rust/kernel/net/addr.rs
rust/kernel/net.rs +2 -0
@@ -9,6 +9,8 @@use crate ::error::{code,Error };use core::cell::UnsafeCell ; + +pub mod addr;pub mod ip;/// The address family.
rust/kernel/net/addr.rs new file +1215 -0
1,215 lines not on this page · raw message
Signature · 1 line
2.41.0
RFC PATCH 3/7 rust: net: add socket-related flags and flagset.
In reply to Michele Dalle RiveAdd enums representing flags related to sockets:
- ReceiveFlag to modify the behaviour of the socket receive operation.
- SendFlag to modify the behaviour of the socket send operation.
- MessageFlag to represent the flags in a msghdr.
- SocketFlag to represent the flags in the socket struct.
Introduce a FlagSet structure to offer a convenient way to handle the flags.
Having an abstraction over the "raw" numerical value of the flags offers many advantages:
- A FlagSet can be created in different ways: from an IntoIterator, a value, a single flag or using the defined macro flag_set!(...).
- Custom operations can be defined, such as the bitwise or.
- Flags in the set can be set, tested, unset through functions instead of using bitwise operations.
- FlagSet implements the IntoIterator trait, allowing for iteration over the flags contained.
---
Opinions on FlagSet? I think it might be a convenient structure, for any kind of "bitmask flag", not necessarily limited to the socket ones, since it provides methods to easily create and access the flags. However, it might be cumbersome to use to just obtain the same result of a simple bitwise OR.
rust/kernel/net/socket/flags.rs | 467 ++++++++++++++++++++++++++++++++ 1 file changed, 467 insertions(+) create mode 100644 rust/kernel/net/socket/flags.rs
rust/kernel/net/socket/flags.rs new file +467 -0
@@ -0,0 +1,467 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Socket-related flags and utilities. +use crate ::bindings; +use core::fmt::Debug ; +use core::ops::{BitOr ,BitOrAssign }; + +/// Generic socket flag trait. +/// +/// This trait represents any kind of flag with "bitmask" values (i.e. 0x1, 0x2, 0x4, 0x8, etc.) +pub trait Flag : +Into <isize > +TryFrom <isize > +Debug +Copy +Clone +Send +Sync + 'static +{ +} + +/// Socket send operation flags. +/// +/// See <https://linux.die.net/man/2/sendmsg> for more. +#[derive( Debug , Copy , Clone )] +pub enum SendFlag { +/// Got a successful reply. +/// +/// Only valid for datagram and raw sockets. +/// Only valid for IPv4 and IPv6. +Confirm = bindings::MSG_CONFIRM as isize , + +/// Don't use a gateway to send out the packet. +DontRoute = bindings::MSG_DONTROUTE as isize , + +/// Enables nonblocking operation. +/// +/// If the operation would block, return immediately with an error. +DontWait = bindings::MSG_DONTWAIT as isize , + +/// Terminates a record. +EOR = bindings::MSG_EOR as isize , + +/// More data will be sent. +/// +/// Only valid for TCP and UDP sockets. +More = bindings::MSG_MORE as isize , + +/// Don't send SIGPIPE error if the socket is shut down. +NoSignal = bindings::MSG_NOSIGNAL as isize , + +/// Send out-of-band data on supported sockets. +OOB = bindings::MSG_OOB as isize , +} + +impl From <SendFlag >for isize { +fn from (value:SendFlag ) ->Self { + valueas isize + } +} + +impl TryFrom <isize >for SendFlag { +type Error = (); + +fn try_from (value:isize ) ->Result <SendFlag ,Self ::Error > { +let val = valueas u32 ; +match val { + bindings::MSG_CONFIRM =>Ok (SendFlag ::Confirm ), + bindings::MSG_DONTROUTE =>Ok (SendFlag ::DontRoute ), + bindings::MSG_DONTWAIT =>Ok (SendFlag ::DontWait ), + bindings::MSG_EOR =>Ok (SendFlag ::EOR ), + bindings::MSG_MORE =>Ok (SendFlag ::More ), + bindings::MSG_NOSIGNAL =>Ok (SendFlag ::NoSignal ), + bindings::MSG_OOB =>Ok (SendFlag ::OOB ), + _ =>Err (()), + } + } +} + +impl Flag for SendFlag {} + +/// Socket receive operation flags. +/// +/// See <https://linux.die.net/man/2/recvmsg> for more. +#[derive( Debug , Copy , Clone )] +pub enum ReceiveFlag { +/// Enables nonblocking operation. +/// +/// If the operation would block, return immediately with an error. +DontWait = bindings::MSG_DONTWAIT as isize , + +/// Specifies that queued errors should be received from the socket error queue. +ErrQueue = bindings::MSG_ERRQUEUE as isize , + +/// Enables out-of-band reception. +OOB = bindings::MSG_OOB as isize , + +/// Peeks at an incoming message. +/// +/// The data is treated as unread and the next recv() or similar function shall still return this data. +Peek = bindings::MSG_PEEK as isize , + +/// Returns the real length of the packet, even when it was longer than the passed buffer. +/// +/// Only valid for raw, datagram, netlink and UNIX datagram sockets. +Trunc = bindings::MSG_TRUNC as isize , + +/// Waits for the full request to be satisfied. +WaitAll = bindings::MSG_WAITALL as isize , +} + +impl From <ReceiveFlag >for isize { +fn from (value:ReceiveFlag ) ->Self { + valueas isize + } +} + +impl TryFrom <isize >for ReceiveFlag { +type Error = (); + +fn try_from (value:isize ) ->Result <Self ,Self ::Error > { +let val = valueas u32 ; +match val { + bindings::MSG_DONTWAIT =>Ok (ReceiveFlag ::DontWait ), + bindings::MSG_ERRQUEUE =>Ok (ReceiveFlag ::ErrQueue ), + bindings::MSG_OOB =>Ok (ReceiveFlag ::OOB ), + bindings::MSG_PEEK =>Ok (ReceiveFlag ::Peek ), + bindings::MSG_TRUNC =>Ok (ReceiveFlag ::Trunc ), + bindings::MSG_WAITALL =>Ok (ReceiveFlag ::WaitAll ), + _ =>Err (()), + } + } +} + +impl Flag for ReceiveFlag {} + +/// Socket `flags` field flags. +/// +/// These flags are used internally by the kernel. +/// However, they are exposed here for completeness. +/// +/// This enum does not implement the `Flag` trait, since it is not actually a flag. +/// Flags are often defined as a mask that can be used to retrieve the flag value; the socket flags, +/// instead, are defined as the index of the bit that they occupy in the `flags` field. +/// This means that they cannot be used as a mask, just like all the other flags that implement `Flag` do. +/// +/// For example, SOCK_PASSCRED has value 3, meaning that it is represented by the 3rd bit of the `flags` field; +/// a normal flag would represent it as a mask, i.e. 1 << 3 = 0b1000. +/// +/// See [include/linux/net.h](../../../../include/linux/net.h) for more. +pub enum SocketFlag { +/// Undocumented. +NoSpace = bindings::SOCK_NOSPACE as isize , +/// Undocumented. +PassCred = bindings::SOCK_PASSCRED as isize , +/// Undocumented. +PassSecurity = bindings::SOCK_PASSSEC as isize , +/// Undocumented. +SupportZeroCopy = bindings::SOCK_SUPPORT_ZC as isize , +/// Undocumented. +CustomSockOpt = bindings::SOCK_CUSTOM_SOCKOPT as isize , +/// Undocumented. +PassPidFd = bindings::SOCK_PASSPIDFD as isize , +} + +impl From <SocketFlag >for isize { +fn from (value:SocketFlag ) ->Self { + valueas isize + } +} + +impl TryFrom <isize >for SocketFlag { +type Error = (); + +fn try_from (value:isize ) ->Result <Self ,Self ::Error > { +let val = valueas u32 ; +match val { + bindings::SOCK_NOSPACE =>Ok (SocketFlag ::NoSpace ), + bindings::SOCK_PASSCRED =>Ok (SocketFlag ::PassCred ), + bindings::SOCK_PASSSEC =>Ok (SocketFlag ::PassSecurity ), + bindings::SOCK_SUPPORT_ZC =>Ok (SocketFlag ::SupportZeroCopy ), + bindings::SOCK_CUSTOM_SOCKOPT =>Ok (SocketFlag ::CustomSockOpt ), + bindings::SOCK_PASSPIDFD =>Ok (SocketFlag ::PassPidFd ), + _ =>Err (()), + } + } +} + +/// Flags associated with a received message. +/// +/// Represents the flag contained in the `msg_flags` field of a `msghdr` struct. +#[derive( Debug , Copy , Clone )] +pub enum MessageFlag { +/// End of record. +Eor = bindings::MSG_EOR as isize , +/// Trailing portion of the message is discarded. +Trunc = bindings::MSG_TRUNC as isize , +/// Control data was discarded due to lack of space. +Ctrunc = bindings::MSG_CTRUNC as isize , +/// Out-of-band data was received. +Oob = bindings::MSG_OOB as isize , +/// An error was received instead of data. +ErrQueue = bindings::MSG_ERRQUEUE as isize , +} + +impl From <MessageFlag >for isize { +fn from (value:MessageFlag ) ->Self { + valueas isize + } +} + +impl TryFrom <isize >for MessageFlag { +type Error = (); + +fn try_from (value:isize ) ->Result <Self ,Self ::Error > { +let val = valueas u32 ; +match val { + bindings::MSG_EOR =>Ok (MessageFlag ::Eor ), + bindings::MSG_TRUNC =>Ok (MessageFlag ::Trunc ), + bindings::MSG_CTRUNC =>Ok (MessageFlag ::Ctrunc ), + bindings::MSG_OOB =>Ok (MessageFlag ::Oob ), + bindings::MSG_ERRQUEUE =>Ok (MessageFlag ::ErrQueue ), + _ =>Err (()), + } + } +} + +impl Flag for MessageFlag {} + +/// Structure representing a set of flags. +/// +/// This structure is used to represent a set of flags, such as the flags passed to `send` or `recv`. +/// It is generic over the type of flag that it contains. +/// +/// # Invariants +/// The value of the flags must be a valid combination of the flags that it contains. +/// +/// This means that the value must be the bitwise OR of the values of the flags, and that it +/// must be possible to retrieve the value of the flags from the value. +/// +/// # Example +/// ``` +/// use kernel::net::socket::flags::{SendFlag, FlagSet}; +/// +/// let mut flags = FlagSet::<SendFlag>::empty(); +/// flags.insert(SendFlag::DontWait); +/// flags.insert(SendFlag::More); +/// assert!(flags.contains(SendFlag::DontWait)); +/// assert!(flags.contains(SendFlag::More)); +/// flags.clear(); +/// assert_eq!(flags.value(), 0); +/// +/// flags = FlagSet::<SendFlag>::from(SendFlag::More); +/// flags |= SendFlag::DontWait; +/// assert!(flags.contains(SendFlag::DontWait)); +/// ``` +#[derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord )] +pub struct FlagSet <T :Flag > { + value:isize , + _phantom: core::marker::PhantomData <T >, +} + +impl <T :Flag >FlagSet <T > { +/// Create a new empty set of flags. +/// +/// # Example +/// ``` +/// use kernel::net::socket::flags::{SendFlag, FlagSet}; +/// +/// let flags = FlagSet::<SendFlag>::empty(); +/// assert_eq!(flags.value(), 0); +/// ``` +pub fn empty () ->Self { +FlagSet { + value:0 , + _phantom: core::marker::PhantomData , + } + } + +/// Clear all the flags set. +/// +/// # Example +/// ``` +/// use kernel::net::socket::flags::{SendFlag, FlagSet}; +/// +/// let mut flags = FlagSet::<SendFlag>::from(SendFlag::More); +/// assert!(flags.contains(SendFlag::More)); +/// flags.clear(); +/// assert_eq!(flags.value(), 0); +/// ``` +pub fn clear (&mut self) { + self.value =0 ; + } + +/// Add a flag to the set. +/// +/// # Example +/// ``` +/// use kernel::net::socket::flags::{SendFlag, FlagSet}; +/// +/// let mut flags = FlagSet::<SendFlag>::empty(); +/// assert!(!flags.contains(SendFlag::DontWait)); +/// flags.insert(SendFlag::DontWait); +/// assert!(flags.contains(SendFlag::DontWait)); +/// ``` +pub fn insert (&mut self, flag:T ) { + self.value |= flag.into (); + } + +/// Remove a flag from the set. +/// +/// # Example +/// ``` +/// use kernel::net::socket::flags::{SendFlag, FlagSet}; +/// +/// let mut flags = FlagSet::<SendFlag>::from(SendFlag::DontWait); +/// assert!(flags.contains(SendFlag::DontWait)); +/// flags.remove(SendFlag::DontWait); +/// assert!(!flags.contains(SendFlag::DontWait)); +/// ``` +pub fn remove (&mut self, flag:T ) { + self.value &= !flag.into (); + } + +/// Check if a flag is set. +/// +/// # Example +/// ``` +/// use kernel::net::socket::flags::{SendFlag, FlagSet}; +/// +/// let mut flags = FlagSet::<SendFlag>::from(SendFlag::DontWait); +/// assert!(flags.contains(SendFlag::DontWait)); +/// ``` +pub fn contains (&self, flag:T ) ->bool { + self.value & flag.into () !=0 + } + +/// Get the integer value of the flags set. +/// +/// # Example +/// ``` +/// use kernel::net::socket::flags::{SendFlag, FlagSet}; +/// +/// let flags = FlagSet::<SendFlag>::from(SendFlag::DontWait); +/// assert_eq!(flags.value(), SendFlag::DontWait as isize); +/// ``` +pub fn value (&self) ->isize { + self.value + } +} + +impl <T :Flag >BitOr <T >for FlagSet <T > { +type Output =FlagSet <T >; + +fn bitor (self, rhs:T ) ->Self ::Output { +FlagSet { + value: self.value | rhs.into (), + _phantom: core::marker::PhantomData , + } + } +} + +impl <T :Flag >BitOrAssign <T >for FlagSet <T > { +fn bitor_assign (&mut self, rhs:T ) { + self.value |= rhs.into (); + } +} + +// impl from isize for any flags<T> +impl <T :Flag >From <isize >for FlagSet <T > { +fn from (value:isize ) ->Self { +FlagSet { + value, + _phantom: core::marker::PhantomData , + } + } +} + +impl <T :Flag >From <T >for FlagSet <T > { +fn from (value:T ) ->Self { +Self ::from (value.into ()) + } +} + +impl <T :Flag >FromIterator <T >for FlagSet <T > { +fn from_iter <I :IntoIterator <Item =T >>(iter:I ) ->Self { +let mut flags =FlagSet ::empty (); +for flagin iter { + flags.insert (flag); + } + flags + } +} + +impl <T :Flag >From <FlagSet <T >>for isize { +fn from (value:FlagSet <T >) ->Self { + value.value + } +} + +impl <T :Flag >IntoIterator for FlagSet <T > { +type Item =T ; +type IntoIter =FlagSetIterator <T >; + +fn into_iter (self) ->Self ::IntoIter { +FlagSetIterator { + flags: self, + current:0 , + } + } +} + +/// Iterator over the flags in a set. +/// +/// This iterator iterates over the flags in a set, in order of increasing value. +/// +/// # Example +/// ``` +/// use kernel::net::socket::flags::{SendFlag, FlagSet}; +/// +/// let mut flags = FlagSet::from_iter([SendFlag::DontWait, SendFlag::More]); +/// for flag in flags.into_iter() { +/// println!("Flag: {:?}", flag); +/// } +/// ``` +pub struct FlagSetIterator <T :Flag > { + flags:FlagSet <T >, + current:usize , +} + +impl <T :Flag >Iterator for FlagSetIterator <T > { +type Item =T ; + +fn next (&mut self) ->Option <Self ::Item > { +let mut value =1 << self.current; +while value <= self.flags.value { + self.current +=1 ; +if self.flags.value & value !=0 { +if let Ok (flag) =T ::try_from (value) { +return Some (flag); + } + } + value =1 << self.current; + } +None + } +} + +/// Create a set of flags from a list of flags. +/// +/// This macro provides a compact way to create empty sets and sets from a list of flags. +/// +/// # Example +/// ``` +/// use kernel::net::socket::flags::SendFlag; +/// use kernel::flag_set; +/// +/// let mut flags = flag_set!(SendFlag::DontWait, SendFlag::More); +/// assert!(flags.contains(SendFlag::DontWait)); +/// assert!(flags.contains(SendFlag::More)); +/// +/// let mut empty_flags = flag_set!(); +/// assert_eq!(empty_flags.value(), 0); +/// ``` +#[macro_export] +macro_rules! flag_set { + () => { + $crate::net::socket::flags::FlagSet ::empty () + }; + ($($flag:expr),+) => { + $crate::net::socket::flags::FlagSet ::from_iter ([$($flag),+]) + }; +}
Signature · 1 line
2.41.0
RFC PATCH 4/7 rust: net: add socket wrapper.
In reply to Michele Dalle RiveCreate a Socket abstraction, which provides a Rust API to the kernel socket functionalities.
The Socket structures tries to keep the same function signatures of the Rust standard library; at the same time, functions are added or modified in order to provide as much as possible of the C kernel functionalities.
Most of the internals of the C socket is not accessible by Rust, because those structures are still to be wrapped. However, sockets are mainly managed through the functions provided by the kernel; thus, even if some fields are not accessible, since the functions are wrapped, most of the kernel functionality should be available in Rust as well.
Specifically, the usage of msghdr is mostly abstracted away in the Rust interface, because using it would mean having to deal, both in the kernel and in modules, with Pinned instances (msghdr is self-referencing), which would be a struggle that provides no particular advantage. A MessageHeader object is actually created and returned when a message is received, because at that point the structure is not really self-referencing, as long as the source address is copied. The wrapper is not used when a message is sent.
Anyways, some useful functionalities of msghdr, like cmsghdrs, are missing and should be implemented in the future to provide a complete API.
---
Few questions here as well:
- What about MessageHeader? Does it make sense to only have it on a receive? My reasoning was that, AFAIK, when sending a message, the msghdr structure is used for the flags and for eventually the destination address. Does it make sense having to handle a pinned object just to avoid having flags and destination address as arguments of the send function? When a message is received, instead, there is no pinning problem (as long as the address is saved), so the message header can be freely returned and handled. As of now, the MessageHeader only contains sender address and flags, but in the future more msghdr fields could be wrapped.
- In has_flag and set_flag, does it make sense to use the bindgen-provided __BindgenBitfieldUnit? It is particularly convenient, as it handles the big/little endianness. However, I noticed that it changes between different bindgen version, and keeping the code up to date could take more time than manually handle the endianness.
rust/kernel/net.rs | 1 + rust/kernel/net/socket.rs | 550 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 551 insertions(+) create mode 100644 rust/kernel/net/socket.rs
rust/kernel/net.rs +1 -0
@@ -12,6 +12,7 @@pub mod addr;pub mod ip; +pub mod socket;/// The address family. ///
rust/kernel/net/socket.rs new file +550 -0
550 lines not on this page · raw message
Signature · 1 line
2.41.0
RFC PATCH 5/7 rust: net: implement socket options API.
In reply to Michele Dalle RiveCreate socket Options and set_option function in the Socket abstraction.
These changes introduce wrappers and functions to handle socket options in Rust, with compilation-time advantages compared to the C API:
- Type safety: A specific option accepts only a value of the correct type.
- Read/write safety: A read-only option cannot be set.
- Coherence safety: An option of, for example, IP level cannot be set by specifying another level.
The downside of using options in the kernel is the lack of functions to get the value of an option. For this reason, in Rust, kernel options can only be set, but not retrieved.
Everything that can be done by socket options can actually be done through helper functions, or by accessing directly the specific fields. However, since the Rust-wrapped structures are few, it can be useful to have options in order to still be able to modify the behaviour of the socket.
As specified in the documentation of opts.rs, options could (and should) be removed when the Rust API will be developed enough.
---
Does this even make sense? I have been struggling figuring out whether having socket options is reasonable.
As described in the message, I think that even if it just limited to setting options, it could be useful: Rust support is very limited, and having some freedom over the socket is better than having none.
Now, does the implementation make sense? If I had to implement options I wanted to at least make the most out of Rust, trying to make them type-safe and "read/write" safe. The result is effective for sure, but the code might end up being a bit ugly. I preferred using a macro to implement the code, because implementing each trait for each option would have been a huge amount of repeated code.
rust/kernel/net/socket.rs | 91 +++ rust/kernel/net/socket/opts.rs | 1222 ++++++++++++++++++++++++++++++++ 2 files changed, 1313 insertions(+) create mode 100644 rust/kernel/net/socket/opts.rs
rust/kernel/net/socket.rs +91 -0
@@ -16,9 +16,14 @@use crate ::error::{to_result,Result };use crate ::net::addr::*;use crate ::net::ip::IpProtocol ; +use crate ::net::socket::opts::{OptionsLevel ,WritableOption }; +use core::cmp::max; +use core::marker::PhantomData ;use flags::*; +use kernel::net::socket::opts::SocketOption ;pub mod flags; +pub mod opts;/// The socket type. pub enum SockType {
@@ -470,6 +475,72 @@ pub fn send_to( }; self.send_msg (bytes, message, flags) } + +/// Sets an option on the socket. +/// +/// Wraps the `sock_setsockopt` function. +/// +/// The generic type `T` is the type of the option value. +/// See the [options module](opts) for the type and extra information about each option. +/// +/// Unfortunately, options can only be set but not retrieved. +/// This is because the kernel functions to retrieve options are not exported by the kernel. +/// The only exported functions accept user-space pointers, and are therefore not usable in the kernel. +/// +/// # Example +/// ``` +/// use kernel::net::AddressFamily; +/// use kernel::net::ip::IpProtocol;use kernel::net::socket::{Socket, SockType}; +/// use kernel::net::socket::opts; +/// +/// let socket = Socket::new(AddressFamily::Inet, SockType::Datagram, IpProtocol::Udp)?; +/// socket.set_option::<opts::ip::BindAddressNoPort>(true)?; +/// ``` +pub fn set_option <O >(&self, value:impl Into <O ::Type >) ->Result +where +O :SocketOption +WritableOption , + { +let value_ptr =SockPtr ::new (&value); + +// The minimum size is the size of an integer. +let min_size = core::mem::size_of ::<core::ffi::c_int >(); +let size =max (core::mem::size_of ::<O ::Type >(), min_size); + +if O ::level () ==OptionsLevel ::Socket && !self.has_flag (SocketFlag ::CustomSockOpt ) { +// SAFETY: FFI call; +// the address is valid for the lifetime of the wrapper; +// the size is at least the size of an integer; +// the level and name of the option are valid and coherent. +to_result (unsafe { + bindings::sock_setsockopt ( + self.0 , +O ::level ()as isize as _ , +O ::value ()as _ , + value_ptr.to_raw ()as _ , + sizeas _ , + ) + }) + }else { +// SAFETY: FFI call; +// the address is valid for the lifetime of the wrapper; +// the size is at least the size of an integer; +// the level and name of the option are valid and coherent. +to_result (unsafe { + (*(*self.0 ).ops) + .setsockopt + .map (|f| { +f ( + self.0 , +O ::level ()as _ , +O ::value ()as _ , + value_ptr.to_raw ()as _ , + sizeas _ , + ) + }) + .unwrap_or (-(bindings::EOPNOTSUPP as i32 )) + }) + } + } }impl Drop for Socket {
@@ -548,3 +619,23 @@ fn from(hdr: MessageHeader) -> Self { hdr.0 } } + +#[derive( Clone , Copy )] +#[repr(transparent)] +struct SockPtr <'a>(bindings::sockptr_t ,PhantomData <&'a ()>); + +impl <'a>SockPtr <'a> { +fn new <T >(value: &'aT ) ->Self +where +T :Sized , + { +let mut sockptr = bindings::sockptr_t::default (); + sockptr.__bindgen_anon_1.kernel = valueas *const T as _ ; + sockptr._bitfield_1 = bindings::__BindgenBitfieldUnit::new ([1 ;1usize ]);// kernel ptr +SockPtr (sockptr,PhantomData ) + } + +fn to_raw (self) -> bindings::sockptr_t { + self.0 + } +}
rust/kernel/net/socket/opts.rs new file +1222 -0
1,222 lines not on this page · raw message
Signature · 1 line
2.41.0
RFC PATCH 6/7 rust: net: add socket TCP wrappers.
In reply to Michele Dalle RiveAdd TcpListener and TcpStream wrappers around the Rust Socket. They provide a convenient way to handle TCP sockets.
This interface is intended to be as close as possible to the one in std::net.
--- rust/kernel/net.rs | 1 + rust/kernel/net/tcp.rs | 252 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 253 insertions(+) create mode 100644 rust/kernel/net/tcp.rs
rust/kernel/net.rs +1 -0
@@ -13,6 +13,7 @@pub mod addr;pub mod ip;pub mod socket; +pub mod tcp;/// The address family. ///
rust/kernel/net/tcp.rs new file +252 -0
252 lines not on this page · raw message
Signature · 1 line
2.41.0
RFC PATCH 7/7 rust: net: add socket UDP wrappers.
In reply to Michele Dalle RiveAdd a UDP socket wrapper, which allows to handle UDP sockets conveniently.
This interface is intended to be as close as possible to the one in std::net.
--- rust/kernel/net.rs | 1 + rust/kernel/net/udp.rs | 182 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 rust/kernel/net/udp.rs
rust/kernel/net.rs +1 -0
@@ -14,6 +14,7 @@pub mod ip;pub mod socket;pub mod tcp; +pub mod udp;/// The address family. ///
rust/kernel/net/udp.rs new file +182 -0
182 lines not on this page · raw message
Signature · 1 line
2.41.0
Quoted 18 lines · Michele Dalle Rive
This patch series is intended to create Rust abstractions for Sockets and other fundamental network entities.
Specifically, it was added:
- Ip address and Socket address wrappers (forin_addr,in6_addr,sockaddr_in,sockaddr_in6,sockaddr_storage).
- Socket wrapper.
- Socket flags and options enums.
- TCP and UDP specific abstractions over the Rust Socket structure.This series is a RFC because I would appreciate some feedback about:
- The structure of the module: is the division of the files and modules appropriate or should it be more or less fine-grained? Also, should thenetmodule export all the structures of its submodules? I noticed that it is done in the standard library.
- Whether the documentation is comprehensive enough.
- A few other specific questions, written in the individual patches.
Quoted 3 lines
I would greatly appreciate any kind of feedback or opinion. I am pretty new to the patch/mailing list world, so please point out any mistake I might make.
The best feedback is "who will use these new interfaces?" Without that, it's really hard to review a patchset as it's difficult to see how the bindings will be used, right?
thanks,
greg k-h
Quoted 19 lines · Greg KH
Quoted 18 lines · Michele Dalle Rive
This patch series is intended to create Rust abstractions for Sockets and other fundamental network entities.
Specifically, it was added:
- Ip address and Socket address wrappers (forin_addr,in6_addr,sockaddr_in,sockaddr_in6,sockaddr_storage).
- Socket wrapper.
- Socket flags and options enums.
- TCP and UDP specific abstractions over the Rust Socket structure.This series is a RFC because I would appreciate some feedback about:
- The structure of the module: is the division of the files and modules appropriate or should it be more or less fine-grained? Also, should thenetmodule export all the structures of its submodules? I noticed that it is done in the standard library.
- Whether the documentation is comprehensive enough.
- A few other specific questions, written in the individual patches.
Quoted 7 lines
Quoted 3 lines
I would greatly appreciate any kind of feedback or opinion. I am pretty new to the patch/mailing list world, so please point out any mistake I might make.
The best feedback is "who will use these new interfaces?" Without that, it's really hard to review a patchset as it's difficult to see how the bindings will be used, right?
There is a long standing tradition in Linux, you don't get a new API merged without a user.
There is not too much use of in kernel sockets. Network file systems like NFS, and SMB are one. These need to be careful with memory usage, you could be busy writing blocks out because the system is low on memory and trying to free some up, and asking for more memory might not work. Sending kernel log messages to a server. But that needs care because of the different contexts it can be used in. Without knowing what it will be used for, it is hard for us the point the special considerations which need to be made.
So please also let us see the code using this API.
Andrew
Quoted 25 lines · Andrew Lunn
Quoted 23 lines · Greg KH
Quoted 21 lines · Michele Dalle Rive
This patch series is intended to create Rust abstractions for Sockets and other fundamental network entities.
Specifically, it was added:
- Ip address and Socket address wrappers (forin_addr,in6_addr,sockaddr_in,sockaddr_in6,sockaddr_storage).
- Socket wrapper.
- Socket flags and options enums.
- TCP and UDP specific abstractions over the Rust Socket structure.This series is a RFC because I would appreciate some feedback about:
- The structure of the module: is the division of the files and modules appropriate or should it be more or less fine-grained? Also, should thenetmodule export all the structures of its submodules? I noticed that it is done in the standard library.
- Whether the documentation is comprehensive enough.
- A few other specific questions, written in the individual patches.I would greatly appreciate any kind of feedback or opinion. I am pretty new to the patch/mailing list world, so please point out any mistake I might make.
Quoted 6 lines
Quoted 3 lines
The best feedback is "who will use these new interfaces?" Without that, it's really hard to review a patchset as it's difficult to see how the bindings will be used, right?
There is a long standing tradition in Linux, you don't get a new API merged without a user.
Sorry for not being clear on that.
These abstractions are meant to be used by modules: having them, modules can start using the kernel network functionalities through a first, high level interface.
Since there is currently no network support in Rust, this patch series represents a first step to provide access to networking to modules. Sockets are just the highest layer of the network stack: the goal would be to port structures deeper in the network stack, to give modules more access to the network api. However, you need to start somewhere.
Quoted 10 lines
There is not too much use of in kernel sockets. Network file systems like NFS, and SMB are one. These need to be careful with memory usage, you could be busy writing blocks out because the system is low on memory and trying to free some up, and asking for more memory might not work. Sending kernel log messages to a server. But that needs care because of the different contexts it can be used in. Without knowing what it will be used for, it is hard for us the point the special considerations which need to be made.
Quoted 3 lines
So please also let us see the code using this API.
Andrew
The lack of these abstractions was noticed in the context of a research of the usability of Rust for the development of consensus algorithms using UDP. Any kind of kernel module requiring network support can take advantage of these socket abstractions, from a tcp echo server to any kind of complex networking kernel module.
I could add an example kernel module in samples/rust showing an example of usage of these APIs.
Thank you for your time,
Michele
Quoted 25 lines
Quoted 2 lines
There is a long standing tradition in Linux, you don't get a new API merged without a user.
Sorry for not being clear on that.
These abstractions are meant to be used by modules: having them, modules can start using the kernel network functionalities through a first, high level interface.
Since there is currently no network support in Rust, this patch series represents a first step to provide access to networking to modules. Sockets are just the highest layer of the network stack: the goal would be to port structures deeper in the network stack, to give modules more access to the network api. However, you need to start somewhere.
Quoted 10 lines
There is not too much use of in kernel sockets. Network file systems like NFS, and SMB are one. These need to be careful with memory usage, you could be busy writing blocks out because the system is low on memory and trying to free some up, and asking for more memory might not work. Sending kernel log messages to a server. But that needs care because of the different contexts it can be used in. Without knowing what it will be used for, it is hard for us the point the special considerations which need to be made.
Quoted 6 lines
Quoted 3 lines
So please also let us see the code using this API.
Andrew
The lack of these abstractions was noticed in the context of a research of the usability of Rust for the development of consensus algorithms using UDP.
O.K, so what are the use cases for consensus algorithms using UDP within the kernel? Where is this code? Ideally you should post it for merging alongside the rust API to sockets its needs. We can then review both together, just as we would if somebody were submitting a new API in C along with its user.
Andrew
Quoted 33 lines · Andrew Lunn
Quoted 26 lines
Quoted 3 lines
There is a long standing tradition in Linux, you don't get a new API merged without a user.
Sorry for not being clear on that.
These abstractions are meant to be used by modules: having them, modules can start using the kernel network functionalities through a first, high level interface.
Since there is currently no network support in Rust, this patch series represents a first step to provide access to networking to modules. Sockets are just the highest layer of the network stack: the goal would be to port structures deeper in the network stack, to give modules more access to the network api. However, you need to start somewhere.
Quoted 10 lines
There is not too much use of in kernel sockets. Network file systems like NFS, and SMB are one. These need to be careful with memory usage, you could be busy writing blocks out because the system is low on memory and trying to free some up, and asking for more memory might not work. Sending kernel log messages to a server. But that needs care because of the different contexts it can be used in. Without knowing what it will be used for, it is hard for us the point the special considerations which need to be made.
Quoted 6 lines
Quoted 3 lines
So please also let us see the code using this API.
Andrew
The lack of these abstractions was noticed in the context of a research of the usability of Rust for the development of consensus algorithms using UDP.
Quoted 7 lines
O.K, so what are the use cases for consensus algorithms using UDP within the kernel? Where is this code? Ideally you should post it for merging alongside the rust API to sockets its needs. We can then review both together, just as we would if somebody were submitting a new API in C along with its user.
Andrew
Hello Andrew,
in the last few days, I had the opportunity to discuss with some people from the RustForLinux community.
I apologize for not being clear: the goal of these APIs was to give some network support to, in particular, out-of-tree modules; they were not meant to be used by a specific module that was planned to get upstreamed as well. The idea behind this patch is that, as of now, Rust is not a viable option for any OOT module that requires even the highest-level network support.
I am wondering whether the net subsystem is interested in reviewing, giving feedback and eventually accepting code that is currently OOT-only.
Also, it would be interesting if you could provide us any module or functionality you are planning to get in-tree which might use this interface; it could be useful in order to understand the applicability of these abstractions and find a concrete in-kernel use-case.
I included in the email a couple of sample OOT modules that showcase the functionalities of the socket API; they will be attached as an actual patch if there will be a v2.
Thank you for your time,
Michele
From 4c966f6ff3acd3b7c27f7a558648722b227d45a7 Mon Sep 17 00:00:00 2001
From: Michele Dalle Rive <[email protected]>
Date: Thu, 17 Aug 2023 15:51:47 +0200
Subject: [PATCH] rust: net: add OOT sample modules.
Add two sample modules that showcase some functionalities of the Rust socket API.
--- samples/rust/rust_socket.rs | 45 +++++++++++++++++++++++++++++++++ samples/rust/rust_tcp_server.rs | 34 +++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 samples/rust/rust_socket.rs create mode 100644 samples/rust/rust_tcp_server.rs
samples/rust/rust_socket.rs new file +25 -0
@@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust minimal sample. + +use core::str::FromStr ; +use kernel::prelude::*; +use kernel::net::socket::*; +use kernel::net::ip::IpProtocol ; +use kernel::net::addr::{SocketAddr }; +use kernel::net::AddressFamily ; +use kernel::flag_set; + +module! { +type :RustSocket , + name:"rust_socket" , + author:"Rust for Linux Contributors" , + description:"Rust sockets support sample" , + license:"GPL" , +} + +struct RustSocket {} + +impl kernel::Module for RustSocket { +fn init (_module: &'staticThisModule ) ->Result <Self > { + let sock = Socket::new(AddressFamily::Inet,
SockType::Datagram, IpProtocol::Udp)?;
+ let addr = "0.0.0.0:8000";
+ sock.bind(SocketAddr::from_str(addr)?)?;
+
+ sock.set_option::<opts::sock::ReuseAddr>(true)?;
+
+ assert_eq!(sock.sockname()?, SocketAddr::from_str(addr)?);
+
+ let mut buf = [0; 1024];
+ while let Ok((bytes, msghdr)) = sock.receive_msg(&mut buf,
flag_set!()) {
+ if bytes == 0 {
+ break;
+ }
+ pr_info!("Received {} bytes from {}", bytes,
msghdr.address().unwrap());
+ if msghdr.flags().contains(flags::MessageFlag::Trunc) {
+ pr_info!("The message was truncated");
+ }
+ }
+ Ok(Self{})
+ }
+}
\ No newline at end of file
samples/rust/rust_tcp_server.rs new file +22 -0
@@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust echo server sample. + +use core::str::FromStr ; +use kernel::prelude::*; +use kernel::net::tcp::TcpListener ; +use kernel::net::addr::SocketAddr ; +use kernel::flag_set; + +module! { +type :RustTcpServer , + name:"rust_tcp_server" , + author:"Rust for Linux Contributors" , + license:"GPL" , +} + +struct RustTcpServer {} + +impl kernel::Module for RustTcpServer { +fn init (_module: &'staticThisModule ) ->Result <Self > { + let listener =
TcpListener::new(SocketAddr::from_str("0.0.0.0:8000")?)?;
+ while let Ok(stream) = listener.accept() {
+ let mut buf = [0; 1024];
+ while let Ok(size) = stream.receive(&mut buf, flag_set!()) {
+ if size == 0 {
+ break;
+ }
+ stream.send(&buf[..size], flag_set!())?;
+ }
+ }
+ Ok(Self {})
+ }
+}
Signature · 1 line
2.41.0
Quoted 2 lines
I am wondering whether the
netsubsystem is interested in reviewing, giving feedback and eventually accepting code that is currently OOT-only.
netdev, and the linux kernel in general, has no interest in code to support out of tree modules. It adds maintenance cost for no gain.
Quoted 4 lines
Also, it would be interesting if you could provide us any module or functionality you are planning to get in-tree which might use this interface; it could be useful in order to understand the applicability of these abstractions and find a concrete in-kernel use-case.
You need real code which does something useful for the community. Your problem is, there is little use of sockets inside the kernel. I did list a few examples in my first reply. Network file systems, logging kernel messages to a remote server. I thought of one more afterwards, there is dhcp client code used for NFS root, which could make use of raw sockets.
However, you have the problem you cannot just rewrite this existing code in Rust because it is core code and needs to work on all architectures. And my understanding is, there are currently not Rust compilers for all architectures.
What you can however do is implement something new, which the kernel does not have. If it never existed, it is hard to complain it is only available for a restricted number of architectures. So maybe look through the RFCs and IETF documents and see if you can find something which is both useful, and makes sense to be implemented in the kernel.
Andrew
Jakub Kicinski
maintainerQuoted 11 lines · On Thu, 17 Aug 2023 16:53:03 +0200 Michele Dalle Rive wrote:
in the last few days, I had the opportunity to discuss with some people from the RustForLinux community.
I apologize for not being clear: the goal of these APIs was to give some network support to, in particular, out-of-tree modules; they were not meant to be used by a specific module that was planned to get upstreamed as well. The idea behind this patch is that, as of now, Rust is not a viable option for any OOT module that requires even the highest-level network support.
I am wondering whether the
netsubsystem is interested in reviewing, giving feedback and eventually accepting code that is currently OOT-only.
This is a bit concerning. You can white out Rust in that and plonk in some corporate backed project people tried to cram into the kernel without understanding the community aspects. I'm not saying it's the same but the tone reads the same.
"The net subsystem" have given "the RustForLinux community" clear guidance on what a good integration starting point is. And now someone else from Rust comes in and talk about supporting OOT modules.
I thought the Rust was just shaking up the languages we use, not the fundamentals on how this project operates :|
Quoted 18 lines · Jakub Kicinski
Quoted 11 lines · On Thu, 17 Aug 2023 16:53:03 +0200 Michele Dalle Rive wrote:
in the last few days, I had the opportunity to discuss with some people from the RustForLinux community.
I apologize for not being clear: the goal of these APIs was to give some network support to, in particular, out-of-tree modules; they were not meant to be used by a specific module that was planned to get upstreamed as well. The idea behind this patch is that, as of now, Rust is not a viable option for any OOT module that requires even the highest-level network support.
I am wondering whether the
netsubsystem is interested in reviewing, giving feedback and eventually accepting code that is currently OOT-only.This is a bit concerning. You can white out Rust in that and plonk in some corporate backed project people tried to cram into the kernel without understanding the community aspects. I'm not saying it's the same but the tone reads the same.
Quoted 6 lines
"The
netsubsystem" have given "the RustForLinux community" clear guidance on what a good integration starting point is. And now someone else from Rust comes in and talk about supporting OOT modules.I thought the Rust was just shaking up the languages we use, not the fundamentals on how this project operates :|
I want to make it clear that Rust is not a way to get anyone a special pass for OOT support. That's never the intention of people in RUST maintainer entries.
Here Michele did a good job on honestly sharing the context of the work.
After the patchset sent, there was a discusion in Rust-for-Linux zulip about why a in-tree user is needed for review (and merge), and how people who are interested in networking could work with netdev on finding useful in-tree users and correct abstraction/bindings. I personally didn't sense anyone trying to undermining the Linux kernel development.
Please understand that "the RustForLinux community" cannot control everyone's mind to make them aware of Linux kernel's contribution policy, but surely there is no execuse not reading:
https://docs.kernel.org/process/development-process.html
before contribution.
Regards,
Boqun
Quoted 6 lines · Jakub Kicinski
This is a bit concerning. You can white out Rust in that and plonk in some corporate backed project people tried to cram into the kernel without understanding the community aspects. I'm not saying it's the same but the tone reads the same.
Quoted 6 lines
"The
netsubsystem" have given "the RustForLinux community" clear guidance on what a good integration starting point is. And now someone else from Rust comes in and talk about supporting OOT modules.I thought the Rust was just shaking up the languages we use, not the fundamentals on how this project operates :|
I am not sure what you mean by "from Rust", but to clarify, Rust for Linux's focus has never been out-of-tree modules.
In fact, the whole effort since the beginning has been about adding support for Rust in-tree, unlike other projects that e.g. linked rustc-built object files into an out-of-tree kernel module. We have also been advising interested parties/newcomers on kernel guidelines etc.
And, of course, the Rust subsystem is definitely not trying to change any fundamentals. If it looks like so, then please note that, like any other subsystem, we may have patch submitters coming from different backgrounds and not part of the Rust subsystem team.
Cheers,
Miguel
Quoted 2 lines · Jakub Kicinski
I thought the Rust was just shaking up the languages we use, not the fundamentals on how this project operates :|
I don't think this little episode has anything to do with Rust, really; certainly we've had no shortage of companies wanting support for out-of-tree modules written in C over the years.
Instead, this is just the sort of thing you see when people who are new to our community learn - the hard way, sometimes - how kernel development works. Rust seems likely to bring in a fair number of new developers, which is great, but we're going to have to help them learn their way around.
Thanks,
jon
Quoted 6 lines · Michele Dalle Rive
The idea behind this patch is that, as of now, Rust is not a viable option for any OOT module that requires even the highest-level network support.
I am wondering whether the
netsubsystem is interested in reviewing, giving feedback and eventually accepting code that is currently OOT-only.
It is unlikely kernel maintainers in general accept code intended for out-of-tree modules only.
To be clear, Rust for Linux's focus has never been out-of-tree modules. In fact, the whole effort since the beginning was about adding support for Rust in-tree, unlike other projects that e.g. linked rustc-built object files into an out-of-tree kernel module.
We do support out-of-tree modules, and have a sample of that, but that is just only to the degree that the kernel supports out-of-tree modules in general.
The abstractions that have been upstreamed so far are those that have (or should soon have) a user in-tree. Sometimes we have had to bend a bit the rules in order to split the dependency chain or make things easier, but abstractions (in general) cannot be upstreamed that do not have at least an expected and public user that is going upstream too. Here, by user, we generally mean an actual driver or useful component (rather than a sample).
If I understood correctly from Zulip, you cannot (right now) show your use case because it is confidential and therefore you cannot upstream it, so we will need another user (and, of course, that is a necessary but not sufficient condition for the code to be accepted).
Quoted 4 lines
Also, it would be interesting if you could provide us any module or functionality you are planning to get in-tree which might use this interface; it could be useful in order to understand the applicability of these abstractions and find a concrete in-kernel use-case.
I think it is easier if it is the other way around :) That is, the people/companies that already have a use case for the abstractions should come forward and publish them so that the abstractions are justified.
Of course, this does not preclude discussing about them or having a rust-net branch or sub-subsystem or similar. That could be quite useful so develop those users and to experiment. In fact, we are actively trying to onboard more people (and companies and other entities) to the Rust overall kernel effort, so please feel free to join us.
Quoted 3 lines
I included in the email a couple of sample OOT modules that showcase the functionalities of the socket API; they will be attached as an actual patch if there will be a v2.
The sample should not be an out-of-tree module, it should be an in-tree one. However, for upstreaming, just a sample is likely to not be enough for most kernel maintainers, as mentioned above.
By the way, I am a bit confused -- the patch seems to add an in-tree sample, not an out-of-tree one.
Cheers,
Miguel
Quoted 4 lines · On Thu, 17 Aug 2023 19:14:19 +0200 Miguel Ojeda…
If I understood correctly from Zulip, you cannot (right now) show your use case because it is confidential and therefore you cannot upstream it, so we will need another user (and, of course, that is a necessary but not sufficient condition for the code to be accepted).
I thought Rust symbols were all export symbol GPL. And therefore any Rust module using these must also be GPL.
Quoted 3 lines · Stephen Hemminger
I thought Rust symbols were all export symbol GPL. And therefore any Rust module using these must also be GPL.
They may be not distributing it just yet, e.g. it may be in development as part of an academic paper, but they may be able to publish later.
Cheers,
Miguel
Quoted 3 lines
If I understood correctly from Zulip, you cannot (right now) show your use case because it is confidential and therefore you cannot upstream it
Is there a clear path for it to become public and mergable?
There is also a general trend that code developed behind closed doors needs a complete re-write when made public and reviewed by mainline developers. And that rewrite could involve the APIs to other subsystems, like sockets. So any API you define now with a 'toy' in kernel user, could in fact be totally useless by the time your real target gets merged.
As Jon pointed out, we kernel maintainers need to help people coming from a different background. Within the community it is well known that internal APIs are unstable. Any developer can change any API, and all its in kernel users if there is a need to do so. This makes maintaining out of tree modules hard. There are a number of 'vendor crap' drivers which do do this, with lots of ugly code to work around differences between kernel versions. To some extent, that works because the C APIs are not that volatile. My guess is, the Rust APIs are going to be a lot more volatile for a number of years while developers figure out what good APIs look like, and rework them again and again, and again. Anybody trying to maintain an out of tree rust module is going to be in a world of pain. In tree however, benefits from the fast that any developer who wants to change an API needs to also change all users...
Andrew
Quoted 29 lines · Miguel Ojeda
Quoted 6 lines · Michele Dalle Rive
The idea behind this patch is that, as of now, Rust is not a viable option for any OOT module that requires even the highest-level network support.
I am wondering whether the
netsubsystem is interested in reviewing, giving feedback and eventually accepting code that is currently OOT-only.It is unlikely kernel maintainers in general accept code intended for out-of-tree modules only.
To be clear, Rust for Linux's focus has never been out-of-tree modules. In fact, the whole effort since the beginning was about adding support for Rust in-tree, unlike other projects that e.g. linked
rustc-built object files into an out-of-tree kernel module.We do support out-of-tree modules, and have a sample of that, but that is just only to the degree that the kernel supports out-of-tree modules in general.
The abstractions that have been upstreamed so far are those that have (or should soon have) a user in-tree. Sometimes we have had to bend a bit the rules in order to split the dependency chain or make things easier, but abstractions (in general) cannot be upstreamed that do not have at least an expected and public user that is going upstream too. Here, by user, we generally mean an actual driver or useful component (rather than a sample).
Quoted 4 lines
If I understood correctly from Zulip, you cannot (right now) show your use case because it is confidential and therefore you cannot upstream it, so we will need another user (and, of course, that is a necessary but not sufficient condition for the code to be accepted).
Correct. I work with Michele, let me clarify. We are a research lab working on a low-jitter networking prototype implemented as an internal LKM (our last paper: https://www.usenix.org/system/files/atc21-jahnke.pdf). When trying to convert it to Rust, we noticed the lack of socket abstractions which Michele implemented. We then simply thought about sharing the abstractions with the community since they could fit a variety of use-cases - hence the patch. We now understand the necessity of a concrete in-tree user, apologies for not realising this earlier: we are indeed new to patch process as you probably understood.
Our prototype might become available in the future but there's no clear path on when, this is our starting point.
Quoted 6 lines
Of course, this does not preclude discussing about them or having a
rust-netbranch or sub-subsystem or similar. That could be quite useful so develop those users and to experiment. In fact, we are actively trying to onboard more people (and companies and other entities) to the Rust overall kernel effort, so please feel free to join us.
We will be more than happy to. In fact, I think the best place for this patch would be in a net branch/subsystem of the Rust for Linux repo. As mentioned in the Zulip chat, it's hard to provide a "full-fledged" patch in Rust at this point as many network building blocks are missing. The result is a number of bindings patches such as this one which do not match maintainers' interests but could be useful for other developers to eventually make a concrete user. It would have helped us significantly when starting this project, I think other researchers/developers share the same view. If anyone is interested, we could add these patches to the mailing list with a low priority tag for feedback.
Quoted 2 lines
By the way, I am a bit confused -- the patch seems to add an in-tree sample, not an out-of-tree one.
It is an in-tree sample, I guess Michele meant an "out-of-tree use-case" as in the sample doesn't introduce new core features in the kernel, just showcasing.
Quoted 5 lines
Correct. I work with Michele, let me clarify. We are a research lab working on a low-jitter networking prototype implemented as an internal LKM (our last paper: https://www.usenix.org/system/files/atc21-jahnke.pdf). When trying to convert it to Rust, we noticed the lack of socket abstractions which Michele implemented.
I guess i need to read the paper, or wait for the code to be made public, but this API on its own does not make much sense to me. Or my assumptions are all wrong.
So you have an application in user space wanting to use this protocol. I assume it is using BSD sockets to communicate with the kernel and the protocol. So you need an API below sockets to get this traffic, i assume a whole new protocol family? But you have an API on top of sockets for TCP/UDP. So i guess your protocol somehow encapsulate the traffic and then uses the API your are proposing to send over TCP or UDP?
Which makes me think:
Why go through sockets twice for a low jitter networking protocol?
How do you avoid deadlocks when you connect the bottom of sockets back to the top of socket?
Andrew
Quoted 3 lines · Andrew Lunn
So you have an application in user space wanting to use this protocol. I assume it is using BSD sockets to communicate with the kernel and the protocol.
No, at the moment it uses procfs or a shared mmap'd chardev buffer.
Quoted 5 lines
So you need an API below sockets to get this
traffic, i assume a whole new protocol family? But you have an API on top of sockets for TCP/UDP. So i guess your protocol somehow encapsulate the traffic and then uses the API your are proposing to send over TCP or UDP?
Yes, we take a message/value from a user space app and send it to other nodes via UDP according to the chosen protocol. Mind that the term "protocol" might be misleading here as it can be confused with classic network protocols. Our API offers distributed services such as failure detectors, consensus etc.
Quoted 3 lines
Which makes me think:
Why go through sockets twice for a low jitter networking protocol?
The idea behind the system is to be split: user space apps are normal apps that can present arbitrary jitter, kernel space services are isolated to provide low jitter. The same applies in the network via a SDN based protocol which prioritises our traffic. By having end-to-end timely communication and processing, we can achieve new efficient distributed services.
Quoted 9 lines · Davide Rovelli
Quoted 3 lines · Andrew Lunn
So you have an application in user space wanting to use this protocol. I assume it is using BSD sockets to communicate with the kernel and the protocol.
No, at the moment it uses procfs or a shared mmap'd chardev buffer.
O.K, so that will never be accepted. There is support for zero copy in sockets. Look at the work Eric did. And you should look at netlink, which might be needed for the control plane.
Quoted 14 lines
Quoted 5 lines
So you need an API below sockets to get this
traffic, i assume a whole new protocol family? But you have an API on top of sockets for TCP/UDP. So i guess your protocol somehow encapsulate the traffic and then uses the API your are proposing to send over TCP or UDP?Yes, we take a message/value from a user space app and send it to other nodes via UDP according to the chosen protocol. Mind that the term "protocol" might be misleading here as it can be confused with classic network protocols. Our API offers distributed services such as failure detectors, consensus etc.
Quoted 2 lines
Which makes me think:
Quoted 8 lines
Quoted 1 line
Why go through sockets twice for a low jitter networking protocol?
The idea behind the system is to be split: user space apps are normal apps that can present arbitrary jitter, kernel space services are isolated to provide low jitter. The same applies in the network via a SDN based protocol which prioritises our traffic. By having end-to-end timely communication and processing, we can achieve new efficient distributed services.
Having the services running in kernel space is going to limit what services you can actually offer, if you need to go through getting them merged every time. If you only have one or two, yes it can be done. But anything general purpose is not going to be practical. The answer might be to write your services using eBPF. They can then be loaded from user space and do pretty much anything which eBPF can do.
There is often a huge step from academic code to production code. Academic code just needs to implement enough to prove the concept. It can take all sort of short cuts. Production code to be merged into the kernel needs to follow the usual development processes, code quality guidelines, and most importantly, architecture.
If you want to make that step to production code, we can help you, but i don't think it makes any sense to merge API code until you have the correct basic architecture. That architecture will define what APIs you need, and sitting on top of sockets might not be correct.
Andrew