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 netstack3_base::{ChecksumOffloadSpec, Device, NeverBuffer};
8
9use crate::internal::base::{BlackholeDeviceCounters, DeviceReceiveFrameSpec};
10use crate::internal::id::{BaseDeviceId, BasePrimaryDeviceId, BaseWeakDeviceId};
11use crate::internal::queue::DeviceBufferSpec;
12use crate::internal::queue::tx::TxBufferAllocator;
13use crate::internal::state::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 TxBufferAllocator<NeverBuffer> for () {
43    type Error = ();
44
45    fn alloc(&mut self, _len: usize, _qlen: usize) -> Result<NeverBuffer, ()> {
46        Err(())
47    }
48}
49
50impl<BT> DeviceBufferSpec<BT> for BlackholeDevice {
51    type TxBuffer = NeverBuffer;
52    type TxAllocator = ();
53}
54
55impl DeviceStateSpec for BlackholeDevice {
56    type State<BT: crate::DeviceLayerTypes> = BlackholeDeviceState;
57
58    type External<BT: crate::DeviceLayerTypes> = BT::BlackholeDeviceState;
59
60    type CreationProperties = ();
61
62    type Counters = BlackholeDeviceCounters;
63
64    type TimerId<D: netstack3_base::WeakDeviceIdentifier> = !;
65
66    fn new_device_state<
67        CC: netstack3_base::CoreTimerContext<Self::TimerId<CC::WeakDeviceId>, BC>
68            + netstack3_base::DeviceIdContext<Self>,
69        BC: crate::DeviceLayerTypes + netstack3_base::TimerContext,
70    >(
71        _bindings_ctx: &mut BC,
72        _self_id: CC::WeakDeviceId,
73        _properties: Self::CreationProperties,
74        _tx_allocator: <Self as DeviceBufferSpec<BC>>::TxAllocator,
75    ) -> Self::State<BC>
76    where
77        Self: DeviceBufferSpec<BC>,
78    {
79        BlackholeDeviceState {}
80    }
81
82    const IS_LOOPBACK: bool = false;
83
84    const DEBUG_TYPE: &'static str = "Blackhole";
85
86    fn tx_offload_spec<BT: crate::DeviceLayerTypes>(
87        _state: &Self::State<BT>,
88    ) -> Option<ChecksumOffloadSpec> {
89        None
90    }
91}
92
93impl DeviceReceiveFrameSpec for BlackholeDevice {
94    // Blackhole devices never receive frames from bindings, so make it impossible to
95    // instantiate it.
96    type FrameMetadata<D> = !;
97}