blob: 11d9c19e0bb7fc1daadc8955cebec9a624935f0d [file] [log] [blame]
Brian O'Connorb405d082013-12-09 19:45:58 -08001#! /usr/bin/env python
2# -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*-
3
4#
5# A script for generating a number of flows.
6#
7# The output of the script should be saved to a file, and the flows from
8# that file should be added by the following command:
9#
10# web/add_flow.py -f filename
11#
12# NOTE: Currently, some of the parameters fo the flows are hard-coded,
13# and all flows are between same source and destination DPID and ports
14# (differentiated by different matchSrcMac and matchDstMac).
15#
16
17import copy
18import pprint
19import os
20import sys
21import subprocess
22import json
23import argparse
24import io
25import time
26
27## Global Var ##
28
29DEBUG=0
30pp = pprint.PrettyPrinter(indent=4)
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
41if __name__ == "__main__":
42 usage_msg = "Generate a number of flows by using a pre-defined template.\n"
43 usage_msg = usage_msg + "\n"
44 usage_msg = usage_msg + "NOTE: This script is work-in-progress. Currently all flows are within same\n"
45 usage_msg = usage_msg + "pair of switch ports and contain auto-generated MAC-based matching conditions.\n"
46 usage_msg = usage_msg + "\n"
47 usage_msg = usage_msg + "Usage: %s <begin-flow-id> <end-flow-id>\n" % (sys.argv[0])
48 usage_msg = usage_msg + "\n"
49 usage_msg = usage_msg + " The output should be saved to a file, and the flows should be installed\n"
50 usage_msg = usage_msg + " by using the command './add_flow.py -f filename'\n"
51
52
53 # app.debug = False;
54
55 # Usage info
56 if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
57 print(usage_msg)
58 exit(0)
59
60 # Check arguments
61 if len(sys.argv) < 3:
62 log_error(usage_msg)
63 exit(1)
64
65 # Extract the arguments
66 begin_flow_id = int(sys.argv[1], 0)
67 end_flow_id = int(sys.argv[2], 0)
68 if begin_flow_id > end_flow_id:
69 log_error(usage_msg)
70 exit(1)
71
72 #
73 # Do the work
74 #
75 # NOTE: Currently, up to 65536 flows are supported.
76 # More flows can be supported by iterating by, say, iterating over some of
77 # the other bytes of the autogenereated source/destination MAC addresses.
78 #
79 flow_id = begin_flow_id
80 idx = 0
81 while flow_id <= end_flow_id:
82 mac3 = idx / 255
83 mac4 = idx % 255
84 str_mac3 = "%0.2x" % mac3
85 str_mac4 = "%0.2x" % mac4
86 src_mac = "00:00:" + str_mac3 + ":" + str_mac4 + ":00:00";
87 dst_mac = "00:01:" + str_mac3 + ":" + str_mac4 + ":00:00";
88 print "%s FOOBAR 00:00:00:00:00:00:00:01 1 00:00:00:00:00:00:00:01 2 matchSrcMac %s matchDstMac %s" % (flow_id, src_mac, dst_mac)
89 flow_id = flow_id + 1
90 idx = idx + 1