From: Benno Lossin <benno.lossin@proton.me>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: rust-for-linux@vger.kernel.org, netdev@vger.kernel.org,
kuba@kernel.org, andrew@lunn.ch, aliceryhl@google.com,
miguel.ojeda.sandonis@gmail.com
Subject: Re: [PATCH v2 2/5] rust: add support for ethernet operations
Date: Fri, 14 Jul 2023 19:00:53 +0000 [thread overview]
Message-ID: <Gl0dPVYCaKQpn_kU3zHm_f6RyN3M2RhJXwhfZDoV76x8IOiMbTi73XVKG2oYEbve-9Au299c7BnDAG6uUHFE2vxrk_pWCeWH5qzyCau_Ayw=@proton.me> (raw)
In-Reply-To: <20230710073703.147351-3-fujita.tomonori@gmail.com>
> This improves abstractions for network device drivers to implement
> struct ethtool_ops, the majority of ethernet device drivers need to
> do.
>
> struct ethtool_ops also needs to access to device private data like
> struct net_device_ops.
>
> Currently, only get_ts_info operation is supported. The following
> patch adds the Rust version of the dummy network driver, which uses
> the operation.
>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> ---
> rust/bindings/bindings_helper.h | 1 +
> rust/kernel/net/dev.rs | 87 ++++++++++++++++++++++++++++++++-
> 2 files changed, 87 insertions(+), 1 deletion(-)
>
> diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
> index 468bf606f174..6446ff764980 100644
> --- a/rust/bindings/bindings_helper.h
> +++ b/rust/bindings/bindings_helper.h
> @@ -8,6 +8,7 @@
>
> #include <linux/errname.h>
> #include <linux/etherdevice.h>
> +#include <linux/ethtool.h>
> #include <linux/netdevice.h>
> #include <linux/slab.h>
> #include <linux/refcount.h>
> diff --git a/rust/kernel/net/dev.rs b/rust/kernel/net/dev.rs
> index fe20616668a9..ff00616e4fef 100644
> --- a/rust/kernel/net/dev.rs
> +++ b/rust/kernel/net/dev.rs
> @@ -142,7 +142,7 @@ pub fn register(&mut self) -> Result {
> if self.is_registered {
> return Err(code::EINVAL);
> }
> - // SAFETY: The type invariants guarantee that `self.dev.ptr` is valid.
> + // SAFETY: The type invariants of `Device` guarantee that `self.dev.ptr` is valid.
Shouldn't this be squashed into patch 1?
> let ret = unsafe {
> (*self.dev.ptr).netdev_ops = &Self::DEVICE_OPS;
> bindings::register_netdev(self.dev.ptr)
> @@ -155,6 +155,18 @@ pub fn register(&mut self) -> Result {
> }
> }
>
> + /// Sets `ethtool_ops` of `net_device`.
> + pub fn set_ether_operations<E: EtherOperations>(&mut self) -> Result {
> + if self.is_registered {
> + return Err(code::EINVAL);
> + }
> + // SAFETY: The type invariants of `Device` guarantee that `self.dev.ptr` is valid.
> + unsafe {
> + (*(self.dev.ptr)).ethtool_ops = &EtherOperationsAdapter::<E>::ETHER_OPS;
> + }
> + Ok(())
> + }
> +
I think it would make sense to also add
```
pub fn setup_ether_operations(&mut self) -> Result
where
T: EtherOperations
{
self.set_ether_operations::<T>();
}
```
Since normally you would implement `EtherOperations` for the same type
that also implement `DeviceOperations`, right?
> const DEVICE_OPS: bindings::net_device_ops = bindings::net_device_ops {
> ndo_init: if <T>::HAS_INIT {
> Some(Self::init_callback)
> @@ -328,3 +340,76 @@ fn drop(&mut self) {
> build_error!("skb must be released explicitly");
> }
> }
> +
> +/// Builds the kernel's `struct ethtool_ops`.
> +struct EtherOperationsAdapter<E: EtherOperations> {
> + _p: PhantomData<E>,
> +}
> +
> +impl<E: EtherOperations> EtherOperationsAdapter<E> {
> + unsafe extern "C" fn get_ts_info_callback(
> + netdev: *mut bindings::net_device,
> + info: *mut bindings::ethtool_ts_info,
> + ) -> core::ffi::c_int {
> + from_result(|| {
> + // SAFETY: The C API guarantees that `netdev` is valid while this function is running.
> + let dev = unsafe { Device::from_ptr(netdev) };
> + // SAFETY: The C API guarantees that `info` is valid while this function is running.
> + let info = unsafe { EthtoolTsInfo::from_ptr(info) };
> + E::get_ts_info(dev, info)?;
> + Ok(0)
> + })
> + }
> +
> + const ETHER_OPS: bindings::ethtool_ops = bindings::ethtool_ops {
> + get_ts_info: if <E>::HAS_GET_TS_INFO {
> + Some(Self::get_ts_info_callback)
> + } else {
> + None
> + },
> + // SAFETY: The rest is zeroed out to initialize `struct ethtool_ops`,
> + // set `Option<&F>` to be `None`.
> + ..unsafe { core::mem::MaybeUninit::<bindings::ethtool_ops>::zeroed().assume_init() }
> + };
> +}
> +
> +/// Corresponds to the kernel's `struct ethtool_ops`.
> +#[vtable]
> +pub trait EtherOperations: ForeignOwnable + Send + Sync {
> + /// Corresponds to `get_ts_info` in `struct ethtool_ops`.
> + fn get_ts_info(_dev: Device<Self>, _info: EthtoolTsInfo) -> Result {
> + Err(Error::from_errno(bindings::EOPNOTSUPP as i32))
Note that you have to use `-(EOPNOTSUPP as i32)`. Maybe just add this
in `error.rs` via the `declare_err` macro.
--
Cheers,
Benno
> + }
> +}
> +
> +/// Corresponds to the kernel's `struct ethtool_ts_info`.
> +///
> +/// # Invariants
> +///
> +/// The pointer is valid.
> +pub struct EthtoolTsInfo(*mut bindings::ethtool_ts_info);
> +
> +impl EthtoolTsInfo {
> + /// Creates a new `EthtoolTsInfo' instance.
> + ///
> + /// # Safety
> + ///
> + /// Callers must ensure that `ptr` must be valid.
> + unsafe fn from_ptr(ptr: *mut bindings::ethtool_ts_info) -> Self {
> + // INVARIANT: The safety requirements ensure the invariant.
> + Self(ptr)
> + }
> +
> + /// Sets up the info for software timestamping.
> + pub fn ethtool_op_get_ts_info<D: ForeignOwnable + Send + Sync>(
> + dev: &Device<D>,
> + info: &mut EthtoolTsInfo,
> + ) -> Result {
> + // SAFETY: The type invariants of `Device` guarantee that `dev.ptr` are valid.
> + // The type invariants guarantee that `info.0` are valid.
> + unsafe {
> + bindings::ethtool_op_get_ts_info(dev.ptr, info.0);
> + }
> + Ok(())
> + }
> +}
> --
> 2.34.1
>
next prev parent reply other threads:[~2023-07-14 19:01 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-10 7:36 [PATCH v2 0/5] Rust abstractions for network device drivers FUJITA Tomonori
2023-07-10 7:36 ` [PATCH v2 1/5] rust: core " FUJITA Tomonori
2023-07-14 18:59 ` Benno Lossin
2023-07-10 7:37 ` [PATCH v2 2/5] rust: add support for ethernet operations FUJITA Tomonori
2023-07-14 19:00 ` Benno Lossin [this message]
2023-07-10 7:37 ` [PATCH v2 3/5] rust: add methods for configure net_device FUJITA Tomonori
2023-07-14 19:01 ` Benno Lossin
2023-07-10 7:37 ` [PATCH v2 4/5] samples: rust: add dummy network driver FUJITA Tomonori
2023-07-10 7:37 ` [PATCH v2 5/5] MAINTAINERS: add Rust network abstractions files to the NETWORKING DRIVERS entry FUJITA Tomonori
2023-07-10 18:29 ` [PATCH v2 0/5] Rust abstractions for network device drivers Jakub Kicinski
2023-07-10 19:53 ` Greg KH
2023-07-11 10:16 ` FUJITA Tomonori
2023-07-11 13:17 ` Andrew Lunn
2023-07-12 11:45 ` FUJITA Tomonori
[not found] <20230610071848.3722492-1-tomo@exabit.dev>
2023-06-10 7:20 ` [PATCH v2 2/5] rust: add support for ethernet operations FUJITA Tomonori
2023-06-10 16:48 ` Andrew Lunn
2023-06-12 6:51 ` FUJITA Tomonori
2023-06-10 19:14 ` Miguel Ojeda
2023-06-12 8:51 ` FUJITA Tomonori
2023-06-12 13:35 ` Miguel Ojeda
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='Gl0dPVYCaKQpn_kU3zHm_f6RyN3M2RhJXwhfZDoV76x8IOiMbTi73XVKG2oYEbve-9Au299c7BnDAG6uUHFE2vxrk_pWCeWH5qzyCau_Ayw=@proton.me' \
--to=benno.lossin@proton.me \
--cc=aliceryhl@google.com \
--cc=andrew@lunn.ch \
--cc=fujita.tomonori@gmail.com \
--cc=kuba@kernel.org \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=rust-for-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).