Пример #1
0
def main():
    logging.basicConfig(filename='activities.log',
                        level=logging.DEBUG,
                        filemode='w')

    logging.info("---- %s ----" % time.strftime("%d/%m/%Y - %H:%M:%S"))

    env = simpy.Environment()

    topologyMaker = TopologyMaker(env, None, applications)

    datacentres, links, leafnodes = topologyMaker.GenerateTreeFromParameters(
        childStruct=[3, 3, 1],
        sizeStruct=[
            Datacentre.RESOURCE_TYPES['S'], Datacentre.RESOURCE_TYPES['L'],
            Datacentre.RESOURCE_TYPES['S']
        ],
        uplinkStruct=[100, 100, 100],
        downlinkStruct=[100, 100, 100],
        latencyStruct=[0, 0, 0])

    logging.info('Topology generated, with %i datacentres' % len(datacentres))

    topology = Topology(env, datacentres, links, leafnodes)

    l = topology.findNeighbours('DC1', 3)
    print l
Пример #2
0
    def start_topology(self, tid, client_ip, user):
        """Handles starting up the specified topology id.  Returns a 2-tuple.
        The first element is None and the second is a string if an error occurs;
        otherwise the first element is the topology."""
        try:
            topo = Topology(tid, self.raw_socket, client_ip, user)
            topo.interactors = [] # list of TI connections to this topo
        except TopologyCreationException as e:
            return (None, str(e))
        except db.Topology.DoesNotExist:
            return (None, 'topology %d does not exist' % tid)
        except db.IPAssignment.DoesNotExist:
            return (None, 'topology %d is missing an IP assignment' % tid)
        except db.IPBlockAllocation.DoesNotExist:
            return (None, 'topology %d is not allocated any IPs' % tid)
        except:
            msg = 'topology instantiation unexpectedly failed'
            log_exception(logging.ERROR, msg)
            return (None, msg)

        if topo.has_gateway():
            self.resolver.register_topology(topo)
        with self.topologies_lock:
            self.topologies[tid] = topo
            self.topologies_changed = True
        return (topo, None)
Пример #3
0
 def _run(self, input_):
     log_file = "{}.log".format(input_)
     open_log(log_file)
     topology = Topology("{}.txt".format(input_))
     topology.run_topo()
     finish_log()
     log = self._read_log(log_file)
     return self._parse(log)
def run_multiprocessing(a_grofileName):

    # --------------------------------------------------------------------
    # Getting topology for molec0 and molec1
    # --------------------------------------------------------------------
    itpmolec0 = Topology.fromItpFile(workdir, molec0_ipt, Hydros0)
    itpmolec0.fromGroFile(workdir, a_grofileName)

    itpmolec1 = Topology.fromItpFile(workdir, molec1_itp, Hydros1)
    itpmolec1.fromGroFile(workdir, a_grofileName)

    # --------------------------------------------------------------------
    # Getting the molecules0 and molecules1 corresponding to the itpmolec0 and itpmolec1, respectvely
    # --------------------------------------------------------------------
    grofile = FileUtil.FileReader(workdir, a_grofileName).file

    print('\nGetting all molec0 molecules present in grofile: {}\n'.format(a_grofileName))
    molecs0_dict = add.get_molecules(itpmolec0, grofile)

    print('\nGetting all molec1 molecules present in grofile: {}\n'.format(a_grofileName))
    molecs1_dict = add.get_molecules(itpmolec1, grofile)

    # --------------------------------------------------------------------
    # Getting h-bonds interactions
    # --------------------------------------------------------------------

    # h-bonds interactions between molec0 and molec1. Where in molec0 we get X-H bond and in molec 1 we get Y
    # ------------------------------
    print('\nThe hydrogen bonds found between X-H from molec0 and Y from molec1 present in grofile {} are:\n'.format(a_grofileName))
    hbonds0_list = add.get_bonded_interactions(molecs0_dict, itpmolec0, molecs1_dict, Hydros1, Alkali1, dist_thres, angle_thres)

    if not hbonds0_list:
        print('\nNo hydrogen bonds found between X-H from molec0 and Y from molec1 present in grofile {}\n'.format(a_grofileName))
    else:
        print('\nA total of {} hydrogen bonds found between X-H from molec0 and Y from molec1 present in grofile {}\n'.format(str(len(hbonds0_list)), a_grofileName))

    # h-bonds interactions between molec0 and molec1. Where in molec0 we get X-H bond and in molec 1 we get Y
    # ------------------------------
    print('\nThe hydrogen bonds found between X-H from molec1 and Y from molec0 present in grofile {} are:\n'.format(a_grofileName))
    hbonds1_list = add.get_bonded_interactions(molecs1_dict, itpmolec1, molecs0_dict, Hydros0, Alkali0, dist_thres, angle_thres)

    if not hbonds1_list:
        print('\nNo hydrogen bonds found between X-H from molec1 and Y from molec0 present in grofile {}\n'.format(a_grofileName))
    else:
        print('\nA total of {} hydrogen bonds found between X-H from molec1 and Y from molec0 present in grofile {}\n'.format(str(len(hbonds1_list)), a_grofileName))

    # adding both list
    # ------------------------------
    hbond_list = hbonds0_list + hbonds1_list # we just add them

    del(itpmolec0, itpmolec1, grofile, molecs0_dict, molecs1_dict, hbonds0_list, hbonds1_list) # delete things to free space in case we run out of memory during the multiprocessing

    return hbond_list
Пример #5
0
def preferentialAttachment(topo_list, scalar=1.0, **kwargs):
        """
        Creates links between topologies in the topo_list. For each pair of
        topologies t1, t2 in the list a pair of nodes (a in t1), (b in t2) will
        get an edge with probability given by:
         
         (scalar/2) * (a.degree + b.degree) / (t1.numEdges + t2.numEdges)
        
        Each entry in topo_list should be a (name, topo, filter) tuple. The name
        will be used as the prefix for the nodes in that sub-topology. This is
        necessary because two nodes in different topologies may have the same
        name, and there must be no two nodes with the same name in the final
        generated topology. The filter will be used to select nodes which may
        be candidates for attachment in each graph. The filter should be a set
        of labels; any nodes with one or more of those labels may be used in
        attachment. If filter is None, it will be treated as the universal set.
        
        Any extra kwargs provided will be passed to the addEdge function when
        inter-topology links are created.
        """
        
        # create deep copies of input topos because we will modify the nodes
        # we will also use this dict to map a topology to its prefix
        t = {x.copy(): (n,f) for n,x,f in topo_list}
        
        # create new empty topology, then copy nodes and edges from components
        tm = Topology()
        tm.nodeSet = set.union(*[x.nodeSet for x in t])
        tm.edgeSet = set.union(*[x.edgeSet for x in t])
        
        # add prefixes to node names
        for c in t:
                for a in c.nodeSet:
                        a.name = t[c][0] + "_" + a.name
        
        # returns the probability that a, b should have an edge based on the
        # degrees of a and b and total edges in t1 and t2
        def p(a, b, t1, t2):
                if t[t1][1] != None and t[t1][1] & a.labels == set():
                        return 0.0
                if t[t2][1] != None and t[t2][1] & b.labels == set():
                        return 0.0
                return scalar/2.0 * (a.getDegree() + b.getDegree()) / (len(t1.edgeSet) + len(t2.edgeSet))
        
        # add edge with probability p for each pair of nodes (a, b)
        for t1, t2 in itertools.combinations(t, 2):
                for a, b in itertools.product(t1.nodeSet, t2.nodeSet):
                        if random.random() < p(a, b, t1, t2):
                                tm.addEdge(a, b, **kwargs)

                              
        return tm
Пример #6
0
def fattree(k) :
		# Make a k-ary Fat Tree. 
		if not k%2 == 0 :
			k = k - 1
		edge = range(0,k*k/2)
		aggregation = range(k*k/2, k*k)
		core = range(k*k,k*k+(k*k/4))

		topo = Topology()

		for e in edge :
			switch = "e" + str(e)
			neighbours = []
			pod = e / (k/2)
			for off in range(0,k/2) :
				agg = k * (k / 2) + (pod * (k/2)) + off
				neighbours.append("a" + str(agg))
			topo.addSwitch(switch, neighbours)

		for a in aggregation :
			switch = "a" + str(a) 
			neighbours = []
			firstCore = k*k + (a % (k/2)) * (k/2)
			for c in range(firstCore, firstCore + k/2) :
				neighbours.append("c" + str(c))
			topo.addSwitch(switch, neighbours)
						
		for c in core :
			topo.addSwitch("c" + str(c), [])

		return topo
Пример #7
0
    def do_topology_test(self):

        log = self.__logger.get()
        log.info("Running topology test")

        log.info("Instantiating custom Topology class")
        self.__topo = Topology(self.__logger)

        log.info("Instantiating Mininet")
        self.__net = Mininet(topo=self.__topo,
                             controller=mininet.node.RemoteController)
        self.__topo.set_net(self.__net)
        self.__topo.consume_instances()

        log.info("Rendering graph of current topology")
        self.__topo.render_dotgraph(False)

        log.info("Done!")
Пример #8
0
    def run(self):

        log = self.__logger.get()

        controller_log_file = os.path.join(
            self.__logger.make_log_file_directory_path(), "controller.txt")

        controller_name = "ryu_" + self.__run_name
        controller_path_relative = "controllers/"
        if self.__run_name == "demo":
            controller_source_file_name = "Demo_SimpleSwitch.py"
        elif self.__run_name == "hub":
            controller_source_file_name = "DumbHub.py"
        elif self.__run_name == "switch":
            controller_source_file_name = "SimpleSwitch.py"
        elif self.__run_name == "qswitch":
            controller_source_file_name = "QSwitch.py"
        else:
            raise Exception("Invalid run name: " + str(self.__run_name))
        controller_path_relative += controller_source_file_name

        # Instantiate Mininet::Ryu(), which just launches ryu-manager for us
        controller = Ryu(
            controller_name,
            controller_path_relative,
            # "--verbose",
            "--log-file",
            controller_log_file)

        self.__logger.heading("Running with controller: " +
                              controller_source_file_name)

        log.info("Instantiating custom Topology class")
        self.__topo = Topology(self.__logger)
        log.info("Rendering graph of current topology")
        self.__topo.render_dotgraph()

        self.run_with_controller(controller)

        #
        log.info("Done")
Пример #9
0
 def assert_topology_spanning_tree(self, inputFileName, expectedLog):
     #print("assert_topology_spanning_tree")
     actualFileName = 'TestOutput/' + inputFileName + '.log'
     print(actualFileName)
     topo = Topology(inputFileName)
     topo.run_spanning_tree()
     topo.log_spanning_tree(actualFileName)
     actualRead = open(actualFileName, "r")
     actualLog = actualRead.read()
     actualRead.close()
     # self.assertEquals(expectedLog, actualLog)
     self.silent_assert(self.assertEquals, expectedLog, actualLog)
Пример #10
0
def run(all_demands: [Demand], topology: Topology) -> float:
    Logger.create_summary_file()
    demands = all_demands
    print("Total demands: {}".format(len(demands)))
    iteration = 0
    start = time.time()

    while iteration <= TOTAL_DURATION and PROCEED:
        demands_starting_now = list(
            filter(lambda demand: demand.started_at == iteration, demands))
        demands_finishing_now = list(
            filter(
                lambda demand: demand.started_at + demand.duration ==
                iteration, demands))
        prepared_demands = set(demands_starting_now + demands_finishing_now)

        for demand in prepared_demands:
            paths_candidates = topology.get_paths(demand.source.id,
                                                  demand.destination.id)
            number_of_paths = len(paths_candidates)
            chosen_path = 0
            if number_of_paths > 0:
                while chosen_path < number_of_paths:
                    is_success = demand.check_and_allocate(
                        iteration, paths_candidates[chosen_path])

                    if is_success:
                        break
                    else:
                        chosen_path = chosen_path + 1
            else:
                demand.mark_as_failed()

        current_duration = time.time() - start
        Logger.progress_bar(iteration, TOTAL_DURATION,
                            get_time(current_duration))
        iteration = iteration + 1

    duration = time.time() - start
    print("\n Finished. Total time duration: {}".format(get_time(duration)))
    return duration
Пример #11
0
	def __init__(self):
		self.listenTo(core.openflow)
		core.openflow_discovery.addListeners(self)
		log.debug("Enabling NetworkMapper Module")

		# Adjacency map.  [sw1][sw2] -> port from sw1 to sw2
		self.adjacency = defaultdict(lambda:defaultdict(lambda:None))
		
		self.switchMap = dict()
		self.switchConnections = dict()

		self.topology = Topology()

		self.fdb = FlowDatabase(self.topology, QUEUE_SIZE)

		self.routeStatus = []

		self.startTime = time.time()

		self.measurementEpoch = 2
		self.measurementInfra = Timer(self.measurementEpoch, self._measurement, recurring=True)

		self.schedulerEpoch = 20
		self.scheduler = Timer(self.schedulerEpoch, self._scheduler, recurring=True)
Пример #12
0
def main():
	# Data centre sizes
	MY_RESOURCE_TYPES = {
		"L":{ 
				"CPU":		{'CAPACITY':100.0, 'EXECOST': LinearCostFunc(1.0,0.0),'OVERLOADCOST': NoCostFunc(),'MU':1},
				"NET_UP":	{'CAPACITY':100.0, 'EXECOST': NoCostFunc(),'OVERLOADCOST': NoCostFunc(),'MU':1},
				"NET_DOWN":	{'CAPACITY':100.0, 'EXECOST': NoCostFunc(),'OVERLOADCOST': NoCostFunc(),'MU':1}
			},
		"M":{ 
				"CPU":		{'CAPACITY':50.0, 'EXECOST': LinearCostFunc(1.0,0.0),'OVERLOADCOST': NoCostFunc(),'MU':1},
				"NET_UP":	{'CAPACITY':50.0, 'EXECOST': NoCostFunc(),'OVERLOADCOST':NoCostFunc(),'MU':1},
				"NET_DOWN":	{'CAPACITY':50.0, 'EXECOST': NoCostFunc(),'OVERLOADCOST': NoCostFunc(),'MU':1}
			},
		"S":{ 
				"CPU":		{'CAPACITY':10.0, 'EXECOST': LinearCostFunc(1.0,0.0),'OVERLOADCOST': NoCostFunc(),'MU':1},
				"NET_UP":	{'CAPACITY':10.0, 'EXECOST': NoCostFunc(),'OVERLOADCOST': NoCostFunc(),'MU':1},
				"NET_DOWN":	{'CAPACITY':10.0, 'EXECOST': NoCostFunc(),'OVERLOADCOST': NoCostFunc(),'MU':1}
			}
		}
	
	#workloadName = "workload_v1_6_a5mini_1may"
	workloadName = "workfile_tripple_production"
	nbrApps = 5
	depth = 4
	mode = "_continuous"
	testCase  = "_cost_ver"

	logging.basicConfig(filename='activities.log', level=logging.DEBUG, filemode='w')
	logging.info("---- %s ----" % time.strftime("%d/%m/%Y - %H:%M:%S"))
	
	applications = {}
	applicationTypes = Application.TYPES.keys()
	for i in range(0, nbrApps):
		#applications.update({'A%i'%i : Application('A%i'%i, Application.TYPES[random.choice(applicationTypes)])})
		applications.update({'A%i'%i : Application('A%i'%i, Application.TYPES['SYMMETRIC'])})

	env = simpy.Environment()

	topologyMaker = TopologyMaker(env, None, applications)

	datacentres, links, leafnodes = topologyMaker.GenerateTreeFromParameters(	childStruct 	= [2, 2, 1], 
																				sizeStruct 		= [	MY_RESOURCE_TYPES['L'],
																									MY_RESOURCE_TYPES['M'],
																									MY_RESOURCE_TYPES['S'] ], 
																				uplinkStruct 	= [10000,1000,1000], 
																				downlinkStruct 	= [10000,1000,1000], 
																				latencyStruct 	= [1,1,1] )
																				
	logging.info('Topology generated, with %i datacentres' % len(datacentres))
	
	topology = Topology(env, datacentres, links, leafnodes)
	
	scheduler = optScheduler(env, topology, applications)
	logging.info('%s scheduler created' % type(scheduler).__name__)
	
	coordinator = Coordinator(env, topology, scheduler, depth)
	
	workload = Workload(env,'workloads/'+workloadName+'.json', coordinator)
	monitor = SystemMonitor(env, 1, 0.2, workloadName+mode+testCase, topology, coordinator, applications, scheduler, 	
															[	("TOTAL_OVERLOAD", SystemMonitor.measureSystemOverloaFactor),
																("COMPONENT_OVERLOAD", SystemMonitor.measureComponentOverloadFactor),
																("RESOURCE_UTILISATION", SystemMonitor.measureComponentResourceUtilisation),
																("APP_RESOURCE_UTILISATION", SystemMonitor.measureUtilisationPerApp),
																("APP_RTT", SystemMonitor.measureAppLatency),
															], 
															[	("TOTAL_OVERLOAD", SystemMonitor.fileCSVOutput, None),
																("COMPONENT_OVERLOAD", SystemMonitor.fileCSVOutput, SystemMonitor.composeDCLinkHeader),
																("RESOURCE_UTILISATION", SystemMonitor.fileCSVOutput, SystemMonitor.composeDCLinkHeader),
																("APP_RESOURCE_UTILISATION", SystemMonitor.fileCSVOutput, None),
																("APP_RTT", SystemMonitor.fileCSVOutput, SystemMonitor.composeLatencyHeader)
															],
															[])
	
	#workload.produceWorkload()
	env.process(workload.produceWorkload())
	env.process(monitor.measure())
	
	logging.info("Controller started")
	controller = PeriodicController(env, coordinator, 1, 0.1)
	
	logging.info("Simulation started")
	env.run(until=workload.getWorkloadTimeSpan())
	logging.info("Simulation Done")
	
	monitor.compose()
	logging.info("Composing results")
	
	monitor.produceOutput()
	scheduler.output(workloadName+mode+testCase)
	
	print "DONE"
Пример #13
0
class Scheduler (EventMixin):

	def __init__(self):
		self.listenTo(core.openflow)
		core.openflow_discovery.addListeners(self)
		log.debug("Enabling NetworkMapper Module")

		# Adjacency map.  [sw1][sw2] -> port from sw1 to sw2
		self.adjacency = defaultdict(lambda:defaultdict(lambda:None))
		
		self.switchMap = dict()
		self.switchConnections = dict()

		self.topology = Topology()

		self.fdb = FlowDatabase(self.topology, QUEUE_SIZE)

		self.routeStatus = []

		self.startTime = time.time()

		self.measurementEpoch = 2
		self.measurementInfra = Timer(self.measurementEpoch, self._measurement, recurring=True)

		self.schedulerEpoch = 20
		self.scheduler = Timer(self.schedulerEpoch, self._scheduler, recurring=True)

		
	"""This event will be raised each time a switch will connect to the controller"""
	def _handle_ConnectionUp(self, event):
		
		# Use dpid to differentiate between switches (datapath-id)
		# Each switch has its own flow table. As we'll see in this 
		# example we need to write different rules in different tables.
		dpid = dpidToStr(event.dpid)
		
		switchName = ""
		for m in event.connection.features.ports:
			name = m.name.split("-")
			if switchName == "" :
				switchName = name[0]
			if not switchName == name[0] :
				log.debug("Some Error in mapping name from the OpenFlow Switch Up Message.")

		swID = self.topology.addSwitch(switchName, [])
		
		self.switchMap[switchName] = dpid
		self.switchConnections[swID] = event.connection

		
		# msg = of.ofp_flow_mod()
		# msg.actions.append(of.ofp_action_output(port = of.OFPP_FLOOD))
  # 		event.connection.send(msg)

	def findSwitchName(self, dpid) :
		for name in self.switchMap.iterkeys() :
			if self.switchMap[name] == dpid :
				return name

	def getSwitchMacAddr(self, sw) :
		if sw == None : 
			return None
		else : 
			dpid = self.switchMap[sw]
			mac = dpid.replace("-", ":")
			return mac

	def findOutputPort(self, curr, next, prev = None) :
		#curr and next are not adjacent. Find the next switch.
		sw = self.findNeighbour(src = curr, dst = next)
		if sw == prev :
			return of.OFPP_IN_PORT # send back on the input port.

		elif not self.adjacency[self.switchMap[curr]][self.switchMap[sw]] == None :
			return self.adjacency[self.switchMap[curr]][self.switchMap[sw]]
		
		else :	
			print "[ERROR] No edge present."
			return None

	def _handle_LinkEvent (self, event):
		l = event.link
		swdpid1 = dpid_to_str(l.dpid1)
		swdpid2 = dpid_to_str(l.dpid2)

		log.debug ("link %s[%d] <-> %s[%d]",
				   swdpid1, l.port1,
				   swdpid2, l.port2)

		sw1 = self.topology.getSwID(self.findSwitchName(swdpid1))
		sw2 = self.topology.getSwID(self.findSwitchName(swdpid2))

		self.adjacency[sw1][sw2] = int(l.port1)
		self.adjacency[sw2][sw1] = int(l.port2)

		self.topology.addLink(sw1, sw2)

	def _handle_PacketIn (self, event):
		"""
		Handle packet in messages from the switch.
		"""
		packet = event.parsed


		def install_fwdrule(event,srcip,dstip,outport,vlan=0):
			msg = of.ofp_flow_mod()
			
			#Match 
			msg.match = of.ofp_match()
			msg.match.dl_type = ethernet.IP_TYPE
			msg.match.set_nw_src(IPAddr(srcip, 32), 32)
			msg.match.set_nw_dst(IPAddr(dstip, 32), 32)

			
			if not vlan == 0 : 
				# Need to set VLAN Tag for isolation of tenant traffic.
				msg.actions.append(of.ofp_action_vlan_vid(vlan_vid = vlan)) 

			msg.actions.append(of.ofp_action_output(port = outport))


			msg.data = event.ofp
			msg.in_port = event.port
			event.connection.send(msg)

		def installFloodRule(event,packet,outport,vlan=0):
			msg = of.ofp_flow_mod()
			
			#Match 
			msg.match = of.ofp_match.from_packet(packet, event.port)		
			msg.actions.append(of.ofp_action_output(port = outport))
			
			msg.data = event.ofp
			msg.in_port = event.port
			event.connection.send(msg)
		
		def handle_IP_packet (event, packet):
			ip = packet.find('ipv4')
			if ip is None:
				#print "Non IP packet"
				match = of.ofp_match.from_packet(packet)
				if  match.dl_type == packet.ARP_TYPE and match.nw_proto == arp.REQUEST :
					ip = match.nw_dst
					self.respondToARP(ip, packet, match, event)

			else :
				#print ip.__str__() 
				if str(ip.srcip) not in hostMap or str(ip.dstip) not in hostMap :
					return # Ignore packet

				srcSw = self.topology.getSwID(hostMap[str(ip.srcip)])
				dstSw = self.topology.getSwID(hostMap[str(ip.dstip)])


				# Find path from srcSw to dstSw and add rules for ip.srcip and ip.dstip
				self.addForwardingRules(ip.srcip, srcSw, ip.dstip, dstSw)
				#switch is event.dpid
			

		handle_IP_packet(event, packet)

		# flood and install the flow table entry for the flood
	
	def respondToARP(self, ip, packet, match, event):
		# reply to ARP request
		r = arp()
		r.opcode = arp.REPLY
		r.hwdst = match.dl_src
		r.protosrc = ip
		r.protodst = match.nw_src
		r.hwsrc = EthAddr(macMap[str(ip)])
		e = ethernet(type=packet.ARP_TYPE, src=r.hwsrc, dst=r.hwdst)
		e.set_payload(r)
		log.debug("%i %i answering ARP for %s" %
		 ( event.dpid, event.port,
		   str(r.protosrc)))
		msg = of.ofp_packet_out()
		msg.data = e.pack()
		msg.actions.append(of.ofp_action_output(port = of.OFPP_IN_PORT))
		msg.in_port = event.port
		event.connection.send(msg)

	def addForwardingRules(self, srcip, srcSw, dstip, dstSw, path=[]) :
		if path == [] and self.checkRouteStatus(srcip, dstip) : return

		print "Adding forwarding rules for", srcip, "->", dstip, ":", srcSw, "->", dstSw
		if path == [] :
			path = self.topology.getPath(srcSw, dstSw) 
		self.printPath(path)

		# Create flow characteristic for flow
		fc = FlowCharacteristic()
		fid = self.fdb.addFlow([srcip, dstip], srcSw, dstSw, fc) # Adding flow to flow database
		#self.fdb.changePath(fid, path)
		status = [srcip, dstip, path, fc, fid]
		self.routeStatus.append(status)
		
		for i in range(len(path) - 1) :
			sw1 = path[i]
			sw2 = path[i + 1]

			# add rule to go from sw1 to sw2
			self.addRule(srcip, dstip, sw1, sw2)

		# add flooding rule for dst :
		self.addRule(srcip, dstip, dstSw, None)


	def addRule(self, srcip, dstip, sw1, sw2) : 
		msg = of.ofp_flow_mod()
		connection = self.switchConnections[sw1]
		if sw2 == None : 
			outport = of.OFPP_FLOOD
		else :
			outport = self.adjacency[sw1][sw2]

		#Match 
		msg.match = of.ofp_match()
		msg.match.dl_type = ethernet.IP_TYPE
		msg.match.set_nw_src(IPAddr(srcip, 32), 32)
		msg.match.set_nw_dst(IPAddr(dstip, 32), 32)
		msg.priority = of.OFP_DEFAULT_PRIORITY
		msg.actions.append(of.ofp_action_output(port = outport))

		connection.send(msg)

	def deleteRule(self, srcip, dstip, sw1, sw2) : 
		msg = of.ofp_flow_mod()
		connection = self.switchConnections[sw1]
		msg.command = of.OFPFC_DELETE
		#Match 
		msg.match = of.ofp_match()
		msg.match.dl_type = ethernet.IP_TYPE
		msg.match.set_nw_src(IPAddr(srcip, 32), 32)
		msg.match.set_nw_dst(IPAddr(dstip, 32), 32)
		connection.send(msg)

	def printPath(self, path) :
		namedPath = []
		for i in range(len(path)) : 
			namedPath.append(self.topology.getSwName(path[i]))
		print namedPath

	def checkRouteStatus(self, srcip, dstip) :
		for stat in self.routeStatus : 
			if stat[0] == srcip and stat[1] == dstip : 
				return True
		return False

	def deleteStaleForwardingRules(self,srcip, dstip, path) :
		""" Delete stale rules for srcip -> dstip on path """

		for i in range(len(path) - 1) :
			sw1 = path[i]
			sw2 = path[i + 1]

			# delete rule to go from sw1 to sw2
			self.deleteRule(srcip, dstip, sw1, sw2)


	def _handle_FlowStatsReceived (self, event):
		#stats = flow_stats_to_list(event.stats)
		swdpid = dpidToStr(event.dpid)
		swID = self.topology.getSwID(self.findSwitchName(swdpid))

		for f in event.stats:
			for stat in self.routeStatus : 
				if f.match.nw_src == stat[0] and f.match.nw_dst == stat[1] and stat[2][0] == swID : 
					# Edge switch for flow. Update Flow Characteristics
					fc = stat[3]
					val = fc.insert(f.byte_count / 1000, time.time() - self.startTime)


	# def _handle_PortStatsReceived(self, event) :
	# 	print "Port event"
	# 	swdpid = dpidToStr(event.dpid)
	# 	swID = self.topology.getSwID(self.findSwitchName(swdpid))

	# 	swBytes = 0
	# 	print event.stats
	# 	for f in event.stats:
	# 		print f.port_no, f.tx_bytes, f.rx_bytes
	# 		swBytes += f.rx_bytes - f.tx_bytes 

	# 	print "Updating Switch ", swID, swBytes
	# 	#self.fdb.addSwitchBytes(swID, swBytes)


	def _measurement (self) :
		for connection in self.switchConnections.values():
			connection.send(of.ofp_stats_request(body=of.ofp_flow_stats_request()))
			#connection.send(of.ofp_stats_request(body=of.ofp_port_stats_request()))

	def _scheduler(self) :
		# Update new and learned flows
		for stat in self.routeStatus : 
			fid = stat[4]
			fc = stat[3]
			path = stat[2]
			if fc.learned() and len(self.fdb.getPath(fid)) == 0 :
				# Add this flow to the scheduler
				self.fdb.changePath(fid, path)

		# perform scheduling for events in the current epoch.
		self.fdb.updateCriticalTimes()

		rerouteDecisions = self.fdb.scheduleEpoch()

		for dec in rerouteDecisions :
			fid = dec[0]
			oldpath = dec[1]
			newpath = dec[2]
			self.deleteStaleForwardingRules(srcip=self.fdb.getSourceIP(fid), dstip=self.fdb.getDestinationIP(fid), path=oldpath)
			self.addForwardingRules(srcip=self.fdb.getSourceIP(fid), srcSw=newpath[0], dstip=self.fdb.getDestinationIP(fid), dstSw=newpath[len(newpath)-1], path=newpath)

			# update stat
			for stat in self.routeStatus : 
				if fid == stat[4] : stat[2] = newpath
    def __init__(self, func, bounds, args=(),
                 strategy='best1bin', maxiter=1000, popsize=15,
                 tol=0.01, mutation=(0.5, 1), recombination=0.7, seed=None,
                 maxfun=np.inf, callback=None, disp=False, polish=True,
                 init='latinhypercube', atol=0,

                 island_marker=4,number_of_islands=4,number_of_migrations=4,
                 key="ISLAND",
                 wait_time=10, poll_time=3, topology = None
                 ):

        if strategy in self._binomial:
            self.mutation_func = getattr(self, self._binomial[strategy])
        elif strategy in self._exponential:
            self.mutation_func = getattr(self, self._exponential[strategy])
        else:
            raise ValueError("Please select a valid mutation strategy")
        self.strategy = strategy

        self.callback = callback
        self.polish = polish

        self.island_marker = island_marker
        self.number_of_migrations = number_of_migrations
        self.key = key
        self.wait_time = wait_time
        self.poll_time = poll_time
        self.number_of_islands = number_of_islands
        if topology is None:
            self.topology = Topology.create_1_way_ring(number_of_islands=self.number_of_islands)

        else:
            self.topology = topology

        # relative and absolute tolerances for convergence
        self.tol, self.atol = tol, atol

        # Mutation constant should be in [0, 2). If specified as a sequence
        # then dithering is performed.
        self.scale = mutation
        if (not np.all(np.isfinite(mutation)) or
                np.any(np.array(mutation) >= 2) or
                np.any(np.array(mutation) < 0)):
            raise ValueError('The mutation constant must be a float in '
                             'U[0, 2), or specified as a tuple(min, max)'
                             ' where min < max and min, max are in U[0, 2).')

        self.dither = None
        if hasattr(mutation, '__iter__') and len(mutation) > 1:
            self.dither = [mutation[0], mutation[1]]
            self.dither.sort()

        self.cross_over_probability = recombination

        self.func = func
        self.args = args

        # convert tuple of lower and upper bounds to limits
        # [(low_0, high_0), ..., (low_n, high_n]
        #     -> [[low_0, ..., low_n], [high_0, ..., high_n]]
        self.limits = np.array(bounds, dtype='float').T
        if (np.size(self.limits, 0) != 2 or not
                np.all(np.isfinite(self.limits))):
            raise ValueError('bounds should be a sequence containing '
                             'real valued (min, max) pairs for each value'
                             ' in x')

        if maxiter is None:  # the default used to be None
            maxiter = 1000
        self.maxiter = maxiter
        if maxfun is None:  # the default used to be None
            maxfun = np.inf
        self.maxfun = maxfun

        # population is scaled to between [0, 1].
        # We have to scale between parameter <-> population
        # save these arguments for _scale_parameter and
        # _unscale_parameter. This is an optimization
        self.__scale_arg1 = 0.5 * (self.limits[0] + self.limits[1])
        self.__scale_arg2 = np.fabs(self.limits[0] - self.limits[1])

        self.parameter_count = np.size(self.limits, 1)

        self.random_number_generator = check_random_state(seed)

        # default population initialization is a latin hypercube design, but
        # there are other population initializations possible.
        # the minimum is 5 because 'best2bin' requires a population that's at
        # least 5 long
        self.num_population_members = max(5, popsize * self.parameter_count)

        self.population_shape = (self.num_population_members,
                                 self.parameter_count)

        self._nfev = 0
        if isinstance(init, string_types):
            if init == 'latinhypercube':
                self.init_population_lhs()
            elif init == 'random':
                self.init_population_random()
            else:
                raise ValueError(self.__init_error_msg)
        else:
            self.init_population_array(init)

        self.disp = disp
Пример #15
0
@author: HZhao
"""

from Topology import Topology
import networkx as nx
import Sender
import threading
import Relay
import Receiver

iplist=['172.16.1.1','172.16.2.1','172.16.3.2','172.16.4.2','172.16.5.3','172.16.6.4','172.16.7.5','172.16.8.6']
portList=[9001,9009,9012,9002,9007,9008,9010,9013]
fixHOST = '127.0.0.1'

Topology_1 = Topology()
Topology_1.config_construct('config/topology.ini')

connectionBefore, connectionAfter=Topology_1.getConnectionRelation()
RelaySet=[]

#start three relays
for idx in range(6):
    relayIP=iplist[idx+1]
    connectionBefore1=connectionBefore.get(relayIP)
    connectionAfter1=connectionAfter.get(relayIP)
    connectionAfterwithPort=[]
    for afterIP in connectionAfter1:
        connectionAfterwithPort.append((afterIP, portList[iplist.index(afterIP)]))

    connectionBeforewithPort = []
# -------------------------------------------------------------------------------------------------
# Plotting the number of hydrogen bonds for each one of the different grofiles
# -------------------------------------------------------------------------------------------------
plotObj = HbondsPlotter(all_hbonds) # creating the HbondsPlotter object from the all_bonds list
plotObj.plot(workdir, frame_time) # method to print all the h-bonds plots.
print('\nFinished all plots!\n')

# -------------------------------------------------------------------------------------------------
# Getting the density profile using gmx density
# -------------------------------------------------------------------------------------------------

# Getting the residue name for molec0 and molec1 (see Topology.py for more info)
# ------------------------------

molec0_residue= Topology.fromItpFile(workdir, molec0_ipt, Hydros0).residue
molec1_residue = Topology.fromItpFile(workdir, molec1_itp, Hydros1).residue

# Getting the z-axis length of the grofile (any grofile can be used)
# ------------------------------

zBoxLen = float(FileUtil.FileReader(workdir, grofiles[-1]).file[-1].split()[2]) # Any grofile can be used. The last was selected.

# Getting the density file file
# ------------------------------

add.get_gmx_index_density_files(workdir, all_hbonds, molec0_residue, molec1_residue,
                            zBoxLen, gmx_call, grofiles[-1], xtc_filename,
                            tpr_filename, outdens_xvg)

print('\nDensity profile was calculated sucessfully!\n')
Пример #17
0
		# Greedy path search, explore switch with greatest critical time
			
		for pair in swPairList :
			nextsw = pair[0]
			if nextsw == dst : 
				# Found a path to destination
				return [sw, dst]
			updatedfc = 0 # If  sw-nextsw link is chosen, find updated fc
			path = self.greedyDFS(nextsw, updatedfc, dst, cThres) 
			if path <> None : 
				path.insert(sw, 0)
				return path

		# if none of the neighbours have permissible values, return None
		return None











t = Topology()
t.addSwitch("a0", ["a1", "a2"])
t.addSwitch("a1", ["a2", "a3"])
t.addSwitch("a2", ["a4"])
s = Scheduler(t)
Пример #18
0
class CPSC558FinalProject:

    __DEFAULT_RUN_NAME = "main"
    __DEFAULT_FILE_SERVER_DIRECTORY = "data"

    __BANDWIDTH_LIMIT_MBPS = 1000
    __BANDWIDTH_LIMIT_LATENCY = "1ms"

    def __init__(self, run_name):

        self.__run_name = run_name

        self.__logger = Logger(group=self.__run_name,
                               log_name=__name__,
                               label="558 Project")

        self.__logger_summary = Logger(group="summary",
                                       log_name=__name__,
                                       label="558 Project Summary",
                                       append=True)

        self.__net = None  # Mininet
        self.__topo = None
        self.__controller = None

        self.__logger.get().info("Instantiated inside Python version: " +
                                 str(sys.version))

    def run(self):

        log = self.__logger.get()

        controller_log_file = os.path.join(
            self.__logger.make_log_file_directory_path(), "controller.txt")

        controller_name = "ryu_" + self.__run_name
        controller_path_relative = "controllers/"
        if self.__run_name == "demo":
            controller_source_file_name = "Demo_SimpleSwitch.py"
        elif self.__run_name == "hub":
            controller_source_file_name = "DumbHub.py"
        elif self.__run_name == "switch":
            controller_source_file_name = "SimpleSwitch.py"
        elif self.__run_name == "qswitch":
            controller_source_file_name = "QSwitch.py"
        else:
            raise Exception("Invalid run name: " + str(self.__run_name))
        controller_path_relative += controller_source_file_name

        # Instantiate Mininet::Ryu(), which just launches ryu-manager for us
        controller = Ryu(
            controller_name,
            controller_path_relative,
            # "--verbose",
            "--log-file",
            controller_log_file)

        self.__logger.heading("Running with controller: " +
                              controller_source_file_name)

        log.info("Instantiating custom Topology class")
        self.__topo = Topology(self.__logger)
        log.info("Rendering graph of current topology")
        self.__topo.render_dotgraph()

        self.run_with_controller(controller)

        #
        log.info("Done")

    def run_with_controller(self, controller):

        log = self.__logger.get()

        log.info("Instantiating Mininet")

        self.__net = Mininet(topo=self.__topo,
                             controller=controller,
                             waitConnected=False)
        self.__topo.set_net(self.__net)

        log.info("Starting Mininet (will wait for controller)")
        self.__net.start()
        wait_result = self.__net.waitConnected(timeout=10)
        if wait_result is False:
            log.error("Failed to wait for a controller!")
            log.error("FAIL")
            return
        log.info("Mininet found a controller to connect to")

        # Create qos queues
        self.__topo.create_qos_queues_on_switch()

        # Ping tests
        self.ping_all()

        # Begin node traffic
        self.start_tattle_tail(True)
        self.start_file_traffic(True)
        self.start_video_traffic(True)

        #
        self.wait_for_hosts_to_finish()
        self.summarize_node_logs()

    def do_topology_test(self):

        log = self.__logger.get()
        log.info("Running topology test")

        log.info("Instantiating custom Topology class")
        self.__topo = Topology(self.__logger)

        log.info("Instantiating Mininet")
        self.__net = Mininet(topo=self.__topo,
                             controller=mininet.node.RemoteController)
        self.__topo.set_net(self.__net)
        self.__topo.consume_instances()

        log.info("Rendering graph of current topology")
        self.__topo.render_dotgraph(False)

        log.info("Done!")

    def ping_all(self):

        self.__logger.get().info("Attempting to ping between all nodes")
        self.__net.pingAll(timeout=1)
        print()
        self.__logger.get().info("Done pinging between all nodes")

    @staticmethod
    def make_process_stdout_file_path(run_name, file_name, clear=True):

        output_dir = os.path.join(os.path.dirname(__file__), "log", run_name)
        try:
            os.makedirs(output_dir)
        except FileExistsError:
            pass

        file_path = os.path.join(output_dir, file_name + ".txt")
        if clear and os.path.isfile(file_path):
            os.unlink(file_path)

        return file_path

    def start_tattle_tail(self, use_log: bool = True):

        log = self.__logger.get()

        log.info("Starting tattle tail")

        log_file = self.make_process_stdout_file_path(self.__run_name,
                                                      "tattle-tail-stdout")
        log.info(log_file)

        # Get tattle tail instance
        tattle = self.__topo.get_tattle_tail_instance()
        tattle_ip = tattle.IP()
        log.info("Tattle tail IP is: " + str(tattle_ip))

        # Start the tattle tail
        if use_log:
            tattle.cmd("ifconfig | grep eth >> \"" + log_file + "\" 2>&1")
            tattle.sendCmd("./main.py --tattle-tail" + " --run-name \"" +
                           str(self.__run_name) + "\"" + " --name \"" +
                           str(tattle) + "\"" + " >> \"" + log_file +
                           "\" 2>&1")
        else:
            tattle.cmd("ifconfig | grep eth 2>&1")
            tattle.sendCmd("./main.py --tattle-tail" + " --run-name \"" +
                           str(self.__run_name) + "\"" + " --name \"" +
                           str(tattle) + "\"" + " 2>&1")

        log.info("Done starting tattle tail")

    def start_video_traffic(self, use_log: bool = True):

        log = self.__logger.get()

        log.info("Starting video traffic")

        server_log_file = self.make_process_stdout_file_path(
            self.__run_name, "video-server-stdout")
        log.info("Video server stdout: " + server_log_file)
        client_log_file = self.make_process_stdout_file_path(
            self.__run_name, "video-clients-stdout")
        log.info("Video clients stdout: " + client_log_file)

        # Get video server instance
        server = self.__topo.get_video_server_instance()
        server_ip = server.IP()
        log.info("Video server IP is: " + str(server_ip))

        # Start video server
        if use_log:
            server.cmd("ifconfig | grep eth >> \"" + server_log_file +
                       "\" 2>&1")
            server.sendCmd("./main.py --video-server" + " --run-name \"" +
                           str(self.__run_name) + "\"" + " --name \"" +
                           str(server) + "\"" + " >> \"" + server_log_file +
                           "\" 2>&1")
        else:
            server.cmd("ifconfig | grep eth 2>&1")
            server.sendCmd("./main.py --video-server" + " --run-name \"" +
                           str(self.__run_name) + "\"" + " --name \"" +
                           str(server) + "\"" + " 2>&1")

        # Grab client instances
        clients = list(self.__topo.get_video_client_instances().values())

        # Start each client
        for client in clients:

            if use_log:
                client.cmd("ifconfig | grep eth >> \"" + client_log_file +
                           "\" 2>&1")
                client.sendCmd("./main.py --video-client" + " --run-name \"" +
                               str(self.__run_name) + "\"" + " --name \"" +
                               str(client) + "\"" + " --host \"" + server_ip +
                               "\"" + " >> \"" + client_log_file + "\" 2>&1")
            else:
                client.cmd("ifconfig | grep eth 2>&1")
                client.sendCmd("./main.py --video-client" + " --run-name \"" +
                               str(self.__run_name) + "\"" + " --name \"" +
                               str(client) + "\"" + " --host \"" + server_ip +
                               "\"" + " 2>&1")

        log.info("Done starting video traffic")

    def start_file_traffic(self, use_log: bool = True):

        log = self.__logger.get()

        log.info("Starting file traffic")

        server_log_file = self.make_process_stdout_file_path(
            self.__run_name, "file-server-stdout")
        log.info("File server stdout: " + server_log_file)
        client_log_file = self.make_process_stdout_file_path(
            self.__run_name, "file-clients-stdout")
        log.info("File clients stdout: " + client_log_file)

        # Get file server instance
        server = self.__topo.get_file_server_instance()
        server_ip = server.IP()
        log.info("File server IP is: " + str(server_ip))

        # Start file server
        if use_log:
            server.cmd("ifconfig | grep eth >> \"" + server_log_file +
                       "\" 2>&1")
            server.sendCmd("./main.py --file-server" + " --run-name \"" +
                           str(self.__run_name) + "\"" + " --name \"" +
                           str(server) + "\"" + " --directory \"" +
                           self.make_file_server_directory() + "\"" +
                           " >> \"" + server_log_file + "\" 2>&1")
        else:
            server.cmd("ifconfig | grep eth 2>&1")
            server.sendCmd("./main.py --file-server" + " --run-name \"" +
                           str(self.__run_name) + "\"" + " --name \"" +
                           str(server) + "\"" + " --directory \"" +
                           self.make_file_server_directory() + "\"" + " 2>&1")

        # Grab client instances
        clients = list(self.__topo.get_file_client_instances().values())

        # Start each client
        for client in clients:

            if use_log:
                client.cmd("ifconfig | grep eth >> \"" + client_log_file +
                           "\" 2>&1")
                client.sendCmd("./main.py --file-client" + " --run-name \"" +
                               str(self.__run_name) + "\"" + " --name \"" +
                               str(client) + "\"" + " --host \"" + server_ip +
                               "\"" + " >> \"" + client_log_file + "\" 2>&1")
            else:
                client.cmd("ifconfig | grep eth 2>&1")
                client.sendCmd("./main.py --file-client" + " --run-name \"" +
                               str(self.__run_name) + "\"" + " --name \"" +
                               str(client) + "\"" + " --host \"" + server_ip +
                               "\"" + " 2>&1")

        log.info("Done starting file traffic")

    def wait_for_hosts_to_finish(self):

        log = self.__logger.get()

        log.info("Start waiting for all hosts to finish")

        # Gather all hosts to wait for
        hosts_to_wait_for = list()
        for host_name in self.__topo.get_video_client_instances():
            hosts_to_wait_for.append(self.__net.nameToNode[host_name])
        for host_name in self.__topo.get_file_client_instances():
            hosts_to_wait_for.append(self.__net.nameToNode[host_name])

        # Wait for all the hosts
        for host in hosts_to_wait_for:
            log.info("Waiting for host " + str(host) +
                     " to finish its command")
            host.waitOutput(verbose=True)
            log.info("Host " + str(host) + " has finished its command")

        log.info("Done waiting for all hosts to finish")

    def make_file_server_directory(self):

        d = os.path.join(os.path.dirname(__file__),
                         self.__DEFAULT_FILE_SERVER_DIRECTORY)

        return d

    def summarize_node_logs(self):

        self.summarize_node_benchmark_logs()

    def summarize_node_benchmark_logs(self):

        log = self.__logger.get()
        log_s = self.__logger_summary.get()

        log.info("Attempting to summarize node benchmark logs")

        log_s.info("")
        log_s.info("*** " + self.__run_name + " ***")
        log_s.info("Attempting to summarize node benchmark logs")

        logs_dir = self.__logger.make_log_file_directory_path()
        log.info("Pulling from log directory: " + logs_dir)

        # Build a list of all nodes we're interested in
        nodes_file_clients = list(
            self.__topo.get_file_client_instances().values())
        nodes_video_clients = list(
            self.__topo.get_video_client_instances().values())
        nodes_all_clients = nodes_file_clients + nodes_video_clients
        log.info("Will examine logs from " + str(len(nodes_all_clients)) +
                 " client nodes")

        pattern_bytes_received = re.compile(
            """^Bytes received: (?P<bytes>[0-9]+)$""",
            re.MULTILINE | re.IGNORECASE)
        pattern_mbps = re.compile(
            """^Megabits per second: (?P<mbps>[0-9.]+)$""",
            re.MULTILINE | re.IGNORECASE)

        # For each client we're interested in, pull the number of bytes transferred from its logs
        total_bytes = 0
        mbps_all_samples = list()
        mbps_file_samples = list()
        mbps_video_samples = list()
        for node in nodes_all_clients:

            node_log_file_name = str(node) + ".txt"

            log_path = os.path.join(logs_dir, node_log_file_name)

            log.info("Examining log for node \"" + str(node) + "\": " +
                     node_log_file_name)

            # Load the logfile
            with open(log_path, "rt") as f:

                s = f.read()

                # Bytes received
                match = pattern_bytes_received.search(s)
                if match is None:
                    raise Exception(
                        "Failed to parse node; Cannot find bytes received in: "
                        + node_log_file_name)
                node_bytes = int(match.group("bytes"))
                total_bytes += node_bytes
                log.info("Node \"" + str(node) + "\" seems to have received " +
                         str(node_bytes) + " bytes" + " (" + str(total_bytes) +
                         " total)")

                # Megabits per second
                match = pattern_mbps.search(s)
                if match is None:
                    raise Exception(
                        "Failed to parse node; Cannot find megabits per second!"
                    )
                node_mbps = float(match.group("mbps"))
                log.info("Node \"" + str(node) +
                         "\" seems to have received data at " +
                         str(node_mbps) + " megabits per second")

                # Add to sample pools
                mbps_all_samples.append(node_mbps)
                if node in nodes_file_clients:
                    mbps_file_samples.append(node_mbps)
                elif node in nodes_video_clients:
                    mbps_video_samples.append(node_mbps)
                else:
                    raise Exception(
                        "Don't know where to add this node's bandwidth sample!"
                    )

        mbps_average_file = sum(mbps_file_samples) / len(mbps_file_samples)
        mbps_average_video = sum(mbps_video_samples) / len(mbps_video_samples)
        mbps_average_all = sum(mbps_all_samples) / len(mbps_all_samples)

        log.info("We seem to have the following aggregate bandwidths:")
        log_s.info("We seem to have the following aggregate bandwidths:")
        log.info("File clients: " + str(mbps_average_file) + " mbps")
        log_s.info("File clients: " + str(mbps_average_file) + " mbps")
        log.info("Video clients: " + str(mbps_average_video) + " mbps")
        log_s.info("Video clients: " + str(mbps_average_video) + " mbps")
        log.info("All clients: " + str(mbps_average_all) + " mbps")
        log_s.info("All clients: " + str(mbps_average_all) + " mbps")