-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcounters.rs
More file actions
476 lines (447 loc) · 15.7 KB
/
counters.rs
File metadata and controls
476 lines (447 loc) · 15.7 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
// 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 2026 Oxide Computer Company
/// This module contains the support for reading the indirect counters defined
/// by the p4 program. While direct counters are attached to an existing table,
/// indirect counters are implemented in the ASIC as standalone tables. They
/// differ from regular match-action tables in that they are accessed with an
/// index number rather than a match-key, and they have no actions associated
/// with them.
///
use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::convert::TryInto;
use std::sync::Arc;
use std::sync::Mutex;
use crate::Switch;
use crate::table;
use crate::types::{DpdError, DpdResult};
use aal::MatchParse;
use aal_macros::*;
use asic::Handle;
use anyhow::Context;
use dpd_types::views;
// Counters in an indirect table are accessed by their index number rather than
// a key. Still, we define a key anyway to allow us to use the direct counter
// infrastructure to extract the counter data.
#[derive(MatchParse, Hash, Debug)]
struct IndexKey {
#[match_xlate(name = "$COUNTER_INDEX", type = "value")]
idx: u16,
}
/// Represents an indirect counter in a P4 program.
pub struct Counter {
/// The CounterId assigned to this counter at build time.
id: CounterId,
/// The asic-layer Table structure used to identify the indirect counter in
/// the program.
table: table::Table,
}
/// sidecar.p4 defines the following set of indirect counters.
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(u8)]
enum CounterId {
Service,
Ingress,
Egress,
Packet,
DropPort,
DropReason,
EgressDropPort,
EgressDropReason,
Unicast,
Multicast,
MulticastExt,
MulticastLL,
MulticastUL,
MulticastDrop,
}
impl From<CounterId> for u8 {
fn from(c: CounterId) -> u8 {
c as u8
}
}
/// Each indirect counter is identified by different names at different places
/// in the code flow. This structure is used to define all the different names
/// used by each table in a single place.
struct CounterDescription {
// Each counter is assigned an ID, which lets us associate the static
// definitions with the Counter state maintained in the switch
// structure.
id: CounterId,
// The name by which clients will identify a table
client_name: &'static str,
// The name assigned to the table in the compiled p4 program
p4_name: &'static str,
}
const COUNTERS: [CounterDescription; 14] = [
CounterDescription {
id: CounterId::Service,
client_name: "Service",
p4_name: "pipe.Ingress.services.service_ctr",
},
CounterDescription {
id: CounterId::Ingress,
client_name: "Ingress",
p4_name: "pipe.Ingress.ingress_ctr",
},
CounterDescription {
id: CounterId::Packet,
client_name: "Packet",
p4_name: "pipe.Ingress.packet_ctr",
},
CounterDescription {
id: CounterId::Egress,
client_name: "Egress",
p4_name: "pipe.Ingress.egress_ctr",
},
CounterDescription {
id: CounterId::DropPort,
client_name: "Ingress_Drop_Port",
p4_name: "pipe.Ingress.drop_port_ctr",
},
CounterDescription {
id: CounterId::DropReason,
client_name: "Ingress_Drop_Reason",
p4_name: "pipe.Ingress.drop_reason_ctr",
},
CounterDescription {
id: CounterId::EgressDropPort,
client_name: "Egress_Drop_Port",
p4_name: "pipe.Egress.drop_port_ctr",
},
CounterDescription {
id: CounterId::EgressDropReason,
client_name: "Egress_Drop_Reason",
p4_name: "pipe.Egress.drop_reason_ctr",
},
CounterDescription {
id: CounterId::Unicast,
client_name: "Unicast",
p4_name: "pipe.Egress.unicast_ctr",
},
CounterDescription {
id: CounterId::Multicast,
client_name: "Multicast",
p4_name: "pipe.Egress.mcast_ctr",
},
CounterDescription {
id: CounterId::MulticastExt,
client_name: "Multicast_External",
p4_name: "pipe.Egress.external_mcast_ctr",
},
CounterDescription {
id: CounterId::MulticastLL,
client_name: "Multicast_Link_Local",
p4_name: "pipe.Egress.link_local_mcast_ctr",
},
CounterDescription {
id: CounterId::MulticastUL,
client_name: "Multicast_Underlay",
p4_name: "pipe.Egress.underlay_mcast_ctr",
},
CounterDescription {
id: CounterId::MulticastDrop,
client_name: "Multicast_Drop",
p4_name: "pipe.Ingress.filter.drop_mcast_ctr",
},
];
/// Get the list of names by which end users can refer to a counter.
pub fn get_counter_names() -> DpdResult<Vec<String>> {
Ok(COUNTERS.iter().map(|c| c.client_name.to_string()).collect())
}
/// Fetch a counter by name from the switch's list of counters. This call
/// returns a pointer to the mutex that controls access to the counter, and the
/// caller is responsible for locking the mutex.
fn get_counter<'a>(
switch: &'a Switch,
name: &str,
) -> DpdResult<&'a Mutex<Counter>> {
let name = name.to_lowercase();
switch
.counters
.get(&name)
.ok_or(DpdError::Invalid("no such counter".to_string()))
}
/// Given an index into the Packet table, convert it from a bitmap of parsed
/// headers into an ASCII label.
fn packet_label(ctr: u16) -> Option<String> {
const PKT_ETHER: u16 = 0x200;
const PKT_LLDP: u16 = 0x100;
const PKT_VLAN: u16 = 0x080;
const PKT_SIDECAR: u16 = 0x040;
const PKT_ICMP: u16 = 0x020;
const PKT_IPV4: u16 = 0x010;
const PKT_IPV6: u16 = 0x008;
const PKT_UDP: u16 = 0x004;
const PKT_TCP: u16 = 0x002;
const PKT_ARP: u16 = 0x001;
fn has_bit(ctr: u16, bit: u16) -> bool {
ctr & bit == bit
}
let mut f = String::new();
if has_bit(ctr, PKT_ETHER) {
f.push('E');
} else {
f.push('-');
}
if has_bit(ctr, PKT_LLDP) {
f.push('L');
} else {
f.push('-');
}
if has_bit(ctr, PKT_VLAN) {
f.push('V');
} else {
f.push('-');
}
if has_bit(ctr, PKT_SIDECAR) {
f.push('S');
} else {
f.push('-');
}
if has_bit(ctr, PKT_ARP) {
f.push('A');
} else if has_bit(ctr, PKT_IPV4) {
f.push('4');
} else if has_bit(ctr, PKT_IPV6) {
f.push('6');
} else {
f.push('-');
}
if has_bit(ctr, PKT_UDP) {
f.push('U');
} else if has_bit(ctr, PKT_TCP) {
f.push('T');
} else if has_bit(ctr, PKT_ICMP) {
f.push('I');
} else {
f.push('-');
}
Some(f)
}
// Given an index into the Service table, return the name of the service action
// it represents.
fn service_label(ctr: u8) -> Option<String> {
let label = match ctr {
0 => "fw_to_user".to_string(),
1 => "fw_from_user".to_string(),
2 => "ping_v4_reply".to_string(),
3 => "ping_v6_reply".to_string(),
4 => "bad_ping".to_string(),
5 => "inbound_link_local".to_string(),
6 => "pass".to_string(),
x => format!("unknown service counter {x}"),
};
Some(label)
}
// The hardcoded ID of the drop reason, matching those defined in constants.p4
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(u8)]
enum DropReason {
Accepted,
Ipv4SwitchAddrMiss,
Ipv6SwitchAddrMiss,
BadPing,
NatHeaderError,
ArpNull,
ArpMiss,
NdpNull,
NdpMiss,
MulticastToLocalInterface,
Ipv4ChecksumErr,
Ipv4TtlInvalid,
Ipv4TtlExceeded,
Ipv6TtlInvalid,
Ipv6TtlExceeded,
Ipv4Unrouteable,
Ipv6Unrouteable,
NatIngressMiss,
MulticastNoGroup,
MulticastInvalidMac,
MulticastCpuCopy,
MulticastSrcFiltered,
MulticastPathFiltered,
GeneveOptionsTooLong,
GeneveOptionMalformed,
GeneveOptionUnknown,
VlanMismatch,
}
impl TryFrom<u8> for DropReason {
type Error = String;
fn try_from(x: u8) -> Result<Self, Self::Error> {
match x {
0 => Ok(DropReason::Accepted),
1 => Ok(DropReason::Ipv4SwitchAddrMiss),
2 => Ok(DropReason::Ipv6SwitchAddrMiss),
3 => Ok(DropReason::BadPing),
4 => Ok(DropReason::NatHeaderError),
5 => Ok(DropReason::ArpNull),
6 => Ok(DropReason::ArpMiss),
7 => Ok(DropReason::NdpNull),
8 => Ok(DropReason::NdpMiss),
9 => Ok(DropReason::MulticastToLocalInterface),
10 => Ok(DropReason::Ipv4ChecksumErr),
11 => Ok(DropReason::Ipv4TtlInvalid),
12 => Ok(DropReason::Ipv4TtlExceeded),
13 => Ok(DropReason::Ipv6TtlInvalid),
14 => Ok(DropReason::Ipv6TtlExceeded),
15 => Ok(DropReason::Ipv4Unrouteable),
16 => Ok(DropReason::Ipv6Unrouteable),
17 => Ok(DropReason::NatIngressMiss),
18 => Ok(DropReason::MulticastNoGroup),
19 => Ok(DropReason::MulticastInvalidMac),
20 => Ok(DropReason::MulticastCpuCopy),
21 => Ok(DropReason::MulticastSrcFiltered),
22 => Ok(DropReason::MulticastPathFiltered),
23 => Ok(DropReason::GeneveOptionsTooLong),
24 => Ok(DropReason::GeneveOptionMalformed),
25 => Ok(DropReason::GeneveOptionUnknown),
26 => Ok(DropReason::VlanMismatch),
x => Err(format!("Unrecognized drop reason: {x}")),
}
}
}
fn reason_label(ctr: u8) -> Result<Option<String>, String> {
let ctr = DropReason::try_from(ctr)?;
let label = match ctr {
// A 'drop reason' of 0 means that the packet wasn't dropped
DropReason::Accepted => return Ok(None),
DropReason::Ipv4SwitchAddrMiss => "ipv4_switch_addr_miss".to_string(),
DropReason::Ipv6SwitchAddrMiss => "ipv6_switch_addr_miss".to_string(),
DropReason::BadPing => "bad_ping".to_string(),
DropReason::NatHeaderError => "nat_header_error".to_string(),
DropReason::ArpNull => "arp_mapping_null".to_string(),
DropReason::ArpMiss => "arp_miss".to_string(),
DropReason::NdpNull => "ndp_mapping_null".to_string(),
DropReason::NdpMiss => "ndp_miss".to_string(),
DropReason::MulticastToLocalInterface => {
"multicast_to_local_interface".to_string()
}
DropReason::Ipv4ChecksumErr => "ipv4_checksum_err".to_string(),
DropReason::Ipv4TtlInvalid => "ipv4_ttl_invalid".to_string(),
DropReason::Ipv4TtlExceeded => "ipv4_ttl_exceeded".to_string(),
DropReason::Ipv6TtlInvalid => "ipv6_ttl_invalid".to_string(),
DropReason::Ipv6TtlExceeded => "ipv6_ttl_exceeded".to_string(),
DropReason::Ipv4Unrouteable => "ipv4_unrouteable".to_string(),
DropReason::Ipv6Unrouteable => "ipv6_unrouteable".to_string(),
DropReason::NatIngressMiss => "nat_ingress_miss".to_string(),
DropReason::MulticastNoGroup => "multicast_no_group".to_string(),
DropReason::MulticastInvalidMac => "multicast_invalid_mac".to_string(),
DropReason::MulticastCpuCopy => "multicast_cpu_copy".to_string(),
DropReason::MulticastSrcFiltered => {
"multicast_src_filtered".to_string()
}
DropReason::MulticastPathFiltered => {
"multicast_path_filtered".to_string()
}
DropReason::GeneveOptionsTooLong => {
"geneve_options_too_long".to_string()
}
DropReason::GeneveOptionMalformed => {
"geneve_option_malformed".to_string()
}
DropReason::GeneveOptionUnknown => "geneve_option_unknown".to_string(),
DropReason::VlanMismatch => "vlan_mismatch".to_string(),
};
Ok(Some(label))
}
// The per-port counters include all possible ASIC ports, even though only a few
// dozen are associated with configured links at any given time. If this ID
// doesn't match to a port, it's not an error.
async fn port_label(switch: &Switch, ctr: u16) -> Option<String> {
switch
.asic_port_id_to_port_link(ctr)
.map(|(port, link)| format!("{port}/{link}"))
.ok()
}
/// Fetch all of the accumulated values for this counter.
pub async fn get_values(
switch: &Arc<Switch>,
force_sync: bool,
counter_name: String,
) -> DpdResult<Vec<views::TableCounterEntry>> {
let counter_id = {
// While we are grabbing the CounterId here, the primary purpose of this
// little dance is to verify that the counter exists before referencing it
// in the closure below. We can't just pass the counter into the closure,
// because the compiler is worried that the Counter lifetime will exceed
// that of the Switch that contains it.
get_counter(switch, &counter_name)?.lock().unwrap().id
};
let counters = {
let switch = switch.clone();
let name = counter_name.clone();
// Because this call may initiate an ASIC->memory sync operation it can
// be long-running, so we run it in a new task.
tokio::task::spawn_blocking(move || {
get_counter(&switch, &name)
.expect("verified that the table exists in the outer function")
.lock()
.unwrap()
.table
.get_counters::<IndexKey>(&switch.asic_hdl, force_sync)
})
.await
.unwrap()
}?;
let mut entries = Vec::new();
for (idx, data) in counters {
// Given the indirect counter index, look up the label.
let key = match counter_id {
CounterId::Packet => packet_label(idx.idx),
CounterId::Service => service_label(idx.idx as u8),
CounterId::Ingress
| CounterId::Egress
| CounterId::EgressDropPort
| CounterId::DropPort
| CounterId::Unicast
| CounterId::Multicast
| CounterId::MulticastExt
| CounterId::MulticastLL
| CounterId::MulticastUL
| CounterId::MulticastDrop => port_label(switch, idx.idx).await,
CounterId::DropReason | CounterId::EgressDropReason => {
reason_label(idx.idx as u8)?
}
};
if let Some(key) = key {
// The packet counter has 256 possible combinations, most of which
// we will never see, many of which (e.g. an icmp packet that is
// neither IPv4 nor IPv6) are even possible. We don't record
// combinations with 0 hits.
if counter_id == CounterId::Packet && data.pkts == Some(0) {
continue;
}
let mut keys = BTreeMap::new();
// In an indirect counter table, the only "key" is the index in the
// table. We've already translated the index into a label, so
// that's what we're calling the key now.
keys.insert("label".to_string(), key);
entries.push(views::TableCounterEntry { keys, data });
}
}
Ok(entries)
}
pub fn reset(switch: &Switch, counter_name: String) -> DpdResult<()> {
let mut counter = get_counter(switch, &counter_name)?.lock().unwrap();
counter.table.clear(&switch.asic_hdl)
}
/// Create internal structures for managing the counters built into sidecar.p4
pub fn init(hdl: &Handle) -> anyhow::Result<BTreeMap<String, Mutex<Counter>>> {
let mut counters = BTreeMap::new();
for c in COUNTERS {
counters.insert(
c.client_name.to_string().to_lowercase(),
Mutex::new(Counter {
id: c.id,
table: table::Table::new(hdl, c.p4_name).with_context(
|| format!("creating {} counter", c.client_name),
)?,
}),
);
}
Ok(counters)
}