-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtable.rs
More file actions
652 lines (620 loc) · 25.8 KB
/
table.rs
File metadata and controls
652 lines (620 loc) · 25.8 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/
//
// Copyright 2025 Oxide Computer Company
use slog::{error, trace};
use softnpu_lib::{ManagementRequest, TableAdd, TableRemove};
use crate::softnpu::Handle;
use aal::{
ActionParse, AsicError, AsicResult, CounterData, MatchEntryField,
MatchEntryValue, MatchParse, TableOps, ValueTypes,
};
/// Represents a handle to a SoftNPU ASIC table. The `id` member corresponds to
/// the table path in the P4 program. Well known sidecar-lite.p4 paths follow
/// below.
pub struct Table {
id: Option<String>,
dpd_id: Option<String>,
size: usize,
}
// soft-npu table names
const ROUTER_V4_RT: &str = "ingress.router.v4_route.rtr";
const ROUTER_V4_IDX: &str = "ingress.router.v4_idx.rtr";
const ROUTER_V6_RT: &str = "ingress.router.v6_route.rtr";
const ROUTER_V6_IDX: &str = "ingress.router.v6_idx.rtr";
const LOCAL_V6: &str = "ingress.local.local_v6";
const LOCAL_V4: &str = "ingress.local.local_v4";
const NAT_V4: &str = "ingress.nat.nat_v4";
const NAT_V6: &str = "ingress.nat.nat_v6";
const _NAT_ICMP_V6: &str = "ingress.nat.nat_icmp_v6";
const _NAT_ICMP_V4: &str = "ingress.nat.nat_icmp_v4";
const RESOLVER_V4: &str = "ingress.resolver.resolver_v4";
const RESOLVER_V6: &str = "ingress.resolver.resolver_v6";
const MAC_REWRITE: &str = "ingress.mac.mac_rewrite";
const _PROXY_ARP: &str = "ingress.pxarp.proxy_arp";
// sidecar.p4 table names
const SWITCH_ADDR4: &str = "pipe.Ingress.filter.switch_ipv4_addr";
const SWITCH_ADDR6: &str = "pipe.Ingress.filter.switch_ipv6_addr";
const ROUTER4_LOOKUP_RT: &str =
"pipe.Ingress.l3_router.Router4.lookup_idx.route";
const ROUTER4_LOOKUP_IDX: &str =
"pipe.Ingress.l3_router.Router4.lookup_idx.lookup";
const ROUTER6_LOOKUP_RT: &str =
"pipe.Ingress.l3_router.Router6.lookup_idx.route";
const ROUTER6_LOOKUP_IDX: &str =
"pipe.Ingress.l3_router.Router6.lookup_idx.lookup";
const NDP: &str = "pipe.Ingress.l3_router.Router6.Ndp.tbl";
const ARP: &str = "pipe.Ingress.l3_router.Router4.Arp.tbl";
const DPD_MAC_REWRITE: &str = "pipe.Ingress.mac_rewrite.mac_rewrite";
const NAT_INGRESS4: &str = "pipe.Ingress.nat_ingress.ingress_ipv4";
const NAT_INGRESS6: &str = "pipe.Ingress.nat_ingress.ingress_ipv6";
// All tables are defined to be 1024 entries deep
const TABLE_SIZE: usize = 4096;
impl TableOps<Handle> for Table {
fn new(_hdl: &Handle, name: &str) -> AsicResult<Table> {
// TODO just mapping sidecar.p4 things onto simplified sidecar-lite.p4
// things to get started.
let (id, dpd_id) = match name {
ROUTER4_LOOKUP_RT => {
(Some(ROUTER_V4_RT.into()), Some(ROUTER4_LOOKUP_RT.into()))
}
ROUTER4_LOOKUP_IDX => {
(Some(ROUTER_V4_IDX.into()), Some(ROUTER4_LOOKUP_IDX.into()))
}
ROUTER6_LOOKUP_RT => {
(Some(ROUTER_V6_RT.into()), Some(ROUTER6_LOOKUP_RT.into()))
}
ROUTER6_LOOKUP_IDX => {
(Some(ROUTER_V6_IDX.into()), Some(ROUTER6_LOOKUP_IDX.into()))
}
SWITCH_ADDR4 => (Some(LOCAL_V4.into()), Some(SWITCH_ADDR4.into())),
SWITCH_ADDR6 => (Some(LOCAL_V6.into()), Some(SWITCH_ADDR6.into())),
NDP => (Some(RESOLVER_V6.into()), Some(NDP.into())),
ARP => (Some(RESOLVER_V4.into()), Some(ARP.into())),
DPD_MAC_REWRITE => {
(Some(MAC_REWRITE.into()), Some(DPD_MAC_REWRITE.into()))
}
NAT_INGRESS4 => (Some(NAT_V4.into()), Some(NAT_INGRESS4.into())),
NAT_INGRESS6 => (Some(NAT_V6.into()), Some(NAT_INGRESS6.into())),
x => {
println!("TABLE NOT HANDLED {x}");
(None, None)
}
};
Ok(Table {
id,
dpd_id,
size: TABLE_SIZE,
})
}
fn size(&self) -> usize {
self.size
}
fn clear(&self, _hdl: &Handle) -> AsicResult<()> {
//TODO implement in softnpu
Ok(())
}
fn entry_add<M: MatchParse, A: ActionParse>(
&self,
hdl: &Handle,
key: &M,
data: &A,
) -> AsicResult<()> {
let table = match &self.id {
None => return Ok(()),
Some(id) => id.clone(),
};
let dpd_table = match &self.dpd_id {
None => return Ok(()),
Some(id) => id.clone(),
};
let match_data = key.key_to_ir().unwrap();
let action_data = data.action_to_ir().unwrap();
trace!(hdl.log, "entry_add called");
trace!(hdl.log, "table: {}", table);
trace!(hdl.log, "match_data:\n{:#?}", match_data);
trace!(hdl.log, "action_data:\n{:#?}", action_data);
let keyset_data = keyset_data(match_data.fields, &table);
let (action, parameter_data) = match (
dpd_table.as_str(),
action_data.action.as_str(),
) {
// TODO: implement mappings for natv6 actions
(SWITCH_ADDR4, "claimv4") => ("local", Vec::new()),
(SWITCH_ADDR6, "claimv6") => ("local", Vec::new()),
(ROUTER4_LOOKUP_IDX, "index") => {
let mut params = Vec::new();
for arg in action_data.args.iter() {
match &arg.value {
ValueTypes::U64(v) => {
// 16 bit index
// 8 bit slot count
match arg.name.as_str() {
"idx" => {
let v = *v as u16;
params.extend_from_slice(&v.to_le_bytes());
}
"slots" => {
let v = *v as u8;
params.extend_from_slice(&v.to_le_bytes());
}
x => {
error!(hdl.log, "unexpected parameter: {dpd_table}::index {x}")
}
}
}
ValueTypes::Ptr(v) => {
params.extend_from_slice(v.as_slice());
}
}
}
("index", params)
}
(ROUTER4_LOOKUP_RT, "forward") => {
let mut params = Vec::new();
for arg in action_data.args.iter() {
match &arg.value {
ValueTypes::U64(v) => {
// 16 bit port
// 32 bit nexthop
match arg.name.as_str() {
"port" => {
params.extend_from_slice(
&(*v as u16).to_le_bytes(),
);
}
"nexthop" => {
params.extend_from_slice(
&(*v as u32).to_le_bytes(),
);
}
x => {
error!(hdl.log, "unexpected parameter: {dpd_table}::forward {x}")
}
}
}
ValueTypes::Ptr(v) => {
params.extend_from_slice(v.as_slice());
}
}
}
("forward", params)
}
(ROUTER4_LOOKUP_RT, "forward_vlan") => {
let mut params = Vec::new();
for arg in action_data.args.iter() {
match &arg.value {
ValueTypes::U64(v) => {
// 16 bit port
// 32 bit nexthop
// 12 bit vlan
match arg.name.as_str() {
"vlan_id" => {
params.extend_from_slice(
&(*v as u16).to_le_bytes(),
);
}
"port" => {
params.extend_from_slice(
&(*v as u16).to_le_bytes(),
);
}
"nexthop" => {
params.extend_from_slice(
&(*v as u32).to_le_bytes(),
);
}
x => {
error!(hdl.log, "unexpected parameter: {dpd_table}::forward_vlan {x}")
}
}
}
ValueTypes::Ptr(v) => {
params.extend_from_slice(v.as_slice());
}
}
}
("forward_vlan", params)
}
(ROUTER6_LOOKUP_IDX, "index") => {
let mut params = Vec::new();
for arg in action_data.args.iter() {
match &arg.value {
ValueTypes::U64(v) => {
// 16 bit index
// 8 bit slot count
match arg.name.as_str() {
"idx" => {
let v = *v as u16;
params.extend_from_slice(&v.to_le_bytes());
}
"slots" => {
let v = *v as u8;
params.extend_from_slice(&v.to_le_bytes());
}
x => {
error!(hdl.log, "unexpected parameter: {dpd_table}::index {x}")
}
}
}
ValueTypes::Ptr(v) => {
params.extend_from_slice(v.as_slice());
}
}
}
("index", params)
}
(ROUTER6_LOOKUP_RT, "forward") => {
let mut params = Vec::new();
for arg in action_data.args.iter() {
match &arg.value {
ValueTypes::U64(v) => {
// 16 bit port
match arg.name.as_str() {
"port" => {
params.extend_from_slice(
&(*v as u16).to_le_bytes(),
);
}
x => {
error!(hdl.log, "unexpected parameter: {dpd_table}::forward {x}")
}
}
}
ValueTypes::Ptr(v) => {
let mut buf = v.clone();
buf.reverse();
params.extend_from_slice(buf.as_slice());
}
}
}
("forward", params)
}
(ROUTER6_LOOKUP_RT, "forward_vlan") => {
let mut params = Vec::new();
for arg in action_data.args.iter() {
match &arg.value {
ValueTypes::U64(v) => {
// 16 bit port
// 12 bit vlan
match arg.name.as_str() {
"vlan_id" => {
params.extend_from_slice(
&(*v as u16).to_le_bytes(),
);
}
"port" => {
params.extend_from_slice(
&(*v as u16).to_le_bytes(),
);
}
x => {
error!(hdl.log, "unexpected parameter: {dpd_table}::forward_vlan {x}")
}
}
}
ValueTypes::Ptr(v) => {
let mut buf = v.clone();
buf.reverse();
params.extend_from_slice(buf.as_slice());
}
}
}
("forward_vlan", params)
}
(ARP, "rewrite") => {
let mut params = Vec::new();
for arg in action_data.args {
match arg.value {
ValueTypes::U64(v) => {
let mac = v.to_le_bytes();
params.extend_from_slice(&mac[0..6]);
}
ValueTypes::Ptr(v) => {
params.extend_from_slice(v.as_slice());
}
}
}
("rewrite_dst", params)
}
(NDP, "rewrite") => {
let mut params = Vec::new();
for arg in action_data.args {
match arg.value {
ValueTypes::U64(v) => {
let mac = v.to_le_bytes();
params.extend_from_slice(&mac[0..6]);
}
ValueTypes::Ptr(v) => {
params.extend_from_slice(v.as_slice());
}
}
}
("rewrite_dst", params)
}
(DPD_MAC_REWRITE, "rewrite") => {
let mut params = Vec::new();
for arg in action_data.args {
match arg.value {
ValueTypes::U64(v) => {
let mac = v.to_le_bytes();
params.extend_from_slice(&mac[0..6]);
}
ValueTypes::Ptr(v) => {
params.extend_from_slice(v.as_slice());
}
}
}
("rewrite", params)
}
(NAT_INGRESS4, "forward_ipv4_to") => {
let mut target = Vec::new();
let mut vni = Vec::new();
let mut mac = Vec::new();
for arg in action_data.args {
match arg.name.as_str() {
"target" => {
// "target" is 128 bits
let mut data: Vec<u8> = Vec::new();
match &arg.value {
ValueTypes::U64(_) => {
// Currently the ValueType is always Ptr
error!(
hdl.log,
"expected ValueType::Ptr, \
received ValueType::U64"
);
return Ok(());
}
ValueTypes::Ptr(v) => {
data.extend_from_slice(v.as_slice());
}
}
let len = data.len();
let buf = &mut data[len - 16..];
buf.reverse();
target.extend_from_slice(buf);
}
"vni" => {
// "vni" is 24 bits
let mut data: Vec<u8> = Vec::new();
match &arg.value {
ValueTypes::U64(v) => {
data.extend_from_slice(&v.to_le_bytes());
}
ValueTypes::Ptr(_) => {
// Currently the ValueType is always U64
error!(
hdl.log,
"expected ValueType::U64, \
received ValueType::Ptr"
);
return Ok(());
}
}
vni.extend_from_slice(&data[0..3]);
}
"inner_mac" => {
// "mac" is 48 bits
let mut data: Vec<u8> = Vec::new();
match &arg.value {
ValueTypes::U64(v) => {
data.extend_from_slice(&v.to_le_bytes());
}
ValueTypes::Ptr(_) => {
// Currently the ValueType is always U64
error!(
hdl.log,
"expected ValueType::U64, \
received ValueType::Ptr"
);
return Ok(());
}
}
mac.extend_from_slice(&data[0..6])
}
_ => {
error!(hdl.log, "unknown argument: {}", arg.name);
return Ok(());
}
}
}
let mut params = Vec::new();
// arguments currently don't arrive in the correct order,
// so we'll order them manually
params.extend_from_slice(target.as_slice());
params.extend_from_slice(vni.as_slice());
params.extend_from_slice(mac.as_slice());
("forward_to_sled", params)
}
(tbl, x) => {
println!("ACTION NOT HANDLED {tbl} {x}");
return Ok(());
}
};
let action = action.to_string();
trace!(hdl.log, "sending request to softnpu");
trace!(hdl.log, "table: {}", table);
trace!(hdl.log, "action: {:#?}", action);
trace!(hdl.log, "keyset_data:\n{:#?}", keyset_data);
trace!(hdl.log, "parameter_data:\n{:#?}", parameter_data);
let msg = ManagementRequest::TableAdd(TableAdd {
table,
action,
keyset_data,
parameter_data,
});
crate::softnpu::mgmt::write(msg, &hdl.mgmt_config);
Ok(())
}
fn entry_update<M: MatchParse, A: ActionParse>(
&self,
hdl: &Handle,
key: &M,
data: &A,
) -> AsicResult<()> {
let table = match &self.id {
None => return Ok(()),
Some(id) => id.clone(),
};
let match_data = key.key_to_ir().unwrap();
let action_data = data.action_to_ir().unwrap();
trace!(hdl.log, "entry_update called");
trace!(hdl.log, "table: {}", table);
trace!(hdl.log, "match_data:\n{:#?}", match_data);
trace!(hdl.log, "action_data:\n{:#?}", action_data);
//TODO implement in softnpu
Ok(())
}
fn entry_del<M: MatchParse>(
&self,
hdl: &Handle,
key: &M,
) -> AsicResult<()> {
let table = match &self.id {
None => return Ok(()),
Some(id) => id.clone(),
};
let match_data = key.key_to_ir().unwrap();
trace!(hdl.log, "entry_del called");
trace!(hdl.log, "table: {}", table);
trace!(hdl.log, "match_data:\n{:#?}", match_data);
let keyset_data = keyset_data(match_data.fields, &table);
trace!(hdl.log, "sending request to softnpu");
trace!(hdl.log, "table: {}", table);
trace!(hdl.log, "keyset_data:\n{:#?}", keyset_data);
let msg =
ManagementRequest::TableRemove(TableRemove { keyset_data, table });
crate::softnpu::mgmt::write(msg, &hdl.mgmt_config);
Ok(())
}
fn get_entries<M: MatchParse, A: ActionParse>(
&self,
_hdl: &Handle,
) -> AsicResult<Vec<(M, A)>> {
Err(aal::AsicError::OperationUnsupported)
}
fn get_counters<M: MatchParse>(
&self,
_hdl: &Handle,
_force_sync: bool,
) -> AsicResult<Vec<(M, CounterData)>> {
Err(AsicError::OperationUnsupported)
}
}
/// Extract keys from `match_data` and ensure that they are
/// in a data structure with the correct length
fn keyset_data(match_data: Vec<MatchEntryField>, table: &str) -> Vec<u8> {
let mut keyset_data: Vec<u8> = Vec::new();
for m in match_data {
match m.value {
// Exact match
MatchEntryValue::Value(x) => {
let mut data: Vec<u8> = Vec::new();
match table {
RESOLVER_V4 => {
// "nexthop_ipv4" => bit<32>
serialize_value_type(&x, &mut data);
keyset_data.extend_from_slice(&data[..4]);
}
RESOLVER_V6 => {
// "nexthop_ipv6" => bit<128>
let mut buf = Vec::new();
serialize_value_type(&x, &mut buf);
buf.reverse();
keyset_data.extend_from_slice(&buf);
}
MAC_REWRITE => {
serialize_value_type(&x, &mut data);
keyset_data.extend_from_slice(&data[..2]);
}
ROUTER_V4_RT => {
// "idx" => exact => bit<16>
serialize_value_type(&x, &mut data);
keyset_data.extend_from_slice(&data[..2]);
}
ROUTER_V6_RT => {
// "idx" => exact => bit<16>
serialize_value_type(&x, &mut data);
keyset_data.extend_from_slice(&data[..2]);
}
NAT_V4 => {
// "dst_addr" => hdr.ipv4.dst: exact => bit<32>
serialize_value_type(&x, &mut data);
keyset_data.extend_from_slice(&data[..4]);
}
LOCAL_V6 => {
let mut buf = Vec::new();
serialize_value_type(&x, &mut buf);
buf.reverse();
keyset_data.extend_from_slice(&buf);
}
_ => {
serialize_value_type(&x, &mut keyset_data);
}
}
}
// Longest prefix
MatchEntryValue::Lpm(x) => {
let mut data: Vec<u8> = Vec::new();
match table {
ROUTER_V4_IDX => {
// prefix for longest prefix match operation
// "dst_addr" => hdr.ipv4.dst: lpm => bit<32>
serialize_value_type_be(&x.prefix, &mut data);
keyset_data.extend_from_slice(&data[data.len() - 4..]);
// prefix length for longest prefix match operation
keyset_data.push(x.len as u8)
}
_ => {
serialize_value_type_be(&x.prefix, &mut keyset_data);
keyset_data.push(x.len as u8);
}
}
}
// Ranges (i.e. port ranges)
MatchEntryValue::Range(x) => {
match table {
NAT_V4 => {
// "l4_dst_port" => ingress.nat_id: range => bit<16>
let low = &x.low.to_le_bytes();
let high = &x.high.to_le_bytes();
keyset_data.extend_from_slice(&low[..2]);
keyset_data.extend_from_slice(&high[..2]);
}
_ => {
keyset_data.extend_from_slice(&x.low.to_le_bytes());
keyset_data.extend_from_slice(&x.high.to_le_bytes());
}
}
}
// Masked (ternary match)
MatchEntryValue::Mask(x) => {
keyset_data.extend_from_slice(&x.val.to_le_bytes());
keyset_data.extend_from_slice(&x.mask.to_le_bytes());
}
}
}
keyset_data
}
fn serialize_value_type(x: &ValueTypes, data: &mut Vec<u8>) {
match x {
ValueTypes::U64(v) => {
data.extend_from_slice(&v.to_le_bytes());
}
ValueTypes::Ptr(v) => {
data.extend_from_slice(v.as_slice());
}
}
}
fn serialize_value_type_be(x: &ValueTypes, data: &mut Vec<u8>) {
match x {
ValueTypes::U64(v) => {
data.extend_from_slice(&v.to_be_bytes());
}
ValueTypes::Ptr(v) => {
data.extend_from_slice(v.as_slice());
}
}
}