2013-09-18 21:29:28 +08:00
|
|
|
#
|
2013-10-28 04:59:47 +08:00
|
|
|
# Copyright 2013 Red Hat, Inc.
|
2013-09-18 21:29:28 +08:00
|
|
|
# Giuseppe Scrivano <gscrivan@redhat.com>
|
|
|
|
#
|
2018-04-04 21:35:41 +08:00
|
|
|
# This work is licensed under the GNU GPLv2 or later.
|
2018-03-21 03:00:02 +08:00
|
|
|
# See the COPYING file in the top-level directory.
|
2013-09-18 21:29:28 +08:00
|
|
|
|
2018-03-21 00:18:35 +08:00
|
|
|
from .device import Device
|
2018-03-21 00:27:37 +08:00
|
|
|
from ..xmlbuilder import XMLProperty
|
2013-09-18 21:29:28 +08:00
|
|
|
|
|
|
|
|
2018-03-21 00:18:35 +08:00
|
|
|
class DeviceRng(Device):
|
2018-03-21 22:53:34 +08:00
|
|
|
XML_NAME = "rng"
|
2013-09-18 21:29:28 +08:00
|
|
|
|
|
|
|
TYPE_RANDOM = "random"
|
|
|
|
TYPE_EGD = "egd"
|
|
|
|
TYPES = [TYPE_RANDOM, TYPE_EGD]
|
|
|
|
|
|
|
|
BACKEND_TYPE_UDP = "udp"
|
|
|
|
BACKEND_TYPE_TCP = "tcp"
|
|
|
|
BACKEND_TYPES = [BACKEND_TYPE_UDP, BACKEND_TYPE_TCP]
|
|
|
|
|
|
|
|
BACKEND_MODE_BIND = "bind"
|
|
|
|
BACKEND_MODE_CONNECT = "connect"
|
|
|
|
BACKEND_MODES = [BACKEND_MODE_BIND, BACKEND_MODE_CONNECT]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_pretty_type(rng_type):
|
2018-03-21 00:18:35 +08:00
|
|
|
if rng_type == DeviceRng.TYPE_RANDOM:
|
2013-09-18 21:29:28 +08:00
|
|
|
return _("Random")
|
2018-03-21 00:18:35 +08:00
|
|
|
if rng_type == DeviceRng.TYPE_EGD:
|
2013-09-18 21:29:28 +08:00
|
|
|
return _("Entropy Gathering Daemon")
|
|
|
|
return rng_type
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_pretty_backend_type(backend_type):
|
2017-08-05 14:39:32 +08:00
|
|
|
return {"udp": "UDP",
|
2013-09-18 21:29:28 +08:00
|
|
|
"tcp": "TCP"}.get(backend_type) or backend_type
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_pretty_mode(mode):
|
2017-08-05 14:39:32 +08:00
|
|
|
return {"bind": _("Bind"),
|
2016-02-05 23:18:16 +08:00
|
|
|
"connect": _("Connect")}.get(mode) or mode
|
2013-09-18 21:29:28 +08:00
|
|
|
|
|
|
|
def supports_property(self, propname):
|
|
|
|
"""
|
|
|
|
Whether the rng dev type supports the passed property name
|
|
|
|
"""
|
|
|
|
users = {
|
2017-08-05 14:39:32 +08:00
|
|
|
"type": [self.TYPE_EGD, self.TYPE_RANDOM],
|
|
|
|
"model": [self.TYPE_EGD, self.TYPE_RANDOM],
|
|
|
|
"bind_host": [self.TYPE_EGD],
|
|
|
|
"bind_service": [self.TYPE_EGD],
|
|
|
|
"connect_host": [self.TYPE_EGD],
|
|
|
|
"connect_service": [self.TYPE_EGD],
|
|
|
|
"backend_type": [self.TYPE_EGD],
|
|
|
|
"device": [self.TYPE_RANDOM],
|
|
|
|
"rate_bytes": [self.TYPE_EGD, self.TYPE_RANDOM],
|
|
|
|
"rate_period": [self.TYPE_EGD, self.TYPE_RANDOM],
|
2013-09-18 21:29:28 +08:00
|
|
|
}
|
|
|
|
if users.get(propname):
|
|
|
|
return self.type in users[propname]
|
|
|
|
|
|
|
|
return hasattr(self, propname)
|
|
|
|
|
2013-10-25 23:13:26 +08:00
|
|
|
def backend_mode(self):
|
|
|
|
ret = []
|
|
|
|
if self._has_mode_bind:
|
2018-03-21 00:18:35 +08:00
|
|
|
ret.append(DeviceRng.BACKEND_MODE_BIND)
|
2013-10-25 23:13:26 +08:00
|
|
|
if self._has_mode_connect:
|
2018-03-21 00:18:35 +08:00
|
|
|
ret.append(DeviceRng.BACKEND_MODE_CONNECT)
|
2013-10-25 23:13:26 +08:00
|
|
|
return ret
|
|
|
|
|
|
|
|
_XML_PROP_ORDER = ["_has_mode_bind", "_has_mode_connect"]
|
|
|
|
|
|
|
|
_has_mode_connect = XMLProperty("./backend/source[@mode='connect']/@mode")
|
|
|
|
def _set_connect_validate(self, val):
|
|
|
|
if val:
|
2018-03-21 00:18:35 +08:00
|
|
|
self._has_mode_connect = DeviceRng.BACKEND_MODE_CONNECT
|
2013-10-25 23:13:26 +08:00
|
|
|
return val
|
|
|
|
|
|
|
|
_has_mode_bind = XMLProperty("./backend/source[@mode='bind']/@mode")
|
|
|
|
def _set_bind_validate(self, val):
|
|
|
|
if val:
|
2018-03-21 00:18:35 +08:00
|
|
|
self._has_mode_bind = DeviceRng.BACKEND_MODE_BIND
|
2013-10-25 23:13:26 +08:00
|
|
|
return val
|
|
|
|
|
2015-09-06 05:20:43 +08:00
|
|
|
type = XMLProperty("./backend/@model")
|
|
|
|
model = XMLProperty("./@model", default_cb=lambda s: "virtio")
|
2013-09-18 21:29:28 +08:00
|
|
|
|
2015-09-06 05:20:43 +08:00
|
|
|
backend_type = XMLProperty("./backend/@type")
|
2013-10-25 23:13:26 +08:00
|
|
|
|
2015-09-06 05:20:43 +08:00
|
|
|
bind_host = XMLProperty("./backend/source[@mode='bind']/@host",
|
2013-10-25 23:13:26 +08:00
|
|
|
set_converter=_set_bind_validate)
|
2015-09-06 05:20:43 +08:00
|
|
|
bind_service = XMLProperty("./backend/source[@mode='bind']/@service",
|
2013-10-25 23:13:26 +08:00
|
|
|
set_converter=_set_bind_validate)
|
|
|
|
|
2015-09-06 05:20:43 +08:00
|
|
|
connect_host = XMLProperty("./backend/source[@mode='connect']/@host",
|
|
|
|
set_converter=_set_connect_validate)
|
|
|
|
connect_service = XMLProperty("./backend/source[@mode='connect']/@service",
|
|
|
|
set_converter=_set_connect_validate)
|
2013-09-18 21:29:28 +08:00
|
|
|
|
2015-09-06 05:20:43 +08:00
|
|
|
rate_bytes = XMLProperty("./rate/@bytes")
|
|
|
|
rate_period = XMLProperty("./rate/@period")
|
2013-09-18 21:29:28 +08:00
|
|
|
|
2017-03-09 06:48:26 +08:00
|
|
|
device = XMLProperty("./backend[@model='random']")
|