rotonda 0.5.2-alpha.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
use core::sync::atomic::AtomicU32;
use std::net::IpAddr;
use std::{collections::HashMap, path::PathBuf, sync::atomic::Ordering};

use std::sync::RwLock;
use std::fmt;
use inetnum::asn::Asn;
use routecore::bmp::message::{PeerType, RibType};
use paste::paste;

use crate::genoutput_json;
use crate::ingress::http_ng::QueryFilter;
use crate::representation::{Cli, GenOutput, Json, OutputError};
use crate::roto_runtime::types::PeerRibType;

/// Register of ingress/sources, tracked in a serial way.
///
/// Sources are BGP sessions (so multiple per BGP connector Unit), or BMP
/// sessions (a single one per BMP connector unit), or something else.
///
/// The ingress/connector units need to register their connections with the
/// Register, providing additional information such as `SocketAddr`s or
/// string-like names. Any unit/target can query the Register.
#[derive(Debug, Default)]
pub struct Register {
    serial: AtomicU32,
    info: RwLock<HashMap<IngressId, IngressInfo>>,
}

pub type IngressId = u32;
#[derive(serde::Serialize)]
pub struct IdAndInfo<'a> {
    #[serde(rename = "id")]
    pub ingress_id: IngressId,
    #[serde(flatten)]
    pub ingress_info: &'a IngressInfo,
}

#[derive(serde::Serialize)]
pub struct OwnedIdAndInfo {
    #[serde(rename = "id")]
    pub ingress_id: IngressId,
    #[serde(flatten)]
    pub ingress_info: IngressInfo,
}

impl<'a> From<(IngressId, &'a IngressInfo)> for IdAndInfo<'a> {
    fn from(value: (IngressId, &'a IngressInfo)) -> Self {
        IdAndInfo {
            ingress_id: value.0,
            ingress_info: value.1
        }
    }
}


impl From<(IngressId, IngressInfo)> for OwnedIdAndInfo {
    fn from(value: (IngressId, IngressInfo)) -> Self {
        OwnedIdAndInfo {
            ingress_id: value.0,
            ingress_info: value.1
        }
    }
}

pub struct BmpIdAndInfo<'a>(pub IdAndInfo<'a>);
pub struct BgpIdAndInfo<'a>(pub IdAndInfo<'a>);

impl<W: std::io::Write> GenOutput<Cli<W>> for BgpIdAndInfo<'_> {
    fn write(&self, target: &mut Cli<W>) -> Result<(), OutputError> {

        let _ = writeln!(&mut target.0,
            "{:>}\t{:>10}\t{:>40}",
            self.0.ingress_id,
            self.0.ingress_info.remote_asn.map(|asn| asn.to_string()).unwrap_or("no-asn???".into()),
            self.0.ingress_info.remote_addr.map(|ip| ip.to_string()).unwrap_or("no-ip".into()),
        );
        let _ = target.0.flush();
        Ok(())
    }
}

impl<W: std::io::Write> GenOutput<Cli<W>> for BmpIdAndInfo<'_> {
    fn write(&self, target: &mut Cli<W>) -> Result<(), OutputError> {

        let _ = writeln!(&mut target.0,
            "{:>}\t{:>40}\t{}",
            self.0.ingress_id,
            self.0.ingress_info.remote_addr.map(|ip| ip.to_string()).unwrap_or("no-ip".into()),
            self.0.ingress_info.name.as_ref().unwrap_or(&"no-name".into())
        );
        let _ = target.0.flush();
        Ok(())
    }
}

genoutput_json!(IdAndInfo<'_>);
genoutput_json!(BmpIdAndInfo<'_>, 0);
genoutput_json!(BgpIdAndInfo<'_>, 0);

pub struct TestMe {
    field: Asn,
}
genoutput_json!(TestMe, field);

// Used to merge IngressInfo structs, called via info_for_field! in update_info
macro_rules! update_field {
    ($old:ident, $new:ident, $field:ident) => {
        if $new.$field.is_some() {
            $old.$field = $new.$field;
        }
    }
}

// Used to create the builder pattern methods, called in info_for_field!
macro_rules! with_field {
    ($name:ident, $type:ty) => {
        paste! {
            pub fn [<with_$name>](self, $name: impl Into<$type>) -> Self {
                Self {
                    $name: Some($name.into()), ..self }
            }
        }
    }
}

// Creates the IngressInfo (== $name) struct and adds fn Register::update_info
macro_rules! info_for_field{
    ($name:ident { $( $(#[$m:meta])? $field:ident : $type:ty),* } ) => {

        /// Information pertaining to an [`IngressId`]
        ///
        /// The `IngressInfo` struct is quite broad and generic in nature, featuring
        /// fields that might not make sense in all use cases. Therefore, everything
        /// is wrapped as an `Option`, giving the user (mostly connector/ingress
        /// components within Rotonda) the flexibilty to fill in what makes sense in
        /// their specific case.
        #[derive(Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
        #[serde_with::skip_serializing_none]
        #[derive(serde::Serialize)]
        pub struct $name {
            $(
                $(#[$m])?
                pub $field: Option<$type>,
            )*

            // pub last_active: Instant ? to enable 'reconnecting' remotes?
        }

        impl $name {
            $(
                with_field!($field, $type);
            )*

        }

        impl Register {
            /// Change the info related to an [`IngressId`]
            pub fn update_info(
                &self,
                id: IngressId,
                new_info: $name,
            ) -> Option<$name> {
                let mut lock = self.info.write().unwrap();

                //log::debug!("update_info for {id} with {new_info:?}");

                if let Some(mut old) = lock.remove(&id) {

                    $(
                        update_field!(old, new_info, $field);
                    )*
                    lock.insert(id, old) // returns the replaced info
                } else {
                    lock.insert(id, new_info) // returns the replaced info
                }
            }

        }
    }
}

// Creates a boolean expression from mandatory and optional fields in [`IngressInfo`]
//
// Usage:
//
//   find_existing_for!(info, query, {mandatory_fields*}, {optional_fields*}?)
//
// Note that the set of optional_fields is optional itself.
// For all fields in mandatory_fields, we check whether the field is not None
// in the `info` we have stored, then compare it to that field in the `query`.
// For fields in the optional_fields, the check for not None is skipped, the
// the field on `info` is compared to the one on `query` regardless.
macro_rules! find_existing_for {
    (
        $info:ident, $query:ident,
        { $($mandatory_field:ident),*} $(,)?
        $({ $($optional_field:ident),*})?
    ) => {
        $(
            $info.$mandatory_field.is_some() &&
            $info.$mandatory_field == $query.$mandatory_field &&
        )*
        $(
            $(
                $info.$optional_field == $query.$optional_field &&
            )*
        )?
    true // just to fill up the last '&& _' from the repetition
    }

}

impl Register {
    /// Create a new register
    pub(crate) fn new() -> Self {
        Self {
            serial: 1.into(),
            info: RwLock::new(HashMap::new()),
        }
    }

    pub fn current_serial(&self) -> u32 {
        self.serial.load(Ordering::Relaxed)
    }

    pub fn overview(&self) -> String {
        let lock = self.info.read().unwrap();
        let mut res = String::new();
        for (id, info) in lock.iter() {
            res.push_str(&format!(
                "{id:02} {}\t\t{}\n",
                info.remote_asn.map(|a|a.to_string()).unwrap_or("".to_string()),
                info.remote_addr.map(|a|a.to_string()).unwrap_or("".to_string()),
            ));
        }
        res
    }

    pub fn cloned_info(&self) -> HashMap<IngressId, IngressInfo> {
        self.info.read().unwrap().clone()
    }


    pub fn bgp_neighbors<T>(&self, mut target: T) -> fmt::Result
        where for <'a> BgpIdAndInfo<'a>: GenOutput<T>
    {
        for t in [IngressType::BgpViaBmp, IngressType::Bgp, IngressType::Mrt] {
            let res = self.search(QueryFilter { ingress_type: Some(t), ..Default::default() });
            for r in res {
                let _ = BgpIdAndInfo (
                        IdAndInfo { ingress_id: r.ingress_id, ingress_info: &r.ingress_info}
                    ).write(&mut target);

            }
        }
        Ok(())
    }

    pub fn real_bgp_peers<T>(&self, mut target: T) -> fmt::Result
        where for <'a> BgpIdAndInfo<'a>: GenOutput<T>
    {
        let res = self.search(QueryFilter {
            ingress_type: Some(IngressType::Bgp),
            ingress_state: Some(IngressState::Connected),
            ..Default::default()
        });
        for r in res {
            let _ = BgpIdAndInfo (
                IdAndInfo { ingress_id: r.ingress_id, ingress_info: &r.ingress_info}
            ).write(&mut target);

        }
        Ok(())
    }

    pub fn bmp_routers<T>(&self, mut target: T) -> fmt::Result
        where for <'a> BmpIdAndInfo<'a>: GenOutput<T>
    {
        let res = self.search(
            QueryFilter {
                ingress_type: Some(IngressType::Bmp),
                ingress_state: Some(IngressState::Connected),
                ..Default::default()
            }
        );
        for r in res {

            let _ = BmpIdAndInfo (
                    IdAndInfo { ingress_id: r.ingress_id, ingress_info: &r.ingress_info}
                ).write(&mut target);

        }
        Ok(())
    }

    /// Request a new, unique [`IngressId`]
    pub(crate) fn register(&self) -> IngressId {
        self.serial.fetch_add(1, Ordering::Relaxed)
    }

    /// Retrieve the information for the given [`IngressId`]
    pub fn get(&self, id: IngressId) -> Option<IngressInfo> {
        self.info.read().unwrap().get(&id).cloned()
    }

    /// Retrieve the information for the given [`IngressId`]
    pub fn get_tuple(&self, id: IngressId) -> Option<OwnedIdAndInfo> {
        self.info.read().unwrap().get(&id).map(|info|
            (id, info.clone()).into()
        )
    }

    /// Filter all ingresses based on a passed filter and return them
    pub fn search(&self, filter: QueryFilter,) -> Vec<OwnedIdAndInfo> {
        // ALternatively, simply clone the entire thing and release the read-lock asap.
        //let mut reg = self.info.read().unwrap().clone();

        self.info.read().unwrap().iter()
            .filter(|(_, info)| filter.filter(info))
            .map(|(&ingress_id, info)| OwnedIdAndInfo{ingress_id, ingress_info: info.clone()} )
            .collect::<Vec<_>>()

    }

    /// Filter all ingresses based on a passed filter and write it out
    pub fn search_and_output<T>(&self, filter: QueryFilter, mut target: T) -> Result<(), OutputError> 
        where for <'a> IdAndInfo<'a>: GenOutput<T>
    {
        IdAndInfo::write_seq_start(&mut target)?;
        let lock = self.info.read().unwrap();

        let mut iter = lock.iter().filter(|(_, info)| {
            filter.filter(info)
        });

        if let Some((&ingress_id, ingress_info)) = iter.next() {
            let _ = IdAndInfo::from((ingress_id, ingress_info)).write(&mut target);
        }

        for (&ingress_id, ingress_info) in iter {
            IdAndInfo::write_seq_sep(&mut target)?;
            IdAndInfo::from((ingress_id, ingress_info)).write(&mut target)?;
        }

        IdAndInfo::write_seq_end(&mut target)?;

        Ok(())
    }

    /// Find all [`IngressId`]s that are children of the given `parent`
    ///
    /// This is used in cases where for example a BMP session (the parent) is
    /// terminated, and all route information that was learned for that
    /// session (in one or multiple monitored BGP sessions, the children) need
    /// to be withdrawn.
    pub fn ids_for_parent(&self, parent: IngressId) -> Vec<IngressId> {
        let mut res = Vec::new();
        for (id, info) in self.info.read().unwrap().iter() {
            if info.parent_ingress == Some(parent) {
                res.push(*id);
            }
        }
        res
    }

    // find_existing methods:
    // cases to cover:
    //  * match MRT update messages (to ingresses from bdumps):
    //    * from bdump, we have:
    //      * filename, remote addr, remote_asn
    //
    //  * match reconnecting BMP session:
    //      * previous BMP session had ingress_id + remote_addr
    //      * child BGP sessions had their own ingress_id + parent_id + remote
    //      addr + remote asn
    //  * match BGP flap:
    //     * previous had ingress_id + fake name + remote addr + remote asn
    //
    //
    //  do we need to register the unit name (e.g. bmp-in, mrt-in) as well?

    /// Search existing [`IngressId`] on the peer level
    ///
    /// For the peer level, the comparison is based on the parent
    /// `ingress_id`, the remote address and ASN (all three must be set).
    /// Furthermore, the RIB type, Peer type, and the Peer Distinguisher are compared, though these
    /// might be unset, i.e. None.
    pub fn find_existing_peer(
        &self,
        query: &IngressInfo
    ) -> Option<(IngressId, IngressInfo)> {
        let lock = self.info.read().unwrap();
        for (id, info) in lock.iter() {
            if find_existing_for!(info, query,
                {parent_ingress, remote_addr, remote_asn},
                {rib_type, peer_type, distinguisher, vrf_name}
            ) {
                    //log::debug!("found existing peer, id {id}");
                    return Some((*id, info.clone()))
            }
        }
        None
    }

    /// Search existing [`IngressId`] on the BMP router leven
    ///
    /// For the BMP router level, the comparison is based on the parent
    /// `ingress_id` and the remote address (both must be set).
    ///
    /// NB: ideally the sysName is also included when looking for existing routers. This is
    /// currently not trivial without a larger refactor. As a result, multiple BMP exporters coming
    /// from the same remote IP address connecting to the same Rotonda BMP unit will be seen as one
    /// and the same, potentionally causing confusing issues. The workaround is not checking for
    /// existing routers, causing increased memory use. As multiple exporters coming from one and
    /// the same IP address is presumably unlikely, we keep the check as-is for now.
    pub fn find_existing_bmp_router(
        &self,
        query: &IngressInfo
    ) -> Option<(IngressId, IngressInfo)> {
        let lock = self.info.read().unwrap();
        log::debug!("query: {query:?}");
        for (id, info) in lock.iter() {
            if find_existing_for!(info, query,
                {parent_ingress, remote_addr, ingress_type},
                //{name} // to include the sysName in this check, we first need to refactor the BMP unit
            ) {
                    log::debug!("found matching bmp router, id {id}");
                    return Some((*id, info.clone()))
            }
        }
        log::debug!("no match in find_existing_bmp_router");
        None
    }
}

impl IngressInfo {
    pub fn new() -> Self {
        Self::default()
    }
}

#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum IngressType {
    Bmp,
    BgpViaBmp,
    Bgp,
    Mrt,
    Rtr,
}


#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, serde::Serialize, serde::Deserialize)]
pub enum IngressState {
    Connected,
    Disconnected,
    NonNetwork,
}

// TODO this probably needs a 'state' (connected, disconnected, ..) ?
// and with that, a last_active timestamp/Instant
// This constructs the [`IngressInfo`] struct, used as values in the Register.
info_for_field!(IngressInfo{
   unit_name: String,
   ingress_type: IngressType,
   parent_ingress: IngressId,
   state: IngressState,
   remote_addr: IpAddr,
   remote_asn: Asn,
   rib_type: RibType,
   peer_rib_type: PeerRibType, // RibType + pre/post policy 
   filename: PathBuf,
   name: String,
   desc: String,
   local_asn: Asn,
   peer_type: PeerType,
   distinguisher: [u8; 8],
   vrf_name: String,
   #[serde(skip_serializing)]
   local_capabilities: Vec<u8>,
   #[serde(skip_serializing)]
   remote_capabilities: Vec<u8>
});

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn builder_and_update() {
        let res = Register::new();
        let id = res.register();
        let info = IngressInfo::new()
            .with_local_asn(Asn::from_u32(65536));
        assert_eq!(info.local_asn, Some(Asn::from_u32(65536)));

        res.update_info(id, info.clone());
        assert_eq!(
            res.get(id), 
            Some(info)
        );

        let newinfo = IngressInfo::new()
            .with_local_asn(Asn::from_u32(65537));
        res.update_info(id, newinfo.clone());
        assert_eq!(
            res.get(id), 
            Some(newinfo)
        );

        let newinfo_2 = IngressInfo::new()
            .with_rib_type(RibType::LocRib)
        ;
        res.update_info(id, newinfo_2.clone());

        // We still expect the ASN, untouched:
        assert_eq!(
            res.get(id).unwrap().local_asn,
            Some(Asn::from_u32(65537))
        );

        // And the newly set RibType
        assert_eq!(
            res.get(id).unwrap().rib_type,
            Some(RibType::LocRib)
        );


    }

    #[test]
    fn non_mandatory_unset() {
        let res = Register::new();
        let parent_id1 = res.register();
        let session_id1_a = res.register();

        let info1_a = IngressInfo::new()
            .with_parent_ingress(parent_id1)
            .with_remote_addr("1.2.3.4".parse::<IpAddr>().unwrap())
            .with_remote_asn(Asn::from_u32(65000))
            ;

        res.update_info(session_id1_a, info1_a.clone());

        let query1 = IngressInfo::new()
            .with_parent_ingress(parent_id1)
            .with_remote_addr("1.2.3.4".parse::<IpAddr>().unwrap())
            .with_remote_asn(Asn::from_u32(65000))
            ;
        assert_eq!(
            res.find_existing_peer(&query1),
            Some((session_id1_a, info1_a))
        );


    }

    #[test]
    fn non_mandatory_set() {
        let res = Register::new();
        let parent_id1 = res.register();
        let session_id1_a = res.register();

        let info1_a = IngressInfo::new()
            .with_parent_ingress(parent_id1)
            .with_peer_type(PeerType::GlobalInstance)
            .with_remote_addr("1.2.3.4".parse::<IpAddr>().unwrap())
            .with_remote_asn(Asn::from_u32(65000))
            ;

        res.update_info(session_id1_a, info1_a.clone());

        let query1 = IngressInfo::new()
            .with_parent_ingress(parent_id1)
            .with_peer_type(PeerType::GlobalInstance)
            .with_remote_addr("1.2.3.4".parse::<IpAddr>().unwrap())
            .with_remote_asn(Asn::from_u32(65000))
            ;
        assert_eq!(
            res.find_existing_peer(&query1),
            Some((session_id1_a, info1_a))
        );
    }

    #[test]
    fn non_mandatory_query_missing_optional() {
        let res = Register::new();
        let parent_id1 = res.register();
        let session_id1_a = res.register();

        let info1_a = IngressInfo::new()
            .with_parent_ingress(parent_id1)
            .with_peer_type(PeerType::GlobalInstance)
            .with_remote_addr("1.2.3.4".parse::<IpAddr>().unwrap())
            .with_remote_asn(Asn::from_u32(65000))
            ;

        res.update_info(session_id1_a, info1_a.clone());

        let query1 = IngressInfo::new()
            .with_parent_ingress(parent_id1)
            .with_remote_addr("1.2.3.4".parse::<IpAddr>().unwrap())
            .with_remote_asn(Asn::from_u32(65000))
            ;
        assert_eq!(
            res.find_existing_peer(&query1),
            None,
        );
    }

    #[test]
    fn non_mandatory_set_but_different() {
        let res = Register::new();
        let parent_id1 = res.register();
        let session_id1_a = res.register();

        let info1_a = IngressInfo::new()
            .with_parent_ingress(parent_id1)
            .with_remote_addr("1.2.3.4".parse::<IpAddr>().unwrap())
            .with_remote_asn(Asn::from_u32(65000))
            .with_distinguisher([1,2,3,4,9,9,9,9])
            ;

        res.update_info(session_id1_a, info1_a.clone());

        let query1 = IngressInfo::new()
            .with_parent_ingress(parent_id1)
            .with_remote_addr("1.2.3.4".parse::<IpAddr>().unwrap())
            .with_remote_asn(Asn::from_u32(65000))
            .with_distinguisher([9,9,9,9,8,8,8,8])
            ;
        assert_eq!(
            res.find_existing_peer(&query1),
            None,
        );
    }
}