blob: 0d1ec7b4961648a3b7f281713315babff3fc347d [file] [log] [blame]
Garrick Evans5d55f5e2019-07-17 15:28:10 +09001// 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 Evans3388a032020-03-24 11:25:55 +09005#include "patchpanel/arc_service.h"
Garrick Evans5d55f5e2019-07-17 15:28:10 +09006
Garrick Evans54861622019-07-19 09:05:09 +09007#include <linux/rtnetlink.h>
8#include <net/if.h>
Garrick Evans6e4eb3b2020-03-09 07:18:31 +09009#include <sys/ioctl.h>
Garrick Evans71e4a862020-05-18 12:22:23 +090010#include <sys/utsname.h>
Garrick Evans54861622019-07-19 09:05:09 +090011
Garrick Evans5d55f5e2019-07-17 15:28:10 +090012#include <utility>
Jason Jeremy Imanf4156cb2019-11-14 15:36:22 +090013#include <vector>
Garrick Evans5d55f5e2019-07-17 15:28:10 +090014
Garrick Evans54861622019-07-19 09:05:09 +090015#include <base/bind.h>
Garrick Evans5d55f5e2019-07-17 15:28:10 +090016#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 Evans54861622019-07-19 09:05:09 +090021#include <base/strings/stringprintf.h>
Qijiang Fan2d7aeb42020-05-19 02:06:39 +090022#include <base/system/sys_info.h>
Garrick Evans1f5a3612019-11-08 12:59:03 +090023#include <brillo/key_value_store.h>
Garrick Evansb4eb3892019-11-13 12:07:07 +090024#include <chromeos/constants/vm_tools.h>
Garrick Evans54861622019-07-19 09:05:09 +090025
Garrick Evans3388a032020-03-24 11:25:55 +090026#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 Evans5d55f5e2019-07-17 15:28:10 +090032
Garrick Evans3388a032020-03-24 11:25:55 +090033namespace patchpanel {
Garrick Evansf29f5a32019-12-06 11:34:25 +090034namespace test {
35GuestMessage::GuestType guest = GuestMessage::UNKNOWN_GUEST;
36} // namespace test
37
Garrick Evans5d55f5e2019-07-17 15:28:10 +090038namespace {
Garrick Evans015b0d62020-02-07 09:06:38 +090039constexpr pid_t kInvalidPID = 0;
Garrick Evans015b0d62020-02-07 09:06:38 +090040constexpr uint32_t kInvalidCID = 0;
Garrick Evanse94b6de2020-02-20 09:19:13 +090041constexpr char kArcIfname[] = "arc0";
42constexpr char kArcBridge[] = "arcbr0";
43constexpr char kArcVmIfname[] = "arc1";
44constexpr char kArcVmBridge[] = "arc_br1";
Garrick Evans6e4eb3b2020-03-09 07:18:31 +090045constexpr std::array<const char*, 2> kEthernetInterfacePrefixes{{"eth", "usb"}};
46constexpr std::array<const char*, 2> kWifiInterfacePrefixes{{"wlan", "mlan"}};
Garrick Evans86c7d9c2020-03-17 09:25:48 +090047constexpr std::array<const char*, 2> kCellInterfacePrefixes{{"wwan", "rmnet"}};
Garrick Evans54861622019-07-19 09:05:09 +090048
Garrick Evans71e4a862020-05-18 12:22:23 +090049bool KernelVersion(int* major, int* minor) {
50 struct utsname u;
51 if (uname(&u) != 0) {
52 PLOG(ERROR) << "uname failed";
53 *major = *minor = 0;
54 return false;
55 }
56 int unused;
57 if (sscanf(u.release, "%d.%d.%d", major, minor, &unused) != 3) {
58 LOG(ERROR) << "unexpected release string: " << u.release;
59 *major = *minor = 0;
60 return false;
61 }
62 return true;
63}
64
Garrick Evans6d227b92019-12-03 16:11:29 +090065void OneTimeSetup(const Datapath& datapath) {
Garrick Evansa34b5862019-11-20 09:34:01 +090066 static bool done = false;
67 if (done)
68 return;
69
Garrick Evans6d227b92019-12-03 16:11:29 +090070 auto& runner = datapath.runner();
71
72 // Load networking modules needed by Android that are not compiled in the
73 // kernel. Android does not allow auto-loading of kernel modules.
Garrick Evansc53b9702020-05-13 13:20:09 +090074 // Expected for all kernels.
Garrick Evans8e8e3472020-01-23 14:03:50 +090075 if (runner.modprobe_all({
Garrick Evansa34b5862019-11-20 09:34:01 +090076 // The netfilter modules needed by netd for iptables commands.
77 "ip6table_filter",
78 "ip6t_ipv6header",
79 "ip6t_REJECT",
Garrick Evansa34b5862019-11-20 09:34:01 +090080 // The ipsec modules for AH and ESP encryption for ipv6.
81 "ah6",
82 "esp6",
83 }) != 0) {
84 LOG(ERROR) << "One or more required kernel modules failed to load."
85 << " Some Android functionality may be broken.";
86 }
Garrick Evansc53b9702020-05-13 13:20:09 +090087 // The xfrm modules needed for Android's ipsec APIs on kernels < 5.4.
Garrick Evans71e4a862020-05-18 12:22:23 +090088 int major, minor;
89 if (KernelVersion(&major, &minor) &&
90 (major < 5 || (major == 5 && minor < 4)) &&
91 runner.modprobe_all({
92 "xfrm4_mode_transport",
93 "xfrm4_mode_tunnel",
94 "xfrm6_mode_transport",
95 "xfrm6_mode_tunnel",
96 }) != 0) {
Garrick Evansc53b9702020-05-13 13:20:09 +090097 LOG(ERROR) << "One or more required kernel modules failed to load."
98 << " Some Android functionality may be broken.";
99 }
100
Garrick Evansa34b5862019-11-20 09:34:01 +0900101 // Optional modules.
Garrick Evans8e8e3472020-01-23 14:03:50 +0900102 if (runner.modprobe_all({
Garrick Evansa34b5862019-11-20 09:34:01 +0900103 // This module is not available in kernels < 3.18
104 "nf_reject_ipv6",
105 // These modules are needed for supporting Chrome traffic on Android
106 // VPN which uses Android's NAT feature. Android NAT sets up
107 // iptables
108 // rules that use these conntrack modules for FTP/TFTP.
109 "nf_nat_ftp",
110 "nf_nat_tftp",
Hugo Benichia0cde9e2019-12-16 11:57:20 +0900111 // The tun module is needed by the Android 464xlat clatd process.
112 "tun",
Garrick Evansa34b5862019-11-20 09:34:01 +0900113 }) != 0) {
114 LOG(WARNING) << "One or more optional kernel modules failed to load.";
115 }
116
Garrick Evans6d227b92019-12-03 16:11:29 +0900117 // This is only needed for CTS (b/27932574).
Garrick Evans8e8e3472020-01-23 14:03:50 +0900118 if (runner.chown("655360", "655360", "/sys/class/xt_idletimer") != 0) {
Garrick Evans6d227b92019-12-03 16:11:29 +0900119 LOG(ERROR) << "Failed to change ownership of xt_idletimer.";
120 }
121
Garrick Evansa34b5862019-11-20 09:34:01 +0900122 done = true;
123}
124
Garrick Evans508a4bc2019-11-14 08:45:52 +0900125bool IsArcVm() {
Garrick Evansc7071122020-04-17 12:31:57 +0900126 if (test::guest == GuestMessage::ARC_VM) {
127 LOG(WARNING) << "Overridden for testing";
128 return true;
129 }
130
Garrick Evans508a4bc2019-11-14 08:45:52 +0900131 const base::FilePath path("/run/chrome/is_arcvm");
132 std::string contents;
133 if (!base::ReadFileToString(path, &contents)) {
134 PLOG(ERROR) << "Could not read " << path.value();
135 }
136 return contents == "1";
137}
138
Garrick Evansf29f5a32019-12-06 11:34:25 +0900139GuestMessage::GuestType ArcGuest() {
140 if (test::guest != GuestMessage::UNKNOWN_GUEST)
141 return test::guest;
Garrick Evans508a4bc2019-11-14 08:45:52 +0900142
Garrick Evansb05a7ff2020-02-18 12:59:55 +0900143 return IsArcVm() ? GuestMessage::ARC_VM : GuestMessage::ARC;
Garrick Evans508a4bc2019-11-14 08:45:52 +0900144}
145
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900146ArcService::InterfaceType InterfaceTypeFor(const std::string& ifname) {
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900147 for (const auto& prefix : kEthernetInterfacePrefixes) {
148 if (base::StartsWith(ifname, prefix,
149 base::CompareCase::INSENSITIVE_ASCII)) {
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900150 return ArcService::InterfaceType::ETHERNET;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900151 }
152 }
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900153 for (const auto& prefix : kWifiInterfacePrefixes) {
154 if (base::StartsWith(ifname, prefix,
155 base::CompareCase::INSENSITIVE_ASCII)) {
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900156 return ArcService::InterfaceType::WIFI;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900157 }
158 }
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900159 for (const auto& prefix : kCellInterfacePrefixes) {
160 if (base::StartsWith(ifname, prefix,
161 base::CompareCase::INSENSITIVE_ASCII)) {
162 return ArcService::InterfaceType::CELL;
163 }
164 }
165 return ArcService::InterfaceType::UNKNOWN;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900166}
167
168bool IsMulticastInterface(const std::string& ifname) {
169 if (ifname.empty()) {
170 return false;
171 }
172
173 int fd = socket(AF_INET, SOCK_DGRAM, 0);
174 if (fd < 0) {
175 // If IPv4 fails, try to open a socket using IPv6.
176 fd = socket(AF_INET6, SOCK_DGRAM, 0);
177 if (fd < 0) {
178 LOG(ERROR) << "Unable to create socket";
179 return false;
180 }
181 }
182
183 struct ifreq ifr;
184 memset(&ifr, 0, sizeof(ifr));
185 strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ);
186 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
187 PLOG(ERROR) << "SIOCGIFFLAGS failed for " << ifname;
188 close(fd);
189 return false;
190 }
191
192 close(fd);
193 return (ifr.ifr_flags & IFF_MULTICAST);
194}
195
Garrick Evanse94b6de2020-02-20 09:19:13 +0900196// Returns the configuration for the ARC management interface used for VPN
197// forwarding, ADB-over-TCP and single-networked ARCVM.
198std::unique_ptr<Device::Config> MakeArcConfig(AddressManager* addr_mgr,
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900199 AddressManager::Guest guest) {
200 auto ipv4_subnet = addr_mgr->AllocateIPv4Subnet(guest);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900201 if (!ipv4_subnet) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900202 LOG(ERROR) << "Subnet already in use or unavailable";
Garrick Evanse94b6de2020-02-20 09:19:13 +0900203 return nullptr;
204 }
205 auto host_ipv4_addr = ipv4_subnet->AllocateAtOffset(0);
206 if (!host_ipv4_addr) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900207 LOG(ERROR) << "Bridge address already in use or unavailable";
Garrick Evanse94b6de2020-02-20 09:19:13 +0900208 return nullptr;
209 }
210 auto guest_ipv4_addr = ipv4_subnet->AllocateAtOffset(1);
211 if (!guest_ipv4_addr) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900212 LOG(ERROR) << "ARC address already in use or unavailable";
Garrick Evanse94b6de2020-02-20 09:19:13 +0900213 return nullptr;
214 }
215
216 return std::make_unique<Device::Config>(
Garrick Evansc7071122020-04-17 12:31:57 +0900217 addr_mgr->GenerateMacAddress(IsArcVm() ? 1 : kAnySubnetIndex),
218 std::move(ipv4_subnet), std::move(host_ipv4_addr),
219 std::move(guest_ipv4_addr));
Garrick Evanse94b6de2020-02-20 09:19:13 +0900220}
221
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900222} // namespace
223
Garrick Evans69b85872020-02-04 11:40:26 +0900224ArcService::ArcService(ShillClient* shill_client,
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900225 Datapath* datapath,
226 AddressManager* addr_mgr,
Garrick Evansf5862122020-03-16 09:13:45 +0900227 TrafficForwarder* forwarder,
228 bool enable_arcvm_multinet)
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900229 : shill_client_(shill_client),
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900230 datapath_(datapath),
231 addr_mgr_(addr_mgr),
Garrick Evansf5862122020-03-16 09:13:45 +0900232 forwarder_(forwarder),
233 enable_arcvm_multinet_(enable_arcvm_multinet) {
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900234 AllocateAddressConfigs();
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900235 shill_client_->RegisterDevicesChangedHandler(
236 base::Bind(&ArcService::OnDevicesChanged, weak_factory_.GetWeakPtr()));
Jie Jiang84c76a12020-04-17 16:45:20 +0900237 shill_client_->ScanDevices();
Garrick Evansbbdf4b42020-03-05 12:59:06 +0900238 shill_client_->RegisterDefaultInterfaceChangedHandler(base::Bind(
239 &ArcService::OnDefaultInterfaceChanged, weak_factory_.GetWeakPtr()));
Garrick Evansf29f5a32019-12-06 11:34:25 +0900240}
241
242ArcService::~ArcService() {
Garrick Evans664a82f2019-12-17 12:18:05 +0900243 if (impl_) {
Garrick Evans664a82f2019-12-17 12:18:05 +0900244 Stop(impl_->id());
Garrick Evans664a82f2019-12-17 12:18:05 +0900245 }
Garrick Evans54861622019-07-19 09:05:09 +0900246}
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900247
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900248void ArcService::AllocateAddressConfigs() {
249 configs_.clear();
250 // The first usable subnet is the "other" ARC device subnet.
251 // TODO(garrick): This can be removed and ARC_NET will be widened once ARCVM
252 // switches over to use .0/30.
Garrick Evansc7071122020-04-17 12:31:57 +0900253 const bool is_arcvm = IsArcVm();
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900254 AddressManager::Guest alloc =
Garrick Evansc7071122020-04-17 12:31:57 +0900255 is_arcvm ? AddressManager::Guest::ARC : AddressManager::Guest::VM_ARC;
256 // As a temporary workaround, for ARCVM, allocate fixed MAC addresses.
257 uint8_t mac_addr_index = 2;
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900258 // Allocate 2 subnets each for Ethernet and WiFi and 1 for LTE WAN interfaces.
259 for (const auto itype :
260 {InterfaceType::ETHERNET, InterfaceType::ETHERNET, InterfaceType::WIFI,
261 InterfaceType::WIFI, InterfaceType::CELL}) {
262 auto ipv4_subnet = addr_mgr_->AllocateIPv4Subnet(alloc);
263 if (!ipv4_subnet) {
264 LOG(ERROR) << "Subnet already in use or unavailable";
265 continue;
266 }
267 // For here out, use the same slices.
268 alloc = AddressManager::Guest::ARC_NET;
269 auto host_ipv4_addr = ipv4_subnet->AllocateAtOffset(0);
270 if (!host_ipv4_addr) {
271 LOG(ERROR) << "Bridge address already in use or unavailable";
272 continue;
273 }
274 auto guest_ipv4_addr = ipv4_subnet->AllocateAtOffset(1);
275 if (!guest_ipv4_addr) {
276 LOG(ERROR) << "ARC address already in use or unavailable";
277 continue;
278 }
279
Garrick Evansc7071122020-04-17 12:31:57 +0900280 MacAddress mac_addr = is_arcvm
281 ? addr_mgr_->GenerateMacAddress(mac_addr_index++)
282 : addr_mgr_->GenerateMacAddress();
283
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900284 configs_[itype].emplace_back(std::make_unique<Device::Config>(
Garrick Evansc7071122020-04-17 12:31:57 +0900285 mac_addr, std::move(ipv4_subnet), std::move(host_ipv4_addr),
286 std::move(guest_ipv4_addr)));
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900287 }
288}
289
Garrick Evans2961c7c2020-04-03 11:34:40 +0900290std::vector<Device::Config*> ArcService::ReallocateAddressConfigs() {
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900291 std::vector<std::string> existing_devices;
292 for (const auto& d : devices_) {
293 existing_devices.emplace_back(d.first);
294 }
295 for (const auto& d : existing_devices) {
296 RemoveDevice(d);
297 }
298 AllocateAddressConfigs();
Garrick Evans2961c7c2020-04-03 11:34:40 +0900299 std::vector<Device::Config*> configs;
300 if (enable_arcvm_multinet_) {
301 for (const auto& kv : configs_)
302 for (const auto& c : kv.second)
303 configs.push_back(c.get());
304 }
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900305 for (const auto& d : existing_devices) {
306 AddDevice(d);
307 }
Garrick Evans2961c7c2020-04-03 11:34:40 +0900308 return configs;
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900309}
310
311std::unique_ptr<Device::Config> ArcService::AcquireConfig(
312 const std::string& ifname) {
313 auto itype = InterfaceTypeFor(ifname);
314 if (itype == InterfaceType::UNKNOWN) {
315 LOG(ERROR) << "Unsupported interface: " << ifname;
316 return nullptr;
317 }
318
319 auto& configs = configs_[itype];
320 if (configs.empty()) {
321 LOG(ERROR) << "No more addresses available. Cannot make device for "
322 << ifname;
323 return nullptr;
324 }
325 std::unique_ptr<Device::Config> config;
326 config = std::move(configs.front());
327 configs.pop_front();
328 return config;
329}
330
331void ArcService::ReleaseConfig(const std::string& ifname,
332 std::unique_ptr<Device::Config> config) {
333 auto itype = InterfaceTypeFor(ifname);
334 if (itype == InterfaceType::UNKNOWN) {
335 LOG(ERROR) << "Unsupported interface: " << ifname;
336 return;
337 }
338
339 configs_[itype].push_front(std::move(config));
340}
341
Garrick Evans015b0d62020-02-07 09:06:38 +0900342bool ArcService::Start(uint32_t id) {
Garrick Evansf29f5a32019-12-06 11:34:25 +0900343 if (impl_) {
Garrick Evans015b0d62020-02-07 09:06:38 +0900344 uint32_t prev_id;
Garrick Evansf29f5a32019-12-06 11:34:25 +0900345 if (impl_->IsStarted(&prev_id)) {
346 LOG(WARNING) << "Already running - did something crash?"
347 << " Stopping and restarting...";
348 Stop(prev_id);
349 }
Garrick Evansa51d0a12019-11-28 13:51:23 +0900350 }
351
Garrick Evans2961c7c2020-04-03 11:34:40 +0900352 auto configs = ReallocateAddressConfigs();
Garrick Evansf29f5a32019-12-06 11:34:25 +0900353 const auto guest = ArcGuest();
Garrick Evans2961c7c2020-04-03 11:34:40 +0900354 if (guest == GuestMessage::ARC_VM) {
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900355 impl_ = std::make_unique<VmImpl>(shill_client_, datapath_, addr_mgr_,
Garrick Evans2961c7c2020-04-03 11:34:40 +0900356 forwarder_, configs);
357 } else {
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900358 impl_ = std::make_unique<ContainerImpl>(datapath_, addr_mgr_, forwarder_,
359 guest);
Garrick Evans2961c7c2020-04-03 11:34:40 +0900360 }
Garrick Evansf29f5a32019-12-06 11:34:25 +0900361 if (!impl_->Start(id)) {
362 impl_.reset();
Garrick Evans508a4bc2019-11-14 08:45:52 +0900363 return false;
Garrick Evansf29f5a32019-12-06 11:34:25 +0900364 }
Garrick Evanscb791e72019-11-11 15:44:34 +0900365
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900366 // Start already known Shill <-> ARC mapped devices.
367 for (const auto& d : devices_)
368 StartDevice(d.second.get());
Garrick Evanscb791e72019-11-11 15:44:34 +0900369
Garrick Evansf29f5a32019-12-06 11:34:25 +0900370 return true;
Garrick Evanscb791e72019-11-11 15:44:34 +0900371}
372
Garrick Evans015b0d62020-02-07 09:06:38 +0900373void ArcService::Stop(uint32_t id) {
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900374 // Stop Shill <-> ARC mapped devices.
375 for (const auto& d : devices_)
376 StopDevice(d.second.get());
Garrick Evansf29f5a32019-12-06 11:34:25 +0900377
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900378 if (impl_) {
379 impl_->Stop(id);
380 impl_.reset();
381 }
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900382}
383
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900384void ArcService::OnDevicesChanged(const std::set<std::string>& added,
385 const std::set<std::string>& removed) {
386 for (const std::string& name : removed)
387 RemoveDevice(name);
388
389 for (const std::string& name : added)
390 AddDevice(name);
391}
392
393void ArcService::AddDevice(const std::string& ifname) {
394 if (ifname.empty())
395 return;
396
397 if (devices_.find(ifname) != devices_.end()) {
398 LOG(DFATAL) << "Attemping to add already tracked device: " << ifname;
399 return;
400 }
401
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900402 auto itype = InterfaceTypeFor(ifname);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900403 Device::Options opts{
404 .fwd_multicast = IsMulticastInterface(ifname),
405 // TODO(crbug/726815) Also enable |ipv6_enabled| for cellular networks
406 // once IPv6 is enabled on cellular networks in shill.
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900407 .ipv6_enabled =
408 (itype == InterfaceType::ETHERNET || itype == InterfaceType::WIFI),
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900409 };
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900410
411 auto config = AcquireConfig(ifname);
412 if (!config) {
413 LOG(ERROR) << "Cannot add device for " << ifname;
414 return;
415 }
416
Garrick Evans8a067562020-05-11 12:47:30 +0900417 auto device = std::make_unique<Device>(ifname, ArcBridgeName(ifname), ifname,
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900418 std::move(config), opts);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900419
420 StartDevice(device.get());
421 devices_.emplace(ifname, std::move(device));
422}
423
424void ArcService::StartDevice(Device* device) {
425 if (!impl_ || !impl_->IsStarted())
426 return;
427
428 // For now, only start devices for ARC++.
429 if (impl_->guest() != GuestMessage::ARC)
430 return;
431
Garrick Evans54861622019-07-19 09:05:09 +0900432 const auto& config = device->config();
433
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900434 LOG(INFO) << "Adding device " << device->phys_ifname()
435 << " bridge: " << device->host_ifname()
436 << " guest_iface: " << device->guest_ifname();
Garrick Evans54861622019-07-19 09:05:09 +0900437
438 // Create the bridge.
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900439 if (!datapath_->AddBridge(device->host_ifname(), config.host_ipv4_addr(),
Garrick Evans7a1a9ee2020-01-28 11:03:57 +0900440 30)) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900441 LOG(ERROR) << "Failed to setup arc bridge: " << device->host_ifname();
Garrick Evanse94b6de2020-02-20 09:19:13 +0900442 return;
Garrick Evans54861622019-07-19 09:05:09 +0900443 }
444
Garrick Evanse94b6de2020-02-20 09:19:13 +0900445 // Set up iptables.
446 if (!datapath_->AddInboundIPv4DNAT(
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900447 device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr())))
Garrick Evanse94b6de2020-02-20 09:19:13 +0900448 LOG(ERROR) << "Failed to configure ingress traffic rules for "
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900449 << device->phys_ifname();
Garrick Evans54861622019-07-19 09:05:09 +0900450
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900451 if (!datapath_->AddOutboundIPv4(device->host_ifname()))
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900452 LOG(ERROR) << "Failed to configure egress traffic rules for "
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900453 << device->phys_ifname();
Garrick Evans2c263102019-07-26 16:07:18 +0900454
Garrick Evansd90a3822019-11-12 17:53:08 +0900455 if (!impl_->OnStartDevice(device)) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900456 LOG(ERROR) << "Failed to start device " << device->phys_ifname();
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900457 }
458}
459
460void ArcService::RemoveDevice(const std::string& ifname) {
461 const auto it = devices_.find(ifname);
462 if (it == devices_.end()) {
463 LOG(WARNING) << "Unknown device: " << ifname;
Garrick Evanscb791e72019-11-11 15:44:34 +0900464 return;
465 }
466
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900467 // If the container is down, this call does nothing.
468 StopDevice(it->second.get());
469
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900470 ReleaseConfig(ifname, it->second->release_config());
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900471 devices_.erase(it);
Garrick Evans54861622019-07-19 09:05:09 +0900472}
473
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900474void ArcService::StopDevice(Device* device) {
475 if (!impl_ || !impl_->IsStarted())
476 return;
477
478 // For now, devices are only started for ARC++.
479 if (impl_->guest() != GuestMessage::ARC)
480 return;
481
482 impl_->OnStopDevice(device);
Garrick Evans54861622019-07-19 09:05:09 +0900483
484 const auto& config = device->config();
485
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900486 LOG(INFO) << "Removing device " << device->phys_ifname()
487 << " bridge: " << device->host_ifname()
488 << " guest_iface: " << device->guest_ifname();
Garrick Evans54861622019-07-19 09:05:09 +0900489
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900490 datapath_->RemoveOutboundIPv4(device->host_ifname());
Garrick Evanse94b6de2020-02-20 09:19:13 +0900491 datapath_->RemoveInboundIPv4DNAT(
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900492 device->phys_ifname(), IPv4AddressToString(config.guest_ipv4_addr()));
Garrick Evans54861622019-07-19 09:05:09 +0900493
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900494 datapath_->RemoveBridge(device->host_ifname());
Garrick Evanscb791e72019-11-11 15:44:34 +0900495}
496
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900497void ArcService::OnDefaultInterfaceChanged(const std::string& new_ifname,
498 const std::string& prev_ifname) {
Garrick Evansf29f5a32019-12-06 11:34:25 +0900499 if (impl_)
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900500 impl_->OnDefaultInterfaceChanged(new_ifname, prev_ifname);
Garrick Evans54861622019-07-19 09:05:09 +0900501}
Garrick Evansba575742019-07-17 15:48:08 +0900502
Garrick Evans38b25a42020-04-06 15:17:42 +0900503std::vector<const Device::Config*> ArcService::GetDeviceConfigs() const {
504 if (impl_)
505 return impl_->GetDeviceConfigs();
Garrick Evanse94b6de2020-02-20 09:19:13 +0900506
Garrick Evans38b25a42020-04-06 15:17:42 +0900507 return {};
Garrick Evanse94b6de2020-02-20 09:19:13 +0900508}
509
Garrick Evansd90a3822019-11-12 17:53:08 +0900510// ARC++ specific functions.
511
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900512ArcService::ContainerImpl::ContainerImpl(Datapath* datapath,
513 AddressManager* addr_mgr,
514 TrafficForwarder* forwarder,
Garrick Evansd90a3822019-11-12 17:53:08 +0900515 GuestMessage::GuestType guest)
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900516 : pid_(kInvalidPID),
517 datapath_(datapath),
518 addr_mgr_(addr_mgr),
519 forwarder_(forwarder),
520 guest_(guest) {
Garrick Evans6d227b92019-12-03 16:11:29 +0900521 OneTimeSetup(*datapath_);
Garrick Evansa34b5862019-11-20 09:34:01 +0900522}
Garrick Evansd90a3822019-11-12 17:53:08 +0900523
Garrick Evansb4eb3892019-11-13 12:07:07 +0900524GuestMessage::GuestType ArcService::ContainerImpl::guest() const {
525 return guest_;
526}
527
Garrick Evans015b0d62020-02-07 09:06:38 +0900528uint32_t ArcService::ContainerImpl::id() const {
Garrick Evans664a82f2019-12-17 12:18:05 +0900529 return pid_;
530}
531
Garrick Evans015b0d62020-02-07 09:06:38 +0900532bool ArcService::ContainerImpl::Start(uint32_t pid) {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900533 // This could happen if something crashes and the stop signal is not sent.
534 // It can probably be addressed by stopping and restarting the service.
535 if (pid_ != kInvalidPID)
536 return false;
537
Garrick Evans4dec0c42019-11-29 12:51:57 +0900538 if (pid == kInvalidPID) {
Garrick Evansd90a3822019-11-12 17:53:08 +0900539 LOG(ERROR) << "Cannot start service - invalid container PID";
540 return false;
541 }
Garrick Evans4dec0c42019-11-29 12:51:57 +0900542 pid_ = pid;
Garrick Evansd90a3822019-11-12 17:53:08 +0900543
Garrick Evanse94b6de2020-02-20 09:19:13 +0900544 Device::Options opts{
545 .fwd_multicast = false,
546 .ipv6_enabled = false,
Garrick Evanse94b6de2020-02-20 09:19:13 +0900547 };
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900548 auto config = MakeArcConfig(addr_mgr_, AddressManager::Guest::ARC);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900549
550 // Create the bridge.
551 // Per crbug/1008686 this device cannot be deleted and then re-added.
552 // So instead of removing the bridge when the service stops, bring down the
553 // device instead and re-up it on restart.
554 if (!datapath_->AddBridge(kArcBridge, config->host_ipv4_addr(), 30) &&
555 !datapath_->MaskInterfaceFlags(kArcBridge, IFF_UP)) {
556 LOG(ERROR) << "Failed to bring up arc bridge: " << kArcBridge;
557 return false;
558 }
559
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900560 arc_device_ = std::make_unique<Device>(kArcIfname, kArcBridge, kArcIfname,
561 std::move(config), opts);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900562
563 OnStartDevice(arc_device_.get());
564
Garrick Evansd90a3822019-11-12 17:53:08 +0900565 LOG(INFO) << "ARC++ network service started {pid: " << pid_ << "}";
566 return true;
567}
568
Garrick Evans015b0d62020-02-07 09:06:38 +0900569void ArcService::ContainerImpl::Stop(uint32_t /*pid*/) {
Garrick Evans4dec0c42019-11-29 12:51:57 +0900570 if (!IsStarted())
Taoyu Li1c96d272019-12-13 14:17:43 +0900571 return;
Garrick Evans4dec0c42019-11-29 12:51:57 +0900572
Garrick Evanse94b6de2020-02-20 09:19:13 +0900573 // Per crbug/1008686 this device cannot be deleted and then re-added.
574 // So instead of removing the bridge, bring it down and mark it. This will
575 // allow us to detect if the device is re-added in case of a crash restart
576 // and do the right thing.
577 if (arc_device_) {
578 OnStopDevice(arc_device_.get());
579 if (!datapath_->MaskInterfaceFlags(kArcBridge, IFF_DEBUG, IFF_UP))
580 LOG(ERROR) << "Failed to bring down arc bridge "
581 << "- it may not restart correctly";
582 }
Garrick Evansd90a3822019-11-12 17:53:08 +0900583
584 LOG(INFO) << "ARC++ network service stopped {pid: " << pid_ << "}";
585 pid_ = kInvalidPID;
586}
587
Garrick Evans015b0d62020-02-07 09:06:38 +0900588bool ArcService::ContainerImpl::IsStarted(uint32_t* pid) const {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900589 if (pid)
590 *pid = pid_;
591
Garrick Evansd90a3822019-11-12 17:53:08 +0900592 return pid_ != kInvalidPID;
593}
594
595bool ArcService::ContainerImpl::OnStartDevice(Device* device) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900596 LOG(INFO) << "Starting device " << device->phys_ifname()
597 << " bridge: " << device->host_ifname()
598 << " guest_iface: " << device->guest_ifname() << " pid: " << pid_;
Garrick Evansd90a3822019-11-12 17:53:08 +0900599
Garrick Evans2470caa2020-03-04 14:15:41 +0900600 // Set up the virtual pair inside the container namespace.
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900601 const std::string veth_ifname = ArcVethHostName(device->guest_ifname());
Hugo Benichi76675592020-04-08 14:29:57 +0900602 const auto& config = device->config();
603 if (!datapath_->ConnectVethPair(pid_, veth_ifname, device->guest_ifname(),
604 config.mac_addr(), config.guest_ipv4_addr(),
605 30, device->options().fwd_multicast)) {
606 LOG(ERROR) << "Cannot create virtual link for device "
607 << device->phys_ifname();
Garrick Evansd90a3822019-11-12 17:53:08 +0900608 return false;
609 }
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900610 if (!datapath_->AddToBridge(device->host_ifname(), veth_ifname)) {
Garrick Evans2470caa2020-03-04 14:15:41 +0900611 datapath_->RemoveInterface(veth_ifname);
612 LOG(ERROR) << "Failed to bridge interface " << veth_ifname;
613 return false;
614 }
Garrick Evansd90a3822019-11-12 17:53:08 +0900615
Garrick Evans3bd06372020-03-23 10:42:58 +0900616 if (device != arc_device_.get()) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900617 forwarder_->StartForwarding(device->phys_ifname(), device->host_ifname(),
618 device->options().ipv6_enabled,
619 device->options().fwd_multicast);
Garrick Evans3bd06372020-03-23 10:42:58 +0900620 } else {
621 // Signal the container that the network device is ready.
622 datapath_->runner().WriteSentinelToContainer(pid_);
623 }
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900624
Garrick Evansd90a3822019-11-12 17:53:08 +0900625 return true;
626}
627
628void ArcService::ContainerImpl::OnStopDevice(Device* device) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900629 LOG(INFO) << "Stopping device " << device->phys_ifname()
630 << " bridge: " << device->host_ifname()
631 << " guest_iface: " << device->guest_ifname() << " pid: " << pid_;
Garrick Evansd90a3822019-11-12 17:53:08 +0900632
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900633 if (device != arc_device_.get())
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900634 forwarder_->StopForwarding(device->phys_ifname(), device->host_ifname(),
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900635 device->options().ipv6_enabled,
636 device->options().fwd_multicast);
637
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900638 datapath_->RemoveInterface(ArcVethHostName(device->phys_ifname()));
Garrick Evansd90a3822019-11-12 17:53:08 +0900639}
640
641void ArcService::ContainerImpl::OnDefaultInterfaceChanged(
Garrick Evansb05a7ff2020-02-18 12:59:55 +0900642 const std::string& new_ifname, const std::string& prev_ifname) {}
Garrick Evansd90a3822019-11-12 17:53:08 +0900643
Garrick Evansb4eb3892019-11-13 12:07:07 +0900644// VM specific functions
645
Garrick Evansbbdf4b42020-03-05 12:59:06 +0900646ArcService::VmImpl::VmImpl(ShillClient* shill_client,
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900647 Datapath* datapath,
648 AddressManager* addr_mgr,
Garrick Evansf5862122020-03-16 09:13:45 +0900649 TrafficForwarder* forwarder,
Garrick Evans2961c7c2020-04-03 11:34:40 +0900650 const std::vector<Device::Config*>& configs)
Garrick Evansbbdf4b42020-03-05 12:59:06 +0900651 : cid_(kInvalidCID),
652 shill_client_(shill_client),
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900653 datapath_(datapath),
654 addr_mgr_(addr_mgr),
Garrick Evansf5862122020-03-16 09:13:45 +0900655 forwarder_(forwarder),
Garrick Evans2961c7c2020-04-03 11:34:40 +0900656 configs_(configs) {}
Garrick Evansb4eb3892019-11-13 12:07:07 +0900657
658GuestMessage::GuestType ArcService::VmImpl::guest() const {
659 return GuestMessage::ARC_VM;
660}
661
Garrick Evans015b0d62020-02-07 09:06:38 +0900662uint32_t ArcService::VmImpl::id() const {
Garrick Evans664a82f2019-12-17 12:18:05 +0900663 return cid_;
664}
665
Garrick Evans38b25a42020-04-06 15:17:42 +0900666std::vector<const Device::Config*> ArcService::VmImpl::GetDeviceConfigs()
667 const {
668 std::vector<const Device::Config*> configs;
669 for (const auto* c : configs_)
670 configs.emplace_back(c);
671
672 return configs;
673}
674
Garrick Evans015b0d62020-02-07 09:06:38 +0900675bool ArcService::VmImpl::Start(uint32_t cid) {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900676 // This can happen if concierge crashes and doesn't send the vm down RPC.
677 // It can probably be addressed by stopping and restarting the service.
678 if (cid_ != kInvalidCID)
679 return false;
680
Garrick Evans015b0d62020-02-07 09:06:38 +0900681 if (cid == kInvalidCID) {
Garrick Evansb4eb3892019-11-13 12:07:07 +0900682 LOG(ERROR) << "Invalid VM cid " << cid;
683 return false;
684 }
Garrick Evansb4eb3892019-11-13 12:07:07 +0900685 cid_ = cid;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900686
Garrick Evanse94b6de2020-02-20 09:19:13 +0900687 Device::Options opts{
688 .fwd_multicast = true,
689 .ipv6_enabled = true,
Garrick Evanse94b6de2020-02-20 09:19:13 +0900690 };
Garrick Evans2961c7c2020-04-03 11:34:40 +0900691 auto arc_config = MakeArcConfig(addr_mgr_, AddressManager::Guest::VM_ARC);
692 configs_.insert(configs_.begin(), arc_config.get());
Garrick Evanse94b6de2020-02-20 09:19:13 +0900693
Garrick Evans2961c7c2020-04-03 11:34:40 +0900694 // Allocate TAP devices for all configs.
695 for (auto* config : configs_) {
Garrick Evansc7071122020-04-17 12:31:57 +0900696 auto mac = config->mac_addr();
697 auto tap =
698 datapath_->AddTAP("" /* auto-generate name */, &mac,
699 nullptr /* no ipv4 subnet */, vm_tools::kCrosVmUser);
Garrick Evans2961c7c2020-04-03 11:34:40 +0900700 if (tap.empty()) {
701 LOG(ERROR) << "Failed to create TAP device";
702 continue;
703 }
704
705 config->set_tap_ifname(tap);
Garrick Evanse94b6de2020-02-20 09:19:13 +0900706 }
707
Garrick Evans2961c7c2020-04-03 11:34:40 +0900708 arc_device_ = std::make_unique<Device>(
709 kArcVmIfname, kArcVmBridge, kArcVmIfname, std::move(arc_config), opts);
710 // Create the bridge.
711 if (!datapath_->AddBridge(kArcVmBridge,
712 arc_device_->config().host_ipv4_addr(), 30)) {
713 LOG(ERROR) << "Failed to setup arc bridge for device " << *arc_device_;
714 return false;
715 }
Garrick Evanse94b6de2020-02-20 09:19:13 +0900716 OnStartDevice(arc_device_.get());
717
718 LOG(INFO) << "ARCVM network service started {cid: " << cid_ << "}";
Garrick Evansb4eb3892019-11-13 12:07:07 +0900719 return true;
720}
721
Garrick Evans015b0d62020-02-07 09:06:38 +0900722void ArcService::VmImpl::Stop(uint32_t cid) {
Garrick Evans21173b12019-11-20 15:23:16 +0900723 if (cid_ != cid) {
724 LOG(ERROR) << "Mismatched ARCVM CIDs " << cid_ << " != " << cid;
725 return;
726 }
727
Garrick Evans2961c7c2020-04-03 11:34:40 +0900728 for (auto* config : configs_) {
729 const auto& tap = config->tap_ifname();
730 if (!tap.empty()) {
731 datapath_->RemoveInterface(tap);
732 config->set_tap_ifname("");
733 }
734 }
735
Garrick Evanse94b6de2020-02-20 09:19:13 +0900736 OnStopDevice(arc_device_.get());
737 datapath_->RemoveBridge(kArcVmBridge);
738 arc_device_.reset();
739
Garrick Evansb4eb3892019-11-13 12:07:07 +0900740 LOG(INFO) << "ARCVM network service stopped {cid: " << cid_ << "}";
741 cid_ = kInvalidCID;
742}
743
Garrick Evans015b0d62020-02-07 09:06:38 +0900744bool ArcService::VmImpl::IsStarted(uint32_t* cid) const {
Garrick Evansa51d0a12019-11-28 13:51:23 +0900745 if (cid)
746 *cid = cid_;
747
Garrick Evans015b0d62020-02-07 09:06:38 +0900748 return cid_ != kInvalidCID;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900749}
750
751bool ArcService::VmImpl::OnStartDevice(Device* device) {
Garrick Evansf5862122020-03-16 09:13:45 +0900752 // TODO(garrick): Remove once ARCVM P is gone.
Garrick Evans2961c7c2020-04-03 11:34:40 +0900753 if (device == arc_device_.get() && !IsMultinetEnabled())
Garrick Evansf5862122020-03-16 09:13:45 +0900754 return OnStartArcPDevice();
Garrick Evansb4eb3892019-11-13 12:07:07 +0900755
Garrick Evans2961c7c2020-04-03 11:34:40 +0900756 std::string tap;
757 for (auto* config : configs_) {
758 if (config == &device->config()) {
759 tap = config->tap_ifname();
760 break;
761 }
762 }
Garrick Evansb4eb3892019-11-13 12:07:07 +0900763 if (tap.empty()) {
Garrick Evans2961c7c2020-04-03 11:34:40 +0900764 LOG(ERROR) << "No TAP device for: " << *device;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900765 return false;
766 }
767
Garrick Evans2961c7c2020-04-03 11:34:40 +0900768 LOG(INFO) << "Starting device " << *device << " cid: " << cid_;
769
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900770 if (!datapath_->AddToBridge(device->host_ifname(), tap)) {
Garrick Evansb4eb3892019-11-13 12:07:07 +0900771 LOG(ERROR) << "Failed to bridge TAP device " << tap;
772 datapath_->RemoveInterface(tap);
773 return false;
774 }
775
Garrick Evansf5862122020-03-16 09:13:45 +0900776 if (device != arc_device_.get())
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900777 forwarder_->StartForwarding(device->phys_ifname(), device->host_ifname(),
778 device->options().ipv6_enabled,
779 device->options().fwd_multicast);
Garrick Evansf5862122020-03-16 09:13:45 +0900780
781 return true;
782}
783
784bool ArcService::VmImpl::OnStartArcPDevice() {
Garrick Evans2961c7c2020-04-03 11:34:40 +0900785 LOG(INFO) << "Starting device " << *arc_device_ << " cid: " << cid_;
Garrick Evansf5862122020-03-16 09:13:45 +0900786
Garrick Evans2961c7c2020-04-03 11:34:40 +0900787 if (!datapath_->AddToBridge(kArcVmBridge,
788 arc_device_->config().tap_ifname())) {
789 LOG(ERROR) << "Failed to bridge TAP device " << *arc_device_;
Garrick Evansf5862122020-03-16 09:13:45 +0900790 return false;
791 }
792
Garrick Evansf5862122020-03-16 09:13:45 +0900793 // Setup the iptables.
794 if (!datapath_->AddLegacyIPv4DNAT(
795 IPv4AddressToString(arc_device_->config().guest_ipv4_addr())))
796 LOG(ERROR) << "Failed to configure ARC traffic rules";
797
798 if (!datapath_->AddOutboundIPv4(kArcVmBridge))
799 LOG(ERROR) << "Failed to configure egress traffic rules";
800
Garrick Evansbbdf4b42020-03-05 12:59:06 +0900801 OnDefaultInterfaceChanged(shill_client_->default_interface(),
802 "" /*previous*/);
Garrick Evansbcce09e2020-03-10 15:08:04 +0900803
Garrick Evansb4eb3892019-11-13 12:07:07 +0900804 return true;
805}
806
807void ArcService::VmImpl::OnStopDevice(Device* device) {
Garrick Evansf5862122020-03-16 09:13:45 +0900808 // TODO(garrick): Remove once ARCVM P is gone.
Garrick Evans2961c7c2020-04-03 11:34:40 +0900809 if (device == arc_device_.get() && !IsMultinetEnabled())
Garrick Evansf5862122020-03-16 09:13:45 +0900810 return OnStopArcPDevice();
Garrick Evansb4eb3892019-11-13 12:07:07 +0900811
Garrick Evans2961c7c2020-04-03 11:34:40 +0900812 LOG(INFO) << "Stopping device " << *device << " cid: " << cid_;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900813
Garrick Evansf5862122020-03-16 09:13:45 +0900814 if (device != arc_device_.get())
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900815 forwarder_->StopForwarding(device->phys_ifname(), device->host_ifname(),
Garrick Evansf5862122020-03-16 09:13:45 +0900816 device->options().ipv6_enabled,
817 device->options().fwd_multicast);
Garrick Evansbcce09e2020-03-10 15:08:04 +0900818
Garrick Evans2961c7c2020-04-03 11:34:40 +0900819 for (auto* config : configs_) {
820 if (config == &device->config()) {
821 config->set_tap_ifname("");
822 break;
823 }
824 }
Garrick Evansb4eb3892019-11-13 12:07:07 +0900825}
826
Garrick Evansf5862122020-03-16 09:13:45 +0900827void ArcService::VmImpl::OnStopArcPDevice() {
Garrick Evans2961c7c2020-04-03 11:34:40 +0900828 LOG(INFO) << "Stopping device " << *arc_device_.get() << " cid: " << cid_;
Garrick Evansf5862122020-03-16 09:13:45 +0900829
830 datapath_->RemoveOutboundIPv4(kArcVmBridge);
831 datapath_->RemoveLegacyIPv4DNAT();
832
833 OnDefaultInterfaceChanged("" /*new_ifname*/,
834 shill_client_->default_interface());
835
Garrick Evans2961c7c2020-04-03 11:34:40 +0900836 arc_device_->config().set_tap_ifname("");
Garrick Evansf5862122020-03-16 09:13:45 +0900837}
838
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900839void ArcService::VmImpl::OnDefaultInterfaceChanged(
840 const std::string& new_ifname, const std::string& prev_ifname) {
Garrick Evans2961c7c2020-04-03 11:34:40 +0900841 if (!IsStarted() || IsMultinetEnabled())
Garrick Evansb4eb3892019-11-13 12:07:07 +0900842 return;
843
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900844 forwarder_->StopForwarding(prev_ifname, kArcVmBridge, true /*ipv6*/,
845 true /*multicast*/);
846
Garrick Evansb4eb3892019-11-13 12:07:07 +0900847 datapath_->RemoveLegacyIPv4InboundDNAT();
Garrick Evansb4eb3892019-11-13 12:07:07 +0900848
849 // If a new default interface was given, then re-enable with that.
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900850 if (!new_ifname.empty()) {
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900851 datapath_->AddLegacyIPv4InboundDNAT(new_ifname);
Jason Jeremy Iman0e9f8262020-03-06 14:50:49 +0900852 forwarder_->StartForwarding(new_ifname, kArcVmBridge, true /*ipv6*/,
853 true /*multicast*/);
Garrick Evansb4eb3892019-11-13 12:07:07 +0900854 }
855}
856
Garrick Evans2961c7c2020-04-03 11:34:40 +0900857bool ArcService::VmImpl::IsMultinetEnabled() const {
858 return configs_.size() > 1;
859}
860
Garrick Evans3388a032020-03-24 11:25:55 +0900861} // namespace patchpanel