Skip to content

Commit f8e528e

Browse files
committed
call wasm function to trace the network traffic
1 parent 8e62a54 commit f8e528e

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

src/drivers/net/virtio/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ impl smoltcp::phy::TxToken for TxToken<'_> {
265265
result
266266
};
267267

268+
#[cfg(feature = "wasm")]
269+
if let Some(ref mut wasm_manager) = crate::wasm::WASM_MANAGER.lock().as_mut() {
270+
crate::wasm::INPUT.lock().push_back(packet.to_vec());
271+
let _ = wasm_manager.call_func::<(), ()>("pcap_writer", ());
272+
}
273+
268274
let mut header = Box::new_in(<Hdr as Default>::default(), DeviceAlloc);
269275

270276
// If a checksum calculation by the host is necessary, we have to inform the host within the header
@@ -361,6 +367,14 @@ impl smoltcp::phy::RxToken for RxToken<'_> {
361367
.unwrap();
362368
}
363369

370+
#[cfg(feature = "wasm")]
371+
if let Some(ref mut wasm_manager) = crate::wasm::WASM_MANAGER.lock().as_mut() {
372+
crate::wasm::INPUT
373+
.lock()
374+
.push_back(combined_packets.to_vec());
375+
let _ = wasm_manager.call_func::<(), ()>("pcap_writer", ());
376+
}
377+
364378
f(&combined_packets)
365379
}
366380
}

src/wasm/mod.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -579,39 +579,37 @@ impl WasmManager {
579579
}
580580

581581
async fn wasm_run() {
582-
loop {
583-
future::poll_fn(|cx| {
584-
if let Some(mut guard) = OUTPUT.try_lock() {
585-
if let Some((fd, data)) = guard.data.pop_front() {
586-
let obj = match fd {
587-
Descriptor::Stdout => crate::core_scheduler()
588-
.get_object(fd::STDOUT_FILENO)
589-
.unwrap(),
590-
Descriptor::Stderr => crate::core_scheduler()
591-
.get_object(fd::STDERR_FILENO)
592-
.unwrap(),
593-
Descriptor::RawFd(raw_fd) => {
594-
crate::core_scheduler().get_object(raw_fd).unwrap()
595-
}
596-
_ => panic!("Unsuppted {fd:?}"),
597-
};
582+
future::poll_fn(|cx| {
583+
if let Some(mut guard) = OUTPUT.try_lock() {
584+
if let Some((fd, data)) = guard.data.pop_front() {
585+
let obj = match fd {
586+
Descriptor::Stdout => crate::core_scheduler()
587+
.get_object(fd::STDOUT_FILENO)
588+
.unwrap(),
589+
Descriptor::Stderr => crate::core_scheduler()
590+
.get_object(fd::STDERR_FILENO)
591+
.unwrap(),
592+
Descriptor::RawFd(raw_fd) => {
593+
crate::core_scheduler().get_object(raw_fd).unwrap()
594+
}
595+
_ => panic!("Unsuppted {fd:?}"),
596+
};
598597

599-
drop(guard);
600-
while let Poll::Pending = pin!(obj.write(&data)).poll(cx) {}
598+
drop(guard);
599+
while let Poll::Pending = pin!(obj.write(&data)).poll(cx) {}
601600

602-
cx.waker().wake_by_ref();
603-
Poll::<()>::Pending
604-
} else {
605-
guard.waker.register(cx.waker());
606-
Poll::<()>::Pending
607-
}
608-
} else {
609601
cx.waker().wake_by_ref();
610602
Poll::<()>::Pending
603+
} else {
604+
guard.waker.register(cx.waker());
605+
Poll::<()>::Pending
611606
}
612-
})
613-
.await;
614-
}
607+
} else {
608+
cx.waker().wake_by_ref();
609+
Poll::<()>::Pending
610+
}
611+
})
612+
.await;
615613
}
616614

617615
#[hermit_macro::system]

0 commit comments

Comments
 (0)