Commit b2ca0113 authored by Praveen Kumar Shanmugam's avatar Praveen Kumar Shanmugam

Fixed the BW usage calculation module.

The results are way off from the actual usage though as using Mininet!

Need to map with a local mapping table to get around with this!
parent 5e0cdd03
......@@ -7,9 +7,13 @@ from ryu.controller.handler import (MAIN_DISPATCHER, DEAD_DISPATCHER,
from ryu.controller.handler import set_ev_cls
from ryu.lib.dpid import str_to_dpid,dpid_to_str
from ryu.lib import hub
import os
import sys
#sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from ryu.lib import tapDB
from ryu.lib import bwDB
from subprocess import Popen
import logging
#get all the tap points from the tap DB.
# for eac of the metering check the bandwidth associated with it.
......@@ -21,6 +25,8 @@ class dynamicRateLimiter(simple_switch_13.SimpleSwitch13):
self).__init__(*args, **kwargs)
self.datapaths = {}
self.monitor_thread = hub.spawn(self._monitor)
log = logging.getLogger()
log.setLevel(logging.INFO)
@set_ev_cls(ofp_event.EventOFPStateChange,
[MAIN_DISPATCHER, DEAD_DISPATCHER])
......@@ -46,7 +52,7 @@ class dynamicRateLimiter(simple_switch_13.SimpleSwitch13):
def _reconfigure_metering(self, tap):
bwList = bwDB.getBWEntry(tap)
for bw in bwList:
print bw
self.logger.info('reconfiguring : %s', str(bw))
dpid = str_to_dpid(tap.get('dpid'))
datapath = self.datapaths[dpid]
......@@ -67,8 +73,10 @@ class dynamicRateLimiter(simple_switch_13.SimpleSwitch13):
tapid = int(tap.get('tapID'))
meterid = tapid +1
self.logger.info('usedbw: %d', usedbw)
max_rate_limit = (bandwidth / 2) #max limit = 50%
increment = (bandwidth / (1024 * 100)) # 1% increment [bandwidth is in bytes]
increment = (bandwidth / (20)) # 5% increment [bandwidth is in Kbps]
if (pktError > 0):
print "Reduce the rate limiting in multiples of increment"
......
......@@ -7,7 +7,11 @@ from ryu.controller.handler import (MAIN_DISPATCHER, DEAD_DISPATCHER,
from ryu.controller.handler import set_ev_cls
from ryu.lib.dpid import dpid_to_str
from ryu.lib import hub
import os
import sys
#sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from ryu.lib import bwDB
import logging
class SimpleMonitor(simple_switch_13.SimpleSwitch13):
def __init__(self, *args, **kwargs):
......@@ -15,6 +19,8 @@ class SimpleMonitor(simple_switch_13.SimpleSwitch13):
self).__init__(*args, **kwargs)
self.datapaths = {}
self.monitor_thread = hub.spawn(self._monitor)
log = logging.getLogger()
log.setLevel(logging.INFO)
bwDB.createBWDBConnection();
@set_ev_cls(ofp_event.EventOFPStateChange,
......@@ -36,7 +42,8 @@ class SimpleMonitor(simple_switch_13.SimpleSwitch13):
self.datapaths[datapath.id] = datapath
for port,desc in datapath.ports.items():
bwDBEntry['port']=port
bwDBEntry['bandwidth']=desc.curr_speed
bwDBEntry['bandwidth']=(desc.curr_speed) / (1024) #Bps -> KBps
self.logger.debug('bw : %d', desc.curr_speed)
bwDB.insertBWDBDict(bwDBEntry);
elif ev.state == DEAD_DISPATCHER:
if datapath.id in self.datapaths:
......@@ -91,4 +98,5 @@ class SimpleMonitor(simple_switch_13.SimpleSwitch13):
'txDrops':stat.tx_dropped,
'timeStamp':stat.duration_sec
};
self.logger.debug('bwDbEntry: %s', str(bwDbEntry))
Please register or sign in to reply
bwDB.updateBWDict(bwDbEntry);
import os
import numpy as np
from os.path import isfile, getsize
import sqlite3 as sql
import logging
#single write and single read so no locks are implemented.
global bwdb
bwdb ='bwDataBase'
log = logging.getLogger()
log.setLevel(logging.INFO)
def createBWDBConnection():
if os.path.exists(bwdb):
os.remove(bwdb) #clear the sqllite file
......@@ -78,7 +84,7 @@ def getBWEntry(bwDBEntry):
cursor = conn.cursor()
cursor.execute("""select uniqID,bandwidth, usedbw, rxBytes, txBytes,
txDrops, pktError, timeStamp, ratelimit from bwTable where dpid=:dpid and
txDrops, pktError, timeStamp from bwTable where dpid=:dpid and
port=:port """,bwDBEntry);
bwList = []
......@@ -163,8 +169,8 @@ def dumpBWTable():
conn.close();
def updateBWDict(bwDBEntry):
print "updateBWDict: print entry for bandwidth table update query"
#print bwDBEntry
log.debug("updateBWDict: print entry for bandwidth table update query")
log.debug("updateBWDict: %s", str(bwDBEntry))
conn = sql.connect(bwdb)
cursor = conn.cursor()
......@@ -184,40 +190,53 @@ def updateBWDict(bwDBEntry):
pktError = bwDBEntry.get('txDrops') - txDrops
bwDBEntry['pktError'] = pktError
bwDBEntry['txDrops'] = txDrops
prevTimeStamp = row[2];
prevTimeStamp = (row[2]);
bandwidth = row[3];
txByte = bwDBEntry.get('txBytes');
txByte = (bwDBEntry.get('txBytes'));
timeStamp = bwDBEntry.get('timeStamp');
newBytes = txByte - prevTxBytes;
newBytes = (txByte - prevTxBytes);
sec = timeStamp - prevTimeStamp;
try:
rate = newBytes/sec;
KB = ((float(newBytes))/(1024)); #bytes -> KB
rate = (KB/sec); #KBps
utilization = ((rate/float(bandwidth)) * 100) #in percentage
except ZeroDivisionError:
print "zero timestamp; set rate to 0"
log.info("division by zero error setting to 0")
rate = 0;
utilization = 0;
utilization = (bandwidth* 100/rate) #in percentage
#utilization = (rate * 100 / bandwidth) #in percentage
#print "calculated bytes, time and rate"
#print newBytes;
#print sec;
#print rate;
log.info("switch:%s port:%d bw[%d]: newBytes[%d], sec[%d], KB[%f], rate[%f], "
"utilization:[%d]", bwDBEntry.get('dpid'),
int(bwDBEntry.get('port')), bandwidth, newBytes,
sec, KB, rate, utilization)
if utilization != 0:
print ('switch:', bwDBEntry.get('dpid'),
'port:' , bwDBEntry.get('port'),
'[usage]:',utilization)
bwDBEntry['usedbw']= utilization;
cursor.execute("""
update bwTable set usedbw=:usedbw, rxBytes=:rxBytes,
txBytes=:txBytes, txDrops=:txDrops, pktError=:pktError, timeStamp=:timeStamp
where dpid=:dpid and port=:port""", bwDBEntry);
conn.commit();
#print "update done"
bwDBEntry['usedbw']= int(utilization)
cursor.execute("""
update bwTable set usedbw=:usedbw, rxBytes=:rxBytes,
txBytes=:txBytes, txDrops=:txDrops, pktError=:pktError, timeStamp=:timeStamp
where dpid=:dpid and port=:port""", bwDBEntry);
conn.commit();
#print "update done"
elif row is not None:
log.info("Update the first entry!!")
bwDBEntry['usedbw']= 0;
cursor.execute("""
update bwTable set usedbw=:usedbw, rxBytes=:rxBytes,
txBytes=:txBytes, txDrops=:txDrops, pktError=:pktError, timeStamp=:timeStamp
where dpid=:dpid and port=:port""", bwDBEntry);
conn.commit();
else:
log.info("Nothing to update --- WRONG ---!!")
rc = False
except sql.Error as e:
print "An error occured ",e.args[0];
log.debug("An error occured %s ", e.args[0]);
rc = False
cursor.close();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment