diff --git a/holo-vrrp/src/interface.rs b/holo-vrrp/src/interface.rs index 0954e7a7..2d7141e3 100644 --- a/holo-vrrp/src/interface.rs +++ b/holo-vrrp/src/interface.rs @@ -121,7 +121,7 @@ impl Interface { // `mvlan-vrrp{primary-interface-ifindex}{vrid}` let name = format!("mvlan-vrrp-{}", vrid); let mac_address: [u8; 6] = [0x00, 0x00, 0x5e, 0x00, 0x01, vrid]; - southbound::create_macvlan_iface( + southbound::tx::create_macvlan_iface( name.clone(), self.name.clone(), mac_address, // virtual mac address @@ -139,7 +139,7 @@ impl Interface { self.instances.remove(&vrid); if let Some(ifindex) = mvlan_ifindex { - southbound::mvlan_delete(ifindex, &self.tx.ibus); + southbound::tx::mvlan_delete(ifindex, &self.tx.ibus); } } @@ -150,7 +150,7 @@ impl Interface { debug!(%vrid, "state to BACKUP."); if let Some(ifindex) = instance.mac_vlan.system.ifindex { for addr in instance.config.virtual_addresses.clone() { - southbound::addr_del( + southbound::tx::addr_del( ifindex, IpNetwork::V4(addr), &self.tx.ibus, @@ -161,7 +161,7 @@ impl Interface { debug!(%vrid, "state to MASTER."); if let Some(ifindex) = instance.mac_vlan.system.ifindex { for addr in instance.config.virtual_addresses.clone() { - southbound::addr_add( + southbound::tx::addr_add( ifindex, IpNetwork::V4(addr), &self.tx.ibus, @@ -185,7 +185,7 @@ impl Interface { instance.config.virtual_addresses.insert(addr); if let Some(ifindex) = instance.mac_vlan.system.ifindex { - southbound::addr_add( + southbound::tx::addr_add( ifindex, IpNetwork::V4(addr), &self.tx.ibus, @@ -219,7 +219,7 @@ impl Interface { // netlink system call will be initiated to remove the address. // when response is received, this will also be modified in our // system's MacVlan - southbound::addr_del( + southbound::tx::addr_del( ifindex, IpNetwork::V4(addr), &self.tx.ibus, @@ -433,15 +433,15 @@ async fn process_ibus_msg( match msg { // Interface update notification. IbusMsg::InterfaceUpd(msg) => { - southbound::process_iface_update(interface, msg); + southbound::rx::process_iface_update(interface, msg); } // Interface address addition notification. IbusMsg::InterfaceAddressAdd(msg) => { - southbound::process_addr_add(interface, msg); + southbound::rx::process_addr_add(interface, msg); } // Interface address delete notification. IbusMsg::InterfaceAddressDel(msg) => { - southbound::process_addr_del(interface, msg); + southbound::rx::process_addr_del(interface, msg); } // Ignore other events. _ => {} diff --git a/holo-vrrp/src/southbound/mod.rs b/holo-vrrp/src/southbound/mod.rs new file mode 100644 index 00000000..80635437 --- /dev/null +++ b/holo-vrrp/src/southbound/mod.rs @@ -0,0 +1,11 @@ +// +// Copyright (c) The Holo Core Contributors +// +// SPDX-License-Identifier: MIT +// +// Sponsored by NLnet as part of the Next Generation Internet initiative. +// See: https://nlnet.nl/NGI0 +// + +pub mod rx; +pub mod tx; diff --git a/holo-vrrp/src/southbound.rs b/holo-vrrp/src/southbound/rx.rs similarity index 76% rename from holo-vrrp/src/southbound.rs rename to holo-vrrp/src/southbound/rx.rs index bad3c4b6..3ef50da9 100644 --- a/holo-vrrp/src/southbound.rs +++ b/holo-vrrp/src/southbound/rx.rs @@ -6,11 +6,7 @@ use std::collections::BTreeSet; -use holo_utils::ibus::{IbusMsg, IbusSender}; -use holo_utils::southbound::{ - AddressMsg, InterfaceIpAddRequestMsg, InterfaceIpDeleteRequestMsg, - InterfaceUpdateMsg, MacvlanCreateMsg, -}; +use holo_utils::southbound::{AddressMsg, InterfaceUpdateMsg}; use ipnetwork::{IpNetwork, Ipv4Network}; use crate::interface::Interface; @@ -73,6 +69,30 @@ pub(crate) fn process_iface_update( } } +pub(crate) fn process_addr_del(iface: &mut Interface, msg: AddressMsg) { + if msg.ifname != iface.name { + return; + } + + // remove the address from the addresses of parent interfaces + if let IpNetwork::V4(addr) = msg.addr { + iface.system.addresses.remove(&addr); + } + + for (vrid, instance) in iface.instances.iter_mut() { + let name = format!("mvlan-vrrp-{}", vrid); + let mvlan_iface = &mut instance.mac_vlan; + + // if it is one of the macvlans being edited, we + // remove the macvlan's + if mvlan_iface.name == name { + if let IpNetwork::V4(addr) = msg.addr { + mvlan_iface.system.addresses.remove(&addr); + } + } + } +} + pub(crate) fn process_addr_add(iface: &mut Interface, msg: AddressMsg) { if msg.ifname == iface.name { if let IpNetwork::V4(addr) = msg.addr { @@ -103,58 +123,3 @@ pub(crate) fn process_addr_add(iface: &mut Interface, msg: AddressMsg) { iface.reset_timer(vrid); } } - -pub(crate) fn process_addr_del(iface: &mut Interface, msg: AddressMsg) { - if msg.ifname != iface.name { - return; - } - - // remove the address from the addresses of parent interfaces - if let IpNetwork::V4(addr) = msg.addr { - iface.system.addresses.remove(&addr); - } - - for (vrid, instance) in iface.instances.iter_mut() { - let name = format!("mvlan-vrrp-{}", vrid); - let mvlan_iface = &mut instance.mac_vlan; - - // if it is one of the macvlans being edited, we - // remove the macvlan's - if mvlan_iface.name == name { - if let IpNetwork::V4(addr) = msg.addr { - mvlan_iface.system.addresses.remove(&addr); - } - } - } -} - -pub(crate) fn create_macvlan_iface( - name: String, - parent_name: String, - mac_address: [u8; 6], - ibus_tx: &IbusSender, -) { - let msg = MacvlanCreateMsg { - parent_name, - name, - mac_address: Some(mac_address), - }; - let _ = ibus_tx.send(IbusMsg::CreateMacVlan(msg)); -} - -// deletes and interface e.g eth0 entirely -pub(crate) fn mvlan_delete(ifindex: u32, ibus_tx: &IbusSender) { - let _ = ibus_tx.send(IbusMsg::InterfaceDeleteRequest(ifindex)); -} - -// adds an address to an interface -pub(crate) fn addr_add(ifindex: u32, addr: IpNetwork, ibus_tx: &IbusSender) { - let msg = InterfaceIpAddRequestMsg { ifindex, addr }; - let _ = ibus_tx.send(IbusMsg::InterfaceIpAddRequest(msg)); -} - -// removes a specific address from an interface -pub(crate) fn addr_del(ifindex: u32, addr: IpNetwork, ibus_tx: &IbusSender) { - let msg = InterfaceIpDeleteRequestMsg { ifindex, addr }; - let _ = ibus_tx.send(IbusMsg::InterfaceIpDeleteRequest(msg)); -} diff --git a/holo-vrrp/src/southbound/tx.rs b/holo-vrrp/src/southbound/tx.rs new file mode 100644 index 00000000..f4a52dd1 --- /dev/null +++ b/holo-vrrp/src/southbound/tx.rs @@ -0,0 +1,43 @@ +// +// Copyright (c) The Holo Core Contributors +// +// SPDX-License-Identifier: MIT +// + +use holo_utils::{ + ibus::{IbusMsg, IbusSender}, + southbound::{ + InterfaceIpAddRequestMsg, InterfaceIpDeleteRequestMsg, MacvlanCreateMsg, + }, +}; +use ipnetwork::IpNetwork; + +pub(crate) fn create_macvlan_iface( + name: String, + parent_name: String, + mac_address: [u8; 6], + ibus_tx: &IbusSender, +) { + let msg = MacvlanCreateMsg { + parent_name, + name, + mac_address: Some(mac_address), + }; + let _ = ibus_tx.send(IbusMsg::CreateMacVlan(msg)); +} + +pub(crate) fn mvlan_delete(ifindex: u32, ibus_tx: &IbusSender) { + let _ = ibus_tx.send(IbusMsg::InterfaceDeleteRequest(ifindex)); +} + +// adds an address to an interface +pub(crate) fn addr_add(ifindex: u32, addr: IpNetwork, ibus_tx: &IbusSender) { + let msg = InterfaceIpAddRequestMsg { ifindex, addr }; + let _ = ibus_tx.send(IbusMsg::InterfaceIpAddRequest(msg)); +} + +// removes a specific address from an interface +pub(crate) fn addr_del(ifindex: u32, addr: IpNetwork, ibus_tx: &IbusSender) { + let msg = InterfaceIpDeleteRequestMsg { ifindex, addr }; + let _ = ibus_tx.send(IbusMsg::InterfaceIpDeleteRequest(msg)); +}