Implement DGRAM (connectionless) type socket listeners
PATCH 1/5 Implement DGRAM (connectionless) type socket listeners
In reply to a message not in the archiveIntroduce SocketDgramListener and UnixDgramListener classes. We already have STREAM (connection) type socket listener classes in the source tree, but we need DGRAM (connectionless) type listeners to receive udev events.
tools/python/xen/web/connection.py +37 -0
@@ -292,3 +292,40 @@ return True log.warn("Rejected connection from %s (%s).", addrport[0], fqdn) return False + + +class SocketDgramListener: + """A connectionless server socket, running listen in a thread. + """ + + def __init__(self, protocol_class): + self.protocol = protocol_class() + self.sock = self.createSocket() + threading.Thread(target=self.main).start() + + + def close(self): + try: + self.sock.close() + except: + pass + + + def createSocket(self): + raise NotImplementedError() + + + def main(self): + try: + while True: + try: + data = self.sock.recv(BUFFER_SIZE) + self.protocol.dataReceived(data) + except socket.error, ex: + if ex.args[0] not in (EWOULDBLOCK, EAGAIN, EINTR): + break + finally: + try: + self.close() + except: + pass
tools/python/xen/web/unix.py +23 -9
@@ -27,16 +27,19 @@ import connection -def bind(path): - """Create a Unix socket, and bind it to the given path. The socket is -created such that only the current user may access it.""" +def bind(path, type = socket.SOCK_STREAM): + """Create a Unix socket, and bind it to the given path. + The socket is created such that only the current user may access it.""" - parent = os.path.dirname(path) - mkdir.parents(parent, stat.S_IRWXU, True) - if os.path.exists(path): - os.unlink(path) + if path[0] == '\0': # Abstract namespace is used for the path + pass + else: + parent = os.path.dirname(path) + mkdir.parents(parent, stat.S_IRWXU, True) + if os.path.exists(path): + os.unlink(path) - sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + sock = socket.socket(socket.AF_UNIX, type) sock.bind(path) return sock
@@ -48,8 +51,19 @@ def createSocket(self): - return bind(self.path) + return bind(self.path, socket.SOCK_STREAM) def acceptConnection(self, sock, _): connection.SocketServerConnection(sock, self.protocol_class) + + +class UnixDgramListener(connection.SocketDgramListener): + def __init__(self, path, protocol_class): + self.path = path + connection.SocketDgramListener.__init__(self, protocol_class) + + + def createSocket(self): + return bind(self.path, socket.SOCK_DGRAM) +
PATCH 2/5 Add lock for xen-api class instances in XendAPIStore.py
In reply to a message not in the archiveAdd __classes_lock to protect __classes, since it can be modified by the udev listener thread.
tools/python/xen/xend/XendAPIStore.py +36 -13
@@ -25,36 +25,59 @@ by type, to ensure safety """ +import threading + __classes = {} +__classes_lock = threading.RLock() def register(uuid, type, inst): - __classes[(uuid, type)] = inst - return inst + __classes_lock.acquire() + try: + __classes[(uuid, type)] = inst + return inst + finally: + __classes_lock.release() def deregister(uuid, type): - old = get(uuid, type) - if old is not None: - del __classes[(uuid, type)] - return old + __classes_lock.acquire() + try: + old = get(uuid, type) + if old is not None: + del __classes[(uuid, type)] + return old + finally: + __classes_lock.release() def get(uuid, type): """ Get the instances by uuid and type """ - return __classes.get((uuid, type), None) + __classes_lock.acquire() + try: + return __classes.get((uuid, type), None) + finally: + __classes_lock.release() def get_all(all_type): """ Get all instances by type """ - return [inst - for ((uuid, t), inst) in __classes.items() - if t == all_type] + __classes_lock.acquire() + try: + return [inst + for ((uuid, t), inst) in __classes.items() + if t == all_type] + finally: + __classes_lock.release() def get_all_uuid(all_type): """ Get all uuids by type """ - return [uuid - for (uuid, t) in __classes.keys() - if t == all_type] + __classes_lock.acquire() + try: + return [uuid + for (uuid, t) in __classes.keys() + if t == all_type] + finally: + __classes_lock.release()
PATCH 3/5 Add lock for lspci_info in pci.py
In reply to a message not in the archiveAdd lspci_info_lock to protect lspci_info.
------
tools/python/xen/util/pci.py +35 -16
@@ -12,6 +12,7 @@ import types import struct import time +import threading from xen.util import utils PROC_PCI_PATH = '/proc/bus/pci/devices'
@@ -97,6 +98,7 @@ # Global variable to store information from lspci lspci_info = None +lspci_info_lock = threading.RLock() #Calculate PAGE_SHIFT: number of bits to shift an address to get the page number PAGE_SIZE = resource.getpagesize()
@@ -174,12 +176,16 @@ return pci_devs -def create_lspci_info(): +def _create_lspci_info(): + """Execute 'lspci' command and parse the result. + If the command does not exist, lspci_info will be kept blank ({}). + + Expects to be protected by lspci_info_lock. + """ global lspci_info + lspci_info = {} - # Execute 'lspci' command and parse the result. - # If the command does not exist, lspci_info will be kept blank ({}). for paragraph in os.popen(LSPCI_CMD + ' -vmm').read().split('\n\n'): device_name = None device_info = {}
@@ -194,6 +200,14 @@ pass if device_name is not None: lspci_info[device_name] = device_info + +def create_lspci_info(): + global lspci_info_lock + lspci_info_lock.acquire() + try: + _create_lspci_info() + finally: + lspci_info_lock.release() def save_pci_conf_space(devs_string): pci_list = []
@@ -911,22 +925,27 @@ Since we cannot obtain these data from sysfs, use 'lspci' command. """ global lspci_info + global lspci_info_lock - if lspci_info is None: - create_lspci_info() + lspci_info_lock.acquire() + try: + if lspci_info is None: + _create_lspci_info() - try: - device_info = lspci_info[self.name] - self.revision = int(device_info['Rev'], 16) - self.vendorname = device_info['Vendor'] - self.devicename = device_info['Device'] - self.classname = device_info['Class'] - self.subvendorname = device_info['SVendor'] - self.subdevicename = device_info['SDevice'] - except KeyError: - pass + try: + device_info = lspci_info[self.name] + self.revision = int(device_info['Rev'], 16) + self.vendorname = device_info['Vendor'] + self.devicename = device_info['Device'] + self.classname = device_info['Class'] + self.subvendorname = device_info['SVendor'] + self.subdevicename = device_info['SDevice'] + except KeyError: + pass - return True + return True + finally: + lspci_info_lock.release() def __str__(self): str = "PCI Device %s\n" % (self.name)
PATCH 4/5 Accept udev events and update physical resource information
In reply to a message not in the archiveThis is the main part of the patch series.
When a udev event is received, udevevent.py parses the udev data and tells XendNode.py to update the physical resource information.
This patch also add a boolean parameter 'xend-udev-event-server', to let users indicate whether we should enable this function or not.
------
tools/examples/xend-config.sxp +1 -0
@@ -64,6 +64,7 @@ #(xend-relocation-server no) (xend-relocation-server yes) #(xend-relocation-ssl-server no) +#(xend-udev-event-server no) #(xend-unix-path /var/lib/xend/xend-socket)
tools/python/xen/xend/XendNode.py +88 -4
134 lines not on this page · raw message
tools/python/xen/xend/XendOptions.py +7 -0
@@ -75,6 +75,9 @@ """Default for the flag indicating whether xend should run a ssl relocation server.""" xend_relocation_ssl_server_default = 'no' + """Default for the flag indicating whether xend should run a udev event server.""" + xend_udev_event_server_default = 'no' + """Default interface address the xend relocation server listens at. """ xend_relocation_address_default = ''
@@ -215,6 +218,10 @@ def get_xend_relocation_server_ssl_cert_file(self): return self.get_config_string("xend-relocation-server-ssl-cert-file") + + def get_xend_udev_event_server(self): + return self.get_config_bool("xend-udev-event-server", + self.xend_udev_event_server_default) def get_xend_port(self): """Get the port xend listens at for its HTTP interface.
tools/python/xen/xend/server/SrvDaemon.py +2 -0
@@ -24,6 +24,7 @@ from xen.util import mkdir import relocate +import udevevent import SrvServer from params import *
@@ -336,6 +337,7 @@ del xc relocate.listenRelocation() + udevevent.listenUdevEvent() servers = SrvServer.create() servers.start(status) del servers
tools/python/xen/xend/server/udevevent.py new file +68 -0
68 lines not on this page · raw message
PATCH 5/5 Add udev rules to deliver hw events to xend
In reply to a message not in the archive------
Makefile +2 -0
@@ -203,6 +203,8 @@ rm -rf $(D)/etc/hotplug/xen-backend.agent rm -f $(D)/etc/udev/rules.d/xen-backend.rules rm -f $(D)/etc/udev/xen-backend.rules + rm -f $(D)/etc/udev/rules.d/xend.rules + rm -f $(D)/etc/udev/xend.rules rm -f $(D)/etc/sysconfig/xendomains rm -rf $(D)/var/run/xen* $(D)/var/lib/xen* rm -rf $(D)/boot/*xen*
tools/hotplug/Linux/Makefile +1 -1
@@ -29,7 +29,7 @@ XEN_HOTPLUG_SCRIPTS = xen-backend.agent UDEV_RULES_DIR = /etc/udev -UDEV_RULES = xen-backend.rules +UDEV_RULES = xen-backend.rules xend.rules DI = $(if $(DISTDIR),$(shell readlink -f $(DISTDIR)),) DE = $(if $(DESTDIR),$(shell readlink -f $(DESTDIR)),)
tools/hotplug/Linux/xend.rules new file +3 -0
@@ -0,0 +1,3 @@ +SUBSYSTEM=="pci", RUN+="socket:/org/xen/xend/udev_event" +#SUBSYSTEM=="scsi", RUN+="socket:/org/xen/xend/udev_event" +#SUBSYSTEM=="net", KERNEL!="vif[0-9]*.[0-9]*|tap[0-9]*.[0-9]*", RUN+="socket:/org/xen/xend/udev_event"
Re: [PATCH 4/5] Accept udev events and update physicalresource information
In reply to Yosuke IwamatsuHi Yosuke,
I have a question.
Need the patch call save_PPCIs at the end of add_PPCI and the end of remove_PPCI?
Best regards,
Kan
Quoted 193 lines · Yosuke Iwamatsu
This is the main part of the patch series.
When a udev event is received,udevevent.pyparses the udev data and tellsXendNode.pyto update the physical resource information.
This patch also add a boolean parameter 'xend-udev-event-server', to let users
indicate whether we should enable this function or not.Signed-off-by Yosuke Iwamatsu <[email protected]>------
diff -r
c30742011bb8tools/examples/xend-config.sxptools/examples/xend-config.sxp +1
-0@@ -64,6 +64,7 @@ #(xend-relocation-server no) (xend-relocation-server yes) #(xend-relocation-ssl-server no) +#(xend-udev-event-server no) #(xend-unix-path /var/lib/xend/xend-socket)diff -r
c30742011bb8tools/python/xen/xend/XendNode.pytools/python/xen/xend/XendNode.py +19
-4@@ -146,6 +146,18 @@ self.srs = {} + self._init_networks() + self._init_PIFs() + + self._init_SRs() + self._init_PBDs() + + self._init_PPCIs() + + self._init_PSCSIs() + + + def _init_networks(self): # Initialise networks # First configure ones off disk saved_networks = self.state_store.load_state('network')@@ -179,6 +191,7 @@ if unconfigured_bridge != 'tmpbridge': XendNetwork.create_phy(unconfigured_bridge) + def _init_PIFs(self): # Initialise PIFs # First configure ones off disk saved_pifs = self.state_store.load_state('pif')@@ -221,7 +234,8 @@ log.debug("Cannot find network for bridge %s " "when configuring PIF %s", (bridge_name, name)) - + + def _init_SRs(self): # initialise storage saved_srs = self.state_store.load_state('sr') if saved_srs:@@ -240,6 +254,7 @@ qcow_sr_uuid = uuid.createString() self.srs[qcow_sr_uuid] = XendQCoWStorageRepo(qcow_sr_uuid) + def _init_PBDs(self): saved_pbds = self.state_store.load_state('pbd') if saved_pbds: for pbd_uuid, pbd_cfg in saved_pbds.items():@@ -247,8 +262,8 @@ XendPBD.recreate(pbd_uuid, pbd_cfg) except CreateUnspecifiedAttributeError: log.warn("Error recreating PBD %s", pbd_uuid) - - + + def _init_PPCIs(self): # Initialise PPCIs saved_ppcis = self.state_store.load_state('ppci') saved_ppci_table = {}@@ -282,7 +297,7 @@ ppci_uuid = saved_ppci_table.get(pci_dev.name, uuid. createString()) XendPPCI(ppci_uuid, ppci_record) - + def _init_PSCSIs(self): # Initialise PSCSIs saved_pscsis = self.state_store.load_state('pscsi')saved_pscsi_table = {}tools/python/xen/xend/XendNode.py +69
-0@@ -299,6 +314,75 @@ pscsi_uuid = saved_pscsi_table.get(pscsi_record['scsi_id'], uuid.createString()) XendPSCSI(pscsi_uuid, pscsi_record) + + + def add_network(self, interface): + # TODO + log.debug("add_network(): Not implemented.") + + + def remove_network(self, interface): + # TODO + log.debug("remove_network(): Not implemented.") + + + def add_PPCI(self, pci_name): + # Update lspci info + PciUtil.create_lspci_info() + + # Initialise the PPCI + saved_ppcis = self.state_store.load_state('ppci') + saved_ppci_table = {} + if saved_ppcis: + for ppci_uuid, ppci_record in saved_ppcis.items(): + try: + saved_ppci_table[ppci_record['name']] = ppci_uuid + except KeyError: + pass + + (domain, bus, slot, func) = PciUtil.parse_pci_name(pci_name) + pci_dev = PciUtil.PciDevice(domain, bus, slot, func) + ppci_record = { + 'domain': pci_dev.domain, + 'bus': pci_dev.bus, + 'slot': pci_dev.slot, + 'func': pci_dev.func, + 'vendor_id': pci_dev.vendor, + 'vendor_name': pci_dev.vendorname, + 'device_id': pci_dev.device, + 'device_name': pci_dev.devicename, + 'revision_id': pci_dev.revision, + 'class_code': pci_dev.classcode, + 'class_name': pci_dev.classname, + 'subsystem_vendor_id': pci_dev.subvendor, + 'subsystem_vendor_name': pci_dev.subvendorname, + 'subsystem_id': pci_dev.subdevice, + 'subsystem_name': pci_dev.subdevicename, + 'driver': pci_dev.driver + } + # If saved uuid exists, use it. Otherwise create one. + ppci_uuid = saved_ppci_table.get(pci_dev.name, uuid.createString()) + XendPPCI(ppci_uuid, ppci_record) + + + def remove_PPCI(self, pci_name): + # Update lspci info + PciUtil.create_lspci_info() + + # Remove the PPCI + (domain, bus, slot, func) = PciUtil.parse_pci_name(pci_name) + ppci_ref = XendPPCI.get_by_sbdf(domain, bus, slot, func) + XendAPIStore.get(ppci_ref, "PPCI").destroy() + + + def add_PSCSI(self): + # TODO + log.debug("add_network(): Not implemented.") + + + def remove_PSCSI(self): + # TODO + log.debug("add_network(): Not implemented.") ## def network_destroy(self, net_uuid):diff -r
c30742011bb8tools/python/xen/xend/XendOptions.pytools/python/xen/xend/XendOptions.py +0
-0@@ -75,6 +75,9 @@ """Default for the flag indicating whether xend should run a sslrelocation server."""
xend_relocation_ssl_server_default= 'no'+ """Default for the flag indicating whether xend should run a udev
event server."""tools/python/xen/xend/XendOptions.py +6
-0+ xend_udev_event_server_default = 'no' + """Default interface address the xend relocation server listens at. """ xend_relocation_address_default = ''@@ -215,6 +218,10 @@ def get_xend_relocation_server_ssl_cert_file(self): return self.get_config_string("xend-relocation-server-ssl-cert- file") + + def get_xend_udev_event_server(self): + return self.get_config_bool("xend-udev-event-server", + self.xend_udev_event_server_default) def get_xend_port(self):
Quoted 3 lines
"""Get the port xend listens at for its HTTP interface.
diff -rc30742011bb8tools/python/xen/xend/server/SrvDaemon.py
---a/tools/python/xen/xend/server/SrvDaemon.pyThu Mar 12 18:48:09
2009 +
Quoted 2 lines
0000
+++b/tools/python/xen/xend/server/SrvDaemon.pyFri Mar 13 16:38:17
2009 +
tools/python/xen/xend/server/udevevent.py
Quoted 20 lines
0900
tools/python/xen/xend/XendOptions.py +0
-0@@ -24,6 +24,7 @@from xen.util import mkdir
import relocate
+import udevevent
import SrvServer
from params import *tools/python/xen/xend/XendOptions.py +1
-0@@ -336,6 +337,7 @@ del xc relocate.listenRelocation() + udevevent.listenUdevEvent() servers = SrvServer.create() servers.start(status) del serversdiff -r
c30742011bb8tools/python/xen/xend/server/udevevent.pytools/python/xen/xend/server/udevevent.py new file +0
-0
2009 +
Quoted 78 lines
0900
tools/python/xen/xend/server/udevevent.py new file +68
-0@@ -0,0 +1,68 @@ +import socket + +from xen.web import protocol, unix + +from xen.xend.XendLogging import log +from xen.xend import XendNode +from xen.xend import XendOptions + +UDEV_EVENT_PATH = '\0/org/xen/xend/udev_event' + +class UdevEventProtocol(protocol.Protocol): + + def __init__(self): + protocol.Protocol.__init__(self) + + def dataReceived(self, data): + udev_event = {} + for entry in data.split('\0'): + try: + opt, val = entry.split("=") + udev_event[opt] = val + except (TypeError, ValueError): + pass + if udev_event.get('ACTION', None) is None: + log.warn("Invalid udev event received") + return + + log.debug("udev event received: %s", udev_event) + + self._process_event(udev_event) + + def _process_event(self, udev_event): + try: + if (udev_event.get('SUBSYSTEM', None) == 'pci'): + pci_name = udev_event.get('PCI_SLOT_NAME', None) + if (udev_event['ACTION'] == 'add'): + log.info("Adding pci device %s", pci_name) + XendNode.instance().add_PPCI(pci_name) + elif (udev_event['ACTION'] == 'remove'): + log.info("Removing pci device %s", pci_name) + XendNode.instance().remove_PPCI(pci_name) + + elif (udev_event.get('SUBSYSTEMS', None) == 'scsi'): + if (udev_event['ACTION'] == 'add'): + log.info("Adding scsi device") + XendNode.instance().add_PSCSI() + elif (udev_event['ACTION'] == 'remove'): + log.info("Removing scci device") + XendNode.instance().remove_PSCSI() + + elif (udev_event.get('SUBSYSTEM', None) == 'net'): + interface = udev_event.get('INTERFACE', None) + if (udev_event['ACTION'] == 'add'): + log.info("Adding net device %s", interface) + XendNode.instance().add_network(interface) + elif (udev_event['ACTION'] == 'remove'): + log.info("Removing net device %s", interface) + XendNode.instance().remove_network(interface) + + except Exception, e: + log.warn("error while processing udev event(): %s" % str(e)) + + +def listenUdevEvent(): + xoptions = XendOptions.instance() + if xoptions.get_xend_udev_event_server(): + unix.UnixDgramListener(UDEV_EVENT_PATH, UdevEventProtocol) +_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 1/1 xend: save the state of PPCIs after hot-plug ([PATCH 4/5] Accept udev events and update physical resource information)
Quoted 6 lines · Masaki Kanno
Hi Yosuke,
I have a question.
Need the patch call
save_PPCIsat the end ofadd_PPCIand the end ofremove_PPCI?
It seems you are right. I generated a patch to do that. Thank you.
-- Yosuke
--------
xend: Save the state of PPCIs after hot-plug events.
tools/python/xen/xend/XendNode.py +4 -0
@@ -363,6 +363,8 @@ ppci_uuid = saved_ppci_table.get(pci_dev.name, uuid.createString()) XendPPCI(ppci_uuid, ppci_record) + self.save_PPCIs() + def remove_PPCI(self, pci_name): # Update lspci info
@@ -372,6 +374,8 @@ (domain, bus, slot, func) = PciUtil.parse_pci_name(pci_name) ppci_ref = XendPPCI.get_by_sbdf(domain, bus, slot, func) XendAPIStore.get(ppci_ref, "PPCI").destroy() + + self.save_PPCIs() def add_PSCSI(self):
Re: [PATCH] xend: save the state of PPCIs after hot-plug ([PATCH 4/5] Accept udev events and update physical resource information)
Hi Yosuke,
Thanks for your reply.
I have felt sure of my thought by your reply.
I have written a patch for SCSI devices. The patch accepts udev events about SCSI devices, then it updates physical SCSI information. I'm going to send the patch to xen-devel.
Best regards,
Kan
Quoted 48 lines · Yosuke Iwamatsu
Quoted 7 lines · Masaki Kanno
Hi Yosuke,
I have a question.
Need the patch call
save_PPCIsat the end ofadd_PPCIand the end ofremove_PPCI?It seems you are right. I generated a patch to do that. Thank you.
-- Yosuke
--------
xend: Save the state of PPCIs after hot-plug events.
Signed-off-by Yosuke Iwamatsu <[email protected]>diff -r
0477f9061c8atools/python/xen/xend/XendNode.pytools/python/xen/xend/XendNode.py +4
-0@@ -363,6 +363,8 @@ ppci_uuid = saved_ppci_table.get(pci_dev.name, uuid.createString()) XendPPCI(ppci_uuid, ppci_record) + self.save_PPCIs() + def remove_PPCI(self, pci_name): # Update lspci info@@ -372,6 +374,8 @@ (domain, bus, slot, func) = PciUtil.parse_pci_name(pci_name) ppci_ref = XendPPCI.get_by_sbdf(domain, bus, slot, func) XendAPIStore.get(ppci_ref, "PPCI").destroy() + + self.save_PPCIs() def add_PSCSI(self):_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel