Skip to content

Commit

Permalink
various wrapped iterators for rws
Browse files Browse the repository at this point in the history
  • Loading branch information
density215 committed Mar 11, 2024
1 parent 0ef4c03 commit 7f15819
Show file tree
Hide file tree
Showing 6 changed files with 380 additions and 51 deletions.
5 changes: 4 additions & 1 deletion src/bgp/message/nlri.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::bgp::nlri::afisafi::{AfiSafiNlri, Ipv6UnicastNlri};

Check warning on line 1 in src/bgp/message/nlri.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.71)

unused import: `Ipv6UnicastNlri`

Check warning on line 1 in src/bgp/message/nlri.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.71)

unused import: `AfiSafiNlri`

Check failure on line 1 in src/bgp/message/nlri.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

unused import: `Ipv6UnicastNlri`

Check failure on line 1 in src/bgp/message/nlri.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

unused import: `AfiSafiNlri`

Check failure on line 1 in src/bgp/message/nlri.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, stable)

unused import: `Ipv6UnicastNlri`

Check failure on line 1 in src/bgp/message/nlri.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, stable)

unused import: `AfiSafiNlri`
use crate::bgp::types::{Afi, AfiSafi, NextHop};

use crate::addr::Prefix;
Expand All @@ -6,7 +7,7 @@ use crate::util::parser::{parse_ipv4addr, parse_ipv6addr, ParseError};
use crate::bgp::message::update::SessionConfig;
use crate::flowspec::Component;
use crate::typeenum;
use octseq::{Octets, OctetsBuilder, OctetsFrom, Parser};
use octseq::{Octets, OctetsBuilder, OctetsFrom, OctetsInto, Parser};

Check warning on line 10 in src/bgp/message/nlri.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.71)

unused import: `OctetsInto`

Check failure on line 10 in src/bgp/message/nlri.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

unused import: `OctetsInto`

Check failure on line 10 in src/bgp/message/nlri.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, stable)

unused import: `OctetsInto`
use log::debug;

use std::fmt;
Expand All @@ -16,6 +17,8 @@ use std::str::FromStr;
#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};

use crate::bgp::nlri::afisafi::AfiSafi as AfiSafiTrait;

Check warning on line 20 in src/bgp/message/nlri.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.71)

unused import: `crate::bgp::nlri::afisafi::AfiSafi`

Check failure on line 20 in src/bgp/message/nlri.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

unused import: `crate::bgp::nlri::afisafi::AfiSafi`

Check failure on line 20 in src/bgp/message/nlri.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, stable)

unused import: `crate::bgp::nlri::afisafi::AfiSafi`
use crate::bgp::nlri::afisafi::IsPrefix;

Check warning on line 21 in src/bgp/message/nlri.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.71)

unused import: `crate::bgp::nlri::afisafi::IsPrefix`

Check failure on line 21 in src/bgp/message/nlri.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

unused import: `crate::bgp::nlri::afisafi::IsPrefix`

Check failure on line 21 in src/bgp/message/nlri.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, stable)

unused import: `crate::bgp::nlri::afisafi::IsPrefix`

//------------ FixedNlriIter -------------------------------------------------
//
Expand Down
11 changes: 3 additions & 8 deletions src/bgp/message/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,13 @@ impl<Octs: Octets> UpdateMessage<Octs> {
/// unicast NLRI, they might include ADD-PATH Path IDs or not.
/// Once we switch over to the new AfiSafiType enum, we can signal PathId
/// presence/absence.
pub fn afi_safis(&self) -> (
Option<AfiSafi>,
Option<AfiSafi>,
Option<AfiSafi>,
Option<AfiSafi>,
) {
(
pub fn afi_safis(&self) -> [Option<AfiSafi>; 4] {
[
(!self.withdrawals.is_empty()).then_some(AfiSafi::Ipv4Unicast),
(!self.announcements.is_empty()).then_some(AfiSafi::Ipv4Unicast),
self.mp_withdrawals().ok().flatten().map(|a| a.afi_safi()),
self.mp_announcements().ok().flatten().map(|a| a.afi_safi()),
)
]
}

/// Returns the conventional withdrawals.
Expand Down
120 changes: 110 additions & 10 deletions src/bgp/nlri/afisafi.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::bgp::message::nlri::BasicNlri;
use crate::typeenum; // from util::macros

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -368,6 +369,15 @@ impl fmt::Display for Ipv4UnicastNlri {
}
}

impl From<Ipv4UnicastNlri> for BasicNlri {
fn from(value: Ipv4UnicastNlri) -> Self {
BasicNlri {
prefix: value.prefix(),
path_id: None
}
}
}

//--- Ipv4Multicast

#[derive(Clone, Debug, Hash, PartialEq)]
Expand Down Expand Up @@ -399,6 +409,15 @@ impl fmt::Display for Ipv4MulticastNlri {
}
}

impl From<Ipv4MulticastNlri> for BasicNlri {
fn from(value: Ipv4MulticastNlri) -> Self {
BasicNlri {
prefix: value.prefix(),
path_id: None
}
}
}

//--- Ipv4MplsUnicast

#[derive(Clone, Debug, Hash)]
Expand Down Expand Up @@ -444,6 +463,7 @@ impl<T> fmt::Display for Ipv4MplsUnicastNlri<T> {
}
}


//--- Ipv4MplsVpnUnicastNlri

#[derive(Clone, Debug, Hash)]
Expand Down Expand Up @@ -578,6 +598,17 @@ impl fmt::Display for Ipv6UnicastNlri {
}
}


impl From<Ipv6UnicastNlri> for BasicNlri {
fn from(value: Ipv6UnicastNlri) -> Self {
BasicNlri {
prefix: value.prefix(),
path_id: None
}
}
}


//--- Ipv6Multicast

#[derive(Clone, Debug, Hash, PartialEq)]
Expand Down Expand Up @@ -610,6 +641,16 @@ impl fmt::Display for Ipv6MulticastNlri {
}


impl From<Ipv6MulticastNlri> for BasicNlri {
fn from(value: Ipv6MulticastNlri) -> Self {
BasicNlri {
prefix: value.prefix(),
path_id: None
}
}
}


//--- Ipv6MplsUnicast

#[derive(Clone, Debug, Hash)]
Expand Down Expand Up @@ -724,6 +765,13 @@ impl<T> fmt::Display for Ipv6FlowSpecNlri<T> {
}
}

impl<Octs> From<Ipv6FlowSpecNlri<Octs>> for FlowSpecNlri<Octs> {
fn from(value: Ipv6FlowSpecNlri<Octs>) -> Self {
value.0
}
}

//--- Addpath stuff

impl Ipv6UnicastAddpathNlri {
pub fn iter<'a, O, P>(parser: Parser<'a, P>) -> NlriIter<'a, O, P, Self>
Expand All @@ -735,6 +783,61 @@ impl Ipv6UnicastAddpathNlri {
}
}


impl From<Ipv6UnicastAddpathNlri> for BasicNlri {
fn from(value: Ipv6UnicastAddpathNlri) -> Self {
BasicNlri {
prefix: value.prefix(),
// This can't be right.
path_id: Some(crate::bgp::message::nlri::PathId::from_u32(value.path_id().0))
}
}
}

impl From<Ipv6MulticastAddpathNlri> for BasicNlri {
fn from(value: Ipv6MulticastAddpathNlri) -> Self {
BasicNlri {
prefix: value.prefix(),
// This can't be right.
path_id: Some(crate::bgp::message::nlri::PathId::from_u32(value.path_id().0))
}
}
}

impl From<Ipv4UnicastAddpathNlri> for BasicNlri {
fn from(value: Ipv4UnicastAddpathNlri) -> Self {
BasicNlri {
prefix: value.prefix(),
path_id: Some(crate::bgp::message::nlri::PathId::from_u32(value.path_id().0))
}
}
}

impl From<Ipv4MulticastAddpathNlri> for BasicNlri {
fn from(value: Ipv4MulticastAddpathNlri) -> Self {
BasicNlri {
prefix: value.prefix(),
path_id: Some(crate::bgp::message::nlri::PathId::from_u32(value.path_id().0))
}
}
}

// TODO: What do we do with the actual PathId, unlike BasicNlri we don't have
// a slot for it. O well, throwing it away for now.
impl<Octs> From<Ipv4FlowSpecAddpathNlri<Octs>> for FlowSpecNlri<Octs> {
fn from(value: Ipv4FlowSpecAddpathNlri<Octs>) -> Self {
value.1.into()
}
}

impl<Octs> From<Ipv6FlowSpecAddpathNlri<Octs>> for FlowSpecNlri<Octs> {
fn from(value: Ipv6FlowSpecAddpathNlri<Octs>) -> Self {
value.1.into()
}
}

//------------ Other Nlri types ----------------------------------------------

impl<Octs> Ipv4MplsUnicastNlri<Octs> {
pub fn iter<'a, P>(parser: Parser<'a, P>) -> NlriIter<'a, Octs, P, Self>
where
Expand All @@ -745,6 +848,13 @@ impl<Octs> Ipv4MplsUnicastNlri<Octs> {
}
}

impl<Octs> From<Ipv4FlowSpecNlri<Octs>> for FlowSpecNlri<Octs> {
fn from(value: Ipv4FlowSpecNlri<Octs>) -> Self {
value.0
}
}


//------------ L2Vpn ----------------------------------------------------------

//--- L2VpnVpls
Expand Down Expand Up @@ -856,16 +966,6 @@ where
output: std::marker::PhantomData
}
}

pub fn map_into_vec<T, F: Fn(<ASP as AfiSafiParse<'_, O, P>>::Output) -> T>(parser: Parser<'a, P>, fmap: F) -> Vec<T> {
NlriIter {
parser,
asp: std::marker::PhantomData::<ASP>,
output: std::marker::PhantomData::<O>
}
.map(fmap)
.collect::<Vec<_>>()
}
}


Expand Down
2 changes: 1 addition & 1 deletion src/bgp/nlri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod afisafi;
mod common;

mod evpn;
mod flowspec;
pub mod flowspec;
mod mpls;
mod mpls_vpn;
mod routetarget;
Expand Down
14 changes: 7 additions & 7 deletions src/bgp/workshop/afisafi_nlri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub trait AfiSafiNlri<Octs>: Clone + Hash + Debug {
pub trait HasBasicNlri {
fn basic_nlri(&self) -> BasicNlri;
fn make_route_with_nlri<O: Octets, M>(nlri: M, pa: &PaMap)
-> RouteWorkshop<O, M> where M: AfiSafiNlri<O, Nlri = BasicNlri>;
-> RouteWorkshop<M> where M: AfiSafiNlri<O, Nlri = BasicNlri>;
}


Expand All @@ -53,8 +53,8 @@ impl HasBasicNlri for Ipv4UnicastNlri {
}

fn make_route_with_nlri<O: Octets, M>(nlri: M, pa: &PaMap)
-> RouteWorkshop<O, M> where M: AfiSafiNlri<O, Nlri = BasicNlri> {
RouteWorkshop::<O, M>::from_pa_map(nlri, pa.clone())
-> RouteWorkshop<M> where M: AfiSafiNlri<O, Nlri = BasicNlri> {
RouteWorkshop::<M>::from_pa_map(nlri, pa.clone())
}
}

Expand Down Expand Up @@ -108,8 +108,8 @@ impl HasBasicNlri for Ipv6UnicastNlri {
}

fn make_route_with_nlri<O: Octets, M>(nlri: M, pa: &PaMap)
-> RouteWorkshop<O, M> where M: AfiSafiNlri<O, Nlri = BasicNlri> {
RouteWorkshop::<O, M>::from_pa_map(nlri, pa.clone())
-> RouteWorkshop<M> where M: AfiSafiNlri<O, Nlri = BasicNlri> {
RouteWorkshop::<M>::from_pa_map(nlri, pa.clone())
}
}

Expand Down Expand Up @@ -155,8 +155,8 @@ impl HasBasicNlri for Ipv4MulticastNlri {
}

fn make_route_with_nlri<O: Octets, M>(nlri: M, pa: &PaMap)
-> RouteWorkshop<O, M> where M: AfiSafiNlri<O, Nlri = BasicNlri> {
RouteWorkshop::<O, M>::from_pa_map(nlri, pa.clone())
-> RouteWorkshop<M> where M: AfiSafiNlri<O, Nlri = BasicNlri> {
RouteWorkshop::<M>::from_pa_map(nlri, pa.clone())
}
}

Expand Down
Loading

0 comments on commit 7f15819

Please sign in to comment.