Skip to content

Commit

Permalink
refactor cont.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwestphal committed Dec 5, 2024
1 parent 1a4e3eb commit 491f785
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 281 deletions.
2 changes: 1 addition & 1 deletion holo-rip/src/ripng/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl NetworkVersion for Ripng {
) -> std::io::Result<()> {
#[cfg(not(feature = "testing"))]
{
socket.set_multicast_if_v6(ifindex)
socket.set_multicast_ifindex_v6(ifindex)
}
#[cfg(feature = "testing")]
{
Expand Down
2 changes: 1 addition & 1 deletion holo-rip/src/ripv2/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl NetworkVersion for Ripv2 {
) -> std::io::Result<()> {
#[cfg(not(feature = "testing"))]
{
socket.set_multicast_if_v4(ifindex)
socket.set_multicast_ifindex_v4(ifindex)
}
#[cfg(feature = "testing")]
{
Expand Down
60 changes: 30 additions & 30 deletions holo-utils/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ pub trait SocketExt: Sized + AsRawFd {
)
}

// Sets the value of the IP_MULTICAST_IF option for this socket.
fn set_multicast_ifindex_v4(&self, ifindex: u32) -> Result<()> {
let optval = ip_mreqn {
imr_multiaddr: libc::in_addr { s_addr: 0 },
imr_address: libc::in_addr { s_addr: 0 },
imr_ifindex: ifindex as i32,
};

setsockopt(
self,
libc::IPPROTO_IP,
libc::IP_MULTICAST_IF,
&optval as *const _ as *const c_void,
std::mem::size_of::<ip_mreqn>() as libc::socklen_t,
)
}

// Sets the value of the IPV6_TCLASS option for this socket.
fn set_ipv6_tclass(&self, dscp: u8) -> Result<()> {
let optval = dscp as c_int;
Expand Down Expand Up @@ -137,6 +154,19 @@ pub trait SocketExt: Sized + AsRawFd {
)
}

// Sets the value of the IPV6_MULTICAST_IF option for this socket.
fn set_multicast_ifindex_v6(&self, ifindex: u32) -> Result<()> {
let optval = ifindex as i32;

setsockopt(
self,
libc::IPPROTO_IPV6,
libc::IPV6_MULTICAST_IF,
&optval as *const _ as *const c_void,
std::mem::size_of::<i32>() as libc::socklen_t,
)
}

// Executes an operation of the PACKET_ADD_MEMBERSHIP type.
fn join_packet_multicast(&self, addr: [u8; 6], ifindex: u32) -> Result<()> {
let mut optval = packet_mreq {
Expand Down Expand Up @@ -242,36 +272,6 @@ pub trait UdpSocketExt: SocketExt {
)
}

// Sets the value of the IP_MULTICAST_IF option for this socket.
fn set_multicast_if_v4(&self, ifindex: u32) -> Result<()> {
let optval = ip_mreqn {
imr_multiaddr: libc::in_addr { s_addr: 0 },
imr_address: libc::in_addr { s_addr: 0 },
imr_ifindex: ifindex as i32,
};

setsockopt(
self,
libc::IPPROTO_IP,
libc::IP_MULTICAST_IF,
&optval as *const _ as *const c_void,
std::mem::size_of::<ip_mreqn>() as libc::socklen_t,
)
}

// Sets the value of the IPV6_MULTICAST_IF option for this socket.
fn set_multicast_if_v6(&self, ifindex: u32) -> Result<()> {
let optval = ifindex as i32;

setsockopt(
self,
libc::IPPROTO_IPV6,
libc::IPV6_MULTICAST_IF,
&optval as *const _ as *const c_void,
std::mem::size_of::<i32>() as libc::socklen_t,
)
}

// Sets the value of the IPV6_MULTICAST_HOPS option for this socket.
fn set_ipv6_multicast_hopcount(&self, hopcount: u8) -> Result<()> {
let optval = hopcount as c_int;
Expand Down
4 changes: 0 additions & 4 deletions holo-vrrp/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ pub const VRRP_MULTICAST_ADDRESS: Ipv4Addr = Ipv4Addr::new(224, 0, 0, 18);
// number of virtual IP addresses that can be on a VRRP header.
pub const VRRP_IP_COUNT_MAX: usize = 20;

// ==== ARP ====

pub const ARP_PROTOCOL_NUMBER: u16 = 0x0806;

// ==== IP ====

pub const IP_HDR_MIN: usize = 20;
6 changes: 3 additions & 3 deletions holo-vrrp/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum Debug<'a> {
// Instances
InstanceCreate(u8),
InstanceDelete(u8),
InstanceStateChange(u8, fsm::State, fsm::State),
InstanceStateChange(u8, fsm::Event, fsm::State, fsm::State),
// Network
PacketRx(&'a Ipv4Addr, &'a VrrpHdr),
PacketTx(&'a VrrpHdr),
Expand All @@ -37,9 +37,9 @@ impl Debug<'_> {
// Parent span(s): vrrp
debug!(%vrid, "{}", self);
}
Debug::InstanceStateChange(vrid, old_state, new_state) => {
Debug::InstanceStateChange(vrid, event, old_state, new_state) => {
// Parent span(s): vrrp
debug!(%vrid, ?old_state, ?new_state, "{}", self);
debug!(%vrid, ?event, ?old_state, ?new_state, "{}", self);
}
Debug::PacketRx(src, packet) => {
// Parent span(s): vrrp
Expand Down
22 changes: 8 additions & 14 deletions holo-vrrp/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@
use std::fmt::Debug;
use std::net::{IpAddr, Ipv4Addr};

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

use crate::packet::DecodeError;

// VRRP errors.
#[derive(Debug)]
pub enum Error {
// I/O errors
IoError(IoError),
// Packet input
InstanceStartError(u8, IoError),
GlobalError(Ipv4Addr, GlobalError),
VirtualRouterError(Ipv4Addr, VirtualRouterError),
}
Expand Down Expand Up @@ -57,8 +55,8 @@ pub enum VirtualRouterError {
impl Error {
pub(crate) fn log(&self) {
match self {
Error::IoError(error) => {
error.log();
Error::InstanceStartError(vrid, error) => {
error!(%vrid, error = %with_source(error), "{}", self);
}
Error::GlobalError(source, error) => {
warn!(?source, %error, "{}", self);
Expand All @@ -73,7 +71,9 @@ impl Error {
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::IoError(error) => std::fmt::Display::fmt(error, f),
Error::InstanceStartError(..) => {
write!(f, "failed to start VRRP instance")
}
Error::GlobalError(_, error) => std::fmt::Display::fmt(error, f),
Error::VirtualRouterError(_, error) => {
std::fmt::Display::fmt(error, f)
Expand All @@ -85,19 +85,13 @@ impl std::fmt::Display for Error {
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::IoError(error) => Some(error),
Error::InstanceStartError(_, error) => Some(error),
Error::GlobalError(_, error) => Some(error),
Error::VirtualRouterError(_, error) => Some(error),
}
}
}

impl From<IoError> for Error {
fn from(error: IoError) -> Error {
Error::IoError(error)
}
}

impl From<(Ipv4Addr, DecodeError)> for Error {
fn from((src, error): (Ipv4Addr, DecodeError)) -> Error {
match error {
Expand Down
3 changes: 2 additions & 1 deletion holo-vrrp/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub(crate) fn process_vrrp_packet(
instance.change_state(
&interface,
fsm::State::Backup,
fsm::Event::HigherPriorityBackup,
MasterReason::NotMaster,
);
}
Expand All @@ -141,12 +142,12 @@ pub(crate) fn handle_master_down_timer(
};

// RFC 3768: Section 6.4.2 ("If the Master_Down_timer fires")
instance.state.last_event = fsm::Event::MasterTimeout;
instance.send_vrrp_advertisement(src_ip);
instance.send_gratuitous_arp();
instance.change_state(
&interface,
fsm::State::Master,
fsm::Event::MasterTimeout,
MasterReason::NoResponse,
);

Expand Down
Loading

0 comments on commit 491f785

Please sign in to comment.