Last active
September 13, 2017 12:26
-
-
Save hyhilman/b260d2e7a4fe7c257c59cf3c32dbb5ef to your computer and use it in GitHub Desktop.
Load Balancer Swtich with Round Robin or Weighted RR or Simple Least Connection algorithm with Pyretic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyretic.lib.corelib import * | |
from pyretic.lib.std import * | |
from pyretic.lib.query import * | |
from pyretic.modules.mac_learner import * | |
from sys import maxint | |
################################################ | |
# Translate from | |
# client -> public address : client -> servers | |
# server -> client : public address -> client | |
################################################ | |
def translate(c, s, p): | |
cp = match(srcip=c, dstip=p) | |
sc = match(srcip=s, dstip=c) | |
return ((cp >> modify(dstip=s)) + | |
(sc >> modify(srcip=p)) + | |
(~cp & ~sc)) | |
############################################## | |
# Simple round-robin load balancing policy | |
############################################## | |
class rrlb(DynamicPolicy): | |
def __init__(self, clients, servers, public_ip): | |
super(rrlb,self).__init__() | |
print("Server addresses", servers) | |
self.clients = clients | |
self.servers = servers | |
self.public_ip = public_ip | |
self.index = 0 | |
#self.switch = sw | |
self.query = packets(1,['srcip']) | |
self.query.register_callback(self.update_policy) | |
self.public_to_controller = (match(dstip=self.public_ip) >> self.query) | |
self.lb_policy = None | |
self.policy = self.public_to_controller | |
def update_policy(self, pkt): | |
client = pkt['srcip'] | |
dest = pkt ['dstip'] | |
# Becareful not to redirect servers on themselves | |
if client in self.servers: | |
return | |
server = self.next_server() | |
p = translate(client, server, self.public_ip) | |
print("Mapping c:%s to s:%s" % (client, server)) | |
if self.lb_policy: | |
self.lb_policy = self.lb_policy >> p # >> dinamis() | |
else: | |
self.lb_policy = p | |
self.policy = self.lb_policy + self.public_to_controller #+ self.query | |
print "ini dari %s to %s" % (client,server) | |
def next_server(self): | |
server = self.servers[self.index % len(self.servers)] | |
self.index += 1 | |
return server | |
######################################################################## | |
# Modified round-robin load balancing policy with circularsinglelinkedlist | |
######################################################################## | |
class rrlbMod(DynamicPolicy): | |
def __init__(self, clients, servers, public_ip): | |
super(rrlbMod, self).__init__() | |
print("Server addresses", servers) | |
self.clients = clients | |
self.servers = servers | |
self.public_ip = public_ip | |
# self.switch = sw | |
self.server = self.CircularSingleLinkedList(servers[0], None) | |
self.create_server_nodes(servers) | |
self.query = packets(1, ['srcip']) | |
self.query.register_callback(self.update_policy) | |
self.public_to_controller = (match(dstip=self.public_ip) >> self.query) | |
self.lb_policy = None | |
self.policy = self.public_to_controller | |
def create_server_nodes(self, servers): | |
temp = self.server | |
for i in range(1, len(servers) + 1): | |
try: | |
recent = self.CircularSingleLinkedList(servers[i], None) | |
temp.nextlink = recent | |
temp = recent | |
except IndexError: | |
temp.nextlink = self.server | |
def update_policy(self, pkt): | |
client = pkt['srcip'] | |
dest = pkt['dstip'] | |
# Becareful not to redirect servers on themselves | |
if client in self.servers: return | |
server = self.next_server() | |
p = translate(client, server, self.public_ip) | |
print("Mapping c:%s to s:%s" % (client, server)) | |
if self.lb_policy: | |
self.lb_policy = self.lb_policy >> p # >> dinamis() | |
else: | |
self.lb_policy = p | |
self.policy = self.lb_policy + self.public_to_controller # + self.query | |
print "ini dari %s to %s" % (client, server) | |
def next_server(self): | |
server = self.server | |
self.server = self.server.nextlink | |
return server.content | |
class CircularSingleLinkedList(): | |
content = None | |
nextlink = None | |
def __init__(self, content, nextlink): | |
self.content = content | |
self.nextlink = nextlink | |
def nextlink(self): | |
return self.nextlink | |
############################################### | |
# Simple least-connection load balancing policy | |
############################################### | |
class lclb(DynamicPolicy): | |
def __init__(self, clients, servers, public_ip): | |
super(lclb,self).__init__() | |
print("Server addresses", servers) | |
self.clients = clients | |
self.servers = servers | |
self.public_ip = public_ip | |
#self.switch = sw | |
self.server = self.CircularSingleLinkedList(servers[0], None) | |
self.create_server_nodes(servers) | |
self.query = packets(1,['srcip']) | |
self.query.register_callback(self.update_policy) | |
self.public_to_controller = (match(dstip=self.public_ip) >> self.query) | |
self.lb_policy = None | |
self.policy = self.public_to_controller | |
def create_server_nodes(self, servers): | |
temp = self.server | |
for i in range(1, len(servers) + 1): | |
try: | |
recent = self.CircularSingleLinkedList(servers[i], None) | |
temp.nextlink = recent | |
temp = recent | |
except IndexError: | |
temp.nextlink = self.server | |
def update_policy(self, pkt): | |
client = pkt['srcip'] | |
dest = pkt ['dstip'] | |
# Becareful not to redirect servers on themselves | |
if client in self.servers: return | |
server = self.next_server() | |
p = translate(client, server, self.public_ip) | |
print("Mapping c:%s to s:%s" % (client, server)) | |
if self.lb_policy: | |
self.lb_policy = self.lb_policy >> p # >> dinamis() | |
else: | |
self.lb_policy = p | |
self.policy = self.lb_policy + self.public_to_controller #+ self.query | |
print "ini dari %s to %s" % (client,server) | |
def next_server(self): | |
while self.server.currentconn >= 1: | |
self.server = self.server.nextlink | |
self.server.currentconn = self.server.currentconn + 1 | |
server = self.server | |
return server.content | |
class CircularSingleLinkedList(): | |
content = None | |
nextlink = None | |
currentconn = 0 | |
def __init__(self, content, nextlink, maxconn): | |
self.content = content | |
self.nextlink = nextlink | |
self.maxconn = maxconn | |
def nextlink(self): | |
return self.nextlink | |
######################################################################## | |
# Weighted-round-robin load balancing policy with circularsinglelinkedlist | |
######################################################################## | |
class wrrlb(DynamicPolicy): | |
def __init__(self, clients, servers, public_ip, servers_max): | |
super(wrrlb,self).__init__() | |
print("Server addresses", servers) | |
self.clients = clients | |
self.servers = servers | |
self.public_ip = public_ip | |
#self.switch = sw | |
self.server = self.CircularSingleLinkedList(servers[0], None, servers_max[0]) | |
self.create_server_nodes(servers, servers_max) | |
self.query = packets(1,['srcip']) | |
self.query.register_callback(self.update_policy) | |
self.public_to_controller = (match(dstip=self.public_ip) >> self.query) | |
self.lb_policy = None | |
self.policy = self.public_to_controller | |
def create_server_nodes(self, servers, servers_max): | |
temp = self.server | |
for i in range(1, len(servers) + 1): | |
try: | |
recent = self.CircularSingleLinkedList(servers[i], None, servers_max[i]) | |
temp.nextlink = recent | |
temp = recent | |
except IndexError: | |
temp.nextlink = self.server | |
def update_policy(self, pkt): | |
client = pkt['srcip'] | |
dest = pkt ['dstip'] | |
# Becareful not to redirect servers on themselves | |
if client in self.servers: return | |
server = self.next_server() | |
p = translate(client, server, self.public_ip) | |
print("Mapping c:%s to s:%s" % (client, server)) | |
if self.lb_policy: | |
self.lb_policy = self.lb_policy >> p # >> dinamis() | |
else: | |
self.lb_policy = p | |
self.policy = self.lb_policy + self.public_to_controller #+ self.query | |
print "ini dari %s to %s" % (client,server) | |
def next_server(self): | |
while self.server.currentconn >= self.server.maxconn : | |
self.server = self.server.nextlink | |
self.server.currentconn = self.server.currentconn + 1 | |
server = self.server | |
return server.content | |
class CircularSingleLinkedList(): | |
content = None | |
nextlink = None | |
maxconn = 0 | |
currentconn = 0 | |
def __init__(self, content, nextlink, maxconn): | |
self.content = content | |
self.nextlink = nextlink | |
self.maxconn = maxconn | |
def nextlink(self): | |
return self.nextlink | |
def main(): | |
public_ip = IP("10.0.0.100") | |
print("public ip address is %s." % public_ip) | |
client_ips = [IP("10.0.0.5"),IP("10.0.0.6"),IP("10.0.0.7"),IP("10.0.0.8"),IP("10.0.0.9")] | |
server_ips = [IP("10.0.0.1"),IP("10.0.0.2"),IP("10.0.0.3"),IP("10.0.0.4")] | |
server_max = [1,3,2,6] | |
# rrlb | |
# policy = (rrlb(client_ips, server_ips, public_ip) >> flood()) | |
# rrlbmod | |
# policy = (rrlbMod(client_ips, server_ips, public_ip) >> flood()) | |
# wrrlb | |
policy = (wrrlb(client_ips, server_ips, public_ip, server_max) >> flood()) | |
# lclb - masih belum | |
# policy = (lclb(client_ips, server_ips, public_ip) >> flood()) | |
print policy | |
return policy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment