diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-08-03 14:23:11 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-08-03 14:23:11 +0200 |
| commit | 4d60c715e683aaadf25d9f066bde805c725fefb4 (patch) | |
| tree | d3601429cef8850fba4a99df0e148da19fd96efc /examples/std | |
| parent | 2c96fe917de6e0120053e80d8da5a98d0d0f35d0 (diff) | |
net: move tuntap from std example to separate crate. (#1737)
Diffstat (limited to 'examples/std')
| -rw-r--r-- | examples/std/Cargo.toml | 3 | ||||
| -rw-r--r-- | examples/std/src/bin/net.rs | 5 | ||||
| -rw-r--r-- | examples/std/src/bin/net_dns.rs | 5 | ||||
| -rw-r--r-- | examples/std/src/bin/net_udp.rs | 5 | ||||
| -rw-r--r-- | examples/std/src/bin/tcp_accept.rs | 5 | ||||
| -rw-r--r-- | examples/std/src/tuntap.rs | 224 |
6 files changed, 5 insertions, 242 deletions
diff --git a/examples/std/Cargo.toml b/examples/std/Cargo.toml index 42adede10..544176828 100644 --- a/examples/std/Cargo.toml +++ b/examples/std/Cargo.toml | |||
| @@ -9,7 +9,7 @@ embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["lo | |||
| 9 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["arch-std", "executor-thread", "log", "nightly", "integrated-timers"] } | 9 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["arch-std", "executor-thread", "log", "nightly", "integrated-timers"] } |
| 10 | embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["log", "std", "nightly"] } | 10 | embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["log", "std", "nightly"] } |
| 11 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features=[ "std", "nightly", "log", "medium-ethernet", "tcp", "udp", "dns", "dhcpv4", "unstable-traits", "proto-ipv6"] } | 11 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features=[ "std", "nightly", "log", "medium-ethernet", "tcp", "udp", "dns", "dhcpv4", "unstable-traits", "proto-ipv6"] } |
| 12 | embassy-net-driver = { version = "0.1.0", path = "../../embassy-net-driver" } | 12 | embassy-net-tuntap = { version = "0.1.0", path = "../../embassy-net-tuntap" } |
| 13 | embedded-io = { version = "0.4.0", features = ["async", "std", "futures"] } | 13 | embedded-io = { version = "0.4.0", features = ["async", "std", "futures"] } |
| 14 | critical-section = { version = "1.1", features = ["std"] } | 14 | critical-section = { version = "1.1", features = ["std"] } |
| 15 | 15 | ||
| @@ -18,7 +18,6 @@ env_logger = "0.9.0" | |||
| 18 | futures = { version = "0.3.17" } | 18 | futures = { version = "0.3.17" } |
| 19 | log = "0.4.14" | 19 | log = "0.4.14" |
| 20 | nix = "0.26.2" | 20 | nix = "0.26.2" |
| 21 | libc = "0.2.101" | ||
| 22 | clap = { version = "3.0.0-beta.5", features = ["derive"] } | 21 | clap = { version = "3.0.0-beta.5", features = ["derive"] } |
| 23 | rand_core = { version = "0.6.3", features = ["std"] } | 22 | rand_core = { version = "0.6.3", features = ["std"] } |
| 24 | heapless = { version = "0.7.5", default-features = false } | 23 | heapless = { version = "0.7.5", default-features = false } |
diff --git a/examples/std/src/bin/net.rs b/examples/std/src/bin/net.rs index 3aadb029d..e0de14162 100644 --- a/examples/std/src/bin/net.rs +++ b/examples/std/src/bin/net.rs | |||
| @@ -6,6 +6,7 @@ use clap::Parser; | |||
| 6 | use embassy_executor::{Executor, Spawner}; | 6 | use embassy_executor::{Executor, Spawner}; |
| 7 | use embassy_net::tcp::TcpSocket; | 7 | use embassy_net::tcp::TcpSocket; |
| 8 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources}; | 8 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources}; |
| 9 | use embassy_net_tuntap::TunTapDevice; | ||
| 9 | use embassy_time::Duration; | 10 | use embassy_time::Duration; |
| 10 | use embedded_io::asynch::Write; | 11 | use embedded_io::asynch::Write; |
| 11 | use heapless::Vec; | 12 | use heapless::Vec; |
| @@ -13,10 +14,6 @@ use log::*; | |||
| 13 | use rand_core::{OsRng, RngCore}; | 14 | use rand_core::{OsRng, RngCore}; |
| 14 | use static_cell::{make_static, StaticCell}; | 15 | use static_cell::{make_static, StaticCell}; |
| 15 | 16 | ||
| 16 | #[path = "../tuntap.rs"] | ||
| 17 | mod tuntap; | ||
| 18 | |||
| 19 | use crate::tuntap::TunTapDevice; | ||
| 20 | #[derive(Parser)] | 17 | #[derive(Parser)] |
| 21 | #[clap(version = "1.0")] | 18 | #[clap(version = "1.0")] |
| 22 | struct Opts { | 19 | struct Opts { |
diff --git a/examples/std/src/bin/net_dns.rs b/examples/std/src/bin/net_dns.rs index 65b5a2cd9..6c19874d5 100644 --- a/examples/std/src/bin/net_dns.rs +++ b/examples/std/src/bin/net_dns.rs | |||
| @@ -6,15 +6,12 @@ use clap::Parser; | |||
| 6 | use embassy_executor::{Executor, Spawner}; | 6 | use embassy_executor::{Executor, Spawner}; |
| 7 | use embassy_net::dns::DnsQueryType; | 7 | use embassy_net::dns::DnsQueryType; |
| 8 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources}; | 8 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources}; |
| 9 | use embassy_net_tuntap::TunTapDevice; | ||
| 9 | use heapless::Vec; | 10 | use heapless::Vec; |
| 10 | use log::*; | 11 | use log::*; |
| 11 | use rand_core::{OsRng, RngCore}; | 12 | use rand_core::{OsRng, RngCore}; |
| 12 | use static_cell::{make_static, StaticCell}; | 13 | use static_cell::{make_static, StaticCell}; |
| 13 | 14 | ||
| 14 | #[path = "../tuntap.rs"] | ||
| 15 | mod tuntap; | ||
| 16 | |||
| 17 | use crate::tuntap::TunTapDevice; | ||
| 18 | #[derive(Parser)] | 15 | #[derive(Parser)] |
| 19 | #[clap(version = "1.0")] | 16 | #[clap(version = "1.0")] |
| 20 | struct Opts { | 17 | struct Opts { |
diff --git a/examples/std/src/bin/net_udp.rs b/examples/std/src/bin/net_udp.rs index 3fc46156c..98dcc9925 100644 --- a/examples/std/src/bin/net_udp.rs +++ b/examples/std/src/bin/net_udp.rs | |||
| @@ -4,15 +4,12 @@ use clap::Parser; | |||
| 4 | use embassy_executor::{Executor, Spawner}; | 4 | use embassy_executor::{Executor, Spawner}; |
| 5 | use embassy_net::udp::{PacketMetadata, UdpSocket}; | 5 | use embassy_net::udp::{PacketMetadata, UdpSocket}; |
| 6 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources}; | 6 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources}; |
| 7 | use embassy_net_tuntap::TunTapDevice; | ||
| 7 | use heapless::Vec; | 8 | use heapless::Vec; |
| 8 | use log::*; | 9 | use log::*; |
| 9 | use rand_core::{OsRng, RngCore}; | 10 | use rand_core::{OsRng, RngCore}; |
| 10 | use static_cell::{make_static, StaticCell}; | 11 | use static_cell::{make_static, StaticCell}; |
| 11 | 12 | ||
| 12 | #[path = "../tuntap.rs"] | ||
| 13 | mod tuntap; | ||
| 14 | |||
| 15 | use crate::tuntap::TunTapDevice; | ||
| 16 | #[derive(Parser)] | 13 | #[derive(Parser)] |
| 17 | #[clap(version = "1.0")] | 14 | #[clap(version = "1.0")] |
| 18 | struct Opts { | 15 | struct Opts { |
diff --git a/examples/std/src/bin/tcp_accept.rs b/examples/std/src/bin/tcp_accept.rs index df09986ac..0c920a3fb 100644 --- a/examples/std/src/bin/tcp_accept.rs +++ b/examples/std/src/bin/tcp_accept.rs | |||
| @@ -7,6 +7,7 @@ use clap::Parser; | |||
| 7 | use embassy_executor::{Executor, Spawner}; | 7 | use embassy_executor::{Executor, Spawner}; |
| 8 | use embassy_net::tcp::TcpSocket; | 8 | use embassy_net::tcp::TcpSocket; |
| 9 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources}; | 9 | use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources}; |
| 10 | use embassy_net_tuntap::TunTapDevice; | ||
| 10 | use embassy_time::{Duration, Timer}; | 11 | use embassy_time::{Duration, Timer}; |
| 11 | use embedded_io::asynch::Write as _; | 12 | use embedded_io::asynch::Write as _; |
| 12 | use heapless::Vec; | 13 | use heapless::Vec; |
| @@ -14,10 +15,6 @@ use log::*; | |||
| 14 | use rand_core::{OsRng, RngCore}; | 15 | use rand_core::{OsRng, RngCore}; |
| 15 | use static_cell::{make_static, StaticCell}; | 16 | use static_cell::{make_static, StaticCell}; |
| 16 | 17 | ||
| 17 | #[path = "../tuntap.rs"] | ||
| 18 | mod tuntap; | ||
| 19 | |||
| 20 | use crate::tuntap::TunTapDevice; | ||
| 21 | #[derive(Parser)] | 18 | #[derive(Parser)] |
| 22 | #[clap(version = "1.0")] | 19 | #[clap(version = "1.0")] |
| 23 | struct Opts { | 20 | struct Opts { |
diff --git a/examples/std/src/tuntap.rs b/examples/std/src/tuntap.rs deleted file mode 100644 index 167c3da5f..000000000 --- a/examples/std/src/tuntap.rs +++ /dev/null | |||
| @@ -1,224 +0,0 @@ | |||
| 1 | use std::io; | ||
| 2 | use std::io::{Read, Write}; | ||
| 3 | use std::os::unix::io::{AsRawFd, RawFd}; | ||
| 4 | use std::task::Context; | ||
| 5 | |||
| 6 | use async_io::Async; | ||
| 7 | use embassy_net_driver::{self, Capabilities, Driver, HardwareAddress, LinkState}; | ||
| 8 | use log::*; | ||
| 9 | |||
| 10 | pub const SIOCGIFMTU: libc::c_ulong = 0x8921; | ||
| 11 | pub const _SIOCGIFINDEX: libc::c_ulong = 0x8933; | ||
| 12 | pub const _ETH_P_ALL: libc::c_short = 0x0003; | ||
| 13 | pub const TUNSETIFF: libc::c_ulong = 0x400454CA; | ||
| 14 | pub const _IFF_TUN: libc::c_int = 0x0001; | ||
| 15 | pub const IFF_TAP: libc::c_int = 0x0002; | ||
| 16 | pub const IFF_NO_PI: libc::c_int = 0x1000; | ||
| 17 | |||
| 18 | const ETHERNET_HEADER_LEN: usize = 14; | ||
| 19 | |||
| 20 | #[repr(C)] | ||
| 21 | #[derive(Debug)] | ||
| 22 | struct ifreq { | ||
| 23 | ifr_name: [libc::c_char; libc::IF_NAMESIZE], | ||
| 24 | ifr_data: libc::c_int, /* ifr_ifindex or ifr_mtu */ | ||
| 25 | } | ||
| 26 | |||
| 27 | fn ifreq_for(name: &str) -> ifreq { | ||
| 28 | let mut ifreq = ifreq { | ||
| 29 | ifr_name: [0; libc::IF_NAMESIZE], | ||
| 30 | ifr_data: 0, | ||
| 31 | }; | ||
| 32 | for (i, byte) in name.as_bytes().iter().enumerate() { | ||
| 33 | ifreq.ifr_name[i] = *byte as libc::c_char | ||
| 34 | } | ||
| 35 | ifreq | ||
| 36 | } | ||
| 37 | |||
| 38 | fn ifreq_ioctl(lower: libc::c_int, ifreq: &mut ifreq, cmd: libc::c_ulong) -> io::Result<libc::c_int> { | ||
| 39 | unsafe { | ||
| 40 | let res = libc::ioctl(lower, cmd as _, ifreq as *mut ifreq); | ||
| 41 | if res == -1 { | ||
| 42 | return Err(io::Error::last_os_error()); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | Ok(ifreq.ifr_data) | ||
| 47 | } | ||
| 48 | |||
| 49 | #[derive(Debug)] | ||
| 50 | pub struct TunTap { | ||
| 51 | fd: libc::c_int, | ||
| 52 | mtu: usize, | ||
| 53 | } | ||
| 54 | |||
| 55 | impl AsRawFd for TunTap { | ||
| 56 | fn as_raw_fd(&self) -> RawFd { | ||
| 57 | self.fd | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | impl TunTap { | ||
| 62 | pub fn new(name: &str) -> io::Result<TunTap> { | ||
| 63 | unsafe { | ||
| 64 | let fd = libc::open( | ||
| 65 | "/dev/net/tun\0".as_ptr() as *const libc::c_char, | ||
| 66 | libc::O_RDWR | libc::O_NONBLOCK, | ||
| 67 | ); | ||
| 68 | if fd == -1 { | ||
| 69 | return Err(io::Error::last_os_error()); | ||
| 70 | } | ||
| 71 | |||
| 72 | let mut ifreq = ifreq_for(name); | ||
| 73 | ifreq.ifr_data = IFF_TAP | IFF_NO_PI; | ||
| 74 | ifreq_ioctl(fd, &mut ifreq, TUNSETIFF)?; | ||
| 75 | |||
| 76 | let socket = libc::socket(libc::AF_INET, libc::SOCK_DGRAM, libc::IPPROTO_IP); | ||
| 77 | if socket == -1 { | ||
| 78 | return Err(io::Error::last_os_error()); | ||
| 79 | } | ||
| 80 | |||
| 81 | let ip_mtu = ifreq_ioctl(socket, &mut ifreq, SIOCGIFMTU); | ||
| 82 | libc::close(socket); | ||
| 83 | let ip_mtu = ip_mtu? as usize; | ||
| 84 | |||
| 85 | // SIOCGIFMTU returns the IP MTU (typically 1500 bytes.) | ||
| 86 | // smoltcp counts the entire Ethernet packet in the MTU, so add the Ethernet header size to it. | ||
| 87 | let mtu = ip_mtu + ETHERNET_HEADER_LEN; | ||
| 88 | |||
| 89 | Ok(TunTap { fd, mtu }) | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | impl Drop for TunTap { | ||
| 95 | fn drop(&mut self) { | ||
| 96 | unsafe { | ||
| 97 | libc::close(self.fd); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | impl io::Read for TunTap { | ||
| 103 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||
| 104 | let len = unsafe { libc::read(self.fd, buf.as_mut_ptr() as *mut libc::c_void, buf.len()) }; | ||
| 105 | if len == -1 { | ||
| 106 | Err(io::Error::last_os_error()) | ||
| 107 | } else { | ||
| 108 | Ok(len as usize) | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | impl io::Write for TunTap { | ||
| 114 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { | ||
| 115 | let len = unsafe { libc::write(self.fd, buf.as_ptr() as *mut libc::c_void, buf.len()) }; | ||
| 116 | if len == -1 { | ||
| 117 | Err(io::Error::last_os_error()) | ||
| 118 | } else { | ||
| 119 | Ok(len as usize) | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | fn flush(&mut self) -> io::Result<()> { | ||
| 124 | Ok(()) | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | pub struct TunTapDevice { | ||
| 129 | device: Async<TunTap>, | ||
| 130 | } | ||
| 131 | |||
| 132 | impl TunTapDevice { | ||
| 133 | pub fn new(name: &str) -> io::Result<TunTapDevice> { | ||
| 134 | Ok(Self { | ||
| 135 | device: Async::new(TunTap::new(name)?)?, | ||
| 136 | }) | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | impl Driver for TunTapDevice { | ||
| 141 | type RxToken<'a> = RxToken where Self: 'a; | ||
| 142 | type TxToken<'a> = TxToken<'a> where Self: 'a; | ||
| 143 | |||
| 144 | fn receive(&mut self, cx: &mut Context) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> { | ||
| 145 | let mut buf = vec![0; self.device.get_ref().mtu]; | ||
| 146 | loop { | ||
| 147 | match self.device.get_mut().read(&mut buf) { | ||
| 148 | Ok(n) => { | ||
| 149 | buf.truncate(n); | ||
| 150 | return Some(( | ||
| 151 | RxToken { buffer: buf }, | ||
| 152 | TxToken { | ||
| 153 | device: &mut self.device, | ||
| 154 | }, | ||
| 155 | )); | ||
| 156 | } | ||
| 157 | Err(e) if e.kind() == io::ErrorKind::WouldBlock => { | ||
| 158 | if !self.device.poll_readable(cx).is_ready() { | ||
| 159 | return None; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | Err(e) => panic!("read error: {:?}", e), | ||
| 163 | } | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | fn transmit(&mut self, _cx: &mut Context) -> Option<Self::TxToken<'_>> { | ||
| 168 | Some(TxToken { | ||
| 169 | device: &mut self.device, | ||
| 170 | }) | ||
| 171 | } | ||
| 172 | |||
| 173 | fn capabilities(&self) -> Capabilities { | ||
| 174 | let mut caps = Capabilities::default(); | ||
| 175 | caps.max_transmission_unit = self.device.get_ref().mtu; | ||
| 176 | caps | ||
| 177 | } | ||
| 178 | |||
| 179 | fn link_state(&mut self, _cx: &mut Context) -> LinkState { | ||
| 180 | LinkState::Up | ||
| 181 | } | ||
| 182 | |||
| 183 | fn hardware_address(&self) -> HardwareAddress { | ||
| 184 | HardwareAddress::Ethernet([0x02, 0x03, 0x04, 0x05, 0x06, 0x07]) | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | #[doc(hidden)] | ||
| 189 | pub struct RxToken { | ||
| 190 | buffer: Vec<u8>, | ||
| 191 | } | ||
| 192 | |||
| 193 | impl embassy_net_driver::RxToken for RxToken { | ||
| 194 | fn consume<R, F>(mut self, f: F) -> R | ||
| 195 | where | ||
| 196 | F: FnOnce(&mut [u8]) -> R, | ||
| 197 | { | ||
| 198 | f(&mut self.buffer) | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | #[doc(hidden)] | ||
| 203 | pub struct TxToken<'a> { | ||
| 204 | device: &'a mut Async<TunTap>, | ||
| 205 | } | ||
| 206 | |||
| 207 | impl<'a> embassy_net_driver::TxToken for TxToken<'a> { | ||
| 208 | fn consume<R, F>(self, len: usize, f: F) -> R | ||
| 209 | where | ||
| 210 | F: FnOnce(&mut [u8]) -> R, | ||
| 211 | { | ||
| 212 | let mut buffer = vec![0; len]; | ||
| 213 | let result = f(&mut buffer); | ||
| 214 | |||
| 215 | // todo handle WouldBlock with async | ||
| 216 | match self.device.get_mut().write(&buffer) { | ||
| 217 | Ok(_) => {} | ||
| 218 | Err(e) if e.kind() == io::ErrorKind::WouldBlock => info!("transmit WouldBlock"), | ||
| 219 | Err(e) => panic!("transmit error: {:?}", e), | ||
| 220 | } | ||
| 221 | |||
| 222 | result | ||
| 223 | } | ||
| 224 | } | ||
