blob: 542ca36c37c11d60bb398e67474f4476b36cbb2b [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 Evans4b66f632019-01-24 15:09:16 +09005#ifndef ARC_NETWORK_SUBNET_POOL_H_
6#define ARC_NETWORK_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 Evans4b66f632019-01-24 15:09:16 +090018#include "arc/network/subnet.h"
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070019
Garrick Evans4b66f632019-01-24 15:09:16 +090020namespace arc_networkd {
Chirantan Ekbote1977ea22017-12-08 18:57:03 -080021
Garrick Evans0dbd4182019-03-07 08:38:38 +090022// Manages up to 32 IPv4 subnets that can be assigned to guest interfaces.
Stephen Barber47981a72018-01-25 18:45:14 -080023// These use non-publicly routable addresses in the range 100.115.92.0/24.
Garrick Evans4b66f632019-01-24 15:09:16 +090024class BRILLO_EXPORT SubnetPool {
Chirantan Ekbote1977ea22017-12-08 18:57:03 -080025 public:
Garrick Evans0dbd4182019-03-07 08:38:38 +090026 // Returns a new pool or nullptr if num_subnets exceeds 32.
27 static std::unique_ptr<SubnetPool> New(uint32_t base_addr,
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090028 uint32_t prefix_length,
Garrick Evans0dbd4182019-03-07 08:38:38 +090029 uint32_t num_subnets);
Chirantan Ekbote1977ea22017-12-08 18:57:03 -080030 ~SubnetPool();
31
Garrick Evans0dbd4182019-03-07 08:38:38 +090032 // Allocates and returns a new subnet or nullptr if none are available.
33 std::unique_ptr<Subnet> Allocate();
Chirantan Ekbote1977ea22017-12-08 18:57:03 -080034
35 private:
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090036 SubnetPool(uint32_t base_addr, uint32_t prefix_length, uint32_t num_subnets);
Chirantan Ekbote1977ea22017-12-08 18:57:03 -080037
Stephen Barber47981a72018-01-25 18:45:14 -080038 // Called by Subnets on destruction to free a given subnet.
Garrick Evans0dbd4182019-03-07 08:38:38 +090039 void Release(uint32_t index);
Stephen Barber47981a72018-01-25 18:45:14 -080040
Garrick Evans0dbd4182019-03-07 08:38:38 +090041 const uint32_t base_addr_;
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090042 const uint32_t prefix_length_;
Garrick Evans0dbd4182019-03-07 08:38:38 +090043 const uint32_t num_subnets_;
44 const uint32_t addr_per_index_;
45 std::bitset<32> subnets_;
Chirantan Ekbote1977ea22017-12-08 18:57:03 -080046
47 base::WeakPtrFactory<SubnetPool> weak_ptr_factory_{this};
48 DISALLOW_COPY_AND_ASSIGN(SubnetPool);
49};
50
Garrick Evans4b66f632019-01-24 15:09:16 +090051} // namespace arc_networkd
Chirantan Ekbote1977ea22017-12-08 18:57:03 -080052
Garrick Evans4b66f632019-01-24 15:09:16 +090053#endif // ARC_NETWORK_SUBNET_POOL_H_