Android-cuttlefish cvd tool
resource.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 <sys/types.h>
20
21#include <cstdint>
22#include <string>
23
24namespace cuttlefish {
25
26enum class ResourceType {
27 Invalid = 0,
31};
32
34 public:
35 StaticResource() = default;
36 StaticResource(const std::string& name, uid_t uid, ResourceType ty,
37 uint32_t global_id)
38 : name_(name), uid_(uid), global_id_(global_id), ty_(ty){};
39 virtual ~StaticResource() = default;
40 virtual bool ReleaseResource() = 0;
41 virtual bool AcquireResource() = 0;
42
43 std::string GetName() { return name_; }
44 uid_t GetUid() { return uid_; }
46 uint32_t GetGlobalID() { return global_id_; }
47
48 private:
49 std::string name_{};
50 uid_t uid_{};
51 uint32_t global_id_{};
53};
54
56 public:
57 MobileIface() = default;
58 ~MobileIface() = default;
59 MobileIface(const std::string& name, uid_t uid, uint16_t iface_id,
60 uint32_t global_id, std::string ipaddr)
61 : StaticResource(name, uid, ResourceType::MobileIface, global_id),
62 iface_id_(iface_id),
63 ipaddr_(ipaddr) {}
64
65 bool ReleaseResource() override;
66 bool AcquireResource() override;
67
68 uint16_t GetIfaceId() { return iface_id_; }
69 std::string GetIpAddr() { return ipaddr_; }
70
71 static constexpr char kNetmask[] = "/30";
72
73 private:
74 uint16_t iface_id_;
75 std::string ipaddr_;
76};
77
79 public:
80 EthernetIface() = default;
81 ~EthernetIface() = default;
82
83 EthernetIface(const std::string& name, uid_t uid, uint16_t iface_id,
84 uint32_t global_id, std::string bridge_name,
85 std::string ipaddr)
86 : StaticResource(name, uid, ResourceType::MobileIface, global_id),
87 iface_id_(iface_id),
88 bridge_name_(bridge_name),
89 ipaddr_(ipaddr) {}
90
91 bool ReleaseResource() override;
92 bool AcquireResource() override;
93
94 uint16_t GetIfaceId() { return iface_id_; }
95
96 std::string GetBridgeName() { return bridge_name_; }
97 std::string GetIpAddr() { return ipaddr_; }
98
99 void SetHasIpv4(bool ipv4) { has_ipv4_ = ipv4; }
100 void SetHasIpv6(bool ipv6) { has_ipv6_ = ipv6; }
101 void SetUseEbtablesLegacy(bool use_legacy) {
102 use_ebtables_legacy_ = use_legacy;
103 }
104
105 bool GetHasIpv4() { return has_ipv4_; }
106 bool GetHasIpv6() { return has_ipv6_; }
108
109 private:
110 static constexpr char kNetmask[] = "/24";
111 uint16_t iface_id_;
112 std::string bridge_name_;
113 std::string ipaddr_;
114 bool has_ipv4_ = true;
115 bool has_ipv6_ = true;
117};
118
119} // namespace cuttlefish
Definition: resource.h:78
static constexpr char kNetmask[]
Definition: resource.h:110
std::string GetIpAddr()
Definition: resource.h:97
bool has_ipv6_
Definition: resource.h:115
std::string GetBridgeName()
Definition: resource.h:96
uint16_t GetIfaceId()
Definition: resource.h:94
bool AcquireResource() override
Definition: resource.cpp:33
void SetHasIpv4(bool ipv4)
Definition: resource.h:99
void SetHasIpv6(bool ipv6)
Definition: resource.h:100
std::string bridge_name_
Definition: resource.h:112
bool GetHasIpv4()
Definition: resource.h:105
bool use_ebtables_legacy_
Definition: resource.h:116
std::string ipaddr_
Definition: resource.h:113
bool has_ipv4_
Definition: resource.h:114
bool GetUseEbtablesLegacy()
Definition: resource.h:107
bool GetHasIpv6()
Definition: resource.h:106
EthernetIface(const std::string &name, uid_t uid, uint16_t iface_id, uint32_t global_id, std::string bridge_name, std::string ipaddr)
Definition: resource.h:83
bool ReleaseResource() override
Definition: resource.cpp:38
void SetUseEbtablesLegacy(bool use_legacy)
Definition: resource.h:101
uint16_t iface_id_
Definition: resource.h:111
Definition: resource.h:55
uint16_t GetIfaceId()
Definition: resource.h:68
bool AcquireResource() override
Definition: resource.cpp:25
static constexpr char kNetmask[]
Definition: resource.h:71
std::string ipaddr_
Definition: resource.h:75
std::string GetIpAddr()
Definition: resource.h:69
bool ReleaseResource() override
Definition: resource.cpp:29
uint16_t iface_id_
Definition: resource.h:74
MobileIface(const std::string &name, uid_t uid, uint16_t iface_id, uint32_t global_id, std::string ipaddr)
Definition: resource.h:59
Definition: resource.h:33
uint32_t global_id_
Definition: resource.h:51
uint32_t GetGlobalID()
Definition: resource.h:46
uid_t GetUid()
Definition: resource.h:44
ResourceType GetResourceType()
Definition: resource.h:45
virtual ~StaticResource()=default
ResourceType ty_
Definition: resource.h:52
virtual bool ReleaseResource()=0
std::string name_
Definition: resource.h:49
virtual bool AcquireResource()=0
uid_t uid_
Definition: resource.h:50
std::string GetName()
Definition: resource.h:43
StaticResource(const std::string &name, uid_t uid, ResourceType ty, uint32_t global_id)
Definition: resource.h:36
Definition: alloc_utils.cpp:23
ResourceType
Definition: resource.h:26