From ad00284bbf05e20675dbcc94a5fcdb0de937a12e Mon Sep 17 00:00:00 2001 From: weqe Date: Thu, 1 Aug 2024 02:55:31 +0300 Subject: [PATCH] Cargo fmt --- holo-interface/src/interface.rs | 1 - holo-interface/src/netlink.rs | 16 +++++-- holo-vrrp/src/error.rs | 2 +- holo-vrrp/src/events.rs | 85 +++++++++++++++------------------ holo-vrrp/src/instance.rs | 1 - holo-vrrp/src/interface.rs | 36 ++++++-------- holo-vrrp/src/network.rs | 3 +- holo-vrrp/src/tasks.rs | 26 +++++----- 8 files changed, 81 insertions(+), 89 deletions(-) diff --git a/holo-interface/src/interface.rs b/holo-interface/src/interface.rs index 6f4adbbf..b11835cf 100644 --- a/holo-interface/src/interface.rs +++ b/holo-interface/src/interface.rs @@ -211,7 +211,6 @@ impl Interfaces { let iface = &self.arena[iface_idx]; iface.apply_config(ifindex, netlink_handle, self).await; } - } None => { // If the interface does not exist, create a new entry. diff --git a/holo-interface/src/netlink.rs b/holo-interface/src/netlink.rs index 64e72e5b..5548b71e 100644 --- a/holo-interface/src/netlink.rs +++ b/holo-interface/src/netlink.rs @@ -45,7 +45,7 @@ async fn process_newlink_msg( let ifindex = msg.header.index; let mut ifname = None; let mut mtu = None; - let mut mac_address: [u8; 6] = [0u8; 6]; + let mut mac_address: [u8; 6] = [0u8; 6]; let mut flags = InterfaceFlags::empty(); if msg.header.link_layer_type == ARPHRD_LOOPBACK { @@ -61,9 +61,9 @@ async fn process_newlink_msg( Nla::Address(addr) => { mac_address = match addr.try_into() { Ok(a) => a, - Err(e) => [0u8; 6] + Err(e) => [0u8; 6], }; - }, + } _ => (), } } @@ -75,7 +75,15 @@ async fn process_newlink_msg( let ibus_tx = notify.then_some(&master.ibus_tx); master .interfaces - .update(ifname, ifindex, mtu, flags, mac_address, &master.netlink_handle, ibus_tx) + .update( + ifname, + ifindex, + mtu, + flags, + mac_address, + &master.netlink_handle, + ibus_tx, + ) .await; } diff --git a/holo-vrrp/src/error.rs b/holo-vrrp/src/error.rs index 54cae245..5e75341d 100644 --- a/holo-vrrp/src/error.rs +++ b/holo-vrrp/src/error.rs @@ -14,7 +14,7 @@ use tracing::{warn, warn_span}; pub enum Error { // I/O errors IoError(IoError), - InterfaceError(String), + InterfaceError(String), // vrrp-ietf-yang-2018-03-13 specific errors GlobalError(GlobalError), diff --git a/holo-vrrp/src/events.rs b/holo-vrrp/src/events.rs index c1b5d796..ac2a517d 100644 --- a/holo-vrrp/src/events.rs +++ b/holo-vrrp/src/events.rs @@ -15,8 +15,8 @@ use crate::tasks; // To collect actions to be executed later enum Action { - // described in 6.4.1 part 1. Is when the instance owns the - // IP addresses associated with the virtual router + // described in 6.4.1 part 1. Is when the instance owns the + // IP addresses associated with the virtual router Initialize(VrrpPacket), Backup(VrrpPacket), Master(VrrpPacket), @@ -28,29 +28,33 @@ pub(crate) fn process_vrrp_packet( packet: DecodeResult, ) -> Result<(), Error> { // Handle packet decoding errors - let pkt = packet.unwrap(); + let pkt = packet.unwrap(); // collect the actions that are required let mut action = match get_action(interface, pkt) { Ok(a) => a, - Err(e) => return Err(e) + Err(e) => return Err(e), }; - + // execute all collected actions handle_actions(interface, action); Ok(()) } - // gets all the actions that are required to be done bacsed on the interface // configs and incoming packet -fn get_action(interface: &mut Interface, packet: VrrpPacket) -> Result { +fn get_action( + interface: &mut Interface, + packet: VrrpPacket, +) -> Result { // Handle missing instance let instance = match interface.instances.get_mut(&packet.vrid) { Some(inst) => inst, - None => return Err( - Error::InterfaceError(String::from("unable to fetch VRRP instance from interface")) - ), + None => { + return Err(Error::InterfaceError(String::from( + "unable to fetch VRRP instance from interface", + ))) + } }; // Update statistics @@ -62,8 +66,7 @@ fn get_action(interface: &mut Interface, packet: VrrpPacket) -> Result return Ok(Action::Backup(packet)), State::Master => return Ok(Action::Master(packet)), } -} - +} fn handle_actions(interface: &mut Interface, action: Action) { match action { @@ -82,84 +85,76 @@ fn handle_actions(interface: &mut Interface, action: Action) { if let Some(instance) = interface.instances.get_mut(&vrid) { if pkt.priority == 0 { - let duration = Duration::from_secs_f32(instance.state.skew_time); + let duration = + Duration::from_secs_f32(instance.state.skew_time); tasks::set_master_down_timer(interface, vrid, duration); - } - else { + } else { // RFC 3768 Section 6.4.2 // If Preempt Mode if False, or if the priority in the ADVERTISEMENT is // greater than or equal to local priority then: - if (instance.config.preempt == false) - || (pkt.priority > instance.config.priority){ - instance.reset_timer(); + if (instance.config.preempt == false) + || (pkt.priority > instance.config.priority) + { + instance.reset_timer(); } - // drop the packet else { - return + return; } } } - } Action::Master(pkt) => { let vrid = pkt.vrid; let mut send_ad = false; if let Some(instance) = interface.instances.get_mut(&vrid) { - if pkt.priority == 0 { - send_ad = true; + send_ad = true; - instance.reset_timer(); - } - - else if (pkt.priority > instance.config.priority) + } else if (pkt.priority > instance.config.priority) // TODO: in RFC 3768 page 18, we have a requirement, where If the priority // in the ADVERTISEMENT is equal to the local Priority and the primary IP // Address of the sender is greater than the local primary IP Address, then we - // proceed. + // proceed. // // We can get our primary IP address, but purely from the VRRP packet we cannot // get our senders primary. // { interface.change_state(vrid, State::Backup); - } - - else { - return + } else { + return; } } - + if send_ad { interface.send_vrrp_advert(vrid); } - } } } -// ====== Handle Master Down Timer ===== -// This is called when the master down timer fires. -// Basically When the Instance master down timer -// ticks down. +// ====== Handle Master Down Timer ===== +// This is called when the master down timer fires. +// Basically When the Instance master down timer +// ticks down. // -// RFC 3768 : Section 6.4.2 +// RFC 3768 : Section 6.4.2 // 'If the Master_Down_timer fires' pub(crate) fn handle_master_down_timer( - interface: &mut Interface, - vrid: u8 -) -> Result<(), Error>{ + interface: &mut Interface, + vrid: u8, +) -> Result<(), Error> { interface.send_vrrp_advert(vrid); interface.send_gratuitous_arp(vrid); - + let instance: &mut Instance = match interface.instances.get_mut(&vrid) { Some(i) => i, None => { return Err(Error::InterfaceError(String::from( - "unable to get VRRP instance from interface" + "unable to get VRRP instance from interface", ))); } }; @@ -167,5 +162,3 @@ pub(crate) fn handle_master_down_timer( Ok(()) } - - diff --git a/holo-vrrp/src/instance.rs b/holo-vrrp/src/instance.rs index 6094e486..85837a4a 100644 --- a/holo-vrrp/src/instance.rs +++ b/holo-vrrp/src/instance.rs @@ -125,7 +125,6 @@ impl Instance { _ => {} } } - } // ===== impl InstanceState ===== diff --git a/holo-vrrp/src/interface.rs b/holo-vrrp/src/interface.rs index 0107db25..a48645ef 100644 --- a/holo-vrrp/src/interface.rs +++ b/holo-vrrp/src/interface.rs @@ -73,7 +73,7 @@ pub struct InterfaceNet { pub struct ProtocolInputChannelsTx { // Packet Rx event. pub net_packet_rx: Sender, - // Master Down event + // Master Down event pub master_down_timer: Sender, } @@ -121,50 +121,44 @@ impl Interface { } } - pub(crate) fn send_gratuitous_arp(&self, vrid: u8) { - let ifname = &self.name; + pub(crate) fn send_gratuitous_arp(&self, vrid: u8) { + let ifname = &self.name; if let Some(instance) = self.instances.get(&vrid) { - - // send a gratuitous for each of the + // send a gratuitous for each of the // virutal IP addresses for addr in instance.config.virtual_addresses.clone() { - let arp_packet = ArpPacket { - hw_type: 1, - // for Ipv4 + hw_type: 1, + // for Ipv4 proto_type: 0x0800, // mac address length - hw_length: 6, + hw_length: 6, proto_length: 4, operation: 1, // sender hw address is virtual mac. // https://datatracker.ietf.org/doc/html/rfc3768#section-7.3 - sender_hw_address: [0x00, 0x00, 0x5e, 0x00, 0x01, vrid], - sender_proto_address: addr.ip().octets(), + sender_hw_address: [0x00, 0x00, 0x5e, 0x00, 0x01, vrid], + sender_proto_address: addr.ip().octets(), target_hw_address: [0xff, 0xff, 0xff, 0xff, 0xff, 0xff], // broadcast - target_proto_address: addr.ip().octets() + target_proto_address: addr.ip().octets(), }; let mut mac_addr = self.system.mac_address.clone(); let eth_frame = EthernetFrame { ethertype: 0x806, dst_mac: [0xff; 6], - src_mac: mac_addr + src_mac: mac_addr, }; let _ = network::send_packet_arp( - &self.net.socket_arp, - &self.name, - eth_frame, - arp_packet + &self.net.socket_arp, + &self.name, + eth_frame, + arp_packet, ); - } - } } - - } #[async_trait] diff --git a/holo-vrrp/src/network.rs b/holo-vrrp/src/network.rs index a5cd3409..396129db 100644 --- a/holo-vrrp/src/network.rs +++ b/holo-vrrp/src/network.rs @@ -143,8 +143,7 @@ pub(crate) async fn write_loop( while let Some(msg) = net_tx_packetc.recv().await { match msg { NetTxPacketMsg::Vrrp { packet, src, dst } => { - if let Err(error) = - send_packet_vrrp(&socket_vrrp, packet).await + if let Err(error) = send_packet_vrrp(&socket_vrrp, packet).await { error.log(); } diff --git a/holo-vrrp/src/tasks.rs b/holo-vrrp/src/tasks.rs index b97440f2..2da62aa5 100644 --- a/holo-vrrp/src/tasks.rs +++ b/holo-vrrp/src/tasks.rs @@ -69,7 +69,7 @@ pub mod messages { #[derive(Debug, Deserialize, Serialize)] pub struct MasterDownTimerMsg { - pub vrid: u8, + pub vrid: u8, } } @@ -168,24 +168,23 @@ pub(crate) fn net_tx( } // handling the timers... -pub(crate) fn set_timer( interface: &mut Interface, vrid: u8 -) { - if let Some(instance) = interface.instances.get_mut(&vrid){ +pub(crate) fn set_timer(interface: &mut Interface, vrid: u8) { + if let Some(instance) = interface.instances.get_mut(&vrid) { match instance.state.state { crate::instance::State::Initialize => { instance.timer = VrrpTimer::Null; } crate::instance::State::Backup => { - let duration = Duration::from_secs(instance.state.master_down_interval as u64); - set_master_down_timer( - interface, - vrid, - duration + let duration = Duration::from_secs( + instance.state.master_down_interval as u64, ); + set_master_down_timer(interface, vrid, duration); } crate::instance::State::Master => { let timer = IntervalTask::new( - Duration::from_secs(instance.config.advertise_interval as u64), + Duration::from_secs( + instance.config.advertise_interval as u64, + ), true, move || async move { todo!("send VRRP advertisement"); @@ -195,18 +194,19 @@ pub(crate) fn set_timer( interface: &mut Interface, vrid: u8 } } } - } // ==== Set Master Down Timer ==== pub(crate) fn set_master_down_timer( - interface: &mut Interface, vrid: u8, duration: Duration // period: u64 + interface: &mut Interface, + vrid: u8, + duration: Duration, // period: u64 ) { let instance = interface.instances.get_mut(&vrid).unwrap(); let tx = interface.tx.protocol_input.master_down_timer.clone(); let timer = TimeoutTask::new(duration, move || async move { - tx.send(messages::input::MasterDownTimerMsg{ vrid }); + tx.send(messages::input::MasterDownTimerMsg { vrid }); }); instance.timer = VrrpTimer::MasterDownTimer(timer); }