From 26f7195288b60977621545564ad510a13733a760 Mon Sep 17 00:00:00 2001 From: grossmj Date: Sun, 15 Feb 2015 17:45:53 -0700 Subject: [PATCH] Dynamips devices packet capture. --- .../handlers/dynamips_device_handler.py | 91 +++++++++---------- .../modules/dynamips/nodes/atm_switch.py | 12 +-- .../modules/dynamips/nodes/ethernet_hub.py | 12 +-- .../modules/dynamips/nodes/ethernet_switch.py | 12 +-- .../dynamips/nodes/frame_relay_switch.py | 12 +-- gns3server/schemas/dynamips_device.py | 2 +- 6 files changed, 68 insertions(+), 73 deletions(-) diff --git a/gns3server/handlers/dynamips_device_handler.py b/gns3server/handlers/dynamips_device_handler.py index fb5f284e..11c3392c 100644 --- a/gns3server/handlers/dynamips_device_handler.py +++ b/gns3server/handlers/dynamips_device_handler.py @@ -15,7 +15,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . - +import os import asyncio from ..web.route import Route from ..schemas.dynamips_device import DEVICE_CREATE_SCHEMA @@ -184,51 +184,46 @@ class DynamipsDeviceHandler: yield from device.remove_nio(port_number) response.set_status(204) - # @Route.post( - # r"/projects/{project_id}/dynamips/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/start_capture", - # parameters={ - # "project_id": "UUID for the project", - # "vm_id": "UUID for the instance", - # "adapter_number": "Adapter to start a packet capture", - # "port_number": "Port on the adapter" - # }, - # status_codes={ - # 200: "Capture started", - # 400: "Invalid request", - # 404: "Instance doesn't exist" - # }, - # description="Start a packet capture on a Dynamips VM instance", - # input=VM_CAPTURE_SCHEMA) - # def start_capture(request, response): - # - # dynamips_manager = Dynamips.instance() - # vm = dynamips_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"]) - # slot_number = int(request.match_info["adapter_number"]) - # port_number = int(request.match_info["port_number"]) - # pcap_file_path = os.path.join(vm.project.capture_working_directory(), request.json["capture_file_name"]) - # yield from vm.start_capture(slot_number, port_number, pcap_file_path, request.json["data_link_type"]) - # response.json({"pcap_file_path": pcap_file_path}) - # - # @Route.post( - # r"/projects/{project_id}/dynamips/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/stop_capture", - # parameters={ - # "project_id": "UUID for the project", - # "vm_id": "UUID for the instance", - # "adapter_number": "Adapter to stop a packet capture", - # "port_number": "Port on the adapter (always 0)" - # }, - # status_codes={ - # 204: "Capture stopped", - # 400: "Invalid request", - # 404: "Instance doesn't exist" - # }, - # description="Stop a packet capture on a Dynamips VM instance") - # def start_capture(request, response): - # - # dynamips_manager = Dynamips.instance() - # vm = dynamips_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"]) - # slot_number = int(request.match_info["adapter_number"]) - # port_number = int(request.match_info["port_number"]) - # yield from vm.stop_capture(slot_number, port_number) - # response.set_status(204) + @Route.post( + r"/projects/{project_id}/dynamips/devices/{device_id}/ports/{port_number:\d+}/start_capture", + parameters={ + "project_id": "UUID for the project", + "device_id": "UUID for the instance", + "port_number": "Port on the device" + }, + status_codes={ + 200: "Capture started", + 400: "Invalid request", + 404: "Instance doesn't exist" + }, + description="Start a packet capture on a Dynamips device instance", + input=DEVICE_CAPTURE_SCHEMA) + def start_capture(request, response): + dynamips_manager = Dynamips.instance() + device = dynamips_manager.get_device(request.match_info["device_id"], project_id=request.match_info["project_id"]) + port_number = int(request.match_info["port_number"]) + pcap_file_path = os.path.join(device.project.capture_working_directory(), request.json["capture_file_name"]) + yield from device.start_capture(port_number, pcap_file_path, request.json["data_link_type"]) + response.json({"pcap_file_path": pcap_file_path}) + + @Route.post( + r"/projects/{project_id}/dynamips/devices/{device_id}/ports/{port_number:\d+}/stop_capture", + parameters={ + "project_id": "UUID for the project", + "device_id": "UUID for the instance", + "port_number": "Port on the device" + }, + status_codes={ + 204: "Capture stopped", + 400: "Invalid request", + 404: "Instance doesn't exist" + }, + description="Stop a packet capture on a Dynamips device instance") + def start_capture(request, response): + + dynamips_manager = Dynamips.instance() + device = dynamips_manager.get_device(request.match_info["device_id"], project_id=request.match_info["project_id"]) + port_number = int(request.match_info["port_number"]) + yield from device.stop_capture(port_number) + response.set_status(204) diff --git a/gns3server/modules/dynamips/nodes/atm_switch.py b/gns3server/modules/dynamips/nodes/atm_switch.py index 07094479..0416fa08 100644 --- a/gns3server/modules/dynamips/nodes/atm_switch.py +++ b/gns3server/modules/dynamips/nodes/atm_switch.py @@ -368,9 +368,9 @@ class ATMSwitch(Device): yield from nio.bind_filter("both", "capture") yield from nio.setup_filter("both", "{} {}".format(data_link_type, output_file)) - log.info('ATM switch "{name}" [{id}]: starting packet capture on {port}'.format(name=self._name, - id=self._id, - port=port_number)) + log.info('ATM switch "{name}" [{id}]: starting packet capture on port {port}'.format(name=self._name, + id=self._id, + port=port_number)) @asyncio.coroutine def stop_capture(self, port_number): @@ -385,6 +385,6 @@ class ATMSwitch(Device): nio = self._nios[port_number] yield from nio.unbind_filter("both") - log.info('ATM switch "{name}" [{id}]: stopping packet capture on {port}'.format(name=self._name, - id=self._id, - port=port_number)) + log.info('ATM switch "{name}" [{id}]: stopping packet capture on port {port}'.format(name=self._name, + id=self._id, + port=port_number)) diff --git a/gns3server/modules/dynamips/nodes/ethernet_hub.py b/gns3server/modules/dynamips/nodes/ethernet_hub.py index 806b8d3f..33c523ab 100644 --- a/gns3server/modules/dynamips/nodes/ethernet_hub.py +++ b/gns3server/modules/dynamips/nodes/ethernet_hub.py @@ -149,9 +149,9 @@ class EthernetHub(Bridge): yield from nio.bind_filter("both", "capture") yield from nio.setup_filter("both", "{} {}".format(data_link_type, output_file)) - log.info('Ethernet hub "{name}" [{id}]: starting packet capture on {port}'.format(name=self._name, - id=self._id, - port=port_number)) + log.info('Ethernet hub "{name}" [{id}]: starting packet capture on port {port}'.format(name=self._name, + id=self._id, + port=port_number)) @asyncio.coroutine def stop_capture(self, port_number): @@ -166,6 +166,6 @@ class EthernetHub(Bridge): nio = self._mappings[port_number] yield from nio.unbind_filter("both") - log.info('Ethernet hub "{name}" [{id}]: stopping packet capture on {port}'.format(name=self._name, - id=self._id, - port=port_number)) + log.info('Ethernet hub "{name}" [{id}]: stopping packet capture on port {port}'.format(name=self._name, + id=self._id, + port=port_number)) diff --git a/gns3server/modules/dynamips/nodes/ethernet_switch.py b/gns3server/modules/dynamips/nodes/ethernet_switch.py index bf919068..65fe07c1 100644 --- a/gns3server/modules/dynamips/nodes/ethernet_switch.py +++ b/gns3server/modules/dynamips/nodes/ethernet_switch.py @@ -298,9 +298,9 @@ class EthernetSwitch(Device): yield from nio.bind_filter("both", "capture") yield from nio.setup_filter("both", "{} {}".format(data_link_type, output_file)) - log.info('Ethernet switch "{name}" [{id}]: starting packet capture on {port}'.format(name=self._name, - id=self._id, - port=port_number)) + log.info('Ethernet switch "{name}" [{id}]: starting packet capture on port {port}'.format(name=self._name, + id=self._id, + port=port_number)) @asyncio.coroutine def stop_capture(self, port_number): @@ -315,6 +315,6 @@ class EthernetSwitch(Device): nio = self._nios[port_number] yield from nio.unbind_filter("both") - log.info('Ethernet switch "{name}" [{id}]: stopping packet capture on {port}'.format(name=self._name, - id=self._id, - port=port_number)) + log.info('Ethernet switch "{name}" [{id}]: stopping packet capture on port {port}'.format(name=self._name, + id=self._id, + port=port_number)) diff --git a/gns3server/modules/dynamips/nodes/frame_relay_switch.py b/gns3server/modules/dynamips/nodes/frame_relay_switch.py index 301bd732..67d2813d 100644 --- a/gns3server/modules/dynamips/nodes/frame_relay_switch.py +++ b/gns3server/modules/dynamips/nodes/frame_relay_switch.py @@ -272,9 +272,9 @@ class FrameRelaySwitch(Device): yield from nio.bind_filter("both", "capture") yield from nio.setup_filter("both", "{} {}".format(data_link_type, output_file)) - log.info('Frame relay switch "{name}" [{id}]: starting packet capture on {port}'.format(name=self._name, - id=self._id, - port=port_number)) + log.info('Frame relay switch "{name}" [{id}]: starting packet capture on port {port}'.format(name=self._name, + id=self._id, + port=port_number)) @asyncio.coroutine def stop_capture(self, port_number): @@ -289,6 +289,6 @@ class FrameRelaySwitch(Device): nio = self._nios[port_number] yield from nio.unbind_filter("both") - log.info('Frame relay switch "{name}" [{id}]: stopping packet capture on {port}'.format(name=self._name, - id=self._id, - port=port_number)) + log.info('Frame relay switch "{name}" [{id}]: stopping packet capture on port {port}'.format(name=self._name, + id=self._id, + port=port_number)) diff --git a/gns3server/schemas/dynamips_device.py b/gns3server/schemas/dynamips_device.py index 4e44afb4..28ba32be 100644 --- a/gns3server/schemas/dynamips_device.py +++ b/gns3server/schemas/dynamips_device.py @@ -337,5 +337,5 @@ DEVICE_CAPTURE_SCHEMA = { }, }, "additionalProperties": False, - "required": ["capture_file_name"] + "required": ["capture_file_name", "data_link_type"] }