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