Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 1 | // Copyright 2017 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_SUBNET_POOL_H_ |
| 6 | #define PATCHPANEL_SUBNET_POOL_H_ |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 7 | |
| 8 | #include <stdint.h> |
| 9 | |
| 10 | #include <bitset> |
| 11 | #include <memory> |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 12 | |
| 13 | #include <base/callback.h> |
| 14 | #include <base/macros.h> |
| 15 | #include <base/memory/weak_ptr.h> |
Garrick Evans | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 16 | #include <brillo/brillo_export.h> |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 17 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 18 | #include "patchpanel/subnet.h" |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 19 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 20 | namespace patchpanel { |
Garrick Evans | 53a2a98 | 2020-02-05 10:53:35 +0900 | [diff] [blame] | 21 | constexpr uint32_t kAnySubnetIndex = 0; |
| 22 | constexpr uint32_t kMaxSubnets = 32; |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 23 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 24 | // Manages up to 32 IPv4 subnets that can be assigned to guest interfaces. |
Stephen Barber | 47981a7 | 2018-01-25 18:45:14 -0800 | [diff] [blame] | 25 | // These use non-publicly routable addresses in the range 100.115.92.0/24. |
Garrick Evans | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 26 | class BRILLO_EXPORT SubnetPool { |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 27 | public: |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 28 | // Returns a new pool or nullptr if num_subnets exceeds 32. |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 29 | // |base_addr| must be in network-byte order. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 30 | static std::unique_ptr<SubnetPool> New(uint32_t base_addr, |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 31 | uint32_t prefix_length, |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 32 | uint32_t num_subnets); |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 33 | ~SubnetPool(); |
| 34 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 35 | // Allocates and returns a new subnet or nullptr if none are available. |
Garrick Evans | 53a2a98 | 2020-02-05 10:53:35 +0900 | [diff] [blame] | 36 | // |index| may be used to request a particular subnet, it is 1-based so 0 |
| 37 | // indicates no preference. |
| 38 | std::unique_ptr<Subnet> Allocate(uint32_t index = kAnySubnetIndex); |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 39 | |
| 40 | private: |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 41 | SubnetPool(uint32_t base_addr, uint32_t prefix_length, uint32_t num_subnets); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame^] | 42 | SubnetPool(const SubnetPool&) = delete; |
| 43 | SubnetPool& operator=(const SubnetPool&) = delete; |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 44 | |
Stephen Barber | 47981a7 | 2018-01-25 18:45:14 -0800 | [diff] [blame] | 45 | // Called by Subnets on destruction to free a given subnet. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 46 | void Release(uint32_t index); |
Stephen Barber | 47981a7 | 2018-01-25 18:45:14 -0800 | [diff] [blame] | 47 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 48 | const uint32_t base_addr_; |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 49 | const uint32_t prefix_length_; |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 50 | const uint32_t num_subnets_; |
| 51 | const uint32_t addr_per_index_; |
Garrick Evans | 53a2a98 | 2020-02-05 10:53:35 +0900 | [diff] [blame] | 52 | std::bitset<kMaxSubnets + 1> subnets_; |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 53 | |
| 54 | base::WeakPtrFactory<SubnetPool> weak_ptr_factory_{this}; |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 55 | }; |
| 56 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 57 | } // namespace patchpanel |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 58 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 59 | #endif // PATCHPANEL_SUBNET_POOL_H_ |