Kernel: Make sockets use AK::Time

This commit is contained in:
Ben Wiederhake 2021-02-28 02:48:45 +01:00 committed by Andreas Kling
parent 719cb93a1a
commit 5c15ca7b84
Notes: sideshowbarker 2024-07-18 21:47:24 +09:00
13 changed files with 54 additions and 53 deletions

View File

@ -269,7 +269,7 @@ KResultOr<size_t> IPv4Socket::receive_byte_buffered(FileDescription& description
return nreceived;
}
KResultOr<size_t> IPv4Socket::receive_packet_buffered(FileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*> addr, Userspace<socklen_t*> addr_length, timeval& packet_timestamp)
KResultOr<size_t> IPv4Socket::receive_packet_buffered(FileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*> addr, Userspace<socklen_t*> addr_length, Time& packet_timestamp)
{
Locker locker(lock());
ReceivedPacket packet;
@ -352,7 +352,7 @@ KResultOr<size_t> IPv4Socket::receive_packet_buffered(FileDescription& descripti
return protocol_receive(ReadonlyBytes { packet.data.value().data(), packet.data.value().size() }, buffer, buffer_length, flags);
}
KResultOr<size_t> IPv4Socket::recvfrom(FileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*> user_addr, Userspace<socklen_t*> user_addr_length, timeval& packet_timestamp)
KResultOr<size_t> IPv4Socket::recvfrom(FileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*> user_addr, Userspace<socklen_t*> user_addr_length, Time& packet_timestamp)
{
if (user_addr_length) {
socklen_t addr_length;
@ -377,7 +377,7 @@ KResultOr<size_t> IPv4Socket::recvfrom(FileDescription& description, UserOrKerne
return nreceived;
}
bool IPv4Socket::did_receive(const IPv4Address& source_address, u16 source_port, KBuffer&& packet, const timeval& packet_timestamp)
bool IPv4Socket::did_receive(const IPv4Address& source_address, u16 source_port, KBuffer&& packet, const Time& packet_timestamp)
{
LOCKER(lock());

View File

@ -57,13 +57,13 @@ public:
virtual bool can_read(const FileDescription&, size_t) const override;
virtual bool can_write(const FileDescription&, size_t) const override;
virtual KResultOr<size_t> sendto(FileDescription&, const UserOrKernelBuffer&, size_t, int, Userspace<const sockaddr*>, socklen_t) override;
virtual KResultOr<size_t> recvfrom(FileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, timeval&) override;
virtual KResultOr<size_t> recvfrom(FileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, Time&) override;
virtual KResult setsockopt(int level, int option, Userspace<const void*>, socklen_t) override;
virtual KResult getsockopt(FileDescription&, int level, int option, Userspace<void*>, Userspace<socklen_t*>) override;
virtual int ioctl(FileDescription&, unsigned request, FlatPtr arg) override;
bool did_receive(const IPv4Address& peer_address, u16 peer_port, KBuffer&&, const timeval&);
bool did_receive(const IPv4Address& peer_address, u16 peer_port, KBuffer&&, const Time&);
const IPv4Address& local_address() const { return m_local_address; }
u16 local_port() const { return m_local_port; }
@ -109,7 +109,7 @@ private:
virtual bool is_ipv4() const override { return true; }
KResultOr<size_t> receive_byte_buffered(FileDescription&, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>);
KResultOr<size_t> receive_packet_buffered(FileDescription&, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, timeval&);
KResultOr<size_t> receive_packet_buffered(FileDescription&, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, Time&);
void set_can_read(bool);
@ -119,7 +119,7 @@ private:
struct ReceivedPacket {
IPv4Address peer_address;
u16 peer_port;
timeval timestamp;
Time timestamp;
Optional<KBuffer> data;
};

View File

@ -307,7 +307,7 @@ DoubleBuffer* LocalSocket::send_buffer_for(FileDescription& description)
return nullptr;
}
KResultOr<size_t> LocalSocket::recvfrom(FileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_size, int, Userspace<sockaddr*>, Userspace<socklen_t*>, timeval&)
KResultOr<size_t> LocalSocket::recvfrom(FileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_size, int, Userspace<sockaddr*>, Userspace<socklen_t*>, Time&)
{
auto* socket_buffer = receive_buffer_for(description);
if (!socket_buffer)

View File

@ -61,7 +61,7 @@ public:
virtual bool can_read(const FileDescription&, size_t) const override;
virtual bool can_write(const FileDescription&, size_t) const override;
virtual KResultOr<size_t> sendto(FileDescription&, const UserOrKernelBuffer&, size_t, int, Userspace<const sockaddr*>, socklen_t) override;
virtual KResultOr<size_t> recvfrom(FileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, timeval&) override;
virtual KResultOr<size_t> recvfrom(FileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, Time&) override;
virtual KResult getsockopt(FileDescription&, int level, int option, Userspace<void*>, Userspace<socklen_t*>) override;
virtual KResult chown(FileDescription&, uid_t, gid_t) override;
virtual KResult chmod(FileDescription&, mode_t) override;

View File

@ -193,14 +193,13 @@ void NetworkAdapter::did_receive(ReadonlyBytes payload)
}
}
// FIXME: Should use AK::Time internally
m_packet_queue.append({ buffer.value(), kgettimeofday().to_timeval() });
m_packet_queue.append({ buffer.value(), kgettimeofday() });
if (on_receive)
on_receive();
}
size_t NetworkAdapter::dequeue_packet(u8* buffer, size_t buffer_size, timeval& packet_timestamp)
size_t NetworkAdapter::dequeue_packet(u8* buffer, size_t buffer_size, Time& packet_timestamp)
{
InterruptDisabler disabler;
if (m_packet_queue.is_empty())

View File

@ -67,7 +67,7 @@ public:
KResult send_ipv4(const MACAddress&, const IPv4Address&, IPv4Protocol, const UserOrKernelBuffer& payload, size_t payload_size, u8 ttl);
KResult send_ipv4_fragmented(const MACAddress&, const IPv4Address&, IPv4Protocol, const UserOrKernelBuffer& payload, size_t payload_size, u8 ttl);
size_t dequeue_packet(u8* buffer, size_t buffer_size, timeval& packet_timestamp);
size_t dequeue_packet(u8* buffer, size_t buffer_size, Time& packet_timestamp);
bool has_queued_packets() const { return !m_packet_queue.is_empty(); }
@ -96,7 +96,7 @@ private:
struct PacketWithTimestamp {
KBuffer packet;
timeval timestamp;
Time timestamp;
};
SinglyLinkedList<PacketWithTimestamp> m_packet_queue;

View File

@ -44,10 +44,10 @@
namespace Kernel {
static void handle_arp(const EthernetFrameHeader&, size_t frame_size);
static void handle_ipv4(const EthernetFrameHeader&, size_t frame_size, const timeval& packet_timestamp);
static void handle_icmp(const EthernetFrameHeader&, const IPv4Packet&, const timeval& packet_timestamp);
static void handle_udp(const IPv4Packet&, const timeval& packet_timestamp);
static void handle_tcp(const IPv4Packet&, const timeval& packet_timestamp);
static void handle_ipv4(const EthernetFrameHeader&, size_t frame_size, const Time& packet_timestamp);
static void handle_icmp(const EthernetFrameHeader&, const IPv4Packet&, const Time& packet_timestamp);
static void handle_udp(const IPv4Packet&, const Time& packet_timestamp);
static void handle_tcp(const IPv4Packet&, const Time& packet_timestamp);
[[noreturn]] static void NetworkTask_main(void*);
@ -81,7 +81,7 @@ void NetworkTask_main(void*)
};
});
auto dequeue_packet = [&pending_packets](u8* buffer, size_t buffer_size, timeval& packet_timestamp) -> size_t {
auto dequeue_packet = [&pending_packets](u8* buffer, size_t buffer_size, Time& packet_timestamp) -> size_t {
if (pending_packets == 0)
return 0;
size_t packet_size = 0;
@ -100,7 +100,7 @@ void NetworkTask_main(void*)
size_t buffer_size = 64 * KiB;
auto buffer_region = MM.allocate_kernel_region(buffer_size, "Kernel Packet Buffer", Region::Access::Read | Region::Access::Write);
auto buffer = (u8*)buffer_region->vaddr().get();
timeval packet_timestamp;
Time packet_timestamp;
klog() << "NetworkTask: Enter main loop.";
for (;;) {
@ -205,7 +205,7 @@ void handle_arp(const EthernetFrameHeader& eth, size_t frame_size)
}
}
void handle_ipv4(const EthernetFrameHeader& eth, size_t frame_size, const timeval& packet_timestamp)
void handle_ipv4(const EthernetFrameHeader& eth, size_t frame_size, const Time& packet_timestamp)
{
constexpr size_t minimum_ipv4_frame_size = sizeof(EthernetFrameHeader) + sizeof(IPv4Packet);
if (frame_size < minimum_ipv4_frame_size) {
@ -242,7 +242,7 @@ void handle_ipv4(const EthernetFrameHeader& eth, size_t frame_size, const timeva
}
}
void handle_icmp(const EthernetFrameHeader& eth, const IPv4Packet& ipv4_packet, const timeval& packet_timestamp)
void handle_icmp(const EthernetFrameHeader& eth, const IPv4Packet& ipv4_packet, const Time& packet_timestamp)
{
auto& icmp_header = *static_cast<const ICMPHeader*>(ipv4_packet.payload());
#if ICMP_DEBUG
@ -290,7 +290,7 @@ void handle_icmp(const EthernetFrameHeader& eth, const IPv4Packet& ipv4_packet,
}
}
void handle_udp(const IPv4Packet& ipv4_packet, const timeval& packet_timestamp)
void handle_udp(const IPv4Packet& ipv4_packet, const Time& packet_timestamp)
{
if (ipv4_packet.payload_size() < sizeof(UDPPacket)) {
klog() << "handle_udp: Packet too small (" << ipv4_packet.payload_size() << ", need " << sizeof(UDPPacket) << ")";
@ -319,7 +319,7 @@ void handle_udp(const IPv4Packet& ipv4_packet, const timeval& packet_timestamp)
socket->did_receive(ipv4_packet.source(), udp_packet.source_port(), KBuffer::copy(&ipv4_packet, sizeof(IPv4Packet) + ipv4_packet.payload_size()), packet_timestamp);
}
void handle_tcp(const IPv4Packet& ipv4_packet, const timeval& packet_timestamp)
void handle_tcp(const IPv4Packet& ipv4_packet, const Time& packet_timestamp)
{
if (ipv4_packet.payload_size() < sizeof(TCPPacket)) {
klog() << "handle_tcp: IPv4 payload is too small to be a TCP packet (" << ipv4_packet.payload_size() << ", need " << sizeof(TCPPacket) << ")";

View File

@ -110,8 +110,7 @@ KResult Socket::setsockopt(int level, int option, Userspace<const void*> user_va
auto timeout = copy_time_from_user(static_ptr_cast<const timeval*>(user_value));
if (!timeout.has_value())
return EFAULT;
// FIXME: Should use AK::Time internally
m_send_timeout = timeout->to_timeval();
m_send_timeout = timeout.value();
}
return KSuccess;
case SO_RCVTIMEO:
@ -121,8 +120,7 @@ KResult Socket::setsockopt(int level, int option, Userspace<const void*> user_va
auto timeout = copy_time_from_user(static_ptr_cast<const timeval*>(user_value));
if (!timeout.has_value())
return EFAULT;
// FIXME: Should use AK::Time internally
m_receive_timeout = timeout->to_timeval();
m_receive_timeout = timeout.value();
}
return KSuccess;
case SO_BINDTODEVICE: {
@ -178,8 +176,11 @@ KResult Socket::getsockopt(FileDescription&, int level, int option, Userspace<vo
case SO_SNDTIMEO:
if (size < sizeof(timeval))
return EINVAL;
if (!copy_to_user(static_ptr_cast<timeval*>(value), &m_send_timeout))
return EFAULT;
{
timeval tv = m_send_timeout.to_timeval();
if (!copy_to_user(static_ptr_cast<timeval*>(value), &tv))
return EFAULT;
}
size = sizeof(timeval);
if (!copy_to_user(value_size, &size))
return EFAULT;
@ -187,8 +188,11 @@ KResult Socket::getsockopt(FileDescription&, int level, int option, Userspace<vo
case SO_RCVTIMEO:
if (size < sizeof(timeval))
return EINVAL;
if (!copy_to_user(static_ptr_cast<timeval*>(value), &m_receive_timeout))
return EFAULT;
{
timeval tv = m_send_timeout.to_timeval();
if (!copy_to_user(static_ptr_cast<timeval*>(value), &tv))
return EFAULT;
}
size = sizeof(timeval);
if (!copy_to_user(value_size, &size))
return EFAULT;
@ -243,8 +247,8 @@ KResultOr<size_t> Socket::read(FileDescription& description, size_t, UserOrKerne
{
if (is_shut_down_for_reading())
return 0;
timeval tv;
return recvfrom(description, buffer, size, 0, {}, 0, tv);
Time t {};
return recvfrom(description, buffer, size, 0, {}, 0, t);
}
KResultOr<size_t> Socket::write(FileDescription& description, size_t, const UserOrKernelBuffer& data, size_t size)

View File

@ -30,6 +30,7 @@
#include <AK/NonnullRefPtrVector.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/Time.h>
#include <Kernel/FileSystem/File.h>
#include <Kernel/KResult.h>
#include <Kernel/Lock.h>
@ -106,7 +107,7 @@ public:
virtual bool is_local() const { return false; }
virtual bool is_ipv4() const { return false; }
virtual KResultOr<size_t> sendto(FileDescription&, const UserOrKernelBuffer&, size_t, int flags, Userspace<const sockaddr*>, socklen_t) = 0;
virtual KResultOr<size_t> recvfrom(FileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, timeval&) = 0;
virtual KResultOr<size_t> recvfrom(FileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, Time&) = 0;
virtual KResult setsockopt(int level, int option, Userspace<const void*>, socklen_t);
virtual KResult getsockopt(FileDescription&, int level, int option, Userspace<void*>, Userspace<socklen_t*>);
@ -127,11 +128,11 @@ public:
virtual KResult stat(::stat&) const override;
virtual String absolute_path(const FileDescription&) const override = 0;
bool has_receive_timeout() const { return m_receive_timeout.tv_sec || m_receive_timeout.tv_usec; }
const timeval& receive_timeout() const { return m_receive_timeout; }
bool has_receive_timeout() const { return m_receive_timeout != Time::zero(); }
const Time& receive_timeout() const { return m_receive_timeout; }
bool has_send_timeout() const { return m_send_timeout.tv_sec || m_send_timeout.tv_usec; }
const timeval& send_timeout() const { return m_send_timeout; }
bool has_send_timeout() const { return m_send_timeout != Time::zero(); }
const Time& send_timeout() const { return m_send_timeout; }
bool wants_timestamp() const { return m_timestamp; }
@ -170,8 +171,8 @@ private:
RefPtr<NetworkAdapter> m_bound_interface { nullptr };
timeval m_receive_timeout { 0, 0 };
timeval m_send_timeout { 0, 0 };
Time m_receive_timeout {};
Time m_send_timeout {};
int m_timestamp { 0 };
NonnullRefPtrVector<Socket> m_pending;

View File

@ -238,14 +238,13 @@ void TCPSocket::send_outgoing_packets()
auto routing_decision = route_to(peer_address(), local_address(), bound_interface());
VERIFY(!routing_decision.is_zero());
// FIXME: Should use AK::Time internally
auto now = kgettimeofday().to_timeval();
auto now = kgettimeofday();
LOCKER(m_not_acked_lock, Lock::Mode::Shared);
for (auto& packet : m_not_acked) {
timeval diff;
timeval_sub(packet.tx_time, now, diff);
if (diff.tv_sec == 0 && diff.tv_usec <= 500000)
// FIXME: This doesn't look correct to me. Bug?
auto diff = packet.tx_time - now;
if (diff <= Time::from_nanoseconds(500'000'000))
continue;
packet.tx_time = now;
packet.tx_counter++;

View File

@ -202,7 +202,7 @@ private:
u32 ack_number { 0 };
ByteBuffer buffer;
int tx_counter { 0 };
timeval tx_time { 0, 0 };
Time tx_time {};
};
Lock m_not_acked_lock { "TCPSocket unacked packets" };

View File

@ -252,7 +252,7 @@ KResultOr<ssize_t> Process::sys$recvmsg(int sockfd, Userspace<struct msghdr*> us
auto data_buffer = UserOrKernelBuffer::for_user_buffer((u8*)iovs[0].iov_base, iovs[0].iov_len);
if (!data_buffer.has_value())
return EFAULT;
timeval timestamp = { 0, 0 };
Time timestamp {};
auto result = socket.recvfrom(*description, data_buffer.value(), iovs[0].iov_len, flags, user_addr, user_addr_length, timestamp);
if (flags & MSG_DONTWAIT)
description->set_blocking(original_blocking);
@ -276,7 +276,7 @@ KResultOr<ssize_t> Process::sys$recvmsg(int sockfd, Userspace<struct msghdr*> us
if (msg.msg_controllen < control_length) {
msg_flags |= MSG_CTRUNC;
} else {
cmsg_timestamp = { { control_length, SOL_SOCKET, SCM_TIMESTAMP }, timestamp };
cmsg_timestamp = { { control_length, SOL_SOCKET, SCM_TIMESTAMP }, timestamp.to_timeval() };
if (!copy_to_user(msg.msg_control, &cmsg_timestamp, control_length))
return EFAULT;
}

View File

@ -285,8 +285,7 @@ auto Thread::WriteBlocker::override_timeout(const BlockTimeout& timeout) -> cons
if (description.is_socket()) {
auto& socket = *description.socket();
if (socket.has_send_timeout()) {
// FIXME: Should use AK::Time internally
Time send_timeout = Time::from_timeval(socket.send_timeout());
Time send_timeout = socket.send_timeout();
m_timeout = BlockTimeout(false, &send_timeout, timeout.start_time(), timeout.clock_id());
if (timeout.is_infinite() || (!m_timeout.is_infinite() && m_timeout.absolute_time() < timeout.absolute_time()))
return m_timeout;
@ -306,8 +305,7 @@ auto Thread::ReadBlocker::override_timeout(const BlockTimeout& timeout) -> const
if (description.is_socket()) {
auto& socket = *description.socket();
if (socket.has_receive_timeout()) {
// FIXME: Should use AK::Time internally
Time receive_timeout = Time::from_timeval(socket.receive_timeout());
Time receive_timeout = socket.receive_timeout();
m_timeout = BlockTimeout(false, &receive_timeout, timeout.start_time(), timeout.clock_id());
if (timeout.is_infinite() || (!m_timeout.is_infinite() && m_timeout.absolute_time() < timeout.absolute_time()))
return m_timeout;