Message ID | 20230710073703.147351-3-fujita.tomonori@gmail.com (mailing list archive) |
---|---|
State | Changes Requested |
Delegated to: | Netdev Maintainers |
Headers | show |
Series | Rust abstractions for network device drivers | expand |
Context | Check | Description |
---|---|---|
netdev/tree_selection | success | Not a local patch, async |
> 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 >
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. 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(()) + } + 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)) + } +} + +/// 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(()) + } +}
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(-)