1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | py @lldp.set_tlv_type(LLDP_TLV_SEND_TIME) class TimeStamp(LLDPBasicTLV): _PACK_STR = &#39;!d&#39; _PACK_SIZE = struct.calcsize(_PACK_STR) _LEN_MIN = _PACK_SIZE _LEN_MAX = _PACK_SIZE def __init__(self, buf=None, *args, **kwargs): super(TimeStamp, self).__init__(buf, *args, **kwargs) if buf: (self.timestamp, ) = struct.unpack( self._PACK_STR, self.tlv_info[:self._PACK_SIZE]) else: self.timestamp = kwargs[&#39;timestamp&#39;] self.len = self._PACK_SIZE assert self._len_valid self.typelen = (self.tlv_type << LLDP_TLV_TYPE_SHIFT) | self.len def serialize(self): return struct.pack(&#39;!Hd&#39;, self.typelen, self.timestamp) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | py class LLDPPacket(object): # make a LLDP packet for link discovery. CHASSIS_ID_PREFIX = &#39;dpid:&#39; CHASSIS_ID_PREFIX_LEN = len(CHASSIS_ID_PREFIX) CHASSIS_ID_FMT = CHASSIS_ID_PREFIX + &#39;%s&#39; PORT_ID_STR = &#39;!I&#39; # uint32_t PORT_ID_SIZE = 4 DOMAIN_ID_PREFIX = &#39;domain_id:&#39; DOMAIN_ID_PREFIX_LEN = len(DOMAIN_ID_PREFIX) DOMAIN_ID_FMT = DOMAIN_ID_PREFIX + &#39;%s&#39; VPORT_ID_STR = &#39;!I&#39; # uint32_t VPORT_ID_SIZE = 4 class LLDPUnknownFormat(RyuException): message = &#39;%(msg)s&#39; @staticmethod def lldp_packet(dpid, port_no, dl_addr, ttl, timestamp, vport_no=ofproto_v1_0.OFPP_NONE): 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)) 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_timestamp = lldp.TimeStamp(timestamp=timestamp) tlv_end = lldp.End tlvs = (tlv_chassis_id, tlv_port_id, tlv_ttl, tlv_timestamp, tlv_end) lldp_pkt = lldp.lldp(tlvs) pkt.add_protocol(lldp_pkt) pkt.serialize return pkt.data @staticmethod def lldp_parse(data): pkt = packet.Packet(data) i = iter(pkt) eth_pkt = i.next assert type(eth_pkt) == ethernet.ethernet lldp_pkt = i.next if type(lldp_pkt) != lldp.lldp: raise LLDPPacket.LLDPUnknownFormat tlv_chassis_id = lldp_pkt.tlvs[0] if tlv_chassis_id.subtype != lldp.ChassisID.SUB_LOCALLY_ASSIGNED: raise LLDPPacket.LLDPUnknownFormat( msg=&#39;unknown chassis id subtype %d&#39; % tlv_chassis_id.subtype) chassis_id = tlv_chassis_id.chassis_id if not chassis_id.startswith(LLDPPacket.CHASSIS_ID_PREFIX): raise LLDPPacket.LLDPUnknownFormat( msg=&#39;unknown chassis id format %s&#39; % chassis_id) src_dpid = str_to_dpid(chassis_id[LLDPPacket.CHASSIS_ID_PREFIX_LEN:]) tlv_port_id = lldp_pkt.tlvs[1] if tlv_port_id.subtype != lldp.PortID.SUB_PORT_COMPONENT: raise LLDPPacket.LLDPUnknownFormat( msg=&#39;unknown port id subtype %d&#39; % tlv_port_id.subtype) port_id = tlv_port_id.port_id if len(port_id) != LLDPPacket.PORT_ID_SIZE: raise LLDPPacket.LLDPUnknownFormat( msg=&#39;unknown port id %d&#39; % port_id) (src_port_no, ) = struct.unpack(LLDPPacket.PORT_ID_STR, port_id) tlv_timestamp = lldp_pkt.tlvs[3] timestamp = tlv_timestamp.timestamp return src_dpid, src_port_no, timestamp |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | py @lldp.set_tlv_type(LLDP_TLV_VPORT_ID) class VPortID(LLDPBasicTLV): _PACK_STR = &#39;!B&#39; _PACK_SIZE = struct.calcsize(_PACK_STR) # subtype id(1 octet) + port id length(1 - 255 octet) _LEN_MIN = 2 _LEN_MAX = 256 # VPort ID subtype SUB_INTERFACE_ALIAS = 1 # ifAlias (IETF RFC 2863) SUB_PORT_COMPONENT = 2 # entPhysicalAlias (IETF RFC 4133) SUB_MAC_ADDRESS = 3 # MAC address (IEEE Std 802) SUB_NETWORK_ADDRESS = 4 # networkAddress SUB_INTERFACE_NAME = 5 # ifName (IETF RFC 2863) SUB_AGENT_CIRCUIT_ID = 6 # agent circuit ID(IETF RFC 3046) SUB_LOCALLY_ASSIGNED = 7 # local def __init__(self, buf=None, *args, **kwargs): super(VPortID, self).__init__(buf, *args, **kwargs) if buf: (self.subtype, ) = struct.unpack( self._PACK_STR, self.tlv_info[:self._PACK_SIZE]) self.vport_id = self.tlv_info[self._PACK_SIZE:] else: self.subtype = kwargs[&#39;subtype&#39;] self.vport_id = kwargs[&#39;vport_id&#39;] self.len = self._PACK_SIZE + len(self.vport_id) assert self._len_valid self.typelen = (self.tlv_type << LLDP_TLV_TYPE_SHIFT) | self.len def serialize(self): return struct.pack(&#39;!HB&#39;, self.typelen, self.subtype) + self.vport_id |
欢迎光临 51学通信论坛2017新版 (http://bbs.51xuetongxin.com/) | Powered by Discuz! X3 |