Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/core/common/networkmanager/itf/pendingupdatehandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ struct PendingFirewallUpdate {
};

/**
* Handler for pending firewall update notifications.
* Handler for CM -> SM network notifications: pending firewall updates and unit networks updates.
* CM side: CM NetworkManager notifies SMController which pushes to stream.
* SM side: SM NetworkManager implements this to apply resolved pending rules.
* SM side: SM NetworkManager implements this to apply resolved pending rules and to keep the node networks in sync
* with the networks of the unit.
*/
class PendingUpdateHandlerItf {
public:
Expand All @@ -69,6 +70,17 @@ class PendingUpdateHandlerItf {
* @param update pending firewall update.
*/
virtual void OnPendingFirewallUpdate(const String& nodeID, const PendingFirewallUpdate& update) = 0;

/**
* Called with the full set of network IDs that exist in the unit.
*
* The set is complete, not a delta: a receiver has to replace the unit networks it holds with this set. Sent when
* SM subscribes and whenever the set changes.
*
* @param nodeID node ID the update is addressed to.
* @param networkIDs IDs of all networks of the unit.
*/
virtual void OnUnitNetworksUpdate(const String& nodeID, const Array<StaticString<cIDLen>>& networkIDs) = 0;
};

/** @}*/
Expand Down
51 changes: 47 additions & 4 deletions src/core/common/networkmanager/networkmanager.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This document describes the network manager interfaces shared between CM (Communication Manager) and SM (Service
Manager). They define the contract for allocating and releasing network resources, synchronizing network state on
(re)connect and delivering deferred firewall rule updates.
(re)connect, delivering deferred firewall rule updates and announcing the networks of the unit.

The overall networking architecture is described in the public documentation:

Expand All @@ -21,6 +21,10 @@ CM is the allocator: it hands out subnets, IP addresses and per-instance firewal
across the unit. SM is the runtime owner: it asks CM what an instance network should look like and builds it on the
node.

CM also keeps the set of networks that exist in the unit and announces it to every node. SM keeps every network of
this set present on its node, whether or not the node runs instances of it, so instances of different networks on
different nodes can reach each other and an instance can move to another node without waiting for its network.

The interfaces are placed in `common` because both sides depend on them:

- [aos::networkmanager::NetworkProviderItf](itf/networkprovider.hpp) - SM -> CM requests. On the SM side it is
Expand Down Expand Up @@ -54,6 +58,7 @@ classDiagram
class PendingUpdateHandlerItf ["aos::networkmanager::PendingUpdateHandlerItf"] {
<<interface>>
OnPendingFirewallUpdate()
OnUnitNetworksUpdate()
}

%% SM -> CM requests
Expand All @@ -73,7 +78,8 @@ Network provider interface used by SM to request network resources from CM.
### GetNodeNetworkParams

Returns node network parameters (`NetworkParams`: subnet, bridge IP, VLAN ID) for the given network and node. SM calls
it once per network when the first instance of that network is created on the node.
it once per network when the network is not known on the node yet: when CM announces it in the unit networks (see
[Unit networks](#unit-networks)) or when the first instance of that network is created on the node.

### AllocateInstanceNetwork

Expand All @@ -91,7 +97,8 @@ Releases instance network resources on CM. SM calls it when the instance network

### ReleaseNodeNetwork

Releases node network resources on CM. SM calls it when the last instance of the network on the node is released.
Releases node network resources on CM. SM calls it when the network is not in the unit networks and the node has no
instances of it: when the network leaves the unit networks or when its last instance on the node is released.

### SyncNetworkState

Expand All @@ -102,7 +109,7 @@ not drift apart after restarts or connection interruptions.

## aos::networkmanager::PendingUpdateHandlerItf

Handler interface for deferred firewall update notifications.
Handler interface for CM -> SM notifications: deferred firewall updates and unit networks updates.

### OnPendingFirewallUpdate

Expand All @@ -111,6 +118,11 @@ identifier and the instance's whole current rule set, resolved from all of its a
target addresses. The receiver has to replace the rules it holds for the instance with these, so rules to released or
re-addressed targets are removed and rules to new addresses are added.

### OnUnitNetworksUpdate

Called with the IDs of all networks that exist in the unit. The set is complete, not a delta: the receiver replaces the
set it holds with it. CM sends it when SM subscribes and whenever the set changes.

## Deferred firewall rules

When an instance has allowed connections to a target (item ID or hostname) that has no allocated instance yet, CM
Expand Down Expand Up @@ -138,6 +150,37 @@ sequenceDiagram
SM_A ->> SM_A: replace rules, update instance1 firewall
```

## Unit networks

CM announces the networks that exist in the unit to every node via `PendingUpdateHandlerItf::OnUnitNetworksUpdate`. SM
stays the initiator of node network creation: for every announced network it does not have yet, it requests the node
network parameters with `GetNodeNetworkParams` and creates the network links. When a network leaves the set, SM releases
it with `ReleaseNodeNetwork` once the node has no instances of it.

```mermaid
sequenceDiagram
participant CM
participant SM_A as SM (node A)
participant SM_B as SM (node B)

CM ->> SM_A: OnUnitNetworksUpdate(nodeA, [network1, network2])
CM ->> SM_B: OnUnitNetworksUpdate(nodeB, [network1, network2])

loop announced networks not known on the node
SM_B ->> CM: GetNodeNetworkParams(network2, nodeB)
CM -->> SM_B: NetworkParams
SM_B ->> SM_B: create bridge/VLAN
end

CM ->> SM_A: OnUnitNetworksUpdate(nodeA, [network1])
CM ->> SM_B: OnUnitNetworksUpdate(nodeB, [network1])

alt no instances of network2 on node B
SM_B ->> SM_B: remove bridge/VLAN
SM_B ->> CM: ReleaseNodeNetwork(network2, nodeB)
end
```

## Types

The data types used by the interfaces are defined in [core/common/types/network.hpp](../types/network.hpp):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class PendingUpdateHandlerMock : public aos::networkmanager::PendingUpdateHandle
public:
MOCK_METHOD(void, OnPendingFirewallUpdate,
(const aos::String& nodeID, const aos::networkmanager::PendingFirewallUpdate& update), (override));
MOCK_METHOD(void, OnUnitNetworksUpdate,
(const aos::String& nodeID, const aos::Array<aos::StaticString<aos::cIDLen>>& networkIDs), (override));
};

#endif
5 changes: 3 additions & 2 deletions src/core/sm/networkmanager/itf/dnsname.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ class DNSNameItf {
/**
* Removes orphan DNS servers left over from a previous SM lifetime.
* Tears down any dnsmasq state whose networkID is not in
* @p knownNetworkIDs — networks that were released while SM was down.
* @p knownNetworkIDs — networks that were released while SM was down
* or have no instances.
* Called once from NetworkManager::Start, before any CreateServer.
*
* @param knownNetworkIDs networkIDs still present in storage.
* @param knownNetworkIDs networkIDs that have stored instances.
* @return Error.
*/
virtual Error RemoveOrphans(const Array<StaticString<cIDLen>>& knownNetworkIDs) = 0;
Expand Down
6 changes: 4 additions & 2 deletions src/core/sm/networkmanager/itf/networkmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ class NetworkManagerItf : public SystemTrafficProviderItf,
/**
* Stops instance network: tears down DNS/bandwidth/firewall/bridge for the
* instance, deletes its namespace and monitoring.
* If last running instance on network — clears bridge/VLAN.
* If last running instance on network — removes its DNS server and, unless the network is a unit network,
* clears bridge/VLAN.
* Does NOT remove from DB, does NOT call CM.
*
* @param instanceID instance ID.
Expand All @@ -120,7 +121,8 @@ class NetworkManagerItf : public SystemTrafficProviderItf,

/**
* Releases instance network: removes from DB, calls CM ReleaseInstanceNetwork.
* If last instance on network — removes node network info from DB, releases on CM.
* If last instance on network and the network is not a unit network — removes node network info from DB,
* releases on CM.
* Requires Stop to be called first.
*
* @param instanceID instance ID.
Expand Down
Loading
Loading