merge net into eth lib; ipv4 owns its IP via ipv4::init/get_ip; drop state struct in favor of eth::get_mac/ipv4::get_ip

This commit is contained in:
Ian Gulliver
2026-05-01 11:03:16 -07:00
parent cc1448d6a2
commit fceae27f10
17 changed files with 149 additions and 148 deletions
+10 -8
View File
@@ -1,5 +1,6 @@
#include "arp.h"
#include "net.h"
#include "eth.h"
#include "ipv4.h"
#include "parse_buffer.h"
#include "prepend_buffer.h"
@@ -11,7 +12,8 @@ static constexpr uint16_t ARP_OP_REQUEST = __builtin_bswap16(1);
static constexpr uint16_t ARP_OP_REPLY = __builtin_bswap16(2);
void handle(std::span<const uint8_t> frame, span_writer& tx) {
const auto& ns = net::get_state();
const auto& mac = eth::get_mac();
const auto& ip = ipv4::get_ip();
parse_buffer pb(frame);
pb.consume<eth::header>();
auto* arp_hdr = pb.consume<header>();
@@ -21,7 +23,7 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
if (arp_hdr->ptype != ARP_PTYPE_IPV4) return;
if (arp_hdr->hlen != 6 || arp_hdr->plen != 4) return;
if (arp_hdr->oper != ARP_OP_REQUEST) return;
if (arp_hdr->tpa != ns.ip) return;
if (arp_hdr->tpa != ip) return;
prepend_buffer<4096> buf;
auto* reply = buf.template prepend<header>();
@@ -30,18 +32,18 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
reply->hlen = 6;
reply->plen = 4;
reply->oper = ARP_OP_REPLY;
reply->sha = ns.mac;
reply->spa = ns.ip;
reply->sha = mac;
reply->spa = ip;
reply->tha = arp_hdr->sha;
reply->tpa = arp_hdr->spa;
eth::prepend(buf, arp_hdr->sha, ns.mac, eth::ETH_ARP);
eth::prepend(buf, arp_hdr->sha, mac, eth::ETH_ARP);
net::send_raw(buf.span());
eth::send_raw(buf.span());
}
__attribute__((constructor))
static void register_ethertype() {
net::register_ethertype(eth::ETH_ARP, handle);
eth::register_ethertype(eth::ETH_ARP, handle);
}
} // namespace arp
+5 -3
View File
@@ -3,7 +3,8 @@
#include "pico/stdlib.h"
#include "wire.h"
#include "timer_queue.h"
#include "net.h"
#include "eth.h"
#include "ipv4.h"
#include "igmp.h"
#include "udp.h"
#include "debug_log.h"
@@ -34,7 +35,8 @@ static void on_udp_message(std::span<const uint8_t> payload, const udp::address&
void dispatch_init(uint16_t port_be) {
listen_port_be = port_be;
udp::register_port(port_be, on_udp_message);
net::init();
eth::init();
ipv4::init();
dispatch_schedule_ms(60000, igmp_reannounce);
dlog("dispatch_init complete");
}
@@ -59,7 +61,7 @@ bool dispatch_cancel_timer(timer_handle h) {
uint32_t save = save_and_disable_interrupts();
dlog_if_slow("timers", 1000, [&]{ timers.run(); });
dlog_if_slow("net::poll", 1000, [&]{ net::poll(std::span{tx_buf}); });
dlog_if_slow("eth::poll", 1000, [&]{ eth::poll(std::span{tx_buf}); });
__wfi();
restore_interrupts(save);
+4 -4
View File
@@ -5,7 +5,8 @@
#include "hardware/watchdog.h"
#include "flash.h"
#include "dispatch.h"
#include "net.h"
#include "eth.h"
#include "ipv4.h"
#include "debug_log.h"
static boot_reason detected_boot_reason;
@@ -34,9 +35,8 @@ std::optional<ResponseInfo> handle_info(const responder&, const RequestInfo&) {
pico_unique_board_id_t uid;
pico_get_unique_board_id(&uid);
std::copy(uid.id, uid.id + 8, resp.board_id.begin());
auto& ns = net::get_state();
resp.mac = ns.mac;
resp.ip = ns.ip;
resp.mac = eth::get_mac();
resp.ip = ipv4::get_ip();
resp.firmware_name = firmware_name;
resp.boot = detected_boot_reason;
resp.build_epoch = firmware_build_epoch;
+3 -4
View File
@@ -1,7 +1,7 @@
#include "icmp.h"
#include <cstring>
#include "eth.h"
#include "ipv4.h"
#include "net.h"
#include "parse_buffer.h"
#include "prepend_buffer.h"
@@ -24,7 +24,6 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
if (!icmp_pkt) return;
if (icmp_pkt->type != 8) return;
const auto& ns = net::get_state();
prepend_buffer<4096> buf;
memcpy(buf.append(icmp_len), pb.remaining().data() - sizeof(echo), icmp_len);
@@ -33,8 +32,8 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
reply->checksum = 0;
reply->checksum = ipv4::checksum(reply, icmp_len);
ipv4::prepend(buf, eth_hdr->src, ns.mac, ns.ip, ip->src, 1, icmp_len);
net::send_raw(buf.span());
ipv4::prepend(buf, eth_hdr->src, eth::get_mac(), ipv4::get_ip(), ip->src, 1, icmp_len);
eth::send_raw(buf.span());
}
__attribute__((constructor))
+4 -5
View File
@@ -1,7 +1,7 @@
#include "igmp.h"
#include <vector>
#include "eth.h"
#include "ipv4.h"
#include "net.h"
#include "parse_buffer.h"
#include "prepend_buffer.h"
@@ -35,10 +35,9 @@ bool is_member_mac(const eth::mac_addr& mac) {
}
static void send_report(const ipv4::ip4_addr& group) {
const auto& ns = net::get_state();
prepend_buffer<4096> buf;
prepend_report(buf, ns.mac, ns.ip, group);
net::send_raw(buf.span());
prepend_report(buf, eth::get_mac(), ipv4::get_ip(), group);
eth::send_raw(buf.span());
}
void join(const ipv4::ip4_addr& group) {
@@ -82,7 +81,7 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
__attribute__((constructor))
static void register_protocol() {
ipv4::register_protocol(2, handle);
net::register_mac_filter(is_member_mac);
eth::register_mac_filter(is_member_mac);
ipv4::register_addr_filter(is_member);
}
+14 -4
View File
@@ -1,6 +1,6 @@
#include "ipv4.h"
#include <array>
#include "net.h"
#include "eth.h"
#include "parse_buffer.h"
#include "debug_log.h"
@@ -10,6 +10,8 @@ namespace {
constexpr ip4_addr IP_BROADCAST_ALL = {255, 255, 255, 255};
ip4_addr g_ip;
std::array<addr_filter, 4> addr_filters;
size_t addr_filter_count = 0;
@@ -21,12 +23,20 @@ std::array<protocol_entry, 8> protocol_handlers;
size_t protocol_handler_count = 0;
bool default_addr_filter(const ip4_addr& dst) {
const auto& ns = net::get_state();
return dst == ns.ip || dst == IP_BROADCAST_ALL || dst == SUBNET_BROADCAST;
return dst == g_ip || dst == IP_BROADCAST_ALL || dst == SUBNET_BROADCAST;
}
} // namespace
void init() {
const auto& mac = eth::get_mac();
g_ip = {169, 254, mac[4], mac[5]};
}
const ip4_addr& get_ip() {
return g_ip;
}
uint16_t checksum(const void* data, size_t len) {
auto p = static_cast<const uint8_t*>(data);
uint32_t sum = 0;
@@ -93,7 +103,7 @@ void handle(std::span<const uint8_t> frame, span_writer& tx) {
__attribute__((constructor))
static void register_self() {
net::register_ethertype(eth::ETH_IPV4, handle);
eth::register_ethertype(eth::ETH_IPV4, handle);
register_addr_filter(default_addr_filter);
}
-161
View File
@@ -1,161 +0,0 @@
#include "net.h"
#include <array>
#include "pico/unique_id.h"
#include "pico/time.h"
#include "eth.h"
#include "parse_buffer.h"
#include "prepend_buffer.h"
#include "w6300.h"
#include "debug_log.h"
namespace net {
namespace {
state g_state;
w6300::socket_id raw_socket{0};
frame_cb_list frame_callbacks;
struct ethertype_entry {
uint16_t ethertype_be;
ethertype_handler fn;
};
std::array<ethertype_entry, 8> eth_handlers;
size_t eth_handler_count = 0;
std::array<mac_filter, 4> mac_filters;
size_t mac_filter_count = 0;
bool default_mac_filter(const eth::mac_addr& dst) {
return dst == g_state.mac || dst == eth::MAC_BROADCAST;
}
bool mac_match(const eth::mac_addr& dst) {
for (size_t i = 0; i < mac_filter_count; i++) {
if (mac_filters[i](dst)) {
return true;
}
}
return false;
}
void process_frame(std::span<const uint8_t> frame, span_writer& tx) {
if (frame.size() < sizeof(eth::header)) {
return;
}
auto& eth_hdr = *reinterpret_cast<const eth::header*>(frame.data());
if (!mac_match(eth_hdr.dst)) {
return;
}
frame_callbacks.for_each([&](frame_cb_list::node* n) {
if (n->value(frame)) {
frame_callbacks.remove(n);
}
});
for (size_t i = 0; i < eth_handler_count; i++) {
if (eth_handlers[i].ethertype_be == eth_hdr.ethertype) {
eth_handlers[i].fn(frame, tx);
break;
}
}
}
__attribute__((constructor))
void register_default_mac_filter() {
register_mac_filter(default_mac_filter);
}
} // namespace
void register_ethertype(uint16_t ethertype_be, ethertype_handler fn) {
if (eth_handler_count >= eth_handlers.size()) {
dlogf("net::register_ethertype overflow: ethertype=0x%04x dropped", __builtin_bswap16(ethertype_be));
return;
}
eth_handlers[eth_handler_count++] = {ethertype_be, fn};
}
void register_mac_filter(mac_filter fn) {
if (mac_filter_count >= mac_filters.size()) {
dlog("net::register_mac_filter overflow: filter dropped");
return;
}
mac_filters[mac_filter_count++] = fn;
}
void send_raw(std::span<const uint8_t> data) {
dlog_if_slow("net::send_raw", 1000, [&]{
auto result = w6300::send(raw_socket, data);
if (!result) {
dlogf("w6300 send failed: %zu bytes, err %d",
data.size(), static_cast<int>(result.error()));
}
});
}
bool init() {
w6300::init_spi();
w6300::reset();
w6300::init();
if (!w6300::check()) {
return false;
}
pico_unique_board_id_t uid;
pico_get_unique_board_id(&uid);
g_state.mac[0] = (uid.id[0] & 0xFC) | 0x02;
g_state.mac[1] = uid.id[1];
g_state.mac[2] = uid.id[2];
g_state.mac[3] = uid.id[3];
g_state.mac[4] = uid.id[4];
g_state.mac[5] = uid.id[5];
g_state.ip[0] = 169;
g_state.ip[1] = 254;
g_state.ip[2] = g_state.mac[4];
g_state.ip[3] = g_state.mac[5];
w6300::open_socket(raw_socket, w6300::protocol::macraw, w6300::sock_flag::none);
w6300::set_interrupt_mask(w6300::ik_sock_0);
return true;
}
const state& get_state() {
return g_state;
}
frame_cb_handle add_frame_callback(frame_callback cb) {
auto h = frame_callbacks.insert(cb);
if (!h) {
dlog("net::add_frame_callback overflow: callback dropped");
}
return h;
}
void remove_frame_callback(frame_cb_handle h) {
frame_callbacks.remove(h);
}
void poll(std::span<uint8_t> tx) {
if (!w6300::irq_pending) {
return;
}
w6300::irq_pending = false;
w6300::clear_interrupt(w6300::ik_int_all);
static uint8_t rx_buf[1518];
for (int i = 0; i < 16 && w6300::get_socket_recv_buf(raw_socket) > 0; i++) {
auto result = w6300::recv(raw_socket, std::span{rx_buf});
if (!result) {
break;
}
span_writer tx_writer(tx);
process_frame({rx_buf, *result}, tx_writer);
}
w6300::rearm_gpio_irq();
}
} // namespace net
+20 -23
View File
@@ -4,7 +4,8 @@
#include "pico/stdlib.h"
#include "pico/time.h"
#include "handlers.h"
#include "net.h"
#include "eth.h"
#include "ipv4.h"
#include "icmp.h"
#include "igmp.h"
#include "udp.h"
@@ -55,7 +56,7 @@ struct test_state {
bool in_flight = false;
responder resp;
timer_handle timer = nullptr;
net::frame_cb_handle frame_cb = nullptr;
eth::frame_cb_handle frame_cb = nullptr;
discovery_data* active_discovery = nullptr;
ping_rate_data* active_rate = nullptr;
@@ -72,7 +73,7 @@ static test_state ts;
static void test_end(const ResponseTest& result) {
if (ts.timer) { dispatch_cancel_timer(ts.timer); ts.timer = nullptr; }
if (ts.frame_cb) { net::remove_frame_callback(ts.frame_cb); ts.frame_cb = nullptr; }
if (ts.frame_cb) { eth::remove_frame_callback(ts.frame_cb); ts.frame_cb = nullptr; }
ts.active_discovery = nullptr;
ts.active_rate = nullptr;
ts.resp.respond(result);
@@ -93,7 +94,7 @@ static bool discover_reply_cb(std::span<const uint8_t> frame) {
if (options_len > 0 && !pb.skip(options_len)) return false;
auto* uhdr = pb.consume<udp::header>();
if (!uhdr || uhdr->src_port != PICOMAP_PORT_BE) return false;
if (ip->src == net::get_state().ip) return false;
if (ip->src == ipv4::get_ip()) return false;
dispatch_cancel_timer(ts.timer);
ts.timer = nullptr;
ts.frame_cb = nullptr;
@@ -105,7 +106,7 @@ static bool discover_reply_cb(std::span<const uint8_t> frame) {
}
static void discover_timeout_cb() {
net::remove_frame_callback(ts.frame_cb);
eth::remove_frame_callback(ts.frame_cb);
ts.frame_cb = nullptr;
ts.timer = nullptr;
auto cont = ts.active_discovery ? ts.active_discovery->on_timeout : nullptr;
@@ -119,7 +120,6 @@ static void discover_peer(discovery_data& d,
d.on_timeout = timeout;
ts.active_discovery = &d;
const auto& ns = net::get_state();
eth::mac_addr mcast_mac = igmp::mac_for_ip(PICOMAP_DISCOVERY_GROUP);
prepend_buffer<4096> buf;
@@ -134,13 +134,13 @@ static void discover_peer(discovery_data& d,
}
buf.append(*encoded);
udp::prepend(buf, mcast_mac, ns.mac, ns.ip, PICOMAP_DISCOVERY_GROUP,
udp::prepend(buf, mcast_mac, eth::get_mac(), ipv4::get_ip(), PICOMAP_DISCOVERY_GROUP,
PICOMAP_PORT_BE, PICOMAP_PORT_BE, *encoded, 1);
ts.frame_cb = net::add_frame_callback(discover_reply_cb);
ts.frame_cb = eth::add_frame_callback(discover_reply_cb);
ts.timer = dispatch_schedule_ms(5000, discover_timeout_cb);
net::send_raw(buf.span());
eth::send_raw(buf.span());
}
static bool igmp_report_cb(std::span<const uint8_t> frame) {
@@ -158,14 +158,13 @@ static void igmp_timeout_cb() {
}
static void test_discovery_igmp() {
const auto& ns = net::get_state();
prepend_buffer<4096> buf;
igmp::prepend_query(buf, ns.mac, ns.ip, PICOMAP_DISCOVERY_GROUP);
igmp::prepend_query(buf, eth::get_mac(), ipv4::get_ip(), PICOMAP_DISCOVERY_GROUP);
ts.frame_cb = net::add_frame_callback(igmp_report_cb);
ts.frame_cb = eth::add_frame_callback(igmp_report_cb);
ts.timer = dispatch_schedule_ms(5000, igmp_timeout_cb);
net::send_raw(buf.span());
eth::send_raw(buf.span());
}
static void info_found(const peer_info& peer) {
@@ -184,7 +183,7 @@ static bool ping_reply_cb(std::span<const uint8_t> frame) {
ipv4::ip4_addr src_ip;
if (!icmp::parse_echo_reply(frame, src_ip, PING_ECHO_ID)) return false;
ts.frame_cb = nullptr;
if (src_ip == net::get_state().ip)
if (src_ip == ipv4::get_ip())
test_end({false, {"got reply from self: " + ipv4::to_string(src_ip)}});
else
test_end({true, {"reply from " + ipv4::to_string(src_ip)}});
@@ -197,13 +196,12 @@ static void ping_timeout_cb() {
}
static void start_ping(ipv4::ip4_addr dst_ip) {
const auto& ns = net::get_state();
prepend_buffer<4096> buf;
icmp::prepend_echo_request(buf, ns.mac, ns.ip,
icmp::prepend_echo_request(buf, eth::get_mac(), ipv4::get_ip(),
eth::MAC_BROADCAST, dst_ip, PING_ECHO_ID, 1);
ts.frame_cb = net::add_frame_callback(ping_reply_cb);
ts.frame_cb = eth::add_frame_callback(ping_reply_cb);
ts.timer = dispatch_schedule_ms(5000, ping_timeout_cb);
net::send_raw(buf.span());
eth::send_raw(buf.span());
}
static void test_ping_subnet() { start_ping({169, 254, 255, 255}); }
@@ -215,22 +213,21 @@ static size_t ping_rate_frame_size() {
}
static void ping_rate_send_one() {
const auto& ns = net::get_state();
auto& r = *ts.active_rate;
prepend_buffer<4096> buf;
if (r.payload_len > 0)
memset(buf.append(r.payload_len), 0xAA, r.payload_len);
icmp::prepend_echo_request(buf, ns.mac, ns.ip,
icmp::prepend_echo_request(buf, eth::get_mac(), ipv4::get_ip(),
r.peer.mac, r.peer.ip, PING_RATE_ECHO_ID,
r.sent + 1, r.payload_len);
net::send_raw(buf.span());
eth::send_raw(buf.span());
r.sent++;
}
static bool ping_rate_reply_cb(std::span<const uint8_t> frame) {
ipv4::ip4_addr src_ip;
if (!icmp::parse_echo_reply(frame, src_ip, PING_RATE_ECHO_ID)) return false;
if (src_ip == net::get_state().ip) return false;
if (src_ip == ipv4::get_ip()) return false;
auto& r = *ts.active_rate;
r.received++;
@@ -276,7 +273,7 @@ static void ping_rate_found(const peer_info& peer) {
r.received = 0;
r.start_us = time_us_32();
ts.frame_cb = net::add_frame_callback(ping_rate_reply_cb);
ts.frame_cb = eth::add_frame_callback(ping_rate_reply_cb);
ts.timer = dispatch_schedule_ms(10000, ping_rate_timeout_cb);
for (uint16_t i = 0; i < r.pipeline && r.sent < r.target; i++)
-1
View File
@@ -2,7 +2,6 @@
#include <array>
#include "eth.h"
#include "ipv4.h"
#include "net.h"
#include "parse_buffer.h"
#include "debug_log.h"