13#include <l4/cxx/minmax>
20#include <l4/l4virtio/client/l4virtio>
21#include <l4/l4virtio/l4virtio>
22#include <l4/l4virtio/virtio_net.h>
23#include <l4/l4virtio/virtqueue>
25namespace L4virtio {
namespace Driver {
75 L4Re::Util::Dbg msg_dev = L4Re::Util::Dbg{2})
93 L4virtio::Virtqueue::Desc_align);
95 L4virtio::Virtqueue::Desc_align);
96 auto txpktoff = rxpktoff + rxqsz *
sizeof(Packet);
97 auto totalsz = txpktoff + txqsz *
sizeof(Packet);
106 "Allocate queue dataspace capability");
107 L4Re::chksys(e->mem_alloc()->alloc(totalsz, _queue_ds.get(),
110 "Allocate memory for virtio structures");
117 "Attach dataspace for virtio structures");
121 "Register queue dataspace with device");
123 _rxq.
init_queue(rxqsz, _queue_region.
get() + rxqoff, msg_dev);
124 _txq.
init_queue(txqsz, _queue_region.
get() + txqoff, msg_dev);
133 _rxpkts =
reinterpret_cast<Packet*
>(_queue_region.
get() + rxpktoff);
134 _txpkts =
reinterpret_cast<Packet*
>(_queue_region.
get() + txpktoff);
137 for (
l4_uint16_t descno = 0; descno < rxqsz; ++descno)
139 auto &desc = _rxq.
desc(descno);
141 descno *
sizeof(Packet));
142 desc.len =
sizeof(Packet);
143 desc.flags.write() = 1;
145 for (
l4_uint16_t descno = 0; descno < txqsz; ++descno)
147 auto &desc = _txq.
desc(descno);
149 descno *
sizeof(Packet));
150 desc.len =
sizeof(Packet);
154 _driver_notification_irq =
156 "Allocate notification capability");
159 "Create irq for notifications from device");
161 L4Re::chksys(_device->bind(0, _driver_notification_irq.get()),
162 "Bind driver notification interrupt");
187 return l4_error(_driver_notification_irq->bind_thread(thread, label));
194 {
return _driver_notification_irq.get(); }
204 if (descno >= _rxq.num())
206 return _rxpkts[descno];
225 if (len && descno != Virtqueue::Eoq)
228 *len = cxx::min(*len -
sizeof(_rxpkts[0].hdr),
sizeof(_rxpkts[0].data));
254 if (descno != Virtqueue::Eoq)
257 L4Re::chksys(_driver_notification_irq->receive(),
"Wait for RX");
273 _rxq.free_descriptor(descno, descno);
282 while ((descno = _rxq.alloc_descriptor()) != Virtqueue::Eoq)
283 _rxq.enqueue_descriptor(descno);
303 auto descno = _txq.alloc_descriptor();
304 if (descno == Virtqueue::Eoq)
307 free_used_tx_descriptors();
308 descno = _txq.alloc_descriptor();
309 if (descno == Virtqueue::Eoq)
313 auto &pkt = _txpkts[descno];
314 auto &desc = _txq.desc(descno);
315 desc.len =
sizeof(pkt.hdr) + prepare(pkt);
321 void free_used_tx_descriptors()
326 if (used >= _txq.
num())
337 Packet *_rxpkts, *_txpkts;
static Env const * env() noexcept
Returns the initial environment for the current task.
@ Continuous
Allocate physically contiguous memory.
@ Pinned
Deprecated, use L4Re::Dma_space instead.
@ Invalid
Invalid capability selector.
C++ interface for capabilities.
Client-side implementation for a general virtio device.
int max_queue_size(int num) const
Maximum queue size allowed by the device.
void driver_connect(L4::Cap< L4virtio::Device > srvcap, bool manage_notify=true)
Contacts the device and starts the initial handshake.
void send(Virtqueue &queue, l4_uint16_t descno)
Send a request to the device.
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.
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.
int driver_acknowledge()
Finalize handshake with the device.
Simple class for accessing a virtio net device.
L4::Cap< L4::Irq > get_rx_notification_irq() const
Get notification IRQ.
void setup_device(L4::Cap< L4virtio::Device > srvcap, L4::Cap< L4Re::Dataspace > queue_ds=L4::Cap< L4Re::Dataspace >::Invalid, L4Re::Util::Dbg msg_dev=L4Re::Util::Dbg{2})
Establish a connection to the device and set up shared memory.
int rx_queue_size() const
Return the maximum receive queue size allowed by the device.
void finish_rx(l4_uint16_t descno)
Free an RX descriptor number to make it available for the RX queue again.
l4_uint16_t poll_rx(l4_uint32_t *len=nullptr)
Poll if a network packet has been received from the device and return the descriptor number,...
Packet & rx_pkt(l4_uint16_t descno)
Return a reference to the RX packet buffer of the specified descriptor, e.g.
l4virtio_net_config_t const & device_config() const
Return a reference to the device configuration.
int bind_rx_notification_irq(L4::Cap< L4::Thread > thread, l4_umword_t label)
Bind the rx notification IRQ to the specified thread.
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.
int tx_queue_size() const
Return the maximum transmit queue size allowed by the device.
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...
void queue_rx()
Queue new available descriptors in the RX queue.
Driver-side implementation of a Virtqueue.
void free_descriptor(l4_uint16_t head, l4_uint16_t tail)
Free a chained list of descriptors in the descriptor queue.
l4_uint16_t find_next_used(l4_uint32_t *len=nullptr)
Return the next finished block.
Desc & desc(l4_uint16_t descno)
Return a reference to a descriptor in the descriptor table.
void init_queue(unsigned num, void *desc, void *avail, void *used, L4Re::Util::Dbg msg_dev=L4Re::Util::Dbg{2})
Initialize this virtqueue.
Pointer used in virtio descriptors.
Ptr< void > addr
Address stored in descriptor.
unsigned long avail_offset() const
Get the offset of the available ring from the descriptor table.
static unsigned long total_size(unsigned num)
Calculate the total size for a virtqueue of the given dimensions.
unsigned long used_offset() const
Get the offset of the used ring from the descriptor table.
T get() const noexcept
Return the address.
unsigned long l4_umword_t
Unsigned machine word.
unsigned char l4_uint8_t
Unsigned 8bit value.
unsigned int l4_uint32_t
Unsigned 32bit value.
unsigned short int l4_uint16_t
Unsigned 16bit value.
unsigned long long l4_uint64_t
Unsigned 64bit value.
@ L4_EINVAL
Invalid argument.
@ L4_ENODEV
No such thing.
l4_ret_t l4_error(l4_msgtag_t tag) L4_NOTHROW
Get IPC error code if any or message tag label otherwise for an IPC call.
#define L4_PAGESHIFT
Size of a page, log2-based.
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.
void l4virtio_set_feature(l4_uint32_t *feature_map, unsigned feat)
Set the given feature bit in a feature map.
@ L4VIRTIO_FEATURE_VERSION_1
Virtio protocol version 1 supported. Must be 1 for L4virtio.
@ L4VIRTIO_ID_NET
Virtual ethernet card.
Unique_cap< T > make_unique_cap()
Allocate a capability slot and wrap it in an Unique_cap.
L4::Detail::Unique_cap_impl< T, L4Re::Util::Smart_cap_auto< L4_FP_ALL_SPACES > > Unique_cap
Unique capability that implements automatic free and unmap of the capability selector.
T chkcap(T &&cap, char const *extra="", l4_ret_t err=-L4_ENOMEM)
Check for valid capability or raise C++ exception.
l4_ret_t chksys(l4_ret_t err, char const *extra="", l4_ret_t ret=0)
Generate C++ exception on error.
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.
L4-VIRTIO Transport C++ API.
@ RW
Readable and writable region.
@ Search_addr
Search for a suitable address range.
Structure for a network packet (header including data) with maximum size, assuming that no extra feat...
l4_uint32_t device
device ID
l4_uint32_t num_queues
number of virtqueues
Device configuration for network devices.
Unique_cap / Unique_del_cap.