2019-11-04 20:13:43 -08:00
|
|
|
//! The `packet` module defines data structures and methods to pull data from the network.
|
2022-01-02 09:10:32 -08:00
|
|
|
pub use solana_sdk::packet::{Meta, Packet, PacketFlags, PACKET_DATA_SIZE};
|
2021-12-03 09:00:31 -08:00
|
|
|
use {
|
|
|
|
crate::{cuda_runtime::PinnedVec, recycler::Recycler},
|
|
|
|
bincode::config::Options,
|
|
|
|
serde::Serialize,
|
|
|
|
std::net::SocketAddr,
|
|
|
|
};
|
2019-11-04 20:13:43 -08:00
|
|
|
|
|
|
|
pub const NUM_PACKETS: usize = 1024 * 8;
|
|
|
|
|
2021-03-03 10:23:05 -08:00
|
|
|
pub const PACKETS_PER_BATCH: usize = 128;
|
2019-11-04 20:13:43 -08:00
|
|
|
pub const NUM_RCVMMSGS: usize = 128;
|
|
|
|
|
2021-01-28 16:32:38 -08:00
|
|
|
#[derive(Debug, Default, Clone)]
|
2021-12-11 06:44:15 -08:00
|
|
|
pub struct PacketBatch {
|
2019-11-04 20:13:43 -08:00
|
|
|
pub packets: PinnedVec<Packet>,
|
|
|
|
}
|
|
|
|
|
2021-12-11 06:44:15 -08:00
|
|
|
pub type PacketBatchRecycler = Recycler<PinnedVec<Packet>>;
|
2019-11-04 20:13:43 -08:00
|
|
|
|
2021-12-11 06:44:15 -08:00
|
|
|
impl PacketBatch {
|
2019-11-04 20:13:43 -08:00
|
|
|
pub fn new(packets: Vec<Packet>) -> Self {
|
|
|
|
let packets = PinnedVec::from_vec(packets);
|
2019-12-11 11:58:40 -08:00
|
|
|
Self { packets }
|
2019-11-04 20:13:43 -08:00
|
|
|
}
|
|
|
|
|
2021-01-24 20:27:47 -08:00
|
|
|
pub fn with_capacity(capacity: usize) -> Self {
|
|
|
|
let packets = PinnedVec::with_capacity(capacity);
|
2021-12-11 06:44:15 -08:00
|
|
|
PacketBatch { packets }
|
2021-01-24 20:27:47 -08:00
|
|
|
}
|
|
|
|
|
2021-06-14 07:10:04 -07:00
|
|
|
pub fn new_unpinned_with_recycler(
|
2021-12-11 06:44:15 -08:00
|
|
|
recycler: PacketBatchRecycler,
|
2021-06-14 07:10:04 -07:00
|
|
|
size: usize,
|
|
|
|
name: &'static str,
|
|
|
|
) -> Self {
|
|
|
|
let mut packets = recycler.allocate(name);
|
|
|
|
packets.reserve(size);
|
2021-12-11 06:44:15 -08:00
|
|
|
PacketBatch { packets }
|
2021-06-14 07:10:04 -07:00
|
|
|
}
|
|
|
|
|
2021-12-11 06:44:15 -08:00
|
|
|
pub fn new_with_recycler(
|
|
|
|
recycler: PacketBatchRecycler,
|
|
|
|
size: usize,
|
|
|
|
name: &'static str,
|
|
|
|
) -> Self {
|
2021-04-07 08:15:38 -07:00
|
|
|
let mut packets = recycler.allocate(name);
|
|
|
|
packets.reserve_and_pin(size);
|
2021-12-11 06:44:15 -08:00
|
|
|
PacketBatch { packets }
|
2019-11-04 20:13:43 -08:00
|
|
|
}
|
2021-06-14 07:10:04 -07:00
|
|
|
|
2019-11-20 11:25:18 -08:00
|
|
|
pub fn new_with_recycler_data(
|
2021-12-11 06:44:15 -08:00
|
|
|
recycler: &PacketBatchRecycler,
|
2021-04-07 08:15:38 -07:00
|
|
|
name: &'static str,
|
2019-11-20 11:25:18 -08:00
|
|
|
mut packets: Vec<Packet>,
|
2021-04-07 08:15:38 -07:00
|
|
|
) -> Self {
|
2021-12-11 06:44:15 -08:00
|
|
|
let mut batch = Self::new_with_recycler(recycler.clone(), packets.len(), name);
|
|
|
|
batch.packets.append(&mut packets);
|
|
|
|
batch
|
2019-11-20 11:25:18 -08:00
|
|
|
}
|
2019-11-04 20:13:43 -08:00
|
|
|
|
2022-04-26 19:30:00 -07:00
|
|
|
pub fn new_unpinned_with_recycler_data_and_dests<T: Serialize>(
|
|
|
|
recycler: PacketBatchRecycler,
|
|
|
|
name: &'static str,
|
|
|
|
dests_and_data: &[(SocketAddr, T)],
|
|
|
|
) -> Self {
|
|
|
|
let mut batch =
|
|
|
|
PacketBatch::new_unpinned_with_recycler(recycler, dests_and_data.len(), name);
|
|
|
|
batch
|
|
|
|
.packets
|
|
|
|
.resize(dests_and_data.len(), Packet::default());
|
|
|
|
|
|
|
|
for ((addr, data), packet) in dests_and_data.iter().zip(batch.packets.iter_mut()) {
|
|
|
|
if !addr.ip().is_unspecified() && addr.port() != 0 {
|
|
|
|
if let Err(e) = Packet::populate_packet(packet, Some(addr), &data) {
|
|
|
|
// TODO: This should never happen. Instead the caller should
|
|
|
|
// break the payload into smaller messages, and here any errors
|
|
|
|
// should be propagated.
|
|
|
|
error!("Couldn't write to packet {:?}. Data skipped.", e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
trace!("Dropping packet, as destination is unknown");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
batch
|
|
|
|
}
|
|
|
|
|
2021-06-14 07:10:04 -07:00
|
|
|
pub fn new_unpinned_with_recycler_data(
|
2021-12-11 06:44:15 -08:00
|
|
|
recycler: &PacketBatchRecycler,
|
2021-06-14 07:10:04 -07:00
|
|
|
name: &'static str,
|
|
|
|
mut packets: Vec<Packet>,
|
|
|
|
) -> Self {
|
2021-12-11 06:44:15 -08:00
|
|
|
let mut batch = Self::new_unpinned_with_recycler(recycler.clone(), packets.len(), name);
|
|
|
|
batch.packets.append(&mut packets);
|
|
|
|
batch
|
2021-06-14 07:10:04 -07:00
|
|
|
}
|
|
|
|
|
2019-11-04 20:13:43 -08:00
|
|
|
pub fn set_addr(&mut self, addr: &SocketAddr) {
|
2021-12-11 06:44:15 -08:00
|
|
|
for p in self.packets.iter_mut() {
|
|
|
|
p.meta.set_addr(addr);
|
2019-11-04 20:13:43 -08:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 10:24:53 -08:00
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.packets.is_empty()
|
|
|
|
}
|
2019-11-04 20:13:43 -08:00
|
|
|
}
|
|
|
|
|
2022-04-25 12:50:26 -07:00
|
|
|
pub fn to_packet_batches<T: Serialize>(items: &[T], chunk_size: usize) -> Vec<PacketBatch> {
|
|
|
|
items
|
|
|
|
.chunks(chunk_size)
|
|
|
|
.map(|batch_items| {
|
|
|
|
let mut batch = PacketBatch::with_capacity(batch_items.len());
|
|
|
|
batch.packets.resize(batch_items.len(), Packet::default());
|
|
|
|
for (item, packet) in batch_items.iter().zip(batch.packets.iter_mut()) {
|
|
|
|
Packet::populate_packet(packet, None, item).expect("serialize request");
|
2022-01-24 05:35:47 -08:00
|
|
|
}
|
|
|
|
batch
|
|
|
|
})
|
|
|
|
.collect()
|
2019-11-06 10:52:30 -08:00
|
|
|
}
|
|
|
|
|
2021-01-28 16:32:38 -08:00
|
|
|
#[cfg(test)]
|
2022-04-25 12:50:26 -07:00
|
|
|
pub fn to_packet_batches_for_tests<T: Serialize>(items: &[T]) -> Vec<PacketBatch> {
|
|
|
|
to_packet_batches(items, NUM_PACKETS)
|
2019-11-06 10:52:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn limited_deserialize<T>(data: &[u8]) -> bincode::Result<T>
|
|
|
|
where
|
|
|
|
T: serde::de::DeserializeOwned,
|
|
|
|
{
|
2020-07-08 17:08:05 -07:00
|
|
|
bincode::options()
|
|
|
|
.with_limit(PACKET_DATA_SIZE as u64)
|
|
|
|
.with_fixint_encoding()
|
|
|
|
.allow_trailing_bytes()
|
2020-07-08 14:54:42 -07:00
|
|
|
.deserialize_from(data)
|
2019-11-06 10:52:30 -08:00
|
|
|
}
|
|
|
|
|
2019-11-04 20:13:43 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-12-03 09:00:31 -08:00
|
|
|
use {
|
|
|
|
super::*,
|
|
|
|
solana_sdk::{
|
|
|
|
hash::Hash,
|
|
|
|
signature::{Keypair, Signer},
|
|
|
|
system_transaction,
|
|
|
|
},
|
|
|
|
};
|
2019-11-04 20:13:43 -08:00
|
|
|
|
2019-11-06 10:52:30 -08:00
|
|
|
#[test]
|
2021-12-11 06:44:15 -08:00
|
|
|
fn test_to_packet_batches() {
|
2019-11-06 10:52:30 -08:00
|
|
|
let keypair = Keypair::new();
|
|
|
|
let hash = Hash::new(&[1; 32]);
|
|
|
|
let tx = system_transaction::transfer(&keypair, &keypair.pubkey(), 1, hash);
|
2021-12-11 06:44:15 -08:00
|
|
|
let rv = to_packet_batches_for_tests(&[tx.clone(); 1]);
|
2019-11-06 10:52:30 -08:00
|
|
|
assert_eq!(rv.len(), 1);
|
|
|
|
assert_eq!(rv[0].packets.len(), 1);
|
|
|
|
|
2020-05-15 09:35:43 -07:00
|
|
|
#[allow(clippy::useless_vec)]
|
2021-12-11 06:44:15 -08:00
|
|
|
let rv = to_packet_batches_for_tests(&vec![tx.clone(); NUM_PACKETS]);
|
2019-11-06 10:52:30 -08:00
|
|
|
assert_eq!(rv.len(), 1);
|
|
|
|
assert_eq!(rv[0].packets.len(), NUM_PACKETS);
|
|
|
|
|
2020-05-15 09:35:43 -07:00
|
|
|
#[allow(clippy::useless_vec)]
|
2021-12-11 06:44:15 -08:00
|
|
|
let rv = to_packet_batches_for_tests(&vec![tx; NUM_PACKETS + 1]);
|
2019-11-06 10:52:30 -08:00
|
|
|
assert_eq!(rv.len(), 2);
|
|
|
|
assert_eq!(rv[0].packets.len(), NUM_PACKETS);
|
|
|
|
assert_eq!(rv[1].packets.len(), 1);
|
|
|
|
}
|
2019-11-20 10:22:26 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_packets_pinning() {
|
2021-12-11 06:44:15 -08:00
|
|
|
let recycler = PacketBatchRecycler::default();
|
2019-11-20 10:22:26 -08:00
|
|
|
for i in 0..2 {
|
2021-12-11 06:44:15 -08:00
|
|
|
let _first_packets =
|
|
|
|
PacketBatch::new_with_recycler(recycler.clone(), i + 1, "first one");
|
2019-11-20 10:22:26 -08:00
|
|
|
}
|
|
|
|
}
|
2019-11-04 20:13:43 -08:00
|
|
|
}
|