netstack3_core/
api.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///! Defines the main API entry objects for the exposed API from core.
6use 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
31/// The single entry point for function calls into netstack3 core.
32pub 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    /// Gets access to the UDP API for IP version `I`.
47    pub fn udp<I: Ip>(self) -> UdpApi<I, CoreApiCtxPair<'a, BP>> {
48        let Self(ctx) = self;
49        UdpApi::new(ctx)
50    }
51
52    /// Gets access to the ICMP socket API for IP version `I`.
53    pub fn icmp_echo<I: Ip>(self) -> IcmpEchoSocketApi<I, CoreApiCtxPair<'a, BP>> {
54        let Self(ctx) = self;
55        IcmpEchoSocketApi::new(ctx)
56    }
57
58    /// Gets access to the TCP API for IP version `I`.
59    pub fn tcp<I: Ip>(self) -> TcpApi<I, CoreApiCtxPair<'a, BP>> {
60        let Self(ctx) = self;
61        TcpApi::new(ctx)
62    }
63
64    /// Gets access to the raw IP socket API.
65    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    /// Gets access to the device socket API.
71    pub fn device_socket(self) -> DeviceSocketApi<CoreApiCtxPair<'a, BP>> {
72        let Self(ctx) = self;
73        DeviceSocketApi::new(ctx)
74    }
75
76    /// Gets access to the filtering API.
77    pub fn filter(self) -> FilterApi<CoreApiCtxPair<'a, BP>> {
78        let Self(ctx) = self;
79        FilterApi::new(ctx)
80    }
81
82    /// Gets access to the routes API for IP version `I`.
83    pub fn routes<I: Ip>(self) -> RoutesApi<I, CoreApiCtxPair<'a, BP>> {
84        let Self(ctx) = self;
85        RoutesApi::new(ctx)
86    }
87
88    /// Gets access to the routes API for all IP versions.
89    pub fn routes_any(self) -> RoutesAnyApi<CoreApiCtxPair<'a, BP>> {
90        let Self(ctx) = self;
91        RoutesAnyApi::new(ctx)
92    }
93
94    /// Gets access to the multicast forwarding API for IP version `I`.
95    pub fn multicast_forwarding<I: Ip>(self) -> MulticastForwardingApi<I, CoreApiCtxPair<'a, BP>> {
96        let Self(ctx) = self;
97        MulticastForwardingApi::new(ctx)
98    }
99
100    /// Gets access to the neighbor API for IP version `I` and device `D`.
101    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    /// Gets access to the device API for device type `D`.
107    pub fn device<D>(self) -> DeviceApi<D, CoreApiCtxPair<'a, BP>> {
108        let Self(ctx) = self;
109        DeviceApi::new(ctx)
110    }
111
112    /// Gets access to the device API for all device types.
113    pub fn device_any(self) -> DeviceAnyApi<CoreApiCtxPair<'a, BP>> {
114        let Self(ctx) = self;
115        DeviceAnyApi::new(ctx)
116    }
117
118    /// Gets access to the device IP API for IP version `I``.
119    pub fn device_ip<I: Ip>(self) -> DeviceIpApi<I, CoreApiCtxPair<'a, BP>> {
120        let Self(ctx) = self;
121        DeviceIpApi::new(ctx)
122    }
123
124    /// Gets access to the device IP API for all IP versions.
125    pub fn device_ip_any(self) -> DeviceIpAnyApi<CoreApiCtxPair<'a, BP>> {
126        let Self(ctx) = self;
127        DeviceIpAnyApi::new(ctx)
128    }
129
130    /// Gets access to the transmit queue API for device type `D`.
131    pub fn transmit_queue<D>(self) -> TransmitQueueApi<D, CoreApiCtxPair<'a, BP>> {
132        let Self(ctx) = self;
133        TransmitQueueApi::new(ctx)
134    }
135
136    /// Gets access to the receive queue API for device type `D`.
137    pub fn receive_queue<D>(self) -> ReceiveQueueApi<D, CoreApiCtxPair<'a, BP>> {
138        let Self(ctx) = self;
139        ReceiveQueueApi::new(ctx)
140    }
141
142    /// Gets access to the counters API.
143    pub fn counters(self) -> CountersApi<CoreApiCtxPair<'a, BP>> {
144        let Self(ctx) = self;
145        CountersApi::new(ctx)
146    }
147
148    /// Handles a timer.
149    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}