Skip to content

Commit

Permalink
protocols: move configuration structs to separate file
Browse files Browse the repository at this point in the history
In the long-term we want to automate the generation of configuration
structs from the YANG modules. As a preliminary step towards that
direction, move all protocol configuration structs to the appropriate
northbound/configuration.rs file. This also has the additional benefit
of separating boilerplate code, including defaults initialization,
from the core protocol implementations.

Signed-off-by: Renato Westphal <[email protected]>
  • Loading branch information
rwestphal committed Dec 16, 2023
1 parent b5d35fd commit 191d48b
Show file tree
Hide file tree
Showing 15 changed files with 501 additions and 516 deletions.
42 changes: 41 additions & 1 deletion holo-bfd/src/northbound/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// SPDX-License-Identifier: MIT
//

use std::net::SocketAddr;
use std::net::{IpAddr, SocketAddr};
use std::sync::LazyLock as Lazy;

use async_trait::async_trait;
Expand Down Expand Up @@ -50,6 +50,22 @@ pub static VALIDATION_CALLBACKS: Lazy<ValidationCallbacks> =
Lazy::new(load_validation_callbacks);
pub static CALLBACKS: Lazy<Callbacks<Master>> = Lazy::new(load_callbacks);

// ===== configuration structs =====

#[derive(Debug)]
pub struct SessionCfg {
// Common parameters.
pub local_multiplier: u8,
pub min_tx: u32,
pub min_rx: u32,
pub admin_down: bool,
// IP single-hop parameters.
pub src: Option<IpAddr>,
// IP multihop parameters.
pub tx_ttl: Option<u8>,
pub rx_ttl: Option<u8>,
}

// ===== callbacks =====

fn load_callbacks() -> Callbacks<Master> {
Expand Down Expand Up @@ -418,3 +434,27 @@ fn validate_interval(interval: u32) -> Result<(), String> {

Ok(())
}

// ===== configuration defaults =====

impl Default for SessionCfg {
fn default() -> SessionCfg {
let local_multiplier =
bfd::ip_sh::sessions::session::local_multiplier::DFLT;
let min_tx =
bfd::ip_sh::sessions::session::desired_min_tx_interval::DFLT;
let min_rx =
bfd::ip_sh::sessions::session::required_min_rx_interval::DFLT;
let admin_down = bfd::ip_sh::sessions::session::admin_down::DFLT;

SessionCfg {
local_multiplier,
min_tx,
min_rx,
admin_down,
src: None,
tx_ttl: None,
rx_ttl: None,
}
}
}
39 changes: 1 addition & 38 deletions holo-bfd/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use rand::RngCore;
use crate::debug::Debug;
use crate::error::{Error, IoError};
use crate::master::Master;
use crate::northbound::configuration::SessionCfg;
use crate::northbound::notification;
use crate::packet::{DiagnosticCode, Packet, PacketFlags};
use crate::tasks::messages::input::DetectTimerMsg;
Expand All @@ -48,20 +49,6 @@ pub struct Session {
pub clients: HashMap<ClientId, Option<ClientCfg>>,
}

#[derive(Debug)]
pub struct SessionCfg {
// Common parameters.
pub local_multiplier: u8,
pub min_tx: u32,
pub min_rx: u32,
pub admin_down: bool,
// IP single-hop parameters.
pub src: Option<IpAddr>,
// IP multihop parameters.
pub tx_ttl: Option<u8>,
pub rx_ttl: Option<u8>,
}

#[derive(Debug)]
pub struct SessionState {
pub socket_tx: Option<Arc<UdpSocket>>,
Expand Down Expand Up @@ -423,30 +410,6 @@ impl Drop for Session {
}
}

// ===== impl SessionCfg =====

impl Default for SessionCfg {
fn default() -> SessionCfg {
let local_multiplier =
bfd::ip_sh::sessions::session::local_multiplier::DFLT;
let min_tx =
bfd::ip_sh::sessions::session::desired_min_tx_interval::DFLT;
let min_rx =
bfd::ip_sh::sessions::session::required_min_rx_interval::DFLT;
let admin_down = bfd::ip_sh::sessions::session::admin_down::DFLT;

SessionCfg {
local_multiplier,
min_tx,
min_rx,
admin_down,
src: None,
tx_ttl: None,
rx_ttl: None,
}
}
}

// ===== impl SessionState =====

impl Default for SessionState {
Expand Down
28 changes: 1 addition & 27 deletions holo-ldp/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::sync::Arc;
use std::time::Duration;

use chrono::{DateTime, Utc};
use holo_northbound::paths::control_plane_protocol::mpls_ldp;
use holo_utils::socket::UdpSocket;
use holo_utils::task::{IntervalTask, TimeoutTask};
use holo_utils::Sender;
Expand All @@ -21,6 +20,7 @@ use crate::collections::{
use crate::debug::Debug;
use crate::error::IoError;
use crate::instance::{InstanceState, InstanceUp};
use crate::northbound::configuration::TargetedNbrCfg;
use crate::northbound::notification;
use crate::packet::messages::hello::{
HelloFlags, HelloMsg, TlvCommonHelloParams, TlvConfigSeqNo,
Expand Down Expand Up @@ -78,13 +78,6 @@ pub struct TargetedNbr {
pub hello_interval_task: Option<IntervalTask>,
}

#[derive(Debug)]
pub struct TargetedNbrCfg {
pub enabled: bool,
pub hello_holdtime: u16,
pub hello_interval: u16,
}

// ===== impl Adjacency =====

impl Adjacency {
Expand Down Expand Up @@ -343,25 +336,6 @@ impl Drop for TargetedNbr {
}
}

// ===== impl TargetedNbrCfg =====

impl Default for TargetedNbrCfg {
fn default() -> TargetedNbrCfg {
let enabled =
mpls_ldp::discovery::targeted::address_families::ipv4::target::enabled::DFLT;
let hello_holdtime =
mpls_ldp::discovery::targeted::hello_holdtime::DFLT;
let hello_interval =
mpls_ldp::discovery::targeted::hello_interval::DFLT;

TargetedNbrCfg {
enabled,
hello_holdtime,
hello_interval,
}
}
}

// ===== global functions =====

pub(crate) fn adjacency_delete(
Expand Down
67 changes: 2 additions & 65 deletions holo-ldp/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
// SPDX-License-Identifier: MIT
//

use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::collections::{BTreeMap, BTreeSet};
use std::net::{IpAddr, Ipv4Addr};
use std::sync::atomic::{self, AtomicU32};
use std::sync::Arc;

use async_trait::async_trait;
use derive_new::new;
use enum_as_inner::EnumAsInner;
use holo_northbound::paths::control_plane_protocol::mpls_ldp;
use holo_protocol::{
InstanceChannelsTx, InstanceShared, MessageReceiver, ProtocolInstance,
};
Expand All @@ -30,8 +29,8 @@ use crate::discovery::TargetedNbr;
use crate::error::{Error, IoError};
use crate::fec::Fec;
use crate::interface::Interface;
use crate::neighbor::NeighborCfg;
use crate::network::{tcp, udp};
use crate::northbound::configuration::InstanceCfg;
use crate::tasks::messages::input::{
AdjTimeoutMsg, NbrBackoffTimeoutMsg, NbrKaTimeoutMsg, NbrRxPduMsg,
TcpAcceptMsg, TcpConnectMsg, UdpRxPduMsg,
Expand Down Expand Up @@ -84,26 +83,6 @@ pub struct InstanceSys {
pub ipv6_addr_list: BTreeSet<Ipv6Network>,
}

#[derive(Debug)]
pub struct InstanceCfg {
pub router_id: Option<Ipv4Addr>,
pub session_ka_holdtime: u16,
pub session_ka_interval: u16,
pub password: Option<String>,
pub interface_hello_holdtime: u16,
pub interface_hello_interval: u16,
pub targeted_hello_holdtime: u16,
pub targeted_hello_interval: u16,
pub targeted_hello_accept: bool,
pub ipv4: Option<InstanceIpv4Cfg>,
pub neighbors: HashMap<Ipv4Addr, NeighborCfg>,
}

#[derive(Debug)]
pub struct InstanceIpv4Cfg {
pub enabled: bool,
}

#[derive(Debug)]
pub struct InstanceState {
// Global message ID.
Expand Down Expand Up @@ -529,48 +508,6 @@ impl InstanceCfg {
}
}

impl Default for InstanceCfg {
fn default() -> InstanceCfg {
let session_ka_holdtime = mpls_ldp::peers::session_ka_holdtime::DFLT;
let session_ka_interval = mpls_ldp::peers::session_ka_interval::DFLT;
let interface_hello_holdtime =
mpls_ldp::discovery::interfaces::hello_holdtime::DFLT;
let interface_hello_interval =
mpls_ldp::discovery::interfaces::hello_interval::DFLT;
let targeted_hello_holdtime =
mpls_ldp::discovery::targeted::hello_holdtime::DFLT;
let targeted_hello_interval =
mpls_ldp::discovery::targeted::hello_interval::DFLT;
let targeted_hello_accept =
mpls_ldp::discovery::targeted::hello_accept::enabled::DFLT;

InstanceCfg {
router_id: None,
session_ka_holdtime,
session_ka_interval,
password: None,
interface_hello_holdtime,
interface_hello_interval,
targeted_hello_holdtime,
targeted_hello_interval,
targeted_hello_accept,
ipv4: None,
neighbors: Default::default(),
}
}
}

// ===== impl InstanceIpv4Cfg =====

impl Default for InstanceIpv4Cfg {
fn default() -> InstanceIpv4Cfg {
let enabled =
mpls_ldp::discovery::targeted::address_families::ipv4::target::enabled::DFLT;

InstanceIpv4Cfg { enabled }
}
}

// ===== impl InstanceState =====

impl InstanceState {
Expand Down
42 changes: 1 addition & 41 deletions holo-ldp/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::sync::atomic::AtomicU32;
use std::sync::Arc;
use std::time::Duration;

use holo_northbound::paths::control_plane_protocol::mpls_ldp;
use holo_utils::socket::{UdpSocket, UdpSocketExt};
use holo_utils::southbound::InterfaceFlags;
use holo_utils::task::IntervalTask;
Expand All @@ -20,6 +19,7 @@ use crate::collections::{InterfaceId, InterfaceIndex};
use crate::debug::{Debug, InterfaceInactiveReason};
use crate::error::{Error, IoError};
use crate::instance::{InstanceState, InstanceUp};
use crate::northbound::configuration::InterfaceCfg;
use crate::packet::messages::hello::{
HelloFlags, HelloMsg, TlvCommonHelloParams, TlvConfigSeqNo,
TlvIpv4TransAddr,
Expand All @@ -45,18 +45,6 @@ pub struct InterfaceSys {
pub ipv6_addr_list: BTreeSet<Ipv6Network>,
}

#[derive(Debug)]
pub struct InterfaceCfg {
pub hello_holdtime: u16,
pub hello_interval: u16,
pub ipv4: Option<InterfaceIpv4Cfg>,
}

#[derive(Debug)]
pub struct InterfaceIpv4Cfg {
pub enabled: bool,
}

#[derive(Debug)]
pub struct InterfaceState {
// UDP discovery socket bound to this interface.
Expand Down Expand Up @@ -316,31 +304,3 @@ impl InterfaceSys {
IpAddr::from(addr.ip())
}
}

// ===== impl InterfaceCfg =====

impl Default for InterfaceCfg {
fn default() -> InterfaceCfg {
let hello_holdtime =
mpls_ldp::discovery::interfaces::hello_holdtime::DFLT;
let hello_interval =
mpls_ldp::discovery::interfaces::hello_interval::DFLT;

InterfaceCfg {
hello_holdtime,
hello_interval,
ipv4: None,
}
}
}

// ===== impl InterfaceIpv4Cfg =====

impl Default for InterfaceIpv4Cfg {
fn default() -> InterfaceIpv4Cfg {
let enabled =
mpls_ldp::discovery::interfaces::interface::address_families::ipv4::enabled::DFLT;

InterfaceIpv4Cfg { enabled }
}
}
8 changes: 2 additions & 6 deletions holo-ldp/src/neighbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ use crate::collections::{NeighborId, NeighborIndex};
use crate::debug::Debug;
use crate::error::Error;
use crate::fec::{Fec, LabelMapping, LabelRequest};
use crate::instance::{InstanceCfg, InstanceState, InstanceUp};
use crate::instance::{InstanceState, InstanceUp};
use crate::northbound::configuration::InstanceCfg;
use crate::northbound::notification;
use crate::packet::message::MessageType;
use crate::packet::messages::address::TlvAddressList;
Expand Down Expand Up @@ -133,11 +134,6 @@ pub enum LabelAdvMode {
DownstreamOnDemand,
}

#[derive(Debug, Default)]
pub struct NeighborCfg {
pub password: Option<String>,
}

// Session Initialization FSM:
pub mod fsm {
use serde::{Deserialize, Serialize};
Expand Down
Loading

0 comments on commit 191d48b

Please sign in to comment.