Exemple #1
0
    def sendAndReceiveOF_FEATURE_OPENFLOWV1(self):

        try : 
            # receive OF_FEATURE_REQUEST message
            data = self.receiveDataFromSocket()
            print( " 332 Switch ip " + self.switchIp + " Receive OF_FEATURE_REQUEST message from controller")
            data = unpack_message(data)

            if  data.header.message_type.name != "OFPT_FEATURES_REQUEST" :
                print( " 336 Switch ip " + self.switchIp + " terminate because switch don't receive OF_FEATURES_REQUEST" )
                sys.exit()

            # get tranID from OF_FEATURE_REQUEST message
            tranID = data.header.xid

            #send OF_FEATURE_REPLY message
            listPort = self.getActivePortFromSnmpVersion2C( 0 )

            print("All active port of switch ip " + self.switchIp +  " : ")
            for i in listPort:
                print("Hw_addr : " + i.hw_addr)
                print("Hw_desc : " + i.name)

            # find max value of mac address from list mac address
            maxPort = "000000000000"
            maxIndex = 0
            
            for index , item in enumerate( listPort ):
                tempMac = item.hw_addr.replace(":","")
                if ( int( tempMac , 16 ) > int( maxPort , 16 ) ):
                    maxPort = tempMac
                    maxIndex = index

            
            # create OF_FEATURE_REPLY message
            packed_data = FeaRes()
            packed_data.header.xid = tranID

            # gen datapath_id from hw_addr of first port
            packed_data.datapath_id = listPort[maxIndex].hw_addr + ":ff:ff"
            #packed_data.datapath_id = '00:00:00:00:00:00:00:02'

            packed_data.n_buffers = 256
            packed_data.n_tables = 254
            packed_data.capabilities = 199
            packed_data.actions = 4095

            # create port
            #port1 = PPort(1, '00:00:00:00:00:02','eth1', 0, 0, 192 ,0,0,0)
            #packed_data.ports = [port1]
            packed_data.ports = listPort
            packed_data = packed_data.pack()

            # send OF_FEATURE_REPLY message
            self.s.send(packed_data)
            print("Send OF_FEATURE_REPLY message to controller")

        except Exception as err :
            print( " 387 Switch ip " + self.switchIp + " terminate because handling run-time error : " + str( err ) )
            sys.exit()
Exemple #2
0
    def sendAndReceiveOF_FEATURE_OPENFLOWV1(self, checkData, tempBytes):

        try:
            # receive OF_FEATURE_REQUEST message
            if checkData:
                data = tempBytes
            else:
                data = self.receiveDataFromSocket()

            print("Switch ip " + self.switchIp +
                  " Receive OF_FEATURE_REQUEST message from controller")
            data = unpack_message(data)

            if data.header.message_type.name != "OFPT_FEATURES_REQUEST":
                print(
                    " 336 Switch ip " + self.switchIp +
                    " terminate because switch don't receive OF_FEATURES_REQUEST"
                )
                sys.exit()

            # get tranID from OF_FEATURE_REQUEST message
            tranID = data.header.xid

            #send OF_FEATURE_REPLY message

            listPort = []
            dictRemoteSwitchData = self.getRemoteSwitchDataFromSnmpVersion2C()
            dictPort = self.getPortFromSnmpVersion2C(0)

            for snmpPosition in dictPort:
                if snmpPosition in dictRemoteSwitchData:
                    self.dictActivePort[snmpPosition] = dictPort[snmpPosition]
                    listPort.append(dictPort[snmpPosition])

            print("list active port " + self.switchIp + " : ")
            for key, port in self.dictActivePort.items():
                print("snmp position : " + key + " | hw_addr : " +
                      str(port.hw_addr) + " | port_no : " + str(port.port_no))

            # find max value of mac address from list mac address
            maxPort = "000000000000"
            maxIndex = 0

            for index, item in enumerate(listPort):
                tempMac = item.hw_addr.replace(":", "")
                if (int(tempMac, 16) > int(maxPort, 16)):
                    maxPort = tempMac
                    maxIndex = index

            # create OF_FEATURE_REPLY message
            packed_data = FeaRes()
            packed_data.header.xid = tranID

            # gen datapath_id from hw_addr of first port
            packed_data.datapath_id = listPort[maxIndex].hw_addr + ":ff:ff"
            #packed_data.datapath_id = '00:00:00:00:00:00:00:02'

            packed_data.n_buffers = 256
            packed_data.n_tables = 254
            packed_data.capabilities = 199
            packed_data.actions = 4095

            # create port
            #port1 = PPort(1, '00:00:00:00:00:02','eth1', 0, 0, 192 ,0,0,0)
            #packed_data.ports = [port1]
            packed_data.ports = listPort
            packed_data = packed_data.pack()

            # send OF_FEATURE_REPLY message
            self.s.send(packed_data)
            print("Send OF_FEATURE_REPLY message to controller")

            #record datapath id
            self.datapathId = listPort[maxIndex].hw_addr + ":ff:ff"
            self.datapathId = "dpid:" + self.datapathId.replace(":", "")
            """
            tempPortID = i[1][1].prettyPrint()[2:] # 00000001
            # change 00000001 to \x00\x00\x00\x01
            packer = struct.Struct('bbbb')
            secondData = packer.pack( int( tempPortID[0:2] , 16 ) , int( tempPortID[2:4] , 16 ) , int( tempPortID[4:6] , 16 ) , int( tempPortID[6:8] , 16 ))
            """

            try:
                self.lock.acquire_write()
                # add port to dict all active port in tda
                for snmpPosition, port in self.dictActivePort.items():
                    tempPortID = str(port.port_no)
                    tempPortID = ("0" * (8 - len(tempPortID))) + tempPortID
                    packer = struct.Struct('bbbb')
                    tempPortID = packer.pack(int(tempPortID[0:2], 16),
                                             int(tempPortID[2:4], 16),
                                             int(tempPortID[4:6], 16),
                                             int(tempPortID[6:8], 16))
                    self.dictAllActivePortInTDA[(port.hw_addr, port.name)] = [
                        self.datapathId, tempPortID
                    ]
                self.lock.release()
                print(self.dictAllActivePortInTDA)
            except Exception as err:
                self.lock.release()
                print(" 422 Switch ip " + self.switchIp +
                      " terminate because handling run-time error : " +
                      str(err))
                sys.exit()

        except Exception as err:
            print(" 426 Switch ip " + self.switchIp +
                  " terminate because handling run-time error : " + str(err))
            sys.exit()
Exemple #3
0
    def initConnectionToController(self):

        count = 0

        while count < self.numberOfRetransmission:
            try:
                packed_data = Hello().pack()
                # send OF_HEllO message to contoller
                self.s.send(packed_data)
                print("Switch ip " + self.switchIp +
                      " Send OF_HELLO message to controller")

                # receive OF_HELLO message from controller
                data = self.s.recv(self.buffer_size)
                data = unpack_message(data)

                if data.header.message_type.name == "OFPT_HELLO":

                    print("Switch ip " + self.switchIp +
                          " Receive OF_HELLO message from controller")
                    data = self.s.recv(self.buffer_size)
                    data = unpack_message(data)
                    print(
                        " 428 Switch ip " + self.switchIp +
                        " Receive OF_FEATURE_REQUEST message from controller")
                    if data.header.message_type.name == "OFPT_FEATURES_REQUEST":

                        # get tranID from OF_FEATURE_REQUEST message
                        tranID = data.header.xid

                        #send OF_FEATURE_REPLY message
                        listPort = self.createOFFeatureReplyFromSnmpVersion2C(
                            1)

                        print("All active port of switch ip " + self.switchIp +
                              " : ")
                        for i in listPort:
                            print("Hw_addr : " + i.hw_addr)
                            print("Hw_desc : " + i.name)

                        # find max value of mac address from list mac address
                        maxPort = "000000000000"
                        maxIndex = 0

                        for index, item in enumerate(listPort):
                            tempMac = item.hw_addr.replace(":", "")
                            if (int(tempMac, 16) > int(maxPort, 16)):
                                maxPort = tempMac
                                maxIndex = index

                        # create OF_FEATURE_REPLY message
                        packed_data = FeaRes()
                        packed_data.header.xid = tranID

                        # gen datapath_id from hw_addr of first port
                        packed_data.datapath_id = listPort[
                            maxIndex].hw_addr + ":ff:ff"
                        #packed_data.datapath_id = '00:00:00:00:00:00:00:02'

                        packed_data.n_buffers = 256
                        packed_data.n_tables = 254
                        packed_data.capabilities = 199
                        packed_data.actions = 4095

                        # create port
                        #port1 = PPort(1, '00:00:00:00:00:02','eth1', 0, 0, 192 ,0,0,0)
                        #packed_data.ports = [port1]
                        packed_data.ports = listPort
                        packed_data = packed_data.pack()

                        # send OF_FEATURE_REPLY message
                        self.s.send(packed_data)
                        print("Send OF_FEATURE_REPLY message to controller")

                        return

            except Exception as err:
                count += 1
                print(" 322 Switch ip " + self.switchIp +
                      " handling run-time error of socket : " + str(err))

        print(" Switch ip " + self.switchIp + " terminate")
        sys.exit()