Skip to content

Commit

Permalink
bier: Add custom errors for BfrId and Bitstring
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas Rybowski <[email protected]>
  • Loading branch information
nrybowski committed Oct 1, 2024
1 parent 12f8ec7 commit aec11c7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
30 changes: 19 additions & 11 deletions holo-routing/src/rib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,26 @@ impl Birt {

// Compute Forwarding BitMasks (F-BMs)
for ((sd_id, bfr_id, bsl), nbr) in &self.entries {
let bfr_bs =
Bitstring::from(*bfr_id, *bsl).expect("Invalid BFR-ID");

// Pattern matching is mandatory as Bitstring does not implement Copy, hence cannot use Entry interface
let key = (*sd_id, nbr.bfr_nbr, bfr_bs.si);
match bift.get_mut(&key) {
Some((e, v)) => {
e.mut_or(bfr_bs).unwrap();
v.push(*bfr_id);
match Bitstring::from(*bfr_id, *bsl) {
Ok(bfr_bs) => {
// Pattern matching is mandatory as Bitstring does not implement Copy, hence cannot use Entry interface
let key = (*sd_id, nbr.bfr_nbr, bfr_bs.si);
match bift.get_mut(&key) {
Some((e, v)) => match e.mut_or(bfr_bs) {
Ok(()) => {
v.push(*bfr_id);
}
Err(e) => {
e.log();
}
},
None => {
let _ = bift.insert(key, (bfr_bs, vec![*bfr_id]));
}
}
}
None => {
let _ = bift.insert(key, (bfr_bs, vec![*bfr_id]));
Err(e) => {
e.log();
}
}
}
Expand Down
46 changes: 39 additions & 7 deletions holo-utils/src/bier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use derive_new::new;
use holo_yang::{ToYang, TryFromYang};
use ipnetwork::IpNetwork;
use serde::{Deserialize, Serialize};
use tracing::warn;

use crate::ip::AddressFamily;

Expand All @@ -34,7 +35,6 @@ pub async fn bift_sync(bift: &Bift) {
.enumerate()
.flat_map(|(idx, b)| {
(0..8)
.into_iter()
.filter_map(|i| {
if b & (1 << i) != 0 {
Some(format!("{}", idx * 8 + i))
Expand Down Expand Up @@ -62,6 +62,38 @@ pub async fn bift_sync(bift: &Bift) {
}
}

#[derive(Debug)]
pub enum Error {
InvalidBfrId,
InvalidBitstring,
}

impl Error {
pub fn log(&self) {
match self {
Error::InvalidBfrId => {
warn!("{}", self);
}
Error::InvalidBitstring => {
warn!("{}", self);
}
}
}
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::InvalidBfrId => {
write!(f, "invalid BfrId")
}
Error::InvalidBitstring => {
write!(f, "invalid Bitstring")
}
}
}
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[derive(Deserialize, Serialize)]
pub struct BierInfo {
Expand Down Expand Up @@ -235,27 +267,27 @@ impl Bitstring {
}
}

pub fn or(&self, rhs: Self) -> Result<Self, ()> {
pub fn or(&self, rhs: Self) -> Result<Self, Error> {
if self.si != rhs.si || self.bsl != rhs.bsl {
return Err(());
return Err(Error::InvalidBitstring);
}
let mut ret = Self::new(self.bsl);
ret.si = self.si;
for i in 0..self.bs.len() {
ret.bs[i] = self.bs[i] | rhs.bs[i];
}
return Ok(ret);
Ok(ret)
}

pub fn mut_or(&mut self, rhs: Self) -> Result<(), ()> {
pub fn mut_or(&mut self, rhs: Self) -> Result<(), Error> {
let bs = self.or(rhs)?;
self.bs = bs.bs;
Ok(())
}

pub fn from(id: BfrId, bsl: Bsl) -> Result<Self, ()> {
pub fn from(id: BfrId, bsl: Bsl) -> Result<Self, Error> {
if id == 0 {
return Err(());
return Err(Error::InvalidBfrId);
}
let mut bs = Self::new(bsl);
let bsl = usize::from(bsl);
Expand Down

0 comments on commit aec11c7

Please sign in to comment.