Refactor(netlink): Replaces zerocopy with bytemuck

Migrates from `zerocopy` to `bytemuck` for improved data serialization and deserialization in netlink communication. This enhances maintainability.
This commit is contained in:
Nils Andreas Svee committed 2026-02-21 19:09:10 +07:00
1 parent 5838b0b9ee
commit 6a49ad9112
3 files changed
+29 -31

No files matched your search

Generated
+21 -22
View File
@@ -72,6 +72,26 @@ version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af"
[[package]]
name = "bytemuck"
version = "1.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8efb64bd706a16a1bdde310ae86b351e4d21550d98d056f22f8a7f7a2183fec"
dependencies = [
"bytemuck_derive",
]
[[package]]
name = "bytemuck_derive"
version = "1.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9abbd1bc6865053c427f7198e6af43bfdedc55ab791faed4fbd361d789575ff"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "byteorder"
version = "1.5.0"
@@ -766,6 +786,7 @@ name = "sqm-autorate-rust"
version = "0.2.0"
dependencies = [
"anyhow",
"bytemuck",
"icmp-socket",
"log",
"neli",
@@ -774,8 +795,6 @@ dependencies = [
"rustix",
"thiserror",
"time",
"zerocopy",
"zerocopy-derive",
]
[[package]]
@@ -1169,26 +1188,6 @@ dependencies = [
"wasmparser",
]
[[package]]
name = "zerocopy"
version = "0.8.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db6d35d663eadb6c932438e763b262fe1a70987f9ae936e60158176d710cae4a"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4122cd3169e94605190e77839c9a40d40ed048d305bfdc146e7df40ab0f3e517"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "zmij"
version = "1.0.21"
+1 -2
View File
@@ -20,14 +20,13 @@ strip = "symbols"
[dependencies]
anyhow = "1.0"
bytemuck = { version = "1.25.0", features = ["derive"] }
icmp-socket = "0.4.0"
neli = "0.7"
rand = "0.10"
rustix = { version = "1.1", features = ["fs", "thread", "time"] }
rust-uci = { version = "0.1", optional = true }
thiserror = "2.0"
zerocopy = "0.8.39"
zerocopy-derive = "0.8.39"
[dependencies.log]
features = ["std"]
+7 -7
View File
@@ -1,3 +1,5 @@
use bytemuck::AnyBitPattern;
use bytemuck::checked::{CheckedCastError, try_from_bytes};
use neli::consts::nl::NlmF;
use neli::consts::rtnl::{Arphrd, Iff, Ifla, RtAddrFamily, Rtm, Tca};
use neli::consts::socket::NlFamily;
@@ -10,13 +12,11 @@ use neli::utils::Groups;
use std::io;
use std::str::Utf8Error;
use thiserror::Error;
use zerocopy::FromBytes;
use zerocopy_derive::{FromBytes, Immutable, KnownLayout};
#[derive(Debug, Error)]
pub enum NetlinkError {
#[error("Couldn't deserialize to struct: {0}")]
Deserialization(String),
Deserialization(CheckedCastError),
#[error("Couldn't find intreface `{0}`")]
InterfaceNotFound(String),
@@ -52,7 +52,7 @@ pub struct Qdisc {
parent: u32,
}
#[derive(Copy, Clone, Default, Debug, FromBytes, Immutable, KnownLayout)]
#[derive(AnyBitPattern, Copy, Clone, Default, Debug)]
#[repr(C)]
pub struct RtnlLinkStats64 {
pub rx_packets: u64,
@@ -172,10 +172,10 @@ impl Netlink {
for attr in p.rtattrs().iter() {
if attr.rta_type() == &Ifla::Stats64 {
let buf = attr.rta_payload().as_ref();
let stats = RtnlLinkStats64::read_from_bytes(buf)
.map_err(|e| NetlinkError::Deserialization(e.to_string()))?;
let stats: &RtnlLinkStats64 = try_from_bytes::<RtnlLinkStats64>(buf)
.map_err(|e| NetlinkError::Deserialization(e))?;
return Ok(stats);
return Ok(stats.clone());
}
}
}