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
#![allow(non_snake_case)]

use portus::ipc;
use portus::ipc::BackendBuilder;
use portus::lang::Scope;
use portus::{DatapathTrait, Report};
use pyo3::prelude::*;
use pyo3::types::*;
use pyo3::{exceptions, ToPyObject};
use simple_signal::Signal;
use std::rc::{Rc, Weak};

#[macro_export]
macro_rules! raise {
    ($errtype:ident, $msg:expr) => {
        Err(PyErr::new::<exceptions::$errtype, _>($msg))
    };
}

macro_rules! py_none {
    ($py:expr) => {
        PyTuple::empty($py).into_ptr()
    };
}

mod cong_alg;
use cong_alg::*;

// Copy of the datapath class, $[pyo3(get)] necessary to access the fields in python
#[pyclass(weakref, dict)]
pub struct DatapathInfo {
    #[pyo3(get)]
    pub sock_id: u32,
    #[pyo3(get)]
    pub init_cwnd: u32,
    #[pyo3(get)]
    pub mss: u32,
    #[pyo3(get)]
    pub src_ip: u32,
    #[pyo3(get)]
    pub src_port: u32,
    #[pyo3(get)]
    pub dst_ip: u32,
    #[pyo3(get)]
    pub dst_port: u32,
}

#[pyclass(weakref, dict)]
pub struct Measurements {
    #[pyo3(get)]
    pub acked: u32,
    #[pyo3(get)]
    pub was_timeout: bool,
    #[pyo3(get)]
    pub sacked: u32,
    #[pyo3(get)]
    pub loss: u32,
    #[pyo3(get)]
    pub rtt: u32,
    #[pyo3(get)]
    pub inflight: u32,
}

/// Convenience wrapper around the Report struct for sending to python,
/// keeps a copy of the scope so the python user doesn't need to manage it
#[pyclass(weakref, dict, unsendable)]
struct PyReport {
    report: Report,
    sc: Weak<Scope>,
}

#[pymethods]
impl PyReport {
    #[getter]
    fn __getattr__(&self, name: String) -> PyResult<u64> {
        let field_name = match name.as_ref() {
            "Cwnd" | "Rate" => name.clone(),
            _ => format!("Report.{}", name),
        };
        let sc = match self.sc.upgrade() {
            Some(sc) => sc,
            None => {
                return raise!(
                    PyException,
                    format!(
                        "Failed to get {}: no datapath program installed",
                        field_name.clone()
                    )
                );
            }
        };
        match self.report.get_field(field_name.as_ref(), &sc) {
            Ok(val) => Ok(val),
            Err(portus::Error(e)) => raise!(PyException, format!("Failed to get {}: {}", name, e)),
        }
    }
}

fn get_fields(list: &PyList) -> Vec<(&str, u32)> {
    list.into_iter()
        .map(|tuple_ref| {
            tuple_ref.extract()
                .expect("second argument to datapath.set_program must be a list of tuples of the form (string, int)")
        })
    .collect::<_>()
}

/// Convenience wrapper around datapath struct,
/// python keeps a pointer to this for talking to the datapath
#[pyclass(weakref, dict, unsendable)]
struct PyDatapath {
    backend: Box<dyn DatapathTrait>,
    sc: Option<Rc<Scope>>,
    sock_id: u32,
}

#[pymethods]
impl PyDatapath {
    fn update_field(&self, _py: Python, reg_name: String, val: u32) -> PyResult<()> {
        tracing::debug!(sock_id = ?self.sock_id, ?reg_name, ?val, "Updating field");
        let sc = match self.sc {
            Some(ref s) => s,
            None => {
                return raise!(
                    PyReferenceError,
                    "Cannot update field: no datapath program installed yet!"
                );
            }
        };
        match self.backend.update_field(sc, &[(reg_name.as_str(), val)]) {
            Ok(()) => Ok(()),
            Err(e) => raise!(PyException, format!("Failed to update field, err: {:?}", e)),
        }
    }

    fn update_fields(&self, _py: Python, fields: &PyList) -> PyResult<()> {
        tracing::debug!(sock_id = ?self.sock_id, ?fields, "Updating fields");
        let sc = match self.sc {
            Some(ref s) => s,
            None => {
                return raise!(
                    PyReferenceError,
                    "Cannot update field: no datapath program installed yet!"
                );
            }
        };

        let ret = self.backend.update_field(sc, &get_fields(fields)[..]);

        match ret {
            Ok(()) => Ok(()),
            Err(e) => raise!(
                PyException,
                format!("Failed to update fields, err: {:?}", e)
            ),
        }
    }

    fn set_program(
        &mut self,
        _py: Python,
        program_name: &str,
        fields: Option<&PyList>,
    ) -> PyResult<()> {
        tracing::debug!(sock_id = ?self.sock_id, ?program_name, "switching datapath programs");

        // we have a &'py str and need a &'static str.
        //
        // SAFETY: *in practice*, portus does not rely on the 'static lifetime of program_name (it
        // does not keep it around, only uses it to lookup in its HashMap); therefore, the lifetime of
        // `program_name`, `'py`, is sufficient.
        // If `backend.set_program` changes to keep the string around, then this will no longer
        // work.
        let pname: &'static str = unsafe { std::mem::transmute(program_name) };

        let ret = self.backend.set_program(
            pname,
            fields
                .map(|list| get_fields(list))
                .as_ref()
                .map(|x| x.as_slice()),
        );

        match ret {
            Ok(sc) => {
                self.sc = Some(Rc::new(sc));
                Ok(())
            }
            Err(e) => raise!(
                PyException,
                format!("Failed to set datapath program: {:?}", e)
            ),
        }
    }
}

#[pymodule]
fn pyportus(_py: Python, m: &PyModule) -> PyResult<()> {
    #[pyfn(m)]
    fn start_inner(py: Python, ipc_str: String, alg: PyObject) -> PyResult<i32> {
        simple_signal::set_handler(&[Signal::Int, Signal::Term], move |_signals| {
            tracing::info!("exiting");
            ::std::process::exit(1);
        });

        py_start_inner(py, ipc_str, alg)
    }

    #[pyfn(m)]
    fn try_compile(py: Python, prog: String) -> PyResult<String> {
        py_try_compile(py, prog)
    }

    m.add_class::<DatapathInfo>()?;
    m.add_class::<PyDatapath>()?;
    m.add_class::<PyReport>()?;
    Ok(())
}

fn py_start_inner<'p>(py: Python<'p>, ipc: String, alg: PyObject) -> PyResult<i32> {
    // Check args
    if let Err(e) = portus::algs::ipc_valid(ipc.clone()) {
        return raise!(PyValueError, e);
    };

    tracing_subscriber::fmt::init();

    let py_cong_alg = PyCongAlg { py, alg_obj: alg };

    // SAFETY: _connect will block the Python program, so really we will hold the GIL for
    // the remainder of the program's lifetime, which is 'static.
    let py_cong_alg: PyCongAlg<'static> = unsafe { std::mem::transmute(py_cong_alg) };
    tracing::info!(?ipc, "starting CCP");
    match ipc.as_str() {
        "unix" => {
            use ipc::unix::Socket;
            let b = Socket::<ipc::Blocking>::new("portus")
                .map(|sk| BackendBuilder { sock: sk })
                .expect("create unix socket");
            portus::RunBuilder::new(b).default_alg(py_cong_alg).run()
        }
        #[cfg(all(target_os = "linux"))]
        "netlink" => {
            use ipc::netlink::Socket;
            let b = Socket::<ipc::Blocking>::new()
                .map(|sk| BackendBuilder { sock: sk })
                .expect("create netlink socket");
            portus::RunBuilder::new(b).default_alg(py_cong_alg).run()
        }
        _ => unreachable!(),
    }
    .or_else(|e| raise!(PyException, format!("{:?}", e)))?;
    Ok(0)
}

fn py_try_compile<'p>(_py: Python<'p>, prog: String) -> PyResult<String> {
    use portus::lang;
    match lang::compile(prog.as_bytes(), &[]) {
        Ok(_) => Ok("".to_string()),
        Err(e) => Ok(format!("{}", e)),
    }
}

// Creates an instance of cls and calls __init__(self, *args, **kwargs)
// Returns a pointer to the instance
fn _py_new_instance(
    py: Python,
    cls: *mut pyo3::ffi::PyTypeObject,
    args: *mut pyo3::ffi::PyObject,
    kwargs: *mut pyo3::ffi::PyObject,
) -> PyResult<PyObject> {
    use std::os::raw::c_int;
    unsafe {
        match (*cls).tp_new {
            Some(tp_new) => {
                let obj =
                    pyo3::PyObject::from_owned_ptr(py, tp_new(cls, py_none!(py), py_none!(py)));
                match (*cls).tp_init {
                    Some(tp_init) => {
                        let p = (&obj).into_ptr();
                        let ret: c_int = tp_init(p, args, kwargs);
                        // If there's an error in init, print the traceback
                        if ret < 0 {
                            pyo3::ffi::PyErr_PrintEx(0);
                        }
                        Ok(obj)
                    }
                    None => Ok(py.None()),
                }
            }
            None => Ok(py.None()),
        }
    }
}

pub fn py_setattr<N, V>(o: &PyObject, py: Python, attr_name: N, val: V) -> PyResult<()>
where
    N: ToPyObject,
    V: ToPyObject,
{
    use pyo3::AsPyPointer;
    unsafe {
        let ret = pyo3::ffi::PyObject_SetAttr(
            o.as_ptr(),
            attr_name.to_object(py).as_ptr(),
            val.to_object(py).as_ptr(),
        );
        if ret != -1 {
            Ok(())
        } else {
            Err(PyErr::fetch(py))
        }
    }
}