Dan Talayco | cc047ac | 2010-04-16 09:13:08 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Create veth pairs and start up switch daemons |
| 4 | # |
| 5 | |
| 6 | import os |
| 7 | from subprocess import Popen,PIPE,call,check_call |
| 8 | from optparse import OptionParser |
| 9 | |
| 10 | parser = OptionParser(version="%prog 0.1") |
| 11 | parser.set_defaults(port_count=4) |
| 12 | parser.set_defaults(of_dir="../../openflow") |
| 13 | parser.set_defaults(port=6633) |
| 14 | parser.add_option("-n", "--port_count", type="int", |
| 15 | help="Number of veth pairs to create") |
| 16 | parser.add_option("-o", "--of_dir", help="OpenFlow root directory for host") |
| 17 | parser.add_option("-p", "--port", type="int", |
| 18 | help="Port for OFP to listen on") |
| 19 | (options, args) = parser.parse_args() |
| 20 | |
| 21 | call(["/sbin/modprobe", "veth"]) |
| 22 | for idx in range(0, options.port_count): |
| 23 | print "Creating veth pair " + str(idx) |
| 24 | call(["/sbin/ip", "link", "add", "type", "veth"]) |
| 25 | |
| 26 | for idx in range(0, 2 * options.port_count): |
| 27 | cmd = ["/sbin/ifconfig", |
| 28 | "veth" + str(idx), |
| 29 | "192.168.1" + str(idx) + ".1", |
| 30 | "netmask", |
| 31 | "255.255.255.0"] |
| 32 | print "Cmd: " + str(cmd) |
| 33 | call(cmd) |
| 34 | |
| 35 | veths = "veth0" |
| 36 | for idx in range(1, options.port_count): |
| 37 | veths += ",veth" + str(2 * idx) |
| 38 | |
| 39 | ofd = options.of_dir + "/udatapath/ofdatapath" |
| 40 | ofp = options.of_dir + "/secchan/ofprotocol" |
| 41 | |
| 42 | try: |
| 43 | check_call(["ls", ofd]) |
| 44 | except: |
| 45 | print "Could not find datapath daemon: " + ofd |
| 46 | os.exit(1) |
| 47 | |
| 48 | try: |
| 49 | check_call(["ls", ofp]) |
| 50 | except: |
| 51 | print "Could not find protocol daemon: " + ofp |
| 52 | os.exit(1) |
| 53 | |
| 54 | ofd_op = Popen([ofd, "-i", veths, "punix:/tmp/ofd"]) |
| 55 | print "Started ofdatapath on IFs " + veths + " with pid " + str(ofd_op.pid) |
| 56 | |
| 57 | print "Starting ofprotocol; ^C to quit" |
| 58 | call([ofp, "unix:/tmp/ofd", "tcp:127.0.0.1:" + str(options.port), |
| 59 | "--fail=closed", "--max-backoff=1"]) |
| 60 | |
| 61 | ofd_op.kill() |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |