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 | |
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 | 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 | namespace { |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 22 | constexpr uint32_t kMaxSubnets = 32; |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 23 | } // namespace |
| 24 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 25 | // static |
| 26 | std::unique_ptr<SubnetPool> SubnetPool::New(uint32_t base_addr, |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 27 | uint32_t prefix_length, |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 28 | uint32_t num_subnets) { |
| 29 | if (num_subnets > kMaxSubnets) { |
| 30 | LOG(ERROR) << "Maximum subnets supported is " << kMaxSubnets << "; got " |
| 31 | << num_subnets; |
| 32 | return nullptr; |
| 33 | } |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 34 | return base::WrapUnique( |
| 35 | new SubnetPool(base_addr, prefix_length, num_subnets)); |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | SubnetPool::SubnetPool(uint32_t base_addr, |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 39 | uint32_t prefix_length, |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 40 | uint32_t num_subnets) |
| 41 | : base_addr_(base_addr), |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 42 | prefix_length_(prefix_length), |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 43 | num_subnets_(num_subnets), |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 44 | addr_per_index_(1ull << (kMaxSubnets - prefix_length)) {} |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 45 | |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 46 | SubnetPool::~SubnetPool() { |
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 | 43b4e2d | 2019-12-11 13:43:08 +0900 | [diff] [blame^] | 52 | std::unique_ptr<Subnet> SubnetPool::Allocate(int index) { |
| 53 | if (index < 0) { |
| 54 | index = 0; |
| 55 | // Find the first un-allocated subnet. |
| 56 | while (index < num_subnets_ && subnets_.test(index)) { |
| 57 | ++index; |
| 58 | } |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Garrick Evans | 43b4e2d | 2019-12-11 13:43:08 +0900 | [diff] [blame^] | 61 | if (index >= num_subnets_ || subnets_.test(index)) { |
| 62 | // No applicable subnet available. |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 63 | return nullptr; |
| 64 | } |
| 65 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 66 | subnets_.set(index); |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 67 | uint32_t subnet_addr = htonl(ntohl(base_addr_) + index * addr_per_index_); |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 68 | return std::make_unique<Subnet>( |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 69 | subnet_addr, prefix_length_, |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 70 | base::Bind(&SubnetPool::Release, weak_ptr_factory_.GetWeakPtr(), index)); |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 73 | void SubnetPool::Release(uint32_t index) { |
| 74 | DCHECK(subnets_.test(index)); |
| 75 | subnets_.reset(index); |
Chirantan Ekbote | 1977ea2 | 2017-12-08 18:57:03 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Garrick Evans | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 78 | } // namespace arc_networkd |