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 #include "host/commands/assemble_cvd/alloc.h"
18
19 #include <iomanip>
20 #include <sstream>
21
22 #include "common/libs/fs/shared_fd.h"
23 #include "host/libs/allocd/request.h"
24 #include "host/libs/allocd/utils.h"
25
26 namespace cuttlefish {
27
StrForInstance(const std::string & prefix,int num)28 static std::string StrForInstance(const std::string& prefix, int num) {
29 std::ostringstream stream;
30 stream << prefix << std::setfill('0') << std::setw(2) << num;
31 return stream.str();
32 }
33
DefaultNetworkInterfaces(int num)34 IfaceConfig DefaultNetworkInterfaces(int num) {
35 IfaceConfig config{};
36 config.mobile_tap.name = StrForInstance("cvd-mtap-", num);
37 config.mobile_tap.resource_id = 0;
38 config.mobile_tap.session_id = 0;
39
40 config.bridged_wireless_tap.name = StrForInstance("cvd-wtap-", num);
41 config.bridged_wireless_tap.resource_id = 0;
42 config.bridged_wireless_tap.session_id = 0;
43
44 config.non_bridged_wireless_tap.name = StrForInstance("cvd-wifiap-", num);
45 config.non_bridged_wireless_tap.resource_id = 0;
46 config.non_bridged_wireless_tap.session_id = 0;
47
48 config.ethernet_tap.name = StrForInstance("cvd-etap-", num);
49 config.ethernet_tap.resource_id = 0;
50 config.ethernet_tap.session_id = 0;
51
52 return config;
53 }
54
AllocateNetworkInterfaces()55 std::optional<IfaceConfig> AllocateNetworkInterfaces() {
56 IfaceConfig config{};
57
58 SharedFD allocd_sock = SharedFD::SocketLocalClient(
59 kDefaultLocation, false, SOCK_STREAM);
60 CHECK(allocd_sock->IsOpen())
61 << "Unable to connect to allocd on " << kDefaultLocation
62 << ": " << allocd_sock->StrError();
63
64 Json::Value resource_config;
65 Json::Value request_list;
66 Json::Value req;
67 req["request_type"] = "create_interface";
68 req["uid"] = geteuid();
69 req["iface_type"] = "mtap";
70 request_list.append(req);
71 req["iface_type"] = "wtap";
72 request_list.append(req);
73 req["iface_type"] = "wifiap";
74 request_list.append(req);
75 req["iface_type"] = "etap";
76 request_list.append(req);
77
78 resource_config["config_request"]["request_list"] = request_list;
79
80 CHECK(SendJsonMsg(allocd_sock, resource_config))
81 << "Failed to send JSON to allocd";
82
83 auto resp_opt = RecvJsonMsg(allocd_sock);
84 CHECK(resp_opt.has_value()) << "Bad response from allocd";
85 auto resp = resp_opt.value();
86
87 CHECK(resp.isMember("config_status") && !resp["config_status"].isString())
88 << "Bad response from allocd: " << resp;
89
90 CHECK_EQ(
91 resp["config_status"].asString(),
92 StatusToStr(RequestStatus::Success))
93 <<"Failed to allocate interfaces " << resp;
94
95 CHECK(resp.isMember("session_id") && resp["session_id"].isUInt())
96 << "Bad response from allocd: " << resp;
97 auto session_id = resp["session_id"].asUInt();
98
99 CHECK(resp.isMember("response_list") && resp["response_list"].isArray())
100 << "Bad response from allocd: " << resp;
101
102 Json::Value resp_list = resp["response_list"];
103 Json::Value mtap_resp;
104 Json::Value wtap_resp;
105 Json::Value wifiap_resp;
106 Json::Value etap_resp;
107 for (Json::Value::ArrayIndex i = 0; i != resp_list.size(); ++i) {
108 auto ty = StrToIfaceTy(resp_list[i]["iface_type"].asString());
109
110 switch (ty) {
111 case IfaceType::mtap: {
112 mtap_resp = resp_list[i];
113 break;
114 }
115 case IfaceType::wtap: {
116 wtap_resp = resp_list[i];
117 break;
118 }
119 case IfaceType::wifiap: {
120 wifiap_resp = resp_list[i];
121 break;
122 }
123 case IfaceType::etap: {
124 etap_resp = resp_list[i];
125 break;
126 }
127 default: {
128 break;
129 }
130 }
131 }
132
133 if (!mtap_resp.isMember("iface_type")) {
134 LOG(ERROR) << "Missing mtap response from allocd";
135 return std::nullopt;
136 }
137 if (!wtap_resp.isMember("iface_type")) {
138 LOG(ERROR) << "Missing wtap response from allocd";
139 return std::nullopt;
140 }
141 if (!wifiap_resp.isMember("iface_type")) {
142 LOG(ERROR) << "Missing wifiap response from allocd";
143 return std::nullopt;
144 }
145 if (!etap_resp.isMember("iface_type")) {
146 LOG(ERROR) << "Missing etap response from allocd";
147 return std::nullopt;
148 }
149
150 config.mobile_tap.name = mtap_resp["iface_name"].asString();
151 config.mobile_tap.resource_id = mtap_resp["resource_id"].asUInt();
152 config.mobile_tap.session_id = session_id;
153
154 config.bridged_wireless_tap.name = wtap_resp["iface_name"].asString();
155 config.bridged_wireless_tap.resource_id = wtap_resp["resource_id"].asUInt();
156 config.bridged_wireless_tap.session_id = session_id;
157
158 config.non_bridged_wireless_tap.name = wifiap_resp["iface_name"].asString();
159 config.non_bridged_wireless_tap.resource_id =
160 wifiap_resp["resource_id"].asUInt();
161 config.non_bridged_wireless_tap.session_id = session_id;
162
163 config.ethernet_tap.name = etap_resp["iface_name"].asString();
164 config.ethernet_tap.resource_id = etap_resp["resource_id"].asUInt();
165 config.ethernet_tap.session_id = session_id;
166
167 return config;
168 }
169
170 } // namespace cuttlefish
171