Beispiel #1
0
 def error_msg_handler(self, ev):
     """
     A switch has sent us an error event
     """
     msg = ev.msg
     datapath = msg.datapath
     dpid = datapath.id
     if msg.type == 0x03 and msg.code == 0x00:
         self.logger.error('event=OFPErrorMsg_received: dpid=%s '
                   'type=Flow_Table_Full(0x03) code=0x%02x message=%s',
                   dpid, msg.code, utils.hex_array(msg.data))
     else:
         self.logger.error('event=OFPErrorMsg_received: dpid=%s '
                   'type=0x%02x code=0x%02x message=%s',
                   dpid, msg.type, msg.code, utils.hex_array(msg.data))
Beispiel #2
0
 def test_hex_array_string(self):
     """
     Test hex_array() with str type.
     """
     expected_result = '0x01 0x02 0x03 0x04'
     data = b'\x01\x02\x03\x04'
     eq_(expected_result, utils.hex_array(data))
Beispiel #3
0
    def packet_in_handler(self, ev):
        msg = ev.msg
        dp = msg.datapath
        ofp = dp.ofproto

        if msg.reason == ofp.OFPR_NO_MATCH:
            reason = 'NO MATCH'
        elif msg.reason == ofp.OFPR_ACTION:
            reason = 'ACTION'
        elif msg.reason == ofp.OFPR_INVALID_TTL:
            reason = 'INVALID TTL'
        else:
            reason = 'unknown'

        self.logger.debug(
            'OFPPacketIn received: '
            'buffer_id=%x total_len=%d reason=%s '
            'table_id=%d cookie=%d match=%s data=%s', msg.buffer_id,
            msg.total_len, reason, msg.table_id, msg.cookie, msg.match,
            utils.hex_array(msg.data))

        pkt = packet.packet.Packet(array.array('B', ev.msg.data))
        for p in pkt.protocols:
            print(p)
            if p.protocol_name == 'ipv4':
                print(p.src)
Beispiel #4
0
 def error_msg_handler(self, ev):
     msg = ev.msg
     print "OFPErrorMsg received: type=0x%02x code=0x%02x 'message=%s'" % (
         msg.type,
         msg.code,
         utils.hex_array(msg.data),
     )
Beispiel #5
0
 def record_of_errors(self, ev):
     msg = ev.msg
     self.logger.error("OF Error: type=0x%02x code=0x%02x "
                       "message=%s", msg.type, msg.code,
                       utils.hex_array(msg.data))
     OPENFLOW_ERROR_MSG.labels(error_type="0x%02x" % msg.type,
                               error_code="0x%02x" % msg.code).inc()
Beispiel #6
0
 def openflow_error(self, ev):
     """
         Print Error Received. Useful for troubleshooting, specially with Brocade
     """
     msg = ev.msg
     print ('OFPErrorMsg received: type=0x%02x code=0x%02x message=%s' %
            (msg.type, msg.code, utils.hex_array(msg.data)))
Beispiel #7
0
    def packet_in_handler(self, ev):
        msg = ev.msg
        dp = msg.datapath
        ofp = dp.ofproto

        if msg.reason == ofp.OFPR_NO_MATCH:
            reason = 'NO MATCH'
        elif msg.reason == ofp.OFPR_ACTION:
            reason = 'ACTION'
        elif msg.reason == ofp.OFPR_INVALID_TTL:
            reason = 'INVALID TTL'
        else:
            reason = 'unknown'

        self.logger.debug('OFPPacketIn received: '
                          'buffer_id=%x total_len=%d reason=%s '
                          'table_id=%d cookie=%d match=%s data=%s',
                          msg.buffer_id, msg.total_len, reason,
                          msg.table_id, msg.cookie, msg.match,
                          utils.hex_array(msg.data))

        pkt = packet.packet.Packet(array.array('B', ev.msg.data))
        for p in pkt.protocols:
            print(p)
            if p.protocol_name == 'ipv4':
                print(p.src)
Beispiel #8
0
def msg(datapath, version, msg_type, msg_len, xid, buf):
    exp = None
    try:
        assert len(buf) >= msg_len
    except AssertionError as e:
        exp = e

    msg_parser = _MSG_PARSERS.get(version)
    if msg_parser is None:
        raise exception.OFPUnknownVersion(version=version)

    try:
        msg = msg_parser(datapath, version, msg_type, msg_len, xid, buf)
    except exception.OFPTruncatedMessage as e:
        raise e
    except:
        LOG.exception(
            'Encountered an error while parsing OpenFlow packet from switch. '
            'This implies the switch sent a malformed OpenFlow packet. '
            'version 0x%02x msg_type %d msg_len %d xid %d buf %s',
            version, msg_type, msg_len, xid, utils.hex_array(buf))
        msg = None
    if exp:
        raise exp
    return msg
Beispiel #9
0
 def test_hex_array_bytearray(self):
     """
     Test hex_array() with bytearray type.
     """
     expected_result = '0x01 0x02 0x03 0x04'
     data = bytearray(b'\x01\x02\x03\x04')
     eq_(expected_result, utils.hex_array(data))
Beispiel #10
0
 def openflow_error(self, ev):
     """
         Print Error Received. Useful for troubleshooting
         Args:
             ev: EventOFPErrorMsg
     """
     print('OFPErrorMsg received: type=0x%02x code=0x%02x message=%s' %
           (ev.msg.type, ev.msg.code, utils.hex_array(ev.msg.data)))
Beispiel #11
0
 def OF_error_msg_handler(self, event):
     msg = event.msg
     LOG.error(
         _LE('OFPErrorMsg received: type=0x%(type)02x '
             'code=0x%(code)02x message=%(msg)s'), {
                 'type': msg.type,
                 'code': msg.code,
                 'msg': utils.hex_array(msg.data)
             })
Beispiel #12
0
 def test_hex_array_bytes(self):
     """
     Test hex_array() with bytes type. (Python3 only)
     """
     if six.PY2:
         return
     expected_result = '0x01 0x02 0x03 0x04'
     data = bytes(b'\x01\x02\x03\x04')
     eq_(expected_result, utils.hex_array(data))
Beispiel #13
0
 def test_hex_array_bytes(self):
     """
     Test hex_array() with bytes type. (Python3 only)
     """
     if six.PY2:
         return
     expected_result = '0x01 0x02 0x03 0x04'
     data = bytes(b'\x01\x02\x03\x04')
     eq_(expected_result, utils.hex_array(data))
Beispiel #14
0
 def error_msg_handler(self, ev):
     """
     erroe handler
     :param ev:
     :return:
     """
     msg = ev.msg
     self.logger.info(
         'OFPErrorMsg received: type=0x%02x code=0x%02x '
         'message=%s, datapath:%s', msg.type, msg.code,
         utils.hex_array(msg.data), msg.datapath.id)
 def error_msg_handler(self, ev):
  
  global flow_list
  global pmod
  global lock
  
  msg = ev.msg
  self.logger.info('OFPErrorMsg received: type=0x%02x code=0x%02x '
        'message=%s',
        msg.type, msg.code, utils.hex_array(msg.data))
  if (msg.type == 1):
   if (msg.code == 8):
    print "BUFFER UNKNOW - SATURATION ATTACK"
    incrcounterB(1)
  if (msg.type == 5):
   if (msg.code == 1):
    print "TABLE IS FULL!!!!!!!!!!!!!!"
    incrcounterF(1)
    self.send_table_stats_request(msg.datapath)
    
    if (random() < float(buffer)/float(pmod) ):
     #print "joguei a moeda"
     
     lock.acquire()
     try:
      pmod = pmod + 100
      #print 'PMOD ganhou na moeda:', pmod
     finally:
      lock.release()     

     pos = uniform(0, len(flow_list))
     int_pos = int(pos)
     if (int_pos == 0):
      int_post = 1
     #print 'flow to be removed', flow_list[int_pos] #flow que ira sair
     #print "List initial size:", len(flow_list)
     flow_removed = flow_list.pop(int_pos)
     #print "List final size:", len(flow_list)
     
     match_del = flow_removed.match
     #print match_del
     #procurar pelo pair e remover
     self.check_pair(flow_removed, msg.datapath)
     self.remove_flow(msg.datapath, match_del)
     #Flow removido
     
    else:
    
     lock.acquire()
     try:
      pmod = pmod + 100
      #print 'PMOD ganhou na moeda:', pmod
     finally:
      lock.release()    
Beispiel #16
0
  def handle_error_msg(self, ev):
    msg = ev.msg
    ofp = self.dp.ofproto

    if msg.type == ofp.OFPET_METER_MOD_FAILED:
      cmd = 'ovs-vsctl set bridge s1 datapath_type=netdev'
      self.logger.error('METER_MOD failed, "%s" might help' % cmd)
    elif msg.type and msg.code:
      self.logger.error('OFPErrorMsg received: type=0x%02x code=0x%02x '
                        'message=%s',
                        msg.type, msg.code, utils.hex_array(msg.data))
    else:
      self.logger.error('OFPErrorMsg received: %s', msg)
Beispiel #17
0
 def error_msg_handler(self, ev):
     """
     A switch has sent us an error event
     """
     msg = ev.msg
     datapath = msg.datapath
     dpid = datapath.id
     self.logger.error('event=OFPErrorMsg_received: dpid=%s '
                   'type=%s code=%s message=%s',
                   dpid, msg.type, msg.code, utils.hex_array(msg.data))
     #*** Log human-friendly decodes for the error type and code:
     type1, type2, code1, code2 = of_error_decode.decode(msg.type, msg.code)
     self.logger.error('error_type=%s %s error_code=%s %s', type1, type2,
                             code1, code2)
Beispiel #18
0
 def OF_error_msg_handler(self, event):
     msg = event.msg
     try:
         (version, msg_type, msg_len, xid) = ofproto_parser.header(msg.data)
         ryu_msg = ofproto_parser.msg(
             self._datapath, version, msg_type,
             msg_len - ofproto_common.OFP_HEADER_SIZE, xid, msg.data)
         LOG.error('OFPErrorMsg received: %s', ryu_msg)
     except Exception:
         LOG.error('Unrecognized OFPErrorMsg received: '
                   'type=0x%(type)02x code=0x%(code)02x '
                   'message=%(msg)s',
                   {'type': msg.type, 'code': msg.code,
                    'msg': utils.hex_array(msg.data)})
Beispiel #19
0
 def error_msg_handler(self, ev):
     """
     A switch has sent us an error event
     """
     msg = ev.msg
     datapath = msg.datapath
     dpid = datapath.id
     self.logger.error(
         'event=OFPErrorMsg_received: dpid=%s '
         'type=%s code=%s message=%s', dpid, msg.type, msg.code,
         utils.hex_array(msg.data))
     #*** Log human-friendly decodes for the error type and code:
     type1, type2, code1, code2 = of_error_decode.decode(msg.type, msg.code)
     self.logger.error('error_type=%s %s error_code=%s %s', type1, type2,
                       code1, code2)
Beispiel #20
0
def msg(datapath, version, msg_type, msg_len, xid, buf):
    assert len(buf) >= msg_len

    msg_parser = _MSG_PARSERS.get(version)
    if msg_parser is None:
        raise exception.OFPUnknownVersion(version=version)

    try:
        return msg_parser(datapath, version, msg_type, msg_len, xid, buf)
    except:
        LOG.exception(
            'Encounter an error during parsing OpenFlow packet from switch.'
            'This implies switch sending a malformed OpenFlow packet.'
            'version 0x%02x msg_type %d msg_len %d xid %d buf %s',
            version, msg_type, msg_len, xid, utils.hex_array(buf))
        return None
Beispiel #21
0
def msg(datapath, version, msg_type, msg_len, xid, buf):
    assert len(buf) >= msg_len

    msg_parser = _MSG_PARSERS.get(version)
    if msg_parser is None:
        raise exception.OFPUnknownVersion(version=version)

    try:
        return msg_parser(datapath, version, msg_type, msg_len, xid, buf)
    except:
        LOG.exception(
            'Encounter an error during parsing OpenFlow packet from switch.'
            'This implies switch sending a malformed OpenFlow packet.'
            'version 0x%02x msg_type %d msg_len %d xid %d buf %s', version,
            msg_type, msg_len, xid, utils.hex_array(buf))
        return None
Beispiel #22
0
    def _badWeb_Potect(self, datapath, msg):
        print "in _badWeb_Potect"
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser
        pkt = packet.Packet(msg.data)
        hdata = utils.hex_array(msg.data)
        hdata = hdata.split(' ')
        hex_data = ''
        for hexdata in hdata:
            cc = hexdata.replace('0x', '')
            if len(cc) == 1:
                cc = '0%s' % cc
            hex_data = hex_data + cc
        # print "hex_data", hex_data
        # print 'pkt:', pkt

        hex_dnsdata = hex_data[84:]
        # print "dns hex data", hex_dnsdata
        dns_binary = binascii.unhexlify(hex_dnsdata)
        dns = DNSRecord.parse(dns_binary)
        # print 'dns:', dns
        dns
        web_name = dns.questions[0].get_qname().label
        web_name = ".".join(list(web_name))
        # print web_name

        try:
            conn = MySQLdb.connect(host='localhost',
                                   user='******',
                                   passwd='123456',
                                   db='web',
                                   port=3306)
            cur = conn.cursor()
            select = 'select * from WEB_lacklist where name="%s"' % web_name
            if (cur.execute(select)):
                print ' ilegal web  "%s", it`s dangerous! you  can`t to access it.' % web_name
                cur.close()
                conn.close()
                return
            else:
                print 'legal web "%s",you can access it.' % web_name
                cur.close()
                conn.close()
                self._forwarding(datapath, msg)
        except MySQLdb.Error, e:
            print "Mysql Error %d: %s" % (e.args[0], e.args[1])
Beispiel #23
0
    def error_msg_handler(self, ev):

        global flow_list
        global pmod
        global lock

        msg = ev.msg
        self.logger.info(
            'OFPErrorMsg received: type=0x%02x code=0x%02x '
            'message=%s', msg.type, msg.code, utils.hex_array(msg.data))
        if (msg.type == 1):
            if (msg.code == 8):
                print "BUFFER UNKNOW - SATURATION ATTACK"
                incrcounterB(1)
        if (msg.type == 5):
            if (msg.code == 1):
                print "TABLE IS FULL!!!!!!!!!!!!!!"
                incrcounterF(1)
                self.flows_request_stats(msg.datapath)
Beispiel #24
0
    def _badWeb_Potect(self, datapath, msg):
        print "in _badWeb_Potect"
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser
        pkt = packet.Packet(msg.data)
        hdata = utils.hex_array(msg.data)
        hdata = hdata.split(' ')
        hex_data = ''
        for hexdata in hdata:
            cc = hexdata.replace('0x', '')
            if len(cc) == 1:
                cc = '0%s' % cc
            hex_data = hex_data + cc
        # print "hex_data", hex_data
        # print 'pkt:', pkt

        hex_dnsdata = hex_data[84:]
        # print "dns hex data", hex_dnsdata
        dns_binary = binascii.unhexlify(hex_dnsdata)
        dns = DNSRecord.parse(dns_binary)
        # print 'dns:', dns
        dns
        web_name = dns.questions[0].get_qname().label
        web_name = ".".join(list(web_name))
        # print web_name

        try:
            conn = MySQLdb.connect(
                host='localhost', user='******', passwd='123456', db='web', port=3306)
            cur = conn.cursor()
            select = 'select * from WEB_lacklist where name="%s"' % web_name
            if(cur.execute(select)):
                print ' ilegal web  "%s", it`s dangerous! you  can`t to access it.' % web_name
                cur.close()
                conn.close()
                return
            else:
                print 'legal web "%s",you can access it.' % web_name
                cur.close()
                conn.close()
                self._forwarding(datapath, msg)
        except MySQLdb.Error, e:
            print "Mysql Error %d: %s" % (e.args[0], e.args[1])
Beispiel #25
0
    def packet_in_handler(self, ev):

        self.logger.info('packet_in')

        msg = ev.msg
        datapath = msg.datapath
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser

        #if ofproto_v1_4.OFP_NO_BUFFER == msg.buffer_id:
        #  self.logger.info('not buffered in switch')

        #actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_FLOOD)]
        #out = ofp_parser.OFPPacketOut(
        #  datapath=dp,
        #  buffer_id=msg.buffer_id,
        #  in_port=msg.msg.match['in_port'],
        #  actions=actions)
        #dp.send_msg(out)

        if msg.reason == ofproto.OFPR_TABLE_MISS:
            reason = 'TABLE MISS'
        elif msg.reason == ofproto.OFPR_APPLY_ACTION:
            reason = 'APPLY ACTION'
        elif msg.reason == ofproto.OFPR_INVALID_TTL:
            reason = 'INVALID TTL'
        elif msg.reason == ofproto.OFPR_ACTION_SET:
            reason = 'ACTION SET'
        elif msg.reason == ofproto.OFPR_GROUP:
            reason = 'GROUP'
        elif msg.reason == ofproto.OFPR_PACKET_OUT:
            reason = 'PACKET OUT'
        else:
            reason = 'unknown'

        self.logger.info(
            'OFPPacketIn received: '
            'datapath_id=%016x '
            'buffer_id=%x total_len=%d reason=%s '
            'table_id=%d cookie=%d match=%s data=%s', datapath.id,
            msg.buffer_id, msg.total_len, reason, msg.table_id, msg.cookie,
            msg.match, utils.hex_array(msg.data))
Beispiel #26
0
def msg(datapath, version, msg_type, msg_len, xid, buf):
    assert len(buf) >= msg_len

    msg_parser = _MSG_PARSERS.get(version)
    if msg_parser is None:
        raise exception.OFPUnknownVersion(version=version)

    try:
        return msg_parser(datapath, version, msg_type, msg_len, xid, buf)
    except:
        LOG.exception(
            "Encountered an error while parsing OpenFlow packet from switch. "
            "This implies the switch sent a malformed OpenFlow packet. "
            "version 0x%02x msg_type %d msg_len %d xid %d buf %s",
            version,
            msg_type,
            msg_len,
            xid,
            utils.hex_array(buf),
        )
        return None
Beispiel #27
0
 def test_hex_array_bytearray(self):
     ''' Test bytearray conversion into array of hexes '''
     expected_result = '0x1 0x2 0x3 0x4'
     data = bytearray(b'\01\02\03\04')
     eq_(expected_result, utils.hex_array(data))
Beispiel #28
0
 def test_hex_array_string(self):
     ''' Test string conversion into array of hexes '''
     expected_result = '0x1 0x2 0x3 0x4'
     data = b'\01\02\03\04'
     eq_(expected_result, utils.hex_array(data))
Beispiel #29
0
 def error_msg_handler(self, ev):
     msg = ev.msg
     self.logger.debug('error msg ev %s type 0x%x code 0x%x %s', msg,
                       msg.type, msg.code, utils.hex_array(msg.data))
Beispiel #30
0
 def error_msg_handler(self, ev):
     msg = ev.msg
     self.logger.error("OpenFlow Error In")
     self.logger.debug("Error Info | Type 0x%02x | Code 0x%02x", msg.type,
                       msg.code)
     self.logger.debug("Error Message | %s", hex_array(msg.data))
Beispiel #31
0
 def echo_request_handler(self, event):
     self.logger.info('OFPEchoRequest received: data=%s', utils.hex_array(event.msg.data))
     datapath = event.msg.datapath
     data = event.msg.data
     self.send_echo_request(datapath, data)
Beispiel #32
0
 def _error_handler(self,ev):
     msg = ev.msg
     print 'type=0x%02x code=0x%02x msg : %s'%(msg.type,msg.code,hex_array(msg.data))
Beispiel #33
0
    def _packet_in_handler(self, ev):

        msg = ev.msg
        ofproto = msg.datapath.ofproto
        reason = msg.reason

        if msg.msg_len < msg.total_len:
            self.logger.debug("packet truncated: only %s of %s bytes",
                              ev.msg.msg_len, ev.msg.total_len)

        self.nr_packet_in_total += 1

        if self.sampling_enabled:
            self.pcap_writer.write_pkt(msg.data)
            self.throughput_tmp = self.throughput
            self.throughput += msg.msg_len
            self.nr_packet_in_sampling += 1

        if reason != ofproto.OFPR_ACTION:

            datapath = msg.datapath
            ofp_parser = datapath.ofproto_parser

            in_port = msg.match['in_port']
            pkt = packet.Packet(msg.data)
            eth = pkt.get_protocols(ethernet.ethernet)[0]
            dst = eth.dst
            src = eth.src
            dpid = datapath.id
            self.mac_to_port.setdefault(dpid, {})

            self.logger.debug('packet in %s %s %s %s %s', dpid, src, dst,
                              in_port, reason)
            self.logger.debug(
                'OFPPacketIn received: '
                'buffer_id=%x total_len=%d'
                'table_id=%d match=%s data=%s', msg.buffer_id, msg.total_len,
                msg.table_id, msg.match, utils.hex_array(msg.data))

            # learn a mac address to avoid FLOOD next time.
            self.mac_to_port[dpid][src] = in_port

            if dst in self.mac_to_port[dpid]:
                out_port = self.mac_to_port[dpid][dst]
            else:
                out_port = ofproto.OFPP_FLOOD

            if out_port != ofproto.OFPP_FLOOD:
                if self.group_enabled is False:
                    self.send_group_mod(datapath, self.GROUP_ID,
                                        'add')  # inject group into the switch
                    self.logger.debug('Group %s injected', self.GROUP_ID)
                    self.sampling_thread = hub.spawn(
                        self.sampling_options[self.sampling_technique])
                    self.group_enabled = True

                actions = [ofp_parser.OFPActionGroup(group_id=self.GROUP_ID)]
                match = ofp_parser.OFPMatch(in_port=in_port, eth_dst=dst)

                if msg.buffer_id != ofproto.OFP_NO_BUFFER:
                    self.add_flow(datapath, 0, self.PRIORITY, match, actions,
                                  self.DEFAULT_TABLE, msg.buffer_id)
                else:
                    self.add_flow(datapath, 0, self.PRIORITY, match, actions,
                                  self.DEFAULT_TABLE)

                self.send_packet_out(datapath, msg.buffer_id, msg.data,
                                     in_port)
            else:
                self.flood(msg)  # flood the packet
Beispiel #34
0
 def error_msg_handler(self, ev):
     from ryu import utils
     msg = ev.msg
     self.logger.info('OFPErrorMsg received: type=0x%02x code=0x%02x message=%s', msg.type, msg.code, utils.hex_array(msg.data))
 def error_msg_handler(self, ev):
     msg = ev.msg
     self.logger.debug('error msg ev %s type 0x%x code 0x%x %s',
                       msg, msg.type, msg.code, utils.hex_array(msg.data))
Beispiel #36
0
 def test_hex_array_invalid(self):
     ''' Test conversion into array of hexes with invalid data type '''
     expected_result = None
     data = 1234
     eq_(expected_result, utils.hex_array(data))
Beispiel #37
0
 def error_msg_handler(self, ev):
     from ryu import utils
     msg = ev.msg
     self.logger.info(
         'OFPErrorMsg received: type=0x%02x code=0x%02x message=%s',
         msg.type, msg.code, utils.hex_array(msg.data))
Beispiel #38
0
 def OF_error_msg_handler(self, event):
     msg = event.msg
     LOG.error(_LE('OFPErrorMsg received: type=0x%(type)02x '
                   'code=0x%(code)02x message=%(msg)s'),
               {'type': msg.type, 'code': msg.code,
                'msg': utils.hex_array(msg.data)})
Beispiel #39
0
 def error_msg_handler(self, ev):
   msg = ev.msg
   self.logger.error('OFPErrorMsg received: type=0x%02x code=0x%02x '
     'message=%s',
      msg.type, msg.code, utils.hex_array(msg.data)
    )
Beispiel #40
0
 def echo_request_handler(self, event):
     self.logger.info('OFPEchoRequest received: data=%s',
                      utils.hex_array(event.msg.data))
     datapath = event.msg.datapath
     data = event.msg.data
     self.send_echo_request(datapath, data)