2 # Copyright (c) 2013, 2014 Nicira, Inc.
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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
25 OVS_SRC = HOME + "/ovs"
27 BUILD_GCC = OVS_SRC + "/_build-gcc"
28 BUILD_CLANG = OVS_SRC + "/_build-clang"
29 PATH = "%(ovs)s/utilities:%(ovs)s/ovsdb:%(ovs)s/vswitchd" % {"ovs": BUILD_GCC}
31 ENV["PATH"] = PATH + ":" + ENV["PATH"]
38 def _sh(*args, **kwargs):
39 print "------> " + " ".join(args)
40 shell = len(args) == 1
41 if kwargs.get("capture", False):
42 proc = subprocess.Popen(args, stdout=subprocess.PIPE, shell=shell)
43 return proc.stdout.readlines()
44 elif kwargs.get("check", True):
45 subprocess.check_call(args, shell=shell)
47 subprocess.call(args, shell=shell)
51 return _sh("uname", "-r", capture=True)[0].strip()
58 os.remove(OVS_SRC + "/Makefile")
62 configure = ["../configure", "--prefix=" + ROOT, "--localstatedir=" + ROOT,
63 "--with-logdir=%s/log" % ROOT, "--with-rundir=%s/run" % ROOT,
64 "--enable-silent-rules", "--with-dbdir=" + ROOT, "--silent"]
66 cflags = "-g -fno-omit-frame-pointer"
69 configure.append("--enable-Werror")
71 if options.cache_time:
72 configure.append("--enable-cache-time")
75 configure.append("--mandir=" + options.mandir)
78 configure.append("--with-dpdk=" + options.with_dpdk)
79 cflags += " -Wno-cast-align -Wno-bad-function-cast" # DPDK warnings.
81 if options.optimize is None:
84 cflags += " -O%d" % options.optimize
86 ENV["CFLAGS"] = cflags
93 pass # Directory exists.
96 _sh(*(configure + ["--with-linux=/lib/modules/%s/build" % uname()]))
99 _sh("clang --version", check=True)
101 except subprocess.CalledProcessError:
105 _sh("sparse --version", check=True)
107 except subprocess.CalledProcessError:
112 os.mkdir(BUILD_CLANG)
114 pass # Directory exists.
117 os.chdir(BUILD_CLANG)
127 make_str = "\t$(MAKE) -C %s $@\n"
129 mf = open(OVS_SRC + "/Makefile", "w")
130 mf.write("all:\n%:\n")
132 mf.write(make_str % BUILD_CLANG)
133 mf.write("\t$(MAKE) -C %s %s $@\n" % (BUILD_GCC, c1))
134 mf.write("\ncheck:\n")
135 mf.write(make_str % BUILD_GCC)
137 commands.append(conf)
141 make = "make -s -j 8 " + args
143 commands.append(make)
148 commands.append(check)
152 ctags = ['ctags', '-R', '-f', '.tags']
155 _sh(*(ctags + ['--exclude="datapath/"']))
158 _sh(*ctags) # Some versions of ctags don't have --exclude
163 _sh('cscope', '-R', '-b')
170 for proc in ["ovs-vswitchd", "ovsdb-server"]:
171 if os.path.exists("%s/run/openvswitch/%s.pid" % (ROOT, proc)):
172 _sh("ovs-appctl", "-t", proc, "exit", check=False)
174 _sh("sudo", "killall", "-q", "-2", proc, check=False)
175 commands.append(kill)
180 if os.path.exists(ROOT):
182 for dp in _sh("ovs-dpctl dump-dps", capture=True):
183 _sh("ovs-dpctl", "del-dp", dp.strip())
184 commands.append(reset)
189 for d in ["log", "run"]:
190 d = "%s/%s" % (ROOT, d)
191 shutil.rmtree(d, ignore_errors=True)
194 pki_dir = ROOT + "/pki"
195 if not os.path.exists(pki_dir):
199 _sh("ovs-pki req+sign ovsclient")
202 if not os.path.exists(ROOT + "/conf.db"):
203 _sh("ovsdb-tool", "create", ROOT + "/conf.db",
204 OVS_SRC + "/vswitchd/vswitch.ovsschema")
206 opts = ["--pidfile", "--log-file", "--enable-dummy"]
208 _sh(*(["ovsdb-server",
209 "--remote=punix:%s/run/db.sock" % ROOT,
210 "--remote=db:Open_vSwitch,Open_vSwitch,manager_options",
211 "--private-key=db:Open_vSwitch,SSL,private_key",
212 "--certificate=db:Open_vSwitch,SSL,certificate",
213 "--bootstrap-ca-cert=db:Open_vSwitch,SSL,ca_cert",
214 "--detach", "-vconsole:off"] + opts))
216 _sh("ovs-vsctl --no-wait --bootstrap set-ssl %s/ovsclient-privkey.pem" \
217 " %s/ovsclient-cert.pem %s/vswitchd.cacert"
218 % (pki_dir, pki_dir, pki_dir))
219 version = _sh("ovs-vsctl --no-wait --version", capture=True)
220 version = version[0].strip().split()[3]
221 root_uuid = _sh("ovs-vsctl --no-wait --bare list Open_vSwitch",
222 capture=True)[0].strip()
223 _sh("ovs-vsctl --no-wait set Open_vSwitch %s ovs_version=%s"
224 % (root_uuid, version))
226 cmd = [BUILD_GCC + "/vswitchd/ovs-vswitchd"]
228 cmd = ["gdb", "--args"] + cmd
229 elif options.valgrind:
230 cmd = ["valgrind", "--track-origins=yes", "--leak-check=full",
231 "--suppressions=%s/tests/glibc.supp" % OVS_SRC,
232 "--suppressions=%s/tests/openssl.supp" % OVS_SRC] + cmd
235 opts = opts + ["-vconsole:off", "--detach"]
241 if not os.path.exists("/lib/modules"):
242 print "Missing modules directory. Is this a Linux system?"
246 _sh("rmmod", "openvswitch")
247 except subprocess.CalledProcessError, e:
248 pass # Module isn't loaded
251 _sh("rm /lib/modules/%s/extra/openvswitch.ko" % uname())
252 except subprocess.CalledProcessError, e:
253 pass # Module isn't installed
257 make("modules_install")
259 _sh("modprobe", "openvswitch")
260 _sh("dmesg | grep openvswitch | tail -1")
261 commands.append(modinst)
265 print "export PATH=" + ENV["PATH"]
273 This program is designed to help developers build and run Open vSwitch without
274 necessarily needing to know the gory details. Given some basic requirements
275 (described below), it can be used to build and run Open vSwitch, keeping
276 runtime files in the user's home directory.
279 # This section can be run as a script on ubuntu systems.
281 # First install the basic requirements needed to build Open vSwitch.
282 sudo apt-get install git build-essential libtool autoconf pkg-config \\
283 libssl-dev gdb linux-headers-`uname -r`
285 # Next clone the Open vSwitch source.
286 git clone https://github.com/openvswitch/ovs.git %(ovs)s
288 # Setup environment variables.
294 # Install the kernel module
295 sudo insmod %(ovs)s/datapath/linux/openvswitch.ko
301 conf - Configure the ovs source.
302 make - Build the source (must have been configured).
303 check - Run the unit tests.
304 tag - Run ctags and cscope over the source.
305 kill - Kill all running instances of ovs.
306 reset - Reset any runtime configuration in %(run)s.
308 modinst - Build ovs and install the kernel module.
309 env - Print the required path environment variable.
310 doc - Print this message.
311 """ % {"ovs": OVS_SRC, "v": sys.argv[0], "run": ROOT}
320 description = "Open vSwitch developer configuration. Try `%prog doc`."
321 cmd_names = [c.__name__ for c in commands]
322 parser = optparse.OptionParser(usage="usage: %prog"
323 + " [options] [%s] ..."
324 % "|".join(cmd_names),
325 description=description)
327 group = optparse.OptionGroup(parser, "conf")
328 group.add_option("--disable-Werror", dest="werror", action="store_false",
329 default=True, help="compile without the Werror flag")
330 group.add_option("--cache-time", dest="cache_time",
331 action="store_true", help="configure with cached timing")
332 group.add_option("--mandir", dest="mandir", metavar="MANDIR",
333 help="configure the man documentation install directory")
334 group.add_option("--with-dpdk", dest="with_dpdk", metavar="DPDK_BUILD",
335 help="built with dpdk libraries located at DPDK_BUILD");
338 group.add_option("--O%d" % i, dest="optimize", action="store_const",
339 const=i, help="compile with -O%d" % i)
340 parser.add_option_group(group)
342 group = optparse.OptionGroup(parser, "run")
343 group.add_option("-g", "--gdb", dest="gdb", action="store_true",
344 help="run ovs-vswitchd under gdb")
345 group.add_option("--valgrind", dest="valgrind", action="store_true",
346 help="run ovs-vswitchd under valgrind")
347 parser.add_option_group(group)
349 options, args = parser.parse_args()
352 if arg not in cmd_names:
353 print "Unknown argument " + arg
359 print "Missing %s." % OVS_SRC
364 if arg == cmd.__name__:
368 if __name__ == '__main__':