Android-cuttlefish cvd tool
resource_manager.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 <atomic>
22#include <cstdint>
23#include <map>
24#include <memory>
25#include <optional>
26#include <set>
27
28#include "alloc_utils.h"
30#include "request.h"
31#include "resource.h"
32#include "utils.h"
33
34namespace cuttlefish {
35
36class Session {
37 public:
38 explicit Session(uint32_t session_id, uid_t uid)
39 : session_id_(session_id), uid_(uid) {}
41
42 uint32_t GetSessionID() { return session_id_; }
43 uid_t GetUID() { return uid_; }
44
45 const std::set<std::string>& GetActiveInterfaces() {
46 return active_interfaces_;
47 }
48
49 void Insert(
50 const std::map<uint32_t, std::shared_ptr<StaticResource>>& resources) {
51 managed_resources_.insert(resources.begin(), resources.end());
52 }
53
55 bool success = true;
56 for (auto& res : managed_resources_) {
57 success &= res.second->ReleaseResource();
58 }
59 managed_resources_.clear();
60
61 return success;
62 }
63
64 bool ReleaseResource(uint32_t resource_id) {
65 auto it = managed_resources_.find(resource_id);
66 if (it == managed_resources_.end()) {
67 return false;
68 }
69
70 auto success = it->second->ReleaseResource();
71 if (success) {
72 managed_resources_.erase(it);
73 }
74
75 return success;
76 }
77
78 private:
79 uint32_t session_id_{};
80 uid_t uid_{};
81 std::set<std::string> active_interfaces_;
82 std::map<uint32_t, std::shared_ptr<StaticResource>> managed_resources_;
83};
84
85/* Manages static resources while the daemon is running.
86 * When resources, such as network interfaces are requested the ResourceManager
87 * allocates the resources and takes ownership of them. It will keep maintain
88 * the resource, until requested to release it(i.e. destroy it and/or tear down
89 * related config). When the daemon is stopped, it will walk its list of owned
90 * resources, and deallocate them from the system.
91 *
92 * Clients can request new resources by connecting to a socket, and sending a
93 * JSON request, detailing the type of resource required.
94 */
96 public:
97 ResourceManager() = default;
98
100
101 void SetSocketLocation(const std::string& sock_name);
102
103 void SetUseEbtablesLegacy(bool use_legacy);
104
105 void JsonServer();
106
107 private:
108 uint32_t AllocateResourceID();
109 uint32_t AllocateSessionID();
110
111 bool AddInterface(const std::string& iface, IfaceType ty, uint32_t id,
112 uid_t uid);
113
114 bool RemoveInterface(const std::string& iface, IfaceType ty);
115
116 bool ValidateRequest(const Json::Value& request);
117
118 bool ValidateRequestList(const Json::Value& config);
119
120 bool ValidateConfigRequest(const Json::Value& config);
121
122 Json::Value JsonHandleIdRequest();
123
124 Json::Value JsonHandleShutdownRequest(SharedFD client_socket);
125
126 Json::Value JsonHandleCreateInterfaceRequest(SharedFD client_socket,
127 const Json::Value& request);
128
129 Json::Value JsonHandleDestroyInterfaceRequest(const Json::Value& request);
130
131 Json::Value JsonHandleStopSessionRequest(const Json::Value& request,
132 uid_t uid);
133
134 bool CheckCredentials(SharedFD client_socket, uid_t uid);
135
136 void SetUseIpv4Bridge(bool ipv4) { use_ipv4_bridge_ = ipv4; }
137
138 void SetUseIpv6Bridge(bool ipv6) { use_ipv6_bridge_ = ipv6; }
139
140 std::optional<std::shared_ptr<Session>> FindSession(uint32_t id);
141
142 private:
143 std::atomic_uint32_t global_resource_id_ = 0;
144 std::atomic_uint32_t session_id_ = 0;
145 std::set<std::string> active_interfaces_;
146 std::map<uint32_t, std::shared_ptr<Session>> managed_sessions_;
147 std::map<uint32_t, std::shared_ptr<StaticResource>> pending_add_;
149 bool use_ipv4_bridge_ = true;
150 bool use_ipv6_bridge_ = true;
153};
154
155} // namespace cuttlefish
Definition: resource_manager.h:36
uid_t GetUID()
Definition: resource_manager.h:43
void Insert(const std::map< uint32_t, std::shared_ptr< StaticResource > > &resources)
Definition: resource_manager.h:49
uint32_t GetSessionID()
Definition: resource_manager.h:42
std::set< std::string > active_interfaces_
Definition: resource_manager.h:81
Session(uint32_t session_id, uid_t uid)
Definition: resource_manager.h:38
const std::set< std::string > & GetActiveInterfaces()
Definition: resource_manager.h:45
bool ReleaseResource(uint32_t resource_id)
Definition: resource_manager.h:64
~Session()
Definition: resource_manager.h:40
std::map< uint32_t, std::shared_ptr< StaticResource > > managed_resources_
Definition: resource_manager.h:82
bool ReleaseAllResources()
Definition: resource_manager.h:54
uint32_t session_id_
Definition: resource_manager.h:79
uid_t uid_
Definition: resource_manager.h:80
Definition: shared_fd.h:129
Definition: alloc_utils.cpp:23
constexpr char kDefaultLocation[]
Definition: utils.h:31
IfaceType
Defines interface types supported by allocd.
Definition: request.h:44
Definition: resource_manager.h:95
uint32_t AllocateSessionID()
Definition: resource_manager.cpp:74
~ResourceManager()
Definition: resource_manager.cpp:47
void SetUseIpv4Bridge(bool ipv4)
Definition: resource_manager.h:136
bool RemoveInterface(const std::string &iface, IfaceType ty)
Definition: resource_manager.cpp:145
uint32_t AllocateResourceID()
Definition: resource_manager.cpp:70
std::map< uint32_t, std::shared_ptr< StaticResource > > pending_add_
Definition: resource_manager.h:147
Json::Value JsonHandleDestroyInterfaceRequest(const Json::Value &request)
Definition: resource_manager.cpp:485
void JsonServer()
Definition: resource_manager.cpp:232
bool ValidateRequestList(const Json::Value &config)
Definition: resource_manager.cpp:187
void SetUseIpv6Bridge(bool ipv6)
Definition: resource_manager.h:138
bool ValidateConfigRequest(const Json::Value &config)
Definition: resource_manager.cpp:210
cuttlefish::SharedFD shutdown_socket_
Definition: resource_manager.h:152
bool CheckCredentials(SharedFD client_socket, uid_t uid)
Definition: resource_manager.cpp:382
std::map< uint32_t, std::shared_ptr< Session > > managed_sessions_
Definition: resource_manager.h:146
void SetUseEbtablesLegacy(bool use_legacy)
Definition: resource_manager.cpp:66
bool use_ebtables_legacy_
Definition: resource_manager.h:151
bool use_ipv6_bridge_
Definition: resource_manager.h:150
std::atomic_uint32_t global_resource_id_
Definition: resource_manager.h:143
void SetSocketLocation(const std::string &sock_name)
Definition: resource_manager.cpp:62
std::optional< std::shared_ptr< Session > > FindSession(uint32_t id)
Definition: resource_manager.cpp:613
Json::Value JsonHandleShutdownRequest(SharedFD client_socket)
Definition: resource_manager.cpp:407
bool AddInterface(const std::string &iface, IfaceType ty, uint32_t id, uid_t uid)
Definition: resource_manager.cpp:78
Json::Value JsonHandleCreateInterfaceRequest(SharedFD client_socket, const Json::Value &request)
Definition: resource_manager.cpp:419
Json::Value JsonHandleIdRequest()
Definition: resource_manager.cpp:399
bool ValidateRequest(const Json::Value &request)
Definition: resource_manager.cpp:222
std::atomic_uint32_t session_id_
Definition: resource_manager.h:144
std::string location_
Definition: resource_manager.h:148
std::set< std::string > active_interfaces_
Definition: resource_manager.h:145
bool use_ipv4_bridge_
Definition: resource_manager.h:149
Json::Value JsonHandleStopSessionRequest(const Json::Value &request, uid_t uid)
Definition: resource_manager.cpp:557