diff --git a/src/eeprom/attr.rs b/src/eeprom/attr.rs index 8b16492..891826c 100644 --- a/src/eeprom/attr.rs +++ b/src/eeprom/attr.rs @@ -1,15 +1,12 @@ // SPDX-License-Identifier: MIT -use anyhow::Context; -use byteorder::{ByteOrder, NativeEndian}; -use netlink_packet_utils::{ - nla::{DefaultNla, Nla, NlaBuffer, NlasIterator, NLA_F_NESTED}, - DecodeError, Emitable, Parseable, +use netlink_packet_core::{ + emit_u32, DecodeError, DefaultNla, Emitable, ErrorContext, Nla, NlaBuffer, + NlasIterator, Parseable, NLA_F_NESTED, }; use crate::{EthtoolAttr, EthtoolHeader}; - const ETHTOOL_A_MODULE_EEPROM_HEADER: u16 = 1; const ETHTOOL_A_MODULE_EEPROM_OFFSET: u16 = 2; const ETHTOOL_A_MODULE_EEPROM_LENGTH: u16 = 3; @@ -35,11 +32,8 @@ impl Nla for EthtoolModuleEEPROMAttr { match self { Self::Header(hdrs) => hdrs.as_slice().buffer_len(), Self::Data(data) => data.len(), - Self::Page(_) - | Self::Bank(_) - | Self::I2CAddress(_) => 1, - Self::Offset(_) - | Self::Length(_) => 4, + Self::Page(_) | Self::Bank(_) | Self::I2CAddress(_) => 1, + Self::Offset(_) | Self::Length(_) => 4, Self::Other(attr) => attr.value_len(), } } @@ -61,11 +55,10 @@ impl Nla for EthtoolModuleEEPROMAttr { match self { Self::Header(ref nlas) => nlas.as_slice().emit(buffer), Self::Data(d) => buffer.copy_from_slice(d.as_slice()), - Self::Page(d) - | Self::Bank(d) - | Self::I2CAddress(d) => buffer[0] = *d, - Self::Offset(d) - | Self::Length(d) => NativeEndian::write_u32(buffer, *d), + Self::Page(d) | Self::Bank(d) | Self::I2CAddress(d) => { + buffer[0] = *d + } + Self::Offset(d) | Self::Length(d) => emit_u32(buffer, *d).unwrap(), Self::Other(ref attr) => attr.emit(buffer), } } @@ -79,7 +72,8 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> Ok(match buf.kind() { ETHTOOL_A_MODULE_EEPROM_HEADER => { let mut nlas = Vec::new(); - let error_msg = "failed to parse module eeprom header attributes"; + let error_msg = + "failed to parse module eeprom header attributes"; for nla in NlasIterator::new(payload) { let nla = &nla.context(error_msg)?; let parsed = @@ -88,13 +82,10 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> } Self::Header(nlas) } - ETHTOOL_A_MODULE_EEPROM_DATA => Self::Data( - Vec::from(payload), - ), - kind => Self::Other( - DefaultNla::parse(buf) - .context(format!("invalid ethtool module eeprom NLA kind {kind}"))?, - ), + ETHTOOL_A_MODULE_EEPROM_DATA => Self::Data(Vec::from(payload)), + kind => Self::Other(DefaultNla::parse(buf).context(format!( + "invalid ethtool module eeprom NLA kind {kind}" + ))?), }) } } @@ -104,8 +95,9 @@ pub(crate) fn parse_module_eeprom_nlas( ) -> Result, DecodeError> { let mut nlas = Vec::new(); for nla in NlasIterator::new(buffer) { - let error_msg = - format!("Failed to parse ethtool module eeprom message attribute {nla:?}"); + let error_msg = format!( + "Failed to parse ethtool module eeprom message attribute {nla:?}" + ); let nla = &nla.context(error_msg.clone())?; let parsed = EthtoolModuleEEPROMAttr::parse(nla).context(error_msg)?; nlas.push(EthtoolAttr::ModuleEEPROM(parsed)); diff --git a/src/eeprom/get.rs b/src/eeprom/get.rs index 6a8336c..226ea58 100644 --- a/src/eeprom/get.rs +++ b/src/eeprom/get.rs @@ -16,12 +16,14 @@ pub struct EthtoolModuleEEPROMGetRequest { } impl EthtoolModuleEEPROMGetRequest { - pub(crate) fn new(handle: EthtoolHandle, iface_name: Option<&str>, - offset: u32, - length: u32, - page: u8, - bank: u8, - i2c_address: u8 + pub(crate) fn new( + handle: EthtoolHandle, + iface_name: Option<&str>, + offset: u32, + length: u32, + page: u8, + bank: u8, + i2c_address: u8, ) -> Self { EthtoolModuleEEPROMGetRequest { handle, @@ -30,7 +32,7 @@ impl EthtoolModuleEEPROMGetRequest { length, page, bank, - i2c_address + i2c_address, } } @@ -45,10 +47,17 @@ impl EthtoolModuleEEPROMGetRequest { length, page, bank, - i2c_address + i2c_address, } = self; - let ethtool_msg = EthtoolMessage::new_module_eeprom_get(iface_name.as_deref(), offset, length, page, bank, i2c_address); + let ethtool_msg = EthtoolMessage::new_module_eeprom_get( + iface_name.as_deref(), + offset, + length, + page, + bank, + i2c_address, + ); ethtool_execute(&mut handle, iface_name.is_none(), ethtool_msg).await } } diff --git a/src/eeprom/handle.rs b/src/eeprom/handle.rs index b96f79f..cda4f34 100644 --- a/src/eeprom/handle.rs +++ b/src/eeprom/handle.rs @@ -9,15 +9,25 @@ impl EthtoolModuleEEPROMHandle { EthtoolModuleEEPROMHandle(handle) } - /// Retrieve the module eeprom data pages of a interface (used by `ethtool -m - /// eth1`) - pub fn get(&mut self, iface_name: Option<&str>, - offset: u32, - length: u32, - page: u8, - bank: u8, - i2c_address: u8 + /// Retrieve the module eeprom data pages of a interface (used by `ethtool + /// -m eth1`) + pub fn get( + &mut self, + iface_name: Option<&str>, + offset: u32, + length: u32, + page: u8, + bank: u8, + i2c_address: u8, ) -> EthtoolModuleEEPROMGetRequest { - EthtoolModuleEEPROMGetRequest::new(self.0.clone(), iface_name, offset, length, page, bank, i2c_address) + EthtoolModuleEEPROMGetRequest::new( + self.0.clone(), + iface_name, + offset, + length, + page, + bank, + i2c_address, + ) } } diff --git a/src/handle.rs b/src/handle.rs index 05e0c03..83c7afe 100644 --- a/src/handle.rs +++ b/src/handle.rs @@ -9,9 +9,10 @@ use netlink_packet_core::{ use netlink_packet_generic::GenlMessage; use crate::{ - try_ethtool, EthtoolChannelHandle, EthtoolCoalesceHandle, EthtoolError, EthtoolFeatureHandle, - EthtoolFecHandle, EthtoolLinkModeHandle, EthtoolMessage, EthtoolPauseHandle, EthtoolRingHandle, - EthtoolTsInfoHandle, EthtoolModuleEEPROMHandle + try_ethtool, EthtoolChannelHandle, EthtoolCoalesceHandle, EthtoolError, + EthtoolFeatureHandle, EthtoolFecHandle, EthtoolLinkModeHandle, + EthtoolMessage, EthtoolModuleEEPROMHandle, EthtoolPauseHandle, + EthtoolRingHandle, EthtoolTsInfoHandle, }; #[derive(Clone, Debug)] diff --git a/src/lib.rs b/src/lib.rs index eff2044..bd03001 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,7 +31,10 @@ pub use coalesce::{ #[cfg(feature = "tokio_socket")] pub use connection::new_connection; pub use connection::new_connection_with_socket; -pub use eeprom::{EthtoolModuleEEPROMAttr, EthtoolModuleEEPROMGetRequest, EthtoolModuleEEPROMHandle}; +pub use eeprom::{ + EthtoolModuleEEPROMAttr, EthtoolModuleEEPROMGetRequest, + EthtoolModuleEEPROMHandle, +}; pub use error::EthtoolError; pub use feature::{ EthtoolFeatureAttr, EthtoolFeatureBit, EthtoolFeatureGetRequest, diff --git a/src/message.rs b/src/message.rs index cc9589e..6274cc7 100644 --- a/src/message.rs +++ b/src/message.rs @@ -6,9 +6,9 @@ use netlink_packet_generic::{GenlFamily, GenlHeader}; use crate::{ channel::{parse_channel_nlas, EthtoolChannelAttr}, coalesce::{parse_coalesce_nlas, EthtoolCoalesceAttr}, + eeprom::{parse_module_eeprom_nlas, EthtoolModuleEEPROMAttr}, feature::{parse_feature_nlas, EthtoolFeatureAttr}, fec::{parse_fec_nlas, EthtoolFecAttr}, - eeprom::{parse_module_eeprom_nlas, EthtoolModuleEEPROMAttr}, link_mode::{parse_link_mode_nlas, EthtoolLinkModeAttr}, pause::{parse_pause_nlas, EthtoolPauseAttr}, ring::{parse_ring_nlas, EthtoolRingAttr}, @@ -80,7 +80,9 @@ impl From for u8 { EthtoolCmd::ChannelGetReply => ETHTOOL_MSG_CHANNELS_GET_REPLY, EthtoolCmd::ChannelSet => ETHTOOL_MSG_CHANNELS_SET, EthtoolCmd::ModuleEEPROMGet => ETHTOOL_MSG_MODULE_EEPROM_GET, - EthtoolCmd::ModuleEEPROMGetReply => ETHTOOL_MSG_MODULE_EEPROM_GET_REPLY, + EthtoolCmd::ModuleEEPROMGetReply => { + ETHTOOL_MSG_MODULE_EEPROM_GET_REPLY + } } } } @@ -305,29 +307,43 @@ impl EthtoolMessage { } } - pub fn new_module_eeprom_get( - iface_name: Option<&str>, - offset: u32, - length: u32, - page:u8, - bank:u8, - i2c_address:u8) -> Self { + iface_name: Option<&str>, + offset: u32, + length: u32, + page: u8, + bank: u8, + i2c_address: u8, + ) -> Self { let mut nlas = match iface_name { Some(s) => { - vec![EthtoolAttr::ModuleEEPROM(EthtoolModuleEEPROMAttr::Header(vec![ - EthtoolHeader::DevName(s.to_string()), - ]))] + vec![EthtoolAttr::ModuleEEPROM( + EthtoolModuleEEPROMAttr::Header(vec![ + EthtoolHeader::DevName(s.to_string()), + ]), + )] } None => { - vec![EthtoolAttr::ModuleEEPROM(EthtoolModuleEEPROMAttr::Header(vec![]))] + vec![EthtoolAttr::ModuleEEPROM( + EthtoolModuleEEPROMAttr::Header(vec![]), + )] } }; - nlas.push(EthtoolAttr::ModuleEEPROM(EthtoolModuleEEPROMAttr::Offset(offset))); - nlas.push(EthtoolAttr::ModuleEEPROM(EthtoolModuleEEPROMAttr::Length(length))); - nlas.push(EthtoolAttr::ModuleEEPROM(EthtoolModuleEEPROMAttr::Page(page))); - nlas.push(EthtoolAttr::ModuleEEPROM(EthtoolModuleEEPROMAttr::Bank(bank))); - nlas.push(EthtoolAttr::ModuleEEPROM(EthtoolModuleEEPROMAttr::I2CAddress(i2c_address))); + nlas.push(EthtoolAttr::ModuleEEPROM(EthtoolModuleEEPROMAttr::Offset( + offset, + ))); + nlas.push(EthtoolAttr::ModuleEEPROM(EthtoolModuleEEPROMAttr::Length( + length, + ))); + nlas.push(EthtoolAttr::ModuleEEPROM(EthtoolModuleEEPROMAttr::Page( + page, + ))); + nlas.push(EthtoolAttr::ModuleEEPROM(EthtoolModuleEEPROMAttr::Bank( + bank, + ))); + nlas.push(EthtoolAttr::ModuleEEPROM( + EthtoolModuleEEPROMAttr::I2CAddress(i2c_address), + )); EthtoolMessage { cmd: EthtoolCmd::ModuleEEPROMGet, nlas,