blob: e5aa0307b464fe052924f5ada2d7920fa5027ce0 [file] [log] [blame]
Dan Talaycof75360a2010-02-05 22:22:54 -08001#!/usr/bin/env python2.5
2"""This script fakes as an OpenFlow switch to the controller
3
4(C) Copyright Stanford University
5Author ykk
6Date October 2009
7"""
8import sys
9import getopt
10import openflow
11import time
12import output
13import of.msg
14import of.simu
15
16def 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
27try:
28 opts, args = getopt.getopt(sys.argv[1:], "hvp:",
29 ["help","verbose","port="])
30except getopt.GetoptError:
31 usage()
32 sys.exit(2)
33
34#Check there is only controller
35if not (len(args) == 1):
36 usage()
37 sys.exit(2)
38
39#Parse options
40##Port to connect to
41port = 6633
42##Set output mode
43output.set_mode("INFO")
44for 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
56ofmsg = openflow.messages()
57parser = of.msg.parser(ofmsg)
58ofsw = of.simu.switch(ofmsg, args[0], port,
59 dpid=int("0xcafecafe",16),
60 parser=parser)
61ofsw.send_hello()
62#Send echo and wait
63xid = 22092009
64running = True
65ofsw.send_echo(xid)
66starttime = time.time()
67while 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)