patchpanel: Add support for getting all managed devices.
Adds a new dbus method - GetDevices - and the supporting implementation
in the arc and crostini services to acquire the list of devices
currently managed by patchpanel. This currently includes only the
virtual devices it creates for guests and excludes physical and virtual
(like VPN) devices tracked by shill.
BUG=b:174432555
TEST=units
Change-Id: I3e62c89836bf7bfbc0fade5260ea27eb1f60df9f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2566852
Tested-by: Garrick Evans <garrick@chromium.org>
Commit-Queue: Garrick Evans <garrick@chromium.org>
Reviewed-by: Hugo Benichi <hugobenichi@google.com>
diff --git a/patchpanel/manager.cc b/patchpanel/manager.cc
index ea55ce1..4f7a7c0 100644
--- a/patchpanel/manager.cc
+++ b/patchpanel/manager.cc
@@ -196,6 +196,7 @@
{patchpanel::kConnectNamespaceMethod, &Manager::OnConnectNamespace},
{patchpanel::kGetTrafficCountersMethod, &Manager::OnGetTrafficCounters},
{patchpanel::kModifyPortRuleMethod, &Manager::OnModifyPortRule},
+ {patchpanel::kGetDevicesMethod, &Manager::OnGetDevices},
};
for (const auto& kv : kServiceMethods) {
@@ -397,6 +398,60 @@
cros_svc_->Stop(vm_id, vm_type == GuestMessage::TERMINA_VM);
}
+std::unique_ptr<dbus::Response> Manager::OnGetDevices(
+ dbus::MethodCall* method_call) {
+ std::unique_ptr<dbus::Response> dbus_response(
+ dbus::Response::FromMethodCall(method_call));
+
+ dbus::MessageReader reader(method_call);
+ dbus::MessageWriter writer(dbus_response.get());
+
+ patchpanel::GetDevicesRequest request;
+ patchpanel::GetDevicesResponse response;
+
+ if (!reader.PopArrayOfBytesAsProto(&request)) {
+ LOG(ERROR) << "Unable to parse request";
+ writer.AppendProtoAsArrayOfBytes(response);
+ return dbus_response;
+ }
+
+ static const auto arc_guest_type =
+ USE_ARCVM ? NetworkDevice::ARCVM : NetworkDevice::ARC;
+
+ arc_svc_->ScanDevices(base::BindRepeating(
+ [](patchpanel::GetDevicesResponse* resp, const Device& device) {
+ auto* dev = resp->add_devices();
+ dev->set_ifname(device.host_ifname());
+ dev->set_ipv4_addr(device.config().guest_ipv4_addr());
+ dev->set_guest_type(arc_guest_type);
+ if (const auto* subnet = device.config().ipv4_subnet()) {
+ auto* sub = dev->mutable_ipv4_subnet();
+ sub->set_base_addr(subnet->BaseAddress());
+ sub->set_prefix_len(subnet->PrefixLength());
+ }
+ },
+ &response));
+
+ cros_svc_->ScanDevices(base::BindRepeating(
+ [](patchpanel::GetDevicesResponse* resp, uint64_t vm_id, bool is_termina,
+ const Device& device) {
+ auto* dev = resp->add_devices();
+ dev->set_ifname(device.host_ifname());
+ dev->set_ipv4_addr(device.config().guest_ipv4_addr());
+ dev->set_guest_type(is_termina ? NetworkDevice::TERMINA_VM
+ : NetworkDevice::PLUGIN_VM);
+ if (const auto* subnet = device.config().ipv4_subnet()) {
+ auto* sub = dev->mutable_ipv4_subnet();
+ sub->set_base_addr(subnet->BaseAddress());
+ sub->set_prefix_len(subnet->PrefixLength());
+ }
+ },
+ &response));
+
+ writer.AppendProtoAsArrayOfBytes(response);
+ return dbus_response;
+}
+
std::unique_ptr<dbus::Response> Manager::OnArcStartup(
dbus::MethodCall* method_call) {
LOG(INFO) << "ARC++ starting up";