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