Skip to content

Commit

Permalink
routing: ensure disabled protocols are not compiled
Browse files Browse the repository at this point in the history
Previously, building holod with --no-default-features resulted in
the same binary size as when all features were enabled. The issue
was due to holo-routing not properly handling disabled features,
unlike holo-daemon. This commit fixes that problem.

Now, in the release profile, the binary size of holod on my machine
is 14MB with --no-default-features, compared to 22MB for a full build.

Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
  • Loading branch information
rwestphal committed Apr 5, 2024
1 parent 12d69e9 commit 4403662
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 102 deletions.
224 changes: 124 additions & 100 deletions holo-routing/src/northbound/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use holo_northbound::paths::control_plane_protocol;
use holo_northbound::paths::routing::ribs;
use holo_northbound::paths::routing::segment_routing::sr_mpls;
use holo_northbound::{CallbackKey, NbDaemonSender};
use holo_protocol::spawn_protocol_task;
use holo_utils::ibus::{IbusMsg, SrCfgEvent};
use holo_utils::ip::{AddressFamily, IpNetworkKind};
use holo_utils::mpls::LabelRange;
Expand Down Expand Up @@ -674,15 +673,23 @@ impl Provider for Master {

fn nested_callbacks() -> Option<Vec<CallbackKey>> {
let keys = [
#[cfg(feature = "bfd")]
holo_bfd::northbound::configuration::CALLBACKS.keys(),
#[cfg(feature = "bgp")]
holo_bgp::northbound::configuration::CALLBACKS.keys(),
#[cfg(feature = "ldp")]
holo_ldp::northbound::configuration::CALLBACKS.keys(),
#[cfg(feature = "ospf")]
holo_ospf::northbound::configuration::CALLBACKS_OSPFV2.keys(),
#[cfg(feature = "ospf")]
holo_ospf::northbound::configuration::CALLBACKS_OSPFV3.keys(),
#[cfg(feature = "rip")]
holo_rip::northbound::configuration::CALLBACKS_RIPV2.keys(),
#[cfg(feature = "rip")]
holo_rip::northbound::configuration::CALLBACKS_RIPNG.keys(),
]
.concat();
.into_iter()
.collect();

Some(keys)
}
Expand Down Expand Up @@ -724,104 +731,7 @@ impl Provider for Master {
async fn process_event(&mut self, event: Event) {
match event {
Event::InstanceStart { protocol, name } => {
let instance_id = InstanceId::new(protocol, name.clone());
let event_recorder_config = self.event_recorder_config.clone();

// Start protocol instance.
let nb_daemon_tx = match protocol {
Protocol::BFD => {
// Nothing to do, the BFD task runs permanently.
return;
}
Protocol::BGP => {
use holo_bgp::instance::Instance;

spawn_protocol_task::<Instance>(
name,
&self.nb_tx,
&self.ibus_tx,
Default::default(),
self.shared.clone(),
Some(event_recorder_config),
)
}
Protocol::DIRECT => {
// This protocol type can not be configured.
unreachable!()
}
Protocol::LDP => {
use holo_ldp::instance::Instance;

spawn_protocol_task::<Instance>(
name,
&self.nb_tx,
&self.ibus_tx,
Default::default(),
self.shared.clone(),
Some(event_recorder_config),
)
}
Protocol::OSPFV2 => {
use holo_ospf::instance::Instance;
use holo_ospf::version::Ospfv2;

spawn_protocol_task::<Instance<Ospfv2>>(
name,
&self.nb_tx,
&self.ibus_tx,
Default::default(),
self.shared.clone(),
Some(event_recorder_config),
)
}
Protocol::OSPFV3 => {
use holo_ospf::instance::Instance;
use holo_ospf::version::Ospfv3;

spawn_protocol_task::<Instance<Ospfv3>>(
name,
&self.nb_tx,
&self.ibus_tx,
Default::default(),
self.shared.clone(),
Some(event_recorder_config),
)
}
Protocol::RIPV2 => {
use holo_rip::instance::Instance;
use holo_rip::version::Ripv2;

spawn_protocol_task::<Instance<Ripv2>>(
name,
&self.nb_tx,
&self.ibus_tx,
Default::default(),
self.shared.clone(),
Some(event_recorder_config),
)
}
Protocol::RIPNG => {
use holo_rip::instance::Instance;
use holo_rip::version::Ripng;

spawn_protocol_task::<Instance<Ripng>>(
name,
&self.nb_tx,
&self.ibus_tx,
Default::default(),
self.shared.clone(),
Some(event_recorder_config),
)
}
Protocol::STATIC => {
// Nothing to do.
return;
}
};

// Keep track of northbound channel associated to the protocol
// type and name.
self.instances.insert(instance_id, nb_daemon_tx);
instance_start(self, protocol, name);
}
Event::StaticRouteInstall(prefix) => {
let route = self.static_routes.get(&prefix).unwrap();
Expand Down Expand Up @@ -897,6 +807,120 @@ impl Provider for Master {

// ===== helper functions =====

#[allow(unreachable_code, unused_imports, unused_variables)]
fn instance_start(master: &mut Master, protocol: Protocol, name: String) {
use holo_protocol::spawn_protocol_task;

let instance_id = InstanceId::new(protocol, name.clone());
let event_recorder_config = master.event_recorder_config.clone();

// Start protocol instance.
let nb_daemon_tx = match protocol {
Protocol::BFD => {
// Nothing to do, the BFD task runs permanently.
return;
}
#[cfg(feature = "bgp")]
Protocol::BGP => {
use holo_bgp::instance::Instance;

spawn_protocol_task::<Instance>(
name,
&master.nb_tx,
&master.ibus_tx,
Default::default(),
master.shared.clone(),
Some(event_recorder_config),
)
}
Protocol::DIRECT => {
// This protocol type can not be configured.
unreachable!()
}
#[cfg(feature = "ldp")]
Protocol::LDP => {
use holo_ldp::instance::Instance;

spawn_protocol_task::<Instance>(
name,
&master.nb_tx,
&master.ibus_tx,
Default::default(),
master.shared.clone(),
Some(event_recorder_config),
)
}
#[cfg(feature = "ospf")]
Protocol::OSPFV2 => {
use holo_ospf::instance::Instance;
use holo_ospf::version::Ospfv2;

spawn_protocol_task::<Instance<Ospfv2>>(
name,
&master.nb_tx,
&master.ibus_tx,
Default::default(),
master.shared.clone(),
Some(event_recorder_config),
)
}
#[cfg(feature = "ospf")]
Protocol::OSPFV3 => {
use holo_ospf::instance::Instance;
use holo_ospf::version::Ospfv3;

spawn_protocol_task::<Instance<Ospfv3>>(
name,
&master.nb_tx,
&master.ibus_tx,
Default::default(),
master.shared.clone(),
Some(event_recorder_config),
)
}
#[cfg(feature = "rip")]
Protocol::RIPV2 => {
use holo_rip::instance::Instance;
use holo_rip::version::Ripv2;

spawn_protocol_task::<Instance<Ripv2>>(
name,
&master.nb_tx,
&master.ibus_tx,
Default::default(),
master.shared.clone(),
Some(event_recorder_config),
)
}
#[cfg(feature = "rip")]
Protocol::RIPNG => {
use holo_rip::instance::Instance;
use holo_rip::version::Ripng;

spawn_protocol_task::<Instance<Ripng>>(
name,
&master.nb_tx,
&master.ibus_tx,
Default::default(),
master.shared.clone(),
Some(event_recorder_config),
)
}
Protocol::STATIC => {
// Nothing to do.
return;
}
_ => {
// Nothing to do.
return;
}
};

// Keep track of northbound channel associated to the protocol
// type and name.
master.instances.insert(instance_id, nb_daemon_tx);
}

fn static_nexthop_get(
interfaces: &BTreeMap<String, Interface>,
nexthop: &StaticRouteNexthop,
Expand Down
9 changes: 8 additions & 1 deletion holo-routing/src/northbound/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ use crate::Master;
impl Provider for Master {
fn nested_callbacks() -> Option<Vec<CallbackKey>> {
let keys = [
#[cfg(feature = "bgp")]
holo_bgp::northbound::rpc::CALLBACKS.keys(),
#[cfg(feature = "ldp")]
holo_ldp::northbound::rpc::CALLBACKS.keys(),
#[cfg(feature = "ospf")]
holo_ospf::northbound::rpc::CALLBACKS_OSPFV2.keys(),
#[cfg(feature = "ospf")]
holo_ospf::northbound::rpc::CALLBACKS_OSPFV3.keys(),
#[cfg(feature = "rip")]
holo_rip::northbound::rpc::CALLBACKS_RIPV2.keys(),
#[cfg(feature = "rip")]
holo_rip::northbound::rpc::CALLBACKS_RIPNG.keys(),
]
.concat();
.into_iter()
.collect();

Some(keys)
}
Expand Down
10 changes: 9 additions & 1 deletion holo-routing/src/northbound/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,23 @@ impl Provider for Master {

fn nested_callbacks() -> Option<Vec<CallbackKey>> {
let keys = [
#[cfg(feature = "bfd")]
holo_bfd::northbound::state::CALLBACKS.keys(),
#[cfg(feature = "bgp")]
holo_bgp::northbound::state::CALLBACKS.keys(),
#[cfg(feature = "ldp")]
holo_ldp::northbound::state::CALLBACKS.keys(),
#[cfg(feature = "ospf")]
holo_ospf::northbound::state::CALLBACKS_OSPFV2.keys(),
#[cfg(feature = "ospf")]
holo_ospf::northbound::state::CALLBACKS_OSPFV3.keys(),
#[cfg(feature = "rip")]
holo_rip::northbound::state::CALLBACKS_RIPV2.keys(),
#[cfg(feature = "rip")]
holo_rip::northbound::state::CALLBACKS_RIPNG.keys(),
]
.concat();
.into_iter()
.collect();

Some(keys)
}
Expand Down

0 comments on commit 4403662

Please sign in to comment.