aboutsummaryrefslogtreecommitdiff
path: root/embassy-net-examples/src/tuntap.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-net-examples/src/tuntap.rs')
-rw-r--r--embassy-net-examples/src/tuntap.rs225
1 files changed, 225 insertions, 0 deletions
diff --git a/embassy-net-examples/src/tuntap.rs b/embassy-net-examples/src/tuntap.rs
new file mode 100644
index 000000000..dd453deb3
--- /dev/null
+++ b/embassy-net-examples/src/tuntap.rs
@@ -0,0 +1,225 @@
1use async_io::Async;
2use libc;
3use log::*;
4use smoltcp::wire::EthernetFrame;
5use std::io;
6use std::io::{Read, Write};
7use std::os::unix::io::{AsRawFd, RawFd};
8
9pub const SIOCGIFMTU: libc::c_ulong = 0x8921;
10pub const SIOCGIFINDEX: libc::c_ulong = 0x8933;
11pub const ETH_P_ALL: libc::c_short = 0x0003;
12pub const TUNSETIFF: libc::c_ulong = 0x400454CA;
13pub const IFF_TUN: libc::c_int = 0x0001;
14pub const IFF_TAP: libc::c_int = 0x0002;
15pub const IFF_NO_PI: libc::c_int = 0x1000;
16
17#[repr(C)]
18#[derive(Debug)]
19struct ifreq {
20 ifr_name: [libc::c_char; libc::IF_NAMESIZE],
21 ifr_data: libc::c_int, /* ifr_ifindex or ifr_mtu */
22}
23
24fn ifreq_for(name: &str) -> ifreq {
25 let mut ifreq = ifreq {
26 ifr_name: [0; libc::IF_NAMESIZE],
27 ifr_data: 0,
28 };
29 for (i, byte) in name.as_bytes().iter().enumerate() {
30 ifreq.ifr_name[i] = *byte as libc::c_char
31 }
32 ifreq
33}
34
35fn ifreq_ioctl(
36 lower: libc::c_int,
37 ifreq: &mut ifreq,
38 cmd: libc::c_ulong,
39) -> io::Result<libc::c_int> {
40 unsafe {
41 let res = libc::ioctl(lower, cmd as _, ifreq as *mut ifreq);
42 if res == -1 {
43 return Err(io::Error::last_os_error());
44 }
45 }
46
47 Ok(ifreq.ifr_data)
48}
49
50#[derive(Debug)]
51pub struct TunTap {
52 fd: libc::c_int,
53 ifreq: ifreq,
54 mtu: usize,
55}
56
57impl AsRawFd for TunTap {
58 fn as_raw_fd(&self) -> RawFd {
59 self.fd
60 }
61}
62
63impl TunTap {
64 pub fn new(name: &str) -> io::Result<TunTap> {
65 unsafe {
66 let fd = libc::open(
67 "/dev/net/tun\0".as_ptr() as *const libc::c_char,
68 libc::O_RDWR | libc::O_NONBLOCK,
69 );
70 if fd == -1 {
71 return Err(io::Error::last_os_error());
72 }
73
74 let mut ifreq = ifreq_for(name);
75 ifreq.ifr_data = IFF_TAP | IFF_NO_PI;
76 ifreq_ioctl(fd, &mut ifreq, TUNSETIFF)?;
77
78 let socket = libc::socket(libc::AF_INET, libc::SOCK_DGRAM, libc::IPPROTO_IP);
79 if socket == -1 {
80 return Err(io::Error::last_os_error());
81 }
82
83 let ip_mtu = ifreq_ioctl(socket, &mut ifreq, SIOCGIFMTU);
84 libc::close(socket);
85 let ip_mtu = ip_mtu? as usize;
86
87 // SIOCGIFMTU returns the IP MTU (typically 1500 bytes.)
88 // smoltcp counts the entire Ethernet packet in the MTU, so add the Ethernet header size to it.
89 let mtu = ip_mtu + EthernetFrame::<&[u8]>::header_len();
90
91 Ok(TunTap { fd, mtu, ifreq })
92 }
93 }
94}
95
96impl Drop for TunTap {
97 fn drop(&mut self) {
98 unsafe {
99 libc::close(self.fd);
100 }
101 }
102}
103
104impl io::Read for TunTap {
105 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
106 let len = unsafe { libc::read(self.fd, buf.as_mut_ptr() as *mut libc::c_void, buf.len()) };
107 if len == -1 {
108 Err(io::Error::last_os_error())
109 } else {
110 Ok(len as usize)
111 }
112 }
113}
114
115impl io::Write for TunTap {
116 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
117 let len = unsafe { libc::write(self.fd, buf.as_ptr() as *mut libc::c_void, buf.len()) };
118 if len == -1 {
119 Err(io::Error::last_os_error())
120 } else {
121 Ok(len as usize)
122 }
123 }
124
125 fn flush(&mut self) -> io::Result<()> {
126 Ok(())
127 }
128}
129
130pub struct TunTapDevice {
131 device: Async<TunTap>,
132 waker: Option<Waker>,
133}
134
135impl TunTapDevice {
136 pub fn new(name: &str) -> io::Result<TunTapDevice> {
137 Ok(Self {
138 device: Async::new(TunTap::new(name)?)?,
139 waker: None,
140 })
141 }
142}
143
144use core::task::Waker;
145use embassy_net::{DeviceCapabilities, LinkState, Packet, PacketBox, PacketBoxExt, PacketBuf};
146use std::task::Context;
147
148impl crate::Device for TunTapDevice {
149 fn is_transmit_ready(&mut self) -> bool {
150 true
151 }
152
153 fn transmit(&mut self, pkt: PacketBuf) {
154 // todo handle WouldBlock
155 match self.device.get_mut().write(&pkt) {
156 Ok(_) => {}
157 Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
158 info!("transmit WouldBlock");
159 }
160 Err(e) => panic!("transmit error: {:?}", e),
161 }
162 }
163
164 fn receive(&mut self) -> Option<PacketBuf> {
165 let mut pkt = PacketBox::new(Packet::new()).unwrap();
166 loop {
167 match self.device.get_mut().read(&mut pkt[..]) {
168 Ok(n) => {
169 return Some(pkt.slice(0..n));
170 }
171 Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
172 let ready = if let Some(w) = self.waker.as_ref() {
173 let mut cx = Context::from_waker(w);
174 let ready = self.device.poll_readable(&mut cx).is_ready();
175 ready
176 } else {
177 false
178 };
179 if !ready {
180 return None;
181 }
182 }
183 Err(e) => panic!("read error: {:?}", e),
184 }
185 }
186 }
187
188 fn register_waker(&mut self, w: &Waker) {
189 match self.waker {
190 // Optimization: If both the old and new Wakers wake the same task, we can simply
191 // keep the old waker, skipping the clone. (In most executor implementations,
192 // cloning a waker is somewhat expensive, comparable to cloning an Arc).
193 Some(ref w2) if (w2.will_wake(w)) => {}
194 _ => {
195 // clone the new waker and store it
196 if let Some(old_waker) = core::mem::replace(&mut self.waker, Some(w.clone())) {
197 // We had a waker registered for another task. Wake it, so the other task can
198 // reregister itself if it's still interested.
199 //
200 // If two tasks are waiting on the same thing concurrently, this will cause them
201 // to wake each other in a loop fighting over this WakerRegistration. This wastes
202 // CPU but things will still work.
203 //
204 // If the user wants to have two tasks waiting on the same thing they should use
205 // a more appropriate primitive that can store multiple wakers.
206 old_waker.wake()
207 }
208 }
209 }
210 }
211
212 fn capabilities(&mut self) -> DeviceCapabilities {
213 let mut caps = DeviceCapabilities::default();
214 caps.max_transmission_unit = self.device.get_ref().mtu;
215 caps
216 }
217
218 fn link_state(&mut self) -> LinkState {
219 LinkState::Up
220 }
221
222 fn ethernet_address(&mut self) -> [u8; 6] {
223 [0x02, 0x03, 0x04, 0x05, 0x06, 0x07]
224 }
225}