From: Benno Lossin <benno.lossin@proton.me>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: netdev@vger.kernel.org, rust-for-linux@vger.kernel.org,
aliceryhl@google.com, andrew@lunn.ch,
miguel.ojeda.sandonis@gmail.com
Subject: Re: [PATCH 2/5] rust: add support for ethernet operations
Date: Thu, 15 Jun 2023 13:03:49 +0000 [thread overview]
Message-ID: <QOy6Y8ZgaRA5ibTXnKAqNpMqJhlV49Jc75QaG-cgmF-MN0brxDFgMbCTIPO8lgZNjxbr3QQZMjTC-Fdl7KMmb6ppcN4Gqs9wvULPHroXWL8=@proton.me> (raw)
In-Reply-To: <20230613045326.3938283-3-fujita.tomonori@gmail.com>
On 6/13/23 06:53, FUJITA Tomonori wrote:
> 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_devicve_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 | 108 ++++++++++++++++++++++++++++++++
> 2 files changed, 109 insertions(+)
>
> 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 d072c81f99ce..d6012b2eea33 100644
> --- a/rust/kernel/net/dev.rs
> +++ b/rust/kernel/net/dev.rs
> @@ -141,6 +141,18 @@ pub fn register(&mut self) -> Result {
> }
> }
>
> + /// Sets `ethtool_ops` of `net_device`.
> + pub fn set_ether_operations<U>(&mut self) -> Result
> + where
> + U: EtherOperations<D>,
> + {
> + if self.is_registered {
> + return Err(code::EINVAL);
> + }
> + EtherOperationsAdapter::<U, D>::new().set_ether_ops(&mut self.dev);
> + Ok(())
> + }
Is it really necessary for a device to be able to have multiple different
`EtherOperations`? Couldnt you also just make `T` implement
`EtherOperations<D>`?
> +
> const DEVICE_OPS: bindings::net_device_ops = bindings::net_device_ops {
> ndo_init: if <T>::HAS_INIT {
> Some(Self::init_callback)
> @@ -342,3 +354,99 @@ fn drop(&mut self) {
> }
> }
> }
> +
> +/// Builds the kernel's `struct ethtool_ops`.
> +struct EtherOperationsAdapter<D, T> {
> + _p: PhantomData<(D, T)>,
> +}
> +
> +impl<D, T> EtherOperationsAdapter<T, D>
I think it would be a good idea to add a comment here that explains why
this is needed, i.e. why `ETHER_OPS` cannot be implemented via a constant
in `set_ether_ops`.
> +where
> + D: DriverData,
> + T: EtherOperations<D>,
> +{
> + /// Creates a new instance.
> + fn new() -> Self {
> + EtherOperationsAdapter { _p: PhantomData }
> + }
> +
> + 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 mut dev = unsafe { Device::from_ptr(netdev) };
> + // SAFETY: The returned pointer was initialized by `D::Data::into_foreign` when
> + // `Registration` object was created.
> + // `D::Data::from_foreign` is only called by the object was released.
> + // So we know `data` is valid while this function is running.
> + let data = unsafe { D::Data::borrow(dev.priv_data_ptr()) };
> + // SAFETY: The C API guarantees that `info` is valid while this function is running.
> + let mut info = unsafe { EthtoolTsInfo::from_ptr(info) };
> + T::get_ts_info(&mut dev, data, &mut info)?;
> + Ok(0)
> + })
> + }
> +
> + const ETHER_OPS: bindings::ethtool_ops = bindings::ethtool_ops {
> + get_ts_info: if <T>::HAS_GET_TS_INFO {
> + Some(Self::get_ts_info_callback)
> + } else {
> + None
> + },
> + ..unsafe { core::mem::MaybeUninit::<bindings::ethtool_ops>::zeroed().assume_init() }
> + };
> +
> + const fn build_ether_ops() -> &'static bindings::ethtool_ops {
> + &Self::ETHER_OPS
> + }
Why is this a function?
> +
> + fn set_ether_ops(&self, dev: &mut Device) {
> + // SAFETY: The type invariants guarantee that `dev.0` is valid.
> + unsafe {
> + (*dev.0).ethtool_ops = Self::build_ether_ops();
> + }
> + }
> +}
> +
> +/// Corresponds to the kernel's `struct ethtool_ops`.
> +#[vtable]
> +pub trait EtherOperations<D: DriverData> {
> + /// Corresponds to `get_ts_info` in `struct ethtool_ops`.
> + fn get_ts_info(
> + _dev: &mut Device,
> + _data: <D::Data as ForeignOwnable>::Borrowed<'_>,
> + _info: &mut EthtoolTsInfo,
`&mut EthtoolTsInfo` is again a double pointer.
> + ) -> Result {
> + Err(Error::from_errno(bindings::EOPNOTSUPP as i32))
> + }
> +}
> +
> +/// 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(dev: &mut Device, info: &mut EthtoolTsInfo) -> Result {
> + // SAFETY: The type invariants guarantee that `dev.0` and `info.0` are valid.
> + unsafe {
> + bindings::ethtool_op_get_ts_info(dev.0, info.0);
> + }
> + Ok(())
> +}
Why isn't this an associated function on `EthtoolTsInfo`?
--
Cheers,
Benno
> --
> 2.34.1
>
next prev parent reply other threads:[~2023-06-15 13:04 UTC|newest]
Thread overview: 67+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-13 4:53 [PATCH 0/5] Rust abstractions for network device drivers FUJITA Tomonori
2023-06-13 4:53 ` [PATCH 1/5] rust: core " FUJITA Tomonori
2023-06-15 13:01 ` Benno Lossin
2023-06-21 13:13 ` FUJITA Tomonori
2023-06-25 9:52 ` Benno Lossin
2023-06-25 14:27 ` FUJITA Tomonori
2023-06-25 17:06 ` Benno Lossin
2023-06-21 22:44 ` Boqun Feng
2023-06-22 0:19 ` FUJITA Tomonori
2023-06-13 4:53 ` [PATCH 2/5] rust: add support for ethernet operations FUJITA Tomonori
2023-06-13 7:19 ` Ariel Miculas
2023-06-15 13:03 ` Benno Lossin [this message]
2023-06-15 13:44 ` Andrew Lunn
2023-06-13 4:53 ` [PATCH 3/5] rust: add support for get_stats64 in struct net_device_ops FUJITA Tomonori
2023-06-13 4:53 ` [PATCH 4/5] rust: add methods for configure net_device FUJITA Tomonori
2023-06-15 13:06 ` Benno Lossin
2023-06-13 4:53 ` [PATCH 5/5] samples: rust: add dummy network driver FUJITA Tomonori
2023-06-15 13:08 ` Benno Lossin
2023-06-22 0:23 ` FUJITA Tomonori
2023-06-15 6:01 ` [PATCH 0/5] Rust abstractions for network device drivers Jakub Kicinski
2023-06-15 8:58 ` Miguel Ojeda
2023-06-16 2:19 ` Jakub Kicinski
2023-06-16 12:18 ` FUJITA Tomonori
2023-06-16 13:23 ` Miguel Ojeda
2023-06-16 13:41 ` FUJITA Tomonori
2023-06-16 18:26 ` Jakub Kicinski
2023-06-16 20:05 ` Miguel Ojeda
2023-06-16 13:04 ` Andrew Lunn
2023-06-16 18:31 ` Jakub Kicinski
2023-06-16 13:18 ` Miguel Ojeda
2023-06-15 12:51 ` Andrew Lunn
2023-06-16 2:02 ` Jakub Kicinski
2023-06-16 3:47 ` Richard Cochran
2023-06-16 17:59 ` Andrew Lunn
2023-06-16 13:02 ` FUJITA Tomonori
2023-06-16 13:14 ` Andrew Lunn
2023-06-16 13:48 ` Miguel Ojeda
2023-06-16 14:43 ` Andrew Lunn
2023-06-16 16:01 ` Miguel Ojeda
2023-06-19 11:27 ` Emilio Cobos Álvarez
2023-06-20 18:09 ` Miguel Ojeda
2023-06-20 19:12 ` Andreas Hindborg (Samsung)
2023-06-21 12:30 ` Andreas Hindborg (Samsung)
2023-06-16 18:40 ` Jakub Kicinski
2023-06-16 19:00 ` Alice Ryhl
2023-06-16 19:10 ` Jakub Kicinski
2023-06-16 19:23 ` Alice Ryhl
2023-06-16 20:04 ` Andrew Lunn
2023-06-17 10:08 ` Alice Ryhl
2023-06-17 10:15 ` Greg KH
2023-06-19 8:50 ` FUJITA Tomonori
2023-06-19 9:46 ` Greg KH
2023-06-19 11:05 ` FUJITA Tomonori
2023-06-19 11:14 ` Greg KH
2023-06-19 13:20 ` Andrew Lunn
2023-06-20 11:16 ` David Laight
2023-06-20 15:47 ` Jakub Kicinski
2023-06-20 16:56 ` Alice Ryhl
2023-06-20 17:44 ` Miguel Ojeda
2023-06-20 17:55 ` Miguel Ojeda
2023-06-16 12:28 ` Alice Ryhl
2023-06-16 13:20 ` Andrew Lunn
2023-06-16 13:24 ` Alice Ryhl
[not found] <20230604021736.3392963-1-tomo@exabit.dev>
2023-06-04 2:17 ` [PATCH 2/5] rust: add support for ethernet operations FUJITA Tomonori
2023-06-04 12:48 ` Alice Ryhl
2023-06-05 14:02 ` FUJITA Tomonori
2023-06-05 23:18 ` FUJITA Tomonori
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='QOy6Y8ZgaRA5ibTXnKAqNpMqJhlV49Jc75QaG-cgmF-MN0brxDFgMbCTIPO8lgZNjxbr3QQZMjTC-Fdl7KMmb6ppcN4Gqs9wvULPHroXWL8=@proton.me' \
--to=benno.lossin@proton.me \
--cc=aliceryhl@google.com \
--cc=andrew@lunn.ch \
--cc=fujita.tomonori@gmail.com \
--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).