|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | + |
| 3 | +//! Representation of a C `struct sock`. |
| 4 | +//! |
| 5 | +//! C header: [`include/net/sock.h`](srctree/include/net/sock.h) |
| 6 | +
|
| 7 | +#[cfg(CONFIG_RUST_TCP_ABSTRACTIONS)] |
| 8 | +use crate::net::tcp::{self, InetConnectionSock, TcpSock}; |
| 9 | +use crate::types::Opaque; |
| 10 | +use core::convert::TryFrom; |
| 11 | +use core::ptr::addr_of; |
| 12 | + |
| 13 | +/// Representation of a C `struct sock`. |
| 14 | +/// |
| 15 | +/// Not intended to be used directly by modules. Abstractions should provide a |
| 16 | +/// safe interface to only those operations that are OK to use for the module. |
| 17 | +/// |
| 18 | +/// # Invariants |
| 19 | +/// |
| 20 | +/// Referencing a `sock` using this struct asserts that you are in |
| 21 | +/// a context where all safe methods defined on this struct are indeed safe to |
| 22 | +/// call. |
| 23 | +#[repr(transparent)] |
| 24 | +pub(crate) struct Sock { |
| 25 | + sk: Opaque<bindings::sock>, |
| 26 | +} |
| 27 | + |
| 28 | +impl Sock { |
| 29 | + /// Returns a raw pointer to the wrapped `struct sock`. |
| 30 | + /// |
| 31 | + /// It is up to the caller to use it correctly. |
| 32 | + #[inline] |
| 33 | + pub(crate) fn raw_sk_mut(&mut self) -> *mut bindings::sock { |
| 34 | + self.sk.get() |
| 35 | + } |
| 36 | + |
| 37 | + /// Returns the sockets pacing rate in bytes per second. |
| 38 | + #[inline] |
| 39 | + pub(crate) fn sk_pacing_rate(&self) -> u64 { |
| 40 | + // NOTE: C uses READ_ONCE for this field, thus `read_volatile`. |
| 41 | + // SAFETY: The struct invariant ensures that we may access |
| 42 | + // this field without additional synchronization. It is a C unsigned |
| 43 | + // long so we can always convert it to a u64 without loss. |
| 44 | + unsafe { addr_of!((*self.sk.get()).sk_pacing_rate).read_volatile() as u64 } |
| 45 | + } |
| 46 | + |
| 47 | + /// Returns the sockets pacing status. |
| 48 | + #[inline] |
| 49 | + pub(crate) fn sk_pacing_status(&self) -> Result<Pacing, ()> { |
| 50 | + // SAFETY: The struct invariant ensures that we may access |
| 51 | + // this field without additional synchronization. |
| 52 | + unsafe { Pacing::try_from(*addr_of!((*self.sk.get()).sk_pacing_status)) } |
| 53 | + } |
| 54 | + |
| 55 | + /// Returns the sockets maximum GSO segment size to build. |
| 56 | + #[inline] |
| 57 | + pub(crate) fn sk_gso_max_size(&self) -> u32 { |
| 58 | + // SAFETY: The struct invariant ensures that we may access |
| 59 | + // this field without additional synchronization. It is an unsigned int |
| 60 | + // and we are guaranteed that this will always fit into a u32. |
| 61 | + unsafe { *addr_of!((*self.sk.get()).sk_gso_max_size) as u32 } |
| 62 | + } |
| 63 | + |
| 64 | + /// Returns the [`TcpSock`] that is containing the `Sock`. |
| 65 | + /// |
| 66 | + /// # Safety |
| 67 | + /// |
| 68 | + /// `sk` must be valid for `tcp_sk`. |
| 69 | + #[inline] |
| 70 | + #[cfg(CONFIG_RUST_TCP_ABSTRACTIONS)] |
| 71 | + pub(crate) unsafe fn tcp_sk<'a>(&'a self) -> &'a TcpSock { |
| 72 | + // SAFETY: |
| 73 | + // - Downcasting via `tcp_sk` is OK by the functions precondition. |
| 74 | + // - The cast is OK since `TcpSock` is transparent to `struct tcp_sock`. |
| 75 | + unsafe { &*(bindings::tcp_sk(self.sk.get()) as *const TcpSock) } |
| 76 | + } |
| 77 | + |
| 78 | + /// Returns the [`TcpSock`] that is containing the `Sock`. |
| 79 | + /// |
| 80 | + /// # Safety |
| 81 | + /// |
| 82 | + /// `sk` must be valid for `tcp_sk`. |
| 83 | + #[inline] |
| 84 | + #[cfg(CONFIG_RUST_TCP_ABSTRACTIONS)] |
| 85 | + pub(crate) unsafe fn tcp_sk_mut<'a>(&'a mut self) -> &'a mut TcpSock { |
| 86 | + // SAFETY: |
| 87 | + // - Downcasting via `tcp_sk` is OK by the functions precondition. |
| 88 | + // - The cast is OK since `TcpSock` is transparent to `struct tcp_sock`. |
| 89 | + unsafe { &mut *(bindings::tcp_sk(self.sk.get()) as *mut TcpSock) } |
| 90 | + } |
| 91 | + |
| 92 | + /// Returns the [`InetConnectionSock`] view of this socket. |
| 93 | + /// |
| 94 | + /// # Safety |
| 95 | + /// |
| 96 | + /// `sk` must be valid for `inet_csk`. |
| 97 | + #[inline] |
| 98 | + #[cfg(CONFIG_RUST_TCP_ABSTRACTIONS)] |
| 99 | + pub(crate) unsafe fn inet_csk<'a>(&'a self) -> &'a InetConnectionSock { |
| 100 | + // SAFETY: |
| 101 | + // - Calling `inet_csk` is OK by the functions precondition. |
| 102 | + // - The cast is OK since `InetConnectionSock` is transparent to |
| 103 | + // `struct inet_connection_sock`. |
| 104 | + unsafe { &*(bindings::inet_csk(self.sk.get()) as *const InetConnectionSock) } |
| 105 | + } |
| 106 | + |
| 107 | + /// Tests if the connection's sending rate is limited by the cwnd. |
| 108 | + /// |
| 109 | + /// # Safety |
| 110 | + /// |
| 111 | + /// `sk` must be valid for `tcp_is_cwnd_limited`. |
| 112 | + #[inline] |
| 113 | + #[cfg(CONFIG_RUST_TCP_ABSTRACTIONS)] |
| 114 | + pub(crate) unsafe fn tcp_is_cwnd_limited(&self) -> bool { |
| 115 | + // SAFETY: Calling `tcp_is_cwnd_limited` is OK by the functions |
| 116 | + // precondition. |
| 117 | + unsafe { bindings::tcp_is_cwnd_limited(self.sk.get()) } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/// The socket's pacing status. |
| 122 | +#[repr(u32)] |
| 123 | +#[allow(missing_docs)] |
| 124 | +pub enum Pacing { |
| 125 | + r#None = bindings::sk_pacing_SK_PACING_NONE, |
| 126 | + Needed = bindings::sk_pacing_SK_PACING_NEEDED, |
| 127 | + Fq = bindings::sk_pacing_SK_PACING_FQ, |
| 128 | +} |
| 129 | + |
| 130 | +// TODO: Replace with automatically generated code by bindgen when it becomes |
| 131 | +// possible. |
| 132 | +impl TryFrom<u32> for Pacing { |
| 133 | + type Error = (); |
| 134 | + |
| 135 | + fn try_from(val: u32) -> Result<Self, Self::Error> { |
| 136 | + match val { |
| 137 | + x if x == Pacing::r#None as u32 => Ok(Pacing::r#None), |
| 138 | + x if x == Pacing::Needed as u32 => Ok(Pacing::Needed), |
| 139 | + x if x == Pacing::Fq as u32 => Ok(Pacing::Fq), |
| 140 | + _ => Err(()), |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments