def __init__(self, *args, **kwargs): super(BandwidthMonitor, self).__init__(*args, **kwargs) self.datapaths = {} self.dpset = kwargs['dpset'] self.topo = Topology(cfgfile) self.bwstats = BandwidthStats(self.topo) self.monitor_thread = hub.spawn(self.monitor)
def topology(): if 'file' not in request.files: flash('Потрібно вибрати файл', category='error') return redirect(url_for('index')) file = request.files['file'] filename = file.filename if filename == '': flash('Файл не вибрано', category='error') return redirect(url_for('index')) ext = os.path.splitext(filename)[1][1:] if not allowed_file(filename): flash('Формат {} не підтримується'.format(ext), category='error') return redirect(url_for('index')) topology = Topology(file=file.stream, type=ext) return render_template('topology.html', t=topology.make_report())
def __init__(self, *args, **kwargs): super(Multijet2, self).__init__(*args, **kwargs) wsgi = kwargs['wsgi'] wsgi.register(MultijetServer, {'app': self}) self._dp = None # self._verifiers = {} self._qs = {} self._cps = [100] self._node_id = platform.node() topo = Topology() topo.load('/common/topo.json') self._topo = topo self._pkt_trans = None with open('/common/spanningtree.json') as f: _spanning_tree = json.load(f) self._flood_ports = _spanning_tree[self._node_id]
class BandwidthMonitor(app_manager.RyuApp): OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] _CONTEXTS = { 'dpset' : dpset.DPSet, } def __init__(self, *args, **kwargs): super(BandwidthMonitor, self).__init__(*args, **kwargs) self.datapaths = {} self.dpset = kwargs['dpset'] self.topo = Topology(cfgfile) self.bwstats = BandwidthStats(self.topo) self.monitor_thread = hub.spawn(self.monitor) @set_ev_cls(ofp_event.EventOFPStateChange, [MAIN_DISPATCHER, DEAD_DISPATCHER]) def stateChangeHandler(self, ev): datapath = ev.datapath if datapath.id: if ev.state == MAIN_DISPATCHER: if not datapath.id in self.datapaths: self.logger.debug('register datapath: %016x', datapath.id) self.datapaths[datapath.id] = datapath elif ev.state == DEAD_DISPATCHER: if datapath.id in self.datapaths: self.logger.debug('unregister datapath: %016x', datapath.id) del self.datapaths[datapath.id] def monitor(self): while True: self.statsReplied = 0 for dp in self.datapaths.values(): self.requestStats(dp) hub.sleep(STAT_REQUEST_PERIOD) def requestStats(self, datapath): ofproto = datapath.ofproto parser = datapath.ofproto_parser req = parser.OFPPortStatsRequest(datapath, 0, ofproto.OFPP_ANY) datapath.send_msg(req) @set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER) def statsReplyHandler(self, ev): # Here, a datapath (ie, switch) responds to the stats request. body = ev.msg.body datapath = ev.msg.datapath dpid = datapath.id # name of switch reporting data name = self.topo.dpidToName(dpid) totalDropped = 0 for stat in body: totalDropped += stat.tx_dropped totalDropped += stat.rx_dropped self.bwstats.addDroppedPktStat(name, totalDropped) # ASSIGNMENT 2: # If the switch reporting the statistic is an edge switch, and the # port connects to a host, log the statistic to bwstats, using # self.bwstats.addHostBwStat(hostname, transmitted bytes, received bytes) # (Hint: you can look up the switch or host connected to a port using # self.topo.ports[switch name][port number]) # [ ADD YOUR CODE HERE ] # Get the list of ports portsList = self.topo.ports[name].keys() # Parse all the stats. # This stats were created for a specific port, lets find the port. for stat in body: # We only care for the stats that are related to real ports, # If this is a virtual ports which have a number greater than # the size of the portList list, do NOTHING # But for real ports, find out which host the port is connected to. if stat.port_no < len(portsList): # If we are here we know this is a real port. # Get the host the port is connected to. If the port is conneted to a host which name # contains an h then it is a real host, not a switch, and the switch is an edge switch hostconnected = self.topo.ports[name][stat.port_no] if "h" in hostconnected: # If we are here, we now know that this port is connected to the host we want to get the stats for # Log the stats for this port # addHostBwStat takes care of reversing tx/rx self.bwstats.addHostBwStat(hostconnected,stat.tx_bytes,stat.rx_bytes) # periodically print tenant bandwidth usage self.statsReplied += 1 if self.statsReplied == len(self.datapaths): self.bwstats.updateTenantStats() self.logger.info(self.bwstats.tenantBwString())
class BandwidthMonitor(app_manager.RyuApp): OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] _CONTEXTS = { 'dpset' : dpset.DPSet, } def __init__(self, *args, **kwargs): super(BandwidthMonitor, self).__init__(*args, **kwargs) self.datapaths = {} self.dpset = kwargs['dpset'] self.topo = Topology(cfgfile) self.bwstats = BandwidthStats(self.topo) self.monitor_thread = hub.spawn(self.monitor) @set_ev_cls(ofp_event.EventOFPStateChange, [MAIN_DISPATCHER, DEAD_DISPATCHER]) def stateChangeHandler(self, ev): datapath = ev.datapath if datapath.id: if ev.state == MAIN_DISPATCHER: if not datapath.id in self.datapaths: self.logger.debug('register datapath: %016x', datapath.id) self.datapaths[datapath.id] = datapath elif ev.state == DEAD_DISPATCHER: if datapath.id in self.datapaths: self.logger.debug('unregister datapath: %016x', datapath.id) del self.datapaths[datapath.id] def monitor(self): while True: self.statsReplied = 0 for dp in self.datapaths.values(): self.requestStats(dp) hub.sleep(STAT_REQUEST_PERIOD) def requestStats(self, datapath): ofproto = datapath.ofproto parser = datapath.ofproto_parser req = parser.OFPPortStatsRequest(datapath, 0, ofproto.OFPP_ANY) datapath.send_msg(req) @set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER) def statsReplyHandler(self, ev): # Here, a datapath (ie, switch) responds to the stats request. body = ev.msg.body datapath = ev.msg.datapath dpid = datapath.id # name of switch reporting data name = self.topo.dpidToName(dpid) totalDropped = 0 for stat in body: totalDropped += stat.tx_dropped totalDropped += stat.rx_dropped self.bwstats.addDroppedPktStat(name, totalDropped) # ASSIGNMENT 2: # If the switch reporting the statistic is an edge switch, and the # port connects to a host, log the statistic to bwstats, using # self.bwstats.addHostBwStat(hostname, transmitted bytes, received bytes) # (Hint: you can look up the switch or host connected to a port using # self.topo.ports[switch name][port number]) # [ ADD YOUR CODE HERE ] ## siwtchList = [s101,s102,103] ## hostPorts = [3,4] ## print name ## print self.topo.ports[name].keys() ## if stat.port_no in self.topo.ports[name].keys(): #print "all names:", name ## ## print stat.port_no ## print self.topo.ports[name].keys() ## device = self.topo.ports[name][stat.port_no] ## print device ## ## if stat.port_no in self.topo.ports[name].keys(): ## device = self.topo.ports[name] ## print device ## if device in self.topo.hosts: ## print "device is a host" ## else: ## print "Device is not a host" ## if name in self.topo.edgeSwitches: ## print "name:", name ## print "stat.port_no:", stat.port_no ## print self.topo.ports[name].keys() ## ## if name in self.topo.hosts.keys(): ## print name ## print self.topo.ports[name].keys() ## if stat.port_no not in self.topo.ports[name].keys(): ## print stat.port_no totalTX = 0 totalRX = 0 for stat in body: if stat.port_no in self.topo.ports[name].keys(): #print "port:", stat.port_no #print name device = self.topo.ports[name][stat.port_no] #print device if device in self.topo.hosts.keys(): print "device:", device print "port:", stat.port_no ## totalTX += stat.tx_bytes ## totalRX += stat.rx_bytes self.bwstats.addHostBwStat(device, stat.tx_bytes, stat.rx_bytes) #### print self.topo.hosts.keys() ## if device in self.topo.hosts.keys(): ## print "device is host:", device ## else: ## print "device is not a host:", device ## get device ## check if device is a host: ## add to stat ## if stat.port_no in self.topo.ports[name].keys(): ## self.logger.info('if stat.port_no in self.topo.ports[name].keys()') ## hostname = self.topo.ports[name][stat.port_no] ## self.logger.info('hostname = self.topo.ports[name][stat.port_no]') ## if hostname in self.topo.hosts: ## for txrx in body: ## tx += txrx.tx_bytes ## rx += txrx.rx_bytes ## self.logger.info('for txrx in body') ## self.bwstats.addHostBwStat(hostname, tx, rx) ## self.logger.info('HELLOOO!!!') ## if name in switchList ## if self.topo.ports[name][ ## raw_input() # periodically print tenant bandwidth usage self.statsReplied += 1 if self.statsReplied == len(self.datapaths): self.bwstats.updateTenantStats() self.logger.info(self.bwstats.tenantBwString())
class BandwidthMonitor(app_manager.RyuApp): OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] _CONTEXTS = { 'dpset' : dpset.DPSet, } def __init__(self, *args, **kwargs): super(BandwidthMonitor, self).__init__(*args, **kwargs) self.datapaths = {} self.dpset = kwargs['dpset'] self.topo = Topology(cfgfile) self.bwstats = BandwidthStats(self.topo) self.monitor_thread = hub.spawn(self.monitor) @set_ev_cls(ofp_event.EventOFPStateChange, [MAIN_DISPATCHER, DEAD_DISPATCHER]) def stateChangeHandler(self, ev): datapath = ev.datapath if datapath.id: if ev.state == MAIN_DISPATCHER: if not datapath.id in self.datapaths: self.logger.debug('register datapath: %016x', datapath.id) self.datapaths[datapath.id] = datapath elif ev.state == DEAD_DISPATCHER: if datapath.id in self.datapaths: self.logger.debug('unregister datapath: %016x', datapath.id) del self.datapaths[datapath.id] def monitor(self): while True: self.statsReplied = 0 for dp in self.datapaths.values(): self.requestStats(dp) hub.sleep(STAT_REQUEST_PERIOD) def requestStats(self, datapath): ofproto = datapath.ofproto parser = datapath.ofproto_parser req = parser.OFPPortStatsRequest(datapath, 0, ofproto.OFPP_ANY) datapath.send_msg(req) @set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER) def statsReplyHandler(self, ev): # Here, a datapath (ie, switch) responds to the stats request. body = ev.msg.body datapath = ev.msg.datapath dpid = datapath.id # name of switch reporting data name = self.topo.dpidToName(dpid) totalDropped = 0 for stat in body: totalDropped += stat.tx_dropped totalDropped += stat.rx_dropped self.bwstats.addDroppedPktStat(name, totalDropped) # ASSIGNMENT 2: # If the switch reporting the statistic is an edge switch, and the # port connects to a host, log the statistic to bwstats, using # self.bwstats.addHostBwStat(hostname, transmitted bytes, received bytes) # (Hint: you can look up the switch or host connected to a port using # self.topo.ports[switch name][port number]) for stat in body: if stat.port_no in self.topo.ports[name].keys(): device = self.topo.ports[name][stat.port_no] if device in self.topo.hosts: hostname = self.topo.ports[name][stat.port_no] self.bwstats.addHostBwStat(hostname,stat.tx_packets,stat.rx_packets) print hostname print stat.tx_bytes print stat.rx_bytes # periodically print tenant bandwidth usage self.statsReplied += 1 if self.statsReplied == len(self.datapaths): self.bwstats.updateTenantStats() self.logger.info(self.bwstats.tenantBwString())
class BandwidthMonitor(app_manager.RyuApp): OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] _CONTEXTS = { 'dpset': dpset.DPSet, } def __init__(self, *args, **kwargs): super(BandwidthMonitor, self).__init__(*args, **kwargs) self.datapaths = {} self.dpset = kwargs['dpset'] self.topo = Topology(cfgfile) self.bwstats = BandwidthStats(self.topo) self.monitor_thread = hub.spawn(self.monitor) @set_ev_cls(ofp_event.EventOFPStateChange, [MAIN_DISPATCHER, DEAD_DISPATCHER]) def stateChangeHandler(self, ev): datapath = ev.datapath if datapath.id: if ev.state == MAIN_DISPATCHER: if not datapath.id in self.datapaths: self.logger.debug('register datapath: %016x', datapath.id) self.datapaths[datapath.id] = datapath elif ev.state == DEAD_DISPATCHER: if datapath.id in self.datapaths: self.logger.debug('unregister datapath: %016x', datapath.id) del self.datapaths[datapath.id] def monitor(self): while True: self.statsReplied = 0 for dp in self.datapaths.values(): self.requestStats(dp) hub.sleep(STAT_REQUEST_PERIOD) def requestStats(self, datapath): ofproto = datapath.ofproto parser = datapath.ofproto_parser req = parser.OFPPortStatsRequest(datapath, 0, ofproto.OFPP_ANY) datapath.send_msg(req) @set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER) def statsReplyHandler(self, ev): # Here, a datapath (ie, switch) responds to the stats request. body = ev.msg.body datapath = ev.msg.datapath dpid = datapath.id # name of switch reporting data name = self.topo.dpidToName(dpid) totalDropped = 0 for stat in body: totalDropped += stat.tx_dropped totalDropped += stat.rx_dropped self.bwstats.addDroppedPktStat(name, totalDropped) # ASSIGNMENT 2: # If the switch reporting the statistic is an edge switch, and the # port connects to a host, log the statistic to bwstats, using # self.bwstats.addHostBwStat(hostname, transmitted bytes, received bytes) # (Hint: you can look up the switch or host connected to a port using # self.topo.ports[switch name][port number]) # [ ADD YOUR CODE HERE ] if self.topo.edgeSwitches.get(name, None): for stat in body: if stat.port_no in self.topo.ports[name].keys(): host = self.topo.ports[name][stat.port_no] if self.topo.hosts.get(host, None): self.bwstats.addHostBwStat(self.topo.hosts[host].name, stat.tx_bytes, stat.rx_bytes) # periodically print tenant bandwidth usage self.statsReplied += 1 if self.statsReplied == len(self.datapaths): self.bwstats.updateTenantStats() self.logger.info(self.bwstats.tenantBwString())
pass def exception(self, msg, *args, **kwargs): pass class MockBandwidthStats(object): def __init__(self, topo): self.topo = topo self.hostBw = {} def addHostBwStat(self, host, txbytes, rxbytes): pass cfgfile = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "config.txt")) if not os.path.isfile(cfgfile): print "Cannot run test without file", cfgfile print "To create file, run mdc (eg, sudo ./mdc --vid)" sys.exit(1) topo = Topology(cfgfile) policy = AdaptivePolicy(topo, MockBandwidthStats(topo), MockLogger()) policy.utilization['s101'] = 100 policy.utilization['s102'] = 50 policy.utilization['s103'] = 200 print "Testing with values:", str(policy.utilization) print "Expected min: s102" print "Your code returns min:", str(policy.minUtilization())
class BandwidthMonitor(app_manager.RyuApp): OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] _CONTEXTS = {"dpset": dpset.DPSet} def __init__(self, *args, **kwargs): super(BandwidthMonitor, self).__init__(*args, **kwargs) self.datapaths = {} self.dpset = kwargs["dpset"] self.topo = Topology(cfgfile) self.bwstats = BandwidthStats(self.topo) self.monitor_thread = hub.spawn(self.monitor) @set_ev_cls(ofp_event.EventOFPStateChange, [MAIN_DISPATCHER, DEAD_DISPATCHER]) def stateChangeHandler(self, ev): datapath = ev.datapath if datapath.id: if ev.state == MAIN_DISPATCHER: if not datapath.id in self.datapaths: self.logger.debug("register datapath: %016x", datapath.id) self.datapaths[datapath.id] = datapath elif ev.state == DEAD_DISPATCHER: if datapath.id in self.datapaths: self.logger.debug("unregister datapath: %016x", datapath.id) del self.datapaths[datapath.id] def monitor(self): while True: self.statsReplied = 0 for dp in self.datapaths.values(): self.requestStats(dp) hub.sleep(STAT_REQUEST_PERIOD) def requestStats(self, datapath): ofproto = datapath.ofproto parser = datapath.ofproto_parser req = parser.OFPPortStatsRequest(datapath, 0, ofproto.OFPP_ANY) datapath.send_msg(req) @set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER) def statsReplyHandler(self, ev): # Here, a datapath (ie, switch) responds to the stats request. body = ev.msg.body datapath = ev.msg.datapath dpid = datapath.id # name of switch reporting data name = self.topo.dpidToName(dpid) totalDropped = 0 for stat in body: totalDropped += stat.tx_dropped totalDropped += stat.rx_dropped self.bwstats.addDroppedPktStat(name, totalDropped) # ASSIGNMENT 2: # If the switch reporting the statistic is an edge switch, and the # port connects to a host, log the statistic to bwstats, using # self.bwstats.addHostBwStat(hostname, transmitted bytes, received bytes) # (Hint: you can look up the switch or host connected to a port using # self.topo.ports[switch name][port number]) # [ ADD YOUR CODE HERE ] # prntStr = "statsReplyHandler: name:" + name if name in self.topo.edgeSwitches.keys(): # print prntStr + "; type(body): %s; len(body): %d" % (type(body), len(body)) ix = 0 for stat in body: # print prntStr + "; body[%d]: port_no: %d; rx_bytes %d; tx_bytes %d" % \ # (ix, stat.port_no, stat.rx_bytes, stat.tx_bytes) if stat.port_no in self.topo.ports[name].keys(): if self.topo.ports[name][stat.port_no] in self.topo.hosts.keys(): # print prntStr + "; body[%d]: self.topo.ports[%s][%d]: %s" % \ # (ix, name, stat.port_no, self.topo.ports[name][stat.port_no]) self.bwstats.addHostBwStat(self.topo.ports[name][stat.port_no], stat.tx_bytes, stat.rx_bytes) ix += 1 # print prntStr + " self.topo.edgeSwitches[name].neighbors.len(): %d" % \ # (len(self.topo.edgeSwitches[name].neighbors)) # print prntStr + " self.topo.edgeSwitches[name].neighbors:" + \ # ', '.join(self.topo.edgeSwitches[name].neighbors) # for host in self.topo.edgeSwitches[name].neighbors: # print prntStr + " self.topo.edgeSwitches[name].neighbors[ix]: %s" % (host) # self.bwstats.addHostBwStat(host, ) # else: # print prntStr + " self.topo.edgeSwitches[name]:" + "None" # [ END MY CODE ] # periodically print tenant bandwidth usage self.statsReplied += 1 if self.statsReplied == len(self.datapaths): self.bwstats.updateTenantStats() self.logger.info(self.bwstats.tenantBwString())