blob: f61e9260394c0626d7b8e5dd727a3776daa945b4 [file] [log] [blame]
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -08001#! /usr/bin/env python
2# -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*-
3
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -07004import copy
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -08005import pprint
6import os
7import sys
8import subprocess
9import json
10import argparse
11import io
12import time
13
14from 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 Radoslavov3f3778a2013-03-13 08:16:57 -070021ControllerIP = "127.0.0.1"
22ControllerPort = 8080
23MonitoringEnabled = False
Pavlin Radoslavovb549f082013-03-29 04:28:27 -070024MonitoringByOnos = False
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -070025ReadFromFile = ""
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080026
27DEBUG=0
28pp = pprint.PrettyPrinter(indent=4)
29
30app = Flask(__name__)
31
32## Worker Functions ##
33def log_error(txt):
34 print '%s' % (txt)
35
36def 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#
45def 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 Radoslavoveb2d5a22013-03-14 19:58:14 -070049 parsedResult = []
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080050
51 result = os.popen(command).read()
52 debug("result %s" % result)
53 if len(result) == 0:
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -070054 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 Radoslavovf4ad9892013-03-04 14:15:19 -080058
59 except:
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -070060 log_error("Controller IF has issue: No Path found from %s/%s to %s/%s" % (v1, p1, v2, p2))
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080061
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -070062 return parsedResult
63
64def 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 Radoslavovf4ad9892013-03-04 14:15:19 -080072
73 print "DataPath: (src = %s/%s dst = %s/%s)" % (srcSwitch, srcPort, dstSwitch, dstPort);
74
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -070075 for f in data_path['flowEntries']:
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080076 inPort = f['inPort']['value'];
77 outPort = f['outPort']['value'];
78 dpid = f['dpid']['value']
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -070079 print " FlowEntry: (%s, %s, %s)" % (inPort, dpid, outPort)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080080
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080081def add_flow_path(flow_path):
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -070082 flow_path_json = json.dumps(flow_path)
83
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080084 try:
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -070085 command = "curl -s -H 'Content-Type: application/json' -d '%s' http://%s:%s/wm/flow/add/json" % (flow_path_json, ControllerIP, ControllerPort)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080086 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 Radoslavovb549f082013-03-29 04:28:27 -070095def 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 Radoslavov3f3778a2013-03-13 08:16:57 -0700109def 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 Radoslavovf13923a2013-03-11 19:42:17 -0700116
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700117def extract_flow_args(my_args):
118 # Check the arguments
119 if len(my_args) < 6:
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800120 log_error(usage_msg)
121 exit(1)
122
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700123 # Extract the mandatory arguments
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700124 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 Radoslavovf13923a2013-03-11 19:42:17 -0700130
131 #
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700132 # Extract the "flowPathFlags", "match" and "action" arguments
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700133 #
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700134 flowPathFlags = 0L
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700135 match = {}
136 matchInPortEnabled = True # NOTE: Enabled by default
137 actions = []
138 actionOutputEnabled = True # NOTE: Enabled by default
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700139 idx = 6
140 while idx < len(my_args):
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700141 action = {}
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700142 arg1 = my_args[idx]
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700143 idx = idx + 1
144 # Extract the second argument
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700145 if idx >= len(my_args):
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700146 error_arg = "ERROR: Missing or invalid '" + arg1 + "' argument"
147 log_error(error_arg)
148 log_error(usage_msg)
149 exit(1)
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700150 arg2 = my_args[idx]
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700151 idx = idx + 1
152
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700153 if arg1 == "flowPathFlags":
154 if "DISCARD_FIRST_HOP_ENTRY" in arg2:
155 flowPathFlags = flowPathFlags + 0x1
156 if "KEEP_ONLY_FIRST_HOP_ENTRY" in arg2:
157 flowPathFlags = flowPathFlags + 0x2
158 elif arg1 == "matchInPort":
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700159 # Just mark whether inPort matching is enabled
160 matchInPortEnabled = arg2 in ['True', 'true']
161 # inPort = {}
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700162 # inPort['value'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700163 # match['inPort'] = inPort
164 ## match['matchInPort'] = True
165 elif arg1 == "matchSrcMac":
166 srcMac = {}
167 srcMac['value'] = arg2
168 match['srcMac'] = srcMac
169 # match['matchSrcMac'] = True
170 elif arg1 == "matchDstMac":
171 dstMac = {}
172 dstMac['value'] = arg2
173 match['dstMac'] = dstMac
174 # match['matchDstMac'] = True
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700175 elif arg1 == "matchEthernetFrameType":
176 match['ethernetFrameType'] = int(arg2, 0)
177 # match['matchEthernetFrameType'] = True
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700178 elif arg1 == "matchVlanId":
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700179 match['vlanId'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700180 # match['matchVlanId'] = True
181 elif arg1 == "matchVlanPriority":
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700182 match['vlanPriority'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700183 # match['matchVlanPriority'] = True
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700184 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
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700194 elif arg1 == "matchIpProto":
195 match['ipProto'] = int(arg2, 0)
196 # match['matchIpProto'] = True
197 elif arg1 == "matchIpToS":
198 match['ipToS'] = int(arg2, 0)
199 # match['matchIpToS'] = True
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700200 elif arg1 == "matchSrcTcpUdpPort":
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700201 match['srcTcpUdpPort'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700202 # match['matchSrcTcpUdpPort'] = True
203 elif arg1 == "matchDstTcpUdpPort":
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700204 match['dstTcpUdpPort'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700205 # match['matchDstTcpUdpPort'] = True
206 elif arg1 == "actionOutput":
207 # Just mark whether ACTION_OUTPUT action is enabled
208 actionOutputEnabled = arg2 in ['True', 'true']
209 #
210 # TODO: Complete the implementation for ACTION_OUTPUT
211 # actionOutput = {}
212 # outPort = {}
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700213 # outPort['value'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700214 # actionOutput['port'] = outPort
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700215 # actionOutput['maxLen'] = int(arg3, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700216 # action['actionOutput'] = actionOutput
217 # # action['actionType'] = 'ACTION_OUTPUT'
218 # actions.append(action)
219 #
220 elif arg1 == "actionSetVlanId":
221 vlanId = {}
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700222 vlanId['vlanId'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700223 action['actionSetVlanId'] = vlanId
224 # action['actionType'] = 'ACTION_SET_VLAN_VID'
225 actions.append(copy.deepcopy(action))
226 elif arg1 == "actionSetVlanPriority":
227 vlanPriority = {}
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700228 vlanPriority['vlanPriority'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700229 action['actionSetVlanPriority'] = vlanPriority
230 # action['actionType'] = 'ACTION_SET_VLAN_PCP'
231 actions.append(copy.deepcopy(action))
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700232 elif arg1 == "actionStripVlan":
233 stripVlan = {}
234 stripVlan['stripVlan'] = arg2 in ['True', 'true']
235 action['actionStripVlan'] = stripVlan
236 # action['actionType'] = 'ACTION_STRIP_VLAN'
237 actions.append(copy.deepcopy(action))
238 elif arg1 == "actionSetEthernetSrcAddr":
239 ethernetSrcAddr = {}
240 ethernetSrcAddr['value'] = arg2
241 setEthernetSrcAddr = {}
242 setEthernetSrcAddr['addr'] = ethernetSrcAddr
243 action['actionSetEthernetSrcAddr'] = setEthernetSrcAddr
244 # action['actionType'] = 'ACTION_SET_DL_SRC'
245 actions.append(copy.deepcopy(action))
246 elif arg1 == "actionSetEthernetDstAddr":
247 ethernetDstAddr = {}
248 ethernetDstAddr['value'] = arg2
249 setEthernetDstAddr = {}
250 setEthernetDstAddr['addr'] = ethernetDstAddr
251 action['actionSetEthernetDstAddr'] = setEthernetDstAddr
252 # action['actionType'] = 'ACTION_SET_DL_DST'
253 actions.append(copy.deepcopy(action))
254 elif arg1 == "actionSetIPv4SrcAddr":
255 IPv4SrcAddr = {}
256 IPv4SrcAddr['value'] = arg2
257 setIPv4SrcAddr = {}
258 setIPv4SrcAddr['addr'] = IPv4SrcAddr
259 action['actionSetIPv4SrcAddr'] = setIPv4SrcAddr
260 # action['actionType'] = 'ACTION_SET_NW_SRC'
261 actions.append(copy.deepcopy(action))
262 elif arg1 == "actionSetIPv4DstAddr":
263 IPv4DstAddr = {}
264 IPv4DstAddr['value'] = arg2
265 setIPv4DstAddr = {}
266 setIPv4DstAddr['addr'] = IPv4DstAddr
267 action['actionSetIPv4DstAddr'] = setIPv4DstAddr
268 # action['actionType'] = 'ACTION_SET_NW_DST'
269 actions.append(copy.deepcopy(action))
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700270 elif arg1 == "actionSetIpToS":
271 ipToS = {}
272 ipToS['ipToS'] = int(arg2, 0)
273 action['actionSetIpToS'] = ipToS
274 # action['actionType'] = 'ACTION_SET_NW_TOS'
275 actions.append(copy.deepcopy(action))
276 elif arg1 == "actionSetTcpUdpSrcPort":
277 tcpUdpSrcPort = {}
278 tcpUdpSrcPort['port'] = int(arg2, 0)
279 action['actionSetTcpUdpSrcPort'] = tcpUdpSrcPort
280 # action['actionType'] = 'ACTION_SET_TP_SRC'
281 actions.append(copy.deepcopy(action))
282 elif arg1 == "actionSetTcpUdpDstPort":
283 tcpUdpDstPort = {}
284 tcpUdpDstPort['port'] = int(arg2, 0)
285 action['actionSetTcpUdpDstPort'] = tcpUdpDstPort
286 # action['actionType'] = 'ACTION_SET_TP_DST'
287 actions.append(copy.deepcopy(action))
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700288 elif arg1 == "actionEnqueue":
289 # TODO: Implement ACTION_ENQUEUE
290 actionEnqueue = {}
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700291 # actionEnqueue['queueId'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700292 # enqueuePort = {}
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700293 # enqueuePort['value'] = int(arg3, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700294 # actionEnqueue['port'] = enqueuePort
295 # action['actionEnqueue'] = actionEnqueue
296 # # action['actionType'] = 'ACTION_ENQUEUE'
297 # actions.append(copy.deepcopy(action))
298 #
299 else:
300 log_error("ERROR: Unknown argument '%s'" % (arg1))
301 log_error(usage_msg)
302 exit(1)
303
Pavlin Radoslavov8a002d92013-03-13 20:20:43 -0700304 return {
305 'my_flow_id' : my_flow_id,
306 'my_installer_id' : my_installer_id,
307 'my_src_dpid' : my_src_dpid,
308 'my_src_port' : my_src_port,
309 'my_dst_dpid' : my_dst_dpid,
310 'my_dst_port' : my_dst_port,
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700311 'flowPathFlags' : flowPathFlags,
Pavlin Radoslavov8a002d92013-03-13 20:20:43 -0700312 'match' : match,
313 'matchInPortEnabled' : matchInPortEnabled,
314 'actions' : actions,
315 'actionOutputEnabled' : actionOutputEnabled
316 }
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700317
318def compute_data_path(parsed_args):
319
320 my_src_dpid = parsed_args['my_src_dpid']
321 my_src_port = parsed_args['my_src_port']
322 my_dst_dpid = parsed_args['my_dst_dpid']
323 my_dst_port = parsed_args['my_dst_port']
324
325 # Compute the shortest path
326 data_path = shortest_path(my_src_dpid, my_src_port, my_dst_dpid, my_dst_port)
327
328 debug("Data Path: %s" % data_path)
329 return data_path
330
331def compute_flow_path(parsed_args, data_path):
332
333 my_flow_id = parsed_args['my_flow_id']
334 my_installer_id = parsed_args['my_installer_id']
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700335 myFlowPathFlags = parsed_args['flowPathFlags']
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700336 match = parsed_args['match']
337 matchInPortEnabled = parsed_args['matchInPortEnabled']
338 actions = parsed_args['actions']
339 actionOutputEnabled = parsed_args['actionOutputEnabled']
340 my_data_path = copy.deepcopy(data_path)
341
342 flow_id = {}
343 flow_id['value'] = my_flow_id
344 installer_id = {}
345 installer_id['value'] = my_installer_id
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700346 flowPathFlags = {}
347 flowPathFlags['flags'] = myFlowPathFlags
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700348
349 flow_path = {}
350 flow_path['flowId'] = flow_id
351 flow_path['installerId'] = installer_id
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700352 flow_path['flowPathFlags'] = flowPathFlags
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700353
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700354 if (len(match) > 0):
355 flow_path['flowEntryMatch'] = copy.deepcopy(match)
356
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700357 #
358 # Add the match conditions to each flow entry
359 #
360 if (len(match) > 0) or matchInPortEnabled:
361 idx = 0
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700362 while idx < len(my_data_path['flowEntries']):
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700363 if matchInPortEnabled:
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700364 inPort = my_data_path['flowEntries'][idx]['inPort']
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700365 match['inPort'] = copy.deepcopy(inPort)
366 # match['matchInPort'] = True
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700367 my_data_path['flowEntries'][idx]['flowEntryMatch'] = copy.deepcopy(match)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700368 idx = idx + 1
369
370 #
371 # Set the actions for each flow entry
372 # NOTE: The actions from the command line are aplied
373 # ONLY to the first flow entry.
374 #
375 # If ACTION_OUTPUT action is enabled, then apply it
376 # to each flow entry.
377 #
378 if (len(actions) > 0) or actionOutputEnabled:
379 idx = 0
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700380 while idx < len(my_data_path['flowEntries']):
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700381 if idx > 0:
382 actions = [] # Reset the actions for all but first entry
383 action = {}
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700384 outPort = my_data_path['flowEntries'][idx]['outPort']
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700385 actionOutput = {}
386 actionOutput['port'] = copy.deepcopy(outPort)
387 # actionOutput['maxLen'] = 0 # TODO: not used for now
388 action['actionOutput'] = copy.deepcopy(actionOutput)
389 # action['actionType'] = 'ACTION_OUTPUT'
390 actions.append(copy.deepcopy(action))
391
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700392 my_data_path['flowEntries'][idx]['flowEntryActions'] = copy.deepcopy(actions)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700393 idx = idx + 1
394
395
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700396 flow_path['dataPath'] = my_data_path
397 debug("Flow Path: %s" % flow_path)
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700398 return flow_path
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800399
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700400def exec_monitoring_by_onos(parsed_args):
401 idx = 0
402 while idx < len(parsed_args):
403 data_path = {}
404 src_dpid = {}
405 src_port = {}
406 dst_dpid = {}
407 dst_port = {}
408 src_switch_port = {}
409 dst_switch_port = {}
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700410 flow_entries = []
411
412 src_dpid['value'] = parsed_args[idx]['my_src_dpid']
413 src_port['value'] = parsed_args[idx]['my_src_port']
414 dst_dpid['value'] = parsed_args[idx]['my_dst_dpid']
415 dst_port['value'] = parsed_args[idx]['my_dst_port']
416 src_switch_port['dpid'] = src_dpid
417 src_switch_port['port'] = src_port
418 dst_switch_port['dpid'] = dst_dpid
419 dst_switch_port['port'] = dst_port
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700420
421 data_path['srcPort'] = copy.deepcopy(src_switch_port)
422 data_path['dstPort'] = copy.deepcopy(dst_switch_port)
423 data_path['flowEntries'] = copy.deepcopy(flow_entries)
424
425 #
426 # XXX: Explicitly disable the InPort matching, and
427 # the Output action, because they get in the way
428 # during the compute_flow_path() processing.
429 #
430 parsed_args[idx]['matchInPortEnabled'] = False
431 parsed_args[idx]['actionOutputEnabled'] = False
432
433 flow_path = compute_flow_path(parsed_args[idx], data_path)
434 add_shortest_path_flow(flow_path)
435
436 idx = idx + 1
437
438
439def exec_processing_by_script(parsed_args):
440 #
441 # Initialization
442 #
443 last_data_paths = []
444 idx = 0
445 while idx < len(parsed_args):
446 last_data_path = []
447 last_data_paths.append(copy.deepcopy(last_data_path))
448 idx = idx + 1
449
450 #
451 # Do the work: install and/or periodically monitor each flow
452 #
453 while True:
454 idx = 0
455 while idx < len(parsed_args):
456 last_data_path = last_data_paths[idx]
457 my_flow_id = parsed_args[idx]['my_flow_id']
458 data_path = compute_data_path(parsed_args[idx])
459 if data_path != last_data_path:
460 print_data_path(data_path)
461 if len(last_data_path) > 0:
462 delete_flow_path(my_flow_id)
463 if len(data_path) > 0:
464 flow_path = compute_flow_path(parsed_args[idx], data_path)
465 add_flow_path(flow_path)
466 last_data_paths[idx] = copy.deepcopy(data_path)
467 idx = idx + 1
468
469 if MonitoringEnabled != True:
470 break
471 time.sleep(1)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800472
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700473
474if __name__ == "__main__":
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700475 usage_msg = "Usage: %s [Flags] <flow-id> <installer-id> <src-dpid> <src-port> <dest-dpid> <dest-port> [Flow Path Flags] [Match Conditions] [Actions]\n" % (sys.argv[0])
Pavlin Radoslavov7be3bac2013-03-27 09:59:34 -0700476 usage_msg = usage_msg + "\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700477 usage_msg = usage_msg + " Flags:\n"
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700478 usage_msg = usage_msg + " -m [monitorname] Monitor and maintain the installed shortest path(s)\n"
479 usage_msg = usage_msg + " If 'monitorname' is specified and is set to 'ONOS'\n"
480 usage_msg = usage_msg + " ((case insensitive), then the flow generation and\n"
481 usage_msg = usage_msg + " maintanenance is done by ONOS itself.\n"
482 usage_msg = usage_msg + " Otherwise, it is done by this script.\n"
483 usage_msg = usage_msg + " -f <filename> Read the flow(s) to install from a file\n"
484 usage_msg = usage_msg + " File format: one line per flow starting with <flow-id>\n"
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700485 usage_msg = usage_msg + "\n"
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700486 usage_msg = usage_msg + " Flow Path Flags:\n"
487 usage_msg = usage_msg + " flowPathFlags <Flags> (flag names separated by ',')\n"
488 usage_msg = usage_msg + "\n"
489 usage_msg = usage_msg + " Known flags:\n"
490 usage_msg = usage_msg + " DISCARD_FIRST_HOP_ENTRY : Discard the first-hop flow entry\n"
491 usage_msg = usage_msg + " KEEP_ONLY_FIRST_HOP_ENTRY : Keep only the first-hop flow entry\n"
492 usage_msg = usage_msg + "\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700493 usage_msg = usage_msg + " Match Conditions:\n"
494 usage_msg = usage_msg + " matchInPort <True|False> (default to True)\n"
495 usage_msg = usage_msg + " matchSrcMac <source MAC address>\n"
496 usage_msg = usage_msg + " matchDstMac <destination MAC address>\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700497 usage_msg = usage_msg + " matchEthernetFrameType <Ethernet frame type>\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700498 usage_msg = usage_msg + " matchVlanId <VLAN ID>\n"
499 usage_msg = usage_msg + " matchVlanPriority <VLAN priority>\n"
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700500 usage_msg = usage_msg + " matchSrcIPv4Net <source IPv4 network address>\n"
501 usage_msg = usage_msg + " matchDstIPv4Net <destination IPv4 network address>\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700502 usage_msg = usage_msg + " matchIpProto <IP protocol>\n"
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700503 usage_msg = usage_msg + " matchIpToS <IP ToS (DSCP field, 6 bits)>\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700504 usage_msg = usage_msg + " matchSrcTcpUdpPort <source TCP/UDP port>\n"
505 usage_msg = usage_msg + " matchDstTcpUdpPort <destination TCP/UDP port>\n"
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700506 usage_msg = usage_msg + "\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700507 usage_msg = usage_msg + " Actions:\n"
508 usage_msg = usage_msg + " actionOutput <True|False> (default to True)\n"
509 usage_msg = usage_msg + " actionSetEthernetSrcAddr <source MAC address>\n"
510 usage_msg = usage_msg + " actionSetEthernetDstAddr <destination MAC address>\n"
511 usage_msg = usage_msg + " actionSetIPv4SrcAddr <source IPv4 address>\n"
512 usage_msg = usage_msg + " actionSetIPv4DstAddr <destination IPv4 address>\n"
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700513 usage_msg = usage_msg + "\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700514 usage_msg = usage_msg + " Actions (not implemented yet):\n"
515 usage_msg = usage_msg + " actionSetVlanId <VLAN ID>\n"
516 usage_msg = usage_msg + " actionSetVlanPriority <VLAN priority>\n"
517 usage_msg = usage_msg + " actionSetIpToS <IP ToS (DSCP field, 6 bits)>\n"
518 usage_msg = usage_msg + " actionSetTcpUdpSrcPort <source TCP/UDP port>\n"
519 usage_msg = usage_msg + " actionSetTcpUdpDstPort <destination TCP/UDP port>\n"
520 usage_msg = usage_msg + " actionStripVlan <True|False>\n"
521 usage_msg = usage_msg + " actionEnqueue <dummy argument>\n"
522
523 # app.debug = False;
524
525 # Usage info
526 if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
527 print(usage_msg)
528 exit(0)
529
530 #
531 # Check the flags
532 #
533 start_argv_index = 1
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700534 idx = 1
535 while idx < len(sys.argv):
536 arg1 = sys.argv[idx]
537 idx = idx + 1
538 if arg1 == "-m":
539 MonitoringEnabled = True
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700540 if idx < len(sys.argv):
541 arg2 = sys.argv[idx]
542 if arg2.lower() == "onos":
543 MonitoringByOnos = True
544 idx = idx + 1
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700545 start_argv_index = idx
546 elif arg1 == "-f":
547 if idx >= len(sys.argv):
548 error_arg = "ERROR: Missing or invalid '" + arg1 + "' argument"
549 log_error(error_arg)
550 log_error(usage_msg)
551 exit(1)
552 ReadFromFile = sys.argv[idx]
553 idx = idx + 1
554 start_argv_index = idx
555 else:
556 break;
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700557
558 #
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700559 # Read the arguments from a file or from the remaining command line options
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700560 #
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700561 my_lines = []
562 if len(ReadFromFile) > 0:
563 f = open(ReadFromFile, "rt")
564 my_line = f.readline()
565 while my_line:
566 if len(my_line.rstrip()) > 0 and my_line[0] != "#":
567 my_token_line = my_line.rstrip().split()
568 my_lines.append(my_token_line)
569 my_line = f.readline()
570 else:
571 my_lines.append(copy.deepcopy(sys.argv[start_argv_index:]))
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700572
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700573 #
574 # Initialization
575 #
576 last_data_paths = []
577 parsed_args = []
578 idx = 0
579 while idx < len(my_lines):
580 last_data_path = []
581 last_data_paths.append(copy.deepcopy(last_data_path))
582 #
583 # Parse the flow arguments
584 #
585 my_args = my_lines[idx]
586 parsed_args.append(copy.deepcopy(extract_flow_args(my_args)))
587 # Cleanup leftover state
588 my_flow_id = parsed_args[idx]['my_flow_id']
589 delete_flow_path(my_flow_id)
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700590
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700591 idx = idx + 1
592
593 #
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700594 if MonitoringByOnos == True:
595 exec_monitoring_by_onos(parsed_args)
596 else:
597 exec_processing_by_script(parsed_args)
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700598