mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 00:57:36 +00:00
Kernel: Remove specific devices from network code
By setting up the devices in init() and looping over the registered network adapters in NetworkTask_main, we can remove the remaining hard-coded adapter references from the network code. This also assigns IPs according to the default range supplied by QEMU in its slirp networking mode.
This commit is contained in:
parent
41e9ad5ea0
commit
93c16590f1
3 changed files with 44 additions and 40 deletions
|
@ -1,19 +1,19 @@
|
||||||
#include <Kernel/Lock.h>
|
#include <Kernel/Lock.h>
|
||||||
#include <Kernel/Net/ARP.h>
|
#include <Kernel/Net/ARP.h>
|
||||||
#include <Kernel/Net/E1000NetworkAdapter.h>
|
|
||||||
#include <Kernel/Net/EtherType.h>
|
#include <Kernel/Net/EtherType.h>
|
||||||
#include <Kernel/Net/EthernetFrameHeader.h>
|
#include <Kernel/Net/EthernetFrameHeader.h>
|
||||||
#include <Kernel/Net/ICMP.h>
|
#include <Kernel/Net/ICMP.h>
|
||||||
#include <Kernel/Net/IPv4.h>
|
#include <Kernel/Net/IPv4.h>
|
||||||
#include <Kernel/Net/IPv4Socket.h>
|
#include <Kernel/Net/IPv4Socket.h>
|
||||||
#include <Kernel/Net/LoopbackAdapter.h>
|
#include <Kernel/Net/LoopbackAdapter.h>
|
||||||
#include <Kernel/Net/RTL8139NetworkAdapter.h>
|
#include <Kernel/Net/Routing.h>
|
||||||
#include <Kernel/Net/TCP.h>
|
#include <Kernel/Net/TCP.h>
|
||||||
#include <Kernel/Net/TCPSocket.h>
|
#include <Kernel/Net/TCPSocket.h>
|
||||||
#include <Kernel/Net/UDP.h>
|
#include <Kernel/Net/UDP.h>
|
||||||
#include <Kernel/Net/UDPSocket.h>
|
#include <Kernel/Net/UDPSocket.h>
|
||||||
#include <Kernel/Process.h>
|
#include <Kernel/Process.h>
|
||||||
|
|
||||||
|
//#define NETWORK_TASK_DEBUG
|
||||||
//#define ETHERNET_DEBUG
|
//#define ETHERNET_DEBUG
|
||||||
//#define ETHERNET_VERY_DEBUG
|
//#define ETHERNET_VERY_DEBUG
|
||||||
//#define IPV4_DEBUG
|
//#define IPV4_DEBUG
|
||||||
|
@ -37,49 +37,51 @@ Lockable<HashMap<IPv4Address, MACAddress>>& arp_table()
|
||||||
|
|
||||||
void NetworkTask_main()
|
void NetworkTask_main()
|
||||||
{
|
{
|
||||||
LoopbackAdapter::the();
|
u8 octet = 15;
|
||||||
|
int pending_packets = 0;
|
||||||
auto e1000 = E1000NetworkAdapter::the();
|
NetworkAdapter::for_each([&octet, &pending_packets](auto& adapter) {
|
||||||
if (!e1000)
|
if (String(adapter.class_name()) == "LoopbackAdapter") {
|
||||||
dbgprintf("E1000 network card not found!\n");
|
adapter.set_ipv4_address({ 127, 0, 0, 1 });
|
||||||
if (e1000)
|
adapter.set_ipv4_netmask({ 255, 0, 0, 0 });
|
||||||
e1000->set_ipv4_address(IPv4Address(192, 168, 5, 2));
|
adapter.set_ipv4_gateway({ 0, 0, 0, 0 });
|
||||||
|
} else {
|
||||||
auto rtl8139 = RTL8139NetworkAdapter::the();
|
adapter.set_ipv4_address({ 10, 0, 2, octet++ });
|
||||||
if (!rtl8139)
|
adapter.set_ipv4_netmask({ 255, 255, 255, 0 });
|
||||||
dbgprintf("RTL8139 network card not found!\n");
|
adapter.set_ipv4_gateway({ 10, 0, 2, 2 });
|
||||||
if (rtl8139)
|
|
||||||
rtl8139->set_ipv4_address(IPv4Address(192, 168, 13, 201));
|
|
||||||
|
|
||||||
auto dequeue_packet = [&]() -> Optional<KBuffer> {
|
|
||||||
auto packet = LoopbackAdapter::the().dequeue_packet();
|
|
||||||
if (packet.has_value()) {
|
|
||||||
dbgprintf("Receive loopback packet (%d bytes)\n", packet.value().size());
|
|
||||||
return packet.value();
|
|
||||||
}
|
}
|
||||||
if (e1000 && e1000->has_queued_packets())
|
|
||||||
return e1000->dequeue_packet();
|
kprintf("NetworkTask: %s network adapter found: hw=%s address=%s netmask=%s gateway=%s\n",
|
||||||
if (rtl8139 && rtl8139->has_queued_packets())
|
adapter.class_name(),
|
||||||
return rtl8139->dequeue_packet();
|
adapter.mac_address().to_string().characters(),
|
||||||
return {};
|
adapter.ipv4_address().to_string().characters(),
|
||||||
|
adapter.ipv4_netmask().to_string().characters(),
|
||||||
|
adapter.ipv4_gateway().to_string().characters());
|
||||||
|
|
||||||
|
adapter.set_on_receive([&pending_packets]() {
|
||||||
|
pending_packets++;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
auto dequeue_packet = [&pending_packets]() -> Optional<KBuffer> {
|
||||||
|
Optional<KBuffer> packet;
|
||||||
|
NetworkAdapter::for_each([&packet, &pending_packets](auto& adapter) {
|
||||||
|
if (packet.has_value() || !adapter.has_queued_packets())
|
||||||
|
return;
|
||||||
|
packet = adapter.dequeue_packet();
|
||||||
|
pending_packets--;
|
||||||
|
#ifdef NETWORK_TASK_DEBUG
|
||||||
|
kprintf("NetworkTask: Dequeued packet from %s (%d bytes)\n", adapter.name().characters(), packet.value().size());
|
||||||
|
#endif
|
||||||
|
});
|
||||||
|
return packet;
|
||||||
};
|
};
|
||||||
|
|
||||||
kprintf("NetworkTask: Enter main loop.\n");
|
kprintf("NetworkTask: Enter main loop.\n");
|
||||||
for (;;) {
|
for (;;) {
|
||||||
auto packet_maybe_null = dequeue_packet();
|
auto packet_maybe_null = dequeue_packet();
|
||||||
if (!packet_maybe_null.has_value()) {
|
if (!packet_maybe_null.has_value()) {
|
||||||
(void)current->block_until("Networking", [] {
|
(void)current->block_until("Networking", [&pending_packets] {
|
||||||
if (LoopbackAdapter::the().has_queued_packets())
|
return pending_packets > 0;
|
||||||
return true;
|
|
||||||
if (auto* e1000 = E1000NetworkAdapter::the()) {
|
|
||||||
if (e1000->has_queued_packets())
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (auto* rtl8139 = RTL8139NetworkAdapter::the()) {
|
|
||||||
if (rtl8139->has_queued_packets())
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
});
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,7 @@
|
||||||
#include <Kernel/KParams.h>
|
#include <Kernel/KParams.h>
|
||||||
#include <Kernel/Multiboot.h>
|
#include <Kernel/Multiboot.h>
|
||||||
#include <Kernel/Net/E1000NetworkAdapter.h>
|
#include <Kernel/Net/E1000NetworkAdapter.h>
|
||||||
|
#include <Kernel/Net/LoopbackAdapter.h>
|
||||||
#include <Kernel/Net/NetworkTask.h>
|
#include <Kernel/Net/NetworkTask.h>
|
||||||
#include <Kernel/Net/RTL8139NetworkAdapter.h>
|
#include <Kernel/Net/RTL8139NetworkAdapter.h>
|
||||||
#include <Kernel/PCI.h>
|
#include <Kernel/PCI.h>
|
||||||
|
@ -244,6 +245,7 @@ extern "C" [[noreturn]] void init()
|
||||||
new BXVGADevice;
|
new BXVGADevice;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LoopbackAdapter::the();
|
||||||
auto e1000 = E1000NetworkAdapter::autodetect();
|
auto e1000 = E1000NetworkAdapter::autodetect();
|
||||||
auto rtl8139 = RTL8139NetworkAdapter::autodetect();
|
auto rtl8139 = RTL8139NetworkAdapter::autodetect();
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ elif [ "$1" = "qgrub" ]; then
|
||||||
-device VGA,vgamem_mb=64 \
|
-device VGA,vgamem_mb=64 \
|
||||||
-debugcon stdio \
|
-debugcon stdio \
|
||||||
-object filter-dump,id=hue,netdev=breh,file=e1000.pcap \
|
-object filter-dump,id=hue,netdev=breh,file=e1000.pcap \
|
||||||
-netdev user,id=breh,hostfwd=tcp:127.0.0.1:8888-192.168.5.2:8888 \
|
-netdev user,id=breh,hostfwd=tcp:127.0.0.1:8888-10.0.2.15:8888 \
|
||||||
-device e1000,netdev=breh \
|
-device e1000,netdev=breh \
|
||||||
-hda _disk_image \
|
-hda _disk_image \
|
||||||
-soundhw pcspk
|
-soundhw pcspk
|
||||||
|
@ -57,7 +57,7 @@ else
|
||||||
-device VGA,vgamem_mb=64 \
|
-device VGA,vgamem_mb=64 \
|
||||||
-debugcon stdio \
|
-debugcon stdio \
|
||||||
-object filter-dump,id=hue,netdev=breh,file=e1000.pcap \
|
-object filter-dump,id=hue,netdev=breh,file=e1000.pcap \
|
||||||
-netdev user,id=breh,hostfwd=tcp:127.0.0.1:8888-192.168.5.2:8888 \
|
-netdev user,id=breh,hostfwd=tcp:127.0.0.1:8888-10.0.2.15:8888 \
|
||||||
-device e1000,netdev=breh \
|
-device e1000,netdev=breh \
|
||||||
-kernel kernel \
|
-kernel kernel \
|
||||||
-append "${SERENITY_KERNEL_CMDLINE}" \
|
-append "${SERENITY_KERNEL_CMDLINE}" \
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue