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 | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 5 | #ifndef ARC_NETWORK_SUBNET_POOL_H_ |
| 6 | #define ARC_NETWORK_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 | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 18 | #include "arc/network/subnet.h" |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 19 | |
Garrick Evans | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 20 | namespace arc_networkd { |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 21 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 22 | // 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] | 23 | // 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] | 24 | class BRILLO_EXPORT SubnetPool { |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 25 | public: |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 26 | // Returns a new pool or nullptr if num_subnets exceeds 32. |
| 27 | static std::unique_ptr<SubnetPool> New(uint32_t base_addr, |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame^] | 28 | uint32_t prefix_length, |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 29 | uint32_t num_subnets); |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 30 | ~SubnetPool(); |
| 31 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 32 | // Allocates and returns a new subnet or nullptr if none are available. |
| 33 | std::unique_ptr<Subnet> Allocate(); |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 34 | |
| 35 | private: |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame^] | 36 | SubnetPool(uint32_t base_addr, uint32_t prefix_length, uint32_t num_subnets); |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 37 | |
Stephen Barber | 47981a7 | 2018-01-25 18:45:14 -0800 | [diff] [blame] | 38 | // Called by Subnets on destruction to free a given subnet. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 39 | void Release(uint32_t index); |
Stephen Barber | 47981a7 | 2018-01-25 18:45:14 -0800 | [diff] [blame] | 40 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 41 | const uint32_t base_addr_; |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame^] | 42 | const uint32_t prefix_length_; |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 43 | const uint32_t num_subnets_; |
| 44 | const uint32_t addr_per_index_; |
| 45 | std::bitset<32> subnets_; |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 46 | |
| 47 | base::WeakPtrFactory<SubnetPool> weak_ptr_factory_{this}; |
| 48 | DISALLOW_COPY_AND_ASSIGN(SubnetPool); |
| 49 | }; |
| 50 | |
Garrick Evans | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 51 | } // namespace arc_networkd |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 52 | |
Garrick Evans | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 53 | #endif // ARC_NETWORK_SUBNET_POOL_H_ |