2015-02-14 03:01:18 +00:00
#
# 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/>.
"""
Interface for Dynamips virtual Ethernet switch module ( " ethsw " ) .
http : / / github . com / GNS3 / dynamips / blob / master / README . hypervisor #L558
"""
import asyncio
2016-05-02 15:13:23 +00:00
from gns3server . utils import parse_version
2019-03-20 08:23:30 +00:00
#from gns3server.utils.asyncio.embed_shell import EmbedShell, create_telnet_shell
2017-05-26 15:20:18 +00:00
2015-02-14 03:01:18 +00:00
from . device import Device
2015-02-24 02:00:34 +00:00
from . . nios . nio_udp import NIOUDP
2015-02-14 03:01:18 +00:00
from . . dynamips_error import DynamipsError
2016-09-29 14:01:18 +00:00
from . . . error import NodeError
2015-02-14 03:01:18 +00:00
import logging
log = logging . getLogger ( __name__ )
2019-03-20 08:23:30 +00:00
# class EthernetSwitchConsole(EmbedShell):
# """
# Console for the ethernet switch
# """
#
# def __init__(self, node):
# super().__init__(welcome_message="Welcome to GNS3 builtin Ethernet switch.\n\nType help for available commands\n")
# self._node = node
#
# async def mac(self):
# """
# Show MAC address table
# """
# res = 'Port Mac VLAN\n'
# result = (await self._node._hypervisor.send('ethsw show_mac_addr_table {}'.format(self._node.name)))
# for line in result:
# mac, vlan, nio = line.replace(' ', ' ').split(' ')
# mac = mac.replace('.', '')
# mac = "{}:{}:{}:{}:{}:{}".format(
# mac[0:2],
# mac[2:4],
# mac[4:6],
# mac[6:8],
# mac[8:10],
# mac[10:12])
# for port_number, switch_nio in self._node.nios.items():
# if switch_nio.name == nio:
# res += 'Ethernet' + str(port_number) + ' ' + mac + ' ' + vlan + '\n'
# break
# return res
2017-05-26 15:20:18 +00:00
2015-02-14 03:01:18 +00:00
class EthernetSwitch ( Device ) :
2015-02-23 16:21:39 +00:00
2015-02-14 03:01:18 +00:00
"""
Dynamips Ethernet switch .
: param name : name for this switch
2016-05-20 04:45:04 +00:00
: param node_id : Node instance identifier
2015-02-14 03:01:18 +00:00
: param project : Project instance
: param manager : Parent VM Manager
2016-05-20 04:45:04 +00:00
: param ports : initial switch ports
2015-02-14 03:01:18 +00:00
: param hypervisor : Dynamips hypervisor instance
"""
2021-03-01 03:25:52 +00:00
def __init__ ( self , name , node_id , project , manager , console = None , console_type = None , ports = None , hypervisor = None ) :
2015-02-14 03:01:18 +00:00
2016-05-20 04:45:04 +00:00
super ( ) . __init__ ( name , node_id , project , manager , hypervisor )
2015-02-14 03:01:18 +00:00
self . _nios = { }
2015-02-15 19:18:12 +00:00
self . _mappings = { }
2017-05-26 15:20:18 +00:00
self . _telnet_console = None
2019-03-20 08:23:30 +00:00
#self._telnet_shell = None
#self._telnet_server = None
2018-03-24 11:11:21 +00:00
self . _console = console
self . _console_type = console_type
2021-03-01 03:25:52 +00:00
if self . _console_type is None :
self . _console_type = " none "
2018-03-24 11:11:21 +00:00
if self . _console is not None :
self . _console = self . _manager . port_manager . reserve_tcp_port ( self . _console , self . _project )
else :
self . _console = self . _manager . port_manager . get_free_tcp_port ( self . _project )
2016-05-20 04:45:04 +00:00
if ports is None :
# create 8 ports by default
self . _ports = [ ]
2016-09-29 12:59:11 +00:00
for port_number in range ( 0 , 8 ) :
2016-05-20 04:45:04 +00:00
self . _ports . append ( { " port_number " : port_number ,
2021-04-13 09:07:58 +00:00
" name " : f " Ethernet { port_number } " ,
2016-05-20 04:45:04 +00:00
" type " : " access " ,
" vlan " : 1 } )
else :
self . _ports = ports
2015-02-15 19:18:12 +00:00
def __json__ ( self ) :
ethernet_switch_info = { " name " : self . name ,
2020-07-15 09:45:51 +00:00
" usage " : self . usage ,
2017-05-26 15:20:18 +00:00
" console " : self . console ,
2018-03-24 11:11:21 +00:00
" console_type " : self . console_type ,
2016-05-20 04:45:04 +00:00
" node_id " : self . id ,
" project_id " : self . project . id ,
2016-09-13 07:47:22 +00:00
" ports_mapping " : self . _ports ,
2016-05-20 04:45:04 +00:00
" status " : " started " }
2021-03-01 03:25:52 +00:00
2016-05-20 04:45:04 +00:00
return ethernet_switch_info
2017-05-26 15:20:18 +00:00
@property
def console ( self ) :
return self . _console
@console.setter
2018-03-24 11:11:21 +00:00
def console ( self , console ) :
self . _console = console
@property
def console_type ( self ) :
return self . _console_type
@console_type.setter
def console_type ( self , console_type ) :
2019-05-18 07:28:20 +00:00
2019-05-23 08:57:08 +00:00
if self . _console_type != console_type :
if console_type == " telnet " :
self . project . emit ( " log.warning " , {
2021-04-13 09:07:58 +00:00
" message " : f ' " { self . _name } " : Telnet access for switches is not available in this version of GNS3 ' } )
2019-05-23 08:57:08 +00:00
self . _console_type = console_type
2017-05-26 15:20:18 +00:00
2016-05-20 04:45:04 +00:00
@property
2016-09-29 14:01:18 +00:00
def ports_mapping ( self ) :
2016-05-20 04:45:04 +00:00
"""
Ports on this switch
2015-02-15 19:18:12 +00:00
2016-05-20 04:45:04 +00:00
: returns : ports info
"""
2015-02-15 19:18:12 +00:00
2016-05-20 04:45:04 +00:00
return self . _ports
2016-09-29 14:01:18 +00:00
@ports_mapping.setter
def ports_mapping ( self , ports ) :
2016-05-20 04:45:04 +00:00
"""
Set the ports on this switch
: param ports : ports info
"""
2016-09-29 14:01:18 +00:00
if ports != self . _ports :
2017-03-29 11:22:24 +00:00
if len ( self . _nios ) > 0 and len ( ports ) != len ( self . _ports ) :
2016-09-29 14:01:18 +00:00
raise NodeError ( " Can ' t modify a switch already connected. " )
2016-05-20 04:45:04 +00:00
2016-09-29 14:01:18 +00:00
port_number = 0
for port in ports :
2021-04-13 09:07:58 +00:00
port [ " name " ] = f " Ethernet { port_number } "
2016-09-29 14:01:18 +00:00
port [ " port_number " ] = port_number
port_number + = 1
self . _ports = ports
2015-02-14 03:01:18 +00:00
2018-10-15 10:05:49 +00:00
async def update_port_settings ( self ) :
2017-03-29 11:22:24 +00:00
for port_settings in self . _ports :
port_number = port_settings [ " port_number " ]
if port_number in self . _nios and self . _nios [ port_number ] is not None :
2018-10-15 10:05:49 +00:00
await self . set_port_settings ( port_number , port_settings )
2017-03-29 11:22:24 +00:00
2018-10-15 10:05:49 +00:00
async def create ( self ) :
2015-02-14 03:01:18 +00:00
if self . _hypervisor is None :
2015-02-16 05:13:24 +00:00
module_workdir = self . project . module_working_directory ( self . manager . module_name . lower ( ) )
2018-10-15 10:05:49 +00:00
self . _hypervisor = await self . manager . start_new_hypervisor ( working_dir = module_workdir )
2015-02-14 03:01:18 +00:00
2021-04-13 09:07:58 +00:00
await self . _hypervisor . send ( f ' ethsw create " { self . _name } " ' )
log . info ( f ' Ethernet switch " { self . _name } " [ { self . _id } ] has been created ' )
2017-05-26 15:20:18 +00:00
2019-03-20 08:23:30 +00:00
#self._telnet_shell = EthernetSwitchConsole(self)
#self._telnet_shell.prompt = self._name + '> '
#self._telnet = create_telnet_shell(self._telnet_shell)
#try:
# self._telnet_server = (await asyncio.start_server(self._telnet.run, self._manager.port_manager.console_host, self.console))
#except OSError as e:
# self.project.emit("log.warning", {"message": "Could not start Telnet server on socket {}:{}: {}".format(self._manager.port_manager.console_host, self.console, e)})
2019-05-18 07:28:20 +00:00
if self . _console_type == " telnet " :
2021-04-13 09:07:58 +00:00
self . project . emit ( " log.warning " , { " message " : f ' " { self . _name } " : Telnet access for switches is not available in this version of GNS3 ' } )
2015-02-14 03:01:18 +00:00
self . _hypervisor . devices . append ( self )
2018-10-15 10:05:49 +00:00
async def set_name ( self , new_name ) :
2015-02-14 03:01:18 +00:00
"""
Renames this Ethernet switch .
: param new_name : New name for this switch
"""
2021-04-13 09:07:58 +00:00
await self . _hypervisor . send ( f ' ethsw rename " { self . _name } " " { new_name } " ' )
2015-02-14 03:01:18 +00:00
log . info ( ' Ethernet switch " {name} " [ {id} ]: renamed to " {new_name} " ' . format ( name = self . _name ,
id = self . _id ,
new_name = new_name ) )
self . _name = new_name
@property
def nios ( self ) :
"""
Returns all the NIOs member of this Ethernet switch .
: returns : nio list
"""
return self . _nios
@property
2015-02-15 19:18:12 +00:00
def mappings ( self ) :
2015-02-14 03:01:18 +00:00
"""
2015-02-15 19:18:12 +00:00
Returns port mappings
2015-02-14 03:01:18 +00:00
2015-02-15 19:18:12 +00:00
: returns : mappings list
2015-02-14 03:01:18 +00:00
"""
2015-02-15 19:18:12 +00:00
return self . _mappings
2015-02-14 03:01:18 +00:00
2018-10-15 10:05:49 +00:00
async def delete ( self ) :
return ( await self . close ( ) )
2016-12-12 18:17:06 +00:00
2018-10-15 10:05:49 +00:00
async def close ( self ) :
2015-02-14 03:01:18 +00:00
"""
Deletes this Ethernet switch .
"""
2018-03-24 11:11:21 +00:00
2019-03-20 08:23:30 +00:00
#await self._telnet.close()
#if self._telnet_server:
# self._telnet_server.close()
2018-09-02 08:32:33 +00:00
2015-02-24 02:00:34 +00:00
for nio in self . _nios . values ( ) :
2017-07-10 18:38:28 +00:00
if nio :
2018-10-15 10:05:49 +00:00
await nio . close ( )
2017-05-26 15:20:18 +00:00
self . manager . port_manager . release_tcp_port ( self . _console , self . _project )
2016-12-12 18:17:06 +00:00
if self . _hypervisor :
try :
2021-04-13 09:07:58 +00:00
await self . _hypervisor . send ( f ' ethsw delete " { self . _name } " ' )
log . info ( f ' Ethernet switch " { self . _name } " [ { self . _id } ] has been deleted ' )
2016-12-12 18:17:06 +00:00
except DynamipsError :
2021-04-13 09:07:58 +00:00
log . debug ( f " Could not properly delete Ethernet switch { self . _name } " )
2015-02-25 18:52:52 +00:00
if self . _hypervisor and self in self . _hypervisor . devices :
self . _hypervisor . devices . remove ( self )
2015-02-15 19:18:12 +00:00
if self . _hypervisor and not self . _hypervisor . devices :
2018-10-15 10:05:49 +00:00
await self . hypervisor . stop ( )
2016-12-12 18:17:06 +00:00
self . _hypervisor = None
return True
2015-02-14 03:01:18 +00:00
2018-10-15 10:05:49 +00:00
async def add_nio ( self , nio , port_number ) :
2015-02-14 03:01:18 +00:00
"""
Adds a NIO as new port on Ethernet switch .
: param nio : NIO instance to add
: param port_number : port to allocate for the NIO
"""
if port_number in self . _nios :
2021-04-13 09:07:58 +00:00
raise DynamipsError ( f " Port { port_number } isn ' t free " )
2015-02-14 03:01:18 +00:00
2021-04-13 09:07:58 +00:00
await self . _hypervisor . send ( f ' ethsw add_nio " { self . _name } " { nio } ' )
2015-02-14 03:01:18 +00:00
log . info ( ' Ethernet switch " {name} " [ {id} ]: NIO {nio} bound to port {port} ' . format ( name = self . _name ,
id = self . _id ,
nio = nio ,
port = port_number ) )
self . _nios [ port_number ] = nio
2016-05-20 04:45:04 +00:00
for port_settings in self . _ports :
if port_settings [ " port_number " ] == port_number :
2018-10-15 10:05:49 +00:00
await self . set_port_settings ( port_number , port_settings )
2016-05-20 04:45:04 +00:00
break
2015-02-14 03:01:18 +00:00
2018-10-15 10:05:49 +00:00
async def remove_nio ( self , port_number ) :
2015-02-14 03:01:18 +00:00
"""
Removes the specified NIO as member of this Ethernet switch .
: param port_number : allocated port number
: returns : the NIO that was bound to the port
"""
if port_number not in self . _nios :
2021-04-13 09:07:58 +00:00
raise DynamipsError ( f " Port { port_number } is not allocated " )
2015-02-14 03:01:18 +00:00
2019-04-01 12:47:31 +00:00
await self . stop_capture ( port_number )
2015-02-14 03:01:18 +00:00
nio = self . _nios [ port_number ]
2015-02-24 02:00:34 +00:00
if isinstance ( nio , NIOUDP ) :
2015-03-21 23:19:12 +00:00
self . manager . port_manager . release_udp_port ( nio . lport , self . _project )
2017-02-20 11:19:38 +00:00
if self . _hypervisor :
2021-04-13 09:07:58 +00:00
await self . _hypervisor . send ( f ' ethsw remove_nio " { self . _name } " { nio } ' )
2015-02-14 03:01:18 +00:00
log . info ( ' Ethernet switch " {name} " [ {id} ]: NIO {nio} removed from port {port} ' . format ( name = self . _name ,
id = self . _id ,
nio = nio ,
port = port_number ) )
del self . _nios [ port_number ]
2015-02-15 19:18:12 +00:00
if port_number in self . _mappings :
del self . _mappings [ port_number ]
2015-02-14 03:01:18 +00:00
return nio
2018-10-27 07:47:17 +00:00
def get_nio ( self , port_number ) :
"""
Gets a port NIO binding .
: param port_number : port number
: returns : NIO instance
"""
if port_number not in self . _nios :
2021-04-13 09:07:58 +00:00
raise DynamipsError ( f " Port { port_number } is not allocated " )
2018-10-27 07:47:17 +00:00
nio = self . _nios [ port_number ]
if not nio :
2021-04-13 09:07:58 +00:00
raise DynamipsError ( f " Port { port_number } is not connected " )
2018-10-27 07:47:17 +00:00
return nio
2018-10-15 10:05:49 +00:00
async def set_port_settings ( self , port_number , settings ) :
2015-02-15 19:18:12 +00:00
"""
Applies port settings to a specific port .
: param port_number : port number to set the settings
: param settings : port settings
"""
if settings [ " type " ] == " access " :
2018-10-15 10:05:49 +00:00
await self . set_access_port ( port_number , settings [ " vlan " ] )
2015-02-15 19:18:12 +00:00
elif settings [ " type " ] == " dot1q " :
2018-10-15 10:05:49 +00:00
await self . set_dot1q_port ( port_number , settings [ " vlan " ] )
2015-02-15 19:18:12 +00:00
elif settings [ " type " ] == " qinq " :
2018-10-15 10:05:49 +00:00
await self . set_qinq_port ( port_number , settings [ " vlan " ] , settings . get ( " ethertype " ) )
2015-02-15 19:18:12 +00:00
2018-10-15 10:05:49 +00:00
async def set_access_port ( self , port_number , vlan_id ) :
2015-02-14 03:01:18 +00:00
"""
Sets the specified port as an ACCESS port .
: param port_number : allocated port number
: param vlan_id : VLAN number membership
"""
if port_number not in self . _nios :
2021-04-13 09:07:58 +00:00
raise DynamipsError ( f " Port { port_number } is not allocated " )
2015-02-14 03:01:18 +00:00
nio = self . _nios [ port_number ]
2018-10-15 10:05:49 +00:00
await self . _hypervisor . send ( ' ethsw set_access_port " {name} " {nio} {vlan_id} ' . format ( name = self . _name ,
2015-02-14 03:01:18 +00:00
nio = nio ,
vlan_id = vlan_id ) )
log . info ( ' Ethernet switch " {name} " [ {id} ]: port {port} set as an access port in VLAN {vlan_id} ' . format ( name = self . _name ,
id = self . _id ,
port = port_number ,
vlan_id = vlan_id ) )
2015-02-15 19:18:12 +00:00
self . _mappings [ port_number ] = ( " access " , vlan_id )
2015-02-14 03:01:18 +00:00
2018-10-15 10:05:49 +00:00
async def set_dot1q_port ( self , port_number , native_vlan ) :
2015-02-14 03:01:18 +00:00
"""
Sets the specified port as a 802.1 Q trunk port .
: param port_number : allocated port number
: param native_vlan : native VLAN for this trunk port
"""
if port_number not in self . _nios :
2021-04-13 09:07:58 +00:00
raise DynamipsError ( f " Port { port_number } is not allocated " )
2015-02-14 03:01:18 +00:00
nio = self . _nios [ port_number ]
2018-10-15 10:05:49 +00:00
await self . _hypervisor . send ( ' ethsw set_dot1q_port " {name} " {nio} {native_vlan} ' . format ( name = self . _name ,
2015-02-14 03:01:18 +00:00
nio = nio ,
native_vlan = native_vlan ) )
log . info ( ' Ethernet switch " {name} " [ {id} ]: port {port} set as a 802.1Q port with native VLAN {vlan_id} ' . format ( name = self . _name ,
id = self . _id ,
port = port_number ,
vlan_id = native_vlan ) )
2015-02-15 19:18:12 +00:00
self . _mappings [ port_number ] = ( " dot1q " , native_vlan )
2015-02-14 03:01:18 +00:00
2018-10-15 10:05:49 +00:00
async def set_qinq_port ( self , port_number , outer_vlan , ethertype ) :
2015-02-14 03:01:18 +00:00
"""
Sets the specified port as a trunk ( QinQ ) port .
: param port_number : allocated port number
: param outer_vlan : outer VLAN ( transport VLAN ) for this QinQ port
"""
if port_number not in self . _nios :
2021-04-13 09:07:58 +00:00
raise DynamipsError ( f " Port { port_number } is not allocated " )
2015-02-14 03:01:18 +00:00
nio = self . _nios [ port_number ]
2015-09-08 09:03:11 +00:00
if ethertype != " 0x8100 " and parse_version ( self . hypervisor . version ) < parse_version ( ' 0.2.16 ' ) :
2021-04-13 09:07:58 +00:00
raise DynamipsError ( f " Dynamips version required is >= 0.2.16 to change the default QinQ Ethernet type, detected version is { self . hypervisor . version } " )
2015-09-08 09:03:11 +00:00
2018-10-15 10:05:49 +00:00
await self . _hypervisor . send ( ' ethsw set_qinq_port " {name} " {nio} {outer_vlan} {ethertype} ' . format ( name = self . _name ,
2015-09-08 09:03:11 +00:00
nio = nio ,
outer_vlan = outer_vlan ,
ethertype = ethertype if ethertype != " 0x8100 " else " " ) )
2015-02-14 03:01:18 +00:00
2015-08-20 05:45:30 +00:00
log . info ( ' Ethernet switch " {name} " [ {id} ]: port {port} set as a QinQ ( {ethertype} ) port with outer VLAN {vlan_id} ' . format ( name = self . _name ,
2015-09-08 09:03:11 +00:00
id = self . _id ,
port = port_number ,
vlan_id = outer_vlan ,
ethertype = ethertype ) )
2015-08-20 05:45:30 +00:00
self . _mappings [ port_number ] = ( " qinq " , outer_vlan , ethertype )
2015-02-14 03:01:18 +00:00
2018-10-15 10:05:49 +00:00
async def get_mac_addr_table ( self ) :
2015-02-14 03:01:18 +00:00
"""
Returns the MAC address table for this Ethernet switch .
: returns : list of entries ( Ethernet address , VLAN , NIO )
"""
2021-04-13 09:07:58 +00:00
mac_addr_table = await self . _hypervisor . send ( f ' ethsw show_mac_addr_table " { self . _name } " ' )
2015-02-14 03:01:18 +00:00
return mac_addr_table
2018-10-15 10:05:49 +00:00
async def clear_mac_addr_table ( self ) :
2015-02-14 03:01:18 +00:00
"""
Clears the MAC address table for this Ethernet switch .
"""
2021-04-13 09:07:58 +00:00
await self . _hypervisor . send ( f ' ethsw clear_mac_addr_table " { self . _name } " ' )
2015-02-14 03:01:18 +00:00
2018-10-15 10:05:49 +00:00
async def start_capture ( self , port_number , output_file , data_link_type = " DLT_EN10MB " ) :
2015-02-14 03:01:18 +00:00
"""
Starts a packet capture .
: param port_number : allocated port number
: param output_file : PCAP destination file for the capture
: param data_link_type : PCAP data link type ( DLT_ * ) , default is DLT_EN10MB
"""
2018-10-27 07:47:17 +00:00
nio = self . get_nio ( port_number )
2015-02-14 03:01:18 +00:00
data_link_type = data_link_type . lower ( )
if data_link_type . startswith ( " dlt_ " ) :
data_link_type = data_link_type [ 4 : ]
if nio . input_filter [ 0 ] is not None and nio . output_filter [ 0 ] is not None :
2021-04-13 09:07:58 +00:00
raise DynamipsError ( f " Port { port_number } has already a filter applied " )
2015-02-14 03:01:18 +00:00
2019-04-01 13:58:18 +00:00
await nio . start_packet_capture ( output_file , data_link_type )
2015-02-16 00:45:53 +00:00
log . info ( ' Ethernet switch " {name} " [ {id} ]: starting packet capture on port {port} ' . format ( name = self . _name ,
id = self . _id ,
port = port_number ) )
2015-02-14 03:01:18 +00:00
2018-10-15 10:05:49 +00:00
async def stop_capture ( self , port_number ) :
2015-02-14 03:01:18 +00:00
"""
Stops a packet capture .
: param port_number : allocated port number
"""
2018-10-27 07:47:17 +00:00
nio = self . get_nio ( port_number )
2019-04-01 12:47:31 +00:00
if not nio . capturing :
return
2019-04-01 13:58:18 +00:00
await nio . stop_packet_capture ( )
2015-02-16 00:45:53 +00:00
log . info ( ' Ethernet switch " {name} " [ {id} ]: stopping packet capture on port {port} ' . format ( name = self . _name ,
id = self . _id ,
port = port_number ) )