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