Kernel: Start work on network stack

This commit is contained in:
Bananymous 2024-02-01 23:38:06 +02:00
parent f4e86028d0
commit 99eed9c37a
10 changed files with 224 additions and 27 deletions

View File

@ -52,6 +52,9 @@ set(KERNEL_SOURCES
kernel/Networking/E1000/E1000.cpp
kernel/Networking/E1000/E1000E.cpp
kernel/Networking/NetworkInterface.cpp
kernel/Networking/NetworkManager.cpp
kernel/Networking/NetworkSocket.cpp
kernel/Networking/UDPSocket.cpp
kernel/OpenFileDescriptorSet.cpp
kernel/Panic.cpp
kernel/PCI.cpp

View File

@ -0,0 +1,39 @@
#pragma once
#include <BAN/Vector.h>
#include <kernel/FS/TmpFS/FileSystem.h>
#include <kernel/Networking/NetworkInterface.h>
#include <kernel/Networking/NetworkSocket.h>
#include <kernel/PCI.h>
namespace Kernel
{
class NetworkManager : public TmpFileSystem
{
public:
enum class SocketType
{
STREAM,
DGRAM,
SEQPACKET,
};
public:
static BAN::ErrorOr<void> initialize();
static NetworkManager& get();
BAN::ErrorOr<void> add_interface(PCI::Device& pci_device);
BAN::ErrorOr<void> bind_socket(int port, BAN::RefPtr<NetworkSocket>);
BAN::ErrorOr<BAN::RefPtr<NetworkSocket>> create_socket(SocketType, mode_t, uid_t, gid_t);
private:
NetworkManager();
private:
BAN::Vector<BAN::RefPtr<NetworkInterface>> m_interfaces;
BAN::HashMap<int, BAN::RefPtr<NetworkSocket>> m_bound_sockets;
};
}

View File

@ -0,0 +1,21 @@
#pragma once
#include <kernel/FS/TmpFS/Inode.h>
#include <kernel/Networking/NetworkInterface.h>
namespace Kernel
{
class NetworkSocket : public TmpInode
{
public:
void bind_interface(NetworkInterface*);
protected:
NetworkSocket(mode_t mode, uid_t uid, gid_t gid);
protected:
NetworkInterface* m_interface = nullptr;
};
}

View File

@ -0,0 +1,29 @@
#pragma once
#include <kernel/Networking/NetworkInterface.h>
#include <kernel/Networking/NetworkSocket.h>
namespace Kernel
{
class UDPSocket final : public NetworkSocket
{
public:
static BAN::ErrorOr<BAN::RefPtr<UDPSocket>> create(mode_t, uid_t, gid_t);
void bind_interface(NetworkInterface*);
protected:
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
private:
UDPSocket(mode_t, uid_t, gid_t);
private:
NetworkInterface* m_interface = nullptr;
friend class BAN::RefPtr<UDPSocket>;
};
}

View File

@ -1,4 +1,3 @@
#include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/IDT.h>
#include <kernel/InterruptController.h>
#include <kernel/IO.h>
@ -97,8 +96,6 @@ namespace Kernel
}
#endif
DevFileSystem::get().add_device(this);
return {};
}

View File

@ -0,0 +1,78 @@
#include <BAN/UniqPtr.h>
#include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/Networking/E1000/E1000.h>
#include <kernel/Networking/E1000/E1000E.h>
#include <kernel/Networking/NetworkManager.h>
#include <kernel/Networking/UDPSocket.h>
namespace Kernel
{
static BAN::UniqPtr<NetworkManager> s_instance;
BAN::ErrorOr<void> NetworkManager::initialize()
{
ASSERT(!s_instance);
NetworkManager* manager_ptr = new NetworkManager();
if (manager_ptr == nullptr)
return BAN::Error::from_errno(ENOMEM);
auto manager = BAN::UniqPtr<NetworkManager>::adopt(manager_ptr);
TRY(manager->TmpFileSystem::initialize(0777, 0, 0));
s_instance = BAN::move(manager);
return {};
}
NetworkManager& NetworkManager::get()
{
ASSERT(s_instance);
return *s_instance;
}
NetworkManager::NetworkManager()
: TmpFileSystem(128)
{ }
BAN::ErrorOr<void> NetworkManager::add_interface(PCI::Device& pci_device)
{
BAN::RefPtr<NetworkInterface> interface;
switch (pci_device.subclass())
{
case 0x00:
if (E1000::probe(pci_device))
{
interface = TRY(E1000::create(pci_device));
break;
}
if (E1000E::probe(pci_device))
{
interface = TRY(E1000E::create(pci_device));
break;
}
// fall through
default:
dprintln("unsupported network controller (pci {2H}.{2H}.{2H})", pci_device.class_code(), pci_device.subclass(), pci_device.prog_if());
dprintln(" vendor id: {4H}", pci_device.vendor_id());
dprintln(" device id: {4H}", pci_device.device_id());
return BAN::Error::from_errno(ENOTSUP);
}
ASSERT(interface);
TRY(m_interfaces.push_back(interface));
DevFileSystem::get().add_device(interface);
return {};
}
BAN::ErrorOr<BAN::RefPtr<NetworkSocket>> NetworkManager::create_socket(SocketType type, mode_t mode, uid_t uid, gid_t gid)
{
if (type != SocketType::DGRAM)
return BAN::Error::from_errno(EPROTOTYPE);
auto udp_socket = TRY(UDPSocket::create(mode, uid, gid));
return BAN::RefPtr<NetworkSocket>(udp_socket);
}
}

View File

@ -0,0 +1,23 @@
#include <kernel/Networking/NetworkManager.h>
#include <kernel/Networking/NetworkSocket.h>
namespace Kernel
{
NetworkSocket::NetworkSocket(mode_t mode, uid_t uid, gid_t gid)
// FIXME: what the fuck is this
: TmpInode(
NetworkManager::get(),
MUST(NetworkManager::get().allocate_inode(create_inode_info(mode, uid, gid))),
create_inode_info(mode, uid, gid)
)
{ }
void NetworkSocket::bind_interface(NetworkInterface* interface)
{
ASSERT(!m_interface);
ASSERT(interface);
m_interface = interface;
}
}

View File

@ -0,0 +1,25 @@
#include <kernel/Networking/UDPSocket.h>
namespace Kernel
{
BAN::ErrorOr<BAN::RefPtr<UDPSocket>> UDPSocket::create(mode_t mode, uid_t uid, gid_t gid)
{
return TRY(BAN::RefPtr<UDPSocket>::create(mode, uid, gid));
}
UDPSocket::UDPSocket(mode_t mode, uid_t uid, gid_t gid)
: NetworkSocket(mode, uid, gid)
{ }
BAN::ErrorOr<size_t> UDPSocket::read_impl(off_t, BAN::ByteSpan)
{
return BAN::Error::from_errno(ENOTSUP);
}
BAN::ErrorOr<size_t> UDPSocket::write_impl(off_t, BAN::ConstByteSpan)
{
return BAN::Error::from_errno(ENOTSUP);
}
}

View File

@ -2,8 +2,7 @@
#include <kernel/IO.h>
#include <kernel/Memory/PageTable.h>
#include <kernel/MMIO.h>
#include <kernel/Networking/E1000/E1000.h>
#include <kernel/Networking/E1000/E1000E.h>
#include <kernel/Networking/NetworkManager.h>
#include <kernel/PCI.h>
#include <kernel/Storage/ATA/AHCI/Controller.h>
#include <kernel/Storage/ATA/ATAController.h>
@ -184,28 +183,8 @@ namespace Kernel::PCI
}
case 0x02:
{
switch (pci_device.subclass())
{
case 0x00:
if (E1000::probe(pci_device))
{
if (auto res = E1000::create(pci_device); res.is_error())
dprintln("E1000: {}", res.error());
break;
}
if (E1000E::probe(pci_device))
{
if (auto res = E1000E::create(pci_device); res.is_error())
dprintln("E1000E: {}", res.error());
break;
}
// fall through
default:
dprintln("unsupported network controller (pci {2H}.{2H}.{2H})", pci_device.class_code(), pci_device.subclass(), pci_device.prog_if());
dprintln(" vendor id: {4H}", pci_device.vendor_id());
dprintln(" device id: {4H}", pci_device.device_id());
break;
}
if (auto res = NetworkManager::get().add_interface(pci_device); res.is_error())
dprintln("{}", res.error());
break;
}
default:

View File

@ -15,6 +15,7 @@
#include <kernel/Memory/Heap.h>
#include <kernel/Memory/kmalloc.h>
#include <kernel/Memory/PageTable.h>
#include <kernel/Networking/NetworkManager.h>
#include <kernel/PCI.h>
#include <kernel/PIC.h>
#include <kernel/Process.h>
@ -187,6 +188,8 @@ static void init2(void*)
if (auto res = PS2Controller::initialize(); res.is_error())
dprintln("{}", res.error());
MUST(NetworkManager::initialize());
// NOTE: PCI devices are the last ones to be initialized
// so other devices can reserve predefined interrupts
PCI::PCIManager::initialize();