Android-cuttlefish cvd tool
alloc_utils.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <stdint.h>
20
21#include <string>
22
23#include "absl/strings/string_view.h"
24
26
27namespace cuttlefish {
28
29// Max ID an interface can have
30// Note: Interface names only have 2 digits in addition to the username prefix
31// Additionally limited by available netmask values in MobileNetworkName
32// Exceeding 63 would result in an overflow when calculating the netmask
33inline constexpr uint32_t kMaxIfaceNameId = 63;
34
35// Netmask of the per-instance mobile and wifiap networks.
36inline constexpr char kMobileNetmask[] = "/30";
37
38// struct for managing configuration state
40 bool has_gateway = false;
41 bool has_dnsmasq = false;
42};
43
45
46bool CreateTap(absl::string_view name);
47
48#ifdef __linux__
49Result<void> ValidateTapInterfaceIsUsable(const std::string& interface_name);
50#endif
51
52bool DestroyIface(absl::string_view name);
53
54bool DestroyBridge(absl::string_view name);
55
56std::string MobileNetworkName(absl::string_view ipaddr,
57 absl::string_view netmask, uint16_t id);
58
59bool CreateMobileIface(absl::string_view name, uint16_t id,
60 absl::string_view ipaddr);
61bool DestroyMobileIface(absl::string_view name, uint16_t id,
62 absl::string_view ipaddr);
63
64bool CreateEthernetIface(absl::string_view name, absl::string_view bridge_name);
65bool DestroyEthernetIface(absl::string_view name);
66void CleanupEthernetIface(absl::string_view name);
67
68bool SetupBridgeGateway(absl::string_view name, absl::string_view ipaddr);
69void CleanupBridgeGateway(absl::string_view name, absl::string_view ipaddr,
70 const GatewayConfig& config);
71
72bool CreateEthernetBridgeIface(absl::string_view name,
73 absl::string_view ipaddr);
74bool DestroyEthernetBridgeIface(absl::string_view name,
75 absl::string_view ipaddr);
76
77bool StartDnsmasq(absl::string_view bridge_name, absl::string_view gateway,
78 absl::string_view dhcp_range);
79bool StopDnsmasq(absl::string_view name);
80
81} // namespace cuttlefish
Definition: alloc_driver.h:21
bool StopDnsmasq(absl::string_view name)
Definition: alloc_utils.cpp:286
constexpr char kMobileNetmask[]
Definition: alloc_utils.h:36
bool DestroyEthernetIface(absl::string_view name)
Definition: alloc_utils.cpp:151
std::string MobileNetworkName(absl::string_view ipaddr, absl::string_view netmask, uint16_t id)
Definition: alloc_utils.cpp:110
bool DestroyEthernetBridgeIface(absl::string_view name, absl::string_view ipaddr)
Definition: alloc_utils.cpp:329
Result< std::string > DnsmasqPath()
Definition: alloc_utils.cpp:351
bool CreateEthernetIface(absl::string_view name, absl::string_view bridge_name)
Definition: alloc_utils.cpp:88
bool SetupBridgeGateway(absl::string_view bridge_name, absl::string_view ipaddr)
Definition: alloc_utils.cpp:225
bool CreateEthernetBridgeIface(absl::string_view name, absl::string_view ipaddr)
Definition: alloc_utils.cpp:305
bool DestroyIface(absl::string_view name)
Definition: alloc_utils.cpp:195
bool CreateMobileIface(absl::string_view name, uint16_t id, absl::string_view ipaddr)
Definition: alloc_utils.cpp:117
void CleanupBridgeGateway(absl::string_view name, absl::string_view ipaddr, const GatewayConfig &config)
Definition: alloc_utils.cpp:245
bool StartDnsmasq(absl::string_view bridge_name, absl::string_view gateway, absl::string_view dhcp_range)
Definition: alloc_utils.cpp:259
bool CreateTap(absl::string_view name)
Definition: alloc_utils.cpp:155
void CleanupEthernetIface(absl::string_view name)
Definition: alloc_utils.cpp:153
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30
constexpr uint32_t kMaxIfaceNameId
Definition: alloc_utils.h:33
bool DestroyMobileIface(absl::string_view name, uint16_t id, absl::string_view ipaddr)
Definition: alloc_utils.cpp:138
bool DestroyBridge(absl::string_view name)
Definition: alloc_utils.cpp:211
Definition: alloc_utils.h:39
bool has_gateway
Definition: alloc_utils.h:40
bool has_dnsmasq
Definition: alloc_utils.h:41