From 6c0e6b2341d8242740892ee4747f458067b5d3a5 Mon Sep 17 00:00:00 2001
From: Evan Pratten <ewpratten@gmail.com>
Date: Fri, 4 Aug 2023 21:22:57 -0400
Subject: [PATCH] Embed profiling macros into crates

---
 libs/easy-tun/Cargo.toml                        |  1 +
 libs/easy-tun/src/tun.rs                        |  3 +++
 libs/fast-nat/Cargo.toml                        |  3 ++-
 libs/fast-nat/src/bimap.rs                      | 10 ++++++++--
 libs/fast-nat/src/cpnat.rs                      | 10 ++++++++++
 libs/fast-nat/src/nat.rs                        |  5 +++++
 libs/interproto/Cargo.toml                      |  1 +
 libs/interproto/src/protocols/icmp/mod.rs       |  2 ++
 libs/interproto/src/protocols/icmp/type_code.rs |  2 ++
 libs/interproto/src/protocols/ip.rs             |  2 ++
 libs/interproto/src/protocols/tcp.rs            |  2 ++
 libs/interproto/src/protocols/udp.rs            |  2 ++
 12 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/libs/easy-tun/Cargo.toml b/libs/easy-tun/Cargo.toml
index e638933..2d56d75 100644
--- a/libs/easy-tun/Cargo.toml
+++ b/libs/easy-tun/Cargo.toml
@@ -17,6 +17,7 @@ log = "^0.4"
 libc = "^0.2"
 ioctl-gen = "^0.1.1"
 rtnetlink = "^0.13.0"
+profiling = "1.0.9"
 
 [dev-dependencies]
 env_logger = "0.10.0"
diff --git a/libs/easy-tun/src/tun.rs b/libs/easy-tun/src/tun.rs
index 1aef2a6..4ff5bdf 100644
--- a/libs/easy-tun/src/tun.rs
+++ b/libs/easy-tun/src/tun.rs
@@ -111,16 +111,19 @@ impl IntoRawFd for Tun {
 }
 
 impl Read for Tun {
+    #[profiling::function]
     fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
         self.fd.read(buf)
     }
 }
 
 impl Write for Tun {
+    #[profiling::function]
     fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
         self.fd.write(buf)
     }
 
+    #[profiling::function]
     fn flush(&mut self) -> std::io::Result<()> {
         self.fd.flush()
     }
diff --git a/libs/fast-nat/Cargo.toml b/libs/fast-nat/Cargo.toml
index 7bb1672..416670a 100644
--- a/libs/fast-nat/Cargo.toml
+++ b/libs/fast-nat/Cargo.toml
@@ -16,4 +16,5 @@ categories = []
 log = "^0.4"
 rustc-hash = "1.1.0"
 thiserror = "^1.0.44"
-ipnet = "^2.8.0"
\ No newline at end of file
+ipnet = "^2.8.0"
+profiling = "1.0.9"
\ No newline at end of file
diff --git a/libs/fast-nat/src/bimap.rs b/libs/fast-nat/src/bimap.rs
index e2f5f11..0bb2460 100644
--- a/libs/fast-nat/src/bimap.rs
+++ b/libs/fast-nat/src/bimap.rs
@@ -22,29 +22,33 @@ where
     }
 
     /// Insert a new mapping into the `BiHashMap`
+    #[profiling::function]
     pub fn insert(&mut self, left: Left, right: Right) {
         self.left_to_right.insert(left.clone(), right.clone());
         self.right_to_left.insert(right, left);
     }
 
     /// Get the right value for a given left value
+    #[profiling::function]
     pub fn get_right(&self, left: &Left) -> Option<&Right> {
         self.left_to_right.get(left)
     }
 
     /// Get the left value for a given right value
+    #[profiling::function]
     pub fn get_left(&self, right: &Right) -> Option<&Left> {
         self.right_to_left.get(right)
     }
 
     /// Remove a mapping from the `BiHashMap`
+    #[profiling::function]
     pub fn remove(&mut self, left: &Left, right: &Right) {
         self.left_to_right.remove(left);
         self.right_to_left.remove(right);
     }
 
     /// Remove a mapping from the `BiHashMap` by left value
-    #[allow(dead_code)]
+    #[profiling::function]
     pub fn remove_left(&mut self, left: &Left) {
         if let Some(right) = self.left_to_right.remove(left) {
             self.right_to_left.remove(&right);
@@ -52,7 +56,7 @@ where
     }
 
     /// Remove a mapping from the `BiHashMap` by right value
-    #[allow(dead_code)]
+    #[profiling::function]
     pub fn remove_right(&mut self, right: &Right) {
         if let Some(left) = self.right_to_left.remove(right) {
             self.left_to_right.remove(&left);
@@ -60,11 +64,13 @@ where
     }
 
     /// Get the total number of mappings in the `BiHashMap`
+    #[profiling::function]
     pub fn len(&self) -> usize {
         self.left_to_right.len()
     }
 
     /// Check if the `BiHashMap` is empty
+    #[profiling::function]
     pub fn is_empty(&self) -> bool {
         self.left_to_right.is_empty()
     }
diff --git a/libs/fast-nat/src/cpnat.rs b/libs/fast-nat/src/cpnat.rs
index 8496ed7..c2f8883 100644
--- a/libs/fast-nat/src/cpnat.rs
+++ b/libs/fast-nat/src/cpnat.rs
@@ -25,6 +25,7 @@ impl CrossProtocolNetworkAddressTable {
     }
 
     /// Prune all old mappings
+    #[profiling::function]
     pub fn prune(&mut self) {
         log::trace!("Pruning old network address mappings");
 
@@ -54,6 +55,7 @@ impl CrossProtocolNetworkAddressTable {
     }
 
     /// Insert a new indefinite mapping
+    #[profiling::function]
     pub fn insert_indefinite(&mut self, ipv4: Ipv4Addr, ipv6: Ipv6Addr) {
         self.prune();
         let (ipv4, ipv6) = (ipv4.into(), ipv6.into());
@@ -62,6 +64,7 @@ impl CrossProtocolNetworkAddressTable {
     }
 
     /// Insert a new mapping with a finite time-to-live
+    #[profiling::function]
     pub fn insert(&mut self, ipv4: Ipv4Addr, ipv6: Ipv6Addr, duration: Duration) {
         self.prune();
         let (ipv4, ipv6) = (ipv4.into(), ipv6.into());
@@ -77,6 +80,7 @@ impl CrossProtocolNetworkAddressTable {
 
     /// Get the IPv6 address for a given IPv4 address
     #[must_use]
+    #[profiling::function]
     pub fn get_ipv6(&self, ipv4: &Ipv4Addr) -> Option<Ipv6Addr> {
         self.addr_map
             .get_right(&(*ipv4).into())
@@ -85,6 +89,7 @@ impl CrossProtocolNetworkAddressTable {
 
     /// Get the IPv4 address for a given IPv6 address
     #[must_use]
+    #[profiling::function]
     pub fn get_ipv4(&self, ipv6: &Ipv6Addr) -> Option<Ipv4Addr> {
         self.addr_map
             .get_left(&(*ipv6).into())
@@ -93,12 +98,14 @@ impl CrossProtocolNetworkAddressTable {
 
     /// Get the number of mappings in the table
     #[must_use]
+    #[profiling::function]
     pub fn len(&self) -> usize {
         self.addr_map.len()
     }
 
     /// Check if the table is empty
     #[must_use]
+    #[profiling::function]
     pub fn is_empty(&self) -> bool {
         self.addr_map.is_empty()
     }
@@ -135,6 +142,7 @@ impl CrossProtocolNetworkAddressTableWithIpv4Pool {
     }
 
     /// Insert a new static mapping
+    #[profiling::function]
     pub fn insert_static(&mut self, ipv4: Ipv4Addr, ipv6: Ipv6Addr) -> Result<(), Error> {
         if !self.pool.iter().any(|prefix| prefix.contains(&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)
+    #[profiling::function]
     pub fn get_or_create_ipv4(&mut self, ipv6: &Ipv6Addr) -> Result<Ipv4Addr, Error> {
         // Return the known mapping if it exists
         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
     #[must_use]
+    #[profiling::function]
     pub fn get_ipv6(&self, ipv4: &Ipv4Addr) -> Option<Ipv6Addr> {
         self.table.get_ipv6(ipv4)
     }
diff --git a/libs/fast-nat/src/nat.rs b/libs/fast-nat/src/nat.rs
index 63fb144..b24c527 100644
--- a/libs/fast-nat/src/nat.rs
+++ b/libs/fast-nat/src/nat.rs
@@ -19,6 +19,7 @@ impl NetworkAddressTable {
     }
 
     /// Prune all old mappings
+    #[profiling::function]
     pub fn prune(&mut self) {
         log::trace!("Pruning old network address mappings");
 
@@ -48,6 +49,7 @@ impl NetworkAddressTable {
     }
 
     /// Insert a new indefinite mapping
+    #[profiling::function]
     pub fn insert_indefinite(&mut self, left: Ipv4Addr, right: Ipv4Addr) {
         self.prune();
         let (left, right) = (left.into(), right.into());
@@ -56,6 +58,7 @@ impl NetworkAddressTable {
     }
 
     /// Insert a new mapping with a finite time-to-live
+    #[profiling::function]
     pub fn insert(&mut self, left: Ipv4Addr, right: Ipv4Addr, duration: Duration) {
         self.prune();
         let (left, right) = (left.into(), right.into());
@@ -71,6 +74,7 @@ impl NetworkAddressTable {
 
     /// Get the right value for a given left value
     #[must_use]
+    #[profiling::function]
     pub fn get_right(&self, left: &Ipv4Addr) -> Option<Ipv4Addr> {
         self.addr_map
             .get_right(&(*left).into())
@@ -79,6 +83,7 @@ impl NetworkAddressTable {
 
     /// Get the left value for a given right value
     #[must_use]
+    #[profiling::function]
     pub fn get_left(&self, right: &Ipv4Addr) -> Option<Ipv4Addr> {
         self.addr_map
             .get_left(&(*right).into())
diff --git a/libs/interproto/Cargo.toml b/libs/interproto/Cargo.toml
index 357e1b0..bfa7b2b 100644
--- a/libs/interproto/Cargo.toml
+++ b/libs/interproto/Cargo.toml
@@ -21,3 +21,4 @@ protomask-metrics = { path = "../protomask-metrics", optional = true }
 log = "^0.4"
 pnet = "0.34.0"
 thiserror = "^1.0.44"
+profiling = "1.0.9"
diff --git a/libs/interproto/src/protocols/icmp/mod.rs b/libs/interproto/src/protocols/icmp/mod.rs
index aafc31c..ac08fdf 100644
--- a/libs/interproto/src/protocols/icmp/mod.rs
+++ b/libs/interproto/src/protocols/icmp/mod.rs
@@ -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.
 #[allow(clippy::deprecated_cfg_attr)]
+#[profiling::function]
 pub fn translate_icmp_to_icmpv6(
     icmp_packet: &[u8],
     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.
+#[profiling::function]
 pub fn translate_icmpv6_to_icmp(
     icmpv6_packet: &[u8],
     new_source: Ipv4Addr,
diff --git a/libs/interproto/src/protocols/icmp/type_code.rs b/libs/interproto/src/protocols/icmp/type_code.rs
index e566a84..0be991d 100644
--- a/libs/interproto/src/protocols/icmp/type_code.rs
+++ b/libs/interproto/src/protocols/icmp/type_code.rs
@@ -9,6 +9,7 @@ use crate::error::{Error, Result};
 
 /// Best effort translation from an ICMP type and code to an ICMPv6 type and code
 #[allow(clippy::deprecated_cfg_attr)]
+#[profiling::function]
 pub fn translate_type_and_code_4_to_6(
     icmp_type: IcmpType,
     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
 #[allow(clippy::deprecated_cfg_attr)]
+#[profiling::function]
 pub fn translate_type_and_code_6_to_4(
     icmp_type: Icmpv6Type,
     icmp_code: Icmpv6Code,
diff --git a/libs/interproto/src/protocols/ip.rs b/libs/interproto/src/protocols/ip.rs
index daec44e..7c41170 100644
--- a/libs/interproto/src/protocols/ip.rs
+++ b/libs/interproto/src/protocols/ip.rs
@@ -15,6 +15,7 @@ use pnet::packet::{
 use std::net::{Ipv4Addr, Ipv6Addr};
 
 /// 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(
     ipv4_packet: &[u8],
     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.
+#[profiling::function]
 pub fn translate_ipv6_to_ipv4(
     ipv6_packet: &[u8],
     new_source: Ipv4Addr,
diff --git a/libs/interproto/src/protocols/tcp.rs b/libs/interproto/src/protocols/tcp.rs
index e33bc23..7bb1f3d 100644
--- a/libs/interproto/src/protocols/tcp.rs
+++ b/libs/interproto/src/protocols/tcp.rs
@@ -5,6 +5,7 @@ use pnet::packet::tcp::{self, MutableTcpPacket, TcpPacket};
 use crate::error::{Error, Result};
 
 /// Re-calculates a TCP packet's checksum with a new IPv6 pseudo-header.
+#[profiling::function]
 pub fn recalculate_tcp_checksum_ipv6(
     tcp_packet: &[u8],
     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.
+#[profiling::function]
 pub fn recalculate_tcp_checksum_ipv4(
     tcp_packet: &[u8],
     new_source: Ipv4Addr,
diff --git a/libs/interproto/src/protocols/udp.rs b/libs/interproto/src/protocols/udp.rs
index 099ef7c..4c0d21a 100644
--- a/libs/interproto/src/protocols/udp.rs
+++ b/libs/interproto/src/protocols/udp.rs
@@ -5,6 +5,7 @@ use pnet::packet::udp::{self, MutableUdpPacket, UdpPacket};
 use crate::error::{Error, Result};
 
 /// Re-calculates a UDP packet's checksum with a new IPv6 pseudo-header.
+#[profiling::function]
 pub fn recalculate_udp_checksum_ipv6(
     udp_packet: &[u8],
     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.
+#[profiling::function]
 pub fn recalculate_udp_checksum_ipv4(
     udp_packet: &[u8],
     new_source: Ipv4Addr,