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
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//! Transmit and Receive queue API objects.
use core::marker::PhantomData;
use netstack3_base::{
ContextPair, Device, DeviceIdContext, ResourceCounterContext, WorkQueueReport,
};
use crate::internal::base::DeviceSendFrameError;
use crate::internal::queue::rx::{
ReceiveDequeContext, ReceiveDequeFrameContext as _, ReceiveQueueBindingsContext,
ReceiveQueueContext as _, ReceiveQueueState,
};
use crate::internal::queue::tx::{
self, TransmitDequeueContext, TransmitQueueBindingsContext, TransmitQueueCommon,
TransmitQueueConfiguration, TransmitQueueContext as _, TransmitQueueState,
};
use crate::internal::queue::{fifo, BatchSize, DequeueResult, DequeueState};
use crate::internal::socket::DeviceSocketHandler;
use crate::DeviceCounters;
use log::debug;
/// An API to interact with device `D` transmit queues.
pub struct TransmitQueueApi<D, C>(C, PhantomData<D>);
impl<D, C> TransmitQueueApi<D, C> {
/// Creates a new [`TransmitQueueApi`] from `ctx`.
pub fn new(ctx: C) -> Self {
Self(ctx, PhantomData)
}
}
impl<D, C> TransmitQueueApi<D, C>
where
D: Device,
C: ContextPair,
C::CoreContext:
TransmitDequeueContext<D, C::BindingsContext> + DeviceSocketHandler<D, C::BindingsContext>,
for<'a> <C::CoreContext as TransmitDequeueContext<D, C::BindingsContext>>::TransmitQueueCtx<'a>:
ResourceCounterContext<<C::CoreContext as DeviceIdContext<D>>::DeviceId, DeviceCounters>,
C::BindingsContext:
TransmitQueueBindingsContext<<C::CoreContext as DeviceIdContext<D>>::DeviceId>,
{
fn contexts(&mut self) -> (&mut C::CoreContext, &mut C::BindingsContext) {
let Self(pair, PhantomData) = self;
pair.contexts()
}
fn core_ctx(&mut self) -> &mut C::CoreContext {
self.contexts().0
}
/// Transmits any queued frames.
///
/// Up to `batch_size` frames will attempt to be dequeued and sent in this
/// call.
///
/// `dequeue_context` is directly given to the context to operate on each
/// individual frame.
pub fn transmit_queued_frames(
&mut self,
device_id: &<C::CoreContext as DeviceIdContext<D>>::DeviceId,
batch_size: BatchSize,
dequeue_context: &mut <
C::CoreContext as TransmitQueueCommon<D, C::BindingsContext>
>::DequeueContext,
) -> Result<WorkQueueReport, DeviceSendFrameError> {
let (core_ctx, bindings_ctx) = self.contexts();
core_ctx.with_dequed_packets_and_tx_queue_ctx(
device_id,
|DequeueState { dequeued_frames: dequed_packets }, tx_queue_ctx| {
assert!(
dequed_packets.is_empty(),
"should never have left packets after attempting to dequeue"
);
let ret = tx_queue_ctx.with_transmit_queue_mut(
device_id,
|TransmitQueueState { allocator: _, queue }| {
queue.as_mut().map(|q| q.dequeue_into(dequed_packets, batch_size.into()))
},
);
// If we don't have a transmit queue installed, report no work
// left to be done.
let Some(ret) = ret else { return Ok(WorkQueueReport::AllDone) };
while let Some((meta, p)) = dequed_packets.pop_front() {
tx::deliver_to_device_sockets(tx_queue_ctx, bindings_ctx, device_id, &p, &meta);
match tx_queue_ctx.send_frame(
bindings_ctx,
device_id,
Some(dequeue_context),
meta,
p,
) {
Ok(()) => {}
Err(e) => {
tx_queue_ctx.increment(device_id, |c| &c.send_dropped_dequeue);
// We failed to send the frame so requeue the rest
// and try again later. The failed packet is lost.
// We shouldn't requeue it because it's already been
// delivered to packet sockets.
tx_queue_ctx.with_transmit_queue_mut(
device_id,
|TransmitQueueState { allocator: _, queue }| {
queue.as_mut().unwrap().requeue_items(dequed_packets);
},
);
return Err(e);
}
}
}
Ok(ret.into())
},
)
}
/// Returns the number of frames in `device_id`'s TX queue.
///
/// Returns `None` if the device doesn't have a queue configured.
pub fn count(
&mut self,
device_id: &<C::CoreContext as DeviceIdContext<D>>::DeviceId,
) -> Option<usize> {
self.core_ctx().with_transmit_queue(device_id, |TransmitQueueState { queue, .. }| {
queue.as_ref().map(|q| q.len())
})
}
/// Sets the queue configuration for the device.
pub fn set_configuration(
&mut self,
device_id: &<C::CoreContext as DeviceIdContext<D>>::DeviceId,
config: TransmitQueueConfiguration,
) {
let (core_ctx, bindings_ctx) = self.contexts();
// We take the dequeue lock as well to make sure we finish any current
// dequeuing before changing the configuration.
core_ctx.with_dequed_packets_and_tx_queue_ctx(
device_id,
|DequeueState { dequeued_frames: dequed_packets }, tx_queue_ctx| {
assert!(
dequed_packets.is_empty(),
"should never have left packets after attempting to dequeue"
);
let prev_queue = tx_queue_ctx.with_transmit_queue_mut(
device_id,
|TransmitQueueState { allocator: _, queue }| {
match config {
TransmitQueueConfiguration::None => core::mem::take(queue),
TransmitQueueConfiguration::Fifo => {
match queue {
None => *queue = Some(fifo::Queue::default()),
// Already a FiFo queue.
Some(_) => {}
}
None
}
}
},
);
let Some(mut prev_queue) = prev_queue else { return };
loop {
let ret = prev_queue.dequeue_into(dequed_packets, BatchSize::MAX);
while let Some((meta, p)) = dequed_packets.pop_front() {
tx::deliver_to_device_sockets(
tx_queue_ctx,
bindings_ctx,
device_id,
&p,
&meta,
);
match tx_queue_ctx.send_frame(bindings_ctx, device_id, None, meta, p) {
Ok(()) => {}
Err(err) => {
// We swapped to no-queue and device cannot send
// the frame so we just drop it.
debug!("frame dropped during queue reconfiguration: {err:?}");
}
}
}
match ret {
DequeueResult::NoMoreLeft => break,
DequeueResult::MoreStillQueued => {}
}
}
},
)
}
}
/// /// An API to interact with device `D` receive queues.
pub struct ReceiveQueueApi<D, C>(C, PhantomData<D>);
impl<D, C> ReceiveQueueApi<D, C> {
/// Creates a new [`ReceiveQueueApi`] from `ctx`.
pub fn new(ctx: C) -> Self {
Self(ctx, PhantomData)
}
}
impl<D, C> ReceiveQueueApi<D, C>
where
D: Device,
C: ContextPair,
C::BindingsContext:
ReceiveQueueBindingsContext<<C::CoreContext as DeviceIdContext<D>>::DeviceId>,
C::CoreContext: ReceiveDequeContext<D, C::BindingsContext>,
{
fn contexts(&mut self) -> (&mut C::CoreContext, &mut C::BindingsContext) {
let Self(pair, PhantomData) = self;
pair.contexts()
}
/// Handle a batch of queued RX packets for the device.
///
/// If packets remain in the RX queue after a batch of RX packets has been
/// handled, the RX task will be scheduled to run again so the next batch of
/// RX packets may be handled. See
/// [`ReceiveQueueBindingsContext::wake_rx_task`] for more details.
pub fn handle_queued_frames(
&mut self,
device_id: &<C::CoreContext as DeviceIdContext<D>>::DeviceId,
) -> WorkQueueReport {
let (core_ctx, bindings_ctx) = self.contexts();
core_ctx.with_dequed_frames_and_rx_queue_ctx(
device_id,
|DequeueState { dequeued_frames }, rx_queue_ctx| {
assert_eq!(
dequeued_frames.len(),
0,
"should not keep dequeued frames across calls to this fn"
);
let ret = rx_queue_ctx.with_receive_queue_mut(
device_id,
|ReceiveQueueState { queue }| {
queue.dequeue_into(dequeued_frames, BatchSize::MAX)
},
);
while let Some((meta, p)) = dequeued_frames.pop_front() {
rx_queue_ctx.handle_frame(bindings_ctx, device_id, meta, p);
}
ret.into()
},
)
}
}