Skip to content

Commit

Permalink
vrrp: separate southbound modules
Browse files Browse the repository at this point in the history
Separate the southbound into tx.rs and rx.rs
modules. Separation of concerns and also
readability.

Signed-off-by: Paul Wekesa <[email protected]>
  • Loading branch information
Paul-weqe committed Oct 11, 2024
1 parent ce561d5 commit 3155aa1
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 69 deletions.
18 changes: 9 additions & 9 deletions holo-vrrp/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
_ => {}
Expand Down
11 changes: 11 additions & 0 deletions holo-vrrp/src/southbound/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
85 changes: 25 additions & 60 deletions holo-vrrp/src/southbound.rs → holo-vrrp/src/southbound/rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
}
43 changes: 43 additions & 0 deletions holo-vrrp/src/southbound/tx.rs
Original file line number Diff line number Diff line change
@@ -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));
}

0 comments on commit 3155aa1

Please sign in to comment.