netstack3_core/ip/
multicast_forwarding.rs

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
// Copyright 2024 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.

//! Implementations for multicast forwarding that integrate with traits/types
//! from foreign modules.

use lock_order::lock::{LockLevelFor, UnlockedAccess};
use lock_order::relation::LockBefore;
use lock_order::wrap::{LockedWrapperApi, LockedWrapperUnlockedApi};
use netstack3_base::{CoreTimerContext, CounterContext};
use netstack3_device::{DeviceId, WeakDeviceId};
use netstack3_ip::multicast_forwarding::{
    MulticastForwardingCounters, MulticastForwardingEnabledState,
    MulticastForwardingPendingPackets, MulticastForwardingPendingPacketsContext,
    MulticastForwardingState, MulticastForwardingStateContext, MulticastForwardingTimerId,
    MulticastRouteTable, MulticastRouteTableContext,
};
use netstack3_ip::IpLayerTimerId;

use crate::{lock_ordering, BindingsContext, BindingsTypes, CoreCtx, IpExt, StackState};

#[netstack3_macros::instantiate_ip_impl_block(I)]
impl<
        I: IpExt,
        BC: BindingsContext,
        L: LockBefore<lock_ordering::IpMulticastForwardingState<I>>,
    > MulticastForwardingStateContext<I, BC> for CoreCtx<'_, BC, L>
{
    type Ctx<'a> = CoreCtx<'a, BC, lock_ordering::IpMulticastForwardingState<I>>;

    fn with_state<
        O,
        F: FnOnce(&MulticastForwardingState<I, Self::DeviceId, BC>, &mut Self::Ctx<'_>) -> O,
    >(
        &mut self,
        cb: F,
    ) -> O {
        let (state, mut core_ctx) =
            self.read_lock_and::<lock_ordering::IpMulticastForwardingState<I>>();
        cb(&state, &mut core_ctx)
    }

    fn with_state_mut<
        O,
        F: FnOnce(&mut MulticastForwardingState<I, Self::DeviceId, BC>, &mut Self::Ctx<'_>) -> O,
    >(
        &mut self,
        cb: F,
    ) -> O {
        let (mut state, mut core_ctx) =
            self.write_lock_and::<lock_ordering::IpMulticastForwardingState<I>>();
        cb(&mut state, &mut core_ctx)
    }
}

#[netstack3_macros::instantiate_ip_impl_block(I)]
impl<I: IpExt, BC: BindingsContext, L: LockBefore<lock_ordering::IpMulticastRouteTable<I>>>
    MulticastRouteTableContext<I, BC> for CoreCtx<'_, BC, L>
{
    type Ctx<'a> = CoreCtx<'a, BC, lock_ordering::IpMulticastRouteTable<I>>;

    fn with_route_table<
        O,
        F: FnOnce(&MulticastRouteTable<I, Self::DeviceId, BC>, &mut Self::Ctx<'_>) -> O,
    >(
        &mut self,
        state: &MulticastForwardingEnabledState<I, Self::DeviceId, BC>,
        cb: F,
    ) -> O {
        let mut locked = self.adopt(state);
        let (route_table, mut core_ctx) =
            locked.read_lock_with_and::<lock_ordering::IpMulticastRouteTable<I>, _>(|c| c.right());
        let mut core_ctx = core_ctx.cast_core_ctx();
        cb(&route_table, &mut core_ctx)
    }

    fn with_route_table_mut<
        O,
        F: FnOnce(&mut MulticastRouteTable<I, Self::DeviceId, BC>, &mut Self::Ctx<'_>) -> O,
    >(
        &mut self,
        state: &MulticastForwardingEnabledState<I, Self::DeviceId, BC>,
        cb: F,
    ) -> O {
        let mut locked = self.adopt(state);
        let (mut route_table, mut core_ctx) =
            locked.write_lock_with_and::<lock_ordering::IpMulticastRouteTable<I>, _>(|c| c.right());
        let mut core_ctx = core_ctx.cast_core_ctx();
        cb(&mut route_table, &mut core_ctx)
    }
}

#[netstack3_macros::instantiate_ip_impl_block(I)]
impl<
        I: IpExt,
        BC: BindingsContext,
        L: LockBefore<lock_ordering::IpMulticastForwardingPendingPackets<I>>,
    > MulticastForwardingPendingPacketsContext<I, BC> for CoreCtx<'_, BC, L>
{
    fn with_pending_table_mut<
        O,
        F: FnOnce(&mut MulticastForwardingPendingPackets<I, Self::WeakDeviceId, BC>) -> O,
    >(
        &mut self,
        state: &MulticastForwardingEnabledState<I, Self::DeviceId, BC>,
        cb: F,
    ) -> O {
        let mut locked = self.adopt(state);
        let mut pending_table = locked
            .lock_with::<lock_ordering::IpMulticastForwardingPendingPackets<I>, _>(|c| c.right());
        cb(&mut pending_table)
    }
}

impl<I: IpExt, BT: BindingsTypes> LockLevelFor<MulticastForwardingEnabledState<I, DeviceId<BT>, BT>>
    for lock_ordering::IpMulticastRouteTable<I>
{
    type Data = MulticastRouteTable<I, DeviceId<BT>, BT>;
}

impl<I: IpExt, BT: BindingsTypes> LockLevelFor<MulticastForwardingEnabledState<I, DeviceId<BT>, BT>>
    for lock_ordering::IpMulticastForwardingPendingPackets<I>
{
    type Data = MulticastForwardingPendingPackets<I, WeakDeviceId<BT>, BT>;
}

impl<I, BC, L> CoreTimerContext<MulticastForwardingTimerId<I>, BC> for CoreCtx<'_, BC, L>
where
    I: IpExt,
    BC: BindingsContext,
{
    fn convert_timer(dispatch_id: MulticastForwardingTimerId<I>) -> BC::DispatchId {
        IpLayerTimerId::from(dispatch_id).into()
    }
}

impl<I, BC, L> CounterContext<MulticastForwardingCounters<I>> for CoreCtx<'_, BC, L>
where
    I: IpExt,
    BC: BindingsContext,
{
    fn with_counters<O, F: FnOnce(&MulticastForwardingCounters<I>) -> O>(&self, cb: F) -> O {
        cb(self.unlocked_access::<crate::lock_ordering::MulticastForwardingCounters<I>>())
    }
}

impl<I, BC> UnlockedAccess<crate::lock_ordering::MulticastForwardingCounters<I>> for StackState<BC>
where
    I: IpExt,
    BC: BindingsContext,
{
    type Data = MulticastForwardingCounters<I>;
    type Guard<'l>
        = &'l MulticastForwardingCounters<I>
    where
        Self: 'l;

    fn access(&self) -> Self::Guard<'_> {
        self.inner_ip_state().multicast_forwarding_counters()
    }
}