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
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Types for dealing with time and timers.

use core::convert::Infallible as Never;

use derivative::Derivative;
use net_types::ip::{GenericOverIp, Ip, Ipv4, Ipv6};
use tracing::trace;

use crate::{
    context::{CoreCtx, CoreTimerContext, TimerContext, TimerHandler},
    device::{DeviceId, DeviceLayerTimerId},
    ip::{
        device::{IpDeviceIpExt, IpDeviceTimerId},
        IpLayerTimerId,
    },
    transport::TransportLayerTimerId,
    BindingsTypes,
};

pub use netstack3_base::{Instant, LocalTimerHeap};

/// The identifier for any timer event.
#[derive(Derivative, GenericOverIp)]
#[derivative(
    Clone(bound = ""),
    Eq(bound = ""),
    PartialEq(bound = ""),
    Hash(bound = ""),
    Debug(bound = "")
)]
#[generic_over_ip()]
pub struct TimerId<BT: BindingsTypes>(pub(crate) TimerIdInner<BT>);

#[derive(Derivative, GenericOverIp)]
#[derivative(
    Clone(bound = ""),
    Eq(bound = ""),
    PartialEq(bound = ""),
    Hash(bound = ""),
    Debug(bound = "")
)]
#[generic_over_ip()]
pub(crate) enum TimerIdInner<BT: BindingsTypes> {
    /// A timer event in the device layer.
    DeviceLayer(DeviceLayerTimerId<BT>),
    /// A timer event in the transport layer.
    TransportLayer(TransportLayerTimerId<BT>),
    /// A timer event in the IP layer.
    IpLayer(IpLayerTimerId),
    /// A timer event for an IPv4 device.
    Ipv4Device(IpDeviceTimerId<Ipv4, DeviceId<BT>>),
    /// A timer event for an IPv6 device.
    Ipv6Device(IpDeviceTimerId<Ipv6, DeviceId<BT>>),
}

impl<BT: BindingsTypes> TimerIdInner<BT> {
    fn as_ip_device<I: IpDeviceIpExt>(&self) -> Option<&IpDeviceTimerId<I, DeviceId<BT>>> {
        I::map_ip(
            self,
            |t| match t {
                TimerIdInner::Ipv4Device(d) => Some(d),
                _ => None,
            },
            |t| match t {
                TimerIdInner::Ipv6Device(d) => Some(d),
                _ => None,
            },
        )
    }
}

impl<BT: BindingsTypes> From<DeviceLayerTimerId<BT>> for TimerId<BT> {
    fn from(id: DeviceLayerTimerId<BT>) -> TimerId<BT> {
        TimerId(TimerIdInner::DeviceLayer(id))
    }
}

impl<BT: BindingsTypes> From<IpLayerTimerId> for TimerId<BT> {
    fn from(id: IpLayerTimerId) -> TimerId<BT> {
        TimerId(TimerIdInner::IpLayer(id))
    }
}

impl<BT: BindingsTypes> From<TransportLayerTimerId<BT>> for TimerId<BT> {
    fn from(id: TransportLayerTimerId<BT>) -> Self {
        TimerId(TimerIdInner::TransportLayer(id))
    }
}

impl_timer_context!(
    BT: BindingsTypes,
    TimerId<BT>,
    DeviceLayerTimerId<BT>,
    TimerId(TimerIdInner::DeviceLayer(id)),
    id
);
impl_timer_context!(
    BT: BindingsTypes,
    TimerId<BT>,
    IpLayerTimerId,
    TimerId(TimerIdInner::IpLayer(id)),
    id
);

impl<BT: BindingsTypes, I: IpDeviceIpExt> From<IpDeviceTimerId<I, DeviceId<BT>>> for TimerId<BT> {
    fn from(value: IpDeviceTimerId<I, DeviceId<BT>>) -> Self {
        I::map_ip(
            value,
            |v4| TimerId(TimerIdInner::Ipv4Device(v4)),
            |v6| TimerId(TimerIdInner::Ipv6Device(v6)),
        )
    }
}

impl<BT, I, O> TimerContext<IpDeviceTimerId<I, DeviceId<BT>>> for O
where
    BT: BindingsTypes,
    I: IpDeviceIpExt,
    O: TimerContext<TimerId<BT>>,
{
    fn schedule_timer_instant(
        &mut self,
        time: Self::Instant,
        id: IpDeviceTimerId<I, DeviceId<BT>>,
    ) -> Option<Self::Instant> {
        TimerContext::<TimerId<BT>>::schedule_timer_instant(self, time, id.into())
    }

    fn cancel_timer(&mut self, id: IpDeviceTimerId<I, DeviceId<BT>>) -> Option<Self::Instant> {
        TimerContext::<TimerId<BT>>::cancel_timer(self, id.into())
    }

    fn cancel_timers_with<F: FnMut(&IpDeviceTimerId<I, DeviceId<BT>>) -> bool>(
        &mut self,
        mut f: F,
    ) {
        TimerContext::<TimerId<BT>>::cancel_timers_with(self, move |TimerId(timer_id)| {
            timer_id.as_ip_device::<I>().is_some_and(|d| f(d))
        })
    }

    fn scheduled_instant(&self, id: IpDeviceTimerId<I, DeviceId<BT>>) -> Option<Self::Instant> {
        TimerContext::<TimerId<BT>>::scheduled_instant(self, id.into())
    }
}

impl_timer_context!(
    BT: BindingsTypes,
    TimerId<BT>,
    TransportLayerTimerId<BT>,
    TimerId(TimerIdInner::TransportLayer(id)),
    id
);

impl<BT, CC> TimerHandler<BT, TimerId<BT>> for CC
where
    BT: BindingsTypes,
    CC: TimerHandler<BT, DeviceLayerTimerId<BT>>
        + TimerHandler<BT, TransportLayerTimerId<BT>>
        + TimerHandler<BT, IpLayerTimerId>
        + TimerHandler<BT, IpDeviceTimerId<Ipv4, DeviceId<BT>>>
        + TimerHandler<BT, IpDeviceTimerId<Ipv6, DeviceId<BT>>>,
{
    fn handle_timer(&mut self, bindings_ctx: &mut BT, id: TimerId<BT>) {
        trace!("handle_timer: dispatching timerid: {id:?}");
        match id {
            TimerId(TimerIdInner::DeviceLayer(x)) => self.handle_timer(bindings_ctx, x),
            TimerId(TimerIdInner::TransportLayer(x)) => self.handle_timer(bindings_ctx, x),
            TimerId(TimerIdInner::IpLayer(x)) => self.handle_timer(bindings_ctx, x),
            TimerId(TimerIdInner::Ipv4Device(x)) => self.handle_timer(bindings_ctx, x),
            TimerId(TimerIdInner::Ipv6Device(x)) => self.handle_timer(bindings_ctx, x),
        }
    }
}

impl<'a, BT, L> CoreTimerContext<Never, BT> for CoreCtx<'a, BT, L>
where
    BT: BindingsTypes,
{
    fn convert_timer(dispatch_id: Never) -> <BT as netstack3_base::TimerBindingsTypes>::DispatchId {
        match dispatch_id {}
    }
}