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
use crate::error::{Error, Result};
use crate::link::reconcile::StoredRewrite;
use crate::protocol::command::rewrite_payload;
use crate::protocol::opcode::{
MAX_PAYLOAD, Q_REWRITE, Q_REWRITE_ENTRY, REWRITE_MATCH_MAX, REWRITE_MAX_ENTRIES,
REWRITE_PAYLOAD_POOL,
};
use crate::protocol::{FrameType, Resp, parse_resp};
use crate::types::rewrite::{REWRITE_CLEAR_ID, rewrite_entry_from_payload};
use crate::types::{Direction, RewriteAction, RewriteClass, RewriteRule, RewriteTable};
use super::Device;
impl Device {
/// `REWRITE` (§3.14): add or overwrite one rewrite rule.
///
/// The box's rewrite table matches traffic in flight and rewrites, answers, refuses or drops it
/// per the rule's [`action`](RewriteRule::action). Keyed by `(class, id, direction, match,
/// mask)`; setting an existing key overwrites it. Rules are session state, kept by the
/// keepalive like a [`lock`](Device::lock) and re-asserted after a reconnect, a device-chip
/// restart or any session release the box counts. The box clears them on control-PC silence,
/// [`reset`](Device::reset), a device detach, a link drop, a re-clone, or the opt-in going off.
///
/// A new rule beyond [`REWRITE_MAX_ENTRIES`](crate::REWRITE_MAX_ENTRIES) gives
/// [`Error::RewriteTableFull`](crate::Error::RewriteTableFull). A payload larger than what the
/// other held rules leave of [`REWRITE_PAYLOAD_POOL`](crate::REWRITE_PAYLOAD_POOL) gives
/// [`Error::RewritePoolFull`](crate::Error::RewritePoolFull).
///
/// Gated on [`allow_imperfect_clones`](Device::allow_imperfect_clones): with the opt-in off,
/// [`Error::ImperfectRequired`](crate::Error::ImperfectRequired). `match` and `mask` must be
/// the same length ([`Error::RewriteMaskLength`](crate::Error::RewriteMaskLength)), `action`
/// valid for `class` ([`Error::RewriteActionClass`](crate::Error::RewriteActionClass)), and
/// `direction` not bearing-relative
/// ([`Error::RelativeDirection`](crate::Error::RelativeDirection)). Fire-and-forget:
/// [`query_rewrite`](Device::query_rewrite) confirms what the box holds.
pub fn set_rewrite(&self, rule: &RewriteRule) -> Result<()> {
validate_rule(rule)?;
self.require_imperfect()?;
self.set_rewrite_checked(rule)
}
// Capacity check and send run under one re-assert guard, so two threads can't both take the
// last slot. The async wrapper, which checks the opt-in first, gets the check too. The guard
// isn't reentrant: taking it again in `set_rewrite_send_locked` would deadlock.
pub(crate) fn set_rewrite_checked(&self, rule: &RewriteRule) -> Result<()> {
let _serial = self.link.reassert_guard();
{
let d = self.link.desired().lock();
let key = to_stored(rule).key();
// Costed like the box: payload against the pool less what an overwrite frees, then the
// entry count for a new key.
let free = REWRITE_PAYLOAD_POOL.saturating_sub(d.rewrite_pool_used_except(&key));
if rule.payload.len() > free {
return Err(Error::RewritePoolFull {
len: rule.payload.len(),
free,
limit: REWRITE_PAYLOAD_POOL,
});
}
if !d.holds_rewrite(&key) && d.rewrite_count() >= REWRITE_MAX_ENTRIES {
return Err(Error::RewriteTableFull {
limit: REWRITE_MAX_ENTRIES,
});
}
}
self.set_rewrite_send_locked(rule)
}
// No opt-in pre-check, so the async wrapper can gate on the async query path. The caller holds
// the re-assert guard, so a concurrent remove/clear cannot interleave. Recorded before the
// write so a racing reconnect replays it; rolled back if the frame was never sent.
fn set_rewrite_send_locked(&self, rule: &RewriteRule) -> Result<()> {
let stored = to_stored(rule);
let undo = self.link.desired().lock().apply_rewrite(stored);
let sent = self.link.send(
FrameType::Rewrite,
&rewrite_payload(
rule.class.as_u8(),
rule.id,
rule.direction.as_u8(),
1,
rule.action.as_u8(),
rule.offset,
&rule.match_bytes,
&rule.mask,
&rule.payload,
),
);
if sent.is_err() {
self.link.desired().lock().restore_rewrite(undo);
}
sent
}
/// `REWRITE` remove (§3.14): drop the rule with this rule's `(class, id, direction, match, mask)`
/// key; action and payload are ignored. A no-op on the box if no such rule is held.
pub fn remove_rewrite(&self, rule: &RewriteRule) -> Result<()> {
if rule.match_bytes.len() != rule.mask.len() {
return Err(Error::RewriteMaskLength {
match_len: rule.match_bytes.len(),
mask_len: rule.mask.len(),
});
}
let key = to_stored(rule).key();
// Serialised against re-asserts, or a keepalive tick can re-send a stale add after the
// remove and leave the rule live.
let _serial = self.link.reassert_guard();
let undo = self.link.desired().lock().remove_rewrite(key);
let sent = self.link.send(
FrameType::Rewrite,
&rewrite_payload(
rule.class.as_u8(),
rule.id,
rule.direction.as_u8(),
0,
rule.action.as_u8(),
rule.offset,
&rule.match_bytes,
&rule.mask,
&[],
),
);
if sent.is_err() {
self.link.desired().lock().restore_rewrite(undo);
}
sent
}
/// `REWRITE` clear (§3.14): drop the whole table (the `class 0xFF, id 0xFFFF, state 0` blanket).
/// Always clears the crate's held rules, whatever the opt-in.
pub fn clear_rewrite(&self) -> Result<()> {
// Serialised against re-asserts; the snapshot restores the rules if the send fails (the box
// still holds them).
let _serial = self.link.reassert_guard();
let held = {
let mut d = self.link.desired().lock();
let held = d.held_rewrites();
d.clear_rewrites();
held
};
let sent = self.link.send(
FrameType::Rewrite,
&rewrite_payload(
RewriteClass::Any.as_u8(),
REWRITE_CLEAR_ID,
Direction::Both.as_u8(),
0,
0,
0,
&[],
&[],
&[],
),
);
if sent.is_err() {
let mut d = self.link.desired().lock();
for r in held {
d.apply_rewrite(r);
}
}
sent
}
/// `QUERY(REWRITE)` → [`RewriteTable`] (§4.17): table summary (flags, generation, a row per rule
/// without match/mask/payload bytes). [`query_rewrite_entry`](Device::query_rewrite_entry) reads
/// one rule in full.
pub fn query_rewrite(&self) -> Result<RewriteTable> {
let payload = self.link.query(Q_REWRITE)?;
match parse_resp(&payload) {
Some(Resp::Rewrite(t)) => Ok(t),
_ => Err(Error::NoReply),
}
}
/// `QUERY(REWRITE_ENTRY, index)` → [`RewriteRule`] (§4.17): one rule in full, in
/// [`set_rewrite`](Device::set_rewrite)'s shape so it replays as a set. `index` is the row in the
/// [`query_rewrite`](Device::query_rewrite) summary.
pub fn query_rewrite_entry(&self, index: u8) -> Result<RewriteRule> {
let payload = self.link.query_indexed(Q_REWRITE_ENTRY, index)?;
rewrite_entry_from_payload(&payload).ok_or(Error::NoReply)
}
}
pub(crate) fn to_stored(rule: &RewriteRule) -> StoredRewrite {
StoredRewrite {
class: rule.class.as_u8(),
id: rule.id,
direction: rule.direction.as_u8(),
action: rule.action.as_u8(),
offset: rule.offset,
match_bytes: rule.match_bytes.clone(),
mask: rule.mask.clone(),
payload: rule.payload.clone(),
}
}
pub(crate) fn validate_rule(rule: &RewriteRule) -> Result<()> {
if rule.match_bytes.len() != rule.mask.len() {
return Err(Error::RewriteMaskLength {
match_len: rule.match_bytes.len(),
mask_len: rule.mask.len(),
});
}
if rule.match_bytes.len() > REWRITE_MATCH_MAX {
return Err(Error::RewriteMatchTooLong {
len: rule.match_bytes.len(),
limit: REWRITE_MATCH_MAX,
});
}
// The box rejects the bearing-relative pair by range and drops the frame.
if rule.direction.is_relative() {
return Err(Error::RelativeDirection {
direction: rule.direction,
what: "rewrite rule",
});
}
if !rule.action.is_valid_for(rule.class) {
return Err(Error::RewriteActionClass {
action: rule.action,
class: rule.class,
});
}
// The box refuses a rule its read-back reply can't carry. The reply's header is two bytes wider
// than the command's, so a rule can fit its frame and still be refused.
const ENTRY_HDR: usize = 11;
let room = MAX_PAYLOAD - ENTRY_HDR - 2 * rule.match_bytes.len();
if rule.payload.len() > room {
return Err(Error::RewritePayloadTooLarge {
action: rule.action,
class: rule.class,
len: rule.payload.len(),
offset: rule.offset as usize,
cap: room,
});
}
// Mirrors the box's head caps (rewrite_tab.h): 64 bytes on a report surface, 8+2048 on control.
const HEAD_REPORT: usize = 64;
const HEAD_CONTROL: usize = 8 + 2048;
let plen = rule.payload.len();
let off = rule.offset as usize;
let report_cap = if rule.class == RewriteClass::Control {
HEAD_CONTROL
} else {
HEAD_REPORT
};
let (over, cap) = match rule.action {
RewriteAction::Patch | RewriteAction::ReplyPatch => (off + plen > report_cap, report_cap),
RewriteAction::Replace => (plen > report_cap, report_cap),
RewriteAction::Answer | RewriteAction::ReplyReplace => (plen > HEAD_CONTROL, HEAD_CONTROL),
_ => (false, 0),
};
if over {
return Err(Error::RewritePayloadTooLarge {
action: rule.action,
class: rule.class,
len: plen,
offset: off,
cap,
});
}
Ok(())
}