Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "arc/network/arc_service.h" |
| 6 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 7 | #include <linux/rtnetlink.h> |
| 8 | #include <net/if.h> |
| 9 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 10 | #include <utility> |
Jason Jeremy Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 11 | #include <vector> |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 12 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 13 | #include <base/bind.h> |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 14 | #include <base/files/file_path.h> |
| 15 | #include <base/files/file_util.h> |
| 16 | #include <base/logging.h> |
| 17 | #include <base/strings/string_number_conversions.h> |
| 18 | #include <base/strings/string_util.h> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 19 | #include <base/strings/stringprintf.h> |
Garrick Evans | 1f5a361 | 2019-11-08 12:59:03 +0900 | [diff] [blame] | 20 | #include <brillo/key_value_store.h> |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 21 | #include <chromeos/constants/vm_tools.h> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 22 | #include <shill/net/rtnl_message.h> |
| 23 | |
| 24 | #include "arc/network/datapath.h" |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 25 | #include "arc/network/mac_address_generator.h" |
Jason Jeremy Iman | f4156cb | 2019-11-14 15:36:22 +0900 | [diff] [blame] | 26 | #include "arc/network/manager.h" |
Garrick Evans | 3915af3 | 2019-07-25 15:44:34 +0900 | [diff] [blame] | 27 | #include "arc/network/minijailed_process_runner.h" |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 28 | #include "arc/network/net_util.h" |
| 29 | #include "arc/network/scoped_ns.h" |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 30 | |
| 31 | namespace arc_networkd { |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 32 | |
| 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 | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 39 | constexpr pid_t kTestPID = -2; |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 40 | constexpr uint32_t kInvalidCID = 0; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 41 | |
| 42 | // This wrapper is required since the base class is a singleton that hides its |
| 43 | // constructor. It is necessary here because the message loop thread has to be |
| 44 | // reassociated to the container's network namespace; and since the container |
| 45 | // can be repeatedly created and destroyed, the handler must be as well. |
| 46 | class RTNetlinkHandler : public shill::RTNLHandler { |
| 47 | public: |
| 48 | RTNetlinkHandler() = default; |
| 49 | ~RTNetlinkHandler() = default; |
| 50 | |
| 51 | private: |
| 52 | DISALLOW_COPY_AND_ASSIGN(RTNetlinkHandler); |
| 53 | }; |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 54 | |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 55 | void OneTimeSetup(const Datapath& datapath) { |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 56 | static bool done = false; |
| 57 | if (done) |
| 58 | return; |
| 59 | |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 60 | auto& runner = datapath.runner(); |
| 61 | |
| 62 | // Load networking modules needed by Android that are not compiled in the |
| 63 | // kernel. Android does not allow auto-loading of kernel modules. |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 64 | // These must succeed. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 65 | if (runner.modprobe_all({ |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 66 | // The netfilter modules needed by netd for iptables commands. |
| 67 | "ip6table_filter", |
| 68 | "ip6t_ipv6header", |
| 69 | "ip6t_REJECT", |
| 70 | // The xfrm modules needed for Android's ipsec APIs. |
| 71 | "xfrm4_mode_transport", |
| 72 | "xfrm4_mode_tunnel", |
| 73 | "xfrm6_mode_transport", |
| 74 | "xfrm6_mode_tunnel", |
| 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 | } |
| 82 | // Optional modules. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 83 | if (runner.modprobe_all({ |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 84 | // This module is not available in kernels < 3.18 |
| 85 | "nf_reject_ipv6", |
| 86 | // These modules are needed for supporting Chrome traffic on Android |
| 87 | // VPN which uses Android's NAT feature. Android NAT sets up |
| 88 | // iptables |
| 89 | // rules that use these conntrack modules for FTP/TFTP. |
| 90 | "nf_nat_ftp", |
| 91 | "nf_nat_tftp", |
Hugo Benichi | a0cde9e | 2019-12-16 11:57:20 +0900 | [diff] [blame] | 92 | // The tun module is needed by the Android 464xlat clatd process. |
| 93 | "tun", |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 94 | }) != 0) { |
| 95 | LOG(WARNING) << "One or more optional kernel modules failed to load."; |
| 96 | } |
| 97 | |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 98 | // This is only needed for CTS (b/27932574). |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 99 | if (runner.chown("655360", "655360", "/sys/class/xt_idletimer") != 0) { |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 100 | LOG(ERROR) << "Failed to change ownership of xt_idletimer."; |
| 101 | } |
| 102 | |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 103 | done = true; |
| 104 | } |
| 105 | |
Garrick Evans | 508a4bc | 2019-11-14 08:45:52 +0900 | [diff] [blame] | 106 | bool IsArcVm() { |
| 107 | const base::FilePath path("/run/chrome/is_arcvm"); |
| 108 | std::string contents; |
| 109 | if (!base::ReadFileToString(path, &contents)) { |
| 110 | PLOG(ERROR) << "Could not read " << path.value(); |
| 111 | } |
| 112 | return contents == "1"; |
| 113 | } |
| 114 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 115 | GuestMessage::GuestType ArcGuest() { |
| 116 | if (test::guest != GuestMessage::UNKNOWN_GUEST) |
| 117 | return test::guest; |
Garrick Evans | 508a4bc | 2019-11-14 08:45:52 +0900 | [diff] [blame] | 118 | |
Garrick Evans | b05a7ff | 2020-02-18 12:59:55 +0900 | [diff] [blame^] | 119 | return IsArcVm() ? GuestMessage::ARC_VM : GuestMessage::ARC; |
Garrick Evans | 508a4bc | 2019-11-14 08:45:52 +0900 | [diff] [blame] | 120 | } |
| 121 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 122 | } // namespace |
| 123 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 124 | ArcService::ArcService(ShillClient* shill_client, |
| 125 | DeviceManagerBase* dev_mgr, |
| 126 | Datapath* datapath) |
| 127 | : shill_client_(shill_client), dev_mgr_(dev_mgr), datapath_(datapath) { |
| 128 | DCHECK(shill_client_); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 129 | DCHECK(dev_mgr_); |
Taoyu Li | 179dcc6 | 2019-10-17 11:21:08 +0900 | [diff] [blame] | 130 | DCHECK(datapath_); |
Garrick Evans | 3915af3 | 2019-07-25 15:44:34 +0900 | [diff] [blame] | 131 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 132 | dev_mgr_->RegisterDeviceAddedHandler( |
| 133 | GuestMessage::ARC, |
| 134 | base::Bind(&ArcService::OnDeviceAdded, base::Unretained(this))); |
| 135 | dev_mgr_->RegisterDeviceRemovedHandler( |
| 136 | GuestMessage::ARC, |
| 137 | base::Bind(&ArcService::OnDeviceRemoved, base::Unretained(this))); |
| 138 | dev_mgr_->RegisterDefaultInterfaceChangedHandler( |
| 139 | GuestMessage::ARC, base::Bind(&ArcService::OnDefaultInterfaceChanged, |
| 140 | base::Unretained(this))); |
| 141 | } |
| 142 | |
| 143 | ArcService::~ArcService() { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 144 | if (impl_) { |
| 145 | // Stop the service. |
| 146 | Stop(impl_->id()); |
| 147 | // Delete all the bridges and veth pairs. |
| 148 | dev_mgr_->ProcessDevices( |
| 149 | base::Bind(&ArcService::OnDeviceRemoved, weak_factory_.GetWeakPtr())); |
| 150 | } |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 151 | dev_mgr_->UnregisterAllGuestHandlers(GuestMessage::ARC); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 152 | } |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 153 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 154 | bool ArcService::Start(uint32_t id) { |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 155 | if (impl_) { |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 156 | uint32_t prev_id; |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 157 | if (impl_->IsStarted(&prev_id)) { |
| 158 | LOG(WARNING) << "Already running - did something crash?" |
| 159 | << " Stopping and restarting..."; |
| 160 | Stop(prev_id); |
| 161 | } |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 162 | } |
| 163 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 164 | const auto guest = ArcGuest(); |
| 165 | if (guest == GuestMessage::ARC_VM) |
| 166 | impl_ = std::make_unique<VmImpl>(dev_mgr_, datapath_); |
| 167 | else |
| 168 | impl_ = std::make_unique<ContainerImpl>(dev_mgr_, datapath_, guest); |
| 169 | |
| 170 | if (!impl_->Start(id)) { |
| 171 | impl_.reset(); |
Garrick Evans | 508a4bc | 2019-11-14 08:45:52 +0900 | [diff] [blame] | 172 | return false; |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 173 | } |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 174 | |
| 175 | // Start known host devices, any new ones will be setup in the process. |
| 176 | dev_mgr_->ProcessDevices( |
| 177 | base::Bind(&ArcService::StartDevice, weak_factory_.GetWeakPtr())); |
| 178 | |
| 179 | // If this is the first time the service is starting this will create the |
| 180 | // Android bridge device; otherwise it does nothing (this is a workaround for |
| 181 | // the bug in Shill that casues a Bus crash when it sees the ARC bridge a |
| 182 | // second time). Do this after processing the existing devices so it doesn't |
| 183 | // get started twice. |
Garrick Evans | cfd4282 | 2019-11-15 12:38:23 +0900 | [diff] [blame] | 184 | std::string arc; |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 185 | switch (guest) { |
Garrick Evans | cfd4282 | 2019-11-15 12:38:23 +0900 | [diff] [blame] | 186 | case GuestMessage::ARC: |
| 187 | arc = kAndroidDevice; |
| 188 | break; |
Garrick Evans | cfd4282 | 2019-11-15 12:38:23 +0900 | [diff] [blame] | 189 | case GuestMessage::ARC_VM: |
| 190 | arc = kAndroidVmDevice; |
| 191 | break; |
| 192 | default: |
| 193 | LOG(DFATAL) << "Unexpected guest: " << guest; |
Garrick Evans | 21173b1 | 2019-11-20 15:23:16 +0900 | [diff] [blame] | 194 | return false; |
Garrick Evans | cfd4282 | 2019-11-15 12:38:23 +0900 | [diff] [blame] | 195 | } |
| 196 | dev_mgr_->Add(arc); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 197 | dev_mgr_->OnGuestStart(guest); |
| 198 | return true; |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 199 | } |
| 200 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 201 | void ArcService::Stop(uint32_t id) { |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 202 | if (!impl_) |
| 203 | return; |
| 204 | |
| 205 | dev_mgr_->OnGuestStop(impl_->guest()); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 206 | |
| 207 | // Stop known host devices. Note that this does not teardown any existing |
| 208 | // devices. |
| 209 | dev_mgr_->ProcessDevices( |
| 210 | base::Bind(&ArcService::StopDevice, weak_factory_.GetWeakPtr())); |
Garrick Evans | 21173b1 | 2019-11-20 15:23:16 +0900 | [diff] [blame] | 211 | |
| 212 | impl_->Stop(id); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 213 | impl_.reset(); |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 214 | } |
| 215 | |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 216 | bool ArcService::AllowDevice(Device* device) const { |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 217 | if (!device->IsArc()) |
Garrick Evans | 0001d01 | 2019-11-22 10:06:10 +0900 | [diff] [blame] | 218 | return false; |
| 219 | |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 220 | // ARC P+ is multi-network enabled and should process all devices. |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 221 | if ((impl_ && impl_->guest() == GuestMessage::ARC) || |
| 222 | (!impl_ && ArcGuest() == GuestMessage::ARC)) |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 223 | return true; |
| 224 | |
| 225 | // ARC N and ARCVM (for now) are both single-network - meaning they only use |
| 226 | // the "default" device which uses the default interface from shill. |
| 227 | return device->UsesDefaultInterface(); |
| 228 | } |
| 229 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 230 | void ArcService::OnDeviceAdded(Device* device) { |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 231 | if (!AllowDevice(device)) |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 232 | return; |
Garrick Evans | ba57574 | 2019-07-17 15:48:08 +0900 | [diff] [blame] | 233 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 234 | const auto& config = device->config(); |
| 235 | |
| 236 | LOG(INFO) << "Adding device " << device->ifname() |
| 237 | << " bridge: " << config.host_ifname() |
Garrick Evans | 310ab55 | 2019-10-08 11:07:53 +0900 | [diff] [blame] | 238 | << " guest_iface: " << config.guest_ifname(); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 239 | |
| 240 | // Create the bridge. |
Garrick Evans | 7a1a9ee | 2020-01-28 11:03:57 +0900 | [diff] [blame] | 241 | if (!datapath_->AddBridge(config.host_ifname(), config.host_ipv4_addr(), |
| 242 | 30)) { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 243 | // Per crbug/1008686 this device cannot be deleted and then re-added. |
| 244 | // It could be that arc-networkd was restarted after a crash and this device |
| 245 | // is being re-added. |
| 246 | if (!device->IsAndroid()) { |
| 247 | LOG(ERROR) << "Failed to setup arc bridge: " << config.host_ifname(); |
| 248 | return; |
| 249 | } |
| 250 | if (!datapath_->MaskInterfaceFlags(config.host_ifname(), IFF_UP)) { |
| 251 | LOG(ERROR) << "Failed to bring up arc bridge: " << config.host_ifname(); |
| 252 | return; |
| 253 | } |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // Setup the iptables. |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 257 | if (device->UsesDefaultInterface()) { |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 258 | if (!datapath_->AddLegacyIPv4DNAT( |
| 259 | IPv4AddressToString(config.guest_ipv4_addr()))) |
| 260 | LOG(ERROR) << "Failed to configure ARC traffic rules"; |
| 261 | |
| 262 | if (!datapath_->AddOutboundIPv4(config.host_ifname())) |
| 263 | LOG(ERROR) << "Failed to configure egress traffic rules"; |
| 264 | } else if (!device->IsAndroid()) { |
| 265 | if (!datapath_->AddInboundIPv4DNAT( |
| 266 | device->ifname(), IPv4AddressToString(config.guest_ipv4_addr()))) |
| 267 | LOG(ERROR) << "Failed to configure ingress traffic rules for " |
| 268 | << device->ifname(); |
| 269 | |
| 270 | if (!datapath_->AddOutboundIPv4(config.host_ifname())) |
| 271 | LOG(ERROR) << "Failed to configure egress traffic rules"; |
| 272 | } |
| 273 | |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 274 | device->set_context(std::make_unique<Context>()); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 275 | |
| 276 | StartDevice(device); |
| 277 | } |
| 278 | |
| 279 | void ArcService::StartDevice(Device* device) { |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 280 | if (!AllowDevice(device)) |
Garrick Evans | cfd4282 | 2019-11-15 12:38:23 +0900 | [diff] [blame] | 281 | return; |
| 282 | |
Garrick Evans | 310ab55 | 2019-10-08 11:07:53 +0900 | [diff] [blame] | 283 | // This can happen if OnDeviceAdded is invoked when the container is down. |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 284 | if (!impl_ || !impl_->IsStarted()) |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 285 | return; |
| 286 | |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 287 | // If there is no context, then this is a new device and it needs to run |
| 288 | // through the full setup process. |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 289 | Context* ctx = dynamic_cast<Context*>(device->context()); |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 290 | if (!ctx) |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 291 | return OnDeviceAdded(device); |
| 292 | |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 293 | if (ctx->IsStarted()) { |
| 294 | LOG(ERROR) << "Attempt to restart device " << device->ifname(); |
| 295 | return; |
| 296 | } |
| 297 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 298 | if (!impl_->OnStartDevice(device)) { |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 299 | LOG(ERROR) << "Failed to start device " << device->ifname(); |
| 300 | return; |
| 301 | } |
| 302 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 303 | ctx->Start(); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void ArcService::OnDeviceRemoved(Device* device) { |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 307 | if (!AllowDevice(device)) |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 308 | return; |
| 309 | |
Garrick Evans | 310ab55 | 2019-10-08 11:07:53 +0900 | [diff] [blame] | 310 | // If the container is down, this call does nothing. |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 311 | StopDevice(device); |
| 312 | |
| 313 | const auto& config = device->config(); |
| 314 | |
| 315 | LOG(INFO) << "Removing device " << device->ifname() |
| 316 | << " bridge: " << config.host_ifname() |
| 317 | << " guest_iface: " << config.guest_ifname(); |
| 318 | |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 319 | if (device->UsesDefaultInterface()) { |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 320 | datapath_->RemoveOutboundIPv4(config.host_ifname()); |
| 321 | datapath_->RemoveLegacyIPv4DNAT(); |
| 322 | } else if (!device->IsAndroid()) { |
| 323 | datapath_->RemoveOutboundIPv4(config.host_ifname()); |
| 324 | datapath_->RemoveInboundIPv4DNAT( |
| 325 | device->ifname(), IPv4AddressToString(config.guest_ipv4_addr())); |
| 326 | } |
| 327 | |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 328 | // Per crbug/1008686 this device cannot be deleted and then re-added. |
| 329 | // So instead of removing the bridge, bring it down and mark it. This will |
| 330 | // allow us to detect if the device is re-added in case of a crash restart and |
| 331 | // do the right thing. |
| 332 | if (device->IsAndroid()) { |
| 333 | // This can be safely deleted now. |
| 334 | datapath_->RemoveInterface(ArcVethHostName("arc0")); |
| 335 | if (!datapath_->MaskInterfaceFlags(config.host_ifname(), IFF_DEBUG, IFF_UP)) |
| 336 | LOG(ERROR) << "Failed to bring down arc bridge " |
| 337 | << "- it may not restart correctly"; |
| 338 | } else { |
| 339 | datapath_->RemoveBridge(config.host_ifname()); |
| 340 | } |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 341 | |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 342 | device->set_context(nullptr); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void ArcService::StopDevice(Device* device) { |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 346 | if (!AllowDevice(device)) |
Garrick Evans | cfd4282 | 2019-11-15 12:38:23 +0900 | [diff] [blame] | 347 | return; |
| 348 | |
Garrick Evans | 310ab55 | 2019-10-08 11:07:53 +0900 | [diff] [blame] | 349 | // This can happen if the device if OnDeviceRemoved is invoked when the |
| 350 | // container is down. |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 351 | if (!impl_ || !impl_->IsStarted()) |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 352 | return; |
| 353 | |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 354 | Context* ctx = dynamic_cast<Context*>(device->context()); |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 355 | if (!ctx) { |
| 356 | LOG(ERROR) << "Attempt to stop removed device " << device->ifname(); |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | if (!ctx->IsStarted()) { |
| 361 | LOG(ERROR) << "Attempt to re-stop device " << device->ifname(); |
| 362 | return; |
| 363 | } |
| 364 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 365 | impl_->OnStopDevice(device); |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame] | 366 | |
| 367 | ctx->Stop(); |
| 368 | } |
| 369 | |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 370 | void ArcService::OnDefaultInterfaceChanged(const std::string& new_ifname, |
| 371 | const std::string& prev_ifname) { |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 372 | if (impl_) |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 373 | impl_->OnDefaultInterfaceChanged(new_ifname, prev_ifname); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 374 | } |
Garrick Evans | ba57574 | 2019-07-17 15:48:08 +0900 | [diff] [blame] | 375 | |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 376 | // Context |
| 377 | |
| 378 | ArcService::Context::Context() : Device::Context() { |
| 379 | Stop(); |
| 380 | } |
| 381 | |
| 382 | void ArcService::Context::Start() { |
| 383 | Stop(); |
| 384 | started_ = true; |
| 385 | } |
| 386 | |
| 387 | void ArcService::Context::Stop() { |
| 388 | started_ = false; |
| 389 | link_up_ = false; |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | bool ArcService::Context::IsStarted() const { |
| 393 | return started_; |
| 394 | } |
| 395 | |
| 396 | bool ArcService::Context::IsLinkUp() const { |
| 397 | return link_up_; |
| 398 | } |
| 399 | |
| 400 | bool ArcService::Context::SetLinkUp(bool link_up) { |
| 401 | if (link_up == link_up_) |
| 402 | return false; |
| 403 | |
| 404 | link_up_ = link_up; |
| 405 | return true; |
| 406 | } |
| 407 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 408 | const std::string& ArcService::Context::TAP() const { |
| 409 | return tap_; |
| 410 | } |
| 411 | |
| 412 | void ArcService::Context::SetTAP(const std::string& tap) { |
| 413 | tap_ = tap; |
| 414 | } |
| 415 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 416 | // ARC++ specific functions. |
| 417 | |
| 418 | ArcService::ContainerImpl::ContainerImpl(DeviceManagerBase* dev_mgr, |
| 419 | Datapath* datapath, |
| 420 | GuestMessage::GuestType guest) |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 421 | : pid_(kInvalidPID), dev_mgr_(dev_mgr), datapath_(datapath), guest_(guest) { |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 422 | OneTimeSetup(*datapath_); |
Garrick Evans | a34b586 | 2019-11-20 09:34:01 +0900 | [diff] [blame] | 423 | } |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 424 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 425 | GuestMessage::GuestType ArcService::ContainerImpl::guest() const { |
| 426 | return guest_; |
| 427 | } |
| 428 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 429 | uint32_t ArcService::ContainerImpl::id() const { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 430 | return pid_; |
| 431 | } |
| 432 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 433 | bool ArcService::ContainerImpl::Start(uint32_t pid) { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 434 | // This could happen if something crashes and the stop signal is not sent. |
| 435 | // It can probably be addressed by stopping and restarting the service. |
| 436 | if (pid_ != kInvalidPID) |
| 437 | return false; |
| 438 | |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 439 | // TODO(garrick): Remove this test hack. |
| 440 | if (pid == kTestPID) { |
| 441 | LOG(WARNING) << "Running with test PID"; |
| 442 | pid_ = pid; |
| 443 | return true; |
| 444 | } |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 445 | if (pid == kInvalidPID) { |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 446 | LOG(ERROR) << "Cannot start service - invalid container PID"; |
| 447 | return false; |
| 448 | } |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 449 | pid_ = pid; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 450 | |
| 451 | // Start listening for RTNetlink messages in the container's net namespace |
| 452 | // to be notified whenever it brings up an interface. |
| 453 | { |
| 454 | ScopedNS ns(pid_); |
| 455 | if (ns.IsValid()) { |
| 456 | rtnl_handler_ = std::make_unique<RTNetlinkHandler>(); |
| 457 | rtnl_handler_->Start(RTMGRP_LINK); |
| 458 | link_listener_ = std::make_unique<shill::RTNLListener>( |
| 459 | shill::RTNLHandler::kRequestLink, |
| 460 | Bind(&ArcService::ContainerImpl::LinkMsgHandler, |
| 461 | weak_factory_.GetWeakPtr()), |
| 462 | rtnl_handler_.get()); |
| 463 | } else { |
| 464 | // This is bad - it means we won't ever be able to tell when the container |
| 465 | // brings up an interface. |
| 466 | LOG(ERROR) |
| 467 | << "Cannot start netlink listener - invalid container namespace?"; |
| 468 | return false; |
| 469 | } |
| 470 | } |
| 471 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 472 | LOG(INFO) << "ARC++ network service started {pid: " << pid_ << "}"; |
| 473 | return true; |
| 474 | } |
| 475 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 476 | void ArcService::ContainerImpl::Stop(uint32_t /*pid*/) { |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 477 | if (!IsStarted()) |
Taoyu Li | 1c96d27 | 2019-12-13 14:17:43 +0900 | [diff] [blame] | 478 | return; |
Garrick Evans | 4dec0c4 | 2019-11-29 12:51:57 +0900 | [diff] [blame] | 479 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 480 | rtnl_handler_->RemoveListener(link_listener_.get()); |
| 481 | link_listener_.reset(); |
| 482 | rtnl_handler_.reset(); |
| 483 | |
| 484 | LOG(INFO) << "ARC++ network service stopped {pid: " << pid_ << "}"; |
| 485 | pid_ = kInvalidPID; |
| 486 | } |
| 487 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 488 | bool ArcService::ContainerImpl::IsStarted(uint32_t* pid) const { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 489 | if (pid) |
| 490 | *pid = pid_; |
| 491 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 492 | return pid_ != kInvalidPID; |
| 493 | } |
| 494 | |
| 495 | bool ArcService::ContainerImpl::OnStartDevice(Device* device) { |
| 496 | const auto& config = device->config(); |
| 497 | |
| 498 | LOG(INFO) << "Starting device " << device->ifname() |
| 499 | << " bridge: " << config.host_ifname() |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 500 | << " guest_iface: " << config.guest_ifname() << " pid: " << pid_; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 501 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 502 | // Set up the virtual pair inside the container namespace. |
| 503 | const std::string veth_ifname = ArcVethHostName(config.guest_ifname()); |
| 504 | { |
| 505 | ScopedNS ns(pid_); |
| 506 | if (!ns.IsValid() && pid_ != kTestPID) { |
| 507 | LOG(ERROR) |
| 508 | << "Cannot create virtual link -- invalid container namespace?"; |
| 509 | return false; |
| 510 | } |
| 511 | |
| 512 | if (!datapath_->AddVirtualInterfacePair(veth_ifname, |
| 513 | config.guest_ifname())) { |
| 514 | LOG(ERROR) << "Failed to create virtual interface pair for " |
| 515 | << device->ifname(); |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | if (!datapath_->ConfigureInterface( |
| 520 | config.guest_ifname(), config.guest_mac_addr(), |
| 521 | config.guest_ipv4_addr(), 30, true /* link up */, |
| 522 | device->options().fwd_multicast)) { |
| 523 | LOG(ERROR) << "Failed to configure interface " << config.guest_ifname(); |
| 524 | datapath_->RemoveInterface(config.guest_ifname()); |
| 525 | return false; |
| 526 | } |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 527 | } |
| 528 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 529 | // Now pull the host end out into the root namespace and add it to the bridge. |
| 530 | if (datapath_->runner().RestoreDefaultNamespace(veth_ifname, pid_) != 0) { |
| 531 | LOG(ERROR) << "Failed to prepare interface " << veth_ifname; |
| 532 | { |
| 533 | ScopedNS ns(pid_); |
| 534 | if (ns.IsValid()) { |
| 535 | datapath_->RemoveInterface(config.guest_ifname()); |
| 536 | } else { |
| 537 | LOG(ERROR) << "Failed to re-enter container namespace." |
| 538 | << " Subsequent attempts to restart " << device->ifname() |
| 539 | << " may not succeed."; |
| 540 | } |
| 541 | } |
| 542 | return false; |
| 543 | } |
| 544 | if (!datapath_->ToggleInterface(veth_ifname, true /*up*/)) { |
| 545 | LOG(ERROR) << "Failed to bring up interface " << veth_ifname; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 546 | datapath_->RemoveInterface(veth_ifname); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 547 | return false; |
| 548 | } |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 549 | if (!datapath_->AddToBridge(config.host_ifname(), veth_ifname)) { |
| 550 | datapath_->RemoveInterface(veth_ifname); |
| 551 | LOG(ERROR) << "Failed to bridge interface " << veth_ifname; |
| 552 | return false; |
| 553 | } |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 554 | |
Taoyu Li | 1c96d27 | 2019-12-13 14:17:43 +0900 | [diff] [blame] | 555 | dev_mgr_->StartForwarding(*device); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 556 | return true; |
| 557 | } |
| 558 | |
| 559 | void ArcService::ContainerImpl::OnStopDevice(Device* device) { |
| 560 | const auto& config = device->config(); |
| 561 | |
| 562 | LOG(INFO) << "Stopping device " << device->ifname() |
| 563 | << " bridge: " << config.host_ifname() |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 564 | << " guest_iface: " << config.guest_ifname() << " pid: " << pid_; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 565 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 566 | if (!device->IsAndroid()) { |
| 567 | datapath_->RemoveInterface(ArcVethHostName(device->ifname())); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | void ArcService::ContainerImpl::OnDefaultInterfaceChanged( |
Garrick Evans | b05a7ff | 2020-02-18 12:59:55 +0900 | [diff] [blame^] | 572 | const std::string& new_ifname, const std::string& prev_ifname) {} |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 573 | |
| 574 | void ArcService::ContainerImpl::LinkMsgHandler(const shill::RTNLMessage& msg) { |
| 575 | if (!msg.HasAttribute(IFLA_IFNAME)) { |
| 576 | LOG(ERROR) << "Link event message does not have IFLA_IFNAME"; |
| 577 | return; |
| 578 | } |
| 579 | bool link_up = msg.link_status().flags & IFF_UP; |
| 580 | shill::ByteString b(msg.GetAttribute(IFLA_IFNAME)); |
| 581 | std::string ifname(reinterpret_cast<const char*>( |
| 582 | b.GetSubstring(0, IFNAMSIZ).GetConstData())); |
| 583 | |
| 584 | auto* device = dev_mgr_->FindByGuestInterface(ifname); |
| 585 | if (!device) |
| 586 | return; |
| 587 | |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 588 | Context* ctx = dynamic_cast<Context*>(device->context()); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 589 | if (!ctx) { |
| 590 | LOG(DFATAL) << "Context missing"; |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | // If the link status is unchanged, there is nothing to do. |
| 595 | if (!ctx->SetLinkUp(link_up)) |
| 596 | return; |
| 597 | |
| 598 | if (!link_up) { |
| 599 | LOG(INFO) << ifname << " is now down"; |
| 600 | return; |
| 601 | } |
| 602 | LOG(INFO) << ifname << " is now up"; |
| 603 | |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 604 | if (device->UsesDefaultInterface()) { |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 605 | OnDefaultInterfaceChanged(dev_mgr_->DefaultInterface(), "" /*previous*/); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 606 | return; |
| 607 | } |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 608 | } |
| 609 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 610 | // VM specific functions |
| 611 | |
| 612 | ArcService::VmImpl::VmImpl(DeviceManagerBase* dev_mgr, Datapath* datapath) |
| 613 | : cid_(kInvalidCID), dev_mgr_(dev_mgr), datapath_(datapath) {} |
| 614 | |
| 615 | GuestMessage::GuestType ArcService::VmImpl::guest() const { |
| 616 | return GuestMessage::ARC_VM; |
| 617 | } |
| 618 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 619 | uint32_t ArcService::VmImpl::id() const { |
Garrick Evans | 664a82f | 2019-12-17 12:18:05 +0900 | [diff] [blame] | 620 | return cid_; |
| 621 | } |
| 622 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 623 | bool ArcService::VmImpl::Start(uint32_t cid) { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 624 | // This can happen if concierge crashes and doesn't send the vm down RPC. |
| 625 | // It can probably be addressed by stopping and restarting the service. |
| 626 | if (cid_ != kInvalidCID) |
| 627 | return false; |
| 628 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 629 | if (cid == kInvalidCID) { |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 630 | LOG(ERROR) << "Invalid VM cid " << cid; |
| 631 | return false; |
| 632 | } |
| 633 | |
| 634 | cid_ = cid; |
| 635 | LOG(INFO) << "ARCVM network service started {cid: " << cid_ << "}"; |
| 636 | |
| 637 | return true; |
| 638 | } |
| 639 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 640 | void ArcService::VmImpl::Stop(uint32_t cid) { |
Garrick Evans | 21173b1 | 2019-11-20 15:23:16 +0900 | [diff] [blame] | 641 | if (cid_ != cid) { |
| 642 | LOG(ERROR) << "Mismatched ARCVM CIDs " << cid_ << " != " << cid; |
| 643 | return; |
| 644 | } |
| 645 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 646 | LOG(INFO) << "ARCVM network service stopped {cid: " << cid_ << "}"; |
| 647 | cid_ = kInvalidCID; |
| 648 | } |
| 649 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 650 | bool ArcService::VmImpl::IsStarted(uint32_t* cid) const { |
Garrick Evans | a51d0a1 | 2019-11-28 13:51:23 +0900 | [diff] [blame] | 651 | if (cid) |
| 652 | *cid = cid_; |
| 653 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 654 | return cid_ != kInvalidCID; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | bool ArcService::VmImpl::OnStartDevice(Device* device) { |
| 658 | // TODO(garrick): Remove this once ARCVM supports ad hoc interface |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 659 | // configurations. |
| 660 | if (!device->UsesDefaultInterface()) |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 661 | return false; |
| 662 | |
| 663 | const auto& config = device->config(); |
| 664 | |
| 665 | LOG(INFO) << "Starting device " << device->ifname() |
| 666 | << " bridge: " << config.host_ifname() |
| 667 | << " guest_iface: " << config.guest_ifname() << " cid: " << cid_; |
| 668 | |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 669 | Context* ctx = dynamic_cast<Context*>(device->context()); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 670 | if (!ctx) { |
| 671 | LOG(ERROR) << "Context missing"; |
| 672 | return false; |
| 673 | } |
| 674 | |
| 675 | // Since the interface will be added to the bridge, no address configuration |
| 676 | // should be provided here. |
| 677 | std::string tap = |
| 678 | datapath_->AddTAP("" /* auto-generate name */, nullptr /* no mac addr */, |
| 679 | nullptr /* no ipv4 subnet */, vm_tools::kCrosVmUser); |
| 680 | if (tap.empty()) { |
| 681 | LOG(ERROR) << "Failed to create TAP device for VM"; |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | if (!datapath_->AddToBridge(config.host_ifname(), tap)) { |
| 686 | LOG(ERROR) << "Failed to bridge TAP device " << tap; |
| 687 | datapath_->RemoveInterface(tap); |
| 688 | return false; |
| 689 | } |
| 690 | |
| 691 | ctx->SetTAP(tap); |
| 692 | // TODO(garrick): Remove this once ARCVM supports ad hoc interface |
| 693 | // configurations; but for now ARCVM needs to be treated like ARC++ N. |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 694 | OnDefaultInterfaceChanged(dev_mgr_->DefaultInterface(), "" /*previous*/); |
Taoyu Li | 1c96d27 | 2019-12-13 14:17:43 +0900 | [diff] [blame] | 695 | dev_mgr_->StartForwarding(*device); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 696 | return true; |
| 697 | } |
| 698 | |
| 699 | void ArcService::VmImpl::OnStopDevice(Device* device) { |
| 700 | // TODO(garrick): Remove this once ARCVM supports ad hoc interface |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 701 | // configurations. |
| 702 | if (!device->UsesDefaultInterface()) |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 703 | return; |
| 704 | |
| 705 | const auto& config = device->config(); |
| 706 | |
| 707 | LOG(INFO) << "Stopping " << device->ifname() |
| 708 | << " bridge: " << config.host_ifname() |
| 709 | << " guest_iface: " << config.guest_ifname() << " cid: " << cid_; |
| 710 | |
Garrick Evans | a1134d7 | 2019-12-02 14:25:37 +0900 | [diff] [blame] | 711 | Context* ctx = dynamic_cast<Context*>(device->context()); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 712 | if (!ctx) { |
| 713 | LOG(ERROR) << "Context missing"; |
| 714 | return; |
| 715 | } |
| 716 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 717 | datapath_->RemoveInterface(ctx->TAP()); |
| 718 | } |
| 719 | |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 720 | void ArcService::VmImpl::OnDefaultInterfaceChanged( |
| 721 | const std::string& new_ifname, const std::string& prev_ifname) { |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 722 | if (!IsStarted()) |
| 723 | return; |
| 724 | |
| 725 | // TODO(garrick): Remove this once ARCVM supports ad hoc interface |
| 726 | // configurations; but for now ARCVM needs to be treated like ARC++ N. |
| 727 | datapath_->RemoveLegacyIPv4InboundDNAT(); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 728 | auto* device = dev_mgr_->FindByGuestInterface("arc1"); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 729 | if (!device) { |
| 730 | LOG(DFATAL) << "Expected Android device missing"; |
| 731 | return; |
| 732 | } |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 733 | |
| 734 | // If a new default interface was given, then re-enable with that. |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 735 | if (!new_ifname.empty()) { |
| 736 | datapath_->AddLegacyIPv4InboundDNAT(new_ifname); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 740 | } // namespace arc_networkd |