Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions components/rtps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,22 @@ wire. The announced lease must comfortably exceed the announcement period
a remote lease cap below a peer's announcement period drops that peer while it
is alive.

A participant's GUID prefix on an ESP target is its MAC, the participant id,
the vendor id, the domain id and two incarnation bytes drawn when the Domain
is constructed (`rtps::Domain::guidIncarnation()`), so a board that reboots -
or an application that recreates its Domain - is a new participant to its
peers: what RTPS requires, since every writer of the restarted board numbers
its samples from 1 again and a peer still holding the old incarnation would
ignore them until its lease ran out. The previous incarnation is kept in RTC
memory (`.noinit` RAM on chips without it), which survives a software reset,
and is disposed, as that incarnation, on the first announce cycles, so such
peers forget it at once (after a power-on there is nothing to dispose; the
old incarnation then expires with its lease). A peer
that does reuse a prefix across a restart - this engine's boards never do, but
a stack that derives its prefix from stable hardware identifiers would - is
recognised by the sequence number of its announcements falling far below what
it had reached, and rediscovered on the spot.

### Per-limit capacity overrides

Every capacity cap in the profile headers can be raised (or lowered)
Expand Down
11 changes: 9 additions & 2 deletions components/rtps/include/rtps/config_esp32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ const VendorId_t VENDOR_ID = {13, 37};
// Fallback only: Domain::generateGuidPrefix() builds the prefix from the
// Ethernet or Wi-Fi MAC (bytes 0-5) and uses this template only when neither
// MAC can be read. Bytes 6-9 are always overwritten with the participant id,
// VENDOR_ID and DOMAIN_ID, so two boards that both fall back share bytes 0-5
// and 10-11 and would collide on a domain.
// VENDOR_ID and DOMAIN_ID, so two boards that both fall back share bytes 0-9
// and collide on a domain unless their incarnations differ (2^-16 odds of
// matching). Bytes 10-11 are the boot's incarnation
// (Domain::guidIncarnation(), a nonce drawn once per boot): a board that
// reboots is a NEW participant to its peers, which is what RTPS requires - a
// peer that still held the old incarnation would otherwise ignore the
// restarted writers (their sequence numbers start at 1 again) until the lease
// ran out. The previous boot's incarnation survives a software reset in RTC
// memory and is disposed before the first announcement (SPDPAgent).
const GuidPrefix_t BASE_GUID_PREFIX{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13};

// DDS domain id (the ROS_DOMAIN_ID equivalent; 0..232 with UDP). The port base
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class ParticipantProxyData : public espp::BaseComponent {
// When this proxy entered the remote-participant pool (eviction prefers the
// oldest unused one and spares one still completing endpoint discovery).
std::chrono::time_point<std::chrono::steady_clock> m_discoveredTimestamp;
// The highest sequence number seen from this participant's SPDP writer. A
// later announcement numbered far below it (SPDPAgent::isRestart) means the
// peer restarted with the same GUID prefix - its writers all start again at
// 1 - and must be rediscovered, not refreshed.
SequenceNumber_t m_lastSpdpSn{0, 0};
void reset();

/// Parses the SPDP PL_CDR parameter list starting at `buffer`'s current
Expand Down
34 changes: 33 additions & 1 deletion components/rtps/include/rtps/discovery/SPDPAgent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ class SPDPAgent : public espp::BaseComponent {
/// scheduler at SPDP_RESEND_PERIOD_MS cadence instead of a dedicated
/// broadcast thread.
void announce();
/// `announced` is the sequence number of the announcement just received from
/// a known participant whose highest so far is `last`. A number far below
/// the highest (at most half of it, once the peer has announced at least
/// three times) is not a reordered or duplicated datagram but a peer that
/// restarted with the same GUID prefix and numbers its new SPDP writer from
/// 1 again; such a peer is forgotten and rediscovered (its SEDP and user
/// writers restarted too, and the stateful readers' proxies would keep
/// waiting for the old numbers). Edges: a peer seen fewer than three times
/// is never judged (a reordered second announcement must not tear down a
/// participant just discovered); a restarted peer whose first
/// announcements were lost is caught as long as its number is still at most
/// half of what was seen before, else its lease decides; number 0 never
/// qualifies (not a valid RTPS sequence number). This engine's own boards
/// never reuse a prefix (Domain::guidIncarnation), so this guards against
/// stacks that do. Security: a spoofed low-numbered announcement can tear
/// down and re-add a healthy peer - no more than a spoofed dispose, which
/// the protocol has always allowed from any multicast sender.
static bool isRestart(const SequenceNumber_t &announced, const SequenceNumber_t &last);
std::recursive_mutex m_mutex;

private:
Expand All @@ -94,6 +112,9 @@ class SPDPAgent : public espp::BaseComponent {
/// else GUIDPREFIX_UNKNOWN. For a dispose that carries the key in its
/// payload rather than inline.
static GuidPrefix_t participantPrefixInPayload(const uint8_t *data, DataSize_t size);
/// Parse the announcement in m_inputBuffer into m_proxyDataBuffer. The
/// parser stops at the GUID of a participant that is already known.
bool parseAnnouncement();
/// Participants that disposed themselves lately: the prefix, the dispose's
/// sequence number and when it came. An announcement of such a prefix with
/// a lower number is a delayed or duplicated copy from before the dispose
Expand All @@ -112,7 +133,18 @@ class SPDPAgent : public espp::BaseComponent {
size_t m_nextDisposed = 0;
void rememberDispose(const GuidPrefix_t &prefix, const SequenceNumber_t &sn);
bool isFromBeforeDispose(const GuidPrefix_t &prefix, const SequenceNumber_t &sn) const;
void processProxyData();
/// Send the dispose of the participant we were in the previous boot, AS that
/// participant: an RTPS message whose header prefix and SPDP writer are the
/// old incarnation's, with its participant GUID as key hash + payload and
/// a sequence number above anything it could have sent (every stack's SPDP
/// reader takes a participant's own dispose; one from another writer
/// naming it is refused by Cyclone DDS, Fast DDS and this engine alike).
/// Sent on the first PREVIOUS_INCARNATION_DISPOSES announce cycles - best
/// effort, multicast, a lost datagram costs a peer one lease.
void sendPreviousIncarnationDispose();
static constexpr uint8_t PREVIOUS_INCARNATION_DISPOSES = 3;
uint8_t m_previousIncarnationDisposesLeft = 0;
void processProxyData(const SequenceNumber_t &announcementSn);
bool addProxiesForBuiltInEndpoints();

void addInlineQos(CdrWriter &writer);
Expand Down
24 changes: 24 additions & 0 deletions components/rtps/include/rtps/entities/Domain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Author: i11 - Embedded Software, RWTH Aachen University
#include "task.hpp"
#include <atomic>
#include <mutex>
#include <optional>
#include <rtps/common/Locator.hpp>

namespace rtps {
Expand Down Expand Up @@ -98,6 +99,26 @@ class Domain : public espp::BaseComponent {
void stop();

Participant *createParticipant();

/// Bytes 10-11 of every GUID prefix this Domain generates. On ESP targets
/// a nonce drawn when the Domain is constructed, so a board that reboots -
/// or an application that recreates its Domain - comes back as a NEW
/// participant to every peer (RTPS requires a GUID never to be reused; a
/// peer that still holds the old incarnation would otherwise ignore the
/// restarted writers, whose sequence numbers start at 1 again, until the
/// lease ran out). Host builds return 0: their prefixes are random per
/// process already (or the configured template, for tests). The static
/// form reports the most recently constructed Domain's value.
uint16_t guidIncarnation() const { return m_incarnation; }
static uint16_t currentGuidIncarnation();
/// The incarnation before this Domain's, when known: ESP targets keep the
/// last one in RTC memory across a software reset (not a power cycle), so
/// it is the previous boot's after a reboot and the previous Domain's after
/// a recreation. The SPDP agent disposes that incarnation's participant on
/// its first announce cycles, so peers drop it at once. nullopt on host
/// builds and after a power-on.
std::optional<uint16_t> previousGuidIncarnation() const { return m_previousIncarnation; }
static std::optional<uint16_t> currentPreviousGuidIncarnation();
Writer *createWriter(Participant &part, const char *topicName, const char *typeName,
bool reliable, bool enforceUnicast = false,
const EndpointOptions &options = {});
Expand Down Expand Up @@ -235,6 +256,9 @@ class Domain : public espp::BaseComponent {

bool m_initComplete = false;
bool m_transportSetupOk = true;
uint16_t m_incarnation = 0;
std::optional<uint16_t> m_previousIncarnation{};
void drawGuidIncarnation();
std::recursive_mutex m_mutex;

void receiveCallback(const PacketInfo &packet);
Expand Down
19 changes: 19 additions & 0 deletions components/rtps/include/rtps/entities/Participant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ class Participant : public espp::BaseComponent {
public:
GuidPrefix_t m_guidPrefix;
ParticipantId_t m_participantId;
/// The GUID prefix this participant had in the previous boot, when the
/// Domain could tell (ESP targets keep the prefix's incarnation bytes
/// across a software reset: Domain::generateGuidPrefix). The SPDP agent
/// disposes it before the first announcement, so peers forget the old
/// incarnation at once instead of when its lease runs out.
bool m_hasPreviousIncarnation = false;
GuidPrefix_t m_previousIncarnationPrefix{};
Ip4AddressBytes m_localIpAddress{{0, 0, 0, 0}};
/// Prefix length (CIDR bits) of the local network, used to tell which of a
/// peer's announced unicast locators is reachable (see
Expand Down Expand Up @@ -137,6 +144,18 @@ class Participant : public espp::BaseComponent {

const ParticipantProxyData *findRemoteParticipant(const GuidPrefix_t &prefix);
void refreshRemoteParticipantLiveliness(const GuidPrefix_t &prefix);
/// What an SPDP announcement from `prefix`, numbered `sn`, means for the
/// remote participant we may know by that prefix - decided and recorded in
/// one step under the participant mutex (the lease sweep removes entries
/// concurrently).
enum class AnnouncementVerdict : uint8_t {
Unknown, ///< no such participant: discover it
Known, ///< a known participant; its lease and highest SPDP number were refreshed
Restart, ///< a known participant restarted with this prefix (SPDPAgent::isRestart):
///< forget it and discover it afresh; nothing was refreshed
};
AnnouncementVerdict noteRemoteParticipantAnnouncement(const GuidPrefix_t &prefix,
const SequenceNumber_t &sn);
uint32_t getRemoteParticipantCount();
MessageReceiver *getMessageReceiver();
bool checkAndResetHeartbeats();
Expand Down
7 changes: 7 additions & 0 deletions components/rtps/include/rtps/entities/StatelessWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ class StatelessWriter : public Writer {

EsppTransport *m_transport;

public:
/// The transport this writer sends on and the source port it sends from
/// (SPDPAgent sends the previous incarnation's dispose through them).
EsppTransport *getTransport() const { return m_transport; }
Ip4Port_t getSourcePort() const { return m_srcPort; }

private:
SimpleHistoryCache<Config::HISTORY_SIZE_STATELESS> m_history;
};

Expand Down
2 changes: 2 additions & 0 deletions components/rtps/interop/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ touched.
| golden | wire-format bytes unchanged (see `pc/tests/rtps_golden.cpp`) |
| locator_subnet | of the unicast locators a peer on several networks announces (SEDP and SPDP), the one on our subnet is kept (see `pc/tests/rtps_locator_subnet.cpp`) |
| sedp_inline_qos_dispose | a peer's dispose of an endpoint (SEDP) or of its participant (SPDP) - a DATA whose inline QoS carries `PID_STATUS_INFO` + `PID_KEY_HASH`, as Fast DDS, Cyclone DDS and espp write it, in each of the three forms - unmatches the endpoint / forgets the participant at once instead of at lease expiry (see `pc/tests/rtps_sedp_inline_qos_dispose.cpp`) |
| participant_restart | a peer that restarts with the GUID prefix its peers still hold (every writer numbers from 1 again) is rediscovered at once: an announcement numbered far below the peer's highest (`SPDPAgent::isRestart`) forgets and rediscovers it (a single reordered or duplicated announcement does not); a rebooted board of this engine instead comes back with new incarnation bytes and first disposes its previous incarnation as that incarnation (old prefix, its SPDP writer, a number above anything it sent), which every stack honours as the participant's own dispose - the old one goes at once, a stale announcement of it is ignored, a dispose naming a participant from any other prefix is still refused (see `pc/tests/rtps_participant_restart.cpp`) |
| sedp_history_ring, sedp_history_overflow | the SEDP writers never overwrite a live endpoint announcement when their history is full: a sent dispose is given up first, else the announcement is refused and the endpoint creation fails loudly (the ring policy on a static ring, `pc/tests/rtps_sedp_history_ring.cpp`, a unit that links no engine object); a refused creation gives its pooled endpoint back, and an endpoint whose announcement is gone can still be deleted with its slot reused (`pc/tests/rtps_sedp_history_overflow.cpp`) |
| facade_matched_counts | `RtpsParticipant::matched_writers()` / `matched_readers()` over a loopback: one remote writer counts once across two local readers on the topic, and removing the writer (which announces the SEDP dispose) brings the count to zero within a couple of seconds, not at lease expiry (`pc/tests/rtps_facade_matched_counts.cpp`) |
| gap_before_data | a reliable reader applies a GAP only for sequence numbers the writer has announced (HEARTBEAT lastSN or a DATA): Fast DDS sends its SEDP readers a GAP for the next number after an endpoint deletion and then writes that number for the next announcement, which used to leave a publisher-churning peer unmatched after its first deletion (`pc/tests/rtps_gap_before_data.cpp`; the churn case is the live check) |
Expand Down Expand Up @@ -84,6 +85,7 @@ cd .. && ./run_qemu.sh
| device_boot, device_ping | the engine comes up on lwIP over the emulated OpenCores MAC |
| device_pub->ros2_echo, ros2_pub->device_sub (+ best-effort pair) | delivery both ways, FastDDS and CycloneDDS |
| ros2_pub->device_sub_liveliness | samples still arriving 24-34 s in, past the remote lease |
| restart_fastdds_rediscovered_by_ros2_sub, restart_cyclone_rediscovered_by_ros2_sub, restart_device_sub_receives, restart_fastdds_old_incarnation_dropped, restart_cyclone_old_incarnation_dropped, restart_previous_incarnation_remembered | the device restarts its participant on one "RESTART" trigger (it recreates its Domain - what a board's reboot looks like to its peers; a software reset of QEMU's emulated chip panics inside ESP-IDF's startup, so the chip is not rebooted). The restart is done ONCE for the whole run, with BOTH a FastDDS peer and a CycloneDDS peer watching that single restart, so each stack is proven to honour the dispose without a second recreate: the espp participant is recreated in-process to "reboot", which fragments internal RAM, so the first recreate is reliable and a second is not yet (a teardown/fragmentation follow-up); the espp host engine's own side is covered by `pc/tests/rtps_participant_restart.cpp`. Both a ROS 2 subscriber per stack and a ROS 2 publisher stay up. The device announces a 30 s lease for itself in `sdkconfig.ci.defaults`, so the old incarnation cannot expire during the restart: each stack's subscriber must receive the restarted publisher's first samples ("qemu 0".."qemu 9") and the rebooted device the ROS 2 publisher's samples within seconds. The GUID prefix carries a per-Domain incarnation (bytes 10-11, `Domain::guidIncarnation`); the previous one is kept in RTC memory (`RTPSTEST incarnation cur= prev=`, `restart_previous_incarnation_remembered`) and disposed as that participant on the first announce cycles, so each RMW daemon's count of publishers on the device's topic drops back to one within seconds instead of after the 30 s lease (`restart_fastdds_old_incarnation_dropped`, `restart_cyclone_old_incarnation_dropped`) |
| many_participants_late_joiner | twelve unrelated participants first, the needed one last, against the pool raised to 24 by `CONFIG_RTPS_LIMIT_SPDP_MAX_NUMBER_FOUND_PARTICIPANTS` |
| ros2_pub->device_sub_frag_30k | 30 KB samples reassembled from DATA_FRAG over lwIP |
| participant_pool_overflow_recovers | 28 participants against a pool of 24 (and proxy pools of 20), then all leave: discovery keeps going, every one is forgotten after the capped lease, the next publisher is received |
Expand Down
Loading
Loading