From 58813e72acfee47fc29035b74a95d34fa1851f05 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Tue, 10 Feb 2026 18:31:26 -0700 Subject: [PATCH 01/58] bgp: set TCP_NODELAY on all TCP sessions --- bgp/src/connection_tcp.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bgp/src/connection_tcp.rs b/bgp/src/connection_tcp.rs index 13edd9504..a1c1c741c 100644 --- a/bgp/src/connection_tcp.rs +++ b/bgp/src/connection_tcp.rs @@ -536,6 +536,8 @@ impl BgpConnectionTcp { direction: ConnectionDirection, config: &SessionInfo, ) -> Result { + conn.set_nodelay(true)?; + let id = ConnectionId::new(source, peer); let dropped = Arc::new(AtomicBool::new(false)); From aace8b97464da8df132147482c152181241cefaa Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Tue, 10 Feb 2026 23:34:15 -0700 Subject: [PATCH 02/58] bgp: fix test isolation for cargo test compatibility Three tests failed under cargo test (thread-per-test) but passed under cargo nextest (process-per-test) due to shared global state in the channel-based test network simulation: 1. unnumbered_peering_helper() leaked dispatcher threads by not returning dispatcher handles to callers. Leaked threads would periodically re-bind to addresses in the global NET endpoint map, stealing entries from concurrently running tests. 2. The simulated Network had no unbind mechanism, so endpoint entries persisted after a Listener was dropped, leaving stale state for subsequent tests. 3. Several tests used hardcoded scope_ids (2, 3) for link-local addresses, causing collisions in the shared NET endpoint map when tests ran as concurrent threads in the same process. Fix by: - Adding Network::unbind() and Listener::Drop to clean up NET endpoint entries when a listener goes out of scope - Returning dispatchers from unnumbered_peering_helper() so all callers can shut them down during test cleanup - Switching hardcoded scope_ids to next_scope_id() for unique per-test addresses, matching the newer test infrastructure --- bgp/src/connection_channel.rs | 14 ++- bgp/src/test.rs | 167 ++++++++++++++++++++++++++-------- 2 files changed, 142 insertions(+), 39 deletions(-) diff --git a/bgp/src/connection_channel.rs b/bgp/src/connection_channel.rs index 336ef7b21..f7dc0814c 100644 --- a/bgp/src/connection_channel.rs +++ b/bgp/src/connection_channel.rs @@ -83,6 +83,7 @@ impl std::fmt::Display for Network { /// A listener that can listen for messages on our simulated network. struct Listener { rx: Receiver<(SocketAddr, Endpoint)>, + addr: SocketAddr, } impl Listener { @@ -97,6 +98,12 @@ impl Listener { } } +impl Drop for Listener { + fn drop(&mut self) { + NET.unbind(&self.addr); + } +} + // NOTE: this is not designed to be a full fidelity TCP/IP drop in. It gives // us enough functionality to pass messages between BGP routers to test // state machine transitions above TCP connection tracking. That's all we're @@ -112,7 +119,12 @@ impl Network { fn bind(&self, sa: SocketAddr) -> Listener { let (tx, rx) = mpsc_channel(); lock!(self.endpoints).insert(sa, tx); - Listener { rx } + Listener { rx, addr: sa } + } + + /// Remove a bound address from the network. + fn unbind(&self, addr: &SocketAddr) { + lock!(self.endpoints).remove(addr); } /// Send a copy of the provided endpoint to the endpoint identified by the diff --git a/bgp/src/test.rs b/bgp/src/test.rs index 3b04f8639..56d2157aa 100644 --- a/bgp/src/test.rs +++ b/bgp/src/test.rs @@ -1781,6 +1781,8 @@ fn unnumbered_peering_helper( Arc>, Arc, Vec>>, + Vec>>, + Vec>>, ) { let log = init_file_logger(&format!("{}.log", test_name)); @@ -2013,7 +2015,16 @@ fn unnumbered_peering_helper( .expect("start session2"); } - (router1, mock_ndp1, sessions1, router2, mock_ndp2, sessions2) + ( + router1, + mock_ndp1, + sessions1, + router2, + mock_ndp2, + sessions2, + dispatchers1, + dispatchers2, + ) } /// Test: Session survives NDP neighbor changes and reconnects via new neighbor after reset. @@ -2026,12 +2037,21 @@ fn unnumbered_peering_helper( /// - After AdminEvent::Reset, session reconnects using new NDP neighbor #[test] fn test_unnumbered_session_survives_peer_change() { - let (router1, mock_ndp1, sessions1, router2, _mock_ndp2, sessions2) = - unnumbered_peering_helper( - "unnumbered_peer_change", - vec![("eth0".to_string(), 2)], - RouteExchange::Ipv4 { nexthop: None }, - ); + let scope_id = next_scope_id(); + let ( + router1, + mock_ndp1, + sessions1, + router2, + _mock_ndp2, + sessions2, + disps1, + disps2, + ) = unnumbered_peering_helper( + "unnumbered_peer_change", + vec![("eth0".to_string(), scope_id)], + RouteExchange::Ipv4 { nexthop: None }, + ); let session1 = &sessions1[0]; let session2 = &sessions2[0]; @@ -2054,7 +2074,7 @@ fn test_unnumbered_session_survives_peer_change() { "fe80::2".parse().unwrap(), TEST_BGP_PORT, 0, - 2, + scope_id, )); assert_eq!(session1.get_peer_socket_addr(), Some(peer2_addr)); @@ -2063,8 +2083,12 @@ fn test_unnumbered_session_survives_peer_change() { mock_ndp1.discover_peer("eth0", new_peer_ip).unwrap(); // Router 1's query now returns the new peer - let new_peer_addr = - SocketAddr::V6(SocketAddrV6::new(new_peer_ip, TEST_BGP_PORT, 0, 2)); + let new_peer_addr = SocketAddr::V6(SocketAddrV6::new( + new_peer_ip, + TEST_BGP_PORT, + 0, + scope_id, + )); wait_for!(session1.get_peer_socket_addr() == Some(new_peer_addr)); // CRITICAL: Session must stay Established (NDP change doesn't affect FSM) @@ -2094,6 +2118,12 @@ fn test_unnumbered_session_survives_peer_change() { // Clean up router1.shutdown(); router2.shutdown(); + for d in &disps1 { + d.shutdown(); + } + for d in &disps2 { + d.shutdown(); + } } /// Test: Session handles peer expiry and rediscovery. @@ -2106,12 +2136,21 @@ fn test_unnumbered_session_survives_peer_change() { /// - Session remains Established throughout #[test] fn test_unnumbered_peer_expiry_and_rediscovery() { - let (router1, mock_ndp1, sessions1, router2, _mock_ndp2, sessions2) = - unnumbered_peering_helper( - "unnumbered_expiry", - vec![("eth0".to_string(), 2)], - RouteExchange::Ipv4 { nexthop: None }, - ); + let scope_id = next_scope_id(); + let ( + router1, + mock_ndp1, + sessions1, + router2, + _mock_ndp2, + sessions2, + disps1, + disps2, + ) = unnumbered_peering_helper( + "unnumbered_expiry", + vec![("eth0".to_string(), scope_id)], + RouteExchange::Ipv4 { nexthop: None }, + ); let session1 = &sessions1[0]; let session2 = &sessions2[0]; @@ -2149,7 +2188,7 @@ fn test_unnumbered_peer_expiry_and_rediscovery() { // Router 1's query returns the peer again let peer2_addr = - SocketAddr::V6(SocketAddrV6::new(peer2_ip, TEST_BGP_PORT, 0, 2)); + SocketAddr::V6(SocketAddrV6::new(peer2_ip, TEST_BGP_PORT, 0, scope_id)); wait_for!(session1.get_peer_socket_addr() == Some(peer2_addr)); // Sessions should still be Established @@ -2159,6 +2198,12 @@ fn test_unnumbered_peer_expiry_and_rediscovery() { // Clean up router1.shutdown(); router2.shutdown(); + for d in &disps1 { + d.shutdown(); + } + for d in &disps2 { + d.shutdown(); + } } /// Test: Multiple unnumbered sessions on different interfaces work independently. @@ -2170,12 +2215,25 @@ fn test_unnumbered_peer_expiry_and_rediscovery() { /// - Both sessions stay Established when eth0's NDP changes #[test] fn test_multiple_unnumbered_sessions() { - let (router1, mock_ndp1, sessions1, router2, _mock_ndp2, sessions2) = - unnumbered_peering_helper( - "multiple_unnumbered", - vec![("eth0".to_string(), 2), ("eth1".to_string(), 3)], - RouteExchange::Ipv4 { nexthop: None }, - ); + let scope_eth0 = next_scope_id(); + let scope_eth1 = next_scope_id(); + let ( + router1, + mock_ndp1, + sessions1, + router2, + _mock_ndp2, + sessions2, + disps1, + disps2, + ) = unnumbered_peering_helper( + "multiple_unnumbered", + vec![ + ("eth0".to_string(), scope_eth0), + ("eth1".to_string(), scope_eth1), + ], + RouteExchange::Ipv4 { nexthop: None }, + ); let session1_eth0 = &sessions1[0]; let session1_eth1 = &sessions1[1]; @@ -2191,8 +2249,12 @@ fn test_multiple_unnumbered_sessions() { // Change Router 1's eth0 NDP neighbor let new_peer_ip: Ipv6Addr = "fe80::99".parse().unwrap(); mock_ndp1.discover_peer("eth0", new_peer_ip).unwrap(); - let new_peer = - SocketAddr::V6(SocketAddrV6::new(new_peer_ip, TEST_BGP_PORT, 0, 2)); + let new_peer = SocketAddr::V6(SocketAddrV6::new( + new_peer_ip, + TEST_BGP_PORT, + 0, + scope_eth0, + )); wait_for!(session1_eth0.get_peer_socket_addr() == Some(new_peer)); // CRITICAL: All sessions must stay Established @@ -2222,7 +2284,7 @@ fn test_multiple_unnumbered_sessions() { "fe80::2".parse().unwrap(), TEST_BGP_PORT, 0, - 3, + scope_eth1, )); assert_eq!( session1_eth1.get_peer_socket_addr(), @@ -2233,6 +2295,12 @@ fn test_multiple_unnumbered_sessions() { // Clean up router1.shutdown(); router2.shutdown(); + for d in &disps1 { + d.shutdown(); + } + for d in &disps2 { + d.shutdown(); + } } /// Test: Same link-local address on multiple interfaces. @@ -2267,12 +2335,25 @@ fn test_multiple_unnumbered_sessions() { /// each interface's scope_id correctly identifies which physical link to use. #[test] fn test_same_linklocal_multiple_interfaces() { - let (router1, mock_ndp1, sessions1, router2, _mock_ndp2, sessions2) = - unnumbered_peering_helper( - "same_linklocal", - vec![("eth0".to_string(), 2), ("eth1".to_string(), 3)], - RouteExchange::Ipv4 { nexthop: None }, - ); + let scope_eth0 = next_scope_id(); + let scope_eth1 = next_scope_id(); + let ( + router1, + mock_ndp1, + sessions1, + router2, + _mock_ndp2, + sessions2, + disps1, + disps2, + ) = unnumbered_peering_helper( + "same_linklocal", + vec![ + ("eth0".to_string(), scope_eth0), + ("eth1".to_string(), scope_eth1), + ], + RouteExchange::Ipv4 { nexthop: None }, + ); // The helper already discovers fe80::2 on both interfaces of mock_ndp1. // The point of this test is to verify that the same peer IP (fe80::2) on @@ -2284,13 +2365,13 @@ fn test_same_linklocal_multiple_interfaces() { peer_ip, TEST_BGP_PORT, 0, - 2, // scope_id 2 for eth0 + scope_eth0, )); let peer_eth1 = SocketAddr::V6(SocketAddrV6::new( peer_ip, // SAME IP as eth0 TEST_BGP_PORT, 0, - 3, // scope_id 3 for eth1 + scope_eth1, )); let session1_eth0 = &sessions1[0]; @@ -2313,11 +2394,15 @@ fn test_same_linklocal_multiple_interfaces() { // Verify they're truly independent: change eth0's peer let new_peer_ip: Ipv6Addr = "fe80::99".parse().unwrap(); mock_ndp1.discover_peer("eth0", new_peer_ip).unwrap(); - let new_peer_eth0 = - SocketAddr::V6(SocketAddrV6::new(new_peer_ip, TEST_BGP_PORT, 0, 2)); + let new_peer_eth0 = SocketAddr::V6(SocketAddrV6::new( + new_peer_ip, + TEST_BGP_PORT, + 0, + scope_eth0, + )); wait_for!(session1_eth0.get_peer_socket_addr() == Some(new_peer_eth0)); - // eth1 should still see fe80::2 with scope 3 + // eth1 should still see fe80::2 with its own scope_id assert_eq!( session1_eth1.get_peer_socket_addr(), Some(peer_eth1), @@ -2333,6 +2418,12 @@ fn test_same_linklocal_multiple_interfaces() { // Clean up router1.shutdown(); router2.shutdown(); + for d in &disps1 { + d.shutdown(); + } + for d in &disps2 { + d.shutdown(); + } } // ========================================================================= @@ -3702,7 +3793,7 @@ fn test_unnumbered_interface_lifecycle() { let mock_ndp1 = UnnumberedManagerMock::new(); let mock_ndp2 = UnnumberedManagerMock::new(); - let scope_id = 2u32; + let scope_id = next_scope_id(); // ONLY configure interface mapping - do NOT add to system yet mock_ndp1.configure_interface("eth0".to_string(), scope_id); From 1f85bf8508b05cd3921592030f5f4b5f89bca27a Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Tue, 10 Feb 2026 20:03:22 -0700 Subject: [PATCH 03/58] Chunk originated BGP Updates exceeding max size This adds logic to send_update() to be able to split a large amount of prefixes that would cause the Update to exceed the max BGP packet size. Now, if we have more originated prefixes than can fit into a single Update, we will split the prefixes into chunks that fit the remaining available space (max size - header - fixed length fields - attributes). This also updates the basic_update* tests to exchange and validate 4k routes instead of just 1. This not only exercises the Update chunking, but also recreates the conditions where #634 was observed. Fixes: #614 --- bgp/src/messages.rs | 190 +++++++++++++++++++++++ bgp/src/proptest.rs | 211 ++++++++++++++++++++++++- bgp/src/session.rs | 361 ++++++++++++++++++++++--------------------- bgp/src/test.rs | 346 ++++++++++++++++++++++++++++++++--------- rdb-types/src/lib.rs | 14 ++ 5 files changed, 866 insertions(+), 256 deletions(-) diff --git a/bgp/src/messages.rs b/bgp/src/messages.rs index 73c4ca733..7403106bc 100644 --- a/bgp/src/messages.rs +++ b/bgp/src/messages.rs @@ -834,6 +834,11 @@ pub struct UpdateMessage { } impl UpdateMessage { + /// Size of the Withdrawn Routes Length field (u16) in the UPDATE body. + pub const WITHDRAWN_LEN_SIZE: usize = 2; + /// Size of the Total Path Attribute Length field (u16) in the UPDATE body. + pub const PATH_ATTRS_LEN_SIZE: usize = 2; + /// Returns true if a TreatAsWithdraw error occurred during parsing. /// When true, all NLRI (v4 + v6) should be processed as withdrawals. pub fn treat_as_withdraw(&self) -> bool { @@ -842,6 +847,89 @@ impl UpdateMessage { }) } + /// Set the traditional IPv4 withdrawn routes field. + pub fn set_withdrawn(&mut self, withdrawn: Vec) { + self.withdrawn = withdrawn; + } + + /// Replace the NLRI inside an existing MpReachNlri::Ipv4Unicast + /// attribute, preserving the nexthop. + pub fn set_mp_reach_ipv4_nlri(&mut self, nlri: Vec) { + for attr in &mut self.path_attributes { + if let PathAttributeValue::MpReachNlri(MpReachNlri::Ipv4Unicast( + ref mut inner, + )) = attr.value + { + inner.nlri = nlri; + return; + } + } + } + + /// Replace the NLRI inside an existing MpReachNlri::Ipv6Unicast + /// attribute, preserving the nexthop. + pub fn set_mp_reach_ipv6_nlri(&mut self, nlri: Vec) { + for attr in &mut self.path_attributes { + if let PathAttributeValue::MpReachNlri(MpReachNlri::Ipv6Unicast( + ref mut inner, + )) = attr.value + { + inner.nlri = nlri; + return; + } + } + } + + /// Replace the withdrawn routes inside an existing + /// MpUnreachNlri::Ipv6Unicast attribute. + pub fn set_mp_unreach_ipv6_withdrawn(&mut self, withdrawn: Vec) { + for attr in &mut self.path_attributes { + if let PathAttributeValue::MpUnreachNlri( + MpUnreachNlri::Ipv6Unicast(ref mut inner), + ) = attr.value + { + inner.withdrawn = withdrawn; + return; + } + } + } + + /// Clone this message once per chunk of prefixes, stamping each + /// chunk in via `stamp`, then pass to `send`. + pub fn chunk_and_send

( + &self, + prefixes: Vec

, + stamp: fn(&mut Self, Vec

), + wire_len_fn: fn(&P) -> usize, + max_body: usize, + send: impl Fn(Self) -> Result<(), Error>, + ) -> Result<(), Error> { + let available = max_body.saturating_sub(self.wire_len()); + for chunk in split_prefixes_by_size(prefixes, available, wire_len_fn) { + let mut msg = self.clone(); + stamp(&mut msg, chunk); + send(msg)?; + } + Ok(()) + } + + /// Wire size of the UPDATE body (not including the message header). + pub fn wire_len(&self) -> usize { + let withdrawn: usize = + self.withdrawn.iter().map(Prefix4::wire_len).sum(); + let path_attrs: usize = self + .path_attributes + .iter() + .map(PathAttribute::wire_len) + .sum(); + let nlri: usize = self.nlri.iter().map(Prefix4::wire_len).sum(); + Self::WITHDRAWN_LEN_SIZE + + withdrawn + + Self::PATH_ATTRS_LEN_SIZE + + path_attrs + + nlri + } + pub fn to_wire(&self) -> Result, Error> { let mut buf = Vec::new(); @@ -1720,6 +1808,38 @@ impl UpdateMessage { } } +/// Split a vec of prefixes into chunks where each chunk's total wire +/// size fits within `available` bytes. +pub fn split_prefixes_by_size

( + prefixes: Vec

, + available: usize, + wire_len: fn(&P) -> usize, +) -> Vec> { + let mut chunks = Vec::new(); + let mut current = Vec::new(); + let mut current_size = 0usize; + + for prefix in prefixes { + let size = wire_len(&prefix); + if current_size + size > available && !current.is_empty() { + chunks.push(std::mem::take(&mut current)); + current_size = 0; + } + current_size += size; + current.push(prefix); + } + if !current.is_empty() { + chunks.push(current); + } + + chunks +} + +/// Compute the total wire size of a slice of path attributes. +pub fn path_attrs_wire_len(attrs: &[PathAttribute]) -> usize { + attrs.iter().map(PathAttribute::wire_len).sum() +} + impl Display for UpdateMessage { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { let w_str = self @@ -1777,6 +1897,11 @@ impl From for PathAttribute { path_attribute_flags::OPTIONAL | path_attribute_flags::TRANSITIVE } + PathAttributeValue::MpReachNlri(_) + | PathAttributeValue::MpUnreachNlri(_) => { + path_attribute_flags::OPTIONAL + | path_attribute_flags::EXTENDED_LENGTH + } _ => path_attribute_flags::OPTIONAL, }; Self { @@ -1790,6 +1915,17 @@ impl From for PathAttribute { } impl PathAttribute { + /// Wire size: flags(1) + type_code(1) + length(1 or 2) + value. + pub fn wire_len(&self) -> usize { + let length_field = + match self.typ.flags & path_attribute_flags::EXTENDED_LENGTH { + 0 => 1, // standard: 1-byte length + _ => 2, // extended: 2-byte length + }; + // flags (u8) + type-code (u8) + length + value + 2 + length_field + self.value.wire_len() + } + pub fn to_wire(&self, extended_length: bool) -> Result, Error> { let mut buf = self.typ.to_wire(); let val = &self.value.to_wire()?; @@ -2203,6 +2339,25 @@ pub enum PathAttributeValue { } impl PathAttributeValue { + /// Wire size of the attribute value (not including the attribute header). + pub fn wire_len(&self) -> usize { + match self { + Self::Origin(_) => 1, // 1-byte origin code + Self::AsPath(segments) | Self::As4Path(segments) => { + segments.iter().map(As4PathSegment::wire_len).sum() + } + Self::NextHop(_) => 4, // IPv4 address + Self::MultiExitDisc(_) => 4, // u32 + Self::LocalPref(_) => 4, // u32 + Self::Aggregator(_) => 6, // 2-byte ASN + 4-byte IPv4 + Self::As4Aggregator(_) => 8, // 4-byte ASN + 4-byte IPv4 + Self::Communities(c) => 4 * c.len(), // 4 bytes per community + Self::AtomicAggregate => 0, // presence-only, no value + Self::MpReachNlri(mp) => mp.wire_len(), + Self::MpUnreachNlri(mp) => mp.wire_len(), + } + } + pub fn to_wire(&self) -> Result, Error> { match self { Self::Origin(x) => Ok(vec![(*x).into()]), @@ -2624,6 +2779,12 @@ pub struct As4PathSegment { } impl As4PathSegment { + /// Wire size: 1 (type) + 1 (count) + 4 * count (AS numbers). + pub fn wire_len(&self) -> usize { + // type (u8) + count (u8) + 4 bytes per AS number + 2 + 4 * self.value.len() + } + pub fn to_wire(&self) -> Result, Error> { if self.value.len() > u8::MAX as usize { return Err(Error::TooLarge("AS4 path segment".into())); @@ -3092,6 +3253,21 @@ impl MpReachNlri { }) } + /// Wire size: AFI(2) + SAFI(1) + NH_len(1) + NH + reserved(1) + NLRI. + pub fn wire_len(&self) -> usize { + let nh_len = usize::from(self.nexthop().byte_len()); + let nlri_len: usize = match self { + Self::Ipv4Unicast(inner) => { + inner.nlri.iter().map(Prefix4::wire_len).sum() + } + Self::Ipv6Unicast(inner) => { + inner.nlri.iter().map(Prefix6::wire_len).sum() + } + }; + // AFI (u16) + SAFI (u8) + NH-len (u8) + nexthop + reserved (u8) + NLRI + 2 + 1 + 1 + nh_len + 1 + nlri_len + } + /// Serialize to wire format. pub fn to_wire(&self) -> Vec { let mut buf = Vec::new(); @@ -3359,6 +3535,20 @@ impl MpUnreachNlri { Self::Ipv6Unicast(MpUnreachIpv6Unicast { withdrawn }) } + /// Wire size: AFI(2) + SAFI(1) + withdrawn prefixes. + pub fn wire_len(&self) -> usize { + let withdrawn_len: usize = match self { + Self::Ipv4Unicast(inner) => { + inner.withdrawn.iter().map(Prefix4::wire_len).sum() + } + Self::Ipv6Unicast(inner) => { + inner.withdrawn.iter().map(Prefix6::wire_len).sum() + } + }; + // AFI (u16) + SAFI (u8) + withdrawn + 2 + 1 + withdrawn_len + } + /// Serialize to wire format. pub fn to_wire(&self) -> Result, Error> { let mut buf = Vec::new(); diff --git a/bgp/src/proptest.rs b/bgp/src/proptest.rs index 5be14edc3..28b9654ec 100644 --- a/bgp/src/proptest.rs +++ b/bgp/src/proptest.rs @@ -13,10 +13,11 @@ //! - RFC 7606 compliance (attribute deduplication, MP-BGP ordering) use crate::messages::{ - As4PathSegment, AsPathType, BgpNexthop, BgpWireFormat, Ipv6DoubleNexthop, - MpReachNlri, MpUnreachNlri, PathAttribute, PathAttributeType, - PathAttributeTypeCode, PathAttributeValue, PathOrigin, UpdateMessage, - path_attribute_flags, + As4PathSegment, AsPathType, BgpNexthop, BgpWireFormat, Header, + Ipv6DoubleNexthop, MAX_MESSAGE_SIZE, MpReachNlri, MpUnreachNlri, + PathAttribute, PathAttributeType, PathAttributeTypeCode, + PathAttributeValue, PathOrigin, UpdateMessage, path_attribute_flags, + path_attrs_wire_len, split_prefixes_by_size, }; use proptest::prelude::*; use rdb::types::{Prefix4, Prefix6}; @@ -250,6 +251,37 @@ fn update_strategy() -> impl Strategy { ] } +// ============================================================================= +// Large-Prefix Strategies (for split_prefixes_by_size tests) +// ============================================================================= + +/// Strategy for generating enough IPv4 /24 prefixes to require chunking. +/// Each /24 prefix is 4 bytes on the wire. The max UPDATE body is 4077 +/// bytes; with minimal overhead (~29 bytes) roughly 1012 prefixes fit, +/// so 1100+ always forces a split. +fn large_ipv4_prefixes_strategy() -> impl Strategy> { + (1100u32..1500).prop_flat_map(|count| { + Just( + (0..count) + .map(|i| Prefix4::new(Ipv4Addr::from(i << 8), 24)) + .collect(), + ) + }) +} + +/// Strategy for generating enough IPv6 /48 prefixes to require chunking. +/// Each /48 prefix is 7 bytes on the wire. With ~29 bytes overhead +/// roughly 578 prefixes fit, so 600+ always forces a split. +fn large_ipv6_prefixes_strategy() -> impl Strategy> { + (600u32..800).prop_flat_map(|count| { + Just( + (0..count as u128) + .map(|i| Prefix6::new(Ipv6Addr::from(i << 80), 48)) + .collect(), + ) + }) +} + // ============================================================================= // Property Tests // ============================================================================= @@ -808,4 +840,175 @@ proptest! { "Withdrawn prefixes should be equivalent regardless of encoding" ); } + + // ------------------------------------------------------------------------- + // split_prefixes_by_size Tests + // ------------------------------------------------------------------------- + + /// Property: IPv4 split preserves all prefixes across chunks. + #[test] + fn prop_v4_split_preserves_all_prefixes( + prefixes in large_ipv4_prefixes_strategy() + ) { + let max_body = MAX_MESSAGE_SIZE - Header::WIRE_SIZE; + let skeleton = UpdateMessage { + path_attributes: vec![ + PathAttributeValue::NextHop(Ipv4Addr::UNSPECIFIED).into(), + ], + ..Default::default() + }; + let available = max_body.saturating_sub(skeleton.wire_len()); + let original_count = prefixes.len(); + let chunks = split_prefixes_by_size(prefixes, available, Prefix4::wire_len); + prop_assert!(chunks.len() >= 2, "expected 2+ chunks, got {}", chunks.len()); + let total: usize = chunks.iter().map(|c| c.len()).sum(); + prop_assert_eq!(total, original_count); + } + + /// Property: IPv6 split preserves all prefixes across chunks. + #[test] + fn prop_v6_split_preserves_all_prefixes( + prefixes in large_ipv6_prefixes_strategy() + ) { + let max_body = MAX_MESSAGE_SIZE - Header::WIRE_SIZE; + let skeleton = UpdateMessage { + path_attributes: vec![ + PathAttributeValue::MpReachNlri( + MpReachNlri::ipv6_unicast( + BgpNexthop::Ipv6Single(Ipv6Addr::UNSPECIFIED), + vec![], + ), + ).into(), + ], + ..Default::default() + }; + let available = max_body.saturating_sub(skeleton.wire_len()); + let original_count = prefixes.len(); + let chunks = split_prefixes_by_size(prefixes, available, Prefix6::wire_len); + prop_assert!(chunks.len() >= 2, "expected 2+ chunks, got {}", chunks.len()); + let total: usize = chunks.iter().map(|c| c.len()).sum(); + prop_assert_eq!(total, original_count); + } + + /// Property: Every IPv4 chunk fits within available space. + #[test] + fn prop_v4_all_chunks_within_available( + prefixes in large_ipv4_prefixes_strategy() + ) { + let max_body = MAX_MESSAGE_SIZE - Header::WIRE_SIZE; + let skeleton = UpdateMessage { + path_attributes: vec![ + PathAttributeValue::NextHop(Ipv4Addr::UNSPECIFIED).into(), + ], + ..Default::default() + }; + let available = max_body.saturating_sub(skeleton.wire_len()); + let chunks = split_prefixes_by_size(prefixes, available, Prefix4::wire_len); + for (i, chunk) in chunks.iter().enumerate() { + let size: usize = chunk.iter().map(Prefix4::wire_len).sum(); + prop_assert!( + size <= available, + "chunk {i} is {size} bytes, max is {available}", + ); + } + } + + /// Property: Every IPv6 chunk fits within available space. + #[test] + fn prop_v6_all_chunks_within_available( + prefixes in large_ipv6_prefixes_strategy() + ) { + let max_body = MAX_MESSAGE_SIZE - Header::WIRE_SIZE; + let skeleton = UpdateMessage { + path_attributes: vec![ + PathAttributeValue::MpReachNlri( + MpReachNlri::ipv6_unicast( + BgpNexthop::Ipv6Single(Ipv6Addr::UNSPECIFIED), + vec![], + ), + ).into(), + ], + ..Default::default() + }; + let available = max_body.saturating_sub(skeleton.wire_len()); + let chunks = split_prefixes_by_size(prefixes, available, Prefix6::wire_len); + for (i, chunk) in chunks.iter().enumerate() { + let size: usize = chunk.iter().map(Prefix6::wire_len).sum(); + prop_assert!( + size <= available, + "chunk {i} is {size} bytes, max is {available}", + ); + } + } + + /// Property: Small IPv4 prefix vecs produce exactly 1 chunk. + #[test] + fn prop_small_v4_produces_single_chunk( + prefixes in ipv4_prefixes_strategy() + ) { + let max_body = MAX_MESSAGE_SIZE - Header::WIRE_SIZE; + let skeleton = UpdateMessage { + path_attributes: vec![ + PathAttributeValue::NextHop(Ipv4Addr::UNSPECIFIED).into(), + ], + ..Default::default() + }; + let available = max_body.saturating_sub(skeleton.wire_len()); + let chunks = split_prefixes_by_size(prefixes.clone(), available, Prefix4::wire_len); + if !prefixes.is_empty() { + prop_assert_eq!(chunks.len(), 1, "small prefix vec should not be split"); + } + } + + /// Property: Small IPv6 prefix vecs produce exactly 1 chunk. + #[test] + fn prop_small_v6_produces_single_chunk( + prefixes in ipv6_prefixes_strategy() + ) { + let max_body = MAX_MESSAGE_SIZE - Header::WIRE_SIZE; + let skeleton = UpdateMessage { + path_attributes: vec![ + PathAttributeValue::MpReachNlri( + MpReachNlri::ipv6_unicast( + BgpNexthop::Ipv6Single(Ipv6Addr::UNSPECIFIED), + vec![], + ), + ).into(), + ], + ..Default::default() + }; + let available = max_body.saturating_sub(skeleton.wire_len()); + let chunks = split_prefixes_by_size(prefixes.clone(), available, Prefix6::wire_len); + if !prefixes.is_empty() { + prop_assert_eq!(chunks.len(), 1, "small prefix vec should not be split"); + } + } + + /// Property: Empty prefix vec produces no chunks. + #[test] + fn prop_empty_produces_no_chunks(_dummy in 0u8..1u8) { + let chunks = split_prefixes_by_size(Vec::::new(), 4000, Prefix4::wire_len); + prop_assert_eq!(chunks.len(), 0, "empty vec should produce 0 chunks"); + } + + /// Property: path_attrs_wire_len matches sum of individual wire_len. + #[test] + fn prop_path_attrs_wire_len_consistent( + attrs in distinct_traditional_attrs_strategy() + ) { + let expected: usize = attrs.iter().map(PathAttribute::wire_len).sum(); + prop_assert_eq!(path_attrs_wire_len(&attrs), expected); + } + + /// Property: wire_len() matches the actual to_wire() output length. + #[test] + fn prop_wire_len_matches_to_wire(update in update_strategy()) { + let expected = update.wire_len(); + let wire = update.to_wire().expect("should encode"); + prop_assert_eq!( + wire.len(), expected, + "wire_len() = {} but to_wire() produced {} bytes", + expected, wire.len() + ); + } } diff --git a/bgp/src/session.rs b/bgp/src/session.rs index a50f03a1b..7f4d327ae 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -13,10 +13,10 @@ use crate::{ log::{collision_log, session_log, session_log_lite}, messages::{ AddPathElement, Afi, BgpNexthop, Capability, CeaseErrorSubcode, - Community, ErrorCode, ErrorSubcode, Message, MessageKind, - MessageParseError, MpReachNlri, MpUnreachNlri, NotificationMessage, - OpenErrorSubcode, OpenMessage, PathAttributeValue, RouteRefreshMessage, - Safi, UpdateMessage, + Community, ErrorCode, ErrorSubcode, Header, MAX_MESSAGE_SIZE, Message, + MessageKind, MessageParseError, MpReachNlri, MpUnreachNlri, + NotificationMessage, OpenErrorSubcode, OpenMessage, PathAttribute, + PathAttributeValue, RouteRefreshMessage, Safi, UpdateMessage, }, params::{ BgpCapability, BgpPeerParameters, BgpPeerParametersV1, @@ -7570,154 +7570,192 @@ impl SessionRunner { ) } - /// Add peer-specific path attributes to an UPDATE message. - /// This adds MED, LOCAL_PREF, and Communities based on session configuration. - fn enrich_update(&self, update: &mut UpdateMessage) -> Result<(), Error> { - let session = lock!(self.session); + /// Filter IPv4 prefixes by session export policy. Returns the filtered vec. + fn filter_v4_exports( + prefixes: Vec, + session: &SessionInfo, + ) -> Vec { + if let Some(config4) = &session.ipv4_unicast + && let ImportExportPolicy4::Allow(ref policy) = + config4.export_policy + { + prefixes + .into_iter() + .filter(|p| policy.contains(p)) + .collect() + } else { + prefixes + } + } - // Add MED if configured - if let Some(med) = session.multi_exit_discriminator { - update - .path_attributes - .push(PathAttributeValue::MultiExitDisc(med).into()); + /// Filter IPv6 prefixes by session export policy. Returns the filtered vec. + fn filter_v6_exports( + prefixes: Vec, + session: &SessionInfo, + ) -> Vec { + if let Some(config6) = &session.ipv6_unicast + && let ImportExportPolicy6::Allow(ref policy) = + config6.export_policy + { + prefixes + .into_iter() + .filter(|p| policy.contains(p)) + .collect() + } else { + prefixes } + } + + /// Build the full path-attribute list for an announce skeleton. + /// + /// Combines router base attributes (Origin, AS_PATH), the nexthop + /// attribute, and session-specific enrichment (MED, LOCAL_PREF, + /// Communities). + fn announcement_attrs( + &self, + nexthop_attr: PathAttribute, + ) -> Vec { + let mut attrs = self.router.base_attributes(); + attrs.push(nexthop_attr); - // Add LOCAL_PREF for iBGP + let session = lock!(self.session); + if let Some(med) = session.multi_exit_discriminator { + attrs.push(PathAttributeValue::MultiExitDisc(med).into()); + } if self.is_ibgp().unwrap_or(false) { let local_pref = session.local_pref.unwrap_or(0); - update - .path_attributes - .push(PathAttributeValue::LocalPref(local_pref).into()); + attrs.push(PathAttributeValue::LocalPref(local_pref).into()); } - - // Add communities let communities: Vec = session .communities .clone() .into_iter() .map(Community::from) .collect(); - if !communities.is_empty() { - update - .path_attributes - .push(PathAttributeValue::Communities(communities).into()); + attrs.push(PathAttributeValue::Communities(communities).into()); } - - Ok(()) + attrs } - /// Apply export policy filtering to UPDATE message. - /// Filters NLRI based on per-AF export policy configuration. - fn apply_export_policy( + /// Apply shaper policy and send an UPDATE message to peer. + fn send_shaped( &self, - update: &mut UpdateMessage, + update: UpdateMessage, + pc: &PeerConnection, + shaper_application: &ShaperApplication, ) -> Result<(), Error> { - let session = lock!(self.session); - - // Filter traditional NLRI field (IPv4) using IPv4 export policy - if let Some(config4) = &session.ipv4_unicast - && let ImportExportPolicy4::Allow(ref policy4) = - config4.export_policy - { - update.nlri.retain(|p| policy4.contains(p)); - } - - // Filter MP_REACH_NLRI using the appropriate per-AF policy - if let Some(reach) = update.mp_reach_mut() { - match reach { - MpReachNlri::Ipv4Unicast(reach4) => { - if let Some(config4) = &session.ipv4_unicast - && let ImportExportPolicy4::Allow(ref policy4) = - config4.export_policy - { - reach4.nlri.retain(|p| policy4.contains(p)); - } - } - MpReachNlri::Ipv6Unicast(reach6) => { - if let Some(config6) = &session.ipv6_unicast - && let ImportExportPolicy6::Allow(ref policy6) = - config6.export_policy - { - reach6.nlri.retain(|p| policy6.contains(p)); - } - } + let shaped = match self.shape_update(update, shaper_application)? { + ShaperResult::Emit(msg) => msg, + ShaperResult::Drop => { + session_log!( + self, + debug, + pc.conn, + "update dropped by shaper policy"; + ); + return Ok(()); } - } - - Ok(()) + }; + self.send_update_message(shaped, pc) } - /// Build and send a peer-specific UPDATE message. - /// - /// This constructs UPDATE messages from type-safe routes and handles: - /// - Peer-specific next-hop derivation - /// - AFI-specific encoding (Traditional for IPv4, MP-BGP for IPv6) - /// - Addition of session-specific attributes (MED, LOCAL_PREF, Communities) - /// - Export policy filtering - /// - Shaper policy application - /// - /// # Arguments - /// * `route_update` - Type-safe route update (V4 or V6) - /// * `pc` - Peer connection (provides next-hop and enrichment context) - /// * `shaper_application` - How to apply export shaper policy fn send_update( &self, route_update: RouteUpdate, pc: &PeerConnection, shaper_application: &ShaperApplication, ) -> Result<(), Error> { - // XXX: Handle more originated routes than can fit in a single Update - - // Early exit if nothing to send if route_update.is_empty() { return Ok(()); } - // Build the UpdateMessage based on variant. RFC 7606 compliance: - // Each RouteUpdate is either an announcement OR withdrawal, never both. - let mut update = match route_update { + let max_body = MAX_MESSAGE_SIZE - Header::WIRE_SIZE; + + match route_update { RouteUpdate::V4(RouteUpdate4::Announce(nlri)) => { - match self.derive_nexthop(Afi::Ipv4, pc)? { - BgpNexthop::Ipv4(nh4) => { - let mut path_attributes = self.router.base_attributes(); - path_attributes - .push(PathAttributeValue::NextHop(nh4).into()); - - UpdateMessage { - withdrawn: vec![], - path_attributes, - nlri, - ..Default::default() - } - } - nh6 @ BgpNexthop::Ipv6Single(_) - | nh6 @ BgpNexthop::Ipv6Double(_) => { - let mut path_attrs = self.router.base_attributes(); - let reach = MpReachNlri::ipv4_unicast(nh6, nlri); - path_attrs.push( - PathAttributeValue::MpReachNlri(reach).into(), - ); + let session = lock!(self.session); + let nlri = Self::filter_v4_exports(nlri, &session); + drop(session); - UpdateMessage { - withdrawn: vec![], - path_attributes: path_attrs, - nlri: vec![], - ..Default::default() - } - } + if nlri.is_empty() { + session_log!( + self, + debug, + pc.conn, + "update completely filtered by export policy"; + ); + return Ok(()); } + + let (skeleton, stamp) = + match self.derive_nexthop(Afi::Ipv4, pc)? { + BgpNexthop::Ipv4(nh4) => { + let attrs = self.announcement_attrs( + PathAttributeValue::NextHop(nh4).into(), + ); + let skeleton = UpdateMessage { + path_attributes: attrs, + ..Default::default() + }; + ( + skeleton, + UpdateMessage::set_nlri + as fn(&mut UpdateMessage, Vec), + ) + } + nh6 => { + // ENHE: IPv4 prefixes via MP_REACH_NLRI + // with IPv6 nexthop. + let attrs = self.announcement_attrs( + PathAttributeValue::MpReachNlri( + MpReachNlri::ipv4_unicast(nh6, vec![]), + ) + .into(), + ); + let skeleton = UpdateMessage { + path_attributes: attrs, + ..Default::default() + }; + ( + skeleton, + UpdateMessage::set_mp_reach_ipv4_nlri + as fn(&mut UpdateMessage, Vec), + ) + } + }; + skeleton.chunk_and_send( + nlri, + stamp, + Prefix4::wire_len, + max_body, + |msg| self.send_shaped(msg, pc, shaper_application), + )?; } RouteUpdate::V4(RouteUpdate4::Withdraw(withdrawn)) => { - // Traditional withdrawals don't need path attributes - UpdateMessage { + UpdateMessage::default().chunk_and_send( withdrawn, - path_attributes: vec![], - nlri: vec![], - ..Default::default() - } + UpdateMessage::set_withdrawn, + Prefix4::wire_len, + max_body, + |msg| self.send_shaped(msg, pc, shaper_application), + )?; } RouteUpdate::V6(RouteUpdate6::Announce(nlri)) => { + let session = lock!(self.session); + let nlri = Self::filter_v6_exports(nlri, &session); + drop(session); + + if nlri.is_empty() { + session_log!( + self, + debug, + pc.conn, + "update completely filtered by export policy"; + ); + return Ok(()); + } + let nh6 = self.derive_nexthop(Afi::Ipv6, pc)?; if matches!(nh6, BgpNexthop::Ipv4(_)) { return Err(Error::InvalidAddress( @@ -7725,76 +7763,45 @@ impl SessionRunner { )); } - let mut path_attrs = self.router.base_attributes(); - let reach = MpReachNlri::ipv6_unicast(nh6, nlri); - path_attrs.push(PathAttributeValue::MpReachNlri(reach).into()); - - UpdateMessage { - withdrawn: vec![], - path_attributes: path_attrs, - nlri: vec![], + let attrs = self.announcement_attrs( + PathAttributeValue::MpReachNlri(MpReachNlri::ipv6_unicast( + nh6, + vec![], + )) + .into(), + ); + let skeleton = UpdateMessage { + path_attributes: attrs, ..Default::default() - } + }; + skeleton.chunk_and_send( + nlri, + UpdateMessage::set_mp_reach_ipv6_nlri, + Prefix6::wire_len, + max_body, + |msg| self.send_shaped(msg, pc, shaper_application), + )?; } RouteUpdate::V6(RouteUpdate6::Withdraw(withdrawn)) => { - // MP_UNREACH_NLRI for IPv6 withdrawals - let unreach = MpUnreachNlri::ipv6_unicast(withdrawn); - let path_attrs = - vec![PathAttributeValue::MpUnreachNlri(unreach).into()]; - - UpdateMessage { - withdrawn: vec![], - path_attributes: path_attrs, - nlri: vec![], + let skeleton = UpdateMessage { + path_attributes: vec![ + PathAttributeValue::MpUnreachNlri( + MpUnreachNlri::ipv6_unicast(vec![]), + ) + .into(), + ], ..Default::default() - } + }; + skeleton.chunk_and_send( + withdrawn, + UpdateMessage::set_mp_unreach_ipv6_withdrawn, + Prefix6::wire_len, + max_body, + |msg| self.send_shaped(msg, pc, shaper_application), + )?; } - }; - - // 3. Add peer-specific enrichments - self.enrich_update(&mut update)?; - - // 4. Apply export policy filtering - self.apply_export_policy(&mut update)?; - - // Check if update was completely filtered out - let has_content = !update.nlri.is_empty() - || !update.withdrawn.is_empty() - || update.path_attributes.iter().any(|a| { - matches!( - a.value, - PathAttributeValue::MpReachNlri(_) - | PathAttributeValue::MpUnreachNlri(_) - ) - }); - - if !has_content { - session_log!( - self, - debug, - pc.conn, - "update completely filtered by export policy"; - ); - return Ok(()); } - - // 5. Apply shaper policy - let shaped_update = - match self.shape_update(update, shaper_application)? { - ShaperResult::Emit(msg) => msg, - ShaperResult::Drop => { - session_log!( - self, - debug, - pc.conn, - "update dropped by shaper policy"; - ); - return Ok(()); - } - }; - - // 6. Send the message - self.send_update_message(shaped_update, pc) + Ok(()) } /// Send a pre-constructed UPDATE message to peer. diff --git a/bgp/src/test.rs b/bgp/src/test.rs index 56d2157aa..b960c786b 100644 --- a/bgp/src/test.rs +++ b/bgp/src/test.rs @@ -21,10 +21,12 @@ use lazy_static::lazy_static; use mg_common::log::init_file_logger; use mg_common::test::{IpAllocation, LoopbackIpManager}; use mg_common::*; -use rdb::{Asn, ImportExportPolicy4, ImportExportPolicy6, Prefix, Prefix4}; +use rdb::{ + Asn, ImportExportPolicy4, ImportExportPolicy6, Prefix, Prefix4, Prefix6, +}; use std::{ collections::{BTreeMap, BTreeSet}, - net::{IpAddr, Ipv6Addr, SocketAddr, SocketAddrV6}, + net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV6}, sync::{ Arc, Mutex, atomic::{AtomicU32, Ordering}, @@ -61,6 +63,36 @@ const CONNECT_RETRY_VERIFICATION: Duration = const ESTABLISHED_VERIFICATION: Duration = Duration::from_secs(TEST_HOLD_TIME_SECS + 2); +/// Number of routes to originate in update tests. Sized to require multiple +/// BGP UPDATE messages (each capped at MAX_MESSAGE_SIZE = 4096 bytes), +/// exercising the chunk_and_send splitting and the incremental recv_msg +/// body-read loop. +const TEST_ROUTE_COUNT: usize = 4096; + +/// Generate `count` unique IPv4 /24 prefixes: 10.0.0.0/24 .. 10.x.y.0/24 +fn generate_test_prefixes_v4(count: usize) -> Vec { + (0..count as u32) + .map(|i| { + Prefix::V4(Prefix4 { + value: Ipv4Addr::new(10, (i >> 8) as u8, (i & 0xFF) as u8, 0), + length: 24, + }) + }) + .collect() +} + +/// Generate `count` unique IPv6 /48 prefixes: 3fff:db8:0::/48 .. 3fff:db8:N::/48 +fn generate_test_prefixes_v6(count: usize) -> Vec { + (0..count as u32) + .map(|i| { + Prefix::V6(Prefix6 { + value: Ipv6Addr::new(0x3fff, 0x0db8, i as u16, 0, 0, 0, 0, 0), + length: 48, + }) + }) + .collect() +} + // XXX: add an iBGP option for the tests // XXX: Add test impl of BgpConnection (and Clock?) for FSM tests. // The DUT will still have a SessionRunner & timers, but the test peer will @@ -618,13 +650,14 @@ fn basic_peering_helper< // This test does the following: // 1. Sets up a basic pair of routers -// 2. Configures r1 to originate prefix(es) based on route_exchange +// 2. Configures r1 to originate TEST_ROUTE_COUNT prefixes based on route_exchange // 3. Brings up a BGP session between r1 and r2 // 4. Ensures the BGP FSM moves into Established on both r1 and r2 -// 5. Ensures r2 has succesfully received and installed the prefix(es) -// 6. Shuts down r1 -// 7. Ensures the BGP FSM moves out of Established on both r1 and r2 -// 8. Ensures r2 has successfully uninstalled the implicitly withdrawn prefix(es) +// 5. Ensures r2 has received all TEST_ROUTE_COUNT prefixes +// 6. Verifies path attributes (origin_as, as_path, stale) on every received route +// 7. Shuts down r1 +// 8. Ensures the BGP FSM moves out of Established on both r1 and r2 +// 9. Ensures r2 has successfully uninstalled all implicitly withdrawn prefixes fn basic_update_helper< Cnx: BgpConnection + 'static, Listener: BgpListener + 'static, @@ -702,20 +735,56 @@ fn basic_update_helper< RouteExchange::Ipv4 { nexthop: initial_nexthop, } => { - // IPv4-only: originate and verify IPv4 prefix + let prefixes_v4 = generate_test_prefixes_v4(TEST_ROUTE_COUNT); + r1.router - .create_origin4(vec![cidr!("1.2.3.0/24")]) + .create_origin4(prefixes_v4.clone()) .expect("originate IPv4"); - let prefix_rdb = Prefix::V4(cidr!("1.2.3.0/24")); - wait_for!(!r2.router.db.get_prefix_paths(&prefix_rdb).is_empty()); + // Wait for all routes to arrive at R2 + wait_for!( + prefixes_v4 + .iter() + .filter(|p| { + !r2.router.db.get_prefix_paths(p).is_empty() + }) + .count() + == TEST_ROUTE_COUNT, + "all IPv4 routes should arrive at R2" + ); + + // Verify attributes on every received route + for prefix in &prefixes_v4 { + let paths = r2.router.db.get_prefix_paths(prefix); + assert_eq!(paths.len(), 1, "{prefix:?}: expected 1 path"); + let path = &paths[0]; + + if let Some(expected_nh) = initial_nexthop { + assert_eq!( + path.nexthop, expected_nh, + "{prefix:?}: nexthop mismatch" + ); + } + + let bgp = path.bgp.as_ref().unwrap_or_else(|| { + panic!("{prefix:?}: missing BGP properties") + }); + assert_eq!( + bgp.origin_as, 4200000001, + "{prefix:?}: origin_as mismatch" + ); + assert_eq!( + bgp.as_path, + vec![4200000001u32], + "{prefix:?}: as_path mismatch" + ); + assert!( + bgp.stale.is_none(), + "{prefix:?}: route should not be stale" + ); + } - // Verify initial nexthop if one was configured and test override change - let paths = r2.router.db.get_prefix_paths(&prefix_rdb); - assert_eq!(paths.len(), 1); if let Some(initial_nh) = initial_nexthop { - assert_eq!(paths[0].nexthop, initial_nh); - // Test nexthop override change let new_nexthop = match initial_nh { IpAddr::V4(_) => ip!("10.255.255.254"), @@ -746,39 +815,82 @@ fn basic_update_helper< .update_session(peer_config, session_info) .expect("update nexthop"); - // Verify nexthop change is reflected in re-advertised route + // Verify all routes reflect the new nexthop wait_for!( - { - let paths = r2.router.db.get_prefix_paths(&prefix_rdb); - !paths.is_empty() && paths[0].nexthop == new_nexthop - }, - "nexthop should be updated" + prefixes_v4.iter().all(|p| { + let paths = r2.router.db.get_prefix_paths(p); + paths.len() == 1 && paths[0].nexthop == new_nexthop + }), + "all routes should have updated nexthop" ); } - // Shut down r1 and verify withdrawal + // Shut down r1 and verify withdrawal of all routes r1.shutdown(); wait_for_neq!(r1_session.state(), FsmStateKind::Established); wait_for_neq!(r2_session.state(), FsmStateKind::Established); - wait_for!(r2.router.db.get_prefix_paths(&prefix_rdb).is_empty()); + wait_for!( + prefixes_v4.iter().all(|p| r2 + .router + .db + .get_prefix_paths(p) + .is_empty()), + "all IPv4 routes should be withdrawn" + ); } RouteExchange::Ipv6 { nexthop: initial_nexthop, } => { - // IPv6-only: originate and verify IPv6 prefix + let prefixes_v6 = generate_test_prefixes_v6(TEST_ROUTE_COUNT); + r1.router - .create_origin6(vec![cidr!("3fff:db8::/32")]) + .create_origin6(prefixes_v6.clone()) .expect("originate IPv6"); - let prefix_rdb = Prefix::V6(cidr!("3fff:db8::/32")); - wait_for!(!r2.router.db.get_prefix_paths(&prefix_rdb).is_empty()); + // Wait for all routes to arrive at R2 + wait_for!( + prefixes_v6 + .iter() + .filter(|p| { + !r2.router.db.get_prefix_paths(p).is_empty() + }) + .count() + == TEST_ROUTE_COUNT, + "all IPv6 routes should arrive at R2" + ); + + // Verify attributes on every received route + for prefix in &prefixes_v6 { + let paths = r2.router.db.get_prefix_paths(prefix); + assert_eq!(paths.len(), 1, "{prefix:?}: expected 1 path"); + let path = &paths[0]; + + if let Some(expected_nh) = initial_nexthop { + assert_eq!( + path.nexthop, expected_nh, + "{prefix:?}: nexthop mismatch" + ); + } - // Verify initial nexthop if one was configured and test override change - let paths = r2.router.db.get_prefix_paths(&prefix_rdb); - assert_eq!(paths.len(), 1); - if let Some(initial_nh) = initial_nexthop { - assert_eq!(paths[0].nexthop, initial_nh); + let bgp = path.bgp.as_ref().unwrap_or_else(|| { + panic!("{prefix:?}: missing BGP properties") + }); + assert_eq!( + bgp.origin_as, 4200000001, + "{prefix:?}: origin_as mismatch" + ); + assert_eq!( + bgp.as_path, + vec![4200000001u32], + "{prefix:?}: as_path mismatch" + ); + assert!( + bgp.stale.is_none(), + "{prefix:?}: route should not be stale" + ); + } + if let Some(initial_nh) = initial_nexthop { // Test nexthop override change let new_nexthop = match initial_nh { IpAddr::V6(_) => ip!("3fff:ffff:ffff:ffff::ffff:fffe"), @@ -809,51 +921,123 @@ fn basic_update_helper< .update_session(peer_config, session_info) .expect("update nexthop"); - // Verify nexthop change is reflected in re-advertised route + // Verify all routes reflect the new nexthop wait_for!( - { - let paths = r2.router.db.get_prefix_paths(&prefix_rdb); - !paths.is_empty() && paths[0].nexthop == new_nexthop - }, - "nexthop should be updated" + prefixes_v6.iter().all(|p| { + let paths = r2.router.db.get_prefix_paths(p); + paths.len() == 1 && paths[0].nexthop == new_nexthop + }), + "all routes should have updated nexthop" ); } - // Shut down r1 and verify withdrawal + // Shut down r1 and verify withdrawal of all routes r1.shutdown(); wait_for_neq!(r1_session.state(), FsmStateKind::Established); wait_for_neq!(r2_session.state(), FsmStateKind::Established); - wait_for!(r2.router.db.get_prefix_paths(&prefix_rdb).is_empty()); + wait_for!( + prefixes_v6.iter().all(|p| r2 + .router + .db + .get_prefix_paths(p) + .is_empty()), + "all IPv6 routes should be withdrawn" + ); } RouteExchange::DualStack { ipv4_nexthop, ipv6_nexthop, } => { - // Dual-stack: originate and verify both IPv4 and IPv6 prefixes + let prefixes_v4 = generate_test_prefixes_v4(TEST_ROUTE_COUNT); + let prefixes_v6 = generate_test_prefixes_v6(TEST_ROUTE_COUNT); + r1.router - .create_origin4(vec![cidr!("1.2.3.0/24")]) + .create_origin4(prefixes_v4.clone()) .expect("originate IPv4"); r1.router - .create_origin6(vec![cidr!("3fff:db8::/32")]) + .create_origin6(prefixes_v6.clone()) .expect("originate IPv6"); - let prefix4_rdb = Prefix::V4(cidr!("1.2.3.0/24")); - let prefix6_rdb = Prefix::V6(cidr!("3fff:db8::/32")); - - wait_for!(!r2.router.db.get_prefix_paths(&prefix4_rdb).is_empty()); - wait_for!(!r2.router.db.get_prefix_paths(&prefix6_rdb).is_empty()); + // Wait for all IPv4 and IPv6 routes to arrive at R2 + wait_for!( + { + let v4_count = prefixes_v4 + .iter() + .filter(|p| { + !r2.router.db.get_prefix_paths(p).is_empty() + }) + .count(); + let v6_count = prefixes_v6 + .iter() + .filter(|p| { + !r2.router.db.get_prefix_paths(p).is_empty() + }) + .count(); + v4_count == TEST_ROUTE_COUNT && v6_count == TEST_ROUTE_COUNT + }, + "all dual-stack routes should arrive at R2" + ); + + // Verify attributes on every received IPv4 route + for prefix in &prefixes_v4 { + let paths = r2.router.db.get_prefix_paths(prefix); + assert_eq!(paths.len(), 1, "{prefix:?}: expected 1 path"); + let path = &paths[0]; + + if let Some(expected_nh) = ipv4_nexthop { + assert_eq!( + path.nexthop, expected_nh, + "{prefix:?}: nexthop mismatch" + ); + } - // Verify initial nexthops if configured - let paths4 = r2.router.db.get_prefix_paths(&prefix4_rdb); - assert_eq!(paths4.len(), 1); - if let Some(expected_nexthop) = ipv4_nexthop { - assert_eq!(paths4[0].nexthop, expected_nexthop); + let bgp = path.bgp.as_ref().unwrap_or_else(|| { + panic!("{prefix:?}: missing BGP properties") + }); + assert_eq!( + bgp.origin_as, 4200000001, + "{prefix:?}: origin_as mismatch" + ); + assert_eq!( + bgp.as_path, + vec![4200000001u32], + "{prefix:?}: as_path mismatch" + ); + assert!( + bgp.stale.is_none(), + "{prefix:?}: route should not be stale" + ); } - let paths6 = r2.router.db.get_prefix_paths(&prefix6_rdb); - assert_eq!(paths6.len(), 1); - if let Some(expected_nexthop) = ipv6_nexthop { - assert_eq!(paths6[0].nexthop, expected_nexthop); + // Verify attributes on every received IPv6 route + for prefix in &prefixes_v6 { + let paths = r2.router.db.get_prefix_paths(prefix); + assert_eq!(paths.len(), 1, "{prefix:?}: expected 1 path"); + let path = &paths[0]; + + if let Some(expected_nh) = ipv6_nexthop { + assert_eq!( + path.nexthop, expected_nh, + "{prefix:?}: nexthop mismatch" + ); + } + + let bgp = path.bgp.as_ref().unwrap_or_else(|| { + panic!("{prefix:?}: missing BGP properties") + }); + assert_eq!( + bgp.origin_as, 4200000001, + "{prefix:?}: origin_as mismatch" + ); + assert_eq!( + bgp.as_path, + vec![4200000001u32], + "{prefix:?}: as_path mismatch" + ); + assert!( + bgp.stale.is_none(), + "{prefix:?}: route should not be stale" + ); } // Test nexthop override changes if any were configured @@ -893,37 +1077,49 @@ fn basic_update_helper< .update_session(peer_config, session_info) .expect("update nexthop"); - // Verify IPv4 nexthop change if applicable + // Verify all IPv4 routes reflect the new nexthop if let Some(new_nh) = new_ipv4_nexthop { wait_for!( - { - let paths = - r2.router.db.get_prefix_paths(&prefix4_rdb); - !paths.is_empty() && paths[0].nexthop == new_nh - }, - "IPv4 nexthop should be updated" + prefixes_v4.iter().all(|p| { + let paths = r2.router.db.get_prefix_paths(p); + paths.len() == 1 && paths[0].nexthop == new_nh + }), + "all IPv4 routes should have updated nexthop" ); } - // Verify IPv6 nexthop change if applicable + // Verify all IPv6 routes reflect the new nexthop if let Some(new_nh) = new_ipv6_nexthop { wait_for!( - { - let paths = - r2.router.db.get_prefix_paths(&prefix6_rdb); - !paths.is_empty() && paths[0].nexthop == new_nh - }, - "IPv6 nexthop should be updated" + prefixes_v6.iter().all(|p| { + let paths = r2.router.db.get_prefix_paths(p); + paths.len() == 1 && paths[0].nexthop == new_nh + }), + "all IPv6 routes should have updated nexthop" ); } } - // Shut down r1 and verify withdrawal of both + // Shut down r1 and verify withdrawal of all routes r1.shutdown(); wait_for_neq!(r1_session.state(), FsmStateKind::Established); wait_for_neq!(r2_session.state(), FsmStateKind::Established); - wait_for!(r2.router.db.get_prefix_paths(&prefix4_rdb).is_empty()); - wait_for!(r2.router.db.get_prefix_paths(&prefix6_rdb).is_empty()); + wait_for!( + prefixes_v4.iter().all(|p| r2 + .router + .db + .get_prefix_paths(p) + .is_empty()), + "all IPv4 routes should be withdrawn" + ); + wait_for!( + prefixes_v6.iter().all(|p| r2 + .router + .db + .get_prefix_paths(p) + .is_empty()), + "all IPv6 routes should be withdrawn" + ); } } diff --git a/rdb-types/src/lib.rs b/rdb-types/src/lib.rs index 4705985ce..a046878ec 100644 --- a/rdb-types/src/lib.rs +++ b/rdb-types/src/lib.rs @@ -103,6 +103,13 @@ impl Prefix4 { self_masked == other_masked } + /// Wire size of this prefix: 1 byte for the length field plus the + /// minimum number of bytes needed to hold `length` bits. + pub fn wire_len(&self) -> usize { + // prefix-length byte + ceil(length / 8) address bytes + 1 + usize::from(self.length).div_ceil(8) + } + /// Check if a prefix contains a subnet that is valid for use in the RIB. /// Currently this only checks if the prefix overlaps with Loopback /// (127.0.0.0/8) or Multicast (224.0.0.0/4) address space. We deliberately @@ -231,6 +238,13 @@ impl Prefix6 { self_masked == other_masked } + /// Wire size of this prefix: 1 byte for the length field plus the + /// minimum number of bytes needed to hold `length` bits. + pub fn wire_len(&self) -> usize { + // prefix-length byte + ceil(length / 8) address bytes + 1 + usize::from(self.length).div_ceil(8) + } + /// Check if a prefix contains a subnet that is valid for use in the RIB. /// Currently this only checks if the prefix carries the Unspecified or /// Loopback address (::/128 or ::1/128), Multicast (ff00::/8) or Link-Local From ff9383da33c2e476bf3f151a4e2d2ae90e9e8900 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 11 Feb 2026 22:09:12 -0700 Subject: [PATCH 04/58] Ensure an AFI is enabled for unnumbered peers too --- bgp/src/params.rs | 24 ++++++++++++++++++------ mgd/src/bgp_admin.rs | 4 ++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/bgp/src/params.rs b/bgp/src/params.rs index 026812080..57676835b 100644 --- a/bgp/src/params.rs +++ b/bgp/src/params.rs @@ -205,12 +205,14 @@ pub struct Neighbor { impl Neighbor { /// Validate that at least one address family is enabled pub fn validate_address_families(&self) -> Result<(), String> { - if self.parameters.ipv4_unicast.is_none() - && self.parameters.ipv6_unicast.is_none() - { - return Err("at least one address family must be enabled".into()); - } - Ok(()) + self.parameters.validate_address_families() + } +} + +impl UnnumberedNeighbor { + /// Validate that at least one address family is enabled + pub fn validate_address_families(&self) -> Result<(), String> { + self.parameters.validate_address_families() } } @@ -921,6 +923,16 @@ pub struct BgpPeerParameters { pub connect_retry_jitter: Option, } +impl BgpPeerParameters { + /// Validate that at least one address family is enabled + pub fn validate_address_families(&self) -> Result<(), String> { + if self.ipv4_unicast.is_none() && self.ipv6_unicast.is_none() { + return Err("at least one address family must be enabled".into()); + } + Ok(()) + } +} + #[derive(Debug, Deserialize, Serialize, JsonSchema, Clone, PartialEq)] pub struct BgpPeerParametersV1 { pub hold_time: u64, diff --git a/mgd/src/bgp_admin.rs b/mgd/src/bgp_admin.rs index d57379090..1c0125def 100644 --- a/mgd/src/bgp_admin.rs +++ b/mgd/src/bgp_admin.rs @@ -2167,6 +2167,10 @@ pub(crate) mod helpers { "params" => format!("{rq:#?}") ); + // Validate that at least one AF is enabled + rq.validate_address_families() + .map_err(Error::InvalidRequest)?; + let (event_tx, event_rx) = channel(); let info = SessionInfo::from(&rq.parameters); From b50d9ce9bb68c81f50b5081fbeea236325300519 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 11 Feb 2026 22:45:55 -0700 Subject: [PATCH 05/58] static: add extended next-hop support Allow static routes to use cross-address-family next-hops (e.g. IPv4 prefix via IPv6 next-hop and vice versa). Introduce API version 7 (EXTENDED_NH_STATIC) with StaticRoute4 and StaticRoute6 types that accept IpAddr for the nexthop field instead of the AF-specific Ipv4Addr/Ipv6Addr. The previous types are retired as V1 variants with version-gated endpoints. The internal layers (StaticRouteKey, Path, RIB) already handle cross-AF next-hops; this closes the gap at the API boundary. Closes: #581 --- mg-api/src/lib.rs | 150 +- mgadm/src/static_routing.rs | 74 +- mgd/src/admin.rs | 50 +- mgd/src/static_admin.rs | 98 +- openapi/mg-admin/mg-admin-7.0.0-f92782.json | 5787 +++++++++++++++++++ openapi/mg-admin/mg-admin-latest.json | 2 +- rdb/src/db.rs | 215 + 7 files changed, 6337 insertions(+), 39 deletions(-) create mode 100644 openapi/mg-admin/mg-admin-7.0.0-f92782.json diff --git a/mg-api/src/lib.rs b/mg-api/src/lib.rs index 53880c692..785edd5d2 100644 --- a/mg-api/src/lib.rs +++ b/mg-api/src/lib.rs @@ -43,6 +43,7 @@ api_versions!([ // | example for the next person. // v // (next_int, IDENT), + (7, EXTENDED_NH_STATIC), (6, RIB_EXPORTED_STRING_KEY), (5, UNNUMBERED), (4, MP_BGP), @@ -545,41 +546,76 @@ pub trait MgAdminApi { request: TypedBody, ) -> Result; - #[endpoint { method = PUT, path = "/static/route4" }] + // V1 static route endpoints (pre-EXTENDED_NH_STATIC): typed nexthop per AF + #[endpoint { method = PUT, path = "/static/route4", versions = ..VERSION_EXTENDED_NH_STATIC }] async fn static_add_v4_route( rqctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result; - #[endpoint { method = DELETE, path = "/static/route4" }] + #[endpoint { method = DELETE, path = "/static/route4", versions = ..VERSION_EXTENDED_NH_STATIC }] async fn static_remove_v4_route( rqctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result; - #[endpoint { method = GET, path = "/static/route4" }] + #[endpoint { method = GET, path = "/static/route4", versions = ..VERSION_EXTENDED_NH_STATIC }] async fn static_list_v4_routes( rqctx: RequestContext, ) -> Result, HttpError>; - // IPv6 static routes introduced in VERSION_IPV6_BASIC - #[endpoint { method = PUT, path = "/static/route6", versions = VERSION_IPV6_BASIC.. }] + #[endpoint { method = PUT, path = "/static/route6", versions = VERSION_IPV6_BASIC..VERSION_EXTENDED_NH_STATIC }] async fn static_add_v6_route( ctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result; - #[endpoint { method = DELETE, path = "/static/route6", versions = VERSION_IPV6_BASIC.. }] + #[endpoint { method = DELETE, path = "/static/route6", versions = VERSION_IPV6_BASIC..VERSION_EXTENDED_NH_STATIC }] async fn static_remove_v6_route( ctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result; - #[endpoint { method = GET, path = "/static/route6", versions = VERSION_IPV6_BASIC.. }] + #[endpoint { method = GET, path = "/static/route6", versions = VERSION_IPV6_BASIC..VERSION_EXTENDED_NH_STATIC }] async fn static_list_v6_routes( ctx: RequestContext, ) -> Result, HttpError>; + // V2 static route endpoints (VERSION_EXTENDED_NH_STATIC+): IpAddr nexthop + #[endpoint { method = PUT, path = "/static/route4", versions = VERSION_EXTENDED_NH_STATIC.. }] + async fn static_add_v4_route_v2( + rqctx: RequestContext, + request: TypedBody, + ) -> Result; + + #[endpoint { method = DELETE, path = "/static/route4", versions = VERSION_EXTENDED_NH_STATIC.. }] + async fn static_remove_v4_route_v2( + rqctx: RequestContext, + request: TypedBody, + ) -> Result; + + #[endpoint { method = GET, path = "/static/route4", versions = VERSION_EXTENDED_NH_STATIC.. }] + async fn static_list_v4_routes_v2( + rqctx: RequestContext, + ) -> Result, HttpError>; + + #[endpoint { method = PUT, path = "/static/route6", versions = VERSION_EXTENDED_NH_STATIC.. }] + async fn static_add_v6_route_v2( + ctx: RequestContext, + request: TypedBody, + ) -> Result; + + #[endpoint { method = DELETE, path = "/static/route6", versions = VERSION_EXTENDED_NH_STATIC.. }] + async fn static_remove_v6_route_v2( + ctx: RequestContext, + request: TypedBody, + ) -> Result; + + #[endpoint { method = GET, path = "/static/route6", versions = VERSION_EXTENDED_NH_STATIC.. }] + async fn static_list_v6_routes_v2( + ctx: RequestContext, + ) -> Result, HttpError>; + #[endpoint {method = GET, path = "/switch/identifiers", versions = VERSION_SWITCH_IDENTIFIERS.. }] async fn switch_identifiers( ctx: RequestContext, @@ -880,6 +916,90 @@ pub struct BestpathFanoutResponse { pub type GetRibResult = BTreeMap>; +// ============================================================================ +// Archived Static Route Types (Pre-VERSION_EXTENDED_NH_STATIC) +// ============================================================================ + +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +#[schemars(rename = "AddStaticRoute4Request")] +pub struct AddStaticRoute4V1Request { + pub routes: StaticRoute4V1List, +} + +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +#[schemars(rename = "DeleteStaticRoute4Request")] +pub struct DeleteStaticRoute4V1Request { + pub routes: StaticRoute4V1List, +} + +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +#[schemars(rename = "StaticRoute4List")] +pub struct StaticRoute4V1List { + pub list: Vec, +} + +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +#[schemars(rename = "StaticRoute4")] +pub struct StaticRoute4V1 { + pub prefix: Prefix4, + pub nexthop: Ipv4Addr, + pub vlan_id: Option, + pub rib_priority: u8, +} + +impl From for StaticRouteKey { + fn from(val: StaticRoute4V1) -> Self { + StaticRouteKey { + prefix: val.prefix.into(), + nexthop: val.nexthop.into(), + vlan_id: val.vlan_id, + rib_priority: val.rib_priority, + } + } +} + +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +#[schemars(rename = "AddStaticRoute6Request")] +pub struct AddStaticRoute6V1Request { + pub routes: StaticRoute6V1List, +} + +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +#[schemars(rename = "DeleteStaticRoute6Request")] +pub struct DeleteStaticRoute6V1Request { + pub routes: StaticRoute6V1List, +} + +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +#[schemars(rename = "StaticRoute6List")] +pub struct StaticRoute6V1List { + pub list: Vec, +} + +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +#[schemars(rename = "StaticRoute6")] +pub struct StaticRoute6V1 { + pub prefix: Prefix6, + pub nexthop: Ipv6Addr, + pub vlan_id: Option, + pub rib_priority: u8, +} + +impl From for StaticRouteKey { + fn from(val: StaticRoute6V1) -> Self { + StaticRouteKey { + prefix: val.prefix.into(), + nexthop: val.nexthop.into(), + vlan_id: val.vlan_id, + rib_priority: val.rib_priority, + } + } +} + +// ============================================================================ +// Current Static Route Types (VERSION_EXTENDED_NH_STATIC and later) +// ============================================================================ + #[derive(Debug, Deserialize, Serialize, JsonSchema)] pub struct AddStaticRoute4Request { pub routes: StaticRoute4List, @@ -898,7 +1018,7 @@ pub struct StaticRoute4List { #[derive(Debug, Deserialize, Serialize, JsonSchema)] pub struct StaticRoute4 { pub prefix: Prefix4, - pub nexthop: Ipv4Addr, + pub nexthop: IpAddr, pub vlan_id: Option, pub rib_priority: u8, } @@ -907,7 +1027,7 @@ impl From for StaticRouteKey { fn from(val: StaticRoute4) -> Self { StaticRouteKey { prefix: val.prefix.into(), - nexthop: val.nexthop.into(), + nexthop: val.nexthop, vlan_id: val.vlan_id, rib_priority: val.rib_priority, } @@ -932,7 +1052,7 @@ pub struct StaticRoute6List { #[derive(Debug, Deserialize, Serialize, JsonSchema)] pub struct StaticRoute6 { pub prefix: Prefix6, - pub nexthop: Ipv6Addr, + pub nexthop: IpAddr, pub vlan_id: Option, pub rib_priority: u8, } @@ -941,7 +1061,7 @@ impl From for StaticRouteKey { fn from(val: StaticRoute6) -> Self { StaticRouteKey { prefix: val.prefix.into(), - nexthop: val.nexthop.into(), + nexthop: val.nexthop, vlan_id: val.vlan_id, rib_priority: val.rib_priority, } diff --git a/mgadm/src/static_routing.rs b/mgadm/src/static_routing.rs index 761f187ef..d806a709b 100644 --- a/mgadm/src/static_routing.rs +++ b/mgadm/src/static_routing.rs @@ -7,7 +7,7 @@ use clap::{Args, Subcommand}; use mg_admin_client::{Client, types}; use oxnet::{Ipv4Net, Ipv6Net}; use rdb::{DEFAULT_RIB_PRIORITY_STATIC, Prefix4, Prefix6}; -use std::net::{Ipv4Addr, Ipv6Addr}; +use std::net::IpAddr; #[derive(Subcommand, Debug)] pub enum Commands { @@ -22,7 +22,7 @@ pub enum Commands { #[derive(Debug, Args)] pub struct StaticRoute4 { pub destination: Ipv4Net, - pub nexthop: Ipv4Addr, + pub nexthop: IpAddr, #[clap(long)] pub vlan_id: Option, #[clap(long, default_value_t = DEFAULT_RIB_PRIORITY_STATIC)] @@ -32,7 +32,7 @@ pub struct StaticRoute4 { #[derive(Debug, Args)] pub struct StaticRoute6 { pub destination: Ipv6Net, - pub nexthop: Ipv6Addr, + pub nexthop: IpAddr, #[clap(long)] pub vlan_id: Option, #[clap(long, default_value_t = DEFAULT_RIB_PRIORITY_STATIC)] @@ -42,7 +42,7 @@ pub struct StaticRoute6 { pub async fn commands(command: Commands, client: Client) -> Result<()> { match command { Commands::GetV4Routes => { - let routes = client.static_list_v4_routes().await?; + let routes = client.static_list_v4_routes_v2().await?; println!("{:#?}", routes); } Commands::AddV4Route(route) => { @@ -59,7 +59,7 @@ pub async fn commands(command: Commands, client: Client) -> Result<()> { }], }, }; - client.static_add_v4_route(&arg).await?; + client.static_add_v4_route_v2(&arg).await?; } Commands::RemoveV4Routes(route) => { let arg = types::DeleteStaticRoute4Request { @@ -75,10 +75,10 @@ pub async fn commands(command: Commands, client: Client) -> Result<()> { }], }, }; - client.static_remove_v4_route(&arg).await?; + client.static_remove_v4_route_v2(&arg).await?; } Commands::GetV6Routes => { - let routes = client.static_list_v6_routes().await?; + let routes = client.static_list_v6_routes_v2().await?; println!("{:#?}", routes); } Commands::AddV6Route(route) => { @@ -95,7 +95,7 @@ pub async fn commands(command: Commands, client: Client) -> Result<()> { }], }, }; - client.static_add_v6_route(&arg).await?; + client.static_add_v6_route_v2(&arg).await?; } Commands::RemoveV6Routes(route) => { let arg = types::DeleteStaticRoute6Request { @@ -111,7 +111,7 @@ pub async fn commands(command: Commands, client: Client) -> Result<()> { }], }, }; - client.static_remove_v6_route(&arg).await?; + client.static_remove_v6_route_v2(&arg).await?; } } Ok(()) @@ -120,14 +120,15 @@ pub async fn commands(command: Commands, client: Client) -> Result<()> { #[cfg(test)] mod tests { use super::*; + use std::net::{Ipv4Addr, Ipv6Addr}; use std::str::FromStr; #[test] fn test_route_conversion_to_api_types() { - // IPv4 test case + // IPv4 prefix with IPv4 nexthop let route4 = StaticRoute4 { destination: Ipv4Net::from_str("192.168.0.0/16").unwrap(), - nexthop: Ipv4Addr::from_str("10.0.0.1").unwrap(), + nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()), vlan_id: Some(100), rib_priority: 50, }; @@ -151,10 +152,10 @@ mod tests { assert_eq!(api_route4.vlan_id, route4.vlan_id); assert_eq!(api_route4.rib_priority, route4.rib_priority); - // IPv6 test case + // IPv6 prefix with IPv6 nexthop let route6 = StaticRoute6 { destination: Ipv6Net::from_str("fd00::/8").unwrap(), - nexthop: Ipv6Addr::from_str("fe80::1").unwrap(), + nexthop: IpAddr::V6(Ipv6Addr::from_str("fe80::1").unwrap()), vlan_id: Some(300), rib_priority: 75, }; @@ -177,5 +178,52 @@ mod tests { assert_eq!(api_route6.nexthop, route6.nexthop); assert_eq!(api_route6.vlan_id, route6.vlan_id); assert_eq!(api_route6.rib_priority, route6.rib_priority); + + // IPv4 prefix with IPv6 nexthop (extended next-hop) + let route4_v6nh = StaticRoute4 { + destination: Ipv4Net::from_str("10.0.0.0/8").unwrap(), + nexthop: IpAddr::V6(Ipv6Addr::from_str("fe80::1").unwrap()), + vlan_id: None, + rib_priority: 50, + }; + + let api_route4_v6nh = types::StaticRoute4 { + prefix: Prefix4::new( + route4_v6nh.destination.addr(), + route4_v6nh.destination.width(), + ), + nexthop: route4_v6nh.nexthop, + vlan_id: route4_v6nh.vlan_id, + rib_priority: route4_v6nh.rib_priority, + }; + + assert_eq!( + api_route4_v6nh.nexthop, + IpAddr::V6(Ipv6Addr::from_str("fe80::1").unwrap()) + ); + + // IPv6 prefix with IPv4 nexthop (extended next-hop) + let route6_v4nh = StaticRoute6 { + destination: Ipv6Net::from_str("2001:db8::/32").unwrap(), + nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()), + vlan_id: Some(42), + rib_priority: 50, + }; + + let api_route6_v4nh = types::StaticRoute6 { + prefix: Prefix6::new( + route6_v4nh.destination.addr(), + route6_v4nh.destination.width(), + ), + nexthop: route6_v4nh.nexthop, + vlan_id: route6_v4nh.vlan_id, + rib_priority: route6_v4nh.rib_priority, + }; + + assert_eq!( + api_route6_v4nh.nexthop, + IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()) + ); + assert_eq!(api_route6_v4nh.vlan_id, Some(42)); } } diff --git a/mgd/src/admin.rs b/mgd/src/admin.rs index 523a75221..0d2d86626 100644 --- a/mgd/src/admin.rs +++ b/mgd/src/admin.rs @@ -590,16 +590,17 @@ impl MgAdminApi for MgAdminApiImpl { rib_admin::update_rib_bestpath_fanout(rqctx, request).await } + // V1 static route methods (pre-EXTENDED_NH_STATIC) async fn static_add_v4_route( ctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result { static_admin::static_add_v4_route(ctx, request).await } async fn static_remove_v4_route( ctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result { static_admin::static_remove_v4_route(ctx, request).await } @@ -612,14 +613,14 @@ impl MgAdminApi for MgAdminApiImpl { async fn static_add_v6_route( ctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result { static_admin::static_add_v6_route(ctx, request).await } async fn static_remove_v6_route( ctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result { static_admin::static_remove_v6_route(ctx, request).await } @@ -630,6 +631,47 @@ impl MgAdminApi for MgAdminApiImpl { static_admin::static_list_v6_routes(ctx).await } + // V2 static route methods (VERSION_EXTENDED_NH_STATIC+) + async fn static_add_v4_route_v2( + ctx: RequestContext, + request: TypedBody, + ) -> Result { + static_admin::static_add_v4_route_v2(ctx, request).await + } + + async fn static_remove_v4_route_v2( + ctx: RequestContext, + request: TypedBody, + ) -> Result { + static_admin::static_remove_v4_route_v2(ctx, request).await + } + + async fn static_list_v4_routes_v2( + ctx: RequestContext, + ) -> Result, HttpError> { + static_admin::static_list_v4_routes(ctx).await + } + + async fn static_add_v6_route_v2( + ctx: RequestContext, + request: TypedBody, + ) -> Result { + static_admin::static_add_v6_route_v2(ctx, request).await + } + + async fn static_remove_v6_route_v2( + ctx: RequestContext, + request: TypedBody, + ) -> Result { + static_admin::static_remove_v6_route_v2(ctx, request).await + } + + async fn static_list_v6_routes_v2( + ctx: RequestContext, + ) -> Result, HttpError> { + static_admin::static_list_v6_routes(ctx).await + } + async fn switch_identifiers( ctx: RequestContext, ) -> Result, HttpError> { diff --git a/mgd/src/static_admin.rs b/mgd/src/static_admin.rs index 098ac7264..6ecd386a6 100644 --- a/mgd/src/static_admin.rs +++ b/mgd/src/static_admin.rs @@ -9,15 +9,19 @@ use dropshot::{ HttpResponseUpdatedNoContent, RequestContext, TypedBody, }; use mg_api::{ - AddStaticRoute4Request, AddStaticRoute6Request, DeleteStaticRoute4Request, - DeleteStaticRoute6Request, GetRibResult, + AddStaticRoute4Request, AddStaticRoute4V1Request, AddStaticRoute6Request, + AddStaticRoute6V1Request, DeleteStaticRoute4Request, + DeleteStaticRoute4V1Request, DeleteStaticRoute6Request, + DeleteStaticRoute6V1Request, GetRibResult, }; use rdb::{AddressFamily, Prefix, StaticRouteKey}; use std::{collections::BTreeMap, sync::Arc}; +// V1 handlers (pre-EXTENDED_NH_STATIC): typed nexthop per AF + pub async fn static_add_v4_route( ctx: RequestContext>, - request: TypedBody, + request: TypedBody, ) -> Result { let routes: Vec = request .into_inner() @@ -40,7 +44,7 @@ pub async fn static_add_v4_route( pub async fn static_remove_v4_route( ctx: RequestContext>, - request: TypedBody, + request: TypedBody, ) -> Result { let routes: Vec = request .into_inner() @@ -77,7 +81,7 @@ pub async fn static_list_v4_routes( pub async fn static_add_v6_route( ctx: RequestContext>, - request: TypedBody, + request: TypedBody, ) -> Result { let routes: Vec = request .into_inner() @@ -100,7 +104,7 @@ pub async fn static_add_v6_route( pub async fn static_remove_v6_route( ctx: RequestContext>, - request: TypedBody, + request: TypedBody, ) -> Result { let routes: Vec = request .into_inner() @@ -135,6 +139,88 @@ pub async fn static_list_v6_routes( Ok(HttpResponseOk(static_rib)) } +// V2 handlers (VERSION_EXTENDED_NH_STATIC+): IpAddr nexthop + +pub async fn static_add_v4_route_v2( + ctx: RequestContext>, + request: TypedBody, +) -> Result { + let routes: Vec = request + .into_inner() + .routes + .list + .into_iter() + .map(Into::into) + .collect(); + + let prefixes: Vec = routes.iter().map(|r| r.prefix).collect(); + validate_prefixes(&prefixes)?; + + ctx.context() + .db + .add_static_routes(&routes) + .map_err(|e| HttpError::for_internal_error(e.to_string()))?; + Ok(HttpResponseUpdatedNoContent()) +} + +pub async fn static_remove_v4_route_v2( + ctx: RequestContext>, + request: TypedBody, +) -> Result { + let routes: Vec = request + .into_inner() + .routes + .list + .into_iter() + .map(Into::into) + .collect(); + ctx.context() + .db + .remove_static_routes(&routes) + .map_err(|e| HttpError::for_internal_error(e.to_string()))?; + Ok(HttpResponseDeleted()) +} + +pub async fn static_add_v6_route_v2( + ctx: RequestContext>, + request: TypedBody, +) -> Result { + let routes: Vec = request + .into_inner() + .routes + .list + .into_iter() + .map(Into::into) + .collect(); + + let prefixes: Vec = routes.iter().map(|r| r.prefix).collect(); + validate_prefixes(&prefixes)?; + + ctx.context() + .db + .add_static_routes(&routes) + .map_err(|e| HttpError::for_internal_error(e.to_string()))?; + Ok(HttpResponseUpdatedNoContent()) +} + +pub async fn static_remove_v6_route_v2( + ctx: RequestContext>, + request: TypedBody, +) -> Result { + let routes: Vec = request + .into_inner() + .routes + .list + .into_iter() + .map(Into::into) + .collect(); + ctx.context() + .db + .remove_static_routes(&routes) + .map_err(|e| HttpError::for_internal_error(e.to_string()))?; + Ok(HttpResponseDeleted()) +} + pub(crate) async fn switch_identifiers( ctx: RequestContext>, ) -> Result, HttpError> { diff --git a/openapi/mg-admin/mg-admin-7.0.0-f92782.json b/openapi/mg-admin/mg-admin-7.0.0-f92782.json new file mode 100644 index 000000000..924ee8f1c --- /dev/null +++ b/openapi/mg-admin/mg-admin-7.0.0-f92782.json @@ -0,0 +1,5787 @@ +{ + "openapi": "3.0.3", + "info": { + "title": "Maghemite Admin", + "contact": { + "url": "https://oxide.computer", + "email": "api@oxide.computer" + }, + "version": "7.0.0" + }, + "paths": { + "/bfd/peers": { + "get": { + "summary": "Get all the peers and their associated BFD state. Peers are identified by IP", + "description": "address.", + "operationId": "get_bfd_peers", + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "title": "Array_of_BfdPeerInfo", + "type": "array", + "items": { + "$ref": "#/components/schemas/BfdPeerInfo" + } + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "put": { + "summary": "Add a new peer to the daemon. A session for the specified peer will start", + "description": "immediately.", + "operationId": "add_bfd_peer", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/BfdPeerConfig" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bfd/peers/{addr}": { + "delete": { + "summary": "Remove the specified peer from the daemon. The associated peer session will", + "description": "be stopped immediately.", + "operationId": "remove_bfd_peer", + "parameters": [ + { + "in": "path", + "name": "addr", + "description": "Address of the peer to remove.", + "required": true, + "schema": { + "type": "string", + "format": "ip" + } + } + ], + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/clear/neighbor": { + "post": { + "operationId": "clear_neighbor_v2", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/NeighborResetRequest" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/clear/unnumbered-neighbor": { + "post": { + "operationId": "clear_unnumbered_neighbor", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UnnumberedNeighborResetRequest" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/config/checker": { + "get": { + "operationId": "read_checker", + "parameters": [ + { + "in": "query", + "name": "asn", + "description": "ASN of the router to get imported prefixes from.", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CheckerSource" + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "put": { + "operationId": "create_checker", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CheckerSource" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "post": { + "operationId": "update_checker", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CheckerSource" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "delete": { + "operationId": "delete_checker", + "parameters": [ + { + "in": "query", + "name": "asn", + "description": "ASN of the router to get imported prefixes from.", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + } + ], + "responses": { + "204": { + "description": "successful deletion" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/config/neighbor": { + "put": { + "operationId": "create_neighbor_v3", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Neighbor" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "post": { + "operationId": "update_neighbor_v3", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Neighbor" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/config/neighbor/{asn}/{peer}": { + "get": { + "operationId": "read_neighbor_v3", + "parameters": [ + { + "in": "path", + "name": "asn", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + }, + { + "in": "path", + "name": "peer", + "description": "Peer identifier as a string.\n\n- For numbered peers: IP address (e.g., \"192.0.2.1\" or \"2001:db8::1\") - For unnumbered peers: Interface name (e.g., \"eth0\" or \"cxgbe0\")\n\nServer parses as IP address first; if parsing fails, treats as interface name. Uses PeerId::from_str() for type-safe conversion.", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Neighbor" + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "delete": { + "operationId": "delete_neighbor_v3", + "parameters": [ + { + "in": "path", + "name": "asn", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + }, + { + "in": "path", + "name": "peer", + "description": "Peer identifier as a string.\n\n- For numbered peers: IP address (e.g., \"192.0.2.1\" or \"2001:db8::1\") - For unnumbered peers: Interface name (e.g., \"eth0\" or \"cxgbe0\")\n\nServer parses as IP address first; if parsing fails, treats as interface name. Uses PeerId::from_str() for type-safe conversion.", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "description": "successful deletion" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/config/neighbors/{asn}": { + "get": { + "operationId": "read_neighbors_v3", + "parameters": [ + { + "in": "path", + "name": "asn", + "description": "ASN of the router to get imported prefixes from.", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "title": "Array_of_Neighbor", + "type": "array", + "items": { + "$ref": "#/components/schemas/Neighbor" + } + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/config/origin4": { + "get": { + "operationId": "read_origin4", + "parameters": [ + { + "in": "query", + "name": "asn", + "description": "ASN of the router to get imported prefixes from.", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Origin4" + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "put": { + "operationId": "create_origin4", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Origin4" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "post": { + "operationId": "update_origin4", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Origin4" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "delete": { + "operationId": "delete_origin4", + "parameters": [ + { + "in": "query", + "name": "asn", + "description": "ASN of the router to get imported prefixes from.", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + } + ], + "responses": { + "204": { + "description": "successful deletion" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/config/origin6": { + "get": { + "operationId": "read_origin6", + "parameters": [ + { + "in": "query", + "name": "asn", + "description": "ASN of the router to get imported prefixes from.", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Origin6" + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "put": { + "operationId": "create_origin6", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Origin6" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "post": { + "operationId": "update_origin6", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Origin6" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "delete": { + "operationId": "delete_origin6", + "parameters": [ + { + "in": "query", + "name": "asn", + "description": "ASN of the router to get imported prefixes from.", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + } + ], + "responses": { + "204": { + "description": "successful deletion" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/config/router": { + "get": { + "operationId": "read_router", + "parameters": [ + { + "in": "query", + "name": "asn", + "description": "ASN of the router to get imported prefixes from.", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Router" + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "put": { + "operationId": "create_router", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Router" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "post": { + "operationId": "update_router", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Router" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "delete": { + "operationId": "delete_router", + "parameters": [ + { + "in": "query", + "name": "asn", + "description": "ASN of the router to get imported prefixes from.", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + } + ], + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/config/routers": { + "get": { + "operationId": "read_routers", + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "title": "Array_of_Router", + "type": "array", + "items": { + "$ref": "#/components/schemas/Router" + } + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/config/shaper": { + "get": { + "operationId": "read_shaper", + "parameters": [ + { + "in": "query", + "name": "asn", + "description": "ASN of the router to get imported prefixes from.", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ShaperSource" + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "put": { + "operationId": "create_shaper", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ShaperSource" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "post": { + "operationId": "update_shaper", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ShaperSource" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "delete": { + "operationId": "delete_shaper", + "parameters": [ + { + "in": "query", + "name": "asn", + "description": "ASN of the router to get imported prefixes from.", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + } + ], + "responses": { + "204": { + "description": "successful deletion" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/config/unnumbered-neighbor": { + "get": { + "operationId": "read_unnumbered_neighbor", + "parameters": [ + { + "in": "query", + "name": "asn", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + }, + { + "in": "query", + "name": "interface", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UnnumberedNeighbor" + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "put": { + "operationId": "create_unnumbered_neighbor", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UnnumberedNeighbor" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "post": { + "operationId": "update_unnumbered_neighbor", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UnnumberedNeighbor" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "delete": { + "operationId": "delete_unnumbered_neighbor", + "parameters": [ + { + "in": "query", + "name": "asn", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + }, + { + "in": "query", + "name": "interface", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "description": "successful deletion" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/config/unnumbered-neighbors": { + "get": { + "operationId": "read_unnumbered_neighbors", + "parameters": [ + { + "in": "query", + "name": "asn", + "description": "ASN of the router to get imported prefixes from.", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "title": "Array_of_UnnumberedNeighbor", + "type": "array", + "items": { + "$ref": "#/components/schemas/UnnumberedNeighbor" + } + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/history/fsm": { + "get": { + "operationId": "fsm_history_v2", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/FsmHistoryRequest" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/FsmHistoryResponse" + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/history/message": { + "get": { + "operationId": "message_history_v3", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/MessageHistoryRequest" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/MessageHistoryResponse" + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/omicron/apply": { + "post": { + "operationId": "bgp_apply_v2", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ApplyRequest" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/status/exported": { + "get": { + "operationId": "get_exported_v3", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ExportedSelector" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "title": "Map_of_Array_of_Prefix", + "type": "object", + "additionalProperties": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Prefix" + } + } + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/bgp/status/neighbors": { + "get": { + "operationId": "get_neighbors_v4", + "parameters": [ + { + "in": "query", + "name": "asn", + "description": "ASN of the router to get imported prefixes from.", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "title": "Map_of_PeerInfo", + "type": "object", + "additionalProperties": { + "$ref": "#/components/schemas/PeerInfo" + } + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/ndp/interface": { + "get": { + "operationId": "get_ndp_interface_detail", + "parameters": [ + { + "in": "query", + "name": "asn", + "description": "ASN of the router", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + }, + { + "in": "query", + "name": "interface", + "description": "Interface name", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/NdpInterface" + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/ndp/interfaces": { + "get": { + "operationId": "get_ndp_interfaces", + "parameters": [ + { + "in": "query", + "name": "asn", + "description": "ASN of the router to get imported prefixes from.", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "title": "Array_of_NdpInterface", + "type": "array", + "items": { + "$ref": "#/components/schemas/NdpInterface" + } + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/ndp/manager": { + "get": { + "operationId": "get_ndp_manager_state", + "parameters": [ + { + "in": "query", + "name": "asn", + "description": "ASN of the router to get imported prefixes from.", + "required": true, + "schema": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/NdpManagerState" + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/rib/config/bestpath/fanout": { + "get": { + "operationId": "read_rib_bestpath_fanout", + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/BestpathFanoutResponse" + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "post": { + "operationId": "update_rib_bestpath_fanout", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/BestpathFanoutRequest" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/rib/status/imported": { + "get": { + "operationId": "get_rib_imported_v2", + "parameters": [ + { + "in": "query", + "name": "address_family", + "description": "Filter by address family (None means all families)", + "schema": { + "$ref": "#/components/schemas/AddressFamily" + } + }, + { + "in": "query", + "name": "protocol", + "description": "Filter by protocol (optional)", + "schema": { + "$ref": "#/components/schemas/ProtocolFilter" + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Rib" + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/rib/status/selected": { + "get": { + "operationId": "get_rib_selected_v2", + "parameters": [ + { + "in": "query", + "name": "address_family", + "description": "Filter by address family (None means all families)", + "schema": { + "$ref": "#/components/schemas/AddressFamily" + } + }, + { + "in": "query", + "name": "protocol", + "description": "Filter by protocol (optional)", + "schema": { + "$ref": "#/components/schemas/ProtocolFilter" + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Rib" + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/static/route4": { + "get": { + "operationId": "static_list_v4_routes_v2", + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "title": "Map_of_Set_of_Path", + "type": "object", + "additionalProperties": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Path" + }, + "uniqueItems": true + } + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "put": { + "operationId": "static_add_v4_route_v2", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/AddStaticRoute4Request" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "delete": { + "operationId": "static_remove_v4_route_v2", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DeleteStaticRoute4Request" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "successful deletion" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/static/route6": { + "get": { + "operationId": "static_list_v6_routes_v2", + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "title": "Map_of_Set_of_Path", + "type": "object", + "additionalProperties": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Path" + }, + "uniqueItems": true + } + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "put": { + "operationId": "static_add_v6_route_v2", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/AddStaticRoute6Request" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "resource updated" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + }, + "delete": { + "operationId": "static_remove_v6_route_v2", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DeleteStaticRoute6Request" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "successful deletion" + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + }, + "/switch/identifiers": { + "get": { + "operationId": "switch_identifiers", + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/SwitchIdentifiers" + } + } + } + }, + "4XX": { + "$ref": "#/components/responses/Error" + }, + "5XX": { + "$ref": "#/components/responses/Error" + } + } + } + } + }, + "components": { + "schemas": { + "AddPathElement": { + "description": "The add path element comes as a BGP capability extension as described in RFC 7911.", + "type": "object", + "properties": { + "afi": { + "description": "Address family identifier. ", + "type": "integer", + "format": "uint16", + "minimum": 0 + }, + "safi": { + "description": "Subsequent address family identifier. There are a large pile of these ", + "type": "integer", + "format": "uint8", + "minimum": 0 + }, + "send_receive": { + "description": "This field indicates whether the sender is (a) able to receive multiple paths from its peer (value 1), (b) able to send multiple paths to its peer (value 2), or (c) both (value 3) for the .", + "type": "integer", + "format": "uint8", + "minimum": 0 + } + }, + "required": [ + "afi", + "safi", + "send_receive" + ] + }, + "AddStaticRoute4Request": { + "type": "object", + "properties": { + "routes": { + "$ref": "#/components/schemas/StaticRoute4List" + } + }, + "required": [ + "routes" + ] + }, + "AddStaticRoute6Request": { + "type": "object", + "properties": { + "routes": { + "$ref": "#/components/schemas/StaticRoute6List" + } + }, + "required": [ + "routes" + ] + }, + "Afi": { + "description": "Address families supported by Maghemite BGP.", + "oneOf": [ + { + "description": "Internet protocol version 4", + "type": "string", + "enum": [ + "Ipv4" + ] + }, + { + "description": "Internet protocol version 6", + "type": "string", + "enum": [ + "Ipv6" + ] + } + ] + }, + "AfiSafi": { + "type": "object", + "properties": { + "afi": { + "type": "integer", + "format": "uint16", + "minimum": 0 + }, + "safi": { + "type": "integer", + "format": "uint8", + "minimum": 0 + } + }, + "required": [ + "afi", + "safi" + ] + }, + "Aggregator": { + "description": "AGGREGATOR path attribute (RFC 4271 §5.1.8)\n\nThe AGGREGATOR attribute is an optional transitive attribute that contains the AS number and IP address of the last BGP speaker that formed the aggregate route.", + "type": "object", + "properties": { + "address": { + "description": "IP address of the BGP speaker that formed the aggregate", + "type": "string", + "format": "ipv4" + }, + "asn": { + "description": "Autonomous System Number that formed the aggregate (2-octet)", + "type": "integer", + "format": "uint16", + "minimum": 0 + } + }, + "required": [ + "address", + "asn" + ] + }, + "ApplyRequest": { + "description": "Apply changes to an ASN (current version with per-AF policies).", + "type": "object", + "properties": { + "asn": { + "description": "ASN to apply changes to.", + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "checker": { + "nullable": true, + "description": "Checker rhai code to apply to ingress open and update messages.", + "allOf": [ + { + "$ref": "#/components/schemas/CheckerSource" + } + ] + }, + "originate": { + "description": "Complete set of prefixes to originate.", + "type": "array", + "items": { + "$ref": "#/components/schemas/Prefix" + } + }, + "peers": { + "description": "Lists of peers indexed by peer group.", + "type": "object", + "additionalProperties": { + "type": "array", + "items": { + "$ref": "#/components/schemas/BgpPeerConfig" + } + } + }, + "shaper": { + "nullable": true, + "description": "Checker rhai code to apply to egress open and update messages.", + "allOf": [ + { + "$ref": "#/components/schemas/ShaperSource" + } + ] + }, + "unnumbered_peers": { + "description": "Lists of unnumbered peers indexed by peer group.", + "default": {}, + "type": "object", + "additionalProperties": { + "type": "array", + "items": { + "$ref": "#/components/schemas/UnnumberedBgpPeerConfig" + } + } + } + }, + "required": [ + "asn", + "originate", + "peers" + ] + }, + "As4Aggregator": { + "description": "AS4_AGGREGATOR path attribute (RFC 6793)\n\nThe AS4_AGGREGATOR attribute is an optional transitive attribute with the same semantics as AGGREGATOR, but carries a 4-octet AS number instead of 2-octet.", + "type": "object", + "properties": { + "address": { + "description": "IP address of the BGP speaker that formed the aggregate", + "type": "string", + "format": "ipv4" + }, + "asn": { + "description": "Autonomous System Number that formed the aggregate (4-octet)", + "type": "integer", + "format": "uint32", + "minimum": 0 + } + }, + "required": [ + "address", + "asn" + ] + }, + "As4PathSegment": { + "type": "object", + "properties": { + "typ": { + "$ref": "#/components/schemas/AsPathType" + }, + "value": { + "type": "array", + "items": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + } + }, + "required": [ + "typ", + "value" + ] + }, + "AsPathType": { + "description": "Enumeration describes possible AS path types", + "oneOf": [ + { + "description": "The path is to be interpreted as a set", + "type": "string", + "enum": [ + "as_set" + ] + }, + { + "description": "The path is to be interpreted as a sequence", + "type": "string", + "enum": [ + "as_sequence" + ] + } + ] + }, + "BestpathFanoutRequest": { + "type": "object", + "properties": { + "fanout": { + "description": "Maximum number of equal-cost paths for ECMP forwarding", + "type": "integer", + "format": "uint8", + "minimum": 1 + } + }, + "required": [ + "fanout" + ] + }, + "BestpathFanoutResponse": { + "type": "object", + "properties": { + "fanout": { + "description": "Current maximum number of equal-cost paths for ECMP forwarding", + "type": "integer", + "format": "uint8", + "minimum": 1 + } + }, + "required": [ + "fanout" + ] + }, + "BfdPeerConfig": { + "type": "object", + "properties": { + "detection_threshold": { + "description": "Detection threshold for connectivity as a multipler to required_rx", + "type": "integer", + "format": "uint8", + "minimum": 0 + }, + "listen": { + "description": "Address to listen on for control messages from the peer.", + "type": "string", + "format": "ip" + }, + "mode": { + "description": "Mode is single-hop (RFC 5881) or multi-hop (RFC 5883).", + "allOf": [ + { + "$ref": "#/components/schemas/SessionMode" + } + ] + }, + "peer": { + "description": "Address of the peer to add.", + "type": "string", + "format": "ip" + }, + "required_rx": { + "description": "Acceptable time between control messages in microseconds.", + "type": "integer", + "format": "uint64", + "minimum": 0 + } + }, + "required": [ + "detection_threshold", + "listen", + "mode", + "peer", + "required_rx" + ] + }, + "BfdPeerInfo": { + "type": "object", + "properties": { + "config": { + "$ref": "#/components/schemas/BfdPeerConfig" + }, + "state": { + "$ref": "#/components/schemas/BfdPeerState" + } + }, + "required": [ + "config", + "state" + ] + }, + "BfdPeerState": { + "description": "The possible peer states. See the `State` trait implementations `Down`, `Init`, and `Up` for detailed semantics. Data representation is u8 as this enum is used as a part of the BFD wire protocol.", + "oneOf": [ + { + "description": "A stable down state. Non-responsive to incoming messages.", + "type": "string", + "enum": [ + "AdminDown" + ] + }, + { + "description": "The initial state.", + "type": "string", + "enum": [ + "Down" + ] + }, + { + "description": "The peer has detected a remote peer in the down state.", + "type": "string", + "enum": [ + "Init" + ] + }, + { + "description": "The peer has detected a remote peer in the up or init state while in the init state.", + "type": "string", + "enum": [ + "Up" + ] + } + ] + }, + "BgpCapability": { + "oneOf": [ + { + "type": "string", + "enum": [ + "RouteRefresh" + ] + }, + { + "type": "object", + "properties": { + "MultiprotocolExtensions": { + "$ref": "#/components/schemas/AfiSafi" + } + }, + "required": [ + "MultiprotocolExtensions" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "FourOctetAsn": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + }, + "required": [ + "FourOctetAsn" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "AddPath": { + "type": "object", + "properties": { + "elements": { + "type": "array", + "items": { + "$ref": "#/components/schemas/AfiSafi" + } + } + }, + "required": [ + "elements" + ] + } + }, + "required": [ + "AddPath" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "Unknown": { + "type": "integer", + "format": "uint8", + "minimum": 0 + } + }, + "required": [ + "Unknown" + ], + "additionalProperties": false + } + ] + }, + "BgpNexthop": { + "description": "A BGP next-hop address in one of three formats: IPv4, IPv6 single, or IPv6 double.", + "oneOf": [ + { + "type": "object", + "properties": { + "Ipv4": { + "type": "string", + "format": "ipv4" + } + }, + "required": [ + "Ipv4" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "Ipv6Single": { + "type": "string", + "format": "ipv6" + } + }, + "required": [ + "Ipv6Single" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "Ipv6Double": { + "$ref": "#/components/schemas/Ipv6DoubleNexthop" + } + }, + "required": [ + "Ipv6Double" + ], + "additionalProperties": false + } + ] + }, + "BgpPathProperties": { + "type": "object", + "properties": { + "as_path": { + "type": "array", + "items": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + }, + "id": { + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "local_pref": { + "nullable": true, + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "med": { + "nullable": true, + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "origin_as": { + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "peer": { + "$ref": "#/components/schemas/PeerId" + }, + "stale": { + "nullable": true, + "type": "string", + "format": "date-time" + } + }, + "required": [ + "as_path", + "id", + "origin_as", + "peer" + ] + }, + "BgpPeerConfig": { + "type": "object", + "properties": { + "communities": { + "type": "array", + "items": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + }, + "connect_retry": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "connect_retry_jitter": { + "nullable": true, + "description": "Jitter range for connect_retry timer. When used, the connect_retry timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", + "allOf": [ + { + "$ref": "#/components/schemas/JitterRange" + } + ] + }, + "delay_open": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "deterministic_collision_resolution": { + "description": "Enable deterministic collision resolution in Established state. When true, uses BGP-ID comparison per RFC 4271 §6.8 for collision resolution even when one connection is already in Established state. When false, Established connection always wins (timing-based resolution).", + "type": "boolean" + }, + "enforce_first_as": { + "type": "boolean" + }, + "hold_time": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "host": { + "type": "string" + }, + "idle_hold_jitter": { + "nullable": true, + "description": "Jitter range for idle hold timer. When used, the idle hold timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", + "allOf": [ + { + "$ref": "#/components/schemas/JitterRange" + } + ] + }, + "idle_hold_time": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "ipv4_unicast": { + "nullable": true, + "description": "IPv4 Unicast address family configuration (None = disabled)", + "allOf": [ + { + "$ref": "#/components/schemas/Ipv4UnicastConfig" + } + ] + }, + "ipv6_unicast": { + "nullable": true, + "description": "IPv6 Unicast address family configuration (None = disabled)", + "allOf": [ + { + "$ref": "#/components/schemas/Ipv6UnicastConfig" + } + ] + }, + "keepalive": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "local_pref": { + "nullable": true, + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "md5_auth_key": { + "nullable": true, + "type": "string" + }, + "min_ttl": { + "nullable": true, + "type": "integer", + "format": "uint8", + "minimum": 0 + }, + "multi_exit_discriminator": { + "nullable": true, + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "name": { + "type": "string" + }, + "passive": { + "type": "boolean" + }, + "remote_asn": { + "nullable": true, + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "resolution": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "vlan_id": { + "nullable": true, + "type": "integer", + "format": "uint16", + "minimum": 0 + } + }, + "required": [ + "communities", + "connect_retry", + "delay_open", + "deterministic_collision_resolution", + "enforce_first_as", + "hold_time", + "host", + "idle_hold_time", + "keepalive", + "name", + "passive", + "resolution" + ] + }, + "Capability": { + "description": "Optional capabilities supported by a BGP implementation.", + "oneOf": [ + { + "description": "Multiprotocol extensions as defined in RFC 2858", + "type": "object", + "properties": { + "multiprotocol_extensions": { + "type": "object", + "properties": { + "afi": { + "type": "integer", + "format": "uint16", + "minimum": 0 + }, + "safi": { + "type": "integer", + "format": "uint8", + "minimum": 0 + } + }, + "required": [ + "afi", + "safi" + ] + } + }, + "required": [ + "multiprotocol_extensions" + ], + "additionalProperties": false + }, + { + "description": "Route refresh capability as defined in RFC 2918.", + "type": "object", + "properties": { + "route_refresh": { + "type": "object" + } + }, + "required": [ + "route_refresh" + ], + "additionalProperties": false + }, + { + "description": "Outbound filtering capability as defined in RFC 5291. Note this capability is not yet implemented.", + "type": "object", + "properties": { + "outbound_route_filtering": { + "type": "object" + } + }, + "required": [ + "outbound_route_filtering" + ], + "additionalProperties": false + }, + { + "description": "Multiple routes to destination capability as defined in RFC 8277 (deprecated). Note this capability is not yet implemented.", + "type": "object", + "properties": { + "multiple_routes_to_destination": { + "type": "object" + } + }, + "required": [ + "multiple_routes_to_destination" + ], + "additionalProperties": false + }, + { + "description": "Multiple nexthop encoding capability as defined in RFC 8950.", + "type": "object", + "properties": { + "extended_next_hop_encoding": { + "type": "object" + } + }, + "required": [ + "extended_next_hop_encoding" + ], + "additionalProperties": false + }, + { + "description": "Extended message capability as defined in RFC 8654. Note this capability is not yet implemented.", + "type": "object", + "properties": { + "b_g_p_extended_message": { + "type": "object" + } + }, + "required": [ + "b_g_p_extended_message" + ], + "additionalProperties": false + }, + { + "description": "BGPSec as defined in RFC 8205. Note this capability is not yet implemented.", + "type": "object", + "properties": { + "bgp_sec": { + "type": "object" + } + }, + "required": [ + "bgp_sec" + ], + "additionalProperties": false + }, + { + "description": "Multiple label support as defined in RFC 8277. Note this capability is not yet implemented.", + "type": "object", + "properties": { + "multiple_labels": { + "type": "object" + } + }, + "required": [ + "multiple_labels" + ], + "additionalProperties": false + }, + { + "description": "BGP role capability as defined in RFC 9234. Note this capability is not yet implemented.", + "type": "object", + "properties": { + "bgp_role": { + "type": "object" + } + }, + "required": [ + "bgp_role" + ], + "additionalProperties": false + }, + { + "description": "Graceful restart as defined in RFC 4724. Note this capability is not yet implemented.", + "type": "object", + "properties": { + "graceful_restart": { + "type": "object" + } + }, + "required": [ + "graceful_restart" + ], + "additionalProperties": false + }, + { + "description": "Four octet AS numbers as defined in RFC 6793.", + "type": "object", + "properties": { + "four_octet_as": { + "type": "object", + "properties": { + "asn": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + }, + "required": [ + "asn" + ] + } + }, + "required": [ + "four_octet_as" + ], + "additionalProperties": false + }, + { + "description": "Dynamic capabilities as defined in draft-ietf-idr-dynamic-cap. Note this capability is not yet implemented.", + "type": "object", + "properties": { + "dynamic_capability": { + "type": "object" + } + }, + "required": [ + "dynamic_capability" + ], + "additionalProperties": false + }, + { + "description": "Multi session support as defined in draft-ietf-idr-bgp-multisession. Note this capability is not yet supported.", + "type": "object", + "properties": { + "multisession_bgp": { + "type": "object" + } + }, + "required": [ + "multisession_bgp" + ], + "additionalProperties": false + }, + { + "description": "Add path capability as defined in RFC 7911.", + "type": "object", + "properties": { + "add_path": { + "type": "object", + "properties": { + "elements": { + "type": "array", + "items": { + "$ref": "#/components/schemas/AddPathElement" + }, + "uniqueItems": true + } + }, + "required": [ + "elements" + ] + } + }, + "required": [ + "add_path" + ], + "additionalProperties": false + }, + { + "description": "Enhanced route refresh as defined in RFC 7313. Note this capability is not yet supported.", + "type": "object", + "properties": { + "enhanced_route_refresh": { + "type": "object" + } + }, + "required": [ + "enhanced_route_refresh" + ], + "additionalProperties": false + }, + { + "description": "Long-lived graceful restart as defined in draft-uttaro-idr-bgp-persistence. Note this capability is not yet supported.", + "type": "object", + "properties": { + "long_lived_graceful_restart": { + "type": "object" + } + }, + "required": [ + "long_lived_graceful_restart" + ], + "additionalProperties": false + }, + { + "description": "Routing policy distribution as defined indraft-ietf-idr-rpd-04. Note this capability is not yet supported.", + "type": "object", + "properties": { + "routing_policy_distribution": { + "type": "object" + } + }, + "required": [ + "routing_policy_distribution" + ], + "additionalProperties": false + }, + { + "description": "Fully qualified domain names as defined intdraft-walton-bgp-hostname-capability. Note this capability is not yet supported.", + "type": "object", + "properties": { + "fqdn": { + "type": "object" + } + }, + "required": [ + "fqdn" + ], + "additionalProperties": false + }, + { + "description": "Pre-standard route refresh as defined in RFC 8810 (deprecated). Note this capability is not yet supported.", + "type": "object", + "properties": { + "prestandard_route_refresh": { + "type": "object" + } + }, + "required": [ + "prestandard_route_refresh" + ], + "additionalProperties": false + }, + { + "description": "Pre-standard prefix-based outbound route filtering as defined in RFC 8810 (deprecated). Note this is not yet implemented.", + "type": "object", + "properties": { + "prestandard_orf_and_pd": { + "type": "object" + } + }, + "required": [ + "prestandard_orf_and_pd" + ], + "additionalProperties": false + }, + { + "description": "Pre-standard outbound route filtering as defined in RFC 8810 (deprecated). Note this is not yet implemented.", + "type": "object", + "properties": { + "prestandard_outbound_route_filtering": { + "type": "object" + } + }, + "required": [ + "prestandard_outbound_route_filtering" + ], + "additionalProperties": false + }, + { + "description": "Pre-standard multisession as defined in RFC 8810 (deprecated). Note this is not yet implemented.", + "type": "object", + "properties": { + "prestandard_multisession": { + "type": "object" + } + }, + "required": [ + "prestandard_multisession" + ], + "additionalProperties": false + }, + { + "description": "Pre-standard fully qualified domain names as defined in RFC 8810 (deprecated). Note this is not yet implemented.", + "type": "object", + "properties": { + "prestandard_fqdn": { + "type": "object" + } + }, + "required": [ + "prestandard_fqdn" + ], + "additionalProperties": false + }, + { + "description": "Pre-standard operational messages as defined in RFC 8810 (deprecated). Note this is not yet implemented.", + "type": "object", + "properties": { + "prestandard_operational_message": { + "type": "object" + } + }, + "required": [ + "prestandard_operational_message" + ], + "additionalProperties": false + }, + { + "description": "Experimental capability as defined in RFC 8810.", + "type": "object", + "properties": { + "experimental": { + "type": "object", + "properties": { + "code": { + "type": "integer", + "format": "uint8", + "minimum": 0 + } + }, + "required": [ + "code" + ] + } + }, + "required": [ + "experimental" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "unassigned": { + "type": "object", + "properties": { + "code": { + "type": "integer", + "format": "uint8", + "minimum": 0 + } + }, + "required": [ + "code" + ] + } + }, + "required": [ + "unassigned" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "reserved": { + "type": "object", + "properties": { + "code": { + "type": "integer", + "format": "uint8", + "minimum": 0 + } + }, + "required": [ + "code" + ] + } + }, + "required": [ + "reserved" + ], + "additionalProperties": false + } + ] + }, + "CeaseErrorSubcode": { + "description": "Cease error subcode types from RFC 4486", + "type": "string", + "enum": [ + "unspecific", + "maximum_numberof_prefixes_reached", + "administrative_shutdown", + "peer_deconfigured", + "administrative_reset", + "connection_rejected", + "other_configuration_change", + "connection_collision_resolution", + "out_of_resources" + ] + }, + "CheckerSource": { + "type": "object", + "properties": { + "asn": { + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "code": { + "type": "string" + } + }, + "required": [ + "asn", + "code" + ] + }, + "Community": { + "description": "BGP community value", + "oneOf": [ + { + "description": "All routes received carrying a communities attribute containing this value MUST NOT be advertised outside a BGP confederation boundary (a stand-alone autonomous system that is not part of a confederation should be considered a confederation itself)", + "type": "string", + "enum": [ + "no_export" + ] + }, + { + "description": "All routes received carrying a communities attribute containing this value MUST NOT be advertised to other BGP peers.", + "type": "string", + "enum": [ + "no_advertise" + ] + }, + { + "description": "All routes received carrying a communities attribute containing this value MUST NOT be advertised to external BGP peers (this includes peers in other members autonomous systems inside a BGP confederation).", + "type": "string", + "enum": [ + "no_export_sub_confed" + ] + }, + { + "description": "All routes received carrying a communities attribute containing this value must set the local preference for the received routes to a low value, preferably zero.", + "type": "string", + "enum": [ + "graceful_shutdown" + ] + }, + { + "description": "A user defined community", + "type": "object", + "properties": { + "user_defined": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + }, + "required": [ + "user_defined" + ], + "additionalProperties": false + } + ] + }, + "ConnectionId": { + "description": "Unique identifier for a BGP connection instance", + "type": "object", + "properties": { + "local": { + "description": "Local socket address for this connection", + "type": "string" + }, + "remote": { + "description": "Remote socket address for this connection", + "type": "string" + }, + "uuid": { + "description": "Unique identifier for this connection instance", + "type": "string", + "format": "uuid" + } + }, + "required": [ + "local", + "remote", + "uuid" + ] + }, + "DeleteStaticRoute4Request": { + "type": "object", + "properties": { + "routes": { + "$ref": "#/components/schemas/StaticRoute4List" + } + }, + "required": [ + "routes" + ] + }, + "DeleteStaticRoute6Request": { + "type": "object", + "properties": { + "routes": { + "$ref": "#/components/schemas/StaticRoute6List" + } + }, + "required": [ + "routes" + ] + }, + "Duration": { + "type": "object", + "properties": { + "nanos": { + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "secs": { + "type": "integer", + "format": "uint64", + "minimum": 0 + } + }, + "required": [ + "nanos", + "secs" + ] + }, + "DynamicTimerInfo": { + "type": "object", + "properties": { + "configured": { + "$ref": "#/components/schemas/Duration" + }, + "negotiated": { + "$ref": "#/components/schemas/Duration" + }, + "remaining": { + "$ref": "#/components/schemas/Duration" + } + }, + "required": [ + "configured", + "negotiated", + "remaining" + ] + }, + "Error": { + "description": "Error information from a response.", + "type": "object", + "properties": { + "error_code": { + "type": "string" + }, + "message": { + "type": "string" + }, + "request_id": { + "type": "string" + } + }, + "required": [ + "message", + "request_id" + ] + }, + "ErrorCode": { + "description": "This enumeration contains possible notification error codes.", + "type": "string", + "enum": [ + "header", + "open", + "update", + "hold_timer_expired", + "fsm", + "cease" + ] + }, + "ErrorSubcode": { + "description": "This enumeration contains possible notification error subcodes.", + "oneOf": [ + { + "type": "object", + "properties": { + "header": { + "$ref": "#/components/schemas/HeaderErrorSubcode" + } + }, + "required": [ + "header" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "open": { + "$ref": "#/components/schemas/OpenErrorSubcode" + } + }, + "required": [ + "open" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "update": { + "$ref": "#/components/schemas/UpdateErrorSubcode" + } + }, + "required": [ + "update" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "hold_time": { + "type": "integer", + "format": "uint8", + "minimum": 0 + } + }, + "required": [ + "hold_time" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "fsm": { + "type": "integer", + "format": "uint8", + "minimum": 0 + } + }, + "required": [ + "fsm" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "cease": { + "$ref": "#/components/schemas/CeaseErrorSubcode" + } + }, + "required": [ + "cease" + ], + "additionalProperties": false + } + ] + }, + "ExportedSelector": { + "type": "object", + "properties": { + "afi": { + "nullable": true, + "description": "Optional address family filter (None = all negotiated families)", + "allOf": [ + { + "$ref": "#/components/schemas/Afi" + } + ] + }, + "asn": { + "description": "ASN of the router to get exported prefixes from.", + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "peer": { + "nullable": true, + "description": "Optional peer filter using PeerId enum", + "allOf": [ + { + "$ref": "#/components/schemas/PeerId" + } + ] + } + }, + "required": [ + "asn" + ] + }, + "FsmEventBuffer": { + "oneOf": [ + { + "description": "All FSM events (high frequency, includes all timers)", + "type": "string", + "enum": [ + "all" + ] + }, + { + "description": "Major events only (state transitions, admin, new connections)", + "type": "string", + "enum": [ + "major" + ] + } + ] + }, + "FsmEventCategory": { + "description": "Category of FSM event for filtering and display purposes", + "type": "string", + "enum": [ + "Admin", + "Connection", + "Session", + "StateTransition" + ] + }, + "FsmEventRecord": { + "description": "Serializable record of an FSM event with full context", + "type": "object", + "properties": { + "connection_id": { + "nullable": true, + "description": "Connection ID if event is connection-specific", + "allOf": [ + { + "$ref": "#/components/schemas/ConnectionId" + } + ] + }, + "current_state": { + "description": "FSM state at time of event", + "allOf": [ + { + "$ref": "#/components/schemas/FsmStateKind" + } + ] + }, + "details": { + "nullable": true, + "description": "Additional event details (e.g., \"Received OPEN\", \"Admin command\")", + "type": "string" + }, + "event_category": { + "description": "High-level event category", + "allOf": [ + { + "$ref": "#/components/schemas/FsmEventCategory" + } + ] + }, + "event_type": { + "description": "Specific event type as string (e.g., \"ManualStart\", \"HoldTimerExpires\")", + "type": "string" + }, + "previous_state": { + "nullable": true, + "description": "Previous state if this caused a transition", + "allOf": [ + { + "$ref": "#/components/schemas/FsmStateKind" + } + ] + }, + "timestamp": { + "description": "UTC timestamp when event occurred", + "type": "string", + "format": "date-time" + } + }, + "required": [ + "current_state", + "event_category", + "event_type", + "timestamp" + ] + }, + "FsmHistoryRequest": { + "description": "Unified FSM history request supporting both numbered and unnumbered peers", + "type": "object", + "properties": { + "asn": { + "description": "ASN of the BGP router", + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "buffer": { + "nullable": true, + "description": "Which buffer to retrieve - if None, returns major buffer", + "allOf": [ + { + "$ref": "#/components/schemas/FsmEventBuffer" + } + ] + }, + "peer": { + "nullable": true, + "description": "Optional peer filter using PeerId enum JSON format: {\"ip\": \"192.0.2.1\"} or {\"interface\": \"eth0\"}", + "allOf": [ + { + "$ref": "#/components/schemas/PeerId" + } + ] + } + }, + "required": [ + "asn" + ] + }, + "FsmHistoryResponse": { + "description": "Unified FSM history response with string keys from PeerId Display Keys will be \"192.0.2.1\" or \"eth0\" format", + "type": "object", + "properties": { + "by_peer": { + "description": "Events organized by peer identifier Each peer's value contains only the events from the requested buffer", + "type": "object", + "additionalProperties": { + "type": "array", + "items": { + "$ref": "#/components/schemas/FsmEventRecord" + } + } + } + }, + "required": [ + "by_peer" + ] + }, + "FsmStateKind": { + "description": "Simplified representation of a BGP state without having to carry a connection.", + "oneOf": [ + { + "description": "Initial state. Refuse all incomming BGP connections. No resources allocated to peer.", + "type": "string", + "enum": [ + "Idle" + ] + }, + { + "description": "Waiting for the TCP connection to be completed.", + "type": "string", + "enum": [ + "Connect" + ] + }, + { + "description": "Trying to acquire peer by listening for and accepting a TCP connection.", + "type": "string", + "enum": [ + "Active" + ] + }, + { + "description": "Waiting for open message from peer.", + "type": "string", + "enum": [ + "OpenSent" + ] + }, + { + "description": "Waiting for keepalive or notification from peer.", + "type": "string", + "enum": [ + "OpenConfirm" + ] + }, + { + "description": "Handler for Connection Collisions (RFC 4271 6.8)", + "type": "string", + "enum": [ + "ConnectionCollision" + ] + }, + { + "description": "Sync up with peers.", + "type": "string", + "enum": [ + "SessionSetup" + ] + }, + { + "description": "Able to exchange update, notification and keepliave messages with peers.", + "type": "string", + "enum": [ + "Established" + ] + } + ] + }, + "HeaderErrorSubcode": { + "description": "Header error subcode types", + "type": "string", + "enum": [ + "unspecific", + "connection_not_synchronized", + "bad_message_length", + "bad_message_type" + ] + }, + "ImportExportPolicy4": { + "description": "Import/Export policy for IPv4 prefixes only.", + "oneOf": [ + { + "type": "string", + "enum": [ + "NoFiltering" + ] + }, + { + "type": "object", + "properties": { + "Allow": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Prefix4" + }, + "uniqueItems": true + } + }, + "required": [ + "Allow" + ], + "additionalProperties": false + } + ] + }, + "ImportExportPolicy6": { + "description": "Import/Export policy for IPv6 prefixes only.", + "oneOf": [ + { + "type": "string", + "enum": [ + "NoFiltering" + ] + }, + { + "type": "object", + "properties": { + "Allow": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Prefix6" + }, + "uniqueItems": true + } + }, + "required": [ + "Allow" + ], + "additionalProperties": false + } + ] + }, + "Ipv4UnicastConfig": { + "description": "Per-address-family configuration for IPv4 Unicast", + "type": "object", + "properties": { + "export_policy": { + "$ref": "#/components/schemas/ImportExportPolicy4" + }, + "import_policy": { + "$ref": "#/components/schemas/ImportExportPolicy4" + }, + "nexthop": { + "nullable": true, + "type": "string", + "format": "ip" + } + }, + "required": [ + "export_policy", + "import_policy" + ] + }, + "Ipv6DoubleNexthop": { + "description": "IPv6 double nexthop: global unicast address + link-local address. Per RFC 2545, when advertising IPv6 routes, both addresses may be present.", + "type": "object", + "properties": { + "global": { + "description": "Global unicast address", + "type": "string", + "format": "ipv6" + }, + "link_local": { + "description": "Link-local address", + "type": "string", + "format": "ipv6" + } + }, + "required": [ + "global", + "link_local" + ] + }, + "Ipv6UnicastConfig": { + "description": "Per-address-family configuration for IPv6 Unicast", + "type": "object", + "properties": { + "export_policy": { + "$ref": "#/components/schemas/ImportExportPolicy6" + }, + "import_policy": { + "$ref": "#/components/schemas/ImportExportPolicy6" + }, + "nexthop": { + "nullable": true, + "type": "string", + "format": "ip" + } + }, + "required": [ + "export_policy", + "import_policy" + ] + }, + "JitterRange": { + "description": "Jitter range with minimum and maximum multiplier values. When applied to a timer, the timer duration is multiplied by a random value within [min, max] to help break synchronization patterns.", + "type": "object", + "properties": { + "max": { + "description": "Maximum jitter multiplier (typically 1.0 or similar)", + "type": "number", + "format": "double" + }, + "min": { + "description": "Minimum jitter multiplier (typically 0.75 or similar)", + "type": "number", + "format": "double" + } + }, + "required": [ + "max", + "min" + ] + }, + "Message": { + "description": "Holds a BGP message. May be an Open, Update, Notification or Keep Alive message.", + "oneOf": [ + { + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": [ + "open" + ] + }, + "value": { + "$ref": "#/components/schemas/OpenMessage" + } + }, + "required": [ + "type", + "value" + ] + }, + { + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": [ + "update" + ] + }, + "value": { + "$ref": "#/components/schemas/UpdateMessage" + } + }, + "required": [ + "type", + "value" + ] + }, + { + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": [ + "notification" + ] + }, + "value": { + "$ref": "#/components/schemas/NotificationMessage" + } + }, + "required": [ + "type", + "value" + ] + }, + { + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": [ + "keep_alive" + ] + } + }, + "required": [ + "type" + ] + }, + { + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": [ + "route_refresh" + ] + }, + "value": { + "$ref": "#/components/schemas/RouteRefreshMessage" + } + }, + "required": [ + "type", + "value" + ] + } + ] + }, + "MessageDirection": { + "type": "string", + "enum": [ + "sent", + "received" + ] + }, + "MessageHistory": { + "description": "Message history for a BGP session", + "type": "object", + "properties": { + "received": { + "type": "array", + "items": { + "$ref": "#/components/schemas/MessageHistoryEntry" + } + }, + "sent": { + "type": "array", + "items": { + "$ref": "#/components/schemas/MessageHistoryEntry" + } + } + }, + "required": [ + "received", + "sent" + ] + }, + "MessageHistoryEntry": { + "description": "A message history entry is a BGP message with an associated timestamp and connection ID", + "type": "object", + "properties": { + "connection_id": { + "$ref": "#/components/schemas/ConnectionId" + }, + "message": { + "$ref": "#/components/schemas/Message" + }, + "timestamp": { + "type": "string", + "format": "date-time" + } + }, + "required": [ + "connection_id", + "message", + "timestamp" + ] + }, + "MessageHistoryRequest": { + "description": "Unified message history request supporting both numbered and unnumbered peers", + "type": "object", + "properties": { + "asn": { + "description": "ASN of the BGP router", + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "direction": { + "nullable": true, + "description": "Optional direction filter - if None, returns both sent and received", + "allOf": [ + { + "$ref": "#/components/schemas/MessageDirection" + } + ] + }, + "peer": { + "nullable": true, + "description": "Optional peer filter using PeerId enum JSON format: {\"ip\": \"192.0.2.1\"} or {\"interface\": \"eth0\"}", + "allOf": [ + { + "$ref": "#/components/schemas/PeerId" + } + ] + } + }, + "required": [ + "asn" + ] + }, + "MessageHistoryResponse": { + "description": "Unified message history response with string keys from PeerId Display Keys will be \"192.0.2.1\" or \"eth0\" format", + "type": "object", + "properties": { + "by_peer": { + "type": "object", + "additionalProperties": { + "$ref": "#/components/schemas/MessageHistory" + } + } + }, + "required": [ + "by_peer" + ] + }, + "MpReachNlri": { + "description": "MP_REACH_NLRI path attribute\n\nEach variant represents a specific AFI+SAFI combination, providing compile-time guarantees about the address family of routes being announced.\n\n```text 3. Multiprotocol Reachable NLRI - MP_REACH_NLRI (Type Code 14):\n\nThis is an optional non-transitive attribute that can be used for the following purposes:\n\n(a) to advertise a feasible route to a peer\n\n(b) to permit a router to advertise the Network Layer address of the router that should be used as the next hop to the destinations listed in the Network Layer Reachability Information field of the MP_NLRI attribute.\n\nThe attribute is encoded as shown below:\n\n+---------------------------------------------------------+ | Address Family Identifier (2 octets) | +---------------------------------------------------------+ ```", + "oneOf": [ + { + "description": "IPv4 Unicast routes (AFI=1, SAFI=1)", + "type": "object", + "properties": { + "afi_safi": { + "type": "string", + "enum": [ + "ipv4_unicast" + ] + }, + "nexthop": { + "description": "Next-hop for IPv4 routes.\n\nCurrently must be `BgpNexthop::Ipv4`, but will support IPv6 nexthops when extended next-hop capability (RFC 8950) is implemented.", + "allOf": [ + { + "$ref": "#/components/schemas/BgpNexthop" + } + ] + }, + "nlri": { + "description": "IPv4 prefixes being announced", + "type": "array", + "items": { + "$ref": "#/components/schemas/Prefix4" + } + }, + "reserved": { + "description": "Reserved byte from RFC 4760 §3 (historically \"Number of SNPAs\" in RFC 2858). MUST be 0 per RFC 4760, but MUST be ignored by receiver. Stored for validation logging in session layer. This field is positioned before NLRI to match the wire format encoding.", + "type": "integer", + "format": "uint8", + "minimum": 0 + } + }, + "required": [ + "afi_safi", + "nexthop", + "nlri", + "reserved" + ] + }, + { + "description": "IPv6 Unicast routes (AFI=2, SAFI=1)", + "type": "object", + "properties": { + "afi_safi": { + "type": "string", + "enum": [ + "ipv6_unicast" + ] + }, + "nexthop": { + "description": "Next-hop for IPv6 routes.\n\nCan be `BgpNexthop::Ipv6Single` (16 bytes) or `BgpNexthop::Ipv6Double` (32 bytes with link-local address).", + "allOf": [ + { + "$ref": "#/components/schemas/BgpNexthop" + } + ] + }, + "nlri": { + "description": "IPv6 prefixes being announced", + "type": "array", + "items": { + "$ref": "#/components/schemas/Prefix6" + } + }, + "reserved": { + "description": "Reserved byte from RFC 4760 §3 (historically \"Number of SNPAs\" in RFC 2858). MUST be 0 per RFC 4760, but MUST be ignored by receiver. Stored for validation logging in session layer. This field is positioned before NLRI to match the wire format encoding.", + "type": "integer", + "format": "uint8", + "minimum": 0 + } + }, + "required": [ + "afi_safi", + "nexthop", + "nlri", + "reserved" + ] + } + ] + }, + "MpUnreachNlri": { + "description": "MP_UNREACH_NLRI path attribute\n\nEach variant represents a specific AFI+SAFI combination, providing compile-time guarantees about the address family of routes being withdrawn.\n\n```text 4. Multiprotocol Unreachable NLRI - MP_UNREACH_NLRI (Type Code 15):\n\nThis is an optional non-transitive attribute that can be used for the purpose of withdrawing multiple unfeasible routes from service.\n\nThe attribute is encoded as shown below:\n\n+---------------------------------------------------------+ | Address Family Identifier (2 octets) | +---------------------------------------------------------+ | Subsequent Address Family Identifier (1 octet) | +---------------------------------------------------------+ | Withdrawn Routes (variable) | +---------------------------------------------------------+ ```", + "oneOf": [ + { + "description": "IPv4 Unicast routes being withdrawn (AFI=1, SAFI=1)", + "type": "object", + "properties": { + "afi_safi": { + "type": "string", + "enum": [ + "ipv4_unicast" + ] + }, + "withdrawn": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Prefix4" + } + } + }, + "required": [ + "afi_safi", + "withdrawn" + ] + }, + { + "description": "IPv6 Unicast routes being withdrawn (AFI=2, SAFI=1)", + "type": "object", + "properties": { + "afi_safi": { + "type": "string", + "enum": [ + "ipv6_unicast" + ] + }, + "withdrawn": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Prefix6" + } + } + }, + "required": [ + "afi_safi", + "withdrawn" + ] + } + ] + }, + "NdpInterface": { + "description": "NDP state for an interface", + "type": "object", + "properties": { + "discovered_peer": { + "nullable": true, + "description": "Information about discovered peer (if any, including expired)", + "allOf": [ + { + "$ref": "#/components/schemas/NdpPeer" + } + ] + }, + "interface": { + "description": "Interface name (e.g., \"qsfp0\")", + "type": "string" + }, + "local_address": { + "description": "Local IPv6 link-local address", + "type": "string", + "format": "ipv6" + }, + "router_lifetime": { + "description": "Router lifetime advertised by this router (seconds)", + "type": "integer", + "format": "uint16", + "minimum": 0 + }, + "scope_id": { + "description": "IPv6 scope ID (interface index)", + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "thread_state": { + "nullable": true, + "description": "Thread state for rx/tx loops (None if interface not active in NDP)", + "allOf": [ + { + "$ref": "#/components/schemas/NdpThreadState" + } + ] + } + }, + "required": [ + "interface", + "local_address", + "router_lifetime", + "scope_id" + ] + }, + "NdpManagerState": { + "description": "NDP manager state showing overall health and interface status", + "type": "object", + "properties": { + "active_interfaces": { + "description": "Interfaces currently active in NDP (available on system)", + "type": "array", + "items": { + "type": "string" + } + }, + "monitor_thread_running": { + "description": "Whether the interface monitor thread is running", + "type": "boolean" + }, + "pending_interfaces": { + "description": "Interfaces configured but not yet available on the system", + "type": "array", + "items": { + "$ref": "#/components/schemas/NdpPendingInterface" + } + } + }, + "required": [ + "active_interfaces", + "monitor_thread_running", + "pending_interfaces" + ] + }, + "NdpPeer": { + "description": "Information about a discovered NDP peer", + "type": "object", + "properties": { + "address": { + "description": "Peer IPv6 address", + "type": "string", + "format": "ipv6" + }, + "discovered_at": { + "description": "When the peer was first discovered (ISO 8601 timestamp)", + "type": "string" + }, + "expired": { + "description": "Whether the peer entry has expired", + "type": "boolean" + }, + "last_advertisement": { + "description": "When the most recent Router Advertisement was received (ISO 8601 timestamp)", + "type": "string" + }, + "reachable_time": { + "description": "Reachable time from RA (milliseconds)", + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "retrans_timer": { + "description": "Retransmit timer from RA (milliseconds)", + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "router_lifetime": { + "description": "Router lifetime from RA (seconds)", + "type": "integer", + "format": "uint16", + "minimum": 0 + }, + "time_until_expiry": { + "nullable": true, + "description": "Time until expiry (human-readable), or None if already expired", + "type": "string" + } + }, + "required": [ + "address", + "discovered_at", + "expired", + "last_advertisement", + "reachable_time", + "retrans_timer", + "router_lifetime" + ] + }, + "NdpPendingInterface": { + "description": "Information about a pending NDP interface", + "type": "object", + "properties": { + "interface": { + "description": "Interface name", + "type": "string" + }, + "router_lifetime": { + "description": "Configured router lifetime (seconds)", + "type": "integer", + "format": "uint16", + "minimum": 0 + } + }, + "required": [ + "interface", + "router_lifetime" + ] + }, + "NdpThreadState": { + "description": "Thread state for NDP rx/tx loops on an interface", + "type": "object", + "properties": { + "rx_running": { + "description": "Whether the RX loop thread is running", + "type": "boolean" + }, + "tx_running": { + "description": "Whether the TX loop thread is running", + "type": "boolean" + } + }, + "required": [ + "rx_running", + "tx_running" + ] + }, + "Neighbor": { + "description": "Neighbor configuration with explicit per-address-family enablement (v3 API)", + "type": "object", + "properties": { + "asn": { + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "communities": { + "type": "array", + "items": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + }, + "connect_retry": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "connect_retry_jitter": { + "nullable": true, + "description": "Jitter range for connect_retry timer. When used, the connect_retry timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", + "allOf": [ + { + "$ref": "#/components/schemas/JitterRange" + } + ] + }, + "delay_open": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "deterministic_collision_resolution": { + "description": "Enable deterministic collision resolution in Established state. When true, uses BGP-ID comparison per RFC 4271 §6.8 for collision resolution even when one connection is already in Established state. When false, Established connection always wins (timing-based resolution).", + "type": "boolean" + }, + "enforce_first_as": { + "type": "boolean" + }, + "group": { + "type": "string" + }, + "hold_time": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "host": { + "type": "string" + }, + "idle_hold_jitter": { + "nullable": true, + "description": "Jitter range for idle hold timer. When used, the idle hold timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", + "allOf": [ + { + "$ref": "#/components/schemas/JitterRange" + } + ] + }, + "idle_hold_time": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "ipv4_unicast": { + "nullable": true, + "description": "IPv4 Unicast address family configuration (None = disabled)", + "allOf": [ + { + "$ref": "#/components/schemas/Ipv4UnicastConfig" + } + ] + }, + "ipv6_unicast": { + "nullable": true, + "description": "IPv6 Unicast address family configuration (None = disabled)", + "allOf": [ + { + "$ref": "#/components/schemas/Ipv6UnicastConfig" + } + ] + }, + "keepalive": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "local_pref": { + "nullable": true, + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "md5_auth_key": { + "nullable": true, + "type": "string" + }, + "min_ttl": { + "nullable": true, + "type": "integer", + "format": "uint8", + "minimum": 0 + }, + "multi_exit_discriminator": { + "nullable": true, + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "name": { + "type": "string" + }, + "passive": { + "type": "boolean" + }, + "remote_asn": { + "nullable": true, + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "resolution": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "vlan_id": { + "nullable": true, + "type": "integer", + "format": "uint16", + "minimum": 0 + } + }, + "required": [ + "asn", + "communities", + "connect_retry", + "delay_open", + "deterministic_collision_resolution", + "enforce_first_as", + "group", + "hold_time", + "host", + "idle_hold_time", + "keepalive", + "name", + "passive", + "resolution" + ] + }, + "NeighborResetOp": { + "description": "V2 API neighbor reset operations with per-AF support", + "oneOf": [ + { + "description": "Hard reset - closes TCP connection and restarts session", + "type": "string", + "enum": [ + "Hard" + ] + }, + { + "description": "Soft inbound reset - sends route refresh for specified AF(s) None means all negotiated AFs", + "type": "object", + "properties": { + "SoftInbound": { + "nullable": true, + "allOf": [ + { + "$ref": "#/components/schemas/Afi" + } + ] + } + }, + "required": [ + "SoftInbound" + ], + "additionalProperties": false + }, + { + "description": "Soft outbound reset - re-advertises routes for specified AF(s) None means all negotiated AFs", + "type": "object", + "properties": { + "SoftOutbound": { + "nullable": true, + "allOf": [ + { + "$ref": "#/components/schemas/Afi" + } + ] + } + }, + "required": [ + "SoftOutbound" + ], + "additionalProperties": false + } + ] + }, + "NeighborResetRequest": { + "type": "object", + "properties": { + "addr": { + "type": "string", + "format": "ip" + }, + "asn": { + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "op": { + "$ref": "#/components/schemas/NeighborResetOp" + } + }, + "required": [ + "addr", + "asn", + "op" + ] + }, + "NotificationMessage": { + "description": "Notification messages are exchanged between BGP peers when an exceptional event has occurred.", + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "type": "integer", + "format": "uint8", + "minimum": 0 + } + }, + "error_code": { + "description": "Error code associated with the notification", + "allOf": [ + { + "$ref": "#/components/schemas/ErrorCode" + } + ] + }, + "error_subcode": { + "description": "Error subcode associated with the notification", + "allOf": [ + { + "$ref": "#/components/schemas/ErrorSubcode" + } + ] + } + }, + "required": [ + "data", + "error_code", + "error_subcode" + ] + }, + "OpenErrorSubcode": { + "description": "Open message error subcode types", + "type": "string", + "enum": [ + "unspecific", + "unsupported_version_number", + "bad_peer_a_s", + "bad_bgp_identifier", + "unsupported_optional_parameter", + "deprecated", + "unacceptable_hold_time", + "unsupported_capability" + ] + }, + "OpenMessage": { + "description": "The first message sent by each side once a TCP connection is established.\n\n```text 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Version | My Autonomous System | Hold Time : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : | BGP Identifier : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : | Opt Parm Len | Optional Parameters : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : : : Optional Parameters (cont, variable) : : | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ```\n\nRef: RFC 4271 §4.2", + "type": "object", + "properties": { + "asn": { + "description": "Autonomous system number of the sender. When 4-byte ASNs are in use this value is set to AS_TRANS which has a value of 23456.\n\nRef: RFC 4893 §7", + "type": "integer", + "format": "uint16", + "minimum": 0 + }, + "hold_time": { + "description": "Number of seconds the sender proposes for the hold timer.", + "type": "integer", + "format": "uint16", + "minimum": 0 + }, + "id": { + "description": "BGP identifier of the sender", + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "parameters": { + "description": "A list of optional parameters.", + "type": "array", + "items": { + "$ref": "#/components/schemas/OptionalParameter" + } + }, + "version": { + "description": "BGP protocol version.", + "type": "integer", + "format": "uint8", + "minimum": 0 + } + }, + "required": [ + "asn", + "hold_time", + "id", + "parameters", + "version" + ] + }, + "OptionalParameter": { + "description": "The IANA/IETF currently defines the following optional parameter types.", + "oneOf": [ + { + "description": "Code 0", + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": [ + "reserved" + ] + } + }, + "required": [ + "type" + ] + }, + { + "description": "Code 1: RFC 4217, RFC 5492 (deprecated)", + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": [ + "authentication" + ] + } + }, + "required": [ + "type" + ] + }, + { + "description": "Code 2: RFC 5492", + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": [ + "capabilities" + ] + }, + "value": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Capability" + }, + "uniqueItems": true + } + }, + "required": [ + "type", + "value" + ] + }, + { + "description": "Unassigned", + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": [ + "unassigned" + ] + } + }, + "required": [ + "type" + ] + }, + { + "description": "Code 255: RFC 9072", + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": [ + "extended_length" + ] + } + }, + "required": [ + "type" + ] + } + ] + }, + "Origin4": { + "type": "object", + "properties": { + "asn": { + "description": "ASN of the router to originate from.", + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "prefixes": { + "description": "Set of prefixes to originate.", + "type": "array", + "items": { + "$ref": "#/components/schemas/Prefix4" + } + } + }, + "required": [ + "asn", + "prefixes" + ] + }, + "Origin6": { + "type": "object", + "properties": { + "asn": { + "description": "ASN of the router to originate from.", + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "prefixes": { + "description": "Set of prefixes to originate.", + "type": "array", + "items": { + "$ref": "#/components/schemas/Prefix6" + } + } + }, + "required": [ + "asn", + "prefixes" + ] + }, + "Path": { + "type": "object", + "properties": { + "bgp": { + "nullable": true, + "allOf": [ + { + "$ref": "#/components/schemas/BgpPathProperties" + } + ] + }, + "nexthop": { + "type": "string", + "format": "ip" + }, + "nexthop_interface": { + "nullable": true, + "description": "Interface binding for nexthop resolution.\n\nThis field is only populated for BGP unnumbered sessions where the nexthop is a link-local IPv6 address. For numbered peers, this is always None.\n\nAdded in API version 5.0.0 (UNNUMBERED).", + "type": "string" + }, + "rib_priority": { + "type": "integer", + "format": "uint8", + "minimum": 0 + }, + "shutdown": { + "type": "boolean" + }, + "vlan_id": { + "nullable": true, + "type": "integer", + "format": "uint16", + "minimum": 0 + } + }, + "required": [ + "nexthop", + "rib_priority", + "shutdown" + ] + }, + "PathAttribute": { + "description": "A self-describing BGP path attribute", + "type": "object", + "properties": { + "typ": { + "description": "Type encoding for the attribute", + "allOf": [ + { + "$ref": "#/components/schemas/PathAttributeType" + } + ] + }, + "value": { + "description": "Value of the attribute", + "allOf": [ + { + "$ref": "#/components/schemas/PathAttributeValue" + } + ] + } + }, + "required": [ + "typ", + "value" + ] + }, + "PathAttributeType": { + "description": "Type encoding for a path attribute.", + "type": "object", + "properties": { + "flags": { + "description": "Flags may include, Optional, Transitive, Partial and Extended Length.", + "type": "integer", + "format": "uint8", + "minimum": 0 + }, + "type_code": { + "description": "Type code for the path attribute.", + "allOf": [ + { + "$ref": "#/components/schemas/PathAttributeTypeCode" + } + ] + } + }, + "required": [ + "flags", + "type_code" + ] + }, + "PathAttributeTypeCode": { + "description": "An enumeration describing available path attribute type codes.", + "oneOf": [ + { + "type": "string", + "enum": [ + "as_path", + "next_hop", + "multi_exit_disc", + "local_pref", + "atomic_aggregate", + "aggregator", + "communities", + "mp_unreach_nlri", + "as4_aggregator" + ] + }, + { + "description": "RFC 4271", + "type": "string", + "enum": [ + "origin" + ] + }, + { + "description": "RFC 4760", + "type": "string", + "enum": [ + "mp_reach_nlri" + ] + }, + { + "description": "RFC 6793", + "type": "string", + "enum": [ + "as4_path" + ] + } + ] + }, + "PathAttributeValue": { + "description": "The value encoding of a path attribute.", + "oneOf": [ + { + "description": "The type of origin associated with a path", + "type": "object", + "properties": { + "origin": { + "$ref": "#/components/schemas/PathOrigin" + } + }, + "required": [ + "origin" + ], + "additionalProperties": false + }, + { + "description": "The AS set associated with a path", + "type": "object", + "properties": { + "as_path": { + "type": "array", + "items": { + "$ref": "#/components/schemas/As4PathSegment" + } + } + }, + "required": [ + "as_path" + ], + "additionalProperties": false + }, + { + "description": "The nexthop associated with a path (IPv4 only for traditional BGP)", + "type": "object", + "properties": { + "next_hop": { + "type": "string", + "format": "ipv4" + } + }, + "required": [ + "next_hop" + ], + "additionalProperties": false + }, + { + "description": "A metric used for external (inter-AS) links to discriminate among multiple entry or exit points.", + "type": "object", + "properties": { + "multi_exit_disc": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + }, + "required": [ + "multi_exit_disc" + ], + "additionalProperties": false + }, + { + "description": "Local pref is included in update messages sent to internal peers and indicates a degree of preference.", + "type": "object", + "properties": { + "local_pref": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + }, + "required": [ + "local_pref" + ], + "additionalProperties": false + }, + { + "description": "AGGREGATOR: AS number and IP address of the last aggregating BGP speaker (2-octet ASN)", + "type": "object", + "properties": { + "aggregator": { + "$ref": "#/components/schemas/Aggregator" + } + }, + "required": [ + "aggregator" + ], + "additionalProperties": false + }, + { + "description": "Indicates communities associated with a path.", + "type": "object", + "properties": { + "communities": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Community" + } + } + }, + "required": [ + "communities" + ], + "additionalProperties": false + }, + { + "description": "Indicates this route was formed via aggregation (RFC 4271 §5.1.7)", + "type": "string", + "enum": [ + "atomic_aggregate" + ] + }, + { + "description": "The 4-byte encoded AS set associated with a path", + "type": "object", + "properties": { + "as4_path": { + "type": "array", + "items": { + "$ref": "#/components/schemas/As4PathSegment" + } + } + }, + "required": [ + "as4_path" + ], + "additionalProperties": false + }, + { + "description": "AS4_AGGREGATOR: AS number and IP address of the last aggregating BGP speaker (4-octet ASN)", + "type": "object", + "properties": { + "as4_aggregator": { + "$ref": "#/components/schemas/As4Aggregator" + } + }, + "required": [ + "as4_aggregator" + ], + "additionalProperties": false + }, + { + "description": "Carries reachable MP-BGP NLRI and Next-hop (advertisement).", + "type": "object", + "properties": { + "mp_reach_nlri": { + "$ref": "#/components/schemas/MpReachNlri" + } + }, + "required": [ + "mp_reach_nlri" + ], + "additionalProperties": false + }, + { + "description": "Carries unreachable MP-BGP NLRI (withdrawal).", + "type": "object", + "properties": { + "mp_unreach_nlri": { + "$ref": "#/components/schemas/MpUnreachNlri" + } + }, + "required": [ + "mp_unreach_nlri" + ], + "additionalProperties": false + } + ] + }, + "PathOrigin": { + "description": "An enumeration indicating the origin type of a path.", + "oneOf": [ + { + "description": "Interior gateway protocol", + "type": "string", + "enum": [ + "igp" + ] + }, + { + "description": "Exterior gateway protocol", + "type": "string", + "enum": [ + "egp" + ] + }, + { + "description": "Incomplete path origin", + "type": "string", + "enum": [ + "incomplete" + ] + } + ] + }, + "PeerCounters": { + "description": "Session-level counters that persist across connection changes These serve as aggregate counters across all connections for the session", + "type": "object", + "properties": { + "active_connections_accepted": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "active_connections_declined": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "connection_retries": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "connector_panics": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "hold_timer_expirations": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "idle_hold_timer_expirations": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "keepalive_send_failure": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "keepalives_received": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "keepalives_sent": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "md5_auth_failures": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "notification_send_failure": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "notifications_received": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "notifications_sent": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "open_handle_failures": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "open_send_failure": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "opens_received": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "opens_sent": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "passive_connections_accepted": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "passive_connections_declined": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "prefixes_advertised": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "prefixes_imported": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "route_refresh_received": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "route_refresh_send_failure": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "route_refresh_sent": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "tcp_connection_failure": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "transitions_to_active": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "transitions_to_connect": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "transitions_to_connection_collision": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "transitions_to_established": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "transitions_to_idle": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "transitions_to_open_confirm": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "transitions_to_open_sent": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "transitions_to_session_setup": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "unexpected_keepalive_message": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "unexpected_notification_message": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "unexpected_open_message": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "unexpected_route_refresh_message": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "unexpected_update_message": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "unnegotiated_address_family": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "update_nexhop_missing": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "update_send_failure": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "updates_received": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "updates_sent": { + "type": "integer", + "format": "uint64", + "minimum": 0 + } + }, + "required": [ + "active_connections_accepted", + "active_connections_declined", + "connection_retries", + "connector_panics", + "hold_timer_expirations", + "idle_hold_timer_expirations", + "keepalive_send_failure", + "keepalives_received", + "keepalives_sent", + "md5_auth_failures", + "notification_send_failure", + "notifications_received", + "notifications_sent", + "open_handle_failures", + "open_send_failure", + "opens_received", + "opens_sent", + "passive_connections_accepted", + "passive_connections_declined", + "prefixes_advertised", + "prefixes_imported", + "route_refresh_received", + "route_refresh_send_failure", + "route_refresh_sent", + "tcp_connection_failure", + "transitions_to_active", + "transitions_to_connect", + "transitions_to_connection_collision", + "transitions_to_established", + "transitions_to_idle", + "transitions_to_open_confirm", + "transitions_to_open_sent", + "transitions_to_session_setup", + "unexpected_keepalive_message", + "unexpected_notification_message", + "unexpected_open_message", + "unexpected_route_refresh_message", + "unexpected_update_message", + "unnegotiated_address_family", + "update_nexhop_missing", + "update_send_failure", + "updates_received", + "updates_sent" + ] + }, + "PeerId": { + "description": "Identifies a BGP peer for session management and route tracking.\n\nBGP peers can be identified in two ways: - **Numbered**: Traditional BGP peering using explicit IP addresses - **Unnumbered**: Modern peering using interface names with link-local addresses\n\n# Unnumbered Peering\n\nUnnumbered BGP uses interface names as stable identifiers instead of IP addresses. This is important because: - Link-local IPv6 addresses are discovered dynamically via NDP - Multiple interfaces may have peers with the same link-local address (e.g., fe80::1 on eth0 and fe80::1 on eth1) - Scope ID (interface index) disambiguates link-local addresses, but is not stable across reboots - Interface names provide stable, unambiguous peer identification\n\n# Route Tracking\n\nThis type is used in [`BgpPathProperties`](crate::BgpPathProperties) to track which peer advertised a route. Using `PeerId` instead of `IpAddr` ensures: - Unnumbered peers are properly distinguished even if they share link-local IPs - Route cleanup correctly removes only the routes from the intended peer - No cross-contamination when multiple unnumbered sessions exist\n\n# Examples\n\n``` use rdb_types::PeerId; use std::net::IpAddr;\n\n// Numbered peer let numbered = PeerId::Ip(\"192.0.2.1\".parse::().unwrap());\n\n// Unnumbered peer let unnumbered = PeerId::Interface(\"eth0\".to_string()); ```", + "oneOf": [ + { + "description": "Numbered peer identified by IP address\n\nUsed for traditional BGP sessions where peers are configured with explicit IP addresses (either IPv4 or IPv6 global unicast).", + "type": "object", + "properties": { + "Ip": { + "type": "string", + "format": "ip" + } + }, + "required": [ + "Ip" + ], + "additionalProperties": false + }, + { + "description": "Unnumbered peer identified by interface name\n\nUsed for unnumbered BGP sessions where peers are discovered via NDP on a specific interface. The interface name (e.g., \"eth0\") provides stable identification even though the peer's link-local address may be dynamic or shared with other interfaces.", + "type": "object", + "properties": { + "Interface": { + "type": "string" + } + }, + "required": [ + "Interface" + ], + "additionalProperties": false + } + ] + }, + "PeerInfo": { + "type": "object", + "properties": { + "asn": { + "nullable": true, + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "counters": { + "$ref": "#/components/schemas/PeerCounters" + }, + "fsm_state": { + "$ref": "#/components/schemas/FsmStateKind" + }, + "fsm_state_duration": { + "$ref": "#/components/schemas/Duration" + }, + "id": { + "nullable": true, + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "ipv4_unicast": { + "$ref": "#/components/schemas/Ipv4UnicastConfig" + }, + "ipv6_unicast": { + "$ref": "#/components/schemas/Ipv6UnicastConfig" + }, + "local_ip": { + "type": "string", + "format": "ip" + }, + "local_tcp_port": { + "type": "integer", + "format": "uint16", + "minimum": 0 + }, + "name": { + "type": "string" + }, + "peer_group": { + "type": "string" + }, + "received_capabilities": { + "type": "array", + "items": { + "$ref": "#/components/schemas/BgpCapability" + } + }, + "remote_ip": { + "type": "string", + "format": "ip" + }, + "remote_tcp_port": { + "type": "integer", + "format": "uint16", + "minimum": 0 + }, + "timers": { + "$ref": "#/components/schemas/PeerTimers" + } + }, + "required": [ + "counters", + "fsm_state", + "fsm_state_duration", + "ipv4_unicast", + "ipv6_unicast", + "local_ip", + "local_tcp_port", + "name", + "peer_group", + "received_capabilities", + "remote_ip", + "remote_tcp_port", + "timers" + ] + }, + "PeerTimers": { + "type": "object", + "properties": { + "connect_retry": { + "$ref": "#/components/schemas/StaticTimerInfo" + }, + "connect_retry_jitter": { + "nullable": true, + "allOf": [ + { + "$ref": "#/components/schemas/JitterRange" + } + ] + }, + "delay_open": { + "$ref": "#/components/schemas/StaticTimerInfo" + }, + "hold": { + "$ref": "#/components/schemas/DynamicTimerInfo" + }, + "idle_hold": { + "$ref": "#/components/schemas/StaticTimerInfo" + }, + "idle_hold_jitter": { + "nullable": true, + "allOf": [ + { + "$ref": "#/components/schemas/JitterRange" + } + ] + }, + "keepalive": { + "$ref": "#/components/schemas/DynamicTimerInfo" + } + }, + "required": [ + "connect_retry", + "delay_open", + "hold", + "idle_hold", + "keepalive" + ] + }, + "Prefix": { + "oneOf": [ + { + "type": "object", + "properties": { + "V4": { + "$ref": "#/components/schemas/Prefix4" + } + }, + "required": [ + "V4" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "V6": { + "$ref": "#/components/schemas/Prefix6" + } + }, + "required": [ + "V6" + ], + "additionalProperties": false + } + ] + }, + "Prefix4": { + "type": "object", + "properties": { + "length": { + "type": "integer", + "format": "uint8", + "minimum": 0 + }, + "value": { + "type": "string", + "format": "ipv4" + } + }, + "required": [ + "length", + "value" + ] + }, + "Prefix6": { + "type": "object", + "properties": { + "length": { + "type": "integer", + "format": "uint8", + "minimum": 0 + }, + "value": { + "type": "string", + "format": "ipv6" + } + }, + "required": [ + "length", + "value" + ] + }, + "Rib": { + "type": "object", + "additionalProperties": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Path" + }, + "uniqueItems": true + } + }, + "RouteRefreshMessage": { + "type": "object", + "properties": { + "afi": { + "description": "Address family identifier.", + "type": "integer", + "format": "uint16", + "minimum": 0 + }, + "safi": { + "description": "Subsequent address family identifier.", + "type": "integer", + "format": "uint8", + "minimum": 0 + } + }, + "required": [ + "afi", + "safi" + ] + }, + "Router": { + "type": "object", + "properties": { + "asn": { + "description": "Autonomous system number for this router", + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "graceful_shutdown": { + "description": "Gracefully shut this router down.", + "type": "boolean" + }, + "id": { + "description": "Id for this router", + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "listen": { + "description": "Listening address :", + "type": "string" + } + }, + "required": [ + "asn", + "graceful_shutdown", + "id", + "listen" + ] + }, + "SessionMode": { + "type": "string", + "enum": [ + "SingleHop", + "MultiHop" + ] + }, + "ShaperSource": { + "type": "object", + "properties": { + "asn": { + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "code": { + "type": "string" + } + }, + "required": [ + "asn", + "code" + ] + }, + "StaticRoute4": { + "type": "object", + "properties": { + "nexthop": { + "type": "string", + "format": "ip" + }, + "prefix": { + "$ref": "#/components/schemas/Prefix4" + }, + "rib_priority": { + "type": "integer", + "format": "uint8", + "minimum": 0 + }, + "vlan_id": { + "nullable": true, + "type": "integer", + "format": "uint16", + "minimum": 0 + } + }, + "required": [ + "nexthop", + "prefix", + "rib_priority" + ] + }, + "StaticRoute4List": { + "type": "object", + "properties": { + "list": { + "type": "array", + "items": { + "$ref": "#/components/schemas/StaticRoute4" + } + } + }, + "required": [ + "list" + ] + }, + "StaticRoute6": { + "type": "object", + "properties": { + "nexthop": { + "type": "string", + "format": "ip" + }, + "prefix": { + "$ref": "#/components/schemas/Prefix6" + }, + "rib_priority": { + "type": "integer", + "format": "uint8", + "minimum": 0 + }, + "vlan_id": { + "nullable": true, + "type": "integer", + "format": "uint16", + "minimum": 0 + } + }, + "required": [ + "nexthop", + "prefix", + "rib_priority" + ] + }, + "StaticRoute6List": { + "type": "object", + "properties": { + "list": { + "type": "array", + "items": { + "$ref": "#/components/schemas/StaticRoute6" + } + } + }, + "required": [ + "list" + ] + }, + "StaticTimerInfo": { + "description": "Timer information for static (non-negotiated) timers", + "type": "object", + "properties": { + "configured": { + "$ref": "#/components/schemas/Duration" + }, + "remaining": { + "$ref": "#/components/schemas/Duration" + } + }, + "required": [ + "configured", + "remaining" + ] + }, + "SwitchIdentifiers": { + "description": "Identifiers for a switch.", + "type": "object", + "properties": { + "slot": { + "nullable": true, + "description": "The slot number of the switch being managed.\n\nMGS uses u16 for this internally.", + "type": "integer", + "format": "uint16", + "minimum": 0 + } + } + }, + "UnnumberedBgpPeerConfig": { + "type": "object", + "properties": { + "communities": { + "type": "array", + "items": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + }, + "connect_retry": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "connect_retry_jitter": { + "nullable": true, + "description": "Jitter range for connect_retry timer. When used, the connect_retry timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", + "allOf": [ + { + "$ref": "#/components/schemas/JitterRange" + } + ] + }, + "delay_open": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "deterministic_collision_resolution": { + "description": "Enable deterministic collision resolution in Established state. When true, uses BGP-ID comparison per RFC 4271 §6.8 for collision resolution even when one connection is already in Established state. When false, Established connection always wins (timing-based resolution).", + "type": "boolean" + }, + "enforce_first_as": { + "type": "boolean" + }, + "hold_time": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "idle_hold_jitter": { + "nullable": true, + "description": "Jitter range for idle hold timer. When used, the idle hold timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", + "allOf": [ + { + "$ref": "#/components/schemas/JitterRange" + } + ] + }, + "idle_hold_time": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "interface": { + "type": "string" + }, + "ipv4_unicast": { + "nullable": true, + "description": "IPv4 Unicast address family configuration (None = disabled)", + "allOf": [ + { + "$ref": "#/components/schemas/Ipv4UnicastConfig" + } + ] + }, + "ipv6_unicast": { + "nullable": true, + "description": "IPv6 Unicast address family configuration (None = disabled)", + "allOf": [ + { + "$ref": "#/components/schemas/Ipv6UnicastConfig" + } + ] + }, + "keepalive": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "local_pref": { + "nullable": true, + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "md5_auth_key": { + "nullable": true, + "type": "string" + }, + "min_ttl": { + "nullable": true, + "type": "integer", + "format": "uint8", + "minimum": 0 + }, + "multi_exit_discriminator": { + "nullable": true, + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "name": { + "type": "string" + }, + "passive": { + "type": "boolean" + }, + "remote_asn": { + "nullable": true, + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "resolution": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "router_lifetime": { + "type": "integer", + "format": "uint16", + "minimum": 0 + }, + "vlan_id": { + "nullable": true, + "type": "integer", + "format": "uint16", + "minimum": 0 + } + }, + "required": [ + "communities", + "connect_retry", + "delay_open", + "deterministic_collision_resolution", + "enforce_first_as", + "hold_time", + "idle_hold_time", + "interface", + "keepalive", + "name", + "passive", + "resolution", + "router_lifetime" + ] + }, + "UnnumberedNeighbor": { + "type": "object", + "properties": { + "act_as_a_default_ipv6_router": { + "type": "integer", + "format": "uint16", + "minimum": 0 + }, + "asn": { + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "communities": { + "type": "array", + "items": { + "type": "integer", + "format": "uint32", + "minimum": 0 + } + }, + "connect_retry": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "connect_retry_jitter": { + "nullable": true, + "description": "Jitter range for connect_retry timer. When used, the connect_retry timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", + "allOf": [ + { + "$ref": "#/components/schemas/JitterRange" + } + ] + }, + "delay_open": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "deterministic_collision_resolution": { + "description": "Enable deterministic collision resolution in Established state. When true, uses BGP-ID comparison per RFC 4271 §6.8 for collision resolution even when one connection is already in Established state. When false, Established connection always wins (timing-based resolution).", + "type": "boolean" + }, + "enforce_first_as": { + "type": "boolean" + }, + "group": { + "type": "string" + }, + "hold_time": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "idle_hold_jitter": { + "nullable": true, + "description": "Jitter range for idle hold timer. When used, the idle hold timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", + "allOf": [ + { + "$ref": "#/components/schemas/JitterRange" + } + ] + }, + "idle_hold_time": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "interface": { + "type": "string" + }, + "ipv4_unicast": { + "nullable": true, + "description": "IPv4 Unicast address family configuration (None = disabled)", + "allOf": [ + { + "$ref": "#/components/schemas/Ipv4UnicastConfig" + } + ] + }, + "ipv6_unicast": { + "nullable": true, + "description": "IPv6 Unicast address family configuration (None = disabled)", + "allOf": [ + { + "$ref": "#/components/schemas/Ipv6UnicastConfig" + } + ] + }, + "keepalive": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "local_pref": { + "nullable": true, + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "md5_auth_key": { + "nullable": true, + "type": "string" + }, + "min_ttl": { + "nullable": true, + "type": "integer", + "format": "uint8", + "minimum": 0 + }, + "multi_exit_discriminator": { + "nullable": true, + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "name": { + "type": "string" + }, + "passive": { + "type": "boolean" + }, + "remote_asn": { + "nullable": true, + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "resolution": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "vlan_id": { + "nullable": true, + "type": "integer", + "format": "uint16", + "minimum": 0 + } + }, + "required": [ + "act_as_a_default_ipv6_router", + "asn", + "communities", + "connect_retry", + "delay_open", + "deterministic_collision_resolution", + "enforce_first_as", + "group", + "hold_time", + "idle_hold_time", + "interface", + "keepalive", + "name", + "passive", + "resolution" + ] + }, + "UnnumberedNeighborResetRequest": { + "type": "object", + "properties": { + "asn": { + "type": "integer", + "format": "uint32", + "minimum": 0 + }, + "interface": { + "type": "string" + }, + "op": { + "$ref": "#/components/schemas/NeighborResetOp" + } + }, + "required": [ + "asn", + "interface", + "op" + ] + }, + "UpdateErrorSubcode": { + "description": "Update message error subcode types", + "type": "string", + "enum": [ + "unspecific", + "malformed_attribute_list", + "unrecognized_well_known_attribute", + "missing_well_known_attribute", + "attribute_flags", + "attribute_length", + "invalid_origin_attribute", + "deprecated", + "invalid_nexthop_attribute", + "optional_attribute", + "invalid_network_field", + "malformed_as_path" + ] + }, + "UpdateMessage": { + "description": "An update message is used to advertise feasible routes that share common path attributes to a peer, or to withdraw multiple unfeasible routes from service.\n\n```text 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Witdrawn Length | Withdrawn Routes : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : : : Withdrawn Routes (cont, variable) : : | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Path Attribute Length | Path Attributes : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : : : Path Attributes (cont, variable) : : | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : : : Network Layer Reachability Information (variable) : : | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ```\n\nRef: RFC 4271 §4.3", + "type": "object", + "properties": { + "nlri": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Prefix4" + } + }, + "path_attributes": { + "type": "array", + "items": { + "$ref": "#/components/schemas/PathAttribute" + } + }, + "withdrawn": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Prefix4" + } + } + }, + "required": [ + "nlri", + "path_attributes", + "withdrawn" + ] + }, + "AddressFamily": { + "description": "Represents the address family (protocol version) for network routes.\n\nThis is the canonical source of truth for address family definitions across the entire codebase. All routing-related components (RIB operations, BGP messages, API filtering, CLI tools) use this single enum rather than defining their own.\n\n# Semantics\n\nWhen used in filtering contexts (e.g., database queries or API parameters), `Option` is preferred: - `None` = no filter (match all address families) - `Some(Ipv4)` = IPv4 routes only - `Some(Ipv6)` = IPv6 routes only\n\n# Examples\n\n``` use rdb_types::AddressFamily;\n\nlet ipv4 = AddressFamily::Ipv4; let ipv6 = AddressFamily::Ipv6;\n\n// For filtering, use Option let filter: Option = Some(AddressFamily::Ipv4); let no_filter: Option = None; // matches all families ```", + "oneOf": [ + { + "description": "Internet Protocol Version 4 (IPv4)", + "type": "string", + "enum": [ + "Ipv4" + ] + }, + { + "description": "Internet Protocol Version 6 (IPv6)", + "type": "string", + "enum": [ + "Ipv6" + ] + } + ] + }, + "ProtocolFilter": { + "oneOf": [ + { + "description": "BGP routes only", + "type": "string", + "enum": [ + "Bgp" + ] + }, + { + "description": "Static routes only", + "type": "string", + "enum": [ + "Static" + ] + } + ] + } + }, + "responses": { + "Error": { + "description": "Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + } + } +} diff --git a/openapi/mg-admin/mg-admin-latest.json b/openapi/mg-admin/mg-admin-latest.json index 4f9e13f22..cdfd85214 120000 --- a/openapi/mg-admin/mg-admin-latest.json +++ b/openapi/mg-admin/mg-admin-latest.json @@ -1 +1 @@ -mg-admin-6.0.0-324339.json \ No newline at end of file +mg-admin-7.0.0-f92782.json \ No newline at end of file diff --git a/rdb/src/db.rs b/rdb/src/db.rs index dc3dde1b5..547d27bbe 100644 --- a/rdb/src/db.rs +++ b/rdb/src/db.rs @@ -2190,6 +2190,221 @@ mod test { assert_eq!(final_result.len(), 2); } + #[test] + fn test_static_routing_v4_prefix_v6_nexthop() { + let db = get_test_db(); + let nexthop = IpAddr::V6(Ipv6Addr::from_str("fe80::1").unwrap()); + + let prefix4 = + Prefix4::new(Ipv4Addr::from_str("10.10.0.0").unwrap(), 16); + let static_route = StaticRouteKey { + prefix: Prefix::V4(prefix4), + nexthop, + vlan_id: None, + rib_priority: DEFAULT_RIB_PRIORITY_STATIC, + }; + + // Add the route + db.add_static_routes(&[static_route]).unwrap(); + + // Verify route was added to persistent store + let routes = db.get_static(Some(AddressFamily::Ipv4)).unwrap(); + assert_eq!(routes.len(), 1); + assert_eq!(routes[0], static_route); + + // Check that it appears in RIB + let rib_routes = db.full_rib(Some(AddressFamily::Ipv4)); + assert_eq!(rib_routes.len(), 1); + assert!(rib_routes.contains_key(&Prefix::V4(prefix4))); + + // Verify the path has the IPv6 nexthop + let paths = db.get_prefix_paths(&Prefix::V4(prefix4)); + assert_eq!(paths.len(), 1); + assert_eq!(paths[0].nexthop, nexthop); + + // Remove the route + db.remove_static_routes(&[static_route]).unwrap(); + + // Verify route was removed + let routes = db.get_static(Some(AddressFamily::Ipv4)).unwrap(); + assert!(routes.is_empty()); + + let rib_routes = db.full_rib(Some(AddressFamily::Ipv4)); + assert!(rib_routes.is_empty()); + } + + #[test] + fn test_static_routing_v6_prefix_v4_nexthop() { + let db = get_test_db(); + let nexthop = IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()); + + let prefix6 = + Prefix6::new(Ipv6Addr::from_str("2001:db8:1::").unwrap(), 48); + let static_route = StaticRouteKey { + prefix: Prefix::V6(prefix6), + nexthop, + vlan_id: Some(100), + rib_priority: DEFAULT_RIB_PRIORITY_STATIC, + }; + + // Add the route + db.add_static_routes(&[static_route]).unwrap(); + + // Verify route was added to persistent store + let routes = db.get_static(Some(AddressFamily::Ipv6)).unwrap(); + assert_eq!(routes.len(), 1); + assert_eq!(routes[0], static_route); + + // Check that it appears in RIB + let rib_routes = db.full_rib(Some(AddressFamily::Ipv6)); + assert_eq!(rib_routes.len(), 1); + assert!(rib_routes.contains_key(&Prefix::V6(prefix6))); + + // Verify the path has the IPv4 nexthop + let paths = db.get_prefix_paths(&Prefix::V6(prefix6)); + assert_eq!(paths.len(), 1); + assert_eq!(paths[0].nexthop, nexthop); + + // Remove the route + db.remove_static_routes(&[static_route]).unwrap(); + + // Verify route was removed + let routes = db.get_static(Some(AddressFamily::Ipv6)).unwrap(); + assert!(routes.is_empty()); + + let rib_routes = db.full_rib(Some(AddressFamily::Ipv6)); + assert!(rib_routes.is_empty()); + } + + /// Multiple IPv6 static routes to the same prefix via different IPv4 + /// next-hops. The RIB should hold both paths and bestpath should + /// select the one with the lower rib_priority. + #[test] + fn test_static_routing_v6_prefix_multiple_v4_nexthops() { + let db = get_test_db(); + let prefix6 = + Prefix6::new(Ipv6Addr::from_str("2001:db8:2::").unwrap(), 48); + + let route_a = StaticRouteKey { + prefix: Prefix::V6(prefix6), + nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()), + vlan_id: None, + rib_priority: DEFAULT_RIB_PRIORITY_STATIC, + }; + let route_b = StaticRouteKey { + prefix: Prefix::V6(prefix6), + nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.2").unwrap()), + vlan_id: None, + rib_priority: DEFAULT_RIB_PRIORITY_STATIC + 10, + }; + + db.add_static_routes(&[route_a, route_b]).unwrap(); + + // Both paths should be in rib-in + let paths = db.get_prefix_paths(&Prefix::V6(prefix6)); + assert_eq!(paths.len(), 2); + + // loc-rib should pick route_a (lower rib_priority wins) + let selected = db.get_selected_prefix_paths(&Prefix::V6(prefix6)); + assert_eq!(selected.len(), 1); + assert_eq!(selected[0].nexthop, route_a.nexthop); + + // Remove route_a; route_b should become selected + db.remove_static_routes(&[route_a]).unwrap(); + let selected = db.get_selected_prefix_paths(&Prefix::V6(prefix6)); + assert_eq!(selected.len(), 1); + assert_eq!(selected[0].nexthop, route_b.nexthop); + + db.remove_static_routes(&[route_b]).unwrap(); + assert!(db.full_rib(Some(AddressFamily::Ipv6)).is_empty()); + } + + /// An IPv6 prefix can have paths via both a same-AF (v6) nexthop and + /// a cross-AF (v4) nexthop. Both should coexist in rib-in and + /// bestpath should choose based on rib_priority. + #[test] + fn test_static_routing_v6_prefix_mixed_nexthop_af() { + let db = get_test_db(); + let prefix6 = + Prefix6::new(Ipv6Addr::from_str("2001:db8:3::").unwrap(), 48); + + let route_v6nh = StaticRouteKey { + prefix: Prefix::V6(prefix6), + nexthop: IpAddr::V6(Ipv6Addr::from_str("fe80::1").unwrap()), + vlan_id: None, + rib_priority: DEFAULT_RIB_PRIORITY_STATIC + 10, + }; + let route_v4nh = StaticRouteKey { + prefix: Prefix::V6(prefix6), + nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()), + vlan_id: None, + rib_priority: DEFAULT_RIB_PRIORITY_STATIC, + }; + + db.add_static_routes(&[route_v6nh, route_v4nh]).unwrap(); + + // Both in rib-in + let paths = db.get_prefix_paths(&Prefix::V6(prefix6)); + assert_eq!(paths.len(), 2); + + // loc-rib selects the v4-nexthop route (lower rib_priority) + let selected = db.get_selected_prefix_paths(&Prefix::V6(prefix6)); + assert_eq!(selected.len(), 1); + assert_eq!(selected[0].nexthop, route_v4nh.nexthop); + + // Remove v4-nexthop route; v6-nexthop becomes selected + db.remove_static_routes(&[route_v4nh]).unwrap(); + let selected = db.get_selected_prefix_paths(&Prefix::V6(prefix6)); + assert_eq!(selected.len(), 1); + assert_eq!(selected[0].nexthop, route_v6nh.nexthop); + + db.remove_static_routes(&[route_v6nh]).unwrap(); + assert!(db.full_rib(Some(AddressFamily::Ipv6)).is_empty()); + } + + /// Cross-AF static routes with VLAN IDs: an IPv6 prefix with an IPv4 + /// nexthop should correctly persist and retrieve the vlan_id. + #[test] + fn test_static_routing_v6_prefix_v4_nexthop_with_vlan() { + let db = get_test_db(); + let prefix6 = + Prefix6::new(Ipv6Addr::from_str("2001:db8:4::").unwrap(), 64); + + let route_no_vlan = StaticRouteKey { + prefix: Prefix::V6(prefix6), + nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()), + vlan_id: None, + rib_priority: DEFAULT_RIB_PRIORITY_STATIC, + }; + let route_with_vlan = StaticRouteKey { + prefix: Prefix::V6(prefix6), + nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.2").unwrap()), + vlan_id: Some(4094), + rib_priority: DEFAULT_RIB_PRIORITY_STATIC, + }; + + db.add_static_routes(&[route_no_vlan, route_with_vlan]) + .unwrap(); + + let routes = db.get_static(Some(AddressFamily::Ipv6)).unwrap(); + assert_eq!(routes.len(), 2); + + let no_vlan = routes.iter().find(|r| r.vlan_id.is_none()).unwrap(); + assert_eq!(no_vlan.nexthop, route_no_vlan.nexthop); + + let with_vlan = routes.iter().find(|r| r.vlan_id.is_some()).unwrap(); + assert_eq!(with_vlan.vlan_id, Some(4094)); + assert_eq!(with_vlan.nexthop, route_with_vlan.nexthop); + + // Verify both appear in RIB paths + let paths = db.get_prefix_paths(&Prefix::V6(prefix6)); + assert_eq!(paths.len(), 2); + + db.remove_static_routes(&[route_no_vlan, route_with_vlan]) + .unwrap(); + assert!(db.get_static(Some(AddressFamily::Ipv6)).unwrap().is_empty()); + } + #[test] fn test_prefix4_db_key_serialization() { let prefix = Prefix4::new(Ipv4Addr::new(192, 168, 100, 0), 24); From ccc3196d719a4064f150ff0946c6918a949fb2b9 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 11 Feb 2026 23:04:52 -0700 Subject: [PATCH 06/58] mgadm, ddmadm: handle broken pipe gracefully When stdout is piped into a program that closes early (e.g. `mgadm bgp status neighbors 47 | head`), Rust's println! macro panics on the resulting EPIPE, causing a SIGABRT. Add println_nopipe!/print_nopipe! macros to mg-common that silently exit on BrokenPipe instead of panicking. Use them throughout mgadm's non-test print calls. Also catch EPIPE errors propagated via ? from TabWriter writeln/flush paths at both mgadm and ddmadm main() boundaries. Fixes #567 --- ddmadm/src/main.rs | 10 +- mg-common/src/lib.rs | 58 +++++++++ mgadm/src/bgp.rs | 236 +++++++++++++++++++++--------------- mgadm/src/main.rs | 10 +- mgadm/src/ndp.rs | 86 ++++++------- mgadm/src/rib.rs | 8 +- mgadm/src/static_routing.rs | 5 +- 7 files changed, 265 insertions(+), 148 deletions(-) diff --git a/ddmadm/src/main.rs b/ddmadm/src/main.rs index 800315d86..8c5c89bd7 100644 --- a/ddmadm/src/main.rs +++ b/ddmadm/src/main.rs @@ -90,7 +90,15 @@ struct Peer { } fn main() -> Result<()> { - oxide_tokio_rt::run(run()) + match oxide_tokio_rt::run(run()) { + Ok(()) => Ok(()), + Err(e) => { + if mg_common::is_broken_pipe(&e) { + std::process::exit(0); + } + Err(e) + } + } } async fn run() -> Result<()> { diff --git a/mg-common/src/lib.rs b/mg-common/src/lib.rs index 6d8be10f7..f1a7c4a26 100644 --- a/mg-common/src/lib.rs +++ b/mg-common/src/lib.rs @@ -39,6 +39,64 @@ pub fn format_duration_human(d: Duration) -> String { } } +/// Like `println!`, but silently exits on broken pipe (EPIPE) instead of +/// panicking. Other I/O errors still panic. +#[macro_export] +macro_rules! println_nopipe { + () => { + { + use std::io::Write; + let r = writeln!(std::io::stdout()); + match r { + Ok(_) => {}, + Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => { + std::process::exit(0); + }, + Err(e) => panic!("failed printing to stdout: {e}"), + } + } + }; + ($($arg:tt)*) => { + { + use std::io::Write; + let r = writeln!(std::io::stdout(), $($arg)*); + match r { + Ok(_) => {}, + Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => { + std::process::exit(0); + }, + Err(e) => panic!("failed printing to stdout: {e}"), + } + } + }; +} + +/// Like `print!`, but silently exits on broken pipe (EPIPE) instead of +/// panicking. Other I/O errors still panic. +#[macro_export] +macro_rules! print_nopipe { + ($($arg:tt)*) => { + { + use std::io::Write; + let r = write!(std::io::stdout(), $($arg)*); + match r { + Ok(_) => {}, + Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => { + std::process::exit(0); + }, + Err(e) => panic!("failed printing to stdout: {e}"), + } + } + }; +} + +/// Returns `true` if the root cause of `err` is a broken pipe (EPIPE). +pub fn is_broken_pipe(err: &anyhow::Error) -> bool { + err.root_cause() + .downcast_ref::() + .is_some_and(|e| e.kind() == std::io::ErrorKind::BrokenPipe) +} + #[macro_export] macro_rules! lock { ($mtx:expr) => { diff --git a/mgadm/src/bgp.rs b/mgadm/src/bgp.rs index ca2df495d..a48aea484 100644 --- a/mgadm/src/bgp.rs +++ b/mgadm/src/bgp.rs @@ -13,6 +13,7 @@ use mg_admin_client::{ Ipv6UnicastConfig, NeighborResetRequest, }, }; +use mg_common::{print_nopipe, println_nopipe}; use rdb::types::{PeerId, Prefix4, Prefix6}; use std::{ fs::read_to_string, @@ -982,7 +983,7 @@ pub async fn commands(command: Commands, c: Client) -> Result<()> { async fn read_routers(c: Client) -> Result<()> { let routers = c.read_routers().await?.into_inner(); - println!("{routers:#?}"); + println_nopipe!("{routers:#?}"); Ok(()) } @@ -1010,7 +1011,7 @@ async fn update_router(cfg: RouterConfig, c: Client) -> Result<()> { async fn read_router(asn: u32, c: Client) -> Result<()> { let response = c.read_router(asn).await?; - println!("{response:#?}"); + println_nopipe!("{response:#?}"); Ok(()) } @@ -1066,8 +1067,7 @@ fn display_neighbors_summary( "State Duration".dimmed(), "Hold".dimmed(), "Keepalive".dimmed(), - ) - .unwrap(); + )?; for (addr, info) in neighbors.iter() { writeln!( @@ -1081,10 +1081,9 @@ fn display_neighbors_summary( format_duration_human(info.timers.hold.negotiated), format_duration_human(info.timers.keepalive.configured), format_duration_human(info.timers.keepalive.negotiated), - ) - .unwrap(); + )?; } - tw.flush().unwrap(); + tw.flush()?; Ok(()) } @@ -1093,174 +1092,211 @@ fn display_neighbors_detail( ) -> Result<()> { for (i, (addr, info)) in neighbors.iter().enumerate() { if i > 0 { - println!(); + println_nopipe!(); } - println!("{}", "=".repeat(80)); - println!("{}", format!("Neighbor: {}", addr).bold()); - println!("{}", "=".repeat(80)); + println_nopipe!("{}", "=".repeat(80)); + println_nopipe!("{}", format!("Neighbor: {}", addr).bold()); + println_nopipe!("{}", "=".repeat(80)); - println!("\n{}", "Basic Information:".bold()); - println!(" Name: {}", info.name); - println!(" Peer Group: {}", info.peer_group); - println!(" FSM State: {:?}", info.fsm_state); - println!( + println_nopipe!("\n{}", "Basic Information:".bold()); + println_nopipe!(" Name: {}", info.name); + println_nopipe!(" Peer Group: {}", info.peer_group); + println_nopipe!(" FSM State: {:?}", info.fsm_state); + println_nopipe!( " FSM State Duration: {}", format_duration_human(info.fsm_state_duration) ); if let Some(asn) = info.asn { - println!(" Peer ASN: {}", asn); + println_nopipe!(" Peer ASN: {}", asn); } if let Some(id) = info.id { - println!(" Peer Router ID: {}", Ipv4Addr::from(id)); + println_nopipe!(" Peer Router ID: {}", Ipv4Addr::from(id)); } - println!("\n{}", "Connection:".bold()); - println!(" Local: {}:{}", info.local_ip, info.local_tcp_port); - println!(" Remote: {}:{}", info.remote_ip, info.remote_tcp_port); + println_nopipe!("\n{}", "Connection:".bold()); + println_nopipe!(" Local: {}:{}", info.local_ip, info.local_tcp_port); + println_nopipe!( + " Remote: {}:{}", + info.remote_ip, + info.remote_tcp_port + ); - println!("\n{}", "Address Families:".bold()); - println!(" IPv4 Unicast:"); - println!(" Import Policy: {:?}", info.ipv4_unicast.import_policy); - println!(" Export Policy: {:?}", info.ipv4_unicast.export_policy); + println_nopipe!("\n{}", "Address Families:".bold()); + println_nopipe!(" IPv4 Unicast:"); + println_nopipe!( + " Import Policy: {:?}", + info.ipv4_unicast.import_policy + ); + println_nopipe!( + " Export Policy: {:?}", + info.ipv4_unicast.export_policy + ); if let Some(nh) = info.ipv4_unicast.nexthop { - println!(" Nexthop: {}", nh); + println_nopipe!(" Nexthop: {}", nh); } - println!(" IPv6 Unicast:"); - println!(" Import Policy: {:?}", info.ipv6_unicast.import_policy); - println!(" Export Policy: {:?}", info.ipv6_unicast.export_policy); + println_nopipe!(" IPv6 Unicast:"); + println_nopipe!( + " Import Policy: {:?}", + info.ipv6_unicast.import_policy + ); + println_nopipe!( + " Export Policy: {:?}", + info.ipv6_unicast.export_policy + ); if let Some(nh) = info.ipv6_unicast.nexthop { - println!(" Nexthop: {}", nh); + println_nopipe!(" Nexthop: {}", nh); } - println!("\n{}", "Timers:".bold()); - println!( + println_nopipe!("\n{}", "Timers:".bold()); + println_nopipe!( " Hold Time: configured={}, negotiated={}, remaining={}", format_duration_human(info.timers.hold.configured), format_duration_human(info.timers.hold.negotiated), format_duration_human(info.timers.hold.remaining), ); - println!( + println_nopipe!( " Keepalive: configured={}, negotiated={}, remaining={}", format_duration_human(info.timers.keepalive.configured), format_duration_human(info.timers.keepalive.negotiated), format_duration_human(info.timers.keepalive.remaining), ); - println!( + println_nopipe!( " Connect Retry: configured={}, remaining={}", format_duration_human(info.timers.connect_retry.configured), format_duration_human(info.timers.connect_retry.remaining), ); match &info.timers.connect_retry_jitter { Some(jitter) => { - println!(" Jitter: {}-{}", jitter.min, jitter.max) + println_nopipe!(" Jitter: {}-{}", jitter.min, jitter.max) } - None => println!(" Jitter: none"), + None => println_nopipe!(" Jitter: none"), } - println!( + println_nopipe!( " Idle Hold: configured={}, remaining={}", format_duration_human(info.timers.idle_hold.configured), format_duration_human(info.timers.idle_hold.remaining), ); match &info.timers.idle_hold_jitter { Some(jitter) => { - println!(" Jitter: {}-{}", jitter.min, jitter.max) + println_nopipe!(" Jitter: {}-{}", jitter.min, jitter.max) } - None => println!(" Jitter: none"), + None => println_nopipe!(" Jitter: none"), } - println!( + println_nopipe!( " Delay Open: configured={}, remaining={}", format_duration_human(info.timers.delay_open.configured), format_duration_human(info.timers.delay_open.remaining), ); if !info.received_capabilities.is_empty() { - println!("\n{}", "Received Capabilities:".bold()); + println_nopipe!("\n{}", "Received Capabilities:".bold()); for cap in &info.received_capabilities { - println!(" {:?}", cap); + println_nopipe!(" {:?}", cap); } } - println!("\n{}", "Counters:".bold()); - println!(" Prefixes:"); - println!(" Advertised: {}", info.counters.prefixes_advertised); - println!(" Imported: {}", info.counters.prefixes_imported); - - println!(" Messages Sent:"); - println!(" Opens: {}", info.counters.opens_sent); - println!(" Updates: {}", info.counters.updates_sent); - println!(" Keepalives: {}", info.counters.keepalives_sent); - println!(" Route Refresh: {}", info.counters.route_refresh_sent); - println!(" Notifications: {}", info.counters.notifications_sent); - - println!(" Messages Received:"); - println!(" Opens: {}", info.counters.opens_received); - println!(" Updates: {}", info.counters.updates_received); - println!(" Keepalives: {}", info.counters.keepalives_received); - println!( + println_nopipe!("\n{}", "Counters:".bold()); + println_nopipe!(" Prefixes:"); + println_nopipe!( + " Advertised: {}", + info.counters.prefixes_advertised + ); + println_nopipe!(" Imported: {}", info.counters.prefixes_imported); + + println_nopipe!(" Messages Sent:"); + println_nopipe!(" Opens: {}", info.counters.opens_sent); + println_nopipe!(" Updates: {}", info.counters.updates_sent); + println_nopipe!(" Keepalives: {}", info.counters.keepalives_sent); + println_nopipe!( + " Route Refresh: {}", + info.counters.route_refresh_sent + ); + println_nopipe!( + " Notifications: {}", + info.counters.notifications_sent + ); + + println_nopipe!(" Messages Received:"); + println_nopipe!(" Opens: {}", info.counters.opens_received); + println_nopipe!(" Updates: {}", info.counters.updates_received); + println_nopipe!( + " Keepalives: {}", + info.counters.keepalives_received + ); + println_nopipe!( " Route Refresh: {}", info.counters.route_refresh_received ); - println!( + println_nopipe!( " Notifications: {}", info.counters.notifications_received ); - println!(" FSM Transitions:"); - println!( + println_nopipe!(" FSM Transitions:"); + println_nopipe!( " To Established: {}", info.counters.transitions_to_established ); - println!(" To Idle: {}", info.counters.transitions_to_idle); - println!(" To Connect: {}", info.counters.transitions_to_connect); + println_nopipe!(" To Idle: {}", info.counters.transitions_to_idle); + println_nopipe!( + " To Connect: {}", + info.counters.transitions_to_connect + ); - println!(" Connections:"); - println!( + println_nopipe!(" Connections:"); + println_nopipe!( " Active Accepted: {}", info.counters.active_connections_accepted ); - println!( + println_nopipe!( " Active Declined: {}", info.counters.active_connections_declined ); - println!( + println_nopipe!( " Passive Accepted: {}", info.counters.passive_connections_accepted ); - println!( + println_nopipe!( " Passive Declined: {}", info.counters.passive_connections_declined ); - println!( + println_nopipe!( " Connection Retries: {}", info.counters.connection_retries ); // Error Counters - println!("\n{}", "Error Counters:".bold()); - println!( + println_nopipe!("\n{}", "Error Counters:".bold()); + println_nopipe!( " TCP Connection Failures: {}", info.counters.tcp_connection_failure ); - println!(" MD5 Auth Failures: {}", info.counters.md5_auth_failures); - println!( + println_nopipe!( + " MD5 Auth Failures: {}", + info.counters.md5_auth_failures + ); + println_nopipe!( " Hold Timer Expirations: {}", info.counters.hold_timer_expirations ); - println!( + println_nopipe!( " Update Nexthop Missing: {}", info.counters.update_nexhop_missing ); - println!( + println_nopipe!( " Open Handle Failures: {}", info.counters.open_handle_failures ); - println!( + println_nopipe!( " Notification Send Failures: {}", info.counters.notification_send_failure ); - println!(" Connector Panics: {}", info.counters.connector_panics); + println_nopipe!( + " Connector Panics: {}", + info.counters.connector_panics + ); } Ok(()) @@ -1288,7 +1324,7 @@ async fn get_exported( .await? .into_inner(); - println!("{exported:#?}"); + println_nopipe!("{exported:#?}"); Ok(()) } @@ -1298,7 +1334,7 @@ async fn list_nbr(asn: u32, c: Client) -> Result<()> { let unnumbered = c.read_unnumbered_neighbors(asn).await?.into_inner(); if numbered.is_empty() && unnumbered.is_empty() { - println!("No neighbors configured for ASN {}", asn); + println_nopipe!("No neighbors configured for ASN {}", asn); return Ok(()); } @@ -1354,14 +1390,14 @@ async fn read_nbr(asn: u32, peer: String, c: Client) -> Result<()> { .read_neighbor_v3(asn, &addr.to_string()) .await? .into_inner(); - println!("{nbr:#?}"); + println_nopipe!("{nbr:#?}"); } PeerType::Unnumbered(interface) => { let nbr = c .read_unnumbered_neighbor(asn, &interface) .await? .into_inner(); - println!("{nbr:#?}"); + println_nopipe!("{nbr:#?}"); } } Ok(()) @@ -1455,7 +1491,7 @@ async fn delete_origin4(asn: u32, c: Client) -> Result<()> { async fn read_origin4(asn: u32, c: Client) -> Result<()> { let o4 = c.read_origin4(asn).await?; - println!("{o4:#?}"); + println_nopipe!("{o4:#?}"); Ok(()) } @@ -1494,7 +1530,7 @@ async fn delete_origin6(asn: u32, c: Client) -> Result<()> { async fn read_origin6(asn: u32, c: Client) -> Result<()> { let o6 = c.read_origin6(asn).await?; - println!("{o6:#?}"); + println_nopipe!("{o6:#?}"); Ok(()) } @@ -1518,7 +1554,7 @@ async fn create_chk(filename: String, asn: u32, c: Client) -> Result<()> { async fn read_chk(asn: u32, c: Client) -> Result<()> { let result = c.read_checker(asn).await?; - print!("{result:#?}"); + print_nopipe!("{result:#?}"); Ok(()) } @@ -1550,7 +1586,7 @@ async fn create_shp(filename: String, asn: u32, c: Client) -> Result<()> { async fn read_shp(asn: u32, c: Client) -> Result<()> { let result = c.read_shaper(asn).await?; - print!("{result:#?}"); + print_nopipe!("{result:#?}"); Ok(()) } @@ -1606,9 +1642,9 @@ async fn get_fsm_history( if result.by_peer.is_empty() { if let Some(peer_str) = peer { - println!("No FSM history found for peer {}", peer_str); + println_nopipe!("No FSM history found for peer {}", peer_str); } else { - println!("No FSM history found for ASN {}", asn); + println_nopipe!("No FSM history found for ASN {}", asn); } return Ok(()); } @@ -1627,7 +1663,7 @@ async fn get_fsm_history( // Display FSM history in tabular format for (peer_addr, events) in result.by_peer.iter() { if events.is_empty() { - println!( + println_nopipe!( "\n{}", format!( "FSM Event History - Peer: {} - {} (empty)", @@ -1638,7 +1674,7 @@ async fn get_fsm_history( continue; } - println!( + println_nopipe!( "\n{}", format!( "FSM Event History - Peer: {} - {}", @@ -1646,8 +1682,8 @@ async fn get_fsm_history( ) .dimmed() ); - println!("{}", "=".repeat(100).dimmed()); - println!( + println_nopipe!("{}", "=".repeat(100).dimmed()); + println_nopipe!( "Showing {} of {} events\n", events.len().min(limit), events.len() @@ -1696,7 +1732,7 @@ async fn get_fsm_history( tw.flush()?; if events.len() > limit { - println!( + println_nopipe!( "\n... ({} more events not shown, use --limit all to see everything)", events.len() - limit ); @@ -1748,7 +1784,11 @@ async fn get_message_history( .into_inner(); if result.by_peer.is_empty() { - println!("No message history found for ASN {} peer {}", asn, peer); + println_nopipe!( + "No message history found for ASN {} peer {}", + asn, + peer + ); return Ok(()); } @@ -1787,12 +1827,12 @@ async fn get_message_history( all_messages.iter().rev().take(limit).collect::>(); let total_count = all_messages.len(); - println!( + println_nopipe!( "\n{}", format!("BGP Message History - Peer: {}", peer).dimmed() ); - println!("{}", "=".repeat(80).dimmed()); - println!( + println_nopipe!("{}", "=".repeat(80).dimmed()); + println_nopipe!( "Showing {} of {} messages ({} RX, {} TX)\n", messages_to_show.len(), total_count, @@ -1820,7 +1860,7 @@ async fn get_message_history( msg_content }; - println!( + println_nopipe!( "{} {} [{}] {}", ts_str.to_string().dimmed(), if *direction == "RX" { diff --git a/mgadm/src/main.rs b/mgadm/src/main.rs index 3ceec95c1..340179f57 100644 --- a/mgadm/src/main.rs +++ b/mgadm/src/main.rs @@ -63,7 +63,15 @@ enum Commands { } fn main() -> Result<()> { - oxide_tokio_rt::run(run()) + match oxide_tokio_rt::run(run()) { + Ok(()) => Ok(()), + Err(e) => { + if mg_common::is_broken_pipe(&e) { + std::process::exit(0); + } + Err(e) + } + } } async fn run() -> Result<()> { diff --git a/mgadm/src/ndp.rs b/mgadm/src/ndp.rs index de2a8f376..f4a1dc52c 100644 --- a/mgadm/src/ndp.rs +++ b/mgadm/src/ndp.rs @@ -6,6 +6,7 @@ use anyhow::Result; use clap::{Args, Subcommand}; use colored::Colorize; use mg_admin_client::Client; +use mg_common::println_nopipe; use std::io::{Write, stdout}; use tabwriter::TabWriter; @@ -59,9 +60,9 @@ pub async fn commands(command: Commands, c: Client) -> Result<()> { async fn ndp_manager_status(asn: u32, c: Client) -> Result<()> { let state = c.get_ndp_manager_state(asn).await?.into_inner(); - println!("NDP Manager State (ASN {})", asn); - println!("{}", "=".repeat(60)); - println!(); + println_nopipe!("NDP Manager State (ASN {})", asn); + println_nopipe!("{}", "=".repeat(60)); + println_nopipe!(); // Monitor thread status let monitor_status = if state.monitor_thread_running { @@ -69,33 +70,34 @@ async fn ndp_manager_status(asn: u32, c: Client) -> Result<()> { } else { "Stopped".red() }; - println!("Monitor Thread: {}", monitor_status); - println!(); + println_nopipe!("Monitor Thread: {}", monitor_status); + println_nopipe!(); // Pending interfaces - println!( + println_nopipe!( "Pending Interfaces (configured, waiting for system): {}", state.pending_interfaces.len() ); if state.pending_interfaces.is_empty() { - println!(" (none)"); + println_nopipe!(" (none)"); } else { for pending in &state.pending_interfaces { - println!( + println_nopipe!( " {} (router_lifetime: {}s)", - pending.interface, pending.router_lifetime + pending.interface, + pending.router_lifetime ); } } - println!(); + println_nopipe!(); // Active interfaces - println!("Active Interfaces: {}", state.active_interfaces.len()); + println_nopipe!("Active Interfaces: {}", state.active_interfaces.len()); if state.active_interfaces.is_empty() { - println!(" (none)"); + println_nopipe!(" (none)"); } else { for iface in &state.active_interfaces { - println!(" {}", iface); + println_nopipe!(" {}", iface); } } @@ -106,7 +108,7 @@ async fn ndp_interfaces(asn: u32, c: Client) -> Result<()> { let interfaces = c.get_ndp_interfaces(asn).await?.into_inner(); if interfaces.is_empty() { - println!("No NDP-managed interfaces found for ASN {}", asn); + println_nopipe!("No NDP-managed interfaces found for ASN {}", asn); return Ok(()); } @@ -181,22 +183,22 @@ async fn ndp_interface_detail( .await? .into_inner(); - println!("NDP State: {}", interface); - println!("{}", "=".repeat(60)); - println!(); + println_nopipe!("NDP State: {}", interface); + println_nopipe!("{}", "=".repeat(60)); + println_nopipe!(); - println!("Interface Information:"); - println!(" Name: {}", detail.interface); - println!(" Local Address: {}", detail.local_address); - println!(" Scope ID: {}", detail.scope_id); - println!( + println_nopipe!("Interface Information:"); + println_nopipe!(" Name: {}", detail.interface); + println_nopipe!(" Local Address: {}", detail.local_address); + println_nopipe!(" Scope ID: {}", detail.scope_id); + println_nopipe!( " Router Lifetime (advertised): {}s", detail.router_lifetime ); - println!(); + println_nopipe!(); // Thread state - println!("Thread State:"); + println_nopipe!("Thread State:"); if let Some(ts) = &detail.thread_state { let tx_status = if ts.tx_running { "Running".green() @@ -208,41 +210,41 @@ async fn ndp_interface_detail( } else { "Stopped".red() }; - println!(" TX Loop: {}", tx_status); - println!(" RX Loop: {}", rx_status); + println_nopipe!(" TX Loop: {}", tx_status); + println_nopipe!(" RX Loop: {}", rx_status); } else { - println!(" TX Loop: {}", "Unknown".dimmed()); - println!(" RX Loop: {}", "Unknown".dimmed()); + println_nopipe!(" TX Loop: {}", "Unknown".dimmed()); + println_nopipe!(" RX Loop: {}", "Unknown".dimmed()); } - println!(); + println_nopipe!(); if let Some(peer) = detail.discovered_peer { if peer.expired { - println!("{}", "Discovered Peer (EXPIRED):".red()); + println_nopipe!("{}", "Discovered Peer (EXPIRED):".red()); } else { - println!("Discovered Peer:"); + println_nopipe!("Discovered Peer:"); } - println!(" Address: {}", peer.address); - println!(" Discovered At: {}", peer.discovered_at); - println!(" Last Advertisement: {}", peer.last_advertisement); - println!(" Router Lifetime: {}s", peer.router_lifetime); - println!(" Reachable Time: {}ms", peer.reachable_time); - println!(" Retrans Timer: {}ms", peer.retrans_timer); + println_nopipe!(" Address: {}", peer.address); + println_nopipe!(" Discovered At: {}", peer.discovered_at); + println_nopipe!(" Last Advertisement: {}", peer.last_advertisement); + println_nopipe!(" Router Lifetime: {}s", peer.router_lifetime); + println_nopipe!(" Reachable Time: {}ms", peer.reachable_time); + println_nopipe!(" Retrans Timer: {}ms", peer.retrans_timer); if peer.expired { - println!(" Expired: {}", "Yes".red()); + println_nopipe!(" Expired: {}", "Yes".red()); if let Some(time_since) = peer.time_until_expiry { - println!(" Time Since Expiry: {}", time_since); + println_nopipe!(" Time Since Expiry: {}", time_since); } } else { - println!(" Expired: {}", "No".green()); + println_nopipe!(" Expired: {}", "No".green()); if let Some(time_until) = peer.time_until_expiry { - println!(" Time Until Expiry: {}", time_until); + println_nopipe!(" Time Until Expiry: {}", time_until); } } } else { - println!("Discovered Peer: None"); + println_nopipe!("Discovered Peer: None"); } Ok(()) diff --git a/mgadm/src/rib.rs b/mgadm/src/rib.rs index 21f008281..ee122264a 100644 --- a/mgadm/src/rib.rs +++ b/mgadm/src/rib.rs @@ -2,13 +2,13 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use std::num::NonZeroU8; - use anyhow::Result; use clap::{Args, Subcommand}; use mg_admin_client::types::BestpathFanoutRequest; use mg_admin_client::{Client, print_rib}; +use mg_common::println_nopipe; use rdb::types::{AddressFamily, ProtocolFilter}; +use std::num::NonZeroU8; #[derive(Subcommand, Debug)] pub enum Commands { @@ -120,13 +120,13 @@ async fn get_selected( async fn read_bestpath_fanout(c: Client) -> Result<()> { let result = c.read_rib_bestpath_fanout().await?; - println!("{}", result.into_inner().fanout); + println_nopipe!("{}", result.into_inner().fanout); Ok(()) } async fn update_bestpath_fanout(fanout: NonZeroU8, c: Client) -> Result<()> { c.update_rib_bestpath_fanout(&BestpathFanoutRequest { fanout }) .await?; - println!("Updated bestpath fanout to: {}", fanout); + println_nopipe!("Updated bestpath fanout to: {}", fanout); Ok(()) } diff --git a/mgadm/src/static_routing.rs b/mgadm/src/static_routing.rs index d806a709b..9c4fc68e5 100644 --- a/mgadm/src/static_routing.rs +++ b/mgadm/src/static_routing.rs @@ -5,6 +5,7 @@ use anyhow::Result; use clap::{Args, Subcommand}; use mg_admin_client::{Client, types}; +use mg_common::println_nopipe; use oxnet::{Ipv4Net, Ipv6Net}; use rdb::{DEFAULT_RIB_PRIORITY_STATIC, Prefix4, Prefix6}; use std::net::IpAddr; @@ -43,7 +44,7 @@ pub async fn commands(command: Commands, client: Client) -> Result<()> { match command { Commands::GetV4Routes => { let routes = client.static_list_v4_routes_v2().await?; - println!("{:#?}", routes); + println_nopipe!("{:#?}", routes); } Commands::AddV4Route(route) => { let arg = types::AddStaticRoute4Request { @@ -79,7 +80,7 @@ pub async fn commands(command: Commands, client: Client) -> Result<()> { } Commands::GetV6Routes => { let routes = client.static_list_v6_routes_v2().await?; - println!("{:#?}", routes); + println_nopipe!("{:#?}", routes); } Commands::AddV6Route(route) => { let arg = types::AddStaticRoute6Request { From f4b00d50d47e978be36f9f158a1a2b9601a94395 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Thu, 12 Feb 2026 15:06:56 -0700 Subject: [PATCH 07/58] rdb: Fix Ord for static paths w/ same nexthop The Ord impl we have for Path was only checking nexthop_interface and vlan_id for an identity check which returns Ordering::Equal if bgp is None and the (nexthop, nexthop_interface, vlan_id) are the same. However, when all these fields are not the same that impl moved onto a tie-breaker which compared the nexthop before moving onto the bgp cmp, without ever doing a cmp for nexthop_interface or vlan_id. This meant that any two Paths with the same nexthop but different nexthop_interface and/or vlan_id would be treated as identical paths. BTreeSet relies on this Ord impl to determine whether an insertion should replace or add an element. The above issue in the Ord impl would result in elements being replaced instead of added, which was observed in #528 when we static routes are pulled out of the persistent sled DB and into a BTreeSet before being returned to the API client. The impact of this is not limited to just `mgadm get-v{4,6}-routes` returning misleading information, but also missing static routes if mgd were to restart. However, this bug does not affect ECMP paths with separate next-hops since the nexthop field was compared as part of the tie-breaker when the composite identity check did not match. Fixes: #528 --- rdb/src/db.rs | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ rdb/src/types.rs | 6 ++++ 2 files changed, 84 insertions(+) diff --git a/rdb/src/db.rs b/rdb/src/db.rs index 547d27bbe..31b93b952 100644 --- a/rdb/src/db.rs +++ b/rdb/src/db.rs @@ -2451,6 +2451,84 @@ mod test { assert!("192.168.1.0/abc".parse::().is_err()); } + // Regression test for https://github.com/oxidecomputer/maghemite/issues/528 + // + // Static routes with the same prefix and nexthop but different vlan_ids + // must coexist in both persistent storage and the in-memory RIB. + #[test] + fn test_static_routes_same_nexthop_different_vlans() { + let db = get_test_db(); + let prefix4 = + Prefix4::new(Ipv4Addr::from_str("172.20.0.0").unwrap(), 24); + let nexthop = + IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()); + + let route_vlan100 = StaticRouteKey { + prefix: Prefix::V4(prefix4), + nexthop, + vlan_id: Some(100), + rib_priority: DEFAULT_RIB_PRIORITY_STATIC, + }; + let route_vlan200 = StaticRouteKey { + prefix: Prefix::V4(prefix4), + nexthop, + vlan_id: Some(200), + rib_priority: DEFAULT_RIB_PRIORITY_STATIC, + }; + let route_no_vlan = StaticRouteKey { + prefix: Prefix::V4(prefix4), + nexthop, + vlan_id: None, + rib_priority: DEFAULT_RIB_PRIORITY_STATIC, + }; + + // Add all three routes + db.add_static_routes(&[route_vlan100, route_vlan200, route_no_vlan]) + .unwrap(); + + // Verify persistent storage has all three + let routes = db.get_static(Some(AddressFamily::Ipv4)).unwrap(); + assert_eq!( + routes.len(), + 3, + "persistent store should contain all three routes, got: {routes:?}" + ); + + // Verify in-memory RIB has all three paths for this prefix + let prefix = Prefix::V4(prefix4); + let rib_paths = db.get_prefix_paths(&prefix); + assert_eq!( + rib_paths.len(), + 3, + "in-memory RIB should contain all three paths, got: {rib_paths:?}" + ); + + // Verify each path has the correct vlan_id + let vlan_ids: Vec> = + rib_paths.iter().map(|p| p.vlan_id).collect(); + assert!( + vlan_ids.contains(&Some(100)), + "should contain vlan 100" + ); + assert!( + vlan_ids.contains(&Some(200)), + "should contain vlan 200" + ); + assert!(vlan_ids.contains(&None), "should contain no-vlan route"); + + // Verify removal of one route leaves the other two + db.remove_static_routes(&[route_vlan100]).unwrap(); + let routes = db.get_static(Some(AddressFamily::Ipv4)).unwrap(); + assert_eq!(routes.len(), 2); + let rib_paths = db.get_prefix_paths(&prefix); + assert_eq!(rib_paths.len(), 2); + + db.remove_static_routes(&[route_vlan200, route_no_vlan]) + .unwrap(); + let routes = db.get_static(Some(AddressFamily::Ipv4)).unwrap(); + assert!(routes.is_empty()); + } + #[test] fn test_prefix6_from_str() { let prefix_str = "2001:db8::/32"; diff --git a/rdb/src/types.rs b/rdb/src/types.rs index 94fe10106..eb3d0efb2 100644 --- a/rdb/src/types.rs +++ b/rdb/src/types.rs @@ -130,6 +130,12 @@ impl Ord for Path { if self.nexthop != other.nexthop { return self.nexthop.cmp(&other.nexthop); } + if self.vlan_id != other.vlan_id { + return self.vlan_id.cmp(&other.vlan_id); + } + if self.nexthop_interface != other.nexthop_interface { + return self.nexthop_interface.cmp(&other.nexthop_interface); + } if self.shutdown != other.shutdown { return self.shutdown.cmp(&other.shutdown); } From 3850edd27ed8d6f7638e93f0d4ec9df3c51fdabf Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Thu, 12 Feb 2026 16:04:27 -0700 Subject: [PATCH 08/58] Plumb nexthop_interface through static routing --- mg-api/src/lib.rs | 6 + mgadm/src/static_routing.rs | 16 +++ mgd/src/main.rs | 11 +- ...f92782.json => mg-admin-7.0.0-693bed.json} | 8 ++ openapi/mg-admin/mg-admin-latest.json | 2 +- rdb/src/db.rs | 111 ++++++++++++------ rdb/src/proptest.rs | 38 +++--- rdb/src/test.rs | 1 + rdb/src/types.rs | 7 +- 9 files changed, 141 insertions(+), 59 deletions(-) rename openapi/mg-admin/{mg-admin-7.0.0-f92782.json => mg-admin-7.0.0-693bed.json} (99%) diff --git a/mg-api/src/lib.rs b/mg-api/src/lib.rs index 785edd5d2..33fdb344f 100644 --- a/mg-api/src/lib.rs +++ b/mg-api/src/lib.rs @@ -952,6 +952,7 @@ impl From for StaticRouteKey { StaticRouteKey { prefix: val.prefix.into(), nexthop: val.nexthop.into(), + nexthop_interface: None, vlan_id: val.vlan_id, rib_priority: val.rib_priority, } @@ -990,6 +991,7 @@ impl From for StaticRouteKey { StaticRouteKey { prefix: val.prefix.into(), nexthop: val.nexthop.into(), + nexthop_interface: None, vlan_id: val.vlan_id, rib_priority: val.rib_priority, } @@ -1019,6 +1021,7 @@ pub struct StaticRoute4List { pub struct StaticRoute4 { pub prefix: Prefix4, pub nexthop: IpAddr, + pub nexthop_interface: Option, pub vlan_id: Option, pub rib_priority: u8, } @@ -1028,6 +1031,7 @@ impl From for StaticRouteKey { StaticRouteKey { prefix: val.prefix.into(), nexthop: val.nexthop, + nexthop_interface: val.nexthop_interface, vlan_id: val.vlan_id, rib_priority: val.rib_priority, } @@ -1053,6 +1057,7 @@ pub struct StaticRoute6List { pub struct StaticRoute6 { pub prefix: Prefix6, pub nexthop: IpAddr, + pub nexthop_interface: Option, pub vlan_id: Option, pub rib_priority: u8, } @@ -1062,6 +1067,7 @@ impl From for StaticRouteKey { StaticRouteKey { prefix: val.prefix.into(), nexthop: val.nexthop, + nexthop_interface: val.nexthop_interface, vlan_id: val.vlan_id, rib_priority: val.rib_priority, } diff --git a/mgadm/src/static_routing.rs b/mgadm/src/static_routing.rs index 9c4fc68e5..6cb51f5ed 100644 --- a/mgadm/src/static_routing.rs +++ b/mgadm/src/static_routing.rs @@ -25,6 +25,8 @@ pub struct StaticRoute4 { pub destination: Ipv4Net, pub nexthop: IpAddr, #[clap(long)] + pub nexthop_interface: Option, + #[clap(long)] pub vlan_id: Option, #[clap(long, default_value_t = DEFAULT_RIB_PRIORITY_STATIC)] pub rib_priority: u8, @@ -35,6 +37,8 @@ pub struct StaticRoute6 { pub destination: Ipv6Net, pub nexthop: IpAddr, #[clap(long)] + pub nexthop_interface: Option, + #[clap(long)] pub vlan_id: Option, #[clap(long, default_value_t = DEFAULT_RIB_PRIORITY_STATIC)] pub rib_priority: u8, @@ -55,6 +59,7 @@ pub async fn commands(command: Commands, client: Client) -> Result<()> { route.destination.width(), ), nexthop: route.nexthop, + nexthop_interface: route.nexthop_interface, vlan_id: route.vlan_id, rib_priority: route.rib_priority, }], @@ -71,6 +76,7 @@ pub async fn commands(command: Commands, client: Client) -> Result<()> { route.destination.width(), ), nexthop: route.nexthop, + nexthop_interface: route.nexthop_interface, vlan_id: route.vlan_id, rib_priority: route.rib_priority, }], @@ -91,6 +97,7 @@ pub async fn commands(command: Commands, client: Client) -> Result<()> { length: route.destination.width(), }, nexthop: route.nexthop, + nexthop_interface: route.nexthop_interface, vlan_id: route.vlan_id, rib_priority: route.rib_priority, }], @@ -107,6 +114,7 @@ pub async fn commands(command: Commands, client: Client) -> Result<()> { length: route.destination.width(), }, nexthop: route.nexthop, + nexthop_interface: route.nexthop_interface, vlan_id: route.vlan_id, rib_priority: route.rib_priority, }], @@ -130,6 +138,7 @@ mod tests { let route4 = StaticRoute4 { destination: Ipv4Net::from_str("192.168.0.0/16").unwrap(), nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()), + nexthop_interface: None, vlan_id: Some(100), rib_priority: 50, }; @@ -140,6 +149,7 @@ mod tests { route4.destination.width(), ), nexthop: route4.nexthop, + nexthop_interface: route4.nexthop_interface, vlan_id: route4.vlan_id, rib_priority: route4.rib_priority, }; @@ -157,6 +167,7 @@ mod tests { let route6 = StaticRoute6 { destination: Ipv6Net::from_str("fd00::/8").unwrap(), nexthop: IpAddr::V6(Ipv6Addr::from_str("fe80::1").unwrap()), + nexthop_interface: None, vlan_id: Some(300), rib_priority: 75, }; @@ -167,6 +178,7 @@ mod tests { route6.destination.width(), ), nexthop: route6.nexthop, + nexthop_interface: route6.nexthop_interface, vlan_id: route6.vlan_id, rib_priority: route6.rib_priority, }; @@ -184,6 +196,7 @@ mod tests { let route4_v6nh = StaticRoute4 { destination: Ipv4Net::from_str("10.0.0.0/8").unwrap(), nexthop: IpAddr::V6(Ipv6Addr::from_str("fe80::1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: 50, }; @@ -194,6 +207,7 @@ mod tests { route4_v6nh.destination.width(), ), nexthop: route4_v6nh.nexthop, + nexthop_interface: route4_v6nh.nexthop_interface, vlan_id: route4_v6nh.vlan_id, rib_priority: route4_v6nh.rib_priority, }; @@ -207,6 +221,7 @@ mod tests { let route6_v4nh = StaticRoute6 { destination: Ipv6Net::from_str("2001:db8::/32").unwrap(), nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()), + nexthop_interface: None, vlan_id: Some(42), rib_priority: 50, }; @@ -217,6 +232,7 @@ mod tests { route6_v4nh.destination.width(), ), nexthop: route6_v4nh.nexthop, + nexthop_interface: route6_v4nh.nexthop_interface, vlan_id: route6_v4nh.vlan_id, rib_priority: route6_v4nh.rib_priority, }; diff --git a/mgd/src/main.rs b/mgd/src/main.rs index 9c0e87463..bb04dcb1a 100644 --- a/mgd/src/main.rs +++ b/mgd/src/main.rs @@ -453,7 +453,7 @@ fn initialize_static_routes(db: &rdb::Db, log: &Logger) { let normalized: BTreeSet = routes .iter() .map(|srk| { - let mut normalized = *srk; + let mut normalized = srk.clone(); normalized.prefix.unset_host_bits(); normalized }) @@ -534,6 +534,7 @@ mod tests { length: 24, }), nexthop: IpAddr::V4(Ipv4Addr::from_str("192.168.1.1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: 0, }, @@ -543,6 +544,7 @@ mod tests { length: 24, }), nexthop: IpAddr::V4(Ipv4Addr::from_str("192.168.1.1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: 0, }, @@ -589,6 +591,7 @@ mod tests { length: 24, }), nexthop: IpAddr::V4(Ipv4Addr::from_str("192.168.1.1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: 0, }, @@ -598,6 +601,7 @@ mod tests { length: 24, }), nexthop: IpAddr::V4(Ipv4Addr::from_str("192.168.1.2").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: 0, }, @@ -640,6 +644,7 @@ mod tests { 24, )), nexthop: IpAddr::V4(Ipv4Addr::from_str("192.168.1.1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: 0, }]; @@ -679,6 +684,7 @@ mod tests { length: 64, }), nexthop: IpAddr::V6(Ipv6Addr::from_str("fe80::1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: 0, }, @@ -688,6 +694,7 @@ mod tests { length: 64, }), nexthop: IpAddr::V6(Ipv6Addr::from_str("fe80::2").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: 0, }, @@ -724,6 +731,7 @@ mod tests { length: 24, }), nexthop: IpAddr::V4(Ipv4Addr::from_str("192.168.1.1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: 0, }, @@ -733,6 +741,7 @@ mod tests { length: 64, }), nexthop: IpAddr::V6(Ipv6Addr::from_str("fe80::1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: 0, }, diff --git a/openapi/mg-admin/mg-admin-7.0.0-f92782.json b/openapi/mg-admin/mg-admin-7.0.0-693bed.json similarity index 99% rename from openapi/mg-admin/mg-admin-7.0.0-f92782.json rename to openapi/mg-admin/mg-admin-7.0.0-693bed.json index 924ee8f1c..d5461847b 100644 --- a/openapi/mg-admin/mg-admin-7.0.0-f92782.json +++ b/openapi/mg-admin/mg-admin-7.0.0-693bed.json @@ -5251,6 +5251,10 @@ "type": "string", "format": "ip" }, + "nexthop_interface": { + "nullable": true, + "type": "string" + }, "prefix": { "$ref": "#/components/schemas/Prefix4" }, @@ -5293,6 +5297,10 @@ "type": "string", "format": "ip" }, + "nexthop_interface": { + "nullable": true, + "type": "string" + }, "prefix": { "$ref": "#/components/schemas/Prefix6" }, diff --git a/openapi/mg-admin/mg-admin-latest.json b/openapi/mg-admin/mg-admin-latest.json index cdfd85214..875295713 120000 --- a/openapi/mg-admin/mg-admin-latest.json +++ b/openapi/mg-admin/mg-admin-latest.json @@ -1 +1 @@ -mg-admin-7.0.0-f92782.json \ No newline at end of file +mg-admin-7.0.0-693bed.json \ No newline at end of file diff --git a/rdb/src/db.rs b/rdb/src/db.rs index 31b93b952..31cb4d89e 100644 --- a/rdb/src/db.rs +++ b/rdb/src/db.rs @@ -859,7 +859,7 @@ impl Db { tree.flush()?; for route in routes { - self.add_prefix_path(&route.prefix, &Path::from(*route)); + self.add_prefix_path(&route.prefix, &Path::from(route.clone())); pcn.changed.insert(route.prefix); } @@ -1230,7 +1230,7 @@ impl Db { for route in routes { self.remove_prefix_path(&route.prefix, |rib_path: &Path| { - rib_path.cmp(&Path::from(*route)) == CmpOrdering::Equal + rib_path.cmp(&Path::from(route.clone())) == CmpOrdering::Equal }); } @@ -1246,8 +1246,8 @@ impl Db { (Vec::new(), Vec::new()), |(mut r4, mut r6), srk| { match srk.prefix { - Prefix::V4(_) => r4.push(*srk), - Prefix::V6(_) => r6.push(*srk), + Prefix::V4(_) => r4.push(srk.clone()), + Prefix::V6(_) => r6.push(srk.clone()), } (r4, r6) }, @@ -1584,17 +1584,19 @@ mod test { let static_key0 = StaticRouteKey { prefix: p0, nexthop: remote_ip0, + nexthop_interface: None, vlan_id: None, rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; - let static_path0 = Path::from(static_key0); + let static_path0 = Path::from(static_key0.clone()); let static_key0_updated = StaticRouteKey { prefix: p0, nexthop: remote_ip0, + nexthop_interface: None, vlan_id: None, rib_priority: DEFAULT_RIB_PRIORITY_STATIC + 10, }; - let static_path0_updated = Path::from(static_key0_updated); + let static_path0_updated = Path::from(static_key0_updated.clone()); // Static route for testing ECMP: // static_key1 has a DIFFERENT identity (different nexthop) than static_key0, @@ -1602,10 +1604,11 @@ mod test { let static_key1 = StaticRouteKey { prefix: p0, nexthop: remote_ip1, + nexthop_interface: None, vlan_id: None, rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; - let static_path1 = Path::from(static_key1); + let static_path1 = Path::from(static_key1.clone()); // setup std::fs::create_dir_all("/tmp").expect("create tmp dir"); @@ -1625,7 +1628,7 @@ mod test { // Adding two static routes with the same identity (nexthop, vlan_id) // should result in the second replacing the first. // ===================================================================== - db.add_static_routes(&[static_key0]) + db.add_static_routes(std::slice::from_ref(&static_key0)) .expect("add static_key0"); // Verify static_path0 is installed @@ -1647,7 +1650,7 @@ mod test { // Test 2: ECMP - multiple static routes with different identities // Adding a static route with a different nexthop should coexist. // ===================================================================== - db.add_static_routes(&[static_key1]) + db.add_static_routes(std::slice::from_ref(&static_key1)) .expect("add static_key1"); // Verify both paths coexist (ECMP) @@ -1812,12 +1815,14 @@ mod test { let static_route = StaticRouteKey { prefix: Prefix::V4(prefix4), nexthop, + nexthop_interface: None, vlan_id: Some(100), rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; // Add the route - db.add_static_routes(&[static_route]).unwrap(); + db.add_static_routes(std::slice::from_ref(&static_route)) + .unwrap(); // Verify route was added let routes = db.get_static(Some(AddressFamily::Ipv4)).unwrap(); @@ -1830,7 +1835,8 @@ mod test { assert!(rib_routes.contains_key(&Prefix::V4(prefix4))); // Remove the route - db.remove_static_routes(&[static_route]).unwrap(); + db.remove_static_routes(std::slice::from_ref(&static_route)) + .unwrap(); // Verify route was removed let routes = db.get_static(Some(AddressFamily::Ipv4)).unwrap(); @@ -1852,12 +1858,14 @@ mod test { let static_route = StaticRouteKey { prefix: Prefix::V6(prefix6), nexthop, + nexthop_interface: None, vlan_id: Some(200), rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; // Add the route - db.add_static_routes(&[static_route]).unwrap(); + db.add_static_routes(std::slice::from_ref(&static_route)) + .unwrap(); // Verify route was added let routes = db.get_static(Some(AddressFamily::Ipv6)).unwrap(); @@ -1870,7 +1878,8 @@ mod test { assert!(rib_routes.contains_key(&Prefix::V6(prefix6))); // Remove the route - db.remove_static_routes(&[static_route]).unwrap(); + db.remove_static_routes(std::slice::from_ref(&static_route)) + .unwrap(); // Verify route was removed let routes = db.get_static(Some(AddressFamily::Ipv6)).unwrap(); @@ -1891,6 +1900,7 @@ mod test { let route_no_vlan = StaticRouteKey { prefix: Prefix::V6(prefix6), nexthop: IpAddr::V6(Ipv6Addr::from_str("fe80::1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; @@ -1899,12 +1909,13 @@ mod test { let route_with_vlan = StaticRouteKey { prefix: Prefix::V6(prefix6), nexthop: IpAddr::V6(Ipv6Addr::from_str("fe80::2").unwrap()), + nexthop_interface: None, vlan_id: Some(4094), // Maximum VLAN ID rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; // Add both routes - db.add_static_routes(&[route_no_vlan, route_with_vlan]) + db.add_static_routes(&[route_no_vlan.clone(), route_with_vlan.clone()]) .unwrap(); // Verify both routes were added correctly @@ -1934,6 +1945,7 @@ mod test { let route4 = StaticRouteKey { prefix: Prefix::V4(prefix4), nexthop: IpAddr::V4(Ipv4Addr::from_str("192.168.1.1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; @@ -1941,12 +1953,14 @@ mod test { let route6 = StaticRouteKey { prefix: Prefix::V6(prefix6), nexthop: IpAddr::V6(Ipv6Addr::from_str("fe80::1").unwrap()), + nexthop_interface: None, vlan_id: Some(300), rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; // Add both routes - db.add_static_routes(&[route4, route6]).unwrap(); + db.add_static_routes(&[route4.clone(), route6.clone()]) + .unwrap(); // Test IPv4-only retrieval let ipv4_routes = db.get_static(Some(AddressFamily::Ipv4)).unwrap(); @@ -1984,6 +1998,7 @@ mod test { let route1 = StaticRouteKey { prefix: Prefix::V4(prefix4), nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: 100, }; @@ -1991,12 +2006,14 @@ mod test { let route2 = StaticRouteKey { prefix: Prefix::V4(prefix4), nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.2").unwrap()), + nexthop_interface: None, vlan_id: Some(100), rib_priority: 200, }; // Add both routes - db.add_static_routes(&[route1, route2]).unwrap(); + db.add_static_routes(&[route1.clone(), route2.clone()]) + .unwrap(); // Verify both routes were added let routes = db.get_static(Some(AddressFamily::Ipv4)).unwrap(); @@ -2026,6 +2043,7 @@ mod test { let route_no_vlan = StaticRouteKey { prefix: Prefix::V4(prefix4), nexthop: IpAddr::V4(Ipv4Addr::from_str("198.51.100.1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; @@ -2034,12 +2052,13 @@ mod test { let route_with_vlan = StaticRouteKey { prefix: Prefix::V4(prefix4), nexthop: IpAddr::V4(Ipv4Addr::from_str("198.51.100.2").unwrap()), + nexthop_interface: None, vlan_id: Some(4094), // Maximum VLAN ID rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; // Add both routes - db.add_static_routes(&[route_no_vlan, route_with_vlan]) + db.add_static_routes(&[route_no_vlan.clone(), route_with_vlan.clone()]) .unwrap(); // Verify both routes were added correctly @@ -2084,11 +2103,12 @@ mod test { let route = StaticRouteKey { prefix: Prefix::V4(prefix4_with_host_bits), nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; - db.add_static_routes(&[route]).unwrap(); + db.add_static_routes(std::slice::from_ref(&route)).unwrap(); let routes = db.get_static(Some(AddressFamily::Ipv4)).unwrap(); assert_eq!(routes.len(), 1); @@ -2102,7 +2122,8 @@ mod test { panic!("Expected IPv4 prefix"); } - db.remove_static_routes(&[route]).unwrap(); + db.remove_static_routes(std::slice::from_ref(&route)) + .unwrap(); } #[test] @@ -2200,12 +2221,14 @@ mod test { let static_route = StaticRouteKey { prefix: Prefix::V4(prefix4), nexthop, + nexthop_interface: None, vlan_id: None, rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; // Add the route - db.add_static_routes(&[static_route]).unwrap(); + db.add_static_routes(std::slice::from_ref(&static_route)) + .unwrap(); // Verify route was added to persistent store let routes = db.get_static(Some(AddressFamily::Ipv4)).unwrap(); @@ -2223,7 +2246,8 @@ mod test { assert_eq!(paths[0].nexthop, nexthop); // Remove the route - db.remove_static_routes(&[static_route]).unwrap(); + db.remove_static_routes(std::slice::from_ref(&static_route)) + .unwrap(); // Verify route was removed let routes = db.get_static(Some(AddressFamily::Ipv4)).unwrap(); @@ -2243,12 +2267,14 @@ mod test { let static_route = StaticRouteKey { prefix: Prefix::V6(prefix6), nexthop, + nexthop_interface: None, vlan_id: Some(100), rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; // Add the route - db.add_static_routes(&[static_route]).unwrap(); + db.add_static_routes(std::slice::from_ref(&static_route)) + .unwrap(); // Verify route was added to persistent store let routes = db.get_static(Some(AddressFamily::Ipv6)).unwrap(); @@ -2266,7 +2292,8 @@ mod test { assert_eq!(paths[0].nexthop, nexthop); // Remove the route - db.remove_static_routes(&[static_route]).unwrap(); + db.remove_static_routes(std::slice::from_ref(&static_route)) + .unwrap(); // Verify route was removed let routes = db.get_static(Some(AddressFamily::Ipv6)).unwrap(); @@ -2288,17 +2315,20 @@ mod test { let route_a = StaticRouteKey { prefix: Prefix::V6(prefix6), nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; let route_b = StaticRouteKey { prefix: Prefix::V6(prefix6), nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.2").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: DEFAULT_RIB_PRIORITY_STATIC + 10, }; - db.add_static_routes(&[route_a, route_b]).unwrap(); + db.add_static_routes(&[route_a.clone(), route_b.clone()]) + .unwrap(); // Both paths should be in rib-in let paths = db.get_prefix_paths(&Prefix::V6(prefix6)); @@ -2331,17 +2361,20 @@ mod test { let route_v6nh = StaticRouteKey { prefix: Prefix::V6(prefix6), nexthop: IpAddr::V6(Ipv6Addr::from_str("fe80::1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: DEFAULT_RIB_PRIORITY_STATIC + 10, }; let route_v4nh = StaticRouteKey { prefix: Prefix::V6(prefix6), nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; - db.add_static_routes(&[route_v6nh, route_v4nh]).unwrap(); + db.add_static_routes(&[route_v6nh.clone(), route_v4nh.clone()]) + .unwrap(); // Both in rib-in let paths = db.get_prefix_paths(&Prefix::V6(prefix6)); @@ -2373,17 +2406,19 @@ mod test { let route_no_vlan = StaticRouteKey { prefix: Prefix::V6(prefix6), nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()), + nexthop_interface: None, vlan_id: None, rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; let route_with_vlan = StaticRouteKey { prefix: Prefix::V6(prefix6), nexthop: IpAddr::V4(Ipv4Addr::from_str("10.0.0.2").unwrap()), + nexthop_interface: None, vlan_id: Some(4094), rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; - db.add_static_routes(&[route_no_vlan, route_with_vlan]) + db.add_static_routes(&[route_no_vlan.clone(), route_with_vlan.clone()]) .unwrap(); let routes = db.get_static(Some(AddressFamily::Ipv6)).unwrap(); @@ -2460,31 +2495,37 @@ mod test { let db = get_test_db(); let prefix4 = Prefix4::new(Ipv4Addr::from_str("172.20.0.0").unwrap(), 24); - let nexthop = - IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()); + let nexthop = IpAddr::V4(Ipv4Addr::from_str("10.0.0.1").unwrap()); let route_vlan100 = StaticRouteKey { prefix: Prefix::V4(prefix4), nexthop, + nexthop_interface: None, vlan_id: Some(100), rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; let route_vlan200 = StaticRouteKey { prefix: Prefix::V4(prefix4), nexthop, + nexthop_interface: None, vlan_id: Some(200), rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; let route_no_vlan = StaticRouteKey { prefix: Prefix::V4(prefix4), nexthop, + nexthop_interface: None, vlan_id: None, rib_priority: DEFAULT_RIB_PRIORITY_STATIC, }; // Add all three routes - db.add_static_routes(&[route_vlan100, route_vlan200, route_no_vlan]) - .unwrap(); + db.add_static_routes(&[ + route_vlan100.clone(), + route_vlan200.clone(), + route_no_vlan.clone(), + ]) + .unwrap(); // Verify persistent storage has all three let routes = db.get_static(Some(AddressFamily::Ipv4)).unwrap(); @@ -2506,14 +2547,8 @@ mod test { // Verify each path has the correct vlan_id let vlan_ids: Vec> = rib_paths.iter().map(|p| p.vlan_id).collect(); - assert!( - vlan_ids.contains(&Some(100)), - "should contain vlan 100" - ); - assert!( - vlan_ids.contains(&Some(200)), - "should contain vlan 200" - ); + assert!(vlan_ids.contains(&Some(100)), "should contain vlan 100"); + assert!(vlan_ids.contains(&Some(200)), "should contain vlan 200"); assert!(vlan_ids.contains(&None), "should contain no-vlan route"); // Verify removal of one route leaves the other two diff --git a/rdb/src/proptest.rs b/rdb/src/proptest.rs index a4bba03fb..b7e0d66e0 100644 --- a/rdb/src/proptest.rs +++ b/rdb/src/proptest.rs @@ -71,15 +71,19 @@ fn static_route_key_strategy() -> impl Strategy { ], any::>(), any::(), + proptest::option::of("[a-z]{1,8}"), ) - .prop_map(|(prefix, nexthop, vlan_id, rib_priority)| { - StaticRouteKey { - prefix, - nexthop, - vlan_id, - rib_priority, - } - }) + .prop_map( + |(prefix, nexthop, vlan_id, rib_priority, nexthop_interface)| { + StaticRouteKey { + prefix, + nexthop, + vlan_id, + rib_priority, + nexthop_interface, + } + }, + ) } // Strategy for generating valid SocketAddr for BGP neighbor configuration @@ -317,7 +321,7 @@ proptest! { let mut once = route; once.prefix.unset_host_bits(); - let mut twice = once; + let mut twice = once.clone(); twice.prefix.unset_host_bits(); prop_assert_eq!( @@ -363,7 +367,7 @@ proptest! { /// Property: Normalization preserves nexthop, vlan_id, and rib_priority #[test] fn prop_static_route_key_normalization_preserves_fields(route in static_route_key_strategy()) { - let mut normalized = route; + let mut normalized = route.clone(); normalized.prefix.unset_host_bits(); prop_assert_eq!( @@ -397,6 +401,7 @@ proptest! { nexthop: IpAddr::V4(Ipv4Addr::from(nexthop)), vlan_id, rib_priority, + nexthop_interface: None, }; let route2 = StaticRouteKey { @@ -404,12 +409,13 @@ proptest! { nexthop: IpAddr::V4(Ipv4Addr::from(nexthop)), vlan_id, rib_priority, + nexthop_interface: None, }; - let mut norm1 = route1; + let mut norm1 = route1.clone(); norm1.prefix.unset_host_bits(); - let mut norm2 = route2; + let mut norm2 = route2.clone(); norm2.prefix.unset_host_bits(); // If the normalized prefixes are the same, the entire routes should be equal @@ -425,10 +431,10 @@ proptest! { /// (normalized routes can be safely used in BTreeSet) #[test] fn prop_static_route_key_ord_consistency(route1 in static_route_key_strategy(), route2 in static_route_key_strategy()) { - let mut norm1 = route1; + let mut norm1 = route1.clone(); norm1.prefix.unset_host_bits(); - let mut norm2 = route2; + let mut norm2 = route2.clone(); norm2.prefix.unset_host_bits(); // If two normalized routes are equal, their ordering should be Equal @@ -441,10 +447,10 @@ proptest! { // Ordering should be consistent with equality if norm1 < norm2 { - prop_assert_ne!(norm1, norm2, "Less-than routes should not be equal"); + prop_assert_ne!(&norm1, &norm2, "Less-than routes should not be equal"); } if norm1 > norm2 { - prop_assert_ne!(norm1, norm2, "Greater-than routes should not be equal"); + prop_assert_ne!(&norm1, &norm2, "Greater-than routes should not be equal"); } } diff --git a/rdb/src/test.rs b/rdb/src/test.rs index 8e4b27b35..76eae53ce 100644 --- a/rdb/src/test.rs +++ b/rdb/src/test.rs @@ -101,6 +101,7 @@ impl Drop for TestDb { /// StaticRouteKey { /// prefix: Prefix::V4(Prefix4::new(Ipv4Addr::new(10, 0, 0, 0), 24)), /// nexthop: IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)), +/// nexthop_interface: None, /// vlan_id: None, /// rib_priority: 0, /// }, diff --git a/rdb/src/types.rs b/rdb/src/types.rs index eb3d0efb2..48be0866e 100644 --- a/rdb/src/types.rs +++ b/rdb/src/types.rs @@ -150,7 +150,7 @@ impl From for Path { fn from(value: StaticRouteKey) -> Self { Self { nexthop: value.nexthop, - nexthop_interface: None, // Static routes don't use interface binding + nexthop_interface: value.nexthop_interface, vlan_id: value.vlan_id, rib_priority: value.rib_priority, shutdown: false, @@ -251,7 +251,6 @@ impl Ord for BgpPathProperties { } #[derive( - Copy, Clone, PartialEq, Eq, @@ -265,6 +264,7 @@ impl Ord for BgpPathProperties { pub struct StaticRouteKey { pub prefix: Prefix, pub nexthop: IpAddr, + pub nexthop_interface: Option, pub vlan_id: Option, pub rib_priority: u8, } @@ -273,9 +273,10 @@ impl Display for StaticRouteKey { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!( f, - "[prefix={}, nexthop={}, vlan_id={}, rib_priority={}]", + "[prefix={}, nexthop={}, nexthop_interface={}, vlan_id={}, rib_priority={}]", self.prefix, self.nexthop, + self.nexthop_interface.as_deref().unwrap_or("none"), self.vlan_id.unwrap_or(0), self.rib_priority ) From eee9be5c8984d4ecba62ec46fbed7c2ddeecea4c Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Thu, 12 Feb 2026 17:30:23 -0700 Subject: [PATCH 09/58] Add graceful handler for enforce-first-as change Add handling for changes to enforce-first-as that doesn't involve a hard reset of the session. When enabled, walk the RIB and remove paths from the peer that fail the first-as test. When disabled, send a route-refresh to the peer so we can re-learn any routes we may have filtered out. Fixes: #620 --- bgp/src/session.rs | 54 ++++++++-- bgp/src/test.rs | 205 ++++++++++++++++++++++++++++++++++++ rdb/src/db.rs | 253 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 501 insertions(+), 11 deletions(-) diff --git a/bgp/src/session.rs b/bgp/src/session.rs index 7f4d327ae..c58de2b7a 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -556,6 +556,10 @@ pub enum AdminEvent { /// Fires when we need to re-send our routes to the peer. ReAdvertiseRoutes(Afi), + + /// enforce_first_as was just enabled. Walk the RIB and remove + /// paths from this peer that fail the first-AS check. + EnforceFirstAsEnabled, } impl AdminEvent { @@ -579,6 +583,7 @@ impl AdminEvent { Afi::Ipv4 => "re-advertise routes (ipv4 unicast)", Afi::Ipv6 => "re-advertise routes (ipv6 unicast)", }, + AdminEvent::EnforceFirstAsEnabled => "enforce first-as enabled", } } } @@ -2353,7 +2358,8 @@ impl SessionRunner { | AdminEvent::ExportPolicyChanged(_) | AdminEvent::CheckerChanged(_) | AdminEvent::SendRouteRefresh(_) - | AdminEvent::ReAdvertiseRoutes(_) => { + | AdminEvent::ReAdvertiseRoutes(_) + | AdminEvent::EnforceFirstAsEnabled => { let title = admin_event.title(); session_log_lite!( self, @@ -2598,7 +2604,8 @@ impl SessionRunner { | AdminEvent::ExportPolicyChanged(_) | AdminEvent::CheckerChanged(_) | AdminEvent::SendRouteRefresh(_) - | AdminEvent::ReAdvertiseRoutes(_) => { + | AdminEvent::ReAdvertiseRoutes(_) + | AdminEvent::EnforceFirstAsEnabled => { let title = admin_event.title(); session_log_lite!( self, @@ -2958,7 +2965,8 @@ impl SessionRunner { | AdminEvent::ExportPolicyChanged(_) | AdminEvent::CheckerChanged(_) | AdminEvent::SendRouteRefresh(_) - | AdminEvent::ReAdvertiseRoutes(_) => { + | AdminEvent::ReAdvertiseRoutes(_) + | AdminEvent::EnforceFirstAsEnabled => { let title = admin_event.title(); session_log_lite!( self, @@ -3330,7 +3338,8 @@ impl SessionRunner { | AdminEvent::ExportPolicyChanged(_) | AdminEvent::CheckerChanged(_) | AdminEvent::SendRouteRefresh(_) - | AdminEvent::ReAdvertiseRoutes(_) => { + | AdminEvent::ReAdvertiseRoutes(_) + | AdminEvent::EnforceFirstAsEnabled => { let title = admin_event.title(); session_log!( self, @@ -3906,7 +3915,8 @@ impl SessionRunner { | AdminEvent::CheckerChanged(_) | AdminEvent::ManualStart | AdminEvent::SendRouteRefresh(_) - | AdminEvent::ReAdvertiseRoutes(_) => { + | AdminEvent::ReAdvertiseRoutes(_) + | AdminEvent::EnforceFirstAsEnabled => { let title = admin_event.title(); session_log!( self, @@ -4431,7 +4441,8 @@ impl SessionRunner { | AdminEvent::CheckerChanged(_) | AdminEvent::ManualStart | AdminEvent::SendRouteRefresh(_) - | AdminEvent::ReAdvertiseRoutes(_) => { + | AdminEvent::ReAdvertiseRoutes(_) + | AdminEvent::EnforceFirstAsEnabled => { let title = admin_event.title(); collision_log!( self, @@ -5281,7 +5292,8 @@ impl SessionRunner { | AdminEvent::CheckerChanged(_) | AdminEvent::ManualStart | AdminEvent::SendRouteRefresh(_) - | AdminEvent::ReAdvertiseRoutes(_) => { + | AdminEvent::ReAdvertiseRoutes(_) + | AdminEvent::EnforceFirstAsEnabled => { let title = admin_event.title(); collision_log!( self, @@ -6459,6 +6471,17 @@ impl SessionRunner { FsmState::Established(pc) } + AdminEvent::EnforceFirstAsEnabled => { + self.db.remove_bgp_peer_paths_where( + &self.peer_id(), + |bgp| match bgp.as_path.first() { + Some(&first_as) => first_as != bgp.origin_as, + None => true, + }, + ); + FsmState::Established(pc) + } + AdminEvent::SendRouteRefresh(af) => { self.db.mark_bgp_peer_stale( self.peer_id(), @@ -8896,11 +8919,20 @@ impl SessionRunner { } if current.enforce_first_as != info.enforce_first_as { + let was_enabled = current.enforce_first_as; current.enforce_first_as = info.enforce_first_as; - // XXX: handle more gracefully. - // disabling = send route refresh - // enabling = run rib walker + delete paths failing check - reset_needed = true; + if !was_enabled { + // enabled: walk the RIB and remove paths that + // fail the first-AS check. + self.event_tx + .send(FsmEvent::Admin(AdminEvent::EnforceFirstAsEnabled)) + .map_err(|e| Error::EventSend(e.to_string()))?; + } else { + // disabled: request a route refresh so the peer + // re-advertises routes we previously rejected. + refresh_needed4 = true; + refresh_needed6 = true; + } } if current.vlan_id != info.vlan_id { diff --git a/bgp/src/test.rs b/bgp/src/test.rs index b960c786b..49b36a0b4 100644 --- a/bgp/src/test.rs +++ b/bgp/src/test.rs @@ -4344,3 +4344,208 @@ fn test_unnumbered_interface_lifecycle() { disp1.shutdown(); disp2.shutdown(); } + +/// Test that toggling `enforce_first_as` is handled gracefully without +/// resetting the BGP session. +/// +/// This test verifies that: +/// 1. Enabling enforce_first_as keeps the session Established (no reset) +/// 2. Routes that pass the first-AS check are preserved +/// 3. Disabling enforce_first_as keeps the session Established +/// 4. A route refresh is triggered (routes re-arrive after disable) +#[test] +fn test_enforce_first_as_graceful_toggle() { + let r1_addr: SocketAddr = sockaddr!(&format!("127.0.0.14:{TEST_BGP_PORT}")); + let r2_addr: SocketAddr = sockaddr!(&format!("127.0.0.15:{TEST_BGP_PORT}")); + + let _ip_guard = ensure_loop_ips(&[r1_addr.ip(), r2_addr.ip()]); + + let prefix_a = ip!("10.10.0.0/24"); + let prefix_b = ip!("10.11.0.0/24"); + + let r1_peer_config = PeerConfig { + name: "r2".into(), + group: String::new(), + host: r2_addr, + hold_time: 6, + idle_hold_time: 0, + delay_open: 0, + connect_retry: 1, + keepalive: 3, + resolution: 100, + }; + let r1_session_info = SessionInfo::from_peer_config(&r1_peer_config); + + let r2_peer_config = PeerConfig { + name: "r1".into(), + group: String::new(), + host: r1_addr, + hold_time: 6, + idle_hold_time: 0, + delay_open: 0, + connect_retry: 1, + keepalive: 3, + resolution: 100, + }; + // Start with enforce_first_as disabled on r2 + let r2_session_info = SessionInfo::from_peer_config(&r2_peer_config); + assert!( + !r2_session_info.enforce_first_as, + "enforce_first_as should default to false" + ); + + let routers = vec![ + LogicalRouter { + name: "r1".to_string(), + asn: Asn::FourOctet(4200000001), + id: 1, + listen_addr: r1_addr, + bind_addr: Some(r1_addr), + neighbors: vec![NeighborConfig { + peer_name: "r2".to_string(), + remote_host: r2_addr, + session_info: r1_session_info, + }], + }, + LogicalRouter { + name: "r2".to_string(), + asn: Asn::FourOctet(4200000002), + id: 2, + listen_addr: r2_addr, + bind_addr: Some(r2_addr), + neighbors: vec![NeighborConfig { + peer_name: "r1".to_string(), + remote_host: r1_addr, + session_info: r2_session_info, + }], + }, + ]; + + let (test_routers, _ip_guard2) = test_setup::< + BgpConnectionTcp, + BgpListenerTcp, + >("enforce_first_as", &routers); + + let r1 = &test_routers[0]; + let r2 = &test_routers[1]; + + let r1_session = r1 + .router + .get_session(r2_addr.ip()) + .expect("get r1->r2 session"); + let r2_session = r2 + .router + .get_session(r1_addr.ip()) + .expect("get r2->r1 session"); + wait_for_eq!(r1_session.state(), FsmStateKind::Established); + wait_for_eq!(r2_session.state(), FsmStateKind::Established); + + // Originate routes from r1 + r1.router + .create_origin4(vec![prefix_a, prefix_b]) + .expect("originate prefixes"); + + let prefix_a_rdb = Prefix::V4(cidr!("10.10.0.0/24")); + let prefix_b_rdb = Prefix::V4(cidr!("10.11.0.0/24")); + + // Wait for routes to arrive at r2 + wait_for!( + !r2.router.db.get_prefix_paths(&prefix_a_rdb).is_empty() + && !r2.router.db.get_prefix_paths(&prefix_b_rdb).is_empty(), + "r2 should receive both prefixes" + ); + + // Verify routes arrived with correct AS path + for prefix_rdb in [&prefix_a_rdb, &prefix_b_rdb] { + let paths = r2.router.db.get_prefix_paths(prefix_rdb); + assert_eq!(paths.len(), 1); + let bgp = paths[0].bgp.as_ref().unwrap(); + assert_eq!(bgp.origin_as, 4200000001); + assert_eq!(bgp.as_path, vec![4200000001]); + } + + // ===== Enable enforce_first_as on r2 ===== + // Routes from r1 have as_path[0] == origin_as (4200000001), + // so they all pass the first-AS check. No routes should be + // removed and the session must stay Established. + let r2_info_enabled = { + let mut info = SessionInfo::from_peer_config(&r2_peer_config); + info.enforce_first_as = true; + info + }; + r2.router + .update_session(r2_peer_config.clone(), r2_info_enabled) + .expect("enable enforce_first_as on r2"); + + // Session must stay Established for longer than hold_time + // (verifies keepalives continue and no reset occurs). + let start = Instant::now(); + while start.elapsed() < ESTABLISHED_VERIFICATION { + assert_eq!( + r2_session.state(), + FsmStateKind::Established, + "session should stay Established after enabling enforce_first_as" + ); + assert_eq!( + r1_session.state(), + FsmStateKind::Established, + "remote session should also stay Established" + ); + sleep(Duration::from_millis(100)); + } + + // Routes should still be present (they pass the first-AS check) + for prefix_rdb in [&prefix_a_rdb, &prefix_b_rdb] { + let paths = r2.router.db.get_prefix_paths(prefix_rdb); + assert_eq!( + paths.len(), + 1, + "routes passing first-AS check should be preserved" + ); + } + + // ===== Disable enforce_first_as on r2 ===== + // This should trigger a route refresh (routes re-arrive from + // peer). Session must stay Established. + let r2_info_disabled = { + let mut info = SessionInfo::from_peer_config(&r2_peer_config); + info.enforce_first_as = false; + info + }; + r2.router + .update_session(r2_peer_config.clone(), r2_info_disabled) + .expect("disable enforce_first_as on r2"); + + // Session must stay Established for longer than hold_time + let start = Instant::now(); + while start.elapsed() < ESTABLISHED_VERIFICATION { + assert_eq!( + r2_session.state(), + FsmStateKind::Established, + "session should stay Established after disabling enforce_first_as" + ); + assert_eq!( + r1_session.state(), + FsmStateKind::Established, + "remote session should also stay Established" + ); + sleep(Duration::from_millis(100)); + } + + // Routes should still be present (route refresh re-delivered + // them) + for prefix_rdb in [&prefix_a_rdb, &prefix_b_rdb] { + let paths = r2.router.db.get_prefix_paths(prefix_rdb); + assert_eq!( + paths.len(), + 1, + "routes should be present after route refresh" + ); + let bgp = paths[0].bgp.as_ref().unwrap(); + assert_eq!(bgp.origin_as, 4200000001); + assert_eq!(bgp.as_path, vec![4200000001]); + } + + r1.shutdown(); + r2.shutdown(); +} diff --git a/rdb/src/db.rs b/rdb/src/db.rs index 31cb4d89e..ec2ef463b 100644 --- a/rdb/src/db.rs +++ b/rdb/src/db.rs @@ -1279,6 +1279,40 @@ impl Db { self.notify(pcn); } + /// Remove BGP paths from a specific peer that match an additional + /// predicate. Used for policy changes like enforce-first-AS that + /// need to selectively remove a subset of a peer's paths. + pub fn remove_bgp_peer_paths_where(&self, peer: &PeerId, predicate: F) + where + F: Fn(&BgpPathProperties) -> bool, + { + let all_prefixes4: Vec<_> = self + .full_rib(Some(AddressFamily::Ipv4)) + .keys() + .copied() + .collect(); + let all_prefixes6: Vec<_> = self + .full_rib(Some(AddressFamily::Ipv6)) + .keys() + .copied() + .collect(); + + let mut all_prefixes = Vec::new(); + all_prefixes.extend(all_prefixes4); + all_prefixes.extend(all_prefixes6); + + self.remove_path_for_prefixes(&all_prefixes, |path: &Path| match path + .bgp + { + Some(ref bgp) => bgp.peer == *peer && predicate(bgp), + None => false, + }); + + let mut pcn = PrefixChangeNotification::default(); + pcn.changed.extend(all_prefixes); + self.notify(pcn); + } + // wrapper for remove_bgp_prefixes to handle the "all routes" corner case. // e.g. when peer is deleted or exits Established state pub fn remove_bgp_prefixes_from_peer(&self, peer: &PeerId) { @@ -2564,6 +2598,225 @@ mod test { assert!(routes.is_empty()); } + #[test] + fn test_remove_bgp_peer_paths_where() { + use crate::{ + BgpPathProperties, DEFAULT_RIB_PRIORITY_BGP, Path, PeerId, Prefix, + Prefix4, Prefix6, + }; + + let log = init_file_logger("rib.log"); + let db = crate::test::get_test_db("remove_bgp_peer_paths_where", log) + .expect("create db"); + + let peer_ip = IpAddr::from_str("203.0.113.1").unwrap(); + let other_ip = IpAddr::from_str("203.0.113.2").unwrap(); + let peer = PeerId::Ip(peer_ip); + let other_peer = PeerId::Ip(other_ip); + + let p4 = Prefix::from("10.0.0.0/24".parse::().unwrap()); + let p6 = Prefix::from("2001:db8::/32".parse::().unwrap()); + + // Path where first AS matches origin_as (passes first-AS + // check) + let good_path = Path { + nexthop: peer_ip, + nexthop_interface: None, + rib_priority: DEFAULT_RIB_PRIORITY_BGP, + shutdown: false, + bgp: Some(BgpPathProperties { + origin_as: 65001, + peer: peer.clone(), + id: 1, + med: None, + local_pref: Some(100), + as_path: vec![65001, 65002], + stale: None, + }), + vlan_id: None, + }; + + // Path where first AS does NOT match origin_as (fails + // first-AS check) + let bad_path = Path { + nexthop: peer_ip, + nexthop_interface: None, + rib_priority: DEFAULT_RIB_PRIORITY_BGP, + shutdown: false, + bgp: Some(BgpPathProperties { + origin_as: 65001, + peer: peer.clone(), + id: 1, + med: None, + local_pref: Some(100), + as_path: vec![65099, 65001], + stale: None, + }), + vlan_id: None, + }; + + // Path from a different peer (should never be removed) + let other_peer_path = Path { + nexthop: other_ip, + nexthop_interface: None, + rib_priority: DEFAULT_RIB_PRIORITY_BGP, + shutdown: false, + bgp: Some(BgpPathProperties { + origin_as: 65002, + peer: other_peer.clone(), + id: 2, + med: None, + local_pref: Some(100), + as_path: vec![65099, 65002], + stale: None, + }), + vlan_id: None, + }; + + // Add paths: good_path on p4, bad_path on p6, + // other_peer_path on p4 + db.add_bgp_prefixes(&[p4], good_path.clone()); + db.add_bgp_prefixes(&[p6], bad_path.clone()); + db.add_bgp_prefixes(&[p4], other_peer_path.clone()); + + // Verify initial state: p4 has 2 paths, p6 has 1 path + assert_eq!(db.get_prefix_paths(&p4).len(), 2); + assert_eq!(db.get_prefix_paths(&p6).len(), 1); + + // Remove paths from `peer` where first AS != origin_as + db.remove_bgp_peer_paths_where(&peer, |bgp| { + match bgp.as_path.first() { + Some(&first_as) => first_as != bgp.origin_as, + None => true, + } + }); + + // p4: good_path (passes check) + other_peer_path (different + // peer) should remain + let p4_paths = db.get_prefix_paths(&p4); + assert_eq!( + p4_paths.len(), + 2, + "good path and other peer path should remain on p4" + ); + + // p6: bad_path (fails check, same peer) should be removed + let p6_paths = db.get_prefix_paths(&p6); + assert!(p6_paths.is_empty(), "bad path should be removed from p6"); + + // Verify other_peer_path is still there even though it + // would fail the predicate — it's from a different peer + let has_other = p4_paths + .iter() + .any(|p| p.bgp.as_ref().unwrap().peer == other_peer); + assert!(has_other, "other peer's path should be untouched"); + } + + #[test] + fn test_remove_bgp_peer_paths_where_empty_as_path() { + use crate::{ + BgpPathProperties, DEFAULT_RIB_PRIORITY_BGP, Path, PeerId, Prefix, + Prefix4, + }; + + let log = init_file_logger("rib.log"); + let db = + crate::test::get_test_db("remove_bgp_peer_paths_where_empty", log) + .expect("create db"); + + let peer_ip = IpAddr::from_str("203.0.113.1").unwrap(); + let peer = PeerId::Ip(peer_ip); + let prefix = Prefix::from("10.0.0.0/24".parse::().unwrap()); + + // Path with empty AS path (should be caught by the + // predicate) + let empty_as_path = Path { + nexthop: peer_ip, + nexthop_interface: None, + rib_priority: DEFAULT_RIB_PRIORITY_BGP, + shutdown: false, + bgp: Some(BgpPathProperties { + origin_as: 65001, + peer: peer.clone(), + id: 1, + med: None, + local_pref: Some(100), + as_path: vec![], + stale: None, + }), + vlan_id: None, + }; + + db.add_bgp_prefixes(&[prefix], empty_as_path); + assert_eq!(db.get_prefix_paths(&prefix).len(), 1); + + db.remove_bgp_peer_paths_where(&peer, |bgp| { + match bgp.as_path.first() { + Some(&first_as) => first_as != bgp.origin_as, + None => true, // empty path fails check + } + }); + + assert!( + db.get_prefix_paths(&prefix).is_empty(), + "path with empty AS path should be removed" + ); + } + + #[test] + fn test_remove_bgp_peer_paths_where_no_match() { + use crate::{ + BgpPathProperties, DEFAULT_RIB_PRIORITY_BGP, Path, PeerId, Prefix, + Prefix4, + }; + + let log = init_file_logger("rib.log"); + let db = crate::test::get_test_db( + "remove_bgp_peer_paths_where_nomatch", + log, + ) + .expect("create db"); + + let peer_ip = IpAddr::from_str("203.0.113.1").unwrap(); + let peer = PeerId::Ip(peer_ip); + let prefix = Prefix::from("10.0.0.0/24".parse::().unwrap()); + + // Path that passes the first-AS check + let good_path = Path { + nexthop: peer_ip, + nexthop_interface: None, + rib_priority: DEFAULT_RIB_PRIORITY_BGP, + shutdown: false, + bgp: Some(BgpPathProperties { + origin_as: 65001, + peer: peer.clone(), + id: 1, + med: None, + local_pref: Some(100), + as_path: vec![65001, 65002], + stale: None, + }), + vlan_id: None, + }; + + db.add_bgp_prefixes(&[prefix], good_path); + assert_eq!(db.get_prefix_paths(&prefix).len(), 1); + + // Predicate matches nothing — all paths pass the check + db.remove_bgp_peer_paths_where(&peer, |bgp| { + match bgp.as_path.first() { + Some(&first_as) => first_as != bgp.origin_as, + None => true, + } + }); + + assert_eq!( + db.get_prefix_paths(&prefix).len(), + 1, + "path passing first-AS check should remain" + ); + } + #[test] fn test_prefix6_from_str() { let prefix_str = "2001:db8::/32"; From 3eaf111a82ebee957fe87b290c62359e1bd18f43 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Thu, 12 Feb 2026 18:36:55 -0700 Subject: [PATCH 10/58] rdb: deduplicate v4/v6 dispatch patterns Consolidate paired address-family-specific methods into generic or AF-parameterized equivalents throughout the RIB database layer. Origin methods now use the Prefix enum for consistency with existing static routing methods. Rename BGP removal methods for clarity and fix over-approximate PCN notifications. --- bgp/src/router.rs | 155 ++++--- bgp/src/session.rs | 192 ++++++--- mgd/src/bgp_admin.rs | 57 ++- mgd/src/oxstats.rs | 6 +- rdb/src/db.rs | 995 +++++++++++++++++++------------------------ 5 files changed, 718 insertions(+), 687 deletions(-) diff --git a/bgp/src/router.rs b/bgp/src/router.rs index 8e9d49b08..323a097f1 100644 --- a/bgp/src/router.rs +++ b/bgp/src/router.rs @@ -20,7 +20,7 @@ use crate::{ unnumbered::UnnumberedManager, }; use mg_common::{lock, read_lock, write_lock}; -use rdb::{Asn, Db, Prefix4, Prefix6}; +use rdb::{AddressFamily, Asn, Db, Prefix4, Prefix6}; use rhai::AST; use slog::Logger; use std::{ @@ -465,45 +465,44 @@ impl Router { } pub fn create_origin4(&self, prefixes: Vec) -> Result<(), Error> { - let prefix4: Vec = prefixes - .iter() - .cloned() - .filter_map(|x| match x { - Prefix::V4(p4) => Some(p4), - Prefix::V6(_) => None, - }) - .collect(); - self.db.create_origin4(&prefix4)?; + self.db.create_origin(AddressFamily::Ipv4, &prefixes)?; // Skip network propagation if router is shutdown if !self.shutdown.load(Ordering::Acquire) { + let prefix4: Vec = prefixes + .into_iter() + .filter_map(|x| match x { + Prefix::V4(p4) => Some(p4), + _ => None, + }) + .collect(); self.announce_origin4(prefix4); } Ok(()) } pub fn set_origin4(&self, prefixes: Vec) -> Result<(), Error> { - let origin4 = self.db.get_origin4()?; - let current: BTreeSet<&Prefix4> = origin4.iter().collect(); - - let prefix4: Vec = prefixes - .iter() - .cloned() - .filter_map(|x| match x { - Prefix::V4(p4) => Some(p4), - Prefix::V6(_) => None, + let current = self.db.get_origin(Some(AddressFamily::Ipv4))?; + let current_set: BTreeSet<&Prefix> = current.iter().collect(); + let new_set: BTreeSet<&Prefix> = prefixes.iter().collect(); + + let to_withdraw: Vec = current_set + .difference(&new_set) + .filter_map(|p| match p { + Prefix::V4(p4) => Some(*p4), + _ => None, }) .collect(); - let new: BTreeSet<&Prefix4> = prefix4.iter().collect(); - - let to_withdraw: Vec = - current.difference(&new).map(|x| **x).collect(); - - let to_announce: Vec = - new.difference(¤t).map(|x| **x).collect(); + let to_announce: Vec = new_set + .difference(¤t_set) + .filter_map(|p| match p { + Prefix::V4(p4) => Some(*p4), + _ => None, + }) + .collect(); - self.db.set_origin4(&prefix4)?; + self.db.set_origin(AddressFamily::Ipv4, &prefixes)?; // Skip network propagation if router is shutdown if !self.shutdown.load(Ordering::Acquire) { @@ -514,13 +513,20 @@ impl Router { } pub fn clear_origin4(&self) -> Result<(), Error> { - let current = self.db.get_origin4()?; + let current = self.db.get_origin(Some(AddressFamily::Ipv4))?; // Skip network propagation if router is shutdown if !self.shutdown.load(Ordering::Acquire) { - self.withdraw_origin4(current); - } - self.db.clear_origin4()?; + let current4: Vec = current + .into_iter() + .filter_map(|p| match p { + Prefix::V4(p4) => Some(p4), + _ => None, + }) + .collect(); + self.withdraw_origin4(current4); + } + self.db.clear_origin(AddressFamily::Ipv4)?; Ok(()) } @@ -569,45 +575,44 @@ impl Router { } pub fn create_origin6(&self, prefixes: Vec) -> Result<(), Error> { - let prefix6: Vec = prefixes - .iter() - .cloned() - .filter_map(|x| match x { - Prefix::V6(p6) => Some(p6), - Prefix::V4(_) => None, - }) - .collect(); - self.db.create_origin6(&prefix6)?; + self.db.create_origin(AddressFamily::Ipv6, &prefixes)?; // Skip network propagation if router is shutdown if !self.shutdown.load(Ordering::Acquire) { + let prefix6: Vec = prefixes + .into_iter() + .filter_map(|x| match x { + Prefix::V6(p6) => Some(p6), + _ => None, + }) + .collect(); self.announce_origin6(prefix6); } Ok(()) } pub fn set_origin6(&self, prefixes: Vec) -> Result<(), Error> { - let origin6 = self.db.get_origin6()?; - let current: BTreeSet<&Prefix6> = origin6.iter().collect(); - - let prefix6: Vec = prefixes - .iter() - .cloned() - .filter_map(|x| match x { - Prefix::V6(p6) => Some(p6), - Prefix::V4(_) => None, + let current = self.db.get_origin(Some(AddressFamily::Ipv6))?; + let current_set: BTreeSet<&Prefix> = current.iter().collect(); + let new_set: BTreeSet<&Prefix> = prefixes.iter().collect(); + + let to_withdraw: Vec = current_set + .difference(&new_set) + .filter_map(|p| match p { + Prefix::V6(p6) => Some(*p6), + _ => None, }) .collect(); - let new: BTreeSet<&Prefix6> = prefix6.iter().collect(); - - let to_withdraw: Vec = - current.difference(&new).map(|x| **x).collect(); - - let to_announce: Vec = - new.difference(¤t).map(|x| **x).collect(); + let to_announce: Vec = new_set + .difference(¤t_set) + .filter_map(|p| match p { + Prefix::V6(p6) => Some(*p6), + _ => None, + }) + .collect(); - self.db.set_origin6(&prefix6)?; + self.db.set_origin(AddressFamily::Ipv6, &prefixes)?; // Skip network propagation if router is shutdown if !self.shutdown.load(Ordering::Acquire) { @@ -618,13 +623,20 @@ impl Router { } pub fn clear_origin6(&self) -> Result<(), Error> { - let current = self.db.get_origin6()?; + let current = self.db.get_origin(Some(AddressFamily::Ipv6))?; // Skip network propagation if router is shutdown if !self.shutdown.load(Ordering::Acquire) { - self.withdraw_origin6(current); - } - self.db.clear_origin6()?; + let current6: Vec = current + .into_iter() + .filter_map(|p| match p { + Prefix::V6(p6) => Some(p6), + _ => None, + }) + .collect(); + self.withdraw_origin6(current6); + } + self.db.clear_origin(AddressFamily::Ipv6)?; Ok(()) } @@ -737,7 +749,15 @@ impl Router { } fn announce_all(&self) -> Result<(), Error> { - let originated4 = self.db.get_origin4()?; + let originated4: Vec = self + .db + .get_origin(Some(AddressFamily::Ipv4))? + .into_iter() + .filter_map(|p| match p { + Prefix::V4(p4) => Some(p4), + _ => None, + }) + .collect(); if !originated4.is_empty() { slog::debug!( @@ -750,8 +770,15 @@ impl Router { read_lock!(self.fanout4).send_all(originated4, vec![]); } - // Also announce IPv6 originated routes - let originated6 = self.db.get_origin6()?; + let originated6: Vec = self + .db + .get_origin(Some(AddressFamily::Ipv6))? + .into_iter() + .filter_map(|p| match p { + Prefix::V6(p6) => Some(p6), + _ => None, + }) + .collect(); if !originated6.is_empty() { slog::debug!( diff --git a/bgp/src/session.rs b/bgp/src/session.rs index c58de2b7a..bd2c943f3 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -6031,7 +6031,7 @@ impl SessionRunner { // Collect the prefixes this router is originating. let originated4 = if pc.ipv4_unicast.negotiated() { - match self.db.get_origin4() { + match self.db.get_origin(Some(AddressFamily::Ipv4)) { Ok(value) => value, Err(e) => { //TODO possible death loop. Should we just panic here? @@ -6050,7 +6050,7 @@ impl SessionRunner { }; let originated6 = if pc.ipv6_unicast.negotiated() { - match self.db.get_origin6() { + match self.db.get_origin(Some(AddressFamily::Ipv6)) { Ok(value) => value, Err(e) => { //TODO possible death loop. Should we just panic here? @@ -6096,7 +6096,15 @@ impl SessionRunner { // is originating. if !originated4.is_empty() && let Err(e) = self.send_update( - RouteUpdate::V4(RouteUpdate4::Announce(originated4)), + RouteUpdate::V4(RouteUpdate4::Announce( + originated4 + .into_iter() + .filter_map(|p| match p { + Prefix::V4(p4) => Some(p4), + _ => None, + }) + .collect(), + )), &pc, &ShaperApplication::Current, ) @@ -6114,7 +6122,15 @@ impl SessionRunner { // Send IPv6 Unicast prefixes using MP-BGP encoding if !originated6.is_empty() && let Err(e) = self.send_update( - RouteUpdate::V6(RouteUpdate6::Announce(originated6)), + RouteUpdate::V6(RouteUpdate6::Announce( + originated6 + .into_iter() + .filter_map(|p| match p { + Prefix::V6(p6) => Some(p6), + _ => None, + }) + .collect(), + )), &pc, &ShaperApplication::Current, ) @@ -6139,7 +6155,7 @@ impl SessionRunner { sa: &ShaperApplication, ) -> anyhow::Result<()> { // Get originated IPv4 routes - let originated4 = match self.db.get_origin4() { + let originated4 = match self.db.get_origin(Some(AddressFamily::Ipv4)) { Ok(originated) => originated, Err(e) => { anyhow::bail!("failed to get originated IPv4 from db: {e}"); @@ -6147,15 +6163,22 @@ impl SessionRunner { }; if !originated4.is_empty() { + let p4: Vec = originated4 + .into_iter() + .filter_map(|p| match p { + Prefix::V4(p4) => Some(p4), + _ => None, + }) + .collect(); self.send_update( - RouteUpdate::V4(RouteUpdate4::Announce(originated4)), + RouteUpdate::V4(RouteUpdate4::Announce(p4)), pc, sa, )?; } // Get originated IPv6 routes - let originated6 = match self.db.get_origin6() { + let originated6 = match self.db.get_origin(Some(AddressFamily::Ipv6)) { Ok(originated) => originated, Err(e) => { anyhow::bail!("failed to get originated IPv6 from db: {e}"); @@ -6163,8 +6186,15 @@ impl SessionRunner { }; if !originated6.is_empty() { + let p6: Vec = originated6 + .into_iter() + .filter_map(|p| match p { + Prefix::V6(p6) => Some(p6), + _ => None, + }) + .collect(); self.send_update( - RouteUpdate::V6(RouteUpdate6::Announce(originated6)), + RouteUpdate::V6(RouteUpdate6::Announce(p6)), pc, sa, )?; @@ -6258,7 +6288,10 @@ impl SessionRunner { AdminEvent::ExportPolicyChanged(previous) => { match previous { ImportExportPolicy::V4(previous4) => { - let originated = match self.db.get_origin4() { + let originated = match self + .db + .get_origin(Some(AddressFamily::Ipv4)) + { Ok(value) => value, Err(e) => { session_log!( @@ -6274,7 +6307,7 @@ impl SessionRunner { // Determine which routes to announce/withdraw based on policy change let session = lock!(self.session); - let originated_before: BTreeSet = + let originated_before: BTreeSet = match previous4 { ImportExportPolicy4::NoFiltering => { originated.iter().cloned().collect() @@ -6283,12 +6316,17 @@ impl SessionRunner { originated .iter() .cloned() - .filter(|x| list.contains(x)) + .filter(|x| match x { + Prefix::V4(p4) => { + list.contains(p4) + } + _ => false, + }) .collect() } }; - let originated_after: BTreeSet = + let originated_after: BTreeSet = match session .ipv4_unicast .as_ref() @@ -6302,7 +6340,12 @@ impl SessionRunner { originated .clone() .into_iter() - .filter(|x| list.contains(x)) + .filter(|x| match x { + Prefix::V4(p4) => { + list.contains(p4) + } + _ => false, + }) .collect() } }; @@ -6310,12 +6353,18 @@ impl SessionRunner { let to_withdraw: Vec = originated_before .difference(&originated_after) - .cloned() + .filter_map(|p| match p { + Prefix::V4(p4) => Some(*p4), + _ => None, + }) .collect(); let to_announce: Vec = originated_after .difference(&originated_before) - .cloned() + .filter_map(|p| match p { + Prefix::V4(p4) => Some(*p4), + _ => None, + }) .collect(); // Per RFC 7606, send announcements and withdrawals as @@ -6361,7 +6410,10 @@ impl SessionRunner { FsmState::Established(pc) } ImportExportPolicy::V6(previous6) => { - let originated = match self.db.get_origin6() { + let originated = match self + .db + .get_origin(Some(AddressFamily::Ipv6)) + { Ok(value) => value, Err(e) => { session_log!( @@ -6377,7 +6429,7 @@ impl SessionRunner { // Determine which routes to announce/withdraw based on policy change let session = lock!(self.session); - let originated_before: BTreeSet = + let originated_before: BTreeSet = match previous6 { ImportExportPolicy6::NoFiltering => { originated.iter().cloned().collect() @@ -6386,12 +6438,17 @@ impl SessionRunner { originated .iter() .cloned() - .filter(|x| list.contains(x)) + .filter(|x| match x { + Prefix::V6(p6) => { + list.contains(p6) + } + _ => false, + }) .collect() } }; - let originated_after: BTreeSet = + let originated_after: BTreeSet = match session .ipv6_unicast .as_ref() @@ -6405,7 +6462,12 @@ impl SessionRunner { originated .clone() .into_iter() - .filter(|x| list.contains(x)) + .filter(|x| match x { + Prefix::V6(p6) => { + list.contains(p6) + } + _ => false, + }) .collect() } }; @@ -6413,12 +6475,18 @@ impl SessionRunner { let to_withdraw: Vec = originated_before .difference(&originated_after) - .cloned() + .filter_map(|p| match p { + Prefix::V6(p6) => Some(*p6), + _ => None, + }) .collect(); let to_announce: Vec = originated_after .difference(&originated_before) - .cloned() + .filter_map(|p| match p { + Prefix::V6(p6) => Some(*p6), + _ => None, + }) .collect(); // Per RFC 7606, send announcements and withdrawals as @@ -6472,13 +6540,7 @@ impl SessionRunner { } AdminEvent::EnforceFirstAsEnabled => { - self.db.remove_bgp_peer_paths_where( - &self.peer_id(), - |bgp| match bgp.as_path.first() { - Some(&first_as) => first_as != bgp.origin_as, - None => true, - }, - ); + self.db.enforce_first_as(&self.peer_id()); FsmState::Established(pc) } @@ -7900,7 +7962,7 @@ impl SessionRunner { } // remove peer prefixes from db - self.db.remove_bgp_prefixes_from_peer(&self.peer_id()); + self.db.remove_all_prefixes_from_bgp_peer(&self.peer_id()); } /// Exit the established state into Idle. @@ -8328,7 +8390,7 @@ impl SessionRunner { return Ok(()); } - let originated = match self.db.get_origin4() { + let originated = match self.db.get_origin(Some(AddressFamily::Ipv4)) { Ok(value) => value, Err(e) => { session_log!( @@ -8343,9 +8405,17 @@ impl SessionRunner { } }; - if !originated.is_empty() { + let originated4: Vec = originated + .into_iter() + .filter_map(|p| match p { + Prefix::V4(p4) => Some(p4), + _ => None, + }) + .collect(); + + if !originated4.is_empty() { self.send_update( - RouteUpdate::V4(RouteUpdate4::Announce(originated)), + RouteUpdate::V4(RouteUpdate4::Announce(originated4)), pc, &ShaperApplication::Current, )?; @@ -8361,7 +8431,7 @@ impl SessionRunner { return Ok(()); } - let originated = match self.db.get_origin6() { + let originated = match self.db.get_origin(Some(AddressFamily::Ipv6)) { Ok(value) => value, Err(e) => { session_log!( @@ -8376,9 +8446,17 @@ impl SessionRunner { } }; - if !originated.is_empty() { + let originated6: Vec = originated + .into_iter() + .filter_map(|p| match p { + Prefix::V6(p6) => Some(p6), + _ => None, + }) + .collect(); + + if !originated6.is_empty() { self.send_update( - RouteUpdate::V6(RouteUpdate6::Announce(originated)), + RouteUpdate::V6(RouteUpdate6::Announce(originated6)), pc, &ShaperApplication::Current, )?; @@ -8418,7 +8496,7 @@ impl SessionRunner { /// Update this router's RIB based on an update message from a peer. fn update_rib(&self, update: &UpdateMessage, pc: &PeerConnection) { - let originated4 = match self.db.get_origin4() { + let originated4 = match self.db.get_origin(Some(AddressFamily::Ipv4)) { Ok(value) => value, Err(e) => { session_log!( @@ -8435,19 +8513,22 @@ impl SessionRunner { let withdrawn: Vec = update .withdrawn .iter() - .filter(|p| !originated4.contains(p) && p.valid_for_rib()) + .filter(|p| { + !originated4.contains(&Prefix::V4(**p)) && p.valid_for_rib() + }) .copied() .map(Prefix::V4) .collect(); - self.db.remove_bgp_prefixes(&withdrawn, &self.peer_id()); + self.db + .remove_prefixes_from_bgp_peer(&withdrawn, &self.peer_id()); if let Some(nexthop) = update.nexthop4().map(IpAddr::V4) { let nlri: Vec = update .nlri .iter() .filter(|p| { - !originated4.contains(p) + !originated4.contains(&Prefix::V4(**p)) && p.valid_for_rib() && !self.prefix_via_self(Prefix::V4(**p), nexthop) }) @@ -8517,7 +8598,7 @@ impl SessionRunner { .nlri .iter() .filter(|p| { - !originated4.contains(p) + !originated4.contains(&Prefix::V4(**p)) && p.valid_for_rib() && !self.prefix_via_self( Prefix::V4(**p), @@ -8560,7 +8641,10 @@ impl SessionRunner { } } MpReachNlri::Ipv6Unicast(reach6) => { - let originated6 = match self.db.get_origin6() { + let originated6 = match self + .db + .get_origin(Some(AddressFamily::Ipv6)) + { Ok(value) => value, Err(e) => { session_log!( @@ -8601,7 +8685,7 @@ impl SessionRunner { .nlri .iter() .filter(|p| { - !originated6.contains(p) + !originated6.contains(&Prefix::V6(**p)) && p.valid_for_rib() && !self .prefix_via_self(Prefix::V6(**p), nexthop6) @@ -8650,17 +8734,23 @@ impl SessionRunner { .withdrawn .iter() .filter(|p| { - !originated4.contains(p) && p.valid_for_rib() + !originated4.contains(&Prefix::V4(**p)) + && p.valid_for_rib() }) .copied() .map(Prefix::V4) .collect(); - self.db - .remove_bgp_prefixes(&mp_withdrawn4, &self.peer_id()); + self.db.remove_prefixes_from_bgp_peer( + &mp_withdrawn4, + &self.peer_id(), + ); } MpUnreachNlri::Ipv6Unicast(unreach6) => { - let originated6 = match self.db.get_origin6() { + let originated6 = match self + .db + .get_origin(Some(AddressFamily::Ipv6)) + { Ok(value) => value, Err(e) => { session_log!( @@ -8678,13 +8768,17 @@ impl SessionRunner { .withdrawn .iter() .filter(|p| { - !originated6.contains(p) && p.valid_for_rib() + !originated6.contains(&Prefix::V6(**p)) + && p.valid_for_rib() }) .copied() .map(Prefix::V6) .collect(); - self.db.remove_bgp_prefixes(&withdrawn6, &self.peer_id()); + self.db.remove_prefixes_from_bgp_peer( + &withdrawn6, + &self.peer_id(), + ); } } } diff --git a/mgd/src/bgp_admin.rs b/mgd/src/bgp_admin.rs index 1c0125def..b79e7c61c 100644 --- a/mgd/src/bgp_admin.rs +++ b/mgd/src/bgp_admin.rs @@ -784,17 +784,25 @@ pub async fn read_origin4( ) -> Result, HttpError> { let rq = request.into_inner(); let ctx = ctx.context(); - let mut originated = get_router!(ctx, rq.asn)? + let originated = get_router!(ctx, rq.asn)? .db - .get_origin4() + .get_origin(Some(AddressFamily::Ipv4)) .map_err(|e| HttpError::for_internal_error(e.to_string()))?; + let mut prefixes: Vec = originated + .into_iter() + .filter_map(|p| match p { + Prefix::V4(p4) => Some(p4), + _ => None, + }) + .collect(); + // stable output order for clients - originated.sort(); + prefixes.sort(); Ok(HttpResponseOk(Origin4 { asn: rq.asn, - prefixes: originated, + prefixes, })) } @@ -856,17 +864,25 @@ pub async fn read_origin6( ) -> Result, HttpError> { let rq = request.into_inner(); let ctx = ctx.context(); - let mut originated = get_router!(ctx, rq.asn)? + let originated = get_router!(ctx, rq.asn)? .db - .get_origin6() + .get_origin(Some(AddressFamily::Ipv6)) .map_err(|e| HttpError::for_internal_error(e.to_string()))?; + let mut prefixes: Vec = originated + .into_iter() + .filter_map(|p| match p { + Prefix::V6(p6) => Some(p6), + _ => None, + }) + .collect(); + // stable output order for clients - originated.sort(); + prefixes.sort(); Ok(HttpResponseOk(Origin6 { asn: rq.asn, - prefixes: originated, + prefixes, })) } @@ -911,7 +927,7 @@ pub async fn get_exported( let rq = request.into_inner(); let ctx = ctx.context(); let r = get_router!(ctx, rq.asn)?.clone(); - let orig4 = r.db.get_origin4().map_err(|e| { + let orig4 = r.db.get_origin(Some(AddressFamily::Ipv4)).map_err(|e| { HttpError::for_internal_error(format!("error getting origin: {e}")) })?; let neighs = r.db.get_bgp_neighbors().map_err(|e| { @@ -933,8 +949,7 @@ pub async fn get_exported( continue; } - let mut orig_routes: Vec = - orig4.clone().iter().map(|p| Prefix::from(*p)).collect(); + let mut orig_routes: Vec = orig4.clone(); // Combine per-AF export policies into legacy format for filtering let allow_export = ImportExportPolicyV1::from_per_af_policies( @@ -967,10 +982,10 @@ pub async fn get_exported_v2( let r = get_router!(ctx, rq.asn)?.clone(); // Get originated prefixes for both address families - let orig4 = r.db.get_origin4().map_err(|e| { + let orig4 = r.db.get_origin(Some(AddressFamily::Ipv4)).map_err(|e| { HttpError::for_internal_error(format!("error getting origin4: {e}")) })?; - let orig6 = r.db.get_origin6().map_err(|e| { + let orig6 = r.db.get_origin(Some(AddressFamily::Ipv6)).map_err(|e| { HttpError::for_internal_error(format!("error getting origin6: {e}")) })?; @@ -1026,14 +1041,14 @@ pub async fn get_exported_v3( // Only query originated prefixes for requested address families let orig4 = if process_ipv4 { - r.db.get_origin4().map_err(|e| { + r.db.get_origin(Some(AddressFamily::Ipv4)).map_err(|e| { HttpError::for_internal_error(format!("error getting origin4: {e}")) })? } else { Vec::new() }; let orig6 = if process_ipv6 { - r.db.get_origin6().map_err(|e| { + r.db.get_origin(Some(AddressFamily::Ipv6)).map_err(|e| { HttpError::for_internal_error(format!("error getting origin6: {e}")) })? } else { @@ -1932,7 +1947,7 @@ pub(crate) mod helpers { ) -> Result { bgp_log!(ctx.log, info, "remove neighbor (addr {addr}, asn {asn})"); - ctx.db.remove_bgp_prefixes_from_peer(&PeerId::Ip(addr)); + ctx.db.remove_all_prefixes_from_bgp_peer(&PeerId::Ip(addr)); ctx.db.remove_bgp_neighbor(addr)?; get_router!(&ctx, asn)?.delete_session(addr); @@ -2559,8 +2574,8 @@ pub(crate) mod helpers { /// Returns None if the peer is not Established or has no routes to export. pub(crate) fn get_exported( session: &SessionRunner, - orig4: &[Prefix4], - orig6: &[Prefix6], + orig4: &[Prefix], + orig6: &[Prefix], process_ipv4: bool, process_ipv6: bool, ) -> Option<(PeerId, Vec)> { @@ -2593,8 +2608,7 @@ pub(crate) mod helpers { && ipv4_negotiated && let Some(ref ipv4_config) = session_info.ipv4_unicast { - let mut v4_routes: Vec = - orig4.iter().map(|p| rdb::Prefix::from(*p)).collect(); + let mut v4_routes: Vec = orig4.to_vec(); // Apply export policy match &ipv4_config.export_policy { @@ -2619,8 +2633,7 @@ pub(crate) mod helpers { && ipv6_negotiated && let Some(ref ipv6_config) = session_info.ipv6_unicast { - let mut v6_routes: Vec = - orig6.iter().map(|p| rdb::Prefix::from(*p)).collect(); + let mut v6_routes: Vec = orig6.to_vec(); // Apply export policy match &ipv6_config.export_policy { diff --git a/mgd/src/oxstats.rs b/mgd/src/oxstats.rs index 3f7251733..eb8a918fb 100644 --- a/mgd/src/oxstats.rs +++ b/mgd/src/oxstats.rs @@ -15,7 +15,7 @@ use omicron_common::api::internal::nexus::{ProducerEndpoint, ProducerKind}; use oximeter::types::{Cumulative, ProducerRegistry}; use oximeter::{MetricsError, Producer, Sample}; use oximeter_producer::{ConfigLogging, ConfigLoggingLevel, LogConfig}; -use rdb::Db; +use rdb::{AddressFamily, Db}; use slog::Logger; use std::collections::BTreeMap; use std::net::SocketAddr; @@ -686,7 +686,7 @@ impl Stats { fn static_stats(&mut self) -> Result, MetricsError> { let mut samples = Vec::new(); - match self.db.get_static4_count() { + match self.db.get_static_count(AddressFamily::Ipv4) { Ok(count) => { samples.push(static_counter!( self.hostname.clone().into(), @@ -701,7 +701,7 @@ impl Stats { olog!(self.log, warn, "failed to produce static4 count: {e}"); } } - match self.db.get_static_nexthop4_count() { + match self.db.get_static_nexthop_count(AddressFamily::Ipv4) { Ok(count) => { samples.push(static_counter!( self.hostname.clone().into(), diff --git a/rdb/src/db.rs b/rdb/src/db.rs index ec2ef463b..d1daf0db7 100644 --- a/rdb/src/db.rs +++ b/rdb/src/db.rs @@ -171,74 +171,52 @@ impl Db { } } - fn loc_rib4(&self) -> Rib4 { - lock!(self.rib4_loc).clone() - } - - fn loc_rib6(&self) -> Rib6 { - lock!(self.rib6_loc).clone() - } - pub fn loc_rib(&self, af: Option) -> Rib { match af { - Some(AddressFamily::Ipv4) => self - .loc_rib4() - .into_iter() - .map(|(p4, paths)| (Prefix::from(p4), paths)) + Some(AddressFamily::Ipv4) => lock!(self.rib4_loc) + .iter() + .map(|(p4, paths)| (Prefix::from(*p4), paths.clone())) .collect(), - Some(AddressFamily::Ipv6) => self - .loc_rib6() - .into_iter() - .map(|(p6, paths)| (Prefix::from(p6), paths)) + Some(AddressFamily::Ipv6) => lock!(self.rib6_loc) + .iter() + .map(|(p6, paths)| (Prefix::from(*p6), paths.clone())) .collect(), None => { - let mut rib: Rib = self - .loc_rib4() - .into_iter() - .map(|(p4, paths)| (Prefix::from(p4), paths)) + let mut rib: Rib = lock!(self.rib4_loc) + .iter() + .map(|(p4, paths)| (Prefix::from(*p4), paths.clone())) .collect(); rib.extend( - self.loc_rib6() - .into_iter() - .map(|(p6, paths)| (Prefix::from(p6), paths)), + lock!(self.rib6_loc) + .iter() + .map(|(p6, paths)| (Prefix::from(*p6), paths.clone())), ); rib } } } - fn full_rib4(&self) -> Rib4 { - lock!(self.rib4_in).clone() - } - - fn full_rib6(&self) -> Rib6 { - lock!(self.rib6_in).clone() - } - pub fn full_rib(&self, af: Option) -> Rib { match af { - Some(AddressFamily::Ipv4) => self - .full_rib4() - .into_iter() - .map(|(p4, paths)| (Prefix::from(p4), paths)) + Some(AddressFamily::Ipv4) => lock!(self.rib4_in) + .iter() + .map(|(p4, paths)| (Prefix::from(*p4), paths.clone())) .collect(), - Some(AddressFamily::Ipv6) => self - .full_rib6() - .into_iter() - .map(|(p6, paths)| (Prefix::from(p6), paths)) + Some(AddressFamily::Ipv6) => lock!(self.rib6_in) + .iter() + .map(|(p6, paths)| (Prefix::from(*p6), paths.clone())) .collect(), None => { - let mut rib: Rib = self - .full_rib4() - .into_iter() - .map(|(p4, paths)| (Prefix::from(p4), paths)) + let mut rib: Rib = lock!(self.rib4_in) + .iter() + .map(|(p4, paths)| (Prefix::from(*p4), paths.clone())) .collect(); rib.extend( - self.full_rib6() - .into_iter() - .map(|(p6, paths)| (Prefix::from(p6), paths)), + lock!(self.rib6_in) + .iter() + .map(|(p6, paths)| (Prefix::from(*p6), paths.clone())), ); rib } @@ -482,39 +460,11 @@ impl Db { Ok(result) } - pub fn create_origin4(&self, ps: &[Prefix4]) -> Result<(), Error> { - rdb_log!(self, info, - "create origin4: {ps:?}"; - "unit" => UNIT_PERSISTENT - ); - - let current = self.get_origin4()?; - if !current.is_empty() { - return Err(Error::Conflict("origin already exists".to_string())); - } - - self.set_origin4(ps) - } - - pub fn set_origin4(&self, ps: &[Prefix4]) -> Result<(), Error> { - let tree = self.persistent.open_tree(BGP_ORIGIN4)?; - tree.clear()?; - for p in ps.iter() { - tree.insert(p.db_key(), "")?; - } - tree.flush()?; - Ok(()) - } - - pub fn clear_origin4(&self) -> Result<(), Error> { - let tree = self.persistent.open_tree(BGP_ORIGIN4)?; - tree.clear()?; - tree.flush()?; - Ok(()) - } - - pub fn get_origin4(&self) -> Result, Error> { - let tree = self.persistent.open_tree(BGP_ORIGIN4)?; + fn get_origin_tree( + &self, + tree_name: &str, + ) -> Result, Error> { + let tree = self.persistent.open_tree(tree_name)?; let result = tree .scan_prefix(vec![]) .filter_map(|item| { @@ -530,7 +480,7 @@ impl Db { return None; } }; - Some(match Prefix4::from_db_key(&key) { + Some(match P::from_db_key(&key) { Ok(item) => item, Err(ref e) => { rdb_log!( @@ -547,66 +497,125 @@ impl Db { Ok(result) } - pub fn create_origin6(&self, ps: &[Prefix6]) -> Result<(), Error> { - let current = self.get_origin6()?; + fn set_origin_tree( + &self, + tree_name: &str, + ps: &[P], + ) -> Result<(), Error> { + let tree = self.persistent.open_tree(tree_name)?; + tree.clear()?; + for p in ps.iter() { + tree.insert(p.db_key(), "")?; + } + tree.flush()?; + Ok(()) + } + + fn create_origin_tree( + &self, + tree_name: &str, + ps: &[P], + ) -> Result<(), Error> { + let current: Vec

= self.get_origin_tree(tree_name)?; if !current.is_empty() { return Err(Error::Conflict("origin already exists".to_string())); } + self.set_origin_tree(tree_name, ps) + } - self.set_origin6(ps) + pub fn get_origin( + &self, + af: Option, + ) -> Result, Error> { + match af { + Some(AddressFamily::Ipv4) => { + let ps: Vec = self.get_origin_tree(BGP_ORIGIN4)?; + Ok(ps.into_iter().map(Prefix::from).collect()) + } + Some(AddressFamily::Ipv6) => { + let ps: Vec = self.get_origin_tree(BGP_ORIGIN6)?; + Ok(ps.into_iter().map(Prefix::from).collect()) + } + None => { + let p4: Vec = self.get_origin_tree(BGP_ORIGIN4)?; + let p6: Vec = self.get_origin_tree(BGP_ORIGIN6)?; + let mut result: Vec = + p4.into_iter().map(Prefix::from).collect(); + result.extend(p6.into_iter().map(Prefix::from)); + Ok(result) + } + } } - pub fn set_origin6(&self, ps: &[Prefix6]) -> Result<(), Error> { - let tree = self.persistent.open_tree(BGP_ORIGIN6)?; - tree.clear()?; - for p in ps.iter() { - tree.insert(p.db_key(), "")?; + pub fn set_origin( + &self, + af: AddressFamily, + ps: &[Prefix], + ) -> Result<(), Error> { + match af { + AddressFamily::Ipv4 => { + let p4: Vec = ps + .iter() + .filter_map(|p| match p { + Prefix::V4(p4) => Some(*p4), + _ => None, + }) + .collect(); + self.set_origin_tree(BGP_ORIGIN4, &p4) + } + AddressFamily::Ipv6 => { + let p6: Vec = ps + .iter() + .filter_map(|p| match p { + Prefix::V6(p6) => Some(*p6), + _ => None, + }) + .collect(); + self.set_origin_tree(BGP_ORIGIN6, &p6) + } + } + } + + pub fn create_origin( + &self, + af: AddressFamily, + ps: &[Prefix], + ) -> Result<(), Error> { + match af { + AddressFamily::Ipv4 => { + let p4: Vec = ps + .iter() + .filter_map(|p| match p { + Prefix::V4(p4) => Some(*p4), + _ => None, + }) + .collect(); + self.create_origin_tree(BGP_ORIGIN4, &p4) + } + AddressFamily::Ipv6 => { + let p6: Vec = ps + .iter() + .filter_map(|p| match p { + Prefix::V6(p6) => Some(*p6), + _ => None, + }) + .collect(); + self.create_origin_tree(BGP_ORIGIN6, &p6) + } } - tree.flush()?; - Ok(()) } - pub fn clear_origin6(&self) -> Result<(), Error> { - let tree = self.persistent.open_tree(BGP_ORIGIN6)?; + pub fn clear_origin(&self, af: AddressFamily) -> Result<(), Error> { + let tree_name = match af { + AddressFamily::Ipv4 => BGP_ORIGIN4, + AddressFamily::Ipv6 => BGP_ORIGIN6, + }; + let tree = self.persistent.open_tree(tree_name)?; tree.clear()?; tree.flush()?; Ok(()) } - pub fn get_origin6(&self) -> Result, Error> { - let tree = self.persistent.open_tree(BGP_ORIGIN6)?; - let result = tree - .scan_prefix(vec![]) - .filter_map(|item| { - let (key, _value) = match item { - Ok(item) => item, - Err(ref e) => { - rdb_log!( - self, - error, - "error fetching bgp origin entry {item:?}: {e}"; - "unit" => UNIT_PERSISTENT - ); - return None; - } - }; - Some(match Prefix6::from_db_key(&key) { - Ok(item) => item, - Err(e) => { - rdb_log!( - self, - error, - "error parsing bgp origin entry value {key:?}: {e}"; - "unit" => UNIT_PERSISTENT - ); - return None; - } - }) - }) - .collect(); - Ok(result) - } - pub fn get_prefix_paths(&self, prefix: &Prefix) -> Vec { match prefix { Prefix::V4(p4) => { @@ -645,48 +654,11 @@ impl Db { } } - pub fn update_rib4_loc( - &self, - rib_in: &Rib4, - rib_loc: &mut Rib4, - prefix: &Prefix4, - ) { - let fanout = self.get_bestpath_fanout().unwrap_or_else(|e| { - rdb_log!( - self, - error, - "failed to get bestpath fanout: {e}"; - "unit" => UNIT_PERSISTENT - ); - NonZeroU8::new(DEFAULT_BESTPATH_FANOUT).unwrap() - }); - - match rib_in.get(prefix) { - // rib-in has paths worth evaluating for loc-rib - Some(paths) => { - match bestpaths(paths, fanout.get().into()) { - // bestpath found at least 1 path for loc-rib - Some(bp) => { - rib_loc.insert(*prefix, bp.clone()); - } - // bestpath found no suitable paths - None => { - rib_loc.remove(prefix); - } - } - } - // rib-in has no worthy paths - None => { - rib_loc.remove(prefix); - } - } - } - - pub fn update_rib6_loc( + fn recompute_rib_loc( &self, - rib_in: &Rib6, - rib_loc: &mut Rib6, - prefix: &Prefix6, + rib_in: &BTreeMap>, + rib_loc: &mut BTreeMap>, + prefix: &P, ) { let fanout = self.get_bestpath_fanout().unwrap_or_else(|e| { rdb_log!( @@ -699,20 +671,14 @@ impl Db { }); match rib_in.get(prefix) { - // rib-in has paths worth evaluating for loc-rib Some(paths) => { - match bestpaths(paths, fanout.get().into()) { - // bestpath found at least 1 path for loc-rib - Some(bp) => { - rib_loc.insert(*prefix, bp.clone()); - } - // bestpath found no suitable paths - None => { - rib_loc.remove(prefix); - } - } + Self::update_rib_loc( + prefix, + paths, + rib_loc, + fanout.get().into(), + ); } - // rib-in has no worthy paths None => { rib_loc.remove(prefix); } @@ -722,7 +688,7 @@ impl Db { // generic helper function to kick off a bestpath run for some // subset of prefixes in rib_in. the caller chooses which prefixes // bestpath is run against via the bestpath_needed closure - pub fn trigger_bestpath_when(&self, bestpath_needed: F) + fn trigger_bestpath_when(&self, bestpath_needed: F) where F: Fn(&Prefix, &BTreeSet) -> bool, { @@ -786,40 +752,22 @@ impl Db { } } - fn add_prefix4_path( - &self, - p4: &Prefix4, - path: &Path, - rib_in: &mut Rib4, - rib_loc: &mut Rib4, - ) { - match rib_in.get_mut(p4) { - Some(paths) => { - paths.replace(path.clone()); - } - None => { - rib_in.insert(*p4, BTreeSet::from([path.clone()])); - } - } - self.update_rib4_loc(rib_in, rib_loc, p4); - } - - fn add_prefix6_path( + fn insert_prefix_path( &self, - p6: &Prefix6, + prefix: &P, path: &Path, - rib_in: &mut Rib6, - rib_loc: &mut Rib6, + rib_in: &mut BTreeMap>, + rib_loc: &mut BTreeMap>, ) { - match rib_in.get_mut(p6) { + match rib_in.get_mut(prefix) { Some(paths) => { paths.replace(path.clone()); } None => { - rib_in.insert(*p6, BTreeSet::from([path.clone()])); + rib_in.insert(*prefix, BTreeSet::from([path.clone()])); } } - self.update_rib6_loc(rib_in, rib_loc, p6); + self.recompute_rib_loc(rib_in, rib_loc, prefix); } pub fn add_prefix_path(&self, prefix: &Prefix, path: &Path) { @@ -827,12 +775,12 @@ impl Db { Prefix::V4(p4) => { let mut rib_in = lock!(self.rib4_in); let mut rib_loc = lock!(self.rib4_loc); - self.add_prefix4_path(p4, path, &mut rib_in, &mut rib_loc); + self.insert_prefix_path(p4, path, &mut *rib_in, &mut *rib_loc); } Prefix::V6(p6) => { let mut rib_in = lock!(self.rib6_in); let mut rib_loc = lock!(self.rib6_loc); - self.add_prefix6_path(p6, path, &mut rib_in, &mut rib_loc); + self.insert_prefix_path(p6, path, &mut *rib_in, &mut *rib_loc); } }; } @@ -882,12 +830,12 @@ impl Db { }, ); - { + if !routes4.is_empty() { let tree = self.persistent.open_tree(STATIC4_ROUTES)?; self.add_static_routes_to_tree(tree, &routes4, &mut pcn)?; } - { + if !routes6.is_empty() { let tree = self.persistent.open_tree(STATIC6_ROUTES)?; self.add_static_routes_to_tree(tree, &routes6, &mut pcn)?; } @@ -968,31 +916,21 @@ impl Db { } } - pub fn get_static4_count(&self) -> Result { - let tree = self.persistent.open_tree(STATIC4_ROUTES)?; - Ok(tree.len()) - } - - pub fn get_static_nexthop4_count(&self) -> Result { - let entries = self.get_static(Some(AddressFamily::Ipv4))?; - let mut nexthops = BTreeSet::new(); - for e in entries { - nexthops.insert(e.nexthop); - } - Ok(nexthops.len()) - } - - pub fn get_static6_count(&self) -> Result { - let tree = self.persistent.open_tree(STATIC6_ROUTES)?; + pub fn get_static_count(&self, af: AddressFamily) -> Result { + let tree_name = match af { + AddressFamily::Ipv4 => STATIC4_ROUTES, + AddressFamily::Ipv6 => STATIC6_ROUTES, + }; + let tree = self.persistent.open_tree(tree_name)?; Ok(tree.len()) } - pub fn get_static_nexthop6_count(&self) -> Result { - let entries = self.get_static(Some(AddressFamily::Ipv6))?; - let mut nexthops = BTreeSet::new(); - for e in entries { - nexthops.insert(e.nexthop); - } + pub fn get_static_nexthop_count( + &self, + af: AddressFamily, + ) -> Result { + let entries = self.get_static(Some(af))?; + let nexthops: BTreeSet<_> = entries.iter().map(|e| e.nexthop).collect(); Ok(nexthops.len()) } @@ -1068,105 +1006,39 @@ impl Db { self.notify(pcn); } - fn remove_prefix4_path( - &self, - prefix: &Prefix4, - prefix_cmp: F, - rib_in: &mut Rib4, - rib_loc: &mut Rib4, - ) where - F: Fn(&Path) -> bool, - { - if let Some(paths) = rib_in.get_mut(prefix) { - paths.retain(|p| !prefix_cmp(p)); - if paths.is_empty() { - rib_in.remove(prefix); - } - } - - self.update_rib4_loc(rib_in, rib_loc, prefix); - } - - fn remove_prefix6_path( + fn remove_prefix_path( &self, - prefix: &Prefix6, + prefix: &P, prefix_cmp: F, - rib_in: &mut Rib6, - rib_loc: &mut Rib6, - ) where + rib_in: &mut BTreeMap>, + rib_loc: &mut BTreeMap>, + ) -> bool + where + P: Ord + Copy, F: Fn(&Path) -> bool, { - if let Some(paths) = rib_in.get_mut(prefix) { + let changed = if let Some(paths) = rib_in.get_mut(prefix) { + let before = paths.len(); paths.retain(|p| !prefix_cmp(p)); + let changed = paths.len() != before; if paths.is_empty() { rib_in.remove(prefix); } - } - - self.update_rib6_loc(rib_in, rib_loc, prefix); - } - - fn remove_prefix_path(&self, prefix: &Prefix, prefix_cmp: F) - where - F: Fn(&Path) -> bool, - { - match prefix { - Prefix::V4(p4) => { - let mut rib_in = lock!(self.rib4_in); - let mut rib_loc = lock!(self.rib4_loc); - self.remove_prefix4_path( - p4, - prefix_cmp, - &mut rib_in, - &mut rib_loc, - ); - } - Prefix::V6(p6) => { - let mut rib_in = lock!(self.rib6_in); - let mut rib_loc = lock!(self.rib6_loc); - self.remove_prefix6_path( - p6, - prefix_cmp, - &mut rib_in, - &mut rib_loc, - ); - } - } - } - - fn remove_path_for_prefixes4( - &self, - prefixes: &[Prefix4], - prefix_cmp: F, - rib_in: &mut Rib4, - rib_loc: &mut Rib4, - ) where - F: Fn(&Path) -> bool, - { - for prefix in prefixes.iter() { - self.remove_prefix4_path(prefix, &prefix_cmp, rib_in, rib_loc); - } - } + changed + } else { + false + }; - fn remove_path_for_prefixes6( - &self, - prefixes: &[Prefix6], - prefix_cmp: F, - rib_in: &mut Rib6, - rib_loc: &mut Rib6, - ) where - F: Fn(&Path) -> bool, - { - for prefix in prefixes.iter() { - self.remove_prefix6_path(prefix, &prefix_cmp, rib_in, rib_loc); - } + self.recompute_rib_loc(rib_in, rib_loc, prefix); + changed } - pub fn remove_path_for_prefixes( + fn remove_path_for_prefixes( &self, prefixes: &[Prefix], prefix_cmp: F, - ) where + ) -> BTreeSet + where F: Fn(&Path) -> bool, { // split prefixes into v4 and v6 groups. this allows us to lock the v4 @@ -1183,42 +1055,56 @@ impl Db { }, ); + let mut changed = BTreeSet::new(); + { let mut rib_in = lock!(self.rib4_in); let mut rib_loc = lock!(self.rib4_loc); - self.remove_path_for_prefixes4( - &prefixes4, - &prefix_cmp, - &mut rib_in, - &mut rib_loc, + changed.extend( + prefixes4 + .iter() + .filter(|p| { + self.remove_prefix_path( + *p, + &prefix_cmp, + &mut *rib_in, + &mut *rib_loc, + ) + }) + .map(|p| Prefix::from(*p)), ); } { let mut rib_in = lock!(self.rib6_in); let mut rib_loc = lock!(self.rib6_loc); - self.remove_path_for_prefixes6( - &prefixes6, - &prefix_cmp, - &mut rib_in, - &mut rib_loc, + changed.extend( + prefixes6 + .iter() + .filter(|p| { + self.remove_prefix_path( + *p, + &prefix_cmp, + &mut *rib_in, + &mut *rib_loc, + ) + }) + .map(|p| Prefix::from(*p)), ); } + + changed } - fn remove_static_routes_from_tree( - &self, + /// Remove route keys from a sled tree in a single transaction. + fn flush_static_route_removals( tree: Tree, routes: &[StaticRouteKey], ) -> Result<(), Error> { - let mut pcn = PrefixChangeNotification::default(); - - let mut route_keys = Vec::new(); - for route in routes { - let key = serde_json::to_string(route)?; - route_keys.push(key); - pcn.changed.insert(route.prefix); - } + let route_keys: Vec = routes + .iter() + .map(serde_json::to_string) + .collect::>()?; tree.transaction(|tx_db| { for key in &route_keys { @@ -1227,14 +1113,6 @@ impl Db { Ok(()) })?; tree.flush()?; - - for route in routes { - self.remove_prefix_path(&route.prefix, |rib_path: &Path| { - rib_path.cmp(&Path::from(route.clone())) == CmpOrdering::Equal - }); - } - - self.notify(pcn); Ok(()) } @@ -1253,83 +1131,146 @@ impl Db { }, ); - { + // Persist removals to sled. + if !routes4.is_empty() { let tree = self.persistent.open_tree(STATIC4_ROUTES)?; - self.remove_static_routes_from_tree(tree, &routes4)?; + Self::flush_static_route_removals(tree, &routes4)?; } - { + if !routes6.is_empty() { let tree = self.persistent.open_tree(STATIC6_ROUTES)?; - self.remove_static_routes_from_tree(tree, &routes6)?; + Self::flush_static_route_removals(tree, &routes6)?; + } + + // Remove from RIBs. + let mut pcn = PrefixChangeNotification::default(); + if !routes4.is_empty() { + let mut rib_in = lock!(self.rib4_in); + let mut rib_loc = lock!(self.rib4_loc); + for route in &routes4 { + let Prefix::V4(p4) = &route.prefix else { + continue; + }; + self.remove_prefix_path( + p4, + |rib_path: &Path| { + rib_path.cmp(&Path::from(route.clone())) + == CmpOrdering::Equal + }, + &mut *rib_in, + &mut *rib_loc, + ); + pcn.changed.insert(route.prefix); + } + } + if !routes6.is_empty() { + let mut rib_in = lock!(self.rib6_in); + let mut rib_loc = lock!(self.rib6_loc); + for route in &routes6 { + let Prefix::V6(p6) = &route.prefix else { + continue; + }; + self.remove_prefix_path( + p6, + |rib_path: &Path| { + rib_path.cmp(&Path::from(route.clone())) + == CmpOrdering::Equal + }, + &mut *rib_in, + &mut *rib_loc, + ); + pcn.changed.insert(route.prefix); + } + } + if !pcn.changed.is_empty() { + self.notify(pcn); } Ok(()) } - // for each route in @prefixes, remove all bgp paths learned from @peer - pub fn remove_bgp_prefixes(&self, prefixes: &[Prefix], peer: &PeerId) { - let mut pcn = PrefixChangeNotification::default(); - self.remove_path_for_prefixes( - prefixes, - |rib_path: &Path| match rib_path.bgp { - Some(ref bgp) => bgp.peer == *peer, - None => false, - }, - ); - pcn.changed.extend(prefixes); - self.notify(pcn); + /// Remove specific BGP prefixes from a peer. + /// Used for UPDATE withdrawals. + pub fn remove_prefixes_from_bgp_peer( + &self, + prefixes: &[Prefix], + peer: &PeerId, + ) { + let changed = + self.remove_path_for_prefixes(prefixes, |rib_path: &Path| { + match rib_path.bgp { + Some(ref bgp) => bgp.peer == *peer, + None => false, + } + }); + if !changed.is_empty() { + let pcn = PrefixChangeNotification { changed }; + self.notify(pcn); + } } - /// Remove BGP paths from a specific peer that match an additional - /// predicate. Used for policy changes like enforce-first-AS that - /// need to selectively remove a subset of a peer's paths. - pub fn remove_bgp_peer_paths_where(&self, peer: &PeerId, predicate: F) - where - F: Fn(&BgpPathProperties) -> bool, - { - let all_prefixes4: Vec<_> = self - .full_rib(Some(AddressFamily::Ipv4)) - .keys() - .copied() - .collect(); - let all_prefixes6: Vec<_> = self - .full_rib(Some(AddressFamily::Ipv6)) - .keys() - .copied() - .collect(); - - let mut all_prefixes = Vec::new(); - all_prefixes.extend(all_prefixes4); - all_prefixes.extend(all_prefixes6); - - self.remove_path_for_prefixes(&all_prefixes, |path: &Path| match path - .bgp - { - Some(ref bgp) => bgp.peer == *peer && predicate(bgp), - None => false, - }); - - let mut pcn = PrefixChangeNotification::default(); - pcn.changed.extend(all_prefixes); - self.notify(pcn); + /// Remove all BGP paths from a peer. + /// Used when a peer is deleted or exits Established state. + pub fn remove_all_prefixes_from_bgp_peer(&self, peer: &PeerId) { + let all_prefixes: Vec<_> = + self.full_rib(None).keys().copied().collect(); + self.remove_prefixes_from_bgp_peer(&all_prefixes, peer); + } + + /// Remove BGP paths from a peer that violate the first-AS rule. + /// A path violates first-AS when its AS path is empty or when + /// the first AS in the path does not match the peer's origin AS. + pub fn enforce_first_as(&self, peer: &PeerId) { + let all_prefixes: Vec<_> = + self.full_rib(None).keys().copied().collect(); + let changed = + self.remove_path_for_prefixes(&all_prefixes, |path: &Path| { + match path.bgp { + Some(ref bgp) => { + bgp.peer == *peer + && match bgp.as_path.first() { + Some(&first_as) => first_as != bgp.origin_as, + None => true, + } + } + None => false, + } + }); + if !changed.is_empty() { + let pcn = PrefixChangeNotification { changed }; + self.notify(pcn); + } } - // wrapper for remove_bgp_prefixes to handle the "all routes" corner case. - // e.g. when peer is deleted or exits Established state - pub fn remove_bgp_prefixes_from_peer(&self, peer: &PeerId) { - // TODO(ipv6): call this just for enabled address-families. - // no need to walk the full rib for an AF that isn't affected - let peer_routes4: Vec<_> = self - .full_rib(Some(AddressFamily::Ipv4)) - .keys() - .copied() - .collect(); - let peer_routes6: Vec<_> = self - .full_rib(Some(AddressFamily::Ipv6)) - .keys() - .copied() - .collect(); - self.remove_bgp_prefixes(&peer_routes4, peer); - self.remove_bgp_prefixes(&peer_routes6, peer); + /// Mark all paths from a BGP peer as stale in the local RIB + /// for the given address family. Used for route refresh. + pub fn mark_bgp_peer_stale(&self, peer: PeerId, af: AddressFamily) { + let mut mark = |paths: &mut BTreeSet| { + let targets: Vec = paths + .iter() + .filter_map(|p| { + if let Some(bgp) = p.bgp.as_ref() + && bgp.peer == peer + { + let mut marked = p.clone(); + marked.bgp = Some(bgp.as_stale()); + return Some(marked); + } + None + }) + .collect(); + for t in targets { + paths.replace(t); + } + }; + + match af { + AddressFamily::Ipv4 => { + lock!(self.rib4_loc).values_mut().for_each(&mut mark); + } + AddressFamily::Ipv6 => { + lock!(self.rib6_loc).values_mut().for_each(&mut mark); + } + } } pub fn generation(&self) -> u64 { @@ -1389,50 +1330,6 @@ impl Db { Ok(()) } - pub fn mark_bgp_peer_stale4(&self, peer: PeerId) { - let mut rib = lock!(self.rib4_loc); - rib.iter_mut().for_each(|(_prefix, path)| { - let targets: Vec = path - .iter() - .filter_map(|p| { - if let Some(bgp) = p.bgp.as_ref() - && bgp.peer == peer - { - let mut marked = p.clone(); - marked.bgp = Some(bgp.as_stale()); - return Some(marked); - } - None - }) - .collect(); - for t in targets.into_iter() { - path.replace(t); - } - }); - } - - pub fn mark_bgp_peer_stale6(&self, peer: PeerId) { - let mut rib = lock!(self.rib6_loc); - rib.iter_mut().for_each(|(_prefix, path)| { - let targets: Vec = path - .iter() - .filter_map(|p| { - if let Some(bgp) = p.bgp.as_ref() - && bgp.peer == peer - { - let mut marked = p.clone(); - marked.bgp = Some(bgp.as_stale()); - return Some(marked); - } - None - }) - .collect(); - for t in targets.into_iter() { - path.replace(t); - } - }); - } - pub fn slot(&self) -> Option { match self.slot.read() { Ok(v) => *v, @@ -1447,13 +1344,6 @@ impl Db { let mut value = self.slot.write().unwrap(); *value = slot; } - - pub fn mark_bgp_peer_stale(&self, peer: PeerId, af: AddressFamily) { - match af { - AddressFamily::Ipv4 => self.mark_bgp_peer_stale4(peer.clone()), - AddressFamily::Ipv6 => self.mark_bgp_peer_stale6(peer), - } - } } struct Reaper { @@ -1736,7 +1626,10 @@ mod test { assert!(check_prefix_path(&db, &p2, rib_in_paths, loc_rib_paths)); // withdrawal of p2 via bgp_path1 - db.remove_bgp_prefixes(&[p2], &bgp_path1.clone().bgp.unwrap().peer); + db.remove_prefixes_from_bgp_peer( + &[p2], + &bgp_path1.clone().bgp.unwrap().peer, + ); // expected current state // rib_in: // - p0 via bgp_path0, static_path1 (ordered by nexthop IP) @@ -1758,7 +1651,7 @@ mod test { assert!(check_prefix_path(&db, &p2, rib_in_paths, loc_rib_paths)); // yank all routes from bgp_path0, simulating peer shutdown - db.remove_bgp_prefixes_from_peer(&bgp_path0.bgp.unwrap().peer); + db.remove_all_prefixes_from_bgp_peer(&bgp_path0.bgp.unwrap().peer); // expected current state // rib_in: // - p0 via static_path1 @@ -1780,7 +1673,9 @@ mod test { // yank all routes from bgp_path2, simulating peer shutdown // bgp_path2 should be unaffected, despite also having the same RID - db.remove_bgp_prefixes_from_peer(&bgp_path2.clone().bgp.unwrap().peer); + db.remove_all_prefixes_from_bgp_peer( + &bgp_path2.clone().bgp.unwrap().peer, + ); // expected current state // rib_in: // - p0 via static_path1 @@ -1800,7 +1695,9 @@ mod test { // yank all routes from bgp_path1, simulating peer shutdown // p0 should be unaffected, still retaining the static path - db.remove_bgp_prefixes_from_peer(&bgp_path1.clone().bgp.unwrap().peer); + db.remove_all_prefixes_from_bgp_peer( + &bgp_path1.clone().bgp.unwrap().peer, + ); // expected current state // rib_in: // - p0 via static_path1 @@ -2013,13 +1910,13 @@ mod test { assert!(all_routes.contains(&route6)); // Test counts - assert_eq!(db.get_static4_count().unwrap(), 1); - assert_eq!(db.get_static6_count().unwrap(), 1); + assert_eq!(db.get_static_count(AddressFamily::Ipv4).unwrap(), 1); + assert_eq!(db.get_static_count(AddressFamily::Ipv6).unwrap(), 1); // Remove routes and verify cleanup db.remove_static_routes(&[route4, route6]).unwrap(); - assert_eq!(db.get_static4_count().unwrap(), 0); - assert_eq!(db.get_static6_count().unwrap(), 0); + assert_eq!(db.get_static_count(AddressFamily::Ipv4).unwrap(), 0); + assert_eq!(db.get_static_count(AddressFamily::Ipv6).unwrap(), 0); } #[test] @@ -2164,40 +2061,50 @@ mod test { fn test_ipv4_origin_crud() { let db = get_test_db(); - // Test creating IPv4 origins - let prefixes = vec![ - Prefix4::new(Ipv4Addr::new(192, 168, 1, 0), 24), - Prefix4::new(Ipv4Addr::new(10, 0, 0, 0), 8), - ]; + let p4a = Prefix4::new(Ipv4Addr::new(192, 168, 1, 0), 24); + let p4b = Prefix4::new(Ipv4Addr::new(10, 0, 0, 0), 8); + let prefixes: Vec = vec![Prefix::from(p4a), Prefix::from(p4b)]; - // Create origin4 - should succeed - db.create_origin4(&prefixes).expect("create origin4"); + // Create - should succeed + db.create_origin(AddressFamily::Ipv4, &prefixes) + .expect("create origin4"); - // Get origin4 - should return created prefixes - let retrieved = db.get_origin4().expect("get origin4"); + // Get - should return created prefixes + let retrieved = db + .get_origin(Some(AddressFamily::Ipv4)) + .expect("get origin4"); assert_eq!(retrieved.len(), 2); - assert!(retrieved.contains(&prefixes[0])); - assert!(retrieved.contains(&prefixes[1])); + assert!(retrieved.contains(&Prefix::from(p4a))); + assert!(retrieved.contains(&Prefix::from(p4b))); // Try to create again - should fail with conflict - assert!(db.create_origin4(&prefixes).is_err()); + assert!(db.create_origin(AddressFamily::Ipv4, &prefixes).is_err()); - // Update origin4 with different prefixes - let new_prefixes = vec![Prefix4::new(Ipv4Addr::new(172, 16, 0, 0), 12)]; - db.set_origin4(&new_prefixes).expect("set origin4"); + // Update with different prefixes + let new_p = Prefix4::new(Ipv4Addr::new(172, 16, 0, 0), 12); + let new_prefixes = vec![Prefix::from(new_p)]; + db.set_origin(AddressFamily::Ipv4, &new_prefixes) + .expect("set origin4"); - let updated = db.get_origin4().expect("get updated origin4"); + let updated = db + .get_origin(Some(AddressFamily::Ipv4)) + .expect("get updated origin4"); assert_eq!(updated.len(), 1); - assert_eq!(updated[0], new_prefixes[0]); + assert_eq!(updated[0], Prefix::from(new_p)); - // Clear origin4 - db.clear_origin4().expect("clear origin4"); - let empty = db.get_origin4().expect("get empty origin4"); + // Clear + db.clear_origin(AddressFamily::Ipv4).expect("clear origin4"); + let empty = db + .get_origin(Some(AddressFamily::Ipv4)) + .expect("get empty origin4"); assert!(empty.is_empty()); // Create again after clear - should succeed - db.create_origin4(&prefixes).expect("create after clear"); - let final_result = db.get_origin4().expect("get final origin4"); + db.create_origin(AddressFamily::Ipv4, &prefixes) + .expect("create after clear"); + let final_result = db + .get_origin(Some(AddressFamily::Ipv4)) + .expect("get final origin4"); assert_eq!(final_result.len(), 2); } @@ -2205,43 +2112,52 @@ mod test { fn test_ipv6_origin_crud() { let db = get_test_db(); - // Test creating IPv6 origins - let prefixes = vec![ - Prefix6::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0), 32), - Prefix6::new(Ipv6Addr::new(0xfd00, 0, 0, 0, 0, 0, 0, 0), 8), - ]; + let p6a = + Prefix6::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0), 32); + let p6b = Prefix6::new(Ipv6Addr::new(0xfd00, 0, 0, 0, 0, 0, 0, 0), 8); + let prefixes: Vec = vec![Prefix::from(p6a), Prefix::from(p6b)]; - // Create origin6 - should succeed - db.create_origin6(&prefixes).expect("create origin6"); + // Create - should succeed + db.create_origin(AddressFamily::Ipv6, &prefixes) + .expect("create origin6"); - // Get origin6 - should return created prefixes - let retrieved = db.get_origin6().expect("get origin6"); + // Get - should return created prefixes + let retrieved = db + .get_origin(Some(AddressFamily::Ipv6)) + .expect("get origin6"); assert_eq!(retrieved.len(), 2); - assert!(retrieved.contains(&prefixes[0])); - assert!(retrieved.contains(&prefixes[1])); + assert!(retrieved.contains(&Prefix::from(p6a))); + assert!(retrieved.contains(&Prefix::from(p6b))); // Try to create again - should fail with conflict - assert!(db.create_origin6(&prefixes).is_err()); - - // Update origin6 with different prefixes - let new_prefixes = vec![Prefix6::new( - Ipv6Addr::new(0x2001, 0xdb8, 1, 0, 0, 0, 0, 0), - 48, - )]; - db.set_origin6(&new_prefixes).expect("set origin6"); - - let updated = db.get_origin6().expect("get updated origin6"); + assert!(db.create_origin(AddressFamily::Ipv6, &prefixes).is_err()); + + // Update with different prefixes + let new_p = + Prefix6::new(Ipv6Addr::new(0x2001, 0xdb8, 1, 0, 0, 0, 0, 0), 48); + let new_prefixes = vec![Prefix::from(new_p)]; + db.set_origin(AddressFamily::Ipv6, &new_prefixes) + .expect("set origin6"); + + let updated = db + .get_origin(Some(AddressFamily::Ipv6)) + .expect("get updated origin6"); assert_eq!(updated.len(), 1); - assert_eq!(updated[0], new_prefixes[0]); + assert_eq!(updated[0], Prefix::from(new_p)); - // Clear origin6 - db.clear_origin6().expect("clear origin6"); - let empty = db.get_origin6().expect("get empty origin6"); + // Clear + db.clear_origin(AddressFamily::Ipv6).expect("clear origin6"); + let empty = db + .get_origin(Some(AddressFamily::Ipv6)) + .expect("get empty origin6"); assert!(empty.is_empty()); // Create again after clear - should succeed - db.create_origin6(&prefixes).expect("create after clear"); - let final_result = db.get_origin6().expect("get final origin6"); + db.create_origin(AddressFamily::Ipv6, &prefixes) + .expect("create after clear"); + let final_result = db + .get_origin(Some(AddressFamily::Ipv6)) + .expect("get final origin6"); assert_eq!(final_result.len(), 2); } @@ -2599,14 +2515,14 @@ mod test { } #[test] - fn test_remove_bgp_peer_paths_where() { + fn test_enforce_first_as() { use crate::{ BgpPathProperties, DEFAULT_RIB_PRIORITY_BGP, Path, PeerId, Prefix, Prefix4, Prefix6, }; let log = init_file_logger("rib.log"); - let db = crate::test::get_test_db("remove_bgp_peer_paths_where", log) + let db = crate::test::get_test_db("enforce_first_as", log) .expect("create db"); let peer_ip = IpAddr::from_str("203.0.113.1").unwrap(); @@ -2683,13 +2599,8 @@ mod test { assert_eq!(db.get_prefix_paths(&p4).len(), 2); assert_eq!(db.get_prefix_paths(&p6).len(), 1); - // Remove paths from `peer` where first AS != origin_as - db.remove_bgp_peer_paths_where(&peer, |bgp| { - match bgp.as_path.first() { - Some(&first_as) => first_as != bgp.origin_as, - None => true, - } - }); + // Remove paths from `peer` that violate first-AS + db.enforce_first_as(&peer); // p4: good_path (passes check) + other_peer_path (different // peer) should remain @@ -2713,16 +2624,15 @@ mod test { } #[test] - fn test_remove_bgp_peer_paths_where_empty_as_path() { + fn test_enforce_first_as_empty_as_path() { use crate::{ BgpPathProperties, DEFAULT_RIB_PRIORITY_BGP, Path, PeerId, Prefix, Prefix4, }; let log = init_file_logger("rib.log"); - let db = - crate::test::get_test_db("remove_bgp_peer_paths_where_empty", log) - .expect("create db"); + let db = crate::test::get_test_db("enforce_first_as_empty", log) + .expect("create db"); let peer_ip = IpAddr::from_str("203.0.113.1").unwrap(); let peer = PeerId::Ip(peer_ip); @@ -2750,12 +2660,7 @@ mod test { db.add_bgp_prefixes(&[prefix], empty_as_path); assert_eq!(db.get_prefix_paths(&prefix).len(), 1); - db.remove_bgp_peer_paths_where(&peer, |bgp| { - match bgp.as_path.first() { - Some(&first_as) => first_as != bgp.origin_as, - None => true, // empty path fails check - } - }); + db.enforce_first_as(&peer); assert!( db.get_prefix_paths(&prefix).is_empty(), @@ -2764,18 +2669,15 @@ mod test { } #[test] - fn test_remove_bgp_peer_paths_where_no_match() { + fn test_enforce_first_as_no_match() { use crate::{ BgpPathProperties, DEFAULT_RIB_PRIORITY_BGP, Path, PeerId, Prefix, Prefix4, }; let log = init_file_logger("rib.log"); - let db = crate::test::get_test_db( - "remove_bgp_peer_paths_where_nomatch", - log, - ) - .expect("create db"); + let db = crate::test::get_test_db("enforce_first_as_nomatch", log) + .expect("create db"); let peer_ip = IpAddr::from_str("203.0.113.1").unwrap(); let peer = PeerId::Ip(peer_ip); @@ -2802,13 +2704,8 @@ mod test { db.add_bgp_prefixes(&[prefix], good_path); assert_eq!(db.get_prefix_paths(&prefix).len(), 1); - // Predicate matches nothing — all paths pass the check - db.remove_bgp_peer_paths_where(&peer, |bgp| { - match bgp.as_path.first() { - Some(&first_as) => first_as != bgp.origin_as, - None => true, - } - }); + // All paths pass the first-AS check — nothing removed + db.enforce_first_as(&peer); assert_eq!( db.get_prefix_paths(&prefix).len(), From 9e6d6327ab6da7f636b7d404edd2fc15c122c020 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Fri, 13 Feb 2026 17:27:15 -0700 Subject: [PATCH 11/58] Convert origin{4,6} methods to be AF-agnostic There's a lot of code duplication across {create,get,set}_origin{4,6} methods, which can be simplified to just use Prefix + AddressFamily. This does exactly that -- unifies each method to just a single version that accepts Prefix (AF-agnostic) and uses AddressFamily to distinguish what should be installed where. --- bgp/src/router.rs | 268 ++++++++++++------------------------------- bgp/src/test.rs | 27 ++--- mgd/src/bgp_admin.rs | 22 ++-- rdb-types/src/lib.rs | 9 ++ 4 files changed, 111 insertions(+), 215 deletions(-) diff --git a/bgp/src/router.rs b/bgp/src/router.rs index 323a097f1..487c24cc7 100644 --- a/bgp/src/router.rs +++ b/bgp/src/router.rs @@ -464,95 +464,57 @@ impl Router { Ok(()) } - pub fn create_origin4(&self, prefixes: Vec) -> Result<(), Error> { - self.db.create_origin(AddressFamily::Ipv4, &prefixes)?; + pub fn create_origin( + &self, + af: AddressFamily, + prefixes: Vec, + ) -> Result<(), Error> { + self.db.create_origin(af, &prefixes)?; // Skip network propagation if router is shutdown if !self.shutdown.load(Ordering::Acquire) { - let prefix4: Vec = prefixes - .into_iter() - .filter_map(|x| match x { - Prefix::V4(p4) => Some(p4), - _ => None, - }) - .collect(); - self.announce_origin4(prefix4); + self.announce_origin(af, prefixes); } Ok(()) } - pub fn set_origin4(&self, prefixes: Vec) -> Result<(), Error> { - let current = self.db.get_origin(Some(AddressFamily::Ipv4))?; + pub fn set_origin( + &self, + af: AddressFamily, + prefixes: Vec, + ) -> Result<(), Error> { + let current = self.db.get_origin(Some(af))?; let current_set: BTreeSet<&Prefix> = current.iter().collect(); let new_set: BTreeSet<&Prefix> = prefixes.iter().collect(); - let to_withdraw: Vec = current_set - .difference(&new_set) - .filter_map(|p| match p { - Prefix::V4(p4) => Some(*p4), - _ => None, - }) - .collect(); + let to_withdraw: Vec = + current_set.difference(&new_set).copied().copied().collect(); - let to_announce: Vec = new_set - .difference(¤t_set) - .filter_map(|p| match p { - Prefix::V4(p4) => Some(*p4), - _ => None, - }) - .collect(); + let to_announce: Vec = + new_set.difference(¤t_set).copied().copied().collect(); - self.db.set_origin(AddressFamily::Ipv4, &prefixes)?; + self.db.set_origin(af, &prefixes)?; // Skip network propagation if router is shutdown if !self.shutdown.load(Ordering::Acquire) { - self.withdraw_origin4(to_withdraw); - self.announce_origin4(to_announce); + self.withdraw_origin(af, to_withdraw); + self.announce_origin(af, to_announce); } Ok(()) } - pub fn clear_origin4(&self) -> Result<(), Error> { - let current = self.db.get_origin(Some(AddressFamily::Ipv4))?; + pub fn clear_origin(&self, af: AddressFamily) -> Result<(), Error> { + let current = self.db.get_origin(Some(af))?; // Skip network propagation if router is shutdown if !self.shutdown.load(Ordering::Acquire) { - let current4: Vec = current - .into_iter() - .filter_map(|p| match p { - Prefix::V4(p4) => Some(p4), - _ => None, - }) - .collect(); - self.withdraw_origin4(current4); + self.withdraw_origin(af, current); } - self.db.clear_origin(AddressFamily::Ipv4)?; + self.db.clear_origin(af)?; Ok(()) } - fn announce_origin4(&self, prefixes: Vec) { - if prefixes.is_empty() { - return; - } - - let pfx_str = prefixes - .iter() - .map(ToString::to_string) - .collect::>() - .join(", "); - - slog::debug!( - self.log, - "announcing originated IPv4 prefixes"; - "component" => COMPONENT_BGP, - "prefixes" => format!("[{pfx_str}]"), - "count" => prefixes.len(), - ); - - read_lock!(self.fanout4).send_all(prefixes, vec![]); - } - - fn withdraw_origin4(&self, prefixes: Vec) { + fn announce_origin(&self, af: AddressFamily, prefixes: Vec) { if prefixes.is_empty() { return; } @@ -565,82 +527,37 @@ impl Router { slog::debug!( self.log, - "withdrawing originated IPv4 prefixes"; + "announcing originated {} prefixes", af; "component" => COMPONENT_BGP, "prefixes" => format!("[{pfx_str}]"), "count" => prefixes.len(), ); - read_lock!(self.fanout4).send_all(vec![], prefixes); - } - - pub fn create_origin6(&self, prefixes: Vec) -> Result<(), Error> { - self.db.create_origin(AddressFamily::Ipv6, &prefixes)?; - - // Skip network propagation if router is shutdown - if !self.shutdown.load(Ordering::Acquire) { - let prefix6: Vec = prefixes - .into_iter() - .filter_map(|x| match x { - Prefix::V6(p6) => Some(p6), - _ => None, - }) - .collect(); - self.announce_origin6(prefix6); - } - Ok(()) - } - - pub fn set_origin6(&self, prefixes: Vec) -> Result<(), Error> { - let current = self.db.get_origin(Some(AddressFamily::Ipv6))?; - let current_set: BTreeSet<&Prefix> = current.iter().collect(); - let new_set: BTreeSet<&Prefix> = prefixes.iter().collect(); - - let to_withdraw: Vec = current_set - .difference(&new_set) - .filter_map(|p| match p { - Prefix::V6(p6) => Some(*p6), - _ => None, - }) - .collect(); - - let to_announce: Vec = new_set - .difference(¤t_set) - .filter_map(|p| match p { - Prefix::V6(p6) => Some(*p6), - _ => None, - }) - .collect(); - - self.db.set_origin(AddressFamily::Ipv6, &prefixes)?; - - // Skip network propagation if router is shutdown - if !self.shutdown.load(Ordering::Acquire) { - self.withdraw_origin6(to_withdraw); - self.announce_origin6(to_announce); - } - Ok(()) - } - - pub fn clear_origin6(&self) -> Result<(), Error> { - let current = self.db.get_origin(Some(AddressFamily::Ipv6))?; - - // Skip network propagation if router is shutdown - if !self.shutdown.load(Ordering::Acquire) { - let current6: Vec = current - .into_iter() - .filter_map(|p| match p { - Prefix::V6(p6) => Some(p6), - _ => None, - }) - .collect(); - self.withdraw_origin6(current6); + match af { + AddressFamily::Ipv4 => { + let typed: Vec = prefixes + .into_iter() + .filter_map(|p| match p { + Prefix::V4(p4) => Some(p4), + _ => None, + }) + .collect(); + read_lock!(self.fanout4).send_all(typed, vec![]); + } + AddressFamily::Ipv6 => { + let typed: Vec = prefixes + .into_iter() + .filter_map(|p| match p { + Prefix::V6(p6) => Some(p6), + _ => None, + }) + .collect(); + read_lock!(self.fanout6).send_all(typed, vec![]); + } } - self.db.clear_origin(AddressFamily::Ipv6)?; - Ok(()) } - fn announce_origin6(&self, prefixes: Vec) { + fn withdraw_origin(&self, af: AddressFamily, prefixes: Vec) { if prefixes.is_empty() { return; } @@ -653,35 +570,34 @@ impl Router { slog::debug!( self.log, - "announcing originated IPv6 prefixes"; + "withdrawing originated {} prefixes", af; "component" => COMPONENT_BGP, "prefixes" => format!("[{pfx_str}]"), "count" => prefixes.len(), ); - read_lock!(self.fanout6).send_all(prefixes, vec![]); - } - - fn withdraw_origin6(&self, prefixes: Vec) { - if prefixes.is_empty() { - return; + match af { + AddressFamily::Ipv4 => { + let typed: Vec = prefixes + .into_iter() + .filter_map(|p| match p { + Prefix::V4(p4) => Some(p4), + _ => None, + }) + .collect(); + read_lock!(self.fanout4).send_all(vec![], typed); + } + AddressFamily::Ipv6 => { + let typed: Vec = prefixes + .into_iter() + .filter_map(|p| match p { + Prefix::V6(p6) => Some(p6), + _ => None, + }) + .collect(); + read_lock!(self.fanout6).send_all(vec![], typed); + } } - - let pfx_str = prefixes - .iter() - .map(ToString::to_string) - .collect::>() - .join(", "); - - slog::debug!( - self.log, - "withdrawing originated IPv6 prefixes"; - "component" => COMPONENT_BGP, - "prefixes" => format!("[{pfx_str}]"), - "count" => prefixes.len(), - ); - - read_lock!(self.fanout6).send_all(vec![], prefixes); } pub fn base_attributes(&self) -> Vec { @@ -749,47 +665,11 @@ impl Router { } fn announce_all(&self) -> Result<(), Error> { - let originated4: Vec = self - .db - .get_origin(Some(AddressFamily::Ipv4))? - .into_iter() - .filter_map(|p| match p { - Prefix::V4(p4) => Some(p4), - _ => None, - }) - .collect(); - - if !originated4.is_empty() { - slog::debug!( - self.log, - "announcing all originated IPv4 prefixes"; - "component" => COMPONENT_BGP, - "count" => originated4.len(), - ); - - read_lock!(self.fanout4).send_all(originated4, vec![]); - } + let originated4 = self.db.get_origin(Some(AddressFamily::Ipv4))?; + self.announce_origin(AddressFamily::Ipv4, originated4); - let originated6: Vec = self - .db - .get_origin(Some(AddressFamily::Ipv6))? - .into_iter() - .filter_map(|p| match p { - Prefix::V6(p6) => Some(p6), - _ => None, - }) - .collect(); - - if !originated6.is_empty() { - slog::debug!( - self.log, - "announcing all originated IPv6 prefixes"; - "component" => COMPONENT_BGP, - "count" => originated6.len(), - ); - - read_lock!(self.fanout6).send_all(originated6, vec![]); - } + let originated6 = self.db.get_origin(Some(AddressFamily::Ipv6))?; + self.announce_origin(AddressFamily::Ipv6, originated6); Ok(()) } diff --git a/bgp/src/test.rs b/bgp/src/test.rs index 49b36a0b4..8923bbc00 100644 --- a/bgp/src/test.rs +++ b/bgp/src/test.rs @@ -22,7 +22,8 @@ use mg_common::log::init_file_logger; use mg_common::test::{IpAllocation, LoopbackIpManager}; use mg_common::*; use rdb::{ - Asn, ImportExportPolicy4, ImportExportPolicy6, Prefix, Prefix4, Prefix6, + AddressFamily, Asn, ImportExportPolicy4, ImportExportPolicy6, Prefix, + Prefix4, Prefix6, }; use std::{ collections::{BTreeMap, BTreeSet}, @@ -738,7 +739,7 @@ fn basic_update_helper< let prefixes_v4 = generate_test_prefixes_v4(TEST_ROUTE_COUNT); r1.router - .create_origin4(prefixes_v4.clone()) + .create_origin(AddressFamily::Ipv4, prefixes_v4.clone()) .expect("originate IPv4"); // Wait for all routes to arrive at R2 @@ -844,7 +845,7 @@ fn basic_update_helper< let prefixes_v6 = generate_test_prefixes_v6(TEST_ROUTE_COUNT); r1.router - .create_origin6(prefixes_v6.clone()) + .create_origin(AddressFamily::Ipv6, prefixes_v6.clone()) .expect("originate IPv6"); // Wait for all routes to arrive at R2 @@ -952,10 +953,10 @@ fn basic_update_helper< let prefixes_v6 = generate_test_prefixes_v6(TEST_ROUTE_COUNT); r1.router - .create_origin4(prefixes_v4.clone()) + .create_origin(AddressFamily::Ipv4, prefixes_v4.clone()) .expect("originate IPv4"); r1.router - .create_origin6(prefixes_v6.clone()) + .create_origin(AddressFamily::Ipv6, prefixes_v6.clone()) .expect("originate IPv6"); // Wait for all IPv4 and IPv6 routes to arrive at R2 @@ -1688,7 +1689,7 @@ fn test_import_export_policy_filtering() { // Originate all 3 prefixes from r1 r1.router - .create_origin4(vec![prefix_a, prefix_b, prefix_c]) + .create_origin(AddressFamily::Ipv4, vec![prefix_a, prefix_b, prefix_c]) .expect("originate prefixes"); // Wait for routes to propagate - r2 should only see prefix_a @@ -3740,10 +3741,10 @@ fn test_three_router_chain_unnumbered() { // Step 8a: Originate routes from R1 and R3 r1.router - .create_origin4(vec![cidr!("10.1.0.0/24")]) + .create_origin(AddressFamily::Ipv4, vec![cidr!("10.1.0.0/24")]) .expect("originate IPv4 route on R1"); r3.router - .create_origin4(vec![cidr!("10.3.0.0/24")]) + .create_origin(AddressFamily::Ipv4, vec![cidr!("10.3.0.0/24")]) .expect("originate IPv4 route on R3"); // Step 8b: Verify R2 receives both routes @@ -3859,7 +3860,7 @@ fn test_unnumbered_dualstack_route_exchange() { // Step 1: Originate IPv4 route from R1 r1.router - .create_origin4(vec![cidr!("10.1.0.0/24")]) + .create_origin(AddressFamily::Ipv4, vec![cidr!("10.1.0.0/24")]) .expect("originate IPv4 route on R1"); // Step 2: Verify R2 receives IPv4 route with link-local nexthop @@ -3878,7 +3879,7 @@ fn test_unnumbered_dualstack_route_exchange() { // Step 3: Originate IPv6 route from R1 r1.router - .create_origin6(vec![cidr!("2001:db8:1::/48")]) + .create_origin(AddressFamily::Ipv6, vec![cidr!("2001:db8:1::/48")]) .expect("originate IPv6 route on R1"); // Step 4: Verify R2 receives IPv6 route with link-local nexthop @@ -3897,10 +3898,10 @@ fn test_unnumbered_dualstack_route_exchange() { // Step 5: Originate routes from R2 in the opposite direction r2.router - .create_origin4(vec![cidr!("10.2.0.0/24")]) + .create_origin(AddressFamily::Ipv4, vec![cidr!("10.2.0.0/24")]) .expect("originate IPv4 route on R2"); r2.router - .create_origin6(vec![cidr!("2001:db8:2::/48")]) + .create_origin(AddressFamily::Ipv6, vec![cidr!("2001:db8:2::/48")]) .expect("originate IPv6 route on R2"); // Step 6: Verify R1 receives both routes with R2's link-local nexthop @@ -4442,7 +4443,7 @@ fn test_enforce_first_as_graceful_toggle() { // Originate routes from r1 r1.router - .create_origin4(vec![prefix_a, prefix_b]) + .create_origin(AddressFamily::Ipv4, vec![prefix_a, prefix_b]) .expect("originate prefixes"); let prefix_a_rdb = Prefix::V4(cidr!("10.10.0.0/24")); diff --git a/mgd/src/bgp_admin.rs b/mgd/src/bgp_admin.rs index b79e7c61c..f896930f5 100644 --- a/mgd/src/bgp_admin.rs +++ b/mgd/src/bgp_admin.rs @@ -772,7 +772,7 @@ pub async fn create_origin4( let ctx = ctx.context(); get_router!(ctx, rq.asn)? - .create_origin4(prefixes) + .create_origin(AddressFamily::Ipv4, prefixes) .map_err(|e| HttpError::for_internal_error(e.to_string()))?; Ok(HttpResponseUpdatedNoContent()) @@ -819,7 +819,7 @@ pub async fn update_origin4( let ctx = ctx.context(); get_router!(ctx, rq.asn)? - .set_origin4(prefixes) + .set_origin(AddressFamily::Ipv4, prefixes) .map_err(|e| HttpError::for_internal_error(e.to_string()))?; Ok(HttpResponseUpdatedNoContent()) @@ -833,7 +833,7 @@ pub async fn delete_origin4( let ctx = ctx.context(); get_router!(ctx, rq.asn)? - .clear_origin4() + .clear_origin(AddressFamily::Ipv4) .map_err(|e| HttpError::for_internal_error(e.to_string()))?; Ok(HttpResponseDeleted()) @@ -852,7 +852,7 @@ pub async fn create_origin6( let ctx = ctx.context(); get_router!(ctx, rq.asn)? - .create_origin6(prefixes) + .create_origin(AddressFamily::Ipv6, prefixes) .map_err(|e| HttpError::for_internal_error(e.to_string()))?; Ok(HttpResponseUpdatedNoContent()) @@ -899,7 +899,7 @@ pub async fn update_origin6( let ctx = ctx.context(); get_router!(ctx, rq.asn)? - .set_origin6(prefixes) + .set_origin(AddressFamily::Ipv6, prefixes) .map_err(|e| HttpError::for_internal_error(e.to_string()))?; Ok(HttpResponseUpdatedNoContent()) @@ -913,7 +913,7 @@ pub async fn delete_origin6( let ctx = ctx.context(); get_router!(ctx, rq.asn)? - .clear_origin6() + .clear_origin(AddressFamily::Ipv6) .map_err(|e| HttpError::for_internal_error(e.to_string()))?; Ok(HttpResponseDeleted()) @@ -1593,11 +1593,17 @@ async fn do_bgp_apply( } get_router!(ctx, rq.asn)? - .set_origin4(rq.originate.clone().into_iter().collect()) + .set_origin( + AddressFamily::Ipv4, + rq.originate.clone().into_iter().collect(), + ) .map_err(|e| HttpError::for_internal_error(e.to_string()))?; get_router!(ctx, rq.asn)? - .set_origin6(rq.originate.clone().into_iter().collect()) + .set_origin( + AddressFamily::Ipv6, + rq.originate.clone().into_iter().collect(), + ) .map_err(|e| HttpError::for_internal_error(e.to_string()))?; Ok(HttpResponseUpdatedNoContent()) diff --git a/rdb-types/src/lib.rs b/rdb-types/src/lib.rs index a046878ec..7b1b00e4e 100644 --- a/rdb-types/src/lib.rs +++ b/rdb-types/src/lib.rs @@ -435,6 +435,15 @@ pub enum AddressFamily { Ipv6, } +impl std::fmt::Display for AddressFamily { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + AddressFamily::Ipv4 => write!(f, "IPv4"), + AddressFamily::Ipv6 => write!(f, "IPv6"), + } + } +} + #[derive( Debug, Copy, From 65adb332ba69bfb95db1acbb2c10af81f83b93ff Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Fri, 13 Feb 2026 19:41:33 -0700 Subject: [PATCH 12/58] bgp: multiple treat-as-withdraw bug fixes When an Update was marked treat-as-withdraw as a result of an error (either by the parser or by checks done by the FSM), it was never read or handled by the FSM. So no treat-as-withdraw error would ever actually result in routes being withdrawn... even worse, routes that should have been withdrawn due to errors caught by the parser would get installed if there wasn't a duplicated check in the FSM. There were also a couple other bugs that needed fixing too: 1) EnforceFirstAs was unconditionally applied to all peers, but should only be applied to eBGP peers. 2) FSM layer failures in check_update() would result in an early return rather than a withdrawal, which could lead to routes staying installed indefinitely with outdated attributes. For example, we would see issues as a result of (2) in this scenario: A) We receive an UPDATE for Route X with valid attrs B) We receive an UPDATE for Route X with invalid attrs If we return early after (B) then the second UPDATE is ignored, leaving us with Route X installed with the earlier (now outdated) valid attrs. The correct response to (B) is to withdraw Route X. Let's not stick our fingers in our ears and pretend the second UPDATE never happened. EnforceFirstAs was also explicitly checking for a missing AS_PATH path attribute, however this was already checked by the parser as a protocol level error (mandatory attribute missing == treat-as-withdraw). So this check has been removed, since we now catch treat-as-withdraw flagged by the parser. --- bgp/src/error.rs | 12 - bgp/src/messages.rs | 29 +++ bgp/src/session.rs | 525 ++++++++++++++++++++++++++++---------------- 3 files changed, 360 insertions(+), 206 deletions(-) diff --git a/bgp/src/error.rs b/bgp/src/error.rs index 922346013..42986c06c 100644 --- a/bgp/src/error.rs +++ b/bgp/src/error.rs @@ -145,18 +145,6 @@ pub enum Error { #[error("Unsupported address family: AFI={0} SAFI={1}")] UnsupportedAddressFamily(u16, u8), - #[error("Self loop detected")] - SelfLoopDetected, - - #[error("AS path missing")] - MissingAsPath, - - #[error("AS path is empty")] - EmptyAsPath, - - #[error("Enforce-first-AS check failed: expected: {0}, found: {1:?}")] - EnforceAsFirst(u32, Vec), - #[error("Invalid address: {0}")] InvalidAddress(String), diff --git a/bgp/src/messages.rs b/bgp/src/messages.rs index 7403106bc..69decfdd4 100644 --- a/bgp/src/messages.rs +++ b/bgp/src/messages.rs @@ -847,6 +847,13 @@ impl UpdateMessage { }) } + /// Mark this update for treat-as-withdraw processing due to a + /// session-level check failure. All NLRI will be treated as + /// withdrawals when this flag is set. + pub fn set_treat_as_withdraw(&mut self, reason: UpdateParseErrorReason) { + self.errors.push((reason, AttributeAction::TreatAsWithdraw)); + } + /// Set the traditional IPv4 withdrawn routes field. pub fn set_withdrawn(&mut self, withdrawn: Vec) { self.withdrawn = withdrawn; @@ -5457,6 +5464,13 @@ pub enum UpdateParseErrorReason { available: usize, }, + // Session-level checks (set after parsing, not during from_wire) + /// Own ASN found in AS_PATH — loop detection (session-level check) + AsLoopDetected, + /// First AS in path does not match eBGP peer AS (session-level + /// check) + EnforceFirstAsFailed { peer_as: u32, first_as: Option }, + // Generic fallback /// Other parse error (avoid if possible; prefer specific variant) Other { detail: String }, @@ -5581,6 +5595,21 @@ impl Display for UpdateParseErrorReason { section, needed, available ) } + Self::AsLoopDetected => { + write!(f, "own ASN found in AS_PATH (loop)") + } + Self::EnforceFirstAsFailed { peer_as, first_as } => { + match first_as { + Some(first) => write!( + f, + "enforce-first-AS failed: peer AS {peer_as}, first AS in path {first}" + ), + None => write!( + f, + "enforce-first-AS failed: peer AS {peer_as}, AS_PATH empty or missing" + ), + } + } Self::Other { detail } => { write!(f, "{}", detail) } diff --git a/bgp/src/session.rs b/bgp/src/session.rs index bd2c943f3..f2938b5e4 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -17,13 +17,17 @@ use crate::{ MessageKind, MessageParseError, MpReachNlri, MpUnreachNlri, NotificationMessage, OpenErrorSubcode, OpenMessage, PathAttribute, PathAttributeValue, RouteRefreshMessage, Safi, UpdateMessage, + UpdateParseErrorReason, }, params::{ BgpCapability, BgpPeerParameters, BgpPeerParametersV1, DynamicTimerInfo, Ipv4UnicastConfig, Ipv6UnicastConfig, JitterRange, PeerCounters, PeerInfo, PeerTimers, StaticTimerInfo, TimerConfig, }, - policy::{CheckerResult, ShaperResult}, + policy::{ + CheckerResult, ShaperResult, check_incoming_open, + check_incoming_update, shape_outgoing_open, shape_outgoing_update, + }, recv_event_loop, recv_event_return, router::Router, unnumbered::UnnumberedManager, @@ -1203,6 +1207,7 @@ pub struct SessionCounters { pub update_nexhop_missing: AtomicU64, pub open_handle_failures: AtomicU64, pub unnegotiated_address_family: AtomicU64, + pub updates_treated_as_withdraw: AtomicU64, // Send failure counters pub notification_send_failure: AtomicU64, @@ -7244,7 +7249,7 @@ impl SessionRunner { PeerId::Ip(ip) => ip, PeerId::Interface(_) => IpAddr::V6(Ipv6Addr::UNSPECIFIED), }; - match crate::policy::check_incoming_open( + match check_incoming_open( om.clone(), checker, remote_asn, @@ -7485,7 +7490,7 @@ impl SessionRunner { PeerId::Ip(ip) => ip, PeerId::Interface(_) => IpAddr::V6(Ipv6Addr::UNSPECIFIED), }; - match crate::policy::shape_outgoing_open( + match shape_outgoing_open( msg.clone(), shaper, peer_as, @@ -7589,7 +7594,7 @@ impl SessionRunner { PeerId::Ip(ip) => ip, PeerId::Interface(_) => IpAddr::V6(Ipv6Addr::UNSPECIFIED), }; - Ok(crate::policy::shape_outgoing_update( + Ok(shape_outgoing_update( update.clone(), shaper, peer_as, @@ -7613,7 +7618,7 @@ impl SessionRunner { }; let former = match previous { - Some(shaper) => crate::policy::shape_outgoing_update( + Some(shaper) => shape_outgoing_update( update.clone(), &shaper, peer_as, @@ -8151,18 +8156,7 @@ impl SessionRunner { mut update: UpdateMessage, pc: &PeerConnection, ) { - if let Err(e) = self.check_update(&update, pc.asn) { - session_log!( - self, - warn, - pc.conn, - "update check failed: {e}"; - "error" => format!("{e}"), - "message" => "update", - "message_contents" => format!("{update}") - ); - return; - } + self.check_update(&mut update, pc.asn); // Filter MP-BGP attributes based on negotiation state. // Attributes for unnegotiated AFI/SAFIs are silently removed. @@ -8187,7 +8181,7 @@ impl SessionRunner { PeerId::Ip(ip) => ip, PeerId::Interface(_) => IpAddr::V6(Ipv6Addr::UNSPECIFIED), }; - match crate::policy::check_incoming_update( + match check_incoming_update( update.clone(), checker, pc.asn, @@ -8292,12 +8286,8 @@ impl SessionRunner { // RFC 4760 §3: Check reserved byte (must be 0, but must be ignored) let reserved = match mp_reach { - crate::messages::MpReachNlri::Ipv4Unicast(inner) => { - inner.reserved - } - crate::messages::MpReachNlri::Ipv6Unicast(inner) => { - inner.reserved - } + MpReachNlri::Ipv4Unicast(inner) => inner.reserved, + MpReachNlri::Ipv6Unicast(inner) => inner.reserved, }; if reserved != 0 { session_log!( @@ -8496,6 +8486,8 @@ impl SessionRunner { /// Update this router's RIB based on an update message from a peer. fn update_rib(&self, update: &UpdateMessage, pc: &PeerConnection) { + let treat_as_withdraw = update.treat_as_withdraw(); + let originated4 = match self.db.get_origin(Some(AddressFamily::Ipv4)) { Ok(value) => value, Err(e) => { @@ -8523,7 +8515,33 @@ impl SessionRunner { self.db .remove_prefixes_from_bgp_peer(&withdrawn, &self.peer_id()); - if let Some(nexthop) = update.nexthop4().map(IpAddr::V4) { + if treat_as_withdraw { + self.counters + .updates_treated_as_withdraw + .fetch_add(1, Ordering::Relaxed); + session_log!( + self, + warn, + pc.conn, + "update marked treat-as-withdraw"; + "errors" => format!("{:?}", update.errors), + "message" => "update", + "message_contents" => format!("{update}") + ); + let nlri_as_withdrawn: Vec = update + .nlri + .iter() + .filter(|p| { + !originated4.contains(&Prefix::V4(**p)) && p.valid_for_rib() + }) + .copied() + .map(Prefix::V4) + .collect(); + self.db.remove_prefixes_from_bgp_peer( + &nlri_as_withdrawn, + &self.peer_id(), + ); + } else if let Some(nexthop) = update.nexthop4().map(IpAddr::V4) { let nlri: Vec = update .nlri .iter() @@ -8582,62 +8600,80 @@ impl SessionRunner { if let Some(reach) = update.mp_reach() { match reach { MpReachNlri::Ipv4Unicast(reach4) => { - let mp_nexthop = match &reach4.nexthop { - BgpNexthop::Ipv4(ip4) => IpAddr::V4(*ip4), - BgpNexthop::Ipv6Single(ip6) => IpAddr::V6(*ip6), - BgpNexthop::Ipv6Double(addrs) => { - if self.is_unnumbered() { - IpAddr::V6(addrs.link_local) - } else { - IpAddr::V6(addrs.global) - } - } - }; - - let mp_nlri4: Vec = reach4 - .nlri - .iter() - .filter(|p| { - !originated4.contains(&Prefix::V4(**p)) - && p.valid_for_rib() - && !self.prefix_via_self( - Prefix::V4(**p), - mp_nexthop, - ) - }) - .copied() - .map(Prefix::V4) - .collect(); - - if !mp_nlri4.is_empty() { - let mut as_path = Vec::new(); - if let Some(segments_list) = update.as_path() { - for segments in &segments_list { - as_path.extend(segments.value.iter()); - } - } - let nexthop_interface = derive_nexthop_interface( + if treat_as_withdraw { + // RFC 7606: treat MP_REACH NLRI as withdrawals + let mp_withdrawn4: Vec = reach4 + .nlri + .iter() + .filter(|p| { + !originated4.contains(&Prefix::V4(**p)) + && p.valid_for_rib() + }) + .copied() + .map(Prefix::V4) + .collect(); + self.db.remove_prefixes_from_bgp_peer( + &mp_withdrawn4, &self.peer_id(), - mp_nexthop, ); - let path4 = rdb::Path { - nexthop: mp_nexthop, - nexthop_interface, - shutdown: update.graceful_shutdown(), - rib_priority: DEFAULT_RIB_PRIORITY_BGP, - bgp: Some(BgpPathProperties { - origin_as: pc.asn, - peer: self.peer_id(), - id: pc.id, - med: update.multi_exit_discriminator(), - local_pref: update.local_pref(), - as_path, - stale: None, - }), - vlan_id: lock!(self.session).vlan_id, + } else { + let mp_nexthop = match &reach4.nexthop { + BgpNexthop::Ipv4(ip4) => IpAddr::V4(*ip4), + BgpNexthop::Ipv6Single(ip6) => IpAddr::V6(*ip6), + BgpNexthop::Ipv6Double(addrs) => { + if self.is_unnumbered() { + IpAddr::V6(addrs.link_local) + } else { + IpAddr::V6(addrs.global) + } + } }; - self.db.add_bgp_prefixes(&mp_nlri4, path4); + let mp_nlri4: Vec = reach4 + .nlri + .iter() + .filter(|p| { + !originated4.contains(&Prefix::V4(**p)) + && p.valid_for_rib() + && !self.prefix_via_self( + Prefix::V4(**p), + mp_nexthop, + ) + }) + .copied() + .map(Prefix::V4) + .collect(); + + if !mp_nlri4.is_empty() { + let mut as_path = Vec::new(); + if let Some(segments_list) = update.as_path() { + for segments in &segments_list { + as_path.extend(segments.value.iter()); + } + } + let nexthop_interface = derive_nexthop_interface( + &self.peer_id(), + mp_nexthop, + ); + let path4 = rdb::Path { + nexthop: mp_nexthop, + nexthop_interface, + shutdown: update.graceful_shutdown(), + rib_priority: DEFAULT_RIB_PRIORITY_BGP, + bgp: Some(BgpPathProperties { + origin_as: pc.asn, + peer: self.peer_id(), + id: pc.id, + med: update.multi_exit_discriminator(), + local_pref: update.local_pref(), + as_path, + stale: None, + }), + vlan_id: lock!(self.session).vlan_id, + }; + + self.db.add_bgp_prefixes(&mp_nlri4, path4); + } } } MpReachNlri::Ipv6Unicast(reach6) => { @@ -8658,69 +8694,91 @@ impl SessionRunner { } }; - let nexthop6 = match &reach6.nexthop { - BgpNexthop::Ipv6Single(ip6) => IpAddr::V6(*ip6), - BgpNexthop::Ipv6Double(addrs) => { - if self.is_unnumbered() { - IpAddr::V6(addrs.link_local) - } else { - IpAddr::V6(addrs.global) + if treat_as_withdraw { + // RFC 7606: treat MP_REACH NLRI as withdrawals + let withdrawn6: Vec = reach6 + .nlri + .iter() + .filter(|p| { + !originated6.contains(&Prefix::V6(**p)) + && p.valid_for_rib() + }) + .copied() + .map(Prefix::V6) + .collect(); + self.db.remove_prefixes_from_bgp_peer( + &withdrawn6, + &self.peer_id(), + ); + } else { + let nexthop6 = match &reach6.nexthop { + BgpNexthop::Ipv6Single(ip6) => IpAddr::V6(*ip6), + BgpNexthop::Ipv6Double(addrs) => { + if self.is_unnumbered() { + IpAddr::V6(addrs.link_local) + } else { + IpAddr::V6(addrs.global) + } } - } - BgpNexthop::Ipv4(ip4) => { - // IPv4 nexthop for IPv6 routes is unusual but possible - // in some configurations (e.g., IPv4-mapped IPv6) - session_log!( - self, - warn, - pc.conn, - "IPv4 nexthop in IPv6 MP_REACH_NLRI"; - "nexthop" => format!("{ip4}") - ); - IpAddr::V4(*ip4) - } - }; - - let nlri6: Vec = reach6 - .nlri - .iter() - .filter(|p| { - !originated6.contains(&Prefix::V6(**p)) - && p.valid_for_rib() - && !self - .prefix_via_self(Prefix::V6(**p), nexthop6) - }) - .copied() - .map(Prefix::V6) - .collect(); - - if !nlri6.is_empty() { - let mut as_path = Vec::new(); - if let Some(segments_list) = update.as_path() { - for segments in &segments_list { - as_path.extend(segments.value.iter()); + BgpNexthop::Ipv4(ip4) => { + // IPv4 nexthop for IPv6 routes is unusual but possible + // in some configurations (e.g., IPv4-mapped IPv6) + session_log!( + self, + warn, + pc.conn, + "IPv4 nexthop in IPv6 MP_REACH_NLRI"; + "nexthop" => format!("{ip4}") + ); + IpAddr::V4(*ip4) } - } - let nexthop_interface = - derive_nexthop_interface(&self.peer_id(), nexthop6); - let path6 = rdb::Path { - nexthop: nexthop6, - nexthop_interface, - shutdown: update.graceful_shutdown(), - rib_priority: DEFAULT_RIB_PRIORITY_BGP, - bgp: Some(BgpPathProperties { - origin_as: pc.asn, - peer: self.peer_id(), - id: pc.id, - med: update.multi_exit_discriminator(), - local_pref: update.local_pref(), - as_path, - stale: None, - }), - vlan_id: lock!(self.session).vlan_id, }; - self.db.add_bgp_prefixes(&nlri6, path6); + let nlri6: Vec = reach6 + .nlri + .iter() + .filter(|p| { + !originated6.contains(&Prefix::V6(**p)) + && p.valid_for_rib() + && !self.prefix_via_self( + Prefix::V6(**p), + nexthop6, + ) + }) + .copied() + .map(Prefix::V6) + .collect(); + + if !nlri6.is_empty() { + let mut as_path = Vec::new(); + if let Some(segments_list) = update.as_path() { + for segments in &segments_list { + as_path.extend(segments.value.iter()); + } + } + let nexthop_interface = derive_nexthop_interface( + &self.peer_id(), + nexthop6, + ); + let path6 = rdb::Path { + nexthop: nexthop6, + nexthop_interface, + shutdown: update.graceful_shutdown(), + rib_priority: DEFAULT_RIB_PRIORITY_BGP, + bgp: Some(BgpPathProperties { + origin_as: pc.asn, + peer: self.peer_id(), + id: pc.id, + med: update.multi_exit_discriminator(), + local_pref: update.local_pref(), + as_path, + stale: None, + }), + vlan_id: lock!(self.session).vlan_id, + }; + + self.db.add_bgp_prefixes(&nlri6, path6); + } } } } @@ -8784,21 +8842,21 @@ impl SessionRunner { } } - /// Perform a set of checks on an update to see if we can accept it. - fn check_update( - &self, - update: &UpdateMessage, - peer_as: u32, - ) -> Result<(), Error> { - // Path vector routing and prefix validation - self.check_for_self_in_path(update)?; + /// Perform session-level checks on an update. Failures mark the + /// update for treat-as-withdraw rather than returning an error. + fn check_update(&self, update: &mut UpdateMessage, peer_as: u32) { + let local_asn = match self.asn { + Asn::TwoOctet(asn) => asn as u32, + Asn::FourOctet(asn) => asn, + }; + check_for_self_in_path(update, local_asn); - // Optional enforce-first-AS validation + // Optional enforce-first-AS validation (eBGP only per + // RFC 4271 Section 6.3) let info = lock!(self.session); - if info.enforce_first_as { - self.enforce_first_as(update, peer_as)?; + if info.enforce_first_as && self.is_ebgp().unwrap_or(false) { + enforce_first_as(update, peer_as); } - Ok(()) } fn apply_static_update_policy(&self, update: &mut UpdateMessage) { @@ -8810,31 +8868,6 @@ impl SessionRunner { } } - /// Do not accept routes that have our ASN in the AS_PATH e.g., do - /// path-vector routing not distance-vector routing. - fn check_for_self_in_path( - &self, - update: &UpdateMessage, - ) -> Result<(), Error> { - let asn = match self.asn { - Asn::TwoOctet(asn) => asn as u32, - Asn::FourOctet(asn) => asn, - }; - for pa in &update.path_attributes { - let path = match &pa.value { - PathAttributeValue::AsPath(segments) => segments, - PathAttributeValue::As4Path(segments) => segments, - _ => continue, - }; - for segment in path { - if segment.value.contains(&asn) { - return Err(Error::SelfLoopDetected); - } - } - } - Ok(()) - } - /// Do not accept routes advertised with themselves as the next-hop. /// e.g. /// Do not allow 2001::1/32 with a nexthop of 2001::1 @@ -8850,27 +8883,6 @@ impl SessionRunner { } } - fn enforce_first_as( - &self, - update: &UpdateMessage, - peer_as: u32, - ) -> Result<(), Error> { - let path = match update.as_path() { - Some(path) => path, - None => return Err(Error::MissingAsPath), - }; - let path: Vec = path.into_iter().flat_map(|x| x.value).collect(); - if path.is_empty() { - return Err(Error::EmptyAsPath); - } - - if path[0] != peer_as { - return Err(Error::EnforceAsFirst(peer_as, path)); - } - - Ok(()) - } - /// Return the current BGP peer state of this session runner. pub fn state(&self) -> FsmStateKind { *lock!(self.state) @@ -9376,6 +9388,59 @@ impl From for MessageHistoryV1 { } } +/// Mark the update for treat-as-withdraw if `local_asn` appears in the +/// AS_PATH (path-vector loop detection). +pub(crate) fn check_for_self_in_path( + update: &mut UpdateMessage, + local_asn: u32, +) { + for pa in &update.path_attributes { + let path = match &pa.value { + PathAttributeValue::AsPath(segments) => segments, + PathAttributeValue::As4Path(segments) => segments, + _ => continue, + }; + for segment in path { + if segment.value.contains(&local_asn) { + update.set_treat_as_withdraw( + UpdateParseErrorReason::AsLoopDetected, + ); + return; + } + } + } +} + +/// Mark the update for treat-as-withdraw if the first AS in the path +/// does not match `peer_as` (RFC 4271 Section 6.3). +/// +/// A missing AS_PATH attribute is not checked here — the parser already +/// flags that as TreatAsWithdraw (mandatory attribute validation). +pub(crate) fn enforce_first_as(update: &mut UpdateMessage, peer_as: u32) { + let Some(path) = update.as_path() else { + return; + }; + let path: Vec = path.into_iter().flat_map(|x| x.value).collect(); + if path.is_empty() { + update.set_treat_as_withdraw( + UpdateParseErrorReason::EnforceFirstAsFailed { + peer_as, + first_as: None, + }, + ); + return; + } + + if path[0] != peer_as { + update.set_treat_as_withdraw( + UpdateParseErrorReason::EnforceFirstAsFailed { + peer_as, + first_as: Some(path[0]), + }, + ); + } +} + #[cfg(test)] mod tests { use super::*; @@ -9738,4 +9803,76 @@ mod tests { // Should error because cannot derive IPv6 nexthop from IPv4 connection assert!(result.is_err()); } + + // ================================================================ + // check_for_self_in_path / enforce_first_as unit tests + // ================================================================ + + use crate::messages::{As4PathSegment, AsPathType}; + + /// Build an UpdateMessage with an AS4_PATH containing a single + /// AS_SEQUENCE segment with the given ASNs. + fn make_update_with_as_path(asns: &[u32]) -> UpdateMessage { + let path_attr = PathAttribute::from(PathAttributeValue::As4Path(vec![ + As4PathSegment { + typ: AsPathType::AsSequence, + value: asns.to_vec(), + }, + ])); + UpdateMessage { + withdrawn: Vec::new(), + path_attributes: vec![path_attr], + nlri: Vec::new(), + errors: Vec::new(), + } + } + + #[test] + fn self_in_path_sets_treat_as_withdraw() { + let mut update = make_update_with_as_path(&[65001, 65002]); + check_for_self_in_path(&mut update, 65002); + assert!(update.treat_as_withdraw()); + } + + #[test] + fn self_not_in_path_leaves_update_clean() { + let mut update = make_update_with_as_path(&[65001, 65002]); + check_for_self_in_path(&mut update, 65003); + assert!(!update.treat_as_withdraw()); + } + + #[test] + fn enforce_first_as_match_leaves_update_clean() { + let mut update = make_update_with_as_path(&[65001, 65002]); + enforce_first_as(&mut update, 65001); + assert!(!update.treat_as_withdraw()); + } + + #[test] + fn enforce_first_as_mismatch_sets_treat_as_withdraw() { + let mut update = make_update_with_as_path(&[65001, 65002]); + enforce_first_as(&mut update, 65099); + assert!(update.treat_as_withdraw()); + } + + #[test] + fn enforce_first_as_empty_path_sets_treat_as_withdraw() { + let mut update = make_update_with_as_path(&[]); + enforce_first_as(&mut update, 65001); + assert!(update.treat_as_withdraw()); + } + + #[test] + fn enforce_first_as_missing_path_is_noop() { + // Missing AS_PATH is handled by the parser's mandatory attribute + // validation, not by enforce_first_as. + let mut update = UpdateMessage { + withdrawn: Vec::new(), + path_attributes: Vec::new(), + nlri: Vec::new(), + errors: Vec::new(), + }; + enforce_first_as(&mut update, 65001); + assert!(!update.treat_as_withdraw()); + } } From 23b66f65268366e90c29168dbad711e820a94a6e Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Fri, 13 Feb 2026 21:11:44 -0700 Subject: [PATCH 13/58] Stop walking RIBs for AFIs that weren't negotiated There were multiple places where we kicked off a RIB walker as a result of an event pertaining to a BGP peer which unconditionally locked and walked the RIBs for both IPv4 and IPv6. Update these spots to only walk the RIBs for address-families that have been negotiated with the peer. --- bgp/src/session.rs | 25 +++++++++++++++++++++---- mgd/src/bgp_admin.rs | 3 ++- rdb/src/db.rs | 31 +++++++++++++++++++++---------- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/bgp/src/session.rs b/bgp/src/session.rs index f2938b5e4..3c670633a 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -6545,7 +6545,19 @@ impl SessionRunner { } AdminEvent::EnforceFirstAsEnabled => { - self.db.enforce_first_as(&self.peer_id()); + let peer_id = self.peer_id(); + if pc.ipv4_unicast.negotiated() { + self.db.enforce_first_as( + &peer_id, + Some(AddressFamily::Ipv4), + ); + } + if pc.ipv6_unicast.negotiated() { + self.db.enforce_first_as( + &peer_id, + Some(AddressFamily::Ipv6), + ); + } FsmState::Established(pc) } @@ -7961,13 +7973,18 @@ impl SessionRunner { let peer_id = self.peer_id(); if pc.ipv4_unicast.negotiated() { write_lock!(self.fanout4).remove_egress(&peer_id); + self.db.remove_all_prefixes_from_bgp_peer( + &peer_id, + Some(AddressFamily::Ipv4), + ); } if pc.ipv6_unicast.negotiated() { write_lock!(self.fanout6).remove_egress(&peer_id); + self.db.remove_all_prefixes_from_bgp_peer( + &peer_id, + Some(AddressFamily::Ipv6), + ); } - - // remove peer prefixes from db - self.db.remove_all_prefixes_from_bgp_peer(&self.peer_id()); } /// Exit the established state into Idle. diff --git a/mgd/src/bgp_admin.rs b/mgd/src/bgp_admin.rs index f896930f5..a291c59f6 100644 --- a/mgd/src/bgp_admin.rs +++ b/mgd/src/bgp_admin.rs @@ -1953,7 +1953,8 @@ pub(crate) mod helpers { ) -> Result { bgp_log!(ctx.log, info, "remove neighbor (addr {addr}, asn {asn})"); - ctx.db.remove_all_prefixes_from_bgp_peer(&PeerId::Ip(addr)); + ctx.db + .remove_all_prefixes_from_bgp_peer(&PeerId::Ip(addr), None); ctx.db.remove_bgp_neighbor(addr)?; get_router!(&ctx, asn)?.delete_session(addr); diff --git a/rdb/src/db.rs b/rdb/src/db.rs index d1daf0db7..4b6455c80 100644 --- a/rdb/src/db.rs +++ b/rdb/src/db.rs @@ -1210,18 +1210,24 @@ impl Db { /// Remove all BGP paths from a peer. /// Used when a peer is deleted or exits Established state. - pub fn remove_all_prefixes_from_bgp_peer(&self, peer: &PeerId) { - let all_prefixes: Vec<_> = - self.full_rib(None).keys().copied().collect(); + /// When `af` is `Some`, only prefixes of that address family + /// are examined; when `None`, both address families are walked. + pub fn remove_all_prefixes_from_bgp_peer( + &self, + peer: &PeerId, + af: Option, + ) { + let all_prefixes: Vec<_> = self.full_rib(af).keys().copied().collect(); self.remove_prefixes_from_bgp_peer(&all_prefixes, peer); } /// Remove BGP paths from a peer that violate the first-AS rule. /// A path violates first-AS when its AS path is empty or when /// the first AS in the path does not match the peer's origin AS. - pub fn enforce_first_as(&self, peer: &PeerId) { - let all_prefixes: Vec<_> = - self.full_rib(None).keys().copied().collect(); + /// When `af` is `Some`, only prefixes of that address family + /// are examined; when `None`, both address families are walked. + pub fn enforce_first_as(&self, peer: &PeerId, af: Option) { + let all_prefixes: Vec<_> = self.full_rib(af).keys().copied().collect(); let changed = self.remove_path_for_prefixes(&all_prefixes, |path: &Path| { match path.bgp { @@ -1651,7 +1657,10 @@ mod test { assert!(check_prefix_path(&db, &p2, rib_in_paths, loc_rib_paths)); // yank all routes from bgp_path0, simulating peer shutdown - db.remove_all_prefixes_from_bgp_peer(&bgp_path0.bgp.unwrap().peer); + db.remove_all_prefixes_from_bgp_peer( + &bgp_path0.bgp.unwrap().peer, + None, + ); // expected current state // rib_in: // - p0 via static_path1 @@ -1675,6 +1684,7 @@ mod test { // bgp_path2 should be unaffected, despite also having the same RID db.remove_all_prefixes_from_bgp_peer( &bgp_path2.clone().bgp.unwrap().peer, + None, ); // expected current state // rib_in: @@ -1697,6 +1707,7 @@ mod test { // p0 should be unaffected, still retaining the static path db.remove_all_prefixes_from_bgp_peer( &bgp_path1.clone().bgp.unwrap().peer, + None, ); // expected current state // rib_in: @@ -2600,7 +2611,7 @@ mod test { assert_eq!(db.get_prefix_paths(&p6).len(), 1); // Remove paths from `peer` that violate first-AS - db.enforce_first_as(&peer); + db.enforce_first_as(&peer, None); // p4: good_path (passes check) + other_peer_path (different // peer) should remain @@ -2660,7 +2671,7 @@ mod test { db.add_bgp_prefixes(&[prefix], empty_as_path); assert_eq!(db.get_prefix_paths(&prefix).len(), 1); - db.enforce_first_as(&peer); + db.enforce_first_as(&peer, None); assert!( db.get_prefix_paths(&prefix).is_empty(), @@ -2705,7 +2716,7 @@ mod test { assert_eq!(db.get_prefix_paths(&prefix).len(), 1); // All paths pass the first-AS check — nothing removed - db.enforce_first_as(&peer); + db.enforce_first_as(&peer, None); assert_eq!( db.get_prefix_paths(&prefix).len(), From 4ed7c9599d2159567abe6ca2780c9802f94a6bcd Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Fri, 20 Feb 2026 12:50:19 -0700 Subject: [PATCH 14/58] bgp: handle failed AFI/SAFI negotiation better Add logic to send a NOTIFICATION if we can't find common ground with our BGP peer. If the peer sends us MP capabilities, then we treat the AFIs they send us as an exhaustive list of what they're willing to exchange. If the peer doesn't send us MP capabilities, then we treat them as a legacy peer that supports only IPv4 Unicast and only with traditional encoding. If we haven't enabled IPv4 Unicast and our peer doesn't support MP-BGP, then we do not have any AFIs in common and we'll reset the session. Also adds a basic integration test to ensure that two peers with non overlapping AFI/SAFIs never move into Established. --- bgp/src/error.rs | 3 + bgp/src/session.rs | 375 ++++++++++++++++++++++++++++++--------------- bgp/src/test.rs | 104 +++++++++++++ 3 files changed, 359 insertions(+), 123 deletions(-) diff --git a/bgp/src/error.rs b/bgp/src/error.rs index 42986c06c..8726ee4d4 100644 --- a/bgp/src/error.rs +++ b/bgp/src/error.rs @@ -160,6 +160,9 @@ pub enum Error { #[error("Hold time too small")] HoldTimeTooSmall, + #[error("No compatible address families")] + NoCompatibleAddressFamilies, + #[error("Invalid NLRI prefix: {0:?}")] InvalidNlriPrefix(Vec), diff --git a/bgp/src/session.rs b/bgp/src/session.rs index 3c670633a..047087e5e 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -120,6 +120,13 @@ pub struct PeerConnection { pub ipv4_unicast: AfiSafiState, /// This peer's AFI/SAFI state for IPv6 Unicast pub ipv6_unicast: AfiSafiState, + /// Indicates the peer supports MP-BGP, i.e. they advertised + /// Multiprotocol Extensions capabilities in their OPEN message. + /// True: the peer supports MP-BGP encoding. + /// False: the peer is a legacy BGP-4 speaker that does not support MP-BGP + /// encoding. IPv4 Unicast is implicitly negotiated, and is the only + /// address-family allowed for this peer. + pub mp_bgp: bool, } impl Clone for PeerConnection { @@ -131,6 +138,7 @@ impl Clone for PeerConnection { caps: self.caps.clone(), ipv4_unicast: self.ipv4_unicast, ipv6_unicast: self.ipv6_unicast, + mp_bgp: self.mp_bgp, } } } @@ -1299,40 +1307,58 @@ macro_rules! connect_timeout { }; } -/// Determines AFI/SAFI state based on capability negotiation. +/// Determine AFI/SAFI negotiation state from two capability sets. /// -/// Returns (ipv4_unicast, ipv6_unicast) states: -/// - Negotiated: Both sides advertised the capability -/// - Advertised: We advertised but peer did not -/// - Unconfigured: We did not advertise (not configured) -macro_rules! active_afi { - ($self:expr, $their_caps:ident) => {{ - let cap4 = Capability::ipv4_unicast(); - let cap6 = Capability::ipv6_unicast(); - let our_caps = lock!($self.caps_tx); - - let ipv4_state = if our_caps.contains(&cap4) { - if $their_caps.contains(&cap4) { +/// Returns `(ipv4_unicast, ipv6_unicast, mp_bgp)`: +/// - `Negotiated`: Both sides advertised the capability, **or** the peer +/// is a legacy (non-MP-BGP) speaker and we configured IPv4 Unicast +/// (implicit negotiation per RFC 4271). +/// - `Advertised`: We advertised but peer did not (and no implicit grant). +/// - `Unconfigured`: We did not advertise (not configured). +/// - `mp_bgp`: `true` if the peer advertised any +/// `MultiprotocolExtensions` capabilities. +pub(crate) fn negotiate_afis( + our_caps: &BTreeSet, + their_caps: &BTreeSet, +) -> (AfiSafiState, AfiSafiState, bool) { + let cap4 = Capability::ipv4_unicast(); + let cap6 = Capability::ipv6_unicast(); + + // A peer is an MP-BGP speaker if they advertised any + // MultiprotocolExtensions capabilities. + let mp_bgp = their_caps + .iter() + .any(|c| matches!(c, Capability::MultiprotocolExtensions { .. })); + + let ipv4_state = if our_caps.contains(&cap4) { + if mp_bgp { + // Peer sent MP-BGP caps: check for explicit v4 + if their_caps.contains(&cap4) { AfiSafiState::Negotiated } else { AfiSafiState::Advertised } } else { - AfiSafiState::Unconfigured - }; + // Legacy peer (no MP-BGP caps): implicitly + // negotiates IPv4 Unicast per RFC 4271. + AfiSafiState::Negotiated + } + } else { + AfiSafiState::Unconfigured + }; - let ipv6_state = if our_caps.contains(&cap6) { - if $their_caps.contains(&cap6) { - AfiSafiState::Negotiated - } else { - AfiSafiState::Advertised - } + let ipv6_state = if our_caps.contains(&cap6) { + if their_caps.contains(&cap6) { + AfiSafiState::Negotiated } else { - AfiSafiState::Unconfigured - }; + // Legacy peers never get implicit IPv6. + AfiSafiState::Advertised + } + } else { + AfiSafiState::Unconfigured + }; - (ipv4_state, ipv6_state) - }}; + (ipv4_state, ipv6_state, mp_bgp) } /// Registry for tracking active connections @@ -3840,7 +3866,10 @@ impl SessionRunner { conn_timer!(conn, hold).enable(); let caps = om.get_capabilities(); - let (ipv4_unicast, ipv6_unicast) = active_afi!(self, caps); + let (ipv4_unicast, ipv6_unicast, mp_bgp) = { + let our_caps = lock!(self.caps_tx); + negotiate_afis(&our_caps, &caps) + }; let pc = PeerConnection { conn, @@ -3849,6 +3878,7 @@ impl SessionRunner { caps, ipv4_unicast, ipv6_unicast, + mp_bgp, }; // Upgrade this connection from Partial to Full in the registry @@ -5192,7 +5222,10 @@ impl SessionRunner { .fetch_add(1, Ordering::Relaxed); let caps = om.get_capabilities(); - let (ipv4_unicast, ipv6_unicast) = active_afi!(self, caps); + let (ipv4_unicast, ipv6_unicast, mp_bgp) = { + let our_caps = lock!(self.caps_tx); + negotiate_afis(&our_caps, &caps) + }; let new_pc = PeerConnection { conn: new, @@ -5201,6 +5234,7 @@ impl SessionRunner { caps, ipv4_unicast, ipv6_unicast, + mp_bgp, }; conn_timer!(new_pc.conn, hold).restart(); @@ -5504,7 +5538,10 @@ impl SessionRunner { conn_timer!(exist, keepalive).restart(); let caps = om.get_capabilities(); - let (ipv4_unicast, ipv6_unicast) = active_afi!(self, caps); + let (ipv4_unicast, ipv6_unicast, mp_bgp) = { + let our_caps = lock!(self.caps_tx); + negotiate_afis(&our_caps, &caps) + }; let exist_pc = PeerConnection { conn: exist.clone(), @@ -5513,6 +5550,7 @@ impl SessionRunner { caps, ipv4_unicast, ipv6_unicast, + mp_bgp, }; // Upgrade existing connection from Partial to Full in the registry @@ -5636,7 +5674,10 @@ impl SessionRunner { self.stop(Some(&exist), None, StopReason::CollisionResolution); let caps = om.get_capabilities(); - let (ipv4_unicast, ipv6_unicast) = active_afi!(self, caps); + let (ipv4_unicast, ipv6_unicast, mp_bgp) = { + let our_caps = lock!(self.caps_tx); + negotiate_afis(&our_caps, &caps) + }; let new_pc = PeerConnection { conn: new.clone(), @@ -5645,6 +5686,7 @@ impl SessionRunner { caps, ipv4_unicast, ipv6_unicast, + mp_bgp, }; conn_timer!(new, hold).restart(); @@ -6824,7 +6866,7 @@ impl SessionRunner { // deciding factor when choosing a connection // to retain, but only if an Open isn't // received on the new connection until - // after . + // after the old connection goes Established. // if true: // Collision resolution is performed based // on the BGP-ID of each peer as per the @@ -6837,23 +6879,24 @@ impl SessionRunner { // // Note: This is not a full implementation of // CollisionDetectEstablishedState. - // - // Rather, it is simply a lever for us to - // choose whether to ensure determinism in - // collision resolution (i.e. by forcing the - // use of BGP-ID as tie-breaker) or not - // (sticking to "first to Established wins") - // for the scenario described above. A full - // implementation would involve registration - // and tracking of new connections that complete - // while in Established (likely warranting - // an additional CollisionPair variant and - // collision_detection_* method) and adding - // full handling for connections to go into - // and out of Established while a collision is - // underway. At the time of writing this, a full - // implementation is not believed to be worth - // the added complexity and maintenance burden. + // Rather, it is simply a lever for us to + // choose whether to ensure determinism in + // collision resolution (i.e. by forcing + // the use of BGP-ID as tie-breaker) or + // not (sticking to "first to Established + // wins") for the scenario described + // above. A full implementation would + // involve registration and tracking of + // new connections that complete while + // in Established (likely warranting an + // additional CollisionPair variant and + // collision_detection_* method) and + // adding full handling for connections + // to go into and out of Established while + // a collision is underway. At the time + // of writing this, a full implementation + // is not believed to be worth the added + // complexity and maintenance burden. if lock!(self.session) .deterministic_collision_resolution { @@ -6894,8 +6937,15 @@ impl SessionRunner { ); let caps = om.get_capabilities(); - let (ipv4_unicast, ipv6_unicast) = - active_afi!(self, caps); + let ( + ipv4_unicast, + ipv6_unicast, + mp_bgp, + ) = { + let our_caps = + lock!(self.caps_tx); + negotiate_afis(&our_caps, &caps) + }; let new_pc = PeerConnection { conn: incoming_conn.clone(), @@ -6904,6 +6954,7 @@ impl SessionRunner { caps, ipv4_unicast, ipv6_unicast, + mp_bgp, }; // Clean up the old established connection @@ -7256,6 +7307,31 @@ impl SessionRunner { got: remote_asn, })); } + + // Verify at least one address family is compatible. This + // covers both MP-BGP peers with disjoint AFIs and legacy + // (non-MP-BGP) peers when we don't have IPv4 configured. + let their_caps = om.get_capabilities(); + let our_caps = lock!(self.caps_tx); + let (ipv4, ipv6, _mp_bgp) = negotiate_afis(&our_caps, &their_caps); + drop(our_caps); + + if !ipv4.negotiated() && !ipv6.negotiated() { + session_log!( + self, + warn, + conn, + "no compatible address families negotiated"; + ); + self.send_notification( + conn, + ErrorCode::Open, + ErrorSubcode::Open(OpenErrorSubcode::UnsupportedCapability), + ); + self.unregister_conn(conn.id()); + return Err(Error::NoCompatibleAddressFamilies); + } + if let Some(checker) = read_lock!(self.router.policy.checker).as_ref() { let peer_ip = match self.neighbor.peer { PeerId::Ip(ip) => ip, @@ -7324,6 +7400,7 @@ impl SessionRunner { kt.restart(); } } + Ok(()) } @@ -8288,6 +8365,38 @@ impl SessionRunner { update: &mut UpdateMessage, pc: &PeerConnection, ) -> Result<(), Error> { + // A legacy peer must only use traditional encoding. + // Strip any MP-BGP attributes they send -- they have no + // business using multiprotocol extensions. + if !pc.mp_bgp { + let has_mp_attrs = update.path_attributes.iter().any(|a| { + matches!( + a.value, + PathAttributeValue::MpReachNlri(_) + | PathAttributeValue::MpUnreachNlri(_) + ) + }); + if has_mp_attrs { + session_log!( + self, + warn, + pc.conn, + "stripping MP-BGP attributes from non-MP-BGP peer"; + ); + self.counters + .unnegotiated_address_family + .fetch_add(1, Ordering::Relaxed); + update.path_attributes.retain(|a| { + !matches!( + a.value, + PathAttributeValue::MpReachNlri(_) + | PathAttributeValue::MpUnreachNlri(_) + ) + }); + } + return Ok(()); + } + // We'll rebuild the attributes list, filtering out unnegotiated AFI/SAFIs. // Note: AFI/SAFI and NLRI validation happens during parsing (from_wire), // so we only need to check negotiation state here. @@ -8519,34 +8628,11 @@ impl SessionRunner { } }; - let withdrawn: Vec = update - .withdrawn - .iter() - .filter(|p| { - !originated4.contains(&Prefix::V4(**p)) && p.valid_for_rib() - }) - .copied() - .map(Prefix::V4) - .collect(); - - self.db - .remove_prefixes_from_bgp_peer(&withdrawn, &self.peer_id()); - - if treat_as_withdraw { - self.counters - .updates_treated_as_withdraw - .fetch_add(1, Ordering::Relaxed); - session_log!( - self, - warn, - pc.conn, - "update marked treat-as-withdraw"; - "errors" => format!("{:?}", update.errors), - "message" => "update", - "message_contents" => format!("{update}") - ); - let nlri_as_withdrawn: Vec = update - .nlri + // Traditional NLRI / withdrawn routes fields are IPv4 Unicast + // only. Only process them if IPv4 Unicast was negotiated. + if pc.ipv4_unicast.negotiated() { + let withdrawn: Vec = update + .withdrawn .iter() .filter(|p| { !originated4.contains(&Prefix::V4(**p)) && p.valid_for_rib() @@ -8554,65 +8640,108 @@ impl SessionRunner { .copied() .map(Prefix::V4) .collect(); - self.db.remove_prefixes_from_bgp_peer( - &nlri_as_withdrawn, - &self.peer_id(), - ); - } else if let Some(nexthop) = update.nexthop4().map(IpAddr::V4) { - let nlri: Vec = update - .nlri - .iter() - .filter(|p| { - !originated4.contains(&Prefix::V4(**p)) - && p.valid_for_rib() - && !self.prefix_via_self(Prefix::V4(**p), nexthop) - }) - .copied() - .map(Prefix::V4) - .collect(); - if !nlri.is_empty() { - let mut as_path = Vec::new(); - if let Some(segments_list) = update.as_path() { - for segments in &segments_list { - as_path.extend(segments.value.iter()); + self.db + .remove_prefixes_from_bgp_peer(&withdrawn, &self.peer_id()); + + if treat_as_withdraw { + self.counters + .updates_treated_as_withdraw + .fetch_add(1, Ordering::Relaxed); + session_log!( + self, + warn, + pc.conn, + "update marked treat-as-withdraw"; + "errors" => format!("{:?}", update.errors), + "message" => "update", + "message_contents" => format!("{update}") + ); + let nlri_as_withdrawn: Vec = update + .nlri + .iter() + .filter(|p| { + !originated4.contains(&Prefix::V4(**p)) + && p.valid_for_rib() + }) + .copied() + .map(Prefix::V4) + .collect(); + self.db.remove_prefixes_from_bgp_peer( + &nlri_as_withdrawn, + &self.peer_id(), + ); + } else if let Some(nexthop) = update.nexthop4().map(IpAddr::V4) { + let nlri: Vec = update + .nlri + .iter() + .filter(|p| { + !originated4.contains(&Prefix::V4(**p)) + && p.valid_for_rib() + && !self.prefix_via_self(Prefix::V4(**p), nexthop) + }) + .copied() + .map(Prefix::V4) + .collect(); + + if !nlri.is_empty() { + let mut as_path = Vec::new(); + if let Some(segments_list) = update.as_path() { + for segments in &segments_list { + as_path.extend(segments.value.iter()); + } } - } - let nexthop_interface = - derive_nexthop_interface(&self.peer_id(), nexthop); - let path = rdb::Path { - nexthop, - nexthop_interface, - shutdown: update.graceful_shutdown(), - rib_priority: DEFAULT_RIB_PRIORITY_BGP, - bgp: Some(BgpPathProperties { - origin_as: pc.asn, - peer: self.peer_id(), - id: pc.id, - med: update.multi_exit_discriminator(), - local_pref: update.local_pref(), - as_path, - stale: None, - }), - vlan_id: lock!(self.session).vlan_id, - }; + let nexthop_interface = + derive_nexthop_interface(&self.peer_id(), nexthop); + let path = rdb::Path { + nexthop, + nexthop_interface, + shutdown: update.graceful_shutdown(), + rib_priority: DEFAULT_RIB_PRIORITY_BGP, + bgp: Some(BgpPathProperties { + origin_as: pc.asn, + peer: self.peer_id(), + id: pc.id, + med: update.multi_exit_discriminator(), + local_pref: update.local_pref(), + as_path, + stale: None, + }), + vlan_id: lock!(self.session).vlan_id, + }; - self.db.add_bgp_prefixes(&nlri, path.clone()); + self.db.add_bgp_prefixes(&nlri, path.clone()); + } + } else if !update.nlri.is_empty() { + session_log!( + self, + warn, + pc.conn, + "update has traditional nlri encoding but no nexthop4"; + "message" => "update", + "message_contents" => format!("{update}").as_str() + ); + self.counters + .update_nexhop_missing + .fetch_add(1, Ordering::Relaxed); } - } else if !update.nlri.is_empty() { + } else if !update.withdrawn.is_empty() || !update.nlri.is_empty() { session_log!( self, warn, pc.conn, - "update has traditional nlri encoding but no nexthop4"; - "message" => "update", - "message_contents" => format!("{update}").as_str() + "ignoring traditional NLRI/withdrawn: IPv4 Unicast not negotiated"; ); self.counters - .update_nexhop_missing + .unnegotiated_address_family .fetch_add(1, Ordering::Relaxed); } + // Non-MP-BGP peers only use traditional NLRI (handled above). + if !pc.mp_bgp { + return; + } + // Process MP_REACH_NLRI for IPv4 and IPv6 routes if let Some(reach) = update.mp_reach() { match reach { diff --git a/bgp/src/test.rs b/bgp/src/test.rs index 8923bbc00..a48eec8b0 100644 --- a/bgp/src/test.rs +++ b/bgp/src/test.rs @@ -4550,3 +4550,107 @@ fn test_enforce_first_as_graceful_toggle() { r1.shutdown(); r2.shutdown(); } + +// ============================================================================= +// AFI mismatch rejection test +// ============================================================================= + +/// When two MP-BGP peers have no overlapping address families, neither +/// session should reach Established. Both sides should increment their +/// `open_handle_failures` counter. +#[test] +fn test_mismatched_afi_session_rejected() { + let r1_addr: SocketAddr = sockaddr!(&format!("127.0.0.16:{TEST_BGP_PORT}")); + let r2_addr: SocketAddr = sockaddr!(&format!("127.0.0.17:{TEST_BGP_PORT}")); + + let _ip_guard = ensure_loop_ips(&[r1_addr.ip(), r2_addr.ip()]); + + // r1: IPv4 only, r2: IPv6 only → no common AFI + let routers = vec![ + LogicalRouter { + name: "r1".to_string(), + asn: Asn::FourOctet(4200000001), + id: 1, + listen_addr: r1_addr, + bind_addr: Some(r1_addr), + neighbors: vec![NeighborConfig { + peer_name: "r2".to_string(), + remote_host: r2_addr, + session_info: create_test_session_info( + RouteExchange::Ipv4 { nexthop: None }, + r1_addr, + r2_addr, + false, + ), + }], + }, + LogicalRouter { + name: "r2".to_string(), + asn: Asn::FourOctet(4200000002), + id: 2, + listen_addr: r2_addr, + bind_addr: Some(r2_addr), + neighbors: vec![NeighborConfig { + peer_name: "r1".to_string(), + remote_host: r1_addr, + session_info: create_test_session_info( + RouteExchange::Ipv6 { nexthop: None }, + r2_addr, + r1_addr, + false, + ), + }], + }, + ]; + + let (test_routers, _ip_guard2) = test_setup::< + BgpConnectionTcp, + BgpListenerTcp, + >("mismatched_afi", &routers); + + let r1 = &test_routers[0]; + let r2 = &test_routers[1]; + + let r1_session = r1 + .router + .get_session(r2_addr.ip()) + .expect("get r1->r2 session"); + let r2_session = r2 + .router + .get_session(r1_addr.ip()) + .expect("get r2->r1 session"); + + // Wait long enough for several connect-retry cycles. If the FSM were going + // to incorrectly establish, it will happen during this period. + let start = Instant::now(); + while start.elapsed() < CONNECT_RETRY_VERIFICATION { + assert_ne!( + r1_session.state(), + FsmStateKind::Established, + "r1 must not reach Established with disjoint AFIs" + ); + assert_ne!( + r2_session.state(), + FsmStateKind::Established, + "r2 must not reach Established with disjoint AFIs" + ); + } + + // At least one side should have recorded an open_handle_failure. + let r1_failures = r1_session + .counters + .open_handle_failures + .load(Ordering::Relaxed); + let r2_failures = r2_session + .counters + .open_handle_failures + .load(Ordering::Relaxed); + assert!( + r1_failures > 0 || r2_failures > 0, + "at least one side should record open_handle_failures \ + (r1={r1_failures}, r2={r2_failures})" + ); + + r1.shutdown(); + r2.shutdown(); +} From 8daf8ab0de4292df8e0a58b3ede21122f500a564 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Fri, 20 Feb 2026 18:30:54 -0700 Subject: [PATCH 15/58] bgp: validate OPEN received in Established Adds a missing call to handle_open() in fsm_established(). This covers the case where we've enabled deterministic_collision_resolution and we get an OPEN while we're in Established. The OPEN still needs to be validated here too. --- bgp/src/session.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/bgp/src/session.rs b/bgp/src/session.rs index 047087e5e..b79e03f0d 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -6900,7 +6900,30 @@ impl SessionRunner { if lock!(self.session) .deterministic_collision_resolution { - // Determine which connection wins using pure function + // Make sure the OPEN is valid before we + // consider it for collision resolution. + if let Err(e) = + self.handle_open(&incoming_conn, om) + { + session_log!( + self, + warn, + incoming_conn, + "collision in established: failed to handle open ({e})"; + "error" => format!("{e}") + ); + self.counters + .open_handle_failures + .fetch_add(1, Ordering::Relaxed); + self.stop( + Some(&incoming_conn), + None, + StopReason::ConnectionRejected, + ); + return FsmState::Established(pc); + } + + // Determine which connection wins let resolution = collision_resolution( pc.conn.direction(), om.id, From 972f84650d5f774bc6861925410c7d1261795ae2 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Fri, 20 Feb 2026 18:59:26 -0700 Subject: [PATCH 16/58] bgp: move most fsm events into "major" history Updates is_major_event() to categorize more FSM events as major so they don't live just in the "all" buffer. Now the only events that are not major are Keepalives, ConnectRetryTimerExpires, IdleHoldTimerExpires, and KeepaliveTimerExpires. Every other message or event is significant since they're either low frequency or high importance (triggering an FSM state change, changing a connection state, updating the RIB, etc.) --- bgp/src/session.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/bgp/src/session.rs b/bgp/src/session.rs index b79e03f0d..6c9b58ce4 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -2136,16 +2136,26 @@ impl SessionRunner { } /// Determine if event is "major" (should be in major buffer) - /// Major events: all admin events, new TCP connections, state transitions + /// + /// Non-major events are high-frequency timer/keepalive noise: + /// connect retry timer, idle hold timer, keepalive timer, and + /// received keepalive messages. Everything else is major. fn is_major_event(event: &FsmEvent) -> bool { match event { - FsmEvent::Admin(_) => true, // All admin events are major + FsmEvent::Admin(_) => true, FsmEvent::Session(se) => matches!( se, SessionEvent::TcpConnectionAcked(_) | SessionEvent::TcpConnectionConfirmed(_) ), - FsmEvent::Connection(_) => false, // Major only if causes state transition + FsmEvent::Connection(ce) => !matches!( + ce, + ConnectionEvent::KeepaliveTimerExpires(_) + | ConnectionEvent::Message { + msg: Message::KeepAlive, + .. + } + ), } } From 634dc331937351fe589fd35eebe94886856f8ae9 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Fri, 20 Feb 2026 19:47:42 -0700 Subject: [PATCH 17/58] Split BGP Message History into major/all buffers Plumb major/all buffers through the BGP Message History, mirroring the major/all buffer split of the BGP FSM History. In this case, the only message type that doesn't live in the "major" buffer is keepalives. --- bgp/src/session.rs | 170 +++++++++++++----- mg-api/src/lib.rs | 68 +++++-- mgadm/src/bgp.rs | 145 ++++++++------- mgd/src/admin.rs | 7 + mgd/src/bgp_admin.rs | 123 +++++++++---- ...693bed.json => mg-admin-7.0.0-837dad.json} | 80 +++++---- openapi/mg-admin/mg-admin-latest.json | 2 +- 7 files changed, 407 insertions(+), 188 deletions(-) rename openapi/mg-admin/{mg-admin-7.0.0-693bed.json => mg-admin-7.0.0-837dad.json} (99%) diff --git a/bgp/src/session.rs b/bgp/src/session.rs index 6c9b58ce4..785129480 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -1043,44 +1043,69 @@ impl Clone for SessionEndpoint { } } -pub const MAX_MESSAGE_HISTORY: usize = 1024; +pub const MAX_MESSAGE_HISTORY_ALL: usize = 1024; +pub const MAX_MESSAGE_HISTORY_MAJOR: usize = 1024; + +/// Direction of a BGP message (sent or received). +#[derive(Debug, Clone, Copy, Serialize, Deserialize, JsonSchema, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum MessageDirection { + Sent, + Received, +} -/// A message history entry is a BGP message with an associated timestamp and connection ID +/// A message history entry is a BGP message with an associated timestamp, +/// connection ID, and direction. #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] pub struct MessageHistoryEntry { timestamp: chrono::DateTime, message: Message, connection_id: ConnectionId, + direction: MessageDirection, +} + +impl MessageHistoryEntry { + pub fn direction(&self) -> MessageDirection { + self.direction + } } -/// Message history for a BGP session +/// Dual-buffer message history for a BGP session. +/// +/// The `all` buffer records every message including KeepAlives, useful +/// for detailed debugging. The `major` buffer records only significant +/// messages (Open, Update, Notification, RouteRefresh) for long-term +/// visibility. #[derive(Default, Debug, Clone, Serialize, Deserialize, JsonSchema)] pub struct MessageHistory { - pub received: VecDeque, - pub sent: VecDeque, + pub all: VecDeque, + pub major: VecDeque, } impl MessageHistory { - fn receive(&mut self, msg: Message, connection_id: ConnectionId) { - if self.received.len() >= MAX_MESSAGE_HISTORY { - self.received.pop_back(); - } - self.received.push_front(MessageHistoryEntry { + pub fn record( + &mut self, + msg: Message, + connection_id: ConnectionId, + direction: MessageDirection, + ) { + let is_major = !matches!(msg, Message::KeepAlive); + let entry = MessageHistoryEntry { message: msg, timestamp: chrono::Utc::now(), connection_id, - }); - } - - fn send(&mut self, msg: Message, connection_id: ConnectionId) { - if self.sent.len() >= MAX_MESSAGE_HISTORY { - self.sent.pop_back(); + direction, + }; + if self.all.len() >= MAX_MESSAGE_HISTORY_ALL { + self.all.pop_back(); + } + self.all.push_front(entry.clone()); + if is_major { + if self.major.len() >= MAX_MESSAGE_HISTORY_MAJOR { + self.major.pop_back(); + } + self.major.push_front(entry); } - self.sent.push_front(MessageHistoryEntry { - message: msg, - timestamp: chrono::Utc::now(), - connection_id, - }); } } @@ -3547,8 +3572,11 @@ impl SessionRunner { // RFC 4271 FSM Event 19 if let Message::Open(om) = msg { - lock!(self.message_history) - .receive(om.clone().into(), *conn_id); + lock!(self.message_history).record( + om.clone().into(), + *conn_id, + MessageDirection::Received, + ); self.counters .opens_received .fetch_add(1, Ordering::Relaxed); @@ -4109,7 +4137,11 @@ impl SessionRunner { return FsmState::OpenConfirm(pc); } - lock!(self.message_history).receive(msg.clone(), conn_id); + lock!(self.message_history).record( + msg.clone(), + conn_id, + MessageDirection::Received, + ); // The peer has ACK'd our open message with a keepalive. Start the // session timers and enter session setup. @@ -4855,9 +4887,10 @@ impl SessionRunner { ) { CollisionConnectionKind::New => { if let Message::Open(om) = msg { - lock!(self.message_history).receive( + lock!(self.message_history).record( om.clone().into(), *conn_id, + MessageDirection::Received, ); self.bump_msg_counter(msg_kind, false); @@ -5504,9 +5537,10 @@ impl SessionRunner { CollisionConnectionKind::Exist => { if let Message::Open(om) = msg { // RFC 4271 FSM Event 19 - lock!(self.message_history).receive( + lock!(self.message_history).record( om.clone().into(), *conn_id, + MessageDirection::Received, ); self.bump_msg_counter(msg_kind, false); @@ -5611,9 +5645,10 @@ impl SessionRunner { CollisionConnectionKind::New => { if let Message::Open(om) = msg { // RFC 4271 FSM Event 19 - lock!(self.message_history).receive( + lock!(self.message_history).record( om.clone().into(), *conn_id, + MessageDirection::Received, ); self.bump_msg_counter(msg_kind, false); @@ -7111,8 +7146,11 @@ impl SessionRunner { "message_contents" => format!("{m}") ); self.apply_update(m.clone(), &pc); - lock!(self.message_history) - .receive(m.into(), *conn_id); + lock!(self.message_history).record( + m.into(), + *conn_id, + MessageDirection::Received, + ); self.bump_msg_counter(msg_kind, false); FsmState::Established(pc) } @@ -7147,8 +7185,11 @@ impl SessionRunner { "message" => "notification", "message_contents" => format!("{m}") ); - lock!(self.message_history) - .receive(m.clone().into(), *conn_id); + lock!(self.message_history).record( + m.clone().into(), + *conn_id, + MessageDirection::Received, + ); self.bump_msg_counter(msg_kind, false); self.exit_established(pc) } @@ -7171,6 +7212,11 @@ impl SessionRunner { conn_id.short(); "message" => "keepalive" ); + lock!(self.message_history).record( + Message::KeepAlive, + *conn_id, + MessageDirection::Received, + ); self.bump_msg_counter(msg_kind, false); conn_timer!(pc.conn, hold).reset(); FsmState::Established(pc) @@ -7197,8 +7243,11 @@ impl SessionRunner { "message" => "route refresh", "message_contents" => format!("{m}").as_str() ); - lock!(self.message_history) - .receive(m.clone().into(), *conn_id); + lock!(self.message_history).record( + m.clone().into(), + *conn_id, + MessageDirection::Received, + ); self.bump_msg_counter(msg_kind, false); if let Err(e) = self.handle_refresh(m, &pc) { session_log!( @@ -7454,6 +7503,11 @@ impl SessionRunner { .keepalive_send_failure .fetch_add(1, Ordering::Relaxed); } else { + lock!(self.message_history).record( + Message::KeepAlive, + *conn.id(), + MessageDirection::Sent, + ); self.counters .keepalives_sent .fetch_add(1, Ordering::Relaxed); @@ -7564,7 +7618,11 @@ impl SessionRunner { ); let msg = Message::Notification(notification); - lock!(self.message_history).send(msg.clone(), *conn.id()); + lock!(self.message_history).record( + msg.clone(), + *conn.id(), + MessageDirection::Sent, + ); if let Err(e) = conn.send(msg) { session_log!( @@ -7639,7 +7697,11 @@ impl SessionRunner { } } drop(msg); - lock!(self.message_history).send(out.clone(), *conn.id()); + lock!(self.message_history).record( + out.clone(), + *conn.id(), + MessageDirection::Sent, + ); self.counters.opens_sent.fetch_add(1, Ordering::Relaxed); if let Err(e) = conn.send(out) { @@ -8024,7 +8086,11 @@ impl SessionRunner { pc: &PeerConnection, ) -> Result<(), Error> { // Record in message history - lock!(self.message_history).send(update.clone(), *pc.conn.id()); + lock!(self.message_history).record( + update.clone(), + *pc.conn.id(), + MessageDirection::Sent, + ); // Update counters self.counters.updates_sent.fetch_add(1, Ordering::Relaxed); @@ -9543,6 +9609,26 @@ impl From for MessageHistoryEntryV1 { } } +// V2/V3 API compatibility type for message history entry +// (has ConnectionId but no direction field) +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +#[schemars(rename = "MessageHistoryEntry")] +pub struct MessageHistoryEntryV2 { + timestamp: chrono::DateTime, + message: Message, + connection_id: ConnectionId, +} + +impl From for MessageHistoryEntryV2 { + fn from(entry: MessageHistoryEntry) -> Self { + Self { + timestamp: entry.timestamp, + message: entry.message, + connection_id: entry.connection_id, + } + } +} + // V1 API compatibility type for message history collection #[derive(Default, Debug, Clone, Serialize, Deserialize, JsonSchema)] pub struct MessageHistoryV1 { @@ -9554,13 +9640,17 @@ impl From for MessageHistoryV1 { fn from(history: MessageHistory) -> Self { Self { received: history - .received - .into_iter() + .major + .iter() + .filter(|e| e.direction == MessageDirection::Received) + .cloned() .map(MessageHistoryEntryV1::from) .collect(), sent: history - .sent - .into_iter() + .major + .iter() + .filter(|e| e.direction == MessageDirection::Sent) + .cloned() .map(MessageHistoryEntryV1::from) .collect(), } diff --git a/mg-api/src/lib.rs b/mg-api/src/lib.rs index 33fdb344f..d0854af93 100644 --- a/mg-api/src/lib.rs +++ b/mg-api/src/lib.rs @@ -9,6 +9,7 @@ use std::{ }; use bfd::BfdPeerState; +pub use bgp::session::MessageDirection; use bgp::{ messages::Afi, params::{ @@ -16,7 +17,10 @@ use bgp::{ NeighborResetOpV1, NeighborV1, Origin4, Origin6, PeerInfo, PeerInfoV1, PeerInfoV2, Router, ShaperSource, UnnumberedNeighbor, }, - session::{FsmEventRecord, MessageHistory, MessageHistoryV1, PeerId}, + session::{ + FsmEventRecord, MessageHistoryEntry, MessageHistoryEntryV2, + MessageHistoryV1, PeerId, + }, }; use dropshot::{ HttpError, HttpResponseDeleted, HttpResponseOk, @@ -457,12 +461,18 @@ pub trait MgAdminApi { request: TypedBody, ) -> Result, HttpError>; - #[endpoint { method = GET, path = "/bgp/history/message", versions = VERSION_UNNUMBERED.. }] + #[endpoint { method = GET, path = "/bgp/history/message", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] async fn message_history_v3( rqctx: RequestContext, request: TypedBody, ) -> Result, HttpError>; + #[endpoint { method = GET, path = "/bgp/history/message", versions = VERSION_EXTENDED_NH_STATIC.. }] + async fn message_history_v4( + rqctx: RequestContext, + request: TypedBody, + ) -> Result, HttpError>; + #[endpoint { method = GET, path = "/bgp/history/fsm", versions = VERSION_IPV6_BASIC..VERSION_UNNUMBERED }] async fn fsm_history( rqctx: RequestContext, @@ -807,11 +817,13 @@ pub struct MessageHistoryResponseV1 { pub by_peer: HashMap, } -#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone, Copy, PartialEq)] -#[serde(rename_all = "lowercase")] -pub enum MessageDirection { - Sent, - Received, +/// Backward-compatible message history with sent/received split. +/// Used by v2/v3 API endpoints to preserve the original wire format. +#[derive(Debug, Serialize, JsonSchema, Clone)] +#[schemars(rename = "MessageHistory")] +pub struct MessageHistorySentReceived { + pub sent: std::collections::VecDeque, + pub received: std::collections::VecDeque, } #[derive(Debug, Deserialize, JsonSchema, Clone)] @@ -828,10 +840,10 @@ pub struct MessageHistoryRequestV4 { #[derive(Debug, Serialize, JsonSchema, Clone)] #[schemars(rename = "MessageHistoryResponse")] pub struct MessageHistoryResponseV4 { - pub by_peer: HashMap, + pub by_peer: HashMap, } -/// Unified message history request supporting both numbered and unnumbered peers +/// V3 message history request (VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC) #[derive(Debug, Deserialize, JsonSchema, Clone)] pub struct MessageHistoryRequest { /// ASN of the BGP router @@ -845,11 +857,43 @@ pub struct MessageHistoryRequest { pub direction: Option, } -/// Unified message history response with string keys from PeerId Display -/// Keys will be "192.0.2.1" or "eth0" format +/// V3 message history response (VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC) #[derive(Debug, Serialize, JsonSchema, Clone)] pub struct MessageHistoryResponse { - pub by_peer: HashMap, + pub by_peer: HashMap, +} + +/// Which message buffer to retrieve +#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone, Copy, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum MessageBuffer { + /// All messages including KeepAlives + All, + /// Major messages only (excludes KeepAlives) + Major, +} + +/// V4 message history request (VERSION_EXTENDED_NH_STATIC..) +#[derive(Debug, Deserialize, JsonSchema, Clone)] +pub struct MessageHistoryRequestV5 { + /// ASN of the BGP router + pub asn: u32, + + /// Optional peer filter using PeerId enum + pub peer: Option, + + /// Optional direction filter - if None, returns both directions + pub direction: Option, + + /// Which buffer to retrieve - if None, returns major buffer + pub buffer: Option, +} + +/// V4 message history response (VERSION_EXTENDED_NH_STATIC..) +/// Flat list of entries per peer, each entry carries its own direction. +#[derive(Debug, Serialize, JsonSchema, Clone)] +pub struct MessageHistoryResponseV5 { + pub by_peer: HashMap>, } #[derive(Debug, Deserialize, Serialize, JsonSchema, Clone, Copy, PartialEq)] diff --git a/mgadm/src/bgp.rs b/mgadm/src/bgp.rs index a48aea484..054b025dc 100644 --- a/mgadm/src/bgp.rs +++ b/mgadm/src/bgp.rs @@ -23,6 +23,39 @@ use std::{ }; use tabwriter::TabWriter; +#[derive(Clone, Copy, Debug, ValueEnum)] +pub(crate) enum BufferSelect { + All, + Major, +} + +impl BufferSelect { + fn label(&self) -> &'static str { + match self { + BufferSelect::All => "All", + BufferSelect::Major => "Major", + } + } +} + +impl From for types::FsmEventBuffer { + fn from(b: BufferSelect) -> Self { + match b { + BufferSelect::All => types::FsmEventBuffer::All, + BufferSelect::Major => types::FsmEventBuffer::Major, + } + } +} + +impl From for types::MessageBuffer { + fn from(b: BufferSelect) -> Self { + match b { + BufferSelect::All => types::MessageBuffer::All, + BufferSelect::Major => types::MessageBuffer::Major, + } + } +} + fn jitter_range_to_api(j: JitterRange) -> types::JitterRange { types::JitterRange { min: j.min, @@ -142,7 +175,7 @@ pub enum HistoryCmd { /// Which buffer to display: 'major' (default) or 'all'. #[clap(default_value = "major")] - buffer: String, + buffer: BufferSelect, /// BGP Autonomous System number. Can be specified via ASN env var. #[clap(env)] @@ -162,6 +195,10 @@ pub enum HistoryCmd { /// Peer to show history for (IP address for numbered, interface name for unnumbered). peer: String, + /// Which buffer to display: 'major' (default) or 'all'. + #[clap(default_value = "major")] + buffer: BufferSelect, + /// BGP Autonomous System number. Can be specified via ASN env var. #[clap(env)] asn: Option, @@ -949,11 +986,12 @@ pub async fn commands(command: Commands, c: Client) -> Result<()> { let asn = asn.ok_or_else(|| { anyhow::anyhow!("ASN is required. Specify it on the command line or set the ASN environment variable.") })?; - get_fsm_history(c, asn, peer, &buffer, &limit, wide).await? + get_fsm_history(c, asn, peer, buffer, &limit, wide).await? } HistoryCmd::Message { asn, peer, + buffer, direction, limit, wide, @@ -961,8 +999,10 @@ pub async fn commands(command: Commands, c: Client) -> Result<()> { let asn = asn.ok_or_else(|| { anyhow::anyhow!("ASN is required. Specify it on the command line or set the ASN environment variable.") })?; - get_message_history(c, asn, peer, &direction, &limit, wide) - .await? + get_message_history( + c, asn, peer, buffer, &direction, &limit, wide, + ) + .await? } }, @@ -1609,21 +1649,10 @@ async fn get_fsm_history( c: Client, asn: u32, peer: Option, - buffer: &str, + buffer: BufferSelect, limit_str: &str, wide: bool, ) -> Result<()> { - // Parse buffer type for server-side filtering - let buffer_type = match buffer { - "all" => Some(types::FsmEventBuffer::All), - _ => Some(types::FsmEventBuffer::Major), // Default to major - }; - - let buffer_name = match buffer { - "all" => "All Events", - _ => "Major Events", - }; - // Parse peer filter if provided and convert to API type let peer_id = peer.as_ref().map(|p| { let bgp_peer_id: bgp::session::PeerId = @@ -1635,7 +1664,7 @@ async fn get_fsm_history( .fsm_history_v2(&types::FsmHistoryRequest { asn, peer: peer_id, - buffer: buffer_type, + buffer: Some(buffer.into()), }) .await? .into_inner(); @@ -1666,8 +1695,9 @@ async fn get_fsm_history( println_nopipe!( "\n{}", format!( - "FSM Event History - Peer: {} - {} (empty)", - peer_addr, buffer_name + "FSM Event History - Peer: {} - {} Events (empty)", + peer_addr, + buffer.label() ) .dimmed() ); @@ -1677,8 +1707,9 @@ async fn get_fsm_history( println_nopipe!( "\n{}", format!( - "FSM Event History - Peer: {} - {}", - peer_addr, buffer_name + "FSM Event History - Peer: {} - {} Events", + peer_addr, + buffer.label() ) .dimmed() ); @@ -1746,6 +1777,7 @@ async fn get_message_history( c: Client, asn: u32, peer: String, + buffer: BufferSelect, direction: &str, limit_str: &str, wide: bool, @@ -1775,10 +1807,11 @@ async fn get_message_history( let peer_id = peer_id_to_api(bgp_peer_id); let result = c - .message_history_v3(&types::MessageHistoryRequest { + .message_history_v4(&types::MessageHistoryRequestV5 { asn, peer: Some(peer_id), direction: dir, + buffer: Some(buffer.into()), }) .await? .into_inner(); @@ -1793,65 +1826,45 @@ async fn get_message_history( } // Should only have one peer since we filtered by peer - // Note: by_peer uses String keys (from JSON serialization) - let history = result.by_peer.get(&peer.to_string()).ok_or_else(|| { + let entries = result.by_peer.get(&peer.to_string()).ok_or_else(|| { anyhow::anyhow!("Peer {} not found in message history", peer) })?; - // Combine sent and received messages with their direction - let mut all_messages = Vec::new(); - - for entry in &history.received { - all_messages.push(( - entry.timestamp, - "RX", - &entry.connection_id, - &entry.message, - )); - } - - for entry in &history.sent { - all_messages.push(( - entry.timestamp, - "TX", - &entry.connection_id, - &entry.message, - )); - } - - // Sort by timestamp (oldest first) - all_messages.sort_by_key(|(ts, _, _, _)| *ts); - - // Apply limit - let messages_to_show = - all_messages.iter().rev().take(limit).collect::>(); - let total_count = all_messages.len(); + // Apply limit (entries are already in reverse chronological order) + let messages_to_show: Vec<_> = entries.iter().take(limit).collect(); + let total_count = entries.len(); println_nopipe!( "\n{}", - format!("BGP Message History - Peer: {}", peer).dimmed() + format!( + "BGP Message History - Peer: {} - {} Messages", + peer, + buffer.label() + ) + .dimmed() ); println_nopipe!("{}", "=".repeat(80).dimmed()); println_nopipe!( - "Showing {} of {} messages ({} RX, {} TX)\n", + "Showing {} of {} messages\n", messages_to_show.len(), total_count, - history.received.len(), - history.sent.len() ); - // Display messages in reverse chronological order (newest first) - for (timestamp, direction, conn_id, message) in - messages_to_show.iter().rev() - { - let ts_str = timestamp.format("%Y-%m-%d %H:%M:%S%.3f"); - let uuid_str = conn_id.uuid.to_string(); + // Display messages (already newest-first from server) + for entry in &messages_to_show { + let ts_str = entry.timestamp.format("%Y-%m-%d %H:%M:%S%.3f"); + let uuid_str = entry.connection_id.uuid.to_string(); let conn_short = format!("{}...{}", &uuid_str[..8], &uuid_str[uuid_str.len() - 4..]); + let dir_arrow = match entry.direction { + types::MessageDirection::Received => "←".green(), + types::MessageDirection::Sent => "→".blue(), + }; + // Use Debug formatting for full message content // (client-generated types don't implement Display) - let msg_content = format!("{:?}", message); + let msg_content = format!("{:?}", entry.message); // Apply truncation unless wide mode let msg_display = if !wide && msg_content.len() > 100 { @@ -1863,11 +1876,7 @@ async fn get_message_history( println_nopipe!( "{} {} [{}] {}", ts_str.to_string().dimmed(), - if *direction == "RX" { - "←".green() - } else { - "→".blue() - }, + dir_arrow, conn_short.dimmed(), msg_display ); diff --git a/mgd/src/admin.rs b/mgd/src/admin.rs index 0d2d86626..6ce5ce2a1 100644 --- a/mgd/src/admin.rs +++ b/mgd/src/admin.rs @@ -494,6 +494,13 @@ impl MgAdminApi for MgAdminApiImpl { bgp_admin::message_history_v3(ctx, request).await } + async fn message_history_v4( + ctx: RequestContext, + request: TypedBody, + ) -> Result, HttpError> { + bgp_admin::message_history_v4(ctx, request).await + } + async fn fsm_history( ctx: RequestContext, request: TypedBody, diff --git a/mgd/src/bgp_admin.rs b/mgd/src/bgp_admin.rs index a291c59f6..a4968b527 100644 --- a/mgd/src/bgp_admin.rs +++ b/mgd/src/bgp_admin.rs @@ -20,8 +20,8 @@ use bgp::{ router::{LoadPolicyError, Router}, session::{ AdminEvent, ConnectionKind, FsmEvent, FsmEventRecord, FsmStateKind, - MessageHistory, MessageHistoryV1, PeerId, SessionEndpoint, SessionInfo, - SessionRunner, + MessageDirection, MessageHistoryEntry, MessageHistoryEntryV2, + MessageHistoryV1, PeerId, SessionEndpoint, SessionInfo, SessionRunner, }, }; use chrono::{DateTime, SecondsFormat, Utc}; @@ -32,13 +32,15 @@ use dropshot::{ use mg_api::{ AsnSelector, BestpathFanoutRequest, BestpathFanoutResponse, ExportedSelector, FsmEventBuffer, FsmHistoryRequest, FsmHistoryRequestV4, - FsmHistoryResponse, FsmHistoryResponseV4, MessageDirection, + FsmHistoryResponse, FsmHistoryResponseV4, MessageBuffer, MessageHistoryRequest, MessageHistoryRequestV1, MessageHistoryRequestV4, - MessageHistoryResponse, MessageHistoryResponseV1, MessageHistoryResponseV4, - NdpInterface, NdpInterfaceSelector, NdpManagerState, NdpPeer, - NdpPendingInterface, NdpThreadState, NeighborResetRequest, - NeighborResetRequestV1, NeighborSelector, NeighborSelectorV1, RibV1, - UnnumberedNeighborResetRequest, UnnumberedNeighborSelector, + MessageHistoryRequestV5, MessageHistoryResponse, MessageHistoryResponseV1, + MessageHistoryResponseV4, MessageHistoryResponseV5, + MessageHistorySentReceived, NdpInterface, NdpInterfaceSelector, + NdpManagerState, NdpPeer, NdpPendingInterface, NdpThreadState, + NeighborResetRequest, NeighborResetRequestV1, NeighborSelector, + NeighborSelectorV1, RibV1, UnnumberedNeighborResetRequest, + UnnumberedNeighborSelector, }; use mg_common::lock; use rdb::{ @@ -1609,15 +1611,18 @@ async fn do_bgp_apply( Ok(HttpResponseUpdatedNoContent()) } -// Helper for fetching message history with PeerId filtering -// Returns HashMap with string keys using PeerId Display format +/// Unified helper for message history retrieval. +/// Selects the requested buffer, applies direction filter, and returns +/// a flat `Vec` per peer keyed by PeerId Display. fn get_message_history_filtered( ctx: &Arc, asn: u32, peer: Option, direction: Option, -) -> Result, HttpError> { + buffer: MessageBuffer, +) -> Result>, HttpError> { let mut result = HashMap::new(); + let use_all_buffer = matches!(buffer, MessageBuffer::All); // Determine which peers to fetch history for let peers_to_query: Vec = if let Some(peer_id) = peer { @@ -1638,28 +1643,46 @@ fn get_message_history_filtered( if let Some(session) = lock!(get_router!(ctx, asn)?.sessions).get(&peer_id) { - let mut history = lock!(session.message_history).clone(); + let history = lock!(session.message_history).clone(); + let source = if use_all_buffer { + history.all + } else { + history.major + }; - // Apply direction filter if specified - if let Some(dir) = direction { - match dir { - MessageDirection::Sent => { - history.received.clear(); - } - MessageDirection::Received => { - history.sent.clear(); - } - } - } + let entries: Vec = match direction { + Some(dir) => source + .into_iter() + .filter(|e| e.direction() == dir) + .collect(), + None => source.into_iter().collect(), + }; - // Use PeerId Display impl as HashMap key - result.insert(peer_id.to_string(), history); + result.insert(peer_id.to_string(), entries); } } Ok(result) } +/// Reconstruct the old sent/received split format from a flat entry list. +/// Converts to `MessageHistoryEntryV2` (without direction) for v2/v3 compat. +fn to_sent_received( + entries: Vec, +) -> MessageHistorySentReceived { + let mut sent = std::collections::VecDeque::new(); + let mut received = std::collections::VecDeque::new(); + for entry in entries { + let dir = entry.direction(); + let v2: MessageHistoryEntryV2 = entry.into(); + match dir { + MessageDirection::Sent => sent.push_back(v2), + MessageDirection::Received => received.push_back(v2), + } + } + MessageHistorySentReceived { sent, received } +} + pub async fn message_history( ctx: RequestContext>, request: TypedBody, @@ -1689,24 +1712,29 @@ pub async fn message_history_v2( let rq = request.into_inner(); let ctx = ctx.context(); - // Convert IpAddr filter to PeerId and call unified helper let peer_id = rq.peer.map(PeerId::Ip); - let by_peer_string = - get_message_history_filtered(ctx, rq.asn, peer_id, rq.direction)?; + let by_peer_string = get_message_history_filtered( + ctx, + rq.asn, + peer_id, + rq.direction, + MessageBuffer::Major, + )?; // Convert String keys back to IpAddr (filters out unnumbered peers) let by_peer = by_peer_string .into_iter() - .filter_map(|(key, history)| { - key.parse::().ok().map(|addr| (addr, history)) + .filter_map(|(key, entries)| { + key.parse::() + .ok() + .map(|addr| (addr, to_sent_received(entries))) }) .collect(); Ok(HttpResponseOk(MessageHistoryResponseV4 { by_peer })) } -// UNNUMBERED+ API endpoint (VERSION_UNNUMBERED..) -// Uses PeerId enum for both numbered and unnumbered peers +// V3 API endpoint (VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC) pub async fn message_history_v3( ctx: RequestContext>, request: TypedBody, @@ -1714,11 +1742,38 @@ pub async fn message_history_v3( let rq = request.into_inner(); let ctx = ctx.context(); - let by_peer = - get_message_history_filtered(ctx, rq.asn, rq.peer, rq.direction)?; + let by_peer_flat = get_message_history_filtered( + ctx, + rq.asn, + rq.peer, + rq.direction, + MessageBuffer::Major, + )?; + let by_peer = by_peer_flat + .into_iter() + .map(|(k, entries)| (k, to_sent_received(entries))) + .collect(); Ok(HttpResponseOk(MessageHistoryResponse { by_peer })) } +// V4 API endpoint (VERSION_EXTENDED_NH_STATIC..) +pub async fn message_history_v4( + ctx: RequestContext>, + request: TypedBody, +) -> Result, HttpError> { + let rq = request.into_inner(); + let ctx = ctx.context(); + + let by_peer = get_message_history_filtered( + ctx, + rq.asn, + rq.peer, + rq.direction, + rq.buffer.unwrap_or(MessageBuffer::Major), + )?; + Ok(HttpResponseOk(MessageHistoryResponseV5 { by_peer })) +} + /// Unified helper for FSM history retrieval. /// Returns HashMap with String keys (PeerId Display representation). fn get_fsm_history_filtered( diff --git a/openapi/mg-admin/mg-admin-7.0.0-693bed.json b/openapi/mg-admin/mg-admin-7.0.0-837dad.json similarity index 99% rename from openapi/mg-admin/mg-admin-7.0.0-693bed.json rename to openapi/mg-admin/mg-admin-7.0.0-837dad.json index d5461847b..f5e9cf8e9 100644 --- a/openapi/mg-admin/mg-admin-7.0.0-693bed.json +++ b/openapi/mg-admin/mg-admin-7.0.0-837dad.json @@ -1097,12 +1097,12 @@ }, "/bgp/history/message": { "get": { - "operationId": "message_history_v3", + "operationId": "message_history_v4", "requestBody": { "content": { "application/json": { "schema": { - "$ref": "#/components/schemas/MessageHistoryRequest" + "$ref": "#/components/schemas/MessageHistoryRequestV5" } } }, @@ -1114,7 +1114,7 @@ "content": { "application/json": { "schema": { - "$ref": "#/components/schemas/MessageHistoryResponse" + "$ref": "#/components/schemas/MessageHistoryResponseV5" } } } @@ -3495,42 +3495,43 @@ } ] }, + "MessageBuffer": { + "description": "Which message buffer to retrieve", + "oneOf": [ + { + "description": "All messages including KeepAlives", + "type": "string", + "enum": [ + "all" + ] + }, + { + "description": "Major messages only (excludes KeepAlives)", + "type": "string", + "enum": [ + "major" + ] + } + ] + }, "MessageDirection": { + "description": "Direction of a BGP message (sent or received).", "type": "string", "enum": [ "sent", "received" ] }, - "MessageHistory": { - "description": "Message history for a BGP session", - "type": "object", - "properties": { - "received": { - "type": "array", - "items": { - "$ref": "#/components/schemas/MessageHistoryEntry" - } - }, - "sent": { - "type": "array", - "items": { - "$ref": "#/components/schemas/MessageHistoryEntry" - } - } - }, - "required": [ - "received", - "sent" - ] - }, "MessageHistoryEntry": { - "description": "A message history entry is a BGP message with an associated timestamp and connection ID", + "description": "A message history entry is a BGP message with an associated timestamp, connection ID, and direction.", "type": "object", "properties": { "connection_id": { "$ref": "#/components/schemas/ConnectionId" }, + "direction": { + "$ref": "#/components/schemas/MessageDirection" + }, "message": { "$ref": "#/components/schemas/Message" }, @@ -3541,12 +3542,13 @@ }, "required": [ "connection_id", + "direction", "message", "timestamp" ] }, - "MessageHistoryRequest": { - "description": "Unified message history request supporting both numbered and unnumbered peers", + "MessageHistoryRequestV5": { + "description": "V4 message history request (VERSION_EXTENDED_NH_STATIC..)", "type": "object", "properties": { "asn": { @@ -3555,9 +3557,18 @@ "format": "uint32", "minimum": 0 }, + "buffer": { + "nullable": true, + "description": "Which buffer to retrieve - if None, returns major buffer", + "allOf": [ + { + "$ref": "#/components/schemas/MessageBuffer" + } + ] + }, "direction": { "nullable": true, - "description": "Optional direction filter - if None, returns both sent and received", + "description": "Optional direction filter - if None, returns both directions", "allOf": [ { "$ref": "#/components/schemas/MessageDirection" @@ -3566,7 +3577,7 @@ }, "peer": { "nullable": true, - "description": "Optional peer filter using PeerId enum JSON format: {\"ip\": \"192.0.2.1\"} or {\"interface\": \"eth0\"}", + "description": "Optional peer filter using PeerId enum", "allOf": [ { "$ref": "#/components/schemas/PeerId" @@ -3578,14 +3589,17 @@ "asn" ] }, - "MessageHistoryResponse": { - "description": "Unified message history response with string keys from PeerId Display Keys will be \"192.0.2.1\" or \"eth0\" format", + "MessageHistoryResponseV5": { + "description": "V4 message history response (VERSION_EXTENDED_NH_STATIC..) Flat list of entries per peer, each entry carries its own direction.", "type": "object", "properties": { "by_peer": { "type": "object", "additionalProperties": { - "$ref": "#/components/schemas/MessageHistory" + "type": "array", + "items": { + "$ref": "#/components/schemas/MessageHistoryEntry" + } } } }, diff --git a/openapi/mg-admin/mg-admin-latest.json b/openapi/mg-admin/mg-admin-latest.json index 875295713..d6d5eb58b 120000 --- a/openapi/mg-admin/mg-admin-latest.json +++ b/openapi/mg-admin/mg-admin-latest.json @@ -1 +1 @@ -mg-admin-7.0.0-693bed.json \ No newline at end of file +mg-admin-7.0.0-837dad.json \ No newline at end of file From c1058d9ddcf1f3c81cf42b1c66a951d0c6d60bdc Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Sat, 21 Feb 2026 12:24:06 -0700 Subject: [PATCH 18/58] Track/expose better diagnostics for BGP peers Expands the advertised/imported prefix counters to be per AFI. Adds logic to track the last reset timestamp/reason. Adds logic to track the last sent/received NOTIFICATION and timestamp. Adds unit tests to validate the prefix counters and the last reset and last NOTIFICATION. --- bgp/src/params.rs | 368 ++++++++++++- bgp/src/session.rs | 245 ++++++++- bgp/src/test.rs | 511 +++++++++++++++++- falcon-lab/src/test.rs | 4 +- mg-api/src/lib.rs | 12 +- mgadm/src/bgp.rs | 19 +- mgd/src/admin.rs | 11 +- mgd/src/bgp_admin.rs | 33 +- mgd/src/oxstats.rs | 58 +- ...837dad.json => mg-admin-7.0.0-26a5e9.json} | 120 +++- openapi/mg-admin/mg-admin-latest.json | 2 +- 11 files changed, 1299 insertions(+), 84 deletions(-) rename openapi/mg-admin/{mg-admin-7.0.0-837dad.json => mg-admin-7.0.0-26a5e9.json} (98%) diff --git a/bgp/src/params.rs b/bgp/src/params.rs index 57676835b..462f813cf 100644 --- a/bgp/src/params.rs +++ b/bgp/src/params.rs @@ -575,10 +575,38 @@ pub struct PeerTimers { pub delay_open: StaticTimerInfo, } -/// Session-level counters that persist across connection changes -/// These serve as aggregate counters across all connections for the session +/// Reason for the most recent session reset. +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +pub enum ResetReason { + AdministrativeReset, + AdministrativeShutdown, + HoldTimerExpired, + FsmError, + ConnectionRejected, + CollisionResolution, + IoError, + ParseError, + NotificationReceived, +} + +/// Information about the most recent session reset. +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +pub struct FsmResetRecord { + pub timestamp: chrono::DateTime, + pub reason: ResetReason, +} + +/// A record of a BGP notification message with a timestamp. +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +pub struct NotificationRecord { + pub timestamp: chrono::DateTime, + pub notification: crate::messages::NotificationMessage, +} + +/// Session-level counters (v5-v6 API: aggregate NLRI, no reset_count). #[derive(Debug, Deserialize, Serialize, JsonSchema)] -pub struct PeerCounters { +#[schemars(rename = "PeerCounters")] +pub struct PeerCountersV1 { // FSM Counters pub connection_retries: u64, pub active_connections_accepted: u64, @@ -635,7 +663,7 @@ pub struct PeerCounters { pub connector_panics: u64, } -impl From<&SessionCounters> for PeerCounters { +impl From<&SessionCounters> for PeerCountersV1 { fn from(value: &SessionCounters) -> Self { Self { connection_retries: value @@ -684,9 +712,13 @@ impl From<&SessionCounters> for PeerCounters { .idle_hold_timer_expirations .load(Ordering::Relaxed), prefixes_advertised: value - .prefixes_advertised - .load(Ordering::Relaxed), - prefixes_imported: value.prefixes_imported.load(Ordering::Relaxed), + .ipv4_prefixes_advertised + .load(Ordering::Relaxed) + + value.ipv6_prefixes_advertised.load(Ordering::Relaxed), + prefixes_imported: value + .ipv4_prefixes_imported + .load(Ordering::Relaxed) + + value.ipv6_prefixes_imported.load(Ordering::Relaxed), keepalives_sent: value.keepalives_sent.load(Ordering::Relaxed), keepalives_received: value .keepalives_received @@ -798,6 +830,223 @@ impl From<&Capability> for BgpCapability { } } +/// Peer info for v5-v6 API (aggregate NLRI counters, no reset/notification). +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +#[schemars(rename = "PeerInfo")] +pub struct PeerInfoV3 { + pub name: String, + pub peer_group: String, + pub fsm_state: FsmStateKind, + pub fsm_state_duration: Duration, + pub asn: Option, + pub id: Option, + pub local_ip: IpAddr, + pub remote_ip: IpAddr, + pub local_tcp_port: u16, + pub remote_tcp_port: u16, + pub received_capabilities: Vec, + pub timers: PeerTimers, + pub counters: PeerCountersV1, + pub ipv4_unicast: Ipv4UnicastConfig, + pub ipv6_unicast: Ipv6UnicastConfig, +} + +/// Session-level counters with per-AFI NLRI gauges (v7+ API). +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +pub struct PeerCounters { + // FSM Counters + pub connection_retries: u64, + pub active_connections_accepted: u64, + pub active_connections_declined: u64, + pub passive_connections_accepted: u64, + pub passive_connections_declined: u64, + pub transitions_to_idle: u64, + pub transitions_to_connect: u64, + pub transitions_to_active: u64, + pub transitions_to_open_sent: u64, + pub transitions_to_open_confirm: u64, + pub transitions_to_connection_collision: u64, + pub transitions_to_session_setup: u64, + pub transitions_to_established: u64, + pub hold_timer_expirations: u64, + pub idle_hold_timer_expirations: u64, + + // Per-AFI NLRI gauge counters + pub ipv4_prefixes_advertised: u64, + pub ipv4_prefixes_imported: u64, + pub ipv6_prefixes_advertised: u64, + pub ipv6_prefixes_imported: u64, + + // Message counters + pub keepalives_sent: u64, + pub keepalives_received: u64, + pub route_refresh_sent: u64, + pub route_refresh_received: u64, + pub opens_sent: u64, + pub opens_received: u64, + pub notifications_sent: u64, + pub notifications_received: u64, + pub updates_sent: u64, + pub updates_received: u64, + + // Message error counters + pub unexpected_update_message: u64, + pub unexpected_keepalive_message: u64, + pub unexpected_open_message: u64, + pub unexpected_route_refresh_message: u64, + pub unexpected_notification_message: u64, + pub update_nexhop_missing: u64, + pub open_handle_failures: u64, + pub unnegotiated_address_family: u64, + pub updates_treated_as_withdraw: u64, + + // Send failure counters + pub notification_send_failure: u64, + pub open_send_failure: u64, + pub keepalive_send_failure: u64, + pub route_refresh_send_failure: u64, + pub update_send_failure: u64, + + // Connection failure counters + pub tcp_connection_failure: u64, + pub md5_auth_failures: u64, + pub connector_panics: u64, + + // Reset counter + pub reset_count: u64, +} + +impl From<&SessionCounters> for PeerCounters { + fn from(value: &SessionCounters) -> Self { + Self { + connection_retries: value + .connection_retries + .load(Ordering::Relaxed), + active_connections_accepted: value + .active_connections_accepted + .load(Ordering::Relaxed), + active_connections_declined: value + .active_connections_declined + .load(Ordering::Relaxed), + passive_connections_accepted: value + .passive_connections_accepted + .load(Ordering::Relaxed), + passive_connections_declined: value + .passive_connections_declined + .load(Ordering::Relaxed), + transitions_to_idle: value + .transitions_to_idle + .load(Ordering::Relaxed), + transitions_to_connect: value + .transitions_to_connect + .load(Ordering::Relaxed), + transitions_to_active: value + .transitions_to_active + .load(Ordering::Relaxed), + transitions_to_open_sent: value + .transitions_to_open_sent + .load(Ordering::Relaxed), + transitions_to_open_confirm: value + .transitions_to_open_confirm + .load(Ordering::Relaxed), + transitions_to_connection_collision: value + .transitions_to_connection_collision + .load(Ordering::Relaxed), + transitions_to_session_setup: value + .transitions_to_session_setup + .load(Ordering::Relaxed), + transitions_to_established: value + .transitions_to_established + .load(Ordering::Relaxed), + hold_timer_expirations: value + .hold_timer_expirations + .load(Ordering::Relaxed), + idle_hold_timer_expirations: value + .idle_hold_timer_expirations + .load(Ordering::Relaxed), + ipv4_prefixes_advertised: value + .ipv4_prefixes_advertised + .load(Ordering::Relaxed), + ipv4_prefixes_imported: value + .ipv4_prefixes_imported + .load(Ordering::Relaxed), + ipv6_prefixes_advertised: value + .ipv6_prefixes_advertised + .load(Ordering::Relaxed), + ipv6_prefixes_imported: value + .ipv6_prefixes_imported + .load(Ordering::Relaxed), + keepalives_sent: value.keepalives_sent.load(Ordering::Relaxed), + keepalives_received: value + .keepalives_received + .load(Ordering::Relaxed), + route_refresh_sent: value + .route_refresh_sent + .load(Ordering::Relaxed), + route_refresh_received: value + .route_refresh_received + .load(Ordering::Relaxed), + opens_sent: value.opens_sent.load(Ordering::Relaxed), + opens_received: value.opens_received.load(Ordering::Relaxed), + notifications_sent: value + .notifications_sent + .load(Ordering::Relaxed), + notifications_received: value + .notifications_received + .load(Ordering::Relaxed), + updates_sent: value.updates_sent.load(Ordering::Relaxed), + updates_received: value.updates_received.load(Ordering::Relaxed), + unexpected_update_message: value + .unexpected_update_message + .load(Ordering::Relaxed), + unexpected_keepalive_message: value + .unexpected_keepalive_message + .load(Ordering::Relaxed), + unexpected_open_message: value + .unexpected_open_message + .load(Ordering::Relaxed), + unexpected_route_refresh_message: value + .unexpected_route_refresh_message + .load(Ordering::Relaxed), + unexpected_notification_message: value + .unexpected_notification_message + .load(Ordering::Relaxed), + update_nexhop_missing: value + .update_nexhop_missing + .load(Ordering::Relaxed), + open_handle_failures: value + .open_handle_failures + .load(Ordering::Relaxed), + unnegotiated_address_family: value + .unnegotiated_address_family + .load(Ordering::Relaxed), + updates_treated_as_withdraw: value + .updates_treated_as_withdraw + .load(Ordering::Relaxed), + notification_send_failure: value + .notification_send_failure + .load(Ordering::Relaxed), + open_send_failure: value.open_send_failure.load(Ordering::Relaxed), + keepalive_send_failure: value + .keepalive_send_failure + .load(Ordering::Relaxed), + route_refresh_send_failure: value + .route_refresh_send_failure + .load(Ordering::Relaxed), + update_send_failure: value + .update_send_failure + .load(Ordering::Relaxed), + tcp_connection_failure: value + .tcp_connection_failure + .load(Ordering::Relaxed), + md5_auth_failures: value.md5_auth_failures.load(Ordering::Relaxed), + connector_panics: value.connector_panics.load(Ordering::Relaxed), + reset_count: value.reset_count.load(Ordering::Relaxed), + } + } +} + +/// Peer info for v7+ API (per-AFI counters, reset/notification tracking). #[derive(Debug, Deserialize, Serialize, JsonSchema)] pub struct PeerInfo { pub name: String, @@ -815,6 +1064,111 @@ pub struct PeerInfo { pub counters: PeerCounters, pub ipv4_unicast: Ipv4UnicastConfig, pub ipv6_unicast: Ipv6UnicastConfig, + pub last_reset: Option, + pub last_notification_sent: Option, + pub last_notification_received: Option, +} + +impl From for PeerInfoV3 { + fn from(info: PeerInfo) -> Self { + Self { + name: info.name, + peer_group: info.peer_group, + fsm_state: info.fsm_state, + fsm_state_duration: info.fsm_state_duration, + asn: info.asn, + id: info.id, + local_ip: info.local_ip, + remote_ip: info.remote_ip, + local_tcp_port: info.local_tcp_port, + remote_tcp_port: info.remote_tcp_port, + received_capabilities: info.received_capabilities, + timers: info.timers, + counters: PeerCountersV1 { + connection_retries: info.counters.connection_retries, + active_connections_accepted: info + .counters + .active_connections_accepted, + active_connections_declined: info + .counters + .active_connections_declined, + passive_connections_accepted: info + .counters + .passive_connections_accepted, + passive_connections_declined: info + .counters + .passive_connections_declined, + transitions_to_idle: info.counters.transitions_to_idle, + transitions_to_connect: info.counters.transitions_to_connect, + transitions_to_active: info.counters.transitions_to_active, + transitions_to_open_sent: info + .counters + .transitions_to_open_sent, + transitions_to_open_confirm: info + .counters + .transitions_to_open_confirm, + transitions_to_connection_collision: info + .counters + .transitions_to_connection_collision, + transitions_to_session_setup: info + .counters + .transitions_to_session_setup, + transitions_to_established: info + .counters + .transitions_to_established, + hold_timer_expirations: info.counters.hold_timer_expirations, + idle_hold_timer_expirations: info + .counters + .idle_hold_timer_expirations, + prefixes_advertised: info.counters.ipv4_prefixes_advertised + + info.counters.ipv6_prefixes_advertised, + prefixes_imported: info.counters.ipv4_prefixes_imported + + info.counters.ipv6_prefixes_imported, + keepalives_sent: info.counters.keepalives_sent, + keepalives_received: info.counters.keepalives_received, + route_refresh_sent: info.counters.route_refresh_sent, + route_refresh_received: info.counters.route_refresh_received, + opens_sent: info.counters.opens_sent, + opens_received: info.counters.opens_received, + notifications_sent: info.counters.notifications_sent, + notifications_received: info.counters.notifications_received, + updates_sent: info.counters.updates_sent, + updates_received: info.counters.updates_received, + unexpected_update_message: info + .counters + .unexpected_update_message, + unexpected_keepalive_message: info + .counters + .unexpected_keepalive_message, + unexpected_open_message: info.counters.unexpected_open_message, + unexpected_route_refresh_message: info + .counters + .unexpected_route_refresh_message, + unexpected_notification_message: info + .counters + .unexpected_notification_message, + update_nexhop_missing: info.counters.update_nexhop_missing, + open_handle_failures: info.counters.open_handle_failures, + unnegotiated_address_family: info + .counters + .unnegotiated_address_family, + notification_send_failure: info + .counters + .notification_send_failure, + open_send_failure: info.counters.open_send_failure, + keepalive_send_failure: info.counters.keepalive_send_failure, + route_refresh_send_failure: info + .counters + .route_refresh_send_failure, + update_send_failure: info.counters.update_send_failure, + tcp_connection_failure: info.counters.tcp_connection_failure, + md5_auth_failures: info.counters.md5_auth_failures, + connector_panics: info.counters.connector_panics, + }, + ipv4_unicast: info.ipv4_unicast, + ipv6_unicast: info.ipv6_unicast, + } + } } #[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)] diff --git a/bgp/src/session.rs b/bgp/src/session.rs index 785129480..fa97ea495 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -21,8 +21,9 @@ use crate::{ }, params::{ BgpCapability, BgpPeerParameters, BgpPeerParametersV1, - DynamicTimerInfo, Ipv4UnicastConfig, Ipv6UnicastConfig, JitterRange, - PeerCounters, PeerInfo, PeerTimers, StaticTimerInfo, TimerConfig, + DynamicTimerInfo, FsmResetRecord, Ipv4UnicastConfig, Ipv6UnicastConfig, + JitterRange, NotificationRecord, PeerCounters, PeerInfo, PeerTimers, + ResetReason, StaticTimerInfo, TimerConfig, }, policy::{ CheckerResult, ShaperResult, check_incoming_open, @@ -620,6 +621,8 @@ pub enum StopReason { error_code: ErrorCode, error_subcode: ErrorSubcode, }, + /// A NOTIFICATION message was received from the peer. + NotificationReceived(NotificationMessage), } /// FsmEvents pertaining to a specific Connection @@ -1215,9 +1218,14 @@ pub struct SessionCounters { pub hold_timer_expirations: AtomicU64, pub idle_hold_timer_expirations: AtomicU64, - // NLRI counters - pub prefixes_advertised: AtomicU64, - pub prefixes_imported: AtomicU64, + // Per-AFI NLRI gauge counters (current prefix counts, not cumulative) + pub ipv4_prefixes_advertised: AtomicU64, + pub ipv4_prefixes_imported: AtomicU64, + pub ipv6_prefixes_advertised: AtomicU64, + pub ipv6_prefixes_imported: AtomicU64, + + // Reset counter + pub reset_count: AtomicU64, // Message counters pub keepalives_sent: AtomicU64, @@ -1255,6 +1263,23 @@ pub struct SessionCounters { pub connector_panics: AtomicU64, } +/// Atomically subtract `n` from `counter`, clamping at zero. +fn atomic_saturating_sub(counter: &AtomicU64, n: u64) { + let mut current = counter.load(Ordering::Relaxed); + loop { + let new = current.saturating_sub(n); + match counter.compare_exchange_weak( + current, + new, + Ordering::Relaxed, + Ordering::Relaxed, + ) { + Ok(_) => break, + Err(actual) => current = actual, + } + } +} + pub enum ShaperApplication { Current, Difference(Option), @@ -1680,6 +1705,15 @@ pub struct SessionRunner { /// Used to track outbound connection attempts and prevent duplicate spawns. connector_handle: Mutex>>, + /// Information about the most recent session reset. + last_reset: Mutex>, + + /// The most recently sent notification message. + last_notification_sent: Mutex>, + + /// The most recently received notification message. + last_notification_received: Mutex>, + log: Logger, } @@ -1753,6 +1787,9 @@ impl SessionRunner { Mutex::new(ConnectionRegistry::new()), ), connector_handle: Mutex::new(None), + last_reset: Mutex::new(None), + last_notification_sent: Mutex::new(None), + last_notification_received: Mutex::new(None), }; drop(session_info); runner @@ -2297,6 +2334,26 @@ impl SessionRunner { self.counters .transitions_to_idle .fetch_add(1, Ordering::Relaxed); + + // Conditionally bump reset_count based on + // the reset reason already stored in + // last_reset by stop(). + if let Some(ref info) = *lock!(self.last_reset) { + let count_it = matches!( + previous, + FsmStateKind::Established + | FsmStateKind::SessionSetup + ) || matches!( + info.reason, + ResetReason::AdministrativeReset + | ResetReason::AdministrativeShutdown + ); + if count_it { + self.counters + .reset_count + .fetch_add(1, Ordering::Relaxed); + } + } } FsmStateKind::Connect => { self.counters @@ -2633,6 +2690,7 @@ impl SessionRunner { loop { // Check to see if a shutdown has been requested. if self.shutdown.load(Ordering::Acquire) { + self.stop(None, None, StopReason::Shutdown); return FsmState::Idle; } @@ -2868,6 +2926,7 @@ impl SessionRunner { let title = msg.title(); if let Message::Notification(ref n) = msg { + self.record_notification_received(n); session_log_lite!( self, warn, @@ -2972,6 +3031,7 @@ impl SessionRunner { loop { // Check to see if a shutdown has been requested. if self.shutdown.load(Ordering::Acquire) { + self.stop(None, None, StopReason::Shutdown); return FsmState::Idle; } @@ -3090,6 +3150,7 @@ impl SessionRunner { let title = msg.title(); if let Message::Notification(ref n) = msg { + self.record_notification_received(n); session_log_lite!( self, warn, @@ -3346,6 +3407,7 @@ impl SessionRunner { let om = loop { // Check to see if a shutdown has been requested. if self.shutdown.load(Ordering::Acquire) { + self.stop(Some(&conn), None, StopReason::Shutdown); return FsmState::Idle; } @@ -3929,6 +3991,7 @@ impl SessionRunner { fn fsm_open_confirm(&self, pc: PeerConnection) -> FsmState { // Check to see if a shutdown has been requested. if self.shutdown.load(Ordering::Acquire) { + self.stop(Some(&pc.conn), None, StopReason::Shutdown); return FsmState::Idle; } @@ -4467,6 +4530,7 @@ impl SessionRunner { let om = loop { // Check to see if a shutdown has been requested. if self.shutdown.load(Ordering::Acquire) { + self.stop(Some(&exist.conn), Some(&new), StopReason::Shutdown); return FsmState::Idle; } @@ -5332,6 +5396,7 @@ impl SessionRunner { loop { // Check to see if a shutdown has been requested. if self.shutdown.load(Ordering::Acquire) { + self.stop(Some(&exist), Some(&new), StopReason::Shutdown); return FsmState::Idle; } @@ -6118,7 +6183,8 @@ impl SessionRunner { fn fsm_session_setup(&self, pc: PeerConnection) -> FsmState { // Check to see if a shutdown has been requested. if self.shutdown.load(Ordering::Acquire) { - return FsmState::Idle; + self.stop(Some(&pc.conn), None, StopReason::Shutdown); + return self.exit_established(pc); } // Collect the prefixes this router is originating. @@ -6208,6 +6274,7 @@ impl SessionRunner { "failed to send originated IPv4 routes: {e}"; "error" => format!("{e}") ); + self.stop(Some(&pc.conn), None, StopReason::IoError); return self.exit_established(pc); } @@ -6234,6 +6301,7 @@ impl SessionRunner { "failed to send originated IPv6 routes: {e}"; "error" => format!("{e}") ); + self.stop(Some(&pc.conn), None, StopReason::IoError); return self.exit_established(pc); } @@ -6299,6 +6367,7 @@ impl SessionRunner { fn fsm_established(&self, pc: PeerConnection) -> FsmState { // Check to see if a shutdown has been requested. if self.shutdown.load(Ordering::Acquire) { + self.stop(Some(&pc.conn), None, StopReason::Shutdown); return self.exit_established(pc); } @@ -6352,6 +6421,7 @@ impl SessionRunner { "failed to send update from announce-routes: {e}"; "error" => format!("{e}") ); + self.stop(Some(&pc.conn), None, StopReason::IoError); return self.exit_established(pc); } @@ -6371,6 +6441,11 @@ impl SessionRunner { "failed to originate update, fsm transition to idle"; "error" => format!("{e}") ); + self.stop( + Some(&pc.conn), + None, + StopReason::IoError, + ); self.exit_established(pc) } Ok(()) => FsmState::Established(pc), @@ -6477,6 +6552,11 @@ impl SessionRunner { "failed to send IPv4 export policy announce: {e}"; "error" => format!("{e}") ); + self.stop( + Some(&pc.conn), + None, + StopReason::IoError, + ); return self.exit_established(pc); } @@ -6496,6 +6576,11 @@ impl SessionRunner { "failed to send IPv4 export policy withdraw: {e}"; "error" => format!("{e}") ); + self.stop( + Some(&pc.conn), + None, + StopReason::IoError, + ); return self.exit_established(pc); } @@ -6599,6 +6684,11 @@ impl SessionRunner { "failed to send IPv6 export policy announce: {e}"; "error" => format!("{e}") ); + self.stop( + Some(&pc.conn), + None, + StopReason::IoError, + ); return self.exit_established(pc); } @@ -6618,6 +6708,11 @@ impl SessionRunner { "failed to send IPv6 export policy withdraw: {e}"; "error" => format!("{e}") ); + self.stop( + Some(&pc.conn), + None, + StopReason::IoError, + ); return self.exit_established(pc); } @@ -6666,6 +6761,7 @@ impl SessionRunner { "route re-advertisement error: {e}"; "error" => format!("{e}") ); + self.stop(Some(&pc.conn), None, StopReason::IoError); return self.exit_established(pc); } FsmState::Established(pc) @@ -7191,6 +7287,11 @@ impl SessionRunner { MessageDirection::Received, ); self.bump_msg_counter(msg_kind, false); + self.stop( + Some(&pc.conn), + None, + StopReason::NotificationReceived(m.clone()), + ); self.exit_established(pc) } @@ -7258,6 +7359,11 @@ impl SessionRunner { conn_id.short(); "error" => format!("{e}") ); + self.stop( + Some(&pc.conn), + None, + StopReason::IoError, + ); self.exit_established(pc) } else { FsmState::Established(pc) @@ -7290,7 +7396,14 @@ impl SessionRunner { "error_code" => format!("{error_code:?}"), "error_subcode" => format!("{error_subcode:?}") ); - self.send_notification(&pc.conn, error_code, error_subcode); + self.stop( + Some(&pc.conn), + None, + StopReason::ParseError { + error_code, + error_subcode, + }, + ); self.exit_established(pc) } @@ -7596,6 +7709,20 @@ impl SessionRunner { ) } + fn record_notification_received(&self, m: &NotificationMessage) { + *lock!(self.last_notification_received) = Some(NotificationRecord { + timestamp: chrono::Utc::now(), + notification: m.clone(), + }); + } + + fn record_reset(&self, reason: ResetReason) { + *lock!(self.last_reset) = Some(FsmResetRecord { + timestamp: chrono::Utc::now(), + reason, + }); + } + fn send_notification( &self, conn: &Cnx, @@ -7608,6 +7735,11 @@ impl SessionRunner { data: Vec::new(), }; + *lock!(self.last_notification_sent) = Some(NotificationRecord { + timestamp: chrono::Utc::now(), + notification: notification.clone(), + }); + session_log!( self, info, @@ -7962,6 +8094,7 @@ impl SessionRunner { return Ok(()); } + let count = nlri.len() as u64; let (skeleton, stamp) = match self.derive_nexthop(Afi::Ipv4, pc)? { BgpNexthop::Ipv4(nh4) => { @@ -8005,8 +8138,12 @@ impl SessionRunner { max_body, |msg| self.send_shaped(msg, pc, shaper_application), )?; + self.counters + .ipv4_prefixes_advertised + .fetch_add(count, Ordering::Relaxed); } RouteUpdate::V4(RouteUpdate4::Withdraw(withdrawn)) => { + let count = withdrawn.len() as u64; UpdateMessage::default().chunk_and_send( withdrawn, UpdateMessage::set_withdrawn, @@ -8014,6 +8151,10 @@ impl SessionRunner { max_body, |msg| self.send_shaped(msg, pc, shaper_application), )?; + atomic_saturating_sub( + &self.counters.ipv4_prefixes_advertised, + count, + ); } RouteUpdate::V6(RouteUpdate6::Announce(nlri)) => { let session = lock!(self.session); @@ -8030,6 +8171,7 @@ impl SessionRunner { return Ok(()); } + let count = nlri.len() as u64; let nh6 = self.derive_nexthop(Afi::Ipv6, pc)?; if matches!(nh6, BgpNexthop::Ipv4(_)) { return Err(Error::InvalidAddress( @@ -8055,8 +8197,12 @@ impl SessionRunner { max_body, |msg| self.send_shaped(msg, pc, shaper_application), )?; + self.counters + .ipv6_prefixes_advertised + .fetch_add(count, Ordering::Relaxed); } RouteUpdate::V6(RouteUpdate6::Withdraw(withdrawn)) => { + let count = withdrawn.len() as u64; let skeleton = UpdateMessage { path_attributes: vec![ PathAttributeValue::MpUnreachNlri( @@ -8073,6 +8219,10 @@ impl SessionRunner { max_body, |msg| self.send_shaped(msg, pc, shaper_application), )?; + atomic_saturating_sub( + &self.counters.ipv6_prefixes_advertised, + count, + ); } } Ok(()) @@ -8161,6 +8311,20 @@ impl SessionRunner { Some(AddressFamily::Ipv6), ); } + + // Reset per-AFI NLRI gauge counters + self.counters + .ipv4_prefixes_advertised + .store(0, Ordering::Relaxed); + self.counters + .ipv4_prefixes_imported + .store(0, Ordering::Relaxed); + self.counters + .ipv6_prefixes_advertised + .store(0, Ordering::Relaxed); + self.counters + .ipv6_prefixes_imported + .store(0, Ordering::Relaxed); } /// Exit the established state into Idle. @@ -8252,6 +8416,7 @@ impl SessionRunner { .connection_retries .fetch_add(1, Ordering::Relaxed); session_timer!(self, connect_retry).stop(); + self.record_reset(ResetReason::AdministrativeReset); } StopReason::Shutdown => { @@ -8265,6 +8430,7 @@ impl SessionRunner { .connection_retries .fetch_add(1, Ordering::Relaxed); session_timer!(self, connect_retry).stop(); + self.record_reset(ResetReason::AdministrativeShutdown); } StopReason::FsmError => { @@ -8278,6 +8444,7 @@ impl SessionRunner { .connection_retries .fetch_add(1, Ordering::Relaxed); session_timer!(self, connect_retry).stop(); + self.record_reset(ResetReason::FsmError); } StopReason::HoldTimeExpired => { @@ -8296,6 +8463,7 @@ impl SessionRunner { .connection_retries .fetch_add(1, Ordering::Relaxed); session_timer!(self, connect_retry).stop(); + self.record_reset(ResetReason::HoldTimerExpired); } StopReason::ConnectionRejected => { @@ -8305,6 +8473,7 @@ impl SessionRunner { if let Some(c2) = conn2 { self.send_rejected_notification(c2); } + self.record_reset(ResetReason::ConnectionRejected); } StopReason::CollisionResolution => { @@ -8314,8 +8483,17 @@ impl SessionRunner { if let Some(c2) = conn2 { self.send_collision_resolution_notification(c2); } + self.record_reset(ResetReason::CollisionResolution); + } + + StopReason::IoError => { + self.record_reset(ResetReason::IoError); + } + + StopReason::NotificationReceived(ref m) => { + self.record_notification_received(m); + self.record_reset(ResetReason::NotificationReceived); } - StopReason::IoError => {} StopReason::ParseError { error_code, @@ -8331,6 +8509,7 @@ impl SessionRunner { .connection_retries .fetch_add(1, Ordering::Relaxed); session_timer!(self, connect_retry).stop(); + self.record_reset(ResetReason::ParseError); } } @@ -8742,6 +8921,10 @@ impl SessionRunner { self.db .remove_prefixes_from_bgp_peer(&withdrawn, &self.peer_id()); + atomic_saturating_sub( + &self.counters.ipv4_prefixes_imported, + withdrawn.len() as u64, + ); if treat_as_withdraw { self.counters @@ -8770,6 +8953,10 @@ impl SessionRunner { &nlri_as_withdrawn, &self.peer_id(), ); + atomic_saturating_sub( + &self.counters.ipv4_prefixes_imported, + nlri_as_withdrawn.len() as u64, + ); } else if let Some(nexthop) = update.nexthop4().map(IpAddr::V4) { let nlri: Vec = update .nlri @@ -8810,6 +8997,9 @@ impl SessionRunner { }; self.db.add_bgp_prefixes(&nlri, path.clone()); + self.counters + .ipv4_prefixes_imported + .fetch_add(nlri.len() as u64, Ordering::Relaxed); } } else if !update.nlri.is_empty() { session_log!( @@ -8861,6 +9051,10 @@ impl SessionRunner { &mp_withdrawn4, &self.peer_id(), ); + atomic_saturating_sub( + &self.counters.ipv4_prefixes_imported, + mp_withdrawn4.len() as u64, + ); } else { let mp_nexthop = match &reach4.nexthop { BgpNexthop::Ipv4(ip4) => IpAddr::V4(*ip4), @@ -8918,6 +9112,10 @@ impl SessionRunner { }; self.db.add_bgp_prefixes(&mp_nlri4, path4); + self.counters.ipv4_prefixes_imported.fetch_add( + mp_nlri4.len() as u64, + Ordering::Relaxed, + ); } } } @@ -8955,6 +9153,10 @@ impl SessionRunner { &withdrawn6, &self.peer_id(), ); + atomic_saturating_sub( + &self.counters.ipv6_prefixes_imported, + withdrawn6.len() as u64, + ); } else { let nexthop6 = match &reach6.nexthop { BgpNexthop::Ipv6Single(ip6) => IpAddr::V6(*ip6), @@ -9023,6 +9225,10 @@ impl SessionRunner { }; self.db.add_bgp_prefixes(&nlri6, path6); + self.counters.ipv6_prefixes_imported.fetch_add( + nlri6.len() as u64, + Ordering::Relaxed, + ); } } } @@ -9048,6 +9254,10 @@ impl SessionRunner { &mp_withdrawn4, &self.peer_id(), ); + atomic_saturating_sub( + &self.counters.ipv4_prefixes_imported, + mp_withdrawn4.len() as u64, + ); } MpUnreachNlri::Ipv6Unicast(unreach6) => { let originated6 = match self @@ -9082,6 +9292,10 @@ impl SessionRunner { &withdrawn6, &self.peer_id(), ); + atomic_saturating_sub( + &self.counters.ipv6_prefixes_imported, + withdrawn6.len() as u64, + ); } } } @@ -9430,6 +9644,10 @@ impl SessionRunner { let counters = self.get_counters(); let name = lock!(self.neighbor.name).clone(); let peer_group = self.neighbor.peer_group.clone(); + let last_reset = lock!(self.last_reset).clone(); + let last_notification_sent = lock!(self.last_notification_sent).clone(); + let last_notification_received = + lock!(self.last_notification_received).clone(); // Extract config and runtime state WITHOUT holding any locks long-term let (ipv4_unicast, ipv6_unicast, timer_config) = { @@ -9520,6 +9738,10 @@ impl SessionRunner { counters, ipv4_unicast, ipv6_unicast, + last_reset: last_reset.clone(), + last_notification_sent: last_notification_sent.clone(), + last_notification_received: last_notification_received + .clone(), } } ConnectionKind::Full(pc) => { @@ -9543,6 +9765,10 @@ impl SessionRunner { counters, ipv4_unicast, ipv6_unicast, + last_reset: last_reset.clone(), + last_notification_sent: last_notification_sent.clone(), + last_notification_received: last_notification_received + .clone(), } } }, @@ -9578,6 +9804,9 @@ impl SessionRunner { counters, ipv4_unicast, ipv6_unicast, + last_reset, + last_notification_sent, + last_notification_received, } } } diff --git a/bgp/src/test.rs b/bgp/src/test.rs index a48eec8b0..1db659130 100644 --- a/bgp/src/test.rs +++ b/bgp/src/test.rs @@ -8,7 +8,7 @@ use crate::{ connection_channel::{BgpConnectionChannel, BgpListenerChannel}, connection_tcp::{BgpConnectionTcp, BgpListenerTcp}, dispatcher::Dispatcher, - params::{Ipv4UnicastConfig, Ipv6UnicastConfig, JitterRange}, + params::{Ipv4UnicastConfig, Ipv6UnicastConfig, JitterRange, ResetReason}, router::{EnsureSessionResult, Router}, session::{ AdminEvent, ConnectionKind, FsmEvent, FsmStateKind, PeerId, @@ -676,6 +676,8 @@ fn basic_update_helper< (false, false) => "basic_update", }; + // Use passive/active pairing so the admin-reset cycle later in this + // test avoids connection collisions (both sides racing to reconnect). let routers = vec![ LogicalRouter { name: "r1".to_string(), @@ -690,7 +692,7 @@ fn basic_update_helper< route_exchange, r1_addr, r2_addr, - false, + true, // r1 passive ), }], }, @@ -707,7 +709,7 @@ fn basic_update_helper< route_exchange, r2_addr, r1_addr, - false, + false, // r2 active ), }], }, @@ -785,6 +787,75 @@ fn basic_update_helper< ); } + // Verify per-AFI NLRI gauge counters + wait_for_eq!( + r1_session.get_peer_info().counters.ipv4_prefixes_advertised, + TEST_ROUTE_COUNT as u64, + "r1 ipv4_prefixes_advertised" + ); + wait_for_eq!( + r2_session.get_peer_info().counters.ipv4_prefixes_imported, + TEST_ROUTE_COUNT as u64, + "r2 ipv4_prefixes_imported" + ); + assert_eq!( + r1_session.get_peer_info().counters.ipv6_prefixes_advertised, + 0, + "r1 ipv6_prefixes_advertised should be 0" + ); + + // Admin reset r1 and verify routes re-propagate from DB + let r1_est = r1_session + .counters + .transitions_to_established + .load(Ordering::Relaxed); + let r2_est = r2_session + .counters + .transitions_to_established + .load(Ordering::Relaxed); + r1_session + .event_tx + .send(FsmEvent::Admin(AdminEvent::Reset)) + .expect("reset r1"); + wait_for!( + r1_session + .counters + .transitions_to_established + .load(Ordering::Relaxed) + > r1_est, + "r1 should re-establish after reset" + ); + wait_for!( + r2_session + .counters + .transitions_to_established + .load(Ordering::Relaxed) + > r2_est, + "r2 should re-establish after r1 reset" + ); + + // Routes are re-announced from DB during session_setup + wait_for!( + prefixes_v4 + .iter() + .filter(|p| { + !r2.router.db.get_prefix_paths(p).is_empty() + }) + .count() + == TEST_ROUTE_COUNT, + "all IPv4 routes should re-arrive after reset" + ); + wait_for_eq!( + r1_session.get_peer_info().counters.ipv4_prefixes_advertised, + TEST_ROUTE_COUNT as u64, + "r1 ipv4_prefixes_advertised after reset" + ); + wait_for_eq!( + r2_session.get_peer_info().counters.ipv4_prefixes_imported, + TEST_ROUTE_COUNT as u64, + "r2 ipv4_prefixes_imported after reset" + ); + if let Some(initial_nh) = initial_nexthop { // Test nexthop override change let new_nexthop = match initial_nh { @@ -891,6 +962,75 @@ fn basic_update_helper< ); } + // Verify per-AFI NLRI gauge counters + wait_for_eq!( + r1_session.get_peer_info().counters.ipv6_prefixes_advertised, + TEST_ROUTE_COUNT as u64, + "r1 ipv6_prefixes_advertised" + ); + wait_for_eq!( + r2_session.get_peer_info().counters.ipv6_prefixes_imported, + TEST_ROUTE_COUNT as u64, + "r2 ipv6_prefixes_imported" + ); + assert_eq!( + r1_session.get_peer_info().counters.ipv4_prefixes_advertised, + 0, + "r1 ipv4_prefixes_advertised should be 0" + ); + + // Admin reset r1 and verify routes re-propagate from DB + let r1_est = r1_session + .counters + .transitions_to_established + .load(Ordering::Relaxed); + let r2_est = r2_session + .counters + .transitions_to_established + .load(Ordering::Relaxed); + r1_session + .event_tx + .send(FsmEvent::Admin(AdminEvent::Reset)) + .expect("reset r1"); + wait_for!( + r1_session + .counters + .transitions_to_established + .load(Ordering::Relaxed) + > r1_est, + "r1 should re-establish after reset" + ); + wait_for!( + r2_session + .counters + .transitions_to_established + .load(Ordering::Relaxed) + > r2_est, + "r2 should re-establish after r1 reset" + ); + + // Routes are re-announced from DB during session_setup + wait_for!( + prefixes_v6 + .iter() + .filter(|p| { + !r2.router.db.get_prefix_paths(p).is_empty() + }) + .count() + == TEST_ROUTE_COUNT, + "all IPv6 routes should re-arrive after reset" + ); + wait_for_eq!( + r1_session.get_peer_info().counters.ipv6_prefixes_advertised, + TEST_ROUTE_COUNT as u64, + "r1 ipv6_prefixes_advertised after reset" + ); + wait_for_eq!( + r2_session.get_peer_info().counters.ipv6_prefixes_imported, + TEST_ROUTE_COUNT as u64, + "r2 ipv6_prefixes_imported after reset" + ); + if let Some(initial_nh) = initial_nexthop { // Test nexthop override change let new_nexthop = match initial_nh { @@ -1041,6 +1181,98 @@ fn basic_update_helper< ); } + // Verify per-AFI NLRI gauge counters (dual-stack) + wait_for_eq!( + r1_session.get_peer_info().counters.ipv4_prefixes_advertised, + TEST_ROUTE_COUNT as u64, + "r1 ipv4_prefixes_advertised (dual-stack)" + ); + wait_for_eq!( + r1_session.get_peer_info().counters.ipv6_prefixes_advertised, + TEST_ROUTE_COUNT as u64, + "r1 ipv6_prefixes_advertised (dual-stack)" + ); + wait_for_eq!( + r2_session.get_peer_info().counters.ipv4_prefixes_imported, + TEST_ROUTE_COUNT as u64, + "r2 ipv4_prefixes_imported (dual-stack)" + ); + wait_for_eq!( + r2_session.get_peer_info().counters.ipv6_prefixes_imported, + TEST_ROUTE_COUNT as u64, + "r2 ipv6_prefixes_imported (dual-stack)" + ); + + // Admin reset r1 and verify routes re-propagate from DB + let r1_est = r1_session + .counters + .transitions_to_established + .load(Ordering::Relaxed); + let r2_est = r2_session + .counters + .transitions_to_established + .load(Ordering::Relaxed); + r1_session + .event_tx + .send(FsmEvent::Admin(AdminEvent::Reset)) + .expect("reset r1"); + wait_for!( + r1_session + .counters + .transitions_to_established + .load(Ordering::Relaxed) + > r1_est, + "r1 should re-establish after reset" + ); + wait_for!( + r2_session + .counters + .transitions_to_established + .load(Ordering::Relaxed) + > r2_est, + "r2 should re-establish after r1 reset" + ); + + // Routes are re-announced from DB during session_setup + wait_for!( + { + let v4_count = prefixes_v4 + .iter() + .filter(|p| { + !r2.router.db.get_prefix_paths(p).is_empty() + }) + .count(); + let v6_count = prefixes_v6 + .iter() + .filter(|p| { + !r2.router.db.get_prefix_paths(p).is_empty() + }) + .count(); + v4_count == TEST_ROUTE_COUNT && v6_count == TEST_ROUTE_COUNT + }, + "all dual-stack routes should re-arrive after reset" + ); + wait_for_eq!( + r1_session.get_peer_info().counters.ipv4_prefixes_advertised, + TEST_ROUTE_COUNT as u64, + "r1 ipv4_prefixes_advertised after reset (dual-stack)" + ); + wait_for_eq!( + r1_session.get_peer_info().counters.ipv6_prefixes_advertised, + TEST_ROUTE_COUNT as u64, + "r1 ipv6_prefixes_advertised after reset (dual-stack)" + ); + wait_for_eq!( + r2_session.get_peer_info().counters.ipv4_prefixes_imported, + TEST_ROUTE_COUNT as u64, + "r2 ipv4_prefixes_imported after reset (dual-stack)" + ); + wait_for_eq!( + r2_session.get_peer_info().counters.ipv6_prefixes_imported, + TEST_ROUTE_COUNT as u64, + "r2 ipv6_prefixes_imported after reset (dual-stack)" + ); + // Test nexthop override changes if any were configured if ipv4_nexthop.is_some() || ipv6_nexthop.is_some() { let new_ipv4_nexthop = @@ -1318,8 +1550,8 @@ fn test_basic_peering_passive() { basic_peering_helper::( true, RouteExchange::Ipv4 { nexthop: None }, - sockaddr!(&format!("11.0.0.1:{TEST_BGP_PORT}")), - sockaddr!(&format!("11.0.0.2:{TEST_BGP_PORT}")), + sockaddr!(&format!("10.0.0.3:{TEST_BGP_PORT}")), + sockaddr!(&format!("10.0.0.4:{TEST_BGP_PORT}")), ) } @@ -1328,8 +1560,8 @@ fn test_basic_peering_active() { basic_peering_helper::( false, RouteExchange::Ipv4 { nexthop: None }, - sockaddr!(&format!("12.0.0.1:{TEST_BGP_PORT}")), - sockaddr!(&format!("12.0.0.2:{TEST_BGP_PORT}")), + sockaddr!(&format!("10.0.0.5:{TEST_BGP_PORT}")), + sockaddr!(&format!("10.0.0.6:{TEST_BGP_PORT}")), ) } @@ -1856,8 +2088,8 @@ fn test_basic_update_ipv6() { fn test_basic_update_ipv6_tcp() { basic_update_helper::( RouteExchange::Ipv6 { nexthop: None }, - sockaddr!(&format!("[3fff::a]:{TEST_BGP_PORT}")), - sockaddr!(&format!("[3fff::b]:{TEST_BGP_PORT}")), + sockaddr!(&format!("[3fff::2]:{TEST_BGP_PORT}")), + sockaddr!(&format!("[3fff::3]:{TEST_BGP_PORT}")), ) } @@ -1866,8 +2098,8 @@ fn test_ipv6_basic_peering_passive() { basic_peering_helper::( true, RouteExchange::Ipv6 { nexthop: None }, - sockaddr!(&format!("[3fff::2]:{TEST_BGP_PORT}")), - sockaddr!(&format!("[3fff::3]:{TEST_BGP_PORT}")), + sockaddr!(&format!("[3fff::4]:{TEST_BGP_PORT}")), + sockaddr!(&format!("[3fff::5]:{TEST_BGP_PORT}")), ) } @@ -1876,8 +2108,8 @@ fn test_ipv6_basic_peering_active() { basic_peering_helper::( false, RouteExchange::Ipv6 { nexthop: None }, - sockaddr!(&format!("[3fff::4]:{TEST_BGP_PORT}")), - sockaddr!(&format!("[3fff::5]:{TEST_BGP_PORT}")), + sockaddr!(&format!("[3fff::6]:{TEST_BGP_PORT}")), + sockaddr!(&format!("[3fff::7]:{TEST_BGP_PORT}")), ) } @@ -1886,8 +2118,8 @@ fn test_ipv6_basic_peering_passive_tcp() { basic_peering_helper::( true, RouteExchange::Ipv6 { nexthop: None }, - sockaddr!(&format!("[3fff::6]:{TEST_BGP_PORT}")), - sockaddr!(&format!("[3fff::7]:{TEST_BGP_PORT}")), + sockaddr!(&format!("[3fff::8]:{TEST_BGP_PORT}")), + sockaddr!(&format!("[3fff::9]:{TEST_BGP_PORT}")), ) } @@ -1896,8 +2128,8 @@ fn test_ipv6_basic_peering_active_tcp() { basic_peering_helper::( false, RouteExchange::Ipv6 { nexthop: None }, - sockaddr!(&format!("[3fff::8]:{TEST_BGP_PORT}")), - sockaddr!(&format!("[3fff::9]:{TEST_BGP_PORT}")), + sockaddr!(&format!("[3fff::a]:{TEST_BGP_PORT}")), + sockaddr!(&format!("[3fff::b]:{TEST_BGP_PORT}")), ) } @@ -1915,8 +2147,8 @@ fn test_dual_stack_routes_ipv4_peer_success() { ipv4_nexthop: Some(ip!("10.0.1.1")), ipv6_nexthop: Some(ip!("3fff:db8:1::1")), }, - sockaddr!(&format!("10.0.1.1:{TEST_BGP_PORT}")), - sockaddr!(&format!("10.0.1.2:{TEST_BGP_PORT}")), + sockaddr!(&format!("10.0.0.11:{TEST_BGP_PORT}")), + sockaddr!(&format!("10.0.0.12:{TEST_BGP_PORT}")), ) } @@ -1928,8 +2160,8 @@ fn test_dual_stack_routes_ipv6_peer_success() { ipv4_nexthop: Some(ip!("10.0.2.1")), ipv6_nexthop: Some(ip!("3fff:db8:2::1")), }, - sockaddr!(&format!("[3fff::f]:{TEST_BGP_PORT}")), sockaddr!(&format!("[3fff::10]:{TEST_BGP_PORT}")), + sockaddr!(&format!("[3fff::11]:{TEST_BGP_PORT}")), ) } @@ -1940,8 +2172,8 @@ fn test_ipv4_routes_ipv6_peer_success() { RouteExchange::Ipv4 { nexthop: Some(ip!("10.0.3.1")), }, - sockaddr!(&format!("[3fff::11]:{TEST_BGP_PORT}")), sockaddr!(&format!("[3fff::12]:{TEST_BGP_PORT}")), + sockaddr!(&format!("[3fff::13]:{TEST_BGP_PORT}")), ) } @@ -1952,8 +2184,8 @@ fn test_ipv6_routes_ipv4_peer_success() { RouteExchange::Ipv6 { nexthop: Some(ip!("3fff:db8:4::1")), }, - sockaddr!(&format!("10.0.4.1:{TEST_BGP_PORT}")), - sockaddr!(&format!("10.0.4.2:{TEST_BGP_PORT}")), + sockaddr!(&format!("10.0.0.7:{TEST_BGP_PORT}")), + sockaddr!(&format!("10.0.0.8:{TEST_BGP_PORT}")), ) } @@ -4654,3 +4886,236 @@ fn test_mismatched_afi_session_rejected() { r1.shutdown(); r2.shutdown(); } + +/// Test: Reset/notification tracking fields in PeerInfo. +/// +/// Exercises FsmResetRecord, last_notification_sent/received, +/// reset_count, and timestamp monotonicity through admin resets. +/// +/// Uses passive/active pairing to avoid connection collisions that +/// would overwrite last_reset with CollisionResolution. +#[test] +fn test_reset_tracking() { + let r1_addr = sockaddr!(&format!("10.0.0.9:{TEST_BGP_PORT}")); + let r2_addr = sockaddr!(&format!("10.0.0.10:{TEST_BGP_PORT}")); + + let routers = vec![ + LogicalRouter { + name: "r1".to_string(), + asn: Asn::FourOctet(4200000001), + id: 1, + listen_addr: r1_addr, + bind_addr: Some(r1_addr), + neighbors: vec![NeighborConfig { + peer_name: "r2".to_string(), + remote_host: r2_addr, + session_info: create_test_session_info( + RouteExchange::Ipv4 { nexthop: None }, + r1_addr, + r2_addr, + true, // r1 passive + ), + }], + }, + LogicalRouter { + name: "r2".to_string(), + asn: Asn::FourOctet(4200000002), + id: 2, + listen_addr: r2_addr, + bind_addr: Some(r2_addr), + neighbors: vec![NeighborConfig { + peer_name: "r1".to_string(), + remote_host: r1_addr, + session_info: create_test_session_info( + RouteExchange::Ipv4 { nexthop: None }, + r2_addr, + r1_addr, + false, // r2 active + ), + }], + }, + ]; + + let (test_routers, _ip_guard) = test_setup::< + BgpConnectionChannel, + BgpListenerChannel, + >("reset_tracking", &routers); + + let r1 = &test_routers[0]; + let r2 = &test_routers[1]; + + let r1_session = r1 + .router + .get_session(r2_addr.ip()) + .expect("get r1->r2 session"); + let r2_session = r2 + .router + .get_session(r1_addr.ip()) + .expect("get r2->r1 session"); + + // Wait for initial establishment + wait_for_eq!(r1_session.state(), FsmStateKind::Established); + wait_for_eq!(r2_session.state(), FsmStateKind::Established); + + // Phase A: No reset has occurred yet + assert!( + r1_session.get_peer_info().last_reset.is_none(), + "last_reset should be None before any reset" + ); + + // Record establishment counts before reset. Channel-based connections + // cycle through states so fast that polling state() may miss the + // transient Idle — use the counter to reliably detect re-establishment. + let r1_established_before = r1_session + .counters + .transitions_to_established + .load(Ordering::Relaxed); + let r2_established_before = r2_session + .counters + .transitions_to_established + .load(Ordering::Relaxed); + + // Send AdminEvent::Reset to r1 + r1_session + .event_tx + .send(FsmEvent::Admin(AdminEvent::Reset)) + .expect("send reset to r1"); + + // Wait for both to re-establish (counter-based, not state-based) + wait_for!( + r1_session + .counters + .transitions_to_established + .load(Ordering::Relaxed) + > r1_established_before, + "r1 should re-establish after reset" + ); + wait_for!( + r2_session + .counters + .transitions_to_established + .load(Ordering::Relaxed) + > r2_established_before, + "r2 should re-establish after r1 reset" + ); + wait_for_eq!(r1_session.state(), FsmStateKind::Established); + wait_for_eq!(r2_session.state(), FsmStateKind::Established); + + let r1_info = r1_session.get_peer_info(); + assert!( + r1_info.last_reset.is_some(), + "last_reset should be set after admin reset" + ); + assert!( + matches!( + r1_info.last_reset.as_ref().unwrap().reason, + ResetReason::AdministrativeReset + ), + "reset reason should be AdministrativeReset, got {:?}", + r1_info.last_reset.as_ref().unwrap().reason + ); + assert!( + r1_info.counters.reset_count >= 1, + "reset_count should be >= 1 after admin reset, got {}", + r1_info.counters.reset_count + ); + assert!( + r1_info.last_notification_sent.is_some(), + "last_notification_sent should be set (CEASE sent on reset)" + ); + + // Phase B: Reset r2 so r1 receives a notification + let r1_established_before = r1_session + .counters + .transitions_to_established + .load(Ordering::Relaxed); + let r2_established_before = r2_session + .counters + .transitions_to_established + .load(Ordering::Relaxed); + + r2_session + .event_tx + .send(FsmEvent::Admin(AdminEvent::Reset)) + .expect("send reset to r2"); + + // Wait for both to re-establish + wait_for!( + r1_session + .counters + .transitions_to_established + .load(Ordering::Relaxed) + > r1_established_before, + "r1 should re-establish after r2 reset" + ); + wait_for!( + r2_session + .counters + .transitions_to_established + .load(Ordering::Relaxed) + > r2_established_before, + "r2 should re-establish after reset" + ); + wait_for_eq!(r1_session.state(), FsmStateKind::Established); + wait_for_eq!(r2_session.state(), FsmStateKind::Established); + + let r1_info = r1_session.get_peer_info(); + assert!( + r1_info.last_notification_received.is_some(), + "r1 should have received a notification from r2's reset" + ); + assert!( + matches!( + r1_info.last_reset.as_ref().unwrap().reason, + ResetReason::NotificationReceived + ), + "r1's last reset reason should be NotificationReceived, \ + got {:?}", + r1_info.last_reset.as_ref().unwrap().reason + ); + + let r2_info = r2_session.get_peer_info(); + assert!( + matches!( + r2_info.last_reset.as_ref().unwrap().reason, + ResetReason::AdministrativeReset + ), + "r2's last reset reason should be AdministrativeReset, \ + got {:?}", + r2_info.last_reset.as_ref().unwrap().reason + ); + + // Phase C: Timestamp monotonicity + let old_timestamp = + r1_session.get_peer_info().last_reset.unwrap().timestamp; + let r1_established_before = r1_session + .counters + .transitions_to_established + .load(Ordering::Relaxed); + + r1_session + .event_tx + .send(FsmEvent::Admin(AdminEvent::Reset)) + .expect("send second reset to r1"); + + wait_for!( + r1_session + .counters + .transitions_to_established + .load(Ordering::Relaxed) + > r1_established_before, + "r1 should re-establish after second reset" + ); + wait_for_eq!(r1_session.state(), FsmStateKind::Established); + + let new_timestamp = + r1_session.get_peer_info().last_reset.unwrap().timestamp; + assert!( + new_timestamp > old_timestamp, + "new reset timestamp ({new_timestamp}) should be after \ + old ({old_timestamp})" + ); + + r1.shutdown(); + r2.shutdown(); +} diff --git a/falcon-lab/src/test.rs b/falcon-lab/src/test.rs index 67e030b2f..5365e9497 100644 --- a/falcon-lab/src/test.rs +++ b/falcon-lab/src/test.rs @@ -139,7 +139,7 @@ pub async fn run_trio_unnumbered_test( .context("announce v6 prefix")?; wait_for_eq!( - mgd.get_neighbors_v4(local_asn) + mgd.get_neighbors_v5(local_asn) .await .map(|x| x.into_inner().len()) .unwrap_or(0), @@ -148,7 +148,7 @@ pub async fn run_trio_unnumbered_test( ); wait_for_eq!( - mgd.get_neighbors_v4(local_asn) + mgd.get_neighbors_v5(local_asn) .await .map(|x| x.into_inner().values().nth(0).map(|y| y.fsm_state)) .unwrap_or(None), diff --git a/mg-api/src/lib.rs b/mg-api/src/lib.rs index d0854af93..a07a70de4 100644 --- a/mg-api/src/lib.rs +++ b/mg-api/src/lib.rs @@ -15,7 +15,7 @@ use bgp::{ params::{ ApplyRequest, ApplyRequestV1, CheckerSource, Neighbor, NeighborResetOp, NeighborResetOpV1, NeighborV1, Origin4, Origin6, PeerInfo, PeerInfoV1, - PeerInfoV2, Router, ShaperSource, UnnumberedNeighbor, + PeerInfoV2, PeerInfoV3, Router, ShaperSource, UnnumberedNeighbor, }, session::{ FsmEventRecord, MessageHistoryEntry, MessageHistoryEntryV2, @@ -427,12 +427,18 @@ pub trait MgAdminApi { async fn get_neighbors_v3( rqctx: RequestContext, request: Query, - ) -> Result>, HttpError>; + ) -> Result>, HttpError>; - #[endpoint { method = GET, path = "/bgp/status/neighbors", versions = VERSION_UNNUMBERED.. }] + #[endpoint { method = GET, path = "/bgp/status/neighbors", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] async fn get_neighbors_v4( rqctx: RequestContext, request: Query, + ) -> Result>, HttpError>; + + #[endpoint { method = GET, path = "/bgp/status/neighbors", versions = VERSION_EXTENDED_NH_STATIC.. }] + async fn get_neighbors_v5( + rqctx: RequestContext, + request: Query, ) -> Result>, HttpError>; // V1/V2 API - ApplyRequestV1 with combined import/export policies diff --git a/mgadm/src/bgp.rs b/mgadm/src/bgp.rs index 054b025dc..c2d6511c7 100644 --- a/mgadm/src/bgp.rs +++ b/mgadm/src/bgp.rs @@ -1065,7 +1065,7 @@ async fn get_neighbors( asn: u32, mode: NeighborDisplayMode, ) -> Result<()> { - let result = c.get_neighbors_v4(asn).await?.into_inner(); + let result = c.get_neighbors_v5(asn).await?.into_inner(); let mut sorted: Vec<_> = result.iter().collect(); // Sort using natural sorting to handle both IP addresses and interface names @@ -1240,10 +1240,21 @@ fn display_neighbors_detail( println_nopipe!("\n{}", "Counters:".bold()); println_nopipe!(" Prefixes:"); println_nopipe!( - " Advertised: {}", - info.counters.prefixes_advertised + " IPv4 Advertised: {}", + info.counters.ipv4_prefixes_advertised + ); + println_nopipe!( + " IPv4 Imported: {}", + info.counters.ipv4_prefixes_imported + ); + println_nopipe!( + " IPv6 Advertised: {}", + info.counters.ipv6_prefixes_advertised + ); + println_nopipe!( + " IPv6 Imported: {}", + info.counters.ipv6_prefixes_imported ); - println_nopipe!(" Imported: {}", info.counters.prefixes_imported); println_nopipe!(" Messages Sent:"); println_nopipe!(" Opens: {}", info.counters.opens_sent); diff --git a/mgd/src/admin.rs b/mgd/src/admin.rs index 6ce5ce2a1..68d1a5fa3 100644 --- a/mgd/src/admin.rs +++ b/mgd/src/admin.rs @@ -448,17 +448,24 @@ impl MgAdminApi for MgAdminApiImpl { async fn get_neighbors_v3( ctx: RequestContext, request: Query, - ) -> Result>, HttpError> { + ) -> Result>, HttpError> { bgp_admin::get_neighbors_v3(ctx, request).await } async fn get_neighbors_v4( ctx: RequestContext, request: Query, - ) -> Result>, HttpError> { + ) -> Result>, HttpError> { bgp_admin::get_neighbors_unified(ctx, request).await } + async fn get_neighbors_v5( + ctx: RequestContext, + request: Query, + ) -> Result>, HttpError> { + bgp_admin::get_neighbors_v5(ctx, request).await + } + async fn bgp_apply( ctx: RequestContext, request: TypedBody, diff --git a/mgd/src/bgp_admin.rs b/mgd/src/bgp_admin.rs index a4968b527..bbef4068d 100644 --- a/mgd/src/bgp_admin.rs +++ b/mgd/src/bgp_admin.rs @@ -1246,7 +1246,7 @@ pub async fn get_neighbors_v2( pub async fn get_neighbors_v3( ctx: RequestContext>, request: Query, -) -> Result>, HttpError> { +) -> Result>, HttpError> { let rq = request.into_inner(); let ctx = ctx.context(); @@ -1267,7 +1267,7 @@ pub async fn get_neighbors_v3( PeerId::Ip(ip) => ip, PeerId::Interface(_) => continue, // Skip unnumbered sessions }; - peers.insert(peer_ip, s.get_peer_info()); + peers.insert(peer_ip, PeerInfoV3::from(s.get_peer_info())); } Ok(HttpResponseOk(peers)) @@ -1276,7 +1276,7 @@ pub async fn get_neighbors_v3( pub async fn get_neighbors_unified( ctx: RequestContext>, request: Query, -) -> Result>, HttpError> { +) -> Result>, HttpError> { let rq = request.into_inner(); let ctx = ctx.context(); @@ -1294,6 +1294,33 @@ pub async fn get_neighbors_unified( for s in sessions.iter() { // Use PeerId Display impl as HashMap key + let peer_key = s.neighbor.peer.to_string(); + peers.insert(peer_key, PeerInfoV3::from(s.get_peer_info())); + } + + Ok(HttpResponseOk(peers)) +} + +pub async fn get_neighbors_v5( + ctx: RequestContext>, + request: Query, +) -> Result>, HttpError> { + let rq = request.into_inner(); + let ctx = ctx.context(); + + let mut peers = HashMap::new(); + + // Clone sessions while holding locks, then release them + let sessions: Vec<_> = { + let routers = lock!(ctx.bgp.router); + let r = routers.get(&rq.asn).ok_or(HttpError::for_not_found( + None, + "ASN not found".to_string(), + ))?; + lock!(r.sessions).values().cloned().collect() + }; + + for s in sessions.iter() { let peer_key = s.neighbor.peer.to_string(); peers.insert(peer_key, s.get_peer_info()); } diff --git a/mgd/src/oxstats.rs b/mgd/src/oxstats.rs index eb8a918fb..7187baf49 100644 --- a/mgd/src/oxstats.rs +++ b/mgd/src/oxstats.rs @@ -359,26 +359,44 @@ impl Stats { UpdatesReceived, counters.updates_received )); - samples.push(bgp_session_counter!( - self.hostname.clone().into(), - self.rack_id, - self.sled_id, - self.start_time, - *asn, - *addr, - PrefixesAdvertised, - counters.prefixes_advertised - )); - samples.push(bgp_session_counter!( - self.hostname.clone().into(), - self.rack_id, - self.sled_id, - self.start_time, - *asn, - *addr, - PrefixesImported, - counters.prefixes_imported - )); + let prefixes_advertised = counters + .ipv4_prefixes_advertised + .load(Ordering::Relaxed) + + counters.ipv6_prefixes_advertised.load(Ordering::Relaxed); + samples.push(Sample::new( + &BgpSession { + hostname: self.hostname.clone().into(), + rack_id: self.rack_id, + sled_id: self.sled_id, + local_asn: *asn, + peer: *addr, + }, + &PrefixesAdvertised { + datum: Cumulative::::with_start_time( + self.start_time, + prefixes_advertised, + ), + }, + )?); + let prefixes_imported = counters + .ipv4_prefixes_imported + .load(Ordering::Relaxed) + + counters.ipv6_prefixes_imported.load(Ordering::Relaxed); + samples.push(Sample::new( + &BgpSession { + hostname: self.hostname.clone().into(), + rack_id: self.rack_id, + sled_id: self.sled_id, + local_asn: *asn, + peer: *addr, + }, + &PrefixesImported { + datum: Cumulative::::with_start_time( + self.start_time, + prefixes_imported, + ), + }, + )?); samples.push(bgp_session_counter!( self.hostname.clone().into(), self.rack_id, diff --git a/openapi/mg-admin/mg-admin-7.0.0-837dad.json b/openapi/mg-admin/mg-admin-7.0.0-26a5e9.json similarity index 98% rename from openapi/mg-admin/mg-admin-7.0.0-837dad.json rename to openapi/mg-admin/mg-admin-7.0.0-26a5e9.json index f5e9cf8e9..65d2dfc16 100644 --- a/openapi/mg-admin/mg-admin-7.0.0-837dad.json +++ b/openapi/mg-admin/mg-admin-7.0.0-26a5e9.json @@ -1196,7 +1196,7 @@ }, "/bgp/status/neighbors": { "get": { - "operationId": "get_neighbors_v4", + "operationId": "get_neighbors_v5", "parameters": [ { "in": "query", @@ -3197,6 +3197,23 @@ "by_peer" ] }, + "FsmResetRecord": { + "description": "Information about the most recent session reset.", + "type": "object", + "properties": { + "reason": { + "$ref": "#/components/schemas/ResetReason" + }, + "timestamp": { + "type": "string", + "format": "date-time" + } + }, + "required": [ + "reason", + "timestamp" + ] + }, "FsmStateKind": { "description": "Simplified representation of a BGP state without having to carry a connection.", "oneOf": [ @@ -4171,6 +4188,23 @@ "error_subcode" ] }, + "NotificationRecord": { + "description": "A record of a BGP notification message with a timestamp.", + "type": "object", + "properties": { + "notification": { + "$ref": "#/components/schemas/NotificationMessage" + }, + "timestamp": { + "type": "string", + "format": "date-time" + } + }, + "required": [ + "notification", + "timestamp" + ] + }, "OpenErrorSubcode": { "description": "Open message error subcode types", "type": "string", @@ -4688,7 +4722,7 @@ ] }, "PeerCounters": { - "description": "Session-level counters that persist across connection changes These serve as aggregate counters across all connections for the session", + "description": "Session-level counters with per-AFI NLRI gauges (v7+ API).", "type": "object", "properties": { "active_connections_accepted": { @@ -4721,6 +4755,26 @@ "format": "uint64", "minimum": 0 }, + "ipv4_prefixes_advertised": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "ipv4_prefixes_imported": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "ipv6_prefixes_advertised": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, + "ipv6_prefixes_imported": { + "type": "integer", + "format": "uint64", + "minimum": 0 + }, "keepalive_send_failure": { "type": "integer", "format": "uint64", @@ -4786,12 +4840,7 @@ "format": "uint64", "minimum": 0 }, - "prefixes_advertised": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "prefixes_imported": { + "reset_count": { "type": "integer", "format": "uint64", "minimum": 0 @@ -4905,6 +4954,11 @@ "type": "integer", "format": "uint64", "minimum": 0 + }, + "updates_treated_as_withdraw": { + "type": "integer", + "format": "uint64", + "minimum": 0 } }, "required": [ @@ -4914,6 +4968,10 @@ "connector_panics", "hold_timer_expirations", "idle_hold_timer_expirations", + "ipv4_prefixes_advertised", + "ipv4_prefixes_imported", + "ipv6_prefixes_advertised", + "ipv6_prefixes_imported", "keepalive_send_failure", "keepalives_received", "keepalives_sent", @@ -4927,8 +4985,7 @@ "opens_sent", "passive_connections_accepted", "passive_connections_declined", - "prefixes_advertised", - "prefixes_imported", + "reset_count", "route_refresh_received", "route_refresh_send_failure", "route_refresh_sent", @@ -4950,7 +5007,8 @@ "update_nexhop_missing", "update_send_failure", "updates_received", - "updates_sent" + "updates_sent", + "updates_treated_as_withdraw" ] }, "PeerId": { @@ -4986,6 +5044,7 @@ ] }, "PeerInfo": { + "description": "Peer info for v7+ API (per-AFI counters, reset/notification tracking).", "type": "object", "properties": { "asn": { @@ -5015,6 +5074,30 @@ "ipv6_unicast": { "$ref": "#/components/schemas/Ipv6UnicastConfig" }, + "last_notification_received": { + "nullable": true, + "allOf": [ + { + "$ref": "#/components/schemas/NotificationRecord" + } + ] + }, + "last_notification_sent": { + "nullable": true, + "allOf": [ + { + "$ref": "#/components/schemas/NotificationRecord" + } + ] + }, + "last_reset": { + "nullable": true, + "allOf": [ + { + "$ref": "#/components/schemas/FsmResetRecord" + } + ] + }, "local_ip": { "type": "string", "format": "ip" @@ -5172,6 +5255,21 @@ "value" ] }, + "ResetReason": { + "description": "Reason for the most recent session reset.", + "type": "string", + "enum": [ + "AdministrativeReset", + "AdministrativeShutdown", + "HoldTimerExpired", + "FsmError", + "ConnectionRejected", + "CollisionResolution", + "IoError", + "ParseError", + "NotificationReceived" + ] + }, "Rib": { "type": "object", "additionalProperties": { diff --git a/openapi/mg-admin/mg-admin-latest.json b/openapi/mg-admin/mg-admin-latest.json index d6d5eb58b..3d80ea1f5 120000 --- a/openapi/mg-admin/mg-admin-latest.json +++ b/openapi/mg-admin/mg-admin-latest.json @@ -1 +1 @@ -mg-admin-7.0.0-837dad.json \ No newline at end of file +mg-admin-7.0.0-26a5e9.json \ No newline at end of file From a1462094debad4ddb90045d1c73856a30d6b49c4 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Tue, 24 Feb 2026 08:04:06 -0700 Subject: [PATCH 19/58] Add dev docs --- docs/developer-guide.md | 364 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 364 insertions(+) create mode 100644 docs/developer-guide.md diff --git a/docs/developer-guide.md b/docs/developer-guide.md new file mode 100644 index 000000000..16f842aac --- /dev/null +++ b/docs/developer-guide.md @@ -0,0 +1,364 @@ +# Maghemite Developer Guide + +A suite of routing protocol implementations in Rust for +[Oxide Computer Company](https://oxide.computer)'s rack architecture. +Implements upper-half routing protocols (BGP, DDM, BFD, static) with +support for illumos/Helios and Dendrite/Sidecar data planes. + +## Tech Stack + +- **Language**: Rust 1.92.0 (pinned in `rust-toolchain.toml`) +- **Async runtime**: Tokio via `oxide_tokio_rt::run` (clippy + disallows `#[tokio::main]`) +- **HTTP API framework**: Dropshot (server) + Progenitor (client + generation from OpenAPI) +- **Logging**: slog (structured, bunyan JSON for daemons) +- **Persistence**: sled key-value store (RIB database) +- **Policy engine**: Rhai scripting (BGP policies) +- **Metrics**: Oximeter integration +- **Formatting**: `rustfmt.toml` — edition 2024, max_width 80 + +## Workspace Structure + +### Daemons +- **`mgd`** — Main external routing daemon. Manages BGP, BFD, + static routing, and mg-lower. Admin API on `[::]:4676`, oximeter + on `4677`. BGP listens on TCP 179. +- **`ddmd`** — DDM (Delay Driven Multipath) underlay routing daemon. + Admin API on `[::]:8000`, exchange protocol on TCP `0xdddd` + (57053). + +### CLI Tools +- **`mgadm`** — Admin CLI for mgd. Subcommands: `bgp`, `static`, + `bfd`, `rib`, `ndp`. +- **`ddmadm`** — Admin CLI for ddmd. Commands: get-peers, + get-prefixes, advertise/withdraw, tunnel endpoints. + +### Protocol Libraries +- **`bgp`** — BGP-4 (RFC 4271) + MP-BGP (IPv4/IPv6 unicast) + BGP + unnumbered. Single-FSM-per-peer design. ~31K lines, largest + component. Key files: `session.rs` (FSM), `messages.rs` + (protocol), `router.rs` (management), `policy.rs` (Rhai policies), + `unnumbered.rs` (link-local). +- **`bfd`** — Bidirectional Forwarding Detection. Per-session state + machines, active/passive modes. +- **`ddm`** — DDM state machine per interface. Discovery via + multicast, exchange via path-vector routing. +- **`ndp`** — Neighbor Discovery Protocol for BGP unnumbered peer + discovery. + +### Data & Routing +- **`rdb`** — Routing Information Base shared by BGP, BFD, static, + DDM. Separate in/loc RIBs for IPv4/IPv6. Bestpath algorithm: + shutdown filter -> RIB priority -> local_pref -> AS path length -> + MED. ECMP support. Persistent via sled at `/var/run/rdb`. +- **`rdb-types`** — Shared types for the RIB. +- **`mg-lower`** — Syncs RIB to data plane (Dendrite). Watches RIB + for changes, full sync on startup + incremental updates. Platform + traits: `Dpd`, `Ddm`, `SwitchZone`. +- **`mg-common`** — Shared utilities: CLI styling, logging, + networking, stats, SMF, threading. Macros: `lock!`, `read_lock!`, + `write_lock!`, `println_nopipe!`. + +### API Layer +- **`mg-api`** — Dropshot API trait for mgd. Versioned API (see + [API Type Versioning](#api-type-versioning)). +- **`ddm-api`** — Dropshot API trait for ddmd. +- **`mg-admin-client`** — Progenitor-generated Rust client for mgd. +- **`ddm-admin-client`** — Progenitor-generated Rust client for + ddmd. +- **`dropshot-apis`** — OpenAPI doc management binary. Run via + `cargo xtask openapi`. +- **`openapi/`** — OpenAPI specs for both APIs. + +### Testing & Labs +- **`tests/`** — Integration tests using `ztest` (illumos + zone-based). DDM multi-zone topologies. +- **`bgp/src/test.rs`** — BGP unit/integration tests. Uses loopback + IPs for TCP, channels for unit tests. +- **`clab/`** — Containerlab configs with FRRouting peers. +- **`interop-lab/`** — Docker-based multi-vendor BGP testing. +- **`falcon-lab/`** — Libfalcon-based rack simulation testing. +- **`lab/`** — Solo/duo/trio/quartet test binaries. + +### Build & Packaging +- **`xtask/`** — Cargo xtask for workspace tasks (currently: OpenAPI + management). +- **`package/`** — IPS packaging for illumos (`.p5p` archives). +- **`smf/`** — Service Management Facility manifests and method + scripts for ddmd and mgd. +- **`pkg/`** — Additional packaging resources. +- **`.github/buildomat/`** — CI jobs: build, test-bgp, test-bfd, + test-ddm-trio/quartet, test-interop. Targets Helios 2.0. + +### Other +- **`util/`** — General utilities. +- **`mg-ddm-verify`** — DDM verification tool. +- **`docs/`** — Architecture and developer docs. + +## Building + +The build has ordering constraints — OpenAPI client generation +depends on the mgd binary: + +```bash +# 1. Build mgd first +cargo build --bin mgd + +# 2. Generate OpenAPI specs (requires mgd binary) +cargo xtask openapi generate + +# 3. Build mgadm (depends on generated OpenAPI client) +cargo build --bin mgadm +``` + +## Testing + +`cargo nextest` is preferred over `cargo test`. + +On macOS, several crates require illumos. Use nextest with +exclusions: + +```bash +cargo nextest run --workspace \ + --exclude ddm \ + --exclude ddmd \ + --exclude mg-tests \ + --exclude falcon-lab \ + --exclude lab +``` + +Full integration tests require illumos/Helios and run in CI via +Buildomat. Lab tests (`clab`, `interop-lab`, `falcon-lab`) require +their respective environments (containerlab, Docker, libfalcon). + +## Verification + +All work should compile and pass these checks before submitting: + +```bash +# Compilation (all targets including tests/benches) +cargo check --all-targets + +# Clippy with warnings as errors +cargo clippy --all-targets -- --deny warnings + +# Format check +cargo fmt --all --check + +# Verify OpenAPI specs are up to date +cargo xtask openapi check +``` + +## Key Patterns + +### Concurrency Model +- **BGP sessions**: One OS thread per `SessionRunner` (FSM loop). + Channel-based event passing. +- **Admin APIs/metrics**: Tokio async tasks. +- **Shared state**: `Arc>` with helper macros `lock!`, + `read_lock!`, `write_lock!`. +- **Shutdown**: `AtomicBool` flags checked in loops. + +### Generics for Testability +- `Router` — abstract over connection type. +- Production: TCP connections. Tests: channel-based connections. +- Platform traits in mg-lower: `Dpd`, `Ddm`, `SwitchZone`. + +### Logging +- Structured slog with component/module/unit hierarchy. +- Constants: `COMPONENT_BGP`, `MOD_ROUTER`, `UNIT_*`. +- Custom macros: `session_log!`, `collision_log!`, `rdb_log!`, + `mgl_log!`, `dpd_log!`. + +### Error Handling +- `thiserror` for structured error types in libraries. +- `anyhow::Result` in CLI tools. +- `HttpError` conversions for Dropshot API handlers. +- Broken pipe handling in CLI tools (`println_nopipe!` macro). + +## Platform Notes + +- `mg-lower` only compiles/runs on illumos (feature-gated in mgd). +- Platform-specific code guarded by `cfg(target_os = "illumos")`. +- The `lab` and `package` crates are workspace members but not + default members (require illumos). +- BGP sessions use raw TCP (port 179) — not HTTP. The + admin/management API is separate from the protocol. +- RIB database path defaults to `/var/run/rdb` (mgd) and + `/var/run/ddmdb` (ddmd). + +--- + +## API Type Versioning + +The admin API (`mg-api`) is versioned via +`dropshot-api-manager-types::api_versions`. The version list and +API trait live in `mg-api/src/lib.rs`. + +### When You Need to Change an API Type + +When modifying a type that appears in an API endpoint's request or +response, you must preserve backward compatibility for older API +versions. The process: + +1. **Copy the current type to a versioned name.** If the type is + `Foo`, copy it to `FooV{N}` where `{N}` is one less than the + version that introduces the new type. If versioned copies already + exist, use the next available number. For example, if `FooV1` and + `FooV2` exist, the current `Foo` becomes `FooV3`. + +2. **Add `#[schemars(rename = "Foo")]`** to the old copy. This makes + the OpenAPI spec use the original name `Foo` for all versions, so + clients don't see the internal suffix. + +3. **Modify the original `Foo`** with your changes (new fields, + changed types, etc.). It keeps the name `Foo` with no schemars + rename — it IS `Foo` in the latest OpenAPI spec. + +4. **Implement `From for FooV{N}`** so the API layer can + convert the current internal type to the old API type when + serving older API versions. + +5. **Update endpoints** to use version ranges: + - Old endpoint: `versions = ..VERSION_X` uses `FooV{N}` + - New endpoint: `versions = VERSION_X..` uses `Foo` + +6. **Add a comment** on the old type documenting which API versions + it serves and when it can be deleted. + +### Example + +Suppose `VERSION_X` introduces a new field on `Foo`. The old `Foo` +becomes `FooV1`, and the new `Foo` gets the added field: + +```rust +/// Previous version of Foo. +/// Used for API versions before VERSION_X. +/// Delete when VERSION_X is the minimum supported version. +#[derive(/* derives */)] +#[schemars(rename = "Foo")] // <-- OpenAPI still calls it "Foo" +pub struct FooV1 { + pub field_a: String, + pub field_b: u64, +} + +impl From for FooV1 { /* field-by-field conversion */ } + +/// Current Foo (VERSION_X+). +#[derive(/* derives */)] +pub struct Foo { + pub field_a: String, + pub field_b: u64, + pub field_c: Option, // NEW +} +``` + +And in the API endpoint: + +```rust +// Old endpoint serves versions before VERSION_X +#[endpoint { versions = ..VERSION_X }] +async fn get_foo(...) -> FooV1; + +// New endpoint serves VERSION_X onward +#[endpoint { versions = VERSION_X.. }] +async fn get_foo_v2(...) -> Foo; +``` + +For real examples, search for `#[schemars(rename` in the codebase — +`rdb/src/types.rs`, `bgp/src/params.rs`, `bgp/src/session.rs`, and +`mg-api/src/lib.rs` all contain versioned types following this +pattern. + +### Key Rules + +- Versioned types are **never used internally**. The FSM, RIB, and + all internal code use the current `Foo`. Conversion to `FooV{N}` + happens only at the API boundary. +- The `#[schemars(rename)]` is critical — without it, the OpenAPI + spec would expose the internal suffix to clients. +- The `From` impl handles lossy conversions gracefully (e.g., + `PeerId::Interface` maps to `Ipv6Addr::UNSPECIFIED` in V1). + +After any API type change, regenerate and verify the OpenAPI specs: + +```bash +cargo build --bin mgd +cargo xtask openapi generate +cargo xtask openapi check +``` + +--- + +## BGP Test Address Allocation + +BGP tests in `bgp/src/test.rs` use two connection backends: + +- **Channel-based** (`BgpConnectionChannel` / `BgpListenerChannel`) + — in-process channels, no real sockets. Fast, deterministic. +- **TCP-based** (`BgpConnectionTcp` / `BgpListenerTcp`) — real TCP + sockets on loopback. + +Every test must use **unique socket addresses** to avoid conflicts +when nextest runs tests in parallel. Addresses are allocated +sequentially (hex for `3fff::`) from distinct ranges. + +### Address Ranges + +| Range | Purpose | Real sockets? | +|---|---|---| +| `10.0.0.{1..}` | IPv4 channel + some TCP | Channel: no. TCP: yes | +| `127.0.0.{1..}` | IPv4 TCP tests | Yes (loopback) | +| `3fff::{0..}` | IPv6 numbered (channel + TCP) | Channel: no. TCP: yes | +| `fe80::{1,2}` + scope_id | IPv6 unnumbered | No (virtual) | + +All use port `TEST_BGP_PORT` (10179). + +### Adding a New Test + +To find the current highest-allocated addresses, grep for +`sockaddr!` in `bgp/src/test.rs` and pick the next sequential value +in the appropriate range. + +1. **TCP tests must not share addresses with other TCP tests.** Two + TCP tests binding the same IP:port will fail when run in parallel. + Channel tests don't bind real sockets, so channel-only address + reuse is safe (but not recommended for clarity). + +2. **Each test gets a pair** (or triple for 3-router topologies). + Allocate N consecutive addresses for an N-router test. IPv6 + addresses (`3fff::`) are allocated sequentially in hex. + +3. **Unnumbered tests** reuse `fe80::1`/`fe80::2` and call + `next_scope_id()` for isolation. Don't invent new link-local + addresses without reason. + +4. **Route advertisement prefixes** are separate from session + addresses: + - IPv4: `generate_test_prefixes_v4(count)` from `10.0.0.0/24` + - IPv6: `generate_test_prefixes_v6(count)` from `3fff:db8:0::/48` + +### Test Infrastructure + +**Setup**: `test_setup::(name, routers)` is the +standard harness. TCP variants install loopback IPs via +`LoopbackIpManager`; channel variants skip this. + +**Polling macros** (defined in `mg-common/src/test.rs`): +- `wait_for!(expr, msg)` — polls at 1s intervals, up to 30 + iterations. +- `wait_for_eq!(lhs, rhs, msg)` — shorthand for equality polling. +- `wait_for_neq!(lhs, rhs, msg)` — shorthand for inequality + polling. + +**Counter-based synchronization**: For channel tests where FSM state +transitions happen in microseconds, poll +`session.counters.transitions_to_established` instead of +`session.state()` — the state may cycle through Established and back +before the 1-second poll fires. + +**Passive/active pairing**: Set one router `passive: true` and the +other `passive: false` to prevent connection collisions during +re-establishment. Required for tests that reset sessions and assert +on `last_reset.reason` (collisions overwrite the reason). From 2a0cf4d3c603747333f039c23ba873118642b3df Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Tue, 24 Feb 2026 08:18:07 -0700 Subject: [PATCH 20/58] bgp: disallow wildcard match arms in BGP FSM Adds clippy lints to deny wildcard match arms within the BGP FSM. This ensures that any future work will always perform an exhaustive match so there's no chance of an FSM event handler getting missed. The existing match arms were already exhaustive in almost all cases, but this adds an extra level of confidence that it won't change in the future. --- bgp/src/session.rs | 85 +++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/bgp/src/session.rs b/bgp/src/session.rs index fa97ea495..62f1cf195 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -2421,6 +2421,7 @@ impl SessionRunner { /// Initial state. Refuse all incoming BGP connections. No resources /// allocated to peer. + #[deny(clippy::wildcard_enum_match_arm)] fn fsm_idle(&self) -> FsmState { // Clean up connection registry self.cleanup_connections(); @@ -2686,6 +2687,7 @@ impl SessionRunner { /// is important because in "later" FSM states, a ConnectRetryTimerExpires /// event is considered an FSM error that triggers a Notification and an FSM /// transition back to idle. So we need to get it right. + #[deny(clippy::wildcard_enum_match_arm)] fn fsm_connect(&self) -> FsmState { loop { // Check to see if a shutdown has been requested. @@ -2974,7 +2976,10 @@ impl SessionRunner { ) => { conn_timer!(conn, delay_open).stop(); } - _ => {} + ConnectionEvent::Message { .. } + | ConnectionEvent::ParseError { .. } + | ConnectionEvent::TcpConnectionFails(_) => { + } } } @@ -3027,6 +3032,7 @@ impl SessionRunner { /// is important because in "later" FSM states, a ConnectRetryTimerExpires /// event is considered an FSM error that triggers a Notification and an FSM /// transition back to idle. So we need to get it right. + #[deny(clippy::wildcard_enum_match_arm)] fn fsm_active(&self) -> FsmState { loop { // Check to see if a shutdown has been requested. @@ -3403,6 +3409,7 @@ impl SessionRunner { } /// Waiting for open message from peer. + #[deny(clippy::wildcard_enum_match_arm)] fn fsm_open_sent(&self, conn: Arc) -> FsmState { let om = loop { // Check to see if a shutdown has been requested. @@ -3910,31 +3917,28 @@ impl SessionRunner { * - changes its state to Idle. */ if let Err(e) = self.handle_open(&conn, &om) { - match e { - Error::PolicyCheckFailed => { - session_log!( - self, - info, - conn, - "policy check failed"; - "error" => format!("{e}") - ); - } - e => { - session_log!( - self, - warn, - conn, - "failed to handle open message, fsm transition to idle"; - "error" => format!("{e}") - ); - self.counters - .open_handle_failures - .fetch_add(1, Ordering::Relaxed); - // Notification sent by handle_open for all Errors except - // PolicyCheckFailed, which is handled in other match arm. - return FsmState::Idle; - } + if let Error::PolicyCheckFailed = e { + session_log!( + self, + info, + conn, + "policy check failed"; + "error" => format!("{e}") + ); + } else { + session_log!( + self, + warn, + conn, + "failed to handle open message, fsm transition to idle"; + "error" => format!("{e}") + ); + self.counters + .open_handle_failures + .fetch_add(1, Ordering::Relaxed); + // Notification sent by handle_open for all Errors except + // PolicyCheckFailed, which is handled above. + return FsmState::Idle; } } @@ -3988,6 +3992,7 @@ impl SessionRunner { } /// Waiting for keepalive or notification from peer. + #[deny(clippy::wildcard_enum_match_arm)] fn fsm_open_confirm(&self, pc: PeerConnection) -> FsmState { // Check to see if a shutdown has been requested. if self.shutdown.load(Ordering::Acquire) { @@ -4470,6 +4475,7 @@ impl SessionRunner { /// identifying which real FsmState a connection is in currently. FSM Events /// are handled for a connection according to the real FsmState a connection /// is currently in. + #[deny(clippy::wildcard_enum_match_arm)] fn fsm_connection_collision( self: &Arc, conn_pair: CollisionPair, @@ -4522,6 +4528,7 @@ impl SessionRunner { /// state. Whereas the next valid BGP Message that would progress `new` /// gives us the info needed to perform Collision Resolution, which we must /// do once we have the data available to do so. + #[deny(clippy::wildcard_enum_match_arm)] fn connection_collision_open_confirm( self: &Arc, exist: PeerConnection, @@ -6180,6 +6187,7 @@ impl SessionRunner { } /// Sync up with peers. + #[deny(clippy::wildcard_enum_match_arm)] fn fsm_session_setup(&self, pc: PeerConnection) -> FsmState { // Check to see if a shutdown has been requested. if self.shutdown.load(Ordering::Acquire) { @@ -6259,7 +6267,7 @@ impl SessionRunner { .into_iter() .filter_map(|p| match p { Prefix::V4(p4) => Some(p4), - _ => None, + Prefix::V6(_) => None, }) .collect(), )), @@ -6286,7 +6294,7 @@ impl SessionRunner { .into_iter() .filter_map(|p| match p { Prefix::V6(p6) => Some(p6), - _ => None, + Prefix::V4(_) => None, }) .collect(), )), @@ -6327,7 +6335,7 @@ impl SessionRunner { .into_iter() .filter_map(|p| match p { Prefix::V4(p4) => Some(p4), - _ => None, + Prefix::V6(_) => None, }) .collect(); self.send_update( @@ -6350,7 +6358,7 @@ impl SessionRunner { .into_iter() .filter_map(|p| match p { Prefix::V6(p6) => Some(p6), - _ => None, + Prefix::V4(_) => None, }) .collect(); self.send_update( @@ -6364,6 +6372,7 @@ impl SessionRunner { } /// Able to exchange update, notification and keepliave messages with peers. + #[deny(clippy::wildcard_enum_match_arm)] fn fsm_established(&self, pc: PeerConnection) -> FsmState { // Check to see if a shutdown has been requested. if self.shutdown.load(Ordering::Acquire) { @@ -6487,7 +6496,7 @@ impl SessionRunner { Prefix::V4(p4) => { list.contains(p4) } - _ => false, + Prefix::V6(_) => false, }) .collect() } @@ -6511,7 +6520,7 @@ impl SessionRunner { Prefix::V4(p4) => { list.contains(p4) } - _ => false, + Prefix::V6(_) => false, }) .collect() } @@ -6522,7 +6531,7 @@ impl SessionRunner { .difference(&originated_after) .filter_map(|p| match p { Prefix::V4(p4) => Some(*p4), - _ => None, + Prefix::V6(_) => None, }) .collect(); @@ -6530,7 +6539,7 @@ impl SessionRunner { .difference(&originated_before) .filter_map(|p| match p { Prefix::V4(p4) => Some(*p4), - _ => None, + Prefix::V6(_) => None, }) .collect(); @@ -6619,7 +6628,7 @@ impl SessionRunner { Prefix::V6(p6) => { list.contains(p6) } - _ => false, + Prefix::V4(_) => false, }) .collect() } @@ -6643,7 +6652,7 @@ impl SessionRunner { Prefix::V6(p6) => { list.contains(p6) } - _ => false, + Prefix::V4(_) => false, }) .collect() } @@ -6654,7 +6663,7 @@ impl SessionRunner { .difference(&originated_after) .filter_map(|p| match p { Prefix::V6(p6) => Some(*p6), - _ => None, + Prefix::V4(_) => None, }) .collect(); @@ -6662,7 +6671,7 @@ impl SessionRunner { .difference(&originated_before) .filter_map(|p| match p { Prefix::V6(p6) => Some(*p6), - _ => None, + Prefix::V4(_) => None, }) .collect(); From f1497e39b750dae77fbf3306c236e8e30e49a0a2 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Tue, 24 Feb 2026 09:18:42 -0700 Subject: [PATCH 21/58] Enable IP DSCP / IPv6 Traffic Class for BGP peers Adds a configurable DSCP value for IP packets carrying BGP sessions. Fixes: #565 --- bgp/src/connection.rs | 2 + bgp/src/connection_channel.rs | 1 + bgp/src/connection_tcp.rs | 117 ++++++- bgp/src/dispatcher.rs | 16 +- bgp/src/params.rs | 312 +++++++++++++++++- bgp/src/session.rs | 14 +- bgp/src/test.rs | 4 +- falcon-lab/src/bgp.rs | 2 + falcon-lab/src/test.rs | 4 +- mg-admin-client/src/lib.rs | 1 + mg-api/src/lib.rs | 152 +++++++-- mgadm/src/bgp.rs | 30 +- mgd/src/admin.rs | 107 +++++- mgd/src/bgp_admin.rs | 286 ++++++++++++---- mgd/src/main.rs | 2 + ...26a5e9.json => mg-admin-7.0.0-2e3fe3.json} | 65 +++- openapi/mg-admin/mg-admin-latest.json | 2 +- rdb-types/src/lib.rs | 103 ++++++ rdb/src/proptest.rs | 3 +- rdb/src/types.rs | 6 +- 20 files changed, 1080 insertions(+), 149 deletions(-) rename openapi/mg-admin/{mg-admin-7.0.0-26a5e9.json => mg-admin-7.0.0-2e3fe3.json} (98%) diff --git a/bgp/src/connection.rs b/bgp/src/connection.rs index 3d1cd8842..2eea59de9 100644 --- a/bgp/src/connection.rs +++ b/bgp/src/connection.rs @@ -9,6 +9,7 @@ use crate::{ session::{FsmEvent, PeerId, SessionEndpoint, SessionInfo}, unnumbered::UnnumberedManager, }; +use rdb::Dscp; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use slog::Logger; @@ -147,6 +148,7 @@ pub trait BgpListener { conn: &Cnx, min_ttl: Option, md5_key: Option, + dscp: Dscp, ) -> Result<(), Error>; } diff --git a/bgp/src/connection_channel.rs b/bgp/src/connection_channel.rs index f7dc0814c..d4d26401b 100644 --- a/bgp/src/connection_channel.rs +++ b/bgp/src/connection_channel.rs @@ -279,6 +279,7 @@ impl BgpListener for BgpListenerChannel { _conn: &BgpConnectionChannel, _min_ttl: Option, _md5_key: Option, + _dscp: rdb::Dscp, ) -> Result<(), Error> { // Policy application is ignored for test connections Ok(()) diff --git a/bgp/src/connection_tcp.rs b/bgp/src/connection_tcp.rs index a1c1c741c..a3939a3b0 100644 --- a/bgp/src/connection_tcp.rs +++ b/bgp/src/connection_tcp.rs @@ -25,13 +25,16 @@ use crate::{ }, unnumbered::UnnumberedManager, }; +use libc::{IPPROTO_IP, IPPROTO_IPV6, c_void}; use mg_common::lock; +use rdb::Dscp; use slog::Logger; use std::{ collections::BTreeMap, io::Read, io::Write, net::{SocketAddr, TcpListener, TcpStream, ToSocketAddrs}, + os::fd::AsRawFd, sync::atomic::AtomicBool, sync::{Arc, Mutex, atomic::Ordering, mpsc::Sender}, thread::{JoinHandle, sleep}, @@ -39,10 +42,7 @@ use std::{ }; #[cfg(any(target_os = "linux", target_os = "illumos"))] -use { - libc::{IPPROTO_IP, IPPROTO_IPV6, IPPROTO_TCP, c_int, c_void}, - std::os::fd::AsRawFd, -}; +use libc::{IPPROTO_TCP, c_int}; #[cfg(target_os = "linux")] use crate::connection::MAX_MD5SIG_KEYLEN; @@ -187,6 +187,7 @@ impl BgpListener for BgpListenerTcp { conn: &BgpConnectionTcp, min_ttl: Option, md5_key: Option, + dscp: Dscp, ) -> Result<(), Error> { let tcp_stream = lock!(conn.conn); @@ -194,6 +195,8 @@ impl BgpListener for BgpListenerTcp { apply_min_ttl(&tcp_stream, ttl, conn.peer)?; } + apply_dscp(&tcp_stream, dscp, conn.peer)?; + if let Some(ref key) = md5_key { #[cfg(target_os = "linux")] { @@ -342,6 +345,17 @@ impl BgpConnector for BgpConnectorTcp { return; } + if let Err(e) = apply_dscp(&new_conn, config.dscp, peer) { + connection_log_lite!(log, + warn, + "failed to apply DSCP for {peer}: {e}"; + "direction" => ConnectionDirection::Outbound, + "peer" => format!("{peer}"), + "error" => format!("{e}") + ); + return; + } + // Determine the actual source address let actual_source = match new_conn.local_addr() { Ok(addr) => addr, @@ -1214,6 +1228,44 @@ fn create_outbound_socket( }) } +/// Apply DSCP marking to a BGP TCP connection. +/// +/// Sets the DSCP field in the IP header via IP_TOS (IPv4) or +/// IPV6_TCLASS (IPv6). +fn apply_dscp( + conn: &TcpStream, + dscp: Dscp, + peer: SocketAddr, +) -> Result<(), Error> { + let tos = u32::from(dscp.tos_byte()); + let fd = conn.as_raw_fd(); + unsafe { + if peer.is_ipv4() + && libc::setsockopt( + fd, + IPPROTO_IP, + libc::IP_TOS, + &tos as *const u32 as *const c_void, + std::mem::size_of::() as u32, + ) != 0 + { + return Err(Error::Io(std::io::Error::last_os_error())); + } + if peer.is_ipv6() + && libc::setsockopt( + fd, + IPPROTO_IPV6, + libc::IPV6_TCLASS, + &tos as *const u32 as *const c_void, + std::mem::size_of::() as u32, + ) != 0 + { + return Err(Error::Io(std::io::Error::last_os_error())); + } + } + Ok(()) +} + /// Apply min TTL setting to a TCP connection #[allow(unused_variables)] fn apply_min_ttl( @@ -1506,3 +1558,60 @@ fn setup_outbound_md5( Ok((key.to_string(), local)) } + +#[cfg(test)] +mod tests { + use super::*; + use rdb::Dscp; + use std::net::{TcpListener, TcpStream}; + + #[test] + fn apply_dscp_sets_ip_tos() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap(); + let stream = TcpStream::connect(addr).unwrap(); + + let dscp = Dscp::new(48).unwrap(); + apply_dscp(&stream, dscp, addr).unwrap(); + + let mut readback: u32 = 0; + let mut len = std::mem::size_of::() as libc::socklen_t; + let rc = unsafe { + libc::getsockopt( + stream.as_raw_fd(), + IPPROTO_IP, + libc::IP_TOS, + &mut readback as *mut u32 as *mut c_void, + &mut len, + ) + }; + assert_eq!(rc, 0, "getsockopt failed"); + // DSCP 48 → TOS byte = 48 << 2 = 192 + assert_eq!(readback, u32::from(dscp.tos_byte())); + } + + #[test] + fn apply_dscp_sets_ipv6_tclass() { + let listener = TcpListener::bind("[::1]:0").unwrap(); + let addr = listener.local_addr().unwrap(); + let stream = TcpStream::connect(addr).unwrap(); + + let dscp = Dscp::new(46).unwrap(); // EF + apply_dscp(&stream, dscp, addr).unwrap(); + + let mut readback: u32 = 0; + let mut len = std::mem::size_of::() as libc::socklen_t; + let rc = unsafe { + libc::getsockopt( + stream.as_raw_fd(), + IPPROTO_IPV6, + libc::IPV6_TCLASS, + &mut readback as *mut u32 as *mut c_void, + &mut len, + ) + }; + assert_eq!(rc, 0, "getsockopt failed"); + // DSCP 46 (EF) → TOS byte = 46 << 2 = 184 + assert_eq!(readback, u32::from(dscp.tos_byte())); + } +} diff --git a/bgp/src/dispatcher.rs b/bgp/src/dispatcher.rs index 0e5f8e5a4..261f16298 100644 --- a/bgp/src/dispatcher.rs +++ b/bgp/src/dispatcher.rs @@ -170,13 +170,15 @@ impl Dispatcher { match lock!(self.peer_to_session).get(&key).cloned() { Some(session_endpoint) => { // Apply connection policy from the session configuration - let min_ttl = lock!(session_endpoint.config).min_ttl; - let md5_key = - lock!(session_endpoint.config).md5_auth_key.clone(); - - if let Err(e) = - Listener::apply_policy(&accepted, min_ttl, md5_key) - { + let config = lock!(session_endpoint.config); + let min_ttl = config.min_ttl; + let md5_key = config.md5_auth_key.clone(); + let dscp = config.dscp; + drop(config); + + if let Err(e) = Listener::apply_policy( + &accepted, min_ttl, md5_key, dscp, + ) { dispatcher_log!(self, warn, "failed to apply policy for connection from {}: {e}", peer_addr; diff --git a/bgp/src/params.rs b/bgp/src/params.rs index 462f813cf..5f9725c28 100644 --- a/bgp/src/params.rs +++ b/bgp/src/params.rs @@ -8,7 +8,7 @@ use crate::{ session::{FsmStateKind, SessionCounters, SessionInfo}, }; use rdb::{ - ImportExportPolicy4, ImportExportPolicy6, ImportExportPolicyV1, + Dscp, ImportExportPolicy4, ImportExportPolicy6, ImportExportPolicyV1, PolicyAction, Prefix, Prefix4, Prefix6, }; use schemars::JsonSchema; @@ -398,6 +398,7 @@ impl UnnumberedNeighbor { min: 0.75, max: 1.0, }), + dscp: rq.parameters.dscp, }, } } @@ -464,6 +465,7 @@ impl Neighbor { }), idle_hold_jitter: None, deterministic_collision_resolution: false, + dscp: rq.parameters.dscp, }, } } @@ -1275,6 +1277,11 @@ pub struct BgpPeerParameters { /// is multiplied by a random value within the (min, max) range supplied. /// Useful to help break repeated synchronization of connection collisions. pub connect_retry_jitter: Option, + /// DSCP value for BGP TCP connections (0-63). + /// RFC 4271 Appendix E recommends CS6 (48) for BGP traffic. + /// Default: CS6 (48). + #[serde(default)] + pub dscp: Dscp, } impl BgpPeerParameters { @@ -1344,6 +1351,7 @@ impl From for BgpPeerConfig { }), idle_hold_jitter: None, deterministic_collision_resolution: false, + dscp: Dscp::default(), }, } } @@ -1359,6 +1367,308 @@ pub enum PolicyKind { Shaper, } +// ============================================================================ +// API Compatibility Types (VERSION_MP_BGP..VERSION_EXTENDED_NH_STATIC) +// ============================================================================ +// These types represent the v3-v6 API format (per-AF config, no DSCP). +// Used for API versions between VERSION_MP_BGP and +// VERSION_EXTENDED_NH_STATIC. +// Delete when VERSION_EXTENDED_NH_STATIC is the minimum supported version. + +/// BGP peer parameters for v3-v6 API (per-AF config, no DSCP). +#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone, PartialEq)] +#[schemars(rename = "BgpPeerParameters")] +pub struct BgpPeerParametersV2 { + pub hold_time: u64, + pub idle_hold_time: u64, + pub delay_open: u64, + pub connect_retry: u64, + pub keepalive: u64, + pub resolution: u64, + pub passive: bool, + pub remote_asn: Option, + pub min_ttl: Option, + pub md5_auth_key: Option, + pub multi_exit_discriminator: Option, + pub communities: Vec, + pub local_pref: Option, + pub enforce_first_as: bool, + pub vlan_id: Option, + pub ipv4_unicast: Option, + pub ipv6_unicast: Option, + pub deterministic_collision_resolution: bool, + pub idle_hold_jitter: Option, + pub connect_retry_jitter: Option, +} + +impl From for BgpPeerParametersV2 { + fn from(p: BgpPeerParameters) -> Self { + Self { + hold_time: p.hold_time, + idle_hold_time: p.idle_hold_time, + delay_open: p.delay_open, + connect_retry: p.connect_retry, + keepalive: p.keepalive, + resolution: p.resolution, + passive: p.passive, + remote_asn: p.remote_asn, + min_ttl: p.min_ttl, + md5_auth_key: p.md5_auth_key, + multi_exit_discriminator: p.multi_exit_discriminator, + communities: p.communities, + local_pref: p.local_pref, + enforce_first_as: p.enforce_first_as, + vlan_id: p.vlan_id, + ipv4_unicast: p.ipv4_unicast, + ipv6_unicast: p.ipv6_unicast, + deterministic_collision_resolution: p + .deterministic_collision_resolution, + idle_hold_jitter: p.idle_hold_jitter, + connect_retry_jitter: p.connect_retry_jitter, + } + } +} + +impl From for BgpPeerParameters { + fn from(p: BgpPeerParametersV2) -> Self { + Self { + hold_time: p.hold_time, + idle_hold_time: p.idle_hold_time, + delay_open: p.delay_open, + connect_retry: p.connect_retry, + keepalive: p.keepalive, + resolution: p.resolution, + passive: p.passive, + remote_asn: p.remote_asn, + min_ttl: p.min_ttl, + md5_auth_key: p.md5_auth_key, + multi_exit_discriminator: p.multi_exit_discriminator, + communities: p.communities, + local_pref: p.local_pref, + enforce_first_as: p.enforce_first_as, + vlan_id: p.vlan_id, + ipv4_unicast: p.ipv4_unicast, + ipv6_unicast: p.ipv6_unicast, + deterministic_collision_resolution: p + .deterministic_collision_resolution, + idle_hold_jitter: p.idle_hold_jitter, + connect_retry_jitter: p.connect_retry_jitter, + dscp: Dscp::default(), + } + } +} + +/// BGP peer config for v3-v6 API (no DSCP). +#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone, PartialEq)] +#[schemars(rename = "BgpPeerConfig")] +pub struct BgpPeerConfigV2 { + pub host: SocketAddr, + pub name: String, + #[serde(flatten)] + pub parameters: BgpPeerParametersV2, +} + +impl From for BgpPeerConfigV2 { + fn from(c: BgpPeerConfig) -> Self { + Self { + host: c.host, + name: c.name, + parameters: c.parameters.into(), + } + } +} + +impl From for BgpPeerConfig { + fn from(c: BgpPeerConfigV2) -> Self { + Self { + host: c.host, + name: c.name, + parameters: c.parameters.into(), + } + } +} + +/// Neighbor configuration for v3-v6 API (no DSCP). +#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone, PartialEq)] +#[schemars(rename = "Neighbor")] +pub struct NeighborV2 { + pub asn: u32, + pub name: String, + pub group: String, + pub host: SocketAddr, + #[serde(flatten)] + pub parameters: BgpPeerParametersV2, +} + +impl From for NeighborV2 { + fn from(n: Neighbor) -> Self { + Self { + asn: n.asn, + name: n.name, + group: n.group, + host: n.host, + parameters: n.parameters.into(), + } + } +} + +impl From for Neighbor { + fn from(n: NeighborV2) -> Self { + Self { + asn: n.asn, + name: n.name, + group: n.group, + host: n.host, + parameters: n.parameters.into(), + } + } +} + +/// Unnumbered BGP peer config for v5-v6 API (no DSCP). +#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone, PartialEq)] +#[schemars(rename = "UnnumberedBgpPeerConfig")] +pub struct UnnumberedBgpPeerConfigV1 { + pub interface: String, + pub name: String, + pub router_lifetime: u16, + #[serde(flatten)] + pub parameters: BgpPeerParametersV2, +} + +impl From for UnnumberedBgpPeerConfigV1 { + fn from(c: UnnumberedBgpPeerConfig) -> Self { + Self { + interface: c.interface, + name: c.name, + router_lifetime: c.router_lifetime, + parameters: c.parameters.into(), + } + } +} + +impl From for UnnumberedBgpPeerConfig { + fn from(c: UnnumberedBgpPeerConfigV1) -> Self { + Self { + interface: c.interface, + name: c.name, + router_lifetime: c.router_lifetime, + parameters: c.parameters.into(), + } + } +} + +/// Unnumbered neighbor configuration for v5-v6 API (no DSCP). +#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone, PartialEq)] +#[schemars(rename = "UnnumberedNeighbor")] +pub struct UnnumberedNeighborV1 { + pub asn: u32, + pub name: String, + pub group: String, + pub interface: String, + pub act_as_a_default_ipv6_router: u16, + #[serde(flatten)] + pub parameters: BgpPeerParametersV2, +} + +impl From for UnnumberedNeighborV1 { + fn from(n: UnnumberedNeighbor) -> Self { + Self { + asn: n.asn, + name: n.name, + group: n.group, + interface: n.interface, + act_as_a_default_ipv6_router: n.act_as_a_default_ipv6_router, + parameters: n.parameters.into(), + } + } +} + +impl From for UnnumberedNeighbor { + fn from(n: UnnumberedNeighborV1) -> Self { + Self { + asn: n.asn, + name: n.name, + group: n.group, + interface: n.interface, + act_as_a_default_ipv6_router: n.act_as_a_default_ipv6_router, + parameters: n.parameters.into(), + } + } +} + +/// Apply request for v3-v6 API (no DSCP). +#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)] +#[schemars(rename = "ApplyRequest")] +pub struct ApplyRequestV2 { + pub asn: u32, + pub originate: Vec, + pub checker: Option, + pub shaper: Option, + pub peers: HashMap>, + #[serde(default)] + pub unnumbered_peers: HashMap>, +} + +impl From for ApplyRequestV2 { + fn from(r: ApplyRequest) -> Self { + Self { + asn: r.asn, + originate: r.originate, + checker: r.checker, + shaper: r.shaper, + peers: r + .peers + .into_iter() + .map(|(k, v)| { + (k, v.into_iter().map(BgpPeerConfigV2::from).collect()) + }) + .collect(), + unnumbered_peers: r + .unnumbered_peers + .into_iter() + .map(|(k, v)| { + ( + k, + v.into_iter() + .map(UnnumberedBgpPeerConfigV1::from) + .collect(), + ) + }) + .collect(), + } + } +} + +impl From for ApplyRequest { + fn from(r: ApplyRequestV2) -> Self { + Self { + asn: r.asn, + originate: r.originate, + checker: r.checker, + shaper: r.shaper, + peers: r + .peers + .into_iter() + .map(|(k, v)| { + (k, v.into_iter().map(BgpPeerConfig::from).collect()) + }) + .collect(), + unnumbered_peers: r + .unnumbered_peers + .into_iter() + .map(|(k, v)| { + ( + k, + v.into_iter() + .map(UnnumberedBgpPeerConfig::from) + .collect(), + ) + }) + .collect(), + } + } +} + // ============================================================================ // API Compatibility Types (VERSION_INITIAL / v1.0.0) // ============================================================================ diff --git a/bgp/src/session.rs b/bgp/src/session.rs index 62f1cf195..16540de7c 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -35,7 +35,7 @@ use crate::{ }; use mg_common::{lock, read_lock, write_lock}; use rdb::{ - AddressFamily, Asn, BgpPathProperties, Db, ImportExportPolicy, + AddressFamily, Asn, BgpPathProperties, Db, Dscp, ImportExportPolicy, ImportExportPolicy4, ImportExportPolicy6, Prefix, Prefix4, Prefix6, }; pub use rdb::{DEFAULT_RIB_PRIORITY_BGP, DEFAULT_ROUTE_PRIORITY, PeerId}; @@ -911,6 +911,10 @@ pub struct SessionInfo { /// resolution even when one connection is already in Established state. /// When false, Established connection always wins (timing-based resolution). pub deterministic_collision_resolution: bool, + /// DSCP value for BGP TCP connections (0-63). + /// RFC 4271 Appendix E recommends CS6 (48) for BGP traffic. + /// Default: CS6 (48). + pub dscp: Dscp, } impl SessionInfo { @@ -947,6 +951,7 @@ impl SessionInfo { }), connect_retry_jitter: None, deterministic_collision_resolution: false, + dscp: Dscp::default(), } } } @@ -977,6 +982,7 @@ impl From<&BgpPeerParameters> for SessionInfo { .deterministic_collision_resolution, ipv4_unicast: value.ipv4_unicast.clone(), ipv6_unicast: value.ipv6_unicast.clone(), + dscp: value.dscp, } } } @@ -1013,6 +1019,7 @@ impl From<&BgpPeerParametersV1> for SessionInfo { export_policy: value.allow_export.as_ipv4_policy().clone(), }), ipv6_unicast: None, + dscp: Dscp::default(), } } } @@ -9514,6 +9521,11 @@ impl SessionRunner { reset_needed = true; } + if current.dscp != info.dscp { + current.dscp = info.dscp; + reset_needed = true; + } + // Update jitter settings (no session reset required) if current.connect_retry_jitter != info.connect_retry_jitter { current.connect_retry_jitter = info.connect_retry_jitter; diff --git a/bgp/src/test.rs b/bgp/src/test.rs index 1db659130..470d01b28 100644 --- a/bgp/src/test.rs +++ b/bgp/src/test.rs @@ -22,7 +22,7 @@ use mg_common::log::init_file_logger; use mg_common::test::{IpAllocation, LoopbackIpManager}; use mg_common::*; use rdb::{ - AddressFamily, Asn, ImportExportPolicy4, ImportExportPolicy6, Prefix, + AddressFamily, Asn, Dscp, ImportExportPolicy4, ImportExportPolicy6, Prefix, Prefix4, Prefix6, }; use std::{ @@ -306,6 +306,7 @@ fn create_test_session_info( max: 1.0, }), deterministic_collision_resolution: false, + dscp: Dscp::default(), } } @@ -2961,6 +2962,7 @@ fn create_unnumbered_session_info( connect_retry_jitter: None, idle_hold_jitter: None, deterministic_collision_resolution: false, + dscp: Dscp::default(), } } diff --git a/falcon-lab/src/bgp.rs b/falcon-lab/src/bgp.rs index aa3bf451a..d71841f40 100644 --- a/falcon-lab/src/bgp.rs +++ b/falcon-lab/src/bgp.rs @@ -4,6 +4,7 @@ use mg_admin_client::types::{ ImportExportPolicy4, ImportExportPolicy6, Ipv4UnicastConfig, Ipv6UnicastConfig, UnnumberedNeighbor, }; +use rdb_types::Dscp; pub fn basic_unnumbered_neighbor( name: &str, @@ -46,5 +47,6 @@ pub fn basic_unnumbered_neighbor( connect_retry_jitter: None, deterministic_collision_resolution: false, idle_hold_jitter: None, + dscp: Dscp::default(), } } diff --git a/falcon-lab/src/test.rs b/falcon-lab/src/test.rs index 5365e9497..161f9cd39 100644 --- a/falcon-lab/src/test.rs +++ b/falcon-lab/src/test.rs @@ -104,7 +104,7 @@ pub async fn run_trio_unnumbered_test( .await .context("mgd: create router")?; - mgd.create_unnumbered_neighbor(&basic_unnumbered_neighbor( + mgd.create_unnumbered_neighbor_v2(&basic_unnumbered_neighbor( "cr1", "test", "tfportqsfp0_0", @@ -114,7 +114,7 @@ pub async fn run_trio_unnumbered_test( .await .context("mgd: create cr1 unnumbered neighbor")?; - mgd.create_unnumbered_neighbor(&basic_unnumbered_neighbor( + mgd.create_unnumbered_neighbor_v2(&basic_unnumbered_neighbor( "cr2", "test", "tfportqsfp1_0", diff --git a/mg-admin-client/src/lib.rs b/mg-admin-client/src/lib.rs index 62ceb6355..7a667e2ef 100644 --- a/mg-admin-client/src/lib.rs +++ b/mg-admin-client/src/lib.rs @@ -23,6 +23,7 @@ progenitor::generate_api!( AddressFamily = rdb_types::AddressFamily, ProtocolFilter = rdb_types::ProtocolFilter, PeerId = rdb_types::PeerId, + Dscp = rdb_types::Dscp, Duration = std::time::Duration, } ); diff --git a/mg-api/src/lib.rs b/mg-api/src/lib.rs index a07a70de4..59d440db7 100644 --- a/mg-api/src/lib.rs +++ b/mg-api/src/lib.rs @@ -13,9 +13,10 @@ pub use bgp::session::MessageDirection; use bgp::{ messages::Afi, params::{ - ApplyRequest, ApplyRequestV1, CheckerSource, Neighbor, NeighborResetOp, - NeighborResetOpV1, NeighborV1, Origin4, Origin6, PeerInfo, PeerInfoV1, - PeerInfoV2, PeerInfoV3, Router, ShaperSource, UnnumberedNeighbor, + ApplyRequest, ApplyRequestV1, ApplyRequestV2, CheckerSource, Neighbor, + NeighborResetOp, NeighborResetOpV1, NeighborV1, NeighborV2, Origin4, + Origin6, PeerInfo, PeerInfoV1, PeerInfoV2, PeerInfoV3, Router, + ShaperSource, UnnumberedNeighbor, UnnumberedNeighborV1, }, session::{ FsmEventRecord, MessageHistoryEntry, MessageHistoryEntryV2, @@ -158,29 +159,29 @@ pub trait MgAdminApi { request: Query, ) -> Result; - // V3 API - new Neighbor type with explicit per-AF configuration (numbered peers only) + // V3 API - per-AF configuration, no DSCP (numbered peers only) #[endpoint { method = PUT, path = "/bgp/config/neighbor", versions = VERSION_MP_BGP..VERSION_UNNUMBERED }] async fn create_neighbor_v2( rqctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result; #[endpoint { method = GET, path = "/bgp/config/neighbor", versions = VERSION_MP_BGP..VERSION_UNNUMBERED }] async fn read_neighbor_v2( rqctx: RequestContext, request: Query, - ) -> Result, HttpError>; + ) -> Result, HttpError>; #[endpoint { method = GET, path = "/bgp/config/neighbors", versions = VERSION_MP_BGP..VERSION_UNNUMBERED }] async fn read_neighbors_v2( rqctx: RequestContext, request: Query, - ) -> Result>, HttpError>; + ) -> Result>, HttpError>; #[endpoint { method = POST, path = "/bgp/config/neighbor", versions = VERSION_MP_BGP..VERSION_UNNUMBERED }] async fn update_neighbor_v2( rqctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result; #[endpoint { method = DELETE, path = "/bgp/config/neighbor", versions = VERSION_MP_BGP..VERSION_UNNUMBERED }] @@ -189,38 +190,69 @@ pub trait MgAdminApi { request: Query, ) -> Result; - // Unified API (VERSION_UNNUMBERED..) - supports both numbered and unnumbered neighbors - // Uses PeerId in path parameters with FromStr for type-safe parsing - #[endpoint { method = PUT, path = "/bgp/config/neighbor", versions = VERSION_UNNUMBERED.. }] + // Unified API (VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC) + // Per-AF config, no DSCP. PeerId in path parameters. + #[endpoint { method = PUT, path = "/bgp/config/neighbor", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] async fn create_neighbor_v3( rqctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result; - #[endpoint { method = GET, path = "/bgp/config/neighbor/{asn}/{peer}", versions = VERSION_UNNUMBERED.. }] + #[endpoint { method = GET, path = "/bgp/config/neighbor/{asn}/{peer}", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] async fn read_neighbor_v3( rqctx: RequestContext, path: Path, - ) -> Result, HttpError>; + ) -> Result, HttpError>; - #[endpoint { method = GET, path = "/bgp/config/neighbors/{asn}", versions = VERSION_UNNUMBERED.. }] + #[endpoint { method = GET, path = "/bgp/config/neighbors/{asn}", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] async fn read_neighbors_v3( rqctx: RequestContext, path: Path, - ) -> Result>, HttpError>; + ) -> Result>, HttpError>; - #[endpoint { method = POST, path = "/bgp/config/neighbor", versions = VERSION_UNNUMBERED.. }] + #[endpoint { method = POST, path = "/bgp/config/neighbor", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] async fn update_neighbor_v3( rqctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result; - #[endpoint { method = DELETE, path = "/bgp/config/neighbor/{asn}/{peer}", versions = VERSION_UNNUMBERED.. }] + #[endpoint { method = DELETE, path = "/bgp/config/neighbor/{asn}/{peer}", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] async fn delete_neighbor_v3( rqctx: RequestContext, path: Path, ) -> Result; + // Unified API (VERSION_EXTENDED_NH_STATIC..) - with DSCP support + #[endpoint { method = PUT, path = "/bgp/config/neighbor", versions = VERSION_EXTENDED_NH_STATIC.. }] + async fn create_neighbor_v4( + rqctx: RequestContext, + request: TypedBody, + ) -> Result; + + #[endpoint { method = GET, path = "/bgp/config/neighbor/{asn}/{peer}", versions = VERSION_EXTENDED_NH_STATIC.. }] + async fn read_neighbor_v4( + rqctx: RequestContext, + path: Path, + ) -> Result, HttpError>; + + #[endpoint { method = GET, path = "/bgp/config/neighbors/{asn}", versions = VERSION_EXTENDED_NH_STATIC.. }] + async fn read_neighbors_v4( + rqctx: RequestContext, + path: Path, + ) -> Result>, HttpError>; + + #[endpoint { method = POST, path = "/bgp/config/neighbor", versions = VERSION_EXTENDED_NH_STATIC.. }] + async fn update_neighbor_v4( + rqctx: RequestContext, + request: TypedBody, + ) -> Result; + + #[endpoint { method = DELETE, path = "/bgp/config/neighbor/{asn}/{peer}", versions = VERSION_EXTENDED_NH_STATIC.. }] + async fn delete_neighbor_v4( + rqctx: RequestContext, + path: Path, + ) -> Result; + // V1/V2 API clear neighbor (backwards compatibility w/ IPv4 only support) #[endpoint { method = POST, path = "/bgp/clear/neighbor", versions = ..VERSION_MP_BGP }] async fn clear_neighbor( @@ -237,56 +269,109 @@ pub trait MgAdminApi { // Unnumbered neighbors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Unnumbered neighbors (VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC) + // No DSCP support. #[endpoint { method = GET, path = "/bgp/config/unnumbered-neighbors", - versions = VERSION_UNNUMBERED.., + versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC, }] async fn read_unnumbered_neighbors( rqctx: RequestContext, request: Query, - ) -> Result>, HttpError>; + ) -> Result>, HttpError>; #[endpoint { method = PUT, path = "/bgp/config/unnumbered-neighbor", - versions = VERSION_UNNUMBERED.., + versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC, }] async fn create_unnumbered_neighbor( rqctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result; #[endpoint { method = GET, path = "/bgp/config/unnumbered-neighbor", - versions = VERSION_UNNUMBERED.., + versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC, }] async fn read_unnumbered_neighbor( rqctx: RequestContext, request: Query, - ) -> Result, HttpError>; + ) -> Result, HttpError>; #[endpoint { method = POST, path = "/bgp/config/unnumbered-neighbor", - versions = VERSION_UNNUMBERED.., + versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC, }] async fn update_unnumbered_neighbor( rqctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result; #[endpoint { method = DELETE, path = "/bgp/config/unnumbered-neighbor", - versions = VERSION_UNNUMBERED.., + versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC, }] async fn delete_unnumbered_neighbor( rqctx: RequestContext, request: Query, ) -> Result; + // Unnumbered neighbors (VERSION_EXTENDED_NH_STATIC..) - with DSCP + #[endpoint { + method = GET, + path = "/bgp/config/unnumbered-neighbors", + versions = VERSION_EXTENDED_NH_STATIC.., + }] + async fn read_unnumbered_neighbors_v2( + rqctx: RequestContext, + request: Query, + ) -> Result>, HttpError>; + + #[endpoint { + method = PUT, + path = "/bgp/config/unnumbered-neighbor", + versions = VERSION_EXTENDED_NH_STATIC.., + }] + async fn create_unnumbered_neighbor_v2( + rqctx: RequestContext, + request: TypedBody, + ) -> Result; + + #[endpoint { + method = GET, + path = "/bgp/config/unnumbered-neighbor", + versions = VERSION_EXTENDED_NH_STATIC.., + }] + async fn read_unnumbered_neighbor_v2( + rqctx: RequestContext, + request: Query, + ) -> Result, HttpError>; + + #[endpoint { + method = POST, + path = "/bgp/config/unnumbered-neighbor", + versions = VERSION_EXTENDED_NH_STATIC.., + }] + async fn update_unnumbered_neighbor_v2( + rqctx: RequestContext, + request: TypedBody, + ) -> Result; + + #[endpoint { + method = DELETE, + path = "/bgp/config/unnumbered-neighbor", + versions = VERSION_EXTENDED_NH_STATIC.., + }] + async fn delete_unnumbered_neighbor_v2( + rqctx: RequestContext, + request: Query, + ) -> Result; + #[endpoint { method = POST, path = "/bgp/clear/unnumbered-neighbor", @@ -448,9 +533,16 @@ pub trait MgAdminApi { request: TypedBody, ) -> Result; - // V3 API - ApplyRequest with per-AF policies - #[endpoint { method = POST, path = "/bgp/omicron/apply", versions = VERSION_MP_BGP.. }] + // V3 API - ApplyRequest with per-AF policies (no DSCP) + #[endpoint { method = POST, path = "/bgp/omicron/apply", versions = VERSION_MP_BGP..VERSION_EXTENDED_NH_STATIC }] async fn bgp_apply_v2( + rqctx: RequestContext, + request: TypedBody, + ) -> Result; + + // V4 API - ApplyRequest with DSCP support + #[endpoint { method = POST, path = "/bgp/omicron/apply", versions = VERSION_EXTENDED_NH_STATIC.. }] + async fn bgp_apply_v3( rqctx: RequestContext, request: TypedBody, ) -> Result; diff --git a/mgadm/src/bgp.rs b/mgadm/src/bgp.rs index c2d6511c7..fc1bfa495 100644 --- a/mgadm/src/bgp.rs +++ b/mgadm/src/bgp.rs @@ -14,6 +14,7 @@ use mg_admin_client::{ }, }; use mg_common::{print_nopipe, println_nopipe}; +use rdb::Dscp; use rdb::types::{PeerId, Prefix4, Prefix6}; use std::{ fs::read_to_string, @@ -695,6 +696,11 @@ pub struct Neighbor { /// IPv6 nexthop override for this neighbor (requires --enable-ipv6). #[arg(long, requires = "enable_ipv6")] pub nexthop6: Option, + + /// DSCP value for BGP TCP connections (0-63). + /// RFC 4271 recommends CS6 (48). Default: 48. + #[arg(long)] + pub dscp: Option, } /// Peer type determined by parsing the peer string @@ -805,6 +811,7 @@ impl Neighbor { idle_hold_jitter: self.idle_hold_jitter.map(jitter_range_to_api), deterministic_collision_resolution: self .deterministic_collision_resolution, + dscp: self.dscp.unwrap_or_default(), } } @@ -888,6 +895,7 @@ impl Neighbor { idle_hold_jitter: self.idle_hold_jitter.map(jitter_range_to_api), deterministic_collision_resolution: self .deterministic_collision_resolution, + dscp: self.dscp.unwrap_or_default(), } } } @@ -1381,8 +1389,8 @@ async fn get_exported( async fn list_nbr(asn: u32, c: Client) -> Result<()> { // Get both numbered and unnumbered neighbors - let numbered = c.read_neighbors_v3(asn).await?.into_inner(); - let unnumbered = c.read_unnumbered_neighbors(asn).await?.into_inner(); + let numbered = c.read_neighbors_v4(asn).await?.into_inner(); + let unnumbered = c.read_unnumbered_neighbors_v2(asn).await?.into_inner(); if numbered.is_empty() && unnumbered.is_empty() { println_nopipe!("No neighbors configured for ASN {}", asn); @@ -1425,10 +1433,10 @@ async fn list_nbr(asn: u32, c: Client) -> Result<()> { async fn create_nbr(nbr: Neighbor, c: Client) -> Result<()> { match nbr.into_api_types()? { ApiNeighborType::Numbered(n) => { - c.create_neighbor_v3(&n).await?; + c.create_neighbor_v4(&n).await?; } ApiNeighborType::Unnumbered(n) => { - c.create_unnumbered_neighbor(&n).await?; + c.create_unnumbered_neighbor_v2(&n).await?; } } Ok(()) @@ -1438,14 +1446,14 @@ async fn read_nbr(asn: u32, peer: String, c: Client) -> Result<()> { match parse_peer_type(&peer) { PeerType::Numbered(addr) => { let nbr = c - .read_neighbor_v3(asn, &addr.to_string()) + .read_neighbor_v4(asn, &addr.to_string()) .await? .into_inner(); println_nopipe!("{nbr:#?}"); } PeerType::Unnumbered(interface) => { let nbr = c - .read_unnumbered_neighbor(asn, &interface) + .read_unnumbered_neighbor_v2(asn, &interface) .await? .into_inner(); println_nopipe!("{nbr:#?}"); @@ -1457,10 +1465,10 @@ async fn read_nbr(asn: u32, peer: String, c: Client) -> Result<()> { async fn update_nbr(nbr: Neighbor, c: Client) -> Result<()> { match nbr.into_api_types()? { ApiNeighborType::Numbered(n) => { - c.update_neighbor_v3(&n).await?; + c.update_neighbor_v4(&n).await?; } ApiNeighborType::Unnumbered(n) => { - c.update_unnumbered_neighbor(&n).await?; + c.update_unnumbered_neighbor_v2(&n).await?; } } Ok(()) @@ -1469,10 +1477,10 @@ async fn update_nbr(nbr: Neighbor, c: Client) -> Result<()> { async fn delete_nbr(asn: u32, peer: String, c: Client) -> Result<()> { match parse_peer_type(&peer) { PeerType::Numbered(addr) => { - c.delete_neighbor_v3(asn, &addr.to_string()).await?; + c.delete_neighbor_v4(asn, &addr.to_string()).await?; } PeerType::Unnumbered(interface) => { - c.delete_unnumbered_neighbor(asn, &interface).await?; + c.delete_unnumbered_neighbor_v2(asn, &interface).await?; } } Ok(()) @@ -1588,7 +1596,7 @@ async fn read_origin6(asn: u32, c: Client) -> Result<()> { async fn apply(filename: String, c: Client) -> Result<()> { let contents = read_to_string(filename)?; let request: types::ApplyRequest = serde_json::from_str(&contents)?; - c.bgp_apply_v2(&request).await?; + c.bgp_apply_v3(&request).await?; Ok(()) } diff --git a/mgd/src/admin.rs b/mgd/src/admin.rs index 68d1a5fa3..8fdab7d1e 100644 --- a/mgd/src/admin.rs +++ b/mgd/src/admin.rs @@ -185,13 +185,13 @@ impl MgAdminApi for MgAdminApiImpl { async fn read_neighbors_v2( ctx: RequestContext, request: Query, - ) -> Result>, HttpError> { + ) -> Result>, HttpError> { bgp_admin::read_neighbors_v2(ctx, request).await } async fn create_neighbor_v2( ctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result { bgp_admin::create_neighbor_v2(ctx, request).await } @@ -199,13 +199,13 @@ impl MgAdminApi for MgAdminApiImpl { async fn read_neighbor_v2( ctx: RequestContext, request: Query, - ) -> Result, HttpError> { + ) -> Result, HttpError> { bgp_admin::read_neighbor_v2(ctx, request).await } async fn update_neighbor_v2( ctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result { bgp_admin::update_neighbor_v2(ctx, request).await } @@ -219,7 +219,7 @@ impl MgAdminApi for MgAdminApiImpl { async fn create_neighbor_v3( ctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result { bgp_admin::create_neighbor_v3(ctx, request).await } @@ -227,20 +227,20 @@ impl MgAdminApi for MgAdminApiImpl { async fn read_neighbor_v3( ctx: RequestContext, path: Path, - ) -> Result, HttpError> { + ) -> Result, HttpError> { bgp_admin::read_neighbor_v3(ctx, path).await } async fn read_neighbors_v3( ctx: RequestContext, path: Path, - ) -> Result>, HttpError> { + ) -> Result>, HttpError> { bgp_admin::read_neighbors_v3(ctx, path).await } async fn update_neighbor_v3( ctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result { bgp_admin::update_neighbor_v3(ctx, request).await } @@ -252,6 +252,41 @@ impl MgAdminApi for MgAdminApiImpl { bgp_admin::delete_neighbor_v3(ctx, path).await } + async fn create_neighbor_v4( + ctx: RequestContext, + request: TypedBody, + ) -> Result { + bgp_admin::create_neighbor_v4(ctx, request).await + } + + async fn read_neighbor_v4( + ctx: RequestContext, + path: Path, + ) -> Result, HttpError> { + bgp_admin::read_neighbor_v4(ctx, path).await + } + + async fn read_neighbors_v4( + ctx: RequestContext, + path: Path, + ) -> Result>, HttpError> { + bgp_admin::read_neighbors_v4(ctx, path).await + } + + async fn update_neighbor_v4( + ctx: RequestContext, + request: TypedBody, + ) -> Result { + bgp_admin::update_neighbor_v4(ctx, request).await + } + + async fn delete_neighbor_v4( + ctx: RequestContext, + path: Path, + ) -> Result { + bgp_admin::delete_neighbor_v4(ctx, path).await + } + async fn clear_neighbor( ctx: RequestContext, request: TypedBody, @@ -266,18 +301,18 @@ impl MgAdminApi for MgAdminApiImpl { bgp_admin::clear_neighbor_v2(ctx, request).await } - // Unnumbered neighbors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Unnumbered neighbors (no DSCP) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ async fn read_unnumbered_neighbors( rqctx: RequestContext, request: Query, - ) -> Result>, HttpError> { + ) -> Result>, HttpError> { bgp_admin::read_unnumbered_neighbors(rqctx, request).await } async fn create_unnumbered_neighbor( rqctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result { bgp_admin::create_unnumbered_neighbor(rqctx, request).await } @@ -285,13 +320,13 @@ impl MgAdminApi for MgAdminApiImpl { async fn read_unnumbered_neighbor( rqctx: RequestContext, request: Query, - ) -> Result, HttpError> { + ) -> Result, HttpError> { bgp_admin::read_unnumbered_neighbor(rqctx, request).await } async fn update_unnumbered_neighbor( rqctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result { bgp_admin::update_unnumbered_neighbor(rqctx, request).await } @@ -303,6 +338,43 @@ impl MgAdminApi for MgAdminApiImpl { bgp_admin::delete_unnumbered_neighbor(rqctx, request).await } + // Unnumbered neighbors (with DSCP) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + async fn read_unnumbered_neighbors_v2( + rqctx: RequestContext, + request: Query, + ) -> Result>, HttpError> { + bgp_admin::read_unnumbered_neighbors_v2(rqctx, request).await + } + + async fn create_unnumbered_neighbor_v2( + rqctx: RequestContext, + request: TypedBody, + ) -> Result { + bgp_admin::create_unnumbered_neighbor_v2(rqctx, request).await + } + + async fn read_unnumbered_neighbor_v2( + rqctx: RequestContext, + request: Query, + ) -> Result, HttpError> { + bgp_admin::read_unnumbered_neighbor_v2(rqctx, request).await + } + + async fn update_unnumbered_neighbor_v2( + rqctx: RequestContext, + request: TypedBody, + ) -> Result { + bgp_admin::update_unnumbered_neighbor_v2(rqctx, request).await + } + + async fn delete_unnumbered_neighbor_v2( + rqctx: RequestContext, + request: Query, + ) -> Result { + bgp_admin::delete_unnumbered_neighbor_v2(rqctx, request).await + } + async fn clear_unnumbered_neighbor( rqctx: RequestContext, request: TypedBody, @@ -475,11 +547,18 @@ impl MgAdminApi for MgAdminApiImpl { async fn bgp_apply_v2( ctx: RequestContext, - request: TypedBody, + request: TypedBody, ) -> Result { bgp_admin::bgp_apply_v2(ctx, request).await } + async fn bgp_apply_v3( + ctx: RequestContext, + request: TypedBody, + ) -> Result { + bgp_admin::bgp_apply_v3(ctx, request).await + } + async fn message_history( ctx: RequestContext, request: TypedBody, diff --git a/mgd/src/bgp_admin.rs b/mgd/src/bgp_admin.rs index bbef4068d..8681194c3 100644 --- a/mgd/src/bgp_admin.rs +++ b/mgd/src/bgp_admin.rs @@ -44,7 +44,7 @@ use mg_api::{ }; use mg_common::lock; use rdb::{ - AddressFamily, Asn, BgpRouterInfo, ImportExportPolicy4, + AddressFamily, Asn, BgpRouterInfo, Dscp, ImportExportPolicy4, ImportExportPolicy6, ImportExportPolicyV1, Prefix, Prefix4, Prefix6, }; use slog::Logger; @@ -192,7 +192,7 @@ pub async fn delete_router( pub async fn read_neighbors_v2( ctx: RequestContext>, request: Query, -) -> Result>, HttpError> { +) -> Result>, HttpError> { let rq = request.into_inner(); let ctx = ctx.context(); @@ -204,7 +204,7 @@ pub async fn read_neighbors_v2( let result = nbrs .into_iter() .filter(|x| x.asn == rq.asn) - .map(|x| Neighbor::from_rdb_neighbor_info(rq.asn, &x)) + .map(|x| NeighborV2::from(Neighbor::from_rdb_neighbor_info(rq.asn, &x))) .collect(); Ok(HttpResponseOk(result)) @@ -303,9 +303,9 @@ pub async fn clear_neighbor_v2( pub async fn create_neighbor_v2( ctx: RequestContext>, - request: TypedBody, + request: TypedBody, ) -> Result { - let rq = request.into_inner(); + let rq: Neighbor = request.into_inner().into(); let ctx = ctx.context(); helpers::add_neighbor(ctx.clone(), rq, false)?; Ok(HttpResponseUpdatedNoContent()) @@ -314,7 +314,7 @@ pub async fn create_neighbor_v2( pub async fn read_neighbor_v2( ctx: RequestContext>, request: Query, -) -> Result, HttpError> { +) -> Result, HttpError> { let rq = request.into_inner(); let db_neighbors = ctx.context().db.get_bgp_neighbors().map_err(|e| { HttpError::for_internal_error(format!("get neighbors kv tree: {e}")) @@ -327,15 +327,18 @@ pub async fn read_neighbor_v2( format!("neighbor {} not found in db", rq.addr), ))?; - let result = Neighbor::from_rdb_neighbor_info(rq.asn, neighbor_info); + let result = NeighborV2::from(Neighbor::from_rdb_neighbor_info( + rq.asn, + neighbor_info, + )); Ok(HttpResponseOk(result)) } pub async fn update_neighbor_v2( ctx: RequestContext>, - request: TypedBody, + request: TypedBody, ) -> Result { - let rq = request.into_inner(); + let rq: Neighbor = request.into_inner().into(); let ctx = ctx.context(); helpers::add_neighbor(ctx.clone(), rq, true)?; Ok(HttpResponseUpdatedNoContent()) @@ -350,25 +353,105 @@ pub async fn delete_neighbor_v2( Ok(helpers::remove_neighbor(ctx.clone(), rq.asn, rq.addr).await?) } -// V3 API - Unified neighbor operations supporting both numbered and unnumbered +// V3 API - Unified neighbor operations (no DSCP) pub async fn create_neighbor_v3( ctx: RequestContext>, - request: TypedBody, + request: TypedBody, ) -> Result { - // Delegate to v2 - create operation doesn't depend on selector type - create_neighbor_v2(ctx, request).await + let rq: Neighbor = request.into_inner().into(); + let ctx = ctx.context(); + helpers::add_neighbor(ctx.clone(), rq, false)?; + Ok(HttpResponseUpdatedNoContent()) } pub async fn read_neighbor_v3( ctx: RequestContext>, path: Path, +) -> Result, HttpError> { + Ok(HttpResponseOk(do_read_neighbor(ctx, path)?.into())) +} + +pub async fn read_neighbors_v3( + ctx: RequestContext>, + path: Path, +) -> Result>, HttpError> { + Ok(HttpResponseOk( + do_read_neighbors(ctx, path)? + .into_iter() + .map(NeighborV2::from) + .collect(), + )) +} + +pub async fn update_neighbor_v3( + ctx: RequestContext>, + request: TypedBody, +) -> Result { + let rq: Neighbor = request.into_inner().into(); + let ctx = ctx.context(); + helpers::add_neighbor(ctx.clone(), rq, true)?; + Ok(HttpResponseUpdatedNoContent()) +} + +pub async fn delete_neighbor_v3( + ctx: RequestContext>, + path: Path, +) -> Result { + do_delete_neighbor(ctx, path).await +} + +// V4 API - Unified neighbor operations (with DSCP) +pub async fn create_neighbor_v4( + ctx: RequestContext>, + request: TypedBody, +) -> Result { + let rq = request.into_inner(); + let ctx = ctx.context(); + helpers::add_neighbor(ctx.clone(), rq, false)?; + Ok(HttpResponseUpdatedNoContent()) +} + +pub async fn read_neighbor_v4( + ctx: RequestContext>, + path: Path, ) -> Result, HttpError> { + Ok(HttpResponseOk(do_read_neighbor(ctx, path)?)) +} + +pub async fn read_neighbors_v4( + ctx: RequestContext>, + path: Path, +) -> Result>, HttpError> { + Ok(HttpResponseOk(do_read_neighbors(ctx, path)?)) +} + +pub async fn update_neighbor_v4( + ctx: RequestContext>, + request: TypedBody, +) -> Result { + let rq = request.into_inner(); + let ctx = ctx.context(); + helpers::add_neighbor(ctx.clone(), rq, true)?; + Ok(HttpResponseUpdatedNoContent()) +} + +pub async fn delete_neighbor_v4( + ctx: RequestContext>, + path: Path, +) -> Result { + do_delete_neighbor(ctx, path).await +} + +/// Shared implementation for read_neighbor_v3 and read_neighbor_v4 +fn do_read_neighbor( + ctx: RequestContext>, + path: Path, +) -> Result { let rq = path.into_inner(); let peer_id = rq.to_peer_id(); match peer_id { PeerId::Ip(addr) => { - // Numbered peer - query numbered neighbors DB let db_neighbors = ctx.context().db.get_bgp_neighbors().map_err(|e| { HttpError::for_internal_error(format!( @@ -382,12 +465,9 @@ pub async fn read_neighbor_v3( None, format!("neighbor {} not found in db", addr), ))?; - let result = - Neighbor::from_rdb_neighbor_info(rq.asn, neighbor_info); - Ok(HttpResponseOk(result)) + Ok(Neighbor::from_rdb_neighbor_info(rq.asn, neighbor_info)) } PeerId::Interface(ref iface) => { - // Unnumbered peer - query unnumbered neighbors DB let db_neighbors = ctx.context().db.get_unnumbered_bgp_neighbors().map_err( |e| { @@ -407,8 +487,7 @@ pub async fn read_neighbor_v3( rq.asn, neighbor_info, ); - // Convert UnnumberedNeighbor to Neighbor - Ok(HttpResponseOk(Neighbor { + Ok(Neighbor { asn: result.asn, name: result.name, group: result.group, @@ -417,15 +496,16 @@ pub async fn read_neighbor_v3( 179, ), parameters: result.parameters, - })) + }) } } } -pub async fn read_neighbors_v3( +/// Shared implementation for read_neighbors_v3 and read_neighbors_v4 +fn do_read_neighbors( ctx: RequestContext>, path: Path, -) -> Result>, HttpError> { +) -> Result, HttpError> { let rq = path.into_inner(); let ctx = ctx.context(); @@ -434,24 +514,15 @@ pub async fn read_neighbors_v3( .get_bgp_neighbors() .map_err(|e| HttpError::for_internal_error(e.to_string()))?; - let result = nbrs + Ok(nbrs .into_iter() .filter(|x| x.asn == rq.asn) .map(|x| Neighbor::from_rdb_neighbor_info(rq.asn, &x)) - .collect(); - - Ok(HttpResponseOk(result)) -} - -pub async fn update_neighbor_v3( - ctx: RequestContext>, - request: TypedBody, -) -> Result { - // Delegate to v2 - update operation doesn't depend on selector type - update_neighbor_v2(ctx, request).await + .collect()) } -pub async fn delete_neighbor_v3( +/// Shared implementation for delete_neighbor_v3 and delete_neighbor_v4 +async fn do_delete_neighbor( ctx: RequestContext>, path: Path, ) -> Result { @@ -472,28 +543,68 @@ pub async fn delete_neighbor_v3( // Unnumbered neighbors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// Unnumbered neighbors (no DSCP) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + pub async fn read_unnumbered_neighbors( rqctx: RequestContext>, request: Query, -) -> Result>, HttpError> { - let rq = request.into_inner(); +) -> Result>, HttpError> { + Ok(HttpResponseOk( + do_read_unnumbered_neighbors(rqctx, request)? + .into_iter() + .map(UnnumberedNeighborV1::from) + .collect(), + )) +} + +pub async fn create_unnumbered_neighbor( + rqctx: RequestContext>, + request: TypedBody, +) -> Result { + let rq: UnnumberedNeighbor = request.into_inner().into(); let ctx = rqctx.context(); + helpers::add_unnumbered_neighbor(ctx.clone(), rq, false)?; + Ok(HttpResponseUpdatedNoContent()) +} - let nbrs = ctx - .db - .get_unnumbered_bgp_neighbors() - .map_err(|e| HttpError::for_internal_error(e.to_string()))?; +pub async fn read_unnumbered_neighbor( + rqctx: RequestContext>, + request: Query, +) -> Result, HttpError> { + Ok(HttpResponseOk( + do_read_unnumbered_neighbor(rqctx, request)?.into(), + )) +} - let result = nbrs - .into_iter() - .filter(|x| x.asn == rq.asn) - .map(|x| UnnumberedNeighbor::from_rdb_neighbor_info(rq.asn, &x)) - .collect(); +pub async fn update_unnumbered_neighbor( + rqctx: RequestContext>, + request: TypedBody, +) -> Result { + let rq: UnnumberedNeighbor = request.into_inner().into(); + let ctx = rqctx.context(); + helpers::add_unnumbered_neighbor(ctx.clone(), rq, true)?; + Ok(HttpResponseUpdatedNoContent()) +} - Ok(HttpResponseOk(result)) +pub async fn delete_unnumbered_neighbor( + rqctx: RequestContext>, + request: Query, +) -> Result { + do_delete_unnumbered_neighbor(rqctx, request).await } -pub async fn create_unnumbered_neighbor( +// Unnumbered neighbors (with DSCP) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +pub async fn read_unnumbered_neighbors_v2( + rqctx: RequestContext>, + request: Query, +) -> Result>, HttpError> { + Ok(HttpResponseOk(do_read_unnumbered_neighbors( + rqctx, request, + )?)) +} + +pub async fn create_unnumbered_neighbor_v2( rqctx: RequestContext>, request: TypedBody, ) -> Result { @@ -503,10 +614,55 @@ pub async fn create_unnumbered_neighbor( Ok(HttpResponseUpdatedNoContent()) } -pub async fn read_unnumbered_neighbor( +pub async fn read_unnumbered_neighbor_v2( rqctx: RequestContext>, request: Query, ) -> Result, HttpError> { + Ok(HttpResponseOk(do_read_unnumbered_neighbor(rqctx, request)?)) +} + +pub async fn update_unnumbered_neighbor_v2( + rqctx: RequestContext>, + request: TypedBody, +) -> Result { + let rq = request.into_inner(); + let ctx = rqctx.context(); + helpers::add_unnumbered_neighbor(ctx.clone(), rq, true)?; + Ok(HttpResponseUpdatedNoContent()) +} + +pub async fn delete_unnumbered_neighbor_v2( + rqctx: RequestContext>, + request: Query, +) -> Result { + do_delete_unnumbered_neighbor(rqctx, request).await +} + +/// Shared implementation for read_unnumbered_neighbors{,_v2} +fn do_read_unnumbered_neighbors( + rqctx: RequestContext>, + request: Query, +) -> Result, HttpError> { + let rq = request.into_inner(); + let ctx = rqctx.context(); + + let nbrs = ctx + .db + .get_unnumbered_bgp_neighbors() + .map_err(|e| HttpError::for_internal_error(e.to_string()))?; + + Ok(nbrs + .into_iter() + .filter(|x| x.asn == rq.asn) + .map(|x| UnnumberedNeighbor::from_rdb_neighbor_info(rq.asn, &x)) + .collect()) +} + +/// Shared implementation for read_unnumbered_neighbor{,_v2} +fn do_read_unnumbered_neighbor( + rqctx: RequestContext>, + request: Query, +) -> Result { let rq = request.into_inner(); let db_neighbors = rqctx .context() @@ -523,22 +679,14 @@ pub async fn read_unnumbered_neighbor( format!("neighbor {} not found in db", rq.interface), ))?; - let result = - UnnumberedNeighbor::from_rdb_neighbor_info(rq.asn, neighbor_info); - Ok(HttpResponseOk(result)) -} - -pub async fn update_unnumbered_neighbor( - rqctx: RequestContext>, - request: TypedBody, -) -> Result { - let rq = request.into_inner(); - let ctx = rqctx.context(); - helpers::add_unnumbered_neighbor(ctx.clone(), rq, true)?; - Ok(HttpResponseUpdatedNoContent()) + Ok(UnnumberedNeighbor::from_rdb_neighbor_info( + rq.asn, + neighbor_info, + )) } -pub async fn delete_unnumbered_neighbor( +/// Shared implementation for delete_unnumbered_neighbor{,_v2} +async fn do_delete_unnumbered_neighbor( rqctx: RequestContext>, request: Query, ) -> Result { @@ -1337,6 +1485,13 @@ pub async fn bgp_apply( } pub async fn bgp_apply_v2( + ctx: RequestContext>, + request: TypedBody, +) -> Result { + do_bgp_apply(ctx.context(), ApplyRequest::from(request.into_inner())).await +} + +pub async fn bgp_apply_v3( ctx: RequestContext>, request: TypedBody, ) -> Result { @@ -2141,6 +2296,7 @@ pub(crate) mod helpers { allow_export6: ImportExportPolicy6::NoFiltering, nexthop4: None, nexthop6: None, + dscp: Dscp::default(), }, })?; @@ -2251,6 +2407,7 @@ pub(crate) mod helpers { nexthop4, nexthop6, vlan_id: rq.parameters.vlan_id, + dscp: rq.parameters.dscp, }, })?; @@ -2374,6 +2531,7 @@ pub(crate) mod helpers { nexthop4, nexthop6, vlan_id: rq.parameters.vlan_id, + dscp: rq.parameters.dscp, }, })?; diff --git a/mgd/src/main.rs b/mgd/src/main.rs index bb04dcb1a..695dd26eb 100644 --- a/mgd/src/main.rs +++ b/mgd/src/main.rs @@ -361,6 +361,7 @@ fn start_bgp_routers( None }, vlan_id: nbr.parameters.vlan_id, + dscp: nbr.parameters.dscp, }, }, true, @@ -416,6 +417,7 @@ fn start_bgp_routers( None }, vlan_id: nbr.parameters.vlan_id, + dscp: nbr.parameters.dscp, }, }, true, diff --git a/openapi/mg-admin/mg-admin-7.0.0-26a5e9.json b/openapi/mg-admin/mg-admin-7.0.0-2e3fe3.json similarity index 98% rename from openapi/mg-admin/mg-admin-7.0.0-26a5e9.json rename to openapi/mg-admin/mg-admin-7.0.0-2e3fe3.json index 65d2dfc16..af541a99e 100644 --- a/openapi/mg-admin/mg-admin-7.0.0-26a5e9.json +++ b/openapi/mg-admin/mg-admin-7.0.0-2e3fe3.json @@ -259,7 +259,7 @@ }, "/bgp/config/neighbor": { "put": { - "operationId": "create_neighbor_v3", + "operationId": "create_neighbor_v4", "requestBody": { "content": { "application/json": { @@ -283,7 +283,7 @@ } }, "post": { - "operationId": "update_neighbor_v3", + "operationId": "update_neighbor_v4", "requestBody": { "content": { "application/json": { @@ -309,7 +309,7 @@ }, "/bgp/config/neighbor/{asn}/{peer}": { "get": { - "operationId": "read_neighbor_v3", + "operationId": "read_neighbor_v4", "parameters": [ { "in": "path", @@ -351,7 +351,7 @@ } }, "delete": { - "operationId": "delete_neighbor_v3", + "operationId": "delete_neighbor_v4", "parameters": [ { "in": "path", @@ -388,7 +388,7 @@ }, "/bgp/config/neighbors/{asn}": { "get": { - "operationId": "read_neighbors_v3", + "operationId": "read_neighbors_v4", "parameters": [ { "in": "path", @@ -899,7 +899,7 @@ }, "/bgp/config/unnumbered-neighbor": { "get": { - "operationId": "read_unnumbered_neighbor", + "operationId": "read_unnumbered_neighbor_v2", "parameters": [ { "in": "query", @@ -940,7 +940,7 @@ } }, "put": { - "operationId": "create_unnumbered_neighbor", + "operationId": "create_unnumbered_neighbor_v2", "requestBody": { "content": { "application/json": { @@ -964,7 +964,7 @@ } }, "post": { - "operationId": "update_unnumbered_neighbor", + "operationId": "update_unnumbered_neighbor_v2", "requestBody": { "content": { "application/json": { @@ -988,7 +988,7 @@ } }, "delete": { - "operationId": "delete_unnumbered_neighbor", + "operationId": "delete_unnumbered_neighbor_v2", "parameters": [ { "in": "query", @@ -1024,7 +1024,7 @@ }, "/bgp/config/unnumbered-neighbors": { "get": { - "operationId": "read_unnumbered_neighbors", + "operationId": "read_unnumbered_neighbors_v2", "parameters": [ { "in": "query", @@ -1130,7 +1130,7 @@ }, "/bgp/omicron/apply": { "post": { - "operationId": "bgp_apply_v2", + "operationId": "bgp_apply_v3", "requestBody": { "content": { "application/json": { @@ -2218,6 +2218,15 @@ "description": "Enable deterministic collision resolution in Established state. When true, uses BGP-ID comparison per RFC 4271 §6.8 for collision resolution even when one connection is already in Established state. When false, Established connection always wins (timing-based resolution).", "type": "boolean" }, + "dscp": { + "description": "DSCP value for BGP TCP connections (0-63). RFC 4271 Appendix E recommends CS6 (48) for BGP traffic. Default: CS6 (48).", + "default": 48, + "allOf": [ + { + "$ref": "#/components/schemas/Dscp" + } + ] + }, "enforce_first_as": { "type": "boolean" }, @@ -2875,6 +2884,13 @@ "routes" ] }, + "Dscp": { + "description": "DSCP value (0-63). Default: CS6 (48).", + "type": "integer", + "format": "uint8", + "minimum": 0, + "maximum": 63 + }, "Duration": { "type": "object", "properties": { @@ -3969,6 +3985,15 @@ "description": "Enable deterministic collision resolution in Established state. When true, uses BGP-ID comparison per RFC 4271 §6.8 for collision resolution even when one connection is already in Established state. When false, Established connection always wins (timing-based resolution).", "type": "boolean" }, + "dscp": { + "description": "DSCP value for BGP TCP connections (0-63). RFC 4271 Appendix E recommends CS6 (48) for BGP traffic. Default: CS6 (48).", + "default": 48, + "allOf": [ + { + "$ref": "#/components/schemas/Dscp" + } + ] + }, "enforce_first_as": { "type": "boolean" }, @@ -5511,6 +5536,15 @@ "description": "Enable deterministic collision resolution in Established state. When true, uses BGP-ID comparison per RFC 4271 §6.8 for collision resolution even when one connection is already in Established state. When false, Established connection always wins (timing-based resolution).", "type": "boolean" }, + "dscp": { + "description": "DSCP value for BGP TCP connections (0-63). RFC 4271 Appendix E recommends CS6 (48) for BGP traffic. Default: CS6 (48).", + "default": 48, + "allOf": [ + { + "$ref": "#/components/schemas/Dscp" + } + ] + }, "enforce_first_as": { "type": "boolean" }, @@ -5670,6 +5704,15 @@ "description": "Enable deterministic collision resolution in Established state. When true, uses BGP-ID comparison per RFC 4271 §6.8 for collision resolution even when one connection is already in Established state. When false, Established connection always wins (timing-based resolution).", "type": "boolean" }, + "dscp": { + "description": "DSCP value for BGP TCP connections (0-63). RFC 4271 Appendix E recommends CS6 (48) for BGP traffic. Default: CS6 (48).", + "default": 48, + "allOf": [ + { + "$ref": "#/components/schemas/Dscp" + } + ] + }, "enforce_first_as": { "type": "boolean" }, diff --git a/openapi/mg-admin/mg-admin-latest.json b/openapi/mg-admin/mg-admin-latest.json index 3d80ea1f5..f6f339833 120000 --- a/openapi/mg-admin/mg-admin-latest.json +++ b/openapi/mg-admin/mg-admin-latest.json @@ -1 +1 @@ -mg-admin-7.0.0-26a5e9.json \ No newline at end of file +mg-admin-7.0.0-2e3fe3.json \ No newline at end of file diff --git a/rdb-types/src/lib.rs b/rdb-types/src/lib.rs index 7b1b00e4e..599868b7f 100644 --- a/rdb-types/src/lib.rs +++ b/rdb-types/src/lib.rs @@ -604,3 +604,106 @@ impl PeerId { matches!(self, Self::Ip(_)) } } + +/// DSCP value for BGP TCP connections (0-63). +/// +/// RFC 4271 Appendix E recommends CS6 (48) for BGP traffic. +/// Default: CS6 (48). +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(try_from = "u8", into = "u8")] +pub struct Dscp(u8); + +impl Dscp { + /// CS6 (48) — the RFC 4271 Appendix E recommended default. + pub const CS6: Self = Self(48); + + /// Create a new DSCP value from a raw 6-bit value (0-63). + pub fn new(val: u8) -> Result { + if val > 63 { + Err(format!("DSCP value {val} out of range (0-63)")) + } else { + Ok(Self(val)) + } + } + + /// Create a DSCP value from a TOS/Traffic-Class byte as + /// returned by `getsockopt(IP_TOS)` or `getsockopt(IPV6_TCLASS)`. + /// Extracts the upper 6 bits (DSCP), ignoring the lower 2 ECN + /// bits. + pub fn from_tos_byte(tos: u8) -> Self { + Self(tos >> 2) + } + + /// Return the raw numeric value (0-63). + pub fn value(self) -> u8 { + self.0 + } + + /// Return the TOS/Traffic-Class byte for use with `IP_TOS` or + /// `IPV6_TCLASS`. The DSCP value occupies the upper 6 bits of + /// the byte (value << 2), with the lower 2 ECN bits left zero. + pub fn tos_byte(self) -> u8 { + self.0 << 2 + } +} + +impl Default for Dscp { + fn default() -> Self { + Self::CS6 + } +} + +impl TryFrom for Dscp { + type Error = String; + fn try_from(val: u8) -> Result { + Self::new(val) + } +} + +impl From for u8 { + fn from(d: Dscp) -> u8 { + d.0 + } +} + +impl fmt::Display for Dscp { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } +} + +impl FromStr for Dscp { + type Err = String; + fn from_str(s: &str) -> Result { + let val: u8 = + s.parse().map_err(|_| format!("invalid DSCP value: {s}"))?; + Self::new(val) + } +} + +impl JsonSchema for Dscp { + fn schema_name() -> String { + "Dscp".to_string() + } + fn json_schema( + _g: &mut schemars::r#gen::SchemaGenerator, + ) -> schemars::schema::Schema { + schemars::schema::SchemaObject { + instance_type: Some(schemars::schema::InstanceType::Integer.into()), + format: Some("uint8".to_string()), + number: Some(Box::new(schemars::schema::NumberValidation { + minimum: Some(0.0), + maximum: Some(63.0), + ..Default::default() + })), + metadata: Some(Box::new(schemars::schema::Metadata { + description: Some( + "DSCP value (0-63). Default: CS6 (48).".to_string(), + ), + ..Default::default() + })), + ..Default::default() + } + .into() + } +} diff --git a/rdb/src/proptest.rs b/rdb/src/proptest.rs index b7e0d66e0..9f68faf1f 100644 --- a/rdb/src/proptest.rs +++ b/rdb/src/proptest.rs @@ -9,7 +9,7 @@ //! tests, which are in bgp/src/proptest.rs since they test BgpWireFormat). use crate::{ - BgpNeighborParameters, + BgpNeighborParameters, Dscp, types::{ BgpNeighborInfo, ImportExportPolicy4, ImportExportPolicy6, Prefix, Prefix4, Prefix6, StaticRouteKey, @@ -200,6 +200,7 @@ fn bgp_neighbor_info_strategy() -> impl Strategy { nexthop4, nexthop6, vlan_id: Some(1), + dscp: Dscp::default(), }, } }, diff --git a/rdb/src/types.rs b/rdb/src/types.rs index 48be0866e..0dd42ca1f 100644 --- a/rdb/src/types.rs +++ b/rdb/src/types.rs @@ -17,7 +17,7 @@ use std::str::FromStr; // Re-export core types from rdb-types pub use rdb_types::{ - AddressFamily, PeerId, Prefix, Prefix4, Prefix6, ProtocolFilter, + AddressFamily, Dscp, PeerId, Prefix, Prefix4, Prefix6, ProtocolFilter, }; // Marker types for compile-time address family discrimination. @@ -692,6 +692,10 @@ pub struct BgpNeighborParameters { #[serde(default)] pub nexthop6: Option, pub vlan_id: Option, + /// DSCP value for BGP TCP connections (0-63). + /// Default: CS6 (48). + #[serde(default)] + pub dscp: Dscp, } /// Default value for ipv4_enabled - true for backward compatibility From ebf438ae6bf3f94e52221405aa661307f14fff33 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 25 Feb 2026 17:28:50 -0700 Subject: [PATCH 22/58] bgp: update min_tll and dscp gracefully Adds logic to update the DSCP and TTL values on TCP sockets in-flight for all active connections of a SessionRunner. This means that we update the sockets and allow BGP to manage the aftermath. e.g. We won't explicitly close the connection when setting the TTL to 1, but that doesn't mean the connection isn't doomed. The connection may drop due to the practical implications of changing the TTL on the socket. Manual testing confirms (via snoop) that the QoS marking is properly updated (added/removed/changed) along with the BGP neighbor config, without causing a reset of the session. --- bgp/src/connection.rs | 9 ++ bgp/src/connection_channel.rs | 10 +- bgp/src/connection_tcp.rs | 36 ++++++- bgp/src/session.rs | 22 +++- bgp/src/test.rs | 183 ++++++++++++++++++++++++++++++++++ 5 files changed, 252 insertions(+), 8 deletions(-) diff --git a/bgp/src/connection.rs b/bgp/src/connection.rs index 2eea59de9..91d421b49 100644 --- a/bgp/src/connection.rs +++ b/bgp/src/connection.rs @@ -217,6 +217,15 @@ pub trait BgpConnection: Send + Sync + Sized { /// Start the receive loop for this connection. This method is idempotent. /// Returns Ok(()) upon successful start of recv loop, else Err. fn start_recv_loop(self: &Arc) -> Result<(), Error>; + + /// Update a socket option on a live connection. + fn update_socket_option(&self, option: &SocketOption) -> Result<(), Error>; +} + +/// A socket option that can be updated on a live BGP connection. +pub enum SocketOption { + Dscp(Dscp), + MinTtl(Option), } pub use mg_common::thread::{ManagedThread, ThreadState}; diff --git a/bgp/src/connection_channel.rs b/bgp/src/connection_channel.rs index d4d26401b..352798310 100644 --- a/bgp/src/connection_channel.rs +++ b/bgp/src/connection_channel.rs @@ -12,7 +12,7 @@ use crate::{ clock::ConnectionClock, connection::{ BgpConnection, BgpConnector, BgpListener, ConnectionDirection, - ConnectionId, ThreadState, + ConnectionId, SocketOption, ThreadState, }, error::Error, log::{connection_log, connection_log_lite}, @@ -379,6 +379,14 @@ impl BgpConnection for BgpConnectionChannel { Ok(()) } + + fn update_socket_option( + &self, + _option: &SocketOption, + ) -> Result<(), Error> { + // Socket options are ignored for test connections + Ok(()) + } } impl BgpConnectionChannel { diff --git a/bgp/src/connection_tcp.rs b/bgp/src/connection_tcp.rs index a3939a3b0..44bb2f1a0 100644 --- a/bgp/src/connection_tcp.rs +++ b/bgp/src/connection_tcp.rs @@ -7,7 +7,7 @@ use crate::{ clock::ConnectionClock, connection::{ BgpConnection, BgpConnector, BgpListener, ConnectionDirection, - ConnectionId, ThreadState, + ConnectionId, SocketOption, ThreadState, }, error::Error, log::{connection_log, connection_log_lite}, @@ -518,6 +518,16 @@ impl BgpConnection for BgpConnectionTcp { Ok(()) } + + fn update_socket_option(&self, option: &SocketOption) -> Result<(), Error> { + let guard = lock!(self.conn); + match *option { + SocketOption::Dscp(dscp) => apply_dscp(&guard, dscp, self.peer), + SocketOption::MinTtl(ttl) => { + apply_min_ttl(&guard, ttl.unwrap_or_default(), self.peer) + } + } + } } impl Drop for BgpConnectionTcp { @@ -1266,7 +1276,10 @@ fn apply_dscp( Ok(()) } -/// Apply min TTL setting to a TCP connection +/// Apply min TTL setting to a TCP connection. +/// +/// Sets both the outgoing IP_TTL and the incoming IP_MINTTL filter +/// to `ttl`. #[allow(unused_variables)] fn apply_min_ttl( conn: &TcpStream, @@ -1274,17 +1287,30 @@ fn apply_min_ttl( peer: SocketAddr, ) -> Result<(), Error> { conn.set_ttl(ttl.into())?; + set_ip_minttl(conn, ttl, peer) +} + +/// Set the IP_MINTTL socket option (minimum acceptable incoming TTL). +/// +/// A value of 0 disables the check. Only effective on Linux/illumos; +/// a no-op on other platforms. +#[allow(unused_variables)] +fn set_ip_minttl( + conn: &TcpStream, + min_ttl: u8, + peer: SocketAddr, +) -> Result<(), Error> { #[cfg(any(target_os = "linux", target_os = "illumos"))] { let fd = conn.as_raw_fd(); - let min_ttl = ttl as u32; + let val = min_ttl as u32; unsafe { if peer.is_ipv4() && libc::setsockopt( fd, IPPROTO_IP, IP_MINTTL, - &min_ttl as *const u32 as *const c_void, + &val as *const u32 as *const c_void, std::mem::size_of::() as u32, ) != 0 { @@ -1295,7 +1321,7 @@ fn apply_min_ttl( fd, IPPROTO_IPV6, IP_MINTTL, - &min_ttl as *const u32 as *const c_void, + &val as *const u32 as *const c_void, std::mem::size_of::() as u32, ) != 0 { diff --git a/bgp/src/session.rs b/bgp/src/session.rs index 16540de7c..85dc2e035 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -7,6 +7,7 @@ use crate::{ config::PeerConfig, connection::{ BgpConnection, BgpConnector, ConnectionDirection, ConnectionId, + SocketOption, }, error::{Error, ExpectationMismatch}, fanout::{Fanout4, Fanout6}, @@ -9473,7 +9474,7 @@ impl SessionRunner { if current.min_ttl != info.min_ttl { current.min_ttl = info.min_ttl; - reset_needed = true; + self.apply_sockopt(SocketOption::MinTtl(info.min_ttl)); } if current.md5_auth_key != info.md5_auth_key { @@ -9523,7 +9524,7 @@ impl SessionRunner { if current.dscp != info.dscp { current.dscp = info.dscp; - reset_needed = true; + self.apply_sockopt(SocketOption::Dscp(info.dscp)); } // Update jitter settings (no session reset required) @@ -9636,6 +9637,23 @@ impl SessionRunner { Ok(reset_needed) } + /// Apply a socket option to all live connections. Errors are + /// logged but do not fail the operation — the connection remains + /// valid even if the setsockopt doesn't take effect. + fn apply_sockopt(&self, option: SocketOption) { + let registry = lock!(self.connection_registry); + for conn in registry.all_connections() { + if let Err(e) = conn.connection().update_socket_option(&option) { + session_log_lite!( + self, + error, + "failed to update socket option: {e}"; + "error" => format!("{e}") + ); + } + } + } + /// Get all registered connections pub fn all_connections(&self) -> Vec> { lock!(self.connection_registry) diff --git a/bgp/src/test.rs b/bgp/src/test.rs index 470d01b28..29194532a 100644 --- a/bgp/src/test.rs +++ b/bgp/src/test.rs @@ -5121,3 +5121,186 @@ fn test_reset_tracking() { r1.shutdown(); r2.shutdown(); } + +// ============================================================================= +// In-flight DSCP/min_ttl socket option update test +// ============================================================================= + +/// Verify that DSCP and min_ttl changes are applied to live sockets +/// without resetting the BGP session, while md5_auth_key changes +/// still trigger a session reset. +/// +/// This test uses real TCP connections to exercise the setsockopt +/// path through `update_socket_options()`. +#[test] +fn test_inflight_socket_option_updates() { + let r1_addr: SocketAddr = sockaddr!(&format!("127.0.0.18:{TEST_BGP_PORT}")); + let r2_addr: SocketAddr = sockaddr!(&format!("127.0.0.19:{TEST_BGP_PORT}")); + + let _ip_guard = ensure_loop_ips(&[r1_addr.ip(), r2_addr.ip()]); + + let r1_peer_config = PeerConfig { + name: "r2".into(), + group: String::new(), + host: r2_addr, + hold_time: TEST_HOLD_TIME_SECS, + idle_hold_time: 0, + delay_open: 0, + connect_retry: TEST_CONNECT_RETRY_SECS, + keepalive: 3, + resolution: 100, + }; + let r1_session_info = SessionInfo::from_peer_config(&r1_peer_config); + + let r2_peer_config = PeerConfig { + name: "r1".into(), + group: String::new(), + host: r1_addr, + hold_time: TEST_HOLD_TIME_SECS, + idle_hold_time: 0, + delay_open: 0, + connect_retry: TEST_CONNECT_RETRY_SECS, + keepalive: 3, + resolution: 100, + }; + let r2_session_info = SessionInfo::from_peer_config(&r2_peer_config); + + let routers = vec![ + LogicalRouter { + name: "r1".to_string(), + asn: Asn::FourOctet(4200000001), + id: 1, + listen_addr: r1_addr, + bind_addr: Some(r1_addr), + neighbors: vec![NeighborConfig { + peer_name: "r2".to_string(), + remote_host: r2_addr, + session_info: r1_session_info, + }], + }, + LogicalRouter { + name: "r2".to_string(), + asn: Asn::FourOctet(4200000002), + id: 2, + listen_addr: r2_addr, + bind_addr: Some(r2_addr), + neighbors: vec![NeighborConfig { + peer_name: "r1".to_string(), + remote_host: r1_addr, + session_info: r2_session_info, + }], + }, + ]; + + let (test_routers, _ip_guard2) = test_setup::< + BgpConnectionTcp, + BgpListenerTcp, + >("inflight_sockopt", &routers); + + let r1 = &test_routers[0]; + let r2 = &test_routers[1]; + + let r1_session = r1 + .router + .get_session(r2_addr.ip()) + .expect("get r1->r2 session"); + let r2_session = r2 + .router + .get_session(r1_addr.ip()) + .expect("get r2->r1 session"); + wait_for_eq!(r1_session.state(), FsmStateKind::Established); + wait_for_eq!(r2_session.state(), FsmStateKind::Established); + + // Helper: assert both sessions stay Established for longer + // than hold_time and that neither side's transition counter + // increments (i.e. no reset occurred). + let assert_no_reset = |label: &str| { + let r1_before = r1_session + .counters + .transitions_to_established + .load(Ordering::Relaxed); + let r2_before = r2_session + .counters + .transitions_to_established + .load(Ordering::Relaxed); + + let start = Instant::now(); + while start.elapsed() < ESTABLISHED_VERIFICATION { + assert_eq!( + r1_session.state(), + FsmStateKind::Established, + "r1 should stay Established after {label}" + ); + assert_eq!( + r2_session.state(), + FsmStateKind::Established, + "r2 should stay Established after {label}" + ); + sleep(Duration::from_millis(100)); + } + + assert_eq!( + r1_session + .counters + .transitions_to_established + .load(Ordering::Relaxed), + r1_before, + "r1 should not re-establish after {label}" + ); + assert_eq!( + r2_session + .counters + .transitions_to_established + .load(Ordering::Relaxed), + r2_before, + "r2 should not re-establish after {label}" + ); + }; + + // Track cumulative session state so each phase builds on the + // previous one (mirrors how the real API works). + let mut min_ttl = None; + + // ===== Set DSCP (no reset) ===== + let mut dscp = Dscp::new(46).unwrap(); // EF + let mut info = SessionInfo::from_peer_config(&r2_peer_config); + info.dscp = dscp; + info.min_ttl = min_ttl; + r2.router + .update_session(r2_peer_config.clone(), info) + .expect("set DSCP"); + assert_no_reset("setting DSCP"); + + // ===== Set min_ttl (no reset) ===== + min_ttl = Some(1); + let mut info = SessionInfo::from_peer_config(&r2_peer_config); + info.dscp = dscp; + info.min_ttl = min_ttl; + r2.router + .update_session(r2_peer_config.clone(), info) + .expect("set min_ttl"); + assert_no_reset("setting min_ttl"); + + // ===== Unset min_ttl (no reset) ===== + min_ttl = None; + let mut info = SessionInfo::from_peer_config(&r2_peer_config); + info.dscp = dscp; + info.min_ttl = min_ttl; + r2.router + .update_session(r2_peer_config.clone(), info) + .expect("unset min_ttl"); + assert_no_reset("unsetting min_ttl"); + + // ===== Reset DSCP to default (no reset) ===== + dscp = Dscp::default(); + let mut info = SessionInfo::from_peer_config(&r2_peer_config); + info.dscp = dscp; + info.min_ttl = min_ttl; + r2.router + .update_session(r2_peer_config.clone(), info) + .expect("reset DSCP to default"); + assert_no_reset("resetting DSCP to default"); + + r1.shutdown(); + r2.shutdown(); +} From e6a1ebf6dc2572559dfc36d51e232d476099ee44 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Thu, 26 Feb 2026 10:33:23 -0700 Subject: [PATCH 23/58] bgp: fix TTL sockopt calls for IPv6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The outgoing hop-limit and incoming min-hop filter for IPv6 BGP peers were broken in two ways: 1. apply_min_ttl() called TcpStream::set_ttl() unconditionally, which sets IP_TTL — an IPv4-only option. For IPv6 sockets this is silently ignored. Branch on address family and use setsockopt(IPV6_UNICAST_HOPS) for IPv6 peers. 2. set_ip_minttl() used IPV6_HOPLIMIT as the IPv6 equivalent of IP_MINTTL. IPV6_HOPLIMIT is a recvmsg ancillary-data option that delivers the received hop-limit as a cmsg — it does not filter on it. Replace with IPV6_MINHOPCOUNT, which is the actual IPv6 counterpart to IP_MINTTL. Define IPV6_MINHOPCOUNT as a local constant (value 73 for Linux, 47 for Illumos) since it is not exposed by the libc crate for either platform. Fixes: #647 --- bgp/src/connection_tcp.rs | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/bgp/src/connection_tcp.rs b/bgp/src/connection_tcp.rs index 44bb2f1a0..2b1170962 100644 --- a/bgp/src/connection_tcp.rs +++ b/bgp/src/connection_tcp.rs @@ -58,6 +58,10 @@ const UNIT_CONNECTION: &str = "connection_tcp"; #[cfg(target_os = "illumos")] const IP_MINTTL: i32 = 0x1c; +#[cfg(target_os = "linux")] +const IPV6_MINHOPCOUNT: i32 = 73; +#[cfg(target_os = "illumos")] +const IPV6_MINHOPCOUNT: i32 = 0x2f; #[cfg(target_os = "illumos")] const TCP_MD5SIG: i32 = 0x27; #[cfg(target_os = "illumos")] @@ -1278,20 +1282,40 @@ fn apply_dscp( /// Apply min TTL setting to a TCP connection. /// -/// Sets both the outgoing IP_TTL and the incoming IP_MINTTL filter -/// to `ttl`. +/// Sets both the outgoing TTL/hop-limit and the incoming +/// IP_MINTTL / IPV6_MINHOPCOUNT filter to `ttl`. #[allow(unused_variables)] fn apply_min_ttl( conn: &TcpStream, ttl: u8, peer: SocketAddr, ) -> Result<(), Error> { - conn.set_ttl(ttl.into())?; + if peer.is_ipv4() { + conn.set_ttl(ttl.into())?; + } else { + // TcpStream::set_ttl sets IP_TTL which is IPv4-only. + // Set IPV6_UNICAST_HOPS directly for IPv6 peers. + let val = ttl as u32; + let fd = conn.as_raw_fd(); + let rc = unsafe { + libc::setsockopt( + fd, + IPPROTO_IPV6, + libc::IPV6_UNICAST_HOPS, + &val as *const u32 as *const c_void, + std::mem::size_of::() as u32, + ) + }; + if rc != 0 { + return Err(Error::Io(std::io::Error::last_os_error())); + } + } set_ip_minttl(conn, ttl, peer) } -/// Set the IP_MINTTL socket option (minimum acceptable incoming TTL). +/// Set the minimum acceptable incoming TTL/hop-limit. /// +/// Uses IP_MINTTL for IPv4 and IPV6_MINHOPCOUNT for IPv6. /// A value of 0 disables the check. Only effective on Linux/illumos; /// a no-op on other platforms. #[allow(unused_variables)] @@ -1320,7 +1344,7 @@ fn set_ip_minttl( && libc::setsockopt( fd, IPPROTO_IPV6, - IP_MINTTL, + IPV6_MINHOPCOUNT, &val as *const u32 as *const c_void, std::mem::size_of::() as u32, ) != 0 From effd2801f515dd9f7f306c7bcf02d17da6d543c9 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Thu, 26 Feb 2026 11:27:50 -0700 Subject: [PATCH 24/58] bgp: Use a default TTL for outgoing packets Instead of asking the kernel to reset the TTL back to its own default, let's just manage this explicitly ourselves. This introduces a const BGP_DEFAULT_TTL which is set to 255 -- we are not a 40 year old routing platform that needs to have 100 different nerd knobs to make TTL work in a sane fashion, so let's avoid all the hubbub with making things different for eBGP/iBGP and just set our TTL to 255 from the get-go. --- bgp/src/connection.rs | 5 +- bgp/src/connection_channel.rs | 2 +- bgp/src/connection_tcp.rs | 83 +++++++++++++------ bgp/src/lib.rs | 3 + bgp/src/params.rs | 22 +++-- bgp/src/session.rs | 5 +- bgp/src/test.rs | 3 +- mgadm/src/bgp.rs | 7 +- mgd/src/bgp_admin.rs | 4 +- mgd/src/main.rs | 11 ++- ...2e3fe3.json => mg-admin-7.0.0-4b960d.json} | 8 +- openapi/mg-admin/mg-admin-latest.json | 2 +- 12 files changed, 106 insertions(+), 49 deletions(-) rename openapi/mg-admin/{mg-admin-7.0.0-2e3fe3.json => mg-admin-7.0.0-4b960d.json} (99%) diff --git a/bgp/src/connection.rs b/bgp/src/connection.rs index 91d421b49..6914db28c 100644 --- a/bgp/src/connection.rs +++ b/bgp/src/connection.rs @@ -16,6 +16,7 @@ use slog::Logger; use std::{ collections::BTreeMap, net::{SocketAddr, ToSocketAddrs}, + num::NonZeroU8, sync::{Arc, Mutex, mpsc::Sender}, thread::JoinHandle, time::Duration, @@ -146,7 +147,7 @@ pub trait BgpListener { /// Dispatcher after accept() returns and session lookup is completed. fn apply_policy( conn: &Cnx, - min_ttl: Option, + min_ttl: Option, md5_key: Option, dscp: Dscp, ) -> Result<(), Error>; @@ -225,7 +226,7 @@ pub trait BgpConnection: Send + Sync + Sized { /// A socket option that can be updated on a live BGP connection. pub enum SocketOption { Dscp(Dscp), - MinTtl(Option), + MinTtl(Option), } pub use mg_common::thread::{ManagedThread, ThreadState}; diff --git a/bgp/src/connection_channel.rs b/bgp/src/connection_channel.rs index 352798310..2f725f0a5 100644 --- a/bgp/src/connection_channel.rs +++ b/bgp/src/connection_channel.rs @@ -277,7 +277,7 @@ impl BgpListener for BgpListenerChannel { fn apply_policy( _conn: &BgpConnectionChannel, - _min_ttl: Option, + _min_ttl: Option, _md5_key: Option, _dscp: rdb::Dscp, ) -> Result<(), Error> { diff --git a/bgp/src/connection_tcp.rs b/bgp/src/connection_tcp.rs index 2b1170962..50c85911c 100644 --- a/bgp/src/connection_tcp.rs +++ b/bgp/src/connection_tcp.rs @@ -2,8 +2,9 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. +use std::num::NonZeroU8; use crate::{ - IO_TIMEOUT, + DEFAULT_BGP_TTL, IO_TIMEOUT, clock::ConnectionClock, connection::{ BgpConnection, BgpConnector, BgpListener, ConnectionDirection, @@ -56,6 +57,10 @@ use std::{collections::HashSet, net::IpAddr}; const UNIT_CONNECTION: &str = "connection_tcp"; +/// Value passed to IP_MINTTL / IPV6_MINHOPCOUNT to disable the +/// minimum incoming TTL/hop-limit filter. +const MINTTL_DISABLED: u8 = 0; + #[cfg(target_os = "illumos")] const IP_MINTTL: i32 = 0x1c; #[cfg(target_os = "linux")] @@ -121,6 +126,7 @@ impl BgpListener for BgpListenerTcp { ))?; let listener = TcpListener::bind(addr)?; listener.set_nonblocking(true)?; + set_outgoing_ttl(&listener, DEFAULT_BGP_TTL, addr)?; Ok(Self { listener, unnumbered_manager, @@ -189,15 +195,13 @@ impl BgpListener for BgpListenerTcp { fn apply_policy( conn: &BgpConnectionTcp, - min_ttl: Option, + min_ttl: Option, md5_key: Option, dscp: Dscp, ) -> Result<(), Error> { let tcp_stream = lock!(conn.conn); - if let Some(ttl) = min_ttl { - apply_min_ttl(&tcp_stream, ttl, conn.peer)?; - } + apply_min_ttl(&tcp_stream, min_ttl, conn.peer)?; apply_dscp(&tcp_stream, dscp, conn.peer)?; @@ -335,9 +339,10 @@ impl BgpConnector for BgpConnectorTcp { } }; - // Apply TTL if specified - if let Some(ttl) = config.min_ttl - && let Err(e) = apply_min_ttl(&new_conn, ttl, peer) + // Apply TTL (always set DEFAULT_BGP_TTL unless + // overridden by min_ttl config) + if let Err(e) = + apply_min_ttl(&new_conn, config.min_ttl, peer) { connection_log_lite!(log, warn, @@ -528,7 +533,7 @@ impl BgpConnection for BgpConnectionTcp { match *option { SocketOption::Dscp(dscp) => apply_dscp(&guard, dscp, self.peer), SocketOption::MinTtl(ttl) => { - apply_min_ttl(&guard, ttl.unwrap_or_default(), self.peer) + apply_min_ttl(&guard, ttl, self.peer) } } } @@ -1280,28 +1285,24 @@ fn apply_dscp( Ok(()) } -/// Apply min TTL setting to a TCP connection. +/// Set the outgoing TTL/hop-limit on a socket. /// -/// Sets both the outgoing TTL/hop-limit and the incoming -/// IP_MINTTL / IPV6_MINHOPCOUNT filter to `ttl`. -#[allow(unused_variables)] -fn apply_min_ttl( - conn: &TcpStream, +/// Works on both `TcpStream` and `TcpListener` (anything with +/// `AsRawFd`). Uses `IP_TTL` for IPv4 and `IPV6_UNICAST_HOPS` for +/// IPv6. +fn set_outgoing_ttl( + sock: &impl AsRawFd, ttl: u8, - peer: SocketAddr, + addr: SocketAddr, ) -> Result<(), Error> { - if peer.is_ipv4() { - conn.set_ttl(ttl.into())?; - } else { - // TcpStream::set_ttl sets IP_TTL which is IPv4-only. - // Set IPV6_UNICAST_HOPS directly for IPv6 peers. + if addr.is_ipv4() { let val = ttl as u32; - let fd = conn.as_raw_fd(); + let fd = sock.as_raw_fd(); let rc = unsafe { libc::setsockopt( fd, - IPPROTO_IPV6, - libc::IPV6_UNICAST_HOPS, + IPPROTO_IP, + libc::IP_TTL, &val as *const u32 as *const c_void, std::mem::size_of::() as u32, ) @@ -1309,8 +1310,40 @@ fn apply_min_ttl( if rc != 0 { return Err(Error::Io(std::io::Error::last_os_error())); } + } else { + let val: i32 = ttl.into(); + let fd = sock.as_raw_fd(); + let rc = unsafe { + libc::setsockopt( + fd, + IPPROTO_IPV6, + libc::IPV6_UNICAST_HOPS, + &val as *const i32 as *const c_void, + std::mem::size_of::() as u32, + ) + }; + if rc != 0 { + return Err(Error::Io(std::io::Error::last_os_error())); + } } - set_ip_minttl(conn, ttl, peer) + Ok(()) +} + +/// Apply min TTL setting to a TCP connection. +/// +/// Sets the outgoing TTL/hop-limit to `min_ttl` if configured, or +/// `DEFAULT_BGP_TTL` otherwise. Sets the incoming IP_MINTTL / +/// IPV6_MINHOPCOUNT filter to `min_ttl`, or 0 (disabled) if not +/// configured. +#[allow(unused_variables)] +fn apply_min_ttl( + conn: &TcpStream, + min_ttl: Option, + peer: SocketAddr, +) -> Result<(), Error> { + let ttl = min_ttl.map(NonZeroU8::get); + set_outgoing_ttl(conn, ttl.unwrap_or(DEFAULT_BGP_TTL), peer)?; + set_ip_minttl(conn, ttl.unwrap_or(MINTTL_DISABLED), peer) } /// Set the minimum acceptable incoming TTL/hop-limit. diff --git a/bgp/src/lib.rs b/bgp/src/lib.rs index 095d469f1..70c291668 100644 --- a/bgp/src/lib.rs +++ b/bgp/src/lib.rs @@ -37,6 +37,9 @@ pub mod unnumbered_mock; pub const BGP_PORT: u16 = 179; pub const BGP_VERSION: u8 = 4; +/// Default outgoing TTL/hop-limit for BGP TCP sockets. +pub const DEFAULT_BGP_TTL: u8 = 255; + pub const COMPONENT_BGP: &str = "bgp"; pub const MOD_ROUTER: &str = "router"; pub const MOD_NEIGHBOR: &str = "neighbor"; diff --git a/bgp/src/params.rs b/bgp/src/params.rs index 5f9725c28..f80be9341 100644 --- a/bgp/src/params.rs +++ b/bgp/src/params.rs @@ -16,6 +16,7 @@ use serde::{Deserialize, Serialize}; use std::{ collections::{BTreeMap, HashMap}, net::{IpAddr, SocketAddr, SocketAddrV6}, + num::NonZeroU8, sync::atomic::Ordering, time::Duration, }; @@ -364,7 +365,10 @@ impl UnnumberedNeighbor { act_as_a_default_ipv6_router: rq.router_lifetime, parameters: BgpPeerParameters { remote_asn: rq.parameters.remote_asn, - min_ttl: rq.parameters.min_ttl, + min_ttl: rq + .parameters + .min_ttl + .and_then(NonZeroU8::new), hold_time: rq.parameters.hold_time, idle_hold_time: rq.parameters.idle_hold_time, delay_open: rq.parameters.delay_open, @@ -431,7 +435,10 @@ impl Neighbor { group: rq.group.clone(), parameters: BgpPeerParameters { remote_asn: rq.parameters.remote_asn, - min_ttl: rq.parameters.min_ttl, + min_ttl: rq + .parameters + .min_ttl + .and_then(NonZeroU8::new), hold_time: rq.parameters.hold_time, idle_hold_time: rq.parameters.idle_hold_time, delay_open: rq.parameters.delay_open, @@ -1251,7 +1258,7 @@ pub struct BgpPeerParameters { pub resolution: u64, pub passive: bool, pub remote_asn: Option, - pub min_ttl: Option, + pub min_ttl: Option, pub md5_auth_key: Option, pub multi_exit_discriminator: Option, pub communities: Vec, @@ -1330,7 +1337,10 @@ impl From for BgpPeerConfig { resolution: cfg.parameters.resolution, passive: cfg.parameters.passive, remote_asn: cfg.parameters.remote_asn, - min_ttl: cfg.parameters.min_ttl, + min_ttl: cfg + .parameters + .min_ttl + .and_then(NonZeroU8::new), md5_auth_key: cfg.parameters.md5_auth_key, multi_exit_discriminator: cfg .parameters @@ -1412,7 +1422,7 @@ impl From for BgpPeerParametersV2 { resolution: p.resolution, passive: p.passive, remote_asn: p.remote_asn, - min_ttl: p.min_ttl, + min_ttl: p.min_ttl.map(NonZeroU8::get), md5_auth_key: p.md5_auth_key, multi_exit_discriminator: p.multi_exit_discriminator, communities: p.communities, @@ -1440,7 +1450,7 @@ impl From for BgpPeerParameters { resolution: p.resolution, passive: p.passive, remote_asn: p.remote_asn, - min_ttl: p.min_ttl, + min_ttl: p.min_ttl.and_then(NonZeroU8::new), md5_auth_key: p.md5_auth_key, multi_exit_discriminator: p.multi_exit_discriminator, communities: p.communities, diff --git a/bgp/src/session.rs b/bgp/src/session.rs index 85dc2e035..84bd51027 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -47,6 +47,7 @@ use std::{ collections::{BTreeSet, VecDeque}, fmt::{self, Display, Formatter}, net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}, + num::NonZeroU8, sync::{ Arc, Mutex, RwLock, atomic::{AtomicBool, AtomicU64, Ordering}, @@ -865,7 +866,7 @@ pub struct SessionInfo { /// derived by the system. pub bind_addr: Option, /// Minimum acceptable TTL value for incomming BGP packets. - pub min_ttl: Option, + pub min_ttl: Option, /// Md5 peer authentication key pub md5_auth_key: Option, /// Multi-exit discriminator. This an optional attribute that is intended to @@ -993,7 +994,7 @@ impl From<&BgpPeerParametersV1> for SessionInfo { SessionInfo { passive_tcp_establishment: value.passive, remote_asn: value.remote_asn, - min_ttl: value.min_ttl, + min_ttl: value.min_ttl.and_then(NonZeroU8::new), md5_auth_key: value.md5_auth_key.clone(), multi_exit_discriminator: value.multi_exit_discriminator, communities: value.communities.clone().into_iter().collect(), diff --git a/bgp/src/test.rs b/bgp/src/test.rs index 29194532a..cfb9d364a 100644 --- a/bgp/src/test.rs +++ b/bgp/src/test.rs @@ -28,6 +28,7 @@ use rdb::{ use std::{ collections::{BTreeMap, BTreeSet}, net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV6}, + num::NonZeroU8, sync::{ Arc, Mutex, atomic::{AtomicU32, Ordering}, @@ -5272,7 +5273,7 @@ fn test_inflight_socket_option_updates() { assert_no_reset("setting DSCP"); // ===== Set min_ttl (no reset) ===== - min_ttl = Some(1); + min_ttl = Some(NonZeroU8::new(1).unwrap()); let mut info = SessionInfo::from_peer_config(&r2_peer_config); info.dscp = dscp; info.min_ttl = min_ttl; diff --git a/mgadm/src/bgp.rs b/mgadm/src/bgp.rs index fc1bfa495..4cb4c772e 100644 --- a/mgadm/src/bgp.rs +++ b/mgadm/src/bgp.rs @@ -20,6 +20,7 @@ use std::{ fs::read_to_string, io::{Write, stdout}, net::{IpAddr, Ipv4Addr, SocketAddr}, + num::NonZeroU8, time::Duration, }; use tabwriter::TabWriter; @@ -637,9 +638,9 @@ pub struct Neighbor { #[arg(long)] pub remote_asn: Option, - /// Minimum acceptable TTL for neighbor. - #[arg(long)] - pub min_ttl: Option, + /// Minimum acceptable TTL for neighbor (1-255). + #[arg(long, value_parser = clap::value_parser!(NonZeroU8))] + pub min_ttl: Option, /// Authentication key used for TCP-MD5 with remote peer. #[arg(long)] diff --git a/mgd/src/bgp_admin.rs b/mgd/src/bgp_admin.rs index 8681194c3..bc35ae734 100644 --- a/mgd/src/bgp_admin.rs +++ b/mgd/src/bgp_admin.rs @@ -2383,7 +2383,7 @@ pub(crate) mod helpers { host: rq.host, parameters: BgpNeighborParameters { remote_asn: rq.parameters.remote_asn, - min_ttl: rq.parameters.min_ttl, + min_ttl: rq.parameters.min_ttl.map(|v| v.get()), hold_time: rq.parameters.hold_time, idle_hold_time: rq.parameters.idle_hold_time, delay_open: rq.parameters.delay_open, @@ -2507,7 +2507,7 @@ pub(crate) mod helpers { router_lifetime: rq.act_as_a_default_ipv6_router, parameters: BgpNeighborParameters { remote_asn: rq.parameters.remote_asn, - min_ttl: rq.parameters.min_ttl, + min_ttl: rq.parameters.min_ttl.map(|v| v.get()), hold_time: rq.parameters.hold_time, idle_hold_time: rq.parameters.idle_hold_time, delay_open: rq.parameters.delay_open, diff --git a/mgd/src/main.rs b/mgd/src/main.rs index 695dd26eb..1ed25a35a 100644 --- a/mgd/src/main.rs +++ b/mgd/src/main.rs @@ -21,6 +21,7 @@ use signal::handle_signals; use slog::Logger; use std::collections::{BTreeMap, BTreeSet}; use std::net::{IpAddr, Ipv6Addr, SocketAddr}; +use std::num::NonZeroU8; use std::sync::{Arc, Mutex}; use std::thread::Builder; use uuid::Uuid; @@ -324,7 +325,10 @@ fn start_bgp_routers( host: nbr.host, parameters: BgpPeerParameters { remote_asn: nbr.parameters.remote_asn, - min_ttl: nbr.parameters.min_ttl, + min_ttl: nbr + .parameters + .min_ttl + .and_then(NonZeroU8::new), hold_time: nbr.parameters.hold_time, idle_hold_time: nbr.parameters.idle_hold_time, delay_open: nbr.parameters.delay_open, @@ -380,7 +384,10 @@ fn start_bgp_routers( act_as_a_default_ipv6_router: nbr.router_lifetime, parameters: BgpPeerParameters { remote_asn: nbr.parameters.remote_asn, - min_ttl: nbr.parameters.min_ttl, + min_ttl: nbr + .parameters + .min_ttl + .and_then(NonZeroU8::new), hold_time: nbr.parameters.hold_time, idle_hold_time: nbr.parameters.idle_hold_time, delay_open: nbr.parameters.delay_open, diff --git a/openapi/mg-admin/mg-admin-7.0.0-2e3fe3.json b/openapi/mg-admin/mg-admin-7.0.0-4b960d.json similarity index 99% rename from openapi/mg-admin/mg-admin-7.0.0-2e3fe3.json rename to openapi/mg-admin/mg-admin-7.0.0-4b960d.json index af541a99e..07cb54e8b 100644 --- a/openapi/mg-admin/mg-admin-7.0.0-2e3fe3.json +++ b/openapi/mg-admin/mg-admin-7.0.0-4b960d.json @@ -2289,7 +2289,7 @@ "nullable": true, "type": "integer", "format": "uint8", - "minimum": 0 + "minimum": 1 }, "multi_exit_discriminator": { "nullable": true, @@ -4059,7 +4059,7 @@ "nullable": true, "type": "integer", "format": "uint8", - "minimum": 0 + "minimum": 1 }, "multi_exit_discriminator": { "nullable": true, @@ -5607,7 +5607,7 @@ "nullable": true, "type": "integer", "format": "uint8", - "minimum": 0 + "minimum": 1 }, "multi_exit_discriminator": { "nullable": true, @@ -5778,7 +5778,7 @@ "nullable": true, "type": "integer", "format": "uint8", - "minimum": 0 + "minimum": 1 }, "multi_exit_discriminator": { "nullable": true, diff --git a/openapi/mg-admin/mg-admin-latest.json b/openapi/mg-admin/mg-admin-latest.json index f6f339833..3dad0b7be 120000 --- a/openapi/mg-admin/mg-admin-latest.json +++ b/openapi/mg-admin/mg-admin-latest.json @@ -1 +1 @@ -mg-admin-7.0.0-2e3fe3.json \ No newline at end of file +mg-admin-7.0.0-4b960d.json \ No newline at end of file From 053af1a146106b980e00e18ee7e5c9517b317033 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Fri, 27 Feb 2026 07:17:26 -0700 Subject: [PATCH 25/58] cargo fmt gooder --- bgp/src/connection_tcp.rs | 6 ++---- bgp/src/params.rs | 15 +++------------ mgd/src/main.rs | 10 ++-------- 3 files changed, 7 insertions(+), 24 deletions(-) diff --git a/bgp/src/connection_tcp.rs b/bgp/src/connection_tcp.rs index 50c85911c..6df9b8d47 100644 --- a/bgp/src/connection_tcp.rs +++ b/bgp/src/connection_tcp.rs @@ -2,7 +2,6 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use std::num::NonZeroU8; use crate::{ DEFAULT_BGP_TTL, IO_TIMEOUT, clock::ConnectionClock, @@ -35,6 +34,7 @@ use std::{ io::Read, io::Write, net::{SocketAddr, TcpListener, TcpStream, ToSocketAddrs}, + num::NonZeroU8, os::fd::AsRawFd, sync::atomic::AtomicBool, sync::{Arc, Mutex, atomic::Ordering, mpsc::Sender}, @@ -532,9 +532,7 @@ impl BgpConnection for BgpConnectionTcp { let guard = lock!(self.conn); match *option { SocketOption::Dscp(dscp) => apply_dscp(&guard, dscp, self.peer), - SocketOption::MinTtl(ttl) => { - apply_min_ttl(&guard, ttl, self.peer) - } + SocketOption::MinTtl(ttl) => apply_min_ttl(&guard, ttl, self.peer), } } } diff --git a/bgp/src/params.rs b/bgp/src/params.rs index f80be9341..3ed3f8f48 100644 --- a/bgp/src/params.rs +++ b/bgp/src/params.rs @@ -365,10 +365,7 @@ impl UnnumberedNeighbor { act_as_a_default_ipv6_router: rq.router_lifetime, parameters: BgpPeerParameters { remote_asn: rq.parameters.remote_asn, - min_ttl: rq - .parameters - .min_ttl - .and_then(NonZeroU8::new), + min_ttl: rq.parameters.min_ttl.and_then(NonZeroU8::new), hold_time: rq.parameters.hold_time, idle_hold_time: rq.parameters.idle_hold_time, delay_open: rq.parameters.delay_open, @@ -435,10 +432,7 @@ impl Neighbor { group: rq.group.clone(), parameters: BgpPeerParameters { remote_asn: rq.parameters.remote_asn, - min_ttl: rq - .parameters - .min_ttl - .and_then(NonZeroU8::new), + min_ttl: rq.parameters.min_ttl.and_then(NonZeroU8::new), hold_time: rq.parameters.hold_time, idle_hold_time: rq.parameters.idle_hold_time, delay_open: rq.parameters.delay_open, @@ -1337,10 +1331,7 @@ impl From for BgpPeerConfig { resolution: cfg.parameters.resolution, passive: cfg.parameters.passive, remote_asn: cfg.parameters.remote_asn, - min_ttl: cfg - .parameters - .min_ttl - .and_then(NonZeroU8::new), + min_ttl: cfg.parameters.min_ttl.and_then(NonZeroU8::new), md5_auth_key: cfg.parameters.md5_auth_key, multi_exit_discriminator: cfg .parameters diff --git a/mgd/src/main.rs b/mgd/src/main.rs index 1ed25a35a..e8e5322d8 100644 --- a/mgd/src/main.rs +++ b/mgd/src/main.rs @@ -325,10 +325,7 @@ fn start_bgp_routers( host: nbr.host, parameters: BgpPeerParameters { remote_asn: nbr.parameters.remote_asn, - min_ttl: nbr - .parameters - .min_ttl - .and_then(NonZeroU8::new), + min_ttl: nbr.parameters.min_ttl.and_then(NonZeroU8::new), hold_time: nbr.parameters.hold_time, idle_hold_time: nbr.parameters.idle_hold_time, delay_open: nbr.parameters.delay_open, @@ -384,10 +381,7 @@ fn start_bgp_routers( act_as_a_default_ipv6_router: nbr.router_lifetime, parameters: BgpPeerParameters { remote_asn: nbr.parameters.remote_asn, - min_ttl: nbr - .parameters - .min_ttl - .and_then(NonZeroU8::new), + min_ttl: nbr.parameters.min_ttl.and_then(NonZeroU8::new), hold_time: nbr.parameters.hold_time, idle_hold_time: nbr.parameters.idle_hold_time, delay_open: nbr.parameters.delay_open, From 542ef512ad6c985f9ece25f5ff28c8b195bf80db Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Fri, 27 Feb 2026 08:01:43 -0700 Subject: [PATCH 26/58] un-flatten BgpParameters Stop flattening BgpParameters in Neighbor/UnnumberedNeighbor structs. This allows client to de-duplicate all the common parameters and only implement logic for it once. Fixes: #635 --- bgp/src/params.rs | 4 - falcon-lab/src/bgp.rs | 64 +-- mgadm/src/bgp.rs | 104 ++-- ...4b960d.json => mg-admin-7.0.0-8295c4.json} | 444 ++---------------- openapi/mg-admin/mg-admin-latest.json | 2 +- 5 files changed, 118 insertions(+), 500 deletions(-) rename openapi/mg-admin/{mg-admin-7.0.0-4b960d.json => mg-admin-7.0.0-8295c4.json} (92%) diff --git a/bgp/src/params.rs b/bgp/src/params.rs index 3ed3f8f48..7a24c06d2 100644 --- a/bgp/src/params.rs +++ b/bgp/src/params.rs @@ -199,7 +199,6 @@ pub struct Neighbor { pub name: String, pub group: String, pub host: SocketAddr, - #[serde(flatten)] pub parameters: BgpPeerParameters, } @@ -235,7 +234,6 @@ pub struct UnnumberedNeighbor { pub group: String, pub interface: String, pub act_as_a_default_ipv6_router: u16, - #[serde(flatten)] pub parameters: BgpPeerParameters, } @@ -1229,7 +1227,6 @@ pub struct BgpPeerConfigV1 { pub struct BgpPeerConfig { pub host: SocketAddr, pub name: String, - #[serde(flatten)] pub parameters: BgpPeerParameters, } @@ -1238,7 +1235,6 @@ pub struct UnnumberedBgpPeerConfig { pub interface: String, pub name: String, pub router_lifetime: u16, - #[serde(flatten)] pub parameters: BgpPeerParameters, } diff --git a/falcon-lab/src/bgp.rs b/falcon-lab/src/bgp.rs index d71841f40..e63109c12 100644 --- a/falcon-lab/src/bgp.rs +++ b/falcon-lab/src/bgp.rs @@ -1,8 +1,8 @@ //! BGP utilities use mg_admin_client::types::{ - ImportExportPolicy4, ImportExportPolicy6, Ipv4UnicastConfig, - Ipv6UnicastConfig, UnnumberedNeighbor, + BgpPeerParameters, ImportExportPolicy4, ImportExportPolicy6, + Ipv4UnicastConfig, Ipv6UnicastConfig, UnnumberedNeighbor, }; use rdb_types::Dscp; @@ -16,37 +16,39 @@ pub fn basic_unnumbered_neighbor( UnnumberedNeighbor { asn: local_asn, act_as_a_default_ipv6_router, - communities: Vec::default(), - connect_retry: 5, - delay_open: 0, - enforce_first_as: false, group: group.to_owned(), - hold_time: 6, - idle_hold_time: 0, interface: interface.to_string(), - keepalive: 2, - local_pref: None, - md5_auth_key: None, - min_ttl: None, - multi_exit_discriminator: None, name: name.to_string(), - passive: false, - remote_asn: None, - resolution: 100, - vlan_id: None, - ipv4_unicast: Some(Ipv4UnicastConfig { - import_policy: ImportExportPolicy4::NoFiltering, - export_policy: ImportExportPolicy4::NoFiltering, - nexthop: None, - }), - ipv6_unicast: Some(Ipv6UnicastConfig { - import_policy: ImportExportPolicy6::NoFiltering, - export_policy: ImportExportPolicy6::NoFiltering, - nexthop: None, - }), - connect_retry_jitter: None, - deterministic_collision_resolution: false, - idle_hold_jitter: None, - dscp: Dscp::default(), + parameters: BgpPeerParameters { + communities: Vec::default(), + connect_retry: 5, + delay_open: 0, + enforce_first_as: false, + hold_time: 6, + idle_hold_time: 0, + keepalive: 2, + local_pref: None, + md5_auth_key: None, + min_ttl: None, + multi_exit_discriminator: None, + passive: false, + remote_asn: None, + resolution: 100, + vlan_id: None, + ipv4_unicast: Some(Ipv4UnicastConfig { + import_policy: ImportExportPolicy4::NoFiltering, + export_policy: ImportExportPolicy4::NoFiltering, + nexthop: None, + }), + ipv6_unicast: Some(Ipv6UnicastConfig { + import_policy: ImportExportPolicy6::NoFiltering, + export_policy: ImportExportPolicy6::NoFiltering, + nexthop: None, + }), + connect_retry_jitter: None, + deterministic_collision_resolution: false, + idle_hold_jitter: None, + dscp: Dscp::default(), + }, } } diff --git a/mgadm/src/bgp.rs b/mgadm/src/bgp.rs index 4cb4c772e..953be600d 100644 --- a/mgadm/src/bgp.rs +++ b/mgadm/src/bgp.rs @@ -786,33 +786,37 @@ impl Neighbor { types::Neighbor { asn: self.asn, - remote_asn: self.remote_asn, - min_ttl: self.min_ttl, name: self.name.clone(), host: SocketAddr::new(addr, self.port).to_string(), - hold_time: self.hold_time, - idle_hold_time: self.idle_hold_time, - connect_retry: self.connect_retry_time, - keepalive: self.keepalive_time, - delay_open: self.delay_open_time, - resolution: self.clock_resolution, group: self.group.clone(), - passive: self.passive_connection, - md5_auth_key: self.md5_auth_key.clone(), - multi_exit_discriminator: self.med, - communities: self.communities.clone(), - local_pref: self.local_pref, - enforce_first_as: self.enforce_first_as, - ipv4_unicast, - ipv6_unicast, - vlan_id: self.vlan_id, - connect_retry_jitter: self - .connect_retry_jitter - .map(jitter_range_to_api), - idle_hold_jitter: self.idle_hold_jitter.map(jitter_range_to_api), - deterministic_collision_resolution: self - .deterministic_collision_resolution, - dscp: self.dscp.unwrap_or_default(), + parameters: types::BgpPeerParameters { + remote_asn: self.remote_asn, + min_ttl: self.min_ttl, + hold_time: self.hold_time, + idle_hold_time: self.idle_hold_time, + connect_retry: self.connect_retry_time, + keepalive: self.keepalive_time, + delay_open: self.delay_open_time, + resolution: self.clock_resolution, + passive: self.passive_connection, + md5_auth_key: self.md5_auth_key.clone(), + multi_exit_discriminator: self.med, + communities: self.communities.clone(), + local_pref: self.local_pref, + enforce_first_as: self.enforce_first_as, + ipv4_unicast, + ipv6_unicast, + vlan_id: self.vlan_id, + connect_retry_jitter: self + .connect_retry_jitter + .map(jitter_range_to_api), + idle_hold_jitter: self + .idle_hold_jitter + .map(jitter_range_to_api), + deterministic_collision_resolution: self + .deterministic_collision_resolution, + dscp: self.dscp.unwrap_or_default(), + }, } } @@ -865,8 +869,6 @@ impl Neighbor { types::UnnumberedNeighbor { asn: self.asn, - remote_asn: self.remote_asn, - min_ttl: self.min_ttl, act_as_a_default_ipv6_router: if self.act_as_default_router { 1800 } else { @@ -874,29 +876,35 @@ impl Neighbor { }, name: self.name.clone(), interface, - hold_time: self.hold_time, - idle_hold_time: self.idle_hold_time, - connect_retry: self.connect_retry_time, - keepalive: self.keepalive_time, - delay_open: self.delay_open_time, - resolution: self.clock_resolution, group: self.group.clone(), - passive: self.passive_connection, - md5_auth_key: self.md5_auth_key.clone(), - multi_exit_discriminator: self.med, - communities: self.communities.clone(), - local_pref: self.local_pref, - enforce_first_as: self.enforce_first_as, - ipv4_unicast, - ipv6_unicast, - vlan_id: self.vlan_id, - connect_retry_jitter: self - .connect_retry_jitter - .map(jitter_range_to_api), - idle_hold_jitter: self.idle_hold_jitter.map(jitter_range_to_api), - deterministic_collision_resolution: self - .deterministic_collision_resolution, - dscp: self.dscp.unwrap_or_default(), + parameters: types::BgpPeerParameters { + remote_asn: self.remote_asn, + min_ttl: self.min_ttl, + hold_time: self.hold_time, + idle_hold_time: self.idle_hold_time, + connect_retry: self.connect_retry_time, + keepalive: self.keepalive_time, + delay_open: self.delay_open_time, + resolution: self.clock_resolution, + passive: self.passive_connection, + md5_auth_key: self.md5_auth_key.clone(), + multi_exit_discriminator: self.med, + communities: self.communities.clone(), + local_pref: self.local_pref, + enforce_first_as: self.enforce_first_as, + ipv4_unicast, + ipv6_unicast, + vlan_id: self.vlan_id, + connect_retry_jitter: self + .connect_retry_jitter + .map(jitter_range_to_api), + idle_hold_jitter: self + .idle_hold_jitter + .map(jitter_range_to_api), + deterministic_collision_resolution: self + .deterministic_collision_resolution, + dscp: self.dscp.unwrap_or_default(), + }, } } } diff --git a/openapi/mg-admin/mg-admin-7.0.0-4b960d.json b/openapi/mg-admin/mg-admin-7.0.0-8295c4.json similarity index 92% rename from openapi/mg-admin/mg-admin-7.0.0-4b960d.json rename to openapi/mg-admin/mg-admin-7.0.0-8295c4.json index 07cb54e8b..879b21dea 100644 --- a/openapi/mg-admin/mg-admin-7.0.0-4b960d.json +++ b/openapi/mg-admin/mg-admin-7.0.0-8295c4.json @@ -2185,6 +2185,25 @@ ] }, "BgpPeerConfig": { + "type": "object", + "properties": { + "host": { + "type": "string" + }, + "name": { + "type": "string" + }, + "parameters": { + "$ref": "#/components/schemas/BgpPeerParameters" + } + }, + "required": [ + "host", + "name", + "parameters" + ] + }, + "BgpPeerParameters": { "type": "object", "properties": { "communities": { @@ -2235,9 +2254,6 @@ "format": "uint64", "minimum": 0 }, - "host": { - "type": "string" - }, "idle_hold_jitter": { "nullable": true, "description": "Jitter range for idle hold timer. When used, the idle hold timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", @@ -2297,9 +2313,6 @@ "format": "uint32", "minimum": 0 }, - "name": { - "type": "string" - }, "passive": { "type": "boolean" }, @@ -2328,10 +2341,8 @@ "deterministic_collision_resolution", "enforce_first_as", "hold_time", - "host", "idle_hold_time", "keepalive", - "name", "passive", "resolution" ] @@ -3954,158 +3965,25 @@ "format": "uint32", "minimum": 0 }, - "communities": { - "type": "array", - "items": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - "connect_retry": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "connect_retry_jitter": { - "nullable": true, - "description": "Jitter range for connect_retry timer. When used, the connect_retry timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", - "allOf": [ - { - "$ref": "#/components/schemas/JitterRange" - } - ] - }, - "delay_open": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "deterministic_collision_resolution": { - "description": "Enable deterministic collision resolution in Established state. When true, uses BGP-ID comparison per RFC 4271 §6.8 for collision resolution even when one connection is already in Established state. When false, Established connection always wins (timing-based resolution).", - "type": "boolean" - }, - "dscp": { - "description": "DSCP value for BGP TCP connections (0-63). RFC 4271 Appendix E recommends CS6 (48) for BGP traffic. Default: CS6 (48).", - "default": 48, - "allOf": [ - { - "$ref": "#/components/schemas/Dscp" - } - ] - }, - "enforce_first_as": { - "type": "boolean" - }, "group": { "type": "string" }, - "hold_time": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, "host": { "type": "string" }, - "idle_hold_jitter": { - "nullable": true, - "description": "Jitter range for idle hold timer. When used, the idle hold timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", - "allOf": [ - { - "$ref": "#/components/schemas/JitterRange" - } - ] - }, - "idle_hold_time": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "ipv4_unicast": { - "nullable": true, - "description": "IPv4 Unicast address family configuration (None = disabled)", - "allOf": [ - { - "$ref": "#/components/schemas/Ipv4UnicastConfig" - } - ] - }, - "ipv6_unicast": { - "nullable": true, - "description": "IPv6 Unicast address family configuration (None = disabled)", - "allOf": [ - { - "$ref": "#/components/schemas/Ipv6UnicastConfig" - } - ] - }, - "keepalive": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "local_pref": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "md5_auth_key": { - "nullable": true, - "type": "string" - }, - "min_ttl": { - "nullable": true, - "type": "integer", - "format": "uint8", - "minimum": 1 - }, - "multi_exit_discriminator": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, "name": { "type": "string" }, - "passive": { - "type": "boolean" - }, - "remote_asn": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "resolution": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "vlan_id": { - "nullable": true, - "type": "integer", - "format": "uint16", - "minimum": 0 + "parameters": { + "$ref": "#/components/schemas/BgpPeerParameters" } }, "required": [ "asn", - "communities", - "connect_retry", - "delay_open", - "deterministic_collision_resolution", - "enforce_first_as", "group", - "hold_time", "host", - "idle_hold_time", - "keepalive", "name", - "passive", - "resolution" + "parameters" ] }, "NeighborResetOp": { @@ -5505,158 +5383,25 @@ "UnnumberedBgpPeerConfig": { "type": "object", "properties": { - "communities": { - "type": "array", - "items": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - "connect_retry": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "connect_retry_jitter": { - "nullable": true, - "description": "Jitter range for connect_retry timer. When used, the connect_retry timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", - "allOf": [ - { - "$ref": "#/components/schemas/JitterRange" - } - ] - }, - "delay_open": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "deterministic_collision_resolution": { - "description": "Enable deterministic collision resolution in Established state. When true, uses BGP-ID comparison per RFC 4271 §6.8 for collision resolution even when one connection is already in Established state. When false, Established connection always wins (timing-based resolution).", - "type": "boolean" - }, - "dscp": { - "description": "DSCP value for BGP TCP connections (0-63). RFC 4271 Appendix E recommends CS6 (48) for BGP traffic. Default: CS6 (48).", - "default": 48, - "allOf": [ - { - "$ref": "#/components/schemas/Dscp" - } - ] - }, - "enforce_first_as": { - "type": "boolean" - }, - "hold_time": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "idle_hold_jitter": { - "nullable": true, - "description": "Jitter range for idle hold timer. When used, the idle hold timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", - "allOf": [ - { - "$ref": "#/components/schemas/JitterRange" - } - ] - }, - "idle_hold_time": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, "interface": { "type": "string" }, - "ipv4_unicast": { - "nullable": true, - "description": "IPv4 Unicast address family configuration (None = disabled)", - "allOf": [ - { - "$ref": "#/components/schemas/Ipv4UnicastConfig" - } - ] - }, - "ipv6_unicast": { - "nullable": true, - "description": "IPv6 Unicast address family configuration (None = disabled)", - "allOf": [ - { - "$ref": "#/components/schemas/Ipv6UnicastConfig" - } - ] - }, - "keepalive": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "local_pref": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "md5_auth_key": { - "nullable": true, - "type": "string" - }, - "min_ttl": { - "nullable": true, - "type": "integer", - "format": "uint8", - "minimum": 1 - }, - "multi_exit_discriminator": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, "name": { "type": "string" }, - "passive": { - "type": "boolean" - }, - "remote_asn": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "resolution": { - "type": "integer", - "format": "uint64", - "minimum": 0 + "parameters": { + "$ref": "#/components/schemas/BgpPeerParameters" }, "router_lifetime": { "type": "integer", "format": "uint16", "minimum": 0 - }, - "vlan_id": { - "nullable": true, - "type": "integer", - "format": "uint16", - "minimum": 0 } }, "required": [ - "communities", - "connect_retry", - "delay_open", - "deterministic_collision_resolution", - "enforce_first_as", - "hold_time", - "idle_hold_time", "interface", - "keepalive", "name", - "passive", - "resolution", + "parameters", "router_lifetime" ] }, @@ -5673,159 +5418,26 @@ "format": "uint32", "minimum": 0 }, - "communities": { - "type": "array", - "items": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - "connect_retry": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "connect_retry_jitter": { - "nullable": true, - "description": "Jitter range for connect_retry timer. When used, the connect_retry timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", - "allOf": [ - { - "$ref": "#/components/schemas/JitterRange" - } - ] - }, - "delay_open": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "deterministic_collision_resolution": { - "description": "Enable deterministic collision resolution in Established state. When true, uses BGP-ID comparison per RFC 4271 §6.8 for collision resolution even when one connection is already in Established state. When false, Established connection always wins (timing-based resolution).", - "type": "boolean" - }, - "dscp": { - "description": "DSCP value for BGP TCP connections (0-63). RFC 4271 Appendix E recommends CS6 (48) for BGP traffic. Default: CS6 (48).", - "default": 48, - "allOf": [ - { - "$ref": "#/components/schemas/Dscp" - } - ] - }, - "enforce_first_as": { - "type": "boolean" - }, "group": { "type": "string" }, - "hold_time": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "idle_hold_jitter": { - "nullable": true, - "description": "Jitter range for idle hold timer. When used, the idle hold timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", - "allOf": [ - { - "$ref": "#/components/schemas/JitterRange" - } - ] - }, - "idle_hold_time": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, "interface": { "type": "string" }, - "ipv4_unicast": { - "nullable": true, - "description": "IPv4 Unicast address family configuration (None = disabled)", - "allOf": [ - { - "$ref": "#/components/schemas/Ipv4UnicastConfig" - } - ] - }, - "ipv6_unicast": { - "nullable": true, - "description": "IPv6 Unicast address family configuration (None = disabled)", - "allOf": [ - { - "$ref": "#/components/schemas/Ipv6UnicastConfig" - } - ] - }, - "keepalive": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "local_pref": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "md5_auth_key": { - "nullable": true, - "type": "string" - }, - "min_ttl": { - "nullable": true, - "type": "integer", - "format": "uint8", - "minimum": 1 - }, - "multi_exit_discriminator": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, "name": { "type": "string" }, - "passive": { - "type": "boolean" - }, - "remote_asn": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "resolution": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "vlan_id": { - "nullable": true, - "type": "integer", - "format": "uint16", - "minimum": 0 + "parameters": { + "$ref": "#/components/schemas/BgpPeerParameters" } }, "required": [ "act_as_a_default_ipv6_router", "asn", - "communities", - "connect_retry", - "delay_open", - "deterministic_collision_resolution", - "enforce_first_as", "group", - "hold_time", - "idle_hold_time", "interface", - "keepalive", "name", - "passive", - "resolution" + "parameters" ] }, "UnnumberedNeighborResetRequest": { diff --git a/openapi/mg-admin/mg-admin-latest.json b/openapi/mg-admin/mg-admin-latest.json index 3dad0b7be..8f77d1329 120000 --- a/openapi/mg-admin/mg-admin-latest.json +++ b/openapi/mg-admin/mg-admin-latest.json @@ -1 +1 @@ -mg-admin-7.0.0-4b960d.json \ No newline at end of file +mg-admin-7.0.0-8295c4.json \ No newline at end of file From ed2247de96c2caba8bd86314e57c1fff5e2dc3ca Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Fri, 27 Feb 2026 08:40:08 -0700 Subject: [PATCH 27/58] rdb: implement full RFC 4271 bestpath tie-breaking Extend BgpPathProperties with origin, peer_ip, and internal fields and rewrite the bestpath algorithm to follow the complete RFC 4271 Section 9.1.2.2 decision process: origin preference, per-AS MED, eBGP over iBGP, multipath decision point, router-id, and peer-ip tie-breaking. Move PathOrigin from bgp::messages to rdb-types so it can be shared across the routing stack. Add BgpPathPropertiesV2/PathV2 for API backward compatibility and new v3 RIB endpoints that expose the additional fields. Add comprehensive bestpath tests covering each tie-breaking step individually and a full-pipeline integration test. Fixes: #527 --- Cargo.lock | 1 + bgp/src/messages.rs | 45 +- bgp/src/session.rs | 9 + falcon-lab/src/test.rs | 8 +- mg-api/src/lib.rs | 49 +- mgadm/src/rib.rs | 4 +- mgd/src/admin.rs | 18 +- mgd/src/rib_admin.rs | 29 +- ...8295c4.json => mg-admin-7.0.0-ec3815.json} | 21 +- openapi/mg-admin/mg-admin-latest.json | 2 +- rdb-types/Cargo.toml | 1 + rdb-types/src/lib.rs | 45 ++ rdb/src/bestpath.rs | 519 ++++++++++++++++-- rdb/src/db.rs | 39 +- rdb/src/types.rs | 89 ++- 15 files changed, 780 insertions(+), 99 deletions(-) rename openapi/mg-admin/{mg-admin-7.0.0-8295c4.json => mg-admin-7.0.0-ec3815.json} (99%) diff --git a/Cargo.lock b/Cargo.lock index a1948c790..0e43a4c7c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5386,6 +5386,7 @@ name = "rdb-types" version = "0.1.0" dependencies = [ "clap", + "num_enum 0.7.5", "oxnet", "schemars 0.8.22", "serde", diff --git a/bgp/src/messages.rs b/bgp/src/messages.rs index 69decfdd4..5727dbefd 100644 --- a/bgp/src/messages.rs +++ b/bgp/src/messages.rs @@ -10,8 +10,8 @@ use nom::{ use num_enum::FromPrimitive; use num_enum::{IntoPrimitive, TryFromPrimitive}; use path_attribute_flags::*; -pub use rdb::types::Prefix; use rdb::types::{AddressFamily, Prefix4, Prefix6}; +pub use rdb::types::{PathOrigin, Prefix}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use std::{ @@ -1758,6 +1758,15 @@ impl UpdateMessage { .map(|p| p.iter().fold(0, |a, b| a + b.value.len())) } + pub fn origin(&self) -> PathOrigin { + for a in &self.path_attributes { + if let PathAttributeValue::Origin(origin) = &a.value { + return *origin; + } + } + PathOrigin::Incomplete + } + pub fn has_community(&self, community: Community) -> bool { for a in &self.path_attributes { if let PathAttributeValue::Communities(communities) = &a.value { @@ -2733,40 +2742,6 @@ pub enum Community { UserDefined(u32), } -/// An enumeration indicating the origin type of a path. -#[derive( - Clone, - Copy, - Debug, - Deserialize, - Eq, - IntoPrimitive, - JsonSchema, - PartialEq, - Serialize, - TryFromPrimitive, -)] -#[repr(u8)] -#[serde(rename_all = "snake_case")] -pub enum PathOrigin { - /// Interior gateway protocol - Igp = 0, - /// Exterior gateway protocol - Egp = 1, - /// Incomplete path origin - Incomplete = 2, -} - -impl Display for PathOrigin { - fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { - match self { - PathOrigin::Igp => write!(f, "igp"), - PathOrigin::Egp => write!(f, "egp"), - PathOrigin::Incomplete => write!(f, "incomplete"), - } - } -} - // A self describing segment found in path sets and sequences. #[derive(Debug, PartialEq, Eq, Clone)] pub struct AsPathSegment { diff --git a/bgp/src/session.rs b/bgp/src/session.rs index 84bd51027..abda1a6ac 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -9003,8 +9003,11 @@ impl SessionRunner { shutdown: update.graceful_shutdown(), rib_priority: DEFAULT_RIB_PRIORITY_BGP, bgp: Some(BgpPathProperties { + origin: update.origin(), origin_as: pc.asn, peer: self.peer_id(), + peer_ip: pc.conn.peer().ip(), + internal: self.is_ibgp().unwrap_or(false), id: pc.id, med: update.multi_exit_discriminator(), local_pref: update.local_pref(), @@ -9118,8 +9121,11 @@ impl SessionRunner { shutdown: update.graceful_shutdown(), rib_priority: DEFAULT_RIB_PRIORITY_BGP, bgp: Some(BgpPathProperties { + origin: update.origin(), origin_as: pc.asn, peer: self.peer_id(), + peer_ip: pc.conn.peer().ip(), + internal: self.is_ibgp().unwrap_or(false), id: pc.id, med: update.multi_exit_discriminator(), local_pref: update.local_pref(), @@ -9231,8 +9237,11 @@ impl SessionRunner { shutdown: update.graceful_shutdown(), rib_priority: DEFAULT_RIB_PRIORITY_BGP, bgp: Some(BgpPathProperties { + origin: update.origin(), origin_as: pc.asn, peer: self.peer_id(), + peer_ip: pc.conn.peer().ip(), + internal: self.is_ibgp().unwrap_or(false), id: pc.id, med: update.multi_exit_discriminator(), local_pref: update.local_pref(), diff --git a/falcon-lab/src/test.rs b/falcon-lab/src/test.rs index 161f9cd39..3ebfd9bf6 100644 --- a/falcon-lab/src/test.rs +++ b/falcon-lab/src/test.rs @@ -157,7 +157,7 @@ pub async fn run_trio_unnumbered_test( ); wait_for_eq!( - mgd.get_rib_imported_v2(Some(&AddressFamily::Ipv4), None) + mgd.get_rib_imported_v3(Some(&AddressFamily::Ipv4), None) .await .map(|x| x.len()) .unwrap_or(0), @@ -166,7 +166,7 @@ pub async fn run_trio_unnumbered_test( ); wait_for_eq!( - mgd.get_rib_imported_v2(Some(&AddressFamily::Ipv4), None) + mgd.get_rib_imported_v3(Some(&AddressFamily::Ipv4), None) .await .map(|x| x.values().nth(0).map(|x| x.len())) .unwrap_or(None), @@ -184,7 +184,7 @@ pub async fn run_trio_unnumbered_test( ); wait_for_eq!( - mgd.get_rib_imported_v2(Some(&AddressFamily::Ipv6), None) + mgd.get_rib_imported_v3(Some(&AddressFamily::Ipv6), None) .await .map(|x| x.len()) .unwrap_or(0), @@ -193,7 +193,7 @@ pub async fn run_trio_unnumbered_test( ); wait_for_eq!( - mgd.get_rib_imported_v2(Some(&AddressFamily::Ipv6), None) + mgd.get_rib_imported_v3(Some(&AddressFamily::Ipv6), None) .await .map(|x| x.values().nth(0).map(|x| x.len())) .unwrap_or(None), diff --git a/mg-api/src/lib.rs b/mg-api/src/lib.rs index 59d440db7..54f0bac32 100644 --- a/mg-api/src/lib.rs +++ b/mg-api/src/lib.rs @@ -29,7 +29,7 @@ use dropshot::{ }; use dropshot_api_manager_types::api_versions; use rdb::{ - BfdPeerConfig, Path as RdbPath, PathV1, Prefix, Prefix4, Prefix6, + BfdPeerConfig, Path as RdbPath, PathV1, PathV2, Prefix, Prefix4, Prefix6, StaticRouteKey, types::{AddressFamily, ProtocolFilter}, }; @@ -482,18 +482,32 @@ pub trait MgAdminApi { request: Query, ) -> Result, HttpError>; - // VERSION_UNNUMBERED+: BgpPathProperties.peer is PeerId enum - #[endpoint { method = GET, path = "/rib/status/imported", versions = VERSION_UNNUMBERED.. }] + // VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC: PeerId but no origin/internal + #[endpoint { method = GET, path = "/rib/status/imported", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] async fn get_rib_imported_v2( rqctx: RequestContext, request: Query, - ) -> Result, HttpError>; + ) -> Result, HttpError>; - // VERSION_UNNUMBERED+: BgpPathProperties.peer is PeerId enum - #[endpoint { method = GET, path = "/rib/status/selected", versions = VERSION_UNNUMBERED.. }] + // VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC: PeerId but no origin/internal + #[endpoint { method = GET, path = "/rib/status/selected", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] async fn get_rib_selected_v2( rqctx: RequestContext, request: Query, + ) -> Result, HttpError>; + + // VERSION_EXTENDED_NH_STATIC+: BgpPathProperties with origin/internal + #[endpoint { method = GET, path = "/rib/status/imported", versions = VERSION_EXTENDED_NH_STATIC.. }] + async fn get_rib_imported_v3( + rqctx: RequestContext, + request: Query, + ) -> Result, HttpError>; + + // VERSION_EXTENDED_NH_STATIC+: BgpPathProperties with origin/internal + #[endpoint { method = GET, path = "/rib/status/selected", versions = VERSION_EXTENDED_NH_STATIC.. }] + async fn get_rib_selected_v3( + rqctx: RequestContext, + request: Query, ) -> Result, HttpError>; #[endpoint { method = GET, path = "/bgp/status/neighbors", versions = ..VERSION_IPV6_BASIC }] @@ -828,6 +842,29 @@ impl From for RibV1 { } } +/// V2 Rib with PathV2 (no origin/internal in BgpPathProperties). +/// Used for VERSION_UNNUMBERED through VERSION_EXTENDED_NH_STATIC. +/// Delete when VERSION_EXTENDED_NH_STATIC is the minimum supported +/// version. +#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)] +#[schemars(rename = "Rib")] +pub struct RibV2(BTreeMap>); + +impl From for RibV2 { + fn from(value: rdb::db::Rib) -> Self { + RibV2( + value + .into_iter() + .map(|(k, v)| { + let paths_v2: BTreeSet = + v.into_iter().map(PathV2::from).collect(); + (k.to_string(), paths_v2) + }) + .collect(), + ) + } +} + /// Unified neighbor selector supporting both numbered and unnumbered peers #[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)] pub struct NeighborSelector { diff --git a/mgadm/src/rib.rs b/mgadm/src/rib.rs index ee122264a..ce91b3081 100644 --- a/mgadm/src/rib.rs +++ b/mgadm/src/rib.rs @@ -96,7 +96,7 @@ async fn get_imported( protocol: Option, ) -> Result<()> { let imported = c - .get_rib_imported_v2(address_family.as_ref(), protocol.as_ref()) + .get_rib_imported_v3(address_family.as_ref(), protocol.as_ref()) .await? .into_inner(); @@ -110,7 +110,7 @@ async fn get_selected( protocol: Option, ) -> Result<()> { let selected = c - .get_rib_selected_v2(address_family.as_ref(), protocol.as_ref()) + .get_rib_selected_v3(address_family.as_ref(), protocol.as_ref()) .await? .into_inner(); diff --git a/mgd/src/admin.rs b/mgd/src/admin.rs index 8fdab7d1e..61f3bfaa3 100644 --- a/mgd/src/admin.rs +++ b/mgd/src/admin.rs @@ -492,17 +492,31 @@ impl MgAdminApi for MgAdminApiImpl { async fn get_rib_imported_v2( ctx: RequestContext, request: Query, - ) -> Result, HttpError> { + ) -> Result, HttpError> { rib_admin::get_rib_imported_v2(ctx, request).await } async fn get_rib_selected_v2( ctx: RequestContext, request: Query, - ) -> Result, HttpError> { + ) -> Result, HttpError> { rib_admin::get_rib_selected_v2(ctx, request).await } + async fn get_rib_imported_v3( + ctx: RequestContext, + request: Query, + ) -> Result, HttpError> { + rib_admin::get_rib_imported_v3(ctx, request).await + } + + async fn get_rib_selected_v3( + ctx: RequestContext, + request: Query, + ) -> Result, HttpError> { + rib_admin::get_rib_selected_v3(ctx, request).await + } + async fn get_neighbors( ctx: RequestContext, request: Query, diff --git a/mgd/src/rib_admin.rs b/mgd/src/rib_admin.rs index 52cec9ef0..a32353d95 100644 --- a/mgd/src/rib_admin.rs +++ b/mgd/src/rib_admin.rs @@ -8,7 +8,7 @@ use dropshot::{ RequestContext, TypedBody, }; use mg_api::{ - BestpathFanoutRequest, BestpathFanoutResponse, Rib, RibQuery, RibV1, + BestpathFanoutRequest, BestpathFanoutResponse, Rib, RibQuery, RibV1, RibV2, filter_rib_by_protocol, }; use std::sync::Arc; @@ -36,11 +36,11 @@ pub async fn get_rib_selected( Ok(HttpResponseOk(filtered.into())) } -// VERSION_UNNUMBERED+ (BgpPathProperties.peer is PeerId enum) +// VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC (PeerId, no origin/internal) pub async fn get_rib_imported_v2( ctx: RequestContext>, query: Query, -) -> Result, HttpError> { +) -> Result, HttpError> { let ctx = ctx.context(); let query = query.into_inner(); let imported = ctx.db.full_rib(query.address_family); @@ -51,6 +51,29 @@ pub async fn get_rib_imported_v2( pub async fn get_rib_selected_v2( ctx: RequestContext>, query: Query, +) -> Result, HttpError> { + let ctx = ctx.context(); + let query = query.into_inner(); + let selected = ctx.db.loc_rib(query.address_family); + let filtered = filter_rib_by_protocol(selected, query.protocol); + Ok(HttpResponseOk(filtered.into())) +} + +// VERSION_EXTENDED_NH_STATIC+ (BgpPathProperties with origin/internal) +pub async fn get_rib_imported_v3( + ctx: RequestContext>, + query: Query, +) -> Result, HttpError> { + let ctx = ctx.context(); + let query = query.into_inner(); + let imported = ctx.db.full_rib(query.address_family); + let filtered = filter_rib_by_protocol(imported, query.protocol); + Ok(HttpResponseOk(filtered.into())) +} + +pub async fn get_rib_selected_v3( + ctx: RequestContext>, + query: Query, ) -> Result, HttpError> { let ctx = ctx.context(); let query = query.into_inner(); diff --git a/openapi/mg-admin/mg-admin-7.0.0-8295c4.json b/openapi/mg-admin/mg-admin-7.0.0-ec3815.json similarity index 99% rename from openapi/mg-admin/mg-admin-7.0.0-8295c4.json rename to openapi/mg-admin/mg-admin-7.0.0-ec3815.json index 879b21dea..e19e65fba 100644 --- a/openapi/mg-admin/mg-admin-7.0.0-8295c4.json +++ b/openapi/mg-admin/mg-admin-7.0.0-ec3815.json @@ -1404,7 +1404,7 @@ }, "/rib/status/imported": { "get": { - "operationId": "get_rib_imported_v2", + "operationId": "get_rib_imported_v3", "parameters": [ { "in": "query", @@ -1445,7 +1445,7 @@ }, "/rib/status/selected": { "get": { - "operationId": "get_rib_selected_v2", + "operationId": "get_rib_selected_v3", "parameters": [ { "in": "query", @@ -2151,6 +2151,9 @@ "format": "uint32", "minimum": 0 }, + "internal": { + "type": "boolean" + }, "local_pref": { "nullable": true, "type": "integer", @@ -2163,6 +2166,9 @@ "format": "uint32", "minimum": 0 }, + "origin": { + "$ref": "#/components/schemas/PathOrigin" + }, "origin_as": { "type": "integer", "format": "uint32", @@ -2171,6 +2177,10 @@ "peer": { "$ref": "#/components/schemas/PeerId" }, + "peer_ip": { + "type": "string", + "format": "ip" + }, "stale": { "nullable": true, "type": "string", @@ -2180,8 +2190,11 @@ "required": [ "as_path", "id", + "internal", + "origin", "origin_as", - "peer" + "peer", + "peer_ip" ] }, "BgpPeerConfig": { @@ -4599,7 +4612,7 @@ ] }, "PathOrigin": { - "description": "An enumeration indicating the origin type of a path.", + "description": "BGP ORIGIN path attribute (RFC 4271 Section 4.3).\n\nIndicates how the route was learned: - `Igp` (0): Route interior to the originating AS - `Egp` (1): Route learned via EGP - `Incomplete` (2): Route learned by some other means\n\nUsed in bestpath selection: lower origin value is preferred (IGP < EGP < Incomplete).", "oneOf": [ { "description": "Interior gateway protocol", diff --git a/openapi/mg-admin/mg-admin-latest.json b/openapi/mg-admin/mg-admin-latest.json index 8f77d1329..794c50e92 120000 --- a/openapi/mg-admin/mg-admin-latest.json +++ b/openapi/mg-admin/mg-admin-latest.json @@ -1 +1 @@ -mg-admin-7.0.0-8295c4.json \ No newline at end of file +mg-admin-7.0.0-ec3815.json \ No newline at end of file diff --git a/rdb-types/Cargo.toml b/rdb-types/Cargo.toml index 5cb2368b4..2808400f9 100644 --- a/rdb-types/Cargo.toml +++ b/rdb-types/Cargo.toml @@ -6,6 +6,7 @@ edition = "2024" [dependencies] serde.workspace = true schemars.workspace = true +num_enum.workspace = true clap = { workspace = true, optional = true } oxnet.workspace = true diff --git a/rdb-types/src/lib.rs b/rdb-types/src/lib.rs index 599868b7f..c9737d0e6 100644 --- a/rdb-types/src/lib.rs +++ b/rdb-types/src/lib.rs @@ -707,3 +707,48 @@ impl JsonSchema for Dscp { .into() } } + +/// BGP ORIGIN path attribute (RFC 4271 Section 4.3). +/// +/// Indicates how the route was learned: +/// - `Igp` (0): Route interior to the originating AS +/// - `Egp` (1): Route learned via EGP +/// - `Incomplete` (2): Route learned by some other means +/// +/// Used in bestpath selection: lower origin value is preferred +/// (IGP < EGP < Incomplete). +#[derive( + Clone, + Copy, + Debug, + Deserialize, + Eq, + Hash, + num_enum::IntoPrimitive, + JsonSchema, + Ord, + PartialEq, + PartialOrd, + Serialize, + num_enum::TryFromPrimitive, +)] +#[repr(u8)] +#[serde(rename_all = "snake_case")] +pub enum PathOrigin { + /// Interior gateway protocol + Igp = 0, + /// Exterior gateway protocol + Egp = 1, + /// Incomplete path origin + Incomplete = 2, +} + +impl fmt::Display for PathOrigin { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + PathOrigin::Igp => write!(f, "igp"), + PathOrigin::Egp => write!(f, "egp"), + PathOrigin::Incomplete => write!(f, "incomplete"), + } + } +} diff --git a/rdb/src/bestpath.rs b/rdb/src/bestpath.rs index 58c8c5461..6258ab9d1 100644 --- a/rdb/src/bestpath.rs +++ b/rdb/src/bestpath.rs @@ -2,33 +2,24 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use std::collections::{BTreeMap, BTreeSet}; - -use crate::types::Path; +use crate::types::{Path, PathOrigin}; use itertools::Itertools; +use std::{ + collections::{BTreeMap, BTreeSet}, + net::IpAddr, +}; /// The bestpath algorithm chooses the best set of up to `max` paths for a -/// particular prefix from the RIB. The set of paths chosen will all have -/// equal RIB priority, MED, local_pref, AS path length and shutdown status. -/// The bestpath algorithm performs path filtering in the following ordered -/// sequence of operations. +/// particular prefix from the RIB. Pre-BGP filtering: /// /// - partition candidate paths into active and shutdown groups. /// - if only shutdown routes exist, select from that group, otherwise /// select from the active group. /// - filter the selection group to the set of paths with the smallest /// rib priority -/// - filter the selection group to the set of paths with the largest -/// local preference -/// - filter the selection group to the set of paths with the smallest -/// AS path length -/// - filter the selection group to the set of paths with the smallest -/// multi-exit discriminator (MED) on a per-AS basis. +/// - prefer static routes over BGP when RIB priority is equal /// -/// Upon completion of these filtering operations, if the selection group -/// is larger than `max`, return the first `max` entries. This is a set, -/// so "first" has no semantic meaning, consider it to be random. If the -/// selection group is smaller than `max`, the entire group is returned. +/// BGP-specific tie-breaking follows RFC 4271 Section 9.1.2.2 pub fn bestpaths(paths: &BTreeSet, max: usize) -> Option> { // Short-circuit: if there's only 1 candidate, then it is the best if paths.len() == 1 { @@ -72,10 +63,26 @@ pub fn bgp_bestpaths( candidates: BTreeSet<&Path>, max: usize, ) -> BTreeSet { + // Per RFC 4721 Section 9.1.2.2: + // """ + // The tie-breaking algorithm begins by considering all equally + // preferable routes to the same destination, and then selects routes to + // be removed from consideration. The algorithm terminates as soon as + // only one route remains in consideration. The criteria MUST be + // applied in the order specified. + // """ + // + // We should be returning at any point the `candidates` only contains + // 1 path. "Multipath" is not a standardized BGP behavior, however Cisco + // implements that decision after the IGP metric check and many other + // routing stacks have followed their lead. Again, following the principle + // of least surprise, our multipath decision is made at the same step. + // + // XXX: This also gives us the opportunity to add trace points, debugs, + // or even a "bestpath reason" specific to each step that can provide + // better insight into the bestpath calculation itself. + // Filter down to paths that are not stale (Graceful Restart). - // The `min_set_by_key` method allows us to assign "not stale" paths to the - // `0` set, and "stale" paths to the `1` set. The method will then return - // the `0` set if any "not stale" paths exist. let candidates = candidates .into_iter() @@ -86,8 +93,12 @@ pub fn bgp_bestpaths( }, None => 0, }); + if candidates.len() == 1 { + return candidates.into_iter().cloned().collect(); + } // Filter down to paths with the highest local preference + // RFC 4271 Section 9.1.2 let candidates = candidates .into_iter() @@ -95,8 +106,12 @@ pub fn bgp_bestpaths( Some(ref bgp) => bgp.local_pref.unwrap_or(0), None => 0, }); + if candidates.len() == 1 { + return candidates.into_iter().cloned().collect(); + } // Filter down to paths with the shortest AS-Path length + // RFC 4271 Section 9.1.2.2 (a) let candidates = candidates .into_iter() @@ -104,8 +119,25 @@ pub fn bgp_bestpaths( Some(ref bgp) => bgp.as_path.len(), None => 0, }); + if candidates.len() == 1 { + return candidates.into_iter().cloned().collect(); + } - // Group candidates by AS for MED selection. + // Filter down to paths with the lowest Origin + // RFC 4271 Section 9.1.2.2 (b) + let candidates = + candidates + .into_iter() + .min_set_by_key(|path| match path.bgp { + Some(ref bgp) => bgp.origin, + None => PathOrigin::Incomplete, + }); + if candidates.len() == 1 { + return candidates.into_iter().cloned().collect(); + } + + // Group candidates by AS for MED selection + // RFC 4271 Section 9.1.2.2 (c) let mut as_groups: BTreeMap> = BTreeMap::new(); for path in candidates { let origin_as = path.bgp.as_ref().map(|bgp| bgp.origin_as).unwrap_or(0); @@ -113,29 +145,86 @@ pub fn bgp_bestpaths( } // Filter each AS group to paths with lowest MED - let candidates = as_groups.into_values().flat_map(|paths| { - paths.into_iter().min_set_by_key(|path| { - path.bgp.as_ref().and_then(|bgp| bgp.med).unwrap_or(0) + let candidates: Vec<&Path> = as_groups + .into_values() + .flat_map(|paths| { + paths.into_iter().min_set_by_key(|path| { + path.bgp.as_ref().and_then(|bgp| bgp.med).unwrap_or(0) + }) }) - }); + .collect(); + if candidates.len() == 1 { + return candidates.into_iter().cloned().collect(); + } + + // Filter down to eBGP paths when possible (prefer eBGP over + // iBGP) RFC 4271 Section 9.1.2.2 (d) + let candidates = + candidates + .into_iter() + .min_set_by_key(|path| match path.bgp { + Some(ref bgp) => bgp.internal, + None => false, + }); + if candidates.len() == 1 { + return candidates.into_iter().cloned().collect(); + } + + // TODO(OSPF/IS-IS): Filter down to paths with lowest IGP cost + // RFC 4271 Section 9.1.2.2 (e) + // Note: DDM is currently the only supported IGP and it doesn't + // interface with the external world, so this is N/A. - // Return up to max elements - candidates.take(max).cloned().collect() + // All paths still under consideration at this point have + // identical values for the earlier checks. Multipath is now + // allowed if `max` permits it. + if max > 1 { + return candidates.into_iter().take(max).cloned().collect(); + } + + // Prefer the path with the lowest Router-ID (BGP Identifier) + // RFC 4271 Section 9.1.2.2 (f) + let candidates = + candidates + .into_iter() + .min_set_by_key(|path| match path.bgp { + Some(ref bgp) => bgp.id, + None => u32::MAX, + }); + if candidates.len() == 1 { + return candidates.into_iter().cloned().collect(); + } + + // Prefer the path from the peer with the lowest IP address + // RFC 4271 Section 9.1.2.2 (g) + let candidates = + candidates + .into_iter() + .min_set_by_key(|path| match path.bgp { + Some(ref bgp) => match bgp.peer_ip { + IpAddr::V4(a) => u128::from(a.to_bits()), + IpAddr::V6(a) => a.to_bits(), + }, + None => u128::MAX, + }); + if candidates.len() == 1 { + return candidates.into_iter().cloned().collect(); + } + + // Should be unreachable with distinct peers, but ensure we + // never return more than 1 path after the multipath check. + candidates.into_iter().take(1).cloned().collect() } #[cfg(test)] mod test { - use crate::PeerId; - use std::collections::BTreeSet; - use std::net::IpAddr; - use std::str::FromStr; - use super::bestpaths; use crate::{ BgpPathProperties, DEFAULT_RIB_PRIORITY_BGP, - DEFAULT_RIB_PRIORITY_STATIC, Path, + DEFAULT_RIB_PRIORITY_STATIC, Path, PathOrigin, PeerId, types::test_helpers::path_sets_equal, }; + use std::{collections::BTreeSet, net::IpAddr, str::FromStr}; // Bestpaths is purely a function of the path info itself, so we don't // need a Rib or Prefix, just a set of candidate paths and a set of @@ -155,8 +244,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 470, peer: PeerId::Ip(remote_ip1), + peer_ip: remote_ip1, + internal: false, id: 47, med: Some(75), local_pref: Some(100), @@ -180,8 +272,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 480, peer: PeerId::Ip(remote_ip2), + peer_ip: remote_ip2, + internal: false, id: 48, med: Some(75), local_pref: Some(100), @@ -215,8 +310,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 490, peer: PeerId::Ip(remote_ip3), + peer_ip: remote_ip3, + internal: false, id: 49, med: Some(100), local_pref: Some(100), @@ -316,8 +414,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 470, peer: PeerId::Ip(remote_ip1), + peer_ip: remote_ip1, + internal: false, id: 47, med: Some(75), local_pref: Some(100), @@ -333,8 +434,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: true, // This path is shutdown bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 480, peer: PeerId::Ip(remote_ip2), + peer_ip: remote_ip2, + internal: false, id: 48, med: Some(75), local_pref: Some(100), @@ -378,8 +482,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: true, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 470, peer: PeerId::Ip(remote_ip1), + peer_ip: remote_ip1, + internal: false, id: 47, med: Some(75), local_pref: Some(100), @@ -425,8 +532,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 100, peer: PeerId::Ip(ip1), + peer_ip: ip1, + internal: false, id: 1, med: Some(50), local_pref: Some(100), @@ -442,8 +552,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 100, peer: PeerId::Ip(ip3), + peer_ip: ip3, + internal: false, id: 1, med: Some(100), // Higher MED = worse local_pref: Some(100), @@ -461,8 +574,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 200, peer: PeerId::Ip(ip2), + peer_ip: ip2, + internal: false, id: 2, med: Some(999), // Very high MED, but irrelevant - different AS local_pref: Some(100), @@ -479,8 +595,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 300, peer: PeerId::Ip(ip4), + peer_ip: ip4, + internal: false, id: 3, med: Some(10), // Low MED, but can't "steal" selection from other ASes local_pref: Some(100), @@ -546,8 +665,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 100, peer: PeerId::Ip(ip1), + peer_ip: ip1, + internal: false, id: 1, med: Some(50), local_pref: Some(100), @@ -563,8 +685,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 100, peer: PeerId::Ip(ip2), + peer_ip: ip2, + internal: false, id: 1, med: Some(50), // Same MED local_pref: Some(100), @@ -580,8 +705,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 100, peer: PeerId::Ip(ip3), + peer_ip: ip3, + internal: false, id: 1, med: Some(50), // Same MED local_pref: Some(100), @@ -604,4 +732,329 @@ mod test { let result = bestpaths(&candidates, 2).unwrap(); assert_eq!(result.len(), 2, "max should limit results"); } + + /// Helper: build a BGP path with sensible defaults. Only the + /// fields under test need to be overridden after construction. + fn bgp_path(ip: IpAddr) -> Path { + Path { + nexthop: ip, + nexthop_interface: None, + rib_priority: DEFAULT_RIB_PRIORITY_BGP, + shutdown: false, + bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, + origin_as: 100, + peer: PeerId::Ip(ip), + peer_ip: ip, + internal: false, + id: 1, + med: None, + local_pref: Some(100), + as_path: vec![100], + stale: None, + }), + vlan_id: None, + } + } + + // ---------------------------------------------------------------- + // Origin preference (RFC 4271 Section 9.1.2.2 (b)) + // ---------------------------------------------------------------- + + /// IGP origin is preferred over EGP and Incomplete. + #[test] + fn test_bestpath_origin_igp_preferred() { + let ip1 = IpAddr::from_str("10.0.0.1").unwrap(); + let ip2 = IpAddr::from_str("10.0.0.2").unwrap(); + let ip3 = IpAddr::from_str("10.0.0.3").unwrap(); + + let mut igp = bgp_path(ip1); + igp.bgp.as_mut().unwrap().origin = PathOrigin::Igp; + + let mut egp = bgp_path(ip2); + egp.bgp.as_mut().unwrap().origin = PathOrigin::Egp; + + let mut incomplete = bgp_path(ip3); + incomplete.bgp.as_mut().unwrap().origin = PathOrigin::Incomplete; + + let candidates = + BTreeSet::from([igp.clone(), egp.clone(), incomplete.clone()]); + let result = bestpaths(&candidates, 1).unwrap(); + assert_eq!(result.len(), 1); + assert!(path_sets_equal(&result, &BTreeSet::from([igp.clone()]))); + + // EGP beats Incomplete when IGP is absent + let candidates = BTreeSet::from([egp.clone(), incomplete]); + let result = bestpaths(&candidates, 1).unwrap(); + assert_eq!(result.len(), 1); + assert!(path_sets_equal(&result, &BTreeSet::from([egp.clone()]))); + } + + /// When all paths share the same origin, they all survive + /// the origin filter. + #[test] + fn test_bestpath_origin_equal_passes_all() { + let ip1 = IpAddr::from_str("10.0.0.1").unwrap(); + let ip2 = IpAddr::from_str("10.0.0.2").unwrap(); + + let mut p1 = bgp_path(ip1); + p1.bgp.as_mut().unwrap().origin = PathOrigin::Egp; + let mut p2 = bgp_path(ip2); + p2.bgp.as_mut().unwrap().origin = PathOrigin::Egp; + + let candidates = BTreeSet::from([p1.clone(), p2.clone()]); + let result = bestpaths(&candidates, 10).unwrap(); + assert_eq!(result.len(), 2, "equal origin should not eliminate paths"); + } + + // ---------------------------------------------------------------- + // eBGP over iBGP preference (RFC 4271 Section 9.1.2.2 (d)) + // ---------------------------------------------------------------- + + /// eBGP paths are preferred over iBGP paths. + #[test] + fn test_bestpath_ebgp_over_ibgp() { + let ip1 = IpAddr::from_str("10.0.0.1").unwrap(); + let ip2 = IpAddr::from_str("10.0.0.2").unwrap(); + + let ebgp = bgp_path(ip1); // internal: false by default + + let mut ibgp = bgp_path(ip2); + ibgp.bgp.as_mut().unwrap().internal = true; + + let candidates = BTreeSet::from([ebgp.clone(), ibgp]); + let result = bestpaths(&candidates, 1).unwrap(); + assert_eq!(result.len(), 1); + assert!(path_sets_equal(&result, &BTreeSet::from([ebgp.clone()]))); + } + + /// When all paths are iBGP, they all survive the eBGP filter. + #[test] + fn test_bestpath_all_ibgp_passes_all() { + let ip1 = IpAddr::from_str("10.0.0.1").unwrap(); + let ip2 = IpAddr::from_str("10.0.0.2").unwrap(); + + let mut p1 = bgp_path(ip1); + p1.bgp.as_mut().unwrap().internal = true; + let mut p2 = bgp_path(ip2); + p2.bgp.as_mut().unwrap().internal = true; + + let candidates = BTreeSet::from([p1.clone(), p2.clone()]); + let result = bestpaths(&candidates, 10).unwrap(); + assert_eq!(result.len(), 2, "all-iBGP should not eliminate paths"); + } + + /// When all paths are eBGP, they all survive the eBGP filter. + #[test] + fn test_bestpath_all_ebgp_passes_all() { + let ip1 = IpAddr::from_str("10.0.0.1").unwrap(); + let ip2 = IpAddr::from_str("10.0.0.2").unwrap(); + + let p1 = bgp_path(ip1); // internal: false by default + let p2 = bgp_path(ip2); + + let candidates = BTreeSet::from([p1.clone(), p2.clone()]); + let result = bestpaths(&candidates, 10).unwrap(); + assert_eq!(result.len(), 2, "all-eBGP should not eliminate paths"); + } + + // ---------------------------------------------------------------- + // Multipath decision point (after step (d), before (f)) + // ---------------------------------------------------------------- + + /// When max > 1 and multiple paths survive through eBGP/iBGP, + /// all are returned (up to max) without router-id or peer-ip + /// tie-breaking. + #[test] + fn test_bestpath_multipath_returns_multiple() { + let ip1 = IpAddr::from_str("10.0.0.1").unwrap(); + let ip2 = IpAddr::from_str("10.0.0.2").unwrap(); + let ip3 = IpAddr::from_str("10.0.0.3").unwrap(); + + // Three equivalent eBGP paths with different router-ids + let mut p1 = bgp_path(ip1); + p1.bgp.as_mut().unwrap().id = 10; + let mut p2 = bgp_path(ip2); + p2.bgp.as_mut().unwrap().id = 20; + let mut p3 = bgp_path(ip3); + p3.bgp.as_mut().unwrap().id = 30; + + let candidates = BTreeSet::from([p1.clone(), p2.clone(), p3.clone()]); + + // max=3: all should be returned + let result = bestpaths(&candidates, 3).unwrap(); + assert_eq!(result.len(), 3); + + // max=2: only 2 returned + let result = bestpaths(&candidates, 2).unwrap(); + assert_eq!(result.len(), 2); + } + + // ---------------------------------------------------------------- + // Router-ID tie-breaking (RFC 4271 Section 9.1.2.2 (f)) + // ---------------------------------------------------------------- + + /// Lowest Router-ID wins when max=1, which also means multipath + /// is disabled and the algorithm falls through to tie-breaking. + #[test] + fn test_bestpath_router_id_tiebreak() { + let ip1 = IpAddr::from_str("10.0.0.1").unwrap(); + let ip2 = IpAddr::from_str("10.0.0.2").unwrap(); + + let mut low_id = bgp_path(ip1); + low_id.bgp.as_mut().unwrap().id = 1; + let mut high_id = bgp_path(ip2); + high_id.bgp.as_mut().unwrap().id = 999; + + let candidates = BTreeSet::from([high_id.clone(), low_id.clone()]); + let result = bestpaths(&candidates, 1).unwrap(); + assert_eq!(result.len(), 1); + assert!(path_sets_equal(&result, &BTreeSet::from([low_id]))); + } + + // ---------------------------------------------------------------- + // Peer IP tie-breaking (RFC 4271 Section 9.1.2.2 (g)) + // ---------------------------------------------------------------- + + /// Lowest peer IPv4 address wins when router-ids are equal. + #[test] + fn test_bestpath_peer_ip_tiebreak_v4() { + let ip1 = IpAddr::from_str("10.0.0.1").unwrap(); + let ip2 = IpAddr::from_str("10.0.0.2").unwrap(); + + let low_peer = bgp_path(ip1); + let high_peer = bgp_path(ip2); + + let candidates = BTreeSet::from([high_peer.clone(), low_peer.clone()]); + let result = bestpaths(&candidates, 1).unwrap(); + assert_eq!(result.len(), 1); + assert!(path_sets_equal(&result, &BTreeSet::from([low_peer]))); + } + + /// Lowest peer IPv6 address wins when router-ids are equal. + #[test] + fn test_bestpath_peer_ip_tiebreak_v6() { + let ip1 = IpAddr::from_str("2001:db8::1").unwrap(); + let ip2 = IpAddr::from_str("2001:db8::2").unwrap(); + + let low_peer = bgp_path(ip1); + let high_peer = bgp_path(ip2); + + let candidates = BTreeSet::from([high_peer.clone(), low_peer.clone()]); + let result = bestpaths(&candidates, 1).unwrap(); + assert_eq!(result.len(), 1); + assert!(path_sets_equal(&result, &BTreeSet::from([low_peer]))); + } + + /// When candidates have a mix of IPv4 and IPv6 peer IPs, + /// the IPv4 peer wins because its u128 representation is + /// smaller than any non-trivial IPv6 address. + #[test] + fn test_bestpath_peer_ip_tiebreak_v4_vs_v6() { + let v4 = IpAddr::from_str("203.0.113.1").unwrap(); + let v6 = IpAddr::from_str("2001:db8::1").unwrap(); + + let v4_path = bgp_path(v4); + let v6_path = bgp_path(v6); + + let candidates = BTreeSet::from([v6_path.clone(), v4_path.clone()]); + let result = bestpaths(&candidates, 1).unwrap(); + assert_eq!(result.len(), 1); + assert!(path_sets_equal(&result, &BTreeSet::from([v4_path]))); + } + + // ---------------------------------------------------------------- + // Stale path preference (Graceful Restart) + // ---------------------------------------------------------------- + + /// Non-stale paths are preferred over stale paths. + #[test] + fn test_bestpath_stale_preference() { + let ip1 = IpAddr::from_str("10.0.0.1").unwrap(); + let ip2 = IpAddr::from_str("10.0.0.2").unwrap(); + + let fresh = bgp_path(ip1); + + let mut stale = bgp_path(ip2); + stale.bgp.as_mut().unwrap().stale = Some(chrono::Utc::now()); + + let candidates = BTreeSet::from([fresh.clone(), stale]); + let result = bestpaths(&candidates, 1).unwrap(); + assert_eq!(result.len(), 1); + assert!(path_sets_equal(&result, &BTreeSet::from([fresh]))); + } + + // ---------------------------------------------------------------- + // Full pipeline: exercise multiple tie-breaking steps in one test + // ---------------------------------------------------------------- + + /// Walk the entire BGP bestpath pipeline from local_pref through + /// peer-ip, confirming each step eliminates the right candidate. + #[test] + fn test_bestpath_full_pipeline() { + let ip1 = IpAddr::from_str("10.0.0.1").unwrap(); + let ip2 = IpAddr::from_str("10.0.0.2").unwrap(); + let ip3 = IpAddr::from_str("10.0.0.3").unwrap(); + let ip4 = IpAddr::from_str("10.0.0.4").unwrap(); + let ip5 = IpAddr::from_str("10.0.0.5").unwrap(); + let ip6 = IpAddr::from_str("10.0.0.6").unwrap(); + let ip7 = IpAddr::from_str("10.0.0.7").unwrap(); + + // p1: eliminated by local_pref (lower than others) + let mut p1 = bgp_path(ip1); + p1.bgp.as_mut().unwrap().local_pref = Some(50); + + // p2: eliminated by AS path length (longer) + let mut p2 = bgp_path(ip2); + p2.bgp.as_mut().unwrap().as_path = vec![100, 200, 300]; + + // p3: eliminated by origin (Incomplete vs Igp) + let mut p3 = bgp_path(ip3); + p3.bgp.as_mut().unwrap().origin = PathOrigin::Incomplete; + + // p4: eliminated by MED (higher than p5-p7) + let mut p4 = bgp_path(ip4); + p4.bgp.as_mut().unwrap().med = Some(999); + + // p5: eliminated by eBGP/iBGP (internal) + let mut p5 = bgp_path(ip5); + p5.bgp.as_mut().unwrap().internal = true; + + // p6: eliminated by router-id (higher than p7) + let mut p6 = bgp_path(ip6); + p6.bgp.as_mut().unwrap().id = 999; + + // p7: survives — best at every step + let p7 = bgp_path(ip7); + // id=1, peer_ip=10.0.0.7, internal=false, origin=Igp, + // local_pref=100, as_path=[100], med=None + + let candidates = BTreeSet::from([p1, p2, p3, p4, p5, p6, p7.clone()]); + let result = bestpaths(&candidates, 1).unwrap(); + assert_eq!(result.len(), 1); + assert!(path_sets_equal(&result, &BTreeSet::from([p7]))); + } + + /// Verify that the AS path length filter uses element count, + /// not the raw AS numbers. + #[test] + fn test_bestpath_as_path_length_not_values() { + let ip1 = IpAddr::from_str("10.0.0.1").unwrap(); + let ip2 = IpAddr::from_str("10.0.0.2").unwrap(); + + // Short path with large AS numbers + let mut short = bgp_path(ip1); + short.bgp.as_mut().unwrap().as_path = vec![65000]; + short.bgp.as_mut().unwrap().origin_as = 65000; + + // Long path with small AS numbers + let mut long = bgp_path(ip2); + long.bgp.as_mut().unwrap().as_path = vec![1, 2, 3]; + long.bgp.as_mut().unwrap().origin_as = 1; + + let candidates = BTreeSet::from([short.clone(), long]); + let result = bestpaths(&candidates, 1).unwrap(); + assert_eq!(result.len(), 1); + assert!(path_sets_equal(&result, &BTreeSet::from([short]))); + } } diff --git a/rdb/src/db.rs b/rdb/src/db.rs index 4b6455c80..1796fb8fc 100644 --- a/rdb/src/db.rs +++ b/rdb/src/db.rs @@ -1445,7 +1445,8 @@ mod test { use crate::StaticRouteKey; use crate::{ BgpPathProperties, DEFAULT_RIB_PRIORITY_BGP, - DEFAULT_RIB_PRIORITY_STATIC, Path, PeerId, Prefix, Prefix4, db::Db, + DEFAULT_RIB_PRIORITY_STATIC, Path, PathOrigin, PeerId, Prefix, + Prefix4, db::Db, }; // init test vars let p0 = Prefix::from("192.168.0.0/24".parse::().unwrap()); @@ -1461,8 +1462,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 1111, peer: PeerId::Ip(remote_ip0), + peer_ip: remote_ip0, + internal: false, id: 1111, med: Some(1111), local_pref: Some(1111), @@ -1477,8 +1481,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 2222, peer: PeerId::Ip(remote_ip1), + peer_ip: remote_ip1, + internal: false, id: 2222, med: Some(2222), local_pref: Some(2222), @@ -1498,8 +1505,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 2222, peer: PeerId::Ip(remote_ip2), + peer_ip: remote_ip2, + internal: false, id: 2222, med: Some(2222), local_pref: Some(4444), @@ -2528,8 +2538,8 @@ mod test { #[test] fn test_enforce_first_as() { use crate::{ - BgpPathProperties, DEFAULT_RIB_PRIORITY_BGP, Path, PeerId, Prefix, - Prefix4, Prefix6, + BgpPathProperties, DEFAULT_RIB_PRIORITY_BGP, Path, PathOrigin, + PeerId, Prefix, Prefix4, Prefix6, }; let log = init_file_logger("rib.log"); @@ -2552,8 +2562,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 65001, peer: peer.clone(), + peer_ip, + internal: false, id: 1, med: None, local_pref: Some(100), @@ -2571,8 +2584,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 65001, peer: peer.clone(), + peer_ip, + internal: false, id: 1, med: None, local_pref: Some(100), @@ -2589,8 +2605,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 65002, peer: other_peer.clone(), + peer_ip: other_ip, + internal: false, id: 2, med: None, local_pref: Some(100), @@ -2637,8 +2656,8 @@ mod test { #[test] fn test_enforce_first_as_empty_as_path() { use crate::{ - BgpPathProperties, DEFAULT_RIB_PRIORITY_BGP, Path, PeerId, Prefix, - Prefix4, + BgpPathProperties, DEFAULT_RIB_PRIORITY_BGP, Path, PathOrigin, + PeerId, Prefix, Prefix4, }; let log = init_file_logger("rib.log"); @@ -2657,8 +2676,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 65001, peer: peer.clone(), + peer_ip, + internal: false, id: 1, med: None, local_pref: Some(100), @@ -2682,8 +2704,8 @@ mod test { #[test] fn test_enforce_first_as_no_match() { use crate::{ - BgpPathProperties, DEFAULT_RIB_PRIORITY_BGP, Path, PeerId, Prefix, - Prefix4, + BgpPathProperties, DEFAULT_RIB_PRIORITY_BGP, Path, PathOrigin, + PeerId, Prefix, Prefix4, }; let log = init_file_logger("rib.log"); @@ -2701,8 +2723,11 @@ mod test { rib_priority: DEFAULT_RIB_PRIORITY_BGP, shutdown: false, bgp: Some(BgpPathProperties { + origin: PathOrigin::Igp, origin_as: 65001, peer: peer.clone(), + peer_ip, + internal: false, id: 1, med: None, local_pref: Some(100), diff --git a/rdb/src/types.rs b/rdb/src/types.rs index 0dd42ca1f..2e407ad6b 100644 --- a/rdb/src/types.rs +++ b/rdb/src/types.rs @@ -17,7 +17,8 @@ use std::str::FromStr; // Re-export core types from rdb-types pub use rdb_types::{ - AddressFamily, Dscp, PeerId, Prefix, Prefix4, Prefix6, ProtocolFilter, + AddressFamily, Dscp, PathOrigin, PeerId, Prefix, Prefix4, Prefix6, + ProtocolFilter, }; // Marker types for compile-time address family discrimination. @@ -209,11 +210,95 @@ impl From for BgpPathPropertiesV1 { } } +/// Pre-BESTPATH version of BgpPathProperties (no origin/internal). +/// Used for API versions VERSION_UNNUMBERED through +/// VERSION_EXTENDED_NH_STATIC (5.0.0 - 6.0.0). +/// Delete when VERSION_EXTENDED_NH_STATIC is the minimum supported +/// version. +#[derive( + Debug, + Clone, + Serialize, + Deserialize, + JsonSchema, + Eq, + PartialEq, + PartialOrd, + Ord, +)] +#[schemars(rename = "BgpPathProperties")] +pub struct BgpPathPropertiesV2 { + pub origin_as: u32, + pub id: u32, + pub peer: PeerId, + pub med: Option, + pub local_pref: Option, + pub as_path: Vec, + pub stale: Option>, +} + +impl From for BgpPathPropertiesV2 { + fn from(value: BgpPathProperties) -> Self { + Self { + origin_as: value.origin_as, + id: value.id, + peer: value.peer, + med: value.med, + local_pref: value.local_pref, + as_path: value.as_path, + stale: value.stale, + } + } +} + +/// Pre-BESTPATH version of Path (no origin/internal in BgpPathProperties). +/// Used for API versions VERSION_UNNUMBERED through +/// VERSION_EXTENDED_NH_STATIC (5.0.0 - 6.0.0). +/// Delete when VERSION_EXTENDED_NH_STATIC is the minimum supported +/// version. +#[derive( + Debug, + Clone, + Serialize, + Deserialize, + JsonSchema, + Eq, + PartialEq, + PartialOrd, + Ord, +)] +#[schemars(rename = "Path")] +pub struct PathV2 { + pub nexthop: IpAddr, + #[serde(skip_serializing_if = "Option::is_none", default)] + pub nexthop_interface: Option, + pub shutdown: bool, + pub rib_priority: u8, + pub bgp: Option, + pub vlan_id: Option, +} + +impl From for PathV2 { + fn from(value: Path) -> Self { + Self { + nexthop: value.nexthop, + nexthop_interface: value.nexthop_interface, + shutdown: value.shutdown, + rib_priority: value.rib_priority, + bgp: value.bgp.map(BgpPathPropertiesV2::from), + vlan_id: value.vlan_id, + } + } +} + #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Eq, PartialEq)] pub struct BgpPathProperties { + pub origin: PathOrigin, pub origin_as: u32, - pub id: u32, pub peer: PeerId, + pub peer_ip: IpAddr, + pub internal: bool, + pub id: u32, pub med: Option, pub local_pref: Option, pub as_path: Vec, From 412462eb8c14d8e21a593d83b8c1458f947df655 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Fri, 27 Feb 2026 10:14:27 -0700 Subject: [PATCH 28/58] Add detail option for mgadm rib status Adds a display mode for mgadm rib status imported/selected, which allows mgadm users to choose whether they want the output in a detailed or summarized (default) format. --- mg-admin-client/src/lib.rs | 155 ++++++++++++++++++++++++++++++++----- mgadm/src/rib.rs | 59 ++++++++++++-- 2 files changed, 188 insertions(+), 26 deletions(-) diff --git a/mg-admin-client/src/lib.rs b/mg-admin-client/src/lib.rs index 7a667e2ef..7cb0db3b5 100644 --- a/mg-admin-client/src/lib.rs +++ b/mg-admin-client/src/lib.rs @@ -32,6 +32,7 @@ use colored::*; use rdb_types::{AddressFamily, Prefix, ProtocolFilter}; use std::collections::BTreeMap; use std::io::{Write, stdout}; +use std::net::Ipv4Addr; use tabwriter::TabWriter; use types::{Path, Rib}; @@ -39,6 +40,8 @@ pub fn print_rib( rib: Rib, address_family: Option, protocol_filter: Option, + detail: bool, + prefix_filter: Option<&str>, ) { type CliRib = BTreeMap>; @@ -50,6 +53,13 @@ pub fn print_rib( // Parse and categorize all routes for (prefix, paths) in rib.0.into_iter() { + // Client-side exact-match prefix filter + if let Some(filter) = prefix_filter + && prefix != filter + { + continue; + } + let pfx: Prefix = match prefix.parse() { Ok(p) => p, Err(e) => { @@ -87,20 +97,32 @@ pub fn print_rib( matches!(protocol_filter, None | Some(ProtocolFilter::Static)); let show_bgp = matches!(protocol_filter, None | Some(ProtocolFilter::Bgp)); - if show_ipv4 && show_static && !v4_static.is_empty() { - print_static_routes(&v4_static, "Static Routes (IPv4)"); - } - - if show_ipv4 && show_bgp && !v4_bgp.is_empty() { - print_bgp_routes(&v4_bgp, "BGP Routes (IPv4)"); - } - - if show_ipv6 && show_static && !v6_static.is_empty() { - print_static_routes(&v6_static, "Static Routes (IPv6)"); - } - - if show_ipv6 && show_bgp && !v6_bgp.is_empty() { - print_bgp_routes(&v6_bgp, "BGP Routes (IPv6)"); + if detail { + if show_ipv4 && show_static && !v4_static.is_empty() { + print_static_routes_detail(&v4_static, "Static Routes (IPv4)"); + } + if show_ipv4 && show_bgp && !v4_bgp.is_empty() { + print_bgp_routes_detail(&v4_bgp, "BGP Routes (IPv4)"); + } + if show_ipv6 && show_static && !v6_static.is_empty() { + print_static_routes_detail(&v6_static, "Static Routes (IPv6)"); + } + if show_ipv6 && show_bgp && !v6_bgp.is_empty() { + print_bgp_routes_detail(&v6_bgp, "BGP Routes (IPv6)"); + } + } else { + if show_ipv4 && show_static && !v4_static.is_empty() { + print_static_routes(&v4_static, "Static Routes (IPv4)"); + } + if show_ipv4 && show_bgp && !v4_bgp.is_empty() { + print_bgp_routes(&v4_bgp, "BGP Routes (IPv4)"); + } + if show_ipv6 && show_static && !v6_static.is_empty() { + print_static_routes(&v6_static, "Static Routes (IPv6)"); + } + if show_ipv6 && show_bgp && !v6_bgp.is_empty() { + print_bgp_routes(&v6_bgp, "BGP Routes (IPv6)"); + } } } @@ -111,7 +133,7 @@ fn print_static_routes(routes: &BTreeMap>, title: &str) { "{}\t{}\t{}", "Prefix".dimmed(), "Nexthop".dimmed(), - "RIB Priority".dimmed(), + "RIB Prio".dimmed(), ) .unwrap(); @@ -141,16 +163,19 @@ fn print_bgp_routes(routes: &BTreeMap>, title: &str) { let mut tw = TabWriter::new(stdout()); writeln!( &mut tw, - "{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}", + "{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}", "Prefix".dimmed(), "Nexthop".dimmed(), - "RIB Priority".dimmed(), - "Local Pref".dimmed(), - "Origin AS".dimmed(), + "RIB Prio".dimmed(), + "LocPref".dimmed(), + "Orig AS".dimmed(), "Peer".dimmed(), "MED".dimmed(), "AS Path".dimmed(), "Stale".dimmed(), + "Origin".dimmed(), + "iBGP".dimmed(), + "RID".dimmed(), ) .unwrap(); @@ -159,16 +184,20 @@ fn print_bgp_routes(routes: &BTreeMap>, title: &str) { for path in paths.iter() { let bgp = path.bgp.as_ref().unwrap(); let nexthop_display = match &path.nexthop_interface { - Some(iface) => format!("{}({})", iface, path.nexthop), + Some(iface) => { + format!("{}({})", iface, path.nexthop) + } None => path.nexthop.to_string(), }; let peer_str = match &bgp.peer { rdb_types::PeerId::Ip(ip) => ip.to_string(), rdb_types::PeerId::Interface(iface) => iface.clone(), }; + let router_id = Ipv4Addr::from(bgp.id); writeln!( &mut tw, - "\t{}\t{}\t{:?}\t{}\t{}\t{:?}\t{:?}\t{:?}", + "\t{}\t{}\t{:?}\t{}\t{}\t{:?}\t{:?}\t{:?}\ + \t{}\t{}\t{}", nexthop_display, path.rib_priority, bgp.local_pref, @@ -177,6 +206,9 @@ fn print_bgp_routes(routes: &BTreeMap>, title: &str) { bgp.med, bgp.as_path, bgp.stale, + bgp.origin, + bgp.internal, + router_id, ) .unwrap(); } @@ -186,3 +218,84 @@ fn print_bgp_routes(routes: &BTreeMap>, title: &str) { println!("{}", "=".repeat(title.len()).dimmed()); tw.flush().unwrap(); } + +fn opt_display(v: &Option) -> String { + match v { + Some(val) => val.to_string(), + None => "-".to_string(), + } +} + +fn print_static_routes_detail( + routes: &BTreeMap>, + title: &str, +) { + println!("{}", title.dimmed()); + println!("{}", "=".repeat(title.len()).dimmed()); + + for (prefix, paths) in routes.iter() { + println!("{prefix}"); + for path in paths.iter() { + let nexthop_display = match &path.nexthop_interface { + Some(iface) => { + format!("{}({})", iface, path.nexthop) + } + None => path.nexthop.to_string(), + }; + println!(" via {}:", nexthop_display); + println!( + " Nexthop Interface: {}", + opt_display(&path.nexthop_interface), + ); + println!(" RIB Priority: {}", path.rib_priority,); + println!(" Shutdown: {}", path.shutdown,); + println!(" VLAN ID: {}", opt_display(&path.vlan_id),); + } + } +} + +fn print_bgp_routes_detail(routes: &BTreeMap>, title: &str) { + println!("{}", title.dimmed()); + println!("{}", "=".repeat(title.len()).dimmed()); + + for (prefix, paths) in routes.iter() { + println!("{prefix}"); + for path in paths.iter() { + let bgp = path.bgp.as_ref().unwrap(); + let peer_str = match &bgp.peer { + rdb_types::PeerId::Ip(ip) => ip.to_string(), + rdb_types::PeerId::Interface(iface) => iface.clone(), + }; + println!(" via {}:", peer_str); + println!(" Nexthop: {}", path.nexthop,); + println!( + " Nexthop Interface: {}", + opt_display(&path.nexthop_interface), + ); + println!(" Origin: {}", bgp.origin,); + println!(" Origin AS: {}", bgp.origin_as,); + println!(" Internal: {}", bgp.internal,); + println!(" Router ID: {}", Ipv4Addr::from(bgp.id),); + println!(" Local Pref: {}", opt_display(&bgp.local_pref),); + println!(" MED: {}", opt_display(&bgp.med),); + let as_path_str = bgp + .as_path + .iter() + .map(|asn| asn.to_string()) + .collect::>() + .join(" "); + println!( + " AS Path: {}", + if as_path_str.is_empty() { + "-".to_string() + } else { + as_path_str + }, + ); + println!(" RIB Priority: {}", path.rib_priority,); + println!(" Shutdown: {}", path.shutdown,); + println!(" Stale: {}", opt_display(&bgp.stale),); + println!(" VLAN ID: {}", opt_display(&path.vlan_id),); + } + } +} diff --git a/mgadm/src/rib.rs b/mgadm/src/rib.rs index ce91b3081..6fd6e9be9 100644 --- a/mgadm/src/rib.rs +++ b/mgadm/src/rib.rs @@ -3,13 +3,22 @@ // file, You can obtain one at https://mozilla.org/MPL/2.0/. use anyhow::Result; -use clap::{Args, Subcommand}; +use clap::{Args, Subcommand, ValueEnum}; use mg_admin_client::types::BestpathFanoutRequest; use mg_admin_client::{Client, print_rib}; use mg_common::println_nopipe; use rdb::types::{AddressFamily, ProtocolFilter}; use std::num::NonZeroU8; +#[derive(Clone, Debug, ValueEnum)] +#[allow(non_camel_case_types)] +pub enum RibDisplayMode { + /// Display summary information (default). + summary, + /// Display detailed information. + detail, +} + #[derive(Subcommand, Debug)] pub enum Commands { /// View RIB state @@ -36,6 +45,12 @@ pub enum StatusCmd { /// Protocol filter (optional) #[arg(value_enum)] protocol: Option, + /// Display mode: summary (default) or detail. + #[clap(long, value_enum, default_value = "summary")] + mode: RibDisplayMode, + /// Exact-match prefix filter (e.g. 10.0.0.0/24) + #[clap(long)] + prefix: Option, }, /// Get the loc-rib table. Contains only valid routes and their @@ -47,6 +62,12 @@ pub enum StatusCmd { /// Protocol filter (optional) #[arg(value_enum)] protocol: Option, + /// Display mode: summary (default) or detail. + #[clap(long, value_enum, default_value = "summary")] + mode: RibDisplayMode, + /// Exact-match prefix filter (e.g. 10.0.0.0/24) + #[clap(long)] + prefix: Option, }, } @@ -80,11 +101,23 @@ pub async fn commands(command: Commands, c: Client) -> Result<()> { StatusCmd::Imported { address_family, protocol, - } => get_imported(c, address_family, protocol).await?, + mode, + prefix, + } => { + let detail = matches!(mode, RibDisplayMode::detail); + get_imported(c, address_family, protocol, detail, prefix) + .await? + } StatusCmd::Selected { address_family, protocol, - } => get_selected(c, address_family, protocol).await?, + mode, + prefix, + } => { + let detail = matches!(mode, RibDisplayMode::detail); + get_selected(c, address_family, protocol, detail, prefix) + .await? + } }, } Ok(()) @@ -94,13 +127,21 @@ async fn get_imported( c: Client, address_family: Option, protocol: Option, + detail: bool, + prefix: Option, ) -> Result<()> { let imported = c .get_rib_imported_v3(address_family.as_ref(), protocol.as_ref()) .await? .into_inner(); - print_rib(imported, address_family, protocol); + print_rib( + imported, + address_family, + protocol, + detail, + prefix.as_deref(), + ); Ok(()) } @@ -108,13 +149,21 @@ async fn get_selected( c: Client, address_family: Option, protocol: Option, + detail: bool, + prefix: Option, ) -> Result<()> { let selected = c .get_rib_selected_v3(address_family.as_ref(), protocol.as_ref()) .await? .into_inner(); - print_rib(selected, address_family, protocol); + print_rib( + selected, + address_family, + protocol, + detail, + prefix.as_deref(), + ); Ok(()) } From 5a13cb670137335acc6a004c7800772d69395c59 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Fri, 27 Feb 2026 11:15:22 -0700 Subject: [PATCH 29/58] Add prefix query for rib status Instead of fetching the full RIB and filtering client-side, add a prefix query parameter to the RIB imported/selected status endpoints. This allows a client to query a specific prefix from the RIB instead of unconditionally pulling the full (potentially filtered) RIB every time. --- falcon-lab/src/test.rs | 8 +-- mg-admin-client/src/lib.rs | 41 ++++--------- mg-api/src/lib.rs | 25 ++++++-- mgadm/src/rib.rs | 58 ++++++++++--------- mgd/src/admin.rs | 8 +-- mgd/src/rib_admin.rs | 42 ++++++++++---- ...ec3815.json => mg-admin-7.0.0-a3f1ca.json} | 18 ++++++ openapi/mg-admin/mg-admin-latest.json | 2 +- rdb/src/db.rs | 22 +++++++ 9 files changed, 142 insertions(+), 82 deletions(-) rename openapi/mg-admin/{mg-admin-7.0.0-ec3815.json => mg-admin-7.0.0-a3f1ca.json} (99%) diff --git a/falcon-lab/src/test.rs b/falcon-lab/src/test.rs index 3ebfd9bf6..dd0b7fba8 100644 --- a/falcon-lab/src/test.rs +++ b/falcon-lab/src/test.rs @@ -157,7 +157,7 @@ pub async fn run_trio_unnumbered_test( ); wait_for_eq!( - mgd.get_rib_imported_v3(Some(&AddressFamily::Ipv4), None) + mgd.get_rib_imported_v3(Some(&AddressFamily::Ipv4), None, None) .await .map(|x| x.len()) .unwrap_or(0), @@ -166,7 +166,7 @@ pub async fn run_trio_unnumbered_test( ); wait_for_eq!( - mgd.get_rib_imported_v3(Some(&AddressFamily::Ipv4), None) + mgd.get_rib_imported_v3(Some(&AddressFamily::Ipv4), None, None) .await .map(|x| x.values().nth(0).map(|x| x.len())) .unwrap_or(None), @@ -184,7 +184,7 @@ pub async fn run_trio_unnumbered_test( ); wait_for_eq!( - mgd.get_rib_imported_v3(Some(&AddressFamily::Ipv6), None) + mgd.get_rib_imported_v3(Some(&AddressFamily::Ipv6), None, None) .await .map(|x| x.len()) .unwrap_or(0), @@ -193,7 +193,7 @@ pub async fn run_trio_unnumbered_test( ); wait_for_eq!( - mgd.get_rib_imported_v3(Some(&AddressFamily::Ipv6), None) + mgd.get_rib_imported_v3(Some(&AddressFamily::Ipv6), None, None) .await .map(|x| x.values().nth(0).map(|x| x.len())) .unwrap_or(None), diff --git a/mg-admin-client/src/lib.rs b/mg-admin-client/src/lib.rs index 7cb0db3b5..28c44f8c1 100644 --- a/mg-admin-client/src/lib.rs +++ b/mg-admin-client/src/lib.rs @@ -29,37 +29,22 @@ progenitor::generate_api!( ); use colored::*; -use rdb_types::{AddressFamily, Prefix, ProtocolFilter}; +use rdb_types::Prefix; use std::collections::BTreeMap; use std::io::{Write, stdout}; use std::net::Ipv4Addr; use tabwriter::TabWriter; use types::{Path, Rib}; -pub fn print_rib( - rib: Rib, - address_family: Option, - protocol_filter: Option, - detail: bool, - prefix_filter: Option<&str>, -) { +pub fn print_rib(rib: Rib, detail: bool) { type CliRib = BTreeMap>; - // Always split into 4 collections let mut v4_static = CliRib::new(); let mut v4_bgp = CliRib::new(); let mut v6_static = CliRib::new(); let mut v6_bgp = CliRib::new(); - // Parse and categorize all routes for (prefix, paths) in rib.0.into_iter() { - // Client-side exact-match prefix filter - if let Some(filter) = prefix_filter - && prefix != filter - { - continue; - } - let pfx: Prefix = match prefix.parse() { Ok(p) => p, Err(e) => { @@ -91,36 +76,30 @@ pub fn print_rib( } } - let show_ipv4 = matches!(address_family, None | Some(AddressFamily::Ipv4)); - let show_ipv6 = matches!(address_family, None | Some(AddressFamily::Ipv6)); - let show_static = - matches!(protocol_filter, None | Some(ProtocolFilter::Static)); - let show_bgp = matches!(protocol_filter, None | Some(ProtocolFilter::Bgp)); - if detail { - if show_ipv4 && show_static && !v4_static.is_empty() { + if !v4_static.is_empty() { print_static_routes_detail(&v4_static, "Static Routes (IPv4)"); } - if show_ipv4 && show_bgp && !v4_bgp.is_empty() { + if !v4_bgp.is_empty() { print_bgp_routes_detail(&v4_bgp, "BGP Routes (IPv4)"); } - if show_ipv6 && show_static && !v6_static.is_empty() { + if !v6_static.is_empty() { print_static_routes_detail(&v6_static, "Static Routes (IPv6)"); } - if show_ipv6 && show_bgp && !v6_bgp.is_empty() { + if !v6_bgp.is_empty() { print_bgp_routes_detail(&v6_bgp, "BGP Routes (IPv6)"); } } else { - if show_ipv4 && show_static && !v4_static.is_empty() { + if !v4_static.is_empty() { print_static_routes(&v4_static, "Static Routes (IPv4)"); } - if show_ipv4 && show_bgp && !v4_bgp.is_empty() { + if !v4_bgp.is_empty() { print_bgp_routes(&v4_bgp, "BGP Routes (IPv4)"); } - if show_ipv6 && show_static && !v6_static.is_empty() { + if !v6_static.is_empty() { print_static_routes(&v6_static, "Static Routes (IPv6)"); } - if show_ipv6 && show_bgp && !v6_bgp.is_empty() { + if !v6_bgp.is_empty() { print_bgp_routes(&v6_bgp, "BGP Routes (IPv6)"); } } diff --git a/mg-api/src/lib.rs b/mg-api/src/lib.rs index 54f0bac32..5ee65dfee 100644 --- a/mg-api/src/lib.rs +++ b/mg-api/src/lib.rs @@ -472,28 +472,28 @@ pub trait MgAdminApi { #[endpoint { method = GET, path = "/rib/status/imported", versions = VERSION_IPV6_BASIC..VERSION_UNNUMBERED }] async fn get_rib_imported( rqctx: RequestContext, - request: Query, + request: Query, ) -> Result, HttpError>; // Original version (VERSION_IPV6_BASIC..VERSION_UNNUMBERED): BgpPathProperties.peer is IpAddr #[endpoint { method = GET, path = "/rib/status/selected", versions = VERSION_IPV6_BASIC..VERSION_UNNUMBERED }] async fn get_rib_selected( rqctx: RequestContext, - request: Query, + request: Query, ) -> Result, HttpError>; // VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC: PeerId but no origin/internal #[endpoint { method = GET, path = "/rib/status/imported", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] async fn get_rib_imported_v2( rqctx: RequestContext, - request: Query, + request: Query, ) -> Result, HttpError>; // VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC: PeerId but no origin/internal #[endpoint { method = GET, path = "/rib/status/selected", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] async fn get_rib_selected_v2( rqctx: RequestContext, - request: Query, + request: Query, ) -> Result, HttpError>; // VERSION_EXTENDED_NH_STATIC+: BgpPathProperties with origin/internal @@ -1253,6 +1253,20 @@ impl From for StaticRouteKey { } } +/// Previous version of RibQuery. +/// Used for API versions before VERSION_EXTENDED_NH_STATIC. +/// Delete when VERSION_EXTENDED_NH_STATIC is the minimum supported +/// version. +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +#[schemars(rename = "RibQuery")] +pub struct RibQueryV1 { + /// Filter by address family (None means all families) + #[serde(default)] + pub address_family: Option, + /// Filter by protocol (optional) + pub protocol: Option, +} + #[derive(Debug, Deserialize, Serialize, JsonSchema)] pub struct RibQuery { /// Filter by address family (None means all families) @@ -1260,6 +1274,9 @@ pub struct RibQuery { pub address_family: Option, /// Filter by protocol (optional) pub protocol: Option, + /// Exact-match prefix filter (e.g. "10.0.0.0/24") + #[serde(default)] + pub prefix: Option, } #[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)] diff --git a/mgadm/src/rib.rs b/mgadm/src/rib.rs index 6fd6e9be9..186333456 100644 --- a/mgadm/src/rib.rs +++ b/mgadm/src/rib.rs @@ -45,12 +45,12 @@ pub enum StatusCmd { /// Protocol filter (optional) #[arg(value_enum)] protocol: Option, - /// Display mode: summary (default) or detail. - #[clap(long, value_enum, default_value = "summary")] - mode: RibDisplayMode, /// Exact-match prefix filter (e.g. 10.0.0.0/24) - #[clap(long)] prefix: Option, + /// Display mode: summary (default) or detail. + /// Defaults to detail when a prefix is specified. + #[clap(long, value_enum)] + mode: Option, }, /// Get the loc-rib table. Contains only valid routes and their @@ -62,12 +62,12 @@ pub enum StatusCmd { /// Protocol filter (optional) #[arg(value_enum)] protocol: Option, - /// Display mode: summary (default) or detail. - #[clap(long, value_enum, default_value = "summary")] - mode: RibDisplayMode, /// Exact-match prefix filter (e.g. 10.0.0.0/24) - #[clap(long)] prefix: Option, + /// Display mode: summary (default) or detail. + /// Defaults to detail when a prefix is specified. + #[clap(long, value_enum)] + mode: Option, }, } @@ -101,20 +101,26 @@ pub async fn commands(command: Commands, c: Client) -> Result<()> { StatusCmd::Imported { address_family, protocol, - mode, prefix, + mode, } => { - let detail = matches!(mode, RibDisplayMode::detail); + let detail = match mode { + Some(m) => matches!(m, RibDisplayMode::detail), + None => prefix.is_some(), + }; get_imported(c, address_family, protocol, detail, prefix) .await? } StatusCmd::Selected { address_family, protocol, - mode, prefix, + mode, } => { - let detail = matches!(mode, RibDisplayMode::detail); + let detail = match mode { + Some(m) => matches!(m, RibDisplayMode::detail), + None => prefix.is_some(), + }; get_selected(c, address_family, protocol, detail, prefix) .await? } @@ -131,17 +137,15 @@ async fn get_imported( prefix: Option, ) -> Result<()> { let imported = c - .get_rib_imported_v3(address_family.as_ref(), protocol.as_ref()) + .get_rib_imported_v3( + address_family.as_ref(), + prefix.as_deref(), + protocol.as_ref(), + ) .await? .into_inner(); - print_rib( - imported, - address_family, - protocol, - detail, - prefix.as_deref(), - ); + print_rib(imported, detail); Ok(()) } @@ -153,17 +157,15 @@ async fn get_selected( prefix: Option, ) -> Result<()> { let selected = c - .get_rib_selected_v3(address_family.as_ref(), protocol.as_ref()) + .get_rib_selected_v3( + address_family.as_ref(), + prefix.as_deref(), + protocol.as_ref(), + ) .await? .into_inner(); - print_rib( - selected, - address_family, - protocol, - detail, - prefix.as_deref(), - ); + print_rib(selected, detail); Ok(()) } diff --git a/mgd/src/admin.rs b/mgd/src/admin.rs index 61f3bfaa3..fcdb2a7f6 100644 --- a/mgd/src/admin.rs +++ b/mgd/src/admin.rs @@ -477,28 +477,28 @@ impl MgAdminApi for MgAdminApiImpl { async fn get_rib_imported( ctx: RequestContext, - request: Query, + request: Query, ) -> Result, HttpError> { rib_admin::get_rib_imported(ctx, request).await } async fn get_rib_selected( ctx: RequestContext, - request: Query, + request: Query, ) -> Result, HttpError> { rib_admin::get_rib_selected(ctx, request).await } async fn get_rib_imported_v2( ctx: RequestContext, - request: Query, + request: Query, ) -> Result, HttpError> { rib_admin::get_rib_imported_v2(ctx, request).await } async fn get_rib_selected_v2( ctx: RequestContext, - request: Query, + request: Query, ) -> Result, HttpError> { rib_admin::get_rib_selected_v2(ctx, request).await } diff --git a/mgd/src/rib_admin.rs b/mgd/src/rib_admin.rs index a32353d95..0d592dfd2 100644 --- a/mgd/src/rib_admin.rs +++ b/mgd/src/rib_admin.rs @@ -8,15 +8,17 @@ use dropshot::{ RequestContext, TypedBody, }; use mg_api::{ - BestpathFanoutRequest, BestpathFanoutResponse, Rib, RibQuery, RibV1, RibV2, - filter_rib_by_protocol, + BestpathFanoutRequest, BestpathFanoutResponse, Rib, RibQuery, RibQueryV1, + RibV1, RibV2, filter_rib_by_protocol, }; +use rdb::Prefix; +use std::collections::BTreeMap; use std::sync::Arc; // Original version (VERSION_IPV6_BASIC..VERSION_UNNUMBERED): BgpPathProperties.peer is IpAddr pub async fn get_rib_imported( ctx: RequestContext>, - query: Query, + query: Query, ) -> Result, HttpError> { let ctx = ctx.context(); let query = query.into_inner(); @@ -27,7 +29,7 @@ pub async fn get_rib_imported( pub async fn get_rib_selected( ctx: RequestContext>, - query: Query, + query: Query, ) -> Result, HttpError> { let ctx = ctx.context(); let query = query.into_inner(); @@ -39,7 +41,7 @@ pub async fn get_rib_selected( // VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC (PeerId, no origin/internal) pub async fn get_rib_imported_v2( ctx: RequestContext>, - query: Query, + query: Query, ) -> Result, HttpError> { let ctx = ctx.context(); let query = query.into_inner(); @@ -50,7 +52,7 @@ pub async fn get_rib_imported_v2( pub async fn get_rib_selected_v2( ctx: RequestContext>, - query: Query, + query: Query, ) -> Result, HttpError> { let ctx = ctx.context(); let query = query.into_inner(); @@ -66,8 +68,18 @@ pub async fn get_rib_imported_v3( ) -> Result, HttpError> { let ctx = ctx.context(); let query = query.into_inner(); - let imported = ctx.db.full_rib(query.address_family); - let filtered = filter_rib_by_protocol(imported, query.protocol); + let rib = if let Some(ref prefix_str) = query.prefix { + let prefix: Prefix = prefix_str + .parse() + .map_err(|e: String| HttpError::for_bad_request(None, e))?; + match ctx.db.get_imported_prefix(&prefix) { + Some(paths) => BTreeMap::from([(prefix, paths)]), + None => BTreeMap::new(), + } + } else { + ctx.db.full_rib(query.address_family) + }; + let filtered = filter_rib_by_protocol(rib, query.protocol); Ok(HttpResponseOk(filtered.into())) } @@ -77,8 +89,18 @@ pub async fn get_rib_selected_v3( ) -> Result, HttpError> { let ctx = ctx.context(); let query = query.into_inner(); - let selected = ctx.db.loc_rib(query.address_family); - let filtered = filter_rib_by_protocol(selected, query.protocol); + let rib = if let Some(ref prefix_str) = query.prefix { + let prefix: Prefix = prefix_str + .parse() + .map_err(|e: String| HttpError::for_bad_request(None, e))?; + match ctx.db.get_selected_prefix(&prefix) { + Some(paths) => BTreeMap::from([(prefix, paths)]), + None => BTreeMap::new(), + } + } else { + ctx.db.loc_rib(query.address_family) + }; + let filtered = filter_rib_by_protocol(rib, query.protocol); Ok(HttpResponseOk(filtered.into())) } diff --git a/openapi/mg-admin/mg-admin-7.0.0-ec3815.json b/openapi/mg-admin/mg-admin-7.0.0-a3f1ca.json similarity index 99% rename from openapi/mg-admin/mg-admin-7.0.0-ec3815.json rename to openapi/mg-admin/mg-admin-7.0.0-a3f1ca.json index e19e65fba..275d0317c 100644 --- a/openapi/mg-admin/mg-admin-7.0.0-ec3815.json +++ b/openapi/mg-admin/mg-admin-7.0.0-a3f1ca.json @@ -1414,6 +1414,15 @@ "$ref": "#/components/schemas/AddressFamily" } }, + { + "in": "query", + "name": "prefix", + "description": "Exact-match prefix filter (e.g. \"10.0.0.0/24\")", + "schema": { + "nullable": true, + "type": "string" + } + }, { "in": "query", "name": "protocol", @@ -1455,6 +1464,15 @@ "$ref": "#/components/schemas/AddressFamily" } }, + { + "in": "query", + "name": "prefix", + "description": "Exact-match prefix filter (e.g. \"10.0.0.0/24\")", + "schema": { + "nullable": true, + "type": "string" + } + }, { "in": "query", "name": "protocol", diff --git a/openapi/mg-admin/mg-admin-latest.json b/openapi/mg-admin/mg-admin-latest.json index 794c50e92..ca375ed78 120000 --- a/openapi/mg-admin/mg-admin-latest.json +++ b/openapi/mg-admin/mg-admin-latest.json @@ -1 +1 @@ -mg-admin-7.0.0-ec3815.json \ No newline at end of file +mg-admin-7.0.0-a3f1ca.json \ No newline at end of file diff --git a/rdb/src/db.rs b/rdb/src/db.rs index 1796fb8fc..d38652fa2 100644 --- a/rdb/src/db.rs +++ b/rdb/src/db.rs @@ -223,6 +223,28 @@ impl Db { } } + /// Look up a single prefix in the imported (adj-rib-in) table. + pub fn get_imported_prefix( + &self, + prefix: &Prefix, + ) -> Option> { + match prefix { + Prefix::V4(p4) => lock!(self.rib4_in).get(p4).cloned(), + Prefix::V6(p6) => lock!(self.rib6_in).get(p6).cloned(), + } + } + + /// Look up a single prefix in the selected (loc-rib) table. + pub fn get_selected_prefix( + &self, + prefix: &Prefix, + ) -> Option> { + match prefix { + Prefix::V4(p4) => lock!(self.rib4_loc).get(p4).cloned(), + Prefix::V6(p6) => lock!(self.rib6_loc).get(p6).cloned(), + } + } + pub fn add_bgp_router( &self, asn: u32, From c9bde3b7138aad209df7a5e0ec484a8a8a37f51d Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Fri, 27 Feb 2026 11:43:35 -0700 Subject: [PATCH 30/58] bfd: add ipv6 peers to bfd tests --- bfd/src/lib.rs | 71 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 11 deletions(-) diff --git a/bfd/src/lib.rs b/bfd/src/lib.rs index 5a3d45d34..e0ed8abf2 100644 --- a/bfd/src/lib.rs +++ b/bfd/src/lib.rs @@ -326,17 +326,34 @@ mod test { let mut daemon = Daemon::new(log); assert_eq!(daemon.sessions.len(), 0); + // Add an IPv4 peer. let (a, _b) = bidi::channel(); - let p1_addr = ip("203.0.113.10"); + let v4_addr = ip("203.0.113.10"); daemon.add_peer( - p1_addr, + v4_addr, Duration::from_secs(5), 3, SessionMode::MultiHop, a, db.db().clone(), ); - assert_eq!(daemon.peer_state(p1_addr), Some(BfdPeerState::Down)); + assert_eq!(daemon.peer_state(v4_addr), Some(BfdPeerState::Down),); + + // Add an IPv6 peer to the same daemon. + let (a, _b) = bidi::channel(); + let v6_addr = ip("2001:db8::10"); + daemon.add_peer( + v6_addr, + Duration::from_secs(5), + 3, + SessionMode::MultiHop, + a, + db.db().clone(), + ); + assert_eq!(daemon.peer_state(v6_addr), Some(BfdPeerState::Down),); + + // Both peers coexist. + assert_eq!(daemon.sessions.len(), 2); Ok(()) } @@ -349,39 +366,71 @@ mod test { let mut net = Network::default(); - let addr1 = ip("203.0.113.10"); - let addr2 = ip("203.0.113.20"); + // IPv4 peer pair. + let v4_addr1 = ip("203.0.113.10"); + let v4_addr2 = ip("203.0.113.20"); + // IPv6 peer pair. + let v6_addr1 = ip("2001:db8::10"); + let v6_addr2 = ip("2001:db8::20"); + + // Daemon 1 peers with both v4 and v6 counterparts. let mut d1 = Daemon::new(test_logger()); let (a, b) = bidi::channel(); d1.add_peer( - addr1, + v4_addr1, Duration::from_secs(5), 3, SessionMode::MultiHop, a, db.db().clone(), ); - net.register(addr2, b); + net.register(v4_addr2, b); + let (a, b) = bidi::channel(); + d1.add_peer( + v6_addr1, + Duration::from_secs(5), + 3, + SessionMode::MultiHop, + a, + db.db().clone(), + ); + net.register(v6_addr2, b); + + // Daemon 2 peers with both v4 and v6 counterparts. let mut d2 = Daemon::new(test_logger()); let (a, b) = bidi::channel(); d2.add_peer( - addr2, + v4_addr2, + Duration::from_secs(5), + 3, + SessionMode::MultiHop, + a, + db.db().clone(), + ); + net.register(v4_addr1, b); + + let (a, b) = bidi::channel(); + d2.add_peer( + v6_addr2, Duration::from_secs(5), 3, SessionMode::MultiHop, a, db.db().clone(), ); - net.register(addr1, b); + net.register(v6_addr1, b); net.run(); sleep(Duration::from_secs(10)); - assert_eq!(d1.peer_state(addr1), Some(BfdPeerState::Up)); - assert_eq!(d2.peer_state(addr2), Some(BfdPeerState::Up)); + // All four sessions should reach Up. + assert_eq!(d1.peer_state(v4_addr1), Some(BfdPeerState::Up),); + assert_eq!(d1.peer_state(v6_addr1), Some(BfdPeerState::Up),); + assert_eq!(d2.peer_state(v4_addr2), Some(BfdPeerState::Up),); + assert_eq!(d2.peer_state(v6_addr2), Some(BfdPeerState::Up),); Ok(()) } From 6bf4b69d52dfd93c3d46bd38243e7698c164ee3d Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Sun, 1 Mar 2026 23:42:08 -0700 Subject: [PATCH 31/58] bgp: remove update filtering of originated routes Removes the filtering of originated routes from a received UPDATE. Scenario: 1. Peer advertises us a route that we are originating and we discard it. 2. We stop advertising that route. 3. Us withdrawing our advertisement doesn't cause the peer to choose a different bestpath for that route. 4. We no longer have a path for that route from this peer. This could also be addressed by: A) Sending a route-refresh to the peer when we stop originating routes. B) Inserting a Local path into the RIB for routes we are originating which is preferred over BGP paths learned from peers during bestpath. (A) is not a good idea because we don't know how large the peer's RIB will be and we'd have to re-process their entire RIB every time we stop advertising even a single route. (B) is fine conceptually, as most routing stacks use this approach: When using a "network" statement to pull routes from other protocols into the BGP topology or creating an aggregate, a locally originated path generally goes into the BGP LOC-RIB that is preferred over any peer-learned path. If an existing routing table entry doesn't exist, then this generally creates a blackhole route for the originated prefix (in order to prevent routing loops from occurring as a result of missing entries for the component routes). In the case of maghemite, well, really in the case of dendrite, this routing loop prevention is done without using blackhole routes: packets arriving on a front panel port that enocounter a NAT "miss" are dropped rather than submitted to an LPM lookup in the External routing table (avoiding us becoming a transit router + hairpinning packets sent to us from outside the rack). All that to say: we don't need local routes for any kind of forwarding (or blackholing) data plane behavior. This brings us to the crux of the issue: Inter-VPC routing. Since we don't currently have a native inter-VPC data plane within the rack, then we need to have installed an External route that covers our originated prefix regardless of what mask is used. If we do an exact match check and filtering, then we just make it harder for our peer to advertise us a route we need. We effectively require the peer to advertise us reachability to our own prefix, so long as the mask doesn't match what we're using (e.g. if we send a /24, they have to send us any mask other than a /24 that covers the original IP range). So while removing this filter doesn't address the issues inherent to us needing the peer to provide us a route back to our own address space, it does make it simpler for us to learn that route (no longer needing a route with anything but our own mask) and to avoid extra logic to ensure we can recover routes that we filtered. Fixes: #583 --- bgp/src/session.rs | 86 +++++----------------------------------------- 1 file changed, 9 insertions(+), 77 deletions(-) diff --git a/bgp/src/session.rs b/bgp/src/session.rs index abda1a6ac..ed17dbaf3 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -8910,29 +8910,13 @@ impl SessionRunner { fn update_rib(&self, update: &UpdateMessage, pc: &PeerConnection) { let treat_as_withdraw = update.treat_as_withdraw(); - let originated4 = match self.db.get_origin(Some(AddressFamily::Ipv4)) { - Ok(value) => value, - Err(e) => { - session_log!( - self, - error, - pc.conn, - "failed to get originated ipv4 routes from db: {e}"; - "error" => format!("{e}") - ); - Vec::new() - } - }; - // Traditional NLRI / withdrawn routes fields are IPv4 Unicast // only. Only process them if IPv4 Unicast was negotiated. if pc.ipv4_unicast.negotiated() { let withdrawn: Vec = update .withdrawn .iter() - .filter(|p| { - !originated4.contains(&Prefix::V4(**p)) && p.valid_for_rib() - }) + .filter(|p| p.valid_for_rib()) .copied() .map(Prefix::V4) .collect(); @@ -8960,10 +8944,7 @@ impl SessionRunner { let nlri_as_withdrawn: Vec = update .nlri .iter() - .filter(|p| { - !originated4.contains(&Prefix::V4(**p)) - && p.valid_for_rib() - }) + .filter(|p| p.valid_for_rib()) .copied() .map(Prefix::V4) .collect(); @@ -8980,8 +8961,7 @@ impl SessionRunner { .nlri .iter() .filter(|p| { - !originated4.contains(&Prefix::V4(**p)) - && p.valid_for_rib() + p.valid_for_rib() && !self.prefix_via_self(Prefix::V4(**p), nexthop) }) .copied() @@ -9061,10 +9041,7 @@ impl SessionRunner { let mp_withdrawn4: Vec = reach4 .nlri .iter() - .filter(|p| { - !originated4.contains(&Prefix::V4(**p)) - && p.valid_for_rib() - }) + .filter(|p| p.valid_for_rib()) .copied() .map(Prefix::V4) .collect(); @@ -9093,8 +9070,7 @@ impl SessionRunner { .nlri .iter() .filter(|p| { - !originated4.contains(&Prefix::V4(**p)) - && p.valid_for_rib() + p.valid_for_rib() && !self.prefix_via_self( Prefix::V4(**p), mp_nexthop, @@ -9144,32 +9120,12 @@ impl SessionRunner { } } MpReachNlri::Ipv6Unicast(reach6) => { - let originated6 = match self - .db - .get_origin(Some(AddressFamily::Ipv6)) - { - Ok(value) => value, - Err(e) => { - session_log!( - self, - error, - pc.conn, - "failed to get originated ipv6 routes from db: {e}"; - "error" => format!("{e}") - ); - Vec::new() - } - }; - if treat_as_withdraw { // RFC 7606: treat MP_REACH NLRI as withdrawals let withdrawn6: Vec = reach6 .nlri .iter() - .filter(|p| { - !originated6.contains(&Prefix::V6(**p)) - && p.valid_for_rib() - }) + .filter(|p| p.valid_for_rib()) .copied() .map(Prefix::V6) .collect(); @@ -9209,8 +9165,7 @@ impl SessionRunner { .nlri .iter() .filter(|p| { - !originated6.contains(&Prefix::V6(**p)) - && p.valid_for_rib() + p.valid_for_rib() && !self.prefix_via_self( Prefix::V6(**p), nexthop6, @@ -9269,10 +9224,7 @@ impl SessionRunner { let mp_withdrawn4: Vec = unreach4 .withdrawn .iter() - .filter(|p| { - !originated4.contains(&Prefix::V4(**p)) - && p.valid_for_rib() - }) + .filter(|p| p.valid_for_rib()) .copied() .map(Prefix::V4) .collect(); @@ -9287,30 +9239,10 @@ impl SessionRunner { ); } MpUnreachNlri::Ipv6Unicast(unreach6) => { - let originated6 = match self - .db - .get_origin(Some(AddressFamily::Ipv6)) - { - Ok(value) => value, - Err(e) => { - session_log!( - self, - error, - pc.conn, - "failed to get originated ipv6 routes for withdrawal: {e}"; - "error" => format!("{e}") - ); - Vec::new() - } - }; - let withdrawn6: Vec = unreach6 .withdrawn .iter() - .filter(|p| { - !originated6.contains(&Prefix::V6(**p)) - && p.valid_for_rib() - }) + .filter(|p| p.valid_for_rib()) .copied() .map(Prefix::V6) .collect(); From c16a85b3ac94614c3c3e7355f48b8de6507c1081 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Mon, 2 Mar 2026 00:34:01 -0700 Subject: [PATCH 32/58] Add justfile Adds a justfile for running simple build, test and verification commands with some logic in place for platform-specific exclusions. Updates dev docs to mention justfile for folks who want to use it. --- docs/developer-guide.md | 15 ++++++------- justfile | 49 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 justfile diff --git a/docs/developer-guide.md b/docs/developer-guide.md index 16f842aac..32eb2710f 100644 --- a/docs/developer-guide.md +++ b/docs/developer-guide.md @@ -96,6 +96,13 @@ support for illumos/Helios and Dendrite/Sidecar data planes. - **`mg-ddm-verify`** — DDM verification tool. - **`docs/`** — Architecture and developer docs. +## Justfile + +Common workflows are available as `just` recipes. The justfile +automatically excludes illumos-only crates (`ddm`, `ddmd`, +`falcon-lab`, `lab`) on non-illumos platforms. Run `just --list` +to see all available recipes. + ## Building The build has ordering constraints — OpenAPI client generation @@ -137,16 +144,8 @@ their respective environments (containerlab, Docker, libfalcon). All work should compile and pass these checks before submitting: ```bash -# Compilation (all targets including tests/benches) -cargo check --all-targets - -# Clippy with warnings as errors cargo clippy --all-targets -- --deny warnings - -# Format check cargo fmt --all --check - -# Verify OpenAPI specs are up to date cargo xtask openapi check ``` diff --git a/justfile b/justfile new file mode 100644 index 000000000..9a48948b4 --- /dev/null +++ b/justfile @@ -0,0 +1,49 @@ +# On illumos, only mg-tests (ztest-based) needs exclusion. On other +# platforms, exclude all illumos-only crates. +build_excludes := if os() == "illumos" { + "" +} else { + "--exclude ddm --exclude ddmd --exclude falcon-lab --exclude lab" +} + +test_excludes := build_excludes + " --exclude mg-tests" + +# Build the workspace. +build: + cargo build --workspace {{ build_excludes }} + +# Run cargo test for the workspace. +test: + cargo test --workspace {{ test_excludes }} + +# Run cargo nextest for the workspace. +nextest: + cargo nextest run --workspace {{ test_excludes }} + +# Type-check all targets. +check: + cargo check --all-targets + +# Run clippy with warnings as errors. +clippy: + cargo clippy --all-targets -- --deny warnings + +# Check formatting. +fmt-check: + cargo fmt --all --check + +# Apply formatting. +fmt: + cargo fmt --all + +# Generate OpenAPI specs (requires mgd to be built first). +openapi-generate: + cargo build --bin mgd + cargo xtask openapi generate + +# Verify OpenAPI specs are up to date. +openapi-check: + cargo xtask openapi check + +# Run all verification checks. +verify: clippy fmt-check openapi-check From f164536e26961049332c17e7cddc3a27e00d2a39 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Mon, 9 Mar 2026 17:41:42 -0600 Subject: [PATCH 33/58] bgp: use socket2 for TTL and DSCP socket options Replace unsafe calls to libc::{setsockopt,getsockopt} in apply_dscp, set_outgoing_ttl, and related tests with safe socket2::SockRef equivalents. Options without socket2 support (IP_MINTTL, IPV6_MINHOPCOUNT, TCP_MD5SIG) remain as raw setsockopt calls behind platform cfg gates. --- bgp/src/connection_tcp.rs | 138 ++++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 66 deletions(-) diff --git a/bgp/src/connection_tcp.rs b/bgp/src/connection_tcp.rs index 6df9b8d47..8132bb50f 100644 --- a/bgp/src/connection_tcp.rs +++ b/bgp/src/connection_tcp.rs @@ -25,17 +25,17 @@ use crate::{ }, unnumbered::UnnumberedManager, }; -use libc::{IPPROTO_IP, IPPROTO_IPV6, c_void}; use mg_common::lock; use rdb::Dscp; use slog::Logger; +use socket2::SockRef; use std::{ collections::BTreeMap, io::Read, io::Write, net::{SocketAddr, TcpListener, TcpStream, ToSocketAddrs}, num::NonZeroU8, - os::fd::AsRawFd, + os::fd::AsFd, sync::atomic::AtomicBool, sync::{Arc, Mutex, atomic::Ordering, mpsc::Sender}, thread::{JoinHandle, sleep}, @@ -43,7 +43,7 @@ use std::{ }; #[cfg(any(target_os = "linux", target_os = "illumos"))] -use libc::{IPPROTO_TCP, c_int}; +use libc::{IPPROTO_IP, IPPROTO_IPV6, IPPROTO_TCP, c_int, c_void}; #[cfg(target_os = "linux")] use crate::connection::MAX_MD5SIG_KEYLEN; @@ -53,7 +53,7 @@ use libc::{IP_MINTTL, TCP_MD5SIG, sockaddr_storage}; #[cfg(target_os = "illumos")] use itertools::Itertools; #[cfg(target_os = "illumos")] -use std::{collections::HashSet, net::IpAddr}; +use std::{collections::HashSet, net::IpAddr, os::fd::AsRawFd}; const UNIT_CONNECTION: &str = "connection_tcp"; @@ -1255,30 +1255,11 @@ fn apply_dscp( peer: SocketAddr, ) -> Result<(), Error> { let tos = u32::from(dscp.tos_byte()); - let fd = conn.as_raw_fd(); - unsafe { - if peer.is_ipv4() - && libc::setsockopt( - fd, - IPPROTO_IP, - libc::IP_TOS, - &tos as *const u32 as *const c_void, - std::mem::size_of::() as u32, - ) != 0 - { - return Err(Error::Io(std::io::Error::last_os_error())); - } - if peer.is_ipv6() - && libc::setsockopt( - fd, - IPPROTO_IPV6, - libc::IPV6_TCLASS, - &tos as *const u32 as *const c_void, - std::mem::size_of::() as u32, - ) != 0 - { - return Err(Error::Io(std::io::Error::last_os_error())); - } + let sock = SockRef::from(conn); + if peer.is_ipv4() { + sock.set_tos(tos).map_err(Error::Io)?; + } else { + sock.set_tclass_v6(tos).map_err(Error::Io)?; } Ok(()) } @@ -1289,40 +1270,15 @@ fn apply_dscp( /// `AsRawFd`). Uses `IP_TTL` for IPv4 and `IPV6_UNICAST_HOPS` for /// IPv6. fn set_outgoing_ttl( - sock: &impl AsRawFd, + sock: &impl AsFd, ttl: u8, addr: SocketAddr, ) -> Result<(), Error> { + let s = SockRef::from(sock); if addr.is_ipv4() { - let val = ttl as u32; - let fd = sock.as_raw_fd(); - let rc = unsafe { - libc::setsockopt( - fd, - IPPROTO_IP, - libc::IP_TTL, - &val as *const u32 as *const c_void, - std::mem::size_of::() as u32, - ) - }; - if rc != 0 { - return Err(Error::Io(std::io::Error::last_os_error())); - } + s.set_ttl(ttl as u32).map_err(Error::Io)?; } else { - let val: i32 = ttl.into(); - let fd = sock.as_raw_fd(); - let rc = unsafe { - libc::setsockopt( - fd, - IPPROTO_IPV6, - libc::IPV6_UNICAST_HOPS, - &val as *const i32 as *const c_void, - std::mem::size_of::() as u32, - ) - }; - if rc != 0 { - return Err(Error::Io(std::io::Error::last_os_error())); - } + s.set_unicast_hops_v6(ttl as u32).map_err(Error::Io)?; } Ok(()) } @@ -1655,30 +1611,81 @@ mod tests { let dscp = Dscp::new(48).unwrap(); apply_dscp(&stream, dscp, addr).unwrap(); + let readback = SockRef::from(&stream).tos().unwrap(); + // DSCP 48 → TOS byte = 48 << 2 = 192 + assert_eq!(readback, u32::from(dscp.tos_byte())); + } + + #[test] + fn apply_dscp_sets_ipv6_tclass() { + let listener = TcpListener::bind("[::1]:0").unwrap(); + let addr = listener.local_addr().unwrap(); + let stream = TcpStream::connect(addr).unwrap(); + + let dscp = Dscp::new(46).unwrap(); // EF + apply_dscp(&stream, dscp, addr).unwrap(); + + let readback = SockRef::from(&stream).tclass_v6().unwrap(); + // DSCP 46 (EF) → TOS byte = 46 << 2 = 184 + assert_eq!(readback, u32::from(dscp.tos_byte())); + } + + #[test] + fn set_outgoing_ttl_ipv4() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap(); + + set_outgoing_ttl(&listener, 42, addr).unwrap(); + + let readback = SockRef::from(&listener).ttl().unwrap(); + assert_eq!(readback, 42); + } + + #[test] + fn set_outgoing_ttl_ipv6() { + let listener = TcpListener::bind("[::1]:0").unwrap(); + let addr = listener.local_addr().unwrap(); + + set_outgoing_ttl(&listener, 42, addr).unwrap(); + + let readback = SockRef::from(&listener).unicast_hops_v6().unwrap(); + assert_eq!(readback, 42); + } + + // IP_MINTTL is not implemented on MacOS + #[cfg(any(target_os = "linux", target_os = "illumos"))] + #[test] + fn set_ip_minttl_ipv4() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap(); + let stream = TcpStream::connect(addr).unwrap(); + + set_ip_minttl(&stream, 200, addr).unwrap(); + let mut readback: u32 = 0; let mut len = std::mem::size_of::() as libc::socklen_t; let rc = unsafe { libc::getsockopt( stream.as_raw_fd(), IPPROTO_IP, - libc::IP_TOS, + IP_MINTTL, &mut readback as *mut u32 as *mut c_void, &mut len, ) }; assert_eq!(rc, 0, "getsockopt failed"); - // DSCP 48 → TOS byte = 48 << 2 = 192 - assert_eq!(readback, u32::from(dscp.tos_byte())); + assert_eq!(readback, 200); } + // IPV6_MINHOPCOUNT is not implemented on MacOS + #[cfg(any(target_os = "linux", target_os = "illumos"))] #[test] - fn apply_dscp_sets_ipv6_tclass() { + fn set_ip_minttl_ipv6() { let listener = TcpListener::bind("[::1]:0").unwrap(); let addr = listener.local_addr().unwrap(); let stream = TcpStream::connect(addr).unwrap(); - let dscp = Dscp::new(46).unwrap(); // EF - apply_dscp(&stream, dscp, addr).unwrap(); + set_ip_minttl(&stream, 200, addr).unwrap(); let mut readback: u32 = 0; let mut len = std::mem::size_of::() as libc::socklen_t; @@ -1686,13 +1693,12 @@ mod tests { libc::getsockopt( stream.as_raw_fd(), IPPROTO_IPV6, - libc::IPV6_TCLASS, + IPV6_MINHOPCOUNT, &mut readback as *mut u32 as *mut c_void, &mut len, ) }; assert_eq!(rc, 0, "getsockopt failed"); - // DSCP 46 (EF) → TOS byte = 46 << 2 = 184 - assert_eq!(readback, u32::from(dscp.tos_byte())); + assert_eq!(readback, 200); } } From b97d1d8d7054a941f2618f58056b6c2bb6a22084 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Mon, 9 Mar 2026 19:47:16 -0600 Subject: [PATCH 34/58] bgp: revert socket2 wrappers for DSCP/TCLASS socket2 has SockRef methods for setting DSCP/TCLASS values for IPv4/IPv6 headers, but not for illumos. Revert back to using the unsafe libc *sockopt functions for setting and testing those functions. --- bgp/src/connection_tcp.rs | 63 ++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/bgp/src/connection_tcp.rs b/bgp/src/connection_tcp.rs index 8132bb50f..fce687009 100644 --- a/bgp/src/connection_tcp.rs +++ b/bgp/src/connection_tcp.rs @@ -35,15 +35,17 @@ use std::{ io::Write, net::{SocketAddr, TcpListener, TcpStream, ToSocketAddrs}, num::NonZeroU8, - os::fd::AsFd, + os::fd::{AsFd, AsRawFd}, sync::atomic::AtomicBool, sync::{Arc, Mutex, atomic::Ordering, mpsc::Sender}, thread::{JoinHandle, sleep}, time::{Duration, Instant}, }; +use libc::{IPPROTO_IP, IPPROTO_IPV6, c_void}; + #[cfg(any(target_os = "linux", target_os = "illumos"))] -use libc::{IPPROTO_IP, IPPROTO_IPV6, IPPROTO_TCP, c_int, c_void}; +use libc::{IPPROTO_TCP, c_int}; #[cfg(target_os = "linux")] use crate::connection::MAX_MD5SIG_KEYLEN; @@ -53,7 +55,7 @@ use libc::{IP_MINTTL, TCP_MD5SIG, sockaddr_storage}; #[cfg(target_os = "illumos")] use itertools::Itertools; #[cfg(target_os = "illumos")] -use std::{collections::HashSet, net::IpAddr, os::fd::AsRawFd}; +use std::{collections::HashSet, net::IpAddr}; const UNIT_CONNECTION: &str = "connection_tcp"; @@ -1255,11 +1257,30 @@ fn apply_dscp( peer: SocketAddr, ) -> Result<(), Error> { let tos = u32::from(dscp.tos_byte()); - let sock = SockRef::from(conn); - if peer.is_ipv4() { - sock.set_tos(tos).map_err(Error::Io)?; - } else { - sock.set_tclass_v6(tos).map_err(Error::Io)?; + let fd = conn.as_raw_fd(); + unsafe { + if peer.is_ipv4() + && libc::setsockopt( + fd, + IPPROTO_IP, + libc::IP_TOS, + &tos as *const u32 as *const c_void, + std::mem::size_of::() as u32, + ) != 0 + { + return Err(Error::Io(std::io::Error::last_os_error())); + } + if peer.is_ipv6() + && libc::setsockopt( + fd, + IPPROTO_IPV6, + libc::IPV6_TCLASS, + &tos as *const u32 as *const c_void, + std::mem::size_of::() as u32, + ) != 0 + { + return Err(Error::Io(std::io::Error::last_os_error())); + } } Ok(()) } @@ -1611,7 +1632,18 @@ mod tests { let dscp = Dscp::new(48).unwrap(); apply_dscp(&stream, dscp, addr).unwrap(); - let readback = SockRef::from(&stream).tos().unwrap(); + let mut readback: u32 = 0; + let mut len = std::mem::size_of::() as libc::socklen_t; + let rc = unsafe { + libc::getsockopt( + stream.as_raw_fd(), + IPPROTO_IP, + libc::IP_TOS, + &mut readback as *mut u32 as *mut c_void, + &mut len, + ) + }; + assert_eq!(rc, 0, "getsockopt failed"); // DSCP 48 → TOS byte = 48 << 2 = 192 assert_eq!(readback, u32::from(dscp.tos_byte())); } @@ -1625,7 +1657,18 @@ mod tests { let dscp = Dscp::new(46).unwrap(); // EF apply_dscp(&stream, dscp, addr).unwrap(); - let readback = SockRef::from(&stream).tclass_v6().unwrap(); + let mut readback: u32 = 0; + let mut len = std::mem::size_of::() as libc::socklen_t; + let rc = unsafe { + libc::getsockopt( + stream.as_raw_fd(), + IPPROTO_IPV6, + libc::IPV6_TCLASS, + &mut readback as *mut u32 as *mut c_void, + &mut len, + ) + }; + assert_eq!(rc, 0, "getsockopt failed"); // DSCP 46 (EF) → TOS byte = 46 << 2 = 184 assert_eq!(readback, u32::from(dscp.tos_byte())); } From 1f3821023458889ec951fd3c9fe7fefd20726730 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Tue, 10 Mar 2026 17:53:52 -0600 Subject: [PATCH 35/58] bgp: implement BGP Extended Message Size Adds support for BGP Extended Message Size Capability (RFC 8654). --- bgp/src/connection.rs | 6 + bgp/src/connection_channel.rs | 12 + bgp/src/connection_tcp.rs | 75 ++- bgp/src/messages.rs | 157 +++++- bgp/src/proptest.rs | 481 +++++++++++++----- bgp/src/session.rs | 39 +- mg-common/src/test.rs | 14 + ...a3f1ca.json => mg-admin-7.0.0-af3143.json} | 2 +- openapi/mg-admin/mg-admin-latest.json | 2 +- 9 files changed, 611 insertions(+), 177 deletions(-) rename openapi/mg-admin/{mg-admin-7.0.0-a3f1ca.json => mg-admin-7.0.0-af3143.json} (99%) diff --git a/bgp/src/connection.rs b/bgp/src/connection.rs index 6914db28c..6bbc1628a 100644 --- a/bgp/src/connection.rs +++ b/bgp/src/connection.rs @@ -221,6 +221,12 @@ pub trait BgpConnection: Send + Sync + Sized { /// Update a socket option on a live connection. fn update_socket_option(&self, option: &SocketOption) -> Result<(), Error>; + + /// Update the peer's support for BGP Extended Messages + fn set_extended_msg(&self, ext_msg_supported: bool); + + /// Indicates the peer's support for BGP Extended Messages + fn extended_msg(&self) -> bool; } /// A socket option that can be updated on a live BGP connection. diff --git a/bgp/src/connection_channel.rs b/bgp/src/connection_channel.rs index 2f725f0a5..d639816aa 100644 --- a/bgp/src/connection_channel.rs +++ b/bgp/src/connection_channel.rs @@ -307,6 +307,8 @@ pub struct BgpConnectionChannel { channel_id: u64, // Typestate managing the recv loop thread lifecycle (Ready or Running) recv_loop_state: Mutex, + // BGP Extended Message Size is supported for this peer. + extended_msg: Arc, } impl BgpConnection for BgpConnectionChannel { @@ -387,6 +389,15 @@ impl BgpConnection for BgpConnectionChannel { // Socket options are ignored for test connections Ok(()) } + + fn set_extended_msg(&self, ext_msg_supported: bool) { + self.extended_msg + .store(ext_msg_supported, Ordering::Relaxed) + } + + fn extended_msg(&self) -> bool { + self.extended_msg.load(Ordering::Relaxed) + } } impl BgpConnectionChannel { @@ -433,6 +444,7 @@ impl BgpConnectionChannel { recv_timeout: timeout, channel_id, recv_loop_state: Mutex::new(ThreadState::new()), + extended_msg: Arc::new(AtomicBool::new(false)), } } diff --git a/bgp/src/connection_tcp.rs b/bgp/src/connection_tcp.rs index 8800b8a18..4a77cc4c4 100644 --- a/bgp/src/connection_tcp.rs +++ b/bgp/src/connection_tcp.rs @@ -13,10 +13,10 @@ use crate::{ log::{connection_log, connection_log_lite}, messages::{ ErrorCode, ErrorSubcode, Header, HeaderErrorSubcode, HeaderParseError, - MAX_MESSAGE_SIZE, Message, MessageParseError, MessageType, - NotificationMessage, NotificationParseError, - NotificationParseErrorReason, OpenErrorSubcode, OpenMessage, - OpenParseError, OpenParseErrorReason, RouteRefreshMessage, + MAX_EXTENDED_MESSAGE_SIZE, MAX_MESSAGE_SIZE, Message, + MessageParseError, MessageType, NotificationMessage, + NotificationParseError, NotificationParseErrorReason, OpenErrorSubcode, + OpenMessage, OpenParseError, OpenParseErrorReason, RouteRefreshMessage, RouteRefreshParseError, RouteRefreshParseErrorReason, UpdateMessage, }, session::{ @@ -487,6 +487,8 @@ pub struct BgpConnectionTcp { recv_timeout: Duration, // Typestate managing the recv loop thread lifecycle (Ready or Running) recv_loop_state: Mutex, + // BGP Extended Message Size is supported for this peer. + extended_msg: Arc, } impl BgpConnection for BgpConnectionTcp { @@ -494,7 +496,13 @@ impl BgpConnection for BgpConnectionTcp { fn send(&self, msg: Message) -> Result<(), Error> { let mut guard = lock!(self.conn); - Self::send_msg(&mut guard, &self.log, self.direction, msg) + Self::send_msg( + &mut guard, + &self.log, + self.direction, + msg, + self.extended_msg(), + ) } fn peer(&self) -> SocketAddr { @@ -552,6 +560,15 @@ impl BgpConnection for BgpConnectionTcp { SocketOption::MinTtl(ttl) => apply_min_ttl(&guard, ttl, self.peer), } } + + fn set_extended_msg(&self, ext_msg_supported: bool) { + self.extended_msg + .store(ext_msg_supported, Ordering::Relaxed) + } + + fn extended_msg(&self) -> bool { + self.extended_msg.load(Ordering::Relaxed) + } } impl Drop for BgpConnectionTcp { @@ -616,6 +633,7 @@ impl BgpConnectionTcp { event_tx, recv_timeout: timeout, recv_loop_state: Mutex::new(ThreadState::new()), + extended_msg: Arc::new(AtomicBool::new(false)), }) } @@ -628,6 +646,7 @@ impl BgpConnectionTcp { let log = conn_arc.log.clone(); let direction = conn_arc.direction; let conn_id = conn_arc.id; + let extended_msg = conn_arc.extended_msg.clone(); // Try to clone the stream before spawning the thread // This way we can fail fast if the connection is already broken @@ -678,7 +697,7 @@ impl BgpConnectionTcp { ); break; } - match Self::recv_msg(&mut conn, dropped.clone(), &l, direction) + match Self::recv_msg(&mut conn, dropped.clone(), extended_msg.clone(), &l, direction) { Ok(msg) => { connection_log_lite!(l, trace, @@ -814,13 +833,16 @@ impl BgpConnectionTcp { fn recv_msg( stream: &mut TcpStream, dropped: Arc, + extended_msg: Arc, log: &Logger, direction: ConnectionDirection, ) -> Result { let hdr = Self::recv_header(stream, dropped.clone())?; + let hdr_len = usize::from(hdr.length); + let extended_msg = extended_msg.load(Ordering::Relaxed); // RFC 4271 §4.1: length must be between 19 and 4096 - if usize::from(hdr.length) < Header::WIRE_SIZE { + if hdr_len < Header::WIRE_SIZE { return Err(RecvError::Parse(MessageParseError::Header( HeaderParseError { error_code: ErrorCode::Header, @@ -831,7 +853,20 @@ impl BgpConnectionTcp { }, ))); } - if usize::from(hdr.length) > MAX_MESSAGE_SIZE { + + // Extended Message doesn't apply to Open and Keepalive messages. + // Only use Extended Message Size for supported message types if we've + // negotiated support for it with the peer. + let max_msg_size = if !extended_msg + || matches!(hdr.typ, MessageType::Open) + || matches!(hdr.typ, MessageType::KeepAlive) + { + MAX_MESSAGE_SIZE + } else { + MAX_EXTENDED_MESSAGE_SIZE + }; + + if hdr_len > max_msg_size { return Err(RecvError::Parse(MessageParseError::Header( HeaderParseError { error_code: ErrorCode::Header, @@ -843,7 +878,7 @@ impl BgpConnectionTcp { ))); } - let msg_len = usize::from(hdr.length) - Header::WIRE_SIZE; + let msg_len = hdr_len - Header::WIRE_SIZE; let mut msgbuf = vec![0u8; msg_len]; let mut i = 0; while i < msg_len { @@ -913,6 +948,7 @@ impl BgpConnectionTcp { ErrorCode::Open, ErrorSubcode::Open(subcode), Vec::new(), + extended_msg, ) { connection_log_lite!(log, error, @@ -1036,6 +1072,7 @@ impl BgpConnectionTcp { log: &Logger, direction: ConnectionDirection, msg: Message, + extended_msg: bool, ) -> Result<(), Error> { connection_log_lite!(log, trace, @@ -1045,17 +1082,13 @@ impl BgpConnectionTcp { "message" => msg.title(), "message_contents" => format!("{msg}") ); - let msg_buf = msg.to_wire()?; - let header = Header { - length: (msg_buf.len() + Header::WIRE_SIZE).try_into().map_err( - |_| { - Error::TooLarge( - "BGP message being sent is too large".into(), - ) - }, - )?, - typ: MessageType::from(&msg), - }; + let msg_buf = msg.to_wire(extended_msg)?; + let length = + u16::try_from(msg_buf.len() + Header::WIRE_SIZE).map_err(|_| { + Error::TooLarge("BGP message being sent is too large".into()) + })?; + let header = + Header::new(length, MessageType::from(&msg), extended_msg)?; let mut buf = header.to_wire().to_vec(); buf.extend_from_slice(&msg_buf); connection_log_lite!(log, @@ -1076,6 +1109,7 @@ impl BgpConnectionTcp { error_code: ErrorCode, error_subcode: ErrorSubcode, data: Vec, + extended_msg: bool, ) -> Result<(), Error> { Self::send_msg( stream, @@ -1086,6 +1120,7 @@ impl BgpConnectionTcp { error_subcode, data, }), + extended_msg, ) } diff --git a/bgp/src/messages.rs b/bgp/src/messages.rs index 5727dbefd..f3cb113d4 100644 --- a/bgp/src/messages.rs +++ b/bgp/src/messages.rs @@ -21,6 +21,7 @@ use std::{ }; pub const MAX_MESSAGE_SIZE: usize = 4096; +pub const MAX_EXTENDED_MESSAGE_SIZE: usize = 65535; /// Trait for encoding/decoding values to/from BGP wire format. /// @@ -294,10 +295,10 @@ pub enum Message { } impl Message { - pub fn to_wire(&self) -> Result, Error> { + pub fn to_wire(&self, extended_msg: bool) -> Result, Error> { match self { Self::Open(m) => m.to_wire(), - Self::Update(m) => m.to_wire(), + Self::Update(m) => m.to_wire(extended_msg), Self::Notification(m) => m.to_wire(), Self::KeepAlive => Ok(Vec::new()), Self::RouteRefresh(m) => Ok(m.to_wire()), @@ -494,13 +495,18 @@ const MARKER: [u8; 16] = [0xFFu8; 16]; impl Header { pub const WIRE_SIZE: usize = 19; - /// Create a new BGP message header. Length must be between 19 and 4096 per - /// RFC 4271 §4.1. - pub fn new(length: u16, typ: MessageType) -> Result { + /// Create a new BGP message header. + /// Length must be between 19 and 4096 bytes (RFC 4271 §4.1) unless extended + /// message size is permitted, in which case the max size is 65535 bytes. + pub fn new( + length: u16, + typ: MessageType, + extended_msg: bool, + ) -> Result { if usize::from(length) < Header::WIRE_SIZE { return Err(Error::TooSmall("message header length".into())); } - if usize::from(length) > MAX_MESSAGE_SIZE { + if !extended_msg && usize::from(length) > MAX_MESSAGE_SIZE { return Err(Error::TooLarge("message header length".into())); } Ok(Header { length, typ }) @@ -937,7 +943,7 @@ impl UpdateMessage { + nlri } - pub fn to_wire(&self) -> Result, Error> { + pub fn to_wire(&self, extended_msg: bool) -> Result, Error> { let mut buf = Vec::new(); // withdrawn @@ -965,7 +971,12 @@ impl UpdateMessage { // nlri buf.extend_from_slice(&self.nlri_to_wire()?); - if buf.len() > MAX_MESSAGE_SIZE { + let max_size = if extended_msg { + MAX_EXTENDED_MESSAGE_SIZE + } else { + MAX_MESSAGE_SIZE + }; + if buf.len() > max_size { return Err(Error::TooLarge( "update exceeds max message size".into(), )); @@ -4394,9 +4405,7 @@ pub enum Capability { elements: Vec, }, - //TODO - /// Extended message capability as defined in RFC 8654. Note this - /// capability is not yet implemented. + /// Extended message capability as defined in RFC 8654 BGPExtendedMessage {}, //TODO @@ -4647,6 +4656,10 @@ impl Capability { let buf = vec![CapabilityCode::EnhancedRouteRefresh.into(), 0]; Ok(buf) } + Self::BGPExtendedMessage {} => { + let buf = vec![CapabilityCode::BGPExtendedMessage.into(), 0]; + Ok(buf) + } Self::ExtendedNextHopEncoding { elements } => { let mut buf = vec![ CapabilityCode::ExtendedNextHopEncoding as u8, @@ -4731,7 +4744,6 @@ impl Capability { Capability::PrestandardRouteRefresh {} } CapabilityCode::BGPExtendedMessage => { - //TODO handle for real Capability::BGPExtendedMessage {} } CapabilityCode::LongLivedGracefulRestart => { @@ -6143,7 +6155,7 @@ impl From for Option { #[cfg(test)] mod tests { use super::*; - use mg_common::{cidr, ip, parse}; + use mg_common::{cidr, ip, parse, prefix4}; use pretty_assertions::assert_eq; use pretty_hex::*; use std::net::{Ipv4Addr, Ipv6Addr}; @@ -6278,7 +6290,7 @@ mod tests { errors: vec![], }; - let buf = um0.to_wire().expect("update message to wire"); + let buf = um0.to_wire(false).expect("update message to wire"); println!("buf: {}", buf.hex_dump()); let um1 = @@ -6286,6 +6298,77 @@ mod tests { assert_eq!(um0, um1); } + #[test] + fn extended_update_round_trip() { + // Each /24 is 4 bytes on wire; 1100 prefixes = 4400 bytes of + // NLRI alone, exceeding the standard 4077-byte body limit. + let nlri: Vec = (0u32..1100) + .map(|i| rdb::Prefix4 { + value: std::net::Ipv4Addr::new( + 10, + (i >> 8) as u8, + (i & 0xFF) as u8, + 0, + ), + length: 24, + }) + .collect(); + + let um = UpdateMessage { + withdrawn: vec![], + path_attributes: vec![ + PathAttributeValue::Origin(PathOrigin::Igp).into(), + PathAttributeValue::As4Path(vec![As4PathSegment { + typ: AsPathType::AsSequence, + value: vec![65000], + }]) + .into(), + PathAttributeValue::NextHop(ip!("192.0.2.1")).into(), + ], + nlri, + errors: vec![], + }; + + assert!( + um.wire_len() > MAX_MESSAGE_SIZE - Header::WIRE_SIZE, + "test update must exceed standard body limit" + ); + + // Extended: encodes and round-trips successfully + let buf = um.to_wire(true).expect("extended update should encode"); + let decoded = UpdateMessage::from_wire(&buf) + .expect("extended update should decode"); + assert_eq!(um, decoded); + + // Standard: encoding fails because the body exceeds 4096 + assert!(um.to_wire(false).is_err()); + } + + #[test] + fn small_update_works_with_or_without_extended() { + let um = UpdateMessage { + withdrawn: vec![], + path_attributes: vec![ + PathAttributeValue::Origin(PathOrigin::Igp).into(), + PathAttributeValue::As4Path(vec![As4PathSegment { + typ: AsPathType::AsSequence, + value: vec![65000], + }]) + .into(), + PathAttributeValue::NextHop(ip!("192.0.2.1")).into(), + ], + nlri: vec![prefix4!("10.0.0.0/24")], + errors: vec![], + }; + + let buf_std = um.to_wire(false).expect("standard encode"); + let buf_ext = um.to_wire(true).expect("extended encode"); + assert_eq!(buf_std, buf_ext); + + let decoded = UpdateMessage::from_wire(&buf_ext).expect("decode"); + assert_eq!(um, decoded); + } + #[test] fn notification_round_trip() { // Note: NotificationMessage::to_wire() does not yet serialize the data field @@ -7034,7 +7117,7 @@ mod tests { }; // Encode to wire format - let wire = update.to_wire().expect("encoding should succeed"); + let wire = update.to_wire(false).expect("encoding should succeed"); // Skip withdrawn routes length (2 bytes) and empty withdrawn routes (0 bytes) // Skip path attributes length (2 bytes) @@ -7077,7 +7160,7 @@ mod tests { }; // Encode to wire and decode back - should succeed - let wire = update.to_wire().expect("encoding should succeed"); + let wire = update.to_wire(false).expect("encoding should succeed"); let decoded = UpdateMessage::from_wire(&wire); assert!( decoded.is_ok(), @@ -7136,7 +7219,7 @@ mod tests { }; // Encode to wire and decode back - should succeed - let wire = update.to_wire().expect("encoding should succeed"); + let wire = update.to_wire(false).expect("encoding should succeed"); let decoded = UpdateMessage::from_wire(&wire); assert!( decoded.is_ok(), @@ -7201,7 +7284,7 @@ mod tests { }; // Round-trip through wire format - let wire = update.to_wire().expect("encoding should succeed"); + let wire = update.to_wire(false).expect("encoding should succeed"); let decoded = UpdateMessage::from_wire(&wire).expect("decoding should succeed"); @@ -7266,7 +7349,9 @@ mod tests { errors: vec![], }; - let wire = empty_update.to_wire().expect("encoding should succeed"); + let wire = empty_update + .to_wire(false) + .expect("encoding should succeed"); let decoded = UpdateMessage::from_wire(&wire).expect("decoding should succeed"); @@ -7292,7 +7377,9 @@ mod tests { errors: vec![], }; - let wire = mp_eor_update.to_wire().expect("encoding should succeed"); + let wire = mp_eor_update + .to_wire(false) + .expect("encoding should succeed"); let decoded = UpdateMessage::from_wire(&wire).expect("decoding should succeed"); @@ -7328,7 +7415,9 @@ mod tests { errors: vec![], }; - let wire = mp_eor_v4_update.to_wire().expect("encoding should succeed"); + let wire = mp_eor_v4_update + .to_wire(false) + .expect("encoding should succeed"); let decoded = UpdateMessage::from_wire(&wire).expect("decoding should succeed"); @@ -9104,31 +9193,43 @@ mod tests { #[test] fn new_rejects_length_too_small() { - let result = Header::new(18, MessageType::KeepAlive); + let result = Header::new(18, MessageType::KeepAlive, false); assert!(result.is_err()); } #[test] fn new_rejects_length_too_large() { - let result = Header::new(4097, MessageType::Update); + let result = Header::new(4097, MessageType::Update, false); assert!(result.is_err()); } #[test] fn new_accepts_minimum_length() { - let hdr = Header::new(19, MessageType::KeepAlive).unwrap(); + let hdr = Header::new(19, MessageType::KeepAlive, false).unwrap(); assert_eq!(hdr.length, 19); } #[test] fn new_accepts_maximum_length() { - let hdr = Header::new(4096, MessageType::Update).unwrap(); + let hdr = Header::new(4096, MessageType::Update, false).unwrap(); assert_eq!(hdr.length, 4096); } + #[test] + fn extended_accepts_large_update() { + let hdr = Header::new(65535, MessageType::Update, true).unwrap(); + assert_eq!(hdr.length, 65535); + } + + #[test] + fn non_extended_rejects_large_update() { + let result = Header::new(65535, MessageType::Update, false); + assert!(result.is_err()); + } + #[test] fn roundtrip_keepalive_header() { - let hdr = Header::new(19, MessageType::KeepAlive).unwrap(); + let hdr = Header::new(19, MessageType::KeepAlive, false).unwrap(); let wire = hdr.to_wire(); assert_eq!(wire.len(), Header::WIRE_SIZE); let parsed = Header::from_wire(&wire).unwrap(); @@ -9138,7 +9239,7 @@ mod tests { #[test] fn roundtrip_max_length_header() { - let hdr = Header::new(4096, MessageType::Update).unwrap(); + let hdr = Header::new(4096, MessageType::Update, false).unwrap(); let wire = hdr.to_wire(); let parsed = Header::from_wire(&wire).unwrap(); assert_eq!(parsed.length, 4096); @@ -9172,7 +9273,7 @@ mod tests { #[test] fn from_wire_rejects_bad_marker() { - let hdr = Header::new(19, MessageType::KeepAlive).unwrap(); + let hdr = Header::new(19, MessageType::KeepAlive, false).unwrap(); let mut wire = hdr.to_wire(); wire[0] = 0x00; // corrupt marker assert!(Header::from_wire(&wire).is_err()); diff --git a/bgp/src/proptest.rs b/bgp/src/proptest.rs index 28b9654ec..cd27346f4 100644 --- a/bgp/src/proptest.rs +++ b/bgp/src/proptest.rs @@ -14,10 +14,10 @@ use crate::messages::{ As4PathSegment, AsPathType, BgpNexthop, BgpWireFormat, Header, - Ipv6DoubleNexthop, MAX_MESSAGE_SIZE, MpReachNlri, MpUnreachNlri, - PathAttribute, PathAttributeType, PathAttributeTypeCode, - PathAttributeValue, PathOrigin, UpdateMessage, path_attribute_flags, - path_attrs_wire_len, split_prefixes_by_size, + Ipv6DoubleNexthop, MAX_EXTENDED_MESSAGE_SIZE, MAX_MESSAGE_SIZE, + MpReachNlri, MpUnreachNlri, PathAttribute, PathAttributeType, + PathAttributeTypeCode, PathAttributeValue, PathOrigin, UpdateMessage, + path_attribute_flags, path_attrs_wire_len, split_prefixes_by_size, }; use proptest::prelude::*; use rdb::types::{Prefix4, Prefix6}; @@ -255,11 +255,11 @@ fn update_strategy() -> impl Strategy { // Large-Prefix Strategies (for split_prefixes_by_size tests) // ============================================================================= -/// Strategy for generating enough IPv4 /24 prefixes to require chunking. -/// Each /24 prefix is 4 bytes on the wire. The max UPDATE body is 4077 -/// bytes; with minimal overhead (~29 bytes) roughly 1012 prefixes fit, -/// so 1100+ always forces a split. -fn large_ipv4_prefixes_strategy() -> impl Strategy> { +/// Strategy for generating enough IPv4 /24 prefixes to require chunking +/// at standard message size. Each /24 prefix is 4 bytes on the wire. +/// The max UPDATE body is 4077 bytes; with minimal overhead (~29 bytes) +/// roughly 1012 prefixes fit, so 1100+ always forces a split. +fn ipv4_prefixes_for_std_split() -> impl Strategy> { (1100u32..1500).prop_flat_map(|count| { Just( (0..count) @@ -269,10 +269,11 @@ fn large_ipv4_prefixes_strategy() -> impl Strategy> { }) } -/// Strategy for generating enough IPv6 /48 prefixes to require chunking. -/// Each /48 prefix is 7 bytes on the wire. With ~29 bytes overhead -/// roughly 578 prefixes fit, so 600+ always forces a split. -fn large_ipv6_prefixes_strategy() -> impl Strategy> { +/// Strategy for generating enough IPv6 /48 prefixes to require chunking +/// at standard message size. Each /48 prefix is 7 bytes on the wire. +/// With ~29 bytes overhead roughly 578 prefixes fit, so 600+ always +/// forces a split. +fn ipv6_prefixes_for_std_split() -> impl Strategy> { (600u32..800).prop_flat_map(|count| { Just( (0..count as u128) @@ -282,6 +283,96 @@ fn large_ipv6_prefixes_strategy() -> impl Strategy> { }) } +/// Strategy for generating enough IPv4 /24 prefixes to require chunking +/// at extended message size. The extended body limit is 65516 bytes; +/// with ~29 bytes overhead roughly 16371 prefixes fit, so 17000+ +/// always forces a split. +fn ipv4_prefixes_for_ext_split() -> impl Strategy> { + (17000u32..18000).prop_flat_map(|count| { + Just( + (0..count) + .map(|i| Prefix4::new(Ipv4Addr::from(i << 8), 24)) + .collect(), + ) + }) +} + +/// Strategy for generating enough IPv6 /48 prefixes to require chunking +/// at extended message size. With 7 bytes per prefix and ~29 bytes +/// overhead roughly 9355 prefixes fit, so 10000+ always forces a split. +fn ipv6_prefixes_for_ext_split() -> impl Strategy> { + (10000u32..11000).prop_flat_map(|count| { + Just( + (0..count as u128) + .map(|i| Prefix6::new(Ipv6Addr::from(i << 80), 48)) + .collect(), + ) + }) +} + +// ============================================================================= +// Update Skeleton Builders +// +// These mirror the skeletons used by chunk_and_send in session.rs for +// each type of UPDATE message we send. +// ============================================================================= + +/// Skeleton for a traditional IPv4 announcement (nexthop in body). +fn v4_announce_skeleton() -> UpdateMessage { + UpdateMessage { + path_attributes: vec![ + PathAttributeValue::NextHop(Ipv4Addr::UNSPECIFIED).into(), + ], + ..Default::default() + } +} + +/// Skeleton for an ENHE IPv4 announcement (nexthop in MP_REACH). +fn v4_mp_announce_skeleton() -> UpdateMessage { + UpdateMessage { + path_attributes: vec![ + PathAttributeValue::MpReachNlri(MpReachNlri::ipv4_unicast( + BgpNexthop::Ipv6Single(Ipv6Addr::UNSPECIFIED), + vec![], + )) + .into(), + ], + ..Default::default() + } +} + +/// Skeleton for an IPv4 withdrawal (no path attributes). +fn v4_withdraw_skeleton() -> UpdateMessage { + UpdateMessage::default() +} + +/// Skeleton for an IPv6 announcement. +fn v6_mp_announce_skeleton() -> UpdateMessage { + UpdateMessage { + path_attributes: vec![ + PathAttributeValue::MpReachNlri(MpReachNlri::ipv6_unicast( + BgpNexthop::Ipv6Single(Ipv6Addr::UNSPECIFIED), + vec![], + )) + .into(), + ], + ..Default::default() + } +} + +/// Skeleton for an IPv6 withdrawal. +fn v6_mp_withdraw_skeleton() -> UpdateMessage { + UpdateMessage { + path_attributes: vec![ + PathAttributeValue::MpUnreachNlri(MpUnreachNlri::ipv6_unicast( + vec![], + )) + .into(), + ], + ..Default::default() + } +} + // ============================================================================= // Property Tests // ============================================================================= @@ -328,7 +419,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire().expect("should encode"); + let wire = update.to_wire(false).expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); prop_assert_eq!(decoded.nlri, prefixes, "NLRI prefixes should round-trip"); @@ -344,7 +435,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire().expect("should encode"); + let wire = update.to_wire(false).expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); prop_assert_eq!(decoded.withdrawn, prefixes, "Withdrawn prefixes should round-trip"); @@ -371,7 +462,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire().expect("should encode"); + let wire = update.to_wire(false).expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); // Extract MP_REACH_NLRI and verify prefixes @@ -405,7 +496,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire().expect("should encode"); + let wire = update.to_wire(false).expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); // Extract MP_UNREACH_NLRI and verify prefixes @@ -442,7 +533,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire().expect("should encode"); + let wire = update.to_wire(false).expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); // Extract MP_REACH_NLRI and verify prefixes @@ -476,7 +567,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire().expect("should encode"); + let wire = update.to_wire(false).expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); // Extract MP_UNREACH_NLRI and verify prefixes @@ -515,7 +606,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire().expect("should encode"); + let wire = update.to_wire(false).expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); // Extract and verify the next-hop @@ -542,7 +633,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire().expect("should encode"); + let wire = update.to_wire(false).expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); let decoded_nexthop = decoded.nexthop().expect("should have nexthop"); @@ -568,7 +659,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire().expect("should encode"); + let wire = update.to_wire(false).expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); let decoded_nexthop = decoded.nexthop().expect("should have nexthop"); @@ -582,7 +673,7 @@ proptest! { /// Property: Traditional UpdateMessage round-trip preserves structure #[test] fn prop_update_traditional_roundtrip(update in update_traditional_strategy()) { - let wire = update.to_wire().expect("should encode"); + let wire = update.to_wire(false).expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); prop_assert_eq!(decoded.withdrawn, update.withdrawn); @@ -598,7 +689,7 @@ proptest! { /// Property: MP-BGP UpdateMessage with MP_REACH_NLRI round-trip works #[test] fn prop_update_mp_reach_roundtrip(update in update_mp_reach_strategy()) { - let wire = update.to_wire().expect("should encode"); + let wire = update.to_wire(false).expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); // Should have MP_REACH_NLRI attribute @@ -611,7 +702,7 @@ proptest! { /// Property: MP-BGP UpdateMessage with MP_UNREACH_NLRI round-trip works #[test] fn prop_update_mp_unreach_roundtrip(update in update_mp_unreach_strategy()) { - let wire = update.to_wire().expect("should encode"); + let wire = update.to_wire(false).expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); // Should have MP_UNREACH_NLRI attribute @@ -628,7 +719,7 @@ proptest! { /// Property: MP-BGP attributes are always encoded first (RFC 7606 Section 5.1) #[test] fn prop_mp_bgp_attrs_encoded_first(update in update_mp_reach_strategy()) { - let wire = update.to_wire().expect("should encode"); + let wire = update.to_wire(false).expect("should encode"); // Skip to path attributes section // Wire format: 2 bytes withdrawn len + withdrawn + 2 bytes attrs len + attrs + nlri @@ -695,7 +786,7 @@ proptest! { /// Property: Encoding then decoding produces semantically equivalent message #[test] fn prop_encode_decode_semantic_equivalence(update in update_strategy()) { - let wire = update.to_wire().expect("should encode"); + let wire = update.to_wire(false).expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); // Withdrawn and NLRI should be identical @@ -795,8 +886,8 @@ proptest! { }; // Encode and decode both - let traditional_wire = traditional_update.to_wire().expect("traditional encode"); - let mp_bgp_wire = mp_bgp_update.to_wire().expect("mp-bgp encode"); + let traditional_wire = traditional_update.to_wire(false).expect("traditional encode"); + let mp_bgp_wire = mp_bgp_update.to_wire(false).expect("mp-bgp encode"); let traditional_decoded = UpdateMessage::from_wire(&traditional_wire) .expect("traditional decode"); @@ -843,101 +934,188 @@ proptest! { // ------------------------------------------------------------------------- // split_prefixes_by_size Tests + // + // Five update types mirror the chunk_and_send call sites in session.rs: + // 1. V4 announce (traditional): NextHop attr, NLRI in body + // 2. V4 announce (ENHE/MP-BGP): MpReachNlri(Ipv4Unicast) attr + // 3. V4 withdraw: no attrs, withdrawn in body + // 4. V6 announce: MpReachNlri(Ipv6Unicast) attr + // 5. V6 withdraw: MpUnreachNlri(Ipv6Unicast) attr + // + // Each type is tested at both standard and extended message limits. // ------------------------------------------------------------------------- - /// Property: IPv4 split preserves all prefixes across chunks. + // --- Standard message size (4096) --- + #[test] - fn prop_v4_split_preserves_all_prefixes( - prefixes in large_ipv4_prefixes_strategy() + fn prop_v4_announce_split_std( + prefixes in ipv4_prefixes_for_std_split() ) { - let max_body = MAX_MESSAGE_SIZE - Header::WIRE_SIZE; - let skeleton = UpdateMessage { - path_attributes: vec![ - PathAttributeValue::NextHop(Ipv4Addr::UNSPECIFIED).into(), - ], - ..Default::default() - }; - let available = max_body.saturating_sub(skeleton.wire_len()); - let original_count = prefixes.len(); + let available = (MAX_MESSAGE_SIZE - Header::WIRE_SIZE) + .saturating_sub(v4_announce_skeleton().wire_len()); + let n = prefixes.len(); let chunks = split_prefixes_by_size(prefixes, available, Prefix4::wire_len); prop_assert!(chunks.len() >= 2, "expected 2+ chunks, got {}", chunks.len()); let total: usize = chunks.iter().map(|c| c.len()).sum(); - prop_assert_eq!(total, original_count); + prop_assert_eq!(total, n); + for (i, c) in chunks.iter().enumerate() { + let sz: usize = c.iter().map(Prefix4::wire_len).sum(); + prop_assert!(sz <= available, "chunk {i}: {sz} > {available}"); + } } - /// Property: IPv6 split preserves all prefixes across chunks. #[test] - fn prop_v6_split_preserves_all_prefixes( - prefixes in large_ipv6_prefixes_strategy() + fn prop_v4_mp_announce_split_std( + prefixes in ipv4_prefixes_for_std_split() ) { - let max_body = MAX_MESSAGE_SIZE - Header::WIRE_SIZE; - let skeleton = UpdateMessage { - path_attributes: vec![ - PathAttributeValue::MpReachNlri( - MpReachNlri::ipv6_unicast( - BgpNexthop::Ipv6Single(Ipv6Addr::UNSPECIFIED), - vec![], - ), - ).into(), - ], - ..Default::default() - }; - let available = max_body.saturating_sub(skeleton.wire_len()); - let original_count = prefixes.len(); + let available = (MAX_MESSAGE_SIZE - Header::WIRE_SIZE) + .saturating_sub(v4_mp_announce_skeleton().wire_len()); + let n = prefixes.len(); + let chunks = split_prefixes_by_size(prefixes, available, Prefix4::wire_len); + prop_assert!(chunks.len() >= 2, "expected 2+ chunks, got {}", chunks.len()); + let total: usize = chunks.iter().map(|c| c.len()).sum(); + prop_assert_eq!(total, n); + for (i, c) in chunks.iter().enumerate() { + let sz: usize = c.iter().map(Prefix4::wire_len).sum(); + prop_assert!(sz <= available, "chunk {i}: {sz} > {available}"); + } + } + + #[test] + fn prop_v4_withdraw_split_std( + prefixes in ipv4_prefixes_for_std_split() + ) { + let available = (MAX_MESSAGE_SIZE - Header::WIRE_SIZE) + .saturating_sub(v4_withdraw_skeleton().wire_len()); + let n = prefixes.len(); + let chunks = split_prefixes_by_size(prefixes, available, Prefix4::wire_len); + prop_assert!(chunks.len() >= 2, "expected 2+ chunks, got {}", chunks.len()); + let total: usize = chunks.iter().map(|c| c.len()).sum(); + prop_assert_eq!(total, n); + for (i, c) in chunks.iter().enumerate() { + let sz: usize = c.iter().map(Prefix4::wire_len).sum(); + prop_assert!(sz <= available, "chunk {i}: {sz} > {available}"); + } + } + + #[test] + fn prop_v6_mp_announce_split_std( + prefixes in ipv6_prefixes_for_std_split() + ) { + let available = (MAX_MESSAGE_SIZE - Header::WIRE_SIZE) + .saturating_sub(v6_mp_announce_skeleton().wire_len()); + let n = prefixes.len(); let chunks = split_prefixes_by_size(prefixes, available, Prefix6::wire_len); prop_assert!(chunks.len() >= 2, "expected 2+ chunks, got {}", chunks.len()); let total: usize = chunks.iter().map(|c| c.len()).sum(); - prop_assert_eq!(total, original_count); + prop_assert_eq!(total, n); + for (i, c) in chunks.iter().enumerate() { + let sz: usize = c.iter().map(Prefix6::wire_len).sum(); + prop_assert!(sz <= available, "chunk {i}: {sz} > {available}"); + } } - /// Property: Every IPv4 chunk fits within available space. #[test] - fn prop_v4_all_chunks_within_available( - prefixes in large_ipv4_prefixes_strategy() + fn prop_v6_mp_withdraw_split_std( + prefixes in ipv6_prefixes_for_std_split() ) { - let max_body = MAX_MESSAGE_SIZE - Header::WIRE_SIZE; - let skeleton = UpdateMessage { - path_attributes: vec![ - PathAttributeValue::NextHop(Ipv4Addr::UNSPECIFIED).into(), - ], - ..Default::default() - }; - let available = max_body.saturating_sub(skeleton.wire_len()); + let available = (MAX_MESSAGE_SIZE - Header::WIRE_SIZE) + .saturating_sub(v6_mp_withdraw_skeleton().wire_len()); + let n = prefixes.len(); + let chunks = split_prefixes_by_size(prefixes, available, Prefix6::wire_len); + prop_assert!(chunks.len() >= 2, "expected 2+ chunks, got {}", chunks.len()); + let total: usize = chunks.iter().map(|c| c.len()).sum(); + prop_assert_eq!(total, n); + for (i, c) in chunks.iter().enumerate() { + let sz: usize = c.iter().map(Prefix6::wire_len).sum(); + prop_assert!(sz <= available, "chunk {i}: {sz} > {available}"); + } + } + + // --- Extended message size (65535) --- + + #[test] + fn prop_v4_announce_split_ext( + prefixes in ipv4_prefixes_for_ext_split() + ) { + let available = (MAX_EXTENDED_MESSAGE_SIZE - Header::WIRE_SIZE) + .saturating_sub(v4_announce_skeleton().wire_len()); + let n = prefixes.len(); let chunks = split_prefixes_by_size(prefixes, available, Prefix4::wire_len); - for (i, chunk) in chunks.iter().enumerate() { - let size: usize = chunk.iter().map(Prefix4::wire_len).sum(); - prop_assert!( - size <= available, - "chunk {i} is {size} bytes, max is {available}", - ); + prop_assert!(chunks.len() >= 2, "expected 2+ chunks, got {}", chunks.len()); + let total: usize = chunks.iter().map(|c| c.len()).sum(); + prop_assert_eq!(total, n); + for (i, c) in chunks.iter().enumerate() { + let sz: usize = c.iter().map(Prefix4::wire_len).sum(); + prop_assert!(sz <= available, "chunk {i}: {sz} > {available}"); } } - /// Property: Every IPv6 chunk fits within available space. #[test] - fn prop_v6_all_chunks_within_available( - prefixes in large_ipv6_prefixes_strategy() + fn prop_v4_mp_announce_split_ext( + prefixes in ipv4_prefixes_for_ext_split() ) { - let max_body = MAX_MESSAGE_SIZE - Header::WIRE_SIZE; - let skeleton = UpdateMessage { - path_attributes: vec![ - PathAttributeValue::MpReachNlri( - MpReachNlri::ipv6_unicast( - BgpNexthop::Ipv6Single(Ipv6Addr::UNSPECIFIED), - vec![], - ), - ).into(), - ], - ..Default::default() - }; - let available = max_body.saturating_sub(skeleton.wire_len()); + let available = (MAX_EXTENDED_MESSAGE_SIZE - Header::WIRE_SIZE) + .saturating_sub(v4_mp_announce_skeleton().wire_len()); + let n = prefixes.len(); + let chunks = split_prefixes_by_size(prefixes, available, Prefix4::wire_len); + prop_assert!(chunks.len() >= 2, "expected 2+ chunks, got {}", chunks.len()); + let total: usize = chunks.iter().map(|c| c.len()).sum(); + prop_assert_eq!(total, n); + for (i, c) in chunks.iter().enumerate() { + let sz: usize = c.iter().map(Prefix4::wire_len).sum(); + prop_assert!(sz <= available, "chunk {i}: {sz} > {available}"); + } + } + + #[test] + fn prop_v4_withdraw_split_ext( + prefixes in ipv4_prefixes_for_ext_split() + ) { + let available = (MAX_EXTENDED_MESSAGE_SIZE - Header::WIRE_SIZE) + .saturating_sub(v4_withdraw_skeleton().wire_len()); + let n = prefixes.len(); + let chunks = split_prefixes_by_size(prefixes, available, Prefix4::wire_len); + prop_assert!(chunks.len() >= 2, "expected 2+ chunks, got {}", chunks.len()); + let total: usize = chunks.iter().map(|c| c.len()).sum(); + prop_assert_eq!(total, n); + for (i, c) in chunks.iter().enumerate() { + let sz: usize = c.iter().map(Prefix4::wire_len).sum(); + prop_assert!(sz <= available, "chunk {i}: {sz} > {available}"); + } + } + + #[test] + fn prop_v6_mp_announce_split_ext( + prefixes in ipv6_prefixes_for_ext_split() + ) { + let available = (MAX_EXTENDED_MESSAGE_SIZE - Header::WIRE_SIZE) + .saturating_sub(v6_mp_announce_skeleton().wire_len()); + let n = prefixes.len(); let chunks = split_prefixes_by_size(prefixes, available, Prefix6::wire_len); - for (i, chunk) in chunks.iter().enumerate() { - let size: usize = chunk.iter().map(Prefix6::wire_len).sum(); - prop_assert!( - size <= available, - "chunk {i} is {size} bytes, max is {available}", - ); + prop_assert!(chunks.len() >= 2, "expected 2+ chunks, got {}", chunks.len()); + let total: usize = chunks.iter().map(|c| c.len()).sum(); + prop_assert_eq!(total, n); + for (i, c) in chunks.iter().enumerate() { + let sz: usize = c.iter().map(Prefix6::wire_len).sum(); + prop_assert!(sz <= available, "chunk {i}: {sz} > {available}"); + } + } + + #[test] + fn prop_v6_mp_withdraw_split_ext( + prefixes in ipv6_prefixes_for_ext_split() + ) { + let available = (MAX_EXTENDED_MESSAGE_SIZE - Header::WIRE_SIZE) + .saturating_sub(v6_mp_withdraw_skeleton().wire_len()); + let n = prefixes.len(); + let chunks = split_prefixes_by_size(prefixes, available, Prefix6::wire_len); + prop_assert!(chunks.len() >= 2, "expected 2+ chunks, got {}", chunks.len()); + let total: usize = chunks.iter().map(|c| c.len()).sum(); + prop_assert_eq!(total, n); + for (i, c) in chunks.iter().enumerate() { + let sz: usize = c.iter().map(Prefix6::wire_len).sum(); + prop_assert!(sz <= available, "chunk {i}: {sz} > {available}"); } } @@ -946,14 +1124,8 @@ proptest! { fn prop_small_v4_produces_single_chunk( prefixes in ipv4_prefixes_strategy() ) { - let max_body = MAX_MESSAGE_SIZE - Header::WIRE_SIZE; - let skeleton = UpdateMessage { - path_attributes: vec![ - PathAttributeValue::NextHop(Ipv4Addr::UNSPECIFIED).into(), - ], - ..Default::default() - }; - let available = max_body.saturating_sub(skeleton.wire_len()); + let available = (MAX_MESSAGE_SIZE - Header::WIRE_SIZE) + .saturating_sub(v4_announce_skeleton().wire_len()); let chunks = split_prefixes_by_size(prefixes.clone(), available, Prefix4::wire_len); if !prefixes.is_empty() { prop_assert_eq!(chunks.len(), 1, "small prefix vec should not be split"); @@ -965,19 +1137,8 @@ proptest! { fn prop_small_v6_produces_single_chunk( prefixes in ipv6_prefixes_strategy() ) { - let max_body = MAX_MESSAGE_SIZE - Header::WIRE_SIZE; - let skeleton = UpdateMessage { - path_attributes: vec![ - PathAttributeValue::MpReachNlri( - MpReachNlri::ipv6_unicast( - BgpNexthop::Ipv6Single(Ipv6Addr::UNSPECIFIED), - vec![], - ), - ).into(), - ], - ..Default::default() - }; - let available = max_body.saturating_sub(skeleton.wire_len()); + let available = (MAX_MESSAGE_SIZE - Header::WIRE_SIZE) + .saturating_sub(v6_mp_announce_skeleton().wire_len()); let chunks = split_prefixes_by_size(prefixes.clone(), available, Prefix6::wire_len); if !prefixes.is_empty() { prop_assert_eq!(chunks.len(), 1, "small prefix vec should not be split"); @@ -1004,11 +1165,89 @@ proptest! { #[test] fn prop_wire_len_matches_to_wire(update in update_strategy()) { let expected = update.wire_len(); - let wire = update.to_wire().expect("should encode"); + let wire = update.to_wire(false).expect("should encode"); prop_assert_eq!( wire.len(), expected, "wire_len() = {} but to_wire() produced {} bytes", expected, wire.len() ); } + + // ------------------------------------------------------------------------- + // Extended Message Tests (RFC 8654) + // ------------------------------------------------------------------------- + + /// Property: Extended flag does not alter encoding for small updates. + #[test] + fn prop_extended_flag_no_effect_on_small_updates( + update in update_strategy() + ) { + let wire_std = update.to_wire(false).expect("standard encode"); + let wire_ext = update.to_wire(true).expect("extended encode"); + prop_assert_eq!( + wire_std, wire_ext, + "extended flag should not change encoding for small updates" + ); + } + + /// Property: Traditional updates round-trip with extended flag. + #[test] + fn prop_extended_traditional_roundtrip( + update in update_traditional_strategy() + ) { + let wire = update.to_wire(true).expect("extended encode"); + let decoded = + UpdateMessage::from_wire(&wire).expect("decode"); + prop_assert_eq!(decoded.withdrawn, update.withdrawn); + prop_assert_eq!(decoded.nlri, update.nlri); + } + + /// Property: MP-BGP updates round-trip with extended flag. + #[test] + fn prop_extended_mp_reach_roundtrip( + update in update_mp_reach_strategy() + ) { + let wire = update.to_wire(true).expect("extended encode"); + let decoded = + UpdateMessage::from_wire(&wire).expect("decode"); + let original_mp = update.mp_reach().expect("original mp_reach"); + let decoded_mp = decoded.mp_reach().expect("decoded mp_reach"); + prop_assert_eq!(decoded_mp, original_mp); + } + + /// Property: Large update encodes with extended flag and round-trips. + #[test] + fn prop_large_v4_extended_roundtrip( + prefixes in ipv4_prefixes_for_std_split() + ) { + let update = UpdateMessage { + withdrawn: vec![], + path_attributes: vec![ + PathAttributeValue::Origin(PathOrigin::Igp).into(), + PathAttributeValue::As4Path(vec![As4PathSegment { + typ: AsPathType::AsSequence, + value: vec![65000], + }]) + .into(), + PathAttributeValue::NextHop(Ipv4Addr::UNSPECIFIED) + .into(), + ], + nlri: prefixes, + errors: vec![], + }; + // Must exceed standard limit + prop_assert!( + update.wire_len() + > MAX_MESSAGE_SIZE - Header::WIRE_SIZE + ); + // Standard encoding must fail + prop_assert!(update.to_wire(false).is_err()); + // Extended encoding succeeds and round-trips + let wire = + update.to_wire(true).expect("extended encode"); + let decoded = + UpdateMessage::from_wire(&wire).expect("decode"); + prop_assert_eq!(decoded.nlri, update.nlri); + prop_assert_eq!(decoded.withdrawn, update.withdrawn); + } } diff --git a/bgp/src/session.rs b/bgp/src/session.rs index ed17dbaf3..c3d6e7c9c 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -14,11 +14,11 @@ use crate::{ log::{collision_log, session_log, session_log_lite}, messages::{ AddPathElement, Afi, BgpNexthop, Capability, CeaseErrorSubcode, - Community, ErrorCode, ErrorSubcode, Header, MAX_MESSAGE_SIZE, Message, - MessageKind, MessageParseError, MpReachNlri, MpUnreachNlri, - NotificationMessage, OpenErrorSubcode, OpenMessage, PathAttribute, - PathAttributeValue, RouteRefreshMessage, Safi, UpdateMessage, - UpdateParseErrorReason, + Community, ErrorCode, ErrorSubcode, Header, MAX_EXTENDED_MESSAGE_SIZE, + MAX_MESSAGE_SIZE, Message, MessageKind, MessageParseError, MpReachNlri, + MpUnreachNlri, NotificationMessage, OpenErrorSubcode, OpenMessage, + PathAttribute, PathAttributeValue, RouteRefreshMessage, Safi, + UpdateMessage, UpdateParseErrorReason, }, params::{ BgpCapability, BgpPeerParameters, BgpPeerParametersV1, @@ -132,6 +132,12 @@ pub struct PeerConnection { pub mp_bgp: bool, } +impl PeerConnection { + fn extended_msg(&self) -> bool { + self.conn.extended_msg() + } +} + impl Clone for PeerConnection { fn clone(&self) -> Self { PeerConnection { @@ -2410,6 +2416,7 @@ impl SessionRunner { let mut caps = BTreeSet::from([ //Capability::EnhancedRouteRefresh{}, //Capability::GracefulRestart{}, + Capability::BGPExtendedMessage {}, Capability::AddPath { elements: BTreeSet::from([AddPathElement { afi: 1, //IP @@ -3983,6 +3990,9 @@ impl SessionRunner { let our_caps = lock!(self.caps_tx); negotiate_afis(&our_caps, &caps) }; + if caps.contains(&Capability::BGPExtendedMessage {}) { + conn.set_extended_msg(true); + } let pc = PeerConnection { conn, @@ -5349,6 +5359,9 @@ impl SessionRunner { let our_caps = lock!(self.caps_tx); negotiate_afis(&our_caps, &caps) }; + if caps.contains(&Capability::BGPExtendedMessage {}) { + new.set_extended_msg(true); + } let new_pc = PeerConnection { conn: new, @@ -5667,6 +5680,9 @@ impl SessionRunner { let our_caps = lock!(self.caps_tx); negotiate_afis(&our_caps, &caps) }; + if caps.contains(&Capability::BGPExtendedMessage {}) { + exist.set_extended_msg(true); + } let exist_pc = PeerConnection { conn: exist.clone(), @@ -5804,6 +5820,9 @@ impl SessionRunner { let our_caps = lock!(self.caps_tx); negotiate_afis(&our_caps, &caps) }; + if caps.contains(&Capability::BGPExtendedMessage {}) { + new.set_extended_msg(true); + } let new_pc = PeerConnection { conn: new.clone(), @@ -7128,6 +7147,9 @@ impl SessionRunner { lock!(self.caps_tx); negotiate_afis(&our_caps, &caps) }; + if caps.contains(&Capability::BGPExtendedMessage {}) { + incoming_conn.set_extended_msg(true); + } let new_pc = PeerConnection { conn: incoming_conn.clone(), @@ -8094,7 +8116,12 @@ impl SessionRunner { return Ok(()); } - let max_body = MAX_MESSAGE_SIZE - Header::WIRE_SIZE; + let max_msg_size = if pc.extended_msg() { + MAX_EXTENDED_MESSAGE_SIZE + } else { + MAX_MESSAGE_SIZE + }; + let max_body = max_msg_size - Header::WIRE_SIZE; match route_update { RouteUpdate::V4(RouteUpdate4::Announce(nlri)) => { diff --git a/mg-common/src/test.rs b/mg-common/src/test.rs index 8e1e568cd..5d25d4fb5 100644 --- a/mg-common/src/test.rs +++ b/mg-common/src/test.rs @@ -160,6 +160,20 @@ macro_rules! sockaddr { }; } +#[macro_export] +macro_rules! prefix4 { + ($x:expr) => { + parse!($x, "ipv4 prefix") + }; +} + +#[macro_export] +macro_rules! prefix6 { + ($x:expr) => { + parse!($x, "ipv6 prefix") + }; +} + struct ManagedIp { address: IpAddr, installed: bool, diff --git a/openapi/mg-admin/mg-admin-7.0.0-a3f1ca.json b/openapi/mg-admin/mg-admin-7.0.0-af3143.json similarity index 99% rename from openapi/mg-admin/mg-admin-7.0.0-a3f1ca.json rename to openapi/mg-admin/mg-admin-7.0.0-af3143.json index 275d0317c..30746b7f0 100644 --- a/openapi/mg-admin/mg-admin-7.0.0-a3f1ca.json +++ b/openapi/mg-admin/mg-admin-7.0.0-af3143.json @@ -2463,7 +2463,7 @@ "additionalProperties": false }, { - "description": "Extended message capability as defined in RFC 8654. Note this capability is not yet implemented.", + "description": "Extended message capability as defined in RFC 8654", "type": "object", "properties": { "b_g_p_extended_message": { diff --git a/openapi/mg-admin/mg-admin-latest.json b/openapi/mg-admin/mg-admin-latest.json index ca375ed78..f96ff24bc 120000 --- a/openapi/mg-admin/mg-admin-latest.json +++ b/openapi/mg-admin/mg-admin-latest.json @@ -1 +1 @@ -mg-admin-7.0.0-a3f1ca.json \ No newline at end of file +mg-admin-7.0.0-af3143.json \ No newline at end of file From e0b941c66cda350878ddf87b028c8549bbc3cc28 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 11 Mar 2026 21:04:56 -0600 Subject: [PATCH 36/58] Refactor BGP Capability storage Updates PeerConnection to store a new PeerCabilities struct, which has the negotiated state of each supported capability. This is in addition to the BTreeSets of sent/received capabilities, which are still exposed via the API so operators can see exactly what caps were exchanged rather than just the resulting usable state (e.g. if we wanted to figure out what capability was sent to us + what it contained). --- bgp/src/messages.rs | 4 +- bgp/src/session.rs | 395 +++++++++++++++++++++++++++---------------- mgd/src/bgp_admin.rs | 4 +- 3 files changed, 250 insertions(+), 153 deletions(-) diff --git a/bgp/src/messages.rs b/bgp/src/messages.rs index f3cb113d4..49a8d0aee 100644 --- a/bgp/src/messages.rs +++ b/bgp/src/messages.rs @@ -4335,14 +4335,14 @@ pub struct ExtendedNexthopElement { } impl ExtendedNexthopElement { - fn is_v4_over_v6(&self) -> bool { + pub fn is_v4_over_v6(&self) -> bool { self == &ExtendedNexthopElement { afi: Afi::Ipv4.into(), safi: u8::from(Safi::Unicast).into(), nh_afi: Afi::Ipv6.into(), } } - fn is_v6_over_v4(&self) -> bool { + pub fn is_v6_over_v4(&self) -> bool { self == &ExtendedNexthopElement { afi: Afi::Ipv6.into(), safi: u8::from(Safi::Unicast).into(), diff --git a/bgp/src/session.rs b/bgp/src/session.rs index c3d6e7c9c..9a727c317 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -14,11 +14,11 @@ use crate::{ log::{collision_log, session_log, session_log_lite}, messages::{ AddPathElement, Afi, BgpNexthop, Capability, CeaseErrorSubcode, - Community, ErrorCode, ErrorSubcode, Header, MAX_EXTENDED_MESSAGE_SIZE, - MAX_MESSAGE_SIZE, Message, MessageKind, MessageParseError, MpReachNlri, - MpUnreachNlri, NotificationMessage, OpenErrorSubcode, OpenMessage, - PathAttribute, PathAttributeValue, RouteRefreshMessage, Safi, - UpdateMessage, UpdateParseErrorReason, + Community, ErrorCode, ErrorSubcode, ExtendedNexthopElement, Header, + MAX_EXTENDED_MESSAGE_SIZE, MAX_MESSAGE_SIZE, Message, MessageKind, + MessageParseError, MpReachNlri, MpUnreachNlri, NotificationMessage, + OpenErrorSubcode, OpenMessage, PathAttribute, PathAttributeValue, + RouteRefreshMessage, Safi, UpdateMessage, UpdateParseErrorReason, }, params::{ BgpCapability, BgpPeerParameters, BgpPeerParametersV1, @@ -58,8 +58,7 @@ use std::{ const UNIT_SESSION_RUNNER: &str = "session_runner"; -/// The runtime state of an address-family for a given peer. -/// This is instantiated after capability negotiation has completed. +/// The negotiation state of a capability for a given peer. #[derive( Debug, Clone, @@ -71,37 +70,72 @@ const UNIT_SESSION_RUNNER: &str = "session_runner"; Deserialize, JsonSchema, )] -pub enum AfiSafiState { - /// Not configured for this session. We did not advertise this capability - /// in our OPEN message, so the peer's support is irrelevant. +pub enum CapabilityState { + /// We have not configured or advertised this capability, and the peer did + /// not send it. #[default] Unconfigured, - /// We advertised this capability but the peer did not. Routes for this - /// AFI/SAFI will be ignored. + /// We advertised this capability but the peer did not send it. Advertised, - /// Successfully negotiated with peer. Routes for this AFI/SAFI will be - /// processed. + /// The peer sent this capability but we did not advertise it. + Received, + + /// Both sides advertised this capability. Negotiated, } -impl AfiSafiState { +impl CapabilityState { + pub fn configured(&self) -> bool { + matches!(self, Self::Advertised | Self::Negotiated) + } + + pub fn advertised(&self) -> bool { + matches!(self, Self::Advertised | Self::Negotiated) + } + + pub fn received(&self) -> bool { + matches!(self, Self::Received | Self::Negotiated) + } + pub fn negotiated(&self) -> bool { matches!(self, Self::Negotiated) } } -impl Display for AfiSafiState { +impl Display for CapabilityState { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { Self::Unconfigured => write!(f, "Unconfigured"), Self::Advertised => write!(f, "Advertised"), + Self::Received => write!(f, "Received"), Self::Negotiated => write!(f, "Negotiated"), } } } +/// The result of comparing our OPEN capabilities with the peer's. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub struct NegotiatedCaps { + pub ipv4: CapabilityState, + pub ipv6: CapabilityState, + pub ext_msg: CapabilityState, + pub route_refresh: CapabilityState, + /// Extended nexthop: IPv4 routes carried over IPv6 nexthops (RFC 8950) + pub enhe_v4: CapabilityState, + /// Extended nexthop: IPv6 routes carried over IPv4 nexthops (RFC 8950) + pub enhe_v6: CapabilityState, +} + +impl NegotiatedCaps { + /// The peer is an MP-BGP speaker (advertised at least one + /// MultiprotocolExtensions capability). + pub fn mp_bgp(&self) -> bool { + self.ipv4.received() || self.ipv6.received() + } +} + /// This wraps a BgpConnection with runtime state learned from the peer's Open /// message. This encodes all the dynamic (but non-timer related) information /// for a given connection into the type system (rather than wrapping each @@ -117,19 +151,12 @@ pub struct PeerConnection { pub id: u32, /// The actual ASN learned from the peer (runtime state) pub asn: u32, - /// The actual capabilities received from the peer (runtime state) - pub caps: BTreeSet, - /// This peer's AFI/SAFI state for IPv4 Unicast - pub ipv4_unicast: AfiSafiState, - /// This peer's AFI/SAFI state for IPv6 Unicast - pub ipv6_unicast: AfiSafiState, - /// Indicates the peer supports MP-BGP, i.e. they advertised - /// Multiprotocol Extensions capabilities in their OPEN message. - /// True: the peer supports MP-BGP encoding. - /// False: the peer is a legacy BGP-4 speaker that does not support MP-BGP - /// encoding. IPv4 Unicast is implicitly negotiated, and is the only - /// address-family allowed for this peer. - pub mp_bgp: bool, + /// Negotiated capability state derived from both OPEN messages. + pub cap_state: NegotiatedCaps, + /// Capabilities we sent in our OPEN message. + pub tx_caps: BTreeSet, + /// Capabilities received from the peer's OPEN message. + pub rx_caps: BTreeSet, } impl PeerConnection { @@ -144,10 +171,9 @@ impl Clone for PeerConnection { conn: Arc::clone(&self.conn), id: self.id, asn: self.asn, - caps: self.caps.clone(), - ipv4_unicast: self.ipv4_unicast, - ipv6_unicast: self.ipv6_unicast, - mp_bgp: self.mp_bgp, + cap_state: self.cap_state, + tx_caps: self.tx_caps.clone(), + rx_caps: self.rx_caps.clone(), } } } @@ -241,7 +267,7 @@ fn select_nexthop( nlri_afi: Afi, local_ip: IpAddr, configured_nexthop: Option, - caps: &BTreeSet, + cap_state: &NegotiatedCaps, ) -> Result { // Canonicalize the local_ip to handle IPv4-mapped IPv6 addresses let local_ip = local_ip.to_canonical(); @@ -251,8 +277,12 @@ fn select_nexthop( return match (nlri_afi, nexthop) { (Afi::Ipv4, IpAddr::V4(ipv4)) => Ok(BgpNexthop::Ipv4(ipv4)), (Afi::Ipv6, IpAddr::V6(ipv6)) => Ok(BgpNexthop::Ipv6Single(ipv6)), - (Afi::Ipv4, IpAddr::V6(ipv6)) => v4_over_v6_nexthop(caps, ipv6), - (Afi::Ipv6, IpAddr::V4(ipv4)) => v6_over_v4_nexthop(caps, ipv4), + (Afi::Ipv4, IpAddr::V6(ipv6)) => { + v4_over_v6_nexthop(cap_state, ipv6) + } + (Afi::Ipv6, IpAddr::V4(ipv4)) => { + v6_over_v4_nexthop(cap_state, ipv4) + } }; } @@ -260,8 +290,8 @@ fn select_nexthop( match (nlri_afi, local_ip) { (Afi::Ipv4, IpAddr::V4(ipv4)) => Ok(BgpNexthop::Ipv4(ipv4)), (Afi::Ipv6, IpAddr::V6(ipv6)) => Ok(BgpNexthop::Ipv6Single(ipv6)), - (Afi::Ipv4, IpAddr::V6(ipv6)) => v4_over_v6_nexthop(caps, ipv6), - (Afi::Ipv6, IpAddr::V4(ipv4)) => v6_over_v4_nexthop(caps, ipv4), + (Afi::Ipv4, IpAddr::V6(ipv6)) => v4_over_v6_nexthop(cap_state, ipv6), + (Afi::Ipv6, IpAddr::V4(ipv4)) => v6_over_v4_nexthop(cap_state, ipv4), } } @@ -287,29 +317,27 @@ fn derive_nexthop_interface( } pub fn v4_over_v6_nexthop( - caps: &BTreeSet, + cap_state: &NegotiatedCaps, nexthop: Ipv6Addr, ) -> Result { - let v4_over_v6 = caps.iter().any(|x| x.extended_nh_v4_over_v6()); - if v4_over_v6 { + if cap_state.enhe_v4.received() { Ok(BgpNexthop::Ipv6Single(nexthop)) } else { Err(Error::InvalidAddress(format!( - "Ipv6 nexthop {nexthop} without extended NH v4 over v6 negotiated" + "Ipv6 nexthop {nexthop} without extended NH v4 over v6" ))) } } pub fn v6_over_v4_nexthop( - caps: &BTreeSet, + cap_state: &NegotiatedCaps, nexthop: Ipv4Addr, ) -> Result { - let v6_over_v4 = caps.iter().any(|x| x.extended_nh_v6_over_v4()); - if v6_over_v4 { + if cap_state.enhe_v6.received() { Ok(BgpNexthop::Ipv4(nexthop)) } else { Err(Error::InvalidAddress(format!( - "Ipv4 nexthop {nexthop} without extended NH v6 over v4 negotiated" + "Ipv4 nexthop {nexthop} without extended NH v6 over v4" ))) } } @@ -1372,20 +1400,13 @@ macro_rules! connect_timeout { }; } -/// Determine AFI/SAFI negotiation state from two capability sets. -/// -/// Returns `(ipv4_unicast, ipv6_unicast, mp_bgp)`: -/// - `Negotiated`: Both sides advertised the capability, **or** the peer -/// is a legacy (non-MP-BGP) speaker and we configured IPv4 Unicast -/// (implicit negotiation per RFC 4271). -/// - `Advertised`: We advertised but peer did not (and no implicit grant). -/// - `Unconfigured`: We did not advertise (not configured). -/// - `mp_bgp`: `true` if the peer advertised any -/// `MultiprotocolExtensions` capabilities. -pub(crate) fn negotiate_afis( +/// Compare our OPEN capabilities with the peer's and produce a +/// [`NegotiatedCaps`] describing the resulting state of each +/// capability we care about. +pub(crate) fn negotiate_capabilities( our_caps: &BTreeSet, their_caps: &BTreeSet, -) -> (AfiSafiState, AfiSafiState, bool) { +) -> NegotiatedCaps { let cap4 = Capability::ipv4_unicast(); let cap6 = Capability::ipv6_unicast(); @@ -1395,35 +1416,95 @@ pub(crate) fn negotiate_afis( .iter() .any(|c| matches!(c, Capability::MultiprotocolExtensions { .. })); - let ipv4_state = if our_caps.contains(&cap4) { + let ipv4 = if our_caps.contains(&cap4) { if mp_bgp { - // Peer sent MP-BGP caps: check for explicit v4 if their_caps.contains(&cap4) { - AfiSafiState::Negotiated + CapabilityState::Negotiated } else { - AfiSafiState::Advertised + CapabilityState::Advertised } } else { // Legacy peer (no MP-BGP caps): implicitly // negotiates IPv4 Unicast per RFC 4271. - AfiSafiState::Negotiated + CapabilityState::Negotiated } + } else if their_caps.contains(&cap4) { + CapabilityState::Received } else { - AfiSafiState::Unconfigured + CapabilityState::Unconfigured }; - let ipv6_state = if our_caps.contains(&cap6) { + let ipv6 = if our_caps.contains(&cap6) { if their_caps.contains(&cap6) { - AfiSafiState::Negotiated + CapabilityState::Negotiated } else { - // Legacy peers never get implicit IPv6. - AfiSafiState::Advertised + CapabilityState::Advertised } + } else if their_caps.contains(&cap6) { + CapabilityState::Received } else { - AfiSafiState::Unconfigured + CapabilityState::Unconfigured }; - (ipv4_state, ipv6_state, mp_bgp) + let ext_msg = + negotiate_cap(our_caps, their_caps, &Capability::BGPExtendedMessage {}); + + let route_refresh = + negotiate_cap(our_caps, their_caps, &Capability::RouteRefresh {}); + + let enhe_v4 = negotiate_enhe(our_caps, their_caps, |e| e.is_v4_over_v6()); + let enhe_v6 = negotiate_enhe(our_caps, their_caps, |e| e.is_v6_over_v4()); + + NegotiatedCaps { + ipv4, + ipv6, + ext_msg, + route_refresh, + enhe_v4, + enhe_v6, + } +} + +/// Determine the negotiation state of a single capability. +fn negotiate_cap( + our_caps: &BTreeSet, + their_caps: &BTreeSet, + cap: &Capability, +) -> CapabilityState { + match (our_caps.contains(cap), their_caps.contains(cap)) { + (true, true) => CapabilityState::Negotiated, + (true, false) => CapabilityState::Advertised, + (false, true) => CapabilityState::Received, + (false, false) => CapabilityState::Unconfigured, + } +} + +/// Determine the negotiation state of a specific ENHE element. +fn negotiate_enhe( + our_caps: &BTreeSet, + their_caps: &BTreeSet, + predicate: fn(&ExtendedNexthopElement) -> bool, +) -> CapabilityState { + let we_have = has_enhe_element(our_caps, predicate); + let they_have = has_enhe_element(their_caps, predicate); + match (we_have, they_have) { + (true, true) => CapabilityState::Negotiated, + (true, false) => CapabilityState::Advertised, + (false, true) => CapabilityState::Received, + (false, false) => CapabilityState::Unconfigured, + } +} + +fn has_enhe_element( + caps: &BTreeSet, + predicate: fn(&ExtendedNexthopElement) -> bool, +) -> bool { + caps.iter().any(|c| match c { + Capability::ExtendedNextHopEncoding { elements } => { + elements.iter().any(predicate) + } + _ => false, + }) } /// Registry for tracking active connections @@ -3985,12 +4066,13 @@ impl SessionRunner { // hold_timer set in handle_open(), enable it here conn_timer!(conn, hold).enable(); - let caps = om.get_capabilities(); - let (ipv4_unicast, ipv6_unicast, mp_bgp) = { + let rx_caps = om.get_capabilities(); + let (cap_state, tx_caps) = { let our_caps = lock!(self.caps_tx); - negotiate_afis(&our_caps, &caps) + let neg = negotiate_capabilities(&our_caps, &rx_caps); + (neg, our_caps.clone()) }; - if caps.contains(&Capability::BGPExtendedMessage {}) { + if cap_state.ext_msg.negotiated() { conn.set_extended_msg(true); } @@ -3998,10 +4080,9 @@ impl SessionRunner { conn, id: om.id, asn: om.asn(), - caps, - ipv4_unicast, - ipv6_unicast, - mp_bgp, + cap_state, + tx_caps, + rx_caps, }; // Upgrade this connection from Partial to Full in the registry @@ -5354,12 +5435,13 @@ impl SessionRunner { .connection_retries .fetch_add(1, Ordering::Relaxed); - let caps = om.get_capabilities(); - let (ipv4_unicast, ipv6_unicast, mp_bgp) = { + let rx_caps = om.get_capabilities(); + let (cap_state, tx_caps) = { let our_caps = lock!(self.caps_tx); - negotiate_afis(&our_caps, &caps) + let neg = negotiate_capabilities(&our_caps, &rx_caps); + (neg, our_caps.clone()) }; - if caps.contains(&Capability::BGPExtendedMessage {}) { + if cap_state.ext_msg.negotiated() { new.set_extended_msg(true); } @@ -5367,10 +5449,9 @@ impl SessionRunner { conn: new, id: om.id, asn: om.asn(), - caps, - ipv4_unicast, - ipv6_unicast, - mp_bgp, + cap_state, + tx_caps, + rx_caps, }; conn_timer!(new_pc.conn, hold).restart(); @@ -5675,12 +5756,13 @@ impl SessionRunner { conn_timer!(exist, hold).restart(); conn_timer!(exist, keepalive).restart(); - let caps = om.get_capabilities(); - let (ipv4_unicast, ipv6_unicast, mp_bgp) = { + let rx_caps = om.get_capabilities(); + let (cap_state, tx_caps) = { let our_caps = lock!(self.caps_tx); - negotiate_afis(&our_caps, &caps) + let neg = negotiate_capabilities(&our_caps, &rx_caps); + (neg, our_caps.clone()) }; - if caps.contains(&Capability::BGPExtendedMessage {}) { + if cap_state.ext_msg.negotiated() { exist.set_extended_msg(true); } @@ -5688,10 +5770,9 @@ impl SessionRunner { conn: exist.clone(), id: om.id, asn: om.asn(), - caps, - ipv4_unicast, - ipv6_unicast, - mp_bgp, + cap_state, + tx_caps, + rx_caps, }; // Upgrade existing connection from Partial to Full in the registry @@ -5815,12 +5896,13 @@ impl SessionRunner { self.stop(Some(&exist), None, StopReason::CollisionResolution); - let caps = om.get_capabilities(); - let (ipv4_unicast, ipv6_unicast, mp_bgp) = { + let rx_caps = om.get_capabilities(); + let (cap_state, tx_caps) = { let our_caps = lock!(self.caps_tx); - negotiate_afis(&our_caps, &caps) + let neg = negotiate_capabilities(&our_caps, &rx_caps); + (neg, our_caps.clone()) }; - if caps.contains(&Capability::BGPExtendedMessage {}) { + if cap_state.ext_msg.negotiated() { new.set_extended_msg(true); } @@ -5828,10 +5910,9 @@ impl SessionRunner { conn: new.clone(), id: om.id, asn: om.asn(), - caps, - ipv4_unicast, - ipv6_unicast, - mp_bgp, + cap_state, + tx_caps, + rx_caps, }; conn_timer!(new, hold).restart(); @@ -6224,7 +6305,7 @@ impl SessionRunner { } // Collect the prefixes this router is originating. - let originated4 = if pc.ipv4_unicast.negotiated() { + let originated4 = if pc.cap_state.ipv4.negotiated() { match self.db.get_origin(Some(AddressFamily::Ipv4)) { Ok(value) => value, Err(e) => { @@ -6243,7 +6324,7 @@ impl SessionRunner { Vec::new() }; - let originated6 = if pc.ipv6_unicast.negotiated() { + let originated6 = if pc.cap_state.ipv6.negotiated() { match self.db.get_origin(Some(AddressFamily::Ipv6)) { Ok(value) => value, Err(e) => { @@ -6264,7 +6345,7 @@ impl SessionRunner { // Ensure the router has a fanout entry for this peer. let peer_id = self.peer_id(); - if pc.ipv4_unicast.negotiated() { + if pc.cap_state.ipv4.negotiated() { write_lock!(self.fanout4).add_egress( peer_id.clone(), crate::fanout::Egress { @@ -6273,7 +6354,7 @@ impl SessionRunner { }, ); } - if pc.ipv6_unicast.negotiated() { + if pc.cap_state.ipv6.negotiated() { write_lock!(self.fanout6).add_egress( peer_id, crate::fanout::Egress { @@ -6765,13 +6846,13 @@ impl SessionRunner { AdminEvent::EnforceFirstAsEnabled => { let peer_id = self.peer_id(); - if pc.ipv4_unicast.negotiated() { + if pc.cap_state.ipv4.negotiated() { self.db.enforce_first_as( &peer_id, Some(AddressFamily::Ipv4), ); } - if pc.ipv6_unicast.negotiated() { + if pc.cap_state.ipv6.negotiated() { self.db.enforce_first_as( &peer_id, Some(AddressFamily::Ipv6), @@ -7137,28 +7218,28 @@ impl SessionRunner { msg_kind, false, ); - let caps = om.get_capabilities(); - let ( - ipv4_unicast, - ipv6_unicast, - mp_bgp, - ) = { + let rx_caps = om.get_capabilities(); + let (cap_state, tx_caps) = { let our_caps = lock!(self.caps_tx); - negotiate_afis(&our_caps, &caps) + let neg = + negotiate_capabilities( + &our_caps, &rx_caps, + ); + (neg, our_caps.clone()) }; - if caps.contains(&Capability::BGPExtendedMessage {}) { - incoming_conn.set_extended_msg(true); + if cap_state.ext_msg.negotiated() { + incoming_conn + .set_extended_msg(true); } let new_pc = PeerConnection { conn: incoming_conn.clone(), id: om.id, asn: om.asn(), - caps, - ipv4_unicast, - ipv6_unicast, - mp_bgp, + cap_state, + tx_caps, + rx_caps, }; // Clean up the old established connection @@ -7548,10 +7629,10 @@ impl SessionRunner { // (non-MP-BGP) peers when we don't have IPv4 configured. let their_caps = om.get_capabilities(); let our_caps = lock!(self.caps_tx); - let (ipv4, ipv6, _mp_bgp) = negotiate_afis(&our_caps, &their_caps); + let neg = negotiate_capabilities(&our_caps, &their_caps); drop(our_caps); - if !ipv4.negotiated() && !ipv6.negotiated() { + if !neg.ipv4.negotiated() && !neg.ipv6.negotiated() { session_log!( self, warn, @@ -8012,7 +8093,7 @@ impl SessionRunner { nlri_afi, pc.conn.local().ip(), configured_nexthop, - &pc.caps, + &pc.cap_state, ) } @@ -8342,14 +8423,14 @@ impl SessionRunner { session_timer!(self, connect_retry).stop(); let peer_id = self.peer_id(); - if pc.ipv4_unicast.negotiated() { + if pc.cap_state.ipv4.negotiated() { write_lock!(self.fanout4).remove_egress(&peer_id); self.db.remove_all_prefixes_from_bgp_peer( &peer_id, Some(AddressFamily::Ipv4), ); } - if pc.ipv6_unicast.negotiated() { + if pc.cap_state.ipv6.negotiated() { write_lock!(self.fanout6).remove_egress(&peer_id); self.db.remove_all_prefixes_from_bgp_peer( &peer_id, @@ -8691,7 +8772,7 @@ impl SessionRunner { // A legacy peer must only use traditional encoding. // Strip any MP-BGP attributes they send -- they have no // business using multiprotocol extensions. - if !pc.mp_bgp { + if !pc.cap_state.mp_bgp() { let has_mp_attrs = update.path_attributes.iter().any(|a| { matches!( a.value, @@ -8750,8 +8831,8 @@ impl SessionRunner { // Check if AFI/SAFI was negotiated let afi_state = match (afi, safi) { - (Afi::Ipv4, Safi::Unicast) => pc.ipv4_unicast, - (Afi::Ipv6, Safi::Unicast) => pc.ipv6_unicast, + (Afi::Ipv4, Safi::Unicast) => pc.cap_state.ipv4, + (Afi::Ipv6, Safi::Unicast) => pc.cap_state.ipv6, }; if !afi_state.negotiated() { @@ -8785,8 +8866,8 @@ impl SessionRunner { // Check if AFI/SAFI was negotiated let afi_state = match (afi, safi) { - (Afi::Ipv4, Safi::Unicast) => pc.ipv4_unicast, - (Afi::Ipv6, Safi::Unicast) => pc.ipv6_unicast, + (Afi::Ipv4, Safi::Unicast) => pc.cap_state.ipv4, + (Afi::Ipv6, Safi::Unicast) => pc.cap_state.ipv6, }; if !afi_state.negotiated() { @@ -8825,7 +8906,7 @@ impl SessionRunner { &self, pc: &PeerConnection, ) -> Result<(), Error> { - if !pc.ipv4_unicast.negotiated() { + if !pc.cap_state.ipv4.negotiated() { return Ok(()); } @@ -8866,7 +8947,7 @@ impl SessionRunner { &self, pc: &PeerConnection, ) -> Result<(), Error> { - if !pc.ipv6_unicast.negotiated() { + if !pc.cap_state.ipv6.negotiated() { return Ok(()); } @@ -8939,7 +9020,7 @@ impl SessionRunner { // Traditional NLRI / withdrawn routes fields are IPv4 Unicast // only. Only process them if IPv4 Unicast was negotiated. - if pc.ipv4_unicast.negotiated() { + if pc.cap_state.ipv4.negotiated() { let withdrawn: Vec = update .withdrawn .iter() @@ -9055,7 +9136,7 @@ impl SessionRunner { } // Non-MP-BGP peers only use traditional NLRI (handled above). - if !pc.mp_bgp { + if !pc.cap_state.mp_bgp() { return; } @@ -9756,7 +9837,7 @@ impl SessionRunner { let local = pc.conn.local(); let remote = pc.conn.peer(); let received_capabilities = - pc.caps.iter().map(BgpCapability::from).collect(); + pc.rx_caps.iter().map(BgpCapability::from).collect(); PeerInfo { name, peer_group: peer_group.clone(), @@ -10169,7 +10250,7 @@ mod tests { Afi::Ipv4, local_ip, Some(configured_nh), - &BTreeSet::default(), + &NegotiatedCaps::default(), ); assert!(result.is_ok()); match result.unwrap() { @@ -10191,7 +10272,7 @@ mod tests { Afi::Ipv6, local_ip, Some(configured_nh), - &BTreeSet::default(), + &NegotiatedCaps::default(), ); assert!(result.is_ok()); match result.unwrap() { @@ -10208,8 +10289,12 @@ mod tests { // No nexthop configured, pure IPv4 local_ip should be used for IPv4 routes let local_ip = ip!("10.0.0.1"); - let result = - select_nexthop(Afi::Ipv4, local_ip, None, &BTreeSet::default()); + let result = select_nexthop( + Afi::Ipv4, + local_ip, + None, + &NegotiatedCaps::default(), + ); assert!(result.is_ok()); match result.unwrap() { BgpNexthop::Ipv4(addr) => { @@ -10228,7 +10313,7 @@ mod tests { let mapped = ip!("::ffff:10.0.0.1"); let result = - select_nexthop(Afi::Ipv4, mapped, None, &BTreeSet::default()); + select_nexthop(Afi::Ipv4, mapped, None, &NegotiatedCaps::default()); assert!(result.is_ok()); match result.unwrap() { BgpNexthop::Ipv4(addr) => { @@ -10244,8 +10329,12 @@ mod tests { // No nexthop configured, pure IPv6 local_ip should be used for IPv6 routes let local_ip = ip!("2001:db8::1"); - let result = - select_nexthop(Afi::Ipv6, local_ip, None, &BTreeSet::default()); + let result = select_nexthop( + Afi::Ipv6, + local_ip, + None, + &NegotiatedCaps::default(), + ); assert!(result.is_ok()); match result.unwrap() { BgpNexthop::Ipv6Single(addr) => { @@ -10266,7 +10355,7 @@ mod tests { Afi::Ipv4, local_ip, Some(nexthop), - &BTreeSet::default(), + &NegotiatedCaps::default(), ); // Should error because IPv4 route needs IPv4 nexthop assert!(result.is_err()); @@ -10282,7 +10371,7 @@ mod tests { Afi::Ipv6, local_ip, Some(nexthop), - &BTreeSet::default(), + &NegotiatedCaps::default(), ); // Should error because IPv6 route needs IPv6 nexthop assert!(result.is_err()); @@ -10293,8 +10382,12 @@ mod tests { // IPv4 route with pure IPv6 local_ip and no configured nexthop = error let local_ip = ip!("2001:db8::1"); - let result = - select_nexthop(Afi::Ipv4, local_ip, None, &BTreeSet::default()); + let result = select_nexthop( + Afi::Ipv4, + local_ip, + None, + &NegotiatedCaps::default(), + ); // Should error because cannot derive IPv4 nexthop from IPv6 connection assert!(result.is_err()); } @@ -10304,8 +10397,12 @@ mod tests { // IPv6 route with pure IPv4 local_ip and no configured nexthop = error let local_ip = ip!("10.0.0.1"); - let result = - select_nexthop(Afi::Ipv6, local_ip, None, &BTreeSet::default()); + let result = select_nexthop( + Afi::Ipv6, + local_ip, + None, + &NegotiatedCaps::default(), + ); // Should error because cannot derive IPv6 nexthop from IPv4 connection assert!(result.is_err()); } diff --git a/mgd/src/bgp_admin.rs b/mgd/src/bgp_admin.rs index bc35ae734..f6c64b9d4 100644 --- a/mgd/src/bgp_admin.rs +++ b/mgd/src/bgp_admin.rs @@ -2840,8 +2840,8 @@ pub(crate) mod helpers { // Extract negotiated AFI/SAFI states from the connection let (ipv4_negotiated, ipv6_negotiated) = match primary { ConnectionKind::Full(ref peer_conn) => ( - peer_conn.ipv4_unicast.negotiated(), - peer_conn.ipv6_unicast.negotiated(), + peer_conn.cap_state.ipv4.negotiated(), + peer_conn.cap_state.ipv6.negotiated(), ), ConnectionKind::Partial(_) => return None, }; From 0ff08fb3ec439e2ad09e678e8c4df853bc6be240 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 11 Mar 2026 22:56:52 -0600 Subject: [PATCH 37/58] bgp: store extended-nexthop capability in caps_tx Actually store ENHE in caps_tx so we can track the negotiation state of that capability for a given peer. --- bgp/src/session.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/bgp/src/session.rs b/bgp/src/session.rs index 9a727c317..a7e5e8785 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -320,7 +320,7 @@ pub fn v4_over_v6_nexthop( cap_state: &NegotiatedCaps, nexthop: Ipv6Addr, ) -> Result { - if cap_state.enhe_v4.received() { + if cap_state.enhe_v4.negotiated() { Ok(BgpNexthop::Ipv6Single(nexthop)) } else { Err(Error::InvalidAddress(format!( @@ -333,7 +333,7 @@ pub fn v6_over_v4_nexthop( cap_state: &NegotiatedCaps, nexthop: Ipv4Addr, ) -> Result { - if cap_state.enhe_v6.received() { + if cap_state.enhe_v6.negotiated() { Ok(BgpNexthop::Ipv4(nexthop)) } else { Err(Error::InvalidAddress(format!( @@ -2513,6 +2513,15 @@ impl SessionRunner { if lock!(self.session).ipv6_unicast.is_some() { caps.insert(Capability::ipv6_unicast()); } + if self.use_extended_nexthop() { + caps.insert(Capability::ExtendedNextHopEncoding { + elements: vec![ExtendedNexthopElement { + afi: Afi::Ipv4.into(), + safi: u8::from(Safi::Unicast).into(), + nh_afi: Afi::Ipv6.into(), + }], + }); + } *lock!(self.caps_tx) = caps; } @@ -7899,7 +7908,7 @@ impl SessionRunner { let capabilities = lock!(self.caps_tx).clone(); // pull hold_time from config, not the clock let hold_time = lock!(self.session).hold_time; - let extended_nexthop = self.v4_over_v6_nexthop(); + let extended_nexthop = self.use_extended_nexthop(); let mut msg = match self.asn { Asn::FourOctet(asn) => OpenMessage::new4( asn, @@ -7974,10 +7983,8 @@ impl SessionRunner { } } - fn v4_over_v6_nexthop(&self) -> bool { - let v4_enabled = lock!(self.session).ipv4_unicast.is_some(); - let v6_peer = self.get_peer_info().remote_ip.is_ipv6(); - v4_enabled && v6_peer + fn use_extended_nexthop(&self) -> bool { + self.is_unnumbered() } fn is_ebgp(&self) -> Option { From dad89ecc6454072e15e00ce76802ba08bc0da449 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 11 Mar 2026 23:28:24 -0600 Subject: [PATCH 38/58] bgp: use ipv4-mapped ipv6 addresses for v6-over-v4 When IPv6 NLRI are exchanged using IPv4 next-hops, they use IPv4-Mapped IPv6 addresses for the next-hop rather than relying on ENHE capabilities (Extended Next-Hop Encoding) describing IPv4 NH-AFI for IPv6 Unicast. This updates our nexthop selection to return an IPv4-Mapped IPv6 address on the sending side (for cases where IPv6 Unicast has been configured with an explicit nexthop override where the address is IPv4), and our UPDATE processing to normalize the nexthop prior to RIB installation on the receiving side. e.g. IpAddr::V6(::ffff:10.0.0.1) becomes IpAddr::V4(10.0.0.1) prior to RIB insertion so mg-lower/ddm/dendrite never see a mapped address. --- bgp/src/messages.rs | 17 +++++++++ bgp/src/session.rs | 92 ++++++++++++++------------------------------- 2 files changed, 45 insertions(+), 64 deletions(-) diff --git a/bgp/src/messages.rs b/bgp/src/messages.rs index 49a8d0aee..77a8ee6f0 100644 --- a/bgp/src/messages.rs +++ b/bgp/src/messages.rs @@ -3031,6 +3031,23 @@ impl BgpNexthop { } } + /// Resolve to a canonical IpAddr for RIB installation. + pub fn to_ip(&self, unnumbered: bool) -> IpAddr { + let ip = match self { + BgpNexthop::Ipv4(ip4) => IpAddr::V4(*ip4), + BgpNexthop::Ipv6Single(ip6) => IpAddr::V6(*ip6), + BgpNexthop::Ipv6Double(addrs) => { + if unnumbered { + IpAddr::V6(addrs.link_local) + } else { + IpAddr::V6(addrs.global) + } + } + }; + + ip.to_canonical() + } + /// Get byte length of this next-hop pub fn byte_len(&self) -> u8 { match self { diff --git a/bgp/src/session.rs b/bgp/src/session.rs index a7e5e8785..11f1f51fb 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -118,14 +118,16 @@ impl Display for CapabilityState { /// The result of comparing our OPEN capabilities with the peer's. #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] pub struct NegotiatedCaps { + /// IPv4 Unicast address-family pub ipv4: CapabilityState, + /// IPv6 Unicast address-family pub ipv6: CapabilityState, + /// BGP Extended Message Size (RFC 8654) pub ext_msg: CapabilityState, + /// Route Refresh (RFC 2918) pub route_refresh: CapabilityState, /// Extended nexthop: IPv4 routes carried over IPv6 nexthops (RFC 8950) - pub enhe_v4: CapabilityState, - /// Extended nexthop: IPv6 routes carried over IPv4 nexthops (RFC 8950) - pub enhe_v6: CapabilityState, + pub enhe: CapabilityState, } impl NegotiatedCaps { @@ -281,7 +283,7 @@ fn select_nexthop( v4_over_v6_nexthop(cap_state, ipv6) } (Afi::Ipv6, IpAddr::V4(ipv4)) => { - v6_over_v4_nexthop(cap_state, ipv4) + Ok(BgpNexthop::Ipv6Single(ipv4.to_ipv6_mapped())) } }; } @@ -291,7 +293,9 @@ fn select_nexthop( (Afi::Ipv4, IpAddr::V4(ipv4)) => Ok(BgpNexthop::Ipv4(ipv4)), (Afi::Ipv6, IpAddr::V6(ipv6)) => Ok(BgpNexthop::Ipv6Single(ipv6)), (Afi::Ipv4, IpAddr::V6(ipv6)) => v4_over_v6_nexthop(cap_state, ipv6), - (Afi::Ipv6, IpAddr::V4(ipv4)) => v6_over_v4_nexthop(cap_state, ipv4), + (Afi::Ipv6, IpAddr::V4(ipv4)) => { + Ok(BgpNexthop::Ipv6Single(ipv4.to_ipv6_mapped())) + } } } @@ -320,7 +324,7 @@ pub fn v4_over_v6_nexthop( cap_state: &NegotiatedCaps, nexthop: Ipv6Addr, ) -> Result { - if cap_state.enhe_v4.negotiated() { + if cap_state.enhe.negotiated() { Ok(BgpNexthop::Ipv6Single(nexthop)) } else { Err(Error::InvalidAddress(format!( @@ -329,19 +333,6 @@ pub fn v4_over_v6_nexthop( } } -pub fn v6_over_v4_nexthop( - cap_state: &NegotiatedCaps, - nexthop: Ipv4Addr, -) -> Result { - if cap_state.enhe_v6.negotiated() { - Ok(BgpNexthop::Ipv4(nexthop)) - } else { - Err(Error::InvalidAddress(format!( - "Ipv4 nexthop {nexthop} without extended NH v6 over v4" - ))) - } -} - /// The states a BGP finite state machine may be at any given time. Many /// of these states carry a connection by value. This is the same connection /// that moves from state to state as transitions are made. Transitions from @@ -1452,16 +1443,14 @@ pub(crate) fn negotiate_capabilities( let route_refresh = negotiate_cap(our_caps, their_caps, &Capability::RouteRefresh {}); - let enhe_v4 = negotiate_enhe(our_caps, their_caps, |e| e.is_v4_over_v6()); - let enhe_v6 = negotiate_enhe(our_caps, their_caps, |e| e.is_v6_over_v4()); + let enhe = negotiate_enhe(our_caps, their_caps, |e| e.is_v4_over_v6()); NegotiatedCaps { ipv4, ipv6, ext_msg, route_refresh, - enhe_v4, - enhe_v6, + enhe, } } @@ -9169,17 +9158,8 @@ impl SessionRunner { mp_withdrawn4.len() as u64, ); } else { - let mp_nexthop = match &reach4.nexthop { - BgpNexthop::Ipv4(ip4) => IpAddr::V4(*ip4), - BgpNexthop::Ipv6Single(ip6) => IpAddr::V6(*ip6), - BgpNexthop::Ipv6Double(addrs) => { - if self.is_unnumbered() { - IpAddr::V6(addrs.link_local) - } else { - IpAddr::V6(addrs.global) - } - } - }; + let mp_nexthop = + reach4.nexthop.to_ip(self.is_unnumbered()); let mp_nlri4: Vec = reach4 .nlri @@ -9253,28 +9233,8 @@ impl SessionRunner { withdrawn6.len() as u64, ); } else { - let nexthop6 = match &reach6.nexthop { - BgpNexthop::Ipv6Single(ip6) => IpAddr::V6(*ip6), - BgpNexthop::Ipv6Double(addrs) => { - if self.is_unnumbered() { - IpAddr::V6(addrs.link_local) - } else { - IpAddr::V6(addrs.global) - } - } - BgpNexthop::Ipv4(ip4) => { - // IPv4 nexthop for IPv6 routes is unusual but possible - // in some configurations (e.g., IPv4-mapped IPv6) - session_log!( - self, - warn, - pc.conn, - "IPv4 nexthop in IPv6 MP_REACH_NLRI"; - "nexthop" => format!("{ip4}") - ); - IpAddr::V4(*ip4) - } - }; + let nexthop6 = + reach6.nexthop.to_ip(self.is_unnumbered()); let nlri6: Vec = reach6 .nlri @@ -10369,8 +10329,8 @@ mod tests { } #[test] - fn test_select_nexthop_wrong_af_ipv6_route_ipv4_nexthop() { - // IPv6 route with IPv4 nexthop configured is a mismatch (error) + fn test_select_nexthop_ipv6_route_ipv4_nexthop_mapped() { + // IPv6 route with configured IPv4 nexthop uses v4-mapped v6 let nexthop = ip!("10.0.0.1"); let local_ip = ip!("2001:db8::1"); @@ -10380,8 +10340,10 @@ mod tests { Some(nexthop), &NegotiatedCaps::default(), ); - // Should error because IPv6 route needs IPv6 nexthop - assert!(result.is_err()); + assert_eq!( + result.unwrap(), + BgpNexthop::Ipv6Single("::ffff:10.0.0.1".parse().unwrap()), + ); } #[test] @@ -10400,8 +10362,8 @@ mod tests { } #[test] - fn test_select_nexthop_cross_af_ipv6_routes_ipv4_local_ip_error() { - // IPv6 route with pure IPv4 local_ip and no configured nexthop = error + fn test_select_nexthop_cross_af_ipv6_routes_ipv4_local_ip_mapped() { + // IPv6 route with IPv4 local_ip uses v4-mapped v6 nexthop let local_ip = ip!("10.0.0.1"); let result = select_nexthop( @@ -10410,8 +10372,10 @@ mod tests { None, &NegotiatedCaps::default(), ); - // Should error because cannot derive IPv6 nexthop from IPv4 connection - assert!(result.is_err()); + assert_eq!( + result.unwrap(), + BgpNexthop::Ipv6Single("::ffff:10.0.0.1".parse().unwrap()), + ); } // ================================================================ From b287527046d2624620dc0302365e9329f5c7c697 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Thu, 12 Mar 2026 00:05:51 -0600 Subject: [PATCH 39/58] Finalize extended nexthops for numbered peers Last bits of logic to ensure extended nexthops work for numbered peers. A nexthop override configuration already takes an IpAddr, allowing for either v4 or v6 next-hops to be configured. This ensures that we use proper encoding anytime nexthop-afi != nlri-afi (ENHE for v4-over-v6, v4-mapped v6 addresses for v6-over-v4). Unit and integration tests are added to cover these cases. Also adds in missing type conversions for ENHE and BGP Extended Message capabilities, ensuring they both appear as the correct enum variant rather than the catch-all unknown. --- bgp/src/params.rs | 55 +++++- bgp/src/session.rs | 68 ++++++- bgp/src/test.rs | 185 +++++++++++++++++- mgadm/src/bgp.rs | 14 ++ ...af3143.json => mg-admin-7.0.0-36296f.json} | 101 +++++++++- openapi/mg-admin/mg-admin-latest.json | 2 +- 6 files changed, 417 insertions(+), 8 deletions(-) rename openapi/mg-admin/{mg-admin-7.0.0-af3143.json => mg-admin-7.0.0-36296f.json} (98%) diff --git a/bgp/src/params.rs b/bgp/src/params.rs index 7a24c06d2..14e7c0538 100644 --- a/bgp/src/params.rs +++ b/bgp/src/params.rs @@ -5,7 +5,7 @@ use crate::{ config::PeerConfig, messages::{AddPathElement, Afi, Capability}, - session::{FsmStateKind, SessionCounters, SessionInfo}, + session::{FsmStateKind, NegotiatedCaps, SessionCounters, SessionInfo}, }; use rdb::{ Dscp, ImportExportPolicy4, ImportExportPolicy6, ImportExportPolicyV1, @@ -807,6 +807,8 @@ pub enum BgpCapability { RouteRefresh, FourOctetAsn(u32), AddPath { elements: Vec }, + ExtendedNextHopEncoding, + BGPExtendedMessage, Unknown(u8), } @@ -826,11 +828,52 @@ impl From<&Capability> for BgpCapability { Capability::AddPath { elements } => BgpCapability::AddPath { elements: elements.iter().map(AfiSafi::from).collect(), }, + Capability::ExtendedNextHopEncoding { .. } => { + BgpCapability::ExtendedNextHopEncoding + } + Capability::BGPExtendedMessage {} => { + BgpCapability::BGPExtendedMessage + } c => BgpCapability::Unknown(c.code() as u8), } } } +/// Previous version of BgpCapability. +/// Used for API versions before VERSION_EXTENDED_NH_STATIC. +/// Delete when VERSION_EXTENDED_NH_STATIC is the minimum supported version. +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +#[schemars(rename = "BgpCapability")] +pub enum BgpCapabilityV1 { + MultiprotocolExtensions(AfiSafi), + RouteRefresh, + FourOctetAsn(u32), + AddPath { elements: Vec }, + Unknown(u8), +} + +impl From for BgpCapabilityV1 { + fn from(cap: BgpCapability) -> Self { + match cap { + BgpCapability::MultiprotocolExtensions(afi_safi) => { + BgpCapabilityV1::MultiprotocolExtensions(afi_safi) + } + BgpCapability::RouteRefresh => BgpCapabilityV1::RouteRefresh, + BgpCapability::FourOctetAsn(asn) => { + BgpCapabilityV1::FourOctetAsn(asn) + } + BgpCapability::AddPath { elements } => { + BgpCapabilityV1::AddPath { elements } + } + BgpCapability::ExtendedNextHopEncoding => { + BgpCapabilityV1::Unknown(5) + } + BgpCapability::BGPExtendedMessage => BgpCapabilityV1::Unknown(6), + BgpCapability::Unknown(code) => BgpCapabilityV1::Unknown(code), + } + } +} + /// Peer info for v5-v6 API (aggregate NLRI counters, no reset/notification). #[derive(Debug, Deserialize, Serialize, JsonSchema)] #[schemars(rename = "PeerInfo")] @@ -845,7 +888,7 @@ pub struct PeerInfoV3 { pub remote_ip: IpAddr, pub local_tcp_port: u16, pub remote_tcp_port: u16, - pub received_capabilities: Vec, + pub received_capabilities: Vec, pub timers: PeerTimers, pub counters: PeerCountersV1, pub ipv4_unicast: Ipv4UnicastConfig, @@ -1061,6 +1104,8 @@ pub struct PeerInfo { pub local_tcp_port: u16, pub remote_tcp_port: u16, pub received_capabilities: Vec, + pub sent_capabilities: Vec, + pub cap_state: NegotiatedCaps, pub timers: PeerTimers, pub counters: PeerCounters, pub ipv4_unicast: Ipv4UnicastConfig, @@ -1083,7 +1128,11 @@ impl From for PeerInfoV3 { remote_ip: info.remote_ip, local_tcp_port: info.local_tcp_port, remote_tcp_port: info.remote_tcp_port, - received_capabilities: info.received_capabilities, + received_capabilities: info + .received_capabilities + .into_iter() + .map(BgpCapabilityV1::from) + .collect(), timers: info.timers, counters: PeerCountersV1 { connection_retries: info.counters.connection_retries, diff --git a/bgp/src/session.rs b/bgp/src/session.rs index 11f1f51fb..5015f2d6f 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -116,7 +116,17 @@ impl Display for CapabilityState { } /// The result of comparing our OPEN capabilities with the peer's. -#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +#[derive( + Debug, + Clone, + Copy, + Default, + PartialEq, + Eq, + Serialize, + Deserialize, + JsonSchema, +)] pub struct NegotiatedCaps { /// IPv4 Unicast address-family pub ipv4: CapabilityState, @@ -7973,7 +7983,19 @@ impl SessionRunner { } fn use_extended_nexthop(&self) -> bool { - self.is_unnumbered() + // ENHE is always used for unnumbered peers. + if self.is_unnumbered() { + return true; + } + // Numbered peers use ENHE if: + // 1. We peer with them over IPv6 + // 2. IPv4 Unicast is enabled + // 3. The nexthop for IPv4 Unicast is not IPv4 (v4 over v4 is not ENHE) + matches!(self.neighbor.peer, PeerId::Ip(IpAddr::V6(_))) + && lock!(self.session) + .ipv4_unicast + .as_ref() + .is_some_and(|c| !matches!(c.nexthop, Some(IpAddr::V4(_)))) } fn is_ebgp(&self) -> Option { @@ -9790,6 +9812,8 @@ impl SessionRunner { local_tcp_port: local.port(), remote_tcp_port: remote.port(), received_capabilities: vec![], + sent_capabilities: vec![], + cap_state: NegotiatedCaps::default(), timers, counters, ipv4_unicast, @@ -9805,6 +9829,8 @@ impl SessionRunner { let remote = pc.conn.peer(); let received_capabilities = pc.rx_caps.iter().map(BgpCapability::from).collect(); + let sent_capabilities = + pc.tx_caps.iter().map(BgpCapability::from).collect(); PeerInfo { name, peer_group: peer_group.clone(), @@ -9817,6 +9843,8 @@ impl SessionRunner { local_tcp_port: local.port(), remote_tcp_port: remote.port(), received_capabilities, + sent_capabilities, + cap_state: pc.cap_state, timers, counters, ipv4_unicast, @@ -9856,6 +9884,8 @@ impl SessionRunner { local_tcp_port: 0u16, remote_tcp_port: self.neighbor.port, received_capabilities: vec![], + sent_capabilities: vec![], + cap_state: NegotiatedCaps::default(), timers, counters, ipv4_unicast, @@ -10361,6 +10391,40 @@ mod tests { assert!(result.is_err()); } + #[test] + fn test_select_nexthop_cross_af_ipv4_routes_ipv6_enhe() { + // IPv4 route with IPv6 local_ip and ENHE negotiated = IPv6 nexthop + let local_ip = ip!("2001:db8::1"); + let caps = NegotiatedCaps { + enhe: CapabilityState::Negotiated, + ..Default::default() + }; + + let result = select_nexthop(Afi::Ipv4, local_ip, None, &caps); + assert_eq!( + result.unwrap(), + BgpNexthop::Ipv6Single("2001:db8::1".parse().unwrap()), + ); + } + + #[test] + fn test_select_nexthop_v4_mapped_v6_canonicalized() { + // v4-mapped v6 local_ip is canonicalized to IPv4 before matching + let local_ip: IpAddr = + "::ffff:10.0.0.1".parse::().unwrap().into(); + + let result = select_nexthop( + Afi::Ipv4, + local_ip, + None, + &NegotiatedCaps::default(), + ); + assert_eq!( + result.unwrap(), + BgpNexthop::Ipv4("10.0.0.1".parse().unwrap()), + ); + } + #[test] fn test_select_nexthop_cross_af_ipv6_routes_ipv4_local_ip_mapped() { // IPv6 route with IPv4 local_ip uses v4-mapped v6 nexthop diff --git a/bgp/src/test.rs b/bgp/src/test.rs index 2c52d68bb..c5973d676 100644 --- a/bgp/src/test.rs +++ b/bgp/src/test.rs @@ -2169,7 +2169,7 @@ fn test_dual_stack_routes_ipv6_peer_success() { #[test] fn test_ipv4_routes_ipv6_peer_success() { - // IPv6 connection with IPv4-only routes + // IPv6 connection with IPv4-only routes (configured IPv4 nexthop) basic_update_helper::( RouteExchange::Ipv4 { nexthop: Some(ip!("10.0.3.1")), @@ -2179,6 +2179,95 @@ fn test_ipv4_routes_ipv6_peer_success() { ) } +/// IPv4 routes over an IPv6-addressed numbered peer with no configured +/// nexthop. Both sides negotiate ENHE, so the IPv6 connection address +/// is used as the nexthop for IPv4 NLRI. +#[test] +fn test_ipv4_routes_ipv6_peer_enhe() { + let r1_addr: SocketAddr = sockaddr!(&format!("[3fff::14]:{TEST_BGP_PORT}")); + let r2_addr: SocketAddr = sockaddr!(&format!("[3fff::15]:{TEST_BGP_PORT}")); + + let routers = vec![ + LogicalRouter { + name: "r1".to_string(), + asn: Asn::FourOctet(4200000001), + id: 1, + listen_addr: r1_addr, + bind_addr: Some(r1_addr), + neighbors: vec![NeighborConfig { + peer_name: "r2".to_string(), + remote_host: r2_addr, + session_info: create_test_session_info( + RouteExchange::Ipv4 { nexthop: None }, + r1_addr, + r2_addr, + true, + ), + }], + }, + LogicalRouter { + name: "r2".to_string(), + asn: Asn::FourOctet(4200000002), + id: 2, + listen_addr: r2_addr, + bind_addr: Some(r2_addr), + neighbors: vec![NeighborConfig { + peer_name: "r1".to_string(), + remote_host: r1_addr, + session_info: create_test_session_info( + RouteExchange::Ipv4 { nexthop: None }, + r2_addr, + r1_addr, + false, + ), + }], + }, + ]; + + let (test_routers, _ip_guard) = + test_setup::( + "ipv4_routes_ipv6_peer_enhe", + &routers, + ); + + let r1 = &test_routers[0]; + let r2 = &test_routers[1]; + + let r1_session = + r1.router.get_session(r2_addr.ip()).expect("get r1 session"); + let r2_session = + r2.router.get_session(r1_addr.ip()).expect("get r2 session"); + wait_for_eq!(r1_session.state(), FsmStateKind::Established); + wait_for_eq!(r2_session.state(), FsmStateKind::Established); + + // Originate IPv4 routes from R1 + let prefixes_v4 = generate_test_prefixes_v4(TEST_ROUTE_COUNT); + r1.router + .create_origin(AddressFamily::Ipv4, prefixes_v4.clone()) + .expect("originate IPv4"); + + // Wait for all routes to arrive at R2 + wait_for!( + prefixes_v4 + .iter() + .filter(|p| { !r2.router.db.get_prefix_paths(p).is_empty() }) + .count() + == TEST_ROUTE_COUNT, + "all IPv4 routes should arrive at R2 via ENHE" + ); + + // Verify the nexthop is R1's IPv6 address + for prefix in &prefixes_v4 { + let paths = r2.router.db.get_prefix_paths(prefix); + assert_eq!(paths.len(), 1, "{prefix:?}: expected 1 path"); + assert_eq!( + paths[0].nexthop, + r1_addr.ip(), + "{prefix:?}: nexthop should be R1's IPv6 address", + ); + } +} + #[test] fn test_ipv6_routes_ipv4_peer_success() { // IPv4 connection with IPv6-only routes @@ -2191,6 +2280,100 @@ fn test_ipv6_routes_ipv4_peer_success() { ) } +/// IPv6 routes over an IPv4-addressed numbered peer with no configured +/// nexthop. The sender uses a v4-mapped v6 address as the nexthop on the +/// wire. The receiver canonicalizes this to IPv4 before RIB installation. +#[test] +fn test_ipv6_routes_ipv4_peer_mapped_nexthop() { + let r1_addr: SocketAddr = + sockaddr!(&format!("10.0.0.9:{TEST_BGP_PORT}")); + let r2_addr: SocketAddr = + sockaddr!(&format!("10.0.0.10:{TEST_BGP_PORT}")); + + let routers = vec![ + LogicalRouter { + name: "r1".to_string(), + asn: Asn::FourOctet(4200000001), + id: 1, + listen_addr: r1_addr, + bind_addr: Some(r1_addr), + neighbors: vec![NeighborConfig { + peer_name: "r2".to_string(), + remote_host: r2_addr, + session_info: create_test_session_info( + RouteExchange::Ipv6 { nexthop: None }, + r1_addr, + r2_addr, + true, + ), + }], + }, + LogicalRouter { + name: "r2".to_string(), + asn: Asn::FourOctet(4200000002), + id: 2, + listen_addr: r2_addr, + bind_addr: Some(r2_addr), + neighbors: vec![NeighborConfig { + peer_name: "r1".to_string(), + remote_host: r1_addr, + session_info: create_test_session_info( + RouteExchange::Ipv6 { nexthop: None }, + r2_addr, + r1_addr, + false, + ), + }], + }, + ]; + + let (test_routers, _ip_guard) = + test_setup::( + "ipv6_routes_ipv4_peer_mapped_nh", + &routers, + ); + + let r1 = &test_routers[0]; + let r2 = &test_routers[1]; + + let r1_session = + r1.router.get_session(r2_addr.ip()).expect("get r1 session"); + let r2_session = + r2.router.get_session(r1_addr.ip()).expect("get r2 session"); + wait_for_eq!(r1_session.state(), FsmStateKind::Established); + wait_for_eq!(r2_session.state(), FsmStateKind::Established); + + // Originate IPv6 routes from R1 + let prefixes_v6 = generate_test_prefixes_v6(TEST_ROUTE_COUNT); + r1.router + .create_origin(AddressFamily::Ipv6, prefixes_v6.clone()) + .expect("originate IPv6"); + + // Wait for all routes to arrive at R2 + wait_for!( + prefixes_v6 + .iter() + .filter(|p| { + !r2.router.db.get_prefix_paths(p).is_empty() + }) + .count() + == TEST_ROUTE_COUNT, + "all IPv6 routes should arrive at R2" + ); + + // Verify the nexthop is canonicalized to R1's IPv4 address + for prefix in &prefixes_v6 { + let paths = r2.router.db.get_prefix_paths(prefix); + assert_eq!(paths.len(), 1, "{prefix:?}: expected 1 path"); + assert_eq!( + paths[0].nexthop, + r1_addr.ip(), + "{prefix:?}: nexthop should be R1's IPv4 address \ + (canonicalized from v4-mapped v6)", + ); + } +} + /// Helper to set up two routers with unnumbered BGP sessions. /// /// This creates a realistic unnumbered BGP setup where: diff --git a/mgadm/src/bgp.rs b/mgadm/src/bgp.rs index 953be600d..8bf91981e 100644 --- a/mgadm/src/bgp.rs +++ b/mgadm/src/bgp.rs @@ -1247,6 +1247,20 @@ fn display_neighbors_detail( format_duration_human(info.timers.delay_open.remaining), ); + println_nopipe!("\n{}", "Capabilities:".bold()); + println_nopipe!(" IPv4 Unicast: {}", info.cap_state.ipv4); + println_nopipe!(" IPv6 Unicast: {}", info.cap_state.ipv6); + println_nopipe!(" Extended Messages: {}", info.cap_state.ext_msg); + println_nopipe!(" Route Refresh: {}", info.cap_state.route_refresh); + println_nopipe!(" Extended Next Hop: {}", info.cap_state.enhe); + + if !info.sent_capabilities.is_empty() { + println_nopipe!("\n{}", "Sent Capabilities:".bold()); + for cap in &info.sent_capabilities { + println_nopipe!(" {:?}", cap); + } + } + if !info.received_capabilities.is_empty() { println_nopipe!("\n{}", "Received Capabilities:".bold()); for cap in &info.received_capabilities { diff --git a/openapi/mg-admin/mg-admin-7.0.0-af3143.json b/openapi/mg-admin/mg-admin-7.0.0-36296f.json similarity index 98% rename from openapi/mg-admin/mg-admin-7.0.0-af3143.json rename to openapi/mg-admin/mg-admin-7.0.0-36296f.json index 30746b7f0..0713b2d14 100644 --- a/openapi/mg-admin/mg-admin-7.0.0-af3143.json +++ b/openapi/mg-admin/mg-admin-7.0.0-36296f.json @@ -2042,7 +2042,9 @@ { "type": "string", "enum": [ - "RouteRefresh" + "RouteRefresh", + "ExtendedNextHopEncoding", + "BGPExtendedMessage" ] }, { @@ -2800,6 +2802,39 @@ } ] }, + "CapabilityState": { + "description": "The negotiation state of a capability for a given peer.", + "oneOf": [ + { + "description": "We have not configured or advertised this capability, and the peer did not send it.", + "type": "string", + "enum": [ + "Unconfigured" + ] + }, + { + "description": "We advertised this capability but the peer did not send it.", + "type": "string", + "enum": [ + "Advertised" + ] + }, + { + "description": "The peer sent this capability but we did not advertise it.", + "type": "string", + "enum": [ + "Received" + ] + }, + { + "description": "Both sides advertised this capability.", + "type": "string", + "enum": [ + "Negotiated" + ] + } + ] + }, "CeaseErrorSubcode": { "description": "Cease error subcode types from RFC 4486", "type": "string", @@ -3987,6 +4022,59 @@ "tx_running" ] }, + "NegotiatedCaps": { + "description": "The result of comparing our OPEN capabilities with the peer's.", + "type": "object", + "properties": { + "enhe": { + "description": "Extended nexthop: IPv4 routes carried over IPv6 nexthops (RFC 8950)", + "allOf": [ + { + "$ref": "#/components/schemas/CapabilityState" + } + ] + }, + "ext_msg": { + "description": "BGP Extended Message Size (RFC 8654)", + "allOf": [ + { + "$ref": "#/components/schemas/CapabilityState" + } + ] + }, + "ipv4": { + "description": "IPv4 Unicast address-family", + "allOf": [ + { + "$ref": "#/components/schemas/CapabilityState" + } + ] + }, + "ipv6": { + "description": "IPv6 Unicast address-family", + "allOf": [ + { + "$ref": "#/components/schemas/CapabilityState" + } + ] + }, + "route_refresh": { + "description": "Route Refresh (RFC 2918)", + "allOf": [ + { + "$ref": "#/components/schemas/CapabilityState" + } + ] + } + }, + "required": [ + "enhe", + "ext_msg", + "ipv4", + "ipv6", + "route_refresh" + ] + }, "Neighbor": { "description": "Neighbor configuration with explicit per-address-family enablement (v3 API)", "type": "object", @@ -4987,6 +5075,9 @@ "format": "uint32", "minimum": 0 }, + "cap_state": { + "$ref": "#/components/schemas/NegotiatedCaps" + }, "counters": { "$ref": "#/components/schemas/PeerCounters" }, @@ -5062,11 +5153,18 @@ "format": "uint16", "minimum": 0 }, + "sent_capabilities": { + "type": "array", + "items": { + "$ref": "#/components/schemas/BgpCapability" + } + }, "timers": { "$ref": "#/components/schemas/PeerTimers" } }, "required": [ + "cap_state", "counters", "fsm_state", "fsm_state_duration", @@ -5079,6 +5177,7 @@ "received_capabilities", "remote_ip", "remote_tcp_port", + "sent_capabilities", "timers" ] }, diff --git a/openapi/mg-admin/mg-admin-latest.json b/openapi/mg-admin/mg-admin-latest.json index f96ff24bc..5b9d7f280 120000 --- a/openapi/mg-admin/mg-admin-latest.json +++ b/openapi/mg-admin/mg-admin-latest.json @@ -1 +1 @@ -mg-admin-7.0.0-af3143.json \ No newline at end of file +mg-admin-7.0.0-36296f.json \ No newline at end of file From 268d6c32cb9b2e49c39683f39c99b04155e77bf3 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Thu, 12 Mar 2026 01:43:46 -0600 Subject: [PATCH 40/58] bgp: add negotiate_capabilities and select_nexthop tests Add unit tests for negotiate_capabilities() covering AF negotiation, legacy implicit IPv4, extended messages, route refresh, and ENHE element matching. Add select_nexthop tests for configured cross-AF nexthop with and without ENHE. Add ExtendedNexthopElement::v4_over_v6() constructor; gate v6_over_v4 helpers behind cfg(test). --- bgp/src/messages.rs | 34 ++++--- bgp/src/session.rs | 243 +++++++++++++++++++++++++++++++++++++++++++- bgp/src/test.rs | 10 +- 3 files changed, 261 insertions(+), 26 deletions(-) diff --git a/bgp/src/messages.rs b/bgp/src/messages.rs index 77a8ee6f0..39cd752b3 100644 --- a/bgp/src/messages.rs +++ b/bgp/src/messages.rs @@ -589,11 +589,7 @@ impl OpenMessage { ) -> OpenMessage { let parameters = if extended_nexthop { let caps = BTreeSet::from([Capability::ExtendedNextHopEncoding { - elements: vec![ExtendedNexthopElement { - afi: Afi::Ipv4.into(), - safi: u8::from(Safi::Unicast).into(), - nh_afi: Afi::Ipv6.into(), - }], + elements: vec![ExtendedNexthopElement::v4_over_v6()], }]); vec![OptionalParameter::Capabilities(caps)] } else { @@ -618,11 +614,7 @@ impl OpenMessage { let mut params = BTreeSet::from([Capability::FourOctetAs { asn }]); if extended_nexthop { params.insert(Capability::ExtendedNextHopEncoding { - elements: vec![ExtendedNexthopElement { - afi: Afi::Ipv4.into(), - safi: u8::from(Safi::Unicast).into(), - nh_afi: Afi::Ipv6.into(), - }], + elements: vec![ExtendedNexthopElement::v4_over_v6()], }); } OpenMessage { @@ -4352,20 +4344,33 @@ pub struct ExtendedNexthopElement { } impl ExtendedNexthopElement { - pub fn is_v4_over_v6(&self) -> bool { - self == &ExtendedNexthopElement { + /// IPv4 Unicast NLRI with IPv6 nexthops (RFC 8950). + pub fn v4_over_v6() -> Self { + Self { afi: Afi::Ipv4.into(), safi: u8::from(Safi::Unicast).into(), nh_afi: Afi::Ipv6.into(), } } - pub fn is_v6_over_v4(&self) -> bool { - self == &ExtendedNexthopElement { + + /// IPv6 Unicast NLRI with IPv4 nexthops. + #[cfg(test)] + pub fn v6_over_v4() -> Self { + Self { afi: Afi::Ipv6.into(), safi: u8::from(Safi::Unicast).into(), nh_afi: Afi::Ipv4.into(), } } + + pub fn is_v4_over_v6(&self) -> bool { + self == &Self::v4_over_v6() + } + + #[cfg(test)] + pub fn is_v6_over_v4(&self) -> bool { + self == &Self::v6_over_v4() + } } impl Display for ExtendedNexthopElement { @@ -5015,6 +5020,7 @@ impl Capability { } } + #[cfg(test)] pub fn extended_nh_v6_over_v4(&self) -> bool { if let Self::ExtendedNextHopEncoding { elements } = self { elements.iter().any(|x| x.is_v6_over_v4()) diff --git a/bgp/src/session.rs b/bgp/src/session.rs index 5015f2d6f..02daad92c 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -2514,11 +2514,7 @@ impl SessionRunner { } if self.use_extended_nexthop() { caps.insert(Capability::ExtendedNextHopEncoding { - elements: vec![ExtendedNexthopElement { - afi: Afi::Ipv4.into(), - safi: u8::from(Safi::Unicast).into(), - nh_afi: Afi::Ipv6.into(), - }], + elements: vec![ExtendedNexthopElement::v4_over_v6()], }); } *lock!(self.caps_tx) = caps; @@ -10442,6 +10438,243 @@ mod tests { ); } + // ================================================================ + // select_nexthop: configured nexthop + ENHE + // ================================================================ + + #[test] + fn test_select_nexthop_configured_v6_nexthop_ipv4_route_enhe() { + // Configured IPv6 nexthop for IPv4 routes with ENHE negotiated + let configured_nh = ip!("2001:db8::1"); + let local_ip = ip!("2001:db8::2"); + let caps = NegotiatedCaps { + enhe: CapabilityState::Negotiated, + ..Default::default() + }; + + let result = + select_nexthop(Afi::Ipv4, local_ip, Some(configured_nh), &caps); + assert_eq!( + result.unwrap(), + BgpNexthop::Ipv6Single("2001:db8::1".parse().unwrap()), + ); + } + + #[test] + fn test_select_nexthop_configured_v6_nexthop_ipv4_route_no_enhe() { + // Configured IPv6 nexthop for IPv4 routes without ENHE = error + let configured_nh = ip!("2001:db8::1"); + let local_ip = ip!("2001:db8::2"); + + let result = select_nexthop( + Afi::Ipv4, + local_ip, + Some(configured_nh), + &NegotiatedCaps::default(), + ); + assert!(result.is_err()); + } + + // ================================================================ + // negotiate_capabilities unit tests + // ================================================================ + + use crate::messages::ExtendedNexthopElement; + + fn enhe_v4_over_v6() -> Capability { + Capability::ExtendedNextHopEncoding { + elements: vec![ExtendedNexthopElement::v4_over_v6()], + } + } + + fn enhe_v6_over_v4() -> Capability { + Capability::ExtendedNextHopEncoding { + elements: vec![ExtendedNexthopElement::v6_over_v4()], + } + } + + #[test] + fn negotiate_both_ipv4_ipv6() { + let ours = BTreeSet::from([ + Capability::ipv4_unicast(), + Capability::ipv6_unicast(), + ]); + let theirs = BTreeSet::from([ + Capability::ipv4_unicast(), + Capability::ipv6_unicast(), + ]); + let caps = negotiate_capabilities(&ours, &theirs); + assert_eq!(caps.ipv4, CapabilityState::Negotiated); + assert_eq!(caps.ipv6, CapabilityState::Negotiated); + } + + #[test] + fn negotiate_ipv4_only_we_advertise_ipv6() { + let ours = BTreeSet::from([ + Capability::ipv4_unicast(), + Capability::ipv6_unicast(), + ]); + let theirs = BTreeSet::from([Capability::ipv4_unicast()]); + let caps = negotiate_capabilities(&ours, &theirs); + assert_eq!(caps.ipv4, CapabilityState::Negotiated); + assert_eq!(caps.ipv6, CapabilityState::Advertised); + } + + #[test] + fn negotiate_ipv6_received_not_advertised() { + let ours = BTreeSet::from([Capability::ipv4_unicast()]); + let theirs = BTreeSet::from([ + Capability::ipv4_unicast(), + Capability::ipv6_unicast(), + ]); + let caps = negotiate_capabilities(&ours, &theirs); + assert_eq!(caps.ipv4, CapabilityState::Negotiated); + assert_eq!(caps.ipv6, CapabilityState::Received); + } + + #[test] + fn negotiate_legacy_peer_implicit_ipv4() { + // A peer with no MP-BGP caps implicitly negotiates IPv4 + // Unicast per RFC 4271. + let ours = BTreeSet::from([Capability::ipv4_unicast()]); + let theirs = BTreeSet::from([Capability::FourOctetAs { asn: 65000 }]); + let caps = negotiate_capabilities(&ours, &theirs); + assert_eq!(caps.ipv4, CapabilityState::Negotiated); + assert_eq!(caps.ipv6, CapabilityState::Unconfigured); + } + + #[test] + fn negotiate_legacy_peer_we_dont_advertise_ipv4() { + // Legacy peer but we didn't advertise IPv4 either + let ours = BTreeSet::from([Capability::ipv6_unicast()]); + let theirs = BTreeSet::from([Capability::FourOctetAs { asn: 65000 }]); + let caps = negotiate_capabilities(&ours, &theirs); + assert_eq!(caps.ipv4, CapabilityState::Unconfigured); + } + + #[test] + fn negotiate_ext_msg_both() { + let ours = BTreeSet::from([Capability::BGPExtendedMessage {}]); + let theirs = BTreeSet::from([Capability::BGPExtendedMessage {}]); + let caps = negotiate_capabilities(&ours, &theirs); + assert_eq!(caps.ext_msg, CapabilityState::Negotiated); + } + + #[test] + fn negotiate_ext_msg_one_sided() { + let ours = BTreeSet::from([Capability::BGPExtendedMessage {}]); + let theirs = BTreeSet::new(); + let caps = negotiate_capabilities(&ours, &theirs); + assert_eq!(caps.ext_msg, CapabilityState::Advertised); + + let caps = negotiate_capabilities(&theirs, &ours); + assert_eq!(caps.ext_msg, CapabilityState::Received); + } + + #[test] + fn negotiate_route_refresh() { + let ours = BTreeSet::from([Capability::RouteRefresh {}]); + let theirs = BTreeSet::from([Capability::RouteRefresh {}]); + let caps = negotiate_capabilities(&ours, &theirs); + assert_eq!(caps.route_refresh, CapabilityState::Negotiated); + + let caps = negotiate_capabilities(&ours, &BTreeSet::new()); + assert_eq!(caps.route_refresh, CapabilityState::Advertised); + } + + #[test] + fn negotiate_enhe_v4_over_v6_both() { + let ours = BTreeSet::from([enhe_v4_over_v6()]); + let theirs = BTreeSet::from([enhe_v4_over_v6()]); + let caps = negotiate_capabilities(&ours, &theirs); + assert_eq!(caps.enhe, CapabilityState::Negotiated); + } + + #[test] + fn negotiate_enhe_v4_over_v6_one_sided() { + let ours = BTreeSet::from([enhe_v4_over_v6()]); + let theirs = BTreeSet::new(); + let caps = negotiate_capabilities(&ours, &theirs); + assert_eq!(caps.enhe, CapabilityState::Advertised); + + let caps = negotiate_capabilities(&theirs, &ours); + assert_eq!(caps.enhe, CapabilityState::Received); + } + + #[test] + fn negotiate_enhe_mismatched_elements() { + // We send v4-over-v6, they send v6-over-v4. The v4-over-v6 + // predicate doesn't match on their side, so ENHE should not + // be negotiated. + let ours = BTreeSet::from([enhe_v4_over_v6()]); + let theirs = BTreeSet::from([enhe_v6_over_v4()]); + let caps = negotiate_capabilities(&ours, &theirs); + assert_eq!(caps.enhe, CapabilityState::Advertised); + } + + #[test] + fn negotiate_enhe_neither() { + let caps = negotiate_capabilities(&BTreeSet::new(), &BTreeSet::new()); + assert_eq!(caps.enhe, CapabilityState::Unconfigured); + } + + #[test] + fn negotiate_empty_caps() { + let caps = negotiate_capabilities(&BTreeSet::new(), &BTreeSet::new()); + assert_eq!(caps.ipv4, CapabilityState::Unconfigured); + assert_eq!(caps.ipv6, CapabilityState::Unconfigured); + assert_eq!(caps.ext_msg, CapabilityState::Unconfigured); + assert_eq!(caps.route_refresh, CapabilityState::Unconfigured); + assert_eq!(caps.enhe, CapabilityState::Unconfigured); + } + + #[test] + fn negotiate_full_realistic_session() { + // Simulate a realistic IPv6 peer exchanging IPv4+IPv6 with ENHE + let ours = BTreeSet::from([ + Capability::ipv4_unicast(), + Capability::ipv6_unicast(), + Capability::BGPExtendedMessage {}, + Capability::RouteRefresh {}, + enhe_v4_over_v6(), + ]); + let theirs = BTreeSet::from([ + Capability::ipv4_unicast(), + Capability::ipv6_unicast(), + Capability::BGPExtendedMessage {}, + Capability::RouteRefresh {}, + enhe_v4_over_v6(), + ]); + let caps = negotiate_capabilities(&ours, &theirs); + assert_eq!(caps.ipv4, CapabilityState::Negotiated); + assert_eq!(caps.ipv6, CapabilityState::Negotiated); + assert_eq!(caps.ext_msg, CapabilityState::Negotiated); + assert_eq!(caps.route_refresh, CapabilityState::Negotiated); + assert_eq!(caps.enhe, CapabilityState::Negotiated); + } + + #[test] + fn negotiate_peer_subset_of_ours() { + // We advertise everything, peer only supports IPv4 + route refresh + let ours = BTreeSet::from([ + Capability::ipv4_unicast(), + Capability::ipv6_unicast(), + Capability::BGPExtendedMessage {}, + Capability::RouteRefresh {}, + enhe_v4_over_v6(), + ]); + let theirs = BTreeSet::from([ + Capability::ipv4_unicast(), + Capability::RouteRefresh {}, + ]); + let caps = negotiate_capabilities(&ours, &theirs); + assert_eq!(caps.ipv4, CapabilityState::Negotiated); + assert_eq!(caps.ipv6, CapabilityState::Advertised); + assert_eq!(caps.ext_msg, CapabilityState::Advertised); + assert_eq!(caps.route_refresh, CapabilityState::Negotiated); + assert_eq!(caps.enhe, CapabilityState::Advertised); + } + // ================================================================ // check_for_self_in_path / enforce_first_as unit tests // ================================================================ diff --git a/bgp/src/test.rs b/bgp/src/test.rs index c5973d676..cb13c1702 100644 --- a/bgp/src/test.rs +++ b/bgp/src/test.rs @@ -2285,10 +2285,8 @@ fn test_ipv6_routes_ipv4_peer_success() { /// wire. The receiver canonicalizes this to IPv4 before RIB installation. #[test] fn test_ipv6_routes_ipv4_peer_mapped_nexthop() { - let r1_addr: SocketAddr = - sockaddr!(&format!("10.0.0.9:{TEST_BGP_PORT}")); - let r2_addr: SocketAddr = - sockaddr!(&format!("10.0.0.10:{TEST_BGP_PORT}")); + let r1_addr: SocketAddr = sockaddr!(&format!("10.0.0.9:{TEST_BGP_PORT}")); + let r2_addr: SocketAddr = sockaddr!(&format!("10.0.0.10:{TEST_BGP_PORT}")); let routers = vec![ LogicalRouter { @@ -2353,9 +2351,7 @@ fn test_ipv6_routes_ipv4_peer_mapped_nexthop() { wait_for!( prefixes_v6 .iter() - .filter(|p| { - !r2.router.db.get_prefix_paths(p).is_empty() - }) + .filter(|p| { !r2.router.db.get_prefix_paths(p).is_empty() }) .count() == TEST_ROUTE_COUNT, "all IPv6 routes should arrive at R2" From c3b20e58539318445e0d2bda094241f19e3bf7d4 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Thu, 12 Mar 2026 09:16:37 -0600 Subject: [PATCH 41/58] bgp: reset session when ENHE is toggled Adds logic into update_session_info() to reset the BGP session if a nexthop configuration change results in ENHE capability being toggled. i.e. If the NH-AFI changes for IPv4 Unicast, ENHE must be re-negotiated which requires a full session reset. --- bgp/src/session.rs | 189 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 174 insertions(+), 15 deletions(-) diff --git a/bgp/src/session.rs b/bgp/src/session.rs index 02daad92c..dcd2d9f6b 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -343,6 +343,23 @@ pub fn v4_over_v6_nexthop( } } +/// Returns true if the effective nexthop-AFI for IPv4 unicast is +/// IPv6, which determines whether ENHE is needed. +/// +/// The effective NH-AFI depends on the nexthop override and peer: +/// - Explicit v6 override → v6 regardless of peer +/// - Explicit v4 override → v4 regardless of peer +/// - No override → derived from peer address AFI +fn ipv4_nexthop_afi_is_v6(peer: &PeerId, config: &Ipv4UnicastConfig) -> bool { + match config.nexthop { + Some(IpAddr::V6(_)) => true, + Some(IpAddr::V4(_)) => false, + None => { + matches!(peer, PeerId::Ip(IpAddr::V6(_)) | PeerId::Interface(_)) + } + } +} + /// The states a BGP finite state machine may be at any given time. Many /// of these states carry a connection by value. This is the same connection /// that moves from state to state as transitions are made. Transitions from @@ -7979,19 +7996,13 @@ impl SessionRunner { } fn use_extended_nexthop(&self) -> bool { - // ENHE is always used for unnumbered peers. - if self.is_unnumbered() { - return true; - } - // Numbered peers use ENHE if: - // 1. We peer with them over IPv6 - // 2. IPv4 Unicast is enabled - // 3. The nexthop for IPv4 Unicast is not IPv4 (v4 over v4 is not ENHE) - matches!(self.neighbor.peer, PeerId::Ip(IpAddr::V6(_))) - && lock!(self.session) - .ipv4_unicast - .as_ref() - .is_some_and(|c| !matches!(c.nexthop, Some(IpAddr::V4(_)))) + // ENHE is needed when the effective NH-AFI for IPv4 + // unicast is IPv6. If IPv4 unicast isn't enabled, there's + // nothing to carry over ENHE. + lock!(self.session) + .ipv4_unicast + .as_ref() + .is_some_and(|c| ipv4_nexthop_afi_is_v6(&self.neighbor.peer, c)) } fn is_ebgp(&self) -> Option { @@ -9587,9 +9598,22 @@ impl SessionRunner { refresh_needed4 = true; } - // Nexthop override changed - trigger re-advertisement + // Nexthop override changed if current_v4.map(|c| c.nexthop) != info_v4.map(|c| c.nexthop) { - readvertise_needed4 = true; + let peer = &self.neighbor.peer; + match (current_v4, info_v4) { + (Some(old), Some(new)) + if ipv4_nexthop_afi_is_v6(peer, old) + != ipv4_nexthop_afi_is_v6(peer, new) => + { + // NH-AFI changed — ENHE capability must be + // added or removed, requiring a session reset. + reset_needed = true; + } + _ => { + readvertise_needed4 = true; + } + } } // Export policy changed - send FSM notification @@ -10475,6 +10499,141 @@ mod tests { assert!(result.is_err()); } + // ================================================================ + // ipv4_nexthop_afi_is_v6 unit tests + // ================================================================ + + use crate::params::Ipv4UnicastConfig; + + fn v4_config_with_nexthop(nexthop: Option) -> Ipv4UnicastConfig { + Ipv4UnicastConfig { + nexthop, + import_policy: Default::default(), + export_policy: Default::default(), + } + } + + #[test] + fn nh_afi_v4_peer_no_override() { + let peer = PeerId::Ip(ip!("10.0.0.1")); + let config = v4_config_with_nexthop(None); + assert!(!ipv4_nexthop_afi_is_v6(&peer, &config)); + } + + #[test] + fn nh_afi_v6_peer_no_override() { + let peer = PeerId::Ip(ip!("2001:db8::1")); + let config = v4_config_with_nexthop(None); + assert!(ipv4_nexthop_afi_is_v6(&peer, &config)); + } + + #[test] + fn nh_afi_unnumbered_no_override() { + let peer = PeerId::Interface("eth0".into()); + let config = v4_config_with_nexthop(None); + assert!(ipv4_nexthop_afi_is_v6(&peer, &config)); + } + + #[test] + fn nh_afi_explicit_v4_override_any_peer() { + let config = v4_config_with_nexthop(Some(ip!("10.0.0.1"))); + // v4 override → NH-AFI v4 regardless of peer + for peer in [ + PeerId::Ip(ip!("10.0.0.2")), + PeerId::Ip(ip!("2001:db8::1")), + PeerId::Interface("eth0".into()), + ] { + assert!( + !ipv4_nexthop_afi_is_v6(&peer, &config), + "v4 override should be NH-AFI v4 for {peer:?}" + ); + } + } + + #[test] + fn nh_afi_explicit_v6_override_any_peer() { + let config = v4_config_with_nexthop(Some(ip!("2001:db8::1"))); + // v6 override → NH-AFI v6 regardless of peer + for peer in [ + PeerId::Ip(ip!("10.0.0.1")), + PeerId::Ip(ip!("2001:db8::2")), + PeerId::Interface("eth0".into()), + ] { + assert!( + ipv4_nexthop_afi_is_v6(&peer, &config), + "v6 override should be NH-AFI v6 for {peer:?}" + ); + } + } + + /// Verify NH-AFI transitions that flip ENHE state (require + /// session reset) vs those that don't (readvertise only). + #[test] + fn nh_afi_transitions_v6_peer() { + let peer = PeerId::Ip(ip!("2001:db8::1")); + let no_override = v4_config_with_nexthop(None); + let v4_override = v4_config_with_nexthop(Some(ip!("10.0.0.1"))); + let v6_override = v4_config_with_nexthop(Some(ip!("2001:db8::99"))); + + // Transitions that change NH-AFI (reset needed) + // None → V4: v6 → v4 + assert_ne!( + ipv4_nexthop_afi_is_v6(&peer, &no_override), + ipv4_nexthop_afi_is_v6(&peer, &v4_override), + ); + // V6 → V4: v6 → v4 + assert_ne!( + ipv4_nexthop_afi_is_v6(&peer, &v6_override), + ipv4_nexthop_afi_is_v6(&peer, &v4_override), + ); + + // Transitions that keep NH-AFI (readvertise only) + // None → V6: both v6 + assert_eq!( + ipv4_nexthop_afi_is_v6(&peer, &no_override), + ipv4_nexthop_afi_is_v6(&peer, &v6_override), + ); + // V4(a) → V4(b): both v4 + let v4_override_2 = v4_config_with_nexthop(Some(ip!("10.0.0.2"))); + assert_eq!( + ipv4_nexthop_afi_is_v6(&peer, &v4_override), + ipv4_nexthop_afi_is_v6(&peer, &v4_override_2), + ); + } + + #[test] + fn nh_afi_transitions_v4_peer() { + let peer = PeerId::Ip(ip!("10.0.0.1")); + let no_override = v4_config_with_nexthop(None); + let v4_override = v4_config_with_nexthop(Some(ip!("10.0.0.2"))); + let v6_override = v4_config_with_nexthop(Some(ip!("2001:db8::1"))); + + // Transitions that change NH-AFI (reset needed) + // None → V6: v4 → v6 + assert_ne!( + ipv4_nexthop_afi_is_v6(&peer, &no_override), + ipv4_nexthop_afi_is_v6(&peer, &v6_override), + ); + // V4 → V6: v4 → v6 + assert_ne!( + ipv4_nexthop_afi_is_v6(&peer, &v4_override), + ipv4_nexthop_afi_is_v6(&peer, &v6_override), + ); + + // Transitions that keep NH-AFI (readvertise only) + // None → V4: both v4 + assert_eq!( + ipv4_nexthop_afi_is_v6(&peer, &no_override), + ipv4_nexthop_afi_is_v6(&peer, &v4_override), + ); + // V4(a) → V4(b): both v4 + let v4_override_2 = v4_config_with_nexthop(Some(ip!("10.0.0.3"))); + assert_eq!( + ipv4_nexthop_afi_is_v6(&peer, &v4_override), + ipv4_nexthop_afi_is_v6(&peer, &v4_override_2), + ); + } + // ================================================================ // negotiate_capabilities unit tests // ================================================================ From 14d1a6b8e68635ef73d4c03d1cd186ee1449401c Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Thu, 12 Mar 2026 10:19:00 -0600 Subject: [PATCH 42/58] bgp: increase TEST_ROUTE_COUNT to 32k Bump TEST_ROUTE_COUNT to 32k to ensure we always originate enough routes to trigger UpdateMessage chunking, even when BGP Extended Message Size has been negotiated. --- bgp/src/test.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bgp/src/test.rs b/bgp/src/test.rs index cb13c1702..654449259 100644 --- a/bgp/src/test.rs +++ b/bgp/src/test.rs @@ -66,10 +66,11 @@ const ESTABLISHED_VERIFICATION: Duration = Duration::from_secs(TEST_HOLD_TIME_SECS + 2); /// Number of routes to originate in update tests. Sized to require multiple -/// BGP UPDATE messages (each capped at MAX_MESSAGE_SIZE = 4096 bytes), -/// exercising the chunk_and_send splitting and the incremental recv_msg -/// body-read loop. -const TEST_ROUTE_COUNT: usize = 4096; +/// BGP UPDATE messages even when peers negotiate Extended Message Size +/// (RFC 8654, max 65535 bytes). An IPv4 /24 prefix is 4 bytes on the wire, +/// so ~16371 fit in a single extended UPDATE; 2^15 guarantees at least two +/// chunks for both IPv4 and IPv6. +const TEST_ROUTE_COUNT: usize = 32_768; /// Generate `count` unique IPv4 /24 prefixes: 10.0.0.0/24 .. 10.x.y.0/24 fn generate_test_prefixes_v4(count: usize) -> Vec { From 547e31171e5650096130e914fd5113db0bd87b80 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 4 Mar 2026 15:13:43 -0700 Subject: [PATCH 43/58] Use ManagedThread in bfd Use mg-common's ManagedThread so we retain JoinHandles in a structured fashion in case we want to make explicit use of these later. Also introduce AddPeerRequest to group arguments defining a peer config. This avoids clippy's too many arguments warning and provides some rough structure for the peer add flow. --- bfd/src/lib.rs | 156 +++++++++++++++++++++++-------------------- mgd/src/bfd_admin.rs | 33 ++++++--- 2 files changed, 108 insertions(+), 81 deletions(-) diff --git a/bfd/src/lib.rs b/bfd/src/lib.rs index fd195db50..52be96cb9 100644 --- a/bfd/src/lib.rs +++ b/bfd/src/lib.rs @@ -2,17 +2,19 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. +use mg_common::thread::ManagedThread; use num_enum::TryFromPrimitive; use rdb::SessionMode; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use slog::{Logger, warn}; use sm::StateMachine; -use std::collections::HashMap; -use std::net::IpAddr; -use std::sync::Arc; -use std::sync::atomic::AtomicU64; -use std::time::Duration; +use std::{ + collections::HashMap, + net::IpAddr, + sync::{Arc, atomic::AtomicU64}, + time::Duration, +}; pub mod bidi; pub mod log; @@ -51,15 +53,7 @@ impl Daemon { /// Add a peer session to the deamon. Peer sessions are started immediately /// when added. - pub fn add_peer( - &mut self, - peer: IpAddr, - required_rx: Duration, - detection_multiplier: u8, - mode: SessionMode, - endpoint: BfdEndpoint, - db: rdb::Db, - ) { + pub fn add_peer(&mut self, peer: IpAddr, rq: AddPeerRequest) { if self.sessions.contains_key(&peer) { warn!(self.log, "attempt to add peer that already exists"; "component" => COMPONENT_BFD, @@ -69,18 +63,8 @@ impl Daemon { ); return; } - self.sessions.insert( - peer, - Session::new( - peer, - endpoint, - required_rx, - detection_multiplier, - mode, - db, - self.log.clone(), - ), - ); + self.sessions + .insert(peer, Session::new(peer, rq, self.log.clone())); } /// Remove a peer from the daemon. The peer will be immediately shut down. @@ -112,35 +96,47 @@ pub struct SessionCounters { pub unexpected_message: AtomicU64, } +/// Parameters for adding a new BFD peer session. +pub struct AddPeerRequest { + pub required_rx: Duration, + pub detection_multiplier: u8, + pub mode: SessionMode, + pub endpoint: BfdEndpoint, + pub egress_thread: Option>, + pub db: rdb::Db, +} + /// A session holds a BFD state machine for a particular peer. pub struct Session { pub sm: StateMachine, pub mode: SessionMode, pub counters: Arc, + /// Managed egress thread for UDP packet transmission. Stored here to + /// ensure automatic cleanup on drop — the ManagedThread's Drop impl + /// will signal shutdown and join the thread. None in test contexts + /// where egress is handled differently. + _egress_thread: Option>, } impl Session { /// Create a new session and start running the underlying BFD state machine /// immediately. - fn new( - addr: IpAddr, - ep: BfdEndpoint, - required_rx: Duration, - detection_multiplier: u8, - mode: SessionMode, - db: rdb::Db, - log: Logger, - ) -> Self { + fn new(addr: IpAddr, rq: AddPeerRequest, log: Logger) -> Self { let counters = Arc::new(SessionCounters::default()); let mut sm = StateMachine::new( addr, - required_rx, - detection_multiplier, + rq.required_rx, + rq.detection_multiplier, counters.clone(), log, ); - sm.run(ep, db); - Session { sm, mode, counters } + sm.run(rq.endpoint, rq.db); + Session { + sm, + mode: rq.mode, + counters, + _egress_thread: rq.egress_thread, + } } } @@ -329,26 +325,32 @@ mod test { let v4_addr = ip("203.0.113.10"); daemon.add_peer( v4_addr, - Duration::from_secs(5), - 3, - SessionMode::MultiHop, - a, - db.db().clone(), + AddPeerRequest { + required_rx: Duration::from_secs(5), + detection_multiplier: 3, + mode: SessionMode::MultiHop, + endpoint: a, + egress_thread: None, + db: db.db().clone(), + }, ); - assert_eq!(daemon.peer_state(v4_addr), Some(BfdPeerState::Down),); + assert_eq!(daemon.peer_state(v4_addr), Some(BfdPeerState::Down)); // Add an IPv6 peer to the same daemon. let (a, _b) = bidi::channel(); let v6_addr = ip("2001:db8::10"); daemon.add_peer( v6_addr, - Duration::from_secs(5), - 3, - SessionMode::MultiHop, - a, - db.db().clone(), + AddPeerRequest { + required_rx: Duration::from_secs(5), + detection_multiplier: 3, + mode: SessionMode::MultiHop, + endpoint: a, + egress_thread: None, + db: db.db().clone(), + }, ); - assert_eq!(daemon.peer_state(v6_addr), Some(BfdPeerState::Down),); + assert_eq!(daemon.peer_state(v6_addr), Some(BfdPeerState::Down)); // Both peers coexist. assert_eq!(daemon.sessions.len(), 2); @@ -377,22 +379,28 @@ mod test { let (a, b) = bidi::channel(); d1.add_peer( v4_addr1, - Duration::from_secs(5), - 3, - SessionMode::MultiHop, - a, - db.db().clone(), + AddPeerRequest { + required_rx: Duration::from_secs(5), + detection_multiplier: 3, + mode: SessionMode::MultiHop, + endpoint: a, + egress_thread: None, + db: db.db().clone(), + }, ); net.register(v4_addr2, b); let (a, b) = bidi::channel(); d1.add_peer( v6_addr1, - Duration::from_secs(5), - 3, - SessionMode::MultiHop, - a, - db.db().clone(), + AddPeerRequest { + required_rx: Duration::from_secs(5), + detection_multiplier: 3, + mode: SessionMode::MultiHop, + endpoint: a, + egress_thread: None, + db: db.db().clone(), + }, ); net.register(v6_addr2, b); @@ -401,22 +409,28 @@ mod test { let (a, b) = bidi::channel(); d2.add_peer( v4_addr2, - Duration::from_secs(5), - 3, - SessionMode::MultiHop, - a, - db.db().clone(), + AddPeerRequest { + required_rx: Duration::from_secs(5), + detection_multiplier: 3, + mode: SessionMode::MultiHop, + endpoint: a, + egress_thread: None, + db: db.db().clone(), + }, ); net.register(v4_addr1, b); let (a, b) = bidi::channel(); d2.add_peer( v6_addr2, - Duration::from_secs(5), - 3, - SessionMode::MultiHop, - a, - db.db().clone(), + AddPeerRequest { + required_rx: Duration::from_secs(5), + detection_multiplier: 3, + mode: SessionMode::MultiHop, + endpoint: a, + egress_thread: None, + db: db.db().clone(), + }, ); net.register(v6_addr1, b); diff --git a/mgd/src/bfd_admin.rs b/mgd/src/bfd_admin.rs index 6ac02ed4d..589550292 100644 --- a/mgd/src/bfd_admin.rs +++ b/mgd/src/bfd_admin.rs @@ -4,13 +4,14 @@ use crate::{admin::HandlerContext, log::bfd_log}; use anyhow::Result; -use bfd::{BfdEndpoint, DEFAULT_BFD_TTL, Daemon, bidi, packet}; +use bfd::{AddPeerRequest, BfdEndpoint, DEFAULT_BFD_TTL, Daemon, bidi, packet}; use dropshot::{ HttpError, HttpResponseOk, HttpResponseUpdatedNoContent, Path, RequestContext, TypedBody, }; use mg_api::{BfdPeerInfo, DeleteBfdPeerPathParams}; use mg_common::lock; +use mg_common::thread::ManagedThread; use rdb::{BfdPeerConfig, SessionMode}; use slog::Logger; use socket2::Socket; @@ -118,7 +119,7 @@ pub(crate) fn add_peer( let log = ctx.log.clone(); - let ch = channel( + let (ch, egress_thread) = channel( dispatcher, rq.listen, rq.peer, @@ -137,8 +138,17 @@ pub(crate) fn add_peer( HttpError::for_internal_error(e.to_string()) })?; - let timeout = Duration::from_micros(rq.required_rx); - daemon.add_peer(rq.peer, timeout, rq.detection_threshold, rq.mode, ch, db); + daemon.add_peer( + rq.peer, + AddPeerRequest { + required_rx: Duration::from_micros(rq.required_rx), + detection_multiplier: rq.detection_threshold, + mode: rq.mode, + endpoint: ch, + egress_thread: Some(egress_thread), + db, + }, + ); Ok(()) } @@ -191,7 +201,7 @@ pub(crate) fn channel( src_port: u16, dst_port: u16, log: Logger, -) -> Result { +) -> Result<(BfdEndpoint, Arc)> { let (local, remote) = bidi::channel(); // Ensure there is a dispatcher thread for this listening address and a @@ -201,9 +211,10 @@ pub(crate) fn channel( // Spawn an egress thread to take packets from the session and send them // out a UDP socket. - egress(remote.rx, listen, src_port, dst_port, log.clone()); + let egress_thread = + egress(remote.rx, listen, src_port, dst_port, log.clone()); - Ok(local) + Ok((local, egress_thread)) } /// Bind a UDP socket for BFD egress and configure the TTL/Hop Limit to 255 @@ -226,14 +237,14 @@ fn egress( src_port: u16, dst_port: u16, log: Logger, -) { - spawn(move || { +) -> Arc { + let thread = Arc::new(ManagedThread::new()); + let handle = spawn(move || { let log = log.new(slog::o!( "local" => format!("{local}"), "src_port" => src_port, "dst_port" => dst_port, )); - 'egress: loop { let sk = match egress_socket(local, src_port) { Err(e) => { @@ -271,6 +282,8 @@ fn egress( } } }); + thread.start(handle); + thread } type Sessions = HashMap>; From ae04a430566df249a608e3a7e3192729894b23c3 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 4 Mar 2026 15:18:50 -0700 Subject: [PATCH 44/58] Use thread builder to name BFD threads --- bfd/src/sm.rs | 103 +++++++++++++++++++++++-------------------- mgd/src/bfd_admin.rs | 17 ++++--- 2 files changed, 67 insertions(+), 53 deletions(-) diff --git a/bfd/src/sm.rs b/bfd/src/sm.rs index 6eb4005a5..dd09ecb5b 100644 --- a/bfd/src/sm.rs +++ b/bfd/src/sm.rs @@ -13,8 +13,7 @@ use std::net::IpAddr; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::Sender; use std::sync::{Arc, Mutex, RwLock}; -use std::thread::sleep; -use std::thread::spawn; +use std::thread::{Builder, sleep}; use std::time::Duration; pub const UNIT_SESSION: &str = "session"; @@ -127,7 +126,9 @@ impl StateMachine { let kill_switch = self.kill_switch.clone(); let log = self.log.clone(); let counters = self.counters.clone(); - spawn(move || { + Builder::new() + .name(format!("bfd-recv-{peer}")) + .spawn(move || { loop { let prev = state.read().unwrap().state(); let (st, ep) = match state.read().unwrap().run( @@ -170,7 +171,8 @@ impl StateMachine { sm_log!(log, info, "transition -> {:?}", new; prev, peer); } } - }); + }) + .expect("failed to spawn bfd-recv thread"); } /// This is a send loop for a BFD peer. It takes care of sending out @@ -192,54 +194,59 @@ impl StateMachine { // just copy it out of self for sending into the spawned thread. The // reason this is a dynamic method at all is to get runtime polymorphic // behavior over `State` trait implementors. - spawn(move || { - loop { - if stop.load(Ordering::Relaxed) { - break; - }; - - // Get what we need from peer info, holding the lock a briefly as - // possible. - let (_delay, demand_mode, your_discriminator) = { - let r = lock!(remote); - ( - DeferredDelay(r.required_min_rx), - r.demand_mode, - r.discriminator, - ) - }; + Builder::new() + .name(format!("bfd-send-{peer}")) + .spawn(move || { + loop { + if stop.load(Ordering::Relaxed) { + break; + }; + + // Get what we need from peer info, holding the lock a + // briefly as possible. + let (_delay, demand_mode, your_discriminator) = { + let r = lock!(remote); + ( + DeferredDelay(r.required_min_rx), + r.demand_mode, + r.discriminator, + ) + }; + + // Unsolicited packets are not sent in demand mode. + // + // TODO we could probably just park this thread on a signal + // waiting to leave demand mode instead of continuing to + // iterate. + if demand_mode { + continue; + } - // Unsolicited packets are not sent in demand mode. - // - // TODO we could probably just park this thread on a signal waiting - // to leave demand mode instead of continuing to iterate. - if demand_mode { - continue; - } + let mut pkt = packet::Control { + desired_min_tx: local.desired_min_tx.as_micros() as u32, + required_min_rx: local.required_min_rx.as_micros() + as u32, + my_discriminator: local.discriminator, + your_discriminator, + ..Default::default() + }; - let mut pkt = packet::Control { - desired_min_tx: local.desired_min_tx.as_micros() as u32, - required_min_rx: local.required_min_rx.as_micros() as u32, - my_discriminator: local.discriminator, - your_discriminator, - ..Default::default() - }; + let st = state.read().unwrap().state(); + pkt.set_state(st); - let st = state.read().unwrap().state(); - pkt.set_state(st); - - if let Err(e) = sender.send((peer, pkt)) { - sm_log!(log, warn, "send error: {e}"; st, peer); - counters - .control_packet_send_failures - .fetch_add(1, Ordering::Relaxed); - } else { - counters - .control_packets_sent - .fetch_add(1, Ordering::Relaxed); + if let Err(e) = sender.send((peer, pkt)) { + sm_log!(log, warn, "send error: {e}"; st, peer); + counters + .control_packet_send_failures + .fetch_add(1, Ordering::Relaxed); + } else { + counters + .control_packets_sent + .fetch_add(1, Ordering::Relaxed); + } } - } - }); + }) + .expect("failed to spawn bfd-send thread"); } pub fn required_rx(&self) -> Duration { diff --git a/mgd/src/bfd_admin.rs b/mgd/src/bfd_admin.rs index 589550292..c1bdccd63 100644 --- a/mgd/src/bfd_admin.rs +++ b/mgd/src/bfd_admin.rs @@ -23,7 +23,7 @@ use std::{ atomic::AtomicBool, mpsc::{Receiver, Sender}, }, - thread::{JoinHandle, sleep, spawn}, + thread::{Builder, JoinHandle, sleep}, time::Duration, }; @@ -212,7 +212,7 @@ pub(crate) fn channel( // Spawn an egress thread to take packets from the session and send them // out a UDP socket. let egress_thread = - egress(remote.rx, listen, src_port, dst_port, log.clone()); + egress(remote.rx, listen, peer, src_port, dst_port, log.clone()); Ok((local, egress_thread)) } @@ -234,12 +234,15 @@ fn egress_socket(local: IpAddr, src_port: u16) -> std::io::Result { fn egress( rx: Receiver<(IpAddr, packet::Control)>, local: IpAddr, + peer: IpAddr, src_port: u16, dst_port: u16, log: Logger, ) -> Arc { let thread = Arc::new(ManagedThread::new()); - let handle = spawn(move || { + let handle = Builder::new() + .name(format!("bfd-egress-{peer}")) + .spawn(move || { let log = log.new(slog::o!( "local" => format!("{local}"), "src_port" => src_port, @@ -281,7 +284,8 @@ fn egress( } } } - }); + }) + .expect("failed to spawn bfd-egress thread"); thread.start(handle); thread } @@ -354,7 +358,10 @@ impl Dispatcher { local, Listener { sk: sk.try_clone()?, - handle: spawn(move || Self::listen(skl, sessions, ks, log)), + handle: Builder::new() + .name(format!("bfd-listen-{local}")) + .spawn(move || Self::listen(skl, sessions, ks, log)) + .expect("failed to spawn bfd-listen thread"), peers, kill_switch, }, From db99e96060bf7bb39eed1a2d74697f66fefacaad Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 4 Mar 2026 15:24:58 -0700 Subject: [PATCH 45/58] Plumb Result up from thread spawn After converting to using a thread builder instead of thread::spawn() (builder::spawn returns result, thread::spawn panics), the Result should be propagated back out to the callers so they can do proper error handling. --- bfd/src/lib.rs | 28 +++++++++++++++------------- bfd/src/sm.rs | 20 +++++++++++--------- mgd/src/bfd_admin.rs | 44 ++++++++++++++++++++++++-------------------- 3 files changed, 50 insertions(+), 42 deletions(-) diff --git a/bfd/src/lib.rs b/bfd/src/lib.rs index 52be96cb9..74b2a81f1 100644 --- a/bfd/src/lib.rs +++ b/bfd/src/lib.rs @@ -2,6 +2,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. +use anyhow::Result; use mg_common::thread::ManagedThread; use num_enum::TryFromPrimitive; use rdb::SessionMode; @@ -53,7 +54,7 @@ impl Daemon { /// Add a peer session to the deamon. Peer sessions are started immediately /// when added. - pub fn add_peer(&mut self, peer: IpAddr, rq: AddPeerRequest) { + pub fn add_peer(&mut self, peer: IpAddr, rq: AddPeerRequest) -> Result<()> { if self.sessions.contains_key(&peer) { warn!(self.log, "attempt to add peer that already exists"; "component" => COMPONENT_BFD, @@ -61,10 +62,11 @@ impl Daemon { "unit" => UNIT_PEER, "peer" => format!("{peer}") ); - return; + anyhow::bail!("BFD peer {peer} already exists"); } self.sessions - .insert(peer, Session::new(peer, rq, self.log.clone())); + .insert(peer, Session::new(peer, rq, self.log.clone())?); + Ok(()) } /// Remove a peer from the daemon. The peer will be immediately shut down. @@ -121,7 +123,7 @@ pub struct Session { impl Session { /// Create a new session and start running the underlying BFD state machine /// immediately. - fn new(addr: IpAddr, rq: AddPeerRequest, log: Logger) -> Self { + fn new(addr: IpAddr, rq: AddPeerRequest, log: Logger) -> Result { let counters = Arc::new(SessionCounters::default()); let mut sm = StateMachine::new( addr, @@ -130,13 +132,13 @@ impl Session { counters.clone(), log, ); - sm.run(rq.endpoint, rq.db); - Session { + sm.run(rq.endpoint, rq.db)?; + Ok(Session { sm, mode: rq.mode, counters, _egress_thread: rq.egress_thread, - } + }) } } @@ -333,7 +335,7 @@ mod test { egress_thread: None, db: db.db().clone(), }, - ); + )?; assert_eq!(daemon.peer_state(v4_addr), Some(BfdPeerState::Down)); // Add an IPv6 peer to the same daemon. @@ -349,7 +351,7 @@ mod test { egress_thread: None, db: db.db().clone(), }, - ); + )?; assert_eq!(daemon.peer_state(v6_addr), Some(BfdPeerState::Down)); // Both peers coexist. @@ -387,7 +389,7 @@ mod test { egress_thread: None, db: db.db().clone(), }, - ); + )?; net.register(v4_addr2, b); let (a, b) = bidi::channel(); @@ -401,7 +403,7 @@ mod test { egress_thread: None, db: db.db().clone(), }, - ); + )?; net.register(v6_addr2, b); // Daemon 2 peers with both v4 and v6 counterparts. @@ -417,7 +419,7 @@ mod test { egress_thread: None, db: db.db().clone(), }, - ); + )?; net.register(v4_addr1, b); let (a, b) = bidi::channel(); @@ -431,7 +433,7 @@ mod test { egress_thread: None, db: db.db().clone(), }, - ); + )?; net.register(v6_addr1, b); net.run(); diff --git a/bfd/src/sm.rs b/bfd/src/sm.rs index dd09ecb5b..0f16915aa 100644 --- a/bfd/src/sm.rs +++ b/bfd/src/sm.rs @@ -88,7 +88,7 @@ impl StateMachine { /// incoming control packets. Endpoint is a channel from a connection /// dispatcher that sends control packets to this state machine based /// on peer address and BFD discriminator. - pub fn run(&mut self, endpoint: BfdEndpoint, db: rdb::Db) { + pub fn run(&mut self, endpoint: BfdEndpoint, db: rdb::Db) -> Result<()> { let local = PeerInfo::with_random_discriminator( self.required_rx, self.detection_multiplier, @@ -98,11 +98,13 @@ impl StateMachine { // Spawn a thread that runs the send loop for this state machine. This // loop is responsible for sending out unsolicited periodic control // packets. - self.send_loop(endpoint.tx.clone(), local, remote.clone()); + self.send_loop(endpoint.tx.clone(), local, remote.clone())?; // Spawn a thread that runs the receive loop. This loop is responsible // for handling packets from the connection dispatcher. - self.recv_loop(endpoint, db, local, remote.clone()); + self.recv_loop(endpoint, db, local, remote.clone())?; + + Ok(()) } /// Get the current state of this state machine. @@ -120,7 +122,7 @@ impl StateMachine { db: rdb::Db, local: PeerInfo, remote: Arc>, - ) { + ) -> Result<()> { let state = self.state.clone(); let peer = self.peer; let kill_switch = self.kill_switch.clone(); @@ -171,8 +173,8 @@ impl StateMachine { sm_log!(log, info, "transition -> {:?}", new; prev, peer); } } - }) - .expect("failed to spawn bfd-recv thread"); + })?; + Ok(()) } /// This is a send loop for a BFD peer. It takes care of sending out @@ -184,7 +186,7 @@ impl StateMachine { sender: Sender<(IpAddr, packet::Control)>, local: PeerInfo, remote: Arc>, - ) { + ) -> Result<()> { let state = self.state.clone(); let peer = self.peer; let stop = self.kill_switch.clone(); @@ -245,8 +247,8 @@ impl StateMachine { .fetch_add(1, Ordering::Relaxed); } } - }) - .expect("failed to spawn bfd-send thread"); + })?; + Ok(()) } pub fn required_rx(&self) -> Duration { diff --git a/mgd/src/bfd_admin.rs b/mgd/src/bfd_admin.rs index c1bdccd63..07c82b70c 100644 --- a/mgd/src/bfd_admin.rs +++ b/mgd/src/bfd_admin.rs @@ -6,8 +6,8 @@ use crate::{admin::HandlerContext, log::bfd_log}; use anyhow::Result; use bfd::{AddPeerRequest, BfdEndpoint, DEFAULT_BFD_TTL, Daemon, bidi, packet}; use dropshot::{ - HttpError, HttpResponseOk, HttpResponseUpdatedNoContent, Path, - RequestContext, TypedBody, + ClientErrorStatusCode, HttpError, HttpResponseOk, + HttpResponseUpdatedNoContent, Path, RequestContext, TypedBody, }; use mg_api::{BfdPeerInfo, DeleteBfdPeerPathParams}; use mg_common::lock; @@ -105,7 +105,10 @@ pub(crate) fn add_peer( let db = ctx.db.clone(); if daemon.sessions.contains_key(&rq.peer) { - return Ok(()); + return Err(HttpError::for_client_error_with_status( + Some(format!("BFD peer {} already exists", rq.peer)), + ClientErrorStatusCode::CONFLICT, + )); } let (src_port, dst_port) = match rq.mode { @@ -138,17 +141,19 @@ pub(crate) fn add_peer( HttpError::for_internal_error(e.to_string()) })?; - daemon.add_peer( - rq.peer, - AddPeerRequest { - required_rx: Duration::from_micros(rq.required_rx), - detection_multiplier: rq.detection_threshold, - mode: rq.mode, - endpoint: ch, - egress_thread: Some(egress_thread), - db, - }, - ); + daemon + .add_peer( + rq.peer, + AddPeerRequest { + required_rx: Duration::from_micros(rq.required_rx), + detection_multiplier: rq.detection_threshold, + mode: rq.mode, + endpoint: ch, + egress_thread: Some(egress_thread), + db, + }, + ) + .map_err(|e| HttpError::for_internal_error(e.to_string()))?; Ok(()) } @@ -212,7 +217,7 @@ pub(crate) fn channel( // Spawn an egress thread to take packets from the session and send them // out a UDP socket. let egress_thread = - egress(remote.rx, listen, peer, src_port, dst_port, log.clone()); + egress(remote.rx, listen, peer, src_port, dst_port, log.clone())?; Ok((local, egress_thread)) } @@ -238,7 +243,7 @@ fn egress( src_port: u16, dst_port: u16, log: Logger, -) -> Arc { +) -> Result> { let thread = Arc::new(ManagedThread::new()); let handle = Builder::new() .name(format!("bfd-egress-{peer}")) @@ -285,9 +290,9 @@ fn egress( } } }) - .expect("failed to spawn bfd-egress thread"); + ?; thread.start(handle); - thread + Ok(thread) } type Sessions = HashMap>; @@ -360,8 +365,7 @@ impl Dispatcher { sk: sk.try_clone()?, handle: Builder::new() .name(format!("bfd-listen-{local}")) - .spawn(move || Self::listen(skl, sessions, ks, log)) - .expect("failed to spawn bfd-listen thread"), + .spawn(move || Self::listen(skl, sessions, ks, log))?, peers, kill_switch, }, From 9f7d3169f7c99b4de413932fa11ebe5981ea990f Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 4 Mar 2026 16:22:26 -0700 Subject: [PATCH 46/58] Handful of small improvements ensure() was returning a Result but the caller just dropped it, so now we just return Result<()>. Removes redundant presence check for peer during add_peer callpath. Updates egress() to use recv_timeout() to avoid possible deadlocks where recv() hangs on a dead channel and we miss the killswitch (AtomicBool). --- Cargo.lock | 1 + bfd/Cargo.toml | 1 + bfd/src/lib.rs | 21 ++++++-- mgd/src/bfd_admin.rs | 113 ++++++++++++++++++++++++------------------- 4 files changed, 83 insertions(+), 53 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4a4d047fe..c7bbad8c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -315,6 +315,7 @@ dependencies = [ "slog", "slog-async", "slog-bunyan", + "thiserror 1.0.69", ] [[package]] diff --git a/bfd/Cargo.toml b/bfd/Cargo.toml index 2bb35a0c5..0d23ea453 100644 --- a/bfd/Cargo.toml +++ b/bfd/Cargo.toml @@ -14,6 +14,7 @@ anyhow.workspace = true schemars.workspace = true serde.workspace = true rand.workspace = true +thiserror.workspace = true [dev-dependencies] pretty_assertions.workspace = true diff --git a/bfd/src/lib.rs b/bfd/src/lib.rs index 74b2a81f1..e469c2d83 100644 --- a/bfd/src/lib.rs +++ b/bfd/src/lib.rs @@ -52,9 +52,13 @@ impl Daemon { } } - /// Add a peer session to the deamon. Peer sessions are started immediately - /// when added. - pub fn add_peer(&mut self, peer: IpAddr, rq: AddPeerRequest) -> Result<()> { + /// Add a peer session to the daemon. + /// Peer sessions are started immediately when added. + pub fn add_peer( + &mut self, + peer: IpAddr, + rq: AddPeerRequest, + ) -> Result<(), AddPeerError> { if self.sessions.contains_key(&peer) { warn!(self.log, "attempt to add peer that already exists"; "component" => COMPONENT_BFD, @@ -62,7 +66,7 @@ impl Daemon { "unit" => UNIT_PEER, "peer" => format!("{peer}") ); - anyhow::bail!("BFD peer {peer} already exists"); + return Err(AddPeerError::PeerExists(peer)); } self.sessions .insert(peer, Session::new(peer, rq, self.log.clone())?); @@ -235,6 +239,15 @@ pub enum BfdPeerState { pub struct Admin {} +#[derive(Debug, thiserror::Error)] +pub enum AddPeerError { + #[error("BFD peer {0} already exists")] + PeerExists(IpAddr), + + #[error(transparent)] + Other(#[from] anyhow::Error), +} + #[cfg(test)] mod test { use super::*; diff --git a/mgd/src/bfd_admin.rs b/mgd/src/bfd_admin.rs index 07c82b70c..7877897b3 100644 --- a/mgd/src/bfd_admin.rs +++ b/mgd/src/bfd_admin.rs @@ -20,8 +20,8 @@ use std::{ net::{IpAddr, SocketAddr, UdpSocket}, sync::{ Arc, Mutex, RwLock, - atomic::AtomicBool, - mpsc::{Receiver, Sender}, + atomic::{AtomicBool, Ordering}, + mpsc::{Receiver, RecvTimeoutError, Sender}, }, thread::{Builder, JoinHandle, sleep}, time::Duration, @@ -133,7 +133,7 @@ pub(crate) fn add_peer( .map_err(|e| { bfd_log!(log, error, "udp channel error: {e}"; "params" => format!("{rq:?}"), - "peer" => format!("rq.peer"), + "peer" => format!("{}", rq.peer), "src_port" => src_port, "dst_port" => dst_port, "error" => format!("{e}") @@ -153,7 +153,17 @@ pub(crate) fn add_peer( db, }, ) - .map_err(|e| HttpError::for_internal_error(e.to_string()))?; + .map_err(|e| match e { + bfd::AddPeerError::PeerExists(_) => { + HttpError::for_client_error_with_status( + Some(e.to_string()), + ClientErrorStatusCode::CONFLICT, + ) + } + bfd::AddPeerError::Other(e) => { + HttpError::for_internal_error(e.to_string()) + } + })?; Ok(()) } @@ -245,52 +255,61 @@ fn egress( log: Logger, ) -> Result> { let thread = Arc::new(ManagedThread::new()); - let handle = Builder::new() - .name(format!("bfd-egress-{peer}")) - .spawn(move || { - let log = log.new(slog::o!( - "local" => format!("{local}"), - "src_port" => src_port, - "dst_port" => dst_port, - )); - 'egress: loop { - let sk = match egress_socket(local, src_port) { - Err(e) => { - bfd_log!(log, error, "failed to bind egress socket: {e}"; - "error" => format!("{e}") - ); - // Explicit sleep call here to prevent spin-lock in case - // socket creation/bind failures are persistent. - sleep(Duration::from_secs(5)); - continue; + let dropped = thread.dropped_flag(); + let handle = Builder::new().name(format!("bfd-egress-{peer}")).spawn( + move || { + let log = log.new(slog::o!( + "local" => format!("{local}"), + "src_port" => src_port, + "dst_port" => dst_port, + )); + 'egress: loop { + if dropped.load(Ordering::Relaxed) { + break; } - Ok(sk) => sk, - }; - 'socket: loop { - let (addr, pkt) = match rx.recv() { - Ok(result) => result, + let sk = match egress_socket(local, src_port) { Err(e) => { - bfd_log!(log, warn, "udp egress channel closed: {e}"; + bfd_log!(log, error, "failed to bind egress socket: {e}"; "error" => format!("{e}") ); - break 'egress; + // Explicit sleep call here to prevent spin-lock in case + // socket creation/bind failures are persistent. + sleep(Duration::from_secs(5)); + continue; } + Ok(sk) => sk, }; - let sa = SocketAddr::new(addr, dst_port); - if let Err(e) = sk.send_to(&pkt.to_bytes(), sa) { - bfd_log!(log, error, "udp send error: {e}"; - "message" => "control", - "message_contents" => format!("{pkt}"), - "error" => format!("{e}") - ); - break 'socket; + 'socket: loop { + let (addr, pkt) = + match rx.recv_timeout(Duration::from_secs(1)) { + Ok(result) => result, + Err(RecvTimeoutError::Timeout) => { + if dropped.load(Ordering::Relaxed) { + break 'egress; + } + continue; + } + Err(RecvTimeoutError::Disconnected) => { + bfd_log!(log, warn, "udp egress channel closed"); + break 'egress; + } + }; + + let sa = SocketAddr::new(addr, dst_port); + if let Err(e) = sk.send_to(&pkt.to_bytes(), sa) { + bfd_log!(log, error, "udp send error: {e}"; + "message" => "control", + "message_contents" => format!("{pkt}"), + "error" => format!("{e}") + ); + break 'socket; + } } } - } - }) - ?; + }, + )?; thread.start(handle); Ok(thread) } @@ -299,7 +318,6 @@ type Sessions = HashMap>; #[derive(Debug)] struct Listener { - sk: UdpSocket, #[allow(dead_code)] handle: JoinHandle<()>, peers: HashSet, @@ -342,11 +360,11 @@ impl Dispatcher { sender: Sender<(IpAddr, packet::Control)>, port: u16, log: Logger, - ) -> Result { + ) -> Result<()> { self.sessions.write().unwrap().insert(remote, sender); if let Some(ref mut listener) = self.listeners.get_mut(&local) { listener.peers.insert(remote); - Ok(listener.sk.try_clone()?) + Ok(()) } else { let sessions = self.sessions.clone(); let sa = SocketAddr::new(local, port); @@ -362,7 +380,6 @@ impl Dispatcher { self.listeners.insert( local, Listener { - sk: sk.try_clone()?, handle: Builder::new() .name(format!("bfd-listen-{local}")) .spawn(move || Self::listen(skl, sessions, ks, log))?, @@ -370,7 +387,7 @@ impl Dispatcher { kill_switch, }, ); - Ok(sk) + Ok(()) } } @@ -380,9 +397,7 @@ impl Dispatcher { if listener.peers.contains(&peer) { listener.peers.remove(&peer); if listener.peers.is_empty() { - listener - .kill_switch - .store(true, std::sync::atomic::Ordering::Relaxed); + listener.kill_switch.store(true, Ordering::Relaxed); to_remove.push(*local); } } @@ -401,7 +416,7 @@ impl Dispatcher { log: Logger, ) { loop { - if kill_switch.load(std::sync::atomic::Ordering::Relaxed) { + if kill_switch.load(Ordering::Relaxed) { bfd_log!( log, warn, From bb215368beb4e8a071af3667a618ba2ba868879a Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Tue, 17 Mar 2026 10:43:10 -0600 Subject: [PATCH 47/58] Stop bgp SessionClock thread on session shutdown The BGP SessionClock thread was not being stopped when the FSM was put into shutdown, although all its timers were. After migrating to thread Builder to have thread names, the thread leak test began failing due to the SessionClock threads not being stopped/joined. This adds a stop() method to ManagedThread, allowing the thread itself to be joined so it doesn't linger indefinitely. --- bgp/src/clock.rs | 101 ++++++++++++++++++++++++---------------- bgp/src/session.rs | 10 ++-- mg-common/src/thread.rs | 18 +++++++ 3 files changed, 86 insertions(+), 43 deletions(-) diff --git a/bgp/src/clock.rs b/bgp/src/clock.rs index b2997f998..e03c9414d 100644 --- a/bgp/src/clock.rs +++ b/bgp/src/clock.rs @@ -200,17 +200,21 @@ impl Display for Timer { /// Clock for session-level timers that persist across connections #[derive(Clone, Debug)] -pub struct SessionClock { +pub struct SessionClock { /// The rate at which the clock ticks pub resolution: Duration, /// The collection of BGP timers specific to this SessionRunner pub timers: Arc, /// Handle to the thread running the per-session clock - _thread: Arc, + thread: Arc, + /// Event sender for delivering timer expiry events to the FSM + event_tx: Sender>, + /// Logger for the clock thread + log: Logger, } -impl SessionClock { - pub fn new( +impl SessionClock { + pub fn new( resolution: Duration, connect_retry_interval: Duration, idle_hold_interval: Duration, @@ -233,55 +237,72 @@ impl SessionClock { None => Timer::new(idle_hold_interval), }), }); - let thread = Arc::new(ManagedThread::new()); - let _ = thread.start(Self::run( - resolution, - timers.clone(), - event_tx, - thread.dropped_flag(), - log, - )); Self { resolution, timers, - _thread: thread, + thread: Arc::new(ManagedThread::new()), + event_tx, + log, } } - fn run( + /// Start the clock thread. No-op if already running. + pub fn start(&self) { + if self.thread.is_running() { + return; + } + let dropped = self.thread.dropped_flag(); + let resolution = self.resolution; + let timers = self.timers.clone(); + let event_tx = self.event_tx.clone(); + let log = self.log.clone(); + let handle = std::thread::Builder::new() + .name("bgp-session-clock".into()) + .spawn(move || { + Self::run(resolution, timers, event_tx, dropped, log) + }) + .expect("failed to spawn bgp-session-clock thread"); + self.thread.start(handle); + } + + /// Stop the clock thread and disable all timers. + pub fn shutdown(&self) { + self.stop_all(); + self.thread.stop(); + } + + fn run( resolution: Duration, timers: Arc, event_tx: Sender>, dropped: Arc, log: Logger, - ) -> JoinHandle<()> { - spawn(move || { - loop { - if dropped.load(Ordering::Relaxed) { - break; - } - sleep(resolution); - - Self::step( - resolution, - &lock!(timers.connect_retry), - FsmEvent::Session(SessionEvent::ConnectRetryTimerExpires), - event_tx.clone(), - &log, - ); - - Self::step( - resolution, - &lock!(timers.idle_hold), - FsmEvent::Session(SessionEvent::IdleHoldTimerExpires), - event_tx.clone(), - &log, - ); + ) { + loop { + if dropped.load(Ordering::Relaxed) { + break; } - }) + sleep(resolution); + + Self::step( + resolution, + &lock!(timers.connect_retry), + FsmEvent::Session(SessionEvent::ConnectRetryTimerExpires), + event_tx.clone(), + &log, + ); + + Self::step( + resolution, + &lock!(timers.idle_hold), + FsmEvent::Session(SessionEvent::IdleHoldTimerExpires), + event_tx.clone(), + &log, + ); + } } - fn step( + fn step( resolution: Duration, timer: &Timer, event: FsmEvent, @@ -331,7 +352,7 @@ pub struct SessionTimerSnapshot { pub idle_hold_jitter: Option, } -impl Display for SessionClock { +impl Display for SessionClock { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { let connect_retry = lock!(self.timers.connect_retry); let idle_hold = lock!(self.timers.idle_hold); diff --git a/bgp/src/session.rs b/bgp/src/session.rs index dcd2d9f6b..c186411e2 100644 --- a/bgp/src/session.rs +++ b/bgp/src/session.rs @@ -1787,7 +1787,7 @@ pub struct SessionRunner { pub counters: Arc, /// Session-level clock for timers that persist across connections - pub clock: Arc, + pub clock: Arc>, /// Configuration for this BGP Session pub session: Arc>, @@ -2373,6 +2373,10 @@ impl SessionRunner { return; }; + // Ensure the session clock thread is running. It may have been + // stopped by a previous shutdown(). + self.clock.start(); + self.initialize_capabilities(); // Run the BGP peer state machine. @@ -7593,8 +7597,8 @@ impl SessionRunner { self.join_connector_thread(handle, "shutdown"); } - // Disable session-level timers - self.clock.stop_all(); + // Stop the session clock thread and disable all timers + self.clock.shutdown(); let previous = self.state(); let next = FsmStateKind::Idle; diff --git a/mg-common/src/thread.rs b/mg-common/src/thread.rs index 2415cf175..9d9ea76d8 100644 --- a/mg-common/src/thread.rs +++ b/mg-common/src/thread.rs @@ -95,6 +95,24 @@ impl ManagedThread { pub fn is_running(&self) -> bool { lock!(self.state).is_running() } + + /// Stop the thread: signal shutdown, join, and return to Ready. + /// + /// The dropped flag is reset so the thread can be restarted. + /// No-op if already in the Ready state. + pub fn stop(&self) { + self.dropped.store(true, Ordering::Relaxed); + + let mut state = lock!(self.state); + if let ThreadState::Running(handle) = + std::mem::replace(&mut *state, ThreadState::Ready) + { + drop(state); + let _ = handle.join(); + } + + self.dropped.store(false, Ordering::Relaxed); + } } impl Drop for ManagedThread { From 3c89bc42a6020b0c980fe55e268dbc91a6b4ee4d Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Thu, 12 Mar 2026 15:44:21 -0600 Subject: [PATCH 48/58] rename API version to SPRING_CLEANING --- bgp/src/params.rs | 10 +- mg-api/src/lib.rs | 126 +++++++++--------- mgd/src/admin.rs | 4 +- mgd/src/bgp_admin.rs | 4 +- mgd/src/rib_admin.rs | 4 +- mgd/src/static_admin.rs | 4 +- ...36296f.json => mg-admin-7.0.0-1f75e9.json} | 4 +- openapi/mg-admin/mg-admin-latest.json | 2 +- rdb/src/types.rs | 8 +- 9 files changed, 83 insertions(+), 83 deletions(-) rename openapi/mg-admin/{mg-admin-7.0.0-36296f.json => mg-admin-7.0.0-1f75e9.json} (99%) diff --git a/bgp/src/params.rs b/bgp/src/params.rs index 14e7c0538..0ae1e4b1e 100644 --- a/bgp/src/params.rs +++ b/bgp/src/params.rs @@ -840,8 +840,8 @@ impl From<&Capability> for BgpCapability { } /// Previous version of BgpCapability. -/// Used for API versions before VERSION_EXTENDED_NH_STATIC. -/// Delete when VERSION_EXTENDED_NH_STATIC is the minimum supported version. +/// Used for API versions before VERSION_SPRING_CLEANING. +/// Delete when VERSION_SPRING_CLEANING is the minimum supported version. #[derive(Debug, Deserialize, Serialize, JsonSchema)] #[schemars(rename = "BgpCapability")] pub enum BgpCapabilityV1 { @@ -1414,12 +1414,12 @@ pub enum PolicyKind { } // ============================================================================ -// API Compatibility Types (VERSION_MP_BGP..VERSION_EXTENDED_NH_STATIC) +// API Compatibility Types (VERSION_MP_BGP..VERSION_SPRING_CLEANING) // ============================================================================ // These types represent the v3-v6 API format (per-AF config, no DSCP). // Used for API versions between VERSION_MP_BGP and -// VERSION_EXTENDED_NH_STATIC. -// Delete when VERSION_EXTENDED_NH_STATIC is the minimum supported version. +// VERSION_SPRING_CLEANING. +// Delete when VERSION_SPRING_CLEANING is the minimum supported version. /// BGP peer parameters for v3-v6 API (per-AF config, no DSCP). #[derive(Debug, Deserialize, Serialize, JsonSchema, Clone, PartialEq)] diff --git a/mg-api/src/lib.rs b/mg-api/src/lib.rs index 5ee65dfee..9b879344f 100644 --- a/mg-api/src/lib.rs +++ b/mg-api/src/lib.rs @@ -48,7 +48,7 @@ api_versions!([ // | example for the next person. // v // (next_int, IDENT), - (7, EXTENDED_NH_STATIC), + (7, SPRING_CLEANING), (6, RIB_EXPORTED_STRING_KEY), (5, UNNUMBERED), (4, MP_BGP), @@ -190,64 +190,64 @@ pub trait MgAdminApi { request: Query, ) -> Result; - // Unified API (VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC) + // Unified API (VERSION_UNNUMBERED..VERSION_SPRING_CLEANING) // Per-AF config, no DSCP. PeerId in path parameters. - #[endpoint { method = PUT, path = "/bgp/config/neighbor", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] + #[endpoint { method = PUT, path = "/bgp/config/neighbor", versions = VERSION_UNNUMBERED..VERSION_SPRING_CLEANING }] async fn create_neighbor_v3( rqctx: RequestContext, request: TypedBody, ) -> Result; - #[endpoint { method = GET, path = "/bgp/config/neighbor/{asn}/{peer}", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] + #[endpoint { method = GET, path = "/bgp/config/neighbor/{asn}/{peer}", versions = VERSION_UNNUMBERED..VERSION_SPRING_CLEANING }] async fn read_neighbor_v3( rqctx: RequestContext, path: Path, ) -> Result, HttpError>; - #[endpoint { method = GET, path = "/bgp/config/neighbors/{asn}", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] + #[endpoint { method = GET, path = "/bgp/config/neighbors/{asn}", versions = VERSION_UNNUMBERED..VERSION_SPRING_CLEANING }] async fn read_neighbors_v3( rqctx: RequestContext, path: Path, ) -> Result>, HttpError>; - #[endpoint { method = POST, path = "/bgp/config/neighbor", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] + #[endpoint { method = POST, path = "/bgp/config/neighbor", versions = VERSION_UNNUMBERED..VERSION_SPRING_CLEANING }] async fn update_neighbor_v3( rqctx: RequestContext, request: TypedBody, ) -> Result; - #[endpoint { method = DELETE, path = "/bgp/config/neighbor/{asn}/{peer}", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] + #[endpoint { method = DELETE, path = "/bgp/config/neighbor/{asn}/{peer}", versions = VERSION_UNNUMBERED..VERSION_SPRING_CLEANING }] async fn delete_neighbor_v3( rqctx: RequestContext, path: Path, ) -> Result; - // Unified API (VERSION_EXTENDED_NH_STATIC..) - with DSCP support - #[endpoint { method = PUT, path = "/bgp/config/neighbor", versions = VERSION_EXTENDED_NH_STATIC.. }] + // Unified API (VERSION_SPRING_CLEANING..) - with DSCP support + #[endpoint { method = PUT, path = "/bgp/config/neighbor", versions = VERSION_SPRING_CLEANING.. }] async fn create_neighbor_v4( rqctx: RequestContext, request: TypedBody, ) -> Result; - #[endpoint { method = GET, path = "/bgp/config/neighbor/{asn}/{peer}", versions = VERSION_EXTENDED_NH_STATIC.. }] + #[endpoint { method = GET, path = "/bgp/config/neighbor/{asn}/{peer}", versions = VERSION_SPRING_CLEANING.. }] async fn read_neighbor_v4( rqctx: RequestContext, path: Path, ) -> Result, HttpError>; - #[endpoint { method = GET, path = "/bgp/config/neighbors/{asn}", versions = VERSION_EXTENDED_NH_STATIC.. }] + #[endpoint { method = GET, path = "/bgp/config/neighbors/{asn}", versions = VERSION_SPRING_CLEANING.. }] async fn read_neighbors_v4( rqctx: RequestContext, path: Path, ) -> Result>, HttpError>; - #[endpoint { method = POST, path = "/bgp/config/neighbor", versions = VERSION_EXTENDED_NH_STATIC.. }] + #[endpoint { method = POST, path = "/bgp/config/neighbor", versions = VERSION_SPRING_CLEANING.. }] async fn update_neighbor_v4( rqctx: RequestContext, request: TypedBody, ) -> Result; - #[endpoint { method = DELETE, path = "/bgp/config/neighbor/{asn}/{peer}", versions = VERSION_EXTENDED_NH_STATIC.. }] + #[endpoint { method = DELETE, path = "/bgp/config/neighbor/{asn}/{peer}", versions = VERSION_SPRING_CLEANING.. }] async fn delete_neighbor_v4( rqctx: RequestContext, path: Path, @@ -269,12 +269,12 @@ pub trait MgAdminApi { // Unnumbered neighbors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - // Unnumbered neighbors (VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC) + // Unnumbered neighbors (VERSION_UNNUMBERED..VERSION_SPRING_CLEANING) // No DSCP support. #[endpoint { method = GET, path = "/bgp/config/unnumbered-neighbors", - versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC, + versions = VERSION_UNNUMBERED..VERSION_SPRING_CLEANING, }] async fn read_unnumbered_neighbors( rqctx: RequestContext, @@ -284,7 +284,7 @@ pub trait MgAdminApi { #[endpoint { method = PUT, path = "/bgp/config/unnumbered-neighbor", - versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC, + versions = VERSION_UNNUMBERED..VERSION_SPRING_CLEANING, }] async fn create_unnumbered_neighbor( rqctx: RequestContext, @@ -294,7 +294,7 @@ pub trait MgAdminApi { #[endpoint { method = GET, path = "/bgp/config/unnumbered-neighbor", - versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC, + versions = VERSION_UNNUMBERED..VERSION_SPRING_CLEANING, }] async fn read_unnumbered_neighbor( rqctx: RequestContext, @@ -304,7 +304,7 @@ pub trait MgAdminApi { #[endpoint { method = POST, path = "/bgp/config/unnumbered-neighbor", - versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC, + versions = VERSION_UNNUMBERED..VERSION_SPRING_CLEANING, }] async fn update_unnumbered_neighbor( rqctx: RequestContext, @@ -314,18 +314,18 @@ pub trait MgAdminApi { #[endpoint { method = DELETE, path = "/bgp/config/unnumbered-neighbor", - versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC, + versions = VERSION_UNNUMBERED..VERSION_SPRING_CLEANING, }] async fn delete_unnumbered_neighbor( rqctx: RequestContext, request: Query, ) -> Result; - // Unnumbered neighbors (VERSION_EXTENDED_NH_STATIC..) - with DSCP + // Unnumbered neighbors (VERSION_SPRING_CLEANING..) - with DSCP #[endpoint { method = GET, path = "/bgp/config/unnumbered-neighbors", - versions = VERSION_EXTENDED_NH_STATIC.., + versions = VERSION_SPRING_CLEANING.., }] async fn read_unnumbered_neighbors_v2( rqctx: RequestContext, @@ -335,7 +335,7 @@ pub trait MgAdminApi { #[endpoint { method = PUT, path = "/bgp/config/unnumbered-neighbor", - versions = VERSION_EXTENDED_NH_STATIC.., + versions = VERSION_SPRING_CLEANING.., }] async fn create_unnumbered_neighbor_v2( rqctx: RequestContext, @@ -345,7 +345,7 @@ pub trait MgAdminApi { #[endpoint { method = GET, path = "/bgp/config/unnumbered-neighbor", - versions = VERSION_EXTENDED_NH_STATIC.., + versions = VERSION_SPRING_CLEANING.., }] async fn read_unnumbered_neighbor_v2( rqctx: RequestContext, @@ -355,7 +355,7 @@ pub trait MgAdminApi { #[endpoint { method = POST, path = "/bgp/config/unnumbered-neighbor", - versions = VERSION_EXTENDED_NH_STATIC.., + versions = VERSION_SPRING_CLEANING.., }] async fn update_unnumbered_neighbor_v2( rqctx: RequestContext, @@ -365,7 +365,7 @@ pub trait MgAdminApi { #[endpoint { method = DELETE, path = "/bgp/config/unnumbered-neighbor", - versions = VERSION_EXTENDED_NH_STATIC.., + versions = VERSION_SPRING_CLEANING.., }] async fn delete_unnumbered_neighbor_v2( rqctx: RequestContext, @@ -482,29 +482,29 @@ pub trait MgAdminApi { request: Query, ) -> Result, HttpError>; - // VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC: PeerId but no origin/internal - #[endpoint { method = GET, path = "/rib/status/imported", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] + // VERSION_UNNUMBERED..VERSION_SPRING_CLEANING: PeerId but no origin/internal + #[endpoint { method = GET, path = "/rib/status/imported", versions = VERSION_UNNUMBERED..VERSION_SPRING_CLEANING }] async fn get_rib_imported_v2( rqctx: RequestContext, request: Query, ) -> Result, HttpError>; - // VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC: PeerId but no origin/internal - #[endpoint { method = GET, path = "/rib/status/selected", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] + // VERSION_UNNUMBERED..VERSION_SPRING_CLEANING: PeerId but no origin/internal + #[endpoint { method = GET, path = "/rib/status/selected", versions = VERSION_UNNUMBERED..VERSION_SPRING_CLEANING }] async fn get_rib_selected_v2( rqctx: RequestContext, request: Query, ) -> Result, HttpError>; - // VERSION_EXTENDED_NH_STATIC+: BgpPathProperties with origin/internal - #[endpoint { method = GET, path = "/rib/status/imported", versions = VERSION_EXTENDED_NH_STATIC.. }] + // VERSION_SPRING_CLEANING+: BgpPathProperties with origin/internal + #[endpoint { method = GET, path = "/rib/status/imported", versions = VERSION_SPRING_CLEANING.. }] async fn get_rib_imported_v3( rqctx: RequestContext, request: Query, ) -> Result, HttpError>; - // VERSION_EXTENDED_NH_STATIC+: BgpPathProperties with origin/internal - #[endpoint { method = GET, path = "/rib/status/selected", versions = VERSION_EXTENDED_NH_STATIC.. }] + // VERSION_SPRING_CLEANING+: BgpPathProperties with origin/internal + #[endpoint { method = GET, path = "/rib/status/selected", versions = VERSION_SPRING_CLEANING.. }] async fn get_rib_selected_v3( rqctx: RequestContext, request: Query, @@ -528,13 +528,13 @@ pub trait MgAdminApi { request: Query, ) -> Result>, HttpError>; - #[endpoint { method = GET, path = "/bgp/status/neighbors", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] + #[endpoint { method = GET, path = "/bgp/status/neighbors", versions = VERSION_UNNUMBERED..VERSION_SPRING_CLEANING }] async fn get_neighbors_v4( rqctx: RequestContext, request: Query, ) -> Result>, HttpError>; - #[endpoint { method = GET, path = "/bgp/status/neighbors", versions = VERSION_EXTENDED_NH_STATIC.. }] + #[endpoint { method = GET, path = "/bgp/status/neighbors", versions = VERSION_SPRING_CLEANING.. }] async fn get_neighbors_v5( rqctx: RequestContext, request: Query, @@ -548,14 +548,14 @@ pub trait MgAdminApi { ) -> Result; // V3 API - ApplyRequest with per-AF policies (no DSCP) - #[endpoint { method = POST, path = "/bgp/omicron/apply", versions = VERSION_MP_BGP..VERSION_EXTENDED_NH_STATIC }] + #[endpoint { method = POST, path = "/bgp/omicron/apply", versions = VERSION_MP_BGP..VERSION_SPRING_CLEANING }] async fn bgp_apply_v2( rqctx: RequestContext, request: TypedBody, ) -> Result; // V4 API - ApplyRequest with DSCP support - #[endpoint { method = POST, path = "/bgp/omicron/apply", versions = VERSION_EXTENDED_NH_STATIC.. }] + #[endpoint { method = POST, path = "/bgp/omicron/apply", versions = VERSION_SPRING_CLEANING.. }] async fn bgp_apply_v3( rqctx: RequestContext, request: TypedBody, @@ -573,13 +573,13 @@ pub trait MgAdminApi { request: TypedBody, ) -> Result, HttpError>; - #[endpoint { method = GET, path = "/bgp/history/message", versions = VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC }] + #[endpoint { method = GET, path = "/bgp/history/message", versions = VERSION_UNNUMBERED..VERSION_SPRING_CLEANING }] async fn message_history_v3( rqctx: RequestContext, request: TypedBody, ) -> Result, HttpError>; - #[endpoint { method = GET, path = "/bgp/history/message", versions = VERSION_EXTENDED_NH_STATIC.. }] + #[endpoint { method = GET, path = "/bgp/history/message", versions = VERSION_SPRING_CLEANING.. }] async fn message_history_v4( rqctx: RequestContext, request: TypedBody, @@ -668,72 +668,72 @@ pub trait MgAdminApi { request: TypedBody, ) -> Result; - // V1 static route endpoints (pre-EXTENDED_NH_STATIC): typed nexthop per AF - #[endpoint { method = PUT, path = "/static/route4", versions = ..VERSION_EXTENDED_NH_STATIC }] + // V1 static route endpoints (pre-SPRING_CLEANING): typed nexthop per AF + #[endpoint { method = PUT, path = "/static/route4", versions = ..VERSION_SPRING_CLEANING }] async fn static_add_v4_route( rqctx: RequestContext, request: TypedBody, ) -> Result; - #[endpoint { method = DELETE, path = "/static/route4", versions = ..VERSION_EXTENDED_NH_STATIC }] + #[endpoint { method = DELETE, path = "/static/route4", versions = ..VERSION_SPRING_CLEANING }] async fn static_remove_v4_route( rqctx: RequestContext, request: TypedBody, ) -> Result; - #[endpoint { method = GET, path = "/static/route4", versions = ..VERSION_EXTENDED_NH_STATIC }] + #[endpoint { method = GET, path = "/static/route4", versions = ..VERSION_SPRING_CLEANING }] async fn static_list_v4_routes( rqctx: RequestContext, ) -> Result, HttpError>; - #[endpoint { method = PUT, path = "/static/route6", versions = VERSION_IPV6_BASIC..VERSION_EXTENDED_NH_STATIC }] + #[endpoint { method = PUT, path = "/static/route6", versions = VERSION_IPV6_BASIC..VERSION_SPRING_CLEANING }] async fn static_add_v6_route( ctx: RequestContext, request: TypedBody, ) -> Result; - #[endpoint { method = DELETE, path = "/static/route6", versions = VERSION_IPV6_BASIC..VERSION_EXTENDED_NH_STATIC }] + #[endpoint { method = DELETE, path = "/static/route6", versions = VERSION_IPV6_BASIC..VERSION_SPRING_CLEANING }] async fn static_remove_v6_route( ctx: RequestContext, request: TypedBody, ) -> Result; - #[endpoint { method = GET, path = "/static/route6", versions = VERSION_IPV6_BASIC..VERSION_EXTENDED_NH_STATIC }] + #[endpoint { method = GET, path = "/static/route6", versions = VERSION_IPV6_BASIC..VERSION_SPRING_CLEANING }] async fn static_list_v6_routes( ctx: RequestContext, ) -> Result, HttpError>; - // V2 static route endpoints (VERSION_EXTENDED_NH_STATIC+): IpAddr nexthop - #[endpoint { method = PUT, path = "/static/route4", versions = VERSION_EXTENDED_NH_STATIC.. }] + // V2 static route endpoints (VERSION_SPRING_CLEANING+): IpAddr nexthop + #[endpoint { method = PUT, path = "/static/route4", versions = VERSION_SPRING_CLEANING.. }] async fn static_add_v4_route_v2( rqctx: RequestContext, request: TypedBody, ) -> Result; - #[endpoint { method = DELETE, path = "/static/route4", versions = VERSION_EXTENDED_NH_STATIC.. }] + #[endpoint { method = DELETE, path = "/static/route4", versions = VERSION_SPRING_CLEANING.. }] async fn static_remove_v4_route_v2( rqctx: RequestContext, request: TypedBody, ) -> Result; - #[endpoint { method = GET, path = "/static/route4", versions = VERSION_EXTENDED_NH_STATIC.. }] + #[endpoint { method = GET, path = "/static/route4", versions = VERSION_SPRING_CLEANING.. }] async fn static_list_v4_routes_v2( rqctx: RequestContext, ) -> Result, HttpError>; - #[endpoint { method = PUT, path = "/static/route6", versions = VERSION_EXTENDED_NH_STATIC.. }] + #[endpoint { method = PUT, path = "/static/route6", versions = VERSION_SPRING_CLEANING.. }] async fn static_add_v6_route_v2( ctx: RequestContext, request: TypedBody, ) -> Result; - #[endpoint { method = DELETE, path = "/static/route6", versions = VERSION_EXTENDED_NH_STATIC.. }] + #[endpoint { method = DELETE, path = "/static/route6", versions = VERSION_SPRING_CLEANING.. }] async fn static_remove_v6_route_v2( ctx: RequestContext, request: TypedBody, ) -> Result; - #[endpoint { method = GET, path = "/static/route6", versions = VERSION_EXTENDED_NH_STATIC.. }] + #[endpoint { method = GET, path = "/static/route6", versions = VERSION_SPRING_CLEANING.. }] async fn static_list_v6_routes_v2( ctx: RequestContext, ) -> Result, HttpError>; @@ -843,8 +843,8 @@ impl From for RibV1 { } /// V2 Rib with PathV2 (no origin/internal in BgpPathProperties). -/// Used for VERSION_UNNUMBERED through VERSION_EXTENDED_NH_STATIC. -/// Delete when VERSION_EXTENDED_NH_STATIC is the minimum supported +/// Used for VERSION_UNNUMBERED through VERSION_SPRING_CLEANING. +/// Delete when VERSION_SPRING_CLEANING is the minimum supported /// version. #[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)] #[schemars(rename = "Rib")] @@ -978,7 +978,7 @@ pub struct MessageHistoryResponseV4 { pub by_peer: HashMap, } -/// V3 message history request (VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC) +/// V3 message history request (VERSION_UNNUMBERED..VERSION_SPRING_CLEANING) #[derive(Debug, Deserialize, JsonSchema, Clone)] pub struct MessageHistoryRequest { /// ASN of the BGP router @@ -992,7 +992,7 @@ pub struct MessageHistoryRequest { pub direction: Option, } -/// V3 message history response (VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC) +/// V3 message history response (VERSION_UNNUMBERED..VERSION_SPRING_CLEANING) #[derive(Debug, Serialize, JsonSchema, Clone)] pub struct MessageHistoryResponse { pub by_peer: HashMap, @@ -1008,7 +1008,7 @@ pub enum MessageBuffer { Major, } -/// V4 message history request (VERSION_EXTENDED_NH_STATIC..) +/// V4 message history request (VERSION_SPRING_CLEANING..) #[derive(Debug, Deserialize, JsonSchema, Clone)] pub struct MessageHistoryRequestV5 { /// ASN of the BGP router @@ -1024,7 +1024,7 @@ pub struct MessageHistoryRequestV5 { pub buffer: Option, } -/// V4 message history response (VERSION_EXTENDED_NH_STATIC..) +/// V4 message history response (VERSION_SPRING_CLEANING..) /// Flat list of entries per peer, each entry carries its own direction. #[derive(Debug, Serialize, JsonSchema, Clone)] pub struct MessageHistoryResponseV5 { @@ -1096,7 +1096,7 @@ pub struct BestpathFanoutResponse { pub type GetRibResult = BTreeMap>; // ============================================================================ -// Archived Static Route Types (Pre-VERSION_EXTENDED_NH_STATIC) +// Archived Static Route Types (Pre-VERSION_SPRING_CLEANING) // ============================================================================ #[derive(Debug, Deserialize, Serialize, JsonSchema)] @@ -1178,7 +1178,7 @@ impl From for StaticRouteKey { } // ============================================================================ -// Current Static Route Types (VERSION_EXTENDED_NH_STATIC and later) +// Current Static Route Types (VERSION_SPRING_CLEANING and later) // ============================================================================ #[derive(Debug, Deserialize, Serialize, JsonSchema)] @@ -1254,8 +1254,8 @@ impl From for StaticRouteKey { } /// Previous version of RibQuery. -/// Used for API versions before VERSION_EXTENDED_NH_STATIC. -/// Delete when VERSION_EXTENDED_NH_STATIC is the minimum supported +/// Used for API versions before VERSION_SPRING_CLEANING. +/// Delete when VERSION_SPRING_CLEANING is the minimum supported /// version. #[derive(Debug, Deserialize, Serialize, JsonSchema)] #[schemars(rename = "RibQuery")] diff --git a/mgd/src/admin.rs b/mgd/src/admin.rs index fcdb2a7f6..51fabe5fe 100644 --- a/mgd/src/admin.rs +++ b/mgd/src/admin.rs @@ -697,7 +697,7 @@ impl MgAdminApi for MgAdminApiImpl { rib_admin::update_rib_bestpath_fanout(rqctx, request).await } - // V1 static route methods (pre-EXTENDED_NH_STATIC) + // V1 static route methods (pre-SPRING_CLEANING) async fn static_add_v4_route( ctx: RequestContext, request: TypedBody, @@ -738,7 +738,7 @@ impl MgAdminApi for MgAdminApiImpl { static_admin::static_list_v6_routes(ctx).await } - // V2 static route methods (VERSION_EXTENDED_NH_STATIC+) + // V2 static route methods (VERSION_SPRING_CLEANING+) async fn static_add_v4_route_v2( ctx: RequestContext, request: TypedBody, diff --git a/mgd/src/bgp_admin.rs b/mgd/src/bgp_admin.rs index f6c64b9d4..8e6e52ed5 100644 --- a/mgd/src/bgp_admin.rs +++ b/mgd/src/bgp_admin.rs @@ -1916,7 +1916,7 @@ pub async fn message_history_v2( Ok(HttpResponseOk(MessageHistoryResponseV4 { by_peer })) } -// V3 API endpoint (VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC) +// V3 API endpoint (VERSION_UNNUMBERED..VERSION_SPRING_CLEANING) pub async fn message_history_v3( ctx: RequestContext>, request: TypedBody, @@ -1938,7 +1938,7 @@ pub async fn message_history_v3( Ok(HttpResponseOk(MessageHistoryResponse { by_peer })) } -// V4 API endpoint (VERSION_EXTENDED_NH_STATIC..) +// V4 API endpoint (VERSION_SPRING_CLEANING..) pub async fn message_history_v4( ctx: RequestContext>, request: TypedBody, diff --git a/mgd/src/rib_admin.rs b/mgd/src/rib_admin.rs index 0d592dfd2..74b1cd1d6 100644 --- a/mgd/src/rib_admin.rs +++ b/mgd/src/rib_admin.rs @@ -38,7 +38,7 @@ pub async fn get_rib_selected( Ok(HttpResponseOk(filtered.into())) } -// VERSION_UNNUMBERED..VERSION_EXTENDED_NH_STATIC (PeerId, no origin/internal) +// VERSION_UNNUMBERED..VERSION_SPRING_CLEANING (PeerId, no origin/internal) pub async fn get_rib_imported_v2( ctx: RequestContext>, query: Query, @@ -61,7 +61,7 @@ pub async fn get_rib_selected_v2( Ok(HttpResponseOk(filtered.into())) } -// VERSION_EXTENDED_NH_STATIC+ (BgpPathProperties with origin/internal) +// VERSION_SPRING_CLEANING+ (BgpPathProperties with origin/internal) pub async fn get_rib_imported_v3( ctx: RequestContext>, query: Query, diff --git a/mgd/src/static_admin.rs b/mgd/src/static_admin.rs index 6ecd386a6..132cf9704 100644 --- a/mgd/src/static_admin.rs +++ b/mgd/src/static_admin.rs @@ -17,7 +17,7 @@ use mg_api::{ use rdb::{AddressFamily, Prefix, StaticRouteKey}; use std::{collections::BTreeMap, sync::Arc}; -// V1 handlers (pre-EXTENDED_NH_STATIC): typed nexthop per AF +// V1 handlers (pre-SPRING_CLEANING): typed nexthop per AF pub async fn static_add_v4_route( ctx: RequestContext>, @@ -139,7 +139,7 @@ pub async fn static_list_v6_routes( Ok(HttpResponseOk(static_rib)) } -// V2 handlers (VERSION_EXTENDED_NH_STATIC+): IpAddr nexthop +// V2 handlers (VERSION_SPRING_CLEANING+): IpAddr nexthop pub async fn static_add_v4_route_v2( ctx: RequestContext>, diff --git a/openapi/mg-admin/mg-admin-7.0.0-36296f.json b/openapi/mg-admin/mg-admin-7.0.0-1f75e9.json similarity index 99% rename from openapi/mg-admin/mg-admin-7.0.0-36296f.json rename to openapi/mg-admin/mg-admin-7.0.0-1f75e9.json index 0713b2d14..21d538cc6 100644 --- a/openapi/mg-admin/mg-admin-7.0.0-36296f.json +++ b/openapi/mg-admin/mg-admin-7.0.0-1f75e9.json @@ -3658,7 +3658,7 @@ ] }, "MessageHistoryRequestV5": { - "description": "V4 message history request (VERSION_EXTENDED_NH_STATIC..)", + "description": "V4 message history request (VERSION_SPRING_CLEANING..)", "type": "object", "properties": { "asn": { @@ -3700,7 +3700,7 @@ ] }, "MessageHistoryResponseV5": { - "description": "V4 message history response (VERSION_EXTENDED_NH_STATIC..) Flat list of entries per peer, each entry carries its own direction.", + "description": "V4 message history response (VERSION_SPRING_CLEANING..) Flat list of entries per peer, each entry carries its own direction.", "type": "object", "properties": { "by_peer": { diff --git a/openapi/mg-admin/mg-admin-latest.json b/openapi/mg-admin/mg-admin-latest.json index 5b9d7f280..b4d7df63a 120000 --- a/openapi/mg-admin/mg-admin-latest.json +++ b/openapi/mg-admin/mg-admin-latest.json @@ -1 +1 @@ -mg-admin-7.0.0-36296f.json \ No newline at end of file +mg-admin-7.0.0-1f75e9.json \ No newline at end of file diff --git a/rdb/src/types.rs b/rdb/src/types.rs index 10f0d3975..f92aa5103 100644 --- a/rdb/src/types.rs +++ b/rdb/src/types.rs @@ -211,8 +211,8 @@ impl From for BgpPathPropertiesV1 { /// Pre-BESTPATH version of BgpPathProperties (no origin/internal). /// Used for API versions VERSION_UNNUMBERED through -/// VERSION_EXTENDED_NH_STATIC (5.0.0 - 6.0.0). -/// Delete when VERSION_EXTENDED_NH_STATIC is the minimum supported +/// VERSION_SPRING_CLEANING (5.0.0 - 6.0.0). +/// Delete when VERSION_SPRING_CLEANING is the minimum supported /// version. #[derive( Debug, @@ -252,8 +252,8 @@ impl From for BgpPathPropertiesV2 { /// Pre-BESTPATH version of Path (no origin/internal in BgpPathProperties). /// Used for API versions VERSION_UNNUMBERED through -/// VERSION_EXTENDED_NH_STATIC (5.0.0 - 6.0.0). -/// Delete when VERSION_EXTENDED_NH_STATIC is the minimum supported +/// VERSION_SPRING_CLEANING (5.0.0 - 6.0.0). +/// Delete when VERSION_SPRING_CLEANING is the minimum supported /// version. #[derive( Debug, From 61ba7aa98c72867eb312db6fb37ebd55bc6a21b5 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Tue, 17 Mar 2026 12:29:13 -0600 Subject: [PATCH 49/58] rdb: no means no Fix PolicyAction::from_str() to convert "deny" into Self::Deny instead of Self::Allow. --- rdb/src/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rdb/src/types.rs b/rdb/src/types.rs index f92aa5103..28902482d 100644 --- a/rdb/src/types.rs +++ b/rdb/src/types.rs @@ -520,7 +520,7 @@ impl FromStr for PolicyAction { fn from_str(s: &str) -> Result { match s { "allow" | "Allow" => Ok(Self::Allow), - "deny" | "Deny" => Ok(Self::Allow), + "deny" | "Deny" => Ok(Self::Deny), _ => Err("Unknown policy action, must be allow or deny".into()), } } From 8f25eda45ce11ca22d1d9a3806283b9c4faa0041 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Tue, 17 Mar 2026 16:05:12 -0600 Subject: [PATCH 50/58] openapi: convert v6 to git stub --- openapi/mg-admin/mg-admin-6.0.0-324339.json | 5787 ----------------- .../mg-admin-6.0.0-324339.json.gitstub | 1 + 2 files changed, 1 insertion(+), 5787 deletions(-) delete mode 100644 openapi/mg-admin/mg-admin-6.0.0-324339.json create mode 100644 openapi/mg-admin/mg-admin-6.0.0-324339.json.gitstub diff --git a/openapi/mg-admin/mg-admin-6.0.0-324339.json b/openapi/mg-admin/mg-admin-6.0.0-324339.json deleted file mode 100644 index 4c543b9a5..000000000 --- a/openapi/mg-admin/mg-admin-6.0.0-324339.json +++ /dev/null @@ -1,5787 +0,0 @@ -{ - "openapi": "3.0.3", - "info": { - "title": "Maghemite Admin", - "contact": { - "url": "https://oxide.computer", - "email": "api@oxide.computer" - }, - "version": "6.0.0" - }, - "paths": { - "/bfd/peers": { - "get": { - "summary": "Get all the peers and their associated BFD state. Peers are identified by IP", - "description": "address.", - "operationId": "get_bfd_peers", - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "title": "Array_of_BfdPeerInfo", - "type": "array", - "items": { - "$ref": "#/components/schemas/BfdPeerInfo" - } - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "put": { - "summary": "Add a new peer to the daemon. A session for the specified peer will start", - "description": "immediately.", - "operationId": "add_bfd_peer", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/BfdPeerConfig" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bfd/peers/{addr}": { - "delete": { - "summary": "Remove the specified peer from the daemon. The associated peer session will", - "description": "be stopped immediately.", - "operationId": "remove_bfd_peer", - "parameters": [ - { - "in": "path", - "name": "addr", - "description": "Address of the peer to remove.", - "required": true, - "schema": { - "type": "string", - "format": "ip" - } - } - ], - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/clear/neighbor": { - "post": { - "operationId": "clear_neighbor_v2", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/NeighborResetRequest" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/clear/unnumbered-neighbor": { - "post": { - "operationId": "clear_unnumbered_neighbor", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/UnnumberedNeighborResetRequest" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/config/checker": { - "get": { - "operationId": "read_checker", - "parameters": [ - { - "in": "query", - "name": "asn", - "description": "ASN of the router to get imported prefixes from.", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/CheckerSource" - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "put": { - "operationId": "create_checker", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/CheckerSource" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "post": { - "operationId": "update_checker", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/CheckerSource" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "delete": { - "operationId": "delete_checker", - "parameters": [ - { - "in": "query", - "name": "asn", - "description": "ASN of the router to get imported prefixes from.", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - } - ], - "responses": { - "204": { - "description": "successful deletion" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/config/neighbor": { - "put": { - "operationId": "create_neighbor_v3", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Neighbor" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "post": { - "operationId": "update_neighbor_v3", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Neighbor" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/config/neighbor/{asn}/{peer}": { - "get": { - "operationId": "read_neighbor_v3", - "parameters": [ - { - "in": "path", - "name": "asn", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - { - "in": "path", - "name": "peer", - "description": "Peer identifier as a string.\n\n- For numbered peers: IP address (e.g., \"192.0.2.1\" or \"2001:db8::1\") - For unnumbered peers: Interface name (e.g., \"eth0\" or \"cxgbe0\")\n\nServer parses as IP address first; if parsing fails, treats as interface name. Uses PeerId::from_str() for type-safe conversion.", - "required": true, - "schema": { - "type": "string" - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Neighbor" - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "delete": { - "operationId": "delete_neighbor_v3", - "parameters": [ - { - "in": "path", - "name": "asn", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - { - "in": "path", - "name": "peer", - "description": "Peer identifier as a string.\n\n- For numbered peers: IP address (e.g., \"192.0.2.1\" or \"2001:db8::1\") - For unnumbered peers: Interface name (e.g., \"eth0\" or \"cxgbe0\")\n\nServer parses as IP address first; if parsing fails, treats as interface name. Uses PeerId::from_str() for type-safe conversion.", - "required": true, - "schema": { - "type": "string" - } - } - ], - "responses": { - "204": { - "description": "successful deletion" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/config/neighbors/{asn}": { - "get": { - "operationId": "read_neighbors_v3", - "parameters": [ - { - "in": "path", - "name": "asn", - "description": "ASN of the router to get imported prefixes from.", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "title": "Array_of_Neighbor", - "type": "array", - "items": { - "$ref": "#/components/schemas/Neighbor" - } - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/config/origin4": { - "get": { - "operationId": "read_origin4", - "parameters": [ - { - "in": "query", - "name": "asn", - "description": "ASN of the router to get imported prefixes from.", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Origin4" - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "put": { - "operationId": "create_origin4", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Origin4" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "post": { - "operationId": "update_origin4", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Origin4" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "delete": { - "operationId": "delete_origin4", - "parameters": [ - { - "in": "query", - "name": "asn", - "description": "ASN of the router to get imported prefixes from.", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - } - ], - "responses": { - "204": { - "description": "successful deletion" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/config/origin6": { - "get": { - "operationId": "read_origin6", - "parameters": [ - { - "in": "query", - "name": "asn", - "description": "ASN of the router to get imported prefixes from.", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Origin6" - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "put": { - "operationId": "create_origin6", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Origin6" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "post": { - "operationId": "update_origin6", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Origin6" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "delete": { - "operationId": "delete_origin6", - "parameters": [ - { - "in": "query", - "name": "asn", - "description": "ASN of the router to get imported prefixes from.", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - } - ], - "responses": { - "204": { - "description": "successful deletion" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/config/router": { - "get": { - "operationId": "read_router", - "parameters": [ - { - "in": "query", - "name": "asn", - "description": "ASN of the router to get imported prefixes from.", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Router" - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "put": { - "operationId": "create_router", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Router" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "post": { - "operationId": "update_router", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Router" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "delete": { - "operationId": "delete_router", - "parameters": [ - { - "in": "query", - "name": "asn", - "description": "ASN of the router to get imported prefixes from.", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - } - ], - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/config/routers": { - "get": { - "operationId": "read_routers", - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "title": "Array_of_Router", - "type": "array", - "items": { - "$ref": "#/components/schemas/Router" - } - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/config/shaper": { - "get": { - "operationId": "read_shaper", - "parameters": [ - { - "in": "query", - "name": "asn", - "description": "ASN of the router to get imported prefixes from.", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/ShaperSource" - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "put": { - "operationId": "create_shaper", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/ShaperSource" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "post": { - "operationId": "update_shaper", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/ShaperSource" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "delete": { - "operationId": "delete_shaper", - "parameters": [ - { - "in": "query", - "name": "asn", - "description": "ASN of the router to get imported prefixes from.", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - } - ], - "responses": { - "204": { - "description": "successful deletion" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/config/unnumbered-neighbor": { - "get": { - "operationId": "read_unnumbered_neighbor", - "parameters": [ - { - "in": "query", - "name": "asn", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - { - "in": "query", - "name": "interface", - "required": true, - "schema": { - "type": "string" - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/UnnumberedNeighbor" - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "put": { - "operationId": "create_unnumbered_neighbor", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/UnnumberedNeighbor" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "post": { - "operationId": "update_unnumbered_neighbor", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/UnnumberedNeighbor" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "delete": { - "operationId": "delete_unnumbered_neighbor", - "parameters": [ - { - "in": "query", - "name": "asn", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - { - "in": "query", - "name": "interface", - "required": true, - "schema": { - "type": "string" - } - } - ], - "responses": { - "204": { - "description": "successful deletion" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/config/unnumbered-neighbors": { - "get": { - "operationId": "read_unnumbered_neighbors", - "parameters": [ - { - "in": "query", - "name": "asn", - "description": "ASN of the router to get imported prefixes from.", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "title": "Array_of_UnnumberedNeighbor", - "type": "array", - "items": { - "$ref": "#/components/schemas/UnnumberedNeighbor" - } - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/history/fsm": { - "get": { - "operationId": "fsm_history_v2", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/FsmHistoryRequest" - } - } - }, - "required": true - }, - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/FsmHistoryResponse" - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/history/message": { - "get": { - "operationId": "message_history_v3", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/MessageHistoryRequest" - } - } - }, - "required": true - }, - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/MessageHistoryResponse" - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/omicron/apply": { - "post": { - "operationId": "bgp_apply_v2", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/ApplyRequest" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/status/exported": { - "get": { - "operationId": "get_exported_v3", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/ExportedSelector" - } - } - }, - "required": true - }, - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "title": "Map_of_Array_of_Prefix", - "type": "object", - "additionalProperties": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Prefix" - } - } - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/bgp/status/neighbors": { - "get": { - "operationId": "get_neighbors_v4", - "parameters": [ - { - "in": "query", - "name": "asn", - "description": "ASN of the router to get imported prefixes from.", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "title": "Map_of_PeerInfo", - "type": "object", - "additionalProperties": { - "$ref": "#/components/schemas/PeerInfo" - } - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/ndp/interface": { - "get": { - "operationId": "get_ndp_interface_detail", - "parameters": [ - { - "in": "query", - "name": "asn", - "description": "ASN of the router", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - { - "in": "query", - "name": "interface", - "description": "Interface name", - "required": true, - "schema": { - "type": "string" - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/NdpInterface" - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/ndp/interfaces": { - "get": { - "operationId": "get_ndp_interfaces", - "parameters": [ - { - "in": "query", - "name": "asn", - "description": "ASN of the router to get imported prefixes from.", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "title": "Array_of_NdpInterface", - "type": "array", - "items": { - "$ref": "#/components/schemas/NdpInterface" - } - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/ndp/manager": { - "get": { - "operationId": "get_ndp_manager_state", - "parameters": [ - { - "in": "query", - "name": "asn", - "description": "ASN of the router to get imported prefixes from.", - "required": true, - "schema": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/NdpManagerState" - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/rib/config/bestpath/fanout": { - "get": { - "operationId": "read_rib_bestpath_fanout", - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/BestpathFanoutResponse" - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "post": { - "operationId": "update_rib_bestpath_fanout", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/BestpathFanoutRequest" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/rib/status/imported": { - "get": { - "operationId": "get_rib_imported_v2", - "parameters": [ - { - "in": "query", - "name": "address_family", - "description": "Filter by address family (None means all families)", - "schema": { - "$ref": "#/components/schemas/AddressFamily" - } - }, - { - "in": "query", - "name": "protocol", - "description": "Filter by protocol (optional)", - "schema": { - "$ref": "#/components/schemas/ProtocolFilter" - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Rib" - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/rib/status/selected": { - "get": { - "operationId": "get_rib_selected_v2", - "parameters": [ - { - "in": "query", - "name": "address_family", - "description": "Filter by address family (None means all families)", - "schema": { - "$ref": "#/components/schemas/AddressFamily" - } - }, - { - "in": "query", - "name": "protocol", - "description": "Filter by protocol (optional)", - "schema": { - "$ref": "#/components/schemas/ProtocolFilter" - } - } - ], - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Rib" - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/static/route4": { - "get": { - "operationId": "static_list_v4_routes", - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "title": "Map_of_Set_of_Path", - "type": "object", - "additionalProperties": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Path" - }, - "uniqueItems": true - } - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "put": { - "operationId": "static_add_v4_route", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/AddStaticRoute4Request" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "delete": { - "operationId": "static_remove_v4_route", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DeleteStaticRoute4Request" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "successful deletion" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/static/route6": { - "get": { - "operationId": "static_list_v6_routes", - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "title": "Map_of_Set_of_Path", - "type": "object", - "additionalProperties": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Path" - }, - "uniqueItems": true - } - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "put": { - "operationId": "static_add_v6_route", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/AddStaticRoute6Request" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "resource updated" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - }, - "delete": { - "operationId": "static_remove_v6_route", - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/DeleteStaticRoute6Request" - } - } - }, - "required": true - }, - "responses": { - "204": { - "description": "successful deletion" - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - }, - "/switch/identifiers": { - "get": { - "operationId": "switch_identifiers", - "responses": { - "200": { - "description": "successful operation", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/SwitchIdentifiers" - } - } - } - }, - "4XX": { - "$ref": "#/components/responses/Error" - }, - "5XX": { - "$ref": "#/components/responses/Error" - } - } - } - } - }, - "components": { - "schemas": { - "AddPathElement": { - "description": "The add path element comes as a BGP capability extension as described in RFC 7911.", - "type": "object", - "properties": { - "afi": { - "description": "Address family identifier. ", - "type": "integer", - "format": "uint16", - "minimum": 0 - }, - "safi": { - "description": "Subsequent address family identifier. There are a large pile of these ", - "type": "integer", - "format": "uint8", - "minimum": 0 - }, - "send_receive": { - "description": "This field indicates whether the sender is (a) able to receive multiple paths from its peer (value 1), (b) able to send multiple paths to its peer (value 2), or (c) both (value 3) for the .", - "type": "integer", - "format": "uint8", - "minimum": 0 - } - }, - "required": [ - "afi", - "safi", - "send_receive" - ] - }, - "AddStaticRoute4Request": { - "type": "object", - "properties": { - "routes": { - "$ref": "#/components/schemas/StaticRoute4List" - } - }, - "required": [ - "routes" - ] - }, - "AddStaticRoute6Request": { - "type": "object", - "properties": { - "routes": { - "$ref": "#/components/schemas/StaticRoute6List" - } - }, - "required": [ - "routes" - ] - }, - "Afi": { - "description": "Address families supported by Maghemite BGP.", - "oneOf": [ - { - "description": "Internet protocol version 4", - "type": "string", - "enum": [ - "Ipv4" - ] - }, - { - "description": "Internet protocol version 6", - "type": "string", - "enum": [ - "Ipv6" - ] - } - ] - }, - "AfiSafi": { - "type": "object", - "properties": { - "afi": { - "type": "integer", - "format": "uint16", - "minimum": 0 - }, - "safi": { - "type": "integer", - "format": "uint8", - "minimum": 0 - } - }, - "required": [ - "afi", - "safi" - ] - }, - "Aggregator": { - "description": "AGGREGATOR path attribute (RFC 4271 §5.1.8)\n\nThe AGGREGATOR attribute is an optional transitive attribute that contains the AS number and IP address of the last BGP speaker that formed the aggregate route.", - "type": "object", - "properties": { - "address": { - "description": "IP address of the BGP speaker that formed the aggregate", - "type": "string", - "format": "ipv4" - }, - "asn": { - "description": "Autonomous System Number that formed the aggregate (2-octet)", - "type": "integer", - "format": "uint16", - "minimum": 0 - } - }, - "required": [ - "address", - "asn" - ] - }, - "ApplyRequest": { - "description": "Apply changes to an ASN (current version with per-AF policies).", - "type": "object", - "properties": { - "asn": { - "description": "ASN to apply changes to.", - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "checker": { - "nullable": true, - "description": "Checker rhai code to apply to ingress open and update messages.", - "allOf": [ - { - "$ref": "#/components/schemas/CheckerSource" - } - ] - }, - "originate": { - "description": "Complete set of prefixes to originate.", - "type": "array", - "items": { - "$ref": "#/components/schemas/Prefix" - } - }, - "peers": { - "description": "Lists of peers indexed by peer group.", - "type": "object", - "additionalProperties": { - "type": "array", - "items": { - "$ref": "#/components/schemas/BgpPeerConfig" - } - } - }, - "shaper": { - "nullable": true, - "description": "Checker rhai code to apply to egress open and update messages.", - "allOf": [ - { - "$ref": "#/components/schemas/ShaperSource" - } - ] - }, - "unnumbered_peers": { - "description": "Lists of unnumbered peers indexed by peer group.", - "default": {}, - "type": "object", - "additionalProperties": { - "type": "array", - "items": { - "$ref": "#/components/schemas/UnnumberedBgpPeerConfig" - } - } - } - }, - "required": [ - "asn", - "originate", - "peers" - ] - }, - "As4Aggregator": { - "description": "AS4_AGGREGATOR path attribute (RFC 6793)\n\nThe AS4_AGGREGATOR attribute is an optional transitive attribute with the same semantics as AGGREGATOR, but carries a 4-octet AS number instead of 2-octet.", - "type": "object", - "properties": { - "address": { - "description": "IP address of the BGP speaker that formed the aggregate", - "type": "string", - "format": "ipv4" - }, - "asn": { - "description": "Autonomous System Number that formed the aggregate (4-octet)", - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - "required": [ - "address", - "asn" - ] - }, - "As4PathSegment": { - "type": "object", - "properties": { - "typ": { - "$ref": "#/components/schemas/AsPathType" - }, - "value": { - "type": "array", - "items": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - } - }, - "required": [ - "typ", - "value" - ] - }, - "AsPathType": { - "description": "Enumeration describes possible AS path types", - "oneOf": [ - { - "description": "The path is to be interpreted as a set", - "type": "string", - "enum": [ - "as_set" - ] - }, - { - "description": "The path is to be interpreted as a sequence", - "type": "string", - "enum": [ - "as_sequence" - ] - } - ] - }, - "BestpathFanoutRequest": { - "type": "object", - "properties": { - "fanout": { - "description": "Maximum number of equal-cost paths for ECMP forwarding", - "type": "integer", - "format": "uint8", - "minimum": 1 - } - }, - "required": [ - "fanout" - ] - }, - "BestpathFanoutResponse": { - "type": "object", - "properties": { - "fanout": { - "description": "Current maximum number of equal-cost paths for ECMP forwarding", - "type": "integer", - "format": "uint8", - "minimum": 1 - } - }, - "required": [ - "fanout" - ] - }, - "BfdPeerConfig": { - "type": "object", - "properties": { - "detection_threshold": { - "description": "Detection threshold for connectivity as a multipler to required_rx", - "type": "integer", - "format": "uint8", - "minimum": 0 - }, - "listen": { - "description": "Address to listen on for control messages from the peer.", - "type": "string", - "format": "ip" - }, - "mode": { - "description": "Mode is single-hop (RFC 5881) or multi-hop (RFC 5883).", - "allOf": [ - { - "$ref": "#/components/schemas/SessionMode" - } - ] - }, - "peer": { - "description": "Address of the peer to add.", - "type": "string", - "format": "ip" - }, - "required_rx": { - "description": "Acceptable time between control messages in microseconds.", - "type": "integer", - "format": "uint64", - "minimum": 0 - } - }, - "required": [ - "detection_threshold", - "listen", - "mode", - "peer", - "required_rx" - ] - }, - "BfdPeerInfo": { - "type": "object", - "properties": { - "config": { - "$ref": "#/components/schemas/BfdPeerConfig" - }, - "state": { - "$ref": "#/components/schemas/BfdPeerState" - } - }, - "required": [ - "config", - "state" - ] - }, - "BfdPeerState": { - "description": "The possible peer states. See the `State` trait implementations `Down`, `Init`, and `Up` for detailed semantics. Data representation is u8 as this enum is used as a part of the BFD wire protocol.", - "oneOf": [ - { - "description": "A stable down state. Non-responsive to incoming messages.", - "type": "string", - "enum": [ - "AdminDown" - ] - }, - { - "description": "The initial state.", - "type": "string", - "enum": [ - "Down" - ] - }, - { - "description": "The peer has detected a remote peer in the down state.", - "type": "string", - "enum": [ - "Init" - ] - }, - { - "description": "The peer has detected a remote peer in the up or init state while in the init state.", - "type": "string", - "enum": [ - "Up" - ] - } - ] - }, - "BgpCapability": { - "oneOf": [ - { - "type": "string", - "enum": [ - "RouteRefresh" - ] - }, - { - "type": "object", - "properties": { - "MultiprotocolExtensions": { - "$ref": "#/components/schemas/AfiSafi" - } - }, - "required": [ - "MultiprotocolExtensions" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "FourOctetAsn": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - "required": [ - "FourOctetAsn" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "AddPath": { - "type": "object", - "properties": { - "elements": { - "type": "array", - "items": { - "$ref": "#/components/schemas/AfiSafi" - } - } - }, - "required": [ - "elements" - ] - } - }, - "required": [ - "AddPath" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "Unknown": { - "type": "integer", - "format": "uint8", - "minimum": 0 - } - }, - "required": [ - "Unknown" - ], - "additionalProperties": false - } - ] - }, - "BgpNexthop": { - "description": "A BGP next-hop address in one of three formats: IPv4, IPv6 single, or IPv6 double.", - "oneOf": [ - { - "type": "object", - "properties": { - "Ipv4": { - "type": "string", - "format": "ipv4" - } - }, - "required": [ - "Ipv4" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "Ipv6Single": { - "type": "string", - "format": "ipv6" - } - }, - "required": [ - "Ipv6Single" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "Ipv6Double": { - "$ref": "#/components/schemas/Ipv6DoubleNexthop" - } - }, - "required": [ - "Ipv6Double" - ], - "additionalProperties": false - } - ] - }, - "BgpPathProperties": { - "type": "object", - "properties": { - "as_path": { - "type": "array", - "items": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - "id": { - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "local_pref": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "med": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "origin_as": { - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "peer": { - "$ref": "#/components/schemas/PeerId" - }, - "stale": { - "nullable": true, - "type": "string", - "format": "date-time" - } - }, - "required": [ - "as_path", - "id", - "origin_as", - "peer" - ] - }, - "BgpPeerConfig": { - "type": "object", - "properties": { - "communities": { - "type": "array", - "items": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - "connect_retry": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "connect_retry_jitter": { - "nullable": true, - "description": "Jitter range for connect_retry timer. When used, the connect_retry timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", - "allOf": [ - { - "$ref": "#/components/schemas/JitterRange" - } - ] - }, - "delay_open": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "deterministic_collision_resolution": { - "description": "Enable deterministic collision resolution in Established state. When true, uses BGP-ID comparison per RFC 4271 §6.8 for collision resolution even when one connection is already in Established state. When false, Established connection always wins (timing-based resolution).", - "type": "boolean" - }, - "enforce_first_as": { - "type": "boolean" - }, - "hold_time": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "host": { - "type": "string" - }, - "idle_hold_jitter": { - "nullable": true, - "description": "Jitter range for idle hold timer. When used, the idle hold timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", - "allOf": [ - { - "$ref": "#/components/schemas/JitterRange" - } - ] - }, - "idle_hold_time": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "ipv4_unicast": { - "nullable": true, - "description": "IPv4 Unicast address family configuration (None = disabled)", - "allOf": [ - { - "$ref": "#/components/schemas/Ipv4UnicastConfig" - } - ] - }, - "ipv6_unicast": { - "nullable": true, - "description": "IPv6 Unicast address family configuration (None = disabled)", - "allOf": [ - { - "$ref": "#/components/schemas/Ipv6UnicastConfig" - } - ] - }, - "keepalive": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "local_pref": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "md5_auth_key": { - "nullable": true, - "type": "string" - }, - "min_ttl": { - "nullable": true, - "type": "integer", - "format": "uint8", - "minimum": 0 - }, - "multi_exit_discriminator": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "name": { - "type": "string" - }, - "passive": { - "type": "boolean" - }, - "remote_asn": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "resolution": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "vlan_id": { - "nullable": true, - "type": "integer", - "format": "uint16", - "minimum": 0 - } - }, - "required": [ - "communities", - "connect_retry", - "delay_open", - "deterministic_collision_resolution", - "enforce_first_as", - "hold_time", - "host", - "idle_hold_time", - "keepalive", - "name", - "passive", - "resolution" - ] - }, - "Capability": { - "description": "Optional capabilities supported by a BGP implementation.", - "oneOf": [ - { - "description": "Multiprotocol extensions as defined in RFC 2858", - "type": "object", - "properties": { - "multiprotocol_extensions": { - "type": "object", - "properties": { - "afi": { - "type": "integer", - "format": "uint16", - "minimum": 0 - }, - "safi": { - "type": "integer", - "format": "uint8", - "minimum": 0 - } - }, - "required": [ - "afi", - "safi" - ] - } - }, - "required": [ - "multiprotocol_extensions" - ], - "additionalProperties": false - }, - { - "description": "Route refresh capability as defined in RFC 2918.", - "type": "object", - "properties": { - "route_refresh": { - "type": "object" - } - }, - "required": [ - "route_refresh" - ], - "additionalProperties": false - }, - { - "description": "Outbound filtering capability as defined in RFC 5291. Note this capability is not yet implemented.", - "type": "object", - "properties": { - "outbound_route_filtering": { - "type": "object" - } - }, - "required": [ - "outbound_route_filtering" - ], - "additionalProperties": false - }, - { - "description": "Multiple routes to destination capability as defined in RFC 8277 (deprecated). Note this capability is not yet implemented.", - "type": "object", - "properties": { - "multiple_routes_to_destination": { - "type": "object" - } - }, - "required": [ - "multiple_routes_to_destination" - ], - "additionalProperties": false - }, - { - "description": "Multiple nexthop encoding capability as defined in RFC 8950.", - "type": "object", - "properties": { - "extended_next_hop_encoding": { - "type": "object" - } - }, - "required": [ - "extended_next_hop_encoding" - ], - "additionalProperties": false - }, - { - "description": "Extended message capability as defined in RFC 8654. Note this capability is not yet implemented.", - "type": "object", - "properties": { - "b_g_p_extended_message": { - "type": "object" - } - }, - "required": [ - "b_g_p_extended_message" - ], - "additionalProperties": false - }, - { - "description": "BGPSec as defined in RFC 8205. Note this capability is not yet implemented.", - "type": "object", - "properties": { - "bgp_sec": { - "type": "object" - } - }, - "required": [ - "bgp_sec" - ], - "additionalProperties": false - }, - { - "description": "Multiple label support as defined in RFC 8277. Note this capability is not yet implemented.", - "type": "object", - "properties": { - "multiple_labels": { - "type": "object" - } - }, - "required": [ - "multiple_labels" - ], - "additionalProperties": false - }, - { - "description": "BGP role capability as defined in RFC 9234. Note this capability is not yet implemented.", - "type": "object", - "properties": { - "bgp_role": { - "type": "object" - } - }, - "required": [ - "bgp_role" - ], - "additionalProperties": false - }, - { - "description": "Graceful restart as defined in RFC 4724. Note this capability is not yet implemented.", - "type": "object", - "properties": { - "graceful_restart": { - "type": "object" - } - }, - "required": [ - "graceful_restart" - ], - "additionalProperties": false - }, - { - "description": "Four octet AS numbers as defined in RFC 6793.", - "type": "object", - "properties": { - "four_octet_as": { - "type": "object", - "properties": { - "asn": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - "required": [ - "asn" - ] - } - }, - "required": [ - "four_octet_as" - ], - "additionalProperties": false - }, - { - "description": "Dynamic capabilities as defined in draft-ietf-idr-dynamic-cap. Note this capability is not yet implemented.", - "type": "object", - "properties": { - "dynamic_capability": { - "type": "object" - } - }, - "required": [ - "dynamic_capability" - ], - "additionalProperties": false - }, - { - "description": "Multi session support as defined in draft-ietf-idr-bgp-multisession. Note this capability is not yet supported.", - "type": "object", - "properties": { - "multisession_bgp": { - "type": "object" - } - }, - "required": [ - "multisession_bgp" - ], - "additionalProperties": false - }, - { - "description": "Add path capability as defined in RFC 7911.", - "type": "object", - "properties": { - "add_path": { - "type": "object", - "properties": { - "elements": { - "type": "array", - "items": { - "$ref": "#/components/schemas/AddPathElement" - }, - "uniqueItems": true - } - }, - "required": [ - "elements" - ] - } - }, - "required": [ - "add_path" - ], - "additionalProperties": false - }, - { - "description": "Enhanced route refresh as defined in RFC 7313. Note this capability is not yet supported.", - "type": "object", - "properties": { - "enhanced_route_refresh": { - "type": "object" - } - }, - "required": [ - "enhanced_route_refresh" - ], - "additionalProperties": false - }, - { - "description": "Long-lived graceful restart as defined in draft-uttaro-idr-bgp-persistence. Note this capability is not yet supported.", - "type": "object", - "properties": { - "long_lived_graceful_restart": { - "type": "object" - } - }, - "required": [ - "long_lived_graceful_restart" - ], - "additionalProperties": false - }, - { - "description": "Routing policy distribution as defined indraft-ietf-idr-rpd-04. Note this capability is not yet supported.", - "type": "object", - "properties": { - "routing_policy_distribution": { - "type": "object" - } - }, - "required": [ - "routing_policy_distribution" - ], - "additionalProperties": false - }, - { - "description": "Fully qualified domain names as defined intdraft-walton-bgp-hostname-capability. Note this capability is not yet supported.", - "type": "object", - "properties": { - "fqdn": { - "type": "object" - } - }, - "required": [ - "fqdn" - ], - "additionalProperties": false - }, - { - "description": "Pre-standard route refresh as defined in RFC 8810 (deprecated). Note this capability is not yet supported.", - "type": "object", - "properties": { - "prestandard_route_refresh": { - "type": "object" - } - }, - "required": [ - "prestandard_route_refresh" - ], - "additionalProperties": false - }, - { - "description": "Pre-standard prefix-based outbound route filtering as defined in RFC 8810 (deprecated). Note this is not yet implemented.", - "type": "object", - "properties": { - "prestandard_orf_and_pd": { - "type": "object" - } - }, - "required": [ - "prestandard_orf_and_pd" - ], - "additionalProperties": false - }, - { - "description": "Pre-standard outbound route filtering as defined in RFC 8810 (deprecated). Note this is not yet implemented.", - "type": "object", - "properties": { - "prestandard_outbound_route_filtering": { - "type": "object" - } - }, - "required": [ - "prestandard_outbound_route_filtering" - ], - "additionalProperties": false - }, - { - "description": "Pre-standard multisession as defined in RFC 8810 (deprecated). Note this is not yet implemented.", - "type": "object", - "properties": { - "prestandard_multisession": { - "type": "object" - } - }, - "required": [ - "prestandard_multisession" - ], - "additionalProperties": false - }, - { - "description": "Pre-standard fully qualified domain names as defined in RFC 8810 (deprecated). Note this is not yet implemented.", - "type": "object", - "properties": { - "prestandard_fqdn": { - "type": "object" - } - }, - "required": [ - "prestandard_fqdn" - ], - "additionalProperties": false - }, - { - "description": "Pre-standard operational messages as defined in RFC 8810 (deprecated). Note this is not yet implemented.", - "type": "object", - "properties": { - "prestandard_operational_message": { - "type": "object" - } - }, - "required": [ - "prestandard_operational_message" - ], - "additionalProperties": false - }, - { - "description": "Experimental capability as defined in RFC 8810.", - "type": "object", - "properties": { - "experimental": { - "type": "object", - "properties": { - "code": { - "type": "integer", - "format": "uint8", - "minimum": 0 - } - }, - "required": [ - "code" - ] - } - }, - "required": [ - "experimental" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "unassigned": { - "type": "object", - "properties": { - "code": { - "type": "integer", - "format": "uint8", - "minimum": 0 - } - }, - "required": [ - "code" - ] - } - }, - "required": [ - "unassigned" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "reserved": { - "type": "object", - "properties": { - "code": { - "type": "integer", - "format": "uint8", - "minimum": 0 - } - }, - "required": [ - "code" - ] - } - }, - "required": [ - "reserved" - ], - "additionalProperties": false - } - ] - }, - "CeaseErrorSubcode": { - "description": "Cease error subcode types from RFC 4486", - "type": "string", - "enum": [ - "unspecific", - "maximum_numberof_prefixes_reached", - "administrative_shutdown", - "peer_deconfigured", - "administrative_reset", - "connection_rejected", - "other_configuration_change", - "connection_collision_resolution", - "out_of_resources" - ] - }, - "CheckerSource": { - "type": "object", - "properties": { - "asn": { - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "code": { - "type": "string" - } - }, - "required": [ - "asn", - "code" - ] - }, - "Community": { - "description": "BGP community value", - "oneOf": [ - { - "description": "All routes received carrying a communities attribute containing this value MUST NOT be advertised outside a BGP confederation boundary (a stand-alone autonomous system that is not part of a confederation should be considered a confederation itself)", - "type": "string", - "enum": [ - "no_export" - ] - }, - { - "description": "All routes received carrying a communities attribute containing this value MUST NOT be advertised to other BGP peers.", - "type": "string", - "enum": [ - "no_advertise" - ] - }, - { - "description": "All routes received carrying a communities attribute containing this value MUST NOT be advertised to external BGP peers (this includes peers in other members autonomous systems inside a BGP confederation).", - "type": "string", - "enum": [ - "no_export_sub_confed" - ] - }, - { - "description": "All routes received carrying a communities attribute containing this value must set the local preference for the received routes to a low value, preferably zero.", - "type": "string", - "enum": [ - "graceful_shutdown" - ] - }, - { - "description": "A user defined community", - "type": "object", - "properties": { - "user_defined": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - "required": [ - "user_defined" - ], - "additionalProperties": false - } - ] - }, - "ConnectionId": { - "description": "Unique identifier for a BGP connection instance", - "type": "object", - "properties": { - "local": { - "description": "Local socket address for this connection", - "type": "string" - }, - "remote": { - "description": "Remote socket address for this connection", - "type": "string" - }, - "uuid": { - "description": "Unique identifier for this connection instance", - "type": "string", - "format": "uuid" - } - }, - "required": [ - "local", - "remote", - "uuid" - ] - }, - "DeleteStaticRoute4Request": { - "type": "object", - "properties": { - "routes": { - "$ref": "#/components/schemas/StaticRoute4List" - } - }, - "required": [ - "routes" - ] - }, - "DeleteStaticRoute6Request": { - "type": "object", - "properties": { - "routes": { - "$ref": "#/components/schemas/StaticRoute6List" - } - }, - "required": [ - "routes" - ] - }, - "Duration": { - "type": "object", - "properties": { - "nanos": { - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "secs": { - "type": "integer", - "format": "uint64", - "minimum": 0 - } - }, - "required": [ - "nanos", - "secs" - ] - }, - "DynamicTimerInfo": { - "type": "object", - "properties": { - "configured": { - "$ref": "#/components/schemas/Duration" - }, - "negotiated": { - "$ref": "#/components/schemas/Duration" - }, - "remaining": { - "$ref": "#/components/schemas/Duration" - } - }, - "required": [ - "configured", - "negotiated", - "remaining" - ] - }, - "Error": { - "description": "Error information from a response.", - "type": "object", - "properties": { - "error_code": { - "type": "string" - }, - "message": { - "type": "string" - }, - "request_id": { - "type": "string" - } - }, - "required": [ - "message", - "request_id" - ] - }, - "ErrorCode": { - "description": "This enumeration contains possible notification error codes.", - "type": "string", - "enum": [ - "header", - "open", - "update", - "hold_timer_expired", - "fsm", - "cease" - ] - }, - "ErrorSubcode": { - "description": "This enumeration contains possible notification error subcodes.", - "oneOf": [ - { - "type": "object", - "properties": { - "header": { - "$ref": "#/components/schemas/HeaderErrorSubcode" - } - }, - "required": [ - "header" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "open": { - "$ref": "#/components/schemas/OpenErrorSubcode" - } - }, - "required": [ - "open" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "update": { - "$ref": "#/components/schemas/UpdateErrorSubcode" - } - }, - "required": [ - "update" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "hold_time": { - "type": "integer", - "format": "uint8", - "minimum": 0 - } - }, - "required": [ - "hold_time" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "fsm": { - "type": "integer", - "format": "uint8", - "minimum": 0 - } - }, - "required": [ - "fsm" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "cease": { - "$ref": "#/components/schemas/CeaseErrorSubcode" - } - }, - "required": [ - "cease" - ], - "additionalProperties": false - } - ] - }, - "ExportedSelector": { - "type": "object", - "properties": { - "afi": { - "nullable": true, - "description": "Optional address family filter (None = all negotiated families)", - "allOf": [ - { - "$ref": "#/components/schemas/Afi" - } - ] - }, - "asn": { - "description": "ASN of the router to get exported prefixes from.", - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "peer": { - "nullable": true, - "description": "Optional peer filter using PeerId enum", - "allOf": [ - { - "$ref": "#/components/schemas/PeerId" - } - ] - } - }, - "required": [ - "asn" - ] - }, - "FsmEventBuffer": { - "oneOf": [ - { - "description": "All FSM events (high frequency, includes all timers)", - "type": "string", - "enum": [ - "all" - ] - }, - { - "description": "Major events only (state transitions, admin, new connections)", - "type": "string", - "enum": [ - "major" - ] - } - ] - }, - "FsmEventCategory": { - "description": "Category of FSM event for filtering and display purposes", - "type": "string", - "enum": [ - "Admin", - "Connection", - "Session", - "StateTransition" - ] - }, - "FsmEventRecord": { - "description": "Serializable record of an FSM event with full context", - "type": "object", - "properties": { - "connection_id": { - "nullable": true, - "description": "Connection ID if event is connection-specific", - "allOf": [ - { - "$ref": "#/components/schemas/ConnectionId" - } - ] - }, - "current_state": { - "description": "FSM state at time of event", - "allOf": [ - { - "$ref": "#/components/schemas/FsmStateKind" - } - ] - }, - "details": { - "nullable": true, - "description": "Additional event details (e.g., \"Received OPEN\", \"Admin command\")", - "type": "string" - }, - "event_category": { - "description": "High-level event category", - "allOf": [ - { - "$ref": "#/components/schemas/FsmEventCategory" - } - ] - }, - "event_type": { - "description": "Specific event type as string (e.g., \"ManualStart\", \"HoldTimerExpires\")", - "type": "string" - }, - "previous_state": { - "nullable": true, - "description": "Previous state if this caused a transition", - "allOf": [ - { - "$ref": "#/components/schemas/FsmStateKind" - } - ] - }, - "timestamp": { - "description": "UTC timestamp when event occurred", - "type": "string", - "format": "date-time" - } - }, - "required": [ - "current_state", - "event_category", - "event_type", - "timestamp" - ] - }, - "FsmHistoryRequest": { - "description": "Unified FSM history request supporting both numbered and unnumbered peers", - "type": "object", - "properties": { - "asn": { - "description": "ASN of the BGP router", - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "buffer": { - "nullable": true, - "description": "Which buffer to retrieve - if None, returns major buffer", - "allOf": [ - { - "$ref": "#/components/schemas/FsmEventBuffer" - } - ] - }, - "peer": { - "nullable": true, - "description": "Optional peer filter using PeerId enum JSON format: {\"ip\": \"192.0.2.1\"} or {\"interface\": \"eth0\"}", - "allOf": [ - { - "$ref": "#/components/schemas/PeerId" - } - ] - } - }, - "required": [ - "asn" - ] - }, - "FsmHistoryResponse": { - "description": "Unified FSM history response with string keys from PeerId Display Keys will be \"192.0.2.1\" or \"eth0\" format", - "type": "object", - "properties": { - "by_peer": { - "description": "Events organized by peer identifier Each peer's value contains only the events from the requested buffer", - "type": "object", - "additionalProperties": { - "type": "array", - "items": { - "$ref": "#/components/schemas/FsmEventRecord" - } - } - } - }, - "required": [ - "by_peer" - ] - }, - "FsmStateKind": { - "description": "Simplified representation of a BGP state without having to carry a connection.", - "oneOf": [ - { - "description": "Initial state. Refuse all incomming BGP connections. No resources allocated to peer.", - "type": "string", - "enum": [ - "Idle" - ] - }, - { - "description": "Waiting for the TCP connection to be completed.", - "type": "string", - "enum": [ - "Connect" - ] - }, - { - "description": "Trying to acquire peer by listening for and accepting a TCP connection.", - "type": "string", - "enum": [ - "Active" - ] - }, - { - "description": "Waiting for open message from peer.", - "type": "string", - "enum": [ - "OpenSent" - ] - }, - { - "description": "Waiting for keepalive or notification from peer.", - "type": "string", - "enum": [ - "OpenConfirm" - ] - }, - { - "description": "Handler for Connection Collisions (RFC 4271 6.8)", - "type": "string", - "enum": [ - "ConnectionCollision" - ] - }, - { - "description": "Sync up with peers.", - "type": "string", - "enum": [ - "SessionSetup" - ] - }, - { - "description": "Able to exchange update, notification and keepliave messages with peers.", - "type": "string", - "enum": [ - "Established" - ] - } - ] - }, - "HeaderErrorSubcode": { - "description": "Header error subcode types", - "type": "string", - "enum": [ - "unspecific", - "connection_not_synchronized", - "bad_message_length", - "bad_message_type" - ] - }, - "ImportExportPolicy4": { - "description": "Import/Export policy for IPv4 prefixes only.", - "oneOf": [ - { - "type": "string", - "enum": [ - "NoFiltering" - ] - }, - { - "type": "object", - "properties": { - "Allow": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Prefix4" - }, - "uniqueItems": true - } - }, - "required": [ - "Allow" - ], - "additionalProperties": false - } - ] - }, - "ImportExportPolicy6": { - "description": "Import/Export policy for IPv6 prefixes only.", - "oneOf": [ - { - "type": "string", - "enum": [ - "NoFiltering" - ] - }, - { - "type": "object", - "properties": { - "Allow": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Prefix6" - }, - "uniqueItems": true - } - }, - "required": [ - "Allow" - ], - "additionalProperties": false - } - ] - }, - "Ipv4UnicastConfig": { - "description": "Per-address-family configuration for IPv4 Unicast", - "type": "object", - "properties": { - "export_policy": { - "$ref": "#/components/schemas/ImportExportPolicy4" - }, - "import_policy": { - "$ref": "#/components/schemas/ImportExportPolicy4" - }, - "nexthop": { - "nullable": true, - "type": "string", - "format": "ip" - } - }, - "required": [ - "export_policy", - "import_policy" - ] - }, - "Ipv6DoubleNexthop": { - "description": "IPv6 double nexthop: global unicast address + link-local address. Per RFC 2545, when advertising IPv6 routes, both addresses may be present.", - "type": "object", - "properties": { - "global": { - "description": "Global unicast address", - "type": "string", - "format": "ipv6" - }, - "link_local": { - "description": "Link-local address", - "type": "string", - "format": "ipv6" - } - }, - "required": [ - "global", - "link_local" - ] - }, - "Ipv6UnicastConfig": { - "description": "Per-address-family configuration for IPv6 Unicast", - "type": "object", - "properties": { - "export_policy": { - "$ref": "#/components/schemas/ImportExportPolicy6" - }, - "import_policy": { - "$ref": "#/components/schemas/ImportExportPolicy6" - }, - "nexthop": { - "nullable": true, - "type": "string", - "format": "ip" - } - }, - "required": [ - "export_policy", - "import_policy" - ] - }, - "JitterRange": { - "description": "Jitter range with minimum and maximum multiplier values. When applied to a timer, the timer duration is multiplied by a random value within [min, max] to help break synchronization patterns.", - "type": "object", - "properties": { - "max": { - "description": "Maximum jitter multiplier (typically 1.0 or similar)", - "type": "number", - "format": "double" - }, - "min": { - "description": "Minimum jitter multiplier (typically 0.75 or similar)", - "type": "number", - "format": "double" - } - }, - "required": [ - "max", - "min" - ] - }, - "Message": { - "description": "Holds a BGP message. May be an Open, Update, Notification or Keep Alive message.", - "oneOf": [ - { - "type": "object", - "properties": { - "type": { - "type": "string", - "enum": [ - "open" - ] - }, - "value": { - "$ref": "#/components/schemas/OpenMessage" - } - }, - "required": [ - "type", - "value" - ] - }, - { - "type": "object", - "properties": { - "type": { - "type": "string", - "enum": [ - "update" - ] - }, - "value": { - "$ref": "#/components/schemas/UpdateMessage" - } - }, - "required": [ - "type", - "value" - ] - }, - { - "type": "object", - "properties": { - "type": { - "type": "string", - "enum": [ - "notification" - ] - }, - "value": { - "$ref": "#/components/schemas/NotificationMessage" - } - }, - "required": [ - "type", - "value" - ] - }, - { - "type": "object", - "properties": { - "type": { - "type": "string", - "enum": [ - "keep_alive" - ] - } - }, - "required": [ - "type" - ] - }, - { - "type": "object", - "properties": { - "type": { - "type": "string", - "enum": [ - "route_refresh" - ] - }, - "value": { - "$ref": "#/components/schemas/RouteRefreshMessage" - } - }, - "required": [ - "type", - "value" - ] - } - ] - }, - "MessageDirection": { - "type": "string", - "enum": [ - "sent", - "received" - ] - }, - "MessageHistory": { - "description": "Message history for a BGP session", - "type": "object", - "properties": { - "received": { - "type": "array", - "items": { - "$ref": "#/components/schemas/MessageHistoryEntry" - } - }, - "sent": { - "type": "array", - "items": { - "$ref": "#/components/schemas/MessageHistoryEntry" - } - } - }, - "required": [ - "received", - "sent" - ] - }, - "MessageHistoryEntry": { - "description": "A message history entry is a BGP message with an associated timestamp and connection ID", - "type": "object", - "properties": { - "connection_id": { - "$ref": "#/components/schemas/ConnectionId" - }, - "message": { - "$ref": "#/components/schemas/Message" - }, - "timestamp": { - "type": "string", - "format": "date-time" - } - }, - "required": [ - "connection_id", - "message", - "timestamp" - ] - }, - "MessageHistoryRequest": { - "description": "Unified message history request supporting both numbered and unnumbered peers", - "type": "object", - "properties": { - "asn": { - "description": "ASN of the BGP router", - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "direction": { - "nullable": true, - "description": "Optional direction filter - if None, returns both sent and received", - "allOf": [ - { - "$ref": "#/components/schemas/MessageDirection" - } - ] - }, - "peer": { - "nullable": true, - "description": "Optional peer filter using PeerId enum JSON format: {\"ip\": \"192.0.2.1\"} or {\"interface\": \"eth0\"}", - "allOf": [ - { - "$ref": "#/components/schemas/PeerId" - } - ] - } - }, - "required": [ - "asn" - ] - }, - "MessageHistoryResponse": { - "description": "Unified message history response with string keys from PeerId Display Keys will be \"192.0.2.1\" or \"eth0\" format", - "type": "object", - "properties": { - "by_peer": { - "type": "object", - "additionalProperties": { - "$ref": "#/components/schemas/MessageHistory" - } - } - }, - "required": [ - "by_peer" - ] - }, - "MpReachNlri": { - "description": "MP_REACH_NLRI path attribute\n\nEach variant represents a specific AFI+SAFI combination, providing compile-time guarantees about the address family of routes being announced.\n\n```text 3. Multiprotocol Reachable NLRI - MP_REACH_NLRI (Type Code 14):\n\nThis is an optional non-transitive attribute that can be used for the following purposes:\n\n(a) to advertise a feasible route to a peer\n\n(b) to permit a router to advertise the Network Layer address of the router that should be used as the next hop to the destinations listed in the Network Layer Reachability Information field of the MP_NLRI attribute.\n\nThe attribute is encoded as shown below:\n\n+---------------------------------------------------------+ | Address Family Identifier (2 octets) | +---------------------------------------------------------+ ```", - "oneOf": [ - { - "description": "IPv4 Unicast routes (AFI=1, SAFI=1)", - "type": "object", - "properties": { - "afi_safi": { - "type": "string", - "enum": [ - "ipv4_unicast" - ] - }, - "nexthop": { - "description": "Next-hop for IPv4 routes.\n\nCurrently must be `BgpNexthop::Ipv4`, but will support IPv6 nexthops when extended next-hop capability (RFC 8950) is implemented.", - "allOf": [ - { - "$ref": "#/components/schemas/BgpNexthop" - } - ] - }, - "nlri": { - "description": "IPv4 prefixes being announced", - "type": "array", - "items": { - "$ref": "#/components/schemas/Prefix4" - } - }, - "reserved": { - "description": "Reserved byte from RFC 4760 §3 (historically \"Number of SNPAs\" in RFC 2858). MUST be 0 per RFC 4760, but MUST be ignored by receiver. Stored for validation logging in session layer. This field is positioned before NLRI to match the wire format encoding.", - "type": "integer", - "format": "uint8", - "minimum": 0 - } - }, - "required": [ - "afi_safi", - "nexthop", - "nlri", - "reserved" - ] - }, - { - "description": "IPv6 Unicast routes (AFI=2, SAFI=1)", - "type": "object", - "properties": { - "afi_safi": { - "type": "string", - "enum": [ - "ipv6_unicast" - ] - }, - "nexthop": { - "description": "Next-hop for IPv6 routes.\n\nCan be `BgpNexthop::Ipv6Single` (16 bytes) or `BgpNexthop::Ipv6Double` (32 bytes with link-local address).", - "allOf": [ - { - "$ref": "#/components/schemas/BgpNexthop" - } - ] - }, - "nlri": { - "description": "IPv6 prefixes being announced", - "type": "array", - "items": { - "$ref": "#/components/schemas/Prefix6" - } - }, - "reserved": { - "description": "Reserved byte from RFC 4760 §3 (historically \"Number of SNPAs\" in RFC 2858). MUST be 0 per RFC 4760, but MUST be ignored by receiver. Stored for validation logging in session layer. This field is positioned before NLRI to match the wire format encoding.", - "type": "integer", - "format": "uint8", - "minimum": 0 - } - }, - "required": [ - "afi_safi", - "nexthop", - "nlri", - "reserved" - ] - } - ] - }, - "MpUnreachNlri": { - "description": "MP_UNREACH_NLRI path attribute\n\nEach variant represents a specific AFI+SAFI combination, providing compile-time guarantees about the address family of routes being withdrawn.\n\n```text 4. Multiprotocol Unreachable NLRI - MP_UNREACH_NLRI (Type Code 15):\n\nThis is an optional non-transitive attribute that can be used for the purpose of withdrawing multiple unfeasible routes from service.\n\nThe attribute is encoded as shown below:\n\n+---------------------------------------------------------+ | Address Family Identifier (2 octets) | +---------------------------------------------------------+ | Subsequent Address Family Identifier (1 octet) | +---------------------------------------------------------+ | Withdrawn Routes (variable) | +---------------------------------------------------------+ ```", - "oneOf": [ - { - "description": "IPv4 Unicast routes being withdrawn (AFI=1, SAFI=1)", - "type": "object", - "properties": { - "afi_safi": { - "type": "string", - "enum": [ - "ipv4_unicast" - ] - }, - "withdrawn": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Prefix4" - } - } - }, - "required": [ - "afi_safi", - "withdrawn" - ] - }, - { - "description": "IPv6 Unicast routes being withdrawn (AFI=2, SAFI=1)", - "type": "object", - "properties": { - "afi_safi": { - "type": "string", - "enum": [ - "ipv6_unicast" - ] - }, - "withdrawn": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Prefix6" - } - } - }, - "required": [ - "afi_safi", - "withdrawn" - ] - } - ] - }, - "NdpInterface": { - "description": "NDP state for an interface", - "type": "object", - "properties": { - "discovered_peer": { - "nullable": true, - "description": "Information about discovered peer (if any, including expired)", - "allOf": [ - { - "$ref": "#/components/schemas/NdpPeer" - } - ] - }, - "interface": { - "description": "Interface name (e.g., \"qsfp0\")", - "type": "string" - }, - "local_address": { - "description": "Local IPv6 link-local address", - "type": "string", - "format": "ipv6" - }, - "router_lifetime": { - "description": "Router lifetime advertised by this router (seconds)", - "type": "integer", - "format": "uint16", - "minimum": 0 - }, - "scope_id": { - "description": "IPv6 scope ID (interface index)", - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "thread_state": { - "nullable": true, - "description": "Thread state for rx/tx loops (None if interface not active in NDP)", - "allOf": [ - { - "$ref": "#/components/schemas/NdpThreadState" - } - ] - } - }, - "required": [ - "interface", - "local_address", - "router_lifetime", - "scope_id" - ] - }, - "NdpManagerState": { - "description": "NDP manager state showing overall health and interface status", - "type": "object", - "properties": { - "active_interfaces": { - "description": "Interfaces currently active in NDP (available on system)", - "type": "array", - "items": { - "type": "string" - } - }, - "monitor_thread_running": { - "description": "Whether the interface monitor thread is running", - "type": "boolean" - }, - "pending_interfaces": { - "description": "Interfaces configured but not yet available on the system", - "type": "array", - "items": { - "$ref": "#/components/schemas/NdpPendingInterface" - } - } - }, - "required": [ - "active_interfaces", - "monitor_thread_running", - "pending_interfaces" - ] - }, - "NdpPeer": { - "description": "Information about a discovered NDP peer", - "type": "object", - "properties": { - "address": { - "description": "Peer IPv6 address", - "type": "string", - "format": "ipv6" - }, - "discovered_at": { - "description": "When the peer was first discovered (ISO 8601 timestamp)", - "type": "string" - }, - "expired": { - "description": "Whether the peer entry has expired", - "type": "boolean" - }, - "last_advertisement": { - "description": "When the most recent Router Advertisement was received (ISO 8601 timestamp)", - "type": "string" - }, - "reachable_time": { - "description": "Reachable time from RA (milliseconds)", - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "retrans_timer": { - "description": "Retransmit timer from RA (milliseconds)", - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "router_lifetime": { - "description": "Router lifetime from RA (seconds)", - "type": "integer", - "format": "uint16", - "minimum": 0 - }, - "time_until_expiry": { - "nullable": true, - "description": "Time until expiry (human-readable), or None if already expired", - "type": "string" - } - }, - "required": [ - "address", - "discovered_at", - "expired", - "last_advertisement", - "reachable_time", - "retrans_timer", - "router_lifetime" - ] - }, - "NdpPendingInterface": { - "description": "Information about a pending NDP interface", - "type": "object", - "properties": { - "interface": { - "description": "Interface name", - "type": "string" - }, - "router_lifetime": { - "description": "Configured router lifetime (seconds)", - "type": "integer", - "format": "uint16", - "minimum": 0 - } - }, - "required": [ - "interface", - "router_lifetime" - ] - }, - "NdpThreadState": { - "description": "Thread state for NDP rx/tx loops on an interface", - "type": "object", - "properties": { - "rx_running": { - "description": "Whether the RX loop thread is running", - "type": "boolean" - }, - "tx_running": { - "description": "Whether the TX loop thread is running", - "type": "boolean" - } - }, - "required": [ - "rx_running", - "tx_running" - ] - }, - "Neighbor": { - "description": "Neighbor configuration with explicit per-address-family enablement (v3 API)", - "type": "object", - "properties": { - "asn": { - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "communities": { - "type": "array", - "items": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - "connect_retry": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "connect_retry_jitter": { - "nullable": true, - "description": "Jitter range for connect_retry timer. When used, the connect_retry timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", - "allOf": [ - { - "$ref": "#/components/schemas/JitterRange" - } - ] - }, - "delay_open": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "deterministic_collision_resolution": { - "description": "Enable deterministic collision resolution in Established state. When true, uses BGP-ID comparison per RFC 4271 §6.8 for collision resolution even when one connection is already in Established state. When false, Established connection always wins (timing-based resolution).", - "type": "boolean" - }, - "enforce_first_as": { - "type": "boolean" - }, - "group": { - "type": "string" - }, - "hold_time": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "host": { - "type": "string" - }, - "idle_hold_jitter": { - "nullable": true, - "description": "Jitter range for idle hold timer. When used, the idle hold timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", - "allOf": [ - { - "$ref": "#/components/schemas/JitterRange" - } - ] - }, - "idle_hold_time": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "ipv4_unicast": { - "nullable": true, - "description": "IPv4 Unicast address family configuration (None = disabled)", - "allOf": [ - { - "$ref": "#/components/schemas/Ipv4UnicastConfig" - } - ] - }, - "ipv6_unicast": { - "nullable": true, - "description": "IPv6 Unicast address family configuration (None = disabled)", - "allOf": [ - { - "$ref": "#/components/schemas/Ipv6UnicastConfig" - } - ] - }, - "keepalive": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "local_pref": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "md5_auth_key": { - "nullable": true, - "type": "string" - }, - "min_ttl": { - "nullable": true, - "type": "integer", - "format": "uint8", - "minimum": 0 - }, - "multi_exit_discriminator": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "name": { - "type": "string" - }, - "passive": { - "type": "boolean" - }, - "remote_asn": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "resolution": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "vlan_id": { - "nullable": true, - "type": "integer", - "format": "uint16", - "minimum": 0 - } - }, - "required": [ - "asn", - "communities", - "connect_retry", - "delay_open", - "deterministic_collision_resolution", - "enforce_first_as", - "group", - "hold_time", - "host", - "idle_hold_time", - "keepalive", - "name", - "passive", - "resolution" - ] - }, - "NeighborResetOp": { - "description": "V2 API neighbor reset operations with per-AF support", - "oneOf": [ - { - "description": "Hard reset - closes TCP connection and restarts session", - "type": "string", - "enum": [ - "Hard" - ] - }, - { - "description": "Soft inbound reset - sends route refresh for specified AF(s) None means all negotiated AFs", - "type": "object", - "properties": { - "SoftInbound": { - "nullable": true, - "allOf": [ - { - "$ref": "#/components/schemas/Afi" - } - ] - } - }, - "required": [ - "SoftInbound" - ], - "additionalProperties": false - }, - { - "description": "Soft outbound reset - re-advertises routes for specified AF(s) None means all negotiated AFs", - "type": "object", - "properties": { - "SoftOutbound": { - "nullable": true, - "allOf": [ - { - "$ref": "#/components/schemas/Afi" - } - ] - } - }, - "required": [ - "SoftOutbound" - ], - "additionalProperties": false - } - ] - }, - "NeighborResetRequest": { - "type": "object", - "properties": { - "addr": { - "type": "string", - "format": "ip" - }, - "asn": { - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "op": { - "$ref": "#/components/schemas/NeighborResetOp" - } - }, - "required": [ - "addr", - "asn", - "op" - ] - }, - "NotificationMessage": { - "description": "Notification messages are exchanged between BGP peers when an exceptional event has occurred.", - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "type": "integer", - "format": "uint8", - "minimum": 0 - } - }, - "error_code": { - "description": "Error code associated with the notification", - "allOf": [ - { - "$ref": "#/components/schemas/ErrorCode" - } - ] - }, - "error_subcode": { - "description": "Error subcode associated with the notification", - "allOf": [ - { - "$ref": "#/components/schemas/ErrorSubcode" - } - ] - } - }, - "required": [ - "data", - "error_code", - "error_subcode" - ] - }, - "OpenErrorSubcode": { - "description": "Open message error subcode types", - "type": "string", - "enum": [ - "unspecific", - "unsupported_version_number", - "bad_peer_a_s", - "bad_bgp_identifier", - "unsupported_optional_parameter", - "deprecated", - "unacceptable_hold_time", - "unsupported_capability" - ] - }, - "OpenMessage": { - "description": "The first message sent by each side once a TCP connection is established.\n\n```text 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Version | My Autonomous System | Hold Time : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : | BGP Identifier : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : | Opt Parm Len | Optional Parameters : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : : : Optional Parameters (cont, variable) : : | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ```\n\nRef: RFC 4271 §4.2", - "type": "object", - "properties": { - "asn": { - "description": "Autonomous system number of the sender. When 4-byte ASNs are in use this value is set to AS_TRANS which has a value of 23456.\n\nRef: RFC 4893 §7", - "type": "integer", - "format": "uint16", - "minimum": 0 - }, - "hold_time": { - "description": "Number of seconds the sender proposes for the hold timer.", - "type": "integer", - "format": "uint16", - "minimum": 0 - }, - "id": { - "description": "BGP identifier of the sender", - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "parameters": { - "description": "A list of optional parameters.", - "type": "array", - "items": { - "$ref": "#/components/schemas/OptionalParameter" - } - }, - "version": { - "description": "BGP protocol version.", - "type": "integer", - "format": "uint8", - "minimum": 0 - } - }, - "required": [ - "asn", - "hold_time", - "id", - "parameters", - "version" - ] - }, - "OptionalParameter": { - "description": "The IANA/IETF currently defines the following optional parameter types.", - "oneOf": [ - { - "description": "Code 0", - "type": "object", - "properties": { - "type": { - "type": "string", - "enum": [ - "reserved" - ] - } - }, - "required": [ - "type" - ] - }, - { - "description": "Code 1: RFC 4217, RFC 5492 (deprecated)", - "type": "object", - "properties": { - "type": { - "type": "string", - "enum": [ - "authentication" - ] - } - }, - "required": [ - "type" - ] - }, - { - "description": "Code 2: RFC 5492", - "type": "object", - "properties": { - "type": { - "type": "string", - "enum": [ - "capabilities" - ] - }, - "value": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Capability" - }, - "uniqueItems": true - } - }, - "required": [ - "type", - "value" - ] - }, - { - "description": "Unassigned", - "type": "object", - "properties": { - "type": { - "type": "string", - "enum": [ - "unassigned" - ] - } - }, - "required": [ - "type" - ] - }, - { - "description": "Code 255: RFC 9072", - "type": "object", - "properties": { - "type": { - "type": "string", - "enum": [ - "extended_length" - ] - } - }, - "required": [ - "type" - ] - } - ] - }, - "Origin4": { - "type": "object", - "properties": { - "asn": { - "description": "ASN of the router to originate from.", - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "prefixes": { - "description": "Set of prefixes to originate.", - "type": "array", - "items": { - "$ref": "#/components/schemas/Prefix4" - } - } - }, - "required": [ - "asn", - "prefixes" - ] - }, - "Origin6": { - "type": "object", - "properties": { - "asn": { - "description": "ASN of the router to originate from.", - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "prefixes": { - "description": "Set of prefixes to originate.", - "type": "array", - "items": { - "$ref": "#/components/schemas/Prefix6" - } - } - }, - "required": [ - "asn", - "prefixes" - ] - }, - "Path": { - "type": "object", - "properties": { - "bgp": { - "nullable": true, - "allOf": [ - { - "$ref": "#/components/schemas/BgpPathProperties" - } - ] - }, - "nexthop": { - "type": "string", - "format": "ip" - }, - "nexthop_interface": { - "nullable": true, - "description": "Interface binding for nexthop resolution.\n\nThis field is only populated for BGP unnumbered sessions where the nexthop is a link-local IPv6 address. For numbered peers, this is always None.\n\nAdded in API version 5.0.0 (UNNUMBERED).", - "type": "string" - }, - "rib_priority": { - "type": "integer", - "format": "uint8", - "minimum": 0 - }, - "shutdown": { - "type": "boolean" - }, - "vlan_id": { - "nullable": true, - "type": "integer", - "format": "uint16", - "minimum": 0 - } - }, - "required": [ - "nexthop", - "rib_priority", - "shutdown" - ] - }, - "PathAttribute": { - "description": "A self-describing BGP path attribute", - "type": "object", - "properties": { - "typ": { - "description": "Type encoding for the attribute", - "allOf": [ - { - "$ref": "#/components/schemas/PathAttributeType" - } - ] - }, - "value": { - "description": "Value of the attribute", - "allOf": [ - { - "$ref": "#/components/schemas/PathAttributeValue" - } - ] - } - }, - "required": [ - "typ", - "value" - ] - }, - "PathAttributeType": { - "description": "Type encoding for a path attribute.", - "type": "object", - "properties": { - "flags": { - "description": "Flags may include, Optional, Transitive, Partial and Extended Length.", - "type": "integer", - "format": "uint8", - "minimum": 0 - }, - "type_code": { - "description": "Type code for the path attribute.", - "allOf": [ - { - "$ref": "#/components/schemas/PathAttributeTypeCode" - } - ] - } - }, - "required": [ - "flags", - "type_code" - ] - }, - "PathAttributeTypeCode": { - "description": "An enumeration describing available path attribute type codes.", - "oneOf": [ - { - "type": "string", - "enum": [ - "as_path", - "next_hop", - "multi_exit_disc", - "local_pref", - "atomic_aggregate", - "aggregator", - "communities", - "mp_unreach_nlri", - "as4_aggregator" - ] - }, - { - "description": "RFC 4271", - "type": "string", - "enum": [ - "origin" - ] - }, - { - "description": "RFC 4760", - "type": "string", - "enum": [ - "mp_reach_nlri" - ] - }, - { - "description": "RFC 6793", - "type": "string", - "enum": [ - "as4_path" - ] - } - ] - }, - "PathAttributeValue": { - "description": "The value encoding of a path attribute.", - "oneOf": [ - { - "description": "The type of origin associated with a path", - "type": "object", - "properties": { - "origin": { - "$ref": "#/components/schemas/PathOrigin" - } - }, - "required": [ - "origin" - ], - "additionalProperties": false - }, - { - "description": "The AS set associated with a path", - "type": "object", - "properties": { - "as_path": { - "type": "array", - "items": { - "$ref": "#/components/schemas/As4PathSegment" - } - } - }, - "required": [ - "as_path" - ], - "additionalProperties": false - }, - { - "description": "The nexthop associated with a path (IPv4 only for traditional BGP)", - "type": "object", - "properties": { - "next_hop": { - "type": "string", - "format": "ipv4" - } - }, - "required": [ - "next_hop" - ], - "additionalProperties": false - }, - { - "description": "A metric used for external (inter-AS) links to discriminate among multiple entry or exit points.", - "type": "object", - "properties": { - "multi_exit_disc": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - "required": [ - "multi_exit_disc" - ], - "additionalProperties": false - }, - { - "description": "Local pref is included in update messages sent to internal peers and indicates a degree of preference.", - "type": "object", - "properties": { - "local_pref": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - "required": [ - "local_pref" - ], - "additionalProperties": false - }, - { - "description": "AGGREGATOR: AS number and IP address of the last aggregating BGP speaker (2-octet ASN)", - "type": "object", - "properties": { - "aggregator": { - "$ref": "#/components/schemas/Aggregator" - } - }, - "required": [ - "aggregator" - ], - "additionalProperties": false - }, - { - "description": "Indicates communities associated with a path.", - "type": "object", - "properties": { - "communities": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Community" - } - } - }, - "required": [ - "communities" - ], - "additionalProperties": false - }, - { - "description": "Indicates this route was formed via aggregation (RFC 4271 §5.1.7)", - "type": "string", - "enum": [ - "atomic_aggregate" - ] - }, - { - "description": "The 4-byte encoded AS set associated with a path", - "type": "object", - "properties": { - "as4_path": { - "type": "array", - "items": { - "$ref": "#/components/schemas/As4PathSegment" - } - } - }, - "required": [ - "as4_path" - ], - "additionalProperties": false - }, - { - "description": "AS4_AGGREGATOR: AS number and IP address of the last aggregating BGP speaker (4-octet ASN)", - "type": "object", - "properties": { - "as4_aggregator": { - "$ref": "#/components/schemas/As4Aggregator" - } - }, - "required": [ - "as4_aggregator" - ], - "additionalProperties": false - }, - { - "description": "Carries reachable MP-BGP NLRI and Next-hop (advertisement).", - "type": "object", - "properties": { - "mp_reach_nlri": { - "$ref": "#/components/schemas/MpReachNlri" - } - }, - "required": [ - "mp_reach_nlri" - ], - "additionalProperties": false - }, - { - "description": "Carries unreachable MP-BGP NLRI (withdrawal).", - "type": "object", - "properties": { - "mp_unreach_nlri": { - "$ref": "#/components/schemas/MpUnreachNlri" - } - }, - "required": [ - "mp_unreach_nlri" - ], - "additionalProperties": false - } - ] - }, - "PathOrigin": { - "description": "An enumeration indicating the origin type of a path.", - "oneOf": [ - { - "description": "Interior gateway protocol", - "type": "string", - "enum": [ - "igp" - ] - }, - { - "description": "Exterior gateway protocol", - "type": "string", - "enum": [ - "egp" - ] - }, - { - "description": "Incomplete path origin", - "type": "string", - "enum": [ - "incomplete" - ] - } - ] - }, - "PeerCounters": { - "description": "Session-level counters that persist across connection changes These serve as aggregate counters across all connections for the session", - "type": "object", - "properties": { - "active_connections_accepted": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "active_connections_declined": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "connection_retries": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "connector_panics": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "hold_timer_expirations": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "idle_hold_timer_expirations": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "keepalive_send_failure": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "keepalives_received": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "keepalives_sent": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "md5_auth_failures": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "notification_send_failure": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "notifications_received": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "notifications_sent": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "open_handle_failures": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "open_send_failure": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "opens_received": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "opens_sent": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "passive_connections_accepted": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "passive_connections_declined": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "prefixes_advertised": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "prefixes_imported": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "route_refresh_received": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "route_refresh_send_failure": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "route_refresh_sent": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "tcp_connection_failure": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "transitions_to_active": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "transitions_to_connect": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "transitions_to_connection_collision": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "transitions_to_established": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "transitions_to_idle": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "transitions_to_open_confirm": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "transitions_to_open_sent": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "transitions_to_session_setup": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "unexpected_keepalive_message": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "unexpected_notification_message": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "unexpected_open_message": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "unexpected_route_refresh_message": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "unexpected_update_message": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "unnegotiated_address_family": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "update_nexhop_missing": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "update_send_failure": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "updates_received": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "updates_sent": { - "type": "integer", - "format": "uint64", - "minimum": 0 - } - }, - "required": [ - "active_connections_accepted", - "active_connections_declined", - "connection_retries", - "connector_panics", - "hold_timer_expirations", - "idle_hold_timer_expirations", - "keepalive_send_failure", - "keepalives_received", - "keepalives_sent", - "md5_auth_failures", - "notification_send_failure", - "notifications_received", - "notifications_sent", - "open_handle_failures", - "open_send_failure", - "opens_received", - "opens_sent", - "passive_connections_accepted", - "passive_connections_declined", - "prefixes_advertised", - "prefixes_imported", - "route_refresh_received", - "route_refresh_send_failure", - "route_refresh_sent", - "tcp_connection_failure", - "transitions_to_active", - "transitions_to_connect", - "transitions_to_connection_collision", - "transitions_to_established", - "transitions_to_idle", - "transitions_to_open_confirm", - "transitions_to_open_sent", - "transitions_to_session_setup", - "unexpected_keepalive_message", - "unexpected_notification_message", - "unexpected_open_message", - "unexpected_route_refresh_message", - "unexpected_update_message", - "unnegotiated_address_family", - "update_nexhop_missing", - "update_send_failure", - "updates_received", - "updates_sent" - ] - }, - "PeerId": { - "description": "Identifies a BGP peer for session management and route tracking.\n\nBGP peers can be identified in two ways: - **Numbered**: Traditional BGP peering using explicit IP addresses - **Unnumbered**: Modern peering using interface names with link-local addresses\n\n# Unnumbered Peering\n\nUnnumbered BGP uses interface names as stable identifiers instead of IP addresses. This is important because: - Link-local IPv6 addresses are discovered dynamically via NDP - Multiple interfaces may have peers with the same link-local address (e.g., fe80::1 on eth0 and fe80::1 on eth1) - Scope ID (interface index) disambiguates link-local addresses, but is not stable across reboots - Interface names provide stable, unambiguous peer identification\n\n# Route Tracking\n\nThis type is used in [`BgpPathProperties`](crate::BgpPathProperties) to track which peer advertised a route. Using `PeerId` instead of `IpAddr` ensures: - Unnumbered peers are properly distinguished even if they share link-local IPs - Route cleanup correctly removes only the routes from the intended peer - No cross-contamination when multiple unnumbered sessions exist\n\n# Examples\n\n``` use rdb_types::PeerId; use std::net::IpAddr;\n\n// Numbered peer let numbered = PeerId::Ip(\"192.0.2.1\".parse::().unwrap());\n\n// Unnumbered peer let unnumbered = PeerId::Interface(\"eth0\".to_string()); ```", - "oneOf": [ - { - "description": "Numbered peer identified by IP address\n\nUsed for traditional BGP sessions where peers are configured with explicit IP addresses (either IPv4 or IPv6 global unicast).", - "type": "object", - "properties": { - "Ip": { - "type": "string", - "format": "ip" - } - }, - "required": [ - "Ip" - ], - "additionalProperties": false - }, - { - "description": "Unnumbered peer identified by interface name\n\nUsed for unnumbered BGP sessions where peers are discovered via NDP on a specific interface. The interface name (e.g., \"eth0\") provides stable identification even though the peer's link-local address may be dynamic or shared with other interfaces.", - "type": "object", - "properties": { - "Interface": { - "type": "string" - } - }, - "required": [ - "Interface" - ], - "additionalProperties": false - } - ] - }, - "PeerInfo": { - "type": "object", - "properties": { - "asn": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "counters": { - "$ref": "#/components/schemas/PeerCounters" - }, - "fsm_state": { - "$ref": "#/components/schemas/FsmStateKind" - }, - "fsm_state_duration": { - "$ref": "#/components/schemas/Duration" - }, - "id": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "ipv4_unicast": { - "$ref": "#/components/schemas/Ipv4UnicastConfig" - }, - "ipv6_unicast": { - "$ref": "#/components/schemas/Ipv6UnicastConfig" - }, - "local_ip": { - "type": "string", - "format": "ip" - }, - "local_tcp_port": { - "type": "integer", - "format": "uint16", - "minimum": 0 - }, - "name": { - "type": "string" - }, - "peer_group": { - "type": "string" - }, - "received_capabilities": { - "type": "array", - "items": { - "$ref": "#/components/schemas/BgpCapability" - } - }, - "remote_ip": { - "type": "string", - "format": "ip" - }, - "remote_tcp_port": { - "type": "integer", - "format": "uint16", - "minimum": 0 - }, - "timers": { - "$ref": "#/components/schemas/PeerTimers" - } - }, - "required": [ - "counters", - "fsm_state", - "fsm_state_duration", - "ipv4_unicast", - "ipv6_unicast", - "local_ip", - "local_tcp_port", - "name", - "peer_group", - "received_capabilities", - "remote_ip", - "remote_tcp_port", - "timers" - ] - }, - "PeerTimers": { - "type": "object", - "properties": { - "connect_retry": { - "$ref": "#/components/schemas/StaticTimerInfo" - }, - "connect_retry_jitter": { - "nullable": true, - "allOf": [ - { - "$ref": "#/components/schemas/JitterRange" - } - ] - }, - "delay_open": { - "$ref": "#/components/schemas/StaticTimerInfo" - }, - "hold": { - "$ref": "#/components/schemas/DynamicTimerInfo" - }, - "idle_hold": { - "$ref": "#/components/schemas/StaticTimerInfo" - }, - "idle_hold_jitter": { - "nullable": true, - "allOf": [ - { - "$ref": "#/components/schemas/JitterRange" - } - ] - }, - "keepalive": { - "$ref": "#/components/schemas/DynamicTimerInfo" - } - }, - "required": [ - "connect_retry", - "delay_open", - "hold", - "idle_hold", - "keepalive" - ] - }, - "Prefix": { - "oneOf": [ - { - "type": "object", - "properties": { - "V4": { - "$ref": "#/components/schemas/Prefix4" - } - }, - "required": [ - "V4" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "V6": { - "$ref": "#/components/schemas/Prefix6" - } - }, - "required": [ - "V6" - ], - "additionalProperties": false - } - ] - }, - "Prefix4": { - "type": "object", - "properties": { - "length": { - "type": "integer", - "format": "uint8", - "minimum": 0 - }, - "value": { - "type": "string", - "format": "ipv4" - } - }, - "required": [ - "length", - "value" - ] - }, - "Prefix6": { - "type": "object", - "properties": { - "length": { - "type": "integer", - "format": "uint8", - "minimum": 0 - }, - "value": { - "type": "string", - "format": "ipv6" - } - }, - "required": [ - "length", - "value" - ] - }, - "Rib": { - "type": "object", - "additionalProperties": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Path" - }, - "uniqueItems": true - } - }, - "RouteRefreshMessage": { - "type": "object", - "properties": { - "afi": { - "description": "Address family identifier.", - "type": "integer", - "format": "uint16", - "minimum": 0 - }, - "safi": { - "description": "Subsequent address family identifier.", - "type": "integer", - "format": "uint8", - "minimum": 0 - } - }, - "required": [ - "afi", - "safi" - ] - }, - "Router": { - "type": "object", - "properties": { - "asn": { - "description": "Autonomous system number for this router", - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "graceful_shutdown": { - "description": "Gracefully shut this router down.", - "type": "boolean" - }, - "id": { - "description": "Id for this router", - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "listen": { - "description": "Listening address :", - "type": "string" - } - }, - "required": [ - "asn", - "graceful_shutdown", - "id", - "listen" - ] - }, - "SessionMode": { - "type": "string", - "enum": [ - "SingleHop", - "MultiHop" - ] - }, - "ShaperSource": { - "type": "object", - "properties": { - "asn": { - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "code": { - "type": "string" - } - }, - "required": [ - "asn", - "code" - ] - }, - "StaticRoute4": { - "type": "object", - "properties": { - "nexthop": { - "type": "string", - "format": "ipv4" - }, - "prefix": { - "$ref": "#/components/schemas/Prefix4" - }, - "rib_priority": { - "type": "integer", - "format": "uint8", - "minimum": 0 - }, - "vlan_id": { - "nullable": true, - "type": "integer", - "format": "uint16", - "minimum": 0 - } - }, - "required": [ - "nexthop", - "prefix", - "rib_priority" - ] - }, - "StaticRoute4List": { - "type": "object", - "properties": { - "list": { - "type": "array", - "items": { - "$ref": "#/components/schemas/StaticRoute4" - } - } - }, - "required": [ - "list" - ] - }, - "StaticRoute6": { - "type": "object", - "properties": { - "nexthop": { - "type": "string", - "format": "ipv6" - }, - "prefix": { - "$ref": "#/components/schemas/Prefix6" - }, - "rib_priority": { - "type": "integer", - "format": "uint8", - "minimum": 0 - }, - "vlan_id": { - "nullable": true, - "type": "integer", - "format": "uint16", - "minimum": 0 - } - }, - "required": [ - "nexthop", - "prefix", - "rib_priority" - ] - }, - "StaticRoute6List": { - "type": "object", - "properties": { - "list": { - "type": "array", - "items": { - "$ref": "#/components/schemas/StaticRoute6" - } - } - }, - "required": [ - "list" - ] - }, - "StaticTimerInfo": { - "description": "Timer information for static (non-negotiated) timers", - "type": "object", - "properties": { - "configured": { - "$ref": "#/components/schemas/Duration" - }, - "remaining": { - "$ref": "#/components/schemas/Duration" - } - }, - "required": [ - "configured", - "remaining" - ] - }, - "SwitchIdentifiers": { - "description": "Identifiers for a switch.", - "type": "object", - "properties": { - "slot": { - "nullable": true, - "description": "The slot number of the switch being managed.\n\nMGS uses u16 for this internally.", - "type": "integer", - "format": "uint16", - "minimum": 0 - } - } - }, - "UnnumberedBgpPeerConfig": { - "type": "object", - "properties": { - "communities": { - "type": "array", - "items": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - "connect_retry": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "connect_retry_jitter": { - "nullable": true, - "description": "Jitter range for connect_retry timer. When used, the connect_retry timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", - "allOf": [ - { - "$ref": "#/components/schemas/JitterRange" - } - ] - }, - "delay_open": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "deterministic_collision_resolution": { - "description": "Enable deterministic collision resolution in Established state. When true, uses BGP-ID comparison per RFC 4271 §6.8 for collision resolution even when one connection is already in Established state. When false, Established connection always wins (timing-based resolution).", - "type": "boolean" - }, - "enforce_first_as": { - "type": "boolean" - }, - "hold_time": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "idle_hold_jitter": { - "nullable": true, - "description": "Jitter range for idle hold timer. When used, the idle hold timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", - "allOf": [ - { - "$ref": "#/components/schemas/JitterRange" - } - ] - }, - "idle_hold_time": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "interface": { - "type": "string" - }, - "ipv4_unicast": { - "nullable": true, - "description": "IPv4 Unicast address family configuration (None = disabled)", - "allOf": [ - { - "$ref": "#/components/schemas/Ipv4UnicastConfig" - } - ] - }, - "ipv6_unicast": { - "nullable": true, - "description": "IPv6 Unicast address family configuration (None = disabled)", - "allOf": [ - { - "$ref": "#/components/schemas/Ipv6UnicastConfig" - } - ] - }, - "keepalive": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "local_pref": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "md5_auth_key": { - "nullable": true, - "type": "string" - }, - "min_ttl": { - "nullable": true, - "type": "integer", - "format": "uint8", - "minimum": 0 - }, - "multi_exit_discriminator": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "name": { - "type": "string" - }, - "passive": { - "type": "boolean" - }, - "remote_asn": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "resolution": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "router_lifetime": { - "type": "integer", - "format": "uint16", - "minimum": 0 - }, - "vlan_id": { - "nullable": true, - "type": "integer", - "format": "uint16", - "minimum": 0 - } - }, - "required": [ - "communities", - "connect_retry", - "delay_open", - "deterministic_collision_resolution", - "enforce_first_as", - "hold_time", - "idle_hold_time", - "interface", - "keepalive", - "name", - "passive", - "resolution", - "router_lifetime" - ] - }, - "UnnumberedNeighbor": { - "type": "object", - "properties": { - "act_as_a_default_ipv6_router": { - "type": "integer", - "format": "uint16", - "minimum": 0 - }, - "asn": { - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "communities": { - "type": "array", - "items": { - "type": "integer", - "format": "uint32", - "minimum": 0 - } - }, - "connect_retry": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "connect_retry_jitter": { - "nullable": true, - "description": "Jitter range for connect_retry timer. When used, the connect_retry timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", - "allOf": [ - { - "$ref": "#/components/schemas/JitterRange" - } - ] - }, - "delay_open": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "deterministic_collision_resolution": { - "description": "Enable deterministic collision resolution in Established state. When true, uses BGP-ID comparison per RFC 4271 §6.8 for collision resolution even when one connection is already in Established state. When false, Established connection always wins (timing-based resolution).", - "type": "boolean" - }, - "enforce_first_as": { - "type": "boolean" - }, - "group": { - "type": "string" - }, - "hold_time": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "idle_hold_jitter": { - "nullable": true, - "description": "Jitter range for idle hold timer. When used, the idle hold timer is multiplied by a random value within the (min, max) range supplied. Useful to help break repeated synchronization of connection collisions.", - "allOf": [ - { - "$ref": "#/components/schemas/JitterRange" - } - ] - }, - "idle_hold_time": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "interface": { - "type": "string" - }, - "ipv4_unicast": { - "nullable": true, - "description": "IPv4 Unicast address family configuration (None = disabled)", - "allOf": [ - { - "$ref": "#/components/schemas/Ipv4UnicastConfig" - } - ] - }, - "ipv6_unicast": { - "nullable": true, - "description": "IPv6 Unicast address family configuration (None = disabled)", - "allOf": [ - { - "$ref": "#/components/schemas/Ipv6UnicastConfig" - } - ] - }, - "keepalive": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "local_pref": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "md5_auth_key": { - "nullable": true, - "type": "string" - }, - "min_ttl": { - "nullable": true, - "type": "integer", - "format": "uint8", - "minimum": 0 - }, - "multi_exit_discriminator": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "name": { - "type": "string" - }, - "passive": { - "type": "boolean" - }, - "remote_asn": { - "nullable": true, - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "resolution": { - "type": "integer", - "format": "uint64", - "minimum": 0 - }, - "vlan_id": { - "nullable": true, - "type": "integer", - "format": "uint16", - "minimum": 0 - } - }, - "required": [ - "act_as_a_default_ipv6_router", - "asn", - "communities", - "connect_retry", - "delay_open", - "deterministic_collision_resolution", - "enforce_first_as", - "group", - "hold_time", - "idle_hold_time", - "interface", - "keepalive", - "name", - "passive", - "resolution" - ] - }, - "UnnumberedNeighborResetRequest": { - "type": "object", - "properties": { - "asn": { - "type": "integer", - "format": "uint32", - "minimum": 0 - }, - "interface": { - "type": "string" - }, - "op": { - "$ref": "#/components/schemas/NeighborResetOp" - } - }, - "required": [ - "asn", - "interface", - "op" - ] - }, - "UpdateErrorSubcode": { - "description": "Update message error subcode types", - "type": "string", - "enum": [ - "unspecific", - "malformed_attribute_list", - "unrecognized_well_known_attribute", - "missing_well_known_attribute", - "attribute_flags", - "attribute_length", - "invalid_origin_attribute", - "deprecated", - "invalid_nexthop_attribute", - "optional_attribute", - "invalid_network_field", - "malformed_as_path" - ] - }, - "UpdateMessage": { - "description": "An update message is used to advertise feasible routes that share common path attributes to a peer, or to withdraw multiple unfeasible routes from service.\n\n```text 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Witdrawn Length | Withdrawn Routes : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : : : Withdrawn Routes (cont, variable) : : | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Path Attribute Length | Path Attributes : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : : : Path Attributes (cont, variable) : : | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : : : Network Layer Reachability Information (variable) : : | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ```\n\nRef: RFC 4271 §4.3", - "type": "object", - "properties": { - "nlri": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Prefix4" - } - }, - "path_attributes": { - "type": "array", - "items": { - "$ref": "#/components/schemas/PathAttribute" - } - }, - "withdrawn": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Prefix4" - } - } - }, - "required": [ - "nlri", - "path_attributes", - "withdrawn" - ] - }, - "AddressFamily": { - "description": "Represents the address family (protocol version) for network routes.\n\nThis is the canonical source of truth for address family definitions across the entire codebase. All routing-related components (RIB operations, BGP messages, API filtering, CLI tools) use this single enum rather than defining their own.\n\n# Semantics\n\nWhen used in filtering contexts (e.g., database queries or API parameters), `Option` is preferred: - `None` = no filter (match all address families) - `Some(Ipv4)` = IPv4 routes only - `Some(Ipv6)` = IPv6 routes only\n\n# Examples\n\n``` use rdb_types::AddressFamily;\n\nlet ipv4 = AddressFamily::Ipv4; let ipv6 = AddressFamily::Ipv6;\n\n// For filtering, use Option let filter: Option = Some(AddressFamily::Ipv4); let no_filter: Option = None; // matches all families ```", - "oneOf": [ - { - "description": "Internet Protocol Version 4 (IPv4)", - "type": "string", - "enum": [ - "Ipv4" - ] - }, - { - "description": "Internet Protocol Version 6 (IPv6)", - "type": "string", - "enum": [ - "Ipv6" - ] - } - ] - }, - "ProtocolFilter": { - "oneOf": [ - { - "description": "BGP routes only", - "type": "string", - "enum": [ - "Bgp" - ] - }, - { - "description": "Static routes only", - "type": "string", - "enum": [ - "Static" - ] - } - ] - } - }, - "responses": { - "Error": { - "description": "Error", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Error" - } - } - } - } - } - } -} diff --git a/openapi/mg-admin/mg-admin-6.0.0-324339.json.gitstub b/openapi/mg-admin/mg-admin-6.0.0-324339.json.gitstub new file mode 100644 index 000000000..e7de29fb6 --- /dev/null +++ b/openapi/mg-admin/mg-admin-6.0.0-324339.json.gitstub @@ -0,0 +1 @@ +5ba19839e1f9e39b88a79072d0001c4e116c4f60:openapi/mg-admin/mg-admin-6.0.0-324339.json From 5c2e586335da57f84f44a3f5c09b63cb5da37fdc Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 18 Mar 2026 13:19:24 -0600 Subject: [PATCH 51/58] LoopbackIpManager: Set temp flag on IPs on illumos --- mg-common/src/test.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/mg-common/src/test.rs b/mg-common/src/test.rs index 5d25d4fb5..947b4831c 100644 --- a/mg-common/src/test.rs +++ b/mg-common/src/test.rs @@ -350,6 +350,7 @@ impl LoopbackIpManager { let cmd = [ "ipadm", "create-addr", + "-t", "-T", "static", "-a", From 7c3b3d80e9511d8150cfbcdbfc89e27312502e65 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 18 Mar 2026 21:52:47 +0000 Subject: [PATCH 52/58] LoopbackIpManager: fix addrobj for ipadm delete --- mg-common/src/test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mg-common/src/test.rs b/mg-common/src/test.rs index 947b4831c..f1e03381a 100644 --- a/mg-common/src/test.rs +++ b/mg-common/src/test.rs @@ -495,7 +495,7 @@ impl LoopbackIpManager { }; let mut ip_descr = format!("{v}{}", ip.address); ip_descr.retain(|c| c.is_alphanumeric()); - let addr_obj = format!("{}/test{}", ifname, ip_descr); + let addr_obj = format!("{}/{}", ifname, ip_descr); Command::new("pfexec") .args(["ipadm", "delete-addr", &addr_obj]) .output() From d9399dbcab4613ee0a0e6c2806fbf0eb4bdbec5c Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 18 Mar 2026 22:04:33 +0000 Subject: [PATCH 53/58] LoopbackIpManager: Remember to skip ::1 too We skipped both 127.0.0.1 and ::1 during install, but were not skipping ::1 during uninstall. Let's make that symmetric. --- mg-common/src/test.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mg-common/src/test.rs b/mg-common/src/test.rs index f1e03381a..27b224220 100644 --- a/mg-common/src/test.rs +++ b/mg-common/src/test.rs @@ -411,16 +411,17 @@ impl LoopbackIpManager { /// Skips 127.0.0.1 as it should always remain on loopback interfaces fn uninstall_single_ip(&mut self, target_addr: IpAddr) { // Skip 127.0.0.1 as it should always remain on loopback interfaces - if target_addr.to_string() == "127.0.0.1" { + let target_str = target_addr.to_string(); + if target_str == "127.0.0.1" || target_str == "::1" { info!( self.log, - "skipping 127.0.0.1 cleanup (always present on loopback)" + "skipping {target_str} cleanup (always present on loopback)" ); // Just mark as uninstalled in our tracking, but don't touch the system for ip in &mut self.ips { if ip.address == target_addr { ip.installed = false; - ip.lockfile = None; // No lockfile was created for 127.0.0.1 + ip.lockfile = None; // No lockfile was created for these addresses break; } } From 2c6bc7df812eb2481ac95e28e0e4daa81d3c2679 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 18 Mar 2026 22:14:46 +0000 Subject: [PATCH 54/58] LoopbackIpManager: use int for installed count Move from using an installed bool to using a u32 to track the number of intra-process users of an IP, which prevents early uninstalls. Move loopback-skipping logic (127.0.0.1 / ::1) into a dedicated function and make a few methods private that don't need to be public. --- mg-common/src/test.rs | 122 +++++++++++++++++++++++++----------------- 1 file changed, 74 insertions(+), 48 deletions(-) diff --git a/mg-common/src/test.rs b/mg-common/src/test.rs index 27b224220..834c3134d 100644 --- a/mg-common/src/test.rs +++ b/mg-common/src/test.rs @@ -176,7 +176,10 @@ macro_rules! prefix6 { struct ManagedIp { address: IpAddr, - installed: bool, + /// Number of IpAllocations within this process currently using this IP. + /// The system-level install/uninstall only happens when this transitions + /// between 0 and 1. use_count > 0 implies the IP is installed. + use_count: u32, lockfile: Option, } @@ -210,13 +213,13 @@ impl LoopbackIpManager { } } - pub fn add(&mut self, addresses: &[IpAddr]) { + fn add(&mut self, addresses: &[IpAddr]) { for addr in addresses { // Only add if not already present if !self.ips.iter().any(|ip| ip.address == *addr) { self.ips.push(ManagedIp { address: *addr, - installed: false, + use_count: 0, lockfile: None, }); } @@ -233,7 +236,7 @@ impl LoopbackIpManager { { let mut mgr = lock!(manager); mgr.add(addresses); - mgr.install()?; + mgr.install(addresses)?; } // Return guard that will clean up on drop @@ -244,6 +247,13 @@ impl LoopbackIpManager { } } +/// Returns true for addresses that are always present on loopback interfaces +/// and should never be installed or removed by the manager. +fn is_always_present(addr: IpAddr) -> bool { + addr == IpAddr::V4(std::net::Ipv4Addr::LOCALHOST) + || addr == IpAddr::V6(std::net::Ipv6Addr::LOCALHOST) +} + // Helper functions for lockfile-based reference counting fn flock(path: &str) -> std::io::Result { let file = OpenOptions::new() @@ -272,12 +282,15 @@ fn write_refcount(file: &mut File, count: u32) -> std::io::Result<()> { } impl LoopbackIpManager { - pub fn install(&mut self) -> Result<(), std::io::Error> { + fn install( + &mut self, + addresses: &[IpAddr], + ) -> Result<(), std::io::Error> { let ifname = self.ifname.clone(); let log = self.log.clone(); for ip in &mut self.ips { - if !ip.installed { + if addresses.contains(&ip.address) { Self::install_single_ip_static(&ifname, &log, ip)?; } } @@ -291,14 +304,30 @@ impl LoopbackIpManager { log: &Logger, ip: &mut ManagedIp, ) -> Result<(), std::io::Error> { - // Skip 127.0.0.1/::1 as they're always present on loopback interfaces by default - let ip_str = ip.address.to_string(); - if ip_str == "127.0.0.1" || ip_str == "::1" { - info!(log, "skipping {ip_str} (always present on loopback)"); - ip.installed = true; // Mark as installed but don't create lockfile + if is_always_present(ip.address) { + info!(log, "skipping {} (always present on loopback)", ip.address); + ip.use_count += 1; return Ok(()); } + // If already installed by another allocation in this process, nothing + // more to do — the system IP and lockfile refcount are already set up. + // Increment use_count now since this path can't fail. + if ip.use_count > 0 { + ip.use_count += 1; + info!( + log, + "{}: already installed, use_count now {}", + ip.address, + ip.use_count, + ); + return Ok(()); + } + + // First use in this process: acquire the cross-process lockfile and + // install the IP if no other process has done so yet. + // Don't increment use_count until all fallible work succeeds. + // 1. Acquire lock for this IP let lockfile_path = format!("/tmp/maghemite-ip-{}.lock", ip.address); let mut lockfile = flock(&lockfile_path)?; @@ -319,8 +348,8 @@ impl LoopbackIpManager { ); write_refcount(&mut lockfile, new_refcount)?; - // 5. Update our state - ip.installed = true; + // 5. All fallible work done — update our state + ip.use_count += 1; ip.lockfile = Some(lockfile); Ok(()) @@ -401,7 +430,7 @@ impl LoopbackIpManager { } /// Uninstall all managed addresses - pub fn uninstall(&mut self) { + fn uninstall(&mut self) { let addresses: Vec = self.ips.iter().map(|ip| ip.address).collect(); self.uninstall_addresses(&addresses); @@ -410,18 +439,14 @@ impl LoopbackIpManager { /// Uninstall a single IP address with proper refcount management /// Skips 127.0.0.1 as it should always remain on loopback interfaces fn uninstall_single_ip(&mut self, target_addr: IpAddr) { - // Skip 127.0.0.1 as it should always remain on loopback interfaces - let target_str = target_addr.to_string(); - if target_str == "127.0.0.1" || target_str == "::1" { + if is_always_present(target_addr) { info!( self.log, - "skipping {target_str} cleanup (always present on loopback)" + "skipping {target_addr} cleanup (always present on loopback)" ); - // Just mark as uninstalled in our tracking, but don't touch the system for ip in &mut self.ips { if ip.address == target_addr { - ip.installed = false; - ip.lockfile = None; // No lockfile was created for these addresses + ip.use_count = ip.use_count.saturating_sub(1); break; } } @@ -429,7 +454,22 @@ impl LoopbackIpManager { } for ip in &mut self.ips { - if ip.address == target_addr && ip.installed { + if ip.address == target_addr && ip.use_count > 0 { + ip.use_count -= 1; + + if ip.use_count > 0 { + // Other allocations in this process still need this IP. + info!( + self.log, + "{}: use_count now {}, keeping installed", + ip.address, + ip.use_count, + ); + break; + } + + // Last in-process user: decrement the cross-process refcount + // and remove the IP from the system if no other process needs it. if let Some(mut lockfile) = ip.lockfile.take() { let lockfile_path = format!("/tmp/maghemite-ip-{}.lock", ip.address); @@ -450,7 +490,7 @@ impl LoopbackIpManager { Self::remove_ip_from_system_static( &self.ifname, &self.log, - ip, + ip.address, ); // Remove the lockfile completely when refcount reaches 0 @@ -473,9 +513,6 @@ impl LoopbackIpManager { } } - // Always update our state - ip.installed = false; - ip.lockfile = None; info!(self.log, "uninstalled {}", ip.address); break; } @@ -483,20 +520,16 @@ impl LoopbackIpManager { } /// Remove IP from the system using platform-specific commands - fn remove_ip_from_system_static( - ifname: &str, - log: &Logger, - ip: &ManagedIp, - ) { + fn remove_ip_from_system_static(ifname: &str, log: &Logger, addr: IpAddr) { #[cfg(target_os = "illumos")] let output = { - let v = match ip.address { + let v = match addr { IpAddr::V4(_) => "v4", IpAddr::V6(_) => "v6", }; - let mut ip_descr = format!("{v}{}", ip.address); + let mut ip_descr = format!("{v}{addr}"); ip_descr.retain(|c| c.is_alphanumeric()); - let addr_obj = format!("{}/{}", ifname, ip_descr); + let addr_obj = format!("{ifname}/{ip_descr}"); Command::new("pfexec") .args(["ipadm", "delete-addr", &addr_obj]) .output() @@ -504,11 +537,11 @@ impl LoopbackIpManager { #[cfg(target_os = "linux")] let output = { - let mask = match ip.address { + let mask = match addr { IpAddr::V4(_) => 32, IpAddr::V6(_) => 128, }; - let addr_str = format!("{}/{mask}", ip.address); + let addr_str = format!("{addr}/{mask}"); Command::new("sudo") .args(["ip", "addr", "del", &addr_str, "dev", ifname]) .output() @@ -516,18 +549,12 @@ impl LoopbackIpManager { #[cfg(target_os = "macos")] let output = { - let af = match ip.address { + let af = match addr { IpAddr::V4(_) => "inet", IpAddr::V6(_) => "inet6", }; Command::new("sudo") - .args([ - "ifconfig", - ifname, - af, - &ip.address.to_string(), - "-alias", - ]) + .args(["ifconfig", ifname, af, &addr.to_string(), "-alias"]) .output() }; @@ -542,18 +569,17 @@ impl LoopbackIpManager { { error!( log, - "failed to remove {} from system: {stderr}", - ip.address + "failed to remove {addr} from system: {stderr}" ); return; } } - info!(log, "removed {} from system", ip.address); + info!(log, "removed {addr} from system"); } Err(e) => { error!( log, - "failed to execute remove command for {}: {e}", ip.address + "failed to execute remove command for {addr}: {e}" ); } } From 81d751f6dbd4029a674a6ccb9f3da93f2a3e035a Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 18 Mar 2026 22:34:39 +0000 Subject: [PATCH 55/58] LoopbackIpManager: Rollback IPs on install failure If an installation fails for one of the requested IPs, rollback the IPs. Removes an unused uninstall() method. --- mg-common/src/test.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/mg-common/src/test.rs b/mg-common/src/test.rs index 834c3134d..68b48fadb 100644 --- a/mg-common/src/test.rs +++ b/mg-common/src/test.rs @@ -236,7 +236,10 @@ impl LoopbackIpManager { { let mut mgr = lock!(manager); mgr.add(addresses); - mgr.install(addresses)?; + if let Err(e) = mgr.install(addresses) { + mgr.uninstall_addresses(addresses); + return Err(e); + } } // Return guard that will clean up on drop @@ -429,13 +432,6 @@ impl LoopbackIpManager { } } - /// Uninstall all managed addresses - fn uninstall(&mut self) { - let addresses: Vec = - self.ips.iter().map(|ip| ip.address).collect(); - self.uninstall_addresses(&addresses); - } - /// Uninstall a single IP address with proper refcount management /// Skips 127.0.0.1 as it should always remain on loopback interfaces fn uninstall_single_ip(&mut self, target_addr: IpAddr) { From 9b5e8aebf6b5e63bf6c138b8dd1f6701338bfb65 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Thu, 19 Mar 2026 18:22:24 +0000 Subject: [PATCH 56/58] LoopbackIpManager: cargo fmt --- mg-common/src/test.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/mg-common/src/test.rs b/mg-common/src/test.rs index 68b48fadb..71f815c1d 100644 --- a/mg-common/src/test.rs +++ b/mg-common/src/test.rs @@ -285,10 +285,7 @@ fn write_refcount(file: &mut File, count: u32) -> std::io::Result<()> { } impl LoopbackIpManager { - fn install( - &mut self, - addresses: &[IpAddr], - ) -> Result<(), std::io::Error> { + fn install(&mut self, addresses: &[IpAddr]) -> Result<(), std::io::Error> { let ifname = self.ifname.clone(); let log = self.log.clone(); @@ -573,10 +570,7 @@ impl LoopbackIpManager { info!(log, "removed {addr} from system"); } Err(e) => { - error!( - log, - "failed to execute remove command for {addr}: {e}" - ); + error!(log, "failed to execute remove command for {addr}: {e}"); } } } From 52fb075c06393059186c71a0b6161b23b94aa3ca Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 8 Apr 2026 17:49:50 -0600 Subject: [PATCH 57/58] Review feedback --- bgp/src/connection_tcp.rs | 2 +- bgp/src/messages.rs | 62 ++++++++++++++------------------------- bgp/src/proptest.rs | 60 ++++++++++++++++++++----------------- docs/developer-guide.md | 5 ++-- mgadm/src/rib.rs | 2 ++ mgd/src/rib_admin.rs | 16 ++++++++++ rdb-types/src/lib.rs | 8 +++++ rdb/src/bestpath.rs | 2 +- rdb/src/db.rs | 4 ++- 9 files changed, 89 insertions(+), 72 deletions(-) diff --git a/bgp/src/connection_tcp.rs b/bgp/src/connection_tcp.rs index 3fbf6fb1b..95e25d82e 100644 --- a/bgp/src/connection_tcp.rs +++ b/bgp/src/connection_tcp.rs @@ -1082,7 +1082,7 @@ impl BgpConnectionTcp { "message" => msg.title(), "message_contents" => format!("{msg}") ); - let msg_buf = msg.to_wire(extended_msg)?; + let msg_buf = msg.to_wire()?; let length = u16::try_from(msg_buf.len() + Header::WIRE_SIZE).map_err(|_| { Error::TooLarge("BGP message being sent is too large".into()) diff --git a/bgp/src/messages.rs b/bgp/src/messages.rs index 9fb8dd433..685797fd5 100644 --- a/bgp/src/messages.rs +++ b/bgp/src/messages.rs @@ -295,10 +295,10 @@ pub enum Message { } impl Message { - pub fn to_wire(&self, extended_msg: bool) -> Result, Error> { + pub fn to_wire(&self) -> Result, Error> { match self { Self::Open(m) => m.to_wire(), - Self::Update(m) => m.to_wire(extended_msg), + Self::Update(m) => m.to_wire(), Self::Notification(m) => m.to_wire(), Self::KeepAlive => Ok(Vec::new()), Self::RouteRefresh(m) => Ok(m.to_wire()), @@ -935,7 +935,7 @@ impl UpdateMessage { + nlri } - pub fn to_wire(&self, extended_msg: bool) -> Result, Error> { + pub fn to_wire(&self) -> Result, Error> { let mut buf = Vec::new(); // withdrawn @@ -963,17 +963,6 @@ impl UpdateMessage { // nlri buf.extend_from_slice(&self.nlri_to_wire()?); - let max_size = if extended_msg { - MAX_EXTENDED_MESSAGE_SIZE - } else { - MAX_MESSAGE_SIZE - }; - if buf.len() > max_size { - return Err(Error::TooLarge( - "update exceeds max message size".into(), - )); - } - Ok(buf) } @@ -6313,7 +6302,7 @@ mod tests { errors: vec![], }; - let buf = um0.to_wire(false).expect("update message to wire"); + let buf = um0.to_wire().expect("update message to wire"); println!("buf: {}", buf.hex_dump()); let um1 = @@ -6357,18 +6346,20 @@ mod tests { "test update must exceed standard body limit" ); - // Extended: encodes and round-trips successfully - let buf = um.to_wire(true).expect("extended update should encode"); - let decoded = UpdateMessage::from_wire(&buf) - .expect("extended update should decode"); + // Encodes and round-trips successfully + let buf = um.to_wire().expect("encode"); + let decoded = UpdateMessage::from_wire(&buf).expect("decode"); assert_eq!(um, decoded); - // Standard: encoding fails because the body exceeds 4096 - assert!(um.to_wire(false).is_err()); + // Size enforcement for non-extended messages is handled by + // Header::new(), not to_wire(). + let total = u16::try_from(buf.len() + Header::WIRE_SIZE).unwrap(); + assert!(Header::new(total, MessageType::Update, false).is_err()); + assert!(Header::new(total, MessageType::Update, true).is_ok()); } #[test] - fn small_update_works_with_or_without_extended() { + fn small_update_round_trip() { let um = UpdateMessage { withdrawn: vec![], path_attributes: vec![ @@ -6384,11 +6375,8 @@ mod tests { errors: vec![], }; - let buf_std = um.to_wire(false).expect("standard encode"); - let buf_ext = um.to_wire(true).expect("extended encode"); - assert_eq!(buf_std, buf_ext); - - let decoded = UpdateMessage::from_wire(&buf_ext).expect("decode"); + let buf = um.to_wire().expect("encode"); + let decoded = UpdateMessage::from_wire(&buf).expect("decode"); assert_eq!(um, decoded); } @@ -7140,7 +7128,7 @@ mod tests { }; // Encode to wire format - let wire = update.to_wire(false).expect("encoding should succeed"); + let wire = update.to_wire().expect("encoding should succeed"); // Skip withdrawn routes length (2 bytes) and empty withdrawn routes (0 bytes) // Skip path attributes length (2 bytes) @@ -7183,7 +7171,7 @@ mod tests { }; // Encode to wire and decode back - should succeed - let wire = update.to_wire(false).expect("encoding should succeed"); + let wire = update.to_wire().expect("encoding should succeed"); let decoded = UpdateMessage::from_wire(&wire); assert!( decoded.is_ok(), @@ -7242,7 +7230,7 @@ mod tests { }; // Encode to wire and decode back - should succeed - let wire = update.to_wire(false).expect("encoding should succeed"); + let wire = update.to_wire().expect("encoding should succeed"); let decoded = UpdateMessage::from_wire(&wire); assert!( decoded.is_ok(), @@ -7307,7 +7295,7 @@ mod tests { }; // Round-trip through wire format - let wire = update.to_wire(false).expect("encoding should succeed"); + let wire = update.to_wire().expect("encoding should succeed"); let decoded = UpdateMessage::from_wire(&wire).expect("decoding should succeed"); @@ -7372,9 +7360,7 @@ mod tests { errors: vec![], }; - let wire = empty_update - .to_wire(false) - .expect("encoding should succeed"); + let wire = empty_update.to_wire().expect("encoding should succeed"); let decoded = UpdateMessage::from_wire(&wire).expect("decoding should succeed"); @@ -7400,9 +7386,7 @@ mod tests { errors: vec![], }; - let wire = mp_eor_update - .to_wire(false) - .expect("encoding should succeed"); + let wire = mp_eor_update.to_wire().expect("encoding should succeed"); let decoded = UpdateMessage::from_wire(&wire).expect("decoding should succeed"); @@ -7438,9 +7422,7 @@ mod tests { errors: vec![], }; - let wire = mp_eor_v4_update - .to_wire(false) - .expect("encoding should succeed"); + let wire = mp_eor_v4_update.to_wire().expect("encoding should succeed"); let decoded = UpdateMessage::from_wire(&wire).expect("decoding should succeed"); diff --git a/bgp/src/proptest.rs b/bgp/src/proptest.rs index cd27346f4..34a221848 100644 --- a/bgp/src/proptest.rs +++ b/bgp/src/proptest.rs @@ -15,7 +15,7 @@ use crate::messages::{ As4PathSegment, AsPathType, BgpNexthop, BgpWireFormat, Header, Ipv6DoubleNexthop, MAX_EXTENDED_MESSAGE_SIZE, MAX_MESSAGE_SIZE, - MpReachNlri, MpUnreachNlri, PathAttribute, PathAttributeType, + MessageType, MpReachNlri, MpUnreachNlri, PathAttribute, PathAttributeType, PathAttributeTypeCode, PathAttributeValue, PathOrigin, UpdateMessage, path_attribute_flags, path_attrs_wire_len, split_prefixes_by_size, }; @@ -419,7 +419,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire(false).expect("should encode"); + let wire = update.to_wire().expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); prop_assert_eq!(decoded.nlri, prefixes, "NLRI prefixes should round-trip"); @@ -435,7 +435,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire(false).expect("should encode"); + let wire = update.to_wire().expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); prop_assert_eq!(decoded.withdrawn, prefixes, "Withdrawn prefixes should round-trip"); @@ -462,7 +462,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire(false).expect("should encode"); + let wire = update.to_wire().expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); // Extract MP_REACH_NLRI and verify prefixes @@ -496,7 +496,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire(false).expect("should encode"); + let wire = update.to_wire().expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); // Extract MP_UNREACH_NLRI and verify prefixes @@ -533,7 +533,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire(false).expect("should encode"); + let wire = update.to_wire().expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); // Extract MP_REACH_NLRI and verify prefixes @@ -567,7 +567,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire(false).expect("should encode"); + let wire = update.to_wire().expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); // Extract MP_UNREACH_NLRI and verify prefixes @@ -606,7 +606,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire(false).expect("should encode"); + let wire = update.to_wire().expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); // Extract and verify the next-hop @@ -633,7 +633,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire(false).expect("should encode"); + let wire = update.to_wire().expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); let decoded_nexthop = decoded.nexthop().expect("should have nexthop"); @@ -659,7 +659,7 @@ proptest! { errors: vec![], }; - let wire = update.to_wire(false).expect("should encode"); + let wire = update.to_wire().expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); let decoded_nexthop = decoded.nexthop().expect("should have nexthop"); @@ -673,7 +673,7 @@ proptest! { /// Property: Traditional UpdateMessage round-trip preserves structure #[test] fn prop_update_traditional_roundtrip(update in update_traditional_strategy()) { - let wire = update.to_wire(false).expect("should encode"); + let wire = update.to_wire().expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); prop_assert_eq!(decoded.withdrawn, update.withdrawn); @@ -689,7 +689,7 @@ proptest! { /// Property: MP-BGP UpdateMessage with MP_REACH_NLRI round-trip works #[test] fn prop_update_mp_reach_roundtrip(update in update_mp_reach_strategy()) { - let wire = update.to_wire(false).expect("should encode"); + let wire = update.to_wire().expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); // Should have MP_REACH_NLRI attribute @@ -702,7 +702,7 @@ proptest! { /// Property: MP-BGP UpdateMessage with MP_UNREACH_NLRI round-trip works #[test] fn prop_update_mp_unreach_roundtrip(update in update_mp_unreach_strategy()) { - let wire = update.to_wire(false).expect("should encode"); + let wire = update.to_wire().expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); // Should have MP_UNREACH_NLRI attribute @@ -719,7 +719,7 @@ proptest! { /// Property: MP-BGP attributes are always encoded first (RFC 7606 Section 5.1) #[test] fn prop_mp_bgp_attrs_encoded_first(update in update_mp_reach_strategy()) { - let wire = update.to_wire(false).expect("should encode"); + let wire = update.to_wire().expect("should encode"); // Skip to path attributes section // Wire format: 2 bytes withdrawn len + withdrawn + 2 bytes attrs len + attrs + nlri @@ -786,7 +786,7 @@ proptest! { /// Property: Encoding then decoding produces semantically equivalent message #[test] fn prop_encode_decode_semantic_equivalence(update in update_strategy()) { - let wire = update.to_wire(false).expect("should encode"); + let wire = update.to_wire().expect("should encode"); let decoded = UpdateMessage::from_wire(&wire).expect("should decode"); // Withdrawn and NLRI should be identical @@ -886,8 +886,8 @@ proptest! { }; // Encode and decode both - let traditional_wire = traditional_update.to_wire(false).expect("traditional encode"); - let mp_bgp_wire = mp_bgp_update.to_wire(false).expect("mp-bgp encode"); + let traditional_wire = traditional_update.to_wire().expect("traditional encode"); + let mp_bgp_wire = mp_bgp_update.to_wire().expect("mp-bgp encode"); let traditional_decoded = UpdateMessage::from_wire(&traditional_wire) .expect("traditional decode"); @@ -1165,7 +1165,7 @@ proptest! { #[test] fn prop_wire_len_matches_to_wire(update in update_strategy()) { let expected = update.wire_len(); - let wire = update.to_wire(false).expect("should encode"); + let wire = update.to_wire().expect("should encode"); prop_assert_eq!( wire.len(), expected, "wire_len() = {} but to_wire() produced {} bytes", @@ -1182,8 +1182,8 @@ proptest! { fn prop_extended_flag_no_effect_on_small_updates( update in update_strategy() ) { - let wire_std = update.to_wire(false).expect("standard encode"); - let wire_ext = update.to_wire(true).expect("extended encode"); + let wire_std = update.to_wire().expect("standard encode"); + let wire_ext = update.to_wire().expect("extended encode"); prop_assert_eq!( wire_std, wire_ext, "extended flag should not change encoding for small updates" @@ -1195,7 +1195,7 @@ proptest! { fn prop_extended_traditional_roundtrip( update in update_traditional_strategy() ) { - let wire = update.to_wire(true).expect("extended encode"); + let wire = update.to_wire().expect("extended encode"); let decoded = UpdateMessage::from_wire(&wire).expect("decode"); prop_assert_eq!(decoded.withdrawn, update.withdrawn); @@ -1207,7 +1207,7 @@ proptest! { fn prop_extended_mp_reach_roundtrip( update in update_mp_reach_strategy() ) { - let wire = update.to_wire(true).expect("extended encode"); + let wire = update.to_wire().expect("extended encode"); let decoded = UpdateMessage::from_wire(&wire).expect("decode"); let original_mp = update.mp_reach().expect("original mp_reach"); @@ -1240,11 +1240,17 @@ proptest! { update.wire_len() > MAX_MESSAGE_SIZE - Header::WIRE_SIZE ); - // Standard encoding must fail - prop_assert!(update.to_wire(false).is_err()); - // Extended encoding succeeds and round-trips - let wire = - update.to_wire(true).expect("extended encode"); + // Size enforcement is in Header::new, not to_wire(). + let wire = update.to_wire().expect("encode"); + let total = + u16::try_from(wire.len() + Header::WIRE_SIZE).unwrap(); + prop_assert!( + Header::new(total, MessageType::Update, false).is_err() + ); + prop_assert!( + Header::new(total, MessageType::Update, true).is_ok() + ); + // Round-trips let decoded = UpdateMessage::from_wire(&wire).expect("decode"); prop_assert_eq!(decoded.nlri, update.nlri); diff --git a/docs/developer-guide.md b/docs/developer-guide.md index 32eb2710f..0674cd0b6 100644 --- a/docs/developer-guide.md +++ b/docs/developer-guide.md @@ -7,7 +7,7 @@ support for illumos/Helios and Dendrite/Sidecar data planes. ## Tech Stack -- **Language**: Rust 1.92.0 (pinned in `rust-toolchain.toml`) +- **Language**: Rust (version pinned in `rust-toolchain.toml`) - **Async runtime**: Tokio via `oxide_tokio_rt::run` (clippy disallows `#[tokio::main]`) - **HTTP API framework**: Dropshot (server) + Progenitor (client @@ -51,7 +51,8 @@ support for illumos/Helios and Dendrite/Sidecar data planes. - **`rdb`** — Routing Information Base shared by BGP, BFD, static, DDM. Separate in/loc RIBs for IPv4/IPv6. Bestpath algorithm: shutdown filter -> RIB priority -> local_pref -> AS path length -> - MED. ECMP support. Persistent via sled at `/var/run/rdb`. + multi-exit discriminator (MED). Equal-cost multipath (ECMP) support. + Persistent via sled at `/var/run/rdb`. - **`rdb-types`** — Shared types for the RIB. - **`mg-lower`** — Syncs RIB to data plane (Dendrite). Watches RIB for changes, full sync on startup + incremental updates. Platform diff --git a/mgadm/src/rib.rs b/mgadm/src/rib.rs index 06cb6d11c..78aee3545 100644 --- a/mgadm/src/rib.rs +++ b/mgadm/src/rib.rs @@ -46,6 +46,7 @@ pub enum StatusCmd { #[arg(value_enum)] protocol: Option, /// Exact-match prefix filter (e.g. 10.0.0.0/24) + #[clap(long)] prefix: Option, /// Display mode: summary (default) or detail. /// Defaults to detail when a prefix is specified. @@ -63,6 +64,7 @@ pub enum StatusCmd { #[arg(value_enum)] protocol: Option, /// Exact-match prefix filter (e.g. 10.0.0.0/24) + #[clap(long)] prefix: Option, /// Display mode: summary (default) or detail. /// Defaults to detail when a prefix is specified. diff --git a/mgd/src/rib_admin.rs b/mgd/src/rib_admin.rs index 118fe8a7e..3cb9e5440 100644 --- a/mgd/src/rib_admin.rs +++ b/mgd/src/rib_admin.rs @@ -73,6 +73,14 @@ pub async fn get_rib_imported( let prefix: Prefix = prefix_str .parse() .map_err(|e: String| HttpError::for_bad_request(None, e))?; + if let Some(af) = query.address_family + && af != prefix.address_family() + { + return Err(HttpError::for_bad_request( + None, + "prefix/address_family mismatch".to_string(), + )); + } match ctx.db.get_imported_prefix(&prefix) { Some(paths) => BTreeMap::from([(prefix, paths)]), None => BTreeMap::new(), @@ -94,6 +102,14 @@ pub async fn get_rib_selected( let prefix: Prefix = prefix_str .parse() .map_err(|e: String| HttpError::for_bad_request(None, e))?; + if let Some(af) = query.address_family + && af != prefix.address_family() + { + return Err(HttpError::for_bad_request( + None, + "prefix/address_family mismatch".to_string(), + )); + } match ctx.db.get_selected_prefix(&prefix) { Some(paths) => BTreeMap::from([(prefix, paths)]), None => BTreeMap::new(), diff --git a/rdb-types/src/lib.rs b/rdb-types/src/lib.rs index eec8f59c6..8cd045fac 100644 --- a/rdb-types/src/lib.rs +++ b/rdb-types/src/lib.rs @@ -380,6 +380,14 @@ impl Prefix { matches!(self, Prefix::V4(_)) } + /// Return the address family corresponding to this prefix. + pub fn address_family(&self) -> AddressFamily { + match self { + Prefix::V4(_) => AddressFamily::Ipv4, + Prefix::V6(_) => AddressFamily::Ipv6, + } + } + /// Check if a prefix contains a subnet that is valid for use in the RIB. pub fn valid_for_rib(&self) -> bool { match self { diff --git a/rdb/src/bestpath.rs b/rdb/src/bestpath.rs index 6258ab9d1..f34928343 100644 --- a/rdb/src/bestpath.rs +++ b/rdb/src/bestpath.rs @@ -63,7 +63,7 @@ pub fn bgp_bestpaths( candidates: BTreeSet<&Path>, max: usize, ) -> BTreeSet { - // Per RFC 4721 Section 9.1.2.2: + // Per RFC 4271 Section 9.1.2.2: // """ // The tie-breaking algorithm begins by considering all equally // preferable routes to the same destination, and then selects routes to diff --git a/rdb/src/db.rs b/rdb/src/db.rs index 155ab35c0..4603a7fc9 100644 --- a/rdb/src/db.rs +++ b/rdb/src/db.rs @@ -1051,7 +1051,9 @@ impl Db { false }; - self.recompute_rib_loc(rib_in, rib_loc, prefix); + if changed { + self.recompute_rib_loc(rib_in, rib_loc, prefix); + } changed } From 3d44036dea0a79cda4ba0dbbd943f1602d4507b8 Mon Sep 17 00:00:00 2001 From: Trey Aspelund Date: Wed, 8 Apr 2026 17:43:00 -0600 Subject: [PATCH 58/58] bgp: disallow OPEN/KEEPALIVE from extended msg Per RFC 8654, Extended Message Size does not apply to OPEN and KEEPALIVE messages. Enforce this in Header::new(). --- bgp/src/messages.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/bgp/src/messages.rs b/bgp/src/messages.rs index 685797fd5..227d81ae8 100644 --- a/bgp/src/messages.rs +++ b/bgp/src/messages.rs @@ -496,8 +496,10 @@ impl Header { pub const WIRE_SIZE: usize = 19; /// Create a new BGP message header. - /// Length must be between 19 and 4096 bytes (RFC 4271 §4.1) unless extended - /// message size is permitted, in which case the max size is 65535 bytes. + /// Length must be between 19 and 4096 bytes (RFC 4271 §4.1) unless + /// extended message size is permitted, in which case the max size + /// is 65535 bytes. Per RFC 8654 §4, extended messages apply to all + /// types except OPEN and KEEPALIVE. pub fn new( length: u16, typ: MessageType, @@ -506,7 +508,11 @@ impl Header { if usize::from(length) < Header::WIRE_SIZE { return Err(Error::TooSmall("message header length".into())); } - if !extended_msg && usize::from(length) > MAX_MESSAGE_SIZE { + // RFC 8654 §4: only UPDATE, NOTIFICATION, and ROUTE-REFRESH + // may use extended message size. + let ext_eligible = extended_msg + && !matches!(typ, MessageType::Open | MessageType::KeepAlive); + if !ext_eligible && usize::from(length) > MAX_MESSAGE_SIZE { return Err(Error::TooLarge("message header length".into())); } Ok(Header { length, typ })