netstack3_device/
blackhole.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//! A blackhole device receives no traffic and drops all traffic sent through it.
6
7use core::convert::Infallible as Never;
8
9use netstack3_base::Device;
10
11use crate::internal::base::{BlackholeDeviceCounters, DeviceReceiveFrameSpec};
12use crate::internal::id::{BasePrimaryDeviceId, BaseWeakDeviceId};
13use crate::{BaseDeviceId, DeviceStateSpec};
14
15/// A weak device ID identifying a blackhole device.
16///
17/// This device ID is like [`WeakDeviceId`] but specifically for blackhole
18/// devices.
19///
20/// [`WeakDeviceId`]: crate::device::WeakDeviceId
21pub type BlackholeWeakDeviceId<BT> = BaseWeakDeviceId<BlackholeDevice, BT>;
22
23/// A strong device ID identifying a blackhole device.
24///
25/// This device ID is like [`DeviceId`] but specifically for blackhole devices.
26///
27/// [`DeviceId`]: crate::device::DeviceId
28pub type BlackholeDeviceId<BT> = BaseDeviceId<BlackholeDevice, BT>;
29
30/// The primary reference for a blackhole device.
31pub type BlackholePrimaryDeviceId<BT> = BasePrimaryDeviceId<BlackholeDevice, BT>;
32
33/// State for a blackhole device.
34pub struct BlackholeDeviceState {}
35
36/// Blackhole device domain.
37#[derive(Copy, Clone)]
38pub enum BlackholeDevice {}
39
40impl Device for BlackholeDevice {}
41
42impl DeviceStateSpec for BlackholeDevice {
43    type State<BT: crate::DeviceLayerTypes> = BlackholeDeviceState;
44
45    type External<BT: crate::DeviceLayerTypes> = BT::BlackholeDeviceState;
46
47    type CreationProperties = ();
48
49    type Counters = BlackholeDeviceCounters;
50
51    type TimerId<D: netstack3_base::WeakDeviceIdentifier> = Never;
52
53    fn new_device_state<
54        CC: netstack3_base::CoreTimerContext<Self::TimerId<CC::WeakDeviceId>, BC>
55            + netstack3_base::DeviceIdContext<Self>,
56        BC: crate::DeviceLayerTypes + netstack3_base::TimerContext,
57    >(
58        _bindings_ctx: &mut BC,
59        _self_id: CC::WeakDeviceId,
60        _properties: Self::CreationProperties,
61    ) -> Self::State<BC> {
62        BlackholeDeviceState {}
63    }
64
65    const IS_LOOPBACK: bool = false;
66
67    const DEBUG_TYPE: &'static str = "Blackhole";
68}
69
70impl DeviceReceiveFrameSpec for BlackholeDevice {
71    // Blackhole devices never receive frames from bindings, so make it impossible to
72    // instantiate it.
73    type FrameMetadata<D> = Never;
74}