[ovs-dev] [PATCH 1/2] python/ovs/socket_util: add tcp related helper functions which will be used by tcp

Isaku Yamahata yamahata at valinux.co.jp
Wed Aug 22 10:05:40 UTC 2012


Signed-off-by: Isaku Yamahata <yamahata at valinux.co.jp>
---
 python/ovs/socket_util.py |   40 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 40 insertions(+), 0 deletions(-)

diff --git a/python/ovs/socket_util.py b/python/ovs/socket_util.py
index a8e8d92..d05f165 100644
--- a/python/ovs/socket_util.py
+++ b/python/ovs/socket_util.py
@@ -83,6 +83,39 @@ def check_connection_completion(sock):
         return errno.EAGAIN
 
 
+def inet_parse_active(target, default_port):
+    address = target.split(":")
+    host_name = address[0]
+    if len(address) >= 2:
+        port = int(address[1])
+    elif not default_port:
+        port = default_port
+    else:
+        raise Exception('%s: port nubmer must be specified' % target)
+    return (host_name, port)
+
+
+def inet_open_active(style, target, default_port, dscp):
+    address = inet_parse_active(target, default_port)
+    try:
+        sock = socket.socket(socket.AF_INET, style, 0)
+    except socket.error, e:
+        return get_exception_errno(e), None
+
+    try:
+        set_nonblocking(sock)
+        set_dscp(sock, dscp)
+        try:
+            sock.connect(address)
+        except socket.error, e:
+            if get_exception_errno(e) != errno.EINPROGRESS:
+                raise
+        return 0, sock
+    except socket.error, e:
+        sock.close()
+        return get_exception_errno(e), None
+
+
 def get_socket_error(sock):
     """Returns the errno value associated with 'socket' (0 if no error) and
     resets the socket's error status."""
@@ -147,3 +180,10 @@ def set_nonblocking(sock):
     except socket.error, e:
         vlog.err("could not set nonblocking mode on socket: %s"
                  % os.strerror(get_socket_error(e)))
+
+
+def set_dscp(sock, dscp):
+    if dscp > 63:
+        raise Exception("Invalid dscp %d" % dscp)
+    val = dscp << 2
+    sock.setsockopt(socket.IPPROTO_IP, socket.IP_TOS, val)
-- 
1.7.1.1




More information about the dev mailing list