dndx_forked_unix_udp_sock/lib.rs
1//! Uniform interface to send/recv UDP packets with ECN information.
2use std::{
3 net::{IpAddr, Ipv6Addr, SocketAddr},
4 sync::atomic::{AtomicUsize, Ordering},
5};
6
7pub use crate::cmsg::{AsPtr, EcnCodepoint, Source, Transmit};
8
9mod cmsg;
10
11#[path = "unix.rs"]
12mod imp;
13
14pub use imp::{sync, UdpSocket};
15pub mod framed;
16
17/// Number of UDP packets to send/receive at a time
18pub const BATCH_SIZE: usize = imp::BATCH_SIZE;
19
20/// The capabilities a UDP socket suppports on a certain platform
21#[derive(Debug)]
22pub struct UdpState {
23 max_gso_segments: AtomicUsize,
24 gro_segments: usize,
25}
26
27impl UdpState {
28 pub fn new() -> Self {
29 imp::udp_state()
30 }
31
32 /// The maximum amount of segments which can be transmitted if a platform
33 /// supports Generic Send Offload (GSO).
34 ///
35 /// This is 1 if the platform doesn't support GSO. Subject to change if errors are detected
36 /// while using GSO.
37 #[inline]
38 pub fn max_gso_segments(&self) -> usize {
39 self.max_gso_segments.load(Ordering::Relaxed)
40 }
41
42 /// The number of segments to read when GRO is enabled. Used as a factor to
43 /// compute the receive buffer size.
44 ///
45 /// Returns 1 if the platform doesn't support GRO.
46 #[inline]
47 pub fn gro_segments(&self) -> usize {
48 self.gro_segments
49 }
50}
51
52impl Default for UdpState {
53 fn default() -> Self {
54 Self::new()
55 }
56}
57
58/// Metadata about received packet. Includes which address we
59/// recv'd from, how many bytes, ecn codepoints, what the
60/// destination IP used was and what interface index was used.
61#[derive(Debug, Copy, Clone)]
62pub struct RecvMeta {
63 /// address we received datagram on
64 pub addr: SocketAddr,
65 /// length of datagram
66 pub len: usize,
67 /// received datagram stride
68 pub stride: usize,
69 /// ECN codepoint
70 pub ecn: Option<EcnCodepoint>,
71 /// The destination IP address for this datagram (ipi_addr)
72 pub dst_ip: Option<IpAddr>,
73 /// The destination local IP address for this datagram (ipi_spec_dst)
74 pub dst_local_ip: Option<IpAddr>,
75 /// interface index that datagram was received on
76 pub ifindex: u32,
77}
78
79impl Default for RecvMeta {
80 /// Constructs a value with arbitrary fields, intended to be overwritten
81 fn default() -> Self {
82 Self {
83 addr: SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0),
84 len: 0,
85 stride: 0,
86 ecn: None,
87 dst_ip: None,
88 dst_local_ip: None,
89 ifindex: 0,
90 }
91 }
92}