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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! Socket load balancing with `SO_REUSEPORT`.
use ;
use BPF_PROG_TYPE_SK_REUSEPORT;
pub use SkReuseportAttachType;
use ;
use Error;
use crate::;
// `libc` exposes `SO_ATTACH_REUSEPORT_EBPF` on all architectures, but
// `SO_DETACH_REUSEPORT_BPF` is still commented out in libc's
// `src/unix/linux_like/linux/arch/{mips,powerpc,sparc}/mod.rs`.
// The values below are the asm-generic constants (52 and 68), which are
// correct for every architecture aya supports; sparc uses different values
// but aya does not target sparc. Both are defined locally to keep them
// consistent rather than mixing a libc constant with a hand-written one.
const SO_ATTACH_REUSEPORT_EBPF: c_int = 52;
const SO_DETACH_REUSEPORT_BPF: c_int = 68;
/// Error returned by reuseport socket option operations.
/// A program used to select a socket within a `SO_REUSEPORT` group.
///
/// [`SkReuseport`] programs are attached to sockets with `SO_REUSEPORT` set to
/// provide programmable socket selection when multiple sockets are listening
/// on the same port. The program decides which socket in the reuseport group
/// should handle an incoming connection or packet.
///
/// Attaching or detaching through any socket in the group affects the entire
/// `SO_REUSEPORT` group. Aya therefore does not expose a link-style attachment
/// handle for [`SkReuseport`] or automatically track group attachments for
/// cleanup. Dropping [`SkReuseport`] or [`crate::Ebpf`] does not detach the
/// program from the group; call [`SkReuseport::detach`] explicitly when you
/// want to remove it, or close all sockets in the reuseport group so the group
/// itself is destroyed.
///
/// # Minimum kernel version
///
/// The minimum kernel version required to use this feature is 4.19.
///
/// # Examples
///
/// ```no_run
/// # #[derive(Debug, thiserror::Error)]
/// # enum Error {
/// # #[error(transparent)]
/// # IO(#[from] std::io::Error),
/// # #[error(transparent)]
/// # Map(#[from] aya::maps::MapError),
/// # #[error(transparent)]
/// # Program(#[from] aya::programs::ProgramError),
/// # #[error(transparent)]
/// # Ebpf(#[from] aya::EbpfError)
/// # }
/// # let mut bpf = aya::Ebpf::load(&[])?;
/// use std::{
/// io,
/// net::{Ipv4Addr, SocketAddrV4, TcpListener},
/// os::fd::AsRawFd,
/// };
///
/// use aya::programs::SkReuseport;
/// use nix::sys::socket::{
/// AddressFamily, Backlog, SockFlag, SockType, SockaddrIn, bind, listen, setsockopt,
/// socket, sockopt::ReusePort,
/// };
///
/// // `SO_REUSEPORT` must be set before `bind(2)`. The kernel only adds a
/// // socket to a reuseport group during bind:
/// // - Bind requires both the existing and new sockets to have
/// // `SO_REUSEPORT` set; if either side lacks it, bind fails with
/// // `EADDRINUSE`.
/// // - Setting `SO_REUSEPORT` after bind is silently ignored; the socket is
/// // not added to any reuseport group.
/// // `std::net::TcpListener` does not expose that pre-bind socket setup step,
/// // so this example uses `nix` to create and configure the socket directly.
/// fn reuseport_listener(port: u16) -> io::Result<TcpListener> {
/// let fd = socket(
/// AddressFamily::Inet,
/// SockType::Stream,
/// SockFlag::empty(),
/// None,
/// )
/// .map_err(io::Error::other)?;
///
/// setsockopt(&fd, ReusePort, &true).map_err(io::Error::other)?;
///
/// let addr = SockaddrIn::from(SocketAddrV4::new(Ipv4Addr::LOCALHOST, port));
/// bind(fd.as_raw_fd(), &addr).map_err(io::Error::other)?;
/// listen(&fd, Backlog::MAXCONN).map_err(io::Error::other)?;
///
/// Ok(TcpListener::from(fd))
/// }
///
/// # #[cfg(target_os = "linux")] {
/// let listener = reuseport_listener(8080)?;
/// let program: &mut SkReuseport = bpf.program_mut("select_socket").unwrap().try_into()?;
/// program.load()?;
/// program.attach(&listener)?;
/// # }
/// # Ok::<(), Error>(())
/// ```