netstack3_base/
lib.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//! Base type definitions for netstack3 core.
6//!
7//! This crate contains definitions common to the other netstack3 core crates
8//! and is the base dependency for most of them.
9
10#![no_std]
11#![warn(
12    missing_docs,
13    unreachable_patterns,
14    clippy::useless_conversion,
15    clippy::redundant_clone,
16    clippy::precedence
17)]
18
19extern crate alloc;
20
21mod context;
22mod convert;
23mod counters;
24mod data_structures;
25mod device;
26mod error;
27mod event;
28mod frame;
29mod inspect;
30mod ip;
31mod map_deref;
32mod matchers;
33mod num;
34mod port_alloc;
35mod resource_references;
36mod rng;
37mod settings;
38mod tcp;
39mod test_only;
40mod time;
41mod uninstantiable;
42mod work_queue;
43
44pub use context::{BuildableCoreContext, ContextPair, ContextProvider, CtxPair};
45pub use convert::{BidirectionalConverter, OwnedOrRefsBidirectionalConverter};
46pub use counters::{
47    Counter, CounterCollection, CounterCollectionSpec, CounterContext, CounterRepr,
48    ResourceCounterContext,
49};
50pub use data_structures::token_bucket::TokenBucket;
51pub use device::address::{
52    AssignedAddrIpExt, IpAddressId, IpDeviceAddr, IpDeviceAddressIdContext, Ipv4DeviceAddr,
53    Ipv6DeviceAddr, WeakIpAddressId,
54};
55pub use device::link::{LinkAddress, LinkDevice, LinkUnicastAddress};
56pub use device::{
57    AnyDevice, Device, DeviceIdAnyCompatContext, DeviceIdContext, DeviceIdentifier, EitherDeviceId,
58    StrongDeviceIdentifier, WeakDeviceIdentifier,
59};
60pub use error::{
61    AddressResolutionFailed, ErrorAndSerializer, ExistsError, LocalAddressError, NotFoundError,
62    NotSupportedError, RemoteAddressError, SocketError, ZonedAddressError,
63};
64pub use event::EventContext;
65pub use frame::{
66    CoreTxMetadataContext, FrameDestination, ReceivableFrameMeta, RecvFrameContext,
67    RecvIpFrameMeta, SendFrameContext, SendFrameError, SendFrameErrorReason, SendableFrameMeta,
68    TxMetadata, TxMetadataBindingsTypes,
69};
70pub use inspect::{Inspectable, InspectableValue, Inspector, InspectorDeviceExt, InspectorExt};
71pub use ip::{
72    BroadcastIpExt, IcmpErrorCode, IcmpIpExt, Icmpv4ErrorCode, Icmpv6ErrorCode, IpExt,
73    IpTypesIpExt, Mark, MarkDomain, MarkStorage, Marks, Mms, WrapBroadcastMarker,
74};
75pub use map_deref::{MapDeref, MapDerefExt};
76pub use matchers::{
77    AddressMatcher, AddressMatcherEither, AddressMatcherType, BoundInterfaceMatcher,
78    InterfaceMatcher, InterfaceProperties, IpSocketMatcher, IpSocketProperties,
79    IpSocketPropertiesMatcher, MarkInDomainMatcher, MarkMatcher, MarkMatchers, Matcher,
80    MatcherBindingsTypes, MaybeSocketTransportProperties, PortMatcher, SocketCookieMatcher,
81    SocketTransportProtocolMatcher, SubnetMatcher, TcpSocketMatcher, TcpSocketProperties,
82    TcpSocketState, TcpStateMatcher, UdpSocketMatcher, UdpSocketProperties, UdpSocketState,
83    UdpStateMatcher,
84};
85pub use num::PositiveIsize;
86pub use port_alloc::{EphemeralPort, PortAllocImpl, simple_randomized_port_alloc};
87pub use resource_references::{
88    DeferredResourceRemovalContext, ReferenceNotifiers, ReferenceNotifiersExt,
89    RemoveResourceResult, RemoveResourceResultWithContext,
90};
91pub use rng::RngContext;
92pub use settings::{BufferSizeSettings, SettingsContext};
93pub use tcp::base::{Control, EffectiveMss, FragmentedPayload, Mss, MssSizeLimiters};
94pub use tcp::segment::{
95    HandshakeOptions, Options, Payload, PayloadLen, ResetOptions, SackBlock, SackBlocks, Segment,
96    SegmentHeader, SegmentOptions, VerifiedTcpSegment,
97};
98pub use tcp::seqnum::{SeqNum, UnscaledWindowSize, WindowScale, WindowSize};
99pub use tcp::timestamp::{
100    Milliseconds, RxTimestampOption, Timestamp, TimestampOption, TxTimestampOption, Unitless,
101};
102pub use test_only::TestOnlyPartialEq;
103pub use time::local_timer_heap::LocalTimerHeap;
104pub use time::{
105    AtomicInstant, CoreTimerContext, HandleableTimer, Instant, InstantBindingsTypes,
106    InstantContext, IntoCoreTimerCtx, NestedIntoCoreTimerCtx, TimerBindingsTypes, TimerContext,
107    TimerHandler,
108};
109pub use uninstantiable::{Uninstantiable, UninstantiableWrapper};
110pub use work_queue::WorkQueueReport;
111
112/// Reference counted hash map data structure.
113pub mod ref_counted_hash_map {
114    pub use crate::data_structures::ref_counted_hash_map::{
115        InsertResult, RefCountedHashMap, RefCountedHashSet, RemoveResult,
116    };
117}
118
119/// Read-copy-update data structure.
120pub mod rcu {
121    pub use crate::data_structures::rcu::{ReadGuard, SynchronizedWriterRcu, WriteGuard};
122}
123
124/// Common types and utilities for sockets.
125pub mod socket {
126    mod address;
127    mod base;
128    mod cookie;
129    pub(crate) mod sndbuf;
130
131    pub use address::{
132        AddrIsMappedError, AddrVecIter, ConnAddr, ConnInfoAddr, ConnIpAddr, DualStackConnIpAddr,
133        DualStackListenerIpAddr, DualStackLocalIp, DualStackRemoteIp, ListenerAddr, ListenerIpAddr,
134        SocketIpAddr, StrictlyZonedAddr,
135    };
136    pub use base::{
137        AddrEntry, AddrVec, Bound, BoundSocketMap, DualStackIpExt, DualStackTuple, EitherStack,
138        FoundSockets, IncompatibleError, InsertError, Inserter, Listener, ListenerAddrInfo,
139        MaybeDualStack, NotDualStackCapableError, RemoveResult, ReusePortOption,
140        SetDualStackEnabledError, SharingDomain, Shutdown, ShutdownType, SocketAddrType,
141        SocketDeviceUpdate, SocketDeviceUpdateNotAllowedError, SocketIpAddrExt, SocketIpExt,
142        SocketMapAddrSpec, SocketMapAddrStateSpec, SocketMapAddrStateUpdateSharingSpec,
143        SocketMapConflictPolicy, SocketMapStateSpec, SocketMapUpdateSharingPolicy,
144        SocketStateEntry, SocketZonedAddrExt, UpdateSharingError,
145    };
146    pub use cookie::SocketCookie;
147    pub use sndbuf::{
148        SendBufferFullError, SendBufferSpace, SendBufferTracking, SocketWritableListener,
149    };
150}
151
152/// Defines generic data structures used to implement common application socket
153/// functionality for multiple protocols.
154pub mod socketmap {
155    pub use crate::data_structures::socketmap::{
156        Entry, IterShadows, OccupiedEntry, SocketMap, Tagged, VacantEntry,
157    };
158}
159
160/// Sync utilities common to netstack3.
161pub mod sync {
162    // TODO(https://fxbug.dev/42062225): Support single-threaded variants of
163    // types exported from this module.
164
165    // Exclusively re-exports from the sync crate.
166    pub use netstack3_sync::rc::{
167        DebugReferences, DynDebugReferences, MapNotifier as MapRcNotifier, Notifier as RcNotifier,
168        Primary as PrimaryRc, ResourceToken, ResourceTokenValue, Strong as StrongRc,
169        Weak as WeakRc,
170    };
171    pub use netstack3_sync::{LockGuard, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
172}
173
174/// Test utilities provided to all crates.
175#[cfg(any(test, feature = "testutils"))]
176pub mod testutil {
177    mod addr;
178    mod benchmarks;
179    mod fake_bindings;
180    mod fake_core;
181    mod fake_network;
182    mod misc;
183    mod monotonic_id;
184
185    pub use crate::device::address::testutil::FakeWeakAddressId;
186    pub use crate::device::link::testutil::{FakeLinkAddress, FakeLinkDevice, FakeLinkDeviceId};
187    pub use crate::device::testutil::{
188        FakeDeviceClass, FakeDeviceId, FakeReferencyDeviceId, FakeStrongDeviceId, FakeWeakDeviceId,
189        MultipleDevicesId, MultipleDevicesIdState,
190    };
191    pub use crate::event::testutil::FakeEventCtx;
192    pub use crate::frame::testutil::{FakeFrameCtx, FakeTxMetadata, WithFakeFrameContext};
193    pub use crate::matchers::testutil::FakeMatcherDeviceId;
194    pub use crate::rng::testutil::{FakeCryptoRng, new_rng, run_with_many_seeds};
195    pub use crate::settings::testutil::AlwaysDefaultsSettingsContext;
196    pub use crate::socket::sndbuf::testutil::FakeSocketWritableListener;
197    pub use crate::time::testutil::{
198        FakeAtomicInstant, FakeInstant, FakeInstantCtx, FakeTimerCtx, FakeTimerCtxExt, FakeTimerId,
199        InstantAndData, WithFakeTimerContext,
200    };
201    pub use addr::{TEST_ADDRS_V4, TEST_ADDRS_V6, TestAddrs, TestDualStackIpExt, TestIpExt};
202    pub use benchmarks::{Bencher, RealBencher, TestBencher};
203    pub use fake_bindings::FakeBindingsCtx;
204    pub use fake_core::FakeCoreCtx;
205    pub use fake_network::{
206        FakeNetwork, FakeNetworkLinks, FakeNetworkSpec, PendingFrame, PendingFrameData, StepResult,
207    };
208    pub use misc::{assert_empty, set_logger_for_test};
209    pub use monotonic_id::MonotonicIdentifier;
210}
211
212/// Benchmarks defined in the base crate.
213#[cfg(benchmark)]
214pub mod benchmarks {
215    /// Adds benchmarks defined in the base crate to the provided benchmarker.
216    pub fn add_benches(b: criterion::Benchmark) -> criterion::Benchmark {
217        crate::data_structures::token_bucket::benchmarks::add_benches(b)
218    }
219}