def learn_and_forward(self, dpid, inport, packet, buf, bufid): """Learn MAC src port mapping, then flood or send unicast.""" print inport print mac_to_str(packet.src) print mac_to_str(packet.dst) # Initial hub behavior: flood packet out everything but input port. # Comment out the line below when starting the exercise. # self.send_openflow(dpid, bufid, buf, openflow.OFPP_FLOOD, inport) # Starter psuedocode for learning switch exercise below: you'll need to # replace each pseudocode line with more specific Python code. # Learn the port for the source MAC #self.mac_to_port = <fill in> src_mac = mac_to_int(packet.src) self.mac_to_port[src_mac] = inport dst_mac = mac_to_int(packet.dst) if ( dst_mac in self.mac_to_port ): print 'find a match' # Send unicast packet to known output port outport = self.mac_to_port[dst_mac] actions = [[openflow.OFPAT_OUTPUT, [0, outport]]] self.send_openflow(dpid, bufid, buf, actions, inport) else: print 'no match, flood to all ports' #flood packet out everything but the input port self.send_openflow(dpid, bufid, buf, openflow.OFPP_FLOOD, inport)
def datapath_join_callback(nddi, dp_id, ip_address, stats): # NOX gives us the IP address in the native order, move it to network tmp = struct.pack("@I", ip_address) ip_address = struct.unpack("!I", tmp)[0] if not dp_id in switches: switches.append(dp_id) ports = stats['ports'] #print str(ports) #--- we know we are going to need to get the set of flows so lets just do that now nddi.collection_epoch += 1 flow = of.ofp_match() flow.wildcards = 0xffffffff nddi.send_flow_stats_request(dp_id, flow, 0xff) port_list = [] for p in ports: port = p port['hw_addr'] = mac_to_int(p['hw_addr']) port_list.append(port) #print str(port_list) nddi.rmqi_rpc.emit_signal('datapath_join', 'OF.NOX.event', dpid=dp_id, ip=ip_address, ports=ports)
def datapath_join_callback(nddi, dp_id, ip_address, stats): # NOX gives us the IP address in the native order, move it to network tmp = struct.pack("@I", ip_address) ip_address = struct.unpack("!I", tmp)[0] if not dp_id in switches: switches.append(dp_id) ports = stats['ports']; #print str(ports) #--- we know we are going to need to get the set of flows so lets just do that now nddi.collection_epoch += 1 flow = of.ofp_match() flow.wildcards = 0xffffffff nddi.send_flow_stats_request(dp_id, flow,0xff) port_list = [] for p in ports: port = p port['hw_addr'] = mac_to_int(p['hw_addr']) port_list.append(port) #print str(port_list) nddi.rmqi_rpc.emit_signal('datapath_join','OF.NOX.event', dpid=dp_id, ip=ip_address, ports=ports )
def packet_in_callback(self, dpid, inport, reason, len, bufid, packet): """Packet-in handler""" if not packet.parsed: log.debug('Ignoring incomplete packet') else: if packet.type == packet.LLDP_TYPE: #ignoring lldp packet. pass #elif packet.find('ip'): else: try: ip = ip_to_str(packet.next.dstip) except AttributeError: return CONTINUE if ip == '10.0.2.254': print 'blaaaa' if ip == me.multicast_ip: if not self.overmind.fix_switch(dpid): event = packet.next.next.payload[0] if event == ADD_HOST: self.overmind.add_host(dpid,inport,mac_to_int(packet.src)) elif event == REM_HOST: self.overmind.remove_host(dpid, mac_to_int(packet.src)) elif event == SOURCE_EVENT: self.overmind.change_src(dpid, inport) else: print 'Unknown event, discarding packet' #print 'Packet-in no router', dpid, '--Fonte:', mac_to_str(packet.src), '--Destino:', mac_to_str(packet.dst) #print 'In-Port:', inport pass """if self.im == None: #self.im = InstallationManager(self.topology) self.im.nox = self #self.im.samples_number = 10 self.im.collect_begin_installs() self.install_routes() self.im.collect_end_installs() self.im.start() # else: # self.install_routes() """ return CONTINUE
def port_status_callback(sg, dp_id, ofp_port_reason, attrs): attr_dict = {} if attrs: attr_dict = dbus.Dictionary(attrs, signature='sv') #--- convert mac attr_dict['hw_addr'] = dbus.UInt64(mac_to_int(attr_dict['hw_addr'])) #--- generate signal sg.port_status(dp_id, ofp_port_reason, attr_dict)
def port_status_callback(sg,dp_id, ofp_port_reason, attrs): attr_dict = {} if attrs: attr_dict = dbus.Dictionary(attrs, signature='sv') #--- convert mac attr_dict['hw_addr'] = dbus.UInt64(mac_to_int(attr_dict['hw_addr'])) #--- generate signal sg.port_status(dp_id,ofp_port_reason,attr_dict)
def port_status_callback(nddi, dp_id, ofp_port_reason, attrs): attr_dict = attrs #--- convert mac attr_dict['hw_addr'] = mac_to_int(attrs['hw_addr']) #--- generate signal nddi.rmqi_rpc.emit_signal('port_status', 'OF.NOX.event', dpid=dp_id, ofp_port_reason=ofp_port_reason, attrs=attr_dict)
def port_status_callback(nddi, dp_id, ofp_port_reason, attrs): attr_dict = attrs #--- convert mac attr_dict['hw_addr'] = mac_to_int(attrs['hw_addr']) #--- generate signal nddi.rmqi_rpc.emit_signal('port_status','OF.NOX.event', dpid=dp_id, ofp_port_reason=ofp_port_reason, attrs=attr_dict )
def learn_and_forward(self, dpid, inport, packet, buf, bufid): """Learn MAC src port mapping, then flood or send unicast.""" if dpid not in self.mac_to_port: self.mac_to_port[dpid] = {} # Initial hub behavior: flood packet out everything but input port. # Comment out the line below when starting the exercise. # self.send_openflow(dpid, bufid, buf, openflow.OFPP_FLOOD, inport) # Starter psuedocode for learning switch exercise below: you'll need to # replace each pseudocode line with more specific Python code. # Learn the port for the source MAC #self.mac_to_port = <fill in> src_mac = mac_to_int(packet.src) self.mac_to_port[dpid][src_mac] = inport dst_mac = mac_to_int(packet.dst) if ( dst_mac in self.mac_to_port[dpid] ): print 'find a match' # Send unicast packet to known output port outport = self.mac_to_port[dpid][dst_mac] actions = [[openflow.OFPAT_OUTPUT, [0, outport]]] self.send_openflow(dpid, bufid, buf, actions, inport) # Later, only after learning controller works: # push down flow entry and remove the send_openflow command above. attrs = extract_flow(packet) attrs[core.IN_PORT] = inport attrs[core.DL_DST] = packet.dst idle_timeout = 600 hard_timeout = 3600 self.install_datapath_flow(dpid, attrs, idle_timeout, hard_timeout, actions, bufid, openflow.OFP_DEFAULT_PRIORITY, inport, buf) else: print 'no match, flood to all ports' #flood packet out everything but the input port self.send_openflow(dpid, bufid, buf, openflow.OFPP_FLOOD, inport)
def do_l2_learning(dpid, inport, packet): global inst # learn MAC on incoming port srcaddr = packet.src.tostring() if ord(srcaddr[0]) & 1: return if inst.st[dpid].has_key(srcaddr): dst = inst.st[dpid][srcaddr] if dst[0] != inport: log.msg('MAC has moved from '+str(dst)+'to'+str(inport), system='pyswitch') else: return else: log.msg('learned MAC '+mac_to_str(packet.src)+' on %d %d'% (dpid,inport), system="pyswitch") # learn or update timestamp of entry inst.st[dpid][srcaddr] = (inport, time(), packet) # Replace any old entry for (switch,mac). mac = mac_to_int(packet.src)
def do_l2_learning(dpid, inport, packet): global inst # learn MAC on incoming port srcaddr = packet.src.tostring() if ord(srcaddr[0]) & 1: return if inst.st[dpid].has_key(srcaddr): dst = inst.st[dpid][srcaddr] if dst[0] != inport: pass #('MAC has moved from '+str(dst)+'to'+str(inport), system='pyswitch') else: return else: pass #('learned MAC '+mac_to_str(packet.src)+' on %d %d'% (dpid,inport), system="pyswitch") # learn or update timestamp of entry inst.st[dpid][srcaddr] = (inport, time(), packet) # Replace any old entry for (switch,mac). mac = mac_to_int(packet.src)
def datapath_join_callback(ref, sg, dp_id, ip_address, stats): # NOX gives us the IP address in the native order, move it to network tmp = struct.pack("@I", ip_address) ip_address = struct.unpack("!I", tmp)[0] if not dp_id in switches: switches.append(dp_id) ports = stats['ports'] #print str(ports) #--- we know we are going to need to get the set of flows so lets just do that now sg.collection_epoch += 1 flow = of.ofp_match() flow.wildcards = 0xffffffff ref.send_flow_stats_request(dp_id, flow, 0xff) port_list = [] for i in range(0, len(ports)): port = {} #ports[i] port['name'] = ports[i]['name'] port['hw_addr'] = dbus.UInt64(mac_to_int(ports[i]['hw_addr'])) port['port_no'] = dbus.UInt16(ports[i]['port_no']) #assert(ports[i]['state'] <= 4294967295) port['config'] = ports[i]['config'] #dbus.UInt32(ports[i]['config']) port['state'] = dbus.UInt32(ports[i]['state']) port['curr'] = dbus.UInt32(ports[i]['curr']) port['advertised'] = dbus.UInt32(ports[i]['advertised']) port['supported'] = dbus.UInt32(ports[i]['supported']) port['peer'] = dbus.UInt32(ports[i]['peer']) #print str(port['config']) + " vs " + str(ports[i]['config']) port_dict = dbus.Dictionary(port, signature='sv') port_list.append(port) #print str(port_list) sg.datapath_join(dp_id, ip_address, port_list)
def datapath_join_callback(ref,sg,dp_id,ip_address,stats): # NOX gives us the IP address in the native order, move it to network tmp = struct.pack("@I", ip_address) ip_address = struct.unpack("!I", tmp)[0] if not dp_id in switches: switches.append(dp_id) ports = stats['ports']; #print str(ports) #--- we know we are going to need to get the set of flows so lets just do that now sg.collection_epoch += 1 flow = of.ofp_match() flow.wildcards = 0xffffffff ref.send_flow_stats_request(dp_id, flow,0xff) port_list = [] for i in range(0, len(ports)): port = {}#ports[i] port['name'] = ports[i]['name'] port['hw_addr'] = dbus.UInt64(mac_to_int(ports[i]['hw_addr'])) port['port_no'] = dbus.UInt16(ports[i]['port_no']) #assert(ports[i]['state'] <= 4294967295) port['config'] = ports[i]['config'] #dbus.UInt32(ports[i]['config']) port['state'] = dbus.UInt32(ports[i]['state']) port['curr'] = dbus.UInt32(ports[i]['curr']) port['advertised'] = dbus.UInt32(ports[i]['advertised']) port['supported'] = dbus.UInt32(ports[i]['supported']) port['peer'] = dbus.UInt32(ports[i]['peer']) #print str(port['config']) + " vs " + str(ports[i]['config']) port_dict = dbus.Dictionary(port, signature='sv') port_list.append(port) #print str(port_list) sg.datapath_join(dp_id,ip_address,port_list)
def learn_and_forward(self, dpid, inport, packet, buf, bufid): """Learn MAC src port mapping, then flood or send unicast.""" ip_header = packet.find('ipv4') idle_timeout = 600 hard_timeout = 3600 attrs = extract_flow(packet) attrs[core.IN_PORT] = inport attrs[core.DL_DST] = packet.dst if dpid == 1: # policies for switch 1 if ip_header is not None: ipstr = ip_to_str(ip_header.dstip) if ipstr == "10.0.0.4": outport = 1 actions = [[openflow.OFPAT_OUTPUT, [0, outport]]] self.send_openflow(dpid, bufid, buf, actions, inport) #self.install_datapath_flow(dpid, attrs, idle_timeout, hard_timeout, actions, bufid, openflow.OFP_DEFAULT_PRIORITY, inport, buf) elif ipstr == "10.0.0.5": outport = 2 actions = [[openflow.OFPAT_OUTPUT, [0, outport]]] self.send_openflow(dpid, bufid, buf, actions, inport) #self.install_datapath_flow(dpid, attrs, idle_timeout, hard_timeout, actions, bufid, openflow.OFP_DEFAULT_PRIORITY, inport, buf) elif ipstr == "10.0.0.6": outport = 3 actions = [[openflow.OFPAT_OUTPUT, [0, outport]]] self.send_openflow(dpid, bufid, buf, actions, inport) #self.install_datapath_flow(dpid, attrs, idle_timeout, hard_timeout, actions, bufid, openflow.OFP_DEFAULT_PRIORITY, inport, buf) elif ipstr == "10.0.0.7": outport = 4 actions = [[openflow.OFPAT_OUTPUT, [0, outport]]] self.send_openflow(dpid, bufid, buf, actions, inport) #self.install_datapath_flow(dpid, attrs, idle_timeout, hard_timeout, actions, bufid, openflow.OFP_DEFAULT_PRIORITY, inport, buf) else: if mac_to_int(packet.src) == 4: outport = [2,3] actions = [[openflow.OFPAT_OUTPUT, [0, 2]],[openflow.OFPAT_OUTPUT, [0, 3]]] elif mac_to_int(packet.src) == 5: outport = [1,3] actions = [[openflow.OFPAT_OUTPUT, [0, 1]],[openflow.OFPAT_OUTPUT, [0, 3]]] else: outport = [1,2] actions = [[openflow.OFPAT_OUTPUT, [0, 1]],[openflow.OFPAT_OUTPUT, [0, 2]]] self.send_openflow(dpid, bufid, buf, actions, inport) if dpid == 2: # policies for switch 2 if mac_to_int(packet.src) == 7: if ip_header is not None: ipstr = ip_to_str(ip_header.dstip) if ipstr == "10.0.0.4" or ipstr == "10.0.0.5": outport = 1 actions = [[openflow.OFPAT_OUTPUT, [0, outport]]] self.send_openflow(dpid, bufid, buf, actions, inport) #self.install_datapath_flow(dpid, attrs, idle_timeout, hard_timeout, actions, bufid, openflow.OFP_DEFAULT_PRIORITY, inport, buf) elif ipstr == "10.0.0.6": outport = 3 actions = [[openflow.OFPAT_OUTPUT, [0, outport]]] self.send_openflow(dpid, bufid, buf, actions, inport) #self.install_datapath_flow(dpid, attrs, idle_timeout, hard_timeout, actions, bufid, openflow.OFP_DEFAULT_PRIORITY, inport, buf) else: actions = [[openflow.OFPAT_OUTPUT, [0, 1]], [openflow.OFPAT_OUTPUT, [0, 3]]] outport = [1,3] self.send_openflow(dpid, bufid, buf, actions, inport) else: if ip_header is not None: ipstr = ip_to_str(ip_header.dstip) if ipstr == "10.0.0.4" or ipstr == "10.0.0.5": outport = 1 actions = [[openflow.OFPAT_OUTPUT, [0, outport]]] self.send_openflow(dpid, bufid, buf, actions, inport) #self.install_datapath_flow(dpid, attrs, idle_timeout, hard_timeout, actions, bufid, openflow.OFP_DEFAULT_PRIORITY, inport, buf) elif ipstr == "10.0.0.6": outport = 3 actions = [[openflow.OFPAT_OUTPUT, [0, outport]]] self.send_openflow(dpid, bufid, buf, actions, inport) #self.install_datapath_flow(dpid, attrs, idle_timeout, hard_timeout, actions, bufid, openflow.OFP_DEFAULT_PRIORITY, inport, buf) elif ipstr == "10.0.0.7": outport = 4 actions = [[openflow.OFPAT_OUTPUT, [0, outport]]] self.send_openflow(dpid, bufid, buf, actions, inport) #self.install_datapath_flow(dpid, attrs, idle_timeout, hard_timeout, actions, bufid, openflow.OFP_DEFAULT_PRIORITY, inport, buf) else: if mac_to_int(packet.src) == 4 or mac_to_int(packet.src) == 5: actions = [[openflow.OFPAT_OUTPUT, [0, 3]], [openflow.OFPAT_OUTPUT, [0, 4]]] outport = [3,4] elif mac_to_int(packet.src) == 6: actions = [[openflow.OFPAT_OUTPUT, [0, 1]], [openflow.OFPAT_OUTPUT, [0, 4]]] outport = [1,4] self.send_openflow(dpid, bufid, buf, actions, inport) if dpid == 3: # policies for switch 3 ipstr = ip_to_str(ip_header.dstip) if ipstr == "10.0.0.4" or ipstr == "10.0.0.5": outport = 2 actions = [[openflow.OFPAT_OUTPUT, [0, outport]]] self.send_openflow(dpid, bufid, buf, actions, inport) #self.install_datapath_flow(dpid, attrs, idle_timeout, hard_timeout, actions, bufid, openflow.OFP_DEFAULT_PRIORITY, inport, buf) else: outport = 1 actions = [[openflow.OFPAT_OUTPUT, [0, outport]]] self.send_openflow(dpid, bufid, buf, actions, inport) #self.install_datapath_flow(dpid, attrs, idle_timeout, hard_timeout, actions, bufid, openflow.OFP_DEFAULT_PRIORITY, inport, buf) message = " packet to host " + str(mac_to_int(packet.dst)) + " from host " + str(mac_to_int(packet.src)) + " go to port " + str(outport) + " of switch " + str(dpid) log.info(message)