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) |
Rich Lane | b7611cd | 2012-10-25 13:37:31 -0700 | [diff] [blame] | 13 | parser.set_defaults(of_dir="../openflow") |
Rich Lane | 4d1f3eb | 2013-10-03 13:45:57 -0700 | [diff] [blame^] | 14 | parser.set_defaults(port=6653) |
Dan Talayco | cc047ac | 2010-04-16 09:13:08 -0700 | [diff] [blame] | 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) |
Rich Lane | f57e359 | 2012-10-25 13:42:05 -0700 | [diff] [blame] | 27 | veth = "veth%d" % (idx*2) |
| 28 | veth_peer = "veth%d" % (idx*2+1) |
| 29 | call(["/sbin/ip", "link", "add", "name", veth, "type", "veth", |
| 30 | "peer", "name", veth_peer]) |
Dan Talayco | cc047ac | 2010-04-16 09:13:08 -0700 | [diff] [blame] | 31 | |
| 32 | for idx in range(0, 2 * options.port_count): |
| 33 | cmd = ["/sbin/ifconfig", |
| 34 | "veth" + str(idx), |
| 35 | "192.168.1" + str(idx) + ".1", |
| 36 | "netmask", |
| 37 | "255.255.255.0"] |
| 38 | print "Cmd: " + str(cmd) |
| 39 | call(cmd) |
| 40 | |
| 41 | veths = "veth0" |
| 42 | for idx in range(1, options.port_count): |
| 43 | veths += ",veth" + str(2 * idx) |
| 44 | |
| 45 | ofd = options.of_dir + "/udatapath/ofdatapath" |
| 46 | ofp = options.of_dir + "/secchan/ofprotocol" |
| 47 | |
| 48 | try: |
| 49 | check_call(["ls", ofd]) |
| 50 | except: |
| 51 | print "Could not find datapath daemon: " + ofd |
Rich Lane | ce53f4a | 2012-10-25 13:44:39 -0700 | [diff] [blame] | 52 | sys.exit(1) |
Dan Talayco | cc047ac | 2010-04-16 09:13:08 -0700 | [diff] [blame] | 53 | |
| 54 | try: |
| 55 | check_call(["ls", ofp]) |
| 56 | except: |
| 57 | print "Could not find protocol daemon: " + ofp |
Rich Lane | ce53f4a | 2012-10-25 13:44:39 -0700 | [diff] [blame] | 58 | sys.exit(1) |
Dan Talayco | cc047ac | 2010-04-16 09:13:08 -0700 | [diff] [blame] | 59 | |
Dan Talayco | d344c90 | 2010-05-04 13:02:11 -0700 | [diff] [blame] | 60 | if not options.no_wait: |
| 61 | print "Starting ofprotocol in 2 seconds; ^C to quit" |
| 62 | time.sleep(2) |
| 63 | else: |
| 64 | print "Starting ofprotocol; ^C to quit" |
| 65 | |
Dan Talayco | cc047ac | 2010-04-16 09:13:08 -0700 | [diff] [blame] | 66 | ofd_op = Popen([ofd, "-i", veths, "punix:/tmp/ofd"]) |
| 67 | print "Started ofdatapath on IFs " + veths + " with pid " + str(ofd_op.pid) |
| 68 | |
Dan Talayco | cc047ac | 2010-04-16 09:13:08 -0700 | [diff] [blame] | 69 | call([ofp, "unix:/tmp/ofd", "tcp:127.0.0.1:" + str(options.port), |
| 70 | "--fail=closed", "--max-backoff=1"]) |
| 71 | |
| 72 | ofd_op.kill() |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |