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 | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 26 | // ARC++ management interface. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 27 | ARC, |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 28 | // ARC++ 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 | // ARCVM single interface. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 31 | VM_ARC, |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 32 | /// Crostini VM root namespace. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 33 | VM_TERMINA, |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 34 | // Crostini plugin VMs. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 35 | VM_PLUGIN, |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 36 | // Crostini VM user containers. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 37 | CONTAINER, |
Hugo Benichi | adf1ec5 | 2020-01-17 16:23:58 +0900 | [diff] [blame] | 38 | // Other network namespaces hosting minijailed host processes. |
| 39 | MINIJAIL_NETNS, |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 40 | }; |
| 41 | |
Garrick Evans | 4ee5ce2 | 2020-03-18 07:05:17 +0900 | [diff] [blame] | 42 | AddressManager(); |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 43 | virtual ~AddressManager() = default; |
| 44 | |
| 45 | // Generates a MAC address guaranteed to be unique for the lifetime of this |
| 46 | // object. |
Garrick Evans | 7d9a232 | 2020-04-02 11:59:56 +0900 | [diff] [blame] | 47 | // If |index| is provided, a MAC address will be returned that is stable |
| 48 | // across all invocations and instantions. |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 49 | // Virtual for testing only. |
Garrick Evans | 7d9a232 | 2020-04-02 11:59:56 +0900 | [diff] [blame] | 50 | virtual MacAddress GenerateMacAddress(uint8_t index = kAnySubnetIndex); |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 51 | |
| 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 Evans | 43b4e2d | 2019-12-11 13:43:08 +0900 | [diff] [blame] | 55 | // |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] | 56 | // for |guest|, it is 1-based, so 0 indicates no preference. |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 57 | std::unique_ptr<Subnet> AllocateIPv4Subnet(Guest guest, |
Garrick Evans | 53a2a98 | 2020-02-05 10:53:35 +0900 | [diff] [blame] | 58 | uint32_t index = kAnySubnetIndex); |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 59 | |
| 60 | private: |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 61 | MacAddressGenerator mac_addrs_; |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 62 | 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 Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 69 | } // namespace patchpanel |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 70 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 71 | #endif // PATCHPANEL_ADDRESS_MANAGER_H_ |