Skip to content
Open
Show file tree
Hide file tree
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 Sep 22, 2025
f87733d
[network] Add Subnet class tests
Sploder12 Sep 22, 2025
3b0d639
[network] Implement Subnet class
Sploder12 Sep 22, 2025
b6681b4
[util] Move int rng to MP_UTILS
Sploder12 Sep 23, 2025
d41ecd7
[network] Add subnet contains
Sploder12 Sep 23, 2025
18c2273
[network] Implement generate random subnet
Sploder12 Sep 23, 2025
2fa5365
[network] Make local subnet check robust
Sploder12 Sep 24, 2025
637b2c3
[network] Replace existing subnet code
Sploder12 Sep 25, 2025
a60f38b
[firewall] Update Linux firewall subnet usage
Sploder12 Sep 26, 2025
7ba9f38
[network] Implement virtual_switch_subnet on Linux
Sploder12 Sep 26, 2025
32d8ddf
[az] Use Subnet for AZs
Sploder12 Sep 29, 2025
3d7d3d2
[az] Replace remaining string subnets
Sploder12 Sep 29, 2025
7833d00
[network] Remove virtual_switch_subnet
Sploder12 Sep 29, 2025
064261c
[az] Mock subnet generating in tests
Sploder12 Sep 30, 2025
bc1a93d
[az] Fix Formatting
Sploder12 Sep 30, 2025
e6e4c21
[az] Fix make_qemu_platform on MacOS
Sploder12 Sep 30, 2025
2abb5de
[network] Add C++20 spaceship todos
Sploder12 Sep 30, 2025
380fa6c
[network] Add missing subnet coverage
Sploder12 Sep 30, 2025
66a2f6c
[network] Use warn instead of log
Sploder12 Sep 30, 2025
12f324f
[network] Use strong type for prefix len
Sploder12 Oct 1, 2025
604ac65
[network] Add format specialization for subnet
Sploder12 Oct 1, 2025
6d06c05
[network] Rename subnet network address
Sploder12 Oct 2, 2025
b933e4b
[network] Simplify get_subnet_mask
Sploder12 Oct 2, 2025
6133343
[util] Use long long for random int
Sploder12 Oct 2, 2025
f433397
[network] Rename random_subnet_from_range
Sploder12 Oct 2, 2025
90fdd28
[az] Fixup rebase
Sploder12 Oct 22, 2025
6b837ce
[network] Use C++20 three-way comparison
Sploder12 Oct 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/multipass/availability_zone.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define MULTIPASS_AVAILABILITY_ZONE_H

#include "disabled_copy_move.h"
#include "subnet.h"
#include "virtual_machine.h"

#include <memory>
Expand All @@ -34,7 +35,7 @@ class AvailabilityZone : private DisabledCopyMove
virtual ~AvailabilityZone() = default;

[[nodiscard]] virtual const std::string& get_name() const = 0;
[[nodiscard]] virtual const std::string& get_subnet() const = 0;
[[nodiscard]] virtual const Subnet& get_subnet() const = 0;
[[nodiscard]] virtual bool is_available() const = 0;
virtual void set_available(bool new_available) = 0;
virtual void add_vm(VirtualMachine& vm) = 0;
Expand Down
4 changes: 3 additions & 1 deletion include/multipass/availability_zone_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ class AvailabilityZoneManager : private DisabledCopyMove
using UPtr = std::unique_ptr<AvailabilityZoneManager>;
using ShPtr = std::shared_ptr<AvailabilityZoneManager>;

using Zones = std::vector<std::reference_wrapper<const AvailabilityZone>>;

virtual ~AvailabilityZoneManager() = default;

virtual AvailabilityZone& get_zone(const std::string& name) = 0;
virtual std::vector<std::reference_wrapper<const AvailabilityZone>> get_zones() = 0;
virtual Zones get_zones() = 0;
// this returns a computed zone name, using an algorithm e.g. round-robin
// not to be confused with [get_default_zone_name]
virtual std::string get_automatic_zone_name() = 0;
Expand Down
4 changes: 2 additions & 2 deletions include/multipass/base_availability_zone.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class BaseAvailabilityZone : public AvailabilityZone
BaseAvailabilityZone(const std::string& name, const std::filesystem::path& az_directory);

const std::string& get_name() const override;
const std::string& get_subnet() const override;
const Subnet& get_subnet() const override;
bool is_available() const override;
void set_available(bool new_available) override;
void add_vm(VirtualMachine& vm) override;
Expand All @@ -48,7 +48,7 @@ class BaseAvailabilityZone : public AvailabilityZone
{
const std::string name{};
const std::filesystem::path file_path{};
const std::string subnet{};
const Subnet subnet;
bool available{};
std::vector<std::reference_wrapper<VirtualMachine>> vms{};
// we don't have designated initializers, so mutex remains last so it doesn't need to be
Expand Down
7 changes: 1 addition & 6 deletions include/multipass/ip_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@
std::string as_string() const;
uint32_t as_uint32() const;

bool operator==(const IPAddress& other) const;
bool operator!=(const IPAddress& other) const;
bool operator<(const IPAddress& other) const;
bool operator<=(const IPAddress& other) const;
bool operator>(const IPAddress& other) const;
bool operator>=(const IPAddress& other) const;
auto operator<=>(const IPAddress& other) const = default;

Check failure on line 38 in include/multipass/ip_address.h

View workflow job for this annotation

GitHub Actions / Windows/macOS / BuildAndTest (macos-13, x86_64, mono)

explicitly defaulted three-way comparison operator is implicitly deleted [-Werror,-Wdefaulted-function-deleted]

Check failure on line 38 in include/multipass/ip_address.h

View workflow job for this annotation

GitHub Actions / Windows/macOS / BuildAndTest (macos-13, x86_64, mono)

explicitly defaulted three-way comparison operator is implicitly deleted [-Werror,-Wdefaulted-function-deleted]

Check failure on line 38 in include/multipass/ip_address.h

View workflow job for this annotation

GitHub Actions / Windows/macOS / BuildAndTest (macos-13, x86_64, mono)

explicitly defaulted three-way comparison operator is implicitly deleted [-Werror,-Wdefaulted-function-deleted]
IPAddress operator+(int value) const;

std::array<uint8_t, 4> octets;
Expand Down
3 changes: 3 additions & 0 deletions include/multipass/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <multipass/settings/setting_spec.h>
#include <multipass/singleton.h>
#include <multipass/sshfs_server_config.h>
#include <multipass/subnet.h>
#include <multipass/update_prompt.h>
#include <multipass/virtual_machine_factory.h>
#include <multipass/vm_image_vault.h>
Expand Down Expand Up @@ -77,6 +78,8 @@ class Platform : public Singleton<Platform>
virtual QString default_driver() const;
virtual QString default_privileged_mounts() const;
[[nodiscard]] virtual std::string bridge_nomenclature() const;
[[nodiscard]] virtual bool can_reach_gateway(IPAddress ip) const;
[[nodiscard]] virtual bool subnet_used_locally(Subnet subnet) const;
virtual int get_cpus() const;
virtual long long get_total_ram() const;

Expand Down
131 changes: 131 additions & 0 deletions include/multipass/subnet.h
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);

[[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
2 changes: 2 additions & 0 deletions include/multipass/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe [[nodiscard]] ?

};
} // namespace multipass

Expand Down
1 change: 1 addition & 0 deletions src/network/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
set(CMAKE_AUTOMOC ON)

add_library(network STATIC
subnet.cpp
url_downloader.cpp)

add_library(ip_address STATIC
Expand Down
30 changes: 0 additions & 30 deletions src/network/ip_address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,36 +88,6 @@ uint32_t mp::IPAddress::as_uint32() const
return value;
}

bool mp::IPAddress::operator==(const IPAddress& other) const
{
return octets == other.octets;
}

bool mp::IPAddress::operator!=(const IPAddress& other) const
{
return octets != other.octets;
}

bool mp::IPAddress::operator<(const IPAddress& other) const
{
return as_uint32() < other.as_uint32();
}

bool mp::IPAddress::operator<=(const IPAddress& other) const
{
return as_uint32() <= other.as_uint32();
}

bool mp::IPAddress::operator>(const IPAddress& other) const
{
return as_uint32() > other.as_uint32();
}

bool mp::IPAddress::operator>=(const IPAddress& other) const
{
return as_uint32() >= other.as_uint32();
}

mp::IPAddress mp::IPAddress::operator+(int value) const
{
return mp::IPAddress(as_uint32() + value);
Expand Down
Loading
Loading