]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioNetDxe/TechNotes.txt
86b91f5614950f83b3e13c1462a269b9ea8fe1bc
[mirror_edk2.git] / OvmfPkg / VirtioNetDxe / TechNotes.txt
1 ## @file
2 #
3 # Technical notes for the virtio-net driver.
4 #
5 # Copyright (C) 2013, Red Hat, Inc.
6 #
7 # This program and the accompanying materials are licensed and made available
8 # under the terms and conditions of the BSD License which accompanies this
9 # distribution. The full text of the license may be found at
10 # http://opensource.org/licenses/bsd-license.php
11 #
12 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #
15 ##
16
17 Disclaimer
18 ----------
19
20 All statements concerning standards and specifications are informative and not
21 normative. They are made in good faith. Corrections are most welcome on the
22 edk2-devel mailing list.
23
24 The following documents have been perused while writing the driver and this
25 document:
26 - Unified Extensible Firmware Interface Specification, Version 2.3.1, Errata C;
27 June 27, 2012
28 - Driver Writer's Guide for UEFI 2.3.1, 03/08/2012, Version 1.01;
29 - Virtio PCI Card Specification, v0.9.5 DRAFT, 2012 May 7.
30
31
32 Summary
33 -------
34
35 The VirtioNetDxe UEFI_DRIVER implements the Simple Network Protocol for
36 virtio-net devices. Higher level protocols are automatically installed on top
37 of it by the DXE Core / the ConnectController() boot service, enabling for
38 virtio-net devices eg. DHCP configuration, TCP transfers with edk2 StdLib
39 applications, and PXE booting in OVMF.
40
41
42 UEFI driver structure
43 ---------------------
44
45 A driver instance, belonging to a given virtio-net device, can be in one of
46 four states at any time. The states stack up as follows below. The state
47 transitions are labeled with the primary function (and its important callees
48 faithfully indented) that implement the transition.
49
50 | ^
51 | |
52 [DriverBinding.c] | | [DriverBinding.c]
53 VirtioNetDriverBindingStart | | VirtioNetDriverBindingStop
54 VirtioNetSnpPopulate | | VirtioNetSnpEvacuate
55 VirtioNetGetFeatures | |
56 v |
57 +-------------------------+
58 | EfiSimpleNetworkStopped |
59 +-------------------------+
60 | ^
61 [SnpStart.c] | | [SnpStop.c]
62 VirtioNetStart | | VirtioNetStop
63 | |
64 v |
65 +-------------------------+
66 | EfiSimpleNetworkStarted |
67 +-------------------------+
68 | ^
69 [SnpInitialize.c] | | [SnpShutdown.c]
70 VirtioNetInitialize | | VirtioNetShutdown
71 VirtioNetInitRing {Rx, Tx} | | VirtioNetShutdownRx [SnpSharedHelpers.c]
72 VirtioRingInit | | VirtioNetShutdownTx [SnpSharedHelpers.c]
73 VirtioNetInitTx | | VirtioNetUninitRing [SnpSharedHelpers.c]
74 VirtioNetInitRx | | {Tx, Rx}
75 | | VirtioRingUninit
76 v |
77 +-----------------------------+
78 | EfiSimpleNetworkInitialized |
79 +-----------------------------+
80
81 The state at the top means "nonexistent" and is hence unnamed on the diagram --
82 a driver instance actually doesn't exist at that point. The transition
83 functions out of and into that state implement the Driver Binding Protocol.
84
85 The lower three states characterize an existent driver instance and are all
86 states defined by the Simple Network Protocol. The transition functions between
87 them are member functions of the Simple Network Protocol.
88
89 Each transition function validates its expected source state and its
90 parameters. For example, VirtioNetDriverBindingStop will refuse to disconnect
91 from the controller unless it's in EfiSimpleNetworkStopped.
92
93
94 Driver instance states (Simple Network Protocol)
95 ------------------------------------------------
96
97 In the EfiSimpleNetworkStopped state, the virtio-net device is (has been)
98 re-set. No resources are allocated for networking / traffic purposes. The MAC
99 address and other device attributes have been retrieved from the device (this
100 is necessary for completing the VirtioNetDriverBindingStart transition).
101
102 The EfiSimpleNetworkStarted is completely identical to the
103 EfiSimpleNetworkStopped state for virtio-net, in the functional and
104 resource-usage sense. This state is mandated / provided by the Simple Network
105 Protocol for flexibility that the virtio-net driver doesn't exploit.
106
107 In particular, the EfiSimpleNetworkStarted state is the target of the Shutdown
108 SNP member function, and must therefore correspond to a hardware configuration
109 where "[it] is safe for another driver to initialize". (Clearly another UEFI
110 driver could not do that due to the exclusivity of the driver binding that
111 VirtioNetDriverBindingStart() installs, but a later OS driver might qualify.)
112
113 The EfiSimpleNetworkInitialized state is the live state of the virtio NIC / the
114 driver instance. Virtio and other resources required for network traffic have
115 been allocated, and the following SNP member functions are available (in
116 addition to VirtioNetShutdown which leaves the state):
117
118 - VirtioNetReceive [SnpReceive.c]: poll the virtio NIC for an Rx packet that
119 may have arrived asynchronously;
120
121 - VirtioNetTransmit [SnpTransmit.c]: queue a Tx packet for asynchronous
122 transmission (meant to be used together with VirtioNetGetStatus);
123
124 - VirtioNetGetStatus [SnpGetStatus.c]: query link status and status of pending
125 Tx packets;
126
127 - VirtioNetMcastIpToMac [SnpMcastIpToMac.c]: transform a multicast IPv4/IPv6
128 address into a multicast MAC address;
129
130 - VirtioNetReceiveFilters [SnpReceiveFilters.c]: emulate unicast / multicast /
131 broadcast filter configuration (not their actual effect -- a more liberal
132 filter setting than requested is allowed by the UEFI specification).
133
134 The following SNP member functions are not supported [SnpUnsupported.c]:
135
136 - VirtioNetReset: reinitialize the virtio NIC without shutting it down (a loop
137 from/to EfiSimpleNetworkInitialized);
138
139 - VirtioNetStationAddress: assign a new MAC address to the virtio NIC,
140
141 - VirtioNetStatistics: collect statistics,
142
143 - VirtioNetNvData: access non-volatile data on the virtio NIC.
144
145 Missing support for these functions is allowed by the UEFI specification and
146 doesn't seem to trip up higher level protocols.
147
148
149 Events and task priority levels
150 -------------------------------
151
152 The UEFI specification defines a sophisticated mechanism for asynchronous
153 events / callbacks (see "6.1 Event, Timer, and Task Priority Services" for
154 details). Such callbacks work like software interrupts, and some notion of
155 locking / masking is important to implement critical sections (atomic or
156 exclusive access to data or a device). This notion is defined as Task Priority
157 Levels.
158
159 The virtio-net driver for OVMF must concern itself with events for two reasons:
160
161 - The Simple Network Protocol provides its clients with a (non-optional) WAIT
162 type event called WaitForPacket: it allows them to check or wait for Rx
163 packets by polling or blocking on this event. (This functionality overlaps
164 with the Receive member function.) The event is available to clients starting
165 with EfiSimpleNetworkStopped (inclusive).
166
167 The virtio-net driver is informed about such client polling or blockage by
168 receiving an asynchronous callback (a software interrupt). In the callback
169 function the driver must interrogate the driver instance state, and if it is
170 EfiSimpleNetworkInitialized, access the Rx queue and see if any packets are
171 available for consumption. If so, it must signal the WaitForPacket WAIT type
172 event, waking the client.
173
174 For simplicity and safety, all parts of the virtio-net driver that access any
175 bit of the driver instance (data or device) run at the TPL_CALLBACK level.
176 This is the highest level allowed for an SNP implementation, and all code
177 protected in this manner satisfies even stricter non-blocking requirements
178 than what's documented for TPL_CALLBACK.
179
180 The task priority level for the WaitForPacket callback too is set by the
181 driver, the choice is TPL_CALLBACK again. This in effect serializes the
182 WaitForPacket callback (VirtioNetIsPacketAvailable [Events.c]) with "normal"
183 parts of the driver.
184
185 - According to the Driver Writer's Guide, a network driver should install a
186 callback function for the global EXIT_BOOT_SERVICES event (a special NOTIFY
187 type event). When the ExitBootServices() boot service has cleaned up internal
188 firmware state and is about to pass control to the OS, any network driver has
189 to stop any in-flight DMA transfers, lest it corrupts OS memory. For this
190 reason EXIT_BOOT_SERVICES is emitted and the network driver must abort
191 in-flight DMA transfers.
192
193 This callback (VirtioNetExitBoot) is synchronized with the rest of the driver
194 code just the same as explained for WaitForPacket. In
195 EfiSimpleNetworkInitialized state it resets the virtio NIC, halting all data
196 transfer. After the callback returns, no further driver code is expected to
197 be scheduled.
198
199
200 Virtio internals -- Rx
201 ----------------------
202
203 Requests (Rx and Tx alike) are always submitted by the guest and processed by
204 the host. For Tx, processing means transmission. For Rx, processing means
205 filling in the request with an incoming packet. Submitted requests exist on the
206 "Available Ring", and answered (processed) requests show up on the "Used Ring".
207
208 Packet data includes the media (Ethernet) header: destination MAC, source MAC,
209 and Ethertype (14 bytes total).
210
211 The following structures implement packet reception. Most of them are defined
212 in the Virtio specification, the only driver-specific trait here is the static
213 pre-configuration of the two-part descriptor chains, in VirtioNetInitRx. The
214 diagram is simplified.
215
216 Available Index Available Index
217 last processed incremented
218 by the host by the guest
219 v -------> v
220 Available +-------+-------+-------+-------+-------+
221 Ring |DescIdx|DescIdx|DescIdx|DescIdx|DescIdx|
222 +-------+-------+-------+-------+-------+
223 =D6 =D2
224
225 D2 D3 D4 D5 D6 D7
226 Descr. +----------+----------++----------+----------++----------+----------+
227 Table |Adr:Len:Nx|Adr:Len:Nx||Adr:Len:Nx|Adr:Len:Nx||Adr:Len:Nx|Adr:Len:Nx|
228 +----------+----------++----------+----------++----------+----------+
229 =A2 =D3 =A3 =A4 =D5 =A5 =A6 =D7 =A7
230
231
232 A2 A3 A4 A5 A6 A7
233 Receive +---------------+---------------+---------------+
234 Destination |vnet hdr:packet|vnet hdr:packet|vnet hdr:packet|
235 Area +---------------+---------------+---------------+
236
237 Used Index Used Index incremented
238 last processed by the guest by the host
239 v -------> v
240 Used +-----------+-----------+-----------+-----------+-----------+
241 Ring |DescIdx:Len|DescIdx:Len|DescIdx:Len|DescIdx:Len|DescIdx:Len|
242 +-----------+-----------+-----------+-----------+-----------+
243 =D4
244
245 In VirtioNetInitRx, the guest allocates the fixed size Receive Destination
246 Area, which accommodates all packets delivered asynchronously by the host. To
247 each packet, a slice of this area is dedicated; each slice is further
248 subdivided into virtio-net request header and network packet data. The
249 (guest-physical) addresses of these sub-slices are denoted with A2, A3, A4 and
250 so on. Importantly, an even-subscript "A" always belongs to a virtio-net
251 request header, while an odd-subscript "A" always belongs to a packet
252 sub-slice.
253
254 Furthermore, the guest lays out a static pattern in the Descriptor Table. For
255 each packet that can be in-flight or already arrived from the host,
256 VirtioNetInitRx sets up a separate, two-part descriptor chain. For packet N,
257 the Nth descriptor chain is set up as follows:
258
259 - the first (=head) descriptor, with even index, points to the fixed-size
260 sub-slice receiving the virtio-net request header,
261
262 - the second descriptor (with odd index) points to the fixed (1514 byte) size
263 sub-slice receiving the packet data,
264
265 - a link from the first (head) descriptor in the chain is established to the
266 second (tail) descriptor in the chain.
267
268 Finally, the guest populates the Available Ring with the indices of the head
269 descriptors. All descriptor indices on both the Available Ring and the Used
270 Ring are even.
271
272 Packet reception occurs as follows:
273
274 - The host consumes a descriptor index off the Available Ring. This index is
275 even (=2*N), and fingers the head descriptor of the chain belonging to packet
276 N.
277
278 - The host reads the descriptors D(2*N) and -- following the Next link there
279 --- D(2*N+1), and stores the virtio-net request header at A(2*N), and the
280 packet data at A(2*N+1).
281
282 - The host places the index of the head descriptor, 2*N, onto the Used Ring,
283 and sets the Len field in the same Used Ring Element to the total number of
284 bytes transferred for the entire descriptor chain. This enables the guest to
285 identify the length of Rx packets.
286
287 - VirtioNetReceive polls the Used Ring. If a new Used Ring Element shows up, it
288 copies the data out to the caller, and recycles the index of the head
289 descriptor (ie. 2*N) to the Available Ring.
290
291 - Because the host can process (answer) Rx requests in any order theoretically,
292 the order of head descriptor indices on each of the Available Ring and the
293 Used Ring is virtually random. (Except right after the initial population in
294 VirtioNetInitRx, when the Available Ring is full and increasing, and the Used
295 Ring is empty.)
296
297 - If the Available Ring is empty, the host is forced to drop packets. If the
298 Used Ring is empty, VirtioNetReceive returns EFI_NOT_READY (no packet
299 available).
300
301
302 Virtio internals -- Tx
303 ----------------------
304
305 The transmission structure erected by VirtioNetInitTx is similar, it differs
306 in the following:
307
308 - There is no Receive Destination Area.
309
310 - Each head descriptor, D(2*N), points to a read-only virtio-net request header
311 that is shared by all of the head descriptors. This virtio-net request header
312 is never modified by the host.
313
314 - Each tail descriptor is re-pointed to the caller-supplied packet buffer
315 whenever VirtioNetTransmit places the corresponding head descriptor on the
316 Available Ring. The caller is responsible to hang on to the unmodified buffer
317 until it is reported transmitted by VirtioNetGetStatus.
318
319 Steps of packet transmission:
320
321 - Client code calls VirtioNetTransmit. VirtioNetTransmit tracks free descriptor
322 chains by keeping the indices of their head descriptors in a stack that is
323 private to the driver instance. All elements of the stack are even.
324
325 - If the stack is empty (that is, each descriptor chain, in isolation, is
326 either pending transmission, or has been processed by the host but not
327 yet recycled by a VirtioNetGetStatus call), then VirtioNetTransmit returns
328 EFI_NOT_READY.
329
330 - Otherwise the index of a free chain's head descriptor is popped from the
331 stack. The linked tail descriptor is re-pointed as discussed above. The head
332 descriptor's index is pushed on the Available Ring.
333
334 - The host moves the head descriptor index from the Available Ring to the Used
335 Ring when it transmits the packet.
336
337 - Client code calls VirtioNetGetStatus. In case the Used Ring is empty, the
338 function reports no Tx completion. Otherwise, a head descriptor's index is
339 consumed from the Used Ring and recycled to the private stack. The client
340 code's original packet buffer address is fetched from the tail descriptor
341 (where it has been stored at VirtioNetTransmit time) and returned to the
342 caller.
343
344 - The Len field of the Used Ring Element is not checked. The host is assumed to
345 have transmitted the entire packet -- VirtioNetTransmit had forced it below
346 1514 bytes (inclusive). The Virtio specification suggests this packet size is
347 always accepted (and a lower MTU could be encountered on any later hop as
348 well). Additionally, there's no good way to report a short transmit via
349 VirtioNetGetStatus; EFI_DEVICE_ERROR seems too serious from the specification
350 and higher level protocols could interpret it as a fatal condition.
351
352 - The host can theoretically reorder head descriptor indices when moving them
353 from the Available Ring to the Used Ring (out of order transmission). Because
354 of this (and the choice of a stack over a list for free descriptor chain
355 tracking) the order of head descriptor indices on either Ring is
356 unpredictable.