Ethertype handler registry: arp/ipv4 self-register via __attribute__((constructor)), drop threaded mac/ip/subnet params

This commit is contained in:
Ian Gulliver
2026-04-19 08:26:57 -07:00
parent 59829b569e
commit 7e493b7d70
6 changed files with 50 additions and 30 deletions

View File

@@ -19,7 +19,6 @@ struct __attribute__((packed)) header {
}; };
static_assert(sizeof(header) == 28); static_assert(sizeof(header) == 28);
void handle(std::span<const uint8_t> frame, span_writer& tx, void handle(std::span<const uint8_t> frame, span_writer& tx);
eth::mac_addr our_mac, ipv4::ip4_addr our_ip);
} // namespace arp } // namespace arp

View File

@@ -36,6 +36,8 @@ static_assert(sizeof(header) == 20);
uint16_t checksum(const void* data, size_t len); uint16_t checksum(const void* data, size_t len);
static constexpr ip4_addr SUBNET_BROADCAST = {169, 254, 255, 255};
template <typename Buf> template <typename Buf>
void prepend(Buf& buf, const eth::mac_addr& dst_mac, const eth::mac_addr& src_mac, void prepend(Buf& buf, const eth::mac_addr& dst_mac, const eth::mac_addr& src_mac,
ip4_addr src_ip, ip4_addr dst_ip, uint8_t protocol, ip4_addr src_ip, ip4_addr dst_ip, uint8_t protocol,
@@ -55,7 +57,6 @@ void prepend(Buf& buf, const eth::mac_addr& dst_mac, const eth::mac_addr& src_ma
eth::prepend(buf, dst_mac, src_mac, eth::ETH_IPV4); eth::prepend(buf, dst_mac, src_mac, eth::ETH_IPV4);
} }
void handle(std::span<const uint8_t> frame, span_writer& tx, void handle(std::span<const uint8_t> frame, span_writer& tx);
eth::mac_addr our_mac, ip4_addr our_ip, ip4_addr subnet_broadcast);
} // namespace ipv4 } // namespace ipv4

View File

@@ -16,6 +16,8 @@ using net_frame_callback = bool (*)(std::span<const uint8_t> frame);
using frame_cb_list = callback_list<net_frame_callback, 16>; using frame_cb_list = callback_list<net_frame_callback, 16>;
using frame_cb_handle = frame_cb_list::node*; using frame_cb_handle = frame_cb_list::node*;
using ethertype_handler = void (*)(std::span<const uint8_t> frame, span_writer& tx);
inline constexpr uint16_t PICOMAP_PORT_BE = __builtin_bswap16(28781); inline constexpr uint16_t PICOMAP_PORT_BE = __builtin_bswap16(28781);
bool net_init(); bool net_init();
@@ -24,3 +26,4 @@ frame_cb_handle net_add_frame_callback(net_frame_callback cb);
void net_remove_frame_callback(frame_cb_handle h); void net_remove_frame_callback(frame_cb_handle h);
void net_poll(std::span<uint8_t> tx); void net_poll(std::span<uint8_t> tx);
void net_send_raw(std::span<const uint8_t> data); void net_send_raw(std::span<const uint8_t> data);
void net_register_ethertype(uint16_t ethertype_be, ethertype_handler fn);

View File

@@ -10,8 +10,8 @@ static constexpr uint16_t ARP_PTYPE_IPV4 = __builtin_bswap16(0x0800);
static constexpr uint16_t ARP_OP_REQUEST = __builtin_bswap16(1); static constexpr uint16_t ARP_OP_REQUEST = __builtin_bswap16(1);
static constexpr uint16_t ARP_OP_REPLY = __builtin_bswap16(2); static constexpr uint16_t ARP_OP_REPLY = __builtin_bswap16(2);
void handle(std::span<const uint8_t> frame, span_writer& tx, void handle(std::span<const uint8_t> frame, span_writer& tx) {
eth::mac_addr our_mac, ipv4::ip4_addr our_ip) { const auto& ns = net_get_state();
parse_buffer pb(frame); parse_buffer pb(frame);
pb.consume<eth::header>(); pb.consume<eth::header>();
auto* arp_hdr = pb.consume<header>(); auto* arp_hdr = pb.consume<header>();
@@ -21,7 +21,7 @@ void handle(std::span<const uint8_t> frame, span_writer& tx,
if (arp_hdr->ptype != ARP_PTYPE_IPV4) return; if (arp_hdr->ptype != ARP_PTYPE_IPV4) return;
if (arp_hdr->hlen != 6 || arp_hdr->plen != 4) return; if (arp_hdr->hlen != 6 || arp_hdr->plen != 4) return;
if (arp_hdr->oper != ARP_OP_REQUEST) return; if (arp_hdr->oper != ARP_OP_REQUEST) return;
if (arp_hdr->tpa != our_ip) return; if (arp_hdr->tpa != ns.ip) return;
prepend_buffer<4096> buf; prepend_buffer<4096> buf;
auto* reply = buf.template prepend<header>(); auto* reply = buf.template prepend<header>();
@@ -30,13 +30,18 @@ void handle(std::span<const uint8_t> frame, span_writer& tx,
reply->hlen = 6; reply->hlen = 6;
reply->plen = 4; reply->plen = 4;
reply->oper = ARP_OP_REPLY; reply->oper = ARP_OP_REPLY;
reply->sha = our_mac; reply->sha = ns.mac;
reply->spa = our_ip; reply->spa = ns.ip;
reply->tha = arp_hdr->sha; reply->tha = arp_hdr->sha;
reply->tpa = arp_hdr->spa; reply->tpa = arp_hdr->spa;
eth::prepend(buf, arp_hdr->sha, our_mac, eth::ETH_ARP); eth::prepend(buf, arp_hdr->sha, ns.mac, eth::ETH_ARP);
net_send_raw(buf.span()); net_send_raw(buf.span());
} }
__attribute__((constructor))
static void register_ethertype() {
net_register_ethertype(eth::ETH_ARP, handle);
}
} // namespace arp } // namespace arp

View File

@@ -1,6 +1,7 @@
#include "ipv4.h" #include "ipv4.h"
#include "icmp.h" #include "icmp.h"
#include "igmp.h" #include "igmp.h"
#include "net.h"
#include "udp.h" #include "udp.h"
#include "parse_buffer.h" #include "parse_buffer.h"
@@ -20,12 +21,12 @@ uint16_t checksum(const void* data, size_t len) {
return __builtin_bswap16(~sum); return __builtin_bswap16(~sum);
} }
static bool ip_match(const ip4_addr& dst, const ip4_addr& our_ip, const ip4_addr& subnet_broadcast) { static bool ip_match(const ip4_addr& dst, const ip4_addr& our_ip) {
return dst == our_ip || dst == IP_BROADCAST_ALL || dst == subnet_broadcast || igmp::is_member(dst); return dst == our_ip || dst == IP_BROADCAST_ALL || dst == SUBNET_BROADCAST || igmp::is_member(dst);
} }
void handle(std::span<const uint8_t> frame, span_writer& tx, void handle(std::span<const uint8_t> frame, span_writer& tx) {
eth::mac_addr our_mac, ip4_addr our_ip, ip4_addr subnet_broadcast) { const auto& ns = net_get_state();
parse_buffer pb(frame); parse_buffer pb(frame);
pb.consume<eth::header>(); pb.consume<eth::header>();
auto* ip = pb.consume<header>(); auto* ip = pb.consume<header>();
@@ -37,19 +38,24 @@ void handle(std::span<const uint8_t> frame, span_writer& tx,
switch (ip->protocol) { switch (ip->protocol) {
case 1: case 1:
if (!ip_match(ip->dst, our_ip, subnet_broadcast)) if (!ip_match(ip->dst, ns.ip))
return; return;
icmp::handle(frame, tx, our_mac, our_ip); icmp::handle(frame, tx, ns.mac, ns.ip);
break; break;
case 2: case 2:
igmp::handle(frame, tx, our_mac, our_ip); igmp::handle(frame, tx, ns.mac, ns.ip);
break; break;
case 17: case 17:
if (!ip_match(ip->dst, our_ip, subnet_broadcast)) if (!ip_match(ip->dst, ns.ip))
return; return;
udp::handle(frame, tx); udp::handle(frame, tx);
break; break;
} }
} }
__attribute__((constructor))
static void register_ethertype() {
net_register_ethertype(eth::ETH_IPV4, handle);
}
} // namespace ipv4 } // namespace ipv4

View File

@@ -1,22 +1,30 @@
#include "net.h" #include "net.h"
#include <array>
#include "pico/unique_id.h" #include "pico/unique_id.h"
#include "pico/time.h" #include "pico/time.h"
#include "eth.h" #include "eth.h"
#include "arp.h"
#include "ipv4.h"
#include "udp.h"
#include "igmp.h" #include "igmp.h"
#include "parse_buffer.h" #include "parse_buffer.h"
#include "prepend_buffer.h" #include "prepend_buffer.h"
#include "w6300.h" #include "w6300.h"
#include "debug_log.h" #include "debug_log.h"
static constexpr ipv4::ip4_addr IP_BROADCAST_SUBNET = {169, 254, 255, 255};
static net_state state; static net_state state;
static w6300::socket_id raw_socket{0}; static w6300::socket_id raw_socket{0};
static frame_cb_list frame_callbacks; static frame_cb_list frame_callbacks;
struct ethertype_entry {
uint16_t ethertype_be;
ethertype_handler fn;
};
static std::array<ethertype_entry, 8> eth_handlers;
static size_t eth_handler_count = 0;
void net_register_ethertype(uint16_t ethertype_be, ethertype_handler fn) {
if (eth_handler_count < eth_handlers.size())
eth_handlers[eth_handler_count++] = {ethertype_be, fn};
}
void net_send_raw(std::span<const uint8_t> data) { void net_send_raw(std::span<const uint8_t> data) {
dlog_if_slow("net_send_raw", 1000, [&]{ dlog_if_slow("net_send_raw", 1000, [&]{
auto result = w6300::send(raw_socket, data); auto result = w6300::send(raw_socket, data);
@@ -41,13 +49,11 @@ static void process_frame(std::span<const uint8_t> frame, span_writer& tx) {
frame_callbacks.remove(n); frame_callbacks.remove(n);
}); });
switch (eth_hdr.ethertype) { for (size_t i = 0; i < eth_handler_count; i++) {
case eth::ETH_ARP: if (eth_handlers[i].ethertype_be == eth_hdr.ethertype) {
arp::handle(frame, tx, state.mac, state.ip); eth_handlers[i].fn(frame, tx);
break; break;
case eth::ETH_IPV4: }
ipv4::handle(frame, tx, state.mac, state.ip, IP_BROADCAST_SUBNET);
break;
} }
} }