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, AddressMatcherType, AgnosticAddressMatcher, BoundInterfaceMatcher,
78    InterfaceMatcher, InterfaceProperties, IpSocketMatcher, IpSocketProperties, MarkMatcher,
79    MarkMatchers, Matcher, MatcherBindingsTypes, MaybeSocketTransportProperties, PortMatcher,
80    SocketCookieMatcher, SocketTransportProtocolMatcher, SubnetMatcher, TcpSocketMatcher,
81    TcpSocketProperties, TcpSocketState, TcpStateMatcher, UdpSocketMatcher, UdpSocketProperties,
82    UdpSocketState, UdpStateMatcher,
83};
84pub use num::PositiveIsize;
85pub use port_alloc::{EphemeralPort, PortAllocImpl, simple_randomized_port_alloc};
86pub use resource_references::{
87    DeferredResourceRemovalContext, ReferenceNotifiers, ReferenceNotifiersExt,
88    RemoveResourceResult, RemoveResourceResultWithContext,
89};
90pub use rng::RngContext;
91pub use settings::{BufferSizeSettings, SettingsContext};
92pub use tcp::base::{Control, EffectiveMss, FragmentedPayload, Mss};
93pub use tcp::segment::{
94    HandshakeOptions, Options, Payload, PayloadLen, ResetOptions, SackBlock, SackBlocks, Segment,
95    SegmentHeader, SegmentOptions, VerifiedTcpSegment,
96};
97pub use tcp::seqnum::{SeqNum, UnscaledWindowSize, WindowScale, WindowSize};
98pub use tcp::timestamp::{
99    Milliseconds, RxTimestampOption, Timestamp, TimestampOption, TxTimestampOption, Unitless,
100};
101pub use test_only::TestOnlyPartialEq;
102pub use time::local_timer_heap::LocalTimerHeap;
103pub use time::{
104    AtomicInstant, CoreTimerContext, HandleableTimer, Instant, InstantBindingsTypes,
105    InstantContext, IntoCoreTimerCtx, NestedIntoCoreTimerCtx, TimerBindingsTypes, TimerContext,
106    TimerHandler,
107};
108pub use uninstantiable::{Uninstantiable, UninstantiableWrapper};
109pub use work_queue::WorkQueueReport;
110
111/// Reference counted hash map data structure.
112pub mod ref_counted_hash_map {
113    pub use crate::data_structures::ref_counted_hash_map::{
114        InsertResult, RefCountedHashMap, RefCountedHashSet, RemoveResult,
115    };
116}
117
118/// Read-copy-update data structure.
119pub mod rcu {
120    pub use crate::data_structures::rcu::{ReadGuard, SynchronizedWriterRcu, WriteGuard};
121}
122
123/// Common types and utilities for sockets.
124pub mod socket {
125    mod address;
126    mod base;
127    mod cookie;
128    pub(crate) mod sndbuf;
129
130    pub use address::{
131        AddrIsMappedError, AddrVecIter, ConnAddr, ConnInfoAddr, ConnIpAddr, DualStackConnIpAddr,
132        DualStackListenerIpAddr, DualStackLocalIp, DualStackRemoteIp, ListenerAddr, ListenerIpAddr,
133        SocketIpAddr, StrictlyZonedAddr,
134    };
135    pub use base::{
136        AddrEntry, AddrVec, Bound, BoundSocketMap, DualStackIpExt, DualStackTuple, EitherStack,
137        FoundSockets, IncompatibleError, InsertError, Inserter, Listener, ListenerAddrInfo,
138        MaybeDualStack, NotDualStackCapableError, RemoveResult, SetDualStackEnabledError, Shutdown,
139        ShutdownType, SocketAddrType, SocketDeviceUpdate, SocketDeviceUpdateNotAllowedError,
140        SocketIpAddrExt, SocketIpExt, SocketMapAddrSpec, SocketMapAddrStateSpec,
141        SocketMapAddrStateUpdateSharingSpec, SocketMapConflictPolicy, SocketMapStateSpec,
142        SocketMapUpdateSharingPolicy, SocketStateEntry, SocketZonedAddrExt, UpdateSharingError,
143    };
144    pub use cookie::SocketCookie;
145    pub use sndbuf::{
146        SendBufferFullError, SendBufferSpace, SendBufferTracking, SocketWritableListener,
147    };
148}
149
150/// Defines generic data structures used to implement common application socket
151/// functionality for multiple protocols.
152pub mod socketmap {
153    pub use crate::data_structures::socketmap::{
154        Entry, IterShadows, OccupiedEntry, SocketMap, Tagged, VacantEntry,
155    };
156}
157
158/// Sync utilities common to netstack3.
159pub mod sync {
160    // TODO(https://fxbug.dev/42062225): Support single-threaded variants of
161    // types exported from this module.
162
163    // Exclusively re-exports from the sync crate.
164    pub use netstack3_sync::rc::{
165        DebugReferences, DynDebugReferences, MapNotifier as MapRcNotifier, Notifier as RcNotifier,
166        Primary as PrimaryRc, ResourceToken, ResourceTokenValue, Strong as StrongRc,
167        Weak as WeakRc,
168    };
169    pub use netstack3_sync::{LockGuard, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
170}
171
172/// Test utilities provided to all crates.
173#[cfg(any(test, feature = "testutils"))]
174pub mod testutil {
175    mod addr;
176    mod benchmarks;
177    mod fake_bindings;
178    mod fake_core;
179    mod fake_network;
180    mod misc;
181    mod monotonic_id;
182
183    pub use crate::device::address::testutil::FakeWeakAddressId;
184    pub use crate::device::link::testutil::{FakeLinkAddress, FakeLinkDevice, FakeLinkDeviceId};
185    pub use crate::device::testutil::{
186        FakeDeviceClass, FakeDeviceId, FakeReferencyDeviceId, FakeStrongDeviceId, FakeWeakDeviceId,
187        MultipleDevicesId, MultipleDevicesIdState,
188    };
189    pub use crate::event::testutil::FakeEventCtx;
190    pub use crate::frame::testutil::{FakeFrameCtx, FakeTxMetadata, WithFakeFrameContext};
191    pub use crate::matchers::testutil::FakeMatcherDeviceId;
192    pub use crate::rng::testutil::{FakeCryptoRng, new_rng, run_with_many_seeds};
193    pub use crate::settings::testutil::AlwaysDefaultsSettingsContext;
194    pub use crate::socket::sndbuf::testutil::FakeSocketWritableListener;
195    pub use crate::time::testutil::{
196        FakeAtomicInstant, FakeInstant, FakeInstantCtx, FakeTimerCtx, FakeTimerCtxExt, FakeTimerId,
197        InstantAndData, WithFakeTimerContext,
198    };
199    pub use addr::{TEST_ADDRS_V4, TEST_ADDRS_V6, TestAddrs, TestDualStackIpExt, TestIpExt};
200    pub use benchmarks::{Bencher, RealBencher, TestBencher};
201    pub use fake_bindings::FakeBindingsCtx;
202    pub use fake_core::FakeCoreCtx;
203    pub use fake_network::{
204        FakeNetwork, FakeNetworkLinks, FakeNetworkSpec, PendingFrame, PendingFrameData, StepResult,
205    };
206    pub use misc::{assert_empty, set_logger_for_test};
207    pub use monotonic_id::MonotonicIdentifier;
208}
209
210/// Benchmarks defined in the base crate.
211#[cfg(benchmark)]
212pub mod benchmarks {
213    /// Adds benchmarks defined in the base crate to the provided benchmarker.
214    pub fn add_benches(b: criterion::Benchmark) -> criterion::Benchmark {
215        crate::data_structures::token_bucket::benchmarks::add_benches(b)
216    }
217}