blob: 0c854ab42f58098f92b2ba7d1a500b26b8f18ba9 [file] [log] [blame]
Garrick Evans0dbd4182019-03-07 08:38:38 +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_ADDRESS_MANAGER_H_
6#define PATCHPANEL_ADDRESS_MANAGER_H_
Garrick Evans0dbd4182019-03-07 08:38:38 +09007
8#include <map>
9#include <memory>
10
11#include <base/callback.h>
12#include <base/macros.h>
13#include <base/memory/weak_ptr.h>
14#include <brillo/brillo_export.h>
15
Garrick Evans3388a032020-03-24 11:25:55 +090016#include "patchpanel/mac_address_generator.h"
17#include "patchpanel/subnet.h"
18#include "patchpanel/subnet_pool.h"
Garrick Evans0dbd4182019-03-07 08:38:38 +090019
Garrick Evans3388a032020-03-24 11:25:55 +090020namespace patchpanel {
Garrick Evans0dbd4182019-03-07 08:38:38 +090021
22// Responsible for address provisioning for guest networks.
23class BRILLO_EXPORT AddressManager {
24 public:
25 enum class Guest {
Hugo Benichiadf1ec52020-01-17 16:23:58 +090026 // ARC++ management interface.
Garrick Evans0dbd4182019-03-07 08:38:38 +090027 ARC,
Hugo Benichiadf1ec52020-01-17 16:23:58 +090028 // ARC++ virtual networks connected to shill Devices.
Garrick Evans0dbd4182019-03-07 08:38:38 +090029 ARC_NET,
Hugo Benichiadf1ec52020-01-17 16:23:58 +090030 // ARCVM single interface.
Garrick Evans0dbd4182019-03-07 08:38:38 +090031 VM_ARC,
Hugo Benichiadf1ec52020-01-17 16:23:58 +090032 /// Crostini VM root namespace.
Garrick Evans0dbd4182019-03-07 08:38:38 +090033 VM_TERMINA,
Hugo Benichiadf1ec52020-01-17 16:23:58 +090034 // Crostini plugin VMs.
Garrick Evans0dbd4182019-03-07 08:38:38 +090035 VM_PLUGIN,
Hugo Benichiadf1ec52020-01-17 16:23:58 +090036 // Crostini VM user containers.
Garrick Evans0dbd4182019-03-07 08:38:38 +090037 CONTAINER,
Hugo Benichiadf1ec52020-01-17 16:23:58 +090038 // Other network namespaces hosting minijailed host processes.
39 MINIJAIL_NETNS,
Garrick Evans0dbd4182019-03-07 08:38:38 +090040 };
41
Garrick Evans4ee5ce22020-03-18 07:05:17 +090042 AddressManager();
Garrick Evansf4a93292019-03-13 14:19:43 +090043 virtual ~AddressManager() = default;
44
45 // Generates a MAC address guaranteed to be unique for the lifetime of this
46 // object.
Garrick Evans7d9a2322020-04-02 11:59:56 +090047 // If |index| is provided, a MAC address will be returned that is stable
48 // across all invocations and instantions.
Garrick Evansf4a93292019-03-13 14:19:43 +090049 // Virtual for testing only.
Garrick Evans7d9a2322020-04-02 11:59:56 +090050 virtual MacAddress GenerateMacAddress(uint8_t index = kAnySubnetIndex);
Garrick Evans0dbd4182019-03-07 08:38:38 +090051
52 // Allocates a subnet from the specified guest network pool if available.
53 // Returns nullptr if the guest was configured or no more subnets are
54 // available for allocation.
Garrick Evans43b4e2d2019-12-11 13:43:08 +090055 // |index| is used to acquire a particular subnet from the pool, if supported
Garrick Evans53a2a982020-02-05 10:53:35 +090056 // for |guest|, it is 1-based, so 0 indicates no preference.
Garrick Evans51d5b552020-01-30 10:42:06 +090057 std::unique_ptr<Subnet> AllocateIPv4Subnet(Guest guest,
Garrick Evans53a2a982020-02-05 10:53:35 +090058 uint32_t index = kAnySubnetIndex);
Garrick Evans0dbd4182019-03-07 08:38:38 +090059
60 private:
Garrick Evansf4a93292019-03-13 14:19:43 +090061 MacAddressGenerator mac_addrs_;
Garrick Evans0dbd4182019-03-07 08:38:38 +090062 std::map<Guest, std::unique_ptr<SubnetPool>> pools_;
63
64 base::WeakPtrFactory<AddressManager> weak_ptr_factory_{this};
65
66 DISALLOW_COPY_AND_ASSIGN(AddressManager);
67};
68
Garrick Evans3388a032020-03-24 11:25:55 +090069} // namespace patchpanel
Garrick Evans0dbd4182019-03-07 08:38:38 +090070
Garrick Evans3388a032020-03-24 11:25:55 +090071#endif // PATCHPANEL_ADDRESS_MANAGER_H_