Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions base/cvd/allocd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ package(

string_flag(
name = "driver",
build_setting_default = "iproute2",
)

config_setting(
name = "netlink",
flag_values = {
":driver": "netlink",
},
build_setting_default = "netlink",
)

config_setting(
Expand All @@ -29,14 +22,15 @@ cf_cc_library(
srcs = ["alloc_utils.cpp"],
hdrs = ["alloc_utils.h"],
deps = select({
":netlink": [":alloc_netlink"],
"//conditions:default": [":alloc_iproute2"],
":iproute2": [":alloc_iproute2"],
"//conditions:default": [":alloc_netlink"],
Comment thread
dxapd marked this conversation as resolved.
Resolved
}) + [
"//cuttlefish/common/libs/fs:fd",
"//cuttlefish/common/libs/utils:files",
"//cuttlefish/common/libs/utils:network",
"//cuttlefish/host/commands/cvd/utils:common",
"//cuttlefish/process:execute",
"//cuttlefish/process:proc_file_utils",
"//cuttlefish/result",
"@abseil-cpp//absl/base:no_destructor",
"@abseil-cpp//absl/log",
Expand All @@ -58,10 +52,31 @@ cf_cc_library(
],
)

cf_cc_library(
alias(
name = "alloc_netlink",
actual = select({
"@platforms//os:linux": ":alloc_netlink_linux",
"//conditions:default": ":alloc_netlink_dummy",
}),
)

cf_cc_library(
name = "alloc_netlink_dummy",
srcs = ["alloc_netlink_dummy.cpp"],
hdrs = ["alloc_driver.h"],
deps = [
"//cuttlefish/result",
"@abseil-cpp//absl/strings:string_view",
],
)

cf_cc_library(
name = "alloc_netlink_linux",
srcs = ["alloc_netlink.cpp"],
hdrs = ["alloc_driver.h"],
target_compatible_with = [
"@platforms//os:linux",
],
deps = [
"//allocd/net:netlink",
"//cuttlefish/common/libs/fs:fd",
Expand Down
21 changes: 12 additions & 9 deletions base/cvd/allocd/alloc_netlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,22 @@ Result<void> BringUpIface(absl::string_view name) {
Result<void> AddGateway(absl::string_view name, absl::string_view gateway,
absl::string_view netmask) {
VLOG(0) << "AddGateway: " << name << ", " << gateway << netmask;
auto index = Index(name);
PCHECK(index.has_value()) << "Index: " << name;
const unsigned int index = CF_EXPECT(Index(name));

auto factory = NetlinkClientFactory::Default();
std::unique_ptr<NetlinkClient> nl = factory->New(NETLINK_ROUTE);

const int prefix = Prefix(netmask);
NetlinkRequest req(RTM_NEWADDR, NLM_F_REQUEST | NLM_F_ACK);
req.AddAddrInfo(*index, Prefix(netmask));
req.AddAddrInfo(index, prefix);
in_addr_t inaddr = inet_addr(std::string(gateway).c_str());
req.AddInAddr(IFA_LOCAL, &inaddr); // NOLINT(misc-include-cleaner): netlink
req.AddInAddr(IFA_ADDRESS, &inaddr); // NOLINT(misc-include-cleaner): netlink
// Same as iproute2's "broadcast +".
if (prefix <= 30) {
in_addr_t broadcast = inaddr | htonl(UINT32_MAX >> prefix);
req.AddInAddr(IFA_BROADCAST, &broadcast); // NOLINT(misc-include-cleaner)
}

CF_EXPECT(nl->Send(req), "AddGateway");
return {};
Expand Down Expand Up @@ -165,17 +170,15 @@ Result<void> DestroyGateway(absl::string_view name, absl::string_view gateway,
Result<void> LinkTapToBridge(absl::string_view tap_name,
absl::string_view bridge_name) {
VLOG(0) << "LinkTapToBridge: " << tap_name << ", " << bridge_name;
auto tap_index = Index(tap_name);
PCHECK(tap_index.has_value()) << "Index: " << tap_name;
auto bridge_index = Index(bridge_name);
PCHECK(bridge_index.has_value()) << "Index: " << bridge_name;
const unsigned int tap_index = CF_EXPECT(Index(tap_name));
const unsigned int bridge_index = CF_EXPECT(Index(bridge_name));

auto factory = NetlinkClientFactory::Default();
std::unique_ptr<NetlinkClient> nl = factory->New(NETLINK_ROUTE);

NetlinkRequest req(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK);
req.AddIfInfo(*tap_index, true);
req.AddInt(IFLA_MASTER, *bridge_index);
req.AddIfInfo(tap_index, true);
req.AddInt(IFLA_MASTER, bridge_index);

CF_EXPECT(nl->Send(req), "LinkTapToBridge");
return {};
Expand Down
56 changes: 56 additions & 0 deletions base/cvd/allocd/alloc_netlink_dummy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "absl/strings/string_view.h"

#include "allocd/alloc_driver.h"
#include "cuttlefish/result/result.h"

namespace cuttlefish {
namespace {

constexpr char kLinuxOnly[] = "The netlink driver only supports Linux";

} // namespace

Result<void> AddTapIface(absl::string_view) { return CF_ERR(kLinuxOnly); }

Result<void> ShutdownIface(absl::string_view) { return CF_ERR(kLinuxOnly); }

Result<void> BringUpIface(absl::string_view) { return CF_ERR(kLinuxOnly); }

Result<void> AddGateway(absl::string_view, absl::string_view,
absl::string_view) {
return CF_ERR(kLinuxOnly);
}

Result<void> DestroyGateway(absl::string_view, absl::string_view,
absl::string_view) {
return CF_ERR(kLinuxOnly);
}

Result<void> LinkTapToBridge(absl::string_view, absl::string_view) {
return CF_ERR(kLinuxOnly);
}

Result<void> DeleteIface(absl::string_view) { return CF_ERR(kLinuxOnly); }

Result<bool> BridgeInUse(absl::string_view) { return CF_ERR(kLinuxOnly); }

Result<bool> BridgeExists(absl::string_view) { return CF_ERR(kLinuxOnly); }

Result<void> CreateBridge(absl::string_view) { return CF_ERR(kLinuxOnly); }

} // namespace cuttlefish
61 changes: 42 additions & 19 deletions base/cvd/allocd/alloc_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
#include <net/if.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>

#include <fstream>
#include <optional>
#include <sstream>
#include <string>

#include "absl/base/no_destructor.h"
#include "absl/log/log.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
Expand All @@ -36,6 +38,7 @@
#include "cuttlefish/common/libs/utils/network.h"
#include "cuttlefish/host/commands/cvd/utils/common.h"
#include "cuttlefish/process/execute.h"
#include "cuttlefish/process/proc_file_utils.h"
#include "cuttlefish/result/result.h"

#ifdef __linux__
Expand All @@ -55,6 +58,31 @@ Result<std::string> SearchForDnsmasq() {
return CF_EXPECT(Search({"/usr/sbin", "/sbin"}, "dnsmasq"));
}

std::string DnsmasqPidFile(absl::string_view bridge_name) {
return absl::StrCat(CvdDir(), "/cuttlefish-dnsmasq-", bridge_name, ".pid");
}

std::optional<pid_t> ReadDnsmasqPid(absl::string_view bridge_name) {
const Result<std::string> contents =
ReadFileContents(DnsmasqPidFile(bridge_name));
pid_t pid = 0;
if (!contents.has_value() || !absl::SimpleAtoi(*contents, &pid) || pid <= 0) {
return std::nullopt;
}
return pid;
}

bool DnsmasqAlive(absl::string_view bridge_name) {
const std::optional<pid_t> pid = ReadDnsmasqPid(bridge_name);
return pid.has_value() && ProcessAlive(*pid);
}

bool StartBridgeDnsmasq(absl::string_view bridge_name,
absl::string_view ipaddr) {
return StartDnsmasq(bridge_name, absl::StrFormat("%s.1", ipaddr),
absl::StrFormat("%s.2,%s.255", ipaddr, ipaddr));
}

} // namespace

bool CreateEthernetIface(absl::string_view name,
Expand Down Expand Up @@ -199,15 +227,14 @@ bool SetupBridgeGateway(absl::string_view bridge_name,
GatewayConfig config{false, false};
auto gateway = absl::StrFormat("%s.1", ipaddr);
auto netmask = "/24";
auto dhcp_range = absl::StrFormat("%s.2,%s.255", ipaddr, ipaddr);

if (!AddGateway(bridge_name, gateway, netmask).has_value()) {
return false;
}

config.has_gateway = true;

if (!StartDnsmasq(bridge_name, gateway, dhcp_range)) {
if (!StartBridgeDnsmasq(bridge_name, ipaddr)) {
CleanupBridgeGateway(bridge_name, ipaddr, config);
return false;
}
Expand Down Expand Up @@ -250,31 +277,23 @@ bool StartDnsmasq(absl::string_view bridge_name, absl::string_view gateway,
absl::StrCat("--dhcp-option=option:dns-server,", dns_servers),
absl::StrCat("--dhcp-option=option6:dns-server,", dns6_servers),
"--conf-file=",
absl::StrCat("--pid-file=", CvdDir(), "/cuttlefish-dnsmasq-",
bridge_name, ".pid"),
absl::StrCat("--pid-file=", DnsmasqPidFile(bridge_name)),
absl::StrCat("--dhcp-leasefile=", CvdDir(),
"/cuttlefish-dnsmasq-", bridge_name, ".leases"),
"--dhcp-no-override"}) == 0;
}

bool StopDnsmasq(absl::string_view name) {
std::ifstream file;
std::string filename =
absl::StrCat(CvdDir(), "/cuttlefish-dnsmasq-", name, ".pid");
LOG(INFO) << "stopping dnsmasq for interface: " << name;
file.open(filename);
if (!file.is_open()) {
LOG(INFO) << "dnsmasq file:" << filename
<< " could not be opened, assume dnsmasq has already stopped";
const std::optional<pid_t> pid = ReadDnsmasqPid(name);
if (!pid.has_value()) {
LOG(INFO) << "dnsmasq file:" << DnsmasqPidFile(name)
<< " is missing or empty, assume dnsmasq has already stopped";
return true;
}

std::string pid;
file >> pid;
file.close();

// TODO: Let's use kill(2) instead of subjecting ourselves to this.
bool ret = Execute({"kill", pid}) == 0;
const bool ret = Execute({"kill", std::to_string(*pid)}) == 0;
if (ret) {
LOG(INFO) << "dnsmasq for:" << name << "successfully stopped";
} else {
Expand All @@ -287,8 +306,12 @@ bool CreateEthernetBridgeIface(absl::string_view name,
absl::string_view ipaddr) {
auto exists = BridgeExists(name);
if (exists.has_value() && *exists) {
LOG(INFO) << "Bridge " << name << " exists already, doing nothing.";
return true;
LOG(INFO) << "Bridge " << name << " exists already, reusing it.";
if (DnsmasqAlive(name)) {
return true;
}
LOG(INFO) << "Restarting dnsmasq for bridge " << name;
return StartBridgeDnsmasq(name, ipaddr);
}

if (!CreateBridge(name).has_value()) {
Expand Down
2 changes: 2 additions & 0 deletions base/cvd/cuttlefish/host/commands/cvdalloc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ cf_cc_library(
deps = [
"//cuttlefish/common/libs/fs",
"//cuttlefish/host/libs/command_util",
"//cuttlefish/posix:strerror",
"//cuttlefish/result:expect",
"//cuttlefish/result:result_type",
],
Expand Down Expand Up @@ -57,6 +58,7 @@ cf_cc_binary(
"//allocd/net:nft_nat_table",
"//cuttlefish/common/libs/fs",
"//cuttlefish/common/libs/fs:fd",
"//cuttlefish/common/libs/utils:network",
"//cuttlefish/posix:strerror",
"//cuttlefish/posix:temp_failure_retry",
"//cuttlefish/result:expect",
Expand Down
Loading
Loading