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
use clap;
use clap::Arg;
use portus;
use portus::ipc::{BackendBuilder, Blocking};
use slog;
use std;
use time;
use {
    Alg, GenericCongAvoidAlg, GenericCongAvoidConfigReport,
    GenericCongAvoidConfigSS, DEFAULT_SS_THRESH,
};

pub fn make_args<A: GenericCongAvoidAlg>(
    name: &str,
    logger: impl Into<Option<slog::Logger>>,
) -> Result<(Alg<A>, String), std::num::ParseIntError> {
    let ss_thresh_default = format!("{}", DEFAULT_SS_THRESH);
    let matches = clap::App::new(name)
        .version("0.2.0")
        .author("Akshay Narayan <akshayn@mit.edu>")
        .about("CCP implementation of a congestion avoidance algorithm")
        .arg(Arg::with_name("ipc")
             .long("ipc")
             .help("Sets the type of ipc to use: (netlink|unix)")
             .default_value("unix")
             .validator(portus::algs::ipc_valid))
        .arg(Arg::with_name("init_cwnd")
             .long("init_cwnd")
             .help("Sets the initial congestion window, in bytes. Setting 0 will use datapath default.")
             .default_value("0"))
        .arg(Arg::with_name("ss_thresh")
             .long("ss_thresh")
             .help("Sets the slow start threshold, in bytes")
             .default_value(&ss_thresh_default))
        .arg(Arg::with_name("ss_in_fold")
             .long("ss_in_fold")
             .help("Implement slow start in the datapath"))
        .arg(Arg::with_name("report_per_ack")
             .long("per_ack")
             .help("Specifies that the datapath should send a measurement upon every ACK"))
        .arg(Arg::with_name("report_per_interval")
             .long("report_interval_ms")
             .short("i")
             .takes_value(true))
        .group(clap::ArgGroup::with_name("interval")
             .args(&["report_per_ack", "report_per_interval"])
             .required(false))
        .arg(Arg::with_name("compensate_update")
             .long("compensate_update")
             .help("Scale the congestion window update during slow start to compensate for reporting delay"))
        .arg(Arg::with_name("deficit_timeout")
             .long("deficit_timeout")
             .default_value("0")
             .help("Number of RTTs to wait after a loss event to allow further CWND reductions. \
                   Default 0 means CWND deficit counting is enforced strictly with no timeout."))
        .args(&A::args())
        .get_matches();

    let ipc = String::from(matches.value_of("ipc").unwrap());

    Ok((
        Alg {
            ss_thresh: u32::from_str_radix(matches.value_of("ss_thresh").unwrap(), 10)?,
            init_cwnd: u32::from_str_radix(matches.value_of("init_cwnd").unwrap(), 10)?,
            report_option: if matches.is_present("report_per_ack") {
                GenericCongAvoidConfigReport::Ack
            } else if matches.is_present("report_per_interval") {
                GenericCongAvoidConfigReport::Interval(time::Duration::milliseconds(
                    matches
                        .value_of("report_per_interval")
                        .unwrap()
                        .parse()
                        .unwrap(),
                ))
            } else {
                GenericCongAvoidConfigReport::Rtt
            },
            ss: if matches.is_present("ss_in_fold") {
                GenericCongAvoidConfigSS::Datapath
            } else {
                GenericCongAvoidConfigSS::Ccp
            },
            use_compensation: matches.is_present("compensate_update"),
            deficit_timeout: u32::from_str_radix(matches.value_of("deficit_timeout").unwrap(), 10)?,
            logger: logger.into(),
            alg: A::with_args(matches),
        },
        ipc,
    ))
}

pub fn start<A: GenericCongAvoidAlg>(ipc: &str, log: slog::Logger, alg: Alg<A>)
where
    A: 'static,
{
    match ipc {
        "unix" => {
            use portus::ipc::unix::Socket;
            let b = Socket::<Blocking>::new("in", "out")
                .map(|sk| BackendBuilder { sock: sk })
                .expect("ipc initialization");
            portus::run::<_, Alg<A>>(
                b,
                portus::Config {
                    logger: Some(log),
                },
                alg,
            )
            .unwrap();
        }
        #[cfg(all(target_os = "linux"))]
        "netlink" => {
            use portus::ipc::netlink::Socket;
            let b = Socket::<Blocking>::new()
                .map(|sk| BackendBuilder { sock: sk })
                .expect("ipc initialization");
            portus::run::<_, Alg<A>>(
                b,
                portus::Config {
                    logger: Some(log),
                },
                alg
            )
            .unwrap();
        }
        #[cfg(all(target_os = "linux"))]
        "char" => {
            use portus::ipc::kp::Socket;
            let b = Socket::<Blocking>::new()
                .map(|sk| BackendBuilder { sock: sk })
                .expect("char initialization");
            portus::run::<_, Alg<A>>(
                b,
                portus::Config {
                    logger: Some(log),
                },
                alg
            )
            .unwrap()
        }
        _ => unreachable!(),
    }
}