Move G3 repeater policy tables and MQTT WebSocket buffers to PSRAM
This commit is contained in:
1 parent
a6d472ff9d
commit
74c03cb30f
15 files changed
+1856
-7
No files matched your search
@@ -40,7 +40,7 @@
|
||||
// the fork moves to Arduino core 3.x.
|
||||
//
|
||||
// Scope: this stops the heap corruption and NOTHING else. It does not make an
|
||||
// oversized response fail the handshake — v4.4's read loop also exits on
|
||||
// oversized response fail the handshake; v4.4's read loop also exits on
|
||||
// `header_len < WS_BUFFER_SIZE` going false, and then still accepts the
|
||||
// connection if "Sec-WebSocket-Accept:" was inside those first bytes (it comes
|
||||
// early, so it usually is). ws_connect() therefore returns success on a partial
|
||||
@@ -67,6 +67,11 @@
|
||||
#include "esp_transport.h"
|
||||
#include "esp_transport_ws.h"
|
||||
|
||||
#if defined(STATION_G3_ESP32) && defined(BOARD_HAS_PSRAM)
|
||||
#include "esp_heap_caps.h"
|
||||
#include "MQTTConnectionAdmission.h"
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_WS_BUFFER_SIZE
|
||||
#define CONFIG_WS_BUFFER_SIZE 1024
|
||||
#endif
|
||||
@@ -103,13 +108,30 @@ esp_transport_handle_t __wrap_esp_transport_ws_init(esp_transport_handle_t paren
|
||||
if (ws != nullptr && ws->buffer != nullptr) {
|
||||
// The buffer is untouched at this point (allocated moments ago inside
|
||||
// __real_esp_transport_ws_init), so a swap is safe.
|
||||
char* padded = (char*)malloc(CONFIG_WS_BUFFER_SIZE + 1);
|
||||
if (padded != nullptr) {
|
||||
free(ws->buffer);
|
||||
ws->buffer = padded;
|
||||
const size_t bytes = CONFIG_WS_BUFFER_SIZE + 1;
|
||||
#if defined(STATION_G3_ESP32) && defined(BOARD_HAS_PSRAM)
|
||||
// Upgrade request/response text is CPU-only. Retain this padded buffer
|
||||
// until the SDK destroys the transport; its ordinary free accepts PSRAM.
|
||||
char* padded = (char*)heap_caps_malloc(bytes, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
if (padded == nullptr) {
|
||||
const uint32_t caps = MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA;
|
||||
if (MQTTConnectionAdmission::canFallback(bytes,
|
||||
heap_caps_get_free_size(caps), heap_caps_get_largest_free_block(caps))) {
|
||||
padded = (char*)heap_caps_malloc(bytes, caps | MALLOC_CAP_8BIT);
|
||||
}
|
||||
}
|
||||
// On alloc failure keep the original buffer: same behavior as before
|
||||
// this fix, which is still strictly better than failing init here.
|
||||
#else
|
||||
char* padded = (char*)malloc(bytes);
|
||||
#endif
|
||||
if (padded == nullptr) {
|
||||
// Returning the original 1024-byte buffer would reintroduce the
|
||||
// overflow. This transport has not entered the client's list yet;
|
||||
// destroy releases its context/buffer without destroying its parent.
|
||||
esp_transport_destroy(t);
|
||||
return nullptr;
|
||||
}
|
||||
free(ws->buffer);
|
||||
ws->buffer = padded;
|
||||
}
|
||||
}
|
||||
return t;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <string.h>
|
||||
#include "FloodAdvertRtcPolicy.h"
|
||||
#include "FloodAdvertIramPolicy.h"
|
||||
#include "RepeaterPsramStorage.h"
|
||||
|
||||
#ifndef FLOOD_ADVERT_SOURCE_SLOTS
|
||||
#if defined(STM32_PLATFORM)
|
||||
@@ -89,6 +90,14 @@ protected:
|
||||
}
|
||||
#else
|
||||
FloodAdvertLimiter(Entry* entries, size_t capacity) : _entries(entries), _capacity(capacity) {}
|
||||
#if MESH_REPEATER_PSRAM_TABLES
|
||||
// The derived storage owner is initialized after this base. Bind its full
|
||||
// table before reset() or any limiter operation can inspect the entries.
|
||||
void bindStorage(Entry* entries) {
|
||||
if (!entries || reinterpret_cast<uintptr_t>(entries) % alignof(Entry)) abort();
|
||||
_entries = entries;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
FloodAdvertLimiter(const FloodAdvertLimiter&) = delete;
|
||||
FloodAdvertLimiter& operator=(const FloodAdvertLimiter&) = delete;
|
||||
@@ -196,6 +205,19 @@ public:
|
||||
public:
|
||||
StaticFloodAdvertLimiter(Entry* fast, Entry* slow)
|
||||
: FloodAdvertLimiter(_storage, Capacity, fast, slow) { reset(); }
|
||||
#elif MESH_REPEATER_PSRAM_TABLES
|
||||
static_assert(sizeof(Entry) == 140,
|
||||
"Update the advert PSRAM fallback budget in check_firmware_ram.py");
|
||||
struct Storage { Entry entries[Capacity]; };
|
||||
RepeaterPsramStorage<Storage> _storage;
|
||||
public:
|
||||
StaticFloodAdvertLimiter() : FloodAdvertLimiter(nullptr, Capacity) {
|
||||
// Exhaustion must stop startup rather than disabling advert protection
|
||||
// or reducing its source/hash history. No full internal array is retained.
|
||||
if (!_storage.begin("advert source history")) abort();
|
||||
bindStorage(_storage.data()->entries);
|
||||
reset();
|
||||
}
|
||||
#else
|
||||
Entry _storage[Capacity];
|
||||
public:
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef MESH_REPEATER_PSRAM_TABLES
|
||||
#if defined(STATION_G3_ESP32) && defined(ESP32_PLATFORM) && defined(BOARD_HAS_PSRAM) \
|
||||
&& defined(MESH_CLIENT_REPEATER_ONLY) && MESH_CLIENT_REPEATER_ONLY
|
||||
#define MESH_REPEATER_PSRAM_TABLES 1
|
||||
#else
|
||||
#define MESH_REPEATER_PSRAM_TABLES 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if MESH_REPEATER_PSRAM_TABLES != 0 && MESH_REPEATER_PSRAM_TABLES != 1
|
||||
#error "MESH_REPEATER_PSRAM_TABLES must be 0 or 1"
|
||||
#endif
|
||||
|
||||
#if MESH_REPEATER_PSRAM_TABLES
|
||||
#if !defined(ESP32_PLATFORM) || !defined(BOARD_HAS_PSRAM)
|
||||
#error "Repeater PSRAM tables require an ESP32 target with PSRAM support"
|
||||
#endif
|
||||
|
||||
#include <sdkconfig.h>
|
||||
#if !defined(CONFIG_IDF_TARGET_ESP32S3) || !CONFIG_IDF_TARGET_ESP32S3 \
|
||||
|| !defined(CONFIG_SPIRAM_BOOT_INIT) || !CONFIG_SPIRAM_BOOT_INIT \
|
||||
|| !defined(CONFIG_SPIRAM_USE_MALLOC) || !CONFIG_SPIRAM_USE_MALLOC
|
||||
#error "Repeater PSRAM tables require ESP32-S3 with boot-initialized PSRAM heaps"
|
||||
#endif
|
||||
|
||||
#include <esp_heap_caps.h>
|
||||
#include <helpers/MQTTConnectionAdmission.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <type_traits>
|
||||
|
||||
namespace mesh {
|
||||
|
||||
// Own only CPU data here. ISR state, locks, task stacks and DMA buffers retain
|
||||
// their existing internal storage. G3's pinned SDK initializes PSRAM-backed
|
||||
// heaps before global constructors, so tables can keep their startup lifetime.
|
||||
template<typename T> class RepeaterPsramStorage {
|
||||
static_assert(std::is_trivial<T>::value,
|
||||
"Repeater PSRAM tables require zero-initialized trivial data");
|
||||
T* value_ = nullptr;
|
||||
public:
|
||||
RepeaterPsramStorage() = default;
|
||||
~RepeaterPsramStorage() { if (value_) heap_caps_free(value_); }
|
||||
RepeaterPsramStorage(const RepeaterPsramStorage&) = delete;
|
||||
RepeaterPsramStorage& operator=(const RepeaterPsramStorage&) = delete;
|
||||
|
||||
bool begin(const char* label = "repeater table") {
|
||||
if (value_) return true;
|
||||
value_ = static_cast<T*>(heap_caps_calloc(
|
||||
1, sizeof(T), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
||||
if (!value_) {
|
||||
const uint32_t caps = MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA;
|
||||
if (MQTTConnectionAdmission::canFallback(sizeof(T),
|
||||
heap_caps_get_free_size(caps), heap_caps_get_largest_free_block(caps))) {
|
||||
value_ = static_cast<T*>(heap_caps_calloc(
|
||||
1, sizeof(T), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT));
|
||||
}
|
||||
}
|
||||
if (!value_) {
|
||||
fprintf(stderr, "Cannot allocate %s (%u bytes); full capacity is required\n",
|
||||
label, static_cast<unsigned>(sizeof(T)));
|
||||
}
|
||||
return value_ != nullptr;
|
||||
}
|
||||
|
||||
T* data() { return value_; }
|
||||
const T* data() const { return value_; }
|
||||
T& require(const char* label) {
|
||||
// Used only while binding the owner's references during construction.
|
||||
// Allocation never moves after pointers escape to routing or persistence.
|
||||
if (!begin(label)) abort();
|
||||
return *value_;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mesh
|
||||
#endif
|
||||
Reference in new issue
Block a user