Kernel: Implement loopback network interface

This commit is contained in:
2025-05-11 03:22:19 +03:00
parent a8844ddd28
commit 8e0a56b49a
12 changed files with 164 additions and 21 deletions

View File

@@ -20,6 +20,7 @@ namespace Kernel
NVMeController,
NVMeNamespace,
Ethernet,
Loopback,
TmpFS,
};

View File

@@ -34,7 +34,8 @@ namespace Kernel
protected:
E1000(PCI::Device& pci_device)
: m_pci_device(pci_device)
: NetworkInterface(Type::Ethernet)
, m_pci_device(pci_device)
{ }
BAN::ErrorOr<void> initialize();

View File

@@ -0,0 +1,39 @@
#pragma once
#include <kernel/Networking/NetworkInterface.h>
namespace Kernel
{
class LoopbackInterface : public NetworkInterface
{
public:
static constexpr size_t buffer_size = BAN::numeric_limits<uint16_t>::max() + 1;
public:
static BAN::ErrorOr<BAN::RefPtr<LoopbackInterface>> create();
BAN::MACAddress get_mac_address() const override { return {}; }
bool link_up() override { return true; }
int link_speed() override { return 1000; }
size_t payload_mtu() const override { return buffer_size - sizeof(EthernetHeader); }
protected:
LoopbackInterface()
: NetworkInterface(Type::Loopback)
{}
BAN::ErrorOr<void> send_bytes(BAN::MACAddress destination, EtherType protocol, BAN::ConstByteSpan) override;
bool can_read_impl() const override { return false; }
bool can_write_impl() const override { return false; }
bool has_error_impl() const override { return false; }
private:
SpinLock m_buffer_lock;
BAN::UniqPtr<VirtualRange> m_buffer;
};
}

View File

@@ -32,10 +32,11 @@ namespace Kernel
enum class Type
{
Ethernet,
Loopback,
};
public:
NetworkInterface();
NetworkInterface(Type);
virtual ~NetworkInterface() {}
virtual BAN::MACAddress get_mac_address() const = 0;
@@ -49,6 +50,8 @@ namespace Kernel
BAN::IPv4Address get_gateway() const { return m_gateway; }
void set_gateway(BAN::IPv4Address new_gateway) { m_gateway = new_gateway; }
Type type() const { return m_type; }
virtual bool link_up() = 0;
virtual int link_speed() = 0;

View File

@@ -22,7 +22,7 @@ namespace Kernel
BAN::ErrorOr<void> add_interface(PCI::Device& pci_device);
BAN::Vector<BAN::RefPtr<NetworkInterface>> interfaces() { return m_interfaces; }
BAN::Vector<BAN::RefPtr<NetworkInterface>>& interfaces() { return m_interfaces; }
BAN::ErrorOr<BAN::RefPtr<Socket>> create_socket(Socket::Domain, Socket::Type, mode_t, uid_t, gid_t);
@@ -31,6 +31,8 @@ namespace Kernel
private:
NetworkManager() {}
BAN::ErrorOr<void> add_interface(BAN::RefPtr<NetworkInterface>);
private:
BAN::UniqPtr<IPv4Layer> m_ipv4_layer;
BAN::Vector<BAN::RefPtr<NetworkInterface>> m_interfaces;

View File

@@ -26,7 +26,8 @@ namespace Kernel
protected:
RTL8169(PCI::Device& pci_device)
: m_pci_device(pci_device)
: NetworkInterface(Type::Ethernet)
, m_pci_device(pci_device)
{ }
BAN::ErrorOr<void> initialize();