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 | |
| 252 | void ArcService::ReallocateAddressConfigs() { |
| 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(); |
| 261 | for (const auto& d : existing_devices) { |
| 262 | AddDevice(d); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | std::unique_ptr<Device::Config> ArcService::AcquireConfig( |
| 267 | const std::string& ifname) { |
| 268 | auto itype = InterfaceTypeFor(ifname); |
| 269 | if (itype == InterfaceType::UNKNOWN) { |
| 270 | LOG(ERROR) << "Unsupported interface: " << ifname; |
| 271 | return nullptr; |
| 272 | } |
| 273 | |
| 274 | auto& configs = configs_[itype]; |
| 275 | if (configs.empty()) { |
| 276 | LOG(ERROR) << "No more addresses available. Cannot make device for " |
| 277 | << ifname; |
| 278 | return nullptr; |
| 279 | } |
| 280 | std::unique_ptr<Device::Config> config; |
| 281 | config = std::move(configs.front()); |
| 282 | configs.pop_front(); |
| 283 | return config; |
| 284 | } |
| 285 | |
| 286 | void ArcService::ReleaseConfig(const std::string& ifname, |
| 287 | std::unique_ptr<Device::Config> config) { |
| 288 | auto itype = InterfaceTypeFor(ifname); |
| 289 | if (itype == InterfaceType::UNKNOWN) { |
| 290 | LOG(ERROR) << "Unsupported interface: " << ifname; |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | configs_[itype].push_front(std::move(config)); |
| 295 | } |
| 296 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 297 | bool ArcService::Start(uint32_t id) { |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 298 | if (impl_) { |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 299 | uint32_t prev_id; |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 300 | if (impl_->IsStarted(&prev_id)) { |
| 301 | LOG(WARNING) << "Already running - did something crash?" |
| 302 | << " Stopping and restarting..."; |
| 303 | Stop(prev_id); |
| 304 | } |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 305 | } |
| 306 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame^] | 307 | ReallocateAddressConfigs(); |
| 308 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 309 | const auto guest = ArcGuest(); |
| 310 | if (guest == GuestMessage::ARC_VM) |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 311 | impl_ = std::make_unique<VmImpl>(shill_client_, datapath_, addr_mgr_, |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 312 | forwarder_, enable_arcvm_multinet_); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 313 | else |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 314 | impl_ = std::make_unique<ContainerImpl>(datapath_, addr_mgr_, forwarder_, |
| 315 | guest); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 316 | |
| 317 | if (!impl_->Start(id)) { |
| 318 | impl_.reset(); |
Garrick Evans | 508a4bc | 2019-11-14 08:45:52 +0900 | [diff] [blame] | 319 | return false; |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 320 | } |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 321 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 322 | // Start already known Shill <-> ARC mapped devices. |
| 323 | for (const auto& d : devices_) |
| 324 | StartDevice(d.second.get()); |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 325 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 326 | return true; |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 327 | } |
| 328 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 329 | void ArcService::Stop(uint32_t id) { |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 330 | // Stop Shill <-> ARC mapped devices. |
| 331 | for (const auto& d : devices_) |
| 332 | StopDevice(d.second.get()); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 333 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 334 | if (impl_) { |
| 335 | impl_->Stop(id); |
| 336 | impl_.reset(); |
| 337 | } |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 338 | } |
| 339 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 340 | void ArcService::OnDevicesChanged(const std::set<std::string>& added, |
| 341 | const std::set<std::string>& removed) { |
| 342 | for (const std::string& name : removed) |
| 343 | RemoveDevice(name); |
| 344 | |
| 345 | for (const std::string& name : added) |
| 346 | AddDevice(name); |
| 347 | } |
| 348 | |
| 349 | void ArcService::AddDevice(const std::string& ifname) { |
| 350 | if (ifname.empty()) |
| 351 | return; |
| 352 | |
| 353 | if (devices_.find(ifname) != devices_.end()) { |
| 354 | LOG(DFATAL) << "Attemping to add already tracked device: " << ifname; |
| 355 | return; |
| 356 | } |
| 357 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame^] | 358 | auto itype = InterfaceTypeFor(ifname); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 359 | Device::Options opts{ |
| 360 | .fwd_multicast = IsMulticastInterface(ifname), |
| 361 | // TODO(crbug/726815) Also enable |ipv6_enabled| for cellular networks |
| 362 | // once IPv6 is enabled on cellular networks in shill. |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame^] | 363 | .ipv6_enabled = |
| 364 | (itype == InterfaceType::ETHERNET || itype == InterfaceType::WIFI), |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 365 | .use_default_interface = false, |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 366 | }; |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame^] | 367 | |
| 368 | auto config = AcquireConfig(ifname); |
| 369 | if (!config) { |
| 370 | LOG(ERROR) << "Cannot add device for " << ifname; |
| 371 | return; |
| 372 | } |
| 373 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 374 | std::string host_ifname = base::StringPrintf("arc_%s", ifname.c_str()); |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 375 | auto device = std::make_unique<Device>(ifname, host_ifname, ifname, |
| 376 | std::move(config), opts); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 377 | |
| 378 | StartDevice(device.get()); |
| 379 | devices_.emplace(ifname, std::move(device)); |
| 380 | } |
| 381 | |
| 382 | void ArcService::StartDevice(Device* device) { |
| 383 | if (!impl_ || !impl_->IsStarted()) |
| 384 | return; |
| 385 | |
| 386 | // For now, only start devices for ARC++. |
| 387 | if (impl_->guest() != GuestMessage::ARC) |
| 388 | return; |
| 389 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 390 | const auto& config = device->config(); |
| 391 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 392 | LOG(INFO) << "Adding device " << device->phys_ifname() |
| 393 | << " bridge: " << device->host_ifname() |
| 394 | << " guest_iface: " << device->guest_ifname(); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 395 | |
| 396 | // Create the bridge. |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 397 | if (!datapath_->AddBridge(device->host_ifname(), config.host_ipv4_addr(), |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 398 | 30)) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 399 | LOG(ERROR) << "Failed to setup arc bridge: " << device->host_ifname(); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 400 | return; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 401 | } |
| 402 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 403 | // Set up iptables. |
| 404 | if (!datapath_->AddInboundIPv4DNAT( |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 405 | device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr()))) |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 406 | LOG(ERROR) << "Failed to configure ingress traffic rules for " |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 407 | << device->phys_ifname(); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 408 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 409 | if (!datapath_->AddOutboundIPv4(device->host_ifname())) |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 410 | LOG(ERROR) << "Failed to configure egress traffic rules for " |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 411 | << device->phys_ifname(); |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 412 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 413 | if (!impl_->OnStartDevice(device)) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 414 | LOG(ERROR) << "Failed to start device " << device->phys_ifname(); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
| 418 | void ArcService::RemoveDevice(const std::string& ifname) { |
| 419 | const auto it = devices_.find(ifname); |
| 420 | if (it == devices_.end()) { |
| 421 | LOG(WARNING) << "Unknown device: " << ifname; |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 425 | // If the container is down, this call does nothing. |
| 426 | StopDevice(it->second.get()); |
| 427 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame^] | 428 | ReleaseConfig(ifname, it->second->release_config()); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 429 | devices_.erase(it); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 430 | } |
| 431 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 432 | void ArcService::StopDevice(Device* device) { |
| 433 | if (!impl_ || !impl_->IsStarted()) |
| 434 | return; |
| 435 | |
| 436 | // For now, devices are only started for ARC++. |
| 437 | if (impl_->guest() != GuestMessage::ARC) |
| 438 | return; |
| 439 | |
| 440 | impl_->OnStopDevice(device); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 441 | |
| 442 | const auto& config = device->config(); |
| 443 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 444 | LOG(INFO) << "Removing device " << device->phys_ifname() |
| 445 | << " bridge: " << device->host_ifname() |
| 446 | << " guest_iface: " << device->guest_ifname(); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 447 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 448 | datapath_->RemoveOutboundIPv4(device->host_ifname()); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 449 | datapath_->RemoveInboundIPv4DNAT( |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 450 | device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr())); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 451 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 452 | datapath_->RemoveBridge(device->host_ifname()); |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 453 | } |
| 454 | |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 455 | void ArcService::OnDefaultInterfaceChanged(const std::string& new_ifname, |
| 456 | const std::string& prev_ifname) { |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 457 | if (impl_) |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 458 | impl_->OnDefaultInterfaceChanged(new_ifname, prev_ifname); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 459 | } |
Garrick Evans | ba57574 | 2019-07-17 15:48:08 +0900 | [diff] [blame] | 460 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 461 | Device* ArcService::ArcDevice() const { |
| 462 | if (!impl_) |
| 463 | return nullptr; |
| 464 | |
| 465 | return impl_->ArcDevice(); |
| 466 | } |
| 467 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 468 | // ARC++ specific functions. |
| 469 | |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 470 | ArcService::ContainerImpl::ContainerImpl(Datapath* datapath, |
| 471 | AddressManager* addr_mgr, |
| 472 | TrafficForwarder* forwarder, |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 473 | GuestMessage::GuestType guest) |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 474 | : pid_(kInvalidPID), |
| 475 | datapath_(datapath), |
| 476 | addr_mgr_(addr_mgr), |
| 477 | forwarder_(forwarder), |
| 478 | guest_(guest) { |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 479 | OneTimeSetup(*datapath_); |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 480 | } |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 481 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 482 | GuestMessage::GuestType ArcService::ContainerImpl::guest() const { |
| 483 | return guest_; |
| 484 | } |
| 485 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 486 | uint32_t ArcService::ContainerImpl::id() const { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 487 | return pid_; |
| 488 | } |
| 489 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 490 | bool ArcService::ContainerImpl::Start(uint32_t pid) { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 491 | // This could happen if something crashes and the stop signal is not sent. |
| 492 | // It can probably be addressed by stopping and restarting the service. |
| 493 | if (pid_ != kInvalidPID) |
| 494 | return false; |
| 495 | |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 496 | if (pid == kInvalidPID) { |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 497 | LOG(ERROR) << "Cannot start service - invalid container PID"; |
| 498 | return false; |
| 499 | } |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 500 | pid_ = pid; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 501 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 502 | Device::Options opts{ |
| 503 | .fwd_multicast = false, |
| 504 | .ipv6_enabled = false, |
| 505 | .use_default_interface = false, |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 506 | }; |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 507 | auto config = MakeArcConfig(addr_mgr_, AddressManager::Guest::ARC); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 508 | |
| 509 | // Create the bridge. |
| 510 | // Per crbug/1008686 this device cannot be deleted and then re-added. |
| 511 | // So instead of removing the bridge when the service stops, bring down the |
| 512 | // device instead and re-up it on restart. |
| 513 | if (!datapath_->AddBridge(kArcBridge, config->host_ipv4_addr(), 30) && |
| 514 | !datapath_->MaskInterfaceFlags(kArcBridge, IFF_UP)) { |
| 515 | LOG(ERROR) << "Failed to bring up arc bridge: " << kArcBridge; |
| 516 | return false; |
| 517 | } |
| 518 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 519 | arc_device_ = std::make_unique<Device>(kArcIfname, kArcBridge, kArcIfname, |
| 520 | std::move(config), opts); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 521 | |
| 522 | OnStartDevice(arc_device_.get()); |
| 523 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 524 | LOG(INFO) << "ARC++ network service started {pid: " << pid_ << "}"; |
| 525 | return true; |
| 526 | } |
| 527 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 528 | void ArcService::ContainerImpl::Stop(uint32_t /*pid*/) { |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 529 | if (!IsStarted()) |
Taoyu Li | 1c96d27 | 2019-12-13 14:17:43 +0900 | [diff] [blame] | 530 | return; |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 531 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 532 | // Per crbug/1008686 this device cannot be deleted and then re-added. |
| 533 | // So instead of removing the bridge, bring it down and mark it. This will |
| 534 | // allow us to detect if the device is re-added in case of a crash restart |
| 535 | // and do the right thing. |
| 536 | if (arc_device_) { |
| 537 | OnStopDevice(arc_device_.get()); |
| 538 | if (!datapath_->MaskInterfaceFlags(kArcBridge, IFF_DEBUG, IFF_UP)) |
| 539 | LOG(ERROR) << "Failed to bring down arc bridge " |
| 540 | << "- it may not restart correctly"; |
| 541 | } |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 542 | |
| 543 | LOG(INFO) << "ARC++ network service stopped {pid: " << pid_ << "}"; |
| 544 | pid_ = kInvalidPID; |
| 545 | } |
| 546 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 547 | bool ArcService::ContainerImpl::IsStarted(uint32_t* pid) const { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 548 | if (pid) |
| 549 | *pid = pid_; |
| 550 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 551 | return pid_ != kInvalidPID; |
| 552 | } |
| 553 | |
| 554 | bool ArcService::ContainerImpl::OnStartDevice(Device* device) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 555 | LOG(INFO) << "Starting device " << device->phys_ifname() |
| 556 | << " bridge: " << device->host_ifname() |
| 557 | << " guest_iface: " << device->guest_ifname() << " pid: " << pid_; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 558 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 559 | // Set up the virtual pair inside the container namespace. |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 560 | const std::string veth_ifname = ArcVethHostName(device->guest_ifname()); |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 561 | { |
| 562 | ScopedNS ns(pid_); |
| 563 | if (!ns.IsValid() && pid_ != kTestPID) { |
| 564 | LOG(ERROR) |
| 565 | << "Cannot create virtual link -- invalid container namespace?"; |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | if (!datapath_->AddVirtualInterfacePair(veth_ifname, |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 570 | device->guest_ifname())) { |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 571 | LOG(ERROR) << "Failed to create virtual interface pair for " |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 572 | << device->phys_ifname(); |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 573 | return false; |
| 574 | } |
| 575 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 576 | const auto& config = device->config(); |
| 577 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 578 | if (!datapath_->ConfigureInterface( |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 579 | device->guest_ifname(), config.mac_addr(), config.guest_ipv4_addr(), |
| 580 | 30, true /* link up */, device->options().fwd_multicast)) { |
| 581 | LOG(ERROR) << "Failed to configure interface " << device->guest_ifname(); |
| 582 | datapath_->RemoveInterface(device->guest_ifname()); |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 583 | return false; |
| 584 | } |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 585 | } |
| 586 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 587 | // Now pull the host end out into the root namespace and add it to the bridge. |
| 588 | if (datapath_->runner().RestoreDefaultNamespace(veth_ifname, pid_) != 0) { |
| 589 | LOG(ERROR) << "Failed to prepare interface " << veth_ifname; |
| 590 | { |
| 591 | ScopedNS ns(pid_); |
| 592 | if (ns.IsValid()) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 593 | datapath_->RemoveInterface(device->guest_ifname()); |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 594 | } else { |
| 595 | LOG(ERROR) << "Failed to re-enter container namespace." |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 596 | << " Subsequent attempts to restart " |
| 597 | << device->phys_ifname() << " may not succeed."; |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | return false; |
| 601 | } |
| 602 | if (!datapath_->ToggleInterface(veth_ifname, true /*up*/)) { |
| 603 | LOG(ERROR) << "Failed to bring up interface " << veth_ifname; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 604 | datapath_->RemoveInterface(veth_ifname); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 605 | return false; |
| 606 | } |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 607 | if (!datapath_->AddToBridge(device->host_ifname(), veth_ifname)) { |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 608 | datapath_->RemoveInterface(veth_ifname); |
| 609 | LOG(ERROR) << "Failed to bridge interface " << veth_ifname; |
| 610 | return false; |
| 611 | } |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 612 | |
Garrick Evans | 3bd0637 | 2020-03-23 10:42:58 +0900 | [diff] [blame] | 613 | if (device != arc_device_.get()) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 614 | forwarder_->StartForwarding(device->phys_ifname(), device->host_ifname(), |
| 615 | device->options().ipv6_enabled, |
| 616 | device->options().fwd_multicast); |
Garrick Evans | 3bd0637 | 2020-03-23 10:42:58 +0900 | [diff] [blame] | 617 | } else { |
| 618 | // Signal the container that the network device is ready. |
| 619 | datapath_->runner().WriteSentinelToContainer(pid_); |
| 620 | } |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 621 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 622 | return true; |
| 623 | } |
| 624 | |
| 625 | void ArcService::ContainerImpl::OnStopDevice(Device* device) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 626 | LOG(INFO) << "Stopping device " << device->phys_ifname() |
| 627 | << " bridge: " << device->host_ifname() |
| 628 | << " guest_iface: " << device->guest_ifname() << " pid: " << pid_; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 629 | |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 630 | if (device != arc_device_.get()) |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 631 | forwarder_->StopForwarding(device->phys_ifname(), device->host_ifname(), |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 632 | device->options().ipv6_enabled, |
| 633 | device->options().fwd_multicast); |
| 634 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 635 | datapath_->RemoveInterface(ArcVethHostName(device->phys_ifname())); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | void ArcService::ContainerImpl::OnDefaultInterfaceChanged( |
Garrick Evans | b05a7ff | 2020-02-18 12:59:55 +0900 | [diff] [blame] | 639 | const std::string& new_ifname, const std::string& prev_ifname) {} |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 640 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 641 | // VM specific functions |
| 642 | |
Garrick Evans | bbdf4b4 | 2020-03-05 12:59:06 +0900 | [diff] [blame] | 643 | ArcService::VmImpl::VmImpl(ShillClient* shill_client, |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 644 | Datapath* datapath, |
| 645 | AddressManager* addr_mgr, |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 646 | TrafficForwarder* forwarder, |
| 647 | bool enable_multinet) |
Garrick Evans | bbdf4b4 | 2020-03-05 12:59:06 +0900 | [diff] [blame] | 648 | : cid_(kInvalidCID), |
| 649 | shill_client_(shill_client), |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 650 | datapath_(datapath), |
| 651 | addr_mgr_(addr_mgr), |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 652 | forwarder_(forwarder), |
| 653 | enable_multinet_(enable_multinet) {} |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 654 | |
| 655 | GuestMessage::GuestType ArcService::VmImpl::guest() const { |
| 656 | return GuestMessage::ARC_VM; |
| 657 | } |
| 658 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 659 | uint32_t ArcService::VmImpl::id() const { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 660 | return cid_; |
| 661 | } |
| 662 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 663 | bool ArcService::VmImpl::Start(uint32_t cid) { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 664 | // This can happen if concierge crashes and doesn't send the vm down RPC. |
| 665 | // It can probably be addressed by stopping and restarting the service. |
| 666 | if (cid_ != kInvalidCID) |
| 667 | return false; |
| 668 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 669 | if (cid == kInvalidCID) { |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 670 | LOG(ERROR) << "Invalid VM cid " << cid; |
| 671 | return false; |
| 672 | } |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 673 | cid_ = cid; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 674 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 675 | // Allocate pool of 6 TAP devices and IPv4 allocs |
| 676 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 677 | Device::Options opts{ |
| 678 | .fwd_multicast = true, |
| 679 | .ipv6_enabled = true, |
| 680 | .use_default_interface = true, |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 681 | }; |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 682 | auto config = MakeArcConfig(addr_mgr_, AddressManager::Guest::VM_ARC); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 683 | |
| 684 | // Create the bridge. |
| 685 | if (!datapath_->AddBridge(kArcVmBridge, config->host_ipv4_addr(), 30)) { |
| 686 | LOG(ERROR) << "Failed to setup arc bridge: " << kArcVmBridge; |
| 687 | return false; |
| 688 | } |
| 689 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 690 | arc_device_ = std::make_unique<Device>(kArcVmIfname, kArcVmBridge, |
| 691 | kArcVmIfname, std::move(config), opts); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 692 | |
| 693 | OnStartDevice(arc_device_.get()); |
| 694 | |
| 695 | LOG(INFO) << "ARCVM network service started {cid: " << cid_ << "}"; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 696 | return true; |
| 697 | } |
| 698 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 699 | void ArcService::VmImpl::Stop(uint32_t cid) { |
Garrick Evans | 21173b1 | 2019-11-20 15:23:16 +0900 | [diff] [blame] | 700 | if (cid_ != cid) { |
| 701 | LOG(ERROR) << "Mismatched ARCVM CIDs " << cid_ << " != " << cid; |
| 702 | return; |
| 703 | } |
| 704 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 705 | OnStopDevice(arc_device_.get()); |
| 706 | datapath_->RemoveBridge(kArcVmBridge); |
| 707 | arc_device_.reset(); |
| 708 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 709 | LOG(INFO) << "ARCVM network service stopped {cid: " << cid_ << "}"; |
| 710 | cid_ = kInvalidCID; |
| 711 | } |
| 712 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 713 | bool ArcService::VmImpl::IsStarted(uint32_t* cid) const { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 714 | if (cid) |
| 715 | *cid = cid_; |
| 716 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 717 | return cid_ != kInvalidCID; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | bool ArcService::VmImpl::OnStartDevice(Device* device) { |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 721 | // TODO(garrick): Remove once ARCVM P is gone. |
| 722 | if (device == arc_device_.get() && !enable_multinet_) |
| 723 | return OnStartArcPDevice(); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 724 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 725 | LOG(INFO) << "Starting device " << device->phys_ifname() |
| 726 | << " bridge: " << device->host_ifname() |
| 727 | << " guest_iface: " << device->guest_ifname() << " cid: " << cid_; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 728 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 729 | // Since the interface will be added to the bridge, no address configuration |
| 730 | // should be provided here. |
| 731 | std::string tap = |
| 732 | datapath_->AddTAP("" /* auto-generate name */, nullptr /* no mac addr */, |
| 733 | nullptr /* no ipv4 subnet */, vm_tools::kCrosVmUser); |
| 734 | if (tap.empty()) { |
| 735 | LOG(ERROR) << "Failed to create TAP device for VM"; |
| 736 | return false; |
| 737 | } |
| 738 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 739 | if (!datapath_->AddToBridge(device->host_ifname(), tap)) { |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 740 | LOG(ERROR) << "Failed to bridge TAP device " << tap; |
| 741 | datapath_->RemoveInterface(tap); |
| 742 | return false; |
| 743 | } |
| 744 | |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 745 | device->set_tap_ifname(tap); |
| 746 | |
| 747 | if (device != arc_device_.get()) |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 748 | forwarder_->StartForwarding(device->phys_ifname(), device->host_ifname(), |
| 749 | device->options().ipv6_enabled, |
| 750 | device->options().fwd_multicast); |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 751 | |
| 752 | return true; |
| 753 | } |
| 754 | |
| 755 | bool ArcService::VmImpl::OnStartArcPDevice() { |
| 756 | LOG(INFO) << "Starting device " << kArcVmIfname << " bridge: " << kArcVmBridge |
| 757 | << " guest_iface: " << kArcVmIfname << " cid: " << cid_; |
| 758 | |
| 759 | // Since the interface will be added to the bridge, no address configuration |
| 760 | // should be provided here. |
| 761 | std::string tap = |
| 762 | datapath_->AddTAP("" /* auto-generate name */, nullptr /* no mac addr */, |
| 763 | nullptr /* no ipv4 subnet */, vm_tools::kCrosVmUser); |
| 764 | if (tap.empty()) { |
| 765 | LOG(ERROR) << "Failed to create TAP device for VM"; |
| 766 | return false; |
| 767 | } |
| 768 | |
| 769 | if (!datapath_->AddToBridge(kArcVmBridge, tap)) { |
| 770 | LOG(ERROR) << "Failed to bridge TAP device " << tap; |
| 771 | datapath_->RemoveInterface(tap); |
| 772 | return false; |
| 773 | } |
| 774 | |
| 775 | arc_device_->set_tap_ifname(tap); |
| 776 | |
| 777 | // Setup the iptables. |
| 778 | if (!datapath_->AddLegacyIPv4DNAT( |
| 779 | IPv4AddressToString(arc_device_->config().guest_ipv4_addr()))) |
| 780 | LOG(ERROR) << "Failed to configure ARC traffic rules"; |
| 781 | |
| 782 | if (!datapath_->AddOutboundIPv4(kArcVmBridge)) |
| 783 | LOG(ERROR) << "Failed to configure egress traffic rules"; |
| 784 | |
Garrick Evans | bbdf4b4 | 2020-03-05 12:59:06 +0900 | [diff] [blame] | 785 | OnDefaultInterfaceChanged(shill_client_->default_interface(), |
| 786 | "" /*previous*/); |
Garrick Evans | bcce09e | 2020-03-10 15:08:04 +0900 | [diff] [blame] | 787 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 788 | return true; |
| 789 | } |
| 790 | |
| 791 | void ArcService::VmImpl::OnStopDevice(Device* device) { |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 792 | // TODO(garrick): Remove once ARCVM P is gone. |
| 793 | if (device == arc_device_.get() && !enable_multinet_) |
| 794 | return OnStopArcPDevice(); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 795 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 796 | LOG(INFO) << "Stopping " << device->phys_ifname() |
| 797 | << " bridge: " << device->host_ifname() |
| 798 | << " guest_iface: " << device->guest_ifname() << " cid: " << cid_; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 799 | |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 800 | if (device != arc_device_.get()) |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 801 | forwarder_->StopForwarding(device->phys_ifname(), device->host_ifname(), |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 802 | device->options().ipv6_enabled, |
| 803 | device->options().fwd_multicast); |
Garrick Evans | bcce09e | 2020-03-10 15:08:04 +0900 | [diff] [blame] | 804 | |
| 805 | datapath_->RemoveInterface(device->tap_ifname()); |
| 806 | device->set_tap_ifname(""); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 807 | } |
| 808 | |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 809 | void ArcService::VmImpl::OnStopArcPDevice() { |
| 810 | LOG(INFO) << "Stopping " << kArcVmIfname << " bridge: " << kArcVmBridge |
| 811 | << " guest_iface: " << kArcVmIfname << " cid: " << cid_; |
| 812 | |
| 813 | datapath_->RemoveOutboundIPv4(kArcVmBridge); |
| 814 | datapath_->RemoveLegacyIPv4DNAT(); |
| 815 | |
| 816 | OnDefaultInterfaceChanged("" /*new_ifname*/, |
| 817 | shill_client_->default_interface()); |
| 818 | |
| 819 | datapath_->RemoveInterface(arc_device_->tap_ifname()); |
| 820 | arc_device_->set_tap_ifname(""); |
| 821 | } |
| 822 | |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 823 | void ArcService::VmImpl::OnDefaultInterfaceChanged( |
| 824 | const std::string& new_ifname, const std::string& prev_ifname) { |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 825 | if (!IsStarted() || enable_multinet_) |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 826 | return; |
| 827 | |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 828 | forwarder_->StopForwarding(prev_ifname, kArcVmBridge, true /*ipv6*/, |
| 829 | true /*multicast*/); |
| 830 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 831 | datapath_->RemoveLegacyIPv4InboundDNAT(); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 832 | |
| 833 | // If a new default interface was given, then re-enable with that. |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 834 | if (!new_ifname.empty()) { |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 835 | datapath_->AddLegacyIPv4InboundDNAT(new_ifname); |
Jason Jeremy Iman | 0e9f826 | 2020-03-06 14:50:49 +0900 | [diff] [blame] | 836 | forwarder_->StartForwarding(new_ifname, kArcVmBridge, true /*ipv6*/, |
| 837 | true /*multicast*/); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 841 | } // namespace arc_networkd |