-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
l4.rs
311 lines (281 loc) · 10.9 KB
/
l4.rs
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright 2024 Cloudflare, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use log::warn;
use pingora_error::{
ErrorType::{AcceptError, BindError},
OrErr, Result,
};
use std::fs::Permissions;
use std::io::ErrorKind;
use std::net::{SocketAddr, ToSocketAddrs};
use std::os::unix::io::{AsRawFd, FromRawFd};
use std::os::unix::net::UnixListener as StdUnixListener;
use std::time::Duration;
use tokio::net::TcpSocket;
use crate::protocols::l4::listener::Listener;
pub use crate::protocols::l4::stream::Stream;
use crate::server::ListenFds;
const TCP_LISTENER_MAX_TRY: usize = 30;
const TCP_LISTENER_TRY_STEP: Duration = Duration::from_secs(1);
// TODO: configurable backlog
const LISTENER_BACKLOG: u32 = 65535;
/// Address for listening server, either TCP/UDS socket.
#[derive(Clone, Debug)]
pub enum ServerAddress {
Tcp(String, Option<TcpSocketOptions>),
Uds(String, Option<Permissions>),
}
impl AsRef<str> for ServerAddress {
fn as_ref(&self) -> &str {
match &self {
Self::Tcp(l, _) => l,
Self::Uds(l, _) => l,
}
}
}
/// TCP socket configuration options.
#[derive(Clone, Debug)]
pub struct TcpSocketOptions {
/// IPV6_V6ONLY flag (if true, limit socket to IPv6 communication only).
/// This is mostly useful when binding to `[::]`, which on most Unix distributions
/// will bind to both IPv4 and IPv6 addresses by default.
pub ipv6_only: bool,
// TODO: allow configuring reuseaddr, backlog, etc. from here?
}
mod uds {
use super::{OrErr, Result};
use crate::protocols::l4::listener::Listener;
use log::{debug, error};
use pingora_error::ErrorType::BindError;
use std::fs::{self, Permissions};
use std::io::ErrorKind;
use std::os::unix::fs::PermissionsExt;
use std::os::unix::net::UnixListener as StdUnixListener;
use tokio::net::UnixListener;
use super::LISTENER_BACKLOG;
pub(super) fn set_perms(path: &str, perms: Option<Permissions>) -> Result<()> {
// set read/write permissions for all users on the socket by default
let perms = perms.unwrap_or(Permissions::from_mode(0o666));
fs::set_permissions(path, perms).or_err_with(BindError, || {
format!("Fail to bind to {path}, could not set permissions")
})
}
pub(super) fn set_backlog(l: StdUnixListener, backlog: u32) -> Result<UnixListener> {
let socket: socket2::Socket = l.into();
// Note that we call listen on an already listening socket
// POSIX undefined but on Linux it will update the backlog size
socket
.listen(backlog as i32)
.or_err_with(BindError, || format!("listen() failed on {socket:?}"))?;
UnixListener::from_std(socket.into()).or_err(BindError, "Failed to convert to tokio socket")
}
pub(super) fn bind(addr: &str, perms: Option<Permissions>) -> Result<Listener> {
/*
We remove the filename/address in case there is a dangling reference.
"Binding to a socket with a filename creates a socket in the
filesystem that must be deleted by the caller when it is no
longer needed (using unlink(2))"
*/
match std::fs::remove_file(addr) {
Ok(()) => {
debug!("unlink {addr} done");
}
Err(e) => match e.kind() {
ErrorKind::NotFound => debug!("unlink {addr} not found: {e}"),
_ => error!("unlink {addr} failed: {e}"),
},
}
let listener_socket = UnixListener::bind(addr)
.or_err_with(BindError, || format!("Bind() failed on {addr}"))?;
set_perms(addr, perms)?;
let std_listener = listener_socket.into_std().unwrap();
Ok(set_backlog(std_listener, LISTENER_BACKLOG)?.into())
}
}
// currently, these options can only apply on sockets prior to calling bind()
fn apply_tcp_socket_options(sock: &TcpSocket, opt: Option<&TcpSocketOptions>) -> Result<()> {
let Some(opt) = opt else {
return Ok(());
};
let socket_ref = socket2::SockRef::from(sock);
socket_ref
.set_only_v6(opt.ipv6_only)
.or_err(BindError, "failed to set IPV6_V6ONLY")
}
fn from_raw_fd(address: &ServerAddress, fd: i32) -> Result<Listener> {
match address {
ServerAddress::Uds(addr, perm) => {
let std_listener = unsafe { StdUnixListener::from_raw_fd(fd) };
// set permissions just in case
uds::set_perms(addr, perm.clone())?;
Ok(uds::set_backlog(std_listener, LISTENER_BACKLOG)?.into())
}
ServerAddress::Tcp(_, _) => {
let std_listener_socket = unsafe { std::net::TcpStream::from_raw_fd(fd) };
let listener_socket = TcpSocket::from_std_stream(std_listener_socket);
// Note that we call listen on an already listening socket
// POSIX undefined but on Linux it will update the backlog size
Ok(listener_socket
.listen(LISTENER_BACKLOG)
.or_err_with(BindError, || format!("Listen() failed on {address:?}"))?
.into())
}
}
}
async fn bind_tcp(addr: &str, opt: Option<TcpSocketOptions>) -> Result<Listener> {
let mut try_count = 0;
loop {
let sock_addr = addr
.to_socket_addrs() // NOTE: this could invoke a blocking network lookup
.or_err_with(BindError, || format!("Invalid listen address {addr}"))?
.next() // take the first one for now
.unwrap(); // assume there is always at least one
let listener_socket = match sock_addr {
SocketAddr::V4(_) => TcpSocket::new_v4(),
SocketAddr::V6(_) => TcpSocket::new_v6(),
}
.or_err_with(BindError, || format!("fail to create address {sock_addr}"))?;
// NOTE: this is to preserve the current TcpListener::bind() behavior.
// We have a few test relying on this behavior to allow multiple identical
// test servers to coexist.
listener_socket
.set_reuseaddr(true)
.or_err(BindError, "fail to set_reuseaddr(true)")?;
apply_tcp_socket_options(&listener_socket, opt.as_ref())?;
match listener_socket.bind(sock_addr) {
Ok(()) => {
break Ok(listener_socket
.listen(LISTENER_BACKLOG)
.or_err(BindError, "bind() failed")?
.into())
}
Err(e) => {
if e.kind() != ErrorKind::AddrInUse {
break Err(e).or_err_with(BindError, || format!("bind() failed on {addr}"));
}
try_count += 1;
if try_count >= TCP_LISTENER_MAX_TRY {
break Err(e).or_err_with(BindError, || {
format!("bind() failed, after retries, {addr} still in use")
});
}
warn!("{addr} is in use, will try again");
tokio::time::sleep(TCP_LISTENER_TRY_STEP).await;
}
}
}
}
async fn bind(addr: &ServerAddress) -> Result<Listener> {
match addr {
ServerAddress::Uds(l, perm) => uds::bind(l, perm.clone()),
ServerAddress::Tcp(l, opt) => bind_tcp(l, opt.clone()).await,
}
}
pub struct ListenerEndpoint {
listen_addr: ServerAddress,
listener: Option<Listener>,
}
impl ListenerEndpoint {
pub fn new(listen_addr: ServerAddress) -> Self {
ListenerEndpoint {
listen_addr,
listener: None,
}
}
pub fn as_str(&self) -> &str {
self.listen_addr.as_ref()
}
pub async fn listen(&mut self, fds: Option<ListenFds>) -> Result<()> {
if self.listener.is_some() {
return Ok(());
}
let listener = if let Some(fds_table) = fds {
let addr = self.listen_addr.as_ref();
// consider make this mutex std::sync::Mutex or OnceCell
let mut table = fds_table.lock().await;
if let Some(fd) = table.get(addr.as_ref()) {
from_raw_fd(&self.listen_addr, *fd)?
} else {
// not found
let listener = bind(&self.listen_addr).await?;
table.add(addr.to_string(), listener.as_raw_fd());
listener
}
} else {
// not found, no fd table
bind(&self.listen_addr).await?
};
self.listener = Some(listener);
Ok(())
}
pub async fn accept(&mut self) -> Result<Stream> {
let Some(listener) = self.listener.as_mut() else {
// panic otherwise this thing dead loop
panic!("Need to call listen() first");
};
let mut stream = listener
.accept()
.await
.or_err(AcceptError, "Fail to accept()")?;
stream.set_nodelay()?;
Ok(stream)
}
}
#[cfg(test)]
mod test {
use super::*;
#[tokio::test]
async fn test_listen_tcp() {
let addr = "127.0.0.1:7100";
let mut listener = ListenerEndpoint::new(ServerAddress::Tcp(addr.into(), None));
listener.listen(None).await.unwrap();
tokio::spawn(async move {
// just try to accept once
listener.accept().await.unwrap();
});
tokio::net::TcpStream::connect(addr)
.await
.expect("can connect to TCP listener");
}
#[tokio::test]
async fn test_listen_tcp_ipv6_only() {
let sock_opt = Some(TcpSocketOptions { ipv6_only: true });
let mut listener = ListenerEndpoint::new(ServerAddress::Tcp("[::]:7101".into(), sock_opt));
listener.listen(None).await.unwrap();
tokio::spawn(async move {
// just try to accept twice
listener.accept().await.unwrap();
listener.accept().await.unwrap();
});
tokio::net::TcpStream::connect("127.0.0.1:7101")
.await
.expect_err("cannot connect to v4 addr");
tokio::net::TcpStream::connect("[::1]:7101")
.await
.expect("can connect to v6 addr");
}
#[tokio::test]
async fn test_listen_uds() {
let addr = "/tmp/test_listen_uds";
let mut listener = ListenerEndpoint::new(ServerAddress::Uds(addr.into(), None));
listener.listen(None).await.unwrap();
tokio::spawn(async move {
// just try to accept once
listener.accept().await.unwrap();
});
tokio::net::UnixStream::connect(addr)
.await
.expect("can connect to UDS listener");
}
}