blob: e9f13b511b88ea81d9b3cab7afb025c75739937e [file] [log] [blame]
Matteo Scandoloa229eca2017-08-08 13:05:28 -07001
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 Talaycocc047ac2010-04-16 09:13:08 -070017#!/usr/bin/env python
18#
19# Create veth pairs and start up switch daemons
20#
21
22import os
Dan Talaycod344c902010-05-04 13:02:11 -070023import time
Dan Talaycocc047ac2010-04-16 09:13:08 -070024from subprocess import Popen,PIPE,call,check_call
25from optparse import OptionParser
26
27parser = OptionParser(version="%prog 0.1")
28parser.set_defaults(port_count=4)
Rich Laneb7611cd2012-10-25 13:37:31 -070029parser.set_defaults(of_dir="../openflow")
Rich Lane4d1f3eb2013-10-03 13:45:57 -070030parser.set_defaults(port=6653)
Dan Talaycocc047ac2010-04-16 09:13:08 -070031parser.add_option("-n", "--port_count", type="int",
32 help="Number of veth pairs to create")
33parser.add_option("-o", "--of_dir", help="OpenFlow root directory for host")
34parser.add_option("-p", "--port", type="int",
35 help="Port for OFP to listen on")
Dan Talaycod344c902010-05-04 13:02:11 -070036parser.add_option("-N", "--no_wait", action="store_true",
37 help="Do not wait 2 seconds to start daemons")
Dan Talaycocc047ac2010-04-16 09:13:08 -070038(options, args) = parser.parse_args()
39
40call(["/sbin/modprobe", "veth"])
41for idx in range(0, options.port_count):
42 print "Creating veth pair " + str(idx)
Rich Lanef57e3592012-10-25 13:42:05 -070043 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 Talaycocc047ac2010-04-16 09:13:08 -070047
48for 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
57veths = "veth0"
58for idx in range(1, options.port_count):
59 veths += ",veth" + str(2 * idx)
60
61ofd = options.of_dir + "/udatapath/ofdatapath"
62ofp = options.of_dir + "/secchan/ofprotocol"
63
64try:
65 check_call(["ls", ofd])
66except:
67 print "Could not find datapath daemon: " + ofd
Rich Lanece53f4a2012-10-25 13:44:39 -070068 sys.exit(1)
Dan Talaycocc047ac2010-04-16 09:13:08 -070069
70try:
71 check_call(["ls", ofp])
72except:
73 print "Could not find protocol daemon: " + ofp
Rich Lanece53f4a2012-10-25 13:44:39 -070074 sys.exit(1)
Dan Talaycocc047ac2010-04-16 09:13:08 -070075
Dan Talaycod344c902010-05-04 13:02:11 -070076if not options.no_wait:
77 print "Starting ofprotocol in 2 seconds; ^C to quit"
78 time.sleep(2)
79else:
80 print "Starting ofprotocol; ^C to quit"
81
Dan Talaycocc047ac2010-04-16 09:13:08 -070082ofd_op = Popen([ofd, "-i", veths, "punix:/tmp/ofd"])
83print "Started ofdatapath on IFs " + veths + " with pid " + str(ofd_op.pid)
84
Dan Talaycocc047ac2010-04-16 09:13:08 -070085call([ofp, "unix:/tmp/ofd", "tcp:127.0.0.1:" + str(options.port),
86 "--fail=closed", "--max-backoff=1"])
87
88ofd_op.kill()
89
90
91
92
93