All Projects → bgpkit → bgpkit-parser

bgpkit / bgpkit-parser

Licence: MIT license
MRT/BGP data parser written in Rust.

Programming Languages

rust
11053 projects
shell
77523 projects

Labels

Projects that are alternatives of or similar to bgpkit-parser

mabo
MRT Parser
Stars: ✭ 46 (+6.98%)
Mutual labels:  bgp, mrt
ICNS2ICO
ICNS2ICO lets you easily convert icons from the Apple's ICNS format to the Windows ICO format.
Stars: ✭ 17 (-60.47%)
Mutual labels:  bmp
Asn
ASN / RPKI validity / BGP stats / IPv4v6 / Prefix / URL / ASPath / Organization / IP reputation and geolocation lookup tool / Traceroute server
Stars: ✭ 242 (+462.79%)
Mutual labels:  bgp
invalidroutesreporter
An ExaBGP process to elaborate and report/log invalid routes received by route servers.
Stars: ✭ 14 (-67.44%)
Mutual labels:  bgp
MRTScheduleSwiftUI
MRT Schedule & Locator iOS App built using SwiftUI
Stars: ✭ 32 (-25.58%)
Mutual labels:  mrt
rtrlib
An open-source C implementation of the RPKI/Router Protocol client
Stars: ✭ 62 (+44.19%)
Mutual labels:  bgp
Looking Glass
Easy to deploy Looking Glass
Stars: ✭ 233 (+441.86%)
Mutual labels:  bgp
goplane
an agent for configuring linux network stack via GoBGP
Stars: ✭ 112 (+160.47%)
Mutual labels:  bgp
StbSharp
C# port of the famous C framework
Stars: ✭ 62 (+44.19%)
Mutual labels:  bmp
pocketinternet
A Pocket Internet for teaching how the Internet really works.
Stars: ✭ 28 (-34.88%)
Mutual labels:  bgp
calico-bgp-daemon
GoBGP based Calico BGP Daemon
Stars: ✭ 23 (-46.51%)
Mutual labels:  bgp
StegX
Steganography (BMP, PNG, WAV, MP3, AVI, FLV)
Stars: ✭ 22 (-48.84%)
Mutual labels:  bmp
netbox-bgp
NetBox plugin for BGP related objects documentation
Stars: ✭ 135 (+213.95%)
Mutual labels:  bgp
hybridnet
A CNI plugin, provides networking environment where overlay and underlay containers can run on the same node and have cluster-wide bidirectional network connectivity.
Stars: ✭ 188 (+337.21%)
Mutual labels:  bgp
sail
The missing small and fast image decoding library for humans (not for machines) ⛵ https://sail.software
Stars: ✭ 206 (+379.07%)
Mutual labels:  bmp
Hyperglass
hyperglass is the network looking glass that tries to make the internet better.
Stars: ✭ 242 (+462.79%)
Mutual labels:  bgp
hyperglass
hyperglass is the network looking glass that tries to make the internet better.
Stars: ✭ 478 (+1011.63%)
Mutual labels:  bgp
terraform-metal-kubernetes-bgp
Kubernetes on Equinix Metal using Calico and MetalLB
Stars: ✭ 110 (+155.81%)
Mutual labels:  bgp
rpki-client-portable
Portability shim for OpenBSD's rpki-client
Stars: ✭ 33 (-23.26%)
Mutual labels:  bgp
pilot
Simple web-based SDN controller for family and friends
Stars: ✭ 33 (-23.26%)
Mutual labels:  bgp

BGPKIT Parser

Rust Crates.io Docs.rs License Discord

BGPKIT Parser aims to provides the most ergonomic MRT/BGP/BMP message parsing Rust API.

BGPKIT Parser has the following features:

  • performant: comparable to C-based implementations like bgpdump or bgpreader.
  • actively maintained: we consistently introduce feature updates and bug fixes, and support most of the relevant BGP RFCs.
  • ergonomic API: a three-line for loop can already get you started.
  • battery-included: ready to handle remote or local, bzip2 or gz data files out of the box

Examples

For complete examples, check out the examples folder.

Parsing single MRT file

Let's say we want to print out all the BGP announcements/withdrawal from a single MRT file, either located remotely or locally. Here is an example that does so.

use bgpkit_parser::BgpkitParser;
fn main() {
    let parser = BgpkitParser::new("http://archive.routeviews.org/bgpdata/2021.10/UPDATES/updates.20211001.0000.bz2");
    for elem in parser {
        println!("{}", elem)
    }
}

Yes, it is this simple!

You can even do some more interesting iterator operations that are event shorter. For example, counting the number of announcements/withdrawals in that file:

use bgpkit_parser::BgpkitParser;
fn main() {
    let url = "http://archive.routeviews.org/bgpdata/2021.10/UPDATES/updates.20211001.0000.bz2";
    let count = BgpkitParser::new(url).into_iter().count();
    println!("total: {}", count);
}

and it prints out

total: 255849

Parsing multiple MRT files with BGPKIT Broker

BGPKIT Broker library provides search API for all RouteViews and RIPE RIS MRT data files. Using the broker's Rust API (bgpkit-broker), we can easily compile a list of MRT files that we are interested in for any time period and any data type (update or rib). This allows users to gather information without needing to know about locations of specific data files.

The example below shows a relatively more interesting example that does the following:

  • find all BGP archive data created on time 1634693400
  • filter to only BGP updates files
  • find all announcements originated from AS13335
  • print out the total count of the announcements
fn main(){
    // set broker query parameters
    let mut params = bgpkit_broker::QueryParams::new();
    params = params.start_ts(1634693400);
    params = params.end_ts(1634693400);
    params = params.data_type("update");
    let mut broker = bgpkit_broker::BgpkitBroker::new("https://api.broker.bgpkit.com/v1");
    broker.set_params(&params);

    // loop through data files found by broker
    for item in broker {
        
        // create a parser that takes an URL and automatically determine
        // the file location and file type, and handles data download and
        // decompression streaming intelligently
        let parser = BgpkitParser::new(item.url.as_str());

        // iterating through the parser. the iterator returns `BgpElem` one at a time.
        let elems = parser.into_elem_iter().map(|elem|{
            if let Some(origins) = &elem.origin_asns {
                if origins.contains(&13335) {
                    Some(elem)
                } else {
                    None
                }
            } else {
                None
            }
        }).filter_map(|x|x).collect::<Vec<BgpElem>>();
        log::info!("{} elems matches", elems.len());
    }
}

Filtering BGP Messages

BGPKIT Parser also has built-in filtering mechanism. When creating a new BgpkitParser instance, once can also call add_filter function to customize the parser to only show matching messages when iterating through BgpElems.

use bgpkit_parser::BgpkitParser;

/// This example shows how to parse a MRT file and filter by prefix.
fn main() {
    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();

    log::info!("downloading updates file");

    // create a parser that takes the buffered reader
    let parser = BgpkitParser::new("http://archive.routeviews.org/bgpdata/2021.10/UPDATES/updates.20211001.0000.bz2").unwrap()
        .add_filter("prefix", "211.98.251.0/24").unwrap();

    log::info!("parsing updates file");
    // iterating through the parser. the iterator returns `BgpElem` one at a time.
    for elem in parser {
        log::info!("{}", &elem);
    }
    log::info!("done");
}

Parsing Real-time Data Streams

BGPKIT Parser also provides parsing functionalities for real-time data streams, including RIS-Live and BMP/OpenBMP messages. See the examples below and the documentation for more.

Parsing Messages From RIS-Live

Here is an example of handling RIS-Live message streams. After connecting to the websocket server, we need to subscribe to a specific data stream. In this example, we subscribe to the data stream from on collector (rrc21). We can then loop and read messages from the websocket.

use bgpkit_parser::parse_ris_live_message;
use serde_json::json;
use tungstenite::{connect, Message};
use url::Url;

const RIS_LIVE_URL: &str = "ws://ris-live.ripe.net/v1/ws/?client=rust-bgpkit-parser";

/// This is an example of subscribing to RIS-Live's streaming data from one host (`rrc21`).
///
/// For more RIS-Live details, check out their documentation at https://ris-live.ripe.net/manual/
fn main() {
    // connect to RIPE RIS Live websocket server
    let (mut socket, _response) =
        connect(Url::parse(RIS_LIVE_URL).unwrap())
            .expect("Can't connect to RIS Live websocket server");

    // subscribe to messages from one collector
    let msg = json!({"type": "ris_subscribe", "data": {"host": "rrc21"}}).to_string();
    socket.write_message(Message::Text(msg)).unwrap();

    loop {
        let msg = socket.read_message().expect("Error reading message").to_string();
        if let Ok(elems) = parse_ris_live_message(msg.as_str()) {
            for elem in elems {
                println!("{}", elem);
            }
        }
    }
}

Parsing OpenBMP Messages From RouteViews Kafka Stream

RouteViews provides a real-time Kafka stream of the OpenBMP data received from their collectors. Below is an partial example of how we handle the raw bytes received from the Kafka stream. For full examples, check out the examples folder on GitHub.

let bytes = m.value;
let mut reader = Cursor::new(Vec::from(bytes));
let header = parse_openbmp_header(&mut reader).unwrap();
let bmp_msg = parse_bmp_msg(&mut reader);
match bmp_msg {
    Ok(msg) => {
        let timestamp = header.timestamp;
        let per_peer_header = msg.per_peer_header.unwrap();
        match msg.message_body {
            MessageBody::RouteMonitoring(m) => {
                for elem in Elementor::bgp_to_elems(
                    m.bgp_message,
                    timestamp,
                    &per_peer_header.peer_ip,
                    &per_peer_header.peer_asn
                )
                {
                    info!("{}", elem);
                }
            }
            _ => {}
        }
    }
    Err(_e) => {
        let hex = hex::encode(bytes);
        error!("{}", hex);
        break
    }
}

Command Line Tool

bgpkit-parser is bundled with a utility commandline tool bgpkit-parser-cli.

You can install the tool by running

cargo install bgpkit-parser

or checkout this repository and run

cargo install --path .
➜  cli git:(cli) ✗ bgpkit-parser-cli 0.1.0

Mingwei Zhang <[email protected]>

bgpkit-parser-cli is a simple cli tool that allow parsing of individual MRT files

USAGE:
    bgpkit-parser-cli [FLAGS] [OPTIONS] <FILE>

FLAGS:
    -e, --elems-count      Count BGP elems
    -h, --help             Prints help information
        --json             Output as JSON objects
        --pretty           Pretty-print JSON output
    -r, --records-count    Count MRT records
    -V, --version          Prints version information

OPTIONS:
    -a, --as-path <as-path>          Filter by AS path regex string
    -m, --elem-type <elem-type>      Filter by elem type: announce (a) or withdraw (w)
    -T, --end-ts <end-ts>            Filter by end unix timestamp inclusive
    -o, --origin-asn <origin-asn>    Filter by origin AS Number
    -J, --peer-asn <peer-asn>        Filter by peer IP ASN
    -j, --peer-ip <peer-ip>          Filter by peer IP address
    -p, --prefix <prefix>            Filter by network prefix
    -t, --start-ts <start-ts>        Filter by start unix timestamp inclusive

Data Representation

There are two key data structure to understand for the parsing results:MrtRecord and BgpElem.

MrtRecord: unmodified MRT information representation

The MrtRecord is the data strcutrue that holds the unmodified, complete information parsed from the MRT data file. The code definition of the MrtRecord is defined in the crate bgp-models (documentation).

pub struct MrtRecord {
    pub common_header: CommonHeader,
    pub message: MrtMessage,
}

pub enum MrtMessage {
    TableDumpMessage(TableDumpMessage),
    TableDumpV2Message(TableDumpV2Message),
    Bgp4Mp(Bgp4Mp),
}

MrtRecord record representation is concise, storage efficient, but often less convenient to use. For example, when trying to find out specific BGP announcements for certain IP prefix, we often needs to go through nested layers of internal data structure (NLRI, announced, prefix, or even looking up peer index table for Table Dump V2 format), which could be irrelevant to what users really want to do.

BgpElem: per-prefix BGP information, MRT-format-agnostic

To facilitate simpler data analysis of BGP data, we defined a new data structure called BgpElem in this crate. Each BgpElem contains a piece of self-containing BGP information about one single IP prefix. For example, when a bundled announcement of three prefixes P1, P2, P3 that shares the same AS path is processed, we break the single record into three different BgpElem objects, each presenting a prefix.

pub struct BgpElem {
    pub timestamp: f64,
    pub elem_type: ElemType,
    pub peer_ip: IpAddr,
    pub peer_asn: Asn,
    pub prefix: NetworkPrefix,
    pub next_hop: Option<IpAddr>,
    pub as_path: Option<AsPath>,
    pub origin_asns: Option<Vec<Asn>>,
    pub origin: Option<Origin>,
    pub local_pref: Option<u32>,
    pub med: Option<u32>,
    pub communities: Option<Vec<Community>>,
    pub atomic: Option<AtomicAggregate>,
    pub aggr_asn: Option<Asn>,
    pub aggr_ip: Option<IpAddr>,
}

The main benefit of using BgpElem is that the analysis can be executed on a per-prefix basis, generic to what the backend MRT data format (bgp4mp, tabledumpv1, tabledumpv2, etc.). The obvious drawback is that we will have to duplicate information to save at each elem, that consuming more memory.

RFCs Support

We support most of the RFCs and plan to continue adding support for more recent RFCs in the future. Here is a list of relevant RFCs that we support or plan to add support.

If you would like to see any specific RFC's support, please submit an issue on GitHub.

BGP

  • RFC 2042: Registering New BGP Attribute Types
  • RFC 3392: Capabilities Advertisement with BGP-4
  • RFC 4271: A Border Gateway Protocol 4 (BGP-4)
  • RFC 5065: Autonomous System Confederations for BGP
  • RFC 6793: BGP Support for Four-Octet Autonomous System (AS) Number Space
  • RFC 7911: Advertisement of Multiple Paths in BGP (ADD-PATH)
  • RFC 8950: Advertising IPv4 Network Layer Reachability Information (NLRI) with an IPv6 Next Hop
  • RFC 9072: Extended Optional Parameters Length for BGP OPEN Message Updates

MRT

  • RFC 6396: Multi-Threaded Routing Toolkit (MRT) Routing Information Export Format
  • RFC 6397: Multi-Threaded Routing Toolkit (MRT) Border Gateway Protocol (BGP) Routing Information Export Format with Geo-Location Extensions
  • RFC 8050: Multi-Threaded Routing Toolkit (MRT) Routing Information Export Format with BGP Additional Path Extensions

BMP

  • RFC 7854: BGP Monitoring Protocol (BMP)
  • RFC 8671: Support for Adj-RIB-Out in the BGP Monitoring Protocol (BMP)

Communities

We support normal communities, extended communities, and large communities.

  • RFC 1977: BGP Communities Attribute
  • RFC 4360: BGP Extended Communities Attribute
  • RFC 5668: 4-Octet AS Specific BGP Extended Community
  • RFC 5701: IPv6 Address Specific BGP Extended Community Attribute
  • RFC 7153: IANA Registries for BGP Extended Communities Updates 4360, 5701
  • RFC 8097: BGP Prefix Origin Validation State Extended Community
  • RFC 8092: BGP Large Communities

FlowSpec

  • RFC 8955 Dissemination of Flow Specification Rules
  • RFC 8956 Dissemination of Flow Specification Rules for IPv6
  • RFC 9117 Revised Validation Procedure for BGP Flow Specifications Updates 8955

Minimum Supported Rust Version (MSRV)

1.48.0

Contribution

Issues and pull requests are welcome!

Built with ❤️ by BGPKIT Team

BGPKIT is a small-team start-up that focus on building the best tooling for BGP data in Rust. We have 10 years of experience working with BGP data and believe that our work can enable more companies to start keeping tracks of BGP data on their own turf. Learn more about what services we provide at https://bgpkit.com.

https://bgpkit.com/favicon.ico

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].