blob: 9ab55dabfd5c808f00690411ab5c99afd686e7af [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
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070036def parse_match(match):
37 result = []
38
39 inPort = match['inPort']
40 matchInPort = match['matchInPort']
41 srcMac = match['srcMac']
42 matchSrcMac = match['matchSrcMac']
43 dstMac = match['dstMac']
44 matchDstMac = match['matchDstMac']
45 ethernetFrameType = match['ethernetFrameType']
46 matchEthernetFrameType = match['matchEthernetFrameType']
47 vlanId = match['vlanId']
48 matchVlanId = match['matchVlanId']
49 vlanPriority = match['vlanPriority']
50 matchVlanPriority = match['matchVlanPriority']
51 srcIPv4Net = match['srcIPv4Net']
52 matchSrcIPv4Net = match['matchSrcIPv4Net']
53 dstIPv4Net = match['dstIPv4Net']
54 matchDstIPv4Net = match['matchDstIPv4Net']
55 ipProto = match['ipProto']
56 matchIpProto = match['matchIpProto']
57 ipToS = match['ipToS']
58 matchIpToS = match['matchIpToS']
59 srcTcpUdpPort = match['srcTcpUdpPort']
60 matchSrcTcpUdpPort = match['matchSrcTcpUdpPort']
61 dstTcpUdpPort = match['dstTcpUdpPort']
62 matchDstTcpUdpPort = match['matchDstTcpUdpPort']
63 if matchInPort == True:
64 r = "inPort: %s" % inPort['value']
65 result.append(r)
66 if matchSrcMac == True:
67 r = "srcMac: %s" % srcMac['value']
68 result.append(r)
69 if matchDstMac == True:
70 r = "dstMac: %s" % dstMac['value']
71 result.append(r)
72 if matchEthernetFrameType == True:
73 r = "ethernetFrameType: %s" % hex(ethernetFrameType)
74 result.append(r)
75 if matchVlanId == True:
76 r = "vlanId: %s" % vlanId
77 result.append(r)
78 if matchVlanPriority == True:
79 r = "vlanPriority: %s" % vlanPriority
80 result.append(r)
81 if matchSrcIPv4Net == True:
82 r = "srcIPv4Net: %s" % srcIPv4Net['value']
83 result.append(r)
84 if matchDstIPv4Net == True:
85 r = "dstIPv4Net: %s" % dstIPv4Net['value']
86 result.append(r)
87 if matchIpProto == True:
88 r = "ipProto: %s" % ipProto
89 result.append(r)
90 if matchIpToS == True:
91 r = "ipToS: %s" % ipToS
92 result.append(r)
93 if matchSrcTcpUdpPort == True:
94 r = "srcTcpUdpPort: %s" % srcTcpUdpPort
95 result.append(r)
96 if matchDstTcpUdpPort == True:
97 r = "dstTcpUdpPort: %s" % dstTcpUdpPort
98 result.append(r)
99
100 return result
101
102
103def parse_actions(actions):
104 result = []
105 for a in actions:
106 actionType = a['actionType']
107 if actionType == "ACTION_OUTPUT":
108 port = a['actionOutput']['port']['value']
109 maxLen = a['actionOutput']['maxLen']
110 r = "actionType: %s port: %s maxLen: %s" % (actionType, port, maxLen)
111 result.append(r)
112 if actionType == "ACTION_SET_VLAN_VID":
113 vlanId = a['actionSetVlanId']['vlanId']
114 r = "actionType: %s vlanId: %s" % (actionType, vlanId)
115 result.append(r)
116 if actionType == "ACTION_SET_VLAN_PCP":
117 vlanPriority = a['actionSetVlanPriority']['vlanPriority']
118 r = "actionType: %s vlanPriority: %s" % (actionType, vlanPriority)
119 result.append(r)
120 if actionType == "ACTION_STRIP_VLAN":
121 stripVlan = a['actionStripVlan']['stripVlan']
122 r = "actionType: %s stripVlan: %s" % (actionType, stripVlan)
123 result.append(r)
124 if actionType == "ACTION_SET_DL_SRC":
125 setEthernetSrcAddr = a['actionSetEthernetSrcAddr']['addr']['value']
126 r = "actionType: %s setEthernetSrcAddr: %s" % (actionType, setEthernetSrcAddr)
127 result.append(r)
128 if actionType == "ACTION_SET_DL_DST":
129 setEthernetDstAddr = a['actionSetEthernetDstAddr']['addr']['value']
130 r = "actionType: %s setEthernetDstAddr: %s" % (actionType, setEthernetDstAddr)
131 result.append(r)
132 if actionType == "ACTION_SET_NW_SRC":
133 setIPv4SrcAddr = a['actionSetIPv4SrcAddr']['addr']['value']
134 r = "actionType: %s setIPv4SrcAddr: %s" % (actionType, setIPv4SrcAddr)
135 result.append(r)
136 if actionType == "ACTION_SET_NW_DST":
137 setIPv4DstAddr = a['actionSetIPv4DstAddr']['addr']['value']
138 r = "actionType: %s setIPv4DstAddr: %s" % (actionType, setIPv4DstAddr)
139 result.append(r)
140 if actionType == "ACTION_SET_NW_TOS":
141 setIpToS = a['actionSetIpToS']['ipToS']
142 r = "actionType: %s setIpToS: %s" % (actionType, setIpToS)
143 result.append(r)
144 if actionType == "ACTION_SET_TP_SRC":
145 setTcpUdpSrcPort = a['actionSetTcpUdpSrcPort']['port']
146 r = "actionType: %s setTcpUdpSrcPort: %s" % (actionType, setTcpUdpSrcPort)
147 result.append(r)
148 if actionType == "ACTION_SET_TP_DST":
149 setTcpUdpDstPort = a['actionSetTcpUdpDstPort']['port']
150 r = "actionType: %s setTcpUdpDstPort: %s" % (actionType, setTcpUdpDstPort)
151 result.append(r)
152 if actionType == "ACTION_ENQUEUE":
153 port = a['actionEnqueue']['port']['value']
154 queueId = a['actionEnqueue']['queueId']
155 r = "actionType: %s port: %s queueId: %s" % (actionType, port, queueId)
156 result.append(r)
157
158 return result
159
160
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800161def print_flow_path(parsedResult):
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800162 flowId = parsedResult['flowId']['value']
163 installerId = parsedResult['installerId']['value']
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700164 flowPathFlags = parsedResult['flowPathFlags']['flags']
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800165 srcSwitch = parsedResult['dataPath']['srcPort']['dpid']['value']
166 srcPort = parsedResult['dataPath']['srcPort']['port']['value']
167 dstSwitch = parsedResult['dataPath']['dstPort']['dpid']['value']
168 dstPort = parsedResult['dataPath']['dstPort']['port']['value']
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700169 match = parsedResult['flowEntryMatch'];
170 actions = parsedResult['flowEntryActions']['actions']
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800171
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700172 flowPathFlagsStr = ""
173 if (flowPathFlags & 0x1):
174 if flowPathFlagsStr:
175 flowPathFlagsStr += ","
176 flowPathFlagsStr += "DISCARD_FIRST_HOP_ENTRY"
177 if (flowPathFlags & 0x2):
178 if flowPathFlagsStr:
179 flowPathFlagsStr += ","
180 flowPathFlagsStr += "KEEP_ONLY_FIRST_HOP_ENTRY"
181
182 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 Radoslavov1bc2c472013-07-17 18:11:37 -0700183
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700184 #
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700185 # Print the common match conditions
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700186 #
187 if match == None:
188 print " Match: %s" % (match)
189 else:
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700190 parsedMatch = parse_match(match)
191 for l in parsedMatch:
192 print " %s" % l
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800193
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700194 #
195 # Print the actions
196 #
197 parsedActions = parse_actions(actions)
198 for l in parsedActions:
199 print " %s" % l
200
201 #
202 # Print each Flow Entry
203 #
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800204 for f in parsedResult['dataPath']['flowEntries']:
Pavlin Radoslavov8cdd1a22013-03-15 20:28:00 -0700205 flowEntryId = f['flowEntryId']
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800206 dpid = f['dpid']['value']
207 userState = f['flowEntryUserState']
208 switchState = f['flowEntrySwitchState']
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800209 match = f['flowEntryMatch'];
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700210 actions = f['flowEntryActions']['actions']
211
Pavlin Radoslavov8cdd1a22013-03-15 20:28:00 -0700212 print " FlowEntry: (%s, %s, %s, %s)" % (flowEntryId, dpid, userState, switchState)
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800213
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -0700214 #
215 # Print the match conditions
216 #
217 if match == None:
218 print " Match: %s" % (match)
219 else:
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700220 parsedMatch = parse_match(match)
221 for l in parsedMatch:
222 print " %s" % l
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -0700223 #
224 # Print the actions
225 #
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700226 parsedActions = parse_actions(actions)
227 for l in parsedActions:
228 print " %s" % l
229
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800230
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800231def get_flow_path(flow_id):
232 try:
233 command = "curl -s \"http://%s:%s/wm/flow/get/%s/json\"" % (ControllerIP, ControllerPort, flow_id)
234 debug("get_flow_path %s" % command)
235
236 result = os.popen(command).read()
237 debug("result %s" % result)
238 if len(result) == 0:
Pavlin Radoslavov8e0a00d2013-03-15 18:32:33 -0700239 print "No Flow found"
240 return;
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800241
242 parsedResult = json.loads(result)
243 debug("parsed %s" % parsedResult)
244 except:
245 log_error("Controller IF has issue")
246 exit(1)
247
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800248 print_flow_path(parsedResult)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800249
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800250
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800251def get_installer_flow_paths(installer_id, v1, p1, v2, p2):
252 try:
253 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)
254 debug("get_installer_flow_paths %s" % command)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800255
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800256 result = os.popen(command).read()
257 debug("result %s" % result)
258 if len(result) == 0:
259 print "No Flows found"
260 return;
261
262 parsedResult = json.loads(result)
263 debug("parsed %s" % parsedResult)
264 except:
265 log_error("Controller IF has issue")
266 exit(1)
267
268 for flowPath in parsedResult:
269 print_flow_path(flowPath)
270
271
272def get_endpoints_flow_paths(v1, p1, v2, p2):
273 try:
274 command = "curl -s \"http://%s:%s/wm/flow/getall-by-endpoints/%s/%s/%s/%s/json\"" % (ControllerIP, ControllerPort, v1, p1, v2, p2)
275 debug("get_endpoints_flow_paths %s" % command)
276
277 result = os.popen(command).read()
278 debug("result %s" % result)
279 if len(result) == 0:
280 print "No Flows found"
281 return;
282
283 parsedResult = json.loads(result)
284 debug("parsed %s" % parsedResult)
285 except:
286 log_error("Controller IF has issue")
287 exit(1)
288
289 for flowPath in parsedResult:
290 print_flow_path(flowPath)
291
292
293def get_all_flow_paths():
294 try:
295 command = "curl -s \"http://%s:%s/wm/flow/getall/json\"" % (ControllerIP, ControllerPort)
296 debug("get_all_flow_paths %s" % command)
297
298 result = os.popen(command).read()
299 debug("result %s" % result)
300 if len(result) == 0:
301 print "No Flows found"
302 return;
303
304 parsedResult = json.loads(result)
305 debug("parsed %s" % parsedResult)
306 except:
307 log_error("Controller IF has issue")
308 exit(1)
309
310 for flowPath in parsedResult:
311 print_flow_path(flowPath)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800312
313if __name__ == "__main__":
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800314 usage_msg1 = "Usage:\n"
315 usage_msg2 = "%s <flow_id> : Print flow with Flow ID of <flow_id>\n" % (sys.argv[0])
316 usage_msg3 = " all : Print all flows\n"
317 usage_msg4 = " installer <installer-id> <src-dpid> <src-port> <dest-dpid> <dest-port>\n"
318 usage_msg5 = " endpoints <src-dpid> <src-port> <dest-dpid> <dest-port>"
319 usage_msg = usage_msg1 + usage_msg2 + usage_msg3 + usage_msg4 + usage_msg5;
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800320
321 # app.debug = False;
322
323 # Usage info
324 if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
325 print(usage_msg)
326 exit(0)
327
328 # Check arguments
329 if len(sys.argv) < 2:
330 log_error(usage_msg)
331 exit(1)
332
333 # Do the work
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800334 if sys.argv[1] == "all":
335 get_all_flow_paths()
336 elif sys.argv[1] == "installer":
337 if len(sys.argv) < 7:
338 log_error(usage_msg)
339 exit(1)
340 get_installer_flow_paths(sys.argv[2], sys.argv[3], sys.argv[4],
341 sys.argv[5], sys.argv[6])
342 elif sys.argv[1] == "endpoints":
343 if len(sys.argv) < 6:
344 log_error(usage_msg)
345 exit(1)
346 get_endpoints_flow_paths(sys.argv[2], sys.argv[3], sys.argv[4],
347 sys.argv[5])
348 else:
349 get_flow_path(sys.argv[1])