Skip to content

Commit

Permalink
Merge pull request #3266 from canonical/qemu-macos-add-bridged-interf…
Browse files Browse the repository at this point in the history
…aces

Add bridged interfaces in macOS QEMU
  • Loading branch information
luis4a0 committed Nov 10, 2023
2 parents 0b048bd + 0bac4d4 commit d5411be
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3227,14 +3227,21 @@ void mp::Daemon::configure_new_interfaces(const std::string& name, mp::VirtualMa

auto& commands = run_at_boot[name];

for (const auto i : new_interfaces)
try
{
vm.add_network_interface(i, specs.extra_interfaces[i]);
for (const auto i : new_interfaces)
{
vm.add_network_interface(i, specs.extra_interfaces[i]);

commands.push_back(generate_netplan_script(i, specs.extra_interfaces[i].mac_address));
}
commands.push_back(generate_netplan_script(i, specs.extra_interfaces[i].mac_address));
}

commands.push_back("sudo netplan apply");
commands.push_back("sudo netplan apply");
}
catch (const mp::NotImplementedOnThisBackendException&)

Check warning on line 3241 in src/daemon/daemon.cpp

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L3241

Added line #L3241 was not covered by tests
{
run_at_boot.erase(name);

Check warning on line 3243 in src/daemon/daemon.cpp

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L3243

Added line #L3243 was not covered by tests
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/platform/backends/qemu/linux/qemu_platform_detail.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class QemuPlatformDetail : public QemuPlatform
void remove_resources_for(const std::string& name) override;
void platform_health_check() override;
QStringList vm_platform_args(const VirtualMachineDescription& vm_desc) override;
void add_network_interface(VirtualMachineDescription& desc, const NetworkInterface& net) override;

private:
const QString bridge_name;
Expand Down
11 changes: 11 additions & 0 deletions src/platform/backends/qemu/linux/qemu_platform_detail_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ QStringList mp::QemuPlatformDetail::vm_platform_args(const VirtualMachineDescrip
return opts;
}

void mp::QemuPlatformDetail::add_network_interface(VirtualMachineDescription& desc, const NetworkInterface& net)
{
// TODO: Do not uncomment the following line when implementing bridging in Linux QEMU. Since implementing it would
// yield the same exact implementation than in macOS, we need to move the code out of here, and put it only once
// in src/platform/backends/qemu/qemu_virtual_machine.[h|cpp].
// desc.extra_interfaces.push_back(net);

// For the time being, just complain:
throw NotImplementedOnThisBackendException("add interfaces");
}

mp::QemuPlatform::UPtr mp::QemuPlatformFactory::make_qemu_platform(const Path& data_dir) const
{
return std::make_unique<mp::QemuPlatformDetail>(data_dir);
Expand Down
1 change: 1 addition & 0 deletions src/platform/backends/qemu/qemu_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class QemuPlatform : private DisabledCopyMove
{
throw NotImplementedOnThisBackendException("networks");
};
virtual void add_network_interface(VirtualMachineDescription& desc, const NetworkInterface& net) = 0;

protected:
explicit QemuPlatform() = default;
Expand Down
5 changes: 5 additions & 0 deletions src/platform/backends/qemu/qemu_virtual_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,11 @@ void mp::QemuVirtualMachine::resize_disk(const MemorySize& new_size)
desc.disk_space = new_size;
}

void mp::QemuVirtualMachine::add_network_interface(int /* not used on this backend */, const NetworkInterface& net)

Check warning on line 612 in src/platform/backends/qemu/qemu_virtual_machine.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/backends/qemu/qemu_virtual_machine.cpp#L612

Added line #L612 was not covered by tests
{
return qemu_platform->add_network_interface(desc, net);

Check warning on line 614 in src/platform/backends/qemu/qemu_virtual_machine.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/backends/qemu/qemu_virtual_machine.cpp#L614

Added line #L614 was not covered by tests
}

mp::MountHandler::UPtr mp::QemuVirtualMachine::make_native_mount_handler(const SSHKeyProvider* ssh_key_provider,
const std::string& target,
const VMMount& mount)
Expand Down
2 changes: 2 additions & 0 deletions src/platform/backends/qemu/qemu_virtual_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <shared/base_virtual_machine.h>

#include <multipass/network_interface.h>
#include <multipass/process/process.h>
#include <multipass/virtual_machine_description.h>

Expand Down Expand Up @@ -63,6 +64,7 @@ class QemuVirtualMachine : public QObject, public BaseVirtualMachine
void update_cpus(int num_cores) override;
void resize_memory(const MemorySize& new_size) override;
void resize_disk(const MemorySize& new_size) override;
virtual void add_network_interface(int index, const NetworkInterface& net) override;
virtual MountArgs& modifiable_mount_args();
std::unique_ptr<MountHandler> make_native_mount_handler(const SSHKeyProvider* ssh_key_provider,
const std::string& target, const VMMount& mount) override;
Expand Down
9 changes: 9 additions & 0 deletions tests/qemu/linux/test_qemu_platform_detail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,12 @@ TEST_F(QemuPlatformDetail, writing_ipforward_file_failure_logs_expected_message)

mp::QemuPlatformDetail qemu_platform_detail{data_dir.path()};
}

TEST_F(QemuPlatformDetail, add_network_interface_throws)
{
mp::QemuPlatformDetail qemu_platform_detail{data_dir.path()};

mp::VirtualMachineDescription desc;
mp::NetworkInterface net{"id", "52:54:00:98:76:54", true};
EXPECT_THROW(qemu_platform_detail.add_network_interface(desc, net), mp::NotImplementedOnThisBackendException);
}
1 change: 1 addition & 0 deletions tests/qemu/mock_qemu_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct MockQemuPlatform : public QemuPlatform
MOCK_METHOD(QStringList, vmstate_platform_args, (), (override));
MOCK_METHOD(QStringList, vm_platform_args, (const VirtualMachineDescription&), (override));
MOCK_METHOD(QString, get_directory_name, (), (const, override));
MOCK_METHOD(void, add_network_interface, (VirtualMachineDescription&, const NetworkInterface&), (override));
};

struct MockQemuPlatformFactory : public QemuPlatformFactory
Expand Down

0 comments on commit d5411be

Please sign in to comment.