netstack3_core/device/
socket.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
163
164
165
166
167
168
169
170
171
172
173
174
175
// 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 of traits defined in foreign modules for the types defined
//! in the device socket module.

use lock_order::lock::{DelegatedOrderedLockAccess, LockLevelFor};
use lock_order::relation::LockBefore;
use netstack3_device::socket::{
    AllSockets, AnyDeviceSockets, DeviceSocketAccessor, DeviceSocketContext, DeviceSocketId,
    DeviceSockets, HeldSockets, SocketStateAccessor, Target,
};
use netstack3_device::{for_any_device_id, DeviceId, WeakDeviceId};

use crate::context::prelude::*;
use crate::context::WrapLockLevel;
use crate::device::integration;
use crate::{BindingsContext, BindingsTypes, CoreCtx, StackState};

impl<BC: BindingsContext, L: LockBefore<crate::lock_ordering::AllDeviceSockets>>
    DeviceSocketContext<BC> for CoreCtx<'_, BC, L>
{
    type SocketTablesCoreCtx<'a> =
        CoreCtx<'a, BC, WrapLockLevel<crate::lock_ordering::AnyDeviceSockets>>;

    fn with_all_device_sockets_mut<F: FnOnce(&mut AllSockets<Self::WeakDeviceId, BC>) -> R, R>(
        &mut self,
        cb: F,
    ) -> R {
        let mut locked = self.lock::<crate::lock_ordering::AllDeviceSockets>();
        cb(&mut locked)
    }

    fn with_any_device_sockets<
        F: FnOnce(&AnyDeviceSockets<Self::WeakDeviceId, BC>, &mut Self::SocketTablesCoreCtx<'_>) -> R,
        R,
    >(
        &mut self,
        cb: F,
    ) -> R {
        let (sockets, mut locked) = self.read_lock_and::<crate::lock_ordering::AnyDeviceSockets>();
        cb(&*sockets, &mut locked)
    }

    fn with_any_device_sockets_mut<
        F: FnOnce(
            &mut AnyDeviceSockets<Self::WeakDeviceId, BC>,
            &mut Self::SocketTablesCoreCtx<'_>,
        ) -> R,
        R,
    >(
        &mut self,
        cb: F,
    ) -> R {
        let (mut sockets, mut locked) =
            self.write_lock_and::<crate::lock_ordering::AnyDeviceSockets>();
        cb(&mut *sockets, &mut locked)
    }
}

impl<BC: BindingsContext, L: LockBefore<crate::lock_ordering::DeviceSocketState>>
    SocketStateAccessor<BC> for CoreCtx<'_, BC, L>
{
    fn with_socket_state<F: FnOnce(&BC::SocketState, &Target<Self::WeakDeviceId>) -> R, R>(
        &mut self,
        id: &DeviceSocketId<Self::WeakDeviceId, BC>,
        cb: F,
    ) -> R {
        let external_state = id.socket_state();
        let mut locked = self.adopt(id);
        let guard = locked.lock_with::<crate::lock_ordering::DeviceSocketState, _>(|c| c.right());
        cb(external_state, &*guard)
    }

    fn with_socket_state_mut<
        F: FnOnce(&BC::SocketState, &mut Target<Self::WeakDeviceId>) -> R,
        R,
    >(
        &mut self,
        id: &DeviceSocketId<Self::WeakDeviceId, BC>,
        cb: F,
    ) -> R {
        let external_state = id.socket_state();
        let mut locked = self.adopt(id);
        let mut guard =
            locked.lock_with::<crate::lock_ordering::DeviceSocketState, _>(|c| c.right());
        cb(external_state, &mut *guard)
    }
}

impl<BC: BindingsContext, L: LockBefore<crate::lock_ordering::DeviceSockets>>
    DeviceSocketAccessor<BC> for CoreCtx<'_, BC, L>
{
    type DeviceSocketCoreCtx<'a> =
        CoreCtx<'a, BC, WrapLockLevel<crate::lock_ordering::DeviceSockets>>;

    fn with_device_sockets<
        F: FnOnce(&DeviceSockets<Self::WeakDeviceId, BC>, &mut Self::DeviceSocketCoreCtx<'_>) -> R,
        R,
    >(
        &mut self,
        device: &Self::DeviceId,
        cb: F,
    ) -> R {
        for_any_device_id!(
            DeviceId,
            device,
            device => {
                let mut core_ctx_and_resource =
                    integration::device_state_and_core_ctx(self, device);
                let (device_sockets, mut locked) = core_ctx_and_resource
                    .read_lock_with_and::<crate::lock_ordering::DeviceSockets, _>(
                    |c| c.right(),
                );
                cb(&*device_sockets, &mut locked.cast_core_ctx())
            }
        )
    }

    fn with_device_sockets_mut<
        F: FnOnce(&mut DeviceSockets<Self::WeakDeviceId, BC>, &mut Self::DeviceSocketCoreCtx<'_>) -> R,
        R,
    >(
        &mut self,
        device: &Self::DeviceId,
        cb: F,
    ) -> R {
        for_any_device_id!(
            DeviceId,
            device,
            device => {
                let mut core_ctx_and_resource =
                    integration::device_state_and_core_ctx(self, device);
                let (mut device_sockets, mut locked) = core_ctx_and_resource
                    .write_lock_with_and::<crate::lock_ordering::DeviceSockets, _>(
                    |c| c.right(),
                );
                cb(&mut *device_sockets, &mut locked.cast_core_ctx())
            }
        )
    }
}

impl<BT: BindingsTypes> DelegatedOrderedLockAccess<AnyDeviceSockets<WeakDeviceId<BT>, BT>>
    for StackState<BT>
{
    type Inner = HeldSockets<BT>;
    fn delegate_ordered_lock_access(&self) -> &Self::Inner {
        &self.device.shared_sockets
    }
}

impl<BT: BindingsTypes> DelegatedOrderedLockAccess<AllSockets<WeakDeviceId<BT>, BT>>
    for StackState<BT>
{
    type Inner = HeldSockets<BT>;
    fn delegate_ordered_lock_access(&self) -> &Self::Inner {
        &self.device.shared_sockets
    }
}

impl<BT: BindingsTypes> LockLevelFor<StackState<BT>> for crate::lock_ordering::AnyDeviceSockets {
    type Data = AnyDeviceSockets<WeakDeviceId<BT>, BT>;
}

impl<BT: BindingsTypes> LockLevelFor<StackState<BT>> for crate::lock_ordering::AllDeviceSockets {
    type Data = AllSockets<WeakDeviceId<BT>, BT>;
}

impl<BT: BindingsTypes> LockLevelFor<DeviceSocketId<WeakDeviceId<BT>, BT>>
    for crate::lock_ordering::DeviceSocketState
{
    type Data = Target<WeakDeviceId<BT>>;
}