-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
711 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,75 @@ | ||
//! Packet MMAP. | ||
|
||
#[cfg(all(feature = "mm", feature = "net", feature = "std", target_os = "linux"))] | ||
fn main() -> std::io::Result<()> { | ||
use rustix::mm::{mmap, munmap, MapFlags, ProtFlags}; | ||
use rustix::net::{ | ||
bind_link, eth, | ||
netdevice::name_to_index, | ||
packet::{PacketReq, PacketReqAny, SocketAddrLink}, | ||
recvfrom, socket_with, | ||
sockopt::{ | ||
get_packet_stats, set_packet_rx_ring, set_packet_tx_ring, set_packet_version, | ||
PacketVersion, | ||
}, | ||
AddressFamily, RecvFlags, SocketFlags, SocketType, | ||
}; | ||
use std::{env::args, ptr}; | ||
|
||
let name = args().nth(0).unwrap(); | ||
|
||
let family = AddressFamily::PACKET; | ||
let type_ = SocketType::RAW; | ||
let flags = SocketFlags::empty(); | ||
let fd = socket_with(family, type_, flags, None)?; | ||
|
||
let index = name_to_index(&fd, &name)?; | ||
|
||
set_packet_version(&fd, PacketVersion::V2)?; | ||
|
||
let req = PacketReq { | ||
block_size: 4096, | ||
block_nr: 4, | ||
frame_size: 2048, | ||
frame_nr: 8, | ||
}; | ||
let size = req.block_size as usize * req.block_nr as usize; | ||
|
||
let req = PacketReqAny::V2(req); | ||
set_packet_rx_ring(&fd, &req)?; | ||
set_packet_tx_ring(&fd, &req)?; | ||
|
||
let rx = unsafe { | ||
mmap( | ||
ptr::null_mut(), | ||
size * 2, | ||
ProtFlags::READ | ProtFlags::WRITE, | ||
MapFlags::SHARED, | ||
&fd, | ||
0, | ||
) | ||
}?; | ||
let _tx = rx.wrapping_add(size); | ||
|
||
let addr = SocketAddrLink::new(eth::ALL, index); | ||
bind_link(&fd, &addr)?; | ||
|
||
while let Ok(_) = recvfrom(&fd, &mut [], RecvFlags::empty()) {} | ||
|
||
let stats = get_packet_stats(&fd, PacketVersion::V2)?; | ||
println!("{:?}", stats); | ||
|
||
unsafe { munmap(rx, size * 2) }?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(any( | ||
not(feature = "mm"), | ||
not(feature = "net"), | ||
not(feature = "std"), | ||
not(target_os = "linux") | ||
))] | ||
fn main() -> Result<(), &'static str> { | ||
Err("This example requires --features=mm,net,std and is only supported on Linux.") | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.