BGP-4 Protocol representation and wire format serialization/deserialization (serde)
NetGauze BGP Pkt is library to represent BGP-4 Packets. It aims to achieve 4 goals
To run example: cargo run --example bgp
```rust use std::io::Cursor; use std::net::Ipv4Addr;
use netgauzebgppkt::capabilities::; use netgauze_bgp_pkt::open::; use netgauzebgppkt::; use netgauze_iana::address_family::; use netgauzeparseutils::{ReadablePDUWithOneInput, Span, WritablePDU};
pub fn main() { // Construct a new BGP message let msg = BgpMessage::Open(BgpOpenMessage::new( 100, 180, Ipv4Addr::new(5, 5, 5, 5), vec![ BgpOpenMessageParameter::Capabilities(vec![BgpCapability::MultiProtocolExtensions( MultiProtocolExtensionsCapability::new(AddressType::Ipv4Unicast), )]), BgpOpenMessageParameter::Capabilities(vec![BgpCapability::MultiProtocolExtensions( MultiProtocolExtensionsCapability::new(AddressType::Ipv4MplsLabeledVpn), )]), BgpOpenMessageParameter::Capabilities(vec![BgpCapability::Unrecognized( UnrecognizedCapability::new(128, vec![]), )]), BgpOpenMessageParameter::Capabilities(vec![BgpCapability::RouteRefresh]), BgpOpenMessageParameter::Capabilities(vec![BgpCapability::FourOctetAs( FourOctetAsCapability::new(100), )]), BgpOpenMessageParameter::Capabilities(vec![BgpCapability::ExtendedNextHopEncoding( ExtendedNextHopEncodingCapability::new(vec![ ExtendedNextHopEncoding::new(AddressType::Ipv4Unicast, AddressFamily::IPv6), ExtendedNextHopEncoding::new(AddressType::Ipv4Multicast, AddressFamily::IPv6), ExtendedNextHopEncoding::new( AddressType::Ipv4MplsLabeledVpn, AddressFamily::IPv6, ), ]), )]), ], ));
println!("JSON representation of BGP packet: {}", serdejson::tostring(&msg).unwrap());
// Serialize the message into it's BGP binary format
let mut buf: Vec<u8> = vec![];
let mut cursor = Cursor::new(&mut buf);
msg.write(&mut cursor).unwrap();
assert_eq!(
buf,
vec![
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 83,
1, 4, 0, 100, 0, 180, 5, 5, 5, 5, 54, 2, 6, 1, 4, 0, 1, 0, 1, 2, 6, 1, 4, 0, 1, 0, 128,
2, 2, 128, 0, 2, 2, 2, 0, 2, 6, 65, 4, 0, 0, 0, 100, 2, 20, 5, 18, 0, 1, 0, 1, 0, 2, 0,
1, 0, 2, 0, 2, 0, 1, 0, 128, 0, 2
]
);
// Deserialize the message from binary format
let (_, msg_back) = BgpMessage::from_wire(Span::new(&buf), true).unwrap();
assert_eq!(msg, msg_back);
} ```
| Message Type | RFCs | notes | |--------------|-------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------| | Open | RFC 4271 | See below for the supported capabilities | | Update | RFC 4271 | See below for the supported path attributes | | Notification | RFC 4271 | See below for the supported notif sub-codes | | KeepAlive | RFC 4271 | | | RouteRefresh | RFC 2918 and RFC 7313 | See below for the supported Route refresh ops |
| Capability | RFCs | Notes |
|----------------------------------|-----------------------------------------------------------|---------------------------------------------------------------------------------------------|
| MultiProtocolExtensions (MP-BGP) | RFC 4760 | See MP-BGP supported address families below |
| RouteRefresh | RFC 2918 | |
| EnhancedRouteRefresh | RFC 7313 | |
| Add Path | RFC 7911 | |
| Extended Message | RFC 8654 | |
| Four Octet AS Number | RFC 6793 | |
| Extended Next Hop Encoding | RFC 8950 | |
| Experimental | RFC 8810 | Capabilities with codes 239-254 are marked as experimental, we read their values as Vecu8
vector for it's value |
| Path Attribute | RFCs | Well-known | Optional | transitive | Notes |
|------------------------------|-------------------------------------------------------------------------------------------------------------------------|------------|----------|------------|--------------------------------------------------------------------|
| Origin | RFC 4271 | Yes | No | Yes | |
| ASPATH | RFC 4271 and RFC 6793 | Yes | No | Yes | |
| NEXTHOP | RFC 4271 | Yes | No | Yes | |
| MultiExitDiscriminator (MED) | RFC 4271 | Yes | Yes | No | |
| Local Preference (LocalPref) | RFC 4271 | Yes | | Yes | |
| Atomic Aggregate | RFC 4271 | Yes | Yes | Yes | |
| Aggregator | RFC 4271 | Yes | Yes | Yes | |
| Communities | RFC 1997 | No | Yes | Yes | |
| Extended Communities | RFC 4360 | No | Yes | Yes | |
| Large Communities | RFC 8092 | No | Yes | Yes | |
| Route Reflection | RFC 4456 | No | Yes | No | |
| Four Octet ASPATH | RFC 6793 | No | Yes | Yes | |
| MPREACHNLRI | RFC 4760 | No | Yes | No | |
| MPUNREACH_NLRI | RFC 4760 | No | Yes | No | |
| UnknownAttribute | | N/A | N/A | N/A | Catch all attribute that will read and keep the value as a Vec
| RFC | Address Family (AFI) | Subsequence Address Family (SAFI) | Notes | |-----------------------------------------------------------|----------------------|-----------------------------------|-------| | | 1 = IPv4 | 1 = Unicast | | | | 1 = IPv4 | 2 = Multicast | | | RFC 4364 | 1 = IPv4 | 128 = MPLS Labeled Unicast | | | | 2 = IPv6 | 1 = Unicast | | | | 2 = IPv6 | 2 = Multicast | | | RFC 4659 | 2 = IPv4 | 128 = MPLS Labeled Unicast | |
Running Packet Serde benchmarks
cargo bench --features bench