Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mxyns committed Dec 18, 2024
1 parent 1ac4d45 commit 9ccba48
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 46 deletions.
70 changes: 40 additions & 30 deletions crates/pmacct-gauze-bindings/src/utils/cap_per_af.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub struct AddressTypeNotSupported(pub AddressType);
pub trait PerAddressTypeCapability<T> {
fn from_iter<I>(iterator: I) -> (Self, Vec<AddressTypeNotSupported>)
where
I: IntoIterator<Item=(AddressType, T)>,
Self: Sized
I: IntoIterator<Item = (AddressType, T)>,
Self: Sized,
{
let mut ok: Self = unsafe { std::mem::zeroed() };
let mut errs = Vec::new();
Expand All @@ -29,15 +29,21 @@ pub trait PerAddressTypeCapability<T> {
(ok, errs)
}

fn set_value(&mut self, address_type: AddressType, value: T) -> Result<(), AddressTypeNotSupported>;
fn set_value(
&mut self,
address_type: AddressType,
value: T,
) -> Result<(), AddressTypeNotSupported>;
}

impl PerAddressTypeCapability<u8> for cap_per_af {
fn set_value(&mut self, address_type: AddressType, value: u8) -> Result<(), AddressTypeNotSupported> {
fn set_value(
&mut self,
address_type: AddressType,
value: u8,
) -> Result<(), AddressTypeNotSupported> {
let (afi, safi) = match address_type.try_convert_to() {
Ok((afi, safi)) => {
(afi, safi)
}
Ok((afi, safi)) => (afi, safi),
Err(_) => {
return Err(AddressTypeNotSupported(address_type));
}
Expand All @@ -53,11 +59,13 @@ impl PerAddressTypeCapability<u8> for cap_per_af {
}

impl PerAddressTypeCapability<u16> for cap_per_af_u16 {
fn set_value(&mut self, address_type: AddressType, value: u16) -> Result<(), AddressTypeNotSupported> {
fn set_value(
&mut self,
address_type: AddressType,
value: u16,
) -> Result<(), AddressTypeNotSupported> {
let (afi, safi) = match address_type.try_convert_to() {
Ok((afi, safi)) => {
(afi, safi)
}
Ok((afi, safi)) => (afi, safi),
Err(_) => {
return Err(AddressTypeNotSupported(address_type));
}
Expand All @@ -72,37 +80,39 @@ impl PerAddressTypeCapability<u16> for cap_per_af_u16 {
}
}


#[cfg(test)]
pub mod tests {
use crate::utils::cap_per_af::{AddressTypeNotSupported, PerAddressTypeCapability};
use crate::{cap_per_af, AFI_IP, AFI_IP6, SAFI_MPLS_VPN, SAFI_UNICAST};
use netgauze_bgp_pkt::capabilities::AddPathAddressFamily;
use netgauze_iana::address_family::AddressType::{BgpLsVpn, Ipv4MplsLabeledVpn, Ipv4Unicast, Ipv6MplsLabeledVpn, Ipv6Unicast};
use netgauze_iana::address_family::AddressType::{
BgpLsVpn, Ipv4MplsLabeledVpn, Ipv4Unicast, Ipv6MplsLabeledVpn, Ipv6Unicast,
};

#[test]
pub fn test_address_type_conversion() {
let add_path = vec![
let add_path = [
AddPathAddressFamily::new(Ipv4Unicast, false, false), // 0
AddPathAddressFamily::new(Ipv4MplsLabeledVpn, false, true), // 1
AddPathAddressFamily::new(Ipv6Unicast, true, false), // 2
AddPathAddressFamily::new(Ipv6Unicast, true, false), // 2
AddPathAddressFamily::new(Ipv6MplsLabeledVpn, true, true), // 3
AddPathAddressFamily::new(BgpLsVpn, true, true) // Error
AddPathAddressFamily::new(BgpLsVpn, true, true), // Error
];

let iter = add_path
.iter()
.map(|add_path_address_family|
(
add_path_address_family.address_type(),
match (add_path_address_family.send(), add_path_address_family.receive()) {
(false, false) => 0,
(false, true) => 1,
(true, false) => 2,
(true, true) => 3,
}
)
);
let iter = add_path.iter().map(|add_path_address_family| {
(
add_path_address_family.address_type(),
match (
add_path_address_family.send(),
add_path_address_family.receive(),
) {
(false, false) => 0,
(false, true) => 1,
(true, false) => 2,
(true, true) => 3,
},
)
});

let (ok, err) = cap_per_af::from_iter(iter);
assert_eq!(ok.afi_max, AFI_IP6 as u16);
Expand All @@ -114,4 +124,4 @@ pub mod tests {
assert_eq!(err.len(), 1);
assert_eq!(err[0], AddressTypeNotSupported(BgpLsVpn));
}
}
}
2 changes: 1 addition & 1 deletion crates/pmacct-gauze-bindings/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod cap_per_af;
pub mod cap_per_af;
42 changes: 28 additions & 14 deletions crates/pmacct-gauze-lib/src/capi/bgp/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use crate::extensions::add_path::AddPathCapabilityValue;
use crate::log::{pmacct_log, LogPriority};
use crate::opaque::Opaque;
use pmacct_gauze_bindings::utils::cap_per_af::PerAddressTypeCapability;
use pmacct_gauze_bindings::{bgp_peer, cap_4as, cap_per_af, cap_per_af_u16, host_addr, in_addr, BGP_AS_TRANS};
use pmacct_gauze_bindings::{
bgp_peer, cap_4as, cap_per_af, cap_per_af_u16, host_addr, in_addr, BGP_AS_TRANS,
};

#[repr(C)]
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -52,7 +54,7 @@ pub struct BgpOpenInfo {
/// `bgp_peer` should be not null and point to valid data
#[no_mangle]
pub unsafe extern "C" fn netgauze_bgp_process_open(
bgp_msg: *const Opaque<BgpMessage>
bgp_msg: *const Opaque<BgpMessage>,
) -> BgpOpenProcessResult {
let bgp_msg = unsafe { bgp_msg.as_ref().unwrap().as_ref() };

Expand All @@ -71,7 +73,10 @@ pub unsafe extern "C" fn netgauze_bgp_process_open(
hold_time: open.hold_time(),
bgp_id: host_addr::from(&open.bgp_id()),
capability_mp_protocol: std::mem::zeroed(),
capability_as4: cap_4as { used: false, as4: 0 },
capability_as4: cap_4as {
used: false,
as4: 0,
},
capability_add_paths: std::mem::zeroed(),
capability_route_refresh: false,
capability_ext_nh_enc_data: std::mem::zeroed(),
Expand All @@ -83,7 +88,10 @@ pub unsafe extern "C" fn netgauze_bgp_process_open(
for capability in open_params {
match capability {
BgpCapability::MultiProtocolExtensions(mp_ext) => {
match result.capability_mp_protocol.set_value(mp_ext.address_type(), u8::from(true)) {
match result
.capability_mp_protocol
.set_value(mp_ext.address_type(), u8::from(true))
{
Ok(_) => {}
Err(err) => {
pmacct_log(
Expand All @@ -103,9 +111,15 @@ pub unsafe extern "C" fn netgauze_bgp_process_open(
};
}
BgpCapability::AddPath(addpath) => {
let iter = addpath.address_families().iter().map(|add_path_address_family| {
(add_path_address_family.address_type(), AddPathCapabilityValue::from(add_path_address_family) as u8)
});
let iter = addpath
.address_families()
.iter()
.map(|add_path_address_family| {
(
add_path_address_family.address_type(),
AddPathCapabilityValue::from(add_path_address_family) as u8,
)
});
let (ok, errs) = cap_per_af::from_iter(iter);

for err in errs {
Expand All @@ -124,9 +138,10 @@ pub unsafe extern "C" fn netgauze_bgp_process_open(
result.capability_route_refresh = true;
}
BgpCapability::ExtendedNextHopEncoding(extended_nexthop_encoding) => {
let iter = extended_nexthop_encoding.encodings().iter().map(|encoding| {
(encoding.address_type(), encoding.next_hop_afi() as u16)
});
let iter = extended_nexthop_encoding
.encodings()
.iter()
.map(|encoding| (encoding.address_type(), encoding.next_hop_afi() as u16));

let (ok, errs) = cap_per_af_u16::from_iter(iter);

Expand All @@ -140,7 +155,6 @@ pub unsafe extern "C" fn netgauze_bgp_process_open(
);
}
result.capability_ext_nh_enc_data = ok;

}
BgpCapability::EnhancedRouteRefresh
| BgpCapability::CiscoRouteRefresh
Expand Down Expand Up @@ -181,15 +195,15 @@ pub extern "C" fn netgauze_bgp_open_write_result_err_str(
BgpOpenWriteError::WrongBgpMessageTypeError(_) => c_str! {
"BgpOpenWriteError::WrongBgpMessageTypeError"
}
.as_ptr(),
.as_ptr(),
BgpOpenWriteError::MyAsnTooHighForRemotePeer => c_str! {
"BgpOpenWriteError::MyAsnTooHighForRemotePeer"
}
.as_ptr(),
.as_ptr(),
BgpOpenWriteError::Asn4CapabilityFoundInOpenRxButNotInPeer => c_str! {
"BgpOpenWriteError::PeerStateDoesNotMatchOpenRxMessage"
}
.as_ptr(),
.as_ptr(),
BgpOpenWriteError::NetgauzeWriteError { err_str } => err_str,
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/pmacct-gauze-lib/src/extensions/add_path.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::extensions::add_path::AddPathCapabilityValue::{Both, ReceiveOnly, SendOnly, Unset};
use netgauze_bgp_pkt::capabilities::AddPathAddressFamily;
use netgauze_iana::address_family::AddressType;
use pmacct_gauze_bindings::convert::TryConvertInto;
use pmacct_gauze_bindings::{afi_t, cap_per_af, safi_t};
use std::collections::HashMap;
use netgauze_bgp_pkt::capabilities::AddPathAddressFamily;

#[repr(u8)]
pub enum AddPathCapabilityValue {
Expand Down

0 comments on commit 9ccba48

Please sign in to comment.