xref: /freebsd/sys/net80211/DATAPATH_TRANSMIT.md (revision 490c53e9353feb129fe0acb8d9ba8fa52db24e2c)
1# net80211 Datapath - Transmit
2
3## Overview
4
5This document provides an overview for the transmit data path in
6net80211, between the interface to the operating system, through net80211 and
7into the driver.
8
9The details about underlying implementations (eg how A-MPDU RX aggregation
10is handled) will be covered in dedicated documents.
11
12## Concurrency Notes
13
14The transmit path(s), receive path and control / ioctl paths all run
15in parallel and can be scheduled on multiple concurrently running
16kernel threads.  It's important to keep this in mind.
17
18## Transmit Path
19
20There are two paths from the operating system layer into the net80211 transmit
21path - the normal data path and the BPF / radiotap raw frame path.
22
23It is important to note that both paths have no serialisation between
24them, and multiple sending paths in the OS can and will queue frames
25simultaneously across multiple concurrently executing threads/CPUs.
26Please keep this in mind when reading the transmit handling and
27how it interacts with 802.11 sequence numbering and encryption IV.
28
29### Data Path - net80211
30
31This is configured at the ifnet setup in ieee80211_vap_setup() -
32the output path is ieee80211_vap_transmit().  This input path
33takes 802.3 ethernet frames with no attached metadata (such as
34rate control, transmit power, etc) - it is left up to the stack.
35
36This hands the packet off to ieee80211_start_pkt() which will
37perform the initial 802.11 destination lookup, query the node
38state (eg whether it's in power save) and the VAP state (eg
39is the vap itself in power state, or in a non-RUN state)
40and drop or queue the frame appropriately.
41
42It is then handed over to ieee80211_vap_pkt_send_dest() with
43a destination ieee80211_node reference.
44
45ieee80211_vap_pkt_send_dest() performs the bulk of the
46net80211 transmit handling.  Packets will be queued here if the
47destination node is in a power saving mode.
48
49This includes:
50
51 * Firstly - checking if the packet needs to be queued for
52   power saving operation and will pass it via ieee80211_pwrsave()
53   if needed;
54 * QoS classification via a call to ieee80211_classify();
55 * BPF TX tap via a call to BPF_MTAP();
56 * handling 802.11 encapsulation via ieee80211_encap() if required;
57 * A-MPDU TX decisions, AMSDU and Atheros Fast-Frames decisions.
58
59At this point the packet has been 802.11 encapsulated if required,
60marked as needing encryption if required, and has been optionally
61fragmented into a list of 802.11 fragments.
62
63Finally, the packet / fragment packet chain is sent up to the driver via a call
64to ieee80211_parent_xmitpkt().   The driver is expected to queue the
65packet / fragment list or discard the packet / fragment list.  The specific
66format of the mbuf chain and how ieee80211_node references are kept
67is documented in ieee80211_parent_xmitpkt().
68
69#### Notes on transmit path serialisation
70
71Note that by default the IEEE80211_TX_LOCK() is held across the call to
72ieee80211_encap() and ieee80211_parent_xmitpkt().  Drivers can register
73that they properly handle 802.11 sequence number offloading via
74IEEE80211_FEXT_SEQNO_OFFLOAD.  The lock is to ensure that packets
75queued to the driver layer are added to the driver transmit queue
76in the same order that they are 802.11 encapsulated - which sets the
77802.11 sequence number.  Drivers which set IEEE80211_FEXT_SEQNO_OFFLOAD
78indicate that they will assign the sequence number themselves - likely
79at the same time that the transmit encryption IV number is assigned,
80or simply offloaded in firmware - and thus this lock is not
81required.
82
83### Data path - Driver
84
85The call ieee80211_parent_xmit() will call the driver ic->ic_transmit()
86method.  At this point the driver can choose to queue / send the frame
87(and take ownership of it), or return an error, and return it back
88to net80211.  Currently net80211 will just free the mbuf and node reference
89and return, but drivers should not assume that.
90
91The mbuf passed in will be either a single 802.11/802.3 frame in an mbuf,
92or a list of 802.11 fragments chained by m->m_nextpkt.  If the driver
93has not set IEEE80211_FEXT_SEQNO_OFFLOAD then the packet will have
94a sequence number assigned which the driver can fetch via M_SEQNO_GET().
95The mbuf also holds an ieee80211_node reference.
96
97(Note that fragments do not have sequence numbers assigned nor node
98references.)
99
100The driver needs to do a few things with this frame.  Notably if it's
101an 802.3 offload device, it will be handed an 802.3 frame with no
102802.11 information.  In that case, the driver just needs to queue
103it for send to the hardware/firmware.
104
105For devices which accept 802.11 frames, a few things are needed:
106
107 * It needs to queue them for send, in the order they're given.
108 * If there are any reasons the frames need to be buffered in the
109   driver - eg node power state, asynchronous node/key/state updates -
110   then they'll be buffered here until needed.
111 * It needs to do any local hardware/firmware setup - rate control,
112   transmit configuration, destination queue decisions, etc.
113 * Hardware/firmware typically has some way to mark a frame as a type
114   (control, data, management), whether RTS/CTS is needed,
115 * If IEEE80211_FEXT_SEQNO_OFFLOAD is set in the driver, it may need to
116   allocate 802.11 sequence numbers via a call to ieee80211_output_seqno_assign().
117 * If the frame is part of an MPDU (m->m_flags & M_AMPDU_MPDU) then
118   the frame may need to be handled differently.  (For example rtwn(4)
119   leaves sequence number assignment up to the firmware when A-MPDU is
120   enabled.)
121 * If the mbuf is marked as needing encryption (IEEE80211_FC1_PROTECTED
122   is set in the 802.11 header) then the frame needs to be encrypted
123   with the current encryption state via a call to ieee80211_crypto_encap().
124 * Finally, the frame is queued to the hardware/firmware.
125
126Again it is critical that the 802.11 sequence number and encryption be
127called together in the same order.  This is typically done by the TX work
128being done in a lock, or all frames being pushed into a single software
129TX queue.
130
131### Data path vs control path and the need to buffer frames
132
133net80211 currently treats encryption key programming, VAP state
134and other updates as synchronous calls.  For example, the
135transmit path will call the driver to add a node, then
136set the encryption keys and then queue a frame to be transmitted.
137
138For devices which are programmed directly with no queued operations
139(such as the ath(4) devices) the encryption key and node programming
140is immediate.  However, for many other devices - firmware and
141USB are two examples - these operations are asynchronous.
142And these code paths tend to be in the transmit paths from
143upper layers that may have locks held, so sleeping is not an option.
144
145So for now this needs to be implemented in the driver itself.
146It will need to maintain a per-node queue of transmit frames;
147it will need to track asynchronous node creation/updates and
148encryption key updates and buffer transmit frames for a node
149until the node add/update and encryption key add/update is
150completed.
151
152### Transmit Completion Notifications
153
154The net80211 stack may request a completion notification
155to be called when a transmit frame has completed.
156This will be done via a call to ieee80211_add_callback().
157It is used in various parts of the net80211 stack to
158drive the MAC state machines - for example, being notified
159once an BAR (Block-ACK request) frame has completed so
160the retry timer can be cancelled.
161
162This requires that mbufs that are transmitted with a requested
163completion callback be checked and handled appropriately.
164This is covered in the next section.
165
166### Completing and freeing transmit path mbufs
167
168There are two paths to freeing mbufs - ieee80211_free_mbuf() and
169ieee80211_tx_complete().
170
171#### Before transmit - ieee80211_free_mbuf()
172
173ieee80211_free_mbuf() is used in drivers and net80211 to free
174a list of mbufs as part of the transmit path setup so it can
175properly account for and free an 802.11 MPDU / 802.3 frame,
176or a list of mbufs representing 802.11 fragments.  It doesn't
177handle the ieee80211_node reference as at the early stage
178of transmit there is a single ieee80211_node reference
179covering all of the fragments being passed to the driver
180for transmit.
181
182If you're not supporting 802.11 fragment transmit (and you have
183to register your driver with the IEEE80211_C_TXFRAG capability
184to even support this) then you can ignore all of the above
185and just not call ieee80211_free_mbuf() for now.
186
187This must not be used for receive mbufs.  Yes, this is not
188well named and should likely just be renamed.
189
190#### After transmit queueing / attempts - ieee80211_tx_complete().
191
192In the general case of an transmit mbuf being completed (either
193successfully or unsuccessfully) net80211 provides a call
194to handle everything - ieee80211_tx_complete().  This takes
195the relevant destination node (struct ieee80211_node),
196the mbuf, and a status indiciating success or failure.
197
198A call to ieee80211_tx_complete() handles a variety of
199common functions:
200
201 * It increments the ifnet counters as appropriate;
202 * If the frame has a TX completion notification callback attached
203   it will process said callback;
204 * If a node is supplied then the node reference is freed
205
206In the past some drivers implemented the mbuf TX callback
207handling themselves, resulting in some drivers supporting
208callback and some drivers not supporting callbacks. The goal
209here is to implement a single way for completions to be
210handled.
211
212Note that some hardware / firmware do not support per-frame
213completion / status notification.  For example, USB devices
214tend to not send individual notifications for frames - you
215may be able to request it for specific frames, but the
216status notifications are expensive.  In these cases, drivers
217may just call ieee80211_tx_complete() with a status based
218on whether the frame was queued to the USB endpoint successfully
219or not.
220
221#### Atheros Fast Frames / 802.11n A-MSDU transmit
222
223(Note this is purposely short - a larger write-up for this will be
224done on a separate page.)
225
226The transmit path above will call ieee80211_ff_check() and
227ieee80211_amsdu_check() to see if the given node/frame should be
228queued for an Atheros Fast Frames MPDU or an A-MSDU.
229
230If the frame should be queued it will be queued locally and NULL
231will be returned; if there's already a frame queued it may be
232paired with a queued frame and both returned as a single mbuf / MPDU
233to send.
234
235As far as the driver is concerned, it will be handed a single
236802.11 MPDU to send.
237
238#### 802.11n A-MPDU transmit
239
240net80211 implements the A-MPDU negotiation and block-ack request/response
241handling.  However currently the driver must implement A-MPDU packet
242queuing, buffering, submission and retransmission.
243
244There are some methods that the driver can override to control the
245A-MPDU transmit negotiation flow (ic->ic_addba_request, ic->ic_addba_response,
246ic->ic_addba_response_timeout, ic->ic_addba_stop) and the Block-Ack
247response completion or error/timeout (ic->ic_bar_response).
248
249#### Driver queue completion
250
251Currently there are two things a driver should do when its own queues
252are (mostly) empty:
253
254 * When the transmit queue is empty or mostly empty, call ieee80211_ff_flush()
255   to flush out any pending A-MSDU / Atheros Fast Frames to be transmitted;
256 * When the receive queue is being handled, call ieee80211_ff_age_all() to
257   flush out any frames that are older than a provided time interval.
258
259These calls ensure that any queued frames in Fast Frames / A-MSDU queue
260don't stay in there permanently.
261
262### Non data frame transmission (management, control, action, beacon, etc)
263
264Non data frames are sent via ieee80211_raw_output().  The main exception to
265this is beacon frames, which are separately initialised and pulled from
266net80211 into the driver by the driver specific beacon handling routines.
267
268Raw frames differ from data frames in a couple of ways:
269
270 * Transmit parameters are typically sent from userland or the caller
271   (struct ieee80211_bpf_params \*), and
272 * The input path into the driver is via ic->ic_raw_xmit(), not ic->ic_transmit().
273
274The driver can combine the data and non-data paths into a single path.
275The main reason for keeping these separate is to cleanly support drivers
276and firmware which allow 802.3 frames to be sent and received, but still
277need a side channel to send and receive management frames for various other
278functions.
279
280The raw frame output path is used by:
281
282 * The BPF output path - ieee80211_output() ;
283 * The management frame output path - ieee80211_mgmt_output() ;
284 * The NULL data output path - ieee80211_send_nulldata() ;
285 * Sending probe requests - ieee80211_send_probereq() ;
286 * Sending probe responses - ieee80211_send_proberesp() ;
287 * Sending 802.11n BAR frames - ieee80211_send_bar() ;
288 * .. and anywhere where the individual protocol (eg 802.11s) wishes to send raw
289   non-data frames.
290
291This path is not REALLY designed for high speed data - for example,
292it should work for basic packet injection, but it does not pass through
293the normal functions for encryption, power save, TX aggregation and other
294data specific operations.  It expects to be handed a raw, already encapsulated
295802.11 frame.
296
297Note this is not an 802.11 MPDU - this is an 802.11 frame.  For example,
298non-data frames may not have sequence numbers.  NULL data frames have a sequence
299number but that sequence number must be 0.
300
301Once the driver ic->ic_raw_xmit() call is made, the driver can handle the
302802.11 frame in any way it sees fit.  Again, it can't assume it's an 802.11
303data frame.
304
305### BPF path
306
307Control frames are injected from userland and net80211 via a raw transmit path,
308separate from the data path.  This dates back to the earliest Orinoco/WaveLAN
309cards, where the earlier firmware only allowed 802.3 frames to be sent/received,
310but later firmware introduced raw packet transmit to allow wpa_supplicant
311operation.
312
313Packet injection begins via the BPF/radiotap input path.  The code in
314ieee80211_radiotap.c attaches a BPF operator to the VAP during the
315call to ieee80211_radiotap_vattach().
316
317Raw frames start in BPF and are queued via bpf_ieee80211_write(), which will
318send the frame into the driver via a call to the VAP ifp->if_output() and then
319if provided, a copy of the feedback mbuf via the VAP ifp->if_input().
320
321The ifp->if_output() method by default is ieee80211_output().  The driver
322can override this.  This takes care of validating that it is an 802.11
323frame, extracts the (struct ieee80211_bpf_params \*) header from the
324destination sockaddr passed in via BPF, finds the relevant
325struct ieee80211_node \*) tx node, grabs a reference, some further sanity
326checks and then calls ieee80211_raw_output().  The rest of the raw output
327path is the same as net80211 sourced raw frames.
328
329### Power Save Management
330
331By default, net80211 will track legacy power-save state between IBSS nodes
332and STA <-> AP nodes (ie, full node buffering via the power management bit
333in the 802.11 header; TIM/ATIM bitmaps in beacons, NULL data frames to wake up)
334and PS-POLL frames being sent by stations to request individual frames.
335
336The transmit path will pass frames destined to asleep stations to the power
337save queue via a call to ieee80211_pwrsave().
338
339There are a number of VAP methods for the driver to tie into if it needs to be
340informed about this state (vap->iv_set_tim, vap->iv_recv_pspoll, vap->iv_node_ps).
341These allow the driver to keep its own internal state in sync with net80211
342and allows it to better maintain its own transmit queue state.
343
344See the ath(4) driver for a comprehensive example of how these methods are used
345to correctly transmit and buffer frames from an AP to STA device without packet
346loss.
347
348### Transmit path encryption
349
350The net80211 stack needs to handle a variety of transmit encryption schemes
351based on all the combinations that driver and firmware interfaces may require.
352
353In general, the transmit encryption is done in two phases:
354
355 * In ieee80211_encap(), the transmit key is chosen via a call
356   to ieee80211_crypto_getucastkey() or ieee80211_crypto_getmcastkey() - the
357   key index is added to the 802.11 header and space is reserved between
358   the 802.11 header/payload and at the end for the encryption key data to be
359   added;
360 * Then when the driver transmits the frame, it calls ieee80211_crypto_encap()
361   to actually do the encryption.
362
363Some hardware will completely offload encryption, so although the key choice
364is made, various driver configuration options are set to inform net80211 not
365to add all the padding.  Others will offload encryption but require the
366space to be provided in the frame for the hardware/firmware to add the
367encryption information into.
368
369### What is IEEE80211_F_DATAPAD ?
370
371This is actually to support hardware such as the Atheros 802.11abgn chips,
372which have a 4 byte alignment requirement between the 802.11 header and
373the data payload (including the encryption parts.)
374
375Yes, it likely should be a more generic option.
376
377### Future work
378
379  * It would be nice to more formally define and enforce what drivers should be
380    doing with mbufs during the whole transmit lifecycle of an mbuf.
381  * Perhaps add a function or two for the drivers to use to
382    query whether a given mbuf has a TX notification attached (rather
383    than drivers querying M_TXCB) so they can individually
384    register for explicit notifications so they can provide more
385    accurate completion information.
386  * The fast frames age / flush routines should really be expanded to
387    be required functionality in net80211 drivers rather than optional
388    when IEEE80211_SUPPORT_SUPERG is enabled, so further software transmit
389    queue management is possible in net80211.
390
391