xarxa-driver 0.2.0

Driver trait for the `xarxa` network stack.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#![cfg_attr(not(test), no_std)]
#![doc = include_str!("../README.md")]
//!
//! ## Feature flags
#![doc = document_features::document_features!(feature_label = r#"<span class="stab portability"><code>{feature}</code></span>"#)]

mod buf;
mod meta;

#[cfg(feature = "async")]
use core::task::Waker;

pub use buf::{DriverBuf, RawDriverBuf};
pub use meta::PacketMeta;
#[cfg(feature = "packetmeta-timestamp")]
pub use meta::{Timestamp, TxTimestamp};

/// Error returned by an operation the driver does not support.
#[cfg(feature = "async")]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub struct NotSupported;

/// Link state of a network device.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum LinkState {
    /// The link is down. No frames can pass.
    Down,
    /// The link is up.
    Up,
}

/// Type of medium of a network device.
///
/// This is `#[non_exhaustive]` so that media can be added later without breaking
/// every driver.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
#[non_exhaustive]
pub enum Medium {
    /// Ethernet medium. Devices of this type send and receive Ethernet frames.
    Ethernet,

    /// IP medium. Devices of this type send and receive IP frames, without an
    /// Ethernet header. MAC addresses are not used.
    Ip,

    /// IEEE 802.15.4 medium. Devices of this type send and receive 802.15.4
    /// MAC frames carrying 6LoWPAN.
    ///
    /// [`Capabilities::max_transmission_unit`] is the whole MAC frame
    /// without the FCS: 125 for a 127-byte PHY frame with a 2-byte FCS.
    Ieee802154,
}

/// A hardware (link-layer) address, as reported by a driver.
///
/// This is `#[non_exhaustive]` so that address kinds can be added later without
/// breaking every driver.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum HardwareAddress {
    /// An Ethernet (MAC) address.
    Ethernet([u8; 6]),
    /// No address, for devices that send and receive bare IP packets.
    Ip,
    /// An IEEE 802.15.4 extended (64-bit) address.
    Ieee802154([u8; 8]),
}

impl HardwareAddress {
    /// The medium this kind of address belongs to.
    pub const fn medium(&self) -> Medium {
        match self {
            HardwareAddress::Ethernet(_) => Medium::Ethernet,
            HardwareAddress::Ip => Medium::Ip,
            HardwareAddress::Ieee802154(_) => Medium::Ieee802154,
        }
    }
}

/// Checksum offload capabilities for a given protocol, per direction.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ChecksumOffload {
    /// The device verifies the checksum of received packets.
    ///
    /// The stack then does not verify it in software.
    pub rx: bool,
    /// The device fills in the checksum of transmitted packets.
    ///
    /// The stack then writes the field as zero instead of computing it.
    pub tx: bool,
}

impl ChecksumOffload {
    /// No offload. The stack computes and verifies the checksum in software.
    pub const NONE: Self = Self { rx: false, tx: false };
    /// Offload in both directions.
    pub const BOTH: Self = Self { rx: true, tx: true };
}

/// Checksum offload capabilities per protocol direction.
///
/// The stack skips the offloaded work in software.
///
/// The default is no offload for every protocol: the stack computes and
/// verifies everything itself.
///
/// A checksum the stack does not compute is written as zero, so that a device
/// that fills it in finds a known value there.
///
/// The fields are the protocols with a checksum the stack handles. IGMP is not
/// among them: its checksum is always computed and verified in software.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct ChecksumCapabilities {
    /// Offload for the IPv4 header checksum.
    pub ipv4: ChecksumOffload,
    /// Offload for the UDP checksum.
    pub udp: ChecksumOffload,
    /// Offload for the TCP checksum.
    pub tcp: ChecksumOffload,
    /// Offload for the ICMPv4 checksum.
    pub icmpv4: ChecksumOffload,
    /// Offload for the ICMPv6 checksum.
    pub icmpv6: ChecksumOffload,
}

impl ChecksumCapabilities {
    /// Every checksum offloaded in both directions.
    ///
    /// The stack computes and verifies nothing. Use this for devices where
    /// checksums don't matter, like loopback.
    pub fn all_offloaded() -> Self {
        ChecksumCapabilities {
            ipv4: ChecksumOffload::BOTH,
            udp: ChecksumOffload::BOTH,
            tcp: ChecksumOffload::BOTH,
            icmpv4: ChecksumOffload::BOTH,
            icmpv6: ChecksumOffload::BOTH,
        }
    }
}

/// A description of a device's capabilities.
///
/// This is `#[non_exhaustive]` so that capabilities can be added later without breaking
/// every driver. Drivers live outside this crate and so cannot use a struct expression,
/// they start from [`Default`] and overwrite the fields they care about:
///
/// ```
/// # use xarxa_driver::Capabilities;
/// let mut caps = Capabilities::default();
/// caps.max_transmission_unit = 1514;
/// // caps.medium = Medium::Ethernet; is the default
/// ```
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Capabilities {
    /// Medium of the device.
    pub medium: Medium,

    /// Maximum transmission unit.
    ///
    /// The network device is unable to send or receive frames larger than the value returned
    /// by this function.
    pub max_transmission_unit: usize,

    /// Checksum offload.
    ///
    /// Which checksums the device's hardware verifies or computes, so the
    /// stack skips them in software.
    pub checksum: ChecksumCapabilities,

    /// Headroom the driver needs in front of each frame it transmits, in bytes.
    ///
    /// For drivers that put a header of their own in front of the frame. Every
    /// buffer the stack hands to [`transmit`](Driver::transmit) has at least this
    /// much headroom.
    ///
    /// Default: 0.
    pub tx_headroom: usize,
}

impl Default for Capabilities {
    fn default() -> Self {
        Self {
            medium: Medium::Ethernet,
            max_transmission_unit: 1514,
            checksum: ChecksumCapabilities::default(),
            tx_headroom: 0,
        }
    }
}

/// What a driver answers to [`Driver::rx_wanted`].
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct RxWanted {
    /// How many more empty buffers the driver can take right now.
    pub count: usize,
    /// The driver has no buffer to receive a frame into, so it can't receive
    /// anything until it gets one. The stack keeps a buffer free for each
    /// starved driver.
    ///
    /// `count` may be 0 then, for a driver that only takes a buffer once a frame
    /// is waiting for it. The buffer is kept free for it all the same.
    pub starved: bool,
}

/// A network device driver, sending and receiving raw network frames.
pub trait Driver {
    /// Get a description of the device's capabilities.
    fn capabilities(&self) -> Capabilities;

    /// Get the device's hardware address.
    ///
    /// The address kind must match the medium in [`capabilities`](Self::capabilities):
    /// an Ethernet address for [`Medium::Ethernet`], [`HardwareAddress::Ip`] for
    /// [`Medium::Ip`], an IEEE 802.15.4 extended address for [`Medium::Ieee802154`].
    ///
    /// The stack reads it once, when the driver is added to it. The stack has its
    /// own way to override the address after that.
    fn hardware_address(&self) -> HardwareAddress;

    /// Get the link state.
    ///
    /// Devices that cannot tell, or whose link is always up, return
    /// [`LinkState::Up`], which is the default implementation.
    fn link_state(&mut self) -> LinkState {
        LinkState::Up
    }

    /// Register a waker.
    ///
    /// The driver must wake it when:
    /// - a frame has been received, so [`receive`](Self::receive) may return `Some`.
    /// - there is room to transmit again, after [`can_transmit`](Self::can_transmit)
    ///   returned `false`.
    /// - the link state changed, so [`link_state`](Self::link_state) may return
    ///   something new.
    ///
    /// Only one waker is kept. Registering another replaces it. Wakes are
    /// allowed to be spurious.
    ///
    /// A registered waker is woken just once. The main loop must register it
    /// again if it wants to be woken again.
    ///
    /// # Errors
    /// - `NotSupported`: if the driver cannot wake anything. This is the default
    ///   implementation. Such a driver can only be polled, so a caller that
    ///   needs to sleep until the driver has something new cannot use it.
    #[cfg(feature = "async")]
    fn register_waker(&mut self, waker: &Waker) -> Result<(), NotSupported> {
        let _ = waker;
        Err(NotSupported)
    }

    /// How many more empty buffers the driver can take right now, to receive
    /// frames into, and whether it can receive at all.
    ///
    /// The stack asks on poll, after each call to [`receive`](Self::receive),
    /// and gives the driver up to [`count`](RxWanted::count) buffers with
    /// [`rx_give`](Self::rx_give). It gives fewer if its pool runs low.
    ///
    /// A driver with a receive ring returns the number of slots that have no
    /// buffer. One that reads frames into a buffer on demand can ask for one.
    ///
    /// Set [`starved`](RxWanted::starved) whenever the driver can't receive a
    /// frame without another buffer, whether or not a frame is waiting. The
    /// stack keeps a buffer aside for each starved driver, so that it can always
    /// give it one, but only one. It must be honest: a driver that says it is
    /// starved when it isn't takes buffers from the other interfaces, and one
    /// that doesn't say it when it is can be left with none.
    fn rx_wanted(&mut self) -> RxWanted;

    /// Give the driver an empty buffer to receive a frame into.
    ///
    /// The driver owns it from then on. It hands it back with a frame in it from
    /// [`receive`](Self::receive).
    ///
    /// It shouldn't drop it, except to drop a frame it received into it that it
    /// doesn't hand back. The stack keeps a buffer free for a driver only while
    /// the driver is starved, and it learns that from
    /// [`rx_wanted`](Self::rx_wanted), which it asks after each `receive`. A
    /// driver that drops its buffers some other way can be left with none to
    /// receive into while the buffers that are free are all taken, and then
    /// nothing guarantees it gets one again.
    ///
    /// The stack never gives more buffers than [`rx_wanted`](Self::rx_wanted) asked
    /// for. The buffer has zero headroom and length.
    fn rx_give(&mut self, buf: DriverBuf);

    /// Poll for a received frame.
    ///
    /// Returns a buffer holding the received frame if one is available, transferring
    /// ownership of it to the caller.
    ///
    /// A driver that has per-packet metadata to report, such as an identifier or a
    /// receive timestamp, sets it on the buffer's [`PacketMeta`] here. It travels
    /// with the packet up to the socket that receives it.
    fn receive(&mut self) -> Option<DriverBuf>;

    /// Whether the device can transmit one frame right now.
    ///
    /// Devices typically have a transmit packet queue. This returns
    /// whether this queue has space to take one more frame.
    ///
    /// If this returns `true`, the next `transmit()` call must not fail.
    ///
    /// If this returns `false`, the driver must wake the waker from
    /// `register_waker` once there is room again.
    ///
    /// In devices where there's no queue so transmit always succeeds, this
    /// should always return `true`.
    fn can_transmit(&mut self) -> bool;

    /// Queue a frame for transmission, transferring ownership of the buffer to the driver.
    ///
    /// The driver holds the buffer until the hardware is done with it, then drops it.
    ///
    /// The buffer's [`PacketMeta`] is whatever the sending socket attached to the
    /// packet (default for packets the stack generates itself). A packet sent in
    /// fragments has it on its first fragment, and the default on the others. A
    /// driver that supports transmit timestamping timestamps the frame if
    /// [`request_timestamp`](PacketMeta::request_timestamp) is set, and reports
    /// the result from [`poll_tx_timestamp`](Self::poll_tx_timestamp) tagged with the
    /// packet's [`id`](PacketMeta::id).
    ///
    /// # Errors
    /// - `DriverBuf`: the buffer itself, handed back if the frame cannot be
    ///   queued right now (device busy or queue full).
    fn transmit(&mut self, buf: DriverBuf) -> Result<(), DriverBuf>;

    /// Poll for the timestamp of an already-transmitted packet.
    ///
    /// Returns the transmit timestamp of a packet previously sent with
    /// [`PacketMeta::request_timestamp`] set, tagged with that packet's
    /// [`PacketMeta::id`], or `None` if no timestamp is available right now.
    ///
    /// Transmit timestamps are reported out of band, rather than on the packet like
    /// receive timestamps are, because a packet's transmit timestamp does not exist yet
    /// when [`transmit`](Self::transmit) returns: it has not gone out on the wire yet.
    ///
    /// Callers must be robust against all of the following:
    ///
    /// * Timestamps become available an arbitrary time after `transmit` returned, so
    ///   this should be polled repeatedly, not just once after sending.
    /// * Timestamps may be reported out of order with respect to transmission.
    /// * Timestamps may never arrive at all, e.g. because the hardware ran out of
    ///   timestamp slots. Never block waiting for a particular `id` to show up without
    ///   a timeout.
    ///
    /// Devices that do not support transmit timestamping always return `None`, which is
    /// the default implementation.
    #[cfg(feature = "packetmeta-timestamp")]
    fn poll_tx_timestamp(&mut self) -> Option<TxTimestamp> {
        None
    }

    /// Set the device's multicast hardware address filter.
    ///
    /// `addrs` is the full list of multicast MAC addresses to listen on. It
    /// replaces the previous one.
    ///
    /// A device with no multicast filter can ignore the calls, which
    /// is the default implementation.
    ///
    /// Only called for [`Medium::Ethernet`] devices. The list has no duplicates.
    ///
    /// It may be the same list as last time: the stack does not compare. If
    /// applying the filter is expensive, the driver should should keep the last
    /// list and skip if there were no changes.
    ///
    /// If the list does not fit the filter, receive the addresses anyway if
    /// possible, for example by turning the filter off or switching it to
    /// receive all multicast. Losing filter efficiency is fine, filtering out
    /// traffic the network stack wants is not.
    fn set_multicast_filter(&mut self, addrs: &[[u8; 6]]) {
        let _ = addrs;
    }
}

impl<T: Driver + ?Sized> Driver for &mut T {
    fn capabilities(&self) -> Capabilities {
        T::capabilities(self)
    }
    fn hardware_address(&self) -> HardwareAddress {
        T::hardware_address(self)
    }
    fn link_state(&mut self) -> LinkState {
        T::link_state(self)
    }
    #[cfg(feature = "async")]
    fn register_waker(&mut self, waker: &Waker) -> Result<(), NotSupported> {
        T::register_waker(self, waker)
    }
    fn rx_wanted(&mut self) -> RxWanted {
        T::rx_wanted(self)
    }
    fn rx_give(&mut self, buf: DriverBuf) {
        T::rx_give(self, buf)
    }
    fn receive(&mut self) -> Option<DriverBuf> {
        T::receive(self)
    }
    fn can_transmit(&mut self) -> bool {
        T::can_transmit(self)
    }
    fn transmit(&mut self, buf: DriverBuf) -> Result<(), DriverBuf> {
        T::transmit(self, buf)
    }
    #[cfg(feature = "packetmeta-timestamp")]
    fn poll_tx_timestamp(&mut self) -> Option<TxTimestamp> {
        T::poll_tx_timestamp(self)
    }
    fn set_multicast_filter(&mut self, addrs: &[[u8; 6]]) {
        T::set_multicast_filter(self, addrs)
    }
}