From 18fa7be6ce258a435309ff2b2d5d04325abb8180 Mon Sep 17 00:00:00 2001 From: Aviram Hassan Date: Sun, 1 May 2022 22:08:45 +0300 Subject: [PATCH] * Change behavior of namespace change - set the namespace only in the packet sniffing, in a new thread so command socket will listen on the original network namespace *formatting --- CHANGELOG.md | 3 +++ Cargo.lock | 55 +++++++++++++++++++++++++------------------------- Cargo.toml | 3 +-- src/api.rs | 47 ------------------------------------------ src/main.rs | 32 ++++++++++++++--------------- src/sniffer.rs | 12 ++++++++++- 6 files changed, 57 insertions(+), 95 deletions(-) delete mode 100644 src/api.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 6557660..d3e11af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ ## [Unreleased] +## 2.0.0-alpha-3 - 1/5/2022 +### Changed +* Change behavior of namespace change - set the namespace only in the packet sniffing, in a new thread so "command" socket will listen on the original network namespace ## 2.0.0-alpha-2 - 30/4/2022 ### Fixed * Fixed obtaining namespace & setting it using container id (seems to be a bug in new containerd-client version?) diff --git a/Cargo.lock b/Cargo.lock index 117e1d2..2f8618b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -133,9 +133,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "3.1.12" +version = "3.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c167e37342afc5f33fd87bbc870cedd020d2a6dffa05d45ccd9241fbdd146db" +checksum = "535434c063ced786eb04aaf529308092c5ab60889e8fe24275d15de07b01fa97" dependencies = [ "atty", "bitflags", @@ -163,9 +163,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.1.1" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "189ddd3b5d32a70b35e7686054371742a937b0d99128e76dde6340210e966669" +checksum = "a37c35f1112dad5e6e0b1adaff798507497a18fceeb30cceb3bae7d1427b9213" dependencies = [ "os_str_bytes", ] @@ -561,9 +561,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.4.1" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "memoffset" @@ -599,11 +599,10 @@ dependencies = [ [[package]] name = "mirrord-agent" -version = "2.0.0-alpha-2" +version = "2.0.0-alpha-3" dependencies = [ "actix-codec", "anyhow", - "base64", "clap", "containerd-client", "env_logger", @@ -703,9 +702,9 @@ dependencies = [ [[package]] name = "parking_lot_core" -version = "0.9.2" +version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "995f667a6c822200b0433ac218e05582f0e2efa1b922a3fd2fbaadc5f87bab37" +checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929" dependencies = [ "cfg-if", "libc", @@ -1044,18 +1043,18 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.136" +version = "1.0.137" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" +checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.136" +version = "1.0.137" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" +checksum = "1f26faba0c3959972377d3b2d306ee9f71faee9714294e41bb777f83f88578be" dependencies = [ "proc-macro2", "quote", @@ -1064,9 +1063,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.79" +version = "1.0.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95" +checksum = "f972498cf015f7c0746cac89ebe1d6ef10c293b94175a243a2d9442c163d9944" dependencies = [ "itoa", "ryu", @@ -1450,9 +1449,9 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-sys" -version = "0.34.0" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5acdd78cb4ba54c0045ac14f62d8f94a03d10047904ae2a40afa1e99d8f70825" +checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" dependencies = [ "windows_aarch64_msvc", "windows_i686_gnu", @@ -1463,30 +1462,30 @@ dependencies = [ [[package]] name = "windows_aarch64_msvc" -version = "0.34.0" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17cffbe740121affb56fad0fc0e421804adf0ae00891205213b5cecd30db881d" +checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" [[package]] name = "windows_i686_gnu" -version = "0.34.0" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2564fde759adb79129d9b4f54be42b32c89970c18ebf93124ca8870a498688ed" +checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" [[package]] name = "windows_i686_msvc" -version = "0.34.0" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cd9d32ba70453522332c14d38814bceeb747d80b3958676007acadd7e166956" +checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" [[package]] name = "windows_x86_64_gnu" -version = "0.34.0" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfce6deae227ee8d356d19effc141a509cc503dfd1f850622ec4b0f84428e1f4" +checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" [[package]] name = "windows_x86_64_msvc" -version = "0.34.0" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d19538ccc21819d01deaf88d6a17eae6596a12e9aafdbb97916fb49896d89de9" +checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" diff --git a/Cargo.toml b/Cargo.toml index 883dd2b..f802099 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mirrord-agent" -version = "2.0.0-alpha-2" +version = "2.0.0-alpha-3" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -14,7 +14,6 @@ pcap = { git = "https://github.com/aviramha/pcap.git", branch="make_codec_public pnet = "0.29.0" nix = "0.23.1" anyhow = "1" -base64 = "0.13" clap = { version = "3.1.2", features = ["derive"] } mirrord-protocol = "0.2" env_logger = "0.9" diff --git a/src/api.rs b/src/api.rs deleted file mode 100644 index 87b42b9..0000000 --- a/src/api.rs +++ /dev/null @@ -1,47 +0,0 @@ -use serde::{Deserialize, Serialize}; - -pub type ConnectionID = u64; - -#[derive(Serialize, Deserialize, Debug)] -pub struct TCPConnected { - pub connection_id: ConnectionID, - pub port: u16, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct TCPData { - pub connection_id: ConnectionID, - pub data: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct TCPEnded { - pub connection_id: ConnectionID, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct AgentError { - msg: String, -} - -impl AgentError { - pub fn from_error(error: T) -> AgentError - where - T: ToString, - { - AgentError { - msg: error.to_string(), - } - } -} - -#[derive(Serialize, Deserialize, Debug)] -#[serde(tag = "type", content = "content")] -pub enum Event { - Connected(TCPConnected), - InfoMessage(String), - TCPEnded(TCPEnded), - Data(TCPData), - Error(AgentError), - Done, -} diff --git a/src/main.rs b/src/main.rs index 95485a4..53ac594 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,21 @@ use anyhow::Result; use futures::SinkExt; -use tokio::{select, task}; +use tokio::{ + net::{TcpListener, TcpStream}, + select, + sync::mpsc::{self}, +}; use tokio_stream::StreamExt; use tracing::{debug, error, info}; -use std::borrow::Borrow; -use std::collections::HashSet; -use std::hash::{Hash, Hasher}; -// use mirrord_protocol::{MirrordCodec, MirrordMessage}; -use std::net::{Ipv4Addr, SocketAddrV4}; -use tokio::net::{TcpListener, TcpStream}; - -use tokio::sync::mpsc::{self}; +use mirrord_protocol::{ClientMessage, ConnectionID, DaemonCodec, DaemonMessage, Port}; +use std::{ + borrow::Borrow, + collections::HashSet, + hash::{Hash, Hasher}, + net::{Ipv4Addr, SocketAddrV4}, +}; mod cli; mod runtime; @@ -20,8 +23,6 @@ mod sniffer; mod util; use cli::parse_args; -use mirrord_protocol::{ClientMessage, ConnectionID, DaemonCodec, DaemonMessage, Port}; -use runtime::{get_container_namespace, set_namespace}; use sniffer::{packet_worker, SnifferCommand, SnifferOutput}; use util::{IndexAllocator, Subscriptions}; @@ -140,11 +141,6 @@ async fn peer_handler( async fn start() -> Result<()> { let args = parse_args(); debug!("mirrord-agent starting with args {:?}", args); - if let Some(container_id) = args.container_id { - let namespace = get_container_namespace(container_id).await?; - debug!("Found namespace to attach to {:?}", &namespace); - set_namespace(&namespace)?; - } let listener = TcpListener::bind(SocketAddrV4::new( Ipv4Addr::new(0, 0, 0, 0), @@ -156,10 +152,12 @@ async fn start() -> Result<()> { let (peers_tx, mut peers_rx) = mpsc::channel::(1000); let (packet_sniffer_tx, mut packet_sniffer_rx) = mpsc::channel::(1000); let (packet_command_tx, packet_command_rx) = mpsc::channel::(1000); - let packet_task = task::spawn(packet_worker( + // We use tokio spawn so it'll create another thread. + let packet_task = tokio::spawn(packet_worker( packet_sniffer_tx, packet_command_rx, args.interface.clone(), + args.container_id.clone(), )); loop { select! { diff --git a/src/sniffer.rs b/src/sniffer.rs index d50af40..e581709 100644 --- a/src/sniffer.rs +++ b/src/sniffer.rs @@ -18,7 +18,10 @@ use std::net::{IpAddr, Ipv4Addr}; use tokio::select; use tracing::{debug, error}; -use crate::util::IndexAllocator; +use crate::{ + runtime::{get_container_namespace, set_namespace}, + util::IndexAllocator, +}; use mirrord_protocol::{NewTCPConnection, TCPClose, TCPData}; const DUMMY_BPF: &str = @@ -228,7 +231,14 @@ pub async fn packet_worker( tx: Sender, mut rx: Receiver, interface: String, + container_id: Option, ) -> Result<()> { + debug!("setting namespace"); + if let Some(container_id) = container_id { + let namespace = get_container_namespace(container_id).await?; + debug!("Found namespace to attach to {:?}", &namespace); + set_namespace(&namespace)?; + } debug!("preparing sniffer"); let sniffer = prepare_sniffer(interface)?; debug!("done prepare sniffer");