Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable AfiSafiType optionally coming from a field on a NLRI struct #52

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/bgp/message/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
};

use crate::bgp::nlri::afisafi::{
AfiSafiNlri, AfiSafiParse, NlriIter, NlriEnumIter, Nlri, NlriType
AfiSafi as Saif_al_Afi, AfiSafiNlri, AfiSafiParse, Nlri, NlriEnumIter, NlriIter, NlriType
};

use crate::util::parser::ParseError;
Expand Down Expand Up @@ -291,7 +291,7 @@
where
O: Octets,
Octs: Octets<Range<'a> = O>,
ASP: AfiSafiNlri + AfiSafiParse<'a, O, Octs>
ASP: AfiSafiNlri + Saif_al_Afi + AfiSafiParse<'a, O, Octs>
{
if ASP::afi_safi() == AfiSafi::Ipv4Unicast && !self.withdrawals.is_empty() {
return Ok(Some(NlriIter::<_, _, ASP>::new(
Expand Down Expand Up @@ -442,7 +442,7 @@
where
O: Octets,
Octs: Octets<Range<'a> = O>,
ASP: AfiSafiNlri + AfiSafiParse<'a, O, Octs>
ASP: AfiSafiNlri + Saif_al_Afi + AfiSafiParse<'a, O, Octs>
{
// If the requested announcements are of type Ipv4Unicast, and the
// conventional announcements range is non-zero, return that.
Expand Down Expand Up @@ -1553,7 +1553,7 @@
Tag, Wellknown,
};

use crate::bgp::nlri::afisafi::{AfiSafiNlri, Nlri, Ipv4UnicastNlri, Ipv4MulticastNlri};

Check warning on line 1556 in src/bgp/message/update.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

the item `AfiSafiNlri` is imported redundantly

Check warning on line 1556 in src/bgp/message/update.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

the item `Nlri` is imported redundantly

Check warning on line 1556 in src/bgp/message/update.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

the item `AfiSafiNlri` is imported redundantly

Check warning on line 1556 in src/bgp/message/update.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

the item `Nlri` is imported redundantly

Check warning on line 1556 in src/bgp/message/update.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

the item `AfiSafiNlri` is imported redundantly

Check warning on line 1556 in src/bgp/message/update.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

the item `Nlri` is imported redundantly

Check warning on line 1556 in src/bgp/message/update.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, beta)

the item `AfiSafiNlri` is imported redundantly

Check warning on line 1556 in src/bgp/message/update.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, beta)

the item `Nlri` is imported redundantly

Check warning on line 1556 in src/bgp/message/update.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

the item `AfiSafiNlri` is imported redundantly

Check warning on line 1556 in src/bgp/message/update.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

the item `Nlri` is imported redundantly

Check warning on line 1556 in src/bgp/message/update.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, beta)

the item `AfiSafiNlri` is imported redundantly

Check warning on line 1556 in src/bgp/message/update.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, beta)

the item `Nlri` is imported redundantly
use crate::bgp::types::{PathId, RouteDistinguisher};
use crate::bgp::message::Message;
use inetnum::addr::Prefix;
Expand Down
29 changes: 15 additions & 14 deletions src/bgp/message/update_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
use crate::bgp::aspath::HopPath;
use crate::bgp::communities::StandardCommunity;
use crate::bgp::message::{Header, MsgType, UpdateMessage, SessionConfig};
use crate::bgp::nlri::afisafi::{AfiSafiNlri, AfiSafiParse, NlriCompose};
use crate::bgp::nlri::afisafi::{AfiSafi, AfiSafiNlri, AfiSafiParse, NlriCompose};
use crate::bgp::path_attributes::{Attribute, PaMap, PathAttributeType};
use crate::bgp::types::{AfiSafi, NextHop};
use crate::bgp::types::{AfiSafi as AfiSafiTrait, NextHop};
use crate::bgp::nlri::afisafi::IsNlri;
use crate::util::parser::ParseError;

//------------ UpdateBuilder -------------------------------------------------
Expand All @@ -30,7 +31,7 @@

impl<Target, A> UpdateBuilder<Target, A>
where
A: AfiSafiNlri + NlriCompose,
A: AfiSafiNlri + AfiSafi + NlriCompose,
Target: OctetsBuilder + octseq::Truncate,
{

Expand Down Expand Up @@ -59,7 +60,7 @@

pub fn from_workshop(
ws: crate::bgp::workshop::route::RouteWorkshop<A>
) -> UpdateBuilder<Vec<u8>, A> {
) -> UpdateBuilder<Vec<u8>, A> where A: AfiSafi + IsNlri {

let mut res = Self::from_attributes_builder(ws.attributes().clone());
let nlri = ws.nlri();
Expand Down Expand Up @@ -270,7 +271,7 @@

impl<Target, A> UpdateBuilder<Target, A>
where
A: Clone + AfiSafiNlri + NlriCompose
A: Clone + AfiSafiNlri + AfiSafi + NlriCompose
{
pub fn into_message(self, session_config: &SessionConfig) ->
Result<UpdateMessage<<Target as FreezeBuilder>::Octets>, ComposeError>
Expand Down Expand Up @@ -679,7 +680,7 @@

impl<'a, Target, A> Iterator for PduIterator<'a, Target, A>
where
A: AfiSafiNlri + NlriCompose,
A: AfiSafiNlri + AfiSafi + NlriCompose,
Target: Clone + OctetsBuilder + FreezeBuilder + AsMut<[u8]> + octseq::Truncate,
<Target as FreezeBuilder>::Octets: Octets
{
Expand Down Expand Up @@ -722,13 +723,13 @@
}
*/

impl<A: AfiSafiNlri + NlriCompose> UpdateBuilder<Vec<u8>, A> {
impl<A: AfiSafiNlri + AfiSafi + NlriCompose> UpdateBuilder<Vec<u8>, A> {
pub fn new_vec() -> Self {
UpdateBuilder::from_target(Vec::with_capacity(23)).unwrap()
}
}

impl<A: AfiSafiNlri + NlriCompose> UpdateBuilder<BytesMut, A> {
impl<A: AfiSafiNlri + AfiSafi + NlriCompose> UpdateBuilder<BytesMut, A> {
pub fn new_bytes() -> Self {
Self::from_target(BytesMut::new()).unwrap()
}
Expand Down Expand Up @@ -764,7 +765,7 @@
_session_config: &SessionConfig
)
where
A: AfiSafiNlri + NlriCompose + AfiSafiParse<'a, O, Octs, Output = A>,
A: AfiSafiNlri + AfiSafi + NlriCompose + AfiSafiParse<'a, O, Octs, Output = A>,
Octs: Octets<Range<'a> = O>,
O: Octets,
{
Expand All @@ -776,7 +777,7 @@
}
}

impl<A: AfiSafiNlri + NlriCompose> MpReachNlriBuilder<A> {
impl<A: AfiSafiNlri + AfiSafi + NlriCompose> MpReachNlriBuilder<A> {
pub fn new() -> Self {
Self {
announcements: Vec::new(),
Expand Down Expand Up @@ -810,7 +811,7 @@
}


pub fn afi_safi(&self) -> AfiSafi {
pub fn afi_safi(&self) -> AfiSafiTrait {
A::afi_safi()
}

Expand Down Expand Up @@ -883,7 +884,7 @@
}
}

impl<A: AfiSafiNlri + NlriCompose> Default for MpReachNlriBuilder<A> {
impl<A: AfiSafiNlri + AfiSafi + NlriCompose> Default for MpReachNlriBuilder<A> {
fn default() -> Self {
Self::new()
}
Expand Down Expand Up @@ -1130,7 +1131,7 @@
_session_config: &SessionConfig
)
where
A: AfiSafiNlri + NlriCompose + AfiSafiParse<'a, O, Octs, Output = A>,
A: AfiSafiNlri + AfiSafi + NlriCompose + AfiSafiParse<'a, O, Octs, Output = A>,
Octs: Octets<Range<'a> = O>,
O: Octets,
{
Expand All @@ -1142,7 +1143,7 @@
}
}

impl<A: NlriCompose> MpUnreachNlriBuilder<A> {
impl<A: NlriCompose + AfiSafi> MpUnreachNlriBuilder<A> {
pub fn new() -> Self {
Self {
withdrawals: Vec::new(),
Expand Down Expand Up @@ -1335,7 +1336,7 @@

//use std::collections::BTreeSet;
use std::fs::File;
use std::net::{Ipv4Addr, Ipv6Addr};

Check warning on line 1339 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

the item `Ipv6Addr` is imported redundantly

Check warning on line 1339 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

the item `Ipv6Addr` is imported redundantly

Check warning on line 1339 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

the item `Ipv6Addr` is imported redundantly

Check warning on line 1339 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, beta)

the item `Ipv6Addr` is imported redundantly

Check warning on line 1339 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

the item `Ipv6Addr` is imported redundantly

Check warning on line 1339 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, beta)

the item `Ipv6Addr` is imported redundantly
use std::str::FromStr;

use memmap2::Mmap;
Expand All @@ -1345,8 +1346,8 @@
//use crate::bgp::path_attributes::AttributeHeader;
use inetnum::addr::Prefix;
use inetnum::asn::Asn;
use crate::bgp::aspath::HopPath;

Check warning on line 1349 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

the item `HopPath` is imported redundantly

Check warning on line 1349 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

the item `HopPath` is imported redundantly

Check warning on line 1349 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

the item `HopPath` is imported redundantly

Check warning on line 1349 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, beta)

the item `HopPath` is imported redundantly

Check warning on line 1349 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

the item `HopPath` is imported redundantly

Check warning on line 1349 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, beta)

the item `HopPath` is imported redundantly
use crate::bgp::communities::{StandardCommunity, Tag};

Check warning on line 1350 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

the item `StandardCommunity` is imported redundantly

Check warning on line 1350 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

the item `StandardCommunity` is imported redundantly

Check warning on line 1350 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

the item `StandardCommunity` is imported redundantly

Check warning on line 1350 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, beta)

the item `StandardCommunity` is imported redundantly

Check warning on line 1350 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

the item `StandardCommunity` is imported redundantly

Check warning on line 1350 in src/bgp/message/update_builder.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, beta)

the item `StandardCommunity` is imported redundantly
use crate::bgp::nlri::afisafi::{
Ipv4UnicastNlri,
Ipv4MulticastNlri,
Expand Down
48 changes: 46 additions & 2 deletions src/bgp/nlri/afisafi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
fn nlri(&self) -> Self::Nlri {
self.1.nlri()
}

fn afi_safi_type(&self) -> AfiSafiType {
<[<$nlri Nlri>]$(<$gen>)? as AfiSafi>::afi_safi()
}
}

impl$(<$gen>)? AfiSafi for [<$nlri AddpathNlri>]$(<$gen>)? {
Expand Down Expand Up @@ -475,9 +479,10 @@
}

/// A type representing an NLRI for a certain AFI+SAFI.
pub trait AfiSafiNlri: AfiSafi + IsNlri + Clone + Hash + Debug {
pub trait AfiSafiNlri: Clone + Hash + Debug {
type Nlri;
fn nlri(&self) -> Self::Nlri;
fn afi_safi_type(&self) -> AfiSafiType;

// TODO
//fn nexthop_compatible(&self, nh: &super::nexthop::NextHop) -> bool;
Expand All @@ -490,7 +495,7 @@
fn parse(parser: &mut Parser<'a, P>) -> Result<Self::Output, ParseError>;
}

pub trait NlriCompose: AfiSafiNlri {
pub trait NlriCompose: AfiSafiNlri + AfiSafi + IsNlri {
fn compose<Target: OctetsBuilder>(&self, target: &mut Target)
-> Result<(), Target::AppendError>;

Expand Down Expand Up @@ -590,6 +595,9 @@
fn nlri(&self) -> Self::Nlri {
self.0
}
fn afi_safi_type(&self) -> AfiSafiType {
<Self as AfiSafi>::afi_safi()
}
}

impl FromStr for Ipv4UnicastNlri {
Expand Down Expand Up @@ -682,6 +690,9 @@
fn nlri(&self) -> Self::Nlri {
self.0
}
fn afi_safi_type(&self) -> AfiSafiType {
<Self as AfiSafi>::afi_safi()
}
}

impl FromStr for Ipv4MulticastNlri {
Expand Down Expand Up @@ -760,6 +771,9 @@
fn nlri(&self) -> Self::Nlri {
self.0.clone()
}
fn afi_safi_type(&self) -> AfiSafiType {
<Self as AfiSafi>::afi_safi()
}
}

impl<'a, O, P> AfiSafiParse<'a, O, P> for Ipv4MplsUnicastNlri<O>
Expand Down Expand Up @@ -815,6 +829,9 @@
fn nlri(&self) -> Self::Nlri {
self.0.clone()
}
fn afi_safi_type(&self) -> AfiSafiType {
<Self as AfiSafi>::afi_safi()
}
}

impl<'a, O, P> AfiSafiParse<'a, O, P> for Ipv4MplsVpnUnicastNlri<O>
Expand Down Expand Up @@ -870,6 +887,9 @@
fn nlri(&self) -> Self::Nlri {
self.0.clone()
}
fn afi_safi_type(&self) -> AfiSafiType {
<Self as AfiSafi>::afi_safi()
}
}

impl<'a, O, P> AfiSafiParse<'a, O, P> for Ipv4RouteTargetNlri<O>
Expand Down Expand Up @@ -922,6 +942,9 @@
fn nlri(&self) -> Self::Nlri {
self.0.clone()
}
fn afi_safi_type(&self) -> AfiSafiType {
<Self as AfiSafi>::afi_safi()
}
}

impl<'a, O, P> AfiSafiParse<'a, O, P> for Ipv4FlowSpecNlri<O>
Expand Down Expand Up @@ -1008,6 +1031,9 @@
fn nlri(&self) -> Self::Nlri {
self.0
}
fn afi_safi_type(&self) -> AfiSafiType {
<Self as AfiSafi>::afi_safi()
}
}
impl FromStr for Ipv6UnicastNlri {
type Err = &'static str;
Expand Down Expand Up @@ -1086,6 +1112,9 @@
fn nlri(&self) -> Self::Nlri {
self.0
}
fn afi_safi_type(&self) -> AfiSafiType {
<Self as AfiSafi>::afi_safi()
}
}

impl<'a, O, P> AfiSafiParse<'a, O, P> for Ipv6MulticastNlri
Expand Down Expand Up @@ -1125,6 +1154,9 @@
fn nlri(&self) -> Self::Nlri {
self.0.clone()
}
fn afi_safi_type(&self) -> AfiSafiType {
<Self as AfiSafi>::afi_safi()
}
}

impl<'a, O, P> AfiSafiParse<'a, O, P> for Ipv6MplsUnicastNlri<O>
Expand Down Expand Up @@ -1180,6 +1212,9 @@
fn nlri(&self) -> Self::Nlri {
self.0.clone()
}
fn afi_safi_type(&self) -> AfiSafiType {
<Self as AfiSafi>::afi_safi()
}
}

impl<'a, O, P> AfiSafiParse<'a, O, P> for Ipv6MplsVpnUnicastNlri<O>
Expand Down Expand Up @@ -1235,6 +1270,9 @@
fn nlri(&self) -> Self::Nlri {
self.0.clone()
}
fn afi_safi_type(&self) -> AfiSafiType {
<Self as AfiSafi>::afi_safi()
}
}

impl<'a, O, P> AfiSafiParse<'a, O, P> for Ipv6FlowSpecNlri<O>
Expand Down Expand Up @@ -1318,6 +1356,9 @@
fn nlri(&self) -> Self::Nlri {
self.0
}
fn afi_safi_type(&self) -> AfiSafiType {
<Self as AfiSafi>::afi_safi()
}
}

impl<'a, O, P> AfiSafiParse<'a, O, P> for L2VpnVplsNlri
Expand Down Expand Up @@ -1358,6 +1399,9 @@
fn nlri(&self) -> Self::Nlri {
self.0.clone()
}
fn afi_safi_type(&self) -> AfiSafiType {
<Self as AfiSafi>::afi_safi()
}
}

impl<'a, O, P> AfiSafiParse<'a, O, P> for L2VpnEvpnNlri<O>
Expand Down Expand Up @@ -1584,8 +1628,8 @@
mod tests {

use super::*;
use inetnum::addr::Prefix;

Check warning on line 1631 in src/bgp/nlri/afisafi.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

the item `Prefix` is imported redundantly

Check warning on line 1631 in src/bgp/nlri/afisafi.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

the item `Prefix` is imported redundantly

Check warning on line 1631 in src/bgp/nlri/afisafi.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

the item `Prefix` is imported redundantly

Check warning on line 1631 in src/bgp/nlri/afisafi.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, beta)

the item `Prefix` is imported redundantly

Check warning on line 1631 in src/bgp/nlri/afisafi.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

the item `Prefix` is imported redundantly

Check warning on line 1631 in src/bgp/nlri/afisafi.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, beta)

the item `Prefix` is imported redundantly
use std::str::FromStr;

Check warning on line 1632 in src/bgp/nlri/afisafi.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

the item `FromStr` is imported redundantly

Check warning on line 1632 in src/bgp/nlri/afisafi.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

the item `FromStr` is imported redundantly

Check warning on line 1632 in src/bgp/nlri/afisafi.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

the item `FromStr` is imported redundantly

Check warning on line 1632 in src/bgp/nlri/afisafi.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, beta)

the item `FromStr` is imported redundantly

Check warning on line 1632 in src/bgp/nlri/afisafi.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

the item `FromStr` is imported redundantly

Check warning on line 1632 in src/bgp/nlri/afisafi.rs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, beta)

the item `FromStr` is imported redundantly

#[test]
fn test1() {
Expand Down
3 changes: 2 additions & 1 deletion src/bgp/workshop/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ impl<N: AfiSafiNlri + Clone + Debug + Hash> RouteWorkshop<N> {
where
for<'a> Vec<u8>: OctetsFrom<Octs::Range<'a>>,
{
let afi_safi_type = nlri.afi_safi_type();
let mut res = Self::new(nlri);

if N::afi_safi() == AfiSafiType::Ipv4Unicast &&
if afi_safi_type == AfiSafiType::Ipv4Unicast &&
pdu.has_conventional_nlri()
{
if let Ok(Some(nh)) = pdu.conventional_next_hop() {
Expand Down
Loading