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
2 changes: 1 addition & 1 deletion base/cvd/allocd/.clang-tidy
35 changes: 10 additions & 25 deletions base/cvd/allocd/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("//cuttlefish/bazel:rules.bzl", "cf_cc_binary", "cf_cc_library")

package(
default_visibility = ["//:android_cuttlefish"],
Expand All @@ -26,15 +26,10 @@ config_setting(
},
)

cc_library(
cf_cc_library(
name = "alloc_utils",
srcs = [
"alloc_utils.cpp",
],
hdrs = [
"alloc_utils.h",
],
copts = ["-std=c++20"],
srcs = ["alloc_utils.cpp"],
hdrs = ["alloc_utils.h"],
deps = select({
":netlink": [":alloc_netlink"],
"//conditions:default": [":alloc_iproute2"],
Expand All @@ -53,15 +48,10 @@ cc_library(
],
)

cc_library(
cf_cc_library(
name = "alloc_iproute2",
srcs = [
"alloc_iproute2.cpp",
],
hdrs = [
"alloc_driver.h",
],
copts = ["-std=c++20"],
srcs = ["alloc_iproute2.cpp"],
hdrs = ["alloc_driver.h"],
deps = [
"//cuttlefish/common/libs/utils:subprocess",
"//cuttlefish/result",
Expand All @@ -71,15 +61,10 @@ cc_library(
],
)

cc_library(
cf_cc_library(
name = "alloc_netlink",
srcs = [
"alloc_netlink.cpp",
],
hdrs = [
"alloc_driver.h",
],
copts = ["-std=c++20"],
srcs = ["alloc_netlink.cpp"],
hdrs = ["alloc_driver.h"],
deps = [
"//allocd/net:netlink",
"//cuttlefish/common/libs/fs",
Expand Down
4 changes: 2 additions & 2 deletions base/cvd/allocd/alloc_iproute2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "allocd/alloc_driver.h"

#include <string>
#include <string_view>

#include "absl/strings/str_cat.h"

#include "allocd/alloc_driver.h"
#include "cuttlefish/common/libs/utils/subprocess.h"
#include "cuttlefish/result/result.h"

Expand Down
20 changes: 13 additions & 7 deletions base/cvd/allocd/alloc_netlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "allocd/alloc_driver.h"

#include <arpa/inet.h>
#include <asm/types.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <linux/if_tun.h>
Expand All @@ -25,22 +26,22 @@
#include <net/if_arp.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/socket.h> // IWYU pragma: keep

#include <cstdint>
#include <memory>
#include <string>
#include <string_view>

#include "absl/log/check.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/strip.h"

#include "allocd/alloc_driver.h"
#include "allocd/net/netlink_client.h"
#include "allocd/net/netlink_request.h"
#include "cuttlefish/common/libs/fs/shared_fd.h"
#include "cuttlefish/common/libs/utils/subprocess.h"
#include "cuttlefish/result/result.h"
Expand Down Expand Up @@ -100,8 +101,10 @@ Result<void> ShutdownIface(std::string_view name) {
auto factory = NetlinkClientFactory::Default();
std::unique_ptr<NetlinkClient> nl = factory->New(NETLINK_ROUTE);

// NOLINTNEXTLINE(misc-include-cleaner): rtnetlink
NetlinkRequest req(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK);
req.AddIfInfo(0, false);
// NOLINTNEXTLINE(misc-include-cleaner): rtnetlink
req.AddString(IFLA_IFNAME, std::string(name));
CF_EXPECT(nl->Send(req), "ShutdownIface");
return {};
Expand All @@ -112,6 +115,7 @@ Result<void> BringUpIface(std::string_view name) {
auto factory = NetlinkClientFactory::Default();
std::unique_ptr<NetlinkClient> nl = factory->New(NETLINK_ROUTE);

// NOLINTNEXTLINE(misc-include-cleaner): rtnetlink
NetlinkRequest req(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK);
req.AddIfInfo(0, true);
req.AddString(IFLA_IFNAME, std::string(name));
Expand All @@ -131,8 +135,8 @@ Result<void> AddGateway(std::string_view name, std::string_view gateway,
NetlinkRequest req(RTM_NEWADDR, NLM_F_REQUEST | NLM_F_ACK);
req.AddAddrInfo(*index, Prefix(netmask));
in_addr_t inaddr = inet_addr(std::string(gateway).c_str());
req.AddInAddr(IFA_LOCAL, &inaddr);
req.AddInAddr(IFA_ADDRESS, &inaddr);
req.AddInAddr(IFA_LOCAL, &inaddr); // NOLINT(misc-include-cleaner): netlink
req.AddInAddr(IFA_ADDRESS, &inaddr); // NOLINT(misc-include-cleaner): netlink

CF_EXPECT(nl->Send(req), "AddGateway");
return {};
Expand Down Expand Up @@ -221,12 +225,14 @@ Result<void> CreateBridge(std::string_view name) {
req.Append(ifinfomsg{
.ifi_type = ARPHRD_NETROM,
});
// NOLINTBEGIN(misc-include-cleaner): rtnetlink
req.AddString(IFLA_IFNAME, std::string(name));
req.PushList(IFLA_LINKINFO);
req.AddString(IFLA_INFO_KIND, "bridge");
req.PushList(IFLA_INFO_DATA);
req.AddInt(IFLA_BR_FORWARD_DELAY, 0);
req.AddInt(IFLA_BR_STP_STATE, 0);
// NOLINTEND(misc-include-cleaner): rtnetlink
req.PopList();
req.PopList();

Expand Down
57 changes: 29 additions & 28 deletions base/cvd/allocd/alloc_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,50 @@
*/
#include "allocd/alloc_utils.h"

#include <fcntl.h>
#include <net/if.h>
#include <pwd.h>
#include <stdint.h>
#include <string.h>

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

#include "absl/base/no_destructor.h"
#include "absl/log/log.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"

#include "allocd/alloc_driver.h"
#include "cuttlefish/common/libs/fs/shared_fd.h"
#include "cuttlefish/common/libs/utils/files.h"
#include "cuttlefish/common/libs/utils/network.h"
#include "cuttlefish/common/libs/utils/subprocess.h"
#include "cuttlefish/common/libs/utils/files.h"
#include "cuttlefish/host/commands/cvd/utils/common.h"
#include "cuttlefish/result/result.h"

#ifdef __linux__
#include <linux/if_tun.h>
#endif

#include <net/if.h>
namespace cuttlefish {

namespace {

cuttlefish::Result<std::string> SearchForIptables() {
cuttlefish::Result<std::string> p =
cuttlefish::Search(cuttlefish::Path(), "iptables");
Result<std::string> SearchForIptables() {
Result<std::string> p = Search(Path(), "iptables");
if (p.ok()) {
return p;
}

return cuttlefish::Search({"/usr/sbin", "/sbin"}, "iptables");
return CF_EXPECT(Search({"/usr/sbin", "/sbin"}, "iptables"));
}

} // namespace

namespace cuttlefish {

bool CreateEthernetIface(std::string_view name, std::string_view bridge_name) {
// assume bridge exists

Expand All @@ -77,8 +80,8 @@ std::string MobileGatewayName(std::string_view ipaddr, uint16_t id) {
return ss.str();
}

std::string MobileNetworkName(std::string_view ipaddr,
std::string_view netmask, uint16_t id) {
std::string MobileNetworkName(std::string_view ipaddr, std::string_view netmask,
uint16_t id) {
std::stringstream ss;
ss << ipaddr << "." << (4 * id - 4) << netmask;
return ss.str();
Expand Down Expand Up @@ -109,8 +112,8 @@ bool CreateMobileIface(std::string_view name, uint16_t id,
}

if (!IptableConfig(*iptables_path, network, true).ok()) {
DestroyGateway(name, gateway, netmask);
DestroyIface(name);
(void)DestroyGateway(name, gateway, netmask);
(void)DestroyIface(name);
return false;
};

Expand All @@ -132,8 +135,8 @@ bool DestroyMobileIface(std::string_view name, uint16_t id,
if (!iptables_path.ok()) {
return false;
}
IptableConfig(*iptables_path, network, false);
DestroyGateway(name, gateway, netmask);
(void)IptableConfig(*iptables_path, network, false);
(void)DestroyGateway(name, gateway, netmask);
return DestroyIface(name);
}

Expand All @@ -150,7 +153,7 @@ bool CreateTap(std::string_view name) {

if (!BringUpIface(name).ok()) {
LOG(WARNING) << "Failed to bring up tap interface: " << name;
DeleteIface(name);
(void)DeleteIface(name);
return false;
}

Expand All @@ -160,9 +163,9 @@ bool CreateTap(std::string_view name) {
#ifdef __linux__
Result<void> ValidateTapInterfaceIsUsable(const std::string& interface_name) {
CF_EXPECT(NetworkInterfaceExists(interface_name),
"Tap interface does not exist. Ensure "
"\"cuttlefish-host-resources.service\" is running (start with "
"\"sudo systemctl start cuttlefish-host-resources.service\").");
"Tap interface does not exist. Ensure "
"\"cuttlefish-host-resources.service\" is running (start with "
"\"sudo systemctl start cuttlefish-host-resources.service\").");

constexpr auto kTunTapDev = "/dev/net/tun";

Expand Down Expand Up @@ -222,8 +225,7 @@ bool DestroyBridge(std::string_view name) {
return DeleteIface(name).ok();
}

bool SetupBridgeGateway(std::string_view bridge_name,
std::string_view ipaddr) {
bool SetupBridgeGateway(std::string_view bridge_name, std::string_view ipaddr) {
Result<std::string> iptables_path = IptablesPath();
if (!iptables_path.ok()) {
return false;
Expand Down Expand Up @@ -267,7 +269,7 @@ void CleanupBridgeGateway(std::string_view name, std::string_view ipaddr,
if (config.has_iptable) {
Result<std::string> iptables_path = IptablesPath();
if (iptables_path.ok()) {
IptableConfig(*iptables_path, network, false);
(void)IptableConfig(*iptables_path, network, false);
}
}

Expand All @@ -276,7 +278,7 @@ void CleanupBridgeGateway(std::string_view name, std::string_view ipaddr,
}

if (config.has_gateway) {
DestroyGateway(name, gateway, netmask);
(void)DestroyGateway(name, gateway, netmask);
}
}

Expand All @@ -302,8 +304,8 @@ bool StartDnsmasq(std::string_view bridge_name, std::string_view gateway,

bool StopDnsmasq(std::string_view name) {
std::ifstream file;
std::string filename = absl::StrFormat(
"/var/run/cuttlefish-dnsmasq-%s.pid", name);
std::string filename =
absl::StrFormat("/var/run/cuttlefish-dnsmasq-%s.pid", name);
LOG(INFO) << "stopping dnsmasq for interface: " << name;
file.open(filename);
if (file.is_open()) {
Expand All @@ -326,8 +328,7 @@ bool StopDnsmasq(std::string_view name) {
return ret;
}

bool CreateEthernetBridgeIface(std::string_view name,
std::string_view ipaddr) {
bool CreateEthernetBridgeIface(std::string_view name, std::string_view ipaddr) {
auto exists = BridgeExists(name);
if (exists.ok() && *exists) {
LOG(INFO) << "Bridge " << name << " exists already, doing nothing.";
Expand Down Expand Up @@ -359,7 +360,7 @@ bool DestroyEthernetBridgeIface(std::string_view name,

Result<std::string> IptablesPath() {
static const absl::NoDestructor<std::string> iptables_path(
SearchForIptables().value_or(""));
SearchForIptables().value_or(""));

CF_EXPECT(!iptables_path->empty(), "could not find iptables");
return *iptables_path;
Expand Down
9 changes: 3 additions & 6 deletions base/cvd/allocd/alloc_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

#pragma once

#include <stdint.h>
#include <pwd.h>
#include <stdint.h>
#include <sys/wait.h>
#include <unistd.h>

Expand Down Expand Up @@ -61,7 +61,6 @@ bool CreateTap(std::string_view name);
Result<void> ValidateTapInterfaceIsUsable(const std::string& interface_name);
#endif


bool DestroyIface(std::string_view name);

bool DestroyBridge(std::string_view name);
Expand All @@ -79,10 +78,8 @@ bool SetupBridgeGateway(std::string_view name, std::string_view ipaddr);
void CleanupBridgeGateway(std::string_view name, std::string_view ipaddr,
const GatewayConfig& config);

bool CreateEthernetBridgeIface(std::string_view name,
std::string_view ipaddr);
bool DestroyEthernetBridgeIface(std::string_view name,
std::string_view ipaddr);
bool CreateEthernetBridgeIface(std::string_view name, std::string_view ipaddr);
bool DestroyEthernetBridgeIface(std::string_view name, std::string_view ipaddr);

bool StartDnsmasq(std::string_view bridge_name, std::string_view gateway,
std::string_view dhcp_range);
Expand Down
1 change: 0 additions & 1 deletion base/cvd/allocd/net/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ cf_cc_library(
"netlink_client.h",
"netlink_request.h",
],
clang_format_enabled = False,
deps = [
"//cuttlefish/common/libs/fs",
"@abseil-cpp//absl/log",
Expand Down
Loading
Loading