netstack3_core/
api.rs
1use lock_order::Unlocked;
7use net_types::ip::Ip;
8use netstack3_base::{
9 ContextPair as _, ContextProvider, CtxPair, TimerBindingsTypes, TimerHandler as _,
10};
11use netstack3_device::queue::{ReceiveQueueApi, TransmitQueueApi};
12use netstack3_device::socket::DeviceSocketApi;
13use netstack3_device::{DeviceAnyApi, DeviceApi};
14use netstack3_filter::FilterApi;
15use netstack3_icmp_echo::IcmpEchoSocketApi;
16use netstack3_ip::device::{DeviceIpAnyApi, DeviceIpApi};
17use netstack3_ip::multicast_forwarding::MulticastForwardingApi;
18use netstack3_ip::nud::NeighborApi;
19use netstack3_ip::raw::RawIpSocketApi;
20use netstack3_ip::{RoutesAnyApi, RoutesApi};
21use netstack3_tcp::TcpApi;
22use netstack3_udp::UdpApi;
23
24use crate::context::CoreCtx;
25use crate::counters::CountersApi;
26use crate::time::TimerId;
27use crate::BindingsTypes;
28
29type CoreApiCtxPair<'a, BP> = CtxPair<CoreCtx<'a, <BP as ContextProvider>::Context, Unlocked>, BP>;
30
31pub struct CoreApi<'a, BP>(CoreApiCtxPair<'a, BP>)
33where
34 BP: ContextProvider,
35 BP::Context: BindingsTypes;
36
37impl<'a, BP> CoreApi<'a, BP>
38where
39 BP: ContextProvider,
40 BP::Context: BindingsTypes,
41{
42 pub(crate) fn new(ctx_pair: CoreApiCtxPair<'a, BP>) -> Self {
43 Self(ctx_pair)
44 }
45
46 pub fn udp<I: Ip>(self) -> UdpApi<I, CoreApiCtxPair<'a, BP>> {
48 let Self(ctx) = self;
49 UdpApi::new(ctx)
50 }
51
52 pub fn icmp_echo<I: Ip>(self) -> IcmpEchoSocketApi<I, CoreApiCtxPair<'a, BP>> {
54 let Self(ctx) = self;
55 IcmpEchoSocketApi::new(ctx)
56 }
57
58 pub fn tcp<I: Ip>(self) -> TcpApi<I, CoreApiCtxPair<'a, BP>> {
60 let Self(ctx) = self;
61 TcpApi::new(ctx)
62 }
63
64 pub fn raw_ip_socket<I: Ip>(self) -> RawIpSocketApi<I, CoreApiCtxPair<'a, BP>> {
66 let Self(ctx) = self;
67 RawIpSocketApi::new(ctx)
68 }
69
70 pub fn device_socket(self) -> DeviceSocketApi<CoreApiCtxPair<'a, BP>> {
72 let Self(ctx) = self;
73 DeviceSocketApi::new(ctx)
74 }
75
76 pub fn filter(self) -> FilterApi<CoreApiCtxPair<'a, BP>> {
78 let Self(ctx) = self;
79 FilterApi::new(ctx)
80 }
81
82 pub fn routes<I: Ip>(self) -> RoutesApi<I, CoreApiCtxPair<'a, BP>> {
84 let Self(ctx) = self;
85 RoutesApi::new(ctx)
86 }
87
88 pub fn routes_any(self) -> RoutesAnyApi<CoreApiCtxPair<'a, BP>> {
90 let Self(ctx) = self;
91 RoutesAnyApi::new(ctx)
92 }
93
94 pub fn multicast_forwarding<I: Ip>(self) -> MulticastForwardingApi<I, CoreApiCtxPair<'a, BP>> {
96 let Self(ctx) = self;
97 MulticastForwardingApi::new(ctx)
98 }
99
100 pub fn neighbor<I: Ip, D>(self) -> NeighborApi<I, D, CoreApiCtxPair<'a, BP>> {
102 let Self(ctx) = self;
103 NeighborApi::new(ctx)
104 }
105
106 pub fn device<D>(self) -> DeviceApi<D, CoreApiCtxPair<'a, BP>> {
108 let Self(ctx) = self;
109 DeviceApi::new(ctx)
110 }
111
112 pub fn device_any(self) -> DeviceAnyApi<CoreApiCtxPair<'a, BP>> {
114 let Self(ctx) = self;
115 DeviceAnyApi::new(ctx)
116 }
117
118 pub fn device_ip<I: Ip>(self) -> DeviceIpApi<I, CoreApiCtxPair<'a, BP>> {
120 let Self(ctx) = self;
121 DeviceIpApi::new(ctx)
122 }
123
124 pub fn device_ip_any(self) -> DeviceIpAnyApi<CoreApiCtxPair<'a, BP>> {
126 let Self(ctx) = self;
127 DeviceIpAnyApi::new(ctx)
128 }
129
130 pub fn transmit_queue<D>(self) -> TransmitQueueApi<D, CoreApiCtxPair<'a, BP>> {
132 let Self(ctx) = self;
133 TransmitQueueApi::new(ctx)
134 }
135
136 pub fn receive_queue<D>(self) -> ReceiveQueueApi<D, CoreApiCtxPair<'a, BP>> {
138 let Self(ctx) = self;
139 ReceiveQueueApi::new(ctx)
140 }
141
142 pub fn counters(self) -> CountersApi<CoreApiCtxPair<'a, BP>> {
144 let Self(ctx) = self;
145 CountersApi::new(ctx)
146 }
147
148 pub fn handle_timer(
150 &mut self,
151 dispatch: TimerId<BP::Context>,
152 timer: <BP::Context as TimerBindingsTypes>::UniqueTimerId,
153 ) where
154 BP::Context: crate::BindingsContext,
155 {
156 let Self(ctx) = self;
157 let (core_ctx, bindings_ctx) = ctx.contexts();
158
159 core_ctx.handle_timer(bindings_ctx, dispatch, timer)
160 }
161}