Skip to content

Commit 31b9101

Browse files
committed
Merge pull request #85 from GlobalNOC/1.0.4-dev
1.0.4 dev
2 parents 767447c + 0288a4a commit 31b9101

File tree

5 files changed

+78
-59
lines changed

5 files changed

+78
-59
lines changed

etc/SciPass.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<switch dpid="0000000000000001">
44
<domain name="R&amp;E" mode="SciDMZ" admin_status="active" least_specific_prefix_len="24" most_specific_prefix_len="32"
5-
ipv6least_specific_prefix_len="96" ipv6most_specific_prefix_len="128" blacklist_priority="1000" whitelist_priority="900"
5+
ipv6least_specific_prefix_len="48" ipv6most_specific_prefix_len="64" blacklist_priority="1000" whitelist_priority="900"
66
sensor_min_load_threshold="0.1" sensor_load_delta_threshold="0.1" sensor_configurable_threshold="100"
77
max_prefixes="200" max_flow_count="250" idle_timeout="90" hard_timeout="300" >
88

python/Ryu.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def __init__(self,*args, **kwargs):
173173
self.balance_thread = hub.spawn(self._balance_loop)
174174

175175
self.ports = defaultdict(dict);
176-
self.prefix_bytes = defaultdict(lambda: defaultdict(int))
176+
self.prefix_bytes = defaultdict(lambda: defaultdict(dict))
177177
self.lastStatsTime = {}
178178
self.flowmods = {}
179179

@@ -1010,13 +1010,18 @@ def process_flow_stats_of13(self, stats, dp):
10101010

10111011
for prefix in prefix_bytes:
10121012
for d in ("rx","tx"):
1013-
old_bytes = self.prefix_bytes[prefix][d]
1013+
old_bytes = 0
1014+
if(self.prefix_bytes[dpid][prefix].has_key(d)):
1015+
old_bytes = self.prefix_bytes[dpid][prefix][d]
10141016
new_bytes = prefix_bytes[prefix][d]
10151017
bytes = new_bytes - old_bytes
1018+
self.logger.debug("Switch : " + str(dpid))
1019+
self.logger.debug("Prefix : " + str(prefix))
1020+
self.logger.debug("Bytes %s : %d ", d, bytes)
10161021
#if we are less than the previous counter then we re-balanced
10171022
#set back to 0 and start again
10181023
if(bytes < 0):
1019-
self.prefix_bytes[prefix][d] = 0
1024+
self.prefix_bytes[dpid][prefix][d] = 0
10201025
bytes = 0
10211026

10221027
if(stats_et == None):
@@ -1029,7 +1034,7 @@ def process_flow_stats_of13(self, stats, dp):
10291034
rate = 0
10301035

10311036
prefix_bps[prefix][d] = rate
1032-
self.prefix_bytes[prefix][d] = prefix_bytes[prefix][d]
1037+
self.prefix_bytes[dpid][prefix][d] = prefix_bytes[prefix][d]
10331038

10341039
#--- update the balancer
10351040
for prefix in prefix_bps.keys():
@@ -1153,13 +1158,18 @@ def process_flow_stats_of10(self, stats, dp):
11531158

11541159
for prefix in prefix_bytes:
11551160
for dir in ("rx","tx"):
1156-
old_bytes = self.prefix_bytes[prefix][dir]
1161+
old_bytes = 0
1162+
if(self.prefix_bytes[dpid][prefix].has_key(dir)):
1163+
old_bytes = self.prefix_bytes[dpid][prefix][dir]
11571164
new_bytes = prefix_bytes[prefix][dir]
11581165
bytes = new_bytes - old_bytes
11591166
#if we are less than the previous counter then we re-balanced
11601167
#set back to 0 and start again
1168+
self.logger.debug("Switch : " + str(dpid))
1169+
self.logger.debug("Prefix : " + str(prefix))
1170+
self.logger.debug("Bytes %s : %d ", dir, bytes)
11611171
if(bytes < 0):
1162-
self.prefix_bytes[prefix][dir] = 0
1172+
self.prefix_bytes[dpid][prefix][dir] = 0
11631173
bytes = 0
11641174

11651175
if(stats_et == None):
@@ -1172,7 +1182,7 @@ def process_flow_stats_of10(self, stats, dp):
11721182
rate = 0
11731183

11741184
prefix_bps[prefix][dir] = rate
1175-
self.prefix_bytes[prefix][dir] = prefix_bytes[prefix][dir]
1185+
self.prefix_bytes[dpid][prefix][dir] = prefix_bytes[prefix][dir]
11761186

11771187

11781188
#--- update the balancer

python/SimpleBalancer.py

+4-50
Original file line numberDiff line numberDiff line change
@@ -192,52 +192,6 @@ def getPrefixPriority(self, prefix):
192192
if(pfix.Contains(prefix)):
193193
return self.prefixPriorities[pfix]
194194

195-
def showStatus(self):
196-
"""returns text represention in show format of the current status balancing"""
197-
198-
mode = "Sensor Load and Prefix Bandwidth"
199-
200-
if(self.ignoreSensorLoad > 0):
201-
mode = "Prefix Bandwidth"
202-
203-
if(self.ignoreSensorLoad > 0 and self.ignorePrefixBW > 0):
204-
mode = "IP Space"
205-
206-
status = "";
207-
totalHosts = 0
208-
totalBW = 0;
209-
210-
status = "Balance Method: %s:\n" % mode
211-
212-
sensorHosts = defaultdict(int)
213-
sensorBW = defaultdict(int)
214-
for prefix in self.prefixSensor:
215-
totalHosts = totalHosts + prefix.numhosts
216-
totalBW = totalBW + self.prefixBW[prefix]
217-
sensor = self.prefixSensor[prefix]
218-
sensorBW[sensor] = sensorBW[sensor] + self.prefixBW[prefix]
219-
220-
lastCount = sensorHosts[sensor]
221-
sensorHosts[sensor] = lastCount + prefix.numhosts
222-
223-
for sensor in self.sensorLoad:
224-
sensorHostVal = sensorHosts[sensor] / float(totalHosts)
225-
try:
226-
sensorBwPer = sensorBW[sensor] / float(totalBW)
227-
except ZeroDivisionError:
228-
sensorBwPer = 0
229-
230-
status = status + "sensor: '"+sensor+"' bw: %.2f load: %.3f hosts: %.3f"%(sensorBwPer,self.sensorLoad[sensor],sensorHostVal )+"\n"
231-
232-
for prefix in self.prefixSensor:
233-
if(self.prefixSensor[prefix] == sensor):
234-
prefixBW = self.prefixBW[prefix]
235-
status = status + " "+str(prefix)+": %.3f "%(prefixBW/1000000.0)+"mbps\n"
236-
237-
status = status + "\n"
238-
239-
return status
240-
241195

242196
#distributes prefixes through all groups
243197
#this is not going to be event but it is at least a start
@@ -779,11 +733,11 @@ def merge(self):
779733

780734
if(not self.getGroupStatus(group)): continue
781735

782-
load = self.getGroupLoad(group)
736+
load = self.getGroupLoad(group)
783737

784-
if(load < minLoad):
785-
minLoad = load
786-
minSensor = group;
738+
if(load < minLoad):
739+
minLoad = load
740+
minSensor = group;
787741

788742
self.logger.debug("Min Group: " + str(minSensor))
789743
group_a = self.getPrefixGroup(prefix_a)

python/t/SimpleBalancerTest.py

+55
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
import unittest
66
import xmlrunner
77
import logging
8+
import os
9+
import json
10+
import time
811
from SimpleBalancer import SimpleBalancer,MaxPrefixlenError
12+
from SciPass import SciPass
913
from collections import defaultdict
14+
from mock import Mock
1015

1116
class TestInit(unittest.TestCase):
1217

@@ -531,10 +536,60 @@ def test_to_string(self):
531536
self.balancer = SimpleBalancer()
532537
self
533538

539+
class TestStateChange(unittest.TestCase):
540+
541+
def setUp(self):
542+
self.api = SciPass( logger = logging.getLogger(__name__),
543+
config = str(os.getcwd()) + "/t/etc/SciPass_balancer_only.xml",
544+
readState = False)
545+
self.datapath = Mock(id=1)
546+
self.api.switchJoined(self.datapath)
547+
self.state = "/var/run/" + "%016x" % self.datapath.id + "IUPUI" + ".json"
548+
549+
def tearDown(self):
550+
os.remove(self.state)
551+
552+
553+
def test_initial_config(self):
554+
assert(os.path.isfile(self.state) == 1)
555+
with open(self.state) as data_file:
556+
data = json.load(data_file)
557+
data = data[0]
558+
switches = data["switch"].keys()
559+
assert(switches[0] == "%016x" % self.datapath.id)
560+
domain = data["switch"]["%016x" % self.datapath.id]["domain"].keys()
561+
assert(domain[0] == "IUPUI")
562+
mode = data["switch"]["%016x" % self.datapath.id]["domain"][domain[0]]["mode"].keys()
563+
assert(mode[0] == "Balancer")
564+
565+
def test_state_restore(self):
566+
net1 = ipaddr.IPv4Network("192.168.0.0/24")
567+
group = self.api.getBalancer("%016x" % self.datapath.id, "IUPUI").getPrefixGroup(net1)
568+
res = self.api.getBalancer("%016x" % self.datapath.id, "IUPUI").splitSensorPrefix(group,net1,check=False)
569+
self.assertTrue(res == 1)
570+
net2 = ipaddr.IPv6Network("2001:0DB8::/48")
571+
group = self.api.getBalancer("%016x" % self.datapath.id, "IUPUI").getPrefixGroup(net2)
572+
res = self.api.getBalancer("%016x" % self.datapath.id, "IUPUI").splitSensorPrefix(group,net2,check=False)
573+
self.assertTrue(res == 1)
574+
self.api.switchLeave(self.datapath)
575+
time.sleep(3)
576+
self.api = SciPass( logger = logging.getLogger(__name__),
577+
config = str(os.getcwd()) + "/t/etc/SciPass_balancer_only.xml",
578+
readState = True)
579+
self.api.switchJoined(self.datapath)
580+
prefixes = self.api.getBalancer("%016x" % self.datapath.id, "IUPUI").getPrefixes()
581+
prefixList = prefixes.keys()
582+
assert(net1 not in prefixList)
583+
assert(net2 not in prefixList)
584+
net = [ipaddr.IPv4Network('192.168.0.0/25'), ipaddr.IPv4Network('192.168.0.128/25')]
585+
assert(n in prefixList for n in net)
586+
net = [ipaddr.IPv6Network('2001:db8::/49'), ipaddr.IPv6Network('2001:db8:0:8000::/49')]
587+
assert(n in prefixList for n in net)
534588

535589
def suite():
536590
suite = unittest.TestLoader().loadTestsFromTestCase(TestInit)
537591
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestSensorMods))
538592
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestPrefix))
539593
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestBalance))
594+
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestStateChange))
540595
return suite

python/t/Test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
simple_balancer_only_tests = SimpleBalancerOnlyTest.suite()
2222
inline_tests = InlineTest.suite()
2323
suite = unittest.TestSuite([scipasstests, simplebalancertests, balancer_only_tests, inline_tests, simple_balancer_only_tests])
24-
24+
2525
xmlrunner.XMLTestRunner(output='test-reports').run(suite)
2626

0 commit comments

Comments
 (0)