rustdds 0.13.1

Native Rust DDS implementation with RTPS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! Interface-aware transmit-locator selection.
//!
//! See `src/rtps/transmit_design.md` for the full design rationale.
//!
//! The goal of these types is to let a writer send exactly one datagram per
//! distinct destination "route" instead of blindly sending to every locator a
//! remote advertised on every local interface.
//!
//! Terminology:
//! - [`InterfaceSelector`] identifies a *local* egress interface.
//! - [`SendRoute`] is the resolved destination for one remote reader: at most
//!   one unicast locator and at most one interface-tagged multicast locator.
//! - [`RouteKey`] is the de-duplication key: two readers that resolve to the
//!   same `RouteKey` are served by a single datagram.
//! - [`InterfaceObservations`] records, per remote participant, on which local
//!   interface(s) we have actually seen its traffic arrive. This is the primary
//!   input to route selection.
//!
//! Safety guardrail: when we do not have enough information to narrow a
//! remote's route confidently, [`SendRoute::fallback`] is set and the writer
//! falls back to the legacy "send to every advertised locator on every
//! interface" path, so reachability is never reduced.

use std::{
  collections::BTreeMap,
  net::{IpAddr, SocketAddr},
  time::{Duration, Instant},
};

use crate::structure::{guid::GuidPrefix, locator::Locator};

/// Hysteresis margin for switching a remote participant's chosen multicast
/// egress interface. Once an interface is chosen it stays chosen until we have
/// not heard from it for at least this long *and* the participant's traffic is
/// arriving on a different interface. This keeps the route stable across
/// occasional stray packets on a secondary interface.
pub(crate) const STICKY_SWITCH_MARGIN: Duration = Duration::from_secs(30);

/// Identifies a local network interface to use as the egress for multicast.
///
/// Modelled as the interface's IP address for now; the enum leaves room for an
/// OS interface index variant should the IP prove insufficient (e.g. multiple
/// interfaces sharing an address, or IPv6 scope handling).
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum InterfaceSelector {
  Ip(IpAddr),
}

/// The resolved send destination for a single remote reader.
///
/// `fallback == true` means "we could not narrow this confidently; use the
/// legacy all-locators/all-interfaces path". In that case `unicast`/`multicast`
/// should be ignored by the sender.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SendRoute {
  pub unicast: Option<Locator>,
  pub multicast: Option<(Locator, InterfaceSelector)>,
  pub fallback: bool,
}

impl SendRoute {
  /// A route that instructs the sender to use the legacy behavior.
  pub fn fallback() -> Self {
    Self {
      unicast: None,
      multicast: None,
      fallback: true,
    }
  }
}

impl Default for SendRoute {
  fn default() -> Self {
    // Until a route is resolved, behave exactly like today.
    Self::fallback()
  }
}

/// De-duplication key for a concrete outbound datagram destination.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RouteKey {
  Unicast(Locator),
  Multicast(Locator, InterfaceSelector),
}

/// One local interface's observation record for a remote participant.
#[derive(Clone, Debug)]
pub struct InterfaceObservation {
  pub last_seen: Instant,
  /// Number of packets observed on this interface. Retained for diagnostics and
  /// possible future selection policies; the current sticky heuristic decides
  /// switches purely on `last_seen`.
  #[allow(dead_code)]
  pub count: u64,
  pub source: Option<SocketAddr>,
}

/// What we have observed about how to reach a single remote participant.
#[derive(Clone, Debug, Default)]
pub struct ObservedRoutes {
  /// Local interfaces on which we have seen this participant's traffic.
  by_iface: BTreeMap<InterfaceSelector, InterfaceObservation>,
  /// Most recent source socket address seen for this participant, regardless of
  /// whether the receiving interface could be determined.
  last_source: Option<SocketAddr>,
  /// The interface currently chosen for multicast egress to this participant.
  /// Updated with hysteresis (see [`STICKY_SWITCH_MARGIN`]) so it does not flip
  /// on a single stray packet arriving on another interface.
  current_interface: Option<InterfaceSelector>,
}

impl ObservedRoutes {
  fn record(&mut self, iface: Option<InterfaceSelector>, source: SocketAddr) {
    self.record_at(iface, source, Instant::now(), STICKY_SWITCH_MARGIN);
  }

  /// Testable core of [`Self::record`]: `now` and `margin` are injected so the
  /// hysteresis can be exercised deterministically.
  fn record_at(
    &mut self,
    iface: Option<InterfaceSelector>,
    source: SocketAddr,
    now: Instant,
    margin: Duration,
  ) {
    self.last_source = Some(source);
    if let Some(iface) = iface {
      self
        .by_iface
        .entry(iface)
        .and_modify(|o| {
          o.last_seen = now;
          o.count = o.count.saturating_add(1);
          o.source = Some(source);
        })
        .or_insert(InterfaceObservation {
          last_seen: now,
          count: 1,
          source: Some(source),
        });

      // Sticky choice: a switch is only ever triggered by receiving on a
      // non-current interface (the challenger, whose `last_seen` is `now`).
      // Displace the current interface only if we have not heard from it for at
      // least `margin`; otherwise the current interface stays chosen. A current
      // interface that keeps receiving traffic is thus never displaced.
      match self.current_interface {
        None => self.current_interface = Some(iface),
        Some(cur) if cur == iface => { /* refreshed the current choice; keep */ }
        Some(cur) => {
          let displace = self
            .by_iface
            .get(&cur)
            .is_none_or(|o| now.saturating_duration_since(o.last_seen) >= margin);
          if displace {
            self.current_interface = Some(iface);
          }
        }
      }
    }
  }

  /// The most recently observed source address, if any.
  pub fn last_source(&self) -> Option<SocketAddr> {
    self.last_source
  }

  /// The local interface currently chosen to reach this participant.
  ///
  /// This is sticky: it stays on the previously chosen interface until the
  /// participant's traffic has been arriving on a different interface and the
  /// old one has been silent for at least [`STICKY_SWITCH_MARGIN`] (see
  /// [`Self::record_at`]). `None` if we have never determined a receiving
  /// interface for this participant.
  pub fn best_interface(&self) -> Option<InterfaceSelector> {
    self.current_interface
  }

  /// Number of distinct local interfaces this participant has been seen on.
  #[cfg(test)]
  pub fn interface_count(&self) -> usize {
    self.by_iface.len()
  }
}

/// Per-remote-participant record of observed receive interfaces / source
/// addresses. Populated by the message receiver, consumed by route resolution.
#[derive(Debug, Default)]
pub struct InterfaceObservations {
  by_participant: BTreeMap<GuidPrefix, ObservedRoutes>,
}

impl InterfaceObservations {
  pub fn new() -> Self {
    Self::default()
  }

  /// Record that a packet from `prefix` arrived from `source`, on local
  /// interface `iface` (if it could be determined).
  pub fn record(
    &mut self,
    prefix: GuidPrefix,
    iface: Option<InterfaceSelector>,
    source: SocketAddr,
  ) {
    self
      .by_participant
      .entry(prefix)
      .or_default()
      .record(iface, source);
  }

  pub fn get(&self, prefix: GuidPrefix) -> Option<&ObservedRoutes> {
    self.by_participant.get(&prefix)
  }

  pub fn remove(&mut self, prefix: GuidPrefix) {
    self.by_participant.remove(&prefix);
  }
}

/// Strategy for turning advertised locators + observations into a
/// [`SendRoute`].
///
/// Kept as a trait so the heuristic can evolve (or be swapped) without touching
/// the transmit path.
pub trait RouteSelector {
  fn select(
    &self,
    advertised_unicast: &[Locator],
    advertised_multicast: &[Locator],
    observed: Option<&ObservedRoutes>,
    local_multicast_ifaces: &[InterfaceSelector],
  ) -> SendRoute;
}

/// Conservative default policy.
///
/// - Without any observation for the remote, returns [`SendRoute::fallback`].
/// - With an observation, chooses the observed interface for multicast (only if
///   it is one of our local multicast interfaces) and the advertised unicast
///   locator that matches the observed source address.
/// - Whenever narrowing would risk dropping reachability (e.g. a multicast
///   locator is advertised but its interface cannot be determined, or several
///   unicast candidates cannot be disambiguated), it returns the fallback route
///   rather than guessing.
#[derive(Clone, Copy, Debug, Default)]
pub struct DefaultRouteSelector;

fn first_reachable_udp(locators: &[Locator]) -> Option<Locator> {
  locators
    .iter()
    .copied()
    .find(|l| l.is_udp() && !l.is_loopback())
}

impl RouteSelector for DefaultRouteSelector {
  fn select(
    &self,
    advertised_unicast: &[Locator],
    advertised_multicast: &[Locator],
    observed: Option<&ObservedRoutes>,
    local_multicast_ifaces: &[InterfaceSelector],
  ) -> SendRoute {
    // No origin knowledge -> cannot narrow safely.
    let Some(obs) = observed else {
      return SendRoute::fallback();
    };

    let mc_advertised = first_reachable_udp(advertised_multicast);

    // Pick the egress interface only if it is genuinely one of ours.
    let chosen_iface = obs
      .best_interface()
      .filter(|iface| local_multicast_ifaces.contains(iface));

    let multicast = match (mc_advertised, chosen_iface) {
      (Some(mc), Some(iface)) => Some((mc, iface)),
      _ => None,
    };

    // If the remote advertises multicast but we cannot bind it to a local
    // interface, do not silently drop it: fall back to the legacy path.
    if mc_advertised.is_some() && multicast.is_none() {
      return SendRoute::fallback();
    }

    let unicast = select_unicast(advertised_unicast, obs);

    // Nothing we can confidently send to -> fallback (also covers the case of
    // several ambiguous unicast candidates and no multicast).
    if unicast.is_none() && multicast.is_none() {
      return SendRoute::fallback();
    }

    SendRoute {
      unicast,
      multicast,
      fallback: false,
    }
  }
}

/// Choose a single unicast locator, or `None` if the choice is ambiguous.
fn select_unicast(advertised_unicast: &[Locator], obs: &ObservedRoutes) -> Option<Locator> {
  let candidates: Vec<Locator> = advertised_unicast
    .iter()
    .copied()
    .filter(|l| l.is_udp() && !l.is_loopback())
    .collect();

  match candidates.len() {
    0 => None,
    1 => Some(candidates[0]),
    _ => {
      // Multiple advertised addresses: only pick one if the observed source
      // address disambiguates it. Otherwise stay ambiguous (caller falls back).
      let source_ip = obs.last_source().map(|sa| sa.ip());
      source_ip.and_then(|ip| {
        candidates
          .iter()
          .copied()
          .find(|l| SocketAddr::from(*l).ip() == ip)
      })
    }
  }
}

#[cfg(test)]
mod tests {
  use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};

  use super::*;
  use crate::structure::guid::GuidPrefix;

  fn udp(ip: [u8; 4], port: u16) -> Locator {
    Locator::UdpV4(SocketAddrV4::new(
      Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3]),
      port,
    ))
  }

  fn sockaddr(ip: [u8; 4], port: u16) -> SocketAddr {
    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3])), port)
  }

  fn iface(ip: [u8; 4]) -> InterfaceSelector {
    InterfaceSelector::Ip(IpAddr::V4(Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3])))
  }

  #[test]
  fn no_observation_is_fallback() {
    let sel = DefaultRouteSelector;
    let route = sel.select(&[udp([10, 0, 0, 5], 7410)], &[], None, &[]);
    assert!(route.fallback);
  }

  #[test]
  fn single_unicast_with_observation_narrows() {
    let sel = DefaultRouteSelector;
    let mut obs = ObservedRoutes::default();
    obs.record(Some(iface([10, 0, 0, 1])), sockaddr([10, 0, 0, 5], 7410));
    let route = sel.select(
      &[udp([10, 0, 0, 5], 7410)],
      &[],
      Some(&obs),
      &[iface([10, 0, 0, 1])],
    );
    assert!(!route.fallback);
    assert_eq!(route.unicast, Some(udp([10, 0, 0, 5], 7410)));
    assert_eq!(route.multicast, None);
  }

  #[test]
  fn multicast_tagged_with_observed_interface() {
    let sel = DefaultRouteSelector;
    let mut obs = ObservedRoutes::default();
    obs.record(Some(iface([10, 0, 0, 1])), sockaddr([10, 0, 0, 5], 7410));
    let mc = udp([239, 255, 0, 1], 7401);
    let route = sel.select(
      &[udp([10, 0, 0, 5], 7410)],
      &[mc],
      Some(&obs),
      &[iface([10, 0, 0, 1])],
    );
    assert_eq!(route.multicast, Some((mc, iface([10, 0, 0, 1]))));
    assert!(!route.fallback);
  }

  #[test]
  fn advertised_multicast_but_unknown_interface_falls_back() {
    let sel = DefaultRouteSelector;
    let mut obs = ObservedRoutes::default();
    // Observation without a resolvable local interface (unicast only source).
    obs.record(None, sockaddr([10, 0, 0, 5], 7410));
    let mc = udp([239, 255, 0, 1], 7401);
    let route = sel.select(
      &[udp([10, 0, 0, 5], 7410)],
      &[mc],
      Some(&obs),
      &[iface([10, 0, 0, 1])],
    );
    assert!(route.fallback);
  }

  #[test]
  fn ambiguous_multi_unicast_without_source_match_falls_back() {
    let sel = DefaultRouteSelector;
    let mut obs = ObservedRoutes::default();
    obs.record(None, sockaddr([172, 16, 0, 9], 7410));
    // Two advertised addresses, neither matching the observed source IP.
    let route = sel.select(
      &[udp([10, 0, 0, 5], 7410), udp([192, 168, 1, 5], 7410)],
      &[],
      Some(&obs),
      &[],
    );
    assert!(route.fallback);
  }

  #[test]
  fn multi_unicast_disambiguated_by_source() {
    let sel = DefaultRouteSelector;
    let mut obs = ObservedRoutes::default();
    obs.record(Some(iface([10, 0, 0, 1])), sockaddr([192, 168, 1, 5], 7410));
    let route = sel.select(
      &[udp([10, 0, 0, 5], 7410), udp([192, 168, 1, 5], 7410)],
      &[],
      Some(&obs),
      &[iface([10, 0, 0, 1])],
    );
    assert_eq!(route.unicast, Some(udp([192, 168, 1, 5], 7410)));
    assert!(!route.fallback);
  }

  #[test]
  fn route_key_dedup() {
    use std::collections::BTreeSet;
    let mut set = BTreeSet::new();
    let k1 = RouteKey::Multicast(udp([239, 255, 0, 1], 7401), iface([10, 0, 0, 1]));
    let k2 = RouteKey::Multicast(udp([239, 255, 0, 1], 7401), iface([10, 0, 0, 1]));
    let k3 = RouteKey::Unicast(udp([10, 0, 0, 5], 7410));
    assert!(set.insert(k1));
    assert!(!set.insert(k2)); // duplicate
    assert!(set.insert(k3));
    assert_eq!(set.len(), 2);
  }

  #[test]
  fn observations_sticky_within_margin() {
    // Recording A, A, then B microseconds apart (default 30s margin) must NOT
    // flip the chosen interface: A was seen well within the margin.
    let mut obs = InterfaceObservations::new();
    let p = GuidPrefix::UNKNOWN;
    obs.record(p, Some(iface([10, 0, 0, 1])), sockaddr([10, 0, 0, 5], 7410));
    obs.record(p, Some(iface([10, 0, 0, 1])), sockaddr([10, 0, 0, 5], 7410));
    obs.record(
      p,
      Some(iface([192, 168, 1, 1])),
      sockaddr([192, 168, 1, 5], 7410),
    );
    let recorded = obs.get(p).unwrap();
    assert_eq!(recorded.interface_count(), 2);
    // Sticky: the first-chosen interface stays.
    assert_eq!(recorded.best_interface(), Some(iface([10, 0, 0, 1])));
  }

  #[test]
  fn sticky_first_observation_is_chosen() {
    let mut obs = ObservedRoutes::default();
    let t0 = Instant::now();
    obs.record_at(
      Some(iface([10, 0, 0, 1])),
      sockaddr([10, 0, 0, 5], 7410),
      t0,
      Duration::from_secs(30),
    );
    assert_eq!(obs.best_interface(), Some(iface([10, 0, 0, 1])));
  }

  #[test]
  fn sticky_does_not_switch_within_margin() {
    let mut obs = ObservedRoutes::default();
    let margin = Duration::from_secs(30);
    let t0 = Instant::now();
    obs.record_at(
      Some(iface([10, 0, 0, 1])),
      sockaddr([10, 0, 0, 5], 7410),
      t0,
      margin,
    );
    // Challenger arrives before the current interface has been silent for the
    // full margin -> no switch.
    obs.record_at(
      Some(iface([192, 168, 1, 1])),
      sockaddr([192, 168, 1, 5], 7410),
      t0 + Duration::from_secs(29),
      margin,
    );
    assert_eq!(obs.best_interface(), Some(iface([10, 0, 0, 1])));
  }

  #[test]
  fn sticky_switches_past_margin() {
    let mut obs = ObservedRoutes::default();
    let margin = Duration::from_secs(30);
    let t0 = Instant::now();
    obs.record_at(
      Some(iface([10, 0, 0, 1])),
      sockaddr([10, 0, 0, 5], 7410),
      t0,
      margin,
    );
    // Current interface has been silent for >= margin when the challenger is
    // heard -> switch.
    obs.record_at(
      Some(iface([192, 168, 1, 1])),
      sockaddr([192, 168, 1, 5], 7410),
      t0 + Duration::from_secs(30),
      margin,
    );
    assert_eq!(obs.best_interface(), Some(iface([192, 168, 1, 1])));
  }

  #[test]
  fn sticky_busy_current_never_displaced() {
    let mut obs = ObservedRoutes::default();
    let margin = Duration::from_secs(30);
    let t0 = Instant::now();
    let current = iface([10, 0, 0, 1]);
    let challenger = iface([192, 168, 1, 1]);
    obs.record_at(Some(current), sockaddr([10, 0, 0, 5], 7410), t0, margin);
    // Interleave: the current interface keeps receiving every 10s, while an
    // intermittent challenger also shows up. The current interface is never
    // silent for a full margin, so it is never displaced.
    for k in 1..=6 {
      let t = t0 + Duration::from_secs(10 * k);
      obs.record_at(
        Some(challenger),
        sockaddr([192, 168, 1, 5], 7410),
        t,
        margin,
      );
      obs.record_at(
        Some(current),
        sockaddr([10, 0, 0, 5], 7410),
        t + Duration::from_secs(1),
        margin,
      );
    }
    assert_eq!(obs.best_interface(), Some(current));
  }
}