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 | #include "patchpanel/subnet_pool.h" |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 6 | |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 7 | #include <arpa/inet.h> |
| 8 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 9 | #include <memory> |
Stephen Barber | 47981a7 | 2018-01-25 18:45:14 -0800 | [diff] [blame] | 10 | #include <string> |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 11 | #include <utility> |
| 12 | |
| 13 | #include <base/bind.h> |
| 14 | #include <base/logging.h> |
| 15 | #include <base/memory/ptr_util.h> |
| 16 | #include <base/strings/stringprintf.h> |
| 17 | |
| 18 | using std::string; |
| 19 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 20 | namespace patchpanel { |
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 | // static |
| 23 | std::unique_ptr<SubnetPool> SubnetPool::New(uint32_t base_addr, |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 24 | uint32_t prefix_length, |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 25 | uint32_t num_subnets) { |
| 26 | if (num_subnets > kMaxSubnets) { |
| 27 | LOG(ERROR) << "Maximum subnets supported is " << kMaxSubnets << "; got " |
| 28 | << num_subnets; |
| 29 | return nullptr; |
| 30 | } |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 31 | return base::WrapUnique( |
| 32 | new SubnetPool(base_addr, prefix_length, num_subnets)); |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | SubnetPool::SubnetPool(uint32_t base_addr, |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 36 | uint32_t prefix_length, |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 37 | uint32_t num_subnets) |
| 38 | : base_addr_(base_addr), |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 39 | prefix_length_(prefix_length), |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 40 | num_subnets_(num_subnets), |
Garrick Evans | 53a2a98 | 2020-02-05 10:53:35 +0900 | [diff] [blame] | 41 | addr_per_index_(1ull << (kMaxSubnets - prefix_length)) { |
| 42 | subnets_.set(0); // unused. |
| 43 | } |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 44 | |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 45 | SubnetPool::~SubnetPool() { |
Garrick Evans | 53a2a98 | 2020-02-05 10:53:35 +0900 | [diff] [blame] | 46 | subnets_.reset(0); |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 47 | if (subnets_.any()) { |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 48 | LOG(ERROR) << "SubnetPool destroyed with unreleased subnets"; |
| 49 | } |
| 50 | } |
| 51 | |
Garrick Evans | 53a2a98 | 2020-02-05 10:53:35 +0900 | [diff] [blame] | 52 | std::unique_ptr<Subnet> SubnetPool::Allocate(uint32_t index) { |
| 53 | if (index == 0) { |
| 54 | while (index <= num_subnets_ && subnets_.test(index)) { |
Garrick Evans | 43b4e2d | 2019-12-11 13:43:08 +0900 | [diff] [blame] | 55 | ++index; |
| 56 | } |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Garrick Evans | 53a2a98 | 2020-02-05 10:53:35 +0900 | [diff] [blame] | 59 | if (index > num_subnets_) { |
| 60 | LOG(ERROR) << "Desired index (" << index << ") execeeds number of" |
| 61 | << " available subnets (" << num_subnets_ << ")"; |
| 62 | return nullptr; |
| 63 | } |
| 64 | if (subnets_.test(index)) { |
| 65 | LOG(WARNING) << "Subnet at index (" << index << ") is unavailable"; |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 66 | return nullptr; |
| 67 | } |
| 68 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 69 | subnets_.set(index); |
Garrick Evans | 53a2a98 | 2020-02-05 10:53:35 +0900 | [diff] [blame] | 70 | uint32_t subnet_addr = |
| 71 | htonl(ntohl(base_addr_) + (index - 1) * addr_per_index_); |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 72 | return std::make_unique<Subnet>( |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 73 | subnet_addr, prefix_length_, |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 74 | base::Bind(&SubnetPool::Release, weak_ptr_factory_.GetWeakPtr(), index)); |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 77 | void SubnetPool::Release(uint32_t index) { |
Garrick Evans | 53a2a98 | 2020-02-05 10:53:35 +0900 | [diff] [blame] | 78 | if (index == 0) { |
| 79 | LOG(DFATAL) << "Invalid index value: 0"; |
| 80 | return; |
| 81 | } |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 82 | DCHECK(subnets_.test(index)); |
| 83 | subnets_.reset(index); |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 86 | } // namespace patchpanel |