Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rwestphal committed Nov 28, 2024
1 parent 6aebe80 commit 1e857aa
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 1,952 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;
5 changes: 1 addition & 4 deletions holo-vrrp/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ pub(crate) fn process_vrrp_packet(
}

// collect the actions that are required
let action = match get_vrrp_action(interface, src_ip, pkt) {
Ok(a) => a,
Err(e) => return Err(e),
};
let action = get_vrrp_action(interface, src_ip, pkt)?;

// execute all collected actions
handle_vrrp_actions(interface, action);
Expand Down
99 changes: 42 additions & 57 deletions holo-vrrp/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use holo_utils::socket::{AsyncFd, Socket};
use holo_utils::southbound::InterfaceFlags;
use holo_utils::task::Task;
use holo_utils::{Receiver, Sender, UnboundedSender};
use ipnetwork::{IpNetwork, Ipv4Network};
use ipnetwork::Ipv4Network;
use tokio::sync::mpsc;
use tracing::{debug, debug_span, error, error_span};

Expand Down Expand Up @@ -56,9 +56,6 @@ pub struct Interface {
#[derive(Debug)]
pub struct MacVlanInterface {
// Interface name.
//
// Macvlan interface naming for VRRP will be in the format:
// `mvlan-vrrp-{vrid}`
pub name: String,
// Interface system data.
pub system: InterfaceSys,
Expand Down Expand Up @@ -127,26 +124,21 @@ impl Interface {

pub(crate) fn start_instance(&mut self, vrid: u8) {
let name = format!("mvlan-vrrp-{}", vrid);
let mac_address: [u8; 6] = [0x00, 0x00, 0x5e, 0x00, 0x01, vrid];
southbound::tx::create_macvlan_iface(
name.clone(),
self.name.clone(),
mac_address, // virtual mac address
let virtual_mac_addr: [u8; 6] = [0x00, 0x00, 0x5e, 0x00, 0x01, vrid];
southbound::tx::mvlan_create(
&self.tx.ibus,
self.name.clone(),
name,
virtual_mac_addr,
);
}

pub(crate) fn delete_instance(&mut self, vrid: u8) {
let mvlan_ifindex: Option<u32>;
if let Some(instance) = self.instances.get(&vrid) {
mvlan_ifindex = instance.mac_vlan.system.ifindex;
} else {
return;
}

self.instances.remove(&vrid);
if let Some(ifindex) = mvlan_ifindex {
southbound::tx::mvlan_delete(ifindex, &self.tx.ibus);
if let Some(instance) = self.instances.remove(&vrid) {
southbound::tx::mvlan_delete(
&self.tx.ibus,
&instance.mac_vlan.name,
);
}
}

Expand All @@ -163,26 +155,22 @@ impl Interface {
instance.state.new_master_reason = new_master_reason;
} else if state == State::Backup {
debug!(%vrid, "state to BACKUP.");
if let Some(ifindex) = instance.mac_vlan.system.ifindex {
for addr in instance.config.virtual_addresses.clone() {
southbound::tx::addr_del(
ifindex,
IpNetwork::V4(addr),
&self.tx.ibus,
);
}
for addr in instance.config.virtual_addresses.clone() {
southbound::tx::ip_addr_del(
&self.tx.ibus,
&instance.mac_vlan.name,
addr,
);
}
instance.state.new_master_reason = new_master_reason;
} else if state == State::Master {
debug!(%vrid, "state to MASTER.");
if let Some(ifindex) = instance.mac_vlan.system.ifindex {
for addr in instance.config.virtual_addresses.clone() {
southbound::tx::addr_add(
ifindex,
IpNetwork::V4(addr),
&self.tx.ibus,
);
}
for addr in instance.config.virtual_addresses.clone() {
southbound::tx::ip_addr_add(
&self.tx.ibus,
&instance.mac_vlan.name,
addr,
);
}
instance.state.new_master_reason = new_master_reason;
}
Expand All @@ -201,13 +189,11 @@ impl Interface {
if let Some(instance) = self.instances.get_mut(&vrid).take() {
instance.config.virtual_addresses.insert(addr);

if let Some(ifindex) = instance.mac_vlan.system.ifindex {
southbound::tx::addr_add(
ifindex,
IpNetwork::V4(addr),
&self.tx.ibus,
);
}
southbound::tx::ip_addr_add(
&self.tx.ibus,
&instance.mac_vlan.name,
addr,
);
self.reset_timer(vrid);
}
}
Expand All @@ -218,19 +204,17 @@ impl Interface {
addr: Ipv4Network,
) {
if let Some(instance) = self.instances.get_mut(&vrid) {
if let Some(ifindex) = instance.mac_vlan.system.ifindex {
// remove address from the instance's configs
instance.config.virtual_addresses.remove(&addr);

// 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::tx::addr_del(
ifindex,
IpNetwork::V4(addr),
&self.tx.ibus,
);
}
// remove address from the instance's configs
instance.config.virtual_addresses.remove(&addr);

// 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::tx::ip_addr_del(
&self.tx.ibus,
&instance.mac_vlan.name,
addr,
);
}
}

Expand Down Expand Up @@ -437,13 +421,14 @@ impl ProtocolInstance for Interface {
}

// ==== impl MacVlanInterface ====

impl MacVlanInterface {
pub(crate) fn is_ready(&self) -> bool {
// return true if the ifindex exists
self.system.ifindex.is_some()
}

pub fn new(vrid: u8) -> Self {
pub(crate) fn new(vrid: u8) -> Self {
let name = format!("mvlan-vrrp-{}", vrid);
Self {
name,
Expand All @@ -466,7 +451,7 @@ impl MvlanInterfaceNet {
})
.map(Arc::new)?;

let socket_vrrp_tx = network::socket_vrrp_tx(parent_iface, vrid)
let socket_vrrp_tx = network::socket_vrrp_tx(&instance.mac_vlan)
.map_err(IoError::SocketError)
.and_then(|socket| {
AsyncFd::new(socket).map_err(IoError::SocketError)
Expand Down
Loading

0 comments on commit 1e857aa

Please sign in to comment.