diff --git a/gns3server/modules/qemu/qemu_vm.py b/gns3server/modules/qemu/qemu_vm.py index 996451a5..2b9c44fc 100644 --- a/gns3server/modules/qemu/qemu_vm.py +++ b/gns3server/modules/qemu/qemu_vm.py @@ -40,6 +40,8 @@ from ..base_vm import BaseVM from ...schemas.qemu import QEMU_OBJECT_SCHEMA, QEMU_PLATFORMS from ...utils.asyncio import monitor_process from ...utils.images import md5sum +from ...utils import macaddress_to_int, int_to_macaddress + import logging log = logging.getLogger(__name__) @@ -1374,7 +1376,7 @@ class QemuVM(BaseVM): patched_qemu = True for adapter_number, adapter in enumerate(self._ethernet_adapters): - mac = "%s%02x" % (self._mac_address[:-2], (int(self._mac_address[-2:]) + adapter_number) % 255) + mac = int_to_macaddress(macaddress_to_int(self._mac_address) + adapter_number) nio = adapter.get_nio(0) if self._legacy_networking: # legacy QEMU networking syntax (-net) diff --git a/gns3server/utils/__init__.py b/gns3server/utils/__init__.py index 753df140..fb58158b 100644 --- a/gns3server/utils/__init__.py +++ b/gns3server/utils/__init__.py @@ -16,6 +16,7 @@ # along with this program. If not, see . +import textwrap import posixpath @@ -26,3 +27,20 @@ def force_unix_path(path): path = path.replace("\\", "/") return posixpath.normpath(path) + + +def macaddress_to_int(macaddress): + """ + Convert a macaddress with the format 00:0c:29:11:b0:0a to a int + + :param macaddress: The mac address + :returns: Integer + """ + return int(macaddress.replace(":", ""), 16) + + +def int_to_macaddress(integer): + """ + Convert an integer to a macaddress + """ + return ":".join(textwrap.wrap("%012x" % (integer), width=2)) diff --git a/tests/modules/qemu/test_qemu_vm.py b/tests/modules/qemu/test_qemu_vm.py index 209f270c..33b069af 100644 --- a/tests/modules/qemu/test_qemu_vm.py +++ b/tests/modules/qemu/test_qemu_vm.py @@ -419,6 +419,49 @@ def test_build_command_without_display(vm, loop, fake_qemu_binary): assert "-nographic" in cmd +def test_build_command_two_adapters(vm, loop, fake_qemu_binary, port_manager): + + vm.adapters = 2 + with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: + cmd = loop.run_until_complete(asyncio.async(vm._build_command())) + assert cmd == [ + fake_qemu_binary, + "-name", + "test", + "-m", + "256M", + "-smp", + "cpus=1", + "-boot", + "order=c", + "-serial", + "telnet:127.0.0.1:{},server,nowait".format(vm.console), + "-net", + "none", + "-device", + "e1000,mac=00:00:ab:0e:0f:00", + "-device", + "e1000,mac=00:00:ab:0e:0f:01", + "-nographic" + ] + + +def test_build_command_two_adapters_mac_address(vm, loop, fake_qemu_binary, port_manager): + + vm.adapters = 2 + vm.mac_address = "00:00:ab:0e:0f:09" + with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: + cmd = loop.run_until_complete(asyncio.async(vm._build_command())) + assert "e1000,mac=00:00:ab:0e:0f:09" in cmd + assert "e1000,mac=00:00:ab:0e:0f:0a" in cmd + + vm.mac_address = "00:00:ab:0e:0f:0a" + with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: + cmd = loop.run_until_complete(asyncio.async(vm._build_command())) + assert "e1000,mac=00:00:ab:0e:0f:0a" in cmd + assert "e1000,mac=00:00:ab:0e:0f:0b" in cmd + + # Windows accept this kind of mistake @pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows") def test_build_command_with_invalid_options(vm, loop, fake_qemu_binary): diff --git a/tests/test_utils.py b/tests/test_utils.py index 33af1a0b..567bcccc 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -16,10 +16,18 @@ # along with this program. If not, see . -from gns3server.utils import force_unix_path +from gns3server.utils import force_unix_path, macaddress_to_int, int_to_macaddress def test_force_unix_path(): assert force_unix_path("a/b") == "a/b" assert force_unix_path("a\\b") == "a/b" assert force_unix_path("a\\b\\..\\c") == "a/c" + + +def test_macaddress_to_int(): + assert macaddress_to_int("00:0c:29:11:b0:0a") == 52228632586 + + +def test_int_to_macaddress(): + assert int_to_macaddress(52228632586) == "00:0c:29:11:b0:0a"