Skip to content
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

Initialize VRRP errors #24

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 60 additions & 47 deletions holo-vrrp/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@

use std::net::IpAddr;

use tracing::warn;
use tracing::{warn, warn_span};

// BGP errors.
// VRRP errors.
#[derive(Debug)]
pub enum Error {
// I/O errors
IoError(IoError),
// TODO other errors

// other errors
VersionError,
VridError,
AddressListError(Vec<IpAddr>, Vec<IpAddr>),
IntervalError
}

// BGP I/O errors.
// VRRP I/O errors.
#[derive(Debug)]
pub enum IoError {
SocketError(std::io::Error),
MulticastJoinError(IpAddr, std::io::Error),
MulticastLeaveError(IpAddr, std::io::Error),
RecvError(std::io::Error),
RecvMissingSourceAddr,
SendError(std::io::Error),
ChecksumError(std::io::Error),
PacketLengthError(std::io::Error),
IpTtlError(std::io::Error)
}

// ===== impl Error =====
Expand All @@ -34,6 +36,26 @@ impl Error {
match self {
Error::IoError(error) => {
error.log();
},
Error::VersionError => {
warn_span!("virtual_router").in_scope(|| {
warn!("{}", self);
});
},
Error::VridError => {
warn_span!("virtual_router").in_scope(|| {
warn!("{}", self)
});
},
Error::AddressListError(_, _) => {
warn_span!("virtual_router").in_scope(|| {
warn!("{}", self)
});
},
Error::IntervalError => {
warn_span!("virtual_router").in_scope(|| {
warn!("{}", self)
});
}
}
}
Expand All @@ -43,6 +65,18 @@ impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::IoError(error) => error.fmt(f),
Error::VersionError => {
write!(f, "invalid version received")
},
Error::VridError => {
write!(f, "virtual router id(VRID) not matching locally configured")
},
Error::AddressListError(..) => {
write!(f, "received address list not matching local address list")
},
Error::IntervalError => {
write!(f, "received advert interval not matching local configured advert interval")
}
}
}
}
Expand All @@ -67,46 +101,26 @@ impl From<IoError> for Error {
impl IoError {
pub(crate) fn log(&self) {
match self {
IoError::SocketError(error) => {
IoError::ChecksumError(error)
| IoError::PacketLengthError(error)
| IoError::IpTtlError(error) => {
warn!(error = %with_source(error), "{}", self);
}
IoError::MulticastJoinError(addr, error)
| IoError::MulticastLeaveError(addr, error) => {
warn!(?addr, error = %with_source(error), "{}", self);
}
IoError::RecvError(error) | IoError::SendError(error) => {
warn!(error = %with_source(error), "{}", self);
}
IoError::RecvMissingSourceAddr => {
warn!("{}", self);
}
}
}
}

impl std::fmt::Display for IoError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
IoError::SocketError(..) => {
write!(f, "failed to create raw IP socket")
}
IoError::MulticastJoinError(..) => {
write!(f, "failed to join multicast group")
}
IoError::MulticastLeaveError(..) => {
write!(f, "failed to leave multicast group")
}
IoError::RecvError(..) => {
write!(f, "failed to receive IP packet")
}
IoError::RecvMissingSourceAddr => {
write!(
f,
"failed to retrieve source address from received packet"
)
}
IoError::SendError(..) => {
write!(f, "failed to send IP packet")
IoError::ChecksumError(..) => {
write!(f, "invalid VRRP checksum")
},
IoError::PacketLengthError(..) => {
write!(f, "VRRP packet length not reaching minimum 50 bytes")
},
IoError::IpTtlError(..) => {
write!(f, "VRRP pkt TTL must be 255")
}
}
}
Expand All @@ -115,12 +129,11 @@ impl std::fmt::Display for IoError {
impl std::error::Error for IoError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
IoError::SocketError(error)
| IoError::MulticastJoinError(_, error)
| IoError::MulticastLeaveError(_, error)
| IoError::RecvError(error)
| IoError::SendError(error) => Some(error),
_ => None,
IoError::ChecksumError(error)
| IoError::PacketLengthError(error)
| IoError::IpTtlError(error) => {
Some(error)
}
}
}
}
Expand Down
Loading