/
load_balance.py
309 lines (253 loc) · 9.11 KB
/
load_balance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: jinpf
# @Date: 2014-05-31 16:46:10
# @Last Modified by: jinpf
# @Last Modified time: 2014-06-01 00:22:07
# @Email: jpflcj@sina.com
"""
# @comment here:
"""
from pox.core import core
import pox.openflow.libopenflow_01 as of
from pox.openflow.discovery import Discovery
from pox.lib.packet.arp import arp
from pox.lib.packet.ethernet import ethernet
from pox.lib.addresses import EthAddr
from pox.lib.recoco import Timer
import logging
Swich_Connect_Info={} #Swich_Connect_Info={dpid1:{dpid2:(port1,cost)}} , cost stands for link cost
IP_To_MAC={} #IP_To_MAC={IP:mac}
Host_Info={} #Host_Info={mac:(dpid,port,ip)} , record host direct connect switch
MAXINT=9999
Special_MAC=EthAddr('11:11:11:11:11:11')
###############################
## arp proxy function ###
###############################
#when don`t know ip->mac ,flood arp
def Arp_Flood(arp_packet):
#construct arp request packet
arp_request=arp()
arp_request.hwsrc=arp_packet.hwsrc #Special_MAC
arp_request.hwdst=arp_packet.hwdst #EthAddr(b"\xff\xff\xff\xff\xff\xff")
arp_request.opcode=arp.REQUEST
arp_request.protosrc=arp_packet.protosrc
arp_request.protodst=arp_packet.protodst
ether=ethernet()
ether.type=ethernet.ARP_TYPE
ether.dst=EthAddr(b"\xff\xff\xff\xff\xff\xff")
ether.src=Special_MAC
ether.payload=arp_request
msg=of.ofp_packet_out()
msg.data=ether.pack()
msg.actions.append(of.ofp_action_output(port=of.OFPP_FLOOD))
for connection in core.openflow.connections:
connection.send(msg)
#when we know ip->mac ,reply
def Arp_Reply(event,arp_packet):
#construct arp reply packet
arp_reply=arp()
arp_reply.hwsrc=IP_To_MAC[arp_packet.protodst]
arp_reply.hwdst=arp_packet.hwsrc
arp_reply.opcode=arp.REPLY
arp_reply.protosrc=arp_packet.protodst
arp_reply.protodst=arp_packet.protosrc
ether=ethernet()
ether.type=ethernet.ARP_TYPE
ether.dst=arp_packet.hwsrc
ether.src=IP_To_MAC[arp_packet.protodst]
ether.payload=arp_reply
# send the created arp reply back to switch
msg=of.ofp_packet_out()
msg.data=ether.pack()
msg.actions.append(of.ofp_action_output(port=of.OFPP_IN_PORT))
msg.in_port=event.port
event.connection.send(msg)
################################
### find path algorithm ###
################################
#compare rule,put in min(),data as tumple,data[1] stands for compare data,if data[0] in list L,data[1] won`t be counted when compare
def compare_rule(data,L):
if data[0] in L:
return MAXINT
else:
return data[1]
#return start -> end path in list
def Dijkstra(start,end):
path={} #all shortest path from start
dist={} #dist[i] stands for shortest distance from start to i node
Switches=Swich_Connect_Info.keys() #list of switches
Used_Sw=[start] #already used switches in algorithm
#inital
for sw in Switches:
path[sw]=[]
dist[sw]=MAXINT
mstart=start #mstart stands for start node in each loop
path[start]=[start]
dist[start]=0
while(len(Used_Sw)<len(Switches)):
for sw in Swich_Connect_Info[mstart]:
if sw not in Used_Sw:
if dist[sw] >dist[mstart]+Swich_Connect_Info[mstart][sw][1]:
dist[sw]=dist[mstart]+Swich_Connect_Info[mstart][sw][1]
path[sw]=path[mstart]+[sw]
min_sw=min(dist.items(), key=lambda x: compare_rule(x,Used_Sw)) [0]
Used_Sw.append(min_sw)
mstart=min_sw
print 'path :',start,'->',end,' : ',path[end]
return path[end]
#find all path by using information form Swich_Connect_Info , return all path
def Floyd():
All_Path={} #All_Path={dpid1:dpid2:[]} , record path from dpid1 to dpid2
dist={} #dist[start][end] stands for shortest distance from start to end node
Switches=Swich_Connect_Info.keys() #list of switches
#inital
for src_sw in Switches:
if src_sw not in All_Path:
All_Path[src_sw]={}
if src_sw not in dist:
dist[src_sw]={}
for dst_sw in Switches:
All_Path[src_sw][dst_sw]=[]
if src_sw==dst_sw:
dist[src_sw][dst_sw]=0
else:
dist[src_sw][dst_sw]=MAXINT
for src_sw in Swich_Connect_Info:
for dst_sw in Swich_Connect_Info[src_sw]:
dist[src_sw][dst_sw]=Swich_Connect_Info[src_sw][dst_sw][1]
for mid_sw in Switches:
for src_sw in Switches:
for dst_sw in Switches:
if mid_sw != src_sw and mid_sw != dst_sw:
if dist[src_sw][dst_sw] > dist[src_sw][mid_sw]+dist[mid_sw][dst_sw]:
dist[src_sw][dst_sw]=dist[src_sw][mid_sw]+dist[mid_sw][dst_sw]
All_Path[src_sw][dst_sw]=All_Path[src_sw][mid_sw]+[mid_sw]+All_Path[mid_sw][dst_sw]
for src_sw in Switches:
for dst_sw in Switches:
if src_sw == dst_sw:
All_Path[src_sw][dst_sw]=[src_sw]
elif len(All_Path[src_sw][dst_sw]) != 0 or dist [src_sw][dst_sw] < MAXINT:
All_Path[src_sw][dst_sw]=[src_sw]+All_Path[src_sw][dst_sw]+[dst_sw]
print 'path :',src_sw,'->',dst_sw,' : ',All_Path[src_sw][dst_sw]
return All_Path
##################################
### flow operation function ###
##################################
#install flow on path switch
def Install_Path_Flow(src,dst,path=None,event=None):
start_sw=Host_Info[src][0]
end_sw=Host_Info[dst][0]
if path==None:
path=Dijkstra(start_sw,end_sw)
print 'Install path flow -----------------------------'
#install flow for the path
for i in range(len(path)):
#flow for src->dst
msg1=of.ofp_flow_mod()
msg1.match.dl_dst=dst
msg1.match.dl_src=src
if i==len(path)-1: #last node
msg1.actions.append(of.ofp_action_output( port=Host_Info[dst][1] ) )
else:
msg1.actions.append(of.ofp_action_output( port=Swich_Connect_Info[ path[i] ][ path[i+1] ][0] ))
core.openflow.sendToDPID(path[i],msg1)
#flow for dst->src
msg2=of.ofp_flow_mod()
msg2.match.dl_dst=src
msg2.match.dl_src=dst
if i==0: #first node
msg2.actions.append(of.ofp_action_output( port=Host_Info[src][1] ) )
else:
msg2.actions.append(of.ofp_action_output( port=Swich_Connect_Info[ path[i] ][ path[i-1] ][0] ))
core.openflow.sendToDPID(path[i],msg2)
#send out this Packedin data
if event !=None:
i=path.index(event.dpid)
msg=of.ofp_packet_out(data=event.data)
if i==len(path)-1:
msg.actions.append(of.ofp_action_output(port=Host_Info[dst][1]))
else:
msg.actions.append(of.ofp_action_output(port=Swich_Connect_Info[ path[i] ][ path[i+1] ][0] ))
msg.in_port=event.port
event.connection.send(msg)
#install all flow
def Install_All_Flow(message):
print message
All_Path=Floyd()
for src_host in Host_Info.keys():
for dst_host in Host_Info.keys():
if src_host != dst_host:
Install_Path_Flow(src_host,dst_host,path=All_Path[ Host_Info[src_host][0] ][ Host_Info[dst_host][0] ])
print
#delete all flow on all switch
def Delete_All_Flow():
msg = of.ofp_flow_mod(match = of.ofp_match(),command = of.OFPFC_DELETE)
for connection in core.openflow.connections:
connection.send(msg)
def Clear_All_Information(message):
print message
IP_To_MAC={}
Host_Info={}
Delete_All_Flow()
print
################################
### handle openflow event ####
################################
class Link_Learning(object):
def __init__(self):
core.openflow.addListeners(self)
core.openflow_discovery.addListeners(self)
def _handle_LinkEvent(self,event):
dpid1=event.link[0]
dpid2=event.link[2]
if event.added is True:
if Swich_Connect_Info[dpid1].get(dpid2) is None:
Swich_Connect_Info[dpid1][dpid2]=(event.link[1],1) #cost default set 1
Swich_Connect_Info[dpid2][dpid1]=(event.link[3],1) #cost default set 1
if event.removed is True:
if Swich_Connect_Info[dpid1].get(dpid2) is not None:
del Swich_Connect_Info[dpid1][dpid2]
del Swich_Connect_Info[dpid2][dpid1]
Delete_All_Flow()
#remain to do some work
# print Swich_Connect_Info
def _handle_ConnectionUp(self,event):
Swich_Connect_Info[event.dpid]={}
def _handle_ConnectionDown(self,event):
del Swich_Connect_Info[event.dpid]
for host in Host_Info.keys():
if Host_Info[host][0]==event.dpid:
del Host_Info[host]
def _handle_PacketIn(self,event):
packet = event.parsed
if packet.src!=Special_MAC: #filt packet send by ourself
if packet.find("arp"):
arp_packet=packet.find("arp")
# print 'switch:',event.dpid,'packet_in:','arp_packet:',arp_packet
Host_Info[packet.src]=(event.dpid,event.port,arp_packet.protosrc)
IP_To_MAC[arp_packet.protosrc]=packet.src
if arp_packet.opcode==arp.REQUEST:
if arp_packet.protodst in IP_To_MAC:
Arp_Reply(event,arp_packet)
else:
Arp_Flood(arp_packet)
if packet.find('ipv4'):
ip_packet=packet.find('ipv4')
IP_To_MAC[ip_packet.srcip]=packet.src
Host_Info[packet.src]=(event.dpid,event.port,ip_packet.srcip)
if (packet.src in Host_Info) and (packet.dst in Host_Info):
Install_Path_Flow(packet.src,packet.dst,event=event)
# print 'switch:',event.dpid,'packet_in:',packet
# print 'IP to MAC',IP_To_MAC
# print 'Host_Info:',Host_Info
# print
def launch():
#clear some unimportant message
core.getLogger("packet").setLevel(logging.ERROR)
core.registerNew(Discovery,explicit_drop=False,install_flow = False)
core.registerNew(Link_Learning)
#update flow
Timer(120,Install_All_Flow,recurring=True,args=["All Path------------------"])
#clear all information to keep everything new
# Timer(150,Clear_All_Information,recurring=True,args=["Clear All Information!"])