netstack3_core/device/
socket.rs

1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Implementations of traits defined in foreign modules for the types defined
6//! in the device socket module.
7
8use lock_order::lock::{DelegatedOrderedLockAccess, LockLevelFor};
9use lock_order::relation::LockBefore;
10use netstack3_device::socket::{
11    AllSockets, AnyDeviceSockets, DeviceSocketAccessor, DeviceSocketContext, DeviceSocketId,
12    DeviceSockets, HeldSockets, SocketStateAccessor, Target,
13};
14use netstack3_device::{for_any_device_id, DeviceId, WeakDeviceId};
15
16use crate::context::prelude::*;
17use crate::context::WrapLockLevel;
18use crate::device::integration;
19use crate::{BindingsContext, BindingsTypes, CoreCtx, StackState};
20
21impl<BC: BindingsContext, L: LockBefore<crate::lock_ordering::AllDeviceSockets>>
22    DeviceSocketContext<BC> for CoreCtx<'_, BC, L>
23{
24    type SocketTablesCoreCtx<'a> =
25        CoreCtx<'a, BC, WrapLockLevel<crate::lock_ordering::AnyDeviceSockets>>;
26
27    fn with_all_device_sockets<
28        F: FnOnce(&AllSockets<Self::WeakDeviceId, BC>, &mut Self::SocketTablesCoreCtx<'_>) -> R,
29        R,
30    >(
31        &mut self,
32        cb: F,
33    ) -> R {
34        let (sockets, mut locked) = self.read_lock_and::<crate::lock_ordering::AllDeviceSockets>();
35        let mut locked = locked.cast_locked::<crate::lock_ordering::AnyDeviceSockets>();
36        cb(&*sockets, &mut locked)
37    }
38
39    fn with_all_device_sockets_mut<F: FnOnce(&mut AllSockets<Self::WeakDeviceId, BC>) -> R, R>(
40        &mut self,
41        cb: F,
42    ) -> R {
43        let mut locked = self.write_lock::<crate::lock_ordering::AllDeviceSockets>();
44        cb(&mut locked)
45    }
46
47    fn with_any_device_sockets<
48        F: FnOnce(&AnyDeviceSockets<Self::WeakDeviceId, BC>, &mut Self::SocketTablesCoreCtx<'_>) -> R,
49        R,
50    >(
51        &mut self,
52        cb: F,
53    ) -> R {
54        let (sockets, mut locked) = self.read_lock_and::<crate::lock_ordering::AnyDeviceSockets>();
55        cb(&*sockets, &mut locked)
56    }
57
58    fn with_any_device_sockets_mut<
59        F: FnOnce(
60            &mut AnyDeviceSockets<Self::WeakDeviceId, BC>,
61            &mut Self::SocketTablesCoreCtx<'_>,
62        ) -> R,
63        R,
64    >(
65        &mut self,
66        cb: F,
67    ) -> R {
68        let (mut sockets, mut locked) =
69            self.write_lock_and::<crate::lock_ordering::AnyDeviceSockets>();
70        cb(&mut *sockets, &mut locked)
71    }
72}
73
74impl<BC: BindingsContext, L: LockBefore<crate::lock_ordering::DeviceSocketState>>
75    SocketStateAccessor<BC> for CoreCtx<'_, BC, L>
76{
77    fn with_socket_state<
78        F: FnOnce(&BC::SocketState<Self::WeakDeviceId>, &Target<Self::WeakDeviceId>) -> R,
79        R,
80    >(
81        &mut self,
82        id: &DeviceSocketId<Self::WeakDeviceId, BC>,
83        cb: F,
84    ) -> R {
85        let external_state = id.socket_state();
86        let mut locked = self.adopt(id);
87        let guard = locked.lock_with::<crate::lock_ordering::DeviceSocketState, _>(|c| c.right());
88        cb(external_state, &*guard)
89    }
90
91    fn with_socket_state_mut<
92        F: FnOnce(&BC::SocketState<Self::WeakDeviceId>, &mut Target<Self::WeakDeviceId>) -> R,
93        R,
94    >(
95        &mut self,
96        id: &DeviceSocketId<Self::WeakDeviceId, BC>,
97        cb: F,
98    ) -> R {
99        let external_state = id.socket_state();
100        let mut locked = self.adopt(id);
101        let mut guard =
102            locked.lock_with::<crate::lock_ordering::DeviceSocketState, _>(|c| c.right());
103        cb(external_state, &mut *guard)
104    }
105}
106
107impl<BC: BindingsContext, L: LockBefore<crate::lock_ordering::DeviceSockets>>
108    DeviceSocketAccessor<BC> for CoreCtx<'_, BC, L>
109{
110    type DeviceSocketCoreCtx<'a> =
111        CoreCtx<'a, BC, WrapLockLevel<crate::lock_ordering::DeviceSockets>>;
112
113    fn with_device_sockets<
114        F: FnOnce(&DeviceSockets<Self::WeakDeviceId, BC>, &mut Self::DeviceSocketCoreCtx<'_>) -> R,
115        R,
116    >(
117        &mut self,
118        device: &Self::DeviceId,
119        cb: F,
120    ) -> R {
121        for_any_device_id!(
122            DeviceId,
123            device,
124            device => {
125                let mut core_ctx_and_resource =
126                    integration::device_state_and_core_ctx(self, device);
127                let (device_sockets, mut locked) = core_ctx_and_resource
128                    .read_lock_with_and::<crate::lock_ordering::DeviceSockets, _>(
129                    |c| c.right(),
130                );
131                cb(&*device_sockets, &mut locked.cast_core_ctx())
132            }
133        )
134    }
135
136    fn with_device_sockets_mut<
137        F: FnOnce(&mut DeviceSockets<Self::WeakDeviceId, BC>, &mut Self::DeviceSocketCoreCtx<'_>) -> R,
138        R,
139    >(
140        &mut self,
141        device: &Self::DeviceId,
142        cb: F,
143    ) -> R {
144        for_any_device_id!(
145            DeviceId,
146            device,
147            device => {
148                let mut core_ctx_and_resource =
149                    integration::device_state_and_core_ctx(self, device);
150                let (mut device_sockets, mut locked) = core_ctx_and_resource
151                    .write_lock_with_and::<crate::lock_ordering::DeviceSockets, _>(
152                    |c| c.right(),
153                );
154                cb(&mut *device_sockets, &mut locked.cast_core_ctx())
155            }
156        )
157    }
158}
159
160impl<BT: BindingsTypes> DelegatedOrderedLockAccess<AnyDeviceSockets<WeakDeviceId<BT>, BT>>
161    for StackState<BT>
162{
163    type Inner = HeldSockets<BT>;
164    fn delegate_ordered_lock_access(&self) -> &Self::Inner {
165        &self.device.shared_sockets
166    }
167}
168
169impl<BT: BindingsTypes> DelegatedOrderedLockAccess<AllSockets<WeakDeviceId<BT>, BT>>
170    for StackState<BT>
171{
172    type Inner = HeldSockets<BT>;
173    fn delegate_ordered_lock_access(&self) -> &Self::Inner {
174        &self.device.shared_sockets
175    }
176}
177
178impl<BT: BindingsTypes> LockLevelFor<StackState<BT>> for crate::lock_ordering::AnyDeviceSockets {
179    type Data = AnyDeviceSockets<WeakDeviceId<BT>, BT>;
180}
181
182impl<BT: BindingsTypes> LockLevelFor<StackState<BT>> for crate::lock_ordering::AllDeviceSockets {
183    type Data = AllSockets<WeakDeviceId<BT>, BT>;
184}
185
186impl<BT: BindingsTypes> LockLevelFor<DeviceSocketId<WeakDeviceId<BT>, BT>>
187    for crate::lock_ordering::DeviceSocketState
188{
189    type Data = Target<WeakDeviceId<BT>>;
190}