1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-28 11:18:11 +00:00

Merge pull request #1129 from GNS3/filters_virtualbox

Packet filters support for VirtualBox
This commit is contained in:
Jeremy Grossmann 2017-07-19 13:34:48 +07:00 committed by GitHub
commit ac10ba370a
6 changed files with 88 additions and 14 deletions

View File

@ -94,6 +94,10 @@ class VirtualBoxVM(BaseNode):
json["node_directory"] = None json["node_directory"] = None
return json return json
@property
def ethernet_adapters(self):
return self._ethernet_adapters
@asyncio.coroutine @asyncio.coroutine
def _get_system_properties(self): def _get_system_properties(self):
@ -973,15 +977,38 @@ class VirtualBoxVM(BaseNode):
self._local_udp_tunnels[adapter_number][1], self._local_udp_tunnels[adapter_number][1],
nio) nio)
except KeyError: except KeyError:
raise VirtualBoxError("Adapter {adapter_number} doesn't exist on VirtualBox VM '{name}'".format(name=self.name, raise VirtualBoxError("Adapter {adapter_number} doesn't exist on VirtualBox VM '{name}'".format(
adapter_number=adapter_number)) name=self.name,
adapter_number=adapter_number))
yield from self._control_vm("setlinkstate{} on".format(adapter_number + 1)) yield from self._control_vm("setlinkstate{} on".format(adapter_number + 1))
adapter.add_nio(0, nio) adapter.add_nio(0, nio)
log.info("VirtualBox VM '{name}' [{id}]: {nio} added to adapter {adapter_number}".format(name=self.name, log.info("VirtualBox VM '{name}' [{id}]: {nio} added to adapter {adapter_number}".format(
id=self.id, name=self.name,
nio=nio, id=self.id,
adapter_number=adapter_number)) nio=nio,
adapter_number=adapter_number))
@asyncio.coroutine
def adapter_update_nio_binding(self, adapter_number, nio):
"""
Update a port NIO binding.
:param adapter_number: adapter number
:param nio: NIO instance to add to the adapter
"""
if self.is_running():
try:
yield from self.update_ubridge_udp_connection(
"VBOX-{}-{}".format(self._id, adapter_number),
self._local_udp_tunnels[adapter_number][1],
nio)
except IndexError:
raise VirtualBoxError('Adapter {adapter_number} does not exist on VirtualBox VM "{name}"'.format(
name=self._name,
adapter_number=adapter_number
))
@asyncio.coroutine @asyncio.coroutine
def adapter_remove_nio_binding(self, adapter_number): def adapter_remove_nio_binding(self, adapter_number):

View File

@ -371,6 +371,7 @@ class Link:
'iou', 'iou',
'cloud', 'cloud',
'nat', 'nat',
'virtualbox',
'docker'): 'docker'):
return node["node"] return node["node"]
return None return None

View File

@ -746,11 +746,11 @@ class Project:
yield from self.add_node(compute, name, node_id, dump=False, **node) yield from self.add_node(compute, name, node_id, dump=False, **node)
for link_data in topology.get("links", []): for link_data in topology.get("links", []):
link = yield from self.add_link(link_id=link_data["link_id"]) link = yield from self.add_link(link_id=link_data["link_id"])
if "filters" in link_data:
yield from link.update_filters(link_data["filters"])
for node_link in link_data["nodes"]: for node_link in link_data["nodes"]:
node = self.get_node(node_link["node_id"]) node = self.get_node(node_link["node_id"])
yield from link.add_node(node, node_link["adapter_number"], node_link["port_number"], label=node_link.get("label"), dump=False) yield from link.add_node(node, node_link["adapter_number"], node_link["port_number"], label=node_link.get("label"), dump=False)
if "filters" in link_data:
yield from link.update_filters(link_data["filters"])
for drawing_data in topology.get("drawings", []): for drawing_data in topology.get("drawings", []):
yield from self.add_drawing(dump=False, **drawing_data) yield from self.add_drawing(dump=False, **drawing_data)

View File

@ -294,7 +294,7 @@ class QEMUHandler:
nio = vm.ethernet_adapters[int(request.match_info["adapter_number"])] nio = vm.ethernet_adapters[int(request.match_info["adapter_number"])]
if "filters" in request.json and nio: if "filters" in request.json and nio:
nio.filters = request.json["filters"] nio.filters = request.json["filters"]
yield from vm.adapter_update_nio_binding(int(request.match_info["port_number"]), nio) yield from vm.adapter_update_nio_binding(int(request.match_info["adapter_number"]), nio)
response.set_status(201) response.set_status(201)
response.json(request.json) response.json(request.json)

View File

@ -290,6 +290,33 @@ class VirtualBoxHandler:
response.set_status(201) response.set_status(201)
response.json(nio) response.json(nio)
@Route.put(
r"/projects/{project_id}/virtualbox/nodes/{node_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio",
parameters={
"project_id": "Project UUID",
"node_id": "Node UUID",
"adapter_number": "Network adapter where the nio is located",
"port_number": "Port from where the nio should be updated"
},
status_codes={
201: "NIO updated",
400: "Invalid request",
404: "Instance doesn't exist"
},
input=NIO_SCHEMA,
output=NIO_SCHEMA,
description="Update a NIO from a Virtualbox instance")
def update_nio(request, response):
virtualbox_manager = VirtualBox.instance()
vm = virtualbox_manager.get_node(request.match_info["node_id"], project_id=request.match_info["project_id"])
nio = vm.ethernet_adapters[int(request.match_info["adapter_number"])]
if "filters" in request.json and nio:
nio.filters = request.json["filters"]
yield from vm.adapter_update_nio_binding(int(request.match_info["adapter_number"]), nio)
response.set_status(201)
response.json(request.json)
@Route.delete( @Route.delete(
r"/projects/{project_id}/virtualbox/nodes/{node_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio", r"/projects/{project_id}/virtualbox/nodes/{node_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio",
parameters={ parameters={

View File

@ -26,9 +26,10 @@ def vm(http_compute, project, monkeypatch):
vboxmanage_path = "/fake/VboxManage" vboxmanage_path = "/fake/VboxManage"
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True) as mock: with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True) as mock:
response = http_compute.post("/projects/{project_id}/virtualbox/nodes".format(project_id=project.id), {"name": "VMTEST", response = http_compute.post("/projects/{project_id}/virtualbox/nodes".format(
"vmname": "VMTEST", project_id=project.id), {"name": "VMTEST",
"linked_clone": False}) "vmname": "VMTEST",
"linked_clone": False})
assert mock.called assert mock.called
assert response.status == 201 assert response.status == 201
@ -40,8 +41,8 @@ def test_vbox_create(http_compute, project):
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True): with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True):
response = http_compute.post("/projects/{project_id}/virtualbox/nodes".format(project_id=project.id), {"name": "VM1", response = http_compute.post("/projects/{project_id}/virtualbox/nodes".format(project_id=project.id), {"name": "VM1",
"vmname": "VM1", "vmname": "VM1",
"linked_clone": False}, "linked_clone": False},
example=True) example=True)
assert response.status == 201 assert response.status == 201
assert response.json["name"] == "VM1" assert response.json["name"] == "VM1"
@ -111,6 +112,24 @@ def test_vbox_nio_create_udp(http_compute, vm):
assert response.json["type"] == "nio_udp" assert response.json["type"] == "nio_udp"
def test_virtualbox_nio_update_udp(http_compute, vm):
with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.ethernet_adapters'):
with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock:
response = http_compute.put("/projects/{project_id}/virtualbox/nodes/{node_id}/adapters/0/ports/0/nio".format(
project_id=vm["project_id"],
node_id=vm["node_id"]),
{
"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1",
"filters": {}},
example=True)
assert response.status == 201, response.body.decode()
assert response.route == "/projects/{project_id}/virtualbox/nodes/{node_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_udp"
def test_vbox_delete_nio(http_compute, vm): def test_vbox_delete_nio(http_compute, vm):
with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock: with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock: