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