Compare commits
1 Commits
cc1448d6a2
...
fceae27f10
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fceae27f10 |
@@ -4,6 +4,7 @@ add_subdirectory(util)
|
||||
add_subdirectory(w6300)
|
||||
add_subdirectory(debug_log)
|
||||
add_subdirectory(msgpack)
|
||||
add_subdirectory(eth)
|
||||
|
||||
add_library(limen STATIC
|
||||
src/arp.cpp
|
||||
@@ -13,7 +14,6 @@ add_library(limen STATIC
|
||||
src/icmp.cpp
|
||||
src/igmp.cpp
|
||||
src/ipv4.cpp
|
||||
src/net.cpp
|
||||
src/test_handlers.cpp
|
||||
src/udp.cpp
|
||||
)
|
||||
@@ -29,9 +29,9 @@ target_link_libraries(limen PUBLIC
|
||||
w6300
|
||||
debug_log
|
||||
msgpack
|
||||
eth
|
||||
pico_stdlib
|
||||
pico_sha256
|
||||
pico_unique_id
|
||||
)
|
||||
|
||||
set(LIMEN_PARTITION_TABLE ${CMAKE_CURRENT_SOURCE_DIR}/partition_table.json CACHE INTERNAL "")
|
||||
|
||||
12
eth/CMakeLists.txt
Normal file
12
eth/CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
add_library(eth STATIC eth.cpp)
|
||||
|
||||
target_include_directories(eth PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
target_compile_options(eth PRIVATE -Wall -Wextra -Wno-unused-parameter)
|
||||
|
||||
target_link_libraries(eth PUBLIC
|
||||
util
|
||||
debug_log
|
||||
w6300
|
||||
pico_unique_id
|
||||
)
|
||||
@@ -1,18 +1,16 @@
|
||||
#include "net.h"
|
||||
#include "eth.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 eth {
|
||||
|
||||
namespace {
|
||||
|
||||
state g_state;
|
||||
mac_addr g_mac;
|
||||
w6300::socket_id raw_socket{0};
|
||||
frame_cb_list frame_callbacks;
|
||||
|
||||
@@ -26,11 +24,11 @@ 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 default_mac_filter(const mac_addr& dst) {
|
||||
return dst == g_mac || dst == MAC_BROADCAST;
|
||||
}
|
||||
|
||||
bool mac_match(const eth::mac_addr& dst) {
|
||||
bool mac_match(const mac_addr& dst) {
|
||||
for (size_t i = 0; i < mac_filter_count; i++) {
|
||||
if (mac_filters[i](dst)) {
|
||||
return true;
|
||||
@@ -40,10 +38,10 @@ bool mac_match(const eth::mac_addr& dst) {
|
||||
}
|
||||
|
||||
void process_frame(std::span<const uint8_t> frame, span_writer& tx) {
|
||||
if (frame.size() < sizeof(eth::header)) {
|
||||
if (frame.size() < sizeof(header)) {
|
||||
return;
|
||||
}
|
||||
auto& eth_hdr = *reinterpret_cast<const eth::header*>(frame.data());
|
||||
auto& eth_hdr = *reinterpret_cast<const header*>(frame.data());
|
||||
|
||||
if (!mac_match(eth_hdr.dst)) {
|
||||
return;
|
||||
@@ -72,7 +70,7 @@ void register_default_mac_filter() {
|
||||
|
||||
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));
|
||||
dlogf("eth::register_ethertype overflow: ethertype=0x%04x dropped", __builtin_bswap16(ethertype_be));
|
||||
return;
|
||||
}
|
||||
eth_handlers[eth_handler_count++] = {ethertype_be, fn};
|
||||
@@ -80,14 +78,14 @@ void register_ethertype(uint16_t ethertype_be, ethertype_handler fn) {
|
||||
|
||||
void register_mac_filter(mac_filter fn) {
|
||||
if (mac_filter_count >= mac_filters.size()) {
|
||||
dlog("net::register_mac_filter overflow: filter dropped");
|
||||
dlog("eth::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, [&]{
|
||||
dlog_if_slow("eth::send_raw", 1000, [&]{
|
||||
auto result = w6300::send(raw_socket, data);
|
||||
if (!result) {
|
||||
dlogf("w6300 send failed: %zu bytes, err %d",
|
||||
@@ -106,17 +104,12 @@ bool init() {
|
||||
|
||||
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];
|
||||
g_mac[0] = (uid.id[0] & 0xFC) | 0x02;
|
||||
g_mac[1] = uid.id[1];
|
||||
g_mac[2] = uid.id[2];
|
||||
g_mac[3] = uid.id[3];
|
||||
g_mac[4] = uid.id[4];
|
||||
g_mac[5] = uid.id[5];
|
||||
|
||||
w6300::open_socket(raw_socket, w6300::protocol::macraw, w6300::sock_flag::none);
|
||||
w6300::set_interrupt_mask(w6300::ik_sock_0);
|
||||
@@ -124,14 +117,14 @@ bool init() {
|
||||
return true;
|
||||
}
|
||||
|
||||
const state& get_state() {
|
||||
return g_state;
|
||||
const mac_addr& get_mac() {
|
||||
return g_mac;
|
||||
}
|
||||
|
||||
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");
|
||||
dlog("eth::add_frame_callback overflow: callback dropped");
|
||||
}
|
||||
return h;
|
||||
}
|
||||
@@ -158,4 +151,4 @@ void poll(std::span<uint8_t> tx) {
|
||||
w6300::rearm_gpio_irq();
|
||||
}
|
||||
|
||||
} // namespace net
|
||||
} // namespace eth
|
||||
47
eth/eth.h
Normal file
47
eth/eth.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
#include "callback_list.h"
|
||||
#include "span_writer.h"
|
||||
|
||||
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);
|
||||
|
||||
template <typename Buf>
|
||||
void prepend(Buf& buf, const mac_addr& dst, const mac_addr& src, uint16_t ethertype) {
|
||||
auto* h = buf.template prepend<header>();
|
||||
h->dst = dst;
|
||||
h->src = src;
|
||||
h->ethertype = ethertype;
|
||||
}
|
||||
|
||||
using frame_callback = bool (*)(std::span<const uint8_t> frame);
|
||||
using frame_cb_list = callback_list<frame_callback, 16>;
|
||||
using frame_cb_handle = frame_cb_list::node*;
|
||||
|
||||
using ethertype_handler = void (*)(std::span<const uint8_t> frame, span_writer& tx);
|
||||
using mac_filter = bool (*)(const mac_addr& dst);
|
||||
|
||||
bool init();
|
||||
const mac_addr& get_mac();
|
||||
frame_cb_handle add_frame_callback(frame_callback cb);
|
||||
void remove_frame_callback(frame_cb_handle h);
|
||||
void poll(std::span<uint8_t> tx);
|
||||
void send_raw(std::span<const uint8_t> data);
|
||||
void register_ethertype(uint16_t ethertype_be, ethertype_handler fn);
|
||||
void register_mac_filter(mac_filter fn);
|
||||
|
||||
} // namespace eth
|
||||
@@ -5,7 +5,8 @@
|
||||
#include <span>
|
||||
#include "wire.h"
|
||||
#include "timer_queue.h"
|
||||
#include "net.h"
|
||||
#include "eth.h"
|
||||
#include "ipv4.h"
|
||||
#include "prepend_buffer.h"
|
||||
#include "udp.h"
|
||||
|
||||
@@ -17,7 +18,6 @@ struct responder {
|
||||
|
||||
template <typename T>
|
||||
void respond(const T& msg) const {
|
||||
const auto& ns = net::get_state();
|
||||
prepend_buffer<4096> buf;
|
||||
span_writer out(buf.payload_ptr(), 2048);
|
||||
auto r = encode_response_into(out, message_id, msg);
|
||||
@@ -25,9 +25,9 @@ struct responder {
|
||||
return;
|
||||
}
|
||||
buf.append(*r);
|
||||
udp::prepend(buf, reply_to.mac, ns.mac, ns.ip, reply_to.ip,
|
||||
udp::prepend(buf, reply_to.mac, eth::get_mac(), ipv4::get_ip(), reply_to.ip,
|
||||
dispatch_listen_port_be(), reply_to.port, *r);
|
||||
net::send_raw(buf.span());
|
||||
eth::send_raw(buf.span());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
#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);
|
||||
|
||||
template <typename Buf>
|
||||
void prepend(Buf& buf, const mac_addr& dst, const mac_addr& src, uint16_t ethertype) {
|
||||
auto* h = buf.template prepend<header>();
|
||||
h->dst = dst;
|
||||
h->src = src;
|
||||
h->ethertype = ethertype;
|
||||
}
|
||||
|
||||
} // namespace eth
|
||||
@@ -59,6 +59,9 @@ 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 init();
|
||||
const ip4_addr& get_ip();
|
||||
|
||||
bool addressed_to_us(ip4_addr dst);
|
||||
|
||||
using protocol_handler = void (*)(std::span<const uint8_t> frame, span_writer& tx);
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
#include "eth.h"
|
||||
#include "ipv4.h"
|
||||
#include "span_writer.h"
|
||||
#include "callback_list.h"
|
||||
|
||||
namespace net {
|
||||
|
||||
struct state {
|
||||
eth::mac_addr mac;
|
||||
ipv4::ip4_addr ip;
|
||||
};
|
||||
|
||||
using frame_callback = bool (*)(std::span<const uint8_t> frame);
|
||||
|
||||
using frame_cb_list = callback_list<frame_callback, 16>;
|
||||
using frame_cb_handle = frame_cb_list::node*;
|
||||
|
||||
using ethertype_handler = void (*)(std::span<const uint8_t> frame, span_writer& tx);
|
||||
using mac_filter = bool (*)(const eth::mac_addr& dst);
|
||||
|
||||
bool init();
|
||||
const state& get_state();
|
||||
frame_cb_handle add_frame_callback(frame_callback cb);
|
||||
void remove_frame_callback(frame_cb_handle h);
|
||||
void poll(std::span<uint8_t> tx);
|
||||
void send_raw(std::span<const uint8_t> data);
|
||||
void register_ethertype(uint16_t ethertype_be, ethertype_handler fn);
|
||||
void register_mac_filter(mac_filter fn);
|
||||
|
||||
} // namespace net
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <span>
|
||||
#include "eth.h"
|
||||
#include "ipv4.h"
|
||||
#include "net.h"
|
||||
#include "span_writer.h"
|
||||
|
||||
namespace udp {
|
||||
|
||||
18
src/arp.cpp
18
src/arp.cpp
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
18
src/ipv4.cpp
18
src/ipv4.cpp
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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++)
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#include <array>
|
||||
#include "eth.h"
|
||||
#include "ipv4.h"
|
||||
#include "net.h"
|
||||
#include "parse_buffer.h"
|
||||
#include "debug_log.h"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user