bgpflux 0.3.0

A Rust library and CLI for streaming ordered BGP elements from multiple collectors
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! # bgpflux
//!
//! A Rust library and CLI for streaming ordered BGP elements from multiple route collectors.
//!
//! bgpflux merges BGP data from RIPE RIS and RouteViews collectors in chronological order,
//! supporting both historical archives and real-time feeds.
//!
//! ## Features
//!
//! - **Archive & Live streaming**: Historical data via [BGPKIT Broker](https://bgpkit.com/),
//!   real-time data via RIS Live (WebSocket) and RouteViews Live (Kafka)
//! - **Sorted output**: Elements from multiple collectors are merged in timestamp order
//! - **Filtering**: Origin ASN, prefix, peer IP/ASN, AS path regex, community, IP version
//! - **Caching**: Optional local file caching to skip re-downloading archive data
//! - **Jitter buffer**: Reorder live stream elements with a configurable delay window
//!
//! ## Quick Start — Archive
//!
//! ```no_run
//! use bgpflux::{BgpStream, BgpStreamConfig, DataType};
//!
//! let config = BgpStreamConfig::new(
//!     "2025-01-15T12:00:00Z",
//!     "2025-01-15T13:00:00Z",
//!     &["route-views.wide", "rrc04"],
//!     DataType::Update,
//! )?;
//!
//! let stream = BgpStream::new(config).build();
//!
//! for elem in stream {
//!     println!("{}", elem);
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ## Quick Start — Live (requires `live` feature)
//!
//! ```ignore
//! use bgpflux::{LiveBgpStream, LiveConfig, JitterBufferExt};
//! use std::time::Duration;
//!
//! let config = LiveConfig::new(&["rrc00", "route-views2"])?;
//! let stream = LiveBgpStream::new(config)
//!     .build()
//!     .jitter_buffer(Duration::from_secs(15));
//!
//! for elem in stream {
//!     println!("{}", elem);
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ## Core Components
//!
//! - **[`BgpStream`]**: Streams historical BGP data from archives
//! - **[`BgpStreamConfig`]**: Configuration for archive streams (time range, collectors, data type)
//! - **[`BgpStreamElem`]**: A single BGP element with collector metadata
//!
//! With the `live` feature enabled:
//! - **`LiveBgpStream`**: Streams real-time BGP data from RIS Live and RouteViews Live
//! - **`LiveConfig`**: Configuration for live streams (collectors)
//! - **`JitterBufferExt`**: Extension trait to reorder live stream elements by timestamp
//!
//! ## Acknowledgments
//!
//! This project uses code copied or  adapted from:
//! - [bgpkit-broker](https://github.com/bgpkit/bgpkit-broker)
//! - [bgpkit-parser](https://github.com/bgpkit/bgpkit-parser)

pub mod config;
pub mod elem;
#[cfg(any(feature = "live-ris", feature = "live-routeviews"))]
pub mod live;
mod parser_utils;
pub mod runtime;
mod utils;

use bgpkit_broker::BgpkitBroker;
use chrono::DateTime;
pub use config::{BgpStreamConfig, DataType};
pub use elem::{BgpStreamElem, BgpStreamElemType};
use itertools::Either;
use itertools::Itertools;
#[cfg(any(feature = "live-ris", feature = "live-routeviews"))]
pub use live::{JitterBufferExt, LiveBgpStream, LiveConfig};
use parser_utils::{init_parser_retry, process_parser_to_elems};
use runtime::{download_semaphore, global_runtime, intern_collector};
use std::sync::Arc;
use std::{collections::HashMap, fmt::Display};

/// Makes compiler happy when branching in `build`
enum BgpStreamIter<I1, I2> {
    WithCache(I1),
    NoCache(I2),
}
impl<I1, I2> Iterator for BgpStreamIter<I1, I2>
where
    I1: Iterator<Item = BgpStreamElem>,
    I2: Iterator<Item = BgpStreamElem>,
{
    type Item = BgpStreamElem;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::WithCache(i) => i.next(),
            Self::NoCache(i) => i.next(),
        }
    }
}

/// The main streaming interface for BGP elements from multiple collectors.
///
/// # Examples
///
/// Basic streaming from multiple collectors:
///
/// ```no_run
/// use bgpflux::{BgpStream, BgpStreamConfig, DataType};
///
/// let config = BgpStreamConfig::new(
///     "2010-09-01T00:00:00Z",
///     "2010-09-01T01:00:00Z",
///     &["route-views.wide", "route-views.sydney"],
///     DataType::Update,
/// ).unwrap();
///
/// let stream = BgpStream::new(config).build();
///
/// for elem in stream {
///     println!("{}", elem);
/// }
/// ```
///
/// With caching:
///
/// ```no_run
/// use bgpflux::{BgpStream, BgpStreamConfig, DataType};
///
/// let config = BgpStreamConfig::new(
///     "2023-01-01T00:00:00Z",
///     "2023-01-01T01:00:00Z",
///     &["route-views.wide"],
///     DataType::Update,
/// ).unwrap();
///
/// let stream = BgpStream::new(config)
///     .cache_dir("./bgp_cache")
///     .build();
///
/// for elem in stream {
///     println!("{}", elem);
/// }
/// ```
pub struct BgpStream {
    /// Configuration for time ranges, collectors, and data types
    pub config: BgpStreamConfig,
    /// Optional custom broker URL
    pub broker_url: Option<String>,
    /// Optional local cache directory
    pub cache_dir: Option<String>,
}

impl BgpStream {
    /// Creates a new stream with the given configuration.
    pub fn new(config: BgpStreamConfig) -> Self {
        BgpStream {
            config,
            broker_url: None,
            cache_dir: None,
        }
    }

    /// Sets a custom broker URL for discovering BGP archives.
    pub fn broker_url<S: Display>(mut self, broker_url: S) -> Self {
        self.broker_url = Some(broker_url.to_string());
        self
    }

    /// Sets a local cache directory for downloaded files.
    pub fn cache_dir<S: Display>(mut self, cache_dir: S) -> Self {
        self.cache_dir = Some(cache_dir.to_string());
        self
    }

    /// Builds the stream and returns an iterator over BGP elements ordered by timestamp.
    pub fn build(self) -> impl Iterator<Item = BgpStreamElem> {
        match self.cache_dir {
            Some(_) => BgpStreamIter::WithCache(self.build_with_cache()),
            None => BgpStreamIter::NoCache(self.build_no_cache()),
        }
    }

    /// Query the broker to return time-ordered archives for each (collectors, data type)
    fn query_broker(&self) -> HashMap<(String, bool), Vec<String>> {
        let data_types = match self.config.data_type {
            DataType::Rib => vec!["rib"],
            DataType::Update => vec!["update"],
            DataType::Both => vec!["rib", "update"],
        };

        let mut grouped_urls = HashMap::new();
        for data_type in data_types {
            let broker = BgpkitBroker::new()
                .ts_start(self.config.ts_start.clone())
                .ts_end(self.config.ts_end.clone())
                .collector_id(self.config.collectors.join(","))
                .data_type(data_type);
            let broker = match &self.broker_url {
                Some(url) => broker.broker_url(url),
                None => broker,
            };

            let is_rib = data_type == "rib";
            for item in broker.into_iter() {
                grouped_urls
                    .entry((item.collector_id, is_rib))
                    .or_insert_with(Vec::new)
                    .push(item.url.clone())
            }
        }
        grouped_urls
    }

    fn build_no_cache(self) -> impl Iterator<Item = BgpStreamElem> {
        // Collect sorted urls for each (collector, data_type) pair
        let grouped_urls = self.query_broker();

        // Allow filters to be shared by multiple parsers
        let shared_filters = Arc::new(self.config.filters);

        // Chain archive files for each (collector, data_type) pair
        let start = DateTime::parse_from_rfc3339(&self.config.ts_start)
            .unwrap()
            .timestamp() as f64;
        let end = DateTime::parse_from_rfc3339(&self.config.ts_end)
            .unwrap()
            .timestamp() as f64;
        let mut streams = Vec::new();
        for ((collector, is_rib), urls) in grouped_urls.into_iter() {
            let static_collector = intern_collector(collector);
            let filters = Arc::clone(&shared_filters);

            let stream = urls.into_iter().flat_map(move |url| {
                match init_parser_retry(&url, None) {
                    Ok(parser) => Either::Left(process_parser_to_elems(
                        match filters.as_ref() {
                            Some(f) => parser.with_filters(f),
                            None => parser,
                        },
                        url,
                        is_rib,
                        static_collector,
                        start,
                        end,
                    )),
                    Err(e) => {
                        eprintln!("FAILED to initialize parser: {:?}", e);
                        // "Empty" iterator fallback
                        Either::Right(std::iter::empty())
                    }
                }
            });
            streams.push(stream);
        }

        // Merge them in a single sorted stream
        streams
            .into_iter()
            .kmerge_by(|a, b| a.timestamp <= b.timestamp)
    }

    fn build_with_cache(self) -> impl Iterator<Item = BgpStreamElem> {
        // Collect sorted urls for each (collector, data_type) pair
        let grouped_urls = self.query_broker();

        let cache_dir = Arc::new(self.cache_dir.clone());
        let start = DateTime::parse_from_rfc3339(&self.config.ts_start)
            .unwrap()
            .timestamp() as f64;
        let end = DateTime::parse_from_rfc3339(&self.config.ts_end)
            .unwrap()
            .timestamp() as f64;

        let rt = global_runtime();
        let sem = download_semaphore();
        let mut receivers = Vec::new();

        // Spawn Prefetch Tasks
        for (key, urls) in grouped_urls {
            // Channel restrict the number of prefetch per (collector, data type). For now hardcoded to 1.
            let (tx, rx) = tokio::sync::mpsc::channel(1);
            receivers.push((key, rx));

            let cache = Arc::clone(&cache_dir);

            rt.spawn(async move {
                for url in urls {
                    // Acquire a permit from the global pool before starting download
                    let permit = sem.acquire().await.unwrap();

                    let cache_inner = Arc::clone(&cache);
                    let url_inner = url.clone();

                    let parser_res = tokio::task::spawn_blocking(move || {
                        init_parser_retry(&url_inner, cache_inner.as_ref().as_deref())
                    })
                    .await
                    .unwrap();

                    // Release the semaphore permit immediately after the file is ready/cached
                    // so other streams can start their downloads.
                    drop(permit);

                    match parser_res {
                        Ok(parser) => {
                            if tx.send((url, parser)).await.is_err() {
                                break;
                            }
                        }
                        Err(e) => {
                            eprintln!("FAILED to initialize parser: {:?}", e);
                        }
                    }
                }
            });
        }

        let shared_filters = Arc::new(self.config.filters.clone());

        // Build stream iterators consuming the prefetched BgpkitParser objects
        let mut streams = Vec::new();
        for ((collector, is_rib), mut rx) in receivers {
            let static_collector = intern_collector(collector);
            let filters = Arc::clone(&shared_filters);

            let stream = std::iter::from_fn(move || {
                // Use the global runtime's blocking_recv
                rx.blocking_recv()
            })
            .flat_map(move |(url, parser)| {
                process_parser_to_elems(
                    match filters.as_ref() {
                        Some(f) => parser.with_filters(f),
                        None => parser,
                    },
                    url,
                    is_rib,
                    static_collector,
                    start,
                    end,
                )
            });
            streams.push(stream);
        }

        // Merge them in a single sorted stream
        streams
            .into_iter()
            .kmerge_by(|a, b| a.timestamp <= b.timestamp)
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;
    use std::fs;

    use super::*;

    /// Consume the stream to check if it's consistent with `config` and the number of expected BGP elements
    fn check_stream(
        stream: impl Iterator<Item = BgpStreamElem>,
        config: BgpStreamConfig,
        target_collectors_count: HashMap<(&str, DataType), u32>,
    ) {
        let target_elem_types = match config.data_type {
            DataType::Rib => HashSet::from([BgpStreamElemType::RIB]),
            DataType::Update => {
                HashSet::from([BgpStreamElemType::ANNOUNCE, BgpStreamElemType::WITHDRAW])
            }
            DataType::Both => HashSet::from([
                BgpStreamElemType::ANNOUNCE,
                BgpStreamElemType::WITHDRAW,
                BgpStreamElemType::RIB,
            ]),
        };

        // Collect stream info
        let mut seen_collectors_count = HashMap::new();
        let mut seen_elem_types = HashSet::new();
        let mut timestamps = Vec::new();
        for elem in stream {
            seen_collectors_count
                .entry((elem.collector_id, {
                    match elem.elem_type {
                        BgpStreamElemType::RIB => DataType::Rib,
                        _ => DataType::Update,
                    }
                }))
                .and_modify(|val| *val += 1)
                .or_insert(1);
            seen_elem_types.insert(elem.elem_type);
            timestamps.push(elem.timestamp);
        }

        // Check if config and stream match
        assert_eq!(seen_collectors_count, target_collectors_count);
        assert_eq!(seen_elem_types, target_elem_types);
        assert!(timestamps.is_sorted());
    }

    #[test]
    fn stream_update() {
        let config = BgpStreamConfig::new(
            "2010-09-01T00:00:00Z",
            "2010-09-01T01:55:00Z",
            &["route-views.wide", "route-views.sydney"],
            config::DataType::Update,
        )
        .unwrap();

        let stream = BgpStream::new(config.clone()).build();

        let target_collectors_count = HashMap::from([
            (("route-views.sydney", DataType::Update), 48287),
            (("route-views.wide", DataType::Update), 29490),
        ]);

        check_stream(stream, config, target_collectors_count);
    }

    #[test]
    fn stream_cache() {
        let config = BgpStreamConfig::new(
            "2010-09-01T00:00:00Z",
            "2010-09-01T01:55:00Z",
            &["route-views.wide", "route-views.sydney"],
            config::DataType::Update,
        )
        .unwrap();
        let test_cache_dir = "test_cache";

        // Test cache miss
        fs::remove_dir_all(test_cache_dir).ok();
        fs::create_dir(test_cache_dir).unwrap();
        let stream = BgpStream::new(config.clone())
            .cache_dir(test_cache_dir)
            .build();
        let target_collectors_count = HashMap::from([
            (("route-views.sydney", DataType::Update), 48287),
            (("route-views.wide", DataType::Update), 29490),
        ]);
        check_stream(stream, config.clone(), target_collectors_count.clone());

        // Test cache hit
        let stream = BgpStream::new(config.clone())
            .cache_dir(test_cache_dir)
            .build();
        check_stream(stream, config, target_collectors_count);

        fs::remove_dir_all(test_cache_dir).unwrap();
    }

    #[test]
    fn stream_rib() {
        let config = BgpStreamConfig::new(
            "2010-09-01T00:00:00Z",
            "2010-09-01T1:55:00Z",
            &["route-views.wide", "route-views.sydney"],
            config::DataType::Rib,
        )
        .unwrap();

        let stream = BgpStream::new(config.clone()).build();

        let target_collectors_count = HashMap::from([
            (("route-views.sydney", DataType::Rib), 828937),
            (("route-views.wide", DataType::Rib), 990164),
        ]);

        check_stream(stream, config, target_collectors_count);
    }

    #[test]
    fn stream_both() {
        let config = BgpStreamConfig::new(
            "2010-09-01T00:00:00Z",
            "2010-09-01T1:55:00Z",
            &["route-views.wide", "route-views.sydney"],
            config::DataType::Both,
        )
        .unwrap();

        let stream = BgpStream::new(config.clone()).build();

        let target_collectors_count = HashMap::from([
            (("route-views.sydney", DataType::Rib), 828937),
            (("route-views.wide", DataType::Rib), 990164),
            (("route-views.sydney", DataType::Update), 48287),
            (("route-views.wide", DataType::Update), 29490),
        ]);

        check_stream(stream, config, target_collectors_count);
    }

    #[test]
    #[ignore]
    // Run it with cargo test bench_throughput --release -- --nocapture --ignored
    fn bench_throughput() {
        let config = BgpStreamConfig::new(
            "2026-02-04T15:59:00Z",
            "2026-02-04T18:59:00Z",
            &["route-views.amsix", "route-views.linx"],
            config::DataType::Update,
        )
        .unwrap();

        let start = std::time::Instant::now();
        let mut count = 0;
        let stream = BgpStream::new(config).build();
        for elem in stream {
            std::hint::black_box(&elem);
            count += 1;
        }
        let elapsed = start.elapsed();

        let throughput = count as f64 / elapsed.as_secs_f64();
        println!(
            "{} elements in {:.2?} ({:.0} elem/sec)",
            count, elapsed, throughput
        );
    }

    #[test]
    #[ignore]
    // Run it with cargo test bench_cache_throughput --release -- --nocapture --ignored
    fn bench_cache_throughput() {
        let config = BgpStreamConfig::new(
            "2026-02-04T15:59:00Z",
            "2026-02-04T18:59:00Z",
            &["route-views.amsix", "route-views.linx"],
            config::DataType::Update,
        )
        .unwrap();

        let start = std::time::Instant::now();
        let mut count = 0;
        let stream = BgpStream::new(config).cache_dir("test_cache").build();
        for elem in stream {
            std::hint::black_box(&elem);
            count += 1;
        }
        let elapsed = start.elapsed();

        let throughput = count as f64 / elapsed.as_secs_f64();
        println!(
            "{} elements in {:.2?} ({:.0} elem/sec)",
            count, elapsed, throughput
        );
    }
}