xdp.rs 881 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. use core::ffi::c_void;
  2. use crate::{bindings::xdp_md, BpfContext};
  3. pub struct XdpContext {
  4. pub ctx: *mut xdp_md,
  5. }
  6. impl XdpContext {
  7. pub fn new(ctx: *mut xdp_md) -> XdpContext {
  8. XdpContext { ctx }
  9. }
  10. #[inline]
  11. pub fn data(&self) -> usize {
  12. unsafe { (*self.ctx).data as usize }
  13. }
  14. #[inline]
  15. pub fn data_end(&self) -> usize {
  16. unsafe { (*self.ctx).data_end as usize }
  17. }
  18. /// Return the raw address of the XdpContext metadata.
  19. #[inline(always)]
  20. pub fn metadata(&self) -> usize {
  21. unsafe { (*self.ctx).data_meta as usize }
  22. }
  23. /// Return the raw address immediately after the XdpContext's metadata.
  24. #[inline(always)]
  25. pub fn metadata_end(&self) -> usize {
  26. self.data()
  27. }
  28. }
  29. impl BpfContext for XdpContext {
  30. fn as_ptr(&self) -> *mut c_void {
  31. self.ctx as *mut _
  32. }
  33. }