-
Notifications
You must be signed in to change notification settings - Fork 736
Subnet refactor #4398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Parent:
Availability Zones
Sploder12
wants to merge
27
commits into
availability-zones
Choose a base branch
from
az-networking-refactor
base: availability-zones
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Subnet refactor #4398
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
cb91676
[network] Subnet class API
Sploder12 f87733d
[network] Add Subnet class tests
Sploder12 3b0d639
[network] Implement Subnet class
Sploder12 b6681b4
[util] Move int rng to MP_UTILS
Sploder12 d41ecd7
[network] Add subnet contains
Sploder12 18c2273
[network] Implement generate random subnet
Sploder12 2fa5365
[network] Make local subnet check robust
Sploder12 637b2c3
[network] Replace existing subnet code
Sploder12 a60f38b
[firewall] Update Linux firewall subnet usage
Sploder12 7ba9f38
[network] Implement virtual_switch_subnet on Linux
Sploder12 32d8ddf
[az] Use Subnet for AZs
Sploder12 3d7d3d2
[az] Replace remaining string subnets
Sploder12 7833d00
[network] Remove virtual_switch_subnet
Sploder12 064261c
[az] Mock subnet generating in tests
Sploder12 bc1a93d
[az] Fix Formatting
Sploder12 e6e4c21
[az] Fix make_qemu_platform on MacOS
Sploder12 2abb5de
[network] Add C++20 spaceship todos
Sploder12 380fa6c
[network] Add missing subnet coverage
Sploder12 66a2f6c
[network] Use warn instead of log
Sploder12 12f324f
[network] Use strong type for prefix len
Sploder12 604ac65
[network] Add format specialization for subnet
Sploder12 6d06c05
[network] Rename subnet network address
Sploder12 b933e4b
[network] Simplify get_subnet_mask
Sploder12 6133343
[util] Use long long for random int
Sploder12 f433397
[network] Rename random_subnet_from_range
Sploder12 90fdd28
[az] Fixup rebase
Sploder12 6b837ce
[network] Use C++20 three-way comparison
Sploder12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /* | ||
| * Copyright (C) Canonical, Ltd. | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; version 3. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| * | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <multipass/exceptions/formatted_exception_base.h> | ||
| #include <multipass/path.h> | ||
| #include <multipass/singleton.h> | ||
|
|
||
| #include "ip_address.h" | ||
|
|
||
| #define MP_SUBNET_UTILS multipass::SubnetUtils::instance() | ||
|
|
||
| namespace multipass | ||
| { | ||
| class Subnet | ||
| { | ||
| public: | ||
| struct PrefixLengthOutOfRange final : FormattedExceptionBase<std::out_of_range> | ||
| { | ||
| template <class T> | ||
| explicit PrefixLengthOutOfRange(const T& value) | ||
| : FormattedExceptionBase{ | ||
| "Subnet prefix length must be non-negative and less than 31: {}", | ||
| value} | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| class PrefixLength | ||
| { | ||
| public: | ||
| constexpr PrefixLength(uint8_t value) : value(value) | ||
| { | ||
| if (value >= 31) | ||
| throw PrefixLengthOutOfRange{value}; | ||
| } | ||
|
|
||
| constexpr operator uint8_t() const noexcept | ||
| { | ||
| return value; | ||
| } | ||
|
|
||
| private: | ||
| uint8_t value; | ||
| }; | ||
|
|
||
| Subnet(IPAddress ip, PrefixLength prefix_length); | ||
|
|
||
| Subnet(const std::string& cidr_string); | ||
xmkg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| [[nodiscard]] IPAddress min_address() const; | ||
| [[nodiscard]] IPAddress max_address() const; | ||
| [[nodiscard]] uint32_t usable_address_count() const; | ||
|
|
||
| [[nodiscard]] IPAddress network_address() const; | ||
| [[nodiscard]] PrefixLength prefix_length() const; | ||
| [[nodiscard]] IPAddress subnet_mask() const; | ||
|
|
||
| [[nodiscard]] std::string to_cidr() const; | ||
|
|
||
| // Subnets are either disjoint or the smaller is a subset of the larger | ||
| [[nodiscard]] bool contains(Subnet other) const; | ||
| [[nodiscard]] bool contains(IPAddress ip) const; | ||
|
|
||
| [[nodiscard]] std::strong_ordering operator<=>(const Subnet& other) const; | ||
| [[nodiscard]] bool operator==(const Subnet& other) const = default; | ||
|
|
||
| private: | ||
| IPAddress address; | ||
| PrefixLength prefix; | ||
| }; | ||
|
|
||
| struct SubnetUtils : Singleton<SubnetUtils> | ||
| { | ||
| using Singleton<SubnetUtils>::Singleton; | ||
|
|
||
| [[nodiscard]] virtual Subnet random_subnet_from_range(Subnet::PrefixLength prefix = 24, | ||
| Subnet range = Subnet{ | ||
| "10.0.0.0/8"}) const; | ||
| }; | ||
| } // namespace multipass | ||
|
|
||
| namespace fmt | ||
| { | ||
| template <> | ||
| struct formatter<multipass::Subnet> | ||
| { | ||
| template <typename ParseContext> | ||
| constexpr auto parse(ParseContext& ctx) | ||
| { | ||
| return ctx.begin(); | ||
| } | ||
|
|
||
| template <typename FormatContext> | ||
| auto format(const multipass::Subnet& subnet, FormatContext& ctx) const | ||
| { | ||
| return format_to(ctx.out(), "{}", subnet.to_cidr()); | ||
| } | ||
| }; | ||
|
|
||
| template <> | ||
| struct formatter<multipass::Subnet::PrefixLength> | ||
| { | ||
| template <typename ParseContext> | ||
| constexpr auto parse(ParseContext& ctx) | ||
| { | ||
| return ctx.begin(); | ||
| } | ||
|
|
||
| template <typename FormatContext> | ||
| auto format(const multipass::Subnet::PrefixLength& prefix, FormatContext& ctx) const | ||
| { | ||
| return format_to(ctx.out(), "{}", uint8_t(prefix)); | ||
| } | ||
| }; | ||
| } // namespace fmt | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -257,6 +257,8 @@ class Utils : public Singleton<Utils> | |
| virtual Path default_mount_target(const Path& source) const; | ||
| virtual Path normalize_mount_target(Path target_mount_path) const; | ||
| virtual bool invalid_target_path(const Path& target_path) const; // needs normalized input path | ||
|
|
||
| virtual long long random_int(long long a, long long b) const; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe [[nodiscard]] ? |
||
| }; | ||
| } // namespace multipass | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.