Skip to content

Commit 3355478

Browse files
committed
Ensure the instance RIL is set up properly.
assemble_cvd introspects the on-device tap interfaces to populate flags so that the instance RIL gets the right network information. However, when using cvdalloc, the tap devices aren't available at assemble_cvd time, so defaults are used. Unfortunately, these defaults aren't correct. Instead, we do the same thing that cuttlefish-host-resources does: allocates subnetwork parameters based on instance ID. The RIL flags are populated anticipating the network parameters we will give to the tap device when cvdalloc runs. While we are here, we introduce new address ranges to assign to cvdalloc's interfaces, so they can't conflict with addresses assigned to interfaces created by cuttlefish-host-resources. Additionally, alloc_utils now maps instance number to the relevant address in the same way that cuttlefish-host-resources does.
1 parent fe2bff2 commit 3355478

File tree

6 files changed

+51
-8
lines changed

6 files changed

+51
-8
lines changed

base/cvd/allocd/alloc_utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ bool CreateEthernetIface(const std::string& name, const std::string& bridge_name
117117

118118
std::string MobileGatewayName(const std::string& ipaddr, uint16_t id) {
119119
std::stringstream ss;
120-
ss << ipaddr << "." << (4 * id + 1);
120+
ss << ipaddr << "." << (4 * id - 3);
121121
return ss.str();
122122
}
123123

124124
std::string MobileNetworkName(const std::string& ipaddr,
125125
const std::string& netmask, uint16_t id) {
126126
std::stringstream ss;
127-
ss << ipaddr << "." << (4 * id) << netmask;
127+
ss << ipaddr << "." << (4 * id - 4) << netmask;
128128
return ss.str();
129129
}
130130

base/cvd/cuttlefish/host/commands/assemble_cvd/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,11 @@ cf_cc_library(
399399
hdrs = ["network_flags.h"],
400400
deps = [
401401
"//cuttlefish/common/libs/utils:result",
402+
"//cuttlefish/host/commands/cvdalloc:interface",
402403
"//cuttlefish/host/libs/config:cuttlefish_config",
403404
"//libbase",
405+
"@abseil-cpp//absl/strings",
406+
"@abseil-cpp//absl/strings:str_format",
404407
],
405408
)
406409

base/cvd/cuttlefish/host/commands/assemble_cvd/network_flags.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
#include <ifaddrs.h>
2121

2222
#include <android-base/logging.h>
23+
#include "absl/strings/numbers.h"
24+
#include "absl/strings/str_format.h"
25+
26+
#include "cuttlefish/host/commands/cvdalloc/interface.h"
2327

2428
namespace cuttlefish {
2529
namespace {
@@ -131,6 +135,21 @@ Result<void> ConfigureNetworkSettings(
131135
const std::string& ril_dns_arg,
132136
const CuttlefishConfig::InstanceSpecific& const_instance,
133137
CuttlefishConfig::MutableInstanceSpecific& instance) {
138+
// We can't introspect the tap interfaces using cvdalloc because
139+
// they're not set up yet. However, we can expect the following
140+
// instance to address scheme for the mtap interfaces.
141+
if (const_instance.use_cvdalloc()) {
142+
int num;
143+
CF_EXPECT(absl::SimpleAtoi(const_instance.id(), &num));
144+
145+
instance.set_ril_gateway(InstanceToMobileGatewayAddress(num));
146+
instance.set_ril_ipaddr(InstanceToMobileAddress(num));
147+
instance.set_ril_broadcast(InstanceToMobileBroadcast(num));
148+
instance.set_ril_dns("8.8.8.8");
149+
instance.set_ril_prefixlen(30);
150+
return {};
151+
}
152+
134153
NetConfig netconfig;
135154
// Check the mobile bridge first; this was the traditional way we configured
136155
// the mobile interface. If that fails, it probably means we are using a

base/cvd/cuttlefish/host/commands/cvdalloc/cvdalloc.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ Result<void> Allocate(int id, const std::string &bridge_name) {
4444
LOG(INFO) << "cvdalloc: allocating network resources";
4545

4646
CreateBridge(bridge_name);
47-
CF_EXPECT(
48-
CreateMobileIface(CvdallocInterfaceName("mtap", id), id, kMobileIp));
49-
CF_EXPECT(
50-
CreateMobileIface(CvdallocInterfaceName("wtap", id), id, kWirelessIp));
47+
CF_EXPECT(CreateMobileIface(CvdallocInterfaceName("mtap", id), id,
48+
kCvdallocMobileIpPrefix));
49+
CF_EXPECT(CreateMobileIface(CvdallocInterfaceName("wtap", id), id,
50+
kCvdallocWirelessIpPrefix));
5151
CF_EXPECT(CreateEthernetIface(CvdallocInterfaceName("etap", id), bridge_name,
5252
true, true, false));
5353

@@ -57,8 +57,10 @@ Result<void> Allocate(int id, const std::string &bridge_name) {
5757
Result<void> Teardown(int id, const std::string &bridge_name) {
5858
LOG(INFO) << "cvdalloc: tearing down resources";
5959

60-
DestroyMobileIface(CvdallocInterfaceName("mtap", id), id, kMobileIp);
61-
DestroyMobileIface(CvdallocInterfaceName("wtap", id), id, kWirelessIp);
60+
DestroyMobileIface(CvdallocInterfaceName("mtap", id), id,
61+
kCvdallocMobileIpPrefix);
62+
DestroyMobileIface(CvdallocInterfaceName("wtap", id), id,
63+
kCvdallocWirelessIpPrefix);
6264
DestroyEthernetIface(CvdallocInterfaceName("etap", id), true, true, false);
6365
DestroyBridge(bridge_name);
6466

base/cvd/cuttlefish/host/commands/cvdalloc/interface.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,16 @@ std::string CvdallocInterfaceName(const std::string &name, int num) {
2323
return absl::StrFormat("%s-%s%d", kCvdallocInterfacePrefix, name, num);
2424
}
2525

26+
std::string InstanceToMobileGatewayAddress(int num) {
27+
return absl::StrFormat("%s.%d", kCvdallocMobileIpPrefix, 4 * num - 3);
28+
}
29+
30+
std::string InstanceToMobileAddress(int num) {
31+
return absl::StrFormat("%s.%d", kCvdallocMobileIpPrefix, 4 * num - 2);
32+
}
33+
34+
std::string InstanceToMobileBroadcast(int num) {
35+
return absl::StrFormat("%s.%d", kCvdallocMobileIpPrefix, 4 * num - 1);
36+
}
37+
2638
} // namespace cuttlefish

base/cvd/cuttlefish/host/commands/cvdalloc/interface.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@
2020
namespace cuttlefish {
2121

2222
constexpr std::string_view kCvdallocInterfacePrefix = "cvd-pi";
23+
constexpr char kCvdallocMobileIpPrefix[] = "192.168.144";
24+
constexpr char kCvdallocWirelessIpPrefix[] = "192.168.160";
25+
constexpr char kCvdallocWirelessApIpPrefix[] = "192.168.176";
26+
constexpr char kCvdallocEthernetIpPrefix[] = "192.168.192";
2327

2428
std::string CvdallocInterfaceName(const std::string &name, int num);
29+
std::string InstanceToMobileGatewayAddress(int num);
30+
std::string InstanceToMobileAddress(int num);
31+
std::string InstanceToMobileBroadcast(int num);
2532

2633
} // namespace cuttlefish

0 commit comments

Comments
 (0)