Skip to content

Commit

Permalink
system: introduce the holo-system component
Browse files Browse the repository at this point in the history
Introduce `holo-system`, implementing the following subset of the
ietf-system YANG module:

module: ietf-system
  +--rw system
  |  +--rw contact?    string
  |  +--rw hostname?   inet:domain-name
  |  +--rw location?   string
  +--ro system-state
     +--ro platform
     |  +--ro os-name?      string
     |  +--ro os-release?   string
     |  +--ro os-version?   string
     |  +--ro machine?      string
     +--ro clock
        +--ro current-datetime?   yang:date-and-time
        +--ro boot-datetime?      yang:date-and-time

The main motivation for this component is to provide a centralized
place for storing hostname information and sharing it with protocol
instances (both IS-IS and OSPF have TLVs for flooding hostname
information). Additionally, including the hostname in the configuration
is necessary for implementing a persistent hostname in holo-cli.

Closes #20

Signed-off-by: Renato Westphal <[email protected]>
  • Loading branch information
rwestphal committed Jul 9, 2024
1 parent f2b3bbd commit 7f47337
Show file tree
Hide file tree
Showing 19 changed files with 1,445 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ members = [
"holo-protocol",
"holo-rip",
"holo-routing",
"holo-system",
"holo-tools",
"holo-utils",
"holo-yang",
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ Holo supports the following IETF RFCs and Internet drafts:
| ietf-routing@2018-03-13 | 100.00% | 85.71% | - | - | [92.31%](http://westphal.com.br/holo/ietf-routing.html) |
| ietf-segment-routing-mpls@2021-05-26 | 62.50% | 0.00% | - | 23.53% | [32.76%](http://westphal.com.br/holo/ietf-segment-routing-mpls.html) |
| ietf-segment-routing@2021-05-26 | 100.00% | - | - | - | [100.00%](http://westphal.com.br/holo/ietf-segment-routing.html) |
| ietf-system@2014-08-06 | 26.67% | 60.00% | 0.00% | - | [38.24%](http://westphal.com.br/holo/[email protected]) |

## Funding

Expand Down
3 changes: 3 additions & 0 deletions holo-daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ holo-policy = { path = "../holo-policy", optional = true }
holo-protocol = { path = "../holo-protocol" }
holo-rip = { path = "../holo-rip", optional = true }
holo-routing = { path = "../holo-routing", optional = true }
holo-system = { path = "../holo-system", optional = true }
holo-utils = { path = "../holo-utils" }
holo-yang = { path = "../holo-yang" }

Expand All @@ -61,6 +62,7 @@ default = [
"keychain",
"policy",
"routing",
"system",
# Protocols
"bfd",
"bgp",
Expand All @@ -74,6 +76,7 @@ interface = ["dep:holo-interface"]
keychain = ["dep:holo-keychain"]
policy = ["dep:holo-policy", "dep:holo-routing"]
routing = ["dep:holo-routing", "dep:holo-interface"]
system = ["dep:holo-system"]

# Protocols
bfd = ["dep:holo-bfd", "holo-routing/bfd"]
Expand Down
14 changes: 13 additions & 1 deletion holo-daemon/src/northbound/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,8 @@ fn start_providers(
let ibus_rx_routing = ibus_tx.subscribe();
let ibus_rx_interface = ibus_tx.subscribe();
let ibus_rx_keychain = ibus_tx.subscribe();
let ibus_rx_policy = ibus_rx;
let ibus_rx_policy = ibus_tx.subscribe();
let ibus_rx_system = ibus_rx;

// Start holo-interface.
#[cfg(feature = "interface")]
Expand Down Expand Up @@ -673,6 +674,17 @@ fn start_providers(
providers.push(daemon_tx);
}

// Start holo-system.
#[cfg(feature = "system")]
{
let daemon_tx = holo_system::start(
provider_tx.clone(),
ibus_tx.clone(),
ibus_rx_system,
);
providers.push(daemon_tx);
}

// Start holo-routing.
#[cfg(feature = "routing")]
{
Expand Down
3 changes: 3 additions & 0 deletions holo-daemon/src/northbound/yang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub(crate) fn create_context() {
#[cfg(feature = "policy")]
modules_add::<holo_policy::Master>(&mut modules);

#[cfg(feature = "system")]
modules_add::<holo_system::Master>(&mut modules);

// Add protocol modules based on enabled features.
#[cfg(feature = "bfd")]
{
Expand Down
24 changes: 24 additions & 0 deletions holo-system/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "holo-system"
version.workspace = true
authors.workspace = true
license.workspace = true
edition.workspace = true

[dependencies]
sysinfo = "0.30"

async-trait.workspace = true
capctl.workspace = true
chrono.workspace = true
derive-new.workspace = true
tokio.workspace = true
tracing.workspace = true
yang3.workspace = true

holo-northbound = { path = "../holo-northbound" }
holo-utils = { path = "../holo-utils" }
holo-yang = { path = "../holo-yang" }

[lints]
workspace = true
19 changes: 19 additions & 0 deletions holo-system/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2023 The Holo Core Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
38 changes: 38 additions & 0 deletions holo-system/src/ibus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Copyright (c) The Holo Core Contributors
//
// SPDX-License-Identifier: MIT
//

use holo_utils::ibus::{IbusMsg, IbusSender};

use crate::Master;

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

pub(crate) fn process_msg(master: &mut Master, msg: IbusMsg) {
match msg {
IbusMsg::HostnameQuery => {
notify_hostname_update(
&master.ibus_tx,
master.config.hostname.clone(),
);
}
// Ignore other events.
_ => {}
}
}

pub(crate) fn notify_hostname_update(
ibus_tx: &IbusSender,
hostname: Option<String>,
) {
let msg = IbusMsg::HostnameUpdate(hostname);
notify(ibus_tx, msg);
}

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

fn notify(ibus_tx: &IbusSender, msg: IbusMsg) {
let _ = ibus_tx.send(msg);
}
81 changes: 81 additions & 0 deletions holo-system/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// Copyright (c) The Holo Core Contributors
//
// SPDX-License-Identifier: MIT
//

#![feature(let_chains)]

mod ibus;
pub mod northbound;

use holo_northbound::{
process_northbound_msg, NbDaemonReceiver, NbDaemonSender, NbProviderSender,
ProviderBase,
};
use holo_utils::ibus::{IbusReceiver, IbusSender};
use northbound::configuration::SystemCfg;
use tokio::sync::mpsc;
use tracing::Instrument;

#[derive(Debug)]
pub struct Master {
// Northbound Tx channel.
pub nb_tx: NbProviderSender,
// Internal bus Tx channel.
pub ibus_tx: IbusSender,
// System configuration.
pub config: SystemCfg,
}

// ===== impl Master =====

impl Master {
async fn run(
&mut self,
mut nb_rx: NbDaemonReceiver,
mut ibus_rx: IbusReceiver,
) {
let mut resources = vec![];

loop {
tokio::select! {
Some(request) = nb_rx.recv() => {
process_northbound_msg(
self,
&mut resources,
request,
)
.await;
}
Ok(msg) = ibus_rx.recv() => {
ibus::process_msg(self, msg);
}
}
}
}
}

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

pub fn start(
nb_tx: NbProviderSender,
ibus_tx: IbusSender,
ibus_rx: IbusReceiver,
) -> NbDaemonSender {
let (nb_daemon_tx, nb_daemon_rx) = mpsc::channel(4);

tokio::spawn(async move {
let mut master = Master {
nb_tx,
ibus_tx,
config: Default::default(),
};

// Run task main loop.
let span = Master::debug_span("");
master.run(nb_daemon_rx, ibus_rx).instrument(span).await;
});

nb_daemon_tx
}
103 changes: 103 additions & 0 deletions holo-system/src/northbound/configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//
// Copyright (c) The Holo Core Contributors
//
// SPDX-License-Identifier: MIT
//

use std::sync::LazyLock as Lazy;

use async_trait::async_trait;
use holo_northbound::configuration::{
self, Callbacks, CallbacksBuilder, Provider,
};
use holo_northbound::yang::system;
use holo_utils::yang::DataNodeRefExt;

use crate::{ibus, Master};

static CALLBACKS: Lazy<configuration::Callbacks<Master>> =
Lazy::new(load_callbacks);

#[derive(Debug, Default)]
pub enum ListEntry {
#[default]
None,
}

#[derive(Debug)]
pub enum Resource {}

#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum Event {
HostnameChange,
}

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

#[derive(Debug, Default)]
pub struct SystemCfg {
pub contact: Option<String>,
pub hostname: Option<String>,
pub location: Option<String>,
}

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

fn load_callbacks() -> Callbacks<Master> {
CallbacksBuilder::<Master>::default()
.path(system::contact::PATH)
.modify_apply(|master, args| {
let contact = args.dnode.get_string();
master.config.contact = Some(contact);
})
.delete_apply(|master, _args| {
master.config.contact = None;
})
.path(system::hostname::PATH)
.modify_apply(|master, args| {
let hostname = args.dnode.get_string();
master.config.hostname = Some(hostname);

let event_queue = args.event_queue;
event_queue.insert(Event::HostnameChange);
})
.delete_apply(|master, args| {
master.config.hostname = None;

let event_queue = args.event_queue;
event_queue.insert(Event::HostnameChange);
})
.path(system::location::PATH)
.modify_apply(|master, args| {
let location = args.dnode.get_string();
master.config.location = Some(location);
})
.delete_apply(|master, _args| {
master.config.location = None;
})
.build()
}

// ===== impl Master =====

#[async_trait]
impl Provider for Master {
type ListEntry = ListEntry;
type Event = Event;
type Resource = Resource;

fn callbacks() -> Option<&'static Callbacks<Master>> {
Some(&CALLBACKS)
}

async fn process_event(&mut self, event: Event) {
match event {
Event::HostnameChange => {
ibus::notify_hostname_update(
&self.ibus_tx,
self.config.hostname.clone(),
);
}
}
}
}
32 changes: 32 additions & 0 deletions holo-system/src/northbound/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Copyright (c) The Holo Core Contributors
//
// SPDX-License-Identifier: MIT
//

pub mod configuration;
pub mod state;

use holo_northbound::{rpc, ProviderBase};
use tracing::{debug_span, Span};

use crate::Master;

// ===== impl Master =====

impl ProviderBase for Master {
fn yang_modules() -> &'static [&'static str] {
&["ietf-system"]
}

fn top_level_node(&self) -> String {
"/ietf-system:system".to_owned()
}

fn debug_span(_name: &str) -> Span {
debug_span!("system")
}
}

// No RPC/Actions to implement.
impl rpc::Provider for Master {}
Loading

0 comments on commit 7f47337

Please sign in to comment.