blob: a3441d03cc5060983e12238fc35f23f1712dfcf7 [file] [log] [blame]
Chirantan Ekbote1977ea22017-12-08 18:57:03 -08001// 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 Evans3388a032020-03-24 11:25:55 +09005#ifndef PATCHPANEL_SUBNET_POOL_H_
6#define PATCHPANEL_SUBNET_POOL_H_
Chirantan Ekbote1977ea22017-12-08 18:57:03 -08007
8#include <stdint.h>
9
10#include <bitset>
11#include <memory>
Chirantan Ekbote1977ea22017-12-08 18:57:03 -080012
13#include <base/callback.h>
14#include <base/macros.h>
15#include <base/memory/weak_ptr.h>
Garrick Evans4b66f632019-01-24 15:09:16 +090016#include <brillo/brillo_export.h>
Chirantan Ekbote1977ea22017-12-08 18:57:03 -080017
Garrick Evans3388a032020-03-24 11:25:55 +090018#include "patchpanel/subnet.h"
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070019
Garrick Evans3388a032020-03-24 11:25:55 +090020namespace patchpanel {
Garrick Evans53a2a982020-02-05 10:53:35 +090021constexpr uint32_t kAnySubnetIndex = 0;
22constexpr uint32_t kMaxSubnets = 32;
Chirantan Ekbote1977ea22017-12-08 18:57:03 -080023
Garrick Evans0dbd4182019-03-07 08:38:38 +090024// Manages up to 32 IPv4 subnets that can be assigned to guest interfaces.
Stephen Barber47981a72018-01-25 18:45:14 -080025// These use non-publicly routable addresses in the range 100.115.92.0/24.
Garrick Evans4b66f632019-01-24 15:09:16 +090026class BRILLO_EXPORT SubnetPool {
Chirantan Ekbote1977ea22017-12-08 18:57:03 -080027 public:
Garrick Evans0dbd4182019-03-07 08:38:38 +090028 // Returns a new pool or nullptr if num_subnets exceeds 32.
Hugo Benichi6c63ae22019-05-29 11:19:15 +090029 // |base_addr| must be in network-byte order.
Garrick Evans0dbd4182019-03-07 08:38:38 +090030 static std::unique_ptr<SubnetPool> New(uint32_t base_addr,
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090031 uint32_t prefix_length,
Garrick Evans0dbd4182019-03-07 08:38:38 +090032 uint32_t num_subnets);
Chirantan Ekbote1977ea22017-12-08 18:57:03 -080033 ~SubnetPool();
34
Garrick Evans0dbd4182019-03-07 08:38:38 +090035 // Allocates and returns a new subnet or nullptr if none are available.
Garrick Evans53a2a982020-02-05 10:53:35 +090036 // |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 Ekbote1977ea22017-12-08 18:57:03 -080039
40 private:
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090041 SubnetPool(uint32_t base_addr, uint32_t prefix_length, uint32_t num_subnets);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090042 SubnetPool(const SubnetPool&) = delete;
43 SubnetPool& operator=(const SubnetPool&) = delete;
Chirantan Ekbote1977ea22017-12-08 18:57:03 -080044
Stephen Barber47981a72018-01-25 18:45:14 -080045 // Called by Subnets on destruction to free a given subnet.
Garrick Evans0dbd4182019-03-07 08:38:38 +090046 void Release(uint32_t index);
Stephen Barber47981a72018-01-25 18:45:14 -080047
Garrick Evans0dbd4182019-03-07 08:38:38 +090048 const uint32_t base_addr_;
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090049 const uint32_t prefix_length_;
Garrick Evans0dbd4182019-03-07 08:38:38 +090050 const uint32_t num_subnets_;
51 const uint32_t addr_per_index_;
Garrick Evans53a2a982020-02-05 10:53:35 +090052 std::bitset<kMaxSubnets + 1> subnets_;
Chirantan Ekbote1977ea22017-12-08 18:57:03 -080053
54 base::WeakPtrFactory<SubnetPool> weak_ptr_factory_{this};
Chirantan Ekbote1977ea22017-12-08 18:57:03 -080055};
56
Garrick Evans3388a032020-03-24 11:25:55 +090057} // namespace patchpanel
Chirantan Ekbote1977ea22017-12-08 18:57:03 -080058
Garrick Evans3388a032020-03-24 11:25:55 +090059#endif // PATCHPANEL_SUBNET_POOL_H_