Change in vdsm[master]: netlink: simplify string conversion methods
asegurap at redhat.com
asegurap at redhat.com
Wed May 7 13:55:01 UTC 2014
Antoni Segura Puimedon has uploaded a new change for review.
Change subject: netlink: simplify string conversion methods
......................................................................
netlink: simplify string conversion methods
There are multiple string handling needs shared between the netlink
modules. This patch unifies them to simplify all the modules.
Change-Id: I3cdd56cd243095d75a37c66d79e13c2715642d97
Signed-off-by: Antoni S. Puimedon <asegurap at redhat.com>
---
M lib/vdsm/netlink/__init__.py
M lib/vdsm/netlink/addr.py
M lib/vdsm/netlink/link.py
3 files changed, 31 insertions(+), 48 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/73/27473/1
diff --git a/lib/vdsm/netlink/__init__.py b/lib/vdsm/netlink/__init__.py
index c7bb58f..4eaf80a 100644
--- a/lib/vdsm/netlink/__init__.py
+++ b/lib/vdsm/netlink/__init__.py
@@ -18,8 +18,8 @@
# Refer to the README and COPYING files for full details of the license
#
from contextlib import contextmanager
-from ctypes import (CDLL, CFUNCTYPE, c_char_p, c_int, c_void_p, c_size_t,
- get_errno)
+from ctypes import (CDLL, CFUNCTYPE, c_char, c_char_p, c_int, c_void_p,
+ c_size_t, get_errno, sizeof)
from distutils.version import StrictVersion
from Queue import Empty, Queue
from threading import BoundedSemaphore
@@ -28,6 +28,7 @@
_POOL_SIZE = 5
_NETLINK_ROUTE = 0
+CHARBUFFSIZE = 40 # Increased to fit IPv6 expanded representations
class NLSocketPool(object):
@@ -84,6 +85,25 @@
_nl_cache_free(cache)
+def _nladdr_to_str(addr):
+ """Returns the textual representation of a netlink address (be it hardware
+ or IP) or None if the address is None"""
+ if addr is not None:
+ address = (c_char * CHARBUFFSIZE)()
+ return _nl_addr2str(addr, address, sizeof(address))
+
+
+def _nlaf_to_str(af_num):
+ """Returns the textual address family representation of the numerical id"""
+ family = (c_char * CHARBUFFSIZE)()
+ return _nl_af2str(af_num, family, sizeof(family))
+
+
+def _nlscope_to_str(scope_num):
+ """Returns the textual scope representation of the numerical id"""
+ scope = (c_char * CHARBUFFSIZE)()
+ return _rtnl_scope2str(scope_num, scope, sizeof(scope))
+
# C function prototypes
# http://docs.python.org/2/library/ctypes.html#function-prototypes
# This helps ctypes know the calling conventions it should use to communicate
diff --git a/lib/vdsm/netlink/addr.py b/lib/vdsm/netlink/addr.py
index 33542f5..2b14b7e 100644
--- a/lib/vdsm/netlink/addr.py
+++ b/lib/vdsm/netlink/addr.py
@@ -16,18 +16,15 @@
#
# Refer to the README and COPYING files for full details of the license
#
-from ctypes import (CFUNCTYPE, byref, c_char, c_char_p, c_int, c_size_t,
- c_void_p, sizeof)
+from ctypes import CFUNCTYPE, byref, c_int, c_void_p
from functools import partial
from . import (_alloc_cache, _cache_manager, _nl_cache_get_first,
_nl_cache_get_next)
from . import _char_proto, _int_proto, _void_proto
-from . import (_ethtool_uses_libnl3, LIBNL_ROUTE, _nl_addr2str, _nl_af2str,
- _nl_geterror, _pool)
+from . import _ethtool_uses_libnl3, LIBNL_ROUTE, _nl_geterror, _pool
+from . import (_nladdr_to_str, _nlaf_to_str, _nlscope_to_str)
from .link import _nl_link_cache, _link_index_to_name
-
-CHARBUFFSIZE = 40 # Increased to fit IPv6 expanded representations
def iter_addrs():
@@ -49,30 +46,11 @@
'label': (_rtnl_addr_get_label(addr) or
_link_index_to_name(link_cache, index)),
'index': index,
- 'family': _addr_family(addr),
+ 'family': _nlaf_to_str(_rtnl_addr_get_family(addr)),
'prefixlen': _rtnl_addr_get_prefixlen(addr),
- 'scope': _addr_scope(addr),
+ 'scope': _nlscope_to_str(_rtnl_addr_get_scope(addr)),
'flags': _rtnl_addr_get_flags(addr),
- 'address': _addr_local(addr)}
-
-
-def _addr_scope(addr):
- """Returns the scope name for which the address is defined."""
- scope = (c_char * CHARBUFFSIZE)()
- return _rtnl_scope2str(_rtnl_addr_get_scope(addr), scope, sizeof(scope))
-
-
-def _addr_family(addr):
- """Returns the family name of the address."""
- family = (c_char * CHARBUFFSIZE)()
- return _nl_af2str(_rtnl_addr_get_family(addr), family, sizeof(family))
-
-
-def _addr_local(addr):
- """Returns the textual representation of the address."""
- address = (c_char * CHARBUFFSIZE)()
- return _nl_addr2str(_rtnl_addr_get_local(addr), address, sizeof(address))
-
+ 'address': _nladdr_to_str(_rtnl_addr_get_local(addr))}
# C function prototypes
# http://docs.python.org/2/library/ctypes.html#function-prototypes
@@ -107,5 +85,3 @@
_rtnl_addr_get_scope = _int_proto(('rtnl_addr_get_scope', LIBNL_ROUTE))
_rtnl_addr_get_flags = _int_proto(('rtnl_addr_get_flags', LIBNL_ROUTE))
_rtnl_addr_get_local = _void_proto(('rtnl_addr_get_local', LIBNL_ROUTE))
-_rtnl_scope2str = CFUNCTYPE(c_char_p, c_int, c_char_p, c_size_t)((
- 'rtnl_scope2str', LIBNL_ROUTE))
diff --git a/lib/vdsm/netlink/link.py b/lib/vdsm/netlink/link.py
index 5c3583d..e138c67 100644
--- a/lib/vdsm/netlink/link.py
+++ b/lib/vdsm/netlink/link.py
@@ -24,10 +24,8 @@
from . import (_alloc_cache, _cache_manager, _nl_cache_get_first,
_nl_cache_get_next)
from . import _char_proto, _int_proto, _void_proto
-from . import (_ethtool_uses_libnl3, LIBNL_ROUTE, _nl_addr2str, _nl_geterror,
- _pool)
-
-CHARBUFFSIZE = 40 # Increased to fit IPv6 expanded representations
+from . import _ethtool_uses_libnl3, LIBNL_ROUTE, _nl_geterror, _pool
+from . import _nladdr_to_str, CHARBUFFSIZE
def get_link(name):
@@ -55,7 +53,7 @@
def _link_info(cache, link):
"""Returns a dictionary with the information of the link object."""
info = {}
- info['address'] = _link_address(link)
+ info['address'] = _nladdr_to_str(_rtnl_link_get_addr(link)),
info['flags'] = _rtnl_link_get_flags(link)
info['index'] = _rtnl_link_get_ifindex(link)
info['mtu'] = _rtnl_link_get_mtu(link)
@@ -90,17 +88,6 @@
"""Returns the textual name of the link with index equal to link_index."""
name = (c_char * CHARBUFFSIZE)()
return _rtnl_link_i2name(cache, link_index, name, sizeof(name))
-
-
-def _link_address(link):
- """Returns the MAC address of a link object or None if the link does not
- have a physical address."""
- nl_addr = _rtnl_link_get_addr(link)
- if nl_addr:
- address = (c_char * CHARBUFFSIZE)()
- return _nl_addr2str(nl_addr, address, sizeof(address))
- else:
- return None
def _link_state(link):
--
To view, visit http://gerrit.ovirt.org/27473
To unsubscribe, visit http://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I3cdd56cd243095d75a37c66d79e13c2715642d97
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Antoni Segura Puimedon <asegurap at redhat.com>
More information about the vdsm-patches
mailing list