import ovs.daemon
import ovs.db.idl
import ovs.unixctl
+import ovs.unixctl.server
import ovs.vlog
vlog = ovs.vlog.Vlog("ovs-monitor-ipsec")
ovs.daemon.daemonize()
ovs.unixctl.command_register("exit", "", 0, 0, unixctl_exit, None)
- error, unixctl_server = ovs.unixctl.UnixctlServer.create(None)
+ error, unixctl_server = ovs.unixctl.server.UnixctlServer.create(None)
if error:
ovs.util.ovs_fatal(error, "could not create unixctl server", vlog)
python/ovs/socket_util.py \
python/ovs/stream.py \
python/ovs/timeval.py \
- python/ovs/unixctl.py \
+ python/ovs/unixctl/__init__.py \
+ python/ovs/unixctl/client.py \
+ python/ovs/unixctl/server.py \
python/ovs/util.py \
python/ovs/version.py \
python/ovs/vlog.py
--- /dev/null
+# Copyright (c) 2011, 2012 Nicira, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import types
+
+import ovs.util
+
+commands = {}
+strtypes = types.StringTypes
+
+
+class _UnixctlCommand(object):
+ def __init__(self, usage, min_args, max_args, callback, aux):
+ self.usage = usage
+ self.min_args = min_args
+ self.max_args = max_args
+ self.callback = callback
+ self.aux = aux
+
+
+def _unixctl_help(conn, unused_argv, unused_aux):
+ reply = "The available commands are:\n"
+ command_names = sorted(commands.keys())
+ for name in command_names:
+ reply += " "
+ usage = commands[name].usage
+ if usage:
+ reply += "%-23s %s" % (name, usage)
+ else:
+ reply += name
+ reply += "\n"
+ conn.reply(reply)
+
+
+def command_register(name, usage, min_args, max_args, callback, aux):
+ """ Registers a command with the given 'name' to be exposed by the
+ UnixctlServer. 'usage' describes the arguments to the command; it is used
+ only for presentation to the user in "help" output.
+
+ 'callback' is called when the command is received. It is passed a
+ UnixctlConnection object, the list of arguments as unicode strings, and
+ 'aux'. Normally 'callback' should reply by calling
+ UnixctlConnection.reply() or UnixctlConnection.reply_error() before it
+ returns, but if the command cannot be handled immediately, then it can
+ defer the reply until later. A given connection can only process a single
+ request at a time, so a reply must be made eventually to avoid blocking
+ that connection."""
+
+ assert isinstance(name, strtypes)
+ assert isinstance(usage, strtypes)
+ assert isinstance(min_args, int)
+ assert isinstance(max_args, int)
+ assert isinstance(callback, types.FunctionType)
+
+ if name not in commands:
+ commands[name] = _UnixctlCommand(usage, min_args, max_args, callback,
+ aux)
+
+def socket_name_from_target(target):
+ assert isinstance(target, strtypes)
+
+ if target.startswith("/"):
+ return 0, target
+
+ pidfile_name = "%s/%s.pid" % (ovs.dirs.RUNDIR, target)
+ pid = ovs.daemon.read_pidfile(pidfile_name)
+ if pid < 0:
+ return -pid, "cannot read pidfile \"%s\"" % pidfile_name
+
+ return 0, "%s/%s.%d.ctl" % (ovs.dirs.RUNDIR, target, pid)
+
+command_register("help", "", 0, 0, _unixctl_help, None)
--- /dev/null
+# Copyright (c) 2011, 2012 Nicira, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import copy
+import errno
+import os
+import types
+
+import ovs.jsonrpc
+import ovs.stream
+import ovs.util
+
+
+vlog = ovs.vlog.Vlog("unixctl_client")
+strtypes = types.StringTypes
+
+
+class UnixctlClient(object):
+ def __init__(self, conn):
+ assert isinstance(conn, ovs.jsonrpc.Connection)
+ self._conn = conn
+
+ def transact(self, command, argv):
+ assert isinstance(command, strtypes)
+ assert isinstance(argv, list)
+ for arg in argv:
+ assert isinstance(arg, strtypes)
+
+ request = ovs.jsonrpc.Message.create_request(command, argv)
+ error, reply = self._conn.transact_block(request)
+
+ if error:
+ vlog.warn("error communicating with %s: %s"
+ % (self._conn.name, os.strerror(error)))
+ return error, None, None
+
+ if reply.error is not None:
+ return 0, str(reply.error), None
+ else:
+ assert reply.result is not None
+ return 0, None, str(reply.result)
+
+ def close(self):
+ self._conn.close()
+ self.conn = None
+
+ @staticmethod
+ def create(path):
+ assert isinstance(path, str)
+
+ unix = "unix:%s" % ovs.util.abs_file_name(ovs.dirs.RUNDIR, path)
+ error, stream = ovs.stream.Stream.open_block(
+ ovs.stream.Stream.open(unix))
+
+ if error:
+ vlog.warn("failed to connect to %s" % path)
+ return error, None
+
+ return 0, UnixctlClient(ovs.jsonrpc.Connection(stream))
import os
import types
-import ovs.daemon
import ovs.dirs
import ovs.jsonrpc
import ovs.stream
+import ovs.unixctl
import ovs.util
import ovs.version
import ovs.vlog
Message = ovs.jsonrpc.Message
-vlog = ovs.vlog.Vlog("unixctl")
-commands = {}
+vlog = ovs.vlog.Vlog("unixctl_server")
strtypes = types.StringTypes
-class _UnixctlCommand(object):
- def __init__(self, usage, min_args, max_args, callback, aux):
- self.usage = usage
- self.min_args = min_args
- self.max_args = max_args
- self.callback = callback
- self.aux = aux
-
-
-def _unixctl_help(conn, unused_argv, unused_aux):
- assert isinstance(conn, UnixctlConnection)
- reply = "The available commands are:\n"
- command_names = sorted(commands.keys())
- for name in command_names:
- reply += " "
- usage = commands[name].usage
- if usage:
- reply += "%-23s %s" % (name, usage)
- else:
- reply += name
- reply += "\n"
- conn.reply(reply)
-
-
-def _unixctl_version(conn, unused_argv, version):
- assert isinstance(conn, UnixctlConnection)
- version = "%s (Open vSwitch) %s" % (ovs.util.PROGRAM_NAME, version)
- conn.reply(version)
-
-
-def command_register(name, usage, min_args, max_args, callback, aux):
- """ Registers a command with the given 'name' to be exposed by the
- UnixctlServer. 'usage' describes the arguments to the command; it is used
- only for presentation to the user in "help" output.
-
- 'callback' is called when the command is received. It is passed a
- UnixctlConnection object, the list of arguments as unicode strings, and
- 'aux'. Normally 'callback' should reply by calling
- UnixctlConnection.reply() or UnixctlConnection.reply_error() before it
- returns, but if the command cannot be handled immediately, then it can
- defer the reply until later. A given connection can only process a single
- request at a time, so a reply must be made eventually to avoid blocking
- that connection."""
-
- assert isinstance(name, strtypes)
- assert isinstance(usage, strtypes)
- assert isinstance(min_args, int)
- assert isinstance(max_args, int)
- assert isinstance(callback, types.FunctionType)
-
- if name not in commands:
- commands[name] = _UnixctlCommand(usage, min_args, max_args, callback,
- aux)
-
-
-def socket_name_from_target(target):
- assert isinstance(target, strtypes)
-
- if target.startswith("/"):
- return 0, target
-
- pidfile_name = "%s/%s.pid" % (ovs.dirs.RUNDIR, target)
- pid = ovs.daemon.read_pidfile(pidfile_name)
- if pid < 0:
- return -pid, "cannot read pidfile \"%s\"" % pidfile_name
-
- return 0, "%s/%s.%d.ctl" % (ovs.dirs.RUNDIR, target, pid)
-
-
class UnixctlConnection(object):
def __init__(self, rpc):
assert isinstance(rpc, ovs.jsonrpc.Connection)
error = None
params = request.params
method = request.method
- command = commands.get(method)
+ command = ovs.unixctl.commands.get(method)
if command is None:
error = '"%s" is not a valid command' % method
elif len(params) < command.min_args:
self.reply_error(error)
+def _unixctl_version(conn, unused_argv, version):
+ assert isinstance(conn, UnixctlConnection)
+ version = "%s (Open vSwitch) %s" % (ovs.util.PROGRAM_NAME, version)
+ conn.reply(version)
+
class UnixctlServer(object):
def __init__(self, listener):
assert isinstance(listener, ovs.stream.PassiveStream)
% path)
return error, None
- command_register("help", "", 0, 0, _unixctl_help, None)
- command_register("version", "", 0, 0, _unixctl_version, version)
+ ovs.unixctl.command_register("version", "", 0, 0, _unixctl_version,
+ version)
return 0, UnixctlServer(listener)
import ovs.daemon
import ovs.unixctl
+import ovs.unixctl.client
import ovs.util
import ovs.vlog
else:
socket_name = str_result
- error, client = ovs.unixctl.UnixctlClient.create(socket_name)
+ error, client = ovs.unixctl.client.UnixctlClient.create(socket_name)
if error:
ovs.util.ovs_fatal(error, "cannot connect to \"%s\"" % socket_name)
import ovs.daemon
import ovs.unixctl
+import ovs.unixctl.server
vlog = ovs.vlog.Vlog("test-unixctl")
exiting = False
ovs.vlog.handle_args(args)
ovs.daemon.daemonize_start()
- error, server = ovs.unixctl.UnixctlServer.create(args.unixctl)
+ error, server = ovs.unixctl.server.UnixctlServer.create(args.unixctl)
if error:
ovs.util.ovs_fatal(error, "could not create unixctl server at %s"
% args.unixctl, vlog)
import ovs.daemon
import ovs.db.idl
import ovs.unixctl
+import ovs.unixctl.server
vlog = ovs.vlog.Vlog("ovs-xapi-sync")
session = None
ovs.unixctl.command_register("exit", "", 0, 0, unixctl_exit, None)
ovs.unixctl.command_register("flush-cache", "", 0, 0, unixctl_flush_cache,
None)
- error, unixctl_server = ovs.unixctl.UnixctlServer.create(None)
+ error, unixctl_server = ovs.unixctl.server.UnixctlServer.create(None)
if error:
ovs.util.ovs_fatal(error, "could not create unixctl server", vlog)