Skip to content

Bump netlink-packet-utils and netlink-packet-core crates #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

qmonnet
Copy link

@qmonnet qmonnet commented Apr 3, 2025

“Bump netlink-packet-utils, get rid of anyhow, and adjust the code accordingly.”

In practice, the PR is not ready yet; but I could do with some feedback.

What's missing:

  • A newer version for netlink-packet-core
  • It feels like we're losing a lot of context by discarding all error messages when doing the adjustments, so I'm not sure I've been using the right approach (at the same time, I really hope I don't need to redo all adjustments 😅)
  • There are some changes that I had to do on error types to make the code compile, but I don't think they're correct. I'm not sure what the right approach is, though.
  • Two tests break; but that's probably something we want to address last.

Bump netlink-packet-utils, get rid of anyhow, and adjust the code
accordingly.

Signed-off-by: Quentin Monnet <[email protected]>
Comment on lines -135 to +161
.map(|nla| nla.and_then(|nla| InfoBondPort::parse(&nla)))
.map(|nla| {
nla.and_then(|nla| {
InfoBondPort::parse(&nla)
// Placeholder error, as we need to return a NlaError
.map_err(|_| NlaError::InvalidLength { nla_len: 0 })
})
})
.collect::<Result<Vec<_>, _>>()
.map(InfoPortData::BondPort),
InfoPortKind::Bridge => NlasIterator::new(payload)
.map(|nla| nla.and_then(|nla| InfoBridgePort::parse(&nla)))
.map(|nla| {
nla.and_then(|nla| {
InfoBridgePort::parse(&nla)
// Placeholder error, as we need to return a NlaError
.map_err(|_| NlaError::InvalidLength { nla_len: 0 })
})
})
.collect::<Result<Vec<_>, _>>()
.map(InfoPortData::BridgePort),
InfoPortKind::Vrf => NlasIterator::new(payload)
.map(|nla| nla.and_then(|nla| InfoVrfPort::parse(&nla)))
.map(|nla| {
nla.and_then(|nla| {
InfoVrfPort::parse(&nla)
// Placeholder error, as we need to return a NlaError
.map_err(|_| NlaError::InvalidLength { nla_len: 0 })
})
})
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the chunks that I don't believe are the right way to address the changes.

Comment on lines -83 to +85
TCA_ACT_KIND => Some(
parse_string(nla.value())
.context("failed to parse TCA_ACT_KIND"),
),
TCA_ACT_KIND => {
Some(parse_string(nla.value()).map_err(|_| {
// Placeholder error, as we need to return a NlaError
NlaError::InvalidLength { nla_len: 0 }
}))
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one, too.

Comment on lines -730 to +708
match RouteNetlinkMessage::parse_with_param(&buf, header.message_type) {
Err(e) => Err(e),
Ok(message) => Ok(message),
}
RouteNetlinkMessage::parse_with_param(&buf, header.message_type)
.map_err(|_| NlaError::InvalidLength { nla_len: 0 })
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this seems not correct either.

bitflags = "2"
byteorder = "1.3.2"
libc = "0.2.66"
log = { version = "0.4.20", features = ["std"] }
netlink-packet-core = { version = "0.7.0" }
netlink-packet-utils = { version = "0.5.2" }
netlink-packet-core = { git = "https://github.com/rust-netlink/netlink-packet-core.git", rev = "01e8dd1" }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is temporary, obviously.

@qmonnet
Copy link
Author

qmonnet commented Apr 22, 2025

I note there are merge conflicts here. I'm happy to rebase the PR, but I'd welcome some feedback on the approach first.

@cathay4t
Copy link
Member

@qmonnet Losing context will complex the debug process.

Maybe provide a function like Result<T, DecodeError>.context() -> Result<T, DecodeError> could brings minimum changes to netlink-packet-route.

Let me test my theory.

@cathay4t
Copy link
Member

cathay4t commented May 10, 2025

Please do stuff like below in netlink-packet-core (I am also in a favor of merging netlink-packet-utils into netlink-packet-core).

If you find it too complex, let me know, I can take over.

// SPDX-License-Identifier: Apache-2.0

trait ErrorContext {
    fn context(self, msg: &str) -> Self;
}

#[derive(Debug)]
struct DecodeError {
    msg: String,
}

impl ErrorContext for DecodeError {
    fn context(self, msg: &str) -> Self {
        Self {
            msg: format!("{} caused by {}", msg, self.msg),
        }
    }
}

impl<T> ErrorContext for Result<T, DecodeError>
where
    T: Clone,
{
    fn context(self, msg: &str) -> Result<T, DecodeError> {
        match self {
            Ok(t) => Ok(t),
            Err(e) => Err(e.context(msg)),
        }
    }
}

impl std::fmt::Display for DecodeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.msg)
    }
}

impl std::error::Error for DecodeError {}

fn foo(value: bool) -> Result<String, DecodeError> {
    if value {
        Ok("foo".to_string())
    } else {
        Err(DecodeError {
            msg: "foo_error".to_string(),
        })
    }
}

fn main() -> Result<(), DecodeError> {
    foo(false).context("main func error")?;
    Ok(())
}

@qmonnet
Copy link
Author

qmonnet commented May 19, 2025

Thanks! I'll give it a try, when I can find a moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants