1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! Socket load balancing with `SO_REUSEPORT`.
use crate::{EbpfContext, bindings::sk_reuseport_md, cty::c_void, programs::SockContext};
pub struct SkReuseportContext {
pub md: *mut sk_reuseport_md,
}
impl SkReuseportContext {
pub const fn new(md: *mut sk_reuseport_md) -> Self {
Self { md }
}
#[inline]
fn md(&self) -> &sk_reuseport_md {
unsafe { &*self.md }
}
#[inline]
fn sk_ptr(&self) -> *mut crate::bindings::bpf_sock {
unsafe { self.md().__bindgen_anon_3.sk }
}
#[inline]
fn migrating_sk_ptr(&self) -> *mut crate::bindings::bpf_sock {
unsafe { self.md().__bindgen_anon_4.migrating_sk }
}
/// Returns the start of the directly accessible data.
#[inline]
pub fn data(&self) -> usize {
unsafe { self.md().__bindgen_anon_1.data as usize }
}
/// Returns the end of the directly accessible data.
#[inline]
pub fn data_end(&self) -> usize {
unsafe { self.md().__bindgen_anon_2.data_end as usize }
}
/// Returns the total packet length.
#[inline]
pub fn len(&self) -> usize {
self.md().len as usize
}
/// Returns whether the packet length is zero.
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Returns the Ethernet protocol from the packet in network byte order.
#[inline]
pub fn eth_protocol(&self) -> u32 {
self.md().eth_protocol
}
/// Returns the IP protocol.
#[inline]
pub fn ip_protocol(&self) -> u32 {
self.md().ip_protocol
}
/// Returns whether the socket is bound to an INANY address.
#[inline]
pub fn bind_inany(&self) -> u32 {
self.md().bind_inany
}
/// Returns the hash of the packet's 4-tuple.
#[inline]
pub fn hash(&self) -> u32 {
self.md().hash
}
/// Returns a socket from the current `SO_REUSEPORT` group.
///
/// This socket can be used to inspect the local listener that is being
/// considered for selection.
///
/// Available on Linux 5.14 and later. On earlier kernels the verifier
/// rejects programs that access this field.
#[inline]
pub fn sk(&self) -> SockContext {
SockContext::new(self.sk_ptr())
}
/// Returns the socket being migrated, if the program is running in a
/// migrate path.
///
/// When this returns `None`, the program is handling initial socket
/// selection for a new packet or connection.
///
/// Available on Linux 5.14 and later. On earlier kernels the verifier
/// rejects programs that access this field.
#[inline]
pub fn migrating_sk(&self) -> Option<SockContext> {
let sock = self.migrating_sk_ptr();
if sock.is_null() {
None
} else {
Some(SockContext::new(sock))
}
}
}
impl EbpfContext for SkReuseportContext {
fn as_ptr(&self) -> *mut c_void {
self.md.cast()
}
}