Zero-copy encode: pack response body in place, single shared tx_buf, drain rx loop

This commit is contained in:
Ian Gulliver
2026-04-10 22:48:28 +09:00
parent 58db392bf3
commit 8408603390
8 changed files with 98 additions and 57 deletions

View File

@@ -29,7 +29,7 @@ void dispatch_schedule_ms(uint32_t ms, std::function<void()> fn) {
static usb_cdc usb;
static static_vector<uint8_t, 256> usb_rx_buf;
static uint8_t tx_buf[1514];
static std::array<uint8_t, 1514> tx_buf;
net_set_handler([&](std::span<const uint8_t> payload, span_writer &out) -> size_t {
auto msg = try_decode(payload.data(), payload.size());
@@ -45,7 +45,7 @@ void dispatch_schedule_ms(uint32_t ms, std::function<void()> fn) {
dlog_if_slow("tud_task", 1000, [&]{ tud_task(); });
dlog_if_slow("drain", 1000, [&]{ usb.drain(); });
dlog_if_slow("timers", 1000, [&]{ timers.run(); });
dlog_if_slow("net_poll", 1000, [&]{ net_poll(); });
dlog_if_slow("net_poll", 1000, [&]{ net_poll(std::span{tx_buf}); });
while (tud_cdc_available()) {
uint8_t byte;
@@ -63,16 +63,16 @@ void dispatch_schedule_ms(uint32_t ms, std::function<void()> fn) {
auto it = handler_map.find(msg->type_id);
if (it != handler_map.end()) {
span_writer out(tx_buf, sizeof(tx_buf));
span_writer out(tx_buf);
size_t resp_len = it->second(msg->message_id, msg->payload, out);
if (resp_len > 0) {
if (resp_len > usb.tx.free()) {
span_writer err_out(tx_buf, sizeof(tx_buf));
span_writer err_out(tx_buf);
size_t err_len = encode_response_into(err_out, msg->message_id,
DeviceError{2, "response too large: " + std::to_string(resp_len)});
usb.send(std::span<const uint8_t>{tx_buf, err_len});
usb.send(std::span<const uint8_t>{tx_buf.data(), err_len});
} else {
usb.send(std::span<const uint8_t>{tx_buf, resp_len});
usb.send(std::span<const uint8_t>{tx_buf.data(), resp_len});
}
}
}

View File

@@ -139,9 +139,7 @@ static void handle_arp(const uint8_t* frame, size_t len) {
send_raw(&reply, sizeof(reply));
}
static uint8_t tx_buf[1514];
static void handle_udp(const uint8_t* frame, size_t len) {
static void handle_udp(const uint8_t* frame, size_t len, span_writer &tx) {
if (len < sizeof(udp_header)) return;
auto& pkt = *reinterpret_cast<const udp_header*>(frame);
@@ -156,14 +154,14 @@ static void handle_udp(const uint8_t* frame, size_t len) {
auto* payload = frame + sizeof(udp_header);
size_t payload_len = udp_len - 8;
span_writer resp(tx_buf + sizeof(udp_header), sizeof(tx_buf) - sizeof(udp_header));
auto resp = tx.subspan(sizeof(udp_header));
size_t resp_len = msg_handler(std::span<const uint8_t>{payload, payload_len}, resp);
if (resp_len == 0) return;
size_t ip_total = 20 + 8 + resp_len;
size_t reply_len = sizeof(eth_header) + ip_total;
auto& rip = *reinterpret_cast<ipv4_header*>(tx_buf);
auto& rip = *reinterpret_cast<ipv4_header*>(tx.data());
rip.eth.dst = pkt.ip.eth.src;
rip.eth.src = state.mac;
rip.eth.ethertype = ETH_IPV4;
@@ -179,16 +177,16 @@ static void handle_udp(const uint8_t* frame, size_t len) {
rip.dst = pkt.ip.src;
rip.checksum = ip_checksum(rip.ip_start(), 20);
auto& rudp = *reinterpret_cast<udp_header*>(tx_buf);
auto& rudp = *reinterpret_cast<udp_header*>(tx.data());
rudp.src_port = PICOMAP_PORT;
rudp.dst_port = pkt.src_port;
rudp.length = __builtin_bswap16(8 + resp_len);
rudp.checksum = 0;
send_raw(tx_buf, reply_len);
send_raw(tx.data(), reply_len);
}
static void handle_icmp(const uint8_t* frame, size_t len) {
static void handle_icmp(const uint8_t* frame, size_t len, span_writer &tx) {
auto& ip = *reinterpret_cast<const ipv4_header*>(frame);
size_t ip_hdr_len = ip.ip_header_len();
size_t ip_total = ip.ip_total_len();
@@ -202,12 +200,11 @@ static void handle_icmp(const uint8_t* frame, size_t len) {
if (icmp_len < sizeof(icmp_echo)) return;
if (icmp.type != 8) return;
uint8_t reply_buf[1514];
size_t reply_len = sizeof(eth_header) + ip_total;
if (reply_len > sizeof(reply_buf)) return;
if (reply_len > tx.capacity()) return;
memcpy(reply_buf, frame, reply_len);
auto& rip = *reinterpret_cast<ipv4_header*>(reply_buf);
memcpy(tx.data(), frame, reply_len);
auto& rip = *reinterpret_cast<ipv4_header*>(tx.data());
rip.eth.dst = ip.eth.src;
rip.eth.src = state.mac;
rip.src = state.ip;
@@ -216,30 +213,30 @@ static void handle_icmp(const uint8_t* frame, size_t len) {
rip.checksum = 0;
rip.checksum = ip_checksum(rip.ip_start(), ip_hdr_len);
auto& ricmp = *reinterpret_cast<icmp_echo*>(reply_buf + sizeof(eth_header) + ip_hdr_len);
auto& ricmp = *reinterpret_cast<icmp_echo*>(tx.data() + sizeof(eth_header) + ip_hdr_len);
ricmp.type = 0;
ricmp.checksum = 0;
ricmp.checksum = ip_checksum(&ricmp, icmp_len);
send_raw(reply_buf, reply_len);
send_raw(tx.data(), reply_len);
}
static void handle_ipv4(const uint8_t* frame, size_t len) {
static void handle_ipv4(const uint8_t* frame, size_t len, span_writer &tx) {
if (len < sizeof(ipv4_header)) return;
auto& ip = *reinterpret_cast<const ipv4_header*>(frame);
if ((ip.ver_ihl >> 4) != 4) return;
switch (ip.protocol) {
case 1:
handle_icmp(frame, len);
handle_icmp(frame, len, tx);
break;
case 17:
handle_udp(frame, len);
handle_udp(frame, len, tx);
break;
}
}
static void process_frame(const uint8_t* frame, size_t len) {
static void process_frame(const uint8_t* frame, size_t len, span_writer &tx) {
if (len < sizeof(eth_header)) return;
auto& eth = *reinterpret_cast<const eth_header*>(frame);
@@ -250,7 +247,7 @@ static void process_frame(const uint8_t* frame, size_t len) {
handle_arp(frame, len);
break;
case ETH_IPV4:
handle_ipv4(frame, len);
handle_ipv4(frame, len, tx);
break;
}
}
@@ -289,13 +286,15 @@ void net_set_handler(net_handler handler) {
msg_handler = std::move(handler);
}
void net_poll() {
void net_poll(std::span<uint8_t> tx) {
if (!w6300::irq_pending) return;
w6300::irq_pending = false;
w6300::clear_interrupt(w6300::ik_int_all);
if (w6300::get_socket_recv_buf(raw_socket) == 0) return;
static uint8_t rx_buf[1518];
auto result = w6300::recv(raw_socket, std::span{rx_buf});
if (!result) return;
process_frame(rx_buf, *result);
while (w6300::get_socket_recv_buf(raw_socket) > 0) {
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);
}
}