def connect(self, src_port, dst_sw, dst_port): src_port_name = "p" + str(self.ID) + ':' + str(src_port) + '-' + str(dst_sw.ID) + ':' + str(dst_port) dst_port_name = "p" + str(dst_sw.ID) + ':' + str(dst_port) + '-' + str(self.ID) + ':' + str(src_port) self.attach_port(src_port_name) dst_sw.attach_port(dst_port_name) ovs_vsctl_set('interface', src_port_name, 'type', None, 'patch') ovs_vsctl_set('interface', src_port_name, 'options', 'peer', dst_port_name) ovs_vsctl_set('interface', dst_port_name, 'type', None, 'patch') ovs_vsctl_set('interface', dst_port_name, 'options', 'peer', src_port_name)
def xmlrpc_ovs_vsctl_set(self, table, record, column, key, value): """ This function allows to alter OVS database. """ return vswitch.ovs_vsctl_set(table, record, column, key, value)
def xmlrpc_ovs_vsctl_set(self, table, record, column, key, value): """ This function allows to alter OVS database. """ return vswitch.ovs_vsctl_set(table, record, column, key, value)
import vswitch #borramos configuracion anterior vswitch.ovs_vsctl_del_bridge("br0") #creamos el bridge br0 vswitch.ovs_vsctl_add_bridge("br0") #anadimos interfaces administrativas vswitch.ovs_vsctl_admin_port("br0", "vlan10", "10") vswitch.ovs_vsctl_set_admin("vlan10", "internal") vswitch.ovs_vsctl_admin_port("br0", "vlan20", "20") vswitch.ovs_vsctl_set_admin("vlan20", "internal") vswitch.ifconfig("192.168.0.10/24", "vlan10") vswitch.ifconfig("192.168.1.10/24", "vlan20") #anadimos las interfaces al bridge vswitch.ovs_vsctl_add_port_to_bridge("br0", "eth2") vswitch.ovs_vsctl_add_port_to_bridge("br0", "eth3") vswitch.ovs_vsctl_add_port_to_bridge("br0", "eth4") vswitch.ovs_vsctl_add_port_to_bridge("br0", "eth5") #metemos los vpcs a vlan 10 y 20 vswitch.ovs_vsctl_set("Port", "eth2", "tag", None, "10") vswitch.ovs_vsctl_set("Port", "eth3", "tag", None, "10") vswitch.ovs_vsctl_set("Port", "eth4", "tag", None, "20") vswitch.ovs_vsctl_add_trunk_port("eth5", "trunks=10,20") #vswitch.ovs_vsctl_set("Port","eth5","tag",None,"20")
def run(self, line): if (common.get_console().prompt == "(config-if)"): args = line.split() if (len(args) > 1): options = args[1:][0] important_mode = "" str_options = str(options).strip('[]').strip('\'"') if (str_options == "mode"): mode = args[2:] str_mode = str(mode).strip('[]').strip('\'"') if (str_mode == "trunk"): important_mode = "trunk" if (common.get_multi() == True): common.append_array_multi(line) else: common.append_array_history(line) common.set_control_counter_mode_trunk(1) else: if (str_mode == "access"): important_mode = "access" if (common.get_multi() == True): common.append_array_multi(line) else: common.append_array_history(line) common.set_control_counter_mode_access(1) else: print(Colors.FAIL + "[ERROR] " + Colors.ENDC + "Bad Command use") return else: if (str_options == "trunk"): if (common.get_control_counter_mode_trunk() == 0): print(Colors.FAIL + "[ERROR] " + Colors.ENDC + "first switchport mode trunk") return if (len(args[1:]) == 4): if (len(args[2:]) > 0) and ( args[2:][0] == "allowed") and (args[2:][1] == "vlan"): allowed_vlan = args[4:] allowed_vlan_divide = str( args[4:]).strip('[]').strip('\'"') allowed_vlan_test = allowed_vlan_divide.replace( ',', ' ') vlan_for_testing = allowed_vlan_test.split() testing_vlan = False for i in range(0, len(vlan_for_testing)): testing_vlan = False for j in range(0, common.get_len_vlan_id()): if (vlan_for_testing[i] == common.get_item_vlan_id(j)): testing_vlan = True break if (not (testing_vlan)): print( Colors.FAIL + "[ERROR] " + Colors.ENDC + "One or more vlans specified are not created" ) return str_allowed_vlan = str(allowed_vlan).strip( '[]').strip('\'"') if (common.get_multi() == True): common.append_array_multi(line) else: common.append_array_history(line) vswitch.ovs_vsctl_add_trunk_port( connecting_interfaces.get_connect(), "trunks=%s" % allowed_vlan_divide) print(Colors.OKGREEN + "[OK] " + Colors.ENDC + "Allowed VLAN's = " + allowed_vlan_divide + " in port " + connecting_interfaces.get_connect()) common.set_control_counter_mode_trunk(0) else: print(Colors.FAIL + "[ERROR] " + Colors.ENDC + "first switchport mode [trunk|access]") else: print( Colors.FAIL + "[ERROR] " + Colors.ENDC + "Usage: switchport trunk allowed vlan [vlan1],[vlan2]" ) else: if (str_options == "access"): if (common.get_control_counter_mode_access() == 0): print(Colors.FAIL + "[ERROR] " + Colors.ENDC + "first switchport mode access") return if (len(args[1:]) == 3): if (args[2:][0] == "vlan"): access_vlan = args[3:] str_access_vlan = str(access_vlan).strip( '[]').strip('\'"') connecting_vlan.set_connect_vlan_id_switchport_vlan( str_access_vlan) check = False for i in range(0, common.get_len_vlan_id()): if (common.get_item_vlan_id(i) == str_access_vlan): check = True if (common.get_multi() == True): common.append_array_multi(line) else: common.append_array_history( line) common.append_array_interfaces_vlan( connecting_vlan. get_connect_vlan_interface( ), i) vswitch.ovs_vsctl_set( "Port", connecting_interfaces. get_connect(), "tag", None, str_access_vlan) common.set_control_counter_mode_access( 0) if (not check): print(Colors.FAIL + "[ERROR] " + Colors.ENDC + "this vlan doesnt exist") else: print( Colors.FAIL + "[ERROR] " + Colors.ENDC + "Usage: switchport access vlan [vlan id]" ) else: print( Colors.FAIL + "[ERROR] " + Colors.ENDC + "Usage: switchport access vlan [vlan id]") else: print(Colors.FAIL + "[ERROR] " + Colors.ENDC + "first switchport mode [trunk|access]") else: print(Colors.FAIL + "[ERROR] " + Colors.ENDC + "Bad command use") else: print(Colors.FAIL + "[ERROR] " + Colors.ENDC + "Bad Command use")