summaryrefslogtreecommitdiff
path: root/crates/tor-cell/src/relaycell/flow_ctrl.rs
blob: d5e61d33f3ef89576f35d4ff08998b77f981e239 (plain)
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
//! Cells for flow control (excluding "sendme" cells).

use std::num::NonZero;

use derive_deftly::Deftly;
use tor_bytes::{EncodeResult, Error, Reader, Writer};
use tor_memquota::derive_deftly_template_HasMemoryCost;

use crate::relaycell::msg::Body;

/// An `XON` relay message.
#[derive(Clone, Debug, Deftly)]
#[derive_deftly(HasMemoryCost)]
pub struct Xon {
    /// Cell `version` field.
    version: FlowCtrlVersion,
    /// Cell `kbps_ewma` field.
    kbps_ewma: XonKbpsEwma,
}

/// An `XOFF` relay message.
#[derive(Clone, Debug, Deftly)]
#[derive_deftly(HasMemoryCost)]
pub struct Xoff {
    /// Cell `version` field.
    version: FlowCtrlVersion,
}

impl Xon {
    /// Construct a new [`Xon`] cell.
    pub fn new(version: FlowCtrlVersion, kbps_ewma: XonKbpsEwma) -> Self {
        Self { version, kbps_ewma }
    }

    /// Return the version.
    pub fn version(&self) -> FlowCtrlVersion {
        self.version
    }

    /// Return the rate limit in kbps.
    pub fn kbps_ewma(&self) -> XonKbpsEwma {
        self.kbps_ewma
    }
}

impl Body for Xon {
    fn decode_from_reader(r: &mut Reader<'_>) -> tor_bytes::Result<Self> {
        let version = r.take_u8()?;

        let version = match FlowCtrlVersion::new(version) {
            Ok(x) => x,
            Err(UnrecognizedVersionError) => {
                return Err(Error::InvalidMessage("Unrecognized XON version.".into()));
            }
        };

        let kbps_ewma = XonKbpsEwma::decode(r.take_u32()?);

        Ok(Self::new(version, kbps_ewma))
    }

    fn encode_onto<W: Writer + ?Sized>(self, w: &mut W) -> EncodeResult<()> {
        w.write_u8(*self.version);
        w.write_u32(self.kbps_ewma.encode());
        Ok(())
    }
}

impl Xoff {
    /// Construct a new [`Xoff`] cell.
    pub fn new(version: FlowCtrlVersion) -> Self {
        Self { version }
    }

    /// Return the version.
    pub fn version(&self) -> FlowCtrlVersion {
        self.version
    }
}

impl Body for Xoff {
    fn decode_from_reader(r: &mut Reader<'_>) -> tor_bytes::Result<Self> {
        let version = r.take_u8()?;

        let version = match FlowCtrlVersion::new(version) {
            Ok(x) => x,
            Err(UnrecognizedVersionError) => {
                return Err(Error::InvalidMessage("Unrecognized XOFF version.".into()));
            }
        };

        Ok(Self::new(version))
    }

    fn encode_onto<W: Writer + ?Sized>(self, w: &mut W) -> EncodeResult<()> {
        w.write_u8(*self.version);
        Ok(())
    }
}

/// A recognized flow control version.
#[derive(Copy, Clone, Debug, Deftly)]
#[derive_deftly(HasMemoryCost)]
pub struct FlowCtrlVersion(u8);

impl FlowCtrlVersion {
    /// Version 0, which is currently the only known version.
    pub const V0: Self = Self(0);

    /// If `version` is a recognized XON/XOFF version, returns a new [`FlowCtrlVersion`].
    pub const fn new(version: u8) -> Result<Self, UnrecognizedVersionError> {
        if version != 0 {
            return Err(UnrecognizedVersionError);
        }

        Ok(Self(version))
    }
}

impl TryFrom<u8> for FlowCtrlVersion {
    type Error = UnrecognizedVersionError;

    fn try_from(x: u8) -> Result<Self, Self::Error> {
        Self::new(x)
    }
}

impl std::ops::Deref for FlowCtrlVersion {
    type Target = u8;

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

/// The XON/XOFF cell version was not recognized.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct UnrecognizedVersionError;

/// The `kbps_ewma` field of an XON cell.
#[derive(Copy, Clone, Debug, Deftly)]
#[derive_deftly(HasMemoryCost)]
#[allow(clippy::exhaustive_enums)]
pub enum XonKbpsEwma {
    /// Stream is rate limited to the value in kbps.
    Limited(NonZero<u32>),
    /// Stream is not rate limited.
    Unlimited,
}

impl XonKbpsEwma {
    /// Decode the `kbps_ewma` field of an XON cell.
    fn decode(kbps_ewma: u32) -> Self {
        // prop-324:
        // > In `xon_cell`, a zero value for `kbps_ewma` means that the stream's rate is unlimited.
        match NonZero::new(kbps_ewma) {
            Some(x) => Self::Limited(x),
            None => Self::Unlimited,
        }
    }

    /// Encode as the `kbps_ewma` field of an XON cell.
    fn encode(&self) -> u32 {
        // prop-324:
        // > In `xon_cell`, a zero value for `kbps_ewma` means that the stream's rate is unlimited.
        match self {
            Self::Limited(x) => x.get(),
            Self::Unlimited => 0,
        }
    }
}

impl std::fmt::Display for XonKbpsEwma {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Limited(rate) => write!(f, "{rate} kbps"),
            Self::Unlimited => write!(f, "unlimited"),
        }
    }
}