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.wireless_tap.name = StrForInstance("cvd-wtap-", num);
41 config.wireless_tap.resource_id = 0;
42 config.wireless_tap.session_id = 0;
43
44 config.ethernet_tap.name = StrForInstance("cvd-etap-", num);
45 config.ethernet_tap.resource_id = 0;
46 config.ethernet_tap.session_id = 0;
47
48 return config;
49 }
50
AllocateNetworkInterfaces()51 std::optional<IfaceConfig> AllocateNetworkInterfaces() {
52 IfaceConfig config{};
53
54 SharedFD allocd_sock = SharedFD::SocketLocalClient(
55 kDefaultLocation, false, SOCK_STREAM);
56 CHECK(allocd_sock->IsOpen())
57 << "Unable to connect to allocd on " << kDefaultLocation
58 << ": " << allocd_sock->StrError();
59
60 Json::Value resource_config;
61 Json::Value request_list;
62 Json::Value req;
63 req["request_type"] = "create_interface";
64 req["uid"] = geteuid();
65 req["iface_type"] = "mtap";
66 request_list.append(req);
67 req["iface_type"] = "wtap";
68 request_list.append(req);
69 req["iface_type"] = "etap";
70 request_list.append(req);
71
72 resource_config["config_request"]["request_list"] = request_list;
73
74 CHECK(SendJsonMsg(allocd_sock, resource_config))
75 << "Failed to send JSON to allocd";
76
77 auto resp_opt = RecvJsonMsg(allocd_sock);
78 CHECK(resp_opt.has_value()) << "Bad response from allocd";
79 auto resp = resp_opt.value();
80
81 CHECK(resp.isMember("config_status") && !resp["config_status"].isString())
82 << "Bad response from allocd: " << resp;
83
84 CHECK_EQ(
85 resp["config_status"].asString(),
86 StatusToStr(RequestStatus::Success))
87 <<"Failed to allocate interfaces " << resp;
88
89 CHECK(resp.isMember("session_id") && resp["session_id"].isUInt())
90 << "Bad response from allocd: " << resp;
91 auto session_id = resp["session_id"].asUInt();
92
93 CHECK(resp.isMember("response_list") && resp["response_list"].isArray())
94 << "Bad response from allocd: " << resp;
95
96 Json::Value resp_list = resp["response_list"];
97 Json::Value mtap_resp;
98 Json::Value wtap_resp;
99 Json::Value etap_resp;
100 for (Json::Value::ArrayIndex i = 0; i != resp_list.size(); ++i) {
101 auto ty = StrToIfaceTy(resp_list[i]["iface_type"].asString());
102
103 switch (ty) {
104 case IfaceType::mtap: {
105 mtap_resp = resp_list[i];
106 break;
107 }
108 case IfaceType::wtap: {
109 wtap_resp = resp_list[i];
110 break;
111 }
112 case IfaceType::etap: {
113 etap_resp = resp_list[i];
114 break;
115 }
116 default: {
117 break;
118 }
119 }
120 }
121
122 if (!mtap_resp.isMember("iface_type")) {
123 LOG(ERROR) << "Missing mtap response from allocd";
124 return std::nullopt;
125 }
126 if (!wtap_resp.isMember("iface_type")) {
127 LOG(ERROR) << "Missing wtap response from allocd";
128 return std::nullopt;
129 }
130 if (!etap_resp.isMember("iface_type")) {
131 LOG(ERROR) << "Missing etap response from allocd";
132 return std::nullopt;
133 }
134
135 config.mobile_tap.name = mtap_resp["iface_name"].asString();
136 config.mobile_tap.resource_id = mtap_resp["resource_id"].asUInt();
137 config.mobile_tap.session_id = session_id;
138
139 config.wireless_tap.name = wtap_resp["iface_name"].asString();
140 config.wireless_tap.resource_id = wtap_resp["resource_id"].asUInt();
141 config.wireless_tap.session_id = session_id;
142
143 config.ethernet_tap.name = etap_resp["iface_name"].asString();
144 config.ethernet_tap.resource_id = etap_resp["resource_id"].asUInt();
145 config.ethernet_tap.session_id = session_id;
146
147 return config;
148 }
149
150 } // namespace cuttlefish
151