blob: 8bfce40bce0ecad632122cc9b6cd12c49e0990ca [file] [log] [blame]
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef VM_TOOLS_CICERONE_VIRTUAL_MACHINE_H_
#define VM_TOOLS_CICERONE_VIRTUAL_MACHINE_H_
#include <stdint.h>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <base/macros.h>
#include "container_guest.grpc.pb.h" // NOLINT(build/include)
namespace vm_tools {
namespace cicerone {
// Represents a single instance of a virtual machine.
class VirtualMachine {
public:
// Linux application ID and its icon content.
struct Icon {
std::string desktop_file_id;
std::string content;
};
VirtualMachine(uint32_t container_subnet,
uint32_t container_netmask,
uint32_t ipv4_address);
~VirtualMachine();
// The VM's container subnet netmask in network byte order.
uint32_t container_netmask() const { return container_netmask_; }
// The first address in the VM's container subnet in network byte order.
uint32_t container_subnet() const { return container_subnet_; }
// The first address in the VM's container subnet in network byte order.
uint32_t ipv4_address() const { return ipv4_address_; }
// Registers a container with the VM using the |container_ip| address and
// |container_token|. Returns true if the token is valid, false otherwise.
bool RegisterContainer(const std::string& container_token,
const std::string& container_ip);
// Unregister a container with |container_token| within this VM. Returns true
// if the token is valid, false otherwise.
bool UnregisterContainer(const std::string& container_token);
// Generates a random token string that should be passed into the container
// which can then be used by the container to identify itself when it
// communicates back with us.
std::string GenerateContainerToken(const std::string& container_name);
// Returns the name of the container associated with the passed in
// |container_token|. Returns the empty string if no such mapping exists. This
// will only return a name that has been confirmed after calling
// RegisterContainerIp.
std::string GetContainerNameForToken(const std::string& container_token);
// Launches the application associated with |desktop_file_id| in the container
// named |container_name| within this VM. Returns true on success, false
// otherwise and fills out |out_error| on failure.
bool LaunchContainerApplication(const std::string& container_name,
const std::string& desktop_file_id,
std::string* out_error);
// Launches vshd.
bool LaunchVshd(const std::string& container_name,
uint32_t port,
std::string* out_error);
// Returns whether there is a connected stub to Garcon running inside the
// named |container_name| within this VM.
bool IsContainerRunning(const std::string& container_name);
// Gets icons of those applications with their desktop file IDs specified
// by |desktop_file_ids| from the container named |container_name| within
// this VM. The icons should have size of |icon_size| and designed scale of
// |scale|. The icons are returned through the paramenter |icons|.
bool GetContainerAppIcon(const std::string& container_name,
std::vector<std::string> desktop_file_ids,
uint32_t icon_size,
uint32_t scale,
std::vector<Icon>* icons);
// Gets a list of all the active container names in this VM.
std::vector<std::string> GetContainerNames();
private:
uint32_t container_subnet_;
uint32_t container_netmask_;
uint32_t ipv4_address_;
// Mapping of container tokens to names. The tokens are used to securely
// identify a container when it connects back to concierge to identify itself.
std::map<std::string, std::string> container_token_to_name_;
// Pending map of container tokens to names. The tokens are put in here when
// they are generated and removed once we have a connection from the
// container. We do not immediately put them in the contaienr_token_to_name_
// map because we may get redundant requests to start a container that is
// already running and we don't want to invalidate an in-use token.
std::map<std::string, std::string> pending_container_token_to_name_;
// Mapping of container names to a stub for making RPC requests to the garcon
// process inside the container.
std::map<std::string, std::unique_ptr<vm_tools::container::Garcon::Stub>>
container_name_to_garcon_stub_;
// Mapping of container names to a grpc Channel to the garcon process inside
// the container, which we can test for connectedness.
std::map<std::string, std::shared_ptr<grpc::Channel>>
container_name_to_garcon_channel_;
DISALLOW_COPY_AND_ASSIGN(VirtualMachine);
};
} // namespace cicerone
} // namespace vm_tools
#endif // VM_TOOLS_CICERONE_VIRTUAL_MACHINE_H_