Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "arc/network/arc_service.h" |
| 6 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 7 | #include <linux/rtnetlink.h> |
| 8 | #include <net/if.h> |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 9 | #include <sys/ioctl.h> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 10 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 11 | #include <utility> |
Jason Jeremy Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 12 | #include <vector> |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 13 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 14 | #include <base/bind.h> |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 15 | #include <base/files/file_path.h> |
| 16 | #include <base/files/file_util.h> |
| 17 | #include <base/logging.h> |
| 18 | #include <base/strings/string_number_conversions.h> |
| 19 | #include <base/strings/string_util.h> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 20 | #include <base/strings/stringprintf.h> |
Garrick Evans | 1f5a361 | 2019-11-08 12:59:03 +0900 | [diff] [blame] | 21 | #include <brillo/key_value_store.h> |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 22 | #include <chromeos/constants/vm_tools.h> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 23 | |
| 24 | #include "arc/network/datapath.h" |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 25 | #include "arc/network/mac_address_generator.h" |
Jason Jeremy Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 26 | #include "arc/network/manager.h" |
Garrick Evans | 3915af3 | 2019-07-25 15:44:34 +0900 | [diff] [blame] | 27 | #include "arc/network/minijailed_process_runner.h" |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 28 | #include "arc/network/net_util.h" |
| 29 | #include "arc/network/scoped_ns.h" |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 30 | |
| 31 | namespace arc_networkd { |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 32 | namespace test { |
| 33 | GuestMessage::GuestType guest = GuestMessage::UNKNOWN_GUEST; |
| 34 | } // namespace test |
| 35 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 36 | namespace { |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 37 | constexpr pid_t kInvalidPID = 0; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 38 | constexpr pid_t kTestPID = -2; |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 39 | constexpr uint32_t kInvalidCID = 0; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 40 | constexpr char kArcIfname[] = "arc0"; |
| 41 | constexpr char kArcBridge[] = "arcbr0"; |
| 42 | constexpr char kArcVmIfname[] = "arc1"; |
| 43 | constexpr char kArcVmBridge[] = "arc_br1"; |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 44 | constexpr std::array<const char*, 2> kEthernetInterfacePrefixes{{"eth", "usb"}}; |
| 45 | constexpr std::array<const char*, 2> kWifiInterfacePrefixes{{"wlan", "mlan"}}; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 46 | |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 47 | void OneTimeSetup(const Datapath& datapath) { |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 48 | static bool done = false; |
| 49 | if (done) |
| 50 | return; |
| 51 | |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 52 | auto& runner = datapath.runner(); |
| 53 | |
| 54 | // Load networking modules needed by Android that are not compiled in the |
| 55 | // kernel. Android does not allow auto-loading of kernel modules. |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 56 | // These must succeed. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 57 | if (runner.modprobe_all({ |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 58 | // The netfilter modules needed by netd for iptables commands. |
| 59 | "ip6table_filter", |
| 60 | "ip6t_ipv6header", |
| 61 | "ip6t_REJECT", |
| 62 | // The xfrm modules needed for Android's ipsec APIs. |
| 63 | "xfrm4_mode_transport", |
| 64 | "xfrm4_mode_tunnel", |
| 65 | "xfrm6_mode_transport", |
| 66 | "xfrm6_mode_tunnel", |
| 67 | // The ipsec modules for AH and ESP encryption for ipv6. |
| 68 | "ah6", |
| 69 | "esp6", |
| 70 | }) != 0) { |
| 71 | LOG(ERROR) << "One or more required kernel modules failed to load." |
| 72 | << " Some Android functionality may be broken."; |
| 73 | } |
| 74 | // Optional modules. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 75 | if (runner.modprobe_all({ |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 76 | // This module is not available in kernels < 3.18 |
| 77 | "nf_reject_ipv6", |
| 78 | // These modules are needed for supporting Chrome traffic on Android |
| 79 | // VPN which uses Android's NAT feature. Android NAT sets up |
| 80 | // iptables |
| 81 | // rules that use these conntrack modules for FTP/TFTP. |
| 82 | "nf_nat_ftp", |
| 83 | "nf_nat_tftp", |
Hugo Benichi | a0cde9e | 2019-12-16 11:57:20 +0900 | [diff] [blame] | 84 | // The tun module is needed by the Android 464xlat clatd process. |
| 85 | "tun", |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 86 | }) != 0) { |
| 87 | LOG(WARNING) << "One or more optional kernel modules failed to load."; |
| 88 | } |
| 89 | |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 90 | // This is only needed for CTS (b/27932574). |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 91 | if (runner.chown("655360", "655360", "/sys/class/xt_idletimer") != 0) { |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 92 | LOG(ERROR) << "Failed to change ownership of xt_idletimer."; |
| 93 | } |
| 94 | |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 95 | done = true; |
| 96 | } |
| 97 | |
Garrick Evans | 508a4bc | 2019-11-14 08:45:52 +0900 | [diff] [blame] | 98 | bool IsArcVm() { |
| 99 | const base::FilePath path("/run/chrome/is_arcvm"); |
| 100 | std::string contents; |
| 101 | if (!base::ReadFileToString(path, &contents)) { |
| 102 | PLOG(ERROR) << "Could not read " << path.value(); |
| 103 | } |
| 104 | return contents == "1"; |
| 105 | } |
| 106 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 107 | GuestMessage::GuestType ArcGuest() { |
| 108 | if (test::guest != GuestMessage::UNKNOWN_GUEST) |
| 109 | return test::guest; |
Garrick Evans | 508a4bc | 2019-11-14 08:45:52 +0900 | [diff] [blame] | 110 | |
Garrick Evans | b05a7ff | 2020-02-18 12:59:55 +0900 | [diff] [blame] | 111 | return IsArcVm() ? GuestMessage::ARC_VM : GuestMessage::ARC; |
Garrick Evans | 508a4bc | 2019-11-14 08:45:52 +0900 | [diff] [blame] | 112 | } |
| 113 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 114 | bool IsEthernetInterface(const std::string& ifname) { |
| 115 | for (const auto& prefix : kEthernetInterfacePrefixes) { |
| 116 | if (base::StartsWith(ifname, prefix, |
| 117 | base::CompareCase::INSENSITIVE_ASCII)) { |
| 118 | return true; |
| 119 | } |
| 120 | } |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | bool IsWifiInterface(const std::string& ifname) { |
| 125 | for (const auto& prefix : kWifiInterfacePrefixes) { |
| 126 | if (base::StartsWith(ifname, prefix, |
| 127 | base::CompareCase::INSENSITIVE_ASCII)) { |
| 128 | return true; |
| 129 | } |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | bool IsMulticastInterface(const std::string& ifname) { |
| 135 | if (ifname.empty()) { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | int fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 140 | if (fd < 0) { |
| 141 | // If IPv4 fails, try to open a socket using IPv6. |
| 142 | fd = socket(AF_INET6, SOCK_DGRAM, 0); |
| 143 | if (fd < 0) { |
| 144 | LOG(ERROR) << "Unable to create socket"; |
| 145 | return false; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | struct ifreq ifr; |
| 150 | memset(&ifr, 0, sizeof(ifr)); |
| 151 | strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ); |
| 152 | if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) { |
| 153 | PLOG(ERROR) << "SIOCGIFFLAGS failed for " << ifname; |
| 154 | close(fd); |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | close(fd); |
| 159 | return (ifr.ifr_flags & IFF_MULTICAST); |
| 160 | } |
| 161 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 162 | // Returns the configuration for the ARC management interface used for VPN |
| 163 | // forwarding, ADB-over-TCP and single-networked ARCVM. |
| 164 | std::unique_ptr<Device::Config> MakeArcConfig(AddressManager* addr_mgr, |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 165 | AddressManager::Guest guest) { |
| 166 | auto ipv4_subnet = addr_mgr->AllocateIPv4Subnet(guest); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 167 | if (!ipv4_subnet) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 168 | LOG(ERROR) << "Subnet already in use or unavailable"; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 169 | return nullptr; |
| 170 | } |
| 171 | auto host_ipv4_addr = ipv4_subnet->AllocateAtOffset(0); |
| 172 | if (!host_ipv4_addr) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 173 | LOG(ERROR) << "Bridge address already in use or unavailable"; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 174 | return nullptr; |
| 175 | } |
| 176 | auto guest_ipv4_addr = ipv4_subnet->AllocateAtOffset(1); |
| 177 | if (!guest_ipv4_addr) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 178 | LOG(ERROR) << "ARC address already in use or unavailable"; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 179 | return nullptr; |
| 180 | } |
| 181 | |
| 182 | return std::make_unique<Device::Config>( |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 183 | addr_mgr->GenerateMacAddress(), std::move(ipv4_subnet), |
| 184 | std::move(host_ipv4_addr), std::move(guest_ipv4_addr)); |
| 185 | } |
| 186 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 187 | } // namespace |
| 188 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 189 | ArcService::ArcService(ShillClient* shill_client, |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 190 | Datapath* datapath, |
| 191 | AddressManager* addr_mgr, |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 192 | TrafficForwarder* forwarder, |
| 193 | bool enable_arcvm_multinet) |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 194 | : shill_client_(shill_client), |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 195 | datapath_(datapath), |
| 196 | addr_mgr_(addr_mgr), |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 197 | forwarder_(forwarder), |
| 198 | enable_arcvm_multinet_(enable_arcvm_multinet) { |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 199 | shill_client_->RegisterDevicesChangedHandler( |
| 200 | base::Bind(&ArcService::OnDevicesChanged, weak_factory_.GetWeakPtr())); |
| 201 | shill_client_->ScanDevices( |
| 202 | base::Bind(&ArcService::OnDevicesChanged, weak_factory_.GetWeakPtr())); |
Garrick Evans | bbdf4b4 | 2020-03-05 12:59:06 +0900 | [diff] [blame] | 203 | shill_client_->RegisterDefaultInterfaceChangedHandler(base::Bind( |
| 204 | &ArcService::OnDefaultInterfaceChanged, weak_factory_.GetWeakPtr())); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | ArcService::~ArcService() { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 208 | if (impl_) { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 209 | Stop(impl_->id()); |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 210 | } |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 211 | } |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 212 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 213 | bool ArcService::Start(uint32_t id) { |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 214 | if (impl_) { |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 215 | uint32_t prev_id; |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 216 | if (impl_->IsStarted(&prev_id)) { |
| 217 | LOG(WARNING) << "Already running - did something crash?" |
| 218 | << " Stopping and restarting..."; |
| 219 | Stop(prev_id); |
| 220 | } |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 221 | } |
| 222 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 223 | const auto guest = ArcGuest(); |
| 224 | if (guest == GuestMessage::ARC_VM) |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 225 | impl_ = std::make_unique<VmImpl>(shill_client_, datapath_, addr_mgr_, |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 226 | forwarder_, enable_arcvm_multinet_); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 227 | else |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 228 | impl_ = std::make_unique<ContainerImpl>(datapath_, addr_mgr_, forwarder_, |
| 229 | guest); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 230 | |
| 231 | if (!impl_->Start(id)) { |
| 232 | impl_.reset(); |
Garrick Evans | 508a4bc | 2019-11-14 08:45:52 +0900 | [diff] [blame] | 233 | return false; |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 234 | } |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 235 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 236 | // Start already known Shill <-> ARC mapped devices. |
| 237 | for (const auto& d : devices_) |
| 238 | StartDevice(d.second.get()); |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 239 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 240 | return true; |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 241 | } |
| 242 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 243 | void ArcService::Stop(uint32_t id) { |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 244 | // Stop Shill <-> ARC mapped devices. |
| 245 | for (const auto& d : devices_) |
| 246 | StopDevice(d.second.get()); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 247 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 248 | if (impl_) { |
| 249 | impl_->Stop(id); |
| 250 | impl_.reset(); |
| 251 | } |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 252 | } |
| 253 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 254 | void ArcService::OnDevicesChanged(const std::set<std::string>& added, |
| 255 | const std::set<std::string>& removed) { |
| 256 | for (const std::string& name : removed) |
| 257 | RemoveDevice(name); |
| 258 | |
| 259 | for (const std::string& name : added) |
| 260 | AddDevice(name); |
| 261 | } |
| 262 | |
| 263 | void ArcService::AddDevice(const std::string& ifname) { |
| 264 | if (ifname.empty()) |
| 265 | return; |
| 266 | |
| 267 | if (devices_.find(ifname) != devices_.end()) { |
| 268 | LOG(DFATAL) << "Attemping to add already tracked device: " << ifname; |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | Device::Options opts{ |
| 273 | .fwd_multicast = IsMulticastInterface(ifname), |
| 274 | // TODO(crbug/726815) Also enable |ipv6_enabled| for cellular networks |
| 275 | // once IPv6 is enabled on cellular networks in shill. |
| 276 | .ipv6_enabled = IsEthernetInterface(ifname) || IsWifiInterface(ifname), |
| 277 | .use_default_interface = false, |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 278 | }; |
| 279 | std::string host_ifname = base::StringPrintf("arc_%s", ifname.c_str()); |
| 280 | auto ipv4_subnet = |
| 281 | addr_mgr_->AllocateIPv4Subnet(AddressManager::Guest::ARC_NET); |
| 282 | if (!ipv4_subnet) { |
| 283 | LOG(ERROR) << "Subnet already in use or unavailable. Cannot make device: " |
| 284 | << ifname; |
| 285 | return; |
| 286 | } |
| 287 | auto host_ipv4_addr = ipv4_subnet->AllocateAtOffset(0); |
| 288 | if (!host_ipv4_addr) { |
| 289 | LOG(ERROR) |
| 290 | << "Bridge address already in use or unavailable. Cannot make device: " |
| 291 | << ifname; |
| 292 | return; |
| 293 | } |
| 294 | auto guest_ipv4_addr = ipv4_subnet->AllocateAtOffset(1); |
| 295 | if (!guest_ipv4_addr) { |
| 296 | LOG(ERROR) |
| 297 | << "ARC address already in use or unavailable. Cannot make device: " |
| 298 | << ifname; |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | auto config = std::make_unique<Device::Config>( |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 303 | addr_mgr_->GenerateMacAddress(), std::move(ipv4_subnet), |
| 304 | std::move(host_ipv4_addr), std::move(guest_ipv4_addr)); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 305 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 306 | auto device = std::make_unique<Device>(ifname, host_ifname, ifname, |
| 307 | std::move(config), opts); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 308 | |
| 309 | StartDevice(device.get()); |
| 310 | devices_.emplace(ifname, std::move(device)); |
| 311 | } |
| 312 | |
| 313 | void ArcService::StartDevice(Device* device) { |
| 314 | if (!impl_ || !impl_->IsStarted()) |
| 315 | return; |
| 316 | |
| 317 | // For now, only start devices for ARC++. |
| 318 | if (impl_->guest() != GuestMessage::ARC) |
| 319 | return; |
| 320 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 321 | const auto& config = device->config(); |
| 322 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 323 | LOG(INFO) << "Adding device " << device->phys_ifname() |
| 324 | << " bridge: " << device->host_ifname() |
| 325 | << " guest_iface: " << device->guest_ifname(); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 326 | |
| 327 | // Create the bridge. |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 328 | if (!datapath_->AddBridge(device->host_ifname(), config.host_ipv4_addr(), |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 329 | 30)) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 330 | LOG(ERROR) << "Failed to setup arc bridge: " << device->host_ifname(); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 331 | return; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 332 | } |
| 333 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 334 | // Set up iptables. |
| 335 | if (!datapath_->AddInboundIPv4DNAT( |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 336 | device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr()))) |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 337 | LOG(ERROR) << "Failed to configure ingress traffic rules for " |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 338 | << device->phys_ifname(); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 339 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 340 | if (!datapath_->AddOutboundIPv4(device->host_ifname())) |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 341 | LOG(ERROR) << "Failed to configure egress traffic rules for " |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 342 | << device->phys_ifname(); |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 343 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 344 | if (!impl_->OnStartDevice(device)) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 345 | LOG(ERROR) << "Failed to start device " << device->phys_ifname(); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| 349 | void ArcService::RemoveDevice(const std::string& ifname) { |
| 350 | const auto it = devices_.find(ifname); |
| 351 | if (it == devices_.end()) { |
| 352 | LOG(WARNING) << "Unknown device: " << ifname; |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 353 | return; |
| 354 | } |
| 355 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 356 | // If the container is down, this call does nothing. |
| 357 | StopDevice(it->second.get()); |
| 358 | |
| 359 | devices_.erase(it); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 360 | } |
| 361 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 362 | void ArcService::StopDevice(Device* device) { |
| 363 | if (!impl_ || !impl_->IsStarted()) |
| 364 | return; |
| 365 | |
| 366 | // For now, devices are only started for ARC++. |
| 367 | if (impl_->guest() != GuestMessage::ARC) |
| 368 | return; |
| 369 | |
| 370 | impl_->OnStopDevice(device); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 371 | |
| 372 | const auto& config = device->config(); |
| 373 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 374 | LOG(INFO) << "Removing device " << device->phys_ifname() |
| 375 | << " bridge: " << device->host_ifname() |
| 376 | << " guest_iface: " << device->guest_ifname(); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 377 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 378 | datapath_->RemoveOutboundIPv4(device->host_ifname()); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 379 | datapath_->RemoveInboundIPv4DNAT( |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 380 | device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr())); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 381 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 382 | datapath_->RemoveBridge(device->host_ifname()); |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 383 | } |
| 384 | |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 385 | void ArcService::OnDefaultInterfaceChanged(const std::string& new_ifname, |
| 386 | const std::string& prev_ifname) { |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 387 | if (impl_) |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 388 | impl_->OnDefaultInterfaceChanged(new_ifname, prev_ifname); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 389 | } |
Garrick Evans | ba57574 | 2019-07-17 15:48:08 +0900 | [diff] [blame] | 390 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 391 | Device* ArcService::ArcDevice() const { |
| 392 | if (!impl_) |
| 393 | return nullptr; |
| 394 | |
| 395 | return impl_->ArcDevice(); |
| 396 | } |
| 397 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 398 | // ARC++ specific functions. |
| 399 | |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 400 | ArcService::ContainerImpl::ContainerImpl(Datapath* datapath, |
| 401 | AddressManager* addr_mgr, |
| 402 | TrafficForwarder* forwarder, |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 403 | GuestMessage::GuestType guest) |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 404 | : pid_(kInvalidPID), |
| 405 | datapath_(datapath), |
| 406 | addr_mgr_(addr_mgr), |
| 407 | forwarder_(forwarder), |
| 408 | guest_(guest) { |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 409 | OneTimeSetup(*datapath_); |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 410 | } |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 411 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 412 | GuestMessage::GuestType ArcService::ContainerImpl::guest() const { |
| 413 | return guest_; |
| 414 | } |
| 415 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 416 | uint32_t ArcService::ContainerImpl::id() const { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 417 | return pid_; |
| 418 | } |
| 419 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 420 | bool ArcService::ContainerImpl::Start(uint32_t pid) { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 421 | // This could happen if something crashes and the stop signal is not sent. |
| 422 | // It can probably be addressed by stopping and restarting the service. |
| 423 | if (pid_ != kInvalidPID) |
| 424 | return false; |
| 425 | |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 426 | if (pid == kInvalidPID) { |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 427 | LOG(ERROR) << "Cannot start service - invalid container PID"; |
| 428 | return false; |
| 429 | } |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 430 | pid_ = pid; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 431 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 432 | Device::Options opts{ |
| 433 | .fwd_multicast = false, |
| 434 | .ipv6_enabled = false, |
| 435 | .use_default_interface = false, |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 436 | }; |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 437 | auto config = MakeArcConfig(addr_mgr_, AddressManager::Guest::ARC); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 438 | |
| 439 | // Create the bridge. |
| 440 | // Per crbug/1008686 this device cannot be deleted and then re-added. |
| 441 | // So instead of removing the bridge when the service stops, bring down the |
| 442 | // device instead and re-up it on restart. |
| 443 | if (!datapath_->AddBridge(kArcBridge, config->host_ipv4_addr(), 30) && |
| 444 | !datapath_->MaskInterfaceFlags(kArcBridge, IFF_UP)) { |
| 445 | LOG(ERROR) << "Failed to bring up arc bridge: " << kArcBridge; |
| 446 | return false; |
| 447 | } |
| 448 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 449 | arc_device_ = std::make_unique<Device>(kArcIfname, kArcBridge, kArcIfname, |
| 450 | std::move(config), opts); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 451 | |
| 452 | OnStartDevice(arc_device_.get()); |
| 453 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 454 | LOG(INFO) << "ARC++ network service started {pid: " << pid_ << "}"; |
| 455 | return true; |
| 456 | } |
| 457 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 458 | void ArcService::ContainerImpl::Stop(uint32_t /*pid*/) { |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 459 | if (!IsStarted()) |
Taoyu Li | 1c96d27 | 2019-12-13 14:17:43 +0900 | [diff] [blame] | 460 | return; |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 461 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 462 | // Per crbug/1008686 this device cannot be deleted and then re-added. |
| 463 | // So instead of removing the bridge, bring it down and mark it. This will |
| 464 | // allow us to detect if the device is re-added in case of a crash restart |
| 465 | // and do the right thing. |
| 466 | if (arc_device_) { |
| 467 | OnStopDevice(arc_device_.get()); |
| 468 | if (!datapath_->MaskInterfaceFlags(kArcBridge, IFF_DEBUG, IFF_UP)) |
| 469 | LOG(ERROR) << "Failed to bring down arc bridge " |
| 470 | << "- it may not restart correctly"; |
| 471 | } |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 472 | |
| 473 | LOG(INFO) << "ARC++ network service stopped {pid: " << pid_ << "}"; |
| 474 | pid_ = kInvalidPID; |
| 475 | } |
| 476 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 477 | bool ArcService::ContainerImpl::IsStarted(uint32_t* pid) const { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 478 | if (pid) |
| 479 | *pid = pid_; |
| 480 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 481 | return pid_ != kInvalidPID; |
| 482 | } |
| 483 | |
| 484 | bool ArcService::ContainerImpl::OnStartDevice(Device* device) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 485 | LOG(INFO) << "Starting device " << device->phys_ifname() |
| 486 | << " bridge: " << device->host_ifname() |
| 487 | << " guest_iface: " << device->guest_ifname() << " pid: " << pid_; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 488 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 489 | // Set up the virtual pair inside the container namespace. |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 490 | const std::string veth_ifname = ArcVethHostName(device->guest_ifname()); |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 491 | { |
| 492 | ScopedNS ns(pid_); |
| 493 | if (!ns.IsValid() && pid_ != kTestPID) { |
| 494 | LOG(ERROR) |
| 495 | << "Cannot create virtual link -- invalid container namespace?"; |
| 496 | return false; |
| 497 | } |
| 498 | |
| 499 | if (!datapath_->AddVirtualInterfacePair(veth_ifname, |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 500 | device->guest_ifname())) { |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 501 | LOG(ERROR) << "Failed to create virtual interface pair for " |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 502 | << device->phys_ifname(); |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 503 | return false; |
| 504 | } |
| 505 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 506 | const auto& config = device->config(); |
| 507 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 508 | if (!datapath_->ConfigureInterface( |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 509 | device->guest_ifname(), config.mac_addr(), config.guest_ipv4_addr(), |
| 510 | 30, true /* link up */, device->options().fwd_multicast)) { |
| 511 | LOG(ERROR) << "Failed to configure interface " << device->guest_ifname(); |
| 512 | datapath_->RemoveInterface(device->guest_ifname()); |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 513 | return false; |
| 514 | } |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 515 | } |
| 516 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 517 | // Now pull the host end out into the root namespace and add it to the bridge. |
| 518 | if (datapath_->runner().RestoreDefaultNamespace(veth_ifname, pid_) != 0) { |
| 519 | LOG(ERROR) << "Failed to prepare interface " << veth_ifname; |
| 520 | { |
| 521 | ScopedNS ns(pid_); |
| 522 | if (ns.IsValid()) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 523 | datapath_->RemoveInterface(device->guest_ifname()); |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 524 | } else { |
| 525 | LOG(ERROR) << "Failed to re-enter container namespace." |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 526 | << " Subsequent attempts to restart " |
| 527 | << device->phys_ifname() << " may not succeed."; |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | return false; |
| 531 | } |
| 532 | if (!datapath_->ToggleInterface(veth_ifname, true /*up*/)) { |
| 533 | LOG(ERROR) << "Failed to bring up interface " << veth_ifname; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 534 | datapath_->RemoveInterface(veth_ifname); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 535 | return false; |
| 536 | } |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 537 | if (!datapath_->AddToBridge(device->host_ifname(), veth_ifname)) { |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 538 | datapath_->RemoveInterface(veth_ifname); |
| 539 | LOG(ERROR) << "Failed to bridge interface " << veth_ifname; |
| 540 | return false; |
| 541 | } |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 542 | |
Garrick Evans | 3bd0637 | 2020-03-23 10:42:58 +0900 | [diff] [blame] | 543 | if (device != arc_device_.get()) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 544 | forwarder_->StartForwarding(device->phys_ifname(), device->host_ifname(), |
| 545 | device->options().ipv6_enabled, |
| 546 | device->options().fwd_multicast); |
Garrick Evans | 3bd0637 | 2020-03-23 10:42:58 +0900 | [diff] [blame] | 547 | } else { |
| 548 | // Signal the container that the network device is ready. |
| 549 | datapath_->runner().WriteSentinelToContainer(pid_); |
| 550 | } |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 551 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 552 | return true; |
| 553 | } |
| 554 | |
| 555 | void ArcService::ContainerImpl::OnStopDevice(Device* device) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 556 | LOG(INFO) << "Stopping device " << device->phys_ifname() |
| 557 | << " bridge: " << device->host_ifname() |
| 558 | << " guest_iface: " << device->guest_ifname() << " pid: " << pid_; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 559 | |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 560 | if (device != arc_device_.get()) |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 561 | forwarder_->StopForwarding(device->phys_ifname(), device->host_ifname(), |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 562 | device->options().ipv6_enabled, |
| 563 | device->options().fwd_multicast); |
| 564 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 565 | datapath_->RemoveInterface(ArcVethHostName(device->phys_ifname())); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | void ArcService::ContainerImpl::OnDefaultInterfaceChanged( |
Garrick Evans | b05a7ff | 2020-02-18 12:59:55 +0900 | [diff] [blame] | 569 | const std::string& new_ifname, const std::string& prev_ifname) {} |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 570 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 571 | // VM specific functions |
| 572 | |
Garrick Evans | bbdf4b4 | 2020-03-05 12:59:06 +0900 | [diff] [blame] | 573 | ArcService::VmImpl::VmImpl(ShillClient* shill_client, |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 574 | Datapath* datapath, |
| 575 | AddressManager* addr_mgr, |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 576 | TrafficForwarder* forwarder, |
| 577 | bool enable_multinet) |
Garrick Evans | bbdf4b4 | 2020-03-05 12:59:06 +0900 | [diff] [blame] | 578 | : cid_(kInvalidCID), |
| 579 | shill_client_(shill_client), |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 580 | datapath_(datapath), |
| 581 | addr_mgr_(addr_mgr), |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 582 | forwarder_(forwarder), |
| 583 | enable_multinet_(enable_multinet) {} |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 584 | |
| 585 | GuestMessage::GuestType ArcService::VmImpl::guest() const { |
| 586 | return GuestMessage::ARC_VM; |
| 587 | } |
| 588 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 589 | uint32_t ArcService::VmImpl::id() const { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 590 | return cid_; |
| 591 | } |
| 592 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 593 | bool ArcService::VmImpl::Start(uint32_t cid) { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 594 | // This can happen if concierge crashes and doesn't send the vm down RPC. |
| 595 | // It can probably be addressed by stopping and restarting the service. |
| 596 | if (cid_ != kInvalidCID) |
| 597 | return false; |
| 598 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 599 | if (cid == kInvalidCID) { |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 600 | LOG(ERROR) << "Invalid VM cid " << cid; |
| 601 | return false; |
| 602 | } |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 603 | cid_ = cid; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 604 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 605 | // Allocate pool of 6 TAP devices and IPv4 allocs |
| 606 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 607 | Device::Options opts{ |
| 608 | .fwd_multicast = true, |
| 609 | .ipv6_enabled = true, |
| 610 | .use_default_interface = true, |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 611 | }; |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 612 | auto config = MakeArcConfig(addr_mgr_, AddressManager::Guest::VM_ARC); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 613 | |
| 614 | // Create the bridge. |
| 615 | if (!datapath_->AddBridge(kArcVmBridge, config->host_ipv4_addr(), 30)) { |
| 616 | LOG(ERROR) << "Failed to setup arc bridge: " << kArcVmBridge; |
| 617 | return false; |
| 618 | } |
| 619 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 620 | arc_device_ = std::make_unique<Device>(kArcVmIfname, kArcVmBridge, |
| 621 | kArcVmIfname, std::move(config), opts); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 622 | |
| 623 | OnStartDevice(arc_device_.get()); |
| 624 | |
| 625 | LOG(INFO) << "ARCVM network service started {cid: " << cid_ << "}"; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 626 | return true; |
| 627 | } |
| 628 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 629 | void ArcService::VmImpl::Stop(uint32_t cid) { |
Garrick Evans | 21173b1 | 2019-11-20 15:23:16 +0900 | [diff] [blame] | 630 | if (cid_ != cid) { |
| 631 | LOG(ERROR) << "Mismatched ARCVM CIDs " << cid_ << " != " << cid; |
| 632 | return; |
| 633 | } |
| 634 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 635 | OnStopDevice(arc_device_.get()); |
| 636 | datapath_->RemoveBridge(kArcVmBridge); |
| 637 | arc_device_.reset(); |
| 638 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 639 | LOG(INFO) << "ARCVM network service stopped {cid: " << cid_ << "}"; |
| 640 | cid_ = kInvalidCID; |
| 641 | } |
| 642 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 643 | bool ArcService::VmImpl::IsStarted(uint32_t* cid) const { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 644 | if (cid) |
| 645 | *cid = cid_; |
| 646 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 647 | return cid_ != kInvalidCID; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | bool ArcService::VmImpl::OnStartDevice(Device* device) { |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 651 | // TODO(garrick): Remove once ARCVM P is gone. |
| 652 | if (device == arc_device_.get() && !enable_multinet_) |
| 653 | return OnStartArcPDevice(); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 654 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 655 | LOG(INFO) << "Starting device " << device->phys_ifname() |
| 656 | << " bridge: " << device->host_ifname() |
| 657 | << " guest_iface: " << device->guest_ifname() << " cid: " << cid_; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 658 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 659 | // Since the interface will be added to the bridge, no address configuration |
| 660 | // should be provided here. |
| 661 | std::string tap = |
| 662 | datapath_->AddTAP("" /* auto-generate name */, nullptr /* no mac addr */, |
| 663 | nullptr /* no ipv4 subnet */, vm_tools::kCrosVmUser); |
| 664 | if (tap.empty()) { |
| 665 | LOG(ERROR) << "Failed to create TAP device for VM"; |
| 666 | return false; |
| 667 | } |
| 668 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 669 | if (!datapath_->AddToBridge(device->host_ifname(), tap)) { |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 670 | LOG(ERROR) << "Failed to bridge TAP device " << tap; |
| 671 | datapath_->RemoveInterface(tap); |
| 672 | return false; |
| 673 | } |
| 674 | |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 675 | device->set_tap_ifname(tap); |
| 676 | |
| 677 | if (device != arc_device_.get()) |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 678 | forwarder_->StartForwarding(device->phys_ifname(), device->host_ifname(), |
| 679 | device->options().ipv6_enabled, |
| 680 | device->options().fwd_multicast); |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 681 | |
| 682 | return true; |
| 683 | } |
| 684 | |
| 685 | bool ArcService::VmImpl::OnStartArcPDevice() { |
| 686 | LOG(INFO) << "Starting device " << kArcVmIfname << " bridge: " << kArcVmBridge |
| 687 | << " guest_iface: " << kArcVmIfname << " cid: " << cid_; |
| 688 | |
| 689 | // Since the interface will be added to the bridge, no address configuration |
| 690 | // should be provided here. |
| 691 | std::string tap = |
| 692 | datapath_->AddTAP("" /* auto-generate name */, nullptr /* no mac addr */, |
| 693 | nullptr /* no ipv4 subnet */, vm_tools::kCrosVmUser); |
| 694 | if (tap.empty()) { |
| 695 | LOG(ERROR) << "Failed to create TAP device for VM"; |
| 696 | return false; |
| 697 | } |
| 698 | |
| 699 | if (!datapath_->AddToBridge(kArcVmBridge, tap)) { |
| 700 | LOG(ERROR) << "Failed to bridge TAP device " << tap; |
| 701 | datapath_->RemoveInterface(tap); |
| 702 | return false; |
| 703 | } |
| 704 | |
| 705 | arc_device_->set_tap_ifname(tap); |
| 706 | |
| 707 | // Setup the iptables. |
| 708 | if (!datapath_->AddLegacyIPv4DNAT( |
| 709 | IPv4AddressToString(arc_device_->config().guest_ipv4_addr()))) |
| 710 | LOG(ERROR) << "Failed to configure ARC traffic rules"; |
| 711 | |
| 712 | if (!datapath_->AddOutboundIPv4(kArcVmBridge)) |
| 713 | LOG(ERROR) << "Failed to configure egress traffic rules"; |
| 714 | |
Garrick Evans | bbdf4b4 | 2020-03-05 12:59:06 +0900 | [diff] [blame] | 715 | OnDefaultInterfaceChanged(shill_client_->default_interface(), |
| 716 | "" /*previous*/); |
Garrick Evans | bcce09e | 2020-03-10 15:08:04 +0900 | [diff] [blame] | 717 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 718 | return true; |
| 719 | } |
| 720 | |
| 721 | void ArcService::VmImpl::OnStopDevice(Device* device) { |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 722 | // TODO(garrick): Remove once ARCVM P is gone. |
| 723 | if (device == arc_device_.get() && !enable_multinet_) |
| 724 | return OnStopArcPDevice(); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 725 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 726 | LOG(INFO) << "Stopping " << device->phys_ifname() |
| 727 | << " bridge: " << device->host_ifname() |
| 728 | << " guest_iface: " << device->guest_ifname() << " cid: " << cid_; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 729 | |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 730 | if (device != arc_device_.get()) |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame^] | 731 | forwarder_->StopForwarding(device->phys_ifname(), device->host_ifname(), |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 732 | device->options().ipv6_enabled, |
| 733 | device->options().fwd_multicast); |
Garrick Evans | bcce09e | 2020-03-10 15:08:04 +0900 | [diff] [blame] | 734 | |
| 735 | datapath_->RemoveInterface(device->tap_ifname()); |
| 736 | device->set_tap_ifname(""); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 737 | } |
| 738 | |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 739 | void ArcService::VmImpl::OnStopArcPDevice() { |
| 740 | LOG(INFO) << "Stopping " << kArcVmIfname << " bridge: " << kArcVmBridge |
| 741 | << " guest_iface: " << kArcVmIfname << " cid: " << cid_; |
| 742 | |
| 743 | datapath_->RemoveOutboundIPv4(kArcVmBridge); |
| 744 | datapath_->RemoveLegacyIPv4DNAT(); |
| 745 | |
| 746 | OnDefaultInterfaceChanged("" /*new_ifname*/, |
| 747 | shill_client_->default_interface()); |
| 748 | |
| 749 | datapath_->RemoveInterface(arc_device_->tap_ifname()); |
| 750 | arc_device_->set_tap_ifname(""); |
| 751 | } |
| 752 | |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 753 | void ArcService::VmImpl::OnDefaultInterfaceChanged( |
| 754 | const std::string& new_ifname, const std::string& prev_ifname) { |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 755 | if (!IsStarted() || enable_multinet_) |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 756 | return; |
| 757 | |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 758 | forwarder_->StopForwarding(prev_ifname, kArcVmBridge, true /*ipv6*/, |
| 759 | true /*multicast*/); |
| 760 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 761 | datapath_->RemoveLegacyIPv4InboundDNAT(); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 762 | |
| 763 | // If a new default interface was given, then re-enable with that. |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 764 | if (!new_ifname.empty()) { |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 765 | datapath_->AddLegacyIPv4InboundDNAT(new_ifname); |
Jason Jeremy Iman | 0e9f826 | 2020-03-06 14:50:49 +0900 | [diff] [blame] | 766 | forwarder_->StartForwarding(new_ifname, kArcVmBridge, true /*ipv6*/, |
| 767 | true /*multicast*/); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 771 | } // namespace arc_networkd |