blob: 2da21b826ec8352c3412c418c4a38990a278c288 [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>
Hugo Benichi69c989d2021-03-01 00:23:39 +090010#include <vector>
Garrick Evans47c19272019-11-21 10:58:21 +090011#include <string>
12
13#include <base/memory/weak_ptr.h>
14
Garrick Evans3388a032020-03-24 11:25:55 +090015#include "patchpanel/address_manager.h"
16#include "patchpanel/datapath.h"
17#include "patchpanel/device.h"
Garrick Evans47c19272019-11-21 10:58:21 +090018
Garrick Evans3388a032020-03-24 11:25:55 +090019namespace patchpanel {
Garrick Evans47c19272019-11-21 10:58:21 +090020
Garrick Evansb1c93712020-01-22 09:28:25 +090021// Crostini networking service handling address allocation and TAP device
Garrick Evans51d5b552020-01-30 10:42:06 +090022// management for Crostini VMs.
Garrick Evansf29f5a32019-12-06 11:34:25 +090023class CrostiniService {
Garrick Evans47c19272019-11-21 10:58:21 +090024 public:
Garrick Evans69b85872020-02-04 11:40:26 +090025 // All pointers are required and must not be null, and are owned by the
26 // caller.
Hugo Benichi69c989d2021-03-01 00:23:39 +090027 CrostiniService(AddressManager* addr_mgr,
Garrick Evans1b1f67c2020-02-04 16:21:25 +090028 Datapath* datapath,
Garrick Evans209a80a2020-11-30 14:42:40 +090029 Device::ChangeEventHandler device_changed_handler);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090030 CrostiniService(const CrostiniService&) = delete;
31 CrostiniService& operator=(const CrostiniService&) = delete;
32
Garrick Evansc1ac5c42020-03-31 15:31:22 +090033 ~CrostiniService();
Garrick Evans47c19272019-11-21 10:58:21 +090034
Garrick Evans51d5b552020-01-30 10:42:06 +090035 bool Start(uint64_t vm_id, bool is_termina, int subnet_index);
36 void Stop(uint64_t vm_id, bool is_termina);
Garrick Evans47c19272019-11-21 10:58:21 +090037
Garrick Evans51d5b552020-01-30 10:42:06 +090038 const Device* const TAP(uint64_t vm_id, bool is_termina) const;
Garrick Evans47c19272019-11-21 10:58:21 +090039
Garrick Evans02e6e872020-11-30 11:53:13 +090040 // Walks the current list of devices managed by the service invoking the
41 // callback for each, allowing for safe inspection/evaluation.
42 // The first two callback args correspond to the vm_id and is_termina values
43 // originally provided to the TAP() function that created the device.
44 void ScanDevices(base::RepeatingCallback<void(uint64_t, bool, const Device&)>
45 callback) const;
46
Hugo Benichi69c989d2021-03-01 00:23:39 +090047 // Returns a list of all tap Devices currently managed by this service.
48 std::vector<const Device*> GetDevices() 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);
Garrick Evansb1c93712020-01-22 09:28:25 +090052
Jason Jeremy Imanfa8b6d22020-02-20 03:44:21 +000053 // Checks ADB sideloading status and set it to |adb_sideloading_enabled_|.
54 // This function will call itself again if ADB sideloading status is not
55 // known yet. Otherwise, it will process all currently running Crostini VMs.
56 void CheckAdbSideloadingStatus();
57
58 // Start and stop ADB traffic forwarding from Crostini's TAP device
Garrick Evans3388a032020-03-24 11:25:55 +090059 // patchpanel's adb-proxy. |ifname| is the Crostini's TAP interface that
Jason Jeremy Imanfa8b6d22020-02-20 03:44:21 +000060 // will be forwarded. These methods call permission broker DBUS APIs to port
61 // forward and accept traffic.
62 void StartAdbPortForwarding(const std::string& ifname);
63 void StopAdbPortForwarding(const std::string& ifname);
64
Garrick Evans1b1f67c2020-02-04 16:21:25 +090065 AddressManager* addr_mgr_;
Garrick Evans47c19272019-11-21 10:58:21 +090066 Datapath* datapath_;
Garrick Evans209a80a2020-11-30 14:42:40 +090067 Device::ChangeEventHandler device_changed_handler_;
Garrick Evans1b1f67c2020-02-04 16:21:25 +090068
Garrick Evans51d5b552020-01-30 10:42:06 +090069 // Mapping of VM IDs to TAP devices
70 std::map<std::string, std::unique_ptr<Device>> taps_;
Garrick Evans47c19272019-11-21 10:58:21 +090071
Jason Jeremy Imanfa8b6d22020-02-20 03:44:21 +000072 bool adb_sideloading_enabled_;
73 scoped_refptr<dbus::Bus> bus_;
Jason Jeremy Imanfa8b6d22020-02-20 03:44:21 +000074
Garrick Evans47c19272019-11-21 10:58:21 +090075 base::WeakPtrFactory<CrostiniService> weak_factory_{this};
Garrick Evans47c19272019-11-21 10:58:21 +090076};
77
Garrick Evans3388a032020-03-24 11:25:55 +090078} // namespace patchpanel
Garrick Evans47c19272019-11-21 10:58:21 +090079
Garrick Evans3388a032020-03-24 11:25:55 +090080#endif // PATCHPANEL_CROSTINI_SERVICE_H_