Skip to main content

netstack3_datagram/
tx_metadata.rs

1// Copyright 2026 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//! Datagram socket tx metadata definitions.
6
7use derivative::Derivative;
8use net_types::ip::{GenericOverIp, Ip};
9use netstack3_base::{ChecksumOffloadResult, WeakDeviceIdentifier};
10
11use crate::internal::datagram::{DatagramSocketSpec, IpExt};
12
13/// The tx metadata associated with a datagram socket.
14#[derive(Derivative, GenericOverIp)]
15#[generic_over_ip(I, Ip)]
16#[derivative(Debug(bound = ""))]
17pub struct TxMetadata<I: IpExt, D: WeakDeviceIdentifier, S: DatagramSocketSpec> {
18    socket: S::WeakSocketId<I, D>,
19    _send_token: S::SendToken,
20    checksum_offload_result: Option<ChecksumOffloadResult>,
21}
22
23impl<I: IpExt, D: WeakDeviceIdentifier, S: DatagramSocketSpec> TxMetadata<I, D, S> {
24    /// Creates a new `TxMetadata`.
25    pub(crate) fn new(socket: &S::SocketId<I, D>, send_token: S::SendToken) -> Self {
26        Self {
27            socket: S::downgrade_socket_id(socket),
28            _send_token: send_token,
29            checksum_offload_result: None,
30        }
31    }
32
33    /// Gets the socket from which the packet originates.
34    pub fn socket(&self) -> &S::WeakSocketId<I, D> {
35        &self.socket
36    }
37
38    /// Returns the checksum offload result.
39    pub fn checksum_offload_result(&self) -> Option<ChecksumOffloadResult> {
40        self.checksum_offload_result.clone()
41    }
42
43    /// Sets the checksum offload result.
44    pub fn set_checksum_offload_result(&mut self, result: Option<ChecksumOffloadResult>) {
45        self.checksum_offload_result = result;
46    }
47}
48
49#[cfg(any(test, feature = "testutils"))]
50impl<I: IpExt, D: WeakDeviceIdentifier, S: DatagramSocketSpec> PartialEq for TxMetadata<I, D, S> {
51    fn eq(&self, other: &Self) -> bool {
52        // Tx metadata is always a unique instance accompanying a frame and it's
53        // not copiable. So it may only be equal to another instance if they're
54        // the exact same object.
55        core::ptr::eq(self, other)
56    }
57}