1use core::fmt::{Debug, Display};
6use core::num::NonZeroU64;
7
8use derivative::Derivative;
9use lock_order::lock::{OrderedLockAccess, OrderedLockRef};
10use net_types::ethernet::Mac;
11use net_types::ip::{Ip, IpVersion, Ipv4, Ipv6};
12use netstack3_base::sync::RwLock;
13use netstack3_base::{
14 Counter, CounterRepr, Device, DeviceIdContext, HandleableTimer, Inspectable, Inspector,
15 InspectorExt as _, InstantContext, ReferenceNotifiers, TimerBindingsTypes, TimerHandler,
16 TxMetadataBindingsTypes,
17};
18use netstack3_filter::FilterBindingsTypes;
19use netstack3_hashmap::HashMap;
20use netstack3_ip::device::Ipv6LinkLayerAddr;
21use netstack3_ip::nud::{LinkResolutionContext, NudCounters};
22
23use crate::blackhole::{BlackholeDeviceId, BlackholePrimaryDeviceId};
24use crate::internal::arp::ArpCounters;
25use crate::internal::ethernet::{EthernetLinkDevice, EthernetTimerId};
26use crate::internal::id::{
27 BaseDeviceId, BasePrimaryDeviceId, DeviceId, EthernetDeviceId, EthernetPrimaryDeviceId,
28 EthernetWeakDeviceId,
29};
30use crate::internal::loopback::{LoopbackDeviceId, LoopbackPrimaryDeviceId};
31use crate::internal::pure_ip::{PureIpDeviceId, PureIpPrimaryDeviceId};
32use crate::internal::queue::rx::ReceiveQueueBindingsContext;
33use crate::internal::queue::tx::{TransmitQueueBindingsContext, TxBufferAllocator};
34use crate::internal::socket::{self, DeviceSocketCounters, HeldSockets};
35use crate::internal::state::DeviceStateSpec;
36
37pub struct DevicesIter<'s, BT: DeviceLayerTypes> {
43 pub(super) ethernet:
44 netstack3_hashmap::hash_map::Values<'s, EthernetDeviceId<BT>, EthernetPrimaryDeviceId<BT>>,
45 pub(super) pure_ip:
46 netstack3_hashmap::hash_map::Values<'s, PureIpDeviceId<BT>, PureIpPrimaryDeviceId<BT>>,
47 pub(super) blackhole: netstack3_hashmap::hash_map::Values<
48 's,
49 BlackholeDeviceId<BT>,
50 BlackholePrimaryDeviceId<BT>,
51 >,
52 pub(super) loopback: core::option::Iter<'s, LoopbackPrimaryDeviceId<BT>>,
53}
54
55impl<'s, BT: DeviceLayerTypes> Iterator for DevicesIter<'s, BT> {
56 type Item = DeviceId<BT>;
57
58 fn next(&mut self) -> Option<Self::Item> {
59 let Self { ethernet, pure_ip, blackhole, loopback } = self;
60 ethernet
61 .map(|primary| primary.clone_strong().into())
62 .chain(pure_ip.map(|primary| primary.clone_strong().into()))
63 .chain(blackhole.map(|primary| primary.clone_strong().into()))
64 .chain(loopback.map(|primary| primary.clone_strong().into()))
65 .next()
66 }
67}
68
69#[allow(missing_docs)]
71pub enum Ipv6DeviceLinkLayerAddr {
72 Mac(Mac),
73 }
75
76impl Ipv6LinkLayerAddr for Ipv6DeviceLinkLayerAddr {
77 fn as_bytes(&self) -> &[u8] {
78 match self {
79 Ipv6DeviceLinkLayerAddr::Mac(a) => a.as_ref(),
80 }
81 }
82
83 fn eui64_iid(&self) -> [u8; 8] {
84 match self {
85 Ipv6DeviceLinkLayerAddr::Mac(a) => a.to_eui64(),
86 }
87 }
88}
89
90#[derive(Derivative)]
92#[derivative(
93 Clone(bound = ""),
94 Eq(bound = ""),
95 PartialEq(bound = ""),
96 Hash(bound = ""),
97 Debug(bound = "")
98)]
99pub struct DeviceLayerTimerId<BT: DeviceLayerTypes>(DeviceLayerTimerIdInner<BT>);
100
101#[derive(Derivative)]
102#[derivative(
103 Clone(bound = ""),
104 Eq(bound = ""),
105 PartialEq(bound = ""),
106 Hash(bound = ""),
107 Debug(bound = "")
108)]
109#[allow(missing_docs)]
110enum DeviceLayerTimerIdInner<BT: DeviceLayerTypes> {
111 Ethernet(EthernetTimerId<EthernetWeakDeviceId<BT>>),
112}
113
114impl<BT: DeviceLayerTypes> From<EthernetTimerId<EthernetWeakDeviceId<BT>>>
115 for DeviceLayerTimerId<BT>
116{
117 fn from(id: EthernetTimerId<EthernetWeakDeviceId<BT>>) -> DeviceLayerTimerId<BT> {
118 DeviceLayerTimerId(DeviceLayerTimerIdInner::Ethernet(id))
119 }
120}
121
122impl<CC, BT> HandleableTimer<CC, BT> for DeviceLayerTimerId<BT>
123where
124 BT: DeviceLayerTypes,
125 CC: TimerHandler<BT, EthernetTimerId<EthernetWeakDeviceId<BT>>>,
126{
127 fn handle(self, core_ctx: &mut CC, bindings_ctx: &mut BT, timer: BT::UniqueTimerId) {
128 let Self(id) = self;
129 match id {
130 DeviceLayerTimerIdInner::Ethernet(id) => core_ctx.handle_timer(bindings_ctx, id, timer),
131 }
132 }
133}
134
135#[derive(Derivative)]
137#[derivative(Default(bound = ""))]
138pub struct Devices<BT: DeviceLayerTypes> {
139 pub ethernet: HashMap<EthernetDeviceId<BT>, EthernetPrimaryDeviceId<BT>>,
141 pub pure_ip: HashMap<PureIpDeviceId<BT>, PureIpPrimaryDeviceId<BT>>,
143 pub blackhole: HashMap<BlackholeDeviceId<BT>, BlackholePrimaryDeviceId<BT>>,
145 pub loopback: Option<LoopbackPrimaryDeviceId<BT>>,
147}
148
149impl<BT: DeviceLayerTypes> Devices<BT> {
150 pub fn iter(&self) -> DevicesIter<'_, BT> {
152 let Self { ethernet, pure_ip, blackhole, loopback } = self;
153 DevicesIter {
154 ethernet: ethernet.values(),
155 pure_ip: pure_ip.values(),
156 blackhole: blackhole.values(),
157 loopback: loopback.iter(),
158 }
159 }
160}
161
162#[derive(Derivative)]
164#[derivative(Default(bound = ""))]
165pub struct DeviceLayerState<BT: DeviceLayerTypes> {
166 devices: RwLock<Devices<BT>>,
167 pub origin: OriginTracker,
169 pub shared_sockets: HeldSockets<BT>,
171 pub device_socket_counters: DeviceSocketCounters,
173 pub counters: DeviceCounters,
175 pub ethernet_counters: EthernetDeviceCounters,
177 pub pure_ip_counters: PureIpDeviceCounters,
179 pub nud_v4_counters: NudCounters<Ipv4>,
181 pub nud_v6_counters: NudCounters<Ipv6>,
183 pub arp_counters: ArpCounters,
185}
186
187impl<BT: DeviceLayerTypes> DeviceLayerState<BT> {
188 pub fn nud_counters<I: Ip>(&self) -> &NudCounters<I> {
190 I::map_ip((), |()| &self.nud_v4_counters, |()| &self.nud_v6_counters)
191 }
192}
193
194impl<BT: DeviceLayerTypes> OrderedLockAccess<Devices<BT>> for DeviceLayerState<BT> {
195 type Lock = RwLock<Devices<BT>>;
196 fn ordered_lock_access(&self) -> OrderedLockRef<'_, Self::Lock> {
197 OrderedLockRef::new(&self.devices)
198 }
199}
200
201#[derive(Default)]
203pub struct EthernetDeviceCounters {
204 pub recv_unsupported_ethertype: Counter,
206 pub recv_no_ethertype: Counter,
208}
209
210impl Inspectable for EthernetDeviceCounters {
211 fn record<I: Inspector>(&self, inspector: &mut I) {
212 inspector.record_child("Ethernet", |inspector| {
213 let Self { recv_no_ethertype, recv_unsupported_ethertype } = self;
214 inspector.record_child("Rx", |inspector| {
215 inspector.record_counter("NoEthertype", recv_no_ethertype);
216 inspector.record_counter("UnsupportedEthertype", recv_unsupported_ethertype);
217 });
218 })
219 }
220}
221
222#[derive(Default)]
224pub struct PureIpDeviceCounters {}
225
226impl Inspectable for PureIpDeviceCounters {
227 fn record<I: Inspector>(&self, _inspector: &mut I) {}
228}
229
230pub struct BlackholeDeviceCounters;
232
233impl Inspectable for BlackholeDeviceCounters {
234 fn record<I: Inspector>(&self, _inspector: &mut I) {}
235}
236
237#[derive(Default, Debug)]
239#[cfg_attr(
240 any(test, feature = "testutils"),
241 derive(PartialEq, netstack3_macros::CounterCollection)
242)]
243pub struct DeviceCounters<C: CounterRepr = Counter> {
244 pub send_total_frames: C,
247 pub send_frame: C,
249 pub send_bytes: C,
251 pub send_queue_full: C,
253 pub send_serialize_error: C,
255 pub recv_frame: C,
257 pub recv_bytes: C,
259 pub recv_parse_error: C,
261 pub recv_ipv4_delivered: C,
263 pub recv_ipv6_delivered: C,
265 pub send_ipv4_frame: C,
267 pub send_ipv6_frame: C,
269 pub send_dropped_no_queue: C,
271 pub send_dropped_dequeue: C,
273}
274
275impl DeviceCounters {
276 pub fn send_frame<I: Ip>(&self) -> &Counter {
278 match I::VERSION {
279 IpVersion::V4 => &self.send_ipv4_frame,
280 IpVersion::V6 => &self.send_ipv6_frame,
281 }
282 }
283}
284
285impl Inspectable for DeviceCounters {
286 fn record<I: Inspector>(&self, inspector: &mut I) {
287 let Self {
288 recv_frame,
289 recv_bytes,
290 recv_ipv4_delivered,
291 recv_ipv6_delivered,
292 recv_parse_error,
293 send_dropped_no_queue,
294 send_frame,
295 send_bytes,
296 send_ipv4_frame,
297 send_ipv6_frame,
298 send_queue_full,
299 send_serialize_error,
300 send_total_frames,
301 send_dropped_dequeue,
302 } = self;
303 inspector.record_child("Rx", |inspector| {
304 inspector.record_counter("TotalFrames", recv_frame);
305 inspector.record_counter("TotalBytes", recv_bytes);
306 inspector.record_counter("Malformed", recv_parse_error);
307 inspector.record_counter("Ipv4Delivered", recv_ipv4_delivered);
308 inspector.record_counter("Ipv6Delivered", recv_ipv6_delivered);
309 });
310 inspector.record_child("Tx", |inspector| {
311 inspector.record_counter("TotalFrames", send_total_frames);
312 inspector.record_counter("Sent", send_frame);
313 inspector.record_counter("SentBytes", send_bytes);
314 inspector.record_counter("SendIpv4Frame", send_ipv4_frame);
315 inspector.record_counter("SendIpv6Frame", send_ipv6_frame);
316 inspector.record_counter("NoQueue", send_dropped_no_queue);
317 inspector.record_counter("QueueFull", send_queue_full);
318 inspector.record_counter("SerializeError", send_serialize_error);
319 inspector.record_counter("DequeueDrop", send_dropped_dequeue);
320 });
321 }
322}
323#[derive(Clone, Debug, PartialEq)]
334pub struct OriginTracker(#[cfg(debug_assertions)] u64);
335
336impl Default for OriginTracker {
337 fn default() -> Self {
338 Self::new()
339 }
340}
341
342impl OriginTracker {
343 #[cfg_attr(not(debug_assertions), inline)]
350 fn new() -> Self {
351 Self(
352 #[cfg(debug_assertions)]
353 {
354 static COUNTER: core::sync::atomic::AtomicU64 =
355 core::sync::atomic::AtomicU64::new(0);
356 COUNTER.fetch_add(1, core::sync::atomic::Ordering::Relaxed)
357 },
358 )
359 }
360}
361
362pub trait OriginTrackerContext {
367 fn origin_tracker(&mut self) -> OriginTracker;
369}
370
371pub trait DeviceCollectionContext<D: Device + DeviceStateSpec, BT: DeviceLayerTypes>:
375 DeviceIdContext<D>
376{
377 fn insert(&mut self, device: BasePrimaryDeviceId<D, BT>);
379
380 fn remove(&mut self, device: &BaseDeviceId<D, BT>) -> Option<BasePrimaryDeviceId<D, BT>>;
382}
383
384pub trait DeviceReceiveFrameSpec {
390 type FrameMetadata<D>;
392}
393
394pub trait DeviceLayerStateTypes: InstantContext + FilterBindingsTypes {
396 type LoopbackDeviceState: Send + Sync + DeviceClassMatcher<Self::DeviceClass>;
398
399 type EthernetDeviceState: Send + Sync + DeviceClassMatcher<Self::DeviceClass>;
401
402 type PureIpDeviceState: Send + Sync + DeviceClassMatcher<Self::DeviceClass>;
404
405 type BlackholeDeviceState: Send + Sync + DeviceClassMatcher<Self::DeviceClass>;
407
408 type DeviceIdentifier: Send + Sync + Debug + Display + DeviceIdAndNameMatcher;
411}
412
413pub trait DeviceClassMatcher<DeviceClass> {
416 fn device_class_matches(&self, device_class: &DeviceClass) -> bool;
419}
420
421pub trait DeviceIdAndNameMatcher {
424 fn id_matches(&self, id: &NonZeroU64) -> bool;
426
427 fn name_matches(&self, name: &str) -> bool;
429}
430
431pub trait DeviceBufferBindingsTypes: 'static {
433 type TxBuffer: packet::FragmentedBuffer + AsMut<[u8]> + Send + 'static;
435 type TxAllocator: TxBufferAllocator<Self::TxBuffer> + Send + 'static;
437}
438
439pub trait DeviceLayerTypes:
445 DeviceBufferBindingsTypes
446 + DeviceLayerStateTypes
447 + socket::DeviceSocketTypes
448 + LinkResolutionContext<EthernetLinkDevice>
449 + TimerBindingsTypes
450 + ReferenceNotifiers
451 + TxMetadataBindingsTypes
452 + 'static
453{
454}
455impl<
456 BC: DeviceLayerStateTypes
457 + socket::DeviceSocketTypes
458 + LinkResolutionContext<EthernetLinkDevice>
459 + TimerBindingsTypes
460 + ReferenceNotifiers
461 + TxMetadataBindingsTypes
462 + 'static
463 + DeviceBufferBindingsTypes,
464> DeviceLayerTypes for BC
465{
466}
467
468pub trait DeviceLayerEventDispatcher:
470 DeviceLayerTypes
471 + ReceiveQueueBindingsContext<LoopbackDeviceId<Self>>
472 + TransmitQueueBindingsContext<EthernetDeviceId<Self>>
473 + TransmitQueueBindingsContext<LoopbackDeviceId<Self>>
474 + TransmitQueueBindingsContext<PureIpDeviceId<Self>>
475 + Sized
476{
477 type DequeueContext;
483
484 fn send_ethernet_frame(
494 &mut self,
495 device: &EthernetDeviceId<Self>,
496 frame: Self::TxBuffer,
497 dequeue_context: Option<&mut Self::DequeueContext>,
498 ) -> Result<(), DeviceSendFrameError>;
499
500 fn send_ip_packet(
510 &mut self,
511 device: &PureIpDeviceId<Self>,
512 packet: Self::TxBuffer,
513 ip_version: IpVersion,
514 dequeue_context: Option<&mut Self::DequeueContext>,
515 ) -> Result<(), DeviceSendFrameError>;
516}
517
518#[derive(Debug, PartialEq, Eq)]
520pub enum DeviceSendFrameError {
521 NoBuffers,
523}
524
525#[cfg(any(test, feature = "testutils"))]
526pub mod testutil {
527 use alloc::vec::Vec;
528 use netstack3_base::testutil::FakeBindingsCtx;
529 use netstack3_base::{CounterCollection, ResourceCounterContext};
530
531 use super::*;
532 use crate::internal::queue::tx::BufVecU8Allocator;
533
534 pub type DeviceCounterExpectations = DeviceCounters<u64>;
536
537 impl DeviceCounterExpectations {
538 #[track_caller]
540 pub fn assert_counters<D, CC: ResourceCounterContext<D, DeviceCounters>>(
541 &self,
542 core_ctx: &CC,
543 device: &D,
544 ) {
545 assert_eq!(&core_ctx.counters().cast::<u64>(), self, "stack-wide counters");
546 assert_eq!(
547 &core_ctx.per_resource_counters(device).cast::<u64>(),
548 self,
549 "per-device counters"
550 );
551 }
552 }
553
554 impl<TimerId: 'static, Event: Debug + 'static, State: 'static, FrameMeta: 'static>
555 DeviceBufferBindingsTypes for FakeBindingsCtx<TimerId, Event, State, FrameMeta>
556 {
557 type TxBuffer = packet::Buf<Vec<u8>>;
558 type TxAllocator = BufVecU8Allocator;
559 }
560}
561
562#[cfg(test)]
563mod tests {
564 use super::*;
565
566 #[test]
567 fn origin_tracker() {
568 let tracker = OriginTracker::new();
569 if cfg!(debug_assertions) {
570 assert_ne!(tracker, OriginTracker::new());
571 } else {
572 assert_eq!(tracker, OriginTracker::new());
573 }
574 assert_eq!(tracker.clone(), tracker);
575 }
576}