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 | #include "arc/network/subnet_pool.h" |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 6 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 7 | #include <memory> |
Stephen Barber | 47981a7 | 2018-01-25 18:45:14 -0800 | [diff] [blame] | 8 | #include <string> |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
| 11 | #include <base/bind.h> |
| 12 | #include <base/logging.h> |
| 13 | #include <base/memory/ptr_util.h> |
| 14 | #include <base/strings/stringprintf.h> |
| 15 | |
| 16 | using std::string; |
| 17 | |
Garrick Evans | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 18 | namespace arc_networkd { |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 19 | namespace { |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 20 | constexpr uint32_t kMaxSubnets = 32; |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 21 | } // namespace |
| 22 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 23 | // static |
| 24 | std::unique_ptr<SubnetPool> SubnetPool::New(uint32_t base_addr, |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame^] | 25 | uint32_t prefix_length, |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 26 | uint32_t num_subnets) { |
| 27 | if (num_subnets > kMaxSubnets) { |
| 28 | LOG(ERROR) << "Maximum subnets supported is " << kMaxSubnets << "; got " |
| 29 | << num_subnets; |
| 30 | return nullptr; |
| 31 | } |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame^] | 32 | return base::WrapUnique( |
| 33 | new SubnetPool(base_addr, prefix_length, num_subnets)); |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | SubnetPool::SubnetPool(uint32_t base_addr, |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame^] | 37 | uint32_t prefix_length, |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 38 | uint32_t num_subnets) |
| 39 | : base_addr_(base_addr), |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame^] | 40 | prefix_length_(prefix_length), |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 41 | num_subnets_(num_subnets), |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame^] | 42 | addr_per_index_(1ull << (kMaxSubnets - prefix_length)) {} |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 43 | |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 44 | SubnetPool::~SubnetPool() { |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 45 | if (subnets_.any()) { |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 46 | LOG(ERROR) << "SubnetPool destroyed with unreleased subnets"; |
| 47 | } |
| 48 | } |
| 49 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 50 | std::unique_ptr<Subnet> SubnetPool::Allocate() { |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 51 | // Find the first un-allocated subnet. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 52 | uint32_t index = 0; |
| 53 | while (index < num_subnets_ && subnets_.test(index)) { |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 54 | ++index; |
| 55 | } |
| 56 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 57 | if (index == num_subnets_) { |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 58 | // All subnets are allocated. |
| 59 | return nullptr; |
| 60 | } |
| 61 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 62 | subnets_.set(index); |
| 63 | return std::make_unique<Subnet>( |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame^] | 64 | base_addr_ + (index * addr_per_index_), prefix_length_, |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 65 | base::Bind(&SubnetPool::Release, weak_ptr_factory_.GetWeakPtr(), index)); |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 68 | void SubnetPool::Release(uint32_t index) { |
| 69 | DCHECK(subnets_.test(index)); |
| 70 | subnets_.reset(index); |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Garrick Evans | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 73 | } // namespace arc_networkd |