Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 1 | // 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 Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #ifndef PATCHPANEL_ADDRESS_MANAGER_H_ |
| 6 | #define PATCHPANEL_ADDRESS_MANAGER_H_ |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 7 | |
| 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 Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 16 | #include "patchpanel/mac_address_generator.h" |
| 17 | #include "patchpanel/subnet.h" |
| 18 | #include "patchpanel/subnet_pool.h" |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 19 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 20 | namespace patchpanel { |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 21 | |
| 22 | // Responsible for address provisioning for guest networks. |
| 23 | class BRILLO_EXPORT AddressManager { |
| 24 | public: |
| 25 | enum class Guest { |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame^] | 26 | // ARC++ or ARCVM management interface. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 27 | ARC, |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame^] | 28 | // ARC++ or ARCVM virtual networks connected to shill Devices. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 29 | ARC_NET, |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 30 | /// Crostini VM root namespace. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 31 | VM_TERMINA, |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 32 | // Crostini plugin VMs. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 33 | VM_PLUGIN, |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 34 | // Crostini VM user containers. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 35 | CONTAINER, |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 36 | // Other network namespaces hosting minijailed host processes. |
| 37 | MINIJAIL_NETNS, |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 38 | }; |
| 39 | |
Garrick Evans | 4ee5ce2 | 2020-03-18 07:05:17 +0900 | [diff] [blame] | 40 | AddressManager(); |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 41 | virtual ~AddressManager() = default; |
| 42 | |
| 43 | // Generates a MAC address guaranteed to be unique for the lifetime of this |
| 44 | // object. |
Garrick Evans | 7d9a232 | 2020-04-02 11:59:56 +0900 | [diff] [blame] | 45 | // If |index| is provided, a MAC address will be returned that is stable |
| 46 | // across all invocations and instantions. |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 47 | // Virtual for testing only. |
Garrick Evans | 7d9a232 | 2020-04-02 11:59:56 +0900 | [diff] [blame] | 48 | virtual MacAddress GenerateMacAddress(uint8_t index = kAnySubnetIndex); |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 49 | |
| 50 | // Allocates a subnet from the specified guest network pool if available. |
| 51 | // Returns nullptr if the guest was configured or no more subnets are |
| 52 | // available for allocation. |
Garrick Evans | 43b4e2d | 2019-12-11 13:43:08 +0900 | [diff] [blame] | 53 | // |index| is used to acquire a particular subnet from the pool, if supported |
Garrick Evans | 53a2a98 | 2020-02-05 10:53:35 +0900 | [diff] [blame] | 54 | // for |guest|, it is 1-based, so 0 indicates no preference. |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 55 | std::unique_ptr<Subnet> AllocateIPv4Subnet(Guest guest, |
Garrick Evans | 53a2a98 | 2020-02-05 10:53:35 +0900 | [diff] [blame] | 56 | uint32_t index = kAnySubnetIndex); |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 57 | |
| 58 | private: |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 59 | MacAddressGenerator mac_addrs_; |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 60 | std::map<Guest, std::unique_ptr<SubnetPool>> pools_; |
| 61 | |
| 62 | base::WeakPtrFactory<AddressManager> weak_ptr_factory_{this}; |
| 63 | |
| 64 | DISALLOW_COPY_AND_ASSIGN(AddressManager); |
| 65 | }; |
| 66 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 67 | } // namespace patchpanel |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 68 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 69 | #endif // PATCHPANEL_ADDRESS_MANAGER_H_ |