netstack3_ip/raw/
counters.rs

1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Declares counters for observability/debugging of raw IP sockets.
6
7use net_types::ip::{Ip, IpVersionMarker};
8use netstack3_base::{Counter, Inspectable, Inspector, InspectorExt};
9
10/// Raw IP socket counters.
11///
12/// These counters are used both on a per-socket basis, and in a stack-wide
13/// aggregate.
14#[derive(Default)]
15pub struct RawIpSocketCounters<I: Ip> {
16    _marker: IpVersionMarker<I>,
17    /// Count of incoming IP packets that were delivered to the socket.
18    ///
19    /// Note that a single IP packet may be delivered to multiple raw IP
20    /// sockets. Thus this counter, when tracking the stack-wide aggregate, may
21    /// exceed the total number of IP packets received by the stack.
22    pub(super) rx_packets: Counter,
23    /// Count of outgoing IP packets that were sent by the socket.
24    pub(super) tx_packets: Counter,
25    /// Count of incoming IP packets that were not delivered due to an invalid
26    /// checksum.
27    pub(super) rx_checksum_errors: Counter,
28    /// Count of outgoing IP packets that were not sent because a checksum could
29    /// not be computed.
30    pub(super) tx_checksum_errors: Counter,
31    /// Count of incoming IP packets that were not delivered because they failed
32    /// the socket's ICMP filter.
33    pub(super) rx_icmp_filtered: Counter,
34}
35
36impl<I: Ip> Inspectable for RawIpSocketCounters<I> {
37    fn record<II: Inspector>(&self, inspector: &mut II) {
38        let Self {
39            _marker,
40            rx_packets,
41            tx_packets,
42            rx_checksum_errors,
43            tx_checksum_errors,
44            rx_icmp_filtered,
45        } = self;
46        inspector.record_child("Rx", |inspector| {
47            inspector.record_counter("DeliveredPackets", rx_packets);
48            inspector.record_counter("ChecksumErrors", rx_checksum_errors);
49            inspector.record_counter("IcmpPacketsFiltered", rx_icmp_filtered);
50        });
51        inspector.record_child("Tx", |inspector| {
52            inspector.record_counter("SentPackets", tx_packets);
53            inspector.record_counter("ChecksumErrors", tx_checksum_errors);
54        });
55    }
56}