[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230613045326.3938283-3-fujita.tomonori@gmail.com>
Date: Tue, 13 Jun 2023 13:53:23 +0900
From: FUJITA Tomonori <fujita.tomonori@...il.com>
To: netdev@...r.kernel.org
Cc: rust-for-linux@...r.kernel.org,
aliceryhl@...gle.com,
andrew@...n.ch,
miguel.ojeda.sandonis@...il.com
Subject: [PATCH 2/5] rust: add support for ethernet operations
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@...il.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(())
+ }
+
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>
+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
+ }
+
+ 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,
+ ) -> 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(())
+}
--
2.34.1
Powered by blists - more mailing lists