-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect_state.py
executable file
·264 lines (240 loc) · 10.5 KB
/
collect_state.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
#!/usr/bin/python
# Copyright 2019 Richard Sanger, Wand Network Research Group
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ryu.base import app_manager
from ryu.ofproto import ofproto_v1_3
from ryu.controller import dpset
from ryu.controller.handler import set_ev_cls
from ryu.app.ofctl import api
try:
import cPickle as pickle
except ImportError:
import pickle
class MatchTTP(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
_CONTEXTS = {'dpset': dpset.DPSet}
done = False
def __init__(self, *args, **kwargs):
super(MatchTTP, self).__init__(*args, **kwargs)
self.collection = {}
self.dpset = kwargs['dpset']
@set_ev_cls(dpset.EventDP, dpset.DPSET_EV_DISPATCHER)
def handle_datapath(self, ev):
if not ev.enter or self.done:
print('Skipping')
return
self.done = True
dp = ev.dp
parser = dp.ofproto_parser
print('Switch connected')
print('Collecting')
# Request switch features XXX TODO FAILING
try:
msg = parser.OFPFeaturesRequest(dp)
res = api.send_msg(self, msg, reply_cls=parser.OFPSwitchFeatures)
self.collection['switch_features'] = res.to_json() if res else res
print('Switch Features')
except Exception as e:
print("Exception collecting Switch Features")
print(e)
# Request config - non multipart
try:
msg = parser.OFPGetConfigRequest(dp)
res = api.send_msg(self, msg, reply_cls=parser.OFPGetConfigReply)
self.collection['config'] = ({"flags": res.flags,
"miss_send_len": res.miss_send_len}
if res else res)
print('Switch Config')
except Exception as e:
print("Exception collecting Switch Config")
print(e)
# Request switch description - multipart single item
try:
msg = parser.OFPDescStatsRequest(dp)
res = api.send_msg(self, msg, reply_cls=parser.OFPDescStatsReply,
reply_multi=True)
if len(res) != 1:
raise Exception(('Expecting only a single response from'
' switch description', res))
self.collection['desc'] = res[0].body
print('Switch Description')
except Exception as e:
print("Exception collecting Switch Descripton")
print(e)
# Request all stats from all tables - multipart list
try:
msg = parser.OFPFlowStatsRequest(dp)
res = api.send_msg(self, msg, reply_cls=parser.OFPFlowStatsReply,
reply_multi=True)
l = [stat for stats in res for stat in stats.body]
self.collection['flow_stats'] = l
print('Flows Stats')
except Exception as e:
print("Exception collecting Flow Stats")
print(e)
# Request table stats - multipart list
try:
msg = parser.OFPTableStatsRequest(dp)
res = api.send_msg(self, msg, reply_cls=parser.OFPTableStatsReply,
reply_multi=True)
l = [table for tables in res for table in tables.body]
self.collection['table_stats'] = l
print('Table Stats')
except Exception as e:
print("Exception collecting Table Stats")
print(e)
# Request port stats - multipart list
try:
msg = parser.OFPPortStatsRequest(dp)
res = api.send_msg(self, msg, reply_cls=parser.OFPPortStatsReply,
reply_multi=True)
l = [port for ports in res for port in ports.body]
self.collection['port_stats'] = l
print('Port Stats')
except Exception as e:
print("Exception collecting Port Stats")
print(e)
# Request port descriptions - multipart list
try:
msg = parser.OFPPortDescStatsRequest(dp)
res = api.send_msg(self, msg,
reply_cls=parser.OFPPortDescStatsReply,
reply_multi=True)
l = [port for ports in res for port in ports.body]
self.collection['port_desc'] = l
print('Port Description')
except Exception as e:
print("Exception collecting Port Description")
print(e)
# Request queue stats - mulitpart list
try:
msg = parser.OFPQueueStatsRequest(dp)
res = api.send_msg(self, msg, reply_cls=parser.OFPQueueStatsReply,
reply_multi=True)
l = [queue for queues in res for queue in queues.body]
self.collection['queue_stats'] = l
print('Queue Stats')
except Exception as e:
print("Exception collecting Queue Stats")
print(e)
# Request group stats - multipart list
try:
msg = parser.OFPGroupStatsRequest(dp)
res = api.send_msg(self, msg, reply_cls=parser.OFPGroupStatsReply,
reply_multi=True)
l = [group for groups in res for group in groups.body]
self.collection['group_stats'] = l
print('Group Stats')
except Exception as e:
print("Exception collecting Group Stats")
print(e)
# Request group desc - multipart list
try:
msg = parser.OFPGroupDescStatsRequest(dp)
res = api.send_msg(self, msg,
reply_cls=parser.OFPGroupDescStatsReply,
reply_multi=True)
l = [group for groups in res for group in groups.body]
self.collection['group_desc'] = l
print('Group Description')
except Exception as e:
print("Exception collecting Group Description")
print(e)
# Request group features - multipart single item
try:
msg = parser.OFPGroupFeaturesStatsRequest(dp)
res = api.send_msg(self, msg,
reply_cls=parser.OFPGroupFeaturesStatsReply,
reply_multi=True)
if len(res) != 1:
raise Exception(('Expecting only a single response from'
' group features', res))
self.collection['group_features'] = res[0].body
print('Group Features')
except Exception as e:
print("Exception collecting Group Features")
print(e)
# Request meter stats - multipart list
try:
msg = parser.OFPMeterStatsRequest(dp)
res = api.send_msg(self, msg, reply_cls=parser.OFPMeterStatsReply,
reply_multi=True)
l = [meter for meters in res for meter in meters.body]
self.collection['meter_stats'] = l
print('Meter Stats')
except Exception as e:
print("Exception collecting Meter Stats")
print(e)
# Request meter config - multipart list
try:
msg = parser.OFPMeterConfigStatsRequest(dp)
res = api.send_msg(self, msg,
reply_cls=parser.OFPMeterConfigStatsReply,
reply_multi=True)
l = [meter for meters in res for meter in meters.body]
self.collection['meter_config'] = l
print('Meter Config')
except Exception as e:
print("Exception collecting Meter Config")
print(e)
# Request meter features - single item
try:
msg = parser.OFPMeterFeaturesStatsRequest(dp)
res = api.send_msg(self, msg,
reply_cls=parser.OFPMeterFeaturesStatsReply,
reply_multi=True)
if len(res) != 1:
raise Exception(('Expecting only a single response from'
' meter features', res))
self.collection['meter_features'] = res[0].body
print('Meter Features')
except Exception as e:
print("Exception collecting Meter Features")
print(e)
# Request table features, this can be very large
try:
msg = parser.OFPTableFeaturesStatsRequest(dp)
res = api.send_msg(self, msg,
reply_cls=parser.OFPTableFeaturesStatsReply,
reply_multi=True)
l = [table for tables in res for table in tables.body]
self.collection['table_features'] = l
print('Table Features')
except Exception as e:
print("Exception collecting Table Features")
print(e)
# Request queue config (this is done on a per port basis) so
# querying all does not work so well.
# Not multipart!?
try:
msg = parser.OFPQueueGetConfigRequest(dp, ofproto_v1_3.OFPP_ANY)
res = api.send_msg(self, msg,
reply_cls=parser.OFPQueueGetConfigReply)
self.collection['queue_config'] = res.queues if res else res
print('Queue Config')
except Exception as e:
print("Exception collecting Queue Config")
print(e)
pickle.dump(self.collection, open("switch_state.pickle", "wb"))
print('Switch dumped successfully')
if __name__ == '__main__':
print("This is a ryu app that passively collects all stats, features"
" and configurations to capture the current state of a running"
" OpenFlow Switch v1.3. This includes all flows, groups, queues,"
" meters, ports.\n")
print("The output is written to switch_state.pickle as pickled a"
" dictionary of ryu objects")
print("Run as:")
print ("ryu-mananger --ofp-tcp-listen-port <port> --ofp-listen-host <host>"
" --log-config-file <./log.conf> ./CollectState.py")