Skip to main content

netstack3/
main.rs

1// Copyright 2018 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//! A networking stack.
6#![warn(clippy::unused_async)]
7#![warn(missing_docs, unreachable_patterns, unused)]
8#![recursion_limit = "256"]
9
10mod bindings;
11
12use std::num::NonZeroU8;
13
14use fidl::endpoints::RequestStream as _;
15use fidl_fuchsia_process_lifecycle as fprocess_lifecycle;
16use fuchsia_async as fasync;
17use fuchsia_async::SendExecutorBuilder;
18use fuchsia_component::server::{ServiceFs, ServiceFsDir};
19use futures::{Future, StreamExt as _};
20use log::{error, info};
21
22use bindings::{GlobalConfig, InspectPublisher, InterfaceConfigDefaults, NetstackSeed, Service};
23
24/// Runs Netstack3.
25pub fn main() {
26    let config = ns3_config::Config::take_from_startup_handle();
27    let ns3_config::Config {
28        num_threads,
29        debug_logs,
30        opaque_iids,
31        suspend_enabled,
32        sampled_stats_enabled,
33    } = &config;
34    let num_threads = NonZeroU8::new(*num_threads).expect("invalid 0 thread count value");
35    let mut executor = SendExecutorBuilder::new().num_threads(num_threads.get().into()).build();
36
37    let mut log_options = diagnostics_log::PublishOptions::default();
38
39    // NB: netstack3 is usually launched with a 'netstack' moniker already -
40    // which implies an automatic 'netstack' tag. However, the automatic tag has
41    // shown problems when extra tags are present in specific log lines (e.g.
42    // https://fxbug.dev/390252317, https://fxbug.dev/390252218). Given that,
43    // we always initialize with the netstack tag here.
44    log_options = log_options.tags(&["netstack"]);
45
46    if *debug_logs {
47        // When forcing debug logs, disable all the dynamic features from the
48        // logging framework, we want logs pegged at Severity::Debug.
49        log_options = log_options
50            .minimum_severity(diagnostics_log::Severity::Debug)
51            .listen_for_interest_updates(false);
52    }
53    diagnostics_log::initialize(log_options).expect("failed to initialize log");
54
55    fuchsia_trace_provider::trace_provider_create_with_fdio();
56
57    info!("starting netstack3 with {config:?}");
58
59    let mut fs = ServiceFs::new();
60    let _: &mut ServiceFsDir<'_, _> = fs
61        .dir("svc")
62        // TODO(https://fxbug.dev/42076541): This is transitional. Once the
63        // out-of-stack DHCP client is being used by both netstacks, it
64        // should be moved out of the netstack realm and into the network
65        // realm. The trip through Netstack3 allows for availability of DHCP
66        // client to be dependent on Netstack version when using
67        // netstack-proxy.
68        .add_proxy_service::<fidl_fuchsia_net_dhcp::ClientProviderMarker, _>()
69        .add_fidl_service(Service::Control)
70        .add_service_connector(Service::DebugDiagnostics)
71        .add_fidl_service(Service::DebugInterfaces)
72        .add_fidl_service(Service::DnsServerWatcher)
73        .add_fidl_service(Service::FilterControl)
74        .add_fidl_service(Service::FilterState)
75        .add_fidl_service(Service::HealthCheck)
76        .add_fidl_service(Service::Interfaces)
77        .add_fidl_service(Service::InterfacesAdmin)
78        .add_fidl_service(Service::MulticastAdminV4)
79        .add_fidl_service(Service::MulticastAdminV6)
80        .add_fidl_service(Service::NdpWatcher)
81        .add_fidl_service(Service::Neighbor)
82        .add_fidl_service(Service::NeighborController)
83        .add_fidl_service(Service::PacketSocket)
84        .add_fidl_service(Service::PacketCapture)
85        .add_fidl_service(Service::RawSocket)
86        .add_fidl_service(Service::RootFilter)
87        .add_fidl_service(Service::RootInterfaces)
88        .add_fidl_service(Service::RootRoutesV4)
89        .add_fidl_service(Service::RootRoutesV6)
90        .add_fidl_service(Service::RoutesAdminV4)
91        .add_fidl_service(Service::RoutesAdminV6)
92        .add_fidl_service(Service::RoutesState)
93        .add_fidl_service(Service::RoutesStateV4)
94        .add_fidl_service(Service::RoutesStateV6)
95        .add_fidl_service(Service::RouteTableProviderV4)
96        .add_fidl_service(Service::RouteTableProviderV6)
97        .add_fidl_service(Service::RuleTableV4)
98        .add_fidl_service(Service::RuleTableV6)
99        .add_fidl_service(Service::SettingsControl)
100        .add_fidl_service(Service::SettingsState)
101        .add_fidl_service(Service::Socket)
102        .add_fidl_service(Service::SocketControl)
103        .add_fidl_service(Service::SocketDiagnostics)
104        .add_fidl_service(Service::Stack)
105        .add_fidl_service(Service::WakeGroupProvider);
106
107    let seed = NetstackSeed::new(
108        GlobalConfig {
109            suspend_enabled: *suspend_enabled,
110            sampled_stats_enabled: *sampled_stats_enabled,
111        },
112        &InterfaceConfigDefaults { opaque_iids: *opaque_iids },
113    );
114
115    let inspect_publisher = InspectPublisher::new();
116    inspect_publisher
117        .inspector()
118        .root()
119        .record_child("Config", |config_node| config.record_inspect(config_node));
120
121    let _: &mut ServiceFs<_> = fs.take_and_serve_directory_handle().expect("directory handle");
122
123    // Short circuit when we receive a lifecycle stop request.
124    let fs = fs.take_until(get_lifecycle_stop_fut());
125
126    executor.run(seed.serve(fs, inspect_publisher))
127}
128
129/// Takes the lifecycle handle from startup and returns a future that resolves
130/// whenever the system has requested shutdown.
131fn get_lifecycle_stop_fut() -> impl Future<Output = ()> {
132    // Lifecycle handle takes no args, must be set to zero.
133    // See zircon/processargs.h.
134    const LIFECYCLE_HANDLE_ARG: u16 = 0;
135    let handle = fuchsia_runtime::take_startup_handle(fuchsia_runtime::HandleInfo::new(
136        fuchsia_runtime::HandleType::Lifecycle,
137        LIFECYCLE_HANDLE_ARG,
138    ))
139    .expect("missing lifecycle handle");
140
141    async move {
142        let mut request_stream = fprocess_lifecycle::LifecycleRequestStream::from_channel(
143            fasync::Channel::from_channel(handle.into()).into(),
144        );
145        loop {
146            match request_stream.next().await {
147                Some(Ok(fprocess_lifecycle::LifecycleRequest::Stop { control_handle })) => {
148                    info!("received shutdown request");
149                    // Shutdown request is acknowledged by the lifecycle
150                    // channel shutting down. Intentionally leak the channel
151                    // so it'll only be closed on process termination,
152                    // allowing clean process termination to always be
153                    // observed.
154
155                    // Must drop the control_handle to unwrap the
156                    // lifecycle channel.
157                    std::mem::drop(control_handle);
158                    let (inner, _terminated): (_, bool) = request_stream.into_inner();
159                    let inner = std::sync::Arc::try_unwrap(inner)
160                        .expect("failed to retrieve lifecycle channel");
161                    let inner: zx::Channel = inner.into_channel().into_zx_channel();
162                    std::mem::forget(inner);
163                    break;
164                }
165                Some(Err(e)) => error!("observed error in lifecycle request stream: {e:?}"),
166                None => {
167                    // Something really bad must've happened here. We chose
168                    // not to panic because the system must be in a bad
169                    // state, log an error and hold forever.
170                    error!("lifecycle channel closed");
171                    futures::future::pending::<()>().await;
172                }
173            }
174        }
175    }
176}