1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-29 10:18:10 +00:00
gns3-server/gns3server/modules/dynamips/nios/nio.py

257 lines
8.2 KiB
Python
Raw Normal View History

2013-12-22 00:42:33 +00:00
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Base interface for Dynamips Network Input/Output (NIO) module ("nio").
http://github.com/GNS3/dynamips/blob/master/README.hypervisor#L451
"""
2015-02-10 01:24:13 +00:00
import asyncio
2013-12-22 00:42:33 +00:00
from ..dynamips_error import DynamipsError
import logging
log = logging.getLogger(__name__)
2013-12-22 00:42:33 +00:00
2015-02-10 01:24:13 +00:00
class NIO:
2015-02-13 13:43:28 +00:00
2013-12-22 00:42:33 +00:00
"""
Base NIO class
2014-02-28 04:50:46 +00:00
:param hypervisor: Dynamips hypervisor instance
2013-12-22 00:42:33 +00:00
"""
2015-02-10 01:24:13 +00:00
def __init__(self, name, hypervisor):
2013-12-22 00:42:33 +00:00
self._hypervisor = hypervisor
2015-02-13 22:11:14 +00:00
self._name = name
2013-12-22 00:42:33 +00:00
self._bandwidth = None # no bandwidth constraint by default
self._input_filter = None # no input filter applied by default
self._output_filter = None # no output filter applied by default
self._input_filter_options = None # no input filter options by default
self._output_filter_options = None # no output filter options by default
self._dynamips_direction = {"in": 0, "out": 1, "both": 2}
2015-02-10 01:24:13 +00:00
@asyncio.coroutine
2013-12-22 00:42:33 +00:00
def list(self):
"""
Returns all NIOs.
:returns: NIO list
"""
2015-02-10 01:24:13 +00:00
nio_list = yield from self._hypervisor.send("nio list")
return nio_list
2013-12-22 00:42:33 +00:00
2015-02-10 01:24:13 +00:00
@asyncio.coroutine
2013-12-22 00:42:33 +00:00
def delete(self):
"""
Deletes this NIO.
"""
if self._input_filter or self._output_filter:
2015-02-10 01:24:13 +00:00
yield from self.unbind_filter("both")
yield from self._hypervisor.send("nio delete {}".format(self._name))
log.info("NIO {name} has been deleted".format(name=self._name))
2013-12-22 00:42:33 +00:00
2015-02-10 01:24:13 +00:00
@asyncio.coroutine
2013-12-22 00:42:33 +00:00
def rename(self, new_name):
"""
Renames this NIO
:param new_name: new NIO name
"""
2015-02-10 01:24:13 +00:00
yield from self._hypervisor.send("nio rename {name} {new_name}".format(name=self._name, new_name=new_name))
2015-02-10 01:24:13 +00:00
log.info("NIO {name} renamed to {new_name}".format(name=self._name, new_name=new_name))
2013-12-22 00:42:33 +00:00
self._name = new_name
2015-02-10 01:24:13 +00:00
@asyncio.coroutine
2013-12-22 00:42:33 +00:00
def debug(self, debug):
"""
Enables/Disables debugging for this NIO.
:param debug: debug value (0 = disable, enable = 1)
"""
2015-02-10 01:24:13 +00:00
yield from self._hypervisor.send("nio set_debug {name} {debug}".format(name=self._name, debug=debug))
2013-12-22 00:42:33 +00:00
2015-02-10 01:24:13 +00:00
@asyncio.coroutine
2013-12-22 00:42:33 +00:00
def bind_filter(self, direction, filter_name):
"""
Adds a packet filter to this NIO.
Filter "freq_drop" drops packets.
Filter "capture" captures packets.
:param direction: "in", "out" or "both"
:param filter_name: name of the filter to apply
"""
if direction not in self._dynamips_direction:
raise DynamipsError("Unknown direction {} to bind filter {}:".format(direction, filter_name))
dynamips_direction = self._dynamips_direction[direction]
2015-02-10 01:24:13 +00:00
yield from self._hypervisor.send("nio bind_filter {name} {direction} {filter}".format(name=self._name,
direction=dynamips_direction,
filter=filter_name))
2013-12-22 00:42:33 +00:00
if direction == "in":
self._input_filter = filter_name
elif direction == "out":
self._output_filter = filter_name
elif direction == "both":
self._input_filter = filter_name
self._output_filter = filter_name
2015-02-10 01:24:13 +00:00
@asyncio.coroutine
2013-12-22 00:42:33 +00:00
def unbind_filter(self, direction):
"""
Removes packet filter for this NIO.
:param direction: "in", "out" or "both"
"""
if direction not in self._dynamips_direction:
raise DynamipsError("Unknown direction {} to unbind filter:".format(direction))
dynamips_direction = self._dynamips_direction[direction]
2015-02-10 01:24:13 +00:00
yield from self._hypervisor.send("nio unbind_filter {name} {direction}".format(name=self._name,
direction=dynamips_direction))
2013-12-22 00:42:33 +00:00
if direction == "in":
self._input_filter = None
elif direction == "out":
self._output_filter = None
elif direction == "both":
self._input_filter = None
self._output_filter = None
2015-02-10 01:24:13 +00:00
@asyncio.coroutine
2013-12-22 00:42:33 +00:00
def setup_filter(self, direction, options):
"""
Setups a packet filter bound with this NIO.
2013-12-22 00:42:33 +00:00
Filter "freq_drop" has 1 argument "<frequency>". It will drop
everything with a -1 frequency, drop every Nth packet with a
positive frequency, or drop nothing.
Filter "capture" has 2 arguments "<link_type_name> <output_file>".
It will capture packets to the target output file. The link type
name is a case-insensitive DLT_ name from the PCAP library
constants with the DLT_ part removed.See http://www.tcpdump.org/linktypes.html
for a list of all available DLT values.
:param direction: "in", "out" or "both"
:param options: options for the packet filter (string)
"""
if direction not in self._dynamips_direction:
raise DynamipsError("Unknown direction {} to setup filter:".format(direction))
dynamips_direction = self._dynamips_direction[direction]
2015-02-10 01:24:13 +00:00
yield from self._hypervisor.send("nio setup_filter {name} {direction} {options}".format(name=self._name,
direction=dynamips_direction,
options=options))
2013-12-22 00:42:33 +00:00
if direction == "in":
self._input_filter_options = options
elif direction == "out":
self._output_filter_options = options
elif direction == "both":
self._input_filter_options = options
self._output_filter_options = options
@property
def input_filter(self):
"""
Returns the input packet filter for this NIO.
:returns: tuple (filter name, filter options)
"""
2014-05-28 12:26:20 +00:00
return self._input_filter, self._input_filter_options
2013-12-22 00:42:33 +00:00
@property
def output_filter(self):
"""
Returns the output packet filter for this NIO.
:returns: tuple (filter name, filter options)
"""
2014-05-28 12:26:20 +00:00
return self._output_filter, self._output_filter_options
2013-12-22 00:42:33 +00:00
2015-02-10 01:24:13 +00:00
@asyncio.coroutine
2013-12-22 00:42:33 +00:00
def get_stats(self):
"""
Gets statistics for this NIO.
:returns: NIO statistics (string with packets in, packets out, bytes in, bytes out)
"""
2015-02-10 01:24:13 +00:00
stats = yield from self._hypervisor.send("nio get_stats {}".format(self._name))
return stats[0]
2013-12-22 00:42:33 +00:00
2015-02-10 01:24:13 +00:00
@asyncio.coroutine
2013-12-22 00:42:33 +00:00
def reset_stats(self):
"""
Resets statistics for this NIO.
"""
2015-02-10 01:24:13 +00:00
yield from self._hypervisor.send("nio reset_stats {}".format(self._name))
2013-12-22 00:42:33 +00:00
@property
def bandwidth(self):
"""
Returns the bandwidth constraint for this NIO.
:returns: bandwidth integer value (in Kb/s)
"""
return self._bandwidth
2015-02-10 01:24:13 +00:00
@asyncio.coroutine
def set_bandwidth(self, bandwidth):
"""
Sets bandwidth constraint.
:param bandwidth: bandwidth integer value (in Kb/s)
"""
yield from self._hypervisor.send("nio set_bandwidth {name} {bandwidth}".format(name=self._name, bandwidth=bandwidth))
self._bandwidth = bandwidth
2013-12-22 00:42:33 +00:00
def __str__(self):
"""
NIO string representation.
:returns: NIO name
"""
return self._name
@property
def name(self):
"""
Returns the NIO name.
:returns: NIO name
"""
return self._name