rotonda 0.4.0

composable, programmable BGP engine
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
use std::collections::VecDeque;
use std::future::{Future, IntoFuture};
use std::io::Read;
use std::ops::ControlFlow;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Instant;

use bzip2::bufread::BzDecoder;
use flate2::read::GzDecoder;
use futures::future::{select, Either};
use futures::{pin_mut, FutureExt, TryFutureExt};
use log::{debug, error, info, warn};
use rand::seq::SliceRandom;
use rotonda_store::prefix_record::RouteStatus;
use routecore::bgp::fsm::state_machine::State;
use routecore::bgp::message::{Message as BgpMsg, PduParseInfo};
use routecore::bgp::nlri::afisafi::{Ipv4UnicastNlri, Nlri};
use routecore::bgp::types::AfiSafiType;
use routecore::bgp::workshop::route::RouteWorkshop;
use routecore::bgp::ParseError;
use routecore::mrt::MrtFile;
use serde::Deserialize;
use sha2::{Digest, Sha256};
use smallvec::SmallVec;
use tokio::pin;
use tokio::sync::{mpsc, oneshot};
use tokio::task::JoinSet;

use crate::config::ConfigPath;
use crate::roto_runtime::types::{explode_announcements, explode_withdrawals, FreshRouteContext, MrtContext, Provenance, RouteContext};
use crate::common::unit::UnitActivity;
use crate::comms::{GateStatus, Terminated};
use crate::ingress::{self, IngressId, IngressInfo};
use crate::manager::{Component, WaitPoint};
use crate::payload::{Payload, RotondaPaMap, RotondaRoute, Update};
use crate::units::{Gate, Unit};

use super::api;

#[derive(Clone, Debug, Deserialize)]
pub struct MrtFileIn {
    pub filename: OneOrManyPaths,
    pub update_path: Option<ConfigPath>,
}

#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
pub enum OneOrManyPaths {
    One(ConfigPath),
    Many(Vec<ConfigPath>),
}
pub enum PathsIterator<'a> {
    One(Option<PathBuf>),
    Many(std::slice::Iter<'a, ConfigPath>)
}
impl Iterator for PathsIterator<'_> {
    type Item = PathBuf;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            PathsIterator::One(ref mut p) => p.take(),
            PathsIterator::Many(ref mut iter) => {
                iter.next().cloned().map(Into::into)
            }
        }
    }
}
impl OneOrManyPaths {
    pub fn iter(&self) -> PathsIterator {
        match self {
            OneOrManyPaths::One(p) => {
                PathsIterator::One(Some(p.clone().into()))
            }
            OneOrManyPaths::Many(m) => PathsIterator::Many(m.iter()),
        }
    }
}

pub struct MrtInRunner {
    config: MrtFileIn,
    gate: Gate,
    ingresses: Arc<ingress::Register>,
    parent_id: IngressId,
    queue_tx: mpsc::Sender<QueueEntry>,
    processing: Option<PathBuf>,
    processed: Vec<(PathBuf, String)>,
}

pub type QueueEntry = (
    // the file to be queued
    PathBuf,
    // optional response to the enqueuer
    Option<oneshot::Sender<Result<String, String>>> 
);

impl MrtFileIn {
    pub async fn run(
        self,
        mut component: Component,
        gate: Gate,
        mut waitpoint: WaitPoint,
    ) -> Result<(), crate::comms::Terminated> {
        gate.process_until(waitpoint.ready()).await?;
        waitpoint.running().await;

        let (queue_tx, queue_rx) = mpsc::channel::<QueueEntry>(1024);


        let ingresses = component.ingresses().clone();
        let parent_id = ingresses.register();
        let _ = ingresses.update_info(parent_id,
            IngressInfo::new()
                .with_unit_name(component.name().as_ref())
                .with_desc("mrt-file-in unit")
        );


        for f in self.filename.iter() {
            let _ = queue_tx.send((f, None)).await;
        }

        let endpoint_path = Arc::new(format!("/mrt/{}/", component.name()));
        let api_processor = Arc::new(
            api::Processor::new(
                endpoint_path.clone(),
                self.update_path.clone().map(Into::into),
                queue_tx.clone(),
                )
            );

        component.register_http_resource(
            api_processor.clone(),
            &endpoint_path,
        );

        MrtInRunner::new(self, gate, ingresses, parent_id, queue_tx).run(queue_rx).await
    }
}

impl MrtInRunner {
    fn new(
        mrtin: MrtFileIn,
        gate: Gate,
        ingresses: Arc<ingress::Register>,
        parent_id: IngressId,
        queue_tx: mpsc::Sender<QueueEntry>,
    ) -> Self {
        Self {
            gate,
            config: mrtin,
            ingresses,
            parent_id,
            queue_tx,
            processing: None,
            processed: vec![],
        }
    }

    async fn process_state_change(
        gate: &Gate,
        ingresses: &Arc<ingress::Register>,
        sc: routecore::mrt::StateChangeAs4
    ) {
        match (sc.old_state(), sc.new_state()){
            (x, y) if x == y => {
                warn!("State Change to same state {}, ignoring",
                    x
                )
            }
            (State::Established, State::Idle) => {
                if let Some((ingress_id, _info)) = ingresses.find_existing_peer(
                    &IngressInfo::new()
                    .with_remote_addr(sc.peer_addr())
                    .with_remote_asn(sc.peer_asn())
                ) {
                    let update = Update::Withdraw(ingress_id, None);
                    gate.update_data(update).await;
                    debug!("Withdraw for {ingress_id} sent");
                }
                else {
                    debug!("No IngressInfo for {} {} going Established -> Idle",
                        sc.peer_asn(),
                        sc.peer_addr()
                    );
                }
            }
            (_,_) => {
                debug!("State Change: {} -> {} in MRT, not doing anything",
                    sc.old_state(), sc.new_state()
                )
            }
        }
    }


    async fn process_message(
        gate: &Gate,
        ingresses: &Arc<ingress::Register>,
        parent_id: IngressId,
        msg: routecore::mrt::MessageAs4<'_, &[u8]>,
    ) -> Result<(usize, usize), MrtError> {
        let bgp_msg = match msg.bgp_msg() {
            Ok(msg) => msg,
            Err(e) => {
                error!("{e}");
                return Ok((0, 0));
            }
        };
        let mut announcements_sent = 0;
        let mut withdrawals_sent = 0;

        match bgp_msg {
            BgpMsg::Update(upd) => {
                let received = std::time::Instant::now();
                let mut payloads = SmallVec::new();
                let rr_reach = explode_announcements(&upd)?;
                let rr_unreach = explode_withdrawals(&upd)?;

                announcements_sent += rr_reach.len();
                withdrawals_sent += rr_unreach.len();

                let ingress_query = IngressInfo::new()
                        .with_parent(parent_id)
                        .with_remote_addr(msg.peer_addr())
                        .with_remote_asn(msg.peer_asn())
                ;

                let ingress_id = if let Some((id, _info)) =
                    ingresses.find_existing_peer(&ingress_query)
                {
                    id
                } else {
                    let new_id = ingresses.register();
                    ingresses.update_info(
                        new_id,
                        ingress_query
                    );
                    warn!("no ingress info found, regged {new_id}");
                    new_id
                };

                let provenance = Provenance::for_bgp(
                    ingress_id,
                    msg.peer_addr(),
                    msg.peer_asn(),
                );

                // or do we need a RouteContext::Fresh here?
                let context = MrtContext {
                    status: RouteStatus::Active,
                    provenance
                };

                payloads.extend(
                    rr_reach.into_iter().map(|rr|
                        Payload::with_received(
                            rr,
                            RouteContext::Mrt(context.clone()),
                            None,
                            received,
                        )
                    ));

                let context = MrtContext {
                    status: RouteStatus::Withdrawn,
                    ..context
                };

                payloads.extend(rr_unreach.into_iter().map(|rr|
                        Payload::with_received(
                            rr,
                            RouteContext::Mrt(context.clone()),
                            None,
                            received
                        )
                ));
                let update = payloads.into();
                gate.update_data(update).await;
            }
            BgpMsg::Open(_open_message) => {
                warn!("BGP OPEN in MRT, skipping");
            }
            BgpMsg::Notification(_notification_message) =>{
                debug!("BGP NOTIFICATION in MRT, skipping");
            }
            BgpMsg::Keepalive(_keepalive_message) => {
                debug!("BGP KEEPALIVE in MRT, skipping");
            }
            BgpMsg::RouteRefresh(_route_refresh_message) => {
                debug!("BGP ROUTEREFRESH in MRT, skipping");
            }
        }
        Ok((announcements_sent, withdrawals_sent))
    }

    async fn process_file(
        gate: Gate,
        ingresses: Arc<ingress::Register>,
        parent_id: IngressId,
        filename: PathBuf,
    ) -> Result<(), MrtError> {
        info!("processing {} on thread {:?}",
            filename.to_string_lossy(),
            std::thread::current().id()
        );
        #[allow(unused_variables)] // false positive, used in info!() below)
        let t0 = Instant::now();

        let file = std::fs::File::open(&filename)?;
        let mmap = unsafe { memmap2::Mmap::map(&file)? };
        let mut buf = Vec::<u8>::new();

        let t0 = Instant::now();
        let mrt_file = match filename.as_path().extension()
            .and_then(std::ffi::OsStr::to_str)
        {
            Some("gz") => {
                let mut gz = GzDecoder::new(&mmap[..]);
                gz.read_to_end(&mut buf)
                    .map_err(|_e| MrtError::other("gz decoding failed"))?;
                info!("decompressed {} in {}ms",
                    &filename.to_string_lossy(),
                    t0.elapsed().as_millis());
                MrtFile::new(&buf[..])
            }
            Some("bz2") => {
                let mut bz2 = BzDecoder::new(&mmap[..]);
                bz2.read_to_end(&mut buf)
                    .map_err(|e| {
                        error!("bz2 error: {e}");
                        MrtError::other("bz2 decoding failed")
                    })?;
                info!("decompressed {} in {}ms",
                    &filename.to_string_lossy(),
                    t0.elapsed().as_millis());
                MrtFile::new(&buf[..])
            }
            _ => {
                MrtFile::new(&mmap[..])
            }
        };

        let mut routes_sent = 0;

        // --- Dump part (RIB entries)
        //
        if let Ok(peer_index_table) = mrt_file.pi() {
            debug!("found peer index table of len {} in {}",
                peer_index_table.len(),
                filename.to_string_lossy()
            );
            let mut ingress_map = Vec::with_capacity(peer_index_table.len());
            for peer_entry in &peer_index_table[..] {
                let id = ingresses.register();
                ingresses.update_info(
                    id,
                    IngressInfo::new()
                        .with_parent(parent_id)
                        .with_remote_addr(peer_entry.addr)
                        .with_remote_asn(peer_entry.asn)
                        .with_filename(filename.clone()),
                );
                ingress_map.push(id);
            }


            let rib_entries = mrt_file.rib_entries()?;
            for (afisafi, peer_id, peer_entry, prefix, raw_attr) in rib_entries {
                let rr = match afisafi {
                    AfiSafiType::Ipv4Unicast => {
                        RotondaRoute::Ipv4Unicast(
                            prefix.try_into().map_err(MrtError::other)?,
                            RotondaPaMap::new(routecore::bgp::path_attributes::OwnedPathAttributes::new(PduParseInfo::modern(), raw_attr))
                        )
                    }
                    AfiSafiType::Ipv6Unicast => {
                        RotondaRoute::Ipv6Unicast(
                            prefix.try_into().map_err(MrtError::other)?,
                            RotondaPaMap::new(routecore::bgp::path_attributes::OwnedPathAttributes::new(PduParseInfo::modern(), raw_attr))
                        )
                    }
                    AfiSafiType::Ipv4Multicast |
                    AfiSafiType::Ipv4MplsUnicast |
                    AfiSafiType::Ipv4MplsVpnUnicast |
                    AfiSafiType::Ipv4RouteTarget |
                    AfiSafiType::Ipv4FlowSpec |
                    AfiSafiType::Ipv6Multicast |
                    AfiSafiType::Ipv6MplsUnicast |
                    AfiSafiType::Ipv6MplsVpnUnicast |
                    AfiSafiType::Ipv6FlowSpec |
                    AfiSafiType::L2VpnVpls |
                    AfiSafiType::L2VpnEvpn |
                    AfiSafiType::Unsupported(_, _) => {
                        debug!("unsupported AFI/SAFI {}, skipping", afisafi);
                        continue
                    }
                };
                let provenance = Provenance::for_bgp(
                    ingress_map[usize::from(peer_id)],
                    peer_entry.addr,
                    peer_entry.asn,
                );
                let ctx = RouteContext::for_mrt_dump(provenance);
                let update = Update::Single(Payload::new(rr, ctx, None));

                gate.update_data(update).await;
                
                // Allow other async tasks to have a go by introducing an
                // `await` every N entries:
                if routes_sent % 100_000 == 0 {
                    tokio::time::sleep(std::time::Duration::from_micros(1)).await;
                }
                routes_sent += 1;
            }
        }

        // --- Messages part (update file)

        use routecore::mrt::Bgp4Mp;

        let mut announcements_sent = 0;
        let mut withdrawals_sent = 0;

        let mut messages_processed = 0;
        for msg in mrt_file.messages() {
            match msg {
                Bgp4Mp::StateChange(sc) => {
                    MrtInRunner::process_state_change(&gate, &ingresses, sc.into()).await;
                }
                Bgp4Mp::StateChangeAs4(sc) => {
                    MrtInRunner::process_state_change(&gate, &ingresses, sc).await;
                }
                Bgp4Mp::Message(msg) => {
                    let (reach, unreach) = MrtInRunner::process_message(&gate, &ingresses, parent_id, msg.into()).await?; 
                    announcements_sent += reach;
                    withdrawals_sent += unreach;
                    
                }
                Bgp4Mp::MessageAs4(msg) => {
                    let (reach, unreach) = MrtInRunner::process_message(&gate, &ingresses, parent_id, msg).await?;
                    announcements_sent += reach;
                    withdrawals_sent += unreach;
                    messages_processed += 1;
                }
            }

            // Allow other async tasks to have a go by introducing an
            // `await` every N entries:
            if messages_processed % 100_000 == 0 {
                tokio::time::sleep(std::time::Duration::from_micros(1)).await;
            }
        }


        info!(
            "mrt-in: done processing {}, emitted {} routes, {} announcements, {} withdrawals in {}s",
            filename.to_string_lossy(),
            routes_sent,
            announcements_sent,
            withdrawals_sent,
            t0.elapsed().as_secs()
        );

        Ok(())
    }

    async fn run(
        mut self,
        mut queue: mpsc::Receiver<QueueEntry>,
    ) -> Result<(), Terminated> {

        let gate = self.gate.clone();
        let ingresses = self.ingresses.clone();
        let (results_tx, mut results_rx) = mpsc::unbounded_channel();
        //let (handles_tx, mut handles_rx) = mpsc::unbounded_channel();

        tokio::spawn(async move {
            while let Some((p, enqueuer_tx)) = queue.recv().await {
                let gate = gate.clone();
                let ingresses = ingresses.clone();
                let results_tx = results_tx.clone();
                // concurrent:
                /*
                let handle = tokio::spawn(async move {
                    let r = Self::process_file(
                        gate,
                        ingresses,
                        p.clone()
                    ).await.map(|_| p);
                    results_tx.send(r)
                });
                let _ = handles_tx.send(handle);
                */

                // sequential:
                
                let r = Self::process_file(
                    gate,
                    ingresses,
                    self.parent_id,
                    p.clone()
                ).await.map(|_| p).inspect_err(|e| error!("process_file failed: {e}"));
                if let Err(e) = results_tx.send(r) {
                    error!("failed to send result of file {e}")
                }
                if let Some(tx) = enqueuer_tx {
                    let _ = tx.send(Ok("OK!".into()));
                }
            };
        });

        loop {
            let f = results_rx.recv().map(Ok);
            match self.process_until(f).await {
                ControlFlow::Continue(Ok(r)) => {
                    if let Some(Ok(p)) = r {

                        let filename = p.to_string_lossy();
                        let mut hasher = Sha256::new();
                        let mut file = std::fs::File::open(&p).unwrap();

                        let t0 = Instant::now();
                        let _bytes_written = std::io::copy(&mut file, &mut hasher).unwrap();
                        let hash_bytes = hasher.finalize();
                        let hash_str = format!("{:x}", hash_bytes);
                        info!(
                            "processed {}, sha256: {}",
                            filename, &hash_str
                        );
                        self.processed.push((p, hash_str));
                    }
                }
                ControlFlow::Continue(Err(e)) => {
                    eprintln!("error: {e}");
                }
                ControlFlow::Break(_) => {
                    info!("terminating unit, processed {:?}", self.processed);
                    /* commented out while doing the sequential approach vs
                     * concurrent
                    while let Ok(Some(h)) = tokio::time::timeout(std::time::Duration::from_millis(100), handles_rx.recv()).await {
                        info!("aborting spawned task");
                        h.abort();
                    }
                    */
                    info!("timed out");
                    return Err(Terminated);
                }
            }
        }
    }

    async fn process_until<T, U>(
        &self,
        until_fut: T,
    ) -> ControlFlow<Terminated, std::io::Result<U>>
    where
        T: Future<Output = std::io::Result<U>>,
    {
        let mut until_fut = Box::pin(until_fut);

        loop {
            let process_fut = self.gate.process();
            pin_mut!(process_fut);

            let res = select(process_fut, until_fut).await;

            match res {
                Either::Left((Ok(gate_status), next_fut)) => {
                    match gate_status {
                        GateStatus::Active | GateStatus::Dormant => {}
                        GateStatus::Reconfiguring {
                            new_config:
                                Unit::MrtFileIn(MrtFileIn {
                                    filename: new_filename,
                                    ..
                                }),
                        } => {
                            /*
                            if new_filename != self.config.filename {
                                info!("Reloading mrt-in, processing new file {}", &new_filename.to_string_lossy());
                                if let Err(e) = self
                                    .queue_tx
                                    .send(new_filename.clone())
                                    .await
                                {
                                    error!(
                                        "Failed to process {}: {}",
                                        new_filename.to_string_lossy(),
                                        e
                                    );
                                }
                                //self.config.file
                            }
                            */
                        }
                        GateStatus::Reconfiguring { .. } => {
                            // reconfiguring for other unit types, ignore
                        }
                        GateStatus::ReportLinks { report } => {
                            report.declare_source();
                            //report.set_graph_status(self.metrics.clone());
                        }
                        GateStatus::Triggered { .. } => {
                            warn!("got unexpected Triggered for this unit");
                        }
                    }
                    until_fut = next_fut;
                }
                Either::Left((Err(Terminated), _next_fut)) => {
                    debug!("self.process_until Left Terminated");
                    return ControlFlow::Break(Terminated);
                }
                Either::Right((Ok(until_res), _next_fut)) => {
                    return ControlFlow::Continue(Ok(until_res))
                }
                Either::Right((Err(err), _next_fut)) => {
                    return ControlFlow::Continue(Err(err))
                }
            }
        }
    }
}

#[derive(Debug)]
enum MrtErrorType {
    Io(std::io::Error),
    Parse(ParseError),
    Other(&'static str),
}
#[derive(Debug)]
pub struct MrtError(MrtErrorType);

impl MrtError {
    fn other(s: &'static str) -> Self {
        Self(MrtErrorType::Other(s))
    }
}

impl std::error::Error for MrtError {}
impl std::fmt::Display for MrtError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.0 {
            MrtErrorType::Io(e) => write!(f, "io error: {}", e),
            MrtErrorType::Parse(e) => write!(f, "parse error: {}", e),
            MrtErrorType::Other(e) => write!(f, "error: {}", e),
        }
    }
}

impl From<ParseError> for MrtError {
    fn from(e: ParseError) -> Self {
        Self(MrtErrorType::Parse(e))
    }
}

impl From<std::io::Error> for MrtError {
    fn from(e: std::io::Error) -> Self {
        Self(MrtErrorType::Io(e))
    }
}
impl From<MrtError> for std::io::Error {
    fn from(e: MrtError) -> Self {
        std::io::Error::other(e.to_string())
    }
}