blob: 1330603e9fffc2580f868d85546d9761189bb9fc [file] [log] [blame]
Garrick Evans47c19272019-11-21 10:58:21 +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#ifndef PATCHPANEL_CROSTINI_SERVICE_H_
6#define PATCHPANEL_CROSTINI_SERVICE_H_
Garrick Evans47c19272019-11-21 10:58:21 +09007
Garrick Evansb1c93712020-01-22 09:28:25 +09008#include <map>
9#include <memory>
Garrick Evans47c19272019-11-21 10:58:21 +090010#include <string>
11
12#include <base/memory/weak_ptr.h>
13
Garrick Evans3388a032020-03-24 11:25:55 +090014#include "patchpanel/address_manager.h"
15#include "patchpanel/datapath.h"
16#include "patchpanel/device.h"
17#include "patchpanel/shill_client.h"
18#include "patchpanel/traffic_forwarder.h"
Garrick Evans47c19272019-11-21 10:58:21 +090019
Garrick Evans3388a032020-03-24 11:25:55 +090020namespace patchpanel {
Garrick Evans47c19272019-11-21 10:58:21 +090021
Garrick Evansb1c93712020-01-22 09:28:25 +090022// Crostini networking service handling address allocation and TAP device
Garrick Evans51d5b552020-01-30 10:42:06 +090023// management for Crostini VMs.
Garrick Evansf29f5a32019-12-06 11:34:25 +090024class CrostiniService {
Garrick Evans47c19272019-11-21 10:58:21 +090025 public:
Garrick Evans69b85872020-02-04 11:40:26 +090026 // All pointers are required and must not be null, and are owned by the
27 // caller.
28 CrostiniService(ShillClient* shill_client,
Garrick Evans1b1f67c2020-02-04 16:21:25 +090029 AddressManager* addr_mgr,
30 Datapath* datapath,
Garrick Evans209a80a2020-11-30 14:42:40 +090031 TrafficForwarder* forwarder,
32 Device::ChangeEventHandler device_changed_handler);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090033 CrostiniService(const CrostiniService&) = delete;
34 CrostiniService& operator=(const CrostiniService&) = delete;
35
Garrick Evansc1ac5c42020-03-31 15:31:22 +090036 ~CrostiniService();
Garrick Evans47c19272019-11-21 10:58:21 +090037
Garrick Evans51d5b552020-01-30 10:42:06 +090038 bool Start(uint64_t vm_id, bool is_termina, int subnet_index);
39 void Stop(uint64_t vm_id, bool is_termina);
Garrick Evans47c19272019-11-21 10:58:21 +090040
Garrick Evans51d5b552020-01-30 10:42:06 +090041 const Device* const TAP(uint64_t vm_id, bool is_termina) const;
Garrick Evans47c19272019-11-21 10:58:21 +090042
Garrick Evans02e6e872020-11-30 11:53:13 +090043 // Walks the current list of devices managed by the service invoking the
44 // callback for each, allowing for safe inspection/evaluation.
45 // The first two callback args correspond to the vm_id and is_termina values
46 // originally provided to the TAP() function that created the device.
47 void ScanDevices(base::RepeatingCallback<void(uint64_t, bool, const Device&)>
48 callback) const;
49
Garrick Evans47c19272019-11-21 10:58:21 +090050 private:
Garrick Evans51d5b552020-01-30 10:42:06 +090051 std::unique_ptr<Device> AddTAP(bool is_termina, int subnet_index);
Hugo Benichi78148a02020-10-30 18:37:00 +090052 void OnDefaultDeviceChanged(const ShillClient::Device& new_device,
53 const ShillClient::Device& prev_device);
Garrick Evans1b1f67c2020-02-04 16:21:25 +090054 void StartForwarding(const std::string& phys_ifname,
Jason Jeremy Iman0e9f8262020-03-06 14:50:49 +090055 const std::string& virt_ifname);
Garrick Evans1b1f67c2020-02-04 16:21:25 +090056 void StopForwarding(const std::string& phys_ifname,
57 const std::string& virt_ifname);
Garrick Evansb1c93712020-01-22 09:28:25 +090058
Jason Jeremy Imanfa8b6d22020-02-20 03:44:21 +000059 // Checks ADB sideloading status and set it to |adb_sideloading_enabled_|.
60 // This function will call itself again if ADB sideloading status is not
61 // known yet. Otherwise, it will process all currently running Crostini VMs.
62 void CheckAdbSideloadingStatus();
63
64 // Start and stop ADB traffic forwarding from Crostini's TAP device
Garrick Evans3388a032020-03-24 11:25:55 +090065 // patchpanel's adb-proxy. |ifname| is the Crostini's TAP interface that
Jason Jeremy Imanfa8b6d22020-02-20 03:44:21 +000066 // will be forwarded. These methods call permission broker DBUS APIs to port
67 // forward and accept traffic.
68 void StartAdbPortForwarding(const std::string& ifname);
69 void StopAdbPortForwarding(const std::string& ifname);
70
Garrick Evans69b85872020-02-04 11:40:26 +090071 ShillClient* shill_client_;
Garrick Evans1b1f67c2020-02-04 16:21:25 +090072 AddressManager* addr_mgr_;
Garrick Evans47c19272019-11-21 10:58:21 +090073 Datapath* datapath_;
Garrick Evans1b1f67c2020-02-04 16:21:25 +090074 TrafficForwarder* forwarder_;
Garrick Evans209a80a2020-11-30 14:42:40 +090075 Device::ChangeEventHandler device_changed_handler_;
Garrick Evans1b1f67c2020-02-04 16:21:25 +090076
Garrick Evans51d5b552020-01-30 10:42:06 +090077 // Mapping of VM IDs to TAP devices
78 std::map<std::string, std::unique_ptr<Device>> taps_;
Garrick Evans47c19272019-11-21 10:58:21 +090079
Jason Jeremy Imanfa8b6d22020-02-20 03:44:21 +000080 bool adb_sideloading_enabled_;
81 scoped_refptr<dbus::Bus> bus_;
Jason Jeremy Imanfa8b6d22020-02-20 03:44:21 +000082
Garrick Evans47c19272019-11-21 10:58:21 +090083 base::WeakPtrFactory<CrostiniService> weak_factory_{this};
Garrick Evans47c19272019-11-21 10:58:21 +090084};
85
Garrick Evans3388a032020-03-24 11:25:55 +090086} // namespace patchpanel
Garrick Evans47c19272019-11-21 10:58:21 +090087
Garrick Evans3388a032020-03-24 11:25:55 +090088#endif // PATCHPANEL_CROSTINI_SERVICE_H_