Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Commit

Permalink
* Change behavior of namespace change - set the namespace only in the…
Browse files Browse the repository at this point in the history
… packet sniffing, in a new thread so command socket will listen on the original network namespace

*formatting
  • Loading branch information
aviramha committed May 2, 2022
1 parent 46724b4 commit 18fa7be
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 95 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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?)
Expand Down
55 changes: 27 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
Expand Down
47 changes: 0 additions & 47 deletions src/api.rs

This file was deleted.

32 changes: 15 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
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;
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};

Expand Down Expand Up @@ -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),
Expand All @@ -156,10 +152,12 @@ async fn start() -> Result<()> {
let (peers_tx, mut peers_rx) = mpsc::channel::<PeerMessage>(1000);
let (packet_sniffer_tx, mut packet_sniffer_rx) = mpsc::channel::<SnifferOutput>(1000);
let (packet_command_tx, packet_command_rx) = mpsc::channel::<SnifferCommand>(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! {
Expand Down
12 changes: 11 additions & 1 deletion src/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -228,7 +231,14 @@ pub async fn packet_worker(
tx: Sender<SnifferOutput>,
mut rx: Receiver<SnifferCommand>,
interface: String,
container_id: Option<String>,
) -> 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");
Expand Down

0 comments on commit 18fa7be

Please sign in to comment.