def create_discovery_packet(self, dpid, portNum, portAddr): """ Create LLDP packet """ discovery_packet = lldp() cid = chassis_id() # Maybe this should be a MAC. But a MAC of what? Local port, maybe? cid.fill(cid.SUB_LOCAL, bytes('dpid:' + hex(long(dpid))[2:-1])) discovery_packet.add_tlv(cid) pid = port_id() pid.fill(pid.SUB_PORT, str(portNum)) discovery_packet.add_tlv(pid) ttlv = ttl() ttlv.fill(LLDP_TTL) discovery_packet.add_tlv(ttlv) sysdesc = system_description() sysdesc.fill(bytes('dpid:' + hex(long(dpid))[2:-1])) discovery_packet.add_tlv(sysdesc) discovery_packet.add_tlv(end_tlv()) eth = ethernet() eth.src = portAddr eth.dst = NDP_MULTICAST eth.set_payload(discovery_packet) eth.type = ethernet.LLDP_TYPE po = of.ofp_packet_out(action=of.ofp_action_output(port=portNum), data=eth.pack()) return po.pack()
def create_discovery_packet(self, dpid, portNum, portAddr): """ Create LLDP packet """ discovery_packet = lldp() cid = chassis_id() # Maybe this should be a MAC. But a MAC of what? Local port, maybe? cid.fill(cid.SUB_LOCAL, bytes("dpid:" + hex(long(dpid))[2:-1])) discovery_packet.add_tlv(cid) pid = port_id() pid.fill(pid.SUB_PORT, str(portNum)) discovery_packet.add_tlv(pid) ttlv = ttl() ttlv.fill(LLDP_TTL) discovery_packet.add_tlv(ttlv) sysdesc = system_description() sysdesc.fill(bytes("dpid:" + hex(long(dpid))[2:-1])) discovery_packet.add_tlv(sysdesc) discovery_packet.add_tlv(end_tlv()) eth = ethernet() eth.src = portAddr eth.dst = NDP_MULTICAST eth.set_payload(discovery_packet) eth.type = ethernet.LLDP_TYPE po = of.ofp_packet_out(action=of.ofp_action_output(port=portNum), data=eth.pack()) return po.pack()
def send_lldp_packet(self, event): """ Sends LLDP packet to all neighbour switches by iterating over the ports of the switch and sending the packet to each one apart from the controller. This function is called each second from each switch with the help of the Timer object set for each switch. Note: This function is an exact copy to the code given in Appendix B of the Exercise description. """ dst = Discovery.LLDP_DST_ADDR # == '\x01\x80\xc2\x00\x00\x0e' for p in event.ofp.ports: if p.port_no < of.OFPP_MAX: # Build LLDP packet src = str(p.hw_addr) port = p.port_no lldp_p = lldp() # create LLDP payload ch_id = chassis_id() # Add switch ID part ch_id.subtype = 1 ch_id.id = str(event.dpid) lldp_p.add_tlv(ch_id) po_id = port_id() # Add port ID part po_id.subtype = 2 po_id.id = str(port) lldp_p.add_tlv(po_id) tt = ttl() # Add TTL tt.ttl = Discovery.LLDP_INTERVAL # == 1 lldp_p.add_tlv(tt) lldp_p.add_tlv(end_tlv()) ether = ethernet() # Create an Ethernet packet ether.type = ethernet.LLDP_TYPE # Set its type to LLDP ether.src = src # Set src, dst ether.dst = dst ether.payload = lldp_p # Set payload to be the LLDP payload # send LLDP packet pkt = of.ofp_packet_out(action=of.ofp_action_output(port=port)) pkt.data = ether event.connection.send(pkt)
def _send_lldp(self, event ): """" """"" # log.debug('Flooding packet : dest = {} src = {} in_port = {}'.format(packet.dst, packet.src, packet_in.in_port)) # self.send_packet(packet_in.buffer_id, packet_in.data, of.OFPP_FLOOD, packet_in.in_port) # log.debug("send lldp sw : {}".format(event.dpid)) dst = Discovery.LLDP_DST_ADDR # == '\x01\x80\xc2\x00\x00\x0e' for p in event.ofp.ports: if p.port_no < of.OFPP_MAX: # Build LLDP packet src = str(p.hw_addr) port = p.port_no lldp_p = lldp() # create LLDP payload ch_id = chassis_id() # Add switch ID part ch_id.subtype = 1 ch_id.id = str(event.dpid) lldp_p.add_tlv(ch_id) po_id = port_id() # Add port ID part po_id.subtype = 2 po_id.id = str(port) lldp_p.add_tlv(po_id) tt = ttl() # Add TTL tt.ttl = Discovery.LLDP_INTERVAL # == 1 lldp_p.add_tlv(tt) lldp_p.add_tlv(end_tlv()) ether = ethernet() # Create an Ethernet packet ether.type = ethernet.LLDP_TYPE # Set its type to LLDP ether.src = src # Set src, dst ether.dst = dst ether.payload = lldp_p # Set payload to be the LLDP payload # send LLDP packet pkt = of.ofp_packet_out(action = of.ofp_action_output(port = port)) pkt.data = ether event.connection.send(pkt)
def _send_LLDP_to_switch(self,event): ''' sending lldp packet to all of a switch ports :param event: the switch ConnectionUp Event ''' dst = Discovery.LLDP_DST_ADDR for p in event.ofp.ports: if p.port_no < of.OFPP_MAX: # @UndefinedVariable # Build LLDP packet src = str(p.hw_addr) port = p.port_no lldp_p = lldp() # create LLDP payload ch_id=chassis_id() # Add switch ID part ch_id.fill(ch_id.SUB_LOCAL,bytes(hex(long(event.dpid))[2:-1])) # This works, the appendix example doesn't #ch_id.subtype=chassis_id.SUB_LOCAL #ch_id.id=event.dpid lldp_p.add_tlv(ch_id) po_id = port_id() # Add port ID part po_id.subtype = 2 po_id.id = str(port) lldp_p.add_tlv(po_id) tt = ttl() # Add TTL tt.ttl = 1 lldp_p.add_tlv(tt) lldp_p.add_tlv(end_tlv()) ether = ethernet() # Create an Ethernet packet ether.type = ethernet.LLDP_TYPE # Set its type to LLDP ether.src = src # Set src, dst ether.dst = dst ether.payload = lldp_p # Set payload to be the LLDP payload # send LLDP packet pkt = of.ofp_packet_out(action = of.ofp_action_output(port = port)) pkt.data = ether event.connection.send(pkt)