Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rwestphal committed Dec 3, 2024
1 parent 6aebe80 commit 1a4e3eb
Show file tree
Hide file tree
Showing 24 changed files with 972 additions and 2,950 deletions.
24 changes: 10 additions & 14 deletions holo-interface/src/ibus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub(crate) async fn process_msg(master: &mut Master, msg: IbusMsg) {
master.interfaces.router_id(),
);
}
IbusMsg::CreateMacVlan(msg) => {
IbusMsg::MacvlanAdd(msg) => {
master
.interfaces
.create_macvlan_interface(
Expand All @@ -68,32 +68,28 @@ pub(crate) async fn process_msg(master: &mut Master, msg: IbusMsg) {
)
.await;
}
IbusMsg::MacvlanDel(ifname) => {
let _ = master
.interfaces
.delete_iface(&master.netlink_handle, ifname)
.await;
}
IbusMsg::InterfaceIpAddRequest(msg) => {
let _ = master
.interfaces
.add_iface_address(
&master.netlink_handle,
msg.ifindex,
msg.addr,
)
.add_iface_address(&master.netlink_handle, msg.ifname, msg.addr)
.await;
}
IbusMsg::InterfaceIpDeleteRequest(msg) => {
IbusMsg::InterfaceIpDelRequest(msg) => {
let _ = master
.interfaces
.delete_iface_address(
&master.netlink_handle,
msg.ifindex,
msg.ifname,
msg.addr,
)
.await;
}
IbusMsg::InterfaceDeleteRequest(ifindex) => {
let _ = master
.interfaces
.delete_iface(&master.netlink_handle, ifindex)
.await;
}
// Ignore other events.
_ => {}
}
Expand Down
18 changes: 12 additions & 6 deletions holo-interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,11 @@ impl Interfaces {
pub(crate) async fn delete_iface(
&self,
netlink_handle: &rtnetlink::Handle,
ifindex: u32,
ifname: String,
) {
if self.get_by_ifindex(ifindex).is_some() {
if let Some(iface) = self.get_by_name(&ifname)
&& let Some(ifindex) = iface.ifindex
{
let _ = netlink::iface_delete(netlink_handle, ifindex).await;
}
}
Expand All @@ -442,21 +444,25 @@ impl Interfaces {
pub(crate) async fn add_iface_address(
&self,
netlink_handle: &rtnetlink::Handle,
ifindex: u32,
ifname: String,
addr: IpNetwork,
) {
if self.get_by_ifindex(ifindex).is_some() {
if let Some(iface) = self.get_by_name(&ifname)
&& let Some(ifindex) = iface.ifindex
{
netlink::addr_install(netlink_handle, ifindex, &addr).await;
}
}

pub(crate) async fn delete_iface_address(
&self,
netlink_handle: &rtnetlink::Handle,
ifindex: u32,
ifname: String,
addr: IpNetwork,
) {
if self.get_by_ifindex(ifindex).is_some() {
if let Some(iface) = self.get_by_name(&ifname)
&& let Some(ifindex) = iface.ifindex
{
netlink::addr_uninstall(netlink_handle, ifindex, &addr).await;
}
}
Expand Down
15 changes: 7 additions & 8 deletions holo-utils/src/ibus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ use crate::policy::{MatchSets, Policy};
use crate::protocol::Protocol;
use crate::southbound::{
AddressMsg, BierNbrInstallMsg, BierNbrUninstallMsg,
InterfaceIpAddRequestMsg, InterfaceIpDeleteRequestMsg, InterfaceUpdateMsg,
LabelInstallMsg, LabelUninstallMsg, MacvlanCreateMsg, RouteKeyMsg,
RouteMsg,
InterfaceIpAddRequestMsg, InterfaceIpDelRequestMsg, InterfaceUpdateMsg,
LabelInstallMsg, LabelUninstallMsg, MacvlanAddMsg, RouteKeyMsg, RouteMsg,
};
use crate::sr::SrCfg;

Expand Down Expand Up @@ -66,18 +65,18 @@ pub enum IbusMsg {
InterfaceAddressAdd(AddressMsg),
// Interface address delete notification.
InterfaceAddressDel(AddressMsg),
// Create a Macvlan Address
CreateMacVlan(MacvlanCreateMsg),
// Request to add an address to an interface.
InterfaceIpAddRequest(InterfaceIpAddRequestMsg),
// Request to delete an address to an interface.
InterfaceIpDeleteRequest(InterfaceIpDeleteRequestMsg),
// Request to delete an interface
InterfaceDeleteRequest(u32),
InterfaceIpDelRequest(InterfaceIpDelRequestMsg),
// Keychain update notification.
KeychainUpd(Arc<Keychain>),
// Keychain delete notification.
KeychainDel(String),
// Create a macvlan interface.
MacvlanAdd(MacvlanAddMsg),
// Delete a macvlan interface.
MacvlanDel(String),
// Nexthop tracking registration.
NexthopTrack(IpAddr),
// Nexthop tracking unregistration.
Expand Down
8 changes: 4 additions & 4 deletions holo-utils/src/southbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub struct InterfaceUpdateMsg {

#[derive(Clone, Debug)]
#[derive(Deserialize, Serialize)]
pub struct MacvlanCreateMsg {
pub struct MacvlanAddMsg {
pub parent_name: String,
pub name: String,
pub mac_address: Option<[u8; 6]>,
Expand All @@ -90,14 +90,14 @@ pub struct MacvlanCreateMsg {
#[derive(Clone, Debug)]
#[derive(Deserialize, Serialize)]
pub struct InterfaceIpAddRequestMsg {
pub ifindex: u32,
pub ifname: String,
pub addr: IpNetwork,
}

#[derive(Clone, Debug)]
#[derive(Deserialize, Serialize)]
pub struct InterfaceIpDeleteRequestMsg {
pub ifindex: u32,
pub struct InterfaceIpDelRequestMsg {
pub ifname: String,
pub addr: IpNetwork,
}

Expand Down
6 changes: 5 additions & 1 deletion holo-vrrp/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
// Sponsored by NLnet as part of the Next Generation Internet initiative.
// See: https://nlnet.nl/NGI0
//

use std::net::Ipv4Addr;

// ==== VRRP ===

// valid vrrp versions
pub const VALID_VRRP_VERSIONS: [u8; 1] = [2];
pub const VRRP_PROTO_NUMBER: i32 = 112;
Expand All @@ -26,7 +28,9 @@ pub const VRRP_MULTICAST_ADDRESS: Ipv4Addr = Ipv4Addr::new(224, 0, 0, 18);
pub const VRRP_IP_COUNT_MAX: usize = 20;

// ==== ARP ====
pub const ARP_PROTOCOL_NUMBER: u16 = 0x806_u16;

pub const ARP_PROTOCOL_NUMBER: u16 = 0x0806;

// ==== IP ====

pub const IP_HDR_MIN: usize = 20;
47 changes: 32 additions & 15 deletions holo-vrrp/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,57 @@
// See: https://nlnet.nl/NGI0
//

use std::net::IpAddr;
use std::net::Ipv4Addr;

use tracing::{debug, debug_span};

use crate::instance::fsm;
use crate::packet::VrrpHdr;

// VRRP debug messages.
#[derive(Debug)]
pub enum Debug<'a> {
InstanceCreate,
InstanceDelete,
// Instances
InstanceCreate(u8),
InstanceDelete(u8),
InstanceStateChange(u8, fsm::State, fsm::State),
// Network
PacketRx(&'a IpAddr, &'a VrrpHdr),
PacketTx(&'a IpAddr, &'a VrrpHdr),
PacketRx(&'a Ipv4Addr, &'a VrrpHdr),
PacketTx(&'a VrrpHdr),
ArpTx(&'a Ipv4Addr),
}

// ===== impl Debug =====

impl Debug<'_> {
// Log debug message using the tracing API.
#[expect(unused)]
pub(crate) fn log(&self) {
match self {
Debug::InstanceCreate | Debug::InstanceDelete => {
// Parent span(s): vrrp-instance
debug!("{}", self);
Debug::InstanceCreate(vrid) | Debug::InstanceDelete(vrid) => {
// Parent span(s): vrrp
debug!(%vrid, "{}", self);
}
Debug::InstanceStateChange(vrid, old_state, new_state) => {
// Parent span(s): vrrp
debug!(%vrid, ?old_state, ?new_state, "{}", self);
}
Debug::PacketRx(src, packet) => {
// Parent span(s): vrrp-instance
// Parent span(s): vrrp
debug_span!("network").in_scope(|| {
debug_span!("input").in_scope(|| {
let data = serde_json::to_string(&packet).unwrap();
debug!(%src, %data, "{}", self);
})
})
}
Debug::PacketTx(addr, packet) => {
// Parent span(s): vrrp-instance:network:output
Debug::PacketTx(packet) => {
// Parent span(s): vrrp:network:output
let data = serde_json::to_string(&packet).unwrap();
debug!(%addr, %data, "{}", self);
debug!(%data, "{}", self);
}
Debug::ArpTx(addr) => {
// Parent span(s): vrrp:network:output
debug!(%addr, "{}", self);
}
}
}
Expand All @@ -55,15 +66,21 @@ impl Debug<'_> {
impl std::fmt::Display for Debug<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Debug::InstanceCreate => {
Debug::InstanceCreate(..) => {
write!(f, "instance created")
}
Debug::InstanceDelete => {
Debug::InstanceDelete(..) => {
write!(f, "instance deleted")
}
Debug::InstanceStateChange(..) => {
write!(f, "instance state change")
}
Debug::PacketRx(..) | Debug::PacketTx(..) => {
write!(f, "packet")
}
Debug::ArpTx(..) => {
write!(f, "gratuitous ARP")
}
}
}
}
Loading

0 comments on commit 1a4e3eb

Please sign in to comment.