mod.rs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //! Access to networking hardware.
  2. //!
  3. //! The `phy` module deals with the *network devices*. It provides an interface
  4. //! for transmitting and receiving frames, [Device](trait.Device.html),
  5. //! as well as an implementations of that trait that uses the host OS,
  6. //! [RawSocket](struct.RawSocket.html) and [TapInterface](struct.TapInterface.html).
  7. use Error;
  8. #[cfg(feature = "use_std")]
  9. mod sys;
  10. mod tracer;
  11. #[cfg(feature = "use_std")]
  12. mod raw_socket;
  13. #[cfg(all(feature = "use_std", target_os = "linux"))]
  14. mod tap_interface;
  15. pub use self::tracer::Tracer;
  16. #[cfg(feature = "use_std")]
  17. pub use self::raw_socket::RawSocket;
  18. #[cfg(all(feature = "use_std", target_os = "linux"))]
  19. pub use self::tap_interface::TapInterface;
  20. /// An interface for sending and receiving raw network frames.
  21. ///
  22. /// It is expected that a `Device` implementation would allocate memory for both sending
  23. /// and receiving packets from memory pools; hence, the stack borrows the buffer for a packet
  24. /// that it is about to receive, as well for a packet that it is about to send, from the device.
  25. pub trait Device {
  26. type RxBuffer: AsRef<[u8]>;
  27. type TxBuffer: AsRef<[u8]> + AsMut<[u8]>;
  28. /// Get maximum transmission unit.
  29. ///
  30. /// The network device is unable to send or receive frames larger than the MTU.
  31. /// In practice, MTU will fall between 576 (for IPv4) or 1280 (for IPv6) and 9216 octets.
  32. fn mtu(&self) -> usize;
  33. /// Receive a frame.
  34. ///
  35. /// It is expected that a `receive` implementation, once a packet is written to memory
  36. /// through DMA, would gain ownership of the underlying buffer, provide it for parsing,
  37. /// and return it to the network device once it is dropped.
  38. fn receive(&mut self) -> Result<Self::RxBuffer, Error>;
  39. /// Transmit a frame.
  40. ///
  41. /// It is expected that a `transmit` implementation would gain ownership of a buffer with
  42. /// the requested length, provide it for emission, and schedule it to be read from
  43. /// memory by the network device once it is dropped.
  44. fn transmit(&mut self, len: usize) -> Result<Self::TxBuffer, Error>;
  45. }