Method for start / stop capture on a link

Ref https://github.com/GNS3/gns3-gui/issues/1117
This commit is contained in:
Julien Duponchelle committed 2016-04-21 12:14:09 +02:00
1 parent 78a9785819
commit 04a1b2df3b
9 files changed
+230 -18

No files matched your search

+33 -2
View File
@@ -15,16 +15,18 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
import uuid
import asyncio
class Link:
def __init__(self, project):
def __init__(self, project, data_link_type="DLT_EN10MB"):
self._id = str(uuid.uuid4())
self._vms = []
self._project = project
self._data_link_type = data_link_type
@asyncio.coroutine
def addVM(self, vm, adapter_number, port_number):
@@ -51,6 +53,35 @@ class Link:
"""
raise NotImplementedError
@asyncio.coroutine
def start_capture(self):
"""
Start capture on the link
:returns: Capture object
"""
raise NotImplementedError
@asyncio.coroutine
def stop_capture(self):
"""
Stop capture on the link
"""
raise NotImplementedError
def capture_file_name(self):
"""
:returns: File name for a capture on this link
"""
capture_file_name = "{}_{}-{}_to_{}_{}-{}".format(
self._vms[0]["vm"].name,
self._vms[0]["adapter_number"],
self._vms[0]["port_number"],
self._vms[1]["vm"].name,
self._vms[1]["adapter_number"],
self._vms[1]["port_number"])
return re.sub("[^0-9A-Za-z_-]", "", capture_file_name) + ".pcap"
@property
def id(self):
return self._id
@@ -63,4 +94,4 @@ class Link:
"adapter_number": side["adapter_number"],
"port_number": side["port_number"]
})
return {"vms": res, "link_id": self._id}
return {"vms": res, "link_id": self._id, "data_link_type": self._data_link_type}
+46 -2
View File
@@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import asyncio
import aiohttp
from .link import Link
@@ -23,8 +24,9 @@ from .link import Link
class UDPLink(Link):
def __init__(self, project):
super().__init__(project)
def __init__(self, project, data_link_type="DLT_EN10MB"):
super().__init__(project, data_link_type)
self._capture_vm = None
@asyncio.coroutine
def create(self):
@@ -76,3 +78,45 @@ class UDPLink(Link):
yield from vm1.delete("/adapters/{adapter_number}/ports/{port_number}/nio".format(adapter_number=adapter_number1, port_number=port_number1))
yield from vm2.delete("/adapters/{adapter_number}/ports/{port_number}/nio".format(adapter_number=adapter_number2, port_number=port_number2))
@asyncio.coroutine
def start_capture(self):
"""
Start capture on a link
"""
self._capture_vm = self._choose_capture_side()
data = {
"capture_file_name": self.capture_file_name(),
"data_link_type": self._data_link_type
}
yield from self._capture_vm["vm"].post("/adapters/{adapter_number}/ports/{port_number}/start_capture".format(adapter_number=self._capture_vm["adapter_number"], port_number=self._capture_vm["port_number"]), data=data)
@asyncio.coroutine
def stop_capture(self):
"""
Stop capture on a link
"""
if self._capture_vm:
yield from self._capture_vm["vm"].post("/adapters/{adapter_number}/ports/{port_number}/stop_capture".format(adapter_number=self._capture_vm["adapter_number"], port_number=self._capture_vm["port_number"]))
self._capture_vm = None
def _choose_capture_side(self):
"""
Run capture on the best candidate.
The ideal candidate is a node who support capture on controller
server
:returns: VM where the capture should run
"""
# For saving bandwith we use the local node first
for vm in self._vms:
if vm["vm"].compute.id == "local" and vm["vm"].vm_type not in ["qemu", "vpcs"]:
return vm
for vm in self._vms:
if vm["vm"].vm_type not in ["qemu", "vpcs"]:
return vm
raise aiohttp.web.HTTPConflict(text="Capture is not supported for this link")
+3
View File
@@ -216,6 +216,9 @@ class VM:
else:
return (yield from self._compute.delete("/projects/{}/{}/vms/{}{}".format(self._project.id, self._vm_type, self._id, path)))
def __repr__(self):
return "<gns3server.controller.VM {} {}>".format(self._vm_type, self._name)
def __json__(self):
return {
"compute_id": self._compute.id,