blob: 659936010e18785febb474d00053f631d0f0d74d [file] [log] [blame]
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -08001#! /usr/bin/env python
2# -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*-
3
4import pprint
5import os
6import sys
7import subprocess
8import json
9import argparse
10import io
11import time
12
13from flask import Flask, json, Response, render_template, make_response, request
14
15## Global Var ##
16ControllerIP="127.0.0.1"
17ControllerPort=8080
18
19DEBUG=0
20pp = pprint.PrettyPrinter(indent=4)
21
22app = Flask(__name__)
23
24## Worker Functions ##
25def log_error(txt):
26 print '%s' % (txt)
27
28def debug(txt):
29 if DEBUG:
30 print '%s' % (txt)
31
32# @app.route("/wm/flow/get/<flow-id>/json")
33# Sample output:
34# {"flowId":{"value":"0x5"},"installerId":{"value":"FOOBAR"},"dataPath":{"srcPort":{"dpid":{"value":"00:00:00:00:00:00:00:01"},"port":{"value":0}},"dstPort":{"dpid":{"value":"00:00:00:00:00:00:00:02"},"port":{"value":0}},"flowEntries":[{"flowEntryId":"0x1389","flowEntryMatch":null,"flowEntryActions":null,"dpid":{"value":"00:00:00:00:00:00:00:01"},"inPort":{"value":0},"outPort":{"value":1},"flowEntryUserState":"FE_USER_DELETE","flowEntrySwitchState":"FE_SWITCH_NOT_UPDATED","flowEntryErrorState":null},{"flowEntryId":"0x138a","flowEntryMatch":null,"flowEntryActions":null,"dpid":{"value":"00:00:00:00:00:00:00:02"},"inPort":{"value":9},"outPort":{"value":0},"flowEntryUserState":"FE_USER_DELETE","flowEntrySwitchState":"FE_SWITCH_NOT_UPDATED","flowEntryErrorState":null}]}}
Pavlin Radoslavov706df052013-03-06 10:49:07 -080035
36def print_flow_path(parsedResult):
Pavlin Radoslavovede97582013-03-08 18:57:28 -080037 flowId = parsedResult['flowId']['value']
38 installerId = parsedResult['installerId']['value']
Pavlin Radoslavov204b2862013-07-12 14:15:36 -070039 flowPathFlags = parsedResult['flowPathFlags']['flags']
Pavlin Radoslavovede97582013-03-08 18:57:28 -080040 srcSwitch = parsedResult['dataPath']['srcPort']['dpid']['value']
41 srcPort = parsedResult['dataPath']['srcPort']['port']['value']
42 dstSwitch = parsedResult['dataPath']['dstPort']['dpid']['value']
43 dstPort = parsedResult['dataPath']['dstPort']['port']['value']
Pavlin Radoslavov706df052013-03-06 10:49:07 -080044
Pavlin Radoslavov204b2862013-07-12 14:15:36 -070045 flowPathFlagsStr = ""
46 if (flowPathFlags & 0x1):
47 if flowPathFlagsStr:
48 flowPathFlagsStr += ","
49 flowPathFlagsStr += "DISCARD_FIRST_HOP_ENTRY"
50 if (flowPathFlags & 0x2):
51 if flowPathFlagsStr:
52 flowPathFlagsStr += ","
53 flowPathFlagsStr += "KEEP_ONLY_FIRST_HOP_ENTRY"
54
55 print "FlowPath: (flowId = %s installerId = %s flowPathFlags = 0x%x(%s) src = %s/%s dst = %s/%s)" % (flowId, installerId, flowPathFlags, flowPathFlagsStr, srcSwitch, srcPort, dstSwitch, dstPort)
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -070056 match = parsedResult['flowEntryMatch'];
57 #
58 # Print the common conditions
59 #
60 if match == None:
61 print " Match: %s" % (match)
62 else:
63 # inPort = match['inPort']
64 # matchInPort = match['matchInPort']
65 srcMac = match['srcMac']
66 matchSrcMac = match['matchSrcMac']
67 dstMac = match['dstMac']
68 matchDstMac = match['matchDstMac']
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -070069 ethernetFrameType = match['ethernetFrameType']
70 matchEthernetFrameType = match['matchEthernetFrameType']
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -070071 vlanId = match['vlanId']
72 matchVlanId = match['matchVlanId']
73 vlanPriority = match['vlanPriority']
74 matchVlanPriority = match['matchVlanPriority']
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -070075 srcIPv4Net = match['srcIPv4Net']
76 matchSrcIPv4Net = match['matchSrcIPv4Net']
77 dstIPv4Net = match['dstIPv4Net']
78 matchDstIPv4Net = match['matchDstIPv4Net']
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -070079 ipProto = match['ipProto']
80 matchIpProto = match['matchIpProto']
81 ipToS = match['ipToS']
82 matchIpToS = match['matchIpToS']
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -070083 srcTcpUdpPort = match['srcTcpUdpPort']
84 matchSrcTcpUdpPort = match['matchSrcTcpUdpPort']
85 dstTcpUdpPort = match['dstTcpUdpPort']
86 matchDstTcpUdpPort = match['matchDstTcpUdpPort']
87 # if matchInPort == True:
88 # print " inPort: %s" % inPort['value']
89 if matchSrcMac == True:
90 print " srcMac: %s" % srcMac['value']
91 if matchDstMac == True:
92 print " dstMac: %s" % dstMac['value']
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -070093 if matchEthernetFrameType == True:
94 print " ethernetFrameType: %s" % hex(ethernetFrameType)
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -070095 if matchVlanId == True:
96 print " vlanId: %s" % vlanId
97 if matchVlanPriority == True:
98 print " vlanPriority: %s" % vlanPriority
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -070099 if matchSrcIPv4Net == True:
100 print " srcIPv4Net: %s" % srcIPv4Net['value']
101 if matchDstIPv4Net == True:
102 print " dstIPv4Net: %s" % dstIPv4Net['value']
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700103 if matchIpProto == True:
104 print " ipProto: %s" % ipProto
105 if matchIpToS == True:
106 print " ipToS: %s" % ipToS
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700107 if matchSrcTcpUdpPort == True:
108 print " srcTcpUdpPort: %s" % srcTcpUdpPort
109 if matchDstTcpUdpPort == True:
110 print " dstTcpUdpPort: %s" % dstTcpUdpPort
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800111
112 for f in parsedResult['dataPath']['flowEntries']:
Pavlin Radoslavov8cdd1a22013-03-15 20:28:00 -0700113 flowEntryId = f['flowEntryId']
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800114 dpid = f['dpid']['value']
115 userState = f['flowEntryUserState']
116 switchState = f['flowEntrySwitchState']
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800117 match = f['flowEntryMatch'];
118 actions = f['flowEntryActions']
Pavlin Radoslavov8cdd1a22013-03-15 20:28:00 -0700119 print " FlowEntry: (%s, %s, %s, %s)" % (flowEntryId, dpid, userState, switchState)
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800120
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -0700121 #
122 # Print the match conditions
123 #
124 if match == None:
125 print " Match: %s" % (match)
126 else:
127 inPort = match['inPort']
128 matchInPort = match['matchInPort']
129 srcMac = match['srcMac']
130 matchSrcMac = match['matchSrcMac']
131 dstMac = match['dstMac']
132 matchDstMac = match['matchDstMac']
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700133 ethernetFrameType = match['ethernetFrameType']
134 matchEthernetFrameType = match['matchEthernetFrameType']
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -0700135 vlanId = match['vlanId']
136 matchVlanId = match['matchVlanId']
137 vlanPriority = match['vlanPriority']
138 matchVlanPriority = match['matchVlanPriority']
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -0700139 srcIPv4Net = match['srcIPv4Net']
140 matchSrcIPv4Net = match['matchSrcIPv4Net']
141 dstIPv4Net = match['dstIPv4Net']
142 matchDstIPv4Net = match['matchDstIPv4Net']
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700143 ipProto = match['ipProto']
144 matchIpProto = match['matchIpProto']
145 ipToS = match['ipToS']
146 matchIpToS = match['matchIpToS']
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -0700147 srcTcpUdpPort = match['srcTcpUdpPort']
148 matchSrcTcpUdpPort = match['matchSrcTcpUdpPort']
149 dstTcpUdpPort = match['dstTcpUdpPort']
150 matchDstTcpUdpPort = match['matchDstTcpUdpPort']
151 if matchInPort == True:
152 print " inPort: %s" % inPort['value']
153 if matchSrcMac == True:
154 print " srcMac: %s" % srcMac['value']
155 if matchDstMac == True:
156 print " dstMac: %s" % dstMac['value']
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700157 if matchEthernetFrameType == True:
158 print " ethernetFrameType: %s" % hex(ethernetFrameType)
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -0700159 if matchVlanId == True:
160 print " vlanId: %s" % vlanId
161 if matchVlanPriority == True:
162 print " vlanPriority: %s" % vlanPriority
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -0700163 if matchSrcIPv4Net == True:
164 print " srcIPv4Net: %s" % srcIPv4Net['value']
165 if matchDstIPv4Net == True:
166 print " dstIPv4Net: %s" % dstIPv4Net['value']
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700167 if matchIpProto == True:
168 print " ipProto: %s" % ipProto
169 if matchIpToS == True:
170 print " ipToS: %s" % ipToS
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -0700171 if matchSrcTcpUdpPort == True:
172 print " srcTcpUdpPort: %s" % srcTcpUdpPort
173 if matchDstTcpUdpPort == True:
174 print " dstTcpUdpPort: %s" % dstTcpUdpPort
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800175
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -0700176 #
177 # Print the actions
178 #
179 if actions == None:
180 print " Actions: %s" % (actions)
181 else:
182 for a in actions:
183 actionType = a['actionType']
184 if actionType == "ACTION_OUTPUT":
Pavlin Radoslavov82200bc2013-03-12 13:49:20 -0700185 port = a['actionOutput']['port']['value']
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -0700186 maxLen = a['actionOutput']['maxLen']
187 print " actionType: %s port: %s maxLen: %s" % (actionType, port, maxLen)
188 if actionType == "ACTION_SET_VLAN_VID":
189 vlanId = a['actionSetVlanId']['vlanId']
190 print " actionType: %s vlanId: %s" % (actionType, vlanId)
191 if actionType == "ACTION_SET_VLAN_PCP":
192 vlanPriority = a['actionSetVlanPriority']['vlanPriority']
193 print " actionType: %s vlanPriority: %s" % (actionType, vlanPriority)
194 if actionType == "ACTION_STRIP_VLAN":
195 stripVlan = a['actionStripVlan']['stripVlan']
196 print " actionType: %s stripVlan: %s" % (actionType, stripVlan)
197 if actionType == "ACTION_SET_DL_SRC":
198 setEthernetSrcAddr = a['actionSetEthernetSrcAddr']['addr']['value']
199 print " actionType: %s setEthernetSrcAddr: %s" % (actionType, setEthernetSrcAddr)
200 if actionType == "ACTION_SET_DL_DST":
201 setEthernetDstAddr = a['actionSetEthernetDstAddr']['addr']['value']
202 print " actionType: %s setEthernetDstAddr: %s" % (actionType, setEthernetDstAddr)
203 if actionType == "ACTION_SET_NW_SRC":
204 setIPv4SrcAddr = a['actionSetIPv4SrcAddr']['addr']['value']
205 print " actionType: %s setIPv4SrcAddr: %s" % (actionType, setIPv4SrcAddr)
206 if actionType == "ACTION_SET_NW_DST":
207 setIPv4DstAddr = a['actionSetIPv4DstAddr']['addr']['value']
208 print " actionType: %s setIPv4DstAddr: %s" % (actionType, setIPv4DstAddr)
209 if actionType == "ACTION_SET_NW_TOS":
210 setIpToS = a['actionSetIpToS']['ipToS']
211 print " actionType: %s setIpToS: %s" % (actionType, setIpToS)
212 if actionType == "ACTION_SET_TP_SRC":
213 setTcpUdpSrcPort = a['actionSetTcpUdpSrcPort']['port']
214 print " actionType: %s setTcpUdpSrcPort: %s" % (actionType, setTcpUdpSrcPort)
215 if actionType == "ACTION_SET_TP_DST":
216 setTcpUdpDstPort = a['actionSetTcpUdpDstPort']['port']
217 print " actionType: %s setTcpUdpDstPort: %s" % (actionType, setTcpUdpDstPort)
218 if actionType == "ACTION_ENQUEUE":
219 port = a['actionEnqueue']['port']['value']
220 queueId = a['actionEnqueue']['queueId']
221 print " actionType: %s port: %s queueId: %s" % (actionType, port, queueId)
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800222
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800223def get_flow_path(flow_id):
224 try:
225 command = "curl -s \"http://%s:%s/wm/flow/get/%s/json\"" % (ControllerIP, ControllerPort, flow_id)
226 debug("get_flow_path %s" % command)
227
228 result = os.popen(command).read()
229 debug("result %s" % result)
230 if len(result) == 0:
Pavlin Radoslavov8e0a00d2013-03-15 18:32:33 -0700231 print "No Flow found"
232 return;
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800233
234 parsedResult = json.loads(result)
235 debug("parsed %s" % parsedResult)
236 except:
237 log_error("Controller IF has issue")
238 exit(1)
239
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800240 print_flow_path(parsedResult)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800241
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800242
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800243def get_installer_flow_paths(installer_id, v1, p1, v2, p2):
244 try:
245 command = "curl -s \"http://%s:%s/wm/flow/getall-by-installer-id/%s/%s/%s/%s/%s/json\"" % (ControllerIP, ControllerPort, installer_id, v1, p1, v2, p2)
246 debug("get_installer_flow_paths %s" % command)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800247
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800248 result = os.popen(command).read()
249 debug("result %s" % result)
250 if len(result) == 0:
251 print "No Flows found"
252 return;
253
254 parsedResult = json.loads(result)
255 debug("parsed %s" % parsedResult)
256 except:
257 log_error("Controller IF has issue")
258 exit(1)
259
260 for flowPath in parsedResult:
261 print_flow_path(flowPath)
262
263
264def get_endpoints_flow_paths(v1, p1, v2, p2):
265 try:
266 command = "curl -s \"http://%s:%s/wm/flow/getall-by-endpoints/%s/%s/%s/%s/json\"" % (ControllerIP, ControllerPort, v1, p1, v2, p2)
267 debug("get_endpoints_flow_paths %s" % command)
268
269 result = os.popen(command).read()
270 debug("result %s" % result)
271 if len(result) == 0:
272 print "No Flows found"
273 return;
274
275 parsedResult = json.loads(result)
276 debug("parsed %s" % parsedResult)
277 except:
278 log_error("Controller IF has issue")
279 exit(1)
280
281 for flowPath in parsedResult:
282 print_flow_path(flowPath)
283
284
285def get_all_flow_paths():
286 try:
287 command = "curl -s \"http://%s:%s/wm/flow/getall/json\"" % (ControllerIP, ControllerPort)
288 debug("get_all_flow_paths %s" % command)
289
290 result = os.popen(command).read()
291 debug("result %s" % result)
292 if len(result) == 0:
293 print "No Flows found"
294 return;
295
296 parsedResult = json.loads(result)
297 debug("parsed %s" % parsedResult)
298 except:
299 log_error("Controller IF has issue")
300 exit(1)
301
302 for flowPath in parsedResult:
303 print_flow_path(flowPath)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800304
305if __name__ == "__main__":
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800306 usage_msg1 = "Usage:\n"
307 usage_msg2 = "%s <flow_id> : Print flow with Flow ID of <flow_id>\n" % (sys.argv[0])
308 usage_msg3 = " all : Print all flows\n"
309 usage_msg4 = " installer <installer-id> <src-dpid> <src-port> <dest-dpid> <dest-port>\n"
310 usage_msg5 = " endpoints <src-dpid> <src-port> <dest-dpid> <dest-port>"
311 usage_msg = usage_msg1 + usage_msg2 + usage_msg3 + usage_msg4 + usage_msg5;
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800312
313 # app.debug = False;
314
315 # Usage info
316 if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
317 print(usage_msg)
318 exit(0)
319
320 # Check arguments
321 if len(sys.argv) < 2:
322 log_error(usage_msg)
323 exit(1)
324
325 # Do the work
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800326 if sys.argv[1] == "all":
327 get_all_flow_paths()
328 elif sys.argv[1] == "installer":
329 if len(sys.argv) < 7:
330 log_error(usage_msg)
331 exit(1)
332 get_installer_flow_paths(sys.argv[2], sys.argv[3], sys.argv[4],
333 sys.argv[5], sys.argv[6])
334 elif sys.argv[1] == "endpoints":
335 if len(sys.argv) < 6:
336 log_error(usage_msg)
337 exit(1)
338 get_endpoints_flow_paths(sys.argv[2], sys.argv[3], sys.argv[4],
339 sys.argv[5])
340 else:
341 get_flow_path(sys.argv[1])