1

Embed profiling macros into crates

This commit is contained in:
Evan Pratten 2023-08-04 21:22:57 -04:00
parent 4caa610a80
commit 6c0e6b2341
12 changed files with 40 additions and 3 deletions

View File

@ -17,6 +17,7 @@ log = "^0.4"
libc = "^0.2" libc = "^0.2"
ioctl-gen = "^0.1.1" ioctl-gen = "^0.1.1"
rtnetlink = "^0.13.0" rtnetlink = "^0.13.0"
profiling = "1.0.9"
[dev-dependencies] [dev-dependencies]
env_logger = "0.10.0" env_logger = "0.10.0"

View File

@ -111,16 +111,19 @@ impl IntoRawFd for Tun {
} }
impl Read for Tun { impl Read for Tun {
#[profiling::function]
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.fd.read(buf) self.fd.read(buf)
} }
} }
impl Write for Tun { impl Write for Tun {
#[profiling::function]
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.fd.write(buf) self.fd.write(buf)
} }
#[profiling::function]
fn flush(&mut self) -> std::io::Result<()> { fn flush(&mut self) -> std::io::Result<()> {
self.fd.flush() self.fd.flush()
} }

View File

@ -16,4 +16,5 @@ categories = []
log = "^0.4" log = "^0.4"
rustc-hash = "1.1.0" rustc-hash = "1.1.0"
thiserror = "^1.0.44" thiserror = "^1.0.44"
ipnet = "^2.8.0" ipnet = "^2.8.0"
profiling = "1.0.9"

View File

@ -22,29 +22,33 @@ where
} }
/// Insert a new mapping into the `BiHashMap` /// Insert a new mapping into the `BiHashMap`
#[profiling::function]
pub fn insert(&mut self, left: Left, right: Right) { pub fn insert(&mut self, left: Left, right: Right) {
self.left_to_right.insert(left.clone(), right.clone()); self.left_to_right.insert(left.clone(), right.clone());
self.right_to_left.insert(right, left); self.right_to_left.insert(right, left);
} }
/// Get the right value for a given left value /// Get the right value for a given left value
#[profiling::function]
pub fn get_right(&self, left: &Left) -> Option<&Right> { pub fn get_right(&self, left: &Left) -> Option<&Right> {
self.left_to_right.get(left) self.left_to_right.get(left)
} }
/// Get the left value for a given right value /// Get the left value for a given right value
#[profiling::function]
pub fn get_left(&self, right: &Right) -> Option<&Left> { pub fn get_left(&self, right: &Right) -> Option<&Left> {
self.right_to_left.get(right) self.right_to_left.get(right)
} }
/// Remove a mapping from the `BiHashMap` /// Remove a mapping from the `BiHashMap`
#[profiling::function]
pub fn remove(&mut self, left: &Left, right: &Right) { pub fn remove(&mut self, left: &Left, right: &Right) {
self.left_to_right.remove(left); self.left_to_right.remove(left);
self.right_to_left.remove(right); self.right_to_left.remove(right);
} }
/// Remove a mapping from the `BiHashMap` by left value /// Remove a mapping from the `BiHashMap` by left value
#[allow(dead_code)] #[profiling::function]
pub fn remove_left(&mut self, left: &Left) { pub fn remove_left(&mut self, left: &Left) {
if let Some(right) = self.left_to_right.remove(left) { if let Some(right) = self.left_to_right.remove(left) {
self.right_to_left.remove(&right); self.right_to_left.remove(&right);
@ -52,7 +56,7 @@ where
} }
/// Remove a mapping from the `BiHashMap` by right value /// Remove a mapping from the `BiHashMap` by right value
#[allow(dead_code)] #[profiling::function]
pub fn remove_right(&mut self, right: &Right) { pub fn remove_right(&mut self, right: &Right) {
if let Some(left) = self.right_to_left.remove(right) { if let Some(left) = self.right_to_left.remove(right) {
self.left_to_right.remove(&left); self.left_to_right.remove(&left);
@ -60,11 +64,13 @@ where
} }
/// Get the total number of mappings in the `BiHashMap` /// Get the total number of mappings in the `BiHashMap`
#[profiling::function]
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.left_to_right.len() self.left_to_right.len()
} }
/// Check if the `BiHashMap` is empty /// Check if the `BiHashMap` is empty
#[profiling::function]
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.left_to_right.is_empty() self.left_to_right.is_empty()
} }

View File

@ -25,6 +25,7 @@ impl CrossProtocolNetworkAddressTable {
} }
/// Prune all old mappings /// Prune all old mappings
#[profiling::function]
pub fn prune(&mut self) { pub fn prune(&mut self) {
log::trace!("Pruning old network address mappings"); log::trace!("Pruning old network address mappings");
@ -54,6 +55,7 @@ impl CrossProtocolNetworkAddressTable {
} }
/// Insert a new indefinite mapping /// Insert a new indefinite mapping
#[profiling::function]
pub fn insert_indefinite(&mut self, ipv4: Ipv4Addr, ipv6: Ipv6Addr) { pub fn insert_indefinite(&mut self, ipv4: Ipv4Addr, ipv6: Ipv6Addr) {
self.prune(); self.prune();
let (ipv4, ipv6) = (ipv4.into(), ipv6.into()); let (ipv4, ipv6) = (ipv4.into(), ipv6.into());
@ -62,6 +64,7 @@ impl CrossProtocolNetworkAddressTable {
} }
/// Insert a new mapping with a finite time-to-live /// Insert a new mapping with a finite time-to-live
#[profiling::function]
pub fn insert(&mut self, ipv4: Ipv4Addr, ipv6: Ipv6Addr, duration: Duration) { pub fn insert(&mut self, ipv4: Ipv4Addr, ipv6: Ipv6Addr, duration: Duration) {
self.prune(); self.prune();
let (ipv4, ipv6) = (ipv4.into(), ipv6.into()); let (ipv4, ipv6) = (ipv4.into(), ipv6.into());
@ -77,6 +80,7 @@ impl CrossProtocolNetworkAddressTable {
/// Get the IPv6 address for a given IPv4 address /// Get the IPv6 address for a given IPv4 address
#[must_use] #[must_use]
#[profiling::function]
pub fn get_ipv6(&self, ipv4: &Ipv4Addr) -> Option<Ipv6Addr> { pub fn get_ipv6(&self, ipv4: &Ipv4Addr) -> Option<Ipv6Addr> {
self.addr_map self.addr_map
.get_right(&(*ipv4).into()) .get_right(&(*ipv4).into())
@ -85,6 +89,7 @@ impl CrossProtocolNetworkAddressTable {
/// Get the IPv4 address for a given IPv6 address /// Get the IPv4 address for a given IPv6 address
#[must_use] #[must_use]
#[profiling::function]
pub fn get_ipv4(&self, ipv6: &Ipv6Addr) -> Option<Ipv4Addr> { pub fn get_ipv4(&self, ipv6: &Ipv6Addr) -> Option<Ipv4Addr> {
self.addr_map self.addr_map
.get_left(&(*ipv6).into()) .get_left(&(*ipv6).into())
@ -93,12 +98,14 @@ impl CrossProtocolNetworkAddressTable {
/// Get the number of mappings in the table /// Get the number of mappings in the table
#[must_use] #[must_use]
#[profiling::function]
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.addr_map.len() self.addr_map.len()
} }
/// Check if the table is empty /// Check if the table is empty
#[must_use] #[must_use]
#[profiling::function]
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.addr_map.is_empty() self.addr_map.is_empty()
} }
@ -135,6 +142,7 @@ impl CrossProtocolNetworkAddressTableWithIpv4Pool {
} }
/// Insert a new static mapping /// Insert a new static mapping
#[profiling::function]
pub fn insert_static(&mut self, ipv4: Ipv4Addr, ipv6: Ipv6Addr) -> Result<(), Error> { pub fn insert_static(&mut self, ipv4: Ipv4Addr, ipv6: Ipv6Addr) -> Result<(), Error> {
if !self.pool.iter().any(|prefix| prefix.contains(&ipv4)) { if !self.pool.iter().any(|prefix| prefix.contains(&ipv4)) {
return Err(Error::InvalidIpv4Address(ipv4)); return Err(Error::InvalidIpv4Address(ipv4));
@ -144,6 +152,7 @@ impl CrossProtocolNetworkAddressTableWithIpv4Pool {
} }
/// Gets the IPv4 address for a given IPv6 address or inserts a new mapping if one does not exist (if possible) /// Gets the IPv4 address for a given IPv6 address or inserts a new mapping if one does not exist (if possible)
#[profiling::function]
pub fn get_or_create_ipv4(&mut self, ipv6: &Ipv6Addr) -> Result<Ipv4Addr, Error> { pub fn get_or_create_ipv4(&mut self, ipv6: &Ipv6Addr) -> Result<Ipv4Addr, Error> {
// Return the known mapping if it exists // Return the known mapping if it exists
if let Some(ipv4) = self.table.get_ipv4(ipv6) { if let Some(ipv4) = self.table.get_ipv4(ipv6) {
@ -172,6 +181,7 @@ impl CrossProtocolNetworkAddressTableWithIpv4Pool {
/// Gets the IPv6 address for a given IPv4 address if it exists /// Gets the IPv6 address for a given IPv4 address if it exists
#[must_use] #[must_use]
#[profiling::function]
pub fn get_ipv6(&self, ipv4: &Ipv4Addr) -> Option<Ipv6Addr> { pub fn get_ipv6(&self, ipv4: &Ipv4Addr) -> Option<Ipv6Addr> {
self.table.get_ipv6(ipv4) self.table.get_ipv6(ipv4)
} }

View File

@ -19,6 +19,7 @@ impl NetworkAddressTable {
} }
/// Prune all old mappings /// Prune all old mappings
#[profiling::function]
pub fn prune(&mut self) { pub fn prune(&mut self) {
log::trace!("Pruning old network address mappings"); log::trace!("Pruning old network address mappings");
@ -48,6 +49,7 @@ impl NetworkAddressTable {
} }
/// Insert a new indefinite mapping /// Insert a new indefinite mapping
#[profiling::function]
pub fn insert_indefinite(&mut self, left: Ipv4Addr, right: Ipv4Addr) { pub fn insert_indefinite(&mut self, left: Ipv4Addr, right: Ipv4Addr) {
self.prune(); self.prune();
let (left, right) = (left.into(), right.into()); let (left, right) = (left.into(), right.into());
@ -56,6 +58,7 @@ impl NetworkAddressTable {
} }
/// Insert a new mapping with a finite time-to-live /// Insert a new mapping with a finite time-to-live
#[profiling::function]
pub fn insert(&mut self, left: Ipv4Addr, right: Ipv4Addr, duration: Duration) { pub fn insert(&mut self, left: Ipv4Addr, right: Ipv4Addr, duration: Duration) {
self.prune(); self.prune();
let (left, right) = (left.into(), right.into()); let (left, right) = (left.into(), right.into());
@ -71,6 +74,7 @@ impl NetworkAddressTable {
/// Get the right value for a given left value /// Get the right value for a given left value
#[must_use] #[must_use]
#[profiling::function]
pub fn get_right(&self, left: &Ipv4Addr) -> Option<Ipv4Addr> { pub fn get_right(&self, left: &Ipv4Addr) -> Option<Ipv4Addr> {
self.addr_map self.addr_map
.get_right(&(*left).into()) .get_right(&(*left).into())
@ -79,6 +83,7 @@ impl NetworkAddressTable {
/// Get the left value for a given right value /// Get the left value for a given right value
#[must_use] #[must_use]
#[profiling::function]
pub fn get_left(&self, right: &Ipv4Addr) -> Option<Ipv4Addr> { pub fn get_left(&self, right: &Ipv4Addr) -> Option<Ipv4Addr> {
self.addr_map self.addr_map
.get_left(&(*right).into()) .get_left(&(*right).into())

View File

@ -21,3 +21,4 @@ protomask-metrics = { path = "../protomask-metrics", optional = true }
log = "^0.4" log = "^0.4"
pnet = "0.34.0" pnet = "0.34.0"
thiserror = "^1.0.44" thiserror = "^1.0.44"
profiling = "1.0.9"

View File

@ -15,6 +15,7 @@ mod type_code;
/// Translate an ICMP packet to ICMPv6. This will make a best guess at the ICMPv6 type and code since there is no 1:1 mapping. /// Translate an ICMP packet to ICMPv6. This will make a best guess at the ICMPv6 type and code since there is no 1:1 mapping.
#[allow(clippy::deprecated_cfg_attr)] #[allow(clippy::deprecated_cfg_attr)]
#[profiling::function]
pub fn translate_icmp_to_icmpv6( pub fn translate_icmp_to_icmpv6(
icmp_packet: &[u8], icmp_packet: &[u8],
new_source: Ipv6Addr, new_source: Ipv6Addr,
@ -101,6 +102,7 @@ pub fn translate_icmp_to_icmpv6(
} }
/// Translate an ICMPv6 packet to ICMP. This will make a best guess at the ICMP type and code since there is no 1:1 mapping. /// Translate an ICMPv6 packet to ICMP. This will make a best guess at the ICMP type and code since there is no 1:1 mapping.
#[profiling::function]
pub fn translate_icmpv6_to_icmp( pub fn translate_icmpv6_to_icmp(
icmpv6_packet: &[u8], icmpv6_packet: &[u8],
new_source: Ipv4Addr, new_source: Ipv4Addr,

View File

@ -9,6 +9,7 @@ use crate::error::{Error, Result};
/// Best effort translation from an ICMP type and code to an ICMPv6 type and code /// Best effort translation from an ICMP type and code to an ICMPv6 type and code
#[allow(clippy::deprecated_cfg_attr)] #[allow(clippy::deprecated_cfg_attr)]
#[profiling::function]
pub fn translate_type_and_code_4_to_6( pub fn translate_type_and_code_4_to_6(
icmp_type: IcmpType, icmp_type: IcmpType,
icmp_code: IcmpCode, icmp_code: IcmpCode,
@ -58,6 +59,7 @@ pub fn translate_type_and_code_4_to_6(
/// Best effort translation from an ICMPv6 type and code to an ICMP type and code /// Best effort translation from an ICMPv6 type and code to an ICMP type and code
#[allow(clippy::deprecated_cfg_attr)] #[allow(clippy::deprecated_cfg_attr)]
#[profiling::function]
pub fn translate_type_and_code_6_to_4( pub fn translate_type_and_code_6_to_4(
icmp_type: Icmpv6Type, icmp_type: Icmpv6Type,
icmp_code: Icmpv6Code, icmp_code: Icmpv6Code,

View File

@ -15,6 +15,7 @@ use pnet::packet::{
use std::net::{Ipv4Addr, Ipv6Addr}; use std::net::{Ipv4Addr, Ipv6Addr};
/// Translates an IPv4 packet into an IPv6 packet. The packet payload will be translated recursively as needed. /// Translates an IPv4 packet into an IPv6 packet. The packet payload will be translated recursively as needed.
#[profiling::function]
pub fn translate_ipv4_to_ipv6( pub fn translate_ipv4_to_ipv6(
ipv4_packet: &[u8], ipv4_packet: &[u8],
new_source: Ipv6Addr, new_source: Ipv6Addr,
@ -92,6 +93,7 @@ pub fn translate_ipv4_to_ipv6(
} }
/// Translates an IPv6 packet into an IPv4 packet. The packet payload will be translated recursively as needed. /// Translates an IPv6 packet into an IPv4 packet. The packet payload will be translated recursively as needed.
#[profiling::function]
pub fn translate_ipv6_to_ipv4( pub fn translate_ipv6_to_ipv4(
ipv6_packet: &[u8], ipv6_packet: &[u8],
new_source: Ipv4Addr, new_source: Ipv4Addr,

View File

@ -5,6 +5,7 @@ use pnet::packet::tcp::{self, MutableTcpPacket, TcpPacket};
use crate::error::{Error, Result}; use crate::error::{Error, Result};
/// Re-calculates a TCP packet's checksum with a new IPv6 pseudo-header. /// Re-calculates a TCP packet's checksum with a new IPv6 pseudo-header.
#[profiling::function]
pub fn recalculate_tcp_checksum_ipv6( pub fn recalculate_tcp_checksum_ipv6(
tcp_packet: &[u8], tcp_packet: &[u8],
new_source: Ipv6Addr, new_source: Ipv6Addr,
@ -48,6 +49,7 @@ pub fn recalculate_tcp_checksum_ipv6(
} }
/// Re-calculates a TCP packet's checksum with a new IPv4 pseudo-header. /// Re-calculates a TCP packet's checksum with a new IPv4 pseudo-header.
#[profiling::function]
pub fn recalculate_tcp_checksum_ipv4( pub fn recalculate_tcp_checksum_ipv4(
tcp_packet: &[u8], tcp_packet: &[u8],
new_source: Ipv4Addr, new_source: Ipv4Addr,

View File

@ -5,6 +5,7 @@ use pnet::packet::udp::{self, MutableUdpPacket, UdpPacket};
use crate::error::{Error, Result}; use crate::error::{Error, Result};
/// Re-calculates a UDP packet's checksum with a new IPv6 pseudo-header. /// Re-calculates a UDP packet's checksum with a new IPv6 pseudo-header.
#[profiling::function]
pub fn recalculate_udp_checksum_ipv6( pub fn recalculate_udp_checksum_ipv6(
udp_packet: &[u8], udp_packet: &[u8],
new_source: Ipv6Addr, new_source: Ipv6Addr,
@ -48,6 +49,7 @@ pub fn recalculate_udp_checksum_ipv6(
} }
/// Re-calculates a UDP packet's checksum with a new IPv4 pseudo-header. /// Re-calculates a UDP packet's checksum with a new IPv4 pseudo-header.
#[profiling::function]
pub fn recalculate_udp_checksum_ipv4( pub fn recalculate_udp_checksum_ipv4(
udp_packet: &[u8], udp_packet: &[u8],
new_source: Ipv4Addr, new_source: Ipv4Addr,