The Pedigree Project 0.1
VirtioNet.h
1/* Copyright (c) 2026, Pedigree Developers. SPDX-License-Identifier: ISC */
2#ifndef VIRTIO_NET_H
3#define VIRTIO_NET_H
4
5#include "pedigree/kernel/machine/IrqHandler.h"
6#include "pedigree/kernel/machine/Network.h"
7#include "pedigree/kernel/process/Mutex.h"
8#include "pedigree/kernel/processor/MemoryRegion.h"
9
10#include "modules/drivers/common/virtio/VirtioPci.h"
11#include "modules/drivers/common/virtio/Virtqueue.h"
12
13class VirtioNet : public Network, public IrqHandler {
14 public:
15 explicit VirtioNet(Device* pciDevice);
16 ~VirtioNet() override;
17
18 bool initialise();
19 void getName(String& name) override;
20 bool send(size_t length, uintptr_t buffer) override;
21 bool setStationInfo(const StationInfo& info) override;
22 const StationInfo& getStationInfo() override;
23 bool isConnected() override;
24 IrqDisposition irq(irq_id_t number) override;
25
26 private:
27 static constexpr size_t BufferSize = 2048;
28 static constexpr size_t MaxRxSlots = 64;
29 static constexpr size_t MaxTxSlots = 32;
30
31 struct Slot {
32 uint8_t* data;
33 physical_uintptr_t physical;
34 bool busy;
35 };
36
37 void reclaimTransmit();
38 bool postReceive(Slot& slot);
39
40 Device* m_PciDevice;
41 Virtio::PciTransport m_Transport;
42 Virtio::Queue m_RxQueue;
43 Virtio::Queue m_TxQueue;
44 MemoryRegion m_RxRegion;
45 MemoryRegion m_TxRegion;
46 Mutex m_Lock;
47 Slot m_RxSlots[MaxRxSlots];
48 Slot m_TxSlots[MaxTxSlots];
49 size_t m_RxCount;
50 size_t m_TxCount;
51 size_t m_NextTx;
52 irq_id_t m_IrqId;
53 bool m_HasStatus;
54 bool m_NetworkRegistered;
55 bool m_Initialised;
56 bool m_Stopping;
57
58 VirtioNet(const VirtioNet&) = delete;
59 VirtioNet& operator=(const VirtioNet&) = delete;
60};
61
62#endif
Special memory entity in the kernel's virtual address space.
Definition Mutex.h:56
IrqDisposition irq(irq_id_t number) override
Definition VirtioNet.cc:241
const StationInfo & getStationInfo() override
Definition VirtioNet.cc:225
bool setStationInfo(const StationInfo &info) override
Definition VirtioNet.cc:208
bool isConnected() override
Definition VirtioNet.cc:229
void getName(String &name) override
Definition VirtioNet.cc:152
bool send(size_t length, uintptr_t buffer) override
Definition VirtioNet.cc:169
IrqDisposition
Definition IrqHandler.h:31