Chapter 14: Writing a gateway application (advanced)#

In all previous chapters, provider and consumer lived in the same LoLa domain: the same OS instance, talking through shared memory that both of them can open. In the introduction we mentioned that score::mw::com can reach beyond a single OS instance via a gateway: a dedicated application in each domain that relays service instances to the other domain.

This chapter shows how to write such a gateway application for a hypervisor setup, where two VMs on the same ECU each run their own LoLa domain.

Note

This is an advanced chapter. Unlike the previous chapters, it cannot give you a ready-to-run example: a realistic gateway has to make shared memory created in one VM visible in another VM and has to exchange control messages between the VMs. Both depend on your hypervisor and OS, so score::mw::com cannot provide a generic implementation.

What score::mw::com provides is the gateway logic as a library. You write the application around it and the transport layer for your platform. The application in this chapter is complete, but it uses the bundled sample transport, which leaves the hypervisor-specific parts unimplemented (see The sample transport and its limits).

Prerequisites: chapter 1 and chapter 2 (configuration), plus a basic understanding of shared-memory mechanisms of your hypervisor.

Files/artifacts used#

The bazel project for this chapter is located in score/mw/com/doc/tutorial/chapter_14 and contains the following files:


File Name

Description

BUILD

Bazel targets: the gateway binary and one deployable package per gateway role (source and destination).

gateway_application_main.cpp

main(): command line parsing, signal handlers and score::mw::com runtime initialization.

gateway_application_runner.h / .cpp

RunGatewayApplication(): creates, sets up and starts the gateway, then waits for shutdown.

etc/mw_com_config_source_gateway.json / etc/mw_com_config_destination_gateway.json

Regular score::mw::com configuration of each gateway.

etc/mw_com_gateway_config_source_gateway.json / etc/mw_com_gateway_config_destination_gateway.json

Gateway configuration: which services to forward or accept, and which transport layer to use.

etc/mw_com_gateway_sample_transport_config_source_gateway.json / etc/mw_com_gateway_sample_transport_config_destination_gateway.json

Configuration of the sample transport layer (addresses and ports of the control channel).

logging.json

mw::log configuration of the gateway process.

How a gateway works#

Each LoLa domain runs one gateway process. Together, the gateway processes form one logical gateway, connected by a transport layer. For each forwarded service instance, a gateway plays one of two roles:

  • The source gateway runs in the domain where the service instance is originally provided. It finds the instance with a GenericProxy and tells the other side about it: that it exists, which events it has, and when new event data arrives.

  • The destination gateway runs in the domain that wants to use the service instance. It creates a GenericSkeleton (the forwarding skeleton) as a local stand-in and offers it. Local consumers find and use it like any other service instance; they don’t know that the original provider lives in another VM.

One gateway process can be the source for some service instances and the destination for others at the same time.

For a memory-sharing gateway, which is what this chapter targets, event data is not copied: the forwarding skeleton opens the shared-memory objects that the original provider created in the source domain. The gateways only exchange small control messages (service offered, service gone, consumer subscribed, new data available).

The detailed message sequences are described in the gateway README.

What you get and what you write#

Part

Provided by

Responsibility

GatewayApplication

score::mw::com (library)

Service discovery, generic proxies and skeletons, allow-list check, forwarding of subscriptions and updates.

Transport implementation

You

Control messages between the gateways, and making shared memory accessible across VMs.

Registration in TransportFactory

You

Maps the transport layer ID from the configuration to your Transport implementation.

The application (this chapter’s code)

You

score::mw::com runtime initialization, configuration loading, lifecycle and shutdown.

Configuration files

You

Three files per gateway (see Step 3: Configure both sides).

Step 1: Implement the transport layer for your hypervisor#

The transport layer is the part that depends on your platform. It implements the abstract class Transport from transport_layer/transport.h:

Listing 115 score/mw/com/gateway/transport_layer/transport.h#
class Transport
{
  public:
    Transport() = default;
    virtual ~Transport() = default;

    /// \brief Returns whether this transport implementation supports memory sharing between source and destination.
    /// \return true if shared memory transport is supported, false otherwise.
    virtual bool IsMemorySharingSupported() const = 0;

    /// \brief Sets up the transport layer (e.g. establishes connections or allocates resources).
    /// \return result indicating success or failure.
    virtual score::Result<void> Setup() = 0;

    /// \brief Shuts down the transport layer and releases all resources.
    virtual void Shutdown() = 0;

    /// \brief ProvideService API signature to be provided when IsMemorySharingSupported() == true.
    /// \details Transport layer implementation shall "forward" this call to the destination gateway, which is
    /// expected to create a (generic) skeleton for the given service instance and service elements. The (generic)
    /// skeleton created by the destination gateway in this case expects to work on existing shared memory, which is
    /// shared between the source and destination gateway. The transport layer implementation must ensure, that the
    /// shared memory is visible/accessible on the destination gateway side, when the (generic) skeleton gets created.
    /// \param service_instance_specifier instance specifier of the service to provide. It is expected, that this
    /// specifier is configured/existent in the mw_com_config.json at the destination gateway side.
    /// \param service_elements configuration of the service elements (events, fields, methods) of the service to
    /// provide. This information is needed to create the (generic) skeleton on the destination gateway side.
    /// \return result indicating success or failure.
    virtual score::Result<void> ProvideService(score::mw::com::InstanceSpecifier service_instance_specifier,
                                               std::vector<score::mw::com::EventInfo> service_elements) = 0;

    /// \brief OfferService API to trigger service-instance offering at the destination gateway side.
    /// \details Transport layer implementation shall "forward" this call to the destination gateway and trigger the
    /// offering of the service instance at the destination gateway side. The service instance is expected to be
    /// already created at the destination gateway side, e.g. by a previous call to ProvideService().
    /// \param service_instance_specifier instance specifier of the service instance to offer.
    /// \return result indicating success or failure.
    virtual score::Result<void> OfferService(score::mw::com::InstanceSpecifier service_instance_specifier) = 0;

    /// \brief StopOfferService API to trigger service-instance stop-offer at the destination gateway side.
    /// \details Transport layer implementation shall "forward" this call to the destination gateway and trigger the
    /// stop-offer of the service instance at the destination gateway side. The service instance is expected to be
    /// already created at the destination gateway side, e.g. by a previous call to ProvideService().
    /// \param service_instance_specifier instance specifier of the service instance to stop-offer.
    /// \return result indicating success or failure.
    virtual score::Result<void> StopOfferService(score::mw::com::InstanceSpecifier service_instance_specifier) = 0;

    /// \brief NotifyUpdate API to inform the destination gateway about updates of service elements of a service
    /// instance on the source gateway side.
    /// \details The transport layer implementation shall "forward" this call to the destination gateway, which is
    /// expected to trigger update notifications at the (generic) skeleton of the related service instance on the
    /// destination gateway side for the given service element. API will only be called by the source gateway, if
    /// previously the destination gateway registered for update notifications for the given service instance and
    /// service element by calling the RegisterUpdateNotification API.
    /// \param service_instance_specifier instance specifier of the service instance owning the service element, for
    /// which an update shall be notified.
    /// \param updated_element_type type of the updated service element (event, field, method). Currently only EVENT
    /// is supported.
    /// \param updated_element_name name of the updated service element.
    /// \return result indicating success or failure.
    virtual score::Result<void> NotifyUpdate(score::mw::com::InstanceSpecifier service_instance_specifier,
                                             impl::ServiceElementType updated_element_type,
                                             std::string updated_element_name) = 0;

    /// \brief API to register for update notifications for service elements of a service instance at the source
    /// gateway side.
    /// \details The transport layer implementation shall "forward" this call to the source gateway, which is expected
    /// to register for update notifications at the (generic) proxy of the related service instance on the source
    /// gateway side for the given service element.
    /// \param service_instance_specifier instance specifier of the service instance owning the service element, for
    /// which an update-notification shall be registered.
    /// \param element_type type of the service element (event, field, method). Currently only EVENT is supported.
    /// \param element_name name of the service element.
    /// \return result indicating success or failure.
    virtual score::Result<void> RegisterUpdateNotification(score::mw::com::InstanceSpecifier service_instance_specifier,
                                                           impl::ServiceElementType element_type,
                                                           std::string element_name) = 0;

    /// \brief API to unregister update notifications for service elements of a service instance at the source gateway
    /// side.
    /// \details See RegisterUpdateNotification API for the corresponding registration semantics.
    /// \param service_instance_specifier instance specifier of the service instance owning the service element, for
    /// which the update-notification shall be unregistered.
    /// \param element_type type of the service element (event, field, method). Currently only EVENT is supported.
    /// \param element_name name of the service element.
    /// \return result indicating success or failure.
    virtual score::Result<void> UnregisterUpdateNotification(
        score::mw::com::InstanceSpecifier service_instance_specifier,
        impl::ServiceElementType element_type,
        std::string element_name) = 0;
};

Your implementation has two jobs:

  1. Control channel. Each call above must reach the gateway on the other side. There, your transport receives the message and calls the corresponding method of GatewayCore (gateway_core.h) on the GatewayCore& it was constructed with. That GatewayCore is the GatewayApplication of that side. For calls that return a Result, the outcome on the remote side should be returned to the caller.

  2. Shared memory. When IsMemorySharingSupported() returns true, the destination gateway’s forwarding skeleton opens the shared-memory objects created by the original provider in the source domain. Your transport must make sure those objects are accessible in the destination VM before it calls GatewayCore::ProvideService(), because that call creates the skeleton.

The second job is what makes the transport hypervisor-specific: how shared memory is exported from one VM and mapped in another differs between hypervisors.

The sample transport and its limits#

score::mw::com ships a sample transport (ID sample_hypervisor) in transport_layer/sample, which this chapter’s application uses. It is a good starting point for your own implementation:

  • It implements the control channel completely, over TCP sockets, which most hypervisors offer between VMs. It handles request/acknowledge pairs with a configurable timeout.

  • It does not implement the shared-memory part. These three functions in sample_hypervisor_transport.cpp contain TODO markers and fail a precondition check, which terminates the process:

    • ResolveShmPaths(): map an InstanceSpecifier to the names of its shared-memory objects in your hypervisor’s shared-memory technology.

    • GetShmSizes(): determine the sizes of those objects on the source side, so the destination side can verify them.

    • PreCreateInterVmSharedMemory(): make the objects accessible in the destination VM.

Warning

With the sample transport unchanged, both gateways start up and connect to each other, but the source gateway terminates as soon as it discovers one of its forwarded service instances, because ProvideService() calls GetShmSizes(). To forward services, implement the three functions above for your hypervisor, or write your own Transport.

Registering your transport#

GatewayApplication does not create the transport itself; it asks TransportFactory::Create() in transport_factory.cpp to create the transport for the ID in the gateway configuration. Add a branch for your ID there, which parses your transport’s own configuration file and constructs your implementation. An ID without a branch terminates the process.

Step 2: Write the application#

With a transport in place, the application itself is short. It is split into main() and a reusable RunGatewayApplication(), so that the gateway can also be embedded into an existing process without going through main().

main()#

The application takes two arguments: the path to its score::mw::com configuration and the path to its gateway configuration. Before doing anything else, it installs handlers for SIGINT and SIGTERM, which are used to shut the gateway down cleanly:

Listing 116 gateway_application_main.cpp#
    if (argc != 3)
    {
        score::mw::log::LogError() << "Usage: gateway_application_bin <mw_com_config-path> <gateway-config-path>";
        return 1;
    }

    std::signal(SIGINT, score::mw::com::gateway::SignalHandler);
    std::signal(SIGTERM, score::mw::com::gateway::SignalHandler);

Next, it initializes the score::mw::com runtime with the given configuration, as you have seen since chapter 2. This has to happen before the gateway is created: the gateway creates proxies and skeletons, and both need the deployment information from that configuration. Then it hands over to RunGatewayApplication():

Listing 117 gateway_application_main.cpp#
    const score::cpp::span<char*> args{argv, static_cast<std::size_t>(argc)};
    const std::string mw_com_config_path = args[1];
    const std::string gateway_config_path = args[2];
    score::mw::com::runtime::InitializeRuntime(
        score::mw::com::runtime::RuntimeConfiguration{score::filesystem::Path{mw_com_config_path}});

    score::mw::com::gateway::RunGatewayApplication(gateway_config_path);

RunGatewayApplication()#

RunGatewayApplication() first parses the gateway configuration and creates the GatewayApplication from it. ParseGatewayConfig() terminates the process if the file is missing or invalid:

Listing 118 gateway_application_runner.cpp#
    auto app_configuration = ParseGatewayConfig(config_path);
    GatewayApplication gateway_app{std::move(app_configuration)};

Starting the gateway takes two calls, and their order matters:

  • Setup() creates the transport via TransportFactory and calls Transport::Setup(), which connects to the gateway on the other side. After this, the gateway can receive requests from the other side, so a destination gateway is fully operational at this point. The sample transport’s Setup() blocks until the other gateway is connected, so the two gateways can be started in any order.

  • Start() starts service discovery for every entry in forwarded-services. Whenever one of these service instances appears or disappears, the gateway informs the other side through the transport. For a gateway that only receives services, the list is empty and Start() does nothing.

Listing 119 gateway_application_runner.cpp#
    const auto setup_result = gateway_app.Setup();
    if (!setup_result.has_value())
    {
        score::mw::log::LogError() << "Gateway application setup failed: " << setup_result.error();
        return;
    }

    const auto start_result = gateway_app.Start();
    if (!start_result.has_value())
    {
        score::mw::log::LogError() << "Gateway application failed to start: " << start_result.error();
        return;
    }

All further work happens in callbacks from service discovery and from the transport, so the calling thread only waits until the signal handler requests a shutdown:

Listing 120 gateway_application_runner.cpp#
namespace
{
std::atomic<bool> g_running{true};
std::condition_variable g_shutdown_cv;
std::mutex g_shutdown_mutex;
}  // namespace

void SignalHandler(int /*signal*/) noexcept
{
    g_running.store(false, std::memory_order_relaxed);
    g_shutdown_cv.notify_one();
}
Listing 121 gateway_application_runner.cpp#
    score::mw::log::LogInfo() << "Gateway application started. Press Ctrl+C to stop.";

    std::unique_lock<std::mutex> lock{g_shutdown_mutex};
    while (g_running.load(std::memory_order_relaxed))
    {
        g_shutdown_cv.wait(lock);
    }

    score::mw::log::LogInfo() << "Gateway application shutting down.";

When RunGatewayApplication() returns, gateway_app is destroyed. Its destructor first shuts down the transport, so that no callback stays blocked waiting for the other side, then invalidates all pending callbacks and finally stops offering all forwarding skeletons.

Step 3: Configure both sides#

Each gateway process needs three configuration files. In this chapter, the source gateway forwards two service instances and the destination gateway receives them.

File

Passed via

Content

mw_com_config_*.json

first command line argument

Regular score::mw::com configuration: service types and instances.

mw_com_gateway_config_*.json

second command line argument

Which service instances to forward or accept, and which transport layer to use.

Transport configuration

config-path in the gateway configuration

Transport-specific. For sample_hypervisor: addresses and ports of the control channel.

Gateway configuration#

The source gateway lists the instance specifiers it forwards. The destination gateway lists the instance specifiers it accepts. expected-received-services is an allow-list: the destination gateway rejects any service instance the other side tries to provide that is not listed here.

Listing 122 etc/mw_com_gateway_config_source_gateway.json#
{
  "forwarded-services": [
    "MyHelloWorldServiceInstance",
    "/Vehicle/Service1/Instance"
  ],
  "expected-received-services": [
  ],
  "transport-layer": {
    "id": "sample_hypervisor",
    "config-path": "/opt/SourceGateway/etc/mw_com_gateway_sample_transport_config_source_gateway.json"
  }
}
Listing 123 etc/mw_com_gateway_config_destination_gateway.json#
{
  "forwarded-services": [
  ],
  "expected-received-services": [
    "MyHelloWorldServiceInstance",
    "/Vehicle/Service1/Instance"
  ],
  "transport-layer": {
    "id": "sample_hypervisor",
    "config-path": "/opt/DestinationGateway/etc/mw_com_gateway_sample_transport_config_destination_gateway.json"
  }
}

transport-layer.id selects the transport in TransportFactory. config-path is passed unchanged to the transport, so it is not resolved relative to the gateway configuration file. Here it is an absolute path that matches where the packages of this chapter install their configuration (/opt/<app>/etc).

Transport configuration#

For the sample transport, each side configures the address of the other side and a pair of ports, which are mirrored between the two sides. Adapt remote-ip to the addresses of your VMs.

Listing 124 etc/mw_com_gateway_sample_transport_config_source_gateway.json#
{
  "hypervisor-socket": {
    "remote-ip": "172.17.0.6",
    "local-port": 45001,
    "remote-port": 45002,
    "request-timeout-ms": 5000
  }
}
Listing 125 etc/mw_com_gateway_sample_transport_config_destination_gateway.json#
{
  "hypervisor-socket": {
    "remote-ip": "172.17.0.1",
    "local-port": 45002,
    "remote-port": 45001,
    "request-timeout-ms": 5000
  }
}

score::mw::com configuration#

On the source side, the gateway uses this configuration to find the forwarded service instances. On the destination side, the forwarding skeleton reads the deployment of each received service instance from it, so every received service instance must be configured on the destination side, or creating the forwarding skeleton fails.

The HelloWorldService instance below is the one provided by chapter 2, extended by two settings for sharing it between VMs:

Listing 126 etc/mw_com_config_destination_gateway.json#
    {
      "instanceSpecifier": "MyHelloWorldServiceInstance",
      "serviceTypeName": "/score/mw/com/tutorial/HelloWorldService",
      "version": {
        "major": 1,
        "minor": 0
      },
      "instances": [
        {
          "instanceId": 1,
          "asil-level": "QM",
          "binding": "SHM",
          "events": [
            {
              "eventName": "message",
              "numberOfSampleSlots": 10,
              "maxSubscribers": 3
            }
          ],
          "interVmSupport": true,
          "interVmForwarded": true
        }
      ]
    },
  • interVmSupport creates and opens the shared-memory objects of this instance under names that can be shared between VMs. Since it changes the names, it must be set in the configuration of every application that uses this instance: the original provider and the source gateway, and on the destination side the gateway and all consumers.

  • interVmForwarded tells the forwarding skeleton to open the shared-memory objects created in the source domain, instead of creating its own. It requires interVmSupport.

Step 4: Build and deploy#

The chapter builds one binary and packages it twice, once per role, together with that role’s configuration:

bazel build //score/mw/com/doc/tutorial/chapter_14:source_gateway-tar
bazel build //score/mw/com/doc/tutorial/chapter_14:destination_gateway-tar

Deploy the source package to the VM that runs the original provider and the destination package to the VM with the consumers, then start one gateway in each VM:

# In the source VM
/opt/SourceGateway/bin/gateway_application_bin \
    /opt/SourceGateway/etc/mw_com_config_source_gateway.json \
    /opt/SourceGateway/etc/mw_com_gateway_config_source_gateway.json

# In the destination VM
/opt/DestinationGateway/bin/gateway_application_bin \
    /opt/DestinationGateway/etc/mw_com_config_destination_gateway.json \
    /opt/DestinationGateway/etc/mw_com_gateway_config_destination_gateway.json

mw::log finds logging.json in ../etc relative to the binary. Stop a gateway with Ctrl+C or SIGTERM.

Remember the limits of the sample transport: until you implement its shared-memory part, the gateways connect, but cannot forward a service instance yet.

Summary#

To bring score::mw::com communication across VMs on your hypervisor:

  1. Implement Transport for your hypervisor, starting from the sample transport. Its control channel can be reused; the shared-memory functions have to be written for your platform.

  2. Register your transport ID in TransportFactory::Create().

  3. Write the application as shown in this chapter: initialize the runtime, then Setup(), Start() and wait for shutdown.

  4. Configure both sides: forwarded and accepted service instances, the transport, and interVmSupport / interVmForwarded for every forwarded instance.