-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPolicy.py
executable file
·199 lines (170 loc) · 8.54 KB
/
Policy.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
#i!/usr/bin/python
"""
@Author <Name/Matricno>
Date :
"""
import httplib
import json
import time
class flowStat(object):
def __init__(self, server):
self.server = server
def get(self, switch):
ret = self.rest_call({}, 'GET', switch)
return json.loads(ret[2])
def rest_call(self, data, action, switch):
path = '/wm/core/switch/'+switch+"/flow/json"
headers = {
'Content-type': 'application/json',
'Accept': 'application/json',
}
body = json.dumps(data)
conn = httplib.HTTPConnection(self.server, 8080)
#print path
conn.request(action, path, body, headers)
response = conn.getresponse()
ret = (response.status, response.reason, response.read())
conn.close()
return ret
class StaticFlowPusher(object):
def __init__(self, server):
self.server = server
def get(self, data):
ret = self.rest_call({}, 'GET')
return json.loads(ret[2])
def set(self, data):
ret = self.rest_call(data, 'POST')
return ret[0] == 200
def remove(self, objtype, data):
ret = self.rest_call(data, 'DELETE')
return ret[0] == 200
def rest_call(self, data, action):
path = '/wm/staticflowpusher/json'
headers = {
'Content-type': 'application/json',
'Accept': 'application/json',
}
body = json.dumps(data)
conn = httplib.HTTPConnection(self.server, 8080)
conn.request(action, path, body, headers)
response = conn.getresponse()
ret = (response.status, response.reason, response.read())
#print ret
conn.close()
return ret
pusher = StaticFlowPusher('127.0.0.1')
flowget = flowStat('127.0.0.1')
# To insert the policies for the traffic applicable to path between S1 and S2
def S1toS2():
S1Staticflow1 = {'switch':"00:00:00:00:00:00:00:01","name":"S1h1toh2","cookie":"0",
"priority":"2","in_port":"1","eth_type":"0x800","ipv4_src":"10.0.0.1",
"ipv4_dst":"10.0.0.2","active":"true","actions":"set_queue=1,output=2"}
pusher.set(S1Staticflow1)
# To insert the policies for the traffic applicable to path between S2 and S3
def S2toS3():
for x in range(1000, 1101):
val = str(x)+ "S2Staticflow3"
y = str(x)
val = {'switch':"00:00:00:00:00:00:00:02","name":"S2h2toh3"+y,"cookie":"0",
"priority":"2","in_port":"1", "protocol": "0x11",
"eth_type":"0x800","ipv4_src":"10.0.0.2", "udp_src": y, "udp_dst": y,
"ipv4_dst":"10.0.0.3","active":"true"}
pusher.set(val)
# To insert the policies for the traffic applicable to path between S1 and S3
def S1toS3():
byte_count=0
while(1):
json = flowget.get("00:00:00:00:00:00:00:03")
# print json
#byte_count = 0
for i in json[u'flows']:
if u'ipv4_src' in i[u'match'] and u'ipv4_dst' in i[u'match']:
if i[u'match'][u'ipv4_src'] == "10.0.0.1" and i[u'match'][u'ipv4_dst'] == "10.0.0.3":
byte_count = byte_count + int(i[u'byteCount'])
print "in while", byte_count
if byte_count < 2000000:
print "q1", byte_count
S1Staticflow3 = {'switch':"00:00:00:00:00:00:00:01","name":"S1h1toh3","cookie":"0",
"priority":"2","in_port":"1","eth_type":"0x800","ipv4_src":"10.0.0.1",
"ipv4_dst":"10.0.0.3","active":"true","actions":"set_queue=1,output=3"}
#byte_count = byte_count - 20000000
pusher.set(S1Staticflow3)
elif byte_count > 2000000:
print "q2"
byte_count = byte_count - 2000000
print byte_count
S1Staticflow3 = {'switch':"00:00:00:00:00:00:00:01","name":"S1h1toh3","cookie":"0",
"priority":"2","in_port":"1","eth_type":"0x800","ipv4_src":"10.0.0.1",
"ipv4_dst":"10.0.0.3","active":"true","actions":"set_queue=2,output=3"}
pusher.set(S1Staticflow3)
# while byte_count < 1000000:
# pusher.set(S1Staticflow3)
# byte_count = byte_count + int(i[u'byteCount'])
# byte_count = byte_count - 1000000
def staticForwarding():
# Below 4 flows are for setting up the static forwarding for the path H1->S1->S2->H2 & vice-versa
# Define static flow for Switch S1 for packet forwarding b/w h1 and h2
S1Staticflow1 = {'switch':"00:00:00:00:00:00:00:01","name":"S1h1toh2","cookie":"0",
"priority":"1","in_port":"1","eth_type":"0x800","ipv4_src":"10.0.0.1",
"ipv4_dst":"10.0.0.2","active":"true","actions":"output=2"}
S1Staticflow2 = {'switch':"00:00:00:00:00:00:00:01","name":"S1h2toh1","cookie":"0",
"priority":"1","in_port":"2","eth_type":"0x800","ipv4_src":"10.0.0.2",
"ipv4_dst":"10.0.0.1","active":"true","actions":"output=1"}
# Define static flow for Switch S2 for packet forwarding b/w h1 and h2
S2Staticflow1 = {'switch':"00:00:00:00:00:00:00:02","name":"S2h2toh1","cookie":"0",
"priority":"1","in_port":"1","eth_type":"0x800","ipv4_src":"10.0.0.2",
"ipv4_dst":"10.0.0.1","active":"true","actions":"output=2"}
S2Staticflow2 = {'switch':"00:00:00:00:00:00:00:02","name":"S2h1toh2","cookie":"0",
"priority":"1","in_port":"2","eth_type":"0x800","ipv4_src":"10.0.0.1",
"ipv4_dst":"10.0.0.2","active":"true","actions":"output=1"}
# Below 4 flows are for setting up the static forwarding for the path H1->S1->S3->H3 & vice-versa
# Define static flow for Switch S1 for packet forwarding b/w h1 and h3
S1Staticflow3 = {'switch':"00:00:00:00:00:00:00:01","name":"S1h1toh3","cookie":"0",
"priority":"1","in_port":"1","eth_type":"0x800","ipv4_src":"10.0.0.1",
"ipv4_dst":"10.0.0.3","active":"true","actions":"output=3"}
S1Staticflow4 = {'switch':"00:00:00:00:00:00:00:01","name":"S1h3toh1","cookie":"0",
"priority":"1","in_port":"3","eth_type":"0x800","ipv4_src":"10.0.0.3",
"ipv4_dst":"10.0.0.1","active":"true","actions":"output=1"}
# Define static flow for Switch S3 for packet forwarding b/w h1 and h3
S3Staticflow1 = {'switch':"00:00:00:00:00:00:00:03","name":"S3h3toh1","cookie":"0",
"priority":"1","in_port":"1","eth_type":"0x800","ipv4_src":"10.0.0.3",
"ipv4_dst":"10.0.0.1","active":"true","actions":"output=2"}
S3Staticflow2 = {'switch':"00:00:00:00:00:00:00:03","name":"S3h1toh3","cookie":"0",
"priority":"1","in_port":"2","eth_type":"0x800","ipv4_src":"10.0.0.1",
"ipv4_dst":"10.0.0.3","active":"true","actions":"output=1"}
# Below 4 flows are for setting up the static forwarding for the path H2->S2->S3->H3 & vice-versa
# Define static flow for Switch S1 for packet forwarding b/w h2 and h3
S2Staticflow3 = {'switch':"00:00:00:00:00:00:00:02","name":"S2h2toh3","cookie":"0",
"priority":"1","in_port":"1","eth_type":"0x800","ipv4_src":"10.0.0.2",
"ipv4_dst":"10.0.0.3","active":"true","actions":"output=3"}
S2Staticflow4 = {'switch':"00:00:00:00:00:00:00:02","name":"S2h3toh2","cookie":"0",
"priority":"1","in_port":"3","eth_type":"0x800","ipv4_src":"10.0.0.3",
"ipv4_dst":"10.0.0.2","active":"true","actions":"output=1"}
# Define static flow for Switch S3 for packet forwarding b/w h2 and h3
S3Staticflow3 = {'switch':"00:00:00:00:00:00:00:03","name":"S3h3toh2","cookie":"0",
"priority":"1","in_port":"1","eth_type":"0x800","ipv4_src":"10.0.0.3",
"ipv4_dst":"10.0.0.2","active":"true","actions":"output=3"}
S3Staticflow4 = {'switch':"00:00:00:00:00:00:00:03","name":"S3h2toh3","cookie":"0",
"priority":"1","in_port":"2","eth_type":"0x800","ipv4_src":"10.0.0.2",
"ipv4_dst":"10.0.0.3","active":"true","actions":"output=1"}
#Now, Insert the flows to the switches
pusher.set(S1Staticflow1)
pusher.set(S1Staticflow2)
pusher.set(S1Staticflow3)
pusher.set(S1Staticflow4)
pusher.set(S2Staticflow1)
pusher.set(S2Staticflow2)
pusher.set(S2Staticflow3)
pusher.set(S2Staticflow4)
pusher.set(S3Staticflow1)
pusher.set(S3Staticflow2)
pusher.set(S3Staticflow3)
pusher.set(S3Staticflow4)
if __name__ =='__main__':
print "main"
staticForwarding()
print "after main"
#S1toS2()
#S2toS3()
S1toS3()
pass