Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python2.5 |
| 2 | """This script fakes as an OpenFlow switch to the controller |
| 3 | |
| 4 | (C) Copyright Stanford University |
| 5 | Author ykk |
| 6 | Date October 2009 |
| 7 | """ |
| 8 | import sys |
| 9 | import getopt |
| 10 | import openflow |
| 11 | import time |
| 12 | import output |
| 13 | import of.msg |
| 14 | import of.simu |
| 15 | |
| 16 | def usage(): |
| 17 | """Display usage |
| 18 | """ |
| 19 | print "Usage "+sys.argv[0]+" <options> controller\n"+\ |
| 20 | "Options:\n"+\ |
| 21 | "-p/--port\n\tSpecify port number\n"+\ |
| 22 | "-v/--verbose\n\tPrint message exchange\n"+\ |
| 23 | "-h/--help\n\tPrint this usage guide\n"+\ |
| 24 | "" |
| 25 | |
| 26 | #Parse options and arguments |
| 27 | try: |
| 28 | opts, args = getopt.getopt(sys.argv[1:], "hvp:", |
| 29 | ["help","verbose","port="]) |
| 30 | except getopt.GetoptError: |
| 31 | usage() |
| 32 | sys.exit(2) |
| 33 | |
| 34 | #Check there is only controller |
| 35 | if not (len(args) == 1): |
| 36 | usage() |
| 37 | sys.exit(2) |
| 38 | |
| 39 | #Parse options |
| 40 | ##Port to connect to |
| 41 | port = 6633 |
| 42 | ##Set output mode |
| 43 | output.set_mode("INFO") |
| 44 | for opt,arg in opts: |
| 45 | if (opt in ("-h","--help")): |
| 46 | usage() |
| 47 | sys.exit(0) |
| 48 | elif (opt in ("-v","--verbose")): |
| 49 | output.set_mode("DBG") |
| 50 | elif (opt in ("-p","--port")): |
| 51 | port=int(arg) |
| 52 | else: |
| 53 | assert (False,"Unhandled option :"+opt) |
| 54 | |
| 55 | #Connect to controller |
| 56 | ofmsg = openflow.messages() |
| 57 | parser = of.msg.parser(ofmsg) |
| 58 | ofsw = of.simu.switch(ofmsg, args[0], port, |
| 59 | dpid=int("0xcafecafe",16), |
| 60 | parser=parser) |
| 61 | ofsw.send_hello() |
| 62 | #Send echo and wait |
| 63 | xid = 22092009 |
| 64 | running = True |
| 65 | ofsw.send_echo(xid) |
| 66 | starttime = time.time() |
| 67 | while running: |
| 68 | msg = ofsw.connection.msgreceive(True, 0.00001) |
| 69 | pkttime = time.time() |
| 70 | dic = ofmsg.peek_from_front("ofp_header", msg) |
| 71 | if (dic["type"][0] == ofmsg.get_value("OFPT_ECHO_REPLY") and |
| 72 | dic["xid"][0] == xid): |
| 73 | #Check reply for echo request |
| 74 | output.info("Received echo reply after "+\ |
| 75 | str((pkttime-starttime)*1000)+" ms", "ping-controller") |
| 76 | running = False |
| 77 | else: |
| 78 | ofsw.receive_openflow(msg) |