Skip to content

Commit 0b1f69f

Browse files
committed
Convert all alloc drivers to using Result.
Converting everything here requires some careful logic changes; this part should be relatively straightforward.
1 parent bd92155 commit 0b1f69f

File tree

5 files changed

+101
-134
lines changed

5 files changed

+101
-134
lines changed

base/cvd/allocd/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ cc_library(
5252
"alloc_driver.h",
5353
],
5454
deps = [
55+
"//cuttlefish/result",
5556
"@abseil-cpp//absl/strings:str_format",
5657
"@abseil-cpp//absl/log",
5758
],

base/cvd/allocd/alloc_driver.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,24 @@
1717
#include <string>
1818
#include <string_view>
1919

20+
#include "cuttlefish/result/result.h"
21+
2022
namespace cuttlefish {
2123

2224
inline constexpr char kCvdNetworkGroupName[] = "cvdnetwork";
2325

24-
bool AddTapIface(std::string_view name);
25-
bool ShutdownIface(std::string_view name);
26-
bool BringUpIface(std::string_view name);
27-
bool AddGateway(std::string_view name, std::string_view gateway,
28-
std::string_view netmask);
29-
bool DestroyGateway(std::string_view name, std::string_view gateway,
30-
std::string_view netmask);
31-
bool LinkTapToBridge(std::string_view tap_name, std::string_view bridge_name);
32-
bool DeleteIface(std::string_view name);
33-
bool BridgeExists(std::string_view name);
34-
bool CreateBridge(std::string_view name);
35-
bool IptableConfig(std::string_view network, bool add);
26+
Result<void> AddTapIface(std::string_view name);
27+
Result<void> ShutdownIface(std::string_view name);
28+
Result<void> BringUpIface(std::string_view name);
29+
Result<void> AddGateway(std::string_view name, std::string_view gateway,
30+
std::string_view netmask);
31+
Result<void> DestroyGateway(std::string_view name, std::string_view gateway,
32+
std::string_view netmask);
33+
Result<void> LinkTapToBridge(std::string_view tap_name,
34+
std::string_view bridge_name);
35+
Result<void> DeleteIface(std::string_view name);
36+
Result<bool> BridgeExists(std::string_view name);
37+
Result<void> CreateBridge(std::string_view name);
38+
Result<void> IptableConfig(std::string_view network, bool add);
3639

3740
} // namespace cuttlefish

base/cvd/allocd/alloc_iproute2.cpp

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,118 +22,117 @@
2222
#include "absl/log/log.h"
2323
#include "absl/strings/str_format.h"
2424

25+
#include "cuttlefish/result/result.h"
26+
2527
namespace cuttlefish {
2628

2729
extern int RunExternalCommand(const std::string& name);
2830

29-
bool AddTapIface(std::string_view name) {
31+
Result<void> AddTapIface(std::string_view name) {
3032
std::stringstream ss;
3133
ss << "ip tuntap add dev " << name << " mode tap group cvdnetwork vnet_hdr";
3234
auto add_command = ss.str();
3335
LOG(INFO) << "Create tap interface: " << add_command;
3436
int status = RunExternalCommand(add_command);
35-
return status == 0;
37+
CF_EXPECT_EQ(status, 0);
38+
return {};
3639
}
3740

38-
bool ShutdownIface(std::string_view name) {
41+
Result<void> ShutdownIface(std::string_view name) {
3942
std::stringstream ss;
4043
ss << "ip link set dev " << name << " down";
4144
auto link_command = ss.str();
4245
LOG(INFO) << "Shutdown tap interface: " << link_command;
4346
int status = RunExternalCommand(link_command);
44-
45-
return status == 0;
47+
CF_EXPECT_EQ(status, 0);
48+
return {};
4649
}
4750

48-
bool BringUpIface(std::string_view name) {
51+
Result<void> BringUpIface(std::string_view name) {
4952
std::stringstream ss;
5053
ss << "ip link set dev " << name << " up";
5154
auto link_command = ss.str();
5255
LOG(INFO) << "Bring up tap interface: " << link_command;
5356
int status = RunExternalCommand(link_command);
54-
55-
return status == 0;
57+
CF_EXPECT_EQ(status, 0);
58+
return {};
5659
}
5760

58-
bool AddGateway(std::string_view name, std::string_view gateway,
59-
std::string_view netmask) {
61+
Result<void> AddGateway(std::string_view name, std::string_view gateway,
62+
std::string_view netmask) {
6063
std::stringstream ss;
6164
ss << "ip addr add " << gateway << netmask << " broadcast + dev " << name;
6265
auto command = ss.str();
6366
LOG(INFO) << "setup gateway: " << command;
6467
int status = RunExternalCommand(command);
65-
66-
return status == 0;
68+
CF_EXPECT_EQ(status, 0);
69+
return {};
6770
}
6871

69-
bool DestroyGateway(std::string_view name, std::string_view gateway,
70-
std::string_view netmask) {
72+
Result<void> DestroyGateway(std::string_view name, std::string_view gateway,
73+
std::string_view netmask) {
7174
std::stringstream ss;
7275
ss << "ip addr del " << gateway << netmask << " broadcast + dev " << name;
7376
auto command = ss.str();
7477
LOG(INFO) << "removing gateway: " << command;
7578
int status = RunExternalCommand(command);
76-
77-
return status == 0;
79+
CF_EXPECT_EQ(status, 0);
80+
return {};
7881
}
7982

80-
bool LinkTapToBridge(std::string_view tap_name,
81-
std::string_view bridge_name) {
83+
Result<void> LinkTapToBridge(std::string_view tap_name,
84+
std::string_view bridge_name) {
8285
std::stringstream ss;
8386
ss << "ip link set dev " << tap_name << " master " << bridge_name;
8487
auto command = ss.str();
8588
int status = RunExternalCommand(command);
86-
87-
return status == 0;
89+
CF_EXPECT_EQ(status, 0);
90+
return {};
8891
}
8992

90-
bool DeleteIface(std::string_view name) {
93+
Result<void> DeleteIface(std::string_view name) {
9194
std::stringstream ss;
9295
ss << "ip link delete " << name;
9396
auto link_command = ss.str();
9497
LOG(INFO) << "Delete tap interface: " << link_command;
9598
int status = RunExternalCommand(link_command);
96-
97-
return status == 0;
99+
CF_EXPECT_EQ(status, 0);
100+
return {};
98101
}
99102

100-
bool BridgeExists(std::string_view name) {
103+
Result<bool> BridgeExists(std::string_view name) {
101104
std::stringstream ss;
102105
ss << "ip link show " << name << " >/dev/null";
103106

104107
auto command = ss.str();
105108
LOG(INFO) << "bridge exists: " << command;
106109
int status = RunExternalCommand(command);
107-
108110
return status == 0;
109111
}
110112

111-
bool CreateBridge(std::string_view name) {
113+
Result<void> CreateBridge(std::string_view name) {
112114
std::stringstream ss;
113115
ss << "ip link add name " << name
114116
<< " type bridge forward_delay 0 stp_state 0";
115117

116118
auto command = ss.str();
117119
LOG(INFO) << "create bridge: " << command;
118120
int status = RunExternalCommand(command);
119-
120-
if (status != 0) {
121-
return false;
122-
}
123-
124-
return BringUpIface(name);
121+
CF_EXPECT_EQ(status, 0);
122+
CF_EXPECT(BringUpIface(name));
123+
return {};
125124
}
126125

127-
bool IptableConfig(std::string_view network, bool add) {
126+
Result<void> IptableConfig(std::string_view network, bool add) {
128127
std::stringstream ss;
129128
ss << "iptables -t nat " << (add ? "-A" : "-D") << " POSTROUTING -s "
130129
<< network << " -j MASQUERADE";
131130

132131
auto command = ss.str();
133132
LOG(INFO) << "iptable_config: " << command;
134133
int status = RunExternalCommand(command);
135-
136-
return status == 0;
134+
CF_EXPECT_EQ(status, 0);
135+
return {};
137136
}
138137

139138
} // namespace cuttlefish

0 commit comments

Comments
 (0)