Commit graph

93 commits

Author SHA1 Message Date
Isaac 3d2fcf4244 Kernel/net: Add tracking of dropped packets per adapter 2024-05-09 12:02:26 +02:00
Liav A 3f63be949a Kernel/Net: Don't allocate memory for adapters' names
Instead, use a FixedStringBuffer to store a string with up to 16 chars.
2023-08-12 11:48:48 -06:00
Liav A 336fb4f313 Kernel: Move InterruptDisabler to the Interrupts subdirectory 2023-06-04 21:32:34 +02:00
Liav A 7c0540a229 Everywhere: Move global Kernel pattern code to Kernel/Library directory
This has KString, KBuffer, DoubleBuffer, KBufferBuilder, IOWindow,
UserOrKernelBuffer and ScopedCritical classes being moved to the
Kernel/Library subdirectory.

Also, move the panic and assertions handling code to that directory.
2023-06-04 21:32:34 +02:00
Liav A 1b04726c85 Kernel: Move all tasks-related code to the Tasks subdirectory 2023-06-04 21:32:34 +02:00
kleines Filmröllchen 939600d2d4 Kernel: Use UnixDateTime wherever applicable
"Wherever applicable" = most places, actually :^), especially for
networking and filesystem timestamps.

This includes changes to unzip, which uses DOSPackedTime, since that is
changed for the FAT file systems.
2023-05-24 23:18:07 +02:00
kleines Filmröllchen 213025f210 AK: Rename Time to Duration
That's what this class really is; in fact that's what the first line of
the comment says it is.

This commit does not rename the main files, since those will contain
other time-related classes in a little bit.
2023-05-24 23:18:07 +02:00
Liav A 7c1f645e27 Kernel/Net: Iron out the locking mechanism across the subsystem
There is a big mix of LockRefPtrs all over the Networking subsystem, as
well as lots of room for improvements with our locking patterns, which
this commit will not pursue, but will give a good start for such work.

To deal with this situation, we change the following things:
- Creating instances of NetworkAdapter should always yield a non-locking
  NonnullRefPtr. Acquiring an instance from the NetworkingManagement
  should give a simple RefPtr,as giving LockRefPtr does not really
  protect from concurrency problems in such case.
- Since NetworkingManagement works with normal RefPtrs we should
  protect all instances of RefPtr<NetworkAdapter> with SpinlockProtected
  to ensure references are gone unexpectedly.
- Protect the so_error class member with a proper spinlock. This happens
  to be important because the clear_so_error() method lacked any proper
  locking measures. It also helps preventing a possible TOCTOU when we
  might do a more fine-grained locking in the Socket code, so this could
  be definitely a start for this.
- Change unnecessary LockRefPtr<PacketWithTimestamp> in the structure
  of OutgoingPacket to a simple RefPtr<PacketWithTimestamp> as the whole
  list should be MutexProtected.
2023-04-14 19:27:56 +02:00
Iman Seyed 85feab4095 Kernel: Pass ipv4_packet_size to ipv4.set_length()
Instead of `sizeof(IPv4Packet) + payload_size` expression,
pass `ipv4_packet_size` to `ipv4.set_length()`
2023-02-05 22:14:14 +00:00
Timon Kruiper 9827c11d8b Kernel: Move InterruptDisabler out of Arch directory
The code in this file is not architecture specific, so it can be moved
to the base Kernel directory.
2022-10-17 20:11:31 +02:00
Andreas Kling 11eee67b85 Kernel: Make self-contained locking smart pointers their own classes
Until now, our kernel has reimplemented a number of AK classes to
provide automatic internal locking:

- RefPtr
- NonnullRefPtr
- WeakPtr
- Weakable

This patch renames the Kernel classes so that they can coexist with
the original AK classes:

- RefPtr => LockRefPtr
- NonnullRefPtr => NonnullLockRefPtr
- WeakPtr => LockWeakPtr
- Weakable => LockWeakable

The goal here is to eventually get rid of the Lock* classes in favor of
using external locking.
2022-08-20 17:20:43 +02:00
Tim Schumacher df4ba7b430 Kernel: Put too small unused network packets back into the list 2022-08-19 14:51:58 +02:00
Tim Schumacher 9e7faff181 Kernel: Protect the list of unused network packets with a Spinlock 2022-08-19 14:51:58 +02:00
Tim Schumacher 3b3af58cf6 Kernel: Annotate all KBuffer and DoubleBuffer with a custom name 2022-07-12 00:55:31 +01:00
Timon Kruiper a4534678f9 Kernel: Implement InterruptDisabler using generic Processor functions
Now that the code does not use architectural specific code, it is moved
to the generic Arch directory and the paths are modified accordingly.
2022-06-02 13:14:12 +01:00
Maciej 66ab4d61a4 Kernel: Stop exposing gateway field
It doesn't make sense after introduction of routing table which allows
having multiple gateways for every interface, and isn't used by any of
the userspace programs now.
2022-05-01 13:34:27 +02:00
Idan Horowitz 086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Lenny Maiorani 190cf1507b Kernel: Use default constructors/destructors
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cother-other-default-operation-rules

"The compiler is more likely to get the default semantics right and
you cannot implement these functions better than the compiler."
2022-03-17 00:51:36 -07:00
Sam Atkins 45cf40653a Everywhere: Convert ByteBuffer factory methods from Optional -> ErrorOr
Apologies for the enormous commit, but I don't see a way to split this
up nicely. In the vast majority of cases it's a simple change. A few
extra places can use TRY instead of manual error checking though. :^)
2022-01-24 22:36:09 +01:00
Daniel Bertalan 8e2efe78f7 Kernel: Tighten String-related includes 2021-12-30 14:16:03 +01:00
Idan Horowitz adc9939a7b Kernel+LibC: Add support for the IPv4 TOS field via the IP_TOS sockopt 2021-10-28 11:24:36 +02:00
Brian Gianforcaro 2770433d30 Kernel: Convert network adapter names to Kernel::KString
Another step of incremental progress of removing `AK::String` from the
kernel, to harden against OOM.
2021-10-01 17:22:17 +00:00
Andreas Kling 899cee8185 Kernel: Make KBuffer::try_create_with_size() return KResultOr
This allows us to use TRY() in a lot of new places.
2021-09-07 15:15:08 +02:00
Ali Mohammad Pur 97e97bccab Everywhere: Make ByteBuffer::{create_*,copy}() OOM-safe 2021-09-06 01:53:26 +02:00
Andreas Kling 2cd8b21974 Kernel: Add convenience values to the Memory::Region::Access enum
Instead of `Memory::Region::Access::Read | Memory::Region::AccessWrite`
you can now say `Memory::Region::Access::ReadWrite`.
2021-08-06 22:25:00 +02:00
Andreas Kling 93d98d4976 Kernel: Move Kernel/Memory/ code into Kernel::Memory namespace 2021-08-06 14:05:58 +02:00
Brian Gianforcaro 720a686a76 Kernel: Handle OOM when allocating Packet KBuffers 2021-08-03 18:54:23 +02:00
Brian Gianforcaro 5743561d32 Kernel: Remove unused header includes in Net subtree 2021-07-11 21:37:38 +02:00
Daniel Bertalan f820917a76 Everywhere: Use nothrow new with adopt_{ref,own}_if_nonnull
This commit converts naked `new`s to `AK::try_make` and `AK::try_create`
wherever possible. If the called constructor is private, this can not be
done, so we instead now use the standard-defined and compiler-agnostic
`new (nothrow)`.
2021-06-24 17:35:49 +04:30
Hendiadyoin1 7ca3d413f7 Kernel: Pull apart CPU.h
This does not add any functional changes
2021-06-24 00:38:23 +02:00
Idan Horowitz 70fc0a528c Kernel: Increase m_bytes_out only once during transmission
We were accidentally increasing m_bytes_out by the packet size and then
immediately calling send_packet(), which did the same thing as well.
2021-06-11 00:56:32 +02:00
Liav A 1c94b5e8eb Kernel: Introduce the NetworkingManagement singleton
Instead of initializing network adapters in init.cpp, let's move that
logic into a separate class to handle this.
Also, it seems like a good idea to shift responsiblity on enumeration
of network adapters after the boot process, so this singleton will take
care of finding the appropriate network adapter when asked to with an
IPv4 address or interface name.

With this change being merged, we simplify the creation logic of
NetworkAdapter derived classes, so we enumerate the PCI bus only once,
searching for driver candidates when doing so, and we let each driver
to test if it is resposible for the specified PCI device.
2021-06-09 22:44:09 +04:30
Gunnar Beutner a42383523a Kernel: Update the TX byte counter when sending network packets
This previously worked but was broken by b436dd1.
2021-06-05 00:36:55 +04:30
Gunnar Beutner 60298121d8 Kernel: Make sure we increment the TX counter
This was broken by b436dd1.
2021-06-04 19:06:47 +02:00
Gunnar Beutner b436dd138b Kernel: Avoid allocations when sending IP packets
Previously we'd allocate buffers when sending packets. This patch
avoids these allocations by using the NetworkAdapter's packet queue.

At the same time this also avoids copying partially constructed
packets in order to prepend Ethernet and/or IPv4 headers. It also
properly truncates UDP and raw IP packets.
2021-05-26 23:09:28 +02:00
Gunnar Beutner f8310b7796 Kernel: Move packet allocation into helper methods 2021-05-26 23:09:28 +02:00
Liav A 8d0280ca09 Kernel/Net: Make interfaces to have persistent names
There's no good reason to distinguish between network interfaces based
on their model. It's probably a good idea to try keep the names more
persistent so scripts written for a specific network interface will be
useable after hotplug event (or after rebooting with new hardware
setup).
2021-05-22 11:19:50 +01:00
Gunnar Beutner eb1cecc03d Kernel: Make sure network adapters have unique names
Previously we'd just slap 0 onto the adapter's basename. This ensures
we actually end up with unique names.
2021-05-21 21:55:52 +02:00
Gunnar Beutner 093818de62 Kernel: Avoid allocations when receiving network packets
This avoids two allocations when receiving network packets. One for
inserting a PacketWithTimestamp into m_packet_queue and another one
when inserting buffers into the list of unused packet buffers.

With this fixed the only allocations in NetworkTask happen when
initially allocating the PacketWithTimestamp structs and when switching
contexts.
2021-05-18 16:06:27 +02:00
Gunnar Beutner 006f11f23d Kernel: Avoid allocations when handling network packets 2021-05-16 17:49:42 +02:00
Gunnar Beutner 1bb20a255f Kernel: Avoid unnecessary allocations in NetworkAdapter::for_each
This avoids allocations for initializing the Function<T>
for the NetworkAdapter::for_each callback argument.

Applying this patch decreases CPU utilization for NetworkTask
from 40% to 28% when receiving TCP packets at a rate of 100Mbit/s.
2021-05-13 08:27:42 +02:00
Gunnar Beutner 76deda802d Kernel: Avoid allocating and then freeing packet buffers
We already have another limit for the total number of packet buffers
allowed (max_packet_buffers). This second limit caused us to
repeatedly allocate and then free buffers.
2021-05-13 08:27:42 +02:00
Gunnar Beutner 2b6aa571d1 Kernel: Outbound packets should use the source address from the socket
Previously we'd use the adapter's address as the source address
when sending packets. Instead we should use the socket's bound local
address.
2021-05-12 16:31:29 +02:00
Gunnar Beutner 532db9f768 Kernel: Treat 0.0.0.0 as a loopback address
This matches what other operating systems like Linux do:

$ ip route get 0.0.0.0
local 0.0.0.0 dev lo src 127.0.0.1 uid 1000
    cache <local>

$ ssh 0.0.0.0
gunnar@0.0.0.0's password:

$ ss -na | grep :22 | grep ESTAB
tcp   ESTAB      0      0   127.0.0.1:43118   127.0.0.1:22
tcp   ESTAB      0      0   127.0.0.1:22      127.0.0.1:43118
2021-05-12 13:47:07 +02:00
Gunnar Beutner f999d5a91a Kernel: Limit the number of in-flight packet buffers
This fixes an OOM when hitting the VM with lots of UDP packets.

fixes #6907
2021-05-07 16:00:07 +02:00
Gunnar Beutner a85d111cd1 Kernel: Respond to packets sent to the directed broadcast address
The last IP address in an IPv4 subnet is considered the directed
broadcast address, e.g. for 192.168.3.0/24 the directed broadcast
address is 192.168.3.255. We need to consider this address as
belonging to the interface.

Here's an example with this fix applied, SerenityOS has 192.168.3.190:

[gunnar@nyx ~]$ ping -b 192.168.3.255
WARNING: pinging broadcast address
PING 192.168.3.255 (192.168.3.255) 56(84) bytes of data.
64 bytes from 192.168.3.175: icmp_seq=1 ttl=64 time=0.950 ms
64 bytes from 192.168.3.188: icmp_seq=1 ttl=64 time=2.33 ms
64 bytes from 192.168.3.46: icmp_seq=1 ttl=64 time=2.77 ms
64 bytes from 192.168.3.41: icmp_seq=1 ttl=64 time=4.15 ms
64 bytes from 192.168.3.190: icmp_seq=1 ttl=64 time=29.4 ms
64 bytes from 192.168.3.42: icmp_seq=1 ttl=64 time=30.8 ms
64 bytes from 192.168.3.55: icmp_seq=1 ttl=64 time=31.0 ms
64 bytes from 192.168.3.30: icmp_seq=1 ttl=64 time=33.2 ms
64 bytes from 192.168.3.31: icmp_seq=1 ttl=64 time=33.2 ms
64 bytes from 192.168.3.173: icmp_seq=1 ttl=64 time=41.7 ms
64 bytes from 192.168.3.43: icmp_seq=1 ttl=64 time=47.7 ms
^C
--- 192.168.3.255 ping statistics ---
1 packets transmitted, 1 received, +10 duplicates, 0% packet loss,
time 0ms, rtt min/avg/max/mdev = 0.950/23.376/47.676/16.539 ms
[gunnar@nyx ~]$
2021-04-26 18:39:59 +02:00
Brian Gianforcaro 8d6e9fad40 Kernel: Remove the now defunct LOCKER(..) macro. 2021-04-25 09:38:27 +02:00
Brian Gianforcaro 1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00
Ben Wiederhake 5c15ca7b84 Kernel: Make sockets use AK::Time 2021-03-02 08:36:08 +01:00
Ben Wiederhake 336303bda4 Kernel: Make kgettimeofday use AK::Time 2021-03-02 08:36:08 +01:00