예제 #1
0
    def lldp_packet(dpid, port_no, dl_addr, ttl):
        pkt = packet.Packet()

        dst = lldp.LLDP_MAC_NEAREST_BRIDGE
        src = dl_addr
        ethertype = ETH_TYPE_LLDP
        eth_pkt = ethernet.ethernet(dst, src, ethertype)
        pkt.add_protocol(eth_pkt)

        tlv_chassis_id = lldp.ChassisID(
            subtype=lldp.ChassisID.SUB_LOCALLY_ASSIGNED,
            chassis_id=(LLDPPacket.CHASSIS_ID_FMT %
                        dpid_to_str(dpid)).encode('ascii'))

        tlv_port_id = lldp.PortID(subtype=lldp.PortID.SUB_PORT_COMPONENT,
                                  port_id=struct.pack(LLDPPacket.PORT_ID_STR,
                                                      port_no))

        tlv_ttl = lldp.TTL(ttl=ttl)
        tlv_end = lldp.End()

        tlvs = (tlv_chassis_id, tlv_port_id, tlv_ttl, tlv_end)
        lldp_pkt = lldp.lldp(tlvs)
        pkt.add_protocol(lldp_pkt)

        pkt.serialize()
        return pkt.data
예제 #2
0
 def test_json(self):
     chassis_id = lldp.ChassisID(subtype=lldp.ChassisID.SUB_MAC_ADDRESS,
                                 chassis_id=b'\x00\x01\x30\xf9\xad\xa0')
     port_id = lldp.PortID(subtype=lldp.PortID.SUB_INTERFACE_NAME,
                           port_id=b'1/1')
     ttl = lldp.TTL(ttl=120)
     port_desc = lldp.PortDescription(
         port_description=b'Summit300-48-Port 1001\x00')
     sys_name = lldp.SystemName(system_name=b'Summit300-48\x00')
     sys_desc = lldp.SystemDescription(
         system_description=b'Summit300-48 - Version 7.4e.1 (Build 5) ' +
         b'by Release_Master 05/27/05 04:53:11\x00')
     sys_cap = lldp.SystemCapabilities(system_cap=0x14, enabled_cap=0x14)
     man_addr = lldp.ManagementAddress(addr_subtype=0x06,
                                       addr=b'\x00\x01\x30\xf9\xad\xa0',
                                       intf_subtype=0x02,
                                       intf_num=1001,
                                       oid='')
     org_spec = lldp.OrganizationallySpecific(oui=b'\x00\x12\x0f',
                                              subtype=0x02,
                                              info=b'\x07\x01\x00')
     end = lldp.End()
     tlvs = (chassis_id, port_id, ttl, port_desc, sys_name, sys_desc,
             sys_cap, man_addr, org_spec, end)
     lldp1 = lldp.lldp(tlvs)
     jsondict = lldp1.to_jsondict()
     lldp2 = lldp.lldp.from_jsondict(jsondict['lldp'])
     eq_(str(lldp1), str(lldp2))
예제 #3
0
 def test_tlv(self):
     tlv = lldp.ChassisID(subtype=lldp.ChassisID.SUB_MAC_ADDRESS,
                          chassis_id=b'\x00\x04\x96\x1f\xa7\x26')
     eq_(tlv.tlv_type, lldp.LLDP_TLV_CHASSIS_ID)
     eq_(tlv.len, 7)
     (typelen, ) = struct.unpack('!H', b'\x02\x07')
     eq_(tlv.typelen, typelen)
예제 #4
0
    def test_serialize(self):
        pkt = packet.Packet()

        dst = lldp.LLDP_MAC_NEAREST_BRIDGE
        src = '00:04:96:1f:a7:26'
        ethertype = ether.ETH_TYPE_LLDP
        eth_pkt = ethernet.ethernet(dst, src, ethertype)
        pkt.add_protocol(eth_pkt)

        tlv_chassis_id = lldp.ChassisID(
            subtype=lldp.ChassisID.SUB_MAC_ADDRESS,
            chassis_id=addrconv.mac.text_to_bin(src))
        tlv_port_id = lldp.PortID(subtype=lldp.PortID.SUB_INTERFACE_NAME,
                                  port_id=b'1/3')
        tlv_ttl = lldp.TTL(ttl=120)
        tlv_end = lldp.End()
        tlvs = (tlv_chassis_id, tlv_port_id, tlv_ttl, tlv_end)
        lldp_pkt = lldp.lldp(tlvs)
        pkt.add_protocol(lldp_pkt)

        eq_(len(pkt.protocols), 2)

        pkt.serialize()

        # Note: If ethernet frame is less than 60 bytes length,
        # ethernet.ethernet() appends padding to the payload.
        # So, we splits the serialized data to compare.
        data_len = len(self.data)
        pkt_data_lldp = pkt.data[:data_len]
        pkt_data_pad = pkt.data[data_len:]
        eq_(b'\x00' * (60 - data_len), pkt_data_pad)

        eq_(self.data, pkt_data_lldp)
예제 #5
0
    def test_to_string(self):
        chassis_id = lldp.ChassisID(subtype=lldp.ChassisID.SUB_MAC_ADDRESS,
                                    chassis_id=b'\x00\x04\x96\x1f\xa7\x26')
        port_id = lldp.PortID(subtype=lldp.PortID.SUB_INTERFACE_NAME,
                              port_id=b'1/3')
        ttl = lldp.TTL(ttl=120)
        end = lldp.End()
        tlvs = (chassis_id, port_id, ttl, end)
        lldp_pkt = lldp.lldp(tlvs)

        chassis_id_values = {
            'subtype': lldp.ChassisID.SUB_MAC_ADDRESS,
            'chassis_id': b'\x00\x04\x96\x1f\xa7\x26',
            'len': chassis_id.len,
            'typelen': chassis_id.typelen
        }
        _ch_id_str = ','.join([
            '%s=%s' % (k, repr(chassis_id_values[k]))
            for k, v in inspect.getmembers(chassis_id)
            if k in chassis_id_values
        ])
        tlv_chassis_id_str = '%s(%s)' % (lldp.ChassisID.__name__, _ch_id_str)

        port_id_values = {
            'subtype': port_id.subtype,
            'port_id': port_id.port_id,
            'len': port_id.len,
            'typelen': port_id.typelen
        }
        _port_id_str = ','.join([
            '%s=%s' % (k, repr(port_id_values[k]))
            for k, v in inspect.getmembers(port_id) if k in port_id_values
        ])
        tlv_port_id_str = '%s(%s)' % (lldp.PortID.__name__, _port_id_str)

        ttl_values = {'ttl': ttl.ttl, 'len': ttl.len, 'typelen': ttl.typelen}
        _ttl_str = ','.join([
            '%s=%s' % (k, repr(ttl_values[k]))
            for k, v in inspect.getmembers(ttl) if k in ttl_values
        ])
        tlv_ttl_str = '%s(%s)' % (lldp.TTL.__name__, _ttl_str)

        end_values = {'len': end.len, 'typelen': end.typelen}
        _end_str = ','.join([
            '%s=%s' % (k, repr(end_values[k]))
            for k, v in inspect.getmembers(end) if k in end_values
        ])
        tlv_end_str = '%s(%s)' % (lldp.End.__name__, _end_str)

        _tlvs_str = '(%s, %s, %s, %s)'
        tlvs_str = _tlvs_str % (tlv_chassis_id_str, tlv_port_id_str,
                                tlv_ttl_str, tlv_end_str)

        _lldp_str = '%s(tlvs=%s)'
        lldp_str = _lldp_str % (lldp.lldp.__name__, tlvs_str)

        eq_(str(lldp_pkt), lldp_str)
        eq_(repr(lldp_pkt), lldp_str)
예제 #6
0
    def test_serialize_without_ethernet(self):
        tlv_chassis_id = lldp.ChassisID(subtype=lldp.ChassisID.SUB_MAC_ADDRESS,
                                        chassis_id=b'\x00\x04\x96\x1f\xa7\x26')
        tlv_port_id = lldp.PortID(subtype=lldp.PortID.SUB_INTERFACE_NAME,
                                  port_id=b'1/3')
        tlv_ttl = lldp.TTL(ttl=120)
        tlv_end = lldp.End()
        tlvs = (tlv_chassis_id, tlv_port_id, tlv_ttl, tlv_end)
        lldp_pkt = lldp.lldp(tlvs)

        eq_(lldp_pkt.serialize(None, None),
            self.data[ethernet.ethernet._MIN_LEN:])
예제 #7
0
 def test_json(self):
     chassis_id = lldp.ChassisID(subtype=lldp.ChassisID.SUB_MAC_ADDRESS,
                                 chassis_id=b'\x00\x04\x96\x1f\xa7\x26')
     port_id = lldp.PortID(subtype=lldp.PortID.SUB_INTERFACE_NAME,
                           port_id=b'1/3')
     ttl = lldp.TTL(ttl=120)
     end = lldp.End()
     tlvs = (chassis_id, port_id, ttl, end)
     lldp1 = lldp.lldp(tlvs)
     jsondict = lldp1.to_jsondict()
     lldp2 = lldp.lldp.from_jsondict(jsondict['lldp'])
     eq_(str(lldp1), str(lldp2))
예제 #8
0
    def test_serialize(self):
        pkt = packet.Packet()

        dst = lldp.LLDP_MAC_NEAREST_BRIDGE
        src = '00:01:30:f9:ad:a0'
        ethertype = ether.ETH_TYPE_LLDP
        eth_pkt = ethernet.ethernet(dst, src, ethertype)
        pkt.add_protocol(eth_pkt)

        tlv_chassis_id = lldp.ChassisID(
            subtype=lldp.ChassisID.SUB_MAC_ADDRESS,
            chassis_id=addrconv.mac.text_to_bin(src))
        tlv_port_id = lldp.PortID(subtype=lldp.PortID.SUB_INTERFACE_NAME,
                                  port_id=b'1/1')
        tlv_ttl = lldp.TTL(ttl=120)
        tlv_port_description = lldp.PortDescription(
            port_description=b'Summit300-48-Port 1001\x00')
        tlv_system_name = lldp.SystemName(system_name=b'Summit300-48\x00')
        tlv_system_description = lldp.SystemDescription(
            system_description=b'Summit300-48 - Version 7.4e.1 (Build 5) ' +
            b'by Release_Master 05/27/05 04:53:11\x00')
        tlv_system_capabilities = lldp.SystemCapabilities(system_cap=0x14,
                                                          enabled_cap=0x14)
        tlv_management_address = lldp.ManagementAddress(
            addr_subtype=0x06,
            addr=b'\x00\x01\x30\xf9\xad\xa0',
            intf_subtype=0x02,
            intf_num=1001,
            oid=b'')
        tlv_organizationally_specific = lldp.OrganizationallySpecific(
            oui=b'\x00\x12\x0f', subtype=0x02, info=b'\x07\x01\x00')
        tlv_end = lldp.End()
        tlvs = (tlv_chassis_id, tlv_port_id, tlv_ttl, tlv_port_description,
                tlv_system_name, tlv_system_description,
                tlv_system_capabilities, tlv_management_address,
                tlv_organizationally_specific, tlv_end)
        lldp_pkt = lldp.lldp(tlvs)
        pkt.add_protocol(lldp_pkt)

        eq_(len(pkt.protocols), 2)

        pkt.serialize()

        # self.data has many organizationally specific TLVs
        data = six.binary_type(pkt.data[:-2])
        eq_(data, self.data[:len(data)])
예제 #9
0
def lldp_beacon(eth_src,
                chassis_id,
                port_id,
                ttl,
                org_tlvs=None,
                system_name=None,
                port_descr=None):
    """Return an LLDP frame suitable for a host/access port.

    Args:
        eth_src (str): source Ethernet MAC address.
        chassis_id (str): Chassis ID.
        port_id (int): port ID,
        TTL (int): TTL for payload.
        org_tlvs (list): list of tuples of (OUI, subtype, info).
    Returns:
        ryu.lib.packet.ethernet: Ethernet packet with header.
    """
    pkt = build_pkt_header(None, eth_src, lldp.LLDP_MAC_NEAREST_BRIDGE,
                           valve_of.ether.ETH_TYPE_LLDP)
    tlvs = [
        lldp.ChassisID(subtype=lldp.ChassisID.SUB_MAC_ADDRESS,
                       chassis_id=addrconv.mac.text_to_bin(chassis_id)),
        lldp.PortID(subtype=lldp.PortID.SUB_INTERFACE_NAME,
                    port_id=str(port_id).encode('utf-8')),
        lldp.TTL(ttl=ttl)
    ]
    for tlv, info_name, info in ((lldp.SystemName, 'system_name', system_name),
                                 (lldp.PortDescription, 'port_description',
                                  port_descr)):
        if info is not None:
            info_args = {info_name: info.encode('UTF-8')}
            tlvs.append(tlv(**info_args))
    if org_tlvs is not None:
        for tlv_oui, tlv_subtype, tlv_info in org_tlvs:
            tlvs.append(
                lldp.OrganizationallySpecific(oui=tlv_oui,
                                              subtype=tlv_subtype,
                                              info=tlv_info))
    tlvs.append(lldp.End())
    lldp_pkt = lldp.lldp(tlvs)
    pkt.add_protocol(lldp_pkt)
    pkt.serialize()
    return pkt
예제 #10
0
    def test_to_string(self):
        chassis_id = lldp.ChassisID(subtype=lldp.ChassisID.SUB_MAC_ADDRESS,
                                    chassis_id=b'\x00\x01\x30\xf9\xad\xa0')
        port_id = lldp.PortID(subtype=lldp.PortID.SUB_INTERFACE_NAME,
                              port_id=b'1/1')
        ttl = lldp.TTL(ttl=120)
        port_desc = lldp.PortDescription(
            port_description=b'Summit300-48-Port 1001\x00')
        sys_name = lldp.SystemName(system_name=b'Summit300-48\x00')
        sys_desc = lldp.SystemDescription(
            system_description=b'Summit300-48 - Version 7.4e.1 (Build 5) ' +
            b'by Release_Master 05/27/05 04:53:11\x00')
        sys_cap = lldp.SystemCapabilities(system_cap=0x14, enabled_cap=0x14)
        man_addr = lldp.ManagementAddress(addr_subtype=0x06,
                                          addr=b'\x00\x01\x30\xf9\xad\xa0',
                                          intf_subtype=0x02,
                                          intf_num=1001,
                                          oid='')
        org_spec = lldp.OrganizationallySpecific(oui=b'\x00\x12\x0f',
                                                 subtype=0x02,
                                                 info=b'\x07\x01\x00')
        end = lldp.End()
        tlvs = (chassis_id, port_id, ttl, port_desc, sys_name, sys_desc,
                sys_cap, man_addr, org_spec, end)
        lldp_pkt = lldp.lldp(tlvs)

        # ChassisID string
        chassis_id_values = {
            'subtype': lldp.ChassisID.SUB_MAC_ADDRESS,
            'chassis_id': b'\x00\x01\x30\xf9\xad\xa0',
            'len': chassis_id.len,
            'typelen': chassis_id.typelen
        }
        _ch_id_str = ','.join([
            '%s=%s' % (k, repr(chassis_id_values[k]))
            for k, v in inspect.getmembers(chassis_id)
            if k in chassis_id_values
        ])
        tlv_chassis_id_str = '%s(%s)' % (lldp.ChassisID.__name__, _ch_id_str)

        # PortID string
        port_id_values = {
            'subtype': port_id.subtype,
            'port_id': port_id.port_id,
            'len': port_id.len,
            'typelen': port_id.typelen
        }
        _port_id_str = ','.join([
            '%s=%s' % (k, repr(port_id_values[k]))
            for k, v in inspect.getmembers(port_id) if k in port_id_values
        ])
        tlv_port_id_str = '%s(%s)' % (lldp.PortID.__name__, _port_id_str)

        # TTL string
        ttl_values = {'ttl': ttl.ttl, 'len': ttl.len, 'typelen': ttl.typelen}
        _ttl_str = ','.join([
            '%s=%s' % (k, repr(ttl_values[k]))
            for k, v in inspect.getmembers(ttl) if k in ttl_values
        ])
        tlv_ttl_str = '%s(%s)' % (lldp.TTL.__name__, _ttl_str)

        # PortDescription string
        port_desc_values = {
            'tlv_info': port_desc.tlv_info,
            'len': port_desc.len,
            'typelen': port_desc.typelen
        }
        _port_desc_str = ','.join([
            '%s=%s' % (k, repr(port_desc_values[k]))
            for k, v in inspect.getmembers(port_desc) if k in port_desc_values
        ])
        tlv_port_desc_str = '%s(%s)' % (lldp.PortDescription.__name__,
                                        _port_desc_str)

        # SystemName string
        sys_name_values = {
            'tlv_info': sys_name.tlv_info,
            'len': sys_name.len,
            'typelen': sys_name.typelen
        }
        _system_name_str = ','.join([
            '%s=%s' % (k, repr(sys_name_values[k]))
            for k, v in inspect.getmembers(sys_name) if k in sys_name_values
        ])
        tlv_system_name_str = '%s(%s)' % (lldp.SystemName.__name__,
                                          _system_name_str)

        # SystemDescription string
        sys_desc_values = {
            'tlv_info': sys_desc.tlv_info,
            'len': sys_desc.len,
            'typelen': sys_desc.typelen
        }
        _sys_desc_str = ','.join([
            '%s=%s' % (k, repr(sys_desc_values[k]))
            for k, v in inspect.getmembers(sys_desc) if k in sys_desc_values
        ])
        tlv_sys_desc_str = '%s(%s)' % (lldp.SystemDescription.__name__,
                                       _sys_desc_str)

        # SystemCapabilities string
        sys_cap_values = {
            'system_cap': 0x14,
            'enabled_cap': 0x14,
            'len': sys_cap.len,
            'typelen': sys_cap.typelen
        }
        _sys_cap_str = ','.join([
            '%s=%s' % (k, repr(sys_cap_values[k]))
            for k, v in inspect.getmembers(sys_cap) if k in sys_cap_values
        ])
        tlv_sys_cap_str = '%s(%s)' % (lldp.SystemCapabilities.__name__,
                                      _sys_cap_str)

        # ManagementAddress string
        man_addr_values = {
            'addr_subtype': 0x06,
            'addr': b'\x00\x01\x30\xf9\xad\xa0',
            'addr_len': man_addr.addr_len,
            'intf_subtype': 0x02,
            'intf_num': 1001,
            'oid': '',
            'oid_len': man_addr.oid_len,
            'len': man_addr.len,
            'typelen': man_addr.typelen
        }
        _man_addr_str = ','.join([
            '%s=%s' % (k, repr(man_addr_values[k]))
            for k, v in inspect.getmembers(man_addr) if k in man_addr_values
        ])
        tlv_man_addr_str = '%s(%s)' % (lldp.ManagementAddress.__name__,
                                       _man_addr_str)

        # OrganizationallySpecific string
        org_spec_values = {
            'oui': b'\x00\x12\x0f',
            'subtype': 0x02,
            'info': b'\x07\x01\x00',
            'len': org_spec.len,
            'typelen': org_spec.typelen
        }
        _org_spec_str = ','.join([
            '%s=%s' % (k, repr(org_spec_values[k]))
            for k, v in inspect.getmembers(org_spec) if k in org_spec_values
        ])
        tlv_org_spec_str = '%s(%s)' % (lldp.OrganizationallySpecific.__name__,
                                       _org_spec_str)

        # End string
        end_values = {'len': end.len, 'typelen': end.typelen}
        _end_str = ','.join([
            '%s=%s' % (k, repr(end_values[k]))
            for k, v in inspect.getmembers(end) if k in end_values
        ])
        tlv_end_str = '%s(%s)' % (lldp.End.__name__, _end_str)

        # tlvs string
        _tlvs_str = '(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'
        tlvs_str = _tlvs_str % (
            tlv_chassis_id_str, tlv_port_id_str, tlv_ttl_str,
            tlv_port_desc_str, tlv_system_name_str, tlv_sys_desc_str,
            tlv_sys_cap_str, tlv_man_addr_str, tlv_org_spec_str, tlv_end_str)

        # lldp string
        _lldp_str = '%s(tlvs=%s)'
        lldp_str = _lldp_str % (lldp.lldp.__name__, tlvs_str)

        eq_(str(lldp_pkt), lldp_str)
        eq_(repr(lldp_pkt), lldp_str)