This commit is contained in:
Dario Nieuwenhuis
2021-02-03 05:09:37 +01:00
commit cb5931d583
20 changed files with 1793 additions and 0 deletions

View File

@ -0,0 +1,17 @@
[package]
name = "embassy-net-examples"
version = "0.1.0"
authors = ["Dario Nieuwenhuis <dirbaio@dirbaio.net>"]
edition = "2018"
[dependencies]
heapless = { version = "0.5.6", default-features = false }
embassy = { version = "0.1.0", features=["std", "log"] }
embassy-std = { version = "0.1.0" }
embassy-net = { version = "0.1.0", path = "../embassy-net", features=["std", "log"] }
env_logger = "0.8.2"
log = "0.4.11"
futures = "0.3.8"
libc = "0.2.81"
async-io = "1.3.1"
smoltcp = { version = "0.6.0", default-features = false }

View File

@ -0,0 +1,79 @@
#![feature(type_alias_impl_trait)]
use embassy::executor::{Spawner, task};
use embassy::io::{AsyncBufReadExt, AsyncWriteExt};
use embassy::time::{Duration, Timer};
use embassy::util::Forever;
use embassy_net::*;
use embassy_std::Executor;
use heapless::Vec;
use log::*;
mod tuntap;
use crate::tuntap::TunTapDevice;
static DEVICE: Forever<TunTapDevice> = Forever::new();
static CONFIG: Forever<StaticConfigurator> = Forever::new();
#[task]
async fn net_task() {
embassy_net::run().await
}
#[task]
async fn main_task(spawner: Spawner) {
// Init network device
let device = TunTapDevice::new("tap0").unwrap();
// Static IP configuration
let config = StaticConfigurator::new(UpConfig {
address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 1), 24),
dns_servers: Vec::new(),
gateway: Ipv4Address::new(192, 168, 69, 100),
});
// Init network stack
embassy_net::init(DEVICE.put(device), CONFIG.put(config));
// Launch network task
spawner.spawn(net_task()).unwrap();
// Then we can use it!
let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096];
let mut socket = TcpSocket::new(&mut rx_buffer, &mut tx_buffer);
socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10)));
let remote_endpoint = (Ipv4Address::new(192, 168, 69, 100), 8000);
info!("connecting to {:?}...", remote_endpoint);
let r = socket.connect(remote_endpoint).await;
if let Err(e) = r {
warn!("connect error: {:?}", e);
return;
}
info!("connected!");
loop {
let r = socket.write_all(b"Hello!\n").await;
if let Err(e) = r {
warn!("write error: {:?}", e);
return;
}
}
}
static EXECUTOR: Forever<Executor> = Forever::new();
fn main() {
env_logger::builder()
.filter_level(log::LevelFilter::Debug)
.filter_module("async_io", log::LevelFilter::Info)
.format_timestamp_nanos()
.init();
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
spawner.spawn(main_task(spawner)).unwrap();
});
}

View File

@ -0,0 +1,200 @@
use async_io::Async;
use embassy::util::WakerRegistration;
use libc;
use smoltcp::wire::EthernetFrame;
use std::io;
use std::io::{Read, Write};
use std::os::unix::io::{AsRawFd, RawFd};
use log::*;
pub const SIOCGIFMTU: libc::c_ulong = 0x8921;
pub const SIOCGIFINDEX: libc::c_ulong = 0x8933;
pub const ETH_P_ALL: libc::c_short = 0x0003;
pub const TUNSETIFF: libc::c_ulong = 0x400454CA;
pub const IFF_TUN: libc::c_int = 0x0001;
pub const IFF_TAP: libc::c_int = 0x0002;
pub const IFF_NO_PI: libc::c_int = 0x1000;
#[repr(C)]
#[derive(Debug)]
struct ifreq {
ifr_name: [libc::c_char; libc::IF_NAMESIZE],
ifr_data: libc::c_int, /* ifr_ifindex or ifr_mtu */
}
fn ifreq_for(name: &str) -> ifreq {
let mut ifreq = ifreq {
ifr_name: [0; libc::IF_NAMESIZE],
ifr_data: 0,
};
for (i, byte) in name.as_bytes().iter().enumerate() {
ifreq.ifr_name[i] = *byte as libc::c_char
}
ifreq
}
fn ifreq_ioctl(
lower: libc::c_int,
ifreq: &mut ifreq,
cmd: libc::c_ulong,
) -> io::Result<libc::c_int> {
unsafe {
let res = libc::ioctl(lower, cmd as _, ifreq as *mut ifreq);
if res == -1 {
return Err(io::Error::last_os_error());
}
}
Ok(ifreq.ifr_data)
}
#[derive(Debug)]
pub struct TunTap {
fd: libc::c_int,
ifreq: ifreq,
mtu: usize,
}
impl AsRawFd for TunTap {
fn as_raw_fd(&self) -> RawFd {
self.fd
}
}
impl TunTap {
pub fn new(name: &str) -> io::Result<TunTap> {
unsafe {
let fd = libc::open(
"/dev/net/tun\0".as_ptr() as *const libc::c_char,
libc::O_RDWR | libc::O_NONBLOCK,
);
if fd == -1 {
return Err(io::Error::last_os_error());
}
let mut ifreq = ifreq_for(name);
ifreq.ifr_data = IFF_TAP | IFF_NO_PI;
ifreq_ioctl(fd, &mut ifreq, TUNSETIFF)?;
let socket = libc::socket(libc::AF_INET, libc::SOCK_DGRAM, libc::IPPROTO_IP);
if socket == -1 {
return Err(io::Error::last_os_error());
}
let ip_mtu = ifreq_ioctl(socket, &mut ifreq, SIOCGIFMTU);
libc::close(socket);
let ip_mtu = ip_mtu? as usize;
// SIOCGIFMTU returns the IP MTU (typically 1500 bytes.)
// smoltcp counts the entire Ethernet packet in the MTU, so add the Ethernet header size to it.
let mtu = ip_mtu + EthernetFrame::<&[u8]>::header_len();
Ok(TunTap { fd, mtu, ifreq })
}
}
}
impl Drop for TunTap {
fn drop(&mut self) {
unsafe {
libc::close(self.fd);
}
}
}
impl io::Read for TunTap {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let len = unsafe { libc::read(self.fd, buf.as_mut_ptr() as *mut libc::c_void, buf.len()) };
if len == -1 {
Err(io::Error::last_os_error())
} else {
Ok(len as usize)
}
}
}
impl io::Write for TunTap {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let len = unsafe { libc::write(self.fd, buf.as_ptr() as *mut libc::c_void, buf.len()) };
if len == -1 {
Err(io::Error::last_os_error())
} else {
Ok(len as usize)
}
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
pub struct TunTapDevice {
device: Async<TunTap>,
waker: WakerRegistration,
}
impl TunTapDevice {
pub fn new(name: &str) -> io::Result<TunTapDevice> {
Ok(Self {
device: Async::new(TunTap::new(name)?)?,
waker: WakerRegistration::new(),
})
}
}
use embassy_net::{LinkState, DeviceCapabilities, Packet, PacketBox, PacketBuf};
use core::task::Waker;
impl crate::Device for TunTapDevice {
fn is_transmit_ready(&mut self) -> bool {
true
}
fn transmit(&mut self, pkt: PacketBuf) {
// todo handle WouldBlock
match self.device.get_mut().write(&pkt) {
Ok(_) => {}
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
info!("transmit WouldBlock");
}
Err(e) => panic!("transmit error: {:?}", e),
}
}
fn receive(&mut self) -> Option<PacketBuf> {
let mut pkt = PacketBox::new(Packet::new()).unwrap();
loop {
match self.device.get_mut().read(&mut pkt[..]) {
Ok(n) => {
return Some(pkt.slice(0..n));
}
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
let ready = if let Some(mut cx) = self.waker.context() {
let ready = self.device.poll_readable(&mut cx).is_ready();
ready
} else {
false
};
if !ready {
return None;
}
}
Err(e) => panic!("read error: {:?}", e),
}
}
}
fn register_waker(&mut self, waker: &Waker) {
self.waker.register(waker)
}
fn capabilities(&mut self) -> DeviceCapabilities {
let mut caps = DeviceCapabilities::default();
caps.max_transmission_unit = self.device.get_ref().mtu;
caps
}
fn link_state(&mut self) -> LinkState {
LinkState::Up
}
}