dope-net 0.15.0

The manifold runtime
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
use dope_core::{
    driver::{
        flight, retained,
        route::{self, kind},
    },
    io::fd::handles,
};
use o3::cell::region;

use crate::{
    link::{
        self,
        egress::{self, data},
        engine::sending,
        event, pool,
        slot::types,
    },
    wire::{self, reclaim, send},
};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Retention {
    Clear,
    Held,
}

/// Immediate outcome of one egress submission attempt.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum Progress {
    Runnable,
    Waiting,
    Quiescent,
}

const _: () = assert!(std::mem::size_of::<Progress>() == 1);

fn finish<'d, const ID: u8, W: wire::Wire>(
    wire: &mut W::Connection<'d, ID>,
    submit: send::Outcome<W::Reclaim>,
) -> link::Consumed {
    let amount = match submit {
        send::Outcome::Submitted(consumed, _) => consumed,
        send::Outcome::Rejected(consumed, _) => {
            W::submit_failed(wire);
            if <W::Reclaim as reclaim::Policy>::ON_SUBMIT {
                consumed
            } else {
                0
            }
        }
        send::Outcome::Idle(consumed, _) => {
            if <W::Reclaim as reclaim::Policy>::ON_SUBMIT {
                consumed
            } else {
                0
            }
        }
    };
    link::Consumed::proven(amount)
}

fn submit_pending_graceful<'d, const ID: u8, W: wire::Wire>(
    wire: &mut W::Connection<'d, ID>,
    storage: &mut W::StorageBackend<'d>,
    mut submission: sending::Submission<'_, 'd>,
    fd: &handles::Descriptor<'d>,
    slots: &flight::Slots<'d, route::KeyTag<ID>>,
    driver: &mut retained::Context<'_, '_, 'd>,
    target: route::Target<'d, route::KeyTag<ID>>,
) {
    if !submission.take_pending_graceful() {
        return;
    }
    let prepared = W::graceful_close(wire, send::Storage::new(storage, 0));
    let submit = submission.submit_prepared(fd, slots, driver, target, prepared);
    finish::<ID, W>(wire, submit);
}

pub struct Status<'a, 'd, const ID: u8, W: wire::Wire, S> {
    connection: &'a types::Connection<'d, ID, W, S>,
}

impl<'a, 'd, const ID: u8, W: wire::Wire, S> Status<'a, 'd, ID, W, S> {
    pub(super) fn new(connection: &'a types::Connection<'d, ID, W, S>) -> Self {
        Self { connection }
    }

    pub fn inflight(&self) -> bool {
        self.connection.engine.sending.is_inflight()
    }

    pub fn retention(&self) -> Retention {
        if W::holds_plain(&self.connection.wire, &self.connection.send) {
            Retention::Held
        } else {
            Retention::Clear
        }
    }
}

pub struct Sending<'a, 'd, const ID: u8, W: wire::Wire, S> {
    connection: &'a mut types::Connection<'d, ID, W, S>,
}

#[doc(hidden)]
pub struct DirectSending<'a, 'd, const ID: u8, W: wire::Wire> {
    engine: &'a mut link::Engine<'d>,
    wire: &'a mut W::Connection<'d, ID>,
    storage: &'a mut W::StorageBackend<'d>,
    target: route::Target<'d, route::KeyTag<ID>>,
}

impl<'a, 'd, const ID: u8, W: wire::Wire> DirectSending<'a, 'd, ID, W> {
    pub(super) fn new(
        engine: &'a mut link::Engine<'d>,
        wire: &'a mut W::Connection<'d, ID>,
        storage: &'a mut W::StorageBackend<'d>,
        target: route::Target<'d, route::KeyTag<ID>>,
    ) -> Self {
        Self {
            engine,
            wire,
            storage,
            target,
        }
    }

    pub fn inflight(&self) -> bool {
        self.engine.sending.is_inflight()
    }

    pub fn abort(&mut self) {
        self.engine.lifecycle.abort();
    }

    pub fn submit_plain(
        &mut self,
        slots: &flight::Slots<'d, route::KeyTag<ID>>,
        driver: &mut retained::Context<'_, '_, 'd>,
        plain: send::Plain<'_>,
    ) -> link::Consumed {
        if self.inflight() {
            return link::Consumed::ZERO;
        }
        let Some(fd) = self.engine.establish.fd() else {
            return link::Consumed::ZERO;
        };
        let limit = plain.len();
        let prepared = W::prepare_send(self.wire, send::Storage::new(self.storage, limit), plain);
        let submit = self
            .engine
            .sending
            .submission(&mut self.engine.flights, &mut self.engine.lifecycle)
            .submit_prepared(fd, slots, driver, self.target, prepared);
        finish::<ID, W>(self.wire, submit)
    }

    pub fn submit_vectored(
        &mut self,
        plain: send::Vectored<'_>,
        slots: &flight::Slots<'d, route::KeyTag<ID>>,
        driver: &mut retained::Context<'_, '_, 'd>,
    ) -> link::Consumed {
        if self.inflight() {
            return link::Consumed::ZERO;
        }
        let Some(fd) = self.engine.establish.fd() else {
            return link::Consumed::ZERO;
        };
        let limit = plain.bytes();
        let prepared =
            W::prepare_send_vectored(self.wire, send::Storage::new(self.storage, limit), plain);
        let submit = self
            .engine
            .sending
            .submission(&mut self.engine.flights, &mut self.engine.lifecycle)
            .submit_prepared(fd, slots, driver, self.target, prepared);
        finish::<ID, W>(self.wire, submit)
    }
}

impl<'a, 'd, const ID: u8, W: wire::Wire, S> Sending<'a, 'd, ID, W, S> {
    pub(super) fn new(connection: &'a mut types::Connection<'d, ID, W, S>) -> Self {
        Self { connection }
    }

    fn target(&self) -> route::Target<'d, route::KeyTag<ID>> {
        self.connection.key().target()
    }

    pub fn submit_egress<const IOV: usize, B: data::Payload<'d>>(
        &mut self,
        queue: &mut egress::Queue<'_, 'd, IOV, B>,
        slots: &flight::Slots<'d, route::KeyTag<ID>>,
        driver: &mut retained::Context<'_, '_, 'd>,
    ) -> Result<Progress, link::EgressError> {
        let target = self.target();
        let connection = &mut *self.connection;
        let Some(fd) = connection.engine.establish.fd() else {
            return Ok(Progress::Waiting);
        };
        if connection.engine.sending.is_inflight() || queue.is_send_inflight() {
            return Ok(Progress::Waiting);
        }
        let Some(mut flight) = queue.prepare_flight(driver.region_token(), u32::MAX as usize)
        else {
            return match Self::flush(connection, slots, driver, target) {
                send::Outcome::Submitted(_, _) => Ok(Progress::Waiting),
                send::Outcome::Idle(_, _) => Ok(Progress::Quiescent),
                send::Outcome::Rejected(_, _) => Err(link::EgressError),
            };
        };
        let limit = flight.bytes();
        let submit = {
            let plain = flight.vectored();
            let prepared = W::prepare_send_vectored(
                &mut connection.wire,
                send::Storage::new(&mut connection.send, limit),
                plain,
            );
            connection
                .engine
                .sending
                .submission(
                    &mut connection.engine.flights,
                    &mut connection.engine.lifecycle,
                )
                .submit_prepared(fd, slots, driver, target, prepared)
        };
        match submit {
            send::Outcome::Submitted(consumed, _) => {
                if !<W::Reclaim as reclaim::Policy>::ON_SUBMIT {
                    flight.retain(route::Token::from_target(target).with_kind(kind::SEND));
                } else {
                    let Some(released) = flight.release(consumed) else {
                        connection.engine.lifecycle.abort();
                        return Err(link::EgressError);
                    };
                    if queue
                        .transfer()
                        .settle_submitted(driver.region_token(), released, consumed)
                    {
                        return Ok(Progress::Waiting);
                    }
                    connection.engine.lifecycle.abort();
                    return Err(link::EgressError);
                }
                Ok(Progress::Waiting)
            }
            send::Outcome::Rejected(consumed, _) => {
                W::submit_failed(&mut connection.wire);
                let released = flight.release(if <W::Reclaim as reclaim::Policy>::ON_SUBMIT {
                    consumed
                } else {
                    0
                });
                if let Some(released) = released {
                    let settled = if <W::Reclaim as reclaim::Policy>::ON_SUBMIT {
                        queue
                            .transfer()
                            .settle_submitted(driver.region_token(), released, consumed)
                    } else {
                        queue.transfer().settle(driver.region_token(), released)
                    };
                    if !settled {
                        connection.engine.lifecycle.abort();
                        return Err(link::EgressError);
                    }
                } else {
                    connection.engine.lifecycle.abort();
                }
                let _ = consumed;
                connection.engine.lifecycle.abort();
                Err(link::EgressError)
            }
            send::Outcome::Idle(consumed, _) => {
                let released = flight.release(if <W::Reclaim as reclaim::Policy>::ON_SUBMIT {
                    consumed
                } else {
                    0
                });
                if let Some(released) = released {
                    let settled = if <W::Reclaim as reclaim::Policy>::ON_SUBMIT {
                        queue
                            .transfer()
                            .settle_submitted(driver.region_token(), released, consumed)
                    } else {
                        queue.transfer().settle(driver.region_token(), released)
                    };
                    if !settled {
                        connection.engine.lifecycle.abort();
                        return Err(link::EgressError);
                    }
                } else {
                    connection.engine.lifecycle.abort();
                    return Err(link::EgressError);
                }
                if consumed > 0 && queue.total_bytes() > 0 {
                    Ok(Progress::Runnable)
                } else {
                    Ok(Progress::Quiescent)
                }
            }
        }
    }

    pub fn complete_egress<const IOV: usize, B: data::Payload<'d>>(
        &mut self,
        queue: &mut egress::Queue<'_, 'd, IOV, B>,
        token: &mut region::Token<'d>,
        completion: event::SendCompletion<'d, ID>,
    ) -> Result<usize, event::SendCompletion<'d, ID>> {
        if <W::Reclaim as reclaim::Policy>::ON_SUBMIT {
            return Ok(queue.transfer().take_submitted());
        }
        let bytes = completion.sent().get();
        if !queue
            .transfer()
            .complete(token, route::Token::from(completion.key()), bytes)
        {
            return Err(completion);
        }
        Ok(bytes)
    }

    pub fn abort_egress<const IOV: usize, B: data::Payload<'d>>(
        &mut self,
        queue: &mut egress::Queue<'_, 'd, IOV, B>,
        completion: event::SendCompletion<'d, ID>,
    ) -> bool {
        if <W::Reclaim as reclaim::Policy>::ON_SUBMIT {
            queue.transfer().take_submitted();
            true
        } else {
            queue.transfer().abort(route::Token::from(completion.key()))
        }
    }

    pub fn flush_pending(
        &mut self,
        slots: &flight::Slots<'d, route::KeyTag<ID>>,
        driver: &mut retained::Context<'_, '_, 'd>,
    ) {
        let target = self.target();
        Self::flush(self.connection, slots, driver, target);
    }

    fn flush(
        connection: &mut types::Connection<'d, ID, W, S>,
        slots: &flight::Slots<'d, route::KeyTag<ID>>,
        driver: &mut retained::Context<'_, '_, 'd>,
        target: route::Target<'d, route::KeyTag<ID>>,
    ) -> send::Outcome<W::Reclaim> {
        if connection.engine.sending.is_inflight() {
            return send::Outcome::idle(0);
        }
        let Some(fd) = connection.engine.establish.fd() else {
            return send::Outcome::idle(0);
        };
        let prepared = W::flush_pending(
            &mut connection.wire,
            send::Storage::new(&mut connection.send, 0),
        );
        let submit = connection
            .engine
            .sending
            .submission(
                &mut connection.engine.flights,
                &mut connection.engine.lifecycle,
            )
            .submit_prepared(fd, slots, driver, target, prepared);
        finish::<ID, W>(&mut connection.wire, submit);
        if matches!(submit, send::Outcome::Rejected(_, _)) {
            connection.engine.lifecycle.abort();
        }
        submit
    }

    pub fn seal_graceful(
        &mut self,
        slots: &flight::Slots<'d, route::KeyTag<ID>>,
        driver: &mut retained::Context<'_, '_, 'd>,
    ) -> bool {
        let target = self.target();
        let connection = &mut *self.connection;
        let Some(fd) = connection.engine.establish.fd() else {
            return false;
        };
        if connection.engine.lifecycle.request_graceful() {
            let submission = connection.engine.sending.submission(
                &mut connection.engine.flights,
                &mut connection.engine.lifecycle,
            );
            submit_pending_graceful::<ID, W>(
                &mut connection.wire,
                &mut connection.send,
                submission,
                fd,
                slots,
                driver,
                target,
            );
        }
        if connection.engine.sending.is_inflight() {
            connection.engine.lifecycle.begin_close();
            return true;
        }
        false
    }

    pub(in crate::link) fn sent(
        &mut self,
        slots: &flight::Slots<'d, route::KeyTag<ID>>,
        driver: &mut retained::Context<'_, '_, 'd>,
        bytes: u32,
        key: pool::Key<'d, ID>,
    ) -> (event::SendOutcome<'d, ID>, send::Availability) {
        let target = key.target();
        let connection = &mut *self.connection;
        if !connection.engine.sending.is_inflight() {
            return (event::SendOutcome::Drop, send::Availability::Unchanged);
        }
        let Some(fd) = connection.engine.establish.fd() else {
            connection
                .engine
                .sending
                .done(&mut connection.engine.flights);
            connection.engine.lifecycle.abort();
            return (
                event::SendOutcome::Close(event::SendCompletion::new(key, send::Sent::new(0))),
                send::Availability::Unchanged,
            );
        };
        let Some(sent) = connection.engine.sending.complete(
            &mut connection.engine.flights,
            &mut connection.engine.lifecycle,
            bytes,
        ) else {
            return (
                event::SendOutcome::Close(event::SendCompletion::new(key, send::Sent::new(0))),
                send::Availability::Unchanged,
            );
        };
        let after_send = W::after_send(
            &mut connection.wire,
            send::Storage::new(&mut connection.send, 0),
            sent,
        );
        let (prepared, availability) = after_send.into_parts();
        let submit = connection
            .engine
            .sending
            .submission(
                &mut connection.engine.flights,
                &mut connection.engine.lifecycle,
            )
            .submit_prepared(fd, slots, driver, target, prepared);
        finish::<ID, W>(&mut connection.wire, submit);
        let submission = connection.engine.sending.submission(
            &mut connection.engine.flights,
            &mut connection.engine.lifecycle,
        );
        submit_pending_graceful::<ID, W>(
            &mut connection.wire,
            &mut connection.send,
            submission,
            fd,
            slots,
            driver,
            target,
        );
        if connection.engine.sending.is_inflight() {
            return (event::SendOutcome::Drop, availability);
        }
        (
            event::SendOutcome::Sent(event::SendCompletion::new(key, sent)),
            availability,
        )
    }

    pub(in crate::link) fn failed(&mut self, key: pool::Key<'d, ID>) -> event::SendOutcome<'d, ID> {
        let connection = &mut *self.connection;
        if !connection.engine.sending.is_inflight() {
            return event::SendOutcome::Drop;
        }
        connection
            .engine
            .sending
            .done(&mut connection.engine.flights);
        connection.engine.lifecycle.abort();
        event::SendOutcome::Close(event::SendCompletion::new(key, send::Sent::new(0)))
    }
}