Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | # -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*- |
| 3 | |
| 4 | import pprint |
| 5 | import os |
| 6 | import sys |
| 7 | import subprocess |
| 8 | import json |
| 9 | import argparse |
| 10 | import io |
| 11 | import time |
| 12 | |
| 13 | from flask import Flask, json, Response, render_template, make_response, request |
| 14 | |
| 15 | ## Global Var ## |
| 16 | ControllerIP="127.0.0.1" |
| 17 | ControllerPort=8080 |
| 18 | |
| 19 | DEBUG=0 |
| 20 | pp = pprint.PrettyPrinter(indent=4) |
| 21 | |
| 22 | app = Flask(__name__) |
| 23 | |
| 24 | ## Worker Functions ## |
| 25 | def log_error(txt): |
| 26 | print '%s' % (txt) |
| 27 | |
| 28 | def 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 Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 35 | |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 36 | def 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 | |
| 103 | def 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 Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 161 | def print_flow_path(parsedResult): |
Pavlin Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 162 | flowId = parsedResult['flowId']['value'] |
| 163 | installerId = parsedResult['installerId']['value'] |
Pavlin Radoslavov | d28cf7c | 2013-10-26 11:27:43 -0700 | [diff] [blame] | 164 | flowPathType = parsedResult['flowPathType'] |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 165 | flowPathFlags = parsedResult['flowPathFlags']['flags'] |
Pavlin Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 166 | srcSwitch = parsedResult['dataPath']['srcPort']['dpid']['value'] |
| 167 | srcPort = parsedResult['dataPath']['srcPort']['port']['value'] |
| 168 | dstSwitch = parsedResult['dataPath']['dstPort']['dpid']['value'] |
| 169 | dstPort = parsedResult['dataPath']['dstPort']['port']['value'] |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 170 | match = parsedResult['flowEntryMatch']; |
| 171 | actions = parsedResult['flowEntryActions']['actions'] |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 172 | |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 173 | flowPathFlagsStr = "" |
| 174 | if (flowPathFlags & 0x1): |
| 175 | if flowPathFlagsStr: |
| 176 | flowPathFlagsStr += "," |
| 177 | flowPathFlagsStr += "DISCARD_FIRST_HOP_ENTRY" |
| 178 | if (flowPathFlags & 0x2): |
| 179 | if flowPathFlagsStr: |
| 180 | flowPathFlagsStr += "," |
| 181 | flowPathFlagsStr += "KEEP_ONLY_FIRST_HOP_ENTRY" |
| 182 | |
Pavlin Radoslavov | d28cf7c | 2013-10-26 11:27:43 -0700 | [diff] [blame] | 183 | print "FlowPath: (flowId = %s installerId = %s flowPathType = %s flowPathFlags = 0x%x(%s) src = %s/%s dst = %s/%s)" % (flowId, installerId, flowPathType, flowPathFlags, flowPathFlagsStr, srcSwitch, srcPort, dstSwitch, dstPort) |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 184 | |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 185 | # |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 186 | # Print the common match conditions |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 187 | # |
| 188 | if match == None: |
| 189 | print " Match: %s" % (match) |
| 190 | else: |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 191 | parsedMatch = parse_match(match) |
| 192 | for l in parsedMatch: |
| 193 | print " %s" % l |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 194 | |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 195 | # |
| 196 | # Print the actions |
| 197 | # |
| 198 | parsedActions = parse_actions(actions) |
| 199 | for l in parsedActions: |
| 200 | print " %s" % l |
| 201 | |
| 202 | # |
| 203 | # Print each Flow Entry |
| 204 | # |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 205 | for f in parsedResult['dataPath']['flowEntries']: |
Pavlin Radoslavov | 8cdd1a2 | 2013-03-15 20:28:00 -0700 | [diff] [blame] | 206 | flowEntryId = f['flowEntryId'] |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 207 | dpid = f['dpid']['value'] |
| 208 | userState = f['flowEntryUserState'] |
| 209 | switchState = f['flowEntrySwitchState'] |
Pavlin Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 210 | match = f['flowEntryMatch']; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 211 | actions = f['flowEntryActions']['actions'] |
| 212 | |
Pavlin Radoslavov | 8cdd1a2 | 2013-03-15 20:28:00 -0700 | [diff] [blame] | 213 | print " FlowEntry: (%s, %s, %s, %s)" % (flowEntryId, dpid, userState, switchState) |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 214 | |
Pavlin Radoslavov | 2d59f58 | 2013-03-11 11:36:06 -0700 | [diff] [blame] | 215 | # |
| 216 | # Print the match conditions |
| 217 | # |
| 218 | if match == None: |
| 219 | print " Match: %s" % (match) |
| 220 | else: |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 221 | parsedMatch = parse_match(match) |
| 222 | for l in parsedMatch: |
| 223 | print " %s" % l |
Pavlin Radoslavov | 2d59f58 | 2013-03-11 11:36:06 -0700 | [diff] [blame] | 224 | # |
| 225 | # Print the actions |
| 226 | # |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 227 | parsedActions = parse_actions(actions) |
| 228 | for l in parsedActions: |
| 229 | print " %s" % l |
| 230 | |
Pavlin Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 231 | |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 232 | def get_flow_path(flow_id): |
| 233 | try: |
| 234 | command = "curl -s \"http://%s:%s/wm/flow/get/%s/json\"" % (ControllerIP, ControllerPort, flow_id) |
| 235 | debug("get_flow_path %s" % command) |
| 236 | |
| 237 | result = os.popen(command).read() |
| 238 | debug("result %s" % result) |
| 239 | if len(result) == 0: |
Pavlin Radoslavov | 8e0a00d | 2013-03-15 18:32:33 -0700 | [diff] [blame] | 240 | print "No Flow found" |
| 241 | return; |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 242 | |
| 243 | parsedResult = json.loads(result) |
| 244 | debug("parsed %s" % parsedResult) |
| 245 | except: |
| 246 | log_error("Controller IF has issue") |
| 247 | exit(1) |
| 248 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 249 | print_flow_path(parsedResult) |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 250 | |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 251 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 252 | def get_installer_flow_paths(installer_id, v1, p1, v2, p2): |
| 253 | try: |
| 254 | 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) |
| 255 | debug("get_installer_flow_paths %s" % command) |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 256 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 257 | result = os.popen(command).read() |
| 258 | debug("result %s" % result) |
| 259 | if len(result) == 0: |
| 260 | print "No Flows found" |
| 261 | return; |
| 262 | |
| 263 | parsedResult = json.loads(result) |
| 264 | debug("parsed %s" % parsedResult) |
| 265 | except: |
| 266 | log_error("Controller IF has issue") |
| 267 | exit(1) |
| 268 | |
| 269 | for flowPath in parsedResult: |
| 270 | print_flow_path(flowPath) |
| 271 | |
| 272 | |
| 273 | def get_endpoints_flow_paths(v1, p1, v2, p2): |
| 274 | try: |
| 275 | command = "curl -s \"http://%s:%s/wm/flow/getall-by-endpoints/%s/%s/%s/%s/json\"" % (ControllerIP, ControllerPort, v1, p1, v2, p2) |
| 276 | debug("get_endpoints_flow_paths %s" % command) |
| 277 | |
| 278 | result = os.popen(command).read() |
| 279 | debug("result %s" % result) |
| 280 | if len(result) == 0: |
| 281 | print "No Flows found" |
| 282 | return; |
| 283 | |
| 284 | parsedResult = json.loads(result) |
| 285 | debug("parsed %s" % parsedResult) |
| 286 | except: |
| 287 | log_error("Controller IF has issue") |
| 288 | exit(1) |
| 289 | |
| 290 | for flowPath in parsedResult: |
| 291 | print_flow_path(flowPath) |
| 292 | |
| 293 | |
| 294 | def get_all_flow_paths(): |
| 295 | try: |
| 296 | command = "curl -s \"http://%s:%s/wm/flow/getall/json\"" % (ControllerIP, ControllerPort) |
| 297 | debug("get_all_flow_paths %s" % command) |
| 298 | |
| 299 | result = os.popen(command).read() |
| 300 | debug("result %s" % result) |
| 301 | if len(result) == 0: |
| 302 | print "No Flows found" |
| 303 | return; |
| 304 | |
| 305 | parsedResult = json.loads(result) |
| 306 | debug("parsed %s" % parsedResult) |
| 307 | except: |
| 308 | log_error("Controller IF has issue") |
| 309 | exit(1) |
| 310 | |
| 311 | for flowPath in parsedResult: |
| 312 | print_flow_path(flowPath) |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 313 | |
| 314 | if __name__ == "__main__": |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 315 | usage_msg1 = "Usage:\n" |
| 316 | usage_msg2 = "%s <flow_id> : Print flow with Flow ID of <flow_id>\n" % (sys.argv[0]) |
| 317 | usage_msg3 = " all : Print all flows\n" |
| 318 | usage_msg4 = " installer <installer-id> <src-dpid> <src-port> <dest-dpid> <dest-port>\n" |
| 319 | usage_msg5 = " endpoints <src-dpid> <src-port> <dest-dpid> <dest-port>" |
| 320 | usage_msg = usage_msg1 + usage_msg2 + usage_msg3 + usage_msg4 + usage_msg5; |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 321 | |
| 322 | # app.debug = False; |
| 323 | |
| 324 | # Usage info |
| 325 | if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"): |
| 326 | print(usage_msg) |
| 327 | exit(0) |
| 328 | |
| 329 | # Check arguments |
| 330 | if len(sys.argv) < 2: |
| 331 | log_error(usage_msg) |
| 332 | exit(1) |
| 333 | |
| 334 | # Do the work |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 335 | if sys.argv[1] == "all": |
| 336 | get_all_flow_paths() |
| 337 | elif sys.argv[1] == "installer": |
| 338 | if len(sys.argv) < 7: |
| 339 | log_error(usage_msg) |
| 340 | exit(1) |
| 341 | get_installer_flow_paths(sys.argv[2], sys.argv[3], sys.argv[4], |
| 342 | sys.argv[5], sys.argv[6]) |
| 343 | elif sys.argv[1] == "endpoints": |
| 344 | if len(sys.argv) < 6: |
| 345 | log_error(usage_msg) |
| 346 | exit(1) |
| 347 | get_endpoints_flow_paths(sys.argv[2], sys.argv[3], sys.argv[4], |
| 348 | sys.argv[5]) |
| 349 | else: |
| 350 | get_flow_path(sys.argv[1]) |