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