Skip to content

Commit

Permalink
Cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-weqe committed Jul 31, 2024
1 parent c17e8c2 commit ad00284
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 89 deletions.
1 change: 0 additions & 1 deletion holo-interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 12 additions & 4 deletions holo-interface/src/netlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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],
};
},
}
_ => (),
}
}
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion holo-vrrp/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
85 changes: 39 additions & 46 deletions holo-vrrp/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -28,29 +28,33 @@ pub(crate) fn process_vrrp_packet(
packet: DecodeResult<VrrpPacket>,
) -> 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<Action, Error> {
fn get_action(
interface: &mut Interface,
packet: VrrpPacket,
) -> Result<Action, Error> {
// 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
Expand All @@ -62,8 +66,7 @@ fn get_action(interface: &mut Interface, packet: VrrpPacket) -> Result<Action, E
State::Backup => return Ok(Action::Backup(packet)),
State::Master => return Ok(Action::Master(packet)),
}
}

}

fn handle_actions(interface: &mut Interface, action: Action) {
match action {
Expand All @@ -82,90 +85,80 @@ 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",
)));
}
};
interface.change_state(vrid, State::Master);

Ok(())
}


1 change: 0 additions & 1 deletion holo-vrrp/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ impl Instance {
_ => {}
}
}

}

// ===== impl InstanceState =====
Expand Down
36 changes: 15 additions & 21 deletions holo-vrrp/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub struct InterfaceNet {
pub struct ProtocolInputChannelsTx {
// Packet Rx event.
pub net_packet_rx: Sender<NetRxPacketMsg>,
// Master Down event
// Master Down event
pub master_down_timer: Sender<MasterDownTimerMsg>,
}

Expand Down Expand Up @@ -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]
Expand Down
3 changes: 1 addition & 2 deletions holo-vrrp/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
26 changes: 13 additions & 13 deletions holo-vrrp/src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub mod messages {

#[derive(Debug, Deserialize, Serialize)]
pub struct MasterDownTimerMsg {
pub vrid: u8,
pub vrid: u8,
}
}

Expand Down Expand Up @@ -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");
Expand All @@ -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);
}

0 comments on commit ad00284

Please sign in to comment.