orbit-link 0.6.8

Named reusable duplex sessions between members of an Orbit fleet.
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
//! Named descriptor wake for an inbox lane or a directory watcher.
//!
//! Frames remain in shared memory. The datagram contains one meaningless
//! byte and exists only so an async runtime can sleep on a descriptor instead
//! of waking a dedicated thread that then wakes the runtime.

use std::borrow::Cow;
use std::io;
use std::os::unix::net::{SocketAddr, UnixDatagram};
use std::sync::{Mutex, OnceLock, PoisonError};

#[cfg(feature = "tokio")]
fn recv(
    socket: &tokio::net::UnixDatagram,
    scratch: &mut [u8],
    flags: libc::c_int,
) -> io::Result<usize> {
    use std::os::fd::AsRawFd;

    crate::probe::read(|| {
        // SAFETY: socket owns this fd and scratch is writable for its length.
        let read = unsafe {
            libc::recv(
                socket.as_raw_fd(),
                scratch.as_mut_ptr().cast(),
                scratch.len(),
                flags,
            )
        };
        if read >= 0 {
            Ok(read as usize)
        } else {
            Err(io::Error::last_os_error())
        }
    })
}

/// Empty obsolete bells only when the receiver has no shared work left.
/// Callers disarm first and recheck the shared generation after rearming.
#[cfg(feature = "tokio")]
pub(crate) fn drain(socket: &tokio::net::UnixDatagram) -> io::Result<()> {
    let mut scratch = [0u8; 64];
    loop {
        // A generation recheck may have skipped the readiness wait.
        let error = match recv(socket, &mut scratch, libc::MSG_DONTWAIT) {
            Ok(_) => continue,
            Err(error) => error,
        };
        match error.kind() {
            io::ErrorKind::Interrupted => continue,
            io::ErrorKind::WouldBlock => {
                // Let Tokio clear a cached readiness bit with its own IO
                // attempt. A late bell is harmless; consume it and finish
                // draining before rearming.
                #[cfg(not(feature = "probe"))]
                let late = socket.try_recv(&mut scratch);
                #[cfg(feature = "probe")]
                let late = socket.try_io(tokio::io::Interest::READABLE, || {
                    recv(socket, &mut scratch, 0)
                });
                match late {
                    Ok(_) => continue,
                    Err(error) if error.kind() == io::ErrorKind::WouldBlock => {
                        return Ok(());
                    }
                    Err(error)
                        if error.kind() == io::ErrorKind::Interrupted => {}
                    Err(error) => return Err(error),
                }
            }
            _ => return Err(error),
        }
    }
}

/// Read one bell if one is there. The call goes to the socket itself: Tokio
/// may not have seen the bell arrive yet, and would answer for it.
#[cfg(feature = "tokio")]
pub(crate) fn take_one(socket: &tokio::net::UnixDatagram) -> io::Result<bool> {
    let mut scratch = [0u8; 64];
    loop {
        let error = match recv(socket, &mut scratch, libc::MSG_DONTWAIT) {
            Ok(_) => return Ok(true),
            Err(error) => error,
        };
        match error.kind() {
            io::ErrorKind::Interrupted => continue,
            io::ErrorKind::WouldBlock => return Ok(false),
            _ => return Err(error),
        }
    }
}

/// Drop Tokio's note that the socket is readable, without a call. For a
/// caller that has read every bell it knows was sent: the note is about bells
/// that are gone, and the next bell makes a new one.
#[cfg(feature = "tokio")]
pub(crate) fn forget_readiness(socket: &tokio::net::UnixDatagram) {
    let _ = socket.try_io(tokio::io::Interest::READABLE, || {
        Err::<(), io::Error>(io::ErrorKind::WouldBlock.into())
    });
}

/// Whether a send was refused because the far side's queue is full.
fn queue_full(error: &io::Error) -> bool {
    error.kind() == io::ErrorKind::WouldBlock
        || error.raw_os_error() == Some(libc::ENOBUFS)
}

/// Who a datagram wakes. Lanes and watchers have separate addresses because a
/// named socket has exactly one binder.
#[derive(Clone, Copy)]
pub(crate) enum Doorbell {
    Lane(usize),
    Watcher(usize),
    /// A producer registration waiting for a reader to free a record.
    Producer(usize),
    /// The process waiting for the Link's owner to create it.
    Adopter,
}

impl Doorbell {
    fn suffix(self) -> String {
        match self {
            Self::Lane(lane) => lane.to_string(),
            Self::Watcher(slot) => format!("w{slot}"),
            Self::Producer(index) => format!("p{index}"),
            Self::Adopter => "a".to_owned(),
        }
    }
}

/// A socket connected to one lane's bell, and the lane epoch it was connected
/// under.
struct Connected {
    epoch: u64,
    socket: UnixDatagram,
}

pub(crate) struct Bell {
    prefix: String,
    sender: UnixDatagram,
    group: Option<orbit_core::shm::GroupAccess>,
    /// One entry for each lane this process may ring. A ring over a kept
    /// connection is a plain send; naming the lane again is for the first
    /// ring and for a bell that was replaced. Only the writer that took the
    /// lane's arm rings it, so a lane's entry is not contended.
    lanes: Box<[Mutex<Option<Connected>>]>,
    /// Each door's address, built on its first ring: a door's name is fixed
    /// for the bell's life, so a ring is one send rather than a name to
    /// format again. Indexed by [`Bell::door_index`].
    addresses: Box<[OnceLock<SocketAddr>]>,
}

impl Bell {
    #[cfg(test)]
    pub(crate) fn new(segment: &str) -> io::Result<Self> {
        Self::with_group(segment, None, 1)
    }

    /// `lanes` is how many lane bells this one may keep a connection to;
    /// zero for a bell that only binds.
    pub(crate) fn with_group(
        segment: &str,
        group: Option<orbit_core::shm::GroupAccess>,
        lanes: usize,
    ) -> io::Result<Self> {
        let sender = UnixDatagram::unbound()?;
        sender.set_nonblocking(true)?;
        let prefix = if group.is_some() {
            segment.to_owned()
        } else {
            Self::prefix(segment)
        };
        let addresses =
            (0..Self::door_count(lanes)).map(|_| OnceLock::new()).collect();
        let lanes = (0..lanes).map(|_| Mutex::new(None)).collect();
        Ok(Self { prefix, sender, group, lanes, addresses })
    }

    pub(crate) fn validate_group_address(
        access: &orbit_core::shm::GroupAccess,
        segment: &str,
        last_slot: usize,
    ) -> io::Result<()> {
        for door in Self::longest(last_slot) {
            SocketAddr::from_pathname(
                access.bell_path(segment, &door.suffix()),
            )?;
        }
        Ok(())
    }

    /// The longest names a table of `last_slot + 1` lanes binds: its last
    /// watcher and its last producer registration (two per lane).
    fn longest(last_slot: usize) -> [Doorbell; 2] {
        [Doorbell::Watcher(last_slot), Doorbell::Producer(2 * last_slot + 1)]
    }

    fn group_path(
        &self,
        door: Doorbell,
    ) -> Option<std::path::PathBuf> {
        self.group
            .as_ref()
            .map(|access| access.bell_path(&self.prefix, &door.suffix()))
    }

    fn prefix(segment: &str) -> String {
        // SAFETY: a plain syscall with no arguments.
        let uid = unsafe { libc::getuid() };
        let segment = segment.trim_start_matches('/');
        format!("orbit-link-{uid}/{segment}")
    }

    /// Check the longest lane, watcher or producer address before creating
    /// either SHM resource. A watcher suffix is one byte longer than the lane
    /// suffix; a producer index runs to twice the lane count.
    pub(crate) fn validate_address(
        segment: &str,
        last_slot: usize,
    ) -> io::Result<()> {
        let prefix = Self::prefix(segment);
        for door in Self::longest(last_slot) {
            Self::address_for(&prefix, door)?;
        }
        Ok(())
    }

    #[cfg(target_os = "linux")]
    fn address(
        &self,
        door: Doorbell,
    ) -> io::Result<SocketAddr> {
        if let Some(path) = self.group_path(door) {
            return SocketAddr::from_pathname(path);
        }
        Self::address_for(&self.prefix, door)
    }

    #[cfg(target_os = "linux")]
    fn address_for(
        prefix: &str,
        door: Doorbell,
    ) -> io::Result<SocketAddr> {
        use std::os::linux::net::SocketAddrExt;
        SocketAddr::from_abstract_name(format!("{}.{}", prefix, door.suffix()))
    }

    #[cfg(not(target_os = "linux"))]
    fn path(
        &self,
        door: Doorbell,
    ) -> std::path::PathBuf {
        Self::path_for(&self.prefix, door)
    }

    #[cfg(not(target_os = "linux"))]
    fn path_for(
        prefix: &str,
        door: Doorbell,
    ) -> std::path::PathBuf {
        std::path::PathBuf::from(format!("/tmp/{}.{}", prefix, door.suffix()))
    }

    #[cfg(not(target_os = "linux"))]
    fn address(
        &self,
        door: Doorbell,
    ) -> io::Result<SocketAddr> {
        if let Some(path) = self.group_path(door) {
            return SocketAddr::from_pathname(path);
        }
        Self::address_for(&self.prefix, door)
    }

    #[cfg(not(target_os = "linux"))]
    fn address_for(
        prefix: &str,
        door: Doorbell,
    ) -> io::Result<SocketAddr> {
        SocketAddr::from_pathname(Self::path_for(prefix, door))
    }

    /// Ring a lane's bell over the socket connected to it, connecting first
    /// when there is none, when the lane has changed hands since (`epoch`),
    /// or when the send says the far side is gone.
    pub(crate) fn ring_lane(
        &self,
        lane: usize,
        epoch: u64,
    ) -> io::Result<()> {
        let Some(kept) = self.lanes.get(lane) else {
            return self.ring(Doorbell::Lane(lane));
        };
        let mut kept = kept.lock().unwrap_or_else(PoisonError::into_inner);
        if let Some(connected) = kept.as_ref()
            && connected.epoch == epoch
        {
            match crate::probe::ring(|| connected.socket.send(&[1])) {
                Ok(_) => return Ok(()),
                // A full queue is not a lost bell: the far side has not read
                // the ones it has.
                Err(error) if queue_full(&error) => return Err(error),
                // Closed or replaced: find the lane's bell again.
                Err(_) => {}
            }
        }
        *kept = None;
        let socket = UnixDatagram::unbound()?;
        socket.set_nonblocking(true)?;
        socket.connect_addr(&*self.door_address(Doorbell::Lane(lane))?)?;
        crate::probe::ring(|| socket.send(&[1]))?;
        *kept = Some(Connected { epoch, socket });
        Ok(())
    }

    pub(crate) fn ring(
        &self,
        door: Doorbell,
    ) -> io::Result<()> {
        let address = self.door_address(door)?;
        crate::probe::ring(|| self.sender.send_to_addr(&[1], &address))
            .map(|_| ())
    }

    /// Doors a bell of `lanes` lanes can ring: each lane, each watcher slot,
    /// two producer registrations per lane, and the adopter.
    fn door_count(lanes: usize) -> usize {
        if lanes == 0 { 0 } else { 4 * lanes + 1 }
    }

    fn door_index(
        &self,
        door: Doorbell,
    ) -> Option<usize> {
        let lanes = self.lanes.len();
        let index = match door {
            Doorbell::Lane(lane) if lane < lanes => lane,
            Doorbell::Watcher(slot) if slot < lanes => lanes + slot,
            Doorbell::Producer(index) if index < 2 * lanes => 2 * lanes + index,
            Doorbell::Adopter => 4 * lanes,
            _ => return None,
        };
        (index < self.addresses.len()).then_some(index)
    }

    /// The door's address, built once and kept. A name the platform refuses
    /// is not kept, so it is refused again on the next ring.
    fn door_address(
        &self,
        door: Doorbell,
    ) -> io::Result<Cow<'_, SocketAddr>> {
        let Some(kept) =
            self.door_index(door).and_then(|index| self.addresses.get(index))
        else {
            return self.address(door).map(Cow::Owned);
        };
        if let Some(address) = kept.get() {
            return Ok(Cow::Borrowed(address));
        }
        let address = self.address(door)?;
        Ok(Cow::Borrowed(kept.get_or_init(|| address)))
    }

    #[cfg(feature = "tokio")]
    pub(crate) fn bind(
        &self,
        door: Doorbell,
    ) -> io::Result<UnixDatagram> {
        if let Some(access) = &self.group {
            return access.bind_bell(&self.group_path(door).unwrap());
        }
        #[cfg(not(target_os = "linux"))]
        {
            use std::os::unix::fs::DirBuilderExt;
            let path = self.path(door);
            if let Some(directory) = path.parent() {
                match std::fs::DirBuilder::new().mode(0o700).create(directory) {
                    Err(error)
                        if error.kind() != io::ErrorKind::AlreadyExists =>
                    {
                        return Err(error);
                    }
                    _ => {}
                }
            }
            match std::fs::remove_file(&path) {
                Ok(()) => {}
                Err(error) if error.kind() == io::ErrorKind::NotFound => {}
                Err(error) => return Err(error),
            }
        }
        let socket = UnixDatagram::bind_addr(&self.address(door)?)?;
        socket.set_nonblocking(true)?;
        Ok(socket)
    }

    #[cfg(feature = "tokio")]
    pub(crate) fn unbind(
        &self,
        door: Doorbell,
    ) {
        if let Some(path) = self.group_path(door) {
            let _ = std::fs::remove_file(path);
            return;
        }
        #[cfg(not(target_os = "linux"))]
        let _ = std::fs::remove_file(self.path(door));
        #[cfg(target_os = "linux")]
        let _ = door;
    }
}