netstack3_device/
loopback.rs

1// Copyright 2022 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//! The loopback device.
6
7use alloc::vec::Vec;
8use core::convert::Infallible as Never;
9use core::fmt::Debug;
10use derivative::Derivative;
11
12use lock_order::lock::{OrderedLockAccess, OrderedLockRef};
13use log::trace;
14use net_types::ethernet::Mac;
15use net_types::ip::{Ipv4, Ipv6, Mtu};
16use netstack3_base::sync::Mutex;
17use netstack3_base::{
18    AnyDevice, BroadcastIpExt, CoreTimerContext, Device, DeviceIdAnyCompatContext, DeviceIdContext,
19    FrameDestination, RecvFrameContext, RecvIpFrameMeta, ResourceCounterContext, SendFrameError,
20    SendFrameErrorReason, SendableFrameMeta, StrongDeviceIdentifier, TimerContext,
21    TxMetadataBindingsTypes, WeakDeviceIdentifier,
22};
23use netstack3_ip::{DeviceIpLayerMetadata, IpPacketDestination};
24use packet::{Buf, Buffer as _, BufferMut, Serializer};
25use packet_formats::ethernet::{
26    EtherType, EthernetFrame, EthernetFrameBuilder, EthernetFrameLengthCheck, EthernetIpExt,
27};
28
29use crate::internal::base::{
30    DeviceCounters, DeviceLayerTypes, DeviceReceiveFrameSpec, EthernetDeviceCounters,
31};
32use crate::internal::id::{BaseDeviceId, BasePrimaryDeviceId, BaseWeakDeviceId, WeakDeviceId};
33use crate::internal::queue::rx::{
34    ReceiveDequeFrameContext, ReceiveQueue, ReceiveQueueState, ReceiveQueueTypes,
35};
36use crate::internal::queue::tx::{
37    BufVecU8Allocator, TransmitQueue, TransmitQueueHandler, TransmitQueueState,
38};
39use crate::internal::queue::{DequeueState, TransmitQueueFrameError};
40use crate::internal::socket::{
41    DeviceSocketHandler, DeviceSocketMetadata, DeviceSocketSendTypes, EthernetHeaderParams,
42    ReceivedFrame,
43};
44use crate::internal::state::{DeviceStateSpec, IpLinkDeviceState};
45
46/// The MAC address corresponding to the loopback interface.
47const LOOPBACK_MAC: Mac = Mac::UNSPECIFIED;
48
49/// A weak device ID identifying a loopback device.
50///
51/// This device ID is like [`WeakDeviceId`] but specifically for loopback
52/// devices.
53///
54/// [`WeakDeviceId`]: crate::device::WeakDeviceId
55pub type LoopbackWeakDeviceId<BT> = BaseWeakDeviceId<LoopbackDevice, BT>;
56
57/// A strong device ID identifying a loopback device.
58///
59/// This device ID is like [`DeviceId`] but specifically for loopback devices.
60///
61/// [`DeviceId`]: crate::device::DeviceId
62pub type LoopbackDeviceId<BT> = BaseDeviceId<LoopbackDevice, BT>;
63
64/// The primary reference for a loopback device.
65pub type LoopbackPrimaryDeviceId<BT> = BasePrimaryDeviceId<LoopbackDevice, BT>;
66
67/// Loopback device domain.
68#[derive(Copy, Clone)]
69pub enum LoopbackDevice {}
70
71impl Device for LoopbackDevice {}
72
73impl DeviceStateSpec for LoopbackDevice {
74    type State<BT: DeviceLayerTypes> = LoopbackDeviceState<WeakDeviceId<BT>, BT>;
75    type External<BT: DeviceLayerTypes> = BT::LoopbackDeviceState;
76    type CreationProperties = LoopbackCreationProperties;
77    type Counters = EthernetDeviceCounters;
78    type TimerId<D: WeakDeviceIdentifier> = Never;
79
80    fn new_device_state<
81        CC: CoreTimerContext<Self::TimerId<CC::WeakDeviceId>, BC> + DeviceIdContext<Self>,
82        BC: DeviceLayerTypes + TimerContext,
83    >(
84        _bindings_ctx: &mut BC,
85        _self_id: CC::WeakDeviceId,
86        LoopbackCreationProperties { mtu }: Self::CreationProperties,
87    ) -> Self::State<BC> {
88        LoopbackDeviceState {
89            counters: Default::default(),
90            mtu,
91            rx_queue: Default::default(),
92            tx_queue: Default::default(),
93        }
94    }
95
96    const IS_LOOPBACK: bool = true;
97    const DEBUG_TYPE: &'static str = "Loopback";
98}
99
100/// Properties used to create a loopback device.
101#[derive(Debug)]
102pub struct LoopbackCreationProperties {
103    /// The device's MTU.
104    pub mtu: Mtu,
105}
106
107/// State for a loopback device.
108pub struct LoopbackDeviceState<D: WeakDeviceIdentifier, BT: TxMetadataBindingsTypes> {
109    /// Loopback device counters.
110    pub counters: EthernetDeviceCounters,
111    /// The MTU this device was created with (immutable).
112    pub mtu: Mtu,
113    /// Loopback device receive queue.
114    pub rx_queue: ReceiveQueue<LoopbackRxQueueMeta<D, BT>, Buf<Vec<u8>>>,
115    /// Loopback device transmit queue.
116    pub tx_queue: TransmitQueue<LoopbackTxQueueMeta<D, BT>, Buf<Vec<u8>>, BufVecU8Allocator>,
117}
118
119#[derive(Derivative)]
120#[derivative(Default(bound = ""))]
121/// Metadata associated with a frame in the Loopback TX queue.
122pub struct LoopbackTxQueueMeta<D: WeakDeviceIdentifier, BT: TxMetadataBindingsTypes> {
123    /// Device that should be used to deliver the packet. If not set then the
124    /// packet delivered as if it came from the loopback device.
125    target_device: Option<D>,
126    /// Metadata that is produced and consumed by the IP layer but which traverses
127    /// the device layer through the loopback device.
128    ip_layer_metadata: DeviceIpLayerMetadata<BT>,
129}
130
131/// Metadata associated with a frame in the Loopback RX queue.
132#[derive(Derivative)]
133#[derivative(Debug(bound = ""))]
134pub struct LoopbackRxQueueMeta<D: WeakDeviceIdentifier, BT: TxMetadataBindingsTypes> {
135    /// Device that should be used to deliver the packet. If not set then the
136    /// packet delivered as if it came from the loopback device.
137    target_device: Option<D>,
138    /// Metadata that is produced and consumed by the IP layer but which traverses
139    /// the device layer through the loopback device.
140    ip_layer_metadata: DeviceIpLayerMetadata<BT>,
141}
142
143impl<D: WeakDeviceIdentifier, BT: TxMetadataBindingsTypes> From<LoopbackTxQueueMeta<D, BT>>
144    for LoopbackRxQueueMeta<D, BT>
145{
146    fn from(
147        LoopbackTxQueueMeta { target_device, ip_layer_metadata }: LoopbackTxQueueMeta<D, BT>,
148    ) -> Self {
149        Self { target_device, ip_layer_metadata }
150    }
151}
152
153impl<BT: DeviceLayerTypes>
154    OrderedLockAccess<ReceiveQueueState<LoopbackRxQueueMeta<WeakDeviceId<BT>, BT>, Buf<Vec<u8>>>>
155    for IpLinkDeviceState<LoopbackDevice, BT>
156{
157    type Lock = Mutex<ReceiveQueueState<LoopbackRxQueueMeta<WeakDeviceId<BT>, BT>, Buf<Vec<u8>>>>;
158    fn ordered_lock_access(&self) -> OrderedLockRef<'_, Self::Lock> {
159        OrderedLockRef::new(&self.link.rx_queue.queue)
160    }
161}
162
163impl<BT: DeviceLayerTypes>
164    OrderedLockAccess<DequeueState<LoopbackRxQueueMeta<WeakDeviceId<BT>, BT>, Buf<Vec<u8>>>>
165    for IpLinkDeviceState<LoopbackDevice, BT>
166{
167    type Lock = Mutex<DequeueState<LoopbackRxQueueMeta<WeakDeviceId<BT>, BT>, Buf<Vec<u8>>>>;
168    fn ordered_lock_access(&self) -> OrderedLockRef<'_, Self::Lock> {
169        OrderedLockRef::new(&self.link.rx_queue.deque)
170    }
171}
172
173impl<BT: DeviceLayerTypes>
174    OrderedLockAccess<
175        TransmitQueueState<
176            LoopbackTxQueueMeta<WeakDeviceId<BT>, BT>,
177            Buf<Vec<u8>>,
178            BufVecU8Allocator,
179        >,
180    > for IpLinkDeviceState<LoopbackDevice, BT>
181{
182    type Lock = Mutex<
183        TransmitQueueState<
184            LoopbackTxQueueMeta<WeakDeviceId<BT>, BT>,
185            Buf<Vec<u8>>,
186            BufVecU8Allocator,
187        >,
188    >;
189    fn ordered_lock_access(&self) -> OrderedLockRef<'_, Self::Lock> {
190        OrderedLockRef::new(&self.link.tx_queue.queue)
191    }
192}
193
194impl<BT: DeviceLayerTypes>
195    OrderedLockAccess<DequeueState<LoopbackTxQueueMeta<WeakDeviceId<BT>, BT>, Buf<Vec<u8>>>>
196    for IpLinkDeviceState<LoopbackDevice, BT>
197{
198    type Lock = Mutex<DequeueState<LoopbackTxQueueMeta<WeakDeviceId<BT>, BT>, Buf<Vec<u8>>>>;
199    fn ordered_lock_access(&self) -> OrderedLockRef<'_, Self::Lock> {
200        OrderedLockRef::new(&self.link.tx_queue.deque)
201    }
202}
203
204impl DeviceSocketSendTypes for LoopbackDevice {
205    /// When `None`, data will be sent as a raw Ethernet frame without any
206    /// system-applied headers.
207    type Metadata = Option<EthernetHeaderParams>;
208}
209
210impl<CC, BC> ReceiveDequeFrameContext<LoopbackDevice, BC> for CC
211where
212    CC: DeviceIdContext<LoopbackDevice>
213        + ResourceCounterContext<Self::DeviceId, EthernetDeviceCounters>
214        + ReceiveQueueTypes<
215            LoopbackDevice,
216            BC,
217            Meta = LoopbackRxQueueMeta<<CC as DeviceIdContext<AnyDevice>>::WeakDeviceId, BC>,
218        >,
219    // Loopback needs to deliver messages to `AnyDevice`.
220    CC: DeviceIdAnyCompatContext<LoopbackDevice>
221        + RecvFrameContext<
222            RecvIpFrameMeta<
223                <CC as DeviceIdContext<AnyDevice>>::DeviceId,
224                DeviceIpLayerMetadata<BC>,
225                Ipv4,
226            >,
227            BC,
228        > + RecvFrameContext<
229            RecvIpFrameMeta<
230                <CC as DeviceIdContext<AnyDevice>>::DeviceId,
231                DeviceIpLayerMetadata<BC>,
232                Ipv6,
233            >,
234            BC,
235        > + ResourceCounterContext<<CC as DeviceIdContext<AnyDevice>>::DeviceId, DeviceCounters>
236        + DeviceSocketHandler<AnyDevice, BC>,
237    CC::Buffer: BufferMut + Debug,
238    BC: DeviceLayerTypes,
239{
240    fn handle_frame(
241        &mut self,
242        bindings_ctx: &mut BC,
243        device_id: &Self::DeviceId,
244        rx_meta: Self::Meta,
245        mut buf: Self::Buffer,
246    ) {
247        let (frame, whole_body) =
248            match buf.parse_with_view::<_, EthernetFrame<_>>(EthernetFrameLengthCheck::NoCheck) {
249                Err(e) => {
250                    self.increment_both(&device_id.clone().into(), |counters: &DeviceCounters| {
251                        &counters.recv_parse_error
252                    });
253                    trace!("dropping invalid ethernet frame over loopback: {:?}", e);
254                    return;
255                }
256                Ok(e) => e,
257            };
258
259        let LoopbackRxQueueMeta { target_device, ip_layer_metadata } = rx_meta;
260        let target_device: <CC as DeviceIdContext<AnyDevice>>::DeviceId =
261            match target_device.map(|d| d.upgrade()) {
262                // This is a packet that should be delivered on `target_device`.
263                Some(Some(dev)) => dev,
264
265                // `target_device` is gone. Drop the packet.
266                Some(None) => return,
267
268                // This is a packet sent to the loopback device.
269                None => device_id.clone().into(),
270            };
271
272        self.increment_both(&target_device, |counters: &DeviceCounters| &counters.recv_frame);
273
274        let frame_dest = FrameDestination::from_dest(frame.dst_mac(), Mac::UNSPECIFIED);
275        let ethertype = frame.ethertype();
276
277        DeviceSocketHandler::<AnyDevice, _>::handle_frame(
278            self,
279            bindings_ctx,
280            &target_device,
281            ReceivedFrame::from_ethernet(frame, frame_dest).into(),
282            whole_body,
283        );
284
285        match ethertype {
286            Some(EtherType::Ipv4) => {
287                self.increment_both(&target_device, |counters: &DeviceCounters| {
288                    &counters.recv_ipv4_delivered
289                });
290                self.receive_frame(
291                    bindings_ctx,
292                    RecvIpFrameMeta::<_, _, Ipv4>::new(
293                        target_device,
294                        Some(frame_dest),
295                        ip_layer_metadata,
296                    ),
297                    buf,
298                );
299            }
300            Some(EtherType::Ipv6) => {
301                self.increment_both(&target_device, |counters: &DeviceCounters| {
302                    &counters.recv_ipv6_delivered
303                });
304                self.receive_frame(
305                    bindings_ctx,
306                    RecvIpFrameMeta::<_, _, Ipv6>::new(
307                        target_device,
308                        Some(frame_dest),
309                        ip_layer_metadata,
310                    ),
311                    buf,
312                );
313            }
314            Some(ethertype @ (EtherType::Arp | EtherType::Other(_))) => {
315                self.increment_both(device_id, |counters: &EthernetDeviceCounters| {
316                    &counters.recv_unsupported_ethertype
317                });
318                trace!("not handling loopback frame of type {:?}", ethertype)
319            }
320            None => {
321                self.increment_both(device_id, |counters: &EthernetDeviceCounters| {
322                    &counters.recv_no_ethertype
323                });
324                trace!("dropping ethernet frame without ethertype");
325            }
326        }
327    }
328}
329
330impl<CC, BC> SendableFrameMeta<CC, BC>
331    for DeviceSocketMetadata<LoopbackDevice, <CC as DeviceIdContext<LoopbackDevice>>::DeviceId>
332where
333    CC: TransmitQueueHandler<
334            LoopbackDevice,
335            BC,
336            Meta = LoopbackTxQueueMeta<<CC as DeviceIdContext<AnyDevice>>::WeakDeviceId, BC>,
337        > + ResourceCounterContext<<CC as DeviceIdContext<LoopbackDevice>>::DeviceId, DeviceCounters>
338        + DeviceIdContext<AnyDevice>,
339    BC: DeviceLayerTypes,
340{
341    fn send_meta<S>(
342        self,
343        core_ctx: &mut CC,
344        bindings_ctx: &mut BC,
345        body: S,
346    ) -> Result<(), SendFrameError<S>>
347    where
348        S: Serializer,
349        S::Buffer: BufferMut,
350    {
351        let Self { device_id, metadata } = self;
352        let tx_meta = LoopbackTxQueueMeta::default();
353        match metadata {
354            Some(EthernetHeaderParams { dest_addr, protocol }) => send_as_ethernet_frame_to_dst(
355                core_ctx,
356                bindings_ctx,
357                &device_id,
358                body,
359                protocol,
360                dest_addr,
361                LoopbackTxQueueMeta::default(),
362            ),
363            None => send_ethernet_frame(core_ctx, bindings_ctx, &device_id, body, tx_meta),
364        }
365    }
366}
367
368/// Sends an IP frame `packet` over `device_id`.
369pub fn send_ip_frame<CC, BC, I, S>(
370    core_ctx: &mut CC,
371    bindings_ctx: &mut BC,
372    device_id: &<CC as DeviceIdContext<LoopbackDevice>>::DeviceId,
373    destination: IpPacketDestination<I, &<CC as DeviceIdContext<AnyDevice>>::DeviceId>,
374    ip_layer_metadata: DeviceIpLayerMetadata<BC>,
375    packet: S,
376) -> Result<(), SendFrameError<S>>
377where
378    CC: TransmitQueueHandler<
379            LoopbackDevice,
380            BC,
381            Meta = LoopbackTxQueueMeta<<CC as DeviceIdContext<AnyDevice>>::WeakDeviceId, BC>,
382        > + ResourceCounterContext<<CC as DeviceIdContext<LoopbackDevice>>::DeviceId, DeviceCounters>
383        + DeviceIdContext<AnyDevice>,
384    BC: DeviceLayerTypes,
385    I: EthernetIpExt + BroadcastIpExt,
386    S: Serializer,
387    S::Buffer: BufferMut,
388{
389    core_ctx.increment_both(device_id, DeviceCounters::send_frame::<I>);
390
391    let target_device = match destination {
392        IpPacketDestination::Loopback(device) => Some(device.downgrade()),
393        IpPacketDestination::Broadcast(_)
394        | IpPacketDestination::Multicast(_)
395        | IpPacketDestination::Neighbor(_) => None,
396    };
397    send_as_ethernet_frame_to_dst(
398        core_ctx,
399        bindings_ctx,
400        device_id,
401        packet,
402        I::ETHER_TYPE,
403        LOOPBACK_MAC,
404        LoopbackTxQueueMeta { target_device, ip_layer_metadata },
405    )
406}
407
408fn send_as_ethernet_frame_to_dst<CC, BC, S>(
409    core_ctx: &mut CC,
410    bindings_ctx: &mut BC,
411    device_id: &<CC as DeviceIdContext<LoopbackDevice>>::DeviceId,
412    packet: S,
413    protocol: EtherType,
414    dst_mac: Mac,
415    meta: LoopbackTxQueueMeta<<CC as DeviceIdContext<AnyDevice>>::WeakDeviceId, BC>,
416) -> Result<(), SendFrameError<S>>
417where
418    CC: TransmitQueueHandler<
419            LoopbackDevice,
420            BC,
421            Meta = LoopbackTxQueueMeta<<CC as DeviceIdContext<AnyDevice>>::WeakDeviceId, BC>,
422        > + ResourceCounterContext<<CC as DeviceIdContext<LoopbackDevice>>::DeviceId, DeviceCounters>
423        + DeviceIdContext<AnyDevice>,
424    BC: DeviceLayerTypes,
425    S: Serializer,
426    S::Buffer: BufferMut,
427{
428    /// The minimum length of bodies of Ethernet frames sent over the loopback
429    /// device.
430    ///
431    /// Use zero since the frames are never sent out a physical device, so it
432    /// doesn't matter if they are shorter than would be required.
433    const MIN_BODY_LEN: usize = 0;
434
435    let frame = packet.encapsulate(EthernetFrameBuilder::new(
436        LOOPBACK_MAC,
437        dst_mac,
438        protocol,
439        MIN_BODY_LEN,
440    ));
441
442    send_ethernet_frame(core_ctx, bindings_ctx, device_id, frame, meta)
443        .map_err(|err| err.into_inner())
444}
445
446fn send_ethernet_frame<CC, BC, S>(
447    core_ctx: &mut CC,
448    bindings_ctx: &mut BC,
449    device_id: &<CC as DeviceIdContext<LoopbackDevice>>::DeviceId,
450    frame: S,
451    meta: LoopbackTxQueueMeta<<CC as DeviceIdContext<AnyDevice>>::WeakDeviceId, BC>,
452) -> Result<(), SendFrameError<S>>
453where
454    CC: TransmitQueueHandler<
455            LoopbackDevice,
456            BC,
457            Meta = LoopbackTxQueueMeta<<CC as DeviceIdContext<AnyDevice>>::WeakDeviceId, BC>,
458        > + ResourceCounterContext<<CC as DeviceIdContext<LoopbackDevice>>::DeviceId, DeviceCounters>
459        + DeviceIdContext<AnyDevice>,
460    S: Serializer,
461    S::Buffer: BufferMut,
462    BC: DeviceLayerTypes,
463{
464    core_ctx.increment_both(device_id, |counters: &DeviceCounters| &counters.send_total_frames);
465    match TransmitQueueHandler::<LoopbackDevice, _>::queue_tx_frame(
466        core_ctx,
467        bindings_ctx,
468        device_id,
469        meta,
470        frame,
471    ) {
472        Ok(()) => {
473            core_ctx.increment_both(device_id, |counters: &DeviceCounters| &counters.send_frame);
474            Ok(())
475        }
476        Err(TransmitQueueFrameError::NoQueue(err)) => {
477            unreachable!("loopback never fails to send a frame: {err:?}")
478        }
479        Err(TransmitQueueFrameError::QueueFull(serializer)) => {
480            core_ctx
481                .increment_both(device_id, |counters: &DeviceCounters| &counters.send_queue_full);
482            Err(SendFrameError { serializer, error: SendFrameErrorReason::QueueFull })
483        }
484        Err(TransmitQueueFrameError::SerializeError(err)) => {
485            core_ctx.increment_both(device_id, |counters: &DeviceCounters| {
486                &counters.send_serialize_error
487            });
488            Err(err.err_into())
489        }
490    }
491}
492
493impl DeviceReceiveFrameSpec for LoopbackDevice {
494    // Loopback never receives frames from bindings, so make it impossible to
495    // instantiate it.
496    type FrameMetadata<D> = Never;
497}