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