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 | |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 4 | import copy |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 5 | import pprint |
| 6 | import os |
| 7 | import sys |
| 8 | import subprocess |
| 9 | import json |
| 10 | import argparse |
| 11 | import io |
| 12 | import time |
| 13 | |
| 14 | from flask import Flask, json, Response, render_template, make_response, request |
| 15 | |
| 16 | # |
| 17 | # curl http://127.0.0.1:8080/wm/topology/route/00:00:00:00:00:00:0a:01/1/00:00:00:00:00:00:0a:04/1/json |
| 18 | # |
| 19 | |
| 20 | ## Global Var ## |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 21 | ControllerIP = "127.0.0.1" |
| 22 | ControllerPort = 8080 |
| 23 | MonitoringEnabled = False |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 24 | |
| 25 | DEBUG=0 |
| 26 | pp = pprint.PrettyPrinter(indent=4) |
| 27 | |
| 28 | app = Flask(__name__) |
| 29 | |
| 30 | ## Worker Functions ## |
| 31 | def log_error(txt): |
| 32 | print '%s' % (txt) |
| 33 | |
| 34 | def debug(txt): |
| 35 | if DEBUG: |
| 36 | print '%s' % (txt) |
| 37 | |
| 38 | # @app.route("/wm/topology/route/<srcdpid>/<srcport>/<destdpid>/<destport>/json") |
| 39 | # |
| 40 | # Sample output: |
| 41 | # {'dstPort': {'port': {'value': 0}, 'dpid': {'value': '00:00:00:00:00:00:00:02'}}, 'srcPort': {'port': {'value': 0}, 'dpid': {'value': '00:00:00:00:00:00:00:01'}}, 'flowEntries': [{'outPort': {'value': 1}, 'flowEntryErrorState': None, 'flowEntryMatch': None, 'flowEntryActions': None, 'inPort': {'value': 0}, 'flowEntryId': None, 'flowEntryUserState': 'FE_USER_UNKNOWN', 'dpid': {'value': '00:00:00:00:00:00:00:01'}, 'flowEntrySwitchState': 'FE_SWITCH_UNKNOWN'}, {'outPort': {'value': 0}, 'flowEntryErrorState': None, 'flowEntryMatch': None, 'flowEntryActions': None, 'inPort': {'value': 9}, 'flowEntryId': None, 'flowEntryUserState': 'FE_USER_UNKNOWN', 'dpid': {'value': '00:00:00:00:00:00:00:02'}, 'flowEntrySwitchState': 'FE_SWITCH_UNKNOWN'}]} |
| 42 | # |
| 43 | def shortest_path(v1, p1, v2, p2): |
| 44 | try: |
| 45 | command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (ControllerIP, ControllerPort, v1, p1, v2, p2) |
| 46 | debug("shortest_path %s" % command) |
Pavlin Radoslavov | eb2d5a2 | 2013-03-14 19:58:14 -0700 | [diff] [blame] | 47 | parsedResult = [] |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 48 | |
| 49 | result = os.popen(command).read() |
| 50 | debug("result %s" % result) |
| 51 | if len(result) == 0: |
| 52 | log_error("No Path found") |
Pavlin Radoslavov | eb2d5a2 | 2013-03-14 19:58:14 -0700 | [diff] [blame] | 53 | return parsedResult |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 54 | |
| 55 | parsedResult = json.loads(result) |
| 56 | debug("parsed %s" % parsedResult) |
| 57 | |
| 58 | except: |
| 59 | log_error("Controller IF has issue") |
| 60 | exit(1) |
| 61 | |
| 62 | srcSwitch = parsedResult['srcPort']['dpid']['value']; |
| 63 | srcPort = parsedResult['srcPort']['port']['value']; |
| 64 | dstSwitch = parsedResult['dstPort']['dpid']['value']; |
| 65 | dstPort = parsedResult['dstPort']['port']['value']; |
| 66 | |
| 67 | print "DataPath: (src = %s/%s dst = %s/%s)" % (srcSwitch, srcPort, dstSwitch, dstPort); |
| 68 | |
| 69 | for f in parsedResult['flowEntries']: |
| 70 | inPort = f['inPort']['value']; |
| 71 | outPort = f['outPort']['value']; |
| 72 | dpid = f['dpid']['value'] |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 73 | print " FlowEntry: (%s, %s, %s)" % (inPort, dpid, outPort) |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 74 | |
| 75 | return parsedResult |
| 76 | |
| 77 | def add_flow_path(flow_path): |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 78 | flow_path_json = json.dumps(flow_path) |
| 79 | |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 80 | try: |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 81 | command = "curl -s -H 'Content-Type: application/json' -d '%s' http://%s:%s/wm/flow/add/json" % (flow_path_json, ControllerIP, ControllerPort) |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 82 | debug("add_flow_path %s" % command) |
| 83 | result = os.popen(command).read() |
| 84 | debug("result %s" % result) |
| 85 | # parsedResult = json.loads(result) |
| 86 | # debug("parsed %s" % parsedResult) |
| 87 | except: |
| 88 | log_error("Controller IF has issue") |
| 89 | exit(1) |
| 90 | |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 91 | def delete_flow_path(flow_id): |
| 92 | command = "curl -s \"http://%s:%s/wm/flow/delete/%s/json\"" % (ControllerIP, ControllerPort, flow_id) |
| 93 | debug("delete_flow_path %s" % command) |
| 94 | result = os.popen(command).read() |
| 95 | debug("result %s" % result) |
| 96 | # parsedResult = json.loads(result) |
| 97 | # debug("parsed %s" % parsedResult) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 98 | |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 99 | def extract_flow_args(my_args): |
| 100 | # Check the arguments |
| 101 | if len(my_args) < 6: |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 102 | log_error(usage_msg) |
| 103 | exit(1) |
| 104 | |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 105 | # Extract the mandatory arguments |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 106 | my_flow_id = my_args[0] |
| 107 | my_installer_id = my_args[1] |
| 108 | my_src_dpid = my_args[2] |
| 109 | my_src_port = my_args[3] |
| 110 | my_dst_dpid = my_args[4] |
| 111 | my_dst_port = my_args[5] |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 112 | |
| 113 | # |
| 114 | # Extract the "match" and "action" arguments |
| 115 | # |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 116 | match = {} |
| 117 | matchInPortEnabled = True # NOTE: Enabled by default |
| 118 | actions = [] |
| 119 | actionOutputEnabled = True # NOTE: Enabled by default |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 120 | idx = 6 |
| 121 | while idx < len(my_args): |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 122 | action = {} |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 123 | arg1 = my_args[idx] |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 124 | idx = idx + 1 |
| 125 | # Extract the second argument |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 126 | if idx >= len(my_args): |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 127 | error_arg = "ERROR: Missing or invalid '" + arg1 + "' argument" |
| 128 | log_error(error_arg) |
| 129 | log_error(usage_msg) |
| 130 | exit(1) |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 131 | arg2 = my_args[idx] |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 132 | idx = idx + 1 |
| 133 | |
| 134 | if arg1 == "matchInPort": |
| 135 | # Just mark whether inPort matching is enabled |
| 136 | matchInPortEnabled = arg2 in ['True', 'true'] |
| 137 | # inPort = {} |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 138 | # inPort['value'] = int(arg2, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 139 | # match['inPort'] = inPort |
| 140 | ## match['matchInPort'] = True |
| 141 | elif arg1 == "matchSrcMac": |
| 142 | srcMac = {} |
| 143 | srcMac['value'] = arg2 |
| 144 | match['srcMac'] = srcMac |
| 145 | # match['matchSrcMac'] = True |
| 146 | elif arg1 == "matchDstMac": |
| 147 | dstMac = {} |
| 148 | dstMac['value'] = arg2 |
| 149 | match['dstMac'] = dstMac |
| 150 | # match['matchDstMac'] = True |
| 151 | elif arg1 == "matchVlanId": |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 152 | match['vlanId'] = int(arg2, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 153 | # match['matchVlanId'] = True |
| 154 | elif arg1 == "matchVlanPriority": |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 155 | match['vlanPriority'] = int(arg2, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 156 | # match['matchVlanPriority'] = True |
| 157 | elif arg1 == "matchEthernetFrameType": |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 158 | match['ethernetFrameType'] = int(arg2, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 159 | # match['matchEthernetFrameType'] = True |
| 160 | elif arg1 == "matchIpToS": |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 161 | match['ipToS'] = int(arg2, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 162 | # match['matchIpToS'] = True |
| 163 | elif arg1 == "matchIpProto": |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 164 | match['ipProto'] = int(arg2, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 165 | # match['matchIpProto'] = True |
| 166 | elif arg1 == "matchSrcIPv4Net": |
| 167 | srcIPv4Net = {} |
| 168 | srcIPv4Net['value'] = arg2 |
| 169 | match['srcIPv4Net'] = srcIPv4Net |
| 170 | # match['matchSrcIPv4Net'] = True |
| 171 | elif arg1 == "matchDstIPv4Net": |
| 172 | dstIPv4Net = {} |
| 173 | dstIPv4Net['value'] = arg2 |
| 174 | match['dstIPv4Net'] = dstIPv4Net |
| 175 | # match['matchDstIPv4Net'] = True |
| 176 | elif arg1 == "matchSrcTcpUdpPort": |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 177 | match['srcTcpUdpPort'] = int(arg2, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 178 | # match['matchSrcTcpUdpPort'] = True |
| 179 | elif arg1 == "matchDstTcpUdpPort": |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 180 | match['dstTcpUdpPort'] = int(arg2, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 181 | # match['matchDstTcpUdpPort'] = True |
| 182 | elif arg1 == "actionOutput": |
| 183 | # Just mark whether ACTION_OUTPUT action is enabled |
| 184 | actionOutputEnabled = arg2 in ['True', 'true'] |
| 185 | # |
| 186 | # TODO: Complete the implementation for ACTION_OUTPUT |
| 187 | # actionOutput = {} |
| 188 | # outPort = {} |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 189 | # outPort['value'] = int(arg2, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 190 | # actionOutput['port'] = outPort |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 191 | # actionOutput['maxLen'] = int(arg3, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 192 | # action['actionOutput'] = actionOutput |
| 193 | # # action['actionType'] = 'ACTION_OUTPUT' |
| 194 | # actions.append(action) |
| 195 | # |
| 196 | elif arg1 == "actionSetVlanId": |
| 197 | vlanId = {} |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 198 | vlanId['vlanId'] = int(arg2, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 199 | action['actionSetVlanId'] = vlanId |
| 200 | # action['actionType'] = 'ACTION_SET_VLAN_VID' |
| 201 | actions.append(copy.deepcopy(action)) |
| 202 | elif arg1 == "actionSetVlanPriority": |
| 203 | vlanPriority = {} |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 204 | vlanPriority['vlanPriority'] = int(arg2, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 205 | action['actionSetVlanPriority'] = vlanPriority |
| 206 | # action['actionType'] = 'ACTION_SET_VLAN_PCP' |
| 207 | actions.append(copy.deepcopy(action)) |
| 208 | elif arg1 == "actionSetIpToS": |
| 209 | ipToS = {} |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 210 | ipToS['ipToS'] = int(arg2, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 211 | action['actionSetIpToS'] = ipToS |
| 212 | # action['actionType'] = 'ACTION_SET_NW_TOS' |
| 213 | actions.append(copy.deepcopy(action)) |
| 214 | elif arg1 == "actionSetTcpUdpSrcPort": |
| 215 | tcpUdpSrcPort = {} |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 216 | tcpUdpSrcPort['port'] = int(arg2, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 217 | action['actionSetTcpUdpSrcPort'] = tcpUdpSrcPort |
| 218 | # action['actionType'] = 'ACTION_SET_TP_SRC' |
| 219 | actions.append(copy.deepcopy(action)) |
| 220 | elif arg1 == "actionSetTcpUdpDstPort": |
| 221 | tcpUdpDstPort = {} |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 222 | tcpUdpDstPort['port'] = int(arg2, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 223 | action['actionSetTcpUdpDstPort'] = tcpUdpDstPort |
| 224 | # action['actionType'] = 'ACTION_SET_TP_DST' |
| 225 | actions.append(copy.deepcopy(action)) |
| 226 | elif arg1 == "actionStripVlan": |
| 227 | stripVlan = {} |
| 228 | stripVlan['stripVlan'] = arg2 in ['True', 'true'] |
| 229 | action['actionStripVlan'] = stripVlan |
| 230 | # action['actionType'] = 'ACTION_STRIP_VLAN' |
| 231 | actions.append(copy.deepcopy(action)) |
| 232 | elif arg1 == "actionSetEthernetSrcAddr": |
| 233 | ethernetSrcAddr = {} |
| 234 | ethernetSrcAddr['value'] = arg2 |
| 235 | setEthernetSrcAddr = {} |
| 236 | setEthernetSrcAddr['addr'] = ethernetSrcAddr |
| 237 | action['actionSetEthernetSrcAddr'] = setEthernetSrcAddr |
| 238 | # action['actionType'] = 'ACTION_SET_DL_SRC' |
| 239 | actions.append(copy.deepcopy(action)) |
| 240 | elif arg1 == "actionSetEthernetDstAddr": |
| 241 | ethernetDstAddr = {} |
| 242 | ethernetDstAddr['value'] = arg2 |
| 243 | setEthernetDstAddr = {} |
| 244 | setEthernetDstAddr['addr'] = ethernetDstAddr |
| 245 | action['actionSetEthernetDstAddr'] = setEthernetDstAddr |
| 246 | # action['actionType'] = 'ACTION_SET_DL_DST' |
| 247 | actions.append(copy.deepcopy(action)) |
| 248 | elif arg1 == "actionSetIPv4SrcAddr": |
| 249 | IPv4SrcAddr = {} |
| 250 | IPv4SrcAddr['value'] = arg2 |
| 251 | setIPv4SrcAddr = {} |
| 252 | setIPv4SrcAddr['addr'] = IPv4SrcAddr |
| 253 | action['actionSetIPv4SrcAddr'] = setIPv4SrcAddr |
| 254 | # action['actionType'] = 'ACTION_SET_NW_SRC' |
| 255 | actions.append(copy.deepcopy(action)) |
| 256 | elif arg1 == "actionSetIPv4DstAddr": |
| 257 | IPv4DstAddr = {} |
| 258 | IPv4DstAddr['value'] = arg2 |
| 259 | setIPv4DstAddr = {} |
| 260 | setIPv4DstAddr['addr'] = IPv4DstAddr |
| 261 | action['actionSetIPv4DstAddr'] = setIPv4DstAddr |
| 262 | # action['actionType'] = 'ACTION_SET_NW_DST' |
| 263 | actions.append(copy.deepcopy(action)) |
| 264 | elif arg1 == "actionEnqueue": |
| 265 | # TODO: Implement ACTION_ENQUEUE |
| 266 | actionEnqueue = {} |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 267 | # actionEnqueue['queueId'] = int(arg2, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 268 | # enqueuePort = {} |
Pavlin Radoslavov | 1474891 | 2013-03-12 15:44:56 -0700 | [diff] [blame] | 269 | # enqueuePort['value'] = int(arg3, 0) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 270 | # actionEnqueue['port'] = enqueuePort |
| 271 | # action['actionEnqueue'] = actionEnqueue |
| 272 | # # action['actionType'] = 'ACTION_ENQUEUE' |
| 273 | # actions.append(copy.deepcopy(action)) |
| 274 | # |
| 275 | else: |
| 276 | log_error("ERROR: Unknown argument '%s'" % (arg1)) |
| 277 | log_error(usage_msg) |
| 278 | exit(1) |
| 279 | |
Pavlin Radoslavov | 8a002d9 | 2013-03-13 20:20:43 -0700 | [diff] [blame] | 280 | return { |
| 281 | 'my_flow_id' : my_flow_id, |
| 282 | 'my_installer_id' : my_installer_id, |
| 283 | 'my_src_dpid' : my_src_dpid, |
| 284 | 'my_src_port' : my_src_port, |
| 285 | 'my_dst_dpid' : my_dst_dpid, |
| 286 | 'my_dst_port' : my_dst_port, |
| 287 | 'match' : match, |
| 288 | 'matchInPortEnabled' : matchInPortEnabled, |
| 289 | 'actions' : actions, |
| 290 | 'actionOutputEnabled' : actionOutputEnabled |
| 291 | } |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 292 | |
| 293 | def compute_data_path(parsed_args): |
| 294 | |
| 295 | my_src_dpid = parsed_args['my_src_dpid'] |
| 296 | my_src_port = parsed_args['my_src_port'] |
| 297 | my_dst_dpid = parsed_args['my_dst_dpid'] |
| 298 | my_dst_port = parsed_args['my_dst_port'] |
| 299 | |
| 300 | # Compute the shortest path |
| 301 | data_path = shortest_path(my_src_dpid, my_src_port, my_dst_dpid, my_dst_port) |
| 302 | |
| 303 | debug("Data Path: %s" % data_path) |
| 304 | return data_path |
| 305 | |
| 306 | def compute_flow_path(parsed_args, data_path): |
| 307 | |
| 308 | my_flow_id = parsed_args['my_flow_id'] |
| 309 | my_installer_id = parsed_args['my_installer_id'] |
| 310 | match = parsed_args['match'] |
| 311 | matchInPortEnabled = parsed_args['matchInPortEnabled'] |
| 312 | actions = parsed_args['actions'] |
| 313 | actionOutputEnabled = parsed_args['actionOutputEnabled'] |
| 314 | my_data_path = copy.deepcopy(data_path) |
| 315 | |
| 316 | flow_id = {} |
| 317 | flow_id['value'] = my_flow_id |
| 318 | installer_id = {} |
| 319 | installer_id['value'] = my_installer_id |
| 320 | |
| 321 | flow_path = {} |
| 322 | flow_path['flowId'] = flow_id |
| 323 | flow_path['installerId'] = installer_id |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 324 | |
| 325 | # |
| 326 | # Add the match conditions to each flow entry |
| 327 | # |
| 328 | if (len(match) > 0) or matchInPortEnabled: |
| 329 | idx = 0 |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 330 | while idx < len(my_data_path['flowEntries']): |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 331 | if matchInPortEnabled: |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 332 | inPort = my_data_path['flowEntries'][idx]['inPort'] |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 333 | match['inPort'] = copy.deepcopy(inPort) |
| 334 | # match['matchInPort'] = True |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 335 | my_data_path['flowEntries'][idx]['flowEntryMatch'] = copy.deepcopy(match) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 336 | idx = idx + 1 |
| 337 | |
| 338 | # |
| 339 | # Set the actions for each flow entry |
| 340 | # NOTE: The actions from the command line are aplied |
| 341 | # ONLY to the first flow entry. |
| 342 | # |
| 343 | # If ACTION_OUTPUT action is enabled, then apply it |
| 344 | # to each flow entry. |
| 345 | # |
| 346 | if (len(actions) > 0) or actionOutputEnabled: |
| 347 | idx = 0 |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 348 | while idx < len(my_data_path['flowEntries']): |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 349 | if idx > 0: |
| 350 | actions = [] # Reset the actions for all but first entry |
| 351 | action = {} |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 352 | outPort = my_data_path['flowEntries'][idx]['outPort'] |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 353 | actionOutput = {} |
| 354 | actionOutput['port'] = copy.deepcopy(outPort) |
| 355 | # actionOutput['maxLen'] = 0 # TODO: not used for now |
| 356 | action['actionOutput'] = copy.deepcopy(actionOutput) |
| 357 | # action['actionType'] = 'ACTION_OUTPUT' |
| 358 | actions.append(copy.deepcopy(action)) |
| 359 | |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 360 | my_data_path['flowEntries'][idx]['flowEntryActions'] = copy.deepcopy(actions) |
Pavlin Radoslavov | f13923a | 2013-03-11 19:42:17 -0700 | [diff] [blame] | 361 | idx = idx + 1 |
| 362 | |
| 363 | |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 364 | flow_path['dataPath'] = my_data_path |
| 365 | debug("Flow Path: %s" % flow_path) |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 366 | |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 367 | add_flow_path(flow_path) |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 368 | |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 369 | |
| 370 | if __name__ == "__main__": |
| 371 | usage_msg = "Usage: %s [Flags] <flow-id> <installer-id> <src-dpid> <src-port> <dest-dpid> <dest-port> [Match Conditions] [Actions]\n" % (sys.argv[0]) |
| 372 | usage_msg = usage_msg + " Flags:\n" |
| 373 | usage_msg = usage_msg + " -m Monitor and maintain the installed shortest path\n" |
| 374 | usage_msg = usage_msg + " Match Conditions:\n" |
| 375 | usage_msg = usage_msg + " matchInPort <True|False> (default to True)\n" |
| 376 | usage_msg = usage_msg + " matchSrcMac <source MAC address>\n" |
| 377 | usage_msg = usage_msg + " matchDstMac <destination MAC address>\n" |
| 378 | usage_msg = usage_msg + " matchSrcIPv4Net <source IPv4 network address>\n" |
| 379 | usage_msg = usage_msg + " matchDstIPv4Net <destination IPv4 network address>\n" |
| 380 | usage_msg = usage_msg + " matchEthernetFrameType <Ethernet frame type>\n" |
| 381 | |
| 382 | usage_msg = usage_msg + " Match Conditions (not implemented yet):\n" |
| 383 | usage_msg = usage_msg + " matchVlanId <VLAN ID>\n" |
| 384 | usage_msg = usage_msg + " matchVlanPriority <VLAN priority>\n" |
| 385 | usage_msg = usage_msg + " matchIpToS <IP ToS (DSCP field, 6 bits)>\n" |
| 386 | usage_msg = usage_msg + " matchIpProto <IP protocol>\n" |
| 387 | usage_msg = usage_msg + " matchSrcTcpUdpPort <source TCP/UDP port>\n" |
| 388 | usage_msg = usage_msg + " matchDstTcpUdpPort <destination TCP/UDP port>\n" |
| 389 | usage_msg = usage_msg + " Actions:\n" |
| 390 | usage_msg = usage_msg + " actionOutput <True|False> (default to True)\n" |
| 391 | usage_msg = usage_msg + " actionSetEthernetSrcAddr <source MAC address>\n" |
| 392 | usage_msg = usage_msg + " actionSetEthernetDstAddr <destination MAC address>\n" |
| 393 | usage_msg = usage_msg + " actionSetIPv4SrcAddr <source IPv4 address>\n" |
| 394 | usage_msg = usage_msg + " actionSetIPv4DstAddr <destination IPv4 address>\n" |
| 395 | usage_msg = usage_msg + " Actions (not implemented yet):\n" |
| 396 | usage_msg = usage_msg + " actionSetVlanId <VLAN ID>\n" |
| 397 | usage_msg = usage_msg + " actionSetVlanPriority <VLAN priority>\n" |
| 398 | usage_msg = usage_msg + " actionSetIpToS <IP ToS (DSCP field, 6 bits)>\n" |
| 399 | usage_msg = usage_msg + " actionSetTcpUdpSrcPort <source TCP/UDP port>\n" |
| 400 | usage_msg = usage_msg + " actionSetTcpUdpDstPort <destination TCP/UDP port>\n" |
| 401 | usage_msg = usage_msg + " actionStripVlan <True|False>\n" |
| 402 | usage_msg = usage_msg + " actionEnqueue <dummy argument>\n" |
| 403 | |
| 404 | # app.debug = False; |
| 405 | |
| 406 | # Usage info |
| 407 | if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"): |
| 408 | print(usage_msg) |
| 409 | exit(0) |
| 410 | |
| 411 | # |
| 412 | # Check the flags |
| 413 | # |
| 414 | start_argv_index = 1 |
| 415 | if len(sys.argv) > 1 and sys.argv[1] == "-m": |
| 416 | MonitoringEnabled = True |
| 417 | start_argv_index = start_argv_index + 1 |
| 418 | |
| 419 | # |
| 420 | # Parse the remaining arguments |
| 421 | # |
| 422 | my_args = sys.argv[start_argv_index:] |
| 423 | parsed_args = copy.deepcopy(extract_flow_args(my_args)) |
| 424 | |
| 425 | last_data_path = [] |
| 426 | my_flow_id = parsed_args['my_flow_id'] |
| 427 | # Cleanup leftover state |
| 428 | delete_flow_path(my_flow_id) |
| 429 | |
| 430 | while True: |
| 431 | data_path = compute_data_path(parsed_args) |
| 432 | if data_path != last_data_path: |
| 433 | if len(last_data_path) > 0: |
| 434 | delete_flow_path(my_flow_id) |
Pavlin Radoslavov | eb2d5a2 | 2013-03-14 19:58:14 -0700 | [diff] [blame] | 435 | if len(data_path) > 0: |
| 436 | flow_path = compute_flow_path(parsed_args, data_path) |
| 437 | add_flow_path(flow_path) |
Pavlin Radoslavov | 3f3778a | 2013-03-13 08:16:57 -0700 | [diff] [blame] | 438 | last_data_path = data_path |
| 439 | |
| 440 | if MonitoringEnabled != True: |
| 441 | break |
| 442 | time.sleep(1) |