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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
//! Configuration.
//!
//! Rotonda is configured through a single TOML configuration file. We use
//! [serde] to deserialize this file into the [`Config`] struct provided by
//! this module. This struct also provides the facilities to load the config
//! file referred to in command line options.

use crate::http;
use crate::log::{LogConfig, Terminate};
use crate::manager::{Manager, TargetSet, UnitSet};
use clap::{Arg, ArgMatches, Command};
use log::{error, trace};
use serde::Deserialize;
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::{borrow, error, fmt, fs, io, ops};
use toml::{Spanned, Value};

//------------ Constants -----------------------------------------------------

const CFG_UNITS: &str = "units";
const CFG_TARGETS: &str = "targets";

const ARG_CONFIG: &str = "config";

//------------ Config --------------------------------------------------------

/// The complete Rotonda configuration.
///
/// All configuration is available via public fields.
///
/// The associated function [`init`](Self::init) should be called first thing
/// as it initializes the operational environment such as logging. Thereafter,
/// [`config_args`](Self::config_args) can be used to configure a clap app to
/// be able to pick up the path to the configuration file.
/// [`from_arg_matches`](Self::from_arg_matches) will then load the file
/// referenced in the command line and, upon success, return the config.
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
    /// Location of the .roto script containing all user defined filters.
    pub roto_script: Option<PathBuf>,

    /// The set of configured units.
    pub units: UnitSet,

    /// The set of configured targets.
    pub targets: TargetSet,

    /// The logging configuration.
    #[serde(flatten)]
    pub log: LogConfig,

    /// The HTTP server configuration.
    #[serde(flatten)]
    pub http: http::Server,
}

impl Config {
    /// Initialises everything.
    ///
    /// This function should be called first thing.
    pub fn init() -> Result<(), Terminate> {
        LogConfig::init_logging()
    }

    /// Creates a configuration from a bytes slice with TOML data.
    pub fn from_bytes(
        slice: &[u8],
        base_dir: Option<impl AsRef<Path>>,
    ) -> Result<Self, toml::de::Error> {
        if let Some(ref base_dir) = base_dir {
            ConfigPath::set_base_path(base_dir.as_ref().into())
        }
        let config_str = String::from_utf8_lossy(slice);
        let res = toml::from_str(&config_str);
        ConfigPath::clear_base_path();
        res
    }

    /// Configures a clap app with the arguments to load the configuration.
    pub fn config_args(app: Command) -> Command {
        let app = app.arg(
            Arg::new(ARG_CONFIG)
                .short('c')
                .long(ARG_CONFIG)
                .required(true)
                .value_name("PATH")
                .help("Config file to use"),
        );
        LogConfig::config_args(app)
    }

    /// Loads the configuration based on command line options provided.
    ///
    /// The `matches` must be the result of getting argument matches from a
    /// clap app previously configured with
    /// [`config_args`](Self::config_args). Otherwise, the function is likely
    /// to panic.
    ///
    /// The current path needs to be provided to be able to deal with relative
    /// paths. The manager is necessary to resolve links given in the
    /// configuration.
    pub fn from_arg_matches(
        args: &ArgMatches,
        cur_dir: &Path,
        manager: &mut Manager,
    ) -> Result<(Source, Self), Terminate> {
        let config_file = {
            // With ARG_CONFIG required, we can unwrap here.
            let conf_path_arg = args.get_one::<String>(ARG_CONFIG).unwrap();
            let config_path = cur_dir.join(conf_path_arg);
            ConfigFile::load(&config_path).map_err(|err| {
                error!(
                    "Failed to read config file '{}': {}",
                    config_path.display(),
                    err
                );
                Terminate::error()
            })?
        };

        let mut config = manager.load(&config_file)?;
        config.log.update_with_arg_matches(args, cur_dir)?;
        config.finalise(config_file, manager)
    }

    pub fn from_config_file(
        config_file: ConfigFile,
        manager: &mut Manager,
    ) -> Result<(Source, Self), Terminate> {
        manager.load(&config_file)?.finalise(config_file, manager)
    }

    fn finalise(
        self,
        config_file: ConfigFile,
        manager: &mut Manager,
    ) -> Result<(Source, Self), Terminate> {
        self.log.switch_logging(false)?;

        if log::log_enabled!(log::Level::Trace) {
            trace!("After processing the config file looks like this:");
            trace!("{}", config_file.to_string());
        }

        manager.prepare(&self, &config_file)?;

        // Pass the config file path, as well as the processed config, back to
        // the caller so that they can monitor it for changes while the
        // application is running.
        Ok((config_file.source, self))
    }
}

//------------ Source --------------------------------------------------------

/// Description of the source of configuration.
///
/// This type is used for error reporting. It can refer to a configuration
/// file or an interactive session.
///
/// File names are kept behind and arc and thus this type can be cloned
/// cheaply.
#[derive(Clone, Debug, Default)]
pub struct Source {
    /// The optional path of a config file.
    ///
    /// If this in `None`, the source is an interactive session.
    path: Option<Arc<Path>>,
}

impl Source {
    pub fn is_path(&self) -> bool {
        self.path.is_some()
    }

    pub fn path(&self) -> &Option<Arc<Path>> {
        &self.path
    }
}

impl<'a, T: AsRef<Path>> From<&'a T> for Source {
    fn from(path: &'a T) -> Source {
        Source {
            path: Some(path.as_ref().into()),
        }
    }
}

//------------ LineCol -------------------------------------------------------

/// A pair of a line and column number.
///
/// This is used for error reporting.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct LineCol {
    pub line: usize,
    pub col: usize,
}

//------------ Marked --------------------------------------------------------

/// A value marked with its source location.
///
/// This wrapper is used when data needs to be resolved after parsing has
/// finished. In this case, we need information about the source location
/// to be able to produce meaningful error messages.
#[derive(Clone, Debug, Deserialize)]
#[serde(from = "Spanned<T>")]
pub struct Marked<T> {
    value: T,
    index: usize,
    source: Option<Source>,
    pos: Option<LineCol>,
}

impl<T> Marked<T> {
    /// Resolves the position for the given config file.
    pub fn resolve_config(&mut self, config: &ConfigFile) {
        self.source = Some(config.source.clone());
        self.pos = Some(config.resolve_pos(self.index));
    }

    /// Returns a reference to the value.
    pub fn as_inner(&self) -> &T {
        &self.value
    }

    /// Converts the marked value into is unmarked value.
    pub fn into_inner(self) -> T {
        self.value
    }

    /// Marks some other value with this value’s position.
    pub fn mark<U>(&self, value: U) -> Marked<U> {
        Marked {
            value,
            index: self.index,
            source: self.source.clone(),
            pos: self.pos,
        }
    }

    /// Formats the mark for displaying.
    pub fn format_mark(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let source =
            self.source.as_ref().and_then(|source| source.path.as_ref());
        match (source, self.pos) {
            (Some(source), Some(pos)) => {
                write!(f, "{}:{}:{}", source.display(), pos.line, pos.col)
            }
            (Some(source), None) => write!(f, "{}", source.display()),
            (None, Some(pos)) => write!(f, "{}:{}", pos.line, pos.col),
            (None, None) => Ok(()),
        }
    }
}

//--- From

impl<T> From<T> for Marked<T> {
    fn from(src: T) -> Marked<T> {
        Marked {
            value: src,
            index: 0,
            source: None,
            pos: None,
        }
    }
}

impl<T> From<Spanned<T>> for Marked<T> {
    fn from(src: Spanned<T>) -> Marked<T> {
        Marked {
            index: src.span().start,
            value: src.into_inner(),
            source: None,
            pos: None,
        }
    }
}

//--- Deref, AsRef, Borrow

impl<T> ops::Deref for Marked<T> {
    type Target = T;

    fn deref(&self) -> &T {
        self.as_inner()
    }
}

impl<T> AsRef<T> for Marked<T> {
    fn as_ref(&self) -> &T {
        self.as_inner()
    }
}

impl<T> borrow::Borrow<T> for Marked<T> {
    fn borrow(&self) -> &T {
        self.as_inner()
    }
}

//--- Display and Error

impl<T: fmt::Display> fmt::Display for Marked<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.format_mark(f)?;
        write!(f, ": {}", self.value)
    }
}

impl<T: error::Error> error::Error for Marked<T> {}

//------------ ConfigFile ----------------------------------------------------

/// A config file.
#[derive(Clone, Debug)]
#[cfg_attr(test, derive(Default))]
pub struct ConfigFile {
    /// The source for this file.
    source: Source,

    /// The data of this file.
    bytes: Vec<u8>,

    /// The start indexes of lines.
    ///
    /// The start index of the first line is in `line_start[0]` and so on.
    line_starts: Vec<usize>,
}

impl ConfigFile {
    /// Load a config file from disk.
    pub fn load(path: &impl AsRef<Path>) -> Result<Self, io::Error> {
        match fs::read(path) {
            Ok(bytes) => Self::new(bytes, path.into()),
            Err(e) => Err(e),
        }
    }

    pub fn new(bytes: Vec<u8>, source: Source) -> Result<Self, io::Error> {
        // Handle the special case of a rib unit that is actually a physical
        // rib unit and one or more virtual rib units. Rather than have the
        // user manually configure these separate rib units by hand in the
        // config file, we expand any units with unit `type = "rib"` and
        // `filter_name = [a, b, c, ...]` into multiple chained units each
        // with a single roto script path.
        //
        // Why don't we do this as part of the normal config deserialization
        // then unit/target/gate creation and linking? A single unit can't
        // currently during deserialization cause the creation of additional
        // units.
        //
        // One downside of the approach used here is it will cause line
        // numbers for config file syntax error reports to be confusing as we
        // are changing the users provided config file without them knowing.
        //
        // Another downside is that we have to lookup TOML fields by string
        // name and if the actual field names are later changed in the actual
        // unit and target config definitions those changes won't break
        // anything here at compile time, it will just cease to work as
        // expected at runtime. The "integration test" in main.rs exercises
        // this expansion capability to give at least some verification that
        // it is not obviously broken, but it's not enough.
        let config_str = String::from_utf8_lossy(&bytes);
        let mut toml: Value =
            if let Ok(toml) = toml::de::from_str(&config_str) {
                toml
            } else {
                return Err(io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    "Cannot parse config file",
                ));
            };
        let mut source_remappings = None;

        if let Some(Value::Table(units)) = toml.get_mut(CFG_UNITS) {
            source_remappings = Some(Self::expand_shorthand_vribs(units))
        }

        if let Some(source_remappings) = source_remappings {
            if let Some(Value::Table(targets)) = toml.get_mut(CFG_TARGETS) {
                Self::remap_sources(targets, &source_remappings);
            }
        }

        let config_str = toml::to_string(&toml).unwrap();

        Ok(ConfigFile {
            source,
            line_starts: config_str.as_bytes().split(|ch| *ch == b'\n').fold(
                vec![0],
                |mut starts, slice| {
                    starts.push(starts.last().unwrap() + slice.len());
                    starts
                },
            ),
            bytes: config_str.as_bytes().to_vec(),
        })
    }

    pub fn path(&self) -> Option<&Path> {
        self.source.path.as_ref().map(|path| path.as_ref())
    }

    pub fn dir(&self) -> Option<&Path> {
        self.source.path.as_ref().and_then(|path| path.parent())
    }

    pub fn bytes(&self) -> &[u8] {
        &self.bytes
    }

    pub fn to_string(&self) -> borrow::Cow<'_, str> {
        String::from_utf8_lossy(&self.bytes)
    }

    fn resolve_pos(&self, pos: usize) -> LineCol {
        let line = self
            .line_starts
            .iter()
            .find(|&&start| start < pos)
            .copied()
            .unwrap_or(self.line_starts.len());
        let line = line - 1;
        let col = self.line_starts[line] - pos;
        LineCol { line, col }
    }

    fn expand_shorthand_vribs(
        units: &mut toml::Table,
    ) -> HashMap<String, String> {
        let mut extra_units = HashMap::<String, Value>::new();
        let mut source_remappings = HashMap::<String, String>::new();

        for (unit_name, unit_table_value) in units.iter_mut() {
            if let Value::Table(unit_table) = unit_table_value {
                let unit_type = unit_table.get("type");
                let rib_type = unit_table.get("rib_type");
                #[allow(clippy::collapsible_if)]
                if unit_type == Some(&Value::String("rib".to_string())) {
                    if Option::is_none(&rib_type)
                        || rib_type
                            == Some(&Value::String("Physical".to_string()))
                    {
                        if let Some(Value::Array(filter_names)) =
                            unit_table.remove("filter_names")
                        {
                            if filter_names.len() > 1 {
                                // This is a shorthand definition of a physical RIB with one or more virtual RIBs.
                                // Split them out, e.g.:
                                //
                                //     [unit.shorthand_unit]
                                //     sources = ["a"]
                                //     type = "rib"
                                //     filter_names = ["pRib.roto", "vRib1.roto", "vRib2.roto"]
                                //
                                //     [unit.some_other_unit]
                                //     sources = ["shorthand_unit"]
                                //
                                // Expands to:
                                //
                                //     [unit.shorthand_unit]
                                //     sources = ["a"]
                                //     type = "rib"
                                //     filter_name = "pRib.roto"             # <-- changed
                                //     rib_type = "Physical"                 # <-- new
                                //
                                //     [unit.some_other_unit]
                                //     sources = ["shorthand_unit"]          # <-- no longer correct, see *1 below
                                //
                                //     [unit.shorthand_unit-vRIB-0]          # new
                                //     sources = ["shorthand_unit"]
                                //     type = "rib"
                                //     filter_name = "vRib1.roto"
                                //     vrib_upstream = "shorthand_unit"
                                //
                                //     [unit.shorthand_unit-vRIB-0.rib_type] # new
                                //     GeneratedVirtual = 0
                                //
                                //     [unit.shorthand_unit-vRIB-1]
                                //     sources = ["shorthand_unit-vRIB-0"]   # new
                                //     type = "rib"
                                //     filter_name = "vRib2.roto"
                                //     vrib_upstream = "shorthand_unit"
                                //
                                //     [unit.shorthand_unit-vRIB-1.rib_type] # new
                                //     GeneratedVirtual = 1
                                let mut source = unit_name.clone();
                                for (n, filter_name) in
                                    filter_names[1..].iter().enumerate()
                                {
                                    let mut new_unit_table =
                                        unit_table.clone();
                                    let new_unit_name =
                                        format!("{unit_name}-vRIB-{n}");
                                    new_unit_table.insert(
                                        "sources".to_string(),
                                        Value::Array(vec![Value::String(
                                            source.clone(),
                                        )]),
                                    );
                                    new_unit_table.insert(
                                        "filter_name".to_string(),
                                        filter_name.clone(),
                                    );
                                    let mut rib_type_table =
                                        toml::map::Map::new();
                                    rib_type_table.insert(
                                        "GeneratedVirtual".to_string(),
                                        Value::Integer(n.try_into().unwrap()),
                                    );
                                    new_unit_table.insert(
                                        "rib_type".to_string(),
                                        Value::Table(rib_type_table),
                                    );
                                    new_unit_table.insert(
                                        "vrib_upstream".to_string(),
                                        Value::String(unit_name.clone()),
                                    );
                                    extra_units.insert(
                                        new_unit_name.clone(),
                                        Value::Table(new_unit_table),
                                    );
                                    source = new_unit_name;
                                }

                                // Replace the multiple roto script paths used
                                // by the physical rib unit with just the
                                // first roto script path.
                                unit_table.insert(
                                    "filter_name".to_string(),
                                    filter_names[0].clone(),
                                );

                                // This unit should no longer be the source of
                                // another unit, rather the last vRIB that we
                                // added should replace it as source in all
                                // places it was used before. See *1 below.
                                source_remappings
                                    .insert(unit_name.clone(), source);
                            }
                        }
                    }
                }
            }
        }

        Self::remap_sources(units, &source_remappings);

        // Add the generated vRIB units into the unit set.
        units.extend(extra_units);

        source_remappings
    }

    fn remap_sources(
        units: &mut toml::Table,
        source_remappings: &HashMap<String, String>,
    ) {
        for (_unit_name, unit_table_value) in units.iter_mut() {
            if let Value::Table(unit_table) = unit_table_value {
                if let Some(source) = unit_table.get_mut("source") {
                    match source {
                        Value::String(old_source) => {
                            if let Some(new_source) =
                                source_remappings.get(old_source)
                            {
                                old_source.clone_from(new_source);
                            }
                        }

                        _ => unreachable!(),
                    }
                }
                if let Some(sources) = unit_table.get_mut("sources") {
                    match sources {
                        Value::String(old_source) => {
                            if let Some(new_source) =
                                source_remappings.get(old_source)
                            {
                                old_source.clone_from(new_source);
                            }
                        }

                        Value::Array(old_sources) => {
                            for old_source in old_sources {
                                match old_source {
                                    Value::String(old_source) => {
                                        if let Some(new_source) =
                                            source_remappings.get(old_source)
                                        {
                                            old_source.clone_from(new_source);
                                        }
                                    }

                                    _ => unreachable!(),
                                }
                            }
                        }

                        _ => unreachable!(),
                    }
                }
            }
        }
    }
}

//------------ ConfigError --------------------------------------------------

/// An error occurred during parsing of a configuration file.
#[derive(Clone, Debug)]
pub struct ConfigError {
    err: toml::de::Error,
    pos: Marked<()>,
}

impl ConfigError {
    pub fn new(err: toml::de::Error, file: &ConfigFile) -> Self {
        ConfigError {
            pos: Marked {
                value: (),
                index: 0,
                source: Some(file.source.clone()),
                pos: err.span().map(|span| file.resolve_pos(span.start)),
            },
            err,
        }
    }
}

impl fmt::Display for ConfigError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.pos.format_mark(f)?;
        write!(f, ": {}", self.err)
    }
}

impl error::Error for ConfigError {}

//------------ ConfigPath ----------------------------------------------------

/// A path that encountered in a config file.
///
/// This is a basically a `PathBuf` that, when, deserialized resolves all
/// relative paths from a certain base path so that all relative paths
/// encountered in a config file are automatically resolved relative to the
/// location of the config file.
#[derive(
    Clone, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd,
)]
#[serde(from = "String")]
pub struct ConfigPath(PathBuf);

impl ConfigPath {
    thread_local!(
        static BASE_PATH: RefCell<Option<PathBuf>> = RefCell::new(None)
    );

    fn set_base_path(path: PathBuf) {
        Self::BASE_PATH.with(|base_path| {
            base_path.replace(Some(path));
        })
    }

    fn clear_base_path() {
        Self::BASE_PATH.with(|base_path| {
            base_path.replace(None);
        })
    }
}

impl From<PathBuf> for ConfigPath {
    fn from(path: PathBuf) -> Self {
        Self(path)
    }
}

impl From<ConfigPath> for PathBuf {
    fn from(path: ConfigPath) -> Self {
        path.0
    }
}

impl From<String> for ConfigPath {
    fn from(path: String) -> Self {
        Self::BASE_PATH.with(|base_path| {
            ConfigPath(match base_path.borrow().as_ref() {
                Some(base_path) => base_path.join(path.as_str()),
                None => path.into(),
            })
        })
    }
}

impl ops::Deref for ConfigPath {
    type Target = Path;

    fn deref(&self) -> &Self::Target {
        self.0.as_ref()
    }
}

impl AsRef<Path> for ConfigPath {
    fn as_ref(&self) -> &Path {
        self.0.as_ref()
    }
}