From 625e8397c9fb656960bd1cdf20605f4f20c03deb Mon Sep 17 00:00:00 2001 From: Paul Wekesa Date: Thu, 7 Nov 2024 19:53:44 +0300 Subject: [PATCH] ibus: add derive_more crate Add derive_more crate. Simplifies the calling of the IbusMsg messages. Signed-off-by: Paul Wekesa --- Cargo.toml | 1 + holo-bfd/src/master.rs | 2 +- holo-bfd/src/session.rs | 8 +-- holo-bgp/src/northbound/configuration.rs | 9 +-- holo-bgp/src/southbound/tx.rs | 28 ++++---- holo-interface/src/ibus.rs | 29 ++++---- holo-isis/src/interface.rs | 7 +- holo-isis/src/southbound/tx.rs | 4 +- holo-keychain/src/northbound/configuration.rs | 10 +-- holo-ldp/src/northbound/configuration.rs | 13 ++-- holo-ldp/src/southbound/tx.rs | 18 +++-- holo-ospf/src/neighbor.rs | 28 ++++---- holo-ospf/src/northbound/configuration.rs | 11 ++- holo-ospf/src/southbound/tx.rs | 67 ++++++++----------- holo-policy/src/northbound/configuration.rs | 15 ++--- holo-rip/src/northbound/configuration.rs | 13 ++-- holo-rip/src/southbound/tx.rs | 16 ++--- holo-routing/src/ibus.rs | 17 +++-- holo-routing/src/northbound/configuration.rs | 52 +++++++------- holo-system/src/ibus.rs | 4 +- holo-utils/Cargo.toml | 1 + holo-utils/src/ibus.rs | 4 +- 22 files changed, 166 insertions(+), 191 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c3c4b752..8f5ca3a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,7 @@ chrono = { version = "0.4", features = ["serde"] } convert_case = "0.6" criterion = "0.4" crossbeam-channel = "0.5" +derive_more = { version="1.0.0", features = ["from"] } derive-new = "0.5" enum-as-inner = "0.6" fletcher = "1.0" diff --git a/holo-bfd/src/master.rs b/holo-bfd/src/master.rs index 5e82b92e..a0794800 100644 --- a/holo-bfd/src/master.rs +++ b/holo-bfd/src/master.rs @@ -144,7 +144,7 @@ impl ProtocolInstance for Master { async fn init(&mut self) { // Request information about all interfaces. - let _ = self.tx.ibus.send(IbusMsg::Interface(InterfaceMsg::Dump)); + let _ = self.tx.ibus.send(InterfaceMsg::Dump.into()); } async fn process_ibus_msg(&mut self, msg: IbusMsg) { diff --git a/holo-bfd/src/session.rs b/holo-bfd/src/session.rs index bf4942a7..c835258d 100644 --- a/holo-bfd/src/session.rs +++ b/holo-bfd/src/session.rs @@ -15,7 +15,7 @@ use generational_arena::{Arena, Index}; use holo_northbound::yang::control_plane_protocol::bfd; use holo_protocol::InstanceChannelsTx; use holo_utils::bfd::{ClientCfg, ClientId, SessionKey, State}; -use holo_utils::ibus::{BfdSessionMsg, IbusMsg}; +use holo_utils::ibus::BfdSessionMsg; use holo_utils::ip::{IpAddrExt, IpAddrKind}; use holo_utils::socket::{UdpSocket, TTL_MAX}; use holo_utils::task::{IntervalTask, TimeoutTask}; @@ -141,11 +141,11 @@ impl Session { // Notify protocol clients about the state transition if necessary. if self.should_notify_clients(old_state) && !self.clients.is_empty() { - let msg = IbusMsg::BfdSession(BfdSessionMsg::Update { + let msg = BfdSessionMsg::Update { sess_key: self.key.clone(), state, - }); - let _ = tx.ibus.send(msg); + }; + let _ = tx.ibus.send(msg.into()); } // Send YANG notification. diff --git a/holo-bgp/src/northbound/configuration.rs b/holo-bgp/src/northbound/configuration.rs index ed5246f2..61c8e256 100644 --- a/holo-bgp/src/northbound/configuration.rs +++ b/holo-bgp/src/northbound/configuration.rs @@ -18,7 +18,7 @@ use holo_northbound::configuration::{ }; use holo_northbound::yang::control_plane_protocol::bgp; use holo_utils::bgp::AfiSafi; -use holo_utils::ibus::{IbusMsg, RouteRedistributeMsg}; +use holo_utils::ibus::RouteRedistributeMsg; use holo_utils::ip::{AddressFamily, IpAddrKind}; use holo_utils::policy::{ApplyPolicyCfg, DefaultPolicyType}; use holo_utils::protocol::Protocol; @@ -1363,12 +1363,13 @@ impl Provider for Instance { } } Event::RedistributeRequest(protocol, af) => { - let _ = self.tx.ibus.send(IbusMsg::RouteRedistribute( + let _ = self.tx.ibus.send( RouteRedistributeMsg::Dump { protocol, af: Some(af), - }, - )); + } + .into(), + ); } Event::RedistributeDelete(protocol, afi_safi) => { let Some((mut instance, _)) = self.as_up() else { diff --git a/holo-bgp/src/southbound/tx.rs b/holo-bgp/src/southbound/tx.rs index 9cc39e8e..f1c9f05b 100644 --- a/holo-bgp/src/southbound/tx.rs +++ b/holo-bgp/src/southbound/tx.rs @@ -7,9 +7,7 @@ use std::collections::BTreeSet; use std::net::IpAddr; -use holo_utils::ibus::{ - IbusMsg, IbusSender, NexthopMsg, RouteIpMsg, RouterIdMsg, -}; +use holo_utils::ibus::{IbusSender, NexthopMsg, RouteIpMsg, RouterIdMsg}; use holo_utils::protocol::Protocol; use holo_utils::southbound::{ Nexthop, RouteKeyMsg, RouteMsg, RouteOpaqueAttrs, @@ -21,7 +19,7 @@ use crate::rib::LocalRoute; // ===== global functions ===== pub(crate) fn router_id_query(ibus_tx: &IbusSender) { - let _ = ibus_tx.send(IbusMsg::RouterId(RouterIdMsg::Query)); + let _ = ibus_tx.send(RouterIdMsg::Query.into()); } pub(crate) fn route_install( @@ -43,7 +41,7 @@ pub(crate) fn route_install( .collect::>(); // Install route. - let msg = RouteMsg { + let msg = RouteIpMsg::Add(RouteMsg { protocol: Protocol::BGP, prefix: prefix.into(), distance: distance.into(), @@ -51,9 +49,8 @@ pub(crate) fn route_install( tag: None, opaque_attrs: RouteOpaqueAttrs::None, nexthops: nexthops.clone(), - }; - let msg = IbusMsg::RouteIp(RouteIpMsg::Add(msg)); - let _ = ibus_tx.send(msg); + }); + let _ = ibus_tx.send(msg.into()); } pub(crate) fn route_uninstall( @@ -61,20 +58,19 @@ pub(crate) fn route_uninstall( prefix: impl Into, ) { // Uninstall route. - let msg = RouteKeyMsg { + let msg = RouteIpMsg::Delete(RouteKeyMsg { protocol: Protocol::BGP, prefix: prefix.into(), - }; - let msg = IbusMsg::RouteIp(RouteIpMsg::Delete(msg)); - let _ = ibus_tx.send(msg); + }); + let _ = ibus_tx.send(msg.into()); } pub(crate) fn nexthop_track(ibus_tx: &IbusSender, addr: IpAddr) { - let msg = IbusMsg::Nexthop(NexthopMsg::Track(addr)); - let _ = ibus_tx.send(msg); + let msg = NexthopMsg::Track(addr); + let _ = ibus_tx.send(msg.into()); } pub(crate) fn nexthop_untrack(ibus_tx: &IbusSender, addr: IpAddr) { - let msg = IbusMsg::Nexthop(NexthopMsg::Untrack(addr)); - let _ = ibus_tx.send(msg); + let msg = NexthopMsg::Untrack(addr); + let _ = ibus_tx.send(msg.into()); } diff --git a/holo-interface/src/ibus.rs b/holo-interface/src/ibus.rs index a2683baf..784ddb1d 100644 --- a/holo-interface/src/ibus.rs +++ b/holo-interface/src/ibus.rs @@ -74,8 +74,8 @@ pub(crate) fn notify_router_id_update( ibus_tx: &IbusSender, router_id: Option, ) { - let msg = IbusMsg::RouterId(RouterIdMsg::Update(router_id)); - notify(ibus_tx, msg); + let msg = RouterIdMsg::Update(router_id); + notify(ibus_tx, msg.into()); } pub(crate) fn notify_interface_update(ibus_tx: &IbusSender, iface: &Interface) { @@ -86,14 +86,14 @@ pub(crate) fn notify_interface_update(ibus_tx: &IbusSender, iface: &Interface) { flags: iface.flags, mac_address: iface.mac_address, }; - let msg = IbusMsg::Interface(InterfaceMsg::Update(update_msg)); + let msg = InterfaceMsg::Update(update_msg); - notify(ibus_tx, msg); + notify(ibus_tx, msg.into()); } pub(crate) fn notify_interface_del(ibus_tx: &IbusSender, ifname: String) { - let msg = IbusMsg::Interface(InterfaceMsg::Delete(ifname)); - notify(ibus_tx, msg); + let msg = InterfaceMsg::Delete(ifname); + notify(ibus_tx, msg.into()); } pub(crate) fn notify_addr_add( @@ -107,8 +107,8 @@ pub(crate) fn notify_addr_add( addr, flags, }; - let msg = IbusMsg::InterfaceAddress(InterfaceAddressMsg::Add(addr_msg)); - notify(ibus_tx, msg); + let msg = InterfaceAddressMsg::Add(addr_msg); + notify(ibus_tx, msg.into()); } pub(crate) fn notify_addr_del( @@ -117,13 +117,12 @@ pub(crate) fn notify_addr_del( addr: IpNetwork, flags: AddressFlags, ) { - let msg = - IbusMsg::InterfaceAddress(InterfaceAddressMsg::Delete(AddressMsg { - ifname, - addr, - flags, - })); - notify(ibus_tx, msg); + let msg = InterfaceAddressMsg::Delete(AddressMsg { + ifname, + addr, + flags, + }); + notify(ibus_tx, msg.into()); } // ===== helper functions ===== diff --git a/holo-isis/src/interface.rs b/holo-isis/src/interface.rs index e8034c2c..44270e21 100644 --- a/holo-isis/src/interface.rs +++ b/holo-isis/src/interface.rs @@ -13,7 +13,7 @@ use std::sync::Arc; use chrono::{DateTime, Utc}; use holo_protocol::InstanceChannelsTx; -use holo_utils::ibus::{IbusMsg, IbusSender, InterfaceMsg}; +use holo_utils::ibus::{IbusSender, InterfaceMsg}; use holo_utils::ip::AddressFamily; use holo_utils::socket::{AsyncFd, Socket, SocketExt}; use holo_utils::southbound::InterfaceFlags; @@ -643,10 +643,11 @@ impl Interface { // Sends a southbound request for interface system information, such as // operational status and IP addresses. pub(crate) fn query_southbound(&self, ibus_tx: &IbusSender) { - let _ = ibus_tx.send(IbusMsg::Interface(InterfaceMsg::Query { + let msg = InterfaceMsg::Query { ifname: self.name.clone(), af: None, - })); + }; + let _ = ibus_tx.send(msg.into()); } } diff --git a/holo-isis/src/southbound/tx.rs b/holo-isis/src/southbound/tx.rs index fe328d33..dff62b1d 100644 --- a/holo-isis/src/southbound/tx.rs +++ b/holo-isis/src/southbound/tx.rs @@ -7,10 +7,10 @@ // See: https://nlnet.nl/NGI0 // -use holo_utils::ibus::{IbusMsg, IbusSender, RouterIdMsg}; +use holo_utils::ibus::{IbusSender, RouterIdMsg}; // ===== global functions ===== pub(crate) fn router_id_query(ibus_tx: &IbusSender) { - let _ = ibus_tx.send(IbusMsg::RouterId(RouterIdMsg::Query)); + let _ = ibus_tx.send(RouterIdMsg::Query.into()); } diff --git a/holo-keychain/src/northbound/configuration.rs b/holo-keychain/src/northbound/configuration.rs index 753497c9..ba45625c 100644 --- a/holo-keychain/src/northbound/configuration.rs +++ b/holo-keychain/src/northbound/configuration.rs @@ -15,7 +15,7 @@ use holo_northbound::configuration::{ }; use holo_northbound::yang::key_chains; use holo_utils::crypto::CryptoAlgo; -use holo_utils::ibus::{IbusMsg, KeychainMsg}; +use holo_utils::ibus::KeychainMsg; use holo_utils::keychain::{Key, Keychain, KeychainKey}; use holo_utils::yang::DataNodeRefExt; use holo_yang::TryFromYang; @@ -443,13 +443,13 @@ impl Provider for Master { let keychain = Arc::new(keychain.clone()); // Notify protocols that the keychain has been updated. - let msg = IbusMsg::Keychain(KeychainMsg::Update(keychain)); - let _ = self.ibus_tx.send(msg); + let msg = KeychainMsg::Update(keychain); + let _ = self.ibus_tx.send(msg.into()); } Event::KeychainDelete(name) => { // Notify protocols that the keychain has been deleted. - let msg = IbusMsg::Keychain(KeychainMsg::Delete(name)); - let _ = self.ibus_tx.send(msg); + let msg = KeychainMsg::Delete(name); + let _ = self.ibus_tx.send(msg.into()); } } } diff --git a/holo-ldp/src/northbound/configuration.rs b/holo-ldp/src/northbound/configuration.rs index 40236be8..a040ed1e 100644 --- a/holo-ldp/src/northbound/configuration.rs +++ b/holo-ldp/src/northbound/configuration.rs @@ -15,7 +15,7 @@ use holo_northbound::configuration::{ ValidationCallbacksBuilder, }; use holo_northbound::yang::control_plane_protocol::mpls_ldp; -use holo_utils::ibus::{IbusMsg, InterfaceMsg}; +use holo_utils::ibus::InterfaceMsg; use holo_utils::ip::AddressFamily; use holo_utils::yang::DataNodeRefExt; @@ -471,12 +471,11 @@ impl Provider for Instance { } Event::InterfaceQuerySouthbound(ifname) => { if let Some((instance, _, _)) = self.as_up() { - let _ = instance.tx.ibus.send(IbusMsg::Interface( - InterfaceMsg::Query { - ifname, - af: Some(AddressFamily::Ipv4), - }, - )); + let msg = InterfaceMsg::Query { + ifname, + af: Some(AddressFamily::Ipv4), + }; + let _ = instance.tx.ibus.send(msg.into()); } } Event::TargetedNbrUpdate(tnbr_idx) => { diff --git a/holo-ldp/src/southbound/tx.rs b/holo-ldp/src/southbound/tx.rs index 7632ca7f..cacb0379 100644 --- a/holo-ldp/src/southbound/tx.rs +++ b/holo-ldp/src/southbound/tx.rs @@ -4,7 +4,7 @@ // SPDX-License-Identifier: MIT // -use holo_utils::ibus::{IbusMsg, IbusSender, RouteMplsMsg, RouterIdMsg}; +use holo_utils::ibus::{IbusSender, RouteMplsMsg, RouterIdMsg}; use holo_utils::protocol::Protocol; use holo_utils::southbound::{self, LabelInstallMsg, LabelUninstallMsg}; @@ -13,7 +13,7 @@ use crate::fec::{FecInner, Nexthop}; // ===== global functions ===== pub(crate) fn router_id_query(ibus_tx: &IbusSender) { - let _ = ibus_tx.send(IbusMsg::RouterId(RouterIdMsg::Query)); + let _ = ibus_tx.send(RouterIdMsg::Query.into()); } pub(crate) fn label_install( @@ -35,7 +35,7 @@ pub(crate) fn label_install( let protocol = fec.protocol.unwrap(); // Fill-in message. - let msg = LabelInstallMsg { + let msg = RouteMplsMsg::Add(LabelInstallMsg { protocol: Protocol::LDP, label: local_label, nexthops: [southbound::Nexthop::Address { @@ -46,11 +46,10 @@ pub(crate) fn label_install( .into(), route: Some((protocol, *fec.prefix)), replace: false, - }; + }); // Send message. - let msg = IbusMsg::RouteMpls(RouteMplsMsg::Add(msg)); - let _ = ibus_tx.send(msg); + let _ = ibus_tx.send(msg.into()); } pub(crate) fn label_uninstall( @@ -72,7 +71,7 @@ pub(crate) fn label_uninstall( let protocol = fec.protocol.unwrap(); // Fill-in message. - let msg = LabelUninstallMsg { + let msg = RouteMplsMsg::Delete(LabelUninstallMsg { protocol: Protocol::LDP, label: local_label, nexthops: [southbound::Nexthop::Address { @@ -82,9 +81,8 @@ pub(crate) fn label_uninstall( }] .into(), route: Some((protocol, *fec.prefix)), - }; + }); // Send message. - let msg = IbusMsg::RouteMpls(RouteMplsMsg::Delete(msg)); - let _ = ibus_tx.send(msg); + let _ = ibus_tx.send(msg.into()); } diff --git a/holo-ospf/src/neighbor.rs b/holo-ospf/src/neighbor.rs index cf34b842..08729696 100644 --- a/holo-ospf/src/neighbor.rs +++ b/holo-ospf/src/neighbor.rs @@ -10,7 +10,7 @@ use std::sync::Arc; use chrono::{DateTime, Utc}; use holo_utils::bfd; -use holo_utils::ibus::IbusMsg; +use holo_utils::ibus::BfdSessionMsg; use holo_utils::task::{IntervalTask, TimeoutTask}; use nsm::{Event, State}; use rand::RngCore; @@ -553,14 +553,12 @@ where ) { Debug::::NeighborBfdReg(self.router_id).log(); - let msg = IbusMsg::BfdSession( - holo_utils::ibus::BfdSessionMsg::Registration { - sess_key: self.bfd_session_key(iface), - client_id: self.bfd_client_id(instance), - client_config: Some(iface.config.bfd_params), - }, - ); - let _ = instance.tx.ibus.send(msg); + let msg = BfdSessionMsg::Registration { + sess_key: self.bfd_session_key(iface), + client_id: self.bfd_client_id(instance), + client_config: Some(iface.config.bfd_params), + }; + let _ = instance.tx.ibus.send(msg.into()); } pub(crate) fn bfd_unregister( @@ -570,13 +568,11 @@ where ) { Debug::::NeighborBfdUnreg(self.router_id).log(); - let msg = IbusMsg::BfdSession( - holo_utils::ibus::BfdSessionMsg::Unregistration { - sess_key: self.bfd_session_key(iface), - client_id: self.bfd_client_id(instance), - }, - ); - let _ = instance.tx.ibus.send(msg); + let msg = BfdSessionMsg::Unregistration { + sess_key: self.bfd_session_key(iface), + client_id: self.bfd_client_id(instance), + }; + let _ = instance.tx.ibus.send(msg.into()); } fn bfd_session_key(&self, iface: &Interface) -> bfd::SessionKey { diff --git a/holo-ospf/src/northbound/configuration.rs b/holo-ospf/src/northbound/configuration.rs index f24f1cf9..94287939 100644 --- a/holo-ospf/src/northbound/configuration.rs +++ b/holo-ospf/src/northbound/configuration.rs @@ -1448,12 +1448,11 @@ where } Event::InterfaceQuerySouthbound(ifname, af) => { if self.is_active() { - let _ = self.tx.ibus.send(IbusMsg::Interface( - InterfaceMsg::Query { - ifname, - af: Some(af), - }, - )); + let msg = InterfaceMsg::Query { + ifname, + af: Some(af), + }; + let _ = self.tx.ibus.send(msg.into()); } } Event::StubRouterChange => { diff --git a/holo-ospf/src/southbound/tx.rs b/holo-ospf/src/southbound/tx.rs index b106abd7..b16ff5f3 100644 --- a/holo-ospf/src/southbound/tx.rs +++ b/holo-ospf/src/southbound/tx.rs @@ -8,7 +8,7 @@ use std::collections::BTreeSet; use std::net::IpAddr; use holo_utils::ibus::{ - IbusMsg, IbusSender, RouteBierMsg, RouteIpMsg, RouteMplsMsg, RouterIdMsg, + IbusSender, RouteBierMsg, RouteIpMsg, RouteMplsMsg, RouterIdMsg, }; use holo_utils::mpls::Label; use holo_utils::southbound::{ @@ -24,7 +24,7 @@ use crate::version::Version; // ===== global functions ===== pub(crate) fn router_id_query(ibus_tx: &IbusSender) { - let _ = ibus_tx.send(IbusMsg::RouterId(RouterIdMsg::Query)); + let _ = ibus_tx.send(RouterIdMsg::Query.into()); } pub(crate) fn route_install( @@ -63,7 +63,7 @@ pub(crate) fn route_install( .collect::>(); // Install route. - let msg = RouteMsg { + let msg = RouteIpMsg::Add(RouteMsg { protocol: V::PROTOCOL, prefix: (*destination).into(), distance: distance.into(), @@ -73,46 +73,42 @@ pub(crate) fn route_install( route_type: route.path_type, }, nexthops: nexthops.clone(), - }; - let msg = IbusMsg::RouteIp(RouteIpMsg::Add(msg)); - let _ = ibus_tx.send(msg); + }); + let _ = ibus_tx.send(msg.into()); // Unnstall previous SR Prefix-SID input label if it has changed. if old_sr_label != route.sr_label { if let Some(old_sr_label) = old_sr_label { - let msg = LabelUninstallMsg { + let msg = RouteMplsMsg::Delete(LabelUninstallMsg { protocol: V::PROTOCOL, label: old_sr_label, nexthops: BTreeSet::new(), route: None, - }; - let msg = IbusMsg::RouteMpls(RouteMplsMsg::Delete(msg)); - let _ = ibus_tx.send(msg); + }); + let _ = ibus_tx.send(msg.into()); } } // Install SR Prefix-SID input label. if let Some(sr_label) = &route.sr_label { - let msg = LabelInstallMsg { + let msg = RouteMplsMsg::Add(LabelInstallMsg { protocol: V::PROTOCOL, label: *sr_label, nexthops: nexthops.clone(), route: None, replace: true, - }; - let msg = IbusMsg::RouteMpls(RouteMplsMsg::Add(msg)); - let _ = ibus_tx.send(msg); + }); + let _ = ibus_tx.send(msg.into()); } // Install BIER neighbor entry if let Some(bier_info) = &route.bier_info { - let msg = BierNbrInstallMsg { + let msg = RouteBierMsg::Add(BierNbrInstallMsg { bier_info: bier_info.clone(), nexthops, prefix: (*destination).into(), - }; - let msg = IbusMsg::RouteBier(holo_utils::ibus::RouteBierMsg::Add(msg)); - let _ = ibus_tx.send(msg); + }); + let _ = ibus_tx.send(msg.into()); } } @@ -124,35 +120,32 @@ pub(crate) fn route_uninstall( V: Version, { // Uninstall route. - let msg = RouteKeyMsg { + let msg = RouteIpMsg::Delete(RouteKeyMsg { protocol: V::PROTOCOL, prefix: (*destination).into(), - }; - let msg = IbusMsg::RouteIp(RouteIpMsg::Delete(msg)); - let _ = ibus_tx.send(msg); + }); + let _ = ibus_tx.send(msg.into()); // Uninstall SR Prefix-SID input label. if let Some(sr_label) = &route.sr_label { - let msg = LabelUninstallMsg { + let msg = RouteMplsMsg::Delete(LabelUninstallMsg { protocol: V::PROTOCOL, label: *sr_label, nexthops: BTreeSet::new(), route: None, - }; - let msg = IbusMsg::RouteMpls(RouteMplsMsg::Delete(msg)); - let _ = ibus_tx.send(msg); + }); + let _ = ibus_tx.send(msg.into()); } // Uninstall BIER neighbor entry if let Some(bier_info) = &route.bier_info { for bsl in &bier_info.bfr_bss { - let msg = BierNbrUninstallMsg { + let msg = RouteBierMsg::Delete(BierNbrUninstallMsg { sd_id: bier_info.sd_id, bfr_id: bier_info.bfr_id, bsl: *bsl, - }; - let msg = IbusMsg::RouteBier(RouteBierMsg::Delete(msg)); - let _ = ibus_tx.send(msg); + }); + let _ = ibus_tx.send(msg.into()); } } } @@ -165,7 +158,7 @@ pub(crate) fn adj_sid_install( ) where V: Version, { - let msg = LabelInstallMsg { + let msg = RouteMplsMsg::Add(LabelInstallMsg { protocol: V::PROTOCOL, label, nexthops: [Nexthop::Address { @@ -176,21 +169,19 @@ pub(crate) fn adj_sid_install( .into(), route: None, replace: false, - }; - let msg = IbusMsg::RouteMpls(RouteMplsMsg::Add(msg)); - let _ = ibus_tx.send(msg); + }); + let _ = ibus_tx.send(msg.into()); } pub(crate) fn adj_sid_uninstall(ibus_tx: &IbusSender, label: Label) where V: Version, { - let msg = LabelUninstallMsg { + let msg = RouteMplsMsg::Delete(LabelUninstallMsg { protocol: V::PROTOCOL, label, nexthops: BTreeSet::new(), route: None, - }; - let msg = IbusMsg::RouteMpls(RouteMplsMsg::Delete(msg)); - let _ = ibus_tx.send(msg); + }); + let _ = ibus_tx.send(msg.into()); } diff --git a/holo-policy/src/northbound/configuration.rs b/holo-policy/src/northbound/configuration.rs index 689bcca4..5063547b 100644 --- a/holo-policy/src/northbound/configuration.rs +++ b/holo-policy/src/northbound/configuration.rs @@ -13,7 +13,7 @@ use holo_northbound::configuration::{ self, Callbacks, CallbacksBuilder, Provider, }; use holo_northbound::yang::routing_policy; -use holo_utils::ibus::{IbusMsg, PolicyMsg}; +use holo_utils::ibus::PolicyMsg; use holo_utils::ip::AddressFamily; use holo_utils::policy::{ IpPrefixRange, MatchSetRestrictedType, MatchSetType, MetricType, @@ -1109,9 +1109,8 @@ impl Provider for Master { // Notify protocols that the policy match sets have been // updated. - let msg = - IbusMsg::Policy(PolicyMsg::MatchSetsUpdate(match_sets)); - let _ = self.ibus_tx.send(msg); + let msg = PolicyMsg::MatchSetsUpdate(match_sets); + let _ = self.ibus_tx.send(msg.into()); } Event::PolicyChange(name) => { let policy = self.policies.get_mut(&name).unwrap(); @@ -1121,13 +1120,13 @@ impl Provider for Master { let policy = Arc::new(policy.clone()); // Notify protocols that the policy has been updated. - let msg = IbusMsg::Policy(PolicyMsg::Update(policy)); - let _ = self.ibus_tx.send(msg); + let msg = PolicyMsg::Update(policy); + let _ = self.ibus_tx.send(msg.into()); } Event::PolicyDelete(name) => { // Notify protocols that the policy definition has been deleted. - let msg = IbusMsg::Policy(PolicyMsg::Delete(name)); - let _ = self.ibus_tx.send(msg); + let msg = PolicyMsg::Delete(name); + let _ = self.ibus_tx.send(msg.into()); } } } diff --git a/holo-rip/src/northbound/configuration.rs b/holo-rip/src/northbound/configuration.rs index 1dfbb1f1..ddfa26be 100644 --- a/holo-rip/src/northbound/configuration.rs +++ b/holo-rip/src/northbound/configuration.rs @@ -16,7 +16,7 @@ use holo_northbound::configuration::{ }; use holo_northbound::yang::control_plane_protocol::rip; use holo_utils::crypto::CryptoAlgo; -use holo_utils::ibus::{IbusMsg, InterfaceMsg}; +use holo_utils::ibus::InterfaceMsg; use holo_utils::ip::IpAddrKind; use holo_utils::yang::DataNodeRefExt; use holo_yang::{ToYang, TryFromYang}; @@ -456,12 +456,11 @@ where } Event::InterfaceQuerySouthbound(ifname) => { if let Instance::Up(instance) = self { - let _ = instance.tx.ibus.send(IbusMsg::Interface( - InterfaceMsg::Query { - ifname, - af: Some(V::ADDRESS_FAMILY), - }, - )); + let msg = InterfaceMsg::Query { + ifname, + af: Some(V::ADDRESS_FAMILY), + }; + let _ = instance.tx.ibus.send(msg.into()); } } Event::JoinMulticast(iface_idx) => { diff --git a/holo-rip/src/southbound/tx.rs b/holo-rip/src/southbound/tx.rs index 4f273e81..7054c444 100644 --- a/holo-rip/src/southbound/tx.rs +++ b/holo-rip/src/southbound/tx.rs @@ -4,7 +4,7 @@ // SPDX-License-Identifier: MIT // -use holo_utils::ibus::{IbusMsg, IbusSender, RouteIpMsg}; +use holo_utils::ibus::{IbusSender, RouteIpMsg}; use holo_utils::southbound::{ Nexthop, RouteKeyMsg, RouteMsg, RouteOpaqueAttrs, }; @@ -27,7 +27,7 @@ pub(crate) fn route_install( } // Fill-in message. - let msg = RouteMsg { + let msg = RouteIpMsg::Add(RouteMsg { protocol: V::PROTOCOL, prefix: route.prefix.into(), distance: distance.into(), @@ -40,11 +40,10 @@ pub(crate) fn route_install( labels: Vec::new(), }] .into(), - }; + }); // Send message. - let msg = IbusMsg::RouteIp(RouteIpMsg::Add(msg)); - let _ = ibus_tx.send(msg); + let _ = ibus_tx.send(msg.into()); } // Uninstall RIP route from the RIB. @@ -57,12 +56,11 @@ where } // Fill-in message. - let msg = RouteKeyMsg { + let msg = RouteIpMsg::Delete(RouteKeyMsg { protocol: V::PROTOCOL, prefix: route.prefix.into(), - }; + }); // Send message. - let msg = IbusMsg::RouteIp(RouteIpMsg::Delete(msg)); - let _ = ibus_tx.send(msg); + let _ = ibus_tx.send(msg.into()); } diff --git a/holo-routing/src/ibus.rs b/holo-routing/src/ibus.rs index 5c3e826f..e264e9bc 100644 --- a/holo-routing/src/ibus.rs +++ b/holo-routing/src/ibus.rs @@ -164,7 +164,7 @@ pub(crate) fn process_msg(master: &mut Master, msg: IbusMsg) { // Requests information about all interfaces addresses. pub(crate) fn request_addresses(ibus_tx: &IbusSender) { - send(ibus_tx, IbusMsg::Interface(InterfaceMsg::Dump)); + send(ibus_tx, InterfaceMsg::Dump.into()); } // Sends route redistribute update notification. @@ -173,7 +173,7 @@ pub(crate) fn notify_redistribute_add( prefix: IpNetwork, route: &Route, ) { - let msg = RouteMsg { + let msg = RouteRedistributeMsg::Add(RouteMsg { protocol: route.protocol, prefix, distance: route.distance, @@ -181,9 +181,8 @@ pub(crate) fn notify_redistribute_add( tag: route.tag, opaque_attrs: route.opaque_attrs.clone(), nexthops: route.nexthops.clone(), - }; - let msg = IbusMsg::RouteRedistribute(RouteRedistributeMsg::Add(msg)); - send(ibus_tx, msg); + }); + send(ibus_tx, msg.into()); } // Sends route redistribute delete notification. @@ -193,8 +192,8 @@ pub(crate) fn notify_redistribute_del( protocol: Protocol, ) { let msg = RouteKeyMsg { protocol, prefix }; - let msg = IbusMsg::RouteRedistribute(RouteRedistributeMsg::Delete(msg)); - send(ibus_tx, msg); + let msg = RouteRedistributeMsg::Delete(msg); + send(ibus_tx, msg.into()); } // Sends route redistribute delete notification. @@ -203,8 +202,8 @@ pub(crate) fn notify_nht_update( addr: IpAddr, metric: Option, ) { - let msg = IbusMsg::Nexthop(NexthopMsg::Update { addr, metric }); - send(ibus_tx, msg); + let msg = NexthopMsg::Update { addr, metric }; + send(ibus_tx, msg.into()); } // ===== helper functions ===== diff --git a/holo-routing/src/northbound/configuration.rs b/holo-routing/src/northbound/configuration.rs index 9fe3eac5..6ff6c2e0 100644 --- a/holo-routing/src/northbound/configuration.rs +++ b/holo-routing/src/northbound/configuration.rs @@ -23,7 +23,7 @@ use holo_utils::bier::{ Bsl, SubDomainId, UnderlayProtocolType, }; use holo_utils::ibus::{ - BierCfgEvent, BierCfgMsg, IbusMsg, RouteIpMsg, SrCfgEvent, SrCfgMsg, + BierCfgEvent, BierCfgMsg, RouteIpMsg, SrCfgEvent, SrCfgMsg, }; use holo_utils::ip::{AddressFamily, IpNetworkKind}; use holo_utils::mpls::LabelRange; @@ -1022,7 +1022,7 @@ impl Provider for Master { } // Prepare message. - let msg = RouteMsg { + let msg = RouteIpMsg::Add(RouteMsg { protocol: Protocol::STATIC, prefix, distance: 1, @@ -1030,62 +1030,60 @@ impl Provider for Master { tag: None, opaque_attrs: RouteOpaqueAttrs::None, nexthops, - }; + }); // Send message. - let msg = IbusMsg::RouteIp(RouteIpMsg::Add(msg)); - let _ = self.ibus_tx.send(msg); + let _ = self.ibus_tx.send(msg.into()); } Event::StaticRouteUninstall(prefix) => { // Prepare message. - let msg = RouteKeyMsg { + let msg = RouteIpMsg::Delete(RouteKeyMsg { protocol: Protocol::STATIC, prefix, - }; + }); // Send message. - let msg = IbusMsg::RouteIp(RouteIpMsg::Delete(msg)); - let _ = self.ibus_tx.send(msg); + let _ = self.ibus_tx.send(msg.into()); } Event::SrCfgUpdate => { // Update the shared SR configuration by creating a new reference-counted copy. self.shared.sr_config = Arc::new(self.sr_config.clone()); // Notify protocol instances about the updated SR configuration. - let _ = self.ibus_tx.send(IbusMsg::SrCfg(SrCfgMsg::Update( - self.shared.sr_config.clone(), - ))); + let _ = self.ibus_tx.send( + SrCfgMsg::Update(self.shared.sr_config.clone()).into(), + ); } Event::SrCfgLabelRangeUpdate => { // Notify protocol instances about the updated SRGB/SRLB configuration. - let _ = self.ibus_tx.send(IbusMsg::SrCfg(SrCfgMsg::Event( - SrCfgEvent::LabelRangeUpdate, - ))); + let _ = self + .ibus_tx + .send(SrCfgMsg::Event(SrCfgEvent::LabelRangeUpdate).into()); } Event::SrCfgPrefixSidUpdate(af) => { // Notify protocol instances about the updated Prefix-SID configuration. - let _ = self.ibus_tx.send(IbusMsg::SrCfg(SrCfgMsg::Event( - SrCfgEvent::PrefixSidUpdate(af), - ))); + let _ = self.ibus_tx.send( + SrCfgMsg::Event(SrCfgEvent::PrefixSidUpdate(af)).into(), + ); } Event::BierCfgUpdate => { // Update the shared BIER configuration by creating a new reference-counted copy. self.shared.bier_config = Arc::new(self.bier_config.clone()); // Notify protocol instances about the updated BIER configuration. - let _ = self.ibus_tx.send(IbusMsg::BierCfg( - BierCfgMsg::Update(self.shared.bier_config.clone()), - )); + let _ = self.ibus_tx.send( + BierCfgMsg::Update(self.shared.bier_config.clone()).into(), + ); } Event::BierCfgEncapUpdate(_sd_id, af, _bsl, _encap_type) => { - let _ = self.ibus_tx.send(IbusMsg::BierCfg(BierCfgMsg::Event( - BierCfgEvent::EncapUpdate(af), - ))); + let _ = self.ibus_tx.send( + BierCfgMsg::Event(BierCfgEvent::EncapUpdate(af)).into(), + ); } Event::BierCfgSubDomainUpdate(af) => { - let _ = self.ibus_tx.send(IbusMsg::BierCfg(BierCfgMsg::Event( - BierCfgEvent::SubDomainUpdate(af), - ))); + let _ = self.ibus_tx.send( + BierCfgMsg::Event(BierCfgEvent::SubDomainUpdate(af)).into(), + ); } } } diff --git a/holo-system/src/ibus.rs b/holo-system/src/ibus.rs index c8eb0939..a61f1f14 100644 --- a/holo-system/src/ibus.rs +++ b/holo-system/src/ibus.rs @@ -20,8 +20,8 @@ pub(crate) fn notify_hostname_update( ibus_tx: &IbusSender, hostname: Option, ) { - let msg = IbusMsg::Hostname(HostnameMsg::Update(hostname)); - notify(ibus_tx, msg); + let msg = HostnameMsg::Update(hostname); + notify(ibus_tx, msg.into()); } // ===== helper functions ===== diff --git a/holo-utils/Cargo.toml b/holo-utils/Cargo.toml index 9a9f71ab..9b751fff 100644 --- a/holo-utils/Cargo.toml +++ b/holo-utils/Cargo.toml @@ -13,6 +13,7 @@ bitflags.workspace = true bytes.workspace = true capctl.workspace = true chrono.workspace = true +derive_more.workspace = true derive-new.workspace = true enum-as-inner.workspace = true ipnetwork.workspace = true diff --git a/holo-utils/src/ibus.rs b/holo-utils/src/ibus.rs index e2e1f646..74859b93 100644 --- a/holo-utils/src/ibus.rs +++ b/holo-utils/src/ibus.rs @@ -3,10 +3,10 @@ // // SPDX-License-Identifier: MIT // - use std::net::{IpAddr, Ipv4Addr}; use std::sync::Arc; +use derive_more::From; use serde::{Deserialize, Serialize}; use tokio::sync::broadcast::{Receiver, Sender}; @@ -27,7 +27,7 @@ pub type IbusReceiver = Receiver; pub type IbusSender = Sender; // Ibus message for communication among the different Holo components. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, From)] pub enum IbusMsg { // BFD session BfdSession(BfdSessionMsg),