Skip to content

Commit

Permalink
vrrp: migration of action functions.
Browse files Browse the repository at this point in the history
This commit Initiates the migration of
action functions (create packet, send packet etc
) from the Interface struct to the Instance
struct.

Signed-off-by: Paul Wekesa <[email protected]>
  • Loading branch information
Paul-weqe committed Sep 23, 2024
1 parent d715bf5 commit d0a3fe0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
34 changes: 34 additions & 0 deletions holo-vrrp/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use holo_utils::task::{IntervalTask, TimeoutTask};

use crate::interface::MacVlanInterface;
use crate::northbound::configuration::InstanceCfg;
use crate::packet::VrrpPacket;
use crate::tasks::messages::output::NetTxPacketMsg;

#[derive(Debug)]
pub struct Instance {
Expand Down Expand Up @@ -139,6 +141,38 @@ impl Instance {
self.state.skew_time = skew_time;
self.state.master_down_interval = master_down;
}

pub(crate) fn _send_vrrp_advert(&self) {
let packet = self.vrrp_packet();
let msg = NetTxPacketMsg::Vrrp { packet };

if let Some(net) = &self.mac_vlan.net {
let _ = net.net_tx_packetp.send(msg);
}
}

pub(crate) fn vrrp_packet(&self) -> VrrpPacket {
let mut ip_addresses: Vec<Ipv4Addr> = vec![];
for addr in self.config.virtual_addresses.clone() {
ip_addresses.push(addr.ip());
}

let mut packet = VrrpPacket {
version: 2,
hdr_type: 1,
vrid: u8::default(),
priority: self.config.priority,
count_ip: self.config.virtual_addresses.len() as u8,
auth_type: 0,
adver_int: self.config.advertise_interval,
checksum: 0,
ip_addresses,
auth_data: 0,
auth_data2: 0,
};
packet.generate_checksum();
packet
}
}

// ===== impl InstanceState =====
Expand Down
23 changes: 2 additions & 21 deletions holo-vrrp/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//

use std::collections::{BTreeMap, BTreeSet};
use std::net::Ipv4Addr;
use std::sync::Arc;

use async_trait::async_trait;
Expand All @@ -23,7 +22,7 @@ use tokio::sync::mpsc;

use crate::error::{Error, IoError};
use crate::instance::{Instance, State};
use crate::packet::{ArpPacket, EthernetFrame, VrrpPacket};
use crate::packet::{ArpPacket, EthernetFrame};
use crate::tasks::messages::input::{MasterDownTimerMsg, VrrpNetRxPacketMsg};
use crate::tasks::messages::output::NetTxPacketMsg;
use crate::tasks::messages::{ProtocolInputMsg, ProtocolOutputMsg};
Expand Down Expand Up @@ -154,25 +153,7 @@ impl Interface {

pub(crate) fn send_vrrp_advert(&self, vrid: u8) {
if let Some(instance) = self.instances.get(&vrid) {
let mut ip_addresses: Vec<Ipv4Addr> = vec![];
for addr in &instance.config.virtual_addresses {
ip_addresses.push(addr.ip());
}

let mut packet = VrrpPacket {
version: 2,
hdr_type: 1,
vrid: u8::default(),
priority: instance.config.priority,
count_ip: instance.config.virtual_addresses.len() as u8,
auth_type: 0,
adver_int: instance.config.advertise_interval,
checksum: 0,
ip_addresses,
auth_data: 0,
auth_data2: 0,
};
packet.generate_checksum();
let packet = instance.vrrp_packet();
let msg = NetTxPacketMsg::Vrrp { packet };
let _ = self.net.net_tx_packetp.send(msg);
}
Expand Down

0 comments on commit d0a3fe0

Please sign in to comment.