Change in vdsm[master]: netinfo: get an interface using the ip associated with it

mhuntxu at gmail.com mhuntxu at gmail.com
Tue Jan 29 18:55:02 UTC 2013


Hunt Xu has uploaded a new change for review.

Change subject: netinfo: get an interface using the ip associated with it
......................................................................

netinfo: get an interface using the ip associated with it

Prevent manually parsing the routes files.

Change-Id: I239f92c527524e5651f51712841c3eafcf659e53
Signed-off-by: huntxu <mhuntxu at gmail.com>
---
M tests/capsTests.py
M vdsm/BindingXMLRPC.py
M vdsm/caps.py
M vdsm/netinfo.py
4 files changed, 16 insertions(+), 32 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/19/11519/1

diff --git a/tests/capsTests.py b/tests/capsTests.py
index 0234ccf..7acf836 100644
--- a/tests/capsTests.py
+++ b/tests/capsTests.py
@@ -23,8 +23,6 @@
 from testrunner import VdsmTestCase as TestCaseBase
 
 import caps
-import platform
-from nose.plugins.skip import SkipTest
 
 
 class TestCaps(TestCaseBase):
@@ -80,16 +78,3 @@
         sign = ["=", "&"]
         for res, s in zip(expectedRes, sign):
             self.assertEqual(res, caps._parseKeyVal(lines, s))
-
-    def test_getIfaceByIP(self):
-        expectedRes = ["wlan0", "virbr0"]
-        ip = ["10.201.129.37", "192.168.122.90"]
-        arch = platform.machine()
-        if arch == 'x86_64':
-            route_file = 'route_info.out'
-        elif arch == 'ppc64':
-            route_file = 'route_info_ppc64.out'
-        else:
-            raise SkipTest("Unsupported Architecture %s" % arch)
-        for res, i in zip(expectedRes, ip):
-            self.assertEqual(res, caps._getIfaceByIP(i, route_file))
diff --git a/vdsm/BindingXMLRPC.py b/vdsm/BindingXMLRPC.py
index 9a4db12..c989531 100644
--- a/vdsm/BindingXMLRPC.py
+++ b/vdsm/BindingXMLRPC.py
@@ -26,10 +26,10 @@
 import libvirt
 import threading
 
-import caps
 from vdsm import constants
 from vdsm import utils
 from vdsm.define import doneCode, errCode
+from vdsm.netinfo import getIfaceByIP
 import API
 from vdsm.exception import VdsmException
 try:
@@ -86,9 +86,10 @@
         Return the IP address and last client information
         """
         last = self.server.lastClient
+        lastserver = self.server.lastServerIP
         return {'management_ip': self.serverIP,
                 'lastClient': last,
-                'lastClientIface': caps._getIfaceByIP(last)}
+                'lastClientIface': getIfaceByIP(lastserver)}
 
     def _getKeyCertFilenames(self):
         """
@@ -113,6 +114,7 @@
                 """Track from where client connections are coming."""
                 self.server.lastClient = self.client_address[0]
                 self.server.lastClientTime = time.time()
+                self.server.lastServerIP = self.server.socket.getsockname()[0]
                 # FIXME: The editNetwork API uses this log file to
                 # determine if this host is still accessible.  We use a
                 # file (rather than an event) because editNetwork is
@@ -153,6 +155,7 @@
 
         server.lastClientTime = 0
         server.lastClient = '0.0.0.0'
+        server.lastServerIP = '0.0.0.0'
 
         return server
 
diff --git a/vdsm/caps.py b/vdsm/caps.py
index a307200..903e22f 100644
--- a/vdsm/caps.py
+++ b/vdsm/caps.py
@@ -24,9 +24,6 @@
 from xml.dom import minidom
 import logging
 import time
-import struct
-import socket
-import itertools
 import linecache
 import glob
 
@@ -295,18 +292,6 @@
     caps['guestOverhead'] = config.get('vars', 'guest_ram_overhead')
 
     return caps
-
-
-def _getIfaceByIP(addr, fileName='/proc/net/route'):
-    remote = struct.unpack('I', socket.inet_aton(addr))[0]
-    for line in itertools.islice(file(fileName), 1, None):
-        (iface, dest, gateway, flags, refcnt, use, metric,
-         mask, mtu, window, irtt) = line.split()
-        dest = int(dest, 16)
-        mask = int(mask, 16)
-        if remote & mask == dest & mask:
-            return iface
-    return ''  # should never get here w/ default gw
 
 
 def _getKeyPackages():
diff --git a/vdsm/netinfo.py b/vdsm/netinfo.py
index 922c90c..43f24d4 100644
--- a/vdsm/netinfo.py
+++ b/vdsm/netinfo.py
@@ -430,6 +430,17 @@
     return filter(None, [getaddr(i) for i in ethtool.get_active_devices()])
 
 
+def getIfaceByIP(ip):
+    for iface in ethtool.get_active_devices():
+        ipaddrs = [
+            a.address for a in
+            ethtool.get_interfaces_info(iface)[0].get_ipv6_addresses()]
+        ipaddrs.append(getaddr(iface))
+        if ip in ipaddrs:
+            return iface
+    return ''
+
+
 class NetInfo(object):
     def __init__(self, _netinfo=None):
         if _netinfo is None:


--
To view, visit http://gerrit.ovirt.org/11519
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I239f92c527524e5651f51712841c3eafcf659e53
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Hunt Xu <mhuntxu at gmail.com>


More information about the vdsm-patches mailing list