ovs-dev.py: rename ROOT to RUNDIR
[cascardo/ovs.git] / utilities / ovs-dev.py
index 71d081d..2611a70 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/python
-# Copyright (c) 2013, 2014 Nicira, Inc.
+# Copyright (c) 2013, 2014, 2015 Nicira, Inc.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -22,18 +22,22 @@ import tempfile
 
 ENV = os.environ
 HOME = ENV["HOME"]
+PWD = os.getcwd()
 OVS_SRC = HOME + "/ovs"
-ROOT = HOME + "/root"
+if os.path.exists(PWD + "/WHY-OVS.md"):
+    OVS_SRC = PWD  # Use current directory as OVS source tree
+RUNDIR = OVS_SRC + "/_run"
 BUILD_GCC = OVS_SRC + "/_build-gcc"
 BUILD_CLANG = OVS_SRC + "/_build-clang"
-PATH = "%(ovs)s/utilities:%(ovs)s/ovsdb:%(ovs)s/vswitchd" % {"ovs": BUILD_GCC}
-
-ENV["PATH"] = PATH + ":" + ENV["PATH"]
 
 options = None
 parser = None
 commands = []
 
+def set_path(build):
+    PATH = "%(ovs)s/utilities:%(ovs)s/ovsdb:%(ovs)s/vswitchd" % {"ovs": build}
+
+    ENV["PATH"] = PATH + ":" + ENV["PATH"]
 
 def _sh(*args, **kwargs):
     print "------> " + " ".join(args)
@@ -59,9 +63,11 @@ def conf():
     except OSError:
         pass
 
-    configure = ["../configure", "--prefix=" + ROOT, "--localstatedir=" + ROOT,
-                 "--with-logdir=%s/log" % ROOT, "--with-rundir=%s/run" % ROOT,
-                 "--enable-silent-rules", "--with-dbdir=" + ROOT, "--silent"]
+    configure = ["../configure",
+                 "--prefix=" + RUNDIR, "--localstatedir=" + RUNDIR,
+                 "--with-logdir=%s/log" % RUNDIR,
+                 "--with-rundir=%s/run" % RUNDIR,
+                 "--enable-silent-rules", "--with-dbdir=" + RUNDIR, "--silent"]
 
     cflags = "-g -fno-omit-frame-pointer"
 
@@ -81,7 +87,7 @@ def conf():
     if options.optimize is None:
         options.optimize = 0
 
-    cflags += " -O%d" % options.optimize
+    cflags += " -O%s" % str(options.optimize)
 
     ENV["CFLAGS"] = cflags
 
@@ -181,7 +187,7 @@ commands.append(tag)
 
 def kill():
     for proc in ["ovs-vswitchd", "ovsdb-server"]:
-        if os.path.exists("%s/run/openvswitch/%s.pid" % (ROOT, proc)):
+        if os.path.exists("%s/run/openvswitch/%s.pid" % (RUNDIR, proc)):
             _sh("ovs-appctl", "-t", proc, "exit", check=False)
             time.sleep(.1)
         _sh("sudo", "killall", "-q", "-2", proc, check=False)
@@ -190,8 +196,8 @@ commands.append(kill)
 
 def reset():
     kill()
-    if os.path.exists(ROOT):
-        shutil.rmtree(ROOT)
+    if os.path.exists(RUNDIR):
+        shutil.rmtree(RUNDIR)
     for dp in _sh("ovs-dpctl dump-dps", capture=True):
         _sh("ovs-dpctl", "del-dp", dp.strip())
 commands.append(reset)
@@ -200,11 +206,11 @@ commands.append(reset)
 def run():
     kill()
     for d in ["log", "run"]:
-        d = "%s/%s" % (ROOT, d)
+        d = "%s/%s" % (RUNDIR, d)
         shutil.rmtree(d, ignore_errors=True)
         os.makedirs(d)
 
-    pki_dir = ROOT + "/pki"
+    pki_dir = RUNDIR + "/pki"
     if not os.path.exists(pki_dir):
         os.mkdir(pki_dir)
         os.chdir(pki_dir)
@@ -212,14 +218,14 @@ def run():
         _sh("ovs-pki req+sign ovsclient")
         os.chdir(OVS_SRC)
 
-    if not os.path.exists(ROOT + "/conf.db"):
-        _sh("ovsdb-tool", "create", ROOT + "/conf.db",
+    if not os.path.exists(RUNDIR + "/conf.db"):
+        _sh("ovsdb-tool", "create", RUNDIR + "/conf.db",
             OVS_SRC + "/vswitchd/vswitch.ovsschema")
 
     opts = ["--pidfile", "--log-file"]
 
     _sh(*(["ovsdb-server",
-           "--remote=punix:%s/run/db.sock" % ROOT,
+           "--remote=punix:%s/run/db.sock" % RUNDIR,
            "--remote=db:Open_vSwitch,Open_vSwitch,manager_options",
            "--private-key=db:Open_vSwitch,SSL,private_key",
            "--certificate=db:Open_vSwitch,SSL,certificate",
@@ -236,7 +242,8 @@ def run():
     _sh("ovs-vsctl --no-wait set Open_vSwitch %s ovs_version=%s"
         % (root_uuid, version))
 
-    cmd = [BUILD_GCC + "/vswitchd/ovs-vswitchd"]
+    build = BUILD_CLANG if options.clang else BUILD_GCC
+    cmd = [build + "/vswitchd/ovs-vswitchd"]
 
     if options.dpdk:
         cmd.append("--dpdk")
@@ -267,7 +274,8 @@ def modinst():
         pass  # Module isn't loaded
 
     try:
-        _sh("rm /lib/modules/%s/extra/openvswitch.ko" % uname())
+        _sh("rm -f /lib/modules/%s/extra/openvswitch.ko" % uname())
+        _sh("rm -f /lib/modules/%s/extra/vport-*.ko" % uname())
     except subprocess.CalledProcessError, e:
         pass  # Module isn't installed
 
@@ -277,6 +285,7 @@ def modinst():
 
     _sh("modprobe", "openvswitch")
     _sh("dmesg | grep openvswitch | tail -1")
+    _sh("find /lib/modules/%s/ -iname vport-*.ko -exec insmod '{}' \;" % uname())
 commands.append(modinst)
 
 
@@ -313,6 +322,11 @@ Basic Configuration:
     # Install the kernel module
     sudo insmod %(ovs)s/datapath/linux/openvswitch.ko
 
+    # If needed, manually load all required vport modules:
+    sudo insmod %(ovs)s/datapath/linux/vport-vxlan.ko
+    sudo insmod %(ovs)s/datapath/linux/vport-geneve.ko
+    [...]
+
     # Run the switch.
     %(v)s run
 
@@ -327,7 +341,7 @@ Commands:
     modinst - Build ovs and install the kernel module.
     env     - Print the required path environment variable.
     doc     - Print this message.
-""" % {"ovs": OVS_SRC, "v": sys.argv[0], "run": ROOT}
+""" % {"ovs": OVS_SRC, "v": sys.argv[0], "run": RUNDIR}
     sys.exit(0)
 commands.append(doc)
 
@@ -362,10 +376,13 @@ def main():
                      help="configure the man documentation install directory")
     group.add_option("--with-dpdk", dest="with_dpdk", metavar="DPDK_BUILD",
                      help="built with dpdk libraries located at DPDK_BUILD");
+    parser.add_option_group(group)
 
-    for i in range(4):
-        group.add_option("--O%d" % i, dest="optimize", action="store_const",
-                         const=i, help="compile with -O%d" % i)
+    group = optparse.OptionGroup(parser, "Optimization Flags")
+    for i in ["s", "g"] + range(4) + ["fast"]:
+        group.add_option("--O%s" % str(i), dest="optimize",
+                         action="store_const", const=i,
+                         help="compile with -O%s" % str(i))
     parser.add_option_group(group)
 
     group = optparse.OptionGroup(parser, "check")
@@ -384,6 +401,9 @@ def main():
     group.add_option("--dpdk", dest="dpdk", action="callback",
                      callback=parse_subargs,
                      help="run ovs-vswitchd with dpdk subopts (ended by --)")
+    group.add_option("--clang", dest="clang", action="store_true",
+                     help="Use binaries built by clang")
+
     parser.add_option_group(group)
 
     options, args = parser.parse_args()
@@ -393,6 +413,11 @@ def main():
             print "Unknown argument " + arg
             doc()
 
+    if options.clang:
+        set_path(BUILD_CLANG)
+    else:
+        set_path(BUILD_GCC)
+
     try:
         os.chdir(OVS_SRC)
     except OSError: