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)
149 flags += "-j%d " % options.jobs
153 for arg in str.split(options.tests):
157 flags += "-k %s " % arg
158 ENV["TESTSUITEFLAGS"] = flags
160 commands.append(check)
164 ctags = ['ctags', '-R', '-f', '.tags']
167 _sh(*(ctags + ['--exclude="datapath/"']))
170 _sh(*ctags) # Some versions of ctags don't have --exclude
175 _sh('cscope', '-R', '-b')
182 for proc in ["ovs-vswitchd", "ovsdb-server"]:
183 if os.path.exists("%s/run/openvswitch/%s.pid" % (ROOT, proc)):
184 _sh("ovs-appctl", "-t", proc, "exit", check=False)
186 _sh("sudo", "killall", "-q", "-2", proc, check=False)
187 commands.append(kill)
192 if os.path.exists(ROOT):
194 for dp in _sh("ovs-dpctl dump-dps", capture=True):
195 _sh("ovs-dpctl", "del-dp", dp.strip())
196 commands.append(reset)
201 for d in ["log", "run"]:
202 d = "%s/%s" % (ROOT, d)
203 shutil.rmtree(d, ignore_errors=True)
206 pki_dir = ROOT + "/pki"
207 if not os.path.exists(pki_dir):
211 _sh("ovs-pki req+sign ovsclient")
214 if not os.path.exists(ROOT + "/conf.db"):
215 _sh("ovsdb-tool", "create", ROOT + "/conf.db",
216 OVS_SRC + "/vswitchd/vswitch.ovsschema")
218 opts = ["--pidfile", "--log-file", "--enable-dummy"]
220 _sh(*(["ovsdb-server",
221 "--remote=punix:%s/run/db.sock" % ROOT,
222 "--remote=db:Open_vSwitch,Open_vSwitch,manager_options",
223 "--private-key=db:Open_vSwitch,SSL,private_key",
224 "--certificate=db:Open_vSwitch,SSL,certificate",
225 "--bootstrap-ca-cert=db:Open_vSwitch,SSL,ca_cert",
226 "--detach", "-vconsole:off"] + opts))
228 _sh("ovs-vsctl --no-wait --bootstrap set-ssl %s/ovsclient-privkey.pem" \
229 " %s/ovsclient-cert.pem %s/vswitchd.cacert"
230 % (pki_dir, pki_dir, pki_dir))
231 version = _sh("ovs-vsctl --no-wait --version", capture=True)
232 version = version[0].strip().split()[3]
233 root_uuid = _sh("ovs-vsctl --no-wait --bare list Open_vSwitch",
234 capture=True)[0].strip()
235 _sh("ovs-vsctl --no-wait set Open_vSwitch %s ovs_version=%s"
236 % (root_uuid, version))
238 cmd = [BUILD_GCC + "/vswitchd/ovs-vswitchd"]
242 cmd.extend(options.dpdk)
246 cmd = ["gdb", "--args"] + cmd
247 elif options.valgrind:
248 cmd = ["valgrind", "--track-origins=yes", "--leak-check=full",
249 "--suppressions=%s/tests/glibc.supp" % OVS_SRC,
250 "--suppressions=%s/tests/openssl.supp" % OVS_SRC] + cmd
253 opts = opts + ["-vconsole:off", "--detach"]
259 if not os.path.exists("/lib/modules"):
260 print "Missing modules directory. Is this a Linux system?"
264 _sh("rmmod", "openvswitch")
265 except subprocess.CalledProcessError, e:
266 pass # Module isn't loaded
269 _sh("rm /lib/modules/%s/extra/openvswitch.ko" % uname())
270 except subprocess.CalledProcessError, e:
271 pass # Module isn't installed
275 make("modules_install")
277 _sh("modprobe", "openvswitch")
278 _sh("dmesg | grep openvswitch | tail -1")
279 commands.append(modinst)
283 print "export PATH=" + ENV["PATH"]
291 This program is designed to help developers build and run Open vSwitch without
292 necessarily needing to know the gory details. Given some basic requirements
293 (described below), it can be used to build and run Open vSwitch, keeping
294 runtime files in the user's home directory.
297 # This section can be run as a script on ubuntu systems.
299 # First install the basic requirements needed to build Open vSwitch.
300 sudo apt-get install git build-essential libtool autoconf pkg-config \\
301 libssl-dev gdb linux-headers-`uname -r`
303 # Next clone the Open vSwitch source.
304 git clone https://github.com/openvswitch/ovs.git %(ovs)s
306 # Setup environment variables.
312 # Install the kernel module
313 sudo insmod %(ovs)s/datapath/linux/openvswitch.ko
319 conf - Configure the ovs source.
320 make - Build the source (must have been configured).
321 check - Run the unit tests.
322 tag - Run ctags and cscope over the source.
323 kill - Kill all running instances of ovs.
324 reset - Reset any runtime configuration in %(run)s.
326 modinst - Build ovs and install the kernel module.
327 env - Print the required path environment variable.
328 doc - Print this message.
329 """ % {"ovs": OVS_SRC, "v": sys.argv[0], "run": ROOT}
333 def parse_subargs(option, opt_str, value, parser):
337 dpdkarg = parser.rargs.pop(0)
340 subopts.append(dpdkarg)
342 setattr(parser.values, option.dest, subopts)
348 description = "Open vSwitch developer configuration. Try `%prog doc`."
349 cmd_names = [c.__name__ for c in commands]
350 parser = optparse.OptionParser(usage="usage: %prog"
351 + " [options] [%s] ..."
352 % "|".join(cmd_names),
353 description=description)
355 group = optparse.OptionGroup(parser, "conf")
356 group.add_option("--disable-Werror", dest="werror", action="store_false",
357 default=True, help="compile without the Werror flag")
358 group.add_option("--cache-time", dest="cache_time",
359 action="store_true", help="configure with cached timing")
360 group.add_option("--mandir", dest="mandir", metavar="MANDIR",
361 help="configure the man documentation install directory")
362 group.add_option("--with-dpdk", dest="with_dpdk", metavar="DPDK_BUILD",
363 help="built with dpdk libraries located at DPDK_BUILD");
366 group.add_option("--O%d" % i, dest="optimize", action="store_const",
367 const=i, help="compile with -O%d" % i)
368 parser.add_option_group(group)
370 group = optparse.OptionGroup(parser, "check")
371 group.add_option("-j", "--jobs", dest="jobs", metavar="N", type="int",
372 help="Run N tests in parallel")
373 group.add_option("--tests", dest="tests", metavar="FILTER",
374 help="""run specific tests and/or a test category
375 eg, --tests=\"1-10 megaflow\"""")
376 parser.add_option_group(group)
378 group = optparse.OptionGroup(parser, "run")
379 group.add_option("-g", "--gdb", dest="gdb", action="store_true",
380 help="run ovs-vswitchd under gdb")
381 group.add_option("--valgrind", dest="valgrind", action="store_true",
382 help="run ovs-vswitchd under valgrind")
383 group.add_option("--dpdk", dest="dpdk", action="callback",
384 callback=parse_subargs,
385 help="run ovs-vswitchd with dpdk subopts (ended by --)")
386 parser.add_option_group(group)
388 options, args = parser.parse_args()
391 if arg not in cmd_names:
392 print "Unknown argument " + arg
398 print "Missing %s." % OVS_SRC
403 if arg == cmd.__name__:
407 if __name__ == '__main__':