2 # Copyright (c) 2013, 2014, 2015 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"
35 PATH = "%(ovs)s/utilities:%(ovs)s/ovsdb:%(ovs)s/vswitchd" % {"ovs": build}
37 ENV["PATH"] = PATH + ":" + ENV["PATH"]
39 def _sh(*args, **kwargs):
40 print "------> " + " ".join(args)
41 shell = len(args) == 1
42 if kwargs.get("capture", False):
43 proc = subprocess.Popen(args, stdout=subprocess.PIPE, shell=shell)
44 return proc.stdout.readlines()
45 elif kwargs.get("check", True):
46 subprocess.check_call(args, shell=shell)
48 subprocess.call(args, shell=shell)
52 return _sh("uname", "-r", capture=True)[0].strip()
59 os.remove(OVS_SRC + "/Makefile")
63 configure = ["../configure", "--prefix=" + ROOT, "--localstatedir=" + ROOT,
64 "--with-logdir=%s/log" % ROOT, "--with-rundir=%s/run" % ROOT,
65 "--enable-silent-rules", "--with-dbdir=" + ROOT, "--silent"]
67 cflags = "-g -fno-omit-frame-pointer"
70 configure.append("--enable-Werror")
72 if options.cache_time:
73 configure.append("--enable-cache-time")
76 configure.append("--mandir=" + options.mandir)
79 configure.append("--with-dpdk=" + options.with_dpdk)
80 cflags += " -Wno-cast-align -Wno-bad-function-cast" # DPDK warnings.
82 if options.optimize is None:
85 cflags += " -O%s" % str(options.optimize)
87 ENV["CFLAGS"] = cflags
94 pass # Directory exists.
97 _sh(*(configure + ["--with-linux=/lib/modules/%s/build" % uname()]))
100 _sh("clang --version", check=True)
102 except subprocess.CalledProcessError:
106 _sh("sparse --version", check=True)
108 except subprocess.CalledProcessError:
113 os.mkdir(BUILD_CLANG)
115 pass # Directory exists.
118 os.chdir(BUILD_CLANG)
128 make_str = "\t$(MAKE) -C %s $@\n"
130 mf = open(OVS_SRC + "/Makefile", "w")
131 mf.write("all:\n%:\n")
133 mf.write(make_str % BUILD_CLANG)
134 mf.write("\t$(MAKE) -C %s %s $@\n" % (BUILD_GCC, c1))
135 mf.write("\ncheck-valgrind:\n")
136 mf.write("\ncheck:\n")
137 mf.write(make_str % BUILD_GCC)
139 commands.append(conf)
143 make = "make -s -j 8 " + args
145 commands.append(make)
151 flags += "-j%d " % options.jobs
155 for arg in str.split(options.tests):
159 flags += "-k %s " % arg
160 ENV["TESTSUITEFLAGS"] = flags
162 commands.append(check)
166 ctags = ['ctags', '-R', '-f', '.tags']
169 _sh(*(ctags + ['--exclude="datapath/"']))
172 _sh(*ctags) # Some versions of ctags don't have --exclude
177 _sh('cscope', '-R', '-b')
184 for proc in ["ovs-vswitchd", "ovsdb-server"]:
185 if os.path.exists("%s/run/openvswitch/%s.pid" % (ROOT, proc)):
186 _sh("ovs-appctl", "-t", proc, "exit", check=False)
188 _sh("sudo", "killall", "-q", "-2", proc, check=False)
189 commands.append(kill)
194 if os.path.exists(ROOT):
196 for dp in _sh("ovs-dpctl dump-dps", capture=True):
197 _sh("ovs-dpctl", "del-dp", dp.strip())
198 commands.append(reset)
203 for d in ["log", "run"]:
204 d = "%s/%s" % (ROOT, d)
205 shutil.rmtree(d, ignore_errors=True)
208 pki_dir = ROOT + "/pki"
209 if not os.path.exists(pki_dir):
213 _sh("ovs-pki req+sign ovsclient")
216 if not os.path.exists(ROOT + "/conf.db"):
217 _sh("ovsdb-tool", "create", ROOT + "/conf.db",
218 OVS_SRC + "/vswitchd/vswitch.ovsschema")
220 opts = ["--pidfile", "--log-file"]
222 _sh(*(["ovsdb-server",
223 "--remote=punix:%s/run/db.sock" % ROOT,
224 "--remote=db:Open_vSwitch,Open_vSwitch,manager_options",
225 "--private-key=db:Open_vSwitch,SSL,private_key",
226 "--certificate=db:Open_vSwitch,SSL,certificate",
227 "--bootstrap-ca-cert=db:Open_vSwitch,SSL,ca_cert",
228 "--detach", "-vconsole:off"] + opts))
230 _sh("ovs-vsctl --no-wait --bootstrap set-ssl %s/ovsclient-privkey.pem" \
231 " %s/ovsclient-cert.pem %s/vswitchd.cacert"
232 % (pki_dir, pki_dir, pki_dir))
233 version = _sh("ovs-vsctl --no-wait --version", capture=True)
234 version = version[0].strip().split()[3]
235 root_uuid = _sh("ovs-vsctl --no-wait --bare list Open_vSwitch",
236 capture=True)[0].strip()
237 _sh("ovs-vsctl --no-wait set Open_vSwitch %s ovs_version=%s"
238 % (root_uuid, version))
240 build = BUILD_CLANG if options.clang else BUILD_GCC
241 cmd = [build + "/vswitchd/ovs-vswitchd"]
245 cmd.extend(options.dpdk)
249 cmd = ["gdb", "--args"] + cmd
250 elif options.valgrind:
251 cmd = ["valgrind", "--track-origins=yes", "--leak-check=full",
252 "--suppressions=%s/tests/glibc.supp" % OVS_SRC,
253 "--suppressions=%s/tests/openssl.supp" % OVS_SRC] + cmd
256 opts = opts + ["-vconsole:off", "--detach", "--enable-dummy"]
262 if not os.path.exists("/lib/modules"):
263 print "Missing modules directory. Is this a Linux system?"
267 _sh("rmmod", "openvswitch")
268 except subprocess.CalledProcessError, e:
269 pass # Module isn't loaded
272 _sh("rm -f /lib/modules/%s/extra/openvswitch.ko" % uname())
273 _sh("rm -f /lib/modules/%s/extra/vport-*.ko" % uname())
274 except subprocess.CalledProcessError, e:
275 pass # Module isn't installed
279 make("modules_install")
281 _sh("modprobe", "openvswitch")
282 _sh("dmesg | grep openvswitch | tail -1")
283 _sh("find /lib/modules/%s/ -iname vport-*.ko -exec insmod '{}' \;" % uname())
284 commands.append(modinst)
288 print "export PATH=" + ENV["PATH"]
296 This program is designed to help developers build and run Open vSwitch without
297 necessarily needing to know the gory details. Given some basic requirements
298 (described below), it can be used to build and run Open vSwitch, keeping
299 runtime files in the user's home directory.
302 # This section can be run as a script on ubuntu systems.
304 # First install the basic requirements needed to build Open vSwitch.
305 sudo apt-get install git build-essential libtool autoconf pkg-config \\
306 libssl-dev gdb linux-headers-`uname -r`
308 # Next clone the Open vSwitch source.
309 git clone https://github.com/openvswitch/ovs.git %(ovs)s
311 # Setup environment variables.
317 # Install the kernel module
318 sudo insmod %(ovs)s/datapath/linux/openvswitch.ko
320 # If needed, manually load all required vport modules:
321 sudo insmod %(ovs)s/datapath/linux/vport-vxlan.ko
322 sudo insmod %(ovs)s/datapath/linux/vport-geneve.ko
329 conf - Configure the ovs source.
330 make - Build the source (must have been configured).
331 check - Run the unit tests.
332 tag - Run ctags and cscope over the source.
333 kill - Kill all running instances of ovs.
334 reset - Reset any runtime configuration in %(run)s.
336 modinst - Build ovs and install the kernel module.
337 env - Print the required path environment variable.
338 doc - Print this message.
339 """ % {"ovs": OVS_SRC, "v": sys.argv[0], "run": ROOT}
343 def parse_subargs(option, opt_str, value, parser):
347 dpdkarg = parser.rargs.pop(0)
350 subopts.append(dpdkarg)
352 setattr(parser.values, option.dest, subopts)
358 description = "Open vSwitch developer configuration. Try `%prog doc`."
359 cmd_names = [c.__name__ for c in commands]
360 parser = optparse.OptionParser(usage="usage: %prog"
361 + " [options] [%s] ..."
362 % "|".join(cmd_names),
363 description=description)
365 group = optparse.OptionGroup(parser, "conf")
366 group.add_option("--disable-Werror", dest="werror", action="store_false",
367 default=True, help="compile without the Werror flag")
368 group.add_option("--cache-time", dest="cache_time",
369 action="store_true", help="configure with cached timing")
370 group.add_option("--mandir", dest="mandir", metavar="MANDIR",
371 help="configure the man documentation install directory")
372 group.add_option("--with-dpdk", dest="with_dpdk", metavar="DPDK_BUILD",
373 help="built with dpdk libraries located at DPDK_BUILD");
374 parser.add_option_group(group)
376 group = optparse.OptionGroup(parser, "Optimization Flags")
377 for i in ["s", "g"] + range(4) + ["fast"]:
378 group.add_option("--O%s" % str(i), dest="optimize",
379 action="store_const", const=i,
380 help="compile with -O%s" % str(i))
381 parser.add_option_group(group)
383 group = optparse.OptionGroup(parser, "check")
384 group.add_option("-j", "--jobs", dest="jobs", metavar="N", type="int",
385 help="Run N tests in parallel")
386 group.add_option("--tests", dest="tests", metavar="FILTER",
387 help="""run specific tests and/or a test category
388 eg, --tests=\"1-10 megaflow\"""")
389 parser.add_option_group(group)
391 group = optparse.OptionGroup(parser, "run")
392 group.add_option("-g", "--gdb", dest="gdb", action="store_true",
393 help="run ovs-vswitchd under gdb")
394 group.add_option("--valgrind", dest="valgrind", action="store_true",
395 help="run ovs-vswitchd under valgrind")
396 group.add_option("--dpdk", dest="dpdk", action="callback",
397 callback=parse_subargs,
398 help="run ovs-vswitchd with dpdk subopts (ended by --)")
399 group.add_option("--clang", dest="clang", action="store_true",
400 help="Use binaries built by clang")
402 parser.add_option_group(group)
404 options, args = parser.parse_args()
407 if arg not in cmd_names:
408 print "Unknown argument " + arg
412 set_path(BUILD_CLANG)
419 print "Missing %s." % OVS_SRC
424 if arg == cmd.__name__:
428 if __name__ == '__main__':