Matteo Scandolo | a229eca | 2017-08-08 13:05:28 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Dan Talayco | cc047ac | 2010-04-16 09:13:08 -0700 | [diff] [blame] | 17 | #!/usr/bin/env python |
| 18 | # |
| 19 | # Create veth pairs and start up switch daemons |
| 20 | # |
| 21 | |
| 22 | import os |
Dan Talayco | d344c90 | 2010-05-04 13:02:11 -0700 | [diff] [blame] | 23 | import time |
Dan Talayco | cc047ac | 2010-04-16 09:13:08 -0700 | [diff] [blame] | 24 | from subprocess import Popen,PIPE,call,check_call |
| 25 | from optparse import OptionParser |
| 26 | |
| 27 | parser = OptionParser(version="%prog 0.1") |
| 28 | parser.set_defaults(port_count=4) |
Rich Lane | b7611cd | 2012-10-25 13:37:31 -0700 | [diff] [blame] | 29 | parser.set_defaults(of_dir="../openflow") |
Rich Lane | 4d1f3eb | 2013-10-03 13:45:57 -0700 | [diff] [blame] | 30 | parser.set_defaults(port=6653) |
Dan Talayco | cc047ac | 2010-04-16 09:13:08 -0700 | [diff] [blame] | 31 | parser.add_option("-n", "--port_count", type="int", |
| 32 | help="Number of veth pairs to create") |
| 33 | parser.add_option("-o", "--of_dir", help="OpenFlow root directory for host") |
| 34 | parser.add_option("-p", "--port", type="int", |
| 35 | help="Port for OFP to listen on") |
Dan Talayco | d344c90 | 2010-05-04 13:02:11 -0700 | [diff] [blame] | 36 | parser.add_option("-N", "--no_wait", action="store_true", |
| 37 | help="Do not wait 2 seconds to start daemons") |
Dan Talayco | cc047ac | 2010-04-16 09:13:08 -0700 | [diff] [blame] | 38 | (options, args) = parser.parse_args() |
| 39 | |
| 40 | call(["/sbin/modprobe", "veth"]) |
| 41 | for idx in range(0, options.port_count): |
| 42 | print "Creating veth pair " + str(idx) |
Rich Lane | f57e359 | 2012-10-25 13:42:05 -0700 | [diff] [blame] | 43 | veth = "veth%d" % (idx*2) |
| 44 | veth_peer = "veth%d" % (idx*2+1) |
| 45 | call(["/sbin/ip", "link", "add", "name", veth, "type", "veth", |
| 46 | "peer", "name", veth_peer]) |
Dan Talayco | cc047ac | 2010-04-16 09:13:08 -0700 | [diff] [blame] | 47 | |
| 48 | for idx in range(0, 2 * options.port_count): |
| 49 | cmd = ["/sbin/ifconfig", |
| 50 | "veth" + str(idx), |
| 51 | "192.168.1" + str(idx) + ".1", |
| 52 | "netmask", |
| 53 | "255.255.255.0"] |
| 54 | print "Cmd: " + str(cmd) |
| 55 | call(cmd) |
| 56 | |
| 57 | veths = "veth0" |
| 58 | for idx in range(1, options.port_count): |
| 59 | veths += ",veth" + str(2 * idx) |
| 60 | |
| 61 | ofd = options.of_dir + "/udatapath/ofdatapath" |
| 62 | ofp = options.of_dir + "/secchan/ofprotocol" |
| 63 | |
| 64 | try: |
| 65 | check_call(["ls", ofd]) |
| 66 | except: |
| 67 | print "Could not find datapath daemon: " + ofd |
Rich Lane | ce53f4a | 2012-10-25 13:44:39 -0700 | [diff] [blame] | 68 | sys.exit(1) |
Dan Talayco | cc047ac | 2010-04-16 09:13:08 -0700 | [diff] [blame] | 69 | |
| 70 | try: |
| 71 | check_call(["ls", ofp]) |
| 72 | except: |
| 73 | print "Could not find protocol daemon: " + ofp |
Rich Lane | ce53f4a | 2012-10-25 13:44:39 -0700 | [diff] [blame] | 74 | sys.exit(1) |
Dan Talayco | cc047ac | 2010-04-16 09:13:08 -0700 | [diff] [blame] | 75 | |
Dan Talayco | d344c90 | 2010-05-04 13:02:11 -0700 | [diff] [blame] | 76 | if not options.no_wait: |
| 77 | print "Starting ofprotocol in 2 seconds; ^C to quit" |
| 78 | time.sleep(2) |
| 79 | else: |
| 80 | print "Starting ofprotocol; ^C to quit" |
| 81 | |
Dan Talayco | cc047ac | 2010-04-16 09:13:08 -0700 | [diff] [blame] | 82 | ofd_op = Popen([ofd, "-i", veths, "punix:/tmp/ofd"]) |
| 83 | print "Started ofdatapath on IFs " + veths + " with pid " + str(ofd_op.pid) |
| 84 | |
Dan Talayco | cc047ac | 2010-04-16 09:13:08 -0700 | [diff] [blame] | 85 | call([ofp, "unix:/tmp/ofd", "tcp:127.0.0.1:" + str(options.port), |
| 86 | "--fail=closed", "--max-backoff=1"]) |
| 87 | |
| 88 | ofd_op.kill() |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |