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() { |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame^] | 224 | available_configs_.clear(); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 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(); |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame^] | 253 | available_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 | |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame^] | 259 | void 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(); |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame^] | 268 | all_configs_.clear(); |
| 269 | for (const auto& kv : available_configs_) |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 270 | for (const auto& c : kv.second) |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame^] | 271 | all_configs_.emplace_back(c.get()); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 272 | for (const auto& d : existing_devices) { |
| 273 | AddDevice(d); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | std::unique_ptr<Device::Config> ArcService::AcquireConfig( |
| 278 | const std::string& ifname) { |
| 279 | auto itype = InterfaceTypeFor(ifname); |
| 280 | if (itype == InterfaceType::UNKNOWN) { |
| 281 | LOG(ERROR) << "Unsupported interface: " << ifname; |
| 282 | return nullptr; |
| 283 | } |
| 284 | |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame^] | 285 | auto& configs = available_configs_[itype]; |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 286 | if (configs.empty()) { |
| 287 | LOG(ERROR) << "No more addresses available. Cannot make device for " |
| 288 | << ifname; |
| 289 | return nullptr; |
| 290 | } |
| 291 | std::unique_ptr<Device::Config> config; |
| 292 | config = std::move(configs.front()); |
| 293 | configs.pop_front(); |
| 294 | return config; |
| 295 | } |
| 296 | |
| 297 | void ArcService::ReleaseConfig(const std::string& ifname, |
| 298 | std::unique_ptr<Device::Config> config) { |
| 299 | auto itype = InterfaceTypeFor(ifname); |
| 300 | if (itype == InterfaceType::UNKNOWN) { |
| 301 | LOG(ERROR) << "Unsupported interface: " << ifname; |
| 302 | return; |
| 303 | } |
| 304 | |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame^] | 305 | available_configs_[itype].push_front(std::move(config)); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 306 | } |
| 307 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 308 | bool ArcService::Start(uint32_t id) { |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 309 | if (impl_) { |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 310 | uint32_t prev_id; |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 311 | if (impl_->IsStarted(&prev_id)) { |
| 312 | LOG(WARNING) << "Already running - did something crash?" |
| 313 | << " Stopping and restarting..."; |
| 314 | Stop(prev_id); |
| 315 | } |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 316 | } |
| 317 | |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame^] | 318 | ReallocateAddressConfigs(); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 319 | std::unique_ptr<Device::Config> arc_device_config = |
| 320 | MakeArcConfig(addr_mgr_, AddressManager::Guest::ARC, guest_); |
| 321 | if (guest_ == GuestMessage::ARC_VM) { |
| 322 | // Append arc0 config separately from ArcService::ReallocateAddressConfigs() |
| 323 | // so that VmImpl::Start() can create the necessary tap device. |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame^] | 324 | all_configs_.insert(all_configs_.begin(), arc_device_config.get()); |
| 325 | // Allocate TAP devices for all configs. |
| 326 | for (auto* config : all_configs_) { |
| 327 | auto mac = config->mac_addr(); |
| 328 | auto tap = datapath_->AddTAP("" /* auto-generate name */, &mac, |
| 329 | nullptr /* no ipv4 subnet */, |
| 330 | vm_tools::kCrosVmUser); |
| 331 | if (tap.empty()) { |
| 332 | LOG(ERROR) << "Failed to create TAP device"; |
| 333 | continue; |
| 334 | } |
| 335 | |
| 336 | config->set_tap_ifname(tap); |
| 337 | } |
| 338 | impl_ = std::make_unique<VmImpl>(datapath_); |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 339 | } else { |
Hugo Benichi | 4833fda | 2020-07-06 14:56:56 +0900 | [diff] [blame] | 340 | impl_ = std::make_unique<ContainerImpl>(datapath_); |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 341 | if (!datapath_->NetnsAttachName(kArcNetnsName, id)) { |
| 342 | LOG(ERROR) << "Failed to attach name " << kArcNetnsName << " to pid " |
| 343 | << id; |
| 344 | return false; |
| 345 | } |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 346 | } |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 347 | if (!impl_->Start(id)) { |
| 348 | impl_.reset(); |
Garrick Evans | 508a4bc | 2019-11-14 08:45:52 +0900 | [diff] [blame] | 349 | return false; |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 350 | } |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 351 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 352 | // Create the bridge for the management device arc0. |
| 353 | // Per crbug/1008686 this device cannot be deleted and then re-added. |
| 354 | // So instead of removing the bridge when the service stops, bring down the |
| 355 | // device instead and re-up it on restart. |
| 356 | if (!datapath_->AddBridge(kArcBridge, arc_device_config->host_ipv4_addr(), |
| 357 | 30) && |
| 358 | !datapath_->MaskInterfaceFlags(kArcBridge, IFF_UP)) { |
| 359 | LOG(ERROR) << "Failed to bring up arc bridge " << kArcBridge; |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | Device::Options opts{ |
| 364 | .fwd_multicast = false, |
| 365 | .ipv6_enabled = false, |
| 366 | }; |
| 367 | arc_device_ = std::make_unique<Device>(kArcIfname, kArcBridge, kArcIfname, |
| 368 | std::move(arc_device_config), opts); |
| 369 | |
| 370 | std::string arc_device_ifname; |
| 371 | if (guest_ == GuestMessage::ARC_VM) { |
| 372 | arc_device_ifname = arc_device_->config().tap_ifname(); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 373 | } else { |
| 374 | arc_device_ifname = ArcVethHostName(arc_device_->guest_ifname()); |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 375 | if (!datapath_->ConnectVethPair( |
| 376 | impl_->id(), kArcNetnsName, arc_device_ifname, |
| 377 | arc_device_->guest_ifname(), arc_device_->config().mac_addr(), |
| 378 | arc_device_->config().guest_ipv4_addr(), 30, |
| 379 | arc_device_->options().fwd_multicast)) { |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 380 | LOG(ERROR) << "Cannot create virtual link for device " |
| 381 | << arc_device_->phys_ifname(); |
| 382 | return false; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | if (!datapath_->AddToBridge(kArcBridge, arc_device_ifname)) { |
| 387 | LOG(ERROR) << "Failed to bridge arc device " << arc_device_ifname << " to " |
| 388 | << kArcBridge; |
| 389 | return false; |
| 390 | } |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 391 | LOG(INFO) << "Started ARC management device " << *arc_device_.get(); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 392 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 393 | // Start already known Shill <-> ARC mapped devices. |
| 394 | for (const auto& d : devices_) |
| 395 | StartDevice(d.second.get()); |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 396 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 397 | return true; |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 398 | } |
| 399 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 400 | void ArcService::Stop(uint32_t id) { |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 401 | // Stop Shill <-> ARC mapped devices. |
| 402 | for (const auto& d : devices_) |
| 403 | StopDevice(d.second.get()); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 404 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 405 | // Per crbug/1008686 this device cannot be deleted and then re-added. |
| 406 | // So instead of removing the bridge, bring it down and mark it. This will |
| 407 | // allow us to detect if the device is re-added in case of a crash restart |
| 408 | // and do the right thing. |
| 409 | if (!datapath_->MaskInterfaceFlags(kArcBridge, IFF_DEBUG, IFF_UP)) |
| 410 | LOG(ERROR) << "Failed to bring down arc bridge " |
| 411 | << "- it may not restart correctly"; |
| 412 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 413 | if (guest_ == GuestMessage::ARC) { |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 414 | datapath_->RemoveInterface(ArcVethHostName(arc_device_->phys_ifname())); |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 415 | if (!datapath_->NetnsDeleteName(kArcNetnsName)) |
| 416 | LOG(WARNING) << "Failed to delete netns name " << kArcNetnsName; |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame^] | 417 | } else { |
| 418 | // Destroy pre allocated TAP devices, including the ARC management device. |
| 419 | for (const auto* config : all_configs_) |
| 420 | if (!config->tap_ifname().empty()) |
| 421 | datapath_->RemoveInterface(config->tap_ifname()); |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 422 | } |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 423 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 424 | if (impl_) { |
| 425 | impl_->Stop(id); |
| 426 | impl_.reset(); |
| 427 | } |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 428 | |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 429 | LOG(INFO) << "Stopped ARC management device " << *arc_device_.get(); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 430 | arc_device_.reset(); |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 431 | } |
| 432 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 433 | void ArcService::OnDevicesChanged(const std::set<std::string>& added, |
| 434 | const std::set<std::string>& removed) { |
| 435 | for (const std::string& name : removed) |
| 436 | RemoveDevice(name); |
| 437 | |
| 438 | for (const std::string& name : added) |
| 439 | AddDevice(name); |
| 440 | } |
| 441 | |
| 442 | void ArcService::AddDevice(const std::string& ifname) { |
| 443 | if (ifname.empty()) |
| 444 | return; |
| 445 | |
| 446 | if (devices_.find(ifname) != devices_.end()) { |
| 447 | LOG(DFATAL) << "Attemping to add already tracked device: " << ifname; |
| 448 | return; |
| 449 | } |
| 450 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 451 | auto itype = InterfaceTypeFor(ifname); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 452 | Device::Options opts{ |
| 453 | .fwd_multicast = IsMulticastInterface(ifname), |
| 454 | // TODO(crbug/726815) Also enable |ipv6_enabled| for cellular networks |
| 455 | // once IPv6 is enabled on cellular networks in shill. |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 456 | .ipv6_enabled = |
| 457 | (itype == InterfaceType::ETHERNET || itype == InterfaceType::WIFI), |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 458 | }; |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 459 | |
| 460 | auto config = AcquireConfig(ifname); |
| 461 | if (!config) { |
| 462 | LOG(ERROR) << "Cannot add device for " << ifname; |
| 463 | return; |
| 464 | } |
| 465 | |
Garrick Evans | 8a06756 | 2020-05-11 12:47:30 +0900 | [diff] [blame] | 466 | auto device = std::make_unique<Device>(ifname, ArcBridgeName(ifname), ifname, |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 467 | std::move(config), opts); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 468 | |
| 469 | StartDevice(device.get()); |
| 470 | devices_.emplace(ifname, std::move(device)); |
| 471 | } |
| 472 | |
| 473 | void ArcService::StartDevice(Device* device) { |
| 474 | if (!impl_ || !impl_->IsStarted()) |
| 475 | return; |
| 476 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 477 | const auto& config = device->config(); |
| 478 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 479 | LOG(INFO) << "Starting device " << *device; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 480 | |
| 481 | // Create the bridge. |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 482 | if (!datapath_->AddBridge(device->host_ifname(), config.host_ipv4_addr(), |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 483 | 30)) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 484 | LOG(ERROR) << "Failed to setup arc bridge: " << device->host_ifname(); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 485 | return; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 486 | } |
| 487 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 488 | // Set up iptables. |
| 489 | if (!datapath_->AddInboundIPv4DNAT( |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 490 | device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr()))) |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 491 | LOG(ERROR) << "Failed to configure ingress traffic rules for " |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 492 | << device->phys_ifname(); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 493 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 494 | if (!datapath_->AddOutboundIPv4(device->host_ifname())) |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 495 | LOG(ERROR) << "Failed to configure egress traffic rules for " |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 496 | << device->phys_ifname(); |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 497 | |
Hugo Benichi | 4833fda | 2020-07-06 14:56:56 +0900 | [diff] [blame] | 498 | if (!impl_->OnStartDevice(device)) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 499 | LOG(ERROR) << "Failed to start device " << device->phys_ifname(); |
Hugo Benichi | 4833fda | 2020-07-06 14:56:56 +0900 | [diff] [blame] | 500 | return; |
| 501 | } |
| 502 | |
| 503 | forwarder_->StartForwarding(device->phys_ifname(), device->host_ifname(), |
| 504 | device->options().ipv6_enabled, |
| 505 | device->options().fwd_multicast); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | void ArcService::RemoveDevice(const std::string& ifname) { |
| 509 | const auto it = devices_.find(ifname); |
| 510 | if (it == devices_.end()) { |
| 511 | LOG(WARNING) << "Unknown device: " << ifname; |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 512 | return; |
| 513 | } |
| 514 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 515 | // If the container is down, this call does nothing. |
| 516 | StopDevice(it->second.get()); |
| 517 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 518 | ReleaseConfig(ifname, it->second->release_config()); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 519 | devices_.erase(it); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 520 | } |
| 521 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 522 | void ArcService::StopDevice(Device* device) { |
| 523 | if (!impl_ || !impl_->IsStarted()) |
| 524 | return; |
| 525 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 526 | LOG(INFO) << "Removing device " << *device; |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 527 | |
Hugo Benichi | 4833fda | 2020-07-06 14:56:56 +0900 | [diff] [blame] | 528 | forwarder_->StopForwarding(device->phys_ifname(), device->host_ifname(), |
| 529 | device->options().ipv6_enabled, |
| 530 | device->options().fwd_multicast); |
| 531 | // TAP devices are removed in VmImpl::Stop(). |
| 532 | if (guest_ == GuestMessage::ARC) |
| 533 | datapath_->RemoveInterface(ArcVethHostName(device->phys_ifname())); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 534 | |
| 535 | const auto& config = device->config(); |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 536 | datapath_->RemoveOutboundIPv4(device->host_ifname()); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 537 | datapath_->RemoveInboundIPv4DNAT( |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 538 | device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr())); |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 539 | datapath_->RemoveBridge(device->host_ifname()); |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 540 | } |
| 541 | |
Garrick Evans | 38b25a4 | 2020-04-06 15:17:42 +0900 | [diff] [blame] | 542 | std::vector<const Device::Config*> ArcService::GetDeviceConfigs() const { |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame^] | 543 | std::vector<const Device::Config*> configs; |
| 544 | for (auto* c : all_configs_) |
| 545 | configs.emplace_back(c); |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 546 | |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame^] | 547 | return configs; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 548 | } |
| 549 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 550 | // ARC++ specific functions. |
| 551 | |
Hugo Benichi | 4833fda | 2020-07-06 14:56:56 +0900 | [diff] [blame] | 552 | ArcService::ContainerImpl::ContainerImpl(Datapath* datapath) |
| 553 | : pid_(kInvalidPID), datapath_(datapath) { |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 554 | OneTimeSetup(*datapath_); |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 555 | } |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 556 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 557 | uint32_t ArcService::ContainerImpl::id() const { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 558 | return pid_; |
| 559 | } |
| 560 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 561 | bool ArcService::ContainerImpl::Start(uint32_t pid) { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 562 | // This could happen if something crashes and the stop signal is not sent. |
| 563 | // It can probably be addressed by stopping and restarting the service. |
| 564 | if (pid_ != kInvalidPID) |
| 565 | return false; |
| 566 | |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 567 | if (pid == kInvalidPID) { |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 568 | LOG(ERROR) << "Cannot start service - invalid container PID"; |
| 569 | return false; |
| 570 | } |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 571 | pid_ = pid; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 572 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 573 | LOG(INFO) << "ARC++ network service started {pid: " << pid_ << "}"; |
| 574 | return true; |
| 575 | } |
| 576 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 577 | void ArcService::ContainerImpl::Stop(uint32_t /*pid*/) { |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 578 | if (!IsStarted()) |
Taoyu Li | 1c96d27 | 2019-12-13 14:17:43 +0900 | [diff] [blame] | 579 | return; |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 580 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 581 | LOG(INFO) << "ARC++ network service stopped {pid: " << pid_ << "}"; |
| 582 | pid_ = kInvalidPID; |
| 583 | } |
| 584 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 585 | bool ArcService::ContainerImpl::IsStarted(uint32_t* pid) const { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 586 | if (pid) |
| 587 | *pid = pid_; |
| 588 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 589 | return pid_ != kInvalidPID; |
| 590 | } |
| 591 | |
| 592 | bool ArcService::ContainerImpl::OnStartDevice(Device* device) { |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 593 | // Set up the virtual pair inside the container namespace. |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 594 | const std::string veth_ifname = ArcVethHostName(device->guest_ifname()); |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 595 | const auto& config = device->config(); |
Hugo Benichi | 33860d7 | 2020-07-09 16:34:01 +0900 | [diff] [blame] | 596 | if (!datapath_->ConnectVethPair(pid_, kArcNetnsName, veth_ifname, |
| 597 | device->guest_ifname(), config.mac_addr(), |
| 598 | config.guest_ipv4_addr(), 30, |
| 599 | device->options().fwd_multicast)) { |
Hugo Benichi | 7667559 | 2020-04-08 14:29:57 +0900 | [diff] [blame] | 600 | LOG(ERROR) << "Cannot create virtual link for device " |
| 601 | << device->phys_ifname(); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 602 | return false; |
| 603 | } |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 604 | if (!datapath_->AddToBridge(device->host_ifname(), veth_ifname)) { |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 605 | datapath_->RemoveInterface(veth_ifname); |
| 606 | LOG(ERROR) << "Failed to bridge interface " << veth_ifname; |
| 607 | return false; |
| 608 | } |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 609 | return true; |
| 610 | } |
| 611 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 612 | // VM specific functions |
| 613 | |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame^] | 614 | ArcService::VmImpl::VmImpl(Datapath* datapath) |
| 615 | : cid_(kInvalidCID), datapath_(datapath) {} |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 616 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 617 | uint32_t ArcService::VmImpl::id() const { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 618 | return cid_; |
| 619 | } |
| 620 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 621 | bool ArcService::VmImpl::Start(uint32_t cid) { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 622 | // This can happen if concierge crashes and doesn't send the vm down RPC. |
| 623 | // It can probably be addressed by stopping and restarting the service. |
| 624 | if (cid_ != kInvalidCID) |
| 625 | return false; |
| 626 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 627 | if (cid == kInvalidCID) { |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 628 | LOG(ERROR) << "Invalid VM cid " << cid; |
| 629 | return false; |
| 630 | } |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 631 | cid_ = cid; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 632 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 633 | LOG(INFO) << "ARCVM network service started {cid: " << cid_ << "}"; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 634 | return true; |
| 635 | } |
| 636 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 637 | void ArcService::VmImpl::Stop(uint32_t cid) { |
Garrick Evans | 21173b1 | 2019-11-20 15:23:16 +0900 | [diff] [blame] | 638 | if (cid_ != cid) { |
| 639 | LOG(ERROR) << "Mismatched ARCVM CIDs " << cid_ << " != " << cid; |
| 640 | return; |
| 641 | } |
| 642 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 643 | LOG(INFO) << "ARCVM network service stopped {cid: " << cid_ << "}"; |
| 644 | cid_ = kInvalidCID; |
| 645 | } |
| 646 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 647 | bool ArcService::VmImpl::IsStarted(uint32_t* cid) const { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 648 | if (cid) |
| 649 | *cid = cid_; |
| 650 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 651 | return cid_ != kInvalidCID; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | bool ArcService::VmImpl::OnStartDevice(Device* device) { |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 655 | const std::string& tap = device->config().tap_ifname(); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 656 | if (tap.empty()) { |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 657 | LOG(ERROR) << "No TAP device for: " << *device; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 658 | return false; |
| 659 | } |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 660 | if (!datapath_->AddToBridge(device->host_ifname(), tap)) { |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 661 | LOG(ERROR) << "Failed to bridge TAP device " << tap; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 662 | return false; |
| 663 | } |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 664 | return true; |
| 665 | } |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 666 | } // namespace patchpanel |