def packet_in_callback(self, dpid, inport, reason, len, bufid, packet): if packet.type == Globals.ARP_TYPE: # ARP Response from replicas for our ARP Requests if packet.dst == octstr_to_array(Globals.VMAC): self.foundMac(dpid, mac_to_str(packet.src), ip_to_str(packet.next.protosrc), inport) # Request for VIP, respond with ARP Response elif packet.next.protodst == ipstr_to_int(Globals.VIP): Globals.STATSLOG.write(mac_to_str(dpid) + ' received ' + ip_to_str(packet.next.protosrc) + '\n') srcIP = ip_to_str(packet.next.protosrc) # Install Rewrite Rules (flow, defaultActions, rewriteActions) = IPs.get_forwarding_srcrule(srcIP, mac_to_str(packet.src), Globals.VMAC, Globals.VIP, inport) Multipath.install_microflow_flow(flow, openflow.OFP_FLOW_PERMANENT, openflow.OFP_FLOW_PERMANENT, defaultActions, None, openflow.OFP_DEFAULT_PRIORITY + 20, 0, None, dpid, inport, rewriteActions) # Response arpResponse = Arps.create_virtual_arp_response(packet, octstr_to_array(Globals.VMAC), ipstr_to_int(Globals.VIP)) Multipath.send(dpid, None, arpResponse, [[openflow.OFPAT_OUTPUT, [0, inport]]], openflow.OFPP_CONTROLLER) ip = ip_to_str(packet.next.protosrc) for i, switchJ in enumerate(Globals.SWITCHES): if switchJ['mac'] == dpid: found = False for i, client in enumerate(switchJ['clients']): if client['ip'] == ip: found = True if not found: Globals.STATSLOG.write(mac_to_str(dpid) + ' found ' + ip + '\n') switchJ['clients'].append({'port': inport, 'ip': ip, 'oldCount': 0L, 'newCount': 0L, 'avg': 0L}) elif packet.src != octstr_to_array(Globals.VMAC): Globals.STATSLOG.write(mac_to_str(dpid) + ' received REQ ' + ip_to_str(packet.next.protosrc) + '\n') arpResponse = Arps.create_arp_response(packet) Multipath.send(dpid, None, arpResponse, [[openflow.OFPAT_OUTPUT, [0, inport]]], openflow.OFPP_CONTROLLER) elif packet.type == Globals.IP_TYPE: if reason == openflow.OFPR_ACTION: IPTransition.handleControllerAction(packet)
def installMicroflowRule(self, packet, replica): Globals.RULESLOG.write('Microflow for replica ' + str(replica) +'\n') for i in range(0, Globals.NUMREPLICAS): if Globals.REPLICAS[i]['no'] == replica: Globals.RULESLOG.write('Installed MICROFLOW Rule ' + str(packet.next.srcip) + '\n') (flow, defaultActions, rewriteActions) = IPs.get_microflow_dstrule(packet, Globals.REPLICAS[i]['mac'], Globals.REPLICAS[i]['ip'], replica) self.Multipath.install_replica_flow(flow, Globals.SOFT_TIMEOUT, openflow.OFP_FLOW_PERMANENT, defaultActions, None, openflow.OFP_DEFAULT_PRIORITY + 10, 0, None, replica, rewriteActions)
def actionDeleteRule(rule): Globals.INSTALLLOG.write("***Removing Rule: " + str(rule) + '\n') for i in range(0, Globals.NUMREPLICAS): if Globals.REPLICAS[i]['no'] == rule['replica']: (flow, defaultActions, rewriteActions) = IPs.get_forwarding_dstrule( ipstr_to_int(rule['ip']), rule['wild'], Globals.VIP, Globals.REPLICAS[i]['mac'], Globals.REPLICAS[i]['ip'], Globals.REPLICAS[i]['port']) Multipath.delete_flow(flow, rule['replica'])
def deleteRule(self, rule): for i in range(0, Globals.NUMREPLICAS): if Globals.REPLICAS[i]['no'] == rule['Replica']: Globals.RULESLOG.write('Deleted OLD Rule ' + self.ruleToString(rule) + '\n') (flow, defaultActions, rewriteActions) = IPs.get_forwarding_dstrule( rule['IP'], rule['NumWild'], Globals.VIP, Globals.REPLICAS[i]['mac'], Globals.REPLICAS[i]['ip'], Globals.REPLICAS[i]['port']) self.Multipath.delete_flow(flow, rule['Replica'])
def actionTransitionRule(rule): Globals.INSTALLLOG.write("***Transition Rule: " + str(rule) + '\n') (flow, defaultActions, rewriteActions) = IPs.get_controller_dstrule(ipstr_to_int(rule['ip']), rule['wild'], Globals.VIP) Multipath.install_controller_flow(flow, openflow.OFP_FLOW_PERMANENT, openflow.OFP_FLOW_PERMANENT, defaultActions, None, openflow.OFP_DEFAULT_PRIORITY, 0, None, rule['replica'], rewriteActions) Globals.COMPONENT.post_callback(Globals.HARD_TIMEOUT, lambda: hardTimeoutHandler(rule))
def actionInstallMicroflow(packet, replica): Globals.INSTALLLOG.write("***Microflow " + ip_to_str(packet.next.srcip) + " to replica " + str(replica) + '\n') for i in range(0, Globals.NUMREPLICAS): if Globals.REPLICAS[i]['no'] == replica: (flow, defaultActions, rewriteActions) = IPs.get_microflow_dstrule( packet, Globals.REPLICAS[i]['mac'], Globals.REPLICAS[i]['ip'], replica) Multipath.install_replica_flow(flow, Globals.SOFT_TIMEOUT, openflow.OFP_FLOW_PERMANENT, defaultActions, None, openflow.OFP_DEFAULT_PRIORITY + 10, 0, None, replica, rewriteActions)
def installRuleToController(self, rule): Globals.RULESLOG.write('Installed CONTROLLER Rule ' + self.ruleToString(rule) + '\n') (flow, defaultActions, rewriteActions) = IPs.get_controller_dstrule(rule['IP'], rule['NumWild'], Globals.VIP) self.Multipath.install_controller_flow(flow, Globals.CACHE_TIMEOUT, openflow.OFP_FLOW_PERMANENT, defaultActions, None, openflow.OFP_DEFAULT_PRIORITY, 0, None, rule['Replica'], rewriteActions)
def installMicroflowRule(self, packet, replica): Globals.RULESLOG.write('Microflow for replica ' + str(replica) + '\n') for i in range(0, Globals.NUMREPLICAS): if Globals.REPLICAS[i]['no'] == replica: Globals.RULESLOG.write('Installed MICROFLOW Rule ' + str(packet.next.srcip) + '\n') (flow, defaultActions, rewriteActions) = IPs.get_microflow_dstrule( packet, Globals.REPLICAS[i]['mac'], Globals.REPLICAS[i]['ip'], replica) self.Multipath.install_replica_flow( flow, Globals.SOFT_TIMEOUT, openflow.OFP_FLOW_PERMANENT, defaultActions, None, openflow.OFP_DEFAULT_PRIORITY + 10, 0, None, replica, rewriteActions)
def installPermRule(self, rule): for i in range(0, Globals.NUMREPLICAS): if Globals.REPLICAS[i]['no'] == rule['Replica']: Globals.RULESLOG.write('Installed NEW Rule ' + self.ruleToString(rule) + '\n') (flow, defaultActions, rewriteActions) = IPs.get_forwarding_dstrule( rule['IP'], rule['NumWild'], Globals.VIP, Globals.REPLICAS[i]['mac'], Globals.REPLICAS[i]['ip'], Globals.REPLICAS[i]['port']) self.Multipath.install_replica_flow( flow, Globals.CACHE_TIMEOUT, openflow.OFP_FLOW_PERMANENT, defaultActions, None, openflow.OFP_DEFAULT_PRIORITY, 0, None, rule['Replica'], rewriteActions)
def actionInstallRule(rule): Globals.INSTALLLOG.write("***Installing Rule: " + str(rule) + '\n') for i in range(0, Globals.NUMREPLICAS): if Globals.REPLICAS[i]['no'] == rule['replica']: (flow, defaultActions, rewriteActions) = IPs.get_forwarding_dstrule( ipstr_to_int(rule['ip']), rule['wild'], Globals.VIP, Globals.REPLICAS[i]['mac'], Globals.REPLICAS[i]['ip'], Globals.REPLICAS[i]['port']) Multipath.install_replica_flow(flow, openflow.OFP_FLOW_PERMANENT, openflow.OFP_FLOW_PERMANENT, defaultActions, None, openflow.OFP_DEFAULT_PRIORITY, 0, None, rule['replica'], rewriteActions)
def getMultiFlow(flow, numRules): numBits = 0 found = False while numRules > 1: numBits += 1 if numRules % 2 != 0: found = True numRules = numRules / 2 if found: numBits += 1 if not flow.has_key(NW_SRC_N_WILD): return [flow] flowList = [] for i in range(0, 2 ** numBits): newFlow = IPs.copyflow(flow) numWild = newFlow[NW_SRC_N_WILD] - numBits newFlow[NW_SRC] = ((newFlow[NW_SRC] >> numWild) + i) << numWild newFlow[NW_SRC_N_WILD] = numWild flowList.append(newFlow) return flowList
def getMultiFlow(flow, numRules): numBits = 0 found = False while numRules > 1: numBits += 1 if numRules % 2 != 0: found = True numRules = numRules / 2 if found: numBits += 1 if not flow.has_key(NW_SRC_N_WILD): return [flow] flowList = [] for i in range(0, 2**numBits): newFlow = IPs.copyflow(flow) numWild = newFlow[NW_SRC_N_WILD] - numBits newFlow[NW_SRC] = ((newFlow[NW_SRC] >> numWild) + i) << numWild newFlow[NW_SRC_N_WILD] = numWild flowList.append(newFlow) return flowList
def actionInstallRule(rule): Globals.INSTALLLOG.write("***Installing Rule: " + str(rule) + '\n') for i in range(0, Globals.NUMREPLICAS): if Globals.REPLICAS[i]['no'] == rule['replica']: (flow, defaultActions, rewriteActions) = IPs.get_forwarding_dstrule(ipstr_to_int(rule['ip']), rule['wild'], Globals.VIP, Globals.REPLICAS[i]['mac'], Globals.REPLICAS[i]['ip'], Globals.REPLICAS[i]['port']) Multipath.install_replica_flow(flow, openflow.OFP_FLOW_PERMANENT, openflow.OFP_FLOW_PERMANENT, defaultActions, None, openflow.OFP_DEFAULT_PRIORITY, 0, None, rule['replica'], rewriteActions)
def actionTransitionRule(rule): Globals.INSTALLLOG.write("***Transition Rule: " + str(rule) + '\n') (flow, defaultActions, rewriteActions) = IPs.get_controller_dstrule(ipstr_to_int(rule['ip']), rule['wild'], Globals.VIP) Multipath.install_controller_flow(flow, openflow.OFP_FLOW_PERMANENT, openflow.OFP_FLOW_PERMANENT, defaultActions, None, openflow.OFP_DEFAULT_PRIORITY, 0, None, rule['replica'], rewriteActions) Globals.COMPONENT.post_callback(Globals.HARD_TIMEOUT, lambda : hardTimeoutHandler(rule))
def actionDeleteRule(rule): Globals.INSTALLLOG.write("***Removing Rule: " + str(rule) + '\n') for i in range(0, Globals.NUMREPLICAS): if Globals.REPLICAS[i]['no'] == rule['replica']: (flow, defaultActions, rewriteActions) = IPs.get_forwarding_dstrule(ipstr_to_int(rule['ip']), rule['wild'], Globals.VIP, Globals.REPLICAS[i]['mac'], Globals.REPLICAS[i]['ip'], Globals.REPLICAS[i]['port']) Multipath.delete_flow(flow, rule['replica'])
def packet_in_callback(self, dpid, inport, reason, len, bufid, packet): # Install initial setup forwarding rules if self.Init == True: self.post_callback(Globals.PORT_STATS_PERIOD, lambda : self.port_timer()) self.Init = False # Controller Attention if reason == openflow.OFPR_ACTION: self.myIPTransition.controllerAction(packet) # Incoming ARPs elif packet.type == Globals.ARP_TYPE: # Response to our ARP requests # 1. Record MAC # 2. Install appropriate IP Forwarding Rule: Change dest VIP to dest Replica i if packet.dst == octstr_to_array(Globals.VMAC): foundMacs = True for i in range(0, Globals.NUMREPLICAS): if ((packet.next.protosrc == ipstr_to_int(Globals.REPLICAS[i]['ip'])) and (Globals.REPLICAS[i]['mac'] != mac_to_str(packet.src))): Globals.REPLICAS[i]['mac'] = mac_to_str(packet.src) Globals.REPLICAS[i]['port'] = inport for j, switchJ in enumerate(Globals.SWITCHES): if switchJ['mac'] == dpid: switchJ['replicas'].append(Globals.REPLICAS[i]['no']) if (Globals.REPLICAS[i]['mac'] == ''): foundMacs = False if foundMacs == True and self.AllMacsFound == False: Globals.log.info('REPLICAS ' + str(Globals.REPLICAS)) Globals.log.info('SWITCHES ' + str(Globals.SWITCHES)) Globals.log.info('\n') Globals.log.info('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>') rulePairList = self.myEvalRules.updateAlphas() self.myIPTransition.installRules(rulePairList) Globals.log.info('<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<') Globals.log.info('\n') self.AllMacsFound = True # Requests for VIP respond with ARP Response elif packet.next.protodst == ipstr_to_int(Globals.VIP): srcIP = ip_to_str(packet.next.protosrc) # IP Rules (flow, defaultActions, rewriteActions) = IPs.get_forwarding_srcrule(srcIP, mac_to_str(packet.src), Globals.VMAC, Globals.VIP, inport) self.Multipath.install_microflow_flow(flow, openflow.OFP_FLOW_PERMANENT, openflow.OFP_FLOW_PERMANENT, defaultActions, None, openflow.OFP_DEFAULT_PRIORITY, 0, None, dpid, inport, rewriteActions) arpResponse = Arps.create_virtual_arp_response(packet, octstr_to_array(Globals.VMAC), ipstr_to_int(Globals.VIP)) self.Multipath.send(dpid, None, arpResponse, [[openflow.OFPAT_OUTPUT, [0, inport]]], openflow.OFPP_CONTROLLER) else: arpResponse = Arps.create_arp_response(packet) self.Multipath.send(dpid, None, arpResponse, [[openflow.OFPAT_OUTPUT, [0, inport]]], openflow.OFPP_CONTROLLER) elif packet.type == Globals.IP_TYPE: Globals.log.info("Warning! S1 SHOULD NOT BE RECEIVING IP Traffic!!!") # self.myIPTransition.controllerAction(packet) return CONTINUE
def actionInstallMicroflow(packet, replica): Globals.INSTALLLOG.write("***Microflow " + ip_to_str(packet.next.srcip) + " to replica " + str(replica) + '\n') for i in range(0, Globals.NUMREPLICAS): if Globals.REPLICAS[i]['no'] == replica: (flow, defaultActions, rewriteActions) = IPs.get_microflow_dstrule(packet, Globals.REPLICAS[i]['mac'], Globals.REPLICAS[i]['ip'], replica) Multipath.install_replica_flow(flow, Globals.SOFT_TIMEOUT, openflow.OFP_FLOW_PERMANENT, defaultActions, None, openflow.OFP_DEFAULT_PRIORITY + 10, 0, None, replica, rewriteActions)
def installPermRule(self, rule): for i in range(0, Globals.NUMREPLICAS): if Globals.REPLICAS[i]['no'] == rule['Replica']: Globals.RULESLOG.write('Installed NEW Rule ' + self.ruleToString(rule) + '\n') (flow, defaultActions, rewriteActions) = IPs.get_forwarding_dstrule(rule['IP'], rule['NumWild'], Globals.VIP, Globals.REPLICAS[i]['mac'], Globals.REPLICAS[i]['ip'], Globals.REPLICAS[i]['port']) self.Multipath.install_replica_flow(flow, Globals.CACHE_TIMEOUT, openflow.OFP_FLOW_PERMANENT, defaultActions, None, openflow.OFP_DEFAULT_PRIORITY, 0, None, rule['Replica'], rewriteActions)
def deleteRule(self, rule): for i in range(0, Globals.NUMREPLICAS): if Globals.REPLICAS[i]['no'] == rule['Replica']: Globals.RULESLOG.write('Deleted OLD Rule ' + self.ruleToString(rule) + '\n') (flow, defaultActions, rewriteActions) = IPs.get_forwarding_dstrule(rule['IP'], rule['NumWild'], Globals.VIP, Globals.REPLICAS[i]['mac'], Globals.REPLICAS[i]['ip'], Globals.REPLICAS[i]['port']) self.Multipath.delete_flow(flow, rule['Replica'])