2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-03-10 15:25:33 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-03-11 12:43:45 +01:00
|
|
|
#include <AK/ByteBuffer.h>
|
2019-06-16 07:06:49 +02:00
|
|
|
#include <AK/Function.h>
|
2021-05-14 09:52:49 +02:00
|
|
|
#include <AK/IntrusiveList.h>
|
2020-04-04 15:48:28 +04:30
|
|
|
#include <AK/MACAddress.h>
|
2019-03-10 15:25:33 +01:00
|
|
|
#include <AK/Types.h>
|
2019-08-09 12:34:32 +10:00
|
|
|
#include <AK/WeakPtr.h>
|
2020-03-22 13:12:45 +13:00
|
|
|
#include <AK/Weakable.h>
|
2021-06-25 09:46:17 +03:00
|
|
|
#include <Kernel/Bus/PCI/Definitions.h>
|
2019-08-04 21:22:22 +02:00
|
|
|
#include <Kernel/KBuffer.h>
|
2019-04-02 19:54:38 +02:00
|
|
|
#include <Kernel/Net/ARP.h>
|
2021-05-26 05:35:05 +02:00
|
|
|
#include <Kernel/Net/EthernetFrameHeader.h>
|
2019-04-02 19:54:38 +02:00
|
|
|
#include <Kernel/Net/ICMP.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <Kernel/Net/IPv4.h>
|
2020-09-11 21:11:07 -06:00
|
|
|
#include <Kernel/UserOrKernelBuffer.h>
|
2019-03-20 17:09:46 +01:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-03-20 17:09:46 +01:00
|
|
|
class NetworkAdapter;
|
|
|
|
|
2021-05-15 22:45:22 +02:00
|
|
|
using NetworkByteBuffer = AK::Detail::ByteBuffer<1500>;
|
|
|
|
|
2021-05-26 04:21:19 +02:00
|
|
|
struct PacketWithTimestamp : public RefCounted<PacketWithTimestamp> {
|
2021-08-01 05:11:49 -07:00
|
|
|
PacketWithTimestamp(NonnullOwnPtr<KBuffer> buffer, Time timestamp)
|
2021-05-26 04:21:19 +02:00
|
|
|
: buffer(move(buffer))
|
|
|
|
, timestamp(timestamp)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-08-01 05:11:49 -07:00
|
|
|
ReadonlyBytes bytes() { return { buffer->data(), buffer->size() }; };
|
|
|
|
|
|
|
|
NonnullOwnPtr<KBuffer> buffer;
|
2021-05-26 04:21:19 +02:00
|
|
|
Time timestamp;
|
|
|
|
IntrusiveListNode<PacketWithTimestamp, RefPtr<PacketWithTimestamp>> packet_node;
|
|
|
|
};
|
|
|
|
|
2021-05-26 05:35:05 +02:00
|
|
|
class NetworkAdapter : public RefCounted<NetworkAdapter>
|
|
|
|
, public Weakable<NetworkAdapter> {
|
2019-03-10 15:25:33 +01:00
|
|
|
public:
|
2021-07-25 20:16:42 +02:00
|
|
|
static constexpr i32 LINKSPEED_INVALID = -1;
|
|
|
|
|
2019-03-10 15:25:33 +01:00
|
|
|
virtual ~NetworkAdapter();
|
|
|
|
|
2021-07-11 17:59:38 +02:00
|
|
|
virtual StringView class_name() const = 0;
|
2019-06-16 07:06:49 +02:00
|
|
|
|
|
|
|
const String& name() const { return m_name; }
|
2019-03-10 19:15:22 +01:00
|
|
|
MACAddress mac_address() { return m_mac_address; }
|
2019-03-11 23:21:38 +01:00
|
|
|
IPv4Address ipv4_address() const { return m_ipv4_address; }
|
2019-08-28 11:00:39 +10:00
|
|
|
IPv4Address ipv4_netmask() const { return m_ipv4_netmask; }
|
2021-04-26 15:38:26 +02:00
|
|
|
IPv4Address ipv4_broadcast() const { return IPv4Address { (m_ipv4_address.to_u32() & m_ipv4_netmask.to_u32()) | ~m_ipv4_netmask.to_u32() }; }
|
2019-08-28 11:00:39 +10:00
|
|
|
IPv4Address ipv4_gateway() const { return m_ipv4_gateway; }
|
2019-08-22 00:52:50 +10:00
|
|
|
virtual bool link_up() { return false; }
|
2021-07-25 20:16:42 +02:00
|
|
|
virtual i32 link_speed()
|
|
|
|
{
|
|
|
|
// In Mbit/sec.
|
|
|
|
return LINKSPEED_INVALID;
|
|
|
|
}
|
|
|
|
virtual bool link_full_duplex() { return false; }
|
2019-03-11 23:21:38 +01:00
|
|
|
|
|
|
|
void set_ipv4_address(const IPv4Address&);
|
2019-08-28 11:00:39 +10:00
|
|
|
void set_ipv4_netmask(const IPv4Address&);
|
|
|
|
void set_ipv4_gateway(const IPv4Address&);
|
2019-03-10 15:25:33 +01:00
|
|
|
|
2019-03-10 23:40:09 +01:00
|
|
|
void send(const MACAddress&, const ARPPacket&);
|
2021-05-26 05:35:05 +02:00
|
|
|
void fill_in_ipv4_header(PacketWithTimestamp&, IPv4Address const&, MACAddress const&, IPv4Address const&, IPv4Protocol, size_t, u8);
|
2019-03-10 20:59:23 +01:00
|
|
|
|
2021-02-28 02:48:45 +01:00
|
|
|
size_t dequeue_packet(u8* buffer, size_t buffer_size, Time& packet_timestamp);
|
2019-03-11 12:43:45 +01:00
|
|
|
|
2019-03-20 17:09:46 +01:00
|
|
|
bool has_queued_packets() const { return !m_packet_queue.is_empty(); }
|
|
|
|
|
2019-11-28 07:12:05 +01:00
|
|
|
u32 mtu() const { return m_mtu; }
|
|
|
|
void set_mtu(u32 mtu) { m_mtu = mtu; }
|
|
|
|
|
2019-08-08 12:32:35 +10:00
|
|
|
u32 packets_in() const { return m_packets_in; }
|
|
|
|
u32 bytes_in() const { return m_bytes_in; }
|
|
|
|
u32 packets_out() const { return m_packets_out; }
|
|
|
|
u32 bytes_out() const { return m_bytes_out; }
|
|
|
|
|
2021-05-26 04:21:19 +02:00
|
|
|
RefPtr<PacketWithTimestamp> acquire_packet_buffer(size_t);
|
|
|
|
void release_packet_buffer(PacketWithTimestamp&);
|
|
|
|
|
2021-05-26 05:35:05 +02:00
|
|
|
constexpr size_t layer3_payload_offset() const { return sizeof(EthernetFrameHeader); }
|
|
|
|
constexpr size_t ipv4_payload_offset() const { return layer3_payload_offset() + sizeof(IPv4Packet); }
|
|
|
|
|
2019-08-28 22:14:38 +10:00
|
|
|
Function<void()> on_receive;
|
2019-08-28 11:21:22 +10:00
|
|
|
|
2021-06-04 17:58:55 +02:00
|
|
|
void send_packet(ReadonlyBytes);
|
|
|
|
|
2019-03-10 15:25:33 +01:00
|
|
|
protected:
|
|
|
|
NetworkAdapter();
|
2021-05-22 01:35:46 +03:00
|
|
|
void set_interface_name(const PCI::Address&);
|
2019-03-10 19:15:22 +01:00
|
|
|
void set_mac_address(const MACAddress& mac_address) { m_mac_address = mac_address; }
|
2020-07-28 20:19:22 +02:00
|
|
|
void did_receive(ReadonlyBytes);
|
2021-06-04 17:58:55 +02:00
|
|
|
virtual void send_raw(ReadonlyBytes) = 0;
|
2019-03-10 15:25:33 +01:00
|
|
|
|
2021-05-22 01:35:46 +03:00
|
|
|
void set_loopback_name();
|
|
|
|
|
2019-03-10 15:25:33 +01:00
|
|
|
private:
|
2019-03-10 19:15:22 +01:00
|
|
|
MACAddress m_mac_address;
|
2019-03-11 23:21:38 +01:00
|
|
|
IPv4Address m_ipv4_address;
|
2019-08-28 11:00:39 +10:00
|
|
|
IPv4Address m_ipv4_netmask;
|
|
|
|
IPv4Address m_ipv4_gateway;
|
2020-09-16 12:25:06 -04:00
|
|
|
|
2021-05-06 20:22:57 +02:00
|
|
|
// FIXME: Make this configurable
|
|
|
|
static constexpr size_t max_packet_buffers = 1024;
|
|
|
|
|
2021-05-14 09:52:49 +02:00
|
|
|
using PacketList = IntrusiveList<PacketWithTimestamp, RefPtr<PacketWithTimestamp>, &PacketWithTimestamp::packet_node>;
|
|
|
|
|
|
|
|
PacketList m_packet_queue;
|
2021-05-06 20:22:57 +02:00
|
|
|
size_t m_packet_queue_size { 0 };
|
2021-05-14 09:52:49 +02:00
|
|
|
PacketList m_unused_packets;
|
2019-06-16 07:06:49 +02:00
|
|
|
String m_name;
|
2019-08-08 12:32:35 +10:00
|
|
|
u32 m_packets_in { 0 };
|
|
|
|
u32 m_bytes_in { 0 };
|
|
|
|
u32 m_packets_out { 0 };
|
|
|
|
u32 m_bytes_out { 0 };
|
2019-11-28 07:12:05 +01:00
|
|
|
u32 m_mtu { 1500 };
|
2019-03-10 15:25:33 +01:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|