L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
virtio-net
1// vi:ft=cpp
2/* SPDX-License-Identifier: MIT */
3/*
4 * Copyright (C) 2022, 2024 Kernkonzept GmbH.
5 * Author(s): Stephan Gerhold <stephan.gerhold@kernkonzept.com>
6 */
7#pragma once
8
9#include <cstring>
10#include <functional>
11
12#include <l4/cxx/exceptions>
13#include <l4/cxx/minmax>
14#include <l4/re/dataspace>
15#include <l4/re/env>
16#include <l4/re/error_helper>
17#include <l4/re/util/unique_cap>
18#include <l4/sys/consts.h>
19
20#include <l4/l4virtio/client/l4virtio>
21#include <l4/l4virtio/l4virtio>
22#include <l4/l4virtio/virtio_net.h>
23#include <l4/l4virtio/virtqueue>
24
25namespace L4virtio { namespace Driver {
26
31{
32public:
37 struct Packet
38 {
40 l4_uint8_t data[1500 + 14]; /* MTU + Ethernet header */
41 };
42
47 int rx_queue_size() const
48 { return max_queue_size(0); }
49
54 int tx_queue_size() const
55 { return max_queue_size(1); }
56
73 {
74 // Contact device.
75 driver_connect(srvcap, false);
76
77 if (_config->device != L4VIRTIO_ID_NET)
78 L4Re::chksys(-L4_ENODEV, "Device is not a network device.");
79
80 if (_config->num_queues < 2)
81 L4Re::chksys(-L4_EINVAL, "Invalid number of queues reported.");
82
83 auto rxqsz = rx_queue_size();
84 auto txqsz = tx_queue_size();
85
86 // Allocate memory for RX/TX queue and RX/TX packet buffers
87 auto rxqoff = 0;
88 auto txqoff = l4_round_size(rxqoff + rxqsz * _rxq.total_size(rxqsz),
89 L4virtio::Virtqueue::Desc_align);
90 auto rxpktoff = l4_round_size(txqoff + txqsz * _txq.total_size(txqsz),
91 L4virtio::Virtqueue::Desc_align);
92 auto txpktoff = rxpktoff + rxqsz * sizeof(Packet);
93 auto totalsz = txpktoff + txqsz * sizeof(Packet);
94
95 auto *e = L4Re::Env::env();
96
97 if (queue_ds)
98 _queue_ds = L4Re::Util::Unique_cap<L4Re::Dataspace>(queue_ds);
99 else
100 {
102 "Allocate queue dataspace capability");
103 L4Re::chksys(e->mem_alloc()->alloc(totalsz, _queue_ds.get(),
106 "Allocate memory for virtio structures");
107 }
108
109 L4Re::chksys(e->rm()->attach(&_queue_region, totalsz,
111 L4::Ipc::make_cap_rw(_queue_ds.get()), 0,
113 "Attach dataspace for virtio structures");
114
115 l4_uint64_t devaddr;
116 L4Re::chksys(register_ds(_queue_ds.get(), 0, totalsz, &devaddr),
117 "Register queue dataspace with device");
118
119 _rxq.init_queue(rxqsz, _queue_region.get() + rxqoff);
120 _txq.init_queue(txqsz, _queue_region.get() + txqoff);
121
122 config_queue(0, rxqsz, devaddr + rxqoff,
123 devaddr + rxqoff + _rxq.avail_offset(),
124 devaddr + rxqoff + _rxq.used_offset());
125 config_queue(1, txqsz, devaddr + txqoff,
126 devaddr + txqoff + _txq.avail_offset(),
127 devaddr + txqoff + _txq.used_offset());
128
129 _rxpkts = reinterpret_cast<Packet*>(_queue_region.get() + rxpktoff);
130 _txpkts = reinterpret_cast<Packet*>(_queue_region.get() + txpktoff);
131
132 // Prepare descriptors to save work later
133 for (l4_uint16_t descno = 0; descno < rxqsz; ++descno)
134 {
135 auto &desc = _rxq.desc(descno);
136 desc.addr = L4virtio::Ptr<void>(devaddr + rxpktoff +
137 descno * sizeof(Packet));
138 desc.len = sizeof(Packet);
139 desc.flags.write() = 1;
140 }
141 for (l4_uint16_t descno = 0; descno < txqsz; ++descno)
142 {
143 auto &desc = _txq.desc(descno);
144 desc.addr = L4virtio::Ptr<void>(devaddr + txpktoff +
145 descno * sizeof(Packet));
146 desc.len = sizeof(Packet);
147 }
148
149 // Setup notification IRQ
150 _driver_notification_irq =
152 "Allocate notification capability");
153
154 L4Re::chksys(l4_error(e->factory()->create(_driver_notification_irq.get())),
155 "Create irq for notifications from device");
156
157 L4Re::chksys(_device->bind(0, _driver_notification_irq.get()),
158 "Bind driver notification interrupt");
159
160 // Finish handshake with device
161 l4virtio_set_feature(_config->driver_features_map,
163 l4virtio_set_feature(_config->driver_features_map, L4VIRTIO_NET_F_MAC);
165 }
166
171 {
172 return *_config->device_config<l4virtio_net_config_t>();
173 }
174
182 {
183 return l4_error(_driver_notification_irq->bind_thread(thread, label));
184 }
185
193 {
194 if (descno >= _rxq.num())
195 throw L4::Bounds_error("Invalid used descriptor number in RX queue");
196 return _rxpkts[descno];
197 }
198
215 {
216 l4_uint16_t descno;
217 // Wait until used descriptor becomes available.
218 for (;;)
219 {
220 descno = _rxq.find_next_used(len);
221 if (descno != Virtqueue::Eoq)
222 break;
223
224 L4Re::chksys(_driver_notification_irq->receive(), "Wait for RX");
225 }
226
227 if (len)
228 // Ensure that the length provided by the device in wait_for_next_used()
229 // is not larger than the buffer and subtract the length of the header.
230 *len = cxx::min(*len - sizeof(_rxpkts[0].hdr), sizeof(_rxpkts[0].data));
231 return descno;
232 }
233
243 {
244 _rxq.free_descriptor(descno, descno);
245 }
246
250 void queue_rx()
251 {
252 l4_uint16_t descno;
253 while ((descno = _rxq.alloc_descriptor()) != Virtqueue::Eoq)
254 _rxq.enqueue_descriptor(descno);
255 notify(_rxq);
256 }
257
272 bool tx(std::function<l4_uint32_t(Packet&)> prepare)
273 {
274 auto descno = _txq.alloc_descriptor();
275 if (descno == Virtqueue::Eoq)
276 {
277 // Try again after cleaning old descriptors that have already been used
278 free_used_tx_descriptors();
279 descno = _txq.alloc_descriptor();
280 if (descno == Virtqueue::Eoq)
281 return false;
282 }
283
284 auto &pkt = _txpkts[descno];
285 auto &desc = _txq.desc(descno);
286 desc.len = sizeof(pkt.hdr) + prepare(pkt);
287 send(_txq, descno);
288 return true;
289 }
290
291private:
292 void free_used_tx_descriptors()
293 {
294 l4_uint16_t used;
295 while ((used = _txq.find_next_used()) != Virtqueue::Eoq)
296 {
297 if (used >= _txq.num())
298 throw L4::Bounds_error("Invalid used descriptor number in TX queue");
299 _txq.free_descriptor(used, used);
300 }
301 }
302
303private:
306 L4Re::Util::Unique_cap<L4::Irq> _driver_notification_irq;
308 Packet *_rxpkts, *_txpkts;
309};
310
311} }
static Env const * env() noexcept
Returns the initial environment for the current task.
Definition env:96
@ Continuous
Allocate physically contiguous memory.
Definition mem_alloc:64
@ Pinned
Deprecated, use L4Re::Dma_space instead.
Definition mem_alloc:65
Unique region.
Definition rm:435
Access out of bounds.
Definition exceptions:279
@ Invalid
Invalid capability selector.
Definition capability.h:42
C++ interface for capabilities.
Definition capability.h:224
Client-side implementation for a general virtio device.
Definition l4virtio:32
int max_queue_size(int num) const
Maximum queue size allowed by the device.
Definition l4virtio:230
void driver_connect(L4::Cap< L4virtio::Device > srvcap, bool manage_notify=true)
Contacts the device and starts the initial handshake.
Definition l4virtio:56
void send(Virtqueue &queue, l4_uint16_t descno)
Send a request to the device.
Definition l4virtio:312
int register_ds(L4::Cap< L4Re::Dataspace > ds, l4_umword_t offset, l4_umword_t size, l4_uint64_t *devaddr)
Share a dataspace with the device.
Definition l4virtio:196
int config_queue(int num, unsigned size, l4_uint64_t desc_addr, l4_uint64_t avail_addr, l4_uint64_t used_addr)
Send the virtqueue configuration to the device.
Definition l4virtio:212
int driver_acknowledge()
Finalize handshake with the device.
Definition l4virtio:156
Simple class for accessing a virtio net device.
Definition virtio-net:31
int rx_queue_size() const
Return the maximum receive queue size allowed by the device.
Definition virtio-net:47
void finish_rx(l4_uint16_t descno)
Free an RX descriptor number to make it available for the RX queue again.
Definition virtio-net:242
Packet & rx_pkt(l4_uint16_t descno)
Return a reference to the RX packet buffer of the specified descriptor, e.g.
Definition virtio-net:192
l4virtio_net_config_t const & device_config() const
Return a reference to the device configuration.
Definition virtio-net:170
int bind_rx_notification_irq(L4::Cap< L4::Thread > thread, l4_umword_t label)
Bind the rx notification IRQ to the specified thread.
Definition virtio-net:181
void setup_device(L4::Cap< L4virtio::Device > srvcap, L4::Cap< L4Re::Dataspace > queue_ds=L4::Cap< L4Re::Dataspace >::Invalid)
Establish a connection to the device and set up shared memory.
Definition virtio-net:70
l4_uint16_t wait_rx(l4_uint32_t *len=nullptr)
Block until a network packet has been received from the device and return the descriptor number.
Definition virtio-net:214
int tx_queue_size() const
Return the maximum transmit queue size allowed by the device.
Definition virtio-net:54
bool tx(std::function< l4_uint32_t(Packet &)> prepare)
Attempt to allocate a descriptor in the TX queue and transmit the packet, after calling the prepare c...
Definition virtio-net:272
void queue_rx()
Queue new available descriptors in the RX queue.
Definition virtio-net:250
Driver-side implementation of a Virtqueue.
Definition virtqueue:471
void free_descriptor(l4_uint16_t head, l4_uint16_t tail)
Free a chained list of descriptors in the descriptor queue.
Definition virtqueue:631
l4_uint16_t find_next_used(l4_uint32_t *len=nullptr)
Return the next finished block.
Definition virtqueue:609
Pointer used in virtio descriptors.
Definition virtqueue:48
unsigned num() const
Definition virtqueue:404
Dataspace interface.
Environment interface.
Error helper.
Base exceptions.
unsigned long l4_umword_t
Unsigned machine word.
Definition l4int.h:40
unsigned char l4_uint8_t
Unsigned 8bit value.
Definition l4int.h:25
unsigned int l4_uint32_t
Unsigned 32bit value.
Definition l4int.h:29
unsigned short int l4_uint16_t
Unsigned 16bit value.
Definition l4int.h:27
unsigned long long l4_uint64_t
Unsigned 64bit value.
Definition l4int.h:31
@ L4_EINVAL
Invalid argument.
Definition err.h:46
@ L4_ENODEV
No such thing.
Definition err.h:44
long l4_error(l4_msgtag_t tag) L4_NOTHROW
Get IPC error code if any or message tag label otherwise for an IPC call.
Definition ipc.h:646
#define L4_PAGESHIFT
Size of a page, log2-based.
Definition consts.h:26
l4_addr_t l4_round_size(l4_addr_t value, unsigned char bits) L4_NOTHROW
Round value up to the next alignment with bits size.
Definition consts.h:484
void l4virtio_set_feature(l4_uint32_t *feature_map, unsigned feat)
Set the given feature bit in a feature map.
Definition virtio.h:275
@ L4VIRTIO_FEATURE_VERSION_1
Virtio protocol version 1 supported. Must be 1 for L4virtio.
Definition virtio.h:104
@ L4VIRTIO_ID_NET
Virtual ethernet card.
Definition virtio.h:64
Common constants.
Unique_cap< T > make_unique_cap()
Allocate a capability slot and wrap it in an Unique_cap.
Definition unique_cap:55
L4::Detail::Unique_cap_impl< T, Smart_cap_auto< L4_FP_ALL_SPACES > > Unique_cap
Unique capability that implements automatic free and unmap of the capability selector.
Definition unique_cap:43
long chksys(long err, char const *extra="", long ret=0)
Generate C++ exception on error.
Definition error_helper:72
T chkcap(T &&cap, char const *extra="", long err=-L4_ENOMEM)
Check for valid capability or raise C++ exception.
Definition error_helper:149
Cap< T > make_cap_rw(L4::Cap< T > cap) noexcept
Make an L4::Ipc::Cap<T> for the given capability with L4_CAP_FPAGE_RW rights.
Definition ipc_types:795
L4-VIRTIO Transport C++ API.
Definition l4virtio:26
@ RW
Readable and writable region.
Definition rm:139
@ Search_addr
Search for a suitable address range.
Definition rm:113
Structure for a network packet (header including data) with maximum size, assuming that no extra feat...
Definition virtio-net:38
Device configuration for network devices.
Definition virtio_net.h:35
Header structure of a request for a network device.
Definition virtio_net.h:21
Unique_cap / Unique_del_cap.