def packet_in(self, event): """ Handles packets sent to the controller.""" msg = event.msg packet = Packet(msg.data) self.packet_in_events.append(packet) # self.logger.info("packet: {}".format(msg)) # Let's keep some counts on the types of packets received ether = packet.get_protocol(ryu.lib.packet.ethernet.ethernet) ipv4 = packet.get_protocol(ryu.lib.packet.ipv4.ipv4) udp = packet.get_protocol(ryu.lib.packet.udp.udp) icmp = packet.get_protocol(ryu.lib.packet.icmp.icmp) if not ether: return ether_type = ether.ethertype self.packet_in_types[ether_type] = self.packet_in_types.get(ether_type) + 1 # Might want to add code here to extract more info from packets # Now let's see if we intercepted a UDP packet if ipv4: self.logger.info("Intercepted a packet from: {} to {}".format( ipv4.src, ipv4.dst)) if udp: self.logger.info("Intercepted UDP packet with source port {} and dest port {}".format( udp.src_port, udp.dst_port)) if icmp: self.logger.info("Intercepted ICMP packet from {} to {}".format(ipv4.src, ipv4.dst))
def _packet_in_handler(self, ev): self.logger.debug("my_arp: _packet_in_handler:") # If you hit this you might want to increase # the "miss_send_length" of your switch if ev.msg.msg_len < ev.msg.total_len: self.logger.debug("packet truncated: only %s of %s bytes", ev.msg.msg_len, ev.msg.total_len) msg = ev.msg datapath = msg.datapath # ofproto = datapath.ofproto inPort = msg.match['in_port'] packets = Packet(msg.data) dpid = datapath.id self.mac_to_port.setdefault(dpid, {}) eth = packets.get_protocols(ethernet)[0] src = eth.src dst = eth.dst self.mac_to_port[dpid][src] = inPort data = msg.data self.arp_learning.setdefault(dpid, []) self.packetToport.setdefault(dpid, []) etherFrame = packets.get_protocol(ethernet) # if dst == LLDP_MAC_NEAREST_BRIDGE: # return # print "packets: ", packets # print "packets.get_protocols(ethernet): ", packets.get_protocols(ethernet) # print "etherFrame######", etherFrame # etherFrame = packets.get_protocol(ethernet) etherFrame = packets.get_protocol(ethernet) # print etherFrame # print ether # print hex(etherFrame.ethertype) # print hex(ether.ETH_TYPE_ARP) if etherFrame.ethertype == ether.ETH_TYPE_ARP: arpPacket = packets.get_protocol(arp) arpArriveTime = time.time() srcMac = etherFrame.src arp_dstIP = arpPacket.dst_ip self.packetToport[datapath.id] = [srcMac, arp_dstIP, inPort, arpArriveTime] # print "arp" # print "packets: ", packets # print "packets.get_protocols(ethernet): ", packets.get_protocols(ethernet) # print "ARP: %s" % arpPacket.opcode # # self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port) # if arpPacket.opcode == 1: # print "ARP Requst" # self.logger.info("packet in %s %s %s %s", datapath.id, srcMac, dst, inPort) # elif arpPacket.opcode == 2: # print "ARP Reply" # self.logger.info("packet in %s %s %s %s", datapath.id, srcMac, dst, inPort) self.receive_arp(datapath, packets, etherFrame, inPort, data) return 0 else: self.logger.debug("Drop packet") return 1
def packet_in_handler(self, ev): msg = ev.msg pkt = Packet(msg.data) dp = msg.datapath #self._request_stats(dp) in_port = msg.match['in_port'] #self._request_stats(dp) ofp = dp.ofproto ofp_parser = dp.ofproto_parser #pdb.set_trace() actions = [] #dp.send_msg(out) ip = pkt.get_protocol(ipv4.ipv4) pk_tcp = pkt.get_protocol(tcp.tcp) #self.total = self.total + pk_tcp.window_size actions.append(ofp_parser.OFPActionOutput(ofp.OFPP_FLOOD)) out = ofp_parser.OFPPacketOut( datapath=dp, buffer_id=msg.buffer_id, in_port=in_port, actions=actions) dp.send_msg(out) op = randint(0,1) if ip: #print ip ip.ttl = op #print ip #actions.append(ofp.OFPActionDecNwTtl()) if ip.ttl == 1: self.tabelaPacotesGrandes.append(ip) self.total = self.total + ip.total_length print "----------------------------------TTL COM 1----------------------------------" else: self.tabelaPacotesPequenos.append(ip) self.total = self.total + ip.total_length print "----------------------------------TTL COM 0----------------------------------" self._send_packet(dp, in_port, pkt)
def handle_ipv4(self, packet_in: ofproto_v1_0_parser.OFPPacketIn, frame: packet.Packet, eth_header: ethernet.ethernet, ipv4_header: ipv4.ipv4): self.logger.info(" %s -> %s, proto=0x%x (%s)", ipv4_header.src, ipv4_header.dst, ipv4_header.proto, ip_proto_name(ipv4_header.proto)) datapath_id = packet_in.datapath.id self.ip_to_port.setdefault(datapath_id, {})[ipv4_header.src] = packet_in.in_port if ipv4_header.proto == inet.IPPROTO_TCP: tcp_header = frame.get_protocol(tcp.tcp) self.handle_tcp(packet_in, eth_header, ipv4_header, tcp_header) elif ipv4_header.proto == inet.IPPROTO_ICMP: icmp_header = frame.get_protocol(icmp.icmp) self.handle_icmp(packet_in, eth_header, ipv4_header, icmp_header)
def packet_in_handler(self, ev): msg = ev.msg pkt = Packet(msg.data) dp = msg.datapath in_port = msg.match['in_port'] ofp = dp.ofproto ofp_parser = dp.ofproto_parser #pdb.set_trace() actions = [] #dp.send_msg(out) ip = pkt.get_protocol(ipv4.ipv4) print ip if ip: print ip #actions.append(ofp.OFPActionDecNwTtl()) actions.append(ofp_parser.OFPActionOutput(ofp.OFPP_FLOOD)) out = ofp_parser.OFPPacketOut( datapath=dp, buffer_id=msg.buffer_id, in_port=in_port, actions=actions) dp.send_msg(out)
def packetIn_handler(self,event): msg = event.msg data = msg.data datapath = msg.datapath dapid = datapath.id ofproto = datapath.ofproto parser = datapath.ofproto_parser in_port = msg.match['in_port'] pkt = Packet(data) eth = pkt.get_protocol(ethernet) eth_src = eth.src eth_dst = eth.dst self.mac_to_port.setdefault(datapath.id,{}) self.mac_to_port[datapath.id].setdefault(eth_src,in_port) #self.mac_to_port[datapath.id][eth_src] = in_port allPkts = dict((p.protocol_name,p) for p in pkt.protocols) print("packet in %s %s"%(str(eth_src),str(eth_dst))) if eth_dst in self.mac_to_port[datapath.id]: out_port = self.mac_to_port[datapath.id][eth_dst] else: out_port = ofproto.OFPP_FLOOD actions = [parser.OFPActionOutput(out_port)] pktout_msg = parser.OFPPacketOut(datapath=datapath,buffer_id=ofproto.OFP_NO_BUFFER, in_port=in_port,actions=actions,data=msg.data) datapath.send_msg(pktout_msg)
def receive_ip(self, msg, port): datapath = msg.datapath packet = Packet(msg.data) ip_packet = packet.get_protocol(ipv4.ipv4) LOG.debug("receive IP packet at port %d" % port) eth_packet = packet.get_protocol(ethernet.ethernet) self.arpcache.set_mac(ip_packet.src, eth_packet.src, port) (port_ip, port_mask, port_mac) = self.ports.get_port(port) if ip_packet.proto == inet.IPPROTO_ICMP: ret = self.receive_icmp(datapath, packet, port) if ret == 1: return self.forwarding(msg) LOG.debug("Packet forwarding: " + str(packet)) return
def _packet_in_handler(self, ev): """Handle packet_in events.""" if not self.link_discovery: return msg = ev.msg in_port = msg.match['in_port'] packet = Packet(msg.data) efm = packet.get_protocol(ethernet.ethernet) if efm.ethertype == ether.ETH_TYPE_ARP: self.send_event_to_observers(event.EventArpReceived(ev)) try: src_dpid, src_port_no = LLDPPacket.lldp_parse(msg.data) except LLDPPacket.LLDPUnknownFormat as e: # This handler can receive all the packtes which can be # not-LLDP packet. Ignore it silently return else: dst_dpid = msg.datapath.id dst_port_no = in_port src = self._get_port(src_dpid, src_port_no) if not src or src.dpid == dst_dpid: return dst = self._get_port(dst_dpid, dst_port_no) if not dst: return old_peer = self.links.get_peer(src) #LOG.debug("Packet-In") #LOG.debug(" src=%s", src) #LOG.debug(" dst=%s", dst) #LOG.debug(" old_peer=%s", old_peer) if old_peer and old_peer != dst: old_link = Link(src, old_peer) self.send_event_to_observers(event.EventLinkDelete(old_link)) link = Link(src, dst) if not link in self.links: self.send_event_to_observers(event.EventLinkAdd(link)) if not self.links.update_link(src, dst): # reverse link is not detected yet. # So schedule the check early because it's very likely it's up try: self.ports.lldp_received(dst) except KeyError as e: # There are races between EventOFPPacketIn and # EventDPPortAdd. So packet-in event can happend before # port add event. In that case key error can happend. # LOG.debug('lldp_received: KeyError %s', e) pass else: self.ports.move_front(dst) self.lldp_event.set()
def packet_in(self, event): """ Handles packets sent to the controller. Used for debugging.""" msg = event.msg dp = msg.datapath # Assumes that datapath ID represents an ascii name switchName = dpidDecode(dp.id) packet = Packet(msg.data) # self.logger.info("packet: {}".format(msg)) ether = packet.get_protocol(ryu.lib.packet.ethernet.ethernet) ethertype = ether.ethertype self.logger.info(" Switch {} received packet with ethertype: {}".format(switchName, hex(ethertype))) if ethertype == 0x8847: mpls = packet.get_protocol(ryu.lib.packet.mpls.mpls) self.logger.info("Label: {}, TTL: {}".format(mpls.label, mpls.ttl)) ipv4 = packet.get_protocol(ryu.lib.packet.ipv4.ipv4) if ipv4: self.logger.info("IPv4 src: {} dst: {}".format( ipv4.src, ipv4.dst))
def _packet_in_handler(self, ev): #LOG.debug("received packet!!") msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser in_port = msg.match['in_port'] pkt = Packet(msg.data) eth_frame = pkt.get_protocol(ethernet) if eth_frame.ethertype == ether.ETH_TYPE_ARP: self.handle_arp(datapath, in_port, eth_frame, pkt)
def packet_in_handler(self, ev): msg = ev.msg datapath = msg.datapath in_port = msg.match['in_port'] packet = Packet(msg.data) ether_frame = packet.get_protocol(ethernet) if ether_frame.ethertype == ether.ETH_TYPE_ARP: self.receive_arp(datapath, packet, ether_frame) return 0 elif ether_frame.ethertype == ether.ETH_TYPE_IP: self.receive_ip(datapath, packet, in_port) return 1 else: return 2
def packet_in_handler(self, ev): msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser pkt = Packet(msg.data) pkt_udp = pkt.get_protocol(udp.udp) pkt_tcp = pkt.get_protocol(tcp.tcp) header_list = dict((p.protocol_name, p) for p in pkt.protocols if type(p) != str) self.logger.info(header_list) if pkt_udp: src_port = pkt_udp.src_port dst_port = pkt_udp.dst_port elif pkt_tcp: src_port = pkt_tcp.src_port dst_port = pkt_tcp.dst_port for ipproto in [6,17]: mod_flow_entry(datapath, {'priority' : 100, 'table_id' : 0, 'match' : {'tp_src': src_port, 'dl_type': 2048, 'ip_proto': ipproto}, 'actions' :[{'type': 'WRITE_METADATA', 'metadata': src_port << 16, 'metadata_mask' : 4294901760}, {'type': 'GOTO_TABLE', 'table_id' : 1}]}, ofproto.OFPFC_ADD) mod_flow_entry(datapath, {'priority' : 100, 'table_id' : 1, 'match' : {'tp_dst': dst_port, 'dl_type': 2048, 'ip_proto': ipproto}, 'actions' :[{'type': 'WRITE_METADATA', 'metadata': dst_port, 'metadata_mask': 65535}, {'type' : 'GOTO_TABLE', 'table_id' : 2}]}, ofproto.OFPFC_ADD) self.packet_out(datapath, pkt)
def _packet_in_handler(self, ev): msg = ev.msg # Extracting packet from incoming pkt_in msg packet = Packet(msg.data) ether_frame = packet.get_protocol(ethernet) try: if ether_frame.ethertype == ether.ETH_TYPE_ARP: dp = msg.datapath in_port = msg.match['in_port'] self.handle_arp(dp, packet, ether_frame, in_port) return 0 self.handle_packet(msg) except Exception, e: print("Exception occurred : ", str(e))
def _packet_in_handler(self, ev): self.logger.debug("my_arp: _packet_in_handler:") # If you hit this you might want to increase # the "miss_send_length" of your switch if ev.msg.msg_len < ev.msg.total_len: self.logger.debug("packet truncated: only %s of %s bytes", ev.msg.msg_len, ev.msg.total_len) msg = ev.msg datapath = msg.datapath # ofproto = datapath.ofproto inPort = msg.match['in_port'] packets = Packet(msg.data) dpid = datapath.id eth = packets.get_protocols(ethernet)[0] src = eth.src dst = eth.dst self.mac_to_port.setdefault(hex(dpid), {}) self.arp_learning.setdefault(dpid, []) self.packetToport.setdefault(dpid, {}) if dst == LLDP_MAC_NEAREST_BRIDGE: return if src in self.mac_to_port[hex(dpid)].keys(): pass else: self.mac_to_port[hex(dpid)][src] = inPort data = msg.data etherFrame = packets.get_protocol(ethernet) if etherFrame.ethertype == ether.ETH_TYPE_ARP: self.logger.debug("\n:") # arpPacket = packets.get_protocol(arp) # arpArriveTime = time.time() # srcMac = etherFrame.src # arp_dstIP = arpPacket.dst_ip dst = eth.dst self.receive_arp(datapath, packets, etherFrame, inPort, data) return 0 else: self.logger.debug("Drop packet") return 1
def _packet_in_handler(self, ev): self.logger.debug("my_arp: _packet_in_handler:") # If you hit this you might want to increase # the "miss_send_length" of your switch if ev.msg.msg_len < ev.msg.total_len: self.logger.debug("packet truncated: only %s of %s bytes", ev.msg.msg_len, ev.msg.total_len) msg = ev.msg datapath = msg.datapath # ofproto = datapath.ofproto inPort = msg.match['in_port'] packets = Packet(msg.data) dpid = datapath.id eth = packets.get_protocols(ethernet)[0] src = eth.src dst = eth.dst self.mac_to_port.setdefault(hex(dpid), {}) self.arp_learning.setdefault(dpid, []) self.packetToport.setdefault(dpid, {}) if dst == LLDP_MAC_NEAREST_BRIDGE: return if src in self.mac_to_port[hex(dpid)].keys(): pass else: self.mac_to_port[hex(dpid)][src] = inPort data = msg.data # print mac_to_port for verification # self.logger.info("MySimpleArp: self.mac_to_port") # for key, value in self.mac_to_port.items(): # print "\t", self._hostname_Check(int(str(key), 16)), value etherFrame = packets.get_protocol(ethernet) if etherFrame.ethertype == ether.ETH_TYPE_ARP: self.logger.debug("\n:") dst = eth.dst self.receive_arp(datapath, packets, etherFrame, inPort, data) return 0 else: self.logger.debug("Drop packet") return 1
def _packet_in_handler(self, ev): self.logger.debug("my_arp: _packet_in_handler:") # If you hit this you might want to increase # the "miss_send_length" of your switch if ev.msg.msg_len < ev.msg.total_len: self.logger.debug("packet truncated: only %s of %s bytes", ev.msg.msg_len, ev.msg.total_len) msg = ev.msg datapath = msg.datapath # ofproto = datapath.ofproto inPort = msg.match["in_port"] packets = Packet(msg.data) dpid = datapath.id eth = packets.get_protocols(ethernet)[0] src = eth.src dst = eth.dst self.mac_to_port.setdefault(hex(dpid), {}) self.arp_learning.setdefault(dpid, []) self.packetToport.setdefault(dpid, {}) if dst == LLDP_MAC_NEAREST_BRIDGE: return if src in self.mac_to_port[hex(dpid)].keys(): pass else: self.mac_to_port[hex(dpid)][src] = inPort data = msg.data etherFrame = packets.get_protocol(ethernet) if etherFrame.ethertype == ether.ETH_TYPE_ARP: self.logger.debug("\n:") # arpPacket = packets.get_protocol(arp) # arpArriveTime = time.time() # srcMac = etherFrame.src # arp_dstIP = arpPacket.dst_ip dst = eth.dst self.receive_arp(datapath, packets, etherFrame, inPort, data) return 0 else: self.logger.debug("Drop packet") return 1
def handle_packet_in(self, event): """ Installs flow rules for incoming packets using Dijkstra :param event: The PacketIn event :type event: EventOFPPacketIn :return: Any generated flow IDs :rtype: list """ message = event.msg datapath = message.datapath switch_id = datapath.id packet = Packet(message.data) ipv4_info = packet.get_protocol(ipv4) if ipv4_info is None: return [] out_port, src_subnet, dst_subnet = \ self.routing_algo.calculate_routing_decision( switch_id, ipv4_info.src, ipv4_info.dst ) match = OFPMatch(eth_type=ether_types.ETH_TYPE_IP, ipv4_src=src_subnet, ipv4_dst=dst_subnet) # If packets come in very quickly, the flow rule may not be installed # yet. To avoid creating new flows, we check if the flow rules were # already registered in active_flows. for existing_flow in self.active_flows.values(): if existing_flow.is_ipv4_match_equal(switch_id, match): self.send_pkt(datapath, message.data, out_port) return [] actions = [OFPActionOutput(out_port)] flow_id = self.program_flow(datapath=datapath, match=match, actions=actions, priority=10, hard_timeout=120, idle_timeout=5) self.send_pkt(datapath, message.data, out_port) return [flow_id]
def myPacketIn_handler(self,event): msg = event.msg datapath = msg.datapath print("datapathId is %d"%datapath.id) ofproto = datapath.ofproto parser = datapath.ofproto_parser data = msg.data in_port = msg.in_port self.mac_to_port.setdefault(datapath.id,{}) pkt = Packet(data) eth = pkt.get_protocol(ethernet) src =eth.src dst = eth.dst self.mac_to_port[datapath.id][src] = in_port if dst not in self.mac_to_port[datapath.id]: out_port = ofproto.OFPP_FLOOD else: out_port = self.mac_to_port[datapath.id][dst] actions = [parser.OFPActionOutput(out_port)] out_msg = parser.OFPPacketOut(datapath,msg.buffer_id,in_port,actions,data) datapath.send_msg(out_msg)
def arp_received_handler(self, ev): msg = ev.ev.msg datapath = msg.datapath in_port = msg.match['in_port'] packet = Packet(msg.data) arppkt = packet.get_protocol(arp.arp) if arppkt.opcode == arp.ARP_REQUEST: self.broadcast_to_end_nodes(msg) try: src_port, dst_port = db.handle_arp_packet(arppkt, datapath.id, in_port) self.process_end_hw_addr_flows(src_port) self.process_end_hw_addr_flows(dst_port) if src_port[0] != dst_port[0]: self.process_route(src_port, dst_port) self.process_route(dst_port, src_port) if arppkt.opcode == arp.ARP_REPLY: target_switch = self.switches[dst_port[0]].switch self.arp_packet_out(target_switch.dp, dst_port[1], msg.data) except db.ArpTableNotFoundException: pass
def packetIn_handler(self,event): msg = event.msg data = msg.data datapath = msg.datapath dapid = datapath.id ofproto = datapath.ofproto parser = datapath.ofproto_parser in_port = msg.in_port pkt = Packet(data) eth = pkt.get_protocol(ethernet) src = eth.src dst = eth.dst self.mac_to_port.setdefault(datapath.id,{}) self.mac_to_port[datapath.id].setdefault(src,in_port) if dst in self.mac_to_port[dapid]: out_port = self.mac_to_port[dapid][dst] else: out_port = ofproto.OFPP_FLOOD actions = [parser.OFPActionOutput(out_port)] pktout_msg = parser.OFPPacketOut(datapath=datapath,buffer_id=msg.buffer_id,in_port=in_port,actions=actions,data=data) datapath.send_msg(pktout_msg)
def packetIn_handler(self,event): msg = event.msg data = msg.data datapath = msg.datapath dapid = datapath.id ofproto = datapath.ofproto parser = datapath.ofproto_parser in_port = msg.in_port pkt = Packet(data) eth = pkt.get_protocol(ethernet) eth_src = eth.src eth_dst = eth.dst self.mac_to_port.setdefault(datapath.id,{}) self.mac_to_port[datapath.id][eth_src] = in_port print("eth_src: %s,eth_dst: %s"%(str(eth_src),str(eth_dst))) allPkts = dict((p.protcol_name,p) for p in pkt.protocols) if ARP in allPkts: arp_opcode = allPkts[ARP].opcode arp_src_mac = allPkts[ARP].src_mac arp_dst_mac = allPkts[ARP].dst_mac arp_src_ip = allPkts[ARP].src_ip arp_dst_ip = allPkts[ARP].dst_ip print("opcode: %s,src_mac: %s,dst_mac: %s,src_ip: %s,dst_ip: %s"%( str(arp_opcode),str(arp_src_mac),str(arp_dst_mac), str(arp_src_ip),str(arp_dst_ip))) if eth_dst in self.mac_to_port[datapath.id]: out_port = self.mac_to_port[datapath.id][eth_dst] else: out_port = ofproto.OFPP_FLOOD actions = [parser.OFPActionOutput(out_port)] pktout_msg = parser.OFPPacketOut(datapath=datapath,buffer_id=ofproto.OFP_NO_BUFFER, in_port=in_port,actions=actions,data=msg.data) datapath.send_msg(pktout_msg)
def packet_in_handler(self, ev): msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto inPort = msg.match['in_port'] if msg.reason == ofproto.OFPR_INVALID_TTL: self.ttl_exceeded_response(msg) return packet = Packet(msg.data) etherFrame = packet.get_protocol(ethernet.ethernet) if etherFrame.ethertype == ether.ETH_TYPE_ARP: self.receive_arp(datapath, packet, etherFrame, inPort) elif etherFrame.ethertype == ether.ETH_TYPE_IP: self.receive_ip(msg, inPort) else: LOG.debug("receive Unknown packet %s => %s (port%d)" %(etherFrame.src, etherFrame.dst, inPort)) LOG.debug("Drop packet") return 1 return 0
def packet_in_handler(self, ev): msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser in_port = msg.match["in_port"] pkt = packet.Packet(msg.data) eth = pkt.get_protocols(ethernet)[0] dst = eth.dst packets = Packet(msg.data) data = msg.data if eth.ethertype == ether.ETH_TYPE_ARP: arp_pkt = pkt.get_protocols(arp.arp)[0] srcMac = arp_pkt.src_mac srcIP = arp_pkt.src_ip etherFrame = packets.get_protocol(ethernet) self.receive_arp(datapath, packets, etherFrame, in_port, data) elif eth.ethertype == ether.ETH_TYPE_IP: ip = pkt.get_protocols(ipv4.ipv4)[0] srcMac = eth.src srcIP = ip.src else: return if self.isRouter(srcMac): return if srcIP not in self.hosts: self.hosts[srcIP] = {} # Always update MAC and switch-port location, just in case # DHCP reassigned the IP or the host moved self.hosts[srcIP]["mac"] = srcMac self.updateHostTable(srcIP, dpid_lib.dpid_to_str(datapath.id), in_port)
def packet_in_handler(self, ev): msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser in_port = msg.match['in_port'] pkt = packet.Packet(msg.data) eth = pkt.get_protocols(ethernet)[0] dst = eth.dst packets = Packet(msg.data) data = msg.data if eth.ethertype == ether.ETH_TYPE_ARP: arp_pkt = pkt.get_protocols(arp.arp)[0] srcMac = arp_pkt.src_mac srcIP = arp_pkt.src_ip etherFrame = packets.get_protocol(ethernet) self.receive_arp(datapath, packets, etherFrame, in_port, data) elif eth.ethertype == ether.ETH_TYPE_IP: ip = pkt.get_protocols(ipv4.ipv4)[0] srcMac = eth.src srcIP = ip.src else: return if self.isRouter(srcMac): return if srcIP not in self.hosts: self.hosts[srcIP] = {} # Always update MAC and switch-port location, just in case # DHCP reassigned the IP or the host moved self.hosts[srcIP]['mac'] = srcMac self.updateHostTable(srcIP, dpid_lib.dpid_to_str(datapath.id), in_port)
def forwarding(self, msg): packet = Packet(msg.data) ip_packet = packet.get_protocol(ipv4.ipv4) entry = self.routing.search(ip_packet.dst) if (entry[3] == None): next_hop = ip_packet.dst dir_ip = ip_packet.dst else: next_hop = entry[3] str_mask = str(entry[1]) dir_ip = IPNetwork("%s/%s" % (entry[0], str_mask)) next_mac = self.arpcache.get_mac(next_hop,entry[2]) if(next_mac != None): self.add_forwarding_flow(msg, entry[2], dir_ip, next_mac, 32) else: self.arp_request(msg.datapath, next_hop, entry[2]) self.pending.addPendingPacket(next_hop, msg, dir_ip, entry[1]) None
def packet_in(self, event): """ Handles packets sent to the controller. Used for debugging.""" msg = event.msg in_port = msg.in_port dp = msg.datapath # Assumes that datapath ID represents an ascii name switchName = dpidDecode(dp.id) packet = Packet(msg.data) # self.logger.info("packet: {}".format(msg)) ether = packet.get_protocol(ryu.lib.packet.ethernet.ethernet) ethertype = ether.ethertype if ethertype == 0x86dd: # Ignore IPv6 packets for now return in_tagged = False if ethertype == 0x8100: ethervlan = packet.get_protocol(ryu.lib.packet.vlan.vlan) in_tagged = True if ethervlan.ethertype == 0x86dd: # Ignore IPv6 return self.logger.info("VID: {}".format(ethervlan.vid)) vid = ethervlan.vid else: vid = self.switch_vlan_info[switchName].port2vid[in_port] # The next line maps the source mac address to its port self.switch_vid_mac_port[switchName][vid][ether.src] = in_port self.logger.info( " Switch {} in port {} received packet with ethertype: {}".format( switchName, in_port, hex(ethertype))) ipv4 = packet.get_protocol(ryu.lib.packet.ipv4.ipv4) if ipv4: self.logger.info("IPv4 src: {} dst: {}".format(ipv4.src, ipv4.dst)) # Check to see if we can create a direct flow rule dst = ether.dst if dst in self.switch_vid_mac_port[switchName][vid]: datapath = self.switches[switchName] ofproto = datapath.ofproto parser = datapath.ofproto_parser command = ofproto.OFPFC_ADD out_port = self.switch_vid_mac_port[switchName][vid][dst] if in_port == out_port: return out_tagged = out_port in self.switch_vlan_info[ switchName].taggedPorts self.logger.info( "Creating learned flow switch {}, VID: {}, DST_MAC: {}, in port {}, out port {}, in tagged: {}, out_tagged {}" .format(switchName, vid, dst, in_port, out_port, in_tagged, out_tagged)) if in_tagged: match = parser.OFPMatch(in_port=in_port, dl_vlan=vid, dl_dst=dst) if out_tagged: actions = [parser.OFPActionOutput(out_port)] else: actions = [ parser.OFPActionStripVlan(), parser.OFPActionOutput(out_port) ] else: match = parser.OFPMatch(in_port=in_port, dl_dst=dst) if out_tagged: actions = [ parser.OFPActionVlanVid(vid), parser.OFPActionOutput(out_port) ] else: actions = [parser.OFPActionOutput(out_port)] mod = parser.OFPFlowMod( datapath=datapath, match=match, cookie=0, command=command, idle_timeout=0, hard_timeout=0, priority=20, # Make priority higher than flood flows flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions) datapath.send_msg(mod)
def packet_in_handler(self, ev): msg = ev.msg pkt = Packet(msg.data) dp = msg.datapath buffer_id = msg.buffer_id in_port = msg.match['in_port'] match_old = msg.match ofp = dp.ofproto ofp_parser = dp.ofproto_parser #pdb.set_trace() actions = [] instructions = [] #dp.send_msg(out) ip = pkt.get_protocol(ipv4.ipv4) tcp1 = pkt.get_protocol(tcp.tcp) pk_tcp = pkt.get_protocol(tcp.tcp) #self.total = self.total + pk_tcp.window_size eth = pkt.get_protocol(ethernet.ethernet) dst = eth.dst # destination MAC src = eth.src # source MAC actions.append(ofp_parser.OFPActionOutput(ofp.OFPP_FLOOD)) out = ofp_parser.OFPPacketOut( datapath=dp, buffer_id=msg.buffer_id, in_port=in_port, actions=actions) dp.send_msg(out) op = randint(0,1) actions = [] dpid = dp.id self.mac_to_port.setdefault(dpid, {}) # 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]: #if destination is know send it to the right port out_port = self.mac_to_port[dpid][dst] else: #otherwise, flood out_port = ofp.OFPP_FLOOD if ip and tcp1: #print ip ip.ttl = op #print ip #actions.append(ofp.OFPActionDecNwTtl()) if ip.ttl == 1: self.tabelaPacotesGrandes.append(ip) self.total = self.total + ip.total_length print "----------------------------------TTL COM 1----------------------------------" else: self.tabelaPacotesPequenos.append(ip) self.total = self.total + ip.total_length print "----------------------------------TTL COM 0----------------------------------" self._send_packet(pkt) if ip and tcp1: actions.append(dp.ofproto_parser.OFPActionSetNwTtl(10)) match = dp.ofproto_parser.OFPMatch(dl_dst=ip.dst,dl_src=ip.src,src_port = tcp1.src_port, dst_port =tcp1.dst_port) instructions =[OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS, actions=actions)] prio = 100 else: match = match_old prio = 1 actions.append(dp.ofproto_parser.OFPActionOutput(out_port)) if out_port != ofp.OFPP_FLOOD: mod = dp.ofproto_parser.OFPFlowMod(datapath=dp, cookie=0, cookie_mask=0, table_id=0, command=0,buffer_id = buffer_id, idle_timeout=0, hard_timeout=0, priority=prio, out_port=out_port, flags=ofp.OFPFF_SEND_FLOW_REM, match=match, instructions=instructions) dp.send_msg(mod) out = dp.ofproto_parser.OFPPacketOut(datapath=dp, in_port=in_port,buffer_id = buffer_id, actions=actions) dp.send_msg(out)
def packet_In_handler(self, event): msg = event.msg datapath = msg.datapath if datapath.id not in self.allDatapath: self.allDatapath[datapath.id] = datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser match = msg.match data = msg.data in_port = match["in_port"] pkt = Packet(data) eth = pkt.get_protocol(ethernet) src = eth.src dst = eth.dst self.mac_to_port.setdefault(datapath.id, {}) self.mac_to_port[datapath.id][src] = in_port if dst in self.mac_to_port[datapath.id]: out_port = self.mac_to_port[datapath.id][dst] else: out_port = ofproto.OFPP_FLOOD actions = [parser.OFPActionOutput(out_port)] match = parser.OFPMatch(in_port=in_port, eth_src=src, eth_dst=dst) instructions = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions=actions)] self.addFlow(datapath=datapath, priority=1, match=match, instructions=instructions) out_msg = parser.OFPPacketOut( datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions, data=None ) datapath.send_msg(out_msg)