2019-11-04 20:13:43 -08:00
|
|
|
//! The `packet` module defines data structures and methods to pull data from the network.
|
2019-12-11 11:58:40 -08:00
|
|
|
use crate::{cuda_runtime::PinnedVec, recycler::Recycler};
|
2020-07-08 17:08:05 -07:00
|
|
|
use bincode::config::Options;
|
2019-11-06 10:52:30 -08:00
|
|
|
use serde::Serialize;
|
2019-11-04 20:13:43 -08:00
|
|
|
pub use solana_sdk::packet::{Meta, Packet, PACKET_DATA_SIZE};
|
2019-12-11 11:58:40 -08:00
|
|
|
use 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)]
|
2019-11-04 20:13:43 -08:00
|
|
|
pub struct Packets {
|
|
|
|
pub packets: PinnedVec<Packet>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type PacketsRecycler = Recycler<PinnedVec<Packet>>;
|
|
|
|
|
|
|
|
impl Packets {
|
|
|
|
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);
|
|
|
|
Packets { packets }
|
|
|
|
}
|
|
|
|
|
2021-02-24 00:15:58 -08:00
|
|
|
pub fn new_with_recycler(recycler: PacketsRecycler, size: usize) -> Option<Self> {
|
|
|
|
let maybe_packets = recycler.allocate();
|
|
|
|
maybe_packets.map(|mut packets| {
|
|
|
|
packets.reserve_and_pin(size);
|
|
|
|
Packets { packets }
|
|
|
|
})
|
2019-11-04 20:13:43 -08:00
|
|
|
}
|
2019-11-20 11:25:18 -08:00
|
|
|
pub fn new_with_recycler_data(
|
|
|
|
recycler: &PacketsRecycler,
|
|
|
|
mut packets: Vec<Packet>,
|
2021-02-24 00:15:58 -08:00
|
|
|
) -> Option<Self> {
|
|
|
|
Self::new_with_recycler(recycler.clone(), packets.len()).map(|mut vec| {
|
|
|
|
vec.packets.append(&mut packets);
|
|
|
|
vec
|
|
|
|
})
|
2019-11-20 11:25:18 -08:00
|
|
|
}
|
2019-11-04 20:13:43 -08:00
|
|
|
|
|
|
|
pub fn set_addr(&mut self, addr: &SocketAddr) {
|
|
|
|
for m in self.packets.iter_mut() {
|
|
|
|
m.meta.set_addr(&addr);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-11-06 10:52:30 -08:00
|
|
|
pub fn to_packets_chunked<T: Serialize>(xs: &[T], chunks: usize) -> Vec<Packets> {
|
|
|
|
let mut out = vec![];
|
|
|
|
for x in xs.chunks(chunks) {
|
2021-01-24 20:27:47 -08:00
|
|
|
let mut p = Packets::with_capacity(x.len());
|
2019-11-06 10:52:30 -08:00
|
|
|
p.packets.resize(x.len(), Packet::default());
|
|
|
|
for (i, o) in x.iter().zip(p.packets.iter_mut()) {
|
2019-11-14 10:24:53 -08:00
|
|
|
Packet::populate_packet(o, None, i).expect("serialize request");
|
2019-11-06 10:52:30 -08:00
|
|
|
}
|
|
|
|
out.push(p);
|
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2021-01-28 16:32:38 -08:00
|
|
|
#[cfg(test)]
|
2019-11-06 10:52:30 -08:00
|
|
|
pub fn to_packets<T: Serialize>(xs: &[T]) -> Vec<Packets> {
|
|
|
|
to_packets_chunked(xs, NUM_PACKETS)
|
|
|
|
}
|
|
|
|
|
2019-11-20 11:25:18 -08:00
|
|
|
pub fn to_packets_with_destination<T: Serialize>(
|
|
|
|
recycler: PacketsRecycler,
|
|
|
|
dests_and_data: &[(SocketAddr, T)],
|
|
|
|
) -> Packets {
|
2021-02-24 00:15:58 -08:00
|
|
|
let mut out = Packets::new_with_recycler(recycler, dests_and_data.len()).unwrap();
|
2019-11-14 10:24:53 -08:00
|
|
|
out.packets.resize(dests_and_data.len(), Packet::default());
|
|
|
|
for (dest_and_data, o) in dests_and_data.iter().zip(out.packets.iter_mut()) {
|
2020-02-18 16:14:20 -08:00
|
|
|
if !dest_and_data.0.ip().is_unspecified() && dest_and_data.0.port() != 0 {
|
|
|
|
if let Err(e) = Packet::populate_packet(o, Some(&dest_and_data.0), &dest_and_data.1) {
|
2020-11-19 08:38:01 -08:00
|
|
|
// TODO: This should never happen. Instead the caller should
|
|
|
|
// break the payload into smaller messages, and here any errors
|
|
|
|
// should be propagated.
|
2020-02-18 16:14:20 -08:00
|
|
|
error!("Couldn't write to packet {:?}. Data skipped.", e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
trace!("Dropping packet, as destination is unknown");
|
2019-11-14 10:24:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
use super::*;
|
2019-11-06 10:52:30 -08:00
|
|
|
use solana_sdk::hash::Hash;
|
2020-02-20 13:28:55 -08:00
|
|
|
use solana_sdk::signature::{Keypair, Signer};
|
2019-11-06 10:52:30 -08:00
|
|
|
use solana_sdk::system_transaction;
|
2019-11-04 20:13:43 -08:00
|
|
|
|
2019-11-06 10:52:30 -08:00
|
|
|
#[test]
|
|
|
|
fn test_to_packets() {
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
let hash = Hash::new(&[1; 32]);
|
|
|
|
let tx = system_transaction::transfer(&keypair, &keypair.pubkey(), 1, hash);
|
2020-05-15 09:35:43 -07:00
|
|
|
let rv = to_packets(&[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)]
|
2019-11-06 10:52:30 -08:00
|
|
|
let rv = to_packets(&vec![tx.clone(); NUM_PACKETS]);
|
|
|
|
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)]
|
|
|
|
let rv = to_packets(&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-02-24 00:15:58 -08:00
|
|
|
let recycler = PacketsRecycler::new_without_limit("");
|
2019-11-20 10:22:26 -08:00
|
|
|
for i in 0..2 {
|
2021-02-24 00:15:58 -08:00
|
|
|
let _first_packets = Packets::new_with_recycler(recycler.clone(), i + 1);
|
2019-11-20 10:22:26 -08:00
|
|
|
}
|
|
|
|
}
|
2019-11-04 20:13:43 -08:00
|
|
|
}
|