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