From 58a19af9ac3ee04ee87bd9cec2ebd9e7de4c0ecd Mon Sep 17 00:00:00 2001 From: grossmj Date: Tue, 2 Jun 2020 18:45:22 +0930 Subject: [PATCH] Support to activate/deactive network connection state replication in Qemu. --- gns3server/compute/qemu/qemu_vm.py | 46 +++++++++++++++++++++++------ gns3server/schemas/qemu.py | 13 ++++++++ gns3server/schemas/qemu_template.py | 5 ++++ 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/gns3server/compute/qemu/qemu_vm.py b/gns3server/compute/qemu/qemu_vm.py index 6b0a61fa..0e7387e5 100644 --- a/gns3server/compute/qemu/qemu_vm.py +++ b/gns3server/compute/qemu/qemu_vm.py @@ -117,6 +117,7 @@ class QemuVM(BaseNode): self._kernel_image = "" self._kernel_command_line = "" self._legacy_networking = False + self._replicate_network_connection_state = True self._on_close = "power_off" self._cpu_throttling = 0 # means no CPU throttling self._process_priority = "low" @@ -615,6 +616,30 @@ class QemuVM(BaseNode): log.info('QEMU VM "{name}" [{id}] has disabled legacy networking'.format(name=self._name, id=self._id)) self._legacy_networking = legacy_networking + @property + def replicate_network_connection_state(self): + """ + Returns whether the network connection state for links is replicated in QEMU. + + :returns: boolean + """ + + return self._replicate_network_connection_state + + @replicate_network_connection_state.setter + def replicate_network_connection_state(self, replicate_network_connection_state): + """ + Sets whether the network connection state for links is replicated in QEMU + + :param replicate_network_connection_state: boolean + """ + + if replicate_network_connection_state: + log.info('QEMU VM "{name}" [{id}] has enabled network connection state replication'.format(name=self._name, id=self._id)) + else: + log.info('QEMU VM "{name}" [{id}] has disabled network connection state replication'.format(name=self._name, id=self._id)) + self._replicate_network_connection_state = replicate_network_connection_state + @property def on_close(self): """ @@ -1004,12 +1029,12 @@ class QemuVM(BaseNode): await self.add_ubridge_udp_connection("QEMU-{}-{}".format(self._id, adapter_number), self._local_udp_tunnels[adapter_number][1], nio) - if nio.suspend: + if nio.suspend and self._replicate_network_connection_state: set_link_commands.append("set_link gns3-{} off".format(adapter_number)) - else: + elif self._replicate_network_connection_state: set_link_commands.append("set_link gns3-{} off".format(adapter_number)) - if "-loadvm" not in command_string: + if "-loadvm" not in command_string and self._replicate_network_connection_state: # only set the link statuses if not restoring a previous VM state await self._control_vm_commands(set_link_commands) @@ -1293,7 +1318,8 @@ class QemuVM(BaseNode): await self.add_ubridge_udp_connection("QEMU-{}-{}".format(self._id, adapter_number), self._local_udp_tunnels[adapter_number][1], nio) - await self._control_vm("set_link gns3-{} on".format(adapter_number)) + if self._replicate_network_connection_state: + await self._control_vm("set_link gns3-{} on".format(adapter_number)) except (IndexError, KeyError): raise QemuError('Adapter {adapter_number} does not exist on QEMU VM "{name}"'.format(name=self._name, adapter_number=adapter_number)) @@ -1317,10 +1343,11 @@ class QemuVM(BaseNode): await self.update_ubridge_udp_connection("QEMU-{}-{}".format(self._id, adapter_number), self._local_udp_tunnels[adapter_number][1], nio) - if nio.suspend: - await self._control_vm("set_link gns3-{} off".format(adapter_number)) - else: - await self._control_vm("set_link gns3-{} on".format(adapter_number)) + if self._replicate_network_connection_state: + if nio.suspend: + await self._control_vm("set_link gns3-{} off".format(adapter_number)) + else: + await self._control_vm("set_link gns3-{} on".format(adapter_number)) except IndexError: raise QemuError('Adapter {adapter_number} does not exist on QEMU VM "{name}"'.format(name=self._name, adapter_number=adapter_number)) @@ -1342,7 +1369,8 @@ class QemuVM(BaseNode): await self.stop_capture(adapter_number) if self.is_running(): - await self._control_vm("set_link gns3-{} off".format(adapter_number)) + if self._replicate_network_connection_state: + await self._control_vm("set_link gns3-{} off".format(adapter_number)) await self._ubridge_send("bridge delete {name}".format(name="QEMU-{}-{}".format(self._id, adapter_number))) nio = adapter.get_nio(0) diff --git a/gns3server/schemas/qemu.py b/gns3server/schemas/qemu.py index e3e7a1d6..567e5c3f 100644 --- a/gns3server/schemas/qemu.py +++ b/gns3server/schemas/qemu.py @@ -186,6 +186,10 @@ QEMU_CREATE_SCHEMA = { "description": "Use QEMU legagy networking commands (-net syntax)", "type": ["boolean", "null"], }, + "replicate_network_connection_state": { + "description": "Replicate the network connection state for links in Qemu", + "type": ["boolean", "null"], + }, "on_close": { "description": "Action to execute on the VM is closed", "enum": ["power_off", "shutdown_signal", "save_vm_state"], @@ -372,6 +376,10 @@ QEMU_UPDATE_SCHEMA = { "description": "Use QEMU legagy networking commands (-net syntax)", "type": ["boolean", "null"], }, + "replicate_network_connection_state": { + "description": "Replicate the network connection state for links in Qemu", + "type": ["boolean", "null"], + }, "on_close": { "description": "Action to execute on the VM is closed", "enum": ["power_off", "shutdown_signal", "save_vm_state"], @@ -571,6 +579,10 @@ QEMU_OBJECT_SCHEMA = { "description": "Use QEMU legagy networking commands (-net syntax)", "type": "boolean", }, + "replicate_network_connection_state": { + "description": "Replicate the network connection state for links in Qemu", + "type": "boolean", + }, "on_close": { "description": "Action to execute on the VM is closed", "enum": ["power_off", "shutdown_signal", "save_vm_state"], @@ -640,6 +652,7 @@ QEMU_OBJECT_SCHEMA = { "kernel_image_md5sum", "kernel_command_line", "legacy_networking", + "replicate_network_connection_state", "on_close", "cpu_throttling", "process_priority", diff --git a/gns3server/schemas/qemu_template.py b/gns3server/schemas/qemu_template.py index 18e2a284..f98c81d7 100644 --- a/gns3server/schemas/qemu_template.py +++ b/gns3server/schemas/qemu_template.py @@ -178,6 +178,11 @@ QEMU_TEMPLATE_PROPERTIES = { "type": "boolean", "default": False }, + "replicate_network_connection_state": { + "description": "Replicate the network connection state for links in Qemu", + "type": "boolean", + "default": True + }, "on_close": { "description": "Action to execute on the VM is closed", "enum": ["power_off", "shutdown_signal", "save_vm_state"],