1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
From: Apollon Oikonomopoulos <apoikos@debian.org>
Date: Mon, 28 Jan 2019 11:10:24 +0200
Subject: Do not hardcode arch-dependent libc/linux constants
commit 1abcb876d279f698b0fafe723feac22540d145f9
Author: Apollon Oikonomopoulos <apoikos@debian.org>
Date: Mon Jan 28 11:00:13 2019 +0200
utils.mlock: do not use hardcoded mlockall(2) flags
Switch to using the build-time detected flags from constants.
Signed-off-by: Apollon Oikonomopoulos <apoikos@debian.org>
commit b273f537019249ce693f8df78640ff5c6c6bdf4c
Author: Apollon Oikonomopoulos <apoikos@debian.org>
Date: Mon Jan 28 10:59:31 2019 +0200
kvm.netdev: do not use hardcoded ioctl values
Switch to using the build-time detected values from constants.
Signed-off-by: Apollon Oikonomopoulos <apoikos@debian.org>
commit 681b23e163fc7d1fd1c9a88906834c67b9f0bf2e
Author: Apollon Oikonomopoulos <apoikos@debian.org>
Date: Mon Jan 28 10:51:32 2019 +0200
Derive arch-dependent constant values from libc/linux headers
We are currently hardcoding some C constants in our Python code in two
places: the mlockall(2) flags (used via ctypes), and the TUN/TAP driver
ioctls. These constants are actually architecture-dependent and should
be derived at build time.
Use hsc2py to append these definitions to src/AutoConf.hs, and have them
propagate to Ganeti.Constants (and from there lib/_constants.py). A
follow-up commit will replace the current constants with the derived
ones.
Signed-off-by: Apollon Oikonomopoulos <apoikos@debian.org>
---
Makefile.am | 7 +++++--
autotools/HeaderConstants.hsc | 20 ++++++++++++++++++++
lib/hypervisor/hv_kvm/netdev.py | 8 +++-----
lib/utils/mlock.py | 8 ++------
src/Ganeti/Constants.hs | 17 +++++++++++++++++
test/py/legacy/ganeti.hypervisor.hv_kvm_unittest.py | 2 +-
6 files changed, 48 insertions(+), 14 deletions(-)
create mode 100644 autotools/HeaderConstants.hsc
diff --git a/Makefile.am b/Makefile.am
index 2865c8c..fdd99a4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2342,6 +2342,9 @@ src/Ganeti/Hs2Py/ListConstants.hs: src/Ganeti/Hs2Py/ListConstants.hs.in \
m4 -DPY_CONSTANT_NAMES="$$NAMES" \
$(abs_top_srcdir)/src/Ganeti/Hs2Py/ListConstants.hs.in > $@
+autotools/HeaderConstants.hs: autotools/HeaderConstants.hsc | stamp-directories
+ hsc2hs -o $@ $<
+
test/hs/Test/Ganeti/TestImports.hs: test/hs/Test/Ganeti/TestImports.hs.in \
$(built_base_sources)
set -e; \
@@ -2359,7 +2362,7 @@ lib/_constants.py: Makefile $(HS2PY_PROG) lib/_constants.py.in | stamp-directori
lib/constants.py: lib/_constants.py
-src/AutoConf.hs: Makefile src/AutoConf.hs.in $(PRINT_PY_CONSTANTS) \
+src/AutoConf.hs: Makefile src/AutoConf.hs.in autotools/HeaderConstants.hs $(PRINT_PY_CONSTANTS) \
| $(built_base_sources)
@echo "m4 ... >" $@
@m4 -DPACKAGE_VERSION="$(PACKAGE_VERSION)" \
@@ -2436,7 +2439,7 @@ src/AutoConf.hs: Makefile src/AutoConf.hs.in $(PRINT_PY_CONSTANTS) \
done)" \
-DAF_INET4="$$(PYTHONPATH=. $(PYTHON) $(PRINT_PY_CONSTANTS) AF_INET4)" \
-DAF_INET6="$$(PYTHONPATH=. $(PYTHON) $(PRINT_PY_CONSTANTS) AF_INET6)" \
- $(abs_top_srcdir)/src/AutoConf.hs.in > $@
+ $(abs_top_srcdir)/src/AutoConf.hs.in $(abs_top_srcdir)/autotools/HeaderConstants.hs > $@
lib/_vcsversion.py: Makefile vcs-version | stamp-directories
set -e; \
diff --git a/autotools/HeaderConstants.hsc b/autotools/HeaderConstants.hsc
new file mode 100644
index 0000000..82d56d5
--- /dev/null
+++ b/autotools/HeaderConstants.hsc
@@ -0,0 +1,20 @@
+#include <sys/mman.h>
+#include <sys/ioctl.h>
+#include <linux/if_tun.h>
+
+-- mlockall(2) constants
+mclCurrent :: Int
+mclCurrent = #const MCL_CURRENT
+
+mclFuture :: Int
+mclFuture = #const MCL_FUTURE
+
+-- TUN/TAP interface ioctls
+tungetiff :: Integer
+tungetiff = #const TUNGETIFF
+
+tunsetiff :: Integer
+tunsetiff = #const TUNSETIFF
+
+tungetfeatures :: Integer
+tungetfeatures = #const TUNGETFEATURES
diff --git a/lib/hypervisor/hv_kvm/netdev.py b/lib/hypervisor/hv_kvm/netdev.py
index 08f4523..1938c31 100644
--- a/lib/hypervisor/hv_kvm/netdev.py
+++ b/lib/hypervisor/hv_kvm/netdev.py
@@ -37,15 +37,13 @@ import logging
import struct
import fcntl
+from ganeti import constants
from ganeti import errors
# TUN/TAP driver constants, taken from <linux/if_tun.h>
# They are architecture-independent and already hardcoded in qemu-kvm source,
# so we can safely include them here.
-TUNSETIFF = 0x400454ca
-TUNGETIFF = 0x800454d2
-TUNGETFEATURES = 0x800454cf
IFF_TAP = 0x0002
IFF_NO_PI = 0x1000
IFF_ONE_QUEUE = 0x2000
@@ -61,7 +59,7 @@ def _GetTunFeatures(fd, _ioctl=fcntl.ioctl):
"""
req = struct.pack("I", 0)
try:
- buf = _ioctl(fd, TUNGETFEATURES, req)
+ buf = _ioctl(fd, constants.TUNGETFEATURES, req)
except EnvironmentError as err:
logging.warning("ioctl(TUNGETFEATURES) failed: %s", err)
return None
@@ -173,7 +171,7 @@ def OpenTap(name="", features=None):
ifr = struct.pack("16sh", ifreq_name, flags)
try:
- res = fcntl.ioctl(tapfd, TUNSETIFF, ifr)
+ res = fcntl.ioctl(tapfd, constants.TUNSETIFF, ifr)
except EnvironmentError as err:
raise errors.HypervisorError("Failed to allocate a new TAP device: %s" %
err)
diff --git a/lib/utils/mlock.py b/lib/utils/mlock.py
index 837d716..78ea916 100644
--- a/lib/utils/mlock.py
+++ b/lib/utils/mlock.py
@@ -34,6 +34,7 @@
import os
import logging
+from ganeti import constants
from ganeti import errors
try:
@@ -43,11 +44,6 @@ except ImportError:
ctypes = None
-# Flags for mlockall(2) (from bits/mman.h)
-_MCL_CURRENT = 1
-_MCL_FUTURE = 2
-
-
def Mlockall(_ctypes=ctypes):
"""Lock current process' virtual address space into RAM.
@@ -77,7 +73,7 @@ def Mlockall(_ctypes=ctypes):
# pylint: disable=W0212
libc.__errno_location.restype = _ctypes.POINTER(_ctypes.c_int)
- if libc.mlockall(_MCL_CURRENT | _MCL_FUTURE):
+ if libc.mlockall(constants.MCL_CURRENT | constants.MCL_FUTURE):
# pylint: disable=W0212
logging.error("Cannot set memory lock: %s",
os.strerror(libc.__errno_location().contents.value))
diff --git a/src/Ganeti/Constants.hs b/src/Ganeti/Constants.hs
index bbfc012..ce8ac46 100644
--- a/src/Ganeti/Constants.hs
+++ b/src/Ganeti/Constants.hs
@@ -5528,3 +5528,20 @@ cliWfjcFrequency = 20
-- | Default 'WaitForJobChange' timeout in seconds
defaultWfjcTimeout :: Int
defaultWfjcTimeout = 60
+
+-- | Arch-dependent mlock(2) flags
+mclCurrent :: Int
+mclCurrent = AutoConf.mclCurrent
+
+mclFuture :: Int
+mclFuture = AutoConf.mclFuture
+
+-- | Arch-dependent TUN ioctl(2) values
+tunsetiff :: Integer
+tunsetiff = AutoConf.tunsetiff
+
+tungetiff :: Integer
+tungetiff = AutoConf.tungetiff
+
+tungetfeatures :: Integer
+tungetfeatures = AutoConf.tungetfeatures
diff --git a/test/py/legacy/ganeti.hypervisor.hv_kvm_unittest.py b/test/py/legacy/ganeti.hypervisor.hv_kvm_unittest.py
index 9111bf3..040a8b9 100755
--- a/test/py/legacy/ganeti.hypervisor.hv_kvm_unittest.py
+++ b/test/py/legacy/ganeti.hypervisor.hv_kvm_unittest.py
@@ -653,7 +653,7 @@ class TestGetTunFeatures(unittest.TestCase):
self.assertTrue(result is None)
def _FakeIoctl(self, features, fd, request, buf):
- self.assertEqual(request, netdev.TUNGETFEATURES)
+ self.assertEqual(request, constants.TUNGETFEATURES)
(reqno, ) = struct.unpack("I", buf)
self.assertEqual(reqno, 0)
|