Browse Source

Kernel: Convert network adapter names to Kernel::KString

Another step of incremental progress of removing `AK::String` from the
kernel, to harden against OOM.
Brian Gianforcaro 3 years ago
parent
commit
2770433d30

+ 4 - 4
Kernel/Net/E1000ENetworkAdapter.cpp

@@ -180,14 +180,14 @@ static bool is_valid_device_id(u16 device_id)
     }
 }
 
-UNMAP_AFTER_INIT RefPtr<E1000ENetworkAdapter> E1000ENetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
+UNMAP_AFTER_INIT RefPtr<E1000ENetworkAdapter> E1000ENetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier, NonnullOwnPtr<KString> interface_name)
 {
     if (pci_device_identifier.hardware_id().vendor_id != PCI::VendorID::Intel)
         return {};
     if (!is_valid_device_id(pci_device_identifier.hardware_id().device_id))
         return {};
     u8 irq = pci_device_identifier.interrupt_line().value();
-    auto adapter = adopt_ref_if_nonnull(new (nothrow) E1000ENetworkAdapter(pci_device_identifier.address(), irq));
+    auto adapter = adopt_ref_if_nonnull(new (nothrow) E1000ENetworkAdapter(pci_device_identifier.address(), irq, move(interface_name)));
     if (!adapter)
         return {};
     if (adapter->initialize())
@@ -228,8 +228,8 @@ UNMAP_AFTER_INIT bool E1000ENetworkAdapter::initialize()
     return true;
 }
 
-UNMAP_AFTER_INIT E1000ENetworkAdapter::E1000ENetworkAdapter(PCI::Address address, u8 irq)
-    : E1000NetworkAdapter(address, irq)
+UNMAP_AFTER_INIT E1000ENetworkAdapter::E1000ENetworkAdapter(PCI::Address address, u8 irq, NonnullOwnPtr<KString> interface_name)
+    : E1000NetworkAdapter(address, irq, move(interface_name))
 {
 }
 

+ 2 - 2
Kernel/Net/E1000ENetworkAdapter.h

@@ -21,7 +21,7 @@ namespace Kernel {
 class E1000ENetworkAdapter final
     : public E1000NetworkAdapter {
 public:
-    static RefPtr<E1000ENetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&);
+    static RefPtr<E1000ENetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&, NonnullOwnPtr<KString>);
 
     virtual bool initialize() override;
 
@@ -30,7 +30,7 @@ public:
     virtual StringView purpose() const override { return class_name(); }
 
 private:
-    E1000ENetworkAdapter(PCI::Address, u8 irq);
+    E1000ENetworkAdapter(PCI::Address, u8 irq, NonnullOwnPtr<KString>);
 
     virtual StringView class_name() const override { return "E1000ENetworkAdapter"sv; }
 

+ 5 - 5
Kernel/Net/E1000NetworkAdapter.cpp

@@ -158,14 +158,14 @@ UNMAP_AFTER_INIT static bool is_valid_device_id(u16 device_id)
     }
 }
 
-UNMAP_AFTER_INIT RefPtr<E1000NetworkAdapter> E1000NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
+UNMAP_AFTER_INIT RefPtr<E1000NetworkAdapter> E1000NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier, NonnullOwnPtr<KString> interface_name)
 {
     if (pci_device_identifier.hardware_id().vendor_id != PCI::VendorID::Intel)
         return {};
     if (!is_valid_device_id(pci_device_identifier.hardware_id().device_id))
         return {};
     u8 irq = pci_device_identifier.interrupt_line().value();
-    auto adapter = adopt_ref_if_nonnull(new (nothrow) E1000NetworkAdapter(pci_device_identifier.address(), irq));
+    auto adapter = adopt_ref_if_nonnull(new (nothrow) E1000NetworkAdapter(pci_device_identifier.address(), irq, move(interface_name)));
     if (!adapter)
         return {};
     if (adapter->initialize())
@@ -219,13 +219,13 @@ UNMAP_AFTER_INIT bool E1000NetworkAdapter::initialize()
     return true;
 }
 
-UNMAP_AFTER_INIT E1000NetworkAdapter::E1000NetworkAdapter(PCI::Address address, u8 irq)
-    : PCI::Device(address)
+UNMAP_AFTER_INIT E1000NetworkAdapter::E1000NetworkAdapter(PCI::Address address, u8 irq, NonnullOwnPtr<KString> interface_name)
+    : NetworkAdapter(move(interface_name))
+    , PCI::Device(address)
     , IRQHandler(irq)
     , m_rx_descriptors_region(MM.allocate_contiguous_kernel_region(Memory::page_round_up(sizeof(e1000_rx_desc) * number_of_rx_descriptors + 16), "E1000 RX Descriptors", Memory::Region::Access::ReadWrite).release_value())
     , m_tx_descriptors_region(MM.allocate_contiguous_kernel_region(Memory::page_round_up(sizeof(e1000_tx_desc) * number_of_tx_descriptors + 16), "E1000 TX Descriptors", Memory::Region::Access::ReadWrite).release_value())
 {
-    set_interface_name(pci_address());
 }
 
 UNMAP_AFTER_INIT E1000NetworkAdapter::~E1000NetworkAdapter()

+ 2 - 2
Kernel/Net/E1000NetworkAdapter.h

@@ -20,7 +20,7 @@ class E1000NetworkAdapter : public NetworkAdapter
     , public PCI::Device
     , public IRQHandler {
 public:
-    static RefPtr<E1000NetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&);
+    static RefPtr<E1000NetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&, NonnullOwnPtr<KString>);
 
     virtual bool initialize();
 
@@ -37,7 +37,7 @@ protected:
     void setup_interrupts();
     void setup_link();
 
-    E1000NetworkAdapter(PCI::Address, u8 irq);
+    E1000NetworkAdapter(PCI::Address, u8 irq, NonnullOwnPtr<KString>);
     virtual bool handle_irq(const RegisterState&) override;
     virtual StringView class_name() const override { return "E1000NetworkAdapter"sv; }
 

+ 6 - 3
Kernel/Net/LoopbackAdapter.cpp

@@ -13,14 +13,17 @@ static bool s_loopback_initialized = false;
 
 RefPtr<LoopbackAdapter> LoopbackAdapter::try_create()
 {
-    return adopt_ref_if_nonnull(new LoopbackAdapter());
+    auto interface_name = KString::try_create("loop"sv);
+    if (interface_name.is_error())
+        return {};
+    return adopt_ref_if_nonnull(new LoopbackAdapter(interface_name.release_value()));
 }
 
-LoopbackAdapter::LoopbackAdapter()
+LoopbackAdapter::LoopbackAdapter(NonnullOwnPtr<KString> interface_name)
+    : NetworkAdapter(move(interface_name))
 {
     VERIFY(!s_loopback_initialized);
     s_loopback_initialized = true;
-    set_loopback_name();
     set_mtu(65536);
     set_mac_address({ 19, 85, 2, 9, 0x55, 0xaa });
 }

+ 1 - 1
Kernel/Net/LoopbackAdapter.h

@@ -14,7 +14,7 @@ class LoopbackAdapter final : public NetworkAdapter {
     AK_MAKE_ETERNAL
 
 private:
-    LoopbackAdapter();
+    LoopbackAdapter(NonnullOwnPtr<KString>);
 
 public:
     static RefPtr<LoopbackAdapter> try_create();

+ 5 - 6
Kernel/Net/NE2000NetworkAdapter.cpp

@@ -137,7 +137,7 @@ struct [[gnu::packed]] received_packet_header {
     u16 length;
 };
 
-UNMAP_AFTER_INIT RefPtr<NE2000NetworkAdapter> NE2000NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
+UNMAP_AFTER_INIT RefPtr<NE2000NetworkAdapter> NE2000NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier, NonnullOwnPtr<KString> interface_name)
 {
     constexpr auto ne2k_ids = Array {
         PCI::HardwareID { 0x10EC, 0x8029 }, // RealTek RTL-8029(AS)
@@ -157,16 +157,15 @@ UNMAP_AFTER_INIT RefPtr<NE2000NetworkAdapter> NE2000NetworkAdapter::try_to_initi
     if (!ne2k_ids.span().contains_slow(pci_device_identifier.hardware_id()))
         return {};
     u8 irq = pci_device_identifier.interrupt_line().value();
-    return adopt_ref_if_nonnull(new (nothrow) NE2000NetworkAdapter(pci_device_identifier.address(), irq));
+    return adopt_ref_if_nonnull(new (nothrow) NE2000NetworkAdapter(pci_device_identifier.address(), irq, move(interface_name)));
 }
 
-UNMAP_AFTER_INIT NE2000NetworkAdapter::NE2000NetworkAdapter(PCI::Address address, u8 irq)
-    : PCI::Device(address)
+UNMAP_AFTER_INIT NE2000NetworkAdapter::NE2000NetworkAdapter(PCI::Address address, u8 irq, NonnullOwnPtr<KString> interface_name)
+    : NetworkAdapter(move(interface_name))
+    , PCI::Device(address)
     , IRQHandler(irq)
     , m_io_base(PCI::get_BAR0(pci_address()) & ~3)
 {
-    set_interface_name(address);
-
     dmesgln("NE2000: Found @ {}", pci_address());
 
     dmesgln("NE2000: Port base: {}", m_io_base);

+ 2 - 2
Kernel/Net/NE2000NetworkAdapter.h

@@ -20,7 +20,7 @@ class NE2000NetworkAdapter final : public NetworkAdapter
     , public PCI::Device
     , public IRQHandler {
 public:
-    static RefPtr<NE2000NetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&);
+    static RefPtr<NE2000NetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&, NonnullOwnPtr<KString>);
 
     virtual ~NE2000NetworkAdapter() override;
 
@@ -41,7 +41,7 @@ public:
     virtual StringView purpose() const override { return class_name(); }
 
 private:
-    NE2000NetworkAdapter(PCI::Address, u8 irq);
+    NE2000NetworkAdapter(PCI::Address, u8, NonnullOwnPtr<KString>);
     virtual bool handle_irq(const RegisterState&) override;
     virtual StringView class_name() const override { return "NE2000NetworkAdapter"sv; }
 

+ 2 - 15
Kernel/Net/NetworkAdapter.cpp

@@ -15,7 +15,8 @@
 
 namespace Kernel {
 
-NetworkAdapter::NetworkAdapter()
+NetworkAdapter::NetworkAdapter(NonnullOwnPtr<KString> interface_name)
+    : m_name(move(interface_name))
 {
 }
 
@@ -165,18 +166,4 @@ void NetworkAdapter::set_ipv4_gateway(const IPv4Address& gateway)
     m_ipv4_gateway = gateway;
 }
 
-void NetworkAdapter::set_interface_name(const PCI::Address& pci_address)
-{
-    // Note: This stands for e - "Ethernet", p - "Port" as for PCI bus, "s" for slot as for PCI slot
-    auto name = String::formatted("ep{}s{}", pci_address.bus(), pci_address.device());
-    VERIFY(!NetworkingManagement::the().lookup_by_name(name));
-    m_name = move(name);
-}
-
-void NetworkAdapter::set_loopback_name()
-{
-    auto name = String("loop");
-    VERIFY(!NetworkingManagement::the().lookup_by_name(name));
-    m_name = move(name);
-}
 }

+ 3 - 6
Kernel/Net/NetworkAdapter.h

@@ -50,7 +50,7 @@ public:
 
     virtual StringView class_name() const = 0;
 
-    const String& name() const { return m_name; }
+    StringView name() const { return m_name->view(); }
     MACAddress mac_address() { return m_mac_address; }
     IPv4Address ipv4_address() const { return m_ipv4_address; }
     IPv4Address ipv4_netmask() const { return m_ipv4_netmask; }
@@ -94,14 +94,11 @@ public:
     void send_packet(ReadonlyBytes);
 
 protected:
-    NetworkAdapter();
-    void set_interface_name(const PCI::Address&);
+    NetworkAdapter(NonnullOwnPtr<KString>);
     void set_mac_address(const MACAddress& mac_address) { m_mac_address = mac_address; }
     void did_receive(ReadonlyBytes);
     virtual void send_raw(ReadonlyBytes) = 0;
 
-    void set_loopback_name();
-
 private:
     MACAddress m_mac_address;
     IPv4Address m_ipv4_address;
@@ -116,7 +113,7 @@ private:
     PacketList m_packet_queue;
     size_t m_packet_queue_size { 0 };
     PacketList m_unused_packets;
-    String m_name;
+    NonnullOwnPtr<KString> m_name;
     u32 m_packets_in { 0 };
     u32 m_bytes_in { 0 };
     u32 m_packets_out { 0 };

+ 15 - 5
Kernel/Net/NetworkingManagement.cpp

@@ -8,6 +8,7 @@
 #include <Kernel/Arch/x86/IO.h>
 #include <Kernel/Bus/PCI/API.h>
 #include <Kernel/CommandLine.h>
+#include <Kernel/KString.h>
 #include <Kernel/Memory/AnonymousVMObject.h>
 #include <Kernel/Multiboot.h>
 #include <Kernel/Net/E1000ENetworkAdapter.h>
@@ -75,15 +76,24 @@ RefPtr<NetworkAdapter> NetworkingManagement::lookup_by_name(const StringView& na
 
 UNMAP_AFTER_INIT RefPtr<NetworkAdapter> NetworkingManagement::determine_network_device(PCI::DeviceIdentifier const& device_identifier) const
 {
-    if (auto candidate = E1000NetworkAdapter::try_to_initialize(device_identifier); !candidate.is_null())
+    // Note: This stands for e - "Ethernet", p - "Port" as for PCI bus, "s" for slot as for PCI slot
+    auto name = String::formatted("ep{}s{}", device_identifier.address().bus(), device_identifier.address().device());
+    VERIFY(!NetworkingManagement::the().lookup_by_name(name));
+
+    // TODO: We need some way to to format data into a `KString`.
+    auto interface_name_or_error = KString::try_create(name.view());
+    if (interface_name_or_error.is_error())
+        return {};
+    auto interface_name = interface_name_or_error.release_value();
+    if (auto candidate = E1000NetworkAdapter::try_to_initialize(device_identifier, move(interface_name)); !candidate.is_null())
         return candidate;
-    if (auto candidate = E1000ENetworkAdapter::try_to_initialize(device_identifier); !candidate.is_null())
+    if (auto candidate = E1000ENetworkAdapter::try_to_initialize(device_identifier, move(interface_name)); !candidate.is_null())
         return candidate;
-    if (auto candidate = RTL8139NetworkAdapter::try_to_initialize(device_identifier); !candidate.is_null())
+    if (auto candidate = RTL8139NetworkAdapter::try_to_initialize(device_identifier, move(interface_name)); !candidate.is_null())
         return candidate;
-    if (auto candidate = RTL8168NetworkAdapter::try_to_initialize(device_identifier); !candidate.is_null())
+    if (auto candidate = RTL8168NetworkAdapter::try_to_initialize(device_identifier, move(interface_name)); !candidate.is_null())
         return candidate;
-    if (auto candidate = NE2000NetworkAdapter::try_to_initialize(device_identifier); !candidate.is_null())
+    if (auto candidate = NE2000NetworkAdapter::try_to_initialize(device_identifier, move(interface_name)); !candidate.is_null())
         return candidate;
     return {};
 }

+ 5 - 5
Kernel/Net/RTL8139NetworkAdapter.cpp

@@ -112,24 +112,24 @@ namespace Kernel {
 #define RX_BUFFER_SIZE 32768
 #define TX_BUFFER_SIZE PACKET_SIZE_MAX
 
-UNMAP_AFTER_INIT RefPtr<RTL8139NetworkAdapter> RTL8139NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
+UNMAP_AFTER_INIT RefPtr<RTL8139NetworkAdapter> RTL8139NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier, NonnullOwnPtr<KString> interface_name)
 {
     constexpr PCI::HardwareID rtl8139_id = { 0x10EC, 0x8139 };
     if (pci_device_identifier.hardware_id() != rtl8139_id)
         return {};
     u8 irq = pci_device_identifier.interrupt_line().value();
-    return adopt_ref_if_nonnull(new (nothrow) RTL8139NetworkAdapter(pci_device_identifier.address(), irq));
+    return adopt_ref_if_nonnull(new (nothrow) RTL8139NetworkAdapter(pci_device_identifier.address(), irq, move(interface_name)));
 }
 
-UNMAP_AFTER_INIT RTL8139NetworkAdapter::RTL8139NetworkAdapter(PCI::Address address, u8 irq)
-    : PCI::Device(address)
+UNMAP_AFTER_INIT RTL8139NetworkAdapter::RTL8139NetworkAdapter(PCI::Address address, u8 irq, NonnullOwnPtr<KString> interface_name)
+    : NetworkAdapter(move(interface_name))
+    , PCI::Device(address)
     , IRQHandler(irq)
     , m_io_base(PCI::get_BAR0(pci_address()) & ~1)
     , m_rx_buffer(MM.allocate_contiguous_kernel_region(Memory::page_round_up(RX_BUFFER_SIZE + PACKET_SIZE_MAX), "RTL8139 RX", Memory::Region::Access::ReadWrite).release_value())
     , m_packet_buffer(MM.allocate_contiguous_kernel_region(Memory::page_round_up(PACKET_SIZE_MAX), "RTL8139 Packet buffer", Memory::Region::Access::ReadWrite).release_value())
 {
     m_tx_buffers.ensure_capacity(RTL8139_TX_BUFFER_COUNT);
-    set_interface_name(address);
 
     dmesgln("RTL8139: Found @ {}", pci_address());
 

+ 2 - 2
Kernel/Net/RTL8139NetworkAdapter.h

@@ -22,7 +22,7 @@ class RTL8139NetworkAdapter final : public NetworkAdapter
     , public PCI::Device
     , public IRQHandler {
 public:
-    static RefPtr<RTL8139NetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&);
+    static RefPtr<RTL8139NetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&, NonnullOwnPtr<KString>);
 
     virtual ~RTL8139NetworkAdapter() override;
 
@@ -34,7 +34,7 @@ public:
     virtual StringView purpose() const override { return class_name(); }
 
 private:
-    RTL8139NetworkAdapter(PCI::Address, u8 irq);
+    RTL8139NetworkAdapter(PCI::Address, u8 irq, NonnullOwnPtr<KString>);
     virtual bool handle_irq(const RegisterState&) override;
     virtual StringView class_name() const override { return "RTL8139NetworkAdapter"sv; }
 

+ 5 - 6
Kernel/Net/RTL8168NetworkAdapter.cpp

@@ -181,14 +181,14 @@ namespace Kernel {
 #define TX_BUFFER_SIZE 0x1FF8
 #define RX_BUFFER_SIZE 0x1FF8 // FIXME: this should be increased (0x3FFF)
 
-UNMAP_AFTER_INIT RefPtr<RTL8168NetworkAdapter> RTL8168NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
+UNMAP_AFTER_INIT RefPtr<RTL8168NetworkAdapter> RTL8168NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier, NonnullOwnPtr<KString> interface_name)
 {
     if (pci_device_identifier.hardware_id().vendor_id != PCI::VendorID::Realtek)
         return {};
     if (pci_device_identifier.hardware_id().device_id != 0x8168)
         return {};
     u8 irq = pci_device_identifier.interrupt_line().value();
-    return adopt_ref_if_nonnull(new (nothrow) RTL8168NetworkAdapter(pci_device_identifier.address(), irq));
+    return adopt_ref_if_nonnull(new (nothrow) RTL8168NetworkAdapter(pci_device_identifier.address(), irq, move(interface_name)));
 }
 
 bool RTL8168NetworkAdapter::determine_supported_version() const
@@ -236,15 +236,14 @@ bool RTL8168NetworkAdapter::determine_supported_version() const
     }
 }
 
-UNMAP_AFTER_INIT RTL8168NetworkAdapter::RTL8168NetworkAdapter(PCI::Address address, u8 irq)
-    : PCI::Device(address)
+UNMAP_AFTER_INIT RTL8168NetworkAdapter::RTL8168NetworkAdapter(PCI::Address address, u8 irq, NonnullOwnPtr<KString> interface_name)
+    : NetworkAdapter(move(interface_name))
+    , PCI::Device(address)
     , IRQHandler(irq)
     , m_io_base(PCI::get_BAR0(pci_address()) & ~1)
     , m_rx_descriptors_region(MM.allocate_contiguous_kernel_region(Memory::page_round_up(sizeof(TXDescriptor) * (number_of_rx_descriptors + 1)), "RTL8168 RX", Memory::Region::Access::ReadWrite).release_value())
     , m_tx_descriptors_region(MM.allocate_contiguous_kernel_region(Memory::page_round_up(sizeof(RXDescriptor) * (number_of_tx_descriptors + 1)), "RTL8168 TX", Memory::Region::Access::ReadWrite).release_value())
 {
-    set_interface_name(address);
-
     dmesgln("RTL8168: Found @ {}", pci_address());
     dmesgln("RTL8168: I/O port base: {}", m_io_base);
 

+ 2 - 2
Kernel/Net/RTL8168NetworkAdapter.h

@@ -22,7 +22,7 @@ class RTL8168NetworkAdapter final : public NetworkAdapter
     , public PCI::Device
     , public IRQHandler {
 public:
-    static RefPtr<RTL8168NetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&);
+    static RefPtr<RTL8168NetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&, NonnullOwnPtr<KString>);
 
     virtual ~RTL8168NetworkAdapter() override;
 
@@ -38,7 +38,7 @@ private:
     static const size_t number_of_rx_descriptors = 64;
     static const size_t number_of_tx_descriptors = 16;
 
-    RTL8168NetworkAdapter(PCI::Address, u8 irq);
+    RTL8168NetworkAdapter(PCI::Address, u8 irq, NonnullOwnPtr<KString>);
 
     virtual bool handle_irq(const RegisterState&) override;
     virtual StringView class_name() const override { return "RTL8168NetworkAdapter"sv; }

+ 3 - 2
Kernel/Net/Socket.cpp

@@ -170,9 +170,10 @@ KResult Socket::getsockopt(OpenFileDescription&, int level, int option, Userspac
         if (size < IFNAMSIZ)
             return EINVAL;
         if (m_bound_interface) {
-            const auto& name = m_bound_interface->name();
+            auto name = m_bound_interface->name();
             auto length = name.length() + 1;
-            TRY(copy_to_user(static_ptr_cast<char*>(value), name.characters(), length));
+            auto characters = name.characters_without_null_termination();
+            TRY(copy_to_user(static_ptr_cast<char*>(value), characters, length));
             size = length;
             return copy_to_user(value_size, &size);
         } else {