Skip to main content

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, NeverBuffer};
10
11use crate::internal::base::{BlackholeDeviceCounters, DeviceReceiveFrameSpec};
12use crate::internal::id::{BaseDeviceId, BasePrimaryDeviceId, BaseWeakDeviceId};
13use crate::internal::queue::DeviceBufferSpec;
14use crate::internal::queue::tx::TxBufferAllocator;
15use crate::internal::state::DeviceStateSpec;
16
17/// A weak device ID identifying a blackhole device.
18///
19/// This device ID is like [`WeakDeviceId`] but specifically for blackhole
20/// devices.
21///
22/// [`WeakDeviceId`]: crate::device::WeakDeviceId
23pub type BlackholeWeakDeviceId<BT> = BaseWeakDeviceId<BlackholeDevice, BT>;
24
25/// A strong device ID identifying a blackhole device.
26///
27/// This device ID is like [`DeviceId`] but specifically for blackhole devices.
28///
29/// [`DeviceId`]: crate::device::DeviceId
30pub type BlackholeDeviceId<BT> = BaseDeviceId<BlackholeDevice, BT>;
31
32/// The primary reference for a blackhole device.
33pub type BlackholePrimaryDeviceId<BT> = BasePrimaryDeviceId<BlackholeDevice, BT>;
34
35/// State for a blackhole device.
36pub struct BlackholeDeviceState {}
37
38/// Blackhole device domain.
39#[derive(Copy, Clone)]
40pub enum BlackholeDevice {}
41
42impl Device for BlackholeDevice {}
43
44impl TxBufferAllocator<NeverBuffer> for () {
45    type Error = ();
46
47    fn alloc(&mut self, _len: usize, _qlen: usize) -> Result<NeverBuffer, ()> {
48        Err(())
49    }
50}
51
52impl<BT> DeviceBufferSpec<BT> for BlackholeDevice {
53    type TxBuffer = NeverBuffer;
54    type TxAllocator = ();
55}
56
57impl DeviceStateSpec for BlackholeDevice {
58    type State<BT: crate::DeviceLayerTypes> = BlackholeDeviceState;
59
60    type External<BT: crate::DeviceLayerTypes> = BT::BlackholeDeviceState;
61
62    type CreationProperties = ();
63
64    type Counters = BlackholeDeviceCounters;
65
66    type TimerId<D: netstack3_base::WeakDeviceIdentifier> = Never;
67
68    fn new_device_state<
69        CC: netstack3_base::CoreTimerContext<Self::TimerId<CC::WeakDeviceId>, BC>
70            + netstack3_base::DeviceIdContext<Self>,
71        BC: crate::DeviceLayerTypes + netstack3_base::TimerContext,
72    >(
73        _bindings_ctx: &mut BC,
74        _self_id: CC::WeakDeviceId,
75        _properties: Self::CreationProperties,
76        _tx_allocator: <Self as DeviceBufferSpec<BC>>::TxAllocator,
77    ) -> Self::State<BC>
78    where
79        Self: DeviceBufferSpec<BC>,
80    {
81        BlackholeDeviceState {}
82    }
83
84    const IS_LOOPBACK: bool = false;
85
86    const DEBUG_TYPE: &'static str = "Blackhole";
87}
88
89impl DeviceReceiveFrameSpec for BlackholeDevice {
90    // Blackhole devices never receive frames from bindings, so make it impossible to
91    // instantiate it.
92    type FrameMetadata<D> = Never;
93}