bgp_models/mrt/tabledump.rs
1//! MRT table dump version 1 and 2 structs
2use crate::bgp::Attribute;
3use crate::network::{Afi, Asn, NetworkPrefix, Safi};
4use serde::Serialize;
5use std::collections::HashMap;
6use std::net::{IpAddr, Ipv4Addr};
7
8/// TableDump message version 1
9#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
10pub struct TableDumpMessage {
11 pub view_number: u16,
12 pub sequence_number: u16,
13 pub prefix: NetworkPrefix,
14 pub status: u8,
15 pub originated_time: u64,
16 pub peer_address: IpAddr,
17 pub peer_asn: Asn,
18 pub attributes: Vec<Attribute>,
19}
20
21/// TableDump message version 2 enum
22#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
23pub enum TableDumpV2Message {
24 PeerIndexTable(PeerIndexTable),
25 RibAfiEntries(RibAfiEntries),
26 RibGenericEntries(RibGenericEntries),
27}
28
29/// TableDump version 2 subtypes.
30///
31/// <https://www.iana.org/assignments/mrt/mrt.xhtml#subtype-codes>
32#[derive(Debug, Primitive, Copy, Clone, Serialize, PartialEq, Eq)]
33pub enum TableDumpV2Type {
34 PeerIndexTable = 1,
35 RibIpv4Unicast = 2,
36 RibIpv4Multicast = 3,
37 RibIpv6Unicast = 4,
38 RibIpv6Multicast = 5,
39 RibGeneric = 6,
40 GeoPeerTable = 7,
41 RibIpv4UnicastAddPath = 8,
42 RibIpv4MulticastAddPath = 9,
43 RibIpv6UnicastAddPath = 10,
44 RibIpv6MulticastAddPath = 11,
45 RibGenericAddPath = 12,
46}
47
48/// AFI/SAFI-Specific RIB Subtypes.
49///
50/// ```text
51/// The AFI/SAFI-specific RIB Subtypes consist of the RIB_IPV4_UNICAST,
52/// RIB_IPV4_MULTICAST, RIB_IPV6_UNICAST, and RIB_IPV6_MULTICAST
53/// Subtypes. These specific RIB table entries are given their own MRT
54/// TABLE_DUMP_V2 subtypes as they are the most common type of RIB table
55/// instances, and providing specific MRT subtypes for them permits more
56/// compact encodings. These subtypes permit a single MRT record to
57/// encode multiple RIB table entries for a single prefix. The Prefix
58/// Length and Prefix fields are encoded in the same manner as the BGP
59/// NLRI encoding for IPv4 and IPv6 prefixes. Namely, the Prefix field
60/// contains address prefixes followed by enough trailing bits to make
61/// the end of the field fall on an octet boundary. The value of
62/// trailing bits is irrelevant.
63///
64/// 0 1 2 3
65/// 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
66/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67/// | Sequence Number |
68/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69/// | Prefix Length |
70/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71/// | Prefix (variable) |
72/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73/// | Entry Count | RIB Entries (variable)
74/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
75/// ```
76#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
77pub struct RibAfiEntries {
78 pub rib_type: TableDumpV2Type,
79 pub sequence_number: u32,
80 pub prefix: NetworkPrefix,
81 pub rib_entries: Vec<RibEntry>,
82}
83
84/// RIB generic entries subtype.
85///
86/// ```text
87/// The RIB_GENERIC header is shown below. It is used to cover RIB
88/// entries that do not fall under the common case entries defined above.
89/// It consists of an AFI, Subsequent AFI (SAFI), and a single NLRI
90/// entry. The NLRI information is specific to the AFI and SAFI values.
91/// An implementation that does not recognize particular AFI and SAFI
92/// values SHOULD discard the remainder of the MRT record.
93/// 0 1 2 3
94/// 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
95/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96/// | Sequence Number |
97/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98/// | Address Family Identifier |Subsequent AFI |
99/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100/// | Network Layer Reachability Information (variable) |
101/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102/// | Entry Count | RIB Entries (variable)
103/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104/// ```
105#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
106pub struct RibGenericEntries {
107 pub sequence_number: u32,
108 pub afi: Afi,
109 pub safi: Safi,
110 pub nlri: NetworkPrefix,
111 pub rib_entries: Vec<RibEntry>,
112}
113
114/// RIB entry.
115///
116/// ```text
117/// The RIB Entries are repeated Entry Count times. These entries share
118/// a common format as shown below. They include a Peer Index from the
119/// PEER_INDEX_TABLE MRT record, an originated time for the RIB Entry,
120/// and the BGP path attribute length and attributes. All AS numbers in
121/// the AS_PATH attribute MUST be encoded as 4-byte AS numbers.
122///
123/// 0 1 2 3
124/// 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
125/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
126/// | Peer Index |
127/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
128/// | Originated Time |
129/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130/// | Attribute Length |
131/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132/// | BGP Attributes... (variable)
133/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134/// ```
135#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
136pub struct RibEntry {
137 pub peer_index: u16,
138 pub originated_time: u32,
139 pub attributes: Vec<Attribute>,
140}
141
142/// peer index table.
143///
144/// ```text
145/// An initial PEER_INDEX_TABLE MRT record provides the BGP ID of the
146/// collector, an OPTIONAL view name, and a list of indexed peers.
147/// Following the PEER_INDEX_TABLE MRT record, a series of MRT records is
148/// used to encode RIB table entries. This series of MRT records uses
149/// subtypes 2-6 and is separate from the PEER_INDEX_TABLE MRT record
150/// itself and includes full MRT record headers. The RIB entry MRT
151/// records MUST immediately follow the PEER_INDEX_TABLE MRT record.
152/// ```
153#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
154pub struct PeerIndexTable {
155 pub collector_bgp_id: Ipv4Addr,
156 pub view_name_length: u16,
157 pub view_name: String,
158 pub peer_count: u16,
159 pub peers_map: HashMap<u32, Peer>,
160}
161
162/// Peer struct.
163#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
164pub struct Peer {
165 pub peer_type: u8,
166 pub peer_bgp_id: Ipv4Addr,
167 pub peer_address: IpAddr,
168 pub peer_asn: Asn,
169}