-
Notifications
You must be signed in to change notification settings - Fork 21
Network Testing/Debugging Harness #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
cramt
wants to merge
10
commits into
main
Choose a base branch
from
alex/network-tester
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c2ef1d3
initial version
cramt 4f1f875
ui works
cramt 0dbbf6e
tokio mspc
cramt 4329ff5
no need to cops in mpsc
cramt ab4d390
Merge branch 'main' into alex/network-tester
cramt 338468d
whoopsie wrong impl
cramt e321126
Merge branch 'main' into alex/network-tester
cramt eb658af
Merge remote-tracking branch 'origin/alex/network-tester' into alex/n…
cramt ebe3e6d
connected them up yay
cramt 05799c8
it kinda works
cramt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
crates/ergot/src/interface_manager/interface_impls/tokio_mpsc.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| //! std tcp interface impl | ||
| //! | ||
| //! std tcp uses COBS for framing over a MPSC queues | ||
|
|
||
| use crate::interface_manager::{ | ||
| Interface, | ||
| utils::{framed_stream, std::StdQueue}, | ||
| }; | ||
|
|
||
| /// An interface implementation for MPSC channel using tokio | ||
| pub struct TokioMpscInterface {} | ||
|
|
||
| impl Interface for TokioMpscInterface { | ||
| type Sink = framed_stream::Sink<StdQueue>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
crates/ergot/src/interface_manager/profiles/direct_edge/tokio_mpsc.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| //! A std+tcp edge device profile | ||
| //! | ||
| //! This is useful for std based devices/applications that can directly connect to a DirectRouter | ||
| //! using a tcp connection. | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use crate::{ | ||
| interface_manager::{ | ||
| InterfaceState, Profile, | ||
| interface_impls::tokio_mpsc::TokioMpscInterface, | ||
| profiles::direct_edge::{DirectEdge, process_frame}, | ||
| utils::std::{ | ||
| ReceiverError, StdQueue, | ||
| acc::{CobsAccumulator, FeedResult}, | ||
| }, | ||
| }, | ||
| net_stack::NetStackHandle, | ||
| }; | ||
|
|
||
| use bbq2::{prod_cons::stream::StreamConsumer, traits::bbqhdl::BbqHandle}; | ||
| use log::{error, info, trace, warn}; | ||
| use maitake_sync::WaitQueue; | ||
| use tokio::{ | ||
| select, | ||
| sync::mpsc::{Receiver, Sender}, | ||
| }; | ||
|
|
||
| pub type StdTcpClientIm = DirectEdge<TokioMpscInterface>; | ||
|
|
||
| pub struct RxWorker<N: NetStackHandle> { | ||
| stack: N, | ||
| skt: Receiver<Vec<u8>>, | ||
| closer: Arc<WaitQueue>, | ||
| } | ||
|
|
||
| // ---- impls ---- | ||
|
|
||
| impl<N> RxWorker<N> | ||
| where | ||
| N: NetStackHandle<Profile = DirectEdge<TokioMpscInterface>>, | ||
| { | ||
| pub async fn run(mut self) -> Result<(), ReceiverError> { | ||
| let res = self.run_inner().await; | ||
| // todo: this could live somewhere else? | ||
| self.stack.stack().manage_profile(|im| { | ||
| _ = im.set_interface_state((), InterfaceState::Down); | ||
| }); | ||
| res | ||
| } | ||
|
|
||
| pub async fn run_inner(&mut self) -> Result<(), ReceiverError> { | ||
| let mut net_id = None; | ||
|
|
||
| loop { | ||
| let rd = self.skt.recv(); | ||
| let close = self.closer.wait(); | ||
|
|
||
| let ct = select! { | ||
| r = rd => { | ||
| match r { | ||
| None => { | ||
| warn!("recv run closed"); | ||
| return Err(ReceiverError::SocketClosed) | ||
| }, | ||
| Some(ct) => ct, | ||
| } | ||
| } | ||
| _c = close => { | ||
| return Err(ReceiverError::SocketClosed); | ||
| } | ||
| }; | ||
| process_frame(&mut net_id, ct.as_slice(), &self.stack, ()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, PartialEq)] | ||
| pub struct SocketAlreadyActive; | ||
|
|
||
| // Helper functions | ||
|
|
||
| pub async fn register_target_interface<N>( | ||
| stack: N, | ||
| socket: (Sender<Vec<u8>>, Receiver<Vec<u8>>), | ||
| queue: StdQueue, | ||
| ) -> Result<(), SocketAlreadyActive> | ||
| where | ||
| N: NetStackHandle<Profile = DirectEdge<TokioMpscInterface>>, | ||
| N: Send + 'static, | ||
| { | ||
| let (tx, rx) = socket; | ||
| let closer = Arc::new(WaitQueue::new()); | ||
| stack.stack().manage_profile(|im| { | ||
| match im.interface_state(()) { | ||
| Some(InterfaceState::Down) => {} | ||
| Some(InterfaceState::Inactive) => return Err(SocketAlreadyActive), | ||
| Some(InterfaceState::ActiveLocal { .. }) => return Err(SocketAlreadyActive), | ||
| Some(InterfaceState::Active { .. }) => return Err(SocketAlreadyActive), | ||
| None => {} | ||
| } | ||
|
|
||
| im.set_interface_state((), InterfaceState::Inactive) | ||
| .map_err(|_| SocketAlreadyActive)?; | ||
|
|
||
| Ok(()) | ||
| })?; | ||
| let rx_worker = RxWorker { | ||
| stack, | ||
| skt: rx, | ||
| closer: closer.clone(), | ||
| }; | ||
| // TODO: spawning in a non-async context! | ||
| tokio::task::spawn(tx_worker(tx, queue.stream_consumer(), closer.clone())); | ||
| tokio::task::spawn(rx_worker.run()); | ||
| Ok(()) | ||
| } | ||
|
|
||
| async fn tx_worker(tx: Sender<Vec<u8>>, rx: StreamConsumer<StdQueue>, closer: Arc<WaitQueue>) { | ||
| info!("Started tx_worker"); | ||
| loop { | ||
| let rxf = rx.wait_read(); | ||
| let clf = closer.wait(); | ||
|
|
||
| let frame = select! { | ||
| r = rxf => r, | ||
| _c = clf => { | ||
| break; | ||
| } | ||
| }; | ||
|
|
||
| let len = frame.len(); | ||
| trace!("sending pkt len:{}", len); | ||
| let res = tx.send(frame.to_vec()).await; | ||
| frame.release(len); | ||
| if let Err(e) = res { | ||
| error!("Err: {e:?}"); | ||
| break; | ||
| } | ||
| } | ||
| // TODO: GC waker? | ||
| warn!("Closing interface"); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.