Skip to main content

netstack3_core/
marker.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//! Marker traits with blanket implementations.
6//!
7//! Traits in this module exist to be exported as markers to bindings without
8//! exposing the internal traits directly.
9
10use net_types::ip::{Ipv4, Ipv6};
11use netstack3_base::{
12    AnyDevice, BindingsSyncTypes, DeviceIdContext, InstantBindingsTypes, ReferenceNotifiers,
13    RngContext, TimerBindingsTypes, TxMetadataBindingsTypes,
14};
15use netstack3_datagram as datagram;
16use netstack3_device::ethernet::{
17    EthernetDeviceEventBindingsContext, EthernetDeviceId, EthernetLinkDevice, EthernetWeakDeviceId,
18};
19use netstack3_device::{self as device, DeviceId, DeviceLayerTypes, WeakDeviceId};
20use netstack3_filter::{FilterBindingsContext, FilterBindingsTypes, SocketOpsFilterBindingContext};
21use netstack3_icmp_echo::{IcmpEchoBindingsContext, IcmpEchoBindingsTypes, IcmpEchoStateContext};
22use netstack3_ip::device::{
23    IpDeviceBindingsContext, IpDeviceConfigurationContext, IpDeviceConfigurationHandler,
24    IpDeviceIpExt,
25};
26use netstack3_ip::icmp::{IcmpBindingsContext, IcmpBindingsTypes};
27use netstack3_ip::multicast_forwarding::{
28    MulticastForwardingBindingsContext, MulticastForwardingBindingsTypes,
29    MulticastForwardingStateContext,
30};
31use netstack3_ip::nud::{NudBindingsContext, NudContext};
32use netstack3_ip::raw::{
33    RawIpSocketMapContext, RawIpSocketStateContext, RawIpSocketsBindingsContext,
34    RawIpSocketsBindingsTypes,
35};
36use netstack3_ip::socket::IpSocketContext;
37use netstack3_ip::{
38    self as ip, IpLayerBindingsContext, IpLayerContext, IpLayerIpExt, IpRoutingBindingsTypes,
39    NdpBindingsContext,
40};
41use netstack3_tcp::{self as tcp, TcpBindingsContext, TcpBindingsTypes, TcpContext};
42use netstack3_udp::{self as udp, UdpBindingsContext, UdpBindingsTypes, UdpCounterContext};
43
44use crate::TimerId;
45use crate::transport::CoreTxMetadata;
46
47/// A marker for extensions to IP types.
48///
49/// This trait acts as a marker for [`BaseIpExt`] for both `Self` and
50/// `Self::OtherVersion`.
51pub trait IpExt: BaseIpExt + datagram::DualStackIpExt<OtherVersion: BaseIpExt> {}
52
53impl<I> IpExt for I where I: BaseIpExt + datagram::DualStackIpExt<OtherVersion: BaseIpExt> {}
54
55/// A marker for extensions to IP types.
56pub trait BaseIpExt:
57    IpLayerIpExt
58    + IpDeviceIpExt
59    + netstack3_base::IcmpIpExt
60    + ip::device::IpDeviceIpExt
61    + tcp::DualStackIpExt
62    + datagram::DualStackIpExt
63{
64}
65
66impl<I> BaseIpExt for I where
67    I: ip::IpLayerIpExt
68        + IpDeviceIpExt
69        + netstack3_base::IcmpIpExt
70        + ip::device::IpDeviceIpExt
71        + tcp::DualStackIpExt
72        + datagram::DualStackIpExt
73{
74}
75
76/// A marker trait for core context implementations.
77///
78/// This trait allows bindings to express trait bounds on routines that have IP
79/// type parameters. It is an umbrella of all the core contexts that must be
80/// implemented by [`crate::context::UnlockedCoreCtx`] to satisfy all the API
81/// objects vended by [`crate::api::CoreApi`].
82pub trait CoreContext<I, BC>:
83    udp::StateContext<I, BC>
84    + UdpCounterContext<I, WeakDeviceId<BC>, BC>
85    + TcpContext<I, BC>
86    + IcmpEchoStateContext<I, BC>
87    + ip::icmp::IcmpStateContext
88    + IpLayerContext<I, BC>
89    + NudContext<I, EthernetLinkDevice, BC>
90    + IpDeviceConfigurationContext<I, BC>
91    + IpDeviceConfigurationHandler<I, BC>
92    + IpSocketContext<I, BC>
93    + DeviceIdContext<AnyDevice, DeviceId = DeviceId<BC>, WeakDeviceId = WeakDeviceId<BC>>
94    + DeviceIdContext<
95        EthernetLinkDevice,
96        DeviceId = EthernetDeviceId<BC>,
97        WeakDeviceId = EthernetWeakDeviceId<BC>,
98    > + RawIpSocketMapContext<I, BC>
99    + RawIpSocketStateContext<I, BC>
100    + MulticastForwardingStateContext<I, BC>
101where
102    I: IpExt,
103    BC: IpBindingsContext<I>,
104{
105}
106
107impl<I, BC, O> CoreContext<I, BC> for O
108where
109    I: IpExt,
110    BC: IpBindingsContext<I>,
111    O: udp::StateContext<I, BC>
112        + UdpCounterContext<I, WeakDeviceId<BC>, BC>
113        + TcpContext<I, BC>
114        + IcmpEchoStateContext<I, BC>
115        + ip::icmp::IcmpStateContext
116        + IpLayerContext<I, BC>
117        + NudContext<I, EthernetLinkDevice, BC>
118        + IpDeviceConfigurationContext<I, BC>
119        + IpDeviceConfigurationHandler<I, BC>
120        + IpSocketContext<I, BC>
121        + DeviceIdContext<AnyDevice, DeviceId = DeviceId<BC>, WeakDeviceId = WeakDeviceId<BC>>
122        + DeviceIdContext<
123            EthernetLinkDevice,
124            DeviceId = EthernetDeviceId<BC>,
125            WeakDeviceId = EthernetWeakDeviceId<BC>,
126        > + RawIpSocketMapContext<I, BC>
127        + RawIpSocketStateContext<I, BC>
128        + MulticastForwardingStateContext<I, BC>,
129{
130}
131
132/// A marker trait for all the types stored in core objects that are specified
133/// by bindings.
134pub trait BindingsTypes:
135    InstantBindingsTypes
136    + BindingsSyncTypes
137    + DeviceLayerTypes
138    + TcpBindingsTypes
139    + FilterBindingsTypes
140    + IcmpEchoBindingsTypes
141    + IcmpBindingsTypes
142    + MulticastForwardingBindingsTypes
143    + RawIpSocketsBindingsTypes
144    + UdpBindingsTypes
145    + TimerBindingsTypes<DispatchId = TimerId<Self>>
146    + TxMetadataBindingsTypes<TxMetadata = CoreTxMetadata<Self>>
147    + IpRoutingBindingsTypes
148{
149}
150
151impl<O> BindingsTypes for O where
152    O: InstantBindingsTypes
153        + BindingsSyncTypes
154        + DeviceLayerTypes
155        + TcpBindingsTypes
156        + FilterBindingsTypes
157        + IcmpEchoBindingsTypes
158        + IcmpBindingsTypes
159        + MulticastForwardingBindingsTypes
160        + RawIpSocketsBindingsTypes
161        + UdpBindingsTypes
162        + TimerBindingsTypes<DispatchId = TimerId<Self>>
163        + TxMetadataBindingsTypes<TxMetadata = CoreTxMetadata<Self>>
164        + IpRoutingBindingsTypes
165{
166}
167
168/// The execution context provided by bindings for a given IP version.
169pub trait IpBindingsContext<I: IpExt>:
170    BindingsTypes
171    + RngContext
172    + UdpBindingsContext<I, DeviceId<Self>>
173    + TcpBindingsContext<DeviceId<Self>>
174    + FilterBindingsContext<DeviceId<Self>>
175    + SocketOpsFilterBindingContext<DeviceId<Self>>
176    + IcmpBindingsContext
177    + IcmpEchoBindingsContext<I, DeviceId<Self>>
178    + MulticastForwardingBindingsContext<I, DeviceId<Self>>
179    + RawIpSocketsBindingsContext<I, DeviceId<Self>>
180    + IpDeviceBindingsContext<I, DeviceId<Self>>
181    + IpLayerBindingsContext<I, DeviceId<Self>>
182    + NudBindingsContext<I, EthernetLinkDevice, EthernetDeviceId<Self>>
183    + device::DeviceLayerEventDispatcher
184    + device::socket::DeviceSocketBindingsContext<DeviceId<Self>>
185    + ReferenceNotifiers
186    + 'static
187{
188}
189
190impl<I, BC> IpBindingsContext<I> for BC
191where
192    I: IpExt,
193    BC: BindingsTypes
194        + RngContext
195        + UdpBindingsContext<I, DeviceId<Self>>
196        + TcpBindingsContext<DeviceId<Self>>
197        + FilterBindingsContext<DeviceId<Self>>
198        + SocketOpsFilterBindingContext<DeviceId<Self>>
199        + IcmpBindingsContext
200        + IcmpEchoBindingsContext<I, DeviceId<Self>>
201        + MulticastForwardingBindingsContext<I, DeviceId<Self>>
202        + RawIpSocketsBindingsContext<I, DeviceId<Self>>
203        + IpDeviceBindingsContext<I, DeviceId<Self>>
204        + IpLayerBindingsContext<I, DeviceId<Self>>
205        + NudBindingsContext<I, EthernetLinkDevice, EthernetDeviceId<Self>>
206        + device::DeviceLayerEventDispatcher
207        + device::socket::DeviceSocketBindingsContext<DeviceId<Self>>
208        + ReferenceNotifiers
209        + 'static,
210{
211}
212
213/// The execution context provided by bindings.
214pub trait BindingsContext:
215    IpBindingsContext<Ipv4>
216    + IpBindingsContext<Ipv6>
217    + NdpBindingsContext<DeviceId<Self>>
218    + EthernetDeviceEventBindingsContext<EthernetDeviceId<Self>>
219{
220}
221impl<BC> BindingsContext for BC where
222    BC: IpBindingsContext<Ipv4>
223        + IpBindingsContext<Ipv6>
224        + NdpBindingsContext<DeviceId<Self>>
225        + EthernetDeviceEventBindingsContext<EthernetDeviceId<Self>>
226{
227}