forked from aws/s2n-quic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdp.rs
183 lines (153 loc) · 5.12 KB
/
xdp.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use crate::{io::tokio::Clock, socket::stats};
use s2n_quic_core::{
endpoint::Endpoint, inet::SocketAddress, io::event_loop::EventLoop, path::mtu,
};
pub use s2n_quic_core::{
io::rx,
sync::{spsc, worker},
xdp::path::Tuple as PathHandle,
};
pub use s2n_quic_xdp::*;
use std::io::ErrorKind;
// export the encoder configuration for writing packets
pub mod encoder {
pub use s2n_quic_core::xdp::encoder::State as Config;
}
// export socket types and helpers
pub mod socket {
pub use crate::socket::stats;
pub use s2n_quic_xdp::socket::*;
/// Binds a UDP socket to a particular interface and socket address
pub fn bind_udp(
interface: &::std::ffi::CStr,
addr: ::std::net::SocketAddr,
) -> ::std::io::Result<::std::net::UdpSocket> {
let only_v6 = false;
let socket = crate::syscall::udp_socket(addr, only_v6)?;
// associate the socket with a single interface
crate::syscall::bind_to_interface(&socket, interface)?;
socket.bind(&addr.into())?;
Ok(socket.into())
}
}
pub mod tx {
use crate::socket::stats;
pub use s2n_quic_core::io::tx::*;
pub fn channel(
socket: ::std::net::UdpSocket,
stats: stats::Sender,
) -> (
impl Tx<PathHandle = crate::message::default::Handle>,
impl core::future::Future<Output = ::std::io::Result<()>>,
) {
// Initial packets don't need to be bigger than the minimum
let max_mtu = s2n_quic_core::path::MaxMtu::MIN;
// It's unlikely the initial packets will utilize GSO, so just disable it
let gso = crate::features::Gso::default();
gso.disable();
// compute the payload size for each message from the MaxMtu
let payload_len = {
let max_mtu: u16 = max_mtu.into();
max_mtu as u32
};
// 512Kb
let tx_buffer_size: u32 = 1 << 19;
let entries = tx_buffer_size / payload_len;
let entries = if entries.is_power_of_two() {
entries
} else {
// round up to the nearest power of two, since the ring buffers require it
entries.next_power_of_two()
};
let mut producers = vec![];
let (producer, consumer) = crate::socket::ring::pair(entries, payload_len);
producers.push(producer);
// spawn a task that actually flushes the ring buffer to the socket
let cooldown = s2n_quic_core::task::cooldown::Cooldown::default();
let task = crate::io::tokio::task::tx(socket, consumer, gso.clone(), cooldown, stats);
// construct the TX side for the endpoint event loop
let io = crate::socket::io::tx::Tx::new(producers, gso, max_mtu);
(io, task)
}
}
impl From<PathHandle> for crate::message::msg::Handle {
#[inline]
fn from(handle: PathHandle) -> Self {
let remote_address = handle.remote_address.into();
let local_address = handle.local_address.into();
crate::message::msg::Handle {
remote_address,
local_address,
}
}
}
impl From<&PathHandle> for crate::message::msg::Handle {
#[inline]
fn from(handle: &PathHandle) -> Self {
let remote_address = handle.remote_address.into();
let local_address = handle.local_address.into();
crate::message::msg::Handle {
remote_address,
local_address,
}
}
}
mod builder;
pub use builder::Builder;
pub struct Provider<Rx, Tx> {
rx: Rx,
tx: Tx,
mtu_config_builder: mtu::Builder,
stats: stats::Receiver,
handle: Option<tokio::runtime::Handle>,
}
impl Provider<(), ()> {
/// Creates a builder to construct an XDP provider
pub fn builder() -> Builder {
Builder::default()
}
}
impl<Rx, Tx> Provider<Rx, Tx>
where
Rx: 'static + rx::Rx + Send,
Tx: 'static + tx::Tx<PathHandle = Rx::PathHandle> + Send,
{
pub fn start<E: Endpoint<PathHandle = Rx::PathHandle>>(
self,
mut endpoint: E,
) -> std::io::Result<(tokio::task::JoinHandle<()>, SocketAddress)> {
let Self {
tx,
rx,
mtu_config_builder,
handle,
stats,
} = self;
let mtu_config = mtu_config_builder
.build()
.map_err(|err| std::io::Error::new(ErrorKind::InvalidInput, format!("{err}")))?;
// tell the endpoint what our MTU is
endpoint.set_mtu_config(mtu_config);
// create a tokio clock
let clock = Clock::new();
// create an event loop
let event_loop = EventLoop {
endpoint,
clock,
rx,
tx,
cooldown: crate::io::tokio::cooldown("ENDPOINT"),
stats,
};
let local_addr = SocketAddress::default();
// spawn the event loop on to the tokio handle
let task = if let Some(handle) = handle {
handle.spawn(event_loop.start(local_addr))
} else {
tokio::spawn(event_loop.start(local_addr))
};
Ok((task, local_addr))
}
}