Drop send_raw parameter threading: arp/icmp/igmp/ipv4 call net_send_raw directly

This commit is contained in:
Ian Gulliver
2026-04-17 14:35:10 -07:00
parent cdc113285a
commit e2daf04bed
10 changed files with 30 additions and 45 deletions

View File

@@ -1,5 +1,4 @@
#pragma once #pragma once
#include <functional>
#include <span> #include <span>
#include "eth.h" #include "eth.h"
#include "ipv4.h" #include "ipv4.h"
@@ -21,7 +20,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, eth::mac_addr our_mac, ipv4::ip4_addr our_ip);
std::function<void(std::span<const uint8_t>)> send_raw);
} // namespace arp } // namespace arp

View File

@@ -1,6 +1,5 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <functional>
#include <span> #include <span>
#include "eth.h" #include "eth.h"
#include "ipv4.h" #include "ipv4.h"
@@ -18,8 +17,7 @@ struct __attribute__((packed)) echo {
static_assert(sizeof(echo) == 8); static_assert(sizeof(echo) == 8);
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, eth::mac_addr our_mac, ipv4::ip4_addr our_ip);
std::function<void(std::span<const uint8_t>)> send_raw);
template <typename Buf> template <typename Buf>
void prepend_echo_request(Buf& buf, void prepend_echo_request(Buf& buf,

View File

@@ -1,6 +1,5 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <functional>
#include <span> #include <span>
#include "eth.h" #include "eth.h"
#include "ipv4.h" #include "ipv4.h"
@@ -24,15 +23,12 @@ bool is_member(const ipv4::ip4_addr& ip);
bool is_member_mac(const eth::mac_addr& mac); bool is_member_mac(const eth::mac_addr& mac);
void join(const ipv4::ip4_addr& group, void join(const ipv4::ip4_addr& group,
eth::mac_addr our_mac, ipv4::ip4_addr our_ip, eth::mac_addr our_mac, ipv4::ip4_addr our_ip);
std::function<void(std::span<const uint8_t>)> send_raw);
void send_all_reports(eth::mac_addr our_mac, ipv4::ip4_addr our_ip, void send_all_reports(eth::mac_addr our_mac, ipv4::ip4_addr our_ip);
std::function<void(std::span<const uint8_t>)> send_raw);
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, eth::mac_addr our_mac, ipv4::ip4_addr our_ip);
std::function<void(std::span<const uint8_t>)> send_raw);
template <typename Buf> template <typename Buf>
void prepend_report(Buf& buf, const eth::mac_addr& src_mac, ipv4::ip4_addr src_ip, void prepend_report(Buf& buf, const eth::mac_addr& src_mac, ipv4::ip4_addr src_ip,

View File

@@ -58,7 +58,6 @@ void prepend(Buf& buf, const eth::mac_addr& dst_mac, const eth::mac_addr& src_ma
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, 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); std::function<void(std::span<const uint8_t>, span_writer&)> handle_udp);
} // namespace ipv4 } // namespace ipv4

View File

@@ -1,4 +1,5 @@
#include "arp.h" #include "arp.h"
#include "net.h"
#include "parse_buffer.h" #include "parse_buffer.h"
#include "prepend_buffer.h" #include "prepend_buffer.h"
@@ -10,8 +11,7 @@ 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, eth::mac_addr our_mac, ipv4::ip4_addr our_ip) {
std::function<void(std::span<const uint8_t>)> send_raw) {
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>();
@@ -36,7 +36,7 @@ void handle(std::span<const uint8_t> frame, span_writer& tx,
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, our_mac, eth::ETH_ARP);
send_raw(buf.span()); net_send_raw(buf.span());
} }
} // namespace arp } // namespace arp

View File

@@ -1,5 +1,4 @@
#include "dispatch.h" #include "dispatch.h"
#include <unordered_map>
#include "pico/stdlib.h" #include "pico/stdlib.h"
#include "wire.h" #include "wire.h"
#include "timer_queue.h" #include "timer_queue.h"
@@ -12,7 +11,7 @@ static timer_queue timers;
static void igmp_reannounce() { static void igmp_reannounce() {
auto& ns = net_get_state(); auto& ns = net_get_state();
igmp::send_all_reports(ns.mac, ns.ip, net_send_raw); igmp::send_all_reports(ns.mac, ns.ip);
dispatch_schedule_ms(60000, igmp_reannounce); dispatch_schedule_ms(60000, igmp_reannounce);
} }
@@ -33,7 +32,7 @@ bool dispatch_cancel_timer(timer_handle h) {
} }
[[noreturn]] void dispatch_run(std::span<const handler_entry> handlers) { [[noreturn]] void dispatch_run(std::span<const handler_entry> handlers) {
std::unordered_map<int8_t, handler_fn> handler_map; std::array<handler_fn, 128> handler_map{};
for (auto& entry : handlers) { for (auto& entry : handlers) {
handler_map[entry.type_id] = entry.handle; handler_map[entry.type_id] = entry.handle;
} }
@@ -41,13 +40,12 @@ bool dispatch_cancel_timer(timer_handle h) {
static std::array<uint8_t, 4096> tx_buf; static std::array<uint8_t, 4096> tx_buf;
auto dispatch_msg = [&](const DecodedMessage& msg, send_fn send) { auto dispatch_msg = [&](const DecodedMessage& msg, send_fn send) {
auto it = handler_map.find(msg.type_id); if (msg.type_id < 0 || !handler_map[msg.type_id]) {
if (it == handler_map.end()) {
dlogf("dispatch: unknown type_id %d", msg.type_id); dlogf("dispatch: unknown type_id %d", msg.type_id);
return; return;
} }
responder resp{msg.message_id, std::move(send)}; responder resp{msg.message_id, std::move(send)};
it->second(resp, msg.payload); handler_map[msg.type_id](resp, msg.payload);
}; };
net_set_handler([&](std::span<const uint8_t> payload, net_set_handler([&](std::span<const uint8_t> payload,

View File

@@ -1,14 +1,14 @@
#include "icmp.h" #include "icmp.h"
#include <cstring> #include <cstring>
#include "ipv4.h" #include "ipv4.h"
#include "net.h"
#include "parse_buffer.h" #include "parse_buffer.h"
#include "prepend_buffer.h" #include "prepend_buffer.h"
namespace icmp { namespace icmp {
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, eth::mac_addr our_mac, ipv4::ip4_addr our_ip) {
std::function<void(std::span<const uint8_t>)> send_raw) {
parse_buffer pb(frame); parse_buffer pb(frame);
auto* eth_hdr = pb.consume<eth::header>(); auto* eth_hdr = pb.consume<eth::header>();
auto* ip = pb.consume<ipv4::header>(); auto* ip = pb.consume<ipv4::header>();
@@ -34,7 +34,7 @@ void handle(std::span<const uint8_t> frame, span_writer& tx,
reply->checksum = ipv4::checksum(reply, icmp_len); reply->checksum = ipv4::checksum(reply, icmp_len);
ipv4::prepend(buf, eth_hdr->src, our_mac, our_ip, ip->src, 1, icmp_len); ipv4::prepend(buf, eth_hdr->src, our_mac, our_ip, ip->src, 1, icmp_len);
send_raw(buf.span()); net_send_raw(buf.span());
} }
bool parse_echo_reply(std::span<const uint8_t> frame, ipv4::ip4_addr& src_ip, uint16_t expected_id) { bool parse_echo_reply(std::span<const uint8_t> frame, ipv4::ip4_addr& src_ip, uint16_t expected_id) {

View File

@@ -1,6 +1,7 @@
#include "igmp.h" #include "igmp.h"
#include <vector> #include <vector>
#include "ipv4.h" #include "ipv4.h"
#include "net.h"
#include "parse_buffer.h" #include "parse_buffer.h"
#include "prepend_buffer.h" #include "prepend_buffer.h"
@@ -34,31 +35,27 @@ bool is_member_mac(const eth::mac_addr& mac) {
} }
static void send_report(const ipv4::ip4_addr& group, static void send_report(const ipv4::ip4_addr& group,
eth::mac_addr our_mac, ipv4::ip4_addr our_ip, eth::mac_addr our_mac, ipv4::ip4_addr our_ip) {
std::function<void(std::span<const uint8_t>)> send_raw) {
prepend_buffer<4096> buf; prepend_buffer<4096> buf;
prepend_report(buf, our_mac, our_ip, group); prepend_report(buf, our_mac, our_ip, group);
send_raw(buf.span()); net_send_raw(buf.span());
} }
void join(const ipv4::ip4_addr& group, void join(const ipv4::ip4_addr& group,
eth::mac_addr our_mac, ipv4::ip4_addr our_ip, eth::mac_addr our_mac, ipv4::ip4_addr our_ip) {
std::function<void(std::span<const uint8_t>)> send_raw) {
for (auto& g : groups) for (auto& g : groups)
if (g.ip == group) return; if (g.ip == group) return;
groups.push_back({group, mac_for_ip(group)}); groups.push_back({group, mac_for_ip(group)});
send_report(group, our_mac, our_ip, send_raw); send_report(group, our_mac, our_ip);
} }
void send_all_reports(eth::mac_addr our_mac, ipv4::ip4_addr our_ip, void send_all_reports(eth::mac_addr our_mac, ipv4::ip4_addr our_ip) {
std::function<void(std::span<const uint8_t>)> send_raw) {
for (auto& g : groups) for (auto& g : groups)
send_report(g.ip, our_mac, our_ip, send_raw); send_report(g.ip, our_mac, our_ip);
} }
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, eth::mac_addr our_mac, ipv4::ip4_addr our_ip) {
std::function<void(std::span<const uint8_t>)> send_raw) {
parse_buffer pb(frame); parse_buffer pb(frame);
pb.consume<eth::header>(); pb.consume<eth::header>();
auto* ip = pb.consume<ipv4::header>(); auto* ip = pb.consume<ipv4::header>();
@@ -73,11 +70,11 @@ void handle(std::span<const uint8_t> frame, span_writer& tx,
if (msg->group == ipv4::ip4_addr{0, 0, 0, 0}) { if (msg->group == ipv4::ip4_addr{0, 0, 0, 0}) {
for (auto& g : groups) for (auto& g : groups)
send_report(g.ip, our_mac, our_ip, send_raw); send_report(g.ip, our_mac, our_ip);
} else { } else {
for (auto& g : groups) { for (auto& g : groups) {
if (g.ip == msg->group) { if (g.ip == msg->group) {
send_report(g.ip, our_mac, our_ip, send_raw); send_report(g.ip, our_mac, our_ip);
break; break;
} }
} }

View File

@@ -25,7 +25,6 @@ static bool ip_match(const ip4_addr& dst, const ip4_addr& our_ip, const ip4_addr
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, 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) { std::function<void(std::span<const uint8_t>, span_writer&)> handle_udp) {
parse_buffer pb(frame); parse_buffer pb(frame);
pb.consume<eth::header>(); pb.consume<eth::header>();
@@ -40,10 +39,10 @@ void handle(std::span<const uint8_t> frame, span_writer& tx,
case 1: case 1:
if (!ip_match(ip->dst, our_ip, subnet_broadcast)) if (!ip_match(ip->dst, our_ip, subnet_broadcast))
return; return;
icmp::handle(frame, tx, our_mac, our_ip, send_raw); icmp::handle(frame, tx, our_mac, our_ip);
break; break;
case 2: case 2:
igmp::handle(frame, tx, our_mac, our_ip, send_raw); igmp::handle(frame, tx, our_mac, our_ip);
break; break;
case 17: case 17:
if (!ip_match(ip->dst, our_ip, subnet_broadcast)) if (!ip_match(ip->dst, our_ip, subnet_broadcast))

View File

@@ -81,10 +81,10 @@ static void process_frame(std::span<const uint8_t> frame, span_writer& tx) {
switch (eth_hdr.ethertype) { switch (eth_hdr.ethertype) {
case eth::ETH_ARP: case eth::ETH_ARP:
arp::handle(frame, tx, state.mac, state.ip, net_send_raw); arp::handle(frame, tx, state.mac, state.ip);
break; break;
case eth::ETH_IPV4: case eth::ETH_IPV4:
ipv4::handle(frame, tx, state.mac, state.ip, IP_BROADCAST_SUBNET, net_send_raw, handle_udp); ipv4::handle(frame, tx, state.mac, state.ip, IP_BROADCAST_SUBNET, handle_udp);
break; break;
} }
} }
@@ -112,7 +112,7 @@ bool net_init() {
w6300::open_socket(raw_socket, w6300::protocol::macraw, w6300::sock_flag::none); w6300::open_socket(raw_socket, w6300::protocol::macraw, w6300::sock_flag::none);
w6300::set_interrupt_mask(w6300::ik_sock_0); w6300::set_interrupt_mask(w6300::ik_sock_0);
igmp::join(igmp::PICOMAP_DISCOVERY_GROUP, state.mac, state.ip, net_send_raw); igmp::join(igmp::PICOMAP_DISCOVERY_GROUP, state.mac, state.ip);
return true; return true;
} }