diff options
author | Andreas Kling <kling@serenityos.org> | 2022-12-11 18:48:20 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-12-13 11:20:11 +0100 |
commit | 30d3f2789e3478da415ccdc6766872d3d58918e2 (patch) | |
tree | 06bb64b11b320d38a583cb8b4756432ab0d0200d /Kernel/Net/NetworkingManagement.cpp | |
parent | 9a849c10ae4f376034cfa34a730b1de5fa343601 (diff) | |
download | serenity-30d3f2789e3478da415ccdc6766872d3d58918e2.zip |
Kernel: Propagate errors during network adapter detection/initialization
When scanning for network adapters, we give each driver a chance to
claim the PCI device and whoever claims it first gets to keep it.
Before this patch, the driver API returned a LockRefPtr<AdapterType>,
which made it impossible to propagate errors that occurred during
detection and/or initialization.
This patch changes the API so that errors can bubble all the way out
the PCI enumeration in NetworkingManagement::initialize() where we
perform all the network adapter auto-detection on boot.
When we eventually start to support hot-plugging network adapter in the
future, it will be even more important to propagate errors instead of
swallowing them.
Importantly, before this patch, some errors were "handled" by panicking
the kernel. This is no longer the case.
7 FIXMEs were killed in the making of this commit. :^)
Diffstat (limited to 'Kernel/Net/NetworkingManagement.cpp')
-rw-r--r-- | Kernel/Net/NetworkingManagement.cpp | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/Kernel/Net/NetworkingManagement.cpp b/Kernel/Net/NetworkingManagement.cpp index e3fe772bf6..2da3aef1e9 100644 --- a/Kernel/Net/NetworkingManagement.cpp +++ b/Kernel/Net/NetworkingManagement.cpp @@ -93,19 +93,19 @@ ErrorOr<NonnullOwnPtr<KString>> NetworkingManagement::generate_interface_name_fr return name; } -UNMAP_AFTER_INIT LockRefPtr<NetworkAdapter> NetworkingManagement::determine_network_device(PCI::DeviceIdentifier const& device_identifier) const +UNMAP_AFTER_INIT ErrorOr<NonnullLockRefPtr<NetworkAdapter>> NetworkingManagement::determine_network_device(PCI::DeviceIdentifier const& device_identifier) const { - if (auto candidate = E1000NetworkAdapter::try_to_initialize(device_identifier); !candidate.is_null()) - return candidate; - if (auto candidate = E1000ENetworkAdapter::try_to_initialize(device_identifier); !candidate.is_null()) - return candidate; - if (auto candidate = RTL8139NetworkAdapter::try_to_initialize(device_identifier); !candidate.is_null()) - return candidate; - if (auto candidate = RTL8168NetworkAdapter::try_to_initialize(device_identifier); !candidate.is_null()) - return candidate; - if (auto candidate = NE2000NetworkAdapter::try_to_initialize(device_identifier); !candidate.is_null()) - return candidate; - return {}; + if (auto candidate = TRY(E1000NetworkAdapter::try_to_initialize(device_identifier))) + return candidate.release_nonnull(); + if (auto candidate = TRY(E1000ENetworkAdapter::try_to_initialize(device_identifier))) + return candidate.release_nonnull(); + if (auto candidate = TRY(RTL8139NetworkAdapter::try_to_initialize(device_identifier))) + return candidate.release_nonnull(); + if (auto candidate = TRY(RTL8168NetworkAdapter::try_to_initialize(device_identifier))) + return candidate.release_nonnull(); + if (auto candidate = TRY(NE2000NetworkAdapter::try_to_initialize(device_identifier))) + return candidate.release_nonnull(); + return Error::from_string_literal("Unsupported network adapter"); } bool NetworkingManagement::initialize() @@ -115,8 +115,12 @@ bool NetworkingManagement::initialize() // Note: PCI class 2 is the class of Network devices if (device_identifier.class_code().value() != 0x02) return; - if (auto adapter = determine_network_device(device_identifier); !adapter.is_null()) - m_adapters.with([&](auto& adapters) { adapters.append(adapter.release_nonnull()); }); + auto result = determine_network_device(device_identifier); + if (result.is_error()) { + dmesgln("Failed to initialize network adapter ({} {}): {}", device_identifier.address(), device_identifier.hardware_id(), result.error()); + return; + } + m_adapters.with([&](auto& adapters) { adapters.append(result.release_value()); }); })); } auto loopback = LoopbackAdapter::try_create(); |