Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python2.5 |
| 2 | """This script fakes as n OpenFlow switch and |
| 3 | load the controller with k packets per second. |
| 4 | |
| 5 | (C) Copyright Stanford University |
| 6 | Author ykk |
| 7 | Date January 2010 |
| 8 | """ |
| 9 | import sys |
| 10 | import getopt |
| 11 | import struct |
| 12 | import openflow |
| 13 | import time |
| 14 | import output |
| 15 | import of.msg |
| 16 | import of.simu |
| 17 | import of.network |
| 18 | import dpkt.ethernet |
| 19 | |
| 20 | def 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 |
| 34 | try: |
| 35 | opts, args = getopt.getopt(sys.argv[1:], "hvp:s:d:r:", |
| 36 | ["help","verbose","port=", |
| 37 | "switch=","duration=","rate="]) |
| 38 | except getopt.GetoptError: |
| 39 | usage() |
| 40 | sys.exit(2) |
| 41 | |
| 42 | #Check there is only controller |
| 43 | if not (len(args) == 1): |
| 44 | usage() |
| 45 | sys.exit(2) |
| 46 | |
| 47 | #Parse options |
| 48 | ##Duration |
| 49 | duration = 5 |
| 50 | ##Rate |
| 51 | rate = 1.0 |
| 52 | ##Switch number |
| 53 | swno = 1 |
| 54 | ##Port to connect to |
| 55 | port = 6633 |
| 56 | ##Set output mode |
| 57 | output.set_mode("INFO") |
| 58 | for 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 |
| 77 | pkt = dpkt.ethernet.Ethernet() |
| 78 | pkt.type = dpkt.ethernet.ETH_MIN |
| 79 | pkt.dst = '\xFF\xFF\xFF\xFF\xFF\xFF' |
| 80 | |
| 81 | #Connect to controller |
| 82 | ofmsg = openflow.messages() |
| 83 | parser = of.msg.parser(ofmsg) |
| 84 | ofnet = of.simu.network() |
| 85 | for 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 | |
| 92 | output.info("Running "+str(swno)+" switches at "+str(rate)+\ |
| 93 | " packets per seconds for "+str(duration)+" s") |
| 94 | |
| 95 | starttime = time.time() |
| 96 | running = True |
| 97 | interval = 1.0/(rate*swno) |
| 98 | ntime=time.time()+(interval/10.0) |
| 99 | swindex = 0 |
| 100 | pcount = 0 |
| 101 | rcount = 0 |
| 102 | while 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 | |
| 129 | output.info("Sent "+str(pcount)+" packets at rate "+\ |
| 130 | str(float(pcount)/float(duration))+" and received "+\ |
| 131 | str(rcount)+" back") |