blob: 715a73a3275523fc9363d3512fb5f7fd446bc3a4 [file] [log] [blame]
Dan Talaycod2ca1032010-03-10 14:40:26 -08001#!/usr/bin/env python
Dan Talaycof75360a2010-02-05 22:22:54 -08002"""This script fakes as n OpenFlow switch and
3load the controller with k packets per second.
4
5(C) Copyright Stanford University
6Author ykk
7Date January 2010
8"""
9import sys
10import getopt
11import struct
12import openflow
13import time
14import output
15import of.msg
16import of.simu
17import of.network
18import dpkt.ethernet
19
20def usage():
21 """Display usage
22 """
23 print "Usage "+sys.argv[0]+" <options> controller\n"+\
24 "Options:\n"+\
25 "-p/--port\n\tSpecify port number\n"+\
26 "-v/--verbose\n\tPrint message exchange\n"+\
27 "-r/--rate\n\tSpecify rate per switch to send packets (default=1)\n"+\
28 "-d/--duration\n\tSpecify duration of load test in seconds (default=5)\n"+\
29 "-s/--switch\n\tSpecify number of switches (default=1)\n"+\
30 "-h/--help\n\tPrint this usage guide\n"+\
31 ""
32
33#Parse options and arguments
34try:
35 opts, args = getopt.getopt(sys.argv[1:], "hvp:s:d:r:",
36 ["help","verbose","port=",
37 "switch=","duration=","rate="])
38except getopt.GetoptError:
39 usage()
40 sys.exit(2)
41
42#Check there is only controller
43if not (len(args) == 1):
44 usage()
45 sys.exit(2)
46
47#Parse options
48##Duration
49duration = 5
50##Rate
51rate = 1.0
52##Switch number
53swno = 1
54##Port to connect to
55port = 6633
56##Set output mode
57output.set_mode("INFO")
58for opt,arg in opts:
59 if (opt in ("-h","--help")):
60 usage()
61 sys.exit(0)
62 elif (opt in ("-v","--verbose")):
63 output.set_mode("DBG")
64 elif (opt in ("-p","--port")):
65 port=int(arg)
66 elif (opt in ("-s","--switch")):
67 swno=int(arg)
68 elif (opt in ("-d","--duration")):
69 duration=int(arg)
70 elif (opt in ("-r","--rate")):
71 rate=float(arg)
72 else:
73 print "Unhandled option :"+opt
74 sys.exit(2)
75
76#Form packet
77pkt = dpkt.ethernet.Ethernet()
78pkt.type = dpkt.ethernet.ETH_MIN
79pkt.dst = '\xFF\xFF\xFF\xFF\xFF\xFF'
80
81#Connect to controller
82ofmsg = openflow.messages()
83parser = of.msg.parser(ofmsg)
84ofnet = of.simu.network()
85for i in range(1,swno+1):
86 ofsw = of.simu.switch(ofmsg, args[0], port,
87 dpid=i,
88 parser=parser)
89 ofnet.add_switch(ofsw)
90 ofsw.send_hello()
91
92output.info("Running "+str(swno)+" switches at "+str(rate)+\
93 " packets per seconds for "+str(duration)+" s")
94
95starttime = time.time()
96running = True
97interval = 1.0/(rate*swno)
98ntime=time.time()+(interval/10.0)
99swindex = 0
100pcount = 0
101rcount = 0
102while running:
103 ctime = time.time()
104 time.sleep(max(0,min(ntime-ctime,interval/10.0)))
105
106 if ((ctime-starttime) <= duration):
107 #Send packet if time's up
108 if (ctime >= ntime):
109 ntime += interval
110 pkt.src = struct.pack("Q",pcount)[:6]
111 ofnet.switches[swindex].send_packet(1,10,pkt.pack()+'A'*3)
112 pcount += 1
113 swno += 1
114 if (swno >= len(ofnet.switches)):
115 swno=0
116
117 #Process any received message
118 (ofsw, msg) = ofnet.connections.msgreceive()
119 while (msg != None):
120 dic = ofmsg.peek_from_front("ofp_header", msg)
121 if (dic["type"][0] == ofmsg.get_value("OFPT_FLOW_MOD")):
122 output.dbg("Received flow mod")
123 rcount += 1
124 ofsw.receive_openflow(msg)
125 (ofsw, msg) = ofnet.connections.msgreceive()
126 else:
127 running = False
128
129output.info("Sent "+str(pcount)+" packets at rate "+\
130 str(float(pcount)/float(duration))+" and received "+\
131 str(rcount)+" back")