|
| 1 | +//! Module for the Generic Receiver Offload |
| 2 | +//! |
| 3 | +//! C headers: [`include/net/gro.h`](../../../include/net/gro.h), |
| 4 | +
|
| 5 | +use crate::{ |
| 6 | + bindings, |
| 7 | + net::{Device, SkBuff}, |
| 8 | +}; |
| 9 | +use core::marker::PhantomData; |
| 10 | +use macros::vtable; |
| 11 | + |
| 12 | +/// Abstraction around the kernel's `struct napi_struct` |
| 13 | +/// |
| 14 | +/// For additional documentation about how New API (NAPI) works, consult the |
| 15 | +/// [`Linux Foundation Wiki`](https://wiki.linuxfoundation.org/networking/napi). |
| 16 | +#[repr(transparent)] |
| 17 | +#[derive(Default, Clone, Copy)] |
| 18 | +pub struct Napi(bindings::napi_struct); |
| 19 | + |
| 20 | +/// A trait to implement NAPI Polling Functions |
| 21 | +#[vtable] |
| 22 | +pub trait NapiPoller { |
| 23 | + /// Polling function |
| 24 | + /// |
| 25 | + /// The NAPI structure is given mutably. The network driver should do its |
| 26 | + /// best to receive packets from the interface, and attempt to retrieve at |
| 27 | + /// most `budget` packets, returning the exact number it extracted. |
| 28 | + fn poll(_napi: &mut Napi, _budget: i32) -> i32 { |
| 29 | + // Budget is made into an `i32` because nothing in the kernel forbids |
| 30 | + // drivers from an explicit call to napi_poll(), and their polling |
| 31 | + // functions could return negative values for reasons only they know of. |
| 32 | + 0 |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/// Building structure for poller functions |
| 37 | +pub struct PollerBuilder<T: NapiPoller> { |
| 38 | + _p: PhantomData<T>, |
| 39 | +} |
| 40 | + |
| 41 | +type PollerFunction = unsafe extern "C" fn(*mut bindings::napi_struct, i32) -> i32; |
| 42 | + |
| 43 | +impl<T: NapiPoller> PollerBuilder<T> { |
| 44 | + const FUNC: Option<PollerFunction> = Some(Self::poller_callback); |
| 45 | + |
| 46 | + /// Build the poller function pointer associated with the generics' callback |
| 47 | + pub const fn build_function() -> Option<PollerFunction> { |
| 48 | + Self::FUNC |
| 49 | + } |
| 50 | + |
| 51 | + unsafe extern "C" fn poller_callback(napi: *mut bindings::napi_struct, budget: i32) -> i32 { |
| 52 | + // Try and build the napi from this pointer |
| 53 | + // SAFETY: The kernel will necessarily give us a non-null and valid |
| 54 | + // pointer, so we can dereference it, and use it while satisfying the |
| 55 | + // invariants of `Napi`. Furthermore, the cast is valid because `Napi` |
| 56 | + // is transparent. |
| 57 | + let napi: &mut Napi = unsafe { &mut *napi.cast() }; |
| 58 | + |
| 59 | + // The rest is primitive, hence, trivial |
| 60 | + <T>::poll(napi, budget) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl Napi { |
| 65 | + /// Create a new, empty, NAPI |
| 66 | + pub fn new() -> Self { |
| 67 | + Self(bindings::napi_struct::default()) |
| 68 | + } |
| 69 | + |
| 70 | + /// Obtain the inner pointer cast to the bindings type |
| 71 | + fn get_inner_cast(&mut self) -> *mut bindings::napi_struct { |
| 72 | + (self as *mut Self).cast() |
| 73 | + } |
| 74 | + |
| 75 | + /// Set a bit in the state bitmap of the [`Napi`] to 1 |
| 76 | + pub fn set_state_bit(&mut self, bit: NapiState) { |
| 77 | + let bit_as = u64::from(bit as u32); |
| 78 | + |
| 79 | + self.0.state |= 1 << bit_as; |
| 80 | + } |
| 81 | + |
| 82 | + /// Enable the NAPI |
| 83 | + /// |
| 84 | + /// You must always set a state using [`Self::set_state_bit`] prior to |
| 85 | + /// calling this method. |
| 86 | + pub fn enable(&mut self) { |
| 87 | + let napi_ptr: *mut Napi = self; |
| 88 | + |
| 89 | + // SAFETY: The cast is valid because `Napi` is transparent to that type, |
| 90 | + // and the call is sound because the pointer is guaranteed to be |
| 91 | + // non-null and valid all throughout the lifetime of the call. |
| 92 | + unsafe { bindings::napi_enable(napi_ptr.cast()) }; |
| 93 | + } |
| 94 | + |
| 95 | + /// Disable the NAPI |
| 96 | + pub fn disable(&mut self) { |
| 97 | + let napi_ptr: *mut Napi = self; |
| 98 | + |
| 99 | + // SAFETY: The cast is valid because `Napi` is transparent to that type, |
| 100 | + // and the call is sound because the pointer is guaranteed to be |
| 101 | + // non-null and valid all throughout the lifetime of the call. |
| 102 | + unsafe { bindings::napi_disable(napi_ptr.cast()) }; |
| 103 | + } |
| 104 | + |
| 105 | + /// Schedule the NAPI to run on this CPU |
| 106 | + /// |
| 107 | + /// This is equivalent to calling [`Self::prepare_scheduling`] followed by |
| 108 | + /// [`Self::actually_schedule`] one after the other. |
| 109 | + pub fn schedule(&mut self) { |
| 110 | + // SAFETY: The call is safe because the pointer is guaranteed to be |
| 111 | + // non-null and valid all throughout the call. |
| 112 | + unsafe { bindings::napi_schedule(self.get_inner_cast()) }; |
| 113 | + } |
| 114 | + |
| 115 | + /// Prepare the scheduling of the NAPI |
| 116 | + /// |
| 117 | + /// If the NAPI is already due to be scheduled on this CPU, do nothing |
| 118 | + /// and return `false`. |
| 119 | + /// |
| 120 | + /// Call [`Self::actually_schedule`] if this method returns `true`. |
| 121 | + pub fn prepare_scheduling(&mut self) -> bool { |
| 122 | + // SAFETY: The call is safe because the pointer is guaranteed to be |
| 123 | + // non-null and valid all throughout the call. |
| 124 | + unsafe { bindings::napi_schedule_prep(self.get_inner_cast()) } |
| 125 | + } |
| 126 | + |
| 127 | + /// Actually schedule the NAPI after preparation |
| 128 | + /// |
| 129 | + /// Call [`Self::prepare_scheduling`] prior to calling this method. |
| 130 | + pub fn actually_schedule(&mut self) { |
| 131 | + // SAFETY: The call is safe because the pointer is guaranteed to be |
| 132 | + // non-null and valid all throughout the call. |
| 133 | + unsafe { bindings::__napi_schedule(self.get_inner_cast()) }; |
| 134 | + } |
| 135 | + |
| 136 | + /// Complete after no packets received by the NAPI |
| 137 | + /// |
| 138 | + /// This is equivalent to calling [`Self::complete_done`] with a work of 0. |
| 139 | + pub fn complete(&mut self) -> bool { |
| 140 | + // SAFETY: The call is safe because the pointer is guaranteed to be |
| 141 | + // non-null and valid all throughout the call |
| 142 | + unsafe { bindings::napi_complete_done(self.get_inner_cast(), 0) } |
| 143 | + } |
| 144 | + |
| 145 | + /// Complete with a given number of packets received by the NAPI |
| 146 | + pub fn complete_done(&mut self, work: i32) -> bool { |
| 147 | + // SAFETY: The call is safe because `work` is primitive, and the pointer |
| 148 | + // is guaranteed to be non-null and valid throughout the call's |
| 149 | + // lifetime. |
| 150 | + unsafe { bindings::napi_complete_done(self.get_inner_cast(), work) } |
| 151 | + } |
| 152 | + |
| 153 | + /// Return a reference to the device that the NAPI is currently on, if any |
| 154 | + pub fn get_device(&self) -> Option<&Device> { |
| 155 | + let dev_ptr = self.0.dev; |
| 156 | + if dev_ptr.is_null() { |
| 157 | + None |
| 158 | + } else { |
| 159 | + // SAFETY: We've guaranteed that `dev_ptr` is non-null. The kernel |
| 160 | + // guarantees that it's a pointer to a net_device, and it will stay |
| 161 | + // valid for the duration of the instance given here. |
| 162 | + Some(unsafe { Device::from_ptr(dev_ptr) }) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /// Transmit to the GRO |
| 167 | + pub fn gro_receive(&mut self, sk_buff: &mut SkBuff) -> GroResult { |
| 168 | + let self_ptr = self.get_inner_cast(); |
| 169 | + let skb_ptr: *mut bindings::sk_buff = (sk_buff as *mut SkBuff).cast(); |
| 170 | + |
| 171 | + // SAFETY: The invariants of SkBuff and ourself guarantees that we can |
| 172 | + // use these pointers. |
| 173 | + let res = unsafe { bindings::napi_gro_receive(self_ptr, skb_ptr) }; |
| 174 | + res.try_into() |
| 175 | + .expect("Unable to convert return of napi_gro_receive to gro_result\n") |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +/// Enumerator for the return type of [`SkBuff::gro_receive`] |
| 180 | +#[repr(u32)] |
| 181 | +#[derive(Debug, Clone, Copy)] |
| 182 | +pub enum GroResult { |
| 183 | + /// Merged but not freed |
| 184 | + Merged = bindings::gro_result_GRO_MERGED, |
| 185 | + |
| 186 | + /// Merged and freed |
| 187 | + MergedFree = bindings::gro_result_GRO_MERGED_FREE, |
| 188 | + |
| 189 | + /// Held |
| 190 | + Held = bindings::gro_result_GRO_HELD, |
| 191 | + |
| 192 | + /// Normal |
| 193 | + Normal = bindings::gro_result_GRO_NORMAL, |
| 194 | + |
| 195 | + /// Consumed |
| 196 | + Consumed = bindings::gro_result_GRO_CONSUMED, |
| 197 | +} |
| 198 | + |
| 199 | +impl TryFrom<u32> for GroResult { |
| 200 | + type Error = (); |
| 201 | + fn try_from(u: u32) -> core::result::Result<Self, Self::Error> { |
| 202 | + match u { |
| 203 | + bindings::gro_result_GRO_MERGED => Ok(Self::Merged), |
| 204 | + bindings::gro_result_GRO_MERGED_FREE => Ok(Self::MergedFree), |
| 205 | + bindings::gro_result_GRO_HELD => Ok(Self::Held), |
| 206 | + bindings::gro_result_GRO_NORMAL => Ok(Self::Normal), |
| 207 | + bindings::gro_result_GRO_CONSUMED => Ok(Self::Consumed), |
| 208 | + _ => Err(()), |
| 209 | + } |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +/// Enumerator for the state of a [`Napi`] |
| 214 | +/// |
| 215 | +/// The state of a [`Napi`] must always be set prior to enabling it. |
| 216 | +#[repr(u32)] |
| 217 | +pub enum NapiState { |
| 218 | + /// Poll is scheduled |
| 219 | + Sched = bindings::NAPI_STATE_SCHED, |
| 220 | + |
| 221 | + /// Rescheduling |
| 222 | + Missed = bindings::NAPI_STATE_MISSED, |
| 223 | + |
| 224 | + /// Disable is pending |
| 225 | + Disable = bindings::NAPI_STATE_DISABLE, |
| 226 | + |
| 227 | + /// Netpoll - don't dequeue from poll_list |
| 228 | + Npsvc = bindings::NAPI_STATE_NPSVC, |
| 229 | + |
| 230 | + /// NAPI added to system list |
| 231 | + Listed = bindings::NAPI_STATE_LISTED, |
| 232 | + |
| 233 | + /// Do not add in napi_hash, no busy polling |
| 234 | + NoBusyPoll = bindings::NAPI_STATE_NO_BUSY_POLL, |
| 235 | + |
| 236 | + /// `sk_busy_loop()` owns this NAPI |
| 237 | + InBusyPoll = bindings::NAPI_STATE_IN_BUSY_POLL, |
| 238 | + |
| 239 | + /// Prefer busy-polling over softirqd processing |
| 240 | + PreferBusyPoll = bindings::NAPI_STATE_PREFER_BUSY_POLL, |
| 241 | + |
| 242 | + /// The poll is performed inside its own thread |
| 243 | + Threaded = bindings::NAPI_STATE_THREADED, |
| 244 | + |
| 245 | + /// NAPI is currently scheduled in threaded mode |
| 246 | + SchedThreaded = bindings::NAPI_STATE_SCHED_THREADED, |
| 247 | +} |
| 248 | + |
| 249 | +impl From<NapiState> for u32 { |
| 250 | + fn from(n: NapiState) -> u32 { |
| 251 | + n as u32 |
| 252 | + } |
| 253 | +} |
0 commit comments