1
- #[ cfg( feature = "std" ) ]
2
- use std:: time:: { Instant , Duration } ;
3
-
4
1
use Error ;
5
2
use super :: { DeviceLimits , Device } ;
6
3
@@ -24,22 +21,16 @@ struct Config {
24
21
drop_pct : u8 ,
25
22
reorder_pct : u8 ,
26
23
max_size : usize ,
27
- #[ cfg( feature = "std" ) ]
28
24
max_tx_rate : u64 ,
29
- #[ cfg( feature = "std" ) ]
30
25
max_rx_rate : u64 ,
31
- #[ cfg( feature = "std" ) ]
32
- interval : Duration ,
26
+ interval : u64 ,
33
27
}
34
28
35
29
#[ derive( Debug , Clone , Copy ) ]
36
30
struct State {
37
31
rng_seed : u32 ,
38
- #[ cfg( feature = "std" ) ]
39
- refilled_at : Instant ,
40
- #[ cfg( feature = "std" ) ]
32
+ refilled_at : u64 ,
41
33
tx_bucket : u64 ,
42
- #[ cfg( feature = "std" ) ]
43
34
rx_bucket : u64 ,
44
35
}
45
36
@@ -56,20 +47,18 @@ impl State {
56
47
buffer[ index] ^= bit;
57
48
}
58
49
59
- #[ cfg( feature = "std" ) ]
60
- fn refill ( & mut self , config : & Config ) {
61
- if self . refilled_at . elapsed ( ) > config. interval {
50
+ fn refill ( & mut self , config : & Config , timestamp : u64 ) {
51
+ if self . refilled_at - timestamp > config. interval {
62
52
self . tx_bucket = config. max_tx_rate ;
63
53
self . rx_bucket = config. max_rx_rate ;
64
- self . refilled_at = Instant :: now ( ) ;
54
+ self . refilled_at = timestamp ;
65
55
}
66
56
}
67
57
68
- #[ cfg( feature = "std" ) ]
69
- fn maybe_transmit ( & mut self , config : & Config ) -> bool {
58
+ fn maybe_transmit ( & mut self , config : & Config , timestamp : u64 ) -> bool {
70
59
if config. max_tx_rate == 0 { return true }
71
60
72
- self . refill ( config) ;
61
+ self . refill ( config, timestamp ) ;
73
62
if self . tx_bucket > 0 {
74
63
self . tx_bucket -= 1 ;
75
64
true
@@ -78,28 +67,17 @@ impl State {
78
67
}
79
68
}
80
69
81
- #[ cfg( not( feature = "std" ) ) ]
82
- fn maybe_transmit ( & mut self , _config : & Config ) -> bool {
83
- true
84
- }
85
-
86
- #[ cfg( feature = "std" ) ]
87
- fn maybe_receive ( & mut self , config : & Config ) -> bool {
70
+ fn maybe_receive ( & mut self , config : & Config , timestamp : u64 ) -> bool {
88
71
if config. max_rx_rate == 0 { return true }
89
72
90
- self . refill ( config) ;
73
+ self . refill ( config, timestamp ) ;
91
74
if self . rx_bucket > 0 {
92
75
self . rx_bucket -= 1 ;
93
76
true
94
77
} else {
95
78
false
96
79
}
97
80
}
98
-
99
- #[ cfg( not( feature = "std" ) ) ]
100
- fn maybe_receive ( & mut self , _config : & Config ) -> bool {
101
- true
102
- }
103
81
}
104
82
105
83
/// A fault injector device.
@@ -117,17 +95,12 @@ pub struct FaultInjector<D: Device> {
117
95
impl < D : Device > FaultInjector < D > {
118
96
/// Create a fault injector device, using the given random number generator seed.
119
97
pub fn new ( inner : D , seed : u32 ) -> FaultInjector < D > {
120
- #[ cfg( feature = "std" ) ]
121
98
let state = State {
122
99
rng_seed : seed,
123
- refilled_at : Instant :: now ( ) ,
100
+ refilled_at : 0 ,
124
101
tx_bucket : 0 ,
125
102
rx_bucket : 0 ,
126
103
} ;
127
- #[ cfg( not( feature = "std" ) ) ]
128
- let state = State {
129
- rng_seed : seed,
130
- } ;
131
104
FaultInjector {
132
105
inner : inner,
133
106
state : state,
@@ -156,20 +129,17 @@ impl<D: Device> FaultInjector<D> {
156
129
}
157
130
158
131
/// Return the maximum packet transmission rate, in packets per second.
159
- #[ cfg( feature = "std" ) ]
160
132
pub fn max_tx_rate ( & self ) -> u64 {
161
133
self . config . max_rx_rate
162
134
}
163
135
164
136
/// Return the maximum packet reception rate, in packets per second.
165
- #[ cfg( feature = "std" ) ]
166
137
pub fn max_rx_rate ( & self ) -> u64 {
167
138
self . config . max_tx_rate
168
139
}
169
140
170
141
/// Return the interval for packet rate limiting, in milliseconds.
171
- #[ cfg( feature = "std" ) ]
172
- pub fn bucket_interval ( & self ) -> Duration {
142
+ pub fn bucket_interval ( & self ) -> u64 {
173
143
self . config . interval
174
144
}
175
145
@@ -197,21 +167,18 @@ impl<D: Device> FaultInjector<D> {
197
167
}
198
168
199
169
/// Set the maximum packet transmission rate, in packets per interval.
200
- #[ cfg( feature = "std" ) ]
201
170
pub fn set_max_tx_rate ( & mut self , rate : u64 ) {
202
171
self . config . max_tx_rate = rate
203
172
}
204
173
205
174
/// Set the maximum packet reception rate, in packets per interval.
206
- #[ cfg( feature = "std" ) ]
207
175
pub fn set_max_rx_rate ( & mut self , rate : u64 ) {
208
176
self . config . max_rx_rate = rate
209
177
}
210
178
211
179
/// Set the interval for packet rate limiting, in milliseconds.
212
- #[ cfg( feature = "std" ) ]
213
- pub fn set_bucket_interval ( & mut self , interval : Duration ) {
214
- self . state . refilled_at = Instant :: now ( ) - self . config . interval ;
180
+ pub fn set_bucket_interval ( & mut self , interval : u64 ) {
181
+ self . state . refilled_at = 0 ;
215
182
self . config . interval = interval
216
183
}
217
184
}
@@ -243,7 +210,7 @@ impl<D: Device> Device for FaultInjector<D>
243
210
net_trace ! ( "rx: dropping a packet that is too large" ) ;
244
211
return Err ( Error :: Exhausted )
245
212
}
246
- if !self . state . maybe_receive ( & self . config ) {
213
+ if !self . state . maybe_receive ( & self . config , timestamp ) {
247
214
net_trace ! ( "rx: dropping a packet because of rate limiting" ) ;
248
215
return Err ( Error :: Exhausted )
249
216
}
@@ -258,7 +225,7 @@ impl<D: Device> Device for FaultInjector<D>
258
225
} else if self . config . max_size > 0 && length > self . config . max_size {
259
226
net_trace ! ( "tx: dropping a packet that is too large" ) ;
260
227
buffer = None ;
261
- } else if !self . state . maybe_transmit ( & self . config ) {
228
+ } else if !self . state . maybe_transmit ( & self . config , timestamp ) {
262
229
net_trace ! ( "tx: dropping a packet because of rate limiting" ) ;
263
230
buffer = None ;
264
231
} else {
0 commit comments