Split net stack into eth/arp/ipv4/icmp, deferred handler responses, ping tests

This commit is contained in:
Ian Gulliver
2026-04-11 08:15:41 +09:00
parent 34efaeefd5
commit c35c1de76a
16 changed files with 522 additions and 330 deletions

28
firmware/include/arp.h Normal file
View File

@@ -0,0 +1,28 @@
#pragma once
#include <functional>
#include <span>
#include "eth.h"
#include "ipv4.h"
#include "span_writer.h"
namespace arp {
struct __attribute__((packed)) packet {
eth::header eth;
uint16_t htype;
uint16_t ptype;
uint8_t hlen;
uint8_t plen;
uint16_t oper;
eth::mac_addr sha;
ipv4::ip4_addr spa;
eth::mac_addr tha;
ipv4::ip4_addr tpa;
};
static_assert(sizeof(packet) == 42);
void handle(std::span<const uint8_t> frame, span_writer& tx,
eth::mac_addr our_mac, ipv4::ip4_addr our_ip,
std::function<void(std::span<const uint8_t>)> send_raw);
} // namespace arp

View File

@@ -1,10 +1,24 @@
#pragma once
#include <cstdint>
#include <functional>
#include <optional>
#include <span>
#include "wire.h"
using handler_fn = msgpack::result<size_t> (*)(uint32_t message_id, std::span<const uint8_t> payload, span_writer &out);
struct responder {
uint32_t message_id;
std::function<void(std::span<const uint8_t>)> send;
template <typename T>
void respond(const T& msg) const {
uint8_t buf[1024];
span_writer out(buf, sizeof(buf));
auto r = encode_response_into(out, message_id, msg);
if (r) send({buf, *r});
}
};
using handler_fn = void (*)(responder resp, std::span<const uint8_t> payload);
struct handler_entry {
int8_t type_id;
@@ -12,16 +26,19 @@ struct handler_entry {
};
template <typename Req, auto Fn>
msgpack::result<size_t> typed_handler(uint32_t message_id, std::span<const uint8_t> payload, span_writer &out) {
void typed_handler(responder resp, std::span<const uint8_t> payload) {
msgpack::parser p(payload.data(), static_cast<int>(payload.size()));
Req req;
auto tup = req.as_tuple();
auto r = msgpack::unpack(p, tup);
if (!r) {
return encode_response_into(out, message_id, DeviceError{1, "decode request ext_id=" +
resp.respond(DeviceError{1, "decode request ext_id=" +
std::to_string(Req::ext_id) + ": msgpack error " + std::to_string(static_cast<int>(r.error()))});
return;
}
return encode_response_into(out, message_id, Fn(req));
auto result = Fn(resp, req);
if (result)
resp.respond(*result);
}
void dispatch_init();

20
firmware/include/eth.h Normal file
View File

@@ -0,0 +1,20 @@
#pragma once
#include <array>
#include <cstdint>
namespace eth {
using mac_addr = std::array<uint8_t, 6>;
static constexpr mac_addr MAC_BROADCAST = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
static constexpr uint16_t ETH_ARP = __builtin_bswap16(0x0806);
static constexpr uint16_t ETH_IPV4 = __builtin_bswap16(0x0800);
struct __attribute__((packed)) header {
mac_addr dst;
mac_addr src;
uint16_t ethertype;
};
static_assert(sizeof(header) == 14);
} // namespace eth

View File

@@ -1,9 +1,11 @@
#pragma once
#include <optional>
#include <string_view>
#include "dispatch.h"
#include "wire.h"
extern std::string_view firmware_name;
ResponsePICOBOOT handle_picoboot(const RequestPICOBOOT&);
ResponseInfo handle_info(const RequestInfo&);
ResponseLog handle_log(const RequestLog&);
std::optional<ResponsePICOBOOT> handle_picoboot(const responder& resp, const RequestPICOBOOT&);
std::optional<ResponseInfo> handle_info(const responder& resp, const RequestInfo&);
std::optional<ResponseLog> handle_log(const responder& resp, const RequestLog&);

31
firmware/include/icmp.h Normal file
View File

@@ -0,0 +1,31 @@
#pragma once
#include <cstdint>
#include <functional>
#include <span>
#include "eth.h"
#include "ipv4.h"
#include "span_writer.h"
namespace icmp {
struct __attribute__((packed)) echo {
uint8_t type;
uint8_t code;
uint16_t checksum;
uint16_t id;
uint16_t seq;
};
static_assert(sizeof(echo) == 8);
void handle(std::span<const uint8_t> frame, span_writer& tx,
eth::mac_addr our_mac, ipv4::ip4_addr our_ip,
std::function<void(std::span<const uint8_t>)> send_raw);
size_t build_echo_request(std::span<uint8_t> buf,
eth::mac_addr src_mac, ipv4::ip4_addr src_ip,
eth::mac_addr dst_mac, ipv4::ip4_addr dst_ip,
uint16_t id, uint16_t seq);
bool parse_echo_reply(std::span<const uint8_t> frame, ipv4::ip4_addr& src_ip, uint16_t expected_id);
} // namespace icmp

49
firmware/include/ipv4.h Normal file
View File

@@ -0,0 +1,49 @@
#pragma once
#include <array>
#include <cstdint>
#include <functional>
#include <span>
#include "eth.h"
#include "span_writer.h"
namespace ipv4 {
using ip4_addr = std::array<uint8_t, 4>;
struct __attribute__((packed)) header {
eth::header eth;
uint8_t ver_ihl;
uint8_t dscp_ecn;
uint16_t total_len;
uint16_t identification;
uint16_t flags_frag;
uint8_t ttl;
uint8_t protocol;
uint16_t checksum;
ip4_addr src;
ip4_addr dst;
size_t ip_header_len() const { return (ver_ihl & 0x0F) * 4; }
size_t ip_total_len() const { return __builtin_bswap16(total_len); }
const uint8_t* ip_start() const { return reinterpret_cast<const uint8_t*>(&ver_ihl); }
uint8_t* ip_start() { return reinterpret_cast<uint8_t*>(&ver_ihl); }
};
static_assert(sizeof(header) == 34);
struct __attribute__((packed)) udp_header {
header ip;
uint16_t src_port;
uint16_t dst_port;
uint16_t length;
uint16_t checksum;
};
static_assert(sizeof(udp_header) == 42);
uint16_t checksum(const void* data, size_t len);
void handle(std::span<const uint8_t> frame, span_writer& tx,
eth::mac_addr our_mac, ip4_addr our_ip, ip4_addr subnet_broadcast,
std::function<void(std::span<const uint8_t>)> send_raw,
std::function<void(std::span<const uint8_t>, span_writer&)> handle_udp);
} // namespace ipv4

View File

@@ -1,19 +1,25 @@
#pragma once
#include <array>
#include <cstdint>
#include <expected>
#include <functional>
#include <span>
#include "eth.h"
#include "ipv4.h"
#include "span_writer.h"
#include "msgpack.h"
struct net_state {
std::array<uint8_t, 6> mac;
std::array<uint8_t, 4> ip;
eth::mac_addr mac;
ipv4::ip4_addr ip;
};
using net_handler = std::function<msgpack::result<size_t>(std::span<const uint8_t> payload, span_writer &out)>;
using net_handler = std::function<void(std::span<const uint8_t> payload,
std::function<void(std::span<const uint8_t>)> send)>;
using net_frame_callback = std::function<void(std::span<const uint8_t> frame)>;
bool net_init();
const net_state& net_get_state();
void net_set_handler(net_handler handler);
void net_add_frame_callback(net_frame_callback cb);
void net_poll(std::span<uint8_t> tx);
void net_send_raw(std::span<const uint8_t> data);

View File

@@ -1,5 +1,7 @@
#pragma once
#include <optional>
#include "dispatch.h"
#include "wire.h"
ResponseListTests handle_list_tests(const RequestListTests&);
ResponseTest handle_test(const RequestTest&);
std::optional<ResponseListTests> handle_list_tests(const responder& resp, const RequestListTests&);
std::optional<ResponseTest> handle_test(const responder& resp, const RequestTest&);