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